-
Notifications
You must be signed in to change notification settings - Fork 11
/
dotb.v
1104 lines (987 loc) · 31.8 KB
/
dotb.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
DOING:
- add bindx (and/or bind2/bind1) rules
TODO:
- transitivity will only hold in realizable context
QUESTIONS:
- exact statement of transitivity?
- what's the right notion of realizable types/contexts?
- how can we do induction across realizability evidence?
*)
(* ############################################################ *)
(* 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 T := closed_rec 0 T.
Inductive bound_fv: id -> ty -> Prop :=
| bfv_nof: forall u,
bound_fv u TNoF
| bfv_top: forall u,
bound_fv u TTop
| bfv_bot: forall u,
bound_fv u TBot
| bfv_bool: forall u,
bound_fv u TBool
| bfv_fun: forall u m T1 T2,
bound_fv u T1 ->
bound_fv u T2 ->
bound_fv u (TFun m T1 T2)
| bfv_mem: forall u T1 T2,
bound_fv u T1 ->
bound_fv u T2 ->
bound_fv u (TMem T1 T2)
| bfv_and: forall u T1 T2,
bound_fv u T1 ->
bound_fv u T2 ->
bound_fv u (TAnd T1 T2)
| bfv_bind: forall u T1,
bound_fv u T1 ->
bound_fv u (TBind T1)
| bfv_selb: forall u i,
bound_fv u (TSelB i)
| bfv_sel: forall u x,
u > x ->
bound_fv u (TSel x)
.
Hint Constructors bound_fv.
Inductive bound_fvs: id -> tenv -> Prop :=
| bound_fvs_nil : forall u,
bound_fvs u []
| bound_fvs_cons: forall u x T G,
bound_fv u T ->
bound_fvs u ((x,T)::G)
.
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.
Fixpoint swap (u1: id) (u2: id) (T: ty) { struct T }: ty :=
match T with
| TSel x => if beq_nat u1 x then TSel u2
else if beq_nat u2 x then TSel u1
else TSel x
| TSelB i => TSelB i
| TBind T1 => TBind (swap u1 u2 T1)
| TNoF => TNoF
| TBot => TBot
| TTop => TTop
| TBool => TBool
| TAnd T1 T2 => TAnd (swap u1 u2 T1) (swap u1 u2 T2)
| TMem T1 T2 => TMem (swap u1 u2 T1) (swap u1 u2 T2)
| TFun m T1 T2 => TFun m (swap u1 u2 T1) (swap u1 u2 T2)
end.
(* 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, ... *)
(* ############################################################ *)
(* 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 env x TE TL TU,
index x env = Some (TE) ->
tresolve x TE (TMem TL TU) ->
wf_type env (TSel x)
| wf_bind: forall f env T TA,
bound_fvs (length env) env ->
bound_fv (length env) TA ->
open (length env) TA = T ->
wf_type ((f,T)::env) T ->
wf_type env (TBind TA)
.
Tactic Notation "wf_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "Top" |
Case_aux c "Bool" |
Case_aux c "And" |
Case_aux c "Mem" |
Case_aux c "Fun" |
Case_aux c "Sel" |
Case_aux c "Bind" ].
(* TODO: need to see what to do for expansion / 'has' *)
(*
x: { z => Tz }
x: Tx
x: y.A(L..U)
x: U
*)
Inductive stp : bool -> tenv -> ty -> ty -> nat -> Prop :=
| 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,
stp false G1 T21 T11 n1 ->
stp true G1 T12 T22 n2 ->
stp true G1 (TMem T11 T12) (TMem T21 T22) (S (n1+n2))
| stp_sel2: forall x T1 TX G1 n1,
index x G1 = Some TX ->
stp true 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 true G1 TX (TMem TBot T2) n1->
stp true G1 (TSel x) T2 (S n1)
| stp_selx: forall x TX TL TU G1 n1,
index x G1 = Some TX ->
tresolve x TX (TMem TL TU) ->
stp true G1 (TSel x) (TSel x) 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 f G1 T1 T2 TA1 TA2 n1,
bound_fvs (length G1) G1 ->
bound_fv (length G1) TA1 ->
bound_fv (length G1) TA2 ->
stp true ((f,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)
.
Tactic Notation "stp_cases" tactic(first) ident(c) :=
first;
[
Case_aux c "Bool < Bool" |
Case_aux c "Fun < Fun" |
Case_aux c "Mem < Mem" |
(*
Case_aux c "Bind < Bind" |
Case_aux c "T & ? < T" |
Case_aux c "? & T < T" |
Case_aux c "? < ? & ?" |
*)
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.
Definition stpd b G1 T1 T2 := exists n, stp b G1 T1 T2 n.
Hint Unfold stpd.
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: exists n: nat , _ |- _ =>
destruct H as [e P] *)
end.
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 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 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 ->
stpd true G1 TX (TMem TBot T2) ->
stpd true G1 (TSel x) T2.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd_selx: forall x TX TL TU G1,
index x G1 = Some TX ->
tresolve x TX (TMem TL TU) ->
stpd true G1 (TSel x) (TSel x).
Proof. intros. repeat eu. exists 0. eauto. Qed.
Lemma stpd_bindx: forall f G1 T1 T2 TA1 TA2,
bound_fvs (length G1) G1 ->
bound_fv (length G1) TA1 ->
bound_fv (length G1) TA2 ->
stpd true ((f,T1)::G1) T1 T2 ->
open (length G1) TA1 = T1 ->
open (length G1) TA2 = T2 ->
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.
Definition trans_on n1 :=
forall m T1 T2 T3 G1,
stp m G1 T1 T2 n1 ->
stpd true G1 T2 T3 ->
stpd 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 closed_open_up_rec: forall k j x T,
k >= j ->
closed_rec j (open_rec k x T) ->
closed_rec (S k) T.
Proof.
intros k j x T Hcmp H.
generalize dependent j. generalize dependent k.
induction T;
intros k j Hcmp H;
eauto; try solve [inversion H; subst; eauto].
- Case "SelB".
unfold open_rec in H.
remember (beq_nat k i). destruct b.
+ apply cl_selb. apply beq_nat_eq in Heqb. subst. omega.
+ inversion H. subst. apply cl_selb. omega.
- Case "Bind".
apply cl_bind. apply IHT with (j:=(S k)). omega. inversion H. subst.
apply closed_upgrade with (i:=(S j)). assumption. omega.
Qed.
Lemma closed_open_up: forall x T,
closed (open x T) ->
closed_rec 1 T.
Proof.
intros x T H. unfold closed in H. unfold open in H.
apply closed_open_up_rec with (x:=x) (j:=0).
- omega.
- assumption.
Qed.
Lemma stp_closed: forall m G T1 T2 n,
stp m G T1 T2 n ->
closed T1 /\ closed T2.
Proof.
intros. stp_cases (induction H) Case; eauto;
try solve [inversion IHstp; split; eauto];
try solve [inversion IHstp1; inversion IHstp2; split; eauto];
try solve [inversion IHstp as [IHX IHMem]; inversion IHMem; subst; split; eauto].
- Case "Bind < Bind".
inversion IHstp as [IHstp1 IHstp2]. subst. unfold closed.
split; solve [apply cl_bind; apply closed_open_up with (x:=length G1); assumption].
Qed.
Lemma index_range: forall {X} i G (T:X),
index i G = Some T ->
i < length G.
Proof.
intros X i G T H. induction G.
- inversion H.
- simpl. simpl in H.
remember (beq_nat i (length G)).
destruct b.
+ apply beq_nat_eq in Heqb. omega.
+ apply lt_S. apply IHG. destruct a as [j G']. assumption.
Qed.
Lemma index_neq: forall {X} i j G (T:X),
index i G = Some T ->
j >= length G ->
i <> j.
Proof.
intros. apply index_range in H. omega.
Qed.
Lemma stp_bound_fv: forall m G T1 T2 n,
stp m G T1 T2 n ->
bound_fv (length G) T1 /\ bound_fv (length G) T2.
Proof.
intros m G T1 T2 n H. stp_cases (induction H) Case; eauto;
try solve [
inversion IHstp1; inversion IHstp2;
split; try constructor; assumption].
- Case "? < Sel".
inversion IHstp as [IHX IHMem]. inversion IHMem; subst.
split; eauto. constructor. unfold ">".
eapply index_range. apply H.
- Case "Sel < ?".
inversion IHstp as [IHX IHMem]. inversion IHMem; subst.
split; eauto. constructor. unfold ">".
eapply index_range. apply H.
- Case "Sel < Sel".
split; constructor; eapply index_range; eauto.
Qed.
Lemma bound_fv_inc: forall u T,
bound_fv u T ->
bound_fv (S u) T.
Proof.
intros u T H. induction H; eauto.
Qed.
Lemma stp1_bound_fv: forall m G T1 T2 n,
stp m G T1 T2 n ->
bound_fv (length G) T1.
Proof.
intros m G T1 T2 n H.
apply (proj1 (stp_bound_fv m G T1 T2 n H)).
Qed.
Lemma stp2_bound_fv: forall m G T1 T2 n,
stp m G T1 T2 n ->
bound_fv (length G) T2.
Proof.
intros m G T1 T2 n H.
apply (proj2 (stp_bound_fv m G T1 T2 n H)).
Qed.
Lemma index_shrink: forall {X} i x (Tx:X) G (T:X),
index i ((x,Tx)::G) = Some T ->
i < length G ->
index i G = Some T.
Proof.
intros. inversion H.
remember (beq_nat i (length G)).
induction b.
+ apply beq_nat_eq in Heqb. omega.
+ reflexivity.
Qed.
Lemma index_ext_same: forall {X} G x x' (T:X) (T':X),
index x G = Some T ->
index x ((x',T')::G) = Some T.
Proof.
intros X G x x' T T' H.
assert (x < length G) as A by solve [eapply index_range; apply H].
generalize dependent T'. generalize dependent x'.
destruct G.
- inversion H.
- assert (beq_nat x (S (length G)) = false) as A'.
apply false_beq_nat. simpl in A. omega.
intros x' T'. destruct p as [x1 T1].
simpl. rewrite A'.
remember (beq_nat x (length G)).
destruct b.
+ inversion H. rewrite <- Heqb. reflexivity.
+ inversion H. rewrite <- Heqb. reflexivity.
Qed.
Lemma update_ext_same: forall {X} G x x' (T:X) (T':X) Gu (Tu:X),
index x G = Some T ->
update x Tu G = Gu ->
update x Tu ((x',T')::G) = (x',T')::Gu.
Proof.
intros X G x x' T T' Gu Tu H Hu.
assert (x < length G) as A by solve [eapply index_range; apply H].
generalize dependent T'. generalize dependent x'.
destruct G.
- inversion H.
- assert (beq_nat x (S (length G)) = false) as A'.
apply false_beq_nat. simpl in A. omega.
intros x' T'. destruct p as [x1 T1].
simpl. rewrite A'.
remember (beq_nat x (length G)).
destruct b.
+ inversion Hu. unfold update. rewrite <- Heqb. reflexivity.
+ inversion Hu. unfold update. rewrite <- Heqb. reflexivity.
Qed.
Lemma swap_open_rec: forall n x y T Tx Ty k,
bound_fv n T ->
x >= n ->
y >= n ->
open_rec k x T = Tx ->
open_rec k y T = Ty ->
swap x y Tx = Ty.
Proof.
intros n x y T Tx Ty k HT Hnx Hny HTx HTy.
generalize dependent Ty. generalize dependent Tx. generalize dependent k.
induction T;
intros k Tx HTx Ty HTy;
try solve [
compute in HTx; compute in HTy; subst; compute; reflexivity];
try solve [
unfold open_rec in HTx; fold open_rec in HTx;
unfold open_rec in HTy; fold open_rec in HTy;
subst; unfold swap; fold swap;
f_equal;
try solve [
apply IHT1 with (k:=k);
try solve [inversion HT; subst; assumption];
try solve [reflexivity]];
try solve [
apply IHT2 with (k:=k);
try solve [inversion HT; subst; assumption];
try solve [reflexivity]]].
- Case "Sel".
compute in HTx. compute in HTy. subst.
unfold swap. inversion HT. subst.
remember (beq_nat x i) as bxi.
destruct bxi.
+ apply beq_nat_eq in Heqbxi. omega.
+ remember (beq_nat y i) as byi.
destruct byi.
apply beq_nat_eq in Heqbyi. omega.
reflexivity.
- Case "SelB".
unfold open_rec in HTx.
unfold open_rec in HTy.
remember (beq_nat k i).
destruct b.
+ subst. unfold swap. rewrite <- beq_nat_refl. reflexivity.
+ subst. compute. reflexivity.
- Case "Bind".
unfold open_rec in HTx. fold open_rec in HTx.
unfold open_rec in HTy. fold open_rec in HTy.
subst. simpl. f_equal.
apply IHT with (k:=S k); try reflexivity.
+ inversion HT. subst. assumption.
Qed.
Lemma swap_open: forall n x y T Tx Ty,
bound_fv n T ->
x >= n ->
y >= n ->
open x T = Tx ->
open y T = Ty ->
swap x y Tx = Ty.
Proof.
intros n x y T Tx Ty HT Hnx Hny HTx HTy.
apply (swap_open_rec n x y T Tx Ty 0 HT Hnx Hny HTx HTy).
Qed.
Lemma swap_bound: forall n x y T,
bound_fv n T ->
x >= n ->
y >= n ->
swap x y T = T.
Proof.
intros n x y T HT Hx Hy.
induction T;
try solve [compute; reflexivity];
try solve [unfold swap; fold swap; f_equal;
try solve [apply IHT1; inversion HT; subst; assumption];
try solve [apply IHT2; inversion HT; subst; assumption]].
- Case "Sel".
unfold swap. inversion HT. subst.
remember (beq_nat x i) as bxi.
destruct bxi.
+ apply beq_nat_eq in Heqbxi. omega.
+ remember (beq_nat y i) as byi.
destruct byi.
apply beq_nat_eq in Heqbyi. omega.
reflexivity.
- Case "Bind".
simpl. f_equal.
apply IHT; try reflexivity.
+ inversion HT. subst. assumption.
Qed.
Lemma stp_swap_rec: forall n x Tx y Ty G0 G T1 T2 m,
bound_fvs (length G) G ->
stp m (G0++(x,Tx)::(y,Ty)::G) T1 T2 n ->
stp m
(G0++
(y,(swap (length G) (S (length G)) Ty))::
(x,(swap (length G) (S (length G)) Tx))
::G)
(swap (length G) (S (length G)) T1)
(swap (length G) (S (length G)) T2)
n.
Proof.
intros n x Tx y Ty G0 G T1 T2 m Hb H.
remember (G0++(x,Tx)::(y,Ty)::G) as Gt.
generalize dependent G0.
stp_cases (induction H) Case; intros; auto.
- Case "Fun < Fun".
simpl. apply stp_fun.
+ apply IHstp1; assumption.
+ apply IHstp2; assumption.
- Case "Mem < Mem".
simpl. apply stp_mem.
+ apply IHstp1; assumption.
+ apply IHstp2; assumption.
- Case "? < Sel".
(*
This is gettign too unwiedly.
There are four cases to consider:
TSel x0, such that
x0 is in G0. we still need to express that G0 may contain swaps.
x0 is either length G or (S (length G)) in which case swapping occurs. OK.
x0 is in G. swapping is not possible.
*)
(*
simpl. remember (beq_nat (length G) x0). destruct b.
+ apply beq_nat_eq in Heqb. subst.
assert (index (length G) (G0 ++ (x, Tx) :: (y, Ty) :: G) = Some Ty) as HeqX.
admit.
rewrite H in HeqX. inversion HeqX. clear HeqX. subst.
apply stp_sel2 with (TX:=swap (length G) (S (length G)) Ty).
admit.
apply IHstp. reflexivity.
+ (* sigh *)
*)
admit.
- Case "Sel < ?".
admit.
- Case "Sel < Sel".
admit.
- Case "Bind < Bind".
admit.
- Case "Trans".
simpl. eapply stp_transf.
+ apply IHstp1; assumption.
+ apply IHstp2; assumption.
Qed.
Lemma stp_swap: forall n x Tx y Ty G T1 T2 m,
bound_fvs (length G) G ->
stp m ((x,Tx)::(y,Ty)::G) T1 T2 n ->
stp m
((y,(swap (length G) (S (length G)) Ty))::
(x,(swap (length G) (S (length G)) Tx))
::G)
(swap (length G) (S (length G)) T1)
(swap (length G) (S (length G)) T2)
n.
Proof.
intros n x Tx y Ty G T1 T2 m Hb H.
apply (stp_swap_rec n x Tx y Ty [] G T1 T2 m Hb H).
Qed.
Lemma stp_ext_swap: forall n x Tx y G T1 T2,
bound_fvs (length G) G ->
bound_fv (length G) Tx ->
stp true ((x,Tx)::(y,T1)::G) T1 T2 n ->
stp true
((y,(swap (length G) (S (length G)) T1))::(x,Tx)::G)
(swap (length G) (S (length G)) T1)
(swap (length G) (S (length G)) T2)
n.
Proof.
intros n x Tx y G T1 T2 HbG HbTx H.
assert (swap (length G) (S (length G)) Tx = Tx) as Heq. {
apply swap_bound with (n:=length G).
assumption.
omega.
omega.
}
rewrite <- Heq.
apply stp_swap; assumption.
Qed.
Lemma stp_ext_open: forall n x Tx y G T1 T2,
bound_fvs (length G) G ->
bound_fv (length G) Tx ->
bound_fv (length G) T1 ->
bound_fv (length G) T2 ->
stp true ((x,Tx)::(y,(open (length G) T1))::G) (open (length G) T1) (open (length G) T2) n ->
stp true ((y,(open (length ((x,Tx)::G)) T1))::(x,Tx)::G) (open (length ((x,Tx)::G)) T1) (open (length ((x,Tx)::G)) T2) n.
Proof.
intros n x Tx y G T1 T2 HbG HbTx HbT1 HbT2 H.
simpl.
remember (open (length G) T1) as TO1.
remember (open (length G) T2) as TO2.
remember (open (S (length G)) T1) as TS1.
remember (open (S (length G)) T2) as TS2.
assert (swap (length G) (S (length G)) TO1 = TS1) as HOS1. {
apply swap_open with (n:=length G) (T:=T1).
assumption.
omega.
omega.
rewrite HeqTO1. reflexivity.
rewrite HeqTS1. reflexivity.
}
assert (swap (length G) (S (length G)) TO2 = TS2) as HOS2. {
apply swap_open with (n:=length G) (T:=T2).
assumption.
omega.
omega.
rewrite HeqTO2. reflexivity.
rewrite HeqTS2. reflexivity.
}
rewrite <- HOS1. rewrite <- HOS2.
apply stp_ext_swap.
assumption.
assumption.
assumption.
Qed.
Lemma stp_ext: forall m G T1 T2 n x Tx,
stp m G T1 T2 n ->
bound_fv (length G) Tx -> (* Tx cannot refer to self b/c of swapping in Bind case *)
stp m ((x,Tx)::G) T1 T2 n.
Proof.
intros m G T1 T2 n x Tx H Hmax.
stp_cases (induction H) Case; eauto.
- Case "? < Sel". eapply stp_sel2.
eapply index_ext_same. apply H. apply IHstp.
assumption.
- Case "Sel < ?". eapply stp_sel1.
eapply index_ext_same. apply H. apply IHstp.
assumption.
- Case "Sel < Sel". eapply stp_selx.
eapply index_ext_same. apply H. apply H0.
- Case "Bind < Bind". eapply stp_bindx.
+ subst. apply bound_fvs_cons. simpl. apply bound_fv_inc. assumption.
+ subst. simpl. apply bound_fv_inc. assumption.
+ subst. simpl. apply bound_fv_inc. assumption.
+ subst. eapply stp_ext_open.
assumption.
assumption.
apply H0.
apply H1.
apply IHstp.
simpl. apply bound_fv_inc. assumption.
+ reflexivity.
+ reflexivity.
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. admit. 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. admit. Qed.
Lemma stp_narrow: forall m G1 T1 T2 n1,
stpd m G1 T1 T2 ->
forall x TX1 TX2 G1',
index x G1 = Some TX2 ->
update x TX1 G1 = G1' ->
stp true G1' TX1 TX2 n1 ->
trans_on n1 ->
stpd m G1' T1 T2.
Proof.
intros m G1 T1 T2 n1 H. destruct H as [n2 H].
induction H; intros.
- Case "Bool".
intros. eapply stpd_bool.
- Case "Fun".
intros. eapply stpd_fun. eapply IHstp1; eauto. eapply IHstp2; eauto.
- Case "Mem".
intros. eapply stpd_mem. eapply IHstp1; eauto. eapply IHstp2; eauto.
- Case "Sel2". intros.
{ case_eq (beq_nat x x0); intros E.
(* 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. eapply H4. eapply H3. eapply IHstp; eauto.
(* other binding *)
+ assert (x <> x0) as EX. eapply beq_nat_false_iff; eauto.
eapply stpd_sel2. eapply upd_miss; eauto. eapply IHstp. eapply H1. eauto. eauto. eauto.
}
- Case "Sel1". intros.
{ case_eq (beq_nat x x0); intros E.
(* 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. eapply H4. eapply H3. eapply IHstp; eauto.
(* other binding *)
+ assert (x <> x0) as EX. eapply beq_nat_false_iff; eauto.
eapply stpd_sel1. eapply upd_miss; eauto. eapply IHstp. eapply H1. eauto. eauto. eauto.
}
- Case "Selx". eapply stpd_selx.
- Case "Bindx". eapply stpd_bindx. eapply IHstp.
eapply index_ext_same. eapply H2.
eapply update_ext_same. eapply H2. eapply H3.
- Case "Trans". eapply stpd_transf. eapply IHstp1; eauto. eapply IHstp2; eauto.
- Case "Wrap". eapply stpd_wrapf. eapply IHstp; eauto.
Qed.
Inductive simple_type: ty -> Prop :=
| simple_mem: forall T1,
simple_type (TMem T1 T1) (* otherwise, would need trans_on T1 T2 *)
.
(* no induction, no trans (?) *)
(* proper realizability evidence? *)
Lemma stpd_trans_lo: forall G1 T1 T2 TX,
stpd true G1 T1 T2 ->
stpd true G1 TX (TMem T2 TTop) ->
simple_type TX ->
stpd true G1 TX (TMem T1 TTop).
Proof.
intros. repeat eu. inversion H0; inversion H1.
- eapply stpd_mem. eapply stpd_transf. eexists. eapply H. eexists. subst. eauto. eauto.
- subst. inversion H4. (* NEED TO HANDLE THIS CASE? PROBABLY YES .... *)
Qed.
(* proper realizability evidence? *)
Lemma stpd_trans_hi: forall G1 T1 T2 TX n1,