There are, however, some interesting and important issues which need to be addresses in order to provide the Prolog programmer with a practical, efficient, and well integrated facility for forward chaining. This document describes such a facility, Pfc , which we have implemented in standard Prolog.
A somewhat more complete users manual is available, as is some information on how to get the software and how to use it at UMBC .
spouse(X,Y) <=> spouse(Y,X). spouse(X,Y),gender(X,G1),{otherGender(G1,G2)} =>gender(Y,G2). gender(P,male) <=> male(P). gender(P,female) <=> female(P). parent(X,Y),female(X) <=> mother(X,Y). parent(X,Y),parent(Y,Z) => grandparent(X,Z). grandparent(X,Y),male(X) <=> grandfather(X,Y). grandparent(X,Y),female(X) <=> grandmother(X,Y). mother(Ma,Kid),parent(Kid,GrandKid) =>grandmother(Ma,GrandKid). grandparent(X,Y),female(X) <=> grandmother(X,Y). parent(X,Y),male(X) <=> father(X,Y). mother(Ma,X),mother(Ma,Y),{X\==Y} =>sibling(X,Y).
The Pfc package allows one to define forward chaining rules and to add ordinary Prolog assertions into the database in such a way as to trigger any of the Pfc rules that are satisfied. An example of a simple Pfc rule is:
This rule states that whenever the fact unifying with gender(P,male) is added to the database, then the fact male(P)is true. If this fact is not already in the database, it will be added. In any case, a record will be made that the validity of the fact male(P)depends, in part, on the validity of this forward chaining rule and the fact which triggered it. To make the example concrete, if we add gender(john,male)$, then the fact male(john)will be added to the database unless it was already there.gender(P,male) => male(P)
In order to make this work, it is necessary to use the predicate add/1 rather than assert/1 in order to assert Pfc rules and any facts which might appear in the lhs of a Pfc rule.
However, adding these rules will generate a large number of assertions, most of which will never be needed. An alternative is to define the ancestor relationship by way of backward chaining rules which are invoked whenever a particular ancestor relationship is needed. In Pfc, this need arises whenever facts matching the relationship are sought while trying a forward chaining rule.parent(P1,P2) => ancestor(P1,P2). parent(P1,P2), ancestor(P2,P3) => ancestor(P1,P3).
ancestor(P1,P2) <= {\+var(P1)}, parent(P1,X), ancestor(X,P2). ancestor(P1,P2) <= {var(P1),\+var(P2)}, parent(X,P2), ancestor(P2,X).
A lhs term preceded by a \simis satisfied only if no fact in the database unifies with it. Again, the Pfc system records a justification for the conclusion which, in this case, states that it depends on the absence of the contradictory evidence. The behavior of this rule is demonstrated in the following dialogue:person(P), ~female(P) => male(P).
?- add(person(P), ~female(P) => male(P)). yes ?- add(person(alex)). yes ?- male(alex). yes ?- add(female(alex)). yes ?- male(alex) no
Another advantage is that it allows us to write rules like the following:
Although the declarative reading of this rule can be questioned, its procedural interpretation is clear and useful:at(Obj,Loc1),at(Obj,Loc2)/{Loc1\==Loc2} => {remove(at(Obj,Loc1))}.
If an object is known to be at location Loc1and an assertion is added that it is at some location Loc2$, distinct from Loc1$, then the assertion that it is at Loc1should be removed.
As another example, consider a rule which detects bigamists and sends an appropriate warning to the proper authorities:mother(X,Y) => female(X), parent(X,Y), adult(X).
Each element in the rhs of a rule is processed from left to right --- assertions being added to the database with appropriate support and conditions being satisfied. If a condition can not be satisfied, the rest of the rhs is not processed. We would like to allow rules to be expressed as bi-conditional in so far a possible. Thus, an element in the lhs of a rule should have an appropriate meaning on the rhs as well. What meaning should be assigned to the conditional fact construction (e.g. P/Q$) which can occur in a rules lhs? Such a term in the rhs of a rule is interpreted as a conditioned assertion. Thus the assertion P/Q will match a condition P\primein the lhs of a rule only if Pand P\primeunify and the condition Qis satisfiable. For example, consider the rules that says that an object being located at one place is reason to believe that it is not at any other place:spouse(X,Y), spouse(X,Z), {Y\==Z} => bigamist(X), {format("~N~w is a bigamist, married to both ~w and ~w~n",[X,Y,Z])}.
Note that a conditioned assertion is essentially a Horn clause. We would express this fact in Prolog as the backward chaining rule:at(X,L1) => not(at(X,L2))/L2\==L1
The difference is, of course, that the addition of such a conditioned assertion will trigger forward chaining whereas the assertion of a new backward chaining rule will not.not(at(X,L2)) :- at(X,L1),L1\==L2.
For some applications, it is useful to be able to justify actions performed in the rhs of a rule. To allow this, Pfc supports the idea of declaring certain actions to be undoable and provides the user with a way of specifying methods to undo those actions. Whenever an action is executed in the rhs of a rule and that action is undoable, then a record is made of the justification for that action. If that justification is later invalidated (e.g. through the retraction of one of its justificees) then the support is checked for the action in the same way as it would be for an assertion. If the action does not have support, then Pfc trys each of the methods it knows to undo the action until one of them succeeds.
In fact, in Pfc , one declares an action as undoable just by defining a method to accomplish the undoing. This is done via the predicate pfcUndo/2. The predicate pfcUndo(A1,A2)is true if executing A2is a possible way to undo the execution of A1. For example, we might want to couple an assertional representation of a set of graph nodes with a graphical display of them through the use of Pfc rules:
at(N,XY) => {displayNode(N,XY)}. arc(N1,N2) => {displayArc(N1,N2}. pfcUndo(displayNode(N,XY),eraseNode(N,XY)). pfcUndo(displayArc(N1,N2),eraseArc(N1,N2)).