I think I understand (in general) what shift
and reset
mean. However I do not understand why they are named so ? What do shift
and reset
as Delimited Continuation primitives have to do with "shift" and "reset" words in English?
2 Answers
They're called so because of the way they are implemented (in general).
Quoted from Direct Implementation of Shift and Reset in the MinCaml Compiler
By interpreting a program using the continuation semantics, we can regard the state of the program as a continuation stack. Then,
reset
can be thought of as marking the continuation stack, andshift
capturing the continuation stack up to the nearest mark created byreset
. Here is the overview of the implementation:
- When calling
reset
, set a reset mark to the stack- When calling
shift
(fun k -> M)
, move a part of the stack frames up to the nearest reset mark to the heap- When calling a continuation
k
, set a reset mark to the stack and copy the corresponding frames from the heap to the stack top.A reset mark is inserted when
k
is called, because captured continuations are executed in an empty continuation.
-
11It's worth noting that Scala's delimited continuations aren't implemented in such a direct way - they can't be. The JVM doesn't expose stack manipulation primitives. Commented May 13, 2011 at 15:18
-
1In contrast, Scheme-48 does allow for a direct implementation (Gasbichler/Sperber), as does any language that exposes the call stack, like Squeak Smalltalk. Commented May 22, 2011 at 20:55
Because that's the way Danvy & Filinski called those two operators in the first paper where they exposed that model of continuation-passing style (see also here), and it is what Scala implements.
The implementation in Scala is described in this other paper. The reference therein to Danvy & Filinski is clear:
In this paper, we study the addition of control operators shift and reset to this language framework, which together implement static delimited continuations (Danvy and Filinski 1990, 1992)