Continuation

Continuation is an abstract concept higher, especially functional programming. It refers to the control state of a program at a particular time during its operation time. The term continuation therefore approximates the concept of process context, however, the process context includes the current state of program data, such as the values ​​of local and global variables with.

Access to continuations makes it possible to influence the control flow of a program as desired. So it is possible to stop a program at any time and continue later, or restore the program at a particular point in a previous state. This property also comes in the name of continuation, to German continued to express. Using Continuations any type of loop is simulated. The unrestricted access to the continuation as well as a form of a conditional statement are already sufficient to solve any computable problems. This capability enables a separate programming style, the so-called continuation- passing style.

Different programming languages ​​support the handling of continuations, the most famous representatives in the various Lispdialekte as Scheme. Modern object-oriented languages ​​provide exception handling with concepts such as limited opportunities to access the continuation of a program.

Examples

Finite Automata

In automata theory, a field of theoretical computer science, an essential theoretical construct is the finite state machine. This is located at any point in a potential of a finite set states. This state may be interpreted as the continuation of the time.

Modern microprocessors

Modern microprocessors are mostly Register Machines. On such a machine register, the contents of certain registers at a given time, the continuation of the running process at the time dar. Modern processors to take advantage of this to switch back and forth between multiple processes and thus to give the impression that they are running at the same time. If there is such a context switch, the processor saves the contents of all registers. Then he fills the registers with contents that were backed up at an earlier time from the registers. This is the process that was active at the time of saving, continues at exactly the point at which he was in at the time of backup. The continuation is in this case by the program counter, together with the contents of the call stack, or the contents of the stack pointer represents.

Continuations in Scheme

Scheme continuations treated as first -class objects, ie Scheme not only makes it possible to access the current continuation, but also to place this continuation in any variable and to pass functions as parameters. Scheme provides the function to call / cc. The following code implements the faculty by means of continuations:

(define fac ( lambda ( x)     ( let ( ( x s ) ( cont # f ) )       (call / cc ( lambda ( x ) ( set! cont x) # t))       ( set! s ( - s 1) )       (cond ( (< 1 s ) x) (else (begin ( set! x (* x s ) ) ( cont )))) ))) Here, the function call / cc is passed an anonymous function. call / cc calls this function with the current continuation, the anonymous function stores this continuation in the variable cont. In the last line is called with ( cont ) the continuation, the program flow therefore jumps to the statement following the call to call / cc.

201122
de