-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlir.v
More file actions
1086 lines (906 loc) · 26.4 KB
/
lir.v
File metadata and controls
1086 lines (906 loc) · 26.4 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
(*
** LIR: Lua Intermediate Language. Syntax and semantics.
*)
Require Import Coq.Logic.Decidable.
Require Import PeanoNat.
Require Import Coq.Strings.String.
Require Import Ascii.
Require Import Bool.
Require Import Nat.
Require Import Coq.Program.Equality.
Require Import LIR.maps.
(*
** Tags for LIR: represent unboxed types and
** tags for boxed values.
*)
Inductive Tag : Set := | TgNil | TgInt | TgTbl | TgFun.
(* Tag equality is decidable and computable *)
Lemma dec_Tag : forall (t1 t2 : Tag), {t1 = t2} + {t1 <> t2}.
Proof. decide equality. Defined.
(*
** Types for LIR: a 'Tag2Type' represents unboxed values,
** IRTStar is the type for boxed values.
*)
Inductive IRType : Set :=
| Tag2Type : Tag -> IRType (* Ground Types *)
| IRTStar : IRType
.
(* Type equality is decidable and computable *)
Lemma dec_IRType : forall (t1 t2 : IRType), {t1 = t2} + {t1 <> t2}.
Proof. decide equality. auto using dec_Tag. Defined.
(*
** Get the tag from a type, if possible.
*)
Definition Type2Tag (t : IRType) : option Tag :=
match t with
| Tag2Type tg => Some tg
| IRTStar => None
end.
Definition IRTNil := Tag2Type TgNil.
Definition IRTInt := Tag2Type TgInt.
Definition IRTTbl := Tag2Type TgTbl.
Definition IRTFun := Tag2Type TgFun.
(*
** Addresses represent tables and functions in memory.
*)
Definition address := nat.
(*
** Syntax for LIR terms (expressions).
*)
Inductive IRE : Set :=
| IRENil : IRE (* nil *)
| IRENum : nat -> IRE (* n - integer literal *)
| IREPlus : IRE -> IRE -> IRE (* e + e *)
| IRENew : IRE (* {} - table constructor *)
| IRETAddr : address -> IRE (* table address - only at runtime *)
| IREFAddr : address -> IRE (* function address - only at runtime *)
| IREGet : IRE -> IRE -> IRE (* e[e] - table access *)
| IRESet : IRE -> IRE -> IRE -> IRE (* e[e] := e - table assignment *)
| IREVar : string -> IRE (* id - variables *)
| IRELet : string -> IRType -> IRE -> IRE -> IRE (* let e:τ = e in e *)
| IREFun : string -> IRE -> IRE (* λx. e *)
| IREApp : IRE -> IRE -> IRE (* e e *)
| IREBox : Tag -> IRE -> IRE (* box[τ] e *)
| IREUnbox : Tag -> IRE -> IRE (* unbox[τ] e *)
.
(* Type environments for LIR *)
Definition IREnvironment := Map IRType.
(*
** Typing rules for LIR terms.
*)
Reserved Notation "Γ '|=' e ':' t" (at level 40, no associativity,
e at next level).
Inductive IRTyping : IREnvironment -> IRE -> IRType -> Prop :=
| IRTyNil : forall Γ, Γ |= IRENil : IRTNil
| IRTyVar : forall Γ var T,
In Γ var = Some T ->
Γ |= IREVar var : T
| IRTyInt : forall Γ n, Γ |= IRENum n : IRTInt
| IRTyPlus : forall Γ e1 e2,
Γ |= e1 : IRTInt ->
Γ |= e2 : IRTInt ->
Γ |= (IREPlus e1 e2) : IRTInt
| IRTyNew : forall Γ, Γ |= IRENew : IRTTbl
| IRTyTAddr : forall Γ addr, Γ |= IRETAddr addr : IRTTbl
| IRTyFAddr : forall Γ addr, Γ |= IREFAddr addr : IRTFun
| IRTyGet : forall Γ e1 e2,
Γ |= e1 : IRTTbl ->
Γ |= e2 : IRTStar ->
Γ |= (IREGet e1 e2) : IRTStar
| IRTySet : forall Γ e1 e2 e3,
Γ |= e1 : IRTTbl ->
Γ |= e2 : IRTStar ->
Γ |= e3 : IRTStar ->
Γ |= (IRESet e1 e2 e3) : IRTNil
| IRTyLet : forall Γ var t t' v body,
var |=> t; Γ |= body : t' ->
Γ |= v : t ->
Γ |= (IRELet var t v body) : t'
| IRTyFun : forall Γ var body,
var |=> IRTStar; Γ |= body : IRTStar ->
Γ |= (IREFun var body) : IRTFun
| IRTyApp : forall Γ e1 e2,
Γ |= e1 : IRTFun ->
Γ |= e2 : IRTStar ->
Γ |= (IREApp e1 e2) : IRTStar
| IRTyBox : forall Γ e (tg : Tag),
Γ |= e : (Tag2Type tg) ->
Γ |= (IREBox tg e) : IRTStar
| IRTyUnbox : forall Γ e tg t,
t = Tag2Type tg ->
Γ |= e : IRTStar ->
Γ |= (IREUnbox tg e) : t
where "Γ '|=' e ':' t" := (IRTyping Γ e t)
.
(*
** Types for LIR terms are unique.
*)
Theorem typeUnique : forall Γ e t t',
(Γ |= e : t) -> (Γ |= e : t') -> t = t'.
Proof.
induction 1; inversion 1; subst; auto; congruence.
Qed.
Unset Elimination Schemes.
(*
** Value predicate for LIR terms: The only values are nil,
** numbers, and addresses, plus boxes of one of those previous values.
** (The type system forbids boxes of boxes.)
*)
Inductive Value : IRE -> Prop :=
| Vnil : Value IRENil
| Vnum : forall n, Value (IRENum n)
| Vtbl : forall a, Value (IRETAddr a)
| Vfun : forall a, Value (IREFAddr a)
| Vbox : forall gt v, Value v -> Value (IREBox gt v)
.
Set Elimination Schemes.
Scheme Value_ind := Induction for Value Sort Prop.
(*
** Value proofs are unique.
*)
Lemma Value_unique: forall v (Vv1 Vv2 : Value v), Vv1 = Vv2.
Proof.
intros.
dependent induction Vv1;
dependent destruction Vv2; trivial.
f_equal; auto.
Qed.
(*
** Equivalent to 'Value', but computable
*)
Fixpoint isValue (e : IRE) : bool :=
match e with
| IRENil => true
| IRENum _ => true
| IRETAddr _ => true
| IREFAddr _ => true
| IREBox _ e => isValue e
| _ => false
end.
(*
** 'Value' and 'isValue' agree.
*)
Lemma isValueCorrect : forall e, Value e <-> isValue e = true.
Proof.
split; induction e; trivial;
inversion 1; subst; auto using Value.
Qed.
(*
** A term inside a box value is a value.
*)
Lemma valBoxVal : forall gt e, Value (IREBox gt e) -> Value e.
Proof.
inversion 1; trivial.
Qed.
(*
** Canonical forms for Values.
*)
Lemma valnil : forall Γ e,
Γ |= e : IRTNil -> Value e -> e = IRENil.
Proof.
inversion 1; inversion 1; subst; congruence.
Qed.
Lemma valint : forall Γ e,
Γ |= e : IRTInt -> Value e -> exists n, e = IRENum n.
Proof.
inversion 1; inversion 1; try discriminate.
eexists; auto 1.
Qed.
Lemma valtbl : forall Γ e,
Γ |= e : IRTTbl -> Value e -> exists a, e = IRETAddr a.
Proof.
inversion 1; inversion 1; try discriminate; eauto.
Qed.
Lemma valfun : forall Γ e,
Γ |= e : IRTFun -> Value e -> exists a, e = IREFAddr a.
Proof.
inversion 1; inversion 1; try discriminate; eauto.
Qed.
Lemma valbox : forall Γ e, Γ |= e : IRTStar -> Value e ->
exists o t, e = IREBox t o /\ (Γ |= o : Tag2Type t) /\ Value o.
Proof.
intros * HT HV.
inversion HV;
inversion HT; subst; try discriminate.
match goal with
| [H: IREBox _ _ = IREBox _ _ |- _ ] => injection H; intros; subst end.
eexists; eexists; auto using Value.
Qed.
(*
** Table indices.
** To simplify some proofs, we encode any value used as index as an
** 'Index'. We can think of indices as isomorphic to bxoed values, so
** that in the end only boxed values are used as indices. From another
** angle, indices iron out the differences between boxed and unboxed
** values. Later we will prove that for two values, their indices are
** equal iff they are equal up to boxes and unboxes.
*)
Inductive Index : Set :=
| I : nat -> Tag -> Index
| NI : Index (* indices for non-values *)
.
(*
** Normalize values used as indices, so that boxed and unboxed values
** give the same index.
*)
Fixpoint ToIndex (e : IRE) : Index :=
match e with
| IRENil => I 0 TgNil
| IRENum n => I n TgInt
| IRETAddr a => I a TgTbl
| IREFAddr a => I a TgFun
| IREBox t e' => ToIndex e'
| IREUnbox t e' => ToIndex e'
| _ => NI
end.
(*
** This is an inverse of 'ToIndex'. It shows that Indices fully encode
** boxed values.
*)
Definition FromIndex (idx : Index) : IRE :=
match idx with
| I _ TgNil => IREBox TgNil IRENil
| I n TgInt => IREBox TgInt (IRENum n)
| I a TgTbl => IREBox TgTbl (IRETAddr a)
| I a TgFun => IREBox TgFun (IREFAddr a)
| NI => IREPlus (IRENum 0) (IRENum 0) (* any non-value would do *)
end.
(*
** For any ground value, FromIndex recovers the original value from its
** index (up to boxes).
*)
Lemma IndexValueGround : forall v tg,
Value v ->
MEmpty |= v : Tag2Type tg ->
FromIndex (ToIndex v) = IREBox tg v.
Proof.
induction 1; inversion 1; subst; simpl; trivial.
Qed.
(*
** 'FromIndex' is correct: For any boxed value, FromIndex recovers the
** original value from its index.
*)
Lemma FromIndexCorrect : forall v,
Value v ->
MEmpty |= v : IRTStar ->
FromIndex (ToIndex v) = v.
Proof.
induction 1; inversion 1; subst.
simpl. auto using IndexValueGround.
Qed.
(* Index equality is decidable. *)
Lemma Index_dec : forall (i1 i2 : Index), {i1 = i2} + {i1 <> i2}.
Proof.
decide equality; auto using Nat.eq_dec, dec_Tag.
Defined.
Ltac breakIndexDec :=
repeat match goal with
| [ |- context C [Nat.eq_dec ?V1 ?V2] ] =>
destruct (Nat.eq_dec V1 V2)
| [ |- context C [Index_dec ?V1 ?V2] ] =>
destruct (Index_dec V1 V2) eqn:? ;
try easy
| [ H : context C [Nat.eq_dec ?V1 ?V2] |- _] =>
destruct (Nat.eq_dec V1 V2); subst
end.
(*
** Type representing terms that are values.
*)
Inductive ExpValue : Set :=
| EV : forall e, Value e -> ExpValue.
(* Extract the term from an ExpValue *)
Definition EV2Val (me : ExpValue) : IRE :=
match me with
| EV v _ => v
end.
(*
** Memory for LIR.
*)
Inductive Mem : Set :=
| EmptyMem : Mem (* the empty memory *)
| UpdateT : (* table entries: address[index] := value *)
address -> Index -> ExpValue -> Mem -> Mem
| UpdateF : (* closures: address := λstring.expression *)
address -> string -> IRE -> Mem -> Mem.
Definition BoxedNil : IRE := IREBox TgNil IRENil.
Definition BoxedNilValue : Value BoxedNil := Vbox TgNil IRENil Vnil.
(*
** Get the memory content of a table entry, or 'box nil' for undefined
** entries.
*)
Fixpoint queryT (a : address) (idx : IRE) (m : Mem) : IRE :=
match m with
| EmptyMem => BoxedNil
| UpdateT a' idx' v m' => if Nat.eq_dec a a' then
if Index_dec (ToIndex idx) idx'
then (EV2Val v)
else queryT a idx m'
else queryT a idx m'
| UpdateF _ _ _ m' => queryT a idx m'
end.
(*
** Equal indices return equal contents.
*)
Lemma queryTIndexEq : forall m a idx idx',
ToIndex idx = ToIndex idx' ->
queryT a idx m = queryT a idx' m.
Proof.
intros * HEq.
induction m; trivial.
simpl.
breakIndexDec; subst; trivial;
exfalso; auto.
Qed.
(*
** Get the memory content for closures
*)
Fixpoint queryF (a : address) (m : Mem) : (string * IRE) :=
match m with
| EmptyMem => (""%string, IRELet "" IRTStar (IREVar "") BoxedNil)
| UpdateT a' _ _ m' => queryF a m'
| UpdateF a' var body m' => if Nat.eq_dec a a' then (var, body)
else queryF a m'
end.
(*
** Create a fresh address for a memory.
*)
Fixpoint freshaux (m : Mem) : address :=
match m with
| EmptyMem => 1
| UpdateT _ _ _ m' => S (freshaux m')
| UpdateF _ _ _ m' => S (freshaux m')
end.
(*
** Create a fresh address for a table and initializes
** it with {Nil = Nil}.
*)
Definition freshT (m : Mem) : (address * Mem) :=
let f := freshaux m in
(f, UpdateT f (ToIndex IRENil) (EV BoxedNil BoxedNilValue) m).
(*
** Create a fresh address for a function and initializes
** it with the given values.
*)
Definition freshF (m : Mem) (v : string) (b : IRE) : (address * Mem) :=
let f := freshaux m in
(f, UpdateF f v b m).
(*
** Substitution of closed LIR terms for variables.
*)
Reserved Notation "'[' x ':=' s ']' t" (at level 20, x constr).
Fixpoint substitution (var : string) (y : IRE) (e : IRE) : IRE :=
match e with
| IRENil => e
| IRENum n => e
| IREPlus e1 e2 => IREPlus ([var := y] e1) ([var := y] e2)
| IRENew => e
| IRETAddr a => e
| IREFAddr a => e
| IREGet e1 e2 => IREGet ([var := y] e1) ([var := y] e2)
| IRESet e1 e2 e3 => IRESet ([var := y] e1) ([var := y] e2) ([var := y] e3)
| IREVar var' => if string_dec var var' then y else e
| IRELet var' t v body => if string_dec var var' then
IRELet var' t ([var := y] v) body
else
IRELet var' t ([var := y] v) ([var := y] body)
| IREFun var' body => if string_dec var var' then e
else IREFun var' ([var := y] body)
| IREApp e1 e2 => IREApp ([var := y] e1) ([var := y] e2)
| IREBox tg e => IREBox tg ([var := y] e)
| IREUnbox tg e => IREUnbox tg ([var := y] e)
end
where "'[' x ':=' s ']' t" := (substitution x s t)
.
(*
** Extending an environment preserves typing
*)
Lemma inclusion_typing : forall Γ Γ' e te,
Γ |= e : te ->
inclusion Γ Γ' ->
Γ' |= e : te.
Proof.
intros * Hty Hin.
generalize dependent Γ'.
induction Hty; eauto using IRTyping, inclusion_update.
Qed.
(*
** Particular case when extending the empty environment
*)
Lemma typing_empty : forall Γ e te, MEmpty |= e : te -> Γ |= e : te.
Proof.
eauto using inclusion_typing, inclusion_empty.
Qed.
(*
** Substitution preserves typing
*)
Lemma subst_typing : forall e2 Γ var tv te e1,
(var |=> tv; Γ) |= e2 : te ->
MEmpty |= e1 : tv ->
Γ |= ([var := e1] e2) : te.
Proof.
induction e2; intros * HT2 HT1;
simpl; inversion HT2; subst;
breakStrDec;
eauto 6 using inclusion_typing, inclusion_shadow, inclusion_permute,
IRTyping, typing_empty, InNotEq.
- replace te with tv by congruence.
eauto using typing_empty.
Qed.
(*
** Reduction steps for LIR terms
*)
Reserved Notation "m '/' e --> m1 '/' e1"
(at level 40, e at level 39, m1 at level 39, e1 at level 39).
Reserved Notation "m '/' e --> 'fail'"
(at level 40, e at level 39).
Inductive step : Mem -> IRE -> Mem -> IRE -> Prop :=
| StPlus1 : forall m e1 e2 m' e1',
m / e1 --> m' / e1' ->
m / IREPlus e1 e2 --> m' / IREPlus e1' e2
| StPlus2 : forall m e1 e2 m' e2',
Value e1 ->
m / e2 --> m' / e2' ->
m / IREPlus e1 e2 --> m' / IREPlus e1 e2'
| StPlus : forall m n1 n2,
m / IREPlus (IRENum n1) (IRENum n2) --> m / IRENum (n1 + n2)
| StNew : forall m m' free,
(free, m') = freshT m ->
m / IRENew --> m' / IRETAddr free
| StGet1 : forall m e1 e2 m' e1',
m /e1 --> m' /e1' ->
m / IREGet e1 e2 --> m' / IREGet e1' e2
| StGet2 : forall m e1 e2 m' e2',
Value e1 ->
m /e2 --> m' /e2' ->
m / IREGet e1 e2 --> m' / IREGet e1 e2'
| StGet : forall m a idx,
Value idx ->
m / IREGet (IRETAddr a) idx --> m / queryT a idx m
| StSet1 : forall m e1 e2 e3 m' e1',
m / e1 --> m' / e1' ->
m / IRESet e1 e2 e3 --> m' / IRESet e1' e2 e3
| StSet2 : forall m e1 e2 e3 m' e2',
Value e1 ->
m / e2 --> m' / e2' ->
m / IRESet e1 e2 e3 --> m' / IRESet e1 e2' e3
| StSet3 : forall m e1 e2 e3 m' e3',
Value e1 -> Value e2 ->
m / e3 --> m' / e3' ->
m / IRESet e1 e2 e3 --> m' / IRESet e1 e2 e3'
| StSet : forall m a idx v,
Value idx ->
forall Vv : Value v,
m / IRESet (IRETAddr a) idx v -->
UpdateT a (ToIndex idx) (EV v Vv) m / IRENil
| StLet1 : forall var t body m e m' e',
m / e --> m' / e' ->
m / IRELet var t e body --> m' / IRELet var t e' body
| StLet : forall var t e body m,
Value e ->
m / IRELet var t e body --> m / [var := e] body
| StFun : forall m m' v b free,
(free, m') = freshF m v b ->
m / IREFun v b --> m' / IREFAddr free
| StApp1 : forall m e1 e2 m' e1',
m / e1 --> m' / e1' ->
m / IREApp e1 e2 --> m' / IREApp e1' e2
| StApp2 : forall m e1 e2 m' e2',
Value e1 ->
m / e2 --> m' / e2' ->
m / IREApp e1 e2 --> m' / IREApp e1 e2'
| StApp : forall m a var body v,
Value v ->
(var, body) = queryF a m ->
m / IREApp (IREFAddr a) v --> m / IRELet var IRTStar v body
| StBox1 : forall m t e m' e',
m / e --> m' / e' ->
m / IREBox t e --> m' / IREBox t e'
| StUnbox1 : forall m t e m' e',
m / e --> m' / e' ->
m / IREUnbox t e --> m' / IREUnbox t e'
| StUnbox : forall m t v,
Value v ->
m / IREUnbox t (IREBox t v) --> m / v
where "m / e --> m1 / e1" := (step m e m1 e1).
(*
** Fail reduction for LIR terms
*)
Inductive stepF : Mem -> IRE -> Prop :=
| StPlus1F : forall m e1 e2,
m / e1 --> fail ->
m / IREPlus e1 e2 --> fail
| StPlus2F : forall m e1 e2,
Value e1 ->
m / e2 --> fail ->
m / IREPlus e1 e2 --> fail
| StGet1F : forall m e1 e2,
m /e1 --> fail ->
m / IREGet e1 e2 --> fail
| StGet2F : forall m e1 e2,
Value e1 ->
m /e2 --> fail ->
m / IREGet e1 e2 --> fail
| StSet1F : forall m e1 e2 e3,
m / e1 --> fail ->
m / IRESet e1 e2 e3 --> fail
| StSet2F : forall m e1 e2 e3,
Value e1 ->
m / e2 --> fail ->
m / IRESet e1 e2 e3 --> fail
| StSet3F : forall m e1 e2 e3,
Value e1 -> Value e2 ->
m / e3 --> fail ->
m / IRESet e1 e2 e3 --> fail
| StLet1F : forall var t e body m,
m / e --> fail ->
m / IRELet var t e body --> fail
| StApp1F : forall m e1 e2,
m / e1 --> fail ->
m / IREApp e1 e2 --> fail
| StApp2F : forall m e1 e2,
Value e1 ->
m / e2 --> fail ->
m / IREApp e1 e2 --> fail
| StBox1F : forall m t e,
m / e --> fail ->
m / IREBox t e --> fail
| StUnbox1F : forall m t e,
m / e --> fail ->
m / IREUnbox t e --> fail
| StUnboxF : forall m t t' v,
t <> t' ->
Value v ->
m / IREUnbox t (IREBox t' v) --> fail
where "m / e --> 'fail'" := (stepF m e)
.
(*
** Well-Typed Memory (heap)
*)
Inductive mem_correct : Mem -> Prop :=
| MCE : mem_correct EmptyMem
| MCT : forall a idx v m,
MEmpty |= EV2Val v : IRTStar ->
mem_correct m ->
mem_correct (UpdateT a idx v m)
| MCF : forall a var body m,
var |=> IRTStar; MEmpty |= body : IRTStar ->
mem_correct m ->
mem_correct (UpdateF a var body m)
.
(*
** All terms stored in memory tables are values
*)
Lemma MCValue : forall m a n, Value (queryT a n m).
Proof.
intros.
induction m; eauto using Value.
destruct e. simpl.
breakIndexDec; trivial.
Qed.
(*
** All terms stored in a table of a correct memory have type '*'
*)
Lemma MCTy : forall m a n Γ,
mem_correct m -> Γ |= (queryT a n m) : IRTStar.
Proof.
induction 1; simpl; breakIndexDec;
eauto using typing_empty, IRTyping.
Qed.
(*
** All functions stored in a correct memory have type '* -> *'.
*)
Lemma MCTyF : forall m a var body Γ,
(var, body) = queryF a m ->
mem_correct m ->
var |=> IRTStar; Γ |= body : IRTStar.
Proof.
intros * HEq HMC.
induction HMC; simpl in HEq; breakIndexDec; eauto;
injection HEq; intros; subst;
unfold BoxedNil;
eauto using IRTyping, inclusion_typing, inclusion_update, inclusion_empty.
Qed.
(*
** Table allocation preserves memory correctness
*)
Lemma mem_correct_freshT : forall m m' free,
mem_correct m -> (free,m') = freshT m -> mem_correct m'.
Proof.
unfold freshT. inversion 2.
eauto using mem_correct, IRTyping.
Qed.
(*
** Function allocation preserves memory correctness
*)
Lemma mem_correct_freshF : forall m m' var body free,
var |=> IRTStar; MEmpty |= body : IRTStar ->
mem_correct m ->
(free,m') = freshF m var body ->
mem_correct m'.
Proof.
unfold freshF.
inversion 3; subst.
eauto using mem_correct.
Qed.
(*
** Executing an evaluation step preserves memory
** correctness
*)
Lemma memPreservation : forall (m m' : Mem) e e' t,
mem_correct m ->
MEmpty |= e : t ->
m / e --> m' / e' ->
mem_correct m'.
Proof.
intros * ? HTy ?.
generalize dependent e'.
remember MEmpty as Γ.
induction HTy; inversion 1; subst;
eauto using mem_correct_freshT, mem_correct_freshF, mem_correct.
Qed.
(*
** A well-typed box has the correct type of element inside it.
*)
Lemma boxTyping : forall e t,
MEmpty |= IREBox t e : IRTStar -> MEmpty |= e : Tag2Type t.
Proof. inversion 1; trivial. Qed.
(*
** Preservation of types for LIR terms
*)
Lemma expPreservation : forall m e t m' e',
mem_correct m ->
MEmpty |= e : t ->
m / e --> m' / e' ->
MEmpty |= e' : t.
Proof.
intros * Hmc HT.
generalize dependent e'.
remember MEmpty as Γ.
induction HT; inversion 1; subst;
eauto using IRTyping, MCTy, boxTyping, MCTyF, subst_typing.
Qed.
(*
** Main Preservation theorem for LIR
** (type preservation of memory and term)
*)
Theorem Preservation : forall m e t m' e',
mem_correct m ->
MEmpty |= e : t ->
m / e --> m' / e' ->
mem_correct m' /\ MEmpty |= e' : t.
Proof. intuition; eauto using memPreservation,expPreservation. Qed.
(*
** Values cannot be reduced
*)
Theorem value_normal : forall m e m' v,
m / e --> m' / v ->
Value e ->
False.
Proof.
intros * Hst HV.
generalize dependent m'.
generalize dependent v.
induction HV; inversion 1; subst; eauto.
Qed.
(*
** The same, for fail steps
*)
Theorem value_normalF : forall m e,
m / e --> fail ->
Value e ->
False.
Proof.
induction 1; inversion 1; subst; auto.
Qed.
Ltac open_value rule :=
match goal with
| [ Ht : MEmpty |= ?e : _,
Hv : Value ?e |- _] =>
eapply rule in Ht; trivial; decompose [ex or and] Ht
end.
(*
** Regular steps and fail steps are excludent.
*)
Theorem failNfail : forall e m e' m' T,
MEmpty |= e : T ->
m / e --> m' / e' ->
m / e --> fail ->
False.
Proof.
intros * HTy St HStF.
generalize dependent T.
induction St; intros; inversion HTy; inversion HStF; subst;
eauto using value_normal, value_normalF, Value.
Qed.
(*
** Steps are deterministic, for memories.
*)
Lemma DeterministicStepM : forall m e m1 e1 m2 e2,
m / e --> m1 / e1 ->
m / e --> m2 / e2 ->
m1 = m2.
Proof.
intros * HSt1.
generalize dependent e2.
induction HSt1; inversion 1; subst; eauto;
try (exfalso; eauto using value_normal, Value; fail);
try congruence.
f_equal. f_equal. eauto using Value_unique.
Qed.
(*
** Steps are deterministic, for terms.
*)
Theorem DeterministicStep : forall m e m1 e1 m2 e2,
m / e --> m1 / e1 ->
m / e --> m2 / e2 ->
e1 = e2.
Proof.
intros * HSt1.
generalize dependent e2.
induction HSt1; inversion 1; subst; eauto;
try (exfalso; eauto using value_normal, Value; fail);
f_equal; eauto; congruence.
Qed.
(*
** Progress for LIR terms
*)
Theorem Progress : forall m e t,
MEmpty |= e : t ->
Value e \/
(m / e --> fail \/ exists m' e', m / e --> m' / e').
Proof.
intros * Hty.
remember MEmpty as Γ.
induction Hty; subst;
(* variables *)
try match goal with
| [H : In MEmpty _ = _ |- _] => inversion H
end;
(* break induction hypotheses *)
repeat match goal with
| [H : _ -> _ \/ (_ \/ _) |- _] =>
decompose [or ex and] (H eq_refl); clear H
end;
subst;
(* trivial steps and failures *)
try (right; auto using stepF; fail);
(* trivial values *)
auto using Value;
(* break values *)
repeat open_value valint;
try open_value valtbl;
try open_value valfun;
try open_value valbox;
try match goal with
| [ Ht : MEmpty |= ?e : _,
Hv : Value ?e |- _] => eapply vallam in Ht; trivial;
decompose [ex or and] Ht
end; subst;
(* try cases that became easy after breaking values *)
try (unshelve (right; right; eauto using step, eq_refl); trivial; fail).
- (* cannot find the correct sequence with StSet3 ? *)
right. right. eexists. eexists.
eapply StSet3; eauto.
- (* App *)
right. right.
match goal with [a: address |- _] => destruct (queryF a m) eqn:? end.
eauto using step, Value.
- (* unboxing has to handle success vs. failure *)
match goal with | [t1:Tag, t2:Tag |- _] => destruct (dec_Tag t1 t2) end;
right; subst; eauto using step, stepF.
Qed.
(*
** Multisteps
*)
Reserved Notation "m '/' e -->* m1 '/' e1"
(at level 40, e at level 39, m1 at level 39, e1 at level 39).
Reserved Notation "m '/' e -->* 'fail'"
(at level 40, e at level 39).
Inductive multistep : Mem -> IRE -> Mem -> IRE -> Prop :=
| MStRefl : forall m e, m / e -->* m / e
| MStMStep : forall m e m' e' m'' e'',
m / e --> m' / e' ->
m' / e' -->* m'' / e'' ->
m / e -->* m'' / e''
where "m / e -->* m1 / e1" := (multistep m e m1 e1)
.
Inductive multistepF : Mem -> IRE -> Prop :=
| MStStepF : forall m e m' e',
m / e -->* m' / e' ->
m' / e' --> fail ->
m / e -->* fail
where "m / e -->* 'fail'" := (multistepF m e)
.
(*
** An evaluation that fails in one step fails in multiple steps.
*)
Lemma multistepF1 : forall m e, m / e --> fail -> m / e -->* fail.
Proof.
eauto using multistep, multistepF.
Qed.
(*
** Multistep is transitive
*)
Lemma multiTrans : forall m0 e0 m1 e1 m2 e2,
m0 / e0 -->* m1 / e1 ->
m1 / e1 -->* m2 / e2 ->
m0 / e0 -->* m2 / e2.
Proof.
induction 1; eauto using multistep.
Qed.
(*
** Multistep subsumes step
*)
Lemma multistep1 : forall m0 e0 m1 e1,
m0 / e0 --> m1 / e1 ->
m0 / e0 -->* m1 / e1.
Proof. eauto using multistep. Qed.
(*
** Soundness for LIR. (Progress for multiple steps.)