Continuation
- This article is about continuations as defined in computer science. See analytic continuation for the use of the term in complex analysis; see Continuation War for the Finno-Soviet conflict during World War II; and see continuation application for the special type of patent application.
In computing, a continuation is a representation of the execution state of a program (for example, the register contents and call stack) at a certain point in time. Many languages have constructs that allow a programmer to save the current 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 P. 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.
Programming language support
Many programming languages exhibit such a feature under various names; specifically:
- Scheme:
call/cc
(short forcall-with-current-continuation
) - Standard ML of New Jersey:
SMLofNJ.Cont.callcc
- C:
setcontext
et al. (UNIX System V and GNU libc) - Ruby:
callcc
- Smalltalk:
Continuation currentDo:
, in most modern Smalltalk environments continuations can be implemented without additional VM support. - Stackless Python: (see below) used to have a continuation module, but it has been removed.
- Rhino :
Continuation
- Haskell :
callCC
(in moduleControl.Monad.Cont
) - Factor :
callcc0
andcallcc1
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 multitasking.
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.
Disadvantages
Continuations are the functional expression of the GOTO statement, and the same caveats apply. While they are a sensible option in some special cases, use of continuations can result in code that is difficult to follow. In fact, the esoteric programming language Unlambda includes call-to-current-continuation as one of its features solely because of its resistance to understanding. The external links below illustrate the concept in more detail.
See also
- Continuation passing style
- closure
- coroutine
- spaghetti stack
- denotational semantics
- GOTO
- COMEFROM
- Continuation method
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
- setjmp/longjmp Illustrated from Linux Gazette
- 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
- Using continuations for web programming
- Rhino With Continuations
- Continuations in pure Java from the RIFE web application framework
- Debugging continuations in pure Java from the RIFE web application framework
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. 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
- Will Clinger, Anne Hartheimer, Eric Ost. Implementation Strategies for Continuations. Proceedings of the 1988 ACM conference on LISP and functional programming. 1988. Journal version: Higher-Order and Symbolic Computation, 12(1):7-45, 1999.