Let's explain both backward chaining and forward chaining in terms of minimal logic and not in terms of resolution theorem proving. Normal backward chaining Prolog rule can be viewed as an application of the left implication introduction rule of minimal logic. So basically when we have a goal P and a rule A->P, the combined inference rule says that we might replace the goal P by the goal A:
-------- (Id)
P |- P G, A -> P |- A
--------------------------------- (Left ->)
G, A -> P |- P
The same rule can now be used to model a forward chaining application. This time we don't have a goal P, but instead a condition A has materialized in the premisses. When there is a rule A -> P, this rule licenses us to materialize the head P as well. The combined inference rule reads as follows:
------- (Id)
A |- A G, A, A -> P, P |- B
--------------------------------------- (Left ->)
G, A, A -> P |- B
The idea of our little program is to completely compute the fixpoint F(M) = M of the forward chaining process. What we do is we compute iterations M0, M1, M2, etc.. which only works if the process terminates with a finite result. From deductive databases we have adopted the idea of only computing the delta between Mn+1 and Mn. It is possible to find a G, so that F(Mn)\Mn = G(Mn\Mn-1,Mn-1) with relatively less effort.
The current implementation of G might stumble on cyclic data, since duplicates are currently not eliminated. In a forward chainer that also allows the removal of facts, one could use special forward rules to eliminate duplicates. Fullblown forward chaining systems should anyway allow the removal of facts and they can then be used to even implement constraint solving systems.
But lets leave it for now with the simple idea and correspondingly simple code.
The G Function (delta/2) from the F Function (the original rules)
http://www.xlog.ch/jekejeke/forward/delta.p
The Toy Expert System with Forward Chaining
http://www.xlog.ch/jekejeke/forward/expert.p