I have been learning some functional programming recently and I so I have come across monads. I understand what they are in programming terms, but I would like to understand what they are mathematically. Can anyone explain what a monad is using as little category theory as possible?


Solution 1:

If you want to avoid too much category theory, you can first read this link to understand the definition of monads in Haskell. Then look at Wikibooks for a more mathematical look (thanks Jonathan Fischoff).

There are two descriptions that I know of. The first can easily be found by looking at wiki under Monad or consulting Harry's nice summary. The second is more interesting in my opinion.

I will assume that you don't know the definition of a monoidal action, if you do, just skip ahead.

A monoidal action is a functor from a monoid to the category of endofunctors on a category satisfying two coherence relations. These two coherence relations simply verify that your monoidal product is the same as composition in the target, and that the identity object behaves with the action. The relations are normally written as diagrams, but without latex implement, I wont type them here.

To get an idea of a monoidal action, consider a group action, and formulate it a little more categorically, by writing the two axioms as diagrams. These diagrams, when converted to the language of monoidal categories, are exactly those of a monoidal action.

Now the best part is once you have monoidal action, monads on a category are simply the category of monoidal actions from the trivial monoidal category to your category. Note here that the trivial monoidal category will be the monoidal category with one object one morphism and all the other monoidal data is trivially determined. The monadic coherence relations come for free from your monoidal action coherence relations.

So, my simple explanation?

In this way, we can formulate monads functorially as "representations" of the trivial monoidal category.

One can readily show the two definitions are the same.

Solution 2:

Monads in Haskell and monads in category theory are very much the same: A monad consists of a functor $T: C \to C$ and two natural transformations $\eta_X : X \to T(X)$ (return in Haskell) and $\mu_X : T(T(X)) \to T(X)$ (join in Haskell) subject to the following laws

$\mu_X \circ T(\eta_X) = \mu_X \circ \eta_{T(X)} = 1_{T(X)}$ (left and right unit laws)

$\mu_X \circ \mu_{T(X)} = \mu_X \circ T(\mu_X)$ (associativity)

So, compared to Haskell, the monad is defined in terms of return, join and fmap instead of return and (>>=). For more details on this, see also the Haskell wikibook.

Two examples may illuminate this definition.

The powerset functor

  • $\mathcal{P} = X \mapsto \mathcal{P}(X)$ maps a set to the set of its subsets.
  • Functions $f:X \to Y$ are extended point-wise to $\mathcal{P}(f):\mathcal{P}(X) \to \mathcal{P}(Y)$
  • $\eta_X : X \to \mathcal{P}(X)$ is the function $x \mapsto \left\{x\right\}$
  • $\mu_X : \mathcal{P}(\mathcal{P}(X)) \to \mathcal{P}(X)$ flattens the inner layer of subsets: $\mu_X(A) = \left\{ b | a \in A, b \in a \right\}$.
  • This is similar to the list monad in Haskell.

The closure operation on the subsets of a topological space $S$ is a monad, too.

  • The objects of the category $C$ are the subsets of a given topological space $S$.
  • There is an unique arrow $X \to Y$ between to objects $X$ and $Y$ exactly when $X \subseteq Y$.
  • The monad is given by the functor that maps each object $X$ to its topological closure $\bar X$ and the arrow $X \subseteq Y$ to the arrow $\bar{X}\subseteq \bar{Y}$.
  • Clearly, we have $X \subseteq \bar X$; this is $\eta_X$.
  • Also, we know that $\bar{\bar X} = \bar X$, in particular $\bar{\bar X} \subseteq \bar X$; this is $\mu_X$.