Search code examples
language-agnosticprogramming-languagesfunctional-programmingcontinuations

Difference between delimited and undelimited continuations


I guess the difference between delimited and undelimited continuations is like the difference between call and jump.

If we invoke delimited continuation it will return to the caller once it finishes. If we invoke undelimited continuation it works like goto and never returns to the caller.

Does it make sense? Am I missing something?


Solution

  • You're a bit off track. Continuations, of either flavor, have nothing much to do with goto (jumps). They have everything to do with the stack, however.


    Classic Continuations

    Remember regular continuations capture the notion of a control stack as a first class values. Stacks may be named, passed around as arguments, and values may be applied to them, yielding a change in control flow, with a simple API based on function application via callCC.

    Delimited Continuations

    What do delimited continuations add to the mix?

    Recall that regular continuations capture the entire call stack up to a certain point. What if we could put markers in that say exactly how much of the control stack to capture in the continuation? A sort of "delimit"ing of the control stack.

    That's the idea, and now you have super-cool delimited continuations: delimit, capture and manipulate arbitrary portions of a program, as a value. Perfect for resumption and incremental processing, and other tricky forms of control flow.

    References

    Notes

    Some corrections from Oleg Kiselyov, received off-list: