-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowSensitiveFvSLH.v
More file actions
1874 lines (1749 loc) · 101 KB
/
FlowSensitiveFvSLH.v
File metadata and controls
1874 lines (1749 loc) · 101 KB
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
(** * Flow-Sensitive, Flexible Value SLH *)
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
From Coq Require Import Strings.String.
From SECF Require Import Maps SpecCT UltimateSLH_optimized FiSLH FvSLH.
From Coq Require Import Bool.Bool.
From Coq Require Import Arith.Arith.
From Coq Require Import Arith.EqNat.
From Coq Require Import Arith.PeanoNat. Import Nat.
From Coq Require Import Lia.
From Coq Require Import List. Import ListNotations.
From Coq Require Import Logic.FunctionalExtensionality.
Set Default Goal Selector "!".
(** * Flow-sensitive IFC tracking for flex_slh *)
(* Since we want to apply flex_slh to all programs, without rejecting anything
as "ill-typed", we implement flow-sensitive static IFC tracking: *)
Inductive acom : Type :=
| ASkip
| AAsgn (x : string) (e : aexp)
| ASeq (c1 c2 : acom) (P : pub_vars) (PA : pub_arrs)
| AIf (be : bexp) (lbe : label) (c1 c2 : acom)
| AWhile (be : bexp) (lbe : label) (c : acom) (P : pub_vars) (PA : pub_arrs)
| AARead (x : string) (lx : label) (a : string) (i : aexp) (li : label)
| AAWrite (a : string) (i : aexp) (li : label) (e : aexp)
| ABranch (lbl:label) (c:acom).
Declare Custom Entry acom.
Notation "'<[' e ']>'" := e (at level 0, e custom acom at level 99) : com_scope.
Notation "( x )" := x (in custom acom, x at level 99) : com_scope.
Notation "x" := x (in custom acom at level 0, x constr at level 0) : com_scope.
Notation "f x .. y" := (.. (f x) .. y)
(in custom acom at level 0, only parsing,
f constr at level 0, x constr at level 9,
y constr at level 9) : com_scope.
Notation "'skip'" :=
ASkip (in custom acom at level 0) : com_scope.
Notation "x := y" :=
(AAsgn x y)
(in custom acom at level 0, x constr at level 0,
y custom acom at level 85, no associativity) : com_scope.
Notation "x ; '@(' P ',' PA ')' y" :=
(ASeq x y P PA)
(in custom acom at level 90, right associativity) : com_scope.
Notation "'if' x '@' lbe 'then' y 'else' z 'end'" :=
(AIf x lbe y z)
(in custom acom at level 89, x custom acom at level 99,
y at level 99, z at level 99) : com_scope.
Notation "'while' x '@' lbe 'do' y '@(' P ',' PA ')' 'end'" :=
(AWhile x lbe y P PA)
(in custom acom at level 89, x custom acom at level 99, y at level 99) : com_scope.
Notation "x '@@' lx '<-' a '[[' i '@' li ']]'" :=
(AARead x lx a i li)
(in custom acom at level 0, x constr at level 0,
a at level 85, i custom acom at level 85, no associativity) : com_scope.
Notation "a '[' i '@' li ']' '<-' e" :=
(AAWrite a i li e)
(in custom acom at level 0, a constr at level 0,
i custom acom at level 0, e custom acom at level 85,
no associativity) : com_scope.
Notation "'branch' l c" :=
(ABranch l c) (in custom acom at level 91, l constr at level 0, c custom acom) : com_scope.
Fixpoint erase (ac:acom) : com :=
match ac with
| <[ skip ]> => <{skip}>
| <[ X := ae ]> => <{X := ae}>
| <[ ac1 ;@(P, PA) ac2 ]> => <{ erase ac1; erase ac2 }>
| <[ if be@lbe then ac1 else ac2 end ]> => <{ if be then erase ac1
else erase ac2 end }>
| <[ while be @ lbe do ac1 @(P,PA) end ]> => <{ while be do erase ac1 end }>
| <[ X@@lx <- a[[i@li]] ]> => <{ X <- a[[i]] }>
| <[ a[i@li] <- e ]> => <{ a[i] <- e }>
| <[ branch l ac1 ]> => <{ erase ac1 }>
end.
Definition join_pub_vars (P1 P2: pub_vars) : pub_vars :=
fun x => join (P1 x) (P2 x).
Definition less_precise (P1 P2:pub_vars) : Prop :=
forall x, can_flow (P2 x) (P1 x) = true.
Lemma pub_equiv_less_precise : forall (P P':pub_vars) {A:Type} (x1 x2:total_map A),
pub_equiv P x1 x2 ->
less_precise P' P ->
pub_equiv P' x1 x2.
Proof.
intros. intros x P'x. unfold less_precise, can_flow in H0. specialize (H0 x). apply H.
now rewrite P'x, orb_false_r in H0.
Qed.
Lemma can_flow_trans : forall a b c, can_flow a b = true -> can_flow b c = true -> can_flow a c = true.
Proof.
intros a b c H1 H2. destruct a, b, c; tauto.
Qed.
Lemma can_flow_refl : forall a, can_flow a a = true.
Proof.
now destruct a.
Qed.
Lemma less_precise_refl : forall P, less_precise P P.
Proof. intros P x. now destruct (P x). Qed.
Lemma less_precise_trans : forall P P' P'',
less_precise P' P ->
less_precise P'' P' ->
less_precise P'' P.
Proof.
intros P P' P'' H1 H2 x.
specialize (H1 x). specialize (H2 x).
eapply can_flow_trans; eassumption.
Qed.
Lemma filter_less_precise_nil P P' l:
less_precise P' P ->
filter P l = nil -> filter P' l = nil.
Proof.
intros. induction l.
- reflexivity.
- simpl in *. destruct (P a) eqn: HPa; [congruence|].
specialize (H a). rewrite HPa in H. destruct (P' a); simpl in H; [congruence | tauto].
Qed.
Lemma less_precise_join_l : forall P1 P2,
less_precise (join_pub_vars P1 P2) P1.
Proof.
intros P1 P2 x. unfold join_pub_vars. now destruct (P1 x).
Qed.
Lemma less_precise_join_r : forall P1 P2,
less_precise (join_pub_vars P1 P2) P2.
Proof.
intros P1 P2 x. unfold join_pub_vars. now destruct (P1 x), (P2 x).
Qed.
Lemma join_pub_vars_eq P: join_pub_vars P P = P.
Proof.
apply functional_extensionality.
intros. unfold join_pub_vars. now destruct (P x).
Qed.
Lemma join_pub_vars_assoc P P' P'':
join_pub_vars (join_pub_vars P P') P'' = join_pub_vars P (join_pub_vars P' P'').
Proof.
apply functional_extensionality.
intros. unfold join_pub_vars. now destruct (P x), (P' x), (P'' x).
Qed.
Lemma join_pub_vars_less_precise P P':
join_pub_vars P P' = P <-> less_precise P P'.
Proof.
split.
- intros H x. rewrite <- H. unfold join_pub_vars. now destruct (P' x), (P x).
- intros H. apply functional_extensionality. unfold join_pub_vars.
intro x. specialize (H x). now destruct (P x), (P' x).
Qed.
Definition str_nodup := nodup string_dec.
Lemma filter_nodup {A} dec P (l: list A):
filter P (nodup dec l) = nodup dec (filter P l).
Proof.
induction l.
- reflexivity.
- simpl. destruct (in_dec dec a l).
+ destruct (P a) eqn: Heq2.
* rewrite IHl. assert (In a (filter P l)) by now apply filter_In.
simpl. now destruct (in_dec dec a (filter P l)).
* assumption.
+ simpl. destruct (P a).
* assert (~ In a (filter P l)) by now intros Hin%filter_In. simpl.
destruct (in_dec dec a (filter P l)). 1: contradiction. now f_equal.
* assumption.
Qed.
Lemma nodup_nil {A} dec (l: list A):
nodup dec l = [] <-> l = [].
Proof.
split.
- induction l.
+ reflexivity.
+ simpl. destruct (in_dec dec a l); [|congruence]. intros. specialize (IHl H). subst. contradiction.
- intros ->. reflexivity.
Qed.
Fixpoint assigned_vars c :=
match c with
| <{ skip }> | <{ _ [_] <- _ }> => []
| <{ X := _ }> | <{ X <- _ [[_]] }> => [X]
| <{ c1; c2 }>
| <{ if _ then c1 else c2 end }> => str_nodup (assigned_vars c1 ++ assigned_vars c2)
| <{ while _ do c end }> => assigned_vars c
end.
Fixpoint assigned_arrs c :=
match c with
| <{ skip }>
| <{ _ := _ }>
| <{ _ <- _ [[_]] }> => []
| <{ c1; c2 }>
| <{ if _ then c1 else c2 end }> => str_nodup (assigned_arrs c1 ++ assigned_arrs c2)
| <{ while _ do c end }> => assigned_arrs c
| <{ a[_] <- _ }> => [a]
end.
Definition list_eqb l1 l2 := if list_eq_dec string_dec l1 l2 then true else false.
Fixpoint static_tracking_while (static_tracking : pub_vars -> pub_arrs -> label -> (acom * pub_vars * pub_arrs))
(P : pub_vars) (PA : pub_arrs) (pc : label) (i : nat) (be : bexp) (vars arrs pvars parrs : list string) :=
let lbe := label_of_bexp P be in
let '(ac, P1, PA1) := static_tracking P PA (join pc lbe) in
let P1 := join_pub_vars P P1 in
let PA1 := join_pub_vars PA PA1 in
let pvars1 := filter P1 vars in
let parrs1 := filter PA1 arrs in
let stop := (list_eqb pvars pvars1) && (list_eqb parrs parrs1) in
(* Stopping if a fixpoint was already reached *)
match i, stop with
| _, true | 0, _ => (P1, PA1, lbe, ac)
| S i, false => static_tracking_while static_tracking P1 PA1 pc i be vars arrs pvars1 parrs1
end.
Fixpoint static_tracking (c:com) (P:pub_vars) (PA:pub_arrs) (pc:label)
: (acom*pub_vars*pub_arrs) :=
match c with
| <{ skip }> => (<[skip]>, P, PA)
| <{ x := ae }> => (<[x := ae]>, x !-> (join pc (label_of_aexp P ae)); P, PA)
| <{ c1; c2 }> => let '(ac1, P1, PA1) := static_tracking c1 P PA pc in
let '(ac2, P2, PA2) := static_tracking c2 P1 PA1 pc in
(<[ ac1;@(P1, PA1) ac2 ]>, P2, PA2)
| <{ if be then c1 else c2 end }> =>
let lbe := label_of_bexp P be in
let '(ac1, P1, PA1) := static_tracking c1 P PA (join pc lbe) in
let '(ac2, P2, PA2) := static_tracking c2 P PA (join pc lbe) in
(<[ if be@lbe then ac1 else ac2 end ]>, join_pub_vars P1 P2, join_pub_vars PA1 PA2)
| <{ while be do c1 end }> =>
let vars := assigned_vars c1 in
let arrs := assigned_arrs c1 in
let pvars := filter P vars in
let parrs := filter PA arrs in
let max_iters := length vars + length arrs in
let '(P', PA', lbe, ac1) := static_tracking_while (static_tracking c1) P PA pc max_iters be vars arrs pvars parrs in
(<[ while be@lbe do ac1 @(P', PA') end ]>, P', PA')
| <{ X <- a[[i]] }> =>
let li := label_of_aexp P i in
let lx := join pc (join li (PA a)) in
(<[ X@@lx <- a[[i@li]] ]>, X !-> lx; P, PA)
| <{ a[i] <- e }> =>
let li := label_of_aexp P i in
let la := join (PA a) (join pc (join li (label_of_aexp P e))) in
(<[ a[i@li] <- e ]>, P, a !-> la; PA)
end.
Lemma static_tracking_while_invariant : forall ac P' PA' pc' f P PA pc i be vars arrs pvars parrs (R : acom -> Prop),
(forall ac' P PA P1 PA1 pc, f P PA pc = (ac', P1, PA1) -> R ac') ->
static_tracking_while f P PA pc i be vars arrs pvars parrs = (P', PA', pc', ac) ->
R ac.
Proof.
intros until i. revert f P PA pc P' PA' pc'. induction i; simpl; intros.
+ destruct (f P PA (join pc (label_of_bexp P be))) as ((ac1&P1)&PA1) eqn:Heq1.
eapply H. rewrite Heq1.
destruct (list_eqb pvars (filter (join_pub_vars P P1) vars) && list_eqb parrs (filter (join_pub_vars PA PA1) arrs)); now invert H0.
+ destruct (f P PA (join pc (label_of_bexp P be))) as ((ac1&P1)&PA1) eqn:Heq.
destruct (list_eqb pvars (filter (join_pub_vars P P1) vars) && list_eqb parrs (filter (join_pub_vars PA PA1) arrs)).
- invert H0. eapply H, Heq.
- eapply IHi; eassumption.
Qed.
Fixpoint pc_of_acom pc c :=
match c with
| <[ branch pc' _ ]> => pc'
| <[ c1;@(_,_) c2 ]> => pc_of_acom (pc_of_acom pc c1) c2
| _ => pc
end.
Fixpoint branch_free ac :=
match ac with
| <[ skip ]> | <[ _ := _ ]> | <[ _@@_ <-_[[_@_]] ]> | <[_[_@_] <- _]> => True
| <[ ac1;@(_,_) ac2]> => branch_free ac1 /\ branch_free ac2
| <[ if _@_ then ac1 else ac2 end ]> => branch_free ac1 /\ branch_free ac2
| <[ while _@_ do ac1@(_,_) end ]> => branch_free ac1
| <[ branch _ _ ]> => False
end.
Lemma branch_free_pc_of_acom:
forall ac pc, branch_free ac -> pc_of_acom pc ac = pc.
Proof.
induction ac; simpl; try tauto.
intros pc [H1 H2]. now rewrite IHac1, IHac2.
Qed.
Fixpoint well_labeled_acom ac P PA pc P' PA' : Prop :=
match ac with
| <[ skip ]> => less_precise P' P /\ less_precise PA' PA
| <[ x := ae ]> => less_precise P' (x !->(join pc (label_of_aexp P ae)); P) /\ less_precise PA' PA
| <[ ac1 ;@(Pi, PAi) ac2 ]> => branch_free ac2 /\
well_labeled_acom ac1 P PA pc Pi PAi /\
well_labeled_acom ac2 Pi PAi (pc_of_acom pc ac1) P' PA'
| <[ if be@lbe then ac1 else ac2 end ]> => can_flow (label_of_bexp P be) lbe = true /\
branch_free ac1 /\ branch_free ac2 /\
well_labeled_acom ac1 P PA (join pc lbe) P' PA' /\
well_labeled_acom ac2 P PA (join pc lbe) P' PA'
| <[ while be@lbe do ac1 @(Pi, PAi) end ]> => can_flow (label_of_bexp Pi be) lbe = true /\
branch_free ac1 /\
less_precise Pi P /\
less_precise PAi PA /\
well_labeled_acom ac1 Pi PAi (join pc lbe) Pi PAi /\
less_precise P' Pi /\
less_precise PA' PAi
| <[ X@@lx <-a[[i@li]] ]> => can_flow (label_of_aexp P i) li = true /\
can_flow pc lx = true /\
can_flow li lx = true /\
can_flow (PA a) lx = true /\
less_precise P' (X !-> lx; P) /\
less_precise PA' PA
| <[ a[i@li] <- e]> => can_flow (label_of_aexp P i) li = true /\
less_precise P' P /\
less_precise PA' (a !-> (join (PA a) (join pc (join li (label_of_aexp P e)))); PA)
| <[ branch l ac ]> => well_labeled_acom ac P PA pc P' PA'
end.
Lemma static_tracking_no_branch : forall c P PA P' PA' pc ac lbl,
static_tracking c P PA pc = (<[branch lbl ac]>, P', PA') ->
False.
Proof.
induction c; simpl; intros; invert H.
+ destruct (static_tracking c1 P PA pc) as ((ac1&P1)&PA1), (static_tracking c2 P1 PA1 pc) as ((ac2&P2)&PA2).
invert H1.
+ destruct (static_tracking c1 P PA (join pc (label_of_bexp P be))) as ((ac1&P1)&PA1),
(static_tracking c2 P PA (join pc (label_of_bexp P be))) as ((ac2&P2)&PA2).
invert H1.
+ destruct (static_tracking_while (static_tracking c) P PA pc
(length (assigned_vars c) + length (assigned_arrs c)) be
(assigned_vars c) (assigned_arrs c) (filter P (assigned_vars c)) (filter PA (assigned_arrs c)))
as (((P1&PA1)&lbe)&ac1).
invert H1.
Qed.
Lemma static_tracking_branch_free : forall c P PA P' PA' pc ac,
static_tracking c P PA pc = (ac, P', PA') -> branch_free ac.
Proof.
induction c; simpl; intros; invert H; try easy.
- destruct (static_tracking c1 P PA pc) as ((ac1 & P1) & PA1) eqn: Heq1, (static_tracking c2 P1 PA1 pc)
as ((ac2 & P2) & PA2) eqn: Heq2. invert H1.
simpl. split; [eapply IHc1 | eapply IHc2]; eassumption.
- destruct (static_tracking c1 P PA (join pc (label_of_bexp P be))) as ((ac1 & P1) & PA1) eqn: Heq1,
(static_tracking c2 P PA (join pc (label_of_bexp P be))) as ((ac2 & P2) & PA2) eqn: Heq2. invert H1.
simpl. split; [eapply IHc1 | eapply IHc2]; eassumption.
- destruct (static_tracking_while (static_tracking c) P PA pc
(length (assigned_vars c) + length (assigned_arrs c)) be (assigned_vars c) (assigned_arrs c))
as (((Pi & PAi) & lbe) & ac1) eqn: Heq.
eapply static_tracking_while_invariant in Heq. 2: { intros. apply (IHc P0 PA0 P1 PA1 pc0 ac' H). }
invert H1. exact Heq.
Qed.
Lemma exp_label_less_precise P P':
less_precise P P' ->
(forall ae, can_flow (label_of_aexp P' ae) (label_of_aexp P ae) = true) /\
(forall be, can_flow (label_of_bexp P' be) (label_of_bexp P be) = true).
Proof.
intro Hlp.
apply aexp_bexp_mutind; intros; try easy.
- apply Hlp.
- cbn. now destruct (label_of_aexp P' a1), (label_of_aexp P a1), (label_of_aexp P' a2), (label_of_aexp P a2).
- cbn. now destruct (label_of_aexp P' a1), (label_of_aexp P a1), (label_of_aexp P' a2), (label_of_aexp P a2).
- cbn. now destruct (label_of_aexp P' a1), (label_of_aexp P a1), (label_of_aexp P' a2), (label_of_aexp P a2).
- cbn. now destruct (label_of_bexp P' b), (label_of_bexp P b), (label_of_aexp P' a1), (label_of_aexp P a1),
(label_of_aexp P' a2), (label_of_aexp P a2).
- cbn. now destruct (label_of_aexp P' a1), (label_of_aexp P a1), (label_of_aexp P' a2), (label_of_aexp P a2).
- cbn. now destruct (label_of_aexp P' a1), (label_of_aexp P a1), (label_of_aexp P' a2), (label_of_aexp P a2).
- cbn. now destruct (label_of_aexp P' a1), (label_of_aexp P a1), (label_of_aexp P' a2), (label_of_aexp P a2).
- cbn. now destruct (label_of_aexp P' a1), (label_of_aexp P a1), (label_of_aexp P' a2), (label_of_aexp P a2).
- cbn. now destruct (label_of_bexp P' b1), (label_of_bexp P b1), (label_of_bexp P' b2), (label_of_bexp P b2).
Qed.
Lemma label_of_aexp_less_precise :
forall e P P', less_precise P P' ->
can_flow (label_of_aexp P' e) (label_of_aexp P e) = true.
Proof.
intros. now apply exp_label_less_precise.
Qed.
Lemma label_of_bexp_less_precise :
forall e P P', less_precise P P' ->
can_flow (label_of_bexp P' e) (label_of_bexp P e) = true.
Proof.
intros. now apply exp_label_less_precise.
Qed.
Lemma well_labeled_weaken_post : forall ac P PA pc P' PA' P'' PA'',
well_labeled_acom ac P PA pc P' PA' ->
less_precise P'' P' ->
less_precise PA'' PA' ->
well_labeled_acom ac P PA pc P'' PA''.
Proof.
induction ac; intros; simpl in *.
- destruct H. split; eapply less_precise_trans; eassumption.
- destruct H. split; eapply less_precise_trans; eassumption.
- destruct H as (? & ? & ?). repeat split. 1, 2: assumption. eapply IHac2; eassumption.
- destruct H as (? & ? & ? & ? & ?). repeat split; [assumption.. | eapply IHac1 | eapply IHac2]; eassumption.
- destruct H as (? & ? & ? & ? & ? & ? & ?). repeat split; try assumption; eapply less_precise_trans; eassumption.
- destruct H as (? & ? & ? & ? & ? & ?). repeat split; try assumption; eapply less_precise_trans; eassumption.
- destruct H as (? & ? & ?). repeat split; try assumption; eapply less_precise_trans; eassumption.
- eapply IHac; eassumption.
Qed.
Lemma well_labeled_strengthen_pre : forall ac P PA pc P' PA' P'' PA'',
well_labeled_acom ac P PA pc P' PA' ->
less_precise P P'' ->
less_precise PA PA'' ->
well_labeled_acom ac P'' PA'' pc P' PA'.
Proof.
induction ac; simpl; intros.
- destruct H. split; eapply less_precise_trans; eassumption.
- destruct H. split; [|eapply less_precise_trans; eassumption].
intros y. specialize (H y). unfold t_update in *. destruct (x =? y).
+ apply (label_of_aexp_less_precise e) in H0. now destruct pc, (P' y), (label_of_aexp P e), (label_of_aexp P'' e).
+ specialize (H0 y). eapply can_flow_trans; eassumption.
- destruct H as (? & ? & ?).
repeat split; try assumption. eapply IHac1; eassumption.
- destruct H as (? & ? & ? & ? & ?).
repeat split; try assumption. 1: eapply can_flow_trans; [now apply label_of_bexp_less_precise | eassumption].
1: eapply IHac1; eassumption.
eapply IHac2; eassumption.
- repeat split; try apply H; eapply less_precise_trans; now try apply H.
- repeat split; try apply H.
+ apply (label_of_aexp_less_precise i) in H0. eapply can_flow_trans; now try apply H.
+ apply (label_of_aexp_less_precise a) in H1. eapply can_flow_trans; now try apply H.
+ destruct H as (?&?&?&?&?&?). intros y. specialize (H5 y).
unfold t_update in *. destruct (x =? y).
* assumption.
* eapply can_flow_trans; [apply H0 | apply H5].
+ eapply less_precise_trans; now try apply H.
- repeat split.
+ apply (label_of_aexp_less_precise i) in H0. eapply can_flow_trans; now try apply H.
+ eapply less_precise_trans; now try apply H.
+ destruct H as (?&?&?). intros b. specialize (H3 b). apply (label_of_aexp_less_precise e) in H0 as H0'.
specialize (H1 a) as H1'.
unfold t_update in *. destruct (a =? b).
* destruct (PA a), (PA'' a), pc, li, (label_of_aexp P e), (label_of_aexp P'' e); try easy.
* eapply can_flow_trans. 2: eassumption. apply H1.
- eapply IHac; eassumption.
Qed.
Lemma static_tracking_fixpoint:
forall c P PA pc ac P' PA',
static_tracking c P PA pc = (ac, P', PA') ->
forall P'' PA'',
filter P'' (assigned_vars c) = [] ->
filter PA'' (assigned_arrs c) = [] ->
less_precise P'' P -> less_precise PA'' PA ->
join_pub_vars P'' P' = P'' /\ join_pub_vars PA'' PA' = PA''.
Proof.
induction c.
+ simpl. intros ? ? _ ? ? ? [= _ -> ->] ? ? _ _ ? ?. split; now apply join_pub_vars_less_precise.
+ simpl. intros. invert H; subst. split; [|now apply join_pub_vars_less_precise].
apply functional_extensionality. intro y. unfold join_pub_vars, t_update. destruct (x =? y) eqn: Heq.
- rewrite String.eqb_eq in Heq. subst. specialize (H2 y). now destruct (P y), (P'' y).
- specialize (H2 y); now destruct (P y), (P'' y).
+ simpl. intros.
destruct (static_tracking c1 P PA pc) as ((ac1 & P1) & PA1) eqn: Heq1.
destruct (static_tracking c2 P1 PA1 pc) as ((ac2 & P2) & PA2) eqn: Heq2.
apply IHc1 with (P'' := P'') (PA'' := PA'') in Heq1.
2, 3: unfold str_nodup in H0, H1; rewrite filter_nodup in H0, H1; apply nodup_nil in H0, H1;
rewrite filter_app in H0, H1; now apply app_eq_nil in H0, H1. 2, 3: assumption.
apply IHc2 with (P'' := P'') (PA'' := PA'') in Heq2.
2, 3: unfold str_nodup in H0, H1; rewrite filter_nodup in H0, H1; apply nodup_nil in H0, H1;
rewrite filter_app in H0, H1; now apply app_eq_nil in H0, H1. 2, 3: now apply join_pub_vars_less_precise.
invert H.
split; apply Heq2.
+ simpl. intros.
destruct (static_tracking c1 P PA (join pc (label_of_bexp P be))) as ((ac1 & P1) & PA1) eqn: Heq1.
destruct (static_tracking c2 P PA (join pc (label_of_bexp P be))) as ((ac2 & P2) & PA2) eqn: Heq2.
apply IHc1 with (P'' := P'') (PA'' := PA'') in Heq1.
2, 3: unfold str_nodup in H0, H1; rewrite filter_nodup in H0, H1; apply nodup_nil in H0, H1;
rewrite filter_app in H0, H1; now apply app_eq_nil in H0, H1. 2, 3: assumption.
apply IHc2 with (P'' := P'') (PA'' := PA'') in Heq2.
2, 3: unfold str_nodup in H0, H1; rewrite filter_nodup in H0, H1; apply nodup_nil in H0, H1;
rewrite filter_app in H0, H1; now apply app_eq_nil in H0, H1. 2, 3: assumption.
invert H. do 2 rewrite <- join_pub_vars_assoc. destruct Heq1 as [-> ->]. exact Heq2.
+ simpl.
induction (length (assigned_vars c) + length (assigned_arrs c)) as [|n IHn]; simpl;
intros P PA pc ac P' PA' H.
* destruct (static_tracking c P PA (join pc (label_of_bexp P be))) as ((ac' & Pi') & PAi') eqn: Heq'.
rewrite Tauto.if_same in H. invert H. intros ? ? Hnil1 Hnil2 ? ?. eapply IHc in Heq'; [|eassumption..].
do 2 rewrite <- join_pub_vars_assoc. apply join_pub_vars_less_precise in H as ->, H0 as ->. assumption.
* destruct (static_tracking c P PA (join pc (label_of_bexp P be))) as ((ac' & Pi') & PAi') eqn: Heq'.
destruct (list_eqb (filter P (assigned_vars c)) (filter (join_pub_vars P Pi') (assigned_vars c))
&& list_eqb (filter PA (assigned_arrs c)) (filter (join_pub_vars PA PAi') (assigned_arrs c))) eqn: HeqPPA.
- invert H. intros ? ? Hnil1 Hnil2 ? ?. eapply IHc in Heq'; [|eassumption..].
do 2 rewrite <- join_pub_vars_assoc. apply join_pub_vars_less_precise in H as ->, H0 as ->. assumption.
- intros ? ? Hnil1 Hnil2 ? ?.
eapply IHc in Heq'; [|eassumption..].
eapply IHn with (P'' := join_pub_vars P'' Pi') (PA'' := join_pub_vars PA'' PAi') in H.
2, 3: destruct Heq' as [Heq1 Heq2]; rewrite 1? Heq1; rewrite 1? Heq2; assumption.
2, 3: apply join_pub_vars_less_precise; destruct Heq' as [Heq1 Heq2]; rewrite 1? Heq1; rewrite 1? Heq2; rewrite <- join_pub_vars_assoc.
2: apply join_pub_vars_less_precise in H0 as ->; assumption.
2: apply join_pub_vars_less_precise in H1 as ->; assumption.
destruct Heq' as [<- <-]. exact H.
+ simpl. intros. invert H. split; [|now apply join_pub_vars_less_precise].
apply functional_extensionality. intros y. unfold join_pub_vars, t_update. destruct (x =? y) eqn: Heq.
- rewrite String.eqb_eq in Heq. subst. specialize (H2 y). now destruct (P y), (P'' y).
- specialize (H2 y). now destruct (P y), (P'' y).
+ simpl. intros. invert H. split; [now apply join_pub_vars_less_precise|].
apply functional_extensionality. intros y. unfold join_pub_vars, t_update. destruct (a =? y) eqn: Heq.
- rewrite String.eqb_eq in Heq. subst. specialize (H3 y). now destruct (PA y), (PA'' y).
- specialize (H3 y). now destruct (PA y), (PA'' y).
Qed.
Lemma static_tracking_unassigned: forall c P PA pc ac P' PA',
static_tracking c P PA pc = (ac, P', PA') ->
(forall y, ~In y (assigned_vars c) -> P' y = P y) /\
(forall y, ~In y (assigned_arrs c) -> PA' y = PA y).
Proof.
induction c; simpl; intros.
- invert H. tauto.
- invert H. split; [|tauto]. intros y. unfold t_update. destruct (x =? y) eqn: Heq;
[apply String.eqb_eq in Heq | apply String.eqb_neq in Heq]; tauto.
- destruct (static_tracking c1 P PA pc) as ((ac1 & P1) & PA1) eqn: Hc1. apply IHc1 in Hc1.
destruct (static_tracking c2 P1 PA1 pc) as ((ac2 & P2) & PA2) eqn: Hc2. apply IHc2 in Hc2.
split.
+ intros y Hnin.
assert (~ In y (assigned_vars c1) /\ ~ In y (assigned_vars c2)) as [Hnin1 Hnin2].
{ split; intros Hin; apply Hnin; apply nodup_In; apply in_or_app; [left | right]; assumption. }
invert H. destruct Hc1, Hc2. now rewrite H1, H.
+ intros y Hnin.
assert (~ In y (assigned_arrs c1) /\ ~ In y (assigned_arrs c2)) as [Hnin1 Hnin2].
{ split; intros Hin; apply Hnin; apply nodup_In; apply in_or_app; [left | right]; assumption. }
invert H. destruct Hc1, Hc2. now rewrite H2, H0.
- destruct (static_tracking c1 P PA (join pc (label_of_bexp P be))) as ((ac1 & P1) & PA1) eqn: Hc1. apply IHc1 in Hc1.
destruct (static_tracking c2 P PA (join pc (label_of_bexp P be))) as ((ac2 & P2) & PA2) eqn: Hc2. apply IHc2 in Hc2.
invert H. split.
+ intros y Hnin.
assert (~ In y (assigned_vars c1) /\ ~ In y (assigned_vars c2)) as [Hnin1 Hnin2].
{ split; intros Hin; apply Hnin; apply nodup_In; apply in_or_app; [left | right]; assumption. }
unfold join_pub_vars. destruct Hc1, Hc2. rewrite H, H1; [|assumption..]. now destruct (P y).
+ intros y Hnin.
assert (~ In y (assigned_arrs c1) /\ ~ In y (assigned_arrs c2)) as [Hnin1 Hnin2].
{ split; intros Hin; apply Hnin; apply nodup_In; apply in_or_app; [left | right]; assumption. }
unfold join_pub_vars. destruct Hc1, Hc2. rewrite H0, H2; [|assumption..]. now destruct (PA y).
- revert P PA pc ac P' PA' H.
induction (length (assigned_vars c) + length (assigned_arrs c)) as [|n IHn]; intros.
+ cbn in H. destruct (static_tracking c P PA (join pc (label_of_bexp P be))) as ((aci & Pi) & PAi) eqn: Hi.
apply IHc in Hi. destruct Hi as [Hi1 Hi2].
rewrite Tauto.if_same in H. invert H.
split; intros y Hnin; [specialize (Hi1 y Hnin) | specialize (Hi2 y Hnin)]; unfold join_pub_vars;
[rewrite Hi1; now destruct (P y) | rewrite Hi2; now destruct (PA y)].
+ simpl in H. destruct (static_tracking c P PA (join pc (label_of_bexp P be))) as ((aci & Pi) & PAi) eqn: Hi.
apply IHc in Hi.
destruct (list_eqb (filter P (assigned_vars c)) (filter (join_pub_vars P Pi) (assigned_vars c))
&& list_eqb (filter PA (assigned_arrs c)) (filter (join_pub_vars PA PAi) (assigned_arrs c))).
* invert H. destruct Hi as [Hi1 Hi2].
split; intros y Hnin; [specialize (Hi1 y Hnin) | specialize (Hi2 y Hnin)]; unfold join_pub_vars;
[rewrite Hi1; now destruct (P y) | rewrite Hi2; now destruct (PA y)].
* apply IHn in H. destruct Hi as [Hi1 Hi2], H as [H1 H2].
split; intros y Hnin.
-- specialize (Hi1 y Hnin). specialize (H1 y Hnin). rewrite H1. unfold join_pub_vars. rewrite Hi1.
now destruct (P y).
-- specialize (Hi2 y Hnin). specialize (H2 y Hnin). rewrite H2. unfold join_pub_vars. rewrite Hi2.
now destruct (PA y).
- invert H. split; [|tauto].
intros y. unfold t_update. destruct (x =? y) eqn: Heq;
[apply String.eqb_eq in Heq | apply String.eqb_neq in Heq]; tauto.
- invert H. split; [tauto|].
intros y. unfold t_update. destruct (a =? y) eqn: Heq;
[apply String.eqb_eq in Heq | apply String.eqb_neq in Heq]; tauto.
Qed.
Lemma static_tracking_while_early_fixpoint:
forall c P PA pc ac P' PA',
static_tracking c P PA pc = (ac, P', PA') ->
list_eqb (filter P (assigned_vars c))
(filter (join_pub_vars P P') (assigned_vars c)) &&
list_eqb (filter PA (assigned_arrs c))
(filter (join_pub_vars PA PA') (assigned_arrs c)) = true ->
join_pub_vars P P' = P /\ join_pub_vars PA PA' = PA.
Proof.
intros. apply static_tracking_unassigned in H as [H1 H2].
apply andb_prop in H0 as [HP HPA].
unfold list_eqb in HP, HPA.
destruct (list_eq_dec string_dec (filter P (assigned_vars c)) (filter (join_pub_vars P P') (assigned_vars c)))
as [HeqP|?]; [|congruence].
destruct (list_eq_dec string_dec (filter PA (assigned_arrs c)) (filter (join_pub_vars PA PA') (assigned_arrs c)))
as [HeqPA|?]; [|congruence].
split; unfold join_pub_vars; apply functional_extensionality; intros y.
- destruct (in_dec string_dec y (assigned_vars c)) as [Hin | Hnin].
+ eapply ext_in_filter in HeqP. 2: eassumption. rewrite HeqP at 2. reflexivity.
+ rewrite H1; [|assumption]. now destruct (P y).
- destruct (in_dec string_dec y (assigned_arrs c)) as [Hin | Hnin].
+ eapply ext_in_filter in HeqPA. 2: eassumption. rewrite HeqPA at 2. reflexivity.
+ rewrite H2; [|assumption]. now destruct (PA y).
Qed.
Lemma filter_join P P' l:
filter (join_pub_vars P P') l = filter P' (filter P l).
Proof.
induction l.
- reflexivity.
- simpl. unfold join_pub_vars. destruct (P a); simpl; destruct (P' a); simpl; [f_equal|..]; apply IHl.
Qed.
Lemma filter_shorter_or_eq {A} f (l: list A):
filter f l = l \/ length (filter f l) < length l.
Proof.
induction l.
- now left.
- destruct IHl.
+ simpl. destruct (f a).
* left. now rewrite H.
* right. rewrite H. lia.
+ right. simpl. destruct (f a); simpl; lia.
Qed.
Lemma static_tracking_while_decreases:
forall c P PA P' PA',
list_eqb (filter P (assigned_vars c))
(filter (join_pub_vars P P') (assigned_vars c)) &&
list_eqb (filter PA (assigned_arrs c))
(filter (join_pub_vars PA PA') (assigned_arrs c)) = false ->
length (filter (join_pub_vars P P') (assigned_vars c))
+ length (filter (join_pub_vars PA PA') (assigned_arrs c))
< length (filter P (assigned_vars c))
+ length (filter PA (assigned_arrs c)).
Proof.
intros. apply andb_false_iff in H. unfold list_eqb in H. destruct H as [HP | HPA].
- destruct (list_eq_dec string_dec (filter P (assigned_vars c)) (filter (join_pub_vars P P') (assigned_vars c))) as
[?|HneqP];[congruence|].
repeat rewrite filter_join in *.
pose proof (filter_length_le PA' (filter PA (assigned_arrs c))).
enough (length (filter P' (filter P (assigned_vars c))) < length (filter P (assigned_vars c))) by lia.
destruct (filter_shorter_or_eq P' (filter P (assigned_vars c))); [congruence | lia].
- destruct (list_eq_dec string_dec (filter PA (assigned_arrs c)) (filter (join_pub_vars PA PA') (assigned_arrs c)))
as [?|HneqPA];[congruence|].
repeat rewrite filter_join in *.
pose proof (filter_length_le P' (filter P (assigned_vars c))).
enough (length (filter PA' (filter PA (assigned_arrs c))) < length (filter PA (assigned_arrs c))) by lia.
destruct (filter_shorter_or_eq PA' (filter PA (assigned_arrs c))); [congruence | lia].
Qed.
Lemma static_tracking_while_well_labeled: forall c n P PA pc be P' PA' lbe ac,
(forall P PA pc ac P' PA', static_tracking c P PA pc = (ac, P', PA') -> well_labeled_acom ac P PA pc P' PA') ->
(n >= length (filter P (assigned_vars c)) + length (filter PA (assigned_arrs c))) ->
(static_tracking_while (static_tracking c) P PA pc n be
(assigned_vars c) (assigned_arrs c) (filter P (assigned_vars c)) (filter PA (assigned_arrs c))
= (P', PA', lbe, ac)) ->
can_flow (label_of_bexp P' be) lbe = true /\
less_precise P' P /\
less_precise PA' PA /\
well_labeled_acom ac P' PA' (join pc lbe) P' PA'.
Proof.
induction n; intros.
- cbn in H1.
destruct (static_tracking c P PA (join pc (label_of_bexp P be))) as ((ac1 & P1) & PA1) eqn: Hwl.
apply static_tracking_fixpoint with (P'' := P) (PA'' := PA) in Hwl as Hfix. 4, 5: apply less_precise_refl.
2, 3: apply length_zero_iff_nil; lia.
apply H in Hwl. rewrite Tauto.if_same in H1. invert H1; subst.
pose proof (less_precise_join_r P P1). pose proof (less_precise_join_r PA PA1).
destruct Hfix as [Hfix1 Hfix2]. rewrite Hfix1, Hfix2 in *.
repeat split. 2, 3: apply less_precise_refl.
+ apply can_flow_refl.
+ eapply well_labeled_weaken_post; eassumption.
- cbn in H1.
destruct (static_tracking c P PA (join pc (label_of_bexp P be))) as ((ac1 & P1) & PA1) eqn: Hwl.
apply H in Hwl as Hwl'.
destruct (list_eqb (filter P (assigned_vars c)) (filter (join_pub_vars P P1) (assigned_vars c))
&& list_eqb (filter PA (assigned_arrs c)) (filter (join_pub_vars PA PA1) (assigned_arrs c))) eqn: HeqPPA.
+ invert H1; subst.
pose proof (less_precise_join_r P P1). pose proof (less_precise_join_r PA PA1).
eapply static_tracking_while_early_fixpoint in HeqPPA. 2: eassumption. destruct HeqPPA as [Heq1 Heq2].
rewrite Heq1, Heq2 in *. clear Heq1 Heq2.
repeat split. 2, 3: apply less_precise_refl.
* apply can_flow_refl.
* eapply well_labeled_weaken_post; eassumption.
+ apply IHn in H1. 3: { apply static_tracking_while_decreases in HeqPPA. lia. } 2: exact H.
repeat split; try apply H1; (eapply less_precise_trans; [apply less_precise_join_l | apply H1]).
Qed.
Lemma static_tracking_well_labeled : forall c P PA pc ac P' PA',
static_tracking c P PA pc = (ac, P', PA') ->
well_labeled_acom ac P PA pc P' PA'.
Proof.
induction c; intros.
- simpl in H. invert H. simpl. split; now apply less_precise_refl.
- simpl in H. invert H. simpl. split; now apply less_precise_refl.
- simpl in H. destruct (static_tracking c1 P PA pc) as ((ac1 & P1) & PA1) eqn: Heq1,
(static_tracking c2 P1 PA1 pc) as ((ac2 & P2) & PA2) eqn: Heq2.
invert H. simpl.
assert (pc_of_acom pc ac1 = pc) as ->.
{
apply static_tracking_branch_free in Heq1. now apply branch_free_pc_of_acom.
}
split. 1: eapply static_tracking_branch_free; eassumption.
split; [now apply IHc1 | now apply IHc2].
- simpl in H. destruct (static_tracking c1 P PA (join pc (label_of_bexp P be))) as ((ac1 & P1) & PA1) eqn: Heq1,
(static_tracking c2 P PA (join pc (label_of_bexp P be))) as ((ac2 & P2) & PA2) eqn: Heq2.
invert H. simpl.
split. 1: apply can_flow_refl.
split. 1: eapply static_tracking_branch_free; eassumption.
split. 1: eapply static_tracking_branch_free; eassumption.
apply IHc1 in Heq1. apply IHc2 in Heq2.
split; eapply well_labeled_weaken_post; try eassumption.
+ apply less_precise_join_l.
+ apply less_precise_join_l.
+ apply less_precise_join_r.
+ apply less_precise_join_r.
- apply static_tracking_branch_free in H as H'. simpl in H.
destruct (static_tracking_while (static_tracking c) P PA pc
(length (assigned_vars c) + length (assigned_arrs c)) be (assigned_vars c) (assigned_arrs c)
(filter P (assigned_vars c)) (filter PA (assigned_arrs c))) as (((Pi & PAi) & lbe) & ac1) eqn: Heq.
invert H. cbn.
apply static_tracking_while_well_labeled in Heq. 2: assumption.
2: { pose proof (filter_length_le P (assigned_vars c)). pose proof (filter_length_le PA (assigned_arrs c)). lia. }
repeat split. 2: exact H'. 5, 6: apply less_precise_refl.
all: apply Heq.
- simpl in H. invert H. simpl. repeat split.
5,6: apply less_precise_refl.
+ now destruct (label_of_aexp P i).
+ now destruct pc.
+ now destruct pc, (label_of_aexp P i).
+ now destruct pc, (label_of_aexp P i), (PA' a).
- simpl in H. invert H. simpl. repeat split.
2, 3: apply less_precise_refl.
now destruct (label_of_aexp P' i).
Qed.
Lemma erase_static_tracking : forall c ac P PA P' PA' pc,
static_tracking c P PA pc = (ac, P', PA') ->
erase ac = c.
Proof.
induction c; simpl; intros; try (now invert H; auto).
+ destruct (static_tracking c1 P PA pc) as ((ac1&P1)&PA1) eqn:Heq1.
destruct (static_tracking c2 P1 PA1 pc) as ((ac2&P2)&PA2) eqn:Heq2.
invert H. erewrite <- IHc1; [|eassumption]. erewrite <- IHc2; [tauto|eassumption].
+ destruct (static_tracking c1 P PA (join pc (label_of_bexp P be))) as ((ac1&P1)&PA1) eqn:Heq1.
destruct (static_tracking c2 P PA (join pc (label_of_bexp P be))) as ((ac2&P2)&PA2) eqn:Heq2.
invert H. erewrite <- IHc1; [|eassumption]. erewrite <- IHc2; [tauto|eassumption].
+ destruct (static_tracking_while (static_tracking c) P PA pc (length (assigned_vars c) + length (assigned_arrs c))
be (assigned_vars c) (assigned_arrs c) (filter P (assigned_vars c)) (filter PA (assigned_arrs c)))
as (((P1&PA1)&pc1)&ac1) eqn:Heq.
invert H. simpl. eapply static_tracking_while_invariant in Heq; [|apply IHc]. now rewrite Heq.
Qed.
Fixpoint flex_vslh_acom ac :=
match ac with
| <[ skip ]> => <{ skip }>
| <[ X := ae ]> => <{ X := ae }>
| <[ ac1;@(_,_) ac2 ]> => <{ flex_vslh_acom ac1; flex_vslh_acom ac2 }>
| <[ if be@lbe then ac1 else ac2 end ]> =>
let be' := if lbe then be else <{ "b" = 0 && be }> in
<{ if be' then "b" := be' ? "b" : 1; flex_vslh_acom ac1 else
"b" := be' ? 1 : "b"; flex_vslh_acom ac2 end }>
| <[ while be@lbe do ac@(_,_) end ]> =>
let be' := if lbe then be else <{ "b" = 0 && be }> in
<{ while be' do "b" := be' ? "b" : 1; flex_vslh_acom ac end; "b" := be' ? 1 : "b" }>
| <[ X@@lx <- a[[i@li]] ]> =>
let i' := if li && negb lx then i else <{ ("b" = 1) ? 0 : i }> in
if lx && li then <{ X <- a[[i]]; X := ("b" = 1) ? 0 : X }> else <{ X <- a[[i']] }>
| <[ a[i@li] <- e ]> => let i' := if li then i else <{ ("b" = 1) ? 0 : i }> in
<{ a[i'] <- e }>
| <[ branch l ac1 ]> => <{ flex_vslh_acom ac1 }> (* unreachable anyway *)
end.
Definition fs_flex_vslh P PA c :=
let '(ac, _, _) := static_tracking c P PA public in
flex_vslh_acom ac.
(** * Ideal small-step evaluation *)
Inductive terminal : acom -> Prop :=
| Terminal_Skip : terminal <[ skip ]>
| Terminal_Branch : forall l c, terminal c -> terminal <[ branch l c ]>.
Reserved Notation
"'<[[' c , st , ast , b , pc , P , PA ']]>' '-->i_' ds '^^' os '<[[' ct , stt , astt , bt , pct , Pt , PAt ']]>'"
(at level 40, c custom acom at level 99, ct custom acom at level 99,
st constr, ast constr, stt constr, astt constr at next level).
Inductive ideal_eval_small_step :
acom -> state -> astate -> bool -> label -> pub_vars -> pub_arrs ->
acom -> state -> astate -> bool -> label -> pub_vars -> pub_arrs -> dirs -> obs -> Prop :=
| ISM_Asgn : forall X ae n st ast b pc P PA,
aeval st ae = n ->
<[[X := ae, st, ast, b, pc, P, PA]]> -->i_[]^^[] <[[skip, X !-> n; st, ast, b, pc, X !-> (join pc (label_of_aexp P ae)); P, PA]]>
| ISM_Seq : forall c1 st ast b ds os c1t stt astt bt c2 pc P PA pc' P' PA' Pi PAi,
<[[c1, st, ast, b, pc, P, PA]]> -->i_ds^^os <[[c1t, stt, astt, bt, pc', P', PA']]> ->
<[[(c1;@(Pi,PAi)c2), st, ast, b, pc, P, PA]]> -->i_ds^^os <[[(c1t;@(Pi,PAi) c2), stt, astt, bt, pc', P', PA']]>
| ISM_Seq_Skip : forall st ast b c1 c2 pc P PA Pi PAi,
terminal c1 ->
<[[(c1;@(Pi,PAi)c2), st, ast, b, pc, P, PA]]> -->i_[]^^[] <[[c2, st, ast, b, pc_of_acom pc c1, P, PA]]>
| ISM_If : forall be ct cf st ast b c' b' lbe pc P PA,
b' = (lbe || negb b) && beval st be ->
c' = (if b' then ct else cf) ->
<[[if be@lbe then ct else cf end, st, ast, b, pc, P, PA]]> -->i_[DStep]^^[OBranch b'] <[[branch pc c', st, ast, b, join pc lbe, P, PA]]>
| ISM_If_F : forall be ct cf st ast b c' b' lbe pc P PA,
b' = (lbe || negb b) && beval st be ->
c' = (if b' then cf else ct) ->
<[[if be@lbe then ct else cf end, st, ast, b, pc, P, PA]]> -->i_[DForce]^^[OBranch b'] <[[branch pc c', st, ast, true, join pc lbe, P, PA]]>
| ISM_While : forall be c st ast b lbe c' pc P PA Pi PAi,
c' = <[ if be@lbe then c;@(Pi, PAi) while be@lbe do c@(Pi,PAi) end else skip end ]> ->
<[[while be@lbe do c@(Pi,PAi) end, st, ast, b, pc, P, PA]]> -->i_[]^^[] <[[c', st, ast, b, pc, P, PA]]>
| ISM_ARead : forall X a ie st ast (b :bool) i li lX v pc P PA,
(if (negb li) && b then 0 else (aeval st ie)) = i ->
(if lX && li && b then 0 else nth i (ast a) 0) = v ->
i < length (ast a) ->
<[[X@@lX <- a[[ie@li]], st, ast, b, pc, P, PA]]> -->i_[DStep]^^[OARead a i]
<[[skip, X !-> v; st, ast, b, pc, X !-> lX; P, PA]]>
| ISM_ARead_U : forall X a ie st ast i a' i' v (lX:label) pc P PA,
aeval st ie = i ->
v = (if lX then 0 else nth i' (ast a') 0) ->
i >= length (ast a) ->
i' < length (ast a') ->
<[[X@@lX <- a[[ie@public]], st, ast, true, pc, P, PA]]> -->i_[DLoad a' i']^^[OARead a i]
<[[skip, X !-> v; st, ast, true, pc, X !-> lX; P, PA]]>
| ISM_Write : forall a ie e st ast (b :bool) i n li i' la pc P PA,
aeval st e = n ->
aeval st ie = i ->
(if b && negb li then 0 else i) = i' ->
la = join (PA a) (join pc (join li (label_of_aexp P e))) ->
i' < length (ast a) ->
<[[a[ie@li] <- e, st, ast, b, pc, P, PA]]> -->i_[DStep]^^[OAWrite a i']
<[[skip, st, a !-> upd i' (ast a) n; ast, b, pc, P, a !-> la; PA]]>
| ISM_Write_U : forall a ie e st ast i n a' i' la pc P PA,
aeval st e = n ->
aeval st ie = i ->
i >= length (ast a) ->
i' < length (ast a') ->
la = join (PA a) (join pc (label_of_aexp P e)) ->
<[[a[ie@public] <- e, st, ast, true, pc, P, PA]]> -->i_[DStore a' i']^^[OAWrite a i]
<[[skip, st, a' !-> upd i' (ast a') n; ast, true, pc, P, a !-> la; PA]]>
| ISM_Branch : forall c1 st ast b ds os c1t stt astt bt pc pc' P PA pct Pt PAt,
<[[c1, st, ast, b, pc, P, PA]]> -->i_ds^^os <[[c1t, stt, astt, bt, pct, Pt, PAt]]> ->
<[[branch pc' c1, st, ast, b, pc, P, PA]]> -->i_ds^^os <[[branch pc' c1t, stt, astt, bt, pct, Pt, PAt]]>
where "<[[ c , st , ast , b , pc , P , PA ]]> -->i_ ds ^^ os <[[ ct , stt , astt , bt , pct , Pt , PAt ]]>" :=
(ideal_eval_small_step c st ast b pc P PA ct stt astt bt pct Pt PAt ds os).
Reserved Notation
"'<[[' c , st , ast , b , pc , P , PA ']]>' '-->i*_' ds '^^' os '<[[' ct , stt , astt , bt , pct , Pt , PAt ']]>'"
(at level 40, c custom acom at level 99, ct custom acom at level 99,
st constr, ast constr, stt constr, astt constr at next level).
Inductive multi_ideal (c:acom) (st:state) (ast:astate) (b:bool) (pc:label) (P:pub_vars) (PA:pub_arrs):
acom -> state -> astate -> bool -> label -> pub_vars -> pub_arrs -> dirs -> obs -> Prop :=
| multi_ideal_refl : <[[c, st, ast, b, pc, P, PA]]> -->i*_[]^^[] <[[c, st, ast, b, pc, P, PA]]>
| multi_ideal_trans (c':acom) (st':state) (ast':astate) (b':bool)
(c'':acom) (st'':state) (ast'':astate) (b'':bool) (pc' pc'':label) (P' P'':pub_vars) (PA' PA'':pub_arrs)
(ds1 ds2 : dirs) (os1 os2 : obs) :
<[[c, st, ast, b, pc, P, PA]]> -->i_ds1^^os1 <[[c', st', ast', b', pc', P', PA']]> ->
<[[c', st', ast', b', pc', P', PA']]> -->i*_ds2^^os2 <[[c'', st'', ast'', b'', pc'', P'', PA'']]> ->
<[[c, st, ast, b, pc, P, PA]]> -->i*_(ds1++ds2)^^(os1++os2) <[[c'', st'', ast'', b'', pc'', P'', PA'']]>
where "<[[ c , st , ast , b , pc , P , PA ]]> -->i*_ ds ^^ os <[[ ct , stt , astt , bt , pct , Pt , PAt ]]>" :=
(multi_ideal c st ast b pc P PA ct stt astt bt pct Pt PAt ds os).
Definition ideal_same_obs ac st1 st2 ast1 ast2 P PA :=
forall ds os1 os2 stt1 stt2 astt1 astt2 ac1 ac2 b1 b2 pc1 pc2 Pt1 PAt1 Pt2 PAt2,
<[[ac, st1, ast1, false, public, P, PA]]> -->i*_ds^^os1 <[[ac1, stt1, astt1, b1, pc1, Pt1, PAt1]]> ->
<[[ac, st2, ast2, false, public, P, PA]]> -->i*_ds^^os2 <[[ac2, stt2, astt2, b2, pc2, Pt2, PAt2]]> ->
os1 = os2.
Lemma multi_ideal_trans_nil_l c st ast b c' st' ast' b' ct stt astt bt ds os pc pc' pct P PA P' PA' Pt PAt:
<[[c, st, ast, b, pc, P, PA]]> -->i_[]^^[] <[[c', st', ast', b', pc', P', PA']]> ->
<[[c', st', ast', b', pc', P', PA']]> -->i*_ds^^os <[[ct, stt, astt, bt, pct, Pt, PAt]]> ->
<[[c, st, ast, b, pc, P, PA]]> -->i*_ds^^os <[[ct, stt, astt, bt, pct, Pt, PAt]]>.
Proof.
intros. rewrite <- app_nil_l. rewrite <- app_nil_l with (l:=ds). eapply multi_ideal_trans; eassumption.
Qed.
Lemma multi_ideal_trans_nil_r c st ast b c' st' ast' b' ct stt astt bt ds os pc pc' pct P PA P' PA' Pt PAt:
<[[c, st, ast, b, pc, P, PA]]> -->i_ds^^os <[[c', st', ast', b', pc', P', PA']]> ->
<[[c', st', ast', b', pc', P', PA']]> -->i*_[]^^[] <[[ct, stt, astt, bt, pct, Pt, PAt]]> ->
<[[c, st, ast, b, pc, P, PA]]> -->i*_ds^^os <[[ct, stt, astt, bt, pct, Pt, PAt]]>.
Proof.
intros. rewrite <- app_nil_r. rewrite <- app_nil_r with (l:=ds). eapply multi_ideal_trans; eassumption.
Qed.
Lemma multi_ideal_combined_executions :
forall c st ast b ds cm stm astm bm os dst ct stt astt bt ost pc P PA pcm Pm PAm pct Pt PAt,
<[[c, st, ast, b, pc, P, PA]]> -->i*_ds^^os <[[cm, stm, astm, bm, pcm, Pm, PAm]]> ->
<[[cm, stm, astm, bm, pcm, Pm, PAm]]> -->i*_dst^^ost <[[ct, stt, astt, bt, pct, Pt, PAt]]> ->
<[[c, st, ast, b, pc, P, PA]]> -->i*_(ds++dst)^^(os++ost) <[[ct, stt, astt, bt, pct, Pt, PAt]]>.
Proof.
intros. revert ct stt astt bt pct Pt PAt dst ost H0. induction H; simpl; intros; [tauto|].
rewrite <- 2!app_assoc. eapply multi_ideal_trans.
+ eapply H.
+ now apply IHmulti_ideal.
Qed.
Lemma multi_ideal_add_snd_com : forall c st ast ct stt astt ds os c2 b bt pc P PA pct Pt PAt Pi PAi,
<[[c, st, ast, b, pc, P, PA]]> -->i*_ds^^os <[[ct, stt, astt, bt, pct, Pt, PAt]]> ->
<[[c;@(Pi,PAi) c2, st, ast, b, pc, P, PA]]> -->i*_ds^^os <[[ct;@(Pi,PAi) c2, stt, astt, bt, pct, Pt, PAt]]>.
Proof.
intros. induction H; repeat econstructor; eauto.
Qed.
Lemma multi_ideal_seq : forall ac1 ac2 acm st ast b stm astm bm ds os pc P PA pcm Pm PAm Pi PAi,
<[[ac1;@(Pi, PAi) ac2, st, ast, b, pc, P, PA]]> -->i*_ds^^os <[[acm, stm, astm, bm, pcm, Pm, PAm]]> ->
(exists st' ast' pc' P' PA' act b' ds1 ds2 os1 os2,
terminal act /\ os = os1 ++ os2 /\ ds = ds1 ++ ds2 /\
<[[ac1, st, ast, b, pc, P, PA]]> -->i*_ds1^^os1 <[[act, st', ast', b', pc', P', PA']]> /\
<[[ac2, st', ast', b', pc_of_acom pc' act, P', PA']]> -->i*_ds2^^os2 <[[acm, stm, astm, bm, pcm, Pm, PAm]]>) \/
(exists ac', acm = <[ ac';@(Pi, PAi) ac2 ]> /\
<[[ac1, st, ast, b, pc, P, PA]]> -->i*_ds^^os <[[ac', stm, astm, bm, pcm, Pm, PAm]]>).
Proof.
intros. remember <[ ac1;@(Pi, PAi) ac2 ]> as ac. revert ac1 ac2 Heqac.
induction H; intros; subst.
{ right. repeat eexists. constructor. }
invert H.
+ edestruct IHmulti_ideal; [reflexivity|..].
- destruct H as (?&?&?&?&?&?&?&?&?&?&?&?&->&->&?&?).
left. rewrite !app_assoc.
repeat eexists; [|econstructor|]; eassumption.
- do 2 destruct H. subst. clear IHmulti_ideal.
right. repeat eexists. econstructor; eassumption.
+ left. repeat eexists; [|constructor|]; eassumption.
Qed.
Lemma ideal_eval_small_step_spec_needs_force : forall c st ast ct stt astt ds os pc P PA pct Pt PAt,
<[[c, st, ast, false, pc, P, PA]]> -->i_ds^^os <[[ct, stt, astt, true, pct, Pt, PAt]]> ->
ds = [DForce].
Proof.
intros. remember false as b. remember true as bt. revert Heqb Heqbt.
now induction H; intros; subst; try discriminate; try reflexivity; apply IHideal_eval_small_step.
Qed.
Lemma multi_ideal_spec_needs_force : forall c st ast ct stt astt ds os pc P PA pct Pt PAt,
<[[c, st, ast, false, pc, P, PA]]> -->i*_ds^^os <[[ct, stt, astt, true, pct, Pt, PAt]]> ->
In DForce ds.
Proof.
intros. remember false as b. remember true as bt. revert Heqb Heqbt.
induction H; intros; subst; [discriminate|]. apply in_or_app.
destruct b'.
+ apply ideal_eval_small_step_spec_needs_force in H. subst. left. now left.
+ right. now apply IHmulti_ideal.
Qed.
Lemma ideal_eval_seq_eval : forall c st ast ct stt astt bt n os pc P PA pct Pt PAt,
<[[c, st, ast, false, pc, P, PA]]> -->i_ repeat DStep n ^^ os <[[ct, stt, astt, bt, pct, Pt, PAt]]> ->
<((erase c, st, ast))> -->^os <((erase ct, stt, astt))>.
Proof.
intros. remember false as b in H. remember (repeat DStep n) as ds in H. revert Heqb Heqds.
induction H; intros; subst; try discriminate; try now econstructor.
+ constructor. now apply IHideal_eval_small_step.
+ induction H; simpl in *; [constructor|tauto].
+ rewrite orb_true_r. simpl.
replace (erase (if beval st be then ct else cf)) with (if beval st be then erase ct else erase cf)
by now destruct (beval st be). constructor.
+ symmetry in Heqds. change ([DForce]) with ([] ++ [DForce]) in Heqds.
now apply repeat_eq_elt in Heqds.
+ rewrite ?andb_false_r in *. now constructor.
+ now apply IHideal_eval_small_step.
Qed.
Lemma multi_ideal_branch : forall c st ast b ct stt astt bt ds os l pc P PA pct Pt PAt,
<[[ c, st, ast, b, pc, P, PA]]> -->i*_ds^^os <[[ ct, stt, astt, bt, pct, Pt, PAt]]> ->
<[[ branch l c, st, ast, b, pc, P, PA]]> -->i*_ds^^os <[[ branch l ct, stt, astt, bt, pct, Pt, PAt]]>.
Proof.
intros. induction H; [constructor|].
repeat econstructor; eassumption.
Qed.
Lemma multi_ideal_multi_seq : forall c st ast ct stt astt bt n os pc P PA pct Pt PAt,
<[[c, st, ast, false, pc, P, PA]]> -->i*_ repeat DStep n ^^ os <[[ct, stt, astt, bt, pct, Pt, PAt]]> ->
<((erase c, st, ast ))> -->*^os <((erase ct, stt, astt))>.
Proof.
intros. remember false as b in H. remember (repeat DStep n) as ds in H. revert n Heqb Heqds.
induction H; intros; subst; [constructor|].
symmetry in Heqds. apply repeat_eq_app in Heqds. destruct Heqds.
remember (length ds1) as n1. remember (length ds2) as n2. clear Heqn1 Heqn2 H0. subst.
destruct b'.
{
apply ideal_eval_small_step_spec_needs_force in H. change ([DForce]) with ([] ++ [DForce]) in H.
now apply repeat_eq_elt in H.
}
apply ideal_eval_seq_eval in H. econstructor; [eassumption|].
now eapply IHmulti_ideal; eauto.
Qed.
Lemma ideal_eval_small_step_obs_length : forall c st ast b ds ct stt astt bt os pc P PA pct Pt PAt,
<[[c, st, ast, b, pc, P, PA]]> -->i_ds^^os <[[ct, stt, astt, bt, pct, Pt, PAt]]> ->
length ds = length os.
Proof. intros. induction H; simpl; auto. Qed.
Lemma ideal_terminal_no_step : forall c st ast b ct stt astt bt ds os pc P PA pct Pt PAt,
terminal c ->
<[[ c, st, ast, b, pc, P, PA]]> -->i_ds^^os <[[ ct, stt, astt, bt, pct, Pt, PAt]]> ->
False.
Proof. intros. revert ct H0. induction H; intros; [invert H0|invert H0]. eapply IHterminal, H18. Qed.
Lemma ideal_eval_small_step_same_length :
forall c st1 st2 ast1 ast2 b1 b2 ct1 ct2 stt1 stt2 astt1 astt2 bt1 bt2 os1 os2 ds1 ds2
pc1 P1 PA1 pc2 P2 PA2 pct1 Pt1 PAt1 pct2 Pt2 PAt2,
<[[c, st1, ast1, b1, pc1, P1, PA1]]> -->i_ds1^^os1 <[[ct1, stt1, astt1, bt1, pct1, Pt1, PAt1]]> ->
<[[c, st2, ast2, b2, pc2, P2, PA2]]> -->i_ds2^^os2 <[[ct2, stt2, astt2, bt2, pct2, Pt2, PAt2]]> ->
length ds1 = length ds2.