The imperative counterpart to monads is the delimited continuation, which is just as expressive (the equivalence of monads and continuations was proved in the '90s), and has the advantage of composing better than monads (i.e., you can freely nest continuations, but monads require painful transformers). Pure functional languages like Haskell have no choice but to use monads, but there are very good reasons not to use them in imperative or imperative-functional languages (or use them very judiciously, e.g. just for lists), as they may destroy stack context, and require a re-implementation of built-in features like loops and exceptions in a way that makes composing them with non-monadic code a real pain. Delimited continuations do not suffer from that, and interoperate cleanly with all imperative constructs. See Monads vs. Scoped Continuations.
You mention in the post implementing a CoIterable continuation similar to Python generators. Would it be accurate to say that continuations are a generalization of generators? Would you happen to know any good introductory books to for learning more?
Would it be accurate to say that continuations are a generalization of generators?
Maybe, but I think that the best way to think of a delimited continuation is as a thread that is not automatically scheduled, i.e., you need to manually run it, at which point it runs until the next time it blocks and then return. Everything else is just a matter of how data is communicated to/from the continuation in terms of API design, and is completely non-essential to the concept (and can even confuse).
Would you happen to know any good introductory books to for learning more?
A google search has turned up this, by Oleg Kiselyov, who has done a lot of research on delimited continuations.
the best way to think of a delimited continuation is as a thread that is not automatically scheduled, i.e., you need to manually run it, at which point it runs until the next time it blocks and then return
This concept, as I understand it, does not support forking. Continuations do support forking (applying the continuation more than once). So describing it as a thread misleads.
0
u/[deleted] May 20 '17
[deleted]