Section 1.5 Logic Programming
79
raccoon
fox
rabbit
deer
deer
Note that fish is listed twice as satisfying the last query because fish are eaten by
bear (fact 1) and by raccoon (fact 3). Similarly, deer are eaten by both bear and
wildcat.
pRaCtiCe 29
a. Formulate a Prolog rule that defines the predicate predator.
b. Adding this rule to the database of Example 39, what would be the response to the query
?predator(X )
recursion
Prolog rules are implications. Their antecedents (remember that these will appear
on the right side of the rules) may depend on facts, as in
prey(X ) <= eat(Y, X) and animal(X )
or on other rules, as in
hunted(X ) <= prey(X )
The antecedent of a rule may also depend on that rule itself, in which case the rule
is defined in terms of itself. A definition in which the item being defined is itself
part of the definition is called a recursive definition.
As an example, suppose we wish to use the ecology database of Example 39 to
study food chains. We can then define a binary relation infoodchain(X, Y ), meaning “Y is in X’s food chain.” This, in turn, means one of two things:
1. X eats Y directly
or
2. X eats something that eats something that eats something … that eats Y.
Case 2 can be rewritten as follows:
2′. X eats Z and Y is in Z’s food chain.
Case 1 is simple to test from our existing facts, but without (2′), infoodchain
means nothing different from eat. On the other hand, (2′) without (1) sends us
79
raccoon
fox
rabbit
deer
deer
Note that fish is listed twice as satisfying the last query because fish are eaten by
bear (fact 1) and by raccoon (fact 3). Similarly, deer are eaten by both bear and
wildcat.
pRaCtiCe 29
a. Formulate a Prolog rule that defines the predicate predator.
b. Adding this rule to the database of Example 39, what would be the response to the query
?predator(X )
recursion
Prolog rules are implications. Their antecedents (remember that these will appear
on the right side of the rules) may depend on facts, as in
prey(X ) <= eat(Y, X) and animal(X )
or on other rules, as in
hunted(X ) <= prey(X )
The antecedent of a rule may also depend on that rule itself, in which case the rule
is defined in terms of itself. A definition in which the item being defined is itself
part of the definition is called a recursive definition.
As an example, suppose we wish to use the ecology database of Example 39 to
study food chains. We can then define a binary relation infoodchain(X, Y ), meaning “Y is in X’s food chain.” This, in turn, means one of two things:
1. X eats Y directly
or
2. X eats something that eats something that eats something … that eats Y.
Case 2 can be rewritten as follows:
2′. X eats Z and Y is in Z’s food chain.
Case 1 is simple to test from our existing facts, but without (2′), infoodchain
means nothing different from eat. On the other hand, (2′) without (1) sends us
