forked from PrincetonUniversity/VST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal2ptree.v
1392 lines (1331 loc) · 49.3 KB
/
local2ptree.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 floyd.base.
Require Import floyd.client_lemmas.
Require Import floyd.assert_lemmas.
Require Import floyd.closed_lemmas.
Require Import floyd.canonicalize.
Require Import floyd.type_id_env.
Require Import floyd.nested_field_lemmas.
Require Import floyd.efield_lemmas.
Local Open Scope logic.
Inductive vardesc : Type :=
| vardesc_local_global: type -> val -> val -> vardesc
| vardesc_local: type -> val -> vardesc
| vardesc_visible_global: val -> vardesc
| vardesc_shadowed_global: val -> vardesc.
Definition denote_vardesc (Q: list (environ -> Prop)) (i: ident) (vd: vardesc) : list (environ -> Prop) :=
match vd with
| vardesc_local_global t v v' => lvar i t v :: sgvar i v' :: Q
| vardesc_local t v => lvar i t v :: Q
| vardesc_visible_global v => gvar i v :: Q
| vardesc_shadowed_global v => sgvar i v :: Q
end.
Definition pTree_from_elements {A} (el: list (positive * A)) : PTree.t A :=
fold_right (fun ia t => PTree.set (fst ia) (snd ia) t) (PTree.empty _) el.
Inductive local2ptree:
list (environ -> Prop) -> PTree.t val -> PTree.t vardesc
-> list Prop -> list (environ -> Prop) -> Prop :=
| local2ptree_nil:
local2ptree nil (PTree.empty _) (PTree.empty _) nil nil
| local2ptree_temp: forall v i T1' P'1 Q T1 T2 P' Q',
local2ptree Q T1 T2 P' Q' ->
(T1',P'1) = match T1 ! i with
| None => (PTree.set i v T1, P')
| Some v' => (T1, (v=v')::P')
end ->
local2ptree (temp i v :: Q) T1' T2 P'1 Q'
| local2ptree_lvar: forall v i t Q T2' P'1 T1 T2 P' Q',
local2ptree Q T1 T2 P' Q' ->
(T2', P'1) = match T2 ! i with
| None => (PTree.set i (vardesc_local t v) T2, P')
| Some (vardesc_local_global t' vl vg) => (T2, (vl=v)::(t'=t)::P')
| Some (vardesc_local t' vl) => (T2, (vl=v)::(t'=t)::P')
| Some (vardesc_visible_global vg) => (* impossible *) (T2, False :: P')
| Some (vardesc_shadowed_global vg) => (PTree.set i (vardesc_local_global t v vg) T2, P')
end ->
local2ptree (lvar i t v :: Q) T1 T2' P'1 Q'
| local2ptree_gvar: forall v i Q T2' P'1 T1 T2 P' Q',
local2ptree Q T1 T2 P' Q' ->
(T2', P'1) = match T2 ! i with
| None => (PTree.set i (vardesc_visible_global v) T2, P')
| Some (vardesc_local_global t vl vg) => (*impossible*) (T2, False::P')
| Some (vardesc_local t vl) => (*impossible*) (T2, False::P')
| Some (vardesc_visible_global vg) => (T2, (vg=v)::P')
| Some (vardesc_shadowed_global vg) => (PTree.set i (vardesc_visible_global v) T2, (vg=v)::P')
end ->
local2ptree (gvar i v :: Q) T1 T2' P'1 Q'
| local2ptree_sgvar: forall v i Q T2' P'1 T1 T2 P' Q',
local2ptree Q T1 T2 P' Q' ->
(T2', P'1) = match T2 ! i with
| None => (PTree.set i (vardesc_shadowed_global v) T2, P')
| Some (vardesc_local_global t vl vg) => (T2, (vg=v)::P')
| Some (vardesc_local t vl) => (PTree.set i (vardesc_local_global t vl v) T2, P')
| Some (vardesc_visible_global vg) => (T2, (vg=v)::P')
| Some (vardesc_shadowed_global vg) => (T2, (vg=v)::P')
end ->
local2ptree (sgvar i v :: Q) T1 T2' P'1 Q'
| local2ptree_unknown: forall Q0 Q T1 T2 P' Q',
local2ptree Q T1 T2 P' Q'->
local2ptree (Q0 :: Q) T1 T2 P' (Q0 :: Q').
(* repeat constructor will try the first succesful tactic. So local2ptree_temp_ *)
(* var, local2ptree_gl_var will be used whenever is possible before local2ptree_*)
(* unknown. *)
Ltac prove_local2ptree :=
match goal with |- local2ptree _ _ _ _ _ =>
first [ solve [econstructor]
| econstructor ; [prove_local2ptree | reflexivity ]
| econstructor ; [ prove_local2ptree ]
]
end.
Ltac construct_local2ptree Q H :=
let T1 := fresh "T" in evar (T1: PTree.t val);
let T2 := fresh "T" in evar (T2: PTree.t vardesc);
let P' := fresh "P'" in evar (P' : list Prop);
let Q' := fresh "Q'" in evar (Q' : list (environ -> Prop));
assert (local2ptree Q T1 T2 P' Q') as H; subst T1 T2 P' Q';
[ prove_local2ptree | ].
Module TEST.
Goal False.
construct_local2ptree (temp 1%positive Vundef :: lvar 1%positive tint (Vint (Int.repr 1)) ::
(`(eq 1 3)) :: nil) H.
Abort.
End TEST.
Definition LocalD (T1: PTree.t val) (T2: PTree.t vardesc) (Q: list (environ -> Prop)) :=
PTree.fold (fun Q i v => temp i v :: Q) T1
(PTree.fold denote_vardesc T2 Q).
Lemma PTree_elements_set: forall {A} i (v: A) elm T,
In elm (PTree.elements (PTree.set i v T)) ->
elm = (i, v) \/ In elm (PTree.elements T).
Proof.
intros.
destruct elm as [i' v'].
apply PTree.elements_complete in H.
destruct (ident_eq i i').
+ subst.
rewrite PTree.gss in H.
inversion H.
subst.
left; auto.
+ rewrite PTree.gso in H by auto.
right.
apply PTree.elements_correct.
auto.
Qed.
Lemma LocalD_sound_temp:
forall i v T1 T2 Q,
PTree.get i T1 = Some v -> In (temp i v) (LocalD T1 T2 Q).
Proof.
unfold LocalD; intros.
forget (PTree.fold denote_vardesc T2 Q) as Q'.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
apply PTree.elements_correct in H.
rewrite in_rev in H.
forget (rev (PTree.elements T1)) as L.
induction L; intros; destruct H.
subst a. left. reflexivity.
right. apply IHL. auto.
Qed.
Lemma LocalD_sound_local_global:
forall i t v v' T1 T2 Q,
PTree.get i T2 = Some (vardesc_local_global t v v') ->
In (lvar i t v) (LocalD T1 T2 Q) /\ In (sgvar i v') (LocalD T1 T2 Q).
Proof.
unfold LocalD; intros.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
apply PTree.elements_correct in H.
rewrite in_rev in H.
forget (rev (PTree.elements T1)) as L.
induction L; [ | split; right; apply IHL].
forget (rev (PTree.elements T2)) as L.
simpl.
induction L; intros; destruct H.
subst a.
split.
left; reflexivity.
right; left; reflexivity.
destruct (IHL H); clear IHL H.
split; simpl.
destruct a as [ia vda]; destruct vda; simpl in *; auto.
destruct a as [ia vda]; destruct vda; simpl in *; auto.
Qed.
Lemma LocalD_sound_local:
forall i t v T1 T2 Q,
PTree.get i T2 = Some (vardesc_local t v) ->
In (lvar i t v) (LocalD T1 T2 Q).
Proof.
unfold LocalD; intros.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
apply PTree.elements_correct in H.
rewrite in_rev in H.
forget (rev (PTree.elements T1)) as L.
induction L; [ | right; apply IHL].
forget (rev (PTree.elements T2)) as L.
simpl.
induction L; intros; destruct H.
subst a.
left; reflexivity.
simpl.
destruct a as [ia vda]; destruct vda; simpl in *; auto.
Qed.
Lemma LocalD_sound_visible_global:
forall i v T1 T2 Q,
PTree.get i T2 = Some (vardesc_visible_global v) ->
In (gvar i v) (LocalD T1 T2 Q).
Proof.
unfold LocalD; intros.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
apply PTree.elements_correct in H.
rewrite in_rev in H.
forget (rev (PTree.elements T1)) as L.
induction L; [ | right; apply IHL].
forget (rev (PTree.elements T2)) as L.
simpl.
induction L; intros; destruct H.
subst a.
left; reflexivity.
simpl.
destruct a as [ia vda]; destruct vda; simpl in *; auto.
Qed.
Lemma LocalD_sound_shadowed_global:
forall i v T1 T2 Q,
PTree.get i T2 = Some (vardesc_shadowed_global v) ->
In (sgvar i v) (LocalD T1 T2 Q).
Proof.
unfold LocalD; intros.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
apply PTree.elements_correct in H.
rewrite in_rev in H.
forget (rev (PTree.elements T1)) as L.
induction L; [ | right; apply IHL].
forget (rev (PTree.elements T2)) as L.
simpl.
induction L; intros; destruct H.
subst a.
left; reflexivity.
simpl.
destruct a as [ia vda]; destruct vda; simpl in *; auto.
Qed.
Lemma LocalD_sound_other:
forall q T1 T2 Q,
In q Q ->
In q (LocalD T1 T2 Q).
Proof.
unfold LocalD; intros.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
forget (rev (PTree.elements T1)) as L.
induction L; [ | right; apply IHL].
forget (rev (PTree.elements T2)) as L.
simpl.
induction L; auto.
simpl.
destruct a as [ia vda]; destruct vda; simpl in *; auto.
Qed.
Lemma LocalD_sound: forall q T1 T2 Q,
(exists i v, PTree.get i T1 = Some v /\ q = temp i v) \/
(exists i t v v', PTree.get i T2 = Some (vardesc_local_global t v v')
/\ q = lvar i t v) \/
(exists i t v v', PTree.get i T2 = Some (vardesc_local_global t v v')
/\ q = sgvar i v') \/
(exists i t v, PTree.get i T2 = Some (vardesc_local t v)
/\ q = lvar i t v) \/
(exists i v, PTree.get i T2 = Some (vardesc_visible_global v)
/\ q = gvar i v) \/
(exists i v, PTree.get i T2 = Some (vardesc_shadowed_global v)
/\ q = sgvar i v) \/
In q Q ->
In q (LocalD T1 T2 Q).
Proof.
intros.
repeat match type of H with
| _ \/ _ => destruct H
| ex _ => destruct H
| _ /\ _ => destruct H
end; subst.
apply LocalD_sound_temp; auto.
apply LocalD_sound_local_global with (T1:=T1) (T2:=T2)(Q:=Q) in H; intuition.
apply LocalD_sound_local_global with (T1:=T1) (T2:=T2)(Q:=Q) in H; intuition.
apply LocalD_sound_local with (T1:=T1) (T2:=T2)(Q:=Q) in H; intuition.
apply LocalD_sound_visible_global with (T1:=T1) (T2:=T2)(Q:=Q) in H; intuition.
apply LocalD_sound_shadowed_global with (T1:=T1) (T2:=T2)(Q:=Q) in H; intuition.
apply LocalD_sound_other with (T1:=T1) (T2:=T2)(Q:=Q) in H; intuition.
Qed.
Lemma LocalD_complete: forall q T1 T2 Q,
In q (LocalD T1 T2 Q) ->
(exists i v, PTree.get i T1 = Some v /\ q = temp i v) \/
(exists i t v v', PTree.get i T2 = Some (vardesc_local_global t v v')
/\ q = lvar i t v) \/
(exists i t v v', PTree.get i T2 = Some (vardesc_local_global t v v')
/\ q = sgvar i v') \/
(exists i t v, PTree.get i T2 = Some (vardesc_local t v)
/\ q = lvar i t v) \/
(exists i v, PTree.get i T2 = Some (vardesc_visible_global v)
/\ q = gvar i v) \/
(exists i v, PTree.get i T2 = Some (vardesc_shadowed_global v)
/\ q = sgvar i v) \/
In q Q.
Proof.
intros.
unfold LocalD in H.
rewrite !PTree.fold_spec, <- !fold_left_rev_right in H.
remember (rev (PTree.elements T1)) as L.
simpl in H.
change L with (nil ++ L) in HeqL.
forget (@nil (positive * val)) as K.
revert K HeqL; induction L; intros.
right.
clear K T1 HeqL.
remember (rev (PTree.elements T2)) as L.
simpl in H.
change L with (nil ++ L) in HeqL.
forget (@nil (positive * vardesc)) as K.
revert K HeqL; induction L; intros.
repeat right. apply H.
assert (In a (PTree.elements T2)).
rewrite in_rev, <- HeqL. rewrite in_app. right; left; auto.
destruct a as [i vv].
apply PTree.elements_complete in H0.
destruct vv; destruct H; try subst q; eauto 50.
destruct H; try subst q; eauto 50;
specialize (IHL H (K ++ (i, vardesc_local_global t v v0) :: nil));
rewrite app_ass in IHL; specialize (IHL HeqL); eauto.
specialize (IHL H (K ++ (i, vardesc_local t v) :: nil));
rewrite app_ass in IHL; specialize (IHL HeqL); eauto.
specialize (IHL H (K ++ (i, vardesc_visible_global v) :: nil));
rewrite app_ass in IHL; specialize (IHL HeqL); eauto.
specialize (IHL H (K ++ (i, vardesc_shadowed_global v) :: nil));
rewrite app_ass in IHL; specialize (IHL HeqL); eauto.
destruct H.
subst q.
assert (In a (PTree.elements T1)).
rewrite in_rev, <- HeqL. rewrite in_app. right; left; auto.
destruct a as [i v]; apply PTree.elements_complete in H; eauto.
destruct a as [i v].
specialize (IHL H (K ++ (i,v)::nil)).
rewrite app_ass in IHL; specialize (IHL HeqL); eauto.
Qed.
Lemma in_temp_aux:
forall q L Q,
In q (fold_right
(fun (y : positive * val) (x : list (environ -> Prop)) =>
temp (fst y) (snd y) :: x) Q L) <->
((exists i v, q = temp i v /\ In (i,v) L) \/ In q Q).
Proof.
intros.
induction L.
simpl. intuition. destruct H0 as [? [? [? ?]]]. contradiction.
intuition.
destruct H0. simpl in *. subst q.
left. eauto.
specialize (H H0).
destruct H as [[? [? [? ?]]] | ?].
left. exists x, x0. split; auto. right; auto.
right; auto.
destruct H3 as [i [v [? ?]]]. destruct H3. inv H3. left. reflexivity.
right; apply H1. eauto.
right; auto.
right; auto.
Qed.
Lemma LOCALx_expand_temp_var: forall i v T1 T2 Q Q0,
In Q0 (LocalD (PTree.set i v T1) T2 Q) <->
In Q0 (temp i v :: LocalD (PTree.remove i T1) T2 Q).
Proof.
intros; split; intros.
+ simpl.
apply LocalD_complete in H.
destruct H.
- destruct H as [i0 [v0 [? ?]]].
subst.
destruct (ident_eq i0 i).
* subst.
rewrite PTree.gss in H.
inversion H; subst.
left; reflexivity.
* rewrite PTree.gso in H by auto.
right.
apply LocalD_sound_temp.
rewrite PTree.gro by auto. auto.
- right.
destruct H.
destruct H as [j [t [v1 [v2 [? ?]]]]]; subst Q0.
unfold LocalD.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
induction (rev (PTree.elements (PTree.remove i T1))); simpl.
apply PTree.elements_correct in H. rewrite in_rev in H.
induction (rev (PTree.elements T2)). inv H.
destruct H. destruct a. inv H. simpl. left; auto.
simpl. destruct a as [? [?|?|?|?]]; simpl; repeat right; auto.
right; apply IHl.
destruct H.
destruct H as [j [t [v1 [v2 [? ?]]]]]; subst Q0.
unfold LocalD.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
induction (rev (PTree.elements (PTree.remove i T1))); simpl.
apply PTree.elements_correct in H. rewrite in_rev in H.
induction (rev (PTree.elements T2)). inv H.
destruct H. destruct a. inv H. simpl. right; left; auto.
simpl. destruct a as [? [?|?|?|?]]; simpl; repeat right; auto.
right; apply IHl.
destruct H.
destruct H as [j [t [v1 [? ?]]]]; subst Q0.
unfold LocalD.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
induction (rev (PTree.elements (PTree.remove i T1))); simpl.
apply PTree.elements_correct in H. rewrite in_rev in H.
induction (rev (PTree.elements T2)). inv H.
destruct H. destruct a. inv H. simpl. left; auto.
simpl. destruct a as [? [?|?|?|?]]; simpl; repeat right; auto.
right; apply IHl.
destruct H.
destruct H as [j [v1 [? ?]]]; subst Q0.
unfold LocalD.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
induction (rev (PTree.elements (PTree.remove i T1))); simpl.
apply PTree.elements_correct in H. rewrite in_rev in H.
induction (rev (PTree.elements T2)). inv H.
destruct H. destruct a. inv H. simpl. left; auto.
simpl. destruct a as [? [?|?|?|?]]; simpl; repeat right; auto.
right; apply IHl.
destruct H.
destruct H as [j [v1 [? ?]]]; subst Q0.
unfold LocalD.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
induction (rev (PTree.elements (PTree.remove i T1))); simpl.
apply PTree.elements_correct in H. rewrite in_rev in H.
induction (rev (PTree.elements T2)). inv H.
destruct H. destruct a. inv H. simpl. left; auto.
simpl. destruct a as [? [?|?|?|?]]; simpl; repeat right; auto.
right; apply IHl.
unfold LocalD.
rewrite !PTree.fold_spec, <- !fold_left_rev_right.
induction (rev (PTree.elements (PTree.remove i T1))); simpl.
induction (rev (PTree.elements T2)); simpl; auto.
destruct a as [? [?|?|?|?]]; simpl; repeat right; auto.
auto.
+
destruct H. subst.
apply LocalD_sound_temp. apply PTree.gss.
unfold LocalD in *.
rewrite !PTree.fold_spec, <- !fold_left_rev_right in *.
forget (fold_right
(fun (y : positive * vardesc) (x : list (environ -> Prop)) =>
denote_vardesc x (fst y) (snd y)) Q (rev (PTree.elements T2))) as JJ.
clear - H.
rewrite in_temp_aux. rewrite in_temp_aux in H.
destruct H as [[j [w [? ?]]] |?].
left; exists j,w; split; auto.
rewrite <- in_rev in *.
apply PTree.elements_correct. apply PTree.elements_complete in H0.
clear - H0.
destruct (ident_eq i j); subst. rewrite PTree.grs in H0; inv H0.
rewrite PTree.gro in H0 by auto. rewrite PTree.gso; auto.
right; auto.
Qed.
Lemma denote_vardesc_prefix:
forall Q i vd, exists L, denote_vardesc Q i vd = L ++ Q.
Proof.
intros; destruct vd; simpl.
exists (lvar i t v :: sgvar i v0 :: nil); reflexivity.
exists (lvar i t v :: nil); reflexivity.
exists (gvar i v :: nil); reflexivity.
exists (sgvar i v :: nil); reflexivity.
Qed.
Lemma In_LocalD_remove_set:
forall q T1 i vd T2 Q,
In q (LocalD T1 (PTree.remove i T2) Q) ->
In q (LocalD T1 (PTree.set i vd T2) Q).
Proof.
intros.
apply LocalD_sound.
apply LocalD_complete in H.
repeat match type of H with
| _ \/ _ => destruct H
| ex _ => destruct H
| _ /\ _ => destruct H
end; subst.
*
left; eauto 50.
*
right; left. exists x,x0,x1,x2. split; auto.
destruct (ident_eq i x). subst. rewrite PTree.grs in H; inv H.
rewrite PTree.gro in H by auto; rewrite PTree.gso by auto; auto.
*
right; right; left. exists x,x0,x1,x2. split; auto.
destruct (ident_eq i x). subst. rewrite PTree.grs in H; inv H.
rewrite PTree.gro in H by auto; rewrite PTree.gso by auto; auto.
*
right; right; right; left. exists x,x0,x1. split; auto.
destruct (ident_eq i x). subst. rewrite PTree.grs in H; inv H.
rewrite PTree.gro in H by auto; rewrite PTree.gso by auto; auto.
*
right; right; right; right; left. exists x,x0. split; auto.
destruct (ident_eq i x). subst. rewrite PTree.grs in H; inv H.
rewrite PTree.gro in H by auto; rewrite PTree.gso by auto; auto.
*
right; right; right; right; right; left. exists x,x0. split; auto.
destruct (ident_eq i x). subst. rewrite PTree.grs in H; inv H.
rewrite PTree.gro in H by auto; rewrite PTree.gso by auto; auto.
*
repeat right. auto.
Qed.
Lemma LOCALx_expand_vardesc: forall i vd T1 T2 Q Q0,
In Q0 (LocalD T1 (PTree.set i vd T2) Q) <->
In Q0 (denote_vardesc (LocalD T1 (PTree.remove i T2) Q) i vd).
Proof.
intros; split; intros.
+ simpl.
apply LocalD_complete in H.
repeat match type of H with
| _ \/ _ => destruct H
| ex _ => destruct H
| _ /\ _ => destruct H
end; subst.
-
destruct vd; simpl;
repeat right; apply LocalD_sound_temp; auto.
-
destruct (ident_eq i x).
* subst x; rewrite PTree.gss in H; inv H. simpl. auto.
* rewrite PTree.gso in H by auto.
destruct vd; simpl.
repeat right. apply LocalD_sound.
right.
left. exists x,x0,x1,x2. rewrite PTree.gro by auto. auto.
right. apply LocalD_sound.
right. left. exists x,x0,x1,x2. rewrite PTree.gro by auto. auto.
right. apply LocalD_sound.
right. left. exists x,x0,x1,x2. rewrite PTree.gro by auto. auto.
right. apply LocalD_sound.
right. left. exists x,x0,x1,x2. rewrite PTree.gro by auto. auto.
-
destruct (ident_eq i x).
* subst x; rewrite PTree.gss in H; inv H. simpl. auto.
* rewrite PTree.gso in H by auto.
destruct vd; simpl.
repeat right. apply LocalD_sound.
right. right.
left. exists x,x0,x1,x2. rewrite PTree.gro by auto. auto.
right. apply LocalD_sound.
right; right. left. exists x,x0,x1,x2. rewrite PTree.gro by auto. auto.
right. apply LocalD_sound.
right; right. left. exists x,x0,x1,x2. rewrite PTree.gro by auto. auto.
right. apply LocalD_sound.
right; right. left. exists x,x0,x1,x2. rewrite PTree.gro by auto. auto.
-
destruct (ident_eq i x).
* subst x; rewrite PTree.gss in H; inv H. simpl. auto.
* rewrite PTree.gso in H by auto.
destruct vd; simpl;
repeat right; apply LocalD_sound;
right; right; right; left;
exists x,x0,x1; rewrite PTree.gro by auto; auto.
-
destruct (ident_eq i x).
* subst x; rewrite PTree.gss in H; inv H. simpl. auto.
* rewrite PTree.gso in H by auto.
destruct vd; simpl;
repeat right; apply LocalD_sound;
right; right; right; right; left;
exists x,x0; rewrite PTree.gro by auto; auto.
-
destruct (ident_eq i x).
* subst x; rewrite PTree.gss in H; inv H. simpl. auto.
* rewrite PTree.gso in H by auto.
destruct vd; simpl;
repeat right; apply LocalD_sound;
right; right; right; right; right; left;
exists x,x0; rewrite PTree.gro by auto; auto.
-
destruct (denote_vardesc_prefix ((LocalD T1 (PTree.remove i T2)) Q) i vd) as [L ?].
rewrite H0. rewrite in_app; right.
apply LocalD_sound_other. auto.
+
destruct vd; simpl in H; destruct H; subst.
-
apply LocalD_sound.
right. left. exists i,t,v,v0; split; auto. apply PTree.gss.
-
destruct H; subst.
apply LocalD_sound.
right. right. left. exists i,t,v,v0; split; auto. apply PTree.gss.
apply In_LocalD_remove_set; auto.
-
apply LocalD_sound.
right; right; right. left. exists i,t,v; split; auto. apply PTree.gss.
-
apply In_LocalD_remove_set; auto.
-
apply LocalD_sound.
right; right; right; right. left. exists i,v; split; auto. apply PTree.gss.
-
apply In_LocalD_remove_set; auto.
-
apply LocalD_sound.
do 5 right. left; exists i,v. split; auto. apply PTree.gss.
-
apply In_LocalD_remove_set; auto.
Qed.
Lemma LOCALx_expand_res: forall Q1 T1 T2 Q Q0,
In Q0 (LocalD T1 T2 (Q1 ::Q)) <->
In Q0 (Q1 ::LocalD T1 T2 Q).
Proof.
intros; split; intros.
+ simpl.
apply LocalD_complete in H.
repeat match type of H with
| _ \/ _ => destruct H
| ex _ => destruct H
| _ /\ _ => destruct H
end; subst.
- right.
apply LocalD_sound_temp; auto.
- right.
apply LocalD_sound.
right; left; repeat econstructor; eauto.
-
right.
apply LocalD_sound.
do 2 right; left; repeat econstructor; eauto.
-
right.
apply LocalD_sound.
do 3 right; left; repeat econstructor; eauto.
-
right.
apply LocalD_sound.
do 4 right; left; repeat econstructor; eauto.
-
right.
apply LocalD_sound.
do 5 right; left; repeat econstructor; eauto.
-
destruct H; auto.
right.
apply LocalD_sound_other. auto.
+
destruct H. subst. apply LocalD_sound_other. left; auto.
apply LocalD_complete in H.
apply LocalD_sound.
repeat match type of H with
| _ \/ _ => destruct H
| ex _ => destruct H
| _ /\ _ => destruct H
end; subst.
* do 0 right; left; repeat econstructor; eauto.
* do 1 right; left; repeat econstructor; eauto.
* do 2 right; left; repeat econstructor; eauto.
* do 3 right; left; repeat econstructor; eauto.
* do 4 right; left; repeat econstructor; eauto.
* do 5 right; left; repeat econstructor; eauto.
* do 6 right. right; auto.
Qed.
Lemma LOCALx_shuffle: forall P Q Q' R,
(forall Q0, In Q0 Q' -> In Q0 Q) ->
PROPx P (LOCALx Q (SEPx R)) |-- PROPx P (LOCALx Q' (SEPx R)).
Proof.
intros.
induction Q'; [simpl; intro; normalize |].
pose proof (H a (or_introl _ eq_refl)).
rewrite <- insert_local.
apply andp_right.
+ clear -H0.
induction Q; [inversion H0 |].
rewrite <- insert_local.
simpl in H0; inversion H0.
- subst.
apply andp_left1.
apply derives_refl.
- apply andp_left2.
apply IHQ, H.
+ apply IHQ'.
intros.
apply H.
simpl.
right.
apply H1.
Qed.
Lemma LOCALx_shuffle': forall P Q Q' R,
(forall Q0, In Q0 Q' <-> In Q0 Q) ->
PROPx P (LOCALx Q (SEPx R)) = PROPx P (LOCALx Q' (SEPx R)).
Proof.
intros.
apply pred_ext; apply LOCALx_shuffle; intros; apply H; auto.
Qed.
Lemma LocalD_remove_empty_from_PTree1: forall i T1 T2 Q Q0,
T1 ! i = None ->
(In Q0 (LocalD (PTree.remove i T1) T2 Q) <-> In Q0 (LocalD T1 T2 Q)).
Proof.
intros until Q0; intro G; split; intros;
apply LocalD_sound; apply LocalD_complete in H;
repeat match type of H with
| _ \/ _ => destruct H
| ex _ => destruct H
| _ /\ _ => destruct H
end; subst.
- do 0 right; left. exists x,x0; split; auto.
destruct (ident_eq i x). subst. rewrite PTree.grs in H; inv H.
rewrite PTree.gro in H; auto.
- do 1 right; left. repeat eexists. eauto.
- do 2 right; left. repeat eexists. eauto.
- do 3 right; left. repeat eexists. eauto.
- do 4 right; left. repeat eexists. eauto.
- do 5 right; left. repeat eexists. eauto.
- do 6 right. auto.
- do 0 right; left. exists x,x0; split; auto.
destruct (ident_eq i x). subst. congruence.
rewrite PTree.gro; auto.
- do 1 right; left. repeat eexists. eauto.
- do 2 right; left. repeat eexists. eauto.
- do 3 right; left. repeat eexists. eauto.
- do 4 right; left. repeat eexists. eauto.
- do 5 right; left. repeat eexists. eauto.
- do 6 right. auto.
Qed.
Lemma LocalD_remove_empty_from_PTree2: forall i T1 T2 Q Q0,
T2 ! i = None ->
(In Q0 (LocalD T1 (PTree.remove i T2) Q) <-> In Q0 (LocalD T1 T2 Q)).
Proof.
intros until Q0; intro G; split; intros;
apply LocalD_sound; apply LocalD_complete in H;
repeat match type of H with
| _ \/ _ => destruct H
| ex _ => destruct H
| _ /\ _ => destruct H
end; subst;
try solve [left; repeat eexists; eauto] ;
try solve [repeat right; auto];
try (destruct (ident_eq i x);
[try congruence; subst x; rewrite PTree.grs in H; inv H
| try rewrite PTree.gro in H by auto]).
- do 1 right; left; repeat eexists; eauto.
- do 2 right; left; repeat eexists; eauto.
- do 3 right; left; repeat eexists; eauto.
- do 4 right; left; repeat eexists; eauto.
- do 5 right; left; repeat eexists; eauto.
- do 1 right; left; repeat eexists; rewrite PTree.gro by auto; eauto.
- do 2 right; left; repeat eexists; rewrite PTree.gro by auto; eauto.
- do 3 right; left; repeat eexists; rewrite PTree.gro by auto; eauto.
- do 4 right; left; repeat eexists; rewrite PTree.gro by auto; eauto.
- do 5 right; left; repeat eexists; rewrite PTree.gro by auto; eauto.
Qed.
Lemma subst_lvar: forall i v j t v2,
subst i v (lvar j t v2) = lvar j t v2.
Proof.
intros; unfold subst, lvar.
extensionality rho. simpl. auto.
Qed.
Lemma subst_gvar: forall i v j v1,
subst i v (gvar j v1) = gvar j v1.
Proof.
intros; unfold subst, gvar.
extensionality rho. simpl. auto.
Qed.
Lemma subst_sgvar: forall i v j v1,
subst i v (sgvar j v1) = sgvar j v1.
Proof.
intros; unfold subst, sgvar.
extensionality rho. simpl. auto.
Qed.
Hint Rewrite subst_lvar subst_gvar subst_sgvar : subst.
Lemma LocalD_subst: forall id v Q0 T1 T2 Q,
In Q0 (LocalD (PTree.remove id T1) T2 (map (subst id v) Q)) ->
In Q0 (map (subst id v) (LocalD T1 T2 Q)).
Proof.
intros.
apply in_map_iff.
apply LocalD_complete in H.
repeat match type of H with
| _ \/ _ => destruct H
| ex _ => destruct H
| _ /\ _ => destruct H
end;
try (destruct (peq id x);
[subst; rewrite PTree.grs in H; inv H
| rewrite PTree.gro in H by auto ]).
- exists Q0; split; subst; autorewrite with subst; auto.
apply LocalD_sound_temp; auto.
- exists Q0; split; subst; autorewrite with subst; auto.
eapply LocalD_sound_local_global in H; destruct H; eassumption.
- exists Q0; split; subst; autorewrite with subst; auto.
eapply LocalD_sound_local_global in H; destruct H; eassumption.
- exists Q0; split; subst; autorewrite with subst; auto.
eapply LocalD_sound_local in H; eassumption.
- exists Q0; split; subst; autorewrite with subst; auto.
eapply LocalD_sound_visible_global in H; eassumption.
- exists Q0; split; subst; autorewrite with subst; auto.
eapply LocalD_sound_shadowed_global in H; eassumption.
- apply in_map_iff in H.
destruct H as [x [?H ?H]].
exists x.
split; [auto |].
apply LocalD_sound_other; auto.
Qed.
Lemma SC_remove_subst: forall P T1 T2 R id v old,
PROPx P
(LOCALx (temp id v :: map (subst id `old) (LocalD T1 T2 nil))
(SEPx (map (subst id `old) (map liftx R))))
|-- PROPx P
(LOCALx (LocalD (PTree.set id v T1) T2 nil) (SEPx (map liftx R))).
Proof.
intros.
replace (SEPx (map (subst id `old) (map liftx R))) with (SEPx (map liftx R)).
Focus 2. {
f_equal.
f_equal.
rewrite map_map.
f_equal.
} Unfocus.
apply LOCALx_shuffle; intros.
apply LOCALx_expand_temp_var in H.
destruct H; [left; auto | right].
apply LocalD_subst, H.
Qed.
Lemma local2ptree_sound_aux: forall P Q R Q0 Q1 Q2,
Q1 && local Q0 = Q2 && local Q0 ->
In Q0 Q ->
Q1 && PROPx P (LOCALx Q (SEPx R)) = Q2 && PROPx P (LOCALx Q (SEPx R)).
Proof.
intros.
pose proof in_local _ P _ R H0.
rewrite (add_andp _ _ H1).
rewrite (andp_comm _ (local Q0)).
rewrite <- !andp_assoc.
f_equal.
exact H.
Qed.
Lemma LOCALx_expand_vardesc': forall P R i vd T1 T2 Q,
PROPx P (LOCALx (LocalD T1 (PTree.set i vd T2) Q) (SEPx R)) =
PROPx P (LOCALx (denote_vardesc (LocalD T1 (PTree.remove i T2) Q) i vd) (SEPx R)).
Proof.
intros.
apply LOCALx_shuffle'; intro.
symmetry; apply LOCALx_expand_vardesc.
Qed.
Lemma local_equal_lemma:
forall i t v t' v',
local (lvar i t v) && local (lvar i t' v') =
!!(v' = v) && !!(t'=t) && local (lvar i t' v').
Proof.
intros; extensionality rho.
unfold local, lift1; simpl.
normalize. f_equal. apply prop_ext.
split; intros [? ?].
hnf in H,H0.
destruct (Map.get (ve_of rho) i) as [[? ?] | ] eqn:H8; try contradiction.
destruct (eqb_type t t0) eqn:?; try contradiction.
destruct (eqb_type t' t0) eqn:?; try contradiction.
apply eqb_type_true in Heqb0.
apply eqb_type_true in Heqb1.
subst.
repeat split; auto.
hnf. rewrite H8. rewrite eqb_type_refl. auto.
destruct H0; subst; auto.
Qed.
Lemma local2ptree_soundness: forall P Q R T1 T2 P' Q',
local2ptree Q T1 T2 P' Q' ->
PROPx P (LOCALx Q (SEPx R)) = PROPx (P' ++ P) (LOCALx (LocalD T1 T2 Q') (SEPx R)).
Proof.
intros.
induction H.
+ unfold LocalD.
rewrite !PTree.fold_spec.
simpl.
reflexivity.
+ rewrite <- insert_local.
rewrite IHlocal2ptree.
rewrite insert_local.
destruct (T1 ! i) eqn:H8; inv H0.
Focus 2. {
apply LOCALx_shuffle'; intros.
eapply iff_trans; [apply LOCALx_expand_temp_var |].
simpl.
pose proof LocalD_remove_empty_from_PTree1 i T1 T2 Q' Q0 H8.
tauto. } Unfocus.
simpl app.
rewrite <- move_prop_from_LOCAL.
rewrite <- !insert_local.
(* rewrite IHlocal2ptree. *)
apply local2ptree_sound_aux with (Q0 := temp i v0).
- extensionality rho.
unfold temp.
apply pred_ext; normalize.
- apply LocalD_sound_temp. auto.
+ rewrite <- insert_local.
rewrite IHlocal2ptree; clear IHlocal2ptree.
rewrite insert_local.
destruct (T2 ! i) as [ vd | ] eqn:H9;
try assert (H8 := LOCALx_expand_vardesc i vd T1 T2 Q');
try destruct vd; inv H0.
-
rewrite <- (PTree.gsident _ _ H9) by auto.
rewrite <- insert_local.
rewrite !LOCALx_expand_vardesc'.
simpl app. simpl denote_vardesc.
rewrite <- !canon17, <- !insert_local, <- !andp_assoc.
rewrite local_equal_lemma; auto.
-
rewrite <- (PTree.gsident _ _ H9) by auto.
rewrite <- insert_local.
rewrite !LOCALx_expand_vardesc'.
simpl app. simpl denote_vardesc.
rewrite <- !canon17, <- !insert_local, <- !andp_assoc.
rewrite local_equal_lemma; auto.
-
rewrite <- (PTree.gsident _ _ H9) by auto.
rewrite <- !insert_local.
rewrite !LOCALx_expand_vardesc'.
simpl app. simpl denote_vardesc.
rewrite <- !canon17, <- !insert_local, <- !andp_assoc.
f_equal.
extensionality rho; unfold local, lift1, lvar, gvar; simpl;
normalize.
f_equal; apply prop_ext; intuition.
destruct (Map.get (ve_of rho) i) as [[? ?]|]; intuition.
-
rewrite <- (PTree.gsident _ _ H9) at 1 by auto.
rewrite <- !insert_local.
rewrite !LOCALx_expand_vardesc'.
simpl app. simpl denote_vardesc.
rewrite <- !insert_local, <- !andp_assoc. auto.
-
rewrite <- !insert_local.
rewrite !LOCALx_expand_vardesc'.
simpl denote_vardesc.
rewrite <- !insert_local.
rewrite LOCALx_shuffle'
with (Q:= LocalD T1 (PTree.remove i T2) Q')
(Q':= LocalD T1 T2 Q'); auto.
intro; symmetry; apply (LocalD_remove_empty_from_PTree2); auto.
+
rewrite <- insert_local.
rewrite IHlocal2ptree; clear IHlocal2ptree.
destruct (T2 ! i) as [ vd | ] eqn:H9;
try assert (H8 := LOCALx_expand_vardesc i vd T1 T2 Q');
try destruct vd; inv H0.
-
rewrite <- (PTree.gsident _ _ H9) by auto.
rewrite !LOCALx_expand_vardesc'.
simpl app. simpl denote_vardesc.
rewrite <- !canon17, <- !insert_local, <- !andp_assoc.
f_equal.
extensionality rho; unfold local, lift1, lvar, gvar; simpl;
normalize.
f_equal; apply prop_ext; intuition.
destruct (Map.get (ve_of rho) i) as [[? ?]|]; intuition.
-
rewrite <- (PTree.gsident _ _ H9) by auto.
rewrite !LOCALx_expand_vardesc'.
simpl app. simpl denote_vardesc.
rewrite <- !canon17, <- !insert_local, <- !andp_assoc.
f_equal.
extensionality rho; unfold local, lift1, lvar, gvar; simpl;
normalize.
f_equal; apply prop_ext; intuition.
destruct (Map.get (ve_of rho) i) as [[? ?]|]; intuition.
-
rewrite <- (PTree.gsident _ _ H9) by auto.
rewrite !LOCALx_expand_vardesc'.
simpl app. simpl denote_vardesc.
rewrite <- !canon17, <- !insert_local, <- !andp_assoc.
f_equal.
extensionality rho; unfold local, lift1, lvar, gvar; simpl;
normalize.
f_equal; apply prop_ext; split; intros [? ?].
destruct (Map.get (ve_of rho) i) as [[? ?]|]; intuition.
destruct (ge_of rho i); intuition. subst; auto.
destruct (Map.get (ve_of rho) i) as [[? ?]|]; intuition.
subst; auto.
-
rewrite <- (PTree.gsident _ _ H9) at 1 by auto.
rewrite !LOCALx_expand_vardesc'.
simpl app. simpl denote_vardesc.
rewrite <- !canon17, <- !insert_local, <- !andp_assoc.
f_equal.
extensionality rho; unfold local, lift1, lvar, gvar, sgvar; simpl;
normalize.
f_equal; apply prop_ext; split; intros [? ?].
destruct (Map.get (ve_of rho) i) as [[? ?]|]; intuition.
destruct (ge_of rho i); intuition. subst; auto.
destruct (Map.get (ve_of rho) i) as [[? ?]|]; intuition.
subst; auto.
-
rewrite !LOCALx_expand_vardesc'.
simpl denote_vardesc.
rewrite <- !insert_local.
rewrite LOCALx_shuffle'
with (Q:= LocalD T1 (PTree.remove i T2) Q')
(Q':= LocalD T1 T2 Q'); auto.
intro; symmetry; apply (LocalD_remove_empty_from_PTree2); auto.
+
rewrite <- insert_local.