-
Notifications
You must be signed in to change notification settings - Fork 0
/
newproof.v
1618 lines (1503 loc) · 69.7 KB
/
newproof.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 Import Coq.Init.Datatypes.
Require Import Coq.Lists.List.
Require Import Arith.EqNat.
Require Import Program.Equality.
Module E.
Require Coq.Sets.Ensembles.
Include Ensembles.
End E.
Section generic_defs.
Definition set := E.Ensemble.
Definition empty_set {A : Type} := E.Empty_set A.
Definition set_In {A : Type} (a : A) (s : set A) := E.In A s a.
Definition add {A : Type} (a : A) (s : set A) := E.Add A s a.
Definition union {A : Type} := E.Union A.
Definition union_introl {A : Type} := E.Union_introl A.
Definition union_intror {A : Type} := E.Union_intror A.
Definition subset {A : Type} := E.Included A.
Fixpoint set_from_list {A : Type} (l : list A) :=
match l with
| nil => empty_set
| h :: l' => add h (set_from_list l')
end.
Fixpoint index_of {A : Type} (f : A -> bool) (l : list A) : option nat :=
match l with
| nil => None
| a :: l' => if f a then Some 0 else option_map S (index_of f l')
end.
Fixpoint first_n {A : Type} (l : list A) (n : nat) :=
match n, l with
| S n', h :: l' => h :: (first_n l' n')
| _, _ => nil
end.
Fixpoint remove_last {A : Type} (l : list A) :=
match l with
| nil => nil
| _ :: nil => nil
| h :: l' => h :: (remove_last l')
end.
Fixpoint last_error {A : Type} (l : list A) :=
match l with
| nil => None
| h :: nil => Some h
| _ :: l' => last_error l'
end.
Fixpoint zip_with_ind_h {A : Type} (l : list A) (n : nat) :=
match l with
| nil => nil
| h :: l' => (n, h) :: zip_with_ind_h l' (S n)
end.
Definition zip_with_ind {A : Type} (l : list A) := zip_with_ind_h l 0.
End generic_defs.
Notation "a 'U' b" := (union a b) (at level 65, right associativity).
Section generic_thms.
Theorem Forall_list_impl : forall (A : Type) (P Q : A -> Prop) (l : list A),
Forall P l -> Forall (fun a => P a -> Q a) l -> Forall Q l.
Proof.
intros A P Q l fp fimp. induction l. apply Forall_nil.
inversion fp. inversion fimp.
apply Forall_cons.
- apply H5; trivial.
- apply IHl; trivial.
Qed.
Theorem Forall_map_swap : forall (A : Type) (P : A -> Prop) (f : A -> A) (l : list A),
Forall P (map f l) <-> Forall (fun a => P (f a)) l.
Proof.
intros A P f l.
induction l.
- simpl. split; intros; apply Forall_nil.
- simpl. split; intros; inversion H; apply Forall_cons; trivial; apply IHl; trivial.
Qed.
Theorem Forall_In : forall (A : Type) (P : A -> Prop) (l : list A) (a : A),
Forall P l -> In a l -> P a.
Proof.
induction l.
- intros. inversion H0.
- intros. destruct H0; inversion H; eauto.
rewrite <- H0. eauto.
Qed.
Theorem find_pair_in_zip : forall (A B : Type) (f : A -> bool) (la : list A) (lb lb' : list B) p p',
find (fun p => f (fst p)) (combine la lb) = Some p ->
find (fun p => f (fst p)) (combine la lb') = Some p' ->
In (snd p, snd p') (combine lb lb').
Proof.
induction la.
- intros. inversion H.
- intros. destruct lb; destruct lb'.
+ inversion H.
+ inversion H.
+ inversion H0.
+ simpl in H. simpl in H0. destruct (f a).
* inversion H. inversion H0. simpl. left. reflexivity.
* simpl. right; eauto.
Qed.
Theorem extract_combine : forall {A B : Type} (f : A -> bool) (la : list A) (lb : list B),
length la = length lb -> find (fun p => f (fst p)) (combine la lb) = None -> find f la = None.
Proof.
induction la.
- reflexivity.
- intros. destruct lb.
+ inversion H.
+ simpl in H0. simpl. destruct (f a).
* inversion H0.
* apply IHla with lb; eauto.
Qed.
Theorem zip_In_l : forall {A B : Type} (la : list A) (lb : list B) a b,
In (a, b) (combine la lb) -> In a la.
Proof.
induction la; intros.
- inversion H.
- destruct lb.
+ inversion H.
+ simpl in H. simpl. destruct H.
* inversion H. eauto.
* right. apply IHla with lb b. eauto.
Qed.
Theorem zip_In_r : forall {A B : Type} (la : list A) (lb : list B) a b,
In (a, b) (combine la lb) -> In b lb.
Proof.
intros A B la lb. generalize dependent la. induction lb; intros.
- destruct la; inversion H.
- destruct la.
+ inversion H.
+ simpl in H. simpl. destruct H.
* inversion H. eauto.
* right. apply IHlb with la a0. eauto.
Qed.
Theorem get_nth : forall {A : Type} (l : list A) (m n : nat),
length l = n -> m < n -> exists a, nth_error l m = Some a.
Proof.
induction l; intros; simpl in H.
- rewrite <- H in H0. inversion H0.
- destruct n; inversion H.
destruct m.
+ exists a. reflexivity.
+ simpl. apply IHl with n; trivial.
apply Lt.lt_S_n. trivial.
Qed.
Lemma union_comm : forall {A : Type} (s1 s2 : set A),
s1 U s2 = s2 U s1.
Proof.
intros. apply E.Extensionality_Ensembles.
unfold E.Same_set. unfold E.Included.
split; unfold union; intros; inversion H;
solve [apply union_introl; trivial | apply union_intror; trivial].
Qed.
Lemma union_addl : forall A (s1 s2 : set A) (a : A),
add a s1 U s2 = add a (s1 U s2).
intros. apply E.Extensionality_Ensembles.
unfold E.Same_set. unfold E.Included. unfold union. unfold add.
split; intros; inversion H; try (inversion H0);
solve [apply union_intror; trivial
| apply union_introl; solve [apply union_introl; trivial
| apply union_intror; solve [trivial | reflexivity]]].
Qed.
Lemma union_addr : forall A (s1 s2 : set A) (a : A),
s1 U add a s2 = add a (s1 U s2).
intros. apply E.Extensionality_Ensembles.
unfold E.Same_set. unfold E.Included. unfold union. unfold add.
split; intros; inversion H; try (inversion H0);
solve [ apply union_introl; solve [ apply union_introl; reflexivity || trivial
| apply union_intror; reflexivity || trivial
| reflexivity || trivial]
| apply union_intror; solve [ apply union_introl; reflexivity || trivial
| apply union_intror; reflexivity || trivial
| reflexivity || trivial]].
Qed.
Lemma union_assoc : forall {A: Type} (s1 s2 s3 : set A),
(s1 U s2) U s3 = s1 U s2 U s3.
Proof.
intros. apply E.Extensionality_Ensembles. unfold E.Same_set. unfold E.Included. unfold union.
split; intros; inversion H; try (inversion H0);
solve [ apply union_introl; solve [ apply union_introl; reflexivity || trivial
| apply union_intror; reflexivity || trivial
| reflexivity || trivial]
| apply union_intror; solve [ apply union_introl; reflexivity || trivial
| apply union_intror; reflexivity || trivial
| reflexivity || trivial]].
Qed.
Lemma union_same : forall {A : Type} (s : set A),
s U s = s.
Proof.
intros. apply E.Extensionality_Ensembles. unfold E.Same_set. unfold E.Included. unfold union.
split; intros; try (inversion H); trivial. apply union_introl. apply H.
Qed.
Lemma union_include : forall {A : Type} (s1 s2 : set A),
subset s1 s2 -> s2 = s2 U s1.
Proof.
intros. apply E.Extensionality_Ensembles.
unfold E.Same_set. unfold subset in H. unfold E.Included. unfold E.Included in H.
split; intros.
- apply union_introl. apply H0.
- destruct H0.
+ apply H0.
+ apply H. apply H0.
Qed.
Lemma In_list_set_In_list : forall {A : Type} (l : list A) (a : A),
In a l -> set_In a (set_from_list l).
Proof.
induction l; intros; inversion H.
- subst a0. simpl. apply union_intror. apply E.In_singleton.
- simpl. apply union_introl. apply IHl. apply H0.
Qed.
Lemma nth_error_first_n : forall {A : Type} (l : list A) (a : A) (n : nat),
nth_error l n = Some a -> nth_error (first_n l (S n)) n = Some a.
Proof.
induction l; intros; destruct n; inversion H.
- subst a0. reflexivity.
- simpl. rewrite H1. apply IHl. apply H1.
Qed.
Lemma remove_last_length : forall {A : Type} (l : list A) (n : nat),
length l = S n -> length (remove_last l) = n.
Proof.
induction l; intros; inversion H.
destruct l; try reflexivity.
simpl. simpl in IHl. rewrite IHl with (length l); reflexivity.
Qed.
Lemma remove_last_first_n : forall {A : Type} (l : list A) (n : nat),
n < length l -> first_n l n = first_n (remove_last l) n.
Proof.
induction l; intros.
- inversion H.
- destruct l.
+ simpl in H. inversion H. reflexivity. inversion H1.
+ destruct n; try reflexivity.
simpl. f_equal. unfold first_n in IHl. fold (@first_n A) in IHl.
unfold remove_last in IHl. fold (@remove_last A) in IHl.
apply IHl. apply Lt.lt_S_n. apply H.
Qed.
Lemma remove_last_nth_error : forall {A : Type} (l : list A) (n : nat),
S n < length l -> nth_error l n = nth_error (remove_last l) n.
Proof.
induction l; intros.
- inversion H.
- destruct n.
+ destruct l.
* inversion H. inversion H1.
* reflexivity.
+ destruct l.
* inversion H. inversion H1.
* simpl. simpl in IHl. apply IHl. apply Lt.lt_S_n. apply H.
Qed.
Lemma first_n_length : forall {A : Type} (l : list A),
first_n l (length l) = l.
Proof.
induction l; try reflexivity.
simpl. rewrite IHl. reflexivity.
Qed.
Lemma last_error_In : forall {A : Type} (l : list A) (a : A),
last_error l = Some a -> In a l.
Proof.
induction l.
- intros. inversion H.
- destruct l.
+ intros. inversion H. left. reflexivity.
+ intros. right. apply IHl. apply H.
Qed.
Lemma zip_with_ind_h_length : forall {A : Type} (l : list A) (n : nat),
length (zip_with_ind_h l n) = length l.
Proof.
induction l.
- reflexivity.
- intros. simpl. f_equal. apply IHl.
Qed.
Lemma zip_with_ind_length : forall {A : Type} (l : list A),
length (zip_with_ind l) = length l.
Proof. intros. apply zip_with_ind_h_length. Qed.
Lemma last_repeat : forall {A : Type} (a : A) (n : nat),
last_error (repeat a (S n)) = Some a.
Proof.
induction n.
- reflexivity.
- simpl. apply IHn.
Qed.
Lemma last_error_map : forall {A B : Type} (f : A -> B) (l : list A) (a : A),
last_error l = Some a -> last_error (map f l) = Some (f a).
Proof.
induction l.
- intros. inversion H.
- intros. destruct l.
+ inversion H. subst a0. reflexivity.
+ simpl. apply IHl. apply H.
Qed.
Lemma last_error_zip_with_ind_h : forall {A : Type} (l : list A) (n : nat) (m : nat) (a : A),
length l = S m -> last_error l = Some a ->
last_error (zip_with_ind_h l n) = Some (m + n, a).
Proof.
induction l.
- intros. inversion H.
- intros. destruct l.
+ inversion H0. subst a0. inversion H. reflexivity.
+ destruct m. inversion H. simpl. rewrite plus_n_Sm. apply IHl.
* simpl. f_equal. inversion H. reflexivity.
* apply H0.
Qed.
Lemma last_error_zip_with_ind : forall {A : Type} (l : list A) (n : nat) (a : A),
length l = S n -> last_error l = Some a ->
last_error (zip_with_ind l) = Some (n, a).
Proof.
intros. unfold zip_with_ind. rewrite (plus_n_O n). apply last_error_zip_with_ind_h; trivial.
Qed.
Lemma nth_error_map : forall {A B : Type} (l : list A) (f : A -> B) (a : A) (n : nat),
nth_error l n = Some a -> nth_error (map f l) n = Some (f a).
Proof.
induction l; intros; destruct n; inversion H.
- subst a0. reflexivity.
- rewrite <- IHl with (n := n); try reflexivity. apply H.
Qed.
Lemma nth_error_zip_with_ind_h : forall {A : Type} (l : list A) (n : nat) (m : nat) (a : A),
nth_error l m = Some a -> nth_error (zip_with_ind_h l n) m = Some (n + m, a).
Proof.
induction l; intros; destruct m; inversion H.
- subst a0. rewrite PeanoNat.Nat.add_comm. reflexivity.
- simpl. rewrite <- plus_n_Sm. replace (S (n + m)) with (S n + m) by reflexivity.
apply IHl. trivial.
Qed.
Lemma nth_error_zip_with_ind : forall {A : Type} (l : list A) (m : nat) (a : A),
nth_error l m = Some a -> nth_error (zip_with_ind l) m = Some (m, a).
Proof.
intros. apply nth_error_zip_with_ind_h. trivial.
Qed.
Lemma nth_error_better_Some : forall {A : Type} (l : list A) (n : nat),
n < length l -> exists a, nth_error l n = Some a.
Proof.
intros. assert (nth_error l n <> None) by (apply nth_error_Some; trivial).
destruct (nth_error l n) eqn:ntherr.
- exists a. reflexivity.
- destruct H0; reflexivity.
Qed.
End generic_thms.
Section defs.
Definition field_id := nat.
Definition var_id := nat.
Definition method_id := nat.
Inductive var : Type :=
| Var : var_id -> var
| This : var.
Inductive class : Type :=
| Obj : class
| C : class ->
list (class * field_id) ->
list (class * var_id) ->
list method -> class
with expr : Type :=
| E_Var : var -> expr
| E_Field : expr -> field_id -> expr
| E_New : class -> list expr -> expr
| E_Invk : expr -> method_id -> list expr -> expr
| E_Cast : class -> expr -> expr
| E_Lib : expr
with method : Type :=
| Method : class -> method_id -> list (class * var_id) -> expr -> method.
Definition exprlpt : Type := expr * set expr.
End defs.
Section table_defs.
Definition empty_map : class -> Prop := fun c => False.
Definition class_table : Type := (class -> Prop) * (class -> Prop).
Definition in_app (ct : class_table) c :=
match ct with (app, _) => app c end.
Definition in_lib (ct : class_table) c :=
match ct with (_, lib) => lib c end.
Definition in_table ct c := in_app ct c \/ in_lib ct c.
Definition ct_lib (ct : class_table) : class_table :=
match ct with (app, lib) => (empty_map, lib) end.
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_class (ct : class_table) : class -> Prop :=
| V_Obj : ~ in_app ct Obj -> in_lib ct Obj -> valid_class ct Obj
| V_Class : forall d l l' lm,
valid_class ct d ->
~ (in_lib ct (C d l l' lm) /\ in_app ct (C d l l' lm)) ->
((in_app ct d /\ in_app ct (C d l l' lm)) \/ (in_table ct (C d l l' lm) /\ in_lib ct d)) ->
Forall valid_method lm ->
valid_class ct (C d l l' lm).
Definition valid_table ct : Prop :=
forall c, in_table ct c -> valid_class ct c.
End table_defs.
Section auxiliary_defs.
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 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 context := var -> class.
End auxiliary_defs.
Section auxiliary_functions.
Definition mid (m : method) : method_id := match m with Method _ mi _ _ => mi end.
Definition args (m : method) : list var :=
match m with
| Method _ _ l _ => map Var (map snd l)
end.
Definition body (m : method) : expr := match m with Method _ _ _ e => e end.
Fixpoint fields (c : class) : list (class * field_id) :=
match c with
| Obj => nil
| C d fs _ _ => fs ++ fields d
end.
Definition dfields (c : class) : list (class * field_id) :=
match c with
| Obj => nil
| C d fs _ _ => fs
end.
Definition lookup_field (fi : field_id) (c : class) : option class :=
option_map fst (find (fun p => match p with (_, fi') => beq_nat fi fi' end) (fields c)).
Fixpoint declaring_class (fi : field_id) (c : class) : option class :=
match c with
| Obj => None
| C d fs _ _ => if existsb (fun p => beq_nat fi (snd p)) fs
then Some c
else declaring_class fi d
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 _ mi _ _ => mi end) ms
end.
Definition lookup_method (mi : method_id) (c : class) : option method :=
match c with
| Obj => None
| C _ _ _ ms => find (fun m => match m with Method _ mi' _ _ => beq_nat mi mi' end) ms
end.
Fixpoint find_method (mi : method_id) (c : class) : option method :=
match c with
| Obj => None
| C d _ _ _ => match lookup_method mi c with
| Some m => Some m
| None => find_method mi d
end
end.
Fixpoint mtype (mi : method_id) (c : class) : option (list class * class) :=
match find_method mi c with
| Some (Method ret _ par _) => Some (map fst par, ret)
| None => None
end.
Fixpoint mparams (mi : method_id) (c : class) : option (list var_id) :=
match find_method mi c with
| Some (Method ret _ par _) => Some (map snd par)
| None => None
end.
Fixpoint mbody (mi : method_id) (c : class) : option (list var_id * expr) :=
match find_method mi c with
| Some (Method _ _ par bod) => Some (map snd par, bod)
| None => None
end.
Fixpoint mresolve (m : method_id) (c : class) : option class :=
match c with
| Obj => None
| C d _ _ _ =>
if (existsb (fun m' => beq_nat m m') (dmethods_id c))
then Some c
else mresolve m d
end.
Definition extends (c d : class) : Prop :=
match c with
| Obj => False
| C d' _ _ _ => d = d'
end.
(*Fixpoint subst_from_list (l : list (expr * var)) : subst :=
match l with
| nil => fun _ => None
| (e, v) :: l' => fun v' => if beq_var v v' then Some e else subst_from_list l' v'
end.*)
End auxiliary_functions.
Section auxiliary_lemmas.
Lemma decl_of_lib_in_lib : forall ct c d f,
valid_table ct -> in_lib ct c -> declaring_class f c = Some d -> in_lib ct d.
Proof.
intros ct c d f pft c_in_lib decl.
induction c.
- inversion decl.
- simpl in decl. destruct (existsb (fun p : class * nat => PeanoNat.Nat.eqb f (snd p)) l).
+ inversion decl; trivial.
+ assert (valid_class ct (C c l l0 l1)).
{ apply pft. unfold in_table. right. trivial. }
inversion H. destruct H6 as [[par_app ch_app] | [ch_tab par_lib]].
* destruct H5. split; trivial.
* apply IHc; trivial.
Qed.
Lemma mresolve_in_methods : forall c d m,
mresolve m c = Some d -> In m (dmethods_id d).
Proof.
intros. induction c.
- inversion H.
- unfold mresolve in H.
destruct (existsb (fun m' : nat => PeanoNat.Nat.eqb m m') (dmethods_id (C c l l0 l1))) eqn:exst; eauto.
inversion H. rewrite existsb_exists in exst. destruct exst as [x [in_eqb]].
apply beq_nat_true in H0. rewrite H0. trivial.
Qed.
End auxiliary_lemmas.
Section props.
Definition ave_rel (ct ct' : class_table) : Prop :=
valid_table ct /\ valid_table ct' /\
(forall c, in_app ct c <-> in_app ct' c) /\
(forall c d, extends c d -> in_lib ct d -> in_table ct' c -> in_lib ct' d).
Inductive subtype : class -> class -> Prop :=
| S_Ref : forall c, subtype c c
| S_Trans : forall c d e, subtype c d -> subtype d e -> subtype c e
| S_Sub : forall c d, extends c d -> subtype c d.
Inductive type_of (gamma : context) : expr -> class -> Prop :=
| T_Var : forall x, type_of gamma (E_Var x) (gamma x)
| T_Field : forall e c d f,
type_of gamma e c -> lookup_field f c = Some d ->
type_of gamma (E_Field e f) d
| T_New : forall c le lc ld,
map fst (fields c) = ld ->
length ld = length le -> length le = length lc ->
Forall (fun p => type_of gamma (fst p) (snd p)) (combine le lc) ->
Forall (fun p => subtype (fst p) (snd p)) (combine lc ld) ->
type_of gamma (E_New c le) c
| T_Invk : forall e0 c0 ld ret mi le lc,
type_of gamma e0 c0 -> mtype mi c0 = Some (ld, ret) ->
length ld = length le -> length le = length lc ->
Forall (fun p => type_of gamma (fst p) (snd p)) (combine le lc) ->
Forall (fun p => subtype (fst p) (snd p)) (combine lc ld) ->
type_of gamma (E_Invk e0 mi le) ret
| T_Cast : forall e0 c d, type_of gamma e0 d -> type_of gamma (E_Cast c e0) c.
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 d f lpt,
alpha ct ct' gamma e E_Lib lpt ->
declaring_class f c = Some d -> in_lib ct d ->
type_of gamma e c ->
alpha ct ct' gamma (E_Field e f) E_Lib lpt
| Rel_New : forall c le le' lpt,
in_table ct' c -> 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 c le) (E_New c le') lpt
| Rel_Lib_New : forall c le lpt,
in_lib ct c -> Forall (fun e => alpha ct ct' gamma e E_Lib lpt) le ->
alpha ct ct' gamma (E_New c 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.
Inductive type_checks (ct : class_table) (gamma : context) : expr -> Prop :=
| TC_Var : forall x, in_table ct (gamma x) -> type_checks ct gamma (E_Var x)
| TC_Field : forall e f d, type_of gamma (E_Field e f) d -> in_table ct d -> type_checks ct gamma e ->
type_checks ct gamma (E_Field e f)
| TC_New : forall c le, in_table ct c -> Forall (type_checks ct gamma) le -> type_checks ct gamma (E_New c le)
| TC_Invk : forall e m le d, type_of gamma (E_Invk e m le) d -> in_table ct d ->
type_checks ct gamma e -> Forall (type_checks ct gamma) le ->
type_checks ct gamma (E_Invk e m le)
| TC_Cast : forall e d, in_table ct d -> type_checks ct gamma e -> type_checks ct gamma (E_Cast d e).
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 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 c,
type_checks ct gamma (E_Field e f) ->
type_of gamma e c ->
(exists d, declaring_class f c = Some d) ->
valid_expr ct gamma e ->
valid_expr ct gamma (E_Field e f)
| Val_New : forall c le,
type_checks ct gamma (E_New c le) ->
Forall (valid_expr ct gamma) le ->
valid_expr ct gamma (E_New c le)
| Val_Invk : forall e m le c,
type_checks ct gamma (E_Invk e m le) ->
valid_expr ct gamma e ->
Forall (valid_expr ct gamma) le ->
type_of gamma e c ->
(exists d, mresolve m c = Some d) ->
valid_expr ct gamma (E_Invk e m le)
| Val_Cast : forall c e,
type_checks ct gamma (E_Cast c e) -> valid_expr ct gamma e -> valid_expr ct gamma (E_Cast c e)
| Val_Lib : valid_expr ct gamma E_Lib.
Definition calP (ct : class_table) (gamma : context) (e : expr) (lpt : set expr) :=
type_checks ct gamma e /\ (forall e0, set_In e0 lpt -> type_checks ct gamma e0).
End props.
Section prop_lemmas.
Axiom sub_keep_type : forall (gamma : context) (sig : subst) (e : expr) (c : class),
type_of gamma e c -> type_of gamma (do_sub sig e) c.
Lemma type_of_fn : forall gamma e c d,
type_of gamma e c -> type_of gamma e d -> c = d.
Proof.
intros gamma e c d fc fd. generalize dependent d.
induction fc; intros d0 fd0; inversion fd0; trivial.
- assert (c = c0). apply IHfc; trivial.
rewrite <- H5 in H4. rewrite H4 in H. inversion H. trivial.
- assert (c0 = c1). apply IHfc; trivial.
rewrite H14 in H. rewrite H in H8. inversion H8; trivial.
Qed.
End prop_lemmas.
Section list_inductions.
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) (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) (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)
(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) (l : list expr), Forall P l -> P (E_New c l))
(f2 : forall e : expr, P e -> forall (m : method_id) (l : list expr), Forall P l -> P (E_Invk e m l))
(f3 : forall (c : class) (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 (Forall P) (Forall_nil P) (fun u _ => (Forall_cons u (F u))) l)
| E_Invk e0 m l => f2 e0 (F e0) m l (list_rect (Forall P) (Forall_nil P) (fun u _ => Forall_cons u (F u)) l)
| E_Cast c e0 => f3 c e0 (F e0)
| E_Lib => f4
end.
Print Forall_list_impl.
Print Forall.
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 d : class) (f0 : field_id) (lpt : set expr),
alpha ct ct' gamma e E_Lib lpt ->
P e E_Lib lpt ->
declaring_class f0 c = Some d -> in_lib ct d -> type_of gamma e c -> P (E_Field e f0) E_Lib lpt)
(f1 : forall (c : class) (le le' : list expr) (lpt : set expr),
in_table ct' c ->
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 c le) (E_New c le') lpt)
(f2 : forall (c : class) (le : list expr) (lpt : set expr),
in_lib ct c ->
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 c 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) (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) (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 d f8 lpt a0 e2 i t => f0 e1 c d f8 lpt a0 (F e1 E_Lib lpt a0) e2 i t
| Rel_New _ _ _ c le le' lpt i e1 f8 =>
f1 c 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 _ _ _ c le lpt i f8 =>
f2 c 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.
Section ct_lib_lemmas.
Lemma in_lib_in_ct_lib : forall ct c,
in_lib ct c -> in_table (ct_lib ct) c.
Proof.
intros ct c in_lib. destruct ct as [app lib].
simpl in in_lib. unfold in_table. right. simpl. trivial.
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 in_table in in_ct_lib. destruct ct as [app lib].
destruct in_ct_lib.
- inversion H.
- simpl. simpl in H. trivial.
Qed.
Lemma valid_table_valid_lib : forall (ct : class_table),
valid_table ct -> valid_table (ct_lib ct).
Proof.
unfold valid_table. intros ct val_ct c in_ct_lib. destruct ct as (app, lib).
apply in_ct_lib_in_lib in in_ct_lib as in_lib; trivial.
induction c as [|d].
- apply V_Obj; eauto.
- simpl.
assert (valid_class (app, lib) (C d l l0 l1)) as pfc.
{ apply val_ct. unfold in_table. right. trivial. }
dependent destruction pfc.
assert (lib d).
{ destruct H0.
- destruct H0. destruct H. split; trivial.
- apply H0. }
apply V_Class; eauto.
+ apply IHd; eauto. apply in_lib_in_ct_lib. eauto.
+ unfold not. intros. destruct H3. contradiction.
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;
try constructor; try apply TC_Field with d; try apply TC_Invk with d;
try (unfold in_table; right; apply in_ct_lib_in_lib; trivial);
try apply IHe; try (apply Forall_list_impl with (P := type_checks (ct_lib ct) gamma)); trivial.
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; try inversion val_lib;
try constructor; try (apply Val_Field with c); try (apply Val_Invk with c);
try apply IHe; try apply type_checks_lib_ct;
try (apply Forall_list_impl with (P := valid_expr (ct_lib ct) gamma)); trivial.
Qed.
Lemma typ_check_in_lib_typ_in_lib : forall ct gamma e c,
type_checks (ct_lib ct) gamma e -> type_of gamma e c -> in_lib ct c.
Proof.
intros ct gamma e c typ_chk typ.
destruct e; inversion typ; inversion typ_chk.
- apply in_ct_lib_in_lib; trivial.
- assert (c = d0). apply type_of_fn with gamma (E_Field e f); trivial.
rewrite H9. apply in_ct_lib_in_lib; trivial.
- rewrite <- H5. apply in_ct_lib_in_lib; trivial.
- assert (c = d). apply type_of_fn with gamma (E_Invk e m l); trivial.
rewrite H16. apply in_ct_lib_in_lib; trivial.
- rewrite <- H0. apply in_ct_lib_in_lib; trivial.
Qed.
End ct_lib_lemmas.
Section lemmas.
Lemma mres_lib_in_lib : forall (ct : class_table) (m : method_id) (c e : class),
valid_class ct c -> in_lib ct c -> mresolve m c = Some e -> in_lib ct e.
Proof.
intros ct m c e pfc c_in_app c_res_e.
induction c as [|d].
- inversion c_res_e.
- unfold mresolve in c_res_e. destruct (existsb (fun m' : nat => beq_nat m m') (dmethods_id (C d l l0 l1))).
+ inversion c_res_e; trivial.
+ fold mresolve in c_res_e. dependent destruction pfc. apply IHd; trivial.
destruct H0 as [[_ con] | [_ x]]; trivial.
destruct H. split; 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.
End lemmas.
Section eq_dec.
Lemma eq_var_dec : forall v v' : var, {v = v'} + {v <> v'}.
Proof.
intros. destruct v; destruct v'; try solve [right; unfold not; intro; inversion H].
- assert ({v = v0} + {v <> v0}) by apply PeanoNat.Nat.eq_dec.
destruct H.
+ left. rewrite e. reflexivity.
+ right. unfold not. intros. inversion H. contradiction.
- left. trivial.
Qed.
Inductive Forallt {A : Set} (P : A -> Set) : list A -> Set :=
| Forallt_nil : Forallt P nil
| Forallt_cons : forall a l, P a -> Forallt P l -> Forallt P (a :: l).
Lemma eq_expr_class_dech : forall (e e' : expr) (c c' : class), ({e = e'} + {e <> e'}) * ({c = c'} + {c <> c'}).
Proof.
induction e using expr_rect_list with (Q := fun l => forall e', Forallt (fun e : expr => {e = e'} + {e <> e'}) l).
- intros. apply Forallt_nil.
- intros. assert (({e = e'} + {e <> e'}) * ({Obj = Obj} + {Obj <> Obj})). apply IHe.
destruct H. apply Forallt_cons; eauto.
- intros. destruct e'.
+ assert ({ v = v0 } + {v <> v0}) by apply eq_var_dec.
destruct H.
* Admitted. (*left. rewrite e. reflexivity.
* right. unfold not. intros. inversion H. contradiction.
+ right. unfold not. intro. inversion H.
+ right. unfold not. intro. inversion H.
+ right. unfold not. intro. inversion H.
+ right. unfold not. intro. inversion H.
+ right. unfold not. intro. inversion H.
- intros. destruct e'.
+ right. unfold not. intro. inversion H.
+ assert ({f = f0} + {f <> f0}) by apply PeanoNat.Nat.eq_dec.
assert ({e = e'} + {e <> e'}) by apply IHe.
destruct H; destruct H0.
* left. rewrite e0. rewrite e1. reflexivity.
* right. unfold not. intros. inversion H. contradiction.
* right. unfold not. intros. inversion H. symmetry in H2. contradiction.
* right. unfold not. intros. inversion H. contradiction.
+ right. unfold not. intro. inversion H.
+ right. unfold not. intro. inversion H.
+ right. unfold not. intro. inversion H.
+ right. unfold not. intro. inversion H.
- intros. destruct e'.
+ right. unfold not. intro. inversion H.
+ right. unfold not. intro. inversion H.
+ (* assert ({c = c0} + {c <> c0}) by apply PeanoNat.Nat.eq_dec. *)
Admitted. *)
Lemma eq_class_dec : forall c c' : class, {c = c'} + {c <> c'}.
Proof.
intros.
assert (({E_Var This = E_Var This} + {E_Var This <> E_Var This}) * ({c = c'} + {c <> c'})).
apply eq_expr_class_dech.
destruct H.
apply s0.
Qed.
Lemma eq_expr_dec : forall e e' : expr, {e = e'} + {e <> e'}.
Proof.
intros e.