4

I need to use a forward chainer in certain Prolog problem. I would like to avoid to implement it my self from scratch with a vanilla meta-interpreter (but that is what I will have to do if no other option is available) since doing this with a meta-interpreter will be slow, and also I am sure that some good implementations should be around. Does someone know if YAP or SWI Prolog includes a native and efficient forward chainer ?. If that is the case, a pointer to how to install it/use it would be greatly appreciated.

In case no native forward chainer is available on these two Prolog engines, could someone recommend me a good open source implementation based on a vanilla meta-interpreter that I could use as an external Prolog library ?

Thanks in advance.

false
  • 10,264
  • 13
  • 101
  • 209
Sergio
  • 8,532
  • 11
  • 52
  • 94

2 Answers2

5

YAP and SWI both include an implementation of Constraint Handling Rules - http://dtai.cs.kuleuven.be/projects/CHR/ - which is a forward-chaining rule system.

I cannot speak to its performance with respect to your particular problem, but CHR is known to be efficient (see the papers linked from the CHR site).

CHR also has Java, Haskell, and C implementations, so you could easily port your rules to one of those languages if you need better performance later on.

Nick Main
  • 1,440
  • 8
  • 15
  • Thanks for your answer @NickMain. Do you know if the package is included by default in the YAP installation ?. One question related to efficiency: somehow I thought that a forward chainer implemented in a highly optimized Prolog engine such as YAP should be faster than the same software in a non logic language such as Java or C. Is that not necessarily the case ? – Sergio May 06 '12 at 01:42
  • The YAP manual includes a section on CHR - http://www.dcc.fc.up.pt/~vsc/Yap/documentation.html#CHR - so it probably is. In SWI you just need to `:- use_module(library(chr)).` – Nick Main May 06 '12 at 04:20
  • 2
    One of the papers on the C implementation of CHR includes some benchmarks comparing the SWI, Java and C versions - http://people.cs.kuleuven.be/~pieter.wuille/static/chr07.pdf . It looks like SWI and Java are comparable whilst C is much faster. – Nick Main May 06 '12 at 04:25
4

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