A PDF of this post is available on github.
Motivation
A lens is a reification of the concept of object composition. In a nutshell, it describes the process of decomposing the source object into a focus
and a residue
and recomposing a new object
from a new focus
and the same residue
.
The key observation is that we don’t care what the residue is as long as it exists. This is why a lens can be implemented, in Haskell, as an existential type:
data Lens s t a b where Lens :: (s -> (c, a)) -> ((c, b) -> t) -> Lens s t a b
In category theory, the existential type is represented as a coend—essentially a gigantic sum over all objects in the category
:
There is a simple recipe to turn this representation into the more familiar one. The first step is to use the currying adjunction on the second hom-functor:
Here, is the internal hom, or the function object
(b->t)
.
Once the object appears as the source in the hom-set, we can use the co-Yoneda lemma to eliminate the coend. This is the formula we use:
It works for any functor from the category
to
so, in particular we have:
The result is a pair of arrows:
the first corrsponding to:
set :: s -> b -> t
and the second to:
get :: s -> a
It turns out that this trick works for more general optics. It all depends on being able to isolate the object as the source of the second hom-set.
We’ve been able to do it case-by-case for lenses, prisms, traversals, and the whole zoo of optics. It turns out that the same problem was studied in all its generality by Australian category theorists Janelidze and Kelly in a context that had nothing to do with optics.
Monoidal Actions
Here’s the most general definition of an optic:
This definition involves three separate categories. The category is monoidal, and it has an action defiend on both
and
. A category with a monoidal action is called an actegory.
We get the definition of a lens by having all three categories be the same—a cartesian closed category . We define the action of this category on itself:
(and the same for ).
There are two equivalent ways of defining the action of a monoidal category. One is as the mapping
written in infix notation as . It has to be equipped with two additional structure maps—isomorphisms that relate the tensor product
in
and its unit
to the action in
:
plus some coherence conditions corresponding to associativity and unit laws.
Using this notation, we can rewrite the definition of an optic as:
with the understanding that we use the same symbol for two different actions in and
.
Alternatively, we can curry the action , and use the mapping:
The target category here is the category of endofunctors , which is naturally equipped with a monoidal structure given by functor composition (and, as we well know, a monad is just a monoid in that category).
The two structure maps from the definition of translate to the requirement that
be a strict monoidal functor, mapping tensor product to functor composition and unit object to identity functor.
The Adjunction
When we were eliminating the coend in the definition of a lens, we used the currying adjunction. This particular adjunction works inside a single category but, in general, an adjunction relates two functors between a pair of categories. Therefore, to eliminate the end from the optic, we need an adjunction that looks like this:
The category on the right is the monoidal category , because
is the object from this category.
Using the adjunction and the co-Yoneda lemma we get:
There is a whole slew of monoidal actions that have the right adjoint of this type. Let’s look at an example.
The category of sets is a monoidal category, and we can define its action on another category using the formula:
This is the definition of a copower. A category in which this adjunction holds for all objects is called copowered or tensored over .
The intuition is that a copower is like an iterated sum (hence the multiplication sign). Indeed, a mapping out of a coproduct of copies of
, where
is a set, is equivalent to
mappings out of
.
This formula generalizes to the case in which is a category enriched over a monoidal category
. In that case we have:
Here, the hom is an object in
.
Relation to Enrichment
We are interested in the adjunction:
The functor is covariant in
and contravariant in
:
In other words, it’s a profunctor. In fact, it has the right signature to be a hom-functor. And this is what Janelidze and Kelly show: the functor can serve as the hom-functor that generates the enrichment for
. If we call the enriched category
, we can define the hom-object as:
Our adjunction can be rewritten as:
The counit of this adjunction is a mapping:
which is analogous to function application.
The hom-object in an enriched category must satisfy the composition and identity laws. In an enriched category, composition is a mapping:
Let’s see if we can get this mapping from our adjunction by replacing with
. We get:
The right-hand side should contain the mapping we’re looking for. All we need is to point at a morphism on the left. Indeed, we can define it as the following composite:
We used the structure map and (twice) the counit of the adjunction
.
Similarly, the identity of composition, which is the mapping:
is adjoint to the other structure map .
Janelidze and Kelly go on to prove that the action of a monoidal right-closed category having the right adjoint is equivalent to the existence of the tensored enrichment of the category on which this action is defined.
The two examples of monoidal actions we’ve seen so far are indeed equivalent to enrichments. A cartesian closed category in which we defined the action is automatically self-enriched. The copower action
is equivalent to enrichment over
(which doesn’t mean much, since regular categories are naturally
-enriched; but not all of them are tensored).
Acknowledgments
I’m grateful to Jules Hedges and his collaborators for sharing their insights and unpublished notes with me.
December 5, 2021 at 9:42 pm
May I suggest that the use of a “Concept Map” of your writings on Category Theory in Programing will be very helpful to the readers of your writings. Please take a look at the following site on how “Concept Maps” help ing transferring Knowledge — https://cmap.ihmc.us
Ramayya
>