|
Logical Loops
|
Background Paper and Translation Code
This code implements the loop construct described in:
J.Schimpf, Logical Loops,
18th International Conference on Logic Programming, ICLP 2002,
Copenhagen, Denmark, July/August 2002.
Proceedings, LNCS 2401, pg 224-238,
© Springer-Verlag,
Postscript,
Pdf,
Powerpoint slides,
Pdf slides.
-
ECLiPSe users don't need this because (an extended form of these) loops
are a standard language feature.
-
loops.pl : the basic translation code which should
provide the "interpreted" form for any Prolog system
-
loops_swi.pl : small wrapper for SWI Prolog
(and possibly others) providing the compile-time transformation hook
-
As of release 4.0, SICStus Prolog supports this construct.
A Two-Minute Explanation of Logical Loops
The following is an extract from a
10 Jun 2009 posting in comp.lang.prolog
explaining do-loops in a minimalist way, in response to criticism.
> ... I didn't
> like the loop constructs (there are over ten different constructs and
> I still don't know how to use param).
There is really only one construct called do/2, and only one of the
many iteration specifiers you refer to is really fundamental. Let me
present it in a way that is different from the paper, and may appeal
more to the purist.
A call
?- ( fromto(From,In,Out,To) do Body ).
is translated into
?- do__1(From, To).
do__1(Last, Last) :- !.
do__1(In, Last) :- Body, do__1(Out, Last).
That's more or less all there is to it. Ok, one important thing:
you can have arbitrarily many fromto-specifiers in the same do-loop.
For each of them, you will simply get an additional argument pair in
the generated predicate. So, every fromto maps to one accumulator
in the recursion.
Equipped with this, you can already write all your iterative recursions
as do-loops. But of course there are some very common patterns, like
iteration over list elements or integers, for which one can have
intuitive abbreviations, that's why we have additional notation like
?- ( foreach(X,List) do ... ).
as shorthand for
?- ( fromto(List,[X|Xs],Xs,[]) do ... ).
?- ( for(I,L,H) do ...).
as shorthand for
?- H1 is H+1, ( fromto(L,I,I1,H1) do I1 is I+1, ... ).
?- ( param(S) do ... ).
as shorthand for
?- ( fromto(S,S,S,_) do ... ).
?- ( foreacharg(X,S) do ... ).
as shorthand for
?- N1 is arity(S)+1, ( fromto(1,I,I1,N1),param(S) do arg(I,S,X), I1 is I+1, ... ).
and so on.
These do-loops have been an official language feature of ECLiPSe since
1998, and over the years users have come with many requests to pack
more functionality into them. Some of these (in particular, giving
them more of a while-flavour) we have resisted, but some have been
integrated (the nested-loop features, which are not in the original paper).
Joachim Schimpf