Lecture 12: That’s Yoneda, Babe

Ever since we learned about natural transformations, I’ve been saying that I’m going to talk about the Yoneda lemma. And finally now we are going to do it.

What is the Yoneda lemma? I think of the Yoneda lemma as a “vibe check” for category theory. We say all the time that in category theory “all that matters” is the morphisms out of or into an object; the Yoneda lemma formalizes this.

The Yoneda lemma also underscores the usefulness of representable functors.

We start to understand the Yoneda lemma via the following seemingly trivial statement:

For any set A, there is an isomorphism between elements a \in A and functions f \colon 1 \to A. We can write this as

A \cong \operatorname{Hom}_{\mathsf{Set}}(1, A)

If we think about \mathsf{Set} in Julia, a function 1 \to A is a single-element vector, with that single element being an element of A. The isomorphism is then

f(a::A) = [a]
f_inverse(v::Vector{A}) = v[1]

This is even more obvious if you write it as the algebra 1 equality A^{1} \cong A.

The Yoneda lemma extends this triviality beyond sets to \mathsf{C}-sets.

We can start to get an intuition for the Yoneda lemma by looking at graphs. Given a graph G \colon \mathsf{Gr}\to \mathsf{Set}, we can get the set of vertices of that graph by applying G to V \in \mathsf{Gr} to get G(V) \in \mathsf{Set}. However, we can also get the set of vertices by looking at all of the morphisms into G from the single-vertex graph y_{V} (recall that the single-vertex graph is the representable on V).

Picking out a single vertex with a morphism

It is not hard to see that there is a bijection between:

for the same reason that for any set there is a bijection between functions 1 \to A and elements of A. Namely, given a morphism \alpha \colon y_{V} \to G, we get an element y_{V}(\operatorname{id}_V) \in G(V). And given an element v \in G(V), there is a morphism v^\ast \colon y_V \to G given by \operatorname{id}_V \mapsto v.

We write this consisely as

G(V) \cong \operatorname{Hom}_{\mathsf{Set}^\mathsf{Gr}}(y_{V}, G)

We can do the exact same thing for edges! The edges of G are in bijection with morphisms out of y_{E}, where y_{E} is the graph with a single edge and two vertices.

Picking out a single edge with a morphism

Given an element e \in G(E), there is a unique morphism e^{\ast} \colon y_{E} \to G given by

\operatorname{id}_E \mapsto e \operatorname{src}\mapsto G(\operatorname{src})(e) \operatorname{tgt}\mapsto G(\operatorname{tgt})(e)

Moreover, given any morphism \alpha \colon y_{E} \to G, there is an element of \alpha_{E}(\operatorname{id}_{E}) \in G(E).

We write this as

G(E) \cong \operatorname{Hom}_{\mathsf{Set}^{\mathsf{Gr}}}(y_{E}, G)

Note that this crucially depends on the naturality of the morphism f \colon y_{E} \to G. If we had an unnatural morphism, then we wouldn’t necessarily have commutation of the square

so the vertices in G that the endpoints of \operatorname{id}_{E} are sent to would not be determined by where \operatorname{id}_{E} was sent.

Now that we’ve seen two instances of this pattern, you can probably guess what it looks like in general.

(Yoneda). Given a category \mathsf{C} and a functor F \colon \mathsf{C}\to \mathsf{Set}, for every object X \in \mathsf{C} there is an isomorphism

F(X) \cong \operatorname{Hom}_{\mathsf{Set}^{\mathsf{C}}}(y_{X}, F)

Proof. We will give a map in each direction, then show they are inverse.

Starting from x \in F(X), we construct x^{\ast} \colon y_{X} \to F by the mapping

f \in y_{X}(Y) = \operatorname{Hom}_{\mathsf{C}}(X,Y) \mapsto F(f)(x)

In the other direction, starting from \alpha \colon y_{X} \to F, we get \alpha_{X}(\operatorname{id}_{X}) \in F(X).

Now, given x \in F(X),

x^{\ast}_{X}(\operatorname{id}_X) = F(\operatorname{id}_X)(x) = x

There is one more thing to show. Starting with \alpha \colon y_{X} \to F, consider the naturality square for f \in y_{X}(Y).

Let’s follow the path of \operatorname{id}_{X} \in y_{X}(X). Following the lower path, we get

(\alpha_{Y} \circ y_{X}(f))(\operatorname{id}_{X}) = \alpha_{Y}(y_{X}(f)(\operatorname{id}_{X})) = \alpha_{Y}(\operatorname{id}_{X} \circ f) = \alpha_{Y}(f)

Following the upper path, we get

(F(f) \circ \alpha_X)(\operatorname{id}_X) = F(f)(\alpha_X(\operatorname{id}_X))

Therefore,

\alpha_Y(f) = F(f)(\alpha_X(\operatorname{id}_X))

This says precisely that where f goes is wholly determined by where \operatorname{id}_{X} goes, just like where \operatorname{src} and \operatorname{tgt} went was wholly determined by where \operatorname{id}_{E} went! And more specifically,

(\alpha_{X}(\operatorname{id}_{X}))^{\ast} = \alpha

Thus, we have our desired bijection

F(X) \cong \operatorname{Hom}_{\mathsf{Set}^{\mathsf{C}}}(y_{X}, F)

The feeling that this seems like total abstract nonsense is a valid feeling at this point. It’s very much a tautology in the same way that A \cong A^{1} is, except it’s confusing because there’s more stuff going on (incidentally, can you now see how A \cong A^{1} is a corollary of the Yoneda lemma?)

But this actually turns out to be an extremely useful device. One application is something that I’ve been using a lot; the fact that y_{X} \cong y_{Y} if and only if X \cong Y. In fact, we can show something stronger.

Given X,Y \in \mathsf{C}, there is a bijection \operatorname{Hom}_{\mathsf{C}}(Y,X) \cong \operatorname{Hom}_{\mathsf{Set}^{\mathsf{C}}}(y_{X}, y_{Y}).

With the Yoneda lemma, this is a very fast proof!

Proof. \operatorname{Hom}_{\mathsf{C}}(X,Y) \cong y_{X}(Y) \cong \operatorname{Hom}_{\mathsf{Set}^{\mathsf{C}}}(y_{Y}, y_{X})

As an application of this, \operatorname{Hom}_{\mathsf{Gr}}(E,V) \cong \operatorname{Hom}_{\mathsf{Set}^{\mathsf{Gr}}}(y_{V}, y_{E}). You can see this because there are two morphisms E \to V, and then two morphisms from y_{V} to y_{E}.

The point is that any isomorphism in \operatorname{Hom}_{\mathsf{Set}^{\mathsf{C}}}(y_{Y}, y_{X}) must also show up as an isomorphism in \operatorname{Hom}_{\mathsf{C}}(X,Y).

To state this more formally, we can talk about y as a functor from \mathsf{C} to \mathsf{Set}^{\mathsf{C}}. However, because of the flipping around of \operatorname{Hom}_{\mathsf{C}}(Y,X) \cong \operatorname{Hom}_{\mathsf{Set}^{\mathsf{C}}}(y_{X}, y_{Y}), this actually should be a contravariant functor, and we haven’t gotten to discussing duality yet. So we will leave off here for now!