-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof.v
2045 lines (1890 loc) · 91 KB
/
proof.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 :
forall P : expr -> Prop,
(forall v : var, P (E_Var v)) ->
(forall e : expr, P e -> forall f0 : field_id, P (E_Field e f0)) ->
(forall (c : class_id) (l : list expr), Forall P l -> P (E_New c l)) ->
(forall e : expr, P e -> forall (m : method_id) (l : list expr), Forall P l -> P (E_Invk e m l)) ->
(forall (c : class_id) (e : expr), P e -> P (E_Cast c e)) -> P E_Lib -> forall e : expr, P e :=
fun (P : expr -> Prop) => expr_rect_list P (Forall P) (Forall_nil P) (@Forall_cons expr P).
Definition context := list (var * class_id).
Definition lookup_context (gamma : context) (v : var) : option class_id :=
option_map snd (find (fun p => beq_var v (fst p)) gamma).
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.
(* Table Definitions *)
Definition class_table : Type := (class_id -> option class) * (class_id -> option class).
Hint Unfold class_table.
Definition empty_map : class_id -> option class := fun _ => None.
Hint Unfold empty_map.
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_class (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.
Hint Unfold in_lib_id.
Definition in_app_id (ct : class_table) (i : class_id) : Prop :=
match ct with (app, _) => exists c, app i = Some c end.
Hint Unfold in_app_id.
Definition in_lib (ct : class_table) (c : class) : Prop :=
match ct with (_, lib) => lib (id_of c) = Some c end.
Hint Unfold in_lib.
Definition in_app (ct : class_table) (c : class) : Prop :=
match ct with (app, _) => app (id_of c) = Some c end.
Hint Unfold in_app.
Definition in_table_id (ct : class_table) (i : class_id) : Prop :=
in_app_id ct i \/ in_lib_id ct i.
Hint Unfold in_table_id.
Definition in_table (ct : class_table) (c : class) : Prop :=
in_app ct c \/ in_lib ct c.
Hint Unfold in_table.
(* Table Lemmas *)
Lemma in_app_in_table : forall (ct : class_table) (c : class),
in_app ct c -> in_table ct c.
Proof. eauto. Qed.
Hint Resolve in_app_in_table.
Lemma in_lib_in_table : forall (ct : class_table) (c : class),
in_lib ct c -> in_table ct c.
Proof. eauto. Qed.
Hint Resolve in_lib_in_table.
Lemma in_app_in_table_id : forall (ct : class_table) (ci : class_id),
in_app_id ct ci -> in_table_id ct ci.
Proof. eauto. Qed.
Hint Resolve in_app_in_table_id.
Lemma in_lib_in_table_id : forall (ct : class_table) (ci : class_id),
in_lib_id ct ci -> in_table_id ct ci.
Proof. eauto. Qed.
Hint Resolve in_lib_in_table_id.
Lemma in_app_in_app_id : forall ct c,
in_app ct c -> in_app_id ct (id_of c).
Proof.
intros. destruct ct; eexists; eauto.
Qed.
Hint Resolve in_app_in_app_id.
Lemma in_lib_in_lib_id : forall ct c,
in_lib ct c -> in_lib_id ct (id_of c).
Proof.
intros. destruct ct; eexists; eauto.
Qed.
Hint Resolve in_lib_in_lib_id.
Lemma in_table_in_table_id : forall ct c,
in_table ct c -> in_table_id ct (id_of c).
Proof.
intros. destruct H; eauto.
Qed.
Hint Resolve in_table_in_table_id.
(* More Definitions *)
Definition extends (ct : class_table) (c d : class) : Prop :=
match c with
| Obj => False
| C _ di _ _ _ => lookup_class ct di = Some d
end.
Definition extends_id (ct : class_table) (ci di : class_id) : Prop :=
match lookup_class 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), in_table ct c -> 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), in_table ct c -> extends ct c d -> subtype ct c d.
Hint Constructors subtype.
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.
Hint Constructors subtype_id.
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.
Hint Constructors valid_class.
Definition body (m : method) : expr := match m with Method _ _ _ e => e end.
Hint Unfold body.
Definition ret_type (m : method) : class_id := match m with Method ci _ _ _ => ci end.
Hint Unfold ret_type.
Definition params_of (m : method) : params :=
match m with
| Method _ _ l _ => l
end.
Hint Unfold params_of.
Definition param_names (m : method) : list var := map Var (map snd (params_of m)).
Hint Unfold param_names.
Definition params_as_context (m : method) : list (var * class_id) :=
map (fun p => (Var (snd p), fst p)) (params_of m).
Hint Unfold params_as_context.
Definition mid (m : method) : method_id := match m with Method _ mi _ _ => mi end.
Hint Unfold mid.
Definition mtype_of (m : method) :=
(map fst (params_of m), ret_type m).
Hint Unfold mtype_of.
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.
Hint Unfold find_method.
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 m => Some (mtype_of m)
end
end.
Hint Unfold mtype.
Definition dfields (c : class) : list (class_id * field_id) :=
match c with
| Obj => nil
| C _ _ fs _ _ => fs
end.
Hint Unfold dfields.
Definition dfield_ids (c : class) : list field_id :=
map snd (dfields c).
Hint Unfold dfield_ids.
Definition dfield_typs (c : class) : list class_id :=
map fst (dfields c).
Hint Unfold dfield_typs.
Fixpoint fields {ct : class_table} {c : class} (pfc : valid_class ct c) : list (class_id * field_id) :=
dfields c ++ match pfc with
| ValidParent _ _ _ pfd _ _ _ => fields pfd
| ValidObj _ _ _ => nil
end.
Hint Unfold fields.
Definition field_ids {ct : class_table} {c : class} (pfc : valid_class ct c) : list field_id :=
map snd (fields pfc).
Hint Unfold field_ids.
Definition lookup_field {ct : class_table} {c : class} (pfc : valid_class ct c) (f : field_id) : option class_id :=
option_map fst (find (fun p => beq_nat f (snd p)) (fields pfc)).
Hint Unfold lookup_field.
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) (dfield_ids c))
then Some (id_of c)
else declaring_class ct d pfd fi
end.
Hint Unfold declaring_class.
Inductive type_of (ct : class_table) (gamma : context) : expr -> class_id -> Prop :=
| T_Var : forall x ci,
lookup_context gamma x = Some ci ->
in_table_id ct ci ->
type_of ct gamma (E_Var x) ci
| T_Field : forall e c (pfc : valid_class ct c) di f,
type_of ct gamma e (id_of c) ->
lookup_field pfc f = Some di ->
type_of ct gamma (E_Field e f) di
| T_New : forall c (pfc : valid_class ct c) le lc,
length (field_ids pfc) = 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 (field_ids pfc)) ->
type_of ct gamma (E_New (id_of c) le) (id_of c)
| T_Invk : forall e c pfc ld ret mi le lc,
type_of ct gamma e (id_of c) ->
mtype ct mi c pfc = 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 e mi le) (id_of c)
| T_Cast : forall e ci di,
type_of ct gamma e di ->
in_table_id ct ci ->
type_of ct gamma (E_Cast ci e) ci
| T_Lib : forall ci, in_table_id ct ci -> type_of ct gamma E_Lib ci.
Hint Constructors type_of.
Definition type_of_ind_list :=
fun (ct : class_table) (gamma : context) (P : expr -> class_id -> Prop)
(f : forall (x : var) (ci : class_id), lookup_context gamma x = Some ci -> in_table_id ct ci -> P (E_Var x) ci)
(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 pfc f0 = Some di -> P (E_Field e f0) di)
(f1 : forall (c : class) (pfc : valid_class ct c) (le : list expr) (lc : list class_id),
length (field_ids pfc) = 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 (field_ids pfc)) ->
Forall (fun p : expr * class_id => P (fst p) (snd p)) (combine le lc) ->
P (E_New (id_of c) le) (id_of c))
(f2 : forall (e : expr) (c : class) (pfc : valid_class ct c) (ld : list class_id) (ret : class_id)
(mi : method_id) (le : list expr) (lc : list class_id),
type_of ct gamma e (id_of c) ->
P e (id_of c) ->
mtype ct mi c pfc = 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 e mi le) (id_of c))
(f3 : forall (e : expr) (ci di : class_id),
type_of ct gamma e di -> P e di -> in_table_id ct ci -> P (E_Cast ci e) ci)
(f4 : forall ci : class_id, in_table_id ct ci -> P E_Lib 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 ci e0 i => f x ci e0 i
| T_Field _ _ e0 c0 pfc di f5 t0 e1 => f0 e0 c0 pfc di f5 t0 (F e0 (id_of c0) t0) e1
| T_New _ _ c0 pfc le lc e0 e1 f5 f6 =>
f1 c0 pfc le lc e0 e1 f5 f6
(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) f5)
| T_Invk _ _ e0 c0 pfc ld ret mi le lc t0 e1 e2 e3 f5 f6 =>
f2 e0 c0 pfc ld ret mi le lc t0 (F e0 (id_of c0) t0) e1 e2 e3 f5 f6
(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) f5)
| T_Cast _ _ e0 ci di t0 i => f3 e0 ci di t0 (F e0 di t0) i
| T_Lib _ _ ci H => f4 ci H
end.
Definition type_checks ct gamma e :=
exists ci, type_of ct gamma e ci.
Hint Unfold type_checks.
(*Inductive type_checks (ct : class_table) (gamma : context) : expr -> Prop :=
| TC_Var : forall x ci, lookup_context gamma x = Some ci -> in_table_id ct ci -> 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)
| TC_Lib : type_checks ct gamma E_Lib.
Hint Constructors type_checks.*)
Definition dmethods (c : class) : list method :=
match c with
| Obj => nil
| C _ _ _ _ ms => ms
end.
Hint Unfold dmethods.
Definition dmethods_id (c : class) : list method_id :=
map mid (dmethods c).
Hint Unfold dmethods_id.
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.
Hint Unfold mresolve.
(*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.
Definition valid_expr_ind_list :=
fun (ct : class_table) (gamma : context) (P : expr -> Prop)
(f : forall x : var, type_checks ct gamma (E_Var x) -> P (E_Var x))
(f0 : forall (e : expr) (f0 : field_id) (ci : class_id) (c : class) (pfc : valid_class ct c),
type_checks ct gamma (E_Field e f0) ->
type_of ct gamma e ci ->
id_of c = ci ->
(exists di : class_id, declaring_class ct c pfc f0 = Some di) ->
valid_expr ct gamma e -> P e -> P (E_Field e f0))
(f1 : forall (ci : class_id) (le : list expr),
type_checks ct gamma (E_New ci le) ->
Forall (valid_expr ct gamma) le ->
Forall P le ->
P (E_New ci le))
(f2 : forall (e : expr) (m : method_id) (le : list expr) (ci : class_id) (c : class) (pfc : valid_class ct c),
type_checks ct gamma (E_Invk e m le) ->
valid_expr ct gamma e ->
P e ->
Forall (valid_expr ct gamma) le ->
Forall P le ->
type_of ct gamma e ci ->
id_of c = ci ->
(exists d : class, mresolve ct m c pfc = Some d) ->
P (E_Invk e m le))
(f3 : forall (ci : class_id) (e : expr),
type_checks ct gamma (E_Cast ci e) -> valid_expr ct gamma e -> P e -> P (E_Cast ci e))
(f4 : P E_Lib) =>
fix F (e : expr) (v : valid_expr ct gamma e) {struct v} : P e :=
match v in (valid_expr _ _ e0) return (P e0) with
| Val_Var _ _ x t => f x t
| Val_Field _ _ e0 f5 ci c pfc t t0 e1 e2 v0 => f0 e0 f5 ci c pfc t t0 e1 e2 v0 (F e0 v0)
| Val_New _ _ ci le t f5 => f1 ci le t f5
(let Q := valid_expr ct gamma in
let P' := P in
(fix f_impl (l : list expr) (F_Q : Forall Q l) : Forall P' l :=
match F_Q with
| Forall_nil _ => Forall_nil P'
| @Forall_cons _ _ e l' Qp Ql' => Forall_cons e (F e Qp) (f_impl l' Ql')
end) le f5)
| Val_Invk _ _ e0 m le ci c pfc t v0 f5 t0 e1 e2 => f2 e0 m le ci c pfc t v0 (F e0 v0) f5
(let Q := valid_expr ct gamma in
let P' := P in
(fix f_impl (l : list expr) (F_Q : Forall Q l) : Forall P' l :=
match F_Q with
| Forall_nil _ => Forall_nil P'
| @Forall_cons _ _ e l' Qp Ql' => Forall_cons e (F e Qp) (f_impl l' Ql')
end) le f5) t0 e1 e2
| Val_Cast _ _ ci e0 t v0 => f3 ci e0 t v0 (F e0 v0)
| Val_Lib _ _ => f4
end. *)
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).
Hint Constructors fj_expr.
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).
Hint Constructors expr_In.
Definition T_Method ct c (m : method) : Prop :=
(exists ei, type_of ct ((This, id_of c) :: params_as_context m) (body m) ei /\
subtype_id ct ei (ret_type m)) /\
forall d pfd t, mtype ct (mid m) d pfd = Some t -> t = mtype_of m.
Hint Unfold T_Method.
Definition T_Class ct c : Prop :=
Forall (T_Method ct c) (dmethods c) /\
Forall (in_table_id ct) (dfield_typs c).
Hint Unfold T_Class.
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_class ct (id_of c) = Some c) ->
(forall ci c, lookup_class 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, in_lib ct c -> T_Class (ct_lib ct) c) ->
(forall c, in_table ct c -> T_Class ct c) ->
valid_table ct.
Hint Constructors valid_table.
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.
Hint Resolve extends_injective.
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.
Hint Resolve mres_lib_in_lib.
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'.
Hint Constructors ave_rel.
Inductive alpha (ct ct' : class_table) (gamma : context) : expr -> expr -> set expr -> Prop :=
| 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' ->
Forall (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 -> Forall (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' ->
Forall (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 ->
Forall (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.
Hint Constructors alpha.
Definition alpha_ind_list :=
fun (ct ct' : class_table) (gamma : context) (P : expr -> expr -> set expr -> Prop)
(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' ->
Forall (fun p : expr * expr => alpha ct ct' gamma (fst p) (snd p) lpt) (combine le le') ->
Forall (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 ->
Forall (fun e : expr => alpha ct ct' gamma e E_Lib lpt) le ->
Forall (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' ->
Forall (fun p : expr * expr => alpha ct ct' gamma (fst p) (snd p) lpt) (combine le le') ->
Forall (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 ->
Forall (fun e' : expr => alpha ct ct' gamma e' E_Lib lpt) le ->
Forall (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 : 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) 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 : Forall Q l) : Forall P' l :=
match F_Q with
| Forall_nil _ => Forall_nil P'
| @Forall_cons _ _ e l' Qe Ql' => Forall_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 : 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) 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 : Forall Q l) : Forall P' l :=
match F_Q with
| Forall_nil _ => Forall_nil P'
| @Forall_cons _ _ e l' Qe Ql' => Forall_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.
Hint Unfold do_sub.
Definition valid_sub (ct : class_table) (gamma : context) (sig : subst) : Prop :=
Forall (fun p => exists ci, lookup_context gamma (fst p) = Some ci /\ type_of ct gamma (snd p) ci) sig.
Hint Unfold valid_sub.
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.
Hint Constructors calPexpr.
Definition calP ct' gamma e lpt :=
calPexpr ct' gamma e /\ (forall e0, set_In e0 lpt -> calPexpr ct' gamma e0).
Hint Unfold calP.
(* 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.
Hint Resolve not_lib_app.
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.
Hint Resolve eq_valid.
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.
Hint Resolve valid_in_table.
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.
Hint Resolve id_eq.
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.
Hint Resolve in_ct_lib_in_lib.
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.
Hint Resolve in_ct_lib_in_lib_id.
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.
Hint Resolve valid_obj_Valid_Obj.
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.
Hint Resolve ext_in_lib_ext_in_ct.
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.
Hint Resolve ext_in_lib_ext_in_ct_id.
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.
Hint Resolve valid_in_lib_in_lib.
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) (dfield_ids 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.
Hint Resolve decl_in_lib_in_lib.
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.
Qed.
Hint Resolve supertype_valid.
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) by eauto.
destruct H2; decomp; subst. eauto.
destruct H1; eauto.
Qed.
Hint Resolve supertype_lib_in_lib.
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. eauto. }
split_rec 4; eauto.
Qed.
Hint Resolve lib_par_same.
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.
split_rec 4; eauto.
Qed.
Hint Resolve rel_par_same.
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.
Hint Resolve val_in_lib_val_in_ct.
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 pfc_l = fields 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.
Hint Resolve fields_in_lib_fields_in_ct.
Lemma fields_in_lib_fields_in_ct_id :
forall (ct : class_table) (pft : valid_table ct) (c : class) (pfc_l : valid_class (ct_lib ct) c) (pfc : valid_class ct c),
field_ids pfc_l = field_ids pfc.
Proof. intros. unfold field_ids. erewrite fields_in_lib_fields_in_ct; eauto. Qed.
Hint Resolve fields_in_lib_fields_in_ct_id.
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.
Hint Resolve mtype_in_lib_mtype_in_ct.
Lemma subtyp_id_lib_subtyp_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.