-
Notifications
You must be signed in to change notification settings - Fork 2
/
ML_SP_Unify_wf.v
1828 lines (1647 loc) · 52.2 KB
/
ML_SP_Unify_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 unification for mini-ML with structural polymorphism *
* Jacques Garrigue, July 2008 *
***************************************************************************)
Require Import Arith List Metatheory Cardinal.
Require Import ML_SP_Definitions 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.
(* 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.
Hint Resolve typ_subst_idem : core.
(** Various properties of substitutions *)
Lemma extends_trans : forall S1 S2 S3,
extends S1 S2 -> extends S2 S3 -> extends S1 S3.
Proof.
intros; intro T.
rewrite <- H. rewrite <- (H T).
rewrite* H0.
Qed.
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).
rewrite (proj2 H2) 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.
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 v t).
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 neq_notin_fv : forall v v0,
v <> v0 -> v \notin (typ_fv (typ_fvar v0)).
Proof. simpl*. Qed.
Hint Resolve neq_notin_fv : core.
Lemma is_subst_compose_var : forall S x y,
is_subst S -> x <> y -> disjoint (dom S) {{y}} ->
is_subst (compose (x ~ typ_fvar y) S).
Proof.
intros. auto*.
Qed.
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; simpl*.
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 by auto.
congruence.
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 kh; induction kr; simpl*.
rewrite IHkr. rewrite* 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.
apply* kind_subst_combine.
Qed.
Lemma env_prop_map_idem : forall f (S:subs),
env_prop (fun T => f T = T) S -> map f S = S.
Proof.
intros.
induction S; simpl*.
destruct a.
rewrite* (H v t).
rewrite* IHS.
intro; auto*.
Qed.
Lemma typ_subst_map_idem : forall S,
is_subst S -> ok S -> map (typ_subst S) S = S.
Proof.
intros.
apply env_prop_map_idem.
intro; intros.
rewrite* <- (@binds_typ_subst x a S).
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*.
destruct* (v0 == v).
subst*.
rewrite* IHT1_1. rewrite* IHT1_2.
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.
(* get a kind safely from a kenv *)
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.
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.
(* Decidability of membership *)
Definition decidable (A : Type) (P : A -> Prop) :=
forall x, {P x}+{~P x}.
Definition in_dec L : decidable (fun x => x \in L).
intros x.
case_eq (S.mem x L); intros. left*. exact (S.mem_2 H).
right*.
Qed.
Section RemoveEnv.
(* Removing an element from an env *)
Variable A : Set.
Fixpoint remove_env (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 map_remove_env : forall 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 fv_in_remove_env : forall (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 notin_remove_env : forall x v (E:Env.env A),
x # E -> x # remove_env E v.
Proof.
induction E; simpl; intros. auto.
destruct a. destruct* (v == v0).
simpl. apply* notin_union.
Qed.
Lemma ok_remove_env : forall v (E:Env.env A),
ok E -> ok (remove_env E v).
Proof.
induction 1; simpl. apply ok_empty.
destruct* (v == x).
apply* ok_cons.
apply* notin_remove_env.
Qed.
Lemma dom_remove_env : forall v (K:Env.env A),
ok K -> dom (remove_env K v) = S.remove v (dom K).
Proof.
induction 1; simpl; intros.
apply eq_ext; intros; split; intro. elim (in_empty H).
use (S.remove_3 H).
destruct (v == x).
subst.
rewrite remove_union.
rewrite remove_single. rewrite* remove_notin. rewrite* union_empty_l.
simpl.
rewrite remove_union.
rewrite IHok.
rewrite* (@remove_notin v {{x}}).
Qed.
Lemma binds_remove_env : forall 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); simpl; subst. elim H0; auto.
destruct* (v0 == v0).
auto.
destruct* (x == v0).
Qed.
Lemma binds_orig_remove_env : forall 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_remove_env : forall v (E:Env.env A),
ok E -> get v (remove_env E v) = None.
Proof.
induction 1; simpl; intros. auto.
destruct (v == x).
subst. apply* get_notin_dom.
simpl. destruct* (v == x).
Qed.
End RemoveEnv.
Hint Resolve ok_remove_env notin_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.
(* ====================================================================== *)
(** Start of the algorithm *)
(* Unification of kinds *)
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.
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.
Inductive unify_kinds_spec
: kind -> kind -> option (kind*list(typ*typ)) -> Prop :=
| UKnone1 : forall k, unify_kinds_spec None k (Some (k,nil))
| UKnone2 : forall k, unify_kinds_spec k None (Some (k,nil))
| UKok : forall k1 k2 k pairs,
kind_cstr k = Cstr.lub (kind_cstr k1) (kind_cstr k2) ->
Cstr.valid (kind_cstr k) ->
unify_kind_rel (kind_rel k1 ++ kind_rel k2) nil
(Cstr.unique (kind_cstr k)) nil = (kind_rel k, pairs) ->
unify_kinds_spec (Some k1) (Some k2) (Some (Some k, pairs))
| UKfail : forall k1 k2,
~Cstr.valid (Cstr.lub (kind_cstr k1) (kind_cstr k2)) ->
unify_kinds_spec (Some k1) (Some k2) None.
Lemma unify_kinds_spec_ok : forall k1 k2,
unify_kinds_spec k1 k2 (unify_kinds k1 k2).
Proof.
intros [[kc1 kv1 kr1 kh1]|] k2.
destruct k2 as [[kc2 kv2 kr2 kh2]|].
simpl.
destruct (Cstr.valid_dec (Cstr.lub kc1 kc2)).
apply* UKok.
apply surjective_pairing.
apply* UKfail.
apply UKnone2.
apply UKnone1.
Qed.
Inductive unif_res : Set :=
| Uok : list (typ*typ) -> kenv -> subs -> unif_res
| Ufail : unif_res.
Definition unify_vars K S x y :=
match unify_kinds (get_kind x K) (get_kind y K) with
| Some (k, pairs) => Uok pairs (remove_env (remove_env K x) y & y ~ k)
(compose (x ~ typ_fvar y) S)
| None => Ufail
end.
Inductive unify_vars_spec K S x y : unif_res -> Prop :=
| UVok : forall k pairs,
unify_kinds_spec (get_kind x K) (get_kind y K) (Some (k, pairs)) ->
unify_vars_spec K S x y
(Uok pairs (remove_env (remove_env K x) y & y ~ k)
(compose (x ~ typ_fvar y) S))
| UVfail :
unify_kinds_spec (get_kind x K) (get_kind y K) None ->
unify_vars_spec K S x y Ufail.
Definition unify_nv K S x T :=
if S.mem x (typ_fv T) then Ufail else
match get_kind x K with
| Some _ => Ufail
| None => Uok nil (remove_env K x) (compose (x ~ T) S)
end.
Definition unify1 (T1 T2:typ) (K:kenv) (S:subs) : unif_res :=
match typ_subst S T1, typ_subst S T2 with
| typ_bvar n, typ_bvar m =>
if n === m then Uok nil K S else Ufail
| typ_fvar x, typ_fvar y =>
if x == y then Uok nil K S else unify_vars K S x y
| typ_fvar x, T =>
unify_nv K S x T
| T, typ_fvar x =>
unify_nv K S x T
| typ_arrow T11 T12, typ_arrow T21 T22 =>
Uok ((T11,T21)::(T12,T22)::nil) K S
| _, _ =>
Ufail
end.
Inductive unify_spec : list (typ*typ) -> kenv -> subs ->
option (kenv*subs) -> Prop :=
| USnil : forall K S (HS:is_subst S) (HK:ok K),
unify_spec nil K S (Some (K,S))
| USok : forall T1 T2 pairs K S pairs' K' S' r (HS:is_subst S) (HK:ok K),
unify1 T1 T2 K S = Uok pairs' K' S' ->
unify_spec (pairs' ++ pairs) K' S' r ->
unify_spec ((T1,T2)::pairs) K S r
| USfail : forall T1 T2 pairs K S (HS:is_subst S) (HK:ok K),
unify1 T1 T2 K S = Ufail ->
unify_spec ((T1,T2)::pairs) K S None.
(* Termination measure *)
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.
Definition pair_subst S T := (typ_subst S (fst T), typ_subst S (snd T)).
(* pairs_size : total size of types (after substitution) *)
Fixpoint typ_size (T : typ) : nat :=
match T with
| typ_arrow T1 T2 => S (typ_size T1 + typ_size T2)
| _ => 1
end.
Definition pair_size (p:typ*typ) :=
1 + typ_size (fst p) + typ_size (snd p).
Definition pairs_size S pairs :=
accum pair_size plus 0 (List.map (pair_subst S) pairs).
(* size_pairs : total numbers of variables (after substitution) *)
Definition all_fv S pairs :=
accum (fun p:typ*typ => typ_fv (fst p) \u typ_fv (snd p))
S.union {} (List.map (pair_subst S) pairs).
Definition really_all_fv S K pairs :=
fv_in kind_fv (map (kind_subst S) K) \u all_fv S pairs.
Definition size_pairs S K pairs :=
1+S.cardinal (really_all_fv S K pairs).
(* Lemmas for termination *)
Lemma typ_fv_decr : forall v T S T1,
v # S -> disjoint (typ_fv T) ({{v}} \u dom S) ->
typ_fv (typ_subst (compose (v ~ T) S) T1) <<
S.remove v (typ_fv T \u typ_fv (typ_subst S T1)).
Proof.
intros.
rewrite* typ_subst_compose.
induction (typ_subst S T1); simpl in *; disjoint_solve.
destruct* (v0 == v).
Qed.
Lemma kind_fv_decr : forall v T S k,
v # S -> disjoint (typ_fv T) ({{v}} \u dom S) ->
kind_fv (kind_subst (compose (v ~ T) S) k) <<
S.remove v (typ_fv T \u kind_fv (kind_subst S k)).
Proof.
intros.
unfold kind_fv.
destruct k as [[kc kv kr kh]|]; simpl*.
clear kc kv kh.
induction kr; simpl*.
sets_solve.
use (typ_fv_decr _ _ _ H H0 H1).
Qed.
Lemma fv_in_decr : forall (A:Set) v T S (E:Env.env A) fv (sub:subs -> A -> A),
v # S -> disjoint (typ_fv T) ({{v}} \u dom S) ->
(forall a,
fv (sub (compose (v ~ T) S) a) << S.remove v (typ_fv T \u fv (sub S a))) ->
fv_in fv (map (sub (compose (v ~ T) S)) E) <<
S.remove v (typ_fv T \u fv_in fv (map (sub S) E)).
Proof.
intros.
induction E; simpl*; intros.
destruct a.
simpl.
use (H1 a).
Qed.
Lemma all_fv_decr : forall v T S pairs,
v # S -> disjoint (typ_fv T) ({{v}} \u dom S) ->
all_fv (compose (v ~ T) S) pairs <<
S.remove v (all_fv S ((typ_fvar v, T) :: pairs)).
Proof.
unfold all_fv.
induction pairs; intros; simpl*.
rewrite* get_notin_dom.
sets_solve.
puts (typ_fv_decr _ _ _ H H0 H2).
rewrite* (@typ_subst_fresh S T).
puts (typ_fv_decr _ _ _ H H0 H2).
rewrite* (@typ_subst_fresh S T).
use (IHpairs H H0 _ H1).
simpl in H2.
rewrite get_notin_dom in H2; auto.
Qed.
Lemma really_all_fv_decr : forall S K pairs v T,
v # S -> disjoint (typ_fv T) ({{v}} \u dom S) -> ok K ->
really_all_fv (compose (v ~ T) S) K pairs <<
S.remove v (really_all_fv S K ((typ_fvar v, T) :: pairs)).
Proof.
intros until T. intros Hv Dis HK.
unfold really_all_fv.
sets_solve.
unfold all_fv; simpl. rewrite* get_notin_dom.
repeat rewrite union_assoc.
rewrite* typ_subst_fresh.
forward~ (fv_in_decr _ _ K kind_fv kind_subst Hv Dis); intros.
apply* kind_fv_decr.
apply H.
auto*.
use (all_fv_decr _ _ _ Hv Dis H).
Qed.
Lemma cardinal_decr : forall v T S K pairs,
v # S -> disjoint (typ_fv T) ({{v}} \u dom S) -> ok K ->
S.cardinal (really_all_fv (compose (v ~ T) S) (remove_env K v) pairs) <
S.cardinal (really_all_fv S K ((typ_fvar v, T) :: pairs)).
Proof.
intros.
use (really_all_fv_decr (pairs:=pairs) _ _ H H0 H1).
use (le_lt_n_Sm _ _ (cardinal_subset H2)).
rewrite cardinal_remove in H3.
eapply le_lt_trans; try apply H3.
apply cardinal_subset.
unfold really_all_fv. rewrite map_remove_env.
sets_solve.
apply S.union_2. refine (fv_in_remove_env _ _ _ H4).
auto.
unfold really_all_fv, all_fv; simpl.
rewrite* get_notin_dom.
Qed.
Lemma get_kind_fv_in : forall S v K,
kind_fv (kind_subst S (get_kind v K)) << fv_in kind_fv (map (kind_subst S) K).
Proof.
induction K; simpl. apply subset_refl.
unfold get_kind; simpl.
destruct a. destruct (v == v0).
simpl*.
fold (get_kind v K).
simpl*.
Qed.
Lemma in_typ_fv : forall t l,
In t l -> typ_fv t << typ_fv_list l.
Proof.
induction l; simpl; intros H x Hx. elim H.
destruct* H.
subst; simpl*.
Qed.
Lemma unify_kinds_fv : forall k k0 k1 l S,
unify_kinds k k0 = Some (k1, l) ->
kind_fv (kind_subst S k1) \u all_fv S l <<
kind_fv (kind_subst S k) \u kind_fv (kind_subst S k0).
Proof.
unfold unify_kinds; intros.
destruct k as [[kc kv kr kh]|].
destruct k0 as [[kc0 kv0 kr0 kh0]|].
destruct (Cstr.valid_dec (Cstr.lub kc kc0)); try discriminate.
inversions H; clear H.
simpl.
unfold kind_fv; simpl.
repeat rewrite list_snd_map_snd.
rewrite <- fv_list_map.
unfold list_snd; rewrite <- map_app.
set (pairs := nil(A:=typ*typ)).
set (kr' := nil(A:=Cstr.attr*typ)).
intros x Hx.
rewrite <- union_empty_r.
replace {} with (typ_fv_list (List.map (typ_subst S) (list_snd kr')))
by reflexivity.
rewrite <- union_empty_r.
replace {} with (all_fv S pairs) by reflexivity.
clearbody pairs kr'.
rewrite <- map_app.
gen pairs kr'; induction (kr ++ kr0); simpl; intros.
rewrite <- union_assoc; auto with sets.
destruct a; simpl in *.
case_rewrite R (Cstr.unique (Cstr.lub kc kc0) a).
case_rewrite R1 (assoc Cstr.eq_dec a kr');
poses Hsub (IHl _ _ Hx); clear -Hsub R1.
unfold all_fv in *; simpl in *.
sets_solve.
puts (assoc_sound _ _ _ R1).
puts (in_map_snd (typ_subst S) _ _ _ H0).
rewrite <- combine_fst_snd in H1.
puts (in_combine_r _ _ _ _ H1).
rewrite list_snd_map_snd in H2.
use (in_typ_fv _ _ H2 H).
simpl in Hsub. auto.
poses Hsub (IHl _ _ Hx); clear -Hsub.
simpl in Hsub; auto.
inversions H.
unfold kind_fv, all_fv; simpl*.
inversions H.
unfold kind_fv, all_fv; simpl*.
Qed.
Lemma all_fv_app : forall S l1 l2,
all_fv S (l1 ++ l2) = all_fv S l1 \u all_fv S l2.
Proof.
intros.
unfold all_fv.
induction l1; simpl. rewrite* union_empty_l.
rewrite IHl1.
repeat rewrite union_assoc. auto.
Qed.
Lemma size_pairs_decr : forall v T K S pairs,
v # S -> ok K ->
disjoint (typ_fv T) ({{v}} \u dom S) ->
size_pairs (compose (v ~ T) S) (remove_env K v) pairs <
size_pairs S K ((typ_fvar v,T)::pairs).
Proof.
intros.
unfold size_pairs.
apply lt_n_S.
apply* cardinal_decr.
Qed.
Lemma size_pairs_comm : forall S K T1 T2 pairs,
size_pairs S K ((T1,T2)::pairs) = size_pairs S K ((T2,T1)::pairs).
Proof.
intros; unfold size_pairs, really_all_fv, all_fv; simpl.
rewrite* (union_comm (typ_fv (typ_subst S T1))).
Qed.
Lemma size_pairs_decr_vars : forall S0 K0 pairs v v0 x0 l,
is_subst S0 -> ok K0 ->
v # S0 ->
v0 # S0 ->
v <> v0 ->
unify_kinds (get_kind v K0) (get_kind v0 K0) = Some (x0, l) ->
size_pairs (compose (v ~ typ_fvar v0) S0)
(remove_env (remove_env K0 v) v0 & v0 ~ x0) (l ++ pairs)
< size_pairs S0 K0 ((typ_fvar v, typ_fvar v0) :: pairs).
Proof.
intros.
unfold size_pairs.
assert (v \in really_all_fv S0 K0 ((typ_fvar v,typ_fvar v0)::pairs)).
unfold really_all_fv, all_fv.
simpl. rewrite* get_notin_dom. simpl*.
rewrite <- (cardinal_remove H5). clear H5.
simpl.
set (S := compose (v ~ typ_fvar v0) S0).
poses Hfv (unify_kinds_fv _ _ S H4).
apply le_lt_n_Sm. apply le_n_S.
apply cardinal_subset.
sets_solve.
apply* really_all_fv_decr.
fold S.
unfold really_all_fv in *.
simpl in *.
rewrite all_fv_app in Hy.
do 2 rewrite map_remove_env in Hy.
sets_solve; try use (get_kind_fv_in _ _ _ H5).
apply S.union_2.
refine (fv_in_remove_env _ v _ _); auto.
refine (fv_in_remove_env _ v0 _ _); auto.
Qed.
(* Well-foundedness of pair ordering *)
Section Wf2.
Variables A B:Set.
Variable ltA : A -> A -> Prop.
Variable ltB : B -> B -> Prop.
Definition ltAB (p1 p2:A*B) :=
ltA (fst p1) (fst p2) \/ fst p1 = fst p2 /\ ltB (snd p1) (snd p2).
Hypothesis ltA_wf : well_founded ltA.
Hypothesis ltB_wf : well_founded ltB.
Theorem ltAB_wf : well_founded ltAB.
Proof.
intros [a b].
puts (ltA_wf a).
gen b; induction H; intros.
puts (ltB_wf b).
induction H1.
apply Acc_intro; intros.
destruct y; destruct H3; simpl in H3.
apply* H0.
destruct H3.
subst.
apply* H2.
Defined.
End Wf2.
Definition lt2 := ltAB lt lt.
Lemma lt_wf : forall n, Acc lt n.
Proof.
induction n; apply Acc_intro; intros.
elim (le_Sn_O _ H).
unfold lt in H.
destruct (Lt.le_lt_or_eq _ _ (Le.le_S_n _ _ H)).
apply (Acc_inv IHn _ H0).
subst*.
Defined.
Definition lt2_wf := ltAB_wf lt_wf lt_wf.
Definition Acc_lt2_lt : forall a b c d,
c < a -> Acc lt2 (a,b) -> Acc lt2 (c,d).
intros.
apply (Acc_inv H0).
left*.
Defined.
Definition Acc_lt2_le : forall a b c d,
c <= a -> d < b -> Acc lt2 (a,b) -> Acc lt2 (c,d).
intros.
apply (Acc_inv H1).
destruct H; [right*|left*]; simpl*.
auto with arith.
Defined.
Lemma normalize_Acc_intro : forall (a b:nat) h,
Acc_intro (a,b) h = Acc_intro (a,b) (Acc_inv (lt2_wf (a,b))).
Proof.
intros; apply f_equal. apply ProofIrrelevance.proof_irrelevance.
Qed.
(* The real termination measure *)
Definition size_pairs2 S K pairs :=
(size_pairs S K pairs, pairs_size S pairs).
Lemma lt2_le : forall a b c d,
a <= c -> b < d -> lt2 (a,b) (c,d).
Proof.
intros; destruct H. right*; simpl*. left*; simpl; omega.
Qed.
Lemma size_pairs2_tl : forall S K pairs p,
lt2 (size_pairs2 S K pairs) (size_pairs2 S K (p :: pairs)).
Proof.
intros.
apply lt2_le.
unfold size_pairs2, size_pairs, really_all_fv, all_fv; simpl.
apply le_n_S. apply cardinal_subset. auto.
unfold pairs_size; simpl. omega.
Qed.
Hint Resolve size_pairs2_tl : core.
Lemma size_pairs2_comm : forall S K T1 T2 pairs,
size_pairs2 S K ((T1,T2)::pairs) = size_pairs2 S K ((T2,T1)::pairs).
Proof.
intros. unfold size_pairs2. rewrite size_pairs_comm.
unfold pairs_size; simpl. rewrite* (plus_comm (typ_size (typ_subst S T1))).
Qed.
Section Unify1.
Variables (pairs pairs':list (typ*typ)) (K K':kenv) (S S':subs).
Hypothesis HK : ok K.
Hypothesis HS : is_subst S.
Let size T1 T2 := size_pairs2 S K ((T1,T2)::pairs).
Let size' := size_pairs2 S' K' (pairs' ++ pairs).
Lemma size_pairs2_nv : forall v T T1 T2,
typ_subst S T1 = typ_fvar v ->
typ_subst S T2 = T ->
v \notin typ_fv T ->
lt2 (size_pairs2 (compose (v ~ T) S) (remove_env K v) pairs) (size T1 T2).
Proof.
introv R1 R2 R3.
left*. simpl.
unfold size_pairs at 2, size_pairs, really_all_fv, all_fv; simpl.
rewrite* <- (typ_subst_idem T1 HS).
rewrite* <- (typ_subst_idem T2 HS).
rewrite R1; rewrite R2.
apply* (@size_pairs_decr v T K S pairs).
apply* typ_subst_res_fresh'.
use (typ_subst_res_fresh _ HS R2).
Qed.
Lemma size_pairs2_arrow : forall t t0 t1 t2 T1 T2,
typ_subst S T1 = typ_arrow t1 t2 ->
typ_subst S T2 = typ_arrow t t0 ->
lt2 (size_pairs2 S K ((t1, t) :: (t2, t0) :: pairs)) (size T1 T2).
Proof.
introv R1 R2.
unfold size, size_pairs2, size_pairs, really_all_fv, all_fv, pairs_size;
simpl.
rewrite <- (typ_subst_idem T1 HS).
rewrite <- (typ_subst_idem T2 HS).
rewrite R1; rewrite R2.
right*; simpl; split.
repeat rewrite union_assoc.
rewrite <- (union_assoc _ (typ_fv (typ_subst S t))).
rewrite <- (union_comm _ (typ_fv (typ_subst S t))).
rewrite* union_assoc.
omega.
Qed.
Hint Resolve size_pairs2_tl size_pairs2_nv size_pairs2_arrow : core.
Lemma unify1_decr_nv : forall v T T1 T2,
unify_nv K S v T = Uok pairs' K' S' ->
typ_subst S T1 = typ_fvar v ->
typ_subst S T2 = T ->
lt2 size' (size T1 T2).
Proof.
unfold unify_nv, size', size.