1 / 17

COMP313A Programming Languages

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 -

leala
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 (4)

  2. Lecture Outline • Some Prolog • lists • Unification

  3. 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).

  4. 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.

  5. 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.

  6. Delete • What if we want to delete every instance from the list

  7. del (X, [], []). del (X, [X|Tail], Tail1) :- del (X, Tail, Tail1). del (X, [Y|Tail], [Y|Tail1]) :- del (X, Tail, Tail1).

  8. 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…….

  9. 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 []

  10. [] is even A list is even if its tail is odd A list is odd if its tail is even

  11. evenlength([]). evenlength([First | Rest]) :- oddlength(Rest). oddlength([First | Rest]) :- evenlength(Rest).

  12. 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

  13. 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)

  14. 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)

  15. 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).

  16. 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))

  17. 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