Martin Escardo and Tom de Jong, August 2018, April 2022, September 2023.

Quotients. Much of this material is moved from or abstracted from the
earlier 2018 module Quotient.Large by Martin Escardo.

\begin{code}

{-# OPTIONS --safe --without-K #-}

module Quotient.Type where

open import MLTT.Spartan

open import UF.Base hiding (_β‰ˆ_)
open import UF.Equiv
open import UF.FunExt
open import UF.PropTrunc
open import UF.Sets
open import UF.Sets-Properties
open import UF.SubtypeClassifier
open import UF.SubtypeClassifier-Properties
open import UF.Subsingletons
open import UF.Subsingletons-FunExt

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     _β‰ˆ_

EqRel : {𝓀 π“₯ : Universe} β†’ 𝓀 Μ‡ β†’ 𝓀 βŠ” (π“₯ ⁺) Μ‡
EqRel {𝓀} {π“₯} X = Ξ£ R κž‰ (X β†’ X β†’ π“₯ Μ‡ ) , is-equiv-relation R

_β‰ˆ[_]_ : {X : 𝓀 Μ‡ } β†’ X β†’ EqRel X β†’ X β†’ π“₯ Μ‡
x β‰ˆ[ _β‰ˆ_ , _ ] y = x β‰ˆ y

identifies-related-points : {X : 𝓀 Μ‡ }
                          β†’ EqRel {𝓀} {π“₯} X
                          β†’ {Y : 𝓦 Μ‡ }
                          β†’ (X β†’ Y) β†’ 𝓀 βŠ” π“₯ βŠ” 𝓦 Μ‡
identifies-related-points (_β‰ˆ_ , _) f = βˆ€ {x x'} β†’ x β‰ˆ x' β†’ f x = f x'

\end{code}

To account for the module Quotient.Large, and, at the same time, usual
(small) quotients, we introduce a parametric definion of existence of
quotients. For small quotients we take β„“ = id, and for large quotients
we take β„“ = (_⁺) (see below).

\begin{code}

record general-set-quotients-exist (β„“ : Universe β†’ Universe) : 𝓀ω where
 field
  _/_ : {𝓀 π“₯ : Universe} (X : 𝓀 Μ‡ ) β†’ EqRel {𝓀} {π“₯} X β†’ 𝓀 βŠ” β„“ π“₯ Μ‡
  Ξ·/ : {𝓀 π“₯ : Universe} {X : 𝓀 Μ‡ } (≋ : EqRel {𝓀} {π“₯} X) β†’ X β†’ X / ≋
  Ξ·/-identifies-related-points : {𝓀 π“₯ : Universe}
                                 {X : 𝓀 Μ‡ } (≋ : EqRel {𝓀} {π“₯} X)
                               β†’ identifies-related-points ≋ (Ξ·/ ≋)
  /-is-set : {𝓀 π“₯ : Universe} {X : 𝓀 Μ‡ } (≋ : EqRel {𝓀} {π“₯} X) β†’ is-set (X / ≋)
  /-universality : {𝓀 π“₯ : Universe} {X : 𝓀 Μ‡ } (≋ : EqRel {𝓀} {π“₯} X)
                   {𝓦 : Universe} {Y : 𝓦 Μ‡ }
                 β†’ is-set Y
                 β†’ (f : X β†’ Y)
                 β†’ identifies-related-points ≋ f
                 β†’ βˆƒ! fΜ… κž‰ (X / ≋ β†’ Y) , fΜ… ∘ Ξ·/ ≋ ∼ f

\end{code}

Added 22 August 2022.
The induction principle follows from the universal property.

\begin{code}

 /-induction : {X : 𝓀 Μ‡ } (≋ : EqRel {𝓀} {π“₯} X)
               {P : X / ≋ β†’ 𝓦 Μ‡ }
             β†’ ((x' : X / ≋) β†’ is-prop (P x'))
             β†’ ((x : X) β†’ P (Ξ·/ ≋ x)) β†’ (y : X / ≋) β†’ P y
 /-induction {X = X} ≋ {P} P-is-prop-valued ρ y =
  transport P (happly fΜ…-section-of-pr₁ y) (prβ‚‚ (fΜ… y))
   where
    f : X β†’ Ξ£ P
    f x = (Ξ·/ ≋ x , ρ x)
    f-identifies-related-points : identifies-related-points ≋ f
    f-identifies-related-points r =
     to-subtype-= P-is-prop-valued (Ξ·/-identifies-related-points ≋ r)
    Ξ£P-is-set : is-set (Ξ£ P)
    Ξ£P-is-set = subsets-of-sets-are-sets (X / ≋) P (/-is-set ≋)
                                         (Ξ» {x'} β†’ P-is-prop-valued x')
    u : βˆƒ! fΜ… κž‰ (X / ≋ β†’ Ξ£ P) , fΜ… ∘ Ξ·/ ≋ ∼ f
    u = /-universality ≋ Ξ£P-is-set f f-identifies-related-points
    fΜ… : X / ≋ β†’ Ξ£ P
    fΜ… = βˆƒ!-witness u
    fΜ…-after-Ξ·-is-f : fΜ… ∘ Ξ·/ ≋ ∼ f
    fΜ…-after-Ξ·-is-f = βˆƒ!-is-witness u
    fΜ…-section-of-pr₁ : pr₁ ∘ fΜ… = id
    fΜ…-section-of-pr₁ = ap pr₁ (singletons-are-props c (pr₁ ∘ fΜ… , h)
                                                      (id , Ξ» x β†’ refl))
     where
      c : βˆƒ! g κž‰ (X / ≋ β†’ X / ≋) , g ∘ Ξ·/ ≋ ∼ Ξ·/ ≋
      c = /-universality ≋ (/-is-set ≋) (Ξ·/ ≋) (Ξ·/-identifies-related-points ≋)
      h : pr₁ ∘ fΜ… ∘ Ξ·/ ≋ ∼ Ξ·/ ≋
      h x = ap pr₁ (fΜ…-after-Ξ·-is-f x)

\end{code}

Paying attention to universe levels, it is important to note that the quotient
of X : 𝓀 by a π“₯-valued equivalence relation is assumed to live in 𝓀 βŠ” π“₯. In
particular, the quotient of type in 𝓀 by a 𝓀-valued equivalence relation lives
in 𝓀 again.

The following are facts about quotients, moved from Quotient.Large as
they are of general use.

\begin{code}

 module _
         {X : 𝓀 Μ‡ }
         (≋@(_β‰ˆ_ , β‰ˆp , β‰ˆr , β‰ˆs , β‰ˆt) : EqRel {𝓀} {π“₯} X)
        where

  module _ (pt : propositional-truncations-exist) where

   open PropositionalTruncation pt
   open import UF.ImageAndSurjection pt

   Ξ·/-is-surjection : is-surjection (Ξ·/ {𝓀} {π“₯} {X} ≋)
   Ξ·/-is-surjection = /-induction ≋
                       (Ξ» x' β†’ being-in-the-image-is-prop x' (Ξ·/ ≋))
                       (Ξ» x β†’ ∣ x , refl ∣)
  module _
          {A : 𝓦 Μ‡ }
          (A-is-set : is-set A)
         where

   mediating-map/ : (f : X β†’ A)
                  β†’ identifies-related-points ≋ f
                  β†’ X / ≋ β†’ A
   mediating-map/ f j = βˆƒ!-witness (/-universality ≋ A-is-set f j)

   universality-triangle/ : (f : X β†’ A)
                            (j : identifies-related-points ≋ f)
                          β†’ mediating-map/ f j ∘ Ξ·/ ≋ ∼ f
   universality-triangle/ f j = βˆƒ!-is-witness (/-universality ≋ A-is-set f j)

   at-most-one-mediating-map/ : (g h : X / ≋ β†’ A)
                              β†’ g ∘ Ξ·/ ≋ ∼ h ∘ Ξ·/ ≋
                              β†’ g ∼ h
   at-most-one-mediating-map/ g h p x = Ξ³
    where
     f : X β†’ A
     f = g ∘ Ξ·/ ≋

     j : identifies-related-points ≋ f
     j e = ap g (Ξ·/-identifies-related-points ≋ e)

     q : mediating-map/ f j = g
     q = witness-uniqueness (Ξ» fΜ… β†’ fΜ… ∘ Ξ·/ ≋ ∼ f)
          (/-universality ≋ A-is-set f j)
          (mediating-map/ f j)
          g
          (universality-triangle/ f j)
          (Ξ» x β†’ refl)

     r : mediating-map/ f j = h
     r = witness-uniqueness (Ξ» fΜ… β†’ fΜ… ∘ Ξ·/ ≋ ∼ f)
          (/-universality ≋ A-is-set f j)
          (mediating-map/ f j)
          h
          (universality-triangle/ f j)
          (Ξ» x β†’ (p x)⁻¹)

     γ = g x                  =⟨ happly (q ⁻¹) x ⟩
         mediating-map/ f j x =⟨ happly r x ⟩
         h x                  ∎

  extension/ : (f : X β†’ X / ≋)
             β†’ identifies-related-points ≋ f
             β†’ (X / ≋ β†’ X / ≋)
  extension/ = mediating-map/ (/-is-set ≋)

  extension-triangle/ : (f : X β†’ X / ≋)
                        (i : identifies-related-points ≋ f)
                      β†’ extension/ f i ∘ Ξ·/ ≋ ∼ f
  extension-triangle/ = universality-triangle/ (/-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/ (/-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)

     Ξ΄ : {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 ≋ (Ξ» y β†’ /-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}

We extend unary and binary prop-valued relations to the quotient.

\begin{code}

 module extending-relations-to-quotient (fe : Fun-Ext) (pe : Prop-Ext) where

  module _
          {X : 𝓀 Μ‡ }
          (≋@(_β‰ˆ_ , β‰ˆp , β‰ˆr , β‰ˆs , β‰ˆt) : EqRel {𝓀} {π“₯} X)
         where

   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 ≋ (Ξ» 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}

 module _
         (fe : Fun-Ext)
         {X : 𝓀 Μ‡ }
         (≋ : EqRel {𝓀 } {π“₯} X)
        where

  /-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/ ≋ (/-is-set ≋') (Ξ·/ ≋') i
   f⁻¹ : X / ≋' β†’ X / ≋
   f⁻¹ = mediating-map/ ≋' (/-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/ ≋' (/-is-set ≋) (Ξ·/ ≋) i⁻¹ x)
     II = universality-triangle/ ≋ (/-is-set ≋') (Ξ·/ ≋') i x
   α : f ∘ f⁻¹ ∼ id
   Ξ± = /-induction ≋' (Ξ» _ β†’ /-is-set ≋') a
   b : (x : X) β†’ f⁻¹ (f (Ξ·/ ≋ x)) = Ξ·/ ≋ x
   b x = f⁻¹ (f (Ξ·/ ≋ x)) =⟨ I ⟩
         f⁻¹ (Ξ·/ ≋' x)    =⟨ II ⟩
         Ξ·/ ≋ x           ∎
    where
     I  = ap f⁻¹ (universality-triangle/ ≋ (/-is-set ≋') (Ξ·/ ≋') i x)
     II = universality-triangle/ ≋' (/-is-set ≋) (Ξ·/ ≋) i⁻¹ x
   β : f⁻¹ ∘ f ∼ id
   Ξ² = /-induction ≋ (Ξ» _ β†’ /-is-set ≋) b
   Ξ³ : (X / ≋) ≃ (X / ≋')
   γ = qinveq f (f⁻¹ , β , α)

\end{code}

We now define the existence of small and large quotients:

\begin{code}

set-quotients-exist large-set-quotients-exist : 𝓀ω
set-quotients-exist       = general-set-quotients-exist (Ξ» 𝓀 β†’ 𝓀)
large-set-quotients-exist = general-set-quotients-exist (_⁺)

\end{code}

It turns out that quotients, if they exist, are necessarily
effective. This is proved the module Quotient.Effective. But we need
to include the definition here.

\begin{code}

are-effective : {β„“ : Universe β†’ Universe} β†’ general-set-quotients-exist β„“ β†’ 𝓀ω
are-effective sq = {𝓀 π“₯ : Universe} {X : 𝓀 Μ‡ }
                   (R : EqRel {𝓀} {π“₯} X)
                   {x y : X}
                 β†’ Ξ·/ R x = Ξ·/ R y β†’ x β‰ˆ[ R ] y
 where
  open general-set-quotients-exist sq

\end{code}