-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathSection_5_1.lean
More file actions
416 lines (336 loc) · 15.3 KB
/
Section_5_1.lean
File metadata and controls
416 lines (336 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import Mathlib.Tactic
import Analysis.Section_4_3
set_option doc.verso.suggestions false
/-!
# Analysis I, Section 5.1: Cauchy sequences
I have attempted to make the translation as faithful a paraphrasing as possible of the original
text. When there is a choice between a more idiomatic Lean solution and a more faithful
translation, I have generally chosen the latter. In particular, there will be places where the
Lean code could be "golfed" to be more elegant and idiomatic, but I have consciously avoided
doing so.
Main constructions and results of this section:
- Notion of a sequence of rationals
- Notions of `ε`-steadiness, eventual `ε`-steadiness, and Cauchy sequences
## Tips from past users
Users of the companion who have completed the exercises in this section are welcome to send their tips for future users in this section as PRs.
- (Add tip here)
-/
namespace Chapter5
/--
Definition 5.1.1 (Sequence). To avoid some technicalities involving dependent types, we extend
sequences by zero to the left of the starting point {name (full := Sequence.n₀)}`n₀`.
-/
@[ext]
structure Sequence where
n₀ : ℤ
seq : ℤ → ℚ
vanish : ∀ n < n₀, seq n = 0
/-- Sequences can be thought of as functions from {lean}`ℤ` to {lean}`ℚ`. -/
instance Sequence.instCoeFun : CoeFun Sequence (fun _ ↦ ℤ → ℚ) where
coe := fun a ↦ a.seq
/--
Functions from {lean}`ℕ` to {lean}`ℚ` can be thought of as sequences starting from 0; {name}`Sequence.ofNatFun` performs this conversion.
The {attr}`coe` attribute allows the delaborator to print {lean}`Sequence.ofNatFun f` as {lit}`↑f`, which is more concise; you may safely remove this if you prefer the more explicit notation.
-/
@[coe]
def Sequence.ofNatFun (f : ℕ → ℚ) : Sequence where
n₀ := 0
seq n := if n ≥ 0 then f n.toNat else 0
vanish := by grind
-- Notice how the delaborator prints this as `↑fun x ↦ ↑x ^ 2 : Sequence`.
#check Sequence.ofNatFun (· ^ 2)
/--
If {given}`a : ℕ → ℚ` is used in a context where a {name}`Sequence` is expected, automatically coerce {name}`a` to {lean}`Sequence.ofNatFun a` (which will be pretty-printed as {lean (type :="Sequence")}`↑a`).
-/
instance : Coe (ℕ → ℚ) Sequence where
coe := Sequence.ofNatFun
abbrev Sequence.mk' (n₀:ℤ) (a: { n // n ≥ n₀ } → ℚ) : Sequence where
n₀ := n₀
seq n := if h : n ≥ n₀ then a ⟨n, h⟩ else 0
vanish := by grind
lemma Sequence.eval_mk {n n₀:ℤ} (a: { n // n ≥ n₀ } → ℚ) (h: n ≥ n₀) :
(Sequence.mk' n₀ a) n = a ⟨ n, h ⟩ := by grind
@[simp]
lemma Sequence.eval_coe (n:ℕ) (a: ℕ → ℚ) : (a:Sequence) n = a n := by norm_cast
@[simp]
lemma Sequence.eval_coe_at_int (n:ℤ) (a: ℕ → ℚ) : (a:Sequence) n = if n ≥ 0 then a n.toNat else 0 := by norm_cast
@[simp]
lemma Sequence.n0_coe (a: ℕ → ℚ) : (a:Sequence).n₀ = 0 := by norm_cast
/-- Example 5.1.2 -/
abbrev Sequence.squares : Sequence := ((fun n:ℕ ↦ (n^2:ℚ)):Sequence)
/-- Example 5.1.2 -/
example (n:ℕ) : Sequence.squares n = n^2 := Sequence.eval_coe _ _
/-- Example 5.1.2 -/
abbrev Sequence.three : Sequence := ((fun (_:ℕ) ↦ (3:ℚ)):Sequence)
/-- Example 5.1.2 -/
example (n:ℕ) : Sequence.three n = 3 := Sequence.eval_coe _ (fun (_:ℕ) ↦ (3:ℚ))
/-- Example 5.1.2 -/
abbrev Sequence.squares_from_three : Sequence := mk' 3 (·^2)
/-- Example 5.1.2 -/
example (n:ℤ) (hn: n ≥ 3) : Sequence.squares_from_three n = n^2 := Sequence.eval_mk _ hn
-- need to temporarily leave the `Chapter5` namespace to introduce the following notation
end Chapter5
/--
A slight generalization of Definition 5.1.3 - definition of {name}`ε`-steadiness for a sequence with an
arbitrary starting point {lean}`a.n₀`
-/
abbrev Rat.Steady (ε: ℚ) (a: Chapter5.Sequence) : Prop :=
∀ n ≥ a.n₀, ∀ m ≥ a.n₀, ε.Close (a n) (a m)
lemma Rat.steady_def (ε: ℚ) (a: Chapter5.Sequence) :
ε.Steady a ↔ ∀ n ≥ a.n₀, ∀ m ≥ a.n₀, ε.Close (a n) (a m) := by rfl
namespace Chapter5
/--
Definition 5.1.3 - definition of {name}`ε`-steadiness for a sequence starting at 0
-/
lemma Rat.Steady.coe (ε : ℚ) (a:ℕ → ℚ) :
ε.Steady a ↔ ∀ n m : ℕ, ε.Close (a n) (a m) := by
constructor
· intro h n m; specialize h n ?_ m ?_ <;> simp_all
intro h n hn m hm
lift n to ℕ using hn
lift m to ℕ using hm
simp [h n m]
/--
Not in textbook: the sequence 3, 3 ... is 1-steady.
Intended as a demonstration of {name}`Rat.Steady.coe`.
-/
example : (1:ℚ).Steady ((fun _:ℕ ↦ (3:ℚ)):Sequence) := by
simp [Rat.Steady.coe, Rat.Close]
/--
{given -show}`hn : n ≥ 0, hm : m ≥ 0`
Compare: if you need to work with {name}`Rat.Steady` on the coercion directly, there will be side
conditions {lean}`hn : n ≥ 0` and {lean}`hm : m ≥ 0` that you will need to deal with.
-/
example : (1:ℚ).Steady ((fun _:ℕ ↦ (3:ℚ)):Sequence) := by
intro n _ m _; simp_all [Sequence.n0_coe, Sequence.eval_coe_at_int, Rat.Close]
/--
Example 5.1.5: The sequence `1, 0, 1, 0, ...` is 1-steady.
-/
example : (1:ℚ).Steady ((fun n:ℕ ↦ if Even n then (1:ℚ) else (0:ℚ)):Sequence) := by
rw [Rat.Steady.coe]
intro n m
-- Split into four cases based on whether n and m are even or odd
-- In each case, we know the exact value of a n and a m
split_ifs <;> simp [Rat.Close]
/--
Example 5.1.5: The sequence `1, 0, 1, 0, ...` is not ½-steady.
-/
example : ¬ (0.5:ℚ).Steady ((fun n:ℕ ↦ if Even n then (1:ℚ) else (0:ℚ)):Sequence) := by
rw [Rat.Steady.coe]
by_contra h; specialize h 0 1; simp [Rat.Close] at h
norm_num at h
/--
Example 5.1.5: The sequence 0.1, 0.01, 0.001, ... is 0.1-steady.
-/
example : (0.1:ℚ).Steady ((fun n:ℕ ↦ (10:ℚ) ^ (-(n:ℤ)-1) ):Sequence) := by
rw [Rat.Steady.coe]
intro n m; unfold Rat.Close
wlog h : m ≤ n
· specialize this m n (by linarith); rwa [abs_sub_comm]
rw [abs_sub_comm, abs_of_nonneg]
. rw [show (0.1:ℚ) = (10:ℚ)^(-1:ℤ) - 0 by norm_num]
gcongr <;> try grind
positivity
linarith [show (10:ℚ) ^ (-(n:ℤ)-1) ≤ (10:ℚ) ^ (-(m:ℤ)-1) by gcongr; norm_num]
/--
Example 5.1.5: The sequence 0.1, 0.01, 0.001, ... is not 0.01-steady. Left as an exercise.
-/
example : ¬(0.01:ℚ).Steady ((fun n:ℕ ↦ (10:ℚ) ^ (-(n:ℤ)-1) ):Sequence) := by sorry
/-- Example 5.1.5: The sequence 1, 2, 4, 8, ... is not ε-steady for any ε. Left as an exercise.
-/
example (ε:ℚ) : ¬ ε.Steady ((fun n:ℕ ↦ (2 ^ (n+1):ℚ) ):Sequence) := by sorry
/-- Example 5.1.5:The sequence 2, 2, 2, ... is ε-steady for any ε > 0.
-/
example (ε:ℚ) (hε: ε>0) : ε.Steady ((fun _:ℕ ↦ (2:ℚ) ):Sequence) := by
rw [Rat.Steady.coe]; simp [Rat.Close]; positivity
/--
The sequence 10, 0, 0, ... is 10-steady.
-/
example : (10:ℚ).Steady ((fun n:ℕ ↦ if n = 0 then (10:ℚ) else (0:ℚ)):Sequence) := by
rw [Rat.Steady.coe]; intro n m
-- Split into 4 cases based on whether n and m are 0 or not
split_ifs <;> simp [Rat.Close]
/--
The sequence 10, 0, 0, ... is not ε-steady for any smaller value of ε.
-/
example (ε:ℚ) (hε:ε<10): ¬ ε.Steady ((fun n:ℕ ↦ if n = 0 then (10:ℚ) else (0:ℚ)):Sequence) := by
contrapose! hε; rw [Rat.Steady.coe] at hε; specialize hε 0 1; simpa [Rat.Close] using hε
/--
{name}`Sequence.from` starts {lean}`a : Sequence` from {name}`n₁`. It is intended for use when {lean}`n₁ ≥ n₀`, but returns
the "junk" value of the original sequence {name}`a` otherwise.
-/
abbrev Sequence.from (a:Sequence) (n₁:ℤ) : Sequence :=
mk' (max a.n₀ n₁) (fun n ↦ a (n:ℤ))
lemma Sequence.from_eval (a:Sequence) {n₁ n:ℤ} (hn: n ≥ n₁) :
(a.from n₁) n = a n := by simp [hn]; intro h; exact (a.vanish _ h).symm
end Chapter5
/-- Definition 5.1.6 (Eventually ε-steady) -/
abbrev Rat.EventuallySteady (ε: ℚ) (a: Chapter5.Sequence) : Prop := ∃ N ≥ a.n₀, ε.Steady (a.from N)
lemma Rat.eventuallySteady_def (ε: ℚ) (a: Chapter5.Sequence) :
ε.EventuallySteady a ↔ ∃ N ≥ a.n₀, ε.Steady (a.from N) := by rfl
namespace Chapter5
/--
Example 5.1.7: The sequence 1, 1/2, 1/3, ... is not 0.1-steady
-/
lemma Sequence.ex_5_1_7_a : ¬ (0.1:ℚ).Steady ((fun n:ℕ ↦ (n+1:ℚ)⁻¹ ):Sequence) := by
intro h; rw [Rat.Steady.coe] at h; specialize h 0 2; simp [Rat.Close] at h; norm_num at h
/--
Example 5.1.7: The sequence `a_10, a_11, a_12, ...` is 0.1-steady
-/
lemma Sequence.ex_5_1_7_b : (0.1:ℚ).Steady (((fun n:ℕ ↦ (n+1:ℚ)⁻¹ ):Sequence).from 10) := by
rw [Rat.Steady]
intro n hn m hm; simp at hn hm
lift n to ℕ using (by omega)
lift m to ℕ using (by omega)
simp_all [Rat.Close]
wlog h : m ≤ n
· specialize this m n _ _ _ <;> try omega
rwa [abs_sub_comm] at this
rw [abs_sub_comm]
have : ((n:ℚ) + 1)⁻¹ ≤ ((m:ℚ) + 1)⁻¹ := by gcongr
rw [abs_of_nonneg (by linarith), show (0.1:ℚ) = (10:ℚ)⁻¹ - 0 by norm_num]
gcongr
· norm_cast; omega
positivity
/--
Example 5.1.7: The sequence 1, 1/2, 1/3, ... is eventually 0.1-steady
-/
lemma Sequence.ex_5_1_7_c : (0.1:ℚ).EventuallySteady ((fun n:ℕ ↦ (n+1:ℚ)⁻¹ ):Sequence) :=
⟨10, by simp, ex_5_1_7_b⟩
/--
Example 5.1.7
The sequence 10, 0, 0, ... is eventually ε-steady for every ε > 0. Left as an exercise.
-/
lemma Sequence.ex_5_1_7_d {ε:ℚ} (hε:ε>0) :
ε.EventuallySteady ((fun n:ℕ ↦ if n=0 then (10:ℚ) else (0:ℚ) ):Sequence) := by sorry
abbrev Sequence.IsCauchy (a:Sequence) : Prop := ∀ ε > (0:ℚ), ε.EventuallySteady a
lemma Sequence.isCauchy_def (a:Sequence) :
a.IsCauchy ↔ ∀ ε > (0:ℚ), ε.EventuallySteady a := by rfl
/-- Definition of Cauchy sequences, for a sequence starting at {lean}`0` -/
lemma Sequence.IsCauchy.coe (a:ℕ → ℚ) :
(a:Sequence).IsCauchy ↔ ∀ ε > (0:ℚ), ∃ N, ∀ j ≥ N, ∀ k ≥ N,
Section_4_3.dist (a j) (a k) ≤ ε := by
constructor <;> intro h ε hε
· choose N hN h' using h ε hε
lift N to ℕ using hN; use N
intro j _ k _; simp [Rat.steady_def] at h'; specialize h' j _ k _ <;> try omega
simp_all; exact h'
choose N h' using h ε hε
refine ⟨ max N 0, by simp, ?_ ⟩
intro n hn m hm; simp at hn hm
have npos : 0 ≤ n := ?_
have mpos : 0 ≤ m := ?_
lift n to ℕ using npos
lift m to ℕ using mpos
simp [hn, hm]; specialize h' n _ m _
all_goals try omega
norm_cast
lemma Sequence.IsCauchy.mk {n₀:ℤ} (a: {n // n ≥ n₀} → ℚ) :
(mk' n₀ a).IsCauchy ↔ ∀ ε > (0:ℚ), ∃ N ≥ n₀, ∀ j ≥ N, ∀ k ≥ N,
Section_4_3.dist (mk' n₀ a j) (mk' n₀ a k) ≤ ε := by
constructor <;> intro h ε hε <;> choose N hN h' using h ε hε
· refine ⟨ N, hN, ?_ ⟩; dsimp at hN; intro j _ k _
simp only [Rat.Steady, show max n₀ N = N by omega] at h'
specialize h' j _ k _ <;> try omega
simp_all [show n₀ ≤ j by omega, show n₀ ≤ k by omega]
exact h'
refine ⟨ max n₀ N, by simp, ?_ ⟩
intro n _ m _; simp_all
apply h' n _ m <;> omega
noncomputable def Sequence.sqrt_two : Sequence := (fun n:ℕ ↦ ((⌊ (Real.sqrt 2)*10^n ⌋ / 10^n):ℚ))
/--
Example 5.1.10. (This requires extensive familiarity with Mathlib's API for the real numbers.)
-/
theorem Sequence.ex_5_1_10_a : (1:ℚ).Steady sqrt_two := by sorry
/--
Example 5.1.10. (This requires extensive familiarity with Mathlib's API for the real numbers.)
-/
theorem Sequence.ex_5_1_10_b : (0.1:ℚ).Steady (sqrt_two.from 1) := by sorry
theorem Sequence.ex_5_1_10_c : (0.1:ℚ).EventuallySteady sqrt_two := by sorry
/-- Proposition 5.1.11. The harmonic sequence, defined as a₁ = 1, a₂ = 1/2, ... is a Cauchy sequence. -/
theorem Sequence.IsCauchy.harmonic : (mk' 1 (fun n ↦ (1:ℚ)/n)).IsCauchy := by
rw [IsCauchy.mk]
intro ε hε
-- We go by reverse from the book - first choose N such that N > 1/ε
obtain ⟨ N, hN : N > 1/ε ⟩ := exists_nat_gt (1 / ε)
have hN' : N > 0 := by
observe : (1/ε) > 0
observe : (N:ℚ) > 0
norm_cast at this
refine ⟨ N, by norm_cast, ?_ ⟩
intro j hj k hk
lift j to ℕ using (by linarith)
lift k to ℕ using (by linarith)
norm_cast at hj hk
simp [show j ≥ 1 by linarith, show k ≥ 1 by linarith]
have hdist : Section_4_3.dist ((1:ℚ)/j) ((1:ℚ)/k) ≤ (1:ℚ)/N := by
rw [Section_4_3.dist_eq, abs_le']
/-
We establish the following bounds:
- 1/j ∈ [0, 1/N]
- 1/k ∈ [0, 1/N]
These imply that the distance between 1/j and 1/k is at most 1/N - when they are as "far apart" as possible.
-/
have : 1/j ≤ (1:ℚ)/N := by gcongr
observe : (0:ℚ) ≤ 1/j
have : 1/k ≤ (1:ℚ)/N := by gcongr
observe : (0:ℚ) ≤ 1/k
grind
simp at *; apply hdist.trans
rw [inv_le_comm₀] <;> try positivity
order
abbrev BoundedBy {n:ℕ} (a: Fin n → ℚ) (M:ℚ) : Prop := ∀ i, |a i| ≤ M
/--
Definition 5.1.12 (bounded sequences). Here we start sequences from {lean}`0` rather than {lean}`1` to align
better with Mathlib conventions.
-/
lemma boundedBy_def {n:ℕ} (a: Fin n → ℚ) (M:ℚ) : BoundedBy a M ↔ ∀ i, |a i| ≤ M := by rfl
abbrev Sequence.BoundedBy (a:Sequence) (M:ℚ) : Prop := ∀ n, |a n| ≤ M
/-- Definition 5.1.12 (bounded sequences) -/
lemma Sequence.boundedBy_def (a:Sequence) (M:ℚ) : a.BoundedBy M ↔ ∀ n, |a n| ≤ M := by rfl
abbrev Sequence.IsBounded (a:Sequence) : Prop := ∃ M ≥ 0, a.BoundedBy M
/-- Definition 5.1.12 (bounded sequences) -/
lemma Sequence.isBounded_def (a:Sequence) : a.IsBounded ↔ ∃ M ≥ 0, a.BoundedBy M := by rfl
/-- Example 5.1.13 -/
example : BoundedBy ![1,-2,3,-4] 4 := by intro i; fin_cases i <;> norm_num
/-- Example 5.1.13 -/
example : ¬((fun n:ℕ ↦ (-1)^n * (n+1:ℚ)):Sequence).IsBounded := by sorry
/-- Example 5.1.13 -/
example : ((fun n:ℕ ↦ (-1:ℚ)^n):Sequence).IsBounded := by
refine ⟨ 1, by norm_num, ?_ ⟩; intro i; by_cases h: 0 ≤ i <;> simp [h]
/-- Example 5.1.13 -/
example : ¬((fun n:ℕ ↦ (-1:ℚ)^n):Sequence).IsCauchy := by
rw [Sequence.IsCauchy.coe]
by_contra h; specialize h (1/2 : ℚ) (by norm_num)
choose N h using h; specialize h N _ (N+1) _ <;> try omega
by_cases h': Even N
· simp [h'.neg_one_pow, (h'.add_one).neg_one_pow, Section_4_3.dist] at h
norm_num at h
observe h₁: Odd N
observe h₂: Even (N+1)
simp [h₁.neg_one_pow, h₂.neg_one_pow, Section_4_3.dist] at h
norm_num at h
/-- Lemma 5.1.14 -/
lemma IsBounded.finite {n:ℕ} (a: Fin n → ℚ) : ∃ M ≥ 0, BoundedBy a M := by
-- this proof is written to follow the structure of the original text.
induction' n with n hn
. use 0; simp
set a' : Fin n → ℚ := fun m ↦ a m.castSucc
choose M hpos hM using hn a'
have h1 : BoundedBy a' (M + |a (Fin.ofNat _ n)|) := fun m ↦ (hM m).trans (by simp)
have h2 : |a (Fin.ofNat _ n)| ≤ M + |a (Fin.ofNat _ n)| := by simp [hpos]
refine ⟨ M + |a (Fin.ofNat _ n)|, by positivity, ?_ ⟩
intro m; obtain ⟨ j, rfl ⟩ | rfl := Fin.eq_castSucc_or_eq_last m
. grind
convert h2; simp
/-- Lemma 5.1.15 (Cauchy sequences are bounded) / Exercise 5.1.1 -/
lemma Sequence.isBounded_of_isCauchy {a:Sequence} (h: a.IsCauchy) : a.IsBounded := by
sorry
/-- Exercise 5.1.2 -/
theorem Sequence.isBounded_add {a b:ℕ → ℚ} (ha: (a:Sequence).IsBounded) (hb: (b:Sequence).IsBounded):
(a + b:Sequence).IsBounded := by sorry
theorem Sequence.isBounded_sub {a b:ℕ → ℚ} (ha: (a:Sequence).IsBounded) (hb: (b:Sequence).IsBounded):
(a - b:Sequence).IsBounded := by sorry
theorem Sequence.isBounded_mul {a b:ℕ → ℚ} (ha: (a:Sequence).IsBounded) (hb: (b:Sequence).IsBounded):
(a * b:Sequence).IsBounded := by sorry
end Chapter5