forked from TiarkRompf/minidot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdot3.v
1992 lines (1587 loc) · 57.9 KB
/
dot3.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
- self types
*)
(*
TODO
- object creation without let
(but we can already encode it)
- other static stp rules
and, fun, mem, bot, top etc
- subsumption for has_type rules
- extend dyn stp rules with
static tenv for self refs
- pack/unpack in subtyping
- multiple type members
- lower and upper bounds
*)
(* ############################################################ *)
(* Syntax *)
(* ############################################################ *)
Module DOT.
Definition id := nat.
Inductive ty : Type :=
| TNoF : ty (* marker for empty method list *)
| TBool : ty
| TAnd : ty -> ty -> ty
| TFun : id -> ty -> ty -> ty
| TMem : (option 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) (dc_type_and dcs).
Definition TArrow p x y := TAnd (TMem 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 *)
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.
(* LOCALLY NAMELESS *)
Inductive closed_rec: nat -> ty -> Prop :=
| cl_top: forall k,
closed_rec k TNoF
| cl_bool: forall k,
closed_rec k TBool
| cl_mem_n: forall k,
closed_rec k (TMem None)
| cl_mem_s: forall k T1,
closed_rec k T1 ->
closed_rec k (TMem (Some T1))
| cl_fun: forall k m T1 T2,
closed_rec k T1 ->
closed_rec k T2 ->
closed_rec k (TFun m T1 T2)
| cl_and: forall k T1 T2,
closed_rec k T1 ->
closed_rec k T2 ->
closed_rec k (TAnd T1 T2)
| cl_bind: forall k T1,
closed_rec (S k) T1 ->
closed_rec k (TBind T1)
| cl_sel: forall k x,
closed_rec k (TSel x)
| cl_selb: forall k i,
k > i ->
closed_rec k (TSelB i)
.
Hint Constructors closed_rec.
Definition closed j T := closed_rec j T.
Fixpoint open_rec (k: nat) (u: id) (T: ty) { struct T }: ty :=
match T with
| TSel x => TSel x (* free var remains free. functional, so we can't check for conflict *)
| TSelB i => if beq_nat k i then TSel u else TSelB i
| TBind T1 => TBind (open_rec (S k) u T1)
| TNoF => TNoF
| TBool => TBool
| TAnd T1 T2 => TAnd (open_rec k u T1) (open_rec k u T2)
| TMem (Some T1) => TMem (Some (open_rec k u T1))
| TMem None => TMem None
| TFun m T1 T2 => TFun m (open_rec k u T1) (open_rec k u T2)
end.
Definition open u T := open_rec 0 u T.
(* sanity check *)
Example open_ex1: open 9 (TBind (TAnd (TMem None) (TFun 0 (TSelB 1) (TSelB 0)))) =
(TBind (TAnd (TMem None) (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_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 x TE (TMem TA) ->
wf_type envz (TSel x)
| wf_selb: forall envz x, (* note: disregarding bind-scope *)
wf_type envz (TSelB x)
| wf_bind: forall envz T,
wf_type envz T ->
wf_type envz (TBind T)
.
Tactic Notation "wf_cases" tactic(first) ident(c) :=
first;
[ Case_aux c "Top" |
Case_aux c "Bool" |
Case_aux c "And" |
Case_aux c "MemA" |
Case_aux c "Mem" |
Case_aux c "Fun" |
Case_aux c "Sel" |
Case_aux x "SelB" |
Case_aux c "Bind" ].
(* 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 x TF (TMem (Some T)) ->
atp env T (TSel x)
| atp_sel1: forall x env T TF,
index x env = Some TF ->
tresolve x 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 < ?" ].
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 ->
open n T2 = T ->
wf_type env T ->
wf_type env (TBind T2) ->
has_type env (tvar n) (TBind T2)
| t_var_unpack: forall n env T T2,
index n env = Some (TBind T2) ->
open n T2 = T ->
wf_type env T ->
wf_type env (TBind T2) ->
has_type env (tvar n) T
| t_app: forall env f m x TF T1 T2,
index f env = Some TF ->
tresolve f 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) ->
closed 0 TA -> (* should be implied by WF below *)
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
.
(* ############################################################ *)
(* Dynamic properties: value typing, evaluation, ... *)
(* ############################################################ *)
(* dynamic subtyping: during execution *)
Inductive stp : nat -> env -> ty -> env -> ty -> Prop :=
| stp_top: forall n1 G1 G2,
stp n1 G1 TNoF G2 TNoF (* 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_bind: forall n1 G1 G2 TA1 TA2,
stp n1 G1 TA1 G2 TA2 ->
stp (S n1+n1) G1 (TBind TA1) G2 (TBind TA2) (* may relax to diff types *)
| 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) ->
closed 0 TA ->
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) ->
closed 0 TA ->
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)
| stp_selbx: forall n1 x G1 G2,
stp n1 G1 (TSelB x) G2 (TSelB x)
.
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 "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 "SelB < SelB" ].
Definition stpd G1 T1 G2 T2 := exists n, stp n G1 T1 G2 T2.
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) ->
closed 0 TA -> (* should be implied by WF below *)
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
| v_pack: forall venv venv3 x v T T2 T3,
index x venv = Some v ->
val_type venv v T ->
open x T2 = T ->
stpd venv (TBind T2) venv3 T3 ->
val_type venv3 v T3
.
(* evaluation. 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.
(* not used, just for completeness *)
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.
(* ############################################################ *)
(* Examples *)
(* ############################################################ *)
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 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 solve [eapply t_vara; compute; eauto; crush_has_tp];
try solve [eapply t_var_unpack; compute; eauto; crush_has_tp];
try solve [eapply t_var_pack; compute; eauto; crush_has_tp];
try solve [eapply tr_unpack; 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/unbranding. roughly this:
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
*)
(*
BRANDING
let f: { A = Nat; Nat => f.A } = x => x
let x: { A; Nat => f.A } = f
let f.A: Nat = 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.
(*
SELF TYPES: PACK
let f: { A = Nat; Nat => f.A } = x => x
f
*)
Example tp8 : has_type nil
(tabs f TNat [idx 0 TNat (TSel f)]
(tvar f))
(TBind (TAnd (TMem None) (TAnd (TFun 0 TNat (TSelB 0)) TNoF))).
Proof.
crush_has_tp.
Qed.
(*
SELF TYPES: return from function
let f: { A = Nat; Nat => f.A } = x => x
f
*)
(*
Expand bind so that the (TSel f) becomes well-formed.
*)
Example tp91 : has_type
[(f,(TBind (TAnd (TMem None) (TAnd (TFun 0 TNat (TSelB 0)) TNoF))))]
(tvar f)
(TAnd (TMem None) (TAnd (TFun 0 TNat (TSel f)) TNoF)).
Proof.
crush_has_tp.
Qed.
Example tp92 : has_type
[(f,(TBind (TAnd (TMem None) (TAnd (TFun 0 TNat (TSelB 0)) TNoF))))]
(tapp f 0 ttrue)
(TSel f).
Proof.
econstructor; crush_has_tp.
Qed.
Example tp93 : has_type nil
(tlet f (TBind (TAnd (TMem None) (TAnd (TFun 0 TNat (TSelB 0)) TNoF)))
(tabs f TNat [idx 0 TNat (TSel f)]
(tvar f))
(tlet x (TAnd (TMem None) (TAnd (TFun 0 TNat (TSel f)) TNoF)) (tvar f)
ttrue)) (* note that x.fun has type Nat -> f.A *)
TBool. (* if we want Nat -> x.A we need to assign a Bind to x *)
Proof.
crush_has_tp.
Qed.
Example tp94 : has_type nil
(tlet f (TBind (TAnd (TMem None) (TAnd (TFun 1 (TSelB 0) TNat) (TAnd (TFun 0 TNat (TSelB 0)) TNoF))))
(tabs f TNat [idx 1 (TSel f) TNat; idx 0 TNat (TSel f)]
(tvar f))
(* x binding is not used. just a sanity check that we derive the correct type *)
(tlet x (TAnd (TMem None) (TAnd (TFun 1 (TSel f) TNat) (TAnd (TFun 0 TNat (TSel f)) TNoF))) (tvar f)
(tlet y (TSel f) (tapp f 0 (ttrue)) (* call intro *)
(tlet z TNat (tapp f 1 (tvar y)) (* call elim *)
(tvar z)))))
TBool.
Proof.
econstructor; crush_has_tp.
econstructor; crush_has_tp.
econstructor; crush_has_tp.
econstructor; crush_has_tp.
econstructor; crush_has_tp.
econstructor; crush_has_tp.
Qed.
(* ############################################################ *)
(* Proofs *)
(* ############################################################ *)
Lemma hastp_wf: forall G e T, has_type G e T -> wf_type G T.
Proof. intros. induction H; eauto.
Qed.
Lemma index_max : forall X vs n (T: X),
index n vs = Some T ->
n < length vs.
Proof.
intros X vs. induction vs.
Case "nil". intros. inversion H.
Case "cons".
intros. inversion H. destruct a.
case_eq (beq_nat n (length vs)); intros E.
SCase "hit".
rewrite E in H1. inversion H1. subst.
eapply beq_nat_true in E.
unfold length. unfold length in E. rewrite E. eauto.
SCase "miss".
rewrite E in H1.
assert (n < length vs). eapply IHvs. apply H1.
compute. eauto.
Qed.
Lemma index_extend : forall X vs n a (T: X),
index n vs = Some T ->
index n (a::vs) = Some T.
Proof.
intros.
assert (n < length vs). eapply index_max. eauto.
assert (n <> length vs). omega.
assert (beq_nat n (length vs) = false) as E. eapply beq_nat_false_iff; eauto.
unfold index. unfold index in H. rewrite H. rewrite E. destruct a. reflexivity.
Qed.
Hint Resolve index_extend.
Lemma wft_extend : forall vs x v1 T,
wf_type vs T ->
wf_type ((x,v1)::vs) T.
Proof. intros. induction H; eauto. Qed.
Hint Resolve wft_extend.
(* impossible subtyping cases, uses for contradictions *)
Inductive nostp: ty -> ty -> Prop :=
| nostp_top_fun: forall m T1 T2,
nostp TNoF (TFun m T1 T2)
| nostp_top_mem: forall TA,
nostp TNoF (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.
(* 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 x0). 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 H2. destruct H2.
subst. eexists. eapply stp_sel1. eauto. 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 H2. destruct H2.
subst. eexists. eapply stp_sel1. eauto. eauto. eauto.
Qed.
(* EXTENSION *)
Hint Constructors stp.
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.