Martin Escardo 20-21 December 2012

Development adapted from the module ConvergentSequenceCompact:

Not only is ℕ∞ compact (or searchable), but, moreover, minimal
witnesses can be found.

\begin{code}

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

open import UF.FunExt
open import MLTT.Spartan

module TypeTopology.ConvergentSequenceHasInf (fe₀ : funext 𝓤₀ 𝓤₀) where


open import CoNaturals.Type
open import MLTT.Two-Properties
open import Notation.CanonicalMap
open import Notation.Order
open import Ordinals.InfProperty

ℕ∞-has-inf : has-inf _≼_
ℕ∞-has-inf p = a , putative-root-lemma , lower-bound-lemma , uborlb-lemma
 where
  α :   𝟚
  α 0       = p (ι 0)
  α (succ n) = min𝟚 (α n) (p (ι (succ n)))

  a : ℕ∞
  a = (α , λ i  Lemma[minab≤₂a])

  Dagger₀ : (n : )  a  ι n  p (ι n)  
  Dagger₀ 0        r = ap  -  ι - 0) r
  Dagger₀ (succ n) r = p (ι (succ n)) =⟨ w 
                       α (succ n)     =⟨ t 
                                     
   where
    s : α n  
    s = ap  -  ι - n) r  ℕ-to-ℕ∞-diagonal₁ n

    t = α (succ n)              =⟨ ap  -  ι - (succ n)) r 
        ι (ι (succ n)) (succ n) =⟨ ℕ-to-ℕ∞-diagonal₀ n 
                               

    w : p (ι (succ n))  α (succ n)
    w = (ap  -  min𝟚 - (p (ι (succ n)))) s)⁻¹

  Dagger₁ : a    (n : )  p (ι n)  
  Dagger₁ r 0 = ap  -  ι - 0) r
  Dagger₁ r (succ n) = p (ι (succ n)) =⟨ w 
                       α (succ n)     =⟨ t 
                                     
   where
    s : α n  
    s = ap  -  ι - n) r

    t : α (succ n)  
    t = ap  -  ι - (succ n)) r

    w : p (ι (succ n))  α (succ n)
    w = (ap  -  min𝟚 - (p (ι (succ n)))) s)⁻¹

  Claim₀ : p a    (n : )  a  ι n
  Claim₀ r n s = equal-₁-different-from-₀ r (Lemma s)
   where
    Lemma : a  ι n  p a  
    Lemma t = p a     =⟨ ap p t 
              p (ι n) =⟨ Dagger₀ n t 
                     

  Claim₁ : p a    a  
  Claim₁ r = not-finite-is-∞ fe₀ (Claim₀ r)

  Claim₂ : p a    (n : )  p (ι n)  
  Claim₂ r = Dagger₁ (Claim₁ r)

  Claim₃ : p a    p   
  Claim₃ r = p  =⟨ (ap p (Claim₁ r))⁻¹ 
             p a =⟨ r 
                

  Lemma : p a    (v : ℕ∞)  p v  
  Lemma r = ℕ∞-𝟚-density fe₀ (Claim₂ r) (Claim₃ r)

  putative-root-lemma : (Σ u  ℕ∞ , p u  )  p a  
  putative-root-lemma (x , r) = lemma claim
   where
    lemma : ¬ ((x : ℕ∞)  p x  )  p a  
    lemma = different-from-₁-equal-₀  (contrapositive Lemma)

    claim : ¬ ((x : ℕ∞)  p x  )
    claim f = equal-₁-different-from-₀ (f x) r

  lower-bound-lemma : (u : ℕ∞)→ p u    a  u
  lower-bound-lemma u r 0 s = lemma
    where
     claim₀ : ι u 0    p u  α 0
     claim₀ t = ap p (is-Zero-equal-Zero fe₀ t)

     claim₁ : ι u 0      
     claim₁ t =    =⟨ r ⁻¹ 
                p u =⟨ claim₀ t 
                α 0 =⟨ s 
                   

     lemma : ι u 0  
     lemma = different-from-₀-equal-₁ (contrapositive claim₁ zero-is-not-one)

  lower-bound-lemma u r (succ n) s = lemma
   where
    _ : min𝟚 (ι a n) (p (ι (succ n)))  
    _ = s

    IH : ι a n    ι u n  
    IH = lower-bound-lemma u r n

    claim₀ : ι u n  
    claim₀ = IH (Lemma[min𝟚ab=₁→a=₁] s)

    claim₁ : p (ι (succ n))  
    claim₁ = Lemma[min𝟚ab=₁→b=₁]{(ι a n)} s

    claim₂ : ι u (succ n)    u  ι (succ n)
    claim₂ = Succ-criterion fe₀ claim₀

    claim₃ : ι u (succ n)    p u  p (ι (succ n))
    claim₃ t = ap p (claim₂ t)

    claim₄ : ι u (succ n)    p u  
    claim₄ t = p u            =⟨ claim₃ t 
               p (ι (succ n)) =⟨ claim₁ 
                             

    claim₅ : ι u (succ n)  
    claim₅ t = equal-₁-different-from-₀ (claim₄ t) r

    lemma : ι u (succ n)  
    lemma = different-from-₀-equal-₁ claim₅

  uborlb-lemma : (l : ℕ∞)  ((x : ℕ∞)  p x    l  x)  l  a
  uborlb-lemma l lower-bounder = 𝟚-equality-cases lemma₀ lemma₁
   where
    lemma₀ : p a    l  a
    lemma₀ = lower-bounder a

    lemma₁ : p a    l  a
    lemma₁ r n x = ap  -  ι - n) (Claim₁ r)

\end{code}