220 likes | 339 Vues
This lecture outline covers essential concepts of Prolog programming, including unification, recursive rules, and data structures such as atoms, variables, and lists. We will explore how to establish relationships using Prolog's facts and rules, such as the predecessor relation, parent-child relationships, and list operations like membership and concatenation. Additionally, we will demonstrate examples of unification and important programming constructs within Prolog, equipping learners with a foundational understanding of logic programming.
E N D
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… predecessor(X,Z) :- parent(X,Y), predecessor(Y,Z).
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).
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).
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
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, _)
Data ObjectsStructured objects • objects that have several components location(X, Y, Orientation). location(156, 786, 25). location(156, 786, Orientation). location is the functor
Structures date(X,Y,Z). date(20, june, 2005). date(Day, june, 2005). date(Day, Month, Year) :- Day > 0, Day <= 31,…….
data objects structures simple objects constants variables atoms numbers These are all terms
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]). ?
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
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……
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]
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.
Concatentation conc([], L, L). conc([X | L1], L2, [X | L3]) :- conc(L1, L2, L3).
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
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)
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)
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).
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))
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))