54

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?

Ben Millwood
  • 14,540
Casebash
  • 9,437
  • 1
    I added the intuition tag to reflect the "simple explanation" part. – BBischof Jul 21 '10 at 23:50
  • 4
    @Casebash: If you have no intention of accepting our answers, please stop asking these types of questions. –  Jul 22 '10 at 18:23
  • I don't think Haskell's monad and the monad in category theory are the same, but I could be wrong. – Jonathan Fischoff Jul 22 '10 at 21:42
  • 3
    @Jonathan Actually, http://www.haskell.org/tutorial/monads.html they are the same. I don't completely understand that link, but what I extract makes me think they are the same. And they come right out and say it. Also, our last names are similar. – BBischof Jul 22 '10 at 22:50
  • 1
    @BBischof good to know! I also found this link which looks promising. http://en.wikibooks.org/wiki/Haskell/Category_theory. Yes your name makes me think I misspelled my name :). – Jonathan Fischoff Jul 22 '10 at 23:28
  • My name is actually just a German word: bishop. When I was in Germany I was constantly noticing buildings named after me :D – BBischof Jul 23 '10 at 03:17
  • Its a monoid in the category of endofuntors. What's the problem? – Christopher King May 31 '15 at 00:56
  • 1
    TheCatsters have short YouTube videos explaining monads, and other parts of category theory: http://www.youtube.com/user/TheCatsters – Kevin H. Lin Jul 31 '10 at 01:03

3 Answers3

19

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$.
  • They are very much the same because a monad in Haskell is just an example of a category-theoretical monad in the category $\bf Hask$ (well, internal to Hask that is.) Its pretty much the same as $\bf Set$ though. – Christopher King May 31 '15 at 01:00
  • It looks convincing, and I upvoted it, but I still don't see it. Let's consider your second example. The endofunctor $T:C\to C$ is given by $X\to \bar{X}$ and $(X\subseteq Y)\to (\bar{X}\subseteq \bar{Y})$. This gives the only object of the monad (monoid), namely $T$. However, what are the morphisms in this Monad? It seems to me it cannot be neither $\mu$ nor $\eta$, as, e.g., for $\eta$ being a "morphism" in this monad, its "type" should be $\eta : T\to T$, such that $\eta_X : T(X)\to T(X)$ which is $\eta_X : \bar{X}\to\bar{X}$. – MASL May 19 '16 at 19:11
  • I mean, your examples are sound, and they clearly satisfy the definition of a monad as stated, say, in Riehl's book. What I have troubles with is in seeing how this is equivalent to the definition of it as "a monoid in the cat of endofunctors on $C$". I can indentify the only object of this monoid, namely, $T$(:$\bf{Set} \to {\bf Set}$ / ${\cal P}(S)\to {\cal P}(S)$) with the mapping of objects and arrorws in $C$ as you stated. What I fail to see is what the natural transformations are that act as morphisms of the corresponding monoid, and how they relate to $\eta$ and $\mu$. – MASL May 19 '16 at 20:32
  • @MASL It appears to me that "How can a monad be interpreted as a monoid in the category of endofunctors" is a different question than the one addressed with this answer? – Greg Graviton May 20 '16 at 09:53
  • @GregGraviton They are indeed, and that's why I submitted a separate question http://math.stackexchange.com/questions/1792409/monads-as-monoids which only is evidencing that I'm still conflating a few concepts here. Thanks for replying. – MASL May 20 '16 at 11:17
  • So, just to recap: in your examples the $η$ transformation is the same as the functor, and this is still a valid monad, right? – Hi-Angel Jan 09 '17 at 14:29
  • 2
    @Hi-Angel : Actually no, $\eta$ is not the functor. The functor is a mapping that assigns to each object another object and to each morphism another morphism, while $\eta$ is a morphism between two objects. The functor does not know what to do with "elements" of objects, only morphisms can do that. – Greg Graviton Feb 12 '17 at 15:42
19

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.

Casebash
  • 9,437
BBischof
  • 5,967
  • 2
    The link was actually the most useful answer posted, so I edited it into your answer and accepted it – Casebash Aug 18 '10 at 06:35
  • 1
    I find this and other similar descriptions of monads like the one by 97823123 rather un-enlightening. While knowing that monads are monoids in the (strict) monoidal endofunctor category (or functors in some specially concocted category) is an interesting and useful result, it fails to explain what monads are in any deeper sense or what they are good for. But the OP has already accepted this answer and any answer I could possibly give would need some heavy category theory, so I'll just register my disapointment and shut up. – G. Rodrigues Aug 18 '10 at 21:52
  • @G. Rodrigues Two things. First, I was going for simple as the OP asked. And second, isn't this formulation deep to you? I find it very deep and satisfying. As the representations of a monoidal category, I feel like monads are much more interesting than the normal definition. Please DON'T shut up. If not for the sake of the OP, then for my sake, please tell me your deeper formulation! I am very interested! I also probably have the necessary background. If you think this forum is inappropriate email me: bryan (dot) bischof (at) gmail (dot) com. I would love to discuss this with you! :D – BBischof Aug 18 '10 at 23:44
  • @G. Rodrigues, BBischof: As I said in my comment, the link was the best response I recieved to this answer. It gives a mathematical explanation of monads, while avoiding much of the complexities of category theory – Casebash Aug 19 '10 at 00:16
  • @BBischof: to answer your question, no not really. While the result is useful (some properties of the category of monads fall right out of it) it does not, as I said in my comment, explain what monads really are or what they are good for. IMHO, it is mostly a description of the same object under two different formal wrappings, but without any added insight. Since it seems I cannot stop jabbering, I will try to post an answer in the next few days. – G. Rodrigues Aug 19 '10 at 11:39
  • @Casebash: but that is precisely the gist of the problem. You cannot avoid the complexities of category theory if you want to understand monads (mathematically speaking that is. Maybe computer scientists have other ways to come to grips with them). Monads are subtle and complex thingumajigs -- advanced category theory as it were. I really cannot say I understand them fully and none of the answers here has furthered my understanding (but that may well be an exclusively personal problem). – G. Rodrigues Aug 19 '10 at 11:53
  • @G Rodrigues: Well, it was actually the two links posted that together provided a good explanation of monads. The second link was posted by Jonathan Fischoff went more into the mathematics, but he didn't post an answer, so wasn't eligible for it to be accepted. Of course this misses much of the subtleties of monads, so if you want to really understand them, like I do, the answer is to just start learning category theory. – Casebash Aug 19 '10 at 12:34
  • What's an endofunctor? What's "the category of endofunctors"? What's a "coherence relation"? What's a "monoidal product"? I assume "group action" means the same thing as "operation" in group theory? – Omnifarious Dec 19 '17 at 18:51
5

Let $C$ be a category. Then a monad based at $C$ is a monoid in the strict monoidal category

$$\mathcal{End}(C)=\mathcal{Hom}_{Cat}(C,C),$$

where the natural monoidal product is given by composition of endofunctors, and the monoidal unit is the identity functor.

A monoid in a monoidal category is defined here.

If you need more explanation, just give me a call.

Notation: The category of functors $C\to D$ is also written as $Fun(C,D)$, but this notation is nonstandard. The standard notations are $\mathcal{Hom}_{Cat}(C,D)$ or simply $Cat(C,D)$.