April 2021
Monthly Archive
April 1, 2021
Previously: Profunctors.
Traversals
A traversal is a kind of optic that can focus on zero or more items at a time. Naively, we would expect to have a getter that returns a list of values, and a setter that replaces a list of values. Think of a tree with
leaves: a traversal would return a list of leaves, and it would allow you to replace them with a new list. The problem is that the size of the list you pass to the setter cannot be arbitrary—it must match the number of leaves in the particular tree. This is why, in Haskell, the setter and the getter are usually combined in a single function:
s -> ([b] -> t, [a])
Still, Haskell is not able to force the sizes of both lists to be equal.
Since a list type can be represented as an infinite sum of tuples, I knew that the categorical version of this formula must involve a power series, or a polynomial functor:

but was unable to come up with an existential form for it.
Pickering, Gibbons, and Wu came up with a representation for traversals using profunctors that were cartesian, cocartesian, and monoidal at the same time, but the monoidal constraint didn’t fit neatly into the Tambara scheme:
class Profunctor p => Monoidal p where
par :: p a b -> p c d -> p (a, c) (b, d)
empty :: p () ()
We’ve been struggling with this problem, when one of my students, Mario Román came up with the ingenious idea to make
existential.
The idea is that a coend in the existential representation of optics acts like a sum (or like an integral—hence the notation). A sum over natural numbers is equivalent to the coend over the category of natural numbers.
At the root of all optics there is a monoidal action. For lenses, this action is given by “scaling”

For prisms, it’s the “translation”

For grates it’s the exponentiation

The composition of a prism and a lens is an affine transformation

A traversal is similarly generated by a polynomial functor, or a power series functor:

The key observation here is that there is a different object
for every power of
, which can only be expressed using dependent types in programming. For every multiplicity of foci, the residue is of a different type.
In category theory, we can express the whole infinite sequence of residues as a functor from the monoidal category
of natural numbers to
. (The sum is really a coend over
.)
The existential version of a traversal is thus given by:
![\int^{c \colon [\mathbb{N}, \mathbf{Set}]} \mathbf{Set}\big(s, \sum_n c_n \times a^n\big) \times \mathbf{Set}\big( \sum_m c_m \times b^m, t\big)](https://s0.wp.com/latex.php?latex=%5Cint%5E%7Bc+%5Ccolon+%5B%5Cmathbb%7BN%7D%2C+%5Cmathbf%7BSet%7D%5D%7D+%5Cmathbf%7BSet%7D%5Cbig%28s%2C+%5Csum_n+c_n+%5Ctimes+a%5En%5Cbig%29+%5Ctimes+%5Cmathbf%7BSet%7D%5Cbig%28+%5Csum_m+c_m+%5Ctimes+b%5Em%2C+t%5Cbig%29+&bg=ffffff&fg=29303b&s=0&c=20201002)
We can now use the continuity of the hom-set to replace the mapping out of a sum with a product of mappings:
![\int^{c \colon [\mathbb{N}, \mathbf{Set}]} \mathbf{Set}\big(s, \sum_n c_n \times a^n\big) \times \prod_m \mathbf{Set}\big( c_m \times b^m, t\big)](https://s0.wp.com/latex.php?latex=%5Cint%5E%7Bc+%5Ccolon+%5B%5Cmathbb%7BN%7D%2C+%5Cmathbf%7BSet%7D%5D%7D+%5Cmathbf%7BSet%7D%5Cbig%28s%2C+%5Csum_n+c_n+%5Ctimes+a%5En%5Cbig%29+%5Ctimes+%5Cprod_m+%5Cmathbf%7BSet%7D%5Cbig%28+c_m+%5Ctimes+b%5Em%2C+t%5Cbig%29+&bg=ffffff&fg=29303b&s=0&c=20201002)
and use the currying adjunction
![\int^{c \colon [\mathbb{N}, \mathbf{Set}]} \mathbf{Set}\big(s, \sum_n c_n \times a^n\big) \times \prod_m \mathbf{Set}\big( c_m, \mathbf{Set}( b^m, t)\big)](https://s0.wp.com/latex.php?latex=%5Cint%5E%7Bc+%5Ccolon+%5B%5Cmathbb%7BN%7D%2C+%5Cmathbf%7BSet%7D%5D%7D+%5Cmathbf%7BSet%7D%5Cbig%28s%2C+%5Csum_n+c_n+%5Ctimes+a%5En%5Cbig%29+%5Ctimes+%5Cprod_m+%5Cmathbf%7BSet%7D%5Cbig%28+c_m%2C+%5Cmathbf%7BSet%7D%28+b%5Em%2C+t%29%5Cbig%29+&bg=ffffff&fg=29303b&s=0&c=20201002)
The product of hom-sets is really an end over
, or a set of natural transformations in ![[\mathbb{N}, \mathbf{Set}]](https://s0.wp.com/latex.php?latex=%5B%5Cmathbb%7BN%7D%2C+%5Cmathbf%7BSet%7D%5D&bg=ffffff&fg=29303b&s=0&c=20201002)
![\int^{c \colon [\mathbb{N}, \mathbf{Set}]} \mathbf{Set}\big(s, \sum_n c_n \times a^n\big) \times [\mathbb{N}, \mathbf{Set}]\big( c_-, \mathbf{Set}( b^-, t)\big)](https://s0.wp.com/latex.php?latex=%5Cint%5E%7Bc+%5Ccolon+%5B%5Cmathbb%7BN%7D%2C+%5Cmathbf%7BSet%7D%5D%7D+%5Cmathbf%7BSet%7D%5Cbig%28s%2C+%5Csum_n+c_n+%5Ctimes+a%5En%5Cbig%29+%5Ctimes+%5B%5Cmathbb%7BN%7D%2C+%5Cmathbf%7BSet%7D%5D%5Cbig%28+c_-%2C+%5Cmathbf%7BSet%7D%28+b%5E-%2C+t%29%5Cbig%29+&bg=ffffff&fg=29303b&s=0&c=20201002)
and we can apply the Yoneda lemma to “integrate” over
to get:

which is exactly the formula for traversals.
Once we understood the existential representation of traversals, the profunctor representation followed. The equivalent of Tambara modules for traversals is a category of profunctors equipped with the monoidal action parameterized by objects in
:

The double Yoneda trick works for these profunctors as well, proving the equivalence with the existential representation.
Generalizations
As hinted in my blog post and formalized by Mitchell Riley, Tambara modules can be generalized to an arbitrary monoidal action. We have also realized that we can combine actions in two different categories. We could take an arbitrary monoidal category
, define its action on two categories,
and
using strong monoidal functors:
![F \colon \mathcal{M} \to [\mathcal{C}, \mathcal{C}]](https://s0.wp.com/latex.php?latex=F+%5Ccolon+%5Cmathcal%7BM%7D+%5Cto+%5B%5Cmathcal%7BC%7D%2C+%5Cmathcal%7BC%7D%5D+&bg=ffffff&fg=29303b&s=0&c=20201002)
![G \colon \mathcal{M} \to [\mathcal{D}, \mathcal{D}]](https://s0.wp.com/latex.php?latex=G+%5Ccolon+%5Cmathcal%7BM%7D+%5Cto+%5B%5Cmathcal%7BD%7D%2C+%5Cmathcal%7BD%7D%5D+&bg=ffffff&fg=29303b&s=0&c=20201002)
These actions define the most general existential optic:

Notice that the pairs of arguments are heterogenous—e.g., in
,
is from
, and
is from
.
We have also generalized Tambara modules:

and the Pastro Street derivation of the promonad. That lead us to a more general proof of isomorphism between the profunctor formulation and the existential formulation of optics. Just to be general enough, we did it for enriched categories, replacing
with an arbitrary monoidal category.
Finally, we described some new interesting optics like algebraic and monadic lenses.
The Physicist’s Explanation
The traversal result confirmed my initial intuition from general relativity that the most general optics are generated by the analog of diffeomorphisms. These are the smooth coordinate transformations under which Einstein’s theory is covariant.
Physicists have long been using symmetry groups to build theories. Laws of physics are symmetric with respect to translations, time shifts, rotations, etc.; leading to laws of conservation of momentum, energy, angular momentum, etc. There is an uncanny resemblance of these transformations to some of the monoidal actions in optics. The prism is related to translations, the lens to rotations or scaling, etc.
There are many global symmetries in physics, but the real power comes from local symmetries: gauge symmetries and diffeomorphisms. These give rise to the Standard Model and to Einstein’s theory of gravity.
A general monoidal action seen in optics is highly reminiscent of a diffeomorphism, and the symmetry behind a traversal looks like it’s generated by an analytical function.
In my opinion, these similarities are a reflection of a deeper principle of compositionality. There is only a limited set of ways we can decompose complex problems, and sooner or later they all end up in category theory.
The main difference between physics and category theory is that category theory is more interested in one-way mappings, whereas physics deals with invertible transformations. For instance, in category theory, monoids are more fundamental than groups.
Here’s how categorical optics might be seen by a physicist.
In physics we would start with a group of transformations. Its representations would be used, for instance, to classify elementary particles. In optics we start with a monoidal category
and define its action in the target category
. (Notice the use of a monoid rather than a group.)
![F \colon \mathcal{M} \to [\mathcal{C}, \mathcal{C}]](https://s0.wp.com/latex.php?latex=F+%5Ccolon+%5Cmathcal%7BM%7D+%5Cto+%5B%5Cmathcal%7BC%7D%2C+%5Cmathcal%7BC%7D%5D+&bg=ffffff&fg=29303b&s=0&c=20201002)
In physics we would represent the group using matrices, here we use endofunctors.
A profunctor is like a path that connects the initial state to the final state. It describes all the ways in which
can evolve into
.
If we use mixed optics, final states come from a different category
, but their transformations are parameterized by the same monoidal category:
![G \colon \mathcal{M} \to [\mathcal{D}, \mathcal{D}]](https://s0.wp.com/latex.php?latex=G+%5Ccolon+%5Cmathcal%7BM%7D+%5Cto+%5B%5Cmathcal%7BD%7D%2C+%5Cmathcal%7BD%7D%5D+&bg=ffffff&fg=29303b&s=0&c=20201002)
A path may be arbitrarily extended, at both ends, by a pair of morphisms. Given a morphism in
:

and another one in 

the profunctor uses them to extend the path:


A (generalized) Tambara module is like the space of paths that can be extended by transforming their endpoints.

If we have a path that can evolve
into
, then the same path can be used to evolve
into
. In physics, we would say that the paths are “invariant” under the transformation, but in category theory we are fine with a one-way mapping.

The profunctor representation is like a path integral:

We fix the end-states but we vary the paths. We integrate over all paths that have the “invariance” or extensibility property that defines the Tambara module.
For every such path, we have a mapping that takes the evolution from
to
and produces the evolution (along the same path) from
to
.
The main theorem of profunctor optics states that if, for a given collection of states,
, such a mapping exists, then these states are related. There exists a transformation and a pair of morphisms that are secretly used in the path integral to extend the original path.

Again, the mappings are one-way rather than both ways. They let us get from
to
and from
to
.
This pair of morphisms is enough to extend any path
to
by first applying
and then lifting the two morphisms. The converse is also true: if every path can be extended then such a pair of morphisms must exist.

What seems unique to optics is the interplay between transformations and decompositions: The way
can be interpreted both as parameterizing a monoidal action and the residue left over after removing the focus.
Conclusion
For all the details and a list of references you can look at our paper “Profunctor optics, a categorical update.” It’s the result of our work at the Adjoint School of Applied Category Theory in Oxford in 2019. It’s avaliable on arXiv.
I’d like to thank Mario Román for reading the draft and providing valuable feedback.
April 1, 2021
Previously: Existentials.
Double Yoneda
If you squint hard enough, the Yoneda lemma:

could be interpreted as the representable functor
acting as the unit with respect to taking the end. It takes an
and returns an
. Let’s keep this in mind.
We are going to need an identity that involves higher-order natural transformations between two higher-order functors. These are actually the functors
that we’ve encountered before. They are parameterized by objects in
, and their action on functors (co-presheaves) is to apply those functors to objects. They are the “give me a functor and I’ll apply it to my favorite object” kind of functors.
We need a natural transformation between two such functors, and we can express it as an end:

Here’s the trick: replace these functors with their Yoneda equivalents:

Notice that this is now a mapping between two hom-sets in the functor category, the first one being:
![\int_{x} \mathbf{Set}\big(\mathcal{C}(a, x), fx\big) = [\mathcal{C}, \mathbf{Set}]\big(\mathcal{C}(a, -), f\big)](https://s0.wp.com/latex.php?latex=%5Cint_%7Bx%7D+%5Cmathbf%7BSet%7D%5Cbig%28%5Cmathcal%7BC%7D%28a%2C+x%29%2C+fx%5Cbig%29+%3D+%5B%5Cmathcal%7BC%7D%2C+%5Cmathbf%7BSet%7D%5D%5Cbig%28%5Cmathcal%7BC%7D%28a%2C+-%29%2C+f%5Cbig%29&bg=ffffff&fg=29303b&s=0&c=20201002)
We can now use the corollary of the Yoneda lemma to replace the set of natural transformation between these two hom-functors with the hom-set:
![[\mathcal{C}, \mathbf{Set}]\big(\mathcal{C}(s, -), \mathcal{C}(a, -) \big)](https://s0.wp.com/latex.php?latex=%5B%5Cmathcal%7BC%7D%2C+%5Cmathbf%7BSet%7D%5D%5Cbig%28%5Cmathcal%7BC%7D%28s%2C+-%29%2C+%5Cmathcal%7BC%7D%28a%2C+-%29+%5Cbig%29&bg=ffffff&fg=29303b&s=0&c=20201002)
But this is again a natural transformation between two hom-functors, so it can be further reduced to
. The result is:

We’ve used the Yoneda lemma twice, so this trick is called the double-Yoneda.
Profunctors
It turns out that the prism also has a functor-polymorphic representation, but it uses profunctors in place of regular functors. A profunctor is a functor of two arguments, but its action on arrows has a twist. Here’s the Haskell definition:
class Profunctor p where
dimap :: (a' -> a) -> (b -> b') -> (p a b -> p a' b')
It lifts a pair of functions, where the first one goes in the opposite direction.
In category theory, the “twist” is encoded by using the opposite category
, so a profunctor is defined a functor from
to
.
The prime example of a profunctor is the hom-functor which, on objects, assigns the set
to every pair
.
Before we talk about the profunctor representation of prisms and lenses, there is a simple optic called Iso
. It’s defined by a pair of functions:
from :: s -> a
to :: b -> t
The key observation here is that such a pair of arrows is an element of the hom set in the category
between the pair
and the pair
:

The “twist” of using
reverses the direction of the first arrow.
Iso
has a simple profunctor representation:
type Iso s t a b = forall p. Profunctor p => p a b -> p s t
This formula can be translated to category theory as an end in the profunctor category:

Profunctor category is a category of co-presheaves
. We can immediately apply the double Yoneda identity to it to get:

which shows the equivalence of the two representations.
Tambara Modules
Here’s the profunctor representation of a prism:
type Prism s t a b = forall p. Choice p => p a b -> p s t
It looks almost the same as Iso
, except that the quantification goes over a smaller class of profunctors called Choice
(or cocartesian). This class is defined as:
class Profunctor p => Choice where
left' :: p a b -> p (Either a c) (Either b c)
right' :: p a b -> p (Either c a) (Either c b)
Lenses can also be defined in a similar way, using the class of profunctors called Strong
(or cartesian).
class Profunctor p => Strong where
first' :: p a b -> p (a, c) (b, c)
second' :: p a b -> p (c, a) (c, b)
Profunctor categories with these structures are called Tambara modules. Tambara formulated them in the context of monoidal categories, for a more general tensor product. Sum (Either
) and product (,)
are just two special cases.
A Tambara module is an object in a profunctor category with additional structure defined by a family of morphisms:

with some naturality and coherence conditions.
Lenses and prisms can thus be defined as ends in the appropriate Tambara modules

We can now use the double Yoneda trick to get the usual representation.
The problem is, we don’t know in what category the result should be. We know the objects are pairs
, but what are the morphisms between them? It turns out this problem was solved in a paper by Pastro and Street. The category in question is the Kleisli category for a particular promonad. This category is now better known as
. Let me explain.
Double Yoneda with Adjunctions
The double Yoneda trick worked for an unconstrained category of functors. We need to generalize it to a category with some additional structure (for instance, a Tambara module).
Let’s say we start with a functor category
and endow it with some structure, resulting in another functor category
. It means that there is a (higher-order) forgetful functor
that forgets this additional structure. We’ll also assume that there is the right adjoint functor
that freely generates the structure.
We will re-start the derivation of double Yoneda using the forgetful functor

Here,
and
are objects in
and
is a functor in
.
We perform the Yoneda trick the same way as before to get:

Again, we have two sets of natural transformations, the first one being:
![\int_{x \colon C} \mathbf{Set}\big(\mathcal{C}(a, x), (U f) x\big) = [\mathcal{C}, \mathbf{Set}]\big(\mathcal{C}(a, -), U f\big)](https://s0.wp.com/latex.php?latex=%5Cint_%7Bx+%5Ccolon+C%7D+%5Cmathbf%7BSet%7D%5Cbig%28%5Cmathcal%7BC%7D%28a%2C+x%29%2C+%28U+f%29+x%5Cbig%29+%3D+%5B%5Cmathcal%7BC%7D%2C+%5Cmathbf%7BSet%7D%5D%5Cbig%28%5Cmathcal%7BC%7D%28a%2C+-%29%2C+U+f%5Cbig%29&bg=ffffff&fg=29303b&s=0&c=20201002)
The adjunction tells us that
![[\mathcal{C}, \mathbf{Set}]\big(\mathcal{C}(a, -), U f\big) \cong \mathcal{T}\Big(F\big(\mathcal{C}(a, -)\big), f\Big)](https://s0.wp.com/latex.php?latex=%5B%5Cmathcal%7BC%7D%2C+%5Cmathbf%7BSet%7D%5D%5Cbig%28%5Cmathcal%7BC%7D%28a%2C+-%29%2C+U+f%5Cbig%29+%5Ccong+%5Cmathcal%7BT%7D%5CBig%28F%5Cbig%28%5Cmathcal%7BC%7D%28a%2C+-%29%5Cbig%29%2C+f%5CBig%29&bg=ffffff&fg=29303b&s=0&c=20201002)
The right-hand side is a hom-set in the functor category
. Plugging this back into the original formula, we get

This is the set of natural transformations between two hom-functors, so we can use the corollary of the Yoneda lemma to replace it with:

We can then use the adjunction again, in the opposite direction, to get:
![[\mathcal{C}, \mathbf{Set}] \Big( \mathcal{C}(s, -), (U \circ F)\big(\mathcal{C}(a, -)\big) \Big)](https://s0.wp.com/latex.php?latex=%5B%5Cmathcal%7BC%7D%2C+%5Cmathbf%7BSet%7D%5D+%5CBig%28+%5Cmathcal%7BC%7D%28s%2C+-%29%2C+%28U+%5Ccirc+F%29%5Cbig%28%5Cmathcal%7BC%7D%28a%2C+-%29%5Cbig%29+%5CBig%29+&bg=ffffff&fg=29303b&s=0&c=20201002)
or, using the end notation:

Finally, we use the Yoneda lemma again to get:

This is the action of the higher-order functor
on the hom-functor
, the result of which is applied to
.
The composition of two functors that form an adjunction is a monad
. This is a monad in the functor category
. Altogether, we get:

Profunctor Representation of Lenses and Prisms
The previous formula can be immediately applied to the category of Tambara modules. The forgetful functor takes a Tambara module and maps it to a regular profunctor
, an object in the functor category
. We replace
and
with pairs of objects. We get:

The only missing piece is the higher order monad
—a monad operating on profunctors.
The key observation by Pastro and Street was that Tambara modules are higher-order coalgebras. The mappings:

can be thought of as components of a natural transformation

By continuity of hom-sets, we can move the end over
to the right:

We can use this to define a higher order functor that acts on profunctors:

so that the family of Tambara mappings can be written as a set of natural transformations
:

Natural transformations are morphisms in the category of profunctors, and such a morphism
is, by definition, a coalgebra for the functor
.
Pastro and Street go on showing that
is more than a functor, it’s a comonad, and the Tambara structure is not just a coalgebra, it’s a comonad coalgebra.
What’s more, there is a monad that is adjoint to this comonad:

When a monad is adjoint to a comonad, the comonad coalgebras are isomorphic to monad algebras—in this case, Tambara modules. Indeed, the algebras
are given by natural transformations:

Substituting the formula for
,

by continuity of the hom-set (with the coend in the negative position turning into an end),

using the currying adjunction,

and the Yoneda lemma, we get

which is the Tambara structure
.
is exactly the monad that appears on the right-hand side of the double-Yoneda with adjunctions. This is because every monad can be decomposed into a pair of adjoint functors. The decomposition we’re interested in is the one that involves the Kleisli category of free algebras for
. And now we know that these algebras are Tambara modules.
All that remains is to evaluate the action of
on the represesentable functor:

It’s a matter of simple substitution:

and using the Yoneda lemma to replace
with
. The result is:

This is exactly the existential represenation of the lens and the prism:

This was an encouraging result, and I was able to derive a few other optics using the same approach.
The idea was that Tambara modules were just one example of a monoidal action, and it could be easily generalized to other types of optics, like Grate
, where the action
is replaced by the (contravariant in
) action
(or c->a
, in Haskell).
There was just one optic that resisted that treatment, the Traversal
. The breakthrough came when I was joined by a group of talented students at the Applied Category Theory School in Oxford.
Next: Traversals.
April 1, 2021
Posted by Bartosz Milewski under
Programming | Tags:
Category Theory,
Lens,
Optics |
[3] Comments
Note: A PDF version of this series is available on github.
My gateway drug to category theory was the Haskell lens library. What first piqued my attention was the van Laarhoven representation, which used functions that are functor-polymorphic. The following function type:
type Lens s t a b =
forall f. Functor f => (a -> f b) -> (s -> f t)
is isomorphic to the getter/setter pair that traditionally defines a lens:
get :: s -> a
set :: s -> b -> t
My intuition was that the Yoneda lemma must be somehow involved. I remember sharing this idea excitedly with Edward Kmett, who was the only expert on category theory I knew back then. The reasoning was that a polymorphic function in Haskell is equivalent to a natural transformation in category theory. The Yoneda lemma relates natural transformations to functor values. Let me explain.
In Haskell, the Yoneda lemma says that, for any functor f
, this polymorphic function:
forall x. (a -> x) -> f x
is isomorphic to (f a)
.
In category theory, one way of writing it is:

If this looks a little intimidating, let me go through the notation:
- The functor
goes from some category
to the category of sets, which is called
. Such functor is called a co-presheaf.
stands for the set of arrows from
to
in
, so it corresponds to the Haskell type a->x
. In category theory it’s called a hom-set. The notation for hom-sets is: the name of the category followed by names of two objects in parentheses.
stands for a set of functions from
to
or, in Haskell (a -> x)-> f x
. It’s a hom-set in
.
- Think of the integral sign as the
forall
quantifier. In category theory it’s called an end. Natural transformations between two functors
and
can be expressed using the end notation:

As you can see, the translation is pretty straightforward. The van Laarhoven representation in this notation reads:

If you vary
in
, it becomes a functor, which is called a representable functor—the object
“representing” the whole functor. In Haskell, we call it the reader functor:
newtype Reader b x = Reader (b -> x)
You can plug a representable functor for
in the Yoneda lemma to get the following very important corollary:

The set of natural transformation between two representable functors is isomorphic to a hom-set between the representing objects. (Notice that the objects are swapped on the right-hand side.)
The van Laarhoven representation
There is just one little problem: the forall
quantifier in the van Laarhoven formula goes over functors, not types.
This is okay, though, because category theory works at many levels. Functors themselves form a category, and the Yoneda lemma works in that category too.
For instance, the category of functors from
to
is called
. A hom-set in that category is a set of natural transformations between two functors which, as we’ve seen, can be expressed as an end:
 \cong \int_x \mathbf{Set}(f x, g x)](https://s0.wp.com/latex.php?latex=%5B%5Cmathcal%7BC%7D%2C%5Cmathbf%7BSet%7D%5D%28f%2C+g%29+%5Ccong+%5Cint_x+%5Cmathbf%7BSet%7D%28f+x%2C+g+x%29+&bg=ffffff&fg=29303b&s=0&c=20201002)
Remember, it’s the name of the category, here
, followed by names of two objects (here, functors
and
) in parentheses.
So the corollary to the Yoneda lemma in the functor category, after a few renamings, reads:
, [\mathcal{C},\mathbf{Set}](h, f)\big) \cong [\mathcal{C},\mathbf{Set}](h, g)](https://s0.wp.com/latex.php?latex=%5Cint_f+%5Cmathbf%7BSet%7D%5Cbig%28+%5B%5Cmathcal%7BC%7D%2C%5Cmathbf%7BSet%7D%5D%28g%2C+f%29%2C+%5B%5Cmathcal%7BC%7D%2C%5Cmathbf%7BSet%7D%5D%28h%2C+f%29%5Cbig%29+%5Ccong+%5B%5Cmathcal%7BC%7D%2C%5Cmathbf%7BSet%7D%5D%28h%2C+g%29+&bg=ffffff&fg=29303b&s=0&c=20201002)
This is getting closer to the van Laarhoven formula because we have the end over functors, which is equivalent to
forall f. Functor f => ...
In fact, a judicious choice of
and
is all we need to finish the proof.
But sometimes it’s easier to define a functor indirectly, as an adjoint to another functor. Adjunctions actually allow us to switch categories. A functor
defined by a mapping-out in one category can be adjoint to another functor
defined by its mapping-in in another category.

A useful example is the currying adjunction in
:

where
corresponds to the function type a->y
and, in
, is isomorphic to the hom-set
. This is just saying that a function of two arguments is equivalent to a function returning a function.
Here’s the clever trick: let’s replace
and
in the functorial Yoneda lemma with
and
, where
and
are some higher-order functors from
to
(as you will see, this notation anticipates the final substitution). We get:
, [\mathcal{C},\mathbf{Set}](L_t s, f)\big) \cong [\mathcal{C},\mathbf{Set}](L_t s, L_b a)](https://s0.wp.com/latex.php?latex=%5Cint_f+%5Cmathbf%7BSet%7D%5Cbig%28+%5B%5Cmathcal%7BC%7D%2C%5Cmathbf%7BSet%7D%5D%28L_b+a%2C+f%29%2C+%5B%5Cmathcal%7BC%7D%2C%5Cmathbf%7BSet%7D%5D%28L_t+s%2C+f%29%5Cbig%29+%5Ccong+%5B%5Cmathcal%7BC%7D%2C%5Cmathbf%7BSet%7D%5D%28L_t+s%2C+L_b+a%29+&bg=ffffff&fg=29303b&s=0&c=20201002)
Now suppose that these functors are left adjoint to some other functors:
and
that go in the opposite direction from
to
. We can then replace all mappings-out in
with the corresponding mappings-in in
:

We are almost there! The last step is to realize that, in order to get the van Laarhoven formula, we need:


So these are just functors that apply
to some fixed objects:
and
, respectively. The left-hand side becomes:

which is exactly the van Laarhoven representation.
Now let’s look at the right-hand side:

We know what
is, but what’s its left adjoint
? It must satisfy the adjunction:
 \cong \mathcal{C}(a, R_b f) = \mathcal{C}(a, f b)](https://s0.wp.com/latex.php?latex=%5B%5Cmathcal%7BC%7D%2C%5Cmathbf%7BSet%7D%5D%28L_b+a%2C+f%29+%5Ccong+%5Cmathcal%7BC%7D%28a%2C+R_b+f%29+%3D+%5Cmathcal%7BC%7D%28a%2C+f+b%29&bg=ffffff&fg=29303b&s=0&c=20201002)
or, using the end notation:

This identity has a simple solution when
is
, so we’ll just temporarily switch to
. We have:

which is known as the IStore
comonad in Haskell. We can check the identity by first applying the currying adjunction to eliminate the product:

and then using the Yoneda lemma to “integrate” over
, which replaces
with
,

So the right hand side of the original identity (after replacing
with
) becomes:

which can be translated to Haskell as:
(s -> b -> t, s -> a)
or a pair of set
and get
.
I was very proud of myself for finding the right chain of substitutions, so I was pretty surprised when I learned from Mauro Jaskelioff and Russell O’Connor that they had a paper ready for publication with exactly the same proof. (They added a reference to my blog in their publication, which was probably a first.)
The Existentials
But there’s more: there are other optics for which this trick doesn’t work. The simplest one was the prism defined by a pair of functions:
match :: s -> Either t a
build :: b -> t
In this form it’s hard to see a commonality between a lens and a prism. There is, however, a way to unify them using existential types.
Here’s the idea: A lens can be applied to types that, at least conceptually, can be decomposed into two parts: the focus and the residue. It lets us extract the focus using get
, and replace it with a new value using set
, leaving the residue unchanged.
The important property of the residue is that it’s opaque: we don’t know how to retrieve it, and we don’t know how to modify it. All we know about it is that it exists and that it can be combined with the focus. This property can be expressed using existential types.
Symbolically, we would want to write something like this:
type Lens s t a b = exists c . (s -> (c, a), (c, b) -> t)
where c
is the residue. We have here a pair of functions: The first decomposes the source s
into the product of the residue c
and the focus a
. The second recombines the residue with the new focus b
resulting in the target t
.
Existential types can be encoded in Haskell using GADTs:
data Lens s t a b where
Lens :: (s -> (c, a), (c, b) -> t) -> Lens s t a b
They can also be encoded in category theory using coends. So the lens can be written as:

The integral sign with the argument at the top is called a coend. You can read it as “there exists a
”.
There is a version of the Yoneda lemma for coends as well:

The intuition here is that, given a functorful of
‘s and a function c->a
, we can fmap
the latter over the former to obtain f a
. We can do it even if we have no idea what the type c
is.
We can use the currying adjunction and the Yoneda lemma to transform the new definition of the lens to the old one:

The exponential
translates to the function type b->t
, so this this is really the set
/get
pair that defines the lens.
The beauty of this representation is that it can be immediately applied to the prism, just by replacing the product with the sum (coproduct). This is the existential representation of a prism:

To recover the standard encoding, we use the mapping-out property of the sum:

This is simply saying that a function from the sum type is equivalent to a pair of functions—what we call case analysis in programming.
We get:

This has the form suitable for the use of the Yoneda lemma, namely:

with the functor

The result of the Yoneda is replacing
with
, so the result is:

which is exactly the match
/build
pair (in Haskell, the sum is translated to Either
).
It turns out that every optic has an existential form.
Next: Profunctors.