forked from TiarkRompf/minidot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dot_soundness_alt.v
1250 lines (1185 loc) · 54.2 KB
/
dot_soundness_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
Require Import dot.
(* :! -- directly invertible value typing *)
Inductive vtp(*possible types*) : nat(*pack count*) -> venv -> id -> ty -> nat(*size*) -> Prop :=
| vtp_top: forall m G1 x n1,
x < length G1 ->
vtp m G1 x TTop (S n1)
| vtp_typ: forall m G1 x l ds TX T1 T2 n1 n2,
index x G1 = Some (vobj ds) ->
index l (dms_to_list ds) = Some (dty TX) ->
stp [] G1 T1 TX n1 ->
stp [] G1 TX T2 n2 ->
vtp m G1 x (TTyp l T1 T2) (S (n1+n2))
| vtp_fun: forall m G1 x l ds dsx OT1 OT2 OT1x OT2x T1 T2 T3 T4 T2' T4' t T1x T2x tx T' T2x' n1 n2 n3 n4,
index x G1 = Some (vobj ds) ->
index l (dms_to_list ds) = Some (dfun OT1 OT2 t) ->
eq_some OT1 T1 ->
eq_some OT2 T2 ->
subst_dms x dsx = ds ->
dms_has_type [T'] G1 dsx T' n4 ->
subst_dm x (dfun OT1x OT2x tx) = (dfun OT1 OT2 t) ->
eq_some OT1x T1x ->
eq_some OT2x T2x ->
substt x T1x = T1 ->
substt x T2x = T2 ->
T2x' = (open 0 (TVar false 1) T2x) ->
has_type [T1x;T'] G1 tx T2x' n3 ->
stp [] G1 T3 T1 n1 ->
T2' = (open 0 (TVar false 0) T2) ->
T4' = (open 0 (TVar false 0) T4) ->
closed 0 (length G1) 1 T2 ->
closed 0 (length G1) 1 T4 ->
stp [T3] G1 T2' T4' n2 ->
vtp m G1 x (TFun l T3 T4) (S (n1+n2+n3+n4))
| vtp_bind: forall m G1 x T2 n1,
vtp m G1 x (open 0 (TVar true x) T2) n1 ->
closed 0 (length G1) 1 T2 ->
vtp (S m) G1 x (TBind T2) (S (n1))
| vtp_sel: forall m G1 x y l ds TX n1,
index y G1 = Some (vobj ds) ->
index l (dms_to_list ds) = Some (dty TX) ->
vtp m G1 x TX n1 ->
vtp m G1 x (TSel (TVar true y) l) (S (n1))
| vtp_and: forall m m1 m2 G1 x T1 T2 n1 n2,
vtp m1 G1 x T1 n1 ->
vtp m2 G1 x T2 n2 ->
m1 <= m -> m2 <= m ->
vtp m G1 x (TAnd T1 T2) (S (n1+n2))
| vtp_or1: forall m m1 m2 G1 x T1 T2 n1,
vtp m1 G1 x T1 n1 ->
closed 0 (length G1) 0 T2 ->
m1 <= m -> m2 <= m ->
vtp m G1 x (TOr T1 T2) (S (n1))
| vtp_or2: forall m m1 m2 G1 x T1 T2 n1,
vtp m1 G1 x T2 n1 ->
closed 0 (length G1) 0 T1 ->
m1 <= m -> m2 <= m ->
vtp m G1 x (TOr T1 T2) (S (n1))
.
Definition vtpd m G1 x T1 := exists n, vtp m G1 x T1 n.
Definition vtpdd m G1 x T1 := exists m1 n, vtp m1 G1 x T1 n /\ m1 <= m.
Hint Constructors vtp.
Ltac euv := match goal with
| H: vtpd _ _ _ _ |- _ => destruct H as [? H]
| H: vtpdd _ _ _ _ |- _ => destruct H as [? [? [H ?]]]
end.
Hint Unfold vtpd.
Hint Unfold vtpdd.
Lemma vtp_all_extend: forall ni,
(forall m v1 x G1 T2 n,
vtp m G1 x T2 n -> n < ni ->
vtp m (v1::G1) x T2 n).
Proof.
intros n. induction n. repeat split; intros; omega.
intros; inversion H.
(* vtp *)
- econstructor. simpl. eauto.
- econstructor. eapply index_extend. eauto. eauto. eapply stp_extend. eauto. eapply stp_extend. eauto.
- econstructor. eapply index_extend. eauto. eauto. eauto. eauto. eauto.
eapply dms_has_type_extend. eauto. eauto. eauto. eauto. eauto. eauto. eauto.
eapply has_type_extend. eauto. eapply stp_extend. eauto. eauto. eauto.
eapply closed_extend. eauto. eapply closed_extend. eauto. eapply stp_extend. eauto.
- econstructor. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- econstructor. eapply index_extend. eauto. eauto. eapply IHn. eauto. omega.
- econstructor. eapply IHn. eauto. omega. eapply IHn. eauto. omega. eauto. eauto.
- econstructor. eapply IHn. eauto. omega. eapply closed_extend. eauto. omega. eauto.
- eapply vtp_or2. eapply IHn. eauto. omega. eapply closed_extend. eauto. omega. eauto.
Qed.
Lemma vtp_all_closed: forall ni,
(forall m x G1 T2 n,
vtp m G1 x T2 n -> n < ni ->
x < length G1) /\
(forall m x G1 T2 n,
vtp m G1 x T2 n -> n < ni ->
closed 0 (length G1) 0 T2).
Proof.
intros n. induction n. repeat split; intros; omega.
repeat split; intros; inversion H; destruct IHn as [IHV1 IHV2].
(* vtp left *)
- eauto.
- eapply index_max. eauto.
- eapply index_max. eauto.
- eapply IHV1. eauto. omega.
- eapply IHV1. eauto. omega.
- eapply IHV1. eauto. omega.
- eapply IHV1. eauto. omega.
- eapply IHV1. eauto. omega.
(* vtp right *)
- econstructor.
- change 0 with (length ([]:tenv)) at 1. econstructor. eapply stp_closed1. eauto. eapply stp_closed2. eauto.
- change 0 with (length ([]:tenv)) at 1. econstructor. eapply stp_closed1. eauto. eauto.
- econstructor. eauto.
- econstructor. econstructor. eapply index_max. eauto.
- econstructor. eapply IHV2. eauto. omega. eapply IHV2. eauto. omega.
- econstructor. eapply IHV2. eauto. omega. eauto.
- econstructor. eauto. eapply IHV2. eauto. omega.
Qed.
Lemma vtp_extend : forall m v1 x G1 T2 n,
vtp m G1 x T2 n ->
vtp m (v1::G1) x T2 n.
Proof. intros. eapply vtp_all_extend. eauto. eauto. Qed.
Lemma vtp_closed: forall m G1 x T2 n1,
vtp m G1 x T2 n1 ->
closed 0 (length G1) 0 T2.
Proof. intros. eapply vtp_all_closed. eauto. eauto. Qed.
Lemma vtp_closed1: forall m G1 x T2 n1,
vtp m G1 x T2 n1 ->
x < length G1.
Proof. intros. eapply vtp_all_closed. eauto. eauto. Qed.
Lemma stp_subst_narrow0: forall n, forall GH G1 T1 T2 TX x n2,
stp (GH++[TX]) G1 T1 T2 n2 -> x < length G1 -> n2 < n ->
(forall GH (T3 : ty) (n1 : nat),
htp (GH++[TX]) G1 0 T3 n1 -> n1 < n ->
exists m2, vtpd m2 G1 x (substt x T3)) ->
stpd (map (substt x) GH) G1 (substt x T1) (substt x T2).
Proof.
intros n. induction n. intros. omega.
intros ? ? ? ? ? ? ? ? ? ? narrowX.
(* helper lemma for htp *)
assert (forall ni n2, forall GH T2 xi,
htp (GH ++ [TX]) G1 xi T2 n2 -> xi <> 0 -> n2 < ni -> ni < S n ->
htpd (map (substt x) GH) G1 (xi-1) (substt x T2)) as htp_subst_narrow02. {
induction ni. intros. omega.
intros. inversion H2.
+ (* var *) subst.
repeat eexists. eapply htp_var. eapply index_subst1. eauto. eauto.
eapply closed_subst0. eapply closed_upgrade_gh. eauto. omega. eauto.
+ (* bind *) subst.
assert (htpd (map (substt x) (GH0)) G1 (xi-1) (substt x (TBind TX0))) as BB.
eapply IHni. eapply H6. eauto. omega. omega.
rewrite subst_open5.
eu. repeat eexists. eapply htp_unpack. eauto.
eapply closed_upgrade_gh. eapply closed_subst1. eauto. eauto. eauto. omega.
apply []. eauto.
+ (* sub *) subst.
assert (exists GL0, GL = GL0 ++ [TX] /\ GH0 = GU ++ GL0) as A. eapply gh_match1. eauto. omega.
destruct A as [GL0 [? ?]]. subst GL.
assert (htpd (map (substt x) GH0) G1 (xi-1) (substt x T3)) as AA.
eapply IHni. eauto. eauto. omega. omega.
assert (stpd (map (substt x) GL0) G1 (substt x T3) (substt x T0)) as BB.
eapply IHn. eauto. eauto. omega. { intros. eapply narrowX. eauto. eauto. }
eu. eu. repeat eexists. eapply htp_sub. eauto. eauto.
(* - *)
rewrite map_length. rewrite app_length in H8. simpl in H8. unfold id in *. omega.
subst GH0. rewrite map_app. eauto.
+ (* andi *) subst.
assert (htpd (map (substt x) GH0) G1 (xi - 1) (TAnd (substt x T3) (substt x T4))). {
eapply IHni in H6. eu. eapply IHni in H7. eu.
exists (S(x0+x1)).
eapply htp_andi. eapply H6. eapply H7. eauto. omega. omega. eauto. omega. omega.
}
eauto.
}
(* special case *)
assert (forall ni n2, forall T0 T2,
htp (T0 :: GH ++ [TX]) G1 (length (GH ++ [TX])) T2 n2 -> n2 < ni -> ni < S n ->
htpd (map (substt x) (T0::GH)) G1 (length GH) (substt x T2)) as htp_subst_narrow0. {
intros.
rewrite app_comm_cons in H2.
remember (T0::GH) as GH1. remember (length (GH ++ [TX])) as xi.
rewrite app_length in Heqxi. simpl in Heqxi.
assert (length GH = xi-1) as R. omega.
rewrite R. eapply htp_subst_narrow02. eauto. omega. eauto. eauto.
}
(* main logic *)
inversion H.
- Case "bot". subst.
eapply stpd_bot; eauto. rewrite map_length. simpl. eapply closed_subst0.
rewrite app_length in H2. simpl in H2. eapply H2. eauto.
- Case "top". subst.
eapply stpd_top; eauto. rewrite map_length. simpl. eapply closed_subst0.
rewrite app_length in H2. simpl in H2. eapply H2. eauto.
- Case "fun". subst.
eapply stpd_fun. eauto. eauto.
rewrite map_length. eapply closed_subst0. rewrite app_length in *. simpl in *. eauto. omega.
rewrite map_length. eapply closed_subst0. rewrite app_length in *. simpl in *. eauto. omega.
eapply IHn; eauto. omega.
rewrite <- subst_open_commute_z. rewrite <- subst_open_commute_z.
specialize (IHn (T4::GH)). simpl in IHn.
unfold substt in IHn at 2. unfold substt in IHn at 3. unfold substt in IHn at 3.
simpl in IHn. eapply IHn.
rewrite map_length. rewrite app_length in *. eassumption.
omega. omega. eauto.
- Case "typ". subst.
eapply stpd_typ. eapply IHn; eauto. omega. eapply IHn; eauto. omega.
- Case "ssel1". subst.
assert (substt x T2 = T2) as R. eapply subst_closed_id. eapply stpd_closed2 with (GH:=[]). eauto.
eexists. eapply stp_strong_sel1. eauto. eauto. rewrite R. eauto.
- Case "ssel2". subst.
assert (substt x T1 = T1) as R. eapply subst_closed_id. eapply stpd_closed1 with (GH:=[]). eauto.
eexists. eapply stp_strong_sel2. eauto. eauto. rewrite R. eauto.
- Case "sel1". subst. (* invert htp to vtp and create strong_sel node *)
case_eq (beq_nat x0 0); intros E.
+ assert (x0 = 0). eapply beq_nat_true_iff. eauto. subst x0.
assert (exists m0, vtpd m0 G1 x (substt x (TTyp l TBot T2))) as A. eapply narrowX. eauto. omega.
destruct A as [? A]. euv. inversion A. subst.
repeat eexists. eapply stp_strong_sel1. eauto. eauto. unfold substt.
eauto.
+ assert (x0 <> 0). eapply beq_nat_false_iff. eauto.
eapply htp_subst_narrow02 in H2.
eu. repeat eexists. unfold substt. simpl. rewrite E. eapply stp_sel1. eapply H2. eauto. eauto. eauto.
- Case "sel2". subst. (* invert htp to vtp and create strong_sel node *)
case_eq (beq_nat x0 0); intros E.
+ assert (x0 = 0). eapply beq_nat_true_iff. eauto. subst x0.
assert (exists m0, vtpd m0 G1 x (substt x (TTyp l T1 TTop))) as A. eapply narrowX. eauto. omega.
destruct A as [? A]. euv. inversion A. subst.
repeat eexists. eapply stp_strong_sel2. eauto. eauto. unfold substt.
eauto.
+ assert (x0 <> 0). eapply beq_nat_false_iff. eauto.
eapply htp_subst_narrow02 in H2.
eu. repeat eexists. unfold substt. simpl. rewrite E. eapply stp_sel2. eapply H2. eauto. eauto. eauto.
- Case "selx".
eexists. eapply stp_selx. subst. eapply vr_closed_subst. rewrite app_length in H2. simpl in H2. rewrite map_length. eauto. eauto.
- Case "bind1".
assert (stpd (map (substt x) (T1'::GH)) G1 (substt x T1') (substt x T2)) as A.
eapply IHn; eauto. omega.
eu. repeat eexists. eapply stp_bind1. eapply A.
simpl. subst T1'. fold subst. eapply subst_open4.
fold subst. eapply closed_subst0. rewrite app_length in H4. simpl in H4. rewrite map_length. eauto. eauto.
eapply closed_subst0. rewrite map_length. rewrite app_length in H5. simpl in H5. eauto. eauto.
- Case "bindx".
assert (stpd (map (substt x) (T1'::GH)) G1 (substt x T1') (substt x T2')) as A.
eapply IHn; eauto. omega.
eu. repeat eexists. eapply stp_bindx. eapply A.
subst T1'. fold subst. eapply subst_open4.
subst T2'. fold subst. eapply subst_open4.
rewrite app_length in H5. simpl in H5. eauto. eapply closed_subst0. rewrite map_length. eauto. eauto.
rewrite app_length in H6. simpl in H6. eauto. eapply closed_subst0. rewrite map_length. eauto. eauto.
- Case "and_bind". subst.
rewrite app_length in H2, H3. simpl in H2, H3.
eapply stpd_and_bind; eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
- Case "and_typ". subst.
rewrite app_length in H2, H3, H4, H5. simpl in H2, H3, H4, H5.
eapply stpd_and_typ; eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
- Case "or_typ". subst.
rewrite app_length in H2, H3, H4, H5. simpl in H2, H3, H4, H5.
eapply stpd_or_typ; eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
+ rewrite map_length. eapply closed_subst0. eauto. eauto.
- Case "and11".
assert (stpd (map (substt x) GH) G1 (substt x T0) (substt x T2)). eapply IHn. eauto. eauto. omega. eauto.
eu. eexists. eapply stp_and11. eauto. eapply closed_subst0. rewrite app_length in H3. rewrite map_length. eauto. eauto.
- Case "and12".
assert (stpd (map (substt x) GH) G1 (substt x T3) (substt x T2)). eapply IHn. eauto. eauto. omega. eauto.
eu. eexists. eapply stp_and12. eauto. eapply closed_subst0. rewrite app_length in H3. rewrite map_length. eauto. eauto.
- Case "and2".
assert (stpd (map (substt x) GH) G1 (substt x T1) (substt x T0)). eapply IHn. eauto. eauto. omega. eauto.
assert (stpd (map (substt x) GH) G1 (substt x T1) (substt x T3)). eapply IHn. eauto. eauto. omega. eauto.
eu. eu. eexists. eapply stp_and2. eauto. eauto.
- Case "or21".
assert (stpd (map (substt x) GH) G1 (substt x T1) (substt x T0)). eapply IHn. eauto. eauto. omega. eauto.
eu. eexists. eapply stp_or21. eauto. eapply closed_subst0. rewrite app_length in H3. rewrite map_length. eauto. eauto.
- Case "or22".
assert (stpd (map (substt x) GH) G1 (substt x T1) (substt x T3)). eapply IHn. eauto. eauto. omega. eauto.
eu. eexists. eapply stp_or22. eauto. eapply closed_subst0. rewrite app_length in H3. rewrite map_length. eauto. eauto.
- Case "or1".
assert (stpd (map (substt x) GH) G1 (substt x T0) (substt x T2)). eapply IHn. eauto. eauto. omega. eauto.
assert (stpd (map (substt x) GH) G1 (substt x T3) (substt x T2)). eapply IHn. eauto. eauto. omega. eauto.
eu. eu. eexists. eapply stp_or1. eauto. eauto.
- Case "trans".
assert (stpd (map (substt x) GH) G1 (substt x T1) (substt x T3)).
eapply IHn; eauto. omega.
assert (stpd (map (substt x) GH) G1 (substt x T3) (substt x T2)).
eapply IHn; eauto. omega.
eu. eu. repeat eexists. eapply stp_trans. eauto. eauto.
Grab Existential Variables.
apply 0. apply 0.
Qed.
Lemma stp_subst_narrowX: forall ml, forall nl, forall m GH G1 T2 TX x n1 n2,
vtp m G1 x (substt x TX) n1 ->
htp (GH++[TX]) G1 0 T2 n2 -> x < length G1 -> m < ml -> n2 < nl ->
(forall (m0 : nat) (G1 : venv) x (T2 T3 : ty) (n1 n2 : nat),
vtp m0 G1 x (substt x T2) n1 ->
stp [] G1 (substt x T2) (substt x T3) n2 -> m0 <= m ->
vtpdd m0 G1 x (substt x T3)) ->
vtpdd m G1 x (substt x T2). (* decrease b/c transitivity *)
Proof.
intros ml. (* induction ml. intros. omega. *)
intros nl. induction nl. intros. omega.
intros.
inversion H0.
- Case "var". subst.
assert (T2 = TX). eapply index_hit0. eauto.
subst T2.
repeat eexists. eauto. eauto.
- Case "unpack". subst.
assert (vtpdd m G1 x (substt x (TBind TX0))) as A.
eapply IHnl. eauto. eauto. eauto. eauto. omega. eauto.
destruct A as [? [? [A ?]]]. inversion A. subst.
rewrite subst_open_commute0b. repeat eexists. eauto. omega.
- Case "sub". subst.
destruct GL.
assert (vtpdd m G1 x (substt x T1)) as A.
eapply IHnl. eauto. eauto. eauto. eauto. omega. eauto.
euv.
assert (stpd [] G1 (substt x T1) (substt x T2)) as B.
erewrite subst_closed_id. erewrite subst_closed_id. eexists. eassumption.
eapply stp_closed2 in H6. simpl in H6. eapply H6.
eapply stp_closed1 in H6. simpl in H6. eapply H6.
simpl in B. eu.
assert (vtpdd x0 G1 x (substt x T2)).
eapply H4. eauto. eauto. eauto.
euv. repeat eexists. eauto. omega.
assert (length GL = 0) as LenGL. simpl in *. omega.
assert (GL = []). destruct GL. reflexivity. simpl in LenGL. inversion LenGL.
subst GL.
assert (TX = t). eapply proj2. apply app_inj_tail. eassumption.
subst t.
assert (vtpdd m G1 x (substt x T1)) as A.
eapply IHnl. eauto. eauto. eauto. eauto. omega. eauto.
euv.
assert (stpd (map (substt x) []) G1 (substt x T1) (substt x T2)) as B.
eapply stp_subst_narrow0. eauto. eauto. eauto. {
intros. eapply IHnl in H. euv. repeat eexists. eauto. eauto. eauto. eauto. omega. eauto.
}
simpl in B. eu.
assert (vtpdd x0 G1 x (substt x T2)).
eapply H4. eauto. eauto. eauto.
euv. repeat eexists. eauto. omega.
- Case "andi". subst.
assert (vtpdd m G1 x (substt x T0)) as A. {
eapply IHnl. eauto. eauto. eauto. eauto. omega. eauto.
}
euv.
assert (vtpdd m G1 x (substt x T1)) as B. {
eapply IHnl. eauto. eauto. eauto. eauto. omega. eauto.
}
euv.
exists m. exists (S(x3+x1)).
split.
+ eapply vtp_and; fold subst. eapply B. eapply A. omega. omega.
+ eauto.
Qed.
(* possible types closure *)
Lemma vtp_widen: forall l, forall n, forall k, forall m1 G1 x T2 T3 n1 n2,
vtp m1 G1 x T2 n1 ->
stp [] G1 T2 T3 n2 ->
m1 < l -> n2 < n -> n1 < k ->
vtpdd m1 G1 x T3.
Proof.
intros l. induction l. intros. solve by inversion.
intros n. induction n. intros. solve by inversion.
intros k. induction k; intros. solve by inversion.
inversion H.
- Case "top". inversion H0; subst; invty.
+ SCase "top". repeat eexists; eauto.
+ SCase "ssel2".
assert (vtpdd m1 G1 x TX). eapply IHn; eauto. omega.
euv. repeat eexists. eapply vtp_sel. eauto. eauto. eauto. eauto.
+ SCase "sel2".
eapply stp_closed2 in H0. simpl in H0. inversion H0. inversion H9. omega.
+ SCase "and".
assert (vtpdd m1 G1 x T1). eapply IHn; eauto. omega. euv.
assert (vtpdd m1 G1 x T0). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_and; eauto. eauto.
+ SCase "or1".
assert (vtpdd m1 G1 x T1). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or1; eauto. eauto.
+ SCase "or2".
assert (vtpdd m1 G1 x T0). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or2; eauto. eauto.
+ SCase "trans".
assert (vtpdd m1 G1 x T0) as LHS. eapply IHn. eauto. eauto. eauto. omega. eauto. euv.
assert (vtpdd x0 G1 x T3) as BB. eapply IHn. eapply LHS. eauto. omega. omega. eauto. euv.
repeat eexists. eauto. omega.
- Case "typ". inversion H0; subst; invty.
+ SCase "top". repeat eexists. eapply vtp_top. eapply index_max. eauto. eauto.
+ SCase "typ". invty. subst.
repeat eexists. eapply vtp_typ. eauto. eauto.
eapply stp_trans. eauto. eauto.
eapply stp_trans. eauto. eauto.
eauto.
+ SCase "sel2".
assert (vtpdd m1 G1 x TX0). eapply IHn; eauto. omega.
euv. repeat eexists. eapply vtp_sel. eauto. eauto. eauto. eauto.
+ SCase "sel2".
eapply stp_closed2 in H0. simpl in H0. inversion H0. inversion H12. omega.
+ SCase "and".
assert (vtpdd m1 G1 x T4). eapply IHn; eauto. omega. euv.
assert (vtpdd m1 G1 x T5). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_and; eauto. eauto.
+ SCase "or1".
assert (vtpdd m1 G1 x T4). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or1; eauto. eauto.
+ SCase "or2".
assert (vtpdd m1 G1 x T5). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or2; eauto. eauto.
+ SCase "trans".
assert (vtpdd m1 G1 x T5) as LHS. eapply IHn. eauto. eauto. eauto. omega. eauto. euv.
assert (vtpdd x0 G1 x T3) as BB. eapply IHn. eapply LHS. eauto. omega. omega. eauto. euv.
repeat eexists. eauto. omega.
- Case "fun". inversion H0; subst; invty.
+ SCase "top". repeat eexists. eapply vtp_top. eapply index_max. eauto. eauto.
+ SCase "fun". invty. subst.
remember (substt x T2x) as T0.
assert (stpd [T8] G1 (open 0 (TVar false 0) T0) (open 0 (TVar false 0) T5)) as A. {
eapply stp_narrow_norec. simpl. eassumption. simpl. eassumption.
}
destruct A as [na A].
repeat eexists. eapply vtp_fun. eauto. eauto. eauto. eauto. eauto. eauto. eauto.
eauto. eauto. eauto. eauto. eauto. eauto.
eapply stp_trans. eauto. eauto. eauto. eauto. eauto. eauto. eauto. reflexivity.
+ SCase "sel2".
assert (vtpdd m1 G1 x TX). eapply IHn; eauto. omega.
euv. repeat eexists. eapply vtp_sel. eauto. eauto. eauto. eauto.
+ SCase "sel2".
eapply stp_closed2 in H0. simpl in H0. inversion H0. subst. inversion H18. omega.
+ SCase "and".
assert (vtpdd m1 G1 x T6). eapply IHn; eauto. omega. euv.
assert (vtpdd m1 G1 x T7). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_and; eauto. eauto.
+ SCase "or1".
assert (vtpdd m1 G1 x T6). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or1; eauto. eauto.
+ SCase "or2".
assert (vtpdd m1 G1 x T7). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or2; eauto. eauto.
+ SCase "trans".
assert (vtpdd m1 G1 x T7) as LHS. eapply IHn. eauto. eauto. eauto. omega. eauto. euv.
assert (vtpdd x0 G1 x T3) as BB. eapply IHn. eapply LHS. eauto. omega. omega. eauto. euv.
repeat eexists. eauto. omega.
- Case "bind".
inversion H0; subst; invty.
+ SCase "top". repeat eexists. eapply vtp_top. eapply vtp_closed1. eauto. eauto.
+ SCase "sel2".
assert (vtpdd (S m) G1 x TX). eapply IHn; eauto. omega.
euv. repeat eexists. eapply vtp_sel. eauto. eauto. eauto. eauto.
+ SCase "sel2".
eapply stp_closed2 in H0. simpl in H0. inversion H0. inversion H10. omega.
+ SCase "bind1".
invty. subst.
remember (TVar false (length [])) as VZ.
remember (TVar true x) as VX.
(* left *)
assert (vtpd m G1 x (open 0 VX T0)) as LHS. eexists. eassumption.
euv.
(* right *)
assert (substt x (open 0 VZ T0) = (open 0 VX T0)) as R. unfold substt. subst. eapply subst_open_commute0. eauto.
assert (substt x T3 = T3) as R1. eapply subst_closed_id. eauto.
assert (vtpdd m G1 x (substt x T3)) as BB. {
eapply stp_subst_narrowX. rewrite <-R in LHS. eapply LHS.
instantiate (2:=nil). simpl.
eapply htp_sub. eapply htp_var. simpl. reflexivity.
eapply stp_closed1 in H11. simpl in H11. eapply H11. eapply H11. eauto.
instantiate (1:=nil). simpl. reflexivity. eapply vtp_closed1. eauto. eauto. eauto.
{ intros. eapply IHl. eauto. eauto. omega. eauto. eauto. }
}
rewrite R1 in BB.
euv. repeat eexists. eauto. omega.
+ SCase "bindx".
invty. subst.
remember (TVar false (length [])) as VZ.
remember (TVar true x) as VX.
(* left *)
assert (vtpd m G1 x (open 0 VX T0)) as LHS. eexists. eassumption.
euv.
(* right *)
assert (substt x (open 0 VZ T0) = (open 0 VX T0)) as R. unfold substt. subst. eapply subst_open_commute0. eauto.
assert (vtpdd m G1 x (substt x (open 0 VZ T4))) as BB. {
eapply stp_subst_narrowX. rewrite <-R in LHS. eapply LHS.
instantiate (2:=nil). simpl.
eapply htp_sub. eapply htp_var. simpl. reflexivity.
eapply stp_closed1 in H11. simpl in H11. eapply H11. eapply H11. eauto.
instantiate (1:=nil). simpl. reflexivity. eapply vtp_closed1. eauto. eauto. eauto.
{ intros. eapply IHl. eauto. eauto. omega. eauto. eauto. }
}
unfold substt in BB. subst. erewrite subst_open_commute0 in BB.
clear R.
euv. repeat eexists. eapply vtp_bind. eauto. eauto. omega. eauto. (* enough slack to add bind back *)
+ SCase "and".
assert (vtpdd (S m) G1 x T1). eapply IHn; eauto. omega. euv.
assert (vtpdd (S m) G1 x T4). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_and; eauto. eauto.
+ SCase "or1".
assert (vtpdd (S m) G1 x T1). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or1; eauto. eauto.
+ SCase "or2".
assert (vtpdd (S m) G1 x T4). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or2; eauto. eauto.
+ SCase "trans".
assert (vtpdd (S m) G1 x T4) as LHS. eapply IHn. eauto. eauto. eauto. omega. eauto. euv.
assert (vtpdd x0 G1 x T3) as BB. eapply IHn. eapply LHS. eauto. omega. omega. eauto. euv.
repeat eexists. eauto. omega.
- Case "ssel2". subst. inversion H0; subst; invty.
+ SCase "top". repeat eexists. eapply vtp_top. eapply vtp_closed1. eauto. eauto.
+ SCase "ssel1". index_subst. index_subst. eapply IHn. eapply H6. eauto. eauto. omega. eauto.
+ SCase "ssel2".
assert (vtpdd m1 G1 x TX0). eapply IHn; eauto. omega.
euv. repeat eexists. eapply vtp_sel. eauto. eauto. eauto. eauto.
+ SCase "sel1".
assert (closed (length ([]:tenv)) (length G1) 0 (TSel (TVar false x0) l1)) as A.
eapply stpd_closed2. eauto.
simpl in A. inversion A. inversion H12. omega.
+ SCase "selx".
eauto.
+ SCase "and".
assert (vtpdd m1 G1 x T1). eapply IHn; eauto. omega. euv.
assert (vtpdd m1 G1 x T2). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_and; eauto. eauto.
+ SCase "or1".
assert (vtpdd m1 G1 x T1). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or1; eauto. eauto.
+ SCase "or2".
assert (vtpdd m1 G1 x T2). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or2; eauto. eauto.
+ SCase "trans".
assert (vtpdd m1 G1 x T2) as LHS. eapply IHn. eauto. eauto. eauto. omega. eauto. euv.
assert (vtpdd x0 G1 x T3) as BB. eapply IHn. eapply LHS. eauto. omega. omega. eauto. euv.
repeat eexists. eauto. omega.
- Case "and". subst. inversion H0; subst; invty.
+ SCase "top". repeat eexists. eapply vtp_top. eapply vtp_closed1. eauto. eauto.
+ SCase "sel2".
assert (vtpdd m1 G1 x TX). eapply IHn; eauto. omega.
euv. repeat eexists. eapply vtp_sel. eauto. eauto. eauto. eauto.
+ SCase "sel2".
eapply stp_closed2 in H0. simpl in H0. inversion H0. inversion H13. omega.
+ SCase "and_bind".
assert (vtpdd (m1-1) G1 x (open 0 (TVar true x) (TAnd T2 T4))) as Hx. {
replace (open 0 (TVar true x) (TAnd T2 T4)) with
(TAnd (open 0 (TVar true x) T2) (open 0 (TVar true x) T4)) by eauto.
inversion H. inversion H11. inversion H17. subst.
exists (m1-1). exists (S (n5+n6)).
repeat eexists.
eapply vtp_and.
eapply H21. eapply H28. omega. omega. eauto.
}
euv. repeat eexists. eapply vtp_bind. eauto. eauto.
destruct m1.
* inversion H5. subst. inversion H7.
* omega.
+ SCase "and_typ".
inversion H4. inversion H5. subst.
rewrite H26 in H13. inversion H13. subst.
rewrite H30 in H19. inversion H19. subst.
repeat eexists. eapply vtp_typ. eauto. eauto.
eapply stp_or1. eauto. eauto.
eapply stp_and2. eauto. eauto. eauto.
+ SCase "and11". eapply IHn in H4. euv. repeat eexists. eauto. omega. eauto. omega. omega. eauto.
+ SCase "and12". eapply IHn in H5. euv. repeat eexists. eauto. omega. eauto. omega. omega. eauto.
+ SCase "and".
assert (vtpdd m1 G1 x T2). eapply IHn; eauto. omega. euv.
assert (vtpdd m1 G1 x T4). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_and; eauto. eauto.
+ SCase "or1".
assert (vtpdd m1 G1 x T2). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or1; eauto. eauto.
+ SCase "or2".
assert (vtpdd m1 G1 x T4). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or2; eauto. eauto.
+ SCase "trans".
assert (vtpdd m1 G1 x T4) as LHS. eapply IHn. eauto. eauto. eauto. omega. eauto. euv.
assert (vtpdd x0 G1 x T3) as BB. eapply IHn. eapply LHS. eauto. omega. omega. eauto. euv.
repeat eexists. eauto. omega.
- Case "or1". subst. inversion H0; subst; invty.
+ SCase "top". repeat eexists. eapply vtp_top. eapply vtp_closed1. eauto. eauto.
+ SCase "sel2".
assert (vtpdd m1 G1 x TX). eapply IHn; eauto. omega.
euv. repeat eexists. eapply vtp_sel. eauto. eauto. eauto. eauto.
+ SCase "sel2".
eapply stp_closed2 in H0. simpl in H0. inversion H0. inversion H13. omega.
+ SCase "or_typ".
inversion H4. subst.
repeat eexists. eapply vtp_typ. eauto. eauto. eapply stp_and11. eauto. eauto.
eapply stp_or21. eauto. eauto. eauto.
+ SCase "and".
assert (vtpdd m1 G1 x T2). eapply IHn; eauto. omega. euv.
assert (vtpdd m1 G1 x T4). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_and; eauto. eauto.
+ SCase "or1".
assert (vtpdd m1 G1 x T2). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or1; eauto. eauto.
+ SCase "or2".
assert (vtpdd m1 G1 x T4). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or2; eauto. eauto.
+ SCase "or...".
eapply IHn in H4. euv.
repeat eexists. eapply H4. omega. eauto. omega. omega. eauto.
+ SCase "or...".
assert (vtpdd m1 G1 x T4) as A. eapply IHn. eapply H. eauto. omega. omega. eauto. euv.
eapply IHn in A. euv.
repeat eexists. eauto. omega. eauto. omega. omega. eauto.
- Case "or2". subst. inversion H0; subst; invty.
+ SCase "top". repeat eexists. eapply vtp_top. eapply vtp_closed1. eauto. eauto.
+ SCase "sel2".
assert (vtpdd m1 G1 x TX). eapply IHn; eauto. omega.
euv. repeat eexists. eapply vtp_sel. eauto. eauto. eauto. eauto.
+ SCase "sel2".
eapply stp_closed2 in H0. simpl in H0. inversion H0. inversion H13. omega.
+ SCase "or_typ".
inversion H4. subst.
repeat eexists. eapply vtp_typ. eauto. eauto. eapply stp_and12. eauto. eauto.
eapply stp_or22. eauto. eauto. eauto.
+ SCase "and".
assert (vtpdd m1 G1 x T2). eapply IHn; eauto. omega. euv.
assert (vtpdd m1 G1 x T4). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_and; eauto. eauto.
+ SCase "or1".
assert (vtpdd m1 G1 x T2). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or1; eauto. eauto.
+ SCase "or2".
assert (vtpdd m1 G1 x T4). eapply IHn; eauto. omega. euv.
repeat eexists. eapply vtp_or2; eauto. eauto.
+ SCase "or...".
eapply IHn in H4. euv.
repeat eexists. eapply H4. omega. eauto. omega. omega. eauto.
+ SCase "or...".
assert (vtpdd m1 G1 x T4) as A. eapply IHn. eapply H. eauto. omega. omega. eauto. euv.
eapply IHn in A. euv.
repeat eexists. eauto. omega. eauto. omega. omega. eauto.
Grab Existential Variables.
apply 0. apply 0. apply 0. apply 0. apply 0. apply 0. apply 0. apply 0. apply 0.
Qed.
Lemma stp_subst_narrow_z: forall GH0 TX G1 T1 T2 x m n1 n2,
stp (GH0 ++ [TX]) G1 T1 T2 n2 ->
vtp m G1 x (substt x TX) n1 ->
stpd (map (substt x) GH0) G1 (substt x T1) (substt x T2).
Proof.
intros.
edestruct stp_subst_narrow0. eauto. eapply vtp_closed1. eauto. eauto.
{ intros. edestruct stp_subst_narrowX. eauto. eauto.
eapply vtp_closed1. eauto. eauto. eauto.
{ intros. eapply vtp_widen; eauto. }
ev. repeat eexists. eauto.
}
eexists. eassumption.
Qed.
Lemma dms_hastp_inv: forall G1 x ds' T' n,
dms_has_type [T'] G1 ds' T' n ->
closed 0 (length G1) 0 (substt x T') ->
index x G1 = Some (vobj (subst_dms x ds')) ->
exists m n, vtp m G1 x (substt x T') n.
Proof.
intros G1 x ds' T' n H HC HI.
remember T' as T0. remember H as HT0. clear HeqHT0.
rewrite HeqT0 in H at 2. rewrite HeqT0. rewrite HeqT0 in HC. clear HeqT0.
remember ds' as ds0. rewrite Heqds0 in H.
assert (exists dsa, dms_to_list ds0 = dsa ++ dms_to_list ds') as Hds. {
exists []. rewrite app_nil_l. subst. reflexivity.
}
clear Heqds0.
remember n as n0. rewrite Heqn0 in *. rewrite <- Heqn0 in HT0. clear Heqn0.
remember [T0] as GH. generalize dependent T0.
induction H; intros.
- repeat eexists. eapply vtp_top. eapply index_max. eauto.
- subst.
assert (closed 0 (length G1) 0 (substt x TS)) as HCS. {
unfold substt in *. simpl in HC. inversion HC; subst.
eauto.
}
assert (closed 0 (length G1) 0 (substt x T11)) as HC11. {
unfold substt in *. simpl in HC. inversion HC; subst.
inversion H6; subst. eauto.
}
assert (stpd [] G1 (substt x T11) (substt x T11)) as A. {
eapply stpd_refl. eauto.
}
eu.
destruct Hds as [dsa Hdsa]. simpl in Hdsa.
edestruct IHdms_has_type as [? [? AS]]. eauto. eauto. eauto.
exists (dsa ++ [dty T11]). rewrite <- app_assoc. simpl. eauto. eauto. eauto.
unfold substt in *. simpl.
repeat eexists. eapply vtp_and. eapply vtp_typ. eauto.
erewrite index_subst_dms with (D:=dty T11). simpl. reflexivity. eauto.
eauto. eauto. eauto. eauto. eauto.
- subst.
assert (closed 0 (length G1) 0 (substt x TS)) as HCS. {
unfold substt in *. simpl in HC. inversion HC; subst.
eauto.
}
assert (closed 0 (length G1) 0 (substt x T11)) as HC11. {
unfold substt in *. simpl in HC. inversion HC; subst.
inversion H10; subst. eauto.
}
assert (closed 1 (length G1) 0 (open 0 (TVar false 0) (substt x T12))) as HC12. {
unfold substt in *. simpl in HC. inversion HC; subst. inversion H10; subst.
eapply closed_open. eapply closed_upgrade_gh. eauto. omega.
econstructor. omega.
}
assert (stpd [] G1 (substt x T11) (substt x T11)) as A. {
eapply stpd_refl. eauto.
}
eu.
assert (stpd [(substt x T11)] G1 (open 0 (TVar false 0) (substt x T12)) (open 0 (TVar false 0) (substt x T12))) as B. {
eapply stpd_refl. eauto.
}
eu.
destruct Hds as [dsa Hdsa]. simpl in Hdsa.
edestruct IHdms_has_type as [? [? AS]]. eauto. eauto. eauto.
exists (dsa ++ [dfun OT11 OT12 t12]). rewrite <- app_assoc. simpl. eauto. eauto. eauto.
unfold substt in *. simpl.
repeat eexists. eapply vtp_and. eapply vtp_fun. eauto.
erewrite index_subst_dms with (D:=dfun OT11 OT12 t12). simpl. reflexivity. eauto.
eapply subst_eq_some; eauto. eapply subst_eq_some; eauto.
eauto. eapply HT0. simpl. reflexivity. eauto. eauto. eauto. eauto. eauto.
eauto. eauto. eauto. eauto.
eapply closed_subst. eauto. econstructor. eapply index_max. eauto.
eapply closed_subst. eauto. econstructor. eapply index_max. eauto.
eauto. eauto. eauto. eauto.
Grab Existential Variables.
apply 0. apply 0.
Qed.
Lemma hastp_inv: forall G1 x T n1,
has_type [] G1 (tvar true x) T n1 ->
exists m n1, vtp m G1 x T n1.
Proof.
intros. remember [] as GH. remember (tvar true x) as t.
induction H; subst; try inversion Heqt.
- Case "var". subst. eapply dms_hastp_inv; eauto.
- Case "pack". subst.
destruct IHhas_type. eauto. eauto. ev.
repeat eexists. eapply vtp_bind. eauto. eauto.
- Case "unpack". subst.
destruct IHhas_type. eauto. eauto. ev. inversion H0.
repeat eexists. eauto.
- Case "sub".
destruct IHhas_type. eauto. eauto. ev.
assert (exists m0, vtpdd m0 G1 x T2). eexists. eapply vtp_widen; eauto.
ev. euv. repeat eexists. eauto.
- Case "andi".
destruct IHhas_type1. eauto. eauto. ev.
assert (exists m0, vtpdd m0 G1 x T1). eexists. eauto.
destruct IHhas_type2. eauto. eauto. ev.
assert (exists m0, vtpdd m0 G1 x T2). eexists. eauto.
repeat eexists.
eapply vtp_and. eauto. eauto.
assert (x0 <= x0 + x2). omega. eauto. omega.
Qed.
Lemma stp_subst_narrow: forall GH0 TX G1 T1 T2 x m n1 n2,
stp (GH0 ++ [TX]) G1 T1 T2 n2 ->
vtp m G1 x TX n1 ->
stpd (map (substt x) GH0) G1 (substt x T1) (substt x T2).
Proof.
intros. eapply stp_subst_narrow_z. eauto.
erewrite subst_closed_id. eauto. eapply vtp_closed in H0. eauto.
Qed.
Lemma hastp_subst_aux_z: forall ni, (forall G1 GH TX T x t n1 n2,
has_type (GH++[TX]) G1 t T n2 -> n2 < ni ->
has_type [] G1 (tvar true x) (substt x TX) n1 ->
exists n3, has_type (map (substt x) GH) G1 (subst_tm x t) (substt x T) n3) /\
(forall G1 GH TX T x ds n1 n2,
dms_has_type (GH++[TX]) G1 ds T n2 -> n2 < ni ->
has_type [] G1 (tvar true x) (substt x TX) n1 ->
exists n3, dms_has_type (map (substt x) GH) G1 (subst_dms x ds) (substt x T) n3).
Proof.
intro ni. induction ni. split; intros; omega. destruct IHni as [IHniT IHniD].
split;
intros; remember (GH++[TX]) as GH0; revert GH HeqGH0; inversion H; intros.
- Case "vary".
assert (substt x T = T) as EqT. {
erewrite subst_closed_id. reflexivity. eauto.
}
subst. simpl. eexists. eapply T_Vary. eauto. eauto. eauto.
rewrite EqT. reflexivity. rewrite EqT. eauto.
- Case "varz". subst. simpl.
case_eq (beq_nat x0 0); intros E.
+ assert (x0 = 0). eapply beq_nat_true_iff; eauto. subst x0.
eapply index_hit0 in H2. subst.
eapply hastp_upgrade_gh. eauto.
+ assert (x0 <> 0). eapply beq_nat_false_iff; eauto.
eexists. eapply T_Varz. eapply index_subst1. eauto. eauto.
rewrite map_length. eapply closed_subst0.
rewrite app_length in H3. simpl in H3. eapply H3. eapply has_type_closed1. eauto.
- Case "pack". subst. simpl.
edestruct IHniT as [? IH]. eauto. omega. eauto.
assert (substt x (TBind T1) = (TBind (substt x T1))) as A. {
eauto.
}
rewrite A.
destruct b.
+ eexists. eapply T_VarPack. eapply IH.
unfold substt. rewrite subst_open_commute1. reflexivity.
rewrite map_length. eapply closed_subst0. rewrite app_length in H4. simpl in H4.
apply H4. eapply has_type_closed1. eauto.
+ case_eq (beq_nat x0 0); intros E.
* assert (x0 = 0). eapply beq_nat_true_iff; eauto. subst x0.
simpl in IH.
eexists. eapply T_VarPack. eapply IH. rewrite subst_open_commute0b. eauto.
rewrite map_length. eapply closed_subst. rewrite app_length in H4. simpl in H4.
eapply H4. econstructor. eapply has_type_closed1. eauto.
* assert (x0 <> 0). eapply beq_nat_false_iff; eauto.
simpl in IH. rewrite E in IH.
eexists. eapply T_VarPack. eapply IH.
remember (x0 - 1) as z.
assert (x0 = z + 1) as B. {
intuition. destruct x0. specialize (H3 eq_refl). inversion H3.
subst. simpl. rewrite <- minus_n_O. rewrite NPeano.Nat.add_1_r.
reflexivity.
}
rewrite B. unfold substt.
rewrite subst_open_commute_z. reflexivity.
rewrite map_length. eapply closed_subst. rewrite app_length in H4.
simpl in H4. eapply H4.
econstructor. eapply has_type_closed1. eauto.
- Case "unpack". subst. simpl.
edestruct IHniT as [? IH]. eapply H2. omega. eauto.
assert (substt x (TBind T1) = (TBind (substt x T1))) as A. {
eauto.
}
rewrite A in IH.
destruct b.
+ eexists. eapply T_VarUnpack. eapply IH.
unfold substt. rewrite subst_open_commute1. reflexivity.
rewrite map_length. eapply closed_subst0. rewrite app_length in H4. simpl in H4.
apply H4. eapply has_type_closed1. eauto.
+ case_eq (beq_nat x0 0); intros E.
* assert (x0 = 0). eapply beq_nat_true_iff; eauto. subst x0.
simpl in IH.
eexists. eapply T_VarUnpack. eapply IH. rewrite subst_open_commute0b. eauto.
rewrite map_length. eapply closed_subst. rewrite app_length in H4. simpl in H4.
eapply H4. econstructor. eapply has_type_closed1. eauto.
* assert (x0 <> 0). eapply beq_nat_false_iff; eauto.
simpl in IH. rewrite E in IH.
eexists. eapply T_VarUnpack. eapply IH.
remember (x0 - 1) as z.
assert (x0 = z + 1) as B. {
intuition. destruct x0. specialize (H3 eq_refl). inversion H3.
subst. simpl. rewrite <- minus_n_O. rewrite NPeano.Nat.add_1_r.
reflexivity.
}
rewrite B. unfold substt.
rewrite subst_open_commute_z. reflexivity.
rewrite map_length. eapply closed_subst. rewrite app_length in H4.
simpl in H4. eapply H4.
econstructor. eapply has_type_closed1. eauto.
- Case "obj".
edestruct IHniD with (GH:=T'::GH1) as [? IH]. subst. eauto. omega. subst. eauto.
subst. simpl.
eexists. eapply T_Obj. eauto.
rewrite app_length. simpl. unfold substt. rewrite subst_open_commute_z.
rewrite map_length. eauto.
eapply closed_subst. rewrite app_length in *. simpl in *. rewrite map_length. eauto.
econstructor. eapply has_type_closed1. eauto.
- Case "app". subst. simpl.
edestruct IHniT as [? IH1]. eapply H2. omega. eauto.
edestruct IHniT as [? IH2]. eapply H3. omega. eauto.
eexists. eapply T_App. eauto. eauto. eapply closed_subst.
subst. rewrite map_length. rewrite app_length in *. simpl in *. eauto.
subst. rewrite map_length. econstructor. eapply has_type_closed1. eauto.
- Case "appvar". subst. simpl.
edestruct IHniT as [? IH1]. eapply H2. omega. eauto.
edestruct IHniT as [? IH2]. eapply H3. omega. eauto.
destruct b2.
eexists. eapply T_AppVar. eauto. eauto.
rewrite subst_open_commute1. eauto.
eapply closed_subst. subst. rewrite map_length. rewrite app_length in *. simpl in *.
eapply closed_upgrade_gh. eassumption. omega.
subst. rewrite map_length. econstructor. eapply has_type_closed1. eauto.
case_eq (beq_nat x2 0); intros E.
eapply beq_nat_true in E. subst.
rewrite subst_open_commute0b.
eexists. eapply T_AppVar. eauto. eauto. eauto.
rewrite map_length. rewrite <- subst_open_commute0b.
eapply closed_subst. eapply closed_upgrade_gh. eassumption.
rewrite app_length. simpl. omega.
econstructor. eapply has_type_closed1. eauto.
rewrite subst_open5.
simpl in *. rewrite E in *.
eexists. eapply T_AppVar. eauto. eauto. eauto.
rewrite <- subst_open5. eapply closed_subst.
subst. rewrite map_length. rewrite app_length in *. simpl in *. eassumption.
subst. rewrite map_length. econstructor. eapply has_type_closed1. eauto.
apply []. apply beq_nat_false. apply E. apply []. apply beq_nat_false. apply E.
- Case "sub". subst.
edestruct hastp_inv as [? [? HV]]; eauto.
edestruct stp_subst_narrow_z. eapply H3. eapply HV.
edestruct IHniT as [? IH]. eapply H2. omega. eauto.
eexists. eapply T_Sub. eauto. eauto.
- Case "andi". subst. simpl.
edestruct IHniT as [? IH1]. eapply H2. omega. eauto.
edestruct IHniT as [? IH2]. eapply H3. omega. eauto.
eexists. eapply T_AndI.
eapply IH1. eapply IH2.
- Case "dnil". subst. simpl.
eexists. eapply D_Nil.
- Case "typ". subst. simpl.
edestruct IHniD as [? IH]. eapply H2. omega. eauto.
eexists. eapply D_Typ. eauto. eapply closed_subst0.
rewrite app_length in H3. rewrite map_length. eauto.
eapply has_type_closed1. eauto. eauto.
unfold substt. simpl. rewrite <- length_subst_dms. reflexivity.
- Case "abs". subst. simpl.
edestruct IHniD as [? IHD]. eapply H2. omega. eauto.
edestruct IHniT with (GH:=T11::GH1) as [? HI] . eauto. omega. eauto.
simpl in HI.
eexists. eapply D_Fun. eapply IHD. eapply HI.
rewrite map_length. rewrite app_length. simpl.
rewrite subst_open. unfold substt. reflexivity.
eapply closed_subst0. rewrite map_length.
rewrite app_length in H5. simpl in H5. eauto. eauto.
eapply has_type_closed1. eauto.
eapply closed_subst0. rewrite map_length.
rewrite app_length in H6. simpl in H6. eauto.
eapply has_type_closed1. eauto. eauto.
unfold substt. simpl. rewrite <- length_subst_dms. reflexivity.
eapply subst_eq_some; eauto. eapply subst_eq_some; eauto.
Grab Existential Variables.
apply 0. apply 0.
Qed.
Lemma hastp_subst_z: forall G1 GH TX T x t n1 n2,
has_type (GH++[TX]) G1 t T n2 ->
has_type [] G1 (tvar true x) (substt x TX) n1 ->
exists n3, has_type (map (substt x) GH) G1 (subst_tm x t) (substt x T) n3.
Proof.
intros. eapply hastp_subst_aux_z with (t:=t). eauto. eauto. eauto.
Qed.
Lemma hastp_subst: forall G1 GH TX T x t n1 n2,
has_type (GH++[TX]) G1 t T n2 ->
has_type [] G1 (tvar true x) TX n1 ->
exists n3, has_type (map (substt x) GH) G1 (subst_tm x t) (substt x T) n3.
Proof.
intros. eapply hastp_subst_z with (t:=t). eauto.
erewrite subst_closed_id. eauto. eapply has_type_closed in H0. eauto.
Qed.
Theorem type_safety : forall G t T n1,
has_type [] G t T n1 ->
((exists x, t = tvar true x /\ (exists ds, index x G = Some ds)) \/
(exists G' t', step G t (G'++G) t')) /\
(forall G' t', step G t (G'++G) t' -> exists n2, has_type [] (G'++G) t' T n2).
Proof.
intros.