# What is a Monad, Really?

Ever since I started learning mathematics I wondered what the actual fuck is a monad. I’ve been programming since high-school, and I started learning Haskell about six months before my first semester at university. Learning Haskell was quite a paradigm shift from the more traditional, imperative languages I was used too, but I eventually able to understand it enough that I could be productive with it.

I learned how to work with typical monads in Haskell, such as `[a]`

and ```
Maybe
a
```

, yet I wasn’t able to understand what that all meant in mathematical terms.
Being a student of pure mathematics, that bothered me for a while. Well, I’m
happy to say I was finally able to understand it. I’ll try to summarize my
knowledge in this article, in the hopes that it helps other people understand
what in God’s green earth is a Monad.

## The Formal Definition

I suppose you could look it up in Wikipedia, but I’ll write it down in here anyways. Monads are a very special type of endofunctors — functors from a category to itself. Let $\text{C}$ be a category and let $M:\text{C}\to \text{C}$ be a functor. Let $\eta :{1}_{\text{C}}\Rightarrow M$ and $\mu :{M}^{2}\Rightarrow M$ be natural transformations such that the following diagrams commute:

Then the triple $\left(M,\eta ,\mu \right)$ — often denoted by simply $M$ — is said to be a monad. Ok, but what does this all mean?

## What is a Category, Really?

Category theory can be though-of as a generalization of Set Theory: it’s a uniform language for describing sets with a given structure — those are called objects — and functions that preserves structure between such sets — those are called morphisms or homomorphisms. A category is nothing more than that: a collection of sets with a given structure together with a collection of transformations between such sets.

For example, the category $k\text{-}\text{Vect}$ of vector spaces over $k$ is the collection of all vector-spaces over $k$, together with the collection of all linear transformations between such vector-spaces.

Note that the identity function $i{d}_{V}:V\to V$ is a linear transformation. Furthermore, if $T:{V}_{1}\to {V}_{2}$ and $S:{V}_{2}\to {V}_{3}$ are linear transformations, then the composition $S\u26acT:{V}_{1}\to {V}_{3}$ is a linear transformation. This is a key concept in Category Theory! The identity function and the composition of two morphisms must be morphisms for a suitable collection to be called a category!

In Haskell there’s only one category we care about: the category of Haskell types, whose objects are Haskell types and whose morphisms are Haskell functions.

## What is a Functor, Really?

A functor is a map between categories: it takes objects and morphisms as input and returns objects and morphism from another category. In other words, a functor $F:C\to D$ is a map that associates an object $F\left(X\right):D$ for each object $X:C$ and a marphism $F\left(f\right):F\left(X\right)\to F\left(Y\right)$ for each morphism $f:X\to Y$ in $C$. For $F$ to be a functor it must also preserve identity — i.e. $F\left({1}_{X}\right)={1}_{F\left(X\right)}$ — and composition:

The idea behind this is that $F$ not only maps objects to objects — like a regular function or a morphism — it also gives us a consistent way to turn every morphism in $C$ into a morphism in $D$.

This mechanism for converting morphisms in $C$ to morphisms in
$D$ is sometimes referred-to as *penetration*, such as in
Haskell’s standard library:

```
class Functor f where
-- `fmap g fa` "penetrates" `g` in `fa`.
-- A implementation of `fmap` is expected to
-- respect the composition of functions, i.e.
-- `fmap (h . g) == (fmap h) . (fmap g)`.
fmap :: (a -> b) -> f a -> fb
```

Note that an instance of `Functor`

is an endofunctor of the category of Haskell
types.

### Example

Consider the map $P:\text{Set}\to \text{Set}$ where $P\left(X\right)$ is the power set of $X$ and $P\left(f\right)$ — where $f:X\to Y$ — is the function that takes a subset of $X$ to it’s image under $f$.

Here $\text{Set}$ denotes the category of sets, where objects are sets
and morphisms are regular functions. We not only know how to map a set to it’s
power set, we also know to how turn every *function* between sets into a
function between their corresponding power sets. This is the power set functor.

## What is a Natural Transformation, Really?

Natural transformations are maps between functors: it takes objects as inputs and returns morphisms between the images of those objects under different functors. If $F,G:C\to D$ are functors a natural transformation $\eta :F\Rightarrow G$ from $F$ and $G$ takes $X:C$ to a morphism ${\eta}_{X}:F\left(X\right)\to G\left(X\right)$. For $\eta $ to be considered a natural transformation the following diagram must commute for each $X,Y:C$ and $f:X\to Y$:

The idea in here is that you have *natural* way to convert the action of
$F$ to the action of $G$. In other words,
$\eta :F\Rightarrow G$ is a morphism between $F$ and $G$
in the category of functors between $C$ and $D$ — where the objects are functors between $C$ and $D$.

### Example

Consider the maps $\eta :{1}_{\text{Set}}\Rightarrow P$ and $\mu :{P}^{2}\Rightarrow P$ given by ${\eta}_{X}\left(x\right)=\left\{x\right\}$ and ${\mu}_{X}\left(S\right)={\cup}_{s\in S}s$, respectively. Here ${1}_{\text{Set}}$ denotes the identity functor on the category of sets, i.e. ${1}_{\text{Set}}:\text{Set}\to \text{Set}$ is such that ${1}_{\text{Set}}\left(X\right)=X$ and ${1}_{\text{Set}}\left(f\right)=f$. On the other hand, ${P}^{2}$ denotes the functor that takes a set to the power set of it’s power set. This are both natural transformation.

We not only know how to take a set $X$ to it’s power set $P$ and a function $f:X\to Y$ to the corresponding $P\left(f\right):P\left(X\right)\to P\left(Y\right)$, we also know how to convert each element of $X$ into an element of $P\left(X\right)$ — using the map ${\eta}_{X}$ given by $x\mapsto \left\{x\right\}$ — and how to flatten an element of $P\left(P\left(X\right)\right)$ into an element of $P\left(X\right)$ — using the map ${\mu}_{X}$ given by $S\mapsto {\cup}_{s\in S}s$. Can you see where I’m going?

## What is a Monad, Really?

A monad is simply an endofunctor $M$ — a functor from a category to itself — equipped with two natural transformations $\eta :{1}_{C}\Rightarrow M$ and $\mu :{M}^{2}\Rightarrow M$ such that the following diagrams commute:

The idea is that we have natural and consistent ways of converting elements of an object $X:C$ into elements of $M\left(X\right)$ and elements of $M\left(M\left(X\right)\right)$ into elements of $M\left(X\right)$ — this is sometimes called flattening.

For the haskellers out there, $\eta $ is `return`

and $\mu $ is
`(>>= id)`

. For the mathematicians out there, `return`

is $\eta $ and
`(>>= f)`

is ${\mu}_{Y}\u26acM\left(f\right)$.

### Examples

The power set functor $P$ equipped with the natural transformations $\eta :{1}_{\text{Set}}\Rightarrow P$ and $\mu :{P}^{2}\Rightarrow P$ given by ${\eta}_{X}\left(x\right)=\left\{x\right\}$ e ${\mu}_{X}\left(S\right)={\cup}_{s\in S}s$ is a monad! In fact, all of the following diagrams commute:

## Conclusion

I hope this helps someone to understand monads. Happy haskelling!