Martin Escardo, July 2023

A type of numbers used to measure lengths of paths in trees in W-types
(see the module W.Paths).

For an exposition of what is done here, see the post 7/6 of this thread:
https://mathstodon.xyz/@MartinEscardo/110753930251021051

\begin{code}

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

open import MLTT.Spartan hiding (_^_)

module W.Numbers where

open import Fin.Type hiding (suc)
open import NotionsOfDecidability.Decidable
open import UF.Base
open import UF.DiscreteAndSeparated
open import UF.Embeddings
open import UF.Equiv
open import UF.EquivalenceExamples
open import UF.ClassicalLogic
open import UF.FunExt
open import UF.PropIndexedPiSigma
open import UF.PropTrunc
open import UF.Retracts
open import UF.Sets
open import UF.SubtypeClassifier
open import UF.SubtypeClassifier-Properties
open import UF.Subsingletons
open import UF.Subsingletons-FunExt
open import W.Properties
open import W.Type

\end{code}

We work with a fixed universe π“₯.

\begin{code}

module _ {π“₯ : Universe} where

 𝓝 : π“₯ ⁺ Μ‡
 𝓝 = W (Ξ© π“₯) _holds

 positive : 𝓝 β†’ Ξ© π“₯
 positive = W-root

 is-positive : 𝓝 β†’ π“₯ Μ‡
 is-positive n = positive n holds

\end{code}

The predecessor function is defined on positive numbers.

\begin{code}

 Pred : (n : 𝓝) β†’ is-positive n β†’ 𝓝
 Pred = W-forest

\end{code}

For every proposition p, there is a p-ary successor function. Notice
that we can regard an element of 𝓝 ^ p as a p-indexed tuple of
elements of 𝓝, or, equivalently, as a partial element of 𝓝.

\begin{code}

 _^_ : 𝓀 Μ‡ β†’ Ξ© π“₯ β†’ π“₯ βŠ” 𝓀 Μ‡
 X ^ p = p holds β†’ X

 Suc : (p : Ξ© π“₯) β†’ 𝓝 ^ p β†’ 𝓝
 Suc = ssup

 Suc-positivity : (p : Ξ© π“₯) (ns : 𝓝 ^ p) β†’ positive (Suc p ns) = p
 Suc-positivity = W-ssup-root

 Pred-Suc : (p : Ξ© π“₯) (ns : 𝓝 ^ p) β†’ Pred (Suc p ns) = ns
 Pred-Suc = W-ssup-forest

 Suc-Pred : (n : 𝓝) β†’ Suc (positive n) (Pred n) = n
 Suc-Pred = W-Ξ·

 𝓝-induction : (P : 𝓝 β†’ 𝓦 Μ‡ )
             β†’ ((p : Ξ© π“₯) (ns : 𝓝 ^ p)
                   β†’ ((h : p holds) β†’ P (ns h))
                   β†’ P (Suc p ns))
             β†’ (n : 𝓝) β†’ P n
 𝓝-induction = W-induction

 𝓝-induction' : (P : 𝓝 β†’ 𝓦 Μ‡ )
              β†’ ((n : 𝓝)
                    β†’ ((p : is-positive n) β†’ P (Pred n p))
                    β†’ P n)
              β†’ (n : 𝓝) β†’ P n
 𝓝-induction' = W-induction'

\end{code}

A criterion for equality on 𝓝.

\begin{code}

 to-𝓝-= : {p q : Ξ© π“₯} {ms : 𝓝 ^ p} {ns : 𝓝 ^ q}
         β†’ (Ξ£ e κž‰ p = q , (ms = ns ∘ transport _holds e))
         β†’ Suc p ms = Suc q ns
 to-𝓝-= = to-W-= (Ξ© π“₯) _holds

 from-𝓝-= : {p q : Ξ© π“₯} {ms : 𝓝 ^ p} {ns : 𝓝 ^ q}
           β†’ Suc p ms = Suc q ns
           β†’ (Ξ£ e κž‰ p = q , (ms = ns ∘ transport _holds e))
 from-𝓝-= = from-W-= (Ξ© π“₯) _holds

\end{code}

The βŠ₯-ary successor function amounts to the number zero, and the ⊀-ary
successor function amounts to the ordinary successor function.

\begin{code}

 private
  Zero' : (𝟘 β†’ 𝓝) β†’ 𝓝
  Zero' = Suc βŠ₯

  Succ' : (πŸ™ β†’ 𝓝) β†’ 𝓝
  Succ' = Suc ⊀

 Zero : 𝓝
 Zero = Zero' unique-from-𝟘

 Succ : 𝓝 β†’ 𝓝
 Succ n = Succ' (Ξ» _ β†’ n)

 being-positive-is-prop : (n : 𝓝) β†’ is-prop (is-positive n)
 being-positive-is-prop n = holds-is-prop (positive n)

 Succ-is-positive : (n : 𝓝) β†’ is-positive (Succ n)
 Succ-is-positive n = ⊀-holds

 Zero-is-not-positive : Β¬ is-positive Zero
 Zero-is-not-positive = βŠ₯-doesnt-hold

 Succ-is-not-Zero : (n : 𝓝) β†’ Succ n β‰  Zero
 Succ-is-not-Zero n e = Zero-is-not-positive
                         (transport
                           is-positive
                           e
                           (Succ-is-positive n))

 Zero-is-not-Succ : (n : 𝓝) β†’ Zero β‰  Succ n
 Zero-is-not-Succ n = β‰ -sym (Succ-is-not-Zero n)

\end{code}

The type of positive numbers.

\begin{code}

 𝓝⁺ : π“₯ ⁺ Μ‡
 𝓝⁺ = Ξ£ n κž‰ 𝓝 , is-positive n

 forget-positivity : 𝓝⁺ β†’ 𝓝
 forget-positivity = pr₁

 forget-positivity-is-embedding : is-embedding forget-positivity
 forget-positivity-is-embedding = pr₁-is-embedding being-positive-is-prop

 Pred⁺ : 𝓝⁺ β†’ 𝓝
 Pred⁺ = uncurry Pred

 Succ⁺ : 𝓝 β†’ 𝓝⁺
 Succ⁺ n = Succ n , Succ-is-positive n

 Pred⁺-Succ⁺ : (n : 𝓝) β†’ Pred⁺ (Succ⁺ n) = n
 Pred⁺-Succ⁺ n = refl

 Succ-lc : left-cancellable Succ
 Succ-lc {m} {n} e = II
  where
   have-e : Succ m = Succ n
   have-e = e

   I : Succ⁺ m = Succ⁺ n
   I = embeddings-are-lc forget-positivity forget-positivity-is-embedding e

   II : m = n
   II = ap Pred⁺ I

\end{code}

The type of natural numbers is embedded into our type of numbers.

\begin{code}

 β„•-to-𝓝 : β„• β†’ 𝓝
 β„•-to-𝓝 zero     = Zero
 β„•-to-𝓝 (succ n) = Succ (β„•-to-𝓝 n)

 β„•-to-𝓝-lc : left-cancellable β„•-to-𝓝
 β„•-to-𝓝-lc {zero}   {zero}   e = refl
 β„•-to-𝓝-lc {zero}   {succ n} e = 𝟘-elim (Zero-is-not-Succ (β„•-to-𝓝 n) e)
 β„•-to-𝓝-lc {succ m} {zero}   e = 𝟘-elim (Succ-is-not-Zero (β„•-to-𝓝 m) e)
 β„•-to-𝓝-lc {succ m} {succ n} e = ap succ (β„•-to-𝓝-lc (Succ-lc e))

\end{code}

We now assume functional and propositional extensionality.

\begin{code}

 module _ (fe : Fun-Ext) (pe : Prop-Ext) where

  𝓝-is-set : is-set 𝓝
  𝓝-is-set = W-is-set (Ξ© π“₯) _holds fe (Ξ©-is-set fe pe)

  Succ-is-embedding : is-embedding Succ
  Succ-is-embedding = lc-maps-into-sets-are-embeddings Succ Succ-lc 𝓝-is-set

  β„•-to-𝓝-is-embedding : is-embedding β„•-to-𝓝
  β„•-to-𝓝-is-embedding = lc-maps-into-sets-are-embeddings β„•-to-𝓝 β„•-to-𝓝-lc 𝓝-is-set

  Succ⁺-Pred⁺ : (n⁺ : 𝓝⁺) β†’ Succ⁺ (Pred⁺ n⁺) = n⁺
  Succ⁺-Pred⁺ (n , pos) = to-subtype-= being-positive-is-prop I
   where
    I = Succ (Pred n pos)         =⟨ refl ⟩
        Suc ⊀ (Ξ» _ β†’ Pred n pos)  =⟨ II ⟩
        Suc (positive n) (Pred n) =⟨ Suc-Pred n ⟩
        n                         ∎
     where
      II = to-𝓝-=
            (((true-gives-equal-⊀ pe fe
                (is-positive n)
                (being-positive-is-prop n)
                pos)⁻¹) ,
            dfunext fe (Ξ» h β†’ ap (Pred n) (being-positive-is-prop n _ _)))

\end{code}

Hence 𝓝⁺ and 𝓝 are equivalent.

\begin{code}

  𝓝⁺-≃-𝓝 : 𝓝⁺ ≃ 𝓝
  𝓝⁺-≃-𝓝 = qinveq Pred⁺ (Succ⁺ , Succ⁺-Pred⁺ , Pred⁺-Succ⁺)

\end{code}

End of the anonymous submodule assuming Fun-Ext and Prop-Ext.

Our numbers "count" the number of elements of certain types.

\begin{code}

 𝓕𝓲𝓷 : 𝓝 β†’ π“₯ Μ‡
 𝓕𝓲𝓷 (ssup p ns) = p holds + (Ξ£ h κž‰ p holds , 𝓕𝓲𝓷 (ns h))

\end{code}

The map Fin : β„• β†’ 𝓀₀ factors as β„•-to-𝓝 : β„• β†’ 𝓝 followed
by 𝓕𝓲𝓷 : 𝓝 β†’ π“₯.

\begin{code}

 Fin-factor : (n : β„•) β†’ 𝓕𝓲𝓷 (β„•-to-𝓝 n) ≃ Fin n
 Fin-factor zero =
  𝟘 + (Ξ£ h κž‰ 𝟘 , 𝓕𝓲𝓷 (𝟘-elim h)) β‰ƒβŸ¨ 𝟘-lneutral ⟩
  (Ξ£ h κž‰ 𝟘 , 𝓕𝓲𝓷 (𝟘-elim h))     β‰ƒβŸ¨ prop-indexed-sum-zero id ⟩
  𝟘                              β– 

 Fin-factor (succ n) = I
  where
   IH : 𝓕𝓲𝓷 (β„•-to-𝓝 n) ≃ Fin n
   IH = Fin-factor n

   I = 𝓕𝓲𝓷 (β„•-to-𝓝 (succ n))          β‰ƒβŸ¨ ≃-refl _ ⟩
       πŸ™ + (Ξ£ h κž‰ πŸ™ , 𝓕𝓲𝓷 (β„•-to-𝓝 n)) β‰ƒβŸ¨ II  ⟩
       πŸ™ + 𝓕𝓲𝓷 (β„•-to-𝓝 n)             β‰ƒβŸ¨ III ⟩
       πŸ™ + Fin n                       β‰ƒβŸ¨ +comm ⟩
       Fin n + πŸ™ {π“₯}                   β‰ƒβŸ¨ IV ⟩
       Fin n + πŸ™ {𝓀₀}                  β‰ƒβŸ¨ ≃-refl _ ⟩
       Fin (succ n)                    β– 
    where
     II  = +-cong (≃-refl πŸ™) πŸ™-lneutral
     III = +-cong (≃-refl πŸ™) IH
     IV  = +-cong (≃-refl _) one-πŸ™-only

\end{code}

Although we can't say that β„•-to-𝓝 n is a surjection, its image has
empty complement.

\begin{code}

 Ξ©-to-𝓝 : Ξ© π“₯ β†’ 𝓝
 Ξ©-to-𝓝 p = Suc p (Ξ» _ β†’ Zero)

 Ξ©-to-𝓝-behaviour : (p : Ξ© π“₯) β†’ is-positive (Ξ©-to-𝓝 p) = (p holds)
 Ξ©-to-𝓝-behaviour p = refl

 Ξ©-is-retract-of-𝓝 : retract Ξ© π“₯ of 𝓝
 Ξ©-is-retract-of-𝓝 = positive , Ξ©-to-𝓝 , (Ξ» x β†’ refl)

 Ξ©-to-𝓝-is-section : is-section Ξ©-to-𝓝
 Ξ©-to-𝓝-is-section = section-is-section Ξ©-is-retract-of-𝓝

 Ξ©-to-𝓝-lc : left-cancellable Ξ©-to-𝓝
 Ξ©-to-𝓝-lc = sections-are-lc Ξ©-to-𝓝 Ξ©-to-𝓝-is-section

 decidability-of-positivity-gives-LEM : ((n : 𝓝) β†’ is-decidable (is-positive n))
                                      β†’  LEM π“₯
 decidability-of-positivity-gives-LEM f p = I
  where
   I : is-decidable (is-positive (Ξ©-to-𝓝 p))
   I = f (Ξ©-to-𝓝 p)

\end{code}

We now assume functional and propositional extensionality
again. Sections are not necessarily embeddings
(https://doi.org/10.2168/LMCS-12(3:9)2016), but sections into sets
are:

\begin{code}

 module _ (fe : Fun-Ext) (pe : Prop-Ext) where

  Ξ©-to-𝓝-is-embedding : is-embedding Ξ©-to-𝓝
  Ξ©-to-𝓝-is-embedding = lc-maps-into-sets-are-embeddings
                          Ξ©-to-𝓝
                          Ξ©-to-𝓝-lc
                          (𝓝-is-set fe pe)

  lc-map-from-Ξ©-to-β„•-gives-LEM : (Ξ£ f κž‰ (Ξ© π“₯ β†’ β„•) , left-cancellable f)
                               β†’ LEM π“₯
  lc-map-from-Ξ©-to-β„•-gives-LEM (f , f-lc) p = I (β„•-is-discrete (f ⊀) (f p))
   where
    I : is-decidable (f ⊀ = f p) β†’ is-decidable (p holds)
    I (inl e) = inl (Idtofun (ap _holds (f-lc e)) ⋆)
    I (inr Ξ½) = inr (Ξ» (h : p holds)
                          β†’ Ξ½ (ap f (holds-gives-equal-⊀ pe fe p h)⁻¹))

\end{code}

We now further assume that propositional truncations exist.

\begin{code}

  module _ (pt : propositional-truncations-exist) where

   open PropositionalTruncation pt
   open import UF.ImageAndSurjection pt

   β„•-to-𝓝-surjection-gives-LEM : is-surjection β„•-to-𝓝 β†’ LEM π“₯
   β„•-to-𝓝-surjection-gives-LEM s p = I
    where
     f : β„• ≃ 𝓝
     f = β„•-to-𝓝 ,
         surjective-embeddings-are-equivs β„•-to-𝓝
          (β„•-to-𝓝-is-embedding fe pe) s

     g : Ξ© π“₯ β†’ β„•
     g = ⌜ f ⌝⁻¹ ∘ Ξ©-to-𝓝

     g-is-emb : is-embedding g
     g-is-emb = ∘-is-embedding
                 Ξ©-to-𝓝-is-embedding
                 (equivs-are-embeddings ⌜ f ⌝⁻¹ ⌜ f ⌝⁻¹-is-equiv)

     I : is-decidable (p holds)
     I = lc-map-from-Ξ©-to-β„•-gives-LEM (g , embeddings-are-lc g g-is-emb) p

\end{code}

Although the above shows that β„•-to-𝓝 isn't necessarily a surjection,
its image has empty complement in the sense that there is no 𝓷 : 𝓝
which is different from β„•-to-𝓝 n for every n : β„•.

\begin{code}

   β„•-to-𝓝-has-empty-complement : Β¬ (Ξ£ 𝓷 κž‰ 𝓝 , ((n : β„•) β†’ β„•-to-𝓝 n β‰  𝓷))
   β„•-to-𝓝-has-empty-complement = uncurry ψ
    where
     ψ : (𝓷 : 𝓝) β†’ Β¬ ((n : β„•) β†’ β„•-to-𝓝 n β‰  𝓷)
     ψ (ssup p ns) f = III IV
      where
       I : Zero β‰  ssup p ns
       I = f 0

       II : Β¬ (p holds) β†’ Zero = ssup p ns
       II Ξ½ = to-𝓝-= ((II₁ ⁻¹) , dfunext fe (Ξ» x β†’ 𝟘-elim x))
        where
         II₁ : p = βŠ₯
         II₁ = fails-gives-equal-βŠ₯ pe fe p Ξ½


       III : ¬¬ (p holds)
       III h = I (II h)

       IV : Β¬ (p holds)
       IV h = ψ (ns h) f'
        where
         IV₁ : p = ⊀
         IV₁ = holds-gives-equal-⊀ pe fe p h

         f' : (n : β„•) β†’ β„•-to-𝓝 n β‰  ns h
         f' n e = f (succ n) IVβ‚‚
          where
           IVβ‚‚ = Succ (β„•-to-𝓝 n)        =⟨ refl ⟩
                 Suc ⊀ (Ξ» _ β†’ β„•-to-𝓝 n) =⟨ IV₃ ⟩
                 Suc ⊀ (Ξ» _ β†’ ns h)     =⟨ IVβ‚„ ⟩
                 Suc p ns               ∎
                  where
                   IV₃ = ap (Ξ» - β†’ Suc ⊀ (Ξ» _ β†’ -)) e
                   IVβ‚„ = to-𝓝-=
                          ((IV₁ ⁻¹) ,
                           dfunext fe (Ξ» _ β†’ ap ns (holds-is-prop p _ _)))

\end{code}

So if excluded middle holds then β„•-to-𝓝 is a surjection and the types β„•
and 𝓝 are equivalent.

\begin{code}

   LEM-gives-β„•-to-𝓝-surjection : LEM (π“₯ ⁺) β†’ is-surjection β„•-to-𝓝
   LEM-gives-β„•-to-𝓝-surjection lem 𝓷 = III
    where
     em = LEM-gives-EM lem

     I : Β¬ ((n : β„•) β†’ β„•-to-𝓝 n β‰  𝓷)
     I = not-Ξ£-implies-Ξ -not β„•-to-𝓝-has-empty-complement 𝓷

     II : βˆƒ n κž‰ β„• , ¬¬ (β„•-to-𝓝 n = 𝓷)
     II = not-Ξ -implies-βˆƒ-not pt em (Ξ» n β†’ negations-are-props fe) I

     III : βˆƒ n κž‰ β„• , β„•-to-𝓝 n = 𝓷
     III = βˆ₯βˆ₯-functor
            (Ξ» (n , Ξ½) β†’ (n , ¬¬-elim (em (β„•-to-𝓝 n = 𝓷) (𝓝-is-set fe pe)) Ξ½))
            II

\end{code}

It is possible to reach the same conclusion assuming LEM π“₯ rather than
LEM (π“₯ ⁺).

\begin{code}

   LEM-gives-β„•-to-𝓝-is-equiv : LEM π“₯ β†’ is-equiv β„•-to-𝓝
   LEM-gives-β„•-to-𝓝-is-equiv lem =
    qinvs-are-equivs β„•-to-𝓝
     ((Ξ» 𝓷 β†’ f 𝓷 (lem (positive 𝓷))) ,
     (Ξ» n β†’ Ξ· n (lem (positive (β„•-to-𝓝 n)))) ,
     (Ξ» 𝓷 β†’ Ξ΅ 𝓷 (lem (positive 𝓷))))
    where
     f : (𝓷 : 𝓝) β†’ is-decidable (is-positive 𝓷) β†’ β„•
     f (ssup p ns) (inr Ξ½) = zero
     f (ssup p ns) (inl h) = succ (f (ns h) (lem (positive (ns h))))

     Ξ· : (n : β„•) (d : is-decidable (is-positive (β„•-to-𝓝 n)))
       β†’ f (β„•-to-𝓝 n) d = n
     Ξ· zero (inr Ξ½)     = refl
     Ξ· (succ n) (inr Ξ½) = 𝟘-elim (Ξ½ (Succ-is-positive (Succ (β„•-to-𝓝 n))))
     Ξ· (succ n) (inl h) = ap succ (Ξ· n (lem (positive (β„•-to-𝓝 n))))

     Ξ΅ : (𝓷 : 𝓝) (d : is-decidable (is-positive 𝓷)) β†’ β„•-to-𝓝 (f 𝓷 d) = 𝓷
     Ξ΅ (ssup p ns) (inr Ξ½) = to-𝓝-= ((I ⁻¹) , dfunext fe (Ξ» x β†’ 𝟘-elim x))
      where
       I : p = βŠ₯
       I = fails-gives-equal-βŠ₯ pe fe p Ξ½
     Ξ΅ (ssup p ns) (inl h) =
      to-𝓝-= ((I ⁻¹) ,
               dfunext fe (Ξ» _ β†’
                β„•-to-𝓝 (f (ns h) (lem (positive (ns h)))) =⟨ II ⟩
                ns h                                      =⟨ III ⟩
                (ns ∘ transport _holds (I ⁻¹)) _          ∎))
      where
       I : p = ⊀
       I = holds-gives-equal-⊀ pe fe p h

       II  = Ξ΅ (ns h) (lem (positive (ns h)))
       III = ap ns (holds-is-prop p h _)

\end{code}

TODO. Show that 𝓝 has the structure of an ordinal. This requires more
work.