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?
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:
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”
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With