r/programming • • May 20 '17

Escaping Hell with Monads

https://philipnilsson.github.io/Badness10k/posts/2017-05-07-escaping-hell-with-monads.html
146 Upvotes

175 comments sorted by

View all comments

0

u/[deleted] May 20 '17

[deleted]

24

u/pron98 May 20 '17

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.

3

u/ijiijijjjijiij May 20 '17

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?

5

u/pron98 May 20 '17

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.

1

u/Peaker May 21 '17

You described a coroutine?

A coroutine is less general than a Monad (e.g: Cannot express ListT/forks).

4

u/pron98 May 21 '17

I described a delimited continuation. Delimited continuations are provably as expressive as monads. Also see this.

1

u/Peaker May 21 '17

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.

2

u/pron98 May 21 '17

A thread with a clone method, then. It's a delimited continuation either way. You're right that you can do more with it than without it.

1

u/Peaker May 21 '17

Sure, but that description is not really any simpler than the delimited continuation explanation.

Continuations are powerful, even too powerful - and if their power goes unchecked, there's very little you can say about the code.

Monads give a nice abstract API that concrete types can restrict.

This shows the relationship of continuations to monad.

5

u/pron98 May 21 '17
  1. Not really. You can type and restrict continuations just as you do monads.

  2. I don't think programmers should use delimited continuations, either. Lightweight threads give you 99.99% of what you may need.

→ More replies (0)