state
stringlengths
0
159k
srcUpToTactic
stringlengths
387
167k
nextTactic
stringlengths
3
9k
declUpToTactic
stringlengths
22
11.5k
declId
stringlengths
38
95
decl
stringlengths
16
1.89k
file_tag
stringlengths
17
73
a b c : β„š this : Rat.Nonneg (b - a) ∨ Rat.Nonneg (-(b - a)) ⊒ Rat.Nonneg (b - a) ∨ Rat.Nonneg (a - b)
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg]
rwa [neg_sub] at this
protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg]
Mathlib.Data.Rat.Order.166_0.NTjR6KCugNscheB
protected theorem le_total : a ≀ b ∨ b ≀ a
Mathlib_Data_Rat_Order
a✝ b✝ c a b : β„š hab : a ≀ b hba : b ≀ a ⊒ a = b
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by
rw [Rat.le_iff_Nonneg] at hab hba
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by
Mathlib.Data.Rat.Order.172_0.NTjR6KCugNscheB
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b
Mathlib_Data_Rat_Order
a✝ b✝ c a b : β„š hab : Rat.Nonneg (b - a) hba : Rat.Nonneg (a - b) ⊒ a = b
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba
rw [sub_eq_add_neg] at hba
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba
Mathlib.Data.Rat.Order.172_0.NTjR6KCugNscheB
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b
Mathlib_Data_Rat_Order
a✝ b✝ c a b : β„š hab : Rat.Nonneg (b - a) hba : Rat.Nonneg (a + -b) ⊒ a = b
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba
rw [← neg_sub, sub_eq_add_neg] at hab
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba
Mathlib.Data.Rat.Order.172_0.NTjR6KCugNscheB
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b
Mathlib_Data_Rat_Order
a✝ b✝ c a b : β„š hab : Rat.Nonneg (-(a + -b)) hba : Rat.Nonneg (a + -b) ⊒ a = b
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab
have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab)
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab
Mathlib.Data.Rat.Order.172_0.NTjR6KCugNscheB
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b
Mathlib_Data_Rat_Order
a✝ b✝ c a b : β„š hab : Rat.Nonneg (-(a + -b)) hba : Rat.Nonneg (a + -b) this : a = - -b ⊒ a = b
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab)
rwa [neg_neg] at this
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab)
Mathlib.Data.Rat.Order.172_0.NTjR6KCugNscheB
protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b
Mathlib_Data_Rat_Order
a✝ b✝ c✝ a b c : β„š hab : a ≀ b hbc : b ≀ c ⊒ a ≀ c
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by
rw [Rat.le_iff_Nonneg] at hab hbc
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by
Mathlib.Data.Rat.Order.180_0.NTjR6KCugNscheB
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c
Mathlib_Data_Rat_Order
a✝ b✝ c✝ a b c : β„š hab : Rat.Nonneg (b - a) hbc : Rat.Nonneg (c - b) ⊒ a ≀ c
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc
have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc
Mathlib.Data.Rat.Order.180_0.NTjR6KCugNscheB
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c
Mathlib_Data_Rat_Order
a✝ b✝ c✝ a b c : β„š hab : Rat.Nonneg (b - a) hbc : Rat.Nonneg (c - b) this : Rat.Nonneg (b - a + (c - b)) ⊒ a ≀ c
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc
simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc
Mathlib.Data.Rat.Order.180_0.NTjR6KCugNscheB
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c
Mathlib_Data_Rat_Order
a✝ b✝ c✝ a b c : β„š hab : Rat.Nonneg (b - a) hbc : Rat.Nonneg (c - b) this : Rat.Nonneg (c - a) ⊒ a ≀ c
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this
rw [Rat.le_iff_Nonneg]
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this
Mathlib.Data.Rat.Order.180_0.NTjR6KCugNscheB
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c
Mathlib_Data_Rat_Order
a✝ b✝ c✝ a b c : β„š hab : Rat.Nonneg (b - a) hbc : Rat.Nonneg (c - b) this : Rat.Nonneg (c - a) ⊒ Rat.Nonneg (c - a)
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg]
exact this
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg]
Mathlib.Data.Rat.Order.180_0.NTjR6KCugNscheB
protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c
Mathlib_Data_Rat_Order
a b c x✝¹ x✝ : β„š ⊒ x✝¹ < x✝ ↔ x✝¹ ≀ x✝ ∧ Β¬x✝ ≀ x✝¹
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by
rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left]
instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by
Mathlib.Data.Rat.Order.192_0.NTjR6KCugNscheB
instance linearOrder : LinearOrder β„š where le_refl
Mathlib_Data_Rat_Order
a b c x✝¹ x✝ : β„š ⊒ Decidable ((fun x x_1 => x ≀ x_1) x✝¹ x✝)
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by
infer_instance
instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by
Mathlib.Data.Rat.Order.192_0.NTjR6KCugNscheB
instance linearOrder : LinearOrder β„š where le_refl
Mathlib_Data_Rat_Order
a b c : β„š ⊒ LT β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by
infer_instance
instance : LT β„š := by
Mathlib.Data.Rat.Order.202_0.NTjR6KCugNscheB
instance : LT β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ DistribLattice β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by
infer_instance
instance : DistribLattice β„š := by
Mathlib.Data.Rat.Order.204_0.NTjR6KCugNscheB
instance : DistribLattice β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ Lattice β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by
infer_instance
instance : Lattice β„š := by
Mathlib.Data.Rat.Order.206_0.NTjR6KCugNscheB
instance : Lattice β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ SemilatticeInf β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by
infer_instance
instance : SemilatticeInf β„š := by
Mathlib.Data.Rat.Order.208_0.NTjR6KCugNscheB
instance : SemilatticeInf β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ SemilatticeSup β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by
infer_instance
instance : SemilatticeSup β„š := by
Mathlib.Data.Rat.Order.210_0.NTjR6KCugNscheB
instance : SemilatticeSup β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ Inf β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by
infer_instance
instance : Inf β„š := by
Mathlib.Data.Rat.Order.212_0.NTjR6KCugNscheB
instance : Inf β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ Sup β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by
infer_instance
instance : Sup β„š := by
Mathlib.Data.Rat.Order.214_0.NTjR6KCugNscheB
instance : Sup β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ PartialOrder β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by
infer_instance
instance : PartialOrder β„š := by
Mathlib.Data.Rat.Order.216_0.NTjR6KCugNscheB
instance : PartialOrder β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ Preorder β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by
infer_instance
instance : Preorder β„š := by
Mathlib.Data.Rat.Order.218_0.NTjR6KCugNscheB
instance : Preorder β„š
Mathlib_Data_Rat_Order
a b c p q : β„š ⊒ p ≀ q ↔ p.num * ↑q.den ≀ q.num * ↑p.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by
rw [← @num_den q, ← @num_den p]
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by
Mathlib.Data.Rat.Order.220_0.NTjR6KCugNscheB
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den
Mathlib_Data_Rat_Order
a b c p q : β„š ⊒ p.num /. ↑p.den ≀ q.num /. ↑q.den ↔ (p.num /. ↑p.den).num * ↑(q.num /. ↑q.den).den ≀ (q.num /. ↑q.den).num * ↑(p.num /. ↑p.den).den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p]
conv_rhs => simp only [num_den]
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p]
Mathlib.Data.Rat.Order.220_0.NTjR6KCugNscheB
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den
Mathlib_Data_Rat_Order
a b c p q : β„š | (p.num /. ↑p.den).num * ↑(q.num /. ↑q.den).den ≀ (q.num /. ↑q.den).num * ↑(p.num /. ↑p.den).den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs =>
simp only [num_den]
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs =>
Mathlib.Data.Rat.Order.220_0.NTjR6KCugNscheB
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den
Mathlib_Data_Rat_Order
a b c p q : β„š | (p.num /. ↑p.den).num * ↑(q.num /. ↑q.den).den ≀ (q.num /. ↑q.den).num * ↑(p.num /. ↑p.den).den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs =>
simp only [num_den]
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs =>
Mathlib.Data.Rat.Order.220_0.NTjR6KCugNscheB
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den
Mathlib_Data_Rat_Order
a b c p q : β„š | (p.num /. ↑p.den).num * ↑(q.num /. ↑q.den).den ≀ (q.num /. ↑q.den).num * ↑(p.num /. ↑p.den).den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs =>
simp only [num_den]
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs =>
Mathlib.Data.Rat.Order.220_0.NTjR6KCugNscheB
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den
Mathlib_Data_Rat_Order
a b c p q : β„š ⊒ p.num /. ↑p.den ≀ q.num /. ↑q.den ↔ p.num * ↑q.den ≀ q.num * ↑p.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den]
exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos)
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den]
Mathlib.Data.Rat.Order.220_0.NTjR6KCugNscheB
protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den
Mathlib_Data_Rat_Order
a b c p q : β„š ⊒ p < q ↔ p.num * ↑q.den < q.num * ↑p.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by
rw [lt_iff_le_and_ne, Rat.le_def']
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by
Mathlib.Data.Rat.Order.226_0.NTjR6KCugNscheB
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den
Mathlib_Data_Rat_Order
a b c p q : β„š ⊒ p.num * ↑q.den ≀ q.num * ↑p.den ∧ p β‰  q ↔ p.num * ↑q.den < q.num * ↑p.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def']
suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def']
Mathlib.Data.Rat.Order.226_0.NTjR6KCugNscheB
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den
Mathlib_Data_Rat_Order
a b c p q : β„š this : p β‰  q ↔ p.num * ↑q.den β‰  q.num * ↑p.den ⊒ p.num * ↑q.den ≀ q.num * ↑p.den ∧ p β‰  q ↔ p.num * ↑q.den < q.num * ↑p.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by
constructor
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by
Mathlib.Data.Rat.Order.226_0.NTjR6KCugNscheB
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den
Mathlib_Data_Rat_Order
case mp a b c p q : β„š this : p β‰  q ↔ p.num * ↑q.den β‰  q.num * ↑p.den ⊒ p.num * ↑q.den ≀ q.num * ↑p.den ∧ p β‰  q β†’ p.num * ↑q.den < q.num * ↑p.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;>
intro h
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;>
Mathlib.Data.Rat.Order.226_0.NTjR6KCugNscheB
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den
Mathlib_Data_Rat_Order
case mpr a b c p q : β„š this : p β‰  q ↔ p.num * ↑q.den β‰  q.num * ↑p.den ⊒ p.num * ↑q.den < q.num * ↑p.den β†’ p.num * ↑q.den ≀ q.num * ↑p.den ∧ p β‰  q
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;>
intro h
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;>
Mathlib.Data.Rat.Order.226_0.NTjR6KCugNscheB
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den
Mathlib_Data_Rat_Order
case mp a b c p q : β„š this : p β‰  q ↔ p.num * ↑q.den β‰  q.num * ↑p.den h : p.num * ↑q.den ≀ q.num * ↑p.den ∧ p β‰  q ⊒ p.num * ↑q.den < q.num * ↑p.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β·
exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β·
Mathlib.Data.Rat.Order.226_0.NTjR6KCugNscheB
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den
Mathlib_Data_Rat_Order
case mpr a b c p q : β„š this : p β‰  q ↔ p.num * ↑q.den β‰  q.num * ↑p.den h : p.num * ↑q.den < q.num * ↑p.den ⊒ p.num * ↑q.den ≀ q.num * ↑p.den ∧ p β‰  q
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β·
have tmp := lt_iff_le_and_ne.mp h
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β·
Mathlib.Data.Rat.Order.226_0.NTjR6KCugNscheB
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den
Mathlib_Data_Rat_Order
case mpr a b c p q : β„š this : p β‰  q ↔ p.num * ↑q.den β‰  q.num * ↑p.den h : p.num * ↑q.den < q.num * ↑p.den tmp : p.num * ↑q.den ≀ q.num * ↑p.den ∧ p.num * ↑q.den β‰  q.num * ↑p.den ⊒ p.num * ↑q.den ≀ q.num * ↑p.den ∧ p β‰  q
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h
exact ⟨tmp.left, this.mpr tmp.right⟩
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h
Mathlib.Data.Rat.Order.226_0.NTjR6KCugNscheB
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den
Mathlib_Data_Rat_Order
a b c p q : β„š ⊒ p β‰  q ↔ p.num * ↑q.den β‰  q.num * ↑p.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩
exact not_iff_not.mpr eq_iff_mul_eq_mul
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩
Mathlib.Data.Rat.Order.226_0.NTjR6KCugNscheB
protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den
Mathlib_Data_Rat_Order
a✝ b c a : β„š ⊒ Rat.Nonneg a ↔ 0 ≀ a
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by
rw [Rat.le_iff_Nonneg]
theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by
Mathlib.Data.Rat.Order.236_0.NTjR6KCugNscheB
theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a
Mathlib_Data_Rat_Order
a✝ b c a : β„š ⊒ Rat.Nonneg a ↔ Rat.Nonneg (a - 0)
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg]
show Rat.Nonneg a ↔ Rat.Nonneg (a - 0)
theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg]
Mathlib.Data.Rat.Order.236_0.NTjR6KCugNscheB
theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a
Mathlib_Data_Rat_Order
a✝ b c a : β„š ⊒ Rat.Nonneg a ↔ Rat.Nonneg (a - 0)
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0)
simp
theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0)
Mathlib.Data.Rat.Order.236_0.NTjR6KCugNscheB
theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a
Mathlib_Data_Rat_Order
a✝ b✝ c✝ a b c : β„š ⊒ c + a ≀ c + b ↔ a ≀ b
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by
rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg]
protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by
Mathlib.Data.Rat.Order.246_0.NTjR6KCugNscheB
protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b
Mathlib_Data_Rat_Order
a✝ b✝ c a b : β„š ha : 0 ≀ a hb : 0 ≀ b ⊒ 0 ≀ a * b
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by
rw [← nonneg_iff_zero_le] at ha hb ⊒
protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by
Mathlib.Data.Rat.Order.250_0.NTjR6KCugNscheB
protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b
Mathlib_Data_Rat_Order
a✝ b✝ c a b : β„š ha : Rat.Nonneg a hb : Rat.Nonneg b ⊒ Rat.Nonneg (a * b)
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒;
exact Rat.nonneg_mul ha hb
protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒;
Mathlib.Data.Rat.Order.250_0.NTjR6KCugNscheB
protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b
Mathlib_Data_Rat_Order
a b c : β„š src✝² : Field β„š := field src✝¹ : LinearOrder β„š := linearOrder src✝ : Semiring β„š := semiring ⊒ 0 ≀ 1
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by
decide
instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by
Mathlib.Data.Rat.Order.254_0.NTjR6KCugNscheB
instance : LinearOrderedField β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ LinearOrderedCommRing β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by
infer_instance
instance : LinearOrderedCommRing β„š := by
Mathlib.Data.Rat.Order.263_0.NTjR6KCugNscheB
instance : LinearOrderedCommRing β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ LinearOrderedRing β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by
infer_instance
instance : LinearOrderedRing β„š := by
Mathlib.Data.Rat.Order.265_0.NTjR6KCugNscheB
instance : LinearOrderedRing β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ OrderedRing β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by
infer_instance
instance : OrderedRing β„š := by
Mathlib.Data.Rat.Order.267_0.NTjR6KCugNscheB
instance : OrderedRing β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ LinearOrderedSemiring β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by
infer_instance
instance : LinearOrderedSemiring β„š := by
Mathlib.Data.Rat.Order.269_0.NTjR6KCugNscheB
instance : LinearOrderedSemiring β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ OrderedSemiring β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by
infer_instance
instance : OrderedSemiring β„š := by
Mathlib.Data.Rat.Order.271_0.NTjR6KCugNscheB
instance : OrderedSemiring β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ LinearOrderedAddCommGroup β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by
infer_instance
instance : LinearOrderedAddCommGroup β„š := by
Mathlib.Data.Rat.Order.273_0.NTjR6KCugNscheB
instance : LinearOrderedAddCommGroup β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ OrderedAddCommGroup β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by
infer_instance
instance : OrderedAddCommGroup β„š := by
Mathlib.Data.Rat.Order.275_0.NTjR6KCugNscheB
instance : OrderedAddCommGroup β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ OrderedCancelAddCommMonoid β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by
infer_instance
instance : OrderedCancelAddCommMonoid β„š := by
Mathlib.Data.Rat.Order.277_0.NTjR6KCugNscheB
instance : OrderedCancelAddCommMonoid β„š
Mathlib_Data_Rat_Order
a b c : β„š ⊒ OrderedAddCommMonoid β„š
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by
infer_instance
instance : OrderedAddCommMonoid β„š := by
Mathlib.Data.Rat.Order.279_0.NTjR6KCugNscheB
instance : OrderedAddCommMonoid β„š
Mathlib_Data_Rat_Order
a✝ b c a : β„š ⊒ a.num ≀ 0 ↔ a ≀ 0
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by
simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a)
theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by
Mathlib.Data.Rat.Order.281_0.NTjR6KCugNscheB
theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a
Mathlib_Data_Rat_Order
a✝ b c a : β„š ⊒ (-a).num = -a.num
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by
cases a
theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by
Mathlib.Data.Rat.Order.281_0.NTjR6KCugNscheB
theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a
Mathlib_Data_Rat_Order
case mk' a b c : β„š num✝ : β„€ den✝ : β„• den_nz✝ : den✝ β‰  0 reduced✝ : Nat.Coprime (Int.natAbs num✝) den✝ ⊒ (-mk' num✝ den✝).num = -(mk' num✝ den✝).num
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a;
rfl
theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a;
Mathlib.Data.Rat.Order.281_0.NTjR6KCugNscheB
theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a
Mathlib_Data_Rat_Order
a✝ b✝ c✝ : β„š a b c d : β„€ b_pos : 0 < b d_pos : 0 < d ⊒ ↑a / ↑b < ↑c / ↑d ↔ a * d < c * b
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by
simp only [lt_iff_le_not_le]
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by
Mathlib.Data.Rat.Order.286_0.NTjR6KCugNscheB
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b
Mathlib_Data_Rat_Order
a✝ b✝ c✝ : β„š a b c d : β„€ b_pos : 0 < b d_pos : 0 < d ⊒ ↑a / ↑b ≀ ↑c / ↑d ∧ ¬↑c / ↑d ≀ ↑a / ↑b ↔ a * d ≀ c * b ∧ Β¬c * b ≀ a * d
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le]
apply and_congr
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le]
Mathlib.Data.Rat.Order.286_0.NTjR6KCugNscheB
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b
Mathlib_Data_Rat_Order
case h₁ a✝ b✝ c✝ : β„š a b c d : β„€ b_pos : 0 < b d_pos : 0 < d ⊒ ↑a / ↑b ≀ ↑c / ↑d ↔ a * d ≀ c * b
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β·
simp [div_num_den, Rat.le_def b_pos d_pos]
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β·
Mathlib.Data.Rat.Order.286_0.NTjR6KCugNscheB
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b
Mathlib_Data_Rat_Order
case hβ‚‚ a✝ b✝ c✝ : β„š a b c d : β„€ b_pos : 0 < b d_pos : 0 < d ⊒ ¬↑c / ↑d ≀ ↑a / ↑b ↔ Β¬c * b ≀ a * d
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β·
apply not_congr
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β·
Mathlib.Data.Rat.Order.286_0.NTjR6KCugNscheB
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b
Mathlib_Data_Rat_Order
case hβ‚‚.h a✝ b✝ c✝ : β„š a b c d : β„€ b_pos : 0 < b d_pos : 0 < d ⊒ ↑c / ↑d ≀ ↑a / ↑b ↔ c * b ≀ a * d
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr
simp [div_num_den, Rat.le_def d_pos b_pos]
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr
Mathlib.Data.Rat.Order.286_0.NTjR6KCugNscheB
theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b
Mathlib_Data_Rat_Order
a b c q : β„š ⊒ q < 1 ↔ q.num < ↑q.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr simp [div_num_den, Rat.le_def d_pos b_pos] #align rat.div_lt_div_iff_mul_lt_mul Rat.div_lt_div_iff_mul_lt_mul theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den := by
simp [Rat.lt_def]
theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den := by
Mathlib.Data.Rat.Order.295_0.NTjR6KCugNscheB
theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den
Mathlib_Data_Rat_Order
a b c q : β„š ⊒ |q| = ↑(Int.natAbs q.num) /. ↑q.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr simp [div_num_den, Rat.le_def d_pos b_pos] #align rat.div_lt_div_iff_mul_lt_mul Rat.div_lt_div_iff_mul_lt_mul theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den := by simp [Rat.lt_def] #align rat.lt_one_iff_num_lt_denom Rat.lt_one_iff_num_lt_denom theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by
rcases le_total q 0 with hq | hq
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by
Mathlib.Data.Rat.Order.298_0.NTjR6KCugNscheB
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den
Mathlib_Data_Rat_Order
case inl a b c q : β„š hq : q ≀ 0 ⊒ |q| = ↑(Int.natAbs q.num) /. ↑q.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr simp [div_num_den, Rat.le_def d_pos b_pos] #align rat.div_lt_div_iff_mul_lt_mul Rat.div_lt_div_iff_mul_lt_mul theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den := by simp [Rat.lt_def] #align rat.lt_one_iff_num_lt_denom Rat.lt_one_iff_num_lt_denom theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β·
rw [abs_of_nonpos hq]
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β·
Mathlib.Data.Rat.Order.298_0.NTjR6KCugNscheB
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den
Mathlib_Data_Rat_Order
case inl a b c q : β„š hq : q ≀ 0 ⊒ -q = ↑(Int.natAbs q.num) /. ↑q.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr simp [div_num_den, Rat.le_def d_pos b_pos] #align rat.div_lt_div_iff_mul_lt_mul Rat.div_lt_div_iff_mul_lt_mul theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den := by simp [Rat.lt_def] #align rat.lt_one_iff_num_lt_denom Rat.lt_one_iff_num_lt_denom theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq]
rw [← @num_den q, ← divInt_zero_one, Rat.le_def (Int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq]
Mathlib.Data.Rat.Order.298_0.NTjR6KCugNscheB
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den
Mathlib_Data_Rat_Order
case inl a b c q : β„š hq : q.num ≀ 0 ⊒ -q = ↑(Int.natAbs q.num) /. ↑q.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr simp [div_num_den, Rat.le_def d_pos b_pos] #align rat.div_lt_div_iff_mul_lt_mul Rat.div_lt_div_iff_mul_lt_mul theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den := by simp [Rat.lt_def] #align rat.lt_one_iff_num_lt_denom Rat.lt_one_iff_num_lt_denom theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def (Int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq
rw [Int.ofNat_natAbs_of_nonpos hq, ← neg_def, num_den]
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def (Int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq
Mathlib.Data.Rat.Order.298_0.NTjR6KCugNscheB
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den
Mathlib_Data_Rat_Order
case inr a b c q : β„š hq : 0 ≀ q ⊒ |q| = ↑(Int.natAbs q.num) /. ↑q.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr simp [div_num_den, Rat.le_def d_pos b_pos] #align rat.div_lt_div_iff_mul_lt_mul Rat.div_lt_div_iff_mul_lt_mul theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den := by simp [Rat.lt_def] #align rat.lt_one_iff_num_lt_denom Rat.lt_one_iff_num_lt_denom theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def (Int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq rw [Int.ofNat_natAbs_of_nonpos hq, ← neg_def, num_den] Β·
rw [abs_of_nonneg hq]
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def (Int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq rw [Int.ofNat_natAbs_of_nonpos hq, ← neg_def, num_den] Β·
Mathlib.Data.Rat.Order.298_0.NTjR6KCugNscheB
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den
Mathlib_Data_Rat_Order
case inr a b c q : β„š hq : 0 ≀ q ⊒ q = ↑(Int.natAbs q.num) /. ↑q.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr simp [div_num_den, Rat.le_def d_pos b_pos] #align rat.div_lt_div_iff_mul_lt_mul Rat.div_lt_div_iff_mul_lt_mul theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den := by simp [Rat.lt_def] #align rat.lt_one_iff_num_lt_denom Rat.lt_one_iff_num_lt_denom theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def (Int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq rw [Int.ofNat_natAbs_of_nonpos hq, ← neg_def, num_den] Β· rw [abs_of_nonneg hq]
rw [← @num_den q, ← divInt_zero_one, Rat.le_def zero_lt_one (Int.coe_nat_pos.2 q.pos), mul_one, zero_mul] at hq
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def (Int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq rw [Int.ofNat_natAbs_of_nonpos hq, ← neg_def, num_den] Β· rw [abs_of_nonneg hq]
Mathlib.Data.Rat.Order.298_0.NTjR6KCugNscheB
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den
Mathlib_Data_Rat_Order
case inr a b c q : β„š hq : 0 ≀ q.num ⊒ q = ↑(Int.natAbs q.num) /. ↑q.den
/- Copyright (c) 2019 Johannes HΓΆlzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes HΓΆlzl, Mario Carneiro -/ import Mathlib.Algebra.Order.Field.Defs import Mathlib.Data.Rat.Basic import Mathlib.Data.Int.Cast.Lemmas #align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e" /-! # Order for Rational Numbers ## Summary We define the order on `β„š`, prove that `β„š` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Tags rat, rationals, field, β„š, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace Rat variable (a b c : β„š) open Rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def Nonneg (r : β„š) : Prop := 0 ≀ r.num #align rat.nonneg Rat.Nonneg @[simp] theorem divInt_nonneg (a : β„€) {b : β„€} (h : 0 < b) : (a /. b).Nonneg ↔ 0 ≀ a := by generalize ha : a /. b = x; cases' x with n₁ d₁ h₁ c₁; rw [num_den'] at ha simp only [Rat.Nonneg] have d0 := Int.ofNat_lt.2 (Nat.pos_of_ne_zero h₁) have := (divInt_eq_iff (ne_of_gt h) (ne_of_gt d0)).1 ha constructor <;> intro hβ‚‚ Β· apply nonneg_of_mul_nonneg_left _ d0 rw [this] exact mul_nonneg hβ‚‚ (le_of_lt h) Β· apply nonneg_of_mul_nonneg_left _ h rw [← this] exact mul_nonneg hβ‚‚ (Int.ofNat_zero_le _) #align rat.mk_nonneg Rat.divInt_nonneg protected theorem nonneg_add {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a + b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) simp only [d₁0, dβ‚‚0, h₁, hβ‚‚, mul_pos, divInt_nonneg, add_def'', Ne.def, Nat.cast_eq_zero, not_false_iff] intro n₁0 nβ‚‚0 apply add_nonneg <;> apply mul_nonneg <;> Β· first |assumption|apply Int.ofNat_zero_le #align rat.nonneg_add Rat.nonneg_add protected theorem nonneg_mul {a b} : Rat.Nonneg a β†’ Rat.Nonneg b β†’ Rat.Nonneg (a * b) := numDenCasesOn' a fun n₁ d₁ h₁ => numDenCasesOn' b fun nβ‚‚ dβ‚‚ hβ‚‚ => by have d₁0 : 0 < (d₁ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h₁) have dβ‚‚0 : 0 < (dβ‚‚ : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero hβ‚‚) rw [mul_def' d₁0.ne.symm dβ‚‚0.ne.symm, divInt_nonneg _ d₁0, divInt_nonneg _ dβ‚‚0, divInt_nonneg _ (mul_pos d₁0 dβ‚‚0)] apply mul_nonneg #align rat.nonneg_mul Rat.nonneg_mul protected theorem nonneg_antisymm {a} : Rat.Nonneg a β†’ Rat.Nonneg (-a) β†’ a = 0 := numDenCasesOn' a fun n d h => by have d0 : 0 < (d : β„€) := Int.coe_nat_pos.2 (Nat.pos_of_ne_zero h) rw [divInt_nonneg _ d0, neg_def, divInt_nonneg _ d0, Right.nonneg_neg_iff, divInt_eq_zero d0.ne.symm] exact fun h₁ hβ‚‚ => le_antisymm hβ‚‚ h₁ #align rat.nonneg_antisymm Rat.nonneg_antisymm protected theorem nonneg_total : Rat.Nonneg a ∨ Rat.Nonneg (-a) := by cases' a with n; exact Or.imp_right neg_nonneg_of_nonpos (le_total 0 n) #align rat.nonneg_total Rat.nonneg_total instance decidableNonneg : Decidable (Rat.Nonneg a) := by cases a; unfold Rat.Nonneg; infer_instance #align rat.decidable_nonneg Rat.decidableNonneg -- Porting note: Now `Std` defines `≀` on `Rat`. -- This is the old mathlib3 definition. /-- Relation `a ≀ b` on `β„š` defined as `a ≀ b ↔ Rat.Nonneg (b - a)`. Use `a ≀ b` instead of `Rat.le a b`. -/ protected def le' (a b : β„š) := Rat.Nonneg (b - a) #align rat.le Rat.le' /-- Define a (dependent) function or prove `βˆ€ r : β„š, p r` by dealing with rational numbers of the form `mk' n d` with `d β‰  0`. -/ -- Porting note: TODO move @[elab_as_elim] def numDenCasesOn''.{u} {C : β„š β†’ Sort u} (a : β„š) (H : βˆ€ (n : β„€) (d : β„•) (nz red), C (mk' n d nz red)) : C a := numDenCasesOn a fun n d h h' => by rw [← mk_eq_divInt _ _ h.ne' h'] exact H n d h.ne' _ -- Porting note: TODO can this be shortened? protected theorem le_iff_Nonneg (a b : β„š) : a ≀ b ↔ Rat.Nonneg (b - a) := numDenCasesOn'' a fun na da ha hared => numDenCasesOn'' b fun nb db hb hbred => by change Rat.blt _ _ = false ↔ _ unfold Rat.blt simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.ite_eq_false_distrib, decide_eq_false_iff_not, not_lt, ite_eq_left_iff, not_and, not_le] split_ifs with h h' Β· rw [Rat.sub_def] simp only [Rat.Nonneg, false_iff, not_le] simp only [normalize_eq] apply Int.ediv_neg' Β· rw [sub_neg] apply lt_of_lt_of_le Β· apply mul_neg_of_neg_of_pos h.1 rwa [Nat.cast_pos, pos_iff_ne_zero] Β· apply mul_nonneg h.2 (Nat.cast_nonneg _) Β· simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] Β· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h' simp [h', Rat.Nonneg] Β· simp [Rat.Nonneg, Rat.sub_def, normalize_eq] refine ⟨fun H => ?_, fun H _ => ?_⟩ Β· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _) rw [sub_nonneg] push_neg at h obtain hb|hb := Ne.lt_or_lt h' Β· apply H intro H' exact (hb.trans H').false.elim Β· obtain ha|ha := le_or_lt na 0 Β· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _) exact mul_nonneg hb.le (Nat.cast_nonneg _) Β· exact H (fun _ => ha) Β· rw [← sub_nonneg] contrapose! H apply Int.ediv_neg' H simp only [Nat.cast_pos] apply Nat.gcd_pos_of_pos_right apply mul_pos <;> rwa [pos_iff_ne_zero] protected theorem le_def {a b c d : β„€} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≀ c /. d ↔ a * d ≀ c * b := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg _ ↔ _ rw [← sub_nonneg] simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] #align rat.le_def Rat.le_def protected theorem le_refl : a ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg (a - a) rw [sub_self] exact le_refl (0 : β„€) #align rat.le_refl Rat.le_refl protected theorem le_total : a ≀ b ∨ b ≀ a := by have := Rat.nonneg_total (b - a) rw [Rat.le_iff_Nonneg, Rat.le_iff_Nonneg] rwa [neg_sub] at this #align rat.le_total Rat.le_total protected theorem le_antisymm {a b : β„š} (hab : a ≀ b) (hba : b ≀ a) : a = b := by rw [Rat.le_iff_Nonneg] at hab hba rw [sub_eq_add_neg] at hba rw [← neg_sub, sub_eq_add_neg] at hab have := eq_neg_of_add_eq_zero_left (Rat.nonneg_antisymm hba hab) rwa [neg_neg] at this #align rat.le_antisymm Rat.le_antisymm protected theorem le_trans {a b c : β„š} (hab : a ≀ b) (hbc : b ≀ c) : a ≀ c := by rw [Rat.le_iff_Nonneg] at hab hbc have : Rat.Nonneg (b - a + (c - b)) := Rat.nonneg_add hab hbc simp_rw [sub_eq_add_neg, add_left_comm (b + -a) c (-b), add_comm (b + -a) (-b), add_left_comm (-b) b (-a), add_comm (-b) (-a), add_neg_cancel_comm_assoc, ← sub_eq_add_neg] at this rw [Rat.le_iff_Nonneg] exact this #align rat.le_trans Rat.le_trans protected theorem not_le {a b : β„š} : Β¬a ≀ b ↔ b < a := (Bool.not_eq_false _).to_iff instance linearOrder : LinearOrder β„š where le_refl := Rat.le_refl le_trans := @Rat.le_trans le_antisymm := @Rat.le_antisymm le_total := Rat.le_total decidableLE _ _ := by infer_instance lt_iff_le_not_le _ _ := by rw [← Rat.not_le, and_iff_right_of_imp (Rat.le_total _ _).resolve_left] -- Extra instances to short-circuit type class resolution instance : LT β„š := by infer_instance instance : DistribLattice β„š := by infer_instance instance : Lattice β„š := by infer_instance instance : SemilatticeInf β„š := by infer_instance instance : SemilatticeSup β„š := by infer_instance instance : Inf β„š := by infer_instance instance : Sup β„š := by infer_instance instance : PartialOrder β„š := by infer_instance instance : Preorder β„š := by infer_instance protected theorem le_def' {p q : β„š} : p ≀ q ↔ p.num * q.den ≀ q.num * p.den := by rw [← @num_den q, ← @num_den p] conv_rhs => simp only [num_den] exact Rat.le_def (mod_cast p.pos) (mod_cast q.pos) #align rat.le_def' Rat.le_def' protected theorem lt_def {p q : β„š} : p < q ↔ p.num * q.den < q.num * p.den := by rw [lt_iff_le_and_ne, Rat.le_def'] suffices p β‰  q ↔ p.num * q.den β‰  q.num * p.den by constructor <;> intro h Β· exact lt_iff_le_and_ne.mpr ⟨h.left, this.mp h.right⟩ Β· have tmp := lt_iff_le_and_ne.mp h exact ⟨tmp.left, this.mpr tmp.right⟩ exact not_iff_not.mpr eq_iff_mul_eq_mul #align rat.lt_def Rat.lt_def theorem nonneg_iff_zero_le {a} : Rat.Nonneg a ↔ 0 ≀ a := by rw [Rat.le_iff_Nonneg] show Rat.Nonneg a ↔ Rat.Nonneg (a - 0) simp #align rat.nonneg_iff_zero_le Rat.nonneg_iff_zero_le theorem num_nonneg_iff_zero_le : βˆ€ {a : β„š}, 0 ≀ a.num ↔ 0 ≀ a | ⟨n, d, h, c⟩ => @nonneg_iff_zero_le ⟨n, d, h, c⟩ #align rat.num_nonneg_iff_zero_le Rat.num_nonneg_iff_zero_le protected theorem add_le_add_left {a b c : β„š} : c + a ≀ c + b ↔ a ≀ b := by rw [Rat.le_iff_Nonneg, add_sub_add_left_eq_sub, Rat.le_iff_Nonneg] #align rat.add_le_add_left Rat.add_le_add_left protected theorem mul_nonneg {a b : β„š} (ha : 0 ≀ a) (hb : 0 ≀ b) : 0 ≀ a * b := by rw [← nonneg_iff_zero_le] at ha hb ⊒; exact Rat.nonneg_mul ha hb #align rat.mul_nonneg Rat.mul_nonneg instance : LinearOrderedField β„š := { Rat.field, Rat.linearOrder, Rat.semiring with zero_le_one := by decide add_le_add_left := fun a b ab c => Rat.add_le_add_left.2 ab mul_pos := fun a b ha hb => lt_of_le_of_ne (Rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm } -- Extra instances to short-circuit type class resolution instance : LinearOrderedCommRing β„š := by infer_instance instance : LinearOrderedRing β„š := by infer_instance instance : OrderedRing β„š := by infer_instance instance : LinearOrderedSemiring β„š := by infer_instance instance : OrderedSemiring β„š := by infer_instance instance : LinearOrderedAddCommGroup β„š := by infer_instance instance : OrderedAddCommGroup β„š := by infer_instance instance : OrderedCancelAddCommMonoid β„š := by infer_instance instance : OrderedAddCommMonoid β„š := by infer_instance theorem num_pos_iff_pos {a : β„š} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le <| by simpa [(by cases a; rfl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) #align rat.num_pos_iff_pos Rat.num_pos_iff_pos theorem div_lt_div_iff_mul_lt_mul {a b c d : β„€} (b_pos : 0 < b) (d_pos : 0 < d) : (a : β„š) / b < c / d ↔ a * d < c * b := by simp only [lt_iff_le_not_le] apply and_congr Β· simp [div_num_den, Rat.le_def b_pos d_pos] Β· apply not_congr simp [div_num_den, Rat.le_def d_pos b_pos] #align rat.div_lt_div_iff_mul_lt_mul Rat.div_lt_div_iff_mul_lt_mul theorem lt_one_iff_num_lt_denom {q : β„š} : q < 1 ↔ q.num < q.den := by simp [Rat.lt_def] #align rat.lt_one_iff_num_lt_denom Rat.lt_one_iff_num_lt_denom theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def (Int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq rw [Int.ofNat_natAbs_of_nonpos hq, ← neg_def, num_den] Β· rw [abs_of_nonneg hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def zero_lt_one (Int.coe_nat_pos.2 q.pos), mul_one, zero_mul] at hq
rw [Int.natAbs_of_nonneg hq, num_den]
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den := by rcases le_total q 0 with hq | hq Β· rw [abs_of_nonpos hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def (Int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq rw [Int.ofNat_natAbs_of_nonpos hq, ← neg_def, num_den] Β· rw [abs_of_nonneg hq] rw [← @num_den q, ← divInt_zero_one, Rat.le_def zero_lt_one (Int.coe_nat_pos.2 q.pos), mul_one, zero_mul] at hq
Mathlib.Data.Rat.Order.298_0.NTjR6KCugNscheB
theorem abs_def (q : β„š) : |q| = q.num.natAbs /. q.den
Mathlib_Data_Rat_Order
C : Type u_1 inst✝³ : Category.{u_2, u_1} C inst✝² : HasZeroMorphisms C inst✝¹ : HasZeroObject C X : C inst✝ : ArtinianObject X h : Β¬IsZero X ⊒ βˆƒ Y, Simple (underlying.obj Y)
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.CategoryTheory.Subobject.Lattice import Mathlib.CategoryTheory.EssentiallySmall import Mathlib.CategoryTheory.Simple #align_import category_theory.noetherian from "leanprover-community/mathlib"@"7c4c90f422a4a4477e4d8bc4dc9f1e634e6b2349" /-! # Artinian and noetherian categories An artinian category is a category in which objects do not have infinite decreasing sequences of subobjects. A noetherian category is a category in which objects do not have infinite increasing sequences of subobjects. We show that any nonzero artinian object has a simple subobject. ## Future work The Jordan-HΓΆlder theorem, following https://stacks.math.columbia.edu/tag/0FCK. -/ namespace CategoryTheory open CategoryTheory.Limits variable {C : Type*} [Category C] /-- A noetherian object is an object which does not have infinite increasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCG -/ class NoetherianObject (X : C) : Prop where subobject_gt_wellFounded' : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.noetherian_object CategoryTheory.NoetherianObject lemma NoetherianObject.subobject_gt_wellFounded (X : C) [NoetherianObject X] : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) := NoetherianObject.subobject_gt_wellFounded' /-- An artinian object is an object which does not have infinite decreasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCF -/ class ArtinianObject (X : C) : Prop where subobject_lt_wellFounded' : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.artinian_object CategoryTheory.ArtinianObject lemma ArtinianObject.subobject_lt_wellFounded (X : C) [ArtinianObject X] : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) := ArtinianObject.subobject_lt_wellFounded' variable (C) /-- A category is noetherian if it is essentially small and all objects are noetherian. -/ class Noetherian extends EssentiallySmall C : Prop where noetherianObject : βˆ€ X : C, NoetherianObject X #align category_theory.noetherian CategoryTheory.Noetherian attribute [instance] Noetherian.noetherianObject /-- A category is artinian if it is essentially small and all objects are artinian. -/ class Artinian extends EssentiallySmall C : Prop where artinianObject : βˆ€ X : C, ArtinianObject X #align category_theory.artinian CategoryTheory.Artinian attribute [instance] Artinian.artinianObject variable {C} open Subobject variable [HasZeroMorphisms C] [HasZeroObject C] theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by
haveI : Nontrivial (Subobject X) := nontrivial_of_not_isZero h
theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by
Mathlib.CategoryTheory.Noetherian.82_0.bQzOkYhrFw250Z9
theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C)
Mathlib_CategoryTheory_Noetherian
C : Type u_1 inst✝³ : Category.{u_2, u_1} C inst✝² : HasZeroMorphisms C inst✝¹ : HasZeroObject C X : C inst✝ : ArtinianObject X h : Β¬IsZero X this : Nontrivial (Subobject X) ⊒ βˆƒ Y, Simple (underlying.obj Y)
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.CategoryTheory.Subobject.Lattice import Mathlib.CategoryTheory.EssentiallySmall import Mathlib.CategoryTheory.Simple #align_import category_theory.noetherian from "leanprover-community/mathlib"@"7c4c90f422a4a4477e4d8bc4dc9f1e634e6b2349" /-! # Artinian and noetherian categories An artinian category is a category in which objects do not have infinite decreasing sequences of subobjects. A noetherian category is a category in which objects do not have infinite increasing sequences of subobjects. We show that any nonzero artinian object has a simple subobject. ## Future work The Jordan-HΓΆlder theorem, following https://stacks.math.columbia.edu/tag/0FCK. -/ namespace CategoryTheory open CategoryTheory.Limits variable {C : Type*} [Category C] /-- A noetherian object is an object which does not have infinite increasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCG -/ class NoetherianObject (X : C) : Prop where subobject_gt_wellFounded' : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.noetherian_object CategoryTheory.NoetherianObject lemma NoetherianObject.subobject_gt_wellFounded (X : C) [NoetherianObject X] : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) := NoetherianObject.subobject_gt_wellFounded' /-- An artinian object is an object which does not have infinite decreasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCF -/ class ArtinianObject (X : C) : Prop where subobject_lt_wellFounded' : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.artinian_object CategoryTheory.ArtinianObject lemma ArtinianObject.subobject_lt_wellFounded (X : C) [ArtinianObject X] : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) := ArtinianObject.subobject_lt_wellFounded' variable (C) /-- A category is noetherian if it is essentially small and all objects are noetherian. -/ class Noetherian extends EssentiallySmall C : Prop where noetherianObject : βˆ€ X : C, NoetherianObject X #align category_theory.noetherian CategoryTheory.Noetherian attribute [instance] Noetherian.noetherianObject /-- A category is artinian if it is essentially small and all objects are artinian. -/ class Artinian extends EssentiallySmall C : Prop where artinianObject : βˆ€ X : C, ArtinianObject X #align category_theory.artinian CategoryTheory.Artinian attribute [instance] Artinian.artinianObject variable {C} open Subobject variable [HasZeroMorphisms C] [HasZeroObject C] theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by haveI : Nontrivial (Subobject X) := nontrivial_of_not_isZero h
haveI := isAtomic_of_orderBot_wellFounded_lt (ArtinianObject.subobject_lt_wellFounded X)
theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by haveI : Nontrivial (Subobject X) := nontrivial_of_not_isZero h
Mathlib.CategoryTheory.Noetherian.82_0.bQzOkYhrFw250Z9
theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C)
Mathlib_CategoryTheory_Noetherian
C : Type u_1 inst✝³ : Category.{u_2, u_1} C inst✝² : HasZeroMorphisms C inst✝¹ : HasZeroObject C X : C inst✝ : ArtinianObject X h : Β¬IsZero X this✝ : Nontrivial (Subobject X) this : IsAtomic (Subobject X) ⊒ βˆƒ Y, Simple (underlying.obj Y)
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.CategoryTheory.Subobject.Lattice import Mathlib.CategoryTheory.EssentiallySmall import Mathlib.CategoryTheory.Simple #align_import category_theory.noetherian from "leanprover-community/mathlib"@"7c4c90f422a4a4477e4d8bc4dc9f1e634e6b2349" /-! # Artinian and noetherian categories An artinian category is a category in which objects do not have infinite decreasing sequences of subobjects. A noetherian category is a category in which objects do not have infinite increasing sequences of subobjects. We show that any nonzero artinian object has a simple subobject. ## Future work The Jordan-HΓΆlder theorem, following https://stacks.math.columbia.edu/tag/0FCK. -/ namespace CategoryTheory open CategoryTheory.Limits variable {C : Type*} [Category C] /-- A noetherian object is an object which does not have infinite increasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCG -/ class NoetherianObject (X : C) : Prop where subobject_gt_wellFounded' : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.noetherian_object CategoryTheory.NoetherianObject lemma NoetherianObject.subobject_gt_wellFounded (X : C) [NoetherianObject X] : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) := NoetherianObject.subobject_gt_wellFounded' /-- An artinian object is an object which does not have infinite decreasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCF -/ class ArtinianObject (X : C) : Prop where subobject_lt_wellFounded' : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.artinian_object CategoryTheory.ArtinianObject lemma ArtinianObject.subobject_lt_wellFounded (X : C) [ArtinianObject X] : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) := ArtinianObject.subobject_lt_wellFounded' variable (C) /-- A category is noetherian if it is essentially small and all objects are noetherian. -/ class Noetherian extends EssentiallySmall C : Prop where noetherianObject : βˆ€ X : C, NoetherianObject X #align category_theory.noetherian CategoryTheory.Noetherian attribute [instance] Noetherian.noetherianObject /-- A category is artinian if it is essentially small and all objects are artinian. -/ class Artinian extends EssentiallySmall C : Prop where artinianObject : βˆ€ X : C, ArtinianObject X #align category_theory.artinian CategoryTheory.Artinian attribute [instance] Artinian.artinianObject variable {C} open Subobject variable [HasZeroMorphisms C] [HasZeroObject C] theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by haveI : Nontrivial (Subobject X) := nontrivial_of_not_isZero h haveI := isAtomic_of_orderBot_wellFounded_lt (ArtinianObject.subobject_lt_wellFounded X)
obtain ⟨Y, s⟩ := (IsAtomic.eq_bot_or_exists_atom_le (⊀ : Subobject X)).resolve_left top_ne_bot
theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by haveI : Nontrivial (Subobject X) := nontrivial_of_not_isZero h haveI := isAtomic_of_orderBot_wellFounded_lt (ArtinianObject.subobject_lt_wellFounded X)
Mathlib.CategoryTheory.Noetherian.82_0.bQzOkYhrFw250Z9
theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C)
Mathlib_CategoryTheory_Noetherian
case intro C : Type u_1 inst✝³ : Category.{u_2, u_1} C inst✝² : HasZeroMorphisms C inst✝¹ : HasZeroObject C X : C inst✝ : ArtinianObject X h : Β¬IsZero X this✝ : Nontrivial (Subobject X) this : IsAtomic (Subobject X) Y : Subobject X s : IsAtom Y ∧ Y ≀ ⊀ ⊒ βˆƒ Y, Simple (underlying.obj Y)
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.CategoryTheory.Subobject.Lattice import Mathlib.CategoryTheory.EssentiallySmall import Mathlib.CategoryTheory.Simple #align_import category_theory.noetherian from "leanprover-community/mathlib"@"7c4c90f422a4a4477e4d8bc4dc9f1e634e6b2349" /-! # Artinian and noetherian categories An artinian category is a category in which objects do not have infinite decreasing sequences of subobjects. A noetherian category is a category in which objects do not have infinite increasing sequences of subobjects. We show that any nonzero artinian object has a simple subobject. ## Future work The Jordan-HΓΆlder theorem, following https://stacks.math.columbia.edu/tag/0FCK. -/ namespace CategoryTheory open CategoryTheory.Limits variable {C : Type*} [Category C] /-- A noetherian object is an object which does not have infinite increasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCG -/ class NoetherianObject (X : C) : Prop where subobject_gt_wellFounded' : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.noetherian_object CategoryTheory.NoetherianObject lemma NoetherianObject.subobject_gt_wellFounded (X : C) [NoetherianObject X] : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) := NoetherianObject.subobject_gt_wellFounded' /-- An artinian object is an object which does not have infinite decreasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCF -/ class ArtinianObject (X : C) : Prop where subobject_lt_wellFounded' : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.artinian_object CategoryTheory.ArtinianObject lemma ArtinianObject.subobject_lt_wellFounded (X : C) [ArtinianObject X] : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) := ArtinianObject.subobject_lt_wellFounded' variable (C) /-- A category is noetherian if it is essentially small and all objects are noetherian. -/ class Noetherian extends EssentiallySmall C : Prop where noetherianObject : βˆ€ X : C, NoetherianObject X #align category_theory.noetherian CategoryTheory.Noetherian attribute [instance] Noetherian.noetherianObject /-- A category is artinian if it is essentially small and all objects are artinian. -/ class Artinian extends EssentiallySmall C : Prop where artinianObject : βˆ€ X : C, ArtinianObject X #align category_theory.artinian CategoryTheory.Artinian attribute [instance] Artinian.artinianObject variable {C} open Subobject variable [HasZeroMorphisms C] [HasZeroObject C] theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by haveI : Nontrivial (Subobject X) := nontrivial_of_not_isZero h haveI := isAtomic_of_orderBot_wellFounded_lt (ArtinianObject.subobject_lt_wellFounded X) obtain ⟨Y, s⟩ := (IsAtomic.eq_bot_or_exists_atom_le (⊀ : Subobject X)).resolve_left top_ne_bot
exact ⟨Y, (subobject_simple_iff_isAtom _).mpr s.1⟩
theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by haveI : Nontrivial (Subobject X) := nontrivial_of_not_isZero h haveI := isAtomic_of_orderBot_wellFounded_lt (ArtinianObject.subobject_lt_wellFounded X) obtain ⟨Y, s⟩ := (IsAtomic.eq_bot_or_exists_atom_le (⊀ : Subobject X)).resolve_left top_ne_bot
Mathlib.CategoryTheory.Noetherian.82_0.bQzOkYhrFw250Z9
theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C)
Mathlib_CategoryTheory_Noetherian
C : Type u_1 inst✝³ : Category.{?u.7072, u_1} C inst✝² : HasZeroMorphisms C inst✝¹ : HasZeroObject C X : C inst✝ : ArtinianObject X h : ¬IsZero X ⊒ Mono (simpleSubobjectArrow h)
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.CategoryTheory.Subobject.Lattice import Mathlib.CategoryTheory.EssentiallySmall import Mathlib.CategoryTheory.Simple #align_import category_theory.noetherian from "leanprover-community/mathlib"@"7c4c90f422a4a4477e4d8bc4dc9f1e634e6b2349" /-! # Artinian and noetherian categories An artinian category is a category in which objects do not have infinite decreasing sequences of subobjects. A noetherian category is a category in which objects do not have infinite increasing sequences of subobjects. We show that any nonzero artinian object has a simple subobject. ## Future work The Jordan-HΓΆlder theorem, following https://stacks.math.columbia.edu/tag/0FCK. -/ namespace CategoryTheory open CategoryTheory.Limits variable {C : Type*} [Category C] /-- A noetherian object is an object which does not have infinite increasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCG -/ class NoetherianObject (X : C) : Prop where subobject_gt_wellFounded' : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.noetherian_object CategoryTheory.NoetherianObject lemma NoetherianObject.subobject_gt_wellFounded (X : C) [NoetherianObject X] : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) := NoetherianObject.subobject_gt_wellFounded' /-- An artinian object is an object which does not have infinite decreasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCF -/ class ArtinianObject (X : C) : Prop where subobject_lt_wellFounded' : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.artinian_object CategoryTheory.ArtinianObject lemma ArtinianObject.subobject_lt_wellFounded (X : C) [ArtinianObject X] : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) := ArtinianObject.subobject_lt_wellFounded' variable (C) /-- A category is noetherian if it is essentially small and all objects are noetherian. -/ class Noetherian extends EssentiallySmall C : Prop where noetherianObject : βˆ€ X : C, NoetherianObject X #align category_theory.noetherian CategoryTheory.Noetherian attribute [instance] Noetherian.noetherianObject /-- A category is artinian if it is essentially small and all objects are artinian. -/ class Artinian extends EssentiallySmall C : Prop where artinianObject : βˆ€ X : C, ArtinianObject X #align category_theory.artinian CategoryTheory.Artinian attribute [instance] Artinian.artinianObject variable {C} open Subobject variable [HasZeroMorphisms C] [HasZeroObject C] theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by haveI : Nontrivial (Subobject X) := nontrivial_of_not_isZero h haveI := isAtomic_of_orderBot_wellFounded_lt (ArtinianObject.subobject_lt_wellFounded X) obtain ⟨Y, s⟩ := (IsAtomic.eq_bot_or_exists_atom_le (⊀ : Subobject X)).resolve_left top_ne_bot exact ⟨Y, (subobject_simple_iff_isAtom _).mpr s.1⟩ #align category_theory.exists_simple_subobject CategoryTheory.exists_simple_subobject /-- Choose an arbitrary simple subobject of a non-zero artinian object. -/ noncomputable def simpleSubobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : C := (exists_simple_subobject h).choose #align category_theory.simple_subobject CategoryTheory.simpleSubobject /-- The monomorphism from the arbitrary simple subobject of a non-zero artinian object. -/ noncomputable def simpleSubobjectArrow {X : C} [ArtinianObject X] (h : Β¬IsZero X) : simpleSubobject h ⟢ X := (exists_simple_subobject h).choose.arrow #align category_theory.simple_subobject_arrow CategoryTheory.simpleSubobjectArrow instance mono_simpleSubobjectArrow {X : C} [ArtinianObject X] (h : Β¬IsZero X) : Mono (simpleSubobjectArrow h) := by
dsimp only [simpleSubobjectArrow]
instance mono_simpleSubobjectArrow {X : C} [ArtinianObject X] (h : Β¬IsZero X) : Mono (simpleSubobjectArrow h) := by
Mathlib.CategoryTheory.Noetherian.101_0.bQzOkYhrFw250Z9
instance mono_simpleSubobjectArrow {X : C} [ArtinianObject X] (h : Β¬IsZero X) : Mono (simpleSubobjectArrow h)
Mathlib_CategoryTheory_Noetherian
C : Type u_1 inst✝³ : Category.{?u.7072, u_1} C inst✝² : HasZeroMorphisms C inst✝¹ : HasZeroObject C X : C inst✝ : ArtinianObject X h : Β¬IsZero X ⊒ Mono (arrow (Exists.choose (_ : βˆƒ Y, Simple (underlying.obj Y))))
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.CategoryTheory.Subobject.Lattice import Mathlib.CategoryTheory.EssentiallySmall import Mathlib.CategoryTheory.Simple #align_import category_theory.noetherian from "leanprover-community/mathlib"@"7c4c90f422a4a4477e4d8bc4dc9f1e634e6b2349" /-! # Artinian and noetherian categories An artinian category is a category in which objects do not have infinite decreasing sequences of subobjects. A noetherian category is a category in which objects do not have infinite increasing sequences of subobjects. We show that any nonzero artinian object has a simple subobject. ## Future work The Jordan-HΓΆlder theorem, following https://stacks.math.columbia.edu/tag/0FCK. -/ namespace CategoryTheory open CategoryTheory.Limits variable {C : Type*} [Category C] /-- A noetherian object is an object which does not have infinite increasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCG -/ class NoetherianObject (X : C) : Prop where subobject_gt_wellFounded' : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.noetherian_object CategoryTheory.NoetherianObject lemma NoetherianObject.subobject_gt_wellFounded (X : C) [NoetherianObject X] : WellFounded ((Β· > Β·) : Subobject X β†’ Subobject X β†’ Prop) := NoetherianObject.subobject_gt_wellFounded' /-- An artinian object is an object which does not have infinite decreasing sequences of subobjects. See https://stacks.math.columbia.edu/tag/0FCF -/ class ArtinianObject (X : C) : Prop where subobject_lt_wellFounded' : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) #align category_theory.artinian_object CategoryTheory.ArtinianObject lemma ArtinianObject.subobject_lt_wellFounded (X : C) [ArtinianObject X] : WellFounded ((Β· < Β·) : Subobject X β†’ Subobject X β†’ Prop) := ArtinianObject.subobject_lt_wellFounded' variable (C) /-- A category is noetherian if it is essentially small and all objects are noetherian. -/ class Noetherian extends EssentiallySmall C : Prop where noetherianObject : βˆ€ X : C, NoetherianObject X #align category_theory.noetherian CategoryTheory.Noetherian attribute [instance] Noetherian.noetherianObject /-- A category is artinian if it is essentially small and all objects are artinian. -/ class Artinian extends EssentiallySmall C : Prop where artinianObject : βˆ€ X : C, ArtinianObject X #align category_theory.artinian CategoryTheory.Artinian attribute [instance] Artinian.artinianObject variable {C} open Subobject variable [HasZeroMorphisms C] [HasZeroObject C] theorem exists_simple_subobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : βˆƒ Y : Subobject X, Simple (Y : C) := by haveI : Nontrivial (Subobject X) := nontrivial_of_not_isZero h haveI := isAtomic_of_orderBot_wellFounded_lt (ArtinianObject.subobject_lt_wellFounded X) obtain ⟨Y, s⟩ := (IsAtomic.eq_bot_or_exists_atom_le (⊀ : Subobject X)).resolve_left top_ne_bot exact ⟨Y, (subobject_simple_iff_isAtom _).mpr s.1⟩ #align category_theory.exists_simple_subobject CategoryTheory.exists_simple_subobject /-- Choose an arbitrary simple subobject of a non-zero artinian object. -/ noncomputable def simpleSubobject {X : C} [ArtinianObject X] (h : Β¬IsZero X) : C := (exists_simple_subobject h).choose #align category_theory.simple_subobject CategoryTheory.simpleSubobject /-- The monomorphism from the arbitrary simple subobject of a non-zero artinian object. -/ noncomputable def simpleSubobjectArrow {X : C} [ArtinianObject X] (h : Β¬IsZero X) : simpleSubobject h ⟢ X := (exists_simple_subobject h).choose.arrow #align category_theory.simple_subobject_arrow CategoryTheory.simpleSubobjectArrow instance mono_simpleSubobjectArrow {X : C} [ArtinianObject X] (h : Β¬IsZero X) : Mono (simpleSubobjectArrow h) := by dsimp only [simpleSubobjectArrow]
infer_instance
instance mono_simpleSubobjectArrow {X : C} [ArtinianObject X] (h : Β¬IsZero X) : Mono (simpleSubobjectArrow h) := by dsimp only [simpleSubobjectArrow]
Mathlib.CategoryTheory.Noetherian.101_0.bQzOkYhrFw250Z9
instance mono_simpleSubobjectArrow {X : C} [ArtinianObject X] (h : Β¬IsZero X) : Mono (simpleSubobjectArrow h)
Mathlib_CategoryTheory_Noetherian
n : β„• ⊒ ack 0 n = n + 1
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by
rw [ack]
@[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by
Mathlib.Computability.Ackermann.69_0.nk1BuTevlQj1gCN
@[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1
Mathlib_Computability_Ackermann
m : β„• ⊒ ack (m + 1) 0 = ack m 1
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by
rw [ack]
@[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by
Mathlib.Computability.Ackermann.73_0.nk1BuTevlQj1gCN
@[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1
Mathlib_Computability_Ackermann
m n : β„• ⊒ ack (m + 1) (n + 1) = ack m (ack (m + 1) n)
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by
rw [ack]
@[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by
Mathlib.Computability.Ackermann.77_0.nk1BuTevlQj1gCN
@[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n)
Mathlib_Computability_Ackermann
n : β„• ⊒ ack 1 n = n + 2
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by
induction' n with n IH
@[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by
Mathlib.Computability.Ackermann.81_0.nk1BuTevlQj1gCN
@[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2
Mathlib_Computability_Ackermann
case zero ⊒ ack 1 zero = zero + 2
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β·
rfl
@[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β·
Mathlib.Computability.Ackermann.81_0.nk1BuTevlQj1gCN
@[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2
Mathlib_Computability_Ackermann
case succ n : β„• IH : ack 1 n = n + 2 ⊒ ack 1 (succ n) = succ n + 2
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β·
simp [IH]
@[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β·
Mathlib.Computability.Ackermann.81_0.nk1BuTevlQj1gCN
@[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2
Mathlib_Computability_Ackermann
n : β„• ⊒ ack 2 n = 2 * n + 3
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by
induction' n with n IH
@[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by
Mathlib.Computability.Ackermann.88_0.nk1BuTevlQj1gCN
@[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3
Mathlib_Computability_Ackermann
case zero ⊒ ack 2 zero = 2 * zero + 3
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β·
rfl
@[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β·
Mathlib.Computability.Ackermann.88_0.nk1BuTevlQj1gCN
@[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3
Mathlib_Computability_Ackermann
case succ n : β„• IH : ack 2 n = 2 * n + 3 ⊒ ack 2 (succ n) = 2 * succ n + 3
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β·
simpa [mul_succ]
@[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β·
Mathlib.Computability.Ackermann.88_0.nk1BuTevlQj1gCN
@[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3
Mathlib_Computability_Ackermann
n : β„• ⊒ ack 3 n = 2 ^ (n + 3) - 3
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by
induction' n with n IH
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by
Mathlib.Computability.Ackermann.96_0.nk1BuTevlQj1gCN
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3
Mathlib_Computability_Ackermann
case zero ⊒ ack 3 zero = 2 ^ (zero + 3) - 3
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β·
rfl
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β·
Mathlib.Computability.Ackermann.96_0.nk1BuTevlQj1gCN
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3
Mathlib_Computability_Ackermann
case succ n : β„• IH : ack 3 n = 2 ^ (n + 3) - 3 ⊒ ack 3 (succ n) = 2 ^ (succ n + 3) - 3
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β·
rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β·
Mathlib.Computability.Ackermann.96_0.nk1BuTevlQj1gCN
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3
Mathlib_Computability_Ackermann
case succ n : β„• IH : ack 3 n = 2 ^ (n + 3) - 3 ⊒ 2 * 3 ≀ 2 * 2 ^ (n + 3)
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right]
Mathlib.Computability.Ackermann.96_0.nk1BuTevlQj1gCN
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3
Mathlib_Computability_Ackermann
n : β„• IH : ack 3 n = 2 ^ (n + 3) - 3 ⊒ 2 * 3 ≀ 2 * 2 ^ 3
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by
norm_num
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by
Mathlib.Computability.Ackermann.96_0.nk1BuTevlQj1gCN
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3
Mathlib_Computability_Ackermann
case succ n : β„• IH : ack 3 n = 2 ^ (n + 3) - 3 H : 2 * 3 ≀ 2 * 2 ^ 3 ⊒ 2 * 3 ≀ 2 * 2 ^ (n + 3)
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num
apply H.trans
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num
Mathlib.Computability.Ackermann.96_0.nk1BuTevlQj1gCN
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3
Mathlib_Computability_Ackermann
case succ n : β„• IH : ack 3 n = 2 ^ (n + 3) - 3 H : 2 * 3 ≀ 2 * 2 ^ 3 ⊒ 2 * 2 ^ 3 ≀ 2 * 2 ^ (n + 3)
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans
simp [pow_le_pow_right (show 1 ≀ 2 by norm_num)]
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans
Mathlib.Computability.Ackermann.96_0.nk1BuTevlQj1gCN
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3
Mathlib_Computability_Ackermann
n : β„• IH : ack 3 n = 2 ^ (n + 3) - 3 H : 2 * 3 ≀ 2 * 2 ^ 3 ⊒ 1 ≀ 2
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by
norm_num
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by
Mathlib.Computability.Ackermann.96_0.nk1BuTevlQj1gCN
@[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3
Mathlib_Computability_Ackermann
n : β„• ⊒ 0 < ack 0 n
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by norm_num)] #align ack_three ack_three theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by
simp
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by
Mathlib.Computability.Ackermann.107_0.nk1BuTevlQj1gCN
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ] apply ack_pos
Mathlib_Computability_Ackermann
m : β„• ⊒ 0 < ack (m + 1) 0
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by norm_num)] #align ack_three ack_three theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by
rw [ack_succ_zero]
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by
Mathlib.Computability.Ackermann.107_0.nk1BuTevlQj1gCN
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ] apply ack_pos
Mathlib_Computability_Ackermann
m : β„• ⊒ 0 < ack m 1
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by norm_num)] #align ack_three ack_three theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero]
apply ack_pos
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero]
Mathlib.Computability.Ackermann.107_0.nk1BuTevlQj1gCN
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ] apply ack_pos
Mathlib_Computability_Ackermann
m n : β„• ⊒ 0 < ack (m + 1) (n + 1)
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by norm_num)] #align ack_three ack_three theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by
rw [ack_succ_succ]
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by
Mathlib.Computability.Ackermann.107_0.nk1BuTevlQj1gCN
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ] apply ack_pos
Mathlib_Computability_Ackermann
m n : β„• ⊒ 0 < ack m (ack (m + 1) n)
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by norm_num)] #align ack_three ack_three theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ]
apply ack_pos
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ]
Mathlib.Computability.Ackermann.107_0.nk1BuTevlQj1gCN
theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ] apply ack_pos
Mathlib_Computability_Ackermann
n : β„• ⊒ 1 < ack (0 + 1) n
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by norm_num)] #align ack_three ack_three theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ] apply ack_pos #align ack_pos ack_pos theorem one_lt_ack_succ_left : βˆ€ m n, 1 < ack (m + 1) n | 0, n => by
simp
theorem one_lt_ack_succ_left : βˆ€ m n, 1 < ack (m + 1) n | 0, n => by
Mathlib.Computability.Ackermann.117_0.nk1BuTevlQj1gCN
theorem one_lt_ack_succ_left : βˆ€ m n, 1 < ack (m + 1) n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply one_lt_ack_succ_left | m + 1, n + 1 => by rw [ack_succ_succ] apply one_lt_ack_succ_left
Mathlib_Computability_Ackermann
m : β„• ⊒ 1 < ack (m + 1 + 1) 0
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by norm_num)] #align ack_three ack_three theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ] apply ack_pos #align ack_pos ack_pos theorem one_lt_ack_succ_left : βˆ€ m n, 1 < ack (m + 1) n | 0, n => by simp | m + 1, 0 => by
rw [ack_succ_zero]
theorem one_lt_ack_succ_left : βˆ€ m n, 1 < ack (m + 1) n | 0, n => by simp | m + 1, 0 => by
Mathlib.Computability.Ackermann.117_0.nk1BuTevlQj1gCN
theorem one_lt_ack_succ_left : βˆ€ m n, 1 < ack (m + 1) n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply one_lt_ack_succ_left | m + 1, n + 1 => by rw [ack_succ_succ] apply one_lt_ack_succ_left
Mathlib_Computability_Ackermann
m : β„• ⊒ 1 < ack (m + 1) 1
/- Copyright (c) 2022 Violeta HernΓ‘ndez Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta HernΓ‘ndez Palacios -/ import Mathlib.Computability.Primrec import Mathlib.Tactic.Ring import Mathlib.Tactic.Linarith #align_import computability.ackermann from "leanprover-community/mathlib"@"9b2660e1b25419042c8da10bf411aa3c67f14383" /-! # Ackermann function In this file, we define the two-argument Ackermann function `ack`. Despite having a recursive definition, we show that this isn't a primitive recursive function. ## Main results - `exists_lt_ack_of_nat_primrec`: any primitive recursive function is pointwise bounded above by `ack m` for some `m`. - `not_primrecβ‚‚_ack`: the two-argument Ackermann function is not primitive recursive. ## Proof approach We very broadly adapt the proof idea from https://www.planetmath.org/ackermannfunctionisnotprimitiverecursive. Namely, we prove that for any primitive recursive `f : β„• β†’ β„•`, there exists `m` such that `f n < ack m n` for all `n`. This then implies that `fun n => ack n n` can't be primitive recursive, and so neither can `ack`. We aren't able to use the same bounds as in that proof though, since our approach of using pairing functions differs from their approach of using multivariate functions. The important bounds we show during the main inductive proof (`exists_lt_ack_of_nat_primrec`) are the following. Assuming `βˆ€ n, f n < ack a n` and `βˆ€ n, g n < ack b n`, we have: - `βˆ€ n, pair (f n) (g n) < ack (max a b + 3) n`. - `βˆ€ n, g (f n) < ack (max a b + 2) n`. - `βˆ€ n, Nat.rec (f n.unpair.1) (fun (y IH : β„•) => g (pair n.unpair.1 (pair y IH))) n.unpair.2 < ack (max a b + 9) n`. The last one is evidently the hardest. Using `unpair_add_le`, we reduce it to the more manageable - `βˆ€ m n, rec (f m) (fun (y IH : β„•) => g (pair m (pair y IH))) n < ack (max a b + 9) (m + n)`. We then prove this by induction on `n`. Our proof crucially depends on `ack_pair_lt`, which is applied twice, giving us a constant of `4 + 4`. The rest of the proof consists of simpler bounds which bump up our constant to `9`. -/ open Nat /-- The two-argument Ackermann function, defined so that - `ack 0 n = n + 1` - `ack (m + 1) 0 = ack m 1` - `ack (m + 1) (n + 1) = ack m (ack (m + 1) n)`. This is of interest as both a fast-growing function, and as an example of a recursive function that isn't primitive recursive. -/ def ack : β„• β†’ β„• β†’ β„• | 0, n => n + 1 | m + 1, 0 => ack m 1 | m + 1, n + 1 => ack m (ack (m + 1) n) termination_by ack m n => (m, n) #align ack ack @[simp] theorem ack_zero (n : β„•) : ack 0 n = n + 1 := by rw [ack] #align ack_zero ack_zero @[simp] theorem ack_succ_zero (m : β„•) : ack (m + 1) 0 = ack m 1 := by rw [ack] #align ack_succ_zero ack_succ_zero @[simp] theorem ack_succ_succ (m n : β„•) : ack (m + 1) (n + 1) = ack m (ack (m + 1) n) := by rw [ack] #align ack_succ_succ ack_succ_succ @[simp] theorem ack_one (n : β„•) : ack 1 n = n + 2 := by induction' n with n IH Β· rfl Β· simp [IH] #align ack_one ack_one @[simp] theorem ack_two (n : β„•) : ack 2 n = 2 * n + 3 := by induction' n with n IH Β· rfl Β· simpa [mul_succ] #align ack_two ack_two -- Porting note: re-written to get rid of ack_three_aux @[simp] theorem ack_three (n : β„•) : ack 3 n = 2 ^ (n + 3) - 3 := by induction' n with n IH Β· rfl Β· rw [ack_succ_succ, IH, ack_two, Nat.succ_add, Nat.pow_succ 2 (n + 3), mul_comm _ 2, Nat.mul_sub_left_distrib, ← Nat.sub_add_comm, two_mul 3, Nat.add_sub_add_right] have H : 2 * 3 ≀ 2 * 2 ^ 3 := by norm_num apply H.trans simp [pow_le_pow_right (show 1 ≀ 2 by norm_num)] #align ack_three ack_three theorem ack_pos : βˆ€ m n, 0 < ack m n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply ack_pos | m + 1, n + 1 => by rw [ack_succ_succ] apply ack_pos #align ack_pos ack_pos theorem one_lt_ack_succ_left : βˆ€ m n, 1 < ack (m + 1) n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero]
apply one_lt_ack_succ_left
theorem one_lt_ack_succ_left : βˆ€ m n, 1 < ack (m + 1) n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero]
Mathlib.Computability.Ackermann.117_0.nk1BuTevlQj1gCN
theorem one_lt_ack_succ_left : βˆ€ m n, 1 < ack (m + 1) n | 0, n => by simp | m + 1, 0 => by rw [ack_succ_zero] apply one_lt_ack_succ_left | m + 1, n + 1 => by rw [ack_succ_succ] apply one_lt_ack_succ_left
Mathlib_Computability_Ackermann