(X-posted on the CyberCat Institute blog)
Iβm going to record something that I think is known to everyone doing research on categorical cybernetics, but I donβt think has been written down somewhere: an even more general version of mixed optics that replaces the backwards actegory with an enrichment. With it, Iβll make sense of a curious definition appearing in The Compiler Forest.
Actegories and enrichments
An actegory consists of a monoidal category , a category and a functor that behaves like an βexternal productβ: namely that itβs equipped with coherent isomorphisms and .
An enriched category consists of a category , a monoidal category and a functor that behaves like an βexternal homβ (Iβm not going to write down what this means because itβs more complicated).
Thereβs a very close relationship between actegories and enrichments, to the point that I consider them different perspectives on the same idea. This is the final form of the famous tensor-hom adjunction, aka. currying. (I learned this incredible fact from Matteo Capucci, and I have no idea where itβs written down, although itβs definitely written down somewhere.)
A tensored enrichment is one where every has a left adjoint . Allowing to vary results in a functor which (nontrivial theorem) is always an actegory.
A closed actegory is one where every has a right adjoint . Allowing to vary results in a functor which (nontrivial theorem) is always an enrichment.
So, closed actegories and tensored enrichments are equivalent ways of defining the same thing, namely a monoidal category and category equipped with and related by a tensor-hom adjunction .
Parametrisation
Given an actegory, we can define a bicategory , whose objects are objects of and 1-cells are pairs of and . We can also define a bicategory , whose objects are objects of and 1-cells are pairs of and .
Given an enriched category, we can define a bicategory , whose objects are objects of and morphisms are pairs of and . If this is a tensored enrichment then the two definitions of are equivalent.
In all of these cases we are locally fibred over , and I will write , for the set of co/parametrised morphisms with a fixed parameter type.
Itβs not possible to define for an enrichment. Thereβs a very slick common generalisation of actegories and enrichments called a locally graded category, which is a category enriched in presheaves with Day convolution. Thereβs also a very slick definition of for a locally graded category. Iβd like to know, for exactly which locally graded categories is possible to define ?
Mixed optics
If we have two actegories that share the same acting category then we can define mixed optics, which first appeared in Profunctor Optics: A Categorical Update. This is a 1-category whose objects are pairs of an object of and an object of , and a morphism is an element of the coend
$$ $\int^{M : \mathcal M} \mathbf{Copara}_\mathcal M (\mathcal C) (X, Y) (M) \times \mathbf{Para}_\mathcal M (\mathcal D) (Y', X') (M) $$
Thereβs a slightly more general definition called βweighted opticsβ that appears in Brunoβs thesis and was used very productively there, which replaces with two monoidal categories related by a Tambara module. I think that itβs an orthogonal generalisation to the one Iβm about to do here.
Enriched closed lenses
Putting together everything Iβve just said, the next step is clear. If we have categories and a monoidal category , with acting on and enriched in , then we can still define in exactly the same way, replacing with its enriched version. But now, unlike before, we can use the ninja Yoneda lemma to eliminate the coend and get
In general I refer to optics that can be defined without type quantification as lenses, and so this is an enriched closed lens. Itβs the final form of βlinear lensesβ, the version of lenses that is defined like Lens s t a b = s -> (a, b -> t)
.
Into the compiler forest
Section 5 of The Compiler Forest by Budiu, Galenson and Plotkin has a very interesting definition in it. They have a cartesian closed category (whose internal hom Iβll write as ) and a strong monad on it, and they define a category whose objects are pairs of objects of and whose morphisms are morphisms of .
They also nail an intuition for lenses that I use constantly and I havenβt seen written down anywhere else: problems go forwards, solutions go backwards.
Me and this definition have quite a history. It came to my attention while polishing Bayesian Open Games for submission. For a while, I thought that it was equivalent to optics in the kleisli category of , and weβd wasted a years of our lives trying to understand optics (this being around 2018, when optics were still a niche idea). Then, for a while I thought that the paper made a mistake and these things donβt compose associatively. Now Iβve made peace: I think their definition is conceptually subtly wrong in a way that makes no difference in practice, and I can say very precisely how it relates to kleisli optics.
There is an action of on given by , where is the tensor product of which on objects is given by the product of . Thatβs the actegory generated by the strong monoidal embedding . There is also an enrichment of in , given by . This action and enrichment are adjoint to each other: .
The category defined in Compiler Forest turns out to be equivalent to
whose forwards pass is given by the action of on and whose backwards pass is given by the enrichment of in . Its hom-sets are given by
which Yoneda-reduces to the definition in the paper.
Even though the action and enrichment are adjoint, this is not the same as optics in the klesli category:
where the hom-sets of the latter are defined by
This equivalence, between optics whose backwards passes are an adjoint action or enrichment, would be a completely reasonable-looking lemma but it just isnβt true!
The difference between them is extremely subtle, though. The βproperβ definition of kleisli optics identifies morphisms that agree up to sliding any kleisli morphism, whereas the definition in Compiler Forest only identifies morphisms that agree up to sliding pure morphisms of . So hom-sets of coend optics are a quotient of the hom-sets defined in Compiler Forest. While writing this up, I realised that most of this conclusion actually appears in section 4.9 of Rileyβs original paper.
As long as you donβt care about equality of morphisms - which in practice is never, because they are made of functions - the difference between them can be safely ignored. The only genuine reason to prefer kleisli optics is their better runtime performance.