Subject: [SWIPL] Performance!

From: <[email-masked]>
Date: Sun, 04 Nov 2001 16:46:14 EET

Hi, everybody!

I do have a set of rules, a part of which is given below. I use
these rules to derive facts that are not explicitly stored in my
factsbase. I save about 40% from the space; however, the cost of
execution is high. The main reason behind it is that many of my
rules recompute a set of facts many times as can be seen in the
rule set below (p2-west/3 and p3-west/3 recomputes p1-west/3 each
time they are called). I thought about using modules: p1-west/3,
p2-west/3 and p3-west/3 may create their own modules, and the
module created for p1-west/3 may be imported to the modules
created for p2-west/3 and p3-west/3 before the first call to them
is made. However, as noted in the SWI-Prolog manual, this creates
a runtime overhead, which may actaully exceed the time spent for
recomputing the facts.

I could not find any way to reorganize the rules so that the
performance gets better. I am open to any suggestion, and any
comment or solution would be very much appreciated.

Have a nice day,

Mehmet E. Donderler

/* WEST, EAST, RIGHT, LEFT */

p-west(X, Y, F) :- p1-west(X, Y, F); p2-west(X, Y, F);
             p3-west(X, Y, F).

p1-west(X, Y, F) :- west(X, Y, F).

/* if object X is to the west of object Y, and object Y is to the
west of object Z, then object X is to the west of object Z.
(Transition Rule). */

p1-west(X, Z, F) :- west(X, Y, F), p1-west(Y, Z, F).

/* if object X is equal to object Y, and object Y is to the west
of object Z, then object X is to the west of object Z. */

p2-west(X, Z, F) :- p1-equal(X, Y, F), p1-west(Y, Z, F).

/* Extension Rule */

p2-west(X, Z, F) :- p1-equal(X, Y, F), p1-west(Y, W, F),
    (p1-equal(W, Z, F); p2-west(W, Z, F)).

/* if object X is to the west of object Y, and object Y is equal
to object Z, then object X is to the west of object Z. */

p3-west(X, Z, F) :- p1-west(X, Y, F), p1-equal(Y, Z, F).

/* Extension Rule */

p3-west(X, Z, F) :- p1-west(X, Y, F), p1-equal(Y, W, F),
    (p1-west(W, Z, F); p3-west(W, Z, F)).

/* if object Y is to the west of object X, then object X is to the
east of object Y (Inverse Rule). */

p-east(X, Y, F) :- p-west(Y, X, F).

/* if object X is to the east/north-east/south-east of object X,
then object X is to the right of object Y. */

p-right(X, Y, F) :- p-east(X, Y, F); p-north-east(X, Y, F);
                 p-south-east(X, Y, F).

/* if object X is to the west/north-west/south-west of object Y,
then object X is  to the left of object Y. */

p-left(X, Y, F) :- p-west(X, Y, F); p-north-west(X, Y, F);
            p-south-west(X, Y, F).