Skip to content

TWA.Escardo-Simpson-LICS2001

Todd Waugh Ambridge, 27th April 2020.

We formalize, in univalent mathematics in Agda, some definitions in

M.H. Escardo and A. Simpson. A universal characterization of the
closed Euclidean interval (extended abstract). Proceedings of the 16th
Annual IEEE Symposium on Logic in Computer Science,
pp.115--125. Boston, Massachusetts, June 16-19, 2001.

https://www.cs.bham.ac.uk/~mhe/papers/lics2001-revised.pdf
https://www.cs.bham.ac.uk/~mhe/papers/interval.pdf
https://www.cs.bham.ac.uk/~mhe/.talks/map2011/


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

open import UF.FunExt

module TWA.Escardo-Simpson-LICS2001 (fe : FunExt) where

open import MLTT.Spartan
open import Naturals.Addition renaming (_+_ to _+ℕ_)
open import Naturals.Sequence fe
open import UF.Sets
open import UF.Subsingletons public


First we give basic properties on binary functions,
as well as a specific property about equality of streams under some arithmetic.


idempotent transpositional : {X : 𝓤 ̇ }  (X  X  X)  𝓤 ̇
idempotent       _∙_ =  a        a  a              a
transpositional  _∙_ =  a b c d  (a  b)  (c  d)  (a  c)  (b  d)

seq-add-push : {A : 𝓤 ̇ } (α :   A) (n : )
               (i : )  α (succ i +ℕ n))   (i : )  α (succ (i +ℕ n)))
seq-add-push α 0 = refl
seq-add-push α (succ n) = seq-add-push (α  succ) n


The initial structure we define is a Midpoint-algebra.


midpoint-algebra-axioms : (A : 𝓤 ̇ )  (A  A  A)  𝓤 ̇
midpoint-algebra-axioms {𝓤} A _⊕_ = is-set A
                                  × idempotent _⊕_ × commutative _⊕_ × transpositional _⊕_

Midpoint-algebra : (𝓤 : Universe)  𝓤  ̇
Midpoint-algebra 𝓤 = Σ A  𝓤 ̇ , Σ _⊕_  (A  A  A) , (midpoint-algebra-axioms A _⊕_)


 We introduce two more properties on binary functions: cancellation and iteration.
 For a particular type, the iterator is unique.


cancellative : {X : 𝓤 ̇ }  (X  X  X)  𝓤 ̇
cancellative  _∙_ =  a b c  a  c  b  c  a  b

iterative : {A : 𝓤 ̇ }  (A  A  A)  𝓤 ̇
iterative {𝓤} {A} _⊕_ = Σ M  ((  A)  A) , ((a :   A)  M a  a 0  M (tail a))
                                            × ((a x :   A)
                                                ((i : )  a i  x i  a (succ i))
                                                a 0  M x)

iterative-uniqueness· : {A : 𝓤 ̇ }  (_⊕_ : A  A  A)
                       (F M : iterative _⊕_)
                       pr₁ F  pr₁ M
iterative-uniqueness· {𝓤} {𝕀} _⊕_ (F , p₁ , q₁) (M , p₂ , q₂) x = q₂ M' x γ
  where M' :   𝕀
        M' i = F  n  x (n +ℕ i))
        γ : (i : )  M' i  (x i  M' (succ i))
        γ i = p₁  n  x (n +ℕ i))
             ap  -  x -  F  n  x (succ n +ℕ i))) (zero-left-neutral i)
             ap  -  x i  F -) (seq-add-push x i)

iterative-uniqueness : {A : 𝓤 ̇ }  (_⊕_ : A  A  A)
                      (F M : iterative _⊕_)
                      pr₁ F  pr₁ M
iterative-uniqueness {𝓤} _⊕_ F M = dfunext (fe 𝓤 𝓤) (iterative-uniqueness· _⊕_ F M)


 A Convex-body is a cancellative, iterative Midpoint-algebra.


convex-body-axioms : (A : 𝓤 ̇ )  (A  A  A)  𝓤 ̇
convex-body-axioms {𝓤} A _⊕_ = (midpoint-algebra-axioms A _⊕_)
                             × (cancellative _⊕_)
                             × (iterative _⊕_)

Convex-body : (𝓤 : Universe)  𝓤  ̇
Convex-body 𝓤 = Σ A  𝓤 ̇ , Σ _⊕_  (A  A  A) , (convex-body-axioms A _⊕_)

⟨_⟩ : Convex-body 𝓤  𝓤 ̇
 A , _  = A

midpoint-operation : (𝓐 : Convex-body 𝓤)   𝓐    𝓐    𝓐 
midpoint-operation (A , _⊕_ , _) = _⊕_

syntax midpoint-operation 𝓐 x y = x ⊕⟨ 𝓐  y


 Definition of a midpoint-homomorphism.
 The identity function is a midpoint-hom.
 The unary functions given by a constant midpoint are midpoint-homs.
 The composition of two midpoint-homs is a midpoint-hom.


is-⊕-homomorphism : (𝓐 : Convex-body 𝓤) (𝓑 : Convex-body 𝓥)
                 ( 𝓐    𝓑 )  𝓤  𝓥 ̇
is-⊕-homomorphism 𝓐 𝓑 h = (x y :  𝓐 )  h (x ⊕⟨ 𝓐  y)  h x ⊕⟨ 𝓑  h y

id-is-⊕-homomorphism : (𝓐 : Convex-body 𝓤)  is-⊕-homomorphism 𝓐 𝓐 id
id-is-⊕-homomorphism 𝓐 x y = refl

⊕-is-⊕-homomorphism-r : (𝓐 : Convex-body 𝓤)
                     (a :  𝓐 )  is-⊕-homomorphism 𝓐 𝓐  y  a ⊕⟨ 𝓐  y)
⊕-is-⊕-homomorphism-r (𝓐 , _⊕_ , (_ , ⊕-idem , _ , ⊕-tran) , _) a x y
 =    a     (x  y) =⟨ ap (_⊕ (x  y)) (⊕-idem a ⁻¹) 
   (a  a)  (x  y) =⟨ ⊕-tran a a x y 
   (a  x)  (a  y) 

⊕-is-⊕-homomorphism-l : (𝓐 : Convex-body 𝓤)
                     (b :  𝓐 )  is-⊕-homomorphism 𝓐 𝓐  x  x ⊕⟨ 𝓐  b)
⊕-is-⊕-homomorphism-l (𝓐 , _⊕_ , (_ , ⊕-idem , _ , ⊕-tran) , _) b x y
 = (x  y)     b    =⟨ ap ((x  y) ⊕_) (⊕-idem b ⁻¹) 
   (x  y)  (b  b) =⟨ ⊕-tran x y b b 
   (x  b)  (y  b) 

⊕-hom-composition : (𝓐 : Convex-body 𝓤) (𝓑 : Convex-body 𝓥) (𝓒 : Convex-body 𝓦)
                           (h₁ :  𝓐    𝓑 )  (h₂ :  𝓑    𝓒 )
                           is-⊕-homomorphism 𝓐 𝓑 h₁  is-⊕-homomorphism 𝓑 𝓒 h₂
                           is-⊕-homomorphism 𝓐 𝓒 (h₂  h₁)
⊕-hom-composition {𝓤} {𝓥} {𝓦} 𝓐 𝓑 𝓒 h₁ h₂ i₁ i₂ x y
 = (h₂  h₁) (x ⊕⟨ 𝓐  y)                       =⟨ ap h₂ (i₁ x y) 
         h₂  ((h₁ x) ⊕⟨ 𝓑  (h₁ y))             =⟨ i₂ (h₁ x) (h₁ y) 
             ((h₂  h₁) x) ⊕⟨ 𝓒  ((h₂  h₁) y) 


 The key structure of the axiomatisation: an interval object.
 An interval object is defined by a Convex-body 𝓘 and two points u,v : ⟨𝓘⟩.
 For every two points a,b : ⟨𝓐⟩ of a Convex-body 𝓐,
   there exists a unique h : ⟨𝓘⟩ → ⟨𝓐⟩ such that:
    * h u = a,
    * h v = b,
    * ∀ x,y : ⟨𝓘⟩. h (x ⊕⟨ 𝓘 ⟩ y) = h x ⊕⟨ 𝓐 ⟩ h y).


is-interval-object : (𝓘 : Convex-body 𝓤) (𝓥 : Universe)   𝓘    𝓘   𝓤  𝓥  ̇
is-interval-object 𝓘 𝓥 u v =
    (𝓐 : Convex-body 𝓥) (a b :  𝓐 ) -- h = affine a b
    ∃! h  ( 𝓘    𝓐 ) , (h u  a)
                            × (h v  b)
                            × ((x y :  𝓘 )  h (x ⊕⟨ 𝓘  y)  h x ⊕⟨ 𝓐  h y)


 The type of an interval object axiomatisation as a record.


record Interval-object (𝓤 : Universe) : 𝓤ω where
 field
  𝕀 : 𝓤 ̇
  _⊕_ : 𝕀  𝕀  𝕀
  u v : 𝕀
  mpaa : midpoint-algebra-axioms 𝕀 _⊕_
  ca : cancellative _⊕_
  ia : iterative _⊕_
  universal-property : is-interval-object (𝕀 , _⊕_ , mpaa , ca , ia) 𝓤 u v


 The type of a doubling function axiomatisation.


has-double : (𝓥 : Universe) (io : Interval-object 𝓥)  𝓥 ̇
has-double 𝓥 io = Σ double  (𝕀  𝕀)
                 , ((x : 𝕀)  double (x  (u  v))  x)
                 × ((x : 𝕀)  double (u  (u  x))  u)
                 × ((x : 𝕀)  double (v  (v  x))  v)
 where
   𝕀 = Interval-object.𝕀 io
   u = Interval-object.u io
   v = Interval-object.v io
   _⊕_ = Interval-object._⊕_ io


 We now prove things within a universe
 with an Interval-object and a doubling function.


module basic-interval-object-development {𝓤 : Universe}
 (io : Interval-object 𝓤) (hd : has-double 𝓤 io) where


 First we unpack all of the axioms from the Interval-object
 affine : 𝕀 → 𝕀 → 𝕀 → 𝕀 is given by the unique map h : 𝕀 → 𝕀.



 open Interval-object io public

 ⊕-idem : idempotent _⊕_
 ⊕-idem = pr₁ (pr₂ mpaa)

 ⊕-comm : commutative _⊕_
 ⊕-comm = pr₁ (pr₂ (pr₂ mpaa))

 ⊕-tran : transpositional _⊕_
 ⊕-tran = pr₂ (pr₂ (pr₂ mpaa))

 ⊕-canc : cancellative _⊕_
 ⊕-canc = Interval-object.ca io

 𝓘 : Convex-body 𝓤
 𝓘 = 𝕀 , _⊕_ , mpaa , ⊕-canc , ia

 affine : 𝕀  𝕀  𝕀  𝕀
 affine x y = ∃!-witness (universal-property 𝓘 x y)

 affine-equation-l : (x y : 𝕀)  affine x y u  x
 affine-equation-l x y = pr₁ (∃!-is-witness (universal-property 𝓘 x y))

 affine-equation-r : (x y : 𝕀)  affine x y v  y
 affine-equation-r x y = pr₁ (pr₂ (∃!-is-witness (universal-property 𝓘 x y)))

 affine-is-⊕-homomorphism : (x y : 𝕀) (a b : 𝕀)
                         affine x y (a  b)  affine x y a  affine x y b
 affine-is-⊕-homomorphism x y = pr₂ (pr₂ (∃!-is-witness (universal-property 𝓘 x y)))

 affine-uniqueness : (f : 𝕀  𝕀) (a b : 𝕀)
                    f u  a
                    f v  b
                    is-⊕-homomorphism 𝓘 𝓘 f
                    affine a b  f
 affine-uniqueness f a b l r i
  = ap pr₁ (∃!-uniqueness' (universal-property 𝓘 a b) (f , l , r , i))

 affine-uniqueness· : (f : 𝕀  𝕀) (a b : 𝕀)
                    f u  a
                    f v  b
                    is-⊕-homomorphism 𝓘 𝓘 f
                    affine a b  f
 affine-uniqueness· f a b l r i x = ap  -  - x) (affine-uniqueness f a b l r i)


 Many of the following proofs follow from the uniqueness of affine.
 For example, affine u v is point-wise equivalent to the identity function.


 affine-uv-involutive : affine u v  id
 affine-uv-involutive = affine-uniqueness· id u v refl refl (id-is-⊕-homomorphism 𝓘)

 affine-constant : (a : 𝕀) (x : 𝕀)  affine a a x  a
 affine-constant a = affine-uniqueness·  _  a) a a refl refl  _ _  ⊕-idem a ⁻¹)


 The iterator is called M.
 We prove that it is idempotent, symmetric and is a midpoint-hom.


 M : (  𝕀)  𝕀
 M = pr₁ ia

 M-prop₁ : (a :   𝕀)  M a  a 0  (M (a  succ))
 M-prop₁ = pr₁ (pr₂ ia)

 M-prop₂ : (a x :   𝕀)  ((i : )  a i  x i  a (succ i))  a 0  M x
 M-prop₂ = pr₂ (pr₂ ia)

 M-idem : (x : 𝕀)  M  _  x)  x
 M-idem x = M-prop₂  _  x)  _  x)  _  ⊕-idem x ⁻¹) ⁻¹

 M-hom : (x y :   𝕀)  (M x  M y)  M  i  x i  y i)
 M-hom x y = M-prop₂ M'  i  x i  y i) γ where
   M' :   𝕀
   M' i = M  n  x (n +ℕ i))  M  n  y (n +ℕ i))
   γ : (i : )  M' i  ((x i  y i)  M' (succ i))
   γ i = M  n  x (n +ℕ i))  M  n  y (n +ℕ i))
             =⟨ ap (_⊕ M  n  y (n +ℕ i)))
                   (M-prop₁  n  x (n +ℕ i))) 
         (x (0 +ℕ i)  M  n  x (succ n +ℕ i)))  M  n  y (n +ℕ i))
             =⟨ ap ((x (0 +ℕ i)  M  n  x (succ n +ℕ i))) ⊕_)
                   (M-prop₁  n  y (n +ℕ i))) 
         (x (0 +ℕ i)  M  n  x (succ n +ℕ i)))  (y (0 +ℕ i)  M  n  y (succ n +ℕ i)))
             =⟨ ⊕-tran
                   (x (0 +ℕ i)) (M  n  x (succ n +ℕ i)))
                   (y (0 +ℕ i)) (M  n  y (succ n +ℕ i))) 
         ((x (0 +ℕ i)  y (0 +ℕ i))  (M  n  x (succ n +ℕ i))  M  n  y (succ n +ℕ i))))
             =⟨ ap  -  (x -  y -)
                         (M  n  x (succ n +ℕ i))  M  n  y (succ n +ℕ i))))
                   (zero-left-neutral i) 
         ((x i  y i)  (M  n  x (succ n +ℕ i))  M  n  y (succ n +ℕ i))))
             =⟨ ap  -  (x i  y i)  (M -  M  n  y (succ n +ℕ i))))
                   (seq-add-push x i) 
         ((x i  y i)  (M  n  x (succ (n +ℕ i)))  M  n  y (succ n +ℕ i))))
             =⟨ ap  -  (x i  y i)  (M  n  x (succ (n +ℕ i)))  M -))
                   (seq-add-push y i) 
         (x i  y i)  M' (succ i) 

 M-prop₁-inner : (x :     𝕀)  M  i  M  j  x i j))
                                  M  i  x i 0  M  j  x i (succ j)))
 M-prop₁-inner x = ap M (dfunext (fe 𝓤₀ 𝓤)  i  M-prop₁ (x i)))

 M-symm : (x :     𝕀)  M  i  M  j  x i j))  M  i  (M λ j  x j i))
 M-symm x = M-prop₂ M'  i  M  j  x j i)) γ where
   M' :   𝕀
   M' n = M  i  M  j  x i (j +ℕ n)))
   γ : (i : )  M' i  (pr₁ ia  j  x j i)  M' (succ i))
   γ n = M  i  M  j  x i (j +ℕ n)))
             =⟨ M-prop₁-inner  i j  x i (j +ℕ n)) 
         M  i  x i (0 +ℕ n)  M  j  x i (succ j +ℕ n)))
             =⟨ M-hom  i  x i (0 +ℕ n))
                       i  M  j  x i (succ j +ℕ n))) ⁻¹ 
         M  i  x i (0 +ℕ n))  M  i  M  j  x i (succ j +ℕ n)))
             =⟨ ap  -  M  i  x i -)
                     M  i  M  j  x i (succ j +ℕ n))))
                   (zero-left-neutral n) 
         M  i  x i n)  M  i  M  j  x i (succ j +ℕ n)))
             =⟨ ap (M  j  x j n) ⊕_) (seq-seq-add-push x n) 
         M  j  x j n)  M' (succ n) 
     where
       seq-seq-add-push : (x :     𝕀) (n : )
                         M  i  M  j  x i (succ j +ℕ n)))
                         M  i  M  j  x i (succ (j +ℕ n))))
       seq-seq-add-push x 0 = refl
       seq-seq-add-push x (succ n) = seq-seq-add-push  i j  x i (succ j)) n


 Any midpoint-hom is automatically an M-hom.
 Thus, M is an M-hom.


 ⊕-homs-are-M-homs : (h : 𝕀  𝕀)  is-⊕-homomorphism 𝓘 𝓘 h
            (z :   𝕀)  h (M z)  M  n  h (z n))
 ⊕-homs-are-M-homs h hom z = M-prop₂ M'  n  h (z n)) γ where
   M' :   𝕀
   M' i = h (M  n  z (n +ℕ i)))
   γ : (i : )  M' i  (h (z i)  M' (succ i))
   γ i = h (M  n  z (n +ℕ i)))
            =⟨ ap h (M-prop₁  n  z (n +ℕ i))) 
         h (z (0 +ℕ i)  M  n  z (succ n +ℕ i)))
            =⟨ hom (z (0 +ℕ i)) (M  n  z (succ n +ℕ i))) 
         h (z (0 +ℕ i))  h (M  n  z (succ n +ℕ i)))
            =⟨ ap  -  h (z -)  h (M  n  z (succ n +ℕ i))))
                  (zero-left-neutral i) 
         h (z i)  h (M  n  z (succ n +ℕ i)))
            =⟨ ap  -  h (z i)  h (M -))
                  (seq-add-push z i) 
         h (z i)  M' (succ i)
            

 affine-M-hom : (x y : 𝕀) (z :   𝕀)  affine x y (M z)  M  n  affine x y (z n))
 affine-M-hom x y z = ⊕-homs-are-M-homs (affine x y) (affine-is-⊕-homomorphism x y) z


 We adopt the convention u = −1 and v = +1 for the following.


 −1 O +1 : 𝕀
 −1 = u
 +1 = v
 O  = −1  +1


 The negation function and related properties,
 culminating in proving negation is involutive.


 −_ : 𝕀  𝕀
 −_ = affine +1 −1

 infixl 100 −_

 −-is-⊕-homomorphism : (a b : 𝕀)   (a  b)   a   b
 −-is-⊕-homomorphism = affine-is-⊕-homomorphism +1 −1

 −1-inverse :  −1  +1
 −1-inverse = affine-equation-l +1 −1

 +1-inverse :  +1  −1
 +1-inverse = affine-equation-r +1 −1

 O-inverse :  O  O
 O-inverse =     O      =⟨ −-is-⊕-homomorphism −1 +1 
              −1   +1 =⟨ ap (_⊕  +1) −1-inverse 
               +1   +1 =⟨ ap (+1 ⊕_)   +1-inverse 
               +1  −1   =⟨ ⊕-comm +1 −1 
                  O      

 −1-neg-inv :   −1  −1
 −1-neg-inv =   −1 =⟨ ap −_ −1-inverse 
                 +1 =⟨ +1-inverse 
                  −1 

 +1-neg-inv :   +1  +1
 +1-neg-inv =   +1 =⟨ ap −_ +1-inverse 
                 −1 =⟨ −1-inverse 
                  +1 

 −-involutive : (x : 𝕀)    x  x
 −-involutive x =           x =⟨ negation-involutive' x ⁻¹ 
                 affine −1 +1 x =⟨ affine-uv-involutive x 
                              x  
  where
   −−-is-⊕-homomorphism : is-⊕-homomorphism 𝓘 𝓘  x   ( x))
   −−-is-⊕-homomorphism = ⊕-hom-composition 𝓘 𝓘 𝓘 −_ −_
                          −-is-⊕-homomorphism −-is-⊕-homomorphism
   negation-involutive' : (x : 𝕀)  affine −1 +1 x   ( x)
   negation-involutive' = affine-uniqueness·  x   ( x))
                          −1 +1 −1-neg-inv +1-neg-inv
                          −−-is-⊕-homomorphism

 fact : (x y : 𝕀)  x   y   ( x  y)
 fact x y =     x   y =⟨ ap (_⊕ ( y)) (−-involutive x ⁻¹) 
              x   y =⟨ −-is-⊕-homomorphism ( x) y ⁻¹ 
             ( x  y) 


 The "midpoint subtraction" function from midpoint and negation.
 The midpoint subtraction of any x with itself is O.


 _⊖_ : 𝕀  𝕀  𝕀
 x  y = x  ( y)

 ⊖-zero : (x : 𝕀)  x  x  O
 ⊖-zero x = x  x        =⟨ ⊖-fact' ⁻¹ 
            affine O O x =⟨ affine-constant O x 
            O            
   where
    ⊖-fact' : affine O O x  x  x
    ⊖-fact' = affine-uniqueness·  x  x  x) O O
              (ap (−1 ⊕_) −1-inverse)
              (ap (+1 ⊕_) +1-inverse  ⊕-comm +1 −1)
               x y  ap ((x  y) ⊕_)
                          (−-is-⊕-homomorphism x y)
                      ⊕-tran x y ( x) ( y))
              x


 The multiplication function and related properties,
 culminating in proving multiplication is
 commutative and associative'.


 _*_ : 𝕀  𝕀  𝕀
 x * y = affine ( x) x y

 infixl 99 _*_

 *-gives-negation-l : (x : 𝕀)  x * −1   x
 *-gives-negation-l x = affine-equation-l ( x) x

 *-gives-negation-r : (y : 𝕀)  −1 * y   y
 *-gives-negation-r y = ap  -  affine - −1 y) −1-inverse

 *-gives-id-l : (x : 𝕀)  x * +1  x
 *-gives-id-l x = affine-equation-r ( x) x

 *-gives-id-r : (y : 𝕀)  +1 * y  y
 *-gives-id-r y = ap  -  affine - +1 y) +1-inverse  affine-uv-involutive y

 *-is-⊕-homomorphism-l : (a : 𝕀)  is-⊕-homomorphism 𝓘 𝓘 (a *_)
 *-is-⊕-homomorphism-l a x y = affine-is-⊕-homomorphism ( a) a x y

 *-commutative : commutative _*_
 *-commutative x y = γ y
  where
   j : (a b : 𝕀)  is-⊕-homomorphism 𝓘 𝓘  x  a * x  b * x)
   j a b x y
       = ap (_⊕ b * (x  y)) (*-is-⊕-homomorphism-l a x y)
        ap ((a * x  a * y) ⊕_) (*-is-⊕-homomorphism-l b x y)
        ⊕-tran (a * x) (a * y) (affine ( b) b x) (affine ( b) b y)
   i : is-⊕-homomorphism 𝓘 𝓘  y  y * x)
   i y z = p
    where
     p : (y  z) * x  (y * x  z * x)
     p = affine-uniqueness·  x  y * x  z * x) ( (y  z)) (y  z)
         (ap (_⊕ z * u) (*-gives-negation-l y)
          ap (( y) ⊕_) (*-gives-negation-l z)
          affine-is-⊕-homomorphism +1 −1 y z ⁻¹)
         (ap (_⊕ z * v) (*-gives-id-l y)
          ap (y ⊕_) (*-gives-id-l z))
         (j y z) x
   γ :  y  x * y)   y  y * x)
   γ = affine-uniqueness·  y  y * x)
       ( x) x (*-gives-negation-r x) (*-gives-id-r x)
       i

 *-is-⊕-homomorphism-r : (b : 𝕀)  is-⊕-homomorphism 𝓘 𝓘 (_* b)
 *-is-⊕-homomorphism-r b x y =
      (x  y) * b       =⟨ *-commutative (x  y) b 
      b * (x  y)       =⟨ *-is-⊕-homomorphism-l b x y 
      (b * x)  (b * y) =⟨ ap ((b * x) ⊕_) (*-commutative b y) 
      (b * x)  (y * b) =⟨ ap (_⊕ (y * b)) (*-commutative b x) 
      (x * b)  (y * b) 

 *-prop : (x y : 𝕀)  x * y   (x *  y)
 *-prop x y = affine-uniqueness·  -   (x * ( -))) ( x) x l r i y
  where
   l =  (x * ( −1)) =⟨ ap  -   (x * -)) −1-inverse 
        (x *    +1 ) =⟨ ap −_ (*-gives-id-l x) 
         x           
   r =  (x * ( +1)) =⟨ ap  -   (x * -)) +1-inverse 
        (x *    −1 ) =⟨ ap −_ (*-gives-negation-l x) 
         ( x)       =⟨ −-involutive x 
             x        
   i : is-⊕-homomorphism 𝓘 𝓘  -   (x * ( -)))
   i a b =   (x * ( (a  b)))
                =⟨ ap  -   (x * -)) (−-is-⊕-homomorphism a b) 
             (x * ( a   b))
                =⟨ ap −_ (affine-is-⊕-homomorphism ( x) x ( a) ( b)) 
            ((x *  a)  (x *  b))
                =⟨ affine-is-⊕-homomorphism +1 −1 (x * ( a)) (x * ( b)) 
            (x *  a)   (x *  b) 

 *-assoc : (x y z : 𝕀)  x * (y * z)  (x * y) * z
 *-assoc x y z = γ z ⁻¹
  where
   l =      x * (y * −1) =⟨ ap (x *_) (*-gives-negation-l y) 
            x *  ( y)   =⟨ −-involutive (x * ( y)) ⁻¹ 
     ( ( (x *  y)))   =⟨ ap −_ (*-prop x y ⁻¹) 
          (x * y)       
   r = x * (y * +1) =⟨ ap (x *_) (*-gives-id-l y) 
       x * y        
   i : is-⊕-homomorphism 𝓘 𝓘  z  x * (y * z))
   i a b = x * (y * (a  b))
                =⟨ ap (x *_) (*-is-⊕-homomorphism-l y a b) 
           x * (y * a  y * b)
                =⟨ affine-is-⊕-homomorphism ( x) x (y * a) (y * b) 
           x * (y * a)  x * (y * b) 
   γ :  z  (x * y) * z)   z  x * (y * z))
   γ = affine-uniqueness·  z  x * (y * z)) ( (x * y)) (x * y) l r i


 Power series can be implemented from multiplication.
 We also define a halving function from the midpoint.


 _**_ : 𝕀    𝕀
 a ** 0      = +1
 a ** succ n = a * (a ** n)

 powerseries : (  𝕀)  (𝕀  𝕀)
 powerseries a = λ x  M  n  (a n) * (x ** n))

 _/2 : 𝕀  𝕀
 _/2 = _⊕ O
 +1/2 = +1 /2
 −1/2 = −1 /2

 infixl 99 _/2

 −-half : (x : 𝕀)   (x /2)   x /2
 −-half x = −-is-⊕-homomorphism x O  ap ( x ⊕_) O-inverse

 O-half : O /2  O
 O-half = ⊕-idem O

 −1-half :  +1/2  −1/2
 −1-half = −-half +1  ap _/2 +1-inverse

 +1-half :  −1/2  +1/2
 +1-half = −-half −1  ap _/2 −1-inverse

 half-is-⊕-homomorphism : is-⊕-homomorphism 𝓘 𝓘 _/2
 half-is-⊕-homomorphism = ⊕-is-⊕-homomorphism-l 𝓘 O

 half-same : (x : 𝕀)  +1/2 * x  x /2
 half-same x = ap  -  affine - +1/2 x) −1-half
              affine-uniqueness· _/2 −1/2 +1/2
               refl refl half-is-⊕-homomorphism x


 Now we assume that we have a doubling function.
 This allows the definition
 of truncated addition and subtraction.


 double : 𝕀  𝕀
 double = pr₁ hd

 double-mid : (x : 𝕀)  double (x /2)  x
 double-mid = pr₁ (pr₂ hd)

 double-left : (x : 𝕀)  double (−1  (−1  x))  −1
 double-left = pr₁ (pr₂ (pr₂ hd))

 double-right : (x : 𝕀)  double (+1  (+1  x))  +1
 double-right = pr₂ (pr₂ (pr₂ hd))

 _+𝕀_ _−𝕀_ : 𝕀  𝕀  𝕀
 x +𝕀 y = double (x  y)
 x −𝕀 y = double (x  y)

 +𝕀-comm : commutative _+𝕀_
 +𝕀-comm x y = ap double (⊕-comm x y)

 +𝕀-itself : (x : 𝕀)  x +𝕀 x  double x
 +𝕀-itself x = ap double (⊕-idem x)

 +𝕀-tran : (x y s t : 𝕀)  (x  y) +𝕀 (s  t)  (x  s) +𝕀 (y  t)
 +𝕀-tran x y s t = ap double (⊕-tran x y s t)

 +𝕀-fact : (x y : 𝕀)  x +𝕀  y  double ( (y  x))
 +𝕀-fact x y = ap double (fact x y  ap −_ (⊕-comm ( x) y))


 Double and half allows it to define a max operation.
 First, there is an operation for maxO,
 this is then used to define max itself.

 We wish to prove that max is a semi-lattice
 (idempotent, commutative and associative').


 maxO : 𝕀  𝕀
 maxO x = double (−1/2 +𝕀 x) /2 +𝕀 +1/2

 O-midpoint-of-halves : −1/2  +1/2  O
 O-midpoint-of-halves = −1/2  +1/2     =⟨ ap (−1/2 ⊕_) (+1-half ⁻¹) 
                        −1/2  ( −1/2) =⟨ ⊖-zero −1/2 
                        O 

 double-O-is-O : double O  O
 double-O-is-O = double O       =⟨ ap double (⊕-idem O ⁻¹) 
                 double (O  O) =⟨ double-mid O 
                 O 

 double-−1/2-is-−1 : double −1/2  −1
 double-−1/2-is-−1 = double-mid −1

 double-+1/2-is-+1 : double +1/2  +1
 double-+1/2-is-+1 = double-mid +1

 double-−1-is-−1 : double −1  −1
 double-−1-is-−1 = ap double (⊕-idem −1 ⁻¹  ap (−1 ⊕_) (⊕-idem −1 ⁻¹))  double-left −1

 double-+1-is-+1 : double +1  +1
 double-+1-is-+1 = ap double (⊕-idem +1 ⁻¹  ap (+1 ⊕_) (⊕-idem +1 ⁻¹))  double-right +1

 maxO-O-is-O : maxO O  O
 maxO-O-is-O = maxO O
                 =⟨ ap  -  (double - /2) +𝕀 +1/2) (double-mid −1/2) 
               (double −1/2 /2) +𝕀 +1/2
                 =⟨ ap  -  - /2 +𝕀 +1/2) (double-left +1) 
               −1/2 +𝕀 +1/2
                 =⟨ ap double O-midpoint-of-halves  double-O-is-O 
               O 

 max _∨_ : 𝕀  𝕀  𝕀
 max x y = double (x /2 +𝕀 maxO (y  x))
 _∨_ = max

 max-idem : idempotent _∨_
 max-idem a = a  a
                =⟨ ap  -  double ((a /2) +𝕀 maxO -))
                      (⊖-zero a) 
              double (double (a /2  maxO O))
                =⟨ ap  -  double ((a /2) +𝕀 -))
                      maxO-O-is-O 
              double (a /2 +𝕀 O)
                =⟨ ap double (double-mid (a /2)) 
              double (a /2)
                =⟨ double-mid a 
              a 

 -- max-comm : commutative _∨_
 -- max-comm x y = {!!}

 -- max-assoc : associative' _∨_
 -- max-assoc = {!!}



 Other functions can be derived from max.


 min : 𝕀  𝕀  𝕀
 min x y =  (max ( x) ( y))

 abs : 𝕀  𝕀
 abs x = max ( x) x



 TODO list:
  * max (_∨_) is a semilattice -- assoc, comm (done idem)
    - derive order from this semilattice.

  * Page 42. - Prove the limit *is* the limit, as above.

  * Binary expansions
           (ℕ      →      ℕ          →           𝕀)
           numerator     denominator   numer/denom
           (binary expansion stream applied to M).