<p>I think the nLab definition is too abstract and is confusing you more than anything. Perhaps you should start from <a href="http://en.wikipedia.org/wiki/Monad_%28functional_programming%29" rel="nofollow noreferrer">Wikipedia</a>?  Anyway, here are some comments.</p>

<blockquote>
  <p>For the case of programming languages, what are the objects (0-cells), morphisms (1-cells) and 2-morphisms (2-cells)?</p>
</blockquote>

<p>In the case of programming languages (and in most usual cases) the bicategory in question is $\mathbf{Cat}$, the 2-category whose 0-cells are categories, 1-cells functors and 2-cells natural transformations.</p>

<p>Spelling out the nLab definition, the object is a category, the endomorphism is an endofunctor and multiplication and unit are natural transformations.</p>

<p>A typed functional programming language usually induces a category as follows: the objects of the category are types; a morphism $A\rightarrow B$ is an expression (<em>i.e.</em>, a program) of type B containing a parameter of typa A, modulo evaluation.  I am not overly familiar with Haskell but I believe that people call the induced category $\mathbf{Hask}$, so a Haskell monad is nothing but an endofunctor (with suitable natural transformations) of $\mathbf{Hask}$.  If I am not mistaken, in that context the unit is called "return", whereas multiplication is called "bind", <em>i.e.</em>, the <code>&gt;&gt;=</code> operator (the category-theorist's definition of multiplication $\mu$ may be easily recovered from <code>&gt;&gt;=</code>, and vice versa).</p>

<blockquote>
  <p>Are the endomorphism the lines of code preserving the type <strong>a</strong>?</p>
</blockquote>

<p>No.  As I said above, the endomorphisms are endofunctors. For Haskell, the object <strong>a</strong> is not a type but the category $\mathbf{Hask}$.</p>

<blockquote>
  <p>Then, can a computer program be represented in terms of these diagrams?</p>
</blockquote>

<p>The answer is actually yes, but not through the connection you are describing. In fact, the string diagrams you mentioned live in the 2-category $\mathbf{Cat}$, whereas programs live in the category induced by the programming language.  A little more on this topic may be found in the answers to <a href="https://cstheory.stackexchange.com/questions/16159/how-should-i-think-about-proof-nets">this cstheory.SE question</a>.</p>

CC BY-SA 3.0
2014-10-10T18:43:57.537Z
Is it possible to illustrate the equivalence of the diagrams using code rather than just category theory?
john mangual
CC BY-SA 3.0
2014-10-11T08:11:36.483Z
Yes of course! The [Wikipedia article on monads](http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#Monad_laws) does this precisely using Haskell, with respect to the bind/return formulation of monads. The nLab diagrams correspond to the multiplication/unit formulation, but in that case too may be formulated using code. In fact, @jozefg already did it in the answer to [your previous question](http://cs.stackexchange.com/questions/30757/how-is-io-a-monad).
Damiano Mazza
CC BY-SA 3.0
2014-10-09T09:08:54.743Z
2014-10-09T09:08:54.743Z
2017-04-13T12:32:30.623Z
Damiano Mazza
<p>I am trying to understand better how the category definition of monad is related to the computer science definition.  <a href="https://ncatlab.org/nlab/show/monad" rel="nofollow noreferrer">nlab</a> has a rather terse definition of Monad in terms of a <a href="https://ncatlab.org/nlab/show/bicategory" rel="nofollow noreferrer">bicategory</a>.</p>

<ul>
<li>an object $a$</li>
<li>an endomorphism $t: a \to a$</li>
<li>$2$-cells $\mu, \eta$ (multiplication and unit).</li>
</ul>

<p>For the case of programming languages, what are the objects (<strong>0</strong>-cells), morphisms (<strong>1</strong>-cells) and 2-morphisms (<strong>2</strong>-cells)? I am guessing the object is a type and multiplication is the <code>&gt;&gt;=</code> operator (some way of putting commands in succession).  I won' t know what the endomorphism $t$ could be.</p>

<p>I was told that <code>list</code> is a monad, so is <code>maybe</code> and <code>IO</code> but I don't see the category or endomorphisms there.</p>

<hr>

<p>Alternatively they have another definition that kind of makes sense:</p>

<blockquote>
  <p>Indeed, one can define a monad on an object a of a bicategory K as just a monoid in the endomorphism category K(a,a).</p>
</blockquote>

<p>Are the endomorphism the lines of code preserving the type <strong>a</strong>?</p>

<hr>

<p>nLab goes on to say, that monads can be represented as string diagrams.  Here are </p>

<p><img src="https://ncatlab.org/nlab/files/monad-data-unlabeled.png" alt=""></p>

<p>The axioms for a monad - could be represented in terms of the <code>&gt;&gt;=</code> operator in a language like Haskell - are represented in terms of string diagrams.</p>

<p><img src="https://ncatlab.org/nlab/files/monad-axioms-unlabeled.png" alt=""></p>

<p>Then, can a computer program be represented in terms of these diagrams?</p>

<hr>

<p>This is a sequel to a previous question:  <a href="https://cs.stackexchange.com/questions/30757/how-is-io-a-monad">https://cs.stackexchange.com/questions/30757/how-is-io-a-monad</a> and can be migrated at the discretion of the admins.</p>

<pl.programming-languages><ct.category-theory>
understanding programming monads through diagrams
john mangual
# understanding programming monads through diagrams

(Asked by: username_0 on 2014-10-08)

I am trying to understand better how the category definition of monad is related to the computer science definition. [nlab](https://ncatlab.org/nlab/show/monad) has a rather terse definition of Monad in terms of a [bicategory](https://ncatlab.org/nlab/show/bicategory).

* an object $a$
* an endomorphism $t: a \to a$
* $2$-cells $\mu, \eta$ (multiplication and unit).

For the case of programming languages, what are the objects (**0**-cells), morphisms (**1**-cells) and 2-morphisms (**2**-cells)? I am guessing the object is a type and multiplication is the `>>=` operator (some way of putting commands in succession). I won' t know what the endomorphism $t$ could be.

I was told that `list` is a monad, so is `maybe` and `IO` but I don't see the category or endomorphisms there.

---

Alternatively they have another definition that kind of makes sense:

> Indeed, one can define a monad on an object a of a bicategory K as just a monoid in the endomorphism category K(a,a).

Are the endomorphism the lines of code preserving the type **a**?

---

nLab goes on to say, that monads can be represented as string diagrams. Here are

![](https://ncatlab.org/nlab/files/monad-data-unlabeled.png)

The axioms for a monad - could be represented in terms of the `>>=` operator in a language like Haskell - are represented in terms of string diagrams.

![](https://ncatlab.org/nlab/files/monad-axioms-unlabeled.png)

Then, can a computer program be represented in terms of these diagrams?

---

This is a sequel to a previous question: <https://cs.stackexchange.com/questions/30757/how-is-io-a-monad> and can be migrated at the discretion of the admins.

**Tags:** pl.programming-languages, ct.category-theory

---

## 1 answers

### Answer by username_1 on 2014-10-09. Score: 3

I think the nLab definition is too abstract and is confusing you more than anything. Perhaps you should start from [Wikipedia](http://en.wikipedia.org/wiki/Monad_%28functional_programming%29)? Anyway, here are some comments.

> For the case of programming languages, what are the objects (0-cells), morphisms (1-cells) and 2-morphisms (2-cells)?

In the case of programming languages (and in most usual cases) the bicategory in question is $\mathbf{Cat}$, the 2-category whose 0-cells are categories, 1-cells functors and 2-cells natural transformations.

Spelling out the nLab definition, the object is a category, the endomorphism is an endofunctor and multiplication and unit are natural transformations.

A typed functional programming language usually induces a category as follows: the objects of the category are types; a morphism $A\rightarrow B$ is an expression (*i.e.*, a program) of type B containing a parameter of typa A, modulo evaluation. I am not overly familiar with Haskell but I believe that people call the induced category $\mathbf{Hask}$, so a Haskell monad is nothing but an endofunctor (with suitable natural transformations) of $\mathbf{Hask}$. If I am not mistaken, in that context the unit is called "return", whereas multiplication is called "bind", *i.e.*, the `>>=` operator (the category-theorist's definition of multiplication $\mu$ may be easily recovered from `>>=`, and vice versa).

> Are the endomorphism the lines of code preserving the type **a**?

No. As I said above, the endomorphisms are endofunctors. For Haskell, the object **a** is not a type but the category $\mathbf{Hask}$.

> Then, can a computer program be represented in terms of these diagrams?

The answer is actually yes, but not through the connection you are describing. In fact, the string diagrams you mentioned live in the 2-category $\mathbf{Cat}$, whereas programs live in the category induced by the programming language. A little more on this topic may be found in the answers to [this cstheory.SE question](https://cstheory.stackexchange.com/questions/16159/how-should-i-think-about-proof-nets).

> **Comments:**
> **username_0**: Is it possible to illustrate the equivalence of the diagrams using code rather than just category theory?  
> **username_1**: Yes of course! The [Wikipedia article on monads](http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#Monad_laws) does this precisely using Haskell, with respect to the bind/return formulation of monads. The nLab diagrams correspond to the multiplication/unit formulation, but in that case too may be formulated using code. In fact, @jozefg already did it in the answer to [your previous question](http://cs.stackexchange.com/questions/30757/how-is-io-a-monad).  

---

