Call-with-current-continuation

In functional programming, the function call-with-current-continuation, commonly abbreviated call/cc, is a control operator that originated in its current form in the Scheme programming language and now exists in several other programming languages.

Taking a function f as its only argument, call/cc takes the current continuation (i.e., a "snapshot" of the current control context or control state of the program) as an object and applies f to it. The continuation object is a first-class value and is represented as a function, with function application as its only operation. When a continuation object is applied to an argument, the existing continuation is eliminated and the applied continuation is restored in its place, so that the program flow will continue at the point at which the continuation was captured and the argument of the continuation will become the "return value" of the call/cc invocation. Continuations created with call/cc may be called more than once, and even from outside the dynamic extent of the call/cc application.

Making this type of implicit program state visible as an object is known in computer science as reification. (Note that Scheme does not syntactically distinguish continuation application from function application.)

With call/cc a programmer can implement a variety of complex control operators from other languages via a few lines of code, e.g., McCarthy's amb operator for non-deterministic choice, Prolog-style backtracking, Simula 67-style coroutines and generalizations thereof, Icon-style generators, or engines and threads.

Contents

Relation to non-constructive logic

The Curry-Howard correspondence between proofs and programs relates call/cc to Peirce's law, which extends intuitionistic logic to non-constructive, classical logic: ((α → β) → α) → α. Here, ((α → β) → α) is the type of the function f, which can either return a value of type α directly or apply an argument to the continuation of type (α → β). Since the existing context is deleted when the continuation is applied, the type β is never used and may be taken to be ⊥.

The principle of double negative elimination ((α → ⊥) → ⊥) → α is comparable to a variant of call-cc which expects its argument f to always evaluate the current continuation without normally returning a value.

Embeddings of classical logic into intuitionistic logic are related to continuation passing style translation.

Examples

As shown by the following example, call/cc can be used to emulate the functionality of the return statement known from C-style languages, which is missing from Scheme:

; displays 3
; displays 2

Calling f with a regular function argument first applies this function to the value 2, then returns 3. However, when f is passed to call/cc (as in the last line of the example), applying the parameter (the continuation) to 2 forces execution of the program to jump to the point where call/cc was called, and causes call/cc to return the value 2. This is then printed by the display function.

In the following example, call/cc is used twice: once to generate a "return" continuation as in the first example and once to suspend an iteration through a list of items:

;; [LISTOF X] -> ( -> X u 'you-fell-off-the-end)
;; Hand the next item from a-list to "return" or an end-of-list marker
;; Grab the current continuation
;; (-> X u 'you-fell-off-the-end)
;; This is the actual generator, producing one item from a-list at a time
;; Return the generator 
 

Every time the loop is about to process another item from the list, the function grabs the current continuation, and assigns it to the variable 'control-state'. This variable initially is the closure that iterates through all elements of the list. As the computation progresses, it becomes a closure that iterates through a suffix of the given list. While the use of "call/cc" is unnecessary for a linear collection, such as [LISTOF X], the code generalizes to any collection that can be traversed.

Call-with-current-continuation can also express other sophisticated primitives. For example, the following code performs cooperative multitasking using continuations:

;; Cooperative multitasking using call-with-current-continuation
;; in 25 lines of scheme
;; The list of threads waiting to run. This is a list of one
;; argument non-returning functions (continuations, mostly)
;; A continuation is a non-returning function, just like (exit),
;; in that it never gives up control to whoever called it.
;; A non-returning function. If there is any other thread
;; waiting to be run, it causes the next thread to run if there
;; is any left to run, otherwise it calls the original exit
;; which exits the whole environment.
;; The original exit which we override.
;; The overriding function.
;; There is another thread waiting to be run.
;; So we run it.
;; Since the readyList is only non-returning
;; functions, this will not return.
;; Nothing left to run.
;; The original (exit) is a non returning function,
;; so this is a non-returning function.
;; Takes a one argument function with a given
;; argument and forks it off.  The forked function's new
;; thread will exit if/when the function ever exits.
;; This function added to the 
;; readyList is non-returning,
;; since exit is non returning.
;; Gives up control for the next thread waiting to be run.
;; Although it will eventually return, it gives up control
;; and will only regain it when the continuation is called.
;; Capture the continuation representing THIS call to yield
;; Stick it on the ready list
;; Get the next thread, and start it running.
;; Run it.
 

It is customary to show the yin-yang puzzle while discussing call/cc:

 

See also

External links

This article was originally based on material from the Free On-line Dictionary of Computing, which is licensed under the GFDL.