12

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?

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
Michael
  • 10,185
  • 12
  • 59
  • 110

2 Answers2

13

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:

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • 1
    On `J`, see Danvy, Shan, and Zerny's "J is for JavaScript: A direct-style correspondence between Algol-like languages and JavaScript using first-class continuations": http://www.cs.rutgers.edu/~ccshan/j/dsz.pdf – sclv May 24 '11 at 15:30
3

Continuations as a language feature (as opposed to continuations as a programming pattern) are reifications of (parts of) the control context ("the stack"). As Don said, undelimited continuations represent the whole context, whereas delimited continuations only represent part of it.

Typically, capturing an undelimited continuation (eg, with call/cc) doesn't change the control context; the control context is only changed when the continuation is invoked (ie, reflected onto the stack).

Typically, capturing a delimited continuation (eg, with shift) immediately aborts the segment of the control context up to the nearest delimiter (eg, reset) and reifies that as what seems to be a plain old function (although it might be implemented as stack trickery rather than however normal functions are implemented).

BTW, continuations are sometimes called "first-class jumps", but that doesn't mean they have any more to do with the jmp instruction than a normal function call does.

Ryan Culpepper
  • 10,495
  • 4
  • 31
  • 30