[Haskell-cafe] On finding the right exposition... (Monads)

Joachim Durchholz jo at durchholz.org
Fri Sep 17 20:29:41 UTC 2021


Am 17.09.21 um 21:47 schrieb Viktor Dukhovni:
> Personally [verbal sequential], I find strained analogies unhelpful
> and distracting, so no desk clerks shuffling forms for me.
> 
> I internalised Haskell monads, by implementing the Functor, Applicative
> and Monad instances for Reader and State a few times from scratch.  Once
> those were solid, ReaderT and StateT and their MonadTrans instances.
> 
> Unsurpsingly, I have a mental model built around sequencing of internal
> state updates while passing data from one state to use in choosing the
> next state transition.

I have been finding the Monad expositions misleading - that idea that 
monads are for sequencing or transitions or mutable state always felt 
wrong to me, since the monad laws don't talk about any of these things 
at all.

And as far as I could understand the concept, monads are not about 
state, they're about "composing a linear sequence of functions". The 
functions' types need not be the same (that would degenerate into the 
concept of associativity), but the result type of one function must be 
the parameter type of the next.
The monad can do whatever it likes with the functions - typically, wrap 
them in some other function and construct a function composition; there 
isn't much else you can do with them, though you're not doing much 
justice to the Maybe monad if you see it as a pipeline.
Ah... right... and of course the pseudo associativity. I.e. it doesn't 
matter what parts of the total sequence you construct first.

Of course, state transitions are a fine application of this concept.
Or anything else where you see a function as a computation step and you 
want to run these steps in sequence.
It's just that there are more kinds of monads.
Except if you see Maybe as "do these operations in sequence until you 
hit the first that returns None, then abort". It depends on seeing 
functions as executable things though, and that's not really what 
Haskell is trying to be (or was trying to be 20 years ago, maybe 
everybody is now happy that Haskel finally can express imperative stuff 
via monads... no idea what people think)

Regards,
Jo


More information about the Haskell-Cafe mailing list