forked from TiarkRompf/minidot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dot.v
2220 lines (2022 loc) · 80.7 KB
/
dot.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
(*
DOT
T ::= Bot | Top | T1 /\ T2 | T1 \/ T2 |
{ def m(x: S): U^x } | { type A: S..U } | x.A | { z => T^z }
t ::= x | { y => d^y... } | t.m(t)
d ::= { def m(x: S): U^x = t^x } | { type A = T }
*)
(* in small-step *)
Require Export SfLib.
Require Export Arith.EqNat.
Require Export Arith.Lt.
(*# Syntax #*)
Definition id := nat. (* identifiers for variables: x,y,z *)
Definition lb := nat. (* labels for records: L, m *)
Inductive vr : Type :=
| TVar : bool(*true for concrete context, false for abstract context *) ->
id(*absolute position in context, from origin, invariant under context extension*) -> vr
| TVarB : id(*bound variable, de Bruijn, locally nameless style -- see open *) -> vr
.
Inductive ty : Type :=
| TBot : ty (* bottom type *)
| TTop : ty (* top type *)
| TFun : lb -> ty -> ty -> ty (* dependent function / method member type:
{ def m(x: S): U^x },
where x is locally bound in U *)
| TTyp : lb -> ty -> ty -> ty (* type member type: { type L: S..U } *)
| TSel : vr -> lb -> ty (* type selection: x.L *)
| TBind : ty -> ty (* Recursive binder: { z => T^z },
where z is locally bound in T *)
| TAnd : ty -> ty -> ty (* Intersection Type: T1 /\ T2 *)
| TOr : ty -> ty -> ty (* Union Type: T1 \/ T2 *)
.
Inductive tm : Type :=
| tvar : bool(*like TVar: true for concrete, false for hypothetical *) -> id -> tm (* variable: x *)
(* N.B.: no varB -- terms just use absolute identifers directly *)
| tobj : dms(*self is next slot in abstract context -- see subst_tm*) -> tm (* new object instance: { z => d... } *)
| tapp : tm -> lb -> tm -> tm (* method invocation: t.m(t) *)
with dm : Type := (* initialization / member definition --
the labels, e.g. m & A, are determined from the position in member list, dms *)
| dfun : option ty -> option ty -> tm -> dm (* method: { def m(x[: S])[: U] = t }, where the types [: S] and [: U] are optional *)
(* Church vs Curry: we show that all options work, by making parameter and return types optional,
when defining a method. *)
| dty : ty -> dm (* type: { type L = T } *)
(* we use our own list-like structure for easy recursion, e.g. in subst_tm *)
with dms : Type := (* list of member defs *)
| dnil : dms
| dcons : dm -> dms -> dms
.
Fixpoint dms_to_list (ds: dms) : list dm :=
match ds with
| dnil => []
| dcons d ds => d :: dms_to_list ds
end.
Inductive vl : Type :=
| vobj : dms -> vl
.
Definition venv := list vl. (*rho G*)
Definition tenv := list ty. (*Gamma GH*)
Hint Unfold venv.
Hint Unfold tenv.
(*# Variable Binding #*)
Fixpoint index {X : Type} (n : id) (l : list X) : option X :=
match l with
| [] => None
| a :: l' => if beq_nat n (length l') then Some a else index n l'
end.
(*
closed i j k -- well-bound in
an abstract environment GH of size >= i
a concrete environment G of size >= j
under >= k binders/de Bruijn levels
*)
Inductive vr_closed: nat(*abstract, TVar false i*) -> nat(*concrete, TVar true j*) -> nat(*bound, TVarB k*) -> vr -> Prop :=
| cl_var0: forall i j k x,
i > x ->
vr_closed i j k (TVar false x)
| cl_var1: forall i j k x,
j > x ->
vr_closed i j k (TVar true x)
| cl_varB: forall i j k x,
k > x ->
vr_closed i j k (TVarB x).
Inductive closed: nat(*abstract, TVar false i*) -> nat(*concrete, TVar true j*) -> nat(*bound, TVarB k*) -> ty -> Prop :=
| cl_bot: forall i j k,
closed i j k TBot
| cl_top: forall i j k,
closed i j k TTop
| cl_fun: forall i j k l T1 T2,
closed i j k T1 ->
closed i j (S k) T2 ->
closed i j k (TFun l T1 T2)
| cl_typ: forall i j k l T1 T2,
closed i j k T1 ->
closed i j k T2 ->
closed i j k (TTyp l T1 T2)
| cl_sel: forall i j k p1 l,
vr_closed i j k p1 ->
closed i j k (TSel p1 l)
| cl_bind: forall i j k T1,
closed i j (S k) T1 ->
closed i j k (TBind T1)
| cl_and: forall i j k T1 T2,
closed i j k T1 ->
closed i j k T2 ->
closed i j k (TAnd T1 T2)
| cl_or: forall i j k T1 T2,
closed i j k T1 ->
closed i j k T2 ->
closed i j k (TOr T1 T2)
.
(* substitute a locally bound variable at de Brujin level k with variable u in type T *)
Definition vr_open (k: nat) (u: vr) (p: vr) : vr :=
match p with
| TVar b x => TVar b x (* free var remains free. functional, so we can't check for conflict *)
| TVarB x => if beq_nat k x then u else TVarB x
end.
Fixpoint open (k: nat) (u: vr) (T: ty) { struct T }: ty :=
match T with
| TTop => TTop
| TBot => TBot
| TSel p1 l => TSel (vr_open k u p1) l
| TFun l T1 T2 => TFun l (open k u T1) (open (S k) u T2)
| TTyp l T1 T2 => TTyp l (open k u T1) (open k u T2)
| TBind T1 => TBind (open (S k) u T1)
| TAnd T1 T2 => TAnd (open k u T1) (open k u T2)
| TOr T1 T2 => TOr (open k u T1) (open k u T2)
end.
(* substitute the first abstract variable (id 0) with variable u in type T --
all other abstract variables are shifted (id decremented) to fit the shrinked abstract context
*)
Definition vr_subst (u : vr) (X : vr): vr :=
match X with
| TVarB i => TVarB i
| TVar true i => TVar true i
(* subst the _first_ aka _oldest_ abstract variables,
the other abstract variables are shifted to resolve in the shrinked context *)
| TVar false i => if beq_nat i 0 then u else TVar false (i-1)
end.
Fixpoint subst (u : vr) (T : ty) {struct T} : ty :=
match T with
| TTop => TTop
| TBot => TBot
| TTyp l T1 T2 => TTyp l (subst u T1) (subst u T2)
| TSel p1 l => TSel (vr_subst u p1) l
| TFun l T1 T2 => TFun l (subst u T1) (subst u T2)
| TBind T2 => TBind (subst u T2)
| TAnd T1 T2 => TAnd (subst u T1) (subst u T2)
| TOr T1 T2 => TOr (subst u T1) (subst u T2)
end.
(* substitute the first hypothetical variable with term u in term t --
like subst, shifts other hypothetical variables *)
Fixpoint subst_tm (u:nat) (t : tm) {struct t} : tm :=
match t with
| tvar true i => tvar true i
| tvar false i => if beq_nat i 0 then (tvar true u) else tvar false (i-1)
| tobj ds => tobj (subst_dms u ds)
| tapp t1 l t2 => tapp (subst_tm u t1) l (subst_tm u t2)
end
with subst_dm (u:nat) (d: dm) {struct d} : dm :=
match d with
| dty T => dty (subst (TVar true u) T)
| dfun T1 T2 t => dfun (option_map (subst (TVar true u)) T1) (option_map (subst (TVar true u)) T2) (subst_tm u t)
end
with subst_dms (u:nat) (ds: dms) {struct ds} : dms :=
match ds with
| dnil => dnil
| dcons d ds1 => dcons (subst_dm u d) (subst_dms u ds1)
end.
(* Shortcut for the common case of replacing abstract with concrete. *)
Definition substt x T := (subst (TVar true x) T).
Hint Immediate substt.
(*# Operational Semantics #*)
(* Reduction semantics *)
Inductive step : venv -> tm -> venv -> tm -> Prop :=
(* Computation Rules *)
| ST_Obj : forall G1 D,
step G1 (tobj D) (vobj (subst_dms (length G1) D)::G1) (tvar true (length G1))
| ST_AppAbs : forall G1 f l x ds T1 T2 t12,
index f G1 = Some (vobj ds) ->
index l (dms_to_list ds) = Some (dfun T1 T2 t12) ->
step G1 (tapp (tvar true f) l (tvar true x)) G1 (subst_tm x t12)
(* Congruence Rules *)
| ST_App1 : forall G1 G1' t1 t1' l t2,
step G1 t1 G1' t1' ->
step G1 (tapp t1 l t2) G1' (tapp t1' l t2)
| ST_App2 : forall G1 G1' f t2 l t2',
step G1 t2 G1' t2' ->
step G1 (tapp (tvar true f) l t2) G1' (tapp (tvar true f) l t2')
.
(*# Static Semantics #*)
Definition eq_some {X} (OT:option X) (T:X) := OT=None \/ OT=Some T.
(* : -- typing *)
Inductive has_type : tenv -> venv -> tm -> ty -> nat -> Prop :=
| T_Vary : forall GH G1 x ds ds' T T' n1,
index x G1 = Some (vobj ds) ->
dms_has_type [T'] G1 ds' T' n1 ->
subst_dms x ds' = ds ->
substt x T' = T ->
closed 0 (length G1) 0 T ->
has_type GH G1 (tvar true x) T (S n1)
| T_Varz : forall G1 GH x T n1,
index x GH = Some T ->
closed (length GH) (length G1) 0 T ->
has_type GH G1 (tvar false x) T (S n1)
| T_VarPack : forall GH G1 b x T1 T1' n1,
has_type GH G1 (tvar b x) T1' n1 ->
T1' = (open 0 (TVar b x) T1) ->
closed (length GH) (length G1) 1 T1 ->
has_type GH G1 (tvar b x) (TBind T1) (S n1)
| T_VarUnpack : forall GH G1 b x T1 T1' n1,
has_type GH G1 (tvar b x) (TBind T1) n1 ->
T1' = (open 0 (TVar b x) T1) ->
closed (length GH) (length G1) 0 T1' ->
has_type GH G1 (tvar b x) T1' (S n1)
| T_Obj : forall GH G1 ds T T' n1,
dms_has_type (T'::GH) G1 ds T' n1 ->
T' = open 0 (TVar false (length GH)) T ->
closed (length GH) (length G1) 1 T ->
has_type GH G1 (tobj ds) (TBind T) (S n1)
| T_App : forall l T1 T2 GH G1 t1 t2 n1 n2,
has_type GH G1 t1 (TFun l T1 T2) n1 ->
has_type GH G1 t2 T1 n2 ->
closed (length GH) (length G1) 0 T2 ->
has_type GH G1 (tapp t1 l t2) T2 (S (n1+n2))
| T_AppVar : forall l T1 T2 T2' GH G1 t1 b2 x2 n1 n2,
has_type GH G1 t1 (TFun l T1 T2) n1 ->
has_type GH G1 (tvar b2 x2) T1 n2 ->
T2' = (open 0 (TVar b2 x2) T2) ->
closed (length GH) (length G1) 0 T2' ->
has_type GH G1 (tapp t1 l (tvar b2 x2)) T2' (S (n1+n2))
| T_Sub : forall GH G1 t T1 T2 n1 n2,
has_type GH G1 t T1 n1 ->
stp GH G1 T1 T2 n2 ->
has_type GH G1 t T2 (S (n1 + n2))
| T_AndI : forall GH G1 t T1 T2 n1 n2,
has_type GH G1 t T1 n1 ->
has_type GH G1 t T2 n2 ->
has_type GH G1 t (TAnd T1 T2) (S (n1+n2))
(* : -- member initialization *)
with dms_has_type: tenv -> venv -> dms -> ty -> nat -> Prop :=
| D_Nil : forall GH G1 n1,
dms_has_type GH G1 dnil TTop (S n1)
| D_Typ : forall GH G1 l T11 ds TS T n1,
dms_has_type GH G1 ds TS n1 ->
closed (length GH) (length G1) 0 T11 ->
l = length (dms_to_list ds) ->
T = TAnd (TTyp l T11 T11) TS ->
dms_has_type GH G1 (dcons (dty T11) ds) T (S n1)
| D_Fun : forall GH G1 l OT11 T11 OT12 T12 T12' t12 ds TS T n1 n2,
dms_has_type GH G1 ds TS n1 ->
has_type (T11::GH) G1 t12 T12' n2 ->
T12' = (open 0 (TVar false (length GH)) T12) ->
closed (length GH) (length G1) 0 T11 ->
closed (length GH) (length G1) 1 T12 ->
l = length (dms_to_list ds) ->
T = TAnd (TFun l T11 T12) TS ->
eq_some OT11 T11 ->
eq_some OT12 T12 ->
dms_has_type GH G1 (dcons (dfun OT11 OT12 t12) ds) T (S (n1+n2))
(* <: -- subtyping *)
with stp: tenv -> venv -> ty -> ty -> nat -> Prop :=
| stp_bot: forall GH G1 T n1,
closed (length GH) (length G1) 0 T ->
stp GH G1 TBot T (S n1)
| stp_top: forall GH G1 T n1,
closed (length GH) (length G1) 0 T ->
stp GH G1 T TTop (S n1)
| stp_fun: forall GH G1 l T1 T2 T3 T4 T2' T4' n1 n2,
T2' = (open 0 (TVar false (length GH)) T2) ->
T4' = (open 0 (TVar false (length GH)) T4) ->
closed (length GH) (length G1) 1 T2 ->
closed (length GH) (length G1) 1 T4 ->
stp GH G1 T3 T1 n1 ->
stp (T3::GH) G1 T2' T4' n2 ->
stp GH G1 (TFun l T1 T2) (TFun l T3 T4) (S (n1+n2))
| stp_typ: forall GH G1 l T1 T2 T3 T4 n1 n2,
stp GH G1 T3 T1 n2 ->
stp GH G1 T2 T4 n1 ->
stp GH G1 (TTyp l T1 T2) (TTyp l T3 T4) (S (n1+n2))
| stp_strong_sel1: forall GH G1 l T2 ds TX x n1,
index x G1 = Some (vobj ds) ->
index l (dms_to_list ds) = Some (dty TX) ->
stp [] G1 TX T2 n1 ->
stp GH G1 (TSel (TVar true x) l) T2 (S n1)
| stp_strong_sel2: forall GH G1 l T1 ds TX x n1,
index x G1 = Some (vobj ds) ->
index l (dms_to_list ds) = Some (dty TX) ->
stp [] G1 T1 TX n1 ->
stp GH G1 T1 (TSel (TVar true x) l) (S n1)
| stp_sel1: forall GH G1 l T2 x n1,
htp GH G1 x (TTyp l TBot T2) n1 ->
stp GH G1 (TSel (TVar false x) l) T2 (S n1)
| stp_sel2: forall GH G1 l T1 x n1,
htp GH G1 x (TTyp l T1 TTop) n1 ->
stp GH G1 T1 (TSel (TVar false x) l) (S n1)
| stp_selx: forall GH G1 l p1 n1,
vr_closed (length GH) (length G1) 0 p1 ->
stp GH G1 (TSel p1 l) (TSel p1 l) (S n1)
| stp_bind1: forall GH G1 T1 T1' T2 n1,
stp (T1'::GH) G1 T1' T2 n1 ->
T1' = (open 0 (TVar false (length GH)) T1) ->
closed (length GH) (length G1) 1 T1 ->
closed (length GH) (length G1) 0 T2 ->
stp GH G1 (TBind T1) T2 (S n1)
| stp_bindx: forall GH G1 T1 T1' T2 T2' n1,
stp (T1'::GH) G1 T1' T2' n1 ->
T1' = (open 0 (TVar false (length GH)) T1) ->
T2' = (open 0 (TVar false (length GH)) T2) ->
closed (length GH) (length G1) 1 T1 ->
closed (length GH) (length G1) 1 T2 ->
stp GH G1 (TBind T1) (TBind T2) (S n1)
| stp_and_bind: forall GH G1 T1 T2 n1,
closed (length GH) (length G1) 1 T1 ->
closed (length GH) (length G1) 1 T2 ->
stp GH G1 (TAnd (TBind T1) (TBind T2)) (TBind (TAnd T1 T2)) (S n1)
| stp_and_typ: forall GH G1 l T1 T2 T3 T4 n1,
closed (length GH) (length G1) 0 T1 ->
closed (length GH) (length G1) 0 T2 ->
closed (length GH) (length G1) 0 T3 ->
closed (length GH) (length G1) 0 T4 ->
stp GH G1 (TAnd (TTyp l T1 T2) (TTyp l T3 T4)) (TTyp l (TOr T1 T3) (TAnd T2 T4)) (S n1)
| stp_or_typ: forall GH G1 l T1 T2 T3 T4 n1,
closed (length GH) (length G1) 0 T1 ->
closed (length GH) (length G1) 0 T2 ->
closed (length GH) (length G1) 0 T3 ->
closed (length GH) (length G1) 0 T4 ->
stp GH G1 (TOr (TTyp l T1 T2) (TTyp l T3 T4)) (TTyp l (TAnd T1 T3) (TOr T2 T4)) (S n1)
| stp_and11: forall GH G1 T1 T2 T n1,
stp GH G1 T1 T n1 ->
closed (length GH) (length G1) 0 T2 ->
stp GH G1 (TAnd T1 T2) T (S n1)
| stp_and12: forall GH G1 T1 T2 T n1,
stp GH G1 T2 T n1 ->
closed (length GH) (length G1) 0 T1 ->
stp GH G1 (TAnd T1 T2) T (S n1)
| stp_and2: forall GH G1 T1 T2 T n1 n2,
stp GH G1 T T1 n1 ->
stp GH G1 T T2 n2 ->
stp GH G1 T (TAnd T1 T2) (S (n1+n2))
| stp_or21: forall GH G1 T1 T2 T n1,
stp GH G1 T T1 n1 ->
closed (length GH) (length G1) 0 T2 ->
stp GH G1 T (TOr T1 T2) (S n1)
| stp_or22: forall GH G1 T1 T2 T n1,
stp GH G1 T T2 n1 ->
closed (length GH) (length G1) 0 T1 ->
stp GH G1 T (TOr T1 T2) (S n1)
| stp_or1: forall GH G1 T1 T2 T n1 n2,
stp GH G1 T1 T n1 ->
stp GH G1 T2 T n2 ->
stp GH G1 (TOr T1 T2) T (S (n1+n2))
| stp_trans: forall GH G1 T1 T2 T3 n1 n2,
stp GH G1 T1 T2 n1 ->
stp GH G1 T2 T3 n2 ->
stp GH G1 T1 T3 (S (n1+n2))
(* :! -- typing for type selection in subtyping *)
with htp: tenv -> venv -> id -> ty -> nat -> Prop :=
| htp_var: forall GH G1 x TX n1,
index x GH = Some TX ->
closed (S x) (length G1) 0 TX ->
htp GH G1 x TX (S n1)
| htp_unpack: forall GH G1 x TX n1,
htp GH G1 x (TBind TX) n1 ->
closed (S x) (length G1) 1 TX ->
htp GH G1 x (open 0 (TVar false x) TX) (S n1)
| htp_sub: forall GH GU GL G1 x T1 T2 n1 n2,
(* use restricted GH. note: this is slightly different
from the big-step version b/c here we do not distinguish
if variables are bound in terms vs types. it would be easy
to do exactly the same thing by adding this distinction. *)
htp GH G1 x T1 n1 ->
stp GL G1 T1 T2 n2 ->
length GL = S x ->
GH = GU ++ GL ->
htp GH G1 x T2 (S (n1+n2))
| htp_andi: forall GH G1 x T1 T2 n1 n2,
htp GH G1 x T1 n1 ->
htp GH G1 x T2 n2 ->
htp GH G1 x (TAnd T1 T2) (S (n1+n2)).
Definition has_typed GH G1 x T1 := exists n, has_type GH G1 x T1 n.
Definition stpd GH G1 T1 T2 := exists n, stp GH G1 T1 T2 n.
Definition htpd GH G1 x T1 := exists n, htp GH G1 x T1 n.
Hint Constructors stp.
Ltac ep := match goal with
| [ |- stp ?GH ?G1 ?T1 ?T2 ?N ] => assert (exists (n:nat), stp GH G1 T1 T2 n) as EEX
end.
Ltac eu := match goal with
| H: has_typed _ _ _ _ |- _ => destruct H as [? H]
| H: stpd _ _ _ _ |- _ => destruct H as [? H]
| H: htpd _ _ _ _ |- _ => destruct H as [? H]
end.
Lemma stpd_bot: forall GH G1 T,
closed (length GH) (length G1) 0 T ->
stpd GH G1 TBot T.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd_top: forall GH G1 T,
closed (length GH) (length G1) 0 T ->
stpd GH G1 T TTop.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd_fun: forall GH G1 l T1 T2 T3 T4 T2' T4',
T2' = (open 0 (TVar false (length GH)) T2) ->
T4' = (open 0 (TVar false (length GH)) T4) ->
closed (length GH) (length G1) 1 T2 ->
closed (length GH) (length G1) 1 T4 ->
stpd GH G1 T3 T1 ->
stpd (T3::GH) G1 T2' T4' ->
stpd GH G1 (TFun l T1 T2) (TFun l T3 T4).
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd_typ: forall GH G1 l T1 T2 T3 T4,
stpd GH G1 T3 T1 ->
stpd GH G1 T2 T4 ->
stpd GH G1 (TTyp l T1 T2) (TTyp l T3 T4).
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd_and_bind: forall GH G1 T1 T2,
closed (length GH) (length G1) 1 T1 ->
closed (length GH) (length G1) 1 T2 ->
stpd GH G1 (TAnd (TBind T1) (TBind T2)) (TBind (TAnd T1 T2)).
Proof. intros. exists 1. eauto. Qed.
Lemma stpd_and_typ: forall GH G1 l T1 T2 T3 T4,
closed (length GH) (length G1) 0 T1 ->
closed (length GH) (length G1) 0 T2 ->
closed (length GH) (length G1) 0 T3 ->
closed (length GH) (length G1) 0 T4 ->
stpd GH G1 (TAnd (TTyp l T1 T2) (TTyp l T3 T4)) (TTyp l (TOr T1 T3) (TAnd T2 T4)).
Proof. intros. exists 1. eauto. Qed.
Lemma stpd_or_typ: forall GH G1 l T1 T2 T3 T4,
closed (length GH) (length G1) 0 T1 ->
closed (length GH) (length G1) 0 T2 ->
closed (length GH) (length G1) 0 T3 ->
closed (length GH) (length G1) 0 T4 ->
stpd GH G1 (TOr (TTyp l T1 T2) (TTyp l T3 T4)) (TTyp l (TAnd T1 T3) (TOr T2 T4)).
Proof. intros. exists 1. eauto. Qed.
Lemma stpd_trans: forall GH G1 T1 T2 T3,
stpd GH G1 T1 T2 ->
stpd GH G1 T2 T3 ->
stpd GH G1 T1 T3.
Proof. intros. repeat eu. eexists. eauto. Qed.
Hint Constructors ty.
Hint Constructors vl.
Hint Constructors stp.
Hint Constructors htp.
Hint Constructors has_type.
Hint Unfold has_typed.
Hint Unfold stpd.
Hint Constructors option.
Hint Constructors list.
Hint Unfold index.
Hint Unfold length.
Hint Resolve ex_intro.
Ltac ev := repeat match goal with
| H: exists _, _ |- _ => destruct H
| H: _ /\ _ |- _ => destruct H
end.
(*# Regularity #*)
Lemma index_max : forall X vs n (T: X),
index n vs = Some T ->
n < length vs.
Proof.
intros X vs. induction vs.
Case "nil". intros. inversion H.
Case "cons".
intros. inversion H.
case_eq (beq_nat n (length vs)); intros E.
SCase "hit".
rewrite E in H1. inversion H1. subst.
eapply beq_nat_true in E.
unfold length. unfold length in E. rewrite E. eauto.
SCase "miss".
rewrite E in H1.
assert (n < length vs). eapply IHvs. apply H1.
compute. eauto.
Qed.
Lemma index_exists : forall X vs n,
n < length vs ->
exists (T:X), index n vs = Some T.
Proof.
intros X vs. induction vs.
Case "nil". intros. inversion H.
Case "cons".
intros. inversion H.
SCase "hit".
assert (beq_nat n (length vs) = true) as E. eapply beq_nat_true_iff. eauto.
simpl. subst n. rewrite E. eauto.
SCase "miss".
assert (beq_nat n (length vs) = false) as E. eapply beq_nat_false_iff. omega.
simpl. rewrite E. eapply IHvs. eauto.
Qed.
Lemma index_extend : forall X vs n a (T: X),
index n vs = Some T ->
index n (a::vs) = Some T.
Proof.
intros.
assert (n < length vs). eapply index_max. eauto.
assert (n <> length vs). omega.
assert (beq_nat n (length vs) = false) as E. eapply beq_nat_false_iff; eauto.
unfold index. unfold index in H. rewrite H. rewrite E. reflexivity.
Qed.
Lemma plus_lt_contra: forall a b,
a + b < b -> False.
Proof.
intros a b H. induction a.
- simpl in H. apply lt_irrefl in H. assumption.
- simpl in H. apply IHa. omega.
Qed.
Lemma index_extend_mult: forall {X} G0 G2 x0 (T:X),
index x0 G0 = Some T ->
index x0 (G2++G0) = Some T.
Proof.
intros X G0 G2. induction G2; intros.
- simpl. assumption.
- simpl.
case_eq (beq_nat x0 (length (G2 ++ G0))); intros E.
+ eapply beq_nat_true_iff in E.
apply index_max in H. subst.
rewrite app_length in H. apply plus_lt_contra in H. inversion H.
+ apply IHG2. assumption.
Qed.
Lemma vr_closed_extend : forall p X (a:X) i k G,
vr_closed i (length G) k p ->
vr_closed i (length (a::G)) k p.
Proof.
intros. inversion H; subst; econstructor; eauto. simpl. omega.
Qed.
Lemma closed_extend : forall T X (a:X) i k G,
closed i (length G) k T ->
closed i (length (a::G)) k T.
Proof.
intros T. induction T; intros; inversion H; econstructor; eauto using vr_closed_extend.
Qed.
Lemma all_extend: forall ni,
(forall GH v1 G1 T1 T2 n,
stp GH G1 T1 T2 n -> n < ni ->
stp GH (v1::G1) T1 T2 n) /\
(forall v1 x GH G1 T2 n,
htp GH G1 x T2 n -> n < ni ->
htp GH (v1::G1) x T2 n) /\
(forall GH G1 t T v n,
has_type GH G1 t T n -> n < ni ->
has_type GH (v::G1) t T n) /\
(forall GH G1 ds T v n,
dms_has_type GH G1 ds T n -> n < ni ->
dms_has_type GH (v::G1) ds T n).
Proof.
intros n. induction n. repeat split; intros; omega.
repeat split; intros; inversion H.
(* stp *)
- econstructor. eapply closed_extend. eauto.
- econstructor. eapply closed_extend. eauto.
- econstructor. eauto. eauto.
eapply closed_extend. eauto. eapply closed_extend. eauto.
eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- econstructor. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- econstructor. eapply index_extend. eauto. eauto. eapply IHn. eauto. omega.
- econstructor. eapply index_extend. eauto. eauto. eapply IHn. eauto. omega.
- econstructor. eapply IHn. eauto. omega.
- econstructor. eapply IHn. eauto. omega.
- econstructor. eapply vr_closed_extend. eauto.
- econstructor. eapply IHn. eauto. omega. eauto. eapply closed_extend. eauto. eapply closed_extend. eauto.
- eapply stp_bindx. eapply IHn. eauto. omega. eauto. eauto. eapply closed_extend. eauto. eapply closed_extend. eauto.
- eapply stp_and_bind. eapply closed_extend. eauto. eapply closed_extend. eauto.
- eapply stp_and_typ. eapply closed_extend. eauto. eapply closed_extend. eauto. eapply closed_extend. eauto. eapply closed_extend. eauto.
- eapply stp_or_typ. eapply closed_extend. eauto. eapply closed_extend. eauto. eapply closed_extend. eauto. eapply closed_extend. eauto.
- eapply stp_and11. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- eapply stp_and12. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- eapply stp_and2. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- eapply stp_or21. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- eapply stp_or22. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- eapply stp_or1. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- eapply stp_trans. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
(* htp *)
- econstructor. eauto. eapply closed_extend. eauto.
- eapply htp_unpack. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- eapply htp_sub. eapply IHn. eauto. omega. eapply IHn. eauto. omega. eauto. eauto.
- eapply htp_andi. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
(* has_type *)
- econstructor. eapply index_extend. eauto. eapply IHn. eauto. omega. eauto. eauto. eapply closed_extend. eauto.
- econstructor. eauto. eapply closed_extend. eauto.
- econstructor. eapply IHn. eauto. omega. eauto. eapply closed_extend. eauto.
- econstructor. eapply IHn. eauto. omega. eauto. eapply closed_extend. eauto.
- econstructor. eapply IHn. eauto. omega. eauto. eapply closed_extend. eauto.
- econstructor. subst. eapply IHn. eauto. omega. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- eapply T_AppVar. eapply IHn. eauto. omega. eapply IHn. eauto. omega. eauto. eapply closed_extend. eauto.
- econstructor. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- eapply T_AndI. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
(* dms_has_type *)
- econstructor.
- econstructor. eapply IHn. eauto. omega. eapply closed_extend. eauto. eauto. eauto.
- econstructor. eapply IHn. eauto. omega. eapply IHn. eauto. omega. eauto.
eapply closed_extend. eauto. eapply closed_extend. eauto. eauto. eauto. eauto. eauto.
Qed.
Lemma vr_closed_upgrade_gh: forall i i1 j k p1,
vr_closed i j k p1 -> i <= i1 -> vr_closed i1 j k p1.
Proof.
intros. inversion H; subst; econstructor; eauto. omega.
Qed.
Lemma closed_upgrade_gh: forall i i1 j k T1,
closed i j k T1 -> i <= i1 -> closed i1 j k T1.
Proof.
intros. generalize dependent i1. induction H; intros; econstructor; eauto using vr_closed_upgrade_gh.
Qed.
Lemma vr_closed_extend_mult : forall p i j j' k,
vr_closed i j k p -> j <= j' ->
vr_closed i j' k p.
Proof.
intros. inversion H; subst; econstructor; eauto. omega.
Qed.
Lemma closed_extend_mult : forall T i j j' k,
closed i j k T -> j <= j' ->
closed i j' k T.
Proof.
intros. generalize dependent j'. induction H; intros; econstructor; eauto using vr_closed_extend_mult.
Qed.
Lemma vr_closed_upgrade: forall i j k k1 p1,
vr_closed i j k p1 -> k <= k1 -> vr_closed i j k1 p1.
Proof.
intros. inversion H; subst; econstructor; eauto. omega.
Qed.
Lemma closed_upgrade: forall i j k k1 T1,
closed i j k T1 -> k <= k1 -> closed i j k1 T1.
Proof.
intros. generalize dependent k1. induction H; intros; econstructor; eauto using vr_closed_upgrade.
eapply IHclosed2. omega.
eapply IHclosed. omega.
Qed.
Lemma vr_closed_open: forall j k n b V p, vr_closed k n (j+1) p -> vr_closed k n j (TVar b V) -> vr_closed k n j (vr_open j (TVar b V) p).
Proof.
intros. inversion H; try econstructor; eauto; subst.
- Case "TVarB". simpl.
case_eq (beq_nat j x); intros E. eauto.
econstructor. eapply beq_nat_false_iff in E. omega.
Qed.
Lemma closed_open: forall j k n b V T, closed k n (j+1) T -> vr_closed k n j (TVar b V) -> closed k n j (open j (TVar b V) T).
Proof.
intros. generalize dependent j. induction T; intros; inversion H;
try econstructor; try eapply IHT1; try eapply IHT2; eauto; try eapply IHT; eauto using vr_closed_open; subst;
eapply vr_closed_upgrade; eauto.
Qed.
Lemma all_closed: forall ni,
(forall GH G1 T1 T2 n,
stp GH G1 T1 T2 n -> n < ni ->
closed (length GH) (length G1) 0 T1) /\
(forall GH G1 T1 T2 n,
stp GH G1 T1 T2 n -> n < ni ->
closed (length GH) (length G1) 0 T2) /\
(forall x GH G1 T2 n,
htp GH G1 x T2 n -> n < ni ->
x < length GH) /\
(forall x GH G1 T2 n,
htp GH G1 x T2 n -> n < ni ->
closed (length GH) (length G1) 0 T2) /\
(forall GH G1 t T n,
has_type GH G1 t T n -> n < ni ->
closed (length GH) (length G1) 0 T) /\
(forall GH G1 ds T n,
dms_has_type GH G1 ds T n -> n < ni ->
closed (length GH) (length G1) 0 T).
Proof.
intros n. induction n. repeat split; intros; omega.
repeat split; intros; inversion H; destruct IHn as [IHS1 [IHS2 [IHH1 [IHH2 [IHT IHD]]]]].
(* stp left *)
- econstructor.
- eauto.
- econstructor. eapply IHS2. eauto. omega. eauto.
- econstructor. eapply IHS2. eauto. omega. eapply IHS1. eauto. omega.
- econstructor. econstructor. eapply index_max. eauto.
- eapply closed_upgrade_gh. eapply IHS1. eauto. omega. simpl. omega.
- econstructor. econstructor. eapply IHH1. eauto. omega.
- eapply closed_upgrade_gh. eapply IHH2 in H1. inversion H1. eauto. omega. simpl. omega.
- econstructor. eauto.
- econstructor. eauto.
- econstructor. eauto.
- econstructor. eapply cl_bind. eauto. eapply cl_bind. eauto.
- econstructor. eapply cl_typ. eauto. eauto. eapply cl_typ. eauto. eauto.
- econstructor. eapply cl_typ. eauto. eauto. eapply cl_typ. eauto. eauto.
- econstructor. eapply IHS1. eauto. omega. eauto.
- econstructor. eauto. eapply IHS1. eauto. omega.
- eapply IHS1. eauto. omega.
- eapply IHS1. eauto. omega.
- eapply IHS1. eauto. omega.
- econstructor. eapply IHS1. eauto. omega. eapply IHS1. eauto. omega.
- eapply IHS1. eauto. omega.
(* stp right *)
- eauto.
- econstructor.
- econstructor. eapply IHS1. eauto. omega. eauto.
- econstructor. eapply IHS1. eauto. omega. eapply IHS2. eauto. omega.
- eapply closed_upgrade_gh. eapply IHS2. eauto. omega. simpl. omega.
- econstructor. econstructor. eapply index_max. eauto.
- eapply closed_upgrade_gh. eapply IHH2 in H1. inversion H1. eauto. omega. simpl. omega.
- econstructor. econstructor. eapply IHH1. eauto. omega.
- econstructor. eauto.
- eauto.
- econstructor. eauto.
- econstructor. eapply cl_and. eauto. eauto.
- econstructor. eapply cl_or. eauto. eauto. eapply cl_and. eauto. eauto.
- econstructor. eapply cl_and. eauto. eauto. eapply cl_or. eauto. eauto.
- eapply IHS2. eauto. omega.
- eapply IHS2. eauto. omega.
- econstructor. eapply IHS2. eauto. omega. eapply IHS2. eauto. omega.
- econstructor. eapply IHS2. eauto. omega. eauto.
- econstructor. eauto. eapply IHS2. eauto. omega.
- eapply IHS2. eauto. omega.
- eapply IHS2. eauto. omega.
(* htp left *)
- eapply index_max. eauto.
- eapply IHH1. eauto. omega.
- eapply IHH1. eauto. omega.
- eapply IHH1. eauto. omega.
(* htp right *)
- eapply closed_upgrade_gh. eauto. subst. eapply index_max in H1. omega.
- eapply IHH1 in H1. eapply closed_open. simpl. eapply closed_upgrade_gh. eauto. omega. econstructor. eauto. omega.
- eapply closed_upgrade_gh. eapply IHS2. eauto. omega. rewrite H4. rewrite app_length. omega.
- eapply IHH2 in H1. eapply IHH2 in H2.
eapply cl_and. eauto. eauto. omega. omega.
(* has_type *)
- subst. eapply closed_upgrade_gh. eauto. omega.
- eauto.
- econstructor. eapply closed_upgrade_gh. eauto. omega.
- eapply IHT in H1. inversion H1; subst. eauto. omega.
- econstructor. eauto.
- eapply IHT in H1. inversion H1. eauto. omega.
- eapply IHT in H1. inversion H1. eauto. omega.
- eapply IHS2. eauto. omega.
- econstructor. eapply IHT. eapply H1. omega. eapply IHT. eapply H2. omega.
(* dms_has_type *)
- econstructor.
- subst. econstructor. econstructor. eauto. eauto. eapply IHD. eauto. omega.
- subst. econstructor. econstructor. eauto. eauto. eapply IHD. eauto. omega.
Qed.
Lemma htp_extend : forall v1 x GH G1 T2 n,
htp GH G1 x T2 n ->
htp GH (v1::G1) x T2 n.
Proof. intros. eapply all_extend. eauto. eauto. Qed.
Lemma stp_extend : forall GH v1 G1 T1 T2 n,
stp GH G1 T1 T2 n ->
stp GH (v1::G1) T1 T2 n.
Proof. intros. eapply all_extend. eauto. eauto. Qed.
Lemma stp_extend_mult : forall GH G1 G' T1 T2 n,
stp GH G1 T1 T2 n ->
stp GH (G'++G1) T1 T2 n.
Proof. intros. induction G'. simpl. eauto. simpl. eapply stp_extend. eauto. Qed.
Lemma has_type_extend: forall GH G1 t T v n1,
has_type GH G1 t T n1 ->
has_type GH (v::G1) t T n1.
Proof. intros. eapply all_extend. eauto. eauto. Qed.
Lemma dms_has_type_extend: forall GH G1 t T v n1,
dms_has_type GH G1 t T n1 ->
dms_has_type GH (v::G1) t T n1.
Proof. intros. eapply all_extend. eauto. eauto. Qed.
Lemma has_type_extend_mult: forall GH G1 t T G' n1,
has_type GH G1 t T n1 ->
has_type GH (G'++G1) t T n1.
Proof. intros. induction G'. simpl. eauto. simpl. eapply has_type_extend. eauto. Qed.
Lemma htp_closed: forall x GH G1 T2 n,
htp GH G1 x T2 n ->
closed (length GH) (length G1) 0 T2.
Proof. intros. eapply all_closed with (x:=x). eauto. eauto. Qed.
Lemma htp_closed1: forall x GH G1 T2 n,
htp GH G1 x T2 n ->
x < length GH.
Proof. intros. eapply all_closed with (x:=x). eauto. eauto. Qed.
Lemma has_type_closed: forall GH G1 t T n1,
has_type GH G1 t T n1 ->
closed (length GH) (length G1) 0 T.
Proof. intros. eapply all_closed with (t:=t). eauto. eauto. Qed.
Lemma dms_has_type_closed: forall GH G1 t T n1,
dms_has_type GH G1 t T n1 ->
closed (length GH) (length G1) 0 T.
Proof. intros. eapply all_closed with (ds:=t). eauto. eauto. Qed.
Lemma has_type_closed_z: forall GH G1 z T n1,
has_type GH G1 (tvar false z) T n1 ->
z < length GH.
Proof.
intros. remember (tvar false z) as t. generalize dependent z.
induction H; intros; inversion Heqt; subst; eauto using index_max.
Qed.
Lemma has_type_closed1: forall GH G1 x T n1,
has_type GH G1 (tvar true x) T n1 ->
x < length G1.
Proof.
intros. remember (tvar true x) as t. generalize dependent x.
induction H; intros; inversion Heqt; subst; eauto using index_max.
Qed.
Lemma has_type_closed_b: forall G1 b x T n1,
has_type [] G1 (tvar b x) T n1 ->
b = true /\ x < length G1.
Proof.
intros.
remember [] as GH.
remember (tvar b x) as t.
generalize dependent x. generalize dependent b. generalize HeqGH. clear HeqGH.
induction H; intros; inversion Heqt; subst; eauto using index_max.
- simpl in H. inversion H.
Qed.
Lemma stp_closed1 : forall GH G1 T1 T2 n1,
stp GH G1 T1 T2 n1 ->
closed (length GH) (length G1) 0 T1.
Proof. intros. edestruct all_closed. eapply H0. eauto. eauto. Qed.
Lemma stp_closed2 : forall GH G1 T1 T2 n1,
stp GH G1 T1 T2 n1 ->
closed (length GH) (length G1) 0 T2.
Proof. intros. edestruct all_closed. destruct H1. eapply H1. eauto. eauto. Qed.
Lemma stpd_closed1 : forall GH G1 T1 T2,
stpd GH G1 T1 T2 ->
closed (length GH) (length G1) 0 T1.
Proof. intros. eu. eapply stp_closed1. eauto. Qed.
Lemma stpd_closed2 : forall GH G1 T1 T2,
stpd GH G1 T1 T2 ->
closed (length GH) (length G1) 0 T2.
Proof. intros. eu. eapply stp_closed2. eauto. Qed.
Lemma beq_nat_true_eq: forall A, beq_nat A A = true.
Proof. intros. eapply beq_nat_true_iff. eauto. Qed.
Fixpoint tsize (T: ty) { struct T }: nat :=
match T with
| TTop => 1
| TBot => 1
| TSel T1 l => 1
| TFun l T1 T2 => S (tsize T1 + tsize T2)
| TTyp l T1 T2 => S (tsize T1 + tsize T2)
| TBind T1 => S (tsize T1)
| TAnd T1 T2 => S (tsize T1 + tsize T2)
| TOr T1 T2 => S (tsize T1 + tsize T2)
end.
Lemma open_preserves_size: forall T b x j,
tsize T = tsize (open j (TVar b x) T).
Proof.
intros T. induction T; intros; simpl; eauto.
Qed.
Lemma stpd_refl_aux: forall n GH G1 T1,
closed (length GH) (length G1) 0 T1 ->
tsize T1 < n ->
stpd GH G1 T1 T1.
Proof.
intros n. induction n; intros; try omega.
inversion H; subst; simpl in H0.
- Case "bot". exists 1. eauto.
- Case "top". exists 1. eauto.
- Case "fun". eapply stpd_fun; eauto.
eapply IHn; eauto; omega.
eapply IHn; eauto.
simpl. apply closed_open. simpl. eapply closed_upgrade_gh. eauto. omega.
econstructor. omega.
rewrite <- open_preserves_size. omega.
- Case "typ". eapply stpd_typ; try eapply IHn; eauto; try omega.
- Case "sel". exists 1. eapply stp_selx. eauto.
- Case "bind".
remember (open 0 (TVar false (length GH)) T0) as T0'.
destruct (IHn (T0'::GH) G1 T0').
subst. eapply closed_open. eapply closed_upgrade_gh. eauto.
simpl. omega. simpl. econstructor. omega.
subst. rewrite <- open_preserves_size. omega.
eexists. eapply stp_bindx; eauto.
- Case "and".
destruct (IHn GH G1 T0 H1). omega.
destruct (IHn GH G1 T2 H2). omega.
eexists. eapply stp_and2. eapply stp_and11. eauto. eauto. eapply stp_and12. eauto. eauto.
- Case "or".
destruct (IHn GH G1 T0 H1). omega.
destruct (IHn GH G1 T2 H2). omega.
eexists. eapply stp_or1. eapply stp_or21. eauto. eauto. eapply stp_or22. eauto. eauto.
Qed.
Lemma stpd_refl: forall GH G1 T1,
closed (length GH) (length G1) 0 T1 ->
stpd GH G1 T1 T1.
Proof.
intros. apply stpd_refl_aux with (n:=S (tsize T1)); eauto.
Qed.
Lemma stpd_reg1 : forall GH G1 T1 T2,
stpd GH G1 T1 T2 ->
stpd GH G1 T1 T1.
Proof. intros. eapply stpd_refl. eapply stpd_closed1. eauto. Qed.
Lemma stpd_reg2 : forall GH G1 T1 T2,
stpd GH G1 T1 T2 ->
stpd GH G1 T2 T2.
Proof. intros. eapply stpd_refl. eapply stpd_closed2. eauto. Qed.
(*# Infrastructure Lemmas #*)
Ltac index_subst := match goal with
| H1: index ?x ?G = ?V1 , H2: index ?x ?G = ?V2 |- _ => rewrite H1 in H2; inversion H2; subst
| _ => idtac
end.
Ltac invty := match goal with
| H1: TBot = _ |- _ => inversion H1
| H1: TSel _ _ = _ |- _ => inversion H1
| H1: TTyp _ _ _ = _ |- _ => inversion H1
| H1: TVar _ _ = _ |- _ => inversion H1