Andrew Sneap, November 2021 \begin{code} {-# OPTIONS --safe --without-K #-} open import MLTT.Spartan renaming (_+_ to _β_) open import Naturals.Properties open import Notation.Order open import UF.Base open import UF.Subsingletons open import Integers.Addition renaming (_+_ to _β€+_) open import Integers.Type open import Integers.Multiplication renaming (_*_ to _β€*_) open import Integers.Order open import Naturals.Multiplication renaming (_*_ to _β*_) open import Rationals.Fractions open import Rationals.FractionsOperations module Rationals.FractionsOrder where _π½β€_ _π½β₯_ : π½ β π½ β π€β Μ (x , a) π½β€ (y , b) = x β€* pos (succ b) β€ y β€* pos (succ a) p π½β₯ q = q π½β€ p π½β€-is-prop : (p q : π½) β is-prop (p π½β€ q) π½β€-is-prop (x , a) (y , b) = β€β€-is-prop (x β€* pos (succ b)) (y β€* pos (succ a)) _π½<_ _π½>_ : π½ β π½ β π€β Μ (x , a) π½< (y , b) = x β€* pos (succ b) < y β€* pos (succ a) p π½> q = q π½< p π½<-coarser-than-β€ : (p q : π½) β p π½< q β p π½β€ q π½<-coarser-than-β€ (x , a) (y , b) l = Ξ³ where Ξ³ : (x , a) π½β€ (y , b) Ξ³ = <-is-β€ (x β€* pos (succ b)) (y β€* pos (succ a)) l π½<-is-prop : (p q : π½) β is-prop (p π½< q) π½<-is-prop (x , a) (y , b) = β€<-is-prop (x β€* pos (succ b)) (y β€* pos (succ a)) π½<-trans : (p q r : π½) β p π½< q β q π½< r β p π½< r π½<-trans (x , a) (y , b) (z , c) Ξ± Ξ² = Ξ³ where a' = pos (succ a) b' = pos (succ b) c' = pos (succ c) I : x β€* c' β€* b' < z β€* a' β€* b' I = β€<-trans (x β€* c' β€* b') (y β€* a' β€* c') (z β€* a' β€* b') i ii where i : x β€* c' β€* b' < y β€* a' β€* c' i = transport (_< y β€* a' β€* c') Ο ΞΈ where Ο : x β€* b' β€* c' οΌ x β€* c' β€* b' Ο = β€-mult-rearrangement x b' c' ΞΈ : x β€* b' β€* c' < y β€* a' β€* c' ΞΈ = positive-multiplication-preserves-order (x β€* b') (y β€* a') c' β Ξ± ii : y β€* a' β€* c' < z β€* a' β€* b' ii = transportβ _<_ Ξ³β Ξ³β Ξ³β where Ξ³β : y β€* c' β€* a' οΌ y β€* a' β€* c' Ξ³β = β€-mult-rearrangement y c' a' Ξ³β : z β€* b' β€* a' οΌ z β€* a' β€* b' Ξ³β = β€-mult-rearrangement z b' a' Ξ³β : y β€* c' β€* a' < z β€* b' β€* a' Ξ³β = positive-multiplication-preserves-order (y β€* c') (z β€* b') a' β Ξ² Ξ³ : x β€* c' < z β€* a' Ξ³ = ordering-right-cancellable (x β€* c') (z β€* a') b' β I π½<-addition-preserves-order : (p q r : π½) β p π½< q β p + r π½< q + r π½<-addition-preserves-order (x , a) (y , b) (z , c) (n , e) = pred (succ c β* succ c β* succ n) , III where a' = pos (succ a) b' = pos (succ b) c' = pos (succ c) n' = pos (succ n) sa = succ a sb = succ b sn = succ n sc = succ c I : Β¬ (sc β* sc β* sn οΌ 0) I Ξ± = positive-not-zero n (mult-left-cancellable sn 0 c ii) where i : sc β* (sc β* sn) οΌ sc β* (sc β* 0) i = sc β* (sc β* sn) οΌβ¨ mult-associativity sc sc sn β»ΒΉ β© sc β* sc β* sn οΌβ¨ Ξ± β© 0 οΌβ¨ zero-right-base sc β»ΒΉ β© sc β* 0 οΌβ¨ ap (sc β*_) (zero-right-base sc β»ΒΉ) β© sc β* (sc β* 0) β ii : sc β* sn οΌ sc β* 0 ii = mult-left-cancellable (sc β* sn) (sc β* 0) c i II : succβ€ (pos (pred (sc β* sc β* sn))) οΌ c' β€* c' β€* n' II = succβ€ (pos (pred (sc β* sc β* sn))) οΌβ¨ refl β© pos (succ (pred (sc β* sc β* sn))) οΌβ¨ i β© pos (sc β* sc β* sn) οΌβ¨ ii β© pos (sc β* sc) β€* pos sn οΌβ¨ iii β© pos (sc) β€* pos (sc) β€* pos sn οΌβ¨ refl β© c' β€* c' β€* n' β where i = ap pos (succ-pred' (sc β* sc β* sn) I) ii = pos-multiplication-equiv-to-β (sc β* sc) sn β»ΒΉ iii = ap (_β€* pos sn) (pos-multiplication-equiv-to-β sc sc β»ΒΉ) III : succβ€ ((x β€* c' β€+ z β€* a') β€* pos (succ (pred (sb β* sc)))) β€+ pos (pred (sc β* sc β* sn)) οΌ (y β€* c' β€+ z β€* b') β€* pos (succ (pred (sa β* sc))) III = succβ€ ((x β€* c' β€+ z β€* a') β€* pos (succ (pred (sb β* sc)))) β€+ pos (pred (sc β* sc β* sn)) οΌβ¨ i β© succβ€ ((x β€* c' β€+ z β€* a') β€* pos (succ (pred (sb β* sc))) β€+ pos (pred (sc β* sc β* sn))) οΌβ¨ ii β© (x β€* c' β€+ z β€* a') β€* pos (succ (pred (sb β* sc))) β€+ succβ€ (pos (pred (sc β* sc β* sn))) οΌβ¨ iii β© (x β€* c' β€+ z β€* a') β€* pos (succ (pred (sb β* sc))) β€+ c' β€* c' β€* n' οΌβ¨ iv β© (x β€* c' β€+ z β€* a') β€* (b' β€* c') β€+ c' β€* c' β€* n' οΌβ¨ v β© x β€* c' β€* (b' β€* c') β€+ z β€* a' β€* (b' β€* c') β€+ c' β€* c' β€* n' οΌβ¨ vi β© x β€* c' β€* (b' β€* c') β€+ (z β€* a' β€* (b' β€* c') β€+ c' β€* c' β€* n') οΌβ¨ vii β© x β€* c' β€* (b' β€* c') β€+ (c' β€* c' β€* n' β€+ z β€* a' β€* (b' β€* c')) οΌβ¨ viii β© x β€* c' β€* (b' β€* c') β€+ c' β€* c' β€* n' β€+ z β€* a' β€* (b' β€* c') οΌβ¨ ix β© x β€* (b' β€* c') β€* c' β€+ n' β€* (c' β€* c') β€+ z β€* a' β€* (b' β€* c') οΌβ¨ xi β© x β€* b' β€* c' β€* c' β€+ n' β€* (c' β€* c') β€+ z β€* a' β€* (b' β€* c') οΌβ¨ xii β© x β€* b' β€* (c' β€* c') β€+ n' β€* (c' β€* c') β€+ z β€* a' β€* (b' β€* c') οΌβ¨ xiii β© (x β€* b' β€+ n') β€* (c' β€* c') β€+ z β€* a' β€* (b' β€* c') οΌβ¨ xiv β© (x β€* b' β€+ n') β€* c' β€* c' β€+ z β€* a' β€* (b' β€* c') οΌβ¨ xv β© (succβ€ (x β€* b' β€+ pos n)) β€* c' β€* c' β€+ z β€* a' β€* (b' β€* c') οΌβ¨ xvi β© (succβ€ (x β€* b') β€+ pos n) β€* c' β€* c' β€+ z β€* a' β€* (b' β€* c') οΌβ¨ xvii β© y β€* a' β€* c' β€* c' β€+ z β€* a' β€* (b' β€* c') οΌβ¨ xviii β© y β€* c' β€* a' β€* c' β€+ z β€* (a' β€* (b' β€* c')) οΌβ¨ xix β© y β€* c' β€* (a' β€* c') β€+ z β€* (b' β€* (a' β€* c')) οΌβ¨ xx β© y β€* c' β€* (a' β€* c') β€+ z β€* b' β€* (a' β€* c') οΌβ¨ xxi β© (y β€* c' β€+ z β€* b') β€* (pos (sa) β€* pos (sc)) οΌβ¨ xxii β© (y β€* c' β€+ z β€* b') β€* pos (succ (pred (sa β* sc))) β where i = β€-left-succ ((x β€* c' β€+ z β€* a') β€* pos (succ (pred (sb β* sc)))) (pos (pred (sc β* sc β* sn))) ii = β€-right-succ ((x β€* c' β€+ z β€* a') β€* pos (succ (pred (sb β* sc)))) (pos (pred (sc β* sc β* sn))) β»ΒΉ iii = ap ((x β€* c' β€+ z β€* a') β€* pos (succ (pred (sb β* sc))) β€+_) II iv = ap (Ξ» - β ((x β€* c' β€+ z β€* a') β€* -) β€+ c' β€* c' β€* n') (denom-setup b c) v = ap (Ξ» - β - β€+ c' β€* c' β€* n') (distributivity-mult-over-β€ (x β€* c') (z β€* a') (b' β€* c')) vi = β€+-assoc ( x β€* c' β€* (b' β€* c')) (z β€* a' β€* (b' β€* c')) (c' β€* c' β€* n') vii = ap (x β€* c' β€* (b' β€* c') β€+_) (β€+-comm (z β€* a' β€* (b' β€* c')) (c' β€* c' β€* n')) viii = β€+-assoc (x β€* c' β€* (b' β€* c')) (c' β€* c' β€* n') (z β€* a' β€* (b' β€* c')) β»ΒΉ ix = apβ (Ξ» Ξ± Ξ² β Ξ± β€+ Ξ² β€+ z β€* a' β€* (b' β€* c')) (β€-mult-rearrangement x c' (b' β€* c')) (β€*-comm (c' β€* c') n') xi = ap (Ξ» - β - β€* c' β€+ n' β€* (c' β€* c') β€+ z β€* a' β€* (b' β€* c')) (β€*-assoc x b' c' β»ΒΉ) xii = ap (Ξ» - β - β€+ n' β€* (c' β€* c') β€+ z β€* a' β€* (b' β€* c')) (β€*-assoc (x β€* b') c' c') xiii = ap (Ξ» - β - β€+ z β€* a' β€* (b' β€* c')) (distributivity-mult-over-β€ ( x β€* b') n' (c' β€* c') β»ΒΉ) xiv = ap (Ξ» - β - β€+ z β€* a' β€* (b' β€* c') ) (β€*-assoc ((x β€* b' β€+ n')) c' c' β»ΒΉ) xv = ap (Ξ» - β - β€* c' β€* c' β€+ z β€* a' β€* (b' β€* c')) (β€-right-succ (x β€* b') (pos n)) xvi = ap (Ξ» - β - β€* c' β€* c' β€+ z β€* a' β€* (b' β€* c')) (β€-left-succ (x β€* b') (pos n) β»ΒΉ) xvii = ap (Ξ» - β - β€* c' β€* c' β€+ z β€* a' β€* (b' β€* c')) e xviii = apβ (Ξ» Ξ± Ξ² β Ξ± β€* c' β€+ Ξ²) (β€-mult-rearrangement y a' c') (β€*-assoc z a' (b' β€* c')) xix = apβ (Ξ» Ξ± Ξ² β Ξ± β€+ z β€* Ξ²) (β€*-assoc (y β€* c') a' c') (β€-mult-rearrangement''' a' b' c') xx = ap (Ξ» - β y β€* c' β€* (a' β€* c') β€+ -) (β€*-assoc z b' (a' β€* c') β»ΒΉ) xxi = distributivity-mult-over-β€ (y β€* c') (z β€* b') (a' β€* c') β»ΒΉ xxii = ap (Ξ» - β (y β€* c' β€+ z β€* b') β€* -) (denom-setup a c β»ΒΉ) π½<-adding : (p q r s : π½) β p π½< q β r π½< s β p + r π½< q + s π½<-adding p q r s lβ lβ = π½<-trans (p + r) (q + r) (q + s) I III where I : (p + r) π½< (q + r) I = π½<-addition-preserves-order p q r lβ II : (r + q) π½< (s + q) II = π½<-addition-preserves-order r s q lβ III : (q + r) π½< (q + s) III = transportβ _π½<_ (π½+-comm r q) (π½+-comm s q) II π½<-adding-zero : (p q : π½) β (pos 0 , 0) π½< p β (pos 0 , 0) π½< q β (pos 0 , 0) π½< (p + q) π½<-adding-zero p q lβ lβ = π½<-adding (pos 0 , 0) p (pos 0 , 0) q lβ lβ π½-pos-multiplication-preserves-order : (p q : π½) β (pos 0 , 0) π½< p β (pos 0 , 0) π½< q β (pos 0 , 0) π½< (p * q) π½-pos-multiplication-preserves-order (x , a) (y , b) (c , eβ) (d , eβ) = pred (succ c β* succ d) , I where Ξ± : pos (succ c) οΌ x Ξ± = pos (succ c) οΌβ¨ i β© pos 0 β€+ pos (succ c) οΌβ¨ refl β© pos 0 β€+ succβ€ (pos c) οΌβ¨ ii β© succβ€ (pos 0 β€+ pos c) οΌβ¨ iii β© succβ€ (pos 0) β€+ pos c οΌβ¨ iv β© succβ€ (pos 0 β€* pos (succ a)) β€+ pos c οΌβ¨ eβ β© x β where i = β€-zero-left-neutral (pos (succ c)) β»ΒΉ ii = β€-right-succ (pos 0) (pos c) iii = β€-left-succ (pos 0) (pos c) β»ΒΉ iv = ap (Ξ» - β succβ€ - β€+ pos c) (β€-zero-left-base (pos (succ a)) β»ΒΉ) Ξ² : pos (succ d) οΌ y Ξ² = pos (succ d) οΌβ¨ i β© pos 0 β€+ pos (succ d) οΌβ¨ refl β© pos 0 β€+ succβ€ (pos d) οΌβ¨ ii β© succβ€ (pos 0 β€+ pos d) οΌβ¨ iii β© succβ€ (pos 0) β€+ pos d οΌβ¨ iv β© succβ€ (pos 0 β€* pos (succ b)) β€+ pos d οΌβ¨ eβ β© y β where i = β€-zero-left-neutral (pos (succ d)) β»ΒΉ ii = β€-right-succ (pos 0) (pos d) iii = β€-left-succ (pos 0) (pos d) β»ΒΉ iv = ap (Ξ» - β succβ€ - β€+ pos d) (β€-zero-left-base (pos (succ b)) β»ΒΉ) I : succβ€ (pos 0 β€* pos (succ (pred (succ a β* succ b)))) β€+ pos (pred (succ c β* succ d)) οΌ x β€* y β€* pos 1 I = succβ€ (pos 0 β€* pos (succ (pred (succ a β* succ b)))) β€+ pos (pred (succ c β* succ d)) οΌβ¨ i β© succβ€ (pos 0) β€+ pos (pred (succ c β* succ d)) οΌβ¨ ii β© succβ€ (pos 0 β€+ pos (pred (succ c β* succ d))) οΌβ¨ iii β© succβ€ (pos (pred (succ c β* succ d))) οΌβ¨ refl β© pos (succ (pred (succ c β* succ d))) οΌβ¨ iv β© pos (succ c β* succ d) οΌβ¨ v β© pos (succ c) β€* pos (succ d) οΌβ¨ vi β© x β€* y οΌβ¨ vii β© x β€* y β€* pos 1 β where iββ : pos 0 β€* pos (succ (pred (succ a β* succ b))) οΌ pos 0 iββ = β€-zero-left-base (pos (succ (pred (succ a β* succ b)))) ivββ : Β¬ (succ c β* succ d οΌ 0) ivββ = β-positive-multiplication-not-zero c d i = ap (Ξ» - β succβ€ - β€+ pos (pred (succ c β* succ d))) iββ ii = β€-left-succ (pos 0) (pos (pred (succ c β* succ d))) iii = ap succβ€ (β€-zero-left-neutral (pos (pred (succ c β* succ d)))) iv = ap pos (succ-pred' (succ c β* succ d) ivββ) v = pos-multiplication-equiv-to-β (succ c) (succ d) β»ΒΉ vi = apβ _β€*_ Ξ± Ξ² vii = β€-mult-right-id (x β€* y) π½β€-pos-multiplication-preserves-order : (p q : π½) β (pos 0 , 0) π½β€ p β (pos 0 , 0) π½β€ q β (pos 0 , 0) π½β€ (p * q) π½β€-pos-multiplication-preserves-order (x , a) (y , b) (c , eβ) (d , eβ) = c β* d , I where a' = pos (succ a) c' = pos c d' = pos d I : pos 0 β€* pos (succ (pred (succ a β* succ b))) β€+ pos (c β* d) οΌ x β€* y β€* pos 1 I = pos 0 β€* pos (succ (pred (succ a β* succ b))) β€+ pos (c β* d) οΌβ¨ i β© pos 0 β€+ pos (c β* d) οΌβ¨ ii β© pos 0 β€+ c' β€* d' οΌβ¨ iii β© c' β€* d' οΌβ¨ iv β© (pos 0 β€+ c') β€* d' οΌβ¨ v β© (pos 0 β€+ c') β€* (pos 0 β€+ d') οΌβ¨ vi β© (pos 0 β€* a' β€+ c') β€* (pos 0 β€+ d') οΌβ¨ vii β© (pos 0 β€* a' β€+ c') β€* (pos 0 β€* pos (succ b) β€+ d') οΌβ¨ viii β© x β€* pos 1 β€* (y β€* pos 1) οΌβ¨ ix β© x β€* y β€* pos 1 β where iββ = β€-zero-left-base (pos (succ (pred (succ a β* succ b)))) viiiββ = β€-zero-left-base (pos (succ b)) β»ΒΉ i = ap (_β€+ pos (c β* d)) iββ ii = ap (pos 0 β€+_) (pos-multiplication-equiv-to-β c d β»ΒΉ) iii = β€-zero-left-neutral (c' β€* d') iv = ap (_β€* d') (β€-zero-left-neutral c' β»ΒΉ) v = ap ((pos 0 β€+ c') β€*_) (β€-zero-left-neutral d' β»ΒΉ) vi = ap (Ξ» z β (z β€+ c') β€* (pos 0 β€+ d')) (β€-zero-left-base a' β»ΒΉ) vii = ap (Ξ» z β (pos 0 β€* a' β€+ c') β€* (z β€+ d')) viiiββ viii = apβ _β€*_ eβ eβ ix = ap (_β€* (y β€* pos 1)) (β€-mult-right-id x β»ΒΉ) 0π½β€1 : (pos 0 , 0) π½β€ (pos 1 , 0) 0π½β€1 = 1 , refl 1π½β€1 : (pos 1 , 0) π½β€ (pos 1 , 0) 1π½β€1 = 0 , refl 2/3π½β€1 : (pos 2 , 2) π½β€ (pos 1 , 0) 2/3π½β€1 = 1 , refl negative-not-greater-than-zero : (x a : β) β Β¬ ((pos 0 , 0) π½< (negsucc x , a)) negative-not-greater-than-zero x a (n , l) = negsucc-not-pos Ξ³ where Ξ³ : negsucc x β€* pos 1 οΌ pos (succ n) Ξ³ = negsucc x β€* pos 1 οΌβ¨ l β»ΒΉ β© succβ€ (pos 0 β€* pos (succ a)) β€+ pos n οΌβ¨ i β© succβ€ (pos 0 β€* pos (succ a) β€+ pos n) οΌβ¨ ii β© pos 0 β€* pos (succ a) β€+ succβ€ (pos n) οΌβ¨ iii β© pos 0 β€+ pos (succ n) οΌβ¨ iv β© pos (succ n) β where i = β€-left-succ (pos 0 β€* pos (succ a)) (pos n) ii = β€-right-succ (pos 0 β€* pos (succ a)) (pos n) β»ΒΉ iii = ap (_β€+ pos (succ n)) (β€-zero-left-base (pos (succ a))) iv = β€-zero-left-neutral (pos (succ n)) positive-order-flip : (m n a b : β) β ((pos (succ m)) , a) π½< ((pos (succ n)) , b) β ((pos (succ a)) , m) π½> ((pos (succ b)) , n) positive-order-flip m n a b l = transportβ _<_ I II l where I : pos (succ m) β€* pos (succ b) οΌ pos (succ b) β€* pos (succ m) I = β€*-comm (pos (succ m)) (pos (succ b)) II : pos (succ n) β€* pos (succ a) οΌ pos (succ a) β€* pos (succ n) II = β€*-comm (pos (succ n)) (pos (succ a)) \end{code}