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))
)
)
)
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.
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.