-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathML_SP_Eval.v
2938 lines (2765 loc) · 92.6 KB
/
ML_SP_Eval.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
(***************************************************************************
* Principality of type inference for mini-ML with structural polymorphism *
* Jacques Garrigue, January 2009 *
***************************************************************************)
Set Implicit Arguments.
Require Import Arith List Metatheory.
Require Import ML_SP_Definitions.
Require Import ML_SP_Rename.
Require Omega.
Require Import Relations.
Module MkEval(Cstr:CstrIntf)(Const:CstIntf).
Module Rename := MkRename(Cstr)(Const).
Import Rename.Sound.
Import Infra.
Import Defs.
Import Metatheory_Env.Env.
Inductive clos : Set :=
| clos_abs : trm -> list clos -> clos
| clos_const : Const.const -> list clos -> clos.
Section ClosInd.
Variable P : clos -> Prop.
Hypothesis Habs : forall t l, list_forall P l -> P (clos_abs t l).
Hypothesis Hconst : forall c l, list_forall P l -> P (clos_const c l).
Fixpoint clos_ind' (c : clos) : P c :=
match c return P c with
| clos_abs t l => Habs t (map_prop P clos_ind' l)
| clos_const c l => Hconst c (map_prop P clos_ind' l)
end.
End ClosInd.
Inductive closed_n : nat -> trm -> Prop :=
| cln_fvar : forall n x, closed_n n (trm_fvar x)
| cln_bvar : forall n m, m < n -> closed_n n (trm_bvar m)
| cln_abs : forall n t, closed_n (S n) t -> closed_n n (trm_abs t)
| cln_let : forall n t1 t2,
closed_n n t1 -> closed_n (S n) t2 -> closed_n n (trm_let t1 t2)
| cln_app : forall n t1 t2,
closed_n n t1 -> closed_n n t2 -> closed_n n (trm_app t1 t2)
| cln_cst : forall n c, closed_n n (trm_cst c).
Lemma trm_inst_rec_more : forall tl t1 t n,
closed_n (S n + List.length tl) t ->
list_forall term tl ->
trm_open_rec n t1 (trm_inst_rec (S n) tl t) = trm_inst_rec n (t1 :: tl) t.
Proof.
Import Omega.
intros.
remember (S n + length tl) as z.
gen n; induction H; intros; subst; auto.
unfold trm_inst_rec.
destruct (le_lt_dec (S n0) m).
destruct (le_lt_dec n0 m); try solve [elimtype False; omega].
remember (m - S n0) as z.
replace (m - n0) with (S z) by omega.
assert (z < length tl) by omega.
simpl.
clear -H0 H1; gen z; induction H0; simpl; intros.
elimtype False; omega.
destruct z. rewrite* <- Infra.trm_open_rec.
assert (z < length L) by omega.
auto*.
destruct (le_lt_dec n0 m).
assert (n0 = m) by omega. subst.
replace (m-m) with 0 by omega. simpl.
destruct* (m === m).
simpl. destruct* (n0 === m). subst; elimtype False; omega.
simpl. rewrite* (IHclosed_n (S n0)).
simpl. rewrite* (IHclosed_n1 n0). rewrite* (IHclosed_n2 (S n0)).
simpl. rewrite* (IHclosed_n1 n0). rewrite* (IHclosed_n2 n0).
Qed.
Lemma term_trm_inst_closed : forall t tl,
closed_n (length tl) t -> list_forall term tl -> term (trm_inst t tl).
Proof.
unfold trm_inst; induction t; intros; inversions H; simpl; auto.
rewrite <- minus_n_O.
generalize (trm_bvar n).
clear H; gen tl; induction n; intros; destruct tl; try elim (lt_n_O _ H3).
simpl. inversion* H0.
simpl.
apply IHn. inversion* H0.
apply* lt_S_n.
apply (@term_abs {}); intros.
unfold trm_open. rewrite* trm_inst_rec_more.
apply* (@term_let {}); intros.
unfold trm_open. rewrite* trm_inst_rec_more.
Qed.
Inductive clos_ok : clos -> Prop :=
| clos_ok_abs : forall t cls,
list_forall clos_ok cls ->
closed_n (S (length cls)) t ->
clos_ok (clos_abs t cls)
| clos_ok_const : forall c cls,
list_forall clos_ok cls ->
List.length cls <= Const.arity c ->
clos_ok (clos_const c cls).
(* Reset clos_ok_ind. *)
Hint Constructors clos_ok : core.
Hint Extern 1 (clos_ok ?x) => solve [list_forall_find clos_ok x] : core.
Section ClosOkInd.
Variable P : clos -> Prop.
Hypothesis Habs : forall t cls,
list_forall clos_ok cls ->
closed_n (S (length cls)) t ->
list_forall P cls -> P (clos_abs t cls).
Hypothesis Hconst : forall c cls,
list_forall clos_ok cls ->
length cls <= Const.arity c ->
list_forall P cls -> P (clos_const c cls).
Lemma clos_ok_ind' : forall c, clos_ok c -> P c.
Proof.
Hint Resolve Habs Hconst : core.
intros c H; induction c using clos_ind'; inversion* H.
Qed.
End ClosOkInd.
Fixpoint clos2trm (c:clos) : trm :=
match c with
| clos_abs t l => trm_inst (trm_abs t) (List.map clos2trm l)
| clos_const cst l => const_app cst (List.map clos2trm l)
end.
Lemma clos_ok_term : forall cl,
clos_ok cl -> term (clos2trm cl).
Proof.
induction 1 using clos_ok_ind'; simpl.
apply term_trm_inst_closed.
rewrite map_length.
constructor; auto.
apply* list_forall_map. intros; auto.
unfold const_app.
cut (term (trm_cst c)); auto.
generalize (trm_cst c).
clear -H H1; induction H; intros; simpl*.
inversion* H1.
Qed.
Record frame : Set := Frame
{ frm_benv : list clos; frm_app : list clos; frm_trm : trm }.
Inductive frame_ok : frame -> Prop :=
frm_ok : forall benv app trm,
list_forall clos_ok benv ->
list_forall clos_ok app ->
closed_n (length benv) trm ->
frame_ok (Frame benv app trm).
Hint Constructors frame_ok : core.
Definition is_bvar t :=
match t with trm_bvar _ => true | _ => false end.
Definition app_trm t1 t2 :=
match t1 with
| trm_abs t => trm_let t2 t
| _ => trm_app t1 t2
end.
Definition app2trm t args :=
List.fold_left app_trm (List.map clos2trm args) t.
Definition inst t benv := trm_inst t (List.map clos2trm benv).
Fixpoint stack2trm t0 (l : list frame) {struct l} : trm :=
match l with
| nil => t0
| Frame benv args t :: rem =>
let t1 := inst t benv in
let t2 := if is_bvar t0 then t1 else app_trm t1 t0 in
stack2trm (app2trm t2 args) rem
end.
Inductive eval_res : Set :=
| Result : nat -> clos -> eval_res
| Inter : list frame -> eval_res.
Inductive result_ok : eval_res -> Prop :=
| rok_R : forall n cl, clos_ok cl -> result_ok (Result n cl)
| rok_I : forall fl, list_forall frame_ok fl -> result_ok (Inter fl).
Hint Constructors result_ok : core.
Definition res2trm res :=
match res with
| Result _ cl => clos2trm cl
| Inter l => stack2trm trm_def l
end.
Definition clos_def := clos_abs trm_def nil.
Lemma clos_ok_def : clos_ok clos_def.
Proof.
unfold clos_def.
constructor. auto.
unfold trm_def.
simpl.
constructor.
auto.
Qed.
Hint Resolve clos_ok_def : core.
Lemma clos_ok_nil : forall c, clos_ok (clos_const c nil).
Proof.
intros; constructor; auto. simpl; omega.
Qed.
Hint Resolve clos_ok_nil : core.
Definition trm2clos (benv : list clos) (fenv : env clos) t :=
match t with
| trm_bvar n => nth n benv clos_def
| trm_fvar v =>
match get v fenv with
| None => clos_def
| Some c => c
end
| trm_cst c => clos_const c nil
| trm_abs t1 => clos_abs t1 benv
| trm_let _ _ | trm_app _ _ => clos_def
end.
Definition trm2app t :=
match t with
| trm_app t1 t2 => Some (t1, t2)
| trm_let t2 t1 => Some (trm_abs t1, t2)
| _ => None
end.
Lemma clos_ok_nth : forall benv n0,
list_forall clos_ok benv ->
clos_ok (nth n0 benv clos_def).
Proof.
intros.
destruct (le_lt_dec (length benv) n0).
rewrite* nth_overflow.
apply (list_forall_out H).
apply* nth_In.
Qed.
Hint Resolve clos_ok_nth : core.
Inductive equiv_clos : clos -> clos -> Prop :=
| Equiv_clos_abs : forall t benv t' benv',
inst (trm_abs t) benv = inst (trm_abs t') benv' ->
equiv_clos (clos_abs t benv) (clos_abs t' benv')
| Equiv_clos_const : forall c args args',
list_forall2 equiv_clos args args' ->
equiv_clos (clos_const c args) (clos_const c args').
Hint Constructors equiv_clos : core.
Lemma equiv_cl : forall cl1 cl2,
equiv_clos cl1 cl2 -> clos2trm cl1 = clos2trm cl2.
Proof.
induction cl1 using clos_ind'; intros; inversions H0; simpl*.
apply f_equal.
clear -H H4.
gen args'; induction H; intros; inversions H4; simpl*.
rewrite* (IHlist_forall lb).
rewrite* (H0 b).
Qed.
Hint Resolve equiv_cl : core.
Lemma equiv_cls : forall cls1 cls2,
list_forall2 equiv_clos cls1 cls2 ->
List.map clos2trm cls1 = List.map clos2trm cls2.
Proof.
induction 1; simpl; intros; auto*.
apply* f_equal2.
Qed.
Definition equiv_frame f1 f2 :=
inst (frm_trm f1) (frm_benv f1) = inst (frm_trm f2) (frm_benv f2) /\
list_forall2 equiv_clos (frm_app f1) (frm_app f2).
Lemma equiv_clos_refl : forall cl, equiv_clos cl cl.
Proof.
induction cl using clos_ind'; constructor; auto.
induction H; auto.
Qed.
Hint Resolve equiv_clos_refl : core.
Lemma equiv_cl_nth : forall n cls1 cls2,
list_forall2 equiv_clos cls1 cls2 ->
equiv_clos (nth n cls1 clos_def) (nth n cls2 clos_def).
Proof.
intros; revert n; induction H; intros. simpl*.
destruct n; simpl*.
Qed.
Module Mk2(Delta:DeltaIntf).
Module Rename2 := Rename.Mk2(Delta).
Import Rename2.
Import Sound2.
Import JudgInfra.
Import Judge.
Definition Gc := (false, GcAny).
Lemma clos_ok_value : forall cl,
clos_ok cl -> value (clos2trm cl).
Proof.
unfold value.
induction 1 using clos_ok_ind'; simpl;
assert (list_forall term (List.map clos2trm cls))
by (clear -H; apply* list_forall_map; auto using clos_ok_term).
exists 0. unfold trm_inst. simpl. constructor.
apply (@term_abs {}). intros.
unfold trm_open. rewrite* trm_inst_rec_more.
fold (trm_inst t (trm_fvar x :: List.map clos2trm cls)).
apply* term_trm_inst_closed. simpl. rewrite* map_length.
rewrite map_length; simpl*.
set (n := Const.arity c - length cls).
exists n. unfold const_app.
set (t := trm_cst c).
assert (valu (Const.arity c) t) by (unfold t; auto).
replace (Const.arity c) with (n + length cls)
in H3 by (unfold n; omega).
gen t n; clear H H0; induction H1; simpl in *; intros.
rewrite <- plus_n_O in H3. auto.
inversions H2; clear H2.
apply* IHlist_forall.
destruct H.
apply* value_app.
replace (S (n + length L)) with (n + S (length L)) by omega; auto.
Qed.
Hint Resolve clos_ok_value : core.
Lemma list_for_n_value : forall n cls,
list_for_n clos_ok n cls ->
list_for_n value n (List.map clos2trm cls).
Proof.
split. rewrite* map_length.
destruct H; apply* list_forall_map.
Qed.
Module Type SndHypIntf2.
Include SndHypIntf.
Parameter reduce_clos : Const.const -> list clos -> clos * list clos.
Parameter reduce_clos_regular : forall c cls cl' cls',
reduce_clos c cls = (cl', cls') ->
list_forall clos_ok cls ->
list_forall clos_ok (cl' :: cls').
Parameter reduce_clos_ext : forall c args args',
list_forall2 equiv_clos args args' ->
let (cl,arg) := reduce_clos c args in
let (cl',arg') := reduce_clos c args' in
equiv_clos cl cl' /\ list_forall2 equiv_clos arg arg'.
Parameter reduce_clos_sound :
forall c cls (CLS : list_for_n clos_ok (S(Const.arity c)) cls) K E T,
K; E |Gc|= const_app c (List.map clos2trm cls) ~: T ->
let (cl', cls') := reduce_clos c cls in
clos_ok cl' /\ list_forall clos_ok cls' /\
fold_left trm_app (List.map clos2trm cls') (clos2trm cl') =
Delta.reduce (list_for_n_value CLS).
End SndHypIntf2.
Module Mk3(SH:SndHypIntf2).
Module Sound3 := Sound2.Mk3(SH).
Import Sound3.
Section Eval.
Variable fenv : env clos.
Definition result eval h c stack :=
match stack with
| nil => Result h c
| Frame benv' app' t :: rem => eval benv' (c::app') t rem
end.
Fixpoint eval (h:nat) (benv:list clos) (app:list clos) (t:trm)
(stack : list frame) {struct h} : eval_res :=
match h with
| 0 => Inter (Frame benv app t :: stack)
| S h =>
match trm2app t with
| Some (t1, t2) =>
eval h benv nil t2 (Frame benv app t1 :: stack)
| _ =>
let c := trm2clos benv fenv t in
match app with
| nil => result (eval h) h c stack
| c1 :: rem =>
match c with
| clos_abs t1 benv =>
eval h (c1::benv) rem t1 stack
| clos_const cst l =>
let nargs := length l + length app in
let nred := S(Const.arity cst) in
if le_lt_dec nred nargs then
let (args, app') := cut nred (List.app l app) in
match SH.reduce_clos cst args with
| (clos_const cst' app'', app3) =>
eval h nil (app'' ++ app3 ++ app') (trm_cst cst') stack
| (clos_abs t1 benv, app3) =>
eval h benv (app3 ++ app') (trm_abs t1) stack
end
else result (eval h) h (clos_const cst (l++app)) stack
end
end end
end.
End Eval.
(*
Definition trm_S :=
trm_abs (trm_abs (trm_abs
(trm_app (trm_app (trm_bvar 2) (trm_bvar 0))
(trm_app (trm_bvar 1) (trm_bvar 0))))).
Definition trm_K :=
trm_abs (trm_abs (trm_bvar 1)).
Eval compute in eval nil 100 nil nil
(trm_app (trm_abs (trm_bvar 0)) (trm_abs (trm_bvar 0))) nil.
Definition skk := eval nil 100 nil nil
(trm_app (trm_app (trm_app trm_S trm_K) (trm_K)) (trm_abs (trm_bvar 13))) nil.
Eval compute in skk.
Eval compute in
match skk with Result c => clos2trm c | _ => trm_def end.
Definition skk' := eval nil 3 nil nil
(trm_app (trm_app (trm_app trm_S trm_K) (trm_K))
(trm_abs (trm_abs (trm_abs (trm_bvar 2))))) nil.
Eval compute in skk'.
Eval compute in res2trm skk'.
*)
Lemma term_trm_inst : forall n t tl,
closed_n n t -> trm_inst_rec n tl t = t.
Proof.
induction 1; simpl*; try congruence.
destruct* (le_lt_dec n m).
elimtype False; omega.
Qed.
Hint Constructors closed_n : core.
Lemma closed_n_le : forall m n t, closed_n m t -> m <= n -> closed_n n t.
Proof.
intros until 1; revert n.
induction H; intuition; omega.
Qed.
Lemma closed_0_1 : forall t x, closed_n 0 (t ^ x) -> closed_n 1 t.
Proof.
intros t x.
unfold trm_open.
generalize 0.
induction t; simpl; intros; auto.
destruct* (le_lt_dec (S n0) n).
destruct (n0 === n). elimtype False; omega.
inversions H. elimtype False; omega.
simpl in H; inversions* H.
simpl in H; inversions* H.
simpl in H; inversions* H.
Qed.
Lemma term_closed_0 : forall t, term t -> closed_n 0 t.
Proof.
induction 1; simpl*;
constructor; auto;
destruct (var_fresh L);
apply* closed_0_1.
Qed.
Definition is_abs t :=
match t with trm_abs _ => true | _ => false end.
Lemma app_trm_cases : forall t1,
(forall t2, app_trm t1 t2 = trm_app t1 t2) \/ (exists t, t1 = trm_abs t).
Proof.
intros.
destruct t1; simpl*.
Qed.
Lemma app_trm_false : forall t1 t2,
is_abs t1 = false -> app_trm t1 t2 = trm_app t1 t2.
Proof.
intros.
destruct* (app_trm_cases t1).
destruct H0; subst; discriminate.
Qed.
Definition retypable E t1 t2 :=
forall K T, K; E |Gc|= t1 ~: T -> K; E |Gc|= t2 ~: T.
Lemma typing_app_abs_let : forall E t1 t2,
retypable E (trm_app (trm_abs t2) t1) (trm_let t1 t2).
Proof.
intros; intro; intros.
inversions H; try discriminate; simpl in *.
inversions H4; try discriminate; simpl in *.
apply (@typing_let Gc (Sch S nil) {} L).
simpl; intros.
destruct Xs; try elim H0.
unfold kinds_open_vars, kinds_open, sch_open_vars; simpl.
rewrite* typ_open_vars_nil.
apply H8.
Qed.
Lemma trm_open_comm : forall i j u v t,
i <> j -> term u -> term v ->
trm_open_rec i u (trm_open_rec j v t) = trm_open_rec j v (trm_open_rec i u t).
Proof.
intros.
revert i j H.
induction t; intros; simpl*.
destruct (j === n).
destruct (i === n); simpl*.
elimtype False; omega.
destruct* (j === n).
rewrite* <- Infra.trm_open_rec.
simpl.
destruct (i === n).
rewrite* <- Infra.trm_open_rec.
simpl.
destruct* (j === n).
rewrite* IHt.
rewrite* IHt1.
rewrite* IHt2.
rewrite* IHt1.
rewrite* IHt2.
Qed.
Lemma retypable_trm_app : forall E t1 t2,
retypable E (trm_app t1 t2) (app_trm t1 t2).
Proof.
intros; intro; intros.
unfold app_trm; destruct* t1.
apply* typing_app_abs_let.
Qed.
Hint Resolve term_closed_0 clos_ok_term : core.
Lemma term_closed_n : forall n t,
term t -> closed_n n t.
Proof.
intros.
apply* (@closed_n_le 0); omega.
Qed.
Hint Resolve term_closed_n : core.
Lemma cln_app_trm : forall n t1 t2,
closed_n n t1 -> closed_n n t2 -> closed_n n (app_trm t1 t2).
Proof.
intros.
destruct (app_trm_cases t1).
rewrite* H1.
destruct H1.
subst; simpl.
inversions* H.
Qed.
Lemma closed_n_app2trm : forall n t args,
closed_n n t ->
list_forall clos_ok args ->
closed_n n (app2trm t args).
Proof.
unfold app2trm.
intros.
induction args using rev_ind. simpl*.
rewrite map_app; rewrite fold_left_app. simpl.
assert (clos_ok x) by apply* (list_forall_out H0).
assert (list_forall clos_ok args) by
(apply list_forall_in; intros; apply* (list_forall_out H0)).
apply* cln_app_trm.
Qed.
Lemma term_app_trm : forall t1 t2,
term t1 -> term t2 -> term (app_trm t1 t2).
Proof.
intros.
destruct (app_trm_cases t1).
rewrite* H1.
destruct H1.
subst; simpl.
inversions* H.
Qed.
Lemma term_app2trm : forall t cl,
term t -> list_forall clos_ok cl -> term (app2trm t cl).
Proof.
unfold app2trm.
intros.
induction cl using rev_ind.
simpl*.
rewrite map_app; rewrite fold_left_app; simpl.
puts (list_forall_out H0).
apply* term_app_trm.
Qed.
Hint Resolve term_app_trm term_app2trm : core.
Lemma retypable_app_trm : forall E t1 t2 t3 t4,
is_abs t1 = false ->
retypable E t1 t2 -> retypable E t3 t4 ->
retypable E (app_trm t1 t3) (app_trm t2 t4).
Proof.
intros.
rewrite (app_trm_false _ _ H).
intro; intros.
apply* retypable_trm_app.
inversions* H2; discriminate.
Qed.
Lemma retypable_app_trm2 : forall E t1 t2 t3,
retypable E t2 t3 -> retypable E (app_trm t1 t2) (app_trm t1 t3).
Proof.
intros; intro; intros.
destruct (app_trm_cases t1).
rewrite H1 in *. inversions* H0; try discriminate.
destruct H1; subst; simpl in *.
inversions* H0; try discriminate.
Qed.
Lemma is_abs_app_trm : forall t1 t2,
is_abs (app_trm t1 t2) = false.
Proof.
intros.
destruct t1; simpl*.
Qed.
Hint Resolve is_abs_app_trm : core.
Lemma app2trm_app : forall t l1 l2,
app2trm t (l1 ++ l2) = app2trm (app2trm t l1) l2.
Proof.
intros; unfold app2trm.
rewrite map_app. rewrite* fold_left_app.
Qed.
Lemma is_abs_fold_left_app_trm : forall t args,
is_abs t = false -> is_abs (fold_left app_trm args t) = false.
Proof.
intros; induction args using rev_ind. auto.
rewrite fold_left_app. simpl. apply is_abs_app_trm.
Qed.
Lemma is_abs_app2trm : forall t args,
is_abs t = false -> is_abs (app2trm t args) = false.
Proof.
intros; unfold app2trm. apply* is_abs_fold_left_app_trm.
Qed.
Hint Resolve is_abs_app2trm : core.
Lemma retypable_app2trm : forall E t1 t2 args,
is_abs t1 = false ->
retypable E t1 t2 ->
list_forall clos_ok args ->
retypable E (app2trm t1 args) (app2trm t2 args).
Proof.
intros; induction args using rev_ind. auto.
repeat rewrite app2trm_app.
assert (list_forall clos_ok args).
apply list_forall_in; intros; apply* (list_forall_out H1).
unfold app2trm at 1 3. simpl.
apply retypable_app_trm; auto.
intro; auto.
Qed.
Lemma term_inst_closed : forall t cl,
closed_n (length cl) t -> list_forall clos_ok cl ->
term (inst t cl).
Proof.
intros.
apply term_trm_inst_closed.
rewrite* map_length.
apply* list_forall_map.
Qed.
Hint Resolve term_inst_closed : core.
Lemma is_bvar_term : forall t, term t -> is_bvar t = false.
Proof. induction 1; simpl*. Qed.
Lemma retypable_stack2trm : forall E t1 t2 fl,
term t1 ->
retypable E t1 t2 ->
list_forall frame_ok fl ->
retypable E (stack2trm t1 fl) (stack2trm t2 fl).
Proof.
intros.
gen t1 t2; induction H1; intros; simpl. auto.
destruct x as [benv app t'].
case_eq (is_bvar t1); intros.
inversions H0; discriminate.
inversions H; clear H.
apply IHlist_forall; clear IHlist_forall; auto.
apply retypable_app2trm; auto.
intro; intros.
rewrite is_bvar_term.
apply* retypable_app_trm2.
destruct (app_trm_cases (inst t' benv)).
rewrite H4 in H.
inversions H; try discriminate.
use (H2 _ _ H14).
destruct H4. rewrite H4 in H; simpl in H.
inversions H; try discriminate.
destruct (var_freshes L1 (sch_arity M)).
use (H2 _ _ (H12 _ f)).
Qed.
Lemma term_fold_app : forall tl t,
list_forall term tl -> term t -> term (fold_left trm_app tl t).
Proof.
intros; gen t.
induction H; intros; simpl*.
Qed.
Lemma typing_app_trm_inv : forall K E t1 t2 T,
is_abs t1 = false ->
K; E |Gc|= app_trm t1 t2 ~: T ->
exists T1, K; E |Gc|= t1 ~: T1.
Proof.
intros.
rewrite (app_trm_false _ _ H) in H0.
inversions* H0; try discriminate.
Qed.
Lemma typing_app2trm_inv : forall K E t1 cl T,
K; E |Gc|= app2trm t1 cl ~: T ->
is_abs t1 = false ->
exists T1, K; E |Gc|= t1 ~: T1.
Proof.
unfold app2trm.
intros.
gen T; induction (List.map clos2trm cl) using rev_ind; simpl; intros.
auto*.
rewrite fold_left_app in H.
simpl in H.
destruct* (typing_app_trm_inv _ _ (is_abs_fold_left_app_trm _ l H0) H).
Qed.
Lemma is_bvar_app_trm : forall t1 t2, is_bvar (app_trm t1 t2) = false.
Proof.
intros; destruct (app_trm_cases t1).
rewrite* H.
destruct H; rewrite* H.
Qed.
Hint Resolve is_bvar_app_trm : core.
Lemma typing_stack2trm_inv : forall K E fl t1 T,
K; E |Gc|= stack2trm t1 fl ~: T ->
is_bvar t1 = false ->
exists T1, exists K, K; E |Gc|= t1 ~: T1.
Proof.
induction fl; simpl; intros. auto*.
destruct a as [benv args t].
rewrite H0 in H.
destruct (IHfl _ _ H) as [T1 [K' Typ]].
clear.
unfold app2trm. induction (List.map clos2trm args) using rev_ind.
simpl*.
rewrite fold_left_app; simpl*.
clear -H0 Typ.
set (t0 := inst t benv) in *.
destruct* (typing_app2trm_inv _ _ Typ).
destruct (app_trm_cases t0).
rewrite H1 in H; inversions* H; try discriminate.
destruct H1; rewrite H1 in H.
inversions* H; try discriminate.
destruct (var_freshes L1 (sch_arity M)).
auto*.
Qed.
Lemma app2trm_cases : forall t cl,
(exists t1, exists t2, app2trm t cl = trm_app t1 t2) \/
(exists t1, exists t2, app2trm t cl = trm_let t1 t2) \/
app2trm t cl = t.
Proof.
intros.
induction cl using rev_ind. simpl*.
rewrite app2trm_app.
destruct (app_trm_cases (app2trm t cl)); unfold app2trm at 1 3; simpl.
rewrite* H.
destruct H; rewrite H. simpl*.
Qed.
Lemma app2trm_cst : forall c cl,
app2trm (trm_cst c) cl = const_app c (List.map clos2trm cl).
Proof.
unfold app2trm, const_app.
induction cl using rev_ind. simpl*.
rewrite map_app. repeat rewrite fold_left_app. simpl.
rewrite app_trm_false. rewrite* IHcl.
apply* is_abs_app2trm.
Qed.
Lemma trm_inst_n : forall d benv n,
closed_n (length benv) (trm_bvar n) ->
trm_inst (trm_bvar n) benv = nth n benv d.
Proof.
unfold trm_inst; simpl; intros.
rewrite <- minus_n_O.
inversions H.
apply* nth_indep.
Qed.
Lemma inst_n : forall benv n,
closed_n (length benv) (trm_bvar n) ->
inst (trm_bvar n) benv = clos2trm (nth n benv clos_def).
Proof.
intros; unfold inst.
rewrite <- map_nth.
apply trm_inst_n.
rewrite* map_length.
Qed.
Lemma term_const_app : forall c cls,
list_forall clos_ok cls ->
term (const_app c (List.map clos2trm cls)).
Proof.
intros.
unfold const_app.
cut (term (trm_cst c)).
generalize (trm_cst c).
induction H; simpl*.
auto.
Qed.
Hint Resolve term_const_app : core.
Lemma clos2trm_const_eq : forall cl c tl,
clos2trm cl = const_app c tl ->
exists args, cl = clos_const c args /\ tl = List.map clos2trm args.
Proof.
unfold const_app; intros.
destruct cl.
induction tl using rev_ind. discriminate.
rewrite fold_left_app in H; discriminate.
simpl in H.
destruct (const_app_eq _ _ _ _ H). subst.
exists* l.
Qed.
Lemma closed_n_inst_rec : forall l t m,
closed_n m (trm_inst_rec m l t) -> closed_n (m + length l) t.
Proof.
unfold trm_inst. induction t; simpl; intros; auto.
constructor.
destruct (le_lt_dec m n); try omega.
destruct (le_lt_dec (m + length l) n); try omega.
rewrite nth_overflow in H; try omega.
inversions H. omega.
constructor.
apply (IHt (S m)).
inversions* H.
inversions H.
constructor; auto.
apply* (IHt2 (S m)).
inversions* H.
Qed.
Lemma closed_n_inst : forall l t,
closed_n 0 (inst t l) -> closed_n (length l) t.
Proof.
intros.
rewrite <- (map_length clos2trm).
refine (closed_n_inst_rec _ _ H).
Qed.
Hint Immediate closed_n_inst : core.
Hint Resolve list_forall_app : core.
Hint Rewrite map_app fold_left_app : list.
Lemma fold_app_eq_inv : forall t t' tl1 tl tl2,
fold_left trm_app tl t = fold_left trm_app (tl1 ++ tl2) t' ->
length tl = length tl2 ->
tl = tl2 /\ t = fold_left trm_app tl1 t'.
Proof.
induction tl using rev_ind; intros; rewrite fold_left_app in *.
destruct tl2; try discriminate.
auto.
destruct tl2 using rev_ind.
rewrite app_length in H0; simpl in H0.
elimtype False; omega.
clear IHtl2.
autorewrite with list in *. simpl in *.
inversions H. rewrite <- fold_left_app in H2.
destruct* (IHtl tl2).
subst*.
Qed.
Section Soundness.
Variables (E:Defs.env) (fenv:env clos).
Hypothesis E_ok : env_ok E.
Hypothesis fenv_ok : env_prop clos_ok fenv /\
forall x M, binds x M E ->
exists cl, binds x cl fenv /\
has_scheme_vars Gc {} empty empty (clos2trm cl) M.
Lemma clos_ok_get : forall v,
clos_ok match get v fenv with
| Some x => x
| None => clos_def
end.
Proof.
intros.
case_eq (get v fenv); intros.
apply (proj1 fenv_ok _ _ (binds_in H)).
auto.
Qed.
Hint Resolve clos_ok_get : core.
Lemma trm2clos_regular : forall benv t,
list_forall clos_ok benv ->
closed_n (length benv) t ->
clos_ok (trm2clos benv fenv t).
Proof.
intros; destruct t; simpl*.
inversions* H0.
Qed.
Hint Resolve trm2clos_regular : core.
Definition eval_restart h fl res :=
match res with
| Inter nil => Inter fl
| Inter (Frame benv args t :: fl') =>
eval fenv h benv args t (fl' ++ fl)
| Result h' c =>
match fl with
| nil => Result (h'+h) c
| Frame benv' app' t :: rem => eval fenv (h'+h) benv' (c::app') t rem
end
end.
Lemma eval_restart_ok : forall h' fl' h benv args t fl,
eval_restart h' fl' (eval fenv h benv args t fl) =
eval fenv (h+h') benv args t (fl++fl').
Proof.
induction h; simpl; intros. auto.
destruct (trm2app t).
destruct p. apply* IHh.
destruct args.
destruct* fl.
destruct f.
simpl*.
destruct* (trm2clos benv fenv t).
destruct (length l + length (c::args)).
destruct* fl. destruct f. simpl*.
destruct (le_lt_dec (Const.arity c0) n); simpl.
destruct (l ++ c :: args).
destruct (SH.reduce_clos c0 nil).
destruct* c1.
destruct (cut (Const.arity c0) l1).
destruct (SH.reduce_clos c0 (c1 :: l2)).
destruct* c2.
destruct* fl.
destruct f. simpl*.
Qed.
Lemma eval_restart_ok' : forall h' fl' h benv args t,
eval_restart h' fl' (eval fenv h benv args t nil) =
eval fenv (h+h') benv args t fl'.
Proof.
intros.
apply* eval_restart_ok.
Qed.
Lemma eval_restart_ok'' : forall h' h benv args t fl,
eval_restart h' nil (eval fenv h benv args t fl) =
eval fenv (h+h') benv args t fl.
Proof.
intros.
rewrite eval_restart_ok. rewrite* <- app_nil_end.
Qed.
Lemma eval_restart_step : forall h c,
eval_restart 1 nil (Result h c) = Result (S h) c.
Proof.
intros; simpl. rewrite plus_comm; reflexivity.
Qed.
Inductive eval_cont : clos -> list frame -> eval_res -> Prop :=
| Eval_nil : forall cl,
eval_cont cl nil (Result 0 cl)
| Eval_Frame : forall cl benv args t fl,
eval_cont cl (Frame benv args t :: fl)
(Inter (Frame benv (cl::args) t :: fl)).
Hint Constructors eval_cont : core.
Lemma eval_cont_ok : forall cl fl,
eval_cont cl fl (eval_restart 0 fl (Result 0 cl)).
Proof.
intros; simpl.
destruct fl. auto.