The predicates reset/3
implement delimited continuations for Prolog. Delimited
continuations for Prolog are described in Schrijvers et
al., 2013 (preprint
PDF). The mechanism allows for proper coroutines, two or
more routines whose execution is interleaved, while they exchange data.
Note that coroutines in this sense differ from coroutines realised using
attributed variables as described in chapter
Note that shift/1
captures the forward continuation. It notably does not capture
choicepoints. Choicepoints created before the continuation is captured
remain open, while choicepoints created when the continuation is
executed live their normal life. Unfortunately the consequences for committing
a choicepoint is complicated. In general a cut (!/0)
in the continuation does not have the expected result. Negation (\+/1)
and if-then(-else) (->/2)
behave as expected, provided the continuation is called immediately.
This works because for \+/1
->/2 the continuation
contains a reference to the choicepoint that must be cancelled and this
reference is restored when possible. If, as with tabling, the
continuation is saved and called later, the commit has no effect. We
illustrate the three scenarios using with the programs below.
reset(gbad, ball, Cont),
( Cont == 0
n, !, fail.
Here, the !/0 has no
The second example uses \+/1,
which is essentially
reset(gok, ball, Cont),
( Cont == 0
In this scenario the normal semantics of \+/1
In the last example we illustrate what happens if we assert the
continuation to be executed later. We write the negation using
if-then-else to make it easier to explain the behaviour.
:- dynamic cont/1.
reset(gassert, ball, Cont),
( Cont == 0
Now, t3/0 succeeds twice. This is because n/0 shifts, so the
commit to the fail/0
branch is not executed and the true/0
branch is evaluated normally. Calling the continuation later using c3/0
fails because the choicepoint that realised the if-then-else does not
exist in the continuation and thus the effective continuation is the
remainder of n/0 and
in gassert/0 .
The suspension mechanism provided by delimited continuations is used
to implement tabling Desouter et
al., 2015, (available
here). See section 7.
- Call Goal. If Goal calls shift/1
and the argument of shift/1
can be unified with Ball,78The
argument order described in Schrijvers et
al., 2013 is
We swapped the argument order for compatibility with catch/3 shift/1
to return, unifying
Continuation with a goal that represents the continuation
In other words, meta-calling Continuation completes the
execution where shift left it. If Goal does not call shift/1,
Continuation are unified with the integer
(zero).79Note that older versions
also unify Ball with
0. Testing whether or not
shift happened on Ball however is always ambiguous.
- Abandon the execution of the current goal, returning control to just
after the matching reset/3
call. This is similar to throw/1
except that (1) nothing is‘undone' and (2) the 3th argument of
is unified with the continuation, which allows the code calling reset/3
to resume the current goal.
- Similar to shift/1.
This version is intended for situations where it is assumed the
continuation is copied and saved to be executed one or multiple times in
a different context. This notably prevents restoring choice points saved