-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof-alpha-type.v
1868 lines (1747 loc) · 87.4 KB
/
proof-alpha-type.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 lemmas.
Definition var_id := nat.
Definition class_id := nat.
Definition obj_id : class_id := 0.
Inductive var : Type :=
| Var : var_id -> var
| This : var.
Definition beq_var (v v' : var) : bool :=
match v, v' with
| This, This => true
| Var vi, Var vi' => beq_nat vi vi'
| _, _ => false
end.
Theorem beq_var_refl : forall v, true = beq_var v v.
Proof. intros. destruct v; eauto. simpl. apply beq_nat_refl. Qed.
Theorem beq_var_true : forall v v', v = v' <-> true = beq_var v v'.
Proof.
intros. split; intro; destruct v; destruct v'; inversion H; eauto.
- apply beq_var_refl.
- symmetry in H1. apply beq_nat_true in H1. eauto.
Qed.
Definition field_id := nat.
Definition method_id := nat.
Inductive expr : Type :=
| E_Var : var -> expr
| E_Field : expr -> field_id -> expr
| E_Invk : expr -> method_id -> list expr -> expr
| E_New : class_id -> list expr -> expr
| E_Cast : class_id -> expr -> expr
| E_Lib : expr.
Definition expr_rect_list :=
fun (P : expr -> Type) (Q : list expr -> Type)
(g : Q nil) (h : forall e l, P e -> Q l -> Q (e :: l))
(f : forall v : var, P (E_Var v))
(f0 : forall e : expr, P e -> forall f0 : field_id, P (E_Field e f0))
(f1 : forall (c : class_id) (l : list expr), Q l -> P (E_New c l))
(f2 : forall e : expr, P e -> forall (m : method_id) (l : list expr), Q l -> P (E_Invk e m l))
(f3 : forall (c : class_id) (e : expr), P e -> P (E_Cast c e)) (f4 : P E_Lib) =>
fix F (e : expr) : P e :=
match e as e0 return (P e0) with
| E_Var v => f v
| E_Field e0 f5 => f0 e0 (F e0) f5
| E_New c l => f1 c l (list_rect Q g (fun u r => h u r (F u)) l)
| E_Invk e0 m l => f2 e0 (F e0) m l (list_rect Q g (fun u r => h u r (F u)) l)
| E_Cast c e0 => f3 c e0 (F e0)
| E_Lib => f4
end.
Definition expr_ind_list :=
fun (P : expr -> Prop) => expr_rect_list P (Forall P) (Forall_nil P) (@Forall_cons expr P).
Definition expr_ind_list_t :=
fun (P : expr -> Type) => expr_rect_list P (Forallt P) (Forallt_nil P) (@Forallt_cons expr P).
Definition context := var -> class_id.
Definition params := list (class_id * var_id).
Inductive constr : Type :=
| Constr : class_id -> params -> list var_id -> list (field_id * var_id) -> constr.
Inductive method := Method : class_id -> method_id -> params -> expr -> method.
Inductive class : Type :=
| Obj : class
| C : class_id -> class_id -> list (class_id * field_id) -> constr -> list method -> class.
Section table_defs.
Definition class_table : Type := (class_id -> option class) * (class_id -> option class).
Definition empty_map : class_id -> option class := fun _ => None.
Definition ct_app (ct : class_table) : class_table :=
match ct with (app, _) => (app, empty_map) end.
Definition ct_lib (ct : class_table) : class_table :=
match ct with (_, lib) => (empty_map, lib) end.
Definition lookup (ct : class_table) (i : class_id) : option class :=
match ct with (app, lib) =>
match app i with
| Some c => Some c
| None => lib i
end
end.
Definition lookup_method (c : class) (mi : method_id) : option method :=
match c with
| Obj => None
| C _ _ _ _ ms => find (fun m => match m with Method _ mi' _ _ => beq_nat mi mi' end) ms
end.
Definition id_of (c : class) : class_id :=
match c with
| Obj => obj_id
| C ci _ _ _ _ => ci
end.
Definition par_id_of (c : class) : option class_id :=
match c with
| Obj => None
| C _ di _ _ _ => Some di
end.
Definition in_lib_id (ct : class_table) (i : class_id) : Prop :=
match ct with (_, lib) => exists c, lib i = Some c end.
Definition in_app_id (ct : class_table) (i : class_id) : Prop :=
match ct with (app, _) => exists c, app i = Some c end.
Definition in_lib (ct : class_table) (c : class) : Prop :=
match ct with (_, lib) => lib (id_of c) = Some c end.
Definition in_app (ct : class_table) (c : class) : Prop :=
match ct with (app, _) => app (id_of c) = Some c end.
Definition in_table_id (ct : class_table) (i : class_id) : Prop :=
in_app_id ct i \/ in_lib_id ct i.
Definition in_table (ct : class_table) (c : class) : Prop :=
in_app ct c \/ in_lib ct c.
End table_defs.
Section table_lemmas.
Lemma in_app_in_table : forall (ct : class_table) (c : class),
in_app ct c -> in_table ct c.
Proof. intros. unfold in_table. left. apply H. Qed.
Lemma in_lib_in_table : forall (ct : class_table) (c : class),
in_lib ct c -> in_table ct c.
Proof. intros. unfold in_table. right. apply H. Qed.
Lemma in_app_in_table_id : forall (ct : class_table) (ci : class_id),
in_app_id ct ci -> in_table_id ct ci.
Proof. intros. unfold in_table_id. left. apply H. Qed.
Lemma in_lib_in_table_id : forall (ct : class_table) (ci : class_id),
in_lib_id ct ci -> in_table_id ct ci.
Proof. intros. unfold in_table_id. right. apply H. Qed.
End table_lemmas.
Definition extends (ct : class_table) (c d : class) : Prop :=
match c with
| Obj => False
| C _ di _ _ _ => lookup ct di = Some d
end.
Definition extends_id (ct : class_table) (ci di : class_id) : Prop :=
match lookup ct ci with
| None => False
| Some Obj => False
| Some (C _ di' _ _ _) => di = di'
end.
Inductive subtype (ct : class_table) : class -> class -> Type :=
| S_Ref : forall (c : class), subtype ct c c
| S_Trans : forall (c d e : class), subtype ct c d -> subtype ct d e -> subtype ct c e
| S_Sub : forall (c d : class), extends ct c d -> subtype ct c d.
Inductive subtype_id (ct : class_table) : class_id -> class_id -> Prop :=
| SI_Ref : forall ci, in_table_id ct ci -> subtype_id ct ci ci
| SI_Sub : forall ci di, extends_id ct ci di -> subtype_id ct ci di
| SI_Trans : forall ci di ei, subtype_id ct ci di -> subtype_id ct di ei -> subtype_id ct ci ei.
Inductive valid_class (ct : class_table) : class -> Type :=
| ValidObj : in_lib ct Obj -> ~ in_app ct Obj -> valid_class ct Obj
| ValidParent : forall c d,
valid_class ct d -> extends ct c d ->
~ (in_lib ct c /\ in_app ct c) ->
(in_lib ct d /\ in_table ct c) \/ (in_app ct d /\ in_app ct c) ->
valid_class ct c.
Definition body (m : method) : expr := match m with Method _ _ _ e => e end.
Definition args (m : method) : list var :=
match m with
| Method _ _ l _ => map Var (map snd l)
end.
Definition mid (m : method) : method_id := match m with Method _ mi _ _ => mi end.
Fixpoint find_method {ct : class_table} {c : class} (mi : method_id) (pfc : valid_class ct c) : option method :=
match pfc with
| ValidObj _ _ _ => None
| ValidParent _ _ _ pfd _ _ _ => match lookup_method c mi with
| Some m => Some m
| None => find_method mi pfd
end
end.
Fixpoint mtype (ct : class_table) (mi : method_id) (c : class) (pfc : valid_class ct c) :
option (list class_id * class_id) :=
match pfc with
| ValidObj _ _ _ => None
| ValidParent _ _ d pfd _ _ _ =>
match lookup_method c mi with
| None => mtype ct mi d pfd
| Some (Method ci _ l_arg _) => Some (map fst l_arg, ci)
end
end.
Fixpoint fields (ct : class_table) (c : class) (pfc : valid_class ct c) : list class_id :=
match pfc, c with
| ValidParent _ _ d pfd _ _ _ , C _ _ fs _ _ => map fst fs ++ fields ct d pfd
| _, C _ _ fs _ _ => map fst fs
| _, _ => nil
end.
Fixpoint field_ids (ct : class_table) (c : class) (pfc : valid_class ct c) : list field_id :=
match pfc, c with
| ValidParent _ _ d pfd _ _ _ , C _ _ fs _ _ => map snd fs ++ field_ids ct d pfd
| _, C _ _ fs _ _ => map fst fs
| _, _ => nil
end.
Definition lookup_field (ct : class_table) (c : class) (pfc : valid_class ct c) (f : field_id) : option class_id :=
find (beq_nat f) (fields ct c pfc).
Inductive type_of (ct : class_table) (gamma : context) : expr -> class_id -> Prop :=
| T_Var : forall x, type_of ct gamma (E_Var x) (gamma x)
| T_Field : forall e c pfc di f,
type_of ct gamma e (id_of c) -> lookup_field ct c pfc f = Some di ->
type_of ct gamma (E_Field e f) di
| T_Invk : forall e0 c0 ci0 pfc0 ld ret mi le lc,
id_of c0 = ci0 -> type_of ct gamma e0 ci0 -> mtype ct mi c0 pfc0 = Some (ld, ret) ->
length ld = length le -> length le = length lc ->
Forall (fun p => type_of ct gamma (fst p) (snd p)) (combine le lc) ->
Forall (fun p => subtype_id ct (fst p) (snd p)) (combine lc ld) ->
type_of ct gamma (E_Invk e0 mi le) ci0
| T_New : forall c ci pfc le lc ld,
id_of c = ci -> fields ct c pfc = ld ->
length ld = length le -> length le = length lc ->
Forall (fun p => type_of ct gamma (fst p) (snd p)) (combine le lc) ->
Forall (fun p => subtype_id ct (fst p) (snd p)) (combine lc ld) ->
type_of ct gamma (E_New ci le) ci
| T_Cast : forall e0 ci di, type_of ct gamma e0 di -> type_of ct gamma (E_Cast ci e0) ci.
Definition type_of_ind_list :=
fun (ct : class_table) (gamma : context) (P : expr -> class_id -> Prop) (f : forall x : var, P (E_Var x) (gamma x))
(f0 : forall (e : expr) (c : class) (pfc : valid_class ct c) (di : class_id) (f0 : field_id),
type_of ct gamma e (id_of c) -> P e (id_of c) -> lookup_field ct c pfc f0 = Some di -> P (E_Field e f0) di)
(f1 : forall (c : class) (ci : class_id) (pfc : valid_class ct c) (le : list expr) (lc ld : list class_id),
id_of c = ci ->
fields ct c pfc = ld ->
length ld = length le ->
length le = length lc ->
Forall (fun p : expr * class_id => type_of ct gamma (fst p) (snd p)) (combine le lc) ->
Forall (fun p : class_id * class_id => subtype_id ct (fst p) (snd p)) (combine lc ld) ->
Forall (fun p : expr * class_id => P (fst p) (snd p)) (combine le lc) ->
P (E_New ci le) ci)
(f2 : forall (e0 : expr) (c0 : class) (ci0 : class_id) (pfc0 : valid_class ct c0) (ld : list class_id)
(ret : class_id) (mi : method_id) (le : list expr) (lc : list class_id),
id_of c0 = ci0 ->
type_of ct gamma e0 ci0 ->
P e0 ci0 ->
mtype ct mi c0 pfc0 = Some (ld, ret) ->
length ld = length le ->
length le = length lc ->
Forall (fun p : expr * class_id => type_of ct gamma (fst p) (snd p)) (combine le lc) ->
Forall (fun p : class_id * class_id => subtype_id ct (fst p) (snd p)) (combine lc ld) ->
Forall (fun p : expr * class_id => P (fst p) (snd p)) (combine le lc) ->
P (E_Invk e0 mi le) ci0)
(f3 : forall (e0 : expr) (ci di : class_id), type_of ct gamma e0 di -> P e0 di -> P (E_Cast ci e0) ci) =>
fix F (e : expr) (c : class_id) (t : type_of ct gamma e c) {struct t} : P e c :=
match t in (type_of _ _ e0 c0) return (P e0 c0) with
| T_Var _ _ x => f x
| T_Field _ _ e0 c0 pfc di f4 t0 e1 => f0 e0 c0 pfc di f4 t0 (F e0 (id_of c0) t0) e1
| T_New _ _ c0 ci pfc le lc ld e0 e1 e2 e3 f4 f5 =>
f1 c0 ci pfc le lc ld e0 e1 e2 e3 f4 f5
(let Q := (fun p => type_of ct gamma (fst p) (snd p)) in
let P' := (fun p => P (fst p) (snd p)) in
(fix f_impl (l : list (expr * class_id)) (F_Q : Forall Q l) : Forall P' l :=
match F_Q with
| Forall_nil _ => Forall_nil P'
| @Forall_cons _ _ p l' Qp Ql' => Forall_cons p (F (fst p) (snd p) Qp) (f_impl l' Ql')
end) (combine le lc) f4)
| T_Invk _ _ e0 c0 ci0 pfc0 ld ret mi le lc e1 t0 e2 e3 e4 f4 f5 =>
f2 e0 c0 ci0 pfc0 ld ret mi le lc e1 t0 (F e0 ci0 t0) e2 e3 e4 f4 f5
(let Q := (fun p => type_of ct gamma (fst p) (snd p)) in
let P' := (fun p => P (fst p) (snd p)) in
(fix f_impl (l : list (expr * class_id)) (F_Q : Forall Q l) : Forall P' l :=
match F_Q with
| Forall_nil _ => Forall_nil P'
| @Forall_cons _ _ p l' Qp Ql' => Forall_cons p (F (fst p) (snd p) Qp) (f_impl l' Ql')
end) (combine le lc) f4)
| T_Cast _ _ e0 ci di t0 => f3 e0 ci di t0 (F e0 di t0)
end.
Inductive type_checks (ct : class_table) (gamma : context) : expr -> Prop :=
| TC_Var : forall x, in_table_id ct (gamma x) -> type_checks ct gamma (E_Var x)
| TC_Field : forall e f di, type_of ct gamma (E_Field e f) di -> in_table_id ct di -> type_checks ct gamma e ->
type_checks ct gamma (E_Field e f)
| TC_New : forall ci le, in_table_id ct ci -> Forall (type_checks ct gamma) le -> type_checks ct gamma (E_New ci le)
| TC_Invk : forall e m le di, type_of ct gamma (E_Invk e m le) di -> in_table_id ct di ->
type_checks ct gamma e -> Forall (type_checks ct gamma) le ->
type_checks ct gamma (E_Invk e m le)
| TC_Cast : forall e di, in_table_id ct di -> type_checks ct gamma e -> type_checks ct gamma (E_Cast di e).
Definition dfields (c : class) : list field_id :=
match c with
| Obj => nil
| C _ _ fs _ _ => map snd fs
end.
Fixpoint declaring_class (ct : class_table) (c : class) (pfc : valid_class ct c) (fi : field_id) : option class_id :=
match pfc with
| ValidObj _ _ _ => None
| ValidParent _ _ d pfd _ _ _ => if (@existsb field_id (beq_nat fi) (dfields c))
then Some (id_of c)
else declaring_class ct d pfd fi
end.
Definition dmethods (c : class) : list method :=
match c with
| Obj => nil
| C _ _ _ _ ms => ms
end.
Definition dmethods_id (c : class) : list method_id :=
match c with
| Obj => nil
| C _ _ _ _ ms => map (fun m => match m with Method _ id _ _ => id end) ms
end.
Fixpoint mresolve (ct : class_table) (mi : method_id) (c : class) (pfc : valid_class ct c) : option class :=
match pfc with
| ValidObj _ _ _ => None
| ValidParent _ _ d pfd _ _ _ => if (existsb (beq_nat mi) (dmethods_id c))
then Some c
else mresolve ct mi d pfd
end.
Inductive valid_expr (ct : class_table) (gamma : context) : expr -> Prop :=
| Val_Var : forall x, type_checks ct gamma (E_Var x) -> valid_expr ct gamma (E_Var x)
| Val_Field : forall e f ci c pfc,
type_checks ct gamma (E_Field e f) ->
type_of ct gamma e ci -> id_of c = ci ->
(exists di, declaring_class ct c pfc f = Some di) ->
valid_expr ct gamma e ->
valid_expr ct gamma (E_Field e f)
| Val_New : forall ci le,
type_checks ct gamma (E_New ci le) ->
Forall (valid_expr ct gamma) le ->
valid_expr ct gamma (E_New ci le)
| Val_Invk : forall e m le ci c pfc,
type_checks ct gamma (E_Invk e m le) ->
valid_expr ct gamma e ->
Forall (valid_expr ct gamma) le ->
type_of ct gamma e ci -> id_of c = ci ->
(exists d, mresolve ct m c pfc = Some d) ->
valid_expr ct gamma (E_Invk e m le)
| Val_Cast : forall ci e,
type_checks ct gamma (E_Cast ci e) -> valid_expr ct gamma e -> valid_expr ct gamma (E_Cast ci e)
| Val_Lib : valid_expr ct gamma E_Lib.
Inductive fj_expr : expr -> Prop :=
| FJ_Var : forall x, fj_expr (E_Var x)
| FJ_Field : forall e f, fj_expr e -> fj_expr (E_Field e f)
| FJ_New : forall c le, Forall fj_expr le -> fj_expr (E_New c le)
| FJ_Invk : forall e m le,
fj_expr e -> Forall fj_expr le ->
fj_expr (E_Invk e m le)
| FJ_Cast : forall e d, fj_expr e -> fj_expr (E_Cast d e).
Inductive expr_In : var -> expr -> Prop :=
| EIn_Var : forall v, expr_In v (E_Var v)
| EIn_Field : forall v e f, expr_In v e -> expr_In v (E_Field e f)
| EIn_New : forall v le c, Exists (expr_In v) le -> expr_In v (E_New c le)
| EIn_Invk : forall v e m le, Exists (expr_In v) le \/ expr_In v e -> expr_In v (E_Invk e m le)
| EIn_Cast : forall v e c, expr_In v e -> expr_In v (E_Cast c e).
Definition valid_method (m : method) : Prop :=
match m with
| Method _ _ lcv e =>
forall v, expr_In v e -> v = This \/ In v (map Var (map snd lcv))
end.
Inductive valid_table : class_table -> Prop :=
| VT : forall ct, (forall c, in_table ct c -> valid_class ct c) ->
(forall c, in_table ct c -> lookup ct (id_of c) = Some c) ->
(forall ci c, lookup ct ci = Some c -> ci = id_of c) ->
(forall ci, ~ (in_lib_id ct ci /\ in_app_id ct ci)) ->
(forall c pfc c' pfc' fi di di',
declaring_class ct c pfc fi = Some di ->
declaring_class ct c' pfc' fi = Some di' ->
di = di') ->
(forall c pfc c' pfc' mi di di',
mresolve ct mi c pfc = Some di ->
mresolve ct mi c' pfc' = Some di' ->
di = di') ->
valid_table ct.
Lemma extends_injective : forall ct c d d',
valid_class ct c -> extends ct c d -> extends ct c d' -> d = d'.
Proof.
intros ct c d d' pfc c_ext_d c_ext_d'.
unfold extends in c_ext_d, c_ext_d'. destruct c as [| ci di k ms fs].
- inversion c_ext_d.
- rewrite c_ext_d in c_ext_d'. inversion c_ext_d'. reflexivity.
Qed.
Lemma mres_lib_in_lib : forall (ct : class_table) (mi : method_id) (c e : class) (pfc : valid_class ct c),
in_lib ct c -> mresolve ct mi c pfc = Some e -> in_lib ct e.
intros ct mi c e pfc c_in_lib c_res_e.
induction pfc.
- inversion c_res_e.
- unfold mresolve in c_res_e. destruct (existsb (beq_nat mi) (dmethods_id c)) in c_res_e.
+ inversion c_res_e. rewrite <- H0. apply c_in_lib.
+ fold mresolve in c_res_e. apply IHpfc.
* destruct o.
-- destruct H. apply H.
-- destruct n. split. apply c_in_lib. destruct H. apply H0.
* apply c_res_e.
Qed.
Inductive ave_rel (ct ct' : class_table) : Prop :=
| AveRel : valid_table ct -> valid_table ct' ->
(forall c, in_app ct c <-> in_app ct' c) ->
(forall c d, (extends ct c d /\ in_table ct' c) <-> extends ct' c d) ->
(forall d, (exists c, extends ct c d /\ in_lib ct d /\ in_table ct' c) <-> in_lib ct' d) ->
ave_rel ct ct'.
Inductive alpha (ct ct' : class_table) (gamma : context) : expr -> expr -> set expr -> Type :=
| Rel_Field : forall e e' f lpt,
alpha ct ct' gamma e e' lpt ->
alpha ct ct' gamma (E_Field e f) (E_Field e' f) lpt
| Rel_Lib_Field : forall e c pfc di f lpt,
alpha ct ct' gamma e E_Lib lpt ->
declaring_class ct c pfc f = Some di -> in_lib_id ct di ->
type_of ct gamma e (id_of c) ->
alpha ct ct' gamma (E_Field e f) E_Lib lpt
| Rel_New : forall ci le le' lpt,
in_table_id ct' ci -> length le = length le' ->
Forallt (fun p => alpha ct ct' gamma (fst p) (snd p) lpt) (combine le le') ->
alpha ct ct' gamma (E_New ci le) (E_New ci le') lpt
| Rel_Lib_New : forall ci le lpt,
in_lib_id ct ci -> Forallt (fun e => alpha ct ct' gamma e E_Lib lpt) le ->
alpha ct ct' gamma (E_New ci le) E_Lib lpt
| Rel_Invk : forall e e' le le' m lpt,
alpha ct ct' gamma e e' lpt ->
length le = length le' ->
Forallt (fun p => alpha ct ct' gamma (fst p) (snd p) lpt) (combine le le') ->
alpha ct ct' gamma (E_Invk e m le) (E_Invk e' m le') lpt
| Rel_Lib_Invk : forall e le mi lpt,
(exists c, in_lib ct c /\ In mi (dmethods_id c)) ->
alpha ct ct' gamma e E_Lib lpt ->
Forallt (fun e' => alpha ct ct' gamma e' E_Lib lpt) le ->
alpha ct ct' gamma (E_Invk e mi le) E_Lib lpt
| Rel_Cast : forall e e' c lpt,
alpha ct ct' gamma e e' lpt ->
alpha ct ct' gamma (E_Cast c e) (E_Cast c e') lpt
| Rel_Lib_Cast : forall e ci lpt,
alpha ct ct' gamma e E_Lib lpt -> alpha ct ct' gamma (E_Cast ci e) E_Lib lpt
| Rel_Lpt : forall e e' lpt,
alpha ct ct' gamma e e' lpt -> set_In e' lpt -> alpha ct ct' gamma e E_Lib lpt.
Definition alpha_ind_list :=
fun (ct ct' : class_table) (gamma : context) (P : expr -> expr -> set expr -> Type)
(f : forall (e e' : expr) (f : field_id) (lpt : set expr),
alpha ct ct' gamma e e' lpt -> P e e' lpt -> P (E_Field e f) (E_Field e' f) lpt)
(f0 : forall (e : expr) (c : class) (pfc : valid_class ct c) (di : class_id) (f0 : field_id) (lpt : set expr),
alpha ct ct' gamma e E_Lib lpt ->
P e E_Lib lpt ->
declaring_class ct c pfc f0 = Some di ->
in_lib_id ct di -> type_of ct gamma e (id_of c) -> P (E_Field e f0) E_Lib lpt)
(f1 : forall (ci : class_id) (le le' : list expr) (lpt : set expr),
in_table_id ct' ci ->
length le = length le' ->
Forallt (fun p : expr * expr => alpha ct ct' gamma (fst p) (snd p) lpt) (combine le le') ->
Forallt (fun p : expr * expr => P (fst p) (snd p) lpt) (combine le le') ->
P (E_New ci le) (E_New ci le') lpt)
(f2 : forall (ci : class_id) (le : list expr) (lpt : set expr),
in_lib_id ct ci ->
Forallt (fun e : expr => alpha ct ct' gamma e E_Lib lpt) le ->
Forallt (fun e : expr => P e E_Lib lpt) le ->
P (E_New ci le) E_Lib lpt)
(f3 : forall (e e' : expr) (le le' : list expr) (m : method_id) (lpt : set expr),
alpha ct ct' gamma e e' lpt ->
P e e' lpt ->
length le = length le' ->
Forallt (fun p : expr * expr => alpha ct ct' gamma (fst p) (snd p) lpt) (combine le le') ->
Forallt (fun p : expr * expr => P (fst p) (snd p) lpt) (combine le le') ->
P (E_Invk e m le) (E_Invk e' m le') lpt)
(f4 : forall (e : expr) (le : list expr) (mi : method_id) (lpt : set expr),
(exists c : class, in_lib ct c /\ In mi (dmethods_id c)) ->
alpha ct ct' gamma e E_Lib lpt ->
P e E_Lib lpt ->
Forallt (fun e' : expr => alpha ct ct' gamma e' E_Lib lpt) le ->
Forallt (fun e : expr => P e E_Lib lpt) le ->
P (E_Invk e mi le) E_Lib lpt)
(f5 : forall (e e' : expr) (c : class_id) (lpt : set expr),
alpha ct ct' gamma e e' lpt -> P e e' lpt -> P (E_Cast c e) (E_Cast c e') lpt)
(f6 : forall (e : expr) (ci : class_id) (lpt : set expr),
alpha ct ct' gamma e E_Lib lpt -> P e E_Lib lpt -> P (E_Cast ci e) E_Lib lpt)
(f7 : forall (e e' : expr) (lpt : set expr),
alpha ct ct' gamma e e' lpt -> P e e' lpt -> set_In e' lpt -> P e E_Lib lpt) =>
fix F (e e0 : expr) (s : set expr) (a : alpha ct ct' gamma e e0 s) {struct a} : P e e0 s :=
match a in (alpha _ _ _ e1 e2 s0) return (P e1 e2 s0) with
| Rel_Field _ _ _ e1 e' f8 lpt a0 => f e1 e' f8 lpt a0 (F e1 e' lpt a0)
| Rel_Lib_Field _ _ _ e1 c pfc di f8 lpt a0 e2 i t => f0 e1 c pfc di f8 lpt a0 (F e1 E_Lib lpt a0) e2 i t
| Rel_New _ _ _ ci le le' lpt i e1 f8 =>
f1 ci le le' lpt i e1 f8
(let Q := (fun p => alpha ct ct' gamma (fst p) (snd p) lpt) in
let P' := (fun p => P (fst p) (snd p) lpt) in
(fix f_impl (l : list (expr * expr)) (F_Q : Forallt Q l) : Forallt P' l :=
match F_Q with
| Forallt_nil _ => Forallt_nil P'
| @Forallt_cons _ _ p l' Qp Ql' => Forallt_cons p (F (fst p) (snd p) lpt Qp) (f_impl l' Ql')
end) (combine le le') f8)
| Rel_Lib_New _ _ _ ci le lpt i f8 =>
f2 ci le lpt i f8
(let Q := (fun e => alpha ct ct' gamma e E_Lib lpt) in
let P' := (fun e => P e E_Lib lpt) in
(fix f_impl (l : list expr) (F_Q : Forallt Q l) : Forallt P' l :=
match F_Q with
| Forallt_nil _ => Forallt_nil P'
| @Forallt_cons _ _ e l' Qe Ql' => Forallt_cons e (F e E_Lib lpt Qe) (f_impl l' Ql')
end) le f8)
| Rel_Invk _ _ _ e1 e' le le' m lpt a0 e2 f8 =>
f3 e1 e' le le' m lpt a0 (F e1 e' lpt a0) e2 f8
(let Q := (fun p => alpha ct ct' gamma (fst p) (snd p) lpt) in
let P' := (fun p => P (fst p) (snd p) lpt) in
(fix f_impl (l : list (expr * expr)) (F_Q : Forallt Q l) : Forallt P' l :=
match F_Q with
| Forallt_nil _ => Forallt_nil P'
| @Forallt_cons _ _ p l' Qp Ql' => Forallt_cons p (F (fst p) (snd p) lpt Qp) (f_impl l' Ql')
end) (combine le le') f8)
| Rel_Lib_Invk _ _ _ e1 le mi lpt e2 a0 f8 =>
f4 e1 le mi lpt e2 a0 (F e1 E_Lib lpt a0) f8
(let Q := (fun e => alpha ct ct' gamma e E_Lib lpt) in
let P' := (fun e => P e E_Lib lpt) in
(fix f_impl (l : list expr) (F_Q : Forallt Q l) : Forallt P' l :=
match F_Q with
| Forallt_nil _ => Forallt_nil P'
| @Forallt_cons _ _ e l' Qe Ql' => Forallt_cons e (F e E_Lib lpt Qe) (f_impl l' Ql')
end) le f8)
| Rel_Cast _ _ _ e1 e' c lpt a0 => f5 e1 e' c lpt a0 (F e1 e' lpt a0)
| Rel_Lib_Cast _ _ _ e1 ci lpt a0 => f6 e1 ci lpt a0 (F e1 E_Lib lpt a0)
| Rel_Lpt _ _ _ e1 e' lpt a0 s0 => f7 e1 e' lpt a0 (F e1 e' lpt a0) s0
end.
Definition subst := list (var * expr).
Fixpoint do_sub (sig : subst) (e : expr) : expr :=
match e with
| E_Var v => match find (fun p => beq_var (fst p) v) sig with
| Some (_, e') => e'
| None => e
end
| E_Field e' f => E_Field (do_sub sig e') f
| E_New c es => E_New c (map (do_sub sig) es)
| E_Invk e' m es => E_Invk (do_sub sig e') m (map (do_sub sig) es)
| E_Cast c e' => E_Cast c (do_sub sig e')
| E_Lib => E_Lib
end.
Definition valid_sub (ct : class_table) (gamma : context) (sig : subst) : Prop :=
Forall (fun p => type_of ct gamma (snd p) (gamma (fst p))) sig.
Print expr.
Inductive calPexpr (ct' : class_table) (gamma : context) : expr -> Prop :=
| P_Var : forall v, calPexpr ct' gamma (E_Var v)
| P_Field : forall e fi c pfc' di,
calPexpr ct' gamma e -> declaring_class ct' c pfc' fi = Some di ->
calPexpr ct' gamma (E_Field e fi)
| P_Invk : forall e mi le c pfc' di,
calPexpr ct' gamma e -> Forall (calPexpr ct' gamma) le -> mresolve ct' mi c pfc' = Some di ->
calPexpr ct' gamma (E_Invk e mi le)
| P_New : forall le ci,
Forall (calPexpr ct' gamma) le -> in_table_id ct' ci ->
calPexpr ct' gamma (E_New ci le)
| P_Cast : forall e ci,
calPexpr ct' gamma e -> in_table_id ct' ci ->
calPexpr ct' gamma (E_Cast ci e)
| P_Lib : calPexpr ct' gamma E_Lib.
Definition calP ct' gamma e lpt :=
calPexpr ct' gamma e /\ (forall e0, set_In e0 lpt -> calPexpr ct' gamma e0).
Section lemmas.
Lemma not_lib_app : forall (ct : class_table) (c : class) (pfc : valid_class ct c),
~ (in_lib ct c /\ in_app ct c).
Proof.
intros ct c pfc. inversion pfc; trivial.
unfold not. intros [_ in_app]. contradiction.
Qed.
Lemma eq_valid : forall (ct : class_table) (c d : class) (pfc : valid_class ct c),
c = d -> valid_class ct d.
Proof.
intros ct c d pfc cdeq.
destruct pfc.
- rewrite <- cdeq. apply ValidObj; trivial.
- apply ValidParent with d0; try rewrite <- cdeq; trivial.
Qed.
Lemma valid_in_table : forall (ct : class_table) (c : class) (pfc : valid_class ct c),
in_table ct c.
Proof.
intros ct c pfc. inversion pfc.
- apply in_lib_in_table. apply H.
- destruct H2.
* apply H2.
* apply in_app_in_table. apply H2.
Qed.
Lemma id_eq : forall (ct : class_table) c d (pfc : valid_class ct c) (pfd : valid_class ct d),
valid_table ct -> id_of c = id_of d -> c = d.
Proof.
intros ct c d pfc pfd pft eq_id. destruct pft as [ct _ look_id _].
apply valid_in_table in pfc. apply valid_in_table in pfd.
apply look_id in pfc. apply look_id in pfd.
rewrite eq_id in pfc. rewrite pfd in pfc. inversion pfc. reflexivity.
Qed.
Lemma in_ct_lib_in_lib : forall (ct : class_table) (c : class),
in_table (ct_lib ct) c -> in_lib ct c.
Proof.
intros ct c in_ct_lib.
unfold ct_lib, in_table in in_ct_lib; unfold in_lib; destruct ct.
destruct in_ct_lib.
- unfold in_app in H. unfold empty_map in H. inversion H.
- unfold in_lib in H. apply H.
Qed.
Lemma in_ct_lib_in_lib_id : forall (ct : class_table) (ci : class_id),
in_table_id (ct_lib ct) ci -> in_lib_id ct ci.
Proof.
intros ct c in_ct_lib_id.
unfold ct_lib, in_table_id in in_ct_lib_id; unfold in_lib_id; destruct ct.
destruct in_ct_lib_id.
- unfold in_app_id in H. unfold empty_map in H. destruct H. inversion H.
- unfold in_lib_id in H. apply H.
Qed.
Lemma valid_obj_Valid_Obj : forall ct (pfc : valid_class ct Obj), exists h1 h2,
pfc = ValidObj ct h1 h2.
Proof.
intros ct pfc. dependent destruction pfc.
- exists i, n. reflexivity.
- inversion e.
Qed.
Lemma ext_in_lib_ext_in_ct :
forall (ct : class_table) (pft : valid_table ct) (c d : class) (pfd : valid_class ct d),
extends (ct_lib ct) c d -> extends ct c d.
Proof.
intros ct pft c d pfd ext_in_lib.
destruct c.
- inversion ext_in_lib.
- simpl. simpl in ext_in_lib.
assert (c0 = id_of d).
{ inversion pft. apply H1. destruct ct as [app lib]. simpl. destruct (app c0) eqn:appc0; try assumption.
destruct (H2 c0). split; simpl. exists d. assumption. exists c2. assumption. }
subst. inversion pft. apply H0. apply valid_in_table. assumption.
Qed.
Lemma ext_in_lib_ext_in_ct_id :
forall (ct : class_table) (pft : valid_table ct) (ci di : class_id),
in_table_id ct ci -> extends_id (ct_lib ct) ci di -> extends_id ct ci di.
Proof.
intros _ [[app lib] _ _ _ nlai] ci di ci_in_ct ext_in_lib.
unfold extends_id. simpl. unfold extends_id in ext_in_lib. simpl in ext_in_lib.
destruct (app ci) eqn:appci; trivial.
destruct (lib ci) eqn:libci; try solve [inversion ext_in_lib].
simpl in nlai. specialize (nlai ci). destruct nlai. split.
- exists c0. assumption.
- exists c. assumption.
Qed.
Lemma valid_in_lib_in_lib : forall ct c (pfc : valid_class (ct_lib ct) c),
in_lib ct c.
Proof.
intros ct c pfc.
destruct ct as [app lib]. simpl.
apply valid_in_table in pfc. unfold in_table in pfc. destruct pfc.
- simpl in H. unfold empty_map in H. inversion H.
- simpl in H. trivial.
Qed.
Lemma decl_in_lib_in_lib : forall ct c (pfc : valid_class (ct_lib ct) c) fi di,
declaring_class (ct_lib ct) c pfc fi = Some di -> in_lib_id ct di.
Proof.
intros ct c pfc fi di decl_in_lib.
remember pfc as pfc'.
induction pfc'.
- simpl in decl_in_lib. inversion decl_in_lib.
- simpl in decl_in_lib. destruct (@existsb field_id (beq_nat fi) (dfields c)).
+ inversion decl_in_lib. destruct ct as [app lib]. simpl. clear Heqpfc'.
apply valid_in_lib_in_lib in pfc. simpl in pfc. exists c. trivial.
+ apply IHpfc' with pfc'. trivial. trivial.
Qed.
Lemma supertype_valid : forall (ct : class_table) (c d : class) (pfc : valid_class ct c),
subtype ct c d -> valid_class ct d.
Proof.
intros ct c d pfc subtyp. induction subtyp.
- eauto.
- eauto.
- inversion pfc.
+ subst. inversion e.
+ replace d with d0; eauto.
{ apply extends_injective with (c := c) (ct := ct); eauto. }
Qed.
Lemma supertype_lib_in_lib : forall (ct : class_table) (c d : class) (pfc : valid_class ct c),
subtype ct c d -> in_lib ct c -> in_lib ct d.
Proof.
intros ct c d pfc subtyp c_in_lib. induction subtyp.
- apply c_in_lib.
- apply IHsubtyp2.
apply supertype_valid with (c := c). eauto. eauto.
apply IHsubtyp1. eauto. eauto.
- inversion pfc.
+ subst. inversion e.
+ assert (d = d0).
{ apply extends_injective with (c:= c) (ct := ct); eauto. }
destruct H2.
* destruct H2. subst. eauto.
* destruct H2. destruct H1. split; eauto.
Qed.
Lemma lib_par_same : forall ct (pft : valid_table ct) c d pfc_l pfd_l ext nla sca pfc,
pfc_l = ValidParent (ct_lib ct) c d pfd_l ext nla sca ->
exists ext' nla' sca' pfd, pfc = ValidParent ct c d pfd ext' nla' sca'.
Proof.
intros. remember pfc as pfc'. destruct pfc.
- inversion ext.
- assert (d = d0); try subst d.
{ apply extends_injective with ct c; trivial.
- inversion pft. apply ext_in_lib_ext_in_ct; trivial.
apply H0. apply in_lib_in_table. apply valid_in_lib_in_lib. trivial. }
exists e, n, o, pfc. trivial.
Qed.
Lemma simul_induct_lib :
forall (ct : class_table) (pft : valid_table ct) (P : forall ct c, valid_class (ct_lib ct) c -> valid_class ct c -> Prop),
(forall o1 o2 o1' o2', P ct Obj (ValidObj (ct_lib ct) o1 o2) (ValidObj ct o1' o2')) ->
(forall c d pfd_l pfd ext nla sca ext' nla' sca',
P ct d pfd_l pfd ->
P ct c (ValidParent (ct_lib ct) c d pfd_l ext nla sca) (ValidParent ct c d pfd ext' nla' sca')) ->
forall c pfc_l pfc, P ct c pfc_l pfc.
Proof.
intros ct pft P Hobj Hind c pfc_l.
remember pfc_l as pfc_lr. induction pfc_l.
- intros. assert (exists h1 h2, pfc = ValidObj ct h1 h2). apply valid_obj_Valid_Obj.
destruct H as [x [y id]]. rewrite id. rewrite Heqpfc_lr. apply Hobj.
- intros. assert (exists ext' nla' sca' pfd, pfc = ValidParent ct c d pfd ext' nla' sca').
{ apply lib_par_same with pfc_lr pfc_l e n o; trivial. }
destruct H as [ext' [nla' [sca' [pfd H']]]].
rewrite H'. rewrite Heqpfc_lr. apply Hind. apply IHpfc_l. trivial.
Qed.
Lemma rel_par_same : forall ct ct' (rel : ave_rel ct ct') c d pfc pfd ext nla sca pfc',
pfc = ValidParent ct c d pfd ext nla sca ->
exists ext' nla' sca' pfd', pfc' = ValidParent ct' c d pfd' ext' nla' sca'.
Proof.
intros. remember pfc' as pfc''. inversion rel as [val_ct val_ct' keep_app keep_ext keep_lib]. destruct pfc'.
- inversion ext.
- assert (d = d0) as deq.
{ apply extends_injective with ct' c.
- trivial.
- apply keep_ext. split.
+ assumption.
+ destruct o.
* apply a.
* apply in_app_in_table. apply a.
- trivial. }
rewrite deq.
exists e, n, o, pfc'. trivial.
Qed.
Lemma simul_induct :
forall (ct ct' : class_table) (rel : ave_rel ct ct')
(P : forall ct ct' rel c, valid_class ct c -> valid_class ct' c -> Prop),
(forall o1 o2 o1' o2', P ct ct' rel Obj (ValidObj ct o1 o2) (ValidObj ct' o1' o2')) ->
(forall c d pfd pfd' ext nla sca ext' nla' sca',
P ct ct' rel d pfd pfd' ->
P ct ct' rel c (ValidParent ct c d pfd ext nla sca) (ValidParent ct' c d pfd' ext' nla' sca')) ->
forall c pfc pfc', P ct ct' rel c pfc pfc'.
Proof.
intros ct ct' rel P Hobj Hind c pfc. inversion rel as [val_ct val_ct' keep_app keep_ext keep_lib].
remember pfc as pfc_r. induction pfc.
- intros. assert (exists h1 h2, pfc' = ValidObj ct' h1 h2). apply valid_obj_Valid_Obj.
destruct H as [x [y id]]. rewrite id. rewrite Heqpfc_r. apply Hobj.
- intros. assert (exists ext' nla' sca' pfd, pfc' = ValidParent ct' c d pfd ext' nla' sca').
{ apply rel_par_same with ct pfc_r pfc e n o.
- trivial.
- trivial. }
destruct H as [ext' [nla' [sca' [pfd' H']]]].
rewrite H'. rewrite Heqpfc_r. apply Hind. apply IHpfc. trivial.
Qed.
Lemma val_in_lib_val_in_ct :
forall (ct : class_table) (c : class) (pfc : valid_class (ct_lib ct) c),
(forall c, in_table ct c -> valid_class ct c) -> valid_class ct c.
Proof.
intros ct c pfc H.
apply H. apply in_lib_in_table. apply valid_in_lib_in_lib. trivial.
Qed.
Lemma fields_in_lib_fields_in_ct :
forall (ct : class_table) (pft : valid_table ct) (c : class) (pfc_l : valid_class (ct_lib ct) c) (pfc : valid_class ct c),
fields (ct_lib ct) c pfc_l = fields ct c pfc.
Proof.
intros ct pft c pfc_l pfc.
apply simul_induct_lib with (ct := ct) (c := c) (pfc_l := pfc_l) (pfc := pfc); trivial.
intros. simpl. destruct c0; trivial.
rewrite H. trivial.
Qed.
Lemma mtype_in_lib_mtype_in_ct :
forall (ct : class_table) (pft : valid_table ct) (c : class) (pfc_l : valid_class (ct_lib ct) c) (pfc : valid_class ct c) mi,
mtype (ct_lib ct) mi c pfc_l = mtype ct mi c pfc.
Proof.
intros ct pft c pfc_l pfc mi.
apply simul_induct_lib with (ct := ct) (c := c) (pfc_l := pfc_l) (pfc := pfc); trivial.
intros. simpl. destruct (lookup_method c0 mi); trivial.
Qed.
Lemma subtyp_id_lib_sybtyp_id_ct : forall (ct : class_table) (ci : class_id) (di : class_id),
valid_table ct -> subtype_id (ct_lib ct) ci di -> subtype_id ct ci di.
Proof.
intros ct ci di pft subtyp_id_lib.
induction subtyp_id_lib.
- apply SI_Ref. apply in_lib_in_table_id. apply in_ct_lib_in_lib_id. trivial.
- apply SI_Sub. apply ext_in_lib_ext_in_ct_id; eauto. apply in_lib_in_table_id. destruct ct as [app lib].
simpl in H. simpl. unfold extends_id in H. destruct (lookup (empty_map, lib) ci) eqn:look; try inversion H.
simpl in look. exists c. trivial.
- apply SI_Trans with di; trivial.
Qed.
Lemma typ_in_lib_typ_in_ct :
forall (ct : class_table) (pft : valid_table ct) (gamma : context) (e : expr) (ci : class_id),
type_of (ct_lib ct) gamma e ci -> type_of ct gamma e ci.
Proof.
intros ct pft gamma e ci typ_in_lib.
induction typ_in_lib using type_of_ind_list.
- apply T_Var.
- inversion pft. assert (valid_class ct c) as pfc'.
{ apply val_in_lib_val_in_ct; trivial. } clear H0 H1 H2 H3 H4.
apply T_Field with c pfc'; trivial.
unfold lookup_field. unfold lookup_field in H.
assert (fields (ct_lib ct) c pfc = fields ct c pfc') by (apply fields_in_lib_fields_in_ct; trivial).
rewrite <- H0. trivial.
- inversion pft. assert (valid_class ct c) as pfc'.
{ apply val_in_lib_val_in_ct; trivial. } clear H6 H7 H8 H9 H10.
apply T_New with c pfc' lc ld; trivial.
+ rewrite <- fields_in_lib_fields_in_ct with (pfc_l := pfc); trivial.
+ eapply Forall_impl; try eassumption.
intros. apply subtyp_id_lib_sybtyp_id_ct; trivial.
- inversion pft. assert (valid_class ct c0) as pfc0'.
{ apply val_in_lib_val_in_ct; trivial. } clear H6 H7 H8 H9 H10.
apply T_Invk with c0 pfc0' ld ret lc; trivial.
* assert (mtype (ct_lib ct) mi c0 pfc0 = mtype ct mi c0 pfc0').
{ apply mtype_in_lib_mtype_in_ct; trivial. }
rewrite <- H6. trivial.
* eapply Forall_impl; try eassumption.
intros a H6. apply subtyp_id_lib_sybtyp_id_ct; trivial.
- apply T_Cast with di; trivial.
Qed.
Lemma decl_in_lib_decl_in_ct :
forall (ct : class_table) (pft : valid_table ct) (c : class) (pfc : valid_class ct c) (pfc_l : valid_class (ct_lib ct) c) fi,
declaring_class (ct_lib ct) c pfc_l fi = declaring_class ct c pfc fi.
intros ct pft c pfc pfc_l.
apply simul_induct_lib with (ct := ct) (c := c) (pfc_l := pfc_l) (pfc := pfc); trivial.
clear c pfc pfc_l. intros c d pfd_l pfd ext nla sca ext' nla' sca' IHd fi.
simpl. destruct (@existsb field_id (beq_nat fi) (dfields c)); trivial.
Qed.
Lemma mresolve_lib_ct :
forall ct (pft : valid_table ct) c (pfc_l : valid_class (ct_lib ct) c) (pfc : valid_class ct c) mi,
mresolve (ct_lib ct) mi c pfc_l = mresolve ct mi c pfc.
Proof.
intros ct pft c pfc_l pfc.
apply simul_induct_lib with (ct := ct) (c := c) (pfc_l := pfc_l) (pfc := pfc); trivial.
clear c pfc pfc_l. intros c d pfd_l pfd ext nla sca ext' nla' sca' IHd mi.
simpl. destruct (existsb (beq_nat mi) (dmethods_id c)); trivial.
Qed.
Lemma invk_valid_in_lib_m_in_lib : forall (ct : class_table) e le mi (gamma : context),
valid_expr (ct_lib ct) gamma (E_Invk e mi le) -> exists c, in_lib ct c /\ In mi (dmethods_id c).
Proof.
intros ct e le mi gamma val_e.
inversion val_e. clear H6. remember pfc as pfc'. induction pfc.
- rewrite Heqpfc' in H7. simpl in H7. destruct H7 as [d cont]. inversion cont.
- rewrite Heqpfc' in H7. simpl in H7. destruct (existsb (beq_nat mi) (dmethods_id c)) eqn:exstb.
+ exists c. split.
* apply in_ct_lib_in_lib. apply valid_in_table. trivial.
* rewrite existsb_exists in exstb. destruct exstb as [mi' [Inmi' Heqmi]].
rewrite beq_nat_true_iff in Heqmi. rewrite Heqmi. trivial.
+ apply IHpfc with pfc; trivial.
Qed.
Lemma find_In_var : forall lv v,
In v lv <-> find (fun v' => beq_var v' v) lv <> None.
Proof.
split; induction lv; intros; eauto; simpl; simpl in H.
- destruct H.
+ rewrite H. rewrite <- beq_var_refl. unfold not. intro. inversion H0.
+ destruct (beq_var a v); eauto. unfold not. intro. inversion H0.
- apply H. reflexivity.
- destruct (beq_var a v) eqn:beq_av.
+ left. apply beq_var_true. eauto.
+ right. apply IHlv. trivial.
Qed.
Lemma type_checks_lib_ct : forall (ct : class_table) (gamma : context) (e : expr),
valid_table ct -> type_checks (ct_lib ct) gamma e -> type_checks ct gamma e.
Proof.
intros ct gamma e pft typ_lib.
induction e using expr_ind_list.
- inversion typ_lib. apply TC_Var. unfold in_table. right. apply in_ct_lib_in_lib_id; trivial.
- inversion typ_lib. apply TC_Field with di; trivial.
+ apply typ_in_lib_typ_in_ct; trivial.
+ unfold in_table. right. apply in_ct_lib_in_lib_id; trivial.
+ apply IHe; trivial.
- inversion typ_lib. apply TC_New; trivial.
+ unfold in_table. right. apply in_ct_lib_in_lib_id; trivial.
+ eapply Forall_list_impl; eassumption.
- inversion typ_lib. apply TC_Invk with di; trivial.
+ apply typ_in_lib_typ_in_ct; trivial.
+ unfold in_table. right. apply in_ct_lib_in_lib_id; trivial.
+ apply IHe. trivial.
+ eapply Forall_list_impl; eassumption.
- inversion typ_lib. apply TC_Cast; trivial.
+ unfold in_table. right. apply in_ct_lib_in_lib_id; trivial.
+ apply IHe; trivial.
- inversion typ_lib.
Qed.
Lemma valid_expr_lib_ct : forall (ct : class_table) (gamma : context) (e : expr),
valid_table ct -> valid_expr (ct_lib ct) gamma e -> valid_expr ct gamma e.
Proof.
intros ct gamma e pft val_lib.
induction e using expr_ind_list.
- inversion val_lib. apply Val_Var; trivial. apply type_checks_lib_ct; trivial.
- inversion val_lib. rename pfc into pfc_l.
inversion pft. assert (valid_class ct c) as pfc.
{ apply val_in_lib_val_in_ct; trivial. } clear H6 H7 H8 H9 H10.
apply Val_Field with ci c pfc; eauto.
+ apply type_checks_lib_ct; trivial.
+ apply typ_in_lib_typ_in_ct; trivial.
+ rewrite <- decl_in_lib_decl_in_ct with (pfc_l := pfc_l); trivial.
- inversion val_lib. apply Val_New; trivial.
+ apply type_checks_lib_ct; trivial.
+ eapply Forall_list_impl; eassumption.
- inversion val_lib. rename pfc into pfc_l.
inversion pft. assert (valid_class ct c) as pfc.
{ apply val_in_lib_val_in_ct; trivial. } clear H9 H10 H11 H12 H13.
apply Val_Invk with ci c pfc; trivial.
+ apply type_checks_lib_ct; trivial.
+ apply IHe; trivial.
+ eapply Forall_list_impl; eassumption.
+ apply typ_in_lib_typ_in_ct; trivial.
+ erewrite <- mresolve_lib_ct; eassumption.
- inversion val_lib. apply Val_Cast; trivial.
+ apply type_checks_lib_ct; trivial.
+ apply IHe; trivial.
- apply Val_Lib.
Qed.
Lemma valid_class_irrelevance :
forall ct c (pfc : valid_class ct c) (pfc' : valid_class ct c),
pfc = pfc'.
Proof.
intros. generalize dependent pfc'. induction pfc.
- intros. assert (exists i' n', pfc' = ValidObj ct i' n') by (apply valid_obj_Valid_Obj; trivial).
destruct H as [i' [n' Heqpfc']]. subst.
replace i' with i by apply proof_irrelevance. replace n' with n by apply proof_irrelevance.
reflexivity.
- intros. destruct pfc' eqn:Heqpfc'; try inversion e.
assert (d = d0) by (apply extends_injective with ct c; eauto). subst d0.
replace e0 with e by apply proof_irrelevance.
replace n0 with n by apply proof_irrelevance.
replace o0 with o by apply proof_irrelevance.
replace v with pfc by apply IHpfc. reflexivity.
Qed.
Lemma type_of_fn : forall ct gamma e ci di,
valid_table ct -> type_of ct gamma e ci -> type_of ct gamma e di -> ci = di.
Proof.
intros ct gamma e ci di pft tci. generalize dependent di.
induction tci; intros di0 tdi0; inversion tdi0; trivial.
- subst e0. subst f0. subst di1. assert (id_of c = id_of c0). apply IHtci; trivial.
assert (c = c0) by (apply id_eq with ct; trivial). subst.
assert (pfc0 = pfc) by apply valid_class_irrelevance. subst pfc0.
rewrite H in H4. inversion H4. reflexivity.
- apply IHtci; trivial.
Qed.
Lemma typ_check_in_lib_typ_in_lib : forall ct gamma e ci,
valid_table ct -> type_checks (ct_lib ct) gamma e -> type_of ct gamma e ci -> in_lib_id ct ci.
Proof.
intros ct gamma e c pft typ_chk typ.
destruct e; inversion typ; inversion typ_chk; subst; try (apply in_ct_lib_in_lib_id; trivial).
- assert (c = di0).
{ apply type_of_fn with ct gamma (E_Field e f); eauto. apply typ_in_lib_typ_in_ct; eauto. }
subst di0; trivial.
- replace (id_of c0) with di; trivial.
apply type_of_fn with ct gamma (E_Invk e m l); trivial.
apply typ_in_lib_typ_in_ct; trivial.
Qed.
Lemma in_lib_id_in_lib : forall {ct} (pft : valid_table ct) c,
in_table ct c -> in_lib_id ct (id_of c) -> in_lib ct c.
Proof.
intros [app lib] pft c H H0. inversion pft. simpl in H0. destruct H0 as [c0 Hc0]. subst.
simpl. apply H2 in H as lookc. simpl in lookc. destruct (app (id_of c)) eqn:appid.
- specialize (H4 (id_of c)). destruct H4. split; simpl; eauto.
- apply lookc.
Qed.