---
title: Monads from Adjunctions
module: Monads and Algebras
moduleNumber: 7
lessonNumber: 1
order: 701
summary: >
  A monad on a category is an endofunctor equipped with a unit and a
  multiplication satisfying associativity and unit laws — the data of a monoid,
  written internally to the category of endofunctors. Every adjunction induces
  one, and the list, exception, and state constructions that model computational
  effects are all monads on Set.
topics: [Monads and Algebras]
sources:
  - book: Barr & Wells
    ref: "Ch. 14 — Algebras for endofunctors; §14.3 Triples"
  - book: Leinster
    ref: "Notes and further reading (monads as adjunction-induced structure)"
draft: false
---

An [adjunction](/category-theory/adjunctions/adjunctions) $F \dashv G$ between
categories $\mathcal{A}$ and $\mathcal{B}$ induces structure on the domain side.
The round trip $A \mapsto GFA$ is an endofunctor
$T = GF : \mathcal{A} \to \mathcal{A}$; the
[unit](/category-theory/adjunctions/units-and-counits) $\eta : \mathrm{id} \to GF$
is a natural transformation into it; and the counit $\varepsilon : FG \to \mathrm{id}$,
inserted in the middle, produces a natural transformation $GFGF \to GF$ collapsing
two applications of $T$ into one. That triple of data (an endofunctor, a unit
into it, and a flattening map) is a **monad**, and it satisfies laws that make it
a monoid.

Barr and Wells call the structure a **triple**, and the two words name the same
thing.[^bw-triple] The monad records what an adjunction does to its domain
without reference to the codomain $\mathcal{B}$ or the functor $F$ that reached
it. Two different adjunctions can induce the same monad, and every monad can be
resolved back into an adjunction, generally into many.

## The definition

> **Definition (Monad).** A **monad** (or **triple**) $\mathbb{T} = (T, \eta, \mu)$
> on a category $\mathcal{A}$ consists of a functor $T : \mathcal{A} \to \mathcal{A}$
> together with natural transformations $\eta : \mathrm{id}_{\mathcal{A}} \to T$
> (the **unit**) and $\mu : T^2 \to T$ (the **multiplication**), such that the
> following diagrams commute for every object:
>
> $$
> \mu \circ T\mu = \mu \circ \mu T
> \qquad\text{(associativity)},
> $$
>
> $$
> \mu \circ \eta T = \mathrm{id}_T = \mu \circ T\eta
> \qquad\text{(left and right unit)}.
> $$

Here $T\mu$ and $\mu T$ are the two whiskerings of $\mu$ by $T$: $T\mu$ has
component $T(\mu_A) : T^3 A \to T^2 A$, while $(\mu T)_A = \mu_{TA} : T^3 A \to T^2 A$.
The associativity law states that the two ways of contracting a triple stack
$T^3 A$ down to $T A$ agree. The unit law states that inserting a fresh $T$-layer
by $\eta$ (either underneath, as $\eta T$, or on top, as $T\eta$) and then
multiplying returns the identity.

$$
% caption: The associativity square: contracting the outer pair of $T$-layers
% ($m T$, writing $m$ for $\mu$) or the inner pair ($T m$) and then multiplying
% again give the same map $T^3 A \to T A$.
\begin{tikzpicture}[>=stealth, font=\small,
  nd/.style={minimum size=8mm, inner sep=2pt}]
  \definecolor{acc}{HTML}{4A6FA5}
  \node[nd] (ttt) at (0,2.4)   {$T^3 A$};
  \node[nd] (ttr) at (3.2,2.4) {$T^2 A$};
  \node[nd] (ttl) at (0,0)     {$T^2 A$};
  \node[nd] (ta)  at (3.2,0)   {$T A$};
  \draw[->, acc, thick] (ttt) -- (ttr) node[midway, above] {$Tm$};
  \draw[->, acc, thick] (ttt) -- (ttl) node[midway, left]  {$mT$};
  \draw[->, acc, thick] (ttr) -- (ta)  node[midway, right] {$m$};
  \draw[->, acc, thick] (ttl) -- (ta)  node[midway, below] {$m$};
\end{tikzpicture}
$$

$$
% caption: The two unit triangles: inserting a layer with the unit $u$ (writing
% $u$ for $\eta$) underneath ($u T$) or on top ($T u$), then multiplying, is the
% identity on $T A$.
\begin{tikzpicture}[>=stealth, font=\small,
  nd/.style={minimum size=8mm, inner sep=2pt}]
  \definecolor{acc}{HTML}{4A6FA5}
  \node[nd] (l)  at (0,1.9)   {$T A$};
  \node[nd] (m)  at (2.6,1.9) {$T^2 A$};
  \node[nd] (r)  at (5.2,1.9) {$T A$};
  \node[nd] (b)  at (2.6,0)   {$T A$};
  \draw[->, acc, thick] (l) -- (m) node[midway, above] {$uT$};
  \draw[->, acc, thick] (r) -- (m) node[midway, above] {$Tu$};
  \draw[->, acc, thick] (m) -- (b) node[midway, right] {$m$};
  \draw[->, black, thick] (l) -- (b) node[midway, below left] {$=$};
  \draw[->, black, thick] (r) -- (b) node[midway, below right] {$=$};
\end{tikzpicture}
$$

The diagonal maps in the second figure are identity arrows $T A \to T A$; the
triangles assert that the two composites down through $T^2 A$ equal them.

## The monoid analogy

The laws reproduce the axioms of a monoid, transcribed one level up. A monoid
$(M, e, m)$ in $\mathbf{Set}$ has a multiplication $m : M \times M \to M$ and a
unit $e : 1 \to M$ satisfying associativity and the unit laws. Replace the set
$M$ by an endofunctor $T$, the cartesian product $\times$ by composition of
endofunctors, the one-point set $1$ by the identity functor, and the axioms
become the monad laws.[^bw-monoid]

| Monoid in $\mathbf{Set}$ | Monad on $\mathcal{A}$ |
| --- | --- |
| carrier set $M$ | endofunctor $T : \mathcal{A} \to \mathcal{A}$ |
| unit $e : 1 \to M$ | unit $\eta : \mathrm{id} \to T$ |
| multiplication $m : M \times M \to M$ | multiplication $\mu : T \circ T \to T$ |
| $m(e, x) = x = m(x, e)$ | $\mu \circ \eta T = \mathrm{id} = \mu \circ T\eta$ |
| $m(m(x,y),z) = m(x,m(y,z))$ | $\mu \circ \mu T = \mu \circ T\mu$ |

The category of endofunctors $[\mathcal{A}, \mathcal{A}]$ carries a composition
$\circ$ that plays the role of a product (a **monoidal** structure, with the
identity functor as its unit). A monad is a monoid object in that monoidal
category. Reading the table line by line gives the standard formulation: a monad
is a monoid in the category of endofunctors.

## Adjunctions induce monads

> **Proposition (Adjunction-induced monad).** Let $F : \mathcal{A} \to \mathcal{B}$
> be left adjoint to $G : \mathcal{B} \to \mathcal{A}$, with unit
> $\eta : \mathrm{id} \to GF$ and counit $\varepsilon : FG \to \mathrm{id}$. Then
> $(GF, \eta, G\varepsilon F)$ is a monad on $\mathcal{A}$.[^bw-adj]

Write $T = GF$. The unit of the monad is the unit of the adjunction. The
multiplication $\mu = G\varepsilon F$ has component $G(\varepsilon_{FA}) : GFGFA
\to GFA$: it applies the counit in the middle, at the object $FA$, and transports
the result back through $G$. The associativity and unit laws of the monad follow
from the two triangle identities of the adjunction, applied under $G$ and over
$F$.

$$
% caption: An adjunction $F \dashv G$ between $\mathcal{A}$ (drawn $\mathbf{A}$)
% and $\mathcal{B}$ (drawn $\mathbf{B}$) yields the monad $T = G F$ on
% $\mathcal{A}$; the counit at $F A$ supplies the multiplication and the
% adjunction unit supplies the monad unit.
\begin{tikzpicture}[>=stealth, font=\small]
  \definecolor{acc}{HTML}{4A6FA5}
  \node[draw, minimum width=24mm, minimum height=15mm] (A) at (0,0) {$\mathbf{A}$};
  \node[draw, minimum width=24mm, minimum height=15mm] (B) at (5,0) {$\mathbf{B}$};
  \draw[->, acc, thick] (A.north east) to[bend left=22] node[midway, above] {$F$} (B.north west);
  \draw[->, acc, thick] (B.south west) to[bend left=22] node[midway, below] {$G$} (A.south east);
  \node[font=\footnotesize] at (2.5,0.05) {adjoint pair};
  \draw[->, black, thick] (A.north west) to[out=155, in=205, looseness=1.7] node[midway, left] {$T=GF$} (A.south west);
\end{tikzpicture}
$$

Every adjunction gives a monad by this proposition, and conversely every monad
arises from some adjunction, generally from many. The
[Kleisli](/category-theory/monads-algebras/kleisli-and-programming) and
[Eilenberg–Moore](/category-theory/monads-algebras/algebras-eilenberg-moore)
constructions produce two canonical adjunctions resolving a given monad; they sit
at the two extremes of a poset of such resolutions.

### The free monoid, as a monad

The free–forgetful [adjunction](/category-theory/adjunctions/free-forgetful-adjunctions)
between $\mathbf{Set}$ and $\mathbf{Mon}$ produces the first computational
example. The free monoid on a set $A$ is $A^\ast$, the set of finite strings
(lists) over $A$; forgetting the monoid structure returns the underlying set.
Composing free after forgetful gives the endofunctor

$$
T A = A^\ast = \{\, \text{finite lists of elements of } A \,\},
$$

with $T f : A^\ast \to B^\ast$ mapping $f$ over each list. This is the **list
monad**.[^bw-list] Its unit and multiplication read off directly:

- **Unit** $\eta_A : A \to A^\ast$ sends an element $a$ to the singleton list
  $[a]$.
- **Multiplication** $\mu_A : (A^\ast)^\ast \to A^\ast$ takes a list of lists and
  concatenates it, erasing the inner brackets. For example
  $\big[[a,b],[c,d,e],[\,],[a,a]\big]$ flattens to $[a,b,c,d,e,a,a]$.

$$
% caption: The list monad's multiplication flattens a list of lists by
% concatenation; the unit wraps a single element as a one-element list.
\begin{tikzpicture}[>=stealth, font=\footnotesize,
  bx/.style={draw, minimum height=7mm, inner sep=3pt}]
  \definecolor{acc}{HTML}{4A6FA5}
  \node[bx] (nest) at (0,0) {[[a,b],[c,d,e],[ ],[a,a]]};
  \node[bx] (flat) at (6.4,0) {[a,b,c,d,e,a,a]};
  \draw[->, acc, thick] (nest) -- (flat) node[midway, above] {$m$};
  \node[bx] (elt) at (0,-1.6) {a};
  \node[bx] (one) at (6.4,-1.6) {[a]};
  \draw[->, black, thick] (elt) -- (one) node[midway, above] {$u$};
\end{tikzpicture}
$$

Associativity of the monad is associativity of concatenation:
flattening a triply-nested list of lists gives the same result whether the inner
brackets or the outer brackets go first. The unit laws say that wrapping every
element as a singleton and then flattening, or wrapping the whole list once and
then flattening, both leave the list unchanged.

> **Worked example (List monad laws on explicit lists).** Fix
> $A = \{a, b, c, d\}$ with $TA = A^\ast$, $\eta_A(a) = [a]$, and $\mu_A$ =
> concatenation. The three laws hold on concrete inputs.
>
> _Left unit_ $\mu_A \circ \eta_{TA}$ wraps a whole list, then flattens:
> $$
> \eta_{TA}([a, b, c]) = \big[\,[a, b, c]\,\big],
> \qquad
> \mu_A\big(\big[\,[a, b, c]\,\big]\big) = [a, b, c].
> $$
> _Right unit_ $\mu_A \circ T\eta_A$ wraps each element, then flattens:
> $$
> T\eta_A([a, b, c]) = \big[\,[a], [b], [c]\,\big],
> \qquad
> \mu_A\big(\big[\,[a], [b], [c]\,\big]\big) = [a, b, c].
> $$
> _Associativity_ $\mu_A \circ T\mu_A = \mu_A \circ \mu_{TA}$, on the
> triply-nested list $x = \big[\,[[a,b],[c]],\ [[d]]\,\big] \in T^3 A$. Flattening
> the inner brackets first,
> $$
> T\mu_A(x) = \big[\,[a, b, c],\ [d]\,\big]
> \xrightarrow{\ \mu_A\ } [a, b, c, d],
> $$
> and flattening the outer brackets first,
> $$
> \mu_{TA}(x) = \big[\,[a, b],\ [c],\ [d]\,\big]
> \xrightarrow{\ \mu_A\ } [a, b, c, d].
> $$
> Both routes deliver $[a, b, c, d]$.

### Monoids and the writer monad

Barr and Wells give a second construction from a fixed monoid $M$, the
**representation triple**.[^bw-rep] Define

$$
T S = M \times S,
\qquad
\eta_S(s) = (1_M, s),
\qquad
\mu_S(m_1, m_2, s) = (m_1 m_2,\, s),
$$

where $1_M$ is the identity of $M$ and $m_1 m_2$ is its product. The unit and
multiplication of the monad are the unit and multiplication of $M$, threaded
past the carried element $s$. In programming this is the **writer monad**: a
value paired with an accumulated $M$-labelled output (a log, a running cost, a
count), and $\mu$ combines two accumulations with the monoid operation. The monad
laws reduce directly to the monoid laws of $M$.

This monad arises from the underlying-set / free adjunction on the category of
$M$-sets, exactly as the list monad arises from $\mathbf{Set}$ and $\mathbf{Mon}$.

## Monads as computational effects

A monad on $\mathbf{Set}$ packages a notion of "computation producing an $A$"
into the type $T A$. The unit injects a pure value as a trivial computation, and
the multiplication collapses a computation-of-a-computation into a single one.
The recurring examples of effectful programming are all monads.

| Monad | $T A$ | $\eta_A(a)$ | Effect modelled |
| --- | --- | --- | --- |
| List | finite lists over $A$ | $[a]$ | nondeterminism, multiple results |
| Maybe | $A + 1$ | $a$ (the left injection) | partiality, failure |
| Writer | $M \times A$ | $(1_M, a)$ | accumulated output over a monoid $M$ |
| Reader | $A^E = (E \to A)$ | constant function $\lambda e.\,a$ | a fixed environment $E$ |
| State | $S \to (A \times S)$ | $\lambda s.\,(a, s)$ | a threaded mutable state $S$ |

The **maybe** monad $T A = A + 1$ adds a distinguished "no result" point;
$\mu : (A + 1) + 1 \to A + 1$ merges the two failure points into one, so a
failure anywhere in a chain propagates. The **state** monad
$T A = (S \to A \times S)$ makes a computation a function that consumes an initial
state and returns a value together with a new state; $\mu$ threads the state
through two such steps in order. Each entry is a monad on $\mathbf{Set}$ arising
from an adjunction.

> **Worked example (Maybe monad).** Let $TA = A + 1$, writing $\mathsf{just}(a)$
> for the left injection of $a \in A$ and $\mathsf{no}$ for the extra point. The
> unit is $\eta_A(a) = \mathsf{just}(a)$; the multiplication
> $\mu_A : (A + 1) + 1 \to A + 1$ collapses failures,
> $$
> \mu_A(\mathsf{just}(\mathsf{just}(a))) = \mathsf{just}(a),
> \qquad
> \mu_A(\mathsf{just}(\mathsf{no})) = \mathsf{no},
> \qquad
> \mu_A(\mathsf{no}) = \mathsf{no}.
> $$
> The unit laws hold on each point. Through $\eta_{TA}$ then $\mu_A$:
> $\mathsf{just}(a) \mapsto \mathsf{just}(\mathsf{just}(a)) \mapsto \mathsf{just}(a)$
> and $\mathsf{no} \mapsto \mathsf{just}(\mathsf{no}) \mapsto \mathsf{no}$.
> Through $T\eta_A$ then $\mu_A$:
> $\mathsf{just}(a) \mapsto \mathsf{just}(\mathsf{just}(a)) \mapsto \mathsf{just}(a)$
> and $\mathsf{no} \mapsto \mathsf{no} \mapsto \mathsf{no}$. For associativity, any
> $\mathsf{no}$ at any depth in $((A + 1) + 1) + 1$ collapses to the single
> $\mathsf{no}$, and $\mathsf{just}^3(a)$ collapses to $\mathsf{just}(a)$, whichever
> pair of layers $\mu$ contracts first.

The [algebras](/category-theory/monads-algebras/algebras-eilenberg-moore) of
these monads recover the corresponding categories of structured sets, and the
multiplication becomes the sequencing operation of effectful programs in the
[Kleisli category](/category-theory/monads-algebras/kleisli-and-programming).

## Comonads

Dualizing the definition — reversing every arrow in the two coherence diagrams —
gives a **comonad**, which Barr and Wells call a **cotriple**.[^bw-cotriple]

> **Definition (Comonad).** A **comonad** $\mathbb{G} = (G, \varepsilon, \delta)$
> on $\mathcal{A}$ is a functor $G : \mathcal{A} \to \mathcal{A}$ with natural
> transformations $\varepsilon : G \to \mathrm{id}$ (the **counit**) and
> $\delta : G \to G^2$ (the **comultiplication**) satisfying the diagrams of the
> monad definition with all arrows reversed: $\delta$ is coassociative and
> $\varepsilon$ is a two-sided counit for it.

An adjunction $F \dashv G$ induces a comonad $(FG, \varepsilon, F\eta G)$ on
$\mathcal{B}$, the codomain side, at the same time as it induces the monad $GF$
on $\mathcal{A}$. Where a monad models building up a structure and collapsing
nested layers, a comonad models a context that can be observed ($\varepsilon$)
and duplicated ($\delta$): infinite streams, labelled trees, and stores of
values are comonads. The Kleisli construction of a comonad — the **co-Kleisli
category** — reappears in the theory of cartesian closed categories built from a
comonad, a route into models of linear logic.[^bw-coklei]

## Algebras, sequencing, and the bare endofunctor

Three constructions build on a monad:

- **Algebras.** Its
  [algebras](/category-theory/monads-algebras/algebras-eilenberg-moore) are objects
  carrying a $T$-structure map compatible with $\eta$ and $\mu$; they form the
  Eilenberg–Moore category.
- **Sequencing.** Its
  [Kleisli category](/category-theory/monads-algebras/kleisli-and-programming) has
  as arrows $A \to T B$ the effectful programs, composed by $\mu$.
- **The bare endofunctor.** Dropping $\eta$ and $\mu$ leaves an
  [endofunctor and its algebras](/category-theory/monads-algebras/algebras-for-endofunctors),
  whose initial object is the least fixed point that models inductive datatypes.

[^bw-triple]: **Barr & Wells**, _Category Theory for Computing Science_, §14.3.1 — Definition of a triple $(T, \eta, \mu)$ and its unitary and associative identities; §14.3 notes that "monad" is the widely used synonym.
[^bw-monoid]: **Barr & Wells**, §14.3.1 — the names "unit" and "multiplication" come from the analogy between triples and monoids, made precise in §14.3.4.
[^bw-adj]: **Barr & Wells**, §14.3.2–14.3.3 (Proposition) — an adjoint pair $F \dashv G$ gives the triple $(UF, \eta, U\varepsilon F)$ on the domain of the left adjoint.
[^bw-list]: **Barr & Wells**, §14.3.6 Exercise 3 — the Kleene-closure functor $A \mapsto A^\ast$ with singleton unit and concatenation multiplication is a triple.
[^bw-rep]: **Barr & Wells**, §14.3.4 — the representation triple $T(S) = M \times S$ of a monoid $M$, arising from the free / underlying adjunction on $M$-sets.
[^bw-cotriple]: **Barr & Wells**, §14.3.5 — a cotriple $(G, \varepsilon, \delta)$ is a triple in $\mathcal{A}^{\mathrm{op}}$.
[^bw-coklei]: **Barr & Wells**, §16.2.5 — the (co-)Kleisli category of a cotriple on a symmetric monoidal closed category, used to construct models of classical logic inside linear logic.
