There are two different generalises of lenses that are important in my research. One is optics, which are a non-obvious generalisation of lenses that work over a monoidal category (whereas lenses only work over a finite product category). We use optics in Bayesian open games, over the category of Markov kernels (kleisli category of probability). The other is dependent lenses, also known as containers and equivalent to polynomial functors. These haven’t appeared in a game theory paper yet, but I use them privately to handle external choice of games better than lenses do.

An interesting and probably-hard question is to find a common generalisation of optics and dependent lenses. In this post I’ll outline the problem and explain a (probable) partial solution that might be useful for somebody, but doesn’t appear useful in game theory. This post will be heavy on category theory: I assume knowledge of fibred categories and the Grothendieck construction.

If is a finite product category and are objects of , then a lens is a pair of functions and . Lenses form a category . If is only a monoidal category and we try to define a lens by and then the composition fails to be associative, so they do not form a category.

There is one way to recover lenses over a monoidal category: we require and to be comonoids in , and we require to be a comonoid homomorphism. For my particular use case this is no good. I take to be the category of Markov kernels, in which every object is canonically a comonoid, but the comonoid homomorphisms are exactly the deterministic maps. In game theory the forwards part is *play* and is generally stochastic.

The “view” functor given by and turns out to be a fibration. The fibre over turns out to be the opposite of the co-kleisli category of the (reader) comonad on . (This co-kleisli category was characterised in Lambek and Scott’s Introduction to higher order categorical logic: it is the “polynomial category” that results from freely adjoining a point to and then closing under finite products.) So the category of lenses can be equivalently constructed via the Grothendieck construction:

Next I’ll talk about dependent lenses. I’m not sure exactly what structure you need on to make it work, I think locally cartesian closed with pullbacks, but if in doubt I’m really thinking about . If are objects and and are dependent types (formally, “fibre bundles” and ), then a dependent lens consists of and (written in pseudo-Agda notation) , that is, . In purely categorical terms, this is a morphism such that the composite equals the projection. They form a category, which I’ll call .

David Spivak taught me this construction, which is apparently folkloric in algebraic geometry. It is a special case of morphisms of ringed spaces, where both spaces and rings are replaced by sets. Ordinary lenses are the full subcategory in which and are constants, or in fibrational terms, when and are the projections. is also known as the category of containers, and I believe that (maybe with some more assumptions) is equivalent to the category of polynomial endofunctors and (all) natural transformations on .

I’ll say something about why dependent lenses come up in game theory. is complete and cocomplete, but in particular I need that it has all coproducts. , on the other hand, only has coproducts of the form . Coproducts of this form are used to construct the external choice operator on open games, which is extremely useful in practice: see sections 8-9 of Morphisms of open games. If we try to take an external choice between open games with source types and , then if we insert a state in then we get out a coutility in , and if we insert a state in then we get out a coutility in . Thus the type of coutility is the *dependent* type over that is over and over .

In this sense dependent types enter the problem “naturally”. Taking a coproduct of ordinary lenses, in general, will result in a dependent lens. Working with open games over the category of dependent lenses will make life easier by making the external choice operator defined everywhere, which makes the resulting graphical language (surface diagrams over a bimonoidal/rig category) significantly better behaved. The catch is that this can only be done for pure strategies, so my difficult question amounts to: how to do this for Bayesian open games? It can beĀ *hacked* over lenses but is an unexploded bomb, because taking a cross section through a surface diagram need not result in a well-defined object.

There is still a fibration . This time it turns out that the fibre over is the opposite of the slice category . It turns out that is also equivalent to the co-Eilenberg-Moore category of the comonad. (I find this the most beautiful part of the whole story.) Thus we can also construct the category of dependent lenses by Grothendieck:

Finally on to optics. If is a monoidal category and are objects then the appropriate generalisation of lenses, which are called optics , are elements of the coend (in the category of sets)

They form a category . When the monoidal product of is a categorical product, then is equivalent to , by an argument involving the Ninja Yoneda Lemma (which says that behaves like a Dirac measure for the integral). Optics have many interesting special cases and also can be generalised further to *mixed optics*, see A categorical update.

One nice thing about the previous cases breaks down: there is no longer a view functor , let alone a fibration.

Here is one notion of “dependent optics” which might be useful for somebody, but is not what I need in game theory. This starts from this MathOverflow post (which was brought to my attention by sarahzrf) whose answers say that *in some sens**e *the appropriate analogue of when is monoidal is the category of comodules over a commutative comonoid in .

A comonoid homomorphism appears to lift to a functor (for the slice category this is contravariant, this is one point that I don’t understand yet). A morphism which is not a comonoid homomorphism does not lift in this way. So I believe we can define a category of sort-of “dependent optics” by Grothendieck construction:

Unfortunately this means that the forwards maps in the category category are forced to be comonoid homomorphisms, so this is actually a generalisation of *lenses* over a monoidal category as I defined at the beginning, rather than a generalisation of optics, and are not suitable for my problem for the same reason.

Fundamentally I expect the “true” category of dependent optics will not be a fibred category, as ordinary optics are not, and so this strategy of defining the category by Grothendieck construction starting from a generalisation of the individual fibres was doomed from the start.