-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsCorrectness.v
5944 lines (5480 loc) · 243 KB
/
JsCorrectness.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
Set Implicit Arguments.
Require Import Shared.
Require Import LibFix LibList.
Require Import JsSyntax JsSyntaxAux JsCommon JsCommonAux JsPreliminary.
Require Import JsInterpreterMonads JsInterpreter JsPrettyInterm JsPrettyRules.
Ltac tryfalse_nothing :=
try match goal with x: nothing |- _ => destruct x end;
tryfalse.
(**************************************************************)
(** ** Implicit Types -- copied from JsPreliminary *)
Implicit Type b : bool.
Implicit Type n : number.
Implicit Type k : int.
Implicit Type s : string.
Implicit Type i : literal.
Implicit Type l : object_loc.
Implicit Type w : prim.
Implicit Type v : value.
Implicit Type r : ref.
Implicit Type ty : type.
Implicit Type rt : restype.
Implicit Type rv : resvalue.
Implicit Type lab : label.
Implicit Type labs : label_set.
Implicit Type R : res.
Implicit Type o : out.
Implicit Type ct : codetype.
Implicit Type x : prop_name.
Implicit Type str : strictness_flag.
Implicit Type m : mutability.
Implicit Type Ad : attributes_data.
Implicit Type Aa : attributes_accessor.
Implicit Type A : attributes.
Implicit Type Desc : descriptor.
Implicit Type D : full_descriptor.
Implicit Type L : env_loc.
Implicit Type E : env_record.
Implicit Type Ed : decl_env_record.
Implicit Type X : lexical_env.
Implicit Type O : object.
Implicit Type S : state.
Implicit Type C : execution_ctx.
Implicit Type P : object_properties_type.
Implicit Type W : result.
Implicit Type e : expr.
Implicit Type p : prog.
Implicit Type t : stat.
Implicit Type T : Type.
(**************************************************************)
(** Correctness Properties *)
Record runs_type_correct runs :=
make_runs_type_correct {
runs_type_correct_expr : forall S C e o,
runs_type_expr runs S C e = o ->
red_expr S C (expr_basic e) o;
runs_type_correct_stat : forall S C t o,
runs_type_stat runs S C t = o ->
red_stat S C (stat_basic t) o;
runs_type_correct_prog : forall S C p o,
runs_type_prog runs S C p = o ->
red_prog S C (prog_basic p) o;
runs_type_correct_call : forall S C l v vs o,
runs_type_call runs S C l v vs = o ->
red_expr S C (spec_call l v vs) o;
runs_type_correct_call_prealloc : forall S C l B args o,
runs_type_call_prealloc runs S C B l args = result_some (specret_out o) ->
red_expr S C (spec_call_prealloc B l args) o;
runs_type_correct_construct : forall S C co l args o,
runs_type_construct runs S C co l args = o ->
red_expr S C (spec_construct_1 co l args) o;
runs_type_correct_function_has_instance : forall S C (lo lv : object_loc) o,
runs_type_function_has_instance runs S lo lv = o ->
red_expr S C (spec_function_has_instance_2 lv lo) o;
runs_type_correct_get_args_for_apply : forall S C array (index n : int) y,
runs_type_get_args_for_apply runs S C array index n = result_some y ->
red_spec S C (spec_function_proto_apply_get_args array index n) y;
runs_type_correct_object_has_instance : forall S C B l v o,
runs_type_object_has_instance runs S C B l v = result_some (specret_out o) ->
red_expr S C (spec_object_has_instance_1 B l v) o;
runs_type_correct_stat_while : forall S C rv ls e t o,
runs_type_stat_while runs S C rv ls e t = o ->
red_stat S C (stat_while_1 ls e t rv) o;
runs_type_correct_stat_do_while : forall S C rv ls e t o,
runs_type_stat_do_while runs S C rv ls e t = o ->
red_stat S C (stat_do_while_1 ls t e rv) o;
runs_type_correct_stat_for_loop : forall S C labs rv eo2 eo3 t o,
runs_type_stat_for_loop runs S C labs rv eo2 eo3 t = o ->
red_stat S C (stat_for_2 labs rv eo2 eo3 t) o;
runs_type_correct_object_delete : forall S C l x str o,
runs_type_object_delete runs S C l x str = o ->
red_expr S C (spec_object_delete l x str) o;
runs_type_correct_object_get_own_prop : forall S C l x sp,
runs_type_object_get_own_prop runs S C l x = result_some sp ->
red_spec S C (spec_object_get_own_prop l x) sp;
runs_type_correct_object_get_prop : forall S C l x sp,
runs_type_object_get_prop runs S C l x = result_some sp ->
red_spec S C (spec_object_get_prop l x) sp;
runs_type_correct_object_get : forall S C l x o,
runs_type_object_get runs S C l x = o ->
red_expr S C (spec_object_get l x) o;
runs_type_correct_object_proto_is_prototype_of : forall S C lthis l o,
runs_type_object_proto_is_prototype_of runs S lthis l = o ->
red_expr S C (spec_call_object_proto_is_prototype_of_2_3 lthis l) o;
runs_type_correct_object_put : forall S C l x v str o,
runs_type_object_put runs S C l x v str = o ->
red_expr S C (spec_object_put l x v str) o;
runs_type_correct_equal : forall S C v1 v2 o,
runs_type_equal runs S C v1 v2 = o ->
red_expr S C (spec_equal v1 v2) o;
runs_type_correct_to_integer : forall S C v o,
runs_type_to_integer runs S C v = o ->
red_expr S C (spec_to_integer v) o;
runs_type_correct_to_string : forall S C v o,
runs_type_to_string runs S C v = o ->
red_expr S C (spec_to_string v) o;
(* ARRAYS *)
runs_type_correct_array_element_list : forall S C l oes o k,
runs_type_array_element_list runs S C l oes k = o ->
red_expr S C (expr_array_3 l oes k) o;
runs_type_correct_object_define_own_prop_array_loop :
forall S C l newLen oldLen newLenDesc newWritable throw o
(def : state -> prop_name -> descriptor -> strictness_flag -> specres nothing)
(def_correct : forall S str o x Desc,
def S x Desc str = res_out o ->
red_expr S C (spec_object_define_own_prop_1 builtin_define_own_prop_default l x Desc str) o),
runs_type_object_define_own_prop_array_loop runs S C l newLen oldLen newLenDesc newWritable throw def = o ->
red_expr S C (spec_object_define_own_prop_array_3l l newLen oldLen newLenDesc newWritable throw) o;
runs_type_correct_array_join_elements : forall S C l k length sep s o,
runs_type_array_join_elements runs S C l k length sep s = result_some (specret_out o) ->
red_expr S C (spec_call_array_proto_join_elements l k length sep s) o
}.
(**************************************************************)
(** Useful Tactics *)
Ltac absurd_neg :=
let H := fresh in
introv H; inverts H; tryfalse.
Hint Constructors abort.
(**************************************************************)
(** General Lemmas *)
Lemma arguments_from_spec_1 : forall args,
exists v, arguments_from args (v::nil)
/\ get_arg 0 args = v.
Proof.
Hint Constructors arguments_from.
intros. destruct args as [|v vs].
exists undef. splits*.
exists v. splits*.
Qed.
Lemma res_overwrite_value_if_empty_empty : forall R,
res_overwrite_value_if_empty resvalue_empty R = R.
Proof. introv. unfolds. cases_if~. destruct R; simpls; inverts~ e. Qed.
Lemma res_type_res_overwrite_value_if_empty : forall rv R,
res_type R = res_type (res_overwrite_value_if_empty rv R).
Proof.
introv. destruct R. unfold res_overwrite_value_if_empty. simpl.
cases_if; reflexivity.
Qed.
Lemma res_label_res_overwrite_value_if_empty : forall rv R,
res_label R = res_label (res_overwrite_value_if_empty rv R).
Proof.
introv. destruct R. unfold res_overwrite_value_if_empty. simpl.
cases_if; reflexivity.
Qed.
Lemma res_overwrite_value_if_empty_resvalue : forall rv1 rv2, exists rv3,
res_normal rv3 = res_overwrite_value_if_empty rv1 rv2 /\ (rv3 = rv1 \/ rv3 = rv2).
Proof. introv. unfolds res_overwrite_value_if_empty. cases_if*. Qed.
Lemma get_arg_correct : forall args vs,
arguments_from args vs ->
forall num,
num < length vs ->
get_arg num args = LibList.nth num vs.
Proof.
introv A. induction~ A.
introv I. false I. lets (I'&_): (rm I). inverts~ I'.
introv I. destruct* num. rewrite nth_succ. rewrite <- IHA.
unfolds. repeat rewrite~ nth_def_nil.
rewrite length_cons in I. nat_math.
introv I. destruct* num. rewrite nth_succ. rewrite <- IHA.
unfolds. rewrite~ nth_def_succ.
rewrite length_cons in I. nat_math.
Qed.
Lemma get_arg_correct_0 : forall args,
arguments_from args (get_arg 0 args :: nil).
Proof. introv. destruct args; do 2 constructors. Qed.
Lemma get_arg_correct_1 : forall args,
arguments_from args (get_arg 0 args :: get_arg 1 args :: nil).
Proof. introv. destruct args as [|? [|? ?]]; do 3 constructors. Qed.
Lemma get_arg_correct_2 : forall args,
arguments_from args (get_arg 0 args :: get_arg 1 args :: get_arg 2 args :: nil).
Proof. introv. destruct args as [|? [|? [|? ?]]]; do 4 constructors. Qed.
Lemma get_arg_first_and_rest_correct : forall args v lv,
(v, lv) = get_arg_first_and_rest args <->
arguments_first_and_rest args (v, lv).
Proof.
induction args; introv; splits; introv Hyp;
unfolds get_arg_first_and_rest; unfolds get_arg;
simpls; inverts~ Hyp.
Qed.
Lemma and_impl_left : forall P1 P2 P3 : Prop,
(P1 -> P2) ->
P1 /\ P3 ->
P2 /\ P3.
Proof. auto*. Qed.
Ltac applys_and_base L :=
applys~ and_impl_left; [applys~ L|]; try reflexivity.
Tactic Notation "applys_and" constr(E) :=
applys_and_base (>> E).
Tactic Notation "applys_and" constr(E) constr(A1) :=
applys_and_base (>> E A1).
Tactic Notation "applys_and" constr(E) constr(A1) constr(A2) :=
applys_and_base (>> E A1 A2).
Tactic Notation "applys_and" constr(E) constr(A1) constr(A2) constr(A3) :=
applys_and_base (>> E A1 A2 A3).
Ltac constructors_and :=
let H := fresh in
eapply and_impl_left; [ intro H; constructors; exact H |].
Lemma run_callable_correct : forall S v co,
run_callable S v = Some co ->
callable S v co.
Proof.
introv E. destruct v; simpls~.
inverts~ E.
sets_eq <- B: (pick_option (object_binds S o)). destruct B; simpls; tryfalse.
exists o0. splits~. forwards~: @pick_option_correct EQB. inverts~ E.
Qed.
(**************************************************************)
(** Monadic Constructors, Lemmas *)
(* Shared defs *)
(** [eqabort o1 o] assert that [o1] and [o] are equal
and satisfy the [abort] predicate. *)
Definition eqabort o1 o :=
o = o1 /\ abort o.
Ltac prove_abort :=
solve [ assumption | (constructor; absurd_neg) ].
(** [isout W Pred] asserts that [W] is in fact
an outcome that satisfies [Pred]. *)
Definition isout W (Pred:out->Prop) :=
exists o1, W = res_out o1 /\ Pred o1.
Hint Unfold isout.
Hint Unfold eqabort.
(* Generic *)
Lemma if_empty_label_out : forall T K S R (o : T),
if_empty_label S R K = result_some o ->
res_label R = label_empty /\ K tt = result_some o.
Proof. introv H. unfolds in H. cases_if; tryfalse. eexists; auto*. Qed.
Lemma if_some_out : forall (A B : Type) (oa : option A) K (b : B),
if_some oa K = result_some b ->
exists (a:A), oa = Some a /\ K a = result_some b.
Proof. introv E. destruct* oa; tryfalse. Qed.
Lemma if_result_some_out : forall (A B : Type) (W : resultof A) K (b : B),
if_result_some W K = result_some b ->
exists (y : A), W = result_some y /\ K y = result_some b.
Proof. introv H. destruct* W; tryfalse. Qed.
Lemma if_some_or_default_out : forall (A B : Type) (oa : option A) d K (b : B),
if_some_or_default oa d K = b ->
(oa = None /\ d = b)
\/ (exists a, oa = Some a /\ K a = b).
Proof. introv E. destruct* oa; tryfalse. Qed.
(* Results *)
Definition if_ter_post (K : _ -> _ -> result) o o1 :=
(o1 = out_div /\ o = o1)
\/ (exists S R, o1 = out_ter S R /\ K S R = o).
Lemma if_ter_out : forall W K o,
if_ter W K = res_out o ->
isout W (if_ter_post K o).
Proof.
introv H. destruct W as [[|o1]| | | ]; simpls; tryfalse_nothing.
exists o1. splits~. unfolds. destruct o1 as [|S R].
inverts* H.
jauto.
Qed.
Definition if_success_state_post rv0 (K : _ -> _ -> result) o o1 :=
(o1 = out_div /\ o = o1) \/
(exists S R, o1 = out_ter S R /\ res_type R = restype_throw /\ o = out_ter S R) \/
(exists S R, o1 = out_ter S R /\ res_type R <> restype_throw /\
res_type R <> restype_normal /\ o = out_ter S (res_overwrite_value_if_empty rv0 R)) \/
exists S rv, o1 = out_ter S (res_normal rv) /\
K S (ifb rv = resvalue_empty then rv0 else rv) = res_out o.
Lemma if_success_state_out : forall rv W K o,
if_success_state rv W K = o ->
isout W (if_success_state_post rv K o).
Proof.
introv E. forwards~ (o1&WE&P): if_ter_out (rm E). subst W. eexists. splits*.
inversion_clear P as [?|(S&R&?&H)]. branch~ 1.
substs. destruct R as [rt rv' rl]. destruct~ rt; simpls;
try solve [branch 3; repeat eexists; [discriminate | discriminate | inverts~ H]].
forwards~ (?&?): if_empty_label_out (rm H). simpls. substs.
branch 4. repeat eexists. auto*.
inverts H. branch 2. repeat eexists.
Qed.
Definition if_success_post (K : _ -> _ -> result) o o1 :=
eqabort o1 o \/
exists S rv, o1 = out_ter S (res_normal rv) /\ K S rv = o.
Lemma if_success_out : forall W K o,
if_success W K = res_out o ->
isout W (if_success_post K o).
Proof.
introv E. forwards~ (o1&WE&P): if_ter_out (rm E). subst W. eexists. splits*.
inversion_clear P as [[? ?]|(S&R&?&H)]. substs. branch~ 1.
substs. destruct R as [rt rv' rl]. destruct~ rt; simpls;
try solve [inverts H; branch 1; splits~; prove_abort].
forwards~ (?&?): if_empty_label_out (rm H). simpls. substs.
branch 2. repeat eexists. auto*.
Qed.
(* Documentation: same with unfolding:
Lemma if_success_out : forall W K o,
if_success W K = o ->
exists o1, W = res_out o1 /\
( (o = o1 /\ abort o)
\/ (exists S rv, o1 = out_ter S rv /\ K S rv = o)).
*)
Definition if_void_post (K : _ -> result) o o1 :=
eqabort o1 o \/
exists S, o1 = out_void S /\ K S = o.
Lemma if_void_out : forall W K o,
if_void W K = o ->
isout W (if_void_post K o).
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_success_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&?&?)]; subst; [ left* | right ].
exists S. destruct R; tryfalse. auto.
Admitted. (*faster*)
(* if_not_throw *)
Definition if_not_throw_post (K : _ -> _ -> result) o o1 :=
eqabort o1 o \/
(exists S R, o1 = out_ter S R /\
((res_type R <> restype_throw /\ K S R = o) \/
(res_type R = restype_throw /\ o = o1))).
Hint Extern 1 (_ <> _ :> restype) => congruence.
Lemma if_not_throw_out : forall W K o,
if_not_throw W K = o ->
isout W (if_not_throw_post K o).
Proof.
introv E. unfolds in E.
forwards~ (o1 & WE & P): if_ter_out (rm E).
exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?] | (S & R & ? & ?)]. branch~ 1.
splits~. substs~.
right. exists S R; splits~.
destruct (res_type R); try solve [left; splits~; discriminate].
right; splits~. subst. inverts~ H0.
Qed.
Definition if_any_or_throw_post (K1 K2 : _ -> _ -> result) o o1 :=
(o1 = out_div /\ o = o1) \/
(exists S R, o1 = out_ter S R /\
( (res_type R <> restype_throw /\ K1 S R = o)
\/ (res_type R = restype_throw /\ exists (v : value), res_value R = v
/\ res_label R = label_empty /\ K2 S v = o))). (* Didn't worked when writing [exists (v : value), R = res_throw v]. *)
Lemma if_any_or_throw_out : forall W K1 K2 o,
if_any_or_throw W K1 K2 = res_out o ->
isout W (if_any_or_throw_post K1 K2 o).
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_ter_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&?&?)]; subst; [ left* | right ].
exists S R. split~. destruct (res_type R); tryfalse; simple*.
right. destruct (res_value R); tryfalse; simple*. split*.
forwards*: if_empty_label_out.
Admitted. (*faster*)
Definition if_success_or_return_post (K1 : state -> result) (K2 : state -> resvalue -> result) o o1 :=
(o1 = out_div /\ o = o1)
\/ exists S R, o1 = out_ter S R /\
( (res_type R = restype_normal /\ res_label R = label_empty /\ K1 S = o)
\/ (res_type R = restype_return /\ res_label R = label_empty /\ K2 S (res_value R) = o)
\/ (res_type R <> restype_normal /\ res_type R <> restype_return /\ o1 = o)).
Lemma if_success_or_return_out : forall W (K1 : state -> result) (K2 : state -> resvalue -> result) o,
if_success_or_return W K1 K2 = o ->
isout W (if_success_or_return_post K1 K2 o).
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_ter_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
exists S R. split~. destruct (res_type R); tryfalse; simple*.
branch 1. forwards*: if_empty_label_out.
branch 3. inverts* E.
branch 3. inverts* E.
branch 2. forwards*: if_empty_label_out.
branch 3. inverts* E.
Admitted. (*faster*)
(* TODO: misssing
if_normal_continue_or_break *)
Definition if_break_post (K : _ -> _ -> result) o o1 :=
(o1 = out_div /\ o = o1)
\/ (exists S R, o1 = out_ter S R /\
( (res_type R <> restype_break /\ o1 = o)
\/ (res_type R = restype_break /\ K S R = o))).
Lemma if_break_out : forall W K o,
if_break W K = o ->
isout W (if_break_post K o).
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_ter_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
exists S R. split~. destruct (res_type R); try inverts E; simple*.
Admitted. (*faster*)
Definition if_value_post (K : _ -> _ -> result) o o1 :=
eqabort o1 o \/
exists S v, o1 = out_ter S (res_val v) /\ K S v = o.
Lemma if_value_out : forall W K o,
if_value W K = res_out o ->
isout W (if_value_post K o).
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_success_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_bool_post (K : _ -> _ -> result) o o1 :=
eqabort o1 o \/
exists S z, o1 = out_ter S (res_val (prim_bool z)) /\ K S z = o.
Lemma if_bool_out : forall W K o,
if_bool W K = res_out o ->
isout W (if_bool_post K o).
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_value_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. destruct p; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_object_post (K : _ -> _ -> result) o o1 :=
eqabort o1 o \/
exists S l, o1 = out_ter S (res_val (value_object l)) /\ K S l = o.
Lemma if_object_out : forall W K o,
if_object W K = res_out o ->
isout W (if_object_post K o).
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_value_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_string_post (K : _ -> _ -> result) o o1 :=
eqabort o1 o \/
exists S s, o1 = out_ter S (res_val (prim_string s)) /\ K S s = o.
Lemma if_string_out : forall W K o,
if_string W K = res_out o ->
isout W (if_string_post K o).
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_value_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. destruct p; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_number_post (K : _ -> _ -> result) o o1 :=
eqabort o1 o \/
exists S n, o1 = out_ter S (res_val (prim_number n)) /\ K S n = o.
Lemma if_number_out : forall W K o,
if_number W K = res_out o ->
isout W (if_number_post K o).
Proof.
introv E. unfolds in E.
forwards~ (o1 & WE & P): if_value_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. destruct p; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_prim_post (K : _ -> _ -> result) o o1 :=
eqabort o1 o \/
exists S w, o1 = out_ter S (res_val (value_prim w)) /\ K S w = o.
Lemma if_prim_out : forall W K o,
if_prim W K = res_out o ->
isout W (if_prim_post K o).
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_value_out (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. exists___*.
Admitted. (*faster*)
Lemma if_abort_out : forall T o K (t : T),
if_abort o K = result_some t ->
abort o /\ K tt = result_some t.
Proof. introv H. destruct* o. simpls. cases_if*. Qed.
Definition if_spec_post (A B:Type) K (b:specret B) y :=
(exists o, y = specret_out o /\ b = specret_out o /\ abort o)
\/ (exists (S:state) (a:A), y = specret_val S a /\ K S a = result_some b).
Lemma if_spec_out : forall (A B : Type) (W : specres A) K (b : specret B),
if_spec W K = result_some b ->
exists y, W = result_some y /\ if_spec_post K b y.
Proof.
introv E. unfolds in E. unfolds in E.
destruct W; tryfalse. exists s. split~.
unfolds. destruct s; [ right | left ].
exists___*.
lets (?&H): if_abort_out E. inverts H. exists___*.
Admitted. (* faster *)
Definition if_ter_spec_post T K (y:specret T) o :=
(y = specret_out o /\ abort o)
\/ (exists S, exists (R : res), o = out_ter S R /\ K S R = result_some y).
Lemma if_ter_spec : forall T W K (y : specret T),
if_ter W K = result_some y ->
isout W (if_ter_spec_post K y).
Proof.
introv H. destruct W as [[|o1]| | | ]; simpls; tryfalse_nothing.
exists o1. splits~. unfolds. destruct o1 as [|S R].
inverts* H.
jauto.
Qed.
Definition if_success_spec_post T K (y:specret T) o :=
(y = specret_out o /\ abort o)
\/ (exists S, exists (rv : resvalue), o = out_ter S rv /\ K S rv = result_some y).
Lemma if_success_spec : forall T W K (y : specret T),
if_success W K = result_some y ->
exists (o : out), W = o /\ if_success_spec_post K y o. (* LATER: Change to [isout] *)
Proof.
introv E. forwards~ (o1&WE&P): if_ter_spec (rm E). subst W. eexists. splits*.
inversion_clear P as [[? ?]|(S&R&?&H)]. substs. branch~ 1.
substs. destruct R as [rt rv' rl]. destruct~ rt; simpls;
try solve [inverts H; branch 1; splits~; prove_abort].
forwards~ (?&?): if_empty_label_out (rm H). simpls. substs.
branch 2. repeat eexists. auto*.
Qed.
Definition if_value_spec_post T K (y:specret T) o :=
(y = specret_out o /\ abort o)
\/ (exists S, exists (v : value), o = out_ter S v /\ K S v = result_some y).
Lemma if_value_spec : forall T W K (y : specret T),
if_value W K = result_some y ->
exists (o : out), W = o /\ if_value_spec_post K y o.
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_success_spec (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_prim_spec_post T K (y:specret T) o :=
(y = specret_out o /\ abort o)
\/ (exists S, exists (w : prim), o = out_ter S w /\ K S w = result_some y).
Lemma if_prim_spec : forall T W K (y : specret T),
if_prim W K = result_some y ->
exists (o : out), W = o /\ if_prim_spec_post K y o.
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_value_spec (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_bool_spec_post T K (y:specret T) o :=
(y = specret_out o /\ abort o)
\/ (exists S, exists (b : bool), o = out_ter S b /\ K S b = result_some y).
Lemma if_bool_spec : forall T W K (y : specret T),
if_bool W K = result_some y ->
exists (o : out), W = o /\ if_bool_spec_post K y o.
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_value_spec (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. destruct p; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_number_spec_post T K (y:specret T) o :=
(y = specret_out o /\ abort o)
\/ (exists S, exists (n : number), o = out_ter S n /\ K S n = result_some y).
Lemma if_number_spec : forall T W K (y : specret T),
if_number W K = result_some y ->
exists (o : out), W = o /\ if_number_spec_post K y o.
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_value_spec (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. destruct p; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_string_spec_post T K (y:specret T) o :=
(y = specret_out o /\ abort o)
\/ (exists S, exists (s : string), o = out_ter S s /\ K S s = result_some y).
Lemma if_string_spec : forall T W K (y : specret T),
if_string W K = result_some y ->
exists (o : out), W = o /\ if_string_spec_post K y o.
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_value_spec (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. destruct p; tryfalse. exists___*.
Admitted. (*faster*)
Definition if_object_spec_post T K (y:specret T) o :=
(y = specret_out o /\ abort o)
\/ (exists S, exists (l : object_loc), o = out_ter S l /\ K S l = result_some y).
Lemma if_object_spec : forall T W K (y : specret T),
if_object W K = result_some y ->
exists (o : out), W = o /\ if_object_spec_post K y o.
Proof.
introv E. unfolds in E.
forwards~ (o1&WE&P): if_value_spec (rm E). exists o1. split~.
unfolds. unfolds in P.
inversion_clear P as [[? ?]|(S&R&H&E)]; subst; [ left* | right ].
destruct R; tryfalse. exists___*.
Qed.
(************************************************************)
(* ** Correctness Tactics *)
(** [prove_not_intercept] proves a goal of
the form "~ abort_intercepted_* _" *)
Ltac prove_not_intercept :=
let H := fresh in intros H; try solve [ inversion H; false~ ].
Hint Extern 1 (~ abort_intercepted_expr _) => prove_not_intercept.
Hint Extern 1 (~ abort_intercepted_stat _) => prove_not_intercept.
Hint Extern 1 (~ abort_intercepted_prog _) => prove_not_intercept.
Ltac abort_tactic L :=
try subst; apply L;
[ simpl; congruence
| try prove_abort
| try prove_not_intercept ].
Tactic Notation "abort_expr" :=
abort_tactic red_expr_abort.
Tactic Notation "abort_stat" :=
abort_tactic red_stat_abort.
Tactic Notation "abort_prog" :=
abort_tactic red_prog_abort.
Tactic Notation "abort_spec" :=
abort_tactic red_spec_abort.
Tactic Notation "abort" :=
match goal with
| |- red_expr _ _ _ _ => abort_expr
| |- red_stat _ _ _ _ => abort_stat
| |- red_prog _ _ _ _ => abort_prog
| |- red_spec _ _ _ _ => abort_spec
end.
(** [run_select_ifres] selects the appropriate "out" lemma *)
Ltac run_select_extra T := fail.
Ltac run_select_ifres H :=
match type of H with ?T = _ => match T with
| @if_ter nothing _ _ => constr:(if_ter_out)
| @if_success nothing _ _ => constr:(if_success_out)
| @if_value nothing _ _ => constr:(if_value_out)
| @if_void nothing _ _ => constr:(if_void_out)
| if_break _ _ => constr:(if_break_out)
| @if_object nothing _ _ => constr:(if_object_out)
| @if_bool nothing _ _ => constr:(if_bool_out)
| @if_string nothing _ _ => constr:(if_string_out)
| @if_number nothing _ _ => constr:(if_number_out)
| @if_prim nothing _ _ => constr:(if_prim_out)
| if_ter _ _ => constr:(if_ter_spec)
| if_success _ _ => constr:(if_success_spec)
| if_value _ _ => constr:(if_value_spec)
| if_bool _ _ => constr:(if_bool_spec)
| if_string _ _ => constr:(if_string_spec)
| if_object _ _ => constr:(if_object_spec)
| if_number _ _ => constr:(if_number_spec)
| if_prim _ _ => constr:(if_prim_spec)
| if_spec _ _ => constr:(if_spec_out)
| if_void _ _ => constr:(if_void_out)
| if_not_throw _ _ => constr:(if_not_throw_out)
| if_any_or_throw _ _ _ => constr:(if_any_or_throw_out)
| if_success_or_return _ _ _ => constr:(if_success_or_return_out)
| if_success_state _ _ _ => constr:(if_success_state_out)
| ?x => run_select_extra T
end end.
(* template:
Ltac run_select_extra T ::=
match T with
| if_any_or_throw _ _ _ => constr:(if_any_or_throw_out)
end.
*)
(** [run_select_proj] is used to obtain automatically
the right correctness lemma out of the correctness record *)
Ltac run_select_proj_extra_error HT := fail.
Ltac run_select_proj_extra_ref HT := fail.
Ltac run_select_proj_extra_conversions HT := fail.
Ltac run_select_proj_extra_construct HT := fail.
Ltac run_select_proj_extra_get_value HT := fail.
Ltac run_select_proj H :=
match type of H with ?T = _ => let HT := get_head T in
match HT with
| runs_type_expr => constr:(runs_type_correct_expr)
| runs_type_stat => constr:(runs_type_correct_stat)
| runs_type_prog => constr:(runs_type_correct_prog)
| runs_type_call => constr:(runs_type_correct_call)
| runs_type_construct => constr:(runs_type_correct_construct)
| runs_type_function_has_instance => constr:(runs_type_correct_function_has_instance)
| runs_type_object_has_instance => constr:(runs_type_correct_object_has_instance)
| runs_type_stat_while => constr:(runs_type_correct_stat_while)
| runs_type_stat_do_while => constr:(runs_type_correct_stat_do_while)
| runs_type_stat_for_loop => constr:(runs_type_correct_stat_for_loop)
| runs_type_object_delete => constr:(runs_type_correct_object_delete)
| runs_type_object_get_own_prop => constr:(runs_type_correct_object_get_own_prop)
| runs_type_object_get_prop => constr:(runs_type_correct_object_get_prop)
| runs_type_object_get => constr:(runs_type_correct_object_get)
| runs_type_object_proto_is_prototype_of => constr:(runs_type_correct_object_proto_is_prototype_of)
| runs_type_object_put => constr:(runs_type_correct_object_put)
| runs_type_equal => constr:(runs_type_correct_equal)
| runs_type_to_integer => constr:(runs_type_correct_to_integer)
| runs_type_to_string => constr:(runs_type_correct_to_string)
| runs_type_array_element_list => constr:(runs_type_correct_array_element_list)
| runs_type_object_define_own_prop_array_loop => constr:(runs_type_correct_object_define_own_prop_array_loop)
| ?x => run_select_proj_extra_error HT
| ?x => run_select_proj_extra_ref HT
| ?x => run_select_proj_extra_conversions HT
| ?x => run_select_proj_extra_construct HT
| ?x => run_select_proj_extra_get_value HT
end end.
(** [prove_runs_type_correct] discharges the trivial goal
that consists in invoking the induction hypothesis*)
Ltac prove_runs_type_correct :=
match goal with |- runs_type_correct _ => assumption end.
(* [run_hyp H] exploits the induction hypothesis
on [runs_type_correct] to the hypothesis [H] *)
Ltac run_hyp_core H R :=
let H' := fresh in rename H into H';
let Proj := run_select_proj H' in
lets R: Proj (rm H');
try prove_runs_type_correct.
(** [select_ind_hyp] returns the induction hypothesis
on [runs_type_correct] *)
Ltac select_ind_hyp tt :=
match goal with IH: runs_type_correct _ |- _ => constr:(IH) end.
(* old run_hyp H:
Ltac run_hyp_core H R :=
let H' := fresh in rename H into H';
let IH := select_ind_hyp tt in
let Proj := run_select_proj H' in
lets R: Proj IH (rm H').
*)
Tactic Notation "run_hyp" hyp(H) "as" simple_intropattern(R) :=
run_hyp_core H R.
Tactic Notation "run_hyp" hyp(H) :=
let T := fresh in rename H into T;
run_hyp T as H.
Tactic Notation "run_hyp" :=
match goal with H: _ = result_some _ |- _ => run_hyp H end.
Tactic Notation "run_hyp" "*" hyp(H) :=
run_hyp H; auto*.
Tactic Notation "run_hyp" "*" :=
run_hyp; auto*.
(* [run_pre] exploits the appropriate "out" lemma, whether it comes
from the correctness record or it is an auxiliary lemma. *)
Ltac run_pre_ifres H o1 R1 K :=
let L := run_select_ifres H in
lets (o1&R1&K): L (rm H). (* deconstruction of the "isout" *)
Ltac run_pre_core H o1 R1 K :=
run_pre_ifres H o1 R1 K;
let O1 := fresh "O1" in
try (rename R1 into O1; run_hyp O1 as R1).
Tactic Notation "run_pre" hyp(H) "as" ident(o1) ident(R1) ident(K) :=
let T := fresh in rename H into T;
run_pre_core T o1 R1 K.
Tactic Notation "run_pre_ifres" "as" ident(o1) ident(R1) :=
unfold result_some_out in *; unfold res_to_res_void in * ; unfold result_out in *; unfold res_out in *;
(* LATER: improve unfolds *)
match goal with H: _ = result_some _ |- _ =>
let T := fresh in rename H into T;
run_pre_ifres T o1 R1 H end.
Tactic Notation "run_pre" "as" ident(o1) ident(R1) :=
unfold result_some_out in *; unfold res_to_res_void in * ; unfold result_out in *; unfold res_out in *;
(* LATER: improve unfolds *)
match goal with H: _ = result_some _ |- _ =>
let T := fresh in rename H into T;
run_pre_core T o1 R1 H end.
Tactic Notation "run_pre" "as" ident(o1) :=
let R1 := fresh "R1" o1 in
run_pre as o1 R1.
Tactic Notation "run_pre" :=
let o1 := fresh "o1" in let R1 := fresh "R1" in
run_pre as o1 R1.
(** [run_apply Red o1 R1] applys a reduction rule on a given
[o1] or reduction reaching [o1]. *)
Tactic Notation "run_apply" constr(Red) constr(o1orR1) :=
applys Red o1orR1.
Tactic Notation "run_apply" constr(Red) constr(o1) constr(R1) :=
first [ applys Red (rm R1)
| applys Red o1 ].
(** [run_post] decomposes the conclusion of the "out"
lemma *)
Ltac run_post_run_expr_get_value := fail.
Ltac run_post_extra := fail.
Ltac run_post_core :=
let Er := fresh "Er" in
let Ab := fresh "Ab" in
let S := fresh "S" in
let O1 := fresh "O1" in
let go H X :=
destruct H as [(Er&Ab)|(S&X&O1&H)];
[ try abort | try subst_hyp O1 ] in
match goal with
| H: if_ter_post _ _ _ |- _ =>
let R := fresh "R" in go H R
| H: if_success_post _ _ _ |- _ =>
let rv := fresh "rv" in go H rv
| H: if_value_post _ _ _ |- _ =>
let v := fresh "v" in go H v
| H: if_void_post _ _ _ |- _ =>
destruct H as [(Er&Ab)|(S&O1&H)];
[ try abort | try subst_hyp O1 ]
| H: if_not_throw_post _ _ _ |- _ =>
let R := fresh "R" in
let N := fresh "N" in let v := fresh "v" in
let E := fresh "E" in let L := fresh "L" in
destruct H as [(Er & Ab) | (S & R & O1 & [(N & H) | (N & H)])];
[try abort | try subst_hyp O1 | try abort]
| H: if_break_post _ _ _ |- _ =>
let R := fresh "R" in let E := fresh "E" in
let HT := fresh "HT" in
destruct H as [(Er&E)|(S&R&O1&[(HT&E)|(HT&H)])];
[ try abort | try subst_hyp O1 | try subst_hyp O1 ]
| H: if_object_post _ _ _ |- _ =>
let l := fresh "l" in go H l
| H: if_bool_post _ _ _ |- _ =>
let b := fresh "b" in go H b
| H: if_string_post _ _ _ |- _ =>
let s := fresh "s" in go H s
| H: if_number_post _ _ _ |- _ =>
let m := fresh "m" in go H m
| H: if_prim_post _ _ _ |- _ =>
let w := fresh "w" in go H w
| H: if_ter_spec_post _ _ _ |- _ =>
let R := fresh "R" in go H R
| H: if_success_spec_post _ _ _ |- _ =>
let rv := fresh "rv" in go H rv
| H: if_value_spec_post _ _ _ |- _ =>
let v := fresh "v" in go H v
| H: if_bool_spec_post _ _ _ |- _ =>
let b := fresh "b" in go H b
| H: if_string_spec_post _ _ _ |- _ =>
let s := fresh "s" in go H s
| H: if_object_spec_post _ _ _ |- _ =>
let l := fresh "l" in go H l
| H: if_number_spec_post _ _ _ |- _ =>
let m := fresh "m" in go H m
| H: if_prim_spec_post _ _ _ |- _ =>
let w := fresh "w" in go H w
| H: if_spec_post _ _ _ |- _ =>
let o := fresh "o" in let Er' := fresh "Er" in
let S := fresh "S" in let a := fresh "a" in
destruct H as [(o&Er&Er'&Ab)|(S&a&O1&H)];
[ try abort | try subst_hyp O1 ]
| H: if_any_or_throw_post _ _ _ _ |- _ =>
let R := fresh "R" in