Jump to content

Continuation

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Matthias.f (talk | contribs) at 17:57, 27 July 2007 (→‎See also). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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.

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 'NEVER-REACHED-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

Continuations in Web development

One area that has seen practical use of continuations is in Web programming.[1][2] Three of the most popular continuation-aware Web servers are the PLT Scheme Web Server, the UnCommon Web Framework for Common Lisp, and the Seaside Web Server for Smalltalk. The Apache Cocoon Web application framework also provides continuations (see the Cocoon manual).

However, there is no consensus yet as to whether continuations are harmful or beneficial for Web programming.

Programming language support

Many programming languages exhibit such a feature under various names; specifically:

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".

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.

Disadvantages

Continuations are the functional expression of the GOTO statement, and the same caveats apply.[citation needed] 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.

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.

See also

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.