Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Prolog Recursion Pepper

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

3 Use in a query just_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'? yes X = cow ; X = frog ; X = mosquito ; X = blood(john) ;

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

5 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

6 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


Download ppt "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."

Similar presentations


Ads by Google