-
Notifications
You must be signed in to change notification settings - Fork 3
/
OQASMProof.v
executable file
·10429 lines (9885 loc) · 340 KB
/
OQASMProof.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 Reals.
Require Import Psatz.
Require Import SQIR.
Require Import VectorStates UnitaryOps Coq.btauto.Btauto Coq.NArith.Nnat.
Require Import Dirac.
Require Import QPE.
Require Import BasicUtility.
Require Import MathSpec.
Require Import Classical_Prop.
Require Import OQASM.
(**********************)
(** Unitary Programs **)
(**********************)
Declare Scope exp_scope.
Delimit Scope exp_scope with exp.
Local Open Scope exp_scope.
Local Open Scope nat_scope.
Lemma mapsto_always_same : forall k v1 v2 s,
@Env.MapsTo (type) k v1 s ->
@Env.MapsTo (type) k v2 s ->
v1 = v2.
Proof.
intros.
apply Env.find_1 in H.
apply Env.find_1 in H0.
rewrite H in H0.
injection H0.
easy.
Qed.
Lemma find_add : forall k v m,
@Env.find (type) k (Env.add k v m) = Some v.
Proof.
intros.
apply Env.find_1.
apply Env.add_1.
easy.
Qed.
Lemma mapsto_add1 : forall k v1 v2 s,
@Env.MapsTo (type) k v1 (Env.add k v2 s) -> v1 = v2.
Proof.
intros.
apply Env.find_1 in H.
rewrite find_add in H.
inversion H.
reflexivity.
Qed.
Lemma mapsto_equal : forall k v s1 s2,
@Env.MapsTo type k v s1 ->
Env.Equal s1 s2 ->
Env.MapsTo k v s2.
Proof.
intros.
unfold Env.Equal in H0.
apply Env.find_2. rewrite <- H0.
apply Env.find_1.
assumption.
Qed.
Lemma map_find_add : forall x v env, @Env.find (type) x (Env.add x v env) = Some v.
Proof.
intros.
apply Env.find_1.
apply Env.add_1. easy.
Qed.
Lemma map_find_neq : forall x y v env, x <> y -> @Env.find (type) x (Env.add y v env) = Env.find x env.
Proof.
intros.
destruct (Env.find (elt:=type) x env) eqn:eq1.
apply Env.find_1. apply Env.add_2. lia.
apply Env.find_2 in eq1. easy.
destruct (Env.find (elt:=type) x (Env.add y v env)) eqn:eq2.
apply Env.find_2 in eq2. apply Env.add_3 in eq2.
apply Env.find_1 in eq2. rewrite eq1 in eq2. inv eq2. lia.
easy.
Qed.
(* Helper theorems for fbrev and rotation. *)
Lemma fbrev_1_same {A}: forall f, @fbrev A 1 f = f.
Proof.
intros.
unfold fbrev.
apply functional_extensionality. intros.
bdestruct (x<?1).
assert (1 - 1 - x = x) by lia.
rewrite H0. easy. easy.
Qed.
Lemma rotate_twice_same_1 : forall r, (rotate (rotate r 1) 1) = r.
Proof.
intros. unfold rotate.
unfold addto.
apply functional_extensionality. intros.
bdestruct (x <? 1).
assert ( x = 0) by lia. subst.
repeat rewrite fbrev_1_same.
destruct (r 0) eqn:eq1.
specialize (cut_n_1_1 r eq1) as eq2.
rewrite eq2.
rewrite sumfb_correct_carry0.
rewrite cut_n_mod.
assert (((1 + 1) mod 2 ^ 1) = 0).
assert ((1 + 1) = 2) by lia. rewrite H0.
rewrite Nat.pow_1_r. rewrite Nat.mod_same. easy. lia.
rewrite H0.
rewrite cut_n_if_cut.
rewrite cut_n_mod.
rewrite Nat.pow_1_r. rewrite Nat.mod_small by lia.
rewrite sumfb_correct_carry0.
rewrite plus_0_l.
rewrite cut_n_mod.
rewrite Nat.pow_1_r.
rewrite Nat.mod_small by lia.
unfold nat2fb. simpl. easy.
rewrite (cut_n_1_0 r eq1).
rewrite sumfb_correct_carry0.
rewrite plus_0_l.
rewrite cut_n_mod.
rewrite Nat.pow_1_r. rewrite Nat.mod_small by lia.
rewrite cut_n_if_cut.
rewrite cut_n_mod.
rewrite Nat.pow_1_r. rewrite Nat.mod_small by lia.
rewrite sumfb_correct_carry0.
assert ((1 + 1) = 2) by lia. rewrite H0.
rewrite cut_n_mod.
rewrite Nat.pow_1_r.
rewrite Nat.mod_same by lia.
unfold nat2fb. easy.
easy.
Qed.
Lemma rotate_1_0: forall r, r 0 = false -> rotate r 1 0 = true.
Proof.
unfold rotate, addto.
intros.
bdestruct (0 <? 1).
repeat rewrite fbrev_1_same.
rewrite (cut_n_1_0 r H).
rewrite sumfb_correct_carry0.
rewrite plus_0_l.
rewrite cut_n_mod.
rewrite Nat.pow_1_r.
rewrite Nat.mod_small by lia. easy. lia.
Qed.
Lemma rotate_1_1: forall r, r 0 = true -> rotate r 1 0 = false.
Proof.
unfold rotate, addto.
intros.
bdestruct (0 <? 1).
repeat rewrite fbrev_1_same.
rewrite (cut_n_1_1 r H).
rewrite sumfb_correct_carry0.
rewrite cut_n_mod.
rewrite Nat.pow_1_r.
rewrite Nat.mod_same by lia. easy. lia.
Qed.
(*
Lemma hexchange_twice_same :
forall v1 v2, hexchange (hexchange v1 v2) v2 = v1.
Proof.
intros.
unfold hexchange.
destruct v1 eqn:eq1. easy.
destruct v2 eqn:eq2. easy.
destruct (eqb b0 b3) eqn:eq3. easy.
assert ((¬ (¬ b2)) = b2) by btauto. rewrite H0. easy.
easy. easy.
Qed.
Lemma h_case_twice_same :
forall t v, right_mode_val t v -> h_case (h_case v) = v.
Proof.
intros. unfold h_case.
destruct v.
destruct (r 0) eqn:eq1.
destruct b.
rewrite rotate_twice_same_1. easy.
rewrite rotate_twice_same_1. easy.
destruct b. simpl. easy. simpl. easy.
inv H0.
destruct b1.
destruct b2. rewrite H3. simpl. easy.
rewrite H3. simpl. easy.
destruct b2.
rewrite (rotate_1_0 r H3).
rewrite rotate_twice_same_1. easy.
rewrite (rotate_1_0 r H3).
rewrite rotate_twice_same_1. easy.
easy.
Qed.
*)
Lemma get_cua_eq : forall f x v, nor_mode f x -> get_cua ((f[x |-> put_cu (f x) v]) x) = v.
Proof.
intros.
unfold get_cua.
rewrite eupdate_index_eq.
unfold put_cu.
unfold nor_mode in H.
destruct (f x). easy. inv H.
Qed.
Lemma type_nor_mode : forall f aenv env p,
Env.MapsTo (fst p) Nor env -> snd p < aenv (fst p) -> right_mode_env aenv env f -> nor_mode f p.
Proof.
intros. unfold right_mode_env in *.
apply (H1 Nor) in H0 ; try easy.
inv H0. unfold nor_mode.
rewrite <- H2. easy.
Qed.
Lemma type_nor_modes : forall f aenv env x,
Env.MapsTo x Nor env -> right_mode_env aenv env f -> nor_modes f x (aenv x).
Proof.
intros. unfold right_mode_env in *.
unfold nor_modes. intros.
specialize (H0 Nor (x,i)).
simpl in H0. apply H0 in H1; try easy.
inv H1. unfold nor_mode.
assert ((@pair var nat x i) = (@pair Env.key nat x i)) by easy.
rewrite H1 in *.
rewrite <- H2. easy.
Qed.
Lemma nor_mode_nval : forall f x, nor_mode f x -> (exists r, f x = nval true r \/ f x = nval false r).
Proof.
intros. unfold nor_mode in *. destruct (f x); inv H.
exists r.
destruct b. left. easy. right. easy.
Qed.
Lemma neq_sym {A} : forall (x y: A), x <> y -> y <> x.
Proof.
intros. intros R.
subst. contradiction.
Qed.
Lemma nor_mode_up : forall f x y v, x <> y -> nor_mode f x -> nor_mode (f[y |-> v]) x.
Proof.
intros. unfold nor_mode in *.
assert ((f [y |-> v]) x = (f x)).
rewrite eupdate_index_neq. reflexivity. apply neq_sym. assumption.
rewrite H1.
destruct (f x); inv H0. easy.
Qed.
Lemma get_cus_cua : forall n f x m, m < n -> get_cus n f x m = get_cua (f (x,m)).
Proof.
intros.
unfold get_cus,get_cua.
bdestruct (m <? n).
destruct (f (x,n)). easy. easy.
lia.
Qed.
Definition put_cus (f:posi -> val) (x:var) (g:nat -> bool) (n:nat) : (posi -> val) :=
fun a => if fst a =? x then if snd a <? n then put_cu (f a) (g (snd a)) else f a else f a.
Lemma get_r_put_same : forall (f:posi -> val) x y g n i,
get_r (put_cus f x g n (y,i)) = get_r (f (y,i)).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (y =? x).
bdestruct (i <? n).
unfold put_cu.
destruct (f (y, i)).
unfold get_r. easy. easy. easy. easy.
Qed.
Lemma get_r_put_cu_same : forall (f:posi -> val) p v,
nor_mode f p ->
get_r (put_cu (f p) v) = get_r (f p).
Proof.
intros.
unfold put_cu,nor_mode in *.
destruct (f p). easy. easy.
Qed.
Lemma cus_get_neq : forall (f:posi -> val) (x y :var) g n i,
x <> y -> get_cua ((put_cus f y g n) (x,i)) = get_cua (f (x,i)).
Proof.
intros.
unfold get_cua.
unfold put_cus.
simpl.
bdestruct (x =? y).
lia. easy.
Qed.
Lemma put_cus_out : forall (f:posi -> val) (x y :var) g n i,
n <= i -> ((put_cus f y g n) (x,i)) = (f (x,i)).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (x =? y).
bdestruct (i <? n). lia.
easy. easy.
Qed.
Lemma nor_mode_cus_eq: forall f x g n y i,
nor_mode f (y,i) -> nor_mode (put_cus f x g n) (y,i).
Proof.
intros. unfold nor_mode in *.
unfold put_cus.
simpl.
bdestruct (y =? x).
bdestruct (i <? n).
destruct (f (y, i)).
unfold put_cu. easy.
inv H.
apply H. apply H.
Qed.
Lemma cus_get_eq : forall (f:posi -> val) (x :var) g n i,
i < n -> nor_modes f x n -> get_cua ((put_cus f x g n) (x,i)) = g i.
Proof.
intros.
unfold get_cua.
unfold put_cus.
simpl.
bdestruct (x =? x).
bdestruct (i <? n).
unfold put_cu.
specialize (H0 i H2). unfold nor_mode in *.
destruct (f (x, i)) eqn:eq1. easy.
inv H0.
lia. lia.
Qed.
Lemma put_cus_eq : forall (f:posi -> val) (x:var) g n i,
i < n -> (put_cus f x g n) (x,i) = put_cu (f (x,i)) (g i).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (x =? x).
bdestruct (i <? n). easy. lia. lia.
Qed.
Lemma put_cus_neq : forall (f:posi -> val) (x y:var) g n i,
x <> y -> (put_cus f x g n) (y,i) = f (y,i).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (y =? x). lia. easy.
Qed.
Lemma put_cus_neq_1 : forall (f:posi -> val) (x:var) g n c,
x <> fst c -> (put_cus f x g n) c = f c.
Proof.
intros.
destruct c. apply put_cus_neq.
simpl in H. assumption.
Qed.
Lemma put_cus_neq_2 : forall (f:posi -> val) (x y:var) g n i,
n <= i -> (put_cus f x g n) (y,i) = f (y,i).
Proof.
intros.
unfold put_cus.
simpl.
bdestruct (y =? x).
bdestruct (i <? n). lia. easy.
easy.
Qed.
Lemma put_cu_cus : forall (f:posi -> val) x y g i n v, put_cu (put_cus f y g n (x,i)) v = put_cu (f (x,i)) v.
Proof.
intros.
unfold put_cus,put_cu.
simpl.
bdestruct (x =? y).
bdestruct (i <? n).
destruct (f (x,i)). easy. easy. easy. easy.
Qed.
Lemma nor_mode_up_1 : forall f x v, nor_mode f x -> nor_mode (f[x |-> put_cu (f x) v]) x.
Proof.
intros.
unfold nor_mode in *.
rewrite eupdate_index_eq.
unfold put_cu.
destruct (f x).
easy. inv H.
Qed.
Lemma nor_mode_ups : forall f f' x v, f x = f' x -> nor_mode f x ->
nor_mode (f[x |-> put_cu (f' x) v]) x.
Proof.
intros. unfold nor_mode in *.
rewrite eupdate_index_eq.
unfold put_cu. rewrite <- H.
destruct (f x); inv H0. easy.
Qed.
Lemma nor_mode_get : forall f x, nor_mode f x -> (exists b, get_cua (f x) = b).
Proof.
intros. unfold nor_mode in *. destruct (f x); inv H.
exists b. unfold get_cua. reflexivity.
Qed.
Lemma put_get_cus_eq :
forall f x n, nor_modes f x n -> (put_cus f x (get_cus n f x) n) = f.
Proof.
intros.
unfold put_cus,get_cus,put_cu.
apply functional_extensionality. intros.
destruct x0. simpl.
bdestruct (v =? x). bdestruct (n0 <? n).
subst.
unfold nor_modes in H.
specialize (H n0 H1) as eq1. unfold nor_mode in eq1.
destruct (f (x,n0)). easy. inv eq1.
easy. easy.
Qed.
Lemma get_cus_put_eq :
forall f x v n, v < 2^n -> nor_modes f x n -> get_cus n (put_cus f x (nat2fb v) n) x = (nat2fb v).
Proof.
intros.
unfold get_cus.
apply functional_extensionality. intros.
bdestruct (x0 <? n).
simpl.
unfold nor_modes in H.
assert (nor_mode (put_cus f x (nat2fb v) n) (x, x0)).
apply nor_mode_cus_eq. apply H0. easy.
unfold nor_mode in H2.
destruct (put_cus f x ((nat2fb v)) n (x, x0)) eqn:eq2.
unfold put_cus in eq2. simpl in eq2.
bdestruct (x =? x).
bdestruct (x0 <? n).
unfold put_cu in eq2.
assert (nor_mode f (x,x0)).
apply H0. easy.
unfold nor_mode in H5.
destruct (f (x, x0)). inv eq2. easy. inv H5. lia. lia.
inv H2.
unfold allfalse.
rewrite nat2fb_bound with (n := n); try easy.
Qed.
Lemma put_cus_same : forall f x g n, nor_modes f x n
-> get_cus n f x = g -> put_cus f x g n = f.
Proof.
intros.
rewrite <- H0.
rewrite put_get_cus_eq. easy. easy.
Qed.
Lemma get_cus_put_neq :
forall f x y g n, x <> y -> get_cus n (put_cus f x g n) y = get_cus n f y.
Proof.
intros. unfold get_cus,put_cus.
apply functional_extensionality. intros.
simpl.
bdestruct ( y =? x). lia.
destruct (f (y, x0)). easy. easy.
Qed.
Lemma get_put_cus_cut_n : forall n f x g, nor_modes f x n
-> (get_cus n (put_cus f x g n) x) = cut_n g n.
Proof.
intros. unfold get_cus,put_cus,cut_n.
apply functional_extensionality. intros.
bdestruct (x0 <? n). simpl.
bdestruct (x =? x).
bdestruct (x0 <? n).
unfold put_cu.
unfold nor_modes in H0.
specialize (H x0 H2). unfold nor_mode in H.
destruct (f (x,x0)). easy. lia. lia.
lia. easy.
Qed.
Definition get_cu (v : val) :=
match v with nval b r => Some b
| _ => None
end.
Lemma put_get_cu : forall f x, nor_mode f x -> put_cu (f x) (get_cua (f x)) = f x.
Proof.
intros. unfold put_cu, get_cua.
unfold nor_mode in H. destruct (f x); inv H.
reflexivity.
Qed.
Lemma get_put_cu : forall f x v, nor_mode f x -> get_cua (put_cu (f x) v) = v.
Proof.
intros. unfold put_cu, get_cua.
unfold nor_mode in H. destruct (f x); inv H.
reflexivity.
Qed.
Lemma get_cua_t : forall b r, get_cua (nval b r) = b.
Proof.
intros. unfold get_cua. reflexivity.
Qed.
(* Proofs of types and syntax. *)
Ltac nor_sym := try (apply neq_sym; assumption) ; try assumption.
Lemma iner_neq : forall (x y:var) (a b:nat), x <> y -> (x,a) <> (y,b).
Proof.
intros. intros R.
inv R. contradiction.
Qed.
Lemma iner_neq_1 : forall (x:var) (c:posi) a, x <> fst c -> (x,a) <> c.
Proof.
intros. intros R.
destruct c.
inv R. contradiction.
Qed.
Lemma iner_neq_2 : forall (x:var) (c:posi) a, x <> fst c -> c <> (x,a).
Proof.
intros. intros R.
destruct c.
inv R. contradiction.
Qed.
Ltac tuple_eq := intros R; inv R; lia.
Ltac iner_p := try nor_sym; try tuple_eq ; try (apply iner_neq ; assumption)
; try (apply iner_neq_1 ; assumption) ; try (apply iner_neq_2 ; assumption).
Lemma assign_r_right_mode : forall n i size f x r, i < n <= size ->
(forall i, i < size -> right_mode_val Nor (f (x,i))) ->
right_mode_val (Phi size) (assign_r f x r n (x,i)).
Proof.
induction n; intros; simpl. inv H. inv H1.
bdestruct (i =? n).
subst. rewrite eupdate_index_eq.
unfold up_qft.
specialize (H0 n).
assert (n < size) by lia. apply H0 in H1. inv H1.
constructor.
rewrite eupdate_index_neq by iner_p.
apply IHn. lia. easy.
Qed.
Lemma assign_h_right_mode : forall i b j size f x, b <= j < size -> j < b + i ->
(forall i, b <= i < size -> right_mode_val Nor (f (x,i))) ->
right_mode_val (Phi b) (assign_h f x b i (x,j)).
Proof.
induction i; intros; simpl. lia.
bdestruct (j =? b + i).
subst.
rewrite eupdate_index_eq. unfold up_h.
destruct (f (x, b + i)) eqn:eq1. destruct b0.
constructor. constructor.
specialize (H1 (b+i) H). inv H1. rewrite eq1 in H2. inv H2.
rewrite eupdate_index_neq by iner_p.
apply IHi with (size := size). lia. lia. easy.
Qed.
Lemma assign_r_right_mode_out : forall n t f x r v i, v <> x ->
right_mode_val t (f (v,i)) ->
right_mode_val t (assign_r f x r n (v,i)).
Proof.
induction n; intros; simpl. easy.
rewrite eupdate_index_neq by iner_p.
apply IHn. lia. easy.
Qed.
Lemma assign_h_right_mode_out : forall n t f x r v i, v <> x ->
right_mode_val t (f (v,i)) ->
right_mode_val t (assign_h f x r n (v,i)).
Proof.
induction n; intros; simpl. easy.
rewrite eupdate_index_neq by iner_p.
apply IHn. lia. easy.
Qed.
Lemma assign_h_r_right_mode : forall i b j size f x, b <= j < size -> j < b + i ->
(forall i, b <= i < size -> right_mode_val (Phi b) (f (x,i))) ->
right_mode_val Nor (assign_h_r f x b i (x,j)).
Proof.
induction i; intros; simpl. lia.
bdestruct (j =? b + i).
subst.
rewrite eupdate_index_eq. unfold up_h.
destruct (f (x, b + i)) eqn:eq1. destruct b0.
specialize (H1 (b+i) H). inv H1. rewrite eq1 in H4. inv H4.
specialize (H1 (b+i) H). inv H1. rewrite eq1 in H4. inv H4.
constructor.
rewrite eupdate_index_neq by iner_p.
apply IHi with (size := size). lia. lia. easy.
Qed.
Lemma assign_h_r_right_mode_out : forall n t f x r v i, v <> x ->
right_mode_val t (f (v,i)) ->
right_mode_val t (assign_h_r f x r n (v,i)).
Proof.
induction n; intros; simpl. easy.
rewrite eupdate_index_neq by iner_p.
apply IHn. lia. easy.
Qed.
Lemma assign_seq_right_mode : forall n i f x r, i < n ->
right_mode_val Nor (assign_seq f x r n (x,i)).
Proof.
induction n; intros; simpl.
inv H.
bdestruct (i =? n).
subst. rewrite eupdate_index_eq.
constructor.
rewrite eupdate_index_neq by iner_p.
apply IHn. lia.
Qed.
Lemma assign_seq_right_mode_out : forall n t f x r v i, v <> x ->
right_mode_val t (f (v,i)) ->
right_mode_val t (assign_seq f x r n (v,i)).
Proof.
induction n; intros; simpl. easy.
rewrite eupdate_index_neq by iner_p.
apply IHn. lia. easy.
Qed.
(*
Lemma h_sem_right_mode_nor : forall n i f x, i < n ->
right_mode_val Nor (f (x,i)) ->
right_mode_val Had (h_sem f x n (x,i)).
Proof.
induction n; intros; simpl. lia.
inv H1.
bdestruct (i =? n). subst.
rewrite eupdate_index_eq.
rewrite <- H2. unfold h_case. destruct (r 0) eqn:eq1; constructor.
rewrite rotate_1_1; try easy. easy.
rewrite eupdate_index_neq by iner_p.
apply IHn. lia. rewrite <- H2. constructor.
Qed.
Lemma h_sem_right_mode_had : forall n i f x, i < n ->
right_mode_val Had (f (x,i)) ->
right_mode_val Nor (h_sem f x n (x,i)).
Proof.
induction n; intros; simpl. lia.
inv H1.
bdestruct (i =? n). subst.
rewrite eupdate_index_eq.
rewrite <- H2. unfold h_case.
destruct b1. destruct b2; constructor.
destruct b2; constructor.
rewrite eupdate_index_neq by iner_p.
apply IHn. lia. rewrite <- H2. constructor. easy.
Qed.
Lemma h_sem_right_mode_out : forall n t f x v i, v <> x ->
right_mode_val t (f (v,i)) ->
right_mode_val t(h_sem f x n (v,i)).
Proof.
induction n; intros; simpl. easy.
rewrite eupdate_index_neq by iner_p.
apply IHn. lia. easy.
Qed.
*)
(* Defining matching shifting stack. *)
Lemma exp_neu_t_prop : forall p x l l', exp_neu_l x l p l' -> exp_neu_prop l -> exp_neu_prop l'.
Proof.
induction p; intros; try easy.
1-7:inv H; easy.
unfold exp_neu_prop in *.
intros. inv H.
destruct l'. simpl in *. lia.
destruct i. simpl in *.
destruct l'. easy.
specialize (H0 1 a).
assert (1 + 1 < S (S (length (s0 :: l')))) by lia.
apply H0 in H. simpl in *. easy.
simpl in *. easy.
specialize (H0 (S (S i)) a).
assert (S (S i) + 1 < length (Rs :: s :: l')).
simpl in *. lia.
apply H0 in H.
simpl in *. easy.
simpl in *. easy.
unfold fst_not_opp in H5. destruct l. simpl in *. lia.
destruct i. simpl in *. inv H2.
unfold opp_ls in *. intros R. inv R. easy.
specialize (H0 i a).
assert (i + 1 < length (s :: l)). simpl in *. lia.
apply H0 in H. simpl in *. easy. simpl in *. easy.
apply H0; try easy.
unfold exp_neu_prop in *.
intros. inv H.
destruct l'. simpl in *. lia.
destruct i. simpl in *.
destruct l'. easy.
specialize (H0 1 a).
assert (1 + 1 < S (S (length (s0 :: l')))) by lia.
apply H0 in H. simpl in *. easy.
simpl in *. easy.
specialize (H0 (S (S i)) a).
assert (S (S i) + 1 < length (Ls :: s :: l')).
simpl in *. lia.
apply H0 in H.
simpl in *. easy.
simpl in *. easy.
unfold fst_not_opp in *. destruct l. simpl in *. lia.
destruct i. simpl in *. inv H2.
unfold opp_ls. intros R. inv R. easy.
specialize (H0 i a).
assert (i + 1 < length (s :: l)). simpl in *. lia.
apply H0 in H. simpl in *. easy. simpl in *. easy.
apply H0; try easy.
unfold exp_neu_prop in *.
intros. inv H.
destruct i. simpl in *.
destruct l'. easy.
specialize (H0 1 a).
assert (1 + 1 < S (length (s :: l'))) by lia.
apply H0 in H. simpl in *. easy.
simpl in *. easy.
specialize (H0 (S (S i)) a).
assert (S (S i) + 1 < length (Re :: l')).
simpl in *. lia.
apply H0 in H.
simpl in *. easy.
simpl in *. easy.
unfold fst_not_opp in *. destruct l. simpl in *. lia.
destruct i. simpl in *. inv H2.
unfold opp_ls. intros R. inv R. easy.
specialize (H0 i a).
assert (i + 1 < length (s :: l)). simpl in *. lia.
apply H0 in H. simpl in *. easy. simpl in *. easy.
apply H0; try easy.
1-2:inv H; easy.
inv H.
apply IHp2 with (x := x) (l := l'0); try easy.
apply IHp1 with (x:=x) (l := l); try easy.
Qed.
Lemma eupdates_twice_neq : forall f x g n c v, x <> fst c
-> (put_cus f x g n)[c |-> v] = put_cus (f[c |-> v]) x g n.
Proof.
intros. unfold put_cus.
apply functional_extensionality.
intros.
bdestruct (x0 ==? c).
subst.
rewrite eupdate_index_eq.
bdestruct (fst c =? x).
subst. contradiction.
rewrite eupdate_index_eq. easy.
rewrite eupdate_index_neq.
bdestruct (fst x0 =? x).
rewrite eupdate_index_neq.
easy. nor_sym.
rewrite eupdate_index_neq by nor_sym.
easy. nor_sym.
Qed.
Lemma get_cus_up : forall n f x c v, fst c <> x -> get_cus n (f[c |-> v]) x = get_cus n f x.
Proof.
intros.
apply functional_extensionality; intro.
unfold get_cus.
bdestruct (x0 <? n). destruct c. simpl in *. rewrite eupdate_index_neq by iner_p.
easy. easy.
Qed.
Lemma get_cus_up_ge : forall n f x c v, n <= snd c -> get_cus n (f[c |-> v]) x = get_cus n f x.
Proof.
intros.
apply functional_extensionality; intro.
unfold get_cus.
bdestruct (x0 <? n). destruct c. simpl in *. rewrite eupdate_index_neq by iner_p.
easy. easy.
Qed.
Lemma put_cu_mid_eq : forall (f g:posi -> val) a v,
nor_mode f a -> nor_mode g a -> get_r (f a) = get_r (g a) -> (put_cu (f a) v) = (put_cu (g a) v).
Proof.
intros.
unfold put_cu. unfold nor_mode in *.
destruct (f a). destruct (g a).
unfold get_r in H1. subst.
easy. inv H0.
inv H.
Qed.
Lemma put_cus_twice_neq : forall (f:posi -> val) (x y:var) g1 g2 n,
x <> y -> (put_cus (put_cus f x g1 n) y g2 n)
= (put_cus (put_cus f y g2 n) x g1 n).
Proof.
intros.
apply functional_extensionality.
unfold put_cus. intros.
destruct x0. simpl.
bdestruct (v =? y).
bdestruct (v =? x). lia. easy.
easy.
Qed.
Lemma put_cu_twice_eq : forall (f:posi -> val) (x:var) v1 v2 i,
put_cu (put_cu (f (x,i)) v1) v2 = put_cu (f (x,i)) v2.
Proof.
intros. unfold put_cu.
destruct (f (x, i)). easy. easy.
Qed.
Lemma put_cu_twice_eq_1 : forall (f:posi -> val) c v1 v2,
put_cu (put_cu (f c) v1) v2 = put_cu (f c) v2.
Proof.
intros. unfold put_cu.
destruct (f c). easy. easy.
Qed.
Lemma put_cus_twice_eq : forall (f:posi -> val) (x:var) g1 g2 n,
(put_cus (put_cus f x g1 n) x g2 n)
= (put_cus f x g2 n).
Proof.
intros.
apply functional_extensionality.
unfold put_cus. intros.
destruct x0. simpl.
bdestruct (v =? x).
bdestruct (n0 <? n). rewrite put_cu_twice_eq. easy.
easy.
easy.
Qed.
Lemma put_cus_sem_eq : forall (f:posi -> val) (x:var) g1 g2 n,
(forall m, m < n -> g1 m = g2 m) ->
(put_cus f x g1 n) = (put_cus f x g2 n).
Proof.
intros.
apply functional_extensionality.
unfold put_cus. intros.
destruct x0. simpl.
bdestruct (v =? x).
bdestruct (n0 <? n). rewrite H. easy.
lia. easy. easy.
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 x_nor_sem : forall aenv f x v, nor_mode f x -> put_cu (f x) (¬ (get_cua (f x))) = v ->
exp_sem aenv (X x) f = (f[x |-> v]).
Proof.
intros.
apply nor_mode_nval in H.
destruct H. destruct H.
unfold get_cua in H0. rewrite H in H0.
unfold put_cu in H0. subst.
assert ((exchange (f x)) = nval (¬ true) x0).
unfold exchange. rewrite H. reflexivity.
rewrite <- H0. simpl. easy.
unfold get_cua in H0. rewrite H in H0.
unfold put_cu in H0. subst.
assert ((exchange (f x)) = nval (¬ false) x0).
unfold exchange. rewrite H.
reflexivity.
rewrite <- H0. simpl. easy.
Qed.
Lemma lshift'_irrelevant :
forall n size f x p, fst p <> x -> lshift' n size f x p = f p.
Proof.
intros.
induction n.
simpl.
rewrite eupdate_index_neq. easy.
apply iner_neq_1. lia.
simpl.
rewrite eupdate_index_neq.
rewrite IHn.
easy.
apply iner_neq_1. lia.
Qed.
Lemma rshift'_irrelevant :
forall n size f x p, fst p <> x -> rshift' n size f x p = f p.
Proof.
intros.
induction n.
intros. simpl.
rewrite eupdate_index_neq. easy.
apply iner_neq_1. lia.
intros. simpl.
rewrite eupdate_index_neq.
rewrite IHn. easy.
apply iner_neq_1. lia.
Qed.
Lemma sr_rotate'_ge :
forall n size f x p, n <= snd p -> sr_rotate' f x n size p = f p.
Proof.
intros. induction n.
easy.
simpl.
rewrite eupdate_index_neq.
rewrite IHn. easy. lia.
destruct p.
bdestruct (x =? v). subst.
intros R. inv R. simpl in H. lia.
intros R. inv R. lia.
Qed.
Lemma sr_rotate'_lt :
forall n size f p, snd p < n -> n <= size ->
sr_rotate' f (fst p) n size p = times_rotate (f p) (size - (snd p)).
Proof.
intros. induction n.
easy.
simpl.
destruct p. simpl in *.
bdestruct (n =? n0). subst.
rewrite eupdate_index_eq. easy.
rewrite eupdate_index_neq by iner_p.
rewrite IHn. easy. lia. lia.
Qed.
Lemma sr_rotate'_irrelevant :
forall n size f x p, fst p <> x -> sr_rotate' f x n size p = f p.
Proof.
intros. induction n.
easy.
simpl.
rewrite eupdate_index_neq.
rewrite IHn. easy.
destruct p. iner_p.
Qed.
Lemma srr_rotate'_lt :
forall n size f p, snd p < n -> n <= size ->
srr_rotate' f (fst p) n size p = times_r_rotate (f p) (size - (snd p)).
Proof.
intros. induction n.
easy.
simpl.
destruct p. simpl in *.
bdestruct (n =? n0). subst.
rewrite eupdate_index_eq. easy.
rewrite eupdate_index_neq by iner_p.
rewrite IHn. easy. lia. lia.
Qed.
Lemma srr_rotate'_ge :
forall n size f x p, n <= snd p -> srr_rotate' f x n size p = f p.
Proof.
intros. induction n.
easy.
simpl.
rewrite eupdate_index_neq.
rewrite IHn. easy. lia.
destruct p.
bdestruct (x =? v). subst.
intros R. inv R. simpl in H. lia.
intros R. inv R. lia.
Qed.
Lemma srr_rotate'_irrelevant :
forall n size f x p, fst p <> x -> srr_rotate' f x n size p = f p.
Proof.
intros. induction n.
easy.