-
Notifications
You must be signed in to change notification settings - Fork 11
/
dot21.v
7853 lines (7059 loc) · 302 KB
/
dot21.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 DOT *)
(* this version is based on dot20.v *)
(* based on that, it adds the t_app_var typing rule, for *)
(* fully dependent method types *)
Require Export SfLib.
Require Export Arith.EqNat.
Require Export Arith.Le.
Require Import Coq.Program.Equality.
Module FSUB.
Definition id := nat.
Inductive var : Type :=
| varF : id -> var
| varH : id -> var
| varB : id -> var
.
Inductive ty : Type :=
| TBool : ty
| TBot : ty
| TTop : ty
| TMem : id -> ty -> ty -> ty
| TSel : var -> id -> ty
| TAll : id -> ty -> ty -> ty
| TBind : ty -> ty
| TAnd : ty -> ty -> ty
.
Inductive tm : Type :=
| ttrue : tm
| tfalse : tm
| tvar : id -> tm
| tapp : tm -> id -> tm -> tm (* \o.m(x) *)
| tobj : id -> list (id * def) -> tm (* \o {d} *)
| tlet : id -> tm -> tm -> tm (* let \x = t1 in t2 *)
with def : Type :=
| dfun : id -> tm -> def
| dmem : ty -> def
.
Inductive vl : Type :=
| vbool : bool -> vl
| vobj : list (id*vl) -> id -> list (id * def) -> 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_mem: forall k l m T1 T2,
closed_rec k l T1 ->
closed_rec k l T2 ->
closed_rec k l (TMem m T1 T2)
| cl_all: forall k l m T1 T2,
closed_rec k l T1 ->
closed_rec (S k) l T2 ->
closed_rec k l (TAll m T1 T2)
| cl_bind: forall k l T2,
closed_rec (S k) l T2 ->
closed_rec k l (TBind T2)
| cl_sel: forall k l x m,
closed_rec k l (TSel (varF x) m)
| cl_and: forall k l T1 T2,
closed_rec k l T1 ->
closed_rec k l T2 ->
closed_rec k l (TAnd T1 T2)
| cl_selh: forall k l x m,
l > x ->
closed_rec k l (TSel (varH x) m)
| cl_selb: forall k l i m,
k > i ->
closed_rec k l (TSel (varB i) m)
.
Hint Constructors closed_rec.
Definition closed j l T := closed_rec j l T.
Fixpoint open_rec (k: nat) (u: var) (T: ty) { struct T }: ty :=
match T with
| TSel (varF x) m => TSel (varF x) m (* free var remains free. functional, so we can't check for conflict *)
| TSel (varH i) m => TSel (varH i) m
| TSel (varB i) m => TSel (if beq_nat k i then u else varB i) m
| TAll m T1 T2 => TAll m (open_rec k u T1) (open_rec (S k) u T2)
| TBind T2 => TBind (open_rec (S k) u T2)
| TTop => TTop
| TBot => TBot
| TBool => TBool
| TMem m T1 T2 => TMem m (open_rec k u T1) (open_rec k u T2)
| TAnd T1 T2 => TAnd (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 (varF 9) (TAll 0 TBool (TAll 0 (TSel (varB 1) 0) (TSel (varB 1) 0))) =
(TAll 0 TBool (TAll 0 (TSel (varF 9) 0) (TSel (varB 1) 0))).
Proof. compute. eauto. Qed.
Fixpoint subst (U : var) (T : ty) {struct T} : ty :=
match T with
| TTop => TTop
| TBot => TBot
| TBool => TBool
| TMem m T1 T2 => TMem m (subst U T1) (subst U T2)
| TSel (varB i) m => TSel (varB i) m
| TSel (varF i) m => TSel (varF i) m
| TSel (varH i) m => TSel (if beq_nat i 0 then U else varH (i-1)) m
| TAll m T1 T2 => TAll m (subst U T1) (subst U T2)
| TBind T2 => TBind (subst U T2)
| 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
| TBool => True
| TMem m T1 T2 => nosubst T1 /\ nosubst T2
| TSel (varB i) m => True
| TSel (varF i) m => True
| TSel (varH i) m => i <> 0
| TAll m T1 T2 => nosubst T1 /\ nosubst T2
| TBind T2 => nosubst T2
| TAnd T1 T2 => nosubst T1 /\ nosubst T2
end.
Hint Unfold open.
Hint Unfold closed.
Inductive stp: tenv -> tenv -> ty -> ty -> Prop :=
| stp_topx: forall G1 GH,
stp G1 GH TTop TTop
| stp_botx: forall G1 GH,
stp G1 GH TBot TBot
| stp_top: forall G1 GH T1,
stp G1 GH T1 T1 -> (* regularity *)
stp G1 GH T1 TTop
| stp_bot: forall G1 GH T2,
stp G1 GH T2 T2 -> (* regularity *)
stp G1 GH TBot T2
| stp_bool: forall G1 GH,
stp G1 GH TBool TBool
| stp_mem: forall G1 GH m T1 T2 T3 T4,
stp G1 GH T3 T1 ->
stp G1 GH T2 T4 ->
stp G1 GH (TMem m T1 T2) (TMem m T3 T4)
| stp_sel1: forall G1 GH TX m T2 x,
index x G1 = Some TX ->
closed 0 0 TX ->
stp G1 GH TX (TMem m TBot T2) ->
stp G1 GH T2 T2 -> (* regularity of stp2 *)
stp G1 GH (TSel (varF x) m) T2
| stp_sel2: forall G1 GH TX m T1 x,
index x G1 = Some TX ->
closed 0 0 TX ->
stp G1 GH TX (TMem m T1 TTop) ->
stp G1 GH T1 T1 -> (* regularity of stp2 *)
stp G1 GH T1 (TSel (varF x) m)
| stp_selb1: forall G1 GH TX m T2 x,
index x G1 = Some TX ->
stp G1 [] TX (TBind (TMem m TBot T2)) -> (* Note GH = [] *)
stp G1 GH (open (varF x) T2) (open (varF x) T2) -> (* regularity *)
stp G1 GH (TSel (varF x) m) (open (varF x) T2)
| stp_selb2: forall G1 GH TX m T1 x,
index x G1 = Some TX ->
stp G1 [] TX (TBind (TMem m T1 TTop)) -> (* Note GH = [] *)
stp G1 GH (open (varF x) T1) (open (varF x) T1) -> (* regularity *)
stp G1 GH (open (varF x) T1) (TSel (varF x) m)
| stp_selx: forall G1 GH TX x m,
index x G1 = Some TX ->
stp G1 GH (TSel (varF x) m) (TSel (varF x) m)
| stp_sela1: forall G1 GH TX m T2 x,
indexr x GH = Some TX ->
closed 0 (S x) TX ->
stp G1 GH TX (TMem m TBot T2) -> (* not using self name for now *)
stp G1 GH T2 T2 -> (* regularity of stp2 *)
stp G1 GH (TSel (varH x) m) T2
| stp_sela2: forall G1 GH TX m T1 x,
indexr x GH = Some TX ->
closed 0 (S x) TX ->
stp G1 GH TX (TMem m T1 TTop) -> (* not using self name for now *)
stp G1 GH T1 T1 -> (* regularity of stp2 *)
stp G1 GH T1 (TSel (varH x) m)
| stp_selab1: forall G1 GH GU GL TX m T2 T2' x,
indexr x GH = Some TX ->
closed 0 (S x) TX ->
closed 0 0 (TBind (TMem m TBot T2)) ->
length GL = (S x) ->
GH = GU ++ GL ->
stp G1 GL TX (TBind (TMem m TBot T2)) ->
T2' = (open (varH x) T2) ->
stp G1 GH T2' T2' -> (* regularity *)
stp G1 GH (TSel (varH x) m) T2'
| stp_selab2: forall G1 GH GU GL TX m T1 T1' x,
indexr x GH = Some TX ->
closed 0 (S x) TX ->
closed 0 0 (TBind (TMem m T1 TTop)) ->
length GL = (S x) ->
GH = GU ++ GL ->
stp G1 GL TX (TBind (TMem m T1 TTop)) ->
T1' = (open (varH x) T1) ->
stp G1 GH T1' T1' -> (* regularity *)
stp G1 GH T1' (TSel (varH x) m)
| stp_selax: forall G1 GH TX x m,
indexr x GH = Some TX ->
stp G1 GH (TSel (varH x) m) (TSel (varH x) m)
| stp_all: forall G1 GH m 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,T1)::GH) (open (varH x) T2) (open (varH x) T2) -> (* regularity *)
stp G1 ((0,T3)::GH) (open (varH x) T2) (open (varH x) T4) ->
stp G1 GH (TAll m T1 T2) (TAll m T3 T4)
| stp_bindx: forall G1 GH T1 T2 x,
x = length GH ->
closed 1 (length GH) T1 -> (* must not accidentally bind x *)
closed 1 (length GH) T2 ->
stp G1 ((0,open (varH x) T2)::GH) (open (varH x) T2) (open (varH x) T2) -> (* regularity *)
stp G1 ((0,open (varH x) T1)::GH) (open (varH x) T1) (open (varH x) T2) ->
stp G1 GH (TBind T1) (TBind T2)
| stp_and11: forall G GH T1 T2 T,
stp G GH T1 T ->
stp G GH T2 T2 -> (* regularity *)
stp G GH (TAnd T1 T2) T
| stp_and12: forall G GH T1 T2 T,
stp G GH T2 T ->
stp G GH T1 T1 -> (* regularity *)
stp G GH (TAnd T1 T2) T
| stp_and2: forall G GH T1 T2 T,
stp G GH T T1 ->
stp G GH T T2 ->
stp G GH T (TAnd T1 T2)
.
(*
with path_type: tenv -> tenv -> id -> ty -> Prop :=
| pt_var: forall G1 GH TX x,
index x G1 = Some TX ->
path_type G1 GH x TX
| pt_sub: forall G1 GH TX x,
path_type has_type env e T1 ->
stp env [] T1 T2 ->
has_type env e T2
with pathH_type: tenv -> tenv -> id -> ty -> Prop :=
| pth_var: forall G1 GH TX T x,
indexr x GH = Some TX ->
stp G1 GH TX T ->
pathH_type G1 GH x T
*)
Hint Constructors stp.
Function tand (t1: ty) (t2: ty) :=
match t2 with
| TTop => t1
| _ => TAnd t1 t2
end.
(* 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_var_pack: forall x env T1,
has_type env (tvar x) (open (varF x) T1) ->
stp env [] (TBind T1) (TBind T1) ->
has_type env (tvar x) (TBind T1)
| t_var_unpack: forall x env T1,
has_type env (tvar x) (TBind T1) ->
stp env [] (open (varF x) T1) (open (varF x) T1) ->
has_type env (tvar x) (open (varF x) T1)
| t_obj: forall env f ds T TX,
fresh env = f ->
open (varF f) T = TX ->
dcs_has_type ((f, TX)::env) f ds T ->
stp ((f, TX)::env) [] TX TX ->
stp env [] (TBind T) (TBind T) ->
has_type env (tobj f ds) (TBind T)
| t_app: forall env f l x T1 T2,
has_type env f (TAll l T1 T2) ->
has_type env x T1 ->
stp env [] T2 T2 ->
has_type env (tapp f l x) T2
| t_app_var: forall env f l x T1 T2 T2X,
has_type env f (TAll l T1 T2) ->
has_type env (tvar x) T1 ->
open (varF x) T2 = T2X ->
stp env [] T2X T2X ->
has_type env (tapp f l (tvar x)) T2X
| t_let: forall env x ex e Tx T,
has_type env ex Tx ->
fresh env <= x ->
has_type ((x, Tx)::env) e T ->
stp env [] T T ->
has_type env (tlet x ex e) T
| t_sub: forall env e T1 T2,
has_type env e T1 ->
stp env [] T1 T2 ->
has_type env e T2
with dcs_has_type: tenv -> id -> list (id * def) -> ty -> Prop :=
| dt_nil: forall env f,
dcs_has_type env f nil TTop
| dt_fun: forall env f x y m T1 T2 dcs TS T,
has_type ((x,open (varF f) T1)::env) y (open (varF x) (open_rec 1 (varF f) T2)) ->
dcs_has_type env f dcs TS ->
fresh env = x ->
m = length dcs ->
T = tand (TAll m T1 T2) TS ->
dcs_has_type env f ((m, dfun x y)::dcs) T
| dt_mem: forall env f m T1 dcs TS T,
dcs_has_type env f dcs TS ->
m = length dcs ->
T = tand (TMem m T1 T1) TS ->
dcs_has_type env f ((m, dmem T1)::dcs) T
.
Definition base (v:vl): venv :=
match v with
| vbool _ => nil
| vobj GX _ _ => GX
end.
Definition MAX := 2.
Inductive stp2: nat -> bool -> venv -> ty -> venv -> ty -> list (id*(venv*ty)) -> nat -> Prop :=
| stp2_topx: forall m G1 G2 GH n1,
stp2 m true G1 TTop G2 TTop GH (S n1)
| stp2_botx: forall m G1 G2 GH n1,
stp2 m true G1 TBot G2 TBot GH (S n1)
| stp2_top: forall m G1 G2 GH T n1,
stp2 m true G1 T G1 T GH n1 -> (* regularity *)
stp2 m true G1 T G2 TTop GH (S n1)
| stp2_bot: forall m G1 G2 GH T n1,
stp2 m true G2 T G2 T GH n1 -> (* regularity *)
stp2 m true G1 TBot G2 T GH (S n1)
| stp2_bool: forall m G1 G2 GH n1,
stp2 m true G1 TBool G2 TBool GH (S n1)
| stp2_mem: forall G1 G2 l T1 T2 T3 T4 GH n1 n2,
stp2 0 false G2 T3 G1 T1 GH n1 ->
stp2 0 true G1 T2 G2 T4 GH n2 ->
stp2 0 true G1 (TMem l T1 T2) G2 (TMem l T3 T4) GH (S (n1+n2))
| stp2_mem2: forall m G1 G2 l T1 T2 T3 T4 GH n1 n2,
stp2 (S m) false G2 T3 G1 T1 GH n1 ->
stp2 (S m) false G1 T2 G2 T4 GH n2 ->
stp2 (S m) true G1 (TMem l T1 T2) G2 (TMem l T3 T4) GH (S (n1+n2))
(* strong version, with precise/invertible bounds *)
| stp2_strong_sel1: forall G1 G2 GX l f ds TX x T2 GH GX' TX' n1 n2 nv,
index x G1 = Some (vobj GX f ds) ->
val_type GX' (vobj GX f ds) TX' nv -> (* for downgrade *)
stp2 0 false GX' TX' G2 (TMem l TBot T2) GH n2 -> (* for downgrade *)
index l ds = Some (dmem TX) ->
closed 1 0 TX ->
stp2 0 true ((f, vobj GX f ds)::GX) (open (varF f) TX) G2 T2 GH n1 ->
stp2 0 true G1 (TSel (varF x) l) G2 T2 GH (S (n1+n2))
| stp2_strong_sel2: forall G1 G2 GX l f ds TX x T1 GH GX' TX' n1 n2 nv,
index x G2 = Some (vobj GX f ds) ->
val_type GX' (vobj GX f ds) TX' nv -> (* for downgrade *)
stp2 0 false GX' TX' G1 (TMem l T1 TTop) GH n2 -> (* for downgrade *)
index l ds = Some (dmem TX) ->
closed 1 0 TX ->
stp2 0 false G1 T1 ((f, vobj GX f ds)::GX) (open (varF f) TX) GH n1 ->
stp2 0 true G1 T1 G2 (TSel (varF x) l) GH (S (n1+n2))
| stp2_strong_selx: forall G1 G2 l v x1 x2 GH n1,
index x1 G1 = Some v ->
index x2 G2 = Some v ->
stp2 0 true G1 (TSel (varF x1) l) G2 (TSel (varF x2) l) GH n1
(* existing object, but imprecise type *)
| stp2_sel1: forall m G1 G2 GX l TX x T2 GH n1 n2 v nv,
index x G1 = Some v ->
val_type GX v TX nv ->
closed 0 0 TX ->
stp2 (S m) false GX TX G2 (TMem l TBot T2) GH n1 ->
stp2 (S m) true G2 T2 G2 T2 GH n2 -> (* regularity *)
stp2 (S m) true G1 (TSel (varF x) l) G2 T2 GH (S (n1+n2))
| stp2_selb1: forall m G1 G2 GX l TX x x' T2 GH n1 n2 v nv,
index x G1 = Some v -> (index x' G2 = Some v \/ closed 0 0 T2) ->
val_type GX v TX nv ->
closed 0 0 TX ->
stp2 (S (S m)) false GX TX G2 (TBind (TMem l TBot T2)) [] n1 -> (* Note GH = [] *)
stp2 (S (S m)) true G2 (open (varF x') T2) G2 (open (varF x') T2) GH n2 -> (* regularity *)
stp2 (S (S m)) true G1 (TSel (varF x) l) G2 (open (varF x') T2) GH (S (n1+n2))
| stp2_sel2: forall m G1 G2 GX l TX x T1 GH n1 n2 v nv,
index x G2 = Some v ->
val_type GX v TX nv ->
closed 0 0 TX ->
stp2 (S m) false GX TX G1 (TMem l T1 TTop) GH n1 ->
stp2 (S m) true G1 T1 G1 T1 GH n2 -> (* regularity *)
stp2 (S m) true G1 T1 G2 (TSel (varF x) l) GH (S (n1+n2))
| stp2_selb2: forall m G1 G2 GX l TX x x' T1 GH n1 n2 v nv,
index x G2 = Some v -> (index x' G1 = Some v \/ closed 0 0 T1) ->
val_type GX v TX nv ->
closed 0 0 TX ->
stp2 (S (S m)) false GX TX G1 (TBind (TMem l T1 TTop)) [] n1 -> (* Note GH = [] *)
stp2 (S (S m)) true G1 (open (varF x') T1) G1 (open (varF x') T1) GH n2 -> (* regularity *)
stp2 (S (S m)) true G1 (open (varF x') T1) G2 (TSel (varF x) l) GH (S (n1+n2))
| stp2_selx: forall m G1 G2 l v x1 x2 GH n1,
index x1 G1 = Some v ->
index x2 G2 = Some v ->
stp2 (S m) true G1 (TSel (varF x1) l) G2 (TSel (varF x2) l) GH (S n1)
(* hypothetical object *)
| stp2_sela1: forall m G1 G2 GX l TX x T2 GH n1 n2,
indexr x GH = Some (GX, TX) ->
closed 0 (S x) TX ->
stp2 (S m) false GX TX G2 (TMem l TBot T2) GH n1 ->
stp2 (S m) true G2 T2 G2 T2 GH n2 -> (* regularity *)
stp2 (S m) true G1 (TSel (varH x) l) G2 T2 GH (S (n1+n2))
| stp2_selab1: forall m G1 G2 GX l TX x T2 T2' GH GU GL n1 n2,
indexr x GH = Some (GX, TX) ->
closed 0 (S x) TX ->
closed 0 0 (TBind (TMem l TBot T2)) ->
length GL = (S x) ->
GH = GU ++ GL ->
stp2 (S m) false GX TX G2 (TBind (TMem l TBot T2)) GL n1 ->
T2' = (open (varH x) T2) ->
stp2 (S m) true G2 T2' G2 T2' GH n2 -> (* regularity *)
stp2 (S m) true G1 (TSel (varH x) l) G2 T2' GH (S (n1+n2))
| stp2_selab2: forall m G1 G2 GX l TX x T1 T1' GH GU GL n1 n2,
indexr x GH = Some (GX, TX) ->
closed 0 (S x) TX ->
closed 0 0 (TBind (TMem l T1 TTop)) ->
length GL = (S x) ->
GH = GU ++ GL ->
stp2 (S m) false GX TX G1 (TBind (TMem l T1 TTop)) GL n1 ->
T1' = (open (varH x) T1) ->
stp2 (S m) true G1 T1' G1 T1' GH n2 -> (* regularity *)
stp2 (S m) true G1 T1' G2 (TSel (varH x) l) GH (S (n1+n2))
| stp2_sela2: forall m G1 G2 GX l TX x T1 GH n1 n2,
indexr x GH = Some (GX, TX) ->
closed 0 (S x) TX ->
stp2 (S m) false GX TX G1 (TMem l T1 TTop) GH n1 ->
stp2 (S m) true G1 T1 G1 T1 GH n2 -> (* regularity *)
stp2 (S m) true G1 T1 G2 (TSel (varH x) l) GH (S (n1+n2))
| stp2_selax: forall m G1 G2 GX l TX x GH n1,
indexr x GH = Some (GX, TX) ->
stp2 (S m) true G1 (TSel (varH x) l) G2 (TSel (varH x) l) GH (S n1)
| stp2_all: forall m G1 G2 l T1 T2 T3 T4 GH n1 n1' n2,
stp2 MAX false G2 T3 G1 T1 GH n1 ->
closed 1 (length GH) T2 -> (* must not accidentally bind x *)
closed 1 (length GH) T4 ->
stp2 MAX false G1 (open (varH (length GH)) T2) G1 (open (varH (length GH)) T2) ((0,(G1, T1))::GH) n1' -> (* regularity *)
stp2 MAX false G1 (open (varH (length GH)) T2) G2 (open (varH (length GH)) T4) ((0,(G2, T3))::GH) n2 ->
stp2 m true G1 (TAll l T1 T2) G2 (TAll l T3 T4) GH (S (n1+n1'+n2))
| stp2_bind: forall G1 G2 T1 T2 GH n1 n2,
closed 1 (length GH) T1 -> (* must not accidentally bind x *)
closed 1 (length GH) T2 ->
stp2 1 false G2 (open (varH (length GH)) T2) G2 (open (varH (length GH)) T2) ((0,(G2, open (varH (length GH)) T2))::GH) n2 -> (* regularity *)
stp2 1 false G1 (open (varH (length GH)) T1) G2 (open (varH (length GH)) T2) ((0,(G1, open (varH (length GH)) T1))::GH) n1 ->
stp2 0 true G1 (TBind T1) G2 (TBind T2) GH (S (n1+n2))
| stp2_bindb: forall m G1 G2 T1 T2 GH n1 n2,
closed 1 (length GH) T1 -> (* must not accidentally bind x *)
closed 1 (length GH) T2 ->
stp2 (S m) false G2 (open (varH (length GH)) T2) G2 (open (varH (length GH)) T2) ((0,(G2, open (varH (length GH)) T2))::GH) n2 -> (* regularity *)
stp2 (S m) false G1 (open (varH (length GH)) T1) G2 (open (varH (length GH)) T2) ((0,(G1, open (varH (length GH)) T1))::GH) n1 ->
stp2 (S m) true G1 (TBind T1) G2 (TBind T2) GH (S (n1+n2))
| stp2_and11: forall m n1 n2 G1 G2 GH T1 T2 T,
stp2 m true G1 T1 G2 T GH n1 ->
stp2 m true G1 T2 G1 T2 GH n2 -> (* regularity *)
stp2 m true G1 (TAnd T1 T2) G2 T GH (S (n1+n2))
| stp2_and12: forall m n1 n2 G1 G2 GH T1 T2 T,
stp2 m true G1 T2 G2 T GH n1 ->
stp2 m true G1 T1 G1 T1 GH n2 -> (* regularity *)
stp2 m true G1 (TAnd T1 T2) G2 T GH (S (n1+n2))
| stp2_and2: forall m n1 n2 G1 G2 GH T1 T2 T,
stp2 m false G1 T G2 T1 GH n1 ->
stp2 m false G1 T G2 T2 GH n2 ->
stp2 m true G1 T G2 (TAnd T1 T2) GH (S (n1+n2))
| stp2_wrapf: forall m G1 G2 T1 T2 GH n1,
stp2 m true G1 T1 G2 T2 GH n1 ->
stp2 m false G1 T1 G2 T2 GH (S n1)
| stp2_transf: forall m G1 G2 G3 T1 T2 T3 GH n1 n2,
stp2 m true G1 T1 G2 T2 GH n1 ->
stp2 m false G2 T2 G3 T3 GH n2 ->
stp2 m false G1 T1 G3 T3 GH (S (n1+n2))
with wf_env : venv -> tenv -> Prop :=
| wfe_nil : wf_env nil nil
| wfe_cons : forall n v t vs ts nv,
val_type ((n,v)::vs) v t nv ->
wf_env vs ts ->
wf_env (cons (n,v) vs) (cons (n,t) ts)
with val_type : venv -> vl -> ty -> nat -> Prop :=
| v_bool: forall venv b TE,
(exists n, stp2 0 true [] TBool venv TE [] n) ->
val_type venv (vbool b) TE 1
| v_obj: forall env venv tenv f ds T TX TE,
wf_env venv tenv ->
open (varF f) T = TX ->
dcs_has_type ((f,TX)::tenv) f ds T ->
fresh venv = f ->
(exists n, stp2 0 true ((f, vobj venv f ds)::venv) TX env TE [] n)->
val_type env (vobj venv f ds) TE 1
| v_pack: forall venv venv3 x v T T2 T3 n,
index x venv = Some v ->
val_type venv v T n ->
open (varF x) T2 = T ->
(exists n, stp2 0 true venv (TBind T2) venv3 T3 [] n) ->
val_type venv3 v T3 (S n)
.
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
.
Definition stpd2 b G1 T1 G2 T2 GH := exists n, stp2 MAX b G1 T1 G2 T2 GH n.
Definition atpd2 b G1 T1 G2 T2 GH := exists n, stp2 1 b G1 T1 G2 T2 GH n.
Definition sstpd2 b G1 T1 G2 T2 GH := exists n, stp2 0 b G1 T1 G2 T2 GH n.
Definition valtpd G v T := exists n, val_type G v T n.
Ltac ep := match goal with
| [ |- stp2 ?M1 ?M2 ?G1 ?T1 ?G2 ?T2 ?GH ?N ] => assert (exists (x:nat), stp2 M1 M2 G1 T1 G2 T2 GH x) as EEX
end.
Ltac eu := match goal with
| H: stpd2 _ _ _ _ _ _ |- _ => destruct H as [? H]
| H: atpd2 _ _ _ _ _ _ |- _ => destruct H as [? H]
| H: sstpd2 _ _ _ _ _ _ |- _ => destruct H as [? H]
(* | H: exists n: nat , _ |- _ =>
destruct H as [e P] *)
end.
Hint Constructors stp2.
Hint Unfold stpd2.
Lemma stpd2_topx: forall G1 G2 GH,
stpd2 true G1 TTop G2 TTop GH.
Proof. intros. repeat exists (S 0). eauto. Qed.
Lemma stpd2_botx: forall G1 G2 GH,
stpd2 true G1 TBot G2 TBot GH.
Proof. intros. repeat exists (S 0). eauto. Qed.
Lemma stpd2_top: forall G1 G2 GH T,
stpd2 true G1 T G1 T GH ->
stpd2 true G1 T G2 TTop GH.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_bot: forall G1 G2 GH T,
stpd2 true G2 T G2 T GH ->
stpd2 true G1 TBot G2 T GH.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_bool: forall G1 G2 GH,
stpd2 true G1 TBool G2 TBool GH.
Proof. intros. repeat exists (S 0). eauto. Qed.
Lemma stpd2_mem: forall G1 G2 GH l T11 T12 T21 T22,
stpd2 false G2 T21 G1 T11 GH ->
stpd2 false G1 T12 G2 T22 GH ->
stpd2 true G1 (TMem l T11 T12) G2 (TMem l T21 T22) GH.
Proof. intros. repeat eu. eauto. unfold stpd2. eexists. eapply stp2_mem2; eauto. Qed.
Lemma stpd2_sel1: forall G1 G2 GX l TX x T2 GH v nv,
index x G1 = Some v ->
val_type GX v TX nv ->
closed 0 0 TX ->
stpd2 false GX TX G2 (TMem l TBot T2) GH ->
stpd2 true G2 T2 G2 T2 GH ->
stpd2 true G1 (TSel (varF x) l) G2 T2 GH.
Proof. intros. repeat eu. eexists. eapply stp2_sel1; eauto. Qed.
Lemma stpd2_selb1: forall G1 G2 GX l TX x x' T2 GH v nv,
index x G1 = Some v -> (index x' G2 = Some v \/ closed 0 0 T2) ->
val_type GX v TX nv ->
closed 0 0 TX ->
stpd2 false GX TX G2 (TBind (TMem l TBot T2)) [] -> (* Note GH = [] *)
stpd2 true G2 (open (varF x') T2) G2 (open (varF x') T2) GH ->
stpd2 true G1 (TSel (varF x) l) G2 (open (varF x') T2) GH.
Proof. intros. repeat eu. eexists. eapply stp2_selb1; eauto. Qed.
Lemma stpd2_sel2: forall G1 G2 GX l TX x T1 GH v nv,
index x G2 = Some v ->
val_type GX v TX nv ->
closed 0 0 TX ->
stpd2 false GX TX G1 (TMem l T1 TTop) GH ->
stpd2 true G1 T1 G1 T1 GH ->
stpd2 true G1 T1 G2 (TSel (varF x) l) GH.
Proof. intros. repeat eu. eexists. eapply stp2_sel2; eauto. Qed.
Lemma stpd2_selb2: forall G1 G2 GX l TX x x' T1 GH v nv,
index x G2 = Some v -> (index x' G1 = Some v \/ closed 0 0 T1) ->
val_type GX v TX nv ->
closed 0 0 TX ->
stpd2 false GX TX G1 (TBind (TMem l T1 TTop)) [] -> (* Note GH = [] *)
stpd2 true G1 (open (varF x') T1) G1 (open (varF x') T1) GH ->
stpd2 true G1 (open (varF x') T1) G2 (TSel (varF x) l) GH.
Proof. intros. repeat eu. eexists. eapply stp2_selb2; eauto. Qed.
Lemma stpd2_selx: forall G1 G2 l x1 x2 GH v,
index x1 G1 = Some v ->
index x2 G2 = Some v ->
stpd2 true G1 (TSel (varF x1) l) G2 (TSel (varF x2) l) GH.
Proof. intros. eauto. exists (S 0). eapply stp2_selx; eauto. Qed.
Lemma stpd2_selab1: forall G1 G2 GX l TX x T2 GH GU GL,
indexr x GH = Some (GX, TX) ->
closed 0 (S x) TX ->
closed 0 0 (TBind (TMem l TBot T2)) ->
length GL = (S x) ->
GH = GU ++ GL ->
stpd2 false GX TX G2 (TBind (TMem l TBot T2)) GL ->
stpd2 true G2 (open (varH x) T2) G2 (open (varH x) T2) GH ->
stpd2 true G1 (TSel (varH x) l) G2 (open (varH x) T2) GH.
Proof. intros. repeat eu. eauto. eexists. eapply stp2_selab1; eauto. Qed.
Lemma stpd2_selab2: forall G1 G2 GX l TX x T1 T1' GH GU GL,
indexr x GH = Some (GX, TX) ->
closed 0 (S x) TX ->
closed 0 0 (TBind (TMem l T1 TTop)) ->
length GL = (S x) ->
GH = GU ++ GL ->
stpd2 false GX TX G1 (TBind (TMem l T1 TTop)) GL ->
T1' = (open (varH x) T1) ->
stpd2 true G1 T1' G1 T1' GH ->
stpd2 true G1 T1' G2 (TSel (varH x) l) GH.
Proof. intros. repeat eu. eauto. eexists. eapply stp2_selab2; eauto. Qed.
Lemma stpd2_sela1: forall G1 G2 GX l TX x T2 GH,
indexr x GH = Some (GX, TX) ->
closed 0 (S x) TX ->
stpd2 false GX TX G2 (TMem l TBot T2) GH ->
stpd2 true G2 T2 G2 T2 GH ->
stpd2 true G1 (TSel (varH x) l) G2 T2 GH.
Proof. intros. repeat eu. eauto. eexists. eapply stp2_sela1; eauto. Qed.
Lemma stpd2_sela2: forall G1 G2 GX l TX x T1 GH,
indexr x GH = Some (GX, TX) ->
closed 0 (S x) TX ->
stpd2 false GX TX G1 (TMem l T1 TTop) GH ->
stpd2 true G1 T1 G1 T1 GH ->
stpd2 true G1 T1 G2 (TSel (varH x) l) GH.
Proof. intros. repeat eu. eauto. eexists. eapply stp2_sela2; eauto. Qed.
Lemma stpd2_selax: forall G1 G2 GX l TX x GH,
indexr x GH = Some (GX, TX) ->
stpd2 true G1 (TSel (varH x) l) G2 (TSel (varH x) l) GH.
Proof. intros. exists (S 0). eauto. eapply stp2_selax; eauto. Qed.
Lemma stpd2_all: forall G1 G2 m T1 T2 T3 T4 GH,
stpd2 false G2 T3 G1 T1 GH ->
closed 1 (length GH) T2 ->
closed 1 (length GH) T4 ->
stpd2 false G1 (open (varH (length GH)) T2) G1 (open (varH (length GH)) T2) ((0,(G1, T1))::GH) ->
stpd2 false G1 (open (varH (length GH)) T2) G2 (open (varH (length GH)) T4) ((0,(G2, T3))::GH) ->
stpd2 true G1 (TAll m T1 T2) G2 (TAll m T3 T4) GH.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_bind: forall G1 G2 T1 T2 GH,
closed 1 (length GH) T1 ->
closed 1 (length GH) T2 ->
stpd2 false G2 (open (varH (length GH)) T2) G2 (open (varH (length GH)) T2) ((0,(G2, open (varH (length GH)) T2))::GH) ->
stpd2 false G1 (open (varH (length GH)) T1) G2 (open (varH (length GH)) T2) ((0,(G1, open (varH (length GH)) T1))::GH) ->
stpd2 true G1 (TBind T1) G2 (TBind T2) GH.
Proof. intros. repeat eu. eauto. unfold stpd2. eexists. eapply stp2_bindb; eauto. Qed.
Lemma stpd2_and11: forall G1 G2 GH T1 T2 T,
stpd2 true G1 T1 G2 T GH ->
stpd2 true G1 T2 G1 T2 GH ->
stpd2 true G1 (TAnd T1 T2) G2 T GH.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_and12: forall G1 G2 GH T1 T2 T,
stpd2 true G1 T2 G2 T GH ->
stpd2 true G1 T1 G1 T1 GH ->
stpd2 true G1 (TAnd T1 T2) G2 T GH.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_and2: forall G1 G2 GH T1 T2 T,
stpd2 false G1 T G2 T1 GH ->
stpd2 false G1 T G2 T2 GH ->
stpd2 true G1 T G2 (TAnd T1 T2) GH.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_wrapf: forall G1 G2 T1 T2 GH,
stpd2 true G1 T1 G2 T2 GH ->
stpd2 false G1 T1 G2 T2 GH.
Proof. intros. repeat eu. eauto. Qed.
Lemma stpd2_transf: forall G1 G2 G3 T1 T2 T3 GH,
stpd2 true G1 T1 G2 T2 GH ->
stpd2 false G2 T2 G3 T3 GH ->
stpd2 false G1 T1 G3 T3 GH.
Proof. intros. repeat eu. eauto. Qed.
Lemma sstpd2_wrapf: forall G1 G2 T1 T2 GH,
sstpd2 true G1 T1 G2 T2 GH ->
sstpd2 false G1 T1 G2 T2 GH.
Proof. intros. repeat eu. eexists. eapply stp2_wrapf. eauto. Qed.
Lemma sstpd2_transf: forall G1 G2 G3 T1 T2 T3 GH,
sstpd2 true G1 T1 G2 T2 GH ->
sstpd2 false G2 T2 G3 T3 GH ->
sstpd2 false G1 T1 G3 T3 GH.
Proof. intros. repeat eu. eexists. eapply stp2_transf; eauto. Qed.
Lemma atpd2_transf: forall G1 G2 G3 T1 T2 T3 GH,
atpd2 true G1 T1 G2 T2 GH ->
atpd2 false G2 T2 G3 T3 GH ->
atpd2 false G1 T1 G3 T3 GH.
Proof. intros. repeat eu. eexists. eapply stp2_transf; eauto. Qed.
(*
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)
| tobj f ds => Some (Some (vobj env f ds))
| tapp ef m 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 (vobj env2 f ds)) =>
match index m ds with
| None => Some None
| Some (dmem _) => Some None
| Some (dfun x ey) =>
teval n ((x,vx)::(f,vobj env2 f ds)::env2) ey
end
end
end
| tlet x ex ey =>
match teval n env ex with
| None => None
| Some None => Some None
| Some (Some vx) => teval n ((x,vx)::env) ey
end
end
end.
Hint Constructors ty.
Hint Constructors tm.
Hint Constructors vl.
Hint Constructors closed_rec.
Hint Constructors has_type dcs_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 [(eapply stp_and2; [eapply stp_and11; crush2 | eapply stp_and12; crush2])];
try solve [(econstructor; compute; eauto; crush2)];
try solve [(eapply t_sub; eapply t_var; compute; eauto; crush2)].
Ltac crush_cl :=
try solve [(econstructor; compute; eauto; crush_cl)].
Ltac crush_wf :=
try solve [(eapply stp_topx; crush_wf)];
try solve [(eapply stp_botx; crush_wf)];
try solve [(eapply stp_bool; crush_wf)];
try solve [(eapply stp_selx; compute; eauto; crush_wf)];
try solve [(eapply stp_selax; compute; eauto; crush_wf)];
try solve [(eapply stp_mem; crush_wf)];
try solve [(eapply stp_all; [crush_wf | (compute; eauto) | crush_cl | crush_cl | crush_wf | crush_wf])];
try solve [(eapply stp_bindx; [(compute; eauto) | crush_cl | crush_cl | crush_wf | crush_wf])];
try solve [(eapply stp_and2; [eapply stp_and11; crush_wf | eapply stp_and12; crush_wf])].
(* define polymorphic identity function *)
Definition polyId := TAll 0 (TBind (TMem 0 TBot TTop)) (TAll 0 (TSel (varB 0) 0) (TSel (varB 1) 0)).
Example ex1: has_type [] (tlet 0 (tobj 0 [(0, dfun 1 (tlet 2 (tobj 2 [(0, (dfun 3 (tvar 3)))]) (tvar 2)))]) (tvar 0)) polyId.
Proof.
apply t_let with (Tx:=(TBind polyId)).
apply t_obj with (TX:=polyId).
eauto. compute. reflexivity.
eapply dt_fun with (T1:=(TBind (TMem 0 TBot TTop))) (T2:=TAll 0 (TSel (varB 0) 0) (TSel (varB 1) 0)).
unfold open. simpl. apply t_let with (Tx:=(TBind (TAll 0 (TSel (varF 1) 0) (TSel (varF 1) 0)))); crush2.
assert ((open (varF 2) (TAll 0 (TSel (varF 1) 0) (TSel (varF 1) 0)))=(TAll 0 (TSel (varF 1) 0) (TSel (varF 1) 0))) as A. { unfold open. simpl. reflexivity. }
rewrite <- A.
eapply t_var_unpack; crush2.
eapply dt_nil.
crush2. crush2. crush2.
unfold polyId. crush_wf.
unfold polyId. crush_wf. crush2.
assert (open (varF 0) polyId=polyId) as A. { unfold open. simpl. reflexivity. }
rewrite <- A at 2.
eapply t_var_unpack; crush2.
unfold polyId. crush_wf.
Qed.
(* instantiate it to bool *)
Example ex2: has_type [(0,polyId)] (tapp (tvar 0) 0 (tlet 1 (tobj 1 [(0,dmem TBool)]) (tvar 1))) (TAll 0 TBool TBool).
Proof.
eapply t_app. instantiate (1:= (TBind (TMem 0 TBool TBool))).
{ eapply t_sub.
{ eapply t_var. simpl. eauto. crush2. }
{ eapply stp_all; eauto. { eapply stp_bindx; crush2. } compute. eapply cl_all; eauto.
eapply stp_all. compute. eapply stp_selax; crush2. crush2. crush2. crush2.
simpl. unfold open. simpl. eapply stp_selax; crush2. crush2.
eapply stp_all. compute. eapply stp_selab2. crush2.
crush2. instantiate (1:=TBool). crush2.
instantiate (1:=[(0, TBind (TMem 0 TBool TBool))]). simpl. reflexivity.
rewrite app_nil_l. reflexivity.
crush2. crush2. crush2. crush2. crush2. crush2. crush2.
simpl. unfold open.
simpl. eapply stp_selab1. crush2.
crush2. instantiate (1:=TBool). crush2.
instantiate (1:=[(0, TBind (TMem 0 TBool TBool))]). simpl. reflexivity.
instantiate (1:=[(0, TBool)]). simpl. reflexivity.
crush2. crush2. crush2.
}
}
{ eapply t_let; crush2. }
crush2.
Qed.
(* define brand / unbrand client function *)
Definition brandUnbrand :=
TAll 0
(TBind (TMem 0 TBot TTop))
(TBind (TAll 0
(TBind (TAnd
(TAll 1 TBool (TSel (varB 3) 0)) (* brand *)
(TAll 0 (TSel (varB 2) 0) TBool) (* unbrand *)
)
)
TBool)).
Example ex3:
has_type []
(tlet 0
(tobj 0 [(0, dfun 1
(tobj 2 [(0, dfun 3
(tapp (tvar 3) 0 (tapp (tvar 3) 1 ttrue)))]))])
(tvar 0))
brandUnbrand.
Proof.
apply t_let with (Tx:=(TBind brandUnbrand)).
apply t_obj with (TX:=brandUnbrand).
eauto. compute. reflexivity.
eapply dt_fun with (T1:=TBind (TMem 0 TBot TTop))
(T2:= (TBind (TAll 0