-
Notifications
You must be signed in to change notification settings - Fork 11
/
dot-stp-single1.v
1514 lines (1278 loc) · 44.4 KB
/
dot-stp-single1.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
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" ].
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 n2,
index x G1 = Some TX ->
itp G1 TX n2 ->
stp true G1 TX (TMem T1 TTop) n1 ->
stp true G1 T1 (TSel x) (S (n1+n2))
| stp_sel1: forall x T2 TX G1 n1 n2,
index x G1 = Some TX ->
itp G1 TX n2 ->
stp true G1 TX (TMem TBot T2) n1 ->
stp true G1 (TSel x) T2 (S (n1+n2))
| 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 n2,
stp true ((length G1,T1)::G1) T1 T2 n1 ->
open (length G1) TA1 = T1 ->
open (length G1) TA2 = T2 ->
(* we need exp T1 D here *)
(* how will this interact with narrowing? *)
(* exp G1 T1 (TMem TL TU) -> *)
(* stp true G1 TL TU n2 -> *)
itp ((length G1,T1)::G1) T1 n2 ->
stp true G1 (TBind TA1) (TBind TA2) (S (n1+n2))
| 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 *)
with 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 -> (* as it looks now, this may not even 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? see itp_narrow for additional circularity considerations *)
itp G1 (TSel x) (S n1)
.
(*
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.
(* ############################################################ *)
(* 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_mem. eapply stp_wrapf. eapply stp_bot.
eapply stp_bool.
eapply stp_top. compute. eauto. compute. eauto. eauto.
Grab Existential Variables. apply 0. apply 0. 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_mem. eapply stp_wrapf.
eapply stp_sel1. compute. eauto. eauto. eapply stp_mem. eauto. eauto. eauto. eauto.
eapply stp_sel2. compute. eauto. eauto.
eauto. eauto. eauto. eauto.
Grab Existential Variables. apply 0. apply 0. apply 0. apply 0. apply 0. apply 0. 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.
Hint Unfold stpd.
Hint Unfold itpd.
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: exists n: nat , _ |- _ =>
destruct H as [e P] *)
end.
Lemma stpd_bot: forall G1 T,
stpd true G1 TBot T.
Proof. intros. exists 2. 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 ->
itpd G1 TX ->
stpd true 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 ->
itpd G1 TX ->
stpd true 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 true ((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 -> *)
itpd ((length G1,T1)::G1) T1 ->
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.
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 n2 G1 T1 T2 T3,
stp false G1 T1 T2 n1 ->
stp false G1 T2 T3 n2 ->
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.
+ eapply stpd_transf. eexists. eapply H2. eexists. eapply H0.
Qed.
(* implementable context *)
Definition env_itp G (n:nat) := forall x T, index x G = Some T -> exists n2, itp G T n2.
(* left: may use axiom but has size. must shrink *)
(* right: no axiom but can grow *)
Definition trans_env_on G1 n2 :=
forall m T1 T2 T3,
stp m G1 T1 T2 n2 ->
stpd true G1 T2 T3 ->
stpd true G1 T1 T3.
Hint Unfold trans_env_on.
Definition trans_on n1 n2 :=
forall G1,
env_itp G1 n1 ->
trans_env_on G1 n2.
Hint Unfold trans_on.
Definition trans_up n := forall n1 n2, n1 + n2 <= n ->
trans_on n1 n2.
Hint Unfold trans_up.
Lemma trans_le: forall n n1 n2,
trans_up n ->
n1 + n2 <= n ->
trans_on n1 n2
.
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 n x v n2,
env_itp G n ->
itp ((x,v)::G) v n2 ->
env_itp ((x,v)::G) n.
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.
eexists. 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 itp_narrow: forall G1 G1' T1 x TX1 TX2,
itpd G1 T1 ->
index x G1 = Some TX2 ->
update x TX1 G1 = G1' ->
(* env_itp G1 nG -> right now not needed at all! *)
itpd G1' TX1 ->
itpd G1' T1.
Proof.
intros.
destruct H. destruct H2.
induction H.
+ SCase "top". exists 0. eauto.
+ SCase "bool". exists 0. eauto.
+ SCase "mem".
assert (exists nx : nat, itp G1' TU nx) as IH. eapply IHitp; eauto.
destruct IH.
eexists. eapply itp_mem. eauto.
+ SCase "sel".
assert (exists nx : nat, itp G1' TX nx) as IH. eapply IHitp; eauto.
destruct IH.
case_eq (beq_nat x0 x); intros E.
* assert (x0 = x) as EX. eapply beq_nat_true_iff; eauto.
subst. index_subst. index_subst.
eexists. eapply itp_sel. eapply upd_hit. eauto. eauto. eauto. eauto.
* assert (x0 <> x) as EX. eapply beq_nat_false_iff; eauto.
subst.
eexists. eapply itp_sel. eapply upd_miss. eauto. eauto. eauto. eauto.
Grab Existential Variables. apply 0.
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' nG nI,
index x G1 = Some TX2 ->
update x TX1 G1 = G1' ->
stp true G1' TX1 TX2 n1 ->
env_itp G1' nG ->
itp G1' TX1 nI ->
trans_on nG n1 ->
stpd m G1' T1 T2.
Proof.
intros n.
induction n.
(* z *)
intros m G1 T1 T2 n1 n0 H NE.
inversion H; intros; try solve [ omega ].
eapply stpd_bot.
eapply stpd_top.
eapply stpd_bool.
(* s n *)
intros m G1 T1 T2 n1 n0 H NE.
inversion H; intros.
- Case "Bot".
intros. eapply stpd_bot.
- Case "Top".
intros. eapply stpd_top.
- Case "Bool".
intros. eapply stpd_bool.
- Case "Fun".
intros. eapply stpd_fun. eapply IHn; eauto. omega. eapply IHn; eauto. omega.
- Case "Mem".
intros. eapply stpd_mem. eapply IHn; eauto. omega. eapply IHn; eauto. omega. eapply IHn; eauto. omega.
- Case "Sel2". intros.
{ case_eq (beq_nat x x0); intros E.
assert (env_itp G1' nG). auto.
(* hit updated binding *)
+ assert (x = x0) as EX. eapply beq_nat_true_iff; eauto. subst. index_subst. index_subst.
eapply stpd_sel2. eapply upd_hit; eauto.
(* already have itp *)
eauto.
(* trans, and narrow by induction *)
eapply H13. eapply H11. eapply H10. eapply IHn; eauto. omega.
(* other binding *)
+ assert (x <> x0) as EX. eapply beq_nat_false_iff; eauto.
eapply stpd_sel2. eapply upd_miss; eauto.
(* new itp from env_itp: alt 1 -- call env_itp *)
(* eapply H5. eapply upd_miss; eauto. *)
(* alt 2 -- call itp_narrow *)
eapply itp_narrow; eauto.
(* narrow stp by induction *)
eapply IHn; eauto. omega.
}
- Case "Sel1". intros.
{ case_eq (beq_nat x x0); intros E.
assert (env_itp G1' nG). auto.
(* hit updated binding *)
+ assert (x = x0) as EX. eapply beq_nat_true_iff; eauto. subst. index_subst. index_subst.
eapply stpd_sel1. eapply upd_hit; eauto.
(* already have itp *)
eauto.
(* trans, and narrow by induction *)
eapply H13. eapply H11. eapply H10. eapply IHn; eauto. omega.
(* other binding *)
+ assert (x <> x0) as EX. eapply beq_nat_false_iff; eauto.
eapply stpd_sel1. eapply upd_miss; eauto.
(* new itp: see above for alternatives *)
eapply itp_narrow; eauto.
(* narrow stp by induction *)
eapply IHn; eauto. omega.
}
- Case "Selx". eapply stpd_selx.
- Case "Bindx".
(*
Note: there is a choice to make whether itp_narrow can use
the extended G1 (i.e. members can refer to this) or
whether it can rely on env_itp. Both together seems tricky,
because env_itp_extend would need result of itp_narrow
(and vice versa).
All upper bounds must be realizable (deep, via itp).
*)
assert (length G1 = length G1'). { eapply update_pres_len; eauto. }
remember (length G1) as L. clear HeqL. subst L.
assert (itp ((length G1', T0)::G1) T0 n3). { eauto. } (* already have it! *)
(* will do induction with extended env. need to prove T1 realizable in G1' *)
assert (itpd ((length G1', T0)::G1') T0) as IE. {
eapply itp_narrow.
eexists. eapply H3.
eapply index_extend; eauto.
eapply update_extend; eauto.
eexists. eapply itp_extend; eauto.
}
destruct IE.
eapply stpd_bindx. {
eapply IHn. eauto. omega.
eapply index_extend; eauto.
eapply update_extend; eauto.
eapply stp_extend; eauto.
eapply env_itp_extend. eauto. eauto.
eapply itp_extend. eauto. eauto.
}
eauto.
eauto.
eauto.
- Case "Trans". eapply stpd_transf. eapply IHn; eauto. omega. eapply IHn; eauto. omega.
- Case "Wrap". eapply stpd_wrapf. eapply IHn; eauto. omega.
Qed.
(* ---------- EXPANSION / MEMBERSHIP ---------- *)
(* In the current version, expansion is an implementation
detail. We just use it to derive some helper lemmas
to show that implementable types have good bounds.
*)
(* expansion / membership *)
(* this is the precise version! no slack in lookup *)
(* TODO: need the name for bind? *)
Inductive exp : tenv -> ty -> ty -> Prop :=
(*
| exp_bot: forall G1,
exp G1 TBot (TMem TTop TBot) (* causes trouble in inv_mem: need to build stp deriv with smaller n *)
*)
| exp_mem: forall G1 T1 T2,
exp G1 (TMem T1 T2) (TMem T1 T2)
| exp_sel: forall G1 x T T2 T3 T4 T5,
index x G1 = Some T ->
exp G1 T (TMem T2 T3) ->
exp G1 T3 (TMem T4 T5) ->
exp G1 (TSel x) (TMem T4 T5)
.
(*
NOT NEEDED (apparently)
Lemma exp_unique: forall G1 T1 TA1 TA2 TA1L TA2L,
exp G1 T1 (TMem TA1L TA1) ->
exp G1 T1 (TMem TA2L TA2) ->
TA1 = TA2.
Proof. Qed.
*)
(* key lemma that relates exp and stp. result has bounded size. *)
Lemma stpd_inv_mem: forall n n1 G1,
forall TA1 TA2 TA1L TA2L T1,
exp G1 T1 (TMem TA1L TA1) ->
stp true G1 T1 (TMem TA2L TA2) n1 ->
n1 <= n ->