Call-with-current-continuation

Call-with-current-continuation

In computer 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 reifies the current continuation (i.e. control context or control state of the program) as an object and applies "f" to it. The continuation object is a first-class value with application as the only operation. Scheme does not syntactically distinguish continuation application from function application. When a continuation object is applied, 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. Continuations created with call/cc may be called more than once, and even from outside the dynamic extent of the call/cc 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.

Lisp users have criticized the "call/cc" function as an unnecessarily complicated and difficult-to-implement means.Fact|date=July 2007 They hold that each one of those tasks can and should be implemented without call/cc, and that doing so will provide faster and more efficient constructs. Indeed, in many implementations of Scheme, the implementation of call/cc comes with a large speed and space overhead as with each call to call/cc the control stack is copied and placed in the heap as a continuation object; a call to a continuation replaces the existing control stack with the encapsulated one. Kent Dybvig, the implementor of the Scheme implementation Chez Scheme, and his collaborators have shown, however, that this overhead can be reduced to a few bits over a programming language implementation that allows arbitrarily deep recursions. The technique employs a form of lazy stack copying, i.e. call/cc becomes a constant-time allocation operation, and returns to captured continuations copy the existing stack only as far as needed.

The Curry-Howard correspondence between proofs and programs relates "call/cc" to Peirce's law, which extends intuitionistic logic to non-constructive, classical logic: ((α → β) → α) → α. 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:

(define (f return) (return 2) 3)

(display (f (lambda (x) x)))

(display (call-with-current-continuation f))

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)(define (generate-one-element-at-a-time lst) ;; (-> X u 'you-fell-off-the-end) ;; This is the actual generator, producing one item from a-list at a time (define (generator) (call/cc control-state)) ;; Hand the next item from a-list to "return" or an end-of-list marker (define (control-state return) (for-each (lambda (element) (set! return (call/cc (lambda (resume-here) ;; Grab the current continuation (set! control-state resume-here) (return element))))) lst) (return 'you-fell-off-the-end)) ;; Return the generator generator)

(define generate-digit (generate-one-element-at-a-time '(0 1 2 3 4 5 6 7 8 9)))

(generate-digit)(generate-digit)

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.

See also

* Continuation passing style

External links

* [http://community.schemewiki.org/?call-with-current-continuation A short introduction to call-with-current-continuation]
* [http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_idx_566 Definition of call-with-current-continuation in the Scheme spec]
* [http://groups.google.com/group/comp.lang.lisp/msg/4e1f782be5ba2841 Humorous explanation of call-with-current-continuation from Rob Warnock in Usenet's comp.lang.lisp]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Continuation — For other uses, see Continuation (disambiguation). In computer science and programming, a continuation is an abstract representation of the control state of a computer program. A continuation reifies the program control state, i.e. the… …   Wikipedia

  • Continuation — Sur les autres projets Wikimedia : « Continuation », sur le Wiktionnaire (dictionnaire universel) En informatique, la continuation d un système désigne son futur, c est à dire la suite des instructions qu il lui reste à exécuter à… …   Wikipédia en Français

  • Continuation-passing style — In functional programming, continuation passing style (CPS) is a style of programming in which control is passed explicitly in the form of a continuation. Gerald Jay Sussman and Guy L. Steele, Jr. coined the phrase in AI Memo 349 (1975), which… …   Wikipedia

  • Continuation — Dieser Artikel wurde aufgrund von inhaltlichen Mängeln auf der Qualitätssicherungsseite der Redaktion Informatik eingetragen. Dies geschieht, um die Qualität der Artikel aus dem Themengebiet Informatik auf ein akzeptables Niveau zu bringen. Hilf… …   Deutsch Wikipedia

  • Call stack — In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run time stack, or machine stack, and… …   Wikipedia

  • Delimited continuation — In programming languages, a delimited continuation, composable continuation or partial continuation, is a slice of a continuation frame that has been reified into a function. Unlike regular continuations, delimited continuations return a value,… …   Wikipedia

  • Closure (computer science) — In computer science, a closure (also lexical closure, function closure, function value or functional value) is a function together with a referencing environment for the non local variables of that function.[1] A closure allows a function to… …   Wikipedia

  • goto — This article is about the programming statement. For other uses, see Goto (disambiguation). goto is a statement found in many computer programming languages. It is a combination of the English words go and to. It performs a one way transfer of… …   Wikipedia

  • Monad (functional programming) — In functional programming, a monad is a programming structure that represents computations. Monads are a kind of abstract data type constructor that encapsulate program logic instead of data in the domain model. A defined monad allows the… …   Wikipedia

  • Curry–Howard correspondence — A proof written as a functional program: the proof of commutativity of addition on natural numbers in the proof assistant Coq. nat ind stands for mathematical induction, eq ind for substitution of equals and f equal for taking the same function… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”