-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathML_SP_Inference.v
3163 lines (3004 loc) · 101 KB
/
ML_SP_Inference.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, August 2008 *
***************************************************************************)
Set Implicit Arguments.
Require Import List Arith Metatheory Cardinal.
Require Import ML_SP_Definitions ML_SP_Unify.
Module MkInfer(Cstr:CstrIntf)(Const:CstIntf).
Module Unify := MkUnify(Cstr)(Const).
Import Unify.
Import MyEval.
Import Rename.
Import Sound.
Import Infra.
Import Defs.
Import Metatheory_Env.Env.
Module Mk2(Delta:DeltaIntf).
Module MyEval2 := MyEval.Mk2(Delta).
Import MyEval2.Rename2.
Import Sound2.
Import Unify.
Import JudgInfra.
Import Judge.
Definition unify K T1 T2 S :=
unify (1 + size_pairs S K ((T1,T2)::nil)) ((T1,T2)::nil) K S.
Definition fvs S K E :=
dom S \u fv_in typ_fv S \u dom K \u fv_in kind_fv K \u env_fv E.
(** Variants looking up a kinding environment *)
Fixpoint close_fvars (n:nat)(K:kenv)(VK:vars)(Vs:vars) {struct n} : vars :=
match n with
| 0 => Vs
| S n' =>
match S.choose (S.inter VK Vs) with
| None => Vs
| Some x =>
let VK' := S.remove x VK in
let Vs' :=
match get x K with
| None => Vs
| Some k => Vs \u kind_fv k
end
in close_fvars n' K VK' Vs'
end
end.
Definition close_fvk K := close_fvars (length K) K (dom K).
Fixpoint split_env (A:Set) (B:vars) (E:env A) {struct E} : env A * env A :=
match E with
| nil => (nil, nil)
| xk::E' =>
let (Eb, EB) := split_env B E' in
if S.mem (fst xk) B then (Eb, xk::EB) else (xk::Eb, EB)
end.
Definition vars_subst S L :=
typ_fv_list (List.map (fun x => typ_subst S (typ_fvar x)) (S.elements L)).
Definition typinf_generalize K' E' L T1 :=
let ftve := close_fvk K' (env_fv E') in
let (K'', KA) := split_env ftve K' in
let B := close_fvk K' (typ_fv T1) in
let (_, KB) := split_env B K'' in
let (Bs, Ks) := split KB in
let Bs' := S.elements (S.diff B (ftve \u dom KB)) in
let Ks' := List.map (fun x:var => @None ckind) Bs' in
let (_, KC) := split_env L K'' in
(KA & KC, sch_generalize (Bs++Bs') T1 (Ks++Ks')).
Fixpoint kdom (E : kenv) : vars :=
match E with
| nil => {}
| (x, Some _) :: E' => {{x}} \u kdom E'
| _ :: E' => kdom E'
end.
Fixpoint trm_depth (t : trm) : nat :=
match t with
| trm_bvar _ => 0
| trm_fvar _ => 0
| trm_abs t1 => S (trm_depth t1)
| trm_let t1 t2 => S (Max.max (trm_depth t1) (trm_depth t2))
| trm_app t1 t2 => S (Max.max (trm_depth t1) (trm_depth t2))
| trm_cst _ => 0
end.
Lemma trm_depth_open : forall x t,
trm_depth (t ^ x) = trm_depth t.
Proof.
intros; unfold trm_open.
generalize 0; induction t; intros; simpl*.
destruct (n0 === n); reflexivity.
Qed.
Lemma lt_wf : forall n, Acc lt n.
Proof.
induction n.
apply Acc_intro; intros. elim (le_Sn_O _ H).
apply Acc_intro; intros.
unfold lt in H.
destruct (Lt.le_lt_or_eq _ _ (Le.le_S_n _ _ H)).
apply (Acc_inv IHn _ H0).
subst*.
Defined.
Lemma dom_inv_abs : forall t t1 x,
Acc lt (trm_depth t) ->
t = trm_abs t1 -> Acc lt (trm_depth (t1 ^ x)).
Proof.
introv P eq.
rewrite eq in P.
rewrite trm_depth_open.
simpl in P.
pose (P1 := le_n (S (trm_depth t1))).
exact (Acc_inv P _ P1).
Defined.
Lemma lt_max_l : forall n1 n2, n1 < (S (Max.max n1 n2)).
Proof.
intros; puts (Max.le_max_l n1 n2); auto with arith.
Qed.
Lemma lt_max_r : forall n1 n2, n2 < (S (Max.max n1 n2)).
Proof.
intros; puts (Max.le_max_r n1 n2); auto with arith.
Qed.
Ltac dom_inv_tac :=
intros t t1 t2 P eq;
rewrite eq in P;
simpl in P;
try rewrite trm_depth_open;
solve [exact (Acc_inv P _ (lt_max_l (trm_depth t1) (trm_depth t2)))
|exact (Acc_inv P _ (lt_max_r (trm_depth t1) (trm_depth t2)))].
Lemma dom_inv_app1 : forall t t1 t2,
Acc lt (trm_depth t) ->
t = trm_app t1 t2 -> Acc lt (trm_depth t1).
Proof. dom_inv_tac. Defined.
Lemma dom_inv_app2 : forall t t1 t2,
Acc lt (trm_depth t) ->
t = trm_app t1 t2 -> Acc lt (trm_depth t2).
Proof. dom_inv_tac. Defined.
Lemma dom_inv_let1 : forall t t1 t2,
Acc lt (trm_depth t) ->
t = trm_let t1 t2 -> Acc lt (trm_depth t1).
Proof. dom_inv_tac. Defined.
Lemma dom_inv_let2 : forall x t t1 t2,
Acc lt (trm_depth t) ->
t = trm_let t1 t2 -> Acc lt (trm_depth (t2 ^ x)).
Proof. intro; dom_inv_tac. Defined.
Fixpoint typinf (K:kenv) (E:Defs.env) (t:trm) (T:typ) (L:vars) (S:subs)
(h:Acc lt (trm_depth t)) {struct h} : option (kenv * subs) * vars :=
match t as t' return t = t' -> option (kenv * subs) * vars with
| trm_bvar _ => fun eq => (None, L)
| trm_fvar x => fun eq =>
match get x E with
| None => (None, L)
| Some M =>
let Vs := proj1_sig (var_freshes L (sch_arity M)) in
(unify (K & kinds_open_vars (sch_kinds M) Vs) (M ^ Vs) T S,
L \u mkset Vs)
end
| trm_abs t1 => fun eq =>
let x := proj1_sig (var_fresh (dom E \u trm_fv t1)) in
let v1 := proj1_sig (var_fresh L) in
let v2 := proj1_sig (var_fresh (L \u {{v1}})) in
match unify K (typ_arrow (typ_fvar v1) (typ_fvar v2)) T S with
| None => (None, L)
| Some (K',S') =>
typinf K' (E & x ~ Sch (typ_fvar v1) nil) (t1 ^ x) (typ_fvar v2)
(L \u {{v1}} \u {{v2}}) S' (dom_inv_abs x h eq)
end
| trm_let t1 t2 => fun eq =>
let v := proj1_sig (var_fresh L) in
match typinf K E t1 (typ_fvar v) (L \u {{v}}) S (dom_inv_let1 h eq) with
| (Some (K0,S'), L') =>
let K' := Env.map (kind_subst S') K0 in
let E' := Env.map (sch_subst S') E in
let T1 := typ_subst S' (typ_fvar v) in
let (KA, M) := typinf_generalize K' E' (vars_subst S' (kdom K)) T1 in
let x := proj1_sig (var_fresh (dom E \u trm_fv t1 \u trm_fv t2)) in
typinf KA (E & x ~ M) (t2 ^ x) T L' S' (dom_inv_let2 x h eq)
| none => none
end
| trm_app t1 t2 => fun eq =>
let v := proj1_sig (var_fresh L) in
match typinf K E t1 (typ_arrow (typ_fvar v) T) (L \u {{v}}) S
(dom_inv_app1 h eq) with
| (Some (K',S'), L') =>
typinf K' E t2 (typ_fvar v) L' S' (dom_inv_app2 h eq)
| none => none
end
| trm_cst c => fun eq =>
let M := Delta.type c in
let Vs := proj1_sig (var_freshes L (sch_arity M)) in
(unify (K & kinds_open_vars (sch_kinds M) Vs) (M ^ Vs) T S,
L \u mkset Vs)
end (refl_equal t).
Definition typinf0 K E t T L S := typinf K E t T L S (lt_wf _).
Lemma normalize_typinf : forall K E t T L S h,
typinf K E t T L S h = typinf0 K E t T L S.
Proof.
intros.
unfold typinf0; apply f_equal. apply ProofIrrelevance.proof_irrelevance.
Qed.
Definition typinf' E trm :=
let v := Variables.var_default in
let min_vars := S.singleton v in
let V := typ_fvar v in
match
typinf empty E trm V min_vars empty (lt_wf _)
with (None, _) => None
| (Some (k, s), _) =>
Some (map (kind_subst s) k, typ_subst s V)
end.
Lemma env_prop_type_compose : forall S1 S2,
env_prop type S1 -> env_prop type S2 -> env_prop type (compose S1 S2).
Proof.
unfold compose.
intros.
intro; intros.
destruct* (in_app_or _ _ _ H1).
destruct (in_map_inv _ _ _ _ H2) as [T [Eq B']].
subst*.
Qed.
Hint Resolve env_prop_type_compose : core.
Lemma unify_rel_all_kind_types :
forall (P:typ->Prop) k k0 kc (v1:Cstr.valid kc),
All_kind_types P (Some k) -> All_kind_types P (Some k0) ->
let krs := kind_rel k ++ kind_rel k0 in
All_kind_types P (Some (Kind v1 (unify_coherent krs))) /\
(forall T1 T2,
In (T1, T2) (snd (unify_kind_rel krs nil (Cstr.unique kc) nil)) ->
P T1 /\ P T2).
Proof.
unfold All_kind_types; intros.
simpl in *.
puts (list_forall_app H H0).
clear H H0.
unfold list_snd in H1; rewrite <- map_app in H1.
set (kr':=@nil (Cstr.attr*typ)).
set (pairs':=@nil (typ*typ)).
assert (list_forall P (List.map (@snd _ _) kr')) by simpl*.
assert (forall T1 T2, In (T1, T2) pairs' -> P T1 /\ P T2) by simpl*.
gen kr' pairs'.
induction (kind_rel k ++ kind_rel k0); simpl; intros. auto.
destruct a.
inversion_clear H1.
case_eq (Cstr.unique kc a); introv R.
case_eq (assoc Cstr.eq_dec a kr'); intros.
apply* IHl.
simpl; intros.
destruct* H4.
inversions H4.
split2*.
clear -H H1.
apply* (list_forall_out H).
puts (assoc_sound _ _ _ H1).
apply (in_map (@snd _ _) _ _ H0).
apply* IHl.
simpl*.
apply* IHl.
simpl*.
Qed.
Lemma incl_remove_env : forall (A:Set) v (K:env A),
incl (remove_env K v) K.
Proof.
induction K; simpl; intro; intros. auto.
destruct a.
destruct* (v == v0).
Qed.
Lemma kenv_ok_remove_env : forall K v,
kenv_ok K -> kenv_ok (remove_env K v).
Proof.
intros; kenv_ok_solve; auto.
intro; intros.
apply (H0 x).
apply* (incl_remove_env v K).
Qed.
Hint Resolve kenv_ok_remove_env : core.
Lemma unify_type : forall K' S' h pairs K S,
Unify.unify h pairs K S = Some (K', S') ->
is_subst S ->
env_prop type S ->
kenv_ok K ->
(forall T1 T2, In (T1, T2) pairs -> type T1 /\ type T2) ->
kenv_ok K' /\ env_prop type S' /\ is_subst S'.
Proof.
induction h; simpl; intros. discriminate.
set (h0 := pairs_size S pairs + 1) in *.
clearbody h0; gen pairs; induction h0; simpl; intros. discriminate.
destruct pairs. inversions* H.
destruct p.
assert (type t /\ type t0). apply* H3.
destruct H4.
puts (typ_subst_type H1 H4).
puts (typ_subst_type H1 H5).
case_rewrite R1 (typ_subst S t); try solve [inversion H6];
case_rewrite R2 (typ_subst S t0); try solve [inversion H7];
try (unfold unify_nv in H;
case_rewrite R3 (S.mem v (typ_fv (typ_arrow t1 t2)));
case_rewrite R4 (get_kind v K); apply* IHh).
destruct (v == v0). apply* (IHh0 pairs).
simpl in H.
unfold unify_vars in H.
assert (Hok: forall k, ok (remove_env (remove_env K v) v0 & v0 ~ k)).
intro; constructor.
repeat apply* ok_remove_env.
rewrite* dom_remove_env.
assert (Horig: forall x a,
In (x, a) (remove_env (remove_env K v) v0) -> All_kind_types type a).
intros; apply (proj2 H2 x a).
puts (incl_remove_env v0 _ _ H8).
apply* (incl_remove_env _ _ _ H9).
case_rewrite R3 (get_kind v K); case_rewrite R4 (get_kind v0 K);
try poses Aktc (proj2 H2 _ _ (binds_in (get_kind_binds _ _ R3)));
try poses Aktc0 (proj2 H2 _ _ (binds_in (get_kind_binds _ _ R4)));
simpl unify_kinds in H.
destruct c as [kc kv kr kh].
destruct c0 as [kc0 kv0 kr0 kh0].
destruct (Cstr.valid_dec (Cstr.lub kc kc0)); try discriminate.
replace kr with (kind_rel (Kind kv kh)) in H by simpl*.
replace kr0 with (kind_rel (Kind kv0 kh0)) in H by simpl*.
destruct* (unify_rel_all_kind_types v1 Aktc Aktc0).
apply* IHh; clear IHh H. split2*.
intro; intros.
unfold concat in H; destruct* (in_app_or _ _ _ H).
destruct c as [kc kv kr kh].
simpl app in H.
apply* IHh. split2*.
cbv iota beta in H. simpl app in H.
apply* IHh. split2*.
cbv iota beta in H. simpl app in H.
apply* IHh. split2*.
apply* IHh0; clear IHh IHh0 H.
simpl; intros.
inversions H6.
inversions H7.
destruct H. inversions* H.
destruct H. inversions* H.
apply* H3.
Qed.
Lemma split_env_ok : forall (A:Set) (B:vars) (E Eb EB:env A),
split_env B E = (Eb, EB) -> ok E ->
ok (EB & Eb) /\ disjoint B (dom Eb) /\ dom EB << B /\
incl E (EB & Eb) /\ incl (EB & Eb) E.
Proof.
induction E; simpl; intros.
inversions H. simpl. split2*.
destruct a.
case_rewrite R1 (split_env B E).
simpl in *.
case_rewrite R2 (S.mem v B).
inversions H; clear H.
inversions H0; clear H0.
destruct* (IHE Eb e0) as [Hok [Dis [Dom [I1 I2]]]]; clear IHE.
destruct (ok_concat_inv _ _ Hok).
case_eq (get v (e0 & Eb)); intros.
elim (binds_fresh (in_ok_binds _ _ (I2 _ (binds_in H1)) H2) H4).
split.
apply* disjoint_ok.
apply* (@ok_push _ e0 v a).
simpl*.
split2*.
split.
simpl. use (S.mem_2 R2).
split; intro; simpl; intros.
destruct H3. apply* in_or_concat.
puts (I1 _ H3). destruct* (in_app_or _ _ _ H5).
destruct* (in_app_or _ _ _ H3).
destruct* H5.
inversions H; clear H.
inversions H0; clear H0.
destruct* (IHE e EB) as [Hok [Dis [Dom [I1 I2]]]]; clear IHE.
destruct (ok_concat_inv _ _ Hok).
case_eq (get v (EB & e)); intros.
elim (binds_fresh (in_ok_binds _ _ (I2 _ (binds_in H1)) H2) H4).
split.
apply* disjoint_ok.
apply* (@ok_push _ e v a).
simpl*.
Qed.
Lemma proper_instance_well_subst : forall S K K' Ks Us,
env_prop type S ->
well_subst K K' S ->
kenv_ok K' ->
proper_instance K Ks Us ->
proper_instance K' (List.map (kind_subst S) Ks) (List.map (typ_subst S) Us).
Proof.
intros.
destruct H2 as [HUs HW].
split.
destruct HUs.
split2*. clear -H H3. induction H3; simpl*.
remember Us as Ts.
pattern Ts at 2.
pattern Ts at 2 in HW.
rewrite HeqTs in *.
clear HeqTs.
destruct HUs.
gen Ks; induction H3; destruct Ks; simpl; intros; try discriminate. auto.
inversions HW; clear HW.
constructor; auto.
rewrite* <- kind_subst_open.
Qed.
Lemma kenv_ok_map : forall K S,
kenv_ok K -> env_prop type S -> kenv_ok (map (kind_subst S) K).
Proof.
intros.
split2*.
destruct H.
intro; intros.
destruct (in_map_inv _ _ _ _ H2) as [b [Hb B]].
subst.
apply* All_kind_types_subst.
Qed.
Lemma kenv_ok_subst : forall K' K Ks Ys S,
env_prop type S ->
kenv_ok (K & kinds_open_vars Ks Ys) ->
kenv_ok K' ->
fresh (dom K') (length Ks) Ys ->
kenv_ok (K' & map (kind_subst S) (kinds_open_vars Ks Ys)).
Proof.
introv TS HK HK' Fr.
apply* kenv_ok_concat.
apply* kenv_ok_map.
Qed.
Lemma env_ok_map : forall E S,
env_ok E -> env_prop type S -> env_ok (map (sch_subst S) E).
Proof.
intros; split2*.
intro; intros.
destruct (in_map_inv _ _ _ _ H1) as [b [Hb B]].
subst.
apply* sch_subst_type.
apply* (proj2 H x).
Qed.
Hint Resolve kenv_ok_subst env_ok_map : core.
Lemma well_subst_extend : forall K S K' Ks Ys,
env_prop type S ->
well_subst K K' S ->
fresh (dom S \u dom K') (length Ks) Ys ->
well_subst (K & kinds_open_vars Ks Ys)
(K' & map (kind_subst S) (kinds_open_vars Ks Ys)) S.
Proof.
introv TS WS Fr.
intro; intros.
binds_cases H.
puts (WS _ _ B).
inversions H. auto.
simpl. rewrite <- H1.
apply* wk_kind.
rewrite typ_subst_fresh by simpl*.
destruct* k as [[kc kv kr kh]|].
simpl.
apply~ wk_kind.
use (binds_map (kind_subst S) B0).
Qed.
Lemma typing_typ_well_subst : forall gc S K K' E t T,
env_prop type S ->
well_subst K K' S ->
kenv_ok K' ->
K ; E |gc|= t ~: T ->
K'; map (sch_subst S) E |gc|= t ~: (typ_subst S T).
Proof.
introv TS WS HK' Typ.
gen K'; induction Typ; intros.
(* Var *)
rewrite~ sch_subst_open. apply* typing_var.
destruct M as [T Ks]; simpl in *.
apply* proper_instance_well_subst.
(* Abs *)
simpl.
apply_fresh* (@typing_abs gc) as y.
replace (Sch (typ_subst S U) nil) with (sch_subst S (Sch U nil)) by auto.
assert (y \notin L) by auto.
use (H1 _ H2 _ WS HK').
(* Let *)
apply_fresh* (@typing_let gc (sch_subst S M)
(L1 \u dom S \u fv_in typ_fv S \u sch_fv M \u dom K \u dom K')) as y.
clear H1 H2. clear L2 T2 t2.
simpl. intros Ys Fr.
destruct M as [T Ks]; simpl in *.
rewrite map_length in Fr.
assert (HK: kenv_ok (K & kinds_open_vars Ks Ys)).
assert (fresh L1 (length Ks) Ys) by auto*.
use (H _ H1).
rewrite* <- sch_subst_open_vars.
rewrite* <- kinds_subst_open_vars.
apply* H0; clear H H0.
apply* well_subst_extend.
replace (y ~ sch_subst S M) with (map (sch_subst S) (y ~ M)) by simpl*.
rewrite <- map_concat.
apply* H2.
(* App *)
simpl in IHTyp1; auto*.
(* Cst *)
rewrite* sch_subst_open.
assert (disjoint (dom S) (sch_fv (Delta.type c))).
intro x. rewrite* Delta.closed.
rewrite* sch_subst_fresh.
apply* typing_cst.
rewrite* <- (sch_subst_fresh _ H2).
destruct (Delta.type c) as [T Ks]; simpl in *.
apply* proper_instance_well_subst.
(* GC *)
apply* (@typing_gc gc (List.map (kind_subst S) Ks)
(L \u dom S \u dom K \u dom K')).
rewrite map_length; intros.
rewrite* <- kinds_subst_open_vars.
apply* (H1 Xs); clear H1.
apply* well_subst_extend.
forward~ (H0 Xs); intro Typ.
apply* (@kenv_ok_subst K' K).
Qed.
Lemma map_compose : forall (A:Set) (f f1 f2:A->A) E,
(forall a, f1 (f2 a) = f a) ->
map f1 (map f2 E) = map f E.
Proof.
intros.
induction* E.
simpl. destruct a. simpl. rewrite H. rewrite* IHE.
Qed.
Lemma map_sch_subst_extend : forall S S0 E,
extends S S0 ->
map (sch_subst S) (map (sch_subst S0) E) = map (sch_subst S) E.
Proof.
intros.
apply map_compose.
intros.
destruct a as [T Ks]; unfold sch_subst; simpl.
rewrite* (H T).
apply (f_equal (Sch (typ_subst S T))).
induction Ks; simpl*.
rewrite IHKs.
rewrite* (@kind_subst_combine S).
Qed.
Lemma kenv_ok_sch_kinds : forall K M Xs,
kenv_ok K ->
scheme M ->
fresh (dom K) (sch_arity M) Xs ->
kenv_ok (K & kinds_open_vars (sch_kinds M) Xs).
Proof.
split.
apply* disjoint_ok.
apply* ok_combine_fresh.
apply env_prop_concat. apply (proj2 H).
apply list_forall_env_prop.
destruct* (H0 Xs).
clear -H3; induction H3. simpl*.
simpl; constructor; auto.
unfold kind_open. unfold typ_open_vars in H3.
apply* All_kind_types_map.
Qed.
Lemma kind_subst_extend : forall S' S k,
extends S' S -> kind_subst S' (kind_subst S k) = kind_subst S' k.
Proof.
intros. apply* kind_subst_combine.
Qed.
Lemma well_subst_compose : forall S S' K1 K2 K3,
extends S' S ->
well_subst K1 K2 S -> well_subst K2 K3 S' -> well_subst K1 K3 S'.
Proof.
intros.
intro; intros.
puts (H0 _ _ H2).
inversions H3; clear H3.
destruct~ k; discriminate.
puts (H1 _ _ H7).
inversions* H3.
destruct k0; try discriminate.
fold (typ_subst S' (typ_fvar x)) in H9.
fold (typ_subst S (typ_fvar Z)) in H5.
rewrite H5 in H9.
rewrite H in H9.
rewrite <- H9.
rewrite* <- (@kind_subst_extend S' S).
rewrite <- H4.
simpl. apply* wk_kind.
refine (entails_trans H12 _).
apply* kind_subst_entails.
Qed.
Lemma well_subst_extends : forall K K0 S S0,
extends S S0 ->
well_subst K0 K S ->
well_subst (map (kind_subst S0) K0) K S.
Proof.
intros; intro; intros.
destruct (binds_map_inv _ _ H1) as [k1 [Hk1 Bk1]].
subst k.
rewrite* (kind_subst_combine S).
Qed.
Hint Resolve well_subst_extends : core.
Lemma kind_entails_fv : forall k1 k2,
kind_entails k1 k2 -> kind_fv k2 << kind_fv k1.
Proof.
unfold kind_entails; intros.
destruct k2.
destruct* k1.
unfold kind_fv; simpl.
destruct H.
clear H; induction (kind_rel c); simpl; intros y Hy. auto.
destruct (S.union_1 Hy).
apply* (in_typ_fv (snd a)).
apply* in_map.
apply* IHl.
intros y Hy. elim (in_empty Hy).
Qed.
Lemma typ_fv_subst0 : forall S T,
typ_fv (typ_subst S T) << S.diff (typ_fv T) (dom S) \u fv_in typ_fv S.
Proof.
induction T; simpl; intros x Hx. elim (in_empty Hx).
case_rewrite R1 (get v S).
use (fv_in_spec typ_fv _ _ _ (binds_in R1)).
puts (get_none_notin _ R1).
simpl in Hx. auto.
auto.
Qed.
Lemma typ_fv_subst : forall S T,
typ_fv (typ_subst S T) << typ_fv T \u fv_in typ_fv S.
Proof.
intros; intros y Hy.
use (typ_fv_subst0 _ _ Hy).
Qed.
Lemma kind_fv_subst : forall S k,
kind_fv (kind_subst S k) << S.diff (kind_fv k) (dom S) \u fv_in typ_fv S.
Proof.
intros.
destruct k as [[kc kv kr kh]|].
unfold kind_fv; simpl.
clear kh; induction kr; simpl; intros x Hx; auto.
destruct* (S.union_1 Hx).
use (typ_fv_subst0 _ _ H).
unfold kind_fv; simpl*.
Qed.
Definition Gc := (false, GcLet).
Definition soundness_spec h t K0 E T L0 S0 K S L :=
trm_depth t < h ->
typinf K0 E t T L0 S0 (lt_wf _) = (Some (K, S), L) ->
is_subst S0 -> env_prop type S0 ->
kenv_ok K0 -> disjoint (dom S0) (dom K0) ->
fvs S0 K0 E \u typ_fv T << L0 ->
env_ok E -> type T ->
extends S S0 /\ env_prop type S /\ is_subst S /\
kenv_ok K /\ disjoint (dom S) (dom K) /\
well_subst K0 (map (kind_subst S) K) S /\
(fvs S K E \u L0 << L /\
map (kind_subst S) K; map (sch_subst S) E |Gc|= t ~: typ_subst S T).
Lemma soundness_ind : forall h t K0 E T L0 S0 K S L s x,
scheme s ->
fresh L0 (sch_arity s) x ->
unify (K0 & kinds_open_vars (sch_kinds s) x) (sch_open_vars s x) T S0 =
Some (K, S) ->
(kenv_ok (K0 & kinds_open_vars (sch_kinds s) x) ->
extends S S0 -> kenv_ok K ->
unifies S ((sch_open_vars s x, T) :: nil) ->
(fvs S K E \u L0 << L /\
map (kind_subst S0) (K0 & kinds_open_vars (sch_kinds s) x);
map (sch_subst S0) E |Gc|= t ~: sch_subst S0 s ^^ typ_fvars x)) ->
soundness_spec h t K0 E T L0 S0 K S L.
Proof.
intros until x; intros Hs f HI Typ Ht _ HS0 HTS0 HK0 Dis HL0 HE HT.
unfold unify in HI.
poses Fr (fresh_sub _ _ f HL0).
assert (kenv_ok (K0 & kinds_open_vars (sch_kinds s) x)).
apply* kenv_ok_sch_kinds.
unfold fvs in Fr; auto.
destruct* (unify_kinds_ok _ _ HI HS0).
unfold fvs in Fr. rewrite dom_concat. rewrite* dom_kinds_open_vars.
poses Hext (typ_subst_extend _ _ _ HS0 HI).
destruct* (unify_type _ _ HI).
simpl; intros.
destruct* H2.
inversions H2; clear H2.
split2*.
unfold sch_open_vars.
destruct* (Hs x).
poses HU (unify_types _ _ _ HI HS0).
destruct* Typ.
intuition.
intro; intros.
apply H7.
apply* binds_concat_ok.
rewrite <- (map_sch_subst_extend E Hext).
rewrite* <- (HU (sch_open_vars s x) T).
rewrite* <- Hext.
unfold fvs in Fr.
rewrite* sch_subst_open_vars.
poses Hkext (fun k => sym_eq (kind_subst_combine _ _ _ k Hext)).
rewrite (map_map_env _ _ _ K Hkext).
apply* typing_typ_well_subst.
rewrite* <- (map_map_env _ _ _ K Hkext).
repeat apply* kenv_ok_map.
Qed.
Lemma well_kinded_open_vars : forall S K Ks Xs,
fresh (dom S \u dom K) (length Ks) Xs ->
env_prop type S ->
list_forall2
(well_kinded (map (kind_subst S) (K & kinds_open_vars Ks Xs)))
(kinds_open (List.map (kind_subst S) Ks) (typ_fvars Xs))
(typ_fvars Xs).
Proof.
unfold kinds_open_vars, kinds_open; intros.
rewrite map_concat.
rewrite map_combine.
rewrite map_map.
rewrite <- (map_ext (fun k => kind_open (kind_subst S k) (typ_fvars Xs))).
rewrite <- (map_map (kind_subst S) (fun k => kind_open k (typ_fvars Xs))).
refine (well_kinded_combine _ _ Xs nil _).
rewrite dom_map.
rewrite* map_length.
intros. rewrite* kind_subst_open_vars.
Qed.
Lemma fv_in_typ_subst : forall S S0,
fv_in typ_fv (map (typ_subst S) S0) <<
S.diff (fv_in typ_fv S0) (dom S) \u fv_in typ_fv S.
Proof.
induction S0; intros y Hy; simpl in *; sets_simpl.
destruct a. simpl in Hy.
sets_solve.
use (typ_fv_subst0 _ _ H).
Qed.
Lemma fv_in_compose : forall S S0,
fv_in typ_fv (compose S S0) <<
S.diff (fv_in typ_fv S0) (dom S) \u fv_in typ_fv S.
Proof.
intros. unfold compose.
rewrite fv_in_concat.
sets_solve.
apply* fv_in_typ_subst.
Qed.
Hint Resolve ok_remove_env : core.
Lemma ok_remove_add_env : forall (A:Set) E v (a:A),
ok E -> ok (remove_env E v & v ~ a).
Proof.
intros. apply* ok_push.
rewrite* dom_remove_env.
Qed.
Hint Resolve ok_remove_add_env : core.
Lemma kind_subst_id : forall k, kind_subst id k = k.
Proof.
intros.
destruct k as [[kc kv kr kh]|]; simpl*.
apply kind_pi; simpl*.
clear kh; induction* kr.
destruct a; simpl. rewrite IHkr. rewrite* typ_subst_id.
Qed.
Lemma fv_in_kind_subst : forall S K,
fv_in kind_fv (map (kind_subst S) K) <<
S.diff (fv_in kind_fv K) (dom S) \u fv_in typ_fv S.
Proof.
induction K; simpl; intros y Hy. auto.
destruct a. simpl in Hy.
use (kind_fv_subst S k).
Qed.
Lemma unify_keep_fv' : forall K S E h pairs K0 S0,
Unify.unify h pairs K0 S0 = Some (K, S) ->
is_subst S0 -> ok K0 ->
fvs S K E << fvs S0 K0 E \u all_fv id pairs.
Proof.
intros until 2.
apply* (unify_ind (K':=K) (S':=S)
(fun K0 S0 pairs => ok K0 -> fvs S K E << fvs S0 K0 E \u all_fv id pairs));
clear H H0 K0 S0 pairs h.
intros until K1.
unfold K1, S1, fvs; clear K1 S1.
intros _ R1 R2 _ _ _ R4 IH HK0 y Hy.
forward ~IH as G; clear IH.
unfold all_fv; simpl. repeat rewrite typ_subst_id.
fold (all_fv id pairs).
rewrite dom_remove_env in G; auto. simpl in G.
puts (G _ Hy); clear G Hy.
sets_solve.
unfold compose in H0; rewrite dom_concat in H0. rewrite dom_map in H0.
simpl in H0.
puts (typ_fv_subst S0 t). rewrite R1 in H. simpl in H.
use (singleton_subset H).
puts (fv_in_compose (v ~ T) S0 H0).
simpl in H.
puts (typ_fv_subst S0 t0). rewrite R2 in H1. auto.
use (fv_in_remove_env _ _ K0 H0).
intros until K1.
unfold K1, S1, fvs; clear K1 S1.
intros Uk _ R1 R2 HS0 HS1 n IH HK0 y Hy.
forward ~IH as G; clear IH.
puts (G _ Hy); clear G Hy.
unfold all_fv; simpl. repeat rewrite typ_subst_id.
fold (all_fv id pairs).
rewrite dom_concat in H.
do 2 rewrite dom_remove_env in H; auto. simpl in H.
puts (typ_fv_subst S0 t). rewrite R1 in H0. simpl in H0.
puts (singleton_subset H0); clear H0.
puts (typ_fv_subst S0 t0). rewrite R2 in H0. simpl in H0.
puts (singleton_subset H0); clear H0.
puts (unify_kinds_fv _ _ id Uk).
rewrite all_fv_app in H.
rewrite kind_subst_id in H0.
puts (get_kind_fv_in id v K0).
puts (get_kind_fv_in id v0 K0).
puts (fv_in_kind_subst id K0).
replace (fv_in typ_fv id) with {} in H5 by (unfold id; simpl*).
sets_solve.
unfold compose in H6; rewrite dom_concat in H6. rewrite dom_map in H6.
simpl in H6. auto.
puts (fv_in_compose _ _ H6). simpl in H. auto.
auto.
puts (fv_in_remove_env _ _ (remove_env K0 v) H).
use (fv_in_remove_env _ _ K0 H6).
unfold all_fv; simpl; intros. use (H3 H4).
unfold all_fv; simpl; intros. use (H3 H4).
unfold all_fv; simpl; intros.
repeat rewrite typ_subst_id in *.
puts (typ_fv_subst S0 t).
puts (typ_fv_subst S0 t0).
rewrite H1 in H5; rewrite H2 in H6.
simpl in *.
puts (H3 H4).
clear -H5 H6 H7.
unfold fvs in *. auto.
unfold all_fv; simpl; intros.
use (H H0).
Qed.
Lemma soundness_var : forall h L0 v K0 E T S0 K S L,
soundness_spec h (trm_fvar v) K0 E T L0 S0 K S L.
Proof.
intros; intros Ht HI HS0 HTS0 HK0 Dis HL0 HE HT.
poses HI' HI; simpl in HI.
case_rewrite R1 (get v E).
destruct (var_freshes L0 (sch_arity s));
simpl proj1_sig in HI.
inversions HI; clear HI. rename H0 into HI.
refine (soundness_ind _ _ _ HI _ _ HI' _ _ _ _ _ _ _); auto.
apply (proj2 HE _ _ (binds_in R1)).
split2*.
unfold unify in HI.
forward~ (unify_keep_fv' _ _ HI HS0 (E:=E)) as G.
unfold fvs in *.
rewrite dom_concat in G; rewrite fv_in_concat in G.
unfold all_fv in G; simpl in G.
rewrite dom_kinds_open_vars in G; auto.
repeat rewrite typ_subst_id in G.
unfold sch_open_vars, typ_open_vars in G; simpl in G.
puts (fv_in_kinds_open_vars (sch_kinds s) x).
puts (fv_in_spec sch_fv _ _ _ (binds_in R1)).
fold env_fv in H4.
unfold sch_fv in H4; simpl in H4.
puts (typ_fv_open (typ_fvars x) (sch_type s)).
rewrite typ_fv_typ_fvars in H5.
auto.
apply* typing_var.
apply* kenv_ok_map.
split.
simpl; rewrite map_length.
rewrite (fresh_length _ _ _ f). apply types_typ_fvars.
destruct s as [Us Ks]; simpl.
apply* well_kinded_open_vars.
unfold fvs in HL0.
use (fresh_sub _ _ f HL0).
Qed.
Lemma kinds_subst_cst : forall S c,
List.map (kind_subst S) (sch_kinds (Delta.type c)) = sch_kinds (Delta.type c).
Proof.
intros.
assert (forall x, x \notin kind_fv_list (sch_kinds (Delta.type c))).
intros x Hx.
assert (x \in sch_fv (Delta.type c)).
unfold sch_fv.
sets_solve.
rewrite Delta.closed in H.
elim (in_empty H).
induction (sch_kinds (Delta.type c)). auto.
simpl in *.
rewrite IHl.
rewrite* kind_subst_fresh.
intro. use (H x).
intro; use (H x).
Qed.
Lemma soundness_cst : forall h L0 c K0 E T S0 K S L,
soundness_spec h (trm_cst c) K0 E T L0 S0 K S L.
Proof.
intros; intros Ht HI HS0 HTS0 HK0 Dis HL0 HE HT.
poses HI' HI; simpl in HI.
destruct (var_freshes L0 (sch_arity (Delta.type c)));
simpl in HI.
inversions HI; clear HI.
refine (soundness_ind _ _ _ H0 _ _ HI' _ _ _ _ _ _ _); auto.
apply Delta.scheme.
intros.
rewrite sch_subst_fresh; try (rewrite Delta.closed; intro; auto).
split.
unfold unify in H0.
forward~ (unify_keep_fv' _ _ H0 HS0 (E:=E)) as G.
unfold fvs in *.
rewrite dom_concat in G; rewrite fv_in_concat in G.
unfold all_fv in G; simpl in G.
rewrite dom_kinds_open_vars in G; auto.
repeat rewrite typ_subst_id in G.
unfold sch_open_vars, typ_open_vars in G; simpl in G.
puts (fv_in_kinds_open_vars (sch_kinds (Delta.type c)) x).
puts (Delta.closed c).
unfold sch_fv in H5; simpl in H5.
puts (eq_subset H5); clear H5.
puts (typ_fv_open (typ_fvars x) (sch_type (Delta.type c))).
rewrite typ_fv_typ_fvars in H5.
auto.
apply* typing_cst.
apply* kenv_ok_map.
split.
rewrite (fresh_length _ _ _ f). apply types_typ_fvars.
pattern (sch_kinds (Delta.type c)) at 2.
rewrite <- (kinds_subst_cst S0 c).
unfold fvs in HL0.
apply* well_kinded_open_vars.
apply* fresh_sub.
Qed.
Lemma soundness_abs : forall h L0 t K0 E T S0 S K L,
(forall t K0 E T L0 S0, soundness_spec h t K0 E T L0 S0 K S L) ->
soundness_spec (Datatypes.S h) (trm_abs t) K0 E T L0 S0 K S L.
Proof.
intros until L; intros IHh Ht HI HS0 HTS0 HK0 Dis HL0 HE HT; simpl in HI.
destruct (var_fresh L0); simpl in HI.
destruct (var_fresh (L0 \u {{x}})); simpl in HI.
destruct (var_fresh (dom E \u trm_fv t)); simpl in HI.
case_rewrite R1 (unify K0 (typ_arrow (typ_fvar x) (typ_fvar x0)) T S0).
destruct p as [K' S'].
rewrite normalize_typinf in HI.
unfold unify in R1.
destruct (unify_keep _ _ _ R1) as [HS' _]; auto.
destruct (unify_type _ _ R1); auto.
simpl; intros. destruct* H.
inversions* H.
destruct* (unify_kinds_ok _ _ R1). clear H1; destruct H2.
simpl in Ht. rewrite <- (trm_depth_open x1) in Ht.
poses Ht' (lt_S_n _ _ Ht).
destruct* (IHh _ _ _ _ _ _ Ht' HI); clear IHh HI.
forward~ (unify_keep_fv' _ _ R1 HS0 (E:=E)) as G.
unfold fvs in *.
unfold all_fv in G; simpl in G.
repeat rewrite typ_subst_id in G.
unfold env_fv; simpl. fold env_fv.