Martin Escardo, August 2018. Set quotients in univalent mathematics in Agda notation. This took place during the Dagstuhl meeting "Formalization of Mathematics in Type Theory", because Dan Grayson wanted to see how universe levels work in Agda and I thought that this would be a nice example to illustrate that. We assume, in addition to Spartan Martin-LΓΆf type theory, * function extensionality (any two pointwise equal functions are equal), * propositional extensionality (any two logically equivalent propositions are equal), * propositional truncation (any type can be universally mapped into a prop in the same universe), and no resizing axioms. The K axiom is not used (the without-K option below). We also make sure pattern matching corresponds to Martin-LΓΆf eliminators, using the option exact-split. With the option safe we make sure that nothing is postulated - any non-MLTT axiom has to be an explicit assumption (argument to a function or a module). \begin{code} {-# OPTIONS --without-K --exact-split --safe --no-sized-types --no-guardedness --auto-inline #-} open import MLTT.Spartan open import UF.FunExt open import UF.PropTrunc open import UF.Base hiding (_β_) open import UF.Subsingletons open import UF.Subsingletons-FunExt open import UF.Equiv module UF.Large-Quotient (pt : propositional-truncations-exist) (fe : Fun-Ext) (pe : Prop-Ext) where \end{code} We define when a relation is subsingleton (or proposition) valued, reflexive, transitive or an equivalence. What is noteworthy, for the purpose of explaining universes in Agda to Dan, is that X is in a universe π€, and the value of the relation is in a universe π₯, where π€ and π₯ are arbitrary. (NB. The Agda library uses the word "Level" for universes, and then what we write "π€ Μ" here is written "Set π€". This is not good for univalent mathematics, because the types in π€ Μ need not be sets, and also because it places emphasis on levels rather than universes themselves.) Then, for example, the function is-prop-valued defined below takes values in the least upper bound of π€ and π₯, which is denoted by π€ β π₯. We first define the type of five functions and then define them, where _β_ is a variable: \begin{code} is-prop-valued is-equiv-relation : {X : π€ Μ } β (X β X β π₯ Μ ) β π€ β π₯ Μ is-prop-valued _β_ = β x y β is-prop (x β y) is-equiv-relation _β_ = is-prop-valued _β_ Γ reflexive _β_ Γ symmetric _β_ Γ transitive _β_ \end{code} Now, using an anonymous module UF.with parameters (corresponding to a section in Coq), we assume propositional truncations that stay in the same universe, function extensionality for all universes, two universes π€ and π₯, propositional truncation for the universe π₯, a type X : π€ Μ, and an equivalence relation _β_ with values in π₯ Μ. \begin{code} module quotient {π€ π₯ : Universe} (X : π€ Μ ) (_β_ : X β X β π₯ Μ ) (βp : is-prop-valued _β_) (βr : reflexive _β_) (βs : symmetric _β_) (βt : transitive _β_) where open PropositionalTruncation pt open import UF.ImageAndSurjection pt \end{code} Now, Ξ© π₯ is the type of subsingletons, or (univalent) propositions, or h-propositions, or mere propositions, in the universe π₯, which lives in the next universe π₯ βΊ. From the relation _β_ : X β (X β π₯ Μ ) we define a relation X β (X β Ξ© π₯), which of course is formally a function. We then take the quotient X/β to be the image of this function. Of course, it is for constructing the image that we need propositional truncations. \begin{code} equiv-rel : X β (X β Ξ© π₯) equiv-rel x y = x β y , βp x y \end{code} Then the quotient lives in the least upper bound of π€ and π₯ βΊ, where π₯ βΊ is the successor of the universe π₯: \begin{code} X/β : π€ β (π₯ βΊ) Μ X/β = image equiv-rel X/β-is-set : is-set X/β X/β-is-set = subsets-of-sets-are-sets (X β Ξ© π₯) _ (powersets-are-sets'' fe fe pe) β₯β₯-is-prop Ξ· : X β X/β Ξ· = corestriction equiv-rel \end{code} Then Ξ· is the universal solution to the problem of transforming equivalence _β_ into equality _οΌ_ (identity type). By construction, Ξ· is a surjection, of course: \begin{code} Ξ·-surjection : is-surjection Ξ· Ξ·-surjection = corestrictions-are-surjections equiv-rel \end{code} It is convenient to use the following induction principle for reasoning about the image. Notice that the property we consider has values in any universe π¦ we please: \begin{code} quotient-induction : β {π¦} (P : X/β β π¦ Μ ) β ((x' : X/β) β is-prop (P x')) β ((x : X) β P (Ξ· x)) β (x' : X/β) β P x' quotient-induction = surjection-induction Ξ· Ξ·-surjection \end{code} The first part of the universal property of Ξ· says that equivalent points are mapped to equal points: \begin{code} Ξ·-equiv-equal : {x y : X} β x β y β Ξ· x οΌ Ξ· y Ξ·-equiv-equal {x} {y} e = to-Ξ£-οΌ (dfunext fe (Ξ» z β to-Ξ£-οΌ (pe (βp x z) (βp y z) (βt y x z (βs x y e)) (βt x y z e) , being-prop-is-prop fe _ _)) , β₯β₯-is-prop _ _) \end{code} We also need the fact that Ξ· reflects equality into equivalence: \begin{code} Ξ·-equal-equiv : {x y : X} β Ξ· x οΌ Ξ· y β x β y Ξ·-equal-equiv {x} {y} p = equiv-rel-reflect (ap prβ p) where equiv-rel-reflect : equiv-rel x οΌ equiv-rel y β x β y equiv-rel-reflect q = b (βr y) where a : (y β y) οΌ (x β y) a = ap (Ξ» - β prβ (- y)) (q β»ΒΉ) b : (y β y) β (x β y) b = Idtofun a \end{code} We are now ready to formulate and prove the universal property of the quotient. What is noteworthy here, regarding universes, is that the universal property says that we can eliminate into any set A of any universe π¦. Ξ· X ------> X/β \ . \ . f \ . f' \ . v A \begin{code} universal-property : β {π¦} (A : π¦ Μ ) β is-set A β (f : X β A) β ({x x' : X} β x β x' β f x οΌ f x') β β! f' κ ( X/β β A), f' β Ξ· οΌ f universal-property {π¦} A iss f pr = ic where Ο : (x' : X/β) β is-prop (Ξ£ a κ A , β x κ X , (Ξ· x οΌ x') Γ (f x οΌ a)) Ο = quotient-induction _ Ξ³ induction-step where induction-step : (y : X) β is-prop (Ξ£ a κ A , β x κ X , (Ξ· x οΌ Ξ· y) Γ (f x οΌ a)) induction-step x (a , d) (b , e) = to-Ξ£-οΌ (p , β₯β₯-is-prop _ _) where h : (Ξ£ x' κ X , (Ξ· x' οΌ Ξ· x) Γ (f x' οΌ a)) β (Ξ£ y' κ X , (Ξ· y' οΌ Ξ· x) Γ (f y' οΌ b)) β a οΌ b h (x' , r , s) (y' , t , u) = s β»ΒΉ β pr (Ξ·-equal-equiv (r β t β»ΒΉ)) β u p : a οΌ b p = β₯β₯-rec iss (Ξ» Ο β β₯β₯-rec iss (h Ο) e) d Ξ³ : (x' : X/β) β is-prop (is-prop (Ξ£ a κ A , β x κ X , (Ξ· x οΌ x') Γ (f x οΌ a))) Ξ³ x' = being-prop-is-prop fe k : (x' : X/β) β Ξ£ a κ A , β x κ X , (Ξ· x οΌ x') Γ (f x οΌ a) k = quotient-induction _ Ο induction-step where induction-step : (y : X) β Ξ£ a κ A , β x κ X , (Ξ· x οΌ Ξ· y) Γ (f x οΌ a) induction-step x = f x , β£ x , refl , refl β£ f' : X/β β A f' x' = prβ (k x') r : f' β Ξ· οΌ f r = dfunext fe h where g : (y : X) β β x κ X , (Ξ· x οΌ Ξ· y) Γ (f x οΌ f' (Ξ· y)) g y = prβ (k (Ξ· y)) j : (y : X) β (Ξ£ x κ X , (Ξ· x οΌ Ξ· y) Γ (f x οΌ f' (Ξ· y))) β f' (Ξ· y) οΌ f y j y (x , p , q) = q β»ΒΉ β pr (Ξ·-equal-equiv p) h : (y : X) β f' (Ξ· y) οΌ f y h y = β₯β₯-rec iss (j y) (g y) c : (Ο : Ξ£ f'' κ (X/β β A), f'' β Ξ· οΌ f) β (f' , r) οΌ Ο c (f'' , s) = to-Ξ£-οΌ (t , v) where w : β x β f' (Ξ· x) οΌ f'' (Ξ· x) w = happly (r β s β»ΒΉ) t : f' οΌ f'' t = dfunext fe (quotient-induction _ (Ξ» _ β iss) w) u : f'' β Ξ· οΌ f u = transport (Ξ» - β - β Ξ· οΌ f) t r v : u οΌ s v = Ξ -is-set fe (Ξ» _ β iss) u s ic : β! f' κ (X/β β A), f' β Ξ· οΌ f ic = (f' , r) , c \end{code} Added 11th February 2021. We now repackage the above for convenient use: \begin{code} module _ {π€ π₯ : Universe} where open quotient open import UF.ImageAndSurjection pt EqRel : π€ Μ β π€ β (π₯ βΊ) Μ EqRel X = Ξ£ R κ (X β X β π₯ Μ ) , is-equiv-relation R _β[_]_ : {X : π€ Μ } β X β EqRel X β X β π₯ Μ x β[ _β_ , _ ] y = x β y _/_ : (X : π€ Μ ) β EqRel X β π€ β (π₯ βΊ) Μ X / (_β_ , p , r , s , t) = X/β X _β_ p r s t module _ {X : π€ Μ } ((_β_ , βp , βr , βs , βt) : EqRel X) where private β : EqRel X β = (_β_ , βp , βr , βs , βt) quotient-is-set : is-set (X / β) quotient-is-set = X/β-is-set _ _β_ βp βr βs βt Ξ·/ : X β X / β Ξ·/ = Ξ· X _β_ βp βr βs βt Ξ·/-is-surjection : is-surjection Ξ·/ Ξ·/-is-surjection = Ξ·-surjection X _β_ βp βr βs βt /-induction : β {π¦} (P : X / β β π¦ Μ ) β ((x' : X / β) β is-prop (P x')) β ((x : X) β P (Ξ·/ x)) β (x' : X / β) β P x' /-induction = surjection-induction Ξ·/ Ξ·/-is-surjection /-induction' : β {π¦} {P : X / β β π¦ Μ } β ((x' : X / β) β is-prop (P x')) β ((x : X) β P (Ξ·/ x)) β (x' : X / β) β P x' /-induction' {π¦} {P} = surjection-induction Ξ·/ Ξ·/-is-surjection P identifies-related-points : {A : π¦ Μ } β (X β A) β π€ β π₯ β π¦ Μ identifies-related-points f = β {x x'} β x β x' β f x οΌ f x' Ξ·/-identifies-related-points : identifies-related-points Ξ·/ Ξ·/-identifies-related-points = Ξ·-equiv-equal X _β_ βp βr βs βt Ξ·/-relates-identified-points : {x y : X} β Ξ·/ x οΌ Ξ·/ y β x β y Ξ·/-relates-identified-points = Ξ·-equal-equiv X _β_ βp βr βs βt module _ {π¦ : Universe} {A : π¦ Μ } where abstract universal-property/ : is-set A β (f : X β A) β identifies-related-points f β β! f' κ (X / β β A), f' β Ξ·/ οΌ f universal-property/ = universal-property X _β_ βp βr βs βt A mediating-map/ : is-set A β (f : X β A) β identifies-related-points f β X / β β A mediating-map/ i f p = prβ (center (universal-property/ i f p)) universality-triangle/οΌ : (i : is-set A) (f : X β A) (p : identifies-related-points f) β mediating-map/ i f p β Ξ·/ οΌ f universality-triangle/οΌ i f p = prβ (center (universal-property/ i f p)) universality-triangle/ : (i : is-set A) (f : X β A) (p : identifies-related-points f) β mediating-map/ i f p β Ξ·/ βΌ f universality-triangle/ i f p = happly (universality-triangle/οΌ i f p) at-most-one-mediating-map/οΌ : is-set A β (g h : X / β β A) β g β Ξ·/ οΌ h β Ξ·/ β g οΌ h at-most-one-mediating-map/οΌ i g h p = q β»ΒΉ β r where f : X β A f = g β Ξ·/ j : identifies-related-points f j e = ap g (Ξ·/-identifies-related-points e) q : mediating-map/ i f j οΌ g q = witness-uniqueness (Ξ» f' β f' β Ξ·/ οΌ f) (universal-property/ i f j) (mediating-map/ i f j) g (universality-triangle/οΌ i f j) refl r : mediating-map/ i f j οΌ h r = witness-uniqueness (Ξ» f' β f' β Ξ·/ οΌ f) (universal-property/ i f j) (mediating-map/ i f j) h (universality-triangle/οΌ i f j) (p β»ΒΉ) at-most-one-mediating-map/ : is-set A β (g h : X / β β A) β g β Ξ·/ βΌ h β Ξ·/ β g βΌ h at-most-one-mediating-map/ i g h p = happly (at-most-one-mediating-map/οΌ i g h (dfunext fe p)) \end{code} Extending unary and binary operations to the quotient: \begin{code} extension/ : (f : X β X / β) β identifies-related-points f β (X / β β X / β) extension/ = mediating-map/ quotient-is-set extension-triangle/ : (f : X β X / β) (i : identifies-related-points f) β extension/ f i β Ξ·/ βΌ f extension-triangle/ = universality-triangle/ quotient-is-set module _ (f : X β X) (p : {x y : X} β x β y β f x β f y) where abstract private Ο : identifies-related-points (Ξ·/ β f) Ο e = Ξ·/-identifies-related-points (p e) extensionβ/ : X / β β X / β extensionβ/ = extension/ (Ξ·/ β f) Ο naturality/ : extensionβ/ β Ξ·/ βΌ Ξ·/ β f naturality/ = universality-triangle/ quotient-is-set (Ξ·/ β f) Ο module _ (f : X β X β X) (p : {x y x' y' : X} β x β x' β y β y' β f x y β f x' y') where abstract private Ο : (x : X) β identifies-related-points (Ξ·/ β f x) Ο x {y} {y'} e = Ξ·/-identifies-related-points (p {x} {y} {x} {y'} (βr x) e) p' : (x : X) {y y' : X} β y β y' β f x y β f x y' p' x {x'} {y'} = p {x} {x'} {x} {y'} (βr x) fβ : X β X / β β X / β fβ x = extensionβ/ (f x) (p' x) n/ : (x : X) β fβ x β Ξ·/ βΌ Ξ·/ β f x n/ x = naturality/ (f x) (p' x) Ξ΄ : {x x' : X} β x β x' β (y : X) β fβ x (Ξ·/ y) οΌ fβ x' (Ξ·/ y) Ξ΄ {x} {x'} e y = fβ x (Ξ·/ y) οΌβ¨ naturality/ (f x) (p' x) y β© Ξ·/ (f x y) οΌβ¨ Ξ·/-identifies-related-points (p e (βr y)) β© Ξ·/ (f x' y) οΌβ¨ (naturality/ (f x') (p' x') y)β»ΒΉ β© fβ x' (Ξ·/ y) β Ο : (b : X / β) {x x' : X} β x β x' β fβ x b οΌ fβ x' b Ο b {x} {x'} e = /-induction (Ξ» b β fβ x b οΌ fβ x' b) (Ξ» y β quotient-is-set) (Ξ΄ e) b fβ : X / β β X / β β X / β fβ d e = extension/ (Ξ» x β fβ x e) (Ο e) d extensionβ/ : X / β β X / β β X / β extensionβ/ = fβ abstract naturalityβ/ : (x y : X) β fβ (Ξ·/ x) (Ξ·/ y) οΌ Ξ·/ (f x y) naturalityβ/ x y = fβ (Ξ·/ x) (Ξ·/ y) οΌβ¨ extension-triangle/ (Ξ» x β fβ x (Ξ·/ y)) (Ο (Ξ·/ y)) x β© fβ x (Ξ·/ y) οΌβ¨ naturality/ (f x) (p (βr x)) y β© Ξ·/ (f x y) β \end{code} Without the above abstract declarations, the use of naturalityβ/ takes for ever in the module FreeGroup.lagda. Added in March 2022 by Tom de Jong. We extend unary and binary prop-valued relations to the quotient. \begin{code} module _ (r : X β Ξ© π£) (p : {x y : X} β x β y β r x οΌ r y) where extension-relβ : X / β β Ξ© π£ extension-relβ = mediating-map/ (Ξ©-is-set fe pe) r p extension-rel-triangleβ : extension-relβ β Ξ·/ βΌ r extension-rel-triangleβ = universality-triangle/ (Ξ©-is-set fe pe) r p module _ (r : X β X β Ξ© π£) (p : {x y x' y' : X} β x β x' β y β y' β r x y οΌ r x' y') where abstract private p' : (x : X) {y y' : X} β y β y' β r x y οΌ r x y' p' x {y} {y'} = p (βr x) rβ : X β X / β β Ξ© π£ rβ x = extension-relβ (r x) (p' x) Ξ΄ : {x x' : X} β x β x' β (y : X) β rβ x (Ξ·/ y) οΌ rβ x' (Ξ·/ y) Ξ΄ {x} {x'} e y = rβ x (Ξ·/ y) οΌβ¨ extension-rel-triangleβ (r x) (p (βr x)) y β© r x y οΌβ¨ p e (βr y) β© r x' y οΌβ¨ (extension-rel-triangleβ (r x') (p (βr x')) y) β»ΒΉ β© rβ x' (Ξ·/ y) β Ο : (q : X / β) {x x' : X} β x β x' β rβ x q οΌ rβ x' q Ο q {x} {x'} e = /-induction (Ξ» p β rβ x p οΌ rβ x' p) (Ξ» q β Ξ©-is-set fe pe) (Ξ΄ e) q rβ : X / β β X / β β Ξ© π£ rβ = mediating-map/ (Ξ -is-set fe (Ξ» _ β Ξ©-is-set fe pe)) rβ (Ξ» {x} {x'} e β dfunext fe (Ξ» q β Ο q e)) Ο : (x : X) β rβ (Ξ·/ x) οΌ rβ x Ο = universality-triangle/ (Ξ -is-set fe (Ξ» _ β Ξ©-is-set fe pe)) rβ (Ξ» {x} {x'} e β dfunext fe (Ξ» q β Ο q e)) Ο : (x y : X) β rβ (Ξ·/ x) (Ξ·/ y) οΌ r x y Ο x y = rβ (Ξ·/ x) (Ξ·/ y) οΌβ¨ happly (Ο x) (Ξ·/ y) β© rβ x (Ξ·/ y) οΌβ¨ extension-rel-triangleβ (r x) (p' x) y β© r x y β extension-relβ : X / β β X / β β Ξ© π£ extension-relβ = rβ extension-rel-triangleβ : (x y : X) β extension-relβ (Ξ·/ x) (Ξ·/ y) οΌ r x y extension-rel-triangleβ = Ο \end{code} For proving properties of an extended binary relation, it is useful to have a binary and ternary versions of quotient induction. \begin{code} /-inductionβ : β {π¦} {P : X / β β X / β β π¦ Μ } β ((x' y' : X / β) β is-prop (P x' y')) β ((x y : X) β P (Ξ·/ x) (Ξ·/ y)) β (x' y' : X / β) β P x' y' /-inductionβ p h = /-induction' (Ξ» x' β Ξ -is-prop fe (p x')) (Ξ» x β /-induction' (p (Ξ·/ x)) (h x)) /-inductionβ : β {π¦} {P : X / β β X / β β X / β β π¦ Μ } β ((x' y' z' : X / β) β is-prop (P x' y' z')) β ((x y z : X) β P (Ξ·/ x) (Ξ·/ y) (Ξ·/ z)) β (x' y' z' : X / β) β P x' y' z' /-inductionβ p h = /-inductionβ (Ξ» x' y' β Ξ -is-prop fe (p x' y')) (Ξ» x y β /-induction' (p (Ξ·/ x) (Ξ·/ y)) (h x y)) quotients-equivalent : (X : π€ Μ ) (R : EqRel {π€} {π₯} X) (R' : EqRel {π€} {π¦} X) β ({x y : X} β x β[ R ] y β x β[ R' ] y) β (X / R) β (X / R') quotients-equivalent X (_β_ , βp , βr , βs , βt ) (_β'_ , βp' , βr' , βs' , βt') Ξ΅ = Ξ³ where β = (_β_ , βp , βr , βs , βt ) β' = (_β'_ , βp' , βr' , βs' , βt') i : {x y : X} β x β y β Ξ·/ β' x οΌ Ξ·/ β' y i e = Ξ·/-identifies-related-points β' (lr-implication Ξ΅ e) i' : {x y : X} β x β' y β Ξ·/ β x οΌ Ξ·/ β y i' e = Ξ·/-identifies-related-points β (rl-implication Ξ΅ e) f : X / β β X / β' f = mediating-map/ β (quotient-is-set β') (Ξ·/ β') i f' : X / β' β X / β f' = mediating-map/ β' (quotient-is-set β) (Ξ·/ β) i' a : (x : X) β f (f' (Ξ·/ β' x)) οΌ Ξ·/ β' x a x = f (f' (Ξ·/ β' x)) οΌβ¨ I β© f (Ξ·/ β x) οΌβ¨ II β© Ξ·/ β' x β where I = ap f (universality-triangle/ β' (quotient-is-set β) (Ξ·/ β) i' x) II = universality-triangle/ β (quotient-is-set β') (Ξ·/ β') i x Ξ± : f β f' βΌ id Ξ± = /-induction β' (Ξ» u β f (f' u) οΌ u) (Ξ» u β quotient-is-set β') a a' : (x : X) β f' (f (Ξ·/ β x)) οΌ Ξ·/ β x a' x = f' (f (Ξ·/ β x)) οΌβ¨ I β© f' (Ξ·/ β' x) οΌβ¨ II β© Ξ·/ β x β where I = ap f' (universality-triangle/ β (quotient-is-set β') (Ξ·/ β') i x) II = universality-triangle/ β' (quotient-is-set β) (Ξ·/ β) i' x Ξ±' : f' β f βΌ id Ξ±' = /-induction β (Ξ» u β f' (f u) οΌ u) (Ξ» u β quotient-is-set β) a' Ξ³ : (X / β) β (X / β') Ξ³ = qinveq f (f' , Ξ±' , Ξ±) \end{code}