A monad is a structure that describes generalized objects and morphisms.

1 Monad

The category \([\cC, \cC]\) of endofunctors is a strict monoidal category with the functor composition as the monoidal multiplication and the identity functor \(\id_\cC\) as the monoidal unit.

A monad in a category \(\cC\) is an endofunctor \(T: \cC \to \cC\) equipped with two natural transformations:

  • composition \(\mu: TT \nat T\)
  • unit \(\eta: \id_\cC \nat T\)

subject to

  • associativity: \(\mu (T \mu) = \mu (\mu T)\)
  • identity: \(\mu (T \eta) = \mu (\eta T) = \id_T\)

In other words, a monad is a monoid in the category of endofunctors.

-- bind
(>>=) :: Monad m => m a -> (a -> m b) -> m b
(=<<) :: Monad m => (a -> m b) -> m a -> m b
(>>)  :: Monad m => m a -> m b -> m b
(>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
(<=<) :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c)

2 T-algebra

For a monad \((T, \mu, \eta)\), a (left) algebra over a monad (T-algebra) is an M-algebra \((A, \act_A: TA \to A)\) in the category of endofunctors over the constant functor \(\Delta A\).

Thus, a T-algebra is compatible with

  • monad composition: \(\act_A \compL T \act_A = \act_A \compL \mu_A\)
  • monad unit: \(\act_A \compL \eta_A = \id_A\)

A T-algebra homomorphism is an F-algebra homomorphism.

3 Eilenberg–Moore category

For a monad \((T, \mu, \eta)\) in a category \(\cC\), the Eilenberg–Moore category \(\cC^T\) is a category of T-algebras and T-algebra homomorphisms.

4 Kleisli category

For a monad \((T, \mu, \eta)\) in a category \(\cC\), the Kleisli category \(\cC_T\) is a category whose

  • objects are \(\cC\)-objects
  • morphisms are Kleisli morphisms \(A \xto{f_\Kl} TB\)
  • composition is the Kleisli composition \(g_\Kl \compL_\Kl f_\Kl := \mu \compL Tg_\Kl \compL f_\Kl\)

5 Monadic adjunction

5.1 Adjunction to monad

An adjunction

\[(F: \cC \toot \cD: G, \epsilon: FG \nat \id_\cD, \eta: \id_\cC \nat GF)\]

gives rise to a monad

\[(GF: \cC \to \cC, G \epsilon F: GFGF \nat GF, \eta: \id_\cC \nat GF)\]

and a comonad

\[(FG: \cD \to \cD, F \eta G: FG \nat FGFG, \epsilon: FG \nat \id_\cD)\]

5.2 Monad to adjunction

6 Monad examples

6.1 Maybe

  • \(MA = 1 + A\) nothing or values
  • \(Mf = 1 + f\) computation with failure
  • \(\mu: MM \nat M\) failure propagation
  • \(\eta: \id \nat M\) just
  • \(A \to MB\) partial function
  • \(MA \to A\) pointed object

6.2 List

free monoid

  • \(\nil: 1 \to LA\) empty list
  • \(\concat: LA \times LA \to LA\) concatenation

free monoid monad

  • \(LA\) list
  • \(Lf\) list map
  • \(\mu: LL \nat L\) concatenation
  • \(\eta: \id \nat L\) singleton
  • \(A \to LB\) list function
  • \(LA \to A\) monoid \((A, \fold [e: 1 \to A, \cdot: A \times A \to A])\)
    • right identity \([a]\)
    • left identity \([[], [a]]\)
    • associativity \([[a, b], [c]]\)

6.3 Powerset

  • \(PA = 2^A\) powerset
  • \(Pf\) set map
  • \(\mu: PP \nat P\) union
  • \(\eta: \id \nat P\) singleton
  • \(A \to PB\) set function
  • \(PA \to A\) suplattice

6.4 Multiset

  • \(NA = \N^A\) multiset
  • \(Nf\) sum map
  • \(\mu: NN \nat N\) multiplication
  • \(\eta: \id \nat N\) singleton
  • \(A \to NB\) multiset function
  • \(NA \to A\) commutative monoid

6.5 Probability

  • \(PA\) probability measures
  • \(Pf\) pushforward measure
  • \(\mu: PP \nat P\) marginalization
  • \(\eta: \id \nat P\) Dirac measure
  • \(A \to PB\) conditional probability
  • \(PA \to A\) expectation

6.6 State

  • \([S, S \times A]\)

6.7 Continuation

  • \([[A, R], R]\)

7 Monoidal monad

A monoidal monad is a monad in the 2-category of monoidal categories, lax monoidal functors, and monoidal natural transformations.

8 Monoidal Kleisli category

For a monoidal monad \((T, \mu_T, \eta_T, \mu, \eta)\) in \((\cC, \otimes, I)\), the Kleisli category \(\cC_T\) inherits the monoidal structure from \(\cC\), whose

  • monoidal multiplication \(\otimes_\Kl\) on objects is the monoidal multiplication \(\otimes\) on \(\cC\)

  • monoidal multiplication \(\otimes_\Kl\) on morphisms is obtained by \(f_\Kl \otimes_\Kl g_\Kl := \mu (f_\Kl \otimes g_\Kl)\)

    \[(A \xto{f_\Kl} TB) \otimes_\Kl (C \xto{g_\Kl} TD) = A \otimes C \xto{f_\Kl \otimes_\Kl g_\Kl} T(B\otimes D) = A \otimes C \xto{f_\Kl \otimes g_\Kl} TB \otimes TD \xto{\mu_{B, D}} T(B \otimes D)\]

  • monoidal unit, associator, and left/right unitors are those of \(\cC\)