140 likes | 284 Views
Unification, Recursion and Lists Chapter 4. Taif University Faculty Of Computers And Information Systems. Unification. The way Prolog matches the target with the source. Consider the Prolog clauses: mortal (X) :- human (X). human(bashir).
E N D
Unification, Recursion and ListsChapter 4 Taif University Faculty Of Computers And Information Systems
Unification • The way Prolog matches the target with the source. • Consider the Prolog clauses: mortal (X) :- human (X). human(bashir). • Now consider the query clause:?- mortal(bashir). • bashir will be unified with the X in mortal(X).
Unification • Now consider the father, child prolog clause: father(X, abdu) :- male(X). male(bashir). • Now look at the query: ?- father(bashir, Y). • bashir will be unified with X (X/bashir) and Y will be unified with abdu (Y/abdu) • In the above Y was used in the query for simplification. It could have been father(bashir, X)
Unification • In Prolog a = b is written as: =(a, b) • Now Consider the following prolog queries: • ?- =(X, omer). • ?- =(ali, omer). • ?-=(X, omer), =(X, Y). Test the above queries and see the results! (#2 will fail!)
Unification • Now Consider the following prolog queries and try to guess the responses: • ?-X = omer. • ?-ali = omer. • ?-X =omer, X = Y. • ?- X = happy(ali).
Recursion • Many things in life are recursively defined!! • Examples: • An ancestor of mine is one of my parents or one of their ancestors. • To unload a load of boxes, remove the one box, then unload the remaining boxes. • To serve the people in the queue, serve the person in the front, then serve those who are still in the queue.
Recursion • Ancestors example in Prolog : parent(abdu, bashir). /* bashir is abdu's parent */ parent(bashir, ali). % ali is bashir's parent parent(ali, zahra). % zahra is ali's parent ancestor(X,Y) :- % Y is an ancestor of X if parent(X,Y). % Y is a parent of X.ancestor(X,Y) :- % Y is an ancestor of X if parent(X,Z), % Z is a parent of X and ancestor(Z,Y). % Y is an ancestor of Z
Recursion • You talk about someone if you know him/her or you know someone who talks about him/her. talks_about(A, B) :- knows(A, B). talks_about(P, R) :- knows(P, Q), talks_about(Q, R). knows(bill, jane). knows(jane, pat). knows(jane, fred). knows(fred, bill).
Lists • Lists are powerful data structures for holding and manipulating groups of things. • In Prolog, a list is simply a collection of terms. • The terms can be any Prolog data type, including lists and other structures. • A list is denoted by square brackets with the terms separated by commas.
Examples of lists • [ice_cream, coffee, chocolate] a list with three elements (all atoms) • [a, b, c, c, d] a list with five elements (all atoms) • [ ] a list with no elements in it (it is an atom) • [book(math), likes(apples), parents(x, y)]a list with three elements (all Prolog clauses) • [happy(omer), [ice_cream, chocolate], [1, [2], 3]] a list with three elements!
List Destruction • The list can be reduced to an empty list by taking elements from the front. • Equating [X|Y] to a none empty list will result in X taking the first element (the head) and Y the rest of the list (the tail). • Example: [X|Y] = [a, b, c, d] • will result in: X = a (head of the list) and Y = [b, c, d] (The tail of the list)
List Construction • Elements can be added to the head of the list. • Example: If X = [omer, othman, ali] Y = [abubakr|X] will result in: Y = [abubakr, omer, othman, ali] • Rather than a single element, bigger junks can be added or removed. EX: [A, B|Y] = [f, g, h, i, j] results in A=f, B=g and Y=[h, i, j]
write/1 • write/1 is a built in predicate that is always true. • Note that the 1 in write/1 indicates that its arity is one, i.e. it requires one argument. • It produces actual output, i.e. it prints its argument.
Recursive Program Using Lists • A recursive program can be written to print out the elements of a list. • write out the first element, then write out the remainder. (which is still a list, the tail.) print_the_list([]). print_the_list(H|T) :- write(H), % write head element write(‘, ’), % write a separator print_the_list(T). % write the tail list