170 likes | 328 Vues
COMP313A Programming Languages. Logic Programming (4). Lecture Outline. Some Prolog lists Unification. Concatentation. conc([], L, L). conc([X | L1], L2, [X | L3]) :- conc(L1, L2, L3). Can use concat to decompose lists How? Can use concat to define the member predicate -
E N D
COMP313A Programming Languages Logic Programming (4)
Lecture Outline • Some Prolog • lists • Unification
Concatentation conc([], L, L). conc([X | L1], L2, [X | L3]) :- conc(L1, L2, L3). Can use concat to decompose lists How? Can use concat to define the member predicate - member2(X, L) :- conc(L1, [X|L2], L).
Adding and deleting • How do you add an item to a list? • Deleting an item from a list – del(X, L, Result) • If X is the head of L – result is the tail of L • If X is contained in the tail of L then recursively split L into its head and tail until X is at the head. Then 1 will apply.
Deleting… del(X, [X|Tail], Tail]). del(X, [Y|Tail], [Y|Tail1]) :- del(X, Tail, Tail1) Deletes one occurrence from the list What happens when: del(a, [a, b, a, a], X). What if we changed it to del(X, [X|Tail], Tail]). del(X, [Y|Tail], [Y|Tail1]) :- del(X, Tail, Tail1), X=\=Y.
Delete • What if we want to delete every instance from the list
del (X, [], []). del (X, [X|Tail], Tail1) :- del (X, Tail, Tail1). del (X, [Y|Tail], [Y|Tail1]) :- del (X, Tail, Tail1).
Relations/Terms/QueriesAn important note • You can define different relations with the same name but with different numbers of arguments • e.g. member/1, member/2, member/3 • If you leave off an argument prolog thinks it is a different relation • If it is undefined for that number of arguments you will get an error message • And if there is such a relation predefined…….
Define two predicates evenlength(List) and oddlength(List) so that they are true if their argument is a list of even or odd length respectively. For example ? evenlength([a,b,c,d]) ? yes ?oddlength([c, d, e]) ?yes The trick is to define them as mutually recursive clauses. Start with []
[] is even A list is even if its tail is odd A list is odd if its tail is even
evenlength([]). evenlength([First | Rest]) :- oddlength(Rest). oddlength([First | Rest]) :- evenlength(Rest).
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))