-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathML_SP_Unify.v
1834 lines (1702 loc) · 54.3 KB
/
ML_SP_Unify.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 unification for mini-ML with structural polymorphism *
* Jacques Garrigue, July 2008 *
***************************************************************************)
Require Import Arith List Metatheory.
Require Import ML_SP_Definitions Cardinal ML_SP_Eval.
Require Omega.
Ltac omega := Omega.omega.
Set Implicit Arguments.
Module MkUnify(Cstr:CstrIntf)(Const:CstIntf).
Module MyEval := MkEval(Cstr)(Const).
Import MyEval.
Import Rename.
Import Sound.
Import Infra.
Import Defs.
(* Composition of substitutions *)
Definition compose S1 S2 : subs := S1 & map (typ_subst S1) S2.
(* Inclusion of substitutions. Very handy to use in proofs *)
Definition extends S S0 :=
forall T, typ_subst S (typ_subst S0 T) = typ_subst S T.
Lemma extends_trans : forall S1 S2 S3,
extends S1 S2 -> extends S2 S3 -> extends S1 S3.
Proof.
intros; intro.
rewrite <- H. rewrite <- (H T).
rewrite* H0.
Qed.
(* Unifiers *)
Definition unifies S pairs :=
forall T1 T2, In (T1, T2) pairs -> typ_subst S T1 = typ_subst S T2.
(* Subsititions should be in normal form *)
Definition is_subst (S : subs) :=
env_prop (fun T => disjoint (dom S) (typ_fv T)) S.
Section Moregen.
(* Here we relate extends with the more usual notional of generality *)
Definition moregen S0 S :=
exists S1, forall T, typ_subst S T = typ_subst S1 (typ_subst S0 T).
(* Extends implies more general *)
Lemma extends_moregen : forall S S0,
extends S S0 -> moregen S0 S.
Proof.
intros.
exists* S.
Qed.
Lemma typ_subst_idem : forall S T,
is_subst S -> typ_subst S (typ_subst S T) = typ_subst S T.
Proof.
intros.
induction T; simpl. auto.
case_eq (get v S); intros.
rewrite* typ_subst_fresh.
simpl.
rewrite* H0.
simpl; congruence.
Qed.
(* For substitutions in normal form, moregeneral implies extends *)
Lemma moregen_extends : forall S S0,
moregen S0 S -> is_subst S0 -> extends S S0.
Proof.
intros; intro.
destruct H as [S1 Heq].
rewrite Heq.
rewrite* typ_subst_idem.
Qed.
End Moregen.
Fixpoint unify_kind_rel (kr kr':list(Cstr.attr*typ)) (uniq:Cstr.attr -> bool)
(pairs:list(typ*typ)) {struct kr} :=
match kr with
| nil => (kr', pairs)
| (l,T)::krem =>
if uniq l then
match assoc Cstr.eq_dec l kr' with
| None => unify_kind_rel krem ((l,T)::kr') uniq pairs
| Some T' => unify_kind_rel krem kr' uniq ((T,T')::pairs)
end
else unify_kind_rel krem ((l,T)::kr') uniq pairs
end.
Fixpoint remove_env (A:Set) (E:Env.env A) (x:var) {struct E} : Env.env A :=
match E with
| nil => nil
| (y,a)::E' =>
if x == y then E' else (y,a) :: remove_env E' x
end.
Lemma unify_coherent : forall kc kr,
coherent kc (fst (unify_kind_rel kr nil (Cstr.unique kc) nil)).
Proof.
intros until kr.
set (kr' := @nil (Cstr.attr*typ)).
set (pairs' := @nil (typ*typ)).
assert (coherent kc kr'). intro; intros. elim H0.
gen kr' pairs'.
induction kr; simpl; intros. auto.
destruct a.
case_eq (Cstr.unique kc a); introv R.
case_eq (assoc Cstr.eq_dec a kr'); introv R1. apply* IHkr.
apply IHkr.
intro; intros.
simpl in *; destruct H1; [inversions H1|]; destruct H2. inversions* H2.
elim (assoc_complete _ _ _ _ R1 H2).
inversions H2; elim (assoc_complete _ _ _ _ R1 H1).
apply* (H x).
apply IHkr.
intro; intros.
simpl in *.
destruct (Cstr.eq_dec x a).
subst. rewrite R in H0; discriminate.
apply* (H x). destruct* H1. inversions* H1.
destruct* H2. inversions* H2.
Qed.
Definition unify_kinds (k1 k2:kind) : option (kind * list (typ*typ)).
intros.
refine (
match k1, k2 with
| None, _ => Some (k2, nil)
| Some _, None => Some (k1, nil)
| Some (@Kind kc1 kv1 kr1 kh1), Some (@Kind kc2 kv2 kr2 kh2) =>
let kc := Cstr.lub kc1 kc2 in
if Cstr.valid_dec kc then
let krp := unify_kind_rel (kr1 ++ kr2) nil (Cstr.unique kc) nil in
Some (Some (@Kind kc _ (fst krp) _), snd krp)
else None
end).
auto.
unfold krp; apply unify_coherent.
Defined.
Definition get_kind x E : kind :=
match get x E with
| Some k => k
| None => None
end.
Lemma binds_get_kind : forall x k K,
binds x k K -> get_kind x K = k.
Proof.
intros.
unfold get_kind. rewrite* H.
Qed.
Lemma get_kind_binds : forall x k K,
get_kind x K = Some k -> binds x (Some k) K.
Proof.
unfold get_kind; intros.
case_rewrite R (get x K).
subst*.
Qed.
Hint Resolve get_kind_binds : core.
Definition unify_vars (K:kenv) (x y:var) :=
match unify_kinds (get_kind x K) (get_kind y K) with
| Some (k, pairs) => Some (remove_env (remove_env K x) y & y ~ k, pairs)
| None => None
end.
Definition unify_nv (unify : kenv -> subs -> option (kenv * subs)) K S x T :=
if S.mem x (typ_fv T) then None else
match get_kind x K with
| Some _ => None
| None => unify (remove_env K x) (compose (x ~ T) S)
end.
Fixpoint unify0 unify (h:nat) (pairs:list(typ*typ)) (K:kenv) (S:subs) {struct h}
: option (kenv * subs) :=
match h with 0 => None
| S h' =>
match pairs with
| nil => Some (K,S)
| (T1,T2) :: pairs' =>
match typ_subst S T1, typ_subst S T2 with
| typ_bvar n, typ_bvar m =>
if n === m then unify0 unify h' pairs' K S else None
| typ_fvar x, typ_fvar y =>
if x == y then unify0 unify h' pairs' K S else
match unify_vars K x y with
| Some (K', pairs) =>
unify (pairs ++ pairs') K' (compose (x ~ typ_fvar y) S)
| None => None
end
| typ_fvar x, T =>
unify_nv (unify pairs') K S x T
| T, typ_fvar x =>
unify_nv (unify pairs') K S x T
| typ_arrow T11 T12, typ_arrow T21 T22 =>
unify0 unify h' ((T11,T21)::(T12,T22)::pairs') K S
| _, _ =>
None
end
end
end.
Section Accum.
Variables A B : Type.
Variables (f : A -> B) (op : B->B->B) (unit : B).
Fixpoint accum (l:list A) {struct l} : B :=
match l with
| nil => unit
| a::rem => op (f a) (accum rem)
end.
Variable op_assoc : forall a b c, op a (op b c) = op (op a b) c.
Variable op_unit : forall a, op unit a = a.
Lemma accum_app : forall l2 l1,
accum (l1 ++ l2) = op (accum l1) (accum l2).
Proof.
induction l1; simpl. rewrite* op_unit.
rewrite <- op_assoc.
rewrite* IHl1.
Qed.
End Accum.
Fixpoint all_types S (pairs:list(typ*typ)) {struct pairs} : list typ :=
match pairs with
| nil => nil
| p::rem =>
typ_subst S (fst p) :: typ_subst S (snd p) :: all_types S rem
end.
Fixpoint typ_size (T : typ) : nat :=
match T with
| typ_arrow T1 T2 => S (typ_size T1 + typ_size T2)
| _ => 1
end.
Definition pairs_size S pairs := accum typ_size plus 0 (all_types S pairs).
Fixpoint unify (h:nat) (pairs:list (typ*typ)) (K:kenv) (S:subs) {struct h} :=
match h with
| 0 => None
| S h' => unify0 (unify h') (pairs_size S pairs + 1) pairs K S
end.
Lemma typ_subst_compose : forall S1 S2 T,
typ_subst (compose S1 S2) T = typ_subst S1 (typ_subst S2 T).
Proof.
induction T; simpl; intros; auto.
unfold compose.
simpl; case_eq (get v S2); intros.
rewrite* (binds_prepend S1 (binds_map (typ_subst S1) H)).
simpl.
case_eq (get v S1); intros.
rewrite* (binds_concat_fresh (map (typ_subst S1) S2) H0).
case_eq (get v (S1 & map (typ_subst S1) S2)); intros; auto.
destruct (binds_concat_inv H1).
destruct H2. rewrite H3 in H0. discriminate.
destruct (binds_map_inv _ _ H2).
rewrite (proj2 H3) in H; discriminate.
rewrite* IHT1.
rewrite* IHT2.
Qed.
Lemma binds_typ_subst : forall x T S,
binds x T S -> typ_subst S (typ_fvar x) = T.
Proof.
intros. simpl. rewrite* H.
Qed.
Lemma disjoint_subst : forall x T L T',
disjoint ({{x}} \u L) (typ_fv T) ->
disjoint L (typ_fv T') ->
disjoint ({{x}} \u L) (typ_fv (typ_subst (x ~ T) T')).
Proof.
induction T'; simpl; intros; auto.
destruct* (v == x).
simpl*.
forward~ IHT'1 as HT1.
forward~ IHT'2 as HT2.
Qed.
Lemma add_binding_is_subst : forall S x T,
is_subst S ->
disjoint (dom S) (typ_fv T) ->
x \notin (typ_fv T) ->
is_subst (compose (x ~ T) S).
Proof.
intros.
unfold compose.
intro; intros.
rewrite dom_concat; rewrite dom_map.
simpl. rewrite union_empty_r.
destruct (in_app_or _ _ _ H2).
destruct (in_map_inv _ _ _ _ H3) as [b [F B']].
subst.
use (H _ _ B').
simpl in *.
apply* disjoint_subst.
simpl in H3. destruct* H3.
inversions* H3.
Qed.
Hint Resolve add_binding_is_subst : core.
Lemma typ_subst_disjoint : forall S T,
is_subst S -> disjoint (dom S) (typ_fv (typ_subst S T)).
Proof.
intros; induction T; simpl in *; auto.
case_eq (get v S); intros.
use (H _ _ (binds_in H0)).
simpl*.
Qed.
Lemma typ_subst_res_fresh : forall S T T',
is_subst S -> typ_subst S T = T' -> disjoint (dom S) (typ_fv T').
Proof.
intros.
use (typ_subst_disjoint T H).
rewrite* <- H0.
Qed.
Lemma typ_subst_res_fresh' : forall S T v,
is_subst S -> typ_subst S T = typ_fvar v -> v # S.
Proof.
intros.
use (typ_subst_res_fresh _ H H0).
Qed.
Hint Resolve typ_subst_disjoint typ_subst_res_fresh typ_subst_res_fresh' : core.
Lemma binds_add_binding : forall S T0 T1 v x T,
typ_subst S T0 = typ_fvar v ->
binds x (typ_subst S T) S ->
binds x (typ_subst (compose (v ~ T1) S) T) (compose (v ~ T1) S).
Proof.
intros.
rewrite typ_subst_compose.
unfold compose.
apply binds_prepend.
apply* binds_map.
Qed.
Hint Resolve binds_add_binding : core.
Definition id := Env.empty (A:=typ).
Lemma typ_subst_id : forall T, typ_subst id T = T.
Proof.
intro.
apply* typ_subst_fresh.
Qed.
Lemma is_subst_id : is_subst id.
Proof.
unfold id, is_subst. intro; intros. simpl*.
Qed.
Lemma dom_remove_env : forall (A:Set) v (K:Env.env A),
ok K -> dom (remove_env K v) = S.remove v (dom K).
Proof.
induction K; simpl; intros.
apply eq_ext; intros; split; intro; auto.
destruct a.
inversions H.
destruct (v == v0).
subst v0.
rewrite remove_union.
rewrite remove_single. rewrite* remove_notin. rewrite* union_empty_l.
simpl.
rewrite remove_union.
rewrite* IHK.
assert (v \notin {{v0}}) by auto.
rewrite* (remove_notin H0).
Qed.
Lemma ok_remove_env : forall (A:Set) v (E:Env.env A),
ok E -> ok (remove_env E v).
Proof.
induction E; simpl; intros. auto.
destruct a.
inversions H.
destruct* (v == v0).
apply* ok_cons.
clear -H4.
induction E; simpl. simpl in H4. auto.
destruct a.
simpl in H4.
destruct* (v == v1).
simpl.
apply* notin_union_l.
Qed.
Hint Resolve ok_remove_env : core.
Lemma binds_remove_env : forall (A:Set) v K x (a:A),
binds x a K -> x <> v -> binds x a (remove_env K v).
Proof.
unfold binds; induction K; simpl; intros. auto.
destruct a; simpl in *.
destruct (x == v0).
destruct (v == v0). subst. elim H0; auto.
simpl. destruct* (x == v0).
destruct* (v == v0).
simpl. destruct* (x == v0).
Qed.
Hint Resolve binds_remove_env : core.
Lemma disjoint_add_binding : forall v T S (K:kenv),
is_subst S -> ok K ->
disjoint (dom S) (dom K) ->
disjoint (dom (compose (v ~ T) S)) (dom (remove_env K v)).
Proof.
intros.
rewrite* dom_remove_env.
unfold compose.
rewrite dom_concat.
simpl; rewrite* dom_map.
Qed.
Hint Resolve disjoint_add_binding : core.
Definition kind_entails k k' :=
match k' with
| None => True
| Some c' => match k with
| Some c => entails c c'
| None => False
end
end.
Lemma kind_entails_well_kinded : forall k k' K T,
kind_entails k k' ->
well_kinded K k T ->
well_kinded K k' T.
Proof.
unfold kind_entails; intros.
inversions H0; clear H0; destruct* k'; try apply wk_any.
apply (wk_kind H1). apply (entails_trans H2 H).
Qed.
Hint Resolve kind_entails_well_kinded : core.
Lemma neq_notin_fv : forall v v0,
v <> v0 -> v \notin (typ_fv (typ_fvar v0)).
Proof. simpl*. Qed.
Hint Resolve neq_notin_fv : core.
Section Soundness.
Variables (K':kenv) (S':subs).
Lemma unify_ind : forall (P : kenv -> subs -> list (typ * typ) -> Prop),
(is_subst S' -> P K' S' nil) ->
(forall h pairs K T S v t t0,
let S1 := compose (v ~ T) S in
let K1 := remove_env K v in
unify h pairs K1 S1 = Some (K', S') ->
typ_subst S t = typ_fvar v ->
typ_subst S t0 = T ->
is_subst S -> is_subst S1 ->
v \notin typ_fv T -> get_kind v K = None ->
P K1 S1 pairs -> P K S ((t,t0)::pairs)) ->
(forall h pairs K S v v0 k l t t0,
let S1 := compose (v ~ typ_fvar v0) S in
let K1 := remove_env (remove_env K v) v0 & v0 ~ k in
unify_kinds (get_kind v K) (get_kind v0 K) = Some (k, l) ->
unify h (l ++ pairs) K1 S1 = Some (K', S') ->
typ_subst S t = typ_fvar v ->
typ_subst S t0 = typ_fvar v0 ->
is_subst S -> is_subst S1 ->
v <> v0 ->
P K1 S1 (l ++ pairs) -> P K S ((t,t0)::pairs)) ->
(forall h h0 K S t t0 pairs n,
unify0 (unify h) h0 pairs K S = Some (K', S') -> is_subst S ->
typ_subst S t = typ_bvar n ->
typ_subst S t0 = typ_bvar n ->
P K S pairs -> P K S ((t,t0)::pairs)) ->
(forall h h0 K S t t0 pairs v,
unify0 (unify h) h0 pairs K S = Some (K', S') -> is_subst S ->
typ_subst S t = typ_fvar v ->
typ_subst S t0 = typ_fvar v ->
P K S pairs -> P K S ((t,t0)::pairs)) ->
(forall h h0 K S t t0 pairs t1 t2 t3 t4,
unify0 (unify h) h0 ((t1,t3)::(t2,t4)::pairs) K S = Some (K',S') ->
is_subst S ->
typ_subst S t = typ_arrow t1 t2 ->
typ_subst S t0 = typ_arrow t3 t4 ->
P K S ((t1,t3)::(t2,t4)::pairs) -> P K S ((t,t0)::pairs)) ->
(forall K S t t0 pairs,
P K S ((t,t0)::pairs) -> P K S ((t0,t)::pairs)) ->
forall h pairs K S,
unify h pairs K S = Some (K', S') ->
is_subst S ->
P K S pairs.
Proof.
introv Hnil Hnv Hvars Hbv Hfv. intros Harr Hsw.
induction h; simpl; intros pairs K S HU HS.
discriminate.
set (h0 := pairs_size S pairs + 1) in HU. clearbody h0.
gen pairs; induction h0; simpl; intros.
discriminate.
destruct pairs.
inversions HU.
auto.
destruct p.
assert (Hnv1: forall v T t t0,
typ_subst S t = typ_fvar v -> typ_subst S t0 = T ->
unify_nv (unify h pairs) K S v T = Some (K',S') ->
P K S ((t,t0)::pairs)).
unfold unify_nv; simpl. introv R1 R2 H'.
case_rewrite R3 (S.mem v (typ_fv T)).
fold kind in *.
case_rewrite R4 (get_kind v K).
apply* Hnv.
case_rewrite R1 (typ_subst S t); case_rewrite R2 (typ_subst S t0).
destruct (n === n0).
subst n0.
auto*.
discriminate.
rewrite <- R1 in HU.
apply Hsw.
apply* Hnv1.
rewrite <- R2 in HU.
apply* Hnv1.
destruct (v == v0).
subst v0. auto*.
unfold unify_vars in HU.
case_rewrite R3 (unify_kinds (get_kind v K) (get_kind v0 K)).
destruct p.
apply* Hvars.
rewrite <- R2 in HU.
apply* Hnv1.
rewrite <- R1 in HU.
apply Hsw.
apply* Hnv1.
apply* Harr.
Qed.
Lemma unify_keep : forall h pairs K S,
unify h pairs K S = Some (K', S') ->
is_subst S ->
is_subst S' /\
forall x T, binds x (typ_subst S T) S -> binds x (typ_subst S' T) S'.
Proof.
intros.
apply* (unify_ind
(fun K S _ => is_subst S' /\
forall x T, binds x (typ_subst S T) S -> binds x (typ_subst S' T) S'));
clear H H0 h pairs K S; intros.
destruct H6; split2*.
intros. apply H7.
apply* binds_add_binding.
intros.
intuition.
apply H8. apply* binds_add_binding.
Qed.
Lemma binds_subst_idem : forall x T S,
binds x T S -> is_subst S -> binds x (typ_subst S T) S.
Proof.
intros.
use (binds_typ_subst H).
use (f_equal (typ_subst S) H1).
rewrite typ_subst_idem in H2; auto.
congruence.
Qed.
Hint Resolve binds_subst_idem : core.
Lemma typ_subst_extend : forall h pairs K S,
is_subst S ->
unify h pairs K S = Some (K', S') ->
extends S' S.
Proof.
intros.
destruct* (unify_keep _ _ _ H0).
clear H0.
intro.
induction T. simpl*.
remember (typ_subst S (typ_fvar v)) as T'.
use (f_equal (typ_subst S) HeqT').
rewrite typ_subst_idem in H0; auto.
simpl in H0.
case_rewrite R (get v S).
subst.
use (H2 _ _ R).
rewrite* (binds_typ_subst H0).
simpl in HeqT'. rewrite R in HeqT'. subst*.
simpl. congruence.
Qed.
Hint Resolve typ_subst_extend : core.
Lemma typ_size_1 : forall T, 1 <= typ_size T.
destruct T; simpl; omega.
Qed.
Lemma pairs_size_decr : forall S t t0 pairs,
Datatypes.S (pairs_size S pairs) < pairs_size S ((t,t0)::pairs).
Proof.
intros.
unfold pairs_size; simpl.
puts (typ_size_1 (typ_subst S t)).
puts (typ_size_1 (typ_subst S t0)).
omega.
Qed.
Lemma unify0_unify : forall h0 h K S K' S' pairs,
unify0 (unify h) h0 pairs K S = Some (K', S') ->
is_subst S ->
unify (Datatypes.S h) pairs K S = Some (K', S').
Proof.
intros.
simpl.
set (h1 := pairs_size S pairs + 1).
assert (pairs_size S pairs < h1) by (unfold h1; omega).
clearbody h1.
gen pairs h1; induction h0; simpl; intros. discriminate.
destruct h1. elimtype False; omega.
simpl.
destruct pairs. auto.
destruct p.
puts (pairs_size_decr S t t0 pairs).
case_rewrite R1 (typ_subst S t); case_rewrite R2 (typ_subst S t0); auto.
destruct (n === n0). subst.
apply* IHh0. omega.
auto.
destruct (v == v0). subst.
apply* IHh0. omega.
auto.
apply* IHh0.
clear IHh0 H H2; unfold pairs_size in *; simpl in *.
rewrite <- (typ_subst_idem t H0) in H1.
rewrite <- (typ_subst_idem t0 H0) in H1.
rewrite R1 in H1; rewrite R2 in H1. simpl in H1.
puts (typ_size_1 (typ_subst S t1)).
puts (typ_size_1 (typ_subst S t2)).
puts (typ_size_1 (typ_subst S t3)).
puts (typ_size_1 (typ_subst S t4)).
omega.
Qed.
Theorem unify_types : forall h pairs K S,
unify h pairs K S = Some (K',S') ->
is_subst S ->
unifies S' pairs.
Proof.
intros.
apply* (unify_ind (fun _ _ => unifies S')); clear H H0 h K S pairs; intros;
intro; simpl; intros; intuition;
try (unfold S1 in *; inversions H8; clear H8);
try (poses HU (unify0_unify _ _ _ _ H H0));
try (inversions H5; clear H5; rewrite <- (typ_subst_extend _ _ _ H0 HU);
rewrite <- (typ_subst_extend _ _ _ H0 HU T2); congruence).
rewrite <- (typ_subst_extend _ _ _ H3 H).
rewrite <- (typ_subst_extend _ _ _ H3 H T2).
rewrite typ_subst_compose. rewrite H0.
simpl. destruct* (v == v).
rewrite typ_subst_compose.
rewrite* (typ_subst_fresh (v ~ typ_subst S T2)).
simpl*. disjoint_solve. intuition.
rewrite <- (typ_subst_extend _ _ _ H4 H0).
rewrite <- (typ_subst_extend _ _ _ H4 H0 T2).
do 2 rewrite typ_subst_compose. rewrite H1; rewrite H2.
simpl. destruct* (v == v). destruct* (v0 == v).
inversions H5; clear H5.
rewrite <- (typ_subst_extend _ _ _ H0 HU).
rewrite <- (typ_subst_extend _ _ _ H0 HU T2).
rewrite H2; rewrite H1.
simpl.
rewrite* (H3 t1 t3).
rewrite* (H3 t2 t4).
inversions H1.
symmetry.
apply* H.
Qed.
Lemma kind_subst_idem : forall S k,
is_subst S -> kind_subst S (kind_subst S k) = kind_subst S k.
Proof.
intros.
destruct k as [[kc kv kr kh]|].
simpl.
apply* kind_pi; simpl.
clear kh; induction kr; simpl. auto.
rewrite IHkr.
rewrite* typ_subst_idem.
auto.
Qed.
Lemma kind_subst_combine : forall S S1 S2 k,
(forall T, typ_subst S1 (typ_subst S2 T) = typ_subst S T) ->
kind_subst S1 (kind_subst S2 k) = kind_subst S k.
Proof.
intros.
destruct k as [[kc kv kr kh]|].
simpl; apply* kind_pi; simpl.
clear kv kh.
induction kr. auto.
simpl. rewrite IHkr. rewrite* H.
auto.
Qed.
Lemma binds_orig_remove_env : forall (A:Set) v x (k:A) E,
ok E -> binds x k (remove_env E v) -> binds x k E.
Proof.
unfold binds.
induction E; simpl; intros. auto.
destruct a.
inversions H.
destruct (v == v0); simpl in H0.
subst.
destruct* (x == v0).
subst. elim (binds_fresh H0 H5).
destruct* (x == v0).
Qed.
Lemma get_kind_subst : forall S x K,
get_kind x (map (kind_subst S) K) = kind_subst S (get_kind x K).
Proof.
unfold get_kind; intros.
case_eq (get x K); introv R1.
rewrite* (binds_map (kind_subst S) R1).
rewrite* (map_get_none (kind_subst S) _ _ R1).
Qed.
Lemma unify_kind_rel_keep : forall kr kr' uniq pairs k' l,
unify_kind_rel kr kr' uniq pairs = (k', l) ->
incl kr' k' /\ incl pairs l.
Proof.
induction kr; simpl; intros. inversions H. split2*.
destruct a.
case_rewrite R (uniq a).
case_rewrite R1 (assoc Cstr.eq_dec a kr'); destruct* (IHkr _ _ _ _ _ H).
destruct* (IHkr _ _ _ _ _ H).
Qed.
Lemma unify_kind_rel_incl : forall kr pairs uniq S kr0 kr' pairs',
unify_kind_rel kr0 kr' uniq pairs' = (kr, pairs) ->
unifies S pairs ->
incl (map_snd (typ_subst S) kr0) (map_snd (typ_subst S) kr).
Proof.
induction kr0; intros; intros T HT. elim HT.
destruct T.
destruct a.
simpl in *.
case_rewrite R (uniq a);
try case_rewrite R1 (assoc Cstr.eq_dec a kr'); simpl in HT; destruct HT;
try solve [apply* (IHkr0 _ _ H)]; inversions H1; clear H1;
destruct (unify_kind_rel_keep _ _ _ _ H).
puts (H1 _ (assoc_sound _ _ _ R1)); clear H1.
assert (In (t0,t1) pairs) by auto.
use (H0 _ _ H1).
rewrite* H4.
apply* in_map_snd.
apply* in_map_snd.
Qed.
Lemma unify_kinds_sound : forall k k0 k1 l S,
unify_kinds k k0 = Some (k1, l) ->
unifies S l ->
kind_entails (kind_subst S k1) (kind_subst S k) /\
kind_entails (kind_subst S k1) (kind_subst S k0).
Proof.
unfold unify_kinds, kind_entails.
intros.
destruct k as [[kc kv kr kh]|]; destruct k0 as [[kc0 kv0 kr0 kh0]|]; simpl.
destruct (Cstr.valid_dec (Cstr.lub kc kc0)); try discriminate.
case_eq (unify_kind_rel (kr ++ kr0) nil (Cstr.unique (Cstr.lub kc kc0))
nil); intros l0 l1 R1.
inversions H; clear H.
rewrite R1 in *.
use (unify_kind_rel_incl _ _ _ _ R1 H0).
destruct (proj2 (Cstr.entails_lub kc kc0 _) (Cstr.entails_refl _)).
split; split2*; simpl; intros;
rewrite R1; apply H; unfold map_snd; rewrite* map_app.
split2*.
inversions H; clear H.
simpl. apply entails_refl.
split2*.
inversions H; clear H.
simpl. apply entails_refl.
auto.
Qed.
Lemma map_remove_env : forall (A:Set) x f (E:Env.env A),
map f (remove_env E x) = remove_env (map f E) x.
Proof.
induction E; simpl in *. auto.
destruct a; simpl.
destruct (x == v); simpl*.
rewrite* IHE.
Qed.
Lemma map_map_env : forall (A:Set) f f1 f2 (E:Env.env A),
(forall x, f x = f1 (f2 x)) -> map f E = map f1 (map f2 E).
Proof.
intros; induction E; simpl. auto.
destruct a; simpl.
rewrite H.
rewrite* IHE.
Qed.
Lemma fv_in_remove_env : forall (A:Set) (fv:A->vars) x E,
fv_in fv (remove_env E x) << fv_in fv E.
Proof.
induction E; simpl; intros. auto.
destruct a. destruct* (x == v); simpl*.
Qed.
Lemma unify_kinds_subst : forall k1 k2 k3 l S,
unify_kinds k1 k2 = Some (k3, l) ->
unify_kinds (kind_subst S k1) (kind_subst S k2) =
Some (kind_subst S k3,
List.map (fun T => (typ_subst S (fst T), typ_subst S (snd T))) l).
Proof.
intros.
destruct k1 as [[kc1 kv1 kr1 kh1]|]; destruct k2 as [[kc2 kv2 kr2 kh2]|];
simpl in *; try solve [inversions* H].
destruct (Cstr.valid_dec (Cstr.lub kc1 kc2)); try discriminate.
inversions H; clear H.
unfold map_snd; rewrite <- map_app.
fold (map_snd (typ_subst S) (kr1++kr2)).
simpl.
refine (f_equal (@Some _) _).
set (kr:=@nil(Cstr.attr*typ)).
set (pairs:=@nil(typ*typ)).
assert (kr = map_snd (typ_subst S) kr) by reflexivity.
assert (pairs =
List.map (fun T => (typ_subst S (fst T), typ_subst S (snd T))) pairs)
by reflexivity.
clear kh1 kh2.
apply injective_projections; simpl; try apply kind_pi; simpl*;
pattern kr at 1; rewrite H;
pattern pairs at 1; rewrite H0; clear H H0;
gen kr pairs; induction (kr1++kr2); intros; simpl*; destruct a;
simpl; destruct (Cstr.unique (Cstr.lub kc1 kc2) a);
try rewrite* <- IHl;
case_eq (assoc Cstr.eq_dec a kr); intros; rewrite <- IHl;
try rewrite* (assoc_map _ (typ_subst S) _ _ H).
Qed.
Lemma well_subst_unify : forall k1 l v v0 S K h pairs,
unify h (l ++ pairs) (remove_env (remove_env K v) v0 & v0 ~ k1)
(compose (v ~ typ_fvar v0) S) = Some (K', S') ->
unify_kinds (get_kind v K) (get_kind v0 K) = Some (k1, l) ->
is_subst (compose (v ~ typ_fvar v0) S) ->
v # S ->
well_subst (remove_env (remove_env K v) v0 & v0 ~ k1)
(map (kind_subst S') K') S' ->
well_subst K (map (kind_subst S') K') S'.
Proof.
intros until 1; intros HU HS1 Hv WS x; intros.
unfold well_subst in WS.
poses Hext (typ_subst_extend _ _ _ HS1 H).
poses Hunif (unify_types _ _ _ H HS1).
assert (Hunif': unifies S' l) by (intro; intros; auto).
clear HS1 H.
destruct (x == v0); subst.
destruct* (unify_kinds_sound _ _ HU Hunif') as [_ Wk].
rewrite* <- (binds_get_kind H0).
destruct (x == v); subst.
assert (well_kinded (map (kind_subst S') K') (kind_subst S' k1)
(typ_subst S' (typ_fvar v))).
rewrite <- Hext.
rewrite* typ_subst_compose.
rewrite (typ_subst_fresh S); simpl*.
destruct* (v == v).
destruct* (unify_kinds_sound _ _ HU Hunif') as [Wk _].
rewrite* <- (binds_get_kind H0).
assert (x # v0 ~ k1) by simpl*.
use (binds_concat_fresh _ (binds_remove_env (binds_remove_env H0 n0) n) H).
Qed.
Lemma unify_kinds_ok : forall h pairs K S,
unify h pairs K S = Some (K',S') -> is_subst S ->
ok K -> disjoint (dom S) (dom K) ->
ok K' /\ disjoint (dom S') (dom K') /\
well_subst K (map (kind_subst S') K') S'.
Proof.
introv H H0.
apply* (unify_ind (fun K S pairs =>
ok K -> disjoint (dom S) (dom K) ->
ok K' /\ disjoint (dom S') (dom K') /\
well_subst K (map (kind_subst S') K') S'));
clear H H0 h pairs K S.
intuition.
intro; intros.
rewrite* typ_subst_fresh.
destruct* k.
use (binds_map (kind_subst S') H2).
apply* wk_kind.
intros until 1.
intros R1 R2 Hs HS1 n R3 IHh HK Dis.
subst S1 K1.
destruct* IHh.
intuition.
clear -R3 H3.
intro; intros.
destruct (Z == v).
subst.
rewrite (binds_get_kind H) in R3. subst*.
use (H3 _ _ (binds_remove_env H n)).
intros until K1.
intros R3 H R1 R2 HS HS1 n IHh HK Dis.
subst S1 K1.
destruct* IHh.
constructor. repeat apply ok_remove_env. auto.
rewrite* dom_remove_env.
simpl.
repeat rewrite* dom_remove_env.
unfold compose.
rewrite dom_concat. rewrite dom_map. simpl.
use (typ_subst_res_fresh' _ HS R2).
intuition.
subst; apply* well_subst_unify.
apply* typ_subst_res_fresh'.
Qed.
End Soundness.
Lemma typ_subst_map_idem : forall S,
is_subst S -> ok S -> map (typ_subst S) S = S.
Proof.
intros.
remember S as S0.
pattern S0 at 1.
rewrite HeqS0.
assert (env_prop (fun T => typ_subst S T = T) S0).
intro; intros.
rewrite <- HeqS0.
rewrite <- (binds_typ_subst (in_ok_binds _ _ H1 H0)).
apply* typ_subst_idem.
clear HeqS0 H.
induction S0. auto.
inversions H0.
simpl. rewrite (H1 x a0).
rewrite* IHS0.
intro; intros.
apply (H1 x0 a).
simpl.
destruct* (x0 == x).
simpl*.
Qed.
Lemma typ_subst_prebind : forall v T S T1,
typ_subst S T = typ_subst S (typ_fvar v) ->
typ_subst S (typ_subst (v~T) T1) = typ_subst S T1.
Proof.
induction T1; intros.
simpl*.
simpl. destruct (v0 == v).
subst*.
reflexivity.
simpl.
rewrite* IHT1_1. rewrite* IHT1_2.
Qed.
Section Mgu.
Variables (K':kenv) (S':subs) (HS' : is_subst S').
Definition mgu_spec K S K0 S0 pairs :=
ok K0 ->
extends S' S0 ->
unifies S' pairs ->
well_subst K0 K' S' ->
extends S' S /\ well_subst K K' S'.
Lemma get_remove_env : forall (A:Set) v (E:Env.env A),
ok E -> get v (remove_env E v) = None.
Proof.
induction E; simpl; intros. auto.
destruct a. destruct* (v == v0).
subst v0; inversions H.
case_eq (get v E); intros. elim (binds_fresh H0 H4). auto.
simpl. destruct* (v == v0). inversions* H.
Qed.
Lemma kind_subst_compose : forall S1 S2 k,
kind_subst (compose S1 S2) k = kind_subst S1 (kind_subst S2 k).
Proof.
intros; symmetry; apply kind_subst_combine.
intro; symmetry; apply* typ_subst_compose.
Qed.
Lemma unify_mgu_nv : forall K0 S0 pairs K S h t t0 v T,
let S1 := compose (v ~ T) S0 in
let K1 := remove_env K0 v in