-
Notifications
You must be signed in to change notification settings - Fork 11
/
dot-smallstep4.v
2832 lines (2589 loc) · 110 KB
/
dot-smallstep4.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
(* smallstep proof *)
(* compared to 3c, it adds multiple members *)
Require Export SfLib.
Require Export Arith.EqNat.
Require Export Arith.Lt.
Module STLC.
Definition id := nat.
Definition lb := nat.
Inductive ty : Type :=
| TBot : ty
| TTop : ty
| TFun : lb -> ty -> ty -> ty
| TMem : lb -> ty -> ty -> ty (* intro *)
| TVar : bool -> id -> ty
| TVarB : id -> ty
| TSel : ty -> lb -> ty (* elim *)
| TBind : ty -> ty
| TAnd : ty -> ty -> ty
.
Inductive tm : Type :=
| tvar : bool -> id -> tm
| tobj : dms -> 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.
Inductive vl : Type :=
| vobj : dms -> vl
.
Definition venv := list vl.
Definition tenv := list ty.
Hint Unfold venv.
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 closed: nat -> nat -> nat -> 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_mem: forall i j k l T1 T2,
closed i j k T1 ->
closed i j k T2 ->
closed i j k (TMem l T1 T2)
| cl_var0: forall i j k x,
i > x ->
closed i j k (TVar false x)
| cl_var1: forall i j k x,
j > x ->
closed i j k (TVar true x)
| cl_varB: forall i j k x,
k > x ->
closed i j k (TVarB x)
| cl_sel: forall i j k T1 l,
closed i j k T1 ->
closed i j k (TSel T1 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)
.
Fixpoint open (k: nat) (u: ty) (T: ty) { struct T }: ty :=
match T 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
| TTop => TTop
| TBot => TBot
| TSel T1 l => TSel (open k u T1) 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)
end.
Fixpoint subst (U : ty) (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 T1 l => TSel (subst U T1) l
| TVarB i => TVarB i
| TVar true i => TVar true i
| TVar false i => if beq_nat i 0 then U else TVar false (i-1)
| 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)
end.
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 (subst (TVar true u) T1) (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.
Definition substt x T := (subst (TVar true x) T).
Hint Immediate substt.
Inductive has_type : tenv -> venv -> tm -> ty -> nat -> Prop :=
| T_Varx : 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_Vary : 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 ->
closed (length GH) (length G1) 1 T ->
T' = open 0 (TVar false (length GH)) 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 ->
stp2 GH G1 T1 T2 n2 ->
has_type GH G1 t T2 (S (n1 + n2))
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_Mem : 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 (TMem l T11 T11) TS ->
dms_has_type GH G1 (dcons (dty T11) ds) T (S n1)
| D_Abs : forall GH G1 l T11 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 ->
dms_has_type GH G1 (dcons (dfun T11 T12 t12) ds) T (S (n1+n2))
with stp2: tenv -> venv -> ty -> ty -> nat -> Prop :=
| stp2_bot: forall GH G1 T n1,
closed (length GH) (length G1) 0 T ->
stp2 GH G1 TBot T (S n1)
| stp2_top: forall GH G1 T n1,
closed (length GH) (length G1) 0 T ->
stp2 GH G1 T TTop (S n1)
| stp2_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 ->
stp2 GH G1 T3 T1 n1 ->
stp2 (T3::GH) G1 T2' T4' n2 ->
stp2 GH G1 (TFun l T1 T2) (TFun l T3 T4) (S (n1+n2))
| stp2_mem: forall GH G1 l T1 T2 T3 T4 n1 n2,
stp2 GH G1 T3 T1 n2 ->
stp2 GH G1 T2 T4 n1 ->
stp2 GH G1 (TMem l T1 T2) (TMem l T3 T4) (S (n1+n2))
| stp2_varx: forall GH G1 x n1,
x < length G1 ->
stp2 GH G1 (TVar true x) (TVar true x) (S n1)
| stp2_varax: forall GH G1 x n1,
x < length GH ->
stp2 GH G1 (TVar false x) (TVar false x) (S n1)
| stp2_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) ->
stp2 [] G1 TX T2 n1 ->
stp2 GH G1 (TSel (TVar true x) l) T2 (S n1)
| stp2_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) ->
stp2 [] G1 T1 TX n1 ->
stp2 GH G1 T1 (TSel (TVar true x) l) (S n1)
| stp2_sel1: forall GH G1 l T2 x n1,
htp GH G1 x (TMem l TBot T2) n1 ->
stp2 GH G1 (TSel (TVar false x) l) T2 (S n1)
| stp2_sel2: forall GH G1 l T1 x n1,
htp GH G1 x (TMem l T1 TTop) n1 ->
stp2 GH G1 T1 (TSel (TVar false x) l) (S n1)
| stp2_selx: forall GH G1 l T1 n1,
closed (length GH) (length G1) 0 T1 ->
stp2 GH G1 (TSel T1 l) (TSel T1 l) (S n1)
| stp2_bind1: forall GH G1 T1 T1' T2 n1,
htp (T1'::GH) G1 (length GH) T2 n1 ->
T1' = (open 0 (TVar false (length GH)) T1) ->
closed (length GH) (length G1) 1 T1 ->
closed (length GH) (length G1) 0 T2 ->
stp2 GH G1 (TBind T1) T2 (S n1)
| stp2_bindx: forall GH G1 T1 T1' T2 T2' n1,
htp (T1'::GH) G1 (length GH) 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 ->
stp2 GH G1 (TBind T1) (TBind T2) (S n1)
| stp2_and11: forall GH G1 T1 T2 T n1,
stp2 GH G1 T1 T n1 ->
closed (length GH) (length G1) 0 T2 ->
stp2 GH G1 (TAnd T1 T2) T (S n1)
| stp2_and12: forall GH G1 T1 T2 T n1,
stp2 GH G1 T2 T n1 ->
closed (length GH) (length G1) 0 T1 ->
stp2 GH G1 (TAnd T1 T2) T (S n1)
| stp2_and2: forall GH G1 T1 T2 T n1 n2,
stp2 GH G1 T T1 n1 ->
stp2 GH G1 T T2 n2 ->
stp2 GH G1 T (TAnd T1 T2) (S (n1+n2))
| stp2_transf: forall GH G1 T1 T2 T3 n1 n2,
stp2 GH G1 T1 T2 n1 ->
stp2 GH G1 T2 T3 n2 ->
stp2 GH G1 T1 T3 (S (n1+n2))
with htp: tenv -> venv -> nat -> 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_bind: forall GH G1 x TX n1,
htp GH G1 x (TBind TX) n1 ->
closed 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 ->
stp2 GL G1 T1 T2 n2 ->
length GL = S x ->
GH = GU ++ GL ->
htp GH G1 x T2 (S (n1+n2))
with vtp : nat -> venv -> nat -> ty -> nat -> Prop :=
| vtp_top: forall m G1 x n1,
x < length G1 ->
vtp m G1 x TTop (S n1)
| vtp_mem: 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) ->
stp2 [] G1 T1 TX n1 ->
stp2 [] G1 TX T2 n2 ->
vtp m G1 x (TMem l T1 T2) (S (n1+n2))
| vtp_fun: forall m G1 x l ds dsx 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 T1 T2 t) ->
subst_dms x dsx = ds ->
dms_has_type [T'] G1 dsx T' n4 ->
subst_dm x (dfun T1x T2x tx) = (dfun T1 T2 t) ->
T2x' = (open 0 (TVar false 1) T2x) ->
has_type [T1x;T'] G1 tx T2x' n3 ->
stp2 [] 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 ->
stp2 [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))
.
Definition has_typed GH G1 x T1 := exists n, has_type GH G1 x T1 n.
Definition stpd2 GH G1 T1 T2 := exists n, stp2 GH G1 T1 T2 n.
Definition htpd GH G1 x T1 := exists n, htp GH G1 x T1 n.
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 stp2.
Hint Constructors vtp.
Ltac ep := match goal with
| [ |- stp2 ?GH ?G1 ?T1 ?T2 ?N ] => assert (exists (n:nat), stp2 GH G1 T1 T2 n) as EEX
end.
Ltac eu := match goal with
| H: has_typed _ _ _ _ |- _ => destruct H as [? H]
| H: stpd2 _ _ _ _ |- _ => destruct H as [? H]
| H: htpd _ _ _ _ |- _ => destruct H as [? H]
| H: vtpd _ _ _ _ |- _ => destruct H as [? H]
| H: vtpdd _ _ _ _ |- _ => destruct H as [? [? [H ?]]]
end.
Lemma stpd2_bot: forall GH G1 T,
closed (length GH) (length G1) 0 T ->
stpd2 GH G1 TBot T.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd2_top: forall GH G1 T,
closed (length GH) (length G1) 0 T ->
stpd2 GH G1 T TTop.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd2_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 ->
stpd2 GH G1 T3 T1 ->
stpd2 (T3::GH) G1 T2' T4' ->
stpd2 GH G1 (TFun l T1 T2) (TFun l T3 T4).
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd2_mem: forall GH G1 l T1 T2 T3 T4,
stpd2 GH G1 T3 T1 ->
stpd2 GH G1 T2 T4 ->
stpd2 GH G1 (TMem l T1 T2) (TMem l T3 T4).
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd2_transf: forall GH G1 T1 T2 T3,
stpd2 GH G1 T1 T2 ->
stpd2 GH G1 T2 T3 ->
stpd2 GH G1 T1 T3.
Proof. intros. repeat eu. eexists. eauto. Qed.
Hint Constructors ty.
Hint Constructors vl.
Hint Constructors stp2.
Hint Constructors vtp.
Hint Constructors htp.
Hint Constructors has_type.
Hint Unfold has_typed.
Hint Unfold stpd2.
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.
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.
simpl. omega.
Qed.
Lemma all_extend: forall ni,
(forall GH v1 G1 T1 T2 n,
stp2 GH G1 T1 T2 n -> n < ni ->
stp2 GH (v1::G1) T1 T2 n) /\
(forall m v1 x G1 T2 n,
vtp m G1 x T2 n -> n < ni ->
vtp m (v1::G1) x 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. simpl. eauto.
- econstructor. eauto.
- 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 closed_extend. eauto.
- econstructor. eapply IHn. eauto. omega. eauto. eapply closed_extend. eauto. eapply closed_extend. eauto.
- eapply stp2_bindx. eapply IHn. eauto. omega. eauto. eauto. eapply closed_extend. eauto. eapply closed_extend. eauto.
- eapply stp2_and11. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- eapply stp2_and12. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- eapply stp2_and2. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- eapply stp2_transf. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
(* vtp *)
- econstructor. simpl. eauto.
- econstructor. eapply index_extend. eauto. eauto. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- econstructor. eapply index_extend. eauto. eauto. eauto. eapply IHn. eauto. omega. eauto. eauto. eapply IHn. eauto. omega. eapply IHn. eauto. omega. eauto. eauto. eapply closed_extend. eauto. eapply closed_extend. eauto. eapply IHn. eauto. omega.
- 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.
(* htp *)
- econstructor. eauto. eapply closed_extend. eauto.
- eapply htp_bind. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- eapply htp_sub. eapply IHn. eauto. omega. eapply IHn. eauto. omega. eauto. eauto.
(* 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. eapply closed_extend. eauto. 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.
(* 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.
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. 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. 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.
eapply IHclosed2. omega.
omega.
eapply IHclosed. omega.
Qed.
Lemma closed_open: forall j k n b V T, closed k n (j+1) T -> 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; eauto; try eapply IHT2; eauto; try eapply IHT; eauto.
- eapply closed_upgrade; eauto.
- Case "TVarB". simpl.
case_eq (beq_nat j i); intros E. eauto.
econstructor. eapply beq_nat_false_iff in E. omega.
- eapply closed_upgrade; eauto.
Qed.
Lemma all_closed: forall ni,
(forall GH G1 T1 T2 n,
stp2 GH G1 T1 T2 n -> n < ni ->
closed (length GH) (length G1) 0 T1) /\
(forall GH G1 T1 T2 n,
stp2 GH G1 T1 T2 n -> n < ni ->
closed (length GH) (length G1) 0 T2) /\
(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) /\
(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 [IHV1 [IHV2 [IHH1 [IHH2 [IHT IHD]]]]]]].
(* stp left *)
- econstructor.
- eauto.
- econstructor. eapply IHS2. eauto. omega. eauto.
- econstructor. eapply IHS2. eauto. omega. eapply IHS1. eauto. omega.
- econstructor. simpl. eauto.
- econstructor. eauto.
- 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 IHS1. eauto. omega. eauto.
- econstructor. eauto. 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.
- econstructor. simpl. eauto.
- econstructor. eauto.
- 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.
- eapply IHS2. eauto. omega.
- eapply IHS2. eauto. omega.
- econstructor. eapply IHS2. eauto. omega. eapply IHS2. eauto. omega.
- eapply IHS2. eauto. omega.
(* vtp left *)
- eauto.
- eapply index_max. eauto.
- eapply index_max. eauto.
- eapply IHV1. eauto. omega.
- eapply IHV1. eauto. omega.
- eapply IHV1. eauto. omega.
(* vtp right *)
- econstructor.
- change 0 with (length ([]:tenv)) at 1. econstructor. eapply IHS1. eauto. omega. eapply IHS2. eauto. omega.
- change 0 with (length ([]:tenv)) at 1. econstructor. eapply IHS1. eauto. omega. eauto.
- econstructor. eauto. (* eapply IHV2 in H1. eauto. omega. *)
- econstructor. econstructor. eapply index_max. eauto.
- econstructor. eapply IHV2. eauto. omega. eapply IHV2. eauto. omega.
(* htp left *)
- eapply index_max. eauto.
- 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.
(* 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.
(* dms_has_type *)
- econstructor.
- subst. econstructor. econstructor. eauto. eauto. eapply IHD. eauto. omega.
- subst. econstructor. econstructor. eauto. eauto. eapply IHD. 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 all_extend. eauto. eauto. 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 stp2_extend : forall GH v1 G1 T1 T2 n,
stp2 GH G1 T1 T2 n ->
stp2 GH (v1::G1) T1 T2 n.
Proof. intros. eapply all_extend. eauto. eauto. Qed.
Lemma stp2_extend_mult : forall GH G1 G' T1 T2 n,
stp2 GH G1 T1 T2 n ->
stp2 GH (G'++G1) T1 T2 n.
Proof. intros. induction G'. simpl. eauto. simpl. eapply stp2_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 vtp_closed: forall m G1 x T2 n1,
vtp m G1 x T2 n1 ->
closed 0 (length G1) 0 T2.
Proof. intros. eapply 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 all_closed. 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 stp2_closed1 : forall GH G1 T1 T2 n1,
stp2 GH G1 T1 T2 n1 ->
closed (length GH) (length G1) 0 T1.
Proof. intros. edestruct all_closed. eapply H0. eauto. eauto. Qed.
Lemma stp2_closed2 : forall GH G1 T1 T2 n1,
stp2 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 stpd2_closed1 : forall GH G1 T1 T2,
stpd2 GH G1 T1 T2 ->
closed (length GH) (length G1) 0 T1.
Proof. intros. eu. eapply stp2_closed1. eauto. Qed.
Lemma stpd2_closed2 : forall GH G1 T1 T2,
stpd2 GH G1 T1 T2 ->
closed (length GH) (length G1) 0 T2.
Proof. intros. eu. eapply stp2_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
| TVar b x => 1
| TVarB x => 1
| TTop => 1
| TBot => 1
| TSel T1 l => S (tsize T1)
| TFun l T1 T2 => S (tsize T1 + tsize T2)
| TMem l T1 T2 => S (tsize T1 + tsize T2)
| TBind T1 => S (tsize T1)
| TAnd 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.
- destruct (beq_nat j i); eauto.
Qed.
Lemma stpd2_refl_aux: forall n GH G1 T1,
closed (length GH) (length G1) 0 T1 ->
tsize T1 < n ->
stpd2 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 stpd2_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 "mem". eapply stpd2_mem; try eapply IHn; eauto; try omega.
- Case "var0". exists 1. eauto.
- Case "var1". exists 1. eauto.
- Case "varb". omega.
- Case "sel". exists 1. eapply stp2_selx. eauto.
- Case "bind".
eexists. eapply stp2_bindx. eapply htp_var. simpl. rewrite beq_nat_true_eq. eauto.
instantiate (1:=open 0 (TVar false (length GH)) T0).
eapply closed_open. eapply closed_upgrade_gh. eauto. omega. econstructor. omega.
eauto. eauto. eauto. eauto.
- Case "and".
destruct (IHn GH G1 T0 H1). omega.
destruct (IHn GH G1 T2 H2). omega.
eexists. eapply stp2_and2. eapply stp2_and11. eauto. eauto. eapply stp2_and12. eauto. eauto.
Grab Existential Variables.
apply 0.
Qed.
Lemma stpd2_refl: forall GH G1 T1,
closed (length GH) (length G1) 0 T1 ->
stpd2 GH G1 T1 T1.
Proof.
intros. apply stpd2_refl_aux with (n:=S (tsize T1)); eauto.
Qed.
Lemma stpd2_reg1 : forall GH G1 T1 T2,
stpd2 GH G1 T1 T2 ->
stpd2 GH G1 T1 T1.
Proof. intros. eapply stpd2_refl. eapply stpd2_closed1. eauto. Qed.
Lemma stpd2_reg2 : forall GH G1 T1 T2,
stpd2 GH G1 T1 T2 ->
stpd2 GH G1 T2 T2.
Proof. intros. eapply stpd2_refl. eapply stpd2_closed2. eauto. Qed.
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: TMem _ _ _ = _ |- _ => inversion H1
| H1: TVar _ _ = _ |- _ => inversion H1
| H1: TFun _ _ _ = _ |- _ => inversion H1
| H1: TBind _ = _ |- _ => inversion H1
| H1: TAnd _ _ = _ |- _ => inversion H1
| _ => idtac
end.
Ltac invstp_var := match goal with
| H1: stp2 _ true _ _ TBot (TVar _ _) _ |- _ => inversion H1
| H1: stp2 _ true _ _ TTop (TVar _ _) _ |- _ => inversion H1
| H1: stp2 _ true _ _ (TFun _ _ _) (TVar _ _) _ |- _ => inversion H1
| H1: stp2 _ true _ _ (TMem _ _ _) (TVar _ _) _ |- _ => inversion H1
| H1: stp2 _ true _ _ (TAnd _ _) (TVar _ _) _ |- _ => inversion H1
| _ => idtac
end.
Lemma closed_no_open: forall T x k l j,
closed l k j T ->
T = open j (TVar false x) T.
Proof.
intros. induction H; intros; eauto;
try solve [compute; compute in IHclosed; rewrite <-IHclosed; auto];
try solve [compute; compute in IHclosed1; compute in IHclosed2; rewrite <-IHclosed1; rewrite <-IHclosed2; auto].
Case "TSelB".
simpl.
assert (k <> x0). omega.
apply beq_nat_false_iff in H0.
rewrite H0. auto.
Qed.
Lemma closed_no_subst: forall T j k TX,
closed 0 j k T ->
subst TX T = T.
Proof.
intros T. induction T; intros; inversion H; simpl; eauto;
try rewrite (IHT j (S k) TX); eauto;
try rewrite (IHT j k TX); eauto;
try rewrite (IHT1 j k TX); eauto; subst.
rewrite (IHT2 j (S k) TX); eauto.
rewrite (IHT2 j k TX); eauto.
omega.
eapply closed_upgrade; eauto.
rewrite (IHT2 j k TX); eauto.
Qed.
Lemma closed_subst: forall j n k V T, closed (n+1) k j T -> closed n k 0 V -> closed n k j (subst V T).
Proof.
intros. generalize dependent j. induction T; intros; inversion H; try econstructor; try eapply IHT1; eauto; try eapply IHT2; eauto; try eapply IHT; eauto.
- Case "TSelH". simpl.
case_eq (beq_nat i 0); intros E. eapply closed_upgrade. eapply closed_upgrade_gh. eauto. eauto. omega. econstructor. subst.
assert (i > 0). eapply beq_nat_false_iff in E. omega. omega.
Qed.
(* not used? *)
Lemma subst_open_commute_m: forall j k n m V T2, closed (n+1) k (j+1) T2 -> closed m k 0 V ->
subst V (open j (TVar false (n+1)) T2) = open j (TVar false n) (subst V T2).
Proof.
intros. generalize dependent j. generalize dependent n.
induction T2; intros; inversion H; simpl; eauto;
try rewrite IHT2_1; try rewrite IHT2_2; try rewrite IHT2; eauto.
simpl. case_eq (beq_nat i 0); intros E.
eapply closed_no_open. eapply closed_upgrade. eauto. omega.
simpl. eauto.
simpl. case_eq (beq_nat j i); intros E.
simpl. case_eq (beq_nat (n+1) 0); intros E2. eapply beq_nat_true_iff in E2. omega.
assert (n+1-1 = n) as A. omega. rewrite A. eauto.
eauto.
Qed.
(* not used? *)
Lemma subst_open_commute: forall j k n V T2, closed (n+1) k (j+1) T2 -> closed 0 k 0 V ->
subst V (open j (TVar false (n+1)) T2) = open j (TVar false n) (subst V T2).
Proof.