Transformation Tools and simple optimiser (Discussion)

Thanks for sharing, interesting work!

Hi Gavin,

Thank you for making your work publicly available.

Could you provide some simple examples that help those who introduce themselves into Prolog understand what exactly you are doing.

Personally, I really only understand once I see representative examples with some short explanation what the problem is that is addressed and how the approach provides a solution.

If you could do that, that would be great …

many thanks,

Dan

Given a source program:

:- module(test_optimiser,[]).

:- use_module(library(transformer)).

:- optimise_all.

p(x,y,z) :- q.
p(x,w,z) :- r.
p(x,y,q) :- s.

p(x,y) :- s.
p(z,w) :- s.

q.
q.

r.
r.

s.
s.

You can load this and obtain the following:

?- [test_optimiser].

Transformer: Performed optimisation on p/2

Transformer: Performed optimisation on p/3

?- listing(p/3).
test_optimiser:p(x, y, z) :-
    !,
    q.
test_optimiser:p(x, w, z) :-
    !,
    r.
test_optimiser:p(x, y, q) :-
    s.

true.

The optimiser has just inserted some cuts where it could reason that the cuts did not alter the semantics as the subsequent clauses were no longer reachable.

In addition there is some infrastructure to make program transformations more convenient.

?- use_module(library(transformer)). 
true.

?- reify_reflect(( p(X) :- X = 1), Reflected), reify_reflect( Term, Reflected).
Reflected = clause(p/1, [X], [X=1]),
Term =  (p(X):-X=1).

The Term structure is transformed to remove the “defaulty” qualities of the prolog code, enabling argument indexing to get us determinacy without the need for cuts. It makes the structure of the optimiser very straightfoward - it’s around 15 lines at core.

Very embarrassing! I’d started the optimiser for a guarded clause language in which each clause would fire deterministically and then mistakenly thought I could get away with just sticking the cuts in! Indeed this is closer to picat. I’ll go back to the guarded clause language :smiley:

Another really nice language that is maybe a bit closer to “functional” while it took a lot of good ideas from Prolog is Erlang. In particular, it has a nice interface to “guarded clauses”. I certainly wished that Prolog had a more intuitive / less error prone way to do “guards”.

(of course the usual disclaimer applies: I don’t really know what exactly you are looking for, and I suspect I don’t know what I’m talking about :wink: )

I am always amazed to learn what else there is out there Prolog land …

thank you for posting

Dan