---
title: The Yoneda Lemma
module: Representables and the Yoneda Lemma
moduleNumber: 3
lessonNumber: 2
order: 302
summary: >
  The Yoneda lemma computes the natural transformations out of a representable
  presheaf: they form a set in natural bijection with X(A). The proof fixes a
  single degree of freedom, the image of the identity arrow, and shows naturality
  forces everything else. We prove the bijection, verify naturality in both
  variables, and read off that a natural transformation out of a representable is
  just one element.
topics: [Representables and the Yoneda Lemma]
sources:
  - book: Leinster
    ref: "Ch. 4 — Representables; §4.2 The Yoneda lemma"
  - book: Barr & Wells
    ref: "Ch. 4 §4.5 The Yoneda Lemma and universal elements; Theorem 4.5.8"
  - book: Simmons
    ref: "Ch. 3–4 — natural transformations and hom-functors (supporting)"
draft: false
---

Fix a locally small category $\mathcal{A}$, an object $A$, and a presheaf
$X : \mathcal{A}^{\mathrm{op}} \to \mathbf{Set}$. The
[representable presheaf](/category-theory/representables-yoneda/representable-functors)
$H_A = \mathcal{A}(-, A)$ and the arbitrary presheaf $X$ are both objects of the
functor category $[\mathcal{A}^{\mathrm{op}}, \mathbf{Set}]$, so it makes sense to
ask what the maps between them are. The maps are natural transformations, and
they form the set $[\mathcal{A}^{\mathrm{op}}, \mathbf{Set}](H_A, X)$. The Yoneda
lemma identifies this set exactly.[^lein-42]

The answer is guessable from an informal principle: from a piece of input data
there is usually only one output of a given type that can be built. The input
here is a pair $(A, X)$. One set built from it is the collection of natural
transformations $H_A \to X$. Another is $X(A)$ itself, obtained by evaluating
$X$ at $A$. The principle suggests these are the same set, and they are.

> **Theorem (Yoneda).** Let $\mathcal{A}$ be a locally small category. For every
> $A \in \mathcal{A}$ and every presheaf $X : \mathcal{A}^{\mathrm{op}} \to
> \mathbf{Set}$,
> $$
> [\mathcal{A}^{\mathrm{op}}, \mathbf{Set}](H_A, X) \;\cong\; X(A)
> $$
> and the bijection is natural in $A$ and in $X$.

Informally: **a natural transformation $H_A \to X$ is an element of $X(A)$.** The
lemma holds equally in covariant form, with $H^A = \mathcal{A}(A, -)$ and a
covariant $X : \mathcal{A} \to \mathbf{Set}$; the statement above uses presheaves
to match the Yoneda embedding.

$$
% caption: The Yoneda bijection: every natural transformation $H_A \to X$
% corresponds to a single element of $X(A)$, and back, with no other data.
\begin{tikzpicture}[>=stealth, font=\footnotesize]
  \definecolor{acc}{HTML}{4A6FA5}
  \node[draw, thick, minimum width=34mm, minimum height=13mm, align=center] (nat) at (0,0) {natural\\transformations $H_A$ to $X$};
  \node[draw, acc, thick, minimum width=20mm, minimum height=13mm] (xa) at (6.0,0) {$X(A)$};
  \draw[->, thick] (nat.10) -- (xa.170) node[midway, above, font=\scriptsize] {evaluate at $1_A$};
  \draw[<-, acc, thick] (nat.-10) -- (xa.-170) node[midway, below, font=\scriptsize] {extend an element};
\end{tikzpicture}
$$

## The explicit bijection

The bijection has an explicit form. Given a natural transformation
$\alpha : H_A \to X$, its component at $A$ is a function
$\alpha_A : H_A(A) = \mathcal{A}(A, A) \to X(A)$. Feed it the one canonical
element of $\mathcal{A}(A, A)$, the identity $1_A$, and record the result.

> **Definition (Yoneda maps).** For fixed $A$ and $X$ define
> $$
> \widehat{(-)} : [\mathcal{A}^{\mathrm{op}}, \mathbf{Set}](H_A, X) \to X(A),
> \qquad \widehat{\alpha} = \alpha_A(1_A),
> $$
> and in the other direction, for $x \in X(A)$ define
> $\widetilde{x} : H_A \to X$ by its components
> $$
> \widetilde{x}_B : \mathcal{A}(B, A) \to X(B), \qquad
> \widetilde{x}_B(f) = \big(X(f)\big)(x).
> $$

The formula for $\widetilde{x}_B$ is forced: $x$ lives in $X(A)$, a map
$f : B \to A$ gives $X(f) : X(A) \to X(B)$, and applying it to $x$ is the only
element of $X(B)$ that $f$ and $x$ produce. The Yoneda lemma asserts that
$\widehat{(-)}$ and $\widetilde{(-)}$ are mutually inverse bijections, natural in
$A$ and $X$. The proof does four things: define each map (done above), then check
each composite is an identity.

## The proof

**The map $\widetilde{x}$ is natural.** For a map $g : B_0 \to B$ in $\mathcal{A}$,
naturality of $\widetilde{x}$ is the square below, whose top edge is
$H_A(g) = - \circ g$ and whose bottom edge is $X(g)$.

$$
% caption: Naturality of $\widetilde{x}$: for $g : B_0 \to B$, both routes send
% $f$ to $\big(X(f g)\big)(x)$, using $X(f g) = X(g) X(f)$ by functoriality.
\begin{tikzpicture}[>=stealth, font=\footnotesize]
  \definecolor{acc}{HTML}{4A6FA5}
  \node (BA)  at (0,1.7)   {$H_A(B)$};
  \node (BpA) at (3.6,1.7) {$H_A(B_0)$};
  \node (XB)  at (0,0)     {$X(B)$};
  \node (XBp) at (3.6,0)   {$X(B_0)$};
  \draw[->, thick] (BA) -- (BpA) node[midway, above] {$H_A(g)$};
  \draw[->, acc, thick] (BA) -- (XB) node[midway, left] {$x_B$};
  \draw[->, acc, thick] (BpA) -- (XBp) node[midway, right] {$x_{B_0}$};
  \draw[->, thick] (XB) -- (XBp) node[midway, below] {$X(g)$};
\end{tikzpicture}
$$

Chasing $f \in \mathcal{A}(B, A)$: the top-then-right route gives
$\widetilde{x}_{B_0}(f \circ g) = \big(X(f \circ g)\big)(x)$, and the left-then-bottom
route gives $\big(X(g)\big)\big(\widetilde{x}_B(f)\big) = \big(X(g) \circ X(f)\big)(x)$.
Functoriality $X(f \circ g) = X(g) \circ X(f)$ makes the two equal, so the square
commutes and $\widetilde{x}$ is a genuine natural transformation.

**The composite $\widehat{\widetilde{x}} = x$.** Directly,
$$
\widehat{\widetilde{x}} = \widetilde{x}_A(1_A) = \big(X(1_A)\big)(x)
= 1_{X(A)}(x) = x,
$$
using $X(1_A) = 1_{X(A)}$.

**The composite $\widetilde{\widehat{\alpha}} = \alpha$.** Two natural
transformations agree iff all components agree, and two functions agree iff they
agree on every element, so it suffices to fix $B$ and $f : B \to A$ and show
$\widetilde{\widehat{\alpha}}_B(f) = \alpha_B(f)$. The left side unfolds to
$$
\widetilde{\widehat{\alpha}}_B(f) = \big(X(f)\big)(\widehat{\alpha})
= \big(X(f)\big)\big(\alpha_A(1_A)\big),
$$
so the claim is $\big(X(f)\big)\big(\alpha_A(1_A)\big) = \alpha_B(f)$. This is
exactly what naturality of $\alpha$ supplies. Naturality of $\alpha$ at the map
$f : B \to A$ is the square

$$
% caption: The identity-arrow chase. Naturality of $\alpha$ at $f$ sends $1_A$
% down-then-right to $\big(X(f)\big)(\alpha_A(1_A))$ and right-then-down to
% $\alpha_B(f)$; the square commutes, so they are equal.
\begin{tikzpicture}[>=stealth, font=\footnotesize]
  \definecolor{acc}{HTML}{4A6FA5}
  \node (AA) at (0,1.7)   {$H_A(A)$};
  \node (BA) at (3.6,1.7) {$H_A(B)$};
  \node (XA) at (0,0)     {$X(A)$};
  \node (XB) at (3.6,0)   {$X(B)$};
  \draw[->, thick] (AA) -- (BA) node[midway, above] {$H_A(f)$};
  \draw[->, acc, thick] (AA) -- (XA) node[midway, left] {$a_A$};
  \draw[->, acc, thick] (BA) -- (XB) node[midway, right] {$a_B$};
  \draw[->, thick] (XA) -- (XB) node[midway, below] {$X(f)$};
  \node[acc, font=\scriptsize] at (0.7,1.98) {$1_A$};
\end{tikzpicture}
$$

evaluated at $1_A \in H_A(A)$. The top edge $H_A(f) = - \circ f$ sends $1_A$ to
$1_A \circ f = f$, so the right-then-down route is $\alpha_B(f)$; the left-then-down
route is $\big(X(f)\big)(\alpha_A(1_A))$; commutativity equates them. (In the
figure the components $\alpha_A, \alpha_B$ are labelled $a_A, a_B$.) This
establishes the bijection for each fixed $A$ and $X$.

The chase records the structural content of the lemma:

> **Corollary (Determination by the identity).** A natural transformation
> $\alpha : H_A \to X$ is determined by the single element
> $\widehat{\alpha} = \alpha_A(1_A) \in X(A)$: every other value is forced by
> $\alpha_B(f) = \big(X(f)\big)(\widehat{\alpha})$.

$$
% caption: A natural transformation out of $H_A$ has a component at every object,
% yet all of them are pinned by one element $u = \alpha_A(1_A)$ of $X(A)$.
\begin{tikzpicture}[>=stealth, font=\footnotesize]
  \definecolor{acc}{HTML}{4A6FA5}
  \node (c1) at (0,1.5)  {component at $B_1$};
  \node (c2) at (0,0.5)  {component at $B_2$};
  \node (c3) at (0,-0.5) {component at $B_3$};
  \node (c4) at (0,-1.5) {$\vdots$};
  \node[draw, acc, thick, minimum width=16mm, minimum height=11mm] (u) at (5.4,0) {$u$};
  \node[acc, font=\scriptsize, anchor=south] at (5.4,0.6) {in $X(A)$};
  \draw[->, black] (c1.east) -- (u.150);
  \draw[->, black] (c2.east) -- (u.170);
  \draw[->, black] (c3.east) -- (u.190);
  \draw[->, black] (c4.east) -- (u.210);
\end{tikzpicture}
$$

## Naturality in both variables

The bijection is not merely one bijection per pair $(A, X)$; the whole family is
natural. By a standard lemma it is enough to check naturality of one of the two
maps, and by another it is enough to check naturality separately in each variable.
Take $\widehat{(-)}$.

- **Naturality in $A$.** A map $f : B \to A$ induces $- \circ H_f$ on
  transformations and $X(f)$ on values, and for $\alpha : H_A \to X$,
  $$
  \big(\alpha \circ H_f\big)_B(1_B) = \alpha_B\big((H_f)_B(1_B)\big)
  = \alpha_B(f \circ 1_B) = \alpha_B(f) = \big(X(f)\big)(\alpha_A(1_A)),
  $$
  the last step by the identity-arrow computation above. So the naturality
  square in $A$ commutes.
- **Naturality in $X$.** A map $\theta : X \to X'$ of presheaves induces
  $\theta \circ -$ on transformations and $\theta_A$ on values, and
  $$
  \widehat{\theta \circ \alpha} = (\theta \circ \alpha)_A(1_A)
  = \theta_A\big(\alpha_A(1_A)\big) = \theta_A(\widehat{\alpha}),
  $$
  using componentwise composition in $[\mathcal{A}^{\mathrm{op}}, \mathbf{Set}]$.
  So the naturality square in $X$ commutes.

Both squares commute, so the bijection is natural in $(A, X)$, completing the
proof. Naturality distinguishes "there exists a
bijection" from "the bijection is canonical." Without it, one could iterate the
construction $X \mapsto X'$ with $X'(A) = [\mathcal{A}^{\mathrm{op}},
\mathbf{Set}](H_A, X)$ and manufacture an infinite sequence of presheaves
$X, X', X'', \dots$. The Yoneda lemma says $X' \cong X$ naturally, so the sequence
never leaves $X$.

## The covariant form and duality

Replacing $\mathcal{A}$ by $\mathcal{A}^{\mathrm{op}}$ dualizes the lemma. A
presheaf on $\mathcal{A}^{\mathrm{op}}$ is a covariant functor
$X : \mathcal{A} \to \mathbf{Set}$, and the representable presheaf at $A$
becomes the covariant hom-functor $H^A = \mathcal{A}(A, -)$, so the dual
statement reads
$$
[\mathcal{A}, \mathbf{Set}](H^A, X) \;\cong\; X(A)
$$
naturally in $A$ and $X$. The explicit maps are the same up to reversal: a
natural transformation $\beta : H^A \to X$ corresponds to
$\beta_A(1_A) \in X(A)$, and an element $c \in X(A)$ induces the transformation
with components $f \mapsto \big(X(f)\big)(c)$ for $f : A \to B$.[^bw-formula]
Nothing new needs proving; both forms are used freely, the contravariant one
because the Yoneda embedding lands in presheaves, the covariant one because
forgetful and hom-set functors are covariant.

| Form | Representable | Statement | Element inducing $\alpha$ |
| --- | --- | --- | --- |
| Contravariant | $H_A = \mathcal{A}(-, A)$ | $[\mathcal{A}^{\mathrm{op}}, \mathbf{Set}](H_A, X) \cong X(A)$ | $\alpha_A(1_A)$ |
| Covariant | $H^A = \mathcal{A}(A, -)$ | $[\mathcal{A}, \mathbf{Set}](H^A, X) \cong X(A)$ | $\alpha_A(1_A)$ |

### Directed graphs

For a computation where every set can be written down, take directed graphs. A
graph is a pair of sets with source and target functions, which is exactly a
set-valued functor on a two-object category: let $\mathcal{G}$ have objects $N$
and $E$ and two non-identity arrows $s, t : N \to E$. A presheaf
$X : \mathcal{G}^{\mathrm{op}} \to \mathbf{Set}$ assigns a set $X(E)$ of edges,
a set $X(N)$ of nodes, and two functions $X(s), X(t) : X(E) \to X(N)$ giving
each edge its source and target.[^bw-graph]

The representable presheaf $H_N = \mathcal{G}(-, N)$ has
$$
H_N(N) = \mathcal{G}(N, N) = \{1_N\}, \qquad
H_N(E) = \mathcal{G}(E, N) = \varnothing,
$$
so $H_N$ is the graph with one node and no edges. The Yoneda lemma then says,
for any graph $X$,
$$
[\mathcal{G}^{\mathrm{op}}, \mathbf{Set}](H_N, X) \;\cong\; X(N):
$$
a map from the one-node graph into $X$ is the same as a node of $X$. This is
visibly true: a graph map out of a single node picks a node and nothing else,
and the identity-arrow chase is one line, since the only element of $H_N(N)$ to
chase is $1_N$.

The representable $H_E$ works the same way: $H_E(E) = \{1_E\}$ and
$H_E(N) = \mathcal{G}(N, E) = \{s, t\}$, with $H_E(s)$ and $H_E(t)$ sending
$1_E$ to $s$ and $t$ respectively. So $H_E$ is the graph with two nodes and a
single edge from one to the other, and Yoneda gives
$[\mathcal{G}^{\mathrm{op}}, \mathbf{Set}](H_E, X) \cong X(E)$: maps from the
single-edge graph into $X$ are the edges of $X$. Again this is visible
directly, since placing the generic edge into $X$ means choosing an edge
together with its (determined) endpoints. In both cases the lemma converts a
statement about all components of a natural transformation at once into a
statement about a single generator.

> **Worked example (Enumerating $H_N \to X$ for a concrete graph).** Let $X$ be
> the graph with two nodes $v, w$ and one edge $e$ from $v$ to $w$, so
> $X(N) = \{v, w\}$, $X(E) = \{e\}$, and $X(s)(e) = v$, $X(t)(e) = w$. Yoneda
> gives $[\mathcal{G}^{\mathrm{op}}, \mathbf{Set}](H_N, X) \cong X(N)$, so there
> are exactly two natural transformations $H_N \to X$. Each is determined by
> the image of $1_N \in H_N(N)$: the transformation $\widetilde{v}$ sends the
> single node of $H_N$ to $v$, and $\widetilde{w}$ sends it to $w$. Since
> $H_N(E) = \varnothing$, there is nothing to specify on edges, and both
> transformations are forced everywhere else. Enumerating maps out of the
> one-node graph is enumerating the nodes of $X$.

## The one-object case

The lemma's content is visible already for a monoid $M$ viewed as a one-object
category, where a presheaf $M^{\mathrm{op}} \to \mathbf{Set}$ is a right
$M$-set.[^lein-monoid] The unique representable presheaf is the **right regular
representation**: the underlying set of $M$ acted on by right multiplication,
$x \cdot m = xm$. Write it $\underline{M}$. For any right $M$-set $X$ and any
$x \in X$, there is a unique map of $M$-sets $\alpha : \underline{M} \to X$ with
$\alpha(1) = x$, namely $\alpha(m) = x \cdot m$. So maps
$\underline{M} \to X$ correspond bijectively to elements of $X$:
$$
\{\text{maps } \underline{M} \to X \text{ of right } M\text{-sets}\} \;\cong\; X.
$$
This is the Yoneda lemma for a one-object category, with the abstract $X(A)$ made
concrete as the underlying set of the $M$-set $X$. A map out of the regular
representation is a choice of where to send the generator $1$, and equivariance
forces the rest.

> **Worked example (Maps out of a regular representation).** Take
> $M = \mathbb{Z}/2 = \{0, 1\}$ under addition and its regular representation
> $\underline{M} = \{0, 1\}$ with right action $x \cdot m = x + m$. Let $X$ be
> the two-element $M$-set $\{+, -\}$ on which $0$ acts as the identity and $1$
> swaps the two points. A map of $M$-sets $\alpha : \underline{M} \to X$ is
> fixed by $\alpha(0)$, and then $\alpha(1) = \alpha(0 \cdot 1) = \alpha(0)
> \cdot 1$ is forced. The two choices give
> $$
> \alpha(0) = {+} \;\Rightarrow\; \alpha(1) = {-}, \qquad
> \alpha(0) = {-} \;\Rightarrow\; \alpha(1) = {+},
> $$
> so there are exactly two equivariant maps $\underline{M} \to X$, matching the
> two elements of $X$. This is $\{\text{maps } \underline{M} \to X\} \cong X$
> written out.

Because a map $H_A \to X$ is
an element of $X(A)$, taking $X$ to be another representable $H_{A'}$ shows that
maps of representables are exactly maps of objects, so the Yoneda embedding
$H_\bullet$ is
[full and faithful](/category-theory/representables-yoneda/yoneda-consequences),
and an object is determined by the maps into it.

[^lein-42]: **Leinster**, _Basic Category Theory_, §4.2 — The Yoneda lemma: statement, the informal "one output per input" heuristic, and the four-part proof by defining $\widehat{(-)}$ and $\widetilde{(-)}$ and chasing $1_A$.
[^lein-monoid]: **Leinster**, _Basic Category Theory_, §4.2, Exercise 4.2.3 — the Yoneda lemma for one-object categories: the unique representable is the right regular representation, and maps out of it correspond to elements. Barr & Wells, Theorem 4.5.8, states the same correspondence via the formula $f \mapsto \big(F(f)\big)(c)$.
[^bw-formula]: **Barr & Wells**, _Category Theory for Computing Science_, §4.5.6–4.5.8 — the covariant statement: an element $c \in F(C)$ induces the natural transformation $\operatorname{Hom}(C, -) \to F$ by $f \mapsto \big(F(f)\big)(c)$, and this assignment is a bijection.
[^bw-graph]: **Barr & Wells**, _Category Theory for Computing Science_, §4.3.10 and §4.5.1 — the set-of-nodes functor on graphs is represented by the one-node graph, and the set-of-arrows functor by the graph with two nodes and one arrow.
