-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathML_SP_Inference_wf.v
3454 lines (3275 loc) · 111 KB
/
ML_SP_Inference_wf.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_wf.
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.
(* Free variables in the inference environment *)
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.
(* Prepare for inclusion of free variables *)
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; auto.
case_rewrite R1 (get v S).
forward* (fv_in_spec typ_fv S v t).
simpl in Hx; 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.
unfold kind_fv.
destruct k as [[kc kv kr kh]|]; simpl*.
clear kh; induction kr; simpl; intros x Hx; auto.
use (typ_fv_subst0 S (snd a)).
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.
use (typ_fv_subst0 S t).
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 T1 T2 K0 S0,
unify_spec ((T1,T2)::nil) K0 S0 (Some (K, S)) ->
fvs S K E << fvs S0 K0 E \u typ_fv T1 \u typ_fv T2.
Proof.
intros.
rewrite <- union_assoc.
replace (typ_fv T1 \u typ_fv T2) with (all_fv id ((T1,T2)::nil))
by (unfold all_fv; simpl; do 2 rewrite typ_subst_id; rewrite* union_empty_r).
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 K0 S0 T1 T2.
intros until K1.
subst K1 S1. unfold fvs in *.
intros _ R1 R2 _ R4 IH _ 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 by auto.
puts (G _ Hy); clear G Hy.
sets_solve.
unfold compose in H0; rewrite dom_concat, 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. subst S1 K1.
unfold fvs.
intros Uk _ R1 R2 n IH _ 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 by 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, dom_map in H6.
simpl in H6. auto.
puts (fv_in_compose _ _ H6). simpl in H. auto.
puts (fv_in_remove_env _ _ (remove_env K0 v) H).
use (fv_in_remove_env _ _ K0 H6).
unfold all_fv; simpl; intros. use (H2 H3).
unfold all_fv; simpl; intros. use (H2 H3).
unfold all_fv; simpl; intros.
repeat rewrite typ_subst_id in *.
puts (typ_fv_subst S0 t).
puts (typ_fv_subst S0 t0).
rewrite H0 in H4; rewrite H1 in H5.
simpl in *.
puts (H2 H3).
clear -H4 H5 H6.
unfold fvs in *. auto.
unfold all_fv; simpl; intros.
use (H H0).
Qed.
(* Dependently typed unification *)
Definition unify_dep T1 T2 K S :
is_subst S -> ok K ->
disjoint (dom S) (dom K) ->
let res := unify_spec ((T1,T2)::nil) K S in
{KS' | res (Some KS') /\ let (K',S') := KS' in
(ok K' /\ is_subst S' /\ disjoint (dom S') (dom K')) /\
(forall E, fvs S' K' E << fvs S K E \u typ_fv T1 \u typ_fv T2)}
+{res None}.
Proof.
introv HS HK D. intro.
subst res.
puts (@unify_spec_sound ((T1,T2)::nil) _ _ HS HK (lt2_wf _)).
destruct (unify HS HK (lt2_wf (size_pairs2 S K ((T1, T2) :: nil)))).
left*; esplit; split2*.
destruct p.
split.
revert H HK D.
eapply (unify_ind
(fun K S p => ok K -> disjoint (dom S) (dom K) ->
ok k /\ is_subst s /\ disjoint (dom s) (dom k))); auto.
auto*.
clear; intros.
apply* H4; clear H0 H4.
unfold S1, K1.
unfold compose; simpl.
rewrite dom_concat, dom_map; simpl.
do 2 rewrite dom_remove_env by auto.
use (typ_subst_res_fresh' _ HS H2).
intros.
apply* unify_keep_fv.
right*.
Qed.
(** 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).
Section SplitEnv.
Variables (A:Set) (B:vars).
Fixpoint split_env (E:env A) : env A * env A :=
match E with
| nil => (nil, nil)
| xk::E' =>
let (Eb, EB) := split_env E' in
if S.mem (fst xk) B then (Eb, xk::EB) else (xk::Eb, EB)
end.
Lemma split_env_ok : forall E Eb EB,
split_env E = (Eb, EB) -> ok E ->
ok (EB & Eb) /\ disjoint B (dom Eb) /\ dom EB << B /\
incl E (EB & Eb) /\ incl (EB & Eb) E.
Proof.
intros.
revert Eb EB H.
induction H0; intros.
inversions H. simpl. env_fix; auto.
simpl in H1.
case_rewrite R1 (split_env E).
case_rewrite R2 (S.mem x B).
inversions H1; clear H1.
env_fix.
destruct~ (IHok Eb e0) as [Hok [Dis [Dom [I1 I2]]]]; clear IHok.
destruct (ok_concat_inv _ _ Hok).
case_eq (get x (e0 & Eb)); intros.
assert (binds x a0 E) by auto.
elim (binds_fresh H4 H).
poses Hv' (get_none_notin _ H3); clear H3.
puts (S.mem_2 R2).
split4*.
split; intro; simpl; intros.
destruct H4. apply* in_or_concat.
destruct* (in_app_or _ _ _ (I1 _ H4)).
destruct* (in_app_or _ _ _ H4).
destruct* H5.
inversions H1; clear H1.
env_fix.
destruct* (IHok e EB) as [Hok [Dis [Dom [I1 I2]]]]; clear IHok.
case_eq (get x (EB & e)); intros.
assert (binds x a0 E) by auto.
elim (binds_fresh H2 H).
use (get_none_notin _ H1).
Qed.
End SplitEnv.
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 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.
Lemma dom_K_L : forall S K E T L,
fvs S K E \u typ_fv T << L -> dom K << L.
Proof.
unfold fvs; intros. auto.
Qed.
Lemma disjoint_fvar : forall S K E T L M Vs,
disjoint (dom S) (dom K) ->
fvs S K E \u typ_fv T << L ->
fresh L (sch_arity M) Vs ->
disjoint (dom S) (dom (K & kinds_open_vars (sch_kinds M) Vs)).
Proof.
intros.
rewrite dom_concat.
rewrite* dom_kinds_open_vars.
unfold fvs in H0. auto.
Qed.
Lemma subset_abs : forall S K E L S' K' v1 v2 T x,
fvs S K E \u typ_fv T << L ->
fvs S' K' E << fvs S K E \u ({{v1}} \u {{v2}}) \u typ_fv T ->
fvs S' K' (E & x ~ Sch (typ_fvar v1) nil) \u {{v2}} <<
L \u {{v1}} \u {{v2}}.
Proof.
intros.
unfold fvs in *.
unfold env_fv. rewrite fv_in_concat. fold (env_fv E).
unfold sch_fv; simpl*.
Qed.
Lemma subset_let1 : forall L0 L1 L2 L,
L0 \u L1 << L -> L0 \u L2 << L \u L2.
Proof.
intros; auto.
Qed.
Lemma typ_fv_generalize : forall Xs T,
typ_fv (typ_generalize Xs T) << typ_fv T.
Proof.
induction T; simpl; intros y Hy; auto.
destruct* (index eq_var_dec 0 v Xs).
Qed.
Lemma kinds_fv_generalize : forall Bs Ks,
kind_fv_list (List.map (kind_map (typ_generalize Bs)) Ks) << kind_fv_list Ks.
Proof.
intros. unfold kind_fv_list.
induction Ks; simpl*.
sets_solve.
apply S.union_2.
unfold kind_fv in *.
clear IHKs Ks; destruct a as [[kc kv kr kh]|]; simpl in *.
clear kh; induction kr; simpl in *. auto.
sets_solve.
use (typ_fv_generalize _ _ H0).
apply* S.union_3.
auto.
Qed.
Lemma sch_fv_generalize : forall Bs T Ks,
sch_fv (sch_generalize Bs T Ks) << sch_fv (Sch T Ks).
Proof.
intros.
unfold sch_generalize, sch_fv; simpl.
sets_solve. use (typ_fv_generalize _ _ H).
use (kinds_fv_generalize _ _ H).
Qed.
Lemma fv_in_kind_fv_list : forall Xs Ks,
length Xs = length Ks ->
fv_in kind_fv (combine Xs Ks) = kind_fv_list Ks.
Proof.
induction Xs; destruct Ks; simpl; intros; try discriminate.
auto.
inversion H.
rewrite* IHXs.
Qed.
Lemma ok_generalize : forall K0 K1 S' S E T L v L' L0 x,
ok K0 -> disjoint (dom S') (dom K0) ->
fvs S K1 E \u typ_fv T << L ->
fvs S' K0 E \u (L \u {{v}}) << 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 KAM := typinf_generalize K E' L0 T1 in
ok (fst KAM) /\ disjoint (dom S') (dom (fst KAM)) /\
fvs S' (fst KAM) (E & x ~ snd KAM) \u typ_fv T << L'.
Proof.
introv HK0 D' HL HL'; intros.
unfold KAM; clear KAM.
unfold typinf_generalize.
case_eq (split_env (close_fvk K (env_fv E')) K); introv R1.
case_eq (split_env (close_fvk K (typ_fv T1)) e); introv R2.
case_eq (split e2); introv R4.
case_eq (split_env L0 e); introv R3.
simpl.
destruct (split_env_ok _ R1 (ok_map0 _ HK0)) as [Okee0 [De [HLe [I1 I2]]]].
destruct (ok_concat_inv _ _ Okee0) as [Oke0 Oke].
puts (ok_disjoint _ _ Okee0).
destruct (split_env_ok _ R3 Oke) as [Oke34 [De2 [HLe2 [I3 I4]]]].
destruct (ok_concat_inv _ _ Oke34) as [Oke4 Oke3].
split.
assert (dom e4 << dom e) by use (incl_subset_dom I4).
auto.
assert (incl (e0 & e4) K).
intro; intros. apply I2.
destruct (in_app_or _ _ _ H0); apply* in_or_app.
puts (incl_subset_dom H0).
split.
clear -D' H1.
subst K. rewrite dom_map in H1. auto.
set (ftve := close_fvk K (env_fv E')).
set (fvT := close_fvk K (typ_fv T1)).
set (B := S.elements (S.diff fvT (ftve \u dom e2))).
unfold fvs, env_fv. simpl.
puts (@sch_fv_generalize (l++B) T1 (l0 ++ List.map (fun _ : var => None) B)).
intros y Hy.
apply HL'; clear HL' L'.
unfold fvs; simpl.
destruct (S.union_1 Hy); clear Hy; auto.
destruct (S.union_1 H3); clear H3.
unfold K in H1. rewrite dom_map in H1.
sets_solve.
puts (incl_fv_in_subset kind_fv H0 H3); clear H3.
puts (fv_in_kind_subst S' K0 H4); clear H4.
clear -H3; auto.
unfold env_fv.
destruct (S.union_1 H4); clear H4; auto.
puts (H2 _ H3); clear H2 H3. unfold sch_fv in H4. simpl in H4.
destruct (S.union_1 H4); clear H4.
clear -H2. subst T1.
use (typ_fv_subst S' _ H2).
puts (split_combine _ R4).
rewrite <- (fv_in_kind_fv_list (l ++ B)) in H2 by auto.
rewrite combine_app in H2 by auto.
rewrite H3 in H2.
puts fv_in_concat. unfold concat in H4.
rewrite H4 in H2; clear H4.
destruct (S.union_1 H2); clear H2.
assert (incl e2 K).
intro; intros.
destruct (split_env_ok _ R2 Oke) as [Oke12 [De1 [HLe1 [I5 I6]]]].
apply I2.
apply in_or_app; left*.
puts (incl_fv_in_subset kind_fv H2 H4); clear H2 H4.
puts (fv_in_kind_subst S' K0 H5); clear H5.
clear -H2; auto.
elimtype False; clear -H4.
unfold kind_fv in H4.
induction B; simpl in *. elim (in_empty H4).
elim IHB. auto.
Qed.
Lemma subset_app1 : forall L0 L1 L L2,
L0 \u L1 << L -> L0 \u (L2 \u L1) << L \u L2.
Proof.
intros; auto.
Qed.
Lemma subset_app2 : forall L0 L L1 L',
L0 \u (L \u L1) << L' -> L0 \u L1 << L'.
Proof.
intros; auto.
Qed.
Lemma subset_fvar : forall S K E T L L0 x M Vs,
fresh L (sch_arity M) Vs ->
binds x M E ->
fvs S K E \u typ_fv T << L ->
L0 << fvs S (K & kinds_open_vars (sch_kinds M) Vs) E \u
typ_fv (sch_open_vars M Vs) \u typ_fv T ->
L0 \u L << L \u mkset Vs.
Proof.
unfold fvs; simpl; introv Fr B HL.
rewrite dom_concat.
rewrite dom_kinds_open_vars by auto.
rewrite fv_in_concat.
puts (fv_in_kinds_open_vars (sch_kinds M) Vs).
assert (sch_fv M << env_fv E) by apply* fv_in_spec.
unfold sch_fv in H0; simpl in H0.
intros; sets_solve.
unfold sch_open_vars, typ_open_vars in H3; simpl in H3.
puts (typ_fv_open _ _ H3).
rewrite typ_fv_typ_fvars in H1.
auto.
Qed.
Lemma subset_abs2 : forall S' K' E x L v1 v2 L',
fvs S' K' (E & x ~ Sch (typ_fvar v1) nil) \u (L \u {{v1}} \u {{v2}}) << L' ->
fvs S' K' E \u L << L'.
Proof.
unfold fvs, env_fv, sch_fv; simpl; intros. auto.
Qed.
Lemma subset_let2 : forall L0 L v L' S' K' E x M L'',
L0 \u (L \u {{v}}) << L' ->
fvs S' K' (E & x ~ M) \u L' << L'' ->
fvs S' K' E \u L << L''.
Proof.
unfold fvs, env_fv; simpl; intros; auto.
Qed.
Lemma subset_app3 : forall L0 L L2 L' L1 L'',
L0 \u (L \u L2) << L' ->
L1 \u L' << L'' ->
L1 \u L << L''.
Proof.
intros; auto.
Qed.
Lemma subset_cst : forall S K E T L L0 c Vs,
let M := Delta.type c in
fresh L (sch_arity M) Vs ->
fvs S K E \u typ_fv T << L ->
L0 << fvs S (K & kinds_open_vars (sch_kinds M) Vs) E \u
typ_fv (sch_open_vars M Vs) \u typ_fv T ->
L0 \u L << L \u mkset Vs.
Proof.
unfold fvs; simpl; intros until Vs.
set (M:=Delta.type c). intros Fr HL.
rewrite dom_concat.
rewrite dom_kinds_open_vars by auto.
rewrite fv_in_concat.
puts (fv_in_kinds_open_vars (sch_kinds M) Vs).
assert (sch_fv M << {}).
puts (Delta.closed c). unfold M; rewrite* H0.
unfold sch_fv in H0; simpl in H0.
intros; sets_solve.
unfold sch_open_vars, typ_open_vars in H3; simpl in H3.
puts (typ_fv_open _ _ H3).
rewrite typ_fv_typ_fvars in H1.
auto.
Qed.
Definition get_dep (A:Set) x E : {a:A | binds x a E}+{x # E}.
Proof.
intros.
unfold binds; case_eq (get x E); intros.
left*; esplit; reflexivity.
right*.
Qed.
Definition typinf_res E L (res : kenv * subs * vars) :=
let (KS',L') := res in let (K',S') := KS' in
(ok K' /\ is_subst S' /\ disjoint (dom S') (dom K')) /\ fvs S' K' E \u L << L'.
Fixpoint typinf K E t T L S (h:Acc lt (trm_depth t)) (HS:is_subst S) (HK:ok K)
(D:disjoint (dom S) (dom K)) (HL: fvs S K E \u typ_fv T << L) {struct h} :
option (sig (typinf_res E L)) :=
match t as t' return t = t' -> option (sig (typinf_res E L)) with
| trm_bvar _ => fun eq => None
| trm_fvar x => fun eq =>
match get_dep x E with
| inright _ => None
| inleft (exist _ M eq1) =>
let (Vs, Fr) := var_freshes L (sch_arity M) in
match unify_dep (M ^ Vs) T HS
(ok_kinds_open_vars _ _ HK (fresh_sub _ _ Fr (dom_K_L _ HL)))
(disjoint_fvar _ _ _ D HL Fr) with
| inleft (exist _ (K',S') (conj _ (conj HKSD' HL'))) =>
Some (exist _ (K',S',L \u mkset Vs)
(conj HKSD' (subset_fvar _ _ Fr eq1 HL (HL' E))))
| inright _ => None
end
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_dep (typ_arrow (typ_fvar v1) (typ_fvar v2)) T HS HK D with
| inright _ => None
| inleft (exist _ (K',S') (conj _ (conj (conj HK' (conj HS' D')) HL'))) =>
match @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) HS' HK' D'
(subset_abs _ HL (HL' E)) with
| None => None
| Some (exist _ (K',S',L') (conj HKSD' HL')) =>
Some (exist _ (K',S',L') (conj HKSD' (subset_abs2 HL')))
end
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)
HS HK D (subset_let1 HL) with
| Some (exist _ (K0,S',L') (conj (conj HK0 (conj HS' D')) HL')) =>
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 KAM := 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
match ok_generalize T (vars_subst S' (kdom K)) x HK0 D' HL HL' with
(conj HKA (conj DKA HL'')) =>
match @typinf (fst KAM) (E & x ~ snd KAM) (t2 ^ x) T L' S'
(dom_inv_let2 x h eq) HS' HKA DKA HL'' with
| None => None
| Some (exist _ (K',S',L') (conj HKSD' HL'')) =>
Some (exist _ (K',S',L') (conj HKSD' (subset_let2 HL' HL'')))
end
end
| 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) HS HK D (subset_app1 HL) with
| Some (exist _ (K',S',L') (conj (conj HK' (conj HS' D')) HL')) =>
match @typinf K' E t2 (typ_fvar v) L' S' (dom_inv_app2 h eq) HS' HK' D'
(subset_app2 HL') with
| None => None
| Some (exist _ (K',S',L') (conj HKSD' HL'')) =>
Some (exist _ (K',S',L') (conj HKSD' (subset_app3 HL' HL'')))
end
| None => None
end
| trm_cst c => fun eq =>
let M := Delta.type c in
let (Vs, Fr) := var_freshes L (sch_arity M) in
match unify_dep (M ^ Vs) T HS
(ok_kinds_open_vars _ _ HK (fresh_sub _ _ Fr (dom_K_L _ HL)))
(disjoint_fvar _ _ _ D HL Fr) with
| inleft (exist _ (K',S') (conj _ (conj HKSD' HL'))) =>
Some (exist _ (K',S',L \u mkset Vs)
(conj HKSD' (subset_cst _ _ _ Fr HL (HL' E))))
| inright _ => None
end
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.
Lemma subset_typinf' : forall E v,
fvs id empty E \u typ_fv (typ_fvar v) << env_fv E \u {{v}}.
Proof.
unfold fvs; simpl.
intros. auto.
Qed.
Definition typinf' E t :=
let v := Variables.var_default in
let min_vars := env_fv E \u {{v}} in
let V := typ_fvar v in
match
@typinf empty E t V min_vars id (lt_wf _) is_subst_id (ok_empty _)
(@disjoint_empty _) (@subset_typinf' E v)
with None => None
| Some (exist _ (k, s, L) _) =>
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 unify_kinds_all_kind_types : forall P k1 k2 k l,
unify_kinds k1 k2 = Some (k, l) ->
All_kind_types P k1 ->
All_kind_types P k2 ->
All_kind_types P k /\
forall T1 T2, In (T1,T2) l -> P T1 /\ P T2.
Proof.
intros.
destruct k1 as [[kc1 kv1 kr1 kh1]|]; destruct k2 as [[kc2 kv2 kr2 kh2]|];
simpl in *; try solve [inversions H; split2*; intros; elim H2].
destruct (Cstr.valid_dec (Cstr.lub kc1 kc2)); try discriminate.
inversions H; clear H; simpl.
apply (unify_rel_all_kind_types v H0 H1).
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' pairs K S,
unify_spec pairs K S (Some (K', 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.
intros until S.
apply (unify_ind (fun K S pairs =>
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')); clear; auto; intros.
subst S1 K1.
apply* H4; clear H4.
apply* env_prop_type_compose.
destruct* (H7 t t0).
puts (typ_subst_type H5 H8). rewrite H1 in H9.
intro; intros.
destruct* H10. inversion H10. subst a. auto.
subst S1 K1.
destruct* (unify_kinds_all_kind_types (P:=type) H).
unfold get_kind. case_eq (get v K); introv Bv; auto.
apply* (proj2 H6 v).
unfold get_kind. case_eq (get v0 K); introv Bv0; auto.
apply* (proj2 H6 v0).
apply~ H4; clear H4.
apply* kenv_ok_concat.
intros. destruct* (in_app_or _ _ _ H4).
apply~ H2; clear H2.
destruct* (H5 t t0).
puts (typ_subst_type H3 H2).
puts (typ_subst_type H3 H6).
rewrite H0 in H7; rewrite H1 in H8.
inversions H7; inversions H8.
simpl. intros.
destruct H9. inversions* H9.
destruct H9. inversions* H9.
apply* H5.
apply~ H; clear H.
simpl; intros.
destruct H. inversions H. destruct* (H2 T2 T1).
apply* H2.
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,