The continuation is an abstract concept of higher , especially functional programming languages . It describes the control status of a program at a certain point in time during its runtime . The term continuation therefore corresponds approximately to the concept of the process context , but the process context includes the current state of the program data, e.g. B. the values of local and global variables.
Access to continuations makes it possible to influence the control flow of a program as required . This makes it possible to pause a program at any point in time and continue it later, or to restore the program to an earlier state at a specific point. This property is also on behalf of continuation to German continued to express. Any kind of loop can be simulated using continuations . Unrestricted access to the continuation and a form of conditional statement are sufficient to solve any predictable problem. This ability enables an individual programming style, the so-called continuation-passing style .
Different programming languages support the handling of continuations, the best known representatives are different Lisp dialects like Scheme . Modern object-oriented languages, with concepts such as exception handling , offer limited options for accessing the continuation of a program.
In automata theory , a sub-area of theoretical computer science , an essential theoretical construct is the finite automaton . This is at any point in time in one of a finite set of possible states. This state can be interpreted as the continuation at the respective point in time.
Modern microprocessors are mostly register machines . On such a register machine, the content of certain registers at a given point in time represents the continuation of the executed process at the respective point in time. Modern processors use this to switch back and forth between several processes and thus give the impression that they are being executed simultaneously . If such a context change is pending , the processor saves the content of all registers. He then fills the registers with content that was saved from the registers at an earlier point in time. This means that the process that was active at the time of the backup is continued at exactly the point at which it was at the time of the backup. The continuation is represented by the command counter , together with the content of the call stack or the content of the stack pointer .
Continuations in Scheme
Scheme treats continuations as first-class objects ; H. Scheme not only makes it possible to access the current continuation, but also to store this continuation in any variable and to pass functions as parameters. Scheme offers the function for this
call/cc. The following code implements the faculty using continuations:
(define fac (lambda (x) (let ((a 1) (cont #f)) (call/cc (lambda (k) (set! cont k) #t)) (cond ((< x 2) a) (else (set! a (* a x)) (set! x (- x 1)) (cont))))))
The function is given
call/ccan anonymous function.
call/cccalls this function with the current continuation, the anonymous function saves this continuation in the variable
cont. In the last line,
(cont)the continuation is called using , the program flow therefore jumps to the instruction after the call from
- Christian Queinnec: Lisp in Small Pieces . Cambridge University Press, 1994, ISBN 0-521-54566-8 .