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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.