comp 307 lecture 4:1 prolog i, ii prolog was taught as a procedural language control structures: if,...
Post on 16-Dec-2015
218 Views
Preview:
TRANSCRIPT
Comp 307 Lecture 4:4
Prolog and a small rule based system
How Prolog works?
Matching
Backtracking
Rule based system
Comp 307 Lecture 4:5
The first program% father(Father, Child)father(bob, liz). father(bob, jim).father(jim, pam).father(jim, pat).
mother(ann, jim).mother(pat, tom).
parent(X,Y) :- mother(X,Y).parent(X,Y) :- father(X,Y).
grandparent(X,Y) :- parent(X,Z), parent(Z,Y).
ancestor(X,Y) :- parent(X,Y).ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).
Comp 307 Lecture 4:6
|?-parent(X, Y).X=ann,Y=jim;X=pat,Y=tom;X=bob,Y=liz;X=bob,Y=jim;X=jim,Y=pam;X=jim,Y=pat;no.| ?- father(bob, X), mother(ann, X).X=jim?;no
Queries and Answers
Comp 307 Lecture 4:7
• Prolog answers a query by matching the query with FACTs and the HEADs of the rules
– If the query matches a fact, then succeed– If the query matches the head of a rule, then
the body of the rule is now treated as a query, and prolog tries to solve that
– Top-down, left-to-right, Depth-first
• If Prolog’s search fails, it backtracks to try alternative solutions, if possible.
How Prolog answers a query
Comp 307 Lecture 4:9
• When– Arguments at the same place– The same variable in one clause– X = Y
• How– Propagate variable bindings forward– Once a variable has a binding, it cannot be
changed -- there is no destructive assignment
Matching
Comp 307 Lecture 4:10
Backtracking
• When– When a sub goal failed– Multiple answers, using “;”– ……., fail.
• How:– Undo variable bindings, try alternative clauses
Comp 307
Simple Rule-based System in Prologmammal:-f(hair). mammal:-f(milk).bird:-f(feathers).bird:-f(eggs), f(flies).carnivore:-mammal, f(meat).classify(tiger):-carnivore, f(tawny), f(striped). classify(puma):-carnivore, f(black).
f(hair).f(black).f(meat).
|?-classify(X).X=puma.
Comp 307 Lecture 4:13
• Term Term Possible Instantiation a 6 no
a Z Z=a A C A=C date(2, 3, 2000) Z Z=date(2, 3, 2000) date(D, M, Y) Z Z=date(D, M, Y) date(X, Y, Z) date(Y, X, Z) X=Y date(D, 5, Y) date(D1, M, 2000) D=D1, M=5, Y=2000 date(6, 5, 2000) date(6, may, 2000) no date(D, M, Y) date1(D, M, Y) no
Matching (Unification)
Comp 307 Lecture 4:14
• Term Term Possible Instantiation [] [] yes
[a, b, c] [X, Y, c] X=a, Y=b
[1, [2, 3], 4] [X, Y, Z] X=1, Y=[2, 3], Z=4
[X,Y] [a,b,c] no[H|T] [a,b] H=a, T=[b][H|T] [1,[2,3],4] H=1, T=[[2,3],4]
Matching (Unification)
Comp 307 Lecture 4:15
Matching (Unification)• If S and T are constants, then S and T match only if they are the
same object• If S is a variable and T is anything, then they match, and S is
instantiated to T (and vice-versa)• If S and T are structures, then they match only if
– S and T have the same principal functor, and– all their corresponding components match (the recursive part)
• If there are more than one bindings, we always compute the most general unifier:
e.g. f(X,Y)=f(a,Z) X=a,Y=Z or X=a,Z=Y (Not X=a, Y=b, Z=b)
top related