SlideShare a Scribd company logo
CSE240 – Introduction to
Programming Languages
Lecture 22:
Programming with Prolog I
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Test Yourselves
LISP
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Example 1
; A function that return the leftmost even member."
(defun find-even (L)
(if (null L)
nil
(if (evenp (first L))
(first L)
(find-even (rest L))
)
)
)
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Example 2
; develop recursive functions that traverse a list
; and count its elements
(defun recursive-list-length (L)
(if (null L)
0
(1+ (recursive-list-length (rest L)))
)
)
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Example 3
; defines a function (get N L) that returns the N member
; of list L –assuming that the elements are numbered from ; zero
onwards
(defun get (N L)
(if (null L)
nil
(if (zerop N)
(first L)
(get (1- N) (rest L))
)
)
)
Logic Programming Paradigm
PROLOG
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
Paradigms
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
Logic Paradigm
• It focus on what is needed (requirements), instead of how to
implement them. It just describe what the problem is and let the
computer find a way to solve the problem, instead describing how to
solve the problem.
• It rely on the environment to find solutions that meet requirements.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Predicate Logic
• It uses a simplified variation of predicate logic syntax, which is similar
to natural language.
• Predicate logic eliminate all unnecessary words from sentences, then
transform the sentence by placing the relationship (predicate) first
and grouping the objects (arguments) after the relationship
predicate (object, object, object ....)
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Natural Language Predicate Logic Type of Predicate
A car is fast. fast(car). fact
A rose is red. red(rose). fact
Bill likes the car if the car is fast. likes(bill, car) :- fast(car). rule
Humidity is high if it rains high(humidity):- rains(). rule
Jane is mother of Elaine mother_of(jane, elaine). fact
Jane is mother of Mike mother_of(jane, mike). fact
David is father of Jesse father_of(david, jesse). fact
Jesse is father of Obed father_of(jesse, obed). fact
Grandmother X of Z if (X is mother of Y
and (Y is mother of Z or Y is father of Z)
grandmother_of(X, Z) :- mother_of(X, Y),
( mother_of(Y, Z); father_of(Y, Z) ).
rule
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11
Predicate Logic
• Facts: What is known, e.g.,
Bill likes car and bike, and he travels with one of them
likes(bill, car), likes(bill, bike)
travels(bill, car); travels(bill, bike)
• Rules: What you can infer from the given facts. Rules enable you to
infer facts from other facts, e.g.,
Bill is the father of Joe, if Joe is the son of bill.
father(bill, joe) :- son(joe, bill).
, means AND
; means OR
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 12
Prolog
• PROLOG (PROgramming LOGic)
• Interpreter.
• Deductive database: set of statements and a deduction system.
• Facts and Rules
• Queries
• Upper-Case variables
• Lower-Case constants and names
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
Install a Prolog Interpreter
SWI - Prolog
http://www.swi-prolog.org/download/stable
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14
Install a Prolog Interpreter
To enter rules from the command line, type this :
[user].
This puts you in a mode where you can enter facts and rules. Otherwise you get this kind of
message:
ERROR: Undefined procedure: (DWIM could not correct goal)
You can then enter facts (or rules) e.g. :
father(me,sarah).
After having entered the knowledge, type CONTROL-D to come back to the mode where you
can enter questions. Then you can ask:
?- father(me,X).
X = sarah
To quit:
halt.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15
Online Prolog Interpreter
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16
Summary
Logic/Declarative Paradigm
Key idea:
A program is a set of facts about
objects,
rules about objects, and questions
(queries) about the relations between
objects.
Features:
• get rid of programming altogether.
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.

More Related Content

PDF
201801 CSE240 Lecture 24
PDF
201801 CSE240 Lecture 23
PDF
201707 CSE110 Lecture 33
PPTX
ProLog (Artificial Intelligence) Introduction
PPTX
NICE Implementations of Variational Inference
PPTX
NICE Research -Variational inference project
ODP
Querying your database in natural language by Daniel Moisset PyData SV 2014
ODP
Quepy
201801 CSE240 Lecture 24
201801 CSE240 Lecture 23
201707 CSE110 Lecture 33
ProLog (Artificial Intelligence) Introduction
NICE Implementations of Variational Inference
NICE Research -Variational inference project
Querying your database in natural language by Daniel Moisset PyData SV 2014
Quepy

Similar to 201801 CSE240 Lecture 22 (20)

PDF
201707 CSE110 Lecture 05
PDF
201801 CSE240 Lecture 21
PDF
201707 CSE110 Lecture 08
PDF
201707 CSE110 Lecture 04
PPT
Claire98
PPTX
Stochastic Processes Homework Help
PPTX
Stochastic Processes Homework Help
PDF
201801 CSE240 Lecture 19
PPT
Pl vol1
PDF
201707 CSE110 Lecture 15
PDF
Real World Haskell: Lecture 2
PPT
cs344-lect15-robotic-knowledge-inferencing-prolog-11feb08.ppt
PDF
10 logic+programming+with+prolog
PDF
201801 CSE240 Lecture 01
ODP
Scala as a Declarative Language
PPTX
Word2vec slide(lab seminar)
PPT
Introduction to lambda calculus
PDF
Types Working for You, Not Against You
PPTX
Prolog 7-Languages
201707 CSE110 Lecture 05
201801 CSE240 Lecture 21
201707 CSE110 Lecture 08
201707 CSE110 Lecture 04
Claire98
Stochastic Processes Homework Help
Stochastic Processes Homework Help
201801 CSE240 Lecture 19
Pl vol1
201707 CSE110 Lecture 15
Real World Haskell: Lecture 2
cs344-lect15-robotic-knowledge-inferencing-prolog-11feb08.ppt
10 logic+programming+with+prolog
201801 CSE240 Lecture 01
Scala as a Declarative Language
Word2vec slide(lab seminar)
Introduction to lambda calculus
Types Working for You, Not Against You
Prolog 7-Languages
Ad

More from Javier Gonzalez-Sanchez (20)

PDF
201804 SER332 Lecture 01
PDF
201801 SER332 Lecture 03
PDF
201801 SER332 Lecture 04
PDF
201801 SER332 Lecture 02
PDF
201801 CSE240 Lecture 26
PDF
201801 CSE240 Lecture 25
PDF
201801 CSE240 Lecture 20
PDF
201801 CSE240 Lecture 18
PDF
201801 CSE240 Lecture 17
PDF
201801 CSE240 Lecture 16
PDF
201801 CSE240 Lecture 15
PDF
201801 CSE240 Lecture 14
PDF
201801 CSE240 Lecture 13
PDF
201801 CSE240 Lecture 12
PDF
201801 CSE240 Lecture 11
PDF
201801 CSE240 Lecture 10
PDF
201801 CSE240 Lecture 09
PDF
201801 CSE240 Lecture 08
PDF
201801 CSE240 Lecture 07
PDF
201801 CSE240 Lecture 06
201804 SER332 Lecture 01
201801 SER332 Lecture 03
201801 SER332 Lecture 04
201801 SER332 Lecture 02
201801 CSE240 Lecture 26
201801 CSE240 Lecture 25
201801 CSE240 Lecture 20
201801 CSE240 Lecture 18
201801 CSE240 Lecture 17
201801 CSE240 Lecture 16
201801 CSE240 Lecture 15
201801 CSE240 Lecture 14
201801 CSE240 Lecture 13
201801 CSE240 Lecture 12
201801 CSE240 Lecture 11
201801 CSE240 Lecture 10
201801 CSE240 Lecture 09
201801 CSE240 Lecture 08
201801 CSE240 Lecture 07
201801 CSE240 Lecture 06
Ad

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PPTX
L1 - Introduction to python Backend.pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
history of c programming in notes for students .pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
medical staffing services at VALiNTRY
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
top salesforce developer skills in 2025.pdf
PDF
Digital Strategies for Manufacturing Companies
Nekopoi APK 2025 free lastest update
L1 - Introduction to python Backend.pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Illustrator 28.6 Crack My Vision of Vector Design
history of c programming in notes for students .pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Which alternative to Crystal Reports is best for small or large businesses.pdf
CHAPTER 2 - PM Management and IT Context
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Design an Analysis of Algorithms II-SECS-1021-03
Softaken Excel to vCard Converter Software.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Odoo POS Development Services by CandidRoot Solutions
Design an Analysis of Algorithms I-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
VVF-Customer-Presentation2025-Ver1.9.pptx
medical staffing services at VALiNTRY
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
top salesforce developer skills in 2025.pdf
Digital Strategies for Manufacturing Companies

201801 CSE240 Lecture 22

  • 1. CSE240 – Introduction to Programming Languages Lecture 22: Programming with Prolog I Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3 Example 1 ; A function that return the leftmost even member." (defun find-even (L) (if (null L) nil (if (evenp (first L)) (first L) (find-even (rest L)) ) ) )
  • 4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4 Example 2 ; develop recursive functions that traverse a list ; and count its elements (defun recursive-list-length (L) (if (null L) 0 (1+ (recursive-list-length (rest L))) ) )
  • 5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5 Example 3 ; defines a function (get N L) that returns the N member ; of list L –assuming that the elements are numbered from ; zero onwards (defun get (N L) (if (null L) nil (if (zerop N) (first L) (get (1- N) (rest L)) ) ) )
  • 7. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7 Paradigms
  • 8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8 Logic Paradigm • It focus on what is needed (requirements), instead of how to implement them. It just describe what the problem is and let the computer find a way to solve the problem, instead describing how to solve the problem. • It rely on the environment to find solutions that meet requirements.
  • 9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9 Predicate Logic • It uses a simplified variation of predicate logic syntax, which is similar to natural language. • Predicate logic eliminate all unnecessary words from sentences, then transform the sentence by placing the relationship (predicate) first and grouping the objects (arguments) after the relationship predicate (object, object, object ....)
  • 10. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10 Natural Language Predicate Logic Type of Predicate A car is fast. fast(car). fact A rose is red. red(rose). fact Bill likes the car if the car is fast. likes(bill, car) :- fast(car). rule Humidity is high if it rains high(humidity):- rains(). rule Jane is mother of Elaine mother_of(jane, elaine). fact Jane is mother of Mike mother_of(jane, mike). fact David is father of Jesse father_of(david, jesse). fact Jesse is father of Obed father_of(jesse, obed). fact Grandmother X of Z if (X is mother of Y and (Y is mother of Z or Y is father of Z) grandmother_of(X, Z) :- mother_of(X, Y), ( mother_of(Y, Z); father_of(Y, Z) ). rule
  • 11. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11 Predicate Logic • Facts: What is known, e.g., Bill likes car and bike, and he travels with one of them likes(bill, car), likes(bill, bike) travels(bill, car); travels(bill, bike) • Rules: What you can infer from the given facts. Rules enable you to infer facts from other facts, e.g., Bill is the father of Joe, if Joe is the son of bill. father(bill, joe) :- son(joe, bill). , means AND ; means OR
  • 12. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 12 Prolog • PROLOG (PROgramming LOGic) • Interpreter. • Deductive database: set of statements and a deduction system. • Facts and Rules • Queries • Upper-Case variables • Lower-Case constants and names
  • 13. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13 Install a Prolog Interpreter SWI - Prolog http://www.swi-prolog.org/download/stable
  • 14. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14 Install a Prolog Interpreter To enter rules from the command line, type this : [user]. This puts you in a mode where you can enter facts and rules. Otherwise you get this kind of message: ERROR: Undefined procedure: (DWIM could not correct goal) You can then enter facts (or rules) e.g. : father(me,sarah). After having entered the knowledge, type CONTROL-D to come back to the mode where you can enter questions. Then you can ask: ?- father(me,X). X = sarah To quit: halt.
  • 15. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15 Online Prolog Interpreter
  • 16. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16 Summary Logic/Declarative Paradigm Key idea: A program is a set of facts about objects, rules about objects, and questions (queries) about the relations between objects. Features: • get rid of programming altogether.
  • 17. CSE240 – Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.