-
Notifications
You must be signed in to change notification settings - Fork 11
/
dot2.v
1573 lines (1237 loc) · 44.6 KB
/
dot2.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.
(*
type safety for minidot-like calculus:
- branding / undbranding example
- static / dynamic stp relation
- no self types at the moment
*)
(* syntax *)
Module DOT.
Definition id := nat.
Inductive ty : Type :=
| TTop : ty
| TBool : ty
| TAnd : ty -> ty -> ty
| TFun : id -> ty -> ty -> ty
| TMem : (option ty) -> ty
| TSel : id -> ty
.
Definition TArrow p x y := TAnd (TMem p) (TAnd (TFun 0 x y) TTop).
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 *)
.
Inductive vl : Type :=
| vbool : bool -> vl
| vabs : list (id*vl) -> id -> ty -> list (id * dc) -> vl. (* clos env f:T = x => y *)
Fixpoint length {X: Type} (l : list X): nat :=
match l with
| [] => 0
| _::l' => 1 + length l'
end.
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.
Definition env := list (nat*vl).
Definition tenv := list (nat*ty).
Fixpoint dc_type_and (dcs: list(nat*dc)) :=
match dcs with
| nil => TTop
| (n, dfun T1 T2 _ _)::dcs =>
TAnd (TFun (length dcs) T1 T2) (dc_type_and dcs)
end.
Definition TObj p dcs := TAnd (TMem p) (dc_type_and dcs).
(* get the canonical type and internal env from an
object in a runtime environment *)
Definition resolve e n: option (env * ty) :=
match (index n e) with
| Some(v) =>
match v with
| vabs GC f TC dcs =>
Some ((f,v)::GC,(TObj (Some TC) dcs))
| vbool b => Some (nil,TBool)
end
| _ => None
end.
(* static type expansion.
needs to imply dynamic subtyping. *)
Inductive tresolve: ty -> ty -> Prop :=
| tr_self: forall T,
tresolve T T
| tr_and1: forall T1 T2 T,
tresolve T1 T ->
tresolve (TAnd T1 T2) T
| tr_and2: forall T1 T2 T,
tresolve T2 T ->
tresolve (TAnd T1 T2) T
.
Tactic Notation "tresolve_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "Self" |
Case_aux c "And1" |
Case_aux c "And2" ].
(* static type well-formedness.
needs to imply dynamic subtyping. *)
Inductive wf_type : tenv -> ty -> Prop :=
| wf_top: forall env,
wf_type env TTop
| 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_mema: forall env,
wf_type env (TMem None)
| wf_mem: forall env TA,
wf_type env TA ->
wf_type env (TMem (Some TA))
| 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 TA,
index x envz = Some (TE) ->
tresolve TE (TMem TA) ->
wf_type envz (TSel x)
.
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" ].
(* static subtyping: during type checking/assignment.
needs to imply dynamic subtyping *)
Inductive atp: tenv -> ty -> ty -> Prop :=
| atp_sel2: forall x env T TF,
index x env = Some TF ->
tresolve TF (TMem (Some T)) ->
atp env T (TSel x)
| atp_sel1: forall x env T TF,
index x env = Some TF ->
tresolve TF (TMem (Some T)) ->
atp env (TSel x) T
.
Tactic Notation "atp_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "? < Sel" | Case_aux c "Sel < ?" ].
(* impossible subtyping cases, uses for contradictions *)
Inductive nostp: ty -> ty -> Prop :=
| nostp_top_fun: forall m T1 T2,
nostp TTop (TFun m T1 T2)
| nostp_top_mem: forall TA,
nostp TTop (TMem TA)
| nostp_fun: forall T1 T2 T3 T4 n1 n2,
not (n1 = n2) ->
nostp (TFun n1 T1 T2) (TFun n2 T3 T4)
| nostp_fun_mem: forall m TA T1 T2,
nostp (TMem TA) (TFun m T1 T2)
| nostp_mem_fun: forall m TA T1 T2,
nostp (TFun m T1 T2) (TMem TA)
| nostp_and: forall T1 T2 T,
nostp T1 T ->
nostp T2 T ->
nostp (TAnd T1 T2) T
.
Hint Constructors nostp.
(* dynamic subtyping: during execution *)
Inductive stp : nat -> env -> ty -> env -> ty -> Prop :=
| stp_top: forall n1 G1 G2,
stp n1 G1 TTop G2 TTop (* don't want to deal with it now *)
| stp_bool: forall n1 G1 G2,
stp n1 G1 TBool G2 TBool
| stp_fun: forall n1 n2 m G1 G2 T11 T12 T21 T22,
stp n1 G2 T21 G1 T11 ->
stp n2 G1 T12 G2 T22 ->
stp (S (n1+n2)) G1 (TFun m T11 T12) G2 (TFun m T21 T22)
| stp_mem_ss: forall n1 n2 G1 G2 TA1 TA2,
stp n1 G1 TA1 G2 TA2 ->
stp n2 G2 TA2 G1 TA1 ->
stp (S (n1+n2)) G1 (TMem (Some TA1)) G2 (TMem (Some TA2))
| stp_mema_sn: forall n1 G1 G2 TA,
stp n1 G1 TA G1 TA -> (* regularity *)
stp (S (n1+n1)) G1 (TMem (Some TA)) G2 (TMem None)
| stp_mema_nn: forall n1 G1 G2,
stp n1 G1 (TMem None) G2 (TMem None)
| stp_and11: forall n1 n2 G1 G2 T1 T2 T,
stp n1 G1 T1 G2 T ->
stp n2 G1 T2 G1 T2 -> (* regularity *)
stp (S (n1+n2)) G1 (TAnd T1 T2) G2 T
| stp_and12: forall n1 n2 G1 G2 T1 T2 T,
stp n1 G1 T2 G2 T ->
stp n2 G1 T1 G1 T1 -> (* regularity *)
stp (S (n1+n2)) G1 (TAnd T1 T2) G2 T
| stp_and2: forall n1 n2 G1 G2 T1 T2 T,
stp n1 G1 T G2 T1 ->
stp n2 G1 T G2 T2 ->
stp (S (n1+n2)) G1 T G2 (TAnd T1 T2)
| stp_sel2: forall n2 f x dcs T1 TA G1 G2 GC,
index x G2 = Some (vabs GC f TA dcs) ->
stp n2 G1 T1 ((f,vabs GC f TA dcs)::GC) TA ->
stp (S n2) G1 T1 G2 (TSel x)
| stp_sel1: forall n2 f x dcs TA T2 G1 G2 GC,
index x G1 = Some (vabs GC f TA dcs) ->
stp n2 ((f,vabs GC f TA dcs)::GC) TA G2 T2 ->
stp (S n2) G1 (TSel x) G2 T2
| stp_selx: forall n1 x1 x2 v G1 G2,
(* resolve G1 x = Some (GC,TC) -> *)
(* don't need TC? but shouldn't we know it's a closure? *)
index x1 G1 = Some v ->
index x2 G2 = Some v ->
stp n1 G1 (TSel x1) G2 (TSel x2)
.
Tactic Notation "stp_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "Top < Top" |
Case_aux c "Bool < Bool" |
Case_aux c "Fun < Fun" |
Case_aux c "Mem Some < Mem Some" |
Case_aux c "Mem Some < Mem None" |
Case_aux c "Mem None < Mem None" |
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" ].
Definition stpd G1 T1 G2 T2 := exists n, stp n G1 T1 G2 T2.
(* INVERSION CASES *)
Lemma stp_mem_invA: forall G1 G2 TA1 TA2,
stpd G1 (TMem (Some TA1)) G2 (TMem (Some TA2)) ->
stpd G1 TA1 G2 TA2.
Proof. intros. destruct H. inversion H. eexists. eauto. Qed.
Lemma stp_mem_invB: forall G1 G2 TA1 TA2,
stpd G1 (TMem (Some TA1)) G2 (TMem (Some TA2)) ->
stpd G2 TA2 G1 TA1.
Proof. intros. destruct H. inversion H. eexists. eauto. Qed.
Lemma stp_funA: forall m G1 G2 T11 T12 T21 T22,
stpd G1 (TFun m T11 T12) G2 (TFun m T21 T22) ->
stpd G2 T21 G1 T11.
Proof. intros. destruct H. inversion H. eexists. eauto. Qed.
Lemma stp_funB: forall m G1 G2 T11 T12 T21 T22,
stpd G1 (TFun m T11 T12) G2 (TFun m T21 T22) ->
stpd G1 T12 G2 T22.
Proof. intros. destruct H. inversion H. eexists. eauto. Qed.
(* invert `and` if one branch is impossible *)
Lemma nostp_no_rhs_and: forall T1 T2 T,
nostp T (TAnd T1 T2) ->
False.
Proof. intros. remember (TAnd T1 T2). induction H; inversion Heqt.
eauto.
Qed.
Lemma nostp_no_rhs_sel: forall T x,
nostp T (TSel x) ->
False.
Proof. intros. remember (TSel x). induction H; inversion Heqt.
eauto.
Qed.
Hint Resolve ex_intro.
Lemma stp_contra: forall T1 T2 G1 G2,
nostp T1 T2 ->
stpd G1 T1 G2 T2 ->
False.
Proof. intros. induction H; destruct H0 as [n H0]; inversion H0; subst; eauto.
eapply IHnostp1. eexists. eauto.
eapply IHnostp2. eexists. eauto.
eapply nostp_no_rhs_and. eauto.
eapply nostp_no_rhs_sel. eauto.
Qed.
Lemma stp_andA: forall G1 G2 T1 T2 T,
stpd G1 (TAnd T1 T2) G2 T ->
nostp T2 T ->
stpd G1 T1 G2 T.
Proof. intros. destruct H. inversion H.
subst. eexists. eauto.
eapply stp_contra in H0. contradiction. exists n1. eauto.
subst. eapply nostp_no_rhs_and in H0. contradiction.
subst. eapply nostp_no_rhs_sel in H0. contradiction.
Qed.
Lemma stp_andB: forall G1 G2 T1 T2 T,
stpd G1 (TAnd T1 T2) G2 T ->
nostp T1 T ->
stpd G1 T2 G2 T.
Proof. intros. destruct H. inversion H.
eapply stp_contra in H0. contradiction. exists n1. eauto.
subst. eexists. eauto.
subst. eapply nostp_no_rhs_and in H0. contradiction.
subst. eapply nostp_no_rhs_sel in H0. contradiction.
Qed.
Lemma stp_and2A: forall G1 G2 T1 T2 T,
stpd G1 T G2 (TAnd T1 T2) ->
stpd G1 T G2 T1.
Proof. intros. remember (TAnd T1 T2). destruct H. induction H; inversion Heqt.
eapply IHstp1 in H1. destruct H1.
eexists. eapply stp_and11. eauto. eauto.
eapply IHstp1 in H1. destruct H1.
eexists. eapply stp_and12. eauto. eauto.
subst. eexists. eauto.
eapply IHstp in H1. destruct H1.
subst. eexists. eapply stp_sel1. eauto. eauto.
Qed.
Lemma stp_and2B: forall G1 G2 T1 T2 T,
stpd G1 T G2 (TAnd T1 T2) ->
stpd G1 T G2 T2.
Proof. intros. remember (TAnd T1 T2). destruct H. induction H; inversion Heqt.
eapply IHstp1 in H1. destruct H1.
eexists. eapply stp_and11. eauto. eauto.
eapply IHstp1 in H1. destruct H1.
eexists. eapply stp_and12. eauto. eauto.
subst. eexists. eauto.
eapply IHstp in H1. destruct H1.
subst. eexists. eapply stp_sel1. eauto. eauto.
Qed.
(* EXTENSION *)
Hint Constructors stp.
Lemma index_extend : forall X n G1 x v (T:X),
index n G1 = Some T ->
index n((x,v)::G1) = Some T.
Proof. admit. Qed. (* proof below *)
Hint Resolve index_extend.
Lemma resolve_extend : forall n x v G1 GC TC,
resolve G1 n = Some (GC,TC) ->
resolve ((x,v)::G1) n = Some (GC,TC).
Proof. intros. unfold resolve in H. remember (index n G1). destruct o. symmetry in Heqo.
assert (index n ((x,v)::G1) = Some v0). eapply index_extend; eauto.
remember (resolve ((x,v)::G1) n). unfold resolve in Heqo0. rewrite H0 in Heqo0.
rewrite H in Heqo0. eauto.
inversion H.
Qed.
Hint Resolve resolve_extend.
Lemma stp_extend : forall SF G1 G2 T1 T2 x v,
stp SF G1 T1 G2 T2 ->
stp SF ((x,v)::G1) T1 G2 T2 /\
stp SF G1 T1 ((x,v)::G2) T2 /\
stp SF ((x,v)::G1) T1 ((x,v)::G2) T2.
Proof. intros. stp_cases (induction H) Case;
try inversion IHstp as [IH_1 [IH_2 IH_12]];
try inversion IHstp1 as [IH1_1 [IH1_2 IH1_12]];
try inversion IHstp2 as [IH2_1 [IH2_2 IH2_12]];
split; try solve [eauto; constructor; eauto].
Qed.
Lemma stp_extend1 : forall SF G1 G2 T1 T2 x v,
stp SF G1 T1 G2 T2 ->
stp SF ((x,v)::G1) T1 G2 T2.
Proof. intros. eapply stp_extend. eauto. Qed.
Lemma stp_extend2 : forall SF G1 G2 T1 T2 x v,
stp SF G1 T1 G2 T2 ->
stp SF G1 T1 ((x,v)::G2) T2.
Proof. intros. eapply stp_extend. eauto. Qed.
(* REGULARITY *)
Lemma stp_reg : forall G1 G2 T1 T2,
stpd G1 T1 G2 T2 ->
stpd G1 T1 G1 T1 /\ stpd G2 T2 G2 T2.
Proof. intros. destruct H. stp_cases (induction H) Case;
try inversion IHstp as [[IH_n1 IH_1] [IH_n2 IH_2]];
try inversion IHstp1 as [[IH_n1 IH_1] [IH_n2 IH_2]];
try inversion IHstp2 as [[IH_n3 IH_3] [IH_n4 IH_4]];
split;
try solve [exists 0; eauto];
try solve [eexists; eauto].
Qed.
Lemma stp_reg1 : forall G1 G2 T1 T2,
stpd G1 T1 G2 T2 ->
stpd G1 T1 G1 T1.
Proof. intros. eapply stp_reg in H. inversion H. eauto. Qed.
Lemma stp_reg2 : forall G1 G2 T1 T2,
stpd G1 T1 G2 T2 ->
stpd G2 T2 G2 T2.
Proof. intros. eapply stp_reg in H. inversion H. eauto. Qed.
(* HELPERS: these mirror stp_X cases, but for stpd (too lazy to fill in) *)
Lemma stpd_sel2: forall G1 T1 G2 GC TA f x dcs,
index x G2 = Some(vabs GC f TA dcs) ->
stpd G1 T1 ((f,vabs GC f TA dcs)::GC) TA ->
stpd G1 T1 G2 (TSel x)
.
Proof. admit. Qed.
Lemma stpd_sel1: forall G1 GC TA T2 G2 f x dcs,
index x G1 = Some(vabs GC f TA dcs) ->
stpd ((f,vabs GC f TA dcs)::GC) TA G2 T2 ->
stpd G1 (TSel x) G2 T2
.
Proof. admit. Qed.
Lemma stpd_and11: forall G1 G2 T1 T2 T,
stpd G1 T1 G2 T ->
stpd G1 T2 G1 T2 ->
stpd G1 (TAnd T1 T2) G2 T
.
Proof. admit. Qed.
Lemma stpd_and12: forall G1 G2 T1 T2 T,
stpd G1 T2 G2 T ->
stpd G1 T1 G1 T1 ->
stpd G1 (TAnd T1 T2) G2 T
.
Proof. admit. Qed.
Lemma stpd_and2: forall G1 G2 T1 T2 T,
stpd G1 T G2 T1 ->
stpd G1 T G2 T2 ->
stpd G1 T G2 (TAnd T1 T2)
.
Proof. admit. Qed.
Lemma stpd_fun: forall m G1 G2 T11 T12 T21 T22,
stpd G2 T21 G1 T11 ->
stpd G1 T12 G2 T22 ->
stpd G1 (TFun m T11 T12) G2 (TFun m T21 T22)
.
Proof. admit. Qed.
Lemma stpd_mem_ss: forall G1 G2 TA1 TA2,
stpd G1 TA1 G2 TA2 ->
stpd G2 TA2 G1 TA1 ->
stpd G1 (TMem (Some TA1)) G2 (TMem (Some TA2)).
Proof. admit. Qed.
Lemma stpd_mema_sn: forall G1 G2 TA,
stpd G1 TA G1 TA -> (* regularity *)
stpd G1 (TMem (Some TA)) G2 (TMem None).
Proof. admit. Qed.
Lemma stpd_mema_nn: forall G1 G2,
stpd G1 (TMem None) G2 (TMem None).
Proof. admit. Qed.
(* TRANSITIVITY *)
Definition trans_on n12 n23 :=
forall T1 T2 T3 G1 G2 G3,
stp n12 G1 T1 G2 T2 ->
stp n23 G2 T2 G3 T3 ->
stpd G1 T1 G3 T3.
Hint Unfold trans_on.
Definition trans_up n := forall n12 n23, n12 + n23 <= n ->
trans_on n12 n23.
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 nostp_inv_dcs_mem: forall dcs TA, (* not needed? *)
nostp (dc_type_and dcs) (TMem TA).
Proof.
intros.
induction dcs.
Case "nil". eauto.
Case "cons".
unfold dc_type_and. destruct a. destruct d.
eapply nostp_and.
eapply nostp_mem_fun.
eapply IHdcs.
Qed.
Lemma stp_trans: forall n, trans_up n.
Proof. intros n.
induction n.
Case "z".
unfold trans_up. unfold trans_on.
intros.
assert (n12 = 0). omega. assert (n23 = 0). omega. subst.
inversion H0; inversion H1; subst;
try solve [inversion H0];
try solve [inversion H1];
try solve [exists 0; eauto].
SCase "Sel < Sel".
inversion H13. subst. rewrite H3 in H9. inversion H9. subst.
subst. exists 0. eapply stp_selx. eauto. eauto.
Case "S n".
unfold trans_up. intros n12 n23 NE T1 T2 T3 G1 G2 G3 S12 S23.
(* case analysis takes a long time! >= 144 cases to start with *)
stp_cases(inversion S12) SCase; stp_cases(inversion S23) SSCase; subst;
try solve [SSCase "? < Sel";
eapply stpd_sel2; [eauto | eapply trans_le in IHn; [ eapply IHn; eauto | omega ]]];
try solve [SCase "Sel < ?";
eapply stpd_sel1; [eauto | eapply trans_le in IHn; [ eapply IHn; eauto | omega ]]];
try solve [SSCase "? < ? & ?";
eapply stpd_and2; [ eapply trans_le in IHn; [ eapply IHn; eauto | omega] |
eapply trans_le in IHn; [ eapply IHn; eauto | omega]]];
try solve [SCase "T & ? < T";
eapply stpd_and11; [ eapply trans_le in IHn; [ eapply IHn; eauto | omega] | eexists; eauto]];
try solve [SCase "? & T < T";
eapply stpd_and12; [ eapply trans_le in IHn; [ eapply IHn; eauto | omega] | eexists; eauto]];
try solve [exists 0; eauto];
try solve by inversion;
idtac.
try solve [SSCase "? < Sel";
eapply stpd_sel2; [eauto | eapply trans_le in IHn; [ eapply IHn; eauto | omega ]]].
(*
SCase "Bool < Bool". SSCase "Bool < Bool".
eapply ex_intro with 0. eapply stp_bool.
*)
SCase "Fun < Fun". SSCase "Fun < Fun". inversion H10. subst.
eapply stpd_fun. eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.
eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.
SCase "Mem Some < Mem Some". SSCase "Mem Some < Mem Some". inversion H10. subst.
eapply stpd_mem_ss. eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.
eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.
SCase "Mem Some < Mem Some". SSCase "Mem Some < Mem None". inversion H9. subst.
eapply stpd_mema_sn. eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.
SCase "Mem Some < Mem None". SSCase "Mem None < Mem None".
eapply stpd_mema_sn. eapply trans_le in IHn. eapply IHn. eauto. eauto. omega.
SCase "? < ? & ?". SSCase "T & ? < T". inversion H10. subst.
eapply trans_le in IHn. eapply IHn. apply H. apply H6. omega.
SCase "? < ? & ?". SSCase "? & T < T". inversion H10. subst.
eapply trans_le in IHn. eapply IHn. apply H0. apply H6. omega.
SCase "? < Sel". SSCase "Sel < ?". (* proper mid *)
assert (trans_on n2 n0) as IHX. eapply trans_le; [ eauto | omega ].
inversion H10. subst x0. rewrite H in H6. inversion H6. subst.
eapply IHX. apply H0. apply H7.
SCase "? < Sel". SSCase "Sel < Sel".
inversion H10. subst x1. rewrite H in H6. inversion H6. subst.
eapply stpd_sel2. eauto. eexists. eapply H0.
SCase "Sel < Sel". SSCase "Sel < ?".
inversion H10. subst x2. rewrite H0 in H6. inversion H6. subst.
eapply stpd_sel1. eauto. eexists. eapply H7.
SCase "Sel < Sel". SSCase "Sel < Sel".
exists 0. eapply stp_selx. eauto. eauto. inversion H10. subst.
rewrite H0 in H6. inversion H6. subst. eapply H7.
Qed.
Lemma stpd_trans: forall G1 G2 G3 T1 T2 T3,
stpd G1 T1 G2 T2 ->
stpd G2 T2 G3 T3 ->
stpd G1 T1 G3 T3.
Proof. intros.
destruct H. destruct H0. eapply (stp_trans (x+x0) x x0). eauto. eapply H. eapply H0.
Qed.
Inductive has_type : list (nat*ty) -> tm -> ty -> Prop :=
| t_true: forall env,
has_type env ttrue TBool
| t_false: forall env,
has_type env tfalse TBool
| t_var: forall n (env:list (nat*ty)) t1,
index n env = Some t1 ->
wf_type env t1 ->
has_type env (tvar n) t1
| t_vara: forall n env T T2,
index n env = Some T ->
atp env T T2 ->
wf_type env T2 ->
has_type env (tvar n) T2
(*
| t_var_pack: forall n env T T2,
index n env = Some T ->
wf_type env T ->
has_type env (tvar n) (TBind n T)
*)
| t_app: forall env f m x TF T1 T2,
index f env = Some TF ->
tresolve TF (TFun m T1 T2) ->
wf_type env T1 ->
wf_type env T2 ->
has_type env x T1 ->
has_type env (tapp f m x) T2
| t_abs: forall env TF TFN f z dcs TA T3,
TF = (TObj (Some TA) dcs) ->
TFN = (TObj None dcs) ->
dc_has_type ((f,TF)::env) dcs ->
has_type ((f,TFN)::env) z T3 ->
wf_type ((f,TF)::env) TF ->
wf_type env T3 ->
has_type env (tabs f TA dcs z) T3
| t_let: forall env x y z T1 T3,
has_type env y T1 ->
has_type ((x,T1)::env) z T3 ->
wf_type env T3 ->
has_type env (tlet x T1 y z) T3
with dc_has_type: list(nat * ty) -> list (nat*dc) -> Prop :=
| dt_fun: forall env x y m T1 T2 dcs,
has_type ((x,T1)::env) y T2 ->
dc_has_type env dcs ->
m = length dcs ->
dc_has_type env ((m, dfun T1 T2 x y)::dcs)
| dt_nil: forall env,
dc_has_type env nil
.
Inductive wf_env : list (nat*vl) -> list (nat*ty) -> 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 : env -> vl -> ty -> Prop :=
| v_bool: forall venv b T,
stpd nil TBool venv T ->
val_type venv (vbool b) T
| v_abs: forall env venv tenv TW TF f dcs TA,
TF = (TObj (Some TA) dcs) ->
dc_has_type ((f,TF)::tenv) dcs ->
wf_env env tenv ->
wf_type ((f,TF)::tenv) TF ->
stpd ((f,(vabs env f TA dcs))::env) TF venv TW ->
val_type venv (vabs env f TA dcs) TW
.
(* could use do-notation to clean up syntax *)
Fixpoint teval(n: nat)(env: env)(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 T dcs z =>
teval n ((f,vabs env f T dcs)::env) z
| tapp x m ex =>
match teval n env ex with
| None => None
| Some None => Some None
| Some (Some vx) =>
match index x env with
| None => Some(None)
| Some (vbool _) => Some(None)
| Some (vabs env2 f T dcs) =>
match index m dcs with
| None => Some(None)
| Some (dfun T1 T2 x ey) =>
teval n ((x,vx)::(f,vabs env2 f T dcs)::env2) ey
end
end
end
| tlet x T1 y z =>
match teval n env y with
| None => None
| Some None => Some None
| Some (Some vx) =>
teval n ((x,vx)::env) z
end
end
end.
Inductive eval : env -> tm -> option vl -> Prop :=
| e_true: forall env,
eval env ttrue (Some (vbool true))
| e_false: forall env,
eval env tfalse (Some (vbool false))
| e_var: forall n (env:list (nat*vl)) v1,
index n env = Some v1 ->
eval env (tvar n) (Some v1)
| e_app: forall env env2 T T1 T2 n m f x ey ex vx rvy dcs,
index n env = Some (vabs env2 f T dcs) ->
index m dcs = Some (dfun T1 T2 x ey) ->
eval env ex (Some vx) ->
eval ((x,vx)::(f,vabs env2 f T dcs)::env2) ey rvy ->
eval env (tapp n m ex) rvy
| e_abs: forall env f T dcs z rvz,
eval ((f,vabs env f T dcs)::env) z rvz ->
eval env (tabs f T dcs z) rvz.
Hint Constructors ty.
Hint Constructors tm.
Hint Constructors vl.
Hint Constructors eval.
Hint Constructors has_type.
Hint Constructors val_type.
Hint Constructors wf_env.
Hint Constructors wf_type.
Hint Constructors stp.
Hint Constructors atp.
Hint Constructors dc_has_type.
Hint Unfold stpd.
Hint Constructors option.
Hint Constructors list.
Hint Unfold index.
Hint Unfold length.
Hint Unfold resolve.
Hint Constructors tresolve.
Hint Resolve ex_intro.
Require Import LibTactics.
Require Import Coq.Program.Equality.
Require Import Coq.Classes.Equivalence.
Require Import Coq.Classes.EquivDec.
Require Import Coq.Logic.Decidable.
(* examples *)
Definition TNat := TBool.
Definition f := 0. (*(Id 10).*)
Hint Unfold f.
Definition x := 1. (*(Id 0).*)
Definition y := 2. (*(Id 1).*)
Definition z := 3. (*(Id 2).*)
Hint Unfold x.
Hint Unfold y.
Hint Unfold z.
Definition t01 := (TArrow (Some TNat) TNat TNat).
Definition t11 := (TArrow None TNat TNat).
Definition t02 := (TArrow (Some TNat) (TSel f) (TSel f)).
Definition t12 := (TArrow None (TSel f) (TSel f)).
Hint Unfold t01.
Hint Unfold t11.
Hint Unfold t02.
Hint Unfold t12.
Definition idx (i:nat) a b := (i, dfun a b x (tvar x)).
Fixpoint tnew i t d z := tabs i t d z.
Example xx1 : eval nil ttrue (Some (vbool true)) .
Proof. eauto. Qed.
Example ev2 : eval nil
(tnew f TNat [idx 0 TNat TNat] (tvar f))
(Some (vabs nil f TNat [idx 0 TNat TNat])).
Proof.
repeat (econstructor; eauto).
Qed.
Example tp2 : has_type nil
(tabs f TNat [idx 0 TNat TNat] (tvar f))
t11. (* want t11 here! *)
Proof.
repeat (econstructor; compute; eauto).
Qed.
(*
let f: { A = Nat; Nat => Nat } = x => x
let x: { A; Nat => Nat } = f
let y: Nat = x(7)
true
*)
Example tp3 : has_type nil
(tabs f TNat [idx 0 TNat TNat]
(tlet x t11 (tvar f) (* abstract type mem *)
ttrue))
TBool.
Proof.
repeat (econstructor; eauto).
Qed.
(* Hint Extern 1 (_ = _) => abstract compute. *)
Hint Constructors has_type.
Hint Constructors dc_has_type.
Hint Unfold idx.
Hint Unfold dc_type_and.
(*
match goal with
| |- has_type _ (tvar _) _ =>
try solve [apply t_vara;
repeat (econstructor; eauto)]
| _ => idtac
end;
*)
Ltac crush_has_tp :=
try solve [econstructor; compute; eauto; crush_has_tp];
try (eapply t_vara; compute; eauto; crush_has_tp).
(*
let f: { A = Nat; Nat => f.A } = x => x
true
*)
Example tp4 : has_type nil
(tabs f TNat [idx 0 TNat (TSel f)]
ttrue)
TBool.
Proof.
crush_has_tp.
Qed.
(*
let f: { A = Nat; Nat => f.A } = x => x
let x: { A; Nat => f.A } = f
true
*)
Example tp5 : has_type nil
(tabs f TNat [idx 0 TNat (TSel f)]
(tlet x (TArrow None TNat (TSel f)) (tvar f) (* abstract type mem *)
ttrue))
TBool.
Proof.
crush_has_tp.
Qed.
(*
BRANDING
let f: { A = Nat; Nat => f.A } = x => x
let x: { A; Nat => f.A } = f
let y: f.A = x(7)
true
*)
Example tp6 : has_type nil
(tabs f TNat [idx 0 TNat (TSel f)]
(tlet x (TArrow None TNat (TSel f)) (tvar f) (* abstract type mem *)
(tlet y (TSel f) (tapp x 0 ttrue)
ttrue)))
TBool.
Proof.
crush_has_tp.
Qed.
(*
UNBRANDING
let f: { A = Nat; Nat => f.A ; f.A => Nat } = x => x ; x => x
let x: Nat = 7
let y: f.A = f.0(x) // intro
let z: Nat = f.1(y) // elim
z
*)
Example tp7 : has_type nil
(tabs f TNat [idx 1 (TSel f) TNat; idx 0 TNat (TSel f)]
(tlet x (TBool) (ttrue)
(tlet y (TSel f) (tapp f 0 (tvar x)) (* call intro *)
(tlet z TNat (tapp f 1 (tvar y)) (* call elim *)
(tvar z)))))
TBool.
Proof.
crush_has_tp.
Qed.
(*
branding/unbranding needs two methods
val a = new {
type A = Nat
def intro(x:Nat): a.A = x
def elim(x:a.A): Nat = x
} // type A abstract outside
val x: a.A = a.intro(7)
val y: Nat = a.elim(x)
val z: a.A = 7 // fail
val u: Nat = x // fail
*)
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.