Continuation
From Wikipedia, the free encyclopedia
In computing, a continuation is a representation of some of the execution state of a program (often the call stack and the current Instruction pointer) at a certain point. Many languages have constructs that allow a programmer to save that execution state into an object, and then restore the state from this object at a later point in time (thereby resuming its execution). This technique has been used in functional programming, imperative programming, and message passing programming.
Continuations are also used in models of computation including the Actor model, process calculi, and the lambda calculus. Steve Russell invented the continuation in his second LISP implementation for the IBM 704, though he did not name it. Christopher Strachey, Christopher F. Wadsworth and John C. Reynolds brought the term continuation into prominence in their work in the field of denotational semantics that makes extensive use of continuations to allow sequential programs to be analysed in terms of functional programming semantics.
Contents |
[edit] Example
In Scheme:
(define theContinuation #f)
(define (test)
(let ((i 0))
; call/cc calls its first function argument, passing
; a continuation variable representing this point in
; the program as the argument to that function.
;
; In this case, the function argument assigns that
; continuation to the variable theContinuation.
(call/cc (lambda (k) (set! theContinuation k)))
;
; The next time theContinuation is called, we start here.
(set! i (+ i 1))
i
)
)
Defines a function test
that sets theContinuation
to the future execution state of itself:
> (test)
1
> (theContinuation)
2
> (theContinuation)
3
> (define anotherContinuation theContinuation)
> (test)
1
> (theContinuation)
2
> (anotherContinuation)
4
Another call with current continuation example:
(define my_abortable_procedure
(lambda (escape_procedure)
; Do stuff
(+ 1 1)
;nonlocal return
(escape_procedure 'SOMEVALUE)
(display 'NEVERREACHED_WHEN_ESCAPE_IS_CALLED)))
(display (call/cc my_abortable_procedure))
; displays: SOMEVALUE
; The return value of call/cc is the value either returned by my_abortable_procedure or
; the value passed to escape_procedure
[edit] Practical Uses of Continuations
One area that has seen practical use of continuations is in web programming [1] [2]. Two of the more popular continuation-aware web servers are the PLT Scheme Web Server, UnCommon Web Framework for Common Lisp and the Seaside Web Server for Smalltalk. The Apache Cocoon Web application framework also provides continuations (see Cocoon manual).
However, there is no consensus yet as to whether continuations are harmful or beneficial for Web programming.
[edit] Programming language support
Many programming languages exhibit such a feature under various names; specifically:
- C:
setcontext
et al. (UNIX System V and GNU libc) - Factor :
callcc0
andcallcc1
- Parrot:
Continuation
PMC; uses continuation passing style for all control flow - Rhino :
Continuation
- Ruby:
callcc
- Scheme:
call-with-current-continuation
(commonly shortened tocall/cc
) - Smalltalk:
Continuation currentDo:
, in most modern Smalltalk environments continuations can be implemented without additional VM support. - Standard ML of New Jersey:
SMLofNJ.Cont.callcc
- Unlambda:
c
, the flow control operation for call with current continuation - Pico:
call(exp())
andcontinue(aContinuation, anyValue)
In any language which supports closures, it is possible to write programs in continuation passing style and manually implement call/cc. This is a particularly common strategy in Haskell, where it is easy to construct a "continuation passing monad".
[edit] Kinds of continuations
Support for continuations varies widely. A programming language supports re-invocable continuations if a continuation may be invoked repeatedly (even after it has already returned). Re-invocable continuations were introduced by Peter J. Landin using his J (for Jump) operator that could transfer the flow of control back into the middle of a procedure invocation. Re-invocable continuations have also been called "re-entrant" in the MzScheme programming language. However this use of the term "re-entrant" is too easily confused with its use in discussions of multithreading.
At one time Gerry Sussman and Drew McDermott thought that using re-invocable continuations (which they called "Hairy Control Structure") was the solution to the AI control structure problems that had originated in Planner. Carl Hewitt et al. developed message passing as an alternative solution in the Actor model. Guy Steele and Gerry Sussman then developed the continuations in Scheme in their attempt to understand the Actor model.
A more limited kind is the escape continuation that may be used to escape the current context to a surrounding one. Many languages which do not explicitly support continuations support exception handling, which is equivalent to escape continuations and can be used for the same purposes. C's setjmp/longjmp
are also equivalent: they can only be used to unwind the stack. Escape continuations can also be used to implement tail-call optimization.
[edit] Disadvantages
Continuations are the functional expression of the GOTO statement, and the same caveats apply. While many believe that they are a sensible option in some special cases such as web programming, use of continuations can result in code that is difficult to follow. In fact, the esoteric programming language Unlambda includes call-with-current-continuation as one of its features solely because of its resistance to understanding. The external links below illustrate the concept in more detail.
[edit] Linguistics
Some phenomena in natural languages can be grasped using the notion of continuation. See Chris Barker's paper Continuations in Natural Language for details. See also Montague grammar.
[edit] See also
- Closure
- COMEFROM
- Continuation passing style
- Control flow
- Coroutine
- Denotational semantics
- GOTO
- Spaghetti stack
[edit] External links
- Continuations for Curmudgeons by Sam Ruby
- Teach Yourself Scheme in Fixnum Days by Dorai Sitaram features a nice chapter on continuations.
- Continuations and Stackless Python by Christian Tismer
- On-line proceedings of the Fourth ACM SIGPLAN Workshop on Continuations
- On-line proceedings of the Second ACM SIGPLAN Workshop on Continuations
- Continuation, functions and jumps
- Rhino With Continuations
- Continuations in pure Java from the RIFE web application framework
- Debugging continuations in pure Java from the RIFE web application framework
- PHP implementation of continuations
- Comparison of generators, coroutines, and continuations, source of the above example
[edit] References
- Peter Landin. A Generalization of Jumps and Labels Report. UNIVAC Systems Programming Research. August 1965. Reprinted in Higher Order and Symbolic Computation, 11(2):125-143, 1998, with a foreword by Hayo Thielecke.
- Drew McDermott and Gerry Sussman. The Conniver Reference Manual MIT AI Memo 259. May 1972.
- Daniel Bobrow: A Model for Control Structures for Artificial Intelligence Programming Languages IJCAI 1973.
- Carl Hewitt, Peter Bishop and Richard Steiger. A Universal Modular Actor Formalism for Artificial Intelligence IJCAI 1973.
- Christopher Strachey and Christopher P. Wadsworth. Continuations: a Mathematical semantics for handling full jumps Technical Monograph PRG-11. Oxford University Computing Laboratory. January 1974. Reprinted in Higher Order and Symbolic Computation, 13(1/2):135--152, 2000, with a foreword by Christopher P. Wadsworth.
- John Reynolds. Definitional Interpreters for Higher-Order Programming Languages Proceedings of 25th ACM National Conference, pp. 717-740, 1972. Reprinted in Higher-Order and Symbolic Computation 11(4):363-397, 1998, with a foreword.
- John Reynolds. On the Relation between Direct and Continuation Semantics Proceedings of Second Colloquium on Automata, Languages, and Programming. LNCS Vol. 14, pp. 141-156, 1974.
- Gerald Sussman and Guy Steele. SCHEME: An Interpreter for Extended Lambda Calculus AI Memo 349, MIT Artificial Intelligence Laboratory, Cambridge, Massachusetts, December 1975. Reprinted in Higher-Order and Symbolic Computation 11(4):405-439, 1998, with a foreword.
- Robert Hieb, R. Kent Dybvig, Carl Bruggeman. Representing Control in the Presence of First-Class Continuations Proceedings of the ACM SIGPLAN '90 Conference on Programming Language Design and Implementation, pp. 66-77.
- Will Clinger, Anne Hartheimer, Eric Ost. Implementation Strategies for Continuations Proceedings of the 1988 ACM conference on LISP and Functional Programming, pp. 124-131, 1988. Journal version: Higher-Order and Symbolic Computation, 12(1):7-45, 1999.