9.4 Continuations
See Sub-expression Evaluation and Continuations and Prompts, Delimited Continuations, and Barriers for general information about continuations. PLT Scheme’s support for prompts and composable continuations most closely resembles Dorai Sitaram’s % and fcontrol operator [Sitaram93].
Scheme installs a continuation barrier around evaluation in the following contexts, preventing full-continuation jumps across the barrier:
applying an exception handler, an error escape handler, or an error display handler (see Exceptions);
applying a macro transformer (see Syntax Transformers), evaluating a compile-time expression, or applying a module name resolver (see Resolving Module Names);
applying a custom-port procedure (see Custom Ports), an event guard procedure (see Events), or a parameter guard procedure (see Parameters);
applying a security-guard procedure (see Security Guards);
applying a will procedure (see Wills and Executors); or
evaluating or loading code from the stand-alone MzScheme command line (see Starting MzScheme or MrEd).
In addition, extensions of PLT Scheme may install barriers in additional contexts. In particular, MrEd installs a continuation barrier around most every callback. Finally, call-with-continuation-barrier applies a thunk barrier between the application and the current continuation.
| |||||||||||||||||||||
| |||||||||||||||||||||
handler : (or/c procedure? false/c) = #f |
Calls thunk with the current continuation extended by a prompt. The prompt is tagged by prompt-tag, which must be a result from either default-continuation-prompt-tag (the default) or make-continuation-prompt-tag. The result of thunk is the result of the call-with-continuation-prompt call.
The handler argument specifies a handler procedure to be called in tail position with respect to the call-with-continuation-prompt call when the installed prompt is the target of a abort-current-continuation call with prompt-tag; the remaining arguments of abort-current-continuation are supplied to the handler procedure. If handler is #f, the default handler accepts a single abort-thunk argument and calls (call-with-continuation-prompt abort-thunk prompt-tag #f); that is, the default handler re-installs the prompt and continues with a given thunk.
| ||||||||||||||
prompt-tag : any/c | ||||||||||||||
v : any/c |
Resets the current continuation to that of the nearest prompt tagged by prompt-tag in the current continuation; if no such prompt exists, the exn:fail:contract:continuation exception is raised. The vs are delivered as arguments to the target prompt’s handler procedure.
The protocol for vs supplied to an abort is specific to the prompt-tag. When abort-current-continuation is used with (default-continuation-prompt-tag), generally a single thunk should be supplied that is suitable for use with the default prompt handler. Similarly, when call-with-continuation-prompt is used with (default-continuation-prompt-tag), the associated handler should generally accept a single thunk argument.
(make-continuation-prompt-tag sym) → continuation-prompt-tag? |
sym : symbol? |
Creates a prompt tag that is not equal? to the result of any other value (including prior or future results from make-continuation-prompt-tag). The optional sym argument, if supplied, is used when printing the prompt tag.
(default-continuation-prompt-tag) → continuation-prompt-tag? |
Returns a constant prompt tag for a which a prompt is installed at the start of every thread’s continuation; the handler for each thread’s initial prompt accepts any number of values and returns. The result of default-continuation-prompt-tag is the default tag for more any procedure that accepts a prompt tag.
| ||||||||||||||
proc : (continuation? . -> . any) | ||||||||||||||
|
Captures the current continuation up to the nearest prompt tagged by prompt-tag; if no such prompt exists, the exn:fail:contract:continuation exception is raised. The truncated continuation includes only continuation marks and dynamic-wind frames installed since the prompt.
The capture continuation is delivered to proc, which is called in tail position with respect to the call-with-current-continuation call.
If the continuation argument to proc is ever applied, then it removes the portion of the current continuation up to the nearest prompt tagged by prompt-tag (not including the prompt; if no such prompt exists, the exn:fail:contract:continuation exception is raised), or up to the nearest continuation frame (if any) shared by the current and captured continuations – whichever is first. While removing continuation frames, dynamic-wind post-thunks are executed. Finally, the (unshared portion of the) captured continuation is appended to the remaining continuation, applying dynamic-wind pre-thunks.
The arguments supplied to an applied procedure become the result values for the restored continuation. In particular, if multiple arguments are supplied, then the continuation receives multiple results.
If, at application time, a continuation barrier appears between the current continuation and the prompt tagged with prompt-tag, and if the same barrier is not part of the captured continuation, then the exn:fail:contract:continuation exception is raised.
A continuation can be invoked from the thread (see Threads) other than the one where it was captured.
proc : (continuation? . -> . any) | ||||||||||||
|
The call/cc binding is an alias for call-with-current-continuation.
| ||||||||||||||
proc : (continuation? . -> . any) | ||||||||||||||
|
Similar to call-with-current-continuation, but applying the resulting continuation procedure does not remove any portion of the current continuation. Instead, application always extends the current continuation with the captured continuation (without installing any prompts other than those be captured in the continuation). When call-with-composable-continuation is called, if a continuation barrier appears in the continuation before the closest prompt tagged by prompt-tag, the exn:fail:contract:continuation exception is raised.
| ||||||||||||||
proc : (continuation? . -> . any) | ||||||||||||||
|
Like call-with-current-continuation, but proc is not called in tail position, and the continuation procedure supplied to proc can only be called during the dynamic extent of the call-with-escape-continuation call. A continuation barrier, however, never prevents the application of the continuation.
Due to the limited applicability of its continuation, call-with-escape-continuation can be implemented more efficiently than call-with-current-continuation.
A continuation obtained from call-with-escape-continuation is actually a kind of prompt. Escape continuations are provided mainly for backward compatibility, since they pre-date general prompts in MzScheme, and because call/ec is often an easy replacement for call/cc to improve performance.
proc : (continuation? . -> . any) | ||||||||||||
|
The call/ec binding is an alias for call-with-escape-continuation.
(let/cc k body ) |
Equivalent to (call/cc (lambda (k) body )).
(let/ec k body ) |
Equivalent to (call/ec (lambda (k) body )).
(call-with-continuation-barrier thunk) → any |
Applies thunk with a barrier between the application and the current continuation. The results of thunk are the results of the call-with-continuation-barrier call.
| ||||||||||||||
prompt-tag : continuation-prompt-tag? | ||||||||||||||
cont : continuation? = (call/cc values) |
Returns #t if cont, which must be a continuation, includes a prompt tagged by prompt-tag, #f otherwise.
(continuation? v) → boolean? |
v : any/c |
Return #t if v is a continuation as produced by call-with-current-continuation, call-with-composable-continuation, or call-with-escape-continuation, #f otherwise.
v : any/c |
Returns #t if v is a continuation prompt tag as produced by default-continuation-prompt-tag or make-continuation-prompt-tag.
| |||||||||||||||||||||
Applies its three thunk arguments in order. The value of a dynamic-wind expression is the value returned by value-thunk. The pre-thunk procedure is invoked before calling value-thunk and post-thunk is invoked after value-thunk returns. The special properties of dynamic-wind are manifest when control jumps into or out of the value-thunk application (either due to a prompt abort or a continuation invocation): every time control jumps into the value-thunk application, pre-thunk is invoked, and every time control jumps out of value-thunk, post-thunk is invoked. (No special handling is performed for jumps into or out of the pre-thunk and post-thunk applications.)
When dynamic-wind calls pre-thunk for normal evaluation of value-thunk, the continuation of the pre-thunk application calls value-thunk (with dynamic-wind’s special jump handling) and then post-thunk. Similarly, the continuation of the post-thunk application returns the value of the preceding value-thunk application to the continuation of the entire dynamic-wind application.
When pre-thunk is called due to a continuation jump, the continuation of pre-thunk
jumps to a more deeply nested pre-thunk, if any, or jumps to the destination continuation; then
continues with the context of the pre-thunk’s dynamic-wind call.
Normally, the second part of this continuation is never reached, due to a jump in the first part. However, the second part is relevant because it enables jumps to escape continuations that are contained in the context of the dynamic-wind call. Furthermore, it means that the continuation marks (see Continuation Marks) and parameterization (see Parameters) for pre-thunk correspond to those of the dynamic-wind call that installed pre-thunk. The pre-thunk call, however, is parameterize-breaked to disable breaks (see also Breaks).
Similarly, when post-thunk is called due to a continuation jump, the continuation of post-thunk jumps to a less deeply nested post-thunk, if any, or jumps to a pre-thunk protecting the destination, if any, or jumps to the destination continuation, then continues from the post-thunk’s dynamic-wind application. As for pre-thunk, the parameterization of the original dynamic-wind call is restored for the call, and the call is parameterize-breaked to disable breaks.
In both cases, the target for a jump is recomputed after each pre-thunk or post-thunk completes. When a prompt-delimited continuation (see Prompts, Delimited Continuations, and Barriers) is captured in a post-thunk, it might be delimited and instantiated in such a way that the target of a jump turns out to be different when the continuation is applied than when the continuation was captured. There may even be no appropriate target, if a relevant prompt or escape continuation is not in the continuation after the restore; in that case, the first step in a pre-thunk or post-thunk’s continuation can raise an exception.
Examples: | |||||||||||||||||
| |||||||||||||||||
in pre out in post out | |||||||||||||||||
| |||||||||||||||||
cancel-canceled | |||||||||||||||||
| |||||||||||||||||
((1 . 5) (2 . 6) (3 . 5) (1 . 5) (2 . 6) (3 . 5)) |
9.4.1 Classical Control Operators
The bindings documented in this section are provided by the scheme/control library, not scheme/base or scheme.
The scheme/control library provides various control operators from the research literature on higher-order control operators, plus a few extra convenience forms. These control operators are implemented in terms of call-with-continuation-prompt, call-with-composable-continuations, etc., and they generally work sensibly together. Many are redundant; for example, reset and shift are aliases.
v : any/c |
Returns the vs to a prompt using the default continuation prompt tag and the default abort handler.
That is, (abort v ...) is equivalent to
| ||
|
Sitaram’s operators [Sitaram93].
The essential reduction rules are:
; where E has no % |
When handler-expr is omitted, % is the same as prompt.
| |
|
Among the earliest operators for higher-order control [Felleisen88, Sitaram90].
The essential reduction rules are:
(lambda (v) E[v]))) |
; where E has no prompt |
| |
|
Like prompt and control, but using specific prompt tags:
(prompt-at tag E[(control-at tag k expr)]) => (prompt-at tag |
((lambda (k) expr) |
(lambda (v) E[v]))) |
; where E has no prompt-at for tag |
| |
|
Danvy and Filinski’s operators [Danvy90].
The essential reduction rules are:
; where E has no reset |
The reset and prompt forms are interchangeable.
| |
|
Like reset and shift, but using the specified prompt tags.
| |
| |
| |
|
Generalizations of prompt, etc. [Shan04].
The essential reduction rules are:
(lambda (v) E[v])) |
The reset0 and prompt0 forms are interchangable. Furthermore, the following reductions apply:
(lambda (v) E[v]))) |
(prompt0 E[(control k expr)]) => (prompt0 ((lambda (k) expr) |
(lambda (v) E[v]))) |
That is, both the prompt/reset and control/shift sites must agree for 0-like behavior, otherwise the non-0 behavior applies.
| |
| |
| |
|
Variants of prompt0, etc. that accept a prompt tag.
The operators of Hieb and Dybvig [Hieb90].
The essential reduction rules are:
(spawn proc) => (prompt tag (proc (lambda (x) (abort tag x)))) |
; where E has no prompt-at for tag |
|
The operator of Queinnec and Serpette [Queinnec91].
The essential reduction rules are:
(proc (lambda (thunk) |
(abort tag thunk)) |
(lambda (proc) |
(control0-at tag k (proc k))))) |
; where E has no prompt-at for tag |
(prompt-at tag E[(control0-at tag k expr)]) => ((lambda (k) expr) |
(lambda (x) E[x])) |
; where E has no prompt-at for tag |
| |
| |
|
The operators of Gunter et al. [Gunter95].
In this library, new-prompt is an alias for make-continuation-prompt-tag, set is an alias for prompt0-at, and cupto is an alias for control0-at.