-
Notifications
You must be signed in to change notification settings - Fork 11
/
dot-stp-single2.v
1649 lines (1375 loc) · 47.8 KB
/
dot-stp-single2.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
Require Export SfLib.
Require Export Arith.EqNat.
Require Export Arith.Le.
(*
subtyping:
- looking at single-environment case again.
- new pushback proof structure: transitivity axiom only
needed in contravariant positions
- realizable type: precise expansion, and upper bounds
are also realizable
based on dot-stp-single1, but has two subtyping relations.
one supports narrowing (stp), the other supports transitivity (stp2)
TODO/QUESTIONs:
- bind2/bind1 rules?
- intersection/union types?
- restrictions: recursive members?
- if lower bound is bottom, must upper bound still be realizable?
--> cannot be part of nontrivial T1 < x.A < T2 chain
*)
(* ############################################################ *)
(* Syntax *)
(* ############################################################ *)
Module DOT.
Definition id := nat.
Inductive ty : Type :=
| TNoF : ty (* marker for empty method list *)
| TBot : ty
| TTop : ty
| TBool : ty
| TAnd : ty -> ty -> ty
| TFun : id -> ty -> ty -> ty
| TMem : ty -> ty -> ty
| TSel : id -> ty
| TSelB : id -> ty
| TBind : ty -> ty
.
Inductive tm : Type :=
| ttrue : tm
| tfalse : tm
| tvar : id -> tm
| tapp : id -> id -> tm -> tm (* a.f(x) *)
| tabs : id -> ty -> list (id * dc) -> tm -> tm (* let f:T = x => y in z *)
| tlet : id -> ty -> tm -> tm -> tm (* let x:T = y *)
with dc: Type :=
| dfun : ty -> ty -> id -> tm -> dc (* def m:T = x => y *)
.
Fixpoint dc_type_and (dcs: list(nat*dc)) :=
match dcs with
| nil => TNoF
| (n, dfun T1 T2 _ _)::dcs =>
TAnd (TFun (length dcs) T1 T2) (dc_type_and dcs)
end.
Definition TObj p dcs := TAnd (TMem p p) (dc_type_and dcs).
Definition TArrow p x y := TAnd (TMem p p) (TAnd (TFun 0 x y) TNoF).
Inductive vl : Type :=
| vbool : bool -> vl
| vabs : list (id*vl) -> id -> ty -> list (id * dc) -> vl (* clos env f:T = x => y *)
| vmock : list (id*vl) -> ty -> id -> id -> vl
.
Definition env := list (nat*vl).
Definition tenv := list (nat*ty).
Fixpoint index {X : Type} (n : nat)
(l : list (nat * X)) : option X :=
match l with
| [] => None
(* for now, ignore binding value n' !!! *)
| (n',a) :: l' => if beq_nat n (length l') then Some a else index n l'
end.
Fixpoint update {X : Type} (n : nat) (x: X)
(l : list (nat * X)) { struct l }: list (nat * X) :=
match l with
| [] => []
(* for now, ignore binding value n' !!! *)
| (n',a) :: l' => if beq_nat n (length l') then (n',x)::l' else (n',a) :: update n x l'
end.
(* LOCALLY NAMELESS *)
Inductive closed_rec: nat -> ty -> Prop :=
| cl_nof: forall k,
closed_rec k TNoF
| cl_top: forall k,
closed_rec k TTop
| cl_bot: forall k,
closed_rec k TBot
| cl_bool: forall k,
closed_rec k TBool
| cl_fun: forall k m T1 T2,
closed_rec k T1 ->
closed_rec k T2 ->
closed_rec k (TFun m T1 T2)
| cl_mem: forall k T1 T2,
closed_rec k T1 ->
closed_rec k T2 ->
closed_rec k (TMem T1 T2)
| cl_and: forall k T1 T2,
closed_rec k T1 ->
closed_rec k T2 ->
closed_rec k (TAnd T1 T2)
| cl_bind: forall k T1,
closed_rec (S k) T1 ->
closed_rec k (TBind T1)
| cl_sel: forall k x,
closed_rec k (TSel x)
| cl_selb: forall k i,
k > i ->
closed_rec k (TSelB i)
.
Hint Constructors closed_rec.
Definition closed j T := closed_rec j T.
Fixpoint open_rec (k: nat) (u: id) (T: ty) { struct T }: ty :=
match T with
| TSel x => TSel x (* free var remains free. functional, so we can't check for conflict *)
| TSelB i => if beq_nat k i then TSel u else TSelB i
| TBind T1 => TBind (open_rec (S k) u T1)
| TNoF => TNoF
| TBot => TBot
| TTop => TTop
| TBool => TBool
| TAnd T1 T2 => TAnd (open_rec k u T1) (open_rec k u T2)
| TMem T1 T2 => TMem (open_rec k u T1) (open_rec k u T2)
| TFun m T1 T2 => TFun m (open_rec k u T1) (open_rec k u T2)
end.
Definition open u T := open_rec 0 u T.
(* sanity check *)
Example open_ex1: open 9 (TBind (TAnd (TMem TBot TTop) (TFun 0 (TSelB 1) (TSelB 0)))) =
(TBind (TAnd (TMem TBot TTop) (TFun 0 (TSel 9) (TSelB 0)))).
Proof. compute. eauto. Qed.
Lemma closed_no_open: forall T x j,
closed_rec j T ->
T = open_rec j x T.
Proof.
intros. induction H; intros; eauto;
try solve [compute; compute in IHclosed_rec; rewrite <-IHclosed_rec; auto];
try solve [compute; compute in IHclosed_rec1; compute in IHclosed_rec2; rewrite <-IHclosed_rec1; rewrite <-IHclosed_rec2; auto].
Case "TSelB".
unfold open_rec. assert (k <> i). omega.
apply beq_nat_false_iff in H0.
rewrite H0. auto.
Qed.
Lemma closed_upgrade: forall i j T,
closed_rec i T ->
j >= i ->
closed_rec j T.
Proof.
intros. generalize dependent j. induction H; intros; eauto.
Case "TBind". econstructor. eapply IHclosed_rec. omega.
Case "TSelB". econstructor. omega.
Qed.
Hint Unfold open.
Hint Unfold closed.
(* ############################################################ *)
(* Static properties: type assignment, subtyping, ... *)
(* ############################################################ *)
(* TODO: wf is not up to date *)
(* static type expansion.
needs to imply dynamic subtyping / value typing. *)
Inductive tresolve: id -> ty -> ty -> Prop :=
| tr_self: forall x T,
tresolve x T T
| tr_and1: forall x T1 T2 T,
tresolve x T1 T ->
tresolve x (TAnd T1 T2) T
| tr_and2: forall x T1 T2 T,
tresolve x T2 T ->
tresolve x (TAnd T1 T2) T
| tr_unpack: forall x T2 T3 T,
open x T2 = T3 ->
tresolve x T3 T ->
tresolve x (TBind T2) T
.
Tactic Notation "tresolve_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "Self" |
Case_aux c "And1" |
Case_aux c "And2" |
Case_aux c "Bind" ].
(* static type well-formedness.
needs to imply dynamic subtyping. *)
Inductive wf_type : tenv -> ty -> Prop :=
| wf_top: forall env,
wf_type env TNoF
| wf_bool: forall env,
wf_type env TBool
| wf_and: forall env T1 T2,
wf_type env T1 ->
wf_type env T2 ->
wf_type env (TAnd T1 T2)
| wf_mem: forall env TL TU,
wf_type env TL ->
wf_type env TU ->
wf_type env (TMem TL TU)
| wf_fun: forall env f T1 T2,
wf_type env T1 ->
wf_type env T2 ->
wf_type env (TFun f T1 T2)
| wf_sel: forall envz x TE TL TU,
index x envz = Some (TE) ->
tresolve x TE (TMem TL TU) ->
wf_type envz (TSel x)
| wf_selb: forall envz x, (* note: disregarding bind-scope *)
wf_type envz (TSelB x)
| wf_bind: forall envz T,
wf_type envz T ->
wf_type envz (TBind T)
.
Tactic Notation "wf_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "Top" |
Case_aux c "Bool" |
Case_aux c "And" |
Case_aux c "MemA" |
Case_aux c "Mem" |
Case_aux c "Fun" |
Case_aux c "Sel" |
Case_aux x "SelB" |
Case_aux c "Bind" ].
(* this is the version we can narrow *)
Inductive stp : bool -> tenv -> ty -> ty -> nat -> Prop :=
| stp_bot: forall G1 T n1,
stp true G1 TBot T n1
| stp_top: forall G1 T n1,
stp true G1 T TTop n1
| stp_bool: forall G1 n1,
stp true G1 TBool TBool n1
| stp_fun: forall m G1 T11 T12 T21 T22 n1 n2,
stp false G1 T21 T11 n1 ->
stp true G1 T12 T22 n2 ->
stp true G1 (TFun m T11 T12) (TFun m T21 T22) (S (n1+n2))
| stp_mem: forall G1 T11 T12 T21 T22 n1 n2 n3,
stp false G1 T21 T11 n1 ->
stp true G1 T11 T12 n2 -> (* NOT SO EASY TO ADD: build_mem! *)
stp true G1 T12 T22 n3 ->
stp true G1 (TMem T11 T12) (TMem T21 T22) (S (n1+n2+n3))
| stp_sel2: forall x T1 TX G1 n1,
index x G1 = Some TX ->
stp false G1 TX (TMem T1 TTop) n1 ->
stp true G1 T1 (TSel x) (S n1)
| stp_sel1: forall x T2 TX G1 n1,
index x G1 = Some TX ->
stp false G1 TX (TMem TBot T2) n1 ->
stp true G1 (TSel x) T2 (S n1)
| stp_selx: forall x G1 n1,
stp true G1 (TSel x) (TSel x) (S n1)
(* TODO!
| stp_bind2: forall f G1 T1 T2 TA2 n1,
stp true ((f,T1)::G1) T1 T2 n1 ->
open (length G1) TA2 = T2 ->
stp true G1 T1 (TBind TA2) (S n1)
| stp_bind1: forall f G1 T1 T2 TA1 n1,
stp true ((f,T1)::G1) T1 T2 n1 ->
open (length G1) TA1 = T1 ->
stp true G1 (TBind TA1) T2 (S n1)
... or at least...
*)
| stp_bindx: forall G1 T1 T2 TA1 TA2 n1,
stp false ((length G1,T1)::G1) T1 T2 n1 ->
open (length G1) TA1 = T1 ->
open (length G1) TA2 = T2 ->
stp true G1 (TBind TA1) (TBind TA2) (S n1)
| stp_transf: forall G1 T1 T2 T3 n1 n2,
stp true G1 T1 T2 n1 ->
stp false G1 T2 T3 n2 ->
stp false G1 T1 T3 (S (n1+n2))
| stp_wrapf: forall G1 T1 T2 n1,
stp true G1 T1 T2 n1 ->
stp false G1 T1 T2 (S n1)
.
(* implementable types *)
Inductive itp: tenv -> ty -> nat -> Prop :=
| itp_top: forall G1 n1,
itp G1 TTop n1
| itp_bool: forall G1 n1,
itp G1 TBool n1
(* TODO: we should have another mem case,
if lower bound is bot, upper bound need
not be realizable (?) *)
| itp_mem: forall G1 TL TU n1 n2,
(* stp true G1 TL TU n1 -> (* may or may not be needed *) *)
itp G1 TU n2 ->
itp G1 (TMem TL TU) (S (n1+n2))
| itp_sel: forall G1 TX x n1,
index x G1 = Some TX ->
itp G1 TX n1 -> (* could / should we rely on env_itp to provide this? *)
itp G1 (TSel x) (S n1)
.
(* this is the version we can trans *)
Inductive stp2 : bool -> tenv -> ty -> ty -> nat -> Prop :=
| stp2_bot: forall G1 T n1,
stp2 true G1 TBot T n1
| stp2_top: forall G1 T n1,
stp2 true G1 T TTop n1
| stp2_bool: forall G1 n1,
stp2 true G1 TBool TBool n1
| stp2_fun: forall m G1 T11 T12 T21 T22 n1 n2,
stp2 false G1 T21 T11 n1 ->
stp2 true G1 T12 T22 n2 ->
stp2 true G1 (TFun m T11 T12) (TFun m T21 T22) (S (n1+n2))
| stp2_mem: forall G1 T11 T12 T21 T22 n1 n2 n3,
stp2 false G1 T21 T11 n1 ->
stp2 true G1 T11 T12 n2 -> (* NOT SO EASY TO ADD: build_mem! *)
stp2 true G1 T12 T22 n3 ->
stp2 true G1 (TMem T11 T12) (TMem T21 T22) (S (n1+n2+n3))
| stp2_sel2: forall x T1 TX G1 n1 n2,
index x G1 = Some TX ->
itp G1 TX n2 ->
stp2 true G1 TX (TMem T1 TTop) n1 ->
stp2 true G1 T1 (TSel x) (S (n1+n2))
| stp2_sel1: forall x T2 TX G1 n1 n2,
index x G1 = Some TX ->
itp G1 TX n2 ->
stp2 true G1 TX (TMem TBot T2) n1 ->
stp2 true G1 (TSel x) T2 (S (n1+n2))
| stp2_selx: forall x G1 n1,
stp2 true G1 (TSel x) (TSel x) (S n1)
(* TODO!
| stp_bind2: forall f G1 T1 T2 TA2 n1,
stp true ((f,T1)::G1) T1 T2 n1 ->
open (length G1) TA2 = T2 ->
stp true G1 T1 (TBind TA2) (S n1)
| stp_bind1: forall f G1 T1 T2 TA1 n1,
stp true ((f,T1)::G1) T1 T2 n1 ->
open (length G1) TA1 = T1 ->
stp true G1 (TBind TA1) T2 (S n1)
... or at least...
*)
| stp2_bindx: forall G1 T1 T2 TA1 TA2 n1,
stp false ((length G1,T1)::G1) T1 T2 n1 -> (* TAKING THE OTHER ONE *)
open (length G1) TA1 = T1 ->
open (length G1) TA2 = T2 ->
(* no itp here, needs to come from env_itp if extracting things *)
(* itp ((length G1,T1)::G1) T1 n2 -> *)
stp2 true G1 (TBind TA1) (TBind TA2) (S n1)
| stp2_transf: forall G1 T1 T2 T3 n1 n2,
stp2 true G1 T1 T2 n1 ->
stp2 false G1 T2 T3 n2 ->
stp2 false G1 T1 T3 (S (n1+n2))
| stp2_wrapf: forall G1 T1 T2 n1,
stp2 true G1 T1 T2 n1 ->
stp2 false G1 T1 T2 (S n1)
.
Definition itp2 a b c := itp a b c.
(*
XXXX ---- XXXX
intersection case for narrowing:
(1) bad bounds
x:Bot..Top, y:Hi..Hi
x.A /\ y.B,
narrow x to Lo..Lo
now x.A /\ y.B has bad bounds Hi..Lo
but this object can't exist, it cannot be in context, so all is good !!!
however, the original type can be in context, and this is the trouble case.
(because we need to show it remains implementable in narrowing).
similarly, the original type can be the body of a self type.
x={A:Bot..{B:Bot..Top}}
{z => x.A & {B:Hi..Hi }}
narrow x to {A:Bot..{B:Lo..Lo}}
the context is still implementable,
but z's body is no longer implementable.
but how can the narrowing happen? x must be bound by a self type...
maybe this will constrain things?
(2) expansion can be lost already, without looking at bounds
*)
(*
what about intersection types:
STP
T <: T1, T <: T2
-----------------
T <: T1 /\ T2
T1 <: T, T2 wfe
----------------
T1 /\ T2 <: T
T1 wfe, T2 <: T
----------------
T1 /\ T2 <: T
EXP
T1 <x {A: L1..U1}, T2 <x {A: L2..U2}
------------------------------------
T1 /\ T2 <x {A: L1 \/ L2 .. U1 /\ U2 }
T1 <x {A: L1..U1}, A not in dom(T2)
------------------------------------
T1 /\ T2 <x {A: L1..U1}
A not in dom(T1), T2 <x {A: L2..U2}
------------------------------------
T1 /\ T2 <x {A: L2..U2}
Problem case (bad bounds):
(Int..Int) /\ (String..String)
Expansion:
(Int \/ String) .. (Int /\ String) <--- not <:
CONSTRAINTS:
- need good bounds (L < U) for stpd_trans_cross
- not part of regular stp. needs to come from env
- but need to be able to do induction on it
HYPOTHESIS:
- put itp in stp_sel1,sel2 evidence: this mirrors
env_itp, but enables induction
- narrow uses env_itp to get new itp (output size
doesn't matter)
Need itp_exp (example):
itp (L1..U1) /\ (L2..U2)
(L1..U1) /\ (L2..U2) <: L..U [n+1]
--->
(L1..U1) /\ (L2..U2) <x (L1 \/ L2 .. U1 /\ U2)
(L1 \/ L2 <: U1 /\ U2) [n]
(L1 \/ L2 .. U1 /\ U2) <: L..U
itp U1 /\ U2
*)
Tactic Notation "stp_cases" tactic(first) ident(c) :=
first;
[
Case_aux c "Bot < ?" |
Case_aux c "? < Top" |
Case_aux c "Bool < Bool" |
Case_aux c "Fun < Fun" |
Case_aux c "Mem < Mem" |
Case_aux c "? < Sel" |
Case_aux c "Sel < ?" |
Case_aux c "Sel < Sel" |
Case_aux c "Bind < Bind" |
Case_aux c "Trans" |
Case_aux c "Wrap"
].
Hint Resolve ex_intro.
Hint Constructors stp.
Hint Constructors itp.
Hint Constructors stp2.
(* ############################################################ *)
(* Examples *)
(* ############################################################ *)
(*
THIS IS NOW FALSE: lhs must expand.
Example ex1: exists n, stp true nil (TBind TBot) (TBind TTop) n.
Proof.
eexists. eapply stp_bindx. eapply stp_bot. eauto. (* false - bot doesn't exp! *).
Grab Existential Variables. apply 0.
Qed.
*)
Example ex2: exists n, stp true nil
(TBind (TMem TBool TBool))
(TBind (TMem TBot TTop)) n.
Proof.
eexists. eapply stp_bindx. eapply stp_wrapf. eapply stp_mem. eapply stp_wrapf. eapply stp_bot.
eapply stp_bool.
eapply stp_top. compute. eauto. compute. eauto.
Grab Existential Variables. apply 0. apply 0. apply 0.
Qed.
Example ex3: exists n, stp true nil
(TBind (TMem TBool TBool))
(TBind (TMem (TSelB 0) (TSelB 0))) n. (* can't do much with this *)
Proof.
eexists. eapply stp_bindx.
instantiate (3 := (TMem TBool TBool)).
instantiate (2 := (TMem (TSel 0) (TSel 0))).
eapply stp_wrapf.
eapply stp_mem. eapply stp_wrapf.
eapply stp_sel1. compute. eauto. eapply stp_wrapf. eapply stp_mem. eauto. eauto. eauto. eauto.
eapply stp_sel2. compute. eauto. eauto.
eauto. eauto.
Grab Existential Variables. apply 0. apply 0. apply 0. apply 0. apply 0. apply 0. apply 0.
Qed.
(* ############################################################ *)
(* Proofs *)
(* ############################################################ *)
Definition stpd b G1 T1 T2 := exists n, stp b G1 T1 T2 n.
Definition itpd G1 T1 := exists n, itp G1 T1 n.
Definition stpd2 b G1 T1 T2 := exists n, stp2 b G1 T1 T2 n.
Definition itpd2 G1 T1 := exists n, itp2 G1 T1 n.
Hint Unfold stpd.
Hint Unfold itpd.
Hint Unfold stpd2.
Hint Unfold itpd2.
Ltac ep := match goal with
| [ |- stp ?M ?G1 ?T1 ?T2 ?N ] => assert (exists (x:nat), stp M G1 T1 T2 x) as EEX
end.
Ltac eu := match goal with
| H: stpd _ _ _ _ |- _ => destruct H
| H: itpd _ _ |- _ => destruct H
| H: stpd2 _ _ _ _ |- _ => destruct H
| H: itpd2 _ _ |- _ => destruct H
(* | H: exists n: nat , _ |- _ =>
destruct H as [e P] *)
end.
Lemma stpd_bot: forall G1 T,
stpd true G1 TBot T.
Proof. intros. exists 0. eauto. Qed.
Lemma stpd_top: forall G1 T,
stpd true G1 T TTop.
Proof. intros. exists 0. eauto. Qed.
Lemma stpd_bool: forall G1,
stpd true G1 TBool TBool.
Proof. intros. exists 0. eauto. Qed.
Lemma stpd_fun: forall m G1 T11 T12 T21 T22,
stpd false G1 T21 T11 ->
stpd true G1 T12 T22 ->
stpd true G1 (TFun m T11 T12) (TFun m T21 T22).
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd_mem: forall G1 T11 T12 T21 T22,
stpd false G1 T21 T11 ->
stpd true G1 T11 T12 ->
stpd true G1 T12 T22 ->
stpd true G1 (TMem T11 T12) (TMem T21 T22).
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd_sel2: forall x T1 TX G1,
index x G1 = Some TX ->
stpd false G1 TX (TMem T1 TTop) ->
stpd true G1 T1 (TSel x).
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd_sel1: forall x T2 TX G1,
index x G1 = Some TX ->
stpd false G1 TX (TMem TBot T2) ->
stpd true G1 (TSel x) T2.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd_selx: forall x G1,
stpd true G1 (TSel x) (TSel x).
Proof. intros. repeat eu. exists 1. eauto. Qed.
Lemma stpd_bindx: forall G1 T1 T2 TA1 TA2,
stpd false ((length G1,T1)::G1) T1 T2 ->
open (length G1) TA1 = T1 ->
open (length G1) TA2 = T2 ->
(* exp G1 T1 (TMem TL TU) -> *)
(* stpd true G1 TL TU -> *)
stpd true G1 (TBind TA1) (TBind TA2).
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd_transf: forall G1 T1 T2 T3,
stpd true G1 T1 T2 ->
stpd false G1 T2 T3 ->
stpd false G1 T1 T3.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd_wrapf: forall G1 T1 T2,
stpd true G1 T1 T2 ->
stpd false G1 T1 T2.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_bot: forall G1 T,
stpd2 true G1 TBot T.
Proof. intros. exists 0. eauto. Qed.
Lemma stpd2_top: forall G1 T,
stpd2 true G1 T TTop.
Proof. intros. exists 0. eauto. Qed.
Lemma stpd2_bool: forall G1,
stpd2 true G1 TBool TBool.
Proof. intros. exists 0. eauto. Qed.
Lemma stpd2_fun: forall m G1 T11 T12 T21 T22,
stpd2 false G1 T21 T11 ->
stpd2 true G1 T12 T22 ->
stpd2 true G1 (TFun m T11 T12) (TFun m T21 T22).
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_mem: forall G1 T11 T12 T21 T22,
stpd2 false G1 T21 T11 ->
stpd2 true G1 T11 T12 ->
stpd2 true G1 T12 T22 ->
stpd2 true G1 (TMem T11 T12) (TMem T21 T22).
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_sel2: forall x T1 TX G1,
index x G1 = Some TX ->
itpd2 G1 TX ->
stpd2 true G1 TX (TMem T1 TTop) ->
stpd2 true G1 T1 (TSel x).
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_sel1: forall x T2 TX G1,
index x G1 = Some TX ->
itpd2 G1 TX ->
stpd2 true G1 TX (TMem TBot T2) ->
stpd2 true G1 (TSel x) T2.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_selx: forall x G1,
stpd2 true G1 (TSel x) (TSel x).
Proof. intros. repeat eu. exists 1. eauto. Qed.
Lemma stpd2_bindx: forall G1 T1 T2 TA1 TA2,
stpd false ((length G1,T1)::G1) T1 T2 -> (* !!! *)
open (length G1) TA1 = T1 ->
open (length G1) TA2 = T2 ->
(* exp G1 T1 (TMem TL TU) -> *)
(* stpd true G1 TL TU -> *)
(* itpd2 ((length G1,T1)::G1) T1 -> *)
stpd2 true G1 (TBind TA1) (TBind TA2).
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_transf: forall G1 T1 T2 T3,
stpd2 true G1 T1 T2 ->
stpd2 false G1 T2 T3 ->
stpd2 false G1 T1 T3.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_wrapf: forall G1 T1 T2,
stpd2 true G1 T1 T2 ->
stpd2 false G1 T1 T2.
Proof. intros. repeat eu. 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.
Lemma stp0f_trans: forall n n1 G1 T1 T2 T3,
stp false G1 T1 T2 n1 ->
stpd false G1 T2 T3 ->
n1 <= n ->
stpd false G1 T1 T3.
Proof.
intros n. induction n.
- Case "z".
intros. assert (n1 = 0). omega. subst. inversion H.
- Case "S n".
intros. inversion H.
+ eapply stpd_transf. eexists. eapply H2. eapply IHn. eapply H3. eapply H0. omega.
+ destruct H0. eapply stpd_transf. eexists. eapply H2. eexists. eapply H0.
Qed.
Lemma stp0f2_trans: forall n n1 G1 T1 T2 T3,
stp2 false G1 T1 T2 n1 ->
stpd2 false G1 T2 T3 ->
n1 <= n ->
stpd2 false G1 T1 T3.
Proof.
intros n. induction n.
- Case "z".
intros. assert (n1 = 0). omega. subst. inversion H.
- Case "S n".
intros. inversion H.
+ eapply stpd2_transf. eexists. eapply H2. eapply IHn. eapply H3. eapply H0. omega.
+ destruct H0. eapply stpd2_transf. eexists. eapply H2. eexists. eapply H0.
Qed.
(* implementable context *)
Definition env_itp G := forall x T, index x G = Some T -> itpd G T.
(* left: may use axiom but has size. must shrink *)
(* right: no axiom but can grow *)
Definition trans_on n2 :=
forall m G1 T1 T2 T3,
stp2 m G1 T1 T2 n2 ->
stpd2 true G1 T2 T3 ->
stpd2 true G1 T1 T3.
Hint Unfold trans_on.
Definition trans_up n := forall n1, n1 <= n ->
trans_on n1.
Hint Unfold trans_up.
Lemma trans_le: forall n n1,
trans_up n ->
n1 <= n ->
trans_on n1
.
Proof. intros. unfold trans_up in H. eapply H. eauto. Qed.
Lemma upd_length_same: forall {X} G x (T:X),
length G = length (update x T G).
Proof.
intros X G x T. induction G.
- simpl. reflexivity.
- destruct a as [n' Ta]. simpl.
remember (beq_nat x (length G)).
destruct b.
+ simpl. reflexivity.
+ simpl. f_equal. apply IHG.
Qed.
Lemma upd_hit: forall {X} G G' x x' (T:X) T',
index x G = Some T ->
update x' T' G = G' ->
beq_nat x x' = true ->
index x G' = Some T'.
Proof.
intros X G G' x x' T T' Hi Hu Heq.
subst. induction G.
- simpl in Hi. inversion Hi.
- destruct a as [n' Ta]. simpl in Hi.
remember (beq_nat x (length G)).
apply beq_nat_true in Heq.
destruct b.
+ simpl.
apply beq_nat_eq in Heqb.
subst. subst.
rewrite <- beq_nat_refl.
simpl.
rewrite <- beq_nat_refl.
reflexivity.
+ simpl.
subst.
rewrite <- Heqb.
simpl.
assert ((length G) = (length (update x' T' G))) as HnG. {
apply upd_length_same.
}
rewrite <- HnG.
rewrite <- Heqb.
apply IHG.
apply Hi.
Qed.
Lemma upd_miss: forall {X} G G' x x' (T:X) T',
index x G = Some T ->
update x' T' G = G' ->
beq_nat x x' = false ->
index x G' = Some T.
Proof.
intros X G G' x x' T T' Hi Hu Heq.
subst. induction G.
- simpl in Hi. inversion Hi.
- destruct a as [n' Ta]. simpl in Hi. simpl.
remember (beq_nat x (length G)).
destruct b.
+ apply beq_nat_eq in Heqb.
subst.
rewrite beq_nat_sym. rewrite Heq.
simpl.
assert ((length G) = (length (update x' T' G))) as HnG. {
apply upd_length_same.
}
rewrite <- HnG.
rewrite <- beq_nat_refl.
apply Hi.
+ remember (beq_nat x' (length G)) as b'.
destruct b'.
* simpl.
rewrite <- Heqb.
apply Hi.
* simpl.
assert ((length G) = (length (update x' T' G))) as HnG. {
apply upd_length_same.
}
rewrite <- HnG.
rewrite <- Heqb.
apply IHG.
apply Hi.
Qed.
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. destruct a.
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_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. destruct a. reflexivity.
Qed.
Lemma update_extend: forall X (T1: X) (TX1: X) G1 G1' x a,
index x G1 = Some T1 ->
update x TX1 G1 = G1' ->
update x TX1 (a::G1) = (a::G1').
Proof.
intros X T1 TX1 G1 G1' x a Hi Hu.
assert (x < length G1) as Hlt. {
eapply index_max.
eauto.
}
assert (x <> length G1) as Hneq by omega.
assert (beq_nat x (length G1) = false) as E. {
eapply beq_nat_false_iff; eauto.
}
destruct a as [n' Ta].
simpl. rewrite E. rewrite Hu. reflexivity.
Qed.
Lemma update_pres_len: forall X (TX1: X) G1 G1' x,
update x TX1 G1 = G1' ->
length G1 = length G1'.
Proof.
intros X TX1 G1 G1' x H. subst. apply upd_length_same.
Qed.
Lemma stp_extend : forall m G1 T1 T2 x v n,
stp m G1 T1 T2 n ->
stp m ((x,v)::G1) T1 T2 n.
Proof. admit. (*intros. destruct H. eexists. eapply stp_extend1. apply H.*) Qed.
Lemma itp_extend: forall G T n x v,
itp G T n ->
itp ((x,v)::G) T n.
Proof.
intros. induction H; eauto using index_extend.
Qed.
(* currently n,n2 unrelated, but may change. keep in sync with env_itp definition *)
Lemma env_itp_extend : forall G x v,
env_itp G ->
itpd ((x,v)::G) v ->
env_itp ((x,v)::G).
Proof.
intros. unfold env_itp in H. unfold env_itp. intros.
case_eq (beq_nat x0 (length G)); intros.
- assert (x0 = (length G)). eapply beq_nat_true_iff; eauto.
subst x0. unfold index in H1. rewrite H2 in H1. inversion H1. subst v.
eapply H0.
- assert (x0 <> (length G)). eapply beq_nat_false_iff; eauto.
assert (x0 < length ((x,v)::G)). eapply index_max; eauto.
unfold index in H1. rewrite H2 in H1.
eapply H in H1. destruct H1. eexists. eapply itp_extend. apply H1.
Qed.
Lemma stp_narrow: forall n, forall m G1 T1 T2 n1 n0,
stp m G1 T1 T2 n0 ->
n0 <= n ->
forall x TX1 TX2 G1',
index x G1 = Some TX2 ->
update x TX1 G1 = G1' ->
stp false G1' TX1 TX2 n1 ->
stpd m G1' T1 T2.