-
Notifications
You must be signed in to change notification settings - Fork 11
/
dot-smallstep1.v
1535 lines (1343 loc) · 58.1 KB
/
dot-smallstep1.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 *)
Require Export SfLib.
Require Export Arith.EqNat.
Require Export Arith.Lt.
Module STLC.
Definition id := nat.
Inductive ty : Type :=
| TBot : ty
| TTop : ty
| TBool : ty
| TFun : ty -> ty -> ty
| TMem : ty -> ty -> ty (* intro *)
| TVar : bool -> id -> ty
| TVarB : id -> ty
| TSel : ty -> ty (* elim *)
| TBind : ty -> ty
| TAnd : ty -> ty -> ty
.
Inductive tm : Type :=
| tvar : bool -> id -> tm
| tbool : bool -> tm
| tobj : dm -> tm (* todo: multiple members *)
| tapp : tm -> tm -> tm
with dm : Type :=
| dfun : ty -> ty -> tm -> dm
| dty : ty -> dm
.
Inductive vl : Type :=
| vbool : bool -> vl
| vobj : dm -> 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_bool: forall i j k,
closed i j k TBool
| cl_fun: forall i j k T1 T2,
closed i j k T1 ->
closed i j k T2 ->
closed i j k (TFun T1 T2)
| cl_mem: forall i j k T1 T2,
closed i j k T1 ->
closed i j k T2 ->
closed i j k (TMem 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,
closed i j k T1 ->
closed i j k (TSel T1)
| 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
| TBool => TBool
| TSel T1 => TSel (open k u T1)
| TFun T1 T2 => TFun (open k u T1) (open k u T2)
| TMem T1 T2 => TMem (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
| TBool => TBool
| TMem T1 T2 => TMem (subst U T1) (subst U T2)
| TSel T1 => TSel (subst U T1)
| 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 T1 T2 => TFun (subst U T1) (subst U T2)
| TBind T2 => TBind (subst U T2)
| TAnd T1 T2 => TAnd (subst U T1) (subst U T2)
end.
Inductive has_type : tenv -> venv -> tm -> ty -> nat -> Prop :=
| T_Varx : forall m GH G1 x T n1,
vtp m G1 x T n1 ->
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)
(* todo: add var pack/unpack *)
(* todo: recursive objects with multiple members *)
| T_Mem : forall GH G1 T11 n1,
closed (length GH) (length G1) 0 T11 ->
has_type GH G1 (tobj (dty T11)) (TMem T11 T11) (S n1)
| T_Abs : forall GH G1 T11 T12 t12 n1,
has_type (T11::GH) G1 t12 T12 n1 ->
closed (length GH) (length G1) 0 T11 ->
closed (length GH) (length G1) 0 T12 ->
has_type GH G1 (tobj (dfun T11 T12 t12)) (TFun T11 T12) (S n1)
| T_App : forall T1 T2 GH G1 t1 t2 n1 n2,
has_type GH G1 t1 (TFun T1 T2) n1 ->
has_type GH G1 t2 T1 n2 ->
has_type GH G1 (tapp t1 t2) T2 (S (n1+n2))
(* todo: add app-var and use dependent functions *)
| 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 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_bool: forall GH G1 n1,
stp2 GH G1 TBool TBool (S n1)
(* todo: enabled dependent functions *)
| stp2_fun: forall GH G1 T1 T2 T3 T4 n1 n2,
stp2 GH G1 T3 T1 n1 ->
stp2 GH G1 T2 T4 n2 ->
stp2 GH G1 (TFun T1 T2) (TFun T3 T4) (S (n1+n2))
| stp2_mem: forall GH G1 T1 T2 T3 T4 n1 n2,
stp2 GH G1 T3 T1 n2 ->
stp2 GH G1 T2 T4 n1 ->
stp2 GH G1 (TMem T1 T2) (TMem 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 T2 TX x n1,
index x G1 = Some (vobj (dty TX)) ->
stp2 [] G1 TX T2 n1 ->
stp2 GH G1 (TSel (TVar true x)) T2 (S n1)
| stp2_strong_sel2: forall GH G1 T1 TX x n1,
index x G1 = Some (vobj (dty TX)) ->
stp2 [] G1 T1 TX n1 ->
stp2 GH G1 T1 (TSel (TVar true x)) (S n1)
| stp2_sel1: forall GH G1 T2 x n1,
htp GH G1 x (TMem TBot T2) n1 ->
stp2 GH G1 (TSel (TVar false x)) T2 (S n1)
| stp2_sel2: forall GH G1 T1 x n1,
htp GH G1 x (TMem T1 TTop) n1 ->
stp2 GH G1 T1 (TSel (TVar false x)) (S n1)
| stp2_selx: forall GH G1 T1 n1,
closed (length GH) (length G1) 0 T1 ->
stp2 GH G1 (TSel T1) (TSel T1) (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 (length GH) (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_bool: forall m G1 x b n1,
index x G1 = Some (vbool b) ->
vtp m G1 x (TBool) (S (n1))
| vtp_mem: forall m G1 x TX T1 T2 n1 n2,
index x G1 = Some (vobj (dty TX)) ->
stp2 [] G1 T1 TX n1 ->
stp2 [] G1 TX T2 n2 ->
vtp m G1 x (TMem T1 T2) (S (n1+n2))
| vtp_fun: forall m G1 x T1 T2 T3 T4 t n1 n2 n3,
index x G1 = Some (vobj (dfun T1 T2 t)) ->
has_type [T1] G1 t T2 n3 ->
stp2 [] G1 T3 T1 n1 ->
stp2 [] G1 T2 T4 n2 ->
vtp m G1 x (TFun T3 T4) (S (n1+n2+n3))
| 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 TX n1,
index y G1 = Some (vobj (dty TX)) ->
vtp m G1 x TX n1 ->
vtp m G1 x (TSel (TVar true y)) (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_bool: forall GH G1,
stpd2 GH G1 TBool TBool.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd2_fun: forall GH G1 T1 T2 T3 T4,
stpd2 GH G1 T3 T1 ->
stpd2 GH G1 T2 T4 ->
stpd2 GH G1 (TFun T1 T2) (TFun T3 T4).
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd2_mem: forall GH G1 T1 T2 T3 T4,
stpd2 GH G1 T3 T1 ->
stpd2 GH G1 T2 T4 ->
stpd2 GH G1 (TMem T1 T2) (TMem 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 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).
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.
- econstructor. 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. eapply IHn. eauto. omega.
- econstructor. eapply index_extend. 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.
- econstructor. eapply index_extend. eauto. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- econstructor. eapply index_extend. eauto. eapply IHn. eauto. omega. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- econstructor. eapply IHn. eauto. omega. eapply closed_extend. eauto.
- econstructor. eapply index_extend. 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 IHn. eauto. omega.
- econstructor. eauto. eapply closed_extend. eauto.
- econstructor. eapply closed_extend. eauto.
- econstructor. eapply IHn. eauto. omega. eapply closed_extend. eauto. eapply closed_extend. eauto.
- econstructor. eapply IHn. eauto. omega. eapply IHn. eauto. omega.
- econstructor. eapply IHn. eauto. omega. eapply IHn. 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. 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. 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.
- 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).
Proof.
intros n. induction n. repeat split; intros; omega.
repeat split; intros; inversion H; destruct IHn as [IHS1 [IHS2 [IHV1 [IHV2 [IHH1 [IHH2 IHT]]]]]].
(* stp left *)
- econstructor.
- eauto.
- econstructor.
- econstructor. eapply IHS2. eauto. omega. eapply IHS1. eauto. omega.
- 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. eapply H2. 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.
- econstructor. eapply IHS1. eauto. omega. eapply IHS2. eauto. omega.
- econstructor. eapply IHS1. eauto. omega. eapply IHS2. eauto. omega.
- econstructor. simpl. eauto.
- econstructor. eauto.
- eapply closed_upgrade_gh. eapply IHS2. eapply H2. 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 index_max. eauto.
- eapply IHV1. eauto. omega.
- eapply IHV1. eauto. omega.
- eapply IHV1. eauto. omega.
(* vtp right *)
- econstructor.
- 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. eapply IHS2. eauto. omega.
- 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 *)
- eauto.
- 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 *)
- eapply closed_upgrade_gh. eapply IHV2. eauto. omega. omega.
- eauto.
- econstructor. eauto. eauto.
- econstructor. eauto. eauto.
- eapply IHT in H1. inversion H1. eauto. omega.
- eapply IHS2. 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 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 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. eauto. eauto. 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.
Lemma stpd2_refl: forall GH G1 T1,
closed (length GH) (length G1) 0 T1 ->
stpd2 GH G1 T1 T1.
Proof.
intros. induction T1; inversion H.
- Case "bot". exists 1. eauto.
- Case "top". exists 1. eauto.
- Case "bool". eapply stpd2_bool; eauto.
- Case "fun". eapply stpd2_fun; try eapply stpd2_wrapf; eauto.
- Case "mem". eapply stpd2_mem; try eapply stpd2_wrapf; eauto.
- Case "var0". exists 1. eauto.
- Case "var1".
assert (exists v, index i G1 = Some v) as E. eapply index_exists; eauto.
destruct E.
eexists. eapply stp2_varx; eauto.
- Case "varb". inversion H4.
- 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)) T1).
eapply closed_open. simpl. eapply closed_upgrade_gh. eauto. omega. econstructor. simpl. omega.
eauto. eauto. eauto. eauto.
- Case "and".
destruct IHT1_1. eauto. destruct IHT1_2. eauto.
eexists. eapply stp2_and2. eapply stp2_and11. eauto. eauto. eapply stp2_and12. eauto. eauto.
Grab Existential Variables.
apply 0. apply 0.
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: TBool = _ |- _ => 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 _ _ TBool (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.
Definition substt x T := (subst (TVar true x) T).
Hint Immediate substt.
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 (IHT2 (S j) TX); eauto; *)
try rewrite (IHT j k TX); eauto;
try rewrite (IHT1 j k TX); eauto;
try rewrite (IHT2 j k TX); eauto.
subst. inversion H4.
eapply closed_upgrade. eauto. 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.
intros. eapply subst_open_commute_m; eauto.
Qed.
Lemma subst_open_commute0: forall T0 n j TX,
closed 0 n (j+1) T0 ->
(subst TX (open j (TVar false 0) T0)) = open j TX T0.
Proof.
intros T0 n. induction T0; intros.
eauto. eauto. eauto.
simpl. inversion H. rewrite IHT0_1. rewrite IHT0_2. eauto. eauto. eauto.
simpl. inversion H. rewrite IHT0_1. rewrite IHT0_2. eauto. eauto. eauto.
simpl. inversion H. omega. eauto.
simpl. inversion H. subst. destruct i. case_eq (beq_nat j 0); intros E; simpl; eauto.
case_eq (beq_nat j (S i)); intros E; simpl; eauto.
simpl. inversion H. rewrite IHT0. eauto. eauto.
simpl. inversion H. rewrite IHT0. eauto. subst. eauto.
simpl. inversion H. rewrite IHT0_1. rewrite IHT0_2. eauto. eauto. eauto.
Qed.
Lemma subst_open_commute1: forall T0 x x0 j,
(open j (TVar true x0) (subst (TVar true x) T0))
= (subst (TVar true x) (open j (TVar true x0) T0)).
Proof.
induction T0; intros.
eauto. eauto. eauto.
simpl. rewrite IHT0_1. rewrite IHT0_2. eauto. eauto. eauto.
simpl. rewrite IHT0_1. rewrite IHT0_2. eauto. eauto. eauto.
simpl. destruct b. simpl. eauto.
case_eq (beq_nat i 0); intros E. simpl. eauto. simpl. eauto.
simpl. case_eq (beq_nat j i); intros E. simpl. eauto. simpl. eauto.
simpl. rewrite IHT0. eauto.
simpl. rewrite IHT0. eauto.
simpl. rewrite IHT0_1. rewrite IHT0_2. eauto.
Qed.
Lemma subst_closed_id: forall x j k T2,
closed 0 j k T2 ->
substt x T2 = T2.
Proof. intros. eapply closed_no_subst. eauto. Qed.
Lemma closed_subst0: forall i j k x T2,
closed (i + 1) j k T2 -> x < j ->
closed i j k (substt x T2).
Proof. intros. eapply closed_subst. eauto. econstructor. eauto. Qed.
Lemma closed_subst1: forall i j k x T2,
closed i j k T2 -> x < j -> i <> 0 ->
closed (i-1) j k (substt x T2).
Proof.
intros. eapply closed_subst.
assert ((i - 1 + 1) = i) as R. omega.
rewrite R. eauto. econstructor. 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: forall TX n x j,
(substt x (open j (TVar false (n+1)) TX)) =
(open j (TVar false n) (substt x TX)).
Proof.
intros TX. induction TX; intros; eauto.
- unfold substt. simpl. unfold substt in IHTX1. unfold substt in IHTX2. erewrite <-IHTX1. erewrite <-IHTX2. eauto.
- unfold substt. simpl. unfold substt in IHTX1. unfold substt in IHTX2. erewrite <-IHTX1. erewrite <-IHTX2. eauto.
- unfold substt. simpl. destruct b. eauto.
case_eq (beq_nat i 0); intros E. eauto. eauto.
- unfold substt. simpl.
case_eq (beq_nat j i); intros E. simpl.
assert (beq_nat (n + 1) 0 = false). eapply beq_nat_false_iff. omega.
assert ((n + 1 - 1 = n)). omega.
rewrite H. rewrite H0. eauto. eauto.
- unfold substt. simpl. unfold substt in IHTX. erewrite <-IHTX. eauto.
- unfold substt. simpl. unfold substt in IHTX. erewrite <-IHTX. eauto.
- unfold substt. simpl. unfold substt in IHTX1. unfold substt in IHTX2. erewrite <-IHTX1. erewrite <-IHTX2. eauto.
Qed.
Lemma subst_open3: forall TX0 (GH:tenv) TX x,
(substt x (open 0 (TVar false (length (GH ++ [TX]))) TX0)) =
(open 0 (TVar false (length GH)) (substt x TX0)).
Proof. intros. rewrite app_length. simpl. eapply subst_open. Qed.
Lemma subst_open4: forall T0 (GH:tenv) TX x,
substt x (open 0 (TVar false (length (GH ++ [TX]))) T0) =
open 0 (TVar false (length (map (substt x) GH))) (substt x T0).
Proof. intros. rewrite map_length. eapply subst_open3. Qed.
Lemma subst_open5: forall (GH:tenv) T0 x xi,
xi <> 0 -> substt x (open 0 (TVar false xi) T0) =
open 0 (TVar false (xi-1)) (substt x T0).
Proof.
intros. remember (xi-1) as n. assert (xi=n+1) as R. omega. rewrite R.
eapply subst_open.
Qed.
Lemma gh_match1: forall (GU:tenv) GH GL TX,
GH ++ [TX] = GU ++ GL ->
length GL > 0 ->
exists GL1, GL = GL1 ++ [TX] /\ GH = GU ++ GL1.
Proof.
intros GU. induction GU; intros.
- eexists. simpl in H. eauto.
- destruct GH. simpl in H.
assert (length [TX] = 1). eauto.
rewrite H in H1. simpl in H1. rewrite app_length in H1. omega.
destruct (IHGU GH GL TX).
simpl in H. inversion H. eauto. eauto.
eexists. split. eapply H1. simpl. destruct H1. simpl in H. inversion H. subst. eauto.
Qed.
Lemma gh_match: forall (GH:tenv) GU GL TX T0,
T0 :: GH ++ [TX] = GU ++ GL ->
length GL = S (length (GH ++ [TX])) ->
GU = [] /\ GL = T0 :: GH ++ [TX].
Proof.
intros. edestruct gh_match1. rewrite app_comm_cons in H. eapply H. omega.
assert (length (T0 :: GH ++ [TX]) = length (GU ++ GL)). rewrite H. eauto.
assert (GU = []). destruct GU. eauto. simpl in H.
simpl in H2. rewrite app_length in H2. simpl in H2. rewrite app_length in H2.
simpl in H2. rewrite H0 in H2. rewrite app_length in H2. simpl in H2.
omega.
split. eauto. rewrite H3 in H1. simpl in H1. subst. simpl in H1. eauto.
Qed.
Lemma sub_env1: forall (GL:tenv) GU GH TX,
GH ++ [TX] = GU ++ GL ->
length GL = 1 ->
GL = [TX].
Proof.
intros.
destruct GL. inversion H0. destruct GL.
eapply app_inj_tail in H. destruct H. subst. eauto.