Prolog Cpt114 - Week 2

650 views
548 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
650
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
41
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Prolog Cpt114 - Week 2

  1. 1. Week 2
  2. 2.  All in prolog Instance and queries Anonymous Variable Arithmetic in Prolog Review A Family Tree
  3. 3. Term Constant Compound Term Variable Names an individual Names an individual Stands for an individual that has parts unable to be named when program is writtenAtom Number likes(john, mary) Xalpha17 0 book(dickens, Z, cricket) Gross_paygross_pay 1 f(x) Diagnosisjohn_smith 57 [1, 3, g(a), 7, 9] _257dyspepsia 1.618 -(+(15, 17), t) _+ 2.04e-27 15 + 17 - t=/= -13.6’12Q&A’
  4. 4. Program consists of Facts Rules Goals
  5. 5. arithmetic comparisons automatically evaluate expressions X =:= Y X and Y must both be arithmetic expressions (no variables) X == Y X > Y ?- 12 =:= 6+6. X >= Y Yes X < Y X =< Y ?- X =:= 6+6. ERROR: Arguments are not sufficiently instantiated Expr1 =:= Expr2 True if expression Expr1 evaluates to a number equal to Expr2.
  6. 6.  it is sometimes convenient to write functors/predicates as operators predefined: +(2, 3)  2 + 3 user defined? likes(dave, cubs)  dave likes cubs
  7. 7. Find:pam tom grandfather grandmother grandson bob liz granddaughter brotherann pat sister jim
  8. 8. Recursive Rules: Predecessor Relation Some X is an indirect predecessor of some Z if there is a partnership chain of people between X and Z. X is a direct predecessor of Z X is an indirect predecessor ofpredecessor(X, Z) :- X Z predecessor(X, Z) :- parent(X, Z). parent(X, Y1), parent parent(Y1, Y2), parent(Y2, Z). X Y1 parent predecessor parent predecessor Z Y2 parent Z 14
  9. 9. Recursive Rules: Predecessor Relation Recursive predecessor(X, Z) :-  Predecessor of any depth. parent(X, Y), predecessor(Y, Z).  Define predecessor relation in terms of itself. For all X and Z X is a predecessor of Z if there is a Y such that (1) X is a parent of Y and (2) Y is a predecessor of Z. predecessor(X, Z) :- parent(X, Z). 15
  10. 10. Family Program parent(pam, bob). parent(tom, bob). mother(X, Y) :- parent(X, Y), parent(tom, liz). female(X). parent(bob, ann). parent(bob, pat). sister(X, Y) :- parent(Z, X), parent(pat, jim). parent(Z, Y), female(X). female(pam). female(liz). female(ann). female(pat). male(tom). male(bob). male(jim). 16
  11. 11. path(X, Y)=a(X,Y). a path(X, Y) :- a(X, Z), path(Z, Y). b carc d e fa(g, h). ga(g, d). ha(e, d).a(h, f).a(e, f).a(a, e).a(a, b).a(b, f).a(b, c). ?- path(f, f).a(f, c). ?- path(a, c). ?- path(g, e). ?- path(g, X). ?- path(X, h).
  12. 12. a b carc d e f g h

×