From c703c36de9766fddb29e26aa3bf295145196f258 Mon Sep 17 00:00:00 2001 From: Brad Dragun <35275808+LuuBluum@users.noreply.github.com> Date: Thu, 2 Nov 2023 05:28:16 -0700 Subject: [PATCH] Rational order (#1071) * Adjust Rationals to use Int instead of QuoInt * Define ordering over rationals and basic properties * More properties of rational order * Clean up definition of order; define trichotomy * Add multiplication cancelling to rationals * Fix whitespace * Remove unnecessary 0<1 proof * Redefine rec2 with pattern matching --- Cubical/Algebra/CommRing/Instances/Int.agda | 2 +- .../Algebra/CommRing/Instances/Rationals.agda | 2 +- .../Algebra/Field/Instances/Rationals.agda | 2 +- Cubical/Algebra/Group/ZAction.agda | 6 +- Cubical/Data/Int/Order.agda | 258 +++++- Cubical/Data/Int/Properties.agda | 853 +++++++++++++++++- Cubical/Data/Rationals/Base.agda | 23 +- .../Data/Rationals/MoreRationals/QuoQ.agda | 6 + .../Rationals/MoreRationals/QuoQ/Base.agda | 75 ++ .../MoreRationals/QuoQ/Properties.agda | 248 +++++ .../MoreRationals/SigmaQ/Properties.agda | 2 +- Cubical/Data/Rationals/Order.agda | 623 +++++++++++++ Cubical/Data/Rationals/Properties.agda | 424 +++++++-- Cubical/HITs/SetQuotients/Properties.agda | 34 +- Cubical/Relation/Binary/Base.agda | 3 + .../Tactics/CommRingSolver/RawAlgebra.agda | 3 + 16 files changed, 2397 insertions(+), 167 deletions(-) create mode 100644 Cubical/Data/Rationals/MoreRationals/QuoQ.agda create mode 100644 Cubical/Data/Rationals/MoreRationals/QuoQ/Base.agda create mode 100644 Cubical/Data/Rationals/MoreRationals/QuoQ/Properties.agda create mode 100644 Cubical/Data/Rationals/Order.agda diff --git a/Cubical/Algebra/CommRing/Instances/Int.agda b/Cubical/Algebra/CommRing/Instances/Int.agda index c20c263cb1..2e6a6b5360 100644 --- a/Cubical/Algebra/CommRing/Instances/Int.agda +++ b/Cubical/Algebra/CommRing/Instances/Int.agda @@ -22,4 +22,4 @@ isCommRing (snd ℤCommRing) = isCommRingℤ isCommRingℤ : IsCommRing 0 1 _+ℤ_ _·ℤ_ -ℤ_ isCommRingℤ = makeIsCommRing isSetℤ Int.+Assoc (λ _ → refl) -Cancel Int.+Comm Int.·Assoc - Int.·Rid ·DistR+ Int.·Comm + Int.·IdR ·DistR+ Int.·Comm diff --git a/Cubical/Algebra/CommRing/Instances/Rationals.agda b/Cubical/Algebra/CommRing/Instances/Rationals.agda index 131125ed07..8b70bf4ec2 100644 --- a/Cubical/Algebra/CommRing/Instances/Rationals.agda +++ b/Cubical/Algebra/CommRing/Instances/Rationals.agda @@ -9,7 +9,7 @@ module Cubical.Algebra.CommRing.Instances.Rationals where open import Cubical.Foundations.Prelude open import Cubical.Algebra.CommRing -open import Cubical.Data.Rationals +open import Cubical.Data.Rationals.MoreRationals.QuoQ renaming (ℚ to ℚType ; _+_ to _+ℚ_; _·_ to _·ℚ_; -_ to -ℚ_) open CommRingStr diff --git a/Cubical/Algebra/Field/Instances/Rationals.agda b/Cubical/Algebra/Field/Instances/Rationals.agda index 1c792f45dc..0e1646e889 100644 --- a/Cubical/Algebra/Field/Instances/Rationals.agda +++ b/Cubical/Algebra/Field/Instances/Rationals.agda @@ -19,7 +19,7 @@ open import Cubical.Data.Int.MoreInts.QuoInt ; ·-zeroˡ to ·ℤ-zeroˡ ; ·-identityʳ to ·ℤ-identityʳ) open import Cubical.HITs.SetQuotients as SetQuot -open import Cubical.Data.Rationals +open import Cubical.Data.Rationals.MoreRationals.QuoQ using (ℚ ; ℕ₊₁→ℤ ; isEquivRel∼) open import Cubical.Algebra.Field diff --git a/Cubical/Algebra/Group/ZAction.agda b/Cubical/Algebra/Group/ZAction.agda index 217a7240f2..38afec7235 100644 --- a/Cubical/Algebra/Group/ZAction.agda +++ b/Cubical/Algebra/Group/ZAction.agda @@ -12,9 +12,9 @@ open import Cubical.Foundations.HLevels open import Cubical.Foundations.Powerset open import Cubical.Data.Sigma -open import Cubical.Data.Int +open import Cubical.Data.Int as ℤ renaming - (_·_ to _*_ ; _+_ to _+ℤ_ ; _-_ to _-ℤ_ ; pos·pos to pos·) hiding (·Assoc) + (_·_ to _*_ ; _+_ to _+ℤ_ ; _-_ to _-ℤ_ ; pos·pos to pos·) hiding (·Assoc; ·IdL; ·IdR) open import Cubical.Data.Nat renaming (_·_ to _·ℕ_ ; _+_ to _+ℕ_) open import Cubical.Data.Nat.Mod open import Cubical.Data.Nat.Order @@ -410,7 +410,7 @@ characℤ≅ℤ e = (λ p → inr (Σ≡Prop (λ _ → isPropIsGroupHom _ _) (Σ≡Prop (λ _ → isPropIsEquiv _) (funExt λ x → - cong (fst (fst e)) (sym (·Rid x)) + cong (fst (fst e)) (sym (ℤ.·IdR x)) ∙ GroupHomℤ→ℤPres· ((fst (fst e)) , (snd e)) x 1 ∙ cong (x *_) p ∙ ·Comm x -1 )))) diff --git a/Cubical/Data/Int/Order.agda b/Cubical/Data/Int/Order.agda index 84bb2fb1f5..fac79698da 100644 --- a/Cubical/Data/Int/Order.agda +++ b/Cubical/Data/Int/Order.agda @@ -2,12 +2,14 @@ module Cubical.Data.Int.Order where open import Cubical.Foundations.Prelude +open import Cubical.Foundations.Isomorphism open import Cubical.Foundations.Function -open import Cubical.Data.Empty as ⊥ +open import Cubical.Data.Empty as ⊥ using (⊥) open import Cubical.Data.Int.Base as ℤ open import Cubical.Data.Int.Properties as ℤ open import Cubical.Data.Nat as ℕ +open import Cubical.Data.NatPlusOne.Base as ℕ₊₁ open import Cubical.Data.Sigma open import Cubical.Relation.Nullary @@ -47,9 +49,35 @@ isProp≤ {m} {n} (k , p) (l , q) lemma : k ≡ l lemma = injPos (inj-z+ (p ∙ sym q)) +isProp< : isProp (m < n) +isProp< = isProp≤ + zero-≤pos : 0 ≤ pos l zero-≤pos {l} = l , (sym (pos0+ (pos l))) +¬-pos<-zero : ¬ (pos k) < 0 +¬-pos<-zero {k} (i , p) = snotz (injPos (pos+ (suc k) i ∙ p)) + +negsuc<-zero : negsuc k < 0 +negsuc<-zero {k} = k , + ((sucℤ (negsuc k) +pos k) ≡⟨ sym (sucℤ+ (negsuc k) (pos k)) ⟩ + sucℤ (negsuc k +pos k) ≡⟨ +sucℤ (negsuc k) (pos k) ⟩ + neg (suc k) ℤ.+ pos (suc k) ≡⟨ -Cancel' (pos (suc k)) ⟩ + pos zero ∎) + +¬pos≤negsuc : ¬ (pos k) ≤ negsuc l +¬pos≤negsuc {k} {l} (i , p) = posNotnegsuc (k ℕ.+ i) l (pos+ k i ∙ p) + +negsuc_ + +private + ·CommR : (a b c : ℤ) → a ℤ.· b ℤ.· c ≡ a ℤ.· c ℤ.· b + ·CommR a b c = sym (ℤ.·Assoc a b c) ∙ cong (a ℤ.·_) (ℤ.·Comm b c) ∙ ℤ.·Assoc a c b + + _≤'_ : ℚ → ℚ → hProp ℓ-zero + _≤'_ = fun + where + lemma≤ : ((a , b) (c , d) (e , f) : (ℤ × ℕ₊₁)) + → (c ℤ.· ℕ₊₁→ℤ f ) ≡ (e ℤ.· ℕ₊₁→ℤ d) + → ((a ℤ.· ℕ₊₁→ℤ d) ℤ.≤ (c ℤ.· ℕ₊₁→ℤ b)) + ≡ ((a ℤ.· ℕ₊₁→ℤ f) ℤ.≤ (e ℤ.· ℕ₊₁→ℤ b)) + lemma≤ (a , b) (c , d) (e , f) cf≡ed = (ua (propBiimpl→Equiv ℤ.isProp≤ ℤ.isProp≤ + (ℤ.≤-·o-cancel {k = -1+ d} ∘ + subst2 ℤ._≤_ (·CommR a (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ f)) + (·CommR c (ℕ₊₁→ℤ b) (ℕ₊₁→ℤ f) ∙ + cong (ℤ._· ℕ₊₁→ℤ b) cf≡ed ∙ + ·CommR e (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ b)) ∘ + ℤ.≤-·o {k = ℕ₊₁→ℕ f}) + (ℤ.≤-·o-cancel {k = -1+ f} ∘ + subst2 ℤ._≤_ (·CommR a (ℕ₊₁→ℤ f) (ℕ₊₁→ℤ d)) + (·CommR e (ℕ₊₁→ℤ b) (ℕ₊₁→ℤ d) ∙ + cong (ℤ._· ℕ₊₁→ℤ b) (sym cf≡ed) ∙ + ·CommR c (ℕ₊₁→ℤ f) (ℕ₊₁→ℤ b)) ∘ + ℤ.≤-·o {k = ℕ₊₁→ℕ d}))) + + fun₀ : ℤ × ℕ₊₁ → ℚ → hProp ℓ-zero + fst (fun₀ (a , b) [ c , d ]) = a ℤ.· ℕ₊₁→ℤ d ℤ.≤ c ℤ.· ℕ₊₁→ℤ b + snd (fun₀ _ [ _ ]) = ℤ.isProp≤ + fun₀ a/b (eq/ c/d e/f cf≡ed i) = record + { fst = lemma≤ a/b c/d e/f cf≡ed i + ; snd = isProp→PathP (λ i → isPropIsProp {A = lemma≤ a/b c/d e/f cf≡ed i}) ℤ.isProp≤ ℤ.isProp≤ i + } + fun₀ a/b (squash/ x y p q i j) = isSet→SquareP (λ _ _ → isSetHProp) + (λ _ → fun₀ a/b x) + (λ _ → fun₀ a/b y) + (λ i → fun₀ a/b (p i)) + (λ i → fun₀ a/b (q i)) j i + + toPath : ∀ a/b c/d (x : a/b ∼ c/d) (y : ℚ) → fun₀ a/b y ≡ fun₀ c/d y + toPath (a , b) (c , d) ad≡cb = elimProp (λ _ → isSetHProp _ _) λ (e , f) → + Σ≡Prop (λ _ → isPropIsProp) (ua (propBiimpl→Equiv ℤ.isProp≤ ℤ.isProp≤ + (ℤ.≤-·o-cancel {k = -1+ b} ∘ + subst2 ℤ._≤_ (·CommR a (ℕ₊₁→ℤ f) (ℕ₊₁→ℤ d) ∙ + cong (ℤ._· ℕ₊₁→ℤ f) ad≡cb ∙ + ·CommR c (ℕ₊₁→ℤ b) (ℕ₊₁→ℤ f)) + (·CommR e (ℕ₊₁→ℤ b) (ℕ₊₁→ℤ d)) ∘ + ℤ.≤-·o {k = ℕ₊₁→ℕ d}) + (ℤ.≤-·o-cancel {k = -1+ d} ∘ + subst2 ℤ._≤_ (·CommR c (ℕ₊₁→ℤ f) (ℕ₊₁→ℤ b) ∙ + cong (ℤ._· ℕ₊₁→ℤ f) (sym ad≡cb) ∙ + ·CommR a (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ f)) + (·CommR e (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ b)) ∘ + ℤ.≤-·o {k = ℕ₊₁→ℕ b}))) + + fun : ℚ → ℚ → hProp ℓ-zero + fun [ a/b ] y = fun₀ a/b y + fun (eq/ a/b c/d ad≡cb i) y = toPath a/b c/d ad≡cb y i + fun (squash/ x y p q i j) z = isSet→SquareP (λ _ _ → isSetHProp) + (λ _ → fun x z) (λ _ → fun y z) (λ i → fun (p i) z) (λ i → fun (q i) z) j i + + _<'_ : ℚ → ℚ → hProp ℓ-zero + _<'_ = fun + where + lemma< : ((a , b) (c , d) (e , f) : (ℤ × ℕ₊₁)) + → (c ℤ.· ℕ₊₁→ℤ f ) ≡ (e ℤ.· ℕ₊₁→ℤ d) + → ((a ℤ.· ℕ₊₁→ℤ d) ℤ.< (c ℤ.· ℕ₊₁→ℤ b)) + ≡ ((a ℤ.· ℕ₊₁→ℤ f) ℤ.< (e ℤ.· ℕ₊₁→ℤ b)) + lemma< (a , b) (c , d) (e , f) cf≡ed = (ua (propBiimpl→Equiv ℤ.isProp< ℤ.isProp< + (ℤ.<-·o-cancel {k = -1+ d} ∘ + subst2 ℤ._<_ (·CommR a (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ f)) + (·CommR c (ℕ₊₁→ℤ b) (ℕ₊₁→ℤ f) ∙ + cong (ℤ._· ℕ₊₁→ℤ b) cf≡ed ∙ + ·CommR e (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ b)) ∘ + ℤ.<-·o {k = -1+ f}) + (ℤ.<-·o-cancel {k = -1+ f} ∘ + subst2 ℤ._<_ (·CommR a (ℕ₊₁→ℤ f) (ℕ₊₁→ℤ d)) + (·CommR e (ℕ₊₁→ℤ b) (ℕ₊₁→ℤ d) ∙ + cong (ℤ._· ℕ₊₁→ℤ b) (sym cf≡ed) ∙ + ·CommR c (ℕ₊₁→ℤ f) (ℕ₊₁→ℤ b)) ∘ + ℤ.<-·o {k = -1+ d}))) + + fun₀ : ℤ × ℕ₊₁ → ℚ → hProp ℓ-zero + fst (fun₀ (a , b) [ c , d ]) = a ℤ.· ℕ₊₁→ℤ d ℤ.< c ℤ.· ℕ₊₁→ℤ b + snd (fun₀ _ [ _ ]) = ℤ.isProp< + fun₀ a/b (eq/ c/d e/f cf≡ed i) = record + { fst = lemma< a/b c/d e/f cf≡ed i + ; snd = isProp→PathP (λ i → isPropIsProp {A = lemma< a/b c/d e/f cf≡ed i}) ℤ.isProp< ℤ.isProp< i + } + fun₀ a/b (squash/ x y p q i j) = isSet→SquareP (λ _ _ → isSetHProp) + (λ _ → fun₀ a/b x) + (λ _ → fun₀ a/b y) + (λ i → fun₀ a/b (p i)) + (λ i → fun₀ a/b (q i)) j i + + toPath : ∀ a/b c/d (x : a/b ∼ c/d) (y : ℚ) → fun₀ a/b y ≡ fun₀ c/d y + toPath (a , b) (c , d) ad≡cb = elimProp (λ _ → isSetHProp _ _) λ (e , f) → + Σ≡Prop (λ _ → isPropIsProp) (ua (propBiimpl→Equiv ℤ.isProp< ℤ.isProp< + (ℤ.<-·o-cancel {k = -1+ b} ∘ + subst2 ℤ._<_ (·CommR a (ℕ₊₁→ℤ f) (ℕ₊₁→ℤ d) ∙ + cong (ℤ._· ℕ₊₁→ℤ f) ad≡cb ∙ + ·CommR c (ℕ₊₁→ℤ b) (ℕ₊₁→ℤ f)) + (·CommR e (ℕ₊₁→ℤ b) (ℕ₊₁→ℤ d)) ∘ + ℤ.<-·o {k = -1+ d}) + (ℤ.<-·o-cancel {k = -1+ d} ∘ + subst2 ℤ._<_ (·CommR c (ℕ₊₁→ℤ f) (ℕ₊₁→ℤ b) ∙ + cong (ℤ._· ℕ₊₁→ℤ f) (sym ad≡cb) ∙ + ·CommR a (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ f)) + (·CommR e (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ b)) ∘ + ℤ.<-·o {k = -1+ b}))) + + fun : ℚ → ℚ → hProp ℓ-zero + fun [ a/b ] y = fun₀ a/b y + fun (eq/ a/b c/d ad≡cb i) y = toPath a/b c/d ad≡cb y i + fun (squash/ x y p q i j) z = isSet→SquareP (λ _ _ → isSetHProp) + (λ _ → fun x z) (λ _ → fun y z) (λ i → fun (p i) z) (λ i → fun (q i) z) j i + +_≤_ : ℚ → ℚ → Type₀ +m ≤ n = fst (m ≤' n) + +_<_ : ℚ → ℚ → Type₀ +m < n = fst (m <' n) + +_≥_ : ℚ → ℚ → Type₀ +m ≥ n = n ≤ m + +_>_ : ℚ → ℚ → Type₀ +m > n = n < m + +_#_ : ℚ → ℚ → Type₀ +m # n = (m < n) ⊎ (n < m) + +data Trichotomy (m n : ℚ) : Type₀ where + lt : m < n → Trichotomy m n + eq : m ≡ n → Trichotomy m n + gt : m > n → Trichotomy m n + +module _ where + open BinaryRelation + + isProp≤ : isPropValued _≤_ + isProp≤ m n = snd (m ≤' n) + + isProp< : isPropValued _<_ + isProp< m n = snd (m <' n) + + isRefl≤ : isRefl _≤_ + isRefl≤ = elimProp {P = λ x → x ≤ x} (λ x → isProp≤ x x) λ _ → ℤ.isRefl≤ + + isIrrefl< : isIrrefl _<_ + isIrrefl< = elimProp {P = λ x → ¬ x < x} (λ _ → isProp¬ _) λ _ → ℤ.isIrrefl< + + isAntisym≤ : isAntisym _≤_ + isAntisym≤ = + elimProp2 {P = λ a b → a ≤ b → b ≤ a → a ≡ b} + (λ x y → isPropΠ2 λ _ _ → isSetℚ x y) + λ a b a≤b b≤a → eq/ a b (ℤ.isAntisym≤ a≤b b≤a) + + isTrans≤ : isTrans _≤_ + isTrans≤ = + elimProp3 {P = λ a b c → a ≤ b → b ≤ c → a ≤ c} + (λ x _ z → isPropΠ2 λ _ _ → isProp≤ x z) + λ { (a , b) (c , d) (e , f) ad≤cb cf≤ed → + ℤ.≤-·o-cancel {k = -1+ d} + (subst (ℤ._≤ e ℤ.· ℕ₊₁→ℤ b ℤ.· ℕ₊₁→ℤ d) + (·CommR a (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ f)) + (ℤ.isTrans≤ (ℤ.≤-·o {k = ℕ₊₁→ℕ f} ad≤cb) + (subst2 ℤ._≤_ + (·CommR c (ℕ₊₁→ℤ f) (ℕ₊₁→ℤ b)) + (·CommR e (ℕ₊₁→ℤ d) (ℕ₊₁→ℤ b)) + (ℤ.≤-·o {k = ℕ₊₁→ℕ b} cf≤ed)))) } + + isTrans< : isTrans _<_ + isTrans< = + elimProp3 {P = λ a b c → a < b → b < c → a < c} + (λ x _ z → isPropΠ2 λ _ _ → isProp< x z) + λ { (a , b) (c , d) (e , f) ad n) +≤→≯ m n m≤n n