3 ms·
Monads don't compose, in the sense that if you two monads of different types you can't always mash them together to create a single monad that encompasses both
by noelwelsh 7y ago
Monads don't compose, in the sense that if you two monads of different types you can't always mash them together to create a single monad that encompasses both of the effect the two monads represent. This is not surprising as (side-)effects don't in general compose so there is no reason that monads should.
Now I disagree with Ron's assertion that continuations compose better than monads. Here's why:
All monads can be expressed in terms of the continuation monad. As the name suggests this is just a monadic encoding of continuations. (See https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/the-mother-of-all-monads https://www.schoolofhaskell.com/school/to-infinity-and-beyon...).
If your language has continuations built-in then all programs are effectively running in some "ambient" monad that can express all other monads. Just like if you have exceptions you're effectively running within an error-handling monad.
So essentially rather than writing `F A` (or `F[A]` or `F<A>` or whatever syntax your language supports) to represent effectful programs all programs are implicitly running within such an effect type.
- pron 7y agoIt's true that monads and delimited continuations are equivalent, but what we have here aren't any delimited continuations but what's known as multi-prompt delimited continuations, and I call them "scoped continuations" (http://blog.paralleluniverse.co/2015/08/07/scoped-continuations/ http://blog.paralleluniverse.co/2015/08/07/scoped-continuati...). Those compose well, and their pure-FP counterpart is algebraic effect systems (based on monads, but don't use monad transformers).
- rjn 7y agoThere are ways to compose monads, https://en.wikibooks.org/wiki/Haskell/Monad_transformers https://en.wikibooks.org/wiki/Haskell/Monad_transformers