forked from taorunz/euler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Misc.v
1711 lines (1586 loc) · 44.5 KB
/
Misc.v
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
(* We copied and modified this file from https://github.com/roglo/coq_euler_prod_form/blob/master/Misc.v *)
(* Theorems of general usage, which could be (or not) in Coq library *)
Set Nested Proofs Allowed.
Require Import Utf8 Arith Psatz Sorted Permutation Decidable.
Import List List.ListNotations.
(* "fast" lia, to improve compilation speed *)
Tactic Notation "flia" hyp_list(Hs) := clear - Hs; intros; nia.
Notation "x '∈' l" := (List.In x l) (at level 70).
Notation "x '∉' l" := (¬ List.In x l) (at level 70).
Notation "x ≤ y ≤ z" := (x <= y ∧ y <= z)%nat (at level 70, y at next level) :
nat_scope.
Notation "x < y ≤ z" := (x < y ∧ y <= z)%nat (at level 70, y at next level) :
nat_scope.
Notation "x ≤ y < z" := (x ≤ y ∧ y < z)%nat (at level 70, y at next level) :
nat_scope.
Notation "x < y < z" := (x < y ∧ y < z)%nat (at level 70, y at next level).
Notation "∃! x .. y , p" :=
(ex (unique (λ x, .. (ex (unique (λ y, p))) ..)))
(at level 200, x binder, right associativity)
: type_scope.
Definition List_combine_all {A} (l1 l2 : list A) (d : A) :=
let '(l'1, l'2) :=
match List.length l1 ?= List.length l2 with
| Eq => (l1, l2)
| Lt => (l1 ++ List.repeat d (List.length l2 - List.length l1), l2)
| Gt => (l1, l2 ++ List.repeat d (List.length l1 - List.length l2))
end
in
List.combine l'1 l'2.
Theorem filter_ext_in {A : Type} : forall (f g : A -> bool) (l : list A),
(forall a, a ∈ l -> f a = g a) -> filter f l = filter g l.
Proof.
intros f g l. induction l; intros. easy.
assert (forall a : A, a ∈ l -> f a = g a).
{ intros x Hx. apply H. simpl. right. easy.
}
assert (f a = g a).
{ apply H. simpl. left. easy.
}
simpl. rewrite IHl by easy. destruct (f a); rewrite <- H1; easy.
Qed.
Lemma filter_app {A : Type} (f : A -> bool) (l l' : list A) :
filter f (l ++ l') = filter f l ++ filter f l'.
Proof.
induction l. simpl. easy.
simpl. rewrite IHl. destruct (f a); easy.
Qed.
Theorem List_cons_app A (a : A) l : a :: l = [a] ++ l.
Proof. easy. Qed.
Theorem List_skipn_1 : ∀ A (l : list A), skipn 1 l = tl l.
Proof. easy. Qed.
Theorem List_fold_left_map :
∀ A B C (f : A → B → A) (g : C → B) (l : list C) a,
fold_left f (map g l) a = fold_left (λ c b, f c (g b)) l a.
Proof.
intros.
revert a.
induction l as [| c]; intros; [ easy | apply IHl ].
Qed.
(* summations *)
Notation "'Σ' ( i = b , e ) , g" :=
(fold_left (λ c i, c + g) (seq b (S e - b)) 0)
(at level 45, i at level 0, b at level 60, e at level 60) : nat_scope.
Theorem fold_left_add_fun_from_0 {A} : ∀ a l (f : A → nat),
fold_left (λ c i, c + f i) l a =
a + fold_left (λ c i, c + f i) l 0.
Proof.
intros.
revert a.
induction l as [| x l]; intros; [ symmetry; apply Nat.add_0_r | cbn ].
rewrite IHl; symmetry; rewrite IHl.
apply Nat.add_assoc.
Qed.
Theorem fold_left_mul_fun_from_1 {A} : ∀ a l (f : A → nat),
fold_left (λ c i, c * f i) l a =
a * fold_left (λ c i, c * f i) l 1.
Proof.
intros.
revert a.
induction l as [| x l]; intros; [ symmetry; apply Nat.mul_1_r | cbn ].
rewrite IHl; symmetry; rewrite IHl.
rewrite Nat.add_0_r.
apply Nat.mul_assoc.
Qed.
Theorem fold_left_mul_from_1 : ∀ a l,
fold_left Nat.mul l a = a * fold_left Nat.mul l 1.
Proof.
intros.
revert a.
induction l as [| x l]; intros; [ symmetry; apply Nat.mul_1_r | cbn ].
rewrite IHl; symmetry; rewrite IHl.
rewrite Nat.add_0_r.
apply Nat.mul_assoc.
Qed.
Theorem fold_right_max_ge : ∀ m l, m ≤ fold_right max m l.
Proof.
intros.
induction l as [| a]; [ easy | cbn ].
etransitivity; [ apply IHl | ].
apply Nat.le_max_r.
Qed.
Theorem summation_split_first : ∀ b e f,
b ≤ e
→ Σ (i = b, e), f i = f b + Σ (i = S b, e), f i.
Proof.
intros * Hbe.
rewrite Nat.sub_succ.
replace (S e - b) with (S (e - b)) by flia Hbe.
cbn.
apply fold_left_add_fun_from_0.
Qed.
Theorem summation_split_last : ∀ b e f,
b ≤ e
→ 1 ≤ e
→ Σ (i = b, e), f i = Σ (i = b, e - 1), f i + f e.
Proof.
intros * Hbe He.
destruct e; [ flia He | clear He ].
rewrite Nat.sub_succ, Nat.sub_0_r.
replace (S (S e) - b) with (S (S e - b)) by flia Hbe.
remember (S e - b) as n eqn:Hn.
revert b Hbe Hn.
induction n; intros. {
now replace (S e) with b by flia Hbe Hn.
}
remember (S n) as sn; cbn; subst sn.
rewrite fold_left_add_fun_from_0.
rewrite IHn; [ | flia Hn | flia Hn ].
rewrite Nat.add_assoc; f_equal; cbn.
now rewrite (fold_left_add_fun_from_0 (f b)).
Qed.
Theorem all_0_summation_0 : ∀ b e f,
(∀ i, b ≤ i ≤ e → f i = 0)
→ Σ (i = b, e), f i = 0.
Proof.
intros * Hz.
remember (S e - b) as n eqn:Hn.
revert b Hz Hn.
induction n; intros; [ easy | cbn ].
rewrite fold_left_add_fun_from_0.
rewrite IHn; [ | | flia Hn ]. {
rewrite Hz; [ easy | flia Hn ].
}
intros i Hi.
apply Hz; flia Hi.
Qed.
Ltac rewrite_in_summation th :=
let b := fresh "b" in
let e := fresh "e" in
let a := fresh "a" in
intros b e;
remember (S e - b) as n eqn:Hn;
remember 0 as a eqn:Ha; clear Ha;
revert e a b Hn;
induction n as [| n IHn]; intros; [ easy | cbn ];
rewrite th;
apply (IHn e); flia Hn.
Theorem summation_eq_compat : ∀ b e g h,
(∀ i, b ≤ i ≤ e → g i = h i)
→ Σ (i = b, e), g i = Σ (i = b, e), h i.
Proof.
intros * Hgh.
remember (S e - b) as n eqn:Hn.
remember 0 as a eqn:Ha; clear Ha.
revert e a b Hn Hgh.
induction n as [| n IHn]; intros; [ easy | cbn ].
rewrite Hgh; [ | flia Hn ].
rewrite (IHn e); [ easy | flia Hn | ].
intros i Hbie.
apply Hgh; flia Hbie.
Qed.
Theorem summation_le_compat: ∀ b e g h,
(∀ i, b ≤ i ≤ e → g i ≤ h i) → Σ (i = b, e), g i ≤ Σ (i = b, e), h i.
Proof.
intros * Hgh.
remember (S e - b) as n eqn:Hn.
remember 0 as a eqn:Ha; clear Ha.
revert a b Hn Hgh.
induction n as [| n IHn]; intros; [ easy | cbn ].
setoid_rewrite fold_left_add_fun_from_0.
do 2 rewrite <- Nat.add_assoc.
apply Nat.add_le_mono_l.
apply Nat.add_le_mono; [ apply Hgh; flia Hn | ].
apply IHn; [ flia Hn | ].
intros i Hbie.
apply Hgh; flia Hbie.
Qed.
Theorem mul_add_distr_r_in_summation : ∀ b e f g h,
Σ (i = b, e), (f i + g i) * h i =
Σ (i = b, e), (f i * h i + g i * h i).
Proof.
intros; revert b e.
rewrite_in_summation Nat.mul_add_distr_r.
Qed.
Theorem double_mul_assoc_in_summation : ∀ b e f g h k,
Σ (i = b, e), f i * g i * h i * k i = Σ (i = b, e), f i * (g i * h i * k i).
Proof.
intros.
assert (H : ∀ a b c d, a * b * c * d = a * (b * c * d)) by flia.
revert b e.
rewrite_in_summation H.
Qed.
Theorem mul_assoc_in_summation : ∀ b e f g h,
Σ (i = b, e), f i * g i * h i = Σ (i = b, e), f i * (g i * h i).
Proof.
intros.
assert (H : ∀ a b c, a * b * c = a * (b * c)) by flia.
revert b e.
rewrite_in_summation H.
Qed.
Theorem mul_comm_in_summation : ∀ b e f g,
Σ (i = b, e), f i * g i = Σ (i = b, e), g i * f i.
Proof.
intros.
assert (H : ∀ a b, a * b = b * a) by flia.
revert b e.
rewrite_in_summation H.
Qed.
Theorem mul_summation_distr_l : ∀ a b e f,
a * (Σ (i = b, e), f i) = Σ (i = b, e), a * f i.
Proof.
intros.
remember (S e - b) as n eqn:Hn.
revert e a b Hn.
induction n; intros; [ apply Nat.mul_0_r | cbn ].
rewrite fold_left_add_fun_from_0.
rewrite Nat.mul_add_distr_l.
rewrite (IHn e); [ | flia Hn ].
symmetry.
apply fold_left_add_fun_from_0.
Qed.
Theorem mul_summation_distr_r : ∀ a b e f,
(Σ (i = b, e), f i) * a = Σ (i = b, e), f i * a.
Proof.
intros.
rewrite Nat.mul_comm.
rewrite mul_summation_distr_l.
now rewrite mul_comm_in_summation.
Qed.
Theorem power_shuffle1_in_summation : ∀ b e a f g,
Σ (i = b, e), a * f i * a ^ (e - i) * g i =
Σ (i = b, e), f i * a ^ (S e - i) * g i.
Proof.
intros.
(* failed to be able to use "rewrite_in_summation" here *)
assert
(H : ∀ i e,
a * f i * a ^ (e - i) * g i = f i * a ^ (S (e - i)) * g i). {
clear e; intros; f_equal.
rewrite <- Nat.mul_assoc, Nat.mul_comm, <- Nat.mul_assoc.
f_equal.
rewrite Nat.mul_comm.
replace a with (a ^ 1) at 1 by apply Nat.pow_1_r.
now rewrite <- Nat.pow_add_r.
}
remember (S e - b) as n eqn:Hn.
remember 0 as z eqn:Hz; clear Hz.
revert e z b Hn.
induction n as [| n IHn]; intros; [ easy | ].
cbn - [ "-" ].
rewrite IHn; [ | flia Hn ].
f_equal; f_equal; rewrite H.
f_equal; f_equal; f_equal; flia Hn.
Qed.
Theorem power_shuffle2_in_summation : ∀ b e a c f,
Σ (i = b, e), c * f i * a ^ (e - i) * c ^ i =
Σ (i = b, e), f i * a ^ (e - i) * c ^ S i.
Proof.
intros.
remember (S e - b) as n eqn:Hn.
remember 0 as z eqn:Hz; clear Hz.
revert e z b Hn.
induction n as [| n IHn]; intros; [ easy | ].
cbn.
rewrite IHn; [ | flia Hn ].
f_equal; f_equal.
do 2 rewrite <- Nat.mul_assoc.
rewrite Nat.mul_comm.
do 3 rewrite <- Nat.mul_assoc.
f_equal; f_equal.
apply Nat.mul_comm.
Qed.
Theorem summation_add : ∀ b e f g,
Σ (i = b, e), (f i + g i) = Σ (i = b, e), f i + Σ (i = b, e), g i.
Proof.
intros.
remember (S e - b) as n eqn:Hn.
revert b Hn.
induction n; intros; [ easy | cbn ].
rewrite fold_left_add_fun_from_0.
rewrite IHn; [ | flia Hn ].
rewrite (fold_left_add_fun_from_0 (f b)).
rewrite (fold_left_add_fun_from_0 (g b)).
flia.
Qed.
Theorem summation_sub : ∀ b e f g,
(∀ i, b ≤ i ≤ e → g i ≤ f i)
→ Σ (i = b, e), (f i - g i) = Σ (i = b, e), f i - Σ (i = b, e), g i.
Proof.
intros * Hgf.
remember (S e - b) as n eqn:Hn.
revert b Hn Hgf.
induction n; intros; [ easy | cbn ].
rewrite fold_left_add_fun_from_0.
rewrite IHn; [ | flia Hn | ]. 2: {
intros i Hi.
apply Hgf; flia Hi.
}
rewrite (fold_left_add_fun_from_0 (f b)).
rewrite (fold_left_add_fun_from_0 (g b)).
rewrite Nat.sub_add_distr.
rewrite Nat.add_sub_swap. 2: {
apply Hgf.
split; [ easy | ].
flia Hn.
}
rewrite Nat.add_sub_assoc; [ easy | ].
assert (Hbe : b + n ≤ e) by flia Hn.
clear - Hbe Hgf.
revert b Hgf Hbe.
induction n; intros; [ easy | ].
replace (S n) with (n + 1) by flia.
rewrite seq_app.
do 2 rewrite fold_left_app.
setoid_rewrite fold_left_add_fun_from_0.
apply Nat.add_le_mono. 2: {
cbn.
apply Hgf.
flia Hbe.
}
apply IHn; [ easy | flia Hbe ].
Qed.
Theorem summation_succ_succ : ∀ b e f,
Σ (i = S b, S e), f i = Σ (i = b, e), f (S i).
Proof.
intros.
rewrite Nat.sub_succ.
remember (S e - b) as n eqn:Hn.
revert b Hn.
induction n; intros; [ easy | cbn ].
setoid_rewrite fold_left_add_fun_from_0.
rewrite IHn; [ easy | flia Hn ].
Qed.
Theorem summation_mod_idemp : ∀ b e f n,
(Σ (i = b, e), f i) mod n = (Σ (i = b, e), f i mod n) mod n.
Proof.
intros.
destruct (Nat.eq_dec n 0) as [Hnz| Hnz]; [ now subst n | ].
remember (S e - b) as m eqn:Hm.
revert b Hm.
induction m; intros; [ easy | cbn ].
rewrite (fold_left_add_fun_from_0 (f b)).
rewrite (fold_left_add_fun_from_0 (f b mod n)).
rewrite Nat.add_mod_idemp_l; [ | easy ].
rewrite <- Nat.add_mod_idemp_r; [ symmetry | easy ].
rewrite <- Nat.add_mod_idemp_r; [ symmetry | easy ].
f_equal; f_equal.
apply IHm; flia Hm.
Qed.
Lemma fold_left_seq_succ_last : ∀ g b len s,
fold_left (λ c i, c + g i) (seq b (S len)) s =
fold_left (λ c i, c + g i) (seq b len) s + g (b + len).
Proof.
intros.
revert b s.
induction len; intros; [ now cbn; rewrite Nat.add_0_r | ].
remember (S len) as x; cbn; subst x.
now rewrite IHlen, Nat.add_succ_comm.
Qed.
Theorem summation_rtl : ∀ g b k,
Σ (i = b, k), g i = Σ (i = b, k), g (k + b - i).
Proof.
intros g b k.
destruct (le_dec (S k) b) as [Hkb| Hkb]. {
cbn - [ "-" ].
now replace (S k - b) with 0 by flia Hkb.
}
apply Nat.nle_gt in Hkb.
apply -> Nat.lt_succ_r in Hkb.
remember 0 as s.
remember (S k - b) as len eqn:Hlen.
replace k with (b + len - 1) by flia Hkb Hlen; clear.
revert s b.
induction len; intros; [ easy | ].
rewrite fold_left_seq_succ_last.
rewrite IHlen; cbn.
rewrite Nat.add_sub.
replace (b + S len - 1) with (b + len) by flia.
rewrite <- seq_shift.
rewrite List_fold_left_map.
setoid_rewrite fold_left_add_fun_from_0.
rewrite Nat.add_shuffle0; f_equal.
destruct len; [ easy | ].
replace (S len) with (S (len + b) - b) by flia.
apply summation_eq_compat.
intros i Hi; f_equal.
flia.
Qed.
(* *)
Theorem match_id {A} : ∀ a (b : A), match a with O => b | S _ => b end = b.
Proof. now intros; destruct a. Qed.
Theorem Nat_sub_sub_swap : ∀ a b c, a - b - c = a - c - b.
Proof.
intros.
rewrite <- Nat.sub_add_distr.
rewrite Nat.add_comm.
now rewrite Nat.sub_add_distr.
Qed.
Theorem Nat_add_div_same : ∀ a b c,
Nat.divide c a
→ a / c + b / c = (a + b) / c.
Proof.
intros * Hca.
destruct (Nat.eq_dec c 0) as [Hcz| Hcz]; [ now subst c | ].
destruct Hca as (d, Hd).
rewrite Hd, Nat.div_mul; [ | easy ].
now rewrite Nat.div_add_l.
Qed.
Theorem Nat_sub_div_same: ∀ a b c,
Nat.divide c a
→ Nat.divide c b
→ a / c - b / c = (a - b) / c.
Proof.
intros * Hca Hcb.
destruct (Nat.eq_dec c 0) as [Hcz| Hcz]; [ now subst c | ].
destruct Hca as (ka, Hka).
destruct Hcb as (kb, Hkb).
subst a b.
rewrite Nat.div_mul; [ | easy ].
rewrite Nat.div_mul; [ | easy ].
rewrite <- Nat.mul_sub_distr_r.
now rewrite Nat.div_mul.
Qed.
Theorem Nat_sub_succ_1 : ∀ n, S n - 1 = n.
Proof. now intros; rewrite Nat.sub_succ, Nat.sub_0_r. Qed.
Theorem Nat_eq_mod_sub_0 : ∀ a b c,
a mod c = b mod c → (a - b) mod c = 0.
Proof.
intros * Hab.
destruct (Nat.eq_dec c 0) as [Hcz| Hcz].
subst c. simpl in *. lia.
specialize (Nat.div_mod a c Hcz) as H1.
specialize (Nat.div_mod b c Hcz) as H2.
rewrite H1, H2, Hab.
rewrite (Nat.add_comm (c * (b / c))).
rewrite Nat.sub_add_distr, Nat.add_sub.
rewrite <- Nat.mul_sub_distr_l, Nat.mul_comm.
now apply Nat.mod_mul.
Qed.
Theorem Nat_mod_add_r_mul_l : ∀ a b c,
b ≠ 0 → (a + b * c) mod b = a mod b.
Proof.
intros * Hbz.
rewrite Nat.mul_comm.
now apply Nat.mod_add.
Qed.
Theorem Nat_mod_add_l_mul_l : ∀ a b c,
b ≠ 0 → (b * c + a) mod b = a mod b.
Proof.
intros * Hbz.
rewrite Nat.add_comm, Nat.mul_comm.
now apply Nat.mod_add.
Qed.
Theorem Nat_mod_add_l_mul_r : ∀ a b c,
b ≠ 0 → (c * b + a) mod b = a mod b.
Proof.
intros * Hbz.
rewrite Nat.add_comm.
now apply Nat.mod_add.
Qed.
Theorem Nat_mod_0_mod_div : ∀ a b,
0 < b ≤ a → a mod b = 0 → a mod (a / b) = 0.
Proof.
intros * Hba Ha.
assert (Hbz : b ≠ 0) by flia Hba.
assert (Habz : a / b ≠ 0). {
intros H.
apply Nat.div_small_iff in H; [ | flia Hba ].
now apply Nat.nle_gt in H.
}
specialize (Nat.div_mod a (a / b) Habz) as H1.
specialize (Nat.div_mod a b Hbz) as H2.
rewrite Ha, Nat.add_0_r in H2.
rewrite H2 in H1 at 3.
rewrite Nat.div_mul in H1; [ | easy ].
rewrite Nat.mul_comm in H1.
flia H1 H2.
Qed.
Theorem Nat_mod_0_div_div : ∀ a b,
0 < b ≤ a → a mod b = 0 → a / (a / b) = b.
Proof.
intros * Hba Ha.
assert (Hbz : b ≠ 0) by flia Hba.
assert (Habz : a / b ≠ 0). {
intros H.
apply Nat.div_small_iff in H; [ | easy ].
now apply Nat.nle_gt in H.
}
specialize (Nat.div_mod a (a / b) Habz) as H1.
rewrite Nat_mod_0_mod_div in H1; [ | easy | easy ].
rewrite Nat.add_0_r in H1.
apply (Nat.mul_cancel_l _ _ (a / b)); [ easy | ].
rewrite <- H1; symmetry.
rewrite Nat.mul_comm.
apply Nat.mod_divide in Ha; [ | easy ].
rewrite <- Nat.divide_div_mul_exact; [ | easy | easy ].
now rewrite Nat.mul_comm, Nat.div_mul.
Qed.
Theorem Nat_fact_succ : ∀ n, fact (S n) = S n * fact n.
Proof. easy. Qed.
Theorem Nat_div_lt_le_mul : ∀ a b c, b ≠ 0 → a / b < c → a ≤ b * c.
Proof.
intros * Hbz Habc.
apply (Nat.mul_le_mono_l _ _ b) in Habc.
transitivity (b * S (a / b)); [ | easy ].
specialize (Nat.div_mod a b Hbz) as H1.
rewrite <- Nat.add_1_r.
rewrite Nat.mul_add_distr_l, Nat.mul_1_r.
rewrite H1 at 1.
apply Nat.add_le_mono_l.
now apply Nat.lt_le_incl, Nat.mod_upper_bound.
Qed.
Theorem Nat_divide_fact_fact : ∀ n d, Nat.divide (fact (n - d)) (fact n).
Proof.
intros *.
revert n.
induction d; intros; [ rewrite Nat.sub_0_r; apply Nat.divide_refl | ].
destruct n; [ apply Nat.divide_refl | ].
rewrite Nat.sub_succ.
apply (Nat.divide_trans _ (fact n)); [ apply IHd | ].
rewrite Nat_fact_succ.
now exists (S n).
Qed.
Theorem Nat_divide_small_fact : ∀ n k, 0 < k ≤ n → Nat.divide k (fact n).
Proof.
intros * Hkn.
revert k Hkn.
induction n; intros; [ flia Hkn | ].
rewrite Nat_fact_succ.
destruct (Nat.eq_dec k (S n)) as [Hksn| Hksn]. {
rewrite Hksn.
apply Nat.divide_factor_l.
}
apply (Nat.divide_trans _ (fact n)). {
apply IHn; flia Hkn Hksn.
}
apply Nat.divide_factor_r.
Qed.
Theorem Nat_divide_mul_fact : ∀ n a b,
0 < a ≤ n
→ 0 < b ≤ n
→ a < b
→ Nat.divide (a * b) (fact n).
Proof.
intros * Han Hbn Hab.
exists (fact (a - 1) * (fact (b - 1) / fact a) * (fact n / fact b)).
rewrite Nat.mul_comm.
rewrite (Nat.mul_shuffle0 _ b).
do 2 rewrite Nat.mul_assoc.
replace (a * fact (a - 1)) with (fact a). 2: {
destruct a; [ flia Han | ].
rewrite Nat_fact_succ.
now rewrite Nat.sub_succ, Nat.sub_0_r.
}
replace (fact a * (fact (b - 1) / fact a)) with (fact (b - 1)). 2: {
specialize (Nat_divide_fact_fact (b - 1) (b - 1 - a)) as H1.
replace (b - 1 - (b - 1 - a)) with a in H1 by flia Hab.
destruct H1 as (c, Hc).
rewrite Hc, Nat.div_mul; [ | apply fact_neq_0 ].
apply Nat.mul_comm.
}
rewrite Nat.mul_comm, Nat.mul_assoc.
replace (b * fact (b - 1)) with (fact b). 2: {
destruct b; [ flia Hbn | ].
rewrite Nat_fact_succ.
now rewrite Nat.sub_succ, Nat.sub_0_r.
}
replace (fact b * (fact n / fact b)) with (fact n). 2: {
specialize (Nat_divide_fact_fact n (n - b)) as H1.
replace (n - (n - b)) with b in H1 by flia Hbn.
destruct H1 as (c, Hc).
rewrite Hc, Nat.div_mul; [ | apply fact_neq_0 ].
apply Nat.mul_comm.
}
easy.
Qed.
(** Bezout commutes *)
Theorem Nat_bezout_comm : ∀ a b g,
b ≠ 0 → Nat.Bezout a b g → Nat.Bezout b a g.
Proof.
intros * Hbz (u & v & Huv).
destruct (Nat.eq_0_gt_0_cases a) as [Haz| Haz]. {
rewrite Haz in Huv |-*.
rewrite Nat.mul_0_r in Huv; symmetry in Huv.
apply Nat.eq_add_0 in Huv.
rewrite (proj1 Huv).
now exists 0, 0; Nat.nzsimpl.
}
apply Nat.neq_0_lt_0 in Haz.
destruct (Nat.lt_trichotomy (u / b) (v / a)) as [Hm|Hm]. {
apply Nat.lt_le_incl in Hm.
remember (v / a + 1) as k eqn:Hk.
exists (k * a - v), (k * b - u).
do 2 rewrite Nat.mul_sub_distr_r.
rewrite Huv.
rewrite (Nat.add_comm _ (v * b)).
rewrite Nat.sub_add_distr.
rewrite Nat.add_sub_assoc. 2: {
apply (Nat.add_le_mono_r _ _ (v * b)).
rewrite <- Huv.
rewrite Nat.sub_add. 2: {
rewrite Nat.mul_shuffle0.
apply Nat.mul_le_mono_r.
rewrite Hk.
specialize (Nat.div_mod v a Haz) as H1.
rewrite Nat.mul_add_distr_r, Nat.mul_1_l, Nat.mul_comm.
rewrite H1 at 1.
apply Nat.add_le_mono_l.
apply Nat.lt_le_incl.
apply Nat.mod_bound_pos; [ apply Nat.le_0_l | ].
now apply Nat.neq_0_lt_0.
}
apply Nat.mul_le_mono_r.
rewrite Hk.
specialize (Nat.div_mod u b Hbz) as H1.
rewrite Nat.mul_add_distr_r, Nat.mul_1_l, Nat.mul_comm.
rewrite H1 at 1.
apply Nat.add_le_mono; [ now apply Nat.mul_le_mono_l | ].
apply Nat.lt_le_incl.
apply Nat.mod_bound_pos; [ apply Nat.le_0_l | ].
now apply Nat.neq_0_lt_0.
}
rewrite Nat.add_comm, Nat.add_sub.
now rewrite Nat.mul_shuffle0.
} {
remember (u / b + 1) as k eqn:Hk.
exists (k * a - v), (k * b - u).
do 2 rewrite Nat.mul_sub_distr_r.
rewrite Huv.
rewrite (Nat.add_comm _ (v * b)).
rewrite Nat.sub_add_distr.
rewrite Nat.add_sub_assoc. 2: {
apply (Nat.add_le_mono_r _ _ (v * b)).
rewrite Nat.sub_add. 2: {
rewrite Nat.mul_shuffle0.
apply Nat.mul_le_mono_r.
rewrite Hk.
specialize (Nat.div_mod v a Haz) as H1.
rewrite Nat.mul_add_distr_r, Nat.mul_1_l, Nat.mul_comm.
rewrite H1 at 1.
apply Nat.add_le_mono. {
apply Nat.mul_le_mono_l.
destruct Hm as [Hm| Hm]; [ now rewrite Hm | ].
now apply Nat.lt_le_incl.
}
apply Nat.lt_le_incl.
apply Nat.mod_bound_pos; [ apply Nat.le_0_l | ].
now apply Nat.neq_0_lt_0.
}
rewrite <- Huv.
apply Nat.mul_le_mono_r.
rewrite Hk.
specialize (Nat.div_mod u b Hbz) as H1.
rewrite Nat.mul_add_distr_r, Nat.mul_1_l, Nat.mul_comm.
rewrite H1 at 1.
apply Nat.add_le_mono_l.
apply Nat.lt_le_incl.
apply Nat.mod_bound_pos; [ apply Nat.le_0_l | ].
now apply Nat.neq_0_lt_0.
}
rewrite Nat.add_comm, Nat.add_sub.
now rewrite Nat.mul_shuffle0.
}
Qed.
Theorem Nat_bezout_mul : ∀ a b c,
Nat.Bezout a c 1
→ Nat.Bezout b c 1
→ Nat.Bezout (a * b) c 1.
Proof.
intros * (ua & uc & Hu) (vb & vc & Hv).
exists (ua * vb).
replace (ua * vb * (a * b)) with ((ua * a) * (vb * b)) by flia.
rewrite Hu, Hv.
exists (uc * vc * c + uc + vc).
ring.
Qed.
Theorem Nat_gcd_le_r : ∀ a b, b ≠ 0 → Nat.gcd a b ≤ b.
Proof.
intros * Hbz.
specialize (Nat.gcd_divide_r a b) as H1.
destruct H1 as (c, Hc); rewrite Hc at 2.
destruct c; [ easy | flia ].
Qed.
Theorem Nat_gcd_1_mul_l : ∀ a b c,
Nat.gcd a c = 1
→ Nat.gcd b c = 1
→ Nat.gcd (a * b) c = 1.
Proof.
intros * Hac Hbc.
destruct (Nat.eq_dec c 0) as [Hcz| Hcz]. {
now subst c; rewrite Nat.gcd_comm in Hac, Hbc; cbn in Hac, Hbc; subst a b.
}
destruct (Nat.eq_dec a 0) as [Haz| Haz]; [ now subst a | ].
destruct (Nat.eq_dec b 0) as [Hbz| Hbz]. {
now subst b; rewrite Nat.mul_0_r.
}
apply Nat.bezout_1_gcd.
apply Nat_bezout_mul. {
rewrite <- Hac.
apply Nat.gcd_bezout_pos.
flia Haz.
} {
rewrite <- Hbc.
apply Nat.gcd_bezout_pos.
flia Hbz.
}
Qed.
Theorem Nat_gcd_1_mul_r : ∀ a b c,
Nat.gcd a b = 1
→ Nat.gcd a c = 1
→ Nat.gcd a (b * c) = 1.
Proof.
intros * Hab Hac.
rewrite Nat.gcd_comm.
now apply Nat_gcd_1_mul_l; rewrite Nat.gcd_comm.
Qed.
Theorem Nat_gcd_sub_diag_l : ∀ m n, n ≤ m → Nat.gcd m (m - n) = Nat.gcd m n.
Proof.
intros * Hnm.
replace m with (n + (m - n)) at 1 by flia Hnm.
rewrite Nat.gcd_comm.
rewrite Nat.gcd_add_diag_r.
rewrite Nat.gcd_comm.
rewrite Nat.gcd_sub_diag_r; [ | easy ].
apply Nat.gcd_comm.
Qed.
(* (a ^ b) mod c defined like that so that we can use "Compute"
for testing; proved equal to (a ^ b) mod c just below *)
Fixpoint Nat_pow_mod_loop a b c :=
match b with
| 0 => 1 mod c
| S b' => (a * Nat_pow_mod_loop a b' c) mod c
end.
Definition Nat_pow_mod a b c := Nat_pow_mod_loop a b c.
Theorem Nat_pow_mod_is_pow_mod : ∀ a b c,
c ≠ 0 → Nat_pow_mod a b c = (a ^ b) mod c.
Proof.
intros * Hcz.
revert a.
induction b; intros; [ easy | ].
cbn; rewrite IHb.
now rewrite Nat.mul_mod_idemp_r.
Qed.
Theorem Nat_pow_sub_pow : ∀ a b n,
n ≠ 0
→ b ≤ a
→ a ^ n - b ^ n =
(a - b) * Σ (i = 0, n - 1), a ^ (n - i - 1) * b ^ i.
Proof.
intros * Hnz Hba.
destruct n; [ easy | clear Hnz ].
induction n; [ now cbn; do 3 rewrite Nat.mul_1_r | ].
remember (S n) as sn; cbn - [ "-" ]; subst sn.
rewrite <- (Nat.sub_add (a * b ^ S n) (a * a ^ S n)). 2: {
apply Nat.mul_le_mono_l.
now apply Nat.pow_le_mono_l.
}
rewrite <- Nat.mul_sub_distr_l.
rewrite <- Nat.add_sub_assoc; [ | now apply Nat.mul_le_mono_r ].
rewrite <- Nat.mul_sub_distr_r.
rewrite (Nat.mul_comm a).
rewrite IHn, <- Nat.mul_assoc.
rewrite <- Nat.mul_add_distr_l; f_equal.
do 2 rewrite Nat.sub_succ.
replace (n - 0) with n by now rewrite Nat.sub_0_r.
replace (S n - 0) with (S n) at 2 by now rewrite Nat.sub_0_r.
rewrite (summation_split_last _ (S n)); [ | flia | flia ].
rewrite Nat.sub_succ.
replace (n - 0) with n by now rewrite Nat.sub_0_r.
replace (S (S n) - S n - 1) with 0 by flia.
rewrite Nat.pow_0_r, Nat.mul_1_l.
f_equal.
rewrite mul_summation_distr_r.
apply summation_eq_compat.
intros i Hi.
rewrite Nat.mul_shuffle0; f_equal.
rewrite <- (Nat.pow_1_r a) at 2.
rewrite <- Nat.pow_add_r.
f_equal; flia Hi.
Qed.
Theorem Nat_sqr_sub_sqr : ∀ a b, a ^ 2 - b ^ 2 = (a + b) * (a - b).
Proof.
intros.
destruct (lt_dec a b) as [Hab| Hba]. {
rewrite (proj2 (Nat.sub_0_le _ _)). 2: {
now apply Nat.pow_le_mono_l, Nat.lt_le_incl.
}
rewrite (proj2 (Nat.sub_0_le _ _)). 2: {
now apply Nat.lt_le_incl.
}
now rewrite Nat.mul_0_r.
}
apply Nat.nlt_ge in Hba.
rewrite Nat.mul_add_distr_r.
rewrite Nat.mul_sub_distr_l.
rewrite Nat.mul_sub_distr_l.
rewrite Nat.add_sub_assoc; [ | now apply Nat.mul_le_mono_l ].
rewrite (Nat.mul_comm b).
rewrite Nat.sub_add; [ | now apply Nat.mul_le_mono_l ].
now do 2 rewrite Nat.pow_2_r.
Qed.
Theorem Nat_sqr_sub_1 : ∀ a, a ^ 2 - 1 = (a + 1) * (a - 1).
Proof.
intros.
destruct (Nat.eq_dec a 0) as [Haz| Haz]; [ now subst a | ].
rewrite Nat.mul_add_distr_r, Nat.mul_1_l.
rewrite Nat.mul_sub_distr_l, Nat.mul_1_r.
rewrite Nat.add_sub_assoc; [ | flia Haz ].
rewrite Nat.pow_2_r.
rewrite Nat.sub_add; [ easy | ].
destruct a; [ easy | flia ].
Qed.
Theorem Nat_sub_sub_assoc : ∀ a b c,
c ≤ b ≤ a + c
→ a - (b - c) = a + c - b.
Proof.
intros * (Hcb, Hba).
revert a c Hcb Hba.
induction b; intros.
-apply Nat.le_0_r in Hcb; subst c.
now rewrite Nat.add_0_r.
-destruct c; [ now rewrite Nat.add_0_r | ].
apply Nat.succ_le_mono in Hcb.
rewrite Nat.add_succ_r in Hba.
apply Nat.succ_le_mono in Hba.
specialize (IHb a c Hcb Hba) as H1.
rewrite Nat.sub_succ, H1.
rewrite Nat.add_succ_r.
now rewrite Nat.sub_succ.
Qed.
Theorem Nat_sub_sub_distr : ∀ a b c, c ≤ b ≤ a → a - (b - c) = a - b + c.
Proof.
intros.
rewrite <- Nat.add_sub_swap; [ | easy ].
apply Nat_sub_sub_assoc.
split; [ easy | ].
apply (Nat.le_trans _ a); [ easy | ].
apply Nat.le_add_r.
Qed.
Theorem Nat_sqr_sub : ∀ a b, b ≤ a → (a - b) ^ 2 = a ^ 2 + b ^ 2 - 2 * a * b.
Proof.
intros * Hba.
do 3 rewrite Nat.pow_2_r.
rewrite Nat.mul_sub_distr_l.
do 2 rewrite Nat.mul_sub_distr_r.
rewrite (Nat.mul_comm b).
rewrite <- Nat.sub_add_distr.
rewrite Nat.add_comm.
rewrite Nat.sub_add_distr.
rewrite Nat_sub_sub_distr. 2: {
split; [ now apply Nat.mul_le_mono_r | now apply Nat.mul_le_mono_l ].
}
replace 2 with (1 + 1) by easy.
rewrite Nat.mul_add_distr_r, Nat.mul_1_l.
rewrite Nat.mul_add_distr_r.
rewrite Nat.sub_add_distr; f_equal.
rewrite Nat.add_sub_swap; [ easy | ].
now apply Nat.mul_le_mono_l.
Qed.
Theorem Nat_sqr_add : ∀ a b, (a + b) ^ 2 = a ^ 2 + b ^ 2 + 2 * a * b.
Proof.
intros.
do 3 rewrite Nat.pow_2_r; flia.
Qed.
Theorem Nat_mod_pow_mod : ∀ a b c, (a mod b) ^ c mod b = a ^ c mod b.
Proof.
intros.
destruct (Nat.eq_dec b 0) as [Hbz| Hbz]; [ now subst b | ].
revert a b Hbz.
induction c; intros; [ easy | cbn ].
rewrite Nat.mul_mod_idemp_l; [ | easy ].
rewrite <- Nat.mul_mod_idemp_r; [ | easy ].
rewrite IHc; [ | easy ].
now rewrite Nat.mul_mod_idemp_r.
Qed.
Notation "a ≡ b 'mod' c" := (a mod c = b mod c) (at level 70, b at level 36).
Notation "a ≢ b 'mod' c" := (a mod c ≠ b mod c) (at level 70, b at level 36).
Theorem Nat_mul_mod_cancel_r : ∀ a b c n,
Nat.gcd c n = 1
→ a * c ≡ (b * c) mod n
→ a ≡ b mod n.
Proof.
intros * Hg Hab.
destruct (Nat.eq_dec n 0) as [Hnz| Hnz].
subst n. rewrite Nat.gcd_0_r in Hg.
simpl in *. lia.
destruct (le_dec b a) as [Hba| Hba]. {
apply Nat_eq_mod_sub_0 in Hab.
rewrite <- Nat.mul_sub_distr_r in Hab.
apply Nat.mod_divide in Hab; [ | easy ].
rewrite Nat.gcd_comm in Hg.
rewrite Nat.mul_comm in Hab.
specialize (Nat.gauss n c (a - b) Hab Hg) as H1.
destruct H1 as (k, Hk).
replace a with (b + k * n) by flia Hba Hk.
now rewrite Nat.mod_add.
} {
apply Nat.nle_gt in Hba.
symmetry in Hab.
apply Nat_eq_mod_sub_0 in Hab.
rewrite <- Nat.mul_sub_distr_r in Hab.
apply Nat.mod_divide in Hab; [ | easy ].
rewrite Nat.gcd_comm in Hg.
rewrite Nat.mul_comm in Hab.
specialize (Nat.gauss n c (b - a) Hab Hg) as H1.
destruct H1 as (k, Hk).
replace b with (a + k * n) by flia Hba Hk.
now rewrite Nat.mod_add.
}
Qed.
Theorem Nat_mul_mod_cancel_l : ∀ a b c n,
Nat.gcd c n = 1
→ c * a ≡ (c * b) mod n