@@ -13,17 +13,57 @@ import algebra.star.basic
13
13
14
14
/-- The type `ℝ` of real numbers constructed as equivalence classes of Cauchy sequences of rational
15
15
numbers. -/
16
- def real := @cau_seq.completion.Cauchy ℚ _ _ _ abs _
16
+ structure real := of_cauchy ::
17
+ (cauchy : @cau_seq.completion.Cauchy ℚ _ _ _ abs _)
17
18
notation `ℝ ` := real
18
19
20
+ attribute [pp_using_anonymous_constructor] real
21
+
19
22
namespace real
20
23
open cau_seq cau_seq.completion
21
24
22
25
variables {x y : ℝ}
23
26
24
- def comm_ring_aux : comm_ring ℝ := Cauchy.comm_ring
27
+ lemma ext_cauchy_iff : ∀ {x y : real}, x = y ↔ x.cauchy = y.cauchy
28
+ | ⟨a⟩ ⟨b⟩ := by split; cc
29
+
30
+ lemma ext_cauchy {x y : real} : x.cauchy = y.cauchy → x = y :=
31
+ ext_cauchy_iff.2
32
+
33
+ /-- The real numbers are isomorphic to the quotient of Cauchy sequences on the rationals. -/
34
+ def equiv_Cauchy : ℝ ≃ cau_seq.completion.Cauchy :=
35
+ ⟨real.cauchy, real.of_cauchy, λ ⟨_⟩, rfl, λ _, rfl⟩
36
+
37
+ -- irreducible doesn't work for instances: https://github.com/leanprover-community/lean/issues/511
38
+ @[irreducible] private def zero : ℝ := ⟨0 ⟩
39
+ @[irreducible] private def one : ℝ := ⟨1 ⟩
40
+ @[irreducible] private def add : ℝ → ℝ → ℝ | ⟨a⟩ ⟨b⟩ := ⟨a + b⟩
41
+ @[irreducible] private def neg : ℝ → ℝ | ⟨a⟩ := ⟨-a⟩
42
+ @[irreducible] private def mul : ℝ → ℝ → ℝ | ⟨a⟩ ⟨b⟩ := ⟨a * b⟩
25
43
26
- instance : comm_ring ℝ := { ..comm_ring_aux }
44
+ instance : has_zero ℝ := ⟨zero⟩
45
+ instance : has_one ℝ := ⟨one⟩
46
+ instance : has_add ℝ := ⟨add⟩
47
+ instance : has_neg ℝ := ⟨neg⟩
48
+ instance : has_mul ℝ := ⟨mul⟩
49
+
50
+ lemma zero_cauchy : (⟨0 ⟩ : ℝ) = 0 := show _ = zero, by rw zero
51
+ lemma one_cauchy : (⟨1 ⟩ : ℝ) = 1 := show _ = one, by rw one
52
+ lemma add_cauchy {a b} : (⟨a⟩ + ⟨b⟩ : ℝ) = ⟨a + b⟩ := show add _ _ = _, by rw add
53
+ lemma neg_cauchy {a} : (-⟨a⟩ : ℝ) = ⟨-a⟩ := show neg _ = _, by rw neg
54
+ lemma mul_cauchy {a b} : (⟨a⟩ * ⟨b⟩ : ℝ) = ⟨a * b⟩ := show mul _ _ = _, by rw mul
55
+
56
+ instance : comm_ring ℝ :=
57
+ begin
58
+ refine_struct { zero := 0 , one := 1 , mul := (*),
59
+ add := (+), neg := @has_neg.neg ℝ _, sub := λ a b, a + (-b) },
60
+ all_goals {
61
+ repeat { rintro ⟨_⟩, },
62
+ simp [← zero_cauchy, ← one_cauchy, add_cauchy, neg_cauchy, mul_cauchy],
63
+ apply add_assoc <|> apply add_comm <|> apply mul_assoc <|> apply mul_comm <|>
64
+ apply left_distrib <|> apply right_distrib <|> apply sub_eq_add_neg <|> skip
65
+ },
66
+ end
27
67
28
68
/- Extra instances to short-circuit type class resolution -/
29
69
instance : ring ℝ := by apply_instance
@@ -41,71 +81,112 @@ instance : comm_monoid ℝ := by apply_instance
41
81
instance : monoid ℝ := by apply_instance
42
82
instance : comm_semigroup ℝ := by apply_instance
43
83
instance : semigroup ℝ := by apply_instance
84
+ instance : has_sub ℝ := by apply_instance
44
85
instance : inhabited ℝ := ⟨0 ⟩
45
86
46
87
/-- The real numbers are a *-ring, with the trivial *-structure. -/
47
88
instance : star_ring ℝ := star_ring_of_comm
48
89
49
90
/-- Coercion `ℚ` → `ℝ` as a `ring_hom`. Note that this
50
91
is `cau_seq.completion.of_rat`, not `rat.cast`. -/
51
- def of_rat : ℚ →+* ℝ := ⟨of_rat, rfl, of_rat_mul, rfl, of_rat_add⟩
92
+ def of_rat : ℚ →+* ℝ :=
93
+ by refine_struct { to_fun := of_cauchy ∘ of_rat };
94
+ simp [of_rat_one, of_rat_zero, of_rat_mul, of_rat_add,
95
+ one_cauchy, zero_cauchy, ← mul_cauchy, ← add_cauchy]
96
+
97
+ lemma of_rat_apply (x : ℚ) : of_rat x = of_cauchy (cau_seq.completion.of_rat x) := rfl
52
98
53
99
/-- Make a real number from a Cauchy sequence of rationals (by taking the equivalence class). -/
54
- def mk (x : cau_seq ℚ abs) : ℝ := cau_seq.completion.mk x
100
+ def mk (x : cau_seq ℚ abs) : ℝ := ⟨ cau_seq.completion.mk x⟩
55
101
56
- theorem of_rat_sub (x y : ℚ) : of_rat (x - y) = of_rat x - of_rat y :=
57
- congr_arg mk (const_sub _ _)
102
+ theorem mk_eq {f g : cau_seq ℚ abs} : mk f = mk g ↔ f ≈ g :=
103
+ ext_cauchy_iff.trans mk_eq
58
104
59
- instance : has_lt ℝ :=
60
- ⟨λ x y, quotient.lift_on₂ x y (<) $
105
+ @[irreducible]
106
+ private def lt : ℝ → ℝ → Prop | ⟨x⟩ ⟨y⟩ :=
107
+ quotient.lift_on₂ x y (<) $
61
108
λ f₁ g₁ f₂ g₂ hf hg, propext $
62
109
⟨λ h, lt_of_eq_of_lt (setoid.symm hf) (lt_of_lt_of_eq h hg),
63
- λ h, lt_of_eq_of_lt hf (lt_of_lt_of_eq h (setoid.symm hg))⟩⟩
110
+ λ h, lt_of_eq_of_lt hf (lt_of_lt_of_eq h (setoid.symm hg))⟩
64
111
65
- @[simp] theorem mk_lt {f g : cau_seq ℚ abs} : mk f < mk g ↔ f < g := iff.rfl
112
+ instance : has_lt ℝ := ⟨lt⟩
66
113
67
- theorem mk_eq {f g : cau_seq ℚ abs } : mk f = mk g ↔ f ≈ g := mk_eq
114
+ lemma lt_cauchy {f g} : (⟨⟦f⟧⟩ : ℝ) < ⟨⟦g⟧⟩ ↔ f < g := show lt _ _ ↔ _, by rw lt; refl
68
115
69
- theorem quotient_mk_eq_mk (f : cau_seq ℚ abs) : ⟦f⟧ = mk f := rfl
116
+ @[simp] theorem mk_lt {f g : cau_seq ℚ abs} : mk f < mk g ↔ f < g :=
117
+ lt_cauchy
70
118
71
- theorem mk_eq_mk {f : cau_seq ℚ abs} : cau_seq.completion.mk f = mk f := rfl
119
+ lemma mk_zero : mk 0 = 0 := by rw ← zero_cauchy; refl
120
+ lemma mk_one : mk 1 = 1 := by rw ← one_cauchy; refl
121
+ lemma mk_add {f g : cau_seq ℚ abs} : mk (f + g) = mk f + mk g := by simp [mk, add_cauchy]
122
+ lemma mk_mul {f g : cau_seq ℚ abs} : mk (f * g) = mk f * mk g := by simp [mk, mul_cauchy]
123
+ lemma mk_neg {f : cau_seq ℚ abs} : mk (-f) = -mk f := by simp [mk, neg_cauchy]
72
124
73
125
@[simp] theorem mk_pos {f : cau_seq ℚ abs} : 0 < mk f ↔ pos f :=
74
- iff_of_eq (congr_arg pos (sub_zero f))
126
+ by rw [← mk_zero, mk_lt]; exact iff_of_eq (congr_arg pos (sub_zero f))
75
127
76
- protected def le (x y : ℝ) : Prop := x < y ∨ x = y
77
- instance : has_le ℝ := ⟨real.le⟩
128
+ @[irreducible] private def le (x y : ℝ) : Prop := x < y ∨ x = y
129
+ instance : has_le ℝ := ⟨le⟩
130
+ private lemma le_def {x y : ℝ} : x ≤ y ↔ x < y ∨ x = y := show le _ _ ↔ _, by rw le
78
131
79
132
@[simp] theorem mk_le {f g : cau_seq ℚ abs} : mk f ≤ mk g ↔ f ≤ g :=
80
- or_congr iff.rfl quotient.eq
133
+ by simp [le_def, mk_eq]; refl
134
+
135
+ @[elab_as_eliminator]
136
+ protected lemma ind_mk {C : real → Prop } (x : real) (h : ∀ y, C (mk y)) : C x :=
137
+ begin
138
+ cases x with x,
139
+ induction x using quot.induction_on with x,
140
+ exact h x
141
+ end
81
142
82
143
theorem add_lt_add_iff_left {a b : ℝ} (c : ℝ) : c + a < c + b ↔ a < b :=
83
- quotient.induction_on₃ a b c (λ f g h,
84
- iff_of_eq (congr_arg pos $ by rw add_sub_add_left_eq_sub))
144
+ begin
145
+ induction a using real.ind_mk,
146
+ induction b using real.ind_mk,
147
+ induction c using real.ind_mk,
148
+ simp only [mk_lt, ← mk_add],
149
+ show pos _ ↔ pos _, rw add_sub_add_left_eq_sub
150
+ end
85
151
86
152
instance : partial_order ℝ :=
87
153
{ le := (≤), lt := (<),
88
- le_refl := λ a, or.inr rfl,
89
- le_trans := λ a b c, quotient.induction_on₃ a b c $
90
- λ f g h, by simpa [quotient_mk_eq_mk] using le_trans,
91
- lt_iff_le_not_le := λ a b, quotient.induction_on₂ a b $
92
- λ f g, by simpa [quotient_mk_eq_mk] using lt_iff_le_not_le,
93
- le_antisymm := λ a b, quotient.induction_on₂ a b $
94
- λ f g, by simpa [mk_eq, quotient_mk_eq_mk] using @cau_seq.le_antisymm _ _ f g }
154
+ lt_iff_le_not_le := λ a b, real.ind_mk a $ λ a, real.ind_mk b $ λ b,
155
+ by simpa using lt_iff_le_not_le,
156
+ le_refl := λ a, a.ind_mk (by intro a; rw mk_le),
157
+ le_trans := λ a b c, real.ind_mk a $ λ a, real.ind_mk b $ λ b, real.ind_mk c $ λ c,
158
+ by simpa using le_trans,
159
+ lt_iff_le_not_le := λ a b, real.ind_mk a $ λ a, real.ind_mk b $ λ b,
160
+ by simpa using lt_iff_le_not_le,
161
+ le_antisymm := λ a b, real.ind_mk a $ λ a, real.ind_mk b $ λ b,
162
+ by simpa [mk_eq] using @cau_seq.le_antisymm _ _ a b }
95
163
96
164
instance : preorder ℝ := by apply_instance
97
165
98
- theorem of_rat_lt {x y : ℚ} : of_rat x < of_rat y ↔ x < y := const_lt
166
+ theorem of_rat_lt {x y : ℚ} : of_rat x < of_rat y ↔ x < y :=
167
+ begin
168
+ rw [mk_lt] {md := tactic.transparency.semireducible},
169
+ exact const_lt
170
+ end
99
171
100
- protected theorem zero_lt_one : (0 : ℝ) < 1 := of_rat_lt.2 zero_lt_one
172
+ protected theorem zero_lt_one : (0 : ℝ) < 1 :=
173
+ by convert of_rat_lt.2 zero_lt_one; simp
101
174
102
175
protected theorem mul_pos {a b : ℝ} : 0 < a → 0 < b → 0 < a * b :=
103
- quotient.induction_on₂ a b $ λ f g,
104
- show pos (f - 0 ) → pos (g - 0 ) → pos (f * g - 0 ),
105
- by simpa using cau_seq.mul_pos
176
+ begin
177
+ induction a using real.ind_mk with a,
178
+ induction b using real.ind_mk with b,
179
+ simpa only [mk_lt, mk_pos, ← mk_mul] using cau_seq.mul_pos
180
+ end
106
181
107
182
instance : ordered_ring ℝ :=
108
- { add_le_add_left := λ a b h c, h.imp (real.add_lt_add_iff_left c).2 (λ h, h ▸ rfl),
183
+ { add_le_add_left :=
184
+ begin
185
+ simp only [le_iff_eq_or_lt],
186
+ rintros a b ⟨rfl, h⟩,
187
+ { simp },
188
+ { exact λ c, or.inr ((add_lt_add_iff_left c).2 ‹_›) }
189
+ end ,
109
190
zero_le_one := le_of_lt real.zero_lt_one,
110
191
mul_pos := @real.mul_pos,
111
192
.. real.comm_ring, .. real.partial_order, .. real.semiring }
@@ -114,18 +195,17 @@ instance : ordered_semiring ℝ := by apply_instance
114
195
instance : ordered_add_comm_group ℝ := by apply_instance
115
196
instance : ordered_cancel_add_comm_monoid ℝ := by apply_instance
116
197
instance : ordered_add_comm_monoid ℝ := by apply_instance
117
- instance : has_one ℝ := by apply_instance
118
- instance : has_zero ℝ := by apply_instance
119
- instance : has_mul ℝ := by apply_instance
120
- instance : has_add ℝ := by apply_instance
121
- instance : has_sub ℝ := by apply_instance
122
198
instance : nontrivial ℝ := ⟨⟨0 , 1 , ne_of_lt real.zero_lt_one⟩⟩
123
199
124
200
open_locale classical
125
201
126
202
noncomputable instance : linear_order ℝ :=
127
- { le_total := λ a b, quotient.induction_on₂ a b $
128
- λ f g, by simpa [quotient_mk_eq_mk] using le_total f g,
203
+ { le_total := begin
204
+ intros a b,
205
+ induction a using real.ind_mk with a,
206
+ induction b using real.ind_mk with b,
207
+ simpa using le_total a b,
208
+ end ,
129
209
decidable_le := by apply_instance,
130
210
.. real.partial_order }
131
211
@@ -138,10 +218,21 @@ noncomputable instance : linear_ordered_semiring ℝ := by apply_instance
138
218
instance : domain ℝ :=
139
219
{ .. real.nontrivial, .. real.comm_ring, .. linear_ordered_ring.to_domain }
140
220
221
+ @[irreducible] private noncomputable def inv' : ℝ → ℝ | ⟨a⟩ := ⟨a⁻¹⟩
222
+ noncomputable instance : has_inv ℝ := ⟨inv'⟩
223
+ lemma inv_cauchy {f} : (⟨f⟩ : ℝ)⁻¹ = ⟨f⁻¹⟩ := show inv' _ = _, by rw inv'
224
+
141
225
noncomputable instance : linear_ordered_field ℝ :=
142
- { ..real.linear_ordered_comm_ring,
143
- ..real.domain,
144
- ..cau_seq.completion.field }
226
+ { inv := has_inv.inv,
227
+ mul_inv_cancel := begin
228
+ rintros ⟨a⟩ h,
229
+ rw mul_comm,
230
+ simp only [inv_cauchy, mul_cauchy, ← one_cauchy, ← zero_cauchy, ne.def] at *,
231
+ exact cau_seq.completion.inv_mul_cancel h,
232
+ end ,
233
+ inv_zero := by simp [← zero_cauchy, inv_cauchy],
234
+ ..real.linear_ordered_comm_ring,
235
+ ..real.domain }
145
236
146
237
/- Extra instances to short-circuit type class resolution -/
147
238
@@ -166,23 +257,30 @@ of_rat.eq_rat_cast
166
257
167
258
theorem le_mk_of_forall_le {f : cau_seq ℚ abs} :
168
259
(∃ i, ∀ j ≥ i, x ≤ f j) → x ≤ mk f :=
169
- quotient.induction_on x $ λ g h, le_of_not_lt $
170
- λ ⟨K, K0, hK⟩,
171
- let ⟨i, H⟩ := exists_forall_ge_and h $
172
- exists_forall_ge_and hK (f.cauchy₃ $ half_pos K0) in
173
260
begin
261
+ intro h,
262
+ induction x using real.ind_mk with x,
263
+ apply le_of_not_lt,
264
+ rw mk_lt,
265
+ rintro ⟨K, K0, hK⟩,
266
+ obtain ⟨i, H⟩ := exists_forall_ge_and h
267
+ (exists_forall_ge_and hK (f.cauchy₃ $ half_pos K0)),
174
268
apply not_lt_of_le (H _ (le_refl _)).1 ,
175
269
rw ← of_rat_eq_cast,
270
+ rw [mk_lt] {md := tactic.transparency.semireducible},
176
271
refine ⟨_, half_pos K0, i, λ j ij, _⟩,
177
272
have := add_le_add (H _ ij).2 .1
178
273
(le_of_lt (abs_lt.1 $ (H _ (le_refl _)).2 .2 _ ij).1 ),
179
274
rwa [← sub_eq_add_neg, sub_self_div_two, sub_apply, sub_add_sub_cancel] at this
180
275
end
181
276
182
- theorem mk_le_of_forall_le {f : cau_seq ℚ abs} {x : ℝ} :
183
- (∃ i, ∀ j ≥ i, (f j : ℝ) ≤ x) → mk f ≤ x
184
- | ⟨i, H⟩ := by rw [← neg_le_neg_iff, ← mk_eq_mk, mk_neg]; exact
185
- le_mk_of_forall_le ⟨i, λ j ij, by simp [H _ ij]⟩
277
+ theorem mk_le_of_forall_le {f : cau_seq ℚ abs} {x : ℝ}
278
+ (h : ∃ i, ∀ j ≥ i, (f j : ℝ) ≤ x) : mk f ≤ x :=
279
+ begin
280
+ cases h with i H,
281
+ rw [← neg_le_neg_iff, ← mk_neg],
282
+ exact le_mk_of_forall_le ⟨i, λ j ij, by simp [H _ ij]⟩
283
+ end
186
284
187
285
theorem mk_near_of_forall_near {f : cau_seq ℚ abs} {x : ℝ} {ε : ℝ}
188
286
(H : ∃ i, ∀ j ≥ i, abs ((f j : ℝ) - x) ≤ ε) : abs (mk f - x) ≤ ε :=
@@ -193,17 +291,11 @@ abs_sub_le_iff.2
193
291
H.imp $ λ i h j ij, sub_le.1 (abs_sub_le_iff.1 $ h j ij).2 ⟩
194
292
195
293
instance : archimedean ℝ :=
196
- archimedean_iff_rat_le.2 $ λ x, quotient.induction_on x $ λ f,
294
+ archimedean_iff_rat_le.2 $ λ x, real.ind_mk x $ λ f,
197
295
let ⟨M, M0, H⟩ := f.bounded' 0 in
198
296
⟨M, mk_le_of_forall_le ⟨0 , λ i _,
199
297
rat.cast_le.2 $ le_of_lt (abs_lt.1 (H i)).2 ⟩⟩
200
298
201
- /- mark `real` irreducible in order to prevent `auto_cases` unfolding reals,
202
- since users rarely want to consider real numbers as Cauchy sequences.
203
- Marking `comm_ring_aux` `irreducible` is done to ensure that there are no problems
204
- with non definitionally equal instances, caused by making `real` irreducible-/
205
- attribute [irreducible] real comm_ring_aux
206
-
207
299
noncomputable instance : floor_ring ℝ := archimedean.floor_ring _
208
300
209
301
theorem is_cau_seq_iff_lift {f : ℕ → ℚ} : is_cau_seq abs f ↔ is_cau_seq abs (λ i, (f i : ℝ)) :=
@@ -296,13 +388,9 @@ theorem Sup_le (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S,
296
388
by simp [Sup_def, h₁, h₂]; exact
297
389
classical.some_spec (exists_sup S h₁ h₂) y
298
390
299
- section
300
- -- this proof times out without this
301
- local attribute [instance, priority 1000 ] classical.prop_decidable
302
391
theorem lt_Sup (S : set ℝ) (h₁ : ∃ x, x ∈ S) (h₂ : ∃ x, ∀ y ∈ S, y ≤ x)
303
392
{y} : y < Sup S ↔ ∃ z ∈ S, y < z :=
304
393
by simpa [not_forall] using not_congr (@Sup_le S h₁ h₂ y)
305
- end
306
394
307
395
theorem le_Sup (S : set ℝ) (h₂ : ∃ x, ∀ y ∈ S, y ≤ x) {x} (xS : x ∈ S) : x ≤ Sup S :=
308
396
(Sup_le S ⟨_, xS⟩ h₂).1 (le_refl _) _ xS
410
498
411
499
noncomputable instance : cau_seq.is_complete ℝ abs := ⟨cau_seq_converges⟩
412
500
413
- attribute [irreducible] real.le
414
-
415
501
end real
0 commit comments