-
Notifications
You must be signed in to change notification settings - Fork 11
/
sms-singletons1-wip.v
1987 lines (1721 loc) · 74.6 KB
/
sms-singletons1-wip.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
(* Clean-slate look at subtyping relation based on *)
(* singleton types (single env) *)
(* TODO:
+ get rid of all admits
+ Rewriting semantics
+ Soundness proof
Type checker / examples
------
+ expansion other than 0
- multiple members
- intersection types
- app var / proper TAll comparison
- remove unnecessary size variables
- stp_selx rule?
- closedness question: expansion cannot use id beyond self
*)
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
.
Inductive tm : Type :=
| tvar : bool -> id -> tm
| tbool : bool -> tm
| tmem : ty -> tm
| tapp : tm -> tm -> tm
| tabs : ty -> ty -> tm -> tm
.
Inductive vl : Type :=
| vbool : bool -> vl
| vabs : ty -> ty -> tm -> vl
| vty : ty -> 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.
(* closed i j k means normal variables < i and < j, bound variables < k *)
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)
.
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)
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)
end.
Inductive has_type : tenv -> venv -> tm -> ty -> nat -> Prop :=
| T_Varx : forall m b GH G1 x T n1,
vtp m b 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)
| T_Mem : forall GH G1 T11 n1,
closed (length GH) (length G1) 0 T11 ->
has_type GH G1 (tmem 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 (tabs 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))
| T_Sub : forall m GH G1 t T1 T2 n1 n2,
has_type GH G1 t T1 n1 ->
stp2 m 0 GH G1 T1 T2 n2 ->
has_type GH G1 t T2 (S (n1 + n2))
with stp2: nat -> nat -> tenv -> venv -> ty -> ty -> nat -> Prop :=
| stp2_bot: forall m b GH G1 T n1,
closed (length GH) (length G1) 0 T ->
stp2 m b GH G1 TBot T (S n1)
| stp2_top: forall m b GH G1 T n1,
closed (length GH) (length G1) 0 T ->
stp2 m b GH G1 T TTop (S n1)
| stp2_bool: forall m b GH G1 n1,
stp2 m b GH G1 TBool TBool (S n1)
| stp2_fun: forall m b GH G1 T1 T2 T3 T4 n1 n2,
stp2 m b GH G1 T3 T1 n1 ->
stp2 m b GH G1 T2 T4 n2 ->
stp2 m b GH G1 (TFun T1 T2) (TFun T3 T4) (S (n1+n2))
| stp2_mem: forall m b GH G1 T1 T2 T3 T4 n1 n2,
stp2 m b GH G1 T3 T1 n2 ->
stp2 m b GH G1 T2 T4 n1 ->
stp2 m b GH G1 (TMem T1 T2) (TMem T3 T4) (S (n1+n2))
| stp2_varx: forall m b GH G1 x n1,
x < length G1 ->
stp2 m b GH G1 (TVar true x) (TVar true x) (S n1)
| stp2_var1: forall m m2 b GH G1 x T2 n1,
vtp m2 b G1 x T2 n1 -> (* slack for: val_type G2 v T2 *)
stp2 m b GH G1 (TVar true x) T2 (S n1)
(* not sure if we should have these 2 or not ... ? *)
(*
| stp2_varb1: forall m GH G1 T2 x n1,
stp2 m true GH G1 (TVar true x) (TBind T2) n1 ->
stp2 m true GH G1 (TVar true x) (open 0 (TVar false x) T2) (S n1)
| stp2_varb2: forall m GH G1 T2 x n1,
stp2 m true GH G1 (TVar true x) (open 0 (TVar false x) T2) n1 ->
stp2 m true GH G1 (TVar true x) (TBind T2) (S n1)
*)
| stp2_varax: forall m b GH G1 x n1,
x < length GH ->
stp2 m b GH G1 (TVar false x) (TVar false x) (S n1)
(*
| stp2_varay: forall m GH G1 TX x1 x2 n1,
index x1 GH = Some TX ->
stp2 m false GH G1 TX (TVar false x2) n1 ->
stp2 m true GH G1 (TVar false x2) (TVar false x1) (S n1)
| stp2_vary: forall m GH G1 x1 x2 n1,
index x1 GH = Some (TVar true x2) ->
x2 < length G1 ->
stp2 m true GH G1 (TVar true x2) (TVar false x1) (S n1)
*)
| stp2_vara1: forall m b GH G1 T2 x n1,
htp m true GH G1 x T2 n1 -> (* TEMP -- SHOULD ALLOW UP TO x *)
stp2 m b GH G1 (TVar false x) T2 (S n1)
(*
| stp2_varab1: forall m GH G1 T2 x n1,
stp2 m true GH G1 (TVar false x) (TBind T2) n1 ->
stp2 m true GH G1 (TVar false x) (open 0 (TVar false x) T2) (S n1)
| stp2_varab2: forall m GH G1 T2 x n1,
stp2 m true GH G1 (TVar false x) (open 0 (TVar false x) T2) n1 ->
stp2 (S m) true GH G1 (TVar false x) (TBind T2) (S n1)
*)
(* NOTE: Currently we require *all* store references (TVar true x)
to expand (strong_sel), even in nested contexts (GH <> nil).
This is directly related to the htp GL restriction: when
substituting, we must derive the precise expansion, and cannot
tolerate potentially unsafe bindings in GH.
Is this an issue? If yes, we can keep sel1/sel2 and htp
for store references, and only invert once we know GH=[].
We need to be careful, though: it is not clear that we can
use stp2_trans on the embedded vtp. We may need to specially
mark self variables (again), since they are the only ones
that occur recursively. *)
| stp2_strong_sel1: forall m b GH G1 T2 TX x n1,
index x G1 = Some (vty TX) ->
stp2 m b [] G1 TX T2 n1 ->
stp2 m b GH G1 (TSel (TVar true x)) T2 (S n1)
| stp2_strong_sel2: forall m b GH G1 T1 TX x n1,
index x G1 = Some (vty TX) ->
stp2 m b [] G1 T1 TX n1 ->
stp2 m b GH G1 T1 (TSel (TVar true x)) (S n1)
(*
| stp2_xsel1: forall m GH G1 T2 TX x n1,
index x G1 = Some (vty TX) ->
stp2 m false [] G1 TX T2 n1 ->
stp2 m true GH G1 (TSel (TVar true x)) T2 (S n1)
| stp2_xsel2: forall m GH G1 T1 TX x n1,
index x G1 = Some (vty TX) ->
stp2 m false [] G1 T1 TX n1 ->
stp2 m true GH G1 T1 (TSel (TVar true x)) (S n1)
*)
| stp2_sel1: forall m b GH G1 T2 x n1,
htp m true GH G1 x (TMem TBot T2) n1 -> m >= b ->
stp2 m b GH G1 (TSel (TVar false x)) T2 (S n1)
| stp2_sel2: forall m b GH G1 T1 x n1,
htp m true GH G1 x (TMem T1 TTop) n1 -> m >= b ->
stp2 m b GH G1 T1 (TSel (TVar false x)) (S n1)
| stp2_bind1: forall m b GH G1 T1 T1' T2 n1,
htp m true (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 m b GH G1 (TBind T1) T2 (S n1)
| stp2_bindx: forall m b GH G1 T1 T1' T2 T2' n1,
stp2 m b (T1'::GH) G1 T1' T2' n1 ->
T1' = (open 0 (TVar false (length GH)) T1) ->
T2' = (open 0 (TVar false (length GH)) T2) ->
closed (length GH) (length G1) 1 T1 ->
closed (length GH) (length G1) 1 T2 ->
stp2 (S m) b GH G1 (TBind T1) (TBind T2) (S n1)
| stp2_bind2: forall m b GH G1 T1 T2' T2 n1,
htp m true (T1::GH) G1 (length GH) T2' n1 ->
T2' = (open 0 (TVar false (length GH)) T2) ->
closed (length GH) (length G1) 0 T1 ->
closed (length GH) (length G1) 1 T2 ->
stp2 m b GH G1 T1 (TBind T2) (S n1)
| stp2_wrapf: forall m b GH G1 T1 T2 n1,
stp2 m b GH G1 T1 T2 n1 ->
stp2 (S m) (S b) GH G1 T1 T2 (S n1)
| stp2_transf: forall m b GH G1 T1 T2 T3 n1 n2 m2 m3,
stp2 m b GH G1 T1 T2 n1 ->
stp2 m2 b GH G1 T2 T3 n2 -> m <= m3 -> m2 <= m3 ->
stp2 m3 b GH G1 T1 T3 (S (n1+n2))
with htp: nat -> bool -> tenv -> venv -> nat -> ty -> nat -> Prop :=
| htp_var: forall m b GH G1 x TX n1,
(* can we assign (TVar false x) ? probably not ... *)
index x GH = Some TX ->
closed (length GH) (length G1) 0 TX ->
htp m b GH G1 x TX (S n1)
| htp_bind: forall m b GH G1 x TX n1,
(* is it needed given stp2_bind1? for the moment, yes ...*)
htp m b GH G1 x (TBind TX) n1 ->
closed x (length G1) 1 TX ->
htp m b GH G1 x (open 0 (TVar false x) TX) (S n1)
| htp_sub: forall m b GH G1 x T1 n1,
htp m b GH G1 x T1 n1 ->
htp m b GH G1 x T1 (S n1)
(*| htp_sub: forall m b GH GU GL G1 x T1 T2 n1 n2,
htp m b GH G1 x T1 n1 ->
stp2 m b GL G1 T1 T2 n2 -> (* XXX change sig? *)
length GL = S x ->
GH = GU ++ GL -> (* NOTE: restriction to GL means we need trans in sel rules *)
htp m b GH G1 x T2 (S (n1+n2))*)
with vtp : nat -> nat -> venv -> nat -> ty -> nat -> Prop :=
| vtp_top: forall m b G1 x n1,
x < length G1 ->
vtp m b G1 x TTop (S n1)
| vtp_bool: forall m b G1 x v n1,
index x G1 = Some (vbool v) ->
vtp m b G1 x (TBool) (S (n1))
| vtp_mem: forall m b G1 x TX T1 T2 n1 n2,
index x G1 = Some (vty TX) ->
stp2 b 0 [] G1 T1 TX n1 ->
stp2 b 0 [] G1 TX T2 n2 ->
vtp m b G1 x (TMem T1 T2) (S (n1+n2))
| vtp_fun: forall m b G1 x T1 T2 T3 T4 t ms n1 n2 n3,
index x G1 = Some (vabs T1 T2 t) ->
has_type [T1] G1 t T2 n3 ->
stp2 ms 0 [] G1 T3 T1 n1 ->
stp2 ms 0 [] G1 T2 T4 n2 ->
vtp m b G1 x (TFun T3 T4) (S (n1+n2+n3))
| vtp_bind: forall m b G1 x T2 n1,
vtp m b G1 x (open 0 (TVar true x) T2) n1 ->
closed 0 (length G1) 1 T2 ->
vtp (S m) b G1 x (TBind T2) (S (n1))
| vtp_sel: forall m b G1 x y TX n1,
index y G1 = Some (vty TX) ->
vtp m b G1 x TX n1 ->
vtp m b G1 x (TSel (TVar true y)) (S (n1))
(* with vtp2 : venv -> nat -> ty -> nat -> Prop := *)
(*
| vtp2_refl: forall G1 x n1,
x < length G1 ->
vtp 0 false G1 x (TVar true x) (S n1)
*)
(*| vtp2_down: forall m G1 x T n1,
vtp m G1 x T n1 ->
vtp2 0 false G1 x T (S n1)
| vtp2_sel: forall G1 x y TX n1,
index y G1 = Some (vty TX) ->
vtp2 G1 x TX n1 ->
vtp2 G1 x (TSel (TVar true y)) (S (n1)). *)
.
Definition has_typed GH G1 x T1 := exists n, has_type GH G1 x T1 n.
Definition stpd2 m b GH G1 T1 T2 := exists n, stp2 m b GH G1 T1 T2 n.
Definition htpd m b GH G1 x T1 := exists n, htp m b GH G1 x T1 n.
Definition vtpd m b G1 x T1 := exists n, vtp m b G1 x T1 n.
Definition vtpd2 G1 x T1 := exists m n, vtp m 0 G1 x T1 n.
Definition vtpdd m b G1 x T1 := exists m1 n, vtp m1 b G1 x T1 n /\ (m1 <= m).
Definition vtpddd b G1 x T1 := exists m1 n, vtp m1 b G1 x T1 n.
Hint Constructors stp2.
Hint Constructors vtp.
Ltac ep := match goal with
| [ |- stp2 ?M ?B ?GH ?G1 ?T1 ?T2 ?N ] => assert (exists (n:nat), stp2 M B 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: vtpd2 _ _ _ |- _ => destruct H as [? H]
| H: vtpdd _ _ _ _ _ |- _ => destruct H as [? [? [H ?]]]
| H: vtpddd _ _ _ _ |- _ => destruct H as [? [? H]]
end.
Lemma stpd2_bot: forall m b GH G1 T,
closed (length GH) (length G1) 0 T ->
stpd2 m b GH G1 TBot T.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd2_top: forall m b GH G1 T,
closed (length GH) (length G1) 0 T ->
stpd2 m b GH G1 T TTop.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd2_bool: forall m b GH G1,
stpd2 m b GH G1 TBool TBool.
Proof. intros. exists 1. eauto. Qed.
Lemma stpd2_fun: forall m b GH G1 T1 T2 T3 T4,
stpd2 m b GH G1 T3 T1 ->
stpd2 m b GH G1 T2 T4 ->
stpd2 m b GH G1 (TFun T1 T2) (TFun T3 T4).
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd2_mem: forall m b GH G1 T1 T2 T3 T4,
stpd2 m b GH G1 T3 T1 ->
stpd2 m b GH G1 T2 T4 ->
stpd2 m b GH G1 (TMem T1 T2) (TMem T3 T4).
Proof. intros. repeat eu. eexists. eauto. Qed.
(*
Lemma stpd2_varx: forall m GH G1 x,
x < length G1 ->
stpd2 m true GH G1 (TVar true x) (TVar true x).
Proof. intros. repeat eu. exists 1. eauto. Qed.
Lemma stpd2_var1: forall m GH G1 TX x T2 v,
index x G1 = Some v ->
val_type0 G1 v TX -> (* slack for: val_type G2 v T2 *)
stpd2 m true GH G1 TX T2 ->
stpd2 m true GH G1 (TVar true x) T2.
Proof. intros. repeat eu. eexists. eauto. Qed.
(*Lemma stpd2_var1b: forall m GH G1 GX TX G2 x x2 T2 v,
index x G1 = Some v ->
index x2 G2 = Some v ->
val_type0 GX v TX -> (* slack for: val_type G2 v T2 *)
stpd2 m true GH GX TX G2 (open 0 (TVar true x2) T2) ->
stpd2 m true GH G1 (TVar true x) G2 (TBind T2).
Proof. intros. repeat eu. eexists. eauto. Qed.*)
Lemma stpd2_sel: forall m GH G1 T1 T2,
stpd2 m false GH G1 T2 T1 ->
stpd2 m true GH G1 T1 T2 ->
stpd2 m true GH G1 (TSel T1) (TSel T2).
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd2_red: forall m GH G1 T1 T2,
stpd2 m true GH G1 T1 (TMem TBot T2) ->
stpd2 m true GH G1 (TSel T1) T2.
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd2_red2: forall m GH G1 TX T1 T2 T0,
real T0 ->
stpd2 m true GH G1 T0 T2 ->
stpd2 m false GH G1 T2 (TMem TX TTop) ->
stpd2 m true GH G1 T0 (TMem TX TTop) ->
stpd2 m false GH G1 T1 TX ->
stpd2 m true GH G1 T1 (TSel T2).
Proof. intros. repeat eu. eexists. eauto. Qed.
(*
Lemma stpd2_bindI: forall G1 G2 T2 x,
stpd2 true G1 (TVar x) G2 (open 0 (TVar x) T2) ->
stpd2 true G1 (TVar x) G2 (TBind T2).
Proof. intros. repeat eu. eexists. eauto. Qed. *
Lemma stpd2_bindE: forall G1 G2 T2 x,
stpd2 true G1 (TVar x) G2 (TBind T2) ->
stpd2 true G1 (TVar x) G2 (open 0 (TVar x) T2).
Proof. intros. repeat eu. eexists. eauto. Qed.
*)
(*Lemma stpd2_bind1: forall m GH G1 T1 T2,
closed (length GH) (length G2) 0 T2 ->
stpd2 m true ((G1,(open 0 (TVar false (length GH)) T1))::GH)
G1 (TVar false (length GH)) G2 T2 ->
stpd2 m true GH G1 (TBind T1) G2 T2.
Proof. intros. repeat eu. eexists. eauto. Qed.
*)
*)
Lemma stpd2_wrapf: forall m b GH G1 T1 T2,
stpd2 m b GH G1 T1 T2 ->
stpd2 (S m) (S b) GH G1 T1 T2.
Proof. intros. repeat eu. eexists. eauto. Qed.
Lemma stpd2_transf: forall m b GH G1 T1 T2 T3,
stpd2 m b GH G1 T1 T2 ->
stpd2 m b GH G1 T2 T3 ->
stpd2 m b 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 m b GH v1 G1 T1 T2 n,
stp2 m b GH G1 T1 T2 n -> n < ni ->
stp2 m b GH (v1::G1) T1 T2 n) /\
(forall m b v1 x G1 T2 n,
vtp m b G1 x T2 n -> n < ni ->
vtp m b (v1::G1) x T2 n) /\
(forall m b v1 x GH G1 T2 n,
htp m b GH G1 x T2 n -> n < ni ->
htp m b 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. eapply IHn. eauto. omega.
- econstructor. eauto.
- econstructor. eapply IHn. eauto. omega.
- econstructor. eapply index_extend. eauto. eapply IHn. eauto. omega.
- econstructor. eapply index_extend. eauto. eapply IHn. eauto. omega.
- econstructor. eapply IHn. eauto. omega. eauto.
- econstructor. eapply IHn. eauto. omega. 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.
- admit.
- econstructor. eapply IHn. eauto. omega.
- eapply stp2_transf. eapply IHn. eauto. omega. eapply IHn. eauto. omega. eauto. eauto.
(* 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.
(* 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 m b GH G1 T1 T2 n,
stp2 m b GH G1 T1 T2 n -> n < ni ->
closed (length GH) (length G1) 0 T1) /\
(forall m b GH G1 T1 T2 n,
stp2 m b GH G1 T1 T2 n -> n < ni ->
closed (length GH) (length G1) 0 T2) /\
(forall m b x G1 T2 n,
vtp m b G1 x T2 n -> n < ni ->
x < length G1) /\
(forall m b x G1 T2 n,
vtp m b G1 x T2 n -> n < ni ->
closed 0 (length G1) 0 T2) /\
(forall m b x GH G1 T2 n,
htp m b GH G1 x T2 n -> n < ni ->
x < length GH) /\
(forall m b x GH G1 T2 n,
htp m b 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. eapply IHV1. eauto. omega.
- econstructor. eauto.
- econstructor. eapply IHH1. eauto. omega.
- 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.
- admit.
- 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.
- eapply closed_upgrade_gh. eapply IHV2. eauto. omega. omega.
- econstructor. eauto.
- eapply IHH2. eauto. omega.
- 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.
- eauto.
- econstructor. eauto.
- admit.
- 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.
(* 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.
(* 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.
- admit. (* 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 b v1 x G1 T2 n,
vtp m b G1 x T2 n ->
vtp m b (v1::G1) x T2 n.
Proof. intros. eapply all_extend. eauto. eauto. Qed.
Lemma htp_extend : forall m b v1 x GH G1 T2 n,
htp m b GH G1 x T2 n ->
htp m b GH (v1::G1) x T2 n.
Proof. intros. eapply all_extend. eauto. eauto. Qed.
Lemma stp2_extend : forall m b GH v1 G1 T1 T2 n,
stp2 m b GH G1 T1 T2 n ->
stp2 m b GH (v1::G1) T1 T2 n.
Proof. intros. eapply all_extend. eauto. eauto. Qed.
Lemma stp2_extend_mult : forall m b GH G1 G' T1 T2 n,
stp2 m b GH G1 T1 T2 n ->
stp2 m b 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 b G1 x T2 n1,
vtp m b G1 x T2 n1 ->
closed 0 (length G1) 0 T2.
Proof. intros. eapply all_closed. eauto. eauto. Qed.
Lemma vtp_closed1: forall m b G1 x T2 n1,
vtp m b 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 m b GH G1 T1 T2 n1,
stp2 m b 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 m b GH G1 T1 T2 n1,
stp2 m b 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 m b GH G1 T1 T2,
stpd2 m b GH G1 T1 T2 ->
closed (length GH) (length G1) 0 T1.
Proof. intros. eu. eapply stp2_closed1. eauto. Qed.
Lemma stpd2_closed2 : forall m b GH G1 T1 T2,
stpd2 m b 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 m GH G1 T1,
closed (length GH) (length G1) 0 T1 ->
stpd2 m 0 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". admit. (* sel-sel ? *)
- Case "bind".
admit. (*
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. *)
Grab Existential Variables.
apply 0. (*apply 0.*)
Qed.
Lemma stpd2_reg1 : forall m b GH G1 T1 T2,
stpd2 m b GH G1 T1 T2 ->
stpd2 m 0 GH G1 T1 T1.
Proof. intros. eapply stpd2_refl. eapply stpd2_closed1. eauto. Qed.
Lemma stpd2_reg2 : forall m b GH G1 T1 T2,
stpd2 m b GH G1 T1 T2 ->
stpd2 m 0 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
| _ => 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
| _ => 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.
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.
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.