Todd Waugh Ambridge, January 2024 # Interval object finite approximations \begin{code} {-# 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) \end{code}