aa r X i v : . [ m a t h . C T ] F e b Generalized Lens Categories via Functors C op → Cat
David I. Spivak ∗ Abstract
Lenses have a rich history and have recently received a great deal of at-tention from applied category theorists. We generalize the notion of lens bydefining a category
Lens F for any category C and functor F : C op → Cat , us-ing a variant of the Grothendieck construction. All of the mathematics inthis note is straightforward; the purpose is simply to see lenses in a broadercontext where some closely-related examples, such as ringed spaces and opencontinuous dynamical systems, can be included.
Roughly speaking, a lens is bi-directional map (cid:0) getput (cid:1) : ( cx ) → (cid:0) dy (cid:1) between pairs;the two parts have the following form: get : c → d and put : c × y → x. (1)Lenses have recently received a great deal of attention from applied category the-orists. One reason is that they show up in many disparate places, such as databaseupdates, learning algorithms, open games, open dynamical systems, and wiringdiagrams. Lenses have been broadly generalized to so-called profunctor optics; seee.g. [Ril18].We will discuss what seems to be a completely different direction of general-ization: we associate a notion of generalized lens category to an arbitrary indexedcategory. Namely for every category C and (pseudo-) functor F : C op → Cat , wedefine a category
Lens F using a variant of the Grothendieck construction. The ideais that a morphism in the Grothendieck construction consists of two parts, whichturn out to be the above get and put maps from lens theory. Taking C = Set ,we can recover the usual category of lenses in a couple of ways (see Example 3.5 ∗ This work supported by Honeywell Inc. as well as AFOSR grants FA9550-17-1-0058 and FA9550-19-1-0113. F to be the slice category functor Slice : Set op → Cat , embed each ( cx ) as the projection π c : c × x → c in Slice ( c ) ,and note that for any choice of function get : c → d , the square shown below is apullback: c × x c × y d × yc d π c y put π d get (2)The function indicated as put does not have quite the same form as in Eq. (1): thereis an extra factor of c in the codomain. However, to be a morphism in the slicecategory Slice ( c ) , such a function c × y → c × x in Slice ( c ) must commute with theprojections. Thus it has no choice with regards to the c -factor in the codomain,and hence the only remaining choice is that of a function put : c × y → x , thusrecovering the notion of morphism ( cx ) → (cid:0) dy (cid:1) from Eq. (1). This idea, to think ofthe an object ( cx ) not as a simple pair but as a dependent pair ( x dependent on c ), isthe main thrust of this note.All of the results we discuss here are straightforward to prove. The proposedcontribution is to provide a setting in which open continuous dynamical systems,ringed spaces, and dependent lenses—none of which fit in with the usual definitionof lens but all of which seem to be quite similar to it in spirit—can be included inthe theory. We also provide a construction of lenses in an arbitrary symmetricmonoidal category, which is known but seems not to have been written downexplicitly before. Acknowledgements.
Thanks to Brendan Fong, Bruno Gavranović, David A. Dal-rymple, Sophie Libkind, Eliana Lorch, and Toby St Clere Smithe for inspiringconversations. Special thanks to David Jaz Myers for Section 2.2—which is entirelydue to him—as well as other useful comments and suggestions, and to ChristinaVasilakopoulou for the observation that wiring diagrams in [VSL15; SSV16] areprisms (Example 2.4). Thanks also to Bruno Gavranović and Sophie Libkind for acareful reading, and to Jules Hedges for several references and helpful comments.
Lenses have been studied in computer science and discussed category-theoreticallyfor several decades [Ole83; Pai89; BPV06; Dis08; JRW12]. There are several variants,and the naming is often inconsistent. A good summary can be found in thisblog post by Jules Hedges; see also the Haskell Lens library. We will be discussingwhat Hedges calls bimorphic lenses in [Hed17], but we will refer to them simplyas lenses . We begin by recalling this notion.2 .1 Lenses in finite product categories
We begin with lenses in a category C with finite products; one may think C = Set .In Section 2.2 we generalize to an arbitrary symmetric monoidal category, and thenmuch further in Section 3.
Notation 2.1.
We denote the composite of f : c → d and g : d → e by ( f g ) : c → e .We denote the identity morphism on an object c either by id c or simply by c . Wedenote the hom-set from c to d in a category C either by Hom( c, d ) , Hom C ( c, d ) , or C ( c, d ) . Definition 2.2 (Lenses in finite product categories) . Let C be a category with finiteproducts. The category of C -lenses , denoted Lens C , × has as objects pairs ( cx ) , where c, x ∈ C ; given another such object (cid:0) dy (cid:1) we define the homset Lens C , × (cid:0) ( cx ) , (cid:0) dy (cid:1)(cid:1) := n(cid:16) ff ♯ (cid:17) (cid:12)(cid:12)(cid:12) f : c → d and f ♯ : c × y → x o . We refer to the C -morphisms f : c → d as the get part and f ♯ : c × y → x as the the put part of the lens (cid:16) ff ♯ (cid:17) .The identity on ( cx ) is (cid:16) id c ǫ c × id x (cid:17) = ( cǫ c × x ) , where ǫ c : c → is the terminal map.The composite of (cid:16) ff ♯ (cid:17) : ( cx ) → (cid:0) dy (cid:1) and (cid:16) gg ♯ (cid:17) : (cid:0) dy (cid:1) → ( ez ) is (cid:16) f g ( δ c × z ) ( c × f × z ) ( c × g ♯ ) f ♯ (cid:17) ,where δ c : c → c × c is the diagonal. In pictures, it is given by the following stringdiagram: f gc e d c f g ♯d f ♯y z x (3) Example . In functional programming the emphasis has oftenbeen on what we call simple lenses [BPV06], which are lenses of the form ( cc ) . Amorphism of simple lenses ( cc ) → (cid:0) dd (cid:1) consists of a function f : c → d and a function f ♯ : c × d → c . Example . If C has finite coproducts then C op has finiteproducts. The category ( Lens C op , + ) op is called the category of prisms in C . A prism ( cx ) → (cid:0) dy (cid:1) consists of a pair of morphisms d → c and morphism x → c + y in C .The category ( Lens
FinSet op , + ) op of prisms in FinSet , or more generally replacing
FinSet by FinSet /T for some set T , is called the category of wiring diagrams in[VSL15]. When T = 1 this forms the left class of a factorization system on thecategory Cob of 1-dimensional oriented cobordisms, and similarly for arbitrary T (using cobordisms with components labeled in T ). See [Aba15]. Example . Given a pair of sets ( A, B ) , a Moore machine (alsocalled an open discrete dynamical system in [Spi15]) consists of a set S and twofunctions f out : S → B and f upd : A × S → S . This is the same as a lens (cid:0) SS (cid:1) → (cid:0) AB (cid:1) .3he notion of dynamical system (and the formula for composing them withwiring diagrams as in Example 2.4) can be generalized to the continuous case, withmanifolds replacing the sets and systems of ordinary differential equations replac-ing the update functions. However, the theory of lenses does not accommodatethis generalization. Remedying this lack was in part the motivation for the presentnote; see Example 3.6. We owe the ideas of this section to David Jaz Myers, though these ideas are appar-ently folklore. For something similar, see [Abo+16, Section 2.2].
Definition 2.6 (Commutative comonoid) . Let ( C , I, ⊗ , σ ) be a symmetric monoidalcategory. A commutative comonoid in C consists of a tuple ( c, ǫ, δ ) where c ∈ C , ǫ : c → I and δ : c → c ⊗ c ) satisfy the axioms:1. δ c ( c ⊗ ǫ c ) = c ,2. δ c σ c,c = δ c , and3. δ c ( δ c ⊗ c ) = δ c ( c ⊗ δ c ) .We refer to ǫ as the counit and δ as the comultiplication . We sometimes write c todenote the comonoid, leaving ǫ and δ implicit.A morphism of commutative comonoids ( c, ǫ c , δ c ) → ( d, ǫ d , δ d ) is a morphism f : c → d in C such that ǫ c = f ǫ d and δ c ( f ⊗ f ) = f δ d . We denote the categoryof commutative comonoids and their morphisms by Comon C . Proposition 2.7 (Finite product categories) . If C has finite products and ( I, ⊗ ) is thecorresponding (“Cartesian”) monoidal structure, then there is an isomorphism of categories C ∼ = Comon C .Proof. See [Fox76].The following is straightforward.
Proposition 2.8.
There is a symmetric monoidal structure on
Comon C such that the functor Comon C → C is strict monoidal. Definition 2.9.
Let ( C , I, ⊗ , σ ) be a symmetric monoidal category. The category of C -lenses , denoted Lens C , ⊗ has as objects pairs ( cx ) , where c ∈ Comon C and x ∈ C .Given another such object (cid:0) dy (cid:1) we define the homset Lens C , ⊗ (cid:0) ( cx ) , (cid:0) dy (cid:1)(cid:1) := n(cid:16) ff ♯ (cid:17) (cid:12)(cid:12)(cid:12) f ∈ Comon C ( c, d ) and f ♯ ∈ C ( c ⊗ y, d ) o . We refer to the comonoid morphism f : c → d as the get part and the map f ♯ : c ⊗ y → d as the put part of the lens (cid:16) ff ♯ (cid:17) . 4he identity on ( cx ) is ( cǫ c ⊗ x ) , where ǫ c : c → is the counit. The composite of (cid:16) ff ♯ (cid:17) and (cid:16) gg ♯ (cid:17) : (cid:0) dy (cid:1) → ( ez ) is (cid:16) f g ( δ c ⊗ z ) ( c ⊗ f ⊗ z ) ( c ⊗ g ♯ ) f ♯ (cid:17) , where δ c : c → c ⊗ c is thecomultiplication. The string diagram for the composite is identical to that in Eq. (3).Definition 2.9 generalizes Definition 2.2, by Proposition 2.7. We define the lens category
Lens F for any functor F : C op → Cat and then giveseveral examples.
Lens F The Grothendieck construction comes in two variants.
Definition 3.1 (Grothendieck constructions) . Let C be a category and F : C → Cat .The covariant Grothendieck construction of F consists of a category Gr ( F ) and a functor π F : Gr ( F ) → C , defined as follows. Ob( Gr ( F )) := G c ∈ Ob( C ) Ob( F ( c )) Gr ( F ) (cid:0) ( c, x ) , ( d, y ) (cid:1) := G f ∈ C ( c,d ) Hom F ( d ) (cid:0) F ( f )( x ) , y (cid:1) That is, an object in Gr ( F ) is a pair ( c, x ) where c ∈ C and x ∈ F ( c ) . A morphism ( c, x ) → ( d, y ) is a pair ( f, f ♯ ) where f : c → d and f ♯ : F ( f )( x ) → y is a morphismin the category F ( d ) . The identity on ( c, x ) is (id c , id x ) , and the composite of ( f, f ♯ ) and ( g, g ♯ ) is given by ( f, f ♯ ) ( g, g ♯ ) := (cid:16) ( f g ) , (cid:0) F ( g )( f ♯ ) g ♯ (cid:1)(cid:17) . The functor π F : Gr ( F ) → C sends ( c, x ) c and ( f, f ♯ ) f .Given a functor F : C op → Cat , the contravariant Grothendieck construction of F consists of a category Gr o ( F ) and a functor π F : Gr o ( F ) → C , defined as follows: Ob( Gr o ( F )) := G c ∈ Ob( C ) Ob( F ( c )) Gr o ( F ) (cid:0) ( c, x ) , ( d, y ) (cid:1) := G f ∈ C ( c,d ) Hom F ( c ) (cid:0) x, F ( f )( y ) (cid:1) Identities, composition, and the functor π F are defined analogously.For any functor F : C → Cat , let F p : C op → Cat denote its pointwise opposite, F p ( c ) := F ( c ) op . The following is straightforward.5 roposition 3.2. Let F : C op → Cat be a functor, and let F p : C op → Cat be its pointwiseopposite. The following three categories are naturally isomorphic:1. Gr ( F ) op , the opposite of the covariant Grothendieck construction of F ,2. Gr o ( F p ) , the contravariant Grothendieck construction of the pointwise opposite of F ,3. the analogous category with objects F c ∈ Ob( C ) Ob( F ( c )) and morphisms Hom(( c, x ) , ( d, y )) := G f ∈ C ( c,d ) Hom F ( c ) (cid:0) F ( f )( y ) , x (cid:1) . (4) Moreover, these isomorphisms commute with the functors π F to C . Definition 3.3 ( F -lenses) . Let F : C op → Cat be a functor. Define the category of F -lenses , denoted Lens F , to be any of the three isomorphic categories from Propo-sition 3.2. We refer to C as the get-category and to π F : Lens F → C as the get-functor .We denote the object ( c, x ) by ( cx ) . From the explicit formula (4), we see that amorphism (cid:16) ff ♯ (cid:17) : ( cx ) → (cid:0) dy (cid:1) consists of a pair ( f, f ♯ ) where f : c → d is in C and f ♯ : F ( f )( y ) → x is a morphism in the category F ( c ) .With this definition, lenses have a diagrammatically simpler form than in Eq. (3).Here we show morphisms (cid:16) ff ♯ (cid:17) : ( cx ) → (cid:0) dy (cid:1) and (cid:16) gg ♯ (cid:17) : (cid:0) dy (cid:1) → ( ez ) : f y c d x cf ♯ = ⇒ g z d e y dg ♯ = ⇒ The wires represent objects c, d, e ∈ C , and white the squares represent morphismsin C . The blue circles and double-arrows represent objects and morphisms in thecategories F ( c ) , etc. Here is a picture of the composite (cid:16) ff ♯ (cid:17) (cid:16) gg ♯ (cid:17) : f g z d ec f y c d x cg ♯ = ⇒ f ♯ = ⇒ One may also imagine these morphisms logically, e.g. the implications ∀ c. y ( f ( c )) ⇒ x ( c ) ∀ d. z ( g ( d )) ⇒ y ( d ) which can be combined to obtain ∀ c. z ( f ( g ( c ))) ⇒ x ( c ) . Remark . The Grothendieck construction of a
Cat -valued functor F : C op → Cat always yields a (split) fibration over C and vice versa, so generalized lens categoriescan be viewed simply as split fibrations. However we chose Lens F to be the fiberwiseopposite of Gr ( F ) —rather than replacing F with F p at the outset—for two reasons.First, in cases of interest F seems to be simpler to specify than F p . Second, the formof (4) is the one that is most familiar in lens theory.6 xample . Let C be a category with pullbacks. There is afunctor Slice : C op → Cat given on an object c by the slice category Slice ( c ) := C /c over c , and on morphisms f : c ❀ b in C op by pullback. That is, for every object p : x → c in Slice ( c ) we obtain an object Slice ( f )( p ) ∈ C /b using the followingpullback diagram in C : b × c x xb c Slice ( f )( p ) y pf This extends to morphisms in
Slice ( c ) using the universal property of pullbacks.The category Lens
Slice has as objects pairs ( cp ) where p : x → c , and as morphismspairs (cid:16) ff ♯ (cid:17) where f : c → d and f ♯ : c × d y → x . We can think of objects in Lens
Slice as dependent lenses; for example if C = Set , then each object p : x → c may assignnon-isomorphic fibers to different elements of c .Note that we can find the category of lenses from Definition 2.2 inside of Lens F .Indeed, it is isomorphic to the full subcategory spanned by all pairs ( cπ ) for which π : c × x → c is the projection for some x ∈ C . This was discussed in the introductionaround Eq. (2). We will recover the category of lenses in a completely different wayin Proposition 3.10.More importantly, the get functor π F : Lens
Slice → C is not only a fibration buta bifibration. Indeed, the functor Slice ( f ) : Slice ( d ) → Slice ( c ) has a left adjoint Σ f : Slice ( c ) → Slice ( d ) , which one may call the dependent sum along f , for anymorphism f : c → d in C . The name “dependent lens” is actually most appropriatewhen C not only has pullbacks but is locally cartesian closed. This simply meansthat each Slice ( f ) additionally has a right adjoint Π f : Slice ( c ) → Slice ( d ) , called the dependent product along f . In this case π F is a trifibration. Example . Recall that a differentiable man-ifold M has a tangent bundle T M and a submersion π M : T M → M . Given a pairof manifolds ( A, B ) , [VSL15] defines an open continuous dynamical system withinputs A and outputs B to be a manifold S (called the state space), a differen-tiable map f out : S → B , and a differentiable map f dyn : A × S → T S such that f dyn π S = π . We can see this as a morphism in a generalized lens category asfollows.Consider the functor Subm : Mfd op → Cat sending each manifold M to thecategory of submersions over M , and sending a differentiable map f : M → N tothe pullback functor along f . Then an open continuous dynamical system withinputs A and outputs B consists of a morphism (cid:0) ST S (cid:1) → (cid:0) Bπ B (cid:1) in Lens
Subm , where π B : A × B → B is the projection. [VSL15] shows that continuous dynamicalsystems can be wired together using prisms, as in Example 2.4.7 xample . Consider again the lens category for the functor
Subm : Mfd op → Cat , as in Example 3.6. Whereas in that example we consideredobjects given by tangent bundles and found that certain morphisms between themwere continuous dynamical systems, here we consider objects given by cotangentbundles and find that we get lenses between them canonically, without makingchoices.To begin, note that a differentiable manifold M also has a cotangent bundle T ∗ M ; its fiber over each point m ∈ M is the dual to the tangent space there, i.e. itis the vector space T ∗ m M := Vect ( T m M, R ) of linear maps from the tangent spaceto the ground field R . Given a differentiable function f : M → N , the derivative(Jacobian) defines a map T f : T M → T N , in particular over each point m ∈ M alinear transformation T m M → T f ( m ) N . This in turn induces a linear transformation Vect ( T f ( m ) N, R ) → Vect ( T m M, R ) for each point m , and these assemble into a mor-phism f ♯ : f ∗ ( T ∗ N ) → T ∗ M of bundles over M . All together we have canonicallyobtained a morphism (cid:16) ff ♯ (cid:17) : (cid:0) MT ∗ M (cid:1) → (cid:0) NT ∗ N (cid:1) in the lens category Lens
Subm , andhence a functor
Mfd → Lens
Subm . Example . The category of ringed spaces from algebraic geometryis an example of a generalized lens category. There is a functor Sh : Top op → Cat ,where
Top is the category of topological spaces and Sh ( X ) is the category of sheavesof rings on X ; given a map f : X → Y in Top , there is a functor f ∗ which sends asheaf on Y to a sheaf on X , hence defining Sh on morphisms.The category Lens Sh of Sh -lenses has as objects pairs (cid:0) X O X (cid:1) where O X is a sheafof rings on X . A morphism (cid:0) X O X (cid:1) → (cid:0) Y O Y (cid:1) is a pair ( f, f ♯ ) where f : X → Y is amap of topological spaces and f ♯ : f ∗ O Y → O X is a map of sheaves of rings. Example . Let C be a category and consider the coslicefunctor Coslice : C op → Cat sending c c/ C ; on morphisms f : d → c the functor Coslice ( f ) sends an object x : c → d ∈ Coslice ( c ) to the composite f x . The category Lens
Coslice is the twisted arrow category tw ( C ) of C . An object ( cx ) in Lens
Coslice is amorphism x : c → d , and a morphism ( cx ) → (cid:0) dy (cid:1) in Lens
Coslice is a twisted square c dd d ′ fx yf ♯ Let ( C , I, ⊗ ) be a symmetric monoidal category, and let Comon C denote its (sym-metric monoidal) category of commutative comonoids (see Definition 2.6). Foreach object c ∈ Comon C there is a comonad on C given by x c ⊗ x ; the counit is8iven by ǫ c ⊗ x and the comultiplication is given by δ c ⊗ x , maps which are naturalin x . Forming the coKleisli category gives a functor coKl C : Comon op C → Cat . Let’sunpack this.The functor coKl C : Comon op C → Cat has the following more explicit formulation.Given an commutative comonoid ( c, ǫ, δ ) , the coKleisli category coKl C ( c ) has objects Ob( C ) and morphisms Hom( x, y ) := C ( c ⊗ x, y ) . The identity on x is given by ( ǫ c ⊗ x ) : c ⊗ x → x , and the composite of f : c ⊗ x → y and g : c ⊗ y → z is given by ( f g ) = ( δ c ⊗ x ) ( c ⊗ f ) g . In pictures: f g Given a morphism of comonoids p : c → d in Comon C , we obtain an identity-on-objects functor coKl C ( p ) : coKl C ( d ) → coKl C ( c ) that sends the morphism d ⊗ x → y to its composite with ( p ⊗ x ) : ( c ⊗ x ) → ( d ⊗ x ) . Proposition 3.10.
Let C be symmetric monoidal and let coKl : Comon op C → Cat be as inSection 3.2. The generalized lens category
Lens coKl is isomorphic to the category
Lens C , ⊗ from Definition 2.9.Proof. The objects of both
Lens coKl and
Lens C , ⊗ are pairs ( cx ) , where c ∈ Comon C and x ∈ Ob( coKl ( c )) = Ob C .The morphisms ( cx ) → (cid:0) dy (cid:1) in the latter are pairs ( f, f ♯ ) where f : c → d is a morphism of comonoids and f ♯ : c ⊗ y → x is any morphism. In the for-mer, the morphisms are pairs ( f, f ♯ ) where f : c → d is a map of comonoids and f ♯ : coKl ( f )( y ) → x is a map in coKl ( c ) . Since coKl ( f ) is identity on objects, f ♯ is amorphism c ⊗ y → x in C , so again the morphisms in the two categories coincide.One may check that the identities and composition formulas also coincide. F is monoidal, so is Lens F Definition 3.11 (Monoidal Grothendieck construction) . Let ( C , I, ⊗ ) be a monoidalcategory and ( F, ϕ ) : C → Cat a lax monoidal functor. The monoidal Grothendieckconstruction [MV18] returns a monoidal structure on the category Gr ( F ) , and henceon Lens F = Gr ( F ) op , by ( cx ) ⊗ (cid:0) dy (cid:1) := (cid:16) c ⊗ dϕ ( x,y ) (cid:17) . Example . The functors coKl C : C → Cat for arbitrary symmetric monoidal C from Section 3.2, as well as Subm : Mfd op → Cat and Sh : Top op → Cat from Ex-amples 3.6 and 3.8 are all lax monoidal. Thus each of the lens categories
Lens coKl , Lens
Subm , and
Lens Sh , inherit symmetric monoidal structures. From the first andthird examples we recover the usual monoidal structure on the usual category oflenses in a symmetric monoidal category, as well as that on ringed spaces.9 eferences [Aba15] Joseph Abadi. On the Existence of an Orthogonal Factorization System on1-Cob and 2-Cob . 2015. eprint: arXiv:1506.03119 .[Abo+16] Faris Abou-Saleh, James Cheney, Jeremy Gibbons, James McKinna, andPerdita Stevens. “Reflections on monadic lenses”. In:
A List of Successesthat can Change the World . Springer, 2016, pp. 1–31.[BPV06] Aaron Bohannon, Benjamin C Pierce, and Jeffrey A Vaughan. “Rela-tional lenses: a language for updatable views”. In:
Proceedings of thetwenty-fifth ACM SIGMOD-SIGACT-SIGART symposium on Principles ofdatabase systems . ACM. 2006, pp. 338–347.[Dis08] Zinovy Diskin. “Algebraic models for bidirectional model synchroniza-tion”. In:
International Conference on Model Driven Engineering Languagesand Systems . Springer. 2008, pp. 21–36.[Fox76] Thomas Fox. “Coalgebras and Cartesian categories”. In:
Comm. Algebra
Coherence for lenses and open games . 2017. eprint: arXiv:1704.02230 .[JRW12] Michael Johnson, Robert Rosebrugh, and Richard J Wood. “Lenses, fi-brations and universal translations”. In:
Mathematical Structures in Com-puter Science preprint (2018). arXiv: .[Ole83] Frank Joseph Oles. “A Category-theoretic approach to the semantics ofprogramming languages”. PhD thesis. Syracuse University, 1983.[Pai89] Valeria de Paiva. “The dialectica categories”. In:
Categories in ComputerScience and Logic
92 (1989), pp. 47–62.[Ril18] Mitchell Riley.
Categories of Optics . 2018. eprint: arXiv:1809.00738 .[Spi15] David I. Spivak.
The steady states of coupled dynamical systems composeaccording to matrix arithmetic . 2015. eprint: arXiv:1512.00802 .[SSV16] Patrick Schultz, David I Spivak, and Christina Vasilakopoulou. “Dy-namical systems and sheaves”. In:
Applied Categorical Structures (2016),pp. 1–57.[VSL15] Dmitry Vagner, David I. Spivak, and Eugene Lerman. “Algebras of opendynamical systems on the operad of wiring diagrams”. In: