-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFsubL_alt.v
1712 lines (1466 loc) · 53.1 KB
/
FsubL_alt.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
(***************************************************************************
Preservation and Progress for System-F
with Subtyping, Bottom and Lower Bounds
- based on the POPLMark solution of
Brian Aydemir & Arthur Charguéraud, March 2007
cf. https://github.com/samuelgruetter/dot-calculus/blob/master/ln/Fsub_Definitions.v
cf. https://github.com/samuelgruetter/dot-calculus/blob/master/ln/Fsub_Infrastructure.v
cf. https://github.com/samuelgruetter/dot-calculus/blob/master/ln/Fsub_Part1A.v
- compared to FsubL, does push back on values, using possible types
***************************************************************************)
Set Implicit Arguments.
Require Import LibLN.
Implicit Types x : var.
Implicit Types X : var.
(***************************************************************************
* Definitions *
***************************************************************************)
(* ********************************************************************** *)
(** * Description of the Language *)
(** Representation of pre-types *)
Inductive typ : Set :=
| typ_top : typ
| typ_bot : typ
| typ_bvar : nat -> typ
| typ_fvar : var -> typ
| typ_arrow : typ -> typ -> typ
| typ_all : typ -> typ -> typ -> typ.
(** Representation of pre-terms *)
Inductive trm : Set :=
| trm_bvar : nat -> trm
| trm_fvar : var -> trm
| trm_abs : typ -> trm -> trm
| trm_app : trm -> trm -> trm
| trm_tabs : typ -> typ -> trm -> trm
| trm_tapp : trm -> typ -> trm.
(** Opening up a type binder occuring in a type *)
Fixpoint open_tt_rec (K : nat) (U : typ) (T : typ) {struct T} : typ :=
match T with
| typ_top => typ_top
| typ_bot => typ_bot
| typ_bvar J => If K = J then U else (typ_bvar J)
| typ_fvar X => typ_fvar X
| typ_arrow T1 T2 => typ_arrow (open_tt_rec K U T1) (open_tt_rec K U T2)
| typ_all T0 T1 T2 => typ_all (open_tt_rec K U T0) (open_tt_rec K U T1) (open_tt_rec (S K) U T2)
end.
Definition open_tt T U := open_tt_rec 0 U T.
(** Opening up a type binder occuring in a term *)
Fixpoint open_te_rec (K : nat) (U : typ) (e : trm) {struct e} : trm :=
match e with
| trm_bvar i => trm_bvar i
| trm_fvar x => trm_fvar x
| trm_abs V e1 => trm_abs (open_tt_rec K U V) (open_te_rec K U e1)
| trm_app e1 e2 => trm_app (open_te_rec K U e1) (open_te_rec K U e2)
| trm_tabs VS VU e1 => trm_tabs (open_tt_rec K U VS) (open_tt_rec K U VU) (open_te_rec (S K) U e1)
| trm_tapp e1 V => trm_tapp (open_te_rec K U e1) (open_tt_rec K U V)
end.
Definition open_te t U := open_te_rec 0 U t.
(** Opening up a term binder occuring in a term *)
Fixpoint open_ee_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 V (open_ee_rec (S k) f e1)
| trm_app e1 e2 => trm_app (open_ee_rec k f e1) (open_ee_rec k f e2)
| trm_tabs VS VU e1 => trm_tabs VS VU (open_ee_rec k f e1)
| trm_tapp e1 V => trm_tapp (open_ee_rec k f e1) V
end.
Definition open_ee t u := open_ee_rec 0 u t.
(** Notation for opening up binders with type or term variables *)
Notation "T 'open_tt_var' X" := (open_tt T (typ_fvar X)) (at level 67).
Notation "t 'open_te_var' X" := (open_te t (typ_fvar X)) (at level 67).
Notation "t 'open_ee_var' x" := (open_ee t (trm_fvar x)) (at level 67).
(** Types as locally closed pre-types *)
Inductive type : typ -> Prop :=
| type_top :
type typ_top
| type_bot :
type typ_bot
| type_var : forall X,
type (typ_fvar X)
| type_arrow : forall T1 T2,
type T1 ->
type T2 ->
type (typ_arrow T1 T2)
| type_all : forall L T0 T1 T2,
type T0 ->
type T1 ->
(forall X, X \notin L -> type (T2 open_tt_var X)) ->
type (typ_all T0 T1 T2).
(** Terms as locally closed pre-terms *)
Inductive 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_ee_var x)) ->
term (trm_abs V e1)
| term_app : forall e1 e2,
term e1 ->
term e2 ->
term (trm_app e1 e2)
| term_tabs : forall L VS VU e1,
type VS ->
type VU ->
(forall X, X \notin L -> term (e1 open_te_var X)) ->
term (trm_tabs VS VU e1)
| term_tapp : forall e1 V,
term e1 ->
type V ->
term (trm_tapp e1 V).
(** Binding are either mapping type or term variables. *)
Inductive bind : Set :=
| bind_sub : typ -> typ -> bind
| bind_typ : typ -> bind.
(** Environment is an associative list of bindings. *)
Definition env := LibEnv.env bind.
(** 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_top : forall E,
wft E typ_top
| wft_bot : forall E,
wft E typ_bot
| wft_var : forall T0 T1 E X,
binds X (bind_sub T0 T1) E ->
wft E (typ_fvar X)
| wft_arrow : forall E T1 T2,
wft E T1 ->
wft E T2 ->
wft E (typ_arrow T1 T2)
| wft_all : forall L E T0 T1 T2,
wft E T0 ->
wft E T1 ->
(forall X, X \notin L ->
wft (E & X ~ bind_sub T0 T1) (T2 open_tt_var X)) ->
wft E (typ_all T0 T1 T2).
(** 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_sub : forall E X T0 T1,
okt E -> wft E T0 -> wft E T1 -> X # E -> okt (E & X ~ bind_sub T0 T1)
| okt_typ : forall E x T,
okt E -> wft E T -> x # E -> okt (E & x ~ bind_typ T).
(** Subtyping relation *)
Inductive sub : env -> typ -> typ -> Prop :=
| sub_top : forall E S,
okt E ->
wft E S ->
sub E S typ_top
| sub_bot : forall E T,
okt E ->
wft E T ->
sub E typ_bot T
| sub_refl_tvar : forall E X,
okt E ->
wft E (typ_fvar X) ->
sub E (typ_fvar X) (typ_fvar X)
| sub_tvar : forall T0 T1 E X,
okt E ->
binds X (bind_sub T0 T1) E ->
sub E (typ_fvar X) T1
| sub_tvar_lower : forall T0 T1 E X,
okt E ->
binds X (bind_sub T0 T1) E ->
sub E T0 (typ_fvar X)
| sub_arrow : forall E S1 S2 T1 T2,
sub E T1 S1 ->
sub E S2 T2 ->
sub E (typ_arrow S1 S2) (typ_arrow T1 T2)
| sub_all : forall L E S0 S1 S2 T0 T1 T2,
sub E S0 T0 ->
sub E T1 S1 ->
(forall X, X \notin L ->
sub (E & X ~ bind_sub T0 T1) (S2 open_tt_var X) (T2 open_tt_var X)) ->
sub E (typ_all S0 S1 S2) (typ_all T0 T1 T2)
| sub_trans : forall E S T U,
sub E S T ->
sub E T U ->
sub E S U
.
(** Typing relation *)
Inductive typing : env -> trm -> typ -> Prop :=
| typing_var : forall E x T,
okt E ->
binds x (bind_typ T) E ->
typing E (trm_fvar x) T
| typing_abs : forall L E V e1 T1,
(forall x, x \notin L ->
typing (E & x ~ bind_typ V) (e1 open_ee_var x) T1) ->
typing E (trm_abs V e1) (typ_arrow V T1)
| typing_app : forall T1 E e1 e2 T2,
typing E e1 (typ_arrow T1 T2) ->
typing E e2 T1 ->
typing E (trm_app e1 e2) T2
| typing_tabs : forall L E VS VU e1 T1,
(forall X, X \notin L ->
typing (E & X ~ bind_sub VS VU) (e1 open_te_var X) (T1 open_tt_var X)) ->
typing E (trm_tabs VS VU e1) (typ_all VS VU T1)
| typing_tapp : forall T0 T1 E e1 T T2,
typing E e1 (typ_all T0 T1 T2) ->
sub E T0 T ->
sub E T T1 ->
typing E (trm_tapp e1 T) (open_tt T2 T)
| typing_sub : forall S E e T,
typing E e S ->
sub E S T ->
typing E e T.
(** Values *)
Inductive value : trm -> Prop :=
| value_abs : forall V e1, term (trm_abs V e1) ->
value (trm_abs V e1)
| value_tabs : forall VS VU e1, term (trm_tabs VS VU e1) ->
value (trm_tabs VS VU e1).
(** 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_tapp : forall e1 e1' V,
type V ->
red e1 e1' ->
red (trm_tapp e1 V) (trm_tapp e1' V)
| red_abs : forall V e1 v2,
term (trm_abs V e1) ->
value v2 ->
red (trm_app (trm_abs V e1) v2) (open_ee e1 v2)
| red_tabs : forall V0 V1 e1 V2,
term (trm_tabs V0 V1 e1) ->
type V2 ->
red (trm_tapp (trm_tabs V0 V1 e1) V2) (open_te 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'.
(***************************************************************************
* Infrastructure *
***************************************************************************)
(* ********************************************************************** *)
(** * Additional Definitions Used in the Proofs *)
(** Computing free type variables in a type *)
Fixpoint fv_tt (T : typ) {struct T} : vars :=
match T with
| typ_top => \{}
| typ_bot => \{}
| typ_bvar J => \{}
| typ_fvar X => \{X}
| typ_arrow T1 T2 => (fv_tt T1) \u (fv_tt T2)
| typ_all T0 T1 T2 => (fv_tt T0) \u (fv_tt T1) \u (fv_tt T2)
end.
(** Computing free type variables in a term *)
Fixpoint fv_te (e : trm) {struct e} : vars :=
match e with
| trm_bvar i => \{}
| trm_fvar x => \{}
| trm_abs V e1 => (fv_tt V) \u (fv_te e1)
| trm_app e1 e2 => (fv_te e1) \u (fv_te e2)
| trm_tabs VS VU e1 => (fv_tt VS) \u (fv_tt VU) \u (fv_te e1)
| trm_tapp e1 V => (fv_tt V) \u (fv_te e1)
end.
(** Computing free term variables in a type *)
Fixpoint fv_ee (e : trm) {struct e} : vars :=
match e with
| trm_bvar i => \{}
| trm_fvar x => \{x}
| trm_abs V e1 => (fv_ee e1)
| trm_app e1 e2 => (fv_ee e1) \u (fv_ee e2)
| trm_tabs VS VU e1 => (fv_ee e1)
| trm_tapp e1 V => (fv_ee e1)
end.
(** Substitution for free type variables in types. *)
Fixpoint subst_tt (Z : var) (U : typ) (T : typ) {struct T} : typ :=
match T with
| typ_top => typ_top
| typ_bot => typ_bot
| typ_bvar J => typ_bvar J
| typ_fvar X => If X = Z then U else (typ_fvar X)
| typ_arrow T1 T2 => typ_arrow (subst_tt Z U T1) (subst_tt Z U T2)
| typ_all T0 T1 T2 => typ_all (subst_tt Z U T0) (subst_tt Z U T1) (subst_tt Z U T2)
end.
(** Substitution for free type variables in terms. *)
Fixpoint subst_te (Z : var) (U : typ) (e : trm) {struct e} : trm :=
match e with
| trm_bvar i => trm_bvar i
| trm_fvar x => trm_fvar x
| trm_abs V e1 => trm_abs (subst_tt Z U V) (subst_te Z U e1)
| trm_app e1 e2 => trm_app (subst_te Z U e1) (subst_te Z U e2)
| trm_tabs VS VU e1 => trm_tabs (subst_tt Z U VS) (subst_tt Z U VU) (subst_te Z U e1)
| trm_tapp e1 V => trm_tapp (subst_te Z U e1) (subst_tt Z U V)
end.
(** Substitution for free term variables in terms. *)
Fixpoint subst_ee (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 V (subst_ee z u e1)
| trm_app e1 e2 => trm_app (subst_ee z u e1) (subst_ee z u e2)
| trm_tabs VS VU e1 => trm_tabs VS VU (subst_ee z u e1)
| trm_tapp e1 V => trm_tapp (subst_ee z u e1) V
end.
(** Substitution for free type variables in environment. *)
Definition subst_tb (Z : var) (P : typ) (b : bind) : bind :=
match b with
| bind_sub T0 T1 => bind_sub (subst_tt Z P T0) (subst_tt Z P T1)
| bind_typ T => bind_typ (subst_tt Z P T)
end.
(* ********************************************************************** *)
(** * Tactics *)
(** Constructors as hints. *)
Hint Constructors type term wft ok okt value red.
Hint Resolve
sub_top sub_bot sub_refl_tvar sub_arrow
typing_var typing_app typing_tapp 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 : trm => fv_te x) in
let D := gather_vars_with (fun x : trm => fv_ee x) in
let E := gather_vars_with (fun x : typ => fv_tt x) in
let F := gather_vars_with (fun x : env => dom x) in
constr:(A \u B \u C \u D \u E \u F).
(** "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
| |- sub ?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*.
(** Tactic to undo when Coq does too much simplification *)
Ltac unsimpl_map_bind_sub :=
match goal with |- context [ ?B (subst_tt ?Z ?P ?U0) (subst_tt ?Z ?P ?U1) ] =>
unsimpl ((subst_tb Z P) (B U0 U1)) end.
Ltac unsimpl_map_bind_typ :=
match goal with |- context [ ?B (subst_tt ?Z ?P ?U) ] =>
unsimpl ((subst_tb Z P) (B U)) end.
Tactic Notation "unsimpl_map_bind_sub" "*" :=
unsimpl_map_bind_sub; auto*.
Tactic Notation "unsimpl_map_bind_typ" "*" :=
unsimpl_map_bind_typ; auto*.
(* ********************************************************************** *)
(** * Properties of Substitutions *)
(* ********************************************************************** *)
(** ** Properties of type substitution in type *)
(** Substitution on indices is identity on well-formed terms. *)
Lemma open_tt_rec_type_core : forall T j V U i, i <> j ->
(open_tt_rec j V T) = open_tt_rec i U (open_tt_rec j V T) ->
T = open_tt_rec i U T.
Proof.
induction T; introv Neq H; simpl in *; inversion H; f_equal*.
case_nat*. case_nat*.
Qed.
Lemma open_tt_rec_type : forall T U,
type T -> forall k, T = open_tt_rec k U T.
Proof.
induction 1; intros; simpl; f_equal*. unfolds open_tt.
pick_fresh X. apply* (@open_tt_rec_type_core T2 0 (typ_fvar X)).
Qed.
(** Substitution for a fresh name is identity. *)
Lemma subst_tt_fresh : forall Z U T,
Z \notin fv_tt T -> subst_tt Z U T = T.
Proof.
induction T; simpl; intros; f_equal*.
case_var*.
Qed.
(** Substitution distributes on the open operation. *)
Lemma subst_tt_open_tt_rec : forall T1 T2 X P n, type P ->
subst_tt X P (open_tt_rec n T2 T1) =
open_tt_rec n (subst_tt X P T2) (subst_tt X P T1).
Proof.
introv WP. generalize n.
induction T1; intros k; simpls; f_equal*.
case_nat*.
case_var*. rewrite* <- open_tt_rec_type.
Qed.
Lemma subst_tt_open_tt : forall T1 T2 X P, type P ->
subst_tt X P (open_tt T1 T2) =
open_tt (subst_tt X P T1) (subst_tt X P T2).
Proof.
unfold open_tt. auto* subst_tt_open_tt_rec.
Qed.
(** Substitution and open_var for distinct names commute. *)
Lemma subst_tt_open_tt_var : forall X Y U T, Y <> X -> type U ->
(subst_tt X U T) open_tt_var Y = subst_tt X U (T open_tt_var Y).
Proof.
introv Neq Wu. rewrite* subst_tt_open_tt.
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_tt_intro : forall X T2 U,
X \notin fv_tt T2 -> type U ->
open_tt T2 U = subst_tt X U (T2 open_tt_var X).
Proof.
introv Fr Wu. rewrite* subst_tt_open_tt.
rewrite* subst_tt_fresh. simpl. case_var*.
Qed.
(* ********************************************************************** *)
(** ** Properties of type substitution in terms *)
Lemma open_te_rec_term_core : forall e j u i P ,
open_ee_rec j u e = open_te_rec i P (open_ee_rec j u e) ->
e = open_te_rec i P e.
Proof.
induction e; intros; simpl in *; inversion H; f_equal*; f_equal*.
Qed.
Lemma open_te_rec_type_core : forall e j Q i P, i <> j ->
open_te_rec j Q e = open_te_rec i P (open_te_rec j Q e) ->
e = open_te_rec i P e.
Proof.
induction e; intros; simpl in *; inversion H0; f_equal*;
match goal with H: ?i <> ?j |- ?t = open_tt_rec ?i _ ?t =>
apply* (@open_tt_rec_type_core t j) end.
Qed.
Lemma open_te_rec_term : forall e U,
term e -> forall k, e = open_te_rec k U e.
Proof.
intros e U WF. induction WF; intros; simpl;
f_equal*; try solve [ apply* open_tt_rec_type ].
unfolds open_ee. pick_fresh x.
apply* (@open_te_rec_term_core e1 0 (trm_fvar x)).
unfolds open_te. pick_fresh X.
apply* (@open_te_rec_type_core e1 0 (typ_fvar X)).
Qed.
(** Substitution for a fresh name is identity. *)
Lemma subst_te_fresh : forall X U e,
X \notin fv_te e -> subst_te X U e = e.
Proof.
induction e; simpl; intros; f_equal*; auto* subst_tt_fresh.
Qed.
(** Substitution distributes on the open operation. *)
Lemma subst_te_open_te : forall e T X U, type U ->
subst_te X U (open_te e T) =
open_te (subst_te X U e) (subst_tt X U T).
Proof.
intros. unfold open_te. generalize 0.
induction e; intros; simpls; f_equal*;
auto* subst_tt_open_tt_rec.
Qed.
(** Substitution and open_var for distinct names commute. *)
Lemma subst_te_open_te_var : forall X Y U e, Y <> X -> type U ->
(subst_te X U e) open_te_var Y = subst_te X U (e open_te_var Y).
Proof.
introv Neq Wu. rewrite* subst_te_open_te.
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_te_intro : forall X U e,
X \notin fv_te e -> type U ->
open_te e U = subst_te X U (e open_te_var X).
Proof.
introv Fr Wu. rewrite* subst_te_open_te.
rewrite* subst_te_fresh. simpl. case_var*.
Qed.
(* ********************************************************************** *)
(** ** Properties of term substitution in terms *)
Lemma open_ee_rec_term_core : forall e j v u i, i <> j ->
open_ee_rec j v e = open_ee_rec i u (open_ee_rec j v e) ->
e = open_ee_rec i u e.
Proof.
induction e; introv Neq H; simpl in *; inversion H; f_equal*.
case_nat*. case_nat*.
Qed.
Lemma open_ee_rec_type_core : forall e j V u i,
open_te_rec j V e = open_ee_rec i u (open_te_rec j V e) ->
e = open_ee_rec i u e.
Proof.
induction e; introv H; simpls; inversion H; f_equal*.
Qed.
Lemma open_ee_rec_term : forall u e,
term e -> forall k, e = open_ee_rec k u e.
Proof.
induction 1; intros; simpl; f_equal*.
unfolds open_ee. pick_fresh x.
apply* (@open_ee_rec_term_core e1 0 (trm_fvar x)).
unfolds open_te. pick_fresh X.
apply* (@open_ee_rec_type_core e1 0 (typ_fvar X)).
Qed.
(** Substitution for a fresh name is identity. *)
Lemma subst_ee_fresh : forall x u e,
x \notin fv_ee e -> subst_ee x u e = e.
Proof.
induction e; simpl; intros; f_equal*.
case_var*.
Qed.
(** Substitution distributes on the open operation. *)
Lemma subst_ee_open_ee : forall t1 t2 u x, term u ->
subst_ee x u (open_ee t1 t2) =
open_ee (subst_ee x u t1) (subst_ee x u t2).
Proof.
intros. unfold open_ee. generalize 0.
induction t1; intros; simpls; f_equal*.
case_nat*.
case_var*. rewrite* <- open_ee_rec_term.
Qed.
(** Substitution and open_var for distinct names commute. *)
Lemma subst_ee_open_ee_var : forall x y u e, y <> x -> term u ->
(subst_ee x u e) open_ee_var y = subst_ee x u (e open_ee_var y).
Proof.
introv Neq Wu. rewrite* subst_ee_open_ee.
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_ee_intro : forall x u e,
x \notin fv_ee e -> term u ->
open_ee e u = subst_ee x u (e open_ee_var x).
Proof.
introv Fr Wu. rewrite* subst_ee_open_ee.
rewrite* subst_ee_fresh. simpl. case_var*.
Qed.
(** Interactions between type substitutions in terms and opening
with term variables in terms. *)
Lemma subst_te_open_ee_var : forall Z P x e,
(subst_te Z P e) open_ee_var x = subst_te Z P (e open_ee_var x).
Proof.
introv. unfold open_ee. generalize 0.
induction e; intros; simpl; f_equal*. case_nat*.
Qed.
(** Interactions between term substitutions in terms and opening
with type variables in terms. *)
Lemma subst_ee_open_te_var : forall z u e X, term u ->
(subst_ee z u e) open_te_var X = subst_ee z u (e open_te_var X).
Proof.
introv. unfold open_te. generalize 0.
induction e; intros; simpl; f_equal*.
case_var*. symmetry. auto* open_te_rec_term.
Qed.
(** Substitutions preserve local closure. *)
Lemma subst_tt_type : forall T Z P,
type T -> type P -> type (subst_tt Z P T).
Proof.
induction 1; intros; simpl; auto.
case_var*.
apply_fresh* type_all as X. rewrite* subst_tt_open_tt_var.
Qed.
Lemma subst_te_term : forall e Z P,
term e -> type P -> term (subst_te Z P e).
Proof.
lets: subst_tt_type. induction 1; intros; simpl; auto.
apply_fresh* term_abs as x. rewrite* subst_te_open_ee_var.
apply_fresh* term_tabs as x. rewrite* subst_te_open_te_var.
Qed.
Lemma subst_ee_term : forall e1 Z e2,
term e1 -> term e2 -> term (subst_ee Z e2 e1).
Proof.
induction 1; intros; simpl; auto.
case_var*.
apply_fresh* term_abs as y. rewrite* subst_ee_open_ee_var.
apply_fresh* term_tabs as Y. rewrite* subst_ee_open_te_var.
Qed.
Hint Resolve subst_tt_type subst_te_term subst_ee_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 wft_type : forall E T,
wft E T -> type T.
Proof.
induction 1; eauto.
Qed.
(** Through weakening *)
Lemma wft_weaken : forall G T E F,
wft (E & G) T ->
ok (E & F & G) ->
wft (E & F & G) T.
Proof.
intros. gen_eq K: (E & G). gen E F G.
induction H; intros; subst; eauto.
(* case: var *)
apply (@wft_var T0 T1). apply* binds_weaken.
(* case: all *)
apply_fresh* wft_all as Y. apply_ih_bind* H2.
Qed.
Lemma wft_weaken_empty : forall T F,
wft empty T ->
ok F ->
wft F T.
Proof.
intros. lets A: (@wft_weaken empty T empty F).
repeat rewrite concat_empty_r in *. repeat rewrite concat_empty_l in *.
apply* A.
Qed.
(** Through narrowing *)
Lemma wft_narrow : forall V0 V1 F U0 U1 T E X,
wft (E & X ~ bind_sub V0 V1 & F) T ->
ok (E & X ~ bind_sub U0 U1 & F) ->
wft (E & X ~ bind_sub U0 U1 & F) T.
Proof.
intros. gen_eq K: (E & X ~ bind_sub V0 V1 & F). gen E F.
induction H; intros; subst; eauto.
destruct (binds_middle_inv H) as [K|[K|K]]; try destructs K.
applys wft_var. apply* binds_concat_right.
subst. applys wft_var. apply~ binds_middle_eq.
applys wft_var. apply~ binds_concat_left.
apply* binds_concat_left.
apply_fresh* wft_all as Y. apply_ih_bind* H2.
Qed.
(** Through strengthening *)
Lemma wft_strengthen : forall E F x U T,
wft (E & x ~ bind_typ U & F) T -> wft (E & F) T.
Proof.
intros. gen_eq G: (E & x ~ bind_typ U & F). gen F.
induction H; intros F EQ; subst; auto.
apply* (@wft_var T0 T1).
destruct (binds_concat_inv H) as [?|[? ?]].
apply~ binds_concat_right.
destruct (binds_push_inv H1) as [[? ?]|[? ?]].
subst. false.
apply~ binds_concat_left.
(* todo: binds_cases tactic *)
apply_fresh* wft_all as Y. apply_ih_bind* H2.
Qed.
(** Through type substitution *)
Lemma wft_subst_tb : forall F Q0 Q1 E Z P T,
wft (E & Z ~ bind_sub Q0 Q1 & F) T ->
wft E P ->
ok (E & map (subst_tb Z P) F) ->
wft (E & map (subst_tb Z P) F) (subst_tt Z P T).
Proof.
introv WT WP. gen_eq G: (E & Z ~ bind_sub Q0 Q1 & F). gen F.
induction WT; intros F EQ Ok; subst; simpl subst_tt; auto.
case_var*.
apply_empty* wft_weaken.
destruct (binds_concat_inv H) as [?|[? ?]].
apply (@wft_var (subst_tt Z P T0) (subst_tt Z P T1)).
apply~ binds_concat_right.
unsimpl_map_bind_sub. apply binds_map.
assumption.
destruct (binds_push_inv H1) as [[? ?]|[? ?]].
subst. false~.
applys wft_var. apply* binds_concat_left.
apply_fresh* wft_all as Y.
unsimpl ((subst_tb Z P) (bind_sub T0 T1)).
lets: wft_type.
rewrite* subst_tt_open_tt_var.
apply_ih_map_bind* H0.
Qed.
Lemma wft_subst_tb_empty : forall F Q0 Q1 Z P T,
wft (Z ~ bind_sub Q0 Q1 & F) T ->
wft empty P ->
ok (map (subst_tb Z P) F) ->
wft (map (subst_tb Z P) F) (subst_tt Z P T).
Proof.
intros.
lets A: (@wft_subst_tb F Q0 Q1 empty Z P T).
repeat rewrite concat_empty_l in *. apply* A.
Qed.
(** Through type reduction *)
Lemma wft_open : forall E U T0 T1 T2,
ok E ->
wft E (typ_all T0 T1 T2) ->
wft E U ->
wft E (open_tt T2 U).
Proof.
introv Ok WA WU. inversions WA. pick_fresh X.
auto* wft_type. rewrite* (@subst_tt_intro X).
lets K: (@wft_subst_tb empty).
specializes_vars K. clean_empty K. apply* K.
(* todo: apply empty ? *)
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 a subtyping assumption in a well-formed environments *)
Lemma wft_from_env_has_sub : forall x U0 U1 E,
okt E -> binds x (bind_sub U0 U1) E -> wft E U0 /\ wft E U1.
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.
inversions H4. split; apply_empty* wft_weaken.
split; apply_empty* wft_weaken.
destruct (eq_push_inv H) as [? [? ?]]. subst. clear H.
destruct (binds_push_inv B) as [[? ?]|[? ?]]. subst.
inversions H3.
split; apply_empty* wft_weaken.
Qed.
(** Extraction from a typing assumption in a well-formed environments *)
Lemma wft_from_env_has_typ : forall x U E,
okt E -> binds x (bind_typ 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.
inversions H4.
apply_empty* wft_weaken.
destruct (eq_push_inv H) as [? [? ?]]. subst. clear H.
destruct (binds_push_inv B) as [[? ?]|[? ?]]. subst.
inversions H3. apply_empty* wft_weaken.
apply_empty* wft_weaken.
Qed.
(** Extraction from a well-formed environment *)
Lemma wft_from_okt_typ : forall x T E,
okt (E & x ~ bind_typ T) -> wft E T.
Proof.
intros. inversions* H.
false (empty_push_inv H1).
destruct (eq_push_inv H0) as [? [? ?]]. false.
destruct (eq_push_inv H0) as [? [? ?]]. inversions~ H4.
Qed.
Lemma wft_from_okt_sub : forall x T0 T1 E,
okt (E & x ~ bind_sub T0 T1) -> wft E T0 /\ wft E T1.
Proof.
intros. inversions* H.
false (empty_push_inv H1).
destruct (eq_push_inv H0) as [? [? ?]]. inversions~ H5.
destruct (eq_push_inv H0) as [? [? ?]]. false.
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_typ wft_from_okt_sub.
Hint Immediate wft_from_env_has_sub wft_from_env_has_typ.
Hint Resolve wft_subst_tb wft_subst_tb_empty.
(* ********************************************************************** *)
(** ** Properties of well-formedness of an environment *)
(** Inversion lemma *)
Lemma okt_push_inv : forall E X B,
okt (E & X ~ B) -> (exists T0 T1, B = bind_sub T0 T1) \/ (exists T, B = bind_typ T).
Proof.
introv O. inverts O.
false* empty_push_inv.
lets (?&?&?): (eq_push_inv H). subst*.
lets (?&?&?): (eq_push_inv H). subst*.
Qed.
Lemma okt_push_sub_inv : forall E X T0 T1,
okt (E & X ~ bind_sub T0 T1) -> okt E /\ wft E T0 /\ wft E T1 /\ X # E.
Proof.
introv O. inverts O.
false* empty_push_inv.
lets (?&M&?): (eq_push_inv H). subst. inverts~ M.
lets (?&?&?): (eq_push_inv H). false.
Qed.
Lemma okt_push_sub_type : forall E X T0 T1,
okt (E & X ~ bind_sub T0 T1) -> type T0 /\ type T1.
Proof. intros. split; applys wft_type; forwards*: okt_push_sub_inv. Qed.
Lemma okt_push_typ_inv : forall E x T,
okt (E & x ~ bind_typ T) -> okt E /\ wft E T /\ x # E.
Proof.
introv O. inverts O.
false* empty_push_inv.
lets (?&?&?): (eq_push_inv H). false.
lets (?&M&?): (eq_push_inv H). subst. inverts~ M.
Qed.
Lemma okt_push_typ_type : forall E X T,
okt (E & X ~ bind_typ T) -> type T.
Proof. intros. applys wft_type. forwards*: okt_push_typ_inv. Qed.
Hint Immediate okt_push_sub_type okt_push_typ_type.
(** Through narrowing *)
Lemma okt_narrow : forall V0 V1 (E F:env) U0 U1 X,
okt (E & X ~ bind_sub V0 V1 & F) ->
wft E U0 -> wft E U1 ->
okt (E & X ~ bind_sub U0 U1 & F).
Proof.
introv O W0 W1. induction F using env_ind.
rewrite concat_empty_r in *. lets*: (okt_push_sub_inv O).
rewrite concat_assoc in *.
lets [[T0 [T1 Hsub]] | [T Htyp]]: (okt_push_inv O); subst.
lets (?&?&?&?): (okt_push_sub_inv O).
applys~ okt_sub; applys* wft_narrow.
lets (?&?&?): (okt_push_typ_inv O).
applys~ okt_typ. applys* wft_narrow.
Qed.
(** Through strengthening *)
Lemma okt_strengthen : forall x T (E F:env),
okt (E & x ~ bind_typ T & F) ->
okt (E & F).
Proof.
introv O. induction F using env_ind.
rewrite concat_empty_r in *. lets*: (okt_push_typ_inv O).
rewrite concat_assoc in *.
lets [[U0 [U1 Hsub]] | [U Htyp]]: (okt_push_inv O); subst.
lets (?&?&?&?): (okt_push_sub_inv O).
applys~ okt_sub; applys* wft_strengthen.
lets (?&?&?): (okt_push_typ_inv O).
applys~ okt_typ. applys* wft_strengthen.