lambda-like continuations (was: maplist and friends)

View: New views
2 Messages — Rating Filter:   Alert me  

lambda-like continuations (was: maplist and friends)

by Ulrich Neumerkel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


In April 2008, we had some discussion ("maplist and friends") about
ways to express lambda like expressions in the place of continuations.
The following variant fits into the predefined definitions using
standard operators:

?- maplist(\X^Y^plus(X,1,Y), [1,2,3], L).
L = [2, 3, 4].

?- maplist(succ, [1,2], L).
L = [2, 3].

?- maplist(\X^succ(X), [1,2], L).
L = [2, 3].

?- maplist(\X^Y^succ(X,Y), [1,2], L).
L = [2, 3].

I.e., all variables are by default local.  Global variables like the X
in

?- Xs = [_,_], maplist(dif(X), Xs).
Xs = [_G2284, _G2287],
dif(X, _G2284),
dif(X, _G2287).

cannot be directly expressed.  The following shows that X is now a
local variable yielding an unintended answer.

?- Xs = [_,_], maplist(\Y^dif(X,Y), Xs).
Xs = [_G2329, _G2332],
dif(_G2350, _G2329),
dif(_G2356, _G2332).

To make X global:

?- Ys = [_,_], maplist(X+ \Y^X^dif(X,Y), Ys).
Ys = [_G2346, _G2349],
dif(X, _G2346),
dif(X, _G2349).

Being equivalent to:

?- Ys = [_,_], maplist(X+ \Y^X1^dif(X1,Y), Ys).
Ys = [_G2364, _G2367],
dif(X, _G2364),
dif(X, _G2367).


Implementation:

:- meta_predicate ^(?,0,?).
:- meta_predicate ^(?,1,?,?).
:- meta_predicate ^(?,2,?,?,?).
:- meta_predicate ^(?,3,?,?,?,?).

^(V1, Goal, V1) :- call(Goal).
^(V1, Goal, V1, V2) :- call(Goal, V2).
^(V1, Goal, V1, V2, V3) :- call(Goal, V2, V3).
^(V1, Goal, V1, V2, V3, V4) :- call(Goal, V2, V3, V4).
^(V1, Goal, V1, V2, V3, V4, V5) :- call(Goal, V2, V3, V4, V5).

:- meta_predicate \(1,?).
:- meta_predicate \(2,?,?).
:- meta_predicate \(3,?,?,?).
:- meta_predicate \(4,?,?,?,?).

\(FC, V1) :- copy_term_nat(FC,C), call(C, V1).
\(FC, V1, V2) :- copy_term_nat(FC,C), call(C, V1, V2).
\(FC, V1, V2, V3) :- copy_term_nat(FC,C), call(C, V1, V2, V3).
\(FC, V1, V2, V3, V4) :- copy_term_nat(FC,C), call(C, V1, V2, V3, V4).
\(FC, V1, V2, V3, V4, V5) :- copy_term_nat(FC,C), call(C, V1, V2, V3, V4, V5).

:- meta_predicate +(?,1).
:- meta_predicate +(?,2,?).
:- meta_predicate +(?,3,?,?).
:- meta_predicate +(?,4,?,?,?).
:- meta_predicate +(?,5,?,?,?,?).

+(V1, Goal) :- call(Goal, V1).
+(V1, Goal, V2) :- call(Goal, V2, V1).
+(V1, Goal, V2, V3) :- call(Goal, V2, V3, V1).
+(V1, Goal, V2, V3, V4) :- call(Goal, V2, V3, V4, V1).
+(V1, Goal, V2, V3, V4, V5) :- call(Goal, V2, V3, V4, V5, V1).
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog

Re: lambda-like continuations (was: maplist and friends)

by Ulrich Neumerkel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In the meantime, and thanks to several comments, I have improved
the notation. The unwieldy notation for nonlocal variables has been
replaced by a more compact one. E.g.

?- Ys = [_,_], maplist(X+\Y^dif(X,Y), Ys).

Everything can now be used by using the module lambda,
discussed and described at:

http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord

Lambdas now allow to make also setof/3 simpler to use!

Comments welcome, as always!
_______________________________________________
SWI-Prolog mailing list
SWI-Prolog@...
https://mailbox.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog