Animals Revisited

We demonstrate forward chaining as provided by the module "delta". As an example, we take a small expert system for animals. This is the same example as from the runtime library where an ordinary Prolog backward chaining solution is given. As already explained in the previous section, the multi-file directive helps us modifying facts from the top-level:

:- multifile motion/1, skin/1, diet/1.
:- thread_local motion/1, skin/1, class/1, diet/1, animal/1.

Ordinary Prolog backward chaining uses rules of the form "Goal :- Sub-Goals". They can be entered with the help of the operator (:-)/2. Forward chaining uses rules of the form "Action <= Condition". The operator (<=)/2 is provided by the module "delta". The module "delta" also pro-vides the post/1 action and the posted/1 condition:

post(class(mamal)) <= posted(motion(walk)), posted(skin(fur)).
post(class(fish)) <= posted(motion(swim)), posted(skin(scale)).
post(class(bird)) <= posted(motion(fly)), posted(skin(feather)).

post(animal(rodent)) <= posted(class(mamal)), posted(diet(plant)).
post(animal(cat)) <= posted(class(mamal)), posted(diet(meat)).
post(animal(salmon)) <= posted(class(fish)), posted(diet(meat)).
post(animal(eagle)) <= posted(class(bird)), posted(diet(meat)).

Ordinary Prolog backward chaining is invoked by sending a goal to the Prolog interpreter. The Prolog interpreter then tries to find answer substitutions in a top-down fashion. The module "delta" provides a forward chaining engine, which is invoked by sending a goal to it. The for-ward chaining engine then tries to find answer sets in a bottom-up fashion.

write('The animal is '), write(X), nl <= posted(animal(X)).

We are interested in an answer for the animal/1 predicate. We therefore added the above for-ward chaining rule as well to the Prolog text, so that when an animal fact arrives, a message is written. The module "delta" allows such a forward chaining rule, since an action can be an arbi-trary Prolog goal. Here are some results:

?- post(motion(walk)), post(skin(fur)).

?- post(motion(walk)), post(skin(fur)), post(diet(meat)).
The animal is cat

?- post(motion(walk)), post(skin(fur)), post(diet(plant)).
The animal is rodent

The module delta not only delivers hypothetical forward chaining but also counter factual for-ward chaining, which we did not demonstrate here. The condition phaseout/1 and phaseout_posted/1 can be used to temporarily remove facts and rules during forward chaining. We refer the interested reader to the module documentation.