-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDsubsup.v
1767 lines (1541 loc) · 50.9 KB
/
Dsubsup.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
(*
DSubSup (D<:>)
T ::= Bot | Top | p.Type | { Type: S..U } | (z: T) -> T^z
t ::= p | t t
p ::= x | v
v ::= { Type = T } | lambda x:T.t
*)
(* based on *)
(***************************************************************************
* Preservation and Progress for System-F with Subtyping - Definitions *
* Brian Aydemir & Arthur Charguéraud, March 2007 *
***************************************************************************)
Set Implicit Arguments.
Require Import LibLN.
Implicit Types x : var.
(* ********************************************************************** *)
(** * Description of the Language *)
(** Representation of pre-types *)
Inductive typ : Set :=
| typ_bot : typ
| typ_top : typ
| typ_sel : trm -> typ
| typ_mem : typ -> typ -> typ
| typ_all : typ -> typ -> typ
(** Representation of pre-terms *)
with trm : Set :=
| trm_bvar : nat -> trm
| trm_fvar : var -> trm
| trm_abs : typ -> trm -> trm
| trm_mem : typ -> trm
| trm_app : trm -> trm -> trm.
Fixpoint open_t_rec (k : nat) (f : trm) (T : typ) {struct T} : typ :=
match T with
| typ_bot => typ_bot
| typ_top => typ_top
| typ_sel t => typ_sel (open_e_rec k f t)
| typ_mem T1 T2 => typ_mem (open_t_rec k f T1) (open_t_rec k f T2)
| typ_all T1 T2 => typ_all (open_t_rec k f T1) (open_t_rec (S k) f T2)
end
(** Opening up a term binder occuring in a term *)
with open_e_rec (k : nat) (f : trm) (e : trm) {struct e} : trm :=
match e with
| trm_bvar i => If k = i then f else (trm_bvar i)
| trm_fvar x => trm_fvar x
| trm_abs V e1 => trm_abs (open_t_rec k f V) (open_e_rec (S k) f e1)
| trm_mem T => trm_mem (open_t_rec k f T)
| trm_app e1 e2 => trm_app (open_e_rec k f e1) (open_e_rec k f e2)
end.
Definition open_t T f := open_t_rec 0 f T.
Definition open_e t u := open_e_rec 0 u t.
(** Notation for opening up binders with variables *)
Notation "t 'open_t_var' x" := (open_t t (trm_fvar x)) (at level 67).
Notation "t 'open_e_var' x" := (open_e t (trm_fvar x)) (at level 67).
(** Types as locally closed pre-types *)
Inductive type : typ -> Prop :=
| type_bot :
type typ_bot
| type_top :
type typ_top
| type_sel : forall e1,
term e1 ->
type (typ_sel e1)
| type_mem : forall T1 T2,
type T1 ->
type T2 ->
type (typ_mem T1 T2)
| type_all : forall L T1 T2,
type T1 ->
(forall x, x \notin L -> type (T2 open_t_var x)) ->
type (typ_all T1 T2)
(** Terms as locally closed pre-terms *)
with term : trm -> Prop :=
| term_var : forall x,
term (trm_fvar x)
| term_abs : forall L V e1,
type V ->
(forall x, x \notin L -> term (e1 open_e_var x)) ->
term (trm_abs V e1)
| term_mem : forall T1,
type T1 ->
term (trm_mem T1)
| term_app : forall e1 e2,
term e1 ->
term e2 ->
term (trm_app e1 e2).
(** Values *)
Inductive value : trm -> Prop :=
| value_abs : forall V e1, term (trm_abs V e1) ->
value (trm_abs V e1)
| value_mem : forall V, term (trm_mem V) ->
value (trm_mem V).
(** Environment is an associative list of bindings. *)
Definition env := LibEnv.env typ.
(** Well-formedness of a pre-type T in an environment E:
all the type variables of T must be bound via a
subtyping relation in E. This predicates implies
that T is a type *)
Inductive wft : env -> typ -> Prop :=
| wft_bot : forall E,
wft E typ_bot
| wft_top : forall E,
wft E typ_top
| wft_sel : forall E e,
value e \/ (exists x, trm_fvar x = e) ->
wfe E e ->
wft E (typ_sel e)
| wft_mem : forall E T1 T2,
wft E T1 ->
wft E T2 ->
wft E (typ_mem T1 T2)
| wft_all : forall L E T1 T2,
wft E T1 ->
(forall x, x \notin L ->
wft (E & x ~ T1) (T2 open_t_var x)) ->
wft E (typ_all T1 T2)
with wfe : env -> trm -> Prop :=
| wfe_var : forall U E x,
binds x U E ->
wfe E (trm_fvar x)
| wfe_abs : forall L E V e,
wft E V ->
(forall x, x \notin L -> wfe (E & x ~ V) (e open_e_var x)) ->
wfe E (trm_abs V e)
| wfe_mem : forall E T,
wft E T ->
wfe E (trm_mem T)
| wfe_app : forall E e1 e2,
wfe E e1 ->
wfe E e2 ->
wfe E (trm_app e1 e2)
.
(** A environment E is well-formed if it contains no duplicate bindings
and if each type in it is well-formed with respect to the environment
it is pushed on to. *)
Inductive okt : env -> Prop :=
| okt_empty :
okt empty
| okt_push : forall E x T,
okt E -> wft E T -> x # E -> okt (E & x ~ T).
(** Subtyping relation *)
Inductive sub : env -> typ -> typ -> Prop :=
| sub_bot : forall E T,
okt E ->
wft E T ->
sub E typ_bot T
| sub_top : forall E S,
okt E ->
wft E S ->
sub E S typ_top
| sub_refl_sel : forall E t,
okt E ->
wft E (typ_sel t) ->
sub E (typ_sel t) (typ_sel t)
| sub_sel1 : forall E S U t,
has E t (typ_mem S U) ->
sub E (typ_sel t) U
| sub_sel2 : forall E S U t,
has E t (typ_mem S U) ->
sub E S (typ_sel t)
| sub_mem : forall E S1 U1 S2 U2,
sub E S2 S1 -> sub E U1 U2 ->
sub E (typ_mem S1 U1) (typ_mem S2 U2)
| sub_all : forall L E S1 S2 T1 T2,
sub E T1 S1 ->
(forall x, x \notin L ->
sub (E & x ~ T1) (S2 open_t_var x) (T2 open_t_var x)) ->
sub E (typ_all S1 S2) (typ_all T1 T2)
| sub_trans : forall E S T U,
sub E S T ->
sub E T U ->
sub E S U
with has : env -> trm -> typ -> Prop :=
| has_var : forall E x T,
okt E ->
binds x T E ->
has E (trm_fvar x) T
| has_mem : forall E T,
okt E -> wft E T ->
has E (trm_mem T) (typ_mem T T)
| has_abs : forall E V e T,(* dummy case for smooth substitution lemma, see val_typing_has *)
okt E -> wfe E (trm_abs V e) -> wft E (typ_all V T) ->
has E (trm_abs V e) (typ_all V T) (* return typ doesn't matter, as long as it's moot for sel1 and sel2 *)
| has_sub : forall E t T U,
has E t T ->
sub E T U ->
has E t U
.
(** Typing relation *)
Inductive typing : env -> trm -> typ -> Prop :=
| typing_var : forall E x T,
okt E ->
binds x T E ->
typing E (trm_fvar x) T
| typing_abs : forall L E V e1 T1,
(forall x, x \notin L ->
typing (E & x ~ V) (e1 open_e_var x) (T1 open_t_var x)) ->
typing E (trm_abs V e1) (typ_all V T1)
| typing_mem : forall E T1,
okt E ->
wft E T1 ->
typing E (trm_mem T1) (typ_mem T1 T1)
| typing_app : forall T1 E e1 e2 T2,
typing E e1 (typ_all T1 T2) ->
typing E e2 T1 ->
wft E T2 ->
typing E (trm_app e1 e2) T2
| typing_appvar : forall T1 E e1 e2 T2 T2' M,
typing E e1 (typ_all T1 T2) ->
typing E e2 T1 ->
has E e2 M ->
T2' = open_t T2 e2 ->
wft E T2' ->
typing E (trm_app e1 e2) T2'
| typing_sub : forall S E e T,
typing E e S ->
sub E S T ->
typing E e T.
(** One-step reduction *)
Inductive red : trm -> trm -> Prop :=
| red_app_1 : forall e1 e1' e2,
term e2 ->
red e1 e1' ->
red (trm_app e1 e2) (trm_app e1' e2)
| red_app_2 : forall e1 e2 e2',
value e1 ->
red e2 e2' ->
red (trm_app e1 e2) (trm_app e1 e2')
| red_abs : forall V e1 v2,
term (trm_abs V e1) ->
value v2 ->
red (trm_app (trm_abs V e1) v2) (open_e e1 v2).
(** Our goal is to prove preservation and progress *)
Definition preservation := forall e e' T,
typing empty e T ->
red e e' ->
typing empty e' T.
Definition progress := forall e T,
typing empty e T ->
value e
\/ exists e', red e e'.
(***************************************************************************
* Preservation and Progress for System-F with Subtyping - Infrastructure *
***************************************************************************)
(* ********************************************************************** *)
(** * Additional Definitions Used in the Proofs *)
(** Computing free variables in a type *)
Fixpoint fv_t (T : typ) {struct T} : vars :=
match T with
| typ_bot => \{}
| typ_top => \{}
| typ_sel t => fv_e t
| typ_mem T1 T2 => (fv_t T1) \u (fv_t T2)
| typ_all T1 T2 => (fv_t T1) \u (fv_t T2)
end
(** Computing free variables in a term *)
with fv_e (e : trm) {struct e} : vars :=
match e with
| trm_bvar i => \{}
| trm_fvar x => \{x}
| trm_abs V e1 => (fv_t V) \u (fv_e e1)
| trm_mem T => fv_t T
| trm_app e1 e2 => (fv_e e1) \u (fv_e e2)
end.
(** Substitution for free type variables in types. *)
Fixpoint subst_t (z : var) (u : trm) (T : typ) {struct T} : typ :=
match T with
| typ_bot => typ_bot
| typ_top => typ_top
| typ_sel t => typ_sel (subst_e z u t)
| typ_mem T1 T2 => typ_mem (subst_t z u T1) (subst_t z u T2)
| typ_all T1 T2 => typ_all (subst_t z u T1) (subst_t z u T2)
end
(** Substitution for free term variables in terms. *)
with subst_e (z : var) (u : trm) (e : trm) {struct e} : trm :=
match e with
| trm_bvar i => trm_bvar i
| trm_fvar x => If x = z then u else (trm_fvar x)
| trm_abs V e1 => trm_abs (subst_t z u V) (subst_e z u e1)
| trm_mem T1 => trm_mem (subst_t z u T1)
| trm_app e1 e2 => trm_app (subst_e z u e1) (subst_e z u e2)
end.
(* ********************************************************************** *)
(** * Tactics *)
(** Constructors as hints. *)
Hint Constructors type term wft wfe ok okt value red.
Hint Resolve
sub_bot sub_top sub_refl_sel
typing_var typing_app typing_sub.
(** Gathering free names already used in the proofs *)
Ltac gather_vars :=
let A := gather_vars_with (fun x : vars => x) in
let B := gather_vars_with (fun x : var => \{x}) in
let C := gather_vars_with (fun x : typ => fv_t x) in
let D := gather_vars_with (fun x : trm => fv_e x) in
let E := gather_vars_with (fun x : env => dom x) in
constr:(A \u B \u C \u D \u E).
(** "pick_fresh x" tactic create a fresh variable with name x *)
Ltac pick_fresh x :=
let L := gather_vars in (pick_fresh_gen L x).
(** "apply_fresh T as x" is used to apply inductive rule which
use an universal quantification over a cofinite set *)
Tactic Notation "apply_fresh" constr(T) "as" ident(x) :=
apply_fresh_base T gather_vars x.
Tactic Notation "apply_fresh" "*" constr(T) "as" ident(x) :=
apply_fresh T as x; auto*.
(** These tactics help applying a lemma which conclusion mentions
an environment (E & F) in the particular case when F is empty *)
Ltac get_env :=
match goal with
| |- wft ?E _ => E
| |- wfe ?E _ => E
| |- sub ?E _ _ => E
| |- has ?E _ _ => E
| |- typing ?E _ _ => E
end.
Tactic Notation "apply_empty_bis" tactic(get_env) constr(lemma) :=
let E := get_env in rewrite <- (concat_empty_r E);
eapply lemma; try rewrite concat_empty_r.
Tactic Notation "apply_empty" constr(F) :=
apply_empty_bis (get_env) F.
Tactic Notation "apply_empty" "*" constr(F) :=
apply_empty F; auto*.
Scheme typ_mut := Induction for typ Sort Prop
with trm_mut := Induction for trm Sort Prop.
Combined Scheme typ_trm_mutind from typ_mut, trm_mut.
Scheme type_mut := Induction for type Sort Prop
with term_mut := Induction for term Sort Prop.
Combined Scheme lc_mutind from type_mut, term_mut.
Scheme wft_mut := Induction for wft Sort Prop
with wfe_mut := Induction for wfe Sort Prop.
Combined Scheme wf_mutind from wft_mut, wfe_mut.
Scheme sub_mut := Induction for sub Sort Prop
with has_mut := Induction for has Sort Prop.
Combined Scheme sub_has_mutind from sub_mut, has_mut.
(* ********************************************************************** *)
(** * Properties of Substitutions *)
(** Substitution on indices is identity on well-formed terms. *)
Lemma open_rec_lc_core : (forall T j v u i, i <> j ->
(open_t_rec j v T) = open_t_rec i u (open_t_rec j v T) ->
T = open_t_rec i u T) /\ (forall e j v u i, i <> j ->
open_e_rec j v e = open_e_rec i u (open_e_rec j v e) ->
e = open_e_rec i u e).
Proof.
apply typ_trm_mutind;
try (introv IH1 IH2 Neq H);
try (introv IH Neq H);
try (introv Neq H);
simpl in *; inversion H; f_equal*.
case_nat*. case_nat*.
Qed.
Lemma open_rec_lc : (forall T,
type T -> forall u k, T = open_t_rec k u T) /\ (forall e,
term e -> forall u k, e = open_e_rec k u e).
Proof.
apply lc_mutind; intros; simpl; f_equal*.
pick_fresh x. apply* ((proj1 open_rec_lc_core) T2 0 (trm_fvar x)).
pick_fresh x. apply* ((proj2 open_rec_lc_core) e1 0 (trm_fvar x)).
Qed.
Lemma open_t_var_type : forall x T,
type T -> T open_t_var x = T.
Proof.
intros. unfold open_t. rewrite* <- (proj1 open_rec_lc).
Qed.
(** Substitution for a fresh name is identity. *)
Lemma subst_fresh : (forall T z u,
z \notin fv_t T -> subst_t z u T = T) /\ (forall e z u,
z \notin fv_e e -> subst_e z u e = e).
Proof.
apply typ_trm_mutind; simpl; intros; f_equal*.
case_var*.
Qed.
(** Substitution distributes on the open operation. *)
Lemma subst_open_rec : (forall T1 t2 x u n, term u ->
subst_t x u (open_t_rec n t2 T1) =
open_t_rec n (subst_e x u t2) (subst_t x u T1)) /\ (forall t1 t2 x u n, term u ->
subst_e x u (open_e_rec n t2 t1) =
open_e_rec n (subst_e x u t2) (subst_e x u t1)).
Proof.
apply typ_trm_mutind; intros; simpls; f_equal*.
case_nat*.
case_var*. rewrite* <- (proj2 open_rec_lc).
Qed.
Lemma subst_t_open_t : forall T1 t2 x u, term u ->
subst_t x u (open_t T1 t2) =
open_t (subst_t x u T1) (subst_e x u t2).
Proof.
unfold open_t. auto* (proj1 subst_open_rec).
Qed.
Lemma subst_e_open_e : forall t1 t2 x u, term u ->
subst_e x u (open_e t1 t2) =
open_e (subst_e x u t1) (subst_e x u t2).
Proof.
unfold open_e. auto* (proj2 subst_open_rec).
Qed.
(** Substitution and open_var for distinct names commute. *)
Lemma subst_t_open_t_var : forall x y u T, y <> x -> term u ->
(subst_t x u T) open_t_var y = subst_t x u (T open_t_var y).
Proof.
introv Neq Wu. rewrite* subst_t_open_t.
simpl. case_var*.
Qed.
Lemma subst_e_open_e_var : forall x y u e, y <> x -> term u ->
(subst_e x u e) open_e_var y = subst_e x u (e open_e_var y).
Proof.
introv Neq Wu. rewrite* subst_e_open_e.
simpl. case_var*.
Qed.
(** Opening up a body t with a type u is the same as opening
up the abstraction with a fresh name x and then substituting u for x. *)
Lemma subst_t_intro : forall x T2 u,
x \notin fv_t T2 -> term u ->
open_t T2 u = subst_t x u (T2 open_t_var x).
Proof.
introv Fr Wu. rewrite* subst_t_open_t.
rewrite* (proj1 subst_fresh). simpl. case_var*.
Qed.
Lemma subst_e_intro : forall x t2 u,
x \notin fv_e t2 -> term u ->
open_e t2 u = subst_e x u (t2 open_e_var x).
Proof.
introv Fr Wu. rewrite* subst_e_open_e.
rewrite* (proj2 subst_fresh). simpl. case_var*.
Qed.
(** Substitutions preserve local closure. *)
Lemma subst_lc :
(forall T, type T -> forall z u, term u -> type (subst_t z u T)) /\
(forall e, term e -> forall z u, term u -> term (subst_e z u e)).
Proof.
apply lc_mutind; intros; simpl; auto.
apply_fresh* type_all as X. rewrite* subst_t_open_t_var.
case_var*.
apply_fresh* term_abs as y. rewrite* subst_e_open_e_var.
Qed.
Lemma subst_t_type : forall T z u,
type T -> term u -> type (subst_t z u T).
Proof.
intros. apply* (proj1 subst_lc).
Qed.
Lemma subst_e_term : forall e1 z e2,
term e1 -> term e2 -> term (subst_e z e2 e1).
Proof.
intros. apply* (proj2 subst_lc).
Qed.
Lemma subst_e_value : forall e1 z e2,
value e1 -> term e2 -> value (subst_e z e2 e1).
Proof.
intros. inversion H; subst; simpl.
- apply value_abs.
assert (trm_abs (subst_t z e2 V) (subst_e z e2 e0) = subst_e z e2 (trm_abs V e0)) as A. {
simpl. reflexivity.
}
rewrite A. apply* subst_e_term.
- apply value_mem.
assert (trm_mem (subst_t z e2 V) = subst_e z e2 (trm_mem V)) as A. {
simpl. reflexivity.
}
rewrite A. apply* subst_e_term.
Qed.
Lemma value_is_term: forall e, value e -> term e.
Proof.
introv H. inversion H; subst; eauto.
Qed.
Hint Resolve subst_t_type subst_e_term subst_e_value value_is_term.
(* ********************************************************************** *)
(** * Properties of well-formedness of a type in an environment *)
(** If a type is well-formed in an environment then it is locally closed. *)
Lemma wf_lc : (forall E T, wft E T -> type T) /\
(forall E e, wfe E e -> term e).
Proof.
apply wf_mutind; eauto.
Qed.
Lemma wft_type : forall E T,
wft E T -> type T.
Proof.
intros. eapply (proj1 wf_lc); eauto.
Qed.
Lemma wfe_term : forall E e,
wfe E e -> term e.
Proof.
intros. eapply (proj2 wf_lc); eauto.
Qed.
(** Through weakening *)
Lemma wf_weaken :
(forall E0 T, wft E0 T ->
forall E F G, E0 = E & G ->
ok (E & F & G) ->
wft (E & F & G) T)
/\
(forall E0 e, wfe E0 e ->
forall E F G, E0 = E & G ->
ok (E & F & G) ->
wfe (E & F & G) e).
Proof.
apply wf_mutind; intros; subst; eauto.
apply_fresh* wft_all as Y. apply_ih_bind* H0.
apply (@wfe_var U). apply* binds_weaken.
apply_fresh* wfe_abs as y. apply_ih_bind* H0.
Qed.
Lemma wft_weaken : forall G T E F,
wft (E & G) T ->
ok (E & F & G) ->
wft (E & F & G) T.
Proof.
intros. eapply (proj1 wf_weaken); eauto.
Qed.
Lemma wft_weaken_empty : forall T E,
wft empty T ->
ok E ->
wft E T.
Proof.
intros.
assert (E = empty & E & empty) as A. {
rewrite concat_empty_l. rewrite concat_empty_r. reflexivity.
}
rewrite A. apply wft_weaken.
rewrite concat_empty_l. auto.
rewrite concat_empty_l. rewrite concat_empty_r. auto.
Qed.
Lemma wfe_weaken : forall G T E F,
wfe (E & G) T ->
ok (E & F & G) ->
wfe (E & F & G) T.
Proof.
intros. eapply (proj2 wf_weaken); eauto.
Qed.
Lemma wfe_weaken_empty : forall T E,
wfe empty T ->
ok E ->
wfe E T.
Proof.
intros.
assert (E = empty & E & empty) as A. {
rewrite concat_empty_l. rewrite concat_empty_r. reflexivity.
}
rewrite A. apply wfe_weaken.
rewrite concat_empty_l. auto.
rewrite concat_empty_l. rewrite concat_empty_r. auto.
Qed.
(** Through narrowing *)
Lemma wf_narrow : (forall E0 T, wft E0 T -> forall V F U E x,
E0 = (E & x ~ V & F) ->
ok (E & x ~ U & F) ->
wft (E & x ~ U & F) T)
/\
(forall E0 e, wfe E0 e -> forall V F U E x,
E0 = (E & x ~ V & F) ->
ok (E & x ~ U & F) ->
wfe (E & x ~ U & F) e).
Proof.
apply wf_mutind; intros; subst; eauto.
apply_fresh* wft_all as Y. apply_ih_bind* H0.
destruct (binds_middle_inv b) as [K|[K|K]]; try destructs K.
applys wfe_var. apply* binds_concat_right.
subst. applys wfe_var. apply~ binds_middle_eq.
applys wfe_var. apply~ binds_concat_left.
apply* binds_concat_left.
apply_fresh* wfe_abs as y. apply_ih_bind* H0.
Qed.
Lemma wft_narrow : forall V F U T E x,
wft (E & x ~ V & F) T ->
ok (E & x ~ U & F) ->
wft (E & x ~ U & F) T.
Proof.
intros. eapply (proj1 wf_narrow); eauto.
Qed.
(** Through substitution *)
Lemma wf_subst : (forall E0 T, wft E0 T -> forall F Q E Z u,
E0 = E & Z ~ Q & F ->
(value u \/ exists x, trm_fvar x = u) -> wfe E u ->
ok (E & map (subst_t Z u) F) ->
wft (E & map (subst_t Z u) F) (subst_t Z u T)) /\
(forall E0 e, wfe E0 e -> forall F Q E Z u,
E0 = E & Z ~ Q & F ->
(value u \/ exists x, trm_fvar x = u) -> wfe E u ->
ok (E & map (subst_t Z u) F) ->
wfe (E & map (subst_t Z u) F) (subst_e Z u e)).
Proof.
apply wf_mutind; intros; subst; simpl; eauto.
- destruct o as [? | [? ?]].
+ apply* wft_sel. left. apply subst_e_value. assumption. apply* wfe_term.
+ subst. simpl. case_var*.
* apply_empty* wft_weaken.
* apply* wft_sel. rewrite* <- ((proj2 subst_fresh) (trm_fvar x) Z u).
simpl. auto.
- apply_fresh* wft_all as Y.
lets: wft_type.
rewrite* subst_t_open_t_var.
apply_ih_map_bind* H0. apply* wfe_term.
- case_var*.
+ apply_empty* (proj2 wf_weaken).
+ destruct (binds_concat_inv b) as [?|[? ?]].
apply (@wfe_var (subst_t Z u U)).
apply~ binds_concat_right.
destruct (binds_push_inv H3) as [[? ?]|[? ?]].
subst. false~.
applys wfe_var. apply* binds_concat_left.
- apply_fresh* wfe_abs as y.
lets: (proj2 wf_lc).
rewrite* subst_e_open_e_var.
apply_ih_map_bind* H0.
Qed.
Lemma wft_subst : forall F Q E Z u T,
wft (E & Z ~ Q & F) T ->
(value u \/ exists x, trm_fvar x = u) -> wfe E u ->
ok (E & map (subst_t Z u) F) ->
wft (E & map (subst_t Z u) F) (subst_t Z u T).
Proof.
intros. eapply (proj1 wf_subst); eauto.
Qed.
Lemma wft_subst1 : forall F Q Z u T,
wft (Z ~ Q & F) T ->
(value u \/ exists x, trm_fvar x = u) -> wfe empty u ->
ok (map (subst_t Z u) F) ->
wft (map (subst_t Z u) F) (subst_t Z u T).
Proof.
intros.
rewrite <- (@concat_empty_l typ (map (subst_t Z u) F)).
apply* wft_subst.
rewrite concat_empty_l. eassumption.
rewrite concat_empty_l. eassumption.
Qed.
Lemma wft_subst_empty : forall Q Z u T,
wft (Z ~ Q) T ->
(value u \/ exists x, trm_fvar x = u) -> wfe empty u ->
wft empty (subst_t Z u T).
Proof.
intros.
assert (empty & map (subst_t Z u) empty = empty) as A. {
rewrite map_empty. rewrite concat_empty_l. reflexivity.
}
rewrite <- A. eapply wft_subst; eauto.
rewrite concat_empty_l. rewrite concat_empty_r. eauto.
rewrite A. eauto.
Qed.
(** Through type reduction *)
Lemma wft_open : forall E u T1 T2,
ok E ->
wft E (typ_all T1 T2) ->
(value u \/ exists x, trm_fvar x = u) -> wfe E u ->
wft E (open_t T2 u).
Proof.
introv Ok WA VU WU. inversions WA. pick_fresh X.
auto* wft_type. rewrite* (@subst_t_intro X).
lets K: (@wft_subst empty).
specializes_vars K. clean_empty K. apply* K.
apply* wfe_term.
Qed.
(* ********************************************************************** *)
(** * Relations between well-formed environment and types well-formed
in environments *)
(** If an environment is well-formed, then it does not contain duplicated keys. *)
Lemma ok_from_okt : forall E,
okt E -> ok E.
Proof.
induction 1; auto.
Qed.
Hint Extern 1 (ok _) => apply ok_from_okt.
(** Extraction from an assumption in a well-formed environments *)
Lemma wft_from_env_has : forall x U E,
okt E -> binds x U E -> wft E U.
Proof.
induction E using env_ind; intros Ok B.
false* binds_empty_inv.
inversions Ok.
false (empty_push_inv H0).
destruct (eq_push_inv H) as [? [? ?]]. subst. clear H.
destruct (binds_push_inv B) as [[? ?]|[? ?]]. subst.
apply_empty* wft_weaken.
apply_empty* wft_weaken.
Qed.
(** Extraction from a well-formed environment *)
Lemma wft_from_okt : forall x T E,
okt (E & x ~ T) -> wft E T.
Proof.
intros. inversions* H.
false (empty_push_inv H1).
destruct (eq_push_inv H0) as [? [? ?]]. subst. assumption.
Qed.
(** Automation *)
Lemma wft_weaken_right : forall T E F,
wft E T ->
ok (E & F) ->
wft (E & F) T.
Proof.
intros. apply_empty* wft_weaken.
Qed.
Hint Resolve wft_weaken_right.
Hint Resolve wft_from_okt.
Hint Immediate wft_from_env_has.
Hint Resolve wft_subst.
(* ********************************************************************** *)
(** ** Properties of well-formedness of an environment *)
(** Inversion lemma *)
Lemma okt_push_inv : forall E x T,
okt (E & x ~ T) -> okt E /\ wft E T /\ x # E.
Proof.
introv O. inverts O.
false* empty_push_inv.
lets (?&M&?): (eq_push_inv H). subst. eauto.
Qed.
Lemma okt_push_type : forall E x T,
okt (E & x ~ T) -> type T.
Proof. intros. applys wft_type. forwards*: okt_push_inv. Qed.
Hint Immediate okt_push_type.
(** Through narrowing *)
Lemma okt_narrow : forall V (E F:env) U x,
okt (E & x ~ V & F) ->
wft E U ->
okt (E & x ~ U & F).
Proof.
introv O W. induction F using env_ind.
rewrite concat_empty_r in *. lets*: (okt_push_inv O).
rewrite concat_assoc in *.
lets (?&?&?): (okt_push_inv O).
applys~ okt_push. applys* wft_narrow.
Qed.
(** Through substitution *)
Lemma okt_subst : forall Q Z u (E F:env),
okt (E & Z ~ Q & F) ->
(value u \/ exists x, trm_fvar x = u) -> wfe E u ->
okt (E & map (subst_t Z u) F).
Proof.
introv O V W. induction F using env_ind.
rewrite map_empty. rewrite concat_empty_r in *.
lets*: (okt_push_inv O).
rewrite map_push. rewrite concat_assoc in *.
lets*: (okt_push_inv O).
apply okt_push. apply* IHF. apply* wft_subst. auto*.
Qed.
Lemma okt_subst1 : forall Q Z u (F:env),
okt (Z ~ Q & F) ->
(value u \/ exists x, trm_fvar x = u) -> wfe empty u ->
okt (map (subst_t Z u) F).
Proof.
intros.
rewrite <- concat_empty_l. apply* okt_subst.
rewrite concat_empty_l. eassumption.
Qed.
(** Automation *)
Hint Resolve okt_narrow okt_subst wft_weaken.
(* ********************************************************************** *)
(** ** Environment is unchanged by substitution from a fresh name *)
Ltac destruct_notin_union :=
match goal with
| H: _ \notin _ \u _ |- _ => eapply notin_union in H; destruct H
end.
Lemma notin_fv_open_rec : (forall T k y x,
x \notin fv_t (open_t_rec k (trm_fvar y) T) ->
x \notin fv_t T) /\ (forall e k y x,
x \notin fv_e (open_e_rec k (trm_fvar y) e) ->
x \notin fv_e e).
Proof.
apply typ_trm_mutind; simpl; intros;
repeat destruct_notin_union; eauto using notin_union_l.
Qed.
Lemma notin_fv_t_open : forall y x T,
x \notin fv_t (T open_t_var y) ->
x \notin fv_t T.
Proof.
unfold open_t. intros. apply* (proj1 notin_fv_open_rec).
Qed.
Lemma notin_fv_e_open : forall y x e,
x \notin fv_e (e open_e_var y) ->
x \notin fv_e e.
Proof.
unfold open_e. intros. apply* (proj2 notin_fv_open_rec).
Qed.
Lemma notin_fv_wf_rec :
(forall E T,
wft E T -> forall x, x # E -> x \notin fv_t T) /\
(forall E e,
wfe E e -> forall x, x # E -> x \notin fv_e e).
Proof.
apply wf_mutind; intros; simpl; eauto.
notin_simpl; auto. pick_fresh Y. apply* (@notin_fv_t_open Y).
rewrite notin_singleton. intro. subst. applys binds_fresh_inv b H.
notin_simpl; auto. pick_fresh y. apply* (@notin_fv_e_open y).
Qed.
Lemma notin_fv_wf : forall E x T,
wft E T -> x # E -> x \notin fv_t T.
Proof.
intros. eapply (proj1 notin_fv_wf_rec); eauto.
Qed.
Lemma map_subst_id : forall G z u,
okt G -> z # G -> G = map (subst_t z u) G.
Proof.
induction 1; intros Fr; autorewrite with rew_env_map; simpl.
auto.
rewrite* <- IHokt. rewrite* (proj1 subst_fresh). apply* notin_fv_wf.
Qed.
(* ********************************************************************** *)
(** ** Regularity of relations *)
(** The subtyping relation is restricted to well-formed objects. *)
Lemma sub_has_regular : (forall E S T,
sub E S T -> okt E /\ wft E S /\ wft E T) /\ (forall E p T,
has E p T -> okt E /\ wft E (typ_sel p) /\ wft E T).
Proof.
apply sub_has_mutind; intros; try auto*.
splits*. destruct H as [? [? A]]. inversion A; subst. assumption.
splits*. destruct H as [? [? A]]. inversion A; subst. assumption.
split. auto*. split;
apply_fresh* wft_all as Y;
forwards~: (H0 Y); apply_empty* (@wft_narrow T1).
splits*. apply wft_sel. left. apply value_mem. apply* wfe_term. apply* wfe_mem.
splits*. apply wft_sel. left. apply value_abs. apply* wfe_term. assumption.
Qed.
Lemma sub_regular : forall E S T,
sub E S T -> okt E /\ wft E S /\ wft E T.
Proof.
intros. apply* (proj1 sub_has_regular).
Qed.
Lemma has_regular : forall E p T,
has E p T -> okt E /\ wft E (typ_sel p) /\ wft E T.
Proof.
intros. apply* (proj2 sub_has_regular).
Qed.
Lemma has_regular_e : forall E p T,
has E p T -> (value p \/ (exists x, trm_fvar x = p)) /\ wfe E p.
Proof.
intros. apply has_regular in H. destruct H as [? [A ?]].
inversion A; subst. split; assumption.
Qed.
(** The typing relation is restricted to well-formed objects. *)
Lemma typing_regular : forall E e T,
typing E e T -> okt E /\ wfe E e /\ wft E T.
Proof.
induction 1.
splits*.
splits.
pick_fresh y. specializes H0 y. destructs~ H0.
forwards*: okt_push_inv.
apply_fresh* wfe_abs as y.
pick_fresh y. forwards~ K: (H0 y). destructs K.
forwards*: okt_push_inv.
forwards~ K: (H0 y). destructs K. auto.
apply_fresh* wft_all as Y.
pick_fresh y. forwards~ K: (H0 y). destructs K.
forwards*: okt_push_inv.
forwards~ K: (H0 Y). destructs K.
forwards*: okt_push_inv.
splits*.
splits*.
splits*.
splits*. destructs~ (sub_regular H0).
Qed.
(** The value relation is restricted to well-formed objects. *)
Lemma value_regular : forall t,