# 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

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 )

I am always amazed to learn what else there is out there Prolog land â€¦

thank you for posting

Dan