Monads and monad transformers have a wholly undeserved reputation for being somewhat fearful and hard to understand. Unfortunately, even the best books on Haskell tend to further this reputation by choosing to introduce monads by giving a couple of examples, then stating the monad laws and running away very fast, and the treatment of monad transformers, even in the otherwise excellent Real World Haskell, is unclear at best.
As I said, this reputation is undeserved. In this paper I will attempt to give a general motivation for monads as machines which carry around information not all of which is visible at any time, and for which there is a defined recipe to, given two old machines, make a new one
. I then move on to do the same for the closely related topics of monoids, additive monads (MonadPlus in Haskell) and finally monad transformers.
For each of these types I give motivation, an explanation of why the monad laws etc are the way they are, and examples, generally simple ones. The section on monad transformers is more complex than the rest: I demonstrate a completely general way in which given two monads m and t then Trans t m a := m (t a) is a monad, and Trans t is a monad transformer. I then show that the classical MaybeT monad transformer is an instance of this construction. As far as I am aware, this is not written down anywhere easily accessible, so this is a useful contribution not only to pedagogy, but to the general theory of monad transformers.
So here it is:
- What is a monad? (PDF file)