----------------------------------------------------------------------------------------------------
-- Index of the Agda Development of the paper
--
--             Set-Theoretic and Type-Theoretic Ordinals Coincide
--
--     Tom de Jong, Nicolai Kraus, Fredrik Nordvall-Forsberg, and Chuangjie Xu
----------------------------------------------------------------------------------------------------

-- The Agda development for Section II is part of EscardΓ³'s TypeTopology library.
-- Source files: UF.CumulativeHierarchy, UF.CumulativeHierarchy-LocallySmall,
--               Ordinals.CumulativeHierarchy, Ordinals.CumulativeHierarchy-Addendum
--
-- The Agda development for Section III is building on the Cubical Agda library (version 0.4).
-- Source files: bitbucket.org/nicolaikraus/constructive-ordinals-in-hott/src/master/MEWO/
--
-- All the source files are tested with Agda version 2.6.3.


{- Β§II ORDINALS IN TYPE THEORY AND SET THEORY -}

{- A. Ordinals in homotopy type theory -}

-- Accessibility
Definition-1 : {X : 𝓀 Μ‡} (_<_ : X β†’ X β†’ π“₯ Μ‡) β†’ X β†’ 𝓀 βŠ” π“₯ Μ‡
Definition-1 = is-accessible

Lemma-2-β†’ : {X : 𝓀 Μ‡} (_<_ : X β†’ X β†’ π“₯ Μ‡)
          β†’ (βˆ€ x β†’ is-accessible _<_ x)
          β†’ βˆ€ {𝓦} (P : X β†’ 𝓦 Μ‡) β†’ (βˆ€ x β†’ (βˆ€ y β†’ y < x β†’ P y) β†’ P x) β†’ βˆ€ x β†’ P x
Lemma-2-β†’ = transfinite-induction
--
Lemma-2-← : {X : 𝓀 Μ‡} (_<_ : X β†’ X β†’ π“₯ Μ‡)
          β†’ (βˆ€ {𝓦} (P : X β†’ 𝓦 Μ‡) β†’ (βˆ€ x β†’ (βˆ€ y β†’ y < x β†’ P y) β†’ P x) β†’ βˆ€ x β†’ P x)
          β†’ βˆ€ x β†’ is-accessible _<_ x
Lemma-2-← _<_ ti = ti (is-accessible _<_) (Ξ» _ β†’ step)

-- Type-theoretic ordinal
Definition-3 : ({X : 𝓀 Μ‡} (_<_ : X β†’ X β†’ π“₯ Μ‡) β†’ (𝓀 βŠ” π“₯ Μ‡))
             Γ— ({X : 𝓀 Μ‡} (_<_ : X β†’ X β†’ π“₯ Μ‡) β†’ (𝓀 βŠ” π“₯ Μ‡))
             Γ— ({X : 𝓀 Μ‡} (_<_ : X β†’ X β†’ π“₯ Μ‡) β†’ (𝓀 βŠ” π“₯ Μ‡))
             Γ— ({X : 𝓀 Μ‡} (_<_ : X β†’ X β†’ π“₯ Μ‡) β†’ (𝓀 βŠ” π“₯ Μ‡))
             Γ— (𝓀 ⁺ Μ‡)
Definition-3 = is-prop-valued
             , is-well-founded
             , is-extensional
             , is-transitive
             , Ordinal _

Remark-4 : (Ξ± : Ordinal 𝓀) β†’ is-set ⟨ Ξ± ⟩
Remark-4 = underlying-type-is-set fe'

-- Initial segment and bounded simulation
-- The bounded simulation relation ⊲ is named < in the paper.
Definition-5 : ((Ξ± : Ordinal 𝓀) β†’ ⟨ Ξ± ⟩ β†’ Ordinal 𝓀)
             Γ— (Ordinal 𝓀 β†’ Ordinal 𝓀 β†’ 𝓀 ⁺ Μ‡)
Definition-5 = _↓_
             , _⊲_

-- ⊲⁻ is the small version of ⊲
Remark-6 : (Ξ± Ξ² : Ordinal 𝓀) β†’ (Ξ± ⊲ Ξ²) ≃ (Ξ± ⊲⁻ Ξ²)
Remark-6 = ⊲-is-equivalent-to-⊲⁻

-- The ordinal OO of ordinals is named Ord in the paper.
Theorem-7 : is-well-order (_⊲_ {𝓀}) Γ— (Ordinal (𝓀 ⁺))
Theorem-7 = ⊲-is-well-order , OO _

-- Simulation
-- The simulation relation ⊴ is named ≀ in the paper.
Definition-8 : ((Ξ± : Ordinal 𝓀) (Ξ² : Ordinal π“₯) β†’ (⟨ Ξ± ⟩ β†’ ⟨ Ξ² ⟩) β†’ 𝓀 βŠ” π“₯ Μ‡)
             Γ— (Ordinal 𝓀 β†’ Ordinal π“₯ β†’ 𝓀 βŠ” π“₯ Μ‡)
Definition-8 = is-simulation
             , _⊴_

Proposition-9 : ((Ξ± : Ordinal 𝓀) β†’ Ξ± ⊴ Ξ±)
              Γ— ((Ξ± Ξ² : Ordinal 𝓀) β†’ Ξ± ⊴ Ξ² β†’ Ξ² ⊴ Ξ± β†’ Ξ± = Ξ²)
              Γ— ((Ξ± : Ordinal 𝓀) (Ξ² : Ordinal π“₯) (Ξ³ : Ordinal 𝓦) β†’ Ξ± ⊴ Ξ² β†’ Ξ² ⊴ Ξ³ β†’ Ξ± ⊴ Ξ³)
              Γ— ((Ξ± Ξ² : Ordinal 𝓀) β†’ Ξ± ⊴ Ξ²
                                   β†’ βˆ€ Ξ³ β†’ Ξ³ ⊲ Ξ± β†’ Ξ³ ⊲ Ξ²)
              Γ— ({Ξ± Ξ² : Ordinal 𝓀} β†’ (βˆ€ Ξ³ β†’ Ξ³ ⊲ Ξ± β†’ Ξ³ ⊲ Ξ²)
                                   β†’ (a : ⟨ Ξ± ⟩) β†’ Ξ£ b κž‰ ⟨ Ξ² ⟩ , (Ξ± ↓ a) = (Ξ² ↓ b))
              Γ— ({Ξ± Ξ² : Ordinal 𝓀} β†’ ((a : ⟨ Ξ± ⟩) β†’ Ξ£ b κž‰ ⟨ Ξ² ⟩ , (Ξ± ↓ a) = (Ξ² ↓ b))
                                   β†’ Ξ± ⊴ Ξ²)
Proposition-9 = ⊴-refl
              , ⊴-antisym
              , ⊴-trans
              , ⊴-gives-β‰Ό
              , from-β‰Ό
              , to-⊴ _ _

Lemma-10 : (Ξ± : Ordinal 𝓀) (Ξ² : Ordinal π“₯) (f : ⟨ Ξ± ⟩ β†’ ⟨ Ξ² ⟩)
         β†’ (is-order-equiv Ξ± Ξ² f) ↔
           (is-equiv f Γ— is-order-preserving Ξ± Ξ² f Γ— is-order-reflecting Ξ± Ξ² f)
Lemma-10 Ξ± Ξ² f = (Ξ» p β†’ order-equivs-are-equivs Ξ± Ξ² p
                      , order-equivs-are-order-preserving Ξ± Ξ² p
                      , order-equivs-are-order-reflecting Ξ± Ξ² f p)
               , (Ξ» (x , y , z) β†’ order-preserving-reflecting-equivs-are-order-equivs Ξ± Ξ² f x y z)

Lemma-11 : (Ξ± : Ordinal 𝓀) (a b : ⟨ Ξ± ⟩) (p : b β‰ΊβŸ¨ Ξ± ⟩ a)
         β†’ ((Ξ± ↓ a ) ↓ (b , p)) = (Ξ± ↓ b)
Lemma-11 = iterated-↓

-- Sum of type-theoretic ordinals
Definition-12 : Ordinal 𝓀 β†’ Ordinal 𝓀 β†’ Ordinal 𝓀
Definition-12 = _+β‚’_

Lemma-13-i : {Ξ± Ξ² : Ordinal 𝓀} (a : ⟨ Ξ± ⟩)
           β†’ (Ξ± ↓ a) = ((Ξ± +β‚’ Ξ²) ↓ inl a)
Lemma-13-i = +β‚’-↓-left
--
Lemma-13-ii : (Ξ± : Ordinal 𝓀) β†’ (Ξ± +β‚’ πŸ™β‚’) ↓ inr ⋆ = Ξ±
Lemma-13-ii = +β‚’-πŸ™β‚’-↓-right

-- Supremum of type-theoretic ordinals
Definition-14 : {I : 𝓀 Μ‡} (Ξ± : I β†’ Ordinal 𝓀) β†’ Ordinal 𝓀
Definition-14 = sup

Lemma-15-i : {I : 𝓀 Μ‡} (Ξ± : I β†’ Ordinal 𝓀)
           β†’ (i : I) (x : ⟨ Ξ± i ⟩) β†’ sup Ξ± ↓ pr₁ (sup-is-upper-bound Ξ± i) x = Ξ± i ↓ x
Lemma-15-i = initial-segment-of-sup-at-component
--
Lemma-15-ii : {I : 𝓀 Μ‡} (Ξ± : I β†’ Ordinal 𝓀)
            β†’ (y : ⟨ sup Ξ± ⟩) β†’ βˆ₯ Ξ£ i κž‰ I , Ξ£ x κž‰ ⟨ Ξ± i ⟩ , sup Ξ± ↓ y = Ξ± i ↓ x βˆ₯
Lemma-15-ii = initial-segment-of-sup-is-initial-segment-of-some-component

{- B. Ordinals in set theory -}

-- (Here, we phrase the definitions and lemmas from set theory in terms of 𝕍 introduced below.)

-- Transitive set
Definition-16 : 𝕍 β†’ 𝓀 ⁺ Μ‡
Definition-16 = is-transitive-set

-- Example 17:

βˆ… βŸ¨βˆ…βŸ© βŸ¨βˆ…,βŸ¨βˆ…βŸ©βŸ© βŸ¨βŸ¨βˆ…βŸ©βŸ© βŸ¨βˆ…,βŸ¨βˆ…βŸ©,βŸ¨βŸ¨βˆ…βŸ©βŸ©βŸ© : 𝕍
βˆ… = 𝕍-set {𝟘} 𝟘-elim
βŸ¨βˆ…βŸ© = 𝕍-set {πŸ™} (Ξ» _ β†’ βˆ…)
βŸ¨βˆ…,βŸ¨βˆ…βŸ©βŸ© = 𝕍-set {πŸ™ {𝓀} + πŸ™ {𝓀}} (cases (Ξ» _ β†’ βˆ…) (Ξ» _ β†’ βŸ¨βˆ…βŸ©))
βŸ¨βŸ¨βˆ…βŸ©βŸ© = 𝕍-set {πŸ™} Ξ» _ β†’ βŸ¨βˆ…βŸ©
βŸ¨βˆ…,βŸ¨βˆ…βŸ©,βŸ¨βŸ¨βˆ…βŸ©βŸ©βŸ© = 𝕍-set {πŸ™ {𝓀} + πŸ™ {𝓀} + πŸ™ {𝓀}} (cases (Ξ» _ β†’ βˆ…) (cases (Ξ» _ β†’ βŸ¨βˆ…βŸ©) Ξ» _ β†’ βŸ¨βŸ¨βˆ…βŸ©βŸ©))

Β¬xβˆˆβˆ… : {x : 𝕍} β†’ x ∈ βˆ… β†’ 𝟘
Β¬xβˆˆβˆ… xβˆˆβˆ… = βˆ₯βˆ₯-rec 𝟘-is-prop pr₁ (from-∈-of-𝕍-set xβˆˆβˆ…)

Example-17-i : is-transitive-set βˆ…
Example-17-i u v uβˆˆβˆ… v∈u = 𝟘-elim (Β¬xβˆˆβˆ… uβˆˆβˆ…)

Example-17-ii : is-transitive-set βŸ¨βˆ…βŸ©
Example-17-ii u v uβˆˆβŸ¨βˆ…βŸ© v∈u =
  βˆ₯βˆ₯-rec ∈-is-prop-valued
        (Ξ» (_ , βˆ…οΌu) β†’ 𝟘-elim (Β¬xβˆˆβˆ… (transport (v ∈_) (βˆ…οΌu ⁻¹) v∈u)))
        (from-∈-of-𝕍-set uβˆˆβŸ¨βˆ…βŸ©)

Example-17-iii : is-transitive-set βŸ¨βˆ…,βŸ¨βˆ…βŸ©βŸ©
Example-17-iii u v uβˆˆβŸ¨βˆ…,βŸ¨βˆ…βŸ©βŸ© v∈u =
  βˆ₯βˆ₯-rec ∈-is-prop-valued
        (Ξ» { (inl _ , βˆ…οΌu) β†’ 𝟘-elim (Β¬xβˆˆβˆ… (transport (v ∈_) (βˆ…οΌu ⁻¹) v∈u))
           ; (inr _ , βŸ¨βˆ…βŸ©οΌu) β†’ βˆ₯βˆ₯-rec ∈-is-prop-valued
                                      (Ξ» (_ , βˆ…οΌv) β†’ to-∈-of-𝕍-set ∣ inl _ , βˆ…οΌv ∣)
                                      (from-∈-of-𝕍-set (transport (v ∈_) (βŸ¨βˆ…βŸ©οΌu ⁻¹) v∈u))
           })
        (from-∈-of-𝕍-set uβˆˆβŸ¨βˆ…,βŸ¨βˆ…βŸ©βŸ©)

Example-17-iv : is-transitive-set βŸ¨βˆ…,βŸ¨βˆ…βŸ©,βŸ¨βŸ¨βˆ…βŸ©βŸ©βŸ©
Example-17-iv u v uβˆˆβŸ¨βˆ…,βŸ¨βŸ¨βˆ…βŸ©βŸ©βŸ© v∈u =
  βˆ₯βˆ₯-rec ∈-is-prop-valued
        (Ξ» { (inl _ , βˆ…οΌu) β†’ 𝟘-elim (Β¬xβˆˆβˆ… (transport (v ∈_) (βˆ…οΌu ⁻¹) v∈u))
           ; (inr (inl _) , βŸ¨βˆ…βŸ©οΌu) β†’ βˆ₯βˆ₯-rec ∈-is-prop-valued
                                            (Ξ» (_ , βˆ…οΌv) β†’ to-∈-of-𝕍-set ∣ inl _ , βˆ…οΌv ∣)
                                            (from-∈-of-𝕍-set (transport (v ∈_) (βŸ¨βˆ…βŸ©οΌu ⁻¹) v∈u))
           ; (inr (inr _) , βŸ¨βŸ¨βˆ…βŸ©βŸ©οΌu) β†’ βˆ₯βˆ₯-rec ∈-is-prop-valued
                                              (Ξ» (_ , βŸ¨βˆ…βŸ©οΌv) β†’ to-∈-of-𝕍-set ∣ inr (inl _) , βŸ¨βˆ…βŸ©οΌv ∣)
                                              (from-∈-of-𝕍-set (transport (v ∈_) (βŸ¨βŸ¨βˆ…βŸ©βŸ©οΌu ⁻¹) v∈u))
           })
        (from-∈-of-𝕍-set uβˆˆβŸ¨βˆ…,βŸ¨βŸ¨βˆ…βŸ©βŸ©βŸ©)

Example-17-v : is-transitive-set βŸ¨βŸ¨βˆ…βŸ©βŸ© β†’ 𝟘 {𝓀}
Example-17-v transitive-βŸ¨βŸ¨βˆ…βŸ©βŸ© =
  βˆ₯βˆ₯-rec 𝟘-is-prop (Ξ» (_ , βŸ¨βˆ…βŸ©οΌβˆ…) β†’ Β¬βˆ…οΌβŸ¨βˆ…βŸ© βŸ¨βˆ…βŸ©οΌβˆ…) (from-∈-of-𝕍-set βˆ…βˆˆβŸ¨βŸ¨βˆ…βŸ©βŸ©)
  where
    βˆ…βˆˆβŸ¨βŸ¨βˆ…βŸ©βŸ© : βˆ… ∈ βŸ¨βŸ¨βˆ…βŸ©βŸ©
    βˆ…βˆˆβŸ¨βŸ¨βˆ…βŸ©βŸ© = transitive-βŸ¨βŸ¨βˆ…βŸ©βŸ© βŸ¨βˆ…βŸ© βˆ… (to-∈-of-𝕍-set ∣ _ , refl ∣) (to-∈-of-𝕍-set ∣ _ , refl ∣)
    Β¬βˆ…οΌβŸ¨βˆ…βŸ© : βŸ¨βˆ…βŸ© = βˆ… β†’ 𝟘 {𝓀}
    Β¬βˆ…οΌβŸ¨βˆ…βŸ© βŸ¨βˆ…βŸ©οΌβˆ… = Β¬xβˆˆβˆ… {βˆ…} (transport (βˆ… ∈_) βŸ¨βˆ…βŸ©οΌβˆ… (to-∈-of-𝕍-set ∣ _ , refl ∣))

-- Set-theoretic ordinal
Definition-18 : 𝕍 β†’ 𝓀 ⁺ Μ‡
Definition-18 = is-set-theoretic-ordinal

Lemma-19 : {x : 𝕍} β†’ is-set-theoretic-ordinal x
         β†’ {y : 𝕍}
         β†’ y ∈ x β†’ is-set-theoretic-ordinal y
Lemma-19 = being-set-theoretic-ordinal-is-hereditary

{- C. Set theory in homotopy type theory -}

-- Equal images
-- f β‰ˆ g denotes that f and g have equal images.
Definition-20 : {A : 𝓀 Μ‡} {B : π“₯ Μ‡} {X : 𝓣 Μ‡} β†’ (A β†’ X) β†’ (B β†’ X) β†’ 𝓀 βŠ” π“₯ βŠ” 𝓣 Μ‡
Definition-20 = _β‰ˆ_

-- Cumulative hierarchy
Definition-21 : 𝓀 ⁺ Μ‡
Definition-21 = 𝕍

-- Set membership
Definition-22 : 𝕍 β†’ 𝕍 β†’ 𝓀 ⁺ Μ‡
Definition-22 = _∈_

-- Subset relation
Definition-23 : 𝕍 β†’ 𝕍 β†’ 𝓀 ⁺ Μ‡
Definition-23 = _βŠ†_

Lemma-24-i : (x y : 𝕍) β†’ (x = y) ↔ (x βŠ† y Γ— y βŠ† x)
Lemma-24-i x y = (Ξ» x=y β†’ =-to-βŠ† x=y , =-to-βŠ† (x=y ⁻¹))
               , (Ξ» (p , q) β†’ ∈-extensionality x y p q)
--
Lemma-24-ii : {𝓣 : Universe} (P : 𝕍 β†’ 𝓣 Μ‡ )
            β†’ ((x : 𝕍) β†’ is-prop (P x))
            β†’ ((x : 𝕍) β†’ ((y : 𝕍) β†’ y ∈ x β†’ P y) β†’ P x)
            β†’ (x : 𝕍) β†’ P x
Lemma-24-ii = ∈-induction

-- Type of set-theoretic ordinals
Definition-25 : 𝓀 ⁺ Μ‡
Definition-25 = π•α΅’Κ³α΅ˆ

Theorem-26 : OrdinalStructure π•α΅’Κ³α΅ˆ
Theorem-26 = _βˆˆα΅’Κ³α΅ˆ_
           , (Ξ» x y β†’ ∈-is-prop-valued)
           , βˆˆα΅’Κ³α΅ˆ-is-well-founded
           , βˆˆα΅’Κ³α΅ˆ-is-extensional
           , βˆˆα΅’Κ³α΅ˆ-is-transitive

{- D. Set-theoretic and type-theoretic ordinals coincide -}

-- Map Ξ¦
Definition-27 : Ordinal 𝓀 β†’ 𝕍
Definition-27 = Ξ¦

Lemma-28-i : (Ξ± Ξ² : Ordinal 𝓀)
           β†’ (Ξ± = Ξ²) ↔ (Ξ¦ Ξ± = Ξ¦ Ξ²)
Lemma-28-i Ξ± Ξ² = ap Ξ¦ , Ξ¦-is-left-cancellable Ξ± Ξ²
--
Lemma-28-ii : (Ξ± Ξ² : Ordinal 𝓀)
            β†’ (Ξ± ⊲ Ξ²) ↔ (Ξ¦ Ξ± ∈ Ξ¦ Ξ²)
Lemma-28-ii Ξ± Ξ² = Ξ¦-preserves-strict-order Ξ± Ξ² , Ξ¦-reflects-strict-order Ξ± Ξ²
--
Lemma-28-iii : (Ξ± Ξ² : Ordinal 𝓀)
             β†’ (Ξ± ⊴ Ξ²) ↔ (Ξ¦ Ξ± βŠ† Ξ¦ Ξ²)
Lemma-28-iii Ξ± Ξ² = Ξ¦-preserves-weak-order Ξ± Ξ² , Ξ¦-reflects-weak-order Ξ± Ξ²

-- The map Ξ¦ : Ord β†’ V factors through the inclusion π•α΅’Κ³α΅ˆ β†’ 𝕍.
Lemma-29 : Ordinal 𝓀 β†’ π•α΅’Κ³α΅ˆ
Lemma-29 = Ξ¦α΅’Κ³α΅ˆ

-- Map Ξ¨
Definition-30 : 𝕍 β†’ Ordinal 𝓀
Definition-30 = Ξ¨

-- Remark-31 : No formalizable content

Proposition-32 : Ξ¦α΅’Κ³α΅ˆ ∘ Ξ¨α΅’Κ³α΅ˆ ∼ id
Proposition-32 = Ξ¨α΅’Κ³α΅ˆ-is-section-of-Ξ¦α΅’Κ³α΅ˆ

Theorem-33 : (OO 𝓀 ≃ₒ 𝕍ᴼᴿᴰ)
           Γ— (OO 𝓀 = 𝕍ᴼᴿᴰ)
Theorem-33 = π•α΅’Κ³α΅ˆ-isomorphic-to-Ord
           , eqtoidβ‚’ (OO 𝓀)  𝕍ᴼᴿᴰ π•α΅’Κ³α΅ˆ-isomorphic-to-Ord

{- E. Revisiting the rank of a set -}

-- Type of elements
Definition-34 : (x : 𝕍) (Οƒ : is-set-theoretic-ordinal x)
              β†’ (𝓀 ⁺ Μ‡)
Definition-34 = 𝕋x ch

-- The type of elements with ∈ is a type-theroetic ordinal.
Proposition-35 : (x : 𝕍) (Οƒ : is-set-theoretic-ordinal x)
               β†’ Ordinal (𝓀 ⁺)
Proposition-35 = 𝕋xα΅’Κ³α΅ˆ ch

-- Bisimulation
-- The 𝓀-valued bisimulation relation =⁻ is named β‰ˆ in the paper.
Definition-36 : 𝕍 β†’ 𝕍 β†’ 𝓀 Μ‡
Definition-36 = _=⁻_

Lemma-37 : {x y : 𝕍} β†’ (x =⁻ y) ≃ (x = y)
Lemma-37 = =⁻-≃-=

-- Membership
-- The 𝓀-valued membership relation ∈⁻ is named ∈ᡀ in the paper.
Definition-38 : 𝕍 β†’ 𝕍 β†’ 𝓀 Μ‡
Definition-38 = _∈⁻_

Lemma-39 : {x y : 𝕍} β†’ x ∈⁻ y ≃ x ∈ y
Lemma-39 = ∈⁻-≃-∈

-- Set quotients
-- The 𝓀-valued equivalence relation ~⁻ is named ~α΅€ in the paper.
Definition-40 : {A : 𝓀 Μ‡} (f : A β†’ 𝕍)
              β†’ (A β†’ A β†’ 𝓀 ⁺ Μ‡) Γ— (𝓀 ⁺ Μ‡)
              Γ— (A β†’ A β†’ 𝓀 Μ‡) Γ— (𝓀 Μ‡)
Definition-40 f = _~_ f , A/~ f
                , _~⁻_ f , A/~⁻ f

Lemma-41 : {A : 𝓀 Μ‡} (f : A β†’ 𝕍)
         β†’ (A/~ f ≃ A/~⁻ f)
         Γ— (image f ≃ A/~ f)
Lemma-41 f = A/~-≃-A/~⁻ f
           , UF.Quotient.set-replacement-construction.image-≃-quotient
                sq pt f 𝕍-is-locally-small 𝕍-is-large-set

-- Order relation
-- The 𝓀-valued order relation ≺⁻ is named β‰Ία΅€ in the paper.
Definition-42 : {A : 𝓀 Μ‡} (f : A β†’ 𝕍) (Οƒ : is-set-theoretic-ordinal (𝕍-set f))
              β†’ (A/~ f β†’ A/~ f β†’ 𝓀 ⁺ Μ‡)
              Γ— (A/~⁻ f β†’ A/~⁻ f β†’ 𝓀 Μ‡)
Definition-42 f Οƒ = _β‰Ί_ f
                  , _≺⁻_ f Οƒ

Proposition-43 : {A : 𝓀 Μ‡} (f : A β†’ 𝕍) (Οƒ : is-set-theoretic-ordinal (𝕍-set f))
               β†’ Ordinal (𝓀 ⁺)
               Γ— Ordinal 𝓀
Proposition-43 f Οƒ = A/~α΅’Κ³α΅ˆ f Οƒ
                   , A/~β»α΅’Κ³α΅ˆ f Οƒ

Lemma-44 : {A : 𝓀 Μ‡} (f : A β†’ 𝕍) (Οƒ : is-set-theoretic-ordinal (𝕍-set f))
         β†’ total-spaceα΅’Κ³α΅ˆ ch (𝕍-set f) Οƒ = A/~α΅’Κ³α΅ˆ f Οƒ
Lemma-44 = total-space-is-quotientα΅’Κ³α΅ˆ

Theorem-45 : {A : 𝓀 Μ‡} (f : A β†’ 𝕍) (Οƒ : is-set-theoretic-ordinal (𝕍-set f))
           β†’ Ξ¨α΅’Κ³α΅ˆ (𝕍-set f , Οƒ) = A/~β»α΅’Κ³α΅ˆ f Οƒ
Theorem-45 = Ξ¨α΅’Κ³α΅ˆ-is-quotient-of-carrier

Corollary-46 : (x : 𝕍) (Οƒ : is-set-theoretic-ordinal x)
             β†’ Ξ¨α΅’Κ³α΅ˆ (x , Οƒ) ≃ₒ total-spaceα΅’Κ³α΅ˆ ch x Οƒ
Corollary-46 = Ξ¨α΅’Κ³α΅ˆ-is-isomorphic-to-total-space ch sq


{- Β§III GENERALIZING FROM ORDINALS TO SETS -}

{- A. Mewos: marked extensional wellfounded orders -}

-- mewos and covered mewos
Definition-47 : (Type β„“ β†’ Type (β„“-suc β„“))
              Γ— ((A : MEWO β„“) β†’ ⟨ A ⟩ β†’ Type β„“)
              Γ— (MEWO β„“ β†’ Type β„“)
              Γ— Type (β„“-suc β„“)
Definition-47 {β„“ = β„“} = MEWO-structure
                     , marked
                     , isCovered
                     , MEWOcov β„“

Remark-48 : ((A : MEWO β„“) β†’ isSet (⟨ A ⟩))
          Γ— ((A B : MEWO β„“) β†’ (A ≑ B) ≃ (A ≃ₒ B))
Remark-48 = isSetCarrier
          , Ξ» A B β†’ (idtoeqβ‚’ A B , UAβ‚’ A B)

-- Ordinals as covered mewos
Example-49 : Ord β†’ MEWOcov _
Example-49 (X ,, _β‰Ί_ ,, trunc ,, wf ,, ext ,, trans) =
           (X , (_β‰Ί_ , Ξ» _ β†’ Unit , isPropUnit) , trunc , ext , wf) ,
           (Ξ» x β†’ ∣ x , tt , ReflTransCl.done ∣)

{- B. Order relations between mewos -}

-- Simulation
Definition-50 : ((A : MEWO β„“)(B : MEWO β„“') (f : ⟨ A ⟩ β†’ ⟨ B ⟩) β†’ Type _)
              Γ— (MEWO β„“ β†’ MEWO β„“' β†’ Type _)
              Γ— (MEWOcov β„“ β†’ MEWOcov β„“ β†’ Type β„“)
Definition-50 = isSimulation
              , _≀_
              , _≀c_

Lemma-51-i : (A : MEWO β„“)(B : MEWO β„“') β†’ (f : ⟨ A ⟩ β†’ ⟨ B ⟩)
           β†’ isSimulation A B f β†’ {x y : ⟨ A ⟩} β†’ f x ≑ f y β†’ x ≑ y
Lemma-51-i = simulationsInjective
--
Lemma-51-ii : ((A : MEWO β„“)(B : MEWO β„“') (f : ⟨ A ⟩ β†’ ⟨ B ⟩) β†’ isProp (isSimulation A B f))
            Γ— ((A : MEWO β„“)(B : MEWO β„“') β†’ isProp (A ≀ B))
Lemma-51-ii = isPropSimulation
            , isPropβŸ¨β‰€βŸ©
--
Lemma-51-iii : (A B : MEWO β„“) β†’ A ≀ B β†’ B ≀ A β†’ A ≑ B
Lemma-51-iii = ≀Antisymmetry
--
Lemma-51-iv  : (A : MEWO β„“)(B : MEWO β„“')(C : MEWO β„“'') β†’ A ≀ B β†’ B ≀ C β†’ A ≀ C
Lemma-51-iv = ≀Trans
--
Lemma-51-v : isSet (MEWO β„“)
Lemma-51-v = isSetMEWO
--
Lemma-51-vi : (A : MEWO β„“)(B : MEWO β„“') (f : ⟨ A ⟩ β†’ ⟨ B ⟩)
            β†’ isSimulationWithExists A B f β†’ isSimulation A B f
Lemma-51-vi = existsSimulationsAreSimulations

-- Initial segment
Definition-52 : (A : MEWO β„“) β†’ ⟨ A ⟩ β†’ MEWO β„“
Definition-52 = _↓+_

Lemma-53 : (A : MEWO β„“) β†’ (a : ⟨ A ⟩) β†’ isCovered (A ↓+ a)
Lemma-53 = initialSegmentsCovered

-- Bounded simulation
Definition-54 : (MEWO β„“ β†’ MEWO β„“' β†’ Type (β„“-max β„“ β„“'))
              Γ— (MEWOcov β„“ β†’ MEWOcov β„“ β†’ Type β„“)
Definition-54 = _<⁻_
              , _≀c_

Lemma-55 : WellFounded (_<_ {β„“ = β„“})
         Γ— WellFounded (_<c_ {β„“ =Β β„“})
Lemma-55 = <Wellfounded
         , coveredWellfounded

{- C. Subtleties caused by markings -}

-- Trivializing the marking: every element of a mewo is marked
Definition-56 : MEWO β„“ β†’ MEWO β„“
Definition-56 = markAll

Lemma-57-i : (X : MEWO β„“) β†’ (x : ⟨ X ⟩) β†’ isSimulation (X ↓+ x) (markAll X) fst
Lemma-57-i = segment-inclusion-markAll
--
Lemma-57-ii : (X Y : MEWO β„“) β†’ X < Y β†’ X ≀ markAll Y
Lemma-57-ii = <-to-<-withoutMarking
--
Lemma-57-iii : (X Y Z : MEWO β„“) β†’ X < Y β†’ Y < Z β†’ X < markAll Z
Lemma-57-iii = <-transWithoutMarking

Lemma-58 : (A : MEWO β„“) (x y : ⟨ A ⟩) β†’ (A ↓+ x) ≑ (A ↓+ y) β†’ x ≑ y
Lemma-58 = ↓+Injective

Corollary-59 : (A B : MEWO β„“) β†’ isProp (A < B)
Corollary-59 = isProp⟨<⟩

{- D. Simulations and coverings -}

Lemma-60 : (A B : MEWO β„“) (f : ⟨ A ⟩ β†’ ⟨ B ⟩) β†’ isMarkingPreserving A B f
         β†’ isSimulation A B f ↔ ((a : ⟨ A ⟩) β†’ A ↓+ a ≑ B ↓+ f a)
Lemma-60 A B f mpf = simulationsPreserve↓ A B f , Ξ» q β†’ preserve↓→Simulation A B f q mpf

Corollary-61 : (X Y Z : MEWO β„“) β†’ X < Y β†’ Y ≀ Z β†’ X < Z
Corollary-61 = <βˆ˜β‰€-in-<

-- Partial simulation
Definition-62 : ((A B : MEWO β„“) β†’ (f : (x : ⟨ A ⟩) β†’ marked A x β†’ ⟨ B ⟩) β†’ Type _)
              Γ— (MEWO β„“ β†’ MEWO β„“ β†’ Type _)
Definition-62 = IsPartialSimulation
              , _β‰€β‚š_

Lemma-63 : ((A B : MEWO β„“) β†’
            A β‰€β‚š B ≑
            ((x : ⟨ A ⟩) β†’ (marked A x) β†’ βˆ₯ Ξ£[ y ∈ ⟨ B ⟩ ] (A ↓+ x ≑ B ↓+ y) Γ— marked B y βˆ₯))
         Γ— ((A B : MEWO β„“) β†’ isProp (A β‰€β‚š B))
Lemma-63 = β‰€β‚šCharacterisation
         , isPropβŸ¨β‰€β‚šβŸ©

-- Principality
Definition-64 : MEWO β„“ β†’ Type (β„“-suc β„“)
Definition-64 = isPrincipal

Lemma-65 : (A : MEWO β„“)
         β†’ isCovered A ↔ isPrincipal A
Lemma-65 A = covered→principal A , principal→covered A

Theorem-66 : isEWO (_<c_ {β„“ =Β β„“})
Theorem-66 = isEWO⟨MEWOcov⟩

{- E. Constructions on mewos -}

-- Singleton
Definition-67 : ((A : MEWO β„“) β†’ ⟨ A ⟩ ⊎ Unit β†’ ⟨ A ⟩ ⊎ Unit β†’ Type β„“)
              Γ— ((A : MEWO β„“) β†’ ⟨ A ⟩ ⊎ Unit β†’ hProp β„“)
              Γ— ((A : MEWO β„“) β†’ isCovered A β†’ MEWO β„“)
Definition-67 = succ-<
              , succ-marking
              , succ

Lemma-68 : ((A : MEWO β„“) β†’ (cov : isCovered A) β†’ isCovered (succ A cov))
         Γ— (MEWOcov β„“ β†’ MEWOcov β„“)
Lemma-68 = succCovered
         , csucc

-- Union of mewos
Definition-69 : (A : Type β„“) β†’ (A β†’ MEWO β„“) β†’ MEWO β„“
Definition-69 = ⋃

-- Remark-70 : No formalizable content

Lemma-71 : (A : Type β„“) (F : A β†’ MEWO β„“)
         β†’ ((a : A) β†’ F a ≀ ⋃ A F)
         Γ— ((X : MEWO β„“) β†’ ((a : A) β†’ F a ≀ X) β†’ ⋃ A F ≀ X)
Lemma-71 A F = ⋃-isUpperBound A F
             , ⋃-isLeastUpperBound A F

Lemma-72 : (A : Type β„“) (F : A β†’ MEWO β„“)
         β†’ ((a : A) β†’ isCovered (F a)) β†’ isCovered (⋃ A F)
Lemma-72 = ⋃-covered

-- Remark-73 : No formalizable content

{- F. V-sets and covered mewos coincide -}

Lemma-74 : V-as-MEWO β„“ ≀ MEWOcov-as-MEWO β„“
Lemma-74 = ψ , ψ-simulation

Lemma-75 : MEWOcov-as-MEWO β„“ ≀ V-as-MEWO β„“
Lemma-75 = Ο† , Ο†-simulation

Theorem-76 : V-as-MEWO β„“ ≑ MEWOcov-as-MEWO β„“
Theorem-76 = V≑MEWOcov