Yoneda Lemma
Table of Contents
Yoneda Lemma
Note: This post is mostly a retelling of [Riehl 2017:2.3] and [Milewski 2015], with quite a few missing details filled in and a number of extra examples.
As discussed by [Milewski 2015],
many constructions in category theory generalize results from other, more specific, areas of mathematics. The Yoneda Lemma stands out in this respect as a sweeping statement about categories in general, with little or no precedent in other areas of mathematics, but with far-reaching implications.
For this reason, the Yoneda Lemma is called by some the "first" theorem in category theory.
Prerequisites
- co/contravariant [[hom-functors]] act on Hom-sets by post/precomposition
- [[representable-functors]] are naturally isormorphic to or
Introduction
Quoted from Milewski
We have seen previously that some -valued functors are [[representable]], being naturally isomorphic to a co/contravariant [[hom-functor]]. The Yoneda Lemma reveals that all -valued functors can be obtained from hom-functors through natural transformations, and explicitly enumerates all such transformations.
In fact, a natural transformation between a hom-functor and any functor is completely determined by specifying the value of a single component at just one point! The rest of the natural transformation follows from naturality conditions, which evidently are quite restrictive!
Moreover, the Yoneda Lemma helps to clarify the relationship between representable functors and [[universal-properties]] by answering the following questions (Riehl):
- If two objects represent the same functor, are they isomorphic?
- What data is involved in the construction of a natural isomorphism between a representable functor and the functor represented by an object ?
- How do the universal properties expressed by representable functors relate to initial and [[terminal-objects]] ?
The Yoneda Lemma is often stated in the language of [[presheaves]] . There is also a version Yoneda Lemma for [[enriched-categories]].
Informal Characterizations of Yoneda
We have previously seen that
Roughly, the Yoneda lemma says that an object in a category is no more and no less than its web of relationships with other objects, formalizing the idea that categories allow for [[point-free]] reasoning.
From [[milewski:ct4p-yoneda]] ,
One way of understanding the Yoneda lemma is to realize that natural transformations between Set-valued functors are just families of functions, and functions are in general lossy -- a function may collapse information and it may cover only parts of its codomain.
The only functions that are not lossy are the ones that are invertible — the isomorphisms. It follows then that the best structure-preserving Set-valued functors are the representable ones. They are either the hom-functors or the functors that are naturally isomorphic to hom-functors.
Any other functor
F
is obtained from a hom-functor through a lossy transformation. Such a transformation may not only lose information, but it may also cover only a small part of the image of the functorF
in Set.
Formal Statement
Let be a locally small category. According to [Riehl 2017], natural transformations whose domain is a represented functor are completely determined by the choice of a single element, which lives in the set defined by evaluating the codomain functor at the representing object , and moreover any choice is permitted.
(Yoneda Lemma) Consider a locally small category . For any object and any (covariant) functor , there is a bijection of sets
that associates each natural transformation with the element . Moreover, this correspondence is natural in both and .
We prove the bijection by manually constructing an inverse and showing that the two maps are mutually inverse. For naturality, see [Riehl 2017], Sec 2.2.
The inverse map must send each to a natural transformation , with component morphisms satisfying the naturality squares depicted below, for all in .
To define for a particular , consider the image of the morphism wrt each path in the naturality square above. Our choice is constrained by the following naturality condition:
Our inverse map must obey for all . Applying our explicit formula for results in following constraint on ,
These two constraints uniquely determine the inverse map,
This choice of guarantees that the naturality squares for any commute, but we must verify naturality for generic with . So, consider the image of any along both paths.
Hence is a valid natural transformation. To show and are mutually inverse,
- By construction .
- To show for all natural transformations , we check componentwise equality. For all , The last step follows from the naturality square for and .
CoYoneda Lemma
There is an analogous coYoneda Lemma for contravariant functors.
(coYoneda Lemma) Consider a locally small category . For any object and any contravariant functor , there is a bijection of sets
Apply the Yoneda Lemma to . Formulas for the forward and reverse bijections are the same as before.
Yoneda Embedding
Let be a locally small category. An important special case of the Yoneda lemma is when is another [[hom-functor]], leading us to the Yoneda Embeddings. The following definition is a special case of the bijection used in the Yoneda proof.
The (covariant) Yoneda Embedding is the covariant functor
sending each object to its contravariant Hom-functor, and each morphism to the natural transformation defined by the -postcomposition.
See [[hom-functor]] for details about the natural transformation . Before proceeding, we should verify that the map is indeed a covariant functor.
The morphisms and in are sent by the Yoneda embedding to the natural transformations
We must show , where the composition has image
whose components act on morphisms as
where the middle step relies on associativity of function composition.
The most useful fact about the Yoneda functor is that it is [[fully-faithful]], meaning that we always have a bijection between hom-sets in the domain and image of .
For a locally small category , the Yoneda Embedding is a [[fully-faithful]] functor, embedding as a [[full-subcategory]] of .
We must show that the maps are surjective (full) and injective (faithful) for all . Choosing in the coYoneda Lemma gives a bijection between these same sets.
We claim . The inverse bijection maps each morphism to a natural transformation whose component morphisms act on morphisms in by the formula
Thus coincides with the Yoneda embedding on . Since is a bijection, the maps are injective and surjective for all .
By now, it should be clear that we did not pull the Yoneda embedding out of thin air -- instead, is the result of applying the Yoneda Lemma to the functors and for all pairs , and assembling the resulting bijections into a functor.
In a locally small category, .
Every [[fully-faithful]] functor reflects isomorphisms.
Contravariant Yoneda Embedding
Naturally, there is a contravariant Yoneda embedding found by applying the above to
[[todo]] : according to a comment on Milewski's blog, the coYoneda embedding functor is also known as the [[free-functor]].
The contravariant Yoneda Embedding is the contravariant functor
sending each object to its covariant [[hom-functor]] and each morphism to the natural transformation defined by -precomposition. The functor is [[fully-faithful]] and embeds as a [[full-subcategory]] of . Consequently,
Yoneda and Universal Elements
See [[universal-element]] for the connection between Yoneda and [[universal-properties]].
Yoneda Embedding / Evaluation
this section is incomplete ! [[TODO]]
The pair in the statement of the Yoneda Lemma defines an object in the product category , where denotes the category of functors and natural transformations between them.
We can think of the bijection maps used in the Yoneda Lemma as components of a natural isomorphism between two functors we will now introduce.
- The codomain of is the covariant bifunctor defined by the evaluation map .
- The domain is the covariant bifunctor
For details of how the domain functor is constructed, see [Riehl 2017]. The Yoneda Lemma guarantees that this bifunctor is valued in the category of sets. Now, taking the Yonda Lemma together for each pair asserts that evaluating the representing object's identity morphism defines a natural isomorphism
Hence every locally small category is isomorphic to the full subcategory of spanned by the contravariant represented functors and likewise, is isomorphic to the full subcategory of spanned by the covariant represented functors, via the canonically-defined covariant and contravariant Yoneda embeddings.
(Yoneda Embedding) The functors below are full and faithful embeddings.
That is, natural transformations between represented functors correspond to morphisms between representing objects!
Applications of Yoneda
everything you need to know about the Yoneda Lemma is encoded by its relationship with other topics! :)
Mathematics
Let be a [[preorder]] category, where each hom-set contains at most one morphism, and where each morphism can be thought of as [[evidence]] that the statement is true. The Yoneda embedding reads
Using [[coq]] syntax, the left-hand side is the set of polymorphic functions which convert evidence that into evidence that , that is,
: forall z, (x <= z) -> (y <= z) η
Yoneda confirms that this proposition is logically equivalent to (y <= x)
. That is,
([Riehl 2017], Cor. 2.2.9) Every row operation on matrices with rows is defined by left multiplication by some matrix, namely the matrix obtained by performing the row operation on the identity matrix.
(Cayley's Theorem / [Riehl 2017], Cor. 2.2.10) Every group is isomorphic to a subgroup of a permutation group.
Yoneda in Haskell
In the category [[hask]], of types and Haskell functions between them,
- a natural transformation is nothing more than a [[polymorphic]] function, and
- the (covariant) [[hom-functor]] is expressed by the signature
c ->
.
Presuming Hask ⊆ Set
, the Yoneda Lemma for a functor F : Hask -> Hask
reads:
In the case of the [[list-functor]], written [ ]
, we see that a polymorphic function η :: forall x. (c -> x) -> [x]
is the same as a list [c]
. This makes sense -- if we can produce a list of x
's when all we have to work with is a function mapping c -> x
, we must have a list of c
's stashed away somewhere! Applying η
to the identity map id :: c -> c
reveals the original list.
Note: We can also use the [[reader-monad]], whose functor instance
Reader c
is (covariantly) represented byc
. The reader monad gives another way to make use of hidden data inside a function!
The signature for η
reads like a partially-applied fmap
, and this is no coincidence! Given data p :: [c]
, the Yoneda bijection gives the following implementation for η
,
:: forall x. (c -> x) -> [x]
η= (fmap f) p η f
The Yoneda Lemma tells us that every implementation of this function signature must have the form above, for some specific value of p
.
In the category [[hask]], the coYoneda Lemma on the list functor [ ]
reads
So a polymorphic function η :: forall x. (x -> c) -> [x]
is the same as a list [c]
, but how? In Hask
, naturality means that η
is [[parametrically-polymorphic]], in the sense that the formula for η
cannot use any specific information about the type x
.
The Yoneda bijection tells us how to extract this hidden data from a particular η
,
id :: c -> c
id x = x
id) :: [c] η(
In the other direction, each [c]
gives rise to an η :: forall x. (x -> c) -> [x]
,
:: forall x. (x -> c) -> [x]
η= ... TODO η
[[todo]] : explain this example better with the writer monad
List is not a contravariant functor!!!
In the category [[hask]], the Yoneda Embedding reads
meaning that a polymorphic function η :: forall x. (a→x) → (b→x)
is equivalent to a function b→a
. That is, if we can convert any (a→x)
to a (b→x)
, we must have a function (b→a)
stashed away somewhere that we can use before applying the (a→x)
. Surely enough, applying η
to the id :: a → a
yields the hidden (b→a)
!
Likewise, every polymorphic function η :: forall x. (a→x) → (b→x)
has the form
p :: b -> a
= ...
p
:: forall x. (a -> x) -> (b -> x)
η= p ; f η f
Continuation-Passing Style
Following [Milewski 2015], Yoneda applied to the identity functor on Hask
says that a polymorphic function η :: forall x. (c -> x) -> x
is the same as c
. In other words, any type c
can be replaced by a function that takes a handler for c
.
A handler is a function that accepts a
and afterwards performs the rest of some computation -- akin to the [[continuation-passing-style]] used by compilers. In fact,
newtype Cont r a = Cont { runCont :: (a -> r) -> r }
Category of Elements
Enriched Yoneda Lemma
The plain Yoneda Lemma deals only with the category of sets. What if we have not Hom-Sets, but Hom-Vector-Spaces or Hom-Lattices? We will use [hinich2016:enriched-yoneda?] as a guide to present the **Enriched Yoneda Lemma*.
References
[Riehl 2017] for the proof structure
[Milewski 2015] for intuition
[hinich2016:enriched-yoneda?] for the enriched Yoneda Lemma