Tom de Jong, 4 & 5 April 2022.

Set Replacement is derivable from the existence of quotients in the
presence of propositional truncations or function extensionality.

\begin{code}

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

module Quotient.GivesSetReplacement where

open import MLTT.Spartan

open import Quotient.Type
open import Quotient.GivesPropTrunc

open import UF.Equiv
open import UF.FunExt
open import UF.PropTrunc
open import UF.Sets
open import UF.Size
open import UF.Subsingletons

module _
        (sq : set-quotients-exist)
        (pt : propositional-truncations-exist)
       where

 open general-set-quotients-exist sq
 open PropositionalTruncation pt
 open import UF.ImageAndSurjection pt

 module set-replacement-construction
         {X : 𝓀 Μ‡ }
         {Y : 𝓦 Μ‡ }
         (f : X β†’ Y)
         (Y-is-loc-small : Y is-locally π“₯ small)
         (Y-is-set : is-set Y)
        where

  _β‰ˆ_ : X β†’ X β†’ 𝓦 Μ‡
  x β‰ˆ x' = f x = f x'

  β‰ˆ-is-prop-valued : is-prop-valued _β‰ˆ_
  β‰ˆ-is-prop-valued x x' = Y-is-set

  β‰ˆ-is-reflexive : reflexive _β‰ˆ_
  β‰ˆ-is-reflexive x = refl

  β‰ˆ-is-symmetric : symmetric _β‰ˆ_
  β‰ˆ-is-symmetric x x' p = p ⁻¹

  β‰ˆ-is-transitive : transitive _β‰ˆ_
  β‰ˆ-is-transitive _ _ _ p q = p βˆ™ q

  β‰ˆ-is-equiv-rel : is-equiv-relation _β‰ˆ_
  β‰ˆ-is-equiv-rel = β‰ˆ-is-prop-valued , β‰ˆ-is-reflexive  ,
                   β‰ˆ-is-symmetric   , β‰ˆ-is-transitive

 \end{code}

 Using that Y is locally π“₯ small, we resize _β‰ˆ_ to a π“₯-valued equivalence
 relation.

 \begin{code}

  _β‰ˆβ»_ : X β†’ X β†’ π“₯ Μ‡
  x β‰ˆβ» x' = pr₁ (Y-is-loc-small (f x) (f x'))

  β‰ˆβ»-≃-β‰ˆ : {x x' : X} β†’ x β‰ˆβ» x' ≃ x β‰ˆ x'
  β‰ˆβ»-≃-β‰ˆ {x} {x'} = prβ‚‚ (Y-is-loc-small (f x) (f x'))

  β‰ˆβ»-is-prop-valued : is-prop-valued _β‰ˆβ»_
  β‰ˆβ»-is-prop-valued x x' = equiv-to-prop β‰ˆβ»-≃-β‰ˆ (β‰ˆ-is-prop-valued x x')

  β‰ˆβ»-is-reflexive : reflexive _β‰ˆβ»_
  β‰ˆβ»-is-reflexive x = ⌜ β‰ˆβ»-≃-β‰ˆ ⌝⁻¹ (β‰ˆ-is-reflexive x)

  β‰ˆβ»-is-symmetric : symmetric _β‰ˆβ»_
  β‰ˆβ»-is-symmetric x x' p = ⌜ β‰ˆβ»-≃-β‰ˆ ⌝⁻¹ (β‰ˆ-is-symmetric x x' (⌜ β‰ˆβ»-≃-β‰ˆ ⌝ p))

  β‰ˆβ»-is-transitive : transitive _β‰ˆβ»_
  β‰ˆβ»-is-transitive x x' x'' p q =
   ⌜ β‰ˆβ»-≃-β‰ˆ ⌝⁻¹ (β‰ˆ-is-transitive x x' x'' (⌜ β‰ˆβ»-≃-β‰ˆ ⌝ p) (⌜ β‰ˆβ»-≃-β‰ˆ ⌝ q))

  β‰ˆβ»-is-equiv-rel : is-equiv-relation _β‰ˆβ»_
  β‰ˆβ»-is-equiv-rel = β‰ˆβ»-is-prop-valued , β‰ˆβ»-is-reflexive  ,
                    β‰ˆβ»-is-symmetric   , β‰ˆβ»-is-transitive

  ≋ : EqRel X
  ≋ = _β‰ˆ_ , β‰ˆ-is-equiv-rel

  X/β‰ˆ : 𝓀 βŠ” 𝓦 Μ‡
  X/β‰ˆ = (X / ≋)

  X/β‰ˆβ» : 𝓀 βŠ” π“₯ Μ‡
  X/β‰ˆβ» = (X / (_β‰ˆβ»_ , β‰ˆβ»-is-equiv-rel))

  [_] : X β†’ X/β‰ˆ
  [_] = Ξ·/ ≋

  X/β‰ˆ-≃-X/β‰ˆβ» : X/β‰ˆ ≃ X/β‰ˆβ»
  X/β‰ˆ-≃-X/β‰ˆβ» = quotients-equivalent X ≋ (_β‰ˆβ»_ , β‰ˆβ»-is-equiv-rel)
                                        (⌜ β‰ˆβ»-≃-β‰ˆ ⌝⁻¹ , ⌜ β‰ˆβ»-≃-β‰ˆ ⌝)

 \end{code}

 We now proceed to show that X/β‰ˆ and image f are equivalent types.

 \begin{code}

  corestriction-respects-β‰ˆ : {x x' : X}
                           β†’ x β‰ˆ x'
                           β†’ corestriction f x = corestriction f x'
  corestriction-respects-β‰ˆ =
   to-subtype-= (Ξ» y β†’ being-in-the-image-is-prop y f)

  quotient-to-image : X/β‰ˆ β†’ image f
  quotient-to-image = mediating-map/ ≋ (image-is-set f Y-is-set)
                       (corestriction f) (corestriction-respects-β‰ˆ)

  image-to-quotient' : (y : image f)
                     β†’ Ξ£ q κž‰ X/β‰ˆ , βˆƒ x κž‰ X , ([ x ] = q) Γ— (f x = pr₁ y)
  image-to-quotient' (y , p) = βˆ₯βˆ₯-rec prp r p
   where
    r : (Ξ£ x κž‰ X , f x = y)
      β†’ (Ξ£ q κž‰ X/β‰ˆ , βˆƒ x κž‰ X , ([ x ] = q) Γ— (f x = y))
    r (x , e) = [ x ] , ∣ x , refl , e ∣
    prp : is-prop (Ξ£ q κž‰ X/β‰ˆ , βˆƒ x κž‰ X , ([ x ] = q) Γ— (f x = y))
    prp (q , u) (q' , u') = to-subtype-= (Ξ» _ β†’ βˆƒ-is-prop)
                                         (βˆ₯βˆ₯-recβ‚‚ (/-is-set ≋) Ξ³ u u')
     where
      Ξ³ : (Ξ£ x  κž‰ X , ([ x  ] = q ) Γ— (f x  = y))
        β†’ (Ξ£ x' κž‰ X , ([ x' ] = q') Γ— (f x' = y))
        β†’ q = q'
      Ξ³ (x , refl , e) (x' , refl , refl) = Ξ·/-identifies-related-points ≋ e

  image-to-quotient : image f β†’ X/β‰ˆ
  image-to-quotient y = pr₁ (image-to-quotient' y)

  image-to-quotient-lemma : (x : X)
                          β†’ image-to-quotient (corestriction f x) = [ x ]
  image-to-quotient-lemma x = βˆ₯βˆ₯-rec (/-is-set ≋) Ξ³ t
   where
    q : X/β‰ˆ
    q = image-to-quotient (corestriction f x)
    t : βˆƒ x' κž‰ X , ([ x' ] = q) Γ— (f x' = f x)
    t = prβ‚‚ (image-to-quotient' (corestriction f x))
    Ξ³ : (Ξ£ x' κž‰ X , ([ x' ] = q) Γ— (f x' = f x))
      β†’ q = [ x ]
    γ (x' , u , v) =   q    =⟨ u ⁻¹ ⟩
                     [ x' ] =⟨ Ξ·/-identifies-related-points ≋ v ⟩
                     [ x  ] ∎

  image-≃-quotient : image f ≃ X/β‰ˆ
  image-≃-quotient = qinveq Ο• (ψ , ρ , Οƒ)
   where
    Ο• : image f β†’ X/β‰ˆ
    Ο• = image-to-quotient
    ψ : X/β‰ˆ β†’ image f
    ψ = quotient-to-image
    Ο„ : (x : X) β†’ ψ [ x ] = corestriction f x
    Ο„ = universality-triangle/ ≋ (image-is-set f Y-is-set)
                               (corestriction f)
                               corestriction-respects-β‰ˆ
    Οƒ : Ο• ∘ ψ ∼ id
    Οƒ = /-induction ≋ (Ξ» x' β†’ /-is-set ≋) Ξ³
     where
      Ξ³ : (x : X) β†’ Ο• (ψ [ x ]) = [ x ]
      Ξ³ x = Ο• (ψ [ x ])            =⟨ ap Ο• (Ο„ x)                ⟩
            Ο• (corestriction f x ) =⟨ image-to-quotient-lemma x ⟩
            [ x ]                  ∎
    ρ : ψ ∘ Ο• ∼ id
    ρ (y , p) = βˆ₯βˆ₯-rec (image-is-set f Y-is-set) Ξ³ p
     where
      Ξ³ : (Ξ£ x κž‰ X , f x = y) β†’ ψ (Ο• (y , p)) = (y , p)
      Ξ³ (x , refl) = ψ (Ο• (f x , p))           =⟨ β¦…1⦆ ⟩
                     ψ (Ο• (corestriction f x)) =⟨ β¦…2⦆ ⟩
                     ψ [ x ]                   =⟨ β¦…3⦆ ⟩
                     corestriction f x         =⟨ β¦…4⦆ ⟩
                     (f x , p)                 ∎
       where
        β¦…4⦆ = to-subtype-= (Ξ» yΒ β†’ being-in-the-image-is-prop y f) refl
        β¦…1⦆ = ap (ψ ∘ Ο•) (β¦…4⦆ ⁻¹)
        β¦…2⦆ = ap ψ (image-to-quotient-lemma x)
        β¦…3⦆ = Ο„ x

 set-replacement-from-set-quotients-and-prop-trunc : Set-Replacement pt
 set-replacement-from-set-quotients-and-prop-trunc
  {𝓦} {𝓣} {𝓀} {π“₯} {X} {Y} f X-is-small Y-is-loc-small Y-is-set = X/β‰ˆβ» , ≃-sym e
  where
   X' : 𝓀 Μ‡
   X' = pr₁ X-is-small
   Ο† : X' ≃ X
   Ο† = prβ‚‚ X-is-small
   f' : X' β†’ Y
   f' = f ∘ ⌜ Ο† ⌝
   open set-replacement-construction f' Y-is-loc-small Y-is-set
   open import UF.EquivalenceExamples
   e = image f  β‰ƒβŸ¨ Ξ£-cong (Ξ» y β†’ βˆ₯βˆ₯-cong pt (h y)) ⟩
       image f' β‰ƒβŸ¨ image-≃-quotient ⟩
       X/β‰ˆ      β‰ƒβŸ¨ X/β‰ˆ-≃-X/β‰ˆβ»       ⟩
       X/β‰ˆβ»     β– 
    where
     h : (y : Y) β†’ (Ξ£ x κž‰ X , f x = y) ≃ (Ξ£ x' κž‰ X' , f' x' = y)
     h y = ≃-sym (Ξ£-change-of-variable (Ξ» x β†’ f x = y) ⌜ Ο† ⌝ (⌜⌝-is-equiv Ο†))


\end{code}

End of anonymous module assuming set-quotients-exist and
propositional-truncations-exist.

\begin{code}

set-replacement-from-set-quotients-and-funext
 : (sq : set-quotients-exist)
   (fe : Fun-Ext)
 β†’ Set-Replacement (propositional-truncations-from-set-quotients sq fe)
set-replacement-from-set-quotients-and-funext sq fe =
 set-replacement-from-set-quotients-and-prop-trunc sq
  (propositional-truncations-from-set-quotients sq fe)

\end{code}