-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdot_storeless_tidy.v
3757 lines (3502 loc) · 143 KB
/
dot_storeless_tidy.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 storeless
T ::= Bot | Top | T1 /\ T2 | T1 \/ T2 |
{ def m(x: S): U^x } | { type A: S..U } | p.A | { z => T^z }
t ::= p | t.m(t)
d ::= { def m(x: S): U^x = t^x } | { type A = T }
v ::= { z => d^z... }
p ::= x | v
*)
(* in small-step *)
Require Export SfLib.
Require Export Arith.EqNat.
Require Export Arith.Lt.
Definition id := nat.
Definition lb := nat.
Inductive vr : Type :=
| VarF: id(*absolute position in context, from origin, invariant under context extension*) -> vr
| VarB: id(*bound variable, de Bruijn, locally nameless style -- see open *) -> vr
| VObj: dms(*self is bound, de Bruijn, var*) -> vr
with ty : Type :=
| TBot : ty
| TTop : ty
| TFun : lb -> ty -> ty -> ty
| TMem : lb -> ty -> ty -> ty
| TSel : vr -> lb -> ty
| TBind : ty -> ty
| TAnd : ty -> ty -> ty
| TOr : ty -> ty -> ty
with tm : Type :=
| tvar : vr -> tm
| tapp : tm -> lb -> tm -> tm
with dm : Type :=
| dfun : ty -> ty -> tm -> dm
| dty : ty -> dm
(* we need our own list-like structure for stuctural recursion, e.g. in subst_tm *)
with dms : Type :=
| 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.
Definition tenv := list ty.
Hint Unfold tenv.
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.
Inductive vr_closed: nat(*i:abstract*) -> nat(*k:bound*) -> vr -> Prop :=
| clv_abs: forall i k x,
i > x ->
vr_closed i k (VarF x)
| clv_bound: forall i k x,
k > x ->
vr_closed i k (VarB x)
| clv_obj: forall i k ds,
dms_closed i (S k) ds ->
vr_closed i k (VObj ds)
with closed: nat -> nat -> ty -> Prop :=
| cl_bot: forall i k,
closed i k TBot
| cl_top: forall i k,
closed i k TTop
| cl_fun: forall i k l T1 T2,
closed i k T1 ->
closed i (S k) T2 ->
closed i k (TFun l T1 T2)
| cl_mem: forall i k l T1 T2,
closed i k T1 ->
closed i k T2 ->
closed i k (TMem l T1 T2)
| cl_sel: forall i k v1 l,
vr_closed i k v1 ->
closed i k (TSel v1 l)
| cl_bind: forall i k T1,
closed i (S k) T1 ->
closed i k (TBind T1)
| cl_and: forall i k T1 T2,
closed i k T1 ->
closed i k T2 ->
closed i k (TAnd T1 T2)
| cl_or: forall i k T1 T2,
closed i k T1 ->
closed i k T2 ->
closed i k (TOr T1 T2)
with tm_closed: nat -> nat -> tm -> Prop :=
| clt_var: forall i k v1,
vr_closed i k v1 ->
tm_closed i k (tvar v1)
| clt_app: forall i k t1 l t2,
tm_closed i k t1 ->
tm_closed i k t2 ->
tm_closed i k (tapp t1 l t2)
with dm_closed: nat -> nat -> dm -> Prop :=
| cld_fun: forall i k T1 T2 t2,
closed i k T1 ->
closed i (S k) T2 ->
tm_closed i (S k) t2 ->
dm_closed i k (dfun T1 T2 t2)
| cld_ty: forall i k T1,
closed i k T1 ->
dm_closed i k (dty T1)
with dms_closed: nat -> nat -> dms -> Prop :=
| clds_nil: forall i k,
dms_closed i k dnil
| clds_cons: forall i k d1 ds2,
dm_closed i k d1 ->
dms_closed i k ds2 ->
dms_closed i k (dcons d1 ds2)
.
Fixpoint vr_open (k: nat) (u: vr) (v: vr) { struct v }: vr :=
match v with
| VarF x => VarF x
| VarB x => if beq_nat k x then u else VarB x
| VObj dms => VObj (dms_open (S k) u dms)
end
with open (k: nat) (u: vr) (T: ty) { struct T }: ty :=
match T with
| TTop => TTop
| TBot => TBot
| TSel v1 l => TSel (vr_open k u v1) l
| TFun l T1 T2 => TFun l (open k u T1) (open (S k) u T2)
| TMem l T1 T2 => TMem 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
with tm_open (k: nat) (u: vr) (t: tm) { struct t }: tm :=
match t with
| tvar v => tvar (vr_open k u v)
| tapp t1 l t2 => tapp (tm_open k u t1) l (tm_open k u t2)
end
with dm_open (k: nat) (u: vr) (d: dm) { struct d }: dm :=
match d with
| dfun T1 T2 t2 => dfun (open k u T1) (open (S k) u T2) (tm_open (S k) u t2)
| dty T1 => dty (open k u T1)
end
with dms_open (k: nat) (u: vr) (ds: dms) { struct ds }: dms :=
match ds with
| dnil => dnil
| dcons d ds => dcons (dm_open k u d) (dms_open k u ds)
end.
Fixpoint vr_subst (u : vr) (v : vr) {struct v} : vr :=
match v with
| VarF i => if beq_nat i 0 then u else VarF (i-1)
| VarB i => VarB i
| VObj ds => VObj (dms_subst u ds)
end
with subst (u : vr) (T : ty) {struct T} : ty :=
match T with
| TTop => TTop
| TBot => TBot
| TMem l T1 T2 => TMem l (subst u T1) (subst u T2)
| TSel v1 l => TSel (vr_subst u v1) 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
with tm_subst (u : vr) (t : tm) { struct t } : tm :=
match t with
| tvar v => tvar (vr_subst u v)
| tapp t1 l t2 => tapp (tm_subst u t1) l (tm_subst u t2)
end
with dm_subst (u : vr) (d : dm) { struct d } : dm :=
match d with
| dfun T1 T2 t2 => dfun (subst u T1) (subst u T2) (tm_subst u t2)
| dty T1 => dty (subst u T1)
end
with dms_subst (u : vr) (ds : dms) { struct ds } : dms :=
match ds with
| dnil => dnil
| dcons d ds => dcons (dm_subst u d) (dms_subst u ds)
end.
Definition subst_dms (u:dms) (ds: dms) := dms_open 0 (VObj u) ds.
Definition subst_dm (x:dms) (D: dm) := dm_open 0 (VObj x) D.
Definition subst_tm (x:dms) (t: tm) := tm_open 0 (VObj x) t.
Definition subst_ty (x:dms) (T: ty) := open 0 (VObj x) T.
Definition substt (x:dms) (T: ty) := (subst (VObj x) T).
Hint Immediate substt.
Inductive has_type : tenv -> tm -> ty -> nat -> Prop :=
| T_VObj : forall GH ds ds' T T' TO n1,
dms_has_type (T'::GH) ds' T' n1 ->
T' = open 0 (VarF (length GH)) T ->
ds' = dms_open 0 (VarF (length GH)) ds ->
closed (length GH) 1 T ->
dms_closed (length GH) 1 ds ->
TO = open 0 (VObj ds) T ->
has_type GH (tvar (VObj ds)) TO (S n1)
| T_VarF : forall GH x T n1,
index x GH = Some T ->
closed (S x) 0 T ->
has_type GH (tvar (VarF x)) T (S n1)
| T_VarPack : forall GH v T1 T1' n1,
has_type GH (tvar v) T1' n1 ->
T1' = (open 0 v T1) ->
closed (length GH) 1 T1 ->
has_type GH (tvar v) (TBind T1) (S n1)
| T_VarUnpack : forall GH v T1 T1' n1,
has_type GH (tvar v) (TBind T1) n1 ->
T1' = (open 0 v T1) ->
closed (length GH) 0 T1' ->
has_type GH (tvar v) T1' (S n1)
| T_App : forall l T1 T2 GH t1 t2 n1 n2,
has_type GH t1 (TFun l T1 T2) n1 ->
has_type GH t2 T1 n2 ->
closed (length GH) 0 T2 ->
has_type GH (tapp t1 l t2) T2 (S (n1+n2))
| T_AppVar : forall l T1 T2 T2' GH t1 v2 n1 n2,
has_type GH t1 (TFun l T1 T2) n1 ->
has_type GH (tvar v2) T1 n2 ->
vr_closed (length GH) 0 v2 ->
T2' = (open 0 v2 T2) ->
closed (length GH) 0 T2' ->
has_type GH (tapp t1 l (tvar v2)) T2' (S (n1+n2))
| T_Sub : forall GH t T1 T2 n1 n2,
has_type GH t T1 n1 ->
stp GH T1 T2 n2 ->
has_type GH t T2 (S (n1 + n2))
with dm_has_type: tenv -> lb -> dm -> ty -> nat -> Prop :=
| D_Mem : forall GH l T11 n1,
closed (length GH) 0 T11 ->
dm_has_type GH l (dty T11) (TMem l T11 T11) (S n1)
| D_Fun : forall GH l T11 T12 T12' t12 t12' n1,
has_type (T11::GH) t12' T12' n1 ->
T12' = (open 0 (VarF (length GH)) T12) ->
t12' = (tm_open 0 (VarF (length GH)) t12) ->
closed (length GH) 0 T11 ->
closed (length GH) 1 T12 ->
tm_closed (length GH) 1 t12 ->
dm_has_type GH l (dfun T11 T12 t12) (TFun l T11 T12) (S n1)
with dms_has_type: tenv -> dms -> ty -> nat -> Prop :=
| D_Nil : forall GH n1,
dms_has_type GH dnil TTop (S n1)
| D_Cons : forall GH l d T ds TS n1 n2,
l = length (dms_to_list ds) ->
dm_has_type GH l d T n1 ->
dms_has_type GH ds TS n2 ->
dms_has_type GH (dcons d ds) (TAnd T TS) (S (n1+n2))
with stp: tenv -> ty -> ty -> nat -> Prop :=
| stp_bot: forall GH T n1,
closed (length GH) 0 T ->
stp GH TBot T (S n1)
| stp_top: forall GH T n1,
closed (length GH) 0 T ->
stp GH T TTop (S n1)
| stp_fun: forall GH l T1 T2 T3 T4 T2' T4' n1 n2,
T2' = (open 0 (VarF (length GH)) T2) ->
T4' = (open 0 (VarF (length GH)) T4) ->
closed (length GH) 1 T2 ->
closed (length GH) 1 T4 ->
stp GH T3 T1 n1 ->
stp (T3::GH) T2' T4' n2 ->
stp GH (TFun l T1 T2) (TFun l T3 T4) (S (n1+n2))
| stp_mem: forall GH l T1 T2 T3 T4 n1 n2,
stp GH T3 T1 n2 ->
stp GH T2 T4 n1 ->
stp GH (TMem l T1 T2) (TMem l T3 T4) (S (n1+n2))
| stp_selx: forall GH l v1 n1,
vr_closed (length GH) 0 v1 ->
stp GH (TSel v1 l) (TSel v1 l) (S n1)
| stp_strong_sel1: forall GH l ds TX n1,
index l (dms_to_list (subst_dms ds ds)) = Some (dty TX) ->
vr_closed (length GH) 0 (VObj ds) ->
stp GH (TSel (VObj ds) l) TX (S n1)
| stp_strong_sel2: forall GH l ds TX n1,
index l (dms_to_list (subst_dms ds ds)) = Some (dty TX) ->
vr_closed (length GH) 0 (VObj ds) ->
stp GH TX (TSel (VObj ds) l) (S n1)
| stp_sel1: forall GH l T2 x n1,
htp GH x (TMem l TBot T2) n1 ->
stp GH (TSel (VarF x) l) T2 (S n1)
| stp_sel2: forall GH l T1 x n1,
htp GH x (TMem l T1 TTop) n1 ->
stp GH T1 (TSel (VarF x) l) (S n1)
| stp_bind1: forall GH T1 T1' T2 n1,
htp (T1'::GH) (length GH) T2 n1 ->
T1' = (open 0 (VarF (length GH)) T1) ->
closed (length GH) 1 T1 ->
closed (length GH) 0 T2 ->
stp GH (TBind T1) T2 (S n1)
| stp_bindx: forall GH T1 T1' T2 T2' n1,
htp (T1'::GH) (length GH) T2' n1 ->
T1' = (open 0 (VarF (length GH)) T1) ->
T2' = (open 0 (VarF (length GH)) T2) ->
closed (length GH) 1 T1 ->
closed (length GH) 1 T2 ->
stp GH (TBind T1) (TBind T2) (S n1)
| stp_and11: forall GH T1 T2 T n1,
stp GH T1 T n1 ->
closed (length GH) 0 T2 ->
stp GH (TAnd T1 T2) T (S n1)
| stp_and12: forall GH T1 T2 T n1,
stp GH T2 T n1 ->
closed (length GH) 0 T1 ->
stp GH (TAnd T1 T2) T (S n1)
| stp_and2: forall GH T1 T2 T n1 n2,
stp GH T T1 n1 ->
stp GH T T2 n2 ->
stp GH T (TAnd T1 T2) (S (n1+n2))
| stp_or21: forall GH T1 T2 T n1,
stp GH T T1 n1 ->
closed (length GH) 0 T2 ->
stp GH T (TOr T1 T2) (S n1)
| stp_or22: forall GH T1 T2 T n1,
stp GH T T2 n1 ->
closed (length GH) 0 T1 ->
stp GH T (TOr T1 T2) (S n1)
| stp_or1: forall GH T1 T2 T n1 n2,
stp GH T1 T n1 ->
stp GH T2 T n2 ->
stp GH (TOr T1 T2) T (S (n1+n2))
| stp_trans: forall GH T1 T2 T3 n1 n2,
stp GH T1 T2 n1 ->
stp GH T2 T3 n2 ->
stp GH T1 T3 (S (n1+n2))
with htp: tenv -> id -> ty -> nat -> Prop :=
| htp_var: forall GH x TX n1,
index x GH = Some TX ->
closed (S x) 0 TX ->
htp GH x TX (S n1)
| htp_bind: forall GH x TX n1,
htp GH x (TBind TX) n1 ->
closed x 1 TX ->
htp GH x (open 0 (VarF x) TX) (S n1)
| htp_sub: forall GH GU GL 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 x T1 n1 ->
stp GL T1 T2 n2 ->
length GL = S x ->
GH = GU ++ GL ->
htp GH x T2 (S (n1+n2)).
Inductive vtp(*possible types*) : nat(*pack count*) -> dms -> ty -> nat(*size*) -> Prop :=
| vtp_top: forall m ds n1,
vr_closed 0 0 (VObj ds) ->
vtp m ds TTop (S n1)
| vtp_mem: forall m l ds TX T1 T2 n1 n2,
index l (dms_to_list (subst_dms ds ds)) = Some (dty TX) ->
stp [] T1 TX n1 ->
stp [] TX T2 n2 ->
vr_closed 0 0 (VObj ds) ->
vtp m ds (TMem l T1 T2) (S (n1+n2))
| vtp_fun: forall m ds T l T3 T4 T1 T2 t T2' T4' ds' T' T1x T2x tx T2x' tx' n1 n2 n3 n4,
index l (dms_to_list (subst_dms ds ds)) = Some (dfun T1 T2 t) ->
dms_has_type [T'] ds' T' n4 ->
T' = open 0 (VarF 0) T ->
ds' = dms_open 0 (VarF 0) ds ->
closed 0 1 T ->
index l (dms_to_list ds') = Some (dfun T1x T2x tx) ->
T2x' = (open 0 (VarF 1) T2x) ->
tx' = (tm_open 0 (VarF 1) tx) ->
has_type [T1x;T'] tx' T2x' n3 ->
stp [] T3 T1 n1 ->
T2' = (open 0 (VarF 0) T2) ->
T4' = (open 0 (VarF 0) T4) ->
closed 0 1 T2 ->
closed 0 1 T4 ->
tm_closed 1 1 tx ->
stp [T3] T2' T4' n2 ->
vr_closed 0 0 (VObj ds) ->
vtp m ds (TFun l T3 T4) (S (n1+n2+n3+n4))
| vtp_bind: forall m ds T2 n1,
vtp m ds (open 0 (VObj ds) T2) n1 ->
closed 0 1 T2 ->
vtp (S m) ds (TBind T2) (S (n1))
| vtp_sel: forall m ds dsy l TX n1,
index l (dms_to_list (subst_dms dsy dsy)) = Some (dty TX) ->
vr_closed 0 0 (VObj dsy) ->
vtp m ds TX n1 ->
vtp m ds (TSel (VObj dsy) l) (S (n1))
| vtp_and: forall m m1 m2 ds T1 T2 n1 n2,
vtp m1 ds T1 n1 ->
vtp m2 ds T2 n2 ->
m1 <= m -> m2 <= m ->
vtp m ds (TAnd T1 T2) (S (n1+n2))
| vtp_or1: forall m m1 m2 ds T1 T2 n1,
vtp m1 ds T1 n1 ->
closed 0 0 T2 ->
m1 <= m -> m2 <= m ->
vtp m ds (TOr T1 T2) (S (n1))
| vtp_or2: forall m m1 m2 ds T1 T2 n1,
vtp m1 ds T2 n1 ->
closed 0 0 T1 ->
m1 <= m -> m2 <= m ->
vtp m ds (TOr T1 T2) (S (n1))
.
Definition has_typed GH x T1 := exists n, has_type GH x T1 n.
Definition stpd GH T1 T2 := exists n, stp GH T1 T2 n.
Definition htpd GH x T1 := exists n, htp GH x T1 n.
Definition vtpd m x T1 := exists n, vtp m x T1 n.
Definition vtpdd m x T1 := exists m1 n, vtp m1 x T1 n /\ m1 <= m.
Hint Constructors stp.
Hint Constructors vtp.
Ltac ep := match goal with
| [ |- stp ?GH ?T1 ?T2 ?N ] => assert (exists (n:nat), stp GH 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]
| H: vtpd _ _ _ |- _ => destruct H as [? H]
| H: vtpdd _ _ _ |- _ => destruct H as [? [? [H ?]]]
end.
Lemma stpd_bot: forall GH T,
closed (length GH) 0 T ->
stpd GH TBot T.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd_top: forall GH T,
closed (length GH) 0 T ->
stpd GH T TTop.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd_fun: forall GH l T1 T2 T3 T4 T2' T4',
T2' = (open 0 (VarF (length GH)) T2) ->
T4' = (open 0 (VarF (length GH)) T4) ->
closed (length GH) 1 T2 ->
closed (length GH) 1 T4 ->
stpd GH T3 T1 ->
stpd (T3::GH) T2' T4' ->
stpd GH (TFun l T1 T2) (TFun l T3 T4).
Proof. intros. repeat eu. eexists. eapply stp_fun; eauto. Qed.
Lemma stpd_mem: forall GH l T1 T2 T3 T4,
stpd GH T3 T1 ->
stpd GH T2 T4 ->
stpd GH (TMem l T1 T2) (TMem l T3 T4).
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd_trans: forall GH T1 T2 T3,
stpd GH T1 T2 ->
stpd GH T2 T3 ->
stpd GH T1 T3.
Proof. intros. repeat eu. eexists. eauto. Qed.
Hint Constructors ty.
Hint Constructors stp.
Hint Constructors vtp.
Hint Constructors htp.
Hint Constructors has_type.
Hint Unfold has_typed.
Hint Unfold stpd.
Hint Unfold vtpd.
Hint Unfold vtpdd.
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.
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.
Scheme vr_mut := Induction for vr Sort Prop
with ty_mut := Induction for ty Sort Prop
with tm_mut := Induction for tm Sort Prop
with dm_mut := Induction for dm Sort Prop
with dms_mut := Induction for dms Sort Prop.
Combined Scheme syntax_mutind from vr_mut, ty_mut, tm_mut, dm_mut, dms_mut.
Scheme vr_cl_mut := Induction for vr_closed Sort Prop
with ty_cl_mut := Induction for closed Sort Prop
with tm_cl_mut := Induction for tm_closed Sort Prop
with dm_cl_mut := Induction for dm_closed Sort Prop
with dms_cl_mut := Induction for dms_closed Sort Prop.
Combined Scheme closed_mutind from vr_cl_mut, ty_cl_mut, tm_cl_mut, dm_cl_mut, dms_cl_mut.
Lemma closed_upgrade_gh_rec:
(forall i k v1, vr_closed i k v1 -> forall i1, i <= i1 -> vr_closed i1 k v1) /\
(forall i k T1, closed i k T1 -> forall i1, i <= i1 -> closed i1 k T1) /\
(forall i k t1, tm_closed i k t1 -> forall i1, i <= i1 -> tm_closed i1 k t1) /\
(forall i k d1, dm_closed i k d1 -> forall i1, i <= i1 -> dm_closed i1 k d1) /\
(forall i k ds1, dms_closed i k ds1 -> forall i1, i <= i1 -> dms_closed i1 k ds1).
Proof.
apply closed_mutind; intros; econstructor; eauto. omega.
Qed.
Lemma closed_upgrade_gh: forall i i1 k T1,
closed i k T1 -> i <= i1 -> closed i1 k T1.
Proof.
intros. eapply (proj1 (proj2 closed_upgrade_gh_rec)); eauto.
Qed.
Lemma closed_upgrade_rec:
(forall i k v1, vr_closed i k v1 -> forall k1, k <= k1 -> vr_closed i k1 v1) /\
(forall i k T1, closed i k T1 -> forall k1, k <= k1 -> closed i k1 T1) /\
(forall i k t1, tm_closed i k t1 -> forall k1, k <= k1 -> tm_closed i k1 t1) /\
(forall i k d1, dm_closed i k d1 -> forall k1, k <= k1 -> dm_closed i k1 d1) /\
(forall i k ds1, dms_closed i k ds1 -> forall k1, k <= k1 -> dms_closed i k1 ds1).
Proof.
apply closed_mutind; intros; econstructor; eauto;
try solve [omega];
try solve [eapply H; omega];
try solve [eapply H0; omega];
try solve [eapply H1; omega].
Qed.
Lemma closed_upgrade: forall i k k1 T1,
closed i k T1 -> k <= k1 -> closed i k1 T1.
Proof.
intros. eapply (proj1 (proj2 closed_upgrade_rec)); eauto.
Qed.
Lemma closed_open_rec:
(forall v1, forall j k v, vr_closed k (j+1) v1 -> vr_closed k j v -> vr_closed k j (vr_open j v v1)) /\
(forall T1, forall j k v, closed k (j+1) T1 -> vr_closed k j v -> closed k j (open j v T1)) /\
(forall t1, forall j k v, tm_closed k (j+1) t1 -> vr_closed k j v -> tm_closed k j (tm_open j v t1)) /\
(forall d1, forall j k v, dm_closed k (j+1) d1 -> vr_closed k j v -> dm_closed k j (dm_open j v d1)) /\
(forall ds1, forall j k v, dms_closed k (j+1) ds1 -> vr_closed k j v -> dms_closed k j (dms_open j v ds1)).
Proof.
apply syntax_mutind; intros;
try solve [
try inversion H; try inversion H0; try inversion H1; try inversion H2;
subst; simpl; econstructor;
try (eapply H; eauto); try (eapply H0; eauto); try (eapply H1; eauto);
eauto;
try solve [omega];
try solve [eapply (proj1 closed_upgrade_rec); eauto]
].
- inversion H; subst. simpl.
case_eq (beq_nat j i); intros E; eauto.
econstructor. eapply beq_nat_false_iff in E. omega.
Qed.
Lemma closed_open: forall j k v T, closed k (j+1) T -> vr_closed k j v -> closed k j (open j v T).
Proof.
intros. eapply (proj1 (proj2 closed_open_rec)); eauto.
Qed.
Lemma beq_nat_true_eq: forall A, beq_nat A A = true.
Proof. intros. eapply beq_nat_true_iff. eauto. Qed.
Lemma closed_no_open_rec:
(forall l j v, vr_closed l j v -> forall vx, v = vr_open j vx v) /\
(forall l j T, closed l j T -> forall vx, T = open j vx T) /\
(forall l j t, tm_closed l j t -> forall vx, t = tm_open j vx t) /\
(forall l j d, dm_closed l j d -> forall vx, d = dm_open j vx d) /\
(forall l j ds, dms_closed l j ds -> forall vx, ds = dms_open j vx ds).
Proof.
apply closed_mutind; intros; eauto; simpl;
try (rewrite <- H); try (rewrite <- H0); try (rewrite <- H1); eauto.
- simpl.
assert (k <> x) as A. omega.
eapply beq_nat_false_iff in A. rewrite A. reflexivity.
Qed.
Lemma closed_no_open: forall T x l j,
closed l j T ->
T = open j (VarF x) T.
Proof.
intros. eapply (proj1 (proj2 closed_no_open_rec)); eauto.
Qed.
Lemma closed_no_subst_rec:
(forall v j, vr_closed 0 j v -> forall vx, vr_subst vx v = v) /\
(forall T j, closed 0 j T -> forall vx, subst vx T = T) /\
(forall t j, tm_closed 0 j t -> forall vx, tm_subst vx t = t) /\
(forall d j, dm_closed 0 j d -> forall vx, dm_subst vx d = d) /\
(forall ds j, dms_closed 0 j ds -> forall vx, dms_subst vx ds = ds).
Proof.
apply syntax_mutind; intros;
try inversion H; try inversion H0; try inversion H1; try inversion H2;
subst; simpl; f_equal;
try solve [erewrite H; eauto];
try solve [erewrite H0; eauto];
try solve [erewrite H1; eauto];
eauto; try omega.
Qed.
Lemma closed_no_subst: forall T k TX,
closed 0 k T ->
subst TX T = T.
Proof.
intros. eapply (proj1 (proj2 closed_no_subst_rec)); eauto.
Qed.
Lemma closed_subst_rec:
(forall v j n V, vr_closed (n+1) j v -> vr_closed n 0 V -> vr_closed n j (vr_subst V v)) /\
(forall T j n V, closed (n+1) j T -> vr_closed n 0 V -> closed n j (subst V T)) /\
(forall t j n V, tm_closed (n+1) j t -> vr_closed n 0 V -> tm_closed n j (tm_subst V t)) /\
(forall d j n V, dm_closed (n+1) j d -> vr_closed n 0 V -> dm_closed n j (dm_subst V d)) /\
(forall ds j n V, dms_closed (n+1) j ds -> vr_closed n 0 V -> dms_closed n j (dms_subst V ds)).
Proof.
apply syntax_mutind; intros;
try inversion H; try inversion H0; try inversion H1; try inversion H2;
subst; simpl; try econstructor;
try solve [eapply H; eauto];
try solve [eapply H0; eauto];
try solve [eapply H1; eauto];
eauto; try omega;
try solve [case_eq (beq_nat i 0); intros E; [
(eapply (proj1 closed_upgrade_rec); eauto; omega) |
(econstructor; eapply beq_nat_false_iff in E; omega) ]].
Qed.
Lemma closed_subst: forall j n V T, closed (n+1) j T -> vr_closed n 0 V -> closed n j (subst V T).
Proof.
intros. eapply (proj1 (proj2 closed_subst_rec)); eauto.
Qed.
Lemma subst_open_distribute: forall k j0 vx v,
vr_closed k j0 vx ->
(forall v0 j, j0 <= j -> vr_subst vx (vr_open j v v0) = vr_open j (vr_subst vx v) (vr_subst vx v0)) /\
(forall T0 j, j0 <= j -> subst vx (open j v T0) = open j (vr_subst vx v) (subst vx T0)) /\
(forall t0 j, j0 <= j -> tm_subst vx (tm_open j v t0) = tm_open j (vr_subst vx v) (tm_subst vx t0)) /\
(forall d0 j, j0 <= j -> dm_subst vx (dm_open j v d0) = dm_open j (vr_subst vx v) (dm_subst vx d0)) /\
(forall ds0 j, j0 <= j -> dms_subst vx (dms_open j v ds0) = dms_open j (vr_subst vx v) (dms_subst vx ds0)).
Proof.
intros k j0 vx v HCx.
apply syntax_mutind; intros; simpl;
try inversion H0; try inversion H1; try inversion H2;
subst;
try rewrite H; try rewrite H0; try rewrite H1;
eauto.
- case_eq (beq_nat i 0); intros E; simpl; eauto.
eapply (proj1 closed_no_open_rec).
eapply (proj1 closed_upgrade_rec). eauto. omega.
- case_eq (beq_nat j i); intros E; simpl; eauto.
Qed.
Lemma subst_open_commute0_rec:
(forall v0 j TX, vr_closed 0 (j+1) v0 -> (vr_subst TX (vr_open j (VarF 0) v0)) = vr_open j TX v0) /\
(forall T0 j TX, closed 0 (j+1) T0 -> (subst TX (open j (VarF 0) T0)) = open j TX T0) /\
(forall t0 j TX, tm_closed 0 (j+1) t0 -> (tm_subst TX (tm_open j (VarF 0) t0)) = tm_open j TX t0) /\
(forall d0 j TX, dm_closed 0 (j+1) d0 -> (dm_subst TX (dm_open j (VarF 0) d0)) = dm_open j TX d0) /\
(forall ds0 j TX, dms_closed 0 (j+1) ds0 -> (dms_subst TX (dms_open j (VarF 0) ds0)) = dms_open j TX ds0).
Proof.
apply syntax_mutind; intros; simpl;
try inversion H0; try inversion H1; try inversion H2;
subst;
try rewrite H; try rewrite H0; try rewrite H1;
eauto.
- inversion H; subst. omega.
- inversion H; subst.
case_eq (beq_nat j i); intros E; simpl; eauto.
Qed.
Lemma subst_open_commute0: forall T0 j TX,
closed 0 (j+1) T0 ->
(subst TX (open j (VarF 0) T0)) = open j TX T0.
Proof.
intros. eapply (proj1 (proj2 subst_open_commute0_rec)); eauto.
Qed.
Lemma subst_open_commute1_rec: forall x x0,
vr_closed 0 0 (VObj x) ->
vr_closed 0 0 (VObj x0) ->
(forall v0 j, (vr_open j (VObj x0) (vr_subst (VObj x) v0)) = (vr_subst (VObj x) (vr_open j (VObj x0) v0))) /\
(forall T0 j, (open j (VObj x0) (subst (VObj x) T0)) = (subst (VObj x) (open j (VObj x0) T0))) /\
(forall t0 j, (tm_open j (VObj x0) (tm_subst (VObj x) t0)) = (tm_subst (VObj x) (tm_open j (VObj x0) t0))) /\
(forall d0 j, (dm_open j (VObj x0) (dm_subst (VObj x) d0)) = (dm_subst (VObj x) (dm_open j (VObj x0) d0))) /\
(forall ds0 j, (dms_open j (VObj x0) (dms_subst (VObj x) ds0)) = (dms_subst (VObj x) (dms_open j (VObj x0) ds0))).
Proof.
intros x x0 HCx HCx0.
apply syntax_mutind; intros; simpl;
try inversion H0; try inversion H1; try inversion H2;
subst;
try rewrite H; try rewrite H0; try rewrite H1;
eauto.
- case_eq (beq_nat i 0); intros E; simpl; eauto.
erewrite <- (proj2 (proj2 (proj2 (proj2 closed_no_open_rec)))).
reflexivity.
inversion HCx; subst.
eapply (proj2 (proj2 (proj2 (proj2 closed_upgrade_rec)))); eauto.
omega.
- case_eq (beq_nat j i); intros E; simpl; eauto.
erewrite (proj2 (proj2 (proj2 (proj2 closed_no_subst_rec)))).
reflexivity.
inversion HCx0; subst.
eassumption.
Qed.
Lemma subst_open_commute1: forall x x0,
vr_closed 0 0 (VObj x) ->
vr_closed 0 0 (VObj x0) -> forall j T0,
(open j (VObj x0) (subst (VObj x) T0))
= (subst (VObj x) (open j (VObj x0) T0)).
Proof.
intros x x0 Hx Hx0. intros.
eapply (proj1 (proj2 (subst_open_commute1_rec x x0 Hx Hx0))); eauto.
Qed.
Lemma subst_closed_id: forall x k T2,
closed 0 k T2 ->
substt x T2 = T2.
Proof. intros. eapply closed_no_subst. eauto. Qed.
Lemma closed_subst0: forall i k x T2,
vr_closed i 0 (VObj x) ->
closed (i + 1) k T2 ->
closed i k (substt x T2).
Proof. intros. eapply closed_subst. eauto. eauto. Qed.
Lemma closed_subst1: forall i k x T2,
closed i k T2 -> i <> 0 ->
vr_closed (i-1) 0 (VObj x) ->
closed (i-1) k (substt x T2).
Proof.
intros. eapply closed_subst.
assert ((i - 1 + 1) = i) as R. omega.
rewrite R. eauto. eauto.
Qed.
Lemma index_subst: forall GH TX T0 T3 x,
index (length (GH ++ [TX])) (T0 :: GH ++ [TX]) = Some T3 ->
index (length GH) (map (substt x) (T0 :: GH)) = Some (substt x T3).
Proof.
intros GH. induction GH; intros; inversion H.
- eauto.
- rewrite beq_nat_true_eq in H1. inversion H1. subst. simpl.
rewrite map_length. rewrite beq_nat_true_eq. eauto.
Qed.
Lemma index_subst1: forall GH TX T3 x x0,
index x0 (GH ++ [TX]) = Some T3 -> x0 <> 0 ->
index (x0-1) (map (substt x) GH) = Some (substt x T3).
Proof.
intros GH. induction GH; intros; inversion H.
- eapply beq_nat_false_iff in H0. rewrite H0 in H2. inversion H2.
- simpl.
assert (beq_nat (x0 - 1) (length (map (substt x) GH)) = beq_nat x0 (length (GH ++ [TX]))). {
case_eq (beq_nat x0 (length (GH ++ [TX]))); intros E.
eapply beq_nat_true_iff. rewrite map_length. eapply beq_nat_true_iff in E. subst x0.
rewrite app_length. simpl. omega.
eapply beq_nat_false_iff. eapply beq_nat_false_iff in E.
rewrite app_length in E. simpl in E. rewrite map_length.
destruct x0. destruct H0. reflexivity. omega.
}
rewrite H1. case_eq (beq_nat x0 (length (GH ++ [TX]))); intros E; rewrite E in H2.
inversion H2. subst. eauto. eauto.
Qed.
Lemma index_hit0: forall (GH:tenv) TX T2,
index 0 (GH ++ [TX]) = Some T2 -> T2 = TX.
Proof.
intros GH. induction GH; intros; inversion H.
- eauto.
- rewrite app_length in H1. simpl in H1.
remember (length GH + 1) as L. destruct L. omega. eauto.
Qed.
Lemma subst_open_rec: forall k x,
(vr_closed k 0 (VObj x)) ->
(forall v j n, (vr_subst (VObj x) (vr_open j (VarF (n+1)) v)) = (vr_open j (VarF n) (vr_subst (VObj x) v))) /\
(forall T j n, (subst (VObj x) (open j (VarF (n+1)) T)) = (open j (VarF n) (subst (VObj x) T))) /\
(forall t j n, (tm_subst (VObj x) (tm_open j (VarF (n+1)) t)) = (tm_open j (VarF n) (tm_subst (VObj x) t))) /\
(forall d j n, (dm_subst (VObj x) (dm_open j (VarF (n+1)) d)) = (dm_open j (VarF n) (dm_subst (VObj x) d))) /\
(forall ds j n, (dms_subst (VObj x) (dms_open j (VarF (n+1)) ds)) = (dms_open j (VarF n) (dms_subst (VObj x) ds))).
Proof.
intros k x Hx.
apply syntax_mutind; intros; simpl;
try inversion H0; try inversion H1; try inversion H2;
subst;
try rewrite H; try rewrite H0; try rewrite H1;
eauto.
- case_eq (beq_nat i 0); intros E; simpl; eauto.
f_equal.
erewrite <- (proj2 (proj2 (proj2 (proj2 closed_no_open_rec)))).
reflexivity. inversion Hx; subst.
eapply (proj2 (proj2 (proj2 (proj2 closed_upgrade_rec)))). eauto. omega.
- case_eq (beq_nat j i); intros E; simpl; eauto.
assert (beq_nat (n + 1) 0 = false) as E1. {
apply beq_nat_false_iff. omega.
}
rewrite E1.
f_equal. omega.
Qed.
Lemma subst_open: forall k x, vr_closed k 0 (VObj x) ->
forall TX n j,
(substt x (open j (VarF (n+1)) TX)) =
(open j (VarF n) (substt x TX)).
Proof.
intros k x Hx. intros. eapply (proj1 (proj2 (subst_open_rec k x Hx))); eauto.
Qed.
Lemma subst_open3: forall k x, vr_closed k 0 (VObj x) -> forall TX0 (GH:tenv) TX,
(substt x (open 0 (VarF (length (GH ++ [TX]))) TX0)) =
(open 0 (VarF (length GH)) (substt x TX0)).
Proof. intros. rewrite app_length. simpl. eapply subst_open. eauto. Qed.
Lemma subst_open4: forall k x, vr_closed k 0 (VObj x) -> forall T0 (GH:tenv) TX,
substt x (open 0 (VarF (length (GH ++ [TX]))) T0) =
open 0 (VarF (length (map (substt x) GH))) (substt x T0).
Proof. intros. rewrite map_length. eapply subst_open3. eauto. Qed.
Lemma subst_open5: forall k x, vr_closed k 0 (VObj x) -> forall (GH:tenv) T0 xi,
xi <> 0 -> substt x (open 0 (VarF xi) T0) =
open 0 (VarF (xi-1)) (substt x T0).
Proof.
intros. remember (xi-1) as n. assert (xi=n+1) as R. omega. rewrite R.
eapply subst_open. eauto.
Qed.
Lemma subst_open_commute0b_rec: forall k x,
(vr_closed k 0 (VObj x)) ->
(forall v1 n, vr_subst (VObj x) (vr_open n (VarF 0) v1) = vr_open n (VObj x) (vr_subst (VObj x) v1)) /\
(forall T1 n, subst (VObj x) (open n (VarF 0) T1) = open n (VObj x) (subst (VObj x) T1)) /\
(forall t1 n, tm_subst (VObj x) (tm_open n (VarF 0) t1) = tm_open n (VObj x) (tm_subst (VObj x) t1)) /\
(forall d1 n, dm_subst (VObj x) (dm_open n (VarF 0) d1) = dm_open n (VObj x) (dm_subst (VObj x) d1)) /\
(forall ds1 n, dms_subst (VObj x) (dms_open n (VarF 0) ds1) = dms_open n (VObj x) (dms_subst (VObj x) ds1)).
Proof.
intros k x Hx.
apply syntax_mutind; intros; simpl;
try inversion H0; try inversion H1; try inversion H2;
subst;
try rewrite H; try rewrite H0; try rewrite H1;
eauto.
- case_eq (beq_nat i 0); intros E; simpl; eauto.
erewrite <- (proj2 (proj2 (proj2 (proj2 closed_no_open_rec)))).
reflexivity.
inversion Hx; subst.
eapply (proj2 (proj2 (proj2 (proj2 closed_upgrade_rec)))); eauto.
omega.
- case_eq (beq_nat n i); intros E; simpl; eauto.
Qed.
Lemma subst_open_commute0b: forall k x,
(vr_closed k 0 (VObj x)) -> forall T1 n,
substt x (open n (VarF 0) T1) = open n (VObj x) (substt x T1).
Proof.
unfold substt.
intros k x Hx. intros.
eapply (proj1 (proj2 (subst_open_commute0b_rec k x Hx))); eauto.
Qed.
Lemma subst_open_commute_z_rec: forall k x,
(vr_closed k 0 (VObj x)) ->
(forall v1 z n, vr_subst (VObj x) (vr_open n (VarF (z + 1)) v1) = vr_open n (VarF z) (vr_subst (VObj x) v1)) /\
(forall T1 z n, subst (VObj x) (open n (VarF (z + 1)) T1) = open n (VarF z) (subst (VObj x) T1)) /\
(forall t1 z n, tm_subst (VObj x) (tm_open n (VarF (z + 1)) t1) = tm_open n (VarF z) (tm_subst (VObj x) t1)) /\
(forall d1 z n, dm_subst (VObj x) (dm_open n (VarF (z + 1)) d1) = dm_open n (VarF z) (dm_subst (VObj x) d1)) /\
(forall ds1 z n, dms_subst (VObj x) (dms_open n (VarF (z + 1)) ds1) = dms_open n (VarF z) (dms_subst (VObj x) ds1)).
Proof.
intros k x Hx.
apply syntax_mutind; intros; simpl;
try inversion H0; try inversion H1; try inversion H2;
subst;
try rewrite H; try rewrite H0; try rewrite H1;
eauto.
- case_eq (beq_nat i 0); intros E; simpl; eauto.
erewrite <- (proj2 (proj2 (proj2 (proj2 closed_no_open_rec)))).
reflexivity.
inversion Hx; subst.
eapply (proj2 (proj2 (proj2 (proj2 closed_upgrade_rec)))); eauto.
omega.
- case_eq (beq_nat n i); intros E; simpl; eauto.
assert (beq_nat (z + 1) 0 = false) as E1. {
eapply beq_nat_false_iff. omega.
}
rewrite E1. f_equal. omega.
Qed.
Lemma subst_open_commute_z: forall k x,
(vr_closed k 0 (VObj x)) -> forall T1 z n,
subst (VObj x) (open n (VarF (z + 1)) T1) =
open n (VarF z) (subst (VObj x) T1).
Proof.
intros k x Hx. intros.
eapply (proj1 (proj2 (subst_open_commute_z_rec k x Hx))); eauto.
Qed.
Lemma length_subst_dms: forall ds x,
(length (dms_to_list ds))=(length (dms_to_list (subst_dms x ds))).
Proof.
intros. induction ds; eauto.
simpl. rewrite IHds. reflexivity.
Qed.
Lemma length_dms_subst: forall ds x,