N I V E U R S E I H T T Y O H F G R E U D I B N Logic Programming: Recursion, lists, data structures Alan Smaill Sep 28 2015 Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 1/28
Today N I V E U R S E I H T T Y O H F G R E U D I B N Recursion proof search practical concerns List processing Programming with terms as data structures. Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 2/28
Recursion N I V E U R S E I H T T Y O H F G R E U D I B N So far the rules we have seen have been (mostly) non-recursive. This is a limit on what can be expressed. Without recursion, we cannot define transitive closure eg define ancestor/2 in terms of parent/2 . Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 3/28
Recursion ctd N I V E U R S E I H T T Y O H F G R E U D I B N In recursive use, the same predicate is used in the head (lhs) of the rule as in the body (rhs) (in the second clause below): ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 4/28
Recursion ctd N I V E U R S E I H T T Y O H F G R E U D I B N In recursive use, the same predicate is used in the head (lhs) of the rule as in the body (rhs) (in the second clause below): ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). This is a fine declarative description of what it is to be an ancestor. But watch out for the traps!!! Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 4/28
Reminder: depth-first search N I V E U R S E I H T T Y O H F G R E U D I B N Prolog searches depth-first in program order (“top to bottom”): Regardless of context . . . even if there is an “obvious” solution elsewhere in the search space. Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 5/28
Reminder: depth-first search N I V E U R S E I H T T Y O H F G R E U D I B N Prolog searches depth-first in program order (“top to bottom”): Regardless of context . . . even if there is an “obvious” solution elsewhere in the search space. p :- p. p. ?- p. — the query will loop on the first clause, and fail to terminate. Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 5/28
Recursion: order can matter N I V E U R S E I H T T Y O H F G R E U D I B N Take the program for ancestor/2 with clauses in the opposite order: ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). ancestor(X,Y) :- parent(X,Y). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 6/28
Recursion: order can matter N I V E U R S E I H T T Y O H F G R E U D I B N Take the program for ancestor/2 with clauses in the opposite order: ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). ancestor(X,Y) :- parent(X,Y). This may be less efficient – looks for longest path first. More likely to loop – if the parent/2 relation has cycles. HEURISTIC: write base cases first (ie non-recursive cases). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 6/28
Rule order affects search N I V E U R S E I H T T Y O H F G R E U D I B N parent(a,b). ancestor(a,b) parent(b,c). parent(a,Z),ancestor(Z,b) parent(a,b) Z=b ancestor(b,b) Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 7/28
Rule order affects search N I V E U R S E I H T T Y O H F G R E U D I B N parent(a,b). ancestor(a,b) parent(b,a). parent(a,Z),ancestor(Z,b) Z=b ancestor(b,b) parent(b,W),ancestor(W,b) W=a ancestor(a,b) . . . Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 8/28
Recursion again N I V E U R S E I H T T Y O H F G R E U D I B N Goal order can matter! ancestor3(X,Y) :- parent(X,Y). ancestor3(X,Y) :- ancestor3(Z,Y), parent(X,Z) Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 9/28
Recursion again N I V E U R S E I H T T Y O H F G R E U D I B N Goal order can matter! ancestor3(X,Y) :- parent(X,Y). ancestor3(X,Y) :- ancestor3(Z,Y), parent(X,Z) This returns all solutions, then loops, eg with the following facts: parent(a,b). parent(b,c). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 9/28
Goal order affects search N I V E U R S E I H T T Y O H F G R E U D I B N ancestor3(X,b) ancestor3(Z,b), parent(X,b) parent(X,Z) X=a ancestor3(W,b), parent(Z,b), parent(Z,W), parent(X,Z) parent(X,Z) Z=a . . . . . . parent(X,a) Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 10/28
More recursion N I V E U R S E I H T T Y O H F G R E U D I B N Clause order can matter. ancestor4(X,Y) :- ancestor4(Z,Y), parent(X,Z). ancestor4(X,Y) :- parent(X,Y). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 11/28
More recursion N I V E U R S E I H T T Y O H F G R E U D I B N Clause order can matter. ancestor4(X,Y) :- ancestor4(Z,Y), parent(X,Z). ancestor4(X,Y) :- parent(X,Y). This will always loop. Heuristic: put non-recursive goals first. Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 11/28
Goal order matters N I V E U R S E I H T T Y O H F G R E U D I B N ancestor4(X,Y) ancestor4(X,Z),parent(Z,Y) ancestor4(X,W),parent(W,Z),parent(Z,Y) ancestor(X,V),parent(V,W),parent(W,Z),parent(Z,Y) . . . Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 12/28
Recursion and terms N I V E U R S E I H T T Y O H F G R E U D I B N Terms can be arbitrarily nested Example: unary natural numbers nat(z). nat(s(X)) :- nat(X). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 13/28
Recursion and terms N I V E U R S E I H T T Y O H F G R E U D I B N Terms can be arbitrarily nested Example: unary natural numbers nat(z). nat(s(X)) :- nat(X). To do interesting things, we need recursion. Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 13/28
Addition, subtraction N I V E U R S E I H T T Y O H F G R E U D I B Addition: N add(z,N,N). add(s(N),M,s(P)) :- add(N,M,P). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28
Addition, subtraction N I V E U R S E I H T T Y O H F G R E U D I B Addition: N add(z,N,N). add(s(N),M,s(P)) :- add(N,M,P). Run in reverse to get all M,N that sum to P: Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28
Addition, subtraction N I V E U R S E I H T T Y O H F G R E U D I B Addition: N add(z,N,N). add(s(N),M,s(P)) :- add(N,M,P). Run in reverse to get all M,N that sum to P: ?- add(X,Y,s(s(s(z)))). X=z,Y=s(s(s(z))); X=s(Z),Y=s(s(z)); ... Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28
Addition, subtraction N I V E U R S E I H T T Y O H F G R E U D I B Addition: N add(z,N,N). add(s(N),M,s(P)) :- add(N,M,P). Run in reverse to get all M,N that sum to P: ?- add(X,Y,s(s(s(z)))). X=z,Y=s(s(s(z))); X=s(Z),Y=s(s(z)); ... Use to define leq/2 : leq(M,N) :- add(M,_,N). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28
Addition, subtraction N I V E U R S E I H T T Y O H F G R E U D I B Addition: N add(z,N,N). add(s(N),M,s(P)) :- add(N,M,P). Run in reverse to get all M,N that sum to P: ?- add(X,Y,s(s(s(z)))). X=z,Y=s(s(s(z))); X=s(Z),Y=s(s(z)); ... Use to define leq/2 : leq(M,N) :- add(M,_,N). Here “ _ ” is a so-called anonymous variable; use to avoid warning of singleton variable in Prolog programs. Can also use, for example, _X , _Anon . Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 14/28
Multiplication N I V E U R S E I H T T Y O H F G R E U D I B N Now define multiplication: multiply(z,N,z). % or: multiply(z,_,z). multiply(s(N),M,P) :- multiply(N,M,Q), add(M,Q,P). square(N,M) :- multiply(N,N,M). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 15/28
List Processing N I V E U R S E I H T T Y O H F G R E U D I B N Recall built-in list syntax: list([]). list([X|L]) :- list(L). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 16/28
List Processing N I V E U R S E I H T T Y O H F G R E U D I B N Recall built-in list syntax: list([]). list([X|L]) :- list(L). Examples: list append append([],L,L). append([X|L],M,[X|N]) :- append(L,M,N). Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 16/28
append in action N I V E U R S E I H T T Y O H F G R E U D I B N Forward direction: ?- append([1,2],[3,4],X). X = [1,2,3,4] Alan Smaill Logic Programming: Recursion, lists, data structures Sep 28 2015 17/28
Recommend
More recommend