Martin Escardo, Added 21st April 2022.

Limit ordinals and suprema of families of ordinals.

(Moved from another file to this new file 15th October 2024.)

\begin{code}

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

open import UF.Univalence

module Ordinals.Limit
       (ua : Univalence)
       where

open import UF.Equiv
open import UF.FunExt
open import UF.Subsingletons
open import UF.UA-FunExt
open import UF.PropTrunc
open import UF.Size

private
 fe : FunExt
 fe = Univalence-gives-FunExt ua

 fe' : Fun-Ext
 fe' {𝓀} {π“₯} = fe 𝓀 π“₯

 pe : PropExt
 pe = Univalence-gives-PropExt ua

open import MLTT.Spartan
open import Notation.CanonicalMap
open import Ordinals.AdditionProperties ua
open import Ordinals.Arithmetic fe
open import Ordinals.ConvergentSequence ua
open import Ordinals.Equivalence
open import Ordinals.Maps
open import Ordinals.OrdinalOfOrdinals ua
open import Ordinals.OrdinalOfOrdinalsSuprema ua
open import Ordinals.Type
open import Ordinals.Underlying

\end{code}

One possible classical definition of limit ordinal.

\begin{code}

is-limit-ordinal⁺ : Ordinal 𝓀 β†’ 𝓀 ⁺ Μ‡
is-limit-ordinal⁺ {𝓀} Ξ± = (Ξ² : Ordinal 𝓀)
                         β†’ ((Ξ³ : Ordinal 𝓀) β†’ Ξ³ ⊲ Ξ± β†’ Ξ³ ⊴ Ξ²)
                         β†’ Ξ± ⊴ Ξ²
\end{code}

We give an equivalent definition below.

Recall from the modules UF.Quotients.FromSetReplacement and
UF.Quotients.GivesSetReplacement that the existence propositional
truncations and the set-replacement property are together equivalent
to the existence of small quotients. With them we can construct
suprema of families of ordinals.

\begin{code}

module _ (pt : propositional-truncations-exist)
         (sr : Set-Replacement pt)
       where

 open suprema pt sr

\end{code}

Recall that, by definition, Ξ³ ⊲ Ξ± iff Ξ³ is of the form Ξ± ↓ x for some
x : ⟨ α ⟩. We define the "floor" of an ordinal to be the supremum of
its predecessors:

\begin{code}

 ⌊_βŒ‹ : Ordinal 𝓀 β†’ Ordinal 𝓀
 ⌊ Ξ± βŒ‹ = sup (Ξ± ↓_)

 βŒŠβŒ‹-lower-bound : (Ξ± : Ordinal 𝓀) β†’ ⌊ Ξ± βŒ‹ ⊴ Ξ±
 βŒŠβŒ‹-lower-bound Ξ± = sup-is-lower-bound-of-upper-bounds _ Ξ± (segment-⊴ Ξ±)

 is-limit-ordinal : Ordinal 𝓀 β†’ 𝓀 Μ‡
 is-limit-ordinal Ξ± = Ξ± ⊴ ⌊ Ξ± βŒ‹

 is-limit-ordinal-fact : (Ξ± : Ordinal 𝓀)
                       β†’ is-limit-ordinal Ξ±
                       ↔ Ξ± = ⌊ Ξ± βŒ‹
 is-limit-ordinal-fact Ξ± = (Ξ» β„“ β†’ ⊴-antisym _ _ β„“ (βŒŠβŒ‹-lower-bound Ξ±)) ,
                           (Ξ» p β†’ transport (Ξ± ⊴_) p (⊴-refl Ξ±))

 βŒŠβŒ‹-of-successor : (Ξ± : Ordinal 𝓀)
                 β†’ ⌊ Ξ± +β‚’ πŸ™β‚’ βŒ‹ ⊴ Ξ±
 βŒŠβŒ‹-of-successor Ξ± = sup-is-lower-bound-of-upper-bounds _ Ξ± h
  where
   h : (x : ⟨ Ξ± +β‚’ πŸ™β‚’ ⟩) β†’ ((Ξ± +β‚’ πŸ™β‚’) ↓ x) ⊴ Ξ±
   h (inl x) = successor-lemma-left Ξ± x
   h (inr ⋆) = transport⁻¹ (_⊴ Ξ±) (successor-lemma-right Ξ±) (⊴-refl Ξ±)

 βŒŠβŒ‹-of-successor' : (Ξ± : Ordinal 𝓀)
                  β†’ ⌊ Ξ± +β‚’ πŸ™β‚’ βŒ‹ = Ξ±
 βŒŠβŒ‹-of-successor' Ξ± = III
  where
   I : ((Ξ± +β‚’ πŸ™β‚’) ↓ inr ⋆) ⊴ ⌊ Ξ± +β‚’ πŸ™β‚’ βŒ‹
   I = sup-is-upper-bound _ (inr ⋆)

   II : Ξ± ⊴ ⌊ Ξ± +β‚’ πŸ™β‚’ βŒ‹
   II = transport (_⊴ ⌊ Ξ± +β‚’ πŸ™β‚’ βŒ‹) (successor-lemma-right Ξ±) I

   III : ⌊ Ξ± +β‚’ πŸ™β‚’ βŒ‹ = Ξ±
   III = ⊴-antisym _ _ (βŒŠβŒ‹-of-successor Ξ±) II

 successors-are-not-limit-ordinals : (Ξ± : Ordinal 𝓀)
                                   β†’ Β¬ is-limit-ordinal (Ξ± +β‚’ πŸ™β‚’)
 successors-are-not-limit-ordinals Ξ± le = irrefl (OO _) Ξ± II
  where
   I : (Ξ± +β‚’ πŸ™β‚’) ⊴ Ξ±
   I = ⊴-trans (Ξ± +β‚’ πŸ™β‚’) ⌊ Ξ± +β‚’ πŸ™β‚’ βŒ‹ Ξ± le (βŒŠβŒ‹-of-successor Ξ±)

   II : α ⊲ α
   II = ⊴-gives-β‰Ό _ _ I Ξ± (successor-increasing Ξ±)

\end{code}

TODO (easy). Show that is-limit-ordinal⁺ α is logically equivalent to
is-limit-ordinal Ξ±.

\begin{code}

 βŒŠβŒ‹-monotone : (Ξ± Ξ² : Ordinal 𝓀) β†’ Ξ± ⊴ Ξ² β†’ ⌊ Ξ± βŒ‹ ⊴ ⌊ Ξ² βŒ‹
 βŒŠβŒ‹-monotone Ξ± Ξ² le = V
  where
   I : (y : ⟨ Ξ² ⟩) β†’ (Ξ² ↓ y) ⊴ ⌊ Ξ² βŒ‹
   I = sup-is-upper-bound (Ξ² ↓_)

   II : (x : ⟨ Ξ± ⟩) β†’ (Ξ± ↓ x) ⊲ Ξ²
   II x = ⊴-gives-β‰Ό _ _ le (Ξ± ↓ x) (x , refl)

   III : (x : ⟨ Ξ± ⟩) β†’ Ξ£ y κž‰ ⟨ Ξ² ⟩ , (Ξ± ↓ x) = (Ξ² ↓ y)
   III = II

   IV : (x : ⟨ Ξ± ⟩) β†’ (Ξ± ↓ x) ⊴ ⌊ Ξ² βŒ‹
   IV x = transport⁻¹ (_⊴ ⌊ Ξ² βŒ‹) (prβ‚‚ (III x)) (I (pr₁ (III x)))

   V : sup (Ξ± ↓_) ⊴ ⌊ Ξ² βŒ‹
   V = sup-is-lower-bound-of-upper-bounds (Ξ± ↓_) ⌊ Ξ² βŒ‹ IV

\end{code}

We now give an example of an ordinal which is not a limit ordinal and
also is not a successor ordinal unless LPO holds:

\begin{code}

 open import CoNaturals.Type
 open import Notation.Order
 open import Naturals.Order

 βŒŠβŒ‹-of-β„•βˆž : ⌊ β„•βˆžβ‚’ βŒ‹ = Ο‰
 βŒŠβŒ‹-of-β„•βˆž = c
  where
   a : ⌊ β„•βˆžβ‚’ βŒ‹ ⊴ Ο‰
   a = sup-is-lower-bound-of-upper-bounds (β„•βˆžβ‚’ ↓_) Ο‰ I
    where
     I : (u : ⟨ β„•βˆžβ‚’ ⟩) β†’ (β„•βˆžβ‚’ ↓ u) ⊴ Ο‰
     I u = β‰Ό-gives-⊴ (β„•βˆžβ‚’ ↓ u) Ο‰ II
      where
       II : (Ξ± : Ordinal 𝓀₀) β†’ Ξ± ⊲ (β„•βˆžβ‚’ ↓ u) β†’ Ξ± ⊲ Ο‰
       II .((β„•βˆžβ‚’ ↓ u) ↓ (ΞΉ n , n , refl , p)) ((.(ΞΉ n) , n , refl , p) , refl) =
        XI
        where
         III : ΞΉ n β‰Ί u
         III = n , refl , p

         IV : ((β„•βˆžβ‚’ ↓ u) ↓ (ΞΉ n , n , refl , p)) = β„•βˆžβ‚’ ↓ ΞΉ n
         IV = iterated-↓ β„•βˆžβ‚’ u (ΞΉ n) III

         V : (β„•βˆžβ‚’ ↓ ΞΉ n) ≃ₒ (Ο‰ ↓ n)
         V = f , fop , qinvs-are-equivs f (g , gf , fg) , gop
          where
           f : ⟨ β„•βˆžβ‚’ ↓ ΞΉ n ⟩ β†’ ⟨ Ο‰ ↓ n ⟩
           f (.(ι k) , k , refl , q) = k , ⊏-gives-< _ _ q

           g : ⟨ Ο‰ ↓ n ⟩ β†’ ⟨ β„•βˆžβ‚’ ↓ ΞΉ n ⟩
           g (k , l) = (ι k , k , refl , <-gives-⊏ _ _ l)

           fg : f ∘ g ∼ id
           fg (k , l) = to-subtype-= (Ξ» k β†’ <-is-prop-valued k n) refl

           gf : g ∘ f ∼ id
           gf (.(ι k) , k , refl , q) = to-subtype-=
                                         (Ξ» u β†’ β‰Ί-prop-valued fe' u (ΞΉ n))
                                         refl

           fop : is-order-preserving (β„•βˆžβ‚’ ↓ ΞΉ n) (Ο‰ ↓ n) f
           fop (.(ΞΉ k) , k , refl , q) (.(ΞΉ k') , k' , refl , q') (m , r , cc) =
            VIII
            where
             VI : k = m
             VI = β„•-to-β„•βˆž-lc r

             VII : m < k'
             VII = ⊏-gives-< _ _ cc

             VIII : k < k'
             VIII = transport⁻¹ (_< k') VI VII

           gop : is-order-preserving (Ο‰ ↓ n) (β„•βˆžβ‚’ ↓ ΞΉ n)  g
           gop (k , l) (k' , l') β„“ = k , refl , <-gives-⊏ _ _ β„“

         IX : β„•βˆžβ‚’ ↓ ΞΉ n = Ο‰ ↓ n
         IX = eqtoidβ‚’ (ua 𝓀₀) fe' _ _ V

         X : (β„•βˆžβ‚’ ↓ (ΞΉ n)) ⊲ Ο‰
         X = n , IX

         XI : ((β„•βˆžβ‚’ ↓ u) ↓ (ΞΉ n , n , refl , p)) ⊲ Ο‰
         XI = transport⁻¹ (_⊲ Ο‰) IV X

   b : Ο‰ ⊴ ⌊ β„•βˆžβ‚’ βŒ‹
   b = transport (_⊴ ⌊ β„•βˆžβ‚’ βŒ‹) (βŒŠβŒ‹-of-successor' Ο‰) I
    where
     I : ⌊ Ο‰ +β‚’ πŸ™β‚’ βŒ‹ ⊴ ⌊ β„•βˆžβ‚’ βŒ‹
     I = βŒŠβŒ‹-monotone (Ο‰ +β‚’ πŸ™β‚’) β„•βˆžβ‚’ Ο‰+πŸ™-is-⊴-β„•βˆž

   c : ⌊ β„•βˆžβ‚’ βŒ‹ = Ο‰
   c = ⊴-antisym _ _ a b

 β„•βˆž-is-not-limit : Β¬ is-limit-ordinal β„•βˆžβ‚’
 β„•βˆž-is-not-limit β„“ = III II
  where
   I = β„•βˆžβ‚’     =⟨ lr-implication (is-limit-ordinal-fact β„•βˆžβ‚’) β„“ ⟩
       ⌊ β„•βˆžβ‚’ βŒ‹ =⟨ βŒŠβŒ‹-of-β„•βˆž  ⟩
       Ο‰       ∎

   II : β„•βˆžβ‚’ ≃ₒ Ο‰
   II = idtoeqβ‚’ _ _ I

   III : Β¬ (β„•βˆžβ‚’ ≃ₒ Ο‰)
   III (f , e) = irrefl Ο‰ (f ∞) VII
    where
     IV : is-largest Ο‰ (f ∞)
     IV = order-equivs-preserve-largest β„•βˆžβ‚’ Ο‰ f e ∞
           (Ξ» u t l β†’ β‰Ίβ‰Ό-gives-β‰Ί t u ∞ l (∞-largest u))

     V : f ∞ β‰ΊβŸ¨ Ο‰ ⟩ succ (f ∞)
     V = <-succ (f ∞)

     VI : succ (f ∞) β‰ΌβŸ¨ Ο‰ ⟩ f ∞
     VI = IV (succ (f ∞))

     VII : f ∞ β‰ΊβŸ¨ Ο‰ ⟩ f ∞
     VII = VI (f ∞) V

 open import Taboos.LPO

 β„•βˆž-successor-gives-LPO : (Ξ£ Ξ± κž‰ Ordinal 𝓀₀ , (β„•βˆžβ‚’ = (Ξ± +β‚’ πŸ™β‚’))) β†’ LPO
 β„•βˆž-successor-gives-LPO (Ξ± , p) = IV
  where
   I = Ξ±           =⟨ (βŒŠβŒ‹-of-successor' Ξ±)⁻¹ ⟩
       ⌊ Ξ± +β‚’ πŸ™β‚’ βŒ‹ =⟨ ap ⌊_βŒ‹ (p ⁻¹) ⟩
       ⌊ β„•βˆžβ‚’ βŒ‹     =⟨ βŒŠβŒ‹-of-β„•βˆž ⟩
       Ο‰           ∎

   II : β„•βˆžβ‚’ = (Ο‰ +β‚’ πŸ™β‚’)
   II = transport (Ξ» - β†’ β„•βˆžβ‚’ = (- +β‚’ πŸ™β‚’)) I p

   III : β„•βˆžβ‚’ ⊴ (Ο‰ +β‚’ πŸ™β‚’)
   III = transport (β„•βˆžβ‚’ ⊴_) II (⊴-refl β„•βˆžβ‚’)

   IV : LPO
   IV = β„•βˆž-⊴-Ο‰+πŸ™-gives-LPO III

 open PropositionalTruncation pt

 β„•βˆž-successor-gives-LPO' : (βˆƒ Ξ± κž‰ Ordinal 𝓀₀ , (β„•βˆžβ‚’ = (Ξ± +β‚’ πŸ™β‚’))) β†’ LPO
 β„•βˆž-successor-gives-LPO' = βˆ₯βˆ₯-rec (LPO-is-prop fe') β„•βˆž-successor-gives-LPO

 LPO-gives-β„•βˆž-successor : LPO β†’ (Ξ£ Ξ± κž‰ Ordinal 𝓀₀ , (β„•βˆžβ‚’ = (Ξ± +β‚’ πŸ™β‚’)))
 LPO-gives-β„•βˆž-successor lpo = Ο‰ , β„•βˆž-is-successor₃ lpo

\end{code}

Therefore, constructively, it is not necessarily the case that every
ordinal is either a successor or a limit.

TODO (1st June 2023). A classically equivalently definition of limit
ordinal Ξ± is that there is some Ξ² < Ξ±, and for every Ξ² < Ξ± there is Ξ³
with Ξ² < Ξ³ < Ξ±. We have that β„•βˆž is a limit ordinal in this sense.