Jan 22 2002
Continuations
A continuation is a, or a description of a, point in an executing program. A continuation includes a current code pointer, frame variables and a set of expected stack parameters. Any point in a program is a continuation.
The examples below are all in a Java pseudo code. We avoid existing language implementations because the complexity of the semantics obfuscates the simplicity of the continuation concept.
Description
Let us define a very special function NOW(n), that returns a continuation starting at the +nth line of code and recording all frame variable values. The implementation of this function requires the implementing language to have code-level reflection. In any case, once we have a continuation object, we many do some things with it. Here is the stub for a continuation.
public class Continuation
public continue(Object returnValue); //continues the defined continuation
public Object getReturn(); //Used to extract the returnValues of the Continuation
//Continuation
We can make an infinite loop
public void main()
Continuation c=NOW(1);
println("Hello World!");
c.continue(null);
//main
Continuations can be used instead of the usual call/return paradigm. Consider a boring function
public int add(int x, int y)
return x+y;
//add
We can write this in continuation form by giving it another parameter; the point to return to
public add(int x, int y, Continuation c)
c.continue(x+y);
//add
Any function can use the add routine by defining the return point, and passing/extracting values to the function
public void main()
Continuation c=NOW(2)
add(2, 3, c);
Integer result=c.getReturn();
println("the result is ", result);
//main
Since a continuation is a point in an executing program, we can make continuations that point to the start of a function.
Continuation a=add
Furthermore, we can execute the function by just continuing...
a.continue(2, 3, c)
We will rewrite the main() function to be clear
public void main()
Continuation a=add;
Continuation c=NOW(2);
a.continue(2, 3, c);
Integer result=c.getReturn();
println("the result is ", result);
//main
To show the potential usefulness of a continuation we make a function that can be called seven times. Note that s returns a continuation to itself so that future invocations continue the for-loop.
public seven(Continuation c)
for(int t=0;t<7;t++)
println("t="+t);
c.continue(NOW(1));
//for
c.continue(null);
//seven
public void main()
Continuation s=Seven
while(s!=null)
Continuation c=NOW(2);
s.continue(c);
s=c.getReturn();
//for
//main
Returning a fresh continuation each time s is continued allows future invocations to remember the value of t. Technically, we can call seven() more than seven times by holding onto the original value of s.
Summary
Continuations act very much like gotos with parameter passing. Gotos are bad in procedural languages because they can be abused, so the same goes for continuations. Procedural languages gain little from the addition of continuations and may actually be hindered because of it ability to obfuscate code..
Continuations are used to define program flow, communication timing and state. They are useful in functional languages that would otherwise need more complex constructs to do the same.
Compilers use the continuation format because they generalize all program control flow, and help optimize function calls.