In Haskell, Monad
is a class of type constructors which act on types that have the following functions implemented:
(>>=) :: m a -> (a -> m b) -> m b
(>>) :: m a -> m b -> m b
return :: a -> m a
and they satisfy the following "laws:"
return a >>= f = f a
m >>= return = m
(m >>= f) >>= g = m >>= m >>= (\x -> f x >>= g)
I understand why a type with these functions implemented with those constraints is useful, because I've used them. But to be completely honest, I'm interested in pure mathematics over programming, so I want to understand the category theoretic origins of monads.
Monads in category theory seem very different, they are ordered triples $\langle T, \mu, \nu\rangle$. Where $T$ is an endofunctor on a given category $C$, $\mu : 1_C \rightarrow T$ is a natural transformation as is $\nu : T\circ T \rightarrow T$ such that they all satisfy conditions reminiscent of the one's for Haskell monads. It seems like return
, which I understand to be the type constructor for an instance of monad, is the endofunctor $T$.
In essence I want to understand which functions are which natural transformations, and why, but I also want to understand why return
is an endofunctor, acting on a category, even though it seems like most types in Haskell are treated as objects.
I was once watching Bartosz Milewski lecture and he said that monads can be thought of as "monoids on the category of endofunctors." I stopped watching because I didn't know what monoids were at the time. But now when I think "monoid on the category of endofunctors" I think "The set of endofunctors on a category under composition." But I don't see how an object like this could relate to the definition of a monad, so if someone could explain that as well it would be greatly appreciated.