Skip to content

Commit 0128f95

Browse files
committed
chore: Move Data.Rat.Order to Algebra.Order.Ring.Rat (#13169)
1 parent c3aaea7 commit 0128f95

File tree

20 files changed

+104
-88
lines changed

20 files changed

+104
-88
lines changed

Mathlib.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ import Mathlib.Algebra.Order.Ring.InjSurj
556556
import Mathlib.Algebra.Order.Ring.Int
557557
import Mathlib.Algebra.Order.Ring.Nat
558558
import Mathlib.Algebra.Order.Ring.Pow
559+
import Mathlib.Algebra.Order.Ring.Rat
559560
import Mathlib.Algebra.Order.Ring.Star
560561
import Mathlib.Algebra.Order.Ring.Synonym
561562
import Mathlib.Algebra.Order.Ring.WithTop
@@ -2190,7 +2191,6 @@ import Mathlib.Data.Rat.Encodable
21902191
import Mathlib.Data.Rat.Floor
21912192
import Mathlib.Data.Rat.Init
21922193
import Mathlib.Data.Rat.Lemmas
2193-
import Mathlib.Data.Rat.Order
21942194
import Mathlib.Data.Rat.Sqrt
21952195
import Mathlib.Data.Rat.Star
21962196
import Mathlib.Data.Rbmap.Basic

Mathlib/Algebra/Order/Field/Rat.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Authors: Johannes Hölzl, Mario Carneiro
55
-/
66
import Mathlib.Algebra.Field.Rat
77
import Mathlib.Algebra.Order.Nonneg.Field
8-
import Mathlib.Data.Rat.Order
8+
import Mathlib.Algebra.Order.Ring.Rat
9+
import Mathlib.Data.NNRat.Defs
910

1011
#align_import data.rat.basic from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e"
1112

Mathlib/Data/Rat/Order.lean renamed to Mathlib/Algebra/Order/Ring/Rat.lean

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ Authors: Johannes Hölzl, Mario Carneiro
55
-/
66
import Mathlib.Algebra.Order.Ring.Int
77
import Mathlib.Algebra.Ring.Rat
8-
import Mathlib.Data.Nat.Cast.Order
9-
import Mathlib.Data.Rat.Defs
108

119
#align_import data.rat.order from "leanprover-community/mathlib"@"a59dad53320b73ef180174aae867addd707ef00e"
1210

1311
/-!
14-
# Order for Rational Numbers
12+
# The rational numbers form a linear ordered field
1513
1614
This file constructs the order on `ℚ` and proves that `ℚ` is a discrete, linearly ordered
1715
commutative ring.
@@ -24,21 +22,20 @@ here because we need the order on `ℚ` to define `ℚ≥0`, which we itself nee
2422
rat, rationals, field, ℚ, numerator, denominator, num, denom, order, ordering
2523
-/
2624

25+
assert_not_exists Field
26+
assert_not_exists Finset
27+
assert_not_exists Set.Icc
28+
assert_not_exists GaloisConnection
2729

2830
namespace Rat
2931

30-
variable {a b c : ℚ}
31-
32-
#noalign rat.nonneg
33-
34-
@[simp] lemma num_nonneg : 0 ≤ a.num ↔ 0 ≤ a := by simp [instLE, Rat.blt, le_iff_lt_or_eq]; tauto
35-
#align rat.num_nonneg_iff_zero_le Rat.num_nonneg
32+
variable {a b c p q : ℚ}
3633

3734
@[simp] lemma divInt_nonneg_iff_of_pos_right {a b : ℤ} (hb : 0 < b) : 0 ≤ a /. b ↔ 0 ≤ a := by
3835
cases' hab : a /. b with n d hd hnd
3936
rw [mk'_eq_divInt, divInt_eq_iff hb.ne' (mod_cast hd)] at hab
4037
rw [← num_nonneg, ← mul_nonneg_iff_of_pos_right hb, ← hab,
41-
mul_nonneg_iff_of_pos_right (mod_cast hd.bot_lt)]
38+
mul_nonneg_iff_of_pos_right (mod_cast Nat.pos_of_ne_zero hd)]
4239
#align rat.mk_nonneg Rat.divInt_nonneg_iff_of_pos_right
4340

4441
@[simp] lemma divInt_nonneg {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a /. b := by
@@ -48,7 +45,7 @@ variable {a b c : ℚ}
4845
rwa [divInt_nonneg_iff_of_pos_right hb]
4946

5047
@[simp] lemma mkRat_nonneg {a : ℤ} (ha : 0 ≤ a) (b : ℕ) : 0 ≤ mkRat a b := by
51-
simpa using divInt_nonneg ha b.cast_nonneg
48+
simpa using divInt_nonneg ha (Int.natCast_nonneg _)
5249

5350
theorem ofScientific_nonneg (m : ℕ) (s : Bool) (e : ℕ) :
5451
0 ≤ Rat.ofScientific m s e := by
@@ -57,9 +54,9 @@ theorem ofScientific_nonneg (m : ℕ) (s : Bool) (e : ℕ) :
5754
· rw [if_neg (by decide)]
5855
refine num_nonneg.mp ?_
5956
rw [num_natCast]
60-
exact Nat.cast_nonneg _
57+
exact Int.natCast_nonneg _
6158
· rw [if_pos rfl, normalize_eq_mkRat]
62-
exact Rat.mkRat_nonneg (Nat.cast_nonneg _) _
59+
exact Rat.mkRat_nonneg (Int.natCast_nonneg _) _
6360

6461
instance _root_.NNRatCast.toOfScientific {K} [NNRatCast K] : OfScientific K where
6562
ofScientific (m : ℕ) (b : Bool) (d : ℕ) :=
@@ -73,8 +70,8 @@ theorem _root_.NNRat.cast_ofScientific {K} [NNRatCast K] (m : ℕ) (s : Bool) (e
7370

7471
protected lemma add_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a + b :=
7572
numDenCasesOn' a fun n₁ d₁ h₁ ↦ numDenCasesOn' b fun n₂ d₂ h₂ ↦ by
76-
have d₁0 : 0 < (d₁ : ℤ) := mod_cast h₁.bot_lt
77-
have d₂0 : 0 < (d₂ : ℤ) := mod_cast h₂.bot_lt
73+
have d₁0 : 0 < (d₁ : ℤ) := mod_cast Nat.pos_of_ne_zero h₁
74+
have d₂0 : 0 < (d₂ : ℤ) := mod_cast Nat.pos_of_ne_zero h₂
7875
simp only [d₁0, d₂0, h₁, h₂, mul_pos, divInt_nonneg_iff_of_pos_right, divInt_add_divInt, Ne,
7976
Nat.cast_eq_zero, not_false_iff]
8077
intro n₁0 n₂0
@@ -84,24 +81,14 @@ protected lemma add_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a + b :=
8481
protected lemma mul_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a * b :=
8582
numDenCasesOn' a fun n₁ d₁ h₁ =>
8683
numDenCasesOn' b fun n₂ d₂ h₂ => by
87-
have d₁0 : 0 < (d₁ : ℤ) := mod_cast h₁.bot_lt
88-
have d₂0 : 0 < (d₂ : ℤ) := mod_cast h₂.bot_lt
84+
have d₁0 : 0 < (d₁ : ℤ) := mod_cast Nat.pos_of_ne_zero h₁
85+
have d₂0 : 0 < (d₂ : ℤ) := mod_cast Nat.pos_of_ne_zero h₂
8986
simp only [d₁0, d₂0, mul_pos, divInt_nonneg_iff_of_pos_right,
9087
divInt_mul_divInt _ _ d₁0.ne' d₂0.ne']
9188
apply mul_nonneg
9289
#align rat.nonneg_mul Rat.mul_nonneg
9390
#align rat.mul_nonneg Rat.mul_nonneg
9491

95-
protected lemma nonneg_antisymm : 0 ≤ a → 0 ≤ -a → a = 0 := by
96-
simp_rw [← num_eq_zero, le_antisymm_iff, ← num_nonneg, num_neg_eq_neg_num, neg_nonneg]; tauto
97-
#align rat.nonneg_antisymm Rat.nonneg_antisymm
98-
99-
protected theorem nonneg_total (a : ℚ) : 0 ≤ a ∨ 0 ≤ -a := by
100-
simp_rw [← num_nonneg, num_neg_eq_neg_num, neg_nonneg]; exact le_total _ _
101-
#align rat.nonneg_total Rat.nonneg_total
102-
103-
#align rat.decidable_nonneg Rat.instDecidableLe
104-
10592
-- Porting note (#11215): TODO can this be shortened?
10693
protected theorem le_iff_sub_nonneg (a b : ℚ) : a ≤ b ↔ 0 ≤ b - a :=
10794
numDenCasesOn'' a fun na da ha hared =>
@@ -118,32 +105,30 @@ protected theorem le_iff_sub_nonneg (a b : ℚ) : a ≤ b ↔ 0 ≤ b - a :=
118105
· rw [sub_neg]
119106
apply lt_of_lt_of_le
120107
· apply mul_neg_of_neg_of_pos h.1
121-
rwa [Nat.cast_pos, pos_iff_ne_zero]
122-
· apply mul_nonneg h.2 (Nat.cast_nonneg _)
123-
· simp only [Nat.cast_pos]
124-
apply Nat.gcd_pos_of_pos_right
125-
apply mul_pos <;> rwa [pos_iff_ne_zero]
108+
rwa [Int.natCast_pos, Nat.pos_iff_ne_zero]
109+
· apply mul_nonneg h.2 (Int.natCast_nonneg _)
110+
· simp only [Int.natCast_pos, Nat.pos_iff_ne_zero]
111+
exact Nat.gcd_ne_zero_right (Nat.mul_ne_zero hb ha)
126112
· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h'
127113
simp [h']
128114
· simp only [Rat.sub_def, normalize_eq]
129115
refine ⟨fun H => ?_, fun H _ => ?_⟩
130-
· refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _)
116+
· refine Int.ediv_nonneg ?_ (Int.natCast_nonneg _)
131117
rw [sub_nonneg]
132118
push_neg at h
133119
obtain hb|hb := Ne.lt_or_lt h'
134120
· apply H
135121
intro H'
136122
exact (hb.trans H').false.elim
137123
· obtain ha|ha := le_or_lt na 0
138-
· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Nat.cast_nonneg _)
139-
exact mul_nonneg hb.le (Nat.cast_nonneg _)
124+
· apply le_trans <| mul_nonpos_of_nonpos_of_nonneg ha (Int.natCast_nonneg _)
125+
exact mul_nonneg hb.le (Int.natCast_nonneg _)
140126
· exact H (fun _ => ha)
141127
· rw [← sub_nonneg]
142128
contrapose! H
143129
apply Int.ediv_neg' H
144-
simp only [Nat.cast_pos]
145-
apply Nat.gcd_pos_of_pos_right
146-
apply mul_pos <;> rwa [pos_iff_ne_zero]
130+
simp only [Int.natCast_pos, Nat.pos_iff_ne_zero]
131+
exact Nat.gcd_ne_zero_right (Nat.mul_ne_zero hb ha)
147132

148133
protected lemma divInt_le_divInt {a b c d : ℤ} (b0 : 0 < b) (d0 : 0 < d) :
149134
a /. b ≤ c /. d ↔ a * d ≤ c * b := by
@@ -181,32 +166,30 @@ instance linearOrder : LinearOrder ℚ where
181166
#align rat.le_antisymm le_antisymm
182167
#align rat.le_trans le_trans
183168

184-
-- Extra instances to short-circuit type class resolution
185-
instance : LT ℚ := by infer_instance
186-
187-
instance : DistribLattice ℚ := by infer_instance
188-
189-
instance instLattice : Lattice ℚ := by infer_instance
190-
191-
instance : SemilatticeInf ℚ := by infer_instance
192-
193-
instance : SemilatticeSup ℚ := by infer_instance
194-
195-
instance : Inf ℚ := by infer_instance
169+
/-!
170+
### Extra instances to short-circuit type class resolution
196171
197-
instance : Sup ℚ := by infer_instance
172+
These also prevent non-computable instances being used to construct these instances non-computably.
173+
-/
198174

199-
instance : PartialOrder ℚ := by infer_instance
175+
instance instDistribLattice : DistribLattice ℚ := inferInstance
176+
instance instLattice : Lattice ℚ := inferInstance
177+
instance instSemilatticeInf : SemilatticeInf ℚ := inferInstance
178+
instance instSemilatticeSup : SemilatticeSup ℚ := inferInstance
179+
instance instInf : Inf ℚ := inferInstance
180+
instance instSup : Sup ℚ := inferInstance
181+
instance instPartialOrder : PartialOrder ℚ := inferInstance
182+
instance instPreorder : Preorder ℚ := inferInstance
200183

201-
instance : Preorder ℚ := by infer_instance
184+
/-! ### Miscellaneous lemmas -/
202185

203-
protected lemma le_def {p q : ℚ} : p ≤ q ↔ p.num * q.den ≤ q.num * p.den := by
186+
protected lemma le_def : p ≤ q ↔ p.num * q.den ≤ q.num * p.den := by
204187
rw [← num_divInt_den q, ← num_divInt_den p]
205188
conv_rhs => simp only [num_divInt_den]
206189
exact Rat.divInt_le_divInt (mod_cast p.pos) (mod_cast q.pos)
207190
#align rat.le_def' Rat.le_def
208191

209-
protected theorem lt_def {p q : ℚ} : p < q ↔ p.num * q.den < q.num * p.den := by
192+
protected lemma lt_def : p < q ↔ p.num * q.den < q.num * p.den := by
210193
rw [lt_iff_le_and_ne, Rat.le_def]
211194
suffices p ≠ q ↔ p.num * q.den ≠ q.num * p.den by
212195
constructor <;> intro h
@@ -279,11 +262,3 @@ theorem abs_def (q : ℚ) : |q| = q.num.natAbs /. q.den := by
279262
#align rat.abs_def Rat.abs_def
280263

281264
end Rat
282-
283-
-- We make some assertions here about declarations that do not need to be in the import dependencies
284-
-- for this file, but have been in the past.
285-
assert_not_exists Fintype
286-
287-
assert_not_exists Set.Icc
288-
289-
assert_not_exists GaloisConnection

Mathlib/Algebra/Ring/Rat.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This file contains the commutative ring instance on the rational numbers.
1717
See note [foundational algebra order theory].
1818
-/
1919

20+
assert_not_exists OrderedCommMonoid
2021
assert_not_exists Field
2122
assert_not_exists PNat
2223
assert_not_exists Nat.dvd_mul

Mathlib/Combinatorics/SetFamily/LYM.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Mathlib.Algebra.Order.Field.Basic
99
import Mathlib.Algebra.Order.Field.Rat
1010
import Mathlib.Combinatorics.Enumerative.DoubleCounting
1111
import Mathlib.Combinatorics.SetFamily.Shadow
12-
import Mathlib.Data.Rat.Order
1312

1413
#align_import combinatorics.set_family.lym from "leanprover-community/mathlib"@"861a26926586cd46ff80264d121cdb6fa0e35cc1"
1514

Mathlib/Data/Int/Defs.lean

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ variable {a b c d m n : ℤ}
4949
#align int.of_nat_nat_abs_eq_of_nonneg Int.ofNat_natAbs_eq_of_nonnegₓ
5050
#align int.nat_abs_of_neg_succ_of_nat Int.natAbs_negSucc
5151

52+
section Order
53+
variable {a b c : ℤ}
54+
55+
protected lemma le_rfl : a ≤ a := a.le_refl
56+
protected lemma lt_or_lt_of_ne : a ≠ b → a < b ∨ b < a := Int.lt_or_gt_of_ne
57+
protected lemma lt_or_le (a b : ℤ) : a < b ∨ b ≤ a := by rw [← Int.not_lt]; exact em _
58+
protected lemma le_or_lt (a b : ℤ) : a ≤ b ∨ b < a := (b.lt_or_le a).symm
59+
protected lemma lt_asymm : a < b → ¬ b < a := by rw [Int.not_lt]; exact Int.le_of_lt
60+
protected lemma le_of_eq (hab : a = b) : a ≤ b := by rw [hab]; exact Int.le_rfl
61+
protected lemma ge_of_eq (hab : a = b) : b ≤ a := Int.le_of_eq hab.symm
62+
protected lemma le_antisymm_iff : a = b ↔ a ≤ b ∧ b ≤ a :=
63+
fun h ↦ ⟨Int.le_of_eq h, Int.ge_of_eq h⟩, fun h ↦ Int.le_antisymm h.1 h.2
64+
protected lemma le_iff_eq_or_lt : a ≤ b ↔ a = b ∨ a < b := by
65+
rw [Int.le_antisymm_iff, Int.lt_iff_le_not_le, ← and_or_left]; simp [em]
66+
protected lemma le_iff_lt_or_eq : a ≤ b ↔ a < b ∨ a = b := by rw [Int.le_iff_eq_or_lt, or_comm]
67+
68+
end Order
69+
5270
-- TODO: Tag in Lean
5371
attribute [simp] natAbs_pos
5472

@@ -162,6 +180,12 @@ set_option linter.deprecated false
162180

163181
end deprecated
164182

183+
protected lemma mul_le_mul_iff_of_pos_right (ha : 0 < a) : b * a ≤ c * a ↔ b ≤ c :=
184+
⟨(le_of_mul_le_mul_right · ha), (Int.mul_le_mul_of_nonneg_right · (Int.le_of_lt ha))⟩
185+
186+
protected lemma mul_nonneg_iff_of_pos_right (hb : 0 < b) : 0 ≤ a * b ↔ 0 ≤ a := by
187+
simpa using (Int.mul_le_mul_iff_of_pos_right hb : 0 * b ≤ a * b ↔ 0 ≤ a)
188+
165189
/-! ### succ and pred -/
166190

167191
#align int.lt_add_one_iff Int.lt_add_one_iff

Mathlib/Data/Int/Order/Lemmas.lean

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import Mathlib.Algebra.Order.Ring.Abs
1212
1313
The distinction between this file and `Data.Int.Order.Basic` is not particularly clear.
1414
They are separated by now to minimize the porting requirements for tactics during the transition to
15-
mathlib4. Now that `Data.Rat.Order` has been ported, please feel free to reorganize these two files.
15+
mathlib4. Now that `Algebra.Order.Ring.Rat` has been ported, please feel free to reorganize these
16+
two files.
1617
-/
1718

1819
open Function Nat

Mathlib/Data/NNRat/Defs.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Yaël Dillies, Bhavik Mehta
55
-/
66
import Mathlib.Algebra.Order.Nonneg.Ring
7+
import Mathlib.Algebra.Order.Ring.Rat
78
import Mathlib.Data.Int.Lemmas
8-
import Mathlib.Data.Rat.Order
99

1010
#align_import data.rat.nnrat from "leanprover-community/mathlib"@"b3f4f007a962e3787aa0f3b5c7942a1317f7d88e"
1111

Mathlib/Data/Nat/Cast/WithTop.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Mathlib.Algebra.Ring.Nat
1212
# Lemma about the coercion `ℕ → WithBot ℕ`.
1313
1414
An orphaned lemma about casting from `ℕ` to `WithBot ℕ`,
15-
exiled here during the port to minimize imports of `Data.Rat.Order`.
15+
exiled here during the port to minimize imports of `Algebra.Order.Ring.Rat`.
1616
-/
1717

1818
instance : WellFoundedRelation (WithTop ℕ) where

Mathlib/Data/Nat/Order/Lemmas.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import Mathlib.Data.Set.Basic
1313
1414
The distinction between this file and `Mathlib.Algebra.Order.Ring.Nat` is not particularly clear.
1515
They are separated for now to minimize the porting requirements for tactics during the transition to
16-
mathlib4. After `Mathlib.Data.Rat.Order` has been ported,
16+
mathlib4. After `Mathlib.Algebra.Order.Ring.Rat` has been ported,
1717
please feel free to reorganize these two files.
1818
-/
1919

0 commit comments

Comments
 (0)