forked from TiarkRompf/minidot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsubsup_total_rec.v
2618 lines (2272 loc) · 98 KB
/
dsubsup_total_rec.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
(* Termination for D<:> with intersection types and recursive self types *)
(* this version includes a proof of totality *)
(*
DSub (D<:) + Bot + /\ + { z => ... }
T ::= Top | Bot | x.Type | { Type: S..U } | (z: T) -> T^z | T1 /\ T2 | { z => T^z }
t ::= x | { Type = T } | lambda x:T.t | t t | unpack(t) { x => t }
*)
Require Export SfLib.
Require Export Arith.EqNat.
Require Export Arith.Le.
Require Import Coq.Program.Equality.
Require Import Omega.
(* ### Syntax ### *)
Definition id := nat.
(* term variables occurring in types *)
Inductive var : Type :=
| varF : id -> var (* free, in concrete environment *)
| varH : id -> var (* free, in abstract environment *)
| varB : id -> var (* locally-bound variable *)
.
Inductive ty : Type :=
| TTop : ty
| TBot : ty
(* (z: T) -> T^z *)
| TAll : ty -> ty -> ty
(* x.Type *)
| TSel : var -> ty
(* { Type: S..U } *)
| TMem : ty(*S*) -> ty(*U*) -> ty
| TBind : ty -> ty (* Recursive binder: { z => T^z },
where z is locally bound in T *)
| TAnd : ty -> ty -> ty (* Intersection Type: T1 /\ T2 *)
.
Inductive tm : Type :=
(* x -- free variable, matching concrete environment *)
| tvar : id -> tm
(* { Type = T } *)
| ttyp : ty -> tm
(* lambda x:T.t *)
| tabs : ty -> tm -> tm
(* t t *)
| tapp : tm -> tm -> tm
(* unpack(e) { x => ... } *)
| tunpack : tm -> tm -> tm
.
Inductive vl : Type :=
(* a closure for a lambda abstraction *)
| vabs : list vl (*H*) -> ty -> tm -> vl
(* a closure for a first-class type *)
| vty : list vl (*H*) -> ty -> vl
.
Definition tenv := list ty. (* Gamma environment: static *)
Definition venv := list vl. (* H environment: run-time *)
(* ### Representation of Bindings ### *)
(* An environment is a list of values, indexed by decrementing ids. *)
Fixpoint indexr {X : Type} (n : id) (l : list X) : option X :=
match l with
| [] => None
| a :: l' =>
if (beq_nat n (length l')) then Some a else indexr n l'
end.
Inductive closed: nat(*B*) -> nat(*H*) -> nat(*F*) -> ty -> Prop :=
| cl_top: forall i j k,
closed i j k TTop
| cl_bot: forall i j k,
closed i j k TBot
| cl_all: forall i j k T1 T2,
closed i j k T1 ->
closed (S i) j k T2 ->
closed i j k (TAll T1 T2)
| cl_sel: forall i j k x,
k > x ->
closed i j k (TSel (varF x))
| cl_selh: forall i j k x,
j > x ->
closed i j k (TSel (varH x))
| cl_selb: forall i j k x,
i > x ->
closed i j k (TSel (varB x))
| cl_mem: forall i j k T1 T2,
closed i j k T1 ->
closed i j k T2 ->
closed i j k (TMem T1 T2)
| cl_bind: forall i j k T,
closed (S i) j k T ->
closed i j k (TBind T)
| cl_and: forall i j k T1 T2,
closed i j k T1 ->
closed i j k T2 ->
closed i j k (TAnd T1 T2)
.
(* open define a locally-nameless encoding wrt to TVarB type variables. *)
(* substitute var u for all occurrences of (varB k) *)
Fixpoint open_rec (k: nat) (u: var) (T: ty) { struct T }: ty :=
match T with
| TTop => TTop
| TBot => TBot
| TAll T1 T2 => TAll (open_rec k u T1) (open_rec (S k) u T2)
| TSel (varF x) => TSel (varF x)
| TSel (varH i) => TSel (varH i)
| TSel (varB i) => if beq_nat k i then TSel u else TSel (varB i)
| TMem T1 T2 => TMem (open_rec k u T1) (open_rec k u T2)
| TBind T => TBind (open_rec (S k) u T)
| TAnd T1 T2 => TAnd (open_rec k u T1) (open_rec k u T2)
end.
Definition open u T := open_rec 0 u T.
(* Locally-nameless encoding with respect to varH variables. *)
Fixpoint subst (U : var) (T : ty) {struct T} : ty :=
match T with
| TTop => TTop
| TBot => TBot
| TAll T1 T2 => TAll (subst U T1) (subst U T2)
| TSel (varB i) => TSel (varB i)
| TSel (varF i) => TSel (varF i)
| TSel (varH i) => if beq_nat i 0 then TSel U else TSel (varH (i-1))
| TMem T1 T2 => TMem (subst U T1) (subst U T2)
| TBind T => TBind (subst U T)
| TAnd T1 T2 => TAnd (subst U T1)(subst U T2)
end.
Fixpoint nosubst (T : ty) {struct T} : Prop :=
match T with
| TTop => True
| TBot => True
| TAll T1 T2 => nosubst T1 /\ nosubst T2
| TSel (varB i) => True
| TSel (varF i) => True
| TSel (varH i) => i <> 0
| TMem T1 T2 => nosubst T1 /\ nosubst T2
| TBind T => nosubst T
| TAnd T1 T2 => nosubst T1 /\ nosubst T2
end.
(* ### Subtyping ### *)
(*
Note: In contrast to the rules on paper, the subtyping
relation has two environments instead of just one.
(The same holds for the semantic types, val_type, below).
This split into an abstract and a concrete environment
was necessary in the D<: soundness development, but is
not required here. We just keep it for consistency with
our earlier Coq files.
The first env is for looking up varF variables.
The first env matches the concrete runtime environment, and is
extended during type assignment.
The second env is for looking up varH variables.
The second env matches the abstract runtime environment, and is
extended during subtyping.
*)
Inductive stp: tenv -> tenv -> ty -> ty -> Prop :=
| stp_top: forall G1 GH T1,
closed 0 (length GH) (length G1) T1 ->
stp G1 GH T1 TTop
| stp_bot: forall G1 GH T2,
closed 0 (length GH) (length G1) T2 ->
stp G1 GH TBot T2
| stp_mem: forall G1 GH S1 U1 S2 U2,
stp G1 GH U1 U2 ->
stp G1 GH S2 S1 ->
stp G1 GH (TMem S1 U1) (TMem S2 U2)
| stp_sel1: forall G1 GH TX T2 x,
indexr x G1 = Some TX ->
closed 0 0 (length G1) TX ->
stp G1 GH TX (TMem TBot T2) ->
stp G1 GH (TSel (varF x)) T2
| stp_sel2: forall G1 GH TX T1 x,
indexr x G1 = Some TX ->
closed 0 0 (length G1) TX ->
stp G1 GH TX (TMem T1 TTop) ->
stp G1 GH T1 (TSel (varF x))
| stp_selx: forall G1 GH v x,
indexr x G1 = Some v ->
stp G1 GH (TSel (varF x)) (TSel (varF x))
| stp_sela1: forall G1 GH TX T2 x,
indexr x GH = Some TX ->
closed 0 x (length G1) TX ->
stp G1 GH TX (TMem TBot T2) ->
stp G1 GH (TSel (varH x)) T2
| stp_sela2: forall G1 GH TX T1 x,
indexr x GH = Some TX ->
closed 0 x (length G1) TX ->
stp G1 GH TX (TMem T1 TTop) ->
stp G1 GH T1 (TSel (varH x))
| stp_selax: forall G1 GH v x,
indexr x GH = Some v ->
stp G1 GH (TSel (varH x)) (TSel (varH x))
(* stp for recursive types and intersection types *)
| stp_bindx: forall GH G1 T1 T1' T2 T2',
stp G1 (T1'::GH) T1' T2' ->
T1' = (open (varH (length GH)) T1) ->
T2' = (open (varH (length GH)) T2) ->
closed 1 (length GH) (length G1) T1 ->
closed 1 (length GH) (length G1) T2 ->
stp G1 GH (TBind T1) (TBind T2)
| stp_and11: forall GH G1 T1 T2 T,
stp G1 GH T1 T ->
closed 0 (length GH) (length G1) T2 ->
stp G1 GH (TAnd T1 T2) T
| stp_and12: forall GH G1 T1 T2 T,
stp G1 GH T2 T ->
closed 0 (length GH) (length G1) T1 ->
stp G1 GH (TAnd T1 T2) T
| stp_and2: forall GH G1 T1 T2 T,
stp G1 GH T T1 ->
stp G1 GH T T2 ->
stp G1 GH T (TAnd T1 T2)
| stp_all: forall G1 GH T1 T2 T3 T4 x,
stp G1 GH T3 T1 ->
x = length GH ->
closed 1 (length GH) (length G1) T2 ->
closed 1 (length GH) (length G1) T4 ->
stp G1 (T3::GH) (open (varH x) T2) (open (varH x) T4) ->
stp G1 GH (TAll T1 T2) (TAll T3 T4)
| stp_trans: forall G1 GH T1 T2 T3,
stp G1 GH T1 T2 ->
stp G1 GH T2 T3 ->
stp G1 GH T1 T3
.
(* ### Type Assignment ### *)
Inductive has_type : tenv -> tm -> ty -> Prop :=
| t_var: forall x env T1,
indexr x env = Some T1 ->
stp env [] T1 T1 ->
has_type env (tvar x) T1
(* pack a recursive type *)
| t_var_pack : forall G1 x T1 T1',
(* has_type G1 (tvar x) T1' -> *)
indexr x G1 = Some (open (varF x) T1) ->
T1' = (open (varF x) T1) ->
closed 1 0 (length G1) T1 ->
has_type G1 (tvar x) (TBind T1)
(* unpack a recursive type: unpack(x:{z=>T^z}) { x:T^x => ... } *)
| t_unpack: forall env x y T1 T1' T2,
has_type env x (TBind T1) ->
T1' = (open (varF (length env)) T1) ->
has_type (T1'::env) y T2 ->
closed 0 0 (length env) T2 ->
has_type env (tunpack x y) T2
(* intersection typing *)
| t_and : forall env x T1 T2,
has_type env (tvar x) T1 ->
has_type env (tvar x) T2 ->
has_type env (tvar x) (TAnd T1 T2)
| t_typ: forall env T1,
closed 0 0 (length env) T1 ->
has_type env (ttyp T1) (TMem T1 T1)
| t_app: forall env f x T1 T2,
has_type env f (TAll T1 T2) ->
has_type env x T1 ->
closed 0 0 (length env) T2 ->
has_type env (tapp f x) T2
| t_dapp:forall env f x T1 T2 T,
has_type env f (TAll T1 T2) ->
has_type env (tvar x) T1 ->
T = open (varF x) T2 ->
closed 0 0 (length env) T ->
has_type env (tapp f (tvar x)) T
| t_abs: forall env y T1 T2,
has_type (T1::env) y (open (varF (length env)) T2) ->
closed 0 0 (length env) (TAll T1 T2) ->
has_type env (tabs 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
.
(* ### Evaluation (Big-Step Semantics) ### *)
(*
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
| tvar x => Some (indexr x env)
| ttyp T => Some (Some (vty env T))
| tabs T y => Some (Some (vabs env 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 (vty _ _)) => Some None
| Some (Some (vabs env2 _ ey)) =>
teval n (vx::env2) ey
end
end
| tunpack ex ey =>
match teval n env ex with
| None => None
| Some None => Some None
| Some (Some vx) =>
teval n (vx::env) ey
end
end
end.
Definition tevaln env e v := exists nm, forall n, n > nm -> teval n env e = Some (Some v).
(* ### Semantic Interpretation of Types (Logical Relations) ### *)
Fixpoint tsize_flat(T: ty) :=
match T with
| TTop => 1
| TBot => 1
| TAll T1 T2 => S (tsize_flat T1 + tsize_flat T2)
| TSel _ => 1
| TMem T1 T2 => S (tsize_flat T1 + tsize_flat T2)
| TBind T => S (tsize_flat T)
| TAnd T1 T2 => S (tsize_flat T1 + tsize_flat T2)
end.
Lemma open_preserves_size: forall T x j,
tsize_flat T = tsize_flat (open_rec j (varH x) T).
Proof.
intros T. induction T; intros; simpl; eauto.
- destruct v; simpl; destruct (beq_nat j i); eauto.
Qed.
(* NEW DESIGN IDEA:
The required invariants about runtime evaluation rely in crucial
ways on transporting properties from the creation site of
type objects to their use sites -- in particular the fact
that only type aliases can be created (TMem T T), and that these
cannot be recursive.
This suggests that in the proof, we should pair each (vty T) value
with the semantic interpretation of the type member [[ T ]].
So [[ T ]] in general is no longer a set of values, but a set of
(vl, vset) pairs. This leads to some complication as the type vset
is now recursive
Definition vset := vset -> vl -> Prop.
which Coq wouldn't let us do (for good reasons).
But we can do some close with an indexed variant such that
vset (S n) := vset n -> vl -> Prop
and quantify over n to denote all finite ones.
As it turns out, we no longer need the previuos l/u bound selectors,
and the TMem case can ensure that the *actual* type member of an
object is inbetween the given bounds. This enables the case for
intersection types.
*)
Fixpoint vset n := match n with
| 0 => vl -> Prop
| S n => vset n -> vl -> Prop
end.
Definition vseta := forall n, vset n.
(* this is just a helper for pattern matching *)
Inductive vset_match : nat -> Type :=
| vsmatch: forall n, vset n -> vset_match n
.
Require Coq.Program.Wf.
Program Fixpoint val_type (env: list vseta) (GH:list vseta) (T:ty) n (dd: vset n) (v:vl) {measure (tsize_flat T)}: Prop :=
match v,T with
| vabs env1 T0 y, TAll T1 T2 =>
closed 0 (length GH) (length env) T1 /\ closed 1 (length GH) (length env) T2 /\
forall jj vx,
(forall kx, val_type env GH T1 kx (jj kx) vx) ->
exists (jj2:vseta) v, tevaln (vx::env1) y v /\ (forall k, val_type env (jj::GH) (open (varH (length GH)) T2) k (jj2 k) v)
| vty env1 TX, TMem T1 T2 =>
closed 0 (length GH) (length env) T1 /\ closed 0 (length GH) (length env) T2 /\
match (vsmatch n dd) with
| vsmatch 0 dd => True
| vsmatch (S n0) dd => forall (dy:vseta) vy,
(val_type env GH T1 n0 (dy n0) vy -> dd (dy n0) vy) /\
(dd (dy n0) vy -> val_type env GH T2 n0 (dy n0) vy)
end
| _, TSel (varF x) =>
match indexr x env with
| Some jj => jj (S n) dd v
| _ => False
end
| _, TSel (varH x) =>
match indexr x GH with
| Some jj => jj (S n) dd v
| _ => False
end
| _, TAnd T1 T2 =>
val_type env GH T1 n dd v /\ val_type env GH T2 n dd v
| _, TBind T1 =>
closed 1 (length GH) (length env) T1 /\
exists jj:vseta, jj n = dd /\ forall n, val_type env (jj::GH) (open (varH (length GH)) T1) n (jj n) v
| _, TTop =>
True
| _,_ =>
False
end.
Next Obligation. simpl. omega. Qed.
Next Obligation. simpl. unfold open. rewrite <-open_preserves_size. omega. Qed. (* TApp case: open *)
Next Obligation. simpl. omega. Qed.
Next Obligation. simpl. omega. Qed.
Next Obligation. simpl. omega. Qed.
Next Obligation. simpl. omega. Qed.
Next Obligation. simpl. unfold open. rewrite <-open_preserves_size. omega. Qed. (* TBind case: open *)
Ltac ev := repeat match goal with
| H: exists _, _ |- _ => destruct H
| H: _ /\ _ |- _ => destruct H
end.
Ltac inv_mem := match goal with
| H: closed 0 (length ?GH) (length ?G) (TMem ?T1 ?T2) |-
closed 0 (length ?GH) (length ?G) ?T2 => inversion H; subst; eauto
| H: closed 0 (length ?GH) (length ?G) (TMem ?T1 ?T2) |-
closed 0 (length ?GH) (length ?G) ?T1 => inversion H; subst; eauto
end.
Next Obligation. compute. repeat split; intros; ev; try solve by inversion. Qed.
Next Obligation. compute. repeat split; intros; ev; try solve by inversion. Qed.
Next Obligation. compute. repeat split; intros; ev; try solve by inversion. Qed.
Next Obligation. compute. repeat split; intros; ev; try solve by inversion. Qed.
Next Obligation. compute. repeat split; intros; ev; try solve by inversion. Qed.
Next Obligation. compute. repeat split; intros; ev; try solve by inversion. Qed.
(*
The expansion of val_type, val_type_func is incomprehensible.
We cannot (easily) unfold and reason about it. Therefore, we prove unfolding of
val_type to its body as a lemma.
(Note that the unfold_sub tactic relies on
functional extensionality)
*)
Import Coq.Program.Wf.
Import WfExtensionality.
Lemma val_type_unfold: forall env GH T n dd v, val_type env GH T n dd v =
match v,T with
| vabs env1 T0 y, TAll T1 T2 =>
closed 0 (length GH) (length env) T1 /\ closed 1 (length GH) (length env) T2 /\
forall jj vx,
(forall kx, val_type env GH T1 kx (jj kx) vx) ->
exists (jj2:vseta) v, tevaln (vx::env1) y v /\ (forall k, val_type env (jj::GH) (open (varH (length GH)) T2) k (jj2 k) v)
| vty env1 TX, TMem T1 T2 =>
closed 0 (length GH) (length env) T1 /\ closed 0 (length GH) (length env) T2 /\
match (vsmatch n dd) with
| vsmatch 0 dd => True
| vsmatch (S n0) dd => forall (dy:vseta) vy,
(val_type env GH T1 n0 (dy n0) vy -> dd (dy n0) vy) /\
(dd (dy n0) vy -> val_type env GH T2 n0 (dy n0) vy)
end
| _, TSel (varF x) =>
match indexr x env with
| Some jj => jj (S n) dd v
| _ => False
end
| _, TSel (varH x) =>
match indexr x GH with
| Some jj => jj (S n) dd v
| _ => False
end
| _, TAnd T1 T2 =>
val_type env GH T1 n dd v /\ val_type env GH T2 n dd v
| _, TBind T1 =>
closed 1 (length GH) (length env) T1 /\
exists jj:vseta, jj n = dd /\ forall n, val_type env (jj::GH) (open (varH (length GH)) T1) n (jj n) v
| _, TTop =>
True
| _,_ =>
False
end.
Proof. (*
intros. unfold val_type at 1. unfold val_type_func.
unfold_sub val_type (val_type env GH T n dd v).
simpl.
...
We admit this lemma here for performance reasons. The invocations
of unfold_sub. simpl. above can take Coq an hour or more to
complete (for reasons that are not clear).
The right-hand side of val_type_unfold has been copied and pasted
literally from val_type, so there is no question about the
validity of the lemma. *)
Admitted.
(* this is just to accelerate Coq -- val_type in the goal is slooow *)
Inductive vtp: list vseta -> list vseta -> ty -> forall n, vset n -> vl -> Prop :=
| vv: forall G H T n dd v, val_type G H T n dd v -> vtp G H T n dd v.
Lemma unvv: forall G H T n dd v,
vtp G H T n dd v -> val_type G H T n dd v.
Proof.
Require Import Coq.Logic.Eqdep_dec.
Require Import Coq.Arith.Peano_dec.
intros. inversion H0. apply inj_pair2_eq_dec in H2. subst. assumption.
apply eq_nat_dec.
Qed.
(* some quick examples *)
Example ex0 : forall n dd v, vtp [] [] (TTop) n dd v.
Proof.
intros. eapply vv. rewrite val_type_unfold. destruct v; auto.
Qed.
Example ex1: forall G1 GH T n, exists (dd:vset (S n)), forall d v, val_type G1 GH T n d v <-> dd d v.
Proof.
intros. remember (vtp G1 GH T n) as V.
simpl.
exists (fun d v => val_type G1 GH T n d v). intros.
split; intros; assumption.
Qed.
Example ex3: forall H T n d, vtp [] [] (TMem TBot TTop) n d (vty H T).
Proof.
intros. eapply vv. rewrite val_type_unfold.
split. constructor.
split. constructor.
destruct n. trivial.
intros. split. intros. rewrite val_type_unfold in H0. destruct vy; inversion H0.
intros. rewrite val_type_unfold. destruct vy; trivial.
Qed.
(* This lemma establishes that val_type indeed defines a value set (vseta).
We need this result in the t_typ/TMem case in the main proof,
to establish a vseta equivalent to [[ T1 ]] that can be passed
to [[ TMem T1 T1 ]].
*)
Example valtp_to_vseta: forall G1 GH T, exists (dd:vseta),
forall n d v, val_type G1 GH T n d v <-> dd (S n) d v.
Proof.
intros. remember (vtp G1 GH T) as V.
simpl.
exists (fun n => match n return vset n with
| 0 => fun v => True
| S n0 => (fun d v => val_type G1 GH T n0 d v)
end).
intros.
split; intros; assumption.
Qed.
(* consistent environment *)
Definition R_env venv genv tenv :=
length venv = length tenv /\
length genv = length tenv /\
forall x TX, indexr x tenv = Some TX ->
(exists (jj:vseta) vx,
indexr x venv = Some vx /\
indexr x genv = Some jj /\
forall n, vtp genv [] TX n (jj n) vx).
(* automation *)
Hint Unfold venv.
Hint Unfold tenv.
Hint Unfold open.
Hint Unfold indexr.
Hint Unfold length.
(* Hint Unfold R. *)
Hint Unfold R_env.
Hint Constructors ty.
Hint Constructors tm.
Hint Constructors vl.
Hint Constructors closed.
Hint Constructors has_type.
Hint Constructors stp.
Hint Constructors option.
Hint Constructors list.
Hint Resolve ex_intro.
(* ############################################################ *)
(* Examples *)
(* ############################################################ *)
Ltac crush :=
try solve [eapply stp_selx; compute; eauto; crush];
try solve [eapply stp_selax; compute; eauto; crush];
try solve [econstructor; compute; eauto; crush];
try solve [eapply t_sub; crush].
(* define polymorphic identity function *)
Definition polyId := TAll (TMem TBot TTop) (TAll (TSel (varB 0)) (TSel (varB 1))).
Example ex10: has_type [] (tabs (TMem TBot TTop) (tabs (TSel (varF 0)) (tvar 1))) polyId.
Proof.
crush.
Qed.
(*
(* instantiate it to TTop *)
Example ex20: has_type [polyId] (tapp (tvar 0) (ttyp TTop)) (TAll TTop TTop).
Proof.
crush.
Qed.
*)
(* ############################################################ *)
(* Proofs *)
(* ############################################################ *)
(* ## Extension, Regularity ## *)
Lemma wf_length : forall vs gs ts,
R_env vs gs ts ->
(length vs = length ts).
Proof.
intros. induction H. auto.
Qed.
Lemma wf_length2 : forall vs gs ts,
R_env vs gs ts ->
(length gs = length ts).
Proof.
intros. destruct H. destruct H0. auto.
Qed.
Hint Immediate wf_length.
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.
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 indexr_extend : forall X vs n x (T: X),
indexr n vs = Some T ->
indexr 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. *)
Fixpoint splice n (T : ty) {struct T} : ty :=
match T with
| TTop => TTop
| TBot => TBot
| TAll T1 T2 => TAll (splice n T1) (splice n T2)
| TSel (varF i) => TSel (varF i)
| TSel (varB i) => TSel (varB i)
| TSel (varH i) => if le_lt_dec n i then TSel (varH (i+1)) else TSel (varH i)
| TMem T1 T2 => TMem (splice n T1) (splice n T2)
| TBind T => TBind (splice n T)
| TAnd T1 T2 => TAnd (splice n T1) (splice n T2)
end.
Definition spliceat n (V: (venv*ty)) :=
match V with
| (G,T) => (G,splice n T)
end.
Lemma splice_open_permute: forall {X} (G0:list X) T2 n j,
(open_rec j (varH (n + S (length G0))) (splice (length G0) T2)) =
(splice (length G0) (open_rec j (varH (n + length G0)) T2)).
Proof.
intros X G T. induction T; intros; simpl; eauto;
try rewrite IHT1; try rewrite IHT2; try rewrite IHT; eauto;
destruct v; eauto.
case_eq (le_lt_dec (length G) i); intros E LE; simpl; eauto.
rewrite LE. eauto.
rewrite LE. eauto.
case_eq (beq_nat j i); intros E; simpl; eauto.
case_eq (le_lt_dec (length G) (n + length G)); intros EL LE.
rewrite E.
assert (n + S (length G) = n + length G + 1). omega.
rewrite H. eauto.
omega.
rewrite E. eauto.
Qed.
Lemma indexr_splice_hi: forall G0 G2 x0 v1 T,
indexr x0 (G2 ++ G0) = Some T ->
length G0 <= x0 ->
indexr (x0 + 1) (map (splice (length G0)) G2 ++ 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.
case_eq (beq_nat x0 (length (G2 ++ G0))); intros E.
+ rewrite E in H. inversion H. subst. simpl.
rewrite app_length in E.
rewrite app_length. rewrite map_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_spliceat_hi: forall G0 G2 x0 v1 G T,
indexr x0 (G2 ++ G0) = Some (G, T) ->
length G0 <= x0 ->
indexr (x0 + 1) (map (spliceat (length G0)) G2 ++ v1 :: G0) =
Some (G, 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 app_length. rewrite map_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 plus_lt_contra: forall a b,
a + b < b -> False.
Proof.
intros a b H. induction a.
- simpl in H. apply lt_irrefl in H. assumption.
- simpl in H. apply IHa. omega.
Qed.
Lemma indexr_splice_lo0: forall {X} G0 G2 x0 (T:X),
indexr x0 (G2 ++ G0) = Some T ->
x0 < length G0 ->
indexr x0 G0 = Some T.
Proof.
intros X G0 G2. induction G2; intros.
- simpl in H. apply H.
- simpl in H.
case_eq (beq_nat x0 (length (G2 ++ G0))); intros E.
+ eapply beq_nat_true_iff in E. subst.
rewrite app_length in H0. apply plus_lt_contra in H0. inversion H0.
+ rewrite E in H. apply IHG2. apply H. apply H0.
Qed.
Lemma indexr_extend_mult: forall {X} G0 G2 x0 (T:X),
indexr x0 G0 = Some T ->
indexr x0 (G2++G0) = Some T.
Proof.
intros X G0 G2. induction G2; intros.
- simpl. assumption.
- simpl.
case_eq (beq_nat x0 (length (G2 ++ G0))); intros E.
+ eapply beq_nat_true_iff in E.
apply indexr_max in H. subst.
rewrite app_length in H. apply plus_lt_contra in H. inversion H.
+ apply IHG2. assumption.
Qed.
Lemma indexr_splice_lo: forall G0 G2 x0 v1 T f,
indexr x0 (G2 ++ G0) = Some T ->
x0 < length G0 ->
indexr x0 (map (splice f) G2 ++ v1 :: G0) = Some T.
Proof.
intros.
assert (indexr x0 G0 = Some T). eapply indexr_splice_lo0; eauto.
eapply indexr_extend_mult. eapply indexr_extend. eauto.
Qed.
Lemma indexr_spliceat_lo: forall G0 G2 x0 v1 G T f,
indexr x0 (G2 ++ G0) = Some (G, T) ->
x0 < length G0 ->
indexr x0 (map (spliceat f) G2 ++ v1 :: G0) = Some (G, T).
Proof.
intros.
assert (indexr x0 G0 = Some (G, T)). eapply indexr_splice_lo0; eauto.
eapply indexr_extend_mult. eapply indexr_extend. eauto.
Qed.
Lemma closed_splice: forall i j k T n,
closed i j k T ->
closed i (S j) k (splice n T).
Proof.
intros. induction H; simpl; eauto.
case_eq (le_lt_dec n x); intros E LE.
apply cl_selh. omega.
apply cl_selh. omega.
Qed.
Lemma map_splice_length_inc: forall G0 G2 v1,
(length (map (splice (length G0)) G2 ++ v1 :: G0)) = (S (length (G2 ++ G0))).
Proof.
intros. rewrite app_length. rewrite map_length. induction G2.
- simpl. reflexivity.
- simpl. eauto.
Qed.
Lemma map_spliceat_length_inc: forall G0 G2 v1,
(length (map (spliceat (length G0)) G2 ++ v1 :: G0)) = (S (length (G2 ++ G0))).
Proof.
intros. rewrite app_length. rewrite map_length. induction G2.
- simpl. reflexivity.
- simpl. eauto.
Qed.
Lemma closed_inc_mult: forall i j k T,
closed i j k T ->
forall i' j' k',
i' >= i -> j' >= j -> k' >= k ->
closed i' j' k' T.
Proof.
intros i j k T H. induction H; intros; eauto; try solve [constructor; omega].
- apply cl_all. apply IHclosed1; omega. apply IHclosed2; omega.
- constructor. apply IHclosed; omega.
Qed.
Lemma closed_inc: forall i j k T,
closed i j k T ->
closed i (S j) k T.
Proof.
intros. apply (closed_inc_mult i j k T H i (S j) k); omega.
Qed.
Lemma closed_splice_idem: forall i j k T n,
closed i j k T ->
n >= j ->
splice n T = T.
Proof.
intros. induction H; eauto.
- (* TAll *) simpl.
rewrite IHclosed1. rewrite IHclosed2.
reflexivity.
assumption. assumption.
- (* TVarH *) simpl.
case_eq (le_lt_dec n x); intros E LE. omega. reflexivity.
- (* TMem *) simpl.
rewrite IHclosed1. rewrite IHclosed2.
reflexivity.
assumption. assumption.
- simpl. rewrite IHclosed. reflexivity. assumption.
- simpl. rewrite IHclosed1. rewrite IHclosed2. reflexivity. assumption. assumption.
Qed.
Lemma stp_closed : forall G GH T1 T2,
stp G GH T1 T2 ->
closed 0 (length GH) (length G) T1 /\ closed 0 (length GH) (length G) T2.
Proof.
intros. induction H;
try solve [repeat ev; split; try inv_mem; eauto using indexr_max].
Qed.
Lemma stp_closed2 : forall G1 GH T1 T2,
stp G1 GH T1 T2 ->
closed 0 (length GH) (length G1) T2.
Proof.
intros. apply (proj2 (stp_closed G1 GH T1 T2 H)).
Qed.
Lemma stp_closed1 : forall G1 GH T1 T2,
stp G1 GH T1 T2 ->
closed 0 (length GH) (length G1) T1.
Proof.
intros. apply (proj1 (stp_closed G1 GH T1 T2 H)).
Qed.
Lemma closed_upgrade: forall i j k i' T,
closed i j k T ->
i' >= i ->
closed i' j k T.
Proof.
intros. apply (closed_inc_mult i j k T H i' j k); omega.
Qed.
Lemma closed_upgrade_free: forall i j k j' T,
closed i j k T ->
j' >= j ->
closed i j' k T.
Proof.
intros. apply (closed_inc_mult i j k T H i j' k); omega.
Qed.
Lemma closed_upgrade_freef: forall i j k k' T,
closed i j k T ->
k' >= k ->
closed i j k' T.
Proof.
intros. apply (closed_inc_mult i j k T H i j k'); omega.
Qed.
Lemma closed_open: forall i j k V T, closed (i+1) j k T -> closed i j k (TSel V) ->
closed i j k (open_rec i V T).
Proof.
intros. generalize dependent i.
induction T; intros; inversion H;