Michael
Michael

Reputation: 10303

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?

Upvotes: 13

Views: 2563

Answers (2)

Don Stewart
Don Stewart

Reputation: 137947

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:

Upvotes: 13

Ryan Culpepper
Ryan Culpepper

Reputation: 10643

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.

Upvotes: 3

Related Questions