In programming languages, a delimited continuation, composable continuation or partial continuation, is a "slice" of a continuationframe that has been reified into a function. Unlike regular continuations, delimited continuations return a value, and thus may be reused and composed. Control delimiters, the basis of delimited continuations, were introduced by Matthias Felleisen in 1988[1] though early allusions to composable and delimited continuations can be found in Carolyn Talcott's Stanford 1984 dissertation, Felleisen et al.,[2] Felleisen's 1987 dissertation,[3] and algorithms for functional backtracking,
e.g., for pattern matching, for parsing, in the Algebraic Logic Functional programming language, and in the functional implementations of Prolog
where the failure continuation is often kept implicit and the reason of being for the success continuation is that it is composable.
History
Delimited continuations were first introduced by Felleisen in 1988[1] with an operator called , first introduced in a tech report in 1987,[2] along with a prompt construct . The operator was designed to be a generalization of control operators that had been described in the literature such as call/cc from Scheme, ISWIM's J operator, John C. Reynolds' escape operator, and others. Subsequently, many competing delimited control operators were invented by the programming languages research community such as prompt and control,[4]shift and reset,[5][6]cupto,[7]fcontrol, and others.
Examples
Various operators for delimited continuations have been proposed in the research literature.[8]
One independent proposal[5] is based on continuation-passing style (CPS) -- i.e., not on continuation frames—and offers two control operators, shift and reset, that give rise to static rather than to dynamic delimited continuations.[9]
The reset operator sets the limit for the continuation while the shift operator captures or reifies the current continuation up to the innermost enclosing reset. For example, consider the following snippet in Scheme:
(*2(reset(+1(shiftk(k5)))))
The reset delimits the continuation that shift captures (named by k in this example). When this snippet is executed, the use of shift will bind k to the continuation (+ 1 []) where [] represents the part of the computation that is to be filled with a value. This continuation directly corresponds to the code that surrounds the shift up to the reset. Because the body of shift (i.e., (k 5)) immediately invokes the continuation, this code is equivalent to the following:
(*2(+15))
In general, these operators can encode more interesting behavior by, for example, returning the captured continuation k as a value or invoking k multiple times. The shift operator passes the captured continuation k to the code in its body, which can either invoke it, produce it as a result, or ignore it entirely. Whatever result that shift produces is provided to the innermost reset, discarding the continuation in between the reset and shift. However, if the continuation is invoked, then it effectively re-installs the continuation after returning to the reset. When the entire computation within reset is completed, the result is returned by the delimited continuation.[10] For example, in this Scheme code:
(reset(*2(shiftkCODE)))
whenever CODE invokes (k N), (* 2 N) is evaluated and returned.
This is equivalent to the following:
(let((k(lambda(x)(*2x))))CODE)
Furthermore, once the entire computation within shift is completed, the continuation is discarded, and execution restarts outside reset. Therefore,
(reset(*2(shiftk(k(k4)))))
invokes (k 4) first (which returns 8), and then (k 8) (which returns 16). At this point, the shift expression has terminated, and the rest of the reset expression is discarded. Therefore, the final result is 16.
Everything that happens outside the reset expression is hidden, i.e. not influenced by the control transfer. For example, this returns 17:
(+1(reset(*2(shiftk(k(k4))))))
Delimited continuations were first described independently by Felleisen et al.[2] and Johnson.[11] They have since been used in a large number of domains, particularly in defining new control operators; see Queinnec[12] for a survey.
Let's take a look at a more complicated example. Let null be the empty list:
(reset(begin(shiftk(cons1(k(void))));; (1)null))
The context captured by shift is (begin [*] null), where [*] is the hole where k's parameter will be injected. The first call of k inside shift evaluates to this context with (void) = #<void> replacing the hole, so the value of (k (void)) is (begin #<void> null) = null. The body of shift, namely (cons 1 null) = (1), becomes the overall value of the reset expression as the final result.
A worked-out illustration of the (shift k k) idiom: the generalized curry function
The generalized curry function is given an uncurried function f and its arity (say, 3),
and it returns the value of (lambda (v1) (lambda (v2) (lambda (v3) (f v1 v2 v3)))).
This example is due to Olivier Danvy and was worked out in the mid-1980s.[13]
Here is a unit-test function to illustrate what the generalized curry function is expected to do:
These unit tests verify whether currying the variadic function + into an n-ary curried function and applying the result to n arguments yields the same result as applying + to these n arguments, for n = 0, 1, 2, 3, and 4.
The following recursive function is accumulator-based and eventually reverses the accumulator before applying the given uncurried function.
In each instance of the induction step, the function (lambda (v) ...) is explicitly applied to an argument in the curried application:
The following recursive function is continuation-based and involves no list reversal.
Likewise, in each instance of the induction step, the function (lambda (v) ...) is explicitly applied to an argument in the curried application:
The following recursive function, curry_d, is the direct-style counterpart of curry_c and features the (shift k k) idiom, using Andrzej Filinski's implementation of shift and reset in terms of a global mutable cell and of call/cc.[14]
In each instance of the induction step, the continuation abstraction is implicitly applied to an argument in the curried application:
The heart of the matter is the observational equivalence between
(reset (... (shift k k) ...))
and
(lambda (x) (reset (... x ...)))
where x is fresh and the ellipses represent a pure context,
i.e., one without control effects.
The definition of curry_d also illustrates static delimited continuations.
This static extent needs to be explicitly encoded if one wants to use control
and prompt:[15]
^Rémy, Didier; Gunter, Carl; Riecke, Jon G. (1995). "A generalization of exceptions and control in ML-like languages". Functional Programming Language and Computer Architecture.
^See for instance the operators offered by the racket/controlRacket library [1]; the following examples can run in Racket using (require racket/control)
^Gasbichler, Martin; Sperber, Michael (2002). International Conference on Functional Programming. CiteSeerX10.1.1.11.3425.
^Johnson, Gregory F. (June 1987). "GL: a denotational testbed with continuations and partial continuations". Proc. SIGPLAN '87 Symposium on Interpreters and Interpretive Techniques. pp. 218–225.
^Queinnec, Christian (April 1994). "A library of high-level control operators". Lisp Pointers, ACM SIGPLAN Special Interest Publ. On Lisp. 6. École Polytechnique and INRIA-Rocquencourt: 11–26. CiteSeerX10.1.1.29.4790.