All Topics  
Continuation

 

   Email Print
   Bookmark   Link






 

Continuation



 
 
In computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
 and programming, a continuation is an abstract representation of the control state
Control flow

In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
, or the "rest of computation" or "rest of code to be executed". This is closely linked with what part of the program you are in: which function and which line is being executed. The "current continuation" or "continuation of the computation step" are the instructions that will be executed after the current line of code is executed.

The term continuations can also be used to refer to first class continuations, which are constructs that give a programming language the ability to save the execution state at any point and return to that point at a later point in the program.

Programs must allocate space in memory for the variables its functions use.






Discussion
Ask a question about 'Continuation'
Start a new discussion about 'Continuation'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computing
Computing

Computing is usually defined as the activity of using and developing computer technology, computer hardware and computer software. It is the computer-specific part of information technology....
 and programming, a continuation is an abstract representation of the control state
Control flow

In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
, or the "rest of computation" or "rest of code to be executed". This is closely linked with what part of the program you are in: which function and which line is being executed. The "current continuation" or "continuation of the computation step" are the instructions that will be executed after the current line of code is executed.

The term continuations can also be used to refer to first class continuations, which are constructs that give a programming language the ability to save the execution state at any point and return to that point at a later point in the program.

Programs must allocate space in memory for the variables its functions use. Most programming languages use a call stack
Call stack

In computer science, a call stack is a dynamic Stack data structure that stores information about the active subroutines of a computer program....
 for storing the variables needed because it allows for fast and simple allocating and automatic deallocation of memory. There are also programming languages that use a heap
Dynamic memory allocation

In computer science, dynamic memory allocation is the allocation of computer storage storage for use in a computer program during the runtime of that program....
 for this, which allows for flexibility but with a higher cost for allocating and deallocating memory. These two different implementations both have benefits and drawbacks in the context of continuations .

Almost all languages have a means for manipulating the order of execution steps (i.e. manipulating the continuation of a computation step). The goto
GOTO

GOTO is a statement found in many computer programming languages. It is a combination of the English words wiktionary:go and wiktionary:to....
 is the most basic form of this. Control structures like if statements, loops
Control flow

In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
, return statements, break statements, and exit
Exit (operating system)

A computer process terminates its execution by making an exit system call. More generally, an exit in a multithreading environment means that a thread of execution has stopped running....
 are more structured (and limited) ways to manipulate the order of executing instructions and are essentially limited goto statements.

More complex constructs exist as well. For example in C
C (programming language)

C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
, setjmp can be used to jump from the middle of one function
Function

Function may refer to:* Function , explaining why a feature survived selection* Function , an abstract entity that associates an input to a corresponding output according to some rule...
 to another function, provided the second function is lower on the stack (if it is waiting for the first function to return, possibly among others). Other more complex examples include coroutine
Coroutine

In computer science, coroutines are program components that generalize subroutines to allow multiple entry points for suspending and resuming of execution at certain locations....
s in Simula 67
Simula

Simula is a name for two programming languages, Simula I and Simula 67, developed in the 1960s at the Norwegian Computing Center in Oslo, by Ole-Johan Dahl and Kristen Nygaard....
, tasklets in Stackless Python
Stackless Python

Stackless Python, or Stackless, is a Python interpreter, so named because it avoids depending on the C call stack for its own stack. The most prominent feature of Stackless is Microthread, which avoid much of the overhead associated with usual operating system Thread s....
, generators
Generator (computer science)

In computer science, a generator is a special subroutine that can be used to control the iteration behaviour of a control flow#Loops. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values....
 in Icon and Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
, Fibers
Fiber (computer science)

In computer science, a fiber is a particularly lightweight thread of execution.Like threads, fibers share address space; where a distinction exists, it is that fibers use Computer multitasking#Cooperative multitasking/time-sharing while threads use pre-emptive multitasking....
 in Ruby
Ruby (programming language)

Ruby is a dynamic programming language, reflection , general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features....
 (starting in 1.9.1), the backtracking
Backtracking

Backtracking is a general algorithm for finding all solutions to some computational problem, that incrementally builds candidates to the solutions, and abandons each partial candidate c as soon as it determines that c cannot possibly be completed to a valid solution ....
 mechanism in Prolog
Prolog

Prolog is a logic programming language. It is a general purpose language often associated with artificial intelligence and computational linguistics....
, and threads
Thread (computer science)

In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process....
.

History

The earliest description of continuations was made by Adriaan van Wijngaarden in September 1964. Wijngaarden spoke at the IFIP Working Conference on Formal Language Description Languages held in Baden bei Wien, Austria. As part of a formulation for an Algol 60 preprocessor, he called for a transformation of proper procedures into continuation-passing style.

Christopher Strachey
Christopher Strachey

Christopher Strachey was a United Kingdom computer scientist. He was one of the founders of denotational semantics, and a pioneer in programming language design....
, Christopher F. Wadsworth and John C. Reynolds
John C. Reynolds

John C. Reynolds is an United States computer scientist.John Reynolds studied at Purdue University and then earned a PhD in theoretical physics from Harvard University in 1961....
 brought the term continuation into prominence in their work in the field of denotational semantics
Denotational semantics

In computer science, denotational semantics is an approach to formalizing the meanings of programming languages by constructing mathematical objects which describe the meanings of expressions from the languages....
 that makes extensive use of continuations to allow sequential programs to be analysed in terms of functional programming
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
 semantics.

Steve Russell
Steve Russell

Steve "Slug" Russell is a programmer and computer scientist most famous for creating Spacewar!, one of the earliest videogames, in 1961 with the fellow members of the Tech Model Railroad Club at MIT working on a Digital Equipment Corporation Digital PDP-1....
 invented the continuation in his second Lisp implementation for the IBM 704
IBM 704

The IBM 704, the first mass-produced computer with floating point arithmetic hardware, was introduced by IBM in April, 1954. The 704 was significantly improved over the IBM 701 in terms of architecture as well as implementation, and was not compatible with its predecessor....
, though he did not name it.

First class continuations


First-class continuations are a language's ability to completely control the execution order of instructions. They can be used to jump to a function that produced the call to the current function, or to a function that has previously exited. One can think of a first-class continuation as saving the state of the program. However, it is important to note that true first-class continuations do not save program data, only the execution context. This is illustrated by the "continuation sandwich" description:

Say you're in the kitchen in front of the refrigerator, thinking about a sandwich. You take a continuation right there and stick it in your pocket. Then you get some turkey and bread out of the refrigerator and make yourself a sandwich, which is now sitting on the counter. You invoke the continuation in your pocket, and you find yourself standing in front of the refrigerator again, thinking about a sandwich. But fortunately, there's a sandwich on the counter, and all the materials used to make it are gone. So you eat it. :-)


Only a few programming languages provide full, unrestrained access to the continuation of a computation step. Scheme was the first full production system, providing first "catch" and then call/cc
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....
. Bruce Duba introduced call/cc into SML
Standard ML

Standard ML is a general-purpose, Module , functional programming language with compile-time type checking and type inference. It is popular among compiler writers and programming language researchers, as well as in the development of automated theorem proving....
. Some Smalltalk
Smalltalk

Smalltalk is an Object-oriented programming, Type system, reflection computer programming programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human?computer symbiosis." It was designed and created in part for educational use, more so for constructionist learning, at PARC by Al...
 and Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
 implementations provide similar access to continuations, though nothing as systematic as Scheme.

Continuations are also used in models of computation including denotational semantics
Denotational semantics

In computer science, denotational semantics is an approach to formalizing the meanings of programming languages by constructing mathematical objects which describe the meanings of expressions from the languages....
, the Actor model
Actor model

In computer science, the Actor model is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message receiv...
, process calculi, and lambda calculus
Lambda calculus

In mathematical logic and computer science, lambda calculus, also written as ?-calculus, is a formal system designed to investigate function definition, function application and recursion....
. These models rely on programmers or semantics engineers to write mathematical functions in the so-called continuation-passing style
Continuation-passing style

In functional programming, continuation-passing style is a style of programming in which control flow is passed explicitly in the form of a continuation....
. This means that each function consumes a function that represents the rest of the computation relative to this function call. To return a value, the function calls this "continuation function" with a return value; to abort the computation it returns a value.

Functional programmers who write their programs in continuation-passing style
Continuation-passing style

In functional programming, continuation-passing style is a style of programming in which control flow is passed explicitly in the form of a continuation....
 gain the expressive power to manipulate the flow of control in arbitrary ways. The cost is that they must maintain the invariants of control and continuations by hand, which is a highly complex undertaking.

Example


The Scheme programming language includes the control operator call-with-current-continuation
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....
 (short: call/cc) with which a Scheme program can manipulate the flow of control:

(define the-continuation #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 the-continuation. ; (call/cc (lambda (k) (set! the-continuation k))) ; ; The next time the-continuation is called, we start here. (set! i (+ i 1)) i))

Defines a function test that sets the-continuation to the future execution state of itself:

> (test) 1 > (the-continuation) 2 > (the-continuation) 3 > (define another-continuation the-continuation) ; stores the current continuation (which will print 4 next) away > (test) ; resets the-continuation 1 > (the-continuation) 2 > (another-continuation) ; uses the previously stored continuation 4

For a gentler introduction to this mechanism, see call-with-current-continuation
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....
.

Programming language support

Many programming languages exhibit first-class continuations under various names; specifically:

  • C
    C (programming language)

    C is a general-purpose computer programming language originally developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories to implement the Unix operating system....
    : setcontext
    Setcontext

    setcontext is one of a family of C library Subroutines used for context control. The setcontext family allows the implementation in C of advanced control flow design patterns such as iterators, fiber , and coroutines....
    et al. (UNIX System V
    UNIX System V

    Unix System V, commonly abbreviated SysV , is one of the versions of the Unix operating system. It was originally developed by AT&T and first released in 1983....
     and GNU
    GNU

    GNU is a computer operating system composed entirely of free software. Its name is a recursive acronym for GNU's Not Unix; it was chosen because its design is Unix-like, but differs from Unix by being free software and containing no Unix code....
     libc)
  • Factor
    Factor programming language

    Factor is a dynamically typed concatenative programming language programming language whose design and implementation is led by Slava Pestov. Factor's main influences are Joy programming language, Forth , Lisp programming language and Self programming language....
     : callcc0 and callcc1
  • Icon, Unicon : create, suspend, @ operator: coexpressions
  • Parrot
    Parrot virtual machine

    Parrot is a register machine virtual machine being developed using the C and intended to run dynamic languages efficiently. It uses just-in-time compilation for speed to reduce the interpretation overhead....
    : Continuation PMC; uses continuation passing style for all control flow
  • Perl
    Perl

    In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
    : and
  • Rhino
    Rhino (JavaScript engine)

    Rhino is an open source JavaScript engine. It is developed entirely in Java and managed by the Mozilla Foundation. The Foundation also provides an implementation of JavaScript in C known as SpiderMonkey ....
     : Continuation
  • Ruby: callcc
  • Scala: Responder
  • Scheme: call-with-current-continuation
    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....
    (commonly shortened to call/cc)
  • Smalltalk: Continuation currentDo:, in most modern Smalltalk environments continuations can be implemented without additional VM support.
  • Standard ML of New Jersey
    Standard ML of New Jersey

    Standard ML of New Jersey is a compiler and programming environment for Standard ML. Aside from its runtime system, which is written in C, SML/NJ is written in Standard ML....
    : SMLofNJ.Cont.callcc
  • Unlambda
    Unlambda

    Unlambda is a minimal functional programming language programming language invented by David Madore. It is based on combinatory logic, a version of the lambda calculus that omits the lambda operator....
    : c, the flow control operation for call with current continuation
  • Pico: call(exp) and continue(aContinuation, anyValue)


In any language which supports closures
Closure (computer science)

In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables....
, it is possible to write programs in continuation passing style and manually implement call/cc. This is a particularly common strategy in Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
, where it is easy to construct a "continuation passing monad" (for example, the Cont monad and ContT monad transformer in the mtl library).

Continuations in Web development

One area that has seen practical use of continuations is in Web programming. The use of continuations shields the programmer from the stateless
Stateless server

A stateless server is a server that treats each request as an independent transaction that is unrelated to any previous request....
 nature of the HTTP protocol. In the traditional model of web programming, the lack of state is reflected in the program's structure, leading to code constructed around a model that lends itself very poorly to expressing computational problems. Thus continuations enable code that has the useful properties associated with inversion of control
Inversion of Control

Inversion of Control, or IoC, is an abstract principle describing an aspect of some software architecture designs in which the control flow of a system is inverted in comparison to the traditional architecture of Library ....
, while avoiding its problems. is a paper that provides a good introduction to continuations applied to web programming.

Some of the more popular continuation-aware Web server
Web server

The term web server can mean one of two things:# A computer program that is responsible for accepting Hypertext Transfer Protocol requests from clients , and Server them HTTP responses along with optional data contents, which usually are web pages such as Hypertext Markup Language documents and linked objects ....
s are the , the and for Common Lisp
Common Lisp

Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
, the Seaside framework for Smalltalk
Smalltalk

Smalltalk is an Object-oriented programming, Type system, reflection computer programming programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human?computer symbiosis." It was designed and created in part for educational use, more so for constructionist learning, at PARC by Al...
 and for OCaml. The Apache Cocoon
Apache Cocoon

Apache Cocoon, usually just called Cocoon, is a web application framework built around the concepts of Pipeline , separation of concerns and component-based web development....
 Web application framework
Web application framework

A web application framework is a software framework that is designed to support the web development of Dynamic web page, Web applications and Web services....
 also provides continuations (see the ).

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
Peter J. Landin

Peter Landin is a United Kingdom computer scientist. He was one of the first to realize that the lambda calculus could be used to model a programming language, an insight that is essential to development of both functional programming and denotational semantics....
 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" can be easily confused with its use in discussions of multithreading
Thread (computer science)

In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process....
.

At one time Gerry Sussman and Drew McDermott
Drew McDermott

Drew McDermott is a Professor of Computer Science at Yale University. He was born in 1949, and lived in the Midwestern United States , for four years, in Brazil ....
 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
Planner programming language

Planner is a programming language designed by Carl Hewitt at MIT, and first published in 1969. First subsets such as Micro-Planner and Pico-Planner were implemented, and then essentially the whole language was implemented in Popler....
. Carl Hewitt
Carl Hewitt

Carl E. Hewitt is Associate Professor Emeritus in the electrical engineering and computer science department at the Massachusetts Institute of Technology ....
 et al. developed message passing as an alternative solution in the Actor model
Actor model

In computer science, the Actor model is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message receiv...
. 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
Exception handling

Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions - special conditions that change the normal flow of execution....
, 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
Tail recursion

In computer science, tail recursion is a special case of Recursion_ in which the last operation of the function, the tail call, is a recursive call....
.

Disadvantages

Continuations are the functional expression of the GOTO
GOTO

GOTO is a statement found in many computer programming languages. It is a combination of the English words wiktionary:go and wiktionary:to....
 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
Esoteric programming language

An esoteric programming language is a programming language designed as a test of the boundaries of computer programming language design, as a proof of concept, or as a joke....
 Unlambda
Unlambda

Unlambda is a minimal functional programming language programming language invented by David Madore. It is based on combinatory logic, a version of the lambda calculus that omits the lambda operator....
 includes call-with-current-continuation
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....
 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 for details. See also Montague grammar
Montague grammar

Montague grammar is an approach to natural language semantics, named after American logician Richard Montague. The Montague grammar is based on formal logic, especially lambda calculus and set theory, and makes use of the notions of intensional logic and type theory....
.

See also

  • Call-with-current-continuation
    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....
  • Closure
    Closure (computer science)

    In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables....
  • COMEFROM
    COMEFROM

    In computer programming, COMEFROM is an obscure control flow structure used in some programming languages, primarily as a joke.COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement....
  • Continuation passing style
  • Control flow
    Control flow

    In computer science control flow refers to the order in which the individual statement , Instruction or function calls of an imperative programming or functional programming computer program are execution or evaluated....
  • Coroutine
    Coroutine

    In computer science, coroutines are program components that generalize subroutines to allow multiple entry points for suspending and resuming of execution at certain locations....
  • Delimited continuation
    Delimited continuation

    In programming languages, a composable continuation, delimited continuation or partial continuation, is a "slice" of a continuation stack frame that has been Reification into a function ....
  • Denotational semantics
    Denotational semantics

    In computer science, denotational semantics is an approach to formalizing the meanings of programming languages by constructing mathematical objects which describe the meanings of expressions from the languages....
  • GOTO
    GOTO

    GOTO is a statement found in many computer programming languages. It is a combination of the English words wiktionary:go and wiktionary:to....
  • Spaghetti stack
    Spaghetti stack

    A spaghetti stack in computer science is an N-ary tree data structure in which child nodes have pointers to the parent nodes . When a list of nodes is traversed from a leaf node to the root node by chasing these parent pointers, the structure looks like a linked list stack ....


External links

  • by Sam Ruby
  • by Dorai Sitaram features a nice chapter on continuations.
  • by Christian Tismer
  • from the RIFE
    RIFE

    RIFE is a full-stack open source Java web application framework with tools and APIs to implement most common web features. Each of its toolkits is usable by itself and together they offer powerful integrated features that boost your productivity....
     web application framework
  • from the RIFE
    RIFE

    RIFE is a full-stack open source Java web application framework with tools and APIs to implement most common web features. Each of its toolkits is usable by itself and together they offer powerful integrated features that boost your productivity....
     web application framework