AI_Part3_Prolog
AI_Part3_Prolog
(Part 3)
AI PROGRAMMING LANGUAGE:
PROLOG
Course Contents
Again..Selected topics for our course. Covering all of AI is impossible!
and ,
Or ;
Only if :-
not not
Facts, Rules, and Queries
n Queries- are also complex terms which are followed by a full stop.
n ?- parent(X,saya). %who is my parent
Prolog command..facts
n Open Swi-Prolog window
n File-New-Type the facts and rules with full stops at the end
parent(ayah,saya).
parent(mak,saya).
female(mak).
male(ayah).
n File- Edit –Choose File – type new rule to indicate relation mother
n Save-Close file
?- parent(X,saya).
?- female(ayah).
?- male(X).
Exercise (Family relationships)
1) Use the predicates male/1,
female/1, and parent_of/2 to
represent your family tree as a
Prolog knowledge base
2) Now, formulate
father(Father,Child)
rules to capture the mother(Mother,Child)
following grandparent(Grandparent,Child)
relationships: sister(Sister,Person)
grandchild(Grandchild,Child)
Ex:
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
father_of(X, Y) :- male(X), parent(X, Y).
Exercise (Family relationships)
Base case
ancestor( Old, Young ) :- parent( Old, Young ).
ancestor( Old, Young ) :- parent( Old, Middle ),
ancestor( Middle, Young ). Recursive-
clause
Recursion in Prolog
n Example run: ancestor( Old, Young ):- parent( Old,Young ).
?- ancestor( paul, harry ). ancestor( Old, Young ):- parent( Old,Middle ),
Call: ancestor(paul, harry ). ancestor( Middle, Young ).
Call: parent(paul, harry ).
Fail.
Retry: ancestor(paul, harry ).
Call: parent(paul, Middle ).
Unify: Middle = lili.
Succeed: parent(paul, lili ).
Call: ancestor( lili, harry ).
Call: parent( lili, harry).
Succeed: parent( lili, harry ).
Succeed: ancestor( lili, harry ).
Succeed: ancestor(paul, harry)
recursive predicate definitions
http://www.coli.uni-saarland.de/~kris/esslli04prolog/slides/0.day2.pdf
RECURSIVE
Exercise in Prolog
ancestor( X, Y):- parent( X,Y ).
ancestor( X, Z ):- parent( X,Z ), ancestor( Z,Y ).
?- ancestor (saya,X).
?- ancestor (X,saya).
?- ancestor (X, mak).
n move(1,8). move(7,2).
n move(2,7). move(6,7).
n move(2,9). move(6,1).
n move(3,8). move(1,6).
n move(3,4). move(8,3).
n move(4,3). move(8,1).
n move(4,9). move(9,4). -TERMINATE RECURSIVE IF X IS IN X
n move(7,6). POSITION
move(9,2).
-AVOID DUPLICATE STATES
member(X,[X|T]).
member(X,[Y|T]) :- member(X,T).
path(Z,Z,L).
path(X,Y,L) :- move(X,Z),not(member(Z,L)),path(Z,Y,[Z|L]).