Todd Waugh Ambridge, January 2024
# Ternary Boehm encodings of real numbers
\begin{code}
{-# OPTIONS --without-K --safe #-}
open import Integers.Addition renaming (_+_ to _β€+_; _-_ to _β€-_)
open import Integers.Negation renaming (-_ to β€-_ )
open import Integers.Order
open import Integers.Type
open import MLTT.Spartan
open import Notation.Order
open import UF.FunExt
open import UF.Powerset hiding (π)
open import UF.PropTrunc
open import UF.Subsingletons
open import UF.Subsingletons-FunExt
open import UF.SubtypeClassifier
open import TWA.Thesis.Chapter5.BoehmStructure
hiding (downLeft; downMid; downRight; upRight; upLeft; _below_)
open import TWA.Thesis.AndrewSneap.DyadicRationals
renaming (normalise to ΞΉ)
open import TWA.Thesis.Chapter5.Integers
open import TWA.Thesis.Chapter5.SignedDigit
module TWA.Thesis.Chapter5.BoehmVerification
(pt : propositional-truncations-exist)
(fe : FunExt)
(pe : PropExt)
(dy : Dyadics)
where
open PropositionalTruncation pt
open Dyadics dy
renaming ( _β€[1/2]+_ to _+π»_ ; β€[1/2]-_ to -_ ; _β€[1/2]-_ to _-_
; _β€[1/2]*_ to _*_ )
open import TWA.Thesis.AndrewSneap.DyadicReals pe pt fe dy
open import TWA.Thesis.Chapter3.ClosenessSpaces fe hiding (β¨_β© ; ΞΉ)
open import TWA.Thesis.Chapter3.ClosenessSpaces-Examples fe
\end{code}
## Structural operations and properties
\begin{code}
downLeft downMid downRight : β€ β β€
downLeft k = (k β€+ k)
downMid k = (k β€+ k) +pos 1
downRight k = (k β€+ k) +pos 2
upRight upLeft : β€ β β€
upRight k = sign k (num k /2)
upLeft k = upRight (predβ€ k)
_below_ : β€ β β€ β π€β Μ
a below b = downLeft b β€ a β€ downRight b
ternary : (β€ β β€) β π€β Μ
ternary x = (Ξ΄ : β€) β x (succβ€ Ξ΄) below x Ξ΄
π : π€β Μ
π = Ξ£ x κ (β€ β β€) , ternary x
β€[1/2]α΄΅ : π€β Μ
β€[1/2]α΄΅ = Ξ£ (l , r) κ (β€[1/2] Γ β€[1/2]) , l β€ r
ld rd : β€[1/2]α΄΅ β β€[1/2]
ld ((l , r) , _) = l
rd ((l , r) , _) = r
ldβ€rd : (p : β€[1/2]α΄΅) β ld p β€ rd p
ldβ€rd ((l , r) , lβ€r) = lβ€r
_covers_ : β€[1/2]α΄΅ β β€[1/2]α΄΅ β π€β Μ
a covers b = (ld a β€ ld b) Γ (rd b β€ rd a)
covers-refl : (ab : β€[1/2]α΄΅) β ab covers ab
covers-refl ab = β€-refl (ld ab) , β€-refl (rd ab)
covers-trans : (a b c : β€[1/2]α΄΅) β a covers b β b covers c β a covers c
covers-trans a b c (lβ€β , rβ€β) (lβ€β , rβ€β)
= trans' (ld a) (ld b) (ld c) lβ€β lβ€β
, trans' (rd c ) (rd b) (rd a) rβ€β rβ€β
nested positioned : (β€ β β€[1/2]α΄΅) β π€β Μ
nested ΞΆ = (n : β€) β (ΞΆ n) covers (ΞΆ (succβ€ n))
positioned ΞΆ = (Ο΅ : β€[1/2]) β is-positive Ο΅
β Ξ£ n κ β€ , (rd (ΞΆ n) - ld (ΞΆ n)) β€ Ο΅
fully-nested' : (β€ β β€[1/2]α΄΅) β β β π€β Μ
fully-nested' ΞΆ k = (n : β€) β (ΞΆ n) covers (ΞΆ (n +pos k))
fully-nested : (β€ β β€[1/2]α΄΅) β π€β Μ
fully-nested ΞΆ = (n m : β€) β n β€ m β (ΞΆ n) covers (ΞΆ m)
nested-implies-fully-nested'
: (ΞΆ : β€ β β€[1/2]α΄΅) β nested ΞΆ β Ξ (fully-nested' ΞΆ)
nested-implies-fully-nested' ΞΆ Ο 0 n = (0 , refl) , (0 , refl)
nested-implies-fully-nested' ΞΆ Ο (succ k) n
= covers-trans (ΞΆ n) (ΞΆ (succβ€ n)) (ΞΆ (succβ€ (n +pos k))) (Ο n)
(nested-implies-fully-nested' (ΞΆ β succβ€) (Ο β succβ€) k n)
nested-implies-fully-nested
: (ΞΆ : β€ β β€[1/2]α΄΅) β nested ΞΆ β fully-nested ΞΆ
nested-implies-fully-nested ΞΆ Ο n m (k , refl)
= nested-implies-fully-nested' ΞΆ Ο k n
\end{code}
## Verification of the structure of ternary Boehm encodings
\begin{code}
β¦
_β¦ : (Ο : β€ β β€[1/2]α΄΅) β nested Ο β positioned Ο β β-d
β¦
_β¦ Ο Ο Ο = (L , R)
, inhabited-l , inhabited-r
, rounded-l , rounded-r
, is-disjoint , is-located
where
L R : β€[1/2] β Ξ© π€β
L p = (β n κ β€ , p < ld (Ο n)) , β-is-prop
R q = (β n κ β€ , rd (Ο n) < q) , β-is-prop
inhabited-l : inhabited-left L
inhabited-l = β£ ld (Ο (pos 0)) - 1β€[1/2]
, β£ (pos 0)
, (β€[1/2]<-neg (ld (Ο (pos 0))) 1β€[1/2] 0<1β€[1/2]) β£ β£
inhabited-r : inhabited-right R
inhabited-r = β£ (rd (Ο (pos 0)) +π» 1β€[1/2])
, β£ pos 0
, β€[1/2]<-+ (rd (Ο (pos 0))) 1β€[1/2] 0<1β€[1/2] β£ β£
rounded-l : rounded-left L
rounded-l p = ltr , rtl
where
ltr : β n κ β€ , (p <β€[1/2] ld (Ο n))
β β p' κ β€[1/2] , p < p' Γ (β n' κ β€ , (p' <β€[1/2] ld (Ο n')))
ltr = β₯β₯-functor I
where
I : Ξ£ n κ β€ , (p <β€[1/2] ld (Ο n))
β Ξ£ p' κ β€[1/2] , p < p' Γ (β n' κ β€ , (p' <β€[1/2] ld (Ο n')))
I (n , p<ΞΆn) = let (p' , p<p' , p'<ΞΆn) = dense p (ld (Ο n)) p<ΞΆn
in p' , (p<p' , β£ n , p'<ΞΆn β£)
rtl : β p' κ β€[1/2] , p < p' Γ (β n κ β€ , (p' <β€[1/2] ld (Ο n)))
β β n κ β€ , (p <β€[1/2] ld (Ο n))
rtl = β₯β₯-rec β-is-prop I
where
I : Ξ£ p' κ β€[1/2] , p < p' Γ (β n κ β€ , (p' <β€[1/2] ld (Ο n)))
β β n κ β€ , (p <β€[1/2] ld (Ο n))
I (p' , p<p' , te) = β₯β₯-functor II te
where
II : Ξ£ n κ β€ , (p' <β€[1/2] ld (Ο n))
β Ξ£ n κ β€ , (p <β€[1/2] ld (Ο n))
II (n , p'<ΞΆn) = n , (trans p p' (ld (Ο n)) p<p' p'<ΞΆn)
rounded-r : rounded-right R
rounded-r q = ltr , rtl
where
ltr : β n κ β€ , rd (Ο n) < q β β q' κ β€[1/2] , q' < q Γ q' β R
ltr = β₯β₯-functor I
where
I : Ξ£ n κ β€ , rd (Ο n) < q β Ξ£ q' κ β€[1/2] , q' < q Γ q' β R
I (n , ΞΆn<q) = let (q' , ΞΆn<q' , q'<q) = dense (rd (Ο n)) q ΞΆn<q
in q' , (q'<q , β£ n , ΞΆn<q' β£)
rtl : β q' κ β€[1/2] , q' < q Γ (R q' holds) β R q holds
rtl = β₯β₯-rec β-is-prop I
where
I : Ξ£ q' κ β€[1/2] , q' < q Γ (R q' holds) β R q holds
I (q' , q'<q , te) = β₯β₯-functor II te
where
II : Ξ£ n κ β€ , (rd (Ο n) < q') β Ξ£ n κ β€ , (rd (Ο n) <β€[1/2] q)
II (n , ΞΆ<q') = n , (trans (rd (Ο n)) q' q ΞΆ<q' q'<q)
is-disjoint : disjoint L R
is-disjoint p q (tp<x , tx<q)
= β₯β₯-rec (<β€[1/2]-is-prop p q)
(Ξ» ((n , p<l) , (n' , r<q))
β I n n' p<l r<q (β€-dichotomous n n'))
(binary-choice tp<x tx<q)
where
I : (n n' : β€)
β p <β€[1/2] ld (Ο n)
β rd (Ο n') <β€[1/2] q
β (n β€ n') + (n' β€ n)
β p <β€[1/2] q
I n n' p<l r<q (inl nβ€n')
= let p<l' = β€[1/2]<-β€ p (ld (Ο n)) (ld (Ο n')) p<l
(prβ (nested-implies-fully-nested
Ο Ο n n' nβ€n'))
l<q' = β€[1/2]β€-< (ld (Ο n')) (rd (Ο n')) q
(ldβ€rd (Ο n')) r<q
in trans p (ld (Ο n')) q p<l' l<q'
I n n' p<l r<q (inr n'β€n)
= let p<r' = β€[1/2]<-β€ p (ld (Ο n)) (rd (Ο n)) p<l
(ldβ€rd (Ο n))
r<q' = β€[1/2]β€-< (rd (Ο n)) (rd (Ο n')) q
(prβ (nested-implies-fully-nested
Ο Ο n' n n'β€n)) r<q
in trans p (rd (Ο n)) q p<r' r<q'
is-located : located L R
is-located p q p<q
= I (Ο (1/2β€[1/2] * (q - p))
(β€[1/2]<-positive-mult 1/2β€[1/2] (q - p)
0<1/2β€[1/2] (diff-positive p q p<q)))
where
0<Ξ΅ : 0β€[1/2] < (1/2β€[1/2] * (q - p))
0<Ξ΅ = <-pos-mult' 1/2β€[1/2] (q - p) 0<1/2β€[1/2]
(diff-positive p q p<q)
I : (Ξ£ n κ β€ , ((rd (Ο n) - ld (Ο n))
β€β€[1/2] (1/2β€[1/2] * (q - p))))
β (L p holds) β¨ (R q holds)
I (n , lβ) = II (β€[1/2]-ordering-property (rd (Ο n))
(ld (Ο n)) q p lβ)
where
lβ :(rd (Ο n) - ld (Ο n)) < (q - p)
lβ = β€[1/2]β€-< (rd (Ο n) - ld (Ο n)) (1/2β€[1/2] * (q - p))
(q - p) lβ (β€[1/2]-1/2-< (q - p) (diff-positive p q p<q))
II : (rd (Ο n) < q) + (p < ld (Ο n)) β (L p holds) β¨ (R q holds)
II (inl ΞΆ<q) = β£ inr β£ n , ΞΆ<q β£ β£
II (inr p<ΞΆ) = β£ inl β£ n , p<ΞΆ β£ β£
β€Β³ : π€β Μ
β€Β³ = Ξ£ ((l , r) , p) κ ((β€ Γ β€) Γ β€) , l β€ r
β€Β³-to-β€[1/2]α΄΅ : β€Β³ β β€[1/2]α΄΅
β€Β³-to-β€[1/2]α΄΅ (((l , r) , p) , i)
= ((ΞΉ (l , p)) , ΞΉ (r , p)) , normalise-β€2 l r p i
β¦
_β¦' : (Ο : β€ β β€Β³)
β nested (β€Β³-to-β€[1/2]α΄΅ β Ο) β positioned (β€Β³-to-β€[1/2]α΄΅ β Ο)
β β-d
β¦
Ο β¦' = β¦
β€Β³-to-β€[1/2]α΄΅ β Ο β¦
β€Β² : π€β Μ
β€Β² = β€ Γ β€
β€Β²-to-β€Β³ : β€Β² β β€Β³
β€Β²-to-β€Β³ (k , p)
= (((k , k +pos 2) , p)
, β€β€-trans k (succβ€ k) (succβ€ (succβ€ k))
(β€-incrβ€ k) (β€-incrβ€ (succβ€ k)))
β€Β²-to-β€[1/2]α΄΅ : β€Β² β β€[1/2]α΄΅
β€Β²-to-β€[1/2]α΄΅ = β€Β³-to-β€[1/2]α΄΅ β β€Β²-to-β€Β³
β¦
_β¦'' : (Ο : β€ β β€Β²)
β nested (β€Β²-to-β€[1/2]α΄΅ β Ο)
β positioned (β€Β²-to-β€[1/2]α΄΅ β Ο)
β β-d
β¦
_β¦'' = β¦
_β¦' β (β€Β²-to-β€Β³ β_)
normalised : (β€ β β€Β²) β π€β Μ
normalised Ο = (n : β€) β prβ (Ο n) οΌ n
β€Β²-width : ((k , p) : β€Β²)
β (ΞΉ (k +pos 2 , p) - ΞΉ (k , p)) οΌ ΞΉ (pos 2 , p)
β€Β²-width (k , p)
= normalise-negation (k +pos 2) k p
β ap (Ξ» - β ΞΉ (- , p))
(β€-left-succ (succβ€ k) (β€- k)
β ap succβ€ (β€-left-succ k (β€- k))
β ap (succβ€ β succβ€) (β€-sum-of-inverse-is-zero k))
normalised-positioned : (Ο : β€ β β€Β²)
β normalised Ο
β positioned (β€Β²-to-β€[1/2]α΄΅ β Ο)
normalised-positioned Ο Ξ· Ο΅ Ο΅βΊ
= q , transport (_β€ Ο΅) (β€Β²-width (Ο q) β»ΒΉ)
(transport (Ξ» - β ΞΉ (pos 2 , -) β€ Ο΅) (Ξ· q β»ΒΉ) Ξ³)
where
q : β€
q = prβ (β€[1/2]-find-lower Ο΅ Ο΅βΊ)
f : prβ (β€[1/2]-find-lower Ο΅ Ο΅βΊ) οΌ
prβ (Ο (prβ (β€[1/2]-find-lower Ο΅ Ο΅βΊ)))
f = Ξ· q β»ΒΉ
Ξ³ : ΞΉ (pos 2 , q) β€ Ο΅
Ξ³ = <-is-β€β€[1/2] (ΞΉ (pos 2 , q)) Ο΅ (prβ (β€[1/2]-find-lower Ο΅ Ο΅βΊ))
β€β€-succ' : (a : β€) (n : β) β succβ€ a β€ succβ€ (a +pos n)
β€β€-succ' a zero = zero , refl
β€β€-succ' a (succ n) = β€β€-trans _ _ _ (β€β€-succ' a n) (1 , refl)
β€β€-succ : (a b : β€) β a β€ b β succβ€ a β€ succβ€ b
β€β€-succ a b (n , refl) = β€β€-succ' a n
β€β€-pred'
: (a : β€) (n : β) β a β€ (a +pos n)
β€β€-pred' a n = n , refl
β€β€-pred : (a b : β€) β succβ€ a β€ succβ€ b β a β€ b
β€β€-pred a b (n , e)
= transport (a β€_)
(succβ€-lc (β€-left-succ-pos a n β»ΒΉ β e))
(β€β€-pred' a n)
downLeft-downRight-2
: (a : β€) β downLeft (a +pos 2) οΌ downRight a +pos 2
downLeft-downRight-2 a
= β€-left-succ (succβ€ a) (succβ€ (succβ€ a))
β ap succβ€ (β€-left-succ a (succβ€ (succβ€ a)))
β ap (succβ€ ^ 2)
(β€-right-succ a (succβ€ a)
β ap succβ€ (β€-right-succ a a))
β€Β³-width : ((((l , r) , p) , _) : β€Β³)
β (ΞΉ (r , p) - ΞΉ (l , p)) οΌ ΞΉ (r β€- l , p)
β€Β³-width (((l , r) , p) , _) = normalise-negation r l p
ternary-nested : (Ο : β€ β β€Β²)
β normalised Ο
β ternary (prβ β Ο)
β nested (β€Β²-to-β€[1/2]α΄΅ β Ο)
prβ (prβ (ternary-nested Ο Ξ·) f n) = Ξ³
where
Ξ³' : ΞΉ (prβ (Ο n) , n) β€ ΞΉ (prβ (Ο (succβ€ n)) , succβ€ n)
Ξ³' = transport (_β€ ΞΉ (prβ (Ο (succβ€ n)) , succβ€ n))
(normalise-succ' (prβ (Ο n)) n β»ΒΉ)
(normalise-β€2
(prβ (Ο n) β€+ prβ (Ο n))
(prβ (Ο (succβ€ n)))
(succβ€ n)
(prβ (f n)))
Ξ³ : ΞΉ (Ο n) β€ ΞΉ (Ο (succβ€ n))
Ξ³ = transport (Ξ» - β ΞΉ (prβ (Ο n) , -)
β€ ΞΉ (Ο (succβ€ n)))
(Ξ· n β»ΒΉ)
(transport (Ξ» - β ΞΉ (prβ (Ο n) , n)
β€ ΞΉ (prβ (Ο (succβ€ n)) , -))
(Ξ· (succβ€ n) β»ΒΉ)
Ξ³')
prβ (prβ (ternary-nested Ο Ξ·) f n)
= transport (Ξ» - β ΞΉ ((prβ (Ο (succβ€ n)) +pos 2) , -)
β€ ΞΉ ((prβ (Ο n) +pos 2) , prβ (Ο n)))
(Ξ· (succβ€ n) β»ΒΉ)
(transport (Ξ» - β ΞΉ ((prβ (Ο (succβ€ n)) +pos 2) , succβ€ n)
β€ ΞΉ ((prβ (Ο n) +pos 2) , -))
(Ξ· n β»ΒΉ)
(transport (ΞΉ ((prβ (Ο (succβ€ n)) +pos 2) , succβ€ n) β€_)
(normalise-succ' (prβ (Ο n) +pos 2) n β»ΒΉ)
(normalise-β€2
(prβ (Ο (succβ€ n)) +pos 2)
((prβ (Ο n) +pos 2) β€+ (prβ (Ο n) +pos 2))
(succβ€ n)
(transport ((prβ (Ο (succβ€ n)) +pos 2) β€_)
(downLeft-downRight-2 (prβ (Ο n)) β»ΒΉ)
(β€β€-succ _ _ (β€β€-succ _ _ (prβ (f n))))))))
prβ (prβ (ternary-nested Ο Ξ·) f n)
= from-normalise-β€-same-denom _ _ (succβ€ n) Ξ³
where
Ξ³' : ΞΉ (prβ (Ο n) , n) β€ ΞΉ (prβ (Ο (succβ€ n)) , succβ€ n)
Ξ³' = transport (Ξ» - β ΞΉ (prβ (Ο n) , -)
β€ ΞΉ (prβ (Ο (succβ€ n)) , succβ€ n))
(Ξ· n)
(transport (Ξ» - β ΞΉ (Ο n) β€ ΞΉ (prβ (Ο (succβ€ n)) , -))
(Ξ· (succβ€ n))
(prβ (f n)))
Ξ³ : ΞΉ (downLeft (prβ (Ο n)) , succβ€ n)
β€ ΞΉ (prβ (Ο (succβ€ n)) , succβ€ n)
Ξ³ = transport (_β€ ΞΉ (prβ (Ο (succβ€ n)) , succβ€ n))
(normalise-succ' (prβ (Ο n)) n)
Ξ³'
prβ (prβ (ternary-nested Ο Ξ·) f n)
= β€β€-pred _ _ (β€β€-pred _ _
(from-normalise-β€-same-denom _ _ (succβ€ n) Ξ³))
where
Ξ³'' : ΞΉ (prβ (Ο (succβ€ n)) +pos 2 , succβ€ n)
β€ ΞΉ (prβ (Ο n) +pos 2 , n)
Ξ³'' = transport (Ξ» - β ΞΉ (prβ (Ο (succβ€ n)) +pos 2 , -)
β€ ΞΉ (prβ (Ο n) +pos 2 , n))
(Ξ· (succβ€ n))
(transport (Ξ» - β ΞΉ (prβ (Ο (succβ€ n)) +pos 2
, prβ (Ο (succβ€ n)))
β€ ΞΉ (prβ (Ο n) +pos 2 , -))
(Ξ· n)
(prβ (f n)))
Ξ³' : ΞΉ (prβ (Ο (succβ€ n)) +pos 2 , succβ€ n)
β€ ΞΉ (downLeft (prβ (Ο n) +pos 2) , succβ€ n)
Ξ³' = transport (ΞΉ (prβ (Ο (succβ€ n)) +pos 2 , succβ€ n) β€_)
(normalise-succ' (prβ (Ο n) +pos 2) n)
Ξ³''
Ξ³ : ΞΉ (prβ (Ο (succβ€ n)) +pos 2 , succβ€ n)
β€ ΞΉ (downRight (prβ (Ο n)) +pos 2 , succβ€ n)
Ξ³ = transport (Ξ» - β ΞΉ (prβ (Ο (succβ€ n)) +pos 2 , succβ€ n)
β€ ΞΉ (- , succβ€ n))
(downLeft-downRight-2 (prβ (Ο n)))
Ξ³'
to-interval-seq : π β (β€ β β€Β²)
to-interval-seq Ο n = (prβ Ο n) , n
πβnested-normalised
: π β Ξ£ Ο κ (β€ β β€Β²) , (nested (β€Β²-to-β€[1/2]α΄΅ β Ο) Γ normalised Ο)
πβnested-normalised Ο
= to-interval-seq Ο
, prβ (ternary-nested _ i) (prβ Ο)
, i
where
i : normalised (to-interval-seq Ο)
i n = refl
ternary-normalisedβπ
: Ξ£ Ο κ (β€ β β€Β²) , (nested (β€Β²-to-β€[1/2]α΄΅ β Ο) Γ normalised Ο) β π
ternary-normalisedβπ (Ο , Ο , Ο)
= (prβ β Ο) , prβ (ternary-nested Ο Ο) Ο
open import UF.Equiv
covers-is-prop : (a b : β€[1/2]α΄΅) β is-prop (a covers b)
covers-is-prop ((lβ , rβ) , _) ((lβ , rβ) , _)
= Γ-is-prop (β€β€[1/2]-is-prop lβ lβ) (β€β€[1/2]-is-prop rβ rβ)
nested-is-prop : (Ο : β€ β β€[1/2]α΄΅) β is-prop (nested Ο)
nested-is-prop Ο
= Ξ -is-prop (fe _ _) (Ξ» n β covers-is-prop (Ο n) (Ο (succβ€ n)))
normalised-is-prop : (Ο : β€ β β€Β²) β is-prop (normalised Ο)
normalised-is-prop Ο = Ξ -is-prop (fe _ _) (Ξ» _ β β€-is-set)
nested-Γ-normalised-is-prop
: (Ο : β€ β β€Β²)
β is-prop (nested (β€Β²-to-β€[1/2]α΄΅ β Ο) Γ normalised Ο)
nested-Γ-normalised-is-prop Ο
= Γ-is-prop (nested-is-prop (β€Β²-to-β€[1/2]α΄΅ β Ο))
(normalised-is-prop Ο)
below-is-prop : (a b : β€) β is-prop (a below b)
below-is-prop a b
= Γ-is-prop (β€β€-is-prop (downLeft b) a)
(β€β€-is-prop a (downRight b))
ternary-is-prop : (Ο : β€ β β€) β is-prop (ternary Ο)
ternary-is-prop Ο
= Ξ -is-prop (fe _ _) (Ξ» n β below-is-prop (Ο (succβ€ n)) (Ο n))
ternary-normalisedβπ : (Ξ£ Ο κ (β€ β β€Β²)
, (nested (β€Β²-to-β€[1/2]α΄΅ β Ο)
Γ normalised Ο))
β π
ternary-normalisedβπ
= qinveq ternary-normalisedβπ (πβnested-normalised , Ο , ΞΌ)
where
Ο : πβnested-normalised β ternary-normalisedβπ βΌ id
Ο (Ο , Ο , Ο)
= to-subtype-οΌ nested-Γ-normalised-is-prop (dfunext (fe _ _) Ξ³)
where
Ξ³ : to-interval-seq (ternary-normalisedβπ (Ο , Ο , Ο)) βΌ Ο
Ξ³ i = ap (prβ (Ο i) ,_) (Ο i β»ΒΉ)
ΞΌ : (ternary-normalisedβπ β πβnested-normalised) βΌ id
ΞΌ (Ο , b) = to-subtype-οΌ ternary-is-prop (dfunext (fe _ _) Ξ³)
where
Ξ³ : (Ξ» x β prβ (prβ (πβnested-normalised (Ο , b)) x)) βΌ Ο
Ξ³ i = refl
πβnested-positioned
: π
β Ξ£ Ο κ (β€ β β€Β²) , (nested (β€Β²-to-β€[1/2]α΄΅ β Ο)
Γ positioned (β€Β²-to-β€[1/2]α΄΅ β Ο))
πβnested-positioned Ο
= Ο' , Ο , normalised-positioned Ο' Ο
where
Ξ³ = πβnested-normalised Ο
Ο' = prβ Ξ³
Ο = prβ (prβ Ξ³)
Ο = prβ (prβ Ξ³)
β¦_β§ : π β β-d
β¦ Ο β§ = β¦
Ο' β¦'' Ο Ο
where
Ξ³ = πβnested-positioned Ο
Ο' = prβ Ξ³
Ο = prβ (prβ Ξ³)
Ο = prβ (prβ Ξ³)
\end{code}
## Representing compact intervals
\begin{code}
CompactInterval : β€ Γ β€ β π€β Μ
CompactInterval (k , Ξ΄) = Ξ£ (x , _) κ π , x(Ξ΄) οΌ k
CompactInterval2 : β€ Γ β€ β π€β Μ
CompactInterval2 (k , Ξ΄)
= Ξ£ Ο κ (β β β€) , (Ο 0 below k)
Γ ((n : β) β Ο (succ n) below Ο n)
CompactInterval-1-to-2 : ((k , Ξ΄) : β€ Γ β€)
β CompactInterval (k , Ξ΄)
β CompactInterval2 (k , Ξ΄)
CompactInterval-1-to-2 (k , Ξ΄) ((Ο' , b') , e')
= Ο , transport (Ο' (succβ€ Ξ΄) below_) e' (b' Ξ΄) , bβ
where
Ο : β β β€
Ο n = Ο' (succβ€ (Ξ΄ +pos n))
bβ : Ο 0 below Ο' Ξ΄
bβ = b' Ξ΄
bβ : (n : β) β Ο (succ n) below Ο n
bβ n = b' (succβ€ (Ξ΄ +pos n))
replace-right''
: ((k , Ξ΄) : β€ Γ β€) β (β β β€) β (n : β€) β trich-locate n Ξ΄ β β€
replace-right'' (k , Ξ΄) Ο n (inl (i , n+siοΌΞ΄))
= (upRight ^ succ i) k
replace-right'' (k , Ξ΄) Ο n (inr (inl refl))
= k
replace-right'' (k , Ξ΄) Ο n (inr (inr (i , Ξ΄+siοΌn)))
= Ο i
replace-right''-correct
: ((k , Ξ΄) : β€ Γ β€)
β (Ο : β β β€)
β Ο 0 below k
β ((n : β) β Ο (succ n) below Ο n)
β (n : β€)
β (Ξ· : trich-locate n Ξ΄)
β replace-right'' (k , Ξ΄) Ο (succβ€ n) (β€-trich-succ n Ξ΄ Ξ·)
below replace-right'' (k , Ξ΄) Ο n Ξ·
replace-right''-correct (k , Ξ΄) Ο bβ bβ n (inl (0 , refl))
= above-implies-below _ _ (upRight-above _)
replace-right''-correct (k , Ξ΄) Ο bβ bβ n (inl (succ i , refl))
= above-implies-below _ _ (upRight-above _)
replace-right''-correct (k , Ξ΄) Ο bβ bβ n (inr (inl refl))
= bβ
replace-right''-correct (k , Ξ΄) Ο bβ bβ n (inr (inr (i , refl)))
= bβ i
CompactInterval-2-to-1 : ((k , Ξ΄) : β€ Γ β€)
β CompactInterval2 (k , Ξ΄)
β CompactInterval (k , Ξ΄)
CompactInterval-2-to-1 (k , Ξ΄) (Ο' , b'β , b'β)
= (Ο , b) , e
where
Ο : β€ β β€
Ο n = replace-right'' (k , Ξ΄) Ο' n (β€-trichotomous n Ξ΄)
b' : (n : β€) β replace-right'' (k , Ξ΄) Ο' (succβ€ n)
(β€-trich-succ n Ξ΄ (β€-trichotomous n Ξ΄))
below
replace-right'' (k , Ξ΄) Ο' n (β€-trichotomous n Ξ΄)
b' n = replace-right''-correct (k , Ξ΄) Ο' b'β b'β n
(β€-trichotomous n Ξ΄)
b : (n : β€) β Ο (succβ€ n) below Ο n
b n = transport (Ξ» - β replace-right'' (k , Ξ΄) Ο' (succβ€ n) -
below Ο n)
(β€-trichotomous-is-prop _ _
(β€-trich-succ n Ξ΄ (β€-trichotomous n Ξ΄))
(β€-trichotomous (succβ€ n) Ξ΄))
(b' n)
e : Ο Ξ΄ οΌ k
e = ap (replace-right'' (k , Ξ΄) Ο' Ξ΄)
(β€-trichotomous-is-prop _ _ (β€-trichotomous Ξ΄ Ξ΄)
(inr (inl refl)))
_β_ : π β π β π€β Μ
(Οβ , _) β (Οβ , _) = Ξ£ Ξ΄ κ β€ , ((n : β€) β Ξ΄ β€ n β Οβ n οΌ Οβ n)
CompactInterval-β
: ((k , Ξ΄) : β€ Γ β€)
β ((Ο , b) : CompactInterval (k , Ξ΄))
β Ο β prβ (CompactInterval-2-to-1 (k , Ξ΄)
(CompactInterval-1-to-2 (k , Ξ΄) (Ο , b)))
CompactInterval-β (k , Ξ΄) ((Ο' , b') , e') = Ξ΄ , Ξ³
where
Ο = prβ (CompactInterval-2-to-1 (k , Ξ΄)
(CompactInterval-1-to-2 (k , Ξ΄) ((Ο' , b') , e')))
Ξ³ : (n : β€) β Ξ΄ β€ n β Ο' n οΌ prβ Ο n
Ξ³ n (0 , refl)
= e'
β ap (replace-right'' (k , Ξ΄)
(prβ (CompactInterval-1-to-2 (k , Ξ΄) ((Ο' , b') , e'))) Ξ΄)
(β€-trichotomous-is-prop _ _
(β€-trichotomous Ξ΄ Ξ΄)
(inr (inl refl))) β»ΒΉ
Ξ³ n (succ i , refl)
= ap (replace-right'' (k , Ξ΄)
(prβ (CompactInterval-1-to-2 (k , Ξ΄) ((Ο' , b') , e')))
(Ξ΄ +pos succ i))
(β€-trichotomous-is-prop _ _
(β€-trichotomous (Ξ΄ +pos succ i) Ξ΄)
(inr (inr (i , β€-left-succ-pos Ξ΄ i)))) β»ΒΉ
down-to-π : (a b : β€) β a below' b β π
down-to-π a b (inl dL ) = β1
down-to-π a b (inr (inl dM)) = O
down-to-π a b (inr (inr dR)) = +1
π-to-down : (a : π) β (β€ β β€)
π-to-down β1 = downLeft
π-to-down O = downMid
π-to-down +1 = downRight
π-down-eq : (a b : β€) (d : a below' b)
β π-to-down (down-to-π a b d) b οΌ a
π-down-eq a b (inl dL ) = dL β»ΒΉ
π-down-eq a b (inr (inl dM)) = dM β»ΒΉ
π-down-eq a b (inr (inr dR)) = dR β»ΒΉ
down-π-eq : (a : π) (b : β€)
β (e : π-to-down a b below' b)
β down-to-π (π-to-down a b) b e οΌ a
down-π-eq β1 b (inl e) = refl
down-π-eq O b (inl e)
= π-elim (downLeftβ downMid b b refl (e β»ΒΉ))
down-π-eq +1 b (inl e)
= π-elim (downLeftβ downRight b b refl (e β»ΒΉ))
down-π-eq β1 b (inr (inl e))
= π-elim (downLeftβ downMid b b refl e)
down-π-eq O b (inr (inl e)) = refl
down-π-eq +1 b (inr (inl e))
= π-elim (downMidβ downRight b b refl (e β»ΒΉ))
down-π-eq β1 b (inr (inr e))
= π-elim (downLeftβ downRight b b refl e)
down-π-eq O b (inr (inr e))
= π-elim (downMidβ downRight b b refl e)
down-π-eq +1 b (inr (inr e)) = refl
CI2-to-πα΄Ί : ((k , i) : β€ Γ β€) β CompactInterval2 (k , i) β πα΄Ί
CI2-to-πα΄Ί (k , i) (Ο , bβ , bβ) 0
= down-to-π (Ο 0) k (below-implies-below' (Ο 0) k bβ)
CI2-to-πα΄Ί (k , i) (Ο , bβ , bβ) (succ n)
= down-to-π (Ο (succ n)) (Ο n)
(below-implies-below' (Ο (succ n)) (Ο n) (bβ n))
π-to-down-is-below : (a : π) (k : β€) β π-to-down a k below k
π-to-down-is-below β1 k = downLeft-below k
π-to-down-is-below O k = downMid-below k
π-to-down-is-below +1 k = downRight-below k
πα΄Ί-to-CI2 : ((k , i) : β€ Γ β€) β πα΄Ί β CompactInterval2 (k , i)
πα΄Ί-to-CI2 (k , i) Ξ± = Ο , bβ , bβ
where
Ο : β β β€
Ο 0 = π-to-down (Ξ± 0) k
Ο (succ n) = π-to-down (Ξ± (succ n)) (Ο n)
bβ : Ο 0 below k
bβ = π-to-down-is-below (Ξ± 0) k
bβ : (n : β) β Ο (succ n) below Ο n
bβ n = π-to-down-is-below (Ξ± (succ n)) (Ο n)
integer-approx : πα΄Ί β (β β β€)
integer-approx Ξ± = prβ (πα΄Ί-to-CI2 (negsucc 0 , pos 0) Ξ±)
π-possibilities : (a : π) β (a οΌ β1) + (a οΌ O) + (a οΌ +1)
π-possibilities β1 = inl refl
π-possibilities O = inr (inl refl)
π-possibilities +1 = inr (inr refl)
CI2-criteria : ((k , i) : β€ Γ β€) β (β β β€) β π€β Μ
CI2-criteria (k , i) Ο = (Ο 0 below k)
Γ ((n : β) β Ο (succ n) below Ο n)
CI2-prop
: ((k , i) : β€ Γ β€)
β (Ο : β β β€)
β is-prop (CI2-criteria (k , i) Ο)
CI2-prop (k , i) Ο
= Γ-is-prop (below-is-prop (Ο 0) k)
(Ξ -is-prop (fe _ _) (Ξ» n β below-is-prop (Ο (succ n)) (Ο n)))
CompactInterval2-ternary
: ((k , i) : β€ Γ β€) β CompactInterval2 (k , i) β πα΄Ί
CompactInterval2-ternary (k , i)
= qinveq (CI2-to-πα΄Ί (k , i)) (πα΄Ί-to-CI2 (k , i) , Ξ· , ΞΌ)
where
Ξ· : (πα΄Ί-to-CI2 (k , i)) β (CI2-to-πα΄Ί (k , i)) βΌ id
Ξ· (Ο , bβ , bβ)
= to-subtype-οΌ (CI2-prop (k , i)) (dfunext (fe _ _) Ξ³)
where
Ο' = prβ (πα΄Ί-to-CI2 (k , i) (CI2-to-πα΄Ί (k , i) (Ο , bβ , bβ)))
Ξ³ : Ο' βΌ Ο
Ξ³ zero = π-down-eq (Ο 0) k (below-implies-below' (Ο 0) k bβ)
Ξ³ (succ n)
= ap (π-to-down (down-to-π (Ο (succ n)) (Ο n)
(below-implies-below' (Ο (succ n)) (Ο n) (bβ n))))
(Ξ³ n)
β π-down-eq (Ο (succ n)) (Ο n)
(below-implies-below' (Ο (succ n)) (Ο n) (bβ n))
ΞΌ : (CI2-to-πα΄Ί (k , i)) β (πα΄Ί-to-CI2 (k , i)) βΌ id
ΞΌ Ξ± = dfunext (fe _ _) Ξ³
where
Ξ±' = πα΄Ί-to-CI2 (k , i) Ξ±
Ξ³ : CI2-to-πα΄Ί (k , i) Ξ±' βΌ Ξ±
Ξ³ 0 = down-π-eq (Ξ± 0) k _
Ξ³ (succ n) = down-π-eq (Ξ± (succ n)) _ _
CI2-ClosenessSpace
: ((k , i) : β€ Γ β€)
β is-closeness-space (CompactInterval2 (k , i))
CI2-ClosenessSpace (k , i)
= Ξ£-clospace (CI2-criteria (k , i)) (CI2-prop (k , i))
(discrete-seq-clospace (Ξ» _ β β€-is-discrete))
_split-below_ : β€ β β€ β π€β Μ
n split-below m = (n οΌ downLeft m) + (n οΌ downRight m)
split-below-is-prop : (n m : β€) β is-prop (n split-below m)
split-below-is-prop n m
= +-is-prop β€-is-set β€-is-set
(Ξ» l r β downLeftβ downRight m m refl (l β»ΒΉ β r))
CI3-criteria : ((k , i) : β€ Γ β€) β (β β β€) β π€β Μ
CI3-criteria (k , i) Ο = (Ο 0 split-below k)
Γ ((n : β) β Ο (succ n) split-below Ο n)
CI3-prop : ((k , i) : β€ Γ β€)
β (Ο : β β β€)
β is-prop (CI3-criteria (k , i) Ο)
CI3-prop (k , i) Ο
= Γ-is-prop (split-below-is-prop (Ο 0) k)
(Ξ -is-prop (fe _ _)
(Ξ» n β split-below-is-prop (Ο (succ n)) (Ο n)))
CompactInterval3 : β€ Γ β€ β π€β Μ
CompactInterval3 (k , i) = Ξ£ (CI3-criteria (k , i))
split-below-implies-below : (n m : β€) β n split-below m β n below m
split-below-implies-below n m (inl refl) = (0 , refl) , (2 , refl)
split-below-implies-below n m (inr refl) = (2 , refl) , (0 , refl)
CI3-to-CI2 : ((k , i) : β€ Γ β€)
β CompactInterval3 (k , i)
β CompactInterval2 (k , i)
CI3-to-CI2 (k , i) (Ο , bβ , bβ)
= Ο , split-below-implies-below (Ο 0) k bβ
, Ξ» n β split-below-implies-below (Ο (succ n)) (Ο n) (bβ n)
CI3-ClosenessSpace
: ((k , i) : β€ Γ β€) β is-closeness-space (CompactInterval3 (k , i))
CI3-ClosenessSpace (k , i)
= Ξ£-clospace (CI3-criteria (k , i)) (CI3-prop (k , i))
(discrete-seq-clospace (Ξ» _ β β€-is-discrete))
πα΄Ί = β β π
down-to-π : (a b : β€) β a split-below b β π
down-to-π a b (inl dL) = β
down-to-π a b (inr dR) = β
π-to-down : (a : π) β (β€ β β€)
π-to-down β = downLeft
π-to-down β = downRight
π-to-down-is-below : (a : π) (k : β€) β π-to-down a k split-below k
π-to-down-is-below β k = inl refl
π-to-down-is-below β k = inr refl
π-down-eq : (a b : β€) (d : a split-below b)
β π-to-down (down-to-π a b d) b οΌ a
π-down-eq a b (inl dL) = dL β»ΒΉ
π-down-eq a b (inr dR) = dR β»ΒΉ
down-π-eq : (a : π) (b : β€) (e : π-to-down a b split-below b)
β down-to-π (π-to-down a b) b e οΌ a
down-π-eq β b (inl e) = refl
down-π-eq β b (inl e) = π-elim (downLeftβ downRight b b refl (e β»ΒΉ))
down-π-eq β b (inr e) = π-elim (downLeftβ downRight b b refl e)
down-π-eq β b (inr e) = refl
CI3-to-πα΄Ί
: ((k , i) : β€ Γ β€) β CompactInterval3 (k , i) β πα΄Ί
CI3-to-πα΄Ί (k , i) (Ο , bβ , bβ) 0
= down-to-π (Ο 0) k bβ
CI3-to-πα΄Ί (k , i) (Ο , bβ , bβ) (succ n)
= down-to-π (Ο (succ n)) (Ο n) (bβ n)
πα΄Ί-to-CI3 : ((k , i) : β€ Γ β€) β πα΄Ί β CompactInterval3 (k , i)
πα΄Ί-to-CI3 (k , i) Ξ± = Ο , bβ , bβ
where
Ο : β β β€
Ο 0 = π-to-down (Ξ± 0) k
Ο (succ n) = π-to-down (Ξ± (succ n)) (Ο n)
bβ : Ο 0 split-below k
bβ = π-to-down-is-below (Ξ± 0) k
bβ : (n : β) β Ο (succ n) split-below Ο n
bβ n = π-to-down-is-below (Ξ± (succ n)) (Ο n)
CompactInterval3-cantor
: ((k , i) : β€ Γ β€) β CompactInterval3 (k , i) β πα΄Ί
CompactInterval3-cantor (k , i)
= qinveq (CI3-to-πα΄Ί (k , i)) (πα΄Ί-to-CI3 (k , i) , Ξ· , ΞΌ)
where
Ξ· : (πα΄Ί-to-CI3 (k , i)) β (CI3-to-πα΄Ί (k , i)) βΌ id
Ξ· (Ο , bβ , bβ)
= to-subtype-οΌ (CI3-prop (k , i)) (dfunext (fe _ _) Ξ³)
where
Ο' = prβ (πα΄Ί-to-CI3 (k , i) (CI3-to-πα΄Ί (k , i) (Ο , bβ , bβ)))
Ξ³ : Ο' βΌ Ο
Ξ³ 0 = π-down-eq (Ο 0) k bβ
Ξ³ (succ n)
= ap (π-to-down (down-to-π (Ο (succ n)) (Ο n) (bβ n))) (Ξ³ n)
β π-down-eq (Ο (succ n)) (Ο n) (bβ n)
ΞΌ : (CI3-to-πα΄Ί (k , i)) β (πα΄Ί-to-CI3 (k , i)) βΌ id
ΞΌ Ξ± = dfunext (fe _ _) Ξ³
where
Ξ±' = πα΄Ί-to-CI3 (k , i) Ξ±
Ξ³ : CI3-to-πα΄Ί (k , i) Ξ±' βΌ Ξ±
Ξ³ 0 = down-π-eq (Ξ± 0) k (π-to-down-is-below (Ξ± 0) k)
Ξ³ (succ n) = down-π-eq (Ξ± (succ n)) _ _
\end{code}