I found an example of shift-reset delimited continuations in Haskell here:
resetT $ do alfa bravo x <- shiftT $ \esc -> do charlie lift $ esc 1 delta lift $ esc 2 return 0 zulu x
This will:
Perform
alfa
Perform
bravo
Perform
charlie
Bind
x
to 1, and thus performzulu 1
Fall off the end of
resetT
, and jump back to just afteresc 1
Perform
delta
Bind
x
to 2, and thus performzulu 2
Fall off the end of
resetT
, and jump back to just afteresc 2
Escape from the
resetT
, causing it to yield 0
I can't figure out how to write the equivalent code using SWI-Prolog's shift/1 and reset/3.
The code below is my attempt. The output is the same, but it seems messy and backwards, and I feel like I'm misusing Ball
to get something similar to the esc 1
and esc 2
in the Haskell example. Also, I am not sure what to do with return 0
.
% not sure about this...
example :-
reset(step, ball(X), Cont),
( writeln("charlie"), X=1, call(Cont), fail
; writeln("delta"), X=2, call(Cont)).
step :-
writeln("alfa"),
writeln("bravo"),
shift(ball(X)),
format("zulu ~w~n", X).
I'm rather confused: Scheme/Haskell/ML-style shift-reset and Prolog shift-reset seem almost like entirely different things! For example, you pass a lambda into Haskell's shiftT
but you do not pass a goal into Prolog's shift/1.
Where is the Prolog equivalent of Haskell's \esc -> ... esc 1
or return 0
? And where is the Haskell equivalent of Prolog's Ball
or call(Cont)
?
I feel that a "proper" port of the Haskell example above would answer these questions.
The reset
and shift
operators originally come from Danvy; Filinski,1990. “Abstracting Control”. The corresponding interfaces in Haskell Control.Monad.Trans.Cont conform to the original semantics, except for some type restrictions. The delimited continuation interfaces in SWI-Prolog are not exactly the original reset
and shift
. They are more closely related to Felleisen,1988's prompt and control or Sitaram’s fcontrol and run operators.
Usually, it is not difficult to translate delimited continuation programs from Haskell to Prolog. The difficulty in your example is that it calls the same continuation esc
twice with different values. For example,
example :-
reset(step, ball(X), Cont),
X=1, call(Cont),
X=2, call(Cont).
After the first call(Cont)
, X
is already bound to 1
, you cannot rebind it to 2
.
TomSchrijvers' advice is to create copies of the continuation with fresh unification variables using copy_term/2 (yes, continuations are also terms in SWI-Prolog!), so the Prolog equivalent of your example is
example(X) :-
reset(step, Ball, Cont),
copy_term(Cont+Ball, Cont1+Ball1),
copy_term(Cont+Ball, Cont2+Ball2),
writeln("charlie"),
ball(X1) = Ball1,
X1=1, reset(Cont1, _, _),
writeln("delta"),
ball(X2) = Ball2,
X2=2, reset(Cont2, _, _),
X=0.
step :-
writeln("alfa"),
writeln("bravo"),
shift(ball(X)),
format("zulu ~w~n", X),
shift(ball(X)).
?- example(X).
alfa
bravo
charlie
zulu 1
delta
zulu 2
X = 0.
More detail discussion, see https://swi-prolog.discourse.group/t/naming-clarification-about-delimited-continuation-reset-3-and-shift-1