@@ -5,13 +5,11 @@ Authors: Johannes Hölzl, Mario Carneiro
5
5
-/
6
6
import Mathlib.Algebra.Order.Ring.Int
7
7
import Mathlib.Algebra.Ring.Rat
8
- import Mathlib.Data.Nat.Cast.Order
9
- import Mathlib.Data.Rat.Defs
10
8
11
9
#align_import data.rat.order from "leanprover-community/mathlib" @"a59dad53320b73ef180174aae867addd707ef00e"
12
10
13
11
/-!
14
- # Order for Rational Numbers
12
+ # The rational numbers form a linear ordered field
15
13
16
14
This file constructs the order on `ℚ` and proves that `ℚ` is a discrete, linearly ordered
17
15
commutative ring.
@@ -24,21 +22,20 @@ here because we need the order on `ℚ` to define `ℚ≥0`, which we itself nee
24
22
rat, rationals, field, ℚ, numerator, denominator, num, denom, order, ordering
25
23
-/
26
24
25
+ assert_not_exists Field
26
+ assert_not_exists Finset
27
+ assert_not_exists Set.Icc
28
+ assert_not_exists GaloisConnection
27
29
28
30
namespace Rat
29
31
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 : ℚ}
36
33
37
34
@[simp] lemma divInt_nonneg_iff_of_pos_right {a b : ℤ} (hb : 0 < b) : 0 ≤ a /. b ↔ 0 ≤ a := by
38
35
cases' hab : a /. b with n d hd hnd
39
36
rw [mk'_eq_divInt, divInt_eq_iff hb.ne' (mod_cast hd)] at hab
40
37
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 )]
42
39
#align rat.mk_nonneg Rat.divInt_nonneg_iff_of_pos_right
43
40
44
41
@[simp] lemma divInt_nonneg {a b : ℤ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a /. b := by
@@ -48,7 +45,7 @@ variable {a b c : ℚ}
48
45
rwa [divInt_nonneg_iff_of_pos_right hb]
49
46
50
47
@[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 _)
52
49
53
50
theorem ofScientific_nonneg (m : ℕ) (s : Bool) (e : ℕ) :
54
51
0 ≤ Rat.ofScientific m s e := by
@@ -57,9 +54,9 @@ theorem ofScientific_nonneg (m : ℕ) (s : Bool) (e : ℕ) :
57
54
· rw [if_neg (by decide)]
58
55
refine num_nonneg.mp ?_
59
56
rw [num_natCast]
60
- exact Nat.cast_nonneg _
57
+ exact Int.natCast_nonneg _
61
58
· rw [if_pos rfl, normalize_eq_mkRat]
62
- exact Rat.mkRat_nonneg (Nat.cast_nonneg _) _
59
+ exact Rat.mkRat_nonneg (Int.natCast_nonneg _) _
63
60
64
61
instance _root_.NNRatCast.toOfScientific {K} [NNRatCast K] : OfScientific K where
65
62
ofScientific (m : ℕ) (b : Bool) (d : ℕ) :=
@@ -73,8 +70,8 @@ theorem _root_.NNRat.cast_ofScientific {K} [NNRatCast K] (m : ℕ) (s : Bool) (e
73
70
74
71
protected lemma add_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a + b :=
75
72
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₂
78
75
simp only [d₁0 , d₂0 , h₁, h₂, mul_pos, divInt_nonneg_iff_of_pos_right, divInt_add_divInt, Ne,
79
76
Nat.cast_eq_zero, not_false_iff]
80
77
intro n₁0 n₂0
@@ -84,24 +81,14 @@ protected lemma add_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a + b :=
84
81
protected lemma mul_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a * b :=
85
82
numDenCasesOn' a fun n₁ d₁ h₁ =>
86
83
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₂
89
86
simp only [d₁0 , d₂0 , mul_pos, divInt_nonneg_iff_of_pos_right,
90
87
divInt_mul_divInt _ _ d₁0 .ne' d₂0 .ne']
91
88
apply mul_nonneg
92
89
#align rat.nonneg_mul Rat.mul_nonneg
93
90
#align rat.mul_nonneg Rat.mul_nonneg
94
91
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
-
105
92
-- Porting note (#11215): TODO can this be shortened?
106
93
protected theorem le_iff_sub_nonneg (a b : ℚ) : a ≤ b ↔ 0 ≤ b - a :=
107
94
numDenCasesOn'' a fun na da ha hared =>
@@ -118,32 +105,30 @@ protected theorem le_iff_sub_nonneg (a b : ℚ) : a ≤ b ↔ 0 ≤ b - a :=
118
105
· rw [sub_neg]
119
106
apply lt_of_lt_of_le
120
107
· 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)
126
112
· simp only [divInt_ofNat, ← zero_iff_num_zero, mkRat_eq_zero hb] at h'
127
113
simp [h']
128
114
· simp only [Rat.sub_def, normalize_eq]
129
115
refine ⟨fun H => ?_, fun H _ => ?_⟩
130
- · refine Int.ediv_nonneg ?_ (Nat.cast_nonneg _)
116
+ · refine Int.ediv_nonneg ?_ (Int.natCast_nonneg _)
131
117
rw [sub_nonneg]
132
118
push_neg at h
133
119
obtain hb|hb := Ne.lt_or_lt h'
134
120
· apply H
135
121
intro H'
136
122
exact (hb.trans H').false .elim
137
123
· 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 _)
140
126
· exact H (fun _ => ha)
141
127
· rw [← sub_nonneg]
142
128
contrapose! H
143
129
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)
147
132
148
133
protected lemma divInt_le_divInt {a b c d : ℤ} (b0 : 0 < b) (d0 : 0 < d) :
149
134
a /. b ≤ c /. d ↔ a * d ≤ c * b := by
@@ -181,32 +166,30 @@ instance linearOrder : LinearOrder ℚ where
181
166
#align rat.le_antisymm le_antisymm
182
167
#align rat.le_trans le_trans
183
168
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
196
171
197
- instance : Sup ℚ := by infer_instance
172
+ These also prevent non-computable instances being used to construct these instances non-computably.
173
+ -/
198
174
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
200
183
201
- instance : Preorder ℚ := by infer_instance
184
+ /-! ### Miscellaneous lemmas -/
202
185
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
204
187
rw [← num_divInt_den q, ← num_divInt_den p]
205
188
conv_rhs => simp only [num_divInt_den]
206
189
exact Rat.divInt_le_divInt (mod_cast p.pos) (mod_cast q.pos)
207
190
#align rat.le_def' Rat.le_def
208
191
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
210
193
rw [lt_iff_le_and_ne, Rat.le_def]
211
194
suffices p ≠ q ↔ p.num * q.den ≠ q.num * p.den by
212
195
constructor <;> intro h
@@ -279,11 +262,3 @@ theorem abs_def (q : ℚ) : |q| = q.num.natAbs /. q.den := by
279
262
#align rat.abs_def Rat.abs_def
280
263
281
264
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
0 commit comments