AI L21 Prolog PDF
AI L21 Prolog PDF
AI L21 Prolog PDF
Pl
?-
Welcome to SWI-Prolog……
For help, use ?- help(Topic).
Structure of a Prolog Program
• Every goal in Prolog ends with dot
• A Prolog program consists of a KB of facts and rules where:
• Fact: head but no body
man(Marcus).
man(Plato).
• Rules: head and body
mortal(X) :- man(X).
• Questions: body but no head
mortal(Marcus).
Use ``;'' to get next possible answer
Yes means true with no variables, no means not consistent with database.
Clauses: Facts and Rules
‘if’
‘provided that’
‘turnstile’
Goals
Example: A very simple Program(Only facts)
Facts:
likes(john,flowers).
likes(john,mary).
likes(paul,mary).
Question:
?- likes(john,X)
Answer:
X=flowers and wait
;
mary
;
no
Example: A very simple Program(facts and
rules)
Facts:
likes(john,flowers). likes(john,mary).likes(paul,mary).likes(flowers, mary).
likes(john,X):-likes(X, mary).
Query: ?likes(john,paul) yes
Query: ?likes(john,mary) no
Query: ?likes(john,X)
X =john;
X = paul;
X = flowers;
No
Query: ?likes(X,paul)
no
Logical Operators in Prolog
a :- b. /* a if b */
a :- b,c. /* a if b and c. */
a :- b;c. /* a if b or c. */
a :- not b. /* a if b fails */
a :- b -> c;d. /* a if (if b then c else d) */
Interpretation of Clauses
Clauses can be given a declarative reading or a procedural
reading.
P :- Q; R, S.
• ancestor(X,Z):- parent(X,Z); parent(X,Y), ancestor(Y,Z).
Conjunctions
• Use ‘,’ and pronounce it as and.
• Example
• Facts:
• likes(mary,food).
• likes(mary,tea).
• likes(john,tea).
• likes(john,mary)
• ?-
• likes(mary,X),likes(john,X).
• Meaning is anything liked by Mary also liked by John?
Backtracking (an inherent property of prolog
programming)
Unification with backtracking
likes(mary,X),likes(john,X)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
likes(mary,X),likes(john,X)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
likes(mary,X),likes(john,X)
likes(mary,food)
likes(mary,tea)
likes(john,tea)
likes(john,mary)
X is a sister of Y is
X is a female and
X and Y have same parents
Question Answering in presence of rules
• Facts
• male (ram).
• male (shyam).
• female (sita).
• female (gita).
• parents (shyam, gita, ram).
• parents (sita, gita, ram).
Question Answering: Y/N type: is sita the sister of
shyam?
?- sister_of (sita, shyam)
parents(sita,M,F) parents(shyam,M,F)
female(sita)
parents(shyam,gita,ram)
parents(sita,gita,ram)
success
Question Answering: wh-type: whose sister is sita?
?- ?- sister_of (sita, X)
parents(sita,M,F) parents(Y,M,F)
female(sita)
parents(Y,gita,ram)
parents(sita,gita,ram)
parents(shyam,gita,ram)
Success
Y=shyam
Exercise
1. From the above it is possible for somebody to be her own sister.
How can this be prevented?
Another Example to recap the control flow
r(a).
s(b,c).
m(b).
n(a).
q(X):- m(X).
q(X):- n(X).
p(X,Y) :- q(X), r(Y).
p(X,Y) :- r(X), s(X,Y).
Goal is: p(a,Y)
How will Prolog respond to the query if we
keep entering..
p(a,b).
P(b,c).
P(X,Y):-p(Y,X).
Query:
?-p(X,Y).
Unification
• Two terms unify if substitutions can be made for any variables in the terms so
that the terms are made identical. If no such substitution exists, the terms do not
unify.
• The Unification Algorithm proceeds by recursive descent of the two terms.
• Constants unify if they are identical
• Variables unify with any term, including other variables
• Compound terms unify if their functors and components unify.
Examples
The terms f(X, a(b,c)) and f(d, a(Z, c)) unify.
f
f d a
X a
Z c
b c
g g
Z f + 17 C f C E
A B
A 17 B D D E
Second lecture of Prolog
Writing a Prolog program for following facts
and rules
(1) Jia is a woman. woman(jia).
(2) John is a man. man(john)
(3) John is healthy. healthy(john)
(4) Jia is healthy. healthy(jia)
(5) John is wealthy. wealthy(john)
(6) Anyone is a traveler if he is healthy and wealthy.
traveler(X):- healthy(X), wealthy(X).
append([Head|Tail],List2,[Head|Result]):-
append(Tail,List2,Result).
• member(X,[X|_]).
Note: _ is called anonymous variable, which can be unified with any
value
• member(X, [_|Y]):- member(X,Y).
Prolog Program Flow, BackTracking and
Cut
Controlling the program flow
How Prolog computes?
• Depth First Search: to find a goal
• Pursues a goal till the end
• Conditional AND; falsity of any goal prevents satisfaction of further
clauses.
• Conditional OR; satisfaction of any goal prevents further clauses being
evaluated.
Control flow (top level)
Given
g:- a, b, c. (1)
g:- d, e, f; g. (2)
If prolog cannot satisfy (1), control will automatically fall through to (2).
Control Flow within a rule
Taking (1): g:- a, b, c.
• If a succeeds, prolog will try to satisfy b, succeeding which, c will be
tried.
• For ANDed clauses, control flows forward till the ‘.’, iff the current
clause is true.
• For ORed clauses, control flows forward till the ‘.’, iff the current
clause evaluates to false.
Fundamental Principle of prolog programming
DO NOT BACKTRACK