-
Notifications
You must be signed in to change notification settings - Fork 1
/
cauchy_real.lean
1713 lines (1486 loc) · 74.1 KB
/
cauchy_real.lean
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import algebra.archimedean data.rat algebra.module
theorem congr_arg₂ {α β γ : Type*} (f : α → β → γ) {x₁ x₂ : α} {y₁ y₂ : β}
(Hx : x₁ = x₂) (Hy : y₁ = y₂) : f x₁ y₁ = f x₂ y₂ :=
eq.drec (eq.drec rfl Hy) Hx
section rat
theorem lt_two_pow (n : nat) : n < 2 ^ n :=
nat.rec_on n dec_trivial $ λ n ih,
calc n + 1
< 2^n + 1 : nat.add_lt_add_right ih 1
... ≤ 2^n + 2^n : nat.add_le_add_left (nat.pow_le_pow_of_le_right dec_trivial $ nat.zero_le n) (2^n)
... = 2^n * 2 : eq.symm $ mul_two (2^n)
... = 2^(n+1) : eq.symm $ nat.pow_succ 2 n
theorem rat.coe_pow (m n : nat) : (m : ℚ) ^ n = (m^n : ℕ) :=
nat.rec_on n rfl $ λ n ih, by simp [pow_succ', ih, nat.pow_succ]
theorem rat.num_pos_of_pos (r : rat) (H : r > 0) : r.num > 0 :=
int.cast_lt.1 $
calc (r.num : ℚ)
= r.num / (r.denom:ℤ) * r.denom : eq.symm $ div_mul_cancel _ $ ne_of_gt $ nat.cast_pos.2 r.pos
... = r * r.denom : by rw [← rat.mk_eq_div, ← rat.num_denom r]
... > 0 : mul_pos H $ nat.cast_pos.2 r.pos
theorem rat.one_le_num_of_pos (r : rat) (H : r > 0) : 1 ≤ (r.num : ℚ) :=
have H1 : ((0+1:ℤ):ℚ) = (1:ℚ), from rfl,
H1 ▸ int.cast_le.2 $ int.add_one_le_of_lt $ rat.num_pos_of_pos r H
theorem rat.lt (r : ℚ) (H : r > 0) : (1 / 2^r.denom : ℚ) < r :=
calc (1 / 2^r.denom : ℚ)
< 1 / r.denom : one_div_lt_one_div_of_lt (nat.cast_pos.2 r.pos)
(trans_rel_left _ (nat.cast_lt.2 $ lt_two_pow _) (rat.coe_pow 2 _).symm)
... ≤ r.num / r.denom : div_le_div_of_le_of_pos (rat.one_le_num_of_pos r H) (nat.cast_pos.2 r.pos)
... = r.num / (r.denom:ℤ) : rfl
... = r : by rw [← rat.mk_eq_div, ← rat.num_denom r]
end rat
section list_max_min
variables {α : Type*} [decidable_linear_order α] [inhabited α] (L : list α)
def list.max : α :=
list.foldr max (inhabited.default _) L
def list.min : α :=
list.foldr min (inhabited.default _) L
theorem list.le_max : ∀ x ∈ L, x ≤ L.max :=
list.rec_on L (λ _, false.elim) $ λ hd tl ih x hx,
or.cases_on hx
(assume H : x = hd, H.symm ▸ le_max_left hd tl.max)
(assume H : x ∈ tl, le_trans (ih x H) (le_max_right hd tl.max))
theorem list.min_le : ∀ x ∈ L, L.min ≤ x :=
list.rec_on L (λ _, false.elim) $ λ hd tl ih x hx,
or.cases_on hx
(assume H : x = hd, H.symm ▸ min_le_left hd tl.min)
(assume H : x ∈ tl, le_trans (min_le_right hd tl.min) (ih x H))
end list_max_min
instance rat.seq : comm_ring (ℕ → ℚ) :=
by refine
{ add := λ f g n, f n + g n,
zero := λ n, 0,
neg := λ f n, -f n,
mul := λ f g n, f n * g n,
one := λ n, 1,
.. };
{ intros,
{ simp [mul_assoc, mul_add, add_mul] }
<|> simp [mul_comm] }
def rat.cau_seq : set (ℕ → ℚ) :=
{ f : ℕ → ℚ | ∀ ε > 0, ∃ N, ∀ m > N, ∀ n > N, abs (f m - f n) < ε }
namespace rat.cau_seq
variables (f g : ℕ → ℚ) (hf : f ∈ rat.cau_seq) (hg : g ∈ rat.cau_seq)
theorem add : f + g ∈ rat.cau_seq := λ ε Hε,
let ⟨n1, h1⟩ := hf (ε/2) (half_pos Hε) in
let ⟨n2, h2⟩ := hg (ε/2) (half_pos Hε) in
⟨max n1 n2, λ m hm n hn,
have H1 : _ := h1 m (lt_of_le_of_lt (le_max_left _ _) hm)
n (lt_of_le_of_lt (le_max_left _ _) hn),
have H2 : _ := h2 m (lt_of_le_of_lt (le_max_right _ _) hm)
n (lt_of_le_of_lt (le_max_right _ _) hn),
calc abs ((f m + g m) - (f n + g n))
= abs ((f m - f n) + (g m - g n)) : by simp
... ≤ abs (f m - f n) + abs (g m - g n) : abs_add _ _
... < (ε/2) + (ε/2) : add_lt_add H1 H2
... = ε : add_halves _⟩
theorem mul : f * g ∈ rat.cau_seq := λ ε Hε,
let ⟨n1, h1⟩ := hf ε Hε in
let ⟨n2, h2⟩ := hg ε Hε in
have H1 : ε + abs (f (n1 + 1)) > 0,
from add_pos_of_pos_of_nonneg Hε $ abs_nonneg _,
have H2 : ε + abs (g (n2 + 1)) > 0,
from add_pos_of_pos_of_nonneg Hε $ abs_nonneg _,
let ⟨n3, h3⟩ := hf (ε/2 / (ε + abs (g (n2 + 1))))
(div_pos (half_pos Hε) H2) in
let ⟨n4, h4⟩ := hg (ε/2 / (ε + abs (f (n1 + 1))))
(div_pos (half_pos Hε) H1) in
⟨max (max n1 n2) (max n3 n4), λ m hm n hn,
have H3 : _ := h1 n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_left _ _)) hn)
(n1 + 1) (nat.lt_succ_self n1),
have H4 : _ := h2 m (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_left _ _)) hm)
(n2 + 1) (nat.lt_succ_self n2),
have H5 : _ := h3 m (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hm)
n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hn),
have H6 : _ := h4 m (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hm)
n (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hn),
calc abs ((f m * g m) - (f n * g n))
= abs ((f m - f n) * g m + f n * (g m - g n)) : by simp [add_mul, mul_add]
... ≤ abs ((f m - f n) * g m) + abs (f n * (g m - g n)) : abs_add _ _
... = abs (f m - f n) * abs (g m) + abs (f n) * abs (g m - g n) : by rw [abs_mul, abs_mul]
... = abs (f m - f n) * abs (g m - g (n2 + 1) + g (n2 + 1)) + abs (f n - f (n1 + 1) + f (n1 + 1)) * abs (g m - g n) : by simp
... ≤ abs (f m - f n) * (abs (g m - g (n2 + 1)) + abs (g (n2 + 1))) + (abs (f n - f (n1 + 1)) + abs (f (n1 + 1))) * abs (g m - g n) :
add_le_add (mul_le_mul_of_nonneg_left (abs_add _ _) (abs_nonneg _)) (mul_le_mul_of_nonneg_right (abs_add _ _) (abs_nonneg _))
... ≤ abs (f m - f n) * (ε + abs (g (n2 + 1))) + (ε + abs (f (n1 + 1))) * abs (g m - g n) :
add_le_add (mul_le_mul_of_nonneg_left (le_of_lt $ add_lt_add_right H4 _) (abs_nonneg _)) (mul_le_mul_of_nonneg_right (le_of_lt $ add_lt_add_right H3 _) (abs_nonneg _))
... < (ε/2 / (ε + abs (g (n2 + 1)))) * (ε + abs (g (n2 + 1))) + (ε + abs (f (n1 + 1))) * (ε/2 / (ε + abs (f (n1 + 1)))) :
add_lt_add (mul_lt_mul_of_pos_right H5 H2) (mul_lt_mul_of_pos_left H6 H1)
... = ε/2 + ε/2 : by rw [div_mul_cancel _ (ne_of_gt H2), mul_div_cancel' _ (ne_of_gt H1)]
... = ε : add_halves _⟩
theorem neg_one : (-1 : ℕ → ℚ) ∈ rat.cau_seq :=
λ ε Hε, ⟨0, λ m hm n hn, show abs (-1 - (-1)) < ε, by simpa using Hε⟩
instance : comm_ring rat.cau_seq :=
by refine
{ add := λ f g, ⟨f.1 + g.1, add _ _ f.2 g.2⟩,
zero := ⟨0, have H : (-1 : ℕ → ℚ) + (-1) * (-1) = 0, by simp,
H ▸ add _ _ neg_one $ mul _ _ neg_one neg_one⟩,
neg := λ f, ⟨-f.1, have H : (-1) * f.1 = -f.1, by simp,
H ▸ mul _ _ neg_one f.2⟩,
mul := λ f g, ⟨f.1 * g.1, mul _ _ f.2 g.2⟩,
one := ⟨1, have H : (-1 : ℕ → ℚ) * (-1) = 1, by simp,
H ▸ mul _ _ neg_one neg_one⟩,
.. };
{ intros,
{ simp [mul_assoc, mul_add, add_mul] }
<|> simp [mul_comm] }
protected def abs : rat.cau_seq :=
⟨λ n, abs (f n), λ ε Hε,
let ⟨N, HN⟩ := hf ε Hε in
⟨N, λ m hm n hn, lt_of_le_of_lt
(abs_abs_sub_abs_le_abs_sub _ _)
(HN m hm n hn)⟩⟩
end rat.cau_seq
def rat.null : set rat.cau_seq :=
{ f : rat.cau_seq | ∀ ε > 0, ∃ N, ∀ n > N, abs (f.1 n) < ε }
namespace rat.null
variables (f g : rat.cau_seq) (hf : f ∈ rat.null) (hg : g ∈ rat.null)
theorem add : f + g ∈ rat.null := λ ε Hε,
let ⟨n1, h1⟩ := hf (ε/2) (half_pos Hε) in
let ⟨n2, h2⟩ := hg (ε/2) (half_pos Hε) in
⟨max n1 n2, λ n hn,
have H1 : _ := h1 n (lt_of_le_of_lt (le_max_left _ _) hn),
have H2 : _ := h2 n (lt_of_le_of_lt (le_max_right _ _) hn),
calc abs (f.1 n + g.1 n)
≤ abs (f.1 n) + abs (g.1 n) : abs_add _ _
... < (ε/2) + (ε/2) : add_lt_add H1 H2
... = ε : add_halves _⟩
theorem zero : (0 : rat.cau_seq) ∈ rat.null :=
λ ε Hε, ⟨0, λ n hn, show abs 0 < ε, by simpa using Hε⟩
theorem mul : f * g ∈ rat.null := λ ε Hε,
let ⟨n1, h1⟩ := f.2 ε Hε in
have H1 : ε + abs (f.1 (n1 + 1)) > 0,
from add_pos_of_pos_of_nonneg Hε $ abs_nonneg _,
let ⟨n2, h2⟩ := hg (ε / (ε + abs (f.1 (n1 + 1)))) (div_pos Hε H1) in
⟨max n1 n2, λ n hn,
have H2 : _ := h1 n (lt_of_le_of_lt (le_max_left _ _) hn)
(n1 + 1) (nat.lt_succ_self n1),
have H3 : _ := h2 n (lt_of_le_of_lt (le_max_right _ _) hn),
calc abs (f.1 n * g.1 n)
= abs (f.1 n) * abs (g.1 n) : abs_mul _ _
... = abs (f.1 n - f.1 (n1 + 1) + f.1 (n1 + 1)) * abs (g.1 n) : by simp
... ≤ (abs (f.1 n - f.1 (n1 + 1)) + abs (f.1 (n1 + 1))) * abs (g.1 n) :
mul_le_mul_of_nonneg_right (abs_add _ _) (abs_nonneg _)
... ≤ (ε + abs (f.1 (n1 + 1))) * abs (g.1 n) :
mul_le_mul_of_nonneg_right (add_le_add_right (le_of_lt H2) _) (abs_nonneg _)
... < (ε + abs (f.1 (n1 + 1))) * (ε / (ε + abs (f.val (n1 + 1)))) :
mul_lt_mul_of_pos_left H3 H1
... = ε : mul_div_cancel' _ $ ne_of_gt H1⟩
protected theorem abs : rat.cau_seq.abs f.1 f.2 ∈ rat.null := λ ε Hε,
let ⟨N, HN⟩ := hf ε Hε in
⟨N, λ n hn, show abs (abs (f.1 n)) < ε,
from (abs_abs (f.1 n)).symm ▸ HN n hn⟩
theorem of_abs (HF : rat.cau_seq.abs f.1 f.2 ∈ rat.null)
: f ∈ rat.null := λ ε Hε,
let ⟨N, HN⟩ := HF ε Hε in
⟨N, λ n hn, (abs_abs (f.1 n)) ▸ HN n hn⟩
local attribute [instance] classical.prop_decidable
theorem abs_pos_of_not_null (H : f ∉ rat.null) :
∃ ε > 0, ∃ N, ∀ n > N, abs (f.1 n) > ε :=
let ⟨ε, Hε⟩ := not_forall.1 H in
let ⟨Hε1, Hε2⟩ := not_imp.1 Hε in
let ⟨N1, HN1⟩ := f.2 (ε/2) (half_pos Hε1) in
let ⟨N2, HN2⟩ := not_forall.1 $ not_exists.1 Hε2 N1 in
let ⟨HN3, HN4⟩ := not_imp.1 HN2 in
⟨ε/2, half_pos Hε1, N2, λ n hn,
have H : _ := HN1 n (lt_trans HN3 hn) N2 HN3,
calc abs (f.1 n)
= abs (f.1 N2 - (f.1 N2 - f.1 n)) : congr_arg abs $ eq.symm $ sub_sub_cancel _ _
... ≥ abs (f.1 N2) - abs (f.1 N2 - f.1 n) : abs_sub_abs_le_abs_sub _ _
... > ε - ε/2 : sub_lt_sub_of_le_of_lt (le_of_not_gt HN4) (abs_sub (f.1 n) (f.1 N2) ▸ H)
... = ε / 2 : sub_half ε⟩
end rat.null
instance real.setoid : setoid rat.cau_seq :=
⟨λ f g, f - g ∈ rat.null,
λ f, by simpa using rat.null.zero,
λ f g hfg, have H : (-1) * (f - g) = g - f, by simp,
show g - f ∈ rat.null, from H ▸ rat.null.mul _ _ hfg,
λ f g h h1 h2, have H : (f - g) + (g - h) = f - h, by simp,
show f - h ∈ rat.null, from H ▸ rat.null.add _ _ h1 h2⟩
def real : Type :=
quotient real.setoid
theorem real.eq_of_eq : ∀ {x y : rat.cau_seq}, x = y → ⟦x⟧ = ⟦y⟧
| _ _ rfl := quotient.sound $ setoid.refl _
instance real.comm_ring : comm_ring real :=
by refine
{ add := λ x y, quotient.lift_on₂ x y (λ f g, ⟦f + g⟧) $
λ f1 f2 g1 g2 hf hg, quotient.sound $
have H : (f1 - g1) + (f2 - g2) = (f1 + f2) - (g1 + g2), by simp,
show (f1 + f2) - (g1 + g2) ∈ rat.null,
from H ▸ rat.null.add _ _ hf hg,
zero := ⟦0⟧,
neg := λ x, quotient.lift_on x (λ f, ⟦-f⟧) $
λ f1 f2 hf, quotient.sound $
have H : (-1) * (f1 - f2) = (-f1) - (-f2), by simp,
show (-f1) - (-f2) ∈ rat.null,
from H ▸ rat.null.mul _ _ hf,
mul := λ x y, quotient.lift_on₂ x y (λ f g, ⟦f * g⟧) $
λ f1 f2 g1 g2 hf hg, quotient.sound $
have H : f2 * (f1 - g1) + g1 * (f2 - g2) = f1 * f2 - g1 * g2,
by simp [mul_add, add_mul, mul_comm],
show f1 * f2 - g1 * g2 ∈ rat.null,
from H ▸ rat.null.add _ _ (rat.null.mul _ _ hf) (rat.null.mul _ _ hg),
one := ⟦1⟧,
.. };
{ intros,
try { apply quotient.induction_on a, intro f },
try { apply quotient.induction_on b, intro g },
try { apply quotient.induction_on c, intro h },
apply real.eq_of_eq,
{ simp [mul_assoc, mul_add, add_mul] }
<|> simp [mul_comm] }
namespace rat.cau_seq
variables (f g h : ℕ → ℚ)
def lt : Prop :=
∃ ε > 0, ∃ N, ∀ n > N, f n + ε < g n
protected theorem lt_trans (H1 : lt f g) (H2 : lt g h) : lt f h :=
let ⟨ε1, Hε1, N1, HN1⟩ := H1 in
let ⟨ε2, Hε2, N2, HN2⟩ := H2 in
⟨ε1, Hε1, max N1 N2, λ n hn,
have H3 : n > N1 := (lt_of_le_of_lt (le_max_left _ _) hn),
have H4 : n > N2 := (lt_of_le_of_lt (le_max_right _ _) hn),
calc f n + ε1
< g n : HN1 n H3
... < g n + ε2 : lt_add_of_pos_right _ Hε2
... < h n : HN2 n H4⟩
theorem lt_asymm (H1 : lt f g) (H2 : lt g f) : false :=
let ⟨ε1, Hε1, N1, HN1⟩ := H1 in
let ⟨ε2, Hε2, N2, HN2⟩ := H2 in
have H1 : _ := HN1 (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_right _ _),
have H2 : _ := HN2 (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_left _ _),
lt_asymm
(lt_trans (lt_add_of_pos_right _ Hε1) H1)
(lt_trans (lt_add_of_pos_right _ Hε2) H2)
protected theorem add_lt_add_left (H : lt g h) : lt (f + g) (f + h) :=
let ⟨ε, Hε, N, HN⟩ := H in
⟨ε, Hε, N, λ n hn, show f n + g n + ε < f n + h n,
from (add_assoc (f n) (g n) ε).symm ▸ add_lt_add_left (HN n hn) _⟩
protected theorem mul_pos (Hf : lt 0 f) (Hg : lt 0 g) : lt 0 (f * g) :=
let ⟨ε1, Hε1, N1, HN1⟩ := Hf in
let ⟨ε2, Hε2, N2, HN2⟩ := Hg in
⟨ε1 * ε2, mul_pos Hε1 Hε2, max N1 N2, λ n hn,
have H1 : n > N1 := (lt_of_le_of_lt (le_max_left _ _) hn),
have H2 : n > N2 := (lt_of_le_of_lt (le_max_right _ _) hn),
have H3 : ε1 < f n := (zero_add ε1) ▸ HN1 n H1,
have H4 : ε2 < g n := (zero_add ε2) ▸ HN2 n H2,
show 0 + ε1 * ε2 < f n * g n,
from (zero_add $ ε1 * ε2).symm ▸ mul_lt_mul H3 (le_of_lt H4)
Hε2 (le_of_lt (lt_trans Hε1 H3))⟩
theorem pos_or_neg_of_not_null (f : rat.cau_seq) (H : f ∉ rat.null) :
lt 0 f.1 ∨ lt f.1 0 :=
let ⟨ε, Hε, N1, HN1⟩ := rat.null.abs_pos_of_not_null f H in
let ⟨N2, HN2⟩ := f.2 (ε/2) (half_pos Hε) in
have H1 : _ := HN1 (N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_right _ _),
or.cases_on (lt_max_iff.1 H1)
(assume H : ε < f.1 (N1 + N2 + 1), or.inl ⟨ε/2, half_pos Hε, N1 + N2, λ n hn,
have H2 : _ := HN2 n (lt_of_le_of_lt (nat.le_add_left _ _) hn)
(N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_left _ _),
calc 0 + ε/2
= ε/2 : zero_add _
... = ε - ε/2 : eq.symm $ sub_half ε
... < f.1 (N1 + N2 + 1) - -(f.1 n - f.1 (N1 + N2 + 1)) : sub_lt_sub H (neg_lt.1 (abs_lt.1 H2).1)
... = f.1 (N1 + N2 + 1) + (f.1 n - f.1 (N1 + N2 + 1)) : sub_neg_eq_add _ _
... = f.1 n : add_sub_cancel'_right _ _⟩)
(assume H : ε < -f.1 (N1 + N2 + 1), or.inr ⟨ε/2, half_pos Hε, N1 + N2, λ n hn,
have H2 : _ := HN2 n (lt_of_le_of_lt (nat.le_add_left _ _) hn)
(N1 + N2 + 1) (nat.succ_le_succ $ nat.le_add_left _ _),
calc f.1 n + ε/2
= f.1 n - f.1 (N1 + N2 + 1) + f.1 (N1 + N2 + 1) + ε/2 : by rw sub_add_cancel
... < ε/2 + -ε + ε/2 : add_lt_add_right (add_lt_add (abs_lt.1 H2).2 (lt_neg_of_lt_neg H)) _
... = ε/2 + ε/2 + -ε : add_right_comm _ _ _
... = 0 : by rw [add_halves, add_neg_self]⟩)
variables {f1 f2 g1 g2 : rat.cau_seq}
variables (hf : f1 ≈ g1)
variables (hg : f2 ≈ g2)
theorem lt_of_lt (H : lt f1.1 f2.1) : lt g1.1 g2.1 :=
let ⟨ε, Hε, N, HN⟩ := H in
let ⟨N1, HN1⟩ := hf (ε/2/2) (half_pos $ half_pos Hε) in
let ⟨N2, HN2⟩ := hg (ε/2/2) (half_pos $ half_pos Hε) in
⟨ε/2, half_pos Hε, max N (max N1 N2), λ n hn,
have H1 : _ := HN n (lt_of_le_of_lt (le_max_left _ _) hn),
have H2 : _ := HN1 n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hn),
have H3 : _ := HN2 n (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hn),
calc g1.1 n + ε/2
= f1.1 n - (f1.1 n - g1.1 n) + ε/2 : by rw sub_sub_cancel
... < f1.1 n - -(ε/2/2) + ε/2 : add_lt_add_right (sub_lt_sub_left (abs_lt.1 H2).1 _) _
... = f1.1 n + (ε/2/2 + (ε/2/2 + ε/2/2) + ε/2/2 - ε/2/2) : by rw [sub_neg_eq_add, add_halves, add_sub_cancel, add_assoc]
... = f1.1 n + ((ε/2/2 + ε/2/2) + (ε/2/2 + ε/2/2) - ε/2/2) : by repeat {rw add_assoc}
... = f1.1 n + (ε - ε/2/2) : by repeat {rw add_halves}
... = (f1.1 n + ε) - ε/2/2 : eq.symm $ add_sub_assoc _ _ _
... < f2.1 n - (f2.1 n - g2.1 n) : sub_lt_sub H1 (abs_lt.1 H3).2
... = g2.1 n : sub_sub_cancel _ _⟩
end rat.cau_seq
namespace real
def lt (x y : real) : Prop :=
quotient.lift_on₂ x y (λ f g, rat.cau_seq.lt f.1 g.1) $
λ f1 f2 g1 g2 hf hg, propext ⟨λ H, rat.cau_seq.lt_of_lt hf hg H,
λ H, rat.cau_seq.lt_of_lt (setoid.symm hf) (setoid.symm hg) H⟩
protected theorem lt_trans (x y z : real) : lt x y → lt y z → lt x z :=
quotient.induction_on₃ x y z $ λ f g h H1 H2,
rat.cau_seq.lt_trans f.1 g.1 h.1 H1 H2
theorem lt_asymm (x y : real) : lt x y → lt y x → false :=
quotient.induction_on₂ x y $ λ f g H1 H2,
rat.cau_seq.lt_asymm f.1 g.1 H1 H2
theorem lt_trichotomy (x y : real) : lt x y ∨ x = y ∨ lt y x :=
classical.by_cases (assume h : x = y, or.inr $ or.inl h) $
quotient.induction_on₂ x y $ λ f g, assume h : ⟦f⟧ ≠ ⟦g⟧,
or.cases_on (rat.cau_seq.pos_or_neg_of_not_null (f - g) (λ H, h $ quotient.sound H))
(assume H : rat.cau_seq.lt 0 (f - g), or.inr $ or.inr $
have H1 : _ := rat.cau_seq.add_lt_add_left g _ _ H,
by simpa using H1)
(assume H : rat.cau_seq.lt (f - g) 0, or.inl $
have H1 : _ := rat.cau_seq.add_lt_add_left g _ _ H,
by simpa using H1)
theorem mul_pos (x y : real) : lt 0 x → lt 0 y → lt 0 (x * y) :=
quotient.induction_on₂ x y $ λ f g hf hg,
rat.cau_seq.mul_pos f g hf hg
end real
instance real.partial_order : partial_order real :=
{ lt := real.lt,
le := λ x y, real.lt x y ∨ x = y,
lt_iff_le_not_le := λ x y,
⟨assume hxy : real.lt x y, ⟨or.inl hxy,
assume hyx : real.lt y x ∨ y = x, real.lt_asymm x y hxy $
or.cases_on hyx id $ λ H, by subst H; from hxy⟩,
assume hxy : _, or.cases_on hxy.1
(assume H : real.lt x y, H)
(assume H : x = y, false.elim $ hxy.2 $ or.inr $ eq.symm H)⟩,
le_refl := λ x, or.inr rfl,
le_trans := λ x y z hxy hyz, or.cases_on hxy
(assume hxy : real.lt x y, or.cases_on hyz
(assume hyz : real.lt y z, or.inl $ real.lt_trans _ _ _ hxy hyz)
(assume hyz : y = z, hyz ▸ or.inl hxy))
(assume hxy : x = y, hxy.symm ▸ hyz),
le_antisymm := λ x y hxy hyx, or.cases_on hxy
(assume hxy : real.lt x y, or.cases_on hyx
(assume hyx : real.lt y x, false.elim $ real.lt_asymm _ _ hxy hyx)
(assume hyx : y = x, eq.symm hyx))
(assume hxy : x = y, hxy) }
instance real.linear_ordered_comm_ring : linear_ordered_comm_ring real :=
{ add_le_add_left := λ x y hxy c, (quotient.induction_on₃ x y c $
λ f g h hfg, or.cases_on hfg
(assume hfg : real.lt ⟦f⟧ ⟦g⟧, or.inl $
rat.cau_seq.add_lt_add_left _ _ _ hfg)
(assume hfg : ⟦f⟧ = ⟦g⟧, or.inr $ hfg ▸ rfl)) hxy,
add_lt_add_left := λ x y hxy c, (quotient.induction_on₃ x y c $
λ f g h hfg, rat.cau_seq.add_lt_add_left _ _ _ hfg) hxy,
zero_lt_one := ⟨0.5, dec_trivial, 0, λ n hn, dec_trivial⟩,
mul_nonneg := λ x y hx hy, or.cases_on hx
(assume hx : 0 < x, or.cases_on hy
(assume hy : 0 < y, or.inl $ real.mul_pos _ _ hx hy)
(assume hy : 0 = y, or.inr $ eq.symm $ hy ▸ mul_zero _))
(assume hx : 0 = x, or.inr $ eq.symm $ hx ▸ zero_mul _),
mul_pos := real.mul_pos,
le_total := λ x y, or.cases_on (real.lt_trichotomy x y)
(assume hxy : x < y, or.inl $ or.inl hxy)
(assume hxy, or.cases_on hxy
(assume hxy : x = y, or.inl $ or.inr hxy)
(assume hxy : y < x, or.inr $ or.inl hxy)),
zero_ne_one := λ H,
let ⟨N, HN⟩ := quotient.exact H.symm 0.5 dec_trivial in
absurd (HN (N + 1) (nat.lt_succ_self N)) dec_trivial,
.. real.comm_ring, .. real.partial_order }
instance real.inhabited : inhabited real :=
⟨0⟩
namespace rat.cau_seq
variables (f : rat.cau_seq) (Hf : f ∉ rat.null)
theorem inv.of_not_null : (λ n, 1 / f.1 n) ∈ rat.cau_seq := λ ε Hε,
let ⟨ε', Hε', N1, HN1⟩ := rat.null.abs_pos_of_not_null f Hf in
let ⟨N2, HN2⟩ := f.2 (ε * ε' * ε') (mul_pos (mul_pos Hε Hε') Hε') in
⟨max N1 N2, λ m hm n hn,
have H : _ := HN2 n (lt_of_le_of_lt (le_max_right _ _) hn)
m (lt_of_le_of_lt (le_max_right _ _) hm),
have H1 : _ := HN1 m (lt_of_le_of_lt (le_max_left _ _) hm),
have H2 : _ := HN1 n (lt_of_le_of_lt (le_max_left _ _) hn),
have H3 : abs (f.1 m) > 0 := lt_trans Hε' H1,
have H4 : abs (f.1 n) > 0 := lt_trans Hε' H2,
calc abs (1 / f.1 m - 1 / f.1 n)
= abs ((1 / f.1 m) * (f.1 n - f.1 m) * (1 / f.1 n)) :
congr_arg abs $ eq.symm $ one_div_mul_sub_mul_one_div_eq_one_div_add_one_div
(ne_zero_of_abs_ne_zero $ ne_of_gt H3)
(ne_zero_of_abs_ne_zero $ ne_of_gt H4)
... = (1 / abs (f.1 m)) * abs (f.1 n - f.1 m) * (1 / abs (f.1 n)) :
by rw [abs_mul, abs_mul, abs_one_div, abs_one_div]
... < (1 / ε') * (ε * ε' * ε') * (1 / ε') :
mul_lt_mul
(mul_lt_mul'
(one_div_le_one_div_of_le Hε' $ le_of_lt H1)
H
(abs_nonneg _)
(one_div_pos_of_pos Hε'))
(one_div_le_one_div_of_le Hε' $ le_of_lt H2)
(one_div_pos_of_pos H4)
(le_of_lt $ mul_pos (one_div_pos_of_pos Hε') (mul_pos (mul_pos Hε Hε') Hε'))
... = ε : by rw [mul_assoc, mul_assoc, mul_one_div_cancel (ne_of_gt Hε'),
mul_one, mul_comm, mul_assoc, mul_one_div_cancel (ne_of_gt Hε'), mul_one]⟩
def inv : rat.cau_seq :=
⟨_, inv.of_not_null f Hf⟩
variables (g : rat.cau_seq) (Hg : g ∉ rat.null)
theorem inv.well_defined (H : f - g ∈ rat.null) :
inv f Hf - inv g Hg ∈ rat.null := λ ε Hε,
let ⟨ε1, Hε1, N1, HN1⟩ := rat.null.abs_pos_of_not_null f Hf in
let ⟨ε2, Hε2, N2, HN2⟩ := rat.null.abs_pos_of_not_null g Hg in
let ⟨N, HN⟩ := H (ε * ε1 * ε2) (mul_pos (mul_pos Hε Hε1) Hε2) in
⟨max N (max N1 N2), λ n hn,
have H1 : _ := HN1 n (lt_of_le_of_lt (le_trans (le_max_left _ _) (le_max_right _ _)) hn),
have H2 : _ := HN2 n (lt_of_le_of_lt (le_trans (le_max_right _ _) (le_max_right _ _)) hn),
have H3 : _ := HN n (lt_of_le_of_lt (le_max_left _ _) hn),
calc abs (1 / f.1 n - 1 / g.1 n)
= abs ((1 / f.1 n) * (g.1 n - f.1 n) * (1 / g.1 n)) :
congr_arg abs $ eq.symm $ one_div_mul_sub_mul_one_div_eq_one_div_add_one_div
(ne_zero_of_abs_ne_zero $ ne_of_gt (lt_trans Hε1 H1))
(ne_zero_of_abs_ne_zero $ ne_of_gt (lt_trans Hε2 H2))
... = (1 / abs (f.1 n)) * abs (f.1 n - g.1 n) * (1 / abs (g.1 n)) :
by rw [abs_mul, abs_mul, abs_one_div, abs_one_div, abs_sub]
... < (1 / ε1) * (ε * ε1 * ε2) * (1 / ε2) :
mul_lt_mul
(mul_lt_mul'
(one_div_le_one_div_of_le Hε1 $ le_of_lt H1)
H3
(abs_nonneg _)
(one_div_pos_of_pos Hε1))
(one_div_le_one_div_of_le Hε2 $ le_of_lt H2)
(one_div_pos_of_pos (lt_trans Hε2 H2))
(le_of_lt $ mul_pos (one_div_pos_of_pos Hε1) (mul_pos (mul_pos Hε Hε1) Hε2))
... = ε : by rw [mul_assoc, mul_assoc, mul_one_div_cancel (ne_of_gt Hε2),
mul_one, mul_comm, mul_assoc, mul_one_div_cancel (ne_of_gt Hε1), mul_one]⟩
theorem mul_inv_cancel : f * inv f Hf - 1 ∈ rat.null :=
let ⟨ε, Hε, N, HN⟩ := rat.null.abs_pos_of_not_null f Hf in
λ ε' Hε', ⟨N, λ n hn,
have H1 : abs (f.1 n) ≠ 0,
from ne_of_gt $ lt_trans Hε $ HN n hn,
have H2 : f.1 n ≠ 0,
from H1 ∘ abs_eq_zero.2,
have H3 : f.1 n * (1 / f.1 n) - 1 = 0,
by rw [mul_one_div_cancel H2, sub_self],
calc abs (f.1 n * (1 / f.1 n) - 1)
= 0 : abs_eq_zero.2 H3
... < ε' : Hε'⟩
end rat.cau_seq
namespace real
-- short circuits
instance : has_zero real := by apply_instance
instance : has_one real := by apply_instance
instance : has_add real := by apply_instance
instance : has_neg real := by apply_instance
instance : has_mul real := by apply_instance
instance : has_scalar real real := by apply_instance
instance : add_comm_group real := by apply_instance
instance : add_comm_semigroup real := by apply_instance
instance : add_comm_monoid real := by apply_instance
instance : add_group real := by apply_instance
instance : add_left_cancel_semigroup real := by apply_instance
instance : add_monoid real := by apply_instance
instance : add_right_cancel_semigroup real := by apply_instance
instance : add_semigroup real := by apply_instance
instance : char_zero real := by apply_instance
instance : comm_semigroup real := by apply_instance
instance : comm_monoid real := by apply_instance
instance : distrib real := by apply_instance
instance : domain real := by apply_instance
instance : integral_domain real := by apply_instance
instance : linear_order real := by apply_instance
instance : linear_ordered_ring real := by apply_instance
instance : linear_ordered_semiring real := by apply_instance
instance : module real real := by apply_instance
instance : monoid real := by apply_instance
instance : mul_zero_class real := by apply_instance
instance : no_bot_order real := by apply_instance
instance : no_top_order real := by apply_instance
instance : no_zero_divisors real := by apply_instance
instance : ordered_cancel_comm_monoid real := by apply_instance
instance : ordered_comm_monoid real := by apply_instance
instance : ordered_comm_group real := by apply_instance
instance : ordered_ring real := by apply_instance
instance : ordered_semiring real := by apply_instance
instance : ring real := by apply_instance
instance : semigroup real := by apply_instance
instance : semiring real := by apply_instance
instance : zero_ne_one_class real := by apply_instance
local attribute [instance] classical.prop_decidable
noncomputable def inv (x : real) : real :=
quotient.lift_on x (λ f, dite _
(assume H : f ∈ rat.null, (0 : real))
(assume H : f ∉ rat.null, ⟦rat.cau_seq.inv f H⟧)) $
λ f g hfg, dite _
(assume H : f ∈ rat.null,
have H1 : f + (-1) * (f - g) = g := by rw [neg_one_mul, ← sub_eq_add_neg, sub_sub_cancel],
have H2 : g ∈ rat.null, from H1 ▸ rat.null.add _ _ H (rat.null.mul _ _ hfg),
(dif_pos H).trans (dif_pos H2).symm)
(assume H : f ∉ rat.null,
have H1 : g + (f - g) = f := add_sub_cancel'_right _ _,
have H2 : g ∉ rat.null, from λ h, H $ H1 ▸ rat.null.add _ _ h hfg,
(dif_neg H).trans $ eq.symm $ (dif_neg H2).trans $ eq.symm $ quotient.sound $
rat.cau_seq.inv.well_defined _ _ _ _ hfg)
theorem mul_inv_cancel {x : real} : x ≠ 0 → x * real.inv x = 1 :=
quotient.induction_on x $ λ f hf,
have H : f ∉ rat.null, from λ h, hf $ quotient.sound $
show f - 0 ∈ rat.null, from (sub_zero f).symm ▸ h,
(congr_arg _ (dif_neg H)).trans $ quotient.sound $
rat.cau_seq.mul_inv_cancel _ _
noncomputable instance : discrete_linear_ordered_field real :=
{ inv := real.inv,
mul_inv_cancel := @real.mul_inv_cancel,
inv_mul_cancel := λ x, (mul_comm _ _).trans ∘ real.mul_inv_cancel,
decidable_le := λ _ _, classical.prop_decidable _,
inv_zero := dif_pos rat.null.zero,
.. real.linear_ordered_comm_ring }
end real
theorem real.mk_eq_coe_nat (q : ℕ) : (q : real) = ⟦⟨λ n, q, λ ε Hε, ⟨0, λ m hm n hn, show abs (q - q : ℚ) < ε,
from (sub_self (q:ℚ)).symm ▸ (@abs_zero ℚ _).symm ▸ Hε⟩⟩⟧ :=
nat.rec_on q rfl $ λ n ih, show (n : real) + 1 = _, by rw ih; refl
theorem real.mk_eq_coe_int (q : ℤ) : (q : real) = ⟦⟨λ n, q, λ ε Hε, ⟨0, λ m hm n hn, show abs (q - q : ℚ) < ε,
from (sub_self (q:ℚ)).symm ▸ (@abs_zero ℚ _).symm ▸ Hε⟩⟩⟧ :=
int.cases_on q real.mk_eq_coe_nat $ λ n, show -((n : real) + 1) = _, by rw real.mk_eq_coe_nat; refl
theorem rat.mk_eq_coe_nat (n : ℕ) : (n : ℚ) = rat.mk' n 1 dec_trivial (nat.coprime_one_right n) :=
nat.rec_on n rfl $ λ n ih, show (n : ℚ) + rat.mk' 1 1 _ _ = _, by rw [ih]; dsimp [(+), (1:ℚ)];
unfold rat.add; unfold rat.mk_pnat; dsimp; congr; simp [-add_comm]; refl
theorem rat.mk_eq_coe_int (n : ℤ) : (n : ℚ) = rat.mk' n 1 dec_trivial (nat.coprime_one_right (int.nat_abs n)) :=
int.cases_on n rat.mk_eq_coe_nat $ λ i, show -((i:ℚ) + rat.mk' 1 1 _ _) = _, by rw rat.mk_eq_coe_nat; dsimp [(+), has_neg.neg, (1:ℚ)];
unfold rat.add; unfold rat.mk_pnat; unfold rat.neg; dsimp; congr; simp; rw [add_comm, int.neg_succ_of_nat_coe]; simp
theorem real.mk_eq_coe_rat (q : ℚ) : (q : real) = ⟦⟨λ n, q, λ ε Hε, ⟨0, λ m hm n hn, show abs (q - q) < ε,
from (sub_self q).symm ▸ (@abs_zero ℚ _).symm ▸ Hε⟩⟩⟧ :=
rat.cases_on q $ λ n d p c, (div_eq_iff_mul_eq $ ne_of_gt $ (@nat.cast_pos real _ d).2 p).2 $
by rw [real.mk_eq_coe_int, real.mk_eq_coe_nat]; apply real.eq_of_eq; apply subtype.eq; funext m; dsimp;
change rat.mk' n d p c * d = (n:ℚ); rw [rat.mk_eq_coe_nat, rat.mk_eq_coe_int]; dsimp [(*)]; unfold rat.mul;
unfold rat.mk_pnat; simp; split; rw [nat.gcd_eq_right]; [rw int.mul_div_cancel, simp [int.nat_abs_mul], rw nat.div_self p, simp [int.nat_abs_mul]];
apply ne_of_gt; apply int.coe_nat_pos.2 p
theorem real.rat_eq_of_coe_eq {x y : ℚ} (H : (x : real) = y) : x = y :=
rat.cast_inj.1 H
theorem real.rat_lt_of_coe_lt {x y : ℚ} (H : (x : real) < y) : x < y :=
rat.cast_lt.1 H
theorem real.ex_rat_lt (x : real) : ∃ q : ℚ, (q : real) < x :=
quotient.induction_on x $ λ f,
let ⟨N, HN⟩ := f.2 1 zero_lt_one in
⟨f.1 (N + 1) - 2, trans_rel_right _ (real.mk_eq_coe_rat _)
⟨1, zero_lt_one, N, λ n hn,
calc f.1 (N + 1) - 2 + 1
= f.1 (N + 1) - (2 - 1) : sub_add _ _ _
... = f.1 (N + 1) - 1 : congr_arg _ $ add_sub_cancel _ _
... = f.1 (N + 1) - f.1 n + f.1 n - 1 : by rw sub_add_cancel
... < 1 + f.1 n - 1 : sub_lt_sub_right
(add_lt_add_right (abs_lt.1 $ HN _ (nat.lt_succ_self N) _ hn).2 _) _
... = f.1 n : add_sub_cancel' _ _⟩⟩
theorem real.ex_lt_rat (x : real) : ∃ q : ℚ, x < (q : real) :=
quotient.induction_on x $ λ f,
let ⟨N, HN⟩ := f.2 1 zero_lt_one in
⟨f.1 (N + 1) + 2, by rw real.mk_eq_coe_rat; from
⟨1, zero_lt_one, N, λ n hn,
calc f.1 n + 1
= f.1 n - f.1 (N + 1) + f.1 (N + 1) + 1 : by rw sub_add_cancel
... < 1 + f.1 (N + 1) + 1 : add_lt_add_right
(add_lt_add_right (abs_lt.1 $ HN _ hn _ (nat.lt_succ_self N)).2 _) _
... = f.1 (N + 1) + 2 : by rw [add_comm (1:ℚ), add_assoc]; refl⟩⟩
instance real.archimedean : archimedean real :=
archimedean_iff_rat_lt.2 real.ex_lt_rat
noncomputable instance real.floor_ring : floor_ring real :=
archimedean.floor_ring real
section completeness
local attribute [instance] classical.prop_decidable
parameters (A : set real) (x ub : real)
parameters (H1 : x ∈ A) (H2 : ∀ x ∈ A, x ≤ ub)
noncomputable def bin_div : ℕ → ℚ × ℚ
| 0 := (classical.some $ real.ex_rat_lt x, classical.some $ real.ex_lt_rat ub)
| (n+1) := if ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then
((bin_div n).1, ((bin_div n).1 + (bin_div n).2)/2)
else
(((bin_div n).1 + (bin_div n).2)/2, (bin_div n).2)
theorem bin_div.snd_sub_fst (n : nat) : (bin_div n).2 - (bin_div n).1 = ((bin_div 0).2 - (bin_div 0).1) / 2^n :=
nat.rec_on n (div_one _).symm $ λ n ih,
if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then
have H1 : bin_div (n+1) = ((bin_div n).1, ((bin_div n).1 + (bin_div n).2)/2),
by dsimp [bin_div]; rw [if_pos H],
calc (bin_div (n+1)).2 - (bin_div (n+1)).1
= (((bin_div n).1 + (bin_div n).2) - ((bin_div n).1 + (bin_div n).1))/2 : by rw [H1, sub_div, add_self_div_two]
... = ((bin_div n).2 - (bin_div n).1)/2 : by rw add_sub_add_left_eq_sub
... = (((bin_div 0).2 - (bin_div 0).1) / 2^n) / 2 : by rw ih
... = ((bin_div 0).2 - (bin_div 0).1) / 2^(n+1) : by rw [div_div_eq_div_mul, pow_add]; refl
else
have H1 : bin_div (n+1) = (((bin_div n).1 + (bin_div n).2)/2, (bin_div n).2),
by dsimp [bin_div]; rw [if_neg H],
calc (bin_div (n+1)).2 - (bin_div (n+1)).1
= (((bin_div n).2 + (bin_div n).2) - ((bin_div n).1 + (bin_div n).2))/2 : by rw [H1, sub_div, add_self_div_two]
... = ((bin_div n).2 - (bin_div n).1)/2 : by rw add_sub_add_right_eq_sub
... = (((bin_div 0).2 - (bin_div 0).1) / 2^n) / 2 : by rw ih
... = ((bin_div 0).2 - (bin_div 0).1) / 2^(n+1) : by rw [div_div_eq_div_mul, pow_add]; refl
theorem bin_div.zero : (bin_div 0).1 < (bin_div 0).2 :=
real.rat_lt_of_coe_lt $
calc ((bin_div 0).1 : real)
< x : classical.some_spec $ real.ex_rat_lt x
... ≤ ub : H2 x H1
... < (bin_div 0).2 : classical.some_spec $ real.ex_lt_rat ub
theorem bin_div.fst_lt_snd_self (n : nat) : (bin_div n).1 < (bin_div n).2 :=
lt_of_sub_pos $ trans_rel_left _
(div_pos (sub_pos_of_lt bin_div.zero) $ pow_pos two_pos _)
(bin_div.snd_sub_fst n).symm
theorem bin_div.lt_snd (r) (hr : r ∈ A) (n : nat) : r < ((bin_div n).2 : real) :=
nat.rec_on n
(calc r
≤ ub : H2 r hr
... < (bin_div 0).2 : classical.some_spec $ real.ex_lt_rat ub) $ λ n ih,
if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then
have H1 : (bin_div (n+1)).2 = ((bin_div n).1 + (bin_div n).2)/2,
by dsimp [bin_div]; rw [if_pos H],
trans_rel_left _ (H r hr) $ congr_arg _ H1.symm
else
have H1 : (bin_div (n+1)).2 = (bin_div n).2,
by dsimp [bin_div]; rw [if_neg H],
trans_rel_left _ ih $ congr_arg _ H1.symm
theorem bin_div.ex_fst_le (n : nat) : ∃ x ∈ A, ((bin_div n).1 : real) ≤ x :=
nat.rec_on n ⟨x, H1, le_of_lt $ classical.some_spec $ real.ex_rat_lt x⟩ $ λ n ih,
if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then
have H1 : (bin_div (n+1)).1 = (bin_div n).1,
by dsimp [bin_div]; rw [if_pos H],
let ⟨y, hy1, hy2⟩ := ih in
⟨y, hy1, trans_rel_right _ (congr_arg _ H1) hy2⟩
else
have H1 : (bin_div (n+1)).1 = ((bin_div n).1 + (bin_div n).2)/2,
by dsimp [bin_div]; rw [if_neg H],
by simpa [not_forall, H1] using H
theorem bin_div.fst_le_succ (n : nat) : (bin_div n).1 ≤ (bin_div (n+1)).1 :=
if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then
have H1 : (bin_div (n+1)).1 = (bin_div n).1,
by dsimp [bin_div]; rw [if_pos H],
le_of_eq H1.symm
else
have H1 : (bin_div (n+1)).1 = ((bin_div n).1 + (bin_div n).2)/2,
by dsimp [bin_div]; rw [if_neg H],
trans_rel_left _ (trans_rel_right _ (add_self_div_two _).symm $
(div_le_div_right two_pos).2 $ add_le_add_left
(le_of_lt $ bin_div.fst_lt_snd_self n) _) H1.symm
theorem bin_div.snd_le_succ (n : nat) : (bin_div (n+1)).2 ≤ (bin_div n).2 :=
if H : ∀ x ∈ A, x < (((bin_div n).1 + (bin_div n).2)/2 : ℚ) then
have H1 : (bin_div (n+1)).2 = ((bin_div n).1 + (bin_div n).2)/2,
by dsimp [bin_div]; rw [if_pos H],
trans_rel_right _ H1 $ le_of_lt $ trans_rel_left _
((div_lt_div_right two_pos).2 $ add_lt_add_right (bin_div.fst_lt_snd_self n) _)
(add_self_div_two _)
else
have H1 : (bin_div (n+1)).2 = (bin_div n).2,
by dsimp [bin_div]; rw [if_neg H],
le_of_eq H1
theorem bin_div.fst_le : ∀ n m : nat, n ≤ m → (bin_div n).1 ≤ (bin_div m).1
| _ _ (nat.less_than_or_equal.refl n) := le_refl _
| n (m+1) (nat.less_than_or_equal.step H) := le_trans
(bin_div.fst_le n m H) (bin_div.fst_le_succ m)
theorem bin_div.snd_le : ∀ n m : nat, n ≤ m → (bin_div m).2 ≤ (bin_div n).2
| _ _ (nat.less_than_or_equal.refl n) := le_refl _
| n (m+1) (nat.less_than_or_equal.step H) := le_trans
(bin_div.snd_le_succ m) (bin_div.snd_le n m H)
theorem bin_div.fst_lt_snd (n m : nat) : (bin_div n).1 < (bin_div m).2 :=
or.cases_on (@nat.le_total m n)
(assume H : m ≤ n, lt_of_lt_of_le (bin_div.fst_lt_snd_self n) $ bin_div.snd_le m n H)
(assume H : n ≤ m, lt_of_le_of_lt (bin_div.fst_le n m H) (bin_div.fst_lt_snd_self m))
theorem bin_div.snd_sub_fst_le : ∀ n m : nat, n ≤ m → (bin_div n).2 - (bin_div m).1 ≤ ((bin_div 0).2 - (bin_div 0).1) / 2^n
| _ _ (nat.less_than_or_equal.refl n) := le_of_eq $ bin_div.snd_sub_fst n
| n (m+1) (nat.less_than_or_equal.step H) := le_trans
(sub_le_sub_left (bin_div.fst_le_succ m) _)
(bin_div.snd_sub_fst_le n m H)
theorem bin_div.fst_sub_fst_lt_pow (n m : nat) : (bin_div n).1 - (bin_div m).1 < ((bin_div 0).2 - (bin_div 0).1) / 2^m :=
trans_rel_left _ (sub_lt_sub_right (bin_div.fst_lt_snd n m) _) (bin_div.snd_sub_fst m)
theorem bin_div.snd_sub_snd_lt_pow (n m : nat) : (bin_div n).2 - (bin_div m).2 < ((bin_div 0).2 - (bin_div 0).1) / 2^n :=
trans_rel_left _ (sub_lt_sub_left (bin_div.fst_lt_snd n m) _) (bin_div.snd_sub_fst n)
theorem bin_div.fst_cau_seq : prod.fst ∘ bin_div ∈ rat.cau_seq := λ ε Hε,
let N := (ε / ((bin_div 0).2 - (bin_div 0).1)).denom in
⟨N, λ m hm n hn,
have H1 : (bin_div m).1 - (bin_div (N+1)).1 ≥ 0, from
sub_nonneg_of_le $ bin_div.fst_le (N+1) m hm,
have H2 : (bin_div n).1 - (bin_div (N+1)).1 ≥ 0, from
sub_nonneg_of_le $ bin_div.fst_le (N+1) n hn,
calc abs ((bin_div m).1 - (bin_div n).1)
≤ abs ((bin_div m).1 - (bin_div (N+1)).1) + abs ((bin_div (N+1)).1 - (bin_div n).1) : abs_sub_le _ _ _
... = abs ((bin_div m).1 - (bin_div (N+1)).1) + abs ((bin_div n).1 - (bin_div (N+1)).1) : by rw abs_sub (bin_div n).1
... = ((bin_div m).1 - (bin_div (N+1)).1) + ((bin_div n).1 - (bin_div (N+1)).1) : by rw [abs_of_nonneg H1, abs_of_nonneg H2]
... < _ : add_lt_add (bin_div.fst_sub_fst_lt_pow m (N+1)) (bin_div.fst_sub_fst_lt_pow n (N+1))
... = ((bin_div 0).2 - (bin_div 0).1) / (2^N) : by rw [pow_succ', ← div_div_eq_div_mul, add_halves]
... = ((bin_div 0).2 - (bin_div 0).1) * (1 / (2^N)) : div_eq_mul_one_div _ _
... < ((bin_div 0).2 - (bin_div 0).1) * (ε / ((bin_div 0).2 - (bin_div 0).1)) : mul_lt_mul_of_pos_left
(rat.lt _ $ div_pos Hε $ sub_pos_of_lt bin_div.zero) (sub_pos_of_lt bin_div.zero)
... = ε : mul_div_cancel' _ $ ne_of_gt $ sub_pos_of_lt bin_div.zero⟩
theorem bin_div.snd_cau_seq : prod.snd ∘ bin_div ∈ rat.cau_seq := λ ε Hε,
let N := (ε / ((bin_div 0).2 - (bin_div 0).1)).denom in
⟨N, λ m hm n hn,
have H1 : (bin_div (N+1)).2 - (bin_div m).2 ≥ 0, from
sub_nonneg_of_le $ bin_div.snd_le (N+1) m hm,
have H2 : (bin_div (N+1)).2 - (bin_div n).2 ≥ 0, from
sub_nonneg_of_le $ bin_div.snd_le (N+1) n hn,
calc abs ((bin_div m).2 - (bin_div n).2)
≤ abs ((bin_div m).2 - (bin_div (N+1)).2) + abs ((bin_div (N+1)).2 - (bin_div n).2) : abs_sub_le _ _ _
... = abs ((bin_div (N+1)).2 - (bin_div m).2) + abs ((bin_div (N+1)).2 - (bin_div n).2) : by rw abs_sub
... = ((bin_div (N+1)).2 - (bin_div m).2) + ((bin_div (N+1)).2 - (bin_div n).2) : by rw [abs_of_nonneg H1, abs_of_nonneg H2]
... < _ : add_lt_add (bin_div.snd_sub_snd_lt_pow (N+1) m) (bin_div.snd_sub_snd_lt_pow (N+1) n)
... = ((bin_div 0).2 - (bin_div 0).1) / (2^N) : by rw [pow_succ', ← div_div_eq_div_mul, add_halves]
... = ((bin_div 0).2 - (bin_div 0).1) * (1 / (2^N)) : div_eq_mul_one_div _ _
... < ((bin_div 0).2 - (bin_div 0).1) * (ε / ((bin_div 0).2 - (bin_div 0).1)) : mul_lt_mul_of_pos_left
(rat.lt _ $ div_pos Hε $ sub_pos_of_lt bin_div.zero) (sub_pos_of_lt bin_div.zero)
... = ε : mul_div_cancel' _ $ ne_of_gt $ sub_pos_of_lt bin_div.zero⟩
noncomputable def sup : real :=
⟦⟨prod.snd ∘ bin_div, bin_div.snd_cau_seq⟩⟧
theorem bin_div.sup_le_snd (n : nat) : sup ≤ (bin_div n).2 :=
(real.mk_eq_coe_rat (bin_div n).2).symm ▸
(le_of_not_gt $ λ ⟨ε, Hε, N, HN⟩, lt_asymm
(HN (n+N+1) (nat.succ_le_succ $ nat.le_add_left _ _))
(lt_of_le_of_lt
(bin_div.snd_le n (n+N+1) (nat.le_add_right n (N+1)))
(lt_add_of_pos_right _ Hε)))
theorem bin_div.mk_fst_eq_sup : (⟦⟨prod.fst ∘ bin_div, bin_div.fst_cau_seq⟩⟧ : real) = sup :=
quotient.sound $ λ ε Hε,
let N := (ε / ((bin_div 0).2 - (bin_div 0).1)).denom in
⟨N, λ n hn,
have H1 : (2:ℚ)^n > (2:ℚ)^N, from
calc (2:ℚ)^n
= ((2^n:ℕ):ℚ) : rat.coe_pow 2 n
... > ((2^N:ℕ):ℚ) : nat.cast_lt.2 $ nat.pow_lt_pow_of_lt_right (nat.lt_succ_self 1) hn
... = (2:ℚ)^N : eq.symm $ rat.coe_pow 2 N,
have H2 : (2:ℚ)^n > (2:ℚ)^N, from
calc (2:ℚ)^n
= ((2^n:ℕ):ℚ) : rat.coe_pow 2 n
... > ((2^N:ℕ):ℚ) : nat.cast_lt.2 $ nat.pow_lt_pow_of_lt_right (nat.lt_succ_self 1) hn
... = (2:ℚ)^N : eq.symm $ rat.coe_pow 2 N,
calc abs ((bin_div n).1 - (bin_div n).2)
= abs ((bin_div n).2 - (bin_div n).1) : abs_sub _ _
... = (bin_div n).2 - (bin_div n).1 : abs_of_pos $ sub_pos_of_lt $ bin_div.fst_lt_snd_self n
... = ((bin_div 0).2 - (bin_div 0).1) / 2^n : bin_div.snd_sub_fst n
... < ((bin_div 0).2 - (bin_div 0).1) / 2^N :
(div_lt_div_left (sub_pos_of_lt bin_div.zero) (pow_pos two_pos _) (pow_pos two_pos _)).2 H1
... = ((bin_div 0).2 - (bin_div 0).1) * (1 / (2^N)) : div_eq_mul_one_div _ _
... < ((bin_div 0).2 - (bin_div 0).1) * (ε / ((bin_div 0).2 - (bin_div 0).1)) : mul_lt_mul_of_pos_left
(rat.lt _ $ div_pos Hε $ sub_pos_of_lt bin_div.zero) (sub_pos_of_lt bin_div.zero)
... = ε : mul_div_cancel' _ $ ne_of_gt $ sub_pos_of_lt bin_div.zero⟩
theorem le_sup (r : real) : r ∈ A → r ≤ sup :=
quotient.induction_on r $ λ f hf, le_of_not_gt $ λ ⟨ε1, Hε1, N1, HN1⟩,
let ⟨N2, HN2⟩ := f.2 (ε1/2) (half_pos Hε1) in
let ⟨ε3, Hε3, N3, HN3⟩ := trans_rel_left _ (bin_div.lt_snd _ hf (N1+N2+1)) (real.mk_eq_coe_rat _) in
have H1 : _ := HN1 (N1+N2+1) (nat.succ_le_succ $ nat.le_add_right _ _),
have H2 : _ := HN2 (N1+N2+1) (nat.succ_le_succ $ nat.le_add_left _ _)
(N2+N3+1) (nat.succ_le_succ $ nat.le_add_right _ _),
have H3 : _ := HN3 (N2+N3+1) (nat.succ_le_succ $ nat.le_add_left _ _),
lt_irrefl _ $
calc f.1 (N2+N3+1) + ε1
< f.1 (N2+N3+1) + ε3 + ε1 : add_lt_add_right (lt_add_of_pos_right _ Hε3) _
... < (bin_div (N1+N2+1)).2 + ε1 : add_lt_add_right H3 _
... < f.1 (N1+N2+1) : H1
... = f.1 (N2+N3+1) + (f.1 (N1+N2+1) - f.1 (N2+N3+1)) : eq.symm $ add_sub_cancel'_right _ _
... < f.1 (N2+N3+1) + ε1/2 : add_lt_add_left (abs_lt.1 H2).2 _
... < f.1 (N2+N3+1) + ε1 : add_lt_add_left (half_lt_self Hε1) _
theorem sup_le (r : real) : (∀ x ∈ A, x ≤ r) → sup ≤ r :=
quotient.induction_on r $ λ f hf, bin_div.mk_fst_eq_sup ▸ (le_of_not_gt $ λ ⟨ε, Hε, N1, HN1⟩,
let ⟨N2, HN2⟩ := f.2 (ε/2) (half_pos Hε) in
let ⟨r, hr1, hr2⟩ := bin_div.ex_fst_le (N1+N2+1) in
have H1 : _ := HN1 (N1+N2+1) (nat.succ_le_succ $ nat.le_add_right _ _),
not_lt_of_ge (le_trans hr2 $ hf r hr1) $
(real.mk_eq_coe_rat (bin_div (N1+N2+1)).1).symm ▸
⟨ε/2, half_pos Hε, N2, λ n hn,
have H2 : _ := HN2 n hn (N1+N2+1) (nat.succ_le_succ $ nat.le_add_left _ _),
calc f.1 n + ε/2
= f.1 (N1+N2+1) + (f.1 n - f.1 (N1+N2+1)) + ε/2 : by rw add_sub_cancel'_right
... < f.1 (N1+N2+1) + ε/2 + ε/2 : add_lt_add_right (add_lt_add_left (abs_lt.1 $ H2).2 _) _
... = f.1 (N1+N2+1) + (ε/2 + ε/2) : add_assoc _ _ _
... = f.1 (N1+N2+1) + ε : by rw add_halves
... < (bin_div (N1+N2+1)).1 : H1⟩)
theorem ex_sup_sub_le (ε : real) (Hε : ε > 0) :
∃ r ∈ A, sup - ε ≤ r :=
classical.by_contradiction $ λ H,
have H3 : sup ≤ sup - ε,
from sup_le (sup - ε) $ λ r hr,
le_of_not_le $ not_bex.1 H r hr,
not_lt_of_le H3 $ sub_lt_self sup Hε
end completeness
section inf
parameters (A : set real) (x lb : real)
parameters (H1 : x ∈ A) (H2 : ∀ x ∈ A, lb ≤ x)
noncomputable def inf : real :=
-sup (has_neg.neg ⁻¹' A) (-x) (-lb)
(show -(-x) ∈ A, from (neg_neg x).symm ▸ H1)
(λ r hr, le_neg_of_le_neg $ H2 (-r) hr)
theorem inf_le (r : real) (H : r ∈ A) : inf ≤ r :=
neg_le_of_neg_le $ le_sup _ _ _ _ _ (-r) $
show -(-r) ∈ A, from (neg_neg r).symm ▸ H
theorem le_inf (r : real) (H : ∀ x ∈ A, r ≤ x) : r ≤ inf :=
le_neg_of_le_neg $ sup_le _ _ _ _ _ (-r) $ λ x hx,
le_neg_of_le_neg $ H (-x) hx
end inf
instance real.seq : comm_ring (ℕ → real) :=
by refine
{ add := λ f g n, f n + g n,
zero := λ n, 0,
neg := λ f n, -f n,
mul := λ f g n, f n * g n,
one := λ n, 1,
.. };
{ intros,
{ simp [mul_assoc, mul_add, add_mul] }
<|> simp [mul_comm] }
namespace real
def cau_seq : set (ℕ → real) :=
{ f | ∀ ε > 0, ∃ N, ∀ m > N, ∀ n > N, abs (f m - f n) < ε }
noncomputable def lim_sup (f : ℕ → real) (ub lb : real)
(H1 : ∀ m, f m ≤ ub) (H2 : ∀ m, ∃ k ≥ m, lb ≤ f m) : real :=
inf
{ x | ∃ m, x = sup
{ y | ∃ k ≥ m, y = f k }
(f m) ub ⟨m, nat.le_refl _, rfl⟩ (λ r ⟨k, hr1, hr2⟩, hr2.symm ▸ H1 k) }
_ lb ⟨0, rfl⟩
(λ r ⟨m, hr⟩, let ⟨k, hk1, hk2⟩ := H2 m in
le_trans hk2 $ hr.symm ▸ le_sup _ _ _ _ _ _ ⟨m, nat.le_refl _, rfl⟩)
variable (f : cau_seq)
theorem cau_seq.ub : ∃ ub, ∀ n, f.1 n ≤ ub :=
let ⟨N, HN⟩ := f.2 1 zero_lt_one in
⟨max (((list.range (N+1)).map f.1).max) (f.1 (N+1) + 1), λ n,
or.cases_on (nat.lt_or_ge n (N+1))
(assume H : n < N + 1, le_max_left_of_le $ list.le_max _ _ $
list.mem_map_of_mem _ $ list.mem_range.2 H)
(assume H : n ≥ N + 1, le_max_right_of_le $ le_of_lt $
calc f.1 n
= f.1 (N+1) + (f.1 n - f.1 (N+1)) : eq.symm $ add_sub_cancel'_right _ _
... < f.1 (N+1) + 1 : add_lt_add_left (abs_lt.1 $ HN _ (nat.lt_of_succ_le H)
(N+1) (nat.lt_succ_self _)).2 _ )⟩
theorem cau_seq.lb : ∃ lb, ∀ n, lb ≤ f.1 n :=
let ⟨N, HN⟩ := f.2 1 zero_lt_one in
⟨min (((list.range (N+1)).map f.1).min) (f.1 (N+1) - 1), λ n,
or.cases_on (nat.lt_or_ge n (N+1))
(assume H : n < N + 1, min_le_left_of_le $ list.min_le _ _ $
list.mem_map_of_mem _ $ list.mem_range.2 H)
(assume H : n ≥ N + 1, min_le_right_of_le $ le_of_lt $
calc f.1 n
= f.1 (N+1) + (f.1 n - f.1 (N+1)) : eq.symm $ add_sub_cancel'_right _ _
... > f.1 (N+1) + -1 : add_lt_add_left (abs_lt.1 $ HN _ (nat.lt_of_succ_le H)
(N+1) (nat.lt_succ_self _)).1 _ )⟩
noncomputable def lim : real :=
lim_sup f.1 (classical.some $ cau_seq.ub f) (classical.some $ cau_seq.lb f)
(classical.some_spec $ cau_seq.ub f)
(λ m, ⟨m, nat.le_refl _, classical.some_spec (cau_seq.lb f) m⟩)
def close (f : ℕ → real) (L : real) : Prop :=
∀ ε > 0, ∃ N, ∀ n > N, abs (f n - L) < ε
theorem lim_close : close f.1 (lim f) := λ ε Hε,
let ⟨N, HN⟩ := f.2 (ε/2) (half_pos Hε) in
⟨N, λ n hn, abs_lt_of_lt_of_neg_lt
(have H1 : f.1 n - ε/2 ≤ lim f,
from le_inf _ _ _ _ _ _ $ λ r ⟨m, hr⟩, hr.symm ▸
le_trans
(le_of_lt $ sub_lt_of_sub_lt (abs_lt.1 $ HN n hn _ $ lt_of_lt_of_le hn $ le_max_right _ _).2)
(le_sup _ _ _ _ _ _ ⟨max m n, le_max_left _ _, rfl⟩),
sub_lt_of_sub_lt $ lt_of_lt_of_le (sub_lt_sub_left (half_lt_self Hε) _) H1)
(calc -(f.1 n - lim f)
= lim f - f.1 n : neg_sub _ _
... ≤ _ - f.1 n : sub_le_sub_right (inf_le _ _ _ _ _ _ ⟨n, rfl⟩) _
... ≤ (f.1 n + ε/2) - f.1 n : sub_le_sub_right (sup_le _ _ _ _ _ _ $ λ r ⟨k, hk1, hk2⟩,
hk2.symm ▸ le_of_lt $ lt_add_of_sub_left_lt (abs_lt.1 $ HN k (gt_of_ge_of_gt hk1 hn) n hn).2) _
... = ε/2 : add_sub_cancel' _ _
... < ε : half_lt_self Hε)⟩
theorem lim_def (L : real) : lim f = L ↔ close f.1 L :=