IntervalObjectApproximation

Todd Waugh Ambridge, January 2024

# Interval object finite approximations

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

open import MLTT.Spartan
open import UF.FunExt
open import Naturals.Addition renaming (_+_ to _+ℕ_)
open import UF.Base
open import MLTT.SpartanList hiding (_∷_;⟨_⟩;[_])

open import TWA.Thesis.Chapter2.Vectors
open import TWA.Thesis.Chapter2.Sequences
open import TWA.Thesis.Chapter5.IntervalObject

module TWA.Thesis.Chapter5.IntervalObjectApproximation
 (fe : FunExt)
 (io : Interval-object fe 𝓤)
 where

open basic-interval-object-development fe io hiding (−1 ; O ; +1)

first-_ : {A : 𝓥 ̇ } (n : )  (  A)  Vec A n
first- n = Seq-to-Vec n

append-one : {X : 𝓥 ̇ } {n : }  X  Vec X n  Vec X (succ n)
append-one {𝓥} {X} {0} y ⟨⟩ = y :: ⟨⟩
append-one {𝓥} {X} {succ n} y (x :: xs) = x :: append-one y xs

m : {n : }  Vec 𝕀 (succ n)  𝕀
m {0} (x :: ⟨⟩) = x
m {succ n} (x :: xs) = x  m xs

n-approx : (x y :   𝕀) (n : )  𝓤 ̇
n-approx x y n = Σ (z , w)  𝕀 × 𝕀
               , m (append-one z ((first- n) x))
                m (append-one w ((first- n) y))

approximation : 𝓤 ̇
approximation = (x y :   𝕀)  Π (n-approx x y)  M x  M y

multi-canc : (w z : 𝕀) (y :   𝕀) (n : )
            m (append-one w ((first- n) y))
            m (append-one z ((first- n) y))
            w  z
multi-canc w .w y 0 refl = refl
multi-canc w z y (succ n) e
 = multi-canc w z (y  succ) n
     (⊕-canc _ _ _ (⊕-comm _ _  e  ⊕-comm _ _))

one-sided-approx : (x : 𝕀) (y :   𝕀)
                  ((n : )
                     Σ w  𝕀 , x  m (append-one w ((first- n) y)))
                  x  M y
one-sided-approx x y f = M-prop₂ ws y γ where
  ws :   𝕀
  ws 0 = x
  ws (succ i) = pr₁ (f (succ i))
  γ : (i : )  ws i  (y i  ws (succ i))
  γ 0 = pr₂ (f 1)
  γ (succ i) = multi-canc
                 (ws (succ i))
                 (y (succ i)  ws (succ (succ i)))
                 y
                 (succ i) (pr₂ (f (succ i)) ⁻¹
              (pr₂ (f (succ (succ i)))
              δ'' y (ws (succ (succ i))) i))
   where
    δ'' : (y :   𝕀) (z : 𝕀) (i : )
         m (append-one z ((first- (succ (succ i))) y))
         m (append-one (y (succ i)  z) ((first- (succ i)) y))
    δ'' y z 0 = refl
    δ'' y z (succ i) = ap (y 0 ⊕_) (δ'' (y  succ) z i)

_++'_ : {n : } {X : 𝓤 ̇ }  Vec X n  (  X)  (  X)
_++'_ {n} {X} v α = Vec-to-Seq n α v

m-prop₁ : (n : ) (a b c :   𝕀) (e : 𝕀)
         (m (append-one e ((first- succ n) a))
           M ((first- n) b ++' λ i  (c (succ (i +ℕ n)))))
         M ((append-one (a n  e) ((first- n) λ i  a i  b i))
            ++'  i  c (succ (i +ℕ n))))
m-prop₁ 0 a b c e = M-prop₁ _ ⁻¹
m-prop₁ (succ n) a b c e
 = ap ((a 0  (a 1  m (append-one e ((first- n) (a  succ  succ))))) ⊕_)
     (M-prop₁ ((first- succ n) b ++'  i  c (succ (i +ℕ succ n)))))
      ⊕-tran _ _ _ _
      ap ((a 0  b 0) ⊕_) (m-prop₁ n (tail a) (tail b) (tail c) e)
      M-prop₁ (append-one (a (succ n)  e)
         ((first- succ n)  i  a i  b i))
         ++'  i  c (succ (i +ℕ succ n)))) ⁻¹

M-append-= : (x :   𝕀) (n : )
             M x
             M (append-one (x n) ((first- n) x)
                   ++'  i  x (succ (i +ℕ n))))
M-append-= x 0 = M-prop₁ x
                    M-prop₁
                       (append-one (x 0) ((first- 0) x)
                         ++'  i  x (succ (i +ℕ 0)))) ⁻¹
M-append-= x (succ n) = M-prop₁ x
                        ap (x 0 ⊕_) (M-append-= (tail x) n)
                        M-prop₁
                           (append-one (x (succ n)) ((first- succ n) x)
                             ++'  i  x (succ (i +ℕ succ n)))) ⁻¹

M-append-++-= : (x y :   𝕀) (n : )
      M  i  x i  y i)
      (m (append-one (y n) ((first- succ n) x))
          M (((first- n) y) ++'  i  (x (succ (i +ℕ n)))
                                       (y (succ (i +ℕ n))))))
M-append-++-= x y n = M-append-=  i  x i  y i) n
            m-prop₁ n x y  i  x i  y i) (y n) ⁻¹

append-++-= : (x y :   𝕀) (w : 𝕀) (n : )
         ((append-one w ((first- n) x)) ++' y)
         (((first- n) x) ++' (w  y))
append-++-= x y w 0 = dfunext (fe 𝓤₀ 𝓤) (ℕ-induction refl λ _ _  refl)
append-++-= x y w (succ n) = dfunext (fe 𝓤₀ 𝓤) (ℕ-induction refl
                            k _  happly (append-++-= (tail x) y w n) k))

tail-_ : {X : 𝓤 ̇ }    (  X)  (  X)
(tail- 0) α = α
(tail- succ n) α = tail ((tail- n) α)

M→m : (α :   𝕀) (n : )
     M α  m (append-one (M ((tail- n) α)) ((first- n) α))
M→m α 0 = refl
M→m α (succ n) = M-prop₁ α
                ap (α 0 ⊕_)
               (transport
                     -  M (tail α)
                          m (append-one (M -) ((first- n) (tail α))))
                    (γ α n) (M→m (tail α) n))
  where
    γ : (α :   𝕀) (n : )  ((tail- n) (tail α))  ((tail- succ n) α)
    γ α 0 = refl
    γ α (succ n) = ap tail (γ α n)

tail-first' : {X : 𝓤 ̇ } {m : }
             (a : Vec X (succ m)) (β :   X) (n : )
             (tail- succ n) (a ++' β)
             (tail- n) (tl {𝓤} {m}  _  X} a ++' β)
tail-first' {X} {m} (_ :: xs) β 0 = refl
tail-first' {X} {m} (_ :: xs) β (succ n)
 = ap tail (tail-first' {X} {m} (_ :: xs) β n)

tail-first : {X : 𝓤 ̇ }
            (α β :   X) (n : )
            (tail- n) (((first- n) α) ++' β)  β
tail-first α β 0 = refl
tail-first α β (succ n)
 = tail-first' ((first- (succ n)) α) β n  tail-first (tail α) β n

first-first : {X : 𝓤 ̇ }
             (α β :   X) (n : )
             ((first- n) ((first- n) α ++' β))  (first- n) α
first-first α β 0 = refl
first-first α β (succ n) = ap (α 0 ::_) (first-first (tail α) β n)

approx-holds : approximation
approx-holds x y f = ⊕-canc (M x) (M y) (M (tail z)) γ
 where
  z w :   𝕀
  z n = pr₁ (pr₁ (f n))
  w n = pr₂ (pr₁ (f n))
  w'' : (n : )  (  𝕀)
  w'' n = (y n  w (succ n))
          i  x (succ (i +ℕ n))
           z (succ (succ (i +ℕ n))))
  γ'' : (n : )  m (append-one (z n) ((first- n) x))
                 m (append-one (w n) ((first- n) y))
  γ'' n = pr₂ (f n)
  γ' : (n : )  Σ w*  𝕀 , M  i  x i  (tail z) i)
      m (append-one w* ((first- n)  i  y i  (tail z) i)))
  γ' n = M (w'' n)
       , (M-append-++-= x (tail z) n
        ap (_⊕ M ((first- n) (tail z) ++'  i  x (succ (i +ℕ n))
                                            tail z (succ (i +ℕ n)))))
            (γ'' (succ n))
        m-prop₁ n y (tail z)  i  x i  (tail z) i) (w (succ n))
        ap M (append-++-=  i  y i  (tail z) i)
                 i  x (succ (i +ℕ n))  (tail z) (succ (i +ℕ n)))
            (w'' n 0) n)
        M→m (((first- n)  i  y i  (tail z) i) ++' (w'' n))) n
        (ap  -  m (append-one (M -)
               ((first- n)
                 ((first- n)
                  i  y i  (tail z) i) ++' (w'' n)))))
          (tail-first  i  y i  (tail z) i) (w'' n) n)
         ap  -  m (append-one (M (w'' n)) -))
           (first-first  i  y i  (tail z) i) (w'' n) n)))
  γ : (M x  M (z  succ))  (M y  M (z  succ))
  γ = M-hom x (z  succ)
         one-sided-approx
            (M  i  x i  pr₁ (pr₁ (f (succ i)))))
             i  y i  z (succ i))
            γ'
         M-hom y (z  succ) ⁻¹

n-approx' : (  𝕀)  (  𝕀)    𝓤 ̇
n-approx' x y n = Σ (z , w)  𝕀 × 𝕀
                , m (append-one z ((first- (succ n)) x))
                 m (append-one w ((first- (succ n)) y))

n-approx'→n-approx
 : (x y :   𝕀)  Π (n-approx' x y)  Π (n-approx x y)
n-approx'→n-approx x y f 0 = (u , u) , refl
n-approx'→n-approx x y f (succ n) = f n

fg-n-approx' : {X : 𝓥 ̇ }  (f g : X    𝕀)    𝓤  𝓥 ̇
fg-n-approx' f g n
 = (∀ x  n-approx' (f x) (g x) n)
  (∀ x  n-approx' (f x) (g x) (succ n))

fg-approx-holds : {X : 𝓥 ̇ } (f g : X    𝕀)
                 Π (fg-n-approx' f g)
                  x  M (f x)  M (g x)
fg-approx-holds {_} {X} f g h x
 = approx-holds (f x) (g x) (n-approx'→n-approx (f x) (g x) (γ x))
 where
  γ : (x : X)  Π (n-approx' (f x) (g x))
  γ x 0 = (g x 0 , f x 0) , ⊕-comm _ _
  γ x (succ n) = h n  y  γ y n) x

cancellation-holds : cancellative fe _⊕_
cancellation-holds a b c f = M-idem a ⁻¹  γ  M-idem b
 where
  n-approx-c : (i : )
              m (append-one c ((first- i)  _  a)))
              m (append-one c ((first- i)  _  b)))
  n-approx-c 0 = refl
  n-approx-c 1 = f
  n-approx-c (succ (succ i))
   = (   a     (a  wa)) =⟨ ap (_⊕ (a  wa)) (⊕-idem a ⁻¹) 
     ((a  a)  (a  wa)) =⟨ ap ((a  a) ⊕_) (n-approx-c (succ i)) 
     ((a  a)  (b  wb)) =⟨ ⊕-tran a a b wb 
     ((a  b)  (a  wb)) =⟨ ap  -  (a  b)  (a  -)) (IH₂ ⁻¹) 
     ((a  b)  (a  wa)) =⟨ ap ((a  b) ⊕_) (n-approx-c (succ i)) 
     ((a  b)  (b  wb)) =⟨ ap (_⊕ (b  wb)) (⊕-comm a b) 
     ((b  a)  (b  wb)) =⟨ ⊕-tran b a b wb 
     ((b  b)  (a  wb)) =⟨ ap  -  (b  b)  (a  -)) (IH₂ ⁻¹) 
     ((b  b)  (a  wa)) =⟨ ap ((b  b) ⊕_) (n-approx-c (succ i)) 
     ((b  b)  (b  wb)) =⟨ ap (_⊕ (b  wb)) (⊕-idem b) 
     (   b     (b  wb)) 
   where
    wa = m (append-one c ((first- i)  _  a)) )
    wb = m (append-one c ((first- i)  _  b)))
    IH₂ : wa  wb
    IH₂ = n-approx-c i
  γ : M  _  a)  M  _  b)
  γ = approx-holds  _  a)  _  b)  i  (c , c) :: n-approx-c i)