Lenses
Ian Ray. 1st October 2025. Minor changes and merged into TypeToplogy in May 2026. We present the notion of a reflexive graph lens which was introduced by Sterling in "Reflexive graph lenses in univalent foundations" (see index for link).{-# OPTIONS --safe --without-K #-} module ReflexiveGraphs.Lenses where open import MLTT.Spartan open import ReflexiveGraphs.Displayed open import ReflexiveGraphs.DisplayedUnivalent open import ReflexiveGraphs.Type open import ReflexiveGraphs.UnivalentGiven a reflexive graph (A , β) it is useful to consider 'transport' along edges, that is, terms of the form push : (x β y) β P(x) β P(y) where P is a type family (but in full generality we wish P to be a reflexive graph family). For example, we have the following term _ : (X β Y) β is-set X β is-set Y on the reflexive graph (π€ , β). In fact, such a term is recorded in Sets-Properties.lagda, albeit contravariantly, as the term equiv-to-set. Of course, many notions of transport arise in the contravariant form as well. For this reason we wish to also consider terms of the form pull : (x β y) β P(y) β P(x). A lens on a reflexive graph can be thought of as such a generic notion of transport. They will manifest in both the covariant and contravariant forms. Lenses allow for a deeper characterization of many structures that use a personalized notion of transport (which under univalence are equivalent up to homotopy). This fact about uniqueness of transport has been dubbed the fundamental lemma of transport along equivalences by Martin Escardo (see UF/FundamentalLemmaOfTransportAlongEquivalences). TODO. Add the ReflexiveGraphs.Examples file which provides a generalization of this theorem. Just as in the statement of this transport theorem one needs data that details behavior at refl, this data must also be included in the definition of a lens. We present the structure of an oplax covariant lens as a record and then collect the type of oplax covariant lenses as a sigma type. NOTE ON TERMINOLOGY: We follow Sterling's terminological conventions, but we make an attempt to explain them here. The term "lens" has a long history of use in computer science and category theory which we make no attempt to recount here. Sterling borrows the term lens from work on a generalization of fibrations which, rather than satisfying a universal property, are only required to satisfy a unit law. The "oplax/lax" terminology is borrowed from category theory; more precisely the notion of a lax (oplax) monoidal functor. Recall, if a monoidal functor F : C β D is lax then in particular there is a morphism 1_D β F(1_C) (in addition to many other requirements) and oplax if there is a morphism F(1_C) β 1_D. It is the direction of these morphisms which motivates Sterling's use of the terminology. In particular, see the direction of the edges in the push-refl and pull-refl data below.record oplax-covariant-lens-structure (π€' π₯' : Universe) (π : Refl-Graph π€ π₯) (π : β¨ π β© β Refl-Graph π€' π₯') : π€ β π₯ β π€' β π₯' Μ where field push : {x y : β¨ π β©} (p : x ββ¨ π β© y) β β¨ π x β© β β¨ π y β© push-refl : {x : β¨ π β©} (u : β¨ π x β©) β push (β-refl π x) u ββ¨ π x β© u Oplax-Covariant-Lens : (π€' π₯' : Universe) (π : Refl-Graph π€ π₯) β π€ β π₯ β (π€' β π₯')βΊ Μ Oplax-Covariant-Lens π€' π₯' π = Ξ£ π κ (β¨ π β© β Refl-Graph π€' π₯') , oplax-covariant-lens-structure π€' π₯' π πWe name the components of an oplax covariant lens.module _ {π€' π₯' : Universe} {π : Refl-Graph π€ π₯} (π@(π , s) : Oplax-Covariant-Lens π€' π₯' π) where lens-push-graph : β¨ π β© β Refl-Graph π€' π₯' lens-push-graph = π lens-push-fam : β¨ π β© β π€' Μ lens-push-fam x = β¨ lens-push-graph x β© lens-push : {x y : β¨ π β©} (p : x ββ¨ π β© y) β lens-push-fam x β lens-push-fam y lens-push = oplax-covariant-lens-structure.push s lens-push-refl : {x : β¨ π β©} (u : lens-push-fam x) β lens-push (β-refl π x) u ββ¨ lens-push-graph x β© u lens-push-refl = oplax-covariant-lens-structure.push-refl sWe now present a lax contravariant lens.record lax-contravariant-lens-structure (π€' π₯' : Universe) (π : Refl-Graph π€ π₯) (π : β¨ π β© β Refl-Graph π€' π₯') : π€ β π₯ β π€' β π₯' Μ where field pull : {x y : β¨ π β©} (p : x ββ¨ π β© y) β β¨ π y β© β β¨ π x β© pull-refl : {x : β¨ π β©} (u : β¨ π x β©) β u ββ¨ π x β© pull (β-refl π x) u Lax-Contravariant-Lens : (π€' π₯' : Universe) (π : Refl-Graph π€ π₯) β π€ β π₯ β (π€' β π₯')βΊ Μ Lax-Contravariant-Lens π€' π₯' π = Ξ£ π κ (β¨ π β© β Refl-Graph π€' π₯') , lax-contravariant-lens-structure π€' π₯' π πWe name the components of an lax contravariant lens.module _ {π€' π₯' : Universe} {π : Refl-Graph π€ π₯} (π@(π , s) : Lax-Contravariant-Lens π€' π₯' π) where lens-pull-graph : β¨ π β© β Refl-Graph π€' π₯' lens-pull-graph = π lens-pull-fam : β¨ π β© β π€' Μ lens-pull-fam x = β¨ lens-pull-graph x β© lens-pull : {x y : β¨ π β©} (p : x ββ¨ π β© y) β lens-pull-fam y β lens-pull-fam x lens-pull = lax-contravariant-lens-structure.pull s lens-pull-refl : {x : β¨ π β©} (u : lens-pull-fam x) β u ββ¨ lens-pull-graph x β© lens-pull (β-refl π x) u lens-pull-refl = lax-contravariant-lens-structure.pull-refl sWe say an oplax (lax) covariant (contravariant) lens is univalent just when each fiber of the underlying family is univalent.module _ {π€' π₯' : Universe} (π : Refl-Graph π€ π₯) where oplax-covariant-lens-is-univalent : Oplax-Covariant-Lens π€' π₯' π β π€ β π€' β π₯' Μ oplax-covariant-lens-is-univalent π = (x : β¨ π β©) β is-univalent-refl-graph (lens-push-graph π x) lax-contravariant-lens-is-univalent : Lax-Contravariant-Lens π€' π₯' π β π€ β π€' β π₯' Μ lax-contravariant-lens-is-univalent π = (x : β¨ π β©) β is-univalent-refl-graph (lens-pull-graph π x)We now define a display of lenses.display-oplax-covariant-lens : Oplax-Covariant-Lens π€' π₯' π β Displayed-Refl-Graph π€' π₯' π display-oplax-covariant-lens π = (lens-push-fam π , I , II) where I : {x y : β¨ π β©} β x ββ¨ π β© y β lens-push-fam π x β lens-push-fam π y β π₯' Μ I {_} {y} p u v = lens-push π p u ββ¨ lens-push-graph π y β© v II : {x : β¨ π β©} (u : lens-push-fam π x) β I (β-refl π x) u u II u = lens-push-refl π u dispβΊ : Oplax-Covariant-Lens π€' π₯' π β Displayed-Refl-Graph π€' π₯' π dispβΊ π = display-oplax-covariant-lens π display-lax-contravariant-lens : Lax-Contravariant-Lens π€' π₯' π β Displayed-Refl-Graph π€' π₯' π display-lax-contravariant-lens π = (lens-pull-fam π , I , II) where I : {x y : β¨ π β©} β x ββ¨ π β© y β lens-pull-fam π x β lens-pull-fam π y β π₯' Μ I {x} p u v = u ββ¨ lens-pull-graph π x β© lens-pull π p v II : {x : β¨ π β©} (u : β¨ lens-pull-graph π x β©) β I (β-refl π x) u u II u = lens-pull-refl π u dispβ» : Lax-Contravariant-Lens π€' π₯' π β Displayed-Refl-Graph π€' π₯' π dispβ» π = display-lax-contravariant-lens πWe observe the components of the displayed lenses are as we expect.private observation : (π : Oplax-Covariant-Lens π€' π₯' π) β (x : β¨ π β©) β [ dispβΊ π ] x οΌ (βͺ dispβΊ π β« x , (Ξ» u v β u ββ¨ (dispβΊ π) βΈ΄ (β-refl π x) β© v) , β-disp-refl (dispβΊ π)) observation π x = refl observation' : (π : Lax-Contravariant-Lens π€' π₯' π) β (x : β¨ π β©) β [ dispβ» π ] x οΌ (βͺ dispβ» π β« x , (Ξ» u v β u ββ¨ (dispβ» π) βΈ΄ (β-refl π x) β© v) , β-disp-refl (dispβ» π)) observation' π x = reflNow let's consider the description of fans of displayed lenses.compute-fan-of-oplax-covariant-lens : (π : Oplax-Covariant-Lens π€' π₯' π) β (x : β¨ π β©) β (u : βͺ dispβΊ π β« x) β fan ([ dispβΊ π ] x) u οΌ fan (lens-push-graph π x) (lens-push π (β-refl π x) u) compute-fan-of-oplax-covariant-lens π x u = refl compute-cofan-of-lax-contravariant-lens : (π : Lax-Contravariant-Lens π€' π₯' π) β (x : β¨ π β©) β (u : βͺ dispβ» π β« x) β cofan ([ dispβ» π ] x) u οΌ cofan (lens-pull-graph π x) (lens-pull π (β-refl π x) u) compute-cofan-of-lax-contravariant-lens π x u = reflWe now show that if each fiber of a lens is univalent then the displayed reflexive graph is univalent. The previous observation should provide some insight into the form of the following proof terms.disp-oplax-covariant-lens-univalent : (π : Oplax-Covariant-Lens π€' π₯' π) β ((x : β¨ π β©) β is-univalent-refl-graph (lens-push-graph π x)) β is-displayed-univalent-refl-graph π (dispβΊ π) disp-oplax-covariant-lens-univalent π fibers-ua x u = fibers-ua x (lens-push π (β-refl π x) u) disp-lax-contravariant-lens-univalent : (π : Lax-Contravariant-Lens π€' π₯' π) β ((x : β¨ π β©) β is-univalent-refl-graph (lens-pull-graph π x)) β is-displayed-univalent-refl-graph π (dispβ» π) disp-lax-contravariant-lens-univalent π fibers-ua x = prop-cofan-to-fan ([ dispβ» π ] x) ((Ξ» - β fibers-co-ua (lens-pull π (β-refl π x) -))) where fibers-co-ua = prop-fan-to-cofan (lens-pull-graph π x) (fibers-ua x)