-
Notifications
You must be signed in to change notification settings - Fork 3
/
OQIMP.v
executable file
·7241 lines (6829 loc) · 292 KB
/
OQIMP.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 OQASM.
Require Import OQASMProof.
Require Import CLArith.
Require Import RZArith.
Require Import Coq.FSets.FMapList.
Require Import Coq.FSets.FMapFacts.
Require Import Coq.Structures.OrderedTypeEx.
(* The definition of QSSA. *)
Local Open Scope exp_scope.
Local Open Scope nat_scope.
(* Define function variables. *)
Definition fvar := nat.
(* Flag for setting if one wants to generate QFT circuit or classical circuit. *)
Inductive flag := QFTA | Classic.
Definition flag_eq (t1 t2:flag) : bool :=
match t1 with QFTA => match t2 with QFTA => true
| _ => false
end
| Classic => match t2 with Classic => true
| _ => false
end
end.
Notation "i '=fl=' j" := (flag_eq i j) (at level 50).
Lemma flag_eqb_eq : forall a b, a =fl= b = true -> a = b.
Proof.
intros. unfold flag_eq in H.
destruct a. destruct b. easy. inv H.
destruct b. inv H. easy.
Qed.
Lemma flag_eqb_neq : forall a b, a =fl= b = false -> a <> b.
Proof.
intros. unfold flag_eq in H.
destruct a. destruct b. inv H. easy.
destruct b. easy. inv H.
Qed.
Lemma flag_eq_reflect : forall r1 r2, reflect (r1 = r2) (flag_eq r1 r2).
Proof.
intros.
destruct (r1 =fl= r2) eqn:eq1.
apply ReflectT.
apply flag_eqb_eq in eq1.
assumption.
constructor.
apply flag_eqb_neq in eq1.
assumption.
Qed.
(* Global and local variables are different like @x and %x in LLVM. *)
Inductive qvar := G (v:var) | L (v:var).
Definition qty_eq (t1 t2:qvar) : bool :=
match t1 with G x => match t2 with G y => (x =? y)
| _ => false
end
| L x => match t2 with L y => (x =? y)
| _ => false
end
end.
Notation "i '=q=' j" := (qty_eq i j) (at level 50).
Lemma qty_eqb_eq : forall a b, a =q= b = true -> a = b.
Proof.
intros. unfold qty_eq in H.
destruct a. destruct b.
apply Nat.eqb_eq in H. subst. easy. inv H.
destruct b. inv H.
apply Nat.eqb_eq in H. subst. easy.
Qed.
Lemma qty_eqb_neq : forall a b, a =q= b = false -> a <> b.
Proof.
intros. unfold qty_eq in H.
destruct a. destruct b.
apply Nat.eqb_neq in H.
intros R. inv R. contradiction.
intros R. inv R.
destruct b.
intros R. inv R.
apply Nat.eqb_neq in H.
intros R. inv R. contradiction.
Qed.
Lemma qty_eq_reflect : forall r1 r2, reflect (r1 = r2) (qty_eq r1 r2).
Proof.
intros.
destruct (r1 =q= r2) eqn:eq1.
apply ReflectT.
apply qty_eqb_eq in eq1.
assumption.
constructor.
apply qty_eqb_neq in eq1.
assumption.
Qed.
Definition qdty_eq (t1 t2:(qvar * nat)) : bool :=
(fst t1 =q= fst t2) && (snd t1 =? snd t2).
Notation "i '=qd=' j" := (qdty_eq i j) (at level 50).
Lemma qdty_eqb_eq : forall a b, a =qd= b = true -> a = b.
Proof.
intros. unfold qdty_eq in H.
destruct a. destruct b.
apply andb_true_iff in H. destruct H.
apply qty_eqb_eq in H.
apply Nat.eqb_eq in H0. simpl in *. subst. easy.
Qed.
Lemma qdty_eqb_neq : forall a b, a =qd= b = false -> a <> b.
Proof.
intros. unfold qdty_eq in H.
destruct a. destruct b.
apply andb_false_iff in H. destruct H.
apply qty_eqb_neq in H. intros R. inv R. easy.
apply Nat.eqb_neq in H.
intros R. inv R. contradiction.
Qed.
Lemma qdty_eq_reflect : forall r1 r2, reflect (r1 = r2) (qdty_eq r1 r2).
Proof.
intros.
destruct (r1 =qd= r2) eqn:eq1.
apply ReflectT.
apply qdty_eqb_eq in eq1.
assumption.
constructor.
apply qdty_eqb_neq in eq1.
assumption.
Qed.
(* a type for const values that cannot appear in a quantum circuit,
and storeister values that can appear in a guantum circuit. *)
Inductive btype := Nat : btype | FixedP : btype | Bl : btype.
Definition bty_eq (t1 t2:btype) : bool :=
match t1 with Nat => match t2 with Nat => true
| _ => false
end
| FixedP => match t2 with FixedP => true
| _ => false
end
| Bl => match t2 with Bl => true
| _ => false
end
end.
Notation "i '=b=' j" := (bty_eq i j) (at level 50).
Lemma bty_eqb_eq : forall a b, a =b= b = true -> a = b.
Proof.
intros. unfold bty_eq in H.
destruct a. destruct b. 1-3:easy.
destruct b. 1-3:easy.
destruct b. 1-3:easy.
Qed.
Lemma bty_eqb_neq : forall a b, a =b= b = false -> a <> b.
Proof.
intros. unfold bty_eq in H.
destruct a. destruct b. 1-3:easy.
destruct b. 1-3:easy.
destruct b. 1-3:easy.
Qed.
Lemma bty_eq_reflect : forall r1 r2, reflect (r1 = r2) (bty_eq r1 r2).
Proof.
intros.
destruct (r1 =b= r2) eqn:eq1.
apply ReflectT.
apply bty_eqb_eq in eq1.
assumption.
constructor.
apply bty_eqb_neq in eq1.
assumption.
Qed.
(* Variables can be C or Q mode. Q stands for quantum variables, while C stands for constants. *)
Inductive atype := C : atype | Q : atype.
Definition aty_eq (t1 t2:atype) : bool :=
match t1 with C => match t2 with C => true
| _ => false
end
| Q => match t2 with Q => true
| _ => false
end
end.
Notation "i '=a=' j" := (aty_eq i j) (at level 50).
Lemma aty_eqb_eq : forall a b, a =a= b = true -> a = b.
Proof.
intros. unfold aty_eq in H.
destruct a. destruct b. 1-2:easy.
destruct b. 1-2:easy.
Qed.
Lemma aty_eqb_neq : forall a b, a =a= b = false -> a <> b.
Proof.
intros. unfold aty_eq in H.
destruct a. destruct b. easy.
easy.
destruct b. easy. easy.
Qed.
Lemma aty_eq_reflect : forall r1 r2, reflect (r1 = r2) (aty_eq r1 r2).
Proof.
intros.
destruct (r1 =a= r2) eqn:eq1.
apply ReflectT.
apply aty_eqb_eq in eq1.
assumption.
constructor.
apply aty_eqb_neq in eq1.
assumption.
Qed.
(* A type for a variable in a program can be an Array type or a single value type. *)
Inductive typ :Type := TArray (a:atype) (b:btype) (n:nat) | TNor (a:atype) (b:btype).
Definition typ_eq (t1 t2:typ) : bool :=
match t1 with TArray a1 b1 n1 =>
match t2 with TArray a2 b2 n2 => (a1 =a= a2) && (b1 =b= b2) && (n1 =? n2)
| _ => false
end
| TNor a1 b1 => match t2 with TNor a2 b2 => (a1 =a= a2) && (b1 =b= b2)
| _ => false end
end.
Notation "i '=t=' j" := (typ_eq i j) (at level 50).
Lemma typ_eqb_eq : forall a b, a =t= b = true -> a = b.
Proof.
intros. unfold typ_eq in H.
destruct a. destruct b.
apply andb_true_iff in H.
destruct H.
apply andb_true_iff in H.
destruct H.
apply aty_eqb_eq in H.
apply bty_eqb_eq in H1.
bdestruct (n =? n0). subst. easy. inv H0.
inv H.
destruct b. inv H.
apply andb_true_iff in H.
destruct H.
apply aty_eqb_eq in H.
apply bty_eqb_eq in H0. subst. easy.
Qed.
Lemma typ_eqb_neq : forall a b, a =t= b = false -> a <> b.
Proof.
intros. unfold typ_eq in H.
destruct a. destruct b.
apply andb_false_iff in H.
destruct H.
apply andb_false_iff in H.
destruct H.
apply aty_eqb_neq in H. intros R. inv R. easy.
apply bty_eqb_neq in H. intros R. inv R. easy.
bdestruct (n =? n0). inv H. intros R. inv R. easy.
easy.
destruct b.
easy.
apply andb_false_iff in H.
destruct H.
apply aty_eqb_neq in H. intros R. inv R. easy.
apply bty_eqb_neq in H. intros R. inv R. easy.
Qed.
Lemma typ_eq_reflect : forall r1 r2, reflect (r1 = r2) (typ_eq r1 r2).
Proof.
intros.
destruct (r1 =t= r2) eqn:eq1.
apply ReflectT.
apply typ_eqb_eq in eq1.
assumption.
constructor.
apply typ_eqb_neq in eq1.
assumption.
Qed.
Inductive qop := nadd | nsub | nmul | fadd | fsub | fmul | qxor | ndiv | nmod | nfac | fdiv
| fndiv.
Definition qop_eq (t1 t2:qop) : bool :=
match (t1,t2) with (nadd,nadd) => true
| (nsub,nsub) => true
| (nmul,nmul) => true
| (fadd,fadd) => true
| (fsub,fsub) => true
| (fmul,fmul) => true
| (qxor,qxor) => true
| (ndiv,ndiv) => true
| (nmod,nmod) => true
| (nfac,nfac) => true
| (fdiv,fdiv) => true
| (fndiv,fndiv) => true
| _ => false
end.
Notation "i '=op=' j" := (qop_eq i j) (at level 50).
Lemma qop_eqb_eq : forall a b, a =op= b = true -> a = b.
Proof.
intros. unfold qop_eq in H.
destruct a. destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
Qed.
Lemma qop_eqb_neq : forall a b, a =op= b = false -> a <> b.
Proof.
intros. unfold qop_eq in H.
destruct a. destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
destruct b. 1-12:easy.
Qed.
Lemma qop_eq_reflect : forall r1 r2, reflect (r1 = r2) (qop_eq r1 r2).
Proof.
intros.
destruct (r1 =op= r2) eqn:eq1.
apply ReflectT.
apply qop_eqb_eq in eq1.
assumption.
constructor.
apply qop_eqb_neq in eq1.
assumption.
Qed.
Hint Resolve flag_eq_reflect aty_eq_reflect qty_eq_reflect qdty_eq_reflect bty_eq_reflect typ_eq_reflect qop_eq_reflect : bdestruct.
(* Make maps in Coq. *)
Module QvarType <: OrderedType.
Definition t := qvar.
Definition eq := @eq t.
Definition lt (x y : qvar) := match x with
L u =>
match y with L v => (u < v)
| G v => True
end
| G u =>
match y with G v => (u < v)
| L v => False
end
end.
Definition eq_refl := @eq_refl t.
Definition eq_sym := @eq_sym t.
Definition eq_trans := @eq_trans t.
Lemma lt_trans : forall x y z : t, lt x y -> lt y z -> lt x z.
Proof.
intros.
unfold lt in *.
destruct x. destruct y. destruct z. lia. lia. lia.
destruct y. destruct z. lia. lia. destruct z. lia. lia.
Qed.
Lemma lt_not_eq : forall x y : t, lt x y -> ~ eq x y.
Proof.
intros.
unfold lt,eq in *.
destruct x. destruct y. intros R. inv R. lia.
easy.
destruct y.
easy. intros R. inv R. lia.
Qed.
Definition compare : forall x y : t, Compare lt eq x y.
Proof.
intros.
destruct x. destruct y.
bdestruct (v <? v0).
apply LT. unfold lt. easy.
bdestruct (v =? v0).
apply EQ; unfold eq;auto.
apply GT;unfold lt. lia.
apply GT;unfold lt. lia.
destruct y.
apply LT. unfold lt. easy.
bdestruct (v <? v0).
apply LT. unfold lt. easy.
bdestruct (v =? v0).
apply EQ; unfold eq;auto.
apply GT;unfold lt. lia.
Defined.
Definition eq_dec : forall x y : t, {eq x y} + {~ eq x y}.
Proof.
intros; elim (compare x y); intro H; [ right | left | right ]; auto.
auto using lt_not_eq.
assert (~ eq y x); auto using lt_not_eq.
unfold eq in *. intros R. subst. contradiction.
Defined.
End QvarType.
Module QvarNatType <: OrderedType.
Definition t : Type := (qvar * nat).
Definition eq := @eq t.
Definition lt_q (x y : qvar) := match x with
L u =>
match y with L v => (u < v)
| G v => True
end
| G u =>
match y with G v => (u < v)
| L v => False
end
end.
Definition lt (x y : (qvar * nat)) :=
(lt_q (fst x) (fst y)) \/ (~ lt_q (fst x) (fst y)
/\ (((fst x = fst y) /\ snd x < snd y))).
Definition eq_refl := @eq_refl t.
Definition eq_sym := @eq_sym t.
Definition eq_trans := @eq_trans t.
Lemma lt_trans : forall x y z : t, lt x y -> lt y z -> lt x z.
Proof.
intros.
unfold lt,lt_q in *.
destruct x. destruct y. destruct z. simpl in *.
destruct q. destruct q0. destruct q1. simpl in *.
destruct H. destruct H0. left. lia.
destruct H0 as [X1 [X2 X3]]. inv X2. left. easy.
destruct H as [X1 [X2 X3]]. inv X2. destruct H0. left. easy.
destruct H as [B1 [B2 B3]]. inv B2.
right. split. easy. split. easy. lia.
destruct H. destruct H0. easy.
destruct H0 as [X1 [X2 X3]]. inv X2.
destruct H0. left. easy.
destruct H as [X1 [X2 X3]].
destruct H0 as [B1 [B2 B3]].
inv X2. inv B2.
destruct H. easy.
destruct H as [X1 [X2 X3]].
inv X2.
destruct q0. destruct q1.
left. easy.
destruct H. destruct H0. easy.
destruct H0 as [X1 [X2 X3]].
inv X2.
destruct H0. easy.
destruct H as [X1 [X2 X3]]. inv X2.
destruct q1.
destruct H. left. easy.
left. easy.
destruct H. destruct H0. left. lia.
destruct H0 as [X1 [X2 X3]]. inv X2.
left. easy.
destruct H0.
destruct H as [X1 [X2 X3]]. inv X2.
left. easy.
destruct H as [X1 [X2 X3]]. inv X2.
destruct H0 as [B1 [B2 B3]]. inv B2.
right. split. easy. split. easy. lia.
Qed.
Lemma lt_not_eq : forall x y : t, lt x y -> ~ eq x y.
Proof.
intros.
unfold lt,lt_q,eq in *.
destruct x. destruct y. simpl in *.
destruct q. destruct q0. simpl in *.
destruct H. intros R. inv R. lia.
destruct H as [X1 [X2 X3]].
inv X2. intros R. inv R. lia.
destruct H. easy.
destruct H as [X1 [X2 X3]]. inv X2.
destruct q0.
intros R. inv R.
destruct H. intros R. inv R. lia.
destruct H as [X1 [X2 X3]]. inv X2.
intros R. inv R. lia.
Qed.
Definition compare : forall x y : t, Compare lt eq x y.
Proof.
intros.
destruct x. destruct y.
destruct q. destruct q0.
bdestruct (v <? v0).
apply LT. unfold lt,lt_q.
simpl in *. left. easy.
bdestruct (v =? v0). subst.
bdestruct (n <? n0).
apply LT. unfold lt,lt_q. simpl in *.
right. split. lia. split. easy. easy.
bdestruct (n =? n0). subst.
apply EQ; unfold eq;auto.
apply GT;unfold lt,lt_q. simpl in *.
right. split. lia. split. easy. lia.
apply GT;unfold lt,lt_q. simpl in *.
left. lia.
apply GT;unfold lt,lt_q. simpl in *.
left. lia.
destruct q0.
apply LT;unfold lt,lt_q. simpl in *.
left. lia.
bdestruct (v =? v0). subst.
bdestruct (n <? n0).
apply LT. unfold lt,lt_q.
simpl in *.
right. split. lia. split. easy. easy.
bdestruct (n =? n0).
apply EQ; unfold eq;auto.
apply GT. unfold lt,lt_q.
simpl in *.
right. split. lia. split. easy. lia.
bdestruct (v <? v0).
apply LT. unfold lt,lt_q.
simpl in *. left. easy.
apply GT;unfold lt,lt_q. simpl in *.
left. lia.
Defined.
Definition eq_dec : forall x y : t, {eq x y} + {~ eq x y}.
Proof.
intros; elim (compare x y); intro H; [ right | left | right ]; auto.
auto using lt_not_eq.
assert (~ eq y x); auto using lt_not_eq.
unfold eq in *. intros R. subst. contradiction.
Defined.
End QvarNatType.
(* Basic element is a Var or a number. Number is represented as bitstring. *)
Inductive factor := Var (v:qvar)
| Num (t:btype) (n:nat -> bool).
(* the first m in Num represents the number of bits.
a value is represented as a natural number x. it means x / 2^m where m is the number of denominator. *)
Inductive cfac := Index (x:qvar) (v:factor) | Nor (v:factor).
(* qadd/qsub/qmul has the property as x = y op x, which is corresponding to
[y][x] -> [y][y op x] structure.
for qadd/qsub, x and y are both float numbers. For mult, x is a natural number while y is a float.
for comparator operations, both are floats. *)
Inductive cexp := clt (x:cfac) (y:cfac)
| ceq (x:cfac) (y:cfac)
| iseven (x:cfac).
(*x := y/n where x,n are a nat *)
(* x := y mod n where x,n are a nat *)
(* x := n! where x is a nat & n is nat *)
(* x := x / n where n is a natural number, x is a float. *)
(* x := y - z all natural and C type *)
(* x := y + z all natural and C type *)
(* x := y - z all natural and C type *)
(* x := y + z all natural and C type *)
(* x := y * z all natural and C type *)
(* z = x/v where x and v are natural numbers, z is float
x and v are both integers, but the final result in z must be a float < 1 *)
Inductive qexp :=
qinv (x:cfac)
| call (v:cfac) (f:fvar) (args: list cfac)
| qif (c:cexp) (e1:qexp) (e2:qexp)
| qfor (x:var) (n:cfac) (e:qexp)
| qseq (q1:qexp) (q2:qexp)
| skip
| init (x:cfac) (v:cfac)
| slrot (x:cfac) (* static rotation. *)
| unary (x:cfac) (aop:qop) (v:cfac)
| binapp (x:cfac) (aop:qop) (v1:cfac) (v2:cfac).
(*functions will do automatic inverse computation after a function is returned.
for each ret statement, there is a list of pairs of vars, and the left one is the global variables to return,
while the left one is the local variables. after a function call is returned,
it will store all the local variables to their correponding global variables, and then reverse the computation. *)
Notation "p1 ;; p2" := (qseq p1 p2) (at level 50) : exp_scope.
Definition func : Type := ( fvar * list (atype * btype * var) * list (typ * var) * qexp * cfac).
(* a function is a fun name, a starting block label, and a list of blocks, and the returned variable. *)
Definition prog : Type := (nat * list (typ * var) * list func * fvar * var).
(* a number of bits in FixedP and Nat
and a list of global vars, and a list of functions.
and the main function to call, and the final global var to write to. *)
(* The following relation defines the type system for expressions and instructions and functions. *)
(* Defining matching shifting stack. *)
Module BEnv := FMapList.Make QvarType.
Module BEnvFacts := FMapFacts.Facts (BEnv).
Definition benv := BEnv.t typ.
Definition empty_benv := @BEnv.empty typ.
Definition qupdate {A} (f : qvar -> A) (i : qvar) (x : A) :=
fun j => if j =q= i then x else f j.
Lemma qupdate_index_eq : forall {A} (f : qvar -> A) i b, (qupdate f i b) i = b.
Proof.
intros.
unfold qupdate.
bdestruct (i =q= i). easy. easy.
Qed.
Lemma qupdate_index_neq : forall {A} (f : qvar -> A) i j b, i <> j -> (qupdate f i b) j = f j.
Proof.
intros.
unfold qupdate.
bdestruct (j =q= i). subst. easy. easy.
Qed.
Lemma qupdate_same : forall {A} (f : qvar -> A) i b,
b = f i -> qupdate f i b = f.
Proof.
intros.
apply functional_extensionality.
intros.
unfold qupdate.
bdestruct (x =q= i); subst; reflexivity.
Qed.
Lemma qupdate_twice_eq : forall {A} (f : qvar -> A) i b b',
qupdate (qupdate f i b) i b' = qupdate f i b'.
Proof.
intros.
apply functional_extensionality.
intros.
unfold qupdate.
bdestruct (x =q= i); subst; reflexivity.
Qed.
Lemma qupdate_twice_neq : forall {A} (f : qvar -> A) i j b b',
i <> j -> qupdate (qupdate f i b) j b' = qupdate (qupdate f j b') i b.
Proof.
intros.
apply functional_extensionality.
intros.
unfold qupdate.
bdestruct (x =q= i); bdestruct (x =q= j); subst; easy.
Qed.
(*Function map consists with an argument list, an expression, a type environment for function body and return type. *)
Module FEnv := FMapList.Make Nat_as_OT.
Module FEnvFacts := FMapFacts.Facts (FEnv).
Definition fenv := FEnv.t (list (atype * btype * var) * list (typ * var) * qexp * benv * cfac).
Definition fenv_empty := @FEnv.empty (list (atype * btype * var) * list (typ * var) * qexp * benv * cfac).
Definition bind {A B} (a : option A) f : option B :=
match a with None => None | Some a => f a end.
Definition ret {A} (a : A) := Some a.
Notation "'do' X '<-' A '@' B" := (bind A (fun X => B)) (at level 200, X ident, A at level 100, B at level 200).
Definition typ_factor (bv:benv) (fc:factor) :=
match fc with Var x => do re <- BEnv.find x bv @
match re with TArray x y n => None
| TNor x y => Some (x,y)
end
| Num t n => Some (C,t)
end.
Definition typ_factor_full (bv:benv) (a:atype) (b:btype) (fc:factor) :=
match fc with Var x => do re <- BEnv.find x bv @
match re with TArray x y n => None
| TNor x y => if (a =a= x) && (y =b= b) then Some (x,y) else None
end
| Num t n => if (a =a= C) && (b =b= t) then Some (C,t) else None
end.
Definition type_factor (bv:benv) (fc:cfac) :=
match fc with Index x ic =>
do re <- BEnv.find x bv @
match re with TArray a b n =>
do ta <- typ_factor_full bv C Nat ic @ Some (a,b)
| TNor x y => None
end
| Nor c => typ_factor bv c
end.
Definition type_factor_full (bv:benv) (a:atype) (t:btype) (fc:cfac) :=
match fc with Index x ic =>
do re <- BEnv.find x bv @
match re with TArray a' b n =>
if (a =a= a') && (b =b= t) then
do ta <- typ_factor_full bv C Nat ic @ Some (a,t)
else None
| TNor x y => None
end
| Nor c => typ_factor_full bv a t c
end.
(* C \subseteq Q *)
Definition meet_atype (a1 a2: atype) :=
match a1 with Q => Q | C => a2 end.
Definition meet_btype (b1 b2: btype) :=
if b1 =b= b2 then Some b1 else None.
Definition meet_type (t1 t2 : (atype * btype)) :=
match t1 with (a1,b1) =>
match t2 with (a2,b2) =>
do bty <- meet_btype b1 b2 @ ret (meet_atype a1 a2, bty)
end
end.
Definition type_cexp (benv:benv) (c:cexp) :=
match c with clt x y =>
do re1 <- type_factor benv x @
do re2 <- type_factor benv y @ (meet_type re1 re2)
| ceq x y =>
do re1 <- type_factor benv x @
do re2 <- type_factor benv y @ (meet_type re1 re2)
| iseven x => type_factor_full benv C Nat x
end.
(*
a_nat2fb is to turn a nat-> bool value to nat.
*)
Definition a_nat2fb f n := natsum n (fun i => Nat.b2n (f i) * 2^i).
Lemma a_nat2fb_scope : forall n f, a_nat2fb f n < 2^n.
Proof.
induction n;intros;simpl.
unfold a_nat2fb. simpl. lia.
specialize (IHn f).
unfold a_nat2fb in *. simpl.
destruct (f n). simpl. lia.
simpl. lia.
Qed.
Definition is_q (t:typ) : bool :=
match t with TArray Q _ _ | TNor Q _ => true
| _ => false
end.
Definition is_c (t:typ) : bool :=
match t with TArray C _ _ | TNor C _ => true
| _ => false
end.
Definition get_var (c:cfac) : option qvar :=
match c with Nor (Var x) => Some x
| Nor (Num b x) => None
| Index x y => Some x
end.
Definition get_index (c:cfac) : option factor :=
match c with Nor x => None
| Index x y => Some y
end.
Definition get_ct (c:typ) :=
match c with TArray x y n => y
| TNor x y => y
end.
(*The semantics of QLLVM.
A store is impelemented as a a list of history values, and the top in the list is the current value.
We kept history values to do inv. *)
Module Store := FMapList.Make QvarNatType.
Module StoreFacts := FMapFacts.Facts (Store).
Definition store : Type := Store.t (list (nat -> bool)).
Definition empty_store := @Store.empty (list (nat -> bool)).
Inductive value {A:Type} := Value (x:A) | Error.
Definition sem_factor (size:nat) (r:store) (fc:factor) :=
match fc with Var x => do vals <- (Store.find (x,0) r) @ (hd_error vals)
| Num b n => if b =b= Bl then Some ( (cut_n n 1))
else if b =b= Nat then Some ( (cut_n n size))
else Some (fbrev size (cut_n n size))
end.
Definition sem_cfac (smap:qvar -> nat) (size:nat) (store:store) (fc:cfac)
: option (@value (nat -> bool)) :=
match fc with Index x n => do v <- (sem_factor size store n) @
if (a_nat2fb v size) <? smap x then
do l <- Store.find (x,a_nat2fb v size) store @
do val <- (hd_error l) @ Some (Value val)
else Some Error
| Nor x => do val <- sem_factor size store x @ Some (Value val)
end.
Definition get_size (size:nat) (t:btype) := if t =b= Bl then 1 else size.
Definition sem_cexp (smap:qvar -> nat) (bv:benv) (size:nat) (store:store) (ce:cexp) : option (@value bool) :=
match ce with clt x y =>
do t <- type_factor bv x @
do v1 <- (sem_cfac smap size store x) @
do v2 <- (sem_cfac smap size store y) @
match v1 with (Value v1') =>
match v2 with (Value v2') =>
Some
(Value (a_nat2fb v1' (get_size size (snd t)) <? a_nat2fb v2' (get_size size (snd t))))
| _ => Some Error
end
| _ => Some Error
end
| ceq x y =>
do t <- type_factor bv x @
do v1 <- (sem_cfac smap size store x) @
do v2 <- (sem_cfac smap size store y) @
match v1 with (Value v1') =>
match v2 with (Value v2') =>
Some (Value (a_nat2fb v1' (get_size size (snd t)) =? a_nat2fb v2' (get_size size (snd t))))
| _ => Some Error
end
| _ => Some Error
end
| iseven x =>
do t <- type_factor bv x @
do v1 <- (sem_cfac smap size store x) @
match v1 with Value v1' =>
Some (Value ((a_nat2fb v1' (get_size size (snd t))) mod 2 =? 0))
| _ => Some Error
end
end.
Definition bv_store_sub (smap : qvar -> nat) (bv:benv) (st:store) :=
forall x i, BEnv.In x bv -> i < smap x -> (exists v, Store.MapsTo (x,i) v st /\ length v > 0).
Definition bv_store_gt_0 (smap : qvar -> nat) (bv:benv) :=
forall x, BEnv.In x bv -> 0 < smap x.
Definition sub_def (f1 f2:nat -> bool) (size:nat) :=
if a_nat2fb f1 size <? a_nat2fb f2 size then (a_nat2fb f1 size + 2^size - a_nat2fb f2 size) mod 2^size
else (a_nat2fb f1 size + a_nat2fb f2 size) mod 2^size.
Fixpoint init_store_n (r:store) (x:qvar) (n:nat) : store :=
match n with 0 => r
| S m => Store.add (x,m) ([(nat2fb 0)]) (init_store_n r x m)
end.
Definition get_type_num (t:typ) :=
match t with TArray x y n => n
| TNor x y => 1
end.
Fixpoint init_store_args (r:store) (l:list (atype * btype * var)) (vl: list (nat -> bool)) : option store :=
match l with [] => Some r
| ((a,b,x)::xl) =>
match vl with [] => None
| (v::vl') => init_store_args (Store.add (L x,0) ([v]) r) xl vl'
end
end.
Fixpoint init_store (r:store) (l:list (typ * var)) : option store :=
match l with [] => Some r
| ((t,x)::xl) => if get_type_num t =? 0 then None else
do new_store <- init_store r xl @
ret (init_store_n new_store (L x) (get_type_num t))
end.
Fixpoint gen_smap_args (l:list (atype * btype * var)) (smap: qvar -> nat) :=
match l with [] => smap
| ((a,b,x)::xl) => match gen_smap_args xl smap with new_map =>
(qupdate new_map (L x) 1) end
end.
Fixpoint gen_smap_l (l:list (typ * var)) (smap: qvar -> nat) :=
match l with [] => smap
| ((t,x)::xl) => match gen_smap_l xl smap with new_map =>
(qupdate new_map (L x) (get_type_num t)) end
end.
Lemma init_store_gt_0 : forall l r r', init_store r l = Some r' ->
(forall t x, In (t,x) l -> 0 < get_type_num t).
Proof.
induction l; intros; simpl in *.
inv H0.
destruct H0.
destruct a. inv H0.
bdestruct (get_type_num t =? 0). inv H. lia.
destruct a.
bdestruct (get_type_num t0 =? 0). inv H.
destruct (init_store r l) eqn:eq1. inv H.
specialize (IHl r s eq1). apply IHl with (x := x). easy. inv H.
Qed.
Lemma store_find_add : forall k v m,
@Store.find ((list (nat -> bool))) k (Store.add k v m) = Some v.
Proof.
intros.
apply Store.find_1.
apply Store.add_1.
easy.
Qed.
Lemma store_mapsto_add1 : forall k v1 v2 s,
@Store.MapsTo ((list (nat -> bool))) k v1 (Store.add k v2 s) -> v1 = v2.
Proof.
intros.
apply Store.find_1 in H.
rewrite store_find_add in H.
inversion H.
reflexivity.
Qed.
Lemma init_store_n_0 : forall n i r x, i < n -> Store.MapsTo (x,i) ([nat2fb 0]) (init_store_n r x n).
Proof.
induction n; intros; simpl in *. lia.
bdestruct (i =? n). subst.
apply Store.add_1. easy.
apply Store.add_2. intros R. inv R. lia.
apply IHn. lia.
Qed.
Lemma init_store_n_neq : forall n i r x y v, x <> y -> Store.MapsTo (x,i) v r ->
Store.MapsTo (x,i) v (init_store_n r y n).
Proof.
induction n; intros; simpl in *. easy.
apply Store.add_2. intros R. inv R. easy.
apply IHn. easy. easy.
Qed.
Definition no_zero (t:typ) := match t with TArray x y n => if n =? 0 then false else true
| TNor x y => true
end.
Fixpoint gen_env (l:list (typ * var)) (bv:benv) : option benv :=
match l with [] => Some bv
| ((t,x)::xl) =>
do new_env <- gen_env xl bv @
if no_zero t then Some (BEnv.add (L x) t new_env) else None
end.
Fixpoint gen_env_l (l:list (atype * btype * var)) (bv:benv) : option benv :=
match l with [] => Some bv
| ((a,t,x)::xl) =>
do new_env <- gen_env_l xl bv @ Some (BEnv.add (L x) (TNor a t) new_env)
end.
Fixpoint gen_genv (l:list (typ * var)) : option benv :=
match l with [] => Some empty_benv
| ((t,x)::xl) =>
do new_env <- gen_genv xl @
if no_zero t then Some (BEnv.add (G x) t new_env) else None
end.
Lemma init_store_bv_sub : forall l r r' bv bv' smap smap', init_store r l = Some r' ->
gen_env l bv = Some bv' -> gen_smap_l l smap = smap' -> bv_store_sub smap bv r ->
bv_store_sub smap' bv' r'.
Proof.
induction l; intros; simpl in *.