-
Notifications
You must be signed in to change notification settings - Fork 0
/
Globalize.v
4103 lines (3825 loc) · 136 KB
/
Globalize.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 stdpp.tactics.
Require Import stdpp.fin_sets.
Require Import LocalTactics.
Require Import Coq.Lists.List.
Import ListNotations.
Require Import DecEq.
Require Import SXML.
Require Import Steps.
Require Import IO.
Require Import SyntaxParams.
Require Import HeapParams.
Module Globalization(varParams: SyntaxParams)(heapParams: HeapParams).
Module Import SXML_ := SXML.SXML varParams heapParams.
(************
Definitions for globalization
************)
Fixpoint elim_defs
(vars: list var) (e: exp): exp
:=
match e with
| letsmall x se er =>
if member x vars
then (elim_defs vars er)
else letsmall x se (elim_defs vars er)
| letabs x xl el er =>
if member x vars
then (elim_defs vars er)
else letabs x xl (elim_defs vars el) (elim_defs vars er)
| branch vb et ef =>
branch vb
(elim_defs vars et)
(elim_defs vars ef)
| tail vf va => tail vf va
| ret x => ret x
end.
Theorem elim_defs_nil :
forall e, elim_defs [] e = e.
Proof.
intros. induction e; simpl.
- rewrite -> IHe. reflexivity.
- rewrite -> IHe1. rewrite -> IHe2. reflexivity.
- rewrite -> IHe1. rewrite -> IHe2. reflexivity.
- reflexivity.
- reflexivity.
Qed.
Hint Resolve elim_defs_nil.
Lemma elim_defs_app :
forall ds1 ds2 e,
elim_defs ds1 (elim_defs ds2 e) =
elim_defs (ds1 ++ ds2) e.
Proof.
intros.
induction e; simpl; try solve [f_equal; assumption].
(* let small *)
- destruct (member v ds2); destruct (member v (ds1 ++ ds2)); simpl.
+ exact IHe.
+ destruct (member v ds1);
exfalso; apply n; apply in_or_app; right; assumption.
+ destruct (member v ds1).
* exact IHe.
* rewrite -> in_app_iff in i.
destruct i; contradiction.
+ destruct (member v ds1).
* exfalso. apply n0. apply in_or_app.
left. exact i.
* f_equal. exact IHe.
(* let abs *)
- destruct (member v ds2); destruct (member v (ds1 ++ ds2)); simpl.
+ exact IHe2.
+ exfalso. apply n. apply in_or_app; right; assumption.
+ destruct (member v ds1).
* exact IHe2.
* rewrite -> in_app_iff in i.
destruct i; contradiction.
+ destruct (member v ds1).
* exfalso. apply n0. apply in_or_app; left; assumption.
* f_equal; assumption.
Qed.
Hint Resolve elim_defs_app.
Lemma elim_defs_comm :
forall ds1 ds2 e,
elim_defs ds1 (elim_defs ds2 e) =
elim_defs ds2 (elim_defs ds1 e).
Proof.
intros.
induction e; simpl;
try solve [f_equal; assumption].
- destruct (member v ds1); destruct (member v ds2).
+ assumption.
+ rewrite <- IHe. simpl.
destruct (member v ds1); try contradiction. reflexivity.
+ rewrite -> IHe. simpl.
destruct (member v ds2); try contradiction. reflexivity.
+ simpl; destruct (member v ds1); destruct (member v ds2);
try contradiction.
f_equal. assumption.
- destruct (member v ds1); destruct (member v ds2).
+ assumption.
+ rewrite <- IHe2. simpl.
destruct (member v ds1); try contradiction. reflexivity.
+ rewrite -> IHe2. simpl.
destruct (member v ds2); try contradiction. reflexivity.
+ simpl; destruct (member v ds1); destruct (member v ds2);
try contradiction.
f_equal; assumption.
Qed.
Inductive defined var
:=
| dtuple : list var -> defined var
| dabs : var -> exp -> defined var
| dconst : bool -> defined var
.
Inductive defined_svalue (defs2: list (var * value)) : defined var -> svalue -> Prop
:=
| defined_stuple xs vals :
searches xs defs2 = Some vals ->
defined_svalue defs2 (dtuple _ xs) (stuple vals)
| defined_sclos xl cl el :
(forall x,
free_in x cl ->
x <> xl ->
exists v,
defs2 !! x = Some v /\ el !! x = Some v) ->
defined_svalue defs2 (dabs _ xl cl) (sclos (xl, cl, el))
.
Inductive defined_value : defined var -> value -> Prop
:=
| defined_tuple_vaddr xs a :
defined_value (dtuple _ xs) (vaddr a)
| defined_clos_vaddr xl el a :
defined_value (dabs _ xl el) (vaddr a)
| defined_vconst b :
defined_value (dconst _ b) (vconst b)
.
Definition bind_defined (x: var) (def: defined var) (er: exp): exp
:=
match def with
| dtuple _ se => letsmall x (tupleExp se) er
| dconst _ b => letsmall x (constExp b) er
| dabs _ xl el => letabs x xl el er
end.
Fixpoint bind_globals
(terms: list (var * defined var)) (e: exp) : exp
:=
match terms with
| [] => e
| (bv, bd) :: ts => bind_globals ts (bind_defined bv bd e)
end.
Lemma bind_globals_app :
forall t1 t2 e,
bind_globals (t1 ++ t2) e =
bind_globals t2 (bind_globals t1 e).
Proof with eauto.
intros. generalize dependent e.
induction t1; intro e...
simpl. destruct a. apply IHt1.
Qed.
(* Definition of globalize *)
Definition globalize
(terms: list (var * defined var)) (e: exp) : exp
:=
bind_globals terms (elim_defs (map fst terms) e).
Lemma globalize_nil :
forall p,
globalize [] p = p.
Proof.
intros; unfold globalize; simpl.
apply elim_defs_nil.
Qed.
Hint Resolve globalize_nil.
Inductive var_in_def : var -> defined var -> Prop
:=
| dv_tuple v vs :
In v vs ->
var_in_def v (dtuple _ vs)
| dv_abs v xl el :
free_in v el ->
v <> xl ->
var_in_def v (dabs _ xl el)
.
Hint Constructors var_in_def.
Inductive defs_well_scoped : list (var * defined var) -> Type
(* in Type for random technical reasons to do with existentials *)
:=
| dv_nil : defs_well_scoped []
| dv_cons v d ds :
(forall v,
var_in_def v d ->
In v (map fst ds)) ->
defs_well_scoped ds ->
defs_well_scoped ((v, d) :: ds)
.
Hint Constructors defs_well_scoped.
Lemma defs_well_scoped_drop : forall defs n,
defs_well_scoped defs ->
defs_well_scoped (drop n defs).
Proof with eauto.
intros.
generalize dependent n.
induction defs; intros.
- destruct n...
- destruct n; simpl.
+ exact X.
+ inversion X; subst.
apply IHdefs...
Qed.
Lemma map_option_all_some {X Y} : forall (f: X -> option Y) vs,
(forall v, In v vs -> { v2 : Y & f v = Some v2 }) ->
{ vs2 & map_option f vs = Some vs2 }.
Proof.
intros. induction vs; simpl.
- eexists; reflexivity.
- destruct (X0 a) as [v2 ->].
+ repeat constructor.
+ destruct IHvs as [vs2 ->].
* intros. apply X0.
right. auto.
* eexists; reflexivity.
Qed.
(************
Proofs about globalization
************)
(* ..., If it's concrete the instance resolver has no problem,
no clue why it needs the extra help *)
Definition def_in_scope bd (e : env) := forall v, var_in_def v bd -> { vl & search v e = Some vl }.
Theorem lookup_store_path_after_extend :
forall (h h': heap svalue) (a: addr) (sval: svalue),
forall (alloc1: alloc h sval = (a, h')),
forall (path : store_path) (v1 v2: value),
lookup_store_path h v1 path = Some v2 ->
lookup_store_path h' v1 path = Some v2.
Proof.
intros.
generalize dependent v1.
pose proof (heap_lookup_earlier _ _ _ _ alloc1) as lookup_carry.
induction path; intros; simpl in *; destruct v1; try done;
destruct (h !! a0) eqn:lookup1; subst; try discriminate;
rewrite -> (heap_lookup_some_later _ _ _ _ alloc1 a0 _ lookup1);
repeat case_match; subst; try done;
apply IHpath;
assumption.
Qed.
(* Define our relations between machine states *)
(* TODO move to set *)
Definition image {X Y XS YS : Type} `{Elements X XS} `{Empty YS, Union YS, Singleton Y YS} (f: X -> option Y) (xs : XS) : YS :=
list_to_set
(filter_map (fun x => f x)
(elements xs)).
(*
Definition is_image {X Y XS YS} `{ElemOf X XS} `{ElemOf Y YS} (f : X -> option Y) (xs : XS) (ys: YS) :=
forall y,
elem_of y ys <-> exists x, elem_of x xs /\ f x = Some y.
Theorem image_is_image {X Y XS YS} `{FinSet X XS} `{SemiSet Y YS} (f : X -> option Y) (xs : XS) :
is_image f xs (image (YS:=YS) f xs).
Proof with auto.
intros.
unfold is_image, image.
intro y; split; intros.
- rewrite -> elem_of_union_list in H12.
destruct H12 as [X0 [X0In yIn]].
set_unfold.
destruct X0In as [x [? ?]].
exists x. subst; set_unfold.
done.
- rewrite -> elem_of_union_list.
set_unfold.
destruct H12 as [x [xIn ?]]; subst.
eexists.
split.
+ exists x...
+ rewrite -> H12. set_solver.
Qed.
Instance set_unfold_image {X Y XS YS} `{FSX: FinSet X XS} `{FSY: SemiSet Y YS} {f: X -> option Y} {xs: XS} {y: Y}:
SetUnfoldElemOf y (image f xs) (exists x, elem_of x xs /\ f x = Some y).
Proof.
constructor.
apply image_is_image.
Qed.
*)
Lemma set_union_elements :
forall {X XS} `{FinSet X XS},
forall (xs: XS),
equiv (union_list (map singleton (elements xs))) xs.
Proof with eauto.
intros.
set_unfold.
split; intros.
- rewrite -> elem_of_union_list in H7.
destruct H7 as [X0 [X0In xIn]].
remember (elements xs) as xs'.
generalize dependent xs.
induction xs'; intros; subst; simpl in *.
+ apply not_elem_of_nil in X0In. contradiction.
+ set_unfold.
destruct X0In.
(* head *)
* subst. set_unfold. subst.
rewrite <- elem_of_elements.
rewrite <- Heqxs'.
constructor.
(* rest *)
* destruct H7 as [? [? yIn]].
subst. set_unfold. subst.
rewrite <- elem_of_elements.
rewrite -> elem_of_list_In in *.
rewrite <- Heqxs'.
right. assumption.
- rewrite -> elem_of_union_list.
exists (singleton x).
set_unfold.
split...
Qed.
Lemma image_union (addrs1 addrs2: addrs) f :
equiv (image f (union addrs1 addrs2))
(union (image f addrs1 : addrs) (image f addrs2 : addrs)).
Proof with eauto.
intros.
unfold image.
set_unfold.
intro x.
split; intros; deep_set_unfold.
- destruct H...
- destruct H; deprod...
Qed.
(*
Lemma image_singleton (a1: addr) (af: addr -> option addr) :
equiv (image af ({[ a1 ]} : addrs))
(match (af a1) with
| Some a2 => {[ a2 ]}
| None => empty
end: addrs).
Proof with eauto.
unfold image. set_unfold.
intro.
rewrite -> elem_of_union_list.
split; intros; deep_set_unfold...
repeat eexists...
set_solver.
Qed.
*)
(* We need to have both future and past defs, because lifting a lambda will require eliminating
definitions before lifting it *)
Fixpoint defs_agree (defs: list (var * defined var)) (c: exp) {struct c} : Prop
:=
match c with
| letsmall v s cr =>
match s with
| tupleExp vs =>
match search v defs with
| Some (dtuple _ vs') => vs = vs' /\ defs_agree defs cr
| Some _ => False
| None => defs_agree defs cr
end
| constExp b =>
match search v defs with
| Some (dconst _ b') => b = b' /\ defs_agree defs cr
| Some _ => False
| None => defs_agree defs cr
end
| _ =>
match member v (map fst defs) with
| left _ => False
| right _ => defs_agree defs cr
end
end
| letabs v vl cl cr =>
match search v defs with
| Some (dabs _ vl' cl') => ~ In vl (map fst defs) /\ vl = vl' /\ elim_defs (map fst defs) cl = cl' /\
defs_agree defs cl /\ defs_agree defs cr
| Some _ => False
| None => ~ In vl (map fst defs) /\ defs_agree defs cl /\ defs_agree defs cr
end
| branch vb ct cf =>
defs_agree defs ct /\ defs_agree defs cf
| tail _ _ => True
| ret _ => True
end.
Section Relations.
Definition address_relation := list (addr * addr).
Definition code_related (defs : list (var * defined var)) (remaining: nat) c cg
:= bind_globals (take remaining defs) (elim_defs (map fst defs) c) = cg /\
defs_agree defs c.
Inductive val_related (ar: address_relation) : value -> value -> Prop :=
| vconst_related b : val_related ar (vconst b) (vconst b)
| vaddr_related a ag :
search a ar = Some ag ->
val_related ar (vaddr a) (vaddr ag).
Inductive env_related (ar: address_relation) defspast (xs xsg : vars): env -> env -> Prop :=
| env_vals_related e eg:
forall
(live_vars:
forall x,
elem_of x xs ->
e !! x <> None)
(pairs_related:
forall x v,
elem_of x xs ->
elem_of x xsg ->
e !! x = Some v ->
exists vg,
eg !! x = Some vg /\
val_related ar v vg)
(global_vals_agree:
forall (x: var) (v vg: value),
In (x, vg) defspast ->
elem_of x xs ->
e !! x = Some v ->
val_related ar v vg)
(globals_present:
forall (x: var) (vg: value),
In (x, vg) defspast ->
elem_of x xsg ->
eg !! x = Some vg),
env_related ar defspast xs xsg e eg.
Inductive clos_related (ar: address_relation) (defs : list (var * defined var)) (defspast : list (var * value)): clos -> clos -> Prop :=
| clos_parts_related xl el cl elg clg :
code_related defs 0 cl clg ->
~ In xl (map fst defspast) ->
env_related ar defspast
(difference (free_vars cl) {[ xl ]})
(difference (free_vars clg) {[ xl ]}) el elg ->
clos_related ar defs defspast (xl, cl, el) (xl, clg, elg).
Inductive sval_related ar defs defspast : svalue -> svalue -> Prop :=
| stuple_related vs vsg :
Forall2 (val_related ar) vs vsg ->
sval_related ar defs defspast (stuple vs) (stuple vsg)
| sclos_related clos closg :
clos_related ar defs defspast clos closg ->
sval_related ar defs defspast (sclos clos) (sclos closg).
Inductive stack_related ar defs defspast : list clos -> list clos -> Prop :=
| stack_nil_related :
stack_related ar defs defspast [] []
| stack_cons_related clos k closg kg :
clos_related ar defs defspast clos closg ->
stack_related ar defs defspast k kg ->
stack_related ar defs defspast (clos :: k) (closg :: kg).
Inductive heap_related ar defs defspast (h hg : heap svalue) :=
| heap_vals_related :
forall
(related_addrs:
forall (a: addr) sv, h !! a = Some sv ->
exists ag svg,
search a ar = Some ag /\
hg !! ag = Some svg /\
sval_related ar defs defspast sv svg)
(global_addrs:
(* Each variable has a past definitions
only for bindings before it *)
forall x v,
In (x, v) defspast ->
match v with
(* incorrect value handled in env *)
| vaddr ag =>
exists d svg,
In (x, d) defs /\
hg !! ag = Some svg /\
defined_svalue defspast d svg
| _ => True
end),
heap_related ar defs defspast h hg.
Inductive state_related ar defs defspast remaining : state -> state -> Prop :=
| state_parts_related c e h k cg eg hg kg :
forall
(* addresses handled in heap *)
(defspast_sound_val:
forall x v, In (x, v) defspast -> exists d,
In (x, d) defs /\
defined_value d v)
(defspast_same :
map fst defspast = drop remaining (map fst defs))
(defspast_valid :
valid_in_heap defspast hg)
(ar_heap_dom:
forall a ag,
In (a, ag) ar ->
h !! a <> None /\ hg !! ag <> None)
(code_rel:
code_related defs remaining c cg)
(env_rel:
env_related ar defspast (free_vars c) (free_vars cg) e eg)
(heap_rel:
heap_related ar defs defspast h hg)
(stack_rel:
stack_related ar defs defspast k kg)
(ar_nodup:
NoDup (map fst ar)),
state_related ar defs defspast remaining (<< c, e, h, k >>) (<< cg, eg, hg, kg >>)
.
Lemma exists_fun_p {X Y} (P : Y -> Prop) :
forall (x: X) (y: Y) (f: X -> option Y),
(exists y, f x = Some y /\ P y) ->
f x = Some y ->
P y.
Proof.
intros.
destruct H as [y0 [? ?]]; simplify_eq.
done.
Qed.
Lemma env_lookup_related : forall ar defspast xs xsg e eg x v vg,
env_related ar defspast xs xsg e eg ->
elem_of x xs ->
elem_of x xsg ->
e !! x = Some v ->
eg !! x = Some vg ->
val_related ar v vg.
Proof with eauto.
intros.
destruct H.
apply exists_fun_p with x (eg !!)...
Qed.
Lemma env_lookups_related : forall ar defspast fvs fvsg e eg xs vs vgs,
env_related ar defspast fvs fvsg e eg ->
(forall x, In x xs -> elem_of x fvs) ->
(forall x, In x xs -> elem_of x fvsg) ->
searches xs e = Some vs ->
searches xs eg = Some vgs ->
Forall2 (val_related ar) vs vgs.
Proof.
unfold searches.
intros.
generalize dependent vgs.
generalize dependent vs.
induction xs; simpl in *; intros vs eqvs vgs eqvgs; simplify_eq.
- constructor.
- forced (search a e).
forced (map_option (flip search e) xs).
forced (search a eg).
forced (map_option (flip search eg) xs).
simplify_eq.
constructor.
{ eapply env_lookup_related; eauto. }
apply (IHxs (fun x fi => H0 x (or_intror fi)) (fun x fi => H1 x (or_intror fi)) l eq_refl l0 eq_refl).
Qed.
Lemma heap_lookup_related : forall {ar defs defspast h hg a ag sv svg},
heap_related ar defs defspast h hg ->
search a ar = Some ag ->
h !! a = Some sv ->
hg !! ag = Some svg ->
sval_related ar defs defspast sv svg.
Proof with eauto.
intros.
destruct H.
destruct (related_addrs a sv H1).
deprod.
simplify_eq...
Qed.
End Relations.
Hint Constructors val_related env_related clos_related sval_related stack_related heap_related state_related : related.
Hint Resolve env_lookup_related env_lookups_related heap_lookup_related : related.
(* For consistency, some relations are constructors with one variant,
so force them to unfold during search *)
Ltac unfold_simple_related :=
repeat match goal with
| H: code_related _ _ _ _ |- _ => unfold code_related in H
| H: clos_related _ _ _ _ |- _ => destruct H
| H: env_related _ _ _ _ |- _ => destruct H
| H: heap_related _ _ _ _ |- _ => destruct H
end.
(* Compatibility over heap extensions in both left and right sides *)
(* Extending environments by related values are related *)
Lemma val_relate_alloc :
forall a ag ar v1 v2,
~ addr_in v1 a ->
val_related ar v1 v2 ->
val_related ((a, ag) :: ar) v1 v2.
Proof with eauto.
intros.
inversion H0; subst; constructor.
simpl.
destruct (decEq a0 a); subst...
- exfalso...
Qed.
Lemma env_relate_new :
forall ar defspast xs xsg e1 e2 x v1 v2,
env_related ar defspast xs xsg e1 e2 ->
~ In x (map fst defspast) ->
val_related ar v1 v2 ->
env_related ar defspast (union xs {[x]}) (union xsg {[x]}) ((x, v1) :: e1) ((x, v2) :: e2).
Proof with eauto.
intros.
destruct H.
constructor; intros; unfold lookup in *.
- simpl in *.
destruct (decEq x0 x); simplify_eq.
+ discriminate.
+ set_unfold. destruct H...
- simpl in *. destruct (decEq x0 x); simplify_eq.
+ exists v2. split...
+ apply pairs_related...
set_unfold. destruct H... contradiction.
set_unfold. destruct H2... contradiction.
- simpl in *.
destruct (decEq x0 x); simplify_eq.
+ exfalso. apply H0. eauto with searches.
+ apply global_vals_agree with x0...
set_unfold. forced H2...
- simpl in *.
destruct (decEq x0 x); simplify_eq.
+ exfalso. apply H0. eauto with searches.
+ apply globals_present...
set_unfold.
destruct H2...
contradiction.
Qed.
Lemma env_relate_alloc :
forall ar defspast xs xsg e1 e2 a ag,
env_related ar defspast xs xsg e1 e2 ->
~ addr_in (e1, xs) a ->
env_related ((a, ag) :: ar) defspast xs xsg e1 e2.
Proof with eauto.
intros.
unfold addr_in, addr_in_env, not in H0.
destruct H. constructor...
(* pairs_related *)
- intros.
pose proof (pairs_related x v H H1 H2).
deprod.
exists vg. split...
apply val_relate_alloc...
intro.
inversion H4; subst.
eauto 10 with addr_in.
eauto 10 with addr_in.
(* *)
- intros.
pose proof (global_vals_agree x v vg H H1).
apply val_relate_alloc...
intro.
apply H0.
eauto 10 with addr_in.
Qed.
Lemma env_relate_subset :
forall ar defspast xs1 xs2 xsg1 xsg2 e1 e2,
env_related ar defspast xs2 xsg2 e1 e2 ->
subseteq xs1 xs2 ->
subseteq xsg1 xsg2 ->
env_related ar defspast xs1 xsg1 e1 e2.
Proof with eauto.
intros.
set_unfold.
destruct H; split; intros...
Qed.
Lemma search_head :
forall (a1 a2 : addr) ar,
search a1 ((a1, a2) :: ar) = Some a2.
Proof.
intros. simpl. rewrite -> decEq_refl. reflexivity.
Qed.
Hint Resolve env_relate_new env_relate_alloc : related.
Lemma stack_relate_alloc defs defspast a1 a2 ar s sg :
stack_related ar defs defspast s sg ->
~ addr_in s a1 ->
stack_related ((a1, a2) :: ar) defs defspast s sg.
Proof with eauto.
intros.
induction H; constructor.
-- inversion H; subst. constructor...
assert (~ addr_in (el, difference (free_vars cl) {[xl]}) a1).
{ intro. apply H0. eauto with addr_in. }
assert (forall x, elem_of x (free_vars cl ∖ {[xl]}) -> free_in x cl).
{ intros. set_unfold. deprod... rewrite <- in_free_vars_iff_free_in... }
eauto with related.
-- apply IHstack_related.
eauto with addr_in.
Qed.
Hint Constructors val_related : related.
Hint Resolve search_head : related.
Hint Resolve val_relate_alloc stack_relate_alloc : related.
(************
Safety hypotheses
***********)
Definition defs2_addrs (defs2: list (var * value)): addrs :=
list_to_set (filter_map (fun x => value_to_addr (x.2)) defs2).
Definition clos_size_g defVars (cls: clos): nat
:= match cls with
| (xl, el, cl) => 1 + size (difference (difference (free_vars el) {[xl]}) (list_to_set defVars))
end.
Definition svalue_size_g defVars (sv : svalue): nat
:=
match sv with
| sclos cls => clos_size_g defVars cls
| stuple vs => 1 + length vs
end.
Definition addrs_space_g defVars h (addrs1: addrs) :=
space_of (svalue_size_g defVars) h addrs1.
Instance addrs_space_g_equiv {h defVars} : Proper (equiv ==> eq) (addrs_space_g defVars h).
Proof.
unfold Proper, respectful.
intros.
unfold addrs_space_g.
enough (Permutation (elements x) (elements y)).
unfold space_of.
rewrite -> H0. done.
rewrite -> H.
reflexivity.
Qed.
Lemma clos_size_g_lt :
forall defVars clos,
clos_size_g defVars clos <= clos_size clos.
Proof.
intros.
unfold clos_size_g, clos_size.
destruct clos0 as [[xl cl] el].
enough (size (free_vars cl ∖ {[xl]} ∖ list_to_set defVars) <= size (free_vars cl ∖ {[xl]})) by lia.
apply subseteq_size.
set_solver.
Qed.
Lemma svalue_size_g_lt :
forall defVars sv,
svalue_size_g defVars sv <= svalue_size sv.
Proof with eauto.
intros.
destruct sv...
simpl. apply clos_size_g_lt.
Qed.
Lemma lookup_size_g_lt :
forall defVars h sv,
lookup_size (svalue_size_g defVars) h sv <= lookup_size svalue_size h sv.
Proof with eauto.
intros.
unfold lookup_size.
destruct (h !! sv)...
apply svalue_size_g_lt.
Qed.
Lemma addrs_space_g_lt :
forall defVars h addrs1,
addrs_space_g defVars h addrs1 <= space_of svalue_size h addrs1.
Proof with eauto.
intros.
unfold addrs_space_g, space_of.
induction (elements addrs1)...
simpl.
pose proof (lookup_size_g_lt defVars h a).
lia.
Qed.
Inductive globalize_safe (ar: address_relation) (defVars: list var) (defs2: list (var * value)) m n : state -> state -> Prop
:=
mk_globalize_safe c e h k cg eg hg kg :
forall
(af_safe : forall (addrs1: addrs),
closed h addrs1 ->
(1 + length defs2) * addrs_space0 h addrs1 + m >=
addrs_space0 hg (union (image (flip search ar) addrs1) (defs2_addrs defs2)))
(af_safe_g : forall (addrs1: addrs),
closed h addrs1 ->
addrs_space0 h addrs1 + n >=
addrs_space_g defVars hg (union (image (flip search ar) addrs1) (defs2_addrs defs2))),
globalize_safe
ar defVars defs2 m n
(<< c, e, h, k >>)%interp
(<< cg, eg, hg, kg >>)%interp.
(************
Main proofs of efficiency / justification
- Initial steps: pivot through the globals in the target program,
- Make single step with same head
- Pivot through skipped source steps, leaving steps with the same head
Thankfully for the single steps, we can split them up,
but in the others, Prop-Set divisions mean that they must be together
************)
Lemma closure_union_list :
forall h addrss,
heap_valid h ->
equiv
(closure _ h (union_list addrss))
(union_list (map (closure _ h) addrss)).
Proof with eauto.
intros.
induction addrss; simpl.
- apply closure_empty...
- rewrite -> closure_union...
rewrite -> IHaddrss.
reflexivity.
Qed.
Instance image_proper {f} : Proper (equiv ==> equiv) (image f : addrs -> addrs).
Proof.
unfold Proper, respectful.
intros.
set_unfold.
set_solver.
Qed.
Lemma free_vars_elim_defs :
forall c defVars,
free_vars (elim_defs defVars c) ⊆ free_vars c ∪ list_to_set defVars.
Proof.
intros c defVars. deep_set_unfold.
rename H into x_in.
induction c; simpl; set_unfold.
- destruct (member v defVars) eqn:v_in_defs;
simpl in *; set_unfold.
+ destruct (decEq x v); subst.
* right. apply elem_of_list_In; done.
* apply IHc in x_in.
destruct x_in; eauto.
+ destruct x_in.
* eauto.
* destruct H.
apply IHc in H.
destruct H; eauto.
- destruct (member v defVars) eqn:v_in_defs;
simpl in *; set_unfold.
+ apply IHc2 in x_in. destruct x_in; eauto.
destruct (decEq x v); subst.
* right. apply elem_of_list_In; done.
* left. right. done.
+ destruct x_in.
* destruct H. apply IHc1 in H.
destruct H; eauto.
* destruct H; apply IHc2 in H.
destruct H; eauto.
- destruct x_in; subst; eauto.
destruct H.
+ apply IHc1 in H.
destruct H; eauto.
+ apply IHc2 in H.
destruct H; eauto.
- left. done.
- left. set_solver.
Qed.
Lemma free_vars_bind_globals :
forall c defs1 defs2,
defs_well_scoped (defs1 ++ defs2) ->
free_vars (bind_globals defs1 c) ⊆ free_vars c ∪ list_to_set (map fst (defs1 ++ defs2)).
Proof with eauto with searches free_in.
intros.
generalize dependent c.
induction defs1; intros.
- simpl. set_solver.
- inversion X; subst.
simpl. pose proof (IHdefs1 X0 (bind_defined v d c)).
transitivity (free_vars (bind_defined v d c) ∪ list_to_set (map fst (defs1 ++ defs2)))...
apply union_least; try solve [clear; set_solver].
clear H.
destruct d; simpl.
+ set_unfold. intros.
pose proof (H0 x) as ix.
rewrite <- elem_of_list_In in ix.
deep_set_unfold.
destruct H; deprod...
right. right...
+ set_unfold. intros.
pose proof (H0 x) as ix.
rewrite <- elem_of_list_In in ix.
deep_set_unfold.
destruct H; deprod...
right. right.
apply ix. constructor...
+ set_unfold. intros.
forced H. deprod.
left...
Qed.
Lemma free_vars_related :
forall c defs remaining,
defs_well_scoped defs ->
free_vars (bind_globals (take remaining defs) (elim_defs (map fst defs) c)) ⊆
free_vars c ∪ list_to_set (map fst defs).
Proof with eauto.
intros.
transitivity (free_vars (elim_defs (map fst defs) c) ∪ list_to_set (map fst defs)).
- rewrite <- (take_drop remaining defs) at 2 3 4.
apply free_vars_bind_globals.
rewrite -> take_drop. exact X.
- apply union_least...
+ apply free_vars_elim_defs.
+ set_unfold. intros. deep_set_unfold.
right. exists (v, d).
repeat rewrite -> elem_of_list_In in *.
split...
Qed.
(*** Initial Steps ***)
(* Prove only safety, first, the relatedness comes in trivially
since the source program hasn't moved, and including it in here adds
extra requirements that muddy the induction.
We'll make sure all globals are live though and then re-establish relation. *)
(* (addrs_space0 (iheap st) (defs2_addrs defs2)) *)
(* use code relation to split free variables correctly *)
(* *)
Definition has_globals (defs2 : list (var * value)) (eg: env): Prop
:=
forall (x: var) v,
eg !! x = Some v <->
defs2 !! x = Some v.
Lemma in_fst_pair : forall {A B} x (ps : list (A * B)),
In x (map fst ps) ->
exists y, In (x, y) ps.
Proof with auto.
intros.
induction ps; try contradiction.
destruct H; subst.
- destruct a; simpl.
exists b...
- destruct (IHps H) as [y iny].
exists y. right...
Qed.
Lemma defined_svalue_extend : forall d sv defs x v,
~ In x (map fst defs) ->
defined_svalue defs d sv ->
defined_svalue ((x, v) :: defs) d sv.
Proof with eauto.
intros.
destruct H0; constructor.
- unfold searches in *.
generalize dependent vals.
induction xs; intros vals lkps.
+ simpl in *; congruence.
+ simpl in *. destruct (decEq a x).
{ subst. exfalso. apply H.
forced (search x defs). eauto with searches. }
forced (search a defs).
forced (map_option (flip search defs) xs).
simplify_eq.
rewrite -> IHxs with l...