Martin Escardo & Tom de Jong, June 2023.

Iterative ordinals.

We define the type of iterative ordinals as a subtype of that of
iterative sets, which in turn, is defined a subtype of that of
iterative multisets.

Iterative ordinals are defined in the same way as in the constructive
and non-constructive set theories CZF and ZFC, following Powell [7],
as transitive sets whose members are also transitive.

The main theorem in this module is that the iterative ordinals
coincide with the HoTT-book ordinals. This builds on

[5] Tom de Jong, Nicolai Kraus, Fredrik Nordvall Forsberg and
    Chuangjie Xu. "Set-Theoretic and Type-Theoretic Ordinals
    Coincide". To appear at LICS 2023, June 2023.
    https://arxiv.org/abs/2301.10696

The difference is that instead of using the HoTT-book higher-inductive
definition of the cumulative hierarchy, we use Gylterud's
construction.

[4] H. R. Gylterud, "From multisets to sets in homotopy type theory".
    The Journal of Symbolic Logic, vol. 83, no. 3, pp. 1132–1146,
    2018. https://doi.org/10.1017/jsl.2017.84

See the module Iterative.index for more bibliographic references,
which uses the same numbering as above.

\begin{code}

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

open import MLTT.Spartan
open import UF.Univalence

module Iterative.Ordinals
        (ua : Univalence)
        (𝓀 : Universe)
       where

open import UF.FunExt
open import UF.UA-FunExt

private
 𝓀⁺ : Universe
 𝓀⁺ = 𝓀 ⁺

 fe : Fun-Ext
 fe = Univalence-gives-Fun-Ext ua

 fe' : FunExt
 fe' 𝓀 π“₯ = fe {𝓀} {π“₯}

open import Iterative.Multisets 𝓀
open import Iterative.Sets ua 𝓀
open import Ordinals.Equivalence
open import Ordinals.Notions
open import Ordinals.OrdinalOfOrdinals ua
open import Ordinals.Type hiding (Ord)
open import Ordinals.Underlying
open import Ordinals.WellOrderTransport
open import UF.Base
open import UF.Embeddings
open import UF.Equiv
open import UF.EquivalenceExamples
open import UF.PairFun
open import UF.Sets
open import UF.Size
open import UF.Subsingletons
open import UF.Subsingletons-FunExt
open import W.Type

\end{code}

An iterative ordinal is a transitive iterative set whose members are
also transitive. This definition is due to Powell [7] in the context
of ZF. Notice that it is not by induction. (von Neumann's inductive
definition is that a set is an ordinal if it is a well-ordered set of
all smaller ordinals.)

\begin{code}

is-transitive-iset : 𝕍 β†’ 𝓀⁺ Μ‡
is-transitive-iset A = (B C : 𝕍) β†’ B ∈ A β†’ C ∈ B β†’ C ∈ A

has-transitive-members : 𝕍 β†’ 𝓀⁺ Μ‡
has-transitive-members A = (B : 𝕍) β†’ B ∈ A β†’ is-transitive-iset B

being-transitive-iset-is-prop : (A : 𝕍) β†’ is-prop (is-transitive-iset A)
being-transitive-iset-is-prop A = Ξ β‚„-is-prop fe (Ξ» B C l m β†’ ∈-is-prop-valued C A)

having-transitive-members-is-prop : (A : 𝕍) β†’ is-prop (has-transitive-members A)
having-transitive-members-is-prop A =
 Ξ β‚‚-is-prop fe (Ξ» B l β†’ being-transitive-iset-is-prop B)

is-iterative-ordinal : 𝕍 β†’ 𝓀⁺ Μ‡
is-iterative-ordinal A = is-transitive-iset A
                       Γ— has-transitive-members A

being-iordinal-is-prop : (A : 𝕍) β†’ is-prop (is-iterative-ordinal A)
being-iordinal-is-prop A = Γ—-is-prop
                            (being-transitive-iset-is-prop A)
                            (having-transitive-members-is-prop A)
\end{code}

We name the projections for the sake of clarity:

\begin{code}

iordinals-are-transitive : (A : 𝕍)
                         β†’ is-iterative-ordinal A
                         β†’ is-transitive-iset A
iordinals-are-transitive A = pr₁

members-of-iordinals-are-transitive : (A : 𝕍)
                                    β†’ is-iterative-ordinal A
                                    β†’ has-transitive-members A
members-of-iordinals-are-transitive A = prβ‚‚

\end{code}

Although the definition of iterative ordinal is not by induction, it
does satisfy the following inductive characterization, which we use a
number of times.

\begin{code}

ordinal-is-hereditary : (A B : 𝕍)
                      β†’ B ∈ A
                      β†’ is-iterative-ordinal A
                      β†’ is-iterative-ordinal B
ordinal-is-hereditary A B B-in-A (A-trans , A-members-trans) = III
 where
  I : is-transitive-iset B
  I = A-members-trans B B-in-A

  II : (C : 𝕍) β†’ C ∈ B β†’ is-transitive-iset C
  II C C-in-B = A-members-trans C (A-trans B C B-in-A C-in-B)

  III : is-iterative-ordinal B
  III = I , II

\end{code}

The type of iterative ordinals is defined as a subtype of that of
iterative sets.

\begin{code}

𝕆 : 𝓀⁺ Μ‡
𝕆 = Ξ£ A κž‰ 𝕍 , is-iterative-ordinal A

𝕆-is-locally-small : is-locally-small 𝕆
𝕆-is-locally-small = subtype-is-locally-small
                      being-iordinal-is-prop
                      𝕍-is-locally-small
\end{code}

We again name the projections for the sake of clarity:

\begin{code}

underlying-iset : 𝕆 β†’ 𝕍
underlying-iset = pr₁

underlying-iset-is-iordinal : (Ξ± : 𝕆) β†’ is-iterative-ordinal (underlying-iset Ξ±)
underlying-iset-is-iordinal = prβ‚‚

\end{code}

Because the notion of iterative ordinal is property, we get that 𝕆 is
a subtype of 𝕍.

\begin{code}

underlying-iset-is-embedding : is-embedding underlying-iset
underlying-iset-is-embedding = pr₁-is-embedding being-iordinal-is-prop

𝕆-is-set : is-set 𝕆
𝕆-is-set = subtypes-of-sets-are-sets
            underlying-iset
            underlying-iset-is-embedding
            𝕍-is-set

\end{code}

We define the less-than relation on ordinals to be the membership
relation, as in material set theory under von Newmann's encoding:

\begin{code}

_<_ : 𝕆 β†’ 𝕆 β†’ 𝓀⁺ Μ‡
α < β = underlying-iset α ∈ underlying-iset β

\end{code}

As is the case for iterative sets, there is a resized down, equivalent
definition of the less-than relation on ordinals:

\begin{code}

_<⁻_ : 𝕆 β†’ 𝕆 β†’ 𝓀 Μ‡
α <⁻ β = underlying-iset α ∈⁻ underlying-iset β

<⁻≃-< : (Ξ± Ξ² : 𝕆) β†’ (Ξ± < Ξ²) ≃ (Ξ± <⁻ Ξ²)
<⁻≃-< Ξ± Ξ² = βˆˆβ»β‰ƒβˆˆ (underlying-iset Ξ±) (underlying-iset Ξ²)

<-is-prop-valued : (Ξ± Ξ² : 𝕆) β†’ is-prop (Ξ± < Ξ²)
<-is-prop-valued α β = ∈-is-prop-valued (underlying-iset α) (underlying-iset β)

\end{code}

We now show that this order is transitive, extensional and accessible,
so that 𝕆 is an ordinal in the sense of the HoTT book. This result is
in de Jong, Kraus, Nordvall Forsberg and Xu [5]. As discussed above,
the difference is that [5] uses the higher-inductive definition of 𝕍.

\begin{code}

<-is-transitive : (Ξ± Ξ² Ξ³ : 𝕆) β†’ Ξ± < Ξ² β†’ Ξ² < Ξ³ β†’ Ξ± < Ξ³
<-is-transitive (A , _) (B , _) (C , C-trans , _) u v = I
 where
  I : A ∈ C
  I = C-trans B A v u

_≀_ : 𝕆 β†’ 𝕆 β†’ 𝓀⁺ Μ‡
Ξ± ≀ Ξ² = βˆ€ Ξ³ β†’ Ξ³ < Ξ± β†’ Ξ³ < Ξ²

βŠ†-gives-≀ : (Ξ± Ξ² : 𝕆)
          β†’ underlying-iset Ξ± βŠ† underlying-iset Ξ²
          β†’ Ξ± ≀ Ξ²
βŠ†-gives-≀ Ξ± Ξ² u Ξ³ = u (underlying-iset Ξ³)

≀-gives-βŠ† : (Ξ± Ξ² : 𝕆)
          β†’ Ξ± ≀ Ξ²
          β†’ underlying-iset Ξ± βŠ† underlying-iset Ξ²
≀-gives-βŠ† Ξ±@(A , A-is-iord) Ξ²@(B , _) u = I
 where
  I : A βŠ† B
  I C C-in-A = I₃
   where
    C-is-iord : is-iterative-ordinal C
    C-is-iord = ordinal-is-hereditary A C C-in-A A-is-iord

    I₁ : is-transitive-iset C
    I₁ = iordinals-are-transitive C C-is-iord

    Iβ‚‚ : (B : 𝕍) β†’ B ∈ C β†’ is-transitive-iset B
    Iβ‚‚ = members-of-iordinals-are-transitive C C-is-iord

    I₃ : C ∈ B
    I₃ = u (C , I₁ , Iβ‚‚) C-in-A

\end{code}

We now define root and forest "destructors" for the type 𝕆. This will
give rise to an inductive characterization of 𝕆 which doesn't mention
𝕄 or 𝕍, which seems to be new.

\begin{code}

𝕆-root : 𝕆 β†’ 𝓀 Μ‡
𝕆-root Ξ± = 𝕍-root (underlying-iset Ξ±)

𝕆-forest : (Ξ± : 𝕆) β†’ 𝕆-root Ξ± β†’ 𝕆
𝕆-forest Ξ± x = 𝕍-forest A x ,
               ordinal-is-hereditary
                A
                (𝕍-forest A x)
                (𝕍-forest-∈ A x)
                (underlying-iset-is-iordinal Ξ±)
 where
  A = underlying-iset Ξ±

\end{code}

By definition, any (immediate) subtree of Ξ± is less than Ξ±:

\begin{code}

𝕆-forest-< : (Ξ± : 𝕆) (x : 𝕆-root Ξ±) β†’ 𝕆-forest Ξ± x < Ξ±
𝕆-forest-< Ξ± = 𝕍-forest-∈ (underlying-iset Ξ±)

\end{code}

The 𝕆-forest map is an embedding, which follows from the fact that the
𝕍-forest map is an embedding:

\begin{code}

𝕆-forest-is-embedding : (Ξ± : 𝕆) β†’ is-embedding (𝕆-forest Ξ±)
𝕆-forest-is-embedding Ξ±@(A@(ssup _ _ , _) , _) =
 pair-fun-is-embedding-special
  (underlying-iset ∘ 𝕆-forest Ξ±)
  (underlying-iset-is-iordinal ∘ 𝕆-forest Ξ±)
  (𝕍-forest-is-embedding A)
  being-iordinal-is-prop

\end{code}

The antisymmetry of the ≀ relation, which amounts to the
extensionality condition:

\begin{code}

≀-is-antisymmetric : (Ξ± Ξ² : 𝕆) β†’ Ξ± ≀ Ξ² β†’ Ξ² ≀ Ξ± β†’ Ξ± = Ξ²
≀-is-antisymmetric Ξ±@(A , _) Ξ²@(B , _) u v = II
 where
  I : A = B
  I = ∈-is-extensional A B (≀-gives-βŠ† Ξ± Ξ² u) (≀-gives-βŠ† Ξ² Ξ± v)

  II : α = β
  II = to-subtype-= (being-iordinal-is-prop) I

<-is-extensional : is-extensional _<_
<-is-extensional = ≀-is-antisymmetric

\end{code}

A characterization of the < relation:

\begin{code}

<-behaviour : (Ξ± Ξ² : 𝕆)
            β†’ (Ξ± < Ξ²) ≃ (Ξ£ y κž‰ 𝕆-root Ξ² , 𝕆-forest Ξ² y = Ξ±)
<-behaviour Ξ±@(A@(M , _) , _) Ξ²@(B@(N@(ssup Y Ξ³) , _) , _) = II
 where
  I : (y : Y) β†’ (Ξ³ y = M) ≃ (𝕆-forest Ξ² y = Ξ±)
  I y = (Ξ³ y = M)          β‰ƒβŸ¨ I₁ ⟩
        (𝕍-forest B y = A) β‰ƒβŸ¨ Iβ‚‚ ⟩
        (𝕆-forest Ξ² y = Ξ±) β– 
         where
          I₁ = embedding-criterion-converse
                underlying-mset
                underlying-mset-is-embedding
                (𝕍-forest B y)
                A
          Iβ‚‚ = embedding-criterion-converse
                underlying-iset
                underlying-iset-is-embedding
                (𝕆-forest Ξ² y)
                Ξ±

  II : (Ξ£ y κž‰ Y , Ξ³ y = M) ≃ (Ξ£ y κž‰ Y , 𝕆-forest Ξ² y = Ξ±)
  II = Ξ£-cong I

\end{code}

The 𝕆-forest map is lower closed:

\begin{code}

is-lower-closed : {X : 𝓀 Μ‡ } β†’ (X β†’ 𝕆) β†’ 𝓀⁺ Μ‡
is-lower-closed {X} Ο• = (Ξ² : 𝕆) (x : X) β†’ Ξ² < Ο• x β†’ Ξ£ y κž‰ X , Ο• y = Ξ²

being-lower-closed-is-prop : {X : 𝓀 Μ‡ } (Ο• : X β†’ 𝕆)
                           β†’ is-embedding Ο•
                           β†’ is-prop (is-lower-closed Ο•)
being-lower-closed-is-prop Ο• e = Π₃-is-prop fe (Ξ» Ξ² _ _ β†’ e Ξ²)

𝕆-forest-is-lower-closed : (Ξ± : 𝕆) β†’ is-lower-closed (𝕆-forest Ξ±)
𝕆-forest-is-lower-closed Ξ± Ξ² x l = VII
 where
  have-l : Ξ² < 𝕆-forest Ξ± x
  have-l = l

  I : 𝕆-forest Ξ± x < Ξ±
  I = 𝕆-forest-< Ξ± x

  II : Ξ² < Ξ±
  II = <-is-transitive Ξ² (𝕆-forest Ξ± x) Ξ± l I

  VII : Ξ£ y κž‰ 𝕆-root Ξ± , 𝕆-forest Ξ± y = Ξ²
  VII = ⌜ <-behaviour β α ⌝ II

\end{code}

The "constructor" of elements of 𝕆:

\begin{code}

𝕆-ssup : (X : 𝓀 Μ‡ ) (Ο• : X β†’ 𝕆) β†’ is-embedding Ο• β†’ is-lower-closed Ο• β†’ 𝕆
𝕆-ssup X Ο• Ο•-emb Ο•-lower = A , A-is-iord
 where
  Ο† : X β†’ 𝕍
  Ο† = underlying-iset ∘ Ο•

  Ο†-iter : (x : X) β†’ is-iterative-ordinal (Ο† x)
  Ο†-iter = underlying-iset-is-iordinal ∘ Ο•

  Ο†-emb : is-embedding Ο†
  Ο†-emb = ∘-is-embedding Ο•-emb underlying-iset-is-embedding

  A : 𝕍
  A = 𝕍-ssup X Ο† Ο†-emb

  A-behaviour : (B : 𝕍) β†’ B ∈ A ≃ (Ξ£ x κž‰ X , Ο† x = B)
  A-behaviour B = ∈-behaviour B X Ο† Ο†-emb

  I : (B : 𝕍) β†’ B ∈ A β†’ is-iterative-ordinal B
  I B B-in-A = I₁
   where
    Iβ‚€ : Ξ£ x κž‰ X , Ο† x = B
    Iβ‚€ = ⌜ A-behaviour B ⌝ B-in-A

    I₁ : is-iterative-ordinal B
    I₁ = transport is-iterative-ordinal (prβ‚‚ Iβ‚€) (Ο†-iter (pr₁ Iβ‚€))

  II :  (B C : 𝕍) β†’ B ∈ A β†’ C ∈ B β†’ C ∈ A
  II B C B-in-A C-in-B = IIβ‚…
   where
    x : X
    x = pr₁ (⌜ A-behaviour B ⌝ B-in-A)

    p : Ο† x = B
    p = prβ‚‚ (⌜ A-behaviour B ⌝ B-in-A)

    Ξ² Ξ³ : 𝕆
    Ξ² = (B , I B B-in-A)
    Ξ³ = (C , ordinal-is-hereditary B C C-in-B (I B B-in-A))

    IIβ‚€ : Ξ³ < Ξ²
    IIβ‚€ = C-in-B

    q : Ο• x = Ξ²
    q = embeddings-are-lc underlying-iset underlying-iset-is-embedding p

    II₁ : Ξ³ < Ο• x
    II₁ = transport (Ξ³ <_) (q ⁻¹) IIβ‚€

    IIβ‚‚ : Ξ£ y κž‰ X , Ο• y = Ξ³
    IIβ‚‚ = Ο•-lower Ξ³ x II₁

    II₃ : type-of IIβ‚‚ β†’ Ξ£ y κž‰ X , Ο† y = C
    II₃ (y , p) = y , ap underlying-iset p

    IIβ‚„ : Ξ£ x κž‰ X , underlying-mset (Ο† x) = underlying-mset C
    IIβ‚„ = ⌜ A-behaviour C ⌝⁻¹ (II₃ IIβ‚‚)

    IIβ‚… : C ∈ A
    IIβ‚… = IIβ‚„

  III : (B : 𝕍) β†’ B ∈ A β†’ is-transitive-iset B
  III B m = iordinals-are-transitive B (I B m)

  A-is-iord : is-iterative-ordinal A
  A-is-iord = II , III

\end{code}

It satisfies the expected properties with respect to the destructors:

\begin{code}

𝕆-ssup-root : (X : 𝓀 Μ‡ )
              (Ο• : X β†’ 𝕆) (e : is-embedding Ο•) (l : is-lower-closed Ο•)
            β†’ 𝕆-root (𝕆-ssup X Ο• e l) = X
𝕆-ssup-root X Ο• e l = refl

𝕆-ssup-forest : (X : 𝓀 Μ‡ )
                (Ο• : X β†’ 𝕆) (e : is-embedding Ο•) (l : is-lower-closed Ο•)
              β†’ 𝕆-forest (𝕆-ssup X Ο• e l) ∼ Ο•
𝕆-ssup-forest X Ο• e l x = to-subtype-= being-iordinal-is-prop refl

𝕆-Ξ·' : (Ξ± : 𝕆) (e : is-embedding (𝕆-forest Ξ±)) (l : is-lower-closed (𝕆-forest Ξ±))
     β†’ 𝕆-ssup (𝕆-root Ξ±) (𝕆-forest Ξ±) e l = Ξ±
𝕆-Ξ·' (A@(ssup _ _ , _) , _) _ _ = to-subtype-= being-iordinal-is-prop (𝕍-Ξ·' A _)

𝕆-Ξ· : (Ξ± : 𝕆)
    β†’ 𝕆-ssup (𝕆-root Ξ±)
             (𝕆-forest Ξ±)
             (𝕆-forest-is-embedding Ξ±)
             (𝕆-forest-is-lower-closed Ξ±)
    = α
𝕆-Ξ· A =  𝕆-Ξ·' A (𝕆-forest-is-embedding A) (𝕆-forest-is-lower-closed A)

\end{code}

A criterion for equality in 𝕆, which is much less general than we can
have, but enough for our purposes for the moment:

\begin{code}

to-𝕆-=-special : (X : 𝓀 Μ‡ ) (Ο• Ο•' : X β†’ 𝕆)
                  (e  : is-embedding Ο• ) (l  : is-lower-closed Ο• )
                  (e' : is-embedding Ο•') (l' : is-lower-closed Ο•')
                β†’ Ο• = Ο•'
                β†’ 𝕆-ssup X Ο• e l = 𝕆-ssup X Ο•' e' l'
to-𝕆-=-special X Ο• Ο•' e l e' l' refl = to-subtype-=
                                         being-iordinal-is-prop
                                         (to-subtype-=
                                           being-iset-is-prop
                                           refl)
\end{code}

We now justify our notation "ssup" in comparison with the more
traditional notation "sup" for the constructors.

𝕆-ssup X Ο• e l is the unique iterative ordinal whose predecessors are
precisely the members of the family Ο•, which is known as the strict
supremum (or successor supremum, or strong supremum) of Ο•, and is also
its rank in the sense of set theory.

\begin{code}

𝕆-ssup-behaviour : (X : 𝓀 Μ‡ )
                   (Ο• : X β†’ 𝕆) (e : is-embedding Ο•) (l : is-lower-closed Ο•)
                   (Ξ± : 𝕆)
                 β†’ (Ξ± < 𝕆-ssup X Ο• e l) ≃ (Ξ£ x κž‰ X , Ο• x = Ξ±)
𝕆-ssup-behaviour X Ο• e l Ξ± =
 (Ξ± < 𝕆-ssup X Ο• e l)                         β‰ƒβŸ¨ I ⟩
 (Ξ£ x κž‰ X , 𝕆-forest (𝕆-ssup X Ο• e l) x = Ξ±) β‰ƒβŸ¨ II ⟩
 (Ξ£ x κž‰ X , Ο• x = Ξ±)                         β– 
 where
  I  = <-behaviour Ξ± (𝕆-ssup X Ο• e l)
  II = Ξ£-cong (Ξ» x β†’ =-cong-l _ _ (𝕆-ssup-forest X Ο• e l x))

\end{code}

We now discuss various equivalent induction principles on 𝕆.

\begin{code}

𝕆-induction' : (P : 𝕆 β†’ π“₯ Μ‡ )
             β†’ ((Ξ± : 𝕆) β†’ ((x : 𝕆-root Ξ±) β†’ P (𝕆-forest Ξ± x)) β†’ P Ξ±)
             β†’ (Ξ± : 𝕆) β†’ P Ξ±
𝕆-induction' P f ((M , M-is-iset) , M-is-iord) = h M M-is-iset M-is-iord
 where
  h : (M : 𝕄)
      (M-is-iset : is-iterative-set M)
      (M-is-iord : is-iterative-ordinal (M , M-is-iset))
    β†’ P ((M , M-is-iset) , M-is-iord)
  h M@(ssup X Ο†) M-is-iset@(Ο†-emb , Ο†-iter) M-is-iord = I
   where
    Ξ± : 𝕆
    Ξ± = (M , M-is-iset) , M-is-iord

    IH : (x : X) β†’ P (𝕆-forest Ξ± x)
    IH x = h (Ο† x)
             (Ο†-iter x)
             (ordinal-is-hereditary
               (M , M-is-iset)
               (Ο† x , Ο†-iter x)
               (𝕄-forest-⁅ M x)
               M-is-iord)
    I : P Ξ±
    I = f Ξ± IH

𝕆-recursion' : (P : π“₯ Μ‡ )
             β†’ ((Ξ± : 𝕆) β†’ (𝕆-root Ξ± β†’ P) β†’ P)
             β†’ 𝕆 β†’ P
𝕆-recursion' P = 𝕆-induction' (Ξ» _ β†’ P)

\end{code}

TODO. Do things get nicer if use use induction on 𝕍 rather than 𝕄 in
the above proof?

It would be nice if we could define 𝕆 inductively as follows:

 data 𝕆 : 𝓀⁺ Μ‡ where
  ssup : (X : 𝓀 Μ‡ ) (Ο† : X β†’ 𝕆) β†’ is-embedding Ο† β†’ is-lower-closed Ο† β†’ 𝕆

However, this is not a strictly positive definition, for the criterion
of strict positivity adopted by Agda, and so it is not accepted.

Nevertheless, all iterative ordinals *are* generated by the
"constructor" 𝕆-ssup, in the following sense, so that we can view 𝕆 as
really inductively defined by the above data declaration, which seems
to be a new result. This is close to von Neumann's definition, with
the difference that we don't require Ο† to be well-ordered - this
follows automatically.

\begin{code}

𝕆-induction : (P : 𝕆 β†’ π“₯ Μ‡ )
            β†’ ((X : 𝓀 Μ‡ ) (Ο• : X β†’ 𝕆) (e : is-embedding Ο•) (l : is-lower-closed Ο•)
                  β†’ ((x : X) β†’ P (Ο• x))
                  β†’ P (𝕆-ssup X Ο• e l))
            β†’ (Ξ± : 𝕆) β†’ P Ξ±
𝕆-induction P f = 𝕆-induction' P f'
 where
  f' : (Ξ± : 𝕆) β†’ ((x : 𝕆-root Ξ±) β†’ P (𝕆-forest Ξ± x)) β†’ P Ξ±
  f' Ξ± IH = transport P (𝕆-Ξ· Ξ±) I
   where
    I : P (𝕆-ssup (𝕆-root Ξ±)
                  (𝕆-forest Ξ±)
                  (𝕆-forest-is-embedding Ξ±)
                  (𝕆-forest-is-lower-closed Ξ±))
    I = f (𝕆-root Ξ±)
          (𝕆-forest Ξ±)
          (𝕆-forest-is-embedding Ξ±)
          (𝕆-forest-is-lower-closed Ξ±)
          IH

𝕆-recursion : (P : π“₯ Μ‡ )
            β†’ ((X : 𝓀 Μ‡ ) (Ο• : X β†’ 𝕆)
                  β†’ is-embedding Ο•
                  β†’ is-lower-closed Ο•
                  β†’ (X β†’ P)
                  β†’ P)
            β†’ 𝕆 β†’ P
𝕆-recursion P f = 𝕆-induction (Ξ» _ β†’ P) (Ξ» X Ο• e l s β†’ f X Ο• e l s)

𝕆-iteration : (P : π“₯ Μ‡ )
            β†’ ((X : 𝓀 Μ‡ ) β†’ (X β†’ P) β†’ P)
            β†’ 𝕆 β†’ P
𝕆-iteration P f = 𝕆-induction (Ξ» _ β†’ P) (Ξ» X Ο• e l β†’ f X)

\end{code}

The usual induction principle for ordinals follows directly from the
above form of induction.

\begin{code}

<-induction : (P : 𝕆 β†’ π“₯ Μ‡ )
            β†’ ((Ξ± : 𝕆) β†’ ((Ξ² : 𝕆) β†’ Ξ² < Ξ± β†’ P Ξ²) β†’ P Ξ±)
            β†’ (Ξ± : 𝕆) β†’ P Ξ±
<-induction P IH = 𝕆-induction P f
 where
  f : (X : 𝓀 Μ‡ ) (Ο• : X β†’ 𝕆) (e : is-embedding Ο•) (l : is-lower-closed Ο•)
    β†’ ((x : X) β†’ P (Ο• x))
    β†’ P (𝕆-ssup X Ο• e l)
  f X Ο• e l u = IH Ξ± s
   where
    Ξ± : 𝕆
    Ξ± = 𝕆-ssup X Ο• e l

    s : (Ξ² : 𝕆) β†’ Ξ² < Ξ± β†’ P Ξ²
    s Ξ²@((.(underlying-mset (underlying-iset (Ο• x))) , is) , io) (x , refl) = III
     where
      I : P (Ο• x)
      I = u x

      II : Ο• x = Ξ²
      II = to-subtype-=
            being-iordinal-is-prop
             (to-subtype-= being-iset-is-prop refl)

      III : P Ξ²
      III = transport P II I

\end{code}

Which in turn gives the accessibility of the order directly:

\begin{code}

<-is-accessible : (Ξ± : 𝕆) β†’ is-accessible _<_ Ξ±
<-is-accessible = <-induction (is-accessible _<_) (Ξ» _ β†’ acc)

\end{code}

Putting the above together we conclude that the type of iterative
ordinals has the structure of an ordinal in the sense of the HoTT
book, which, as mentioned above, had already been proved in [5].

\begin{code}

π“ž : Ordinal 𝓀⁺
π“ž = 𝕆 ,
    _<_ ,
    <-is-prop-valued ,
    <-is-accessible ,
    <-is-extensional ,
    <-is-transitive

\end{code}

We now want to show that π“ž is equivalent to the "ordinal of ordinals"
in the sense of the HoTT book.

Every iterative ordinal can be mapped to a HoTT-book ordinal, by
taking the root of the iterative ordinal to be the underlying set of
the HoTT-book ordinal. This is similar to [5, Proposition 43], with
the difference that we don't need to quotient because the forest is an
embedding in our conception of iterative ordinals. There is also a
minor improvement in avoiding propositional truncations in the
definition of the order on the root of Ξ±, which, together with the
avoidance of quotients, makes the proof more direct.

\begin{code}

Ord : 𝓀⁺ Μ‡
Ord = Ordinal 𝓀

𝕆-to-Ord : 𝕆 β†’ Ord
𝕆-to-Ord Ξ± = Ξ±'
 where
  X : 𝓀 Μ‡
  X = 𝕆-root Ξ±

  _β‰Ί_ :  X β†’ X β†’ 𝓀⁺ Μ‡
  x β‰Ί y = 𝕆-forest Ξ± x < 𝕆-forest Ξ± y

  _βŠ‘_ :  X β†’ X β†’ 𝓀⁺ Μ‡
  x βŠ‘ y = βˆ€ z β†’ z β‰Ί x β†’ z β‰Ί y

  βŠ‘-gives-≀ : (x y : X) β†’ x βŠ‘ y β†’ 𝕆-forest Ξ± x ≀ 𝕆-forest Ξ± y
  βŠ‘-gives-≀ x y l Ξ² m = IV
   where
    I : Ξ£ z κž‰ X , 𝕆-forest Ξ± z = Ξ²
    I = 𝕆-forest-is-lower-closed Ξ± Ξ² x m

    II : pr₁ I β‰Ί x
    II = transport⁻¹ (_< 𝕆-forest Ξ± x) (prβ‚‚ I) m

    III : pr₁ I β‰Ί y
    III = l (pr₁ I) II

    IV : Ξ² < (𝕆-forest Ξ± y)
    IV = transport (_< (𝕆-forest Ξ± y)) (prβ‚‚ I) III

  ≀-gives-βŠ‘ : (x y : X) β†’ 𝕆-forest Ξ± x ≀ 𝕆-forest Ξ± y β†’ x βŠ‘ y
  ≀-gives-βŠ‘ x y l z = l (𝕆-forest Ξ± z)

  β‰Ί-is-prop-valued : (x y : X) β†’ is-prop (x β‰Ί y)
  β‰Ί-is-prop-valued x y = <-is-prop-valued (𝕆-forest Ξ± x) (𝕆-forest Ξ± y)

  β‰Ί-is-accessible : (x : X) β†’ is-accessible _β‰Ί_ x
  β‰Ί-is-accessible x = f x (<-is-accessible (𝕆-forest Ξ± x))
   where
    f : (x : X) β†’ is-accessible _<_ (𝕆-forest Ξ± x) β†’ is-accessible _β‰Ί_ x
    f x (acc u) = acc (Ξ» y l β†’ f y (u (𝕆-forest Ξ± y) l))

  β‰Ί-is-extensional : is-extensional _β‰Ί_
  β‰Ί-is-extensional x y u v = II
   where
    I : 𝕆-forest Ξ± x = 𝕆-forest Ξ± y
    I = <-is-extensional _ _ (βŠ‘-gives-≀ x y u) (βŠ‘-gives-≀ y x v)

    II : x = y
    II = embeddings-are-lc (𝕆-forest Ξ±) (𝕆-forest-is-embedding Ξ±) I

  β‰Ί-is-transitive : is-transitive _β‰Ί_
  β‰Ί-is-transitive x y z = <-is-transitive
                           (𝕆-forest Ξ± x)
                           (𝕆-forest Ξ± y)
                           (𝕆-forest Ξ± z)

  β‰Ί-is-well-order : is-well-order _β‰Ί_
  β‰Ί-is-well-order = β‰Ί-is-prop-valued ,
                    β‰Ί-is-accessible ,
                    β‰Ί-is-extensional ,
                    β‰Ί-is-transitive

  _≺⁻_ :  X β†’ X β†’ 𝓀 Μ‡
  x ≺⁻ y = (𝕆-forest Ξ± x) <⁻ (𝕆-forest Ξ± y)

  ≺⁻≃-β‰Ί : (x y : X) β†’ (x β‰Ί y) ≃ (x ≺⁻ y)
  ≺⁻≃-β‰Ί x y = <⁻≃-< (𝕆-forest Ξ± x) (𝕆-forest Ξ± y)

  ≺⁻-is-well-order : is-well-order _≺⁻_
  ≺⁻-is-well-order = order-transfer-lemma₃.well-order←
                      fe'
                      X
                      _≺⁻_
                      _β‰Ί_
                      (Ξ» x y β†’ ≃-sym (≺⁻≃-β‰Ί x y))
                      β‰Ί-is-well-order
  Ξ±' : Ord
  Ξ±' = X , _≺⁻_ , ≺⁻-is-well-order

\end{code}

The order of 𝕆-to-Ord Ξ± is characterized as follows in terms of the
order of 𝕆, by definition:

\begin{code}

𝕆-to-Ord-order : (Ξ± : 𝕆) (x y : ⟨ 𝕆-to-Ord Ξ± ⟩)
               β†’ (𝕆-forest Ξ± x < 𝕆-forest Ξ± y) ≃ (x β‰ΊβŸ¨ 𝕆-to-Ord Ξ± ⟩ y)
𝕆-to-Ord-order Ξ± x y = <⁻≃-< (𝕆-forest Ξ± x) (𝕆-forest Ξ± y)

\end{code}

We now define the map in the other direction, essentially in the same
way as in [5], in several steps, but we start mapping into 𝕄 rather
than into 𝕍 directly. In any case, 𝕄 doesn't occur in the set-up of [5].

\begin{code}

Ord-to-𝕄 : Ord β†’ 𝕄
Ord-to-𝕄 = transfinite-recursion-on-OO 𝕄 (Ξ» Ξ± β†’ ssup ⟨ Ξ± ⟩)

\end{code}

This is characterized by the following recursive definition,
where Ξ± ↓ x denotes the sub-ordinal of Ξ± consisting of the
elements below x.

\begin{code}

Ord-to-𝕄-behaviour : (Ξ± : Ord)
                   β†’ Ord-to-𝕄 Ξ± = ssup ⟨ Ξ± ⟩ (Ξ» (x : ⟨ Ξ± ⟩) β†’ Ord-to-𝕄 (Ξ± ↓ x))
Ord-to-𝕄-behaviour = transfinite-recursion-on-OO-behaviour 𝕄 (Ξ» Ξ± β†’ ssup ⟨ Ξ± ⟩)

\end{code}

This map is left cancellable and we will later conclude from this fact
that it is actually an embedding.

\begin{code}

Ord-to-𝕄-is-lc : left-cancellable Ord-to-𝕄
Ord-to-𝕄-is-lc {Ξ±} {Ξ²} = transfinite-induction-on-OO _ f Ξ± Ξ²
 where
  f : (Ξ± : Ord)
    β†’ ((a : ⟨ Ξ± ⟩) (Ξ² : Ord) β†’ Ord-to-𝕄 (Ξ± ↓ a) = Ord-to-𝕄 Ξ² β†’ (Ξ± ↓ a) = Ξ²)
    β†’ (Ξ² : Ord) β†’ Ord-to-𝕄 Ξ± = Ord-to-𝕄 Ξ² β†’ Ξ± = Ξ²
  f Ξ± IH Ξ² p = VII
   where
    I : ssup ⟨ Ξ± ⟩ (Ξ» (a : ⟨ Ξ± ⟩) β†’ Ord-to-𝕄 (Ξ± ↓ a))
     = ssup ⟨ Ξ² ⟩ (Ξ» (b : ⟨ Ξ² ⟩) β†’ Ord-to-𝕄 (Ξ² ↓ b))
    I = transportβ‚‚ (_=_) (Ord-to-𝕄-behaviour Ξ±) (Ord-to-𝕄-behaviour Ξ²) p

    II : ⟨ α ⟩ = ⟨ β ⟩
    II = pr₁ (from-𝕄-= I)

    III : (a : ⟨ Ξ± ⟩) β†’ Ord-to-𝕄 (Ξ± ↓ a) = Ord-to-𝕄 (Ξ² ↓ Idtofun II a)
    III = happly (prβ‚‚ (from-𝕄-= I))

    IV : (a : ⟨ Ξ± ⟩) β†’ (Ξ± ↓ a) = (Ξ² ↓ Idtofun II a)
    IV a = IH a (Ξ² ↓ Idtofun II a) (III a)

    V : (a : ⟨ Ξ± ⟩) β†’ (Ξ± ↓ a) ⊲ Ξ²
    V a = Idtofun II a , IV a

    VI : Ξ± β‰Ό Ξ²
    VI = to-β‰Ό V

    II' : ⟨ β ⟩ = ⟨ α ⟩
    II' = pr₁ (from-𝕄-= (I ⁻¹))

    III' : (b : ⟨ Ξ² ⟩) β†’ Ord-to-𝕄 (Ξ² ↓ b) = Ord-to-𝕄 (Ξ± ↓ Idtofun II' b)
    III' = happly (prβ‚‚ (from-𝕄-= (I ⁻¹)))

    IV' : (b : ⟨ Ξ² ⟩) β†’ (Ξ² ↓ b) = (Ξ± ↓ Idtofun II' b)
    IV' b = (IH (Idtofun II' b) (Ξ² ↓ b) ((III' b)⁻¹))⁻¹

    V' : (b : ⟨ Ξ² ⟩) β†’ (Ξ² ↓ b) ⊲ Ξ±
    V' b = Idtofun II' b , IV' b

    VI' : Ξ² β‰Ό Ξ±
    VI' = to-β‰Ό V'

    VII : α = β
    VII = Extensionality (OO 𝓀) Ξ± Ξ² VI VI'

\end{code}

Using this we can show that the elements in the image of Ord-to-𝕄 are
iterative sets, by induction on the ordinal of ordinals in the sense
of the HoTT book.

\begin{code}

Ord-to-𝕄-is-iset : (Ξ± : Ord) β†’ is-iterative-set (Ord-to-𝕄 Ξ±)
Ord-to-𝕄-is-iset = transfinite-induction-on-OO _ f
 where
  f : (Ξ± : Ord)
    β†’ ((x : ⟨ Ξ± ⟩) β†’ is-iterative-set (Ord-to-𝕄 (Ξ± ↓ x)))
    β†’ is-iterative-set (Ord-to-𝕄 Ξ±)
  f Ξ± IH = transport⁻¹ is-iterative-set (Ord-to-𝕄-behaviour Ξ±) I
   where
    I : is-iterative-set (ssup ⟨ Ξ± ⟩ (Ξ» (x : ⟨ Ξ± ⟩) β†’ Ord-to-𝕄 (Ξ± ↓ x)))
    I = II , IH
     where
      II : is-embedding (Ξ» x β†’ Ord-to-𝕄 (Ξ± ↓ x))
      II M = III
       where
        III : is-prop (Ξ£ a κž‰ ⟨ Ξ± ⟩ , Ord-to-𝕄 (Ξ± ↓ a) = M)
        III (a , p) (b , q) = VI
         where
          IV : Ξ± ↓ a = Ξ± ↓ b
          IV = Ord-to-𝕄-is-lc
                (Ord-to-𝕄 (Ξ± ↓ a) =⟨ p ⟩
                 M                =⟨ q ⁻¹ ⟩
                 Ord-to-𝕄 (Ξ± ↓ b) ∎)

          V : a = b
          V = ↓-lc Ξ± a b IV

          VI : (a , p) = (b , q)
          VI = to-subtype-=
                (Ξ» x β†’ isets-are-h-isolated (Ord-to-𝕄 (Ξ± ↓ x)) (IH x))
                V
\end{code}

So we get a map Ord β†’ 𝕍 from the above map Ord β†’ 𝕄.

\begin{code}

Ord-to-𝕍 : Ord β†’ 𝕍
Ord-to-𝕍 Ξ± = Ord-to-𝕄 Ξ± , Ord-to-𝕄-is-iset Ξ±

\end{code}

The next step is to get a map Ord β†’ 𝕆 from this map Ord-to-𝕍.

We have the definitionally commutative triangle

                Ord-to-𝕍
             Ord ------> 𝕍
               \       /
                \     /
       Ord-to-𝕄 \    / underlying-mset
                  \ /
                   v
                   𝕄

We previously showed that Ord-to-𝕄 is left cancellable. Hence Ord-to-𝕍
is left cancellable as well. But 𝕍 is a 0-type, so Ord-to-𝕍 is
actually an embedding. Finally, the map underlying-mset is an
embedding, as 𝕍 is a subtype of 𝕄, so Ord-to-𝕄 is a composition of
embeddings, and therefore an embedding itself.

\begin{code}

private
 commutative-triangle : Ord-to-𝕄 = underlying-mset ∘ Ord-to-𝕍
 commutative-triangle = refl

Ord-to-𝕍-is-embedding : is-embedding Ord-to-𝕍
Ord-to-𝕍-is-embedding = lc-maps-into-sets-are-embeddings
                         Ord-to-𝕍
                         (factor-is-lc
                           Ord-to-𝕍
                           underlying-mset
                           Ord-to-𝕄-is-lc)
                         𝕍-is-set

Ord-to-𝕄-is-embedding : is-embedding Ord-to-𝕄
Ord-to-𝕄-is-embedding = ∘-is-embedding
                          Ord-to-𝕍-is-embedding
                          underlying-mset-is-embedding

Ord-to-𝕍↓-is-embedding : (Ξ± : Ord) β†’ is-embedding (Ξ» x β†’ Ord-to-𝕍 (Ξ± ↓ x))
Ord-to-𝕍↓-is-embedding Ξ± = ∘-is-embedding
                            (↓-is-embedding Ξ±)
                            Ord-to-𝕍-is-embedding
\end{code}

The fact that Ord-to-𝕄 is an embedding seems to be new, and it is
interesting because 𝕄 is not a 0-type.

The following gives a recursive characterization of Ord-to-𝕍:

\begin{code}

Ord-to-𝕍-behaviour : (Ξ± : Ord)
                   β†’ Ord-to-𝕍 Ξ± = 𝕍-ssup ⟨ Ξ± ⟩
                                    (Ξ» (x : ⟨ Ξ± ⟩) β†’ Ord-to-𝕍 (Ξ± ↓ x))
                                    (Ord-to-𝕍↓-is-embedding Ξ±)
Ord-to-𝕍-behaviour Ξ± = to-subtype-= being-iset-is-prop (Ord-to-𝕄-behaviour Ξ±)

\end{code}

It is convenient to name the "body" of the definition for the sake of
brevity.

\begin{code}

Ord-to-𝕍-body : Ord β†’ 𝕍
Ord-to-𝕍-body Ξ± = 𝕍-ssup ⟨ Ξ± ⟩
                   (Ξ» (x : ⟨ Ξ± ⟩) β†’ Ord-to-𝕍 (Ξ± ↓ x))
                   (Ord-to-𝕍↓-is-embedding Ξ±)
\end{code}

We now show that Ord-to-𝕍 Ξ± is an iterative ordinal. We begin with a
useful observation.

\begin{code}

Ord-to-𝕍-membership : (A : 𝕍) (Ξ± : Ord)
                    β†’ A ∈ Ord-to-𝕍-body Ξ± ≃ (Ξ£ x κž‰ ⟨ Ξ± ⟩ , Ord-to-𝕍 (Ξ± ↓ x) = A)
Ord-to-𝕍-membership A Ξ± = ∈-behaviour
                           A
                           ⟨ α ⟩
                           (Ξ» x β†’ Ord-to-𝕍 (Ξ± ↓ x))
                           (Ord-to-𝕍↓-is-embedding Ξ±)
\end{code}

The map Ord-to-𝕍 (Ξ± ↓ -) is lower closed in the following sense:

\begin{code}

Ord-to-𝕍↓-is-lower : (Ξ± : Ord) (A : 𝕍) (x : ⟨ Ξ± ⟩)
                   β†’ A ∈ Ord-to-𝕍 (Ξ± ↓ x)
                   β†’ Ξ£ y κž‰ ⟨ Ξ± ⟩ , (y β‰ΊβŸ¨ Ξ± ⟩ x) Γ— (A = Ord-to-𝕍 (Ξ± ↓ y))
Ord-to-𝕍↓-is-lower Ξ± A x m = IV III
 where
  I : A ∈ Ord-to-𝕍-body (Ξ± ↓ x)
  I = transport (A ∈_) (Ord-to-𝕍-behaviour (Ξ± ↓ x)) m

  II : A ∈ Ord-to-𝕍-body (Ξ± ↓ x) ≃ (Ξ£ u κž‰ ⟨ Ξ± ↓ x ⟩ , Ord-to-𝕍 ((Ξ± ↓ x) ↓ u) = A)
  II = Ord-to-𝕍-membership A (Ξ± ↓ x)

  III : Ξ£ u κž‰ ⟨ Ξ± ↓ x ⟩ , Ord-to-𝕍 ((Ξ± ↓ x) ↓ u) = A
  III = ⌜ II ⌝ I

  IV : type-of III β†’ Ξ£ y κž‰ ⟨ Ξ± ⟩ , (y β‰ΊβŸ¨ Ξ± ⟩ x) Γ— (A = Ord-to-𝕍 (Ξ± ↓ y))
  IV ((y , l) , p) = y , (l , q)
   where
    q = A                            =⟨ p ⁻¹ ⟩
        Ord-to-𝕍 ((Ξ± ↓ x) ↓ (y , l)) =⟨ ap Ord-to-𝕍 (iterated-↓ Ξ± x y l) ⟩
        Ord-to-𝕍 (Ξ± ↓ y)             ∎

\end{code}

After the above preparation we are ready to show the desired
result. Notice that it doesn't require induction.

\begin{code}

Ord-to-𝕍-is-transitive-iset : (Ξ± : Ord) β†’ is-transitive-iset (Ord-to-𝕍 Ξ±)
Ord-to-𝕍-is-transitive-iset Ξ± =
 transport⁻¹ is-transitive-iset (Ord-to-𝕍-behaviour Ξ±) I
 where
  g : (B : 𝕍) β†’ B ∈ Ord-to-𝕍-body Ξ± ≃ (Ξ£ x κž‰ ⟨ Ξ± ⟩ , Ord-to-𝕍 (Ξ± ↓ x) = B)
  g B = Ord-to-𝕍-membership B Ξ±

  I : is-transitive-iset (Ord-to-𝕍-body Ξ±)
  I B C B-in-Ξ± C-in-B = I₁ Iβ‚€
   where
    Iβ‚€ : Ξ£ x κž‰ ⟨ Ξ± ⟩ , Ord-to-𝕍 (Ξ± ↓ x) = B
    Iβ‚€ = ⌜ g B ⌝ B-in-Ξ±

    I₁ : type-of Iβ‚€ β†’ C ∈ Ord-to-𝕍-body Ξ±
    I₁ (x , p) = Iβ‚„ I₃
     where
      Iβ‚‚ : C ∈ Ord-to-𝕍 (Ξ± ↓ x)
      Iβ‚‚ = transport (C ∈_) (p ⁻¹) C-in-B

      I₃ : Ξ£ y κž‰ ⟨ Ξ± ⟩ , (y β‰ΊβŸ¨ Ξ± ⟩ x) Γ— (C = Ord-to-𝕍 (Ξ± ↓ y))
      I₃ = Ord-to-𝕍↓-is-lower Ξ± C x Iβ‚‚

      Iβ‚„ : type-of I₃ β†’ C ∈ Ord-to-𝕍-body Ξ±
      Iβ‚„ (y , _ , q) = ⌜ g C ⌝⁻¹ (y , (q ⁻¹))

Ord-to-𝕍-has-transitive-members : (Ξ± : Ord)
                                β†’ has-transitive-members (Ord-to-𝕍 Ξ±)
Ord-to-𝕍-has-transitive-members Ξ± =
 transport⁻¹ has-transitive-members (Ord-to-𝕍-behaviour Ξ±) I
 where
  I : has-transitive-members (Ord-to-𝕍-body Ξ±)
  I B B-in-Ξ± = I₁ Iβ‚€
   where
    Iβ‚€ : Ξ£ x κž‰ ⟨ Ξ± ⟩ , Ord-to-𝕍 (Ξ± ↓ x) = B
    Iβ‚€ = ⌜ Ord-to-𝕍-membership B Ξ± ⌝ B-in-Ξ±

    I₁ : type-of Iβ‚€ β†’ is-transitive-iset B
    I₁ (x , p) = transport
                  is-transitive-iset
                  p
                  (Ord-to-𝕍-is-transitive-iset (Ξ± ↓ x))

Ord-to-𝕍-is-iordinal : (Ξ± : Ord) β†’ is-iterative-ordinal (Ord-to-𝕍 Ξ±)
Ord-to-𝕍-is-iordinal Ξ± = Ord-to-𝕍-is-transitive-iset Ξ± ,
                         Ord-to-𝕍-has-transitive-members Ξ±
\end{code}

From this we get the desired map Ord β†’ 𝕆, which is easily seen to be
an embedding from the above development:

\begin{code}

Ord-to-𝕆 : Ord β†’ 𝕆
Ord-to-𝕆 Ξ± = Ord-to-𝕍 Ξ± , Ord-to-𝕍-is-iordinal Ξ±

Ord-to-𝕆-is-embedding : is-embedding Ord-to-𝕆
Ord-to-𝕆-is-embedding = pair-fun-is-embedding-special
                         Ord-to-𝕍
                         Ord-to-𝕍-is-iordinal
                         Ord-to-𝕍-is-embedding
                         being-iordinal-is-prop
\end{code}

In order to show that this map is an equivalence, with two sided
inverse 𝕆-to-Ord, we need some preparation:

\begin{code}

Ord-to-𝕆↓-is-embedding : (Ξ± : Ord)
                       β†’ is-embedding (Ξ» x β†’ Ord-to-𝕆 (Ξ± ↓ x))
Ord-to-𝕆↓-is-embedding Ξ± = ∘-is-embedding
                            (↓-is-embedding Ξ±)
                            Ord-to-𝕆-is-embedding

Ord-to-𝕆↓-is-lower-closed : (Ξ± : Ord)
                          β†’ is-lower-closed (Ξ» x β†’ Ord-to-𝕆 (Ξ± ↓ x))
Ord-to-𝕆↓-is-lower-closed Ξ± Ξ² x l = II I
 where
  B : 𝕍
  B = underlying-iset Ξ²

  I : Ξ£ y κž‰ ⟨ Ξ± ⟩ , (y β‰ΊβŸ¨ Ξ± ⟩ x) Γ— (B = Ord-to-𝕍 (Ξ± ↓ y))
  I = Ord-to-𝕍↓-is-lower Ξ± B x l

  II : type-of I β†’ Ξ£ y κž‰ ⟨ Ξ± ⟩ , Ord-to-𝕆 (Ξ± ↓ y) = Ξ²
  II (y , _ , p) = y , to-subtype-= being-iordinal-is-prop (p ⁻¹)

\end{code}

TODO. Instead show that the composition of two lower closed maps is
lower closed, and use this to give a direct proof of the above.

We use this to obtain the following recursive characterization of the
map Ord-to-𝕆.

\begin{code}

Ord-to-𝕆-behaviour : (Ξ± : Ord)
                   β†’ Ord-to-𝕆 Ξ± = 𝕆-ssup
                                    ⟨ α ⟩
                                    ((Ξ» (x : ⟨ Ξ± ⟩) β†’ Ord-to-𝕆 (Ξ± ↓ x)))
                                    (Ord-to-𝕆↓-is-embedding Ξ±)
                                    (Ord-to-𝕆↓-is-lower-closed Ξ±)
Ord-to-𝕆-behaviour Ξ± = to-subtype-=
                        being-iordinal-is-prop
                         (to-subtype-=
                           being-iset-is-prop
                           (Ord-to-𝕄-behaviour Ξ±))
\end{code}

We now establish the following equation by nested induction, first on
𝕆 and then on the ordinal 𝕆-to-Ord Ξ±, which seems to be a new
observation.

\begin{code}

𝕆-to-Ord-equation : (Ξ± : 𝕆) (x : 𝕆-root Ξ±)
                  β†’ (𝕆-to-Ord Ξ±) ↓ x = 𝕆-to-Ord (𝕆-forest Ξ± x)
𝕆-to-Ord-equation = 𝕆-induction' _ f
 where
  f : (Ξ± : 𝕆)
    β†’ ((x : 𝕆-root Ξ±) (y : 𝕆-root (𝕆-forest Ξ± x))
          β†’  𝕆-to-Ord (𝕆-forest Ξ± x) ↓ y
          = 𝕆-to-Ord (𝕆-forest (𝕆-forest Ξ± x) y))
    β†’ (x : 𝕆-root Ξ±) β†’ (𝕆-to-Ord Ξ± ↓ x) = 𝕆-to-Ord (𝕆-forest Ξ± x)
  f Ξ± IH-f = Transfinite-induction (𝕆-to-Ord Ξ±) _ g
   where
    g : (x : 𝕆-root Ξ±)
      β†’ ((y : 𝕆-root Ξ±) β†’ y β‰ΊβŸ¨ 𝕆-to-Ord Ξ± ⟩ x
            β†’ (𝕆-to-Ord Ξ± ↓ y) = 𝕆-to-Ord (𝕆-forest Ξ± y))
      β†’ (𝕆-to-Ord Ξ± ↓ x) = 𝕆-to-Ord (𝕆-forest Ξ± x)
    g x IH-g = ⊲-is-extensional _ _ (to-β‰Ό I) (to-β‰Ό II)
     where
      I : (y : ⟨ 𝕆-to-Ord Ξ± ↓ x ⟩)
        β†’ ((𝕆-to-Ord Ξ± ↓ x) ↓ y) ⊲ 𝕆-to-Ord (𝕆-forest Ξ± x)
      I π•ͺ@(y , l) = (y' , eq)
       where
        I₁ : Ξ£ y' κž‰ 𝕆-root (𝕆-forest Ξ± x)
                  , 𝕆-forest (𝕆-forest Ξ± x) y' = 𝕆-forest Ξ± y
        I₁ = ⌜ <-behaviour (𝕆-forest Ξ± y) (𝕆-forest Ξ± x) ⌝
              (⌜ 𝕆-to-Ord-order Ξ± y x ⌝⁻¹ l)
        y'  = pr₁ I₁
        eq' = prβ‚‚ I₁

        eq = (𝕆-to-Ord Ξ± ↓ x) ↓ π•ͺ                  =⟨ β¦…1⦆ ⟩
             𝕆-to-Ord Ξ± ↓ y                        =⟨ β¦…2⦆ ⟩
             𝕆-to-Ord (𝕆-forest Ξ± y)               =⟨ β¦…3⦆ ⟩
             𝕆-to-Ord (𝕆-forest (𝕆-forest Ξ± x) y') =⟨ β¦…4⦆ ⟩
             𝕆-to-Ord (𝕆-forest Ξ± x) ↓ y'          ∎
         where
          β¦…1⦆ = iterated-↓ (𝕆-to-Ord Ξ±) x y l
          β¦…2⦆ = IH-g y l
          β¦…3⦆ = ap 𝕆-to-Ord (eq' ⁻¹)
          β¦…4⦆ = (IH-f x y')⁻¹

      II : (y : ⟨ 𝕆-to-Ord (𝕆-forest Ξ± x) ⟩)
         β†’ (𝕆-to-Ord (𝕆-forest Ξ± x) ↓ y) ⊲ (𝕆-to-Ord Ξ± ↓ x)
      II y = (π•ͺ , (eq ⁻¹))
       where
        note : 𝕆-root (𝕆-forest Ξ± x) = ⟨ 𝕆-to-Ord (𝕆-forest Ξ± x) ⟩
        note = refl

        Iβ‚‚ : Ξ£ y' κž‰ 𝕆-root Ξ±
                  , 𝕆-forest Ξ± y' = 𝕆-forest (𝕆-forest Ξ± x) y
        Iβ‚‚ = 𝕆-forest-is-lower-closed
              Ξ±
              (𝕆-forest (𝕆-forest Ξ± x) y)
              x
              (𝕆-forest-< (𝕆-forest Ξ± x) y)
        y'  = pr₁ Iβ‚‚
        eq' = prβ‚‚ Iβ‚‚

        l : 𝕆-forest Ξ± y' < 𝕆-forest Ξ± x
        l = ⌜ <-behaviour (𝕆-forest Ξ± y') (𝕆-forest Ξ± x) ⌝⁻¹
             (y , (eq' ⁻¹))
        l' : y' β‰ΊβŸ¨ 𝕆-to-Ord Ξ± ⟩ x
        l' = ⌜ 𝕆-to-Ord-order Ξ± y' x ⌝ l
        π•ͺ = (y' , l')

        eq = (𝕆-to-Ord Ξ± ↓ x) ↓ π•ͺ                 =⟨ β¦…1⦆ ⟩
             𝕆-to-Ord Ξ± ↓ y'                      =⟨ β¦…2⦆ ⟩
             𝕆-to-Ord (𝕆-forest Ξ± y')             =⟨ β¦…3⦆ ⟩
             𝕆-to-Ord (𝕆-forest (𝕆-forest Ξ± x) y) =⟨ β¦…4⦆ ⟩
             𝕆-to-Ord (𝕆-forest Ξ± x) ↓ y          ∎
         where
          β¦…1⦆ = iterated-↓ (𝕆-to-Ord Ξ±) x y' l'
          β¦…2⦆ = IH-g y' l'
          β¦…3⦆ = ap 𝕆-to-Ord eq'
          β¦…4⦆ = (IH-f x y)⁻¹

\end{code}

From this equation and the previous results, we easily deduce that the
map Ord-to-𝕆 is an equivalence, by induction on 𝕆.

\begin{code}

Ord-to-𝕆-is-equiv : is-equiv Ord-to-𝕆
Ord-to-𝕆-is-equiv = embeddings-with-sections-are-equivs
                     Ord-to-𝕆
                     Ord-to-𝕆-is-embedding
                     (𝕆-to-Ord , Ξ·)
 where
  f : (Ξ± : 𝕆)
    β†’ ((x : 𝕆-root Ξ±) β†’ Ord-to-𝕆 (𝕆-to-Ord (𝕆-forest Ξ± x)) = 𝕆-forest Ξ± x)
    β†’ Ord-to-𝕆 (𝕆-to-Ord Ξ±) = Ξ±
  f Ξ± IH =
   Ord-to-𝕆 (𝕆-to-Ord Ξ±)                                   =⟨ I ⟩
   𝕆-ssup (𝕆-root Ξ±) (Ξ» x β†’ Ord-to-𝕆 (𝕆-to-Ord Ξ± ↓ x)) e l =⟨ II ⟩
   𝕆-ssup (𝕆-root Ξ±) (𝕆-forest Ξ±) e' l'                    =⟨ 𝕆-Ξ· Ξ± ⟩
   α                                                       ∎
    where
     e = Ord-to-𝕆↓-is-embedding (𝕆-to-Ord Ξ±)
     l = Ord-to-𝕆↓-is-lower-closed (𝕆-to-Ord Ξ±)

     I   = Ord-to-𝕆-behaviour (𝕆-to-Ord Ξ±)

     e' = 𝕆-forest-is-embedding Ξ±
     l' = 𝕆-forest-is-lower-closed Ξ±

     II' = Ξ» x β†’
      Ord-to-𝕆 (𝕆-to-Ord Ξ± ↓ x)          =⟨ ap Ord-to-𝕆 (𝕆-to-Ord-equation Ξ± x) ⟩
      Ord-to-𝕆 (𝕆-to-Ord (𝕆-forest Ξ± x)) =⟨ IH x ⟩
      𝕆-forest Ξ± x                       ∎

     II  = to-𝕆-=-special
            (𝕆-root Ξ±)
            (Ξ» x β†’ Ord-to-𝕆 (𝕆-to-Ord Ξ± ↓ x))
            (𝕆-forest Ξ±)
            e l e' l'
            (dfunext fe II')

  Ξ· : Ord-to-𝕆 ∘ 𝕆-to-Ord ∼ id
  Ξ· = 𝕆-induction' _ f

\end{code}

Hence Ord and 𝕆 are equivalent types:

\begin{code}

Ordinals-≃ : Ord ≃ 𝕆
Ordinals-≃ = Ord-to-𝕆 , Ord-to-𝕆-is-equiv

\end{code}

But more than this is true: the types π“ž (HoTT-book ordinal of
iterative ordinals) and OO 𝓀 (HoTT-book ordinal of HoTT-book ordinals)
are isomorphic as HoTT-book ordinals.

It is easy to see that 𝕆-to-Ord reflects order:

\begin{code}

𝕆-to-Ord-reflects-order : (Ξ± Ξ² : 𝕆) β†’ 𝕆-to-Ord Ξ± ⊲ 𝕆-to-Ord Ξ² β†’ Ξ± < Ξ²
𝕆-to-Ord-reflects-order Ξ± Ξ² (y , p) = III
 where
  I = 𝕆-to-Ord (𝕆-forest Ξ² y) =⟨ (𝕆-to-Ord-equation Ξ² y)⁻¹ ⟩
      (𝕆-to-Ord Ξ² ↓ y)        =⟨ p ⁻¹ ⟩
      𝕆-to-Ord Ξ±              ∎

  II : 𝕆-forest Ξ² y = Ξ±
  II = equivs-are-lc ⌜ Ordinals-≃ ⌝⁻¹ ⌜ Ordinals-≃ ⌝⁻¹-is-equiv I

  III : Ξ± < Ξ²
  III = ⌜ <-behaviour α β ⌝⁻¹ (y , II)

\end{code}

And a second application of the above equation shows that 𝕆-to-Ord
preserves order.

\begin{code}

𝕆-to-Ord-preserves-order : (Ξ± Ξ² : 𝕆) β†’ Ξ± < Ξ² β†’ 𝕆-to-Ord Ξ± ⊲ 𝕆-to-Ord Ξ²
𝕆-to-Ord-preserves-order Ξ± Ξ² l = II I
 where
  I : Ξ£ y κž‰ 𝕆-root Ξ² , 𝕆-forest Ξ² y = Ξ±
  I = ⌜ <-behaviour α β ⌝ l

  II : type-of I β†’ 𝕆-to-Ord Ξ± ⊲ 𝕆-to-Ord Ξ²
  II (y , refl) = IV
   where
    III : 𝕆-to-Ord (𝕆-forest Ξ² y) = (𝕆-to-Ord Ξ² ↓ y)
    III = (𝕆-to-Ord-equation Ξ² y)⁻¹

    IV : 𝕆-to-Ord (𝕆-forest Ξ² y) ⊲ 𝕆-to-Ord Ξ²
    IV = y , III

\end{code}

Putting this together we get our desired isomorphism of ordinals:

\begin{code}

Ordinals-agreementβ‚’ : π“ž ≃ₒ OO 𝓀
Ordinals-agreementβ‚’ = ⌜ Ordinals-≃ ⌝⁻¹ ,
                      order-preserving-reflecting-equivs-are-order-equivs
                       π“ž
                       (OO 𝓀)
                       ⌜ Ordinals-≃ ⌝⁻¹
                       ⌜ Ordinals-≃ ⌝⁻¹-is-equiv
                       𝕆-to-Ord-preserves-order
                       𝕆-to-Ord-reflects-order
\end{code}

Which then gives an identification between the two types.

\begin{code}

Ordinals-agreement : π“ž = OO 𝓀
Ordinals-agreement = eqtoidβ‚’ (ua 𝓀⁺) fe π“ž (OO 𝓀) Ordinals-agreementβ‚’

\end{code}

Notice that this identification lives in the identity type of the type
of ordinals in the universe 𝓀⁺, which is a 0-type, and therefore is
unique.

\begin{code}

Ordinals-agreement-is-unique : is-singleton (π“ž =[ Ordinal 𝓀⁺ ] OO 𝓀)
Ordinals-agreement-is-unique = pointed-props-are-singletons
                                Ordinals-agreement
                                (the-type-of-ordinals-is-a-set (ua (𝓀 ⁺)) fe)
\end{code}