1 / 22

COMP313A Programming Languages

COMP313A Programming Languages. Logic Programming (3). Lecture Outline. Some Prolog Unification. Some more prolog. Recursive rules example predecessor relation predecessor(X,Z) :- parent(X,Z). predecessor(X,Z) :- parent(X,Y), parent(Y,Z) And so on…

xenia
Download Presentation

COMP313A Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP313A Programming Languages Logic Programming (3)

  2. Lecture Outline • Some Prolog • Unification

  3. Some more prolog • Recursive rules example predecessor relation predecessor(X,Z) :- parent(X,Z). predecessor(X,Z) :- parent(X,Y), parent(Y,Z) And so on… predecessor(X,Z) :- parent(X,Y), predecessor(Y,Z).

  4. Some more prolog parent(pam,bob). parent(tom,bob). parent(tom,liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). ? predecessor(pam,bob). ? predecessor(pam, ann). ? predecessor(pam, liz). ? predecessor(pam, X).

  5. A prolog program comprises clauses - facts, rules and queries PROGRAM big(bear). %clause 1 big(elephant). %clause 2 small(cat). %clause 3 brown(bear). %clause 4 black(cat). %clause 5 grey(elephant). %clause 6 dark(Z) :- black(Z). %clause 7 dark(Z) :- brown(Z). %clause 8 ?dark(X), big(X).

  6. Data ObjectsAtoms versus numbers versus Variables • Atoms • strings of letters, digits, and the underscore character beginning with a lower case letter • some strings of special characters • can enclose strings of characters in single quotes e.g. ‘Fred’ • Numbers • integers and floats

  7. Data ObjectsAtoms versus Variables • Variables are strings of characters, digits and underscores that begin with an uppercase letter or an underscore • Can use the anonymous underscore hasachild(X) :- parent(X,Y) hasachild(X) :- parent(X,_) ? parent(X, _)

  8. Data ObjectsStructured objects • objects that have several components location(X, Y, Orientation). location(156, 786, 25). location(156, 786, Orientation). location is the functor

  9. Structures date(X,Y,Z). date(20, june, 2005). date(Day, june, 2005). date(Day, Month, Year) :- Day > 0, Day <= 31,…….

  10. data objects structures simple objects constants variables atoms numbers These are all terms

  11. Operations on listsMembership • The member relation member(X,L) ? member(d, [a, b, h, d]). ? ? member(d, [a,b,[d h], e]). ? ?member([d, h], [a, [d,h], e f]). ?

  12. Membership • X is a member of L if • X is the head of L, or • X is a member of the tail of L. member(X, [X|Tail]). member(X, [Head | Tail]) :- member(X,Tail). Note two separate clauses

  13. Membership • X is a member of L if • X is the head of L, or • X is a member of the tail of L, or • X is a member of a sublist of L member(X, [X|Tail]). member(X, [Head | Tail]) :- member(X,Tail). plus one more clause……

  14. Concatenation • The concatenation relation – conc(L1, L2, L3) ? conc([a,b], [c,d], [a,b,c,d]) yes ? conc([a,b], [c,d], [a, b, [c,d]]) no ?conc([a,b], [c,d], X). X = [a,b,c,d]

  15. Concatentation • conc(L1, L2, Result) • If L1 is the empty list • then L2 and Result must be equal • If L1 is nonempty • then have [X|L1tail] • recursively X becomes the head of Result and we use conc to find the tail of result [X|TailResult] • Eventually will have exhausted L1 and TailResult will be L2.

  16. Concatentation conc([], L, L). conc([X | L1], L2, [X | L3]) :- conc(L1, L2, L3).

  17. Unification • Matching clauses with variables • Have to find the appropriate substitutions for variables so that the clauses match • Process is called unification • Process by which variables are instantiated

  18. GCD example gcd(u,0,u) gcd(u,v,w) Ü not zero(v), gcd(v, u mod v, w) Using resolution the goal Ü gcd(15, 10, x)

  19. Unification in Prolog • A constant unifies only with itself ? me = me. Yes ?me = you. No Gcd(5, 0, 5) Ü Gcd(5, 0, 5) Gcd(5, 10, w) Ü Gcd(5, 0, w)

  20. Unification in Prolog… • A variable that is uninstantiated unifies with anything and becomes instantiated with that thing ? me = X. X = me ? f(a,X) = f(Y, b). X = b Y = a ? f(X) = f(Y) gcd(u,v,w) Ü not zero(v), gcd(v, u mod v, w), gcd(15, 10, x). gcd(15, 10, x) Ü not zero(10), gcd(10, 15 mod 10, x), gcd(15, 10, x).

  21. Unification in Prolog • A structured term (predicate or function applied to arguments requires • Same predicate/function name • Same number of arguments • Arguments can be unified recursively ? f(X) = g(X) ? f(X) = f(a,b) ? f(a, g(X)) = f(Y, b) ? f(a, g(X)) = f(Y, g(b))

  22. Unification examples • Unify the following : p(X,Y) and p(a, Z) p(X,X) and p(a,b) ancestor(X,Y) and ancestor(bill, W) p(X,a,Y) and p(Z,Z,b) p(Marcus, g(X,Y)) and f(x, g(Caesar, Marcus)) g(X, X) and g(f(X), f(X))

More Related