-
Notifications
You must be signed in to change notification settings - Fork 11
/
fsub1.v
1903 lines (1551 loc) · 59.6 KB
/
fsub1.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
(* Full safety for F-sub (WIP) *)
(* values well-typed with respect to runtime environment *)
(* inversion lemma structure *)
(* this version adds bottom and lower bounds to fsub0.v *)
(*
TODO:
- stp2 trans + narrowing
- stp/stp2 weakening and regularity
*)
Require Export SfLib.
Require Export Arith.EqNat.
Require Export Arith.Le.
Module FSUB.
Definition id := nat.
Inductive ty : Type :=
| TBool : ty
| TBot : ty
| TTop : ty
| TFun : ty -> ty -> ty
| TMem : ty -> ty -> ty
| TSel : id -> ty
| TSelH : id -> ty
| TSelB : id -> ty
| TAll : ty -> ty -> ty
.
Inductive tm : Type :=
| ttrue : tm
| tfalse : tm
| tvar : id -> tm
| tapp : tm -> tm -> tm (* f(x) *)
| tabs : id -> id -> tm -> tm (* \f x.y *)
| ttapp : tm -> ty -> tm (* f[X] *)
| ttabs : id -> ty -> tm -> tm (* \f x.y *)
.
Inductive vl : Type :=
| vty : list (id*vl) -> ty -> vl
| vtya : list (id*vl) -> ty -> vl (* X<T, only used in stp2_all *)
| vbool : bool -> vl
| vabs : list (id*vl) -> id -> id -> tm -> vl
| vtabs : list (id*vl) -> id -> ty -> tm -> vl
.
Definition tenv := list (id*ty).
Definition venv := list (id*vl).
Definition aenv := list (id*(venv*ty)).
Hint Unfold venv.
Hint Unfold tenv.
Fixpoint fresh {X: Type} (l : list (id * X)): nat :=
match l with
| [] => 0
| (n',a)::l' => 1 + n'
end.
Fixpoint index {X : Type} (n : id) (l : list (id * X)) : option X :=
match l with
| [] => None
| (n',a) :: l' =>
if le_lt_dec (fresh l') n' then
if (beq_nat n n') then Some a else index n l'
else None
end.
Fixpoint indexr {X : Type} (n : id) (l : list (id * X)) : option X :=
match l with
| [] => None
| (n',a) :: l' => (* DeBrujin *)
if (beq_nat n (length l')) then Some a else indexr n l'
end.
(*
Fixpoint update {X : Type} (n : nat) (x: X)
(l : list X) { struct l }: list X :=
match l with
| [] => []
| a :: l' => if beq_nat n (length l') then x::l' else a :: update n x l'
end.
*)
(* LOCALLY NAMELESS *)
Inductive closed_rec: nat -> nat -> ty -> Prop :=
| cl_top: forall k l,
closed_rec k l TTop
| cl_bot: forall k l,
closed_rec k l TBot
| cl_bool: forall k l,
closed_rec k l TBool
| cl_fun: forall k l T1 T2,
closed_rec k l T1 ->
closed_rec k l T2 ->
closed_rec k l (TFun T1 T2)
| cl_mem: forall k l T1 T2,
closed_rec k l T1 ->
closed_rec k l T2 ->
closed_rec k l (TMem T1 T2)
| cl_bind: forall k l T1 T2,
closed_rec k l T1 ->
closed_rec (S k) l T2 ->
closed_rec k l (TAll T1 T2)
| cl_sel: forall k l x,
closed_rec k l (TSel x)
| cl_selh: forall k l x,
l > x ->
closed_rec k l (TSelH x)
| cl_selb: forall k l i,
k > i ->
closed_rec k l (TSelB i)
.
Hint Constructors closed_rec.
Definition closed j l T := closed_rec j l T.
Fixpoint open_rec (k: nat) (u: ty) (T: ty) { struct T }: ty :=
match T with
| TSel x => TSel x (* free var remains free. functional, so we can't check for conflict *)
| TSelH i => TSelH i (*if beq_nat k i then u else TSelH i *)
| TSelB i => if beq_nat k i then u else TSelB i
| TAll T1 T2 => TAll (open_rec k u T1) (open_rec (S k) u T2)
| TTop => TTop
| TBot => TBot
| TBool => TBool
| TMem T1 T2 => TMem (open_rec k u T1) (open_rec k u T2)
| TFun T1 T2 => TFun (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 (TSel 9) (TAll TBool (TFun (TSelB 1) (TSelB 0))) =
(TAll TBool (TFun (TSel 9) (TSelB 0))).
Proof. compute. eauto. Qed.
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)
| TFun T1 T2 => TFun (subst U T1) (subst U T2)
| TSelB i => TSelB i
| TSel i => TSel i
| TSelH i => if beq_nat i 0 then U else TSelH (i-1)
| TAll T1 T2 => TAll (subst U T1) (subst U T2)
end.
Fixpoint nosubst (T : ty) {struct T} : Prop :=
match T with
| TTop => True
| TBot => True
| TBool => True
| TMem T1 T2 => nosubst T1 /\ nosubst T2
| TFun T1 T2 => nosubst T1 /\ nosubst T2
| TSelB i => True
| TSel i => True
| TSelH i => i <> 0
| TAll T1 T2 => nosubst T1 /\ nosubst T2
end.
Hint Unfold open.
Hint Unfold closed.
Inductive stp: tenv -> tenv -> ty -> ty -> Prop :=
| stp_top: forall G1 GH T1,
stp G1 GH T1 TTop
| stp_bot: forall G1 GH T2,
stp G1 GH TBot T2
| stp_bool: forall G1 GH,
stp G1 GH TBool TBool
| stp_fun: forall G1 GH T1 T2 T3 T4,
stp G1 GH T3 T1 ->
stp G1 GH T2 T4 ->
stp G1 GH (TFun T1 T2) (TFun T3 T4)
| stp_mem: forall G1 GH T1 T2 T3 T4,
stp G1 GH T3 T1 ->
stp G1 GH T2 T4 ->
stp G1 GH (TMem T1 T2) (TMem T3 T4)
| stp_sel1: forall G1 GH TL TU T2 x,
index x G1 = Some (TMem TL TU) ->
stp G1 GH TU T2 ->
stp G1 GH (TSel x) T2
| stp_sel2: forall G1 GH TL TU T1 x,
index x G1 = Some (TMem TL TU) ->
stp G1 GH T1 TL ->
stp G1 GH T1 (TSel x)
| stp_selx: forall G1 GH TL TU x,
index x G1 = Some (TMem TL TU) ->
stp G1 GH (TSel x) (TSel x)
| stp_sela1: forall G1 GH TL TU T2 x,
indexr x GH = Some (TMem TL TU) ->
stp G1 GH TU T2 ->
stp G1 GH (TSelH x) T2
| stp_sela2: forall G1 GH TL TU T1 x,
indexr x GH = Some (TMem TL TU) ->
stp G1 GH T1 TL ->
stp G1 GH T1 (TSelH x)
| stp_selax: forall G1 GH TL TU x,
indexr x GH = Some (TMem TL TU) ->
stp G1 GH (TSelH x) (TSelH x)
| stp_all: forall G1 GH T1 T2 T3 T4 x,
stp G1 GH T3 T1 ->
x = length GH ->
closed 1 (length GH) T2 -> (* must not accidentally bind x *)
closed 1 (length GH) T4 ->
stp G1 ((0,T3)::GH) (open (TSelH x) T2) (open (TSelH x) T4) ->
stp G1 GH (TAll T1 T2) (TAll T3 T4)
.
Hint Constructors stp.
(* TODO *)
Inductive has_type : tenv -> tm -> ty -> Prop :=
| t_true: forall env,
has_type env ttrue TBool
| t_false: forall env,
has_type env tfalse TBool
| t_var: forall x env T1,
index x env = Some T1 ->
stp env [] T1 T1 ->
has_type env (tvar x) T1
| t_app: forall env f x T1 T2,
has_type env f (TFun T1 T2) ->
has_type env x T1 ->
has_type env (tapp f x) T2
| t_abs: forall env f x y T1 T2,
has_type ((x,T1)::(f,TFun T1 T2)::env) y T2 ->
stp env [] (TFun T1 T2) (TFun T1 T2) ->
fresh env <= f ->
1+f <= x ->
has_type env (tabs f x y) (TFun T1 T2)
| t_tapp: forall env f T11 T12,
has_type env f (TAll (TMem T11 T11) T12) ->
(* T = open T11 T12 -> *)
stp env [] T12 T12 ->
has_type env (ttapp f T11) T12
(*
NOTE: both the POPLmark paper and Cardelli's paper use this rule:
Does it make a difference? It seems like we can always widen f?
| t_tapp: forall env f T2 T11 T12 ,
has_type env f (TAll T11 T12) ->
stp env T2 T11 ->
has_type env (ttapp f T2) (open T2 T12)
*)
| t_tabs: forall env x y T1 T2,
has_type ((x,T1)::env) y (open (TSel x) T2) ->
stp env [] (TAll T1 T2) (TAll T1 T2) ->
fresh env = x ->
has_type env (ttabs x T1 y) (TAll T1 T2)
| t_sub: forall env e T1 T2,
has_type env e T1 ->
stp env [] T1 T2 ->
has_type env e T2
.
Inductive stp2: venv -> ty -> venv -> ty -> list (id*(venv*ty)) -> Prop :=
| stp2_top: forall G1 G2 GH T,
stp2 G1 T G2 TTop GH
| stp2_bot: forall G1 G2 GH T,
stp2 G1 TBot G2 T GH
| stp2_bool: forall G1 G2 GH,
stp2 G1 TBool G2 TBool GH
| stp2_fun: forall G1 G2 T1 T2 T3 T4 GH,
stp2 G2 T3 G1 T1 GH ->
stp2 G1 T2 G2 T4 GH ->
stp2 G1 (TFun T1 T2) G2 (TFun T3 T4) GH
| stp2_mem: forall G1 G2 T1 T2 T3 T4 GH,
stp2 G2 T3 G1 T1 GH ->
stp2 G1 T2 G2 T4 GH ->
stp2 G1 (TMem T1 T2) G2 (TMem T3 T4) GH
(* we do not require TMem TX here, just TX -- the vty is marker enough *)
(* atm not clear if these are needed *)
| stp2_sel1: forall G1 G2 GX TX x T2 GH,
index x G1 = Some (vty GX TX) ->
closed 0 0 TX ->
stp2 GX TX G2 T2 GH ->
stp2 G1 (TSel x) G2 T2 GH
| stp2_sel2: forall G1 G2 GX TX x T1 GH,
index x G2 = Some (vty GX TX) ->
closed 0 0 TX ->
stp2 G1 T1 GX TX GH ->
stp2 G1 T1 G2 (TSel x) GH
(* X<T, one sided *)
| stp2_sela1: forall G1 G2 GX TL TU x T2 GH,
indexr x GH = Some (GX, (TMem TL TU)) ->
(* closed 0 x TX -> *)
stp2 GX TU G2 T2 GH ->
stp2 G1 (TSelH x) G2 T2 GH
| stp2_selax: forall G1 G2 GX TX x GH,
indexr x GH = Some (GX, TX) ->
indexr x GH = Some (GX, TX) ->
(*closed 0 x TX ->*)
stp2 G1 (TSelH x) G2 (TSelH x) GH
| stp2_all: forall G1 G2 T1 T2 T3 T4 GH,
stp2 G2 T3 G1 T1 GH ->
closed 1 (length GH) T2 -> (* must not accidentally bind x *)
closed 1 (length GH) T4 ->
stp2 G1 (open (TSelH (length GH)) T2) G2 (open (TSelH (length GH)) T4) ((0,(G2, T3))::GH) ->
stp2 G1 (TAll T1 T2) G2 (TAll T3 T4) GH
.
Inductive wf_env : venv -> tenv -> Prop :=
| wfe_nil : wf_env nil nil
| wfe_cons : forall n v t vs ts,
val_type ((n,v)::vs) v t ->
wf_env vs ts ->
wf_env (cons (n,v) vs) (cons (n,t) ts)
with val_type : venv -> vl -> ty -> Prop :=
| v_ty: forall env venv tenv T1 TE,
wf_env venv tenv -> (* T1 wf in tenv ? *)
stp2 venv (TMem T1 T1) env TE [] ->
val_type env (vty venv T1) TE
| v_bool: forall venv b TE,
stp2 [] TBool venv TE [] ->
val_type venv (vbool b) TE
| v_abs: forall env venv tenv f x y T1 T2 TE,
wf_env venv tenv ->
has_type ((x,T1)::(f,TFun T1 T2)::tenv) y T2 ->
fresh venv <= f ->
1 + f <= x ->
stp2 venv (TFun T1 T2) env TE [] ->
val_type env (vabs venv f x y) TE
| v_tabs: forall env venv tenv x y T1 T2 TE,
wf_env venv tenv ->
has_type ((x,T1)::tenv) y (open (TSel x) T2) ->
fresh venv = x ->
stp2 venv (TAll T1 T2) env TE [] ->
val_type env (vtabs venv x T1 y) TE
.
Inductive wf_envh : venv -> aenv -> tenv -> Prop :=
| wfeh_nil : forall vvs, wf_envh vvs nil nil
| wfeh_cons : forall n t vs vvs ts,
wf_envh vvs vs ts ->
wf_envh vvs (cons (n,(vvs,t)) vs) (cons (n,t) ts)
.
Inductive valh_type : venv -> aenv -> (venv*ty) -> ty -> Prop :=
| v_tya: forall aenv venv T1,
valh_type venv aenv (venv, T1) T1
.
(*
None means timeout
Some None means stuck
Some (Some v)) means result v
Could use do-notation to clean up syntax.
*)
Fixpoint teval(n: nat)(env: venv)(t: tm){struct n}: option (option vl) :=
match n with
| 0 => None
| S n =>
match t with
| ttrue => Some (Some (vbool true))
| tfalse => Some (Some (vbool false))
| tvar x => Some (index x env)
| tabs f x y => Some (Some (vabs env f x y))
| ttabs x T y => Some (Some (vtabs env x T y))
| tapp ef ex =>
match teval n env ex with
| None => None
| Some None => Some None
| Some (Some vx) =>
match teval n env ef with
| None => None
| Some None => Some None
| Some (Some (vbool _)) => Some None
| Some (Some (vty _ _)) => Some None
| Some (Some (vtya _ _)) => Some None
| Some (Some (vtabs _ _ _ _)) => Some None
| Some (Some (vabs env2 f x ey)) =>
teval n ((x,vx)::(f,vabs env2 f x ey)::env2) ey
end
end
| ttapp ef ex =>
match teval n env ef with
| None => None
| Some None => Some None
| Some (Some (vbool _)) => Some None
| Some (Some (vty _ _)) => Some None
| Some (Some (vtya _ _)) => Some None
| Some (Some (vabs _ _ _ _)) => Some None
| Some (Some (vtabs env2 x T ey)) =>
teval n ((x,vty env ex)::env2) ey
end
end
end.
Hint Constructors ty.
Hint Constructors tm.
Hint Constructors vl.
Hint Constructors closed_rec.
Hint Constructors has_type.
Hint Constructors val_type.
Hint Constructors wf_env.
Hint Constructors stp.
Hint Constructors stp2.
Hint Constructors option.
Hint Constructors list.
Hint Unfold index.
Hint Unfold length.
Hint Unfold closed.
Hint Unfold open.
Hint Resolve ex_intro.
(* ############################################################ *)
(* Examples *)
(* ############################################################ *)
(*
match goal with
| |- has_type _ (tvar _) _ =>
try solve [apply t_vara;
repeat (econstructor; eauto)]
| _ => idtac
end;
*)
Ltac crush_has_tp :=
try solve [eapply stp_selx; compute; eauto; crush_has_tp];
try solve [eapply stp_selax; compute; eauto; crush_has_tp];
try solve [eapply cl_selb; compute; eauto; crush_has_tp];
try solve [(econstructor; compute; eauto; crush_has_tp)].
Ltac crush2 :=
try solve [(eapply stp_selx; compute; eauto; crush2)];
try solve [(eapply stp_selax; compute; eauto; crush2)];
try solve [(eapply stp_sel1; compute; eauto; crush2)];
try solve [(eapply stp_sela1; compute; eauto; crush2)];
try solve [(eapply cl_selb; compute; eauto; crush2)];
try solve [(econstructor; compute; eauto; crush2)];
try solve [(eapply t_sub; eapply t_var; compute; eauto; crush2)].
(* define polymorphic identity function *)
Definition polyId := TAll (TMem TBot TTop) (TFun (TSelB 0) (TSelB 0)).
Example ex1: has_type [] (ttabs 0 (TMem TBot TTop) (tabs 1 2 (tvar 2))) polyId.
Proof.
crush2.
Qed.
(* instantiate it to bool *)
Example ex2: has_type [(0,polyId)] (ttapp (tvar 0) TBool) (TFun TBool TBool).
Proof.
eapply t_tapp. eapply t_sub. eapply t_var. simpl. eauto.
eapply stp_all. eauto. eauto.
crush_has_tp. crush_has_tp. crush_has_tp. compute.
eapply stp_all. crush2. crush2. crush2. crush2. crush2. crush2.
Qed.
(* define brand / unbrand client function *)
Definition brandUnbrand :=
TAll (TMem TBot TTop)
(TFun
(TFun TBool (TSelB 0)) (* brand *)
(TFun
(TFun (TSelB 0) TBool) (* unbrand *)
TBool)).
Example ex3:
has_type []
(ttabs 0 (TMem TBot TTop)
(tabs 1 2
(tabs 3 4
(tapp (tvar 4) (tapp (tvar 2) ttrue)))))
brandUnbrand.
Proof.
crush2.
Qed.
(* instantiating it at bool is admissible *)
Example ex4:
has_type [(1,TFun TBool TBool);(0,brandUnbrand)]
(tvar 0) (TAll (TMem TBool TBool) (TFun (TFun TBool TBool) (TFun (TFun TBool TBool) TBool))).
Proof.
eapply t_sub. crush2. crush2.
Qed.
Hint Resolve ex4.
(* apply it to identity functions *)
Example ex5:
has_type [(1,TFun TBool TBool);(0,brandUnbrand)]
(tapp (tapp (ttapp (tvar 0) TBool) (tvar 1)) (tvar 1)) TBool.
Proof.
crush2.
Qed.
(* ############################################################ *)
(* Proofs *)
(* ############################################################ *)
Lemma wf_fresh : forall vs ts,
wf_env vs ts ->
(fresh vs = fresh ts).
Proof.
intros. induction H. auto.
compute. eauto.
Qed.
Hint Immediate wf_fresh.
Lemma wfh_length : forall vvs vs ts,
wf_envh vvs vs ts ->
(length vs = length ts).
Proof.
intros. induction H. auto.
compute. eauto.
Qed.
Hint Immediate wf_fresh.
Lemma index_max : forall X vs n (T: X),
index n vs = Some T ->
n < fresh vs.
Proof.
intros X vs. induction vs.
- Case "nil". intros. inversion H.
- Case "cons".
intros. inversion H. destruct a.
case_eq (le_lt_dec (fresh vs) i); intros ? E1.
+ SCase "ok".
rewrite E1 in H1.
case_eq (beq_nat n i); intros E2.
* SSCase "hit".
eapply beq_nat_true in E2. subst n. compute. eauto.
* SSCase "miss".
rewrite E2 in H1.
assert (n < fresh vs). eapply IHvs. apply H1.
compute. omega.
+ SCase "bad".
rewrite E1 in H1. inversion H1.
Qed.
Lemma indexr_max : forall X vs n (T: X),
indexr 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 E2.
+ SSCase "hit".
eapply beq_nat_true in E2. subst n. compute. eauto.
+ SSCase "miss".
rewrite E2 in H1.
assert (n < length vs). eapply IHvs. apply H1.
compute. eauto.
Qed.
Lemma le_xx : forall a b,
a <= b ->
exists E, le_lt_dec a b = left E.
Proof. intros.
case_eq (le_lt_dec a b). intros. eauto.
intros. omega.
Qed.
Lemma le_yy : forall a b,
a > b ->
exists E, le_lt_dec a b = right E.
Proof. intros.
case_eq (le_lt_dec a b). intros. omega.
intros. eauto.
Qed.
Lemma index_extend : forall X vs n n' x (T: X),
index n vs = Some T ->
fresh vs <= n' ->
index n ((n',x)::vs) = Some T.
Proof.
intros.
assert (n < fresh vs). eapply index_max. eauto.
assert (n <> n'). omega.
assert (beq_nat n n' = false) as E. eapply beq_nat_false_iff; eauto.
assert (fresh vs <= n') as E2. omega.
elim (le_xx (fresh vs) n' E2). intros ? EX.
unfold index. unfold index in H. rewrite H. rewrite E. rewrite EX. reflexivity.
Qed.
Lemma indexr_extend : forall X vs n n' x (T: X),
indexr n vs = Some T ->
indexr n ((n',x)::vs) = Some T.
Proof.
intros.
assert (n < length vs). eapply indexr_max. eauto.
assert (beq_nat n (length vs) = false) as E. eapply beq_nat_false_iff. omega.
unfold indexr. unfold indexr in H. rewrite H. rewrite E. reflexivity.
Qed.
(* splicing -- for stp_extend. not finished *)
Fixpoint splice n (T : ty) {struct T} : ty :=
match T with
| TTop => TTop
| TBot => TBot
| TBool => TBool
| TMem T1 T2 => TMem (splice n T1) (splice n T2)
| TFun T1 T2 => TFun (splice n T1) (splice n T2)
| TSelB i => TSelB i
| TSel i => TSel i
| TSelH i => if le_lt_dec n i then TSelH (i+1) else TSelH i
| TAll T1 T2 => TAll (splice n T1) (splice n T2)
end.
Definition splicett n (V: (id*ty)) :=
match V with
| (x,T) => (x,(splice n T))
end.
Lemma splice_open_permute: forall (G0:tenv) T2 n j,
(open_rec j (TSelH (n + S (length G0))) (splice (length G0) T2)) =
(splice (length G0) (open_rec j (TSelH (n + length G0)) T2)).
Proof.
intros G T. induction T; intros; simpl; eauto;
try rewrite IHT1; try rewrite IHT2; try rewrite IHT; eauto.
case_eq (le_lt_dec (length G) i); intros E LE; simpl; eauto.
case_eq (beq_nat j i); intros E; simpl; eauto.
case_eq (le_lt_dec (length G) (n + length G)); intros EL LE.
assert (n + S (length G) = n + length G + 1). omega.
rewrite H. eauto.
omega.
Qed.
Lemma indexr_splice_hi: forall G0 G2 x0 x v1 T,
indexr x0 (G2 ++ G0) = Some T ->
length G0 <= x0 ->
indexr (x0 + 1) (map (splicett (length G0)) (G2 ++ (x, v1) :: G0)) = Some (splice (length G0) T).
Proof.
intros G0 G2. induction G2; intros.
- eapply indexr_max in H. simpl in H. omega.
- simpl in H. destruct a.
case_eq (beq_nat x0 (length (G2 ++ G0))); intros E.
+ rewrite E in H. inversion H. subst. simpl.
rewrite app_length in E.
rewrite map_length. rewrite app_length. simpl.
assert (beq_nat (x0 + 1) (length G2 + S (length G0)) = true). eapply beq_nat_true_iff. eapply beq_nat_true_iff in E. omega.
rewrite H1. eauto.
+ rewrite E in H. eapply IHG2 in H. eapply indexr_extend. eapply H. eauto.
Qed.
Lemma indexr_splice_lo0: forall G0 G2 x0 (T:ty),
indexr x0 (G2 ++ G0) = Some T ->
x0 < length G0 ->
indexr x0 G0 = Some T.
Proof. admit. Qed.
Lemma indexr_splice_lo1: forall G0 x0 (T:ty) f,
indexr x0 G0 = Some T ->
map (splicett f) G0 = G0 ->
indexr x0 G0 = Some (splice f T).
Proof. admit. Qed.
Lemma indexr_extend_mult: forall G0 G2 x0 (T:ty),
indexr x0 G0 = Some T ->
indexr x0 (G2++G0) = Some T.
Proof. admit. Qed.
Lemma indexr_splice_lo: forall G0 G2 x0 x v1 T f,
indexr x0 (G2 ++ G0) = Some T ->
map (splicett f) G0 = G0 ->
x0 < length G0 ->
indexr x0 (map (splicett f) (G2 ++ (x, v1) :: G0)) = Some (splice f T).
Proof.
intros.
assert (indexr x0 G0 = Some T). eapply indexr_splice_lo0; eauto.
assert (indexr x0 (map (splicett f) G0) = Some (splice f T)).
rewrite H0. eapply indexr_splice_lo1. eauto. eauto.
rewrite map_app. simpl.
eapply indexr_extend_mult. eapply indexr_extend. eauto.
Qed.
Lemma stp_splice : forall GX G0 G1 T1 T2 x v1,
stp GX (G1++G0) T1 T2 ->
map (splicett (length G0)) G0 = G0 ->
stp GX (map (splicett (length G0)) (G1++(x,v1)::G0)) (splice (length G0) T1) (splice (length G0) T2).
Proof.
intros GX G0 G1 T1 T2 x v1 H. remember (G1++G0) as G.
revert G0 G1 HeqG.
induction H; intros; subst GH; simpl; eauto.
- admit.
- admit.
- Case "sela1".
case_eq (le_lt_dec (length G0) x0); intros E LE.
+ eapply stp_sela1. eapply indexr_splice_hi with (T:=TMem TL TU). eauto. eauto.
eapply IHstp. eauto. eauto.
+ eapply stp_sela1. eapply indexr_splice_lo with (T:=TMem TL TU). eauto. eauto. eauto.
eapply IHstp. eauto. eauto.
- Case "sela2". admit.
- Case "selax".
case_eq (le_lt_dec (length G0) x0); intros E LE.
+ eapply stp_selax. eapply indexr_splice_hi with (T:=TMem TL TU). eauto. eauto.
+ eapply stp_selax. eapply indexr_splice_lo with (T:=TMem TL TU). eauto. eauto. eauto.
- Case "all".
eapply stp_all.
eapply IHstp1. eauto. eauto. eauto.
admit. (* closed *)
admit. (* closed *)
specialize IHstp2 with (G3:=G0) (G4:=(0, T3) :: G2).
simpl in IHstp2. rewrite map_length. rewrite app_length. simpl.
repeat rewrite splice_open_permute with (j:=0). subst x0.
rewrite app_length in IHstp2. simpl in IHstp2.
eapply IHstp2. eauto. eauto.
Qed.
Lemma stp_extend : forall G1 GH T1 T2 x v1,
stp G1 GH T1 T2 ->
stp G1 ((x,v1)::GH) T1 T2.
Proof.
(* use stp_splice: we need to have that env and types are well-formed *)
admit.
Qed.
Lemma stp2_extend2 : forall x v1 G1 G2 T1 T2 H,
stp2 G1 T1 G2 T2 H ->
fresh G2 <= x ->
stp2 G1 T1 ((x,v1)::G2) T2 H.
Proof. admit. Qed.
Lemma stp2_extend1 : forall x v1 G1 G2 T1 T2 H,
stp2 G1 T1 G2 T2 H ->
fresh G1 <= x ->
stp2 ((x,v1)::G1) T1 G2 T2 H.
Proof. admit. Qed.
Lemma stp2_extendH : forall x v1 G1 G2 T1 T2 H,
stp2 G1 T1 G2 T2 H ->
stp2 G1 T1 G2 T2 ((x,v1)::H).
Proof. admit. Qed.
Lemma stp2_extendH_mult : forall G1 G2 T1 T2 H H2,
stp2 G1 T1 G2 T2 H ->
stp2 G1 T1 G2 T2 (H2++H).
Proof. admit. Qed.
Lemma stp2_extendH_mult0 : forall G1 G2 T1 T2 H2,
stp2 G1 T1 G2 T2 [] ->
stp2 G1 T1 G2 T2 H2.
Proof. admit. Qed.
Lemma stp2_reg2 : forall G1 G2 T1 T2 H ,
stp2 G1 T1 G2 T2 H ->
stp2 G2 T2 G2 T2 H.
Proof. admit. Qed.
Lemma stp2_reg1 : forall G1 G2 T1 T2 H,
stp2 G1 T1 G2 T2 H ->
stp2 G1 T1 G1 T1 H.
Proof. admit. Qed.
Lemma stp2_closed2 : forall G1 G2 T1 T2 H ,
stp2 G1 T1 G2 T2 H ->
closed 0 (length H) T2.
Proof. admit. Qed.
Lemma stp2_closed1 : forall G1 G2 T1 T2 H,
stp2 G1 T1 G2 T2 H ->
closed 0 (length H) T1.
Proof. admit. Qed.
Lemma valtp_extend : forall vs v x v1 T,
val_type vs v T ->
fresh vs <= x ->
val_type ((x,v1)::vs) v T.
Proof.
intros. induction H; eauto; econstructor; eauto; eapply stp2_extend2; eauto.
Qed.
Lemma index_safe_ex: forall H1 G1 TF i,
wf_env H1 G1 ->
index i G1 = Some TF ->
exists v, index i H1 = Some v /\ val_type H1 v TF.
Proof. intros. induction H.
- Case "nil". inversion H0.
- Case "cons". inversion H0.
case_eq (le_lt_dec (fresh ts) n); intros ? E1.
+ SCase "ok".
rewrite E1 in H3.
assert ((fresh ts) <= n) as QF. eauto. rewrite <-(wf_fresh vs ts H1) in QF.
elim (le_xx (fresh vs) n QF). intros ? EX.
case_eq (beq_nat i n); intros E2.
* SSCase "hit".
assert (index i ((n, v) :: vs) = Some v). eauto. unfold index. rewrite EX. rewrite E2. eauto.
assert (t = TF).
unfold index in H0. rewrite E1 in H0. rewrite E2 in H0. inversion H0. eauto.
subst t. eauto.
* SSCase "miss".
rewrite E2 in H3.
assert (exists v0, index i vs = Some v0 /\ val_type vs v0 TF) as HI. eapply IHwf_env. eauto.
inversion HI as [v0 HI1]. inversion HI1.
eexists. econstructor. eapply index_extend; eauto. eapply valtp_extend; eauto.
+ SSCase "bad".
rewrite E1 in H3. inversion H3.
Qed.
Lemma index_safeh_ex: forall H1 H2 G1 GH TF i,
wf_env H1 G1 -> wf_envh H1 H2 GH ->
indexr i GH = Some TF ->
exists v, indexr i H2 = Some v /\ valh_type H1 H2 v TF.
Proof. intros. induction H0.
- Case "nil". inversion H3.
- Case "cons". inversion H3.
case_eq (beq_nat i (length ts)); intros E2.
* SSCase "hit".
rewrite E2 in H2. inversion H2. subst. clear H2.
assert (length ts = length vs). symmetry. eapply wfh_length. eauto.
simpl. rewrite H1 in E2. rewrite E2.
eexists. split. eauto. econstructor.
* SSCase "miss".
rewrite E2 in H2.
assert (exists v : venv * ty,
indexr i vs = Some v /\ valh_type vvs vs v TF). eauto.
destruct H1. destruct H1.
eexists. split. eapply indexr_extend. eauto.
inversion H4. subst.
eapply v_tya. (* aenv is not constrained -- bit of a cheat?*)
Qed.
Inductive res_type: venv -> option vl -> ty -> Prop :=
| not_stuck: forall venv v T,
val_type venv v T ->
res_type venv (Some v) T.
Hint Constructors res_type.
Hint Resolve not_stuck.
Hint Constructors stp2.
Lemma stp2_trans: forall G1 G2 G3 T1 T2 T3 H,
stp2 G1 T1 G2 T2 H ->
stp2 G2 T2 G3 T3 H ->
stp2 G1 T1 G3 T3 H.
Proof. admit. Qed.
(* used in trans -- need to generalize interface for induction *)
(*
Lemma stp2_narrow: forall x G1 G2 G3 G4 T1 T2 T3 T4 H,
stp2 G1 T1 G2 T2 H -> (* careful about H! *)
stp2 G3 T3 G4 T4 ((x,vtya G2 T2)::H) ->
stp2 G3 T3 G4 T4 ((x,vtya G1 T1)::H).
Proof. admit. Qed.
*)
Lemma index_miss {X}: forall x x1 (B:X) A G,
index x ((x1,B)::G) = A ->
fresh G <= x1 ->
x <> x1 ->
index x G = A.
Proof.
intros.
unfold index in H.
elim (le_xx (fresh G) x1 H0). intros.
rewrite H2 in H.
assert (beq_nat x x1 = false). eapply beq_nat_false_iff. eauto.
rewrite H3 in H. eapply H.
Qed.
Lemma index_hit {X}: forall x x1 (B:X) A G,
index x ((x1,B)::G) = Some A ->
fresh G <= x1 ->
x = x1 ->
B = A.
Proof.
intros.
unfold index in H.
elim (le_xx (fresh G) x1 H0). intros.
rewrite H2 in H.
assert (beq_nat x x1 = true). eapply beq_nat_true_iff. eauto.
rewrite H3 in H. inversion H. eauto.
Qed.
Lemma index_hit2 {X}: forall x x1 (B:X) A G,
fresh G <= x1 ->
x = x1 ->
B = A ->
index x ((x1,B)::G) = Some A.
Proof.
intros.
unfold index.
elim (le_xx (fresh G) x1 H). intros.
rewrite H2.
assert (beq_nat x x1 = true). eapply beq_nat_true_iff. eauto.
rewrite H3. rewrite H1. eauto.
Qed.
Lemma indexr_miss {X}: forall x x1 (B:X) A G,
indexr x ((x1,B)::G) = A ->
x <> (length G) ->
indexr x G = A.
Proof.
intros.
unfold indexr in H.
assert (beq_nat x (length G) = false). eapply beq_nat_false_iff. eauto.
rewrite H1 in H. eauto.
Qed.
Lemma indexr_hit {X}: forall x x1 (B:X) A G,
indexr x ((x1,B)::G) = Some A ->
x = length G ->
B = A.
Proof.
intros.
unfold indexr in H.
assert (beq_nat x (length G) = true). eapply beq_nat_true_iff. eauto.