Upload
mildred-reynolds
View
214
Download
0
Embed Size (px)
Citation preview
Prolog Recursion
Pepper
A rule with that calls itself
is_digesting(X,Y) :- just_ate(X,Y).is_digesting(X,Y) :- just_ate(X,Z), is_digesting(Z,Y).•2 rules – You are digesting what you just ate– You are digesting whatever you ate just ate• And so on until you come to an animal that did not just
eat something.
Use in a queryjust_ate(mosquito,blood(john)).just_ate(frog,mosquito).just_ate(stork,frog).just_ate(person,cow).just_ate(cow,frog).
is_digesting(person,X)"?Please answer 'y' or 'n'? yesX = cow ;X = frog ;X = mosquito ;X = blood(john) ;
Construction
is_digesting(X,Y) :- just_ate(X,Y).is_digesting(X,Y) :- just_ate(X,Z), is_digesting(Z,Y).•Base case: – Does not use its own predicate.
•Recursive rule: – Handles one case,– Recurses over the rest of the cases.
Proof
Is_digesting(person,A). •What just_ate fact has person on the left side, then the right side can be X. : look for just_ate(person,A). Look for just_ate(X,Z), is_digesting(Z,Y). if just_ate (person,_1) and (_1, A) so (person,cow) and (cow,frog) match so A = frog
Recursion Summary
• How to declare a recursive rule• How to pair the recursive rule with a base
case• How prolog proof leads to recursion• No base case – endless recursion