-
Notifications
You must be signed in to change notification settings - Fork 3
/
MathSpec.v
executable file
·1720 lines (1562 loc) · 49.7 KB
/
MathSpec.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
Require Import Coq.btauto.Btauto Coq.NArith.Nnat Bool.
Require Import Dirac.
Require Import BasicUtility.
Local Open Scope nat_scope.
(* Here we defined the specification of carry value for each bit. *)
(* fb_push is to take a qubit and then push it to the zero position
in the bool function representation of a number. *)
Lemma mod_sum_lt :
forall x y M,
x < M ->
y < M ->
(x + y) mod M < x <-> x + y >= M.
Proof.
intros. split; intros.
- assert ((x + y) mod M < x + y) by lia.
rewrite Nat.div_mod with (y := M) in H2 by lia.
assert (0 < (x + y) / M) by nia.
rewrite Nat.div_str_pos_iff in H3 by lia. lia.
- rewrite Nat.mod_eq by lia.
assert (1 <= (x + y) / M < 2).
{ split.
apply Nat.div_le_lower_bound; lia.
apply Nat.div_lt_upper_bound; lia.
}
replace (M * ((x + y) / M)) with M by nia.
lia.
Qed.
Lemma mod_sum_lt_bool :
forall x y M,
x < M ->
y < M ->
¬ (M <=? x + y) = (x <=? (x + y) mod M).
Proof.
intros. bdestruct (M <=? x + y); bdestruct (x <=? (x + y) mod M); try easy.
assert ((x + y) mod M < x) by (apply mod_sum_lt; lia). lia.
assert (x + y >= M) by (apply mod_sum_lt; lia). lia.
Qed.
Definition allfalse := fun (_:nat) => false.
Definition majb a b c := (a && b) ⊕ (b && c) ⊕ (a && c).
Definition fb_push b f : nat -> bool :=
fun x => match x with
| O => b
| S n => f n
end.
Lemma fb_push_right:
forall n b f, 0 < n -> fb_push b f n = f (n-1).
Proof.
intros. induction n. lia.
simpl. assert ((n - 0) = n) by lia.
rewrite H0. reflexivity.
Qed.
Lemma fb_push_same : forall b f g, (forall i, fb_push b f i = fb_push b g i) -> f = g.
Proof.
intros.
apply functional_extensionality; intros.
specialize (H (S x)).
rewrite fb_push_right in H; try lia.
rewrite fb_push_right in H; try lia.
simpl in H.
assert (x-0 = x) by lia. rewrite H0 in H. easy.
Qed.
Fixpoint fb_push_n n f : nat -> bool :=
match n with 0 => f
| S m => fb_push false (fb_push_n m f)
end.
(* A function to compile positive to a bool function. *)
Fixpoint pos2fb p : nat -> bool :=
match p with
| xH => fb_push true allfalse
| xI p' => fb_push true (pos2fb p')
| xO p' => fb_push false (pos2fb p')
end.
(* A function to compile N to a bool function. *)
Definition N2fb n : nat -> bool :=
match n with
| 0%N => allfalse
| Npos p => pos2fb p
end.
Definition nat2fb n := N2fb (N.of_nat n).
Definition add_c b x y :=
match b with
| false => Pos.add x y
| true => Pos.add_carry x y
end.
Fixpoint carry b n f g :=
match n with
| 0 => b
| S n' => let c := carry b n' f g in
let a := f n' in
let b := g n' in
(a && b) ⊕ (b && c) ⊕ (a && c)
end.
Lemma carry_1 : forall b f g, carry b 1 f g = majb (f 0) (g 0) b.
Proof.
intros. simpl. unfold majb. easy.
Qed.
Lemma carry_n : forall n b f g, carry b (S n) f g = majb (f n) (g n) (carry b n f g).
Proof.
intros. simpl. unfold majb. easy.
Qed.
Lemma carry_sym :
forall b n f g,
carry b n f g = carry b n g f.
Proof.
intros. induction n. reflexivity.
simpl. rewrite IHn. btauto.
Qed.
Lemma carry_false_0_l: forall n f,
carry false n allfalse f = false.
Proof.
unfold allfalse.
induction n.
simpl.
reflexivity.
intros. simpl.
rewrite IHn. rewrite andb_false_r.
reflexivity.
Qed.
Lemma carry_false_0_r: forall n f,
carry false n f allfalse = false.
Proof.
unfold allfalse.
induction n.
simpl.
reflexivity.
intros. simpl.
rewrite IHn. rewrite andb_false_r.
reflexivity.
Qed.
Lemma carry_fbpush :
forall n a ax ay fx fy,
carry a (S n) (fb_push ax fx) (fb_push ay fy) = carry (majb a ax ay) n fx fy.
Proof.
induction n; intros.
simpl. unfold majb. btauto.
remember (S n) as Sn. simpl. rewrite IHn. unfold fb_push. subst.
simpl. easy.
Qed.
Lemma carry_succ :
forall m p,
carry true m (pos2fb p) allfalse = pos2fb (Pos.succ p) m ⊕ (pos2fb p) m.
Proof.
induction m; intros. simpl. destruct p; reflexivity.
replace allfalse with (fb_push false allfalse).
2:{ unfold fb_push, allfalse. apply functional_extensionality. intros. destruct x; reflexivity.
}
Local Opaque fb_push carry.
destruct p; simpl.
rewrite carry_fbpush; unfold majb; simpl. rewrite IHm. reflexivity.
rewrite carry_fbpush; unfold majb; simpl. rewrite carry_false_0_r. Local Transparent fb_push. simpl. btauto.
rewrite carry_fbpush; unfold majb; simpl. Local Transparent carry. destruct m; reflexivity.
Qed.
Lemma carry_succ' :
forall m p,
carry true m allfalse (pos2fb p) = pos2fb (Pos.succ p) m ⊕ (pos2fb p) m.
Proof.
intros. rewrite carry_sym. apply carry_succ.
Qed.
Lemma carry_succ0 :
forall m, carry true m allfalse allfalse = pos2fb xH m.
Proof.
induction m. easy.
replace allfalse with (fb_push false allfalse).
2:{ unfold fb_push, allfalse. apply functional_extensionality. intros. destruct x; reflexivity.
}
rewrite carry_fbpush. unfold majb. simpl. rewrite carry_false_0_l. easy.
Qed.
Lemma carry_add_pos_eq :
forall m b p q,
carry b m (pos2fb p) (pos2fb q) ⊕ (pos2fb p) m ⊕ (pos2fb q) m = pos2fb (add_c b p q) m.
Proof.
induction m; intros. simpl. destruct p, q, b; reflexivity.
Local Opaque carry.
destruct p, q, b; simpl; rewrite carry_fbpush;
try (rewrite IHm; reflexivity);
try (unfold majb; simpl;
try rewrite carry_succ; try rewrite carry_succ';
try rewrite carry_succ0; try rewrite carry_false_0_l;
try rewrite carry_false_0_r;
unfold allfalse; try btauto; try (destruct m; reflexivity)).
Qed.
Lemma carry_add_eq_carry0 :
forall m x y,
carry false m (N2fb x) (N2fb y) ⊕ (N2fb x) m ⊕ (N2fb y) m = (N2fb (x + y)) m.
Proof.
intros.
destruct x as [|p]; destruct y as [|q]; simpl; unfold allfalse.
rewrite carry_false_0_l. easy.
rewrite carry_false_0_l. btauto.
rewrite carry_false_0_r. btauto.
apply carry_add_pos_eq.
Qed.
Lemma carry_add_eq_carry1 :
forall m x y,
carry true m (N2fb x) (N2fb y) ⊕ (N2fb x) m ⊕ (N2fb y) m = (N2fb (x + y + 1)) m.
Proof.
intros.
destruct x as [|p]; destruct y as [|q]; simpl; unfold allfalse.
rewrite carry_succ0. destruct m; easy.
rewrite carry_succ'. replace (q + 1)%positive with (Pos.succ q) by lia. btauto.
rewrite carry_succ. replace (p + 1)%positive with (Pos.succ p) by lia. btauto.
rewrite carry_add_pos_eq. unfold add_c. rewrite Pos.add_carry_spec.
replace (p + q + 1)%positive with (Pos.succ (p + q)) by lia. easy.
Qed.
Definition fbxor f g := fun (i : nat) => f i ⊕ g i.
Definition msma i b f g := fun (x : nat) => if (x <? i) then
(carry b (S x) f g ⊕ (f (S x))) else (if (x =? i) then carry b (S x) f g else f x).
Definition msmb i (b : bool) f g := fun (x : nat) => if (x <=? i) then (f x ⊕ g x) else g x.
Definition msmc i b f g := fun (x : nat) => if (x <=? i) then (f x ⊕ g x) else (carry b x f g ⊕ f x ⊕ g x).
Definition sumfb b f g := fun (x : nat) => carry b x f g ⊕ f x ⊕ g x.
Definition negatem i (f : nat -> bool) := fun (x : nat) => if (x <? i) then ¬ (f x) else f x.
Lemma sumfb_correct_carry0 :
forall x y,
sumfb false (nat2fb x) (nat2fb y) = nat2fb (x + y).
Proof.
intros. unfold nat2fb. rewrite Nnat.Nat2N.inj_add.
apply functional_extensionality. intros. unfold sumfb. rewrite carry_add_eq_carry0. easy.
Qed.
Lemma sumfb_correct_carry1 :
forall x y,
sumfb true (nat2fb x) (nat2fb y) = nat2fb (x + y + 1).
Proof.
intros. unfold nat2fb. do 2 rewrite Nnat.Nat2N.inj_add.
apply functional_extensionality. intros. unfold sumfb. rewrite carry_add_eq_carry1. easy.
Qed.
Lemma sumfb_correct_N_carry0 :
forall x y,
sumfb false (N2fb x) (N2fb y) = N2fb (x + y).
Proof.
intros. apply functional_extensionality. intros. unfold sumfb. rewrite carry_add_eq_carry0. easy.
Qed.
Lemma pos2fb_Postestbit :
forall n i,
(pos2fb n) i = Pos.testbit n (N.of_nat i).
Proof.
induction n; intros.
- destruct i; simpl. easy. rewrite IHn. destruct i; simpl. easy. rewrite Pos.pred_N_succ. easy.
- destruct i; simpl. easy. rewrite IHn. destruct i; simpl. easy. rewrite Pos.pred_N_succ. easy.
- destruct i; simpl. easy. easy.
Qed.
Lemma N2fb_Ntestbit :
forall n i,
(N2fb n) i = N.testbit n (N.of_nat i).
Proof.
intros. destruct n. easy.
simpl. apply pos2fb_Postestbit.
Qed.
Lemma Z2N_Nat2Z_Nat2N :
forall x,
Z.to_N (Z.of_nat x) = N.of_nat x.
Proof.
destruct x; easy.
Qed.
Lemma Nofnat_mod :
forall x y,
y <> 0 ->
N.of_nat (x mod y) = ((N.of_nat x) mod (N.of_nat y))%N.
Proof.
intros. specialize (Zdiv.mod_Zmod x y H) as G.
repeat rewrite <- Z2N_Nat2Z_Nat2N. rewrite G. rewrite Z2N.inj_mod; lia.
Qed.
Lemma Nofnat_pow :
forall x y,
N.of_nat (x ^ y) = ((N.of_nat x) ^ (N.of_nat y))%N.
Proof.
intros. induction y. easy.
Local Opaque N.pow. replace (N.of_nat (S y)) with ((N.of_nat y) + 1)%N by lia.
simpl. rewrite N.pow_add_r. rewrite N.pow_1_r. rewrite Nnat.Nat2N.inj_mul. rewrite IHy. lia.
Qed.
Lemma Ntestbit_lt_pow2n :
forall x n,
(x < 2^n)%N ->
N.testbit x n = false.
Proof.
intros. apply N.mod_small in H. rewrite <- H. apply N.mod_pow2_bits_high. lia.
Qed.
Lemma Ntestbit_in_pow2n_pow2Sn :
forall x n,
(2^n <= x < 2^(N.succ n))%N ->
N.testbit x n = true.
Proof.
intros. assert (N.log2 x = n) by (apply N.log2_unique; lia).
rewrite <- H0. apply N.bit_log2.
assert (2^n <> 0)%N by (apply N.pow_nonzero; easy).
lia.
Qed.
Lemma negatem_Nlnot :
forall (n : nat) (x : N) i,
negatem n (N2fb x) i = N.testbit (N.lnot x (N.of_nat n)) (N.of_nat i).
Proof.
intros. unfold negatem. rewrite N2fb_Ntestbit. symmetry.
bdestruct (i <? n). apply N.lnot_spec_low. lia.
apply N.lnot_spec_high. lia.
Qed.
Lemma negatem_arith :
forall n x,
x < 2^n ->
negatem n (nat2fb x) = nat2fb (2^n - 1 - x).
Proof.
intros. unfold nat2fb. apply functional_extensionality; intro i.
rewrite negatem_Nlnot. rewrite N2fb_Ntestbit.
do 2 rewrite Nnat.Nat2N.inj_sub. rewrite Nofnat_pow. simpl.
bdestruct (x =? 0). subst. simpl. rewrite N.ones_equiv. rewrite N.pred_sub. rewrite N.sub_0_r. easy.
rewrite N.lnot_sub_low. rewrite N.ones_equiv. rewrite N.pred_sub. easy.
apply N.log2_lt_pow2. assert (0 < x) by lia. lia.
replace 2%N with (N.of_nat 2) by lia. rewrite <- Nofnat_pow. lia.
Qed.
(* Here, we define the addto / addto_n functions for angle rotation. *)
Definition cut_n (f:nat -> bool) (n:nat) := fun i => if i <? n then f i else allfalse i.
Definition fbrev' i n (f : nat -> bool) := fun (x : nat) =>
if (x <=? i) then f (n - 1 - x) else if (x <? n - 1 - i)
then f x else if (x <? n) then f (n - 1 - x) else f x.
Definition fbrev {A} n (f : nat -> A) := fun (x : nat) => if (x <? n) then f (n - 1 - x) else f x.
Lemma fbrev'_fbrev :
forall n f,
0 < n ->
fbrev n f = fbrev' ((n - 1) / 2) n f.
Proof.
intros. unfold fbrev, fbrev'. apply functional_extensionality; intro.
assert ((n - 1) / 2 < n) by (apply Nat.div_lt_upper_bound; lia).
assert (2 * ((n - 1) / 2) <= n - 1) by (apply Nat.mul_div_le; easy).
assert (n - 1 - (n - 1) / 2 <= (n - 1) / 2 + 1).
{ assert (n - 1 <= 2 * ((n - 1) / 2) + 1).
{ assert (2 <> 0) by easy.
specialize (Nat.mul_succ_div_gt (n - 1) 2 H2) as G.
lia.
}
lia.
}
IfExpSimpl; easy.
Qed.
Lemma allfalse_0 : forall n, cut_n allfalse n = nat2fb 0.
Proof.
intros. unfold nat2fb. simpl.
unfold cut_n.
apply functional_extensionality; intro.
bdestruct (x <? n).
easy. easy.
Qed.
Lemma f_num_aux_0: forall n f x, cut_n f n = nat2fb x
-> f n = false -> cut_n f (S n) = nat2fb x.
Proof.
intros.
rewrite <- H.
unfold cut_n.
apply functional_extensionality.
intros.
bdestruct (x0 <? n).
bdestruct (x0 <? S n).
easy.
lia.
bdestruct (x0 <? S n).
assert (x0 = n) by lia.
subst. rewrite H0. easy.
easy.
Qed.
Definition twoton_fun (n:nat) := fun i => if i <? n then false else if i=? n then true else false.
Definition times_two_spec (f:nat -> bool) := fun i => if i =? 0 then false else f (i-1).
(* Showing the times_two spec is correct. *)
Lemma nat2fb_even_0:
forall x, nat2fb (2 * x) 0 = false.
Proof.
intros.
unfold nat2fb.
rewrite Nat2N.inj_double.
unfold N.double.
destruct (N.of_nat x).
unfold N2fb,allfalse.
reflexivity.
unfold N2fb.
simpl.
reflexivity.
Qed.
Lemma pos2fb_times_two_eq:
forall p x, x <> 0 -> pos2fb p (x - 1) = pos2fb p~0 x.
Proof.
intros.
induction p.
simpl.
assert ((fb_push false (fb_push true (pos2fb p))) x = (fb_push true (pos2fb p)) (x - 1)).
rewrite fb_push_right.
reflexivity. lia.
rewrite H0.
reflexivity.
simpl.
rewrite (fb_push_right x).
reflexivity. lia.
simpl.
rewrite (fb_push_right x).
reflexivity. lia.
Qed.
Lemma times_two_correct:
forall x, (times_two_spec (nat2fb x)) = (nat2fb (2*x)).
Proof.
intros.
unfold times_two_spec.
apply functional_extensionality; intros.
unfold nat2fb.
bdestruct (x0 =? 0).
rewrite H.
specialize (nat2fb_even_0 x) as H3.
unfold nat2fb in H3.
rewrite H3.
reflexivity.
rewrite Nat2N.inj_double.
unfold N.double,N2fb.
destruct (N.of_nat x).
unfold allfalse.
reflexivity.
rewrite pos2fb_times_two_eq.
reflexivity. lia.
Qed.
Lemma f_twoton_eq : forall n, twoton_fun n = nat2fb (2^n).
Proof.
intros.
induction n.
simpl.
unfold twoton_fun.
apply functional_extensionality.
intros.
IfExpSimpl.
unfold fb_push. destruct x. easy. lia.
unfold fb_push. destruct x. lia. easy.
assert ((2 ^ S n) = 2 * (2^n)). simpl. lia.
rewrite H.
rewrite <- times_two_correct.
rewrite <- IHn.
unfold twoton_fun,times_two_spec.
apply functional_extensionality; intros.
bdestruct (x =? 0).
subst.
bdestruct (0 <? S n).
easy. lia.
bdestruct (x <? S n).
bdestruct (x - 1 <? n).
easy. lia.
bdestruct (x =? S n).
bdestruct (x - 1 <? n). lia.
bdestruct (x - 1 =? n). easy.
lia.
bdestruct (x-1<? n). easy.
bdestruct (x-1 =? n). lia.
easy.
Qed.
Local Transparent carry.
Lemma carry_cut_n_false : forall i n f, carry false i (cut_n f n) (twoton_fun n) = false.
Proof.
intros.
induction i.
simpl. easy.
simpl. rewrite IHi.
unfold cut_n,twoton_fun.
IfExpSimpl. btauto.
simpl.
btauto.
simpl. easy.
Qed.
Lemma carry_lt_same : forall m n f g h b, m < n -> (forall i, i < n -> f i = g i)
-> carry b m f h = carry b m g h.
Proof.
induction m; intros; simpl. easy.
rewrite H0. rewrite (IHm n) with (g:= g); try lia. easy.
easy. lia.
Qed.
Lemma carry_lt_same_1 : forall m n f g h h' b, m < n -> (forall i, i < n -> f i = g i)
-> (forall i, i < n -> h i = h' i) -> carry b m f h = carry b m g h'.
Proof.
induction m; intros; simpl. easy.
rewrite H1. rewrite H0. rewrite (IHm n) with (g:= g) (h' := h'); try lia. easy.
easy. easy. lia. lia.
Qed.
Local Opaque carry.
Lemma sumfb_cut_n : forall n f, f n = true -> sumfb false (cut_n f n) (twoton_fun n) = cut_n f (S n).
Proof.
intros.
unfold sumfb.
apply functional_extensionality; intros.
rewrite carry_cut_n_false.
unfold cut_n, twoton_fun.
IfExpSimpl. btauto.
subst. rewrite H. simpl. easy.
simpl. easy.
Qed.
Lemma f_num_aux_1: forall n f x, cut_n f n = nat2fb x -> f n = true
-> cut_n f (S n) = nat2fb (x + 2^n).
Proof.
intros.
rewrite <- sumfb_correct_carry0.
rewrite <- H.
rewrite <- f_twoton_eq.
rewrite sumfb_cut_n.
easy. easy.
Qed.
Lemma f_num_0 : forall f n, (exists x, cut_n f n = nat2fb x).
Proof.
intros.
induction n.
exists 0.
rewrite <- (allfalse_0 0).
unfold cut_n.
apply functional_extensionality.
intros.
bdestruct (x <? 0).
inv H. easy.
destruct (bool_dec (f n) true).
destruct IHn.
exists (x + 2^n).
rewrite (f_num_aux_1 n f x).
easy. easy. easy.
destruct IHn.
exists x. rewrite (f_num_aux_0 n f x).
easy. easy.
apply not_true_is_false in n0. easy.
Qed.
Lemma cut_n_1_1: forall (r:rz_val), r 0 = true -> cut_n r 1 = nat2fb 1.
Proof.
intros. unfold cut_n.
apply functional_extensionality. intros.
bdestruct (x <? 1).
assert (x = 0) by lia. subst.
unfold nat2fb. simpl. rewrite H. easy.
unfold nat2fb. simpl.
rewrite fb_push_right. easy. lia.
Qed.
Lemma cut_n_1_0: forall (r:rz_val), r 0 = false -> cut_n r 1 = nat2fb 0.
Proof.
intros. unfold cut_n.
apply functional_extensionality. intros.
bdestruct (x <? 1).
assert (x = 0) by lia. subst.
unfold nat2fb. simpl. rewrite H. easy.
unfold nat2fb. simpl. easy.
Qed.
Lemma nat2fb_0: nat2fb 0 = allfalse.
Proof.
unfold nat2fb. simpl. easy.
Qed.
Lemma pos2fb_no_zero : forall p, (exists i, pos2fb p i = true).
Proof.
intros. induction p.
simpl. exists 0.
simpl. easy.
simpl. destruct IHp.
exists (S x).
simpl. easy. simpl.
exists 0. simpl. easy.
Qed.
Lemma cut_n_eq : forall n f, (forall i, i >= n -> f i = false) -> cut_n f n = f.
Proof.
intros. unfold cut_n.
apply functional_extensionality; intro.
bdestruct (x <? n). easy. rewrite H. easy. lia.
Qed.
Lemma cut_n_twice_same : forall n f, cut_n (cut_n f n) n = cut_n f n.
Proof.
intros. unfold cut_n.
apply functional_extensionality.
intros. bdestruct (x <? n). easy. easy.
Qed.
Lemma cut_n_twice_small : forall n m f, n <= m -> cut_n (cut_n f m) n = cut_n f n.
Proof.
intros. unfold cut_n.
apply functional_extensionality.
intros. bdestruct (x <? n). bdestruct (x <? m). easy.
lia. easy.
Qed.
Lemma nat2fb_bound : forall n x, x < 2^n -> (forall i, i >= n -> nat2fb x i = false).
Proof.
intros.
unfold nat2fb in *.
assert ((N.of_nat x < (N.of_nat 2)^ (N.of_nat n))%N).
rewrite <- Nofnat_pow. lia.
apply N.mod_small in H1.
rewrite N2fb_Ntestbit.
rewrite <- H1.
apply N.mod_pow2_bits_high. lia.
Qed.
Lemma pos2fb_sem : forall x y, pos2fb x = pos2fb y -> x = y.
Proof.
induction x; intros.
simpl in *.
destruct y. simpl in H.
assert (forall i, fb_push true (pos2fb x) i = fb_push true (pos2fb y) i).
intros. rewrite H. easy.
apply fb_push_same in H0. apply IHx in H0. subst. easy.
simpl in H.
assert (forall i, fb_push true (pos2fb x) i = fb_push false (pos2fb y) i).
intros. rewrite H. easy.
specialize (H0 0). simpl in H0. inv H0.
simpl in H.
assert (forall i, fb_push true (pos2fb x) i = fb_push true allfalse i).
intros. rewrite H. easy.
apply fb_push_same in H0.
specialize (pos2fb_no_zero x) as eq1.
destruct eq1. rewrite H0 in H1. unfold allfalse in H1. inv H1.
destruct y. simpl in *.
assert (forall i, fb_push false (pos2fb x) i = fb_push true (pos2fb y) i).
intros. rewrite H. easy.
specialize (H0 0). simpl in H0. inv H0.
simpl in *.
assert (forall i, fb_push false (pos2fb x) i = fb_push false (pos2fb y) i).
intros. rewrite H. easy.
apply fb_push_same in H0. apply IHx in H0. subst. easy.
simpl in *.
assert (forall i, fb_push false (pos2fb x) i = fb_push true allfalse i).
intros. rewrite H. easy.
specialize (H0 0). simpl in H0. inv H0.
destruct y. simpl in *.
assert (forall i, fb_push true allfalse i = fb_push true (pos2fb y) i).
intros. rewrite H. easy.
apply fb_push_same in H0.
specialize (pos2fb_no_zero y) as eq1. destruct eq1.
rewrite <- H0 in H1. unfold allfalse in H1. inv H1.
simpl in *.
assert (forall i, fb_push true allfalse i = fb_push false (pos2fb y) i).
intros. rewrite H. easy.
specialize (H0 0). simpl in H0. inv H0. easy.
Qed.
Lemma nat2fb_sem : forall x y, nat2fb x = nat2fb y -> x = y.
Proof.
intros. unfold nat2fb,N2fb in H.
destruct (N.of_nat x) eqn:eq1.
destruct (N.of_nat y) eqn:eq2.
simpl in eq1. lia.
specialize (pos2fb_no_zero p) as eq3.
destruct eq3.
rewrite <- H in H0. unfold allfalse in H0. inv H0.
destruct (N.of_nat y) eqn:eq2.
specialize (pos2fb_no_zero p) as eq3.
destruct eq3.
rewrite H in H0. unfold allfalse in H0. inv H0.
apply pos2fb_sem in H. subst.
rewrite <- eq1 in eq2. lia.
Qed.
Lemma f_num_small : forall f n x, cut_n f n = nat2fb x -> x < 2^n.
Proof.
induction n; intros. simpl in *.
assert (cut_n f 0 = allfalse).
unfold cut_n.
apply functional_extensionality.
intros. bdestruct (x0 <? 0). lia. easy.
rewrite H0 in H.
unfold nat2fb in H.
unfold N2fb in H.
destruct (N.of_nat x) eqn:eq1. lia.
specialize (pos2fb_no_zero p) as eq2.
destruct eq2. rewrite <- H in H1. unfold allfalse in H1.
inv H1.
specialize (f_num_0 f n) as eq1.
destruct eq1.
destruct (f n) eqn:eq2.
rewrite f_num_aux_1 with (x := x0) in H; try easy.
apply IHn in H0. simpl.
apply nat2fb_sem in H. subst. lia.
rewrite f_num_aux_0 with (x := x0) in H; try easy.
apply nat2fb_sem in H. subst.
apply IHn in H0. simpl. lia.
Qed.
Definition addto (r : nat -> bool) (n:nat) : nat -> bool := fun i => if i <? n
then (cut_n (fbrev n (sumfb false (cut_n (fbrev n r) n) (nat2fb 1))) n) i else r i.
Definition addto_n (r:nat -> bool) n := fun i => if i <? n
then (cut_n (fbrev n (sumfb false (cut_n (fbrev n r) n) (negatem n (nat2fb 0)))) n) i else r i.
Lemma addto_n_0 : forall r, addto_n r 0 = r.
Proof.
intros.
unfold addto_n.
apply functional_extensionality.
intros.
IfExpSimpl. easy.
Qed.
Lemma addto_0 : forall r, addto r 0 = r.
Proof.
intros.
unfold addto.
apply functional_extensionality.
intros.
IfExpSimpl. easy.
Qed.
Lemma cut_n_fbrev_flip : forall n f, cut_n (fbrev n f) n = fbrev n (cut_n f n).
Proof.
intros.
unfold cut_n, fbrev.
apply functional_extensionality.
intros.
bdestruct (x <? n).
bdestruct (n - 1 - x <? n).
easy. lia. easy.
Qed.
Lemma cut_n_if_cut : forall n f g, cut_n (fun i => if i <? n then f i else g i) n = cut_n f n.
Proof.
intros.
unfold cut_n.
apply functional_extensionality; intros.
bdestruct (x <? n).
easy. easy.
Qed.
Lemma fbrev_twice_same {A}: forall n f, @fbrev A n (fbrev n f) = f.
Proof.
intros.
unfold fbrev.
apply functional_extensionality.
intros.
bdestruct (x <? n).
bdestruct (n - 1 - x <? n).
assert ((n - 1 - (n - 1 - x)) = x) by lia.
rewrite H1. easy.
lia. easy.
Qed.
Lemma cut_n_mod : forall n x, cut_n (nat2fb x) n = (nat2fb (x mod 2^n)).
Proof.
intros.
bdestruct (x <? 2^n).
rewrite Nat.mod_small by lia.
unfold cut_n.
apply functional_extensionality; intros.
bdestruct (x0 <? n). easy.
specialize (nat2fb_bound n x H x0) as eq1.
rewrite eq1. easy. lia.
unfold cut_n.
apply functional_extensionality; intros.
bdestruct (x0 <? n).
unfold nat2fb.
rewrite N2fb_Ntestbit.
rewrite N2fb_Ntestbit.
rewrite <- N.mod_pow2_bits_low with (n := N.of_nat n).
rewrite Nofnat_mod.
rewrite Nofnat_pow. simpl. easy.
apply Nat.pow_nonzero. lia. lia.
assert (x mod 2 ^ n < 2^n).
apply Nat.mod_small_iff.
apply Nat.pow_nonzero. lia.
rewrite Nat.mod_mod. easy.
apply Nat.pow_nonzero. lia.
specialize (nat2fb_bound n (x mod 2^n) H1 x0 H0) as eq1.
rewrite eq1. easy.
Qed.
Lemma add_to_r_same : forall q r, addto (addto_n r q) q = r.
Proof.
intros.
destruct q eqn:eq1.
rewrite addto_n_0.
rewrite addto_0. easy.
unfold addto_n.
specialize (f_num_0 (fbrev (S n) r) (S n)) as eq2.
destruct eq2.
rewrite negatem_arith.
rewrite H.
rewrite sumfb_correct_carry0.
assert (1 < 2 ^ (S n)).
apply Nat.pow_gt_1. lia. lia.
assert (((2 ^ S n - 1 - 0)) = 2^ S n -1) by lia.
rewrite H1.
unfold addto.
rewrite (cut_n_fbrev_flip (S n) (fun i0 : nat =>
if i0 <? S n
then
cut_n
(fbrev (S n)
(nat2fb
(x + (2 ^ S n - 1))))
(S n) i0
else r i0)).
rewrite cut_n_if_cut.
rewrite (cut_n_fbrev_flip (S n)
(nat2fb
(x + (2 ^ S n - 1)))).
rewrite cut_n_mod.
rewrite <- cut_n_fbrev_flip.
rewrite fbrev_twice_same.
rewrite cut_n_mod.
rewrite Nat.mod_mod by lia.
rewrite sumfb_correct_carry0.
assert (((x + (2 ^ S n - 1)) mod 2 ^ S n + 1) = ((x + (2 ^ S n - 1)) mod 2 ^ S n + (1 mod 2^ S n))).
assert (1 mod 2^ S n = 1).
rewrite Nat.mod_small. easy. easy.
rewrite H2. easy.
rewrite H2.
rewrite cut_n_fbrev_flip.
rewrite cut_n_mod.
rewrite <- Nat.add_mod by lia.
assert ((x + (2 ^ S n - 1) + 1) = x + 2 ^ S n) by lia.
rewrite H3.
rewrite Nat.add_mod by lia.
rewrite Nat.mod_same by lia.
assert (x mod 2 ^ S n = x).
rewrite Nat.mod_small. easy.
apply (f_num_small (fbrev (S n) r)). easy.
rewrite H4.
rewrite plus_0_r.
rewrite H4.
rewrite <- H.
rewrite <- cut_n_fbrev_flip.
rewrite fbrev_twice_same.
apply functional_extensionality.
intros.
bdestruct (x0 <? S n).
unfold cut_n.
bdestruct (x0 <? S n).
easy. lia. easy.
specialize (Nat.pow_nonzero 2 (S n)) as eq2.
assert (2 <> 0) by lia. apply eq2 in H0. lia.
Qed.
Lemma add_to_same : forall q r, addto_n (addto r q) q = r.
Proof.
intros.
destruct q eqn:eq1.
rewrite addto_n_0.
rewrite addto_0. easy.
unfold addto.
specialize (f_num_0 (fbrev (S n) r) (S n)) as eq2.
destruct eq2.
rewrite H.
rewrite sumfb_correct_carry0.
unfold addto_n.
assert (0 < 2^ (S n)).
specialize (Nat.pow_nonzero 2 (S n)) as eq2.
assert (2 <> 0) by lia. apply eq2 in H0. lia.
rewrite negatem_arith by lia.
rewrite (cut_n_fbrev_flip (S n) (fun i0 : nat =>
if i0 <? S n
then
cut_n
(fbrev (S n)
(nat2fb (x + 1)))
(S n) i0
else r i0)).
rewrite cut_n_if_cut.
rewrite (cut_n_fbrev_flip (S n)
(nat2fb (x+1))).
rewrite cut_n_mod.
rewrite <- cut_n_fbrev_flip.
rewrite fbrev_twice_same.
rewrite cut_n_mod.
rewrite Nat.mod_mod by lia.
assert ((2 ^ S n - 1) = (2 ^ S n - 1) mod (2^ S n)).
rewrite Nat.mod_small by lia.
easy.
rewrite H1.
rewrite sumfb_correct_carry0.
rewrite cut_n_fbrev_flip.
rewrite cut_n_mod.
assert ((x + 1) mod 2 ^ S n + ((2 ^ S n - 1) mod 2 ^ S n - 0)
= ((x + 1) mod 2 ^ S n + ((2 ^ S n - 1) mod 2 ^ S n))) by lia.
rewrite H2.
rewrite <- Nat.add_mod by lia.
assert ((x + 1 + (2 ^ S n - 1)) = x + 2 ^ S n) by lia.
rewrite H3.
rewrite Nat.add_mod by lia.
rewrite Nat.mod_same by lia.
rewrite plus_0_r.
rewrite Nat.mod_mod by lia.
rewrite Nat.mod_small.
rewrite <- H.
rewrite cut_n_fbrev_flip.
rewrite fbrev_twice_same.
apply functional_extensionality.
intros.
bdestruct (x0 <? S n).
unfold cut_n.
bdestruct (x0 <? S n).
easy. lia. easy.
apply (f_num_small (fbrev (S n) r)). easy.
Qed.
Lemma posi_neq_f : forall (p p' : posi), p <> p' -> fst p <> fst p' \/ snd p <> snd p'.
Proof.
intros. destruct p. destruct p'.
simpl in *.
bdestruct (v =? v0).
subst. right.
intros R. subst. contradiction.
bdestruct (n =? n0).
subst.
left.
intros R. subst. contradiction.
left. lia.
Qed.
Lemma posi_neq_b : forall (p p' : posi), fst p <> fst p' \/ snd p <> snd p' -> p <> p'.
Proof.
intros. destruct p. destruct p'.
simpl in *.
intros R. inv R.
destruct H.
lia.
lia.
Qed.
Lemma xorb_andb_distr_l : forall x y z, (x ⊕ y) && z = (x && z) ⊕ (y && z).
Proof.
intros. btauto.
Qed.
Lemma xorb_andb_distr_r : forall x y z, z && (x ⊕ y) = (z && x) ⊕ (z && y).
Proof.
intros. btauto.
Qed.
Ltac bt_simpl := repeat (try rewrite xorb_false_r; try rewrite xorb_false_l;
try rewrite xorb_true_r; try rewrite xorb_true_r;
try rewrite andb_false_r; try rewrite andb_false_l;
try rewrite andb_true_r; try rewrite andb_true_l;
try rewrite xorb_andb_distr_l; try rewrite xorb_andb_distr_r;
try rewrite andb_diag).
Lemma msm_eq1 :
forall n i c f g,
S i < n ->