6
Prolog Recursion Pepper

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

Embed Size (px)

Citation preview

Page 1: 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

Prolog Recursion

Pepper

Page 2: 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

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.

Page 3: 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

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) ;

Page 4: 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

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.

Page 5: 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

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

Page 6: 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

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