Artificial Intelligence: Lecture 11: Prolog-I
Artificial Intelligence: Lecture 11: Prolog-I
e.g.
Prolog as Logic
Logic formula:
A Simple Rule
English description:
If
X is male,
F is the father of X,
M is the mother of X,
F is the father of Y,
M is the mother of Y
Then
X is a brother of Y
Logic formula:
male(X)
father(F, X)
mother(M, X)
father(F, Y)
mother(M, Y)
brother(X, Y)
Prolog:
brother(X, Y) :male(X),
father(F,X),
mother(M,X),
father(F,Y),
mother(M, Y).
A Simple Program
A Prolog program is a sequence of facts and rules
brother(X, Y) :- male(X), parent(P, X), parent(P, Y).
parent(X, Y):- father(X, Y); mother(X, Y).
male(philip).
father(bill, philip).
father(bill, jane).
?- brother(philip, jane).
Program Execution
Start from the question
Matching match the question with facts and rules by
substitution (try)
Unification looking for unified solution (consistent
solution)
Backtracking once fails, go back to try another case
Divide-and-conquer
male(philip)
parent(P, philip)
parent(P, jane)
X=philip
father(P,philip)
mother(P,philip)
P=bill
male(philip)
father(bill,philip)
father(P,jane)
P=bill
father(bill, jane)
mother(P,jane)
Another Example
Facts
mother(jane, george).
father(john, george).
brother(bill, john).
Rules:
parent(X, Y) :- mother(X, Y).
parent(X, Y) :- father(X, Y).
uncle(X, Y) :- parent(P, Y), brother(X, P).
Question:
?- uncle(X, george).
parent(P, Y)
brother(X, P)
X/bill, P/john
P/X, Y/george
mother(X, Y)
X/jane
mother(jane, george)
father(X, Y)
X/john
father(john, george)
brother(bill, john)
Recursion
Base case a set of rules for direct solution
Normal case another set of rules for indirection
solution
E.g.
child(X, Y) :- mother(Y, X).
child(X, Y) :- father(Y, X).
descendant(X, Y) :- child(X, Y). base case
descendent(X, Y) :- child(X, Z), descendent(Z, Y).
predecessor(X,Z) :parent(X,Y),
predecessor(Y,Z).
Family Tree
parent(pam,bob).
parent(tom,bob).
parent(tom,liz).
parent(bob,ann).
parent(bob,pat).
parent(pat,jim).
male(tom).
male(bob).
male(jim).
female(pam).
female(ann).
female(liz).
female(pat).
offspring(Y,X) :parent(X,Y).
mother(X,Y) :parent(X,Y),
female(X).
grandparent(X,Z) :parent(X,Y),
parent(Y,Z).
sister(X,Y) :parent(Z,X),
parent(Z,Y),
female(X),
different(X,Y).
predecessor(X,Z) :parent(X,Z).
predecessor(X,Z) :parent(X,Y),
predecessor(Y,Z).