-
Notifications
You must be signed in to change notification settings - Fork 3
/
LocusProof.v
executable file
·2232 lines (2113 loc) · 96 KB
/
LocusProof.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 Complex.
Require Import SQIR.
Require Import VectorStates UnitaryOps Coq.btauto.Btauto Coq.NArith.Nnat Permutation.
Require Import Dirac.
Require Import QPE.
Require Import BasicUtility.
Require Import Classical_Prop.
Require Import MathSpec.
Require Import QafnySyntax.
Require Import LocusDef.
Require Import LocusKind.
Require Import LocusType.
Require Import LocusSem.
Require Import LocusTypeProof.
(**********************)
(** Unitary Programs **)
(**********************)
Require Import Coq.FSets.FMapList.
Require Import Coq.FSets.FMapFacts.
Require Import Coq.Structures.OrderedTypeEx.
Declare Scope pexp_scope.
Delimit Scope pexp_scope with pexp.
Local Open Scope pexp_scope.
Local Open Scope nat_scope.
(*
Definition locus :Set := (var * nat * nat).
Definition atpred_elem :Type := (list locus * se_type).
Definition atpred := list atpred_elem.
*)
(*
TODO: define apply operation with few different applications:
1: switching: mimicing the locus position switching equivalence relation.
2. masking: mimicing the partial measurement.
3. oracle function application using oracle semantics.
4. if conditional as entanglement.
5. H/QFT state prepreation.
*)
(*
Inductive sval := ST (x:state_elem) | SV (s:locus)
| Mask (y:sval) (u:nat) (z:aexp) | AppA (x:exp) (y:sval)
| FSL (e:sval) (l:locus) (s:nat)
| SSL (e:sval) (a:sval) (b:bexp) (l1:locus) (l2:locus).
*)
Inductive sval := SV (s:locus) | Frozen (b:bexp) (s:sval) (s:sval)
| Unfrozen (n:nat) (b:bexp) (s:sval) | FM (x:var) (n:nat) (rv:R * nat) (s:sval).
Definition qpred_elem : Type := (sval * state_elem).
Definition cpred := list cbexp.
Definition qpred : Type := list qpred_elem.
Definition fresh (l:nat) := l +1.
Inductive qpred_equiv {rmax:nat} : qpred -> qpred -> Prop :=
| qpred_id : forall S, qpred_equiv S S
(* | qpred_empty : forall v S, qpred_equiv ((SV nil,v)::S) S *)
| qpred_comm :forall a1 a2, qpred_equiv (a1++a2) (a2++a1)
| qpred_ses_assoc: forall s v S S', qpred_equiv S S' -> qpred_equiv ((s,v)::S) ((s,v)::S')
(*
| qpred_ses_eq: forall s s' v S, ses_eq s s' -> qpred_equiv ((SV s,v)::S) ((SV s',v)::S)
| qpred_sub: forall x v n u a, ses_len x = Some n -> @state_same rmax n v u -> qpred_equiv ((SV x,v)::a) ((SV x,u)::a)
| qpred_mut: forall l1 l2 n a n1 b n2 v u S, ses_len l1 = Some n -> ses_len ([a]) = Some n1 -> ses_len ([b]) = Some n2 ->
mut_state n n1 n2 v u -> qpred_equiv ((SV (l1++(a::b::l2)),v)::S) ((SV (l1++(b::a::l2)),u)::S)
*)
| qpred_merge: forall x n v y u a vu, ses_len x = Some n ->
@times_state rmax n v u vu -> qpred_equiv ((SV x,v)::((SV y,u)::a)) ((SV (x++y),vu)::a)
| qpred_split: forall x n y v v1 v2 a, ses_len x = Some n ->
@split_state rmax n v (v1,v2) -> qpred_equiv ((SV (x++y),v)::a) ((SV x,v1)::(SV y,v2)::a).
Axiom qpred_equiv_cong: forall rmax a P a1 P', @qpred_equiv rmax (a::P) (a1::P')
-> @qpred_equiv rmax ([a]) ([a1]) /\ @qpred_equiv rmax P P'.
Fixpoint sval_subst_c t x v :=
match t with SV s => SV (subst_locus s x v)
| Frozen b s s' => Frozen (subst_bexp b x v) (sval_subst_c s x v) (sval_subst_c s' x v)
| Unfrozen n b s => Unfrozen n (subst_bexp b x v) (sval_subst_c s x v)
| FM y n rv s => FM y n rv (sval_subst_c s x v)
end.
Definition qelem_subst_c (t: qpred_elem) x v := (sval_subst_c (fst t) x v,snd t).
Definition pred_subst_c (t: cpred * qpred) x v :=
(List.map (fun a => subst_cbexp a x v) (fst t), List.map (fun a => qelem_subst_c a x v) (snd t)).
Definition sublist (l:list var) (env:aenv) := Forall (fun b => AEnv.In b env) l.
Fixpoint freeSesSV (a:sval) := match a with SV s => [s]
| Frozen b s s' => freeSesSV s'
| Unfrozen n b s => freeSesSV s
| FM x n rv s => freeSesSV s
end.
Definition freeSesQPred (l:qpred) := List.fold_right (fun b a => freeSesSV (fst b)++a) nil l.
Definition freeSesPred (a:cpred * qpred) := (freeSesQPred (snd a)).
Inductive subst_ses_sval : sval -> locus -> sval -> sval -> Prop :=
subst_ses_svt : forall x v, subst_ses_sval (SV x) x v v
| subst_ses_svf : forall x y v, x <> y -> subst_ses_sval (SV y) x v v
| subst_ses_unf : forall x v s n b v', subst_ses_sval s x v v' -> subst_ses_sval (Unfrozen n b s) x v (Unfrozen n b v')
| subst_ses_fm : forall x v s y n rv v', subst_ses_sval s x v v' -> subst_ses_sval (FM y n rv s) x v (FM y n rv v').
Inductive subst_ses_qpred : qpred -> locus -> sval -> qpred -> Prop :=
subst_ses_empty: forall x v, subst_ses_qpred nil x v nil
| subst_ses_many: forall a b x v a' l l', subst_ses_sval a x v a' -> subst_ses_qpred l x v l'
-> subst_ses_qpred ((a,b)::l) x v ((a',b)::l').
Inductive resolve_frozen : qpred -> qpred -> Prop :=
| resolve_frozen_many_1 : forall l q, resolve_frozen ([(SV l,q)]) ([(SV l,q)])
| resolve_frozen_many_2 : forall l l1 m f f' fc b n n1, @eval_bexp ([(l++l1,Cval m f)]) b ([(l++l1,Cval m f')])
-> ses_len l = Some n -> ses_len l1 = Some n1 ->
mut_state 0 n n1 (Cval (fst (grab_bool f' m n)) (snd (grab_bool f' m n))) fc
-> resolve_frozen ((Frozen b (SV l) (SV l1),Cval m f)::nil) ((SV l1,fc)::nil).
Inductive resolve_unfrz : qpred -> qpred -> Prop :=
| resolve_unfrz_many_1 : forall l q l' q', resolve_unfrz ((SV l,q)::[(SV l',q')]) ((SV l,q)::[(SV l',q')])
| resolve_unfrz_many_2 : forall l l1 q m f f' fc b n n1, ses_len l = Some n -> ses_len l1 = Some n1 ->
eval_bexp ([(l++l1,Cval m f)]) b ([(l++l1,Cval m f')]) -> assem_bool n n1 m f' q fc ->
resolve_unfrz ((Unfrozen n (BNeg b) (SV (l++l1)),Cval m f)::[((Unfrozen n b (SV (l++l1)),q))]) ([(SV (l++l1),fc)]).
Fixpoint ses_in (s:locus) (l:list locus) :=
match l with nil => False
| (a::xl) => ((ses_eq a s) \/ (ses_in s xl))
end.
Fixpoint ses_sublist (s:list locus) (l:list locus) :=
match s with nil => True
| (a::xl) => ((ses_in a l) \/ (ses_sublist xl l))
end.
Definition cpred_check (l:cpred) (env:aenv) := Forall (fun b => sublist (freeVarsCBexp b) env) l.
Inductive sval_check : locus -> sval -> Prop :=
sval_check_sv: forall s, sval_check s (SV s)
| sval_check_frozen: forall sa b s s', sval_check sa s' -> sval_check sa (Frozen b s s')
| sval_check_unfrozen: forall sa n b s, sval_check sa s -> sval_check sa (Unfrozen n b s)
| sval_check_fm: forall sa x n rv s, sval_check sa s -> sval_check sa (FM x n rv s).
Inductive qpred_check : type_map -> qpred -> Prop :=
| qpred_check_empty: qpred_check nil nil
| qpred_check_many: forall sa s t v T Sa, sval_check sa s -> qpred_check T Sa
-> type_state_elem_same t v -> qpred_check ((sa,t)::T) ((s,v)::Sa).
Definition pred_check (env:aenv) (T:type_map) (l:cpred*qpred) := cpred_check (fst l) env /\ qpred_check T (snd l).
(*Definition class_bexp (b:bexp) := match b with CB a => Some a | _ => None end.*)
Inductive qmodel : qstate -> qpred -> Prop :=
model_empty : qmodel nil nil
| model_many : forall n s l v v' P, ses_len l = Some n -> match_value n v v'
-> qmodel s P -> qmodel ((l,v)::s) (((SV l), v')::P).
Inductive eval_cabexp : stack -> bexp -> Prop :=
| ceq_asem : forall s x y r1 r2 n1 n2, eval_aexp s x (r1,n1)
-> eval_aexp s y (r2,n2) -> (r1 = r2) -> n1 = n2 -> eval_cabexp s (CB (CEq x y))
| clt_asem : forall s x y r1 r2 n1 n2, eval_aexp s x (r1,n1) -> eval_aexp s y (r2,n2)
-> r1 = r2 -> n1 < n2 -> eval_cabexp s (CB (CLt x y))
| bneq_asem: forall s e, eval_cabexp s e -> eval_cabexp s (BNeg e).
Definition cmodel (s:stack) (l:cpred) : Prop := Forall (fun b => eval_cabexp s (CB b)) l.
Definition model (s:state) (P:cpred * qpred) := cmodel (fst s) (fst P) /\ qmodel (snd s) (snd P).
Inductive imply (rmax:nat) : cpred * qpred -> cpred * qpred -> Prop :=
| imply_cpred : forall W W' P, (forall s, cmodel s W -> cmodel s W') -> imply rmax (W,P) (W',P)
| imply_qpred: forall W P Q, @qpred_equiv rmax P Q -> imply rmax (W,P) (W,Q).
Definition type_check_proof (rmax :nat) (t:atype) (env:aenv) (T T':type_map) (P Q:cpred * qpred) e :=
pred_check env T P /\ @locus_system rmax t env T e T' /\ pred_check env T' Q.
Inductive simple_qpred_elem : qpred_elem -> Prop :=
simple_qpred_elem_rule : forall l v, simple_qpred_elem (SV l,v).
Definition simple_qpred (Q: qpred) := Forall (fun a => simple_qpred_elem a) Q.
Inductive simp_pred_elem: sval -> state_elem -> (sval * state_elem) -> Prop :=
| simp_pred_mea: forall x n s va va' r v, @pick_mea n va (r,v)
-> build_state_ch n v va = Some va' -> simp_pred_elem (FM x n (r,v) (SV s)) va (SV s,va').
(*
Inductive simp_pred : cpred -> qpred -> (cpred * qpred) -> Prop :=
| simp_pred_empty : forall W, simp_pred W nil (W,nil)
| simp_pred_many : forall W W' W'' P P' s v a,
simp_pred W P (W',P') -> simp_pred_elem W' s v (W'',a) -> simp_pred W ((s,v)::P) (W'',a::P').
*)
Inductive triple {rmax:nat} :
atype -> aenv -> type_map -> cpred*qpred -> pexp -> cpred*qpred -> Prop :=
| triple_frame: forall q env T T1 T' W W' P Q e R,
type_check_proof rmax q env T T1 (W,P) (W',Q) e ->
(* fv_pexp env e l -> sub_qubits l (dom_to_ses(dom T))
-> sub_qubits (dom_to_ses (freeSesQPred R)) (dom_to_ses(dom T'))
-> dis_qubits (dom_to_ses(dom T)) (dom_to_ses(dom T'))
*)
triple q env T (W,P) e (W',Q) -> triple q env (T++T') (W,P++R) e (W',Q++R)
| triple_con_1: forall q env T T1 P P' Q e, type_check_proof rmax q env T T1 P' Q e ->
imply rmax P P' -> triple q env T P' e Q -> triple q env T P e Q
| triple_con_2: forall q env T T1 P Q Q' e, type_check_proof rmax q env T T1 P Q' e ->
imply rmax Q' Q -> pred_check env T1 Q -> triple q env T P e Q' -> triple q env T P e Q
| skip_pf: forall q env T P, triple q env T P PSKIP P
| let_c_pf: forall q env T T1 P x a v e Q,
~ AEnv.In x env ->
simp_aexp a = Some v ->
type_check_proof rmax q env T T1 P Q (subst_pexp e x v) ->
triple q env T P (subst_pexp e x v) Q -> triple q env T P (Let x a e) Q
| let_m_pf: forall q env T T1 W P x a e Q,
type_check_proof rmax q (AEnv.add x (Mo MT) env) T T1 ((CEq (BA x) a)::W,P) ((CEq (BA x) a)::W,Q) e ->
type_aexp env a (Mo MT,nil) -> ~ AEnv.In x env ->
triple q (AEnv.add x (Mo MT) env) T ((CEq (BA x) a)::W,P) e ((CEq (BA x) a)::W,Q)
-> triple q env T (W,P) (Let x (AE a) e) (W,Q)
| let_q_pf: forall env T T1 W P PM x v y n r' v' l e Q,
type_check_proof rmax CT (AEnv.add x (Mo MT) env) ((l, CH) :: T) T1
((CEq (BA x) (MNum r' v'))::W,PM::P) ((CEq (BA x) (MNum r' v'))::W,Q) e ->
AEnv.MapsTo y (QT n) env -> ~ AEnv.In x env ->
simp_pred_elem (FM x n (r',v') (SV l)) v PM ->
triple CT (AEnv.add x (Mo MT) env) ((l,CH)::T) ((CEq (BA x) (MNum r' v'))::W,PM::P) e ((CEq (BA x) (MNum r' v'))::W,Q)
-> triple CT env (((y,BNum 0,BNum n)::l,CH)::T) (W,(SV ((y,BNum 0,BNum n)::l),v)::P) (Let x (Meas y) e) (W,Q)
| appu_nor_pf : forall q env W l r b e ra ba, eval_nor rmax env l r b e = Some (ra,ba) ->
triple q env ([(l,TNor)]) (W,([(SV (l),Nval r b)])) (AppU l e) (W, ([(SV (l),Nval ra ba)]))
| appu_ch_pf : forall q env W l l1 m b e ba, eval_ch rmax env l m b e = Some ba ->
triple q env ([(l++l1,CH)]) (W,([(SV (l++l1),Cval m b)])) (AppU l e) (W, ([(SV (l++l1),Cval m ba)]))
| apph_nor_pf: forall q env W p a r b n, @simp_varia env p a -> ses_len ([a]) = Some n ->
triple q env ([([a], TNor)]) (W,([(SV ([a]),Nval r b)])) (AppSU (RH p)) (W, ([(SV ([a]),(Hval (eval_to_had n b)))]))
| apph_had_pf: forall q env W p a b n, @simp_varia env p a -> ses_len ([a]) = Some n ->
triple q env ([([a], THad)]) (W,([(SV ([a]),Hval b)])) (AppSU (RH p)) (W, ([(SV ([a]),(Nval C1 (eval_to_nor n b)))]))
| if_c_t : forall q env T T1 P Q b e, type_check_proof rmax q env T T1 P Q e -> simp_bexp b = Some true ->
triple q env T P e Q -> triple q env T P (If b e) Q
| if_c_f : forall q env T P b e, simp_bexp b = Some false -> triple q env T P (If b e) P
| if_q : forall q env W W' P P' P'' Q Pa Qa Qa' b e n l l1, type_bexp env b (QT n,l) ->
type_check_proof rmax MT env ([(l1,CH)]) ([(l1,CH)]) (W,P'') (W',Q) e -> ses_len l = Some n ->
subst_ses_qpred P (l++l1) (Frozen b (SV l) (SV l1)) P' -> resolve_frozen P' P'' ->
subst_ses_qpred P (l++l1) (Unfrozen n (BNeg b) (SV (l++l1))) Pa ->
simple_qpred Q -> subst_ses_qpred Q l1 (Unfrozen n b (SV (l++l1))) Qa -> resolve_unfrz (Pa++Qa) Qa' ->
triple MT env ([(l1,CH)]) (W,P'') e (W',Q) -> triple q env ([(l++l1,CH)]) (W,P) (If b e) (W',Qa')
| for_pf_f : forall q env T x l h b p P, h <= l -> triple q env T P (For x (Num l) (Num h) b p) P
| for_pf : forall q env T x l h b p P, ~ AEnv.In x env -> l < h ->
(forall i, l <= i < h -> type_check_proof rmax q env (subst_type_map T x i) (subst_type_map T x (i+1))
(pred_subst_c P x i) (pred_subst_c P x (i+1)) (If (subst_bexp b x i) (subst_pexp p x i))) ->
(forall i, l <= i < h ->
triple q env (subst_type_map T x i) (pred_subst_c P x i)
(If (subst_bexp b x i) (subst_pexp p x i)) (pred_subst_c P x (i+1))) ->
triple q env (subst_type_map T x l) (pred_subst_c P x l) (For x (Num l) (Num h) b p) (pred_subst_c P x h)
| seq_pf: forall q env T T1 T2 P R Q e1 e2,
type_check_proof rmax q env T T1 P R e1 ->
type_check_proof rmax q env T1 T2 R Q e2 ->
triple q env T P e1 R -> triple q env T1 R e2 Q -> triple q env T P (PSeq e1 e2) Q.
Lemma resolve_frozen_simple: forall P a v, resolve_frozen P ([(a,v)]) -> (exists l, a = SV l).
Proof.
intros. inv H. exists l. easy. exists l1. easy.
Qed.
Lemma env_state_eq_dom: forall tenv s, env_state_eq tenv s -> (dom tenv) = dom s.
Proof.
intros. induction H; try easy.
simpl in *. rewrite IHenv_state_eq. easy.
Qed.
Lemma dom_con {A:Type}: forall a (b:list (locus*A)), dom (a::b) = (fst a)::(dom b).
Proof.
intros. unfold dom in *. simpl in *. destruct a; try easy.
Qed.
Lemma qpred_check_length_same : forall T P, qpred_check T P -> length T = length P.
Proof.
intros. induction H; try easy. simpl in *. rewrite IHqpred_check. easy.
Qed.
Lemma qmodel_shrink: forall q1 q2 P Q, length q1 = length P -> qmodel (q1++q2) (P ++ Q) -> qmodel q1 P.
Proof.
intros. remember (q1++q2) as q. remember (P++Q) as C.
generalize dependent q1. generalize dependent P. induction H0; intros;simpl in *.
symmetry in HeqC. symmetry in Heqq. apply app_eq_nil in HeqC as [X1 X2]. apply app_eq_nil in Heqq as [X3 X4]. subst.
constructor.
destruct P0. destruct q1. simpl in *. constructor. simpl in *. easy.
destruct q1. simpl in *. easy. simpl in *. inv HeqC. inv Heqq. apply (model_many n); try easy.
apply IHqmodel; try easy. lia.
Qed.
Lemma qmodel_shrink_1: forall q1 q2 P Q, length q1 = length P -> qmodel (q1++q2) (P ++ Q) -> qmodel q2 Q.
Proof.
intros. remember (q1++q2) as q. remember (P++Q) as C.
generalize dependent q1. generalize dependent P.
generalize dependent q2. generalize dependent Q.
induction H0; intros;simpl in *.
symmetry in HeqC. symmetry in Heqq. apply app_eq_nil in HeqC as [X1 X2]. apply app_eq_nil in Heqq as [X3 X4]. subst.
constructor.
destruct P0. destruct q1. simpl in *. destruct q2. inv Heqq. destruct Q. easy.
inv HeqC. inv Heqq. apply (model_many n); try easy. simpl in *. easy.
destruct q1. simpl in *. easy. inv HeqC. inv Heqq. simpl in *.
apply IHqmodel with (P := P0) (q4 := q1); try easy. lia.
Qed.
Lemma qmodel_combine: forall q1 q2 P Q, qmodel q1 P -> qmodel q2 Q -> qmodel (q1++q2) (P++Q).
Proof.
intros. induction H. simpl in *. easy.
simpl in *. apply (model_many n); try easy.
Qed.
Lemma qmodel_app: forall q P Q, qmodel q (P++Q) -> exists q1 q2, q=q1++q2 /\ length q1 = length P.
Proof.
intros. remember (P++Q) as Qa. generalize dependent P. generalize dependent Q. induction H; intros;simpl in *.
exists nil,nil. subst.
symmetry in HeqQa. apply app_eq_nil in HeqQa as [X1 X2]. subst.
simpl. easy.
destruct P0. destruct Q. simpl in *. easy. simpl in *. inv HeqQa.
specialize (IHqmodel Q nil). simpl in *.
assert (Q = Q) by easy. apply IHqmodel in H2 as [q1 [q2 [X1 X2]]].
apply length_zero_iff_nil in X2. subst. simpl in *.
exists nil. exists ((l,v)::q2). simpl in *. easy.
inv HeqQa.
specialize (IHqmodel Q P0). simpl in *.
assert (P0 ++ Q = P0 ++ Q) by easy.
apply IHqmodel in H2 as [q1 [q2 [X1 X2]]]. subst.
exists ((l, v) :: q1), q2. simpl in *. rewrite X2. easy.
Qed.
Lemma simple_qpred_shrink_l: forall P Q, simple_qpred (P++Q) -> simple_qpred P.
Proof.
intros. unfold simple_qpred in *.
remember (P++Q) as A. generalize dependent P. generalize dependent Q. induction H; intros;simpl in *.
symmetry in HeqA. apply app_eq_nil in HeqA. destruct HeqA;subst.
constructor. destruct P. simpl in *. constructor.
inv HeqA. constructor; try easy. apply (IHForall Q). easy.
Qed.
Lemma simple_qpred_shrink_r: forall P Q, simple_qpred (P++Q) -> simple_qpred Q.
Proof.
intros. unfold simple_qpred in *.
remember (P++Q) as A. generalize dependent P. generalize dependent Q. induction H; intros;simpl in *.
symmetry in HeqA. apply app_eq_nil in HeqA. destruct HeqA;subst.
constructor. destruct P. simpl in *; subst.
constructor; try easy. inv HeqA. eapply IHForall. easy.
Qed.
Lemma simple_qpred_combine: forall P Q, simple_qpred P -> simple_qpred Q -> simple_qpred (P++Q).
Proof.
intros. induction P; intros; simpl in *. easy.
inv H. constructor; try easy. apply IHP. easy.
Qed.
Definition singleTon (P: qpred) := exists s v, P = ([(s,v)]).
Lemma qpred_equiv_single_same: forall rmax P P', @qpred_equiv rmax P P' -> singleTon P -> singleTon P' -> P = P'.
Proof.
intros. induction H; try easy.
unfold singleTon in *. destruct H0 as [s1 [v1 X1]]. destruct H1 as [s2 [v2 X2]].
destruct a1. simpl in *. subst. inv X2. easy.
simpl in *. inv X1. apply app_eq_nil in H1. destruct H1; subst. simpl in *. easy.
unfold singleTon in *. destruct H0 as [s1 [v1 X1]]. destruct H1 as [s2 [v2 X2]].
inv X1. inv X2. easy.
unfold singleTon in *. destruct H0 as [s1 [v1 X1]]. destruct H1 as [s2 [v2 X2]].
inv X1.
unfold singleTon in *. destruct H0 as [sa [va X1]]. destruct H1 as [sv [vb X2]].
inv X2.
Qed.
Lemma qpred_state_consist: forall rmax T q P P', env_state_eq T q -> qmodel q P
-> qpred_check T P -> qpred_check T P' -> @qpred_equiv rmax P P' -> qmodel q P'.
Proof.
induction T; intros; simpl in *; try easy.
inv H. inv H2. constructor.
inv H. inv H1. inv H0. inv H2.
apply qpred_equiv_cong in H3. destruct H3 as [A1 A2].
apply qpred_equiv_single_same in A1 as X1; try easy. inv X1.
apply IHT with (q := l2) in A2; try easy.
apply (model_many n); try easy.
unfold singleTon in *. exists (SV s), v. easy.
exists s0,v0. easy.
Qed.
Lemma qpred_check_consist: forall T T' P, simple_qpred P -> qpred_check T P -> qpred_check T' P -> T = T'.
Proof.
intros. generalize dependent T'. induction H0; intros; simpl in *. inv H1. easy.
inv H3.
assert (simple_qpred Sa).
unfold simple_qpred in * ; intros. inv H.
apply H6; try easy.
assert (sa = sa0).
unfold simple_qpred in *.
inv H. inv H6. inv H0. inv H8. easy. subst.
rewrite (IHqpred_check H3 T0); try easy.
inv H2. inv H10. easy.
inv H10. easy.
inv H10. easy.
Qed.
Lemma pred_check_eq_app: forall T T1 S, simple_qpred S -> qpred_check (T++T1) S
-> exists b1 b2, S = b1 ++ b2 /\ length b1 = length T /\ qpred_check T b1 /\ qpred_check T1 b2.
Proof.
induction T;intros; intros; simpl in *; try easy.
exists nil,S. simpl in *. split; try easy; try constructor. easy.
split; try constructor. easy.
inv H0. inv H. inv H2. inv H3.
apply IHT in H4; try easy.
destruct H4 as [b1 [b2 [X1 [X2 [X3 X4]]]]]; subst.
exists ((SV l, v) :: b1),b2. split; try easy.
split. simpl. rewrite X2. easy.
split. constructor. constructor. easy.
easy. easy.
Qed.
Lemma eval_aexp_not_exists: forall x s a v va, ~ AEnv.In x s -> eval_aexp s a va -> eval_aexp (AEnv.add x v s) a va.
Proof.
intros. induction H0. constructor.
assert (x0 <> x). intros R. subst.
assert (AEnv.In x s). exists (r,n). easy. easy.
apply AEnv.add_2. lia. easy.
constructor. constructor; try easy. apply IHeval_aexp. easy.
apply aplus_sem_2; try easy. apply IHeval_aexp. easy.
constructor; try easy. apply IHeval_aexp. easy.
apply amult_sem_2; try easy. apply IHeval_aexp. easy.
Qed.
Lemma eval_cabexp_not_exists: forall x s a v, ~ AEnv.In x s -> eval_cabexp s a -> eval_cabexp (AEnv.add x v s) a.
Proof.
intros. induction H0; try easy. apply ceq_asem with (r1 := r1) (r2 := r2) (n1 := n1) (n2 := n2); try easy.
apply eval_aexp_not_exists; try easy.
apply eval_aexp_not_exists; try easy.
apply clt_asem with (r1 := r1) (r2 := r2) (n1 := n1) (n2 := n2); try easy.
apply eval_aexp_not_exists; try easy.
apply eval_aexp_not_exists; try easy.
constructor. apply IHeval_cabexp. easy.
Qed.
Lemma pick_mea_exist_same: forall n n0 m ba bl r v, n <= n0 ->
(forall j : nat, j < m -> fst (bl j) = fst (ba j) /\ (forall i : nat, i < n0 -> snd (bl j) i = snd (ba j) i)) ->
pick_mea n (Cval m bl) (r, v) -> pick_mea n (Cval m ba) (r, v).
Proof.
intros. remember (r,v) as V. inv H1. inv H6.
specialize (H0 i). assert (i < m) by lia. apply H0 in H1.
destruct H1. destruct (ba i) eqn:eq1. simpl in *. rewrite H7 in H1. simpl in *. subst.
rewrite H7 in H2. simpl in *.
assert (a_nat2fb bl0 n = a_nat2fb b n).
clear H0 eq1 H5 H7. unfold a_nat2fb. induction n; intros;simpl in *. easy.
rewrite IHn; try lia.
specialize (H2 n). rewrite H2 ; try lia.
rewrite H1.
apply pick_meas with (i := i); try easy.
Qed.
Lemma build_state_ch_exist_same: forall n n0 m ba bl v na bl', n <= n0 ->
(forall j : nat, j < m -> fst (bl j) = fst (ba j) /\ (forall i : nat, i < n0 -> snd (bl j) i = snd (ba j) i)) ->
build_state_ch n v (Cval m bl) = Some (Cval na bl') ->
(exists ba', build_state_ch n v (Cval m ba) = Some (Cval na ba')
/\ match_value (n0 - n) ((Cval na bl')) (Cval na ba')).
Proof.
intros. unfold build_state_ch in *.
assert ((to_sum_c m n v bl) = (to_sum_c m n v ba)). clear H1.
induction m; intros; simpl in *. easy.
assert ((forall j : nat,
j < m ->
fst (bl j) = fst (ba j) /\
(forall i : nat, i < n0 -> snd (bl j) i = snd (ba j) i))).
intros. apply H0. lia. apply IHm in H1. rewrite H1.
assert (a_nat2fb (@snd C rz_val (bl m)) n = a_nat2fb (@snd C rz_val (ba m)) n).
clear H1 IHm.
unfold a_nat2fb in *. induction n;intros;simpl in *. easy.
rewrite IHn; try lia.
specialize (H0 m). assert (m < S m) by lia. apply H0 in H1. destruct H1.
rewrite H2; try lia. easy.
rewrite H2.
assert ((@fst C rz_val (bl m)) = ((@fst C rz_val (ba m)))).
specialize (H0 m). assert (m < S m) by lia.
apply H0 in H3. destruct H3. easy. rewrite H3. easy.
rewrite H2 in *. remember (to_sum_c m n v ba) as f. clear H2. clear Heqf.
destruct (build_state_pars m n v f bl) eqn:eq1. inv H1.
assert (exists pa, build_state_pars m n v f ba = (na,pa)
/\ match_value (n0-n) (Cval na bl') (Cval na pa)).
generalize dependent na. generalize dependent bl'.
induction m;intros;simpl in *; try easy.
exists (fun i => (C0,allfalse)). split. inv eq1. easy.
constructor. intros. inv eq1. split. easy. intros. easy.
assert (a_nat2fb (@snd C rz_val (bl m)) n = a_nat2fb (@snd C rz_val (ba m)) n).
clear eq1 IHm.
unfold a_nat2fb in *. induction n;intros;simpl in *. easy.
rewrite IHn; try lia.
specialize (H0 m). assert (m < S m) by lia. apply H0 in H1. destruct H1.
rewrite H2; try lia. easy.
destruct (build_state_pars m n v f bl) eqn:eq2.
rewrite H1 in *. bdestruct (a_nat2fb (@snd C rz_val (ba m)) n =? v).
inv eq1.
assert ((forall j : nat,
j < m ->
fst (bl j) = fst (ba j) /\
(forall i : nat, i < n0 -> snd (bl j) i = snd (ba j) i))).
intros. apply H0. lia.
destruct (IHm H2 p n1) as [pa [X1 X2]]; try easy.
rewrite X1. exists (update pa n1 ((fst (ba m) / f)%C, lshift_fun (snd (ba m)) n)). split. easy.
inv X2.
constructor. intros. split; simpl in *.
bdestruct (j <? n1).
repeat rewrite update_index_neq; try lia. apply H5. lia.
assert (j = n1) by lia; subst.
repeat rewrite update_index_eq. simpl in *.
assert (fst (bl m) = fst (ba m)). apply H0. lia. rewrite <- H6. easy.
intros.
bdestruct (j <? n1).
repeat rewrite update_index_neq; try lia. apply H5. lia. easy.
assert (j = n1) by lia; subst.
repeat rewrite update_index_eq. simpl in *.
unfold lshift_fun. destruct (H0 m); try lia.
rewrite H8; try easy. lia. apply IHm in eq1.
destruct eq1 as [pa [X1 X2]]. exists pa.
rewrite X1. split. easy. easy. intros. apply H0; try easy. lia.
destruct H1 as [pa [X1 X2]]. rewrite X1.
exists pa. split; easy.
Qed.
Lemma eval_to_nor_switch_same: forall n r1 b, (forall i : nat, i < n -> r1 i = b i)
-> eval_to_had n r1 = eval_to_had n b.
Proof.
intros. unfold eval_to_had in *. apply functional_extensionality.
intros. bdestruct (x <? n). rewrite H; try easy. easy.
Qed.
Lemma eval_to_had_switch_same: forall n r1 b, (forall i : nat, i < n -> r1 i = b i)
-> eval_to_nor n r1 = eval_to_nor n b.
Proof.
intros. unfold eval_to_nor in *. apply functional_extensionality.
intros. bdestruct (x <? n). rewrite H; try easy. easy.
Qed.
Lemma eval_eq_bool_same: forall m n size r bl v,
size < n ->
(forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n -> snd (r j) i = snd (bl j) i)) ->
match_value n (Cval m (eval_eq_bool r m size v)) (Cval m (eval_eq_bool bl m size v)).
Proof.
induction m; intros; constructor; intros; simpl in *; try easy.
assert (forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n -> snd (r j) i = snd (bl j) i)).
intros. apply H0. lia. specialize (IHm n size r bl v H H2).
inv IHm. split.
bdestruct (j =? m); subst. repeat rewrite update_index_eq.
simpl in *. specialize (H0 m). assert (m < S m) by lia.
apply H0 in H1. easy.
repeat rewrite update_index_neq; try lia.
specialize (H5 j). assert (j < m) by lia.
apply H5 in H4. easy.
intros.
bdestruct (j =? m); subst. repeat rewrite update_index_eq.
simpl in *. specialize (H0 m). assert (m < S m) by lia.
apply H0 in H1.
destruct H1. repeat rewrite H6; try lia.
assert (a_nat2fb (@snd C rz_val (r m)) size = (a_nat2fb (@snd C rz_val (bl m)) size)).
unfold a_nat2fb.
clear H0 H1 H2 H5 H3 H4. induction size; simpl; try easy.
rewrite IHsize; try lia. rewrite H6; try lia. easy.
rewrite H7.
bdestruct (i =? size); subst.
repeat rewrite update_index_eq. easy.
repeat rewrite update_index_neq; try lia.
rewrite H6; try lia. easy.
repeat rewrite update_index_neq; try lia.
assert (j < m) by lia. apply H5 in H6.
destruct H6. rewrite H7; try lia. easy.
Qed.
Lemma eval_lt_bool_same: forall m n size r bl v,
size < n ->
(forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n -> snd (r j) i = snd (bl j) i)) ->
match_value n (Cval m (eval_lt_bool r m size v)) (Cval m (eval_lt_bool bl m size v)).
Proof.
induction m; intros; constructor; intros; simpl in *; try easy.
assert (forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n -> snd (r j) i = snd (bl j) i)).
intros. apply H0. lia. specialize (IHm n size r bl v H H2).
inv IHm. split.
bdestruct (j =? m); subst. repeat rewrite update_index_eq.
simpl in *. specialize (H0 m). assert (m < S m) by lia.
apply H0 in H1. easy.
repeat rewrite update_index_neq; try lia.
specialize (H5 j). assert (j < m) by lia.
apply H5 in H4. easy.
intros.
bdestruct (j =? m); subst. repeat rewrite update_index_eq.
simpl in *. specialize (H0 m). assert (m < S m) by lia.
apply H0 in H1.
destruct H1. repeat rewrite H6; try lia.
assert (a_nat2fb (@snd C rz_val (r m)) size = (a_nat2fb (@snd C rz_val (bl m)) size)).
unfold a_nat2fb.
clear H0 H1 H2 H5 H3 H4. induction size; simpl; try easy.
rewrite IHsize; try lia. rewrite H6; try lia. easy.
rewrite H7.
bdestruct (i =? size); subst.
repeat rewrite update_index_eq. easy.
repeat rewrite update_index_neq; try lia.
rewrite H6; try lia. easy.
repeat rewrite update_index_neq; try lia.
assert (j < m) by lia. apply H5 in H6.
destruct H6. rewrite H7; try lia. easy.
Qed.
Lemma eval_rlt_bool_same: forall m n size r bl v,
size < n ->
(forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n -> snd (r j) i = snd (bl j) i)) ->
match_value n (Cval m (eval_rlt_bool r m size v)) (Cval m (eval_rlt_bool bl m size v)).
Proof.
induction m; intros; constructor; intros; simpl in *; try easy.
assert (forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n -> snd (r j) i = snd (bl j) i)).
intros. apply H0. lia. specialize (IHm n size r bl v H H2).
inv IHm. split.
bdestruct (j =? m); subst. repeat rewrite update_index_eq.
simpl in *. specialize (H0 m). assert (m < S m) by lia.
apply H0 in H1. easy.
repeat rewrite update_index_neq; try lia.
specialize (H5 j). assert (j < m) by lia.
apply H5 in H4. easy.
intros.
bdestruct (j =? m); subst. repeat rewrite update_index_eq.
simpl in *. specialize (H0 m). assert (m < S m) by lia.
apply H0 in H1.
destruct H1. repeat rewrite H6; try lia.
assert (a_nat2fb (@snd C rz_val (r m)) size = (a_nat2fb (@snd C rz_val (bl m)) size)).
unfold a_nat2fb.
clear H0 H1 H2 H5 H3 H4. induction size; simpl; try easy.
rewrite IHsize; try lia. rewrite H6; try lia. easy.
rewrite H7.
bdestruct (i =? size); subst.
repeat rewrite update_index_eq. easy.
repeat rewrite update_index_neq; try lia.
rewrite H6; try lia. easy.
repeat rewrite update_index_neq; try lia.
assert (j < m) by lia. apply H5 in H6.
destruct H6. rewrite H7; try lia. easy.
Qed.
Lemma bexp_eval_same: forall b env n n2 l l1 m r bl v s, type_bexp env b (QT n, l) ->
ses_len (l ++ l1) = Some n2 ->
(forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n2 -> snd (r j) i = snd (bl j) i)) ->
eval_bexp ((l ++ l1, Cval m r)::s) b ((l ++ l1, v)::s) ->
(exists v', eval_bexp ((l ++ l1, Cval m bl)::s) b ((l ++ l1, v')::s) /\ match_value n2 v v').
Proof.
induction b;intros;simpl in *; try easy.
apply type_bexp_ses_len in H as X1.
apply ses_len_app_sub with (n := n) in H0 as X2; try easy.
inv H. inv H2; simpl in *.
exists (Cval m (eval_eq_bool bl m m0 b)).
split. constructor.
apply ses_len_app_small with (l1 := l1) (n1 := n2) in X1 as X3; try easy.
apply eval_eq_bool_same; try lia; easy.
inv H2; simpl in *.
exists (Cval m (eval_eq_bool bl m m0 b)).
split. constructor.
apply ses_len_app_small with (l1 := l1) (n1 := n2) in X1 as X3; try easy.
apply eval_eq_bool_same; try lia; easy.
apply type_bexp_ses_len in H as X1.
apply ses_len_app_sub with (n := n) in H0 as X2; try easy.
inv H. inv H2; simpl in *.
exists (Cval m (eval_lt_bool bl m m0 b)).
split. constructor.
apply ses_len_app_small with (l1 := l1) (n1 := n2) in X1 as X3; try easy.
apply eval_lt_bool_same; try lia; easy.
inv H2; simpl in *.
exists (Cval m (eval_rlt_bool bl m m0 b)).
split. constructor.
apply ses_len_app_small with (l1 := l1) (n1 := n2) in X1 as X3; try easy.
apply eval_rlt_bool_same; try lia; easy.
apply type_bexp_ses_len in H as X1.
apply ses_len_app_sub with (n := n) in H0 as X2; try easy.
inv H. inv H2; simpl in *.
exists (Cval m bl).
split. constructor. constructor; try easy.
Qed.
Lemma grab_bool_same_fst: forall m n n1 r bl, 0 < n <= n1 ->
(forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n1 -> snd (r j) i = snd (bl j) i)) ->
(fst (grab_bool r m n)) = (fst (grab_bool bl m n)).
Proof.
unfold grab_bool in *; induction m; intros; simpl in *; try easy.
destruct (H0 m) ; try lia. rewrite H2; try lia.
assert ((@snd C (forall _ : nat, bool) (bl m) (Init.Nat.sub n (S O))
= @snd C rz_val (bl m) (Init.Nat.sub n (S O)))) by easy.
destruct (snd (bl m) (n - 1)) eqn:eq1. rewrite <- H3 in *.
destruct (grab_bool_elem r m (n - 1)) eqn:eq2.
destruct (grab_bool_elem bl m (n - 1)) eqn:eq3.
assert (fst (grab_bool_elem r m (n - 1)) = fst (grab_bool_elem bl m (n - 1))).
erewrite IHm. easy. apply H. intros. apply H0. lia. rewrite eq2 in H4. rewrite eq3 in H4.
simpl in *. rewrite H4. easy.
rewrite <- H3. erewrite IHm. easy. apply H. intros. apply H0. lia.
Qed.
Lemma grab_bool_same_fst_lt: forall m n r,
(fst (grab_bool r m n)) <= m.
Proof.
unfold grab_bool. induction m;intros;simpl in *; try easy.
destruct (snd (r m) (n - 1)).
destruct (grab_bool_elem r m (n - 1)) eqn:eq1.
specialize (IHm n r). rewrite eq1 in IHm. simpl in *. lia.
specialize (IHm n r). lia.
Qed.
Lemma grab_bool_gt_case: forall m n r j, (fst (grab_bool_elem r m n)) <= j
-> (snd (grab_bool_elem r m n)) j = (C0,allfalse).
Proof.
induction m; intros; simpl in *; try easy.
destruct (snd (r m) n) eqn:eq1. destruct (grab_bool_elem r m n) eqn:eq2.
simpl in *. rewrite update_index_neq by lia. replace p with (snd (grab_bool_elem r m n)).
rewrite IHm; try easy. rewrite eq2. simpl. lia. rewrite eq2. easy.
rewrite IHm. easy. lia.
Qed.
Lemma grab_bool_same: forall m n n1 r bl, 0 < n <= n1 ->
(forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n1 -> snd (r j) i = snd (bl j) i))
-> match_value n1 (Cval (fst (grab_bool r m n)) (snd (grab_bool r m n)))
(Cval (fst (grab_bool bl m n)) (snd (grab_bool bl m n))).
Proof.
intros.
rewrite grab_bool_same_fst with (n1 := n1) (bl := bl) in *; try easy.
specialize (grab_bool_same_fst_lt m n bl) as X3.
unfold grab_bool in *.
induction m; intros; constructor; intros;simpl in *; try easy.
assert ((forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < n1 -> snd (r j) i = snd (bl j) i))).
intros. apply H0. lia. specialize (IHm H2). simpl in *.
destruct (H0 m); try lia. rewrite H4 in *; try lia.
assert ((@snd C (forall _ : nat, bool) (bl m) (Init.Nat.sub n (S O))
= @snd C rz_val (bl m) (Init.Nat.sub n (S O)))) by easy.
destruct (snd (bl m) (n - 1)) eqn:eq1.
rewrite <- H5 in *.
destruct (grab_bool_elem r m (n - 1)) eqn:eq2.
destruct (grab_bool_elem bl m (n - 1)) eqn:eq3.
simpl in *.
assert (n0 = n2).
replace n0 with (fst (grab_bool r m n)).
rewrite grab_bool_same_fst with (n1 := n1) (bl := bl); try easy.
unfold grab_bool in *. rewrite eq3. simpl. easy.
unfold grab_bool. rewrite eq2. easy.
subst.
assert (n2 <= m) by lia. apply IHm in H6. inv H6.
bdestruct (j =? n2); subst. repeat rewrite update_index_eq.
apply H0. lia.
repeat rewrite update_index_neq; try lia.
apply H9. lia.
rewrite <- H5 in *.
destruct (grab_bool_elem r m (n - 1)) eqn:eq2.
destruct (grab_bool_elem bl m (n - 1)) eqn:eq3.
simpl in *.
bdestruct (n2 =? S m); subst.
specialize (grab_bool_same_fst_lt m n bl) as X1. unfold grab_bool in *.
rewrite eq3 in *. simpl in *. lia.
assert (n2 <= m) by lia.
apply IHm in H7. inv H7. apply H10. lia.
Qed.
Lemma mut_state_same: forall m pos n n1 r bl fc, 0 < n ->
(forall j : nat,
j < m ->
fst (r j) = fst (bl j) /\
(forall i : nat, i < pos + n + n1 -> snd (r j) i = snd (bl j) i))
-> mut_state pos n n1 (Cval (fst (grab_bool r m n)) (snd (grab_bool r m n))) fc
-> (exists fca, mut_state pos n n1 (Cval (fst (grab_bool bl m n)) (snd (grab_bool bl m n))) fca
/\ match_value (n+n1) fc fca).
Proof.
intros. inv H1.
exists ((Cval (fst (grab_bool bl m n))
(mut_fch_state pos n n1 (snd (grab_bool bl m n))))).
split.
apply fch_mut_state.
assert (0 < n <= pos + n + n1) by lia.
specialize (grab_bool_same m n (pos+n+n1) r bl H1 H0) as X1.
inv X1. constructor. intros. apply H4 in H2 as [X2 X3].
unfold mut_fch_state in *. simpl in *. split. apply X2.
intros. unfold mut_nor_aux in *.
bdestruct (i <? pos). apply X3. lia.
bdestruct (pos <=? i); simpl in *; try lia.
bdestruct (i <? pos + n1); simpl in *.
apply X3. lia.
bdestruct (pos + n1 <=? i); simpl in *; try lia.
bdestruct (i <? pos + n1 + n); simpl in *; try lia.
apply X3. lia.
Qed.
Lemma assem_bool_same: forall m m1 n n1 f f' r r' fc,
(forall j : nat,
j < m ->
fst (f j) = fst (f' j) /\
(forall i : nat, i < n + n1 -> snd (f j) i = snd (f' j) i)) ->
(forall j : nat,
j < m1 ->
fst (r j) = fst (r' j) /\
(forall i : nat, i < n1 -> snd (r j) i = snd (r' j) i)) ->
assem_bool n n1 m f (Cval m1 r) fc ->
(exists fca, assem_bool n n1 m f' (Cval m1 r') fca
/\ match_value (n+n1) fc fca).
Proof.
induction m; intros; simpl in *; try easy. inv H1.
exists (Cval 0 (fun _ : nat => (C0, allfalse))).
split; try constructor. intros. split; try easy.
inv H1. apply IHm with (f' := f') (r' := r') in H6 as X1; try easy.
destruct X1 as [fca [X1 X2]]. inv X2.
replace (cut_n (@snd C rz_val (f m)) n) with (cut_n (@snd C rz_val (f' m)) n) in H8.
apply find_basis_elems_same with (r' := r') in H8; try easy.
destruct H8 as [fv' [X2 X3]].
exists (Cval (S m') (update r2 m' (f' m))).
split. apply assem_bool_many_1 with (fv := fv'); try easy.
constructor. intros.
bdestruct (j =? m'); subst.
repeat rewrite update_index_eq.
assert (m < S m) by lia. destruct (H m H2).
split. rewrite H3. easy. intros. rewrite H4. easy. easy.
repeat rewrite update_index_neq; try lia.
apply H5; try lia.
assert (m < S m) by lia. destruct (H m H1).
unfold cut_n in *. apply functional_extensionality.
intros. bdestruct (x <? n). rewrite H3; try easy. lia.
easy.
intros. apply H. lia.
replace (cut_n (@snd C rz_val (f m)) n) with (cut_n (@snd C rz_val (f' m)) n) in *.
apply IHm with (f' := f') (r' := r') in H5 as X1; try easy.
destruct X1 as [fca [X1 X2]]. inv X2.
apply find_basis_elems_same with (r' := r') in H8; try easy.
destruct H8 as [fv' [X2 X3]].
inv X3.
apply (assem_list_same mv m' n n1
(cut_n (snd (f' m)) n) fv fv' acc r2 ma fa) in H3 as Y1; try easy.
destruct Y1 as [fva [Y1 Y2]].
exists (Cval ma fva).
split. apply assem_bool_many_2 with (m' := m') (acc := r2) (mv := mv) (fv := fv'); try easy.
easy.
intros. apply H. lia.
unfold cut_n. apply functional_extensionality. intros.
bdestruct (x <? n).
assert (m < S m) by lia. apply H in H2.
destruct H2. rewrite H3. easy. lia.
easy.
Qed.
Lemma match_value_shrink: forall n n1 a b, n <= n1 -> match_value n1 a b -> match_value n a b.
Proof.
intros. induction H0. constructor. intros. apply H0. lia.
constructor. intros. apply H0. lia. constructor.
intros. apply H0 in H1. destruct H1. split; try easy.
intros. apply H2. lia.
Qed.
Lemma simp_aexp_no_subst: forall a v x va, simp_aexp a = Some v -> subst_aexp a x va = a.
Proof.
induction a; intros; simpl in *; try easy.
destruct (simp_aexp a1) eqn:eq1; try easy.
destruct (simp_aexp a2) eqn:eq2; try easy.
rewrite IHa1 with (v := n); try easy.
rewrite IHa2 with (v := n0); try easy.
destruct (simp_aexp a1) eqn:eq1; try easy.
destruct (simp_aexp a2) eqn:eq2; try easy.
rewrite IHa1 with (v := n); try easy.
rewrite IHa2 with (v := n0); try easy.
Qed.
Lemma simple_ses_app_combine: forall l l1, simple_ses l -> simple_ses l1 -> simple_ses (l++l1).
Proof.
intros. induction H. simpl. easy. constructor; try easy.
Qed.
Definition add_tenv (T:type_map) (l:locus) :=
match T with ((la,CH)::ta) => Some ((la++l,CH)::ta) | _ => None end.
Lemma subst_type_map_empty: forall T x v, subst_type_map T x v = nil -> T = nil.
Proof.
induction T; intros; simpl in *; try easy. destruct a. inv H.
Qed.
Lemma subst_locus_simple: forall s l1 i v, simple_ses l1
-> subst_locus (s ++ l1) i v = subst_locus s i v ++ l1.
Proof.
induction s; intros; simpl in *. rewrite simple_ses_subst; try easy.
rewrite IHs. easy. easy.
Qed.
Definition single_tenv (T:type_map) := match T with ((l,CH)::ta) => True | _ => False end.
Lemma state_equiv_single: forall rmax sa l m b,
@state_equiv rmax sa ([(l,Cval m b)]) -> sa = ([(l,Cval m b)]).
Proof.
intros. remember ([(l, Cval m b)]) as sb. induction H; subst; try easy.
destruct a2. simpl in *. subst. rewrite app_nil_r. easy.
simpl in *. inv Heqsb. apply app_eq_nil in H1. destruct H1; subst. simpl in *. easy.
inv Heqsb. inv H. easy.
apply app_eq_nil in H2. destruct H2; subst. simpl in *. easy.
inv Heqsb. inv H0.
Qed.
Lemma type_system_not_var: forall e rmax q env T T1 x a,
@locus_system rmax q env T (Let x a e) T1 -> ~ AEnv.In x env.
Proof.
intros. remember (Let x a e) as ea. generalize dependent a.
generalize dependent e. induction H; intros; subst; simpl in *; try easy.
eapply IHlocus_system. easy. inv Heqea. easy.
inv Heqea. easy. inv Heqea. easy.
Qed.
Lemma qpred_check_split: forall T P P', qpred_check T (P++P')
-> exists T1 T2, T = T1 ++ T2 /\ qpred_check T1 P /\ qpred_check T2 P'.
Proof.
intros. remember (P++P') as A. generalize dependent P. generalize dependent P'.
induction H; intros; simpl in *; try easy.
symmetry in HeqA.
apply app_eq_nil in HeqA. destruct HeqA; subst.
exists nil,nil. simpl in *. split; try easy; try constructor.
constructor. constructor.
destruct P. simpl in *; subst.
specialize (IHqpred_check Sa nil). simpl in *.
assert (Sa = Sa) by easy. apply IHqpred_check in H2.
destruct H2 as [T1 [T2 [X1 [X2 X3]]]]; subst.
inv X2. simpl in *. exists nil, ((sa,t)::T2).
split; try easy. split; constructor; try easy.
inv HeqA. assert (P++P' = P++P') by easy.
apply IHqpred_check in H2.
destruct H2 as [T1 [T2 [X1 [X2 X3]]]]; subst.
exists ((sa,t)::T1),T2. split; try easy.
split; try constructor; try easy.
Qed.
Lemma qpred_check_shrink_r: forall T T' P P', qpred_check (T++T') (P++P') -> length T = length P
-> qpred_check T' P'.
Proof.
intros. remember (T++T') as Ta. remember (P ++ P') as Pa.
generalize dependent P. generalize dependent P'.
generalize dependent T. generalize dependent T'.
induction H; intros; simpl in *; try easy.
symmetry in HeqTa. symmetry in HeqPa.
apply app_eq_nil in HeqTa.
apply app_eq_nil in HeqPa.
destruct HeqTa; subst.
destruct HeqPa;subst.
constructor.
destruct T0. simpl in *.
symmetry in H2.
apply length_zero_iff_nil in H2; subst.
simpl in *. subst. constructor; try easy.
inv HeqTa. inv HeqPa. simpl in *. destruct P. simpl in *. lia.
simpl in *. inv H2. inv H4. apply IHqpred_check with (T := T0) (P0 := P); try easy.
Qed.
Lemma qpred_check_combine: forall T T' P P', qpred_check T P -> qpred_check T' P' ->
qpred_check (T++T') (P++P').
Proof.
intros. generalize dependent T'. generalize dependent P'.
induction H; intros; simpl in *; try easy.
constructor; try easy.
apply IHqpred_check. easy.
Qed.
Lemma sval_check_same: forall s sa v, sval_check s v -> sval_check sa v -> s = sa.
Proof.
intros. generalize dependent sa. induction H; intros; simpl in *; try easy.
inv H0. easy. inv H0. apply IHsval_check in H3. easy.
inv H0. apply IHsval_check in H3. easy.
inv H0. apply IHsval_check in H3. easy.
Qed.
Lemma qpred_check_same: forall T T1 P, qpred_check T P -> qpred_check T1 P -> T = T1.
Proof.
intros. generalize dependent T1. induction H; intros; simpl in *; try easy.
inv H0. easy. inv H2.