170 likes | 282 Views
Reversibility and List Processing Chapter 6. Taif University Faculty Of Computers And Information Systems. Reversibility of Prolog Programs. Consider the following database: square(1,1). square(2,4). square(3,9). square(4,16). square(5,25). square(6,36).
E N D
Reversibility and List ProcessingChapter 6 Taif University Faculty Of Computers And Information Systems
Reversibility of Prolog Programs • Consider the following database: square(1,1). square(2,4). square(3,9). square(4,16). square(5,25). square(6,36). It means that, second argument is the square of the first argument.
Reversibility of Prolog Programs • The queries would look like: ?- square(2,X). ?- square(X,5). ?- square(X,Y). ?- square(2,3). • we say that the program for square/2 is reversible.
Evaluation in Prolog • Prolog does not automatically evaluate ‘expressions’. • To evaluate Y = 2 + 1 in a special predicate is/2 is provided. This predicate can be used as in: Y is 2 + 1. • We can use is/2 to implement a successor relation as: successor(X,Y):- Y is X + 1. • Here, successor/2 takes the first argument as input and outputs the second argument which is to be the next largest integer.
Evaluation in Prolog • The evaluation of arithmetic expressions is a one-way process. So successor/2 is not ‘reversible’. • For these queries, ?- successor(3,X). ?- successor(X,4). ?- successor(X,Y). ?- successor(3,5). The 1st and 4th goals result in correct results (success and failure respectively) while the 2nd and 3rd goals produce error messages and fail.
List Processing • The list is an ordered sequence of elements that can have any length. • The order of the elements in the sequence matters. • The elements of a list may be any terms - constants, variables, structures, as well as other lists. • Lists can practically represent any link of structure • Lists can be represented as a special kind of tree. A list is either an empty list(written as []), or it is a structure that has two components: the head and tail. The end of a list is represented as tail that is set to the empty list.
List Processing • In a Prolog program the elements of the list separated by commas, and the whole list is enclosed in square brackets. For example, [a, b, c]. • Lists can contain other lists and variables. The following lists are legal in Prolog: [] [a] [the, men, [like, to, fish]] [a, V1, b, [X, Y]]
List Processing • Lists are manipulated by splitting them up into a head and a tail. • The head of the list is the first element of the list. The tail of the list is a list that consists of every element except the first. • Notice that the empty list has neither a head nor a tail. • There is a special notation in Prolog to represent “the list with head X and tail Y”: [X|Y]. A pattern of this form will instantiate X to the head of a list and Y to the tail of the list, for example: p([1, 2, 3]). p([ the, cat, sat, [on, the, mat]]). ?- p([X|Y|). X = 1 Y = [2, 3]; X = the Y = [cat, sat, [on, the, mat]] ?- p([_,_,_, [_|X]]). X = [the, mat]
List Processing • Examples of lists with their head and tail: List Head Tail [a, b, c] a [b, c] [ ] none none [[the,cat],sat] [the, cat] [sat] [the, [cat,sat]] the [[cat, sat]] [the,[cat,sat],down] the [[cat,sat],down] [X+Y, x+y} X+Y [x+y]
Recursive Search • The memberpredicate: • the goal member(X, Y) is true if the term that X stands for is a member of the list that Y stands for. There are two conditions to check: • It is fact that X will be a member of Y, if X is the same as the head of Y. In Prolog, this fact is: member(X,[X|Xs]). • X is a member of a list providing it is in the tail of that list. member(X,[Y|Ys]):- member(X,Ys). • To check if X is in the tail of the list we use the member predicate itself. This is the essence of recursion.
Recursive Search • When encountering a recursively defined predicate, look for the boundary conditions and the recursive case. There are actually two boundary conditions for the member. Either the object we are looking for is in the list, or it is not in the list. • The first boundary condition is recognized by the first clause, which will cause the search through the list to be stopped if the first argument of member matches the head of the second argument. • The second boundary condition occurs when the second argument of member is the empty list. member(X,[X|Xs]). member(X,[Y|Ys]):- member(X,Ys). ?- member (d, [a, b, c, d, e, f, g ] ). Yes ?- member (y, [a, b, c, d, e, f, g] ) No
Appending List • A very useful list predicate builds lists from other lists or alternatively splits lists into separate pieces is called append. In this predicate the second argument is appended to the first argument to yield the third argument. For example: ?- append([a,b,c],[d,e,f],X). X = [a,b,c,d,e,f] • Also, append([T|Q1],X,[T|Q2]) :- append(Q1,X,Q2).
Appending List Query Unifier Prolog answer ------------------------------------------------------------------------------------ ?- append([],[b,c,d],[b,c,d]). {List = [b,c,d]} yes ?- append([],[b,c,d],X). {List = [b,c,d], X = [b,c,d]} X = [b,c,d] ?- append(X,Y,Z). {X = [], Y = List, Z = List} X = [], Y = Z ?- append([],[b,c],[b,c,d]). None no
Prolog relations can be “evaluated in reverse” ?- append(X,[3,4],[1,2,3,4]). X [1,2] ?- append([1,X],[3,4],[1,2,3,4]). X 2 ?- append(5,[],X). no
Adding an Item • To add an item to a list, it is easiest to put the new item in front of the list so that is becomes the new head. • If Xis the new item and the list to whichXis added is Xsthen the result is[X|Xs]. • We don’t need a procedure for adding a new element in front of a list. Nevertheless, if we want to define such a procedure explicitly. It can be written as the fact: add(X,Xs,[X|Xs]).
Deleting an Item del(X, Xs, Ys). • Thedelrelation can be defined similarly to the membership relation. • We have again, two cases: • If Xis the head of the list then the result after the deletion isthe tail of the list. • If X is in the tail of the list then it is deleted from there. del(X,[X|Xs],Xs]. del(X,[Y|Xs],[Y|Ys]) :- del(X,Xs,Ys). • delwill fail if the list does not contain the item to be deleted. If there are several occurrences of Xin the list then delwill be able to delete anyone of them by backtracking. ? - del(a, [a, b, c, d], L). L = [b, c, d]