-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathModulus.v
1806 lines (1624 loc) · 47.8 KB
/
Modulus.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
(** Some lemmas about nats, especially about div and mod *)
Require Import Prelim.
(** * Automation extending that in Prelim *)
Ltac bdestruct_one :=
let fail_if_iffy H :=
match H with
| context[if _ then _ else _] => fail 1
| _ => idtac
end
in
match goal with
| |- context [ ?a <? ?b ] => fail_if_iffy a; fail_if_iffy b; bdestruct (a <? b)
| |- context [ ?a <=? ?b ] => fail_if_iffy a; fail_if_iffy b; bdestruct (a <=? b)
| |- context [ ?a =? ?b ] => fail_if_iffy a; fail_if_iffy b; bdestruct (a =? b)
| |- context[if ?b then _ else _]
=> fail_if_iffy b; destruct b eqn:?
end.
Ltac bdestructΩ' :=
let tryeasylia := try easy; try lia in
repeat (bdestruct_one; subst; tryeasylia);
tryeasylia.
Ltac replace_bool_lia b0 b1 :=
first [
replace b0 with b1 by (bdestruct b0; lia || (destruct b1 eqn:?; lia)) |
replace b0 with b1 by (bdestruct b1; lia || (destruct b0 eqn:?; lia)) |
replace b0 with b1 by (bdestruct b0; bdestruct b1; lia)
].
Ltac simpl_bools :=
repeat (cbn [andb orb negb xorb];
rewrite ?andb_true_r, ?andb_false_r, ?orb_true_r, ?orb_false_r).
Ltac simplify_bools_lia_one_free :=
let act_T b := ((replace_bool_lia b true || replace_bool_lia b false); simpl) in
let act_F b := ((replace_bool_lia b false || replace_bool_lia b true); simpl) in
match goal with
| |- context[?b && _] => act_F b; rewrite ?andb_true_l, ?andb_false_l
| |- context[_ && ?b] => act_F b; rewrite ?andb_true_r, ?andb_false_r
| |- context[?b || _] => act_T b; rewrite ?orb_true_l, ?orb_false_l
| |- context[_ || ?b] => act_T b; rewrite ?orb_true_r, ?orb_false_r
| |- context[negb ?b] => act_T b; simpl negb
| |- context[if ?b then _ else _] => act_T b
end; simpl_bools.
Ltac simplify_bools_lia_one_kernel :=
let fail_if_iffy H :=
match H with
| context [ if _ then _ else _ ] => fail 1
| _ => idtac
end
in
let fail_if_compound H :=
fail_if_iffy H;
match H with
| context [ ?a && ?b ] => fail 1
| context [ ?a || ?b ] => fail 1
| _ => idtac
end
in
let act_T b := (fail_if_compound b;
(replace_bool_lia b true || replace_bool_lia b false); simpl) in
let act_F b := (fail_if_compound b;
(replace_bool_lia b false || replace_bool_lia b true); simpl) in
match goal with
| |- context[?b && _] => act_F b; rewrite ?andb_true_l, ?andb_false_l
| |- context[_ && ?b] => act_F b; rewrite ?andb_true_r, ?andb_false_r
| |- context[?b || _] => act_T b; rewrite ?orb_true_l, ?orb_false_l
| |- context[_ || ?b] => act_T b; rewrite ?orb_true_r, ?orb_false_r
| |- context[negb ?b] => act_T b; simpl negb
| |- context[if ?b then _ else _] => act_T b
end; simpl_bools.
Ltac simplify_bools_lia_many_kernel :=
let fail_if_iffy H :=
match H with
| context [ if _ then _ else _ ] => fail 1
| _ => idtac
end
in
let fail_if_compound H :=
fail_if_iffy H;
match H with
| context [ ?a && ?b ] => fail 1
| context [ ?a || ?b ] => fail 1
| _ => idtac
end
in
let act_T b := (fail_if_compound b;
(replace_bool_lia b true || replace_bool_lia b false); simpl) in
let act_F b := (fail_if_compound b;
(replace_bool_lia b false || replace_bool_lia b true); simpl) in
multimatch goal with
| |- context[?b && _] => act_F b; rewrite ?andb_true_l, ?andb_false_l
| |- context[_ && ?b] => act_F b; rewrite ?andb_true_r, ?andb_false_r
| |- context[?b || _] => act_T b; rewrite ?orb_true_l, ?orb_false_l
| |- context[_ || ?b] => act_T b; rewrite ?orb_true_r, ?orb_false_r
| |- context[negb ?b] => act_T b; simpl negb
| |- context[if ?b then _ else _] => act_T b
end; simpl_bools.
Ltac simplify_bools_lia_one :=
simplify_bools_lia_one_kernel || simplify_bools_lia_one_free.
Ltac simplify_bools_lia :=
repeat simplify_bools_lia_one.
Ltac bdestruct_one_old :=
let fail_if_iffy H :=
match H with
| context [ if _ then _ else _ ] => fail 1
| _ => idtac
end
in
match goal with
| |- context [ ?a <? ?b ] =>
fail_if_iffy a; fail_if_iffy b; bdestruct (a <? b)
| |- context [ ?a <=? ?b ] =>
fail_if_iffy a; fail_if_iffy b; bdestruct (a <=? b)
| |- context [ ?a =? ?b ] =>
fail_if_iffy a; fail_if_iffy b; bdestruct (a =? b)
| |- context [ if ?b then _ else _ ] => fail_if_iffy b; destruct b eqn:?
end.
Ltac bdestruct_one_new :=
let fail_if_iffy H :=
match H with
| context [ if _ then _ else _ ] => fail 1
| _ => idtac
end
in
let fail_if_booley H :=
fail_if_iffy H;
match H with
| context [ ?a <? ?b ] => fail 1
| context [ ?a <=? ?b ] => fail 1
| context [ ?a =? ?b ] => fail 1
| context [ ?a && ?b ] => fail 1
| context [ ?a || ?b ] => fail 1
| context [ negb ?a ] => fail 1
| context [ xorb ?a ?b ] => fail 1
| _ => idtac
end
in
let rec destruct_kernel H :=
match H with
| context [ if ?b then _ else _ ] => destruct_kernel b
| context [ ?a <? ?b ] =>
tryif fail_if_booley a then
(tryif fail_if_booley b then bdestruct (a <? b)
else destruct_kernel b) else (destruct_kernel a)
| context [ ?a <=? ?b ] =>
tryif fail_if_booley a then
(tryif fail_if_booley b then bdestruct (a <=? b)
else destruct_kernel b) else (destruct_kernel a)
| context [ ?a =? ?b ] =>
tryif fail_if_booley a then
(tryif fail_if_booley b then bdestruct (a =? b); try subst
else destruct_kernel b) else (destruct_kernel a)
| context [ ?a && ?b ] =>
destruct_kernel a || destruct_kernel b
| context [ ?a || ?b ] =>
destruct_kernel a || destruct_kernel b
| context [ xorb ?a ?b ] =>
destruct_kernel a || destruct_kernel b
| context [ negb ?a ] =>
destruct_kernel a
| _ => idtac
end
in
simpl_bools;
match goal with
| |- context [ ?a =? ?b ] =>
fail_if_iffy a; fail_if_iffy b; bdestruct (a =? b); try subst
| |- context [ ?a <? ?b ] =>
fail_if_iffy a; fail_if_iffy b; bdestruct (a <? b)
| |- context [ ?a <=? ?b ] =>
fail_if_iffy a; fail_if_iffy b; bdestruct (a <=? b)
| |- context [ if ?b then _ else _ ] => fail_if_iffy b; destruct b eqn:?
end;
simpl_bools.
Ltac bdestruct_one' := bdestruct_one_new || bdestruct_one_old.
Ltac bdestructΩ'_with tac :=
tac;
repeat (bdestruct_one'; subst; simpl_bools; tac);
tac.
(* Ltac bdestructΩ'simp :=
bdestructΩ'_with ltac:(try easy + lca + lia). *)
Lemma pow2_nonzero n : 2 ^ n <> 0.
Proof.
apply Nat.pow_nonzero; lia.
Qed.
Ltac show_term_nonzero term :=
match term with
| 2 ^ ?a => exact (pow2_nonzero a)
| ?a ^ ?b => exact (Nat.pow_nonzero a b ltac:(show_term_nonzero a))
| ?a * ?b =>
(assert (a <> 0) by (show_term_nonzero a);
assert (b <> 0) by (show_term_nonzero b);
lia)
| ?a + ?b =>
((assert (a <> 0) by (show_term_nonzero a) ||
assert (b <> 0) by (show_term_nonzero b));
lia)
| _ => lia
| _ => nia
end.
Ltac show_nonzero :=
match goal with
| |- ?t <> 0 => show_term_nonzero t
| |- 0 <> ?t => symmetry; show_term_nonzero t
| |- 0 < ?t => assert (t <> 0) by (show_term_nonzero t); lia
| |- ?t > 0 => assert (t <> 0) by (show_term_nonzero t); lia
| _ => lia
end.
Ltac get_div_by_pow_2 t pwr :=
match t with
| 2 ^ pwr => constr:(1)
| 2 ^ pwr * ?a => constr:(a)
| ?a * 2 ^ pwr => constr:(a)
| ?a * ?b => let ra := get_div_by_pow_2 a pwr in constr:(ra * b)
| ?a * ?b => let rb := get_div_by_pow_2 b pwr in constr:(a * rb)
| 2 ^ (?a + ?b) =>
let val := constr:(2 ^ a * 2 ^ b) in
get_div_by_pow_2 val pwr
| ?a + ?b =>
let ra := get_div_by_pow_2 a pwr in
let rb := get_div_by_pow_2 b pwr in
constr:(ra + rb)
| ?a - 1 =>
let ra := get_div_by_pow_2 a pwr in
constr:(ra - 1)
end.
Lemma div_mul_l a b : a <> 0 ->
(a * b) / a = b.
Proof.
rewrite Nat.mul_comm;
apply Nat.div_mul.
Qed.
Ltac show_div_by_pow2_ge t pwr :=
(* Shows t / 2 ^ pwr <= get_div_by_pwr t pwr *)
match t with
| 2 ^ pwr => (* constr:(1) *)
rewrite (Nat.div_same (2^pwr) (pow2_nonzero pwr));
apply Nat.le_refl
| 2 ^ pwr * ?a => (* constr:(a) *)
rewrite (div_mul_l (2^pwr) a (pow2_nonzero pwr));
apply Nat.le_refl
| ?a * 2 ^ pwr => (* constr:(a) *)
rewrite (Nat.div_mul a (2^pwr) (pow2_nonzero pwr));
apply Nat.le_refl
| ?a * (?b * ?c) =>
let rval := constr:(a * b * c) in
show_div_by_pow2_ge rval pwr
| ?a * ?b => (* b is not right, so... *)
let rval := constr:(b * a) in
show_div_by_pow2_ge rval pwr
| ?a + ?b =>
let ra := get_div_by_pow_2 a pwr in
let rb := get_div_by_pow_2 b pwr in
constr:(ra + rb)
| ?a - 1 =>
fail 1 "Case not supported"
| 2 ^ (?a + ?b) =>
let val := constr:(2 ^ a * 2 ^ b) in
rewrite (Nat.pow_add_r 2 a b);
show_div_by_pow2_ge val pwr
end.
Ltac get_div_by t val :=
match t with
| val => constr:(1)
| val * ?a => constr:(a)
| ?a * val => constr:(a)
| ?a * ?b => let ra := get_div_by a val in constr:(ra * b)
| ?a * ?b => let rb := get_div_by b val in constr:(a * rb)
| 2 ^ (?a + ?b) =>
let val' := constr:(2 ^ a * 2 ^ b) in
get_div_by val' val
| ?a + ?b =>
let ra := get_div_by a val in
let rb := get_div_by b val in
constr:(ra + rb)
| ?a - 1 =>
let ra := get_div_by a val in
constr:(ra - 1)
end.
Ltac show_div_by_ge t val :=
(* Shows t / val <= get_div_by t val *)
match t with
| val => (* constr:(1) *)
rewrite (Nat.div_same val ltac:(show_term_nonzero val));
apply Nat.le_refl
| val * ?a => (* constr:(a) *)
rewrite (div_mul_l val a ltac:(show_term_nonzero val));
apply Nat.le_refl
| ?a * val => (* constr:(a) *)
rewrite (Nat.div_mul a val ltac:(show_term_nonzero val));
apply Nat.le_refl
| ?a * (?b * ?c) =>
let rval := constr:(a * b * c) in
show_div_by_ge rval val
| ?a * ?b => (* b is not right, so... *)
let rval := constr:(b * a) in
show_div_by_ge rval val
| ?a + ?b =>
let ra := get_div_by a val in
let rb := get_div_by b val in
constr:(ra + rb)
| ?a - 1 =>
nia ||
fail 1 "Case not supported"
end.
Ltac get_strict_upper_bound term :=
match term with
| ?k mod 0 => let r := get_strict_upper_bound k in constr:(r)
| ?k mod (2 ^ ?a) => constr:(Nat.pow 2 a)
| ?k mod (?a ^ ?b) => constr:(Nat.pow a b)
| ?k mod ?a =>
let _ := match goal with |- _ => assert (H: a <> 0) by show_nonzero end in
constr:(a)
| ?k mod ?a =>
let _ := match goal with |- _ => assert (H: a = 0) by lia end in
constr:(k + 1)
| 2 ^ ?a * ?t => let r := get_strict_upper_bound t in
constr:(Nat.mul (Nat.pow 2 a) r)
| ?t * 2 ^ ?a => let r := get_strict_upper_bound t in
constr:(Nat.mul r (Nat.pow 2 a))
| ?a ^ ?b => constr:(Nat.pow a b + 1)
| ?a + ?b =>
let ra := get_strict_upper_bound a in
let rb := get_strict_upper_bound b in
constr:(ra + rb + 1)
| ?a * ?b =>
let ra := get_strict_upper_bound a in
let rb := get_strict_upper_bound b in
constr:(ra * rb + 1)
| ?a / (?b * (?c * ?d)) => let rval := constr:(a / (b * c * d)) in
let r := get_strict_upper_bound rval in constr:(r)
| ?a / (?b * ?c) => let rval := constr:(a / b / c) in
let r := get_strict_upper_bound rval in constr:(r)
| ?a / (2 ^ ?b) =>
let ra := get_strict_upper_bound a in
let rr := get_div_by_pow_2 ra b in constr:(rr)
| ?t => match goal with
| H : t < ?a |- _ => constr:(a)
| H : t <= ?a |- _ => constr:(a + 1)
| _ => constr:(t + 1)
end
end.
Ltac get_upper_bound term :=
match term with
| ?k mod 0 => let r := get_upper_bound k in constr:(r)
| ?k mod (2 ^ ?a) => constr:(Nat.sub (Nat.pow 2 a) 1)
| ?k mod (?a ^ ?b) => constr:(Nat.sub (Nat.pow a b) 1)
| ?k mod ?a =>
let H := fresh in
let _ := match goal with |- _ =>
assert (H: a <> 0) by show_nonzero; clear H end in
constr:(a - 1)
| ?k mod ?a =>
let H := fresh in
let _ := match goal with |- _ =>
assert (H: a = 0) by lia; clear H end in
let rk := get_upper_bound k in
constr:(rk)
| 2 ^ ?a * ?t => let r := get_upper_bound t in
constr:(Nat.mul (Nat.pow 2 a) r)
| ?t * 2 ^ ?a => let r := get_upper_bound t in
constr:(Nat.mul r (Nat.pow 2 a))
| ?a ^ ?b => constr:(Nat.pow a b)
| ?a + ?b =>
let ra := get_upper_bound a in
let rb := get_upper_bound b in
constr:(ra + rb)
| ?a * ?b =>
let ra := get_upper_bound a in
let rb := get_upper_bound b in
constr:(ra * rb)
| ?a / (?b * (?c * ?d)) => let rval := constr:(a / (b * c * d)) in
let r := get_upper_bound rval in constr:(r)
| ?a / (?b * ?c) => let rval := constr:(a / b / c) in
let r := get_upper_bound rval in constr:(r)
| ?a / (2 ^ ?b) =>
let ra := get_strict_upper_bound a in
let rr := get_div_by_pow_2 ra b in constr:(rr - 1)
| ?a / ?b =>
let ra := get_strict_upper_bound a in
let rr := get_div_by ra b in constr:(rr - 1)
| ?t => match goal with
| H : t < ?a |- _ => constr:(a - 1)
| H : t <= ?a |- _ => constr:(a)
| _ => t
end
end.
Lemma mul_ge_l_of_nonzero p q : q <> 0 ->
p <= p * q.
Proof.
nia.
Qed.
Lemma mul_ge_r_of_nonzero p q : p <> 0 ->
q <= p * q.
Proof.
nia.
Qed.
Ltac show_pow2_le :=
rewrite ?Nat.pow_add_r,
?Nat.mul_add_distr_r, ?Nat.mul_add_distr_l,
?Nat.mul_sub_distr_r, ?Nat.mul_sub_distr_l,
?Nat.mul_1_r, ?Nat.mul_1_l;
repeat match goal with
|- context [2 ^ ?a] =>
tryif assert (2 ^ a <> 0) by assumption
then fail
else pose proof (pow2_nonzero a)
end;
nia || (
repeat match goal with
| |- context [?p * ?q] =>
tryif assert (p <> 0) by assumption
then
(tryif assert (q <> 0) by assumption
then fail
else assert (q <> 0) by nia)
else assert (p <> 0) by nia;
(tryif assert (q <> 0) by assumption
then idtac else assert (q <> 0) by nia)
end;
repeat match goal with
| |- context [?p * ?q] =>
tryif assert (p <= p * q) by assumption
then
(tryif assert (q <= p * q) by assumption
then fail
else pose proof (mul_ge_r_of_nonzero p q ltac:(assumption)))
else pose proof (mul_ge_l_of_nonzero p q ltac:(assumption));
(tryif assert (q <= p * q) by assumption
then idtac
else pose proof (mul_ge_r_of_nonzero p q ltac:(assumption)))
end;
nia).
Lemma lt_of_le_sub_1 a b :
b <> 0 -> a <= b - 1 -> a < b.
Proof. lia. Qed.
Lemma le_sub_1_of_lt a b :
a < b -> a <= b - 1.
Proof. lia. Qed.
(* FIXME: TODO: Remove in favor of Nat.Div0.div_le_mono when we upgrade past Coq ~8.16*)
Lemma div0_div_le_mono : forall a b c : nat, a <= b -> a / c <= b / c.
Proof.
intros a b []; [easy|].
apply Nat.div_le_mono; easy.
Qed.
Lemma div0_div_lt_upper_bound : forall a b c : nat, a < b * c ->
a / b < c.
Proof.
intros a b c H; apply Nat.div_lt_upper_bound; lia.
Qed.
Lemma div0_div_div : forall a b c, a / b / c = a / (b * c).
Proof.
intros a [] []; [rewrite ?Nat.mul_0_r; easy..|].
now apply Nat.div_div.
Qed.
Lemma nat_mod_0_r : forall a, a mod 0 = a.
Proof. easy. Qed.
Lemma div0_mod_0_l : forall a, 0 mod a = 0.
Proof.
intros []; [easy|];
now apply Nat.mod_0_l.
Qed.
Lemma div0_mod_add : forall a b c, (a + b * c) mod c = a mod c.
Proof.
intros a b []; [f_equal; lia|];
now apply Nat.mod_add.
Qed.
Lemma div0_mod_mul_r : forall a b c,
a mod (b * c) = a mod b + b * ((a / b) mod c).
Proof.
intros a [] []; rewrite ?Nat.mul_0_r, ?Nat.mul_0_l,
?nat_mod_0_r; [lia..| pose proof (Nat.div_mod_eq a (S n)); lia |].
now apply Nat.mod_mul_r.
Qed.
Lemma div0_mod_mod : forall a n, (a mod n) mod n = a mod n.
Proof.
intros a []; [easy|]; now apply Nat.mod_mod.
Qed.
Lemma div0_mod_mul : forall a b, (a * b) mod b = 0.
Proof.
intros a []; [cbn;lia|];
now apply Nat.mod_mul.
Qed.
Lemma div0_add_mod_idemp_l : forall a b n : nat,
(a mod n + b) mod n = (a + b) mod n.
Proof.
intros a b []; [easy|]; now apply Nat.add_mod_idemp_l.
Qed.
Lemma div0_add_mod : forall a b n,
(a + b) mod n = (a mod n + b mod n) mod n.
Proof.
intros a b []; [easy|];
now apply Nat.add_mod.
Qed.
Lemma div0_mod_same : forall n,
n mod n = 0.
Proof.
intros []; [easy|]; now apply Nat.mod_same.
Qed.
Lemma div0_div_0_l : forall n, 0 / n = 0.
Proof. intros []; easy. Qed.
Notation "Nat.Div0.div_le_mono" := div0_div_le_mono.
Notation "Nat.Div0.div_lt_upper_bound" := div0_div_lt_upper_bound.
Notation "Nat.Div0.div_div" := div0_div_div.
Notation "Nat.mod_0_r" := nat_mod_0_r.
Notation "Nat.Div0.div_0_l" := div0_div_0_l.
Notation "Nat.Div0.mod_0_l" := div0_mod_0_l.
Notation "Nat.Div0.mod_add" := div0_mod_add.
Notation "Nat.Div0.mod_same" := div0_mod_same.
Notation "Nat.Div0.mod_mul_r" := div0_mod_mul_r.
Notation "Nat.Div0.mod_mod" := div0_mod_mod.
Notation "Nat.Div0.mod_mul" := div0_mod_mul.
Notation "Nat.Div0.add_mod" := div0_add_mod.
Notation "Nat.Div0.add_mod_idemp_l" := div0_add_mod_idemp_l.
Ltac show_le_upper_bound term :=
lazymatch term with
| ?k mod 0 =>
rewrite (Nat.mod_0_r k);
show_le_upper_bound k
| ?k mod (2 ^ ?a) =>
exact (le_sub_1_of_lt (k mod (2^a)) (2^a)
(Nat.mod_upper_bound k (2^a) (pow2_nonzero a)))
| ?k mod (?a ^ ?b) =>
exact (le_sub_1_of_lt (k mod (2^a)) (a^b)
(Nat.mod_upper_bound k (a^b)
(Nat.pow_nonzero a b ltac:(show_term_nonzero a))))
| ?k mod ?a =>
let H := fresh in
let _ := match goal with |- _ =>
assert (H: a <> 0) by show_nonzero end in
exact (le_sub_1_of_lt _ _ (Nat.mod_upper_bound k a H))
| ?k mod ?a =>
let H := fresh in
let _ := match goal with |- _ =>
assert (H: a = 0) by lia end in
rewrite H;
show_le_upper_bound k
| 2 ^ ?a * ?t => let r := get_upper_bound t in
apply (Nat.mul_le_mono_l t _ (2^a));
show_le_upper_bound t
| ?t * 2 ^ ?a => let r := get_upper_bound t in
apply (Nat.mul_le_mono_r t _ (2^a));
show_le_upper_bound t
| ?a ^ ?b =>
apply Nat.le_refl
| ?a + ?b =>
apply Nat.add_le_mono;
[
(* match goal with |- ?G => idtac G "should be about" a end; *)
show_le_upper_bound a |
show_le_upper_bound b]
| ?a * ?b =>
apply Nat.mul_le_mono;
[
(* match goal with |- ?G => idtac G "should be about" a end; *)
show_le_upper_bound a |
show_le_upper_bound b]
| ?a / (?b * (?c * ?d)) =>
let H := fresh in
pose proof (f_equal (fun x => a / x) (Nat.mul_assoc b c d) :
a / (b * (c * d)) = a / (b * c * d)) as H;
rewrite H;
clear H;
let rval := constr:(a / (b * c * d)) in
show_le_upper_bound rval
| ?a / (?b * ?c) =>
let H := fresh in
pose proof (eq_sym (Nat.Div0.div_div a b c) :
a / (b * c) = a / b / c) as H;
rewrite H;
clear H;
let rval := constr:(a / b / c) in
show_le_upper_bound rval
| ?a / (2 ^ ?b) =>
let ra := get_upper_bound a in
apply (Nat.le_trans (a / (2^b)) (ra / (2^b)) _);
[apply Nat.Div0.div_le_mono;
show_le_upper_bound a |
tryif show_div_by_pow2_ge ra b then idtac
else
match goal with
| |- (?val - 1) / 2 ^ ?pwr <= ?rhs - 1 =>
apply le_sub_1_of_lt, Nat.Div0.div_lt_upper_bound;
tryif nia || show_pow2_le then idtac
else fail 20 "nia failed" "on (" val "- 1) / 2 ^" pwr "<=" rhs "- 1"
| |- ?G =>
tryif nia then idtac else
fail 40 "show div failed for" a "/ (2^" b "), ra =" ra
"; full goal:" G
end]
| ?a / ?b =>
let ra := get_upper_bound a in
apply (Nat.le_trans (a / b) (ra / b) _);
[apply Nat.Div0.div_le_mono;
show_le_upper_bound a |
tryif show_div_by_ge ra b then idtac
else
match goal with
| |- (?val - 1) / ?den <= ?rhs - 1 =>
apply le_sub_1_of_lt, Nat.Div0.div_lt_upper_bound;
tryif nia || show_pow2_le then idtac
else fail 20 "nia failed" "on (" val "- 1) / " den "<=" rhs "- 1"
| |- ?G =>
tryif nia then idtac else
fail 40 "show div failed for" a "/ (" b "), ra =" ra
"; full goal:" G
end]
| ?t => match goal with
| _ => nia
end
end.
Create HintDb show_moddy_lt_db.
Ltac show_moddy_lt :=
try trivial with show_moddy_lt_db;
lazymatch goal with
| |- Nat.b2n ?b < ?a =>
apply (Nat.le_lt_trans (Nat.b2n b) (2^1) a);
[destruct b; simpl; lia | show_pow2_le]
| |- ?a < ?b =>
let r := get_upper_bound a in
apply (Nat.le_lt_trans a r b);
[show_le_upper_bound a | show_pow2_le]
| |- ?a <= ?b => (* Likely not to work *)
let r := get_upper_bound a in
apply (Nat.le_trans a r b);
[show_le_upper_bound a | show_pow2_le]
| |- ?a > ?b =>
change (b < a); show_moddy_lt
| |- ?a >= ?b =>
change (b <= a); show_moddy_lt
| |- (?a <? ?b) = true =>
apply (proj2 (Nat.ltb_lt a b));
show_moddy_lt
| |- true = (?a <? ?b) =>
symmetry;
apply (proj2 (Nat.ltb_lt a b));
show_moddy_lt
| |- (?a <=? ?b) = false =>
apply (proj2 (Nat.leb_gt a b));
show_moddy_lt
| |- false = (?a <=? ?b) =>
symmetry;
apply (proj2 (Nat.leb_gt a b));
show_moddy_lt
end.
Ltac try_show_moddy_lt :=
try trivial with show_moddy_lt_db;
lazymatch goal with
| |- Nat.b2n ?b < ?a =>
apply (Nat.le_lt_trans (Nat.b2n b) (2^1) a);
[destruct b; simpl; lia | try show_pow2_le]
| |- ?a < ?b =>
let r := get_upper_bound a in
apply (Nat.le_lt_trans a r b);
[try show_le_upper_bound a | try show_pow2_le]
| |- ?a <= ?b => (* Likely not to work *)
let r := get_upper_bound a in
apply (Nat.le_trans a r b);
[try show_le_upper_bound a | try show_pow2_le]
| |- ?a > ?b =>
change (b < a); try_show_moddy_lt
| |- ?a >= ?b =>
change (b <= a); try_show_moddy_lt
| |- (?a <? ?b) = true =>
apply (proj2 (Nat.ltb_lt a b));
try_show_moddy_lt
| |- true = (?a <? ?b) =>
symmetry;
apply (proj2 (Nat.ltb_lt a b));
try_show_moddy_lt
| |- (?a <=? ?b) = false =>
apply (proj2 (Nat.leb_gt a b));
try_show_moddy_lt
| |- false = (?a <=? ?b) =>
symmetry;
apply (proj2 (Nat.leb_gt a b));
try_show_moddy_lt
end.
Ltac replace_bool_moddy_lia b0 b1 :=
first
[ replace b0 with b1
by (show_moddy_lt || bdestruct b0; show_moddy_lt + lia
|| (destruct b1 eqn:?; lia))
| replace b0 with b1
by (bdestruct b1; lia || (destruct b0 eqn:?; lia))
| replace b0 with b1
by (bdestruct b0; bdestruct b1; lia) ].
Ltac simpl_bools_nosimpl :=
repeat (rewrite ?andb_true_r, ?andb_false_r, ?orb_true_r, ?orb_false_r,
?andb_true_l, ?andb_false_l, ?orb_true_l, ?orb_false_l).
Ltac simplify_bools_moddy_lia_one_kernel :=
let fail_if_iffy H :=
match H with
| context [ if _ then _ else _ ] => fail 1
| _ => idtac
end
in
let fail_if_compound H :=
fail_if_iffy H;
match H with
| context [ ?a && ?b ] => fail 1
| context [ ?a || ?b ] => fail 1
| _ => idtac
end
in
let act_T b := (fail_if_compound b;
(replace_bool_moddy_lia b true
|| replace_bool_moddy_lia b false); simpl) in
let act_F b := (fail_if_compound b;
(replace_bool_moddy_lia b false
|| replace_bool_moddy_lia b true); simpl) in
match goal with
| |- context[?b && _] => act_F b; rewrite ?andb_true_l, ?andb_false_l
| |- context[_ && ?b] => act_F b; rewrite ?andb_true_r, ?andb_false_r
| |- context[?b || _] => act_T b; rewrite ?orb_true_l, ?orb_false_l
| |- context[_ || ?b] => act_T b; rewrite ?orb_true_r, ?orb_false_r
| |- context[negb ?b] => act_T b; cbn [negb]
| |- context[if ?b then _ else _] => act_T b
end; simpl_bools_nosimpl.
(** * Some general nat facts *)
Section nat_lemmas.
Import Nat.
Local Open Scope nat.
Lemma add_sub' n m : m + n - m = n.
Proof.
lia.
Qed.
Lemma add_leb_mono_l n m d :
(n + m <=? n + d) = (m <=? d).
Proof.
bdestructΩ'.
Qed.
Lemma add_ltb_mono_l n m d :
(n + m <? n + d) = (m <? d).
Proof.
bdestructΩ'.
Qed.
Lemma geb_0 n : 0 <=? n = true.
Proof.
bdestructΩ'.
Qed.
Lemma add_le_cancel_l_iff n m d :
n + m <= n + d <-> m <= d.
Proof. lia. Qed.
Lemma add_lt_cancel_l_iff n m d :
n + m < n + d <-> m < d.
Proof. lia. Qed.
Lemma add_ge_cancel_l_iff n m d :
n + m >= n + d <-> m >= d.
Proof. lia. Qed.
Lemma add_gt_cancel_l_iff n m d :
n + m > n + d <-> m > d.
Proof. lia. Qed.
Lemma sub_lt_iff n m p (Hp : 0 <> p) :
n - m < p <-> n < m + p.
Proof.
split; lia.
Qed.
Lemma sub_eq_iff {a b m} : b <= a ->
a - b = m <-> a = b + m.
Proof.
lia.
Qed.
Lemma n_le_pow_2_n (n : nat) : n <= 2 ^ n.
Proof.
induction n; simpl; [lia|].
pose proof (pow_positive 2 n).
lia.
Qed.
Lemma div_mul_not_exact a b : b <> 0 ->
(a / b) * b = a - (a mod b).
Proof.
intros Hb.
rewrite (Nat.div_mod a b Hb) at 1 2.
rewrite Nat.add_sub.
rewrite (Nat.mul_comm b (a/b)), Nat.add_comm, Nat.div_add by easy.
rewrite Nat.div_small by (apply Nat.mod_upper_bound; easy).
easy.
Qed.
Lemma diff_divs_lower_bound a b k n :
(a < n -> b < n -> a / k <> b / k -> k < n)%nat.
Proof.
intros Ha Hb Hne.
bdestructΩ (k <? n).
exfalso; apply Hne.
now rewrite 2!Nat.div_small by lia.
Qed.
Lemma mod_div a b : a mod b / b = 0.
Proof.
destruct b; [easy|].
apply Nat.div_small, Nat.mod_upper_bound; easy.
Qed.
Lemma div_mod : forall (x y z : nat), (x / y) mod z = (x mod (y * z)) / y.
Proof.
intros.
bdestruct (y =? 0); [subst; simpl; now rewrite Nat.Div0.mod_0_l|].
rewrite Nat.Div0.mod_mul_r, Nat.mul_comm, Nat.div_add by easy.
now rewrite mod_div.
Qed.
Lemma sub_mul_mod : forall x y z,
y * z <= x ->
(x - y * z) mod z = x mod z.
Proof.
intros.
replace (x mod z) with ((x - y * z + y * z) mod z) by (f_equal; lia).
now rewrite Nat.Div0.mod_add.
Qed.
Lemma mod_product : forall x y z, x mod (y * z) mod z = x mod z.
Proof.
intros.
rewrite Nat.mul_comm, Nat.Div0.mod_mul_r, Nat.mul_comm.
now rewrite Nat.Div0.mod_add, Nat.Div0.mod_mod.
Qed.
Lemma mod_add_l a b c : (a * b + c) mod b = c mod b.
Proof.
rewrite Nat.add_comm.
apply Nat.Div0.mod_add.
Qed.
Lemma div_eq a b : a / b = (a - a mod b) / b.
Proof.
rewrite (Nat.div_mod_eq a b) at 2.
rewrite Nat.add_sub.
bdestruct (b =? 0).
- now subst.
- now rewrite Nat.mul_comm, Nat.div_mul by easy.
Qed.
Lemma sub_mod_le n m : m <= n ->
(n - m mod n) mod n = (n - m) mod n.
Proof.
intros Hm.
bdestruct (m =? n).
- subst.
now rewrite Nat.Div0.mod_same, Nat.sub_0_r, Nat.sub_diag,
Nat.Div0.mod_same, Nat.Div0.mod_0_l.
- now rewrite (Nat.mod_small m) by lia.
Qed.
Lemma mod_mul_sub_le a b c : c <> 0 -> a <= b * c ->
(b * c - a) mod c =
c * Nat.b2n (¬ a mod c =? 0) - a mod c.
Proof.
intros Hc Ha.
bdestruct (a =? b * c).
- subst.
rewrite Nat.sub_diag, Nat.Div0.mod_mul, Nat.Div0.mod_0_l.
cbn; lia.
- rewrite (Nat.div_mod_eq a c) at 1.
assert (a < b * c) by lia.
assert (a / c < b) by (apply Nat.Div0.div_lt_upper_bound; lia).
assert (a mod c < c) by show_moddy_lt.
replace (b * c - (c * (a / c) + a mod c)) with
((b - a / c - 1) * c + (c - a mod c)) by nia.
rewrite mod_add_l.
bdestruct (a mod c =? 0).
+ replace -> (a mod c).
rewrite Nat.sub_0_r, Nat.Div0.mod_same.
cbn; lia.
+ rewrite Nat.mod_small by lia.
cbn; lia.
Qed.
Lemma div_sub a b c : c <> 0 ->
(b * c - a) / c = b - a / c - Nat.b2n (¬ a mod c =? 0).
Proof.
intros Hc.
bdestruct (a <? b * c); cycle 1.
- replace (b * c - a) with 0 by lia.
rewrite Nat.Div0.div_0_l.
pose proof (Nat.div_le_lower_bound a c b); lia.
- assert (a / c < b) by show_moddy_lt.
apply (Nat.mul_cancel_r _ _ c Hc).
rewrite div_mul_not_exact by easy.
rewrite 2!Nat.mul_sub_distr_r.
rewrite div_mul_not_exact by easy.
pose proof (Nat.mod_le (b * c - a) c Hc).
pose proof (Nat.mod_le a c Hc).
enough (a + (b * c - a) mod c =
(a + c * Nat.b2n (¬ a mod c =? 0) - a mod c))
by lia.
rewrite <- Nat.add_sub_assoc by
(pose proof (Nat.mod_upper_bound a c Hc);
bdestructΩ'; cbn; lia).
f_equal.
apply mod_mul_sub_le; lia.
Qed.
Lemma min_ltb n m : min n m = if n <? m then n else m.
Proof.
bdestructΩ'.
Qed.
Lemma eqb_iff_div_mod_eqb a i j :
i =? j =
(i mod a =? j mod a) && (i / a =? j / a).
Proof.
apply eq_iff_eq_true.
rewrite andb_true_iff, !Nat.eqb_eq.
split; [intros ->; easy|].
intros.
rewrite (Nat.div_mod_eq i a), (Nat.div_mod_eq j a).
lia.
Qed.
Lemma eqb_comb_iff_div_mod_eqb a i x y (Hy : y < a) :
i =? x * a + y =
(i mod a =? y) && (i / a =? x).
Proof.
rewrite (eqb_iff_div_mod_eqb a).
rewrite mod_add_l, Nat.div_add_l,