Professional Documents
Culture Documents
ch03 Prolog
ch03 Prolog
Amey Karkare
karkare @ cse.iitk.ac.in
Oct-21 p.1/41
Goals
Textbook:
Chapter 15, Programming Languages, Tucker and Noonan
Some interpreters:
https://www.swi-prolog.org/
http://xsb.sourceforge.net/
Oct-21 p.2/41
Logic and Horn Clauses
Oct-21 p.2/41
Logic and Horn Clauses
Example:
Oct-21 p.2/41
Logic and Horn Clauses
Example:
Oct-21 p.2/41
Logic and Horn Clauses
Oct-21 p.2/41
Resolution and Unification
Oct-21 p.2/41
Resolution and Unification
Oct-21 p.2/41
Resolution and Unification
Oct-21 p.2/41
Resolution: Example
Oct-21 p.2/41
Resolution: Example
speaks(Mary, English)
speaks(Ram, Hindi)
speaks(Ana, English)
Oct-21 p.2/41
Logic Programming in Prolog
Oct-21 p.2/41
Prolog System
Prolog
Programmer Data
Base
Prolog
User
Query Interpreter Answer
Oct-21 p.3/41
A Real Life Example
Oct-21 p.4/41
A Real Life Example
Oct-21 p.4/41
A Real Life Example (Contd. . . )
• Simple Queries:
Oct-21 p.5/41
A Real Life Example (Contd. . . )
• Simple Queries:
◦ Who teaches CS 350?
Oct-21 p.5/41
A Real Life Example (Contd. . . )
• Simple Queries:
◦ Who teaches CS 350?
◦ What is the course offered by Prof. rkj?
Oct-21 p.5/41
A Real Life Example (Contd. . . )
• Simple Queries:
◦ Who teaches CS 350?
◦ What is the course offered by Prof. rkj?
◦ Who are the students enrolled in CS 638?
Oct-21 p.5/41
A Real Life Example (Contd. . . )
• Simple Queries:
◦ Who teaches CS 350?
◦ What is the course offered by Prof. rkj?
◦ Who are the students enrolled in CS 638?
• Complex Queries:
Oct-21 p.5/41
A Real Life Example (Contd. . . )
• Simple Queries:
◦ Who teaches CS 350?
◦ What is the course offered by Prof. rkj?
◦ Who are the students enrolled in CS 638?
• Complex Queries:
◦ List all professors who teach student with roll # 5044.
Oct-21 p.5/41
A Real Life Example (Contd. . . )
• Simple Queries:
◦ Who teaches CS 350?
◦ What is the course offered by Prof. rkj?
◦ Who are the students enrolled in CS 638?
• Complex Queries:
◦ List all professors who teach student with roll # 5044.
◦ What are all the courses offered by Prof. uday?
Oct-21 p.5/41
A Real Life Example (Contd. . . )
• Simple Queries:
◦ Who teaches CS 350?
◦ What is the course offered by Prof. rkj?
◦ Who are the students enrolled in CS 638?
• Complex Queries:
◦ List all professors who teach student with roll # 5044.
◦ What are all the courses offered by Prof. uday?
◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course
(Of course, other than CS638).
Oct-21 p.5/41
A Real Life Example (Contd. . . )
• Simple Queries:
◦ Who teaches CS 350?
◦ What is the course offered by Prof. rkj?
◦ Who are the students enrolled in CS 638?
• Complex Queries:
◦ List all professors who teach student with roll # 5044.
◦ What are all the courses offered by Prof. uday?
◦ Make pairs of students enrolled in CS 638, such that
both students in a pair are not enrolled in same course
(Of course, other than CS638).
Oct-21 p.5/41
Time to learn Prolog syntax!
Oct-21 p.6/41
Prolog Syntax: Atoms
Oct-21 p.7/41
Prolog Syntax: Atoms
Oct-21 p.7/41
Prolog Syntax: Atoms
Oct-21 p.7/41
Prolog Syntax: Atoms
Oct-21 p.7/41
Prolog Syntax: Numbers
Oct-21 p.8/41
Prolog Syntax: Numbers
Oct-21 p.8/41
Prolog Syntax: Numbers
Oct-21 p.8/41
Prolog Syntax: Variables
Oct-21 p.9/41
Prolog Syntax: Variables
Oct-21 p.9/41
Prolog Syntax: Variables
Oct-21 p.9/41
Prolog Syntax: Variables
Oct-21 p.9/41
Prolog Syntax: Complex Terms
Oct-21 p.10/41
Prolog Syntax: Complex Terms
Oct-21 p.10/41
Prolog Syntax: Complex Terms
Oct-21 p.10/41
Prolog Syntax: Facts
Oct-21 p.11/41
Example: Facts
Oct-21 p.12/41
Example: Facts (Contd. . . )
% enrolled(+Course, +RollNo):
% database of students enrolled in courses
enrolled(cs638, 5007).
enrolled(cs638, 5020).
enrolled(cs638, 5026).
enrolled(cs638, 5601).
enrolled(cs638, 5044).
enrolled(cs638, 5401).
.
.
enrolled(cs612, 5601).
enrolled(cs612, 5401).
enrolled(cs612, 5044).
enrolled(cs612, 5003).
enrolled(cs612, 5023).
enrolled(cs612, 5024).
.
.
Oct-21 p.13/41
Prolog Syntax: Queries
Oct-21 p.14/41
Prolog Syntax: Queries
Oct-21 p.14/41
Prolog Syntax: Queries
Oct-21 p.14/41
Prolog Quick-start
Oct-21 p.15/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 Proo
lg p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Example: Queries
Oct-21 p.16/41
Points to Note
Oct-21 p.17/41
Prolog Syntax: Rules
Oct-21 p.18/41
Example: Rules
• Conjunction
teaches(Prof, Student) :-
offered by(Course, Prof),
enrolled(Course, Student).
Oct-21 p.19/41
Example: Rules
• Disjunction
likes compiler(Student) :-
enrolled(cs638, Student).
likes compiler(Student) :-
enrolled(cs613, Student).
Oct-21 p.20/41
Example: Rules
• Disjunction
likes compiler(Student) :-
enrolled(cs638, Student).
likes compiler(Student) :-
enrolled(cs613, Student).
• Attention:
Check that the different bodies of disjunctive clauses are:
Oct-21 p.20/41
Example: Rules
• Disjunction
likes compiler(Student) :-
enrolled(cs638, Student).
likes compiler(Student) :-
enrolled(cs613, Student).
• Attention:
Check that the different bodies of disjunctive clauses are:
◦ Mutually exclusive - unless you are really sure you want
multiple, non-deterministic answers
Oct-21 p.20/41
Example: Rules
• Disjunction
likes compiler(Student) :-
enrolled(cs638, Student).
likes compiler(Student) :-
enrolled(cs613, Student).
• Attention:
Check that the different bodies of disjunctive clauses are:
◦ Mutually exclusive - unless you are really sure you want
multiple, non-deterministic answers
◦ Exhaustive - cover all the cases, possibly implicitly
Oct-21 p.20/41
Example: Rules
• Negation
dislikes compiler(Student) :-
not(likes compiler(Student)).
Oct-21 p.21/41
Example: Rules
• Negation
dislikes compiler(Student) :-
not(likes compiler(Student)).
• Warning:
Negation may not work as expected (Why?)
Oct-21 p.21/41
Matching
Oct-21 Proo
lg p.22/41
Matching
Oct-21 p.22/41
Matching: Atoms, Variables
X = cs638
Z = Y
W = left(right(building(bricks,walls)))
_ = 5004
Oct-21 p.23/41
Matching: Complex Terms
left(X) = left(right(building(bricks)))
id(1, 1) = id(X, X)
Oct-21 p.24/41
Prolog: Arithmetic
• Arithmetic operators:
+, -, *, /, // (integer division), mod
Oct-21 p.25/41
Prolog: Arithmetic
• Arithmetic operators:
+, -, *, /, // (integer division), mod
| ?- X = 3+2.
X = 3 + 2
yes
Oct-21 p.25/41
Prolog: Arithmetic
• Arithmetic operators:
+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.
| ?- X = 3+2.
X = 3 + 2
yes
Oct-21 p.25/41
Prolog: Arithmetic
• Arithmetic operators:
+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.
| ?- X = 3+2.
X = 3 + 2
yes
| ?- Y is mod(3,4).
Y = 3
yes
Oct-21 p.25/41
Prolog: Arithmetic
• Arithmetic operators:
+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.
• Only right hand side of is is evaluated.
| ?- X = 3+2.
X = 3 + 2
yes
| ?- Y is mod(3,4).
Y = 3
yes
Oct-21 Proo
lg p.25/41
Prolog: Arithmetic
• Arithmetic operators:
+, -, *, /, // (integer division), mod
• is operator is required to force evaluation.
• Only right hand side of is is evaluated.
| ?- X = 3+2.
X = 3 + 2
yes
| ?- Y is mod(3,4).
Y = 3
yes
| ?- 5 is 6 - 1.
yes
| ?- 6 - 1 is 5.
no
Oct-21 p.25/41
Prolog: More Arithmetic
• Comparison operators:
<, =<, >=, >, =:=, =\=
Oct-21 p.26/41
Prolog: More Arithmetic
• Comparison operators:
<, =<, >=, >, =:=, =\=
• Both sides of the operators are evaluated.
| ?- 2+3 < 5+1.
yes
| ?- X < 3+3.
++Error[XSB/Runtime/P]: [Instantiation] Error:
Uninstantiated Arithmetic Expression
| ?- X = 4, X < 3+3.
X = 4
yes
Oct-21 p.26/41
Recursion
Oct-21 p.27/41
Recursion
path(X, Y) :-
edge(X, Y).
path(X, Y) :-
edge(X, Z),
path(Z, Y).
Oct-21 p.27/41
Recursion
ancestor(X, Y) :-
father(X, Z),
ancestor(Z, Y).
Oct-21 p.27/41
Recursion
ancestor(X, Y) :-
ancestor(X, Z),
ancestor(Z, Y).
father(ram, dashrath).
father(lav, ram).
father(kush, ram).
father(sita, janak).
Oct-21 p.27/41
Exercise
Oct-21 p.27/41
Lists
Oct-21 p.28/41
List Examples
Oct-21 p.29/41
Lists (Contd. . . )
•
Most important (Only!) data structure in Prolog
•
Matching a list: | operator separates a list into its head and tail.
Head/tail matched using prolog matching rules.
| ?- [H | T] = [1,2,3,4].
H = 1
T = [2,3,4]
yes
| ?- [H | [2,3,4]] = [1,2,3,4].
H = 1
yes
Oct-21 p.31/41
Working with Lists
Oct-21 p.32/41
Working with Lists: Example Revisited
Oct-21 p.31/41
Working with Lists: Example Revised
Oct-21 p.33/41
Working with Lists: Exercise
% member(+Element, +List)
% true if Element belongs to List.
% subset(+Subset, +Set).
% true if Subset is a subset of Set.
% Set and Subset are given as lists.
Oct-21 p.34/41
Working with Lists: Solutions
subset([], S).
subset([E|Es], S):-
member(E, S),
subset(Es, S).
Oct-21 p.35/41
Working with Lists: Solutions
subset([], S).
subset([E|Es], S):-
member(E, S),
subset(Es, S).
Note:
• [] case handled implicitly in member.
• member rules not mutually exclusive.
• In subset, case of not(member(E, S)) handled
implicitly.
Oct-21 p.35/41
Working with Lists: Another Example
Unnecessary multiple
calls to prime(…)
Oct-21 p.33/41
How to Avoid Unnecessary Duplicate Computation?
• CUT !
• Controls Backtracing
𝐵 → 𝐶% , … , 𝐶&'% , !, 𝐶&(% , … , 𝐶)
• The cut tells control to backtrack past 𝐶&'% , … , 𝐶% , 𝐵 without
considering any remaining rules for them.
• Usage?
• Prune parts of Prolog search that cannot possibly reach a solution
Oct-21 p.36/41
Working with Lists: Another Example
Oct-21 p.33/41
More Points to Note
Oct-21 p.36/41
More Points to Note
Oct-21 p.36/41
More Points to Note
Oct-21 p.36/41
Collecting Solutions
Oct-21 p.37/41
Collecting Solutions
Oct-21 p.37/41
Collecting Solutions
Oct-21 p.37/41
Collecting Solutions: Example
% powerset(+Set, -PowerSet).
% compute Powerset of a Set.
powerset(S, PS) :-
findall(SubS, subset(SubS, S), PS).
Oct-21 p.38/41
Topics Not Covered
Oct-21 p.40/41
Happy Prologging
Oct-21 p.41/41