Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 624
Michael Avatar asked May 19 '11 12:05

Michael


2 Answers

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

  • Regular, "undelimited" continuations.
  • Delimited continuations.
  • Oleg's papers on delimited control
  • PFPL, ch33.

Notes

Some corrections from Oleg Kiselyov, received off-list:

  • Continuations and jumps are deeply related, the first discoveries of continuations were in the context of trying to understand the semantics of jumps, or goto. Landin's J operator was introduced precisely for that purpose; J is the precursor of call/cc. See An Introduction to Landin’s “A Generalization of Jumps and Labels”
  • Further reading on undelimited, "regular" continuations, in "Undelimited continuations are co-values rather than functions", and their foundations.
like image 194
Don Stewart Avatar answered Oct 12 '22 10:10

Don Stewart


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.

like image 30
Ryan Culpepper Avatar answered Oct 12 '22 10:10

Ryan Culpepper