-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDEX_ImplemDomain.v
1525 lines (1392 loc) · 53.2 KB
/
DEX_ImplemDomain.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
(** * Bicolano: Semantic domains (interface implementation) *)
(* Hendra : - Modified to suit DEX program (Removed Operand Stack).
- Removed Reference comparison.
- Also trim the system to contain only Arithmetic *)
Require Export DEX_ImplemProgramWithMap.
Require Export DEX_Domain.
Ltac caseeq t := generalize (refl_equal t); pattern t at -1 in |- * ; case t.
(** All semantic domains and basic operation are encapsulated in a module signature *)
Module DEX_Dom <: DEX_SEMANTIC_DOMAIN.
(** We depend on the choices done for program data structures *)
Module DEX_Prog := DEX_ImplemProgramWithMap.DEX_Make.
Import DEX_Prog.
Open Scope Z_scope.
Module SByte <: Numeric.NUMSIZE. Definition power := 7%nat. End SByte.
Module SShort<: Numeric.NUMSIZE. Definition power := 15%nat. End SShort.
Module SInt <: Numeric.NUMSIZE. Definition power := 31%nat. End SInt.
Module Byte : Numeric.NUMERIC with Definition power := 7%nat := Numeric.Make SByte.
Module Short : Numeric.NUMERIC with Definition power := 15%nat := Numeric.Make SShort.
Module Int : Numeric.NUMERIC with Definition power := 31%nat := Numeric.Make SInt.
(** conversion *)
Definition b2i (b:Byte.t) : Int.t := Int.const (Byte.toZ b).
Definition s2i (s:Short.t) : Int.t := Int.const (Short.toZ s).
Definition i2b (i:Int.t) : Byte.t := Byte.const (Int.toZ i).
Definition i2s (i:Int.t) : Short.t := Short.const (Int.toZ i).
Definition i2bool (i:Int.t) : Byte.t := Byte.const (Int.toZ i mod 2).
Inductive DEX_num : Set :=
| I : Int.t -> DEX_num
| B : Byte.t -> DEX_num
| Sh : Short.t -> DEX_num.
(** Location is the domain of adresses in the heap *)
(* Hendra 10082016 - Only concerns DVM_I
Definition DEX_Location : Set := N.
Definition DEX_Location_dec : forall loc1 loc2:DEX_Location,{loc1=loc2}+{~loc1=loc2} :=
(Aeq_Dec _ _ Neq_spec).
*)
Inductive DEX_value : Set :=
| Num : DEX_num -> DEX_value
(* Hendra 10082016 - Only concerns DVM_I | Ref: DEX_Location -> DEX_value
| Null : DEX_value *).
Definition init_value (t:DEX_type) : DEX_value :=
match t with
(* Hendra 10082016 - Only concerns DVM_I | DEX_ReferenceType _ => Null *)
| DEX_PrimitiveType _ => Num (I (Int.const 0))
end.
(* Hendra 10082016 - Only concerns DVM_I
Definition init_field_value (f:DEX_Field) : DEX_value :=
match DEX_FIELD.initValue f with
| DEX_FIELD.Int z => Num (I (Int.const z))
| DEX_FIELD.NULL => Null
| DEX_FIELD.UNDEF => init_value (DEX_FIELDSIGNATURE.type (DEX_FIELD.signature f))
end.
*)
(** Domain of local variables *)
Module Type DEX_REGISTERS.
Parameter t : Type.
Parameter get : t-> DEX_Reg -> option DEX_value.
Parameter update : t -> DEX_Reg -> DEX_value -> t.
Parameter dom : t -> list DEX_Reg.
(*Parameter ret : DEX_Reg.*)
(* Hendra 10082016 - no need for r0? Parameter r0 : DEX_Reg. *)
Parameter get_update_new : forall l x v, get (update l x v) x = Some v.
Parameter get_update_old : forall l x y v,
x<>y -> get (update l x v) y = get l y.
End DEX_REGISTERS.
Module DEX_MapReg <: MAP with Definition key := DEX_Reg := BinNatMap.
Module DEX_Registers <: DEX_REGISTERS.
Definition t := DEX_MapReg.t DEX_value.
Definition get : t -> DEX_Reg -> option DEX_value := @DEX_MapReg.get DEX_value.
Definition update : t -> DEX_Reg -> DEX_value -> t := @DEX_MapReg.update DEX_value.
Definition dom : t -> list DEX_Reg := @DEX_MapReg.dom DEX_value.
(*Definition ret := Npos (xO (xO (xO (xO (xO (xO (xO (xO
(xO (xO (xO (xO (xO (xO (xO (xO xH)))))))))))))))).*)
(* Hendra 10082016 no need for r0? Definition r0 := (0)%N. *)
(* Definition ex := Npos (xI (xO (xO (xO (xO (xO (xO (xO
(xO (xO (xO (xO (xO (xO (xO (xO xH)))))))))))))))). *)
Lemma get_update_new : forall l x v, get (update l x v) x = Some v.
Proof. exact (DEX_MapReg.get_update1 DEX_value). Qed.
Lemma get_update_old : forall l x y v,
x<>y -> get (update l x v) y = get l y.
Proof.
intros;refine (DEX_MapReg.get_update2 DEX_value _ _ _ _ _).
intro;apply H;subst;trivial.
Qed.
Definition empty := DEX_MapReg.empty DEX_value.
End DEX_Registers.
Fixpoint listreg2regs_rec
(l_ori:DEX_Registers.t) (n:nat) (lv:list DEX_Reg) (l:DEX_Registers.t) {struct n}: DEX_Registers.t :=
match n with
| O => l
| S n =>
match lv with
| nil => l
| h :: t =>
match DEX_Registers.get l_ori h with
| None =>
listreg2regs_rec l_ori n t l
| Some v =>
listreg2regs_rec l_ori n t (DEX_Registers.update l (N_toReg n) v)
end
end
end.
Definition listreg2regs (l_ori:DEX_Registers.t) (n:nat) (lv:list DEX_Reg)
:= listreg2regs_rec l_ori n lv DEX_Registers.empty.
Fixpoint all_super_classes (p:DEX_Program) (c:DEX_Class) (n:nat) {struct n} : option (list DEX_Class) :=
match n with
| O => None
| S n =>
match DEX_CLASS.superClass c with
| None => Some nil
| Some super_name =>
match DEX_PROG.class p super_name with
| None => None
| Some super =>
match (all_super_classes p super n) with
| None => None
| Some l => Some (super::l)
end
end
end
end.
Ltac inv H := inversion H; subst; clear H.
Lemma clos_refl_trans_ind2 :
forall (A:Type) (R:A -> A -> Prop) (P:A -> A -> Prop),
(forall x, P x x) ->
(forall x y z:A, R x y -> clos_refl_trans A R y z -> P y z -> P x z) ->
forall x y, clos_refl_trans A R x y -> P x y.
Proof.
intros A R P H1 H2.
assert (forall x y, clos_refl_trans A R x y ->
forall z, clos_refl_trans A R y z -> P y z -> P x z).
induction 1; eauto; intros.
apply IHclos_refl_trans1.
constructor 3 with z; auto.
apply IHclos_refl_trans2; auto.
intros.
apply H with y; auto.
constructor 2.
Qed.
Lemma subclass_left : forall p c1 c2,
subclass p c1 c2 -> c1=c2 \/ (exists c, direct_subclass p c1 c /\ subclass p c c2).
Proof.
intros p; unfold subclass; apply clos_refl_trans_ind2; intros; auto.
destruct H1; subst; auto.
right; exists z; auto.
destruct H1 as [c [T1 T2]].
right; exists y; split; auto.
Qed.
Definition all_super_classes_correct : forall p n c l,
all_super_classes p c n = Some l ->
DEX_PROG.defined_Class p c ->
forall c', subclass p c c' -> In c' (c::l).
Proof.
induction n; simpl.
intros; discriminate.
intros c l; case_eq (DEX_CLASS.superClass c).
intros c' H'.
case_eq (DEX_PROG.class p c'); try (intros; discriminate).
intros c'' H''.
case_eq (all_super_classes p c'' n); try (intros; discriminate).
intros.
inv H0.
destruct (subclass_left _ _ _ H2); auto.
destruct H0 as [c0 [T1 T2]].
clear H2.
inv T1.
unfold DEX_PROG.defined_Class in *.
assert (c0 = c'') by congruence.
assert (c'=DEX_CLASS.name c'') by congruence.
subst.
clear H2 H3 H1.
right; apply (IHn _ _ H H'' _ T2).
intros.
destruct (subclass_left _ _ _ H2); auto.
destruct H3 as [c0 [T1 T2]].
inv T1.
congruence.
Qed.
Fixpoint all_super_interfaces (p:DEX_Program) (n:nat) {struct n} :
DEX_Interface -> option (list DEX_Interface) :=
match n with
| O => fun _ => None
| S n => fun c =>
List.fold_left
(fun o iname =>
match o with
| None => None
| Some l =>
match DEX_PROG.interface p iname with
| None => None
| Some itf =>
match all_super_interfaces p n itf with
| None => None
| Some l' => Some (itf::l++l')
end
end
end)
(DEX_INTERFACE.superInterfaces c)
(Some (c::nil))
end.
Lemma subinterface_left : forall p c1 c2,
subinterface p c1 c2 -> c1=c2 \/ (exists c, direct_subinterface p c1 c /\ subinterface p c c2).
Proof.
intros p; unfold subinterface; apply clos_refl_trans_ind2; intros; auto.
destruct H1; subst; auto.
right; exists z; auto.
destruct H1 as [c [T1 T2]].
right; exists y; split; auto.
Qed.
Lemma all_super_interfaces_aux : forall p n l3,
fold_left
(fun (o : option (list DEX_Interface)) (iname : DEX_InterfaceName) =>
match o with
| Some l6 =>
match DEX_PROG.interface p iname with
| Some itf =>
match all_super_interfaces p n itf with
| Some l' => Some (itf :: l6 ++ l')
| None => None (A:=list DEX_Interface)
end
| None => None (A:=list DEX_Interface)
end
| None => None (A:=list DEX_Interface)
end) l3 None=None.
Proof.
induction l3; simpl; auto.
Qed.
Lemma all_super_interfaces_aux' : forall p n l0 l1 l2,
fold_left
(fun (o : option (list DEX_Interface)) (iname : DEX_InterfaceName) =>
match o with
| Some l =>
match DEX_PROG.interface p iname with
| Some itf =>
match all_super_interfaces p n itf with
| Some l' => Some (itf :: l ++ l')
| None => None (A:=list DEX_Interface)
end
| None => None (A:=list DEX_Interface)
end
| None => None (A:=list DEX_Interface)
end) l0 (Some l1) = Some l2 -> incl l1 l2.
Proof.
induction l0; simpl.
intros.
inversion H; subst; intro; auto.
destruct (DEX_PROG.interface p a); try (intros; discriminate).
destruct (all_super_interfaces p n d); try (intros; discriminate).
intros.
assert (IH:=IHl0 _ _ H); clear H IHl0.
repeat intro; apply IH.
right; auto with datatypes.
rewrite all_super_interfaces_aux; intros; discriminate.
rewrite all_super_interfaces_aux; intros; discriminate.
Qed.
Definition all_super_interfaces_correct : forall p n c l,
all_super_interfaces p n c = Some l ->
DEX_PROG.defined_Interface p c ->
forall c', subinterface p c c' -> In c' (c::l).
Proof.
induction n; simpl.
intros; discriminate.
intros.
destruct (subinterface_left _ _ _ H1); clear H1; auto.
right; destruct H2 as [c0 [T1 T2]].
inv T1.
generalize dependent (Some (c::nil)).
generalize dependent (DEX_INTERFACE.superInterfaces c).
induction l0; simpl; intros.
elim H3.
destruct o; simpl in H; try discriminate.
case_eq (DEX_PROG.interface p a); intros.
rewrite H4 in H.
case_eq (all_super_interfaces p n d); intros.
rewrite H5 in H.
destruct H3; subst.
unfold DEX_PROG.defined_Interface in *.
assert (c0=d) by congruence; clear H4; subst.
generalize (IHn _ _ H5 H2 _ T2).
assert (T:=all_super_interfaces_aux' _ _ _ _ _ H).
simpl; intros.
apply T.
destruct H3.
left; auto.
right; auto with datatypes.
apply IHl0 with (Some (d :: l1 ++ l2)); auto.
rewrite H5 in H.
rewrite all_super_interfaces_aux in H; discriminate.
rewrite H4 in H.
rewrite all_super_interfaces_aux in H; discriminate.
rewrite all_super_interfaces_aux in H; discriminate.
Qed.
Definition all_interfaces (p:DEX_Program) (n:nat) (c:DEX_Class) : option (list DEX_Interface) :=
List.fold_left
(fun o iname =>
match o with
| None => None
| Some l =>
match DEX_PROG.interface p iname with
| None => None
| Some itf =>
match all_super_interfaces p n itf with
| None => None
| Some l' => Some (itf::l++l')
end
end
end)
(DEX_CLASS.superInterfaces c)
(Some nil).
Lemma all_interfaces_correct : forall p n c l,
all_interfaces p n c = Some l -> forall i I I',
In i (DEX_CLASS.superInterfaces c) ->
DEX_PROG.interface p i = Some I ->
subinterface p I I' ->
In I l.
Proof.
unfold all_interfaces.
intros p n c.
generalize (@nil DEX_Interface).
generalize (DEX_CLASS.superInterfaces c).
induction l; simpl.
intuition.
intros l0; case_eq (DEX_PROG.interface p a).
intros i Hi; case_eq (all_super_interfaces p n i); intros.
destruct H1; subst.
assert (I0=i) by congruence; subst; clear Hi.
apply (all_super_interfaces_aux' _ _ _ _ _ H0).
left; reflexivity.
eapply IHl ;eauto.
rewrite all_super_interfaces_aux in H0; discriminate.
intros.
rewrite all_super_interfaces_aux in H0; intros; discriminate.
Qed.
Set Implicit Arguments.
(* Hendra 10082016 - Only concerns DVM_I
Module Type DEX_HEAP.
Parameter t : Type.
Inductive DEX_AdressingMode : Set :=
| StaticField : DEX_FieldSignature -> DEX_AdressingMode
| DynamicField : DEX_Location -> DEX_FieldSignature -> DEX_AdressingMode
| ArrayElement : DEX_Location -> Z -> DEX_AdressingMode.
Inductive DEX_LocationType : Type :=
| LocationObject : DEX_ClassName -> DEX_LocationType
| LocationArray : Int.t -> DEX_type -> DEX_Method*DEX_PC -> DEX_LocationType.
(** (LocationArray length element_type) *)
Parameter get : t -> DEX_AdressingMode -> option DEX_value.
Parameter update : t -> DEX_AdressingMode -> DEX_value -> t.
Parameter typeof : t -> DEX_Location -> option DEX_LocationType.
(** typeof h loc = None -> no object, no array allocated at location loc *)
Parameter new : t -> DEX_Program -> DEX_LocationType -> option (DEX_Location * t).
(** program is required to compute the size of the allocated element, i.e. to know
the Class associated with a ClassName *)
(** Compatibility between a heap and an adress *)
Inductive Compat (h:t) : DEX_AdressingMode -> Prop :=
| CompatStatic : forall f,
Compat h (StaticField f)
| CompatObject : forall cn loc f,
typeof h loc = Some (LocationObject cn) ->
Compat h (DynamicField loc f)
| CompatArray : forall length tp loc i a,
0 <= i < Int.toZ length ->
typeof h loc = Some (LocationArray length tp a) ->
Compat h (ArrayElement loc i).
Parameter get_update_same : forall h am v, Compat h am -> get (update h am v) am = Some v.
Parameter get_update_old : forall h am1 am2 v, am1<>am2 -> get (update h am1 v) am2 = get h am2.
Parameter get_uncompat : forall h am, ~ Compat h am -> get h am = None.
Parameter typeof_update_same : forall h loc am v,
typeof (update h am v) loc = typeof h loc.
Parameter new_fresh_location : forall (h:t) (p:DEX_Program) (lt:DEX_LocationType) (loc:DEX_Location) (h':t),
new h p lt = Some (loc,h') ->
typeof h loc = None.
Parameter new_typeof : forall (h:t) (p:DEX_Program) (lt:DEX_LocationType) (loc:DEX_Location) (h':t),
new h p lt = Some (loc,h') ->
typeof h' loc = Some lt.
Parameter new_typeof_old : forall (h:t) (p:DEX_Program) (lt:DEX_LocationType) (loc loc':DEX_Location) (h':t),
new h p lt = Some (loc,h') ->
loc <> loc' ->
typeof h' loc' = typeof h loc'.
Parameter new_defined_object_field : forall (h:t) (p:DEX_Program) (cn:DEX_ClassName) (fs:DEX_FieldSignature) (f:DEX_Field) (loc:DEX_Location) (h':t),
new h p (LocationObject cn) = Some (loc,h') ->
is_defined_field p cn fs f ->
get h' (DynamicField loc fs) = Some (init_field_value f).
Parameter new_undefined_object_field : forall (h:t) (p:DEX_Program) (cn:DEX_ClassName) (fs:DEX_FieldSignature) (loc:DEX_Location) (h':t),
new h p (LocationObject cn) = Some (loc,h') ->
~ defined_field p cn fs ->
get h' (DynamicField loc fs) = None.
Parameter new_object_no_change :
forall (h:t) (p:DEX_Program) (cn:DEX_ClassName) (loc:DEX_Location) (h':t) (am:DEX_AdressingMode),
new h p (LocationObject cn) = Some (loc,h') ->
(forall (fs:DEX_FieldSignature), am <> (DynamicField loc fs)) ->
get h' am = get h am.
Parameter new_valid_array_index : forall (h:t) (p:DEX_Program) (length:Int.t) (tp:DEX_type) a (i:Z) (loc:DEX_Location) (h':t),
new h p (LocationArray length tp a) = Some (loc,h') ->
0 <= i < Int.toZ length ->
get h' (ArrayElement loc i) = Some (init_value tp).
Parameter new_unvalid_array_index : forall (h:t) (p:DEX_Program) (length:Int.t) (tp:DEX_type) a (i:Z) (loc:DEX_Location) (h':t),
new h p (LocationArray length tp a) = Some (loc,h') ->
~ 0 <= i < Int.toZ length ->
get h' (ArrayElement loc i) = None.
Parameter new_array_no_change :
forall (h:t) (p:DEX_Program) (length:Int.t) (tp:DEX_type) a (loc:DEX_Location) (h':t) (am:DEX_AdressingMode),
new h p (LocationArray length tp a) = Some (loc,h') ->
(forall (i:Z), am <> (ArrayElement loc i)) ->
get h' am = get h am.
(* These properties should be useless
Parameter get_static_some : forall (h:t) (p:Program) (fs:FieldSignature),
isStatic p fs ->
exists v, get h (StaticField fs) = Some v.
Parameter get_static_some : forall (h:t) (p:Program) (fs:FieldSignature),
~ isStatic p fs ->
exists v, get h (StaticField fs) = None.
*)
End DEX_HEAP.
Module DEX_Heap <: DEX_HEAP.
Module DEX_Object.
Record t : Type := Obj { class : DEX_ClassName; fieldsval : DEX_MapFieldSignature.t DEX_value }.
Definition get (o:t) := DEX_MapFieldSignature.get DEX_value o.(fieldsval).
Definition op_get (o:option t) :=
match o with
| Some o => get o
| None => fun f => None
end.
Definition op_update (f:DEX_FieldSignature) (v:DEX_value) (o:option t) : option t :=
match o with
| None => None
| Some o => Some (Obj o.(class) (DEX_MapFieldSignature.update _ o.(fieldsval) f v))
end.
Definition init_fields : list (DEX_ClassName*DEX_Field) -> DEX_MapFieldSignature.t DEX_value :=
@fold_right (DEX_MapFieldSignature.t DEX_value) _
(fun f m => DEX_MapFieldSignature.update _ m (fst f,DEX_FIELD.signature (snd f)) (init_field_value (snd f)))
(DEX_MapFieldSignature.empty _).
Definition default (c:DEX_Class) : t :=
Obj
(DEX_CLASS.name c)
(init_fields (List.map (fun f => (DEX_CLASS.name c,f)) (DEX_CLASS.definedFields c))).
End DEX_Object.
Module DEX_Array.
Record t : Type := Arr {
length : Int.t;
element_type : DEX_type;
elements : BinNatMap.t DEX_value;
creation_point : DEX_Method * DEX_PC
}.
Definition Ncase (i:Z) : option N :=
match i with
| Z0 => Some N0
| Zpos p => Some (Npos p)
| _ => None
end.
Definition get (a:t) (i:Z) :=
match Ncase i with
| Some n =>
match Zcompare i (Int.toZ a.(length)) with
| Lt => BinNatMap.get DEX_value a.(elements) n
| _ => None
end
| None => None
end.
Definition op_get (a:option (t)) :=
match a with
| Some a => get a
| None => fun i => None
end.
Definition op_update (i:Z) (v:DEX_value) (a:option t) : option t :=
match a with
| None => None
| Some a =>
match Ncase i with
| Some n =>
match Zcompare i (Int.toZ a.(length)) with
| Lt => Some
(Arr a.(length) a.(element_type) (BinNatMap.update _ a.(elements) n v) a.(creation_point))
| _ => Some a
end
| _ => Some a
end
end.
Fixpoint init_array_rec (n:nat) (v:DEX_value) {struct n} : BinNatMap.t DEX_value :=
match n with
| O => BinNatMap.empty _
| S p => BinNatMap.update _ (init_array_rec p v) (N_of_nat p) v
end.
Definition default (lgth:Int.t) (n:nat) (tp:DEX_type) (a:DEX_Method*DEX_PC): t:=
Arr lgth tp (init_array_rec n (init_value tp)) a.
Definition N_to_nat (n:N) : nat :=
match n with
| Npos p => nat_of_P p
| N0 => O
end.
Lemma ZL4_inf : forall y:positive, {h : nat | nat_of_P y = S h}.
Proof.
intro y; induction y as [p H| p H1| ];
[ elim H; intros x H1; exists (S x + S x)%nat; unfold nat_of_P in |- *; simpl in |- *; rewrite ZL0; rewrite Pmult_nat_r_plus_morphism; unfold nat_of_P in H1; rewrite H1; auto with arith | elim H1; intros x H2; exists (x + S x)%nat; unfold nat_of_P in |- *; simpl in |- *; rewrite ZL0; rewrite Pmult_nat_r_plus_morphism; unfold nat_of_P in H2; rewrite H2; auto with arith | exists 0%nat; auto with arith ].
Qed.
Lemma N_of_nat_N_to_nat : forall n, N_of_nat (N_to_nat n) = n.
Proof.
unfold N_of_nat, N_to_nat; intros.
destruct n.
reflexivity.
destruct (ZL4_inf p).
rewrite e.
generalize (nat_of_P_o_P_of_succ_nat_eq_succ x).
rewrite <- e; intros.
assert (Zpos (P_of_succ_nat x) = Zpos p).
rewrite Zpos_eq_Z_of_nat_o_nat_of_P.
rewrite H.
rewrite Zpos_eq_Z_of_nat_o_nat_of_P.
auto.
congruence.
Qed.
Lemma N_to_nat_N_of_nat : forall n, N_to_nat (N_of_nat n) = n.
Proof.
unfold N_of_nat, N_to_nat; intros.
destruct n.
reflexivity.
rewrite nat_of_P_o_P_of_succ_nat_eq_succ.
reflexivity.
Qed.
Lemma get_init_array_rec: forall n v i,
(N_to_nat i<n)%nat ->
BinNatMap.get _ (init_array_rec n v) i = Some v.
Proof.
induction n; simpl; intros.
apply False_ind; omega.
assert (N_to_nat i < n \/ N_to_nat i = n)%nat by omega.
destruct H0; clear H.
rewrite BinNatMap.get_update2.
auto.
intro; subst.
rewrite N_to_nat_N_of_nat in H0; omega.
subst.
rewrite N_of_nat_N_to_nat.
rewrite BinNatMap.get_update1.
reflexivity.
Qed.
Lemma get_init_array_none_rec: forall n v i,
(N_to_nat i>=n)%nat ->
BinNatMap.get _ (init_array_rec n v) i = None.
Proof.
induction n; simpl; intros.
apply BinNatMap.get_empty.
rewrite BinNatMap.get_update2.
apply IHn.
omega.
intro; subst.
rewrite N_to_nat_N_of_nat in H; omega.
Qed.
End DEX_Array.
Module DEX_LocMap := BinNatMap.
Record t_ : Type := hp {
statics : DEX_MapFieldSignature.t DEX_value;
objects : DEX_LocMap.t DEX_Object.t;
arrays : DEX_LocMap.t DEX_Array.t;
next_loc : DEX_Location
}.
Definition t : Type := t_.
Inductive DEX_AdressingMode : Set :=
| StaticField : DEX_FieldSignature -> DEX_AdressingMode
| DynamicField : DEX_Location -> DEX_FieldSignature -> DEX_AdressingMode
| ArrayElement : DEX_Location -> Z -> DEX_AdressingMode.
Inductive DEX_LocationType : Type :=
| LocationObject : DEX_ClassName -> DEX_LocationType
| LocationArray : Int.t -> DEX_type -> DEX_Method*DEX_PC -> DEX_LocationType.
Definition typeof (h:t) (l:DEX_Location) : option (DEX_LocationType) :=
match DEX_LocMap.get _ h.(objects) l with
| Some o => Some (LocationObject o.(DEX_Object.class))
| None =>
match DEX_LocMap.get _ h.(arrays) l with
| Some a => Some (LocationArray a.(DEX_Array.length) a.(DEX_Array.element_type) a.(DEX_Array.creation_point))
| None => None
end
end.
(** Compatibility between a heap and an adress *)
Inductive Compat (h:t) : DEX_AdressingMode -> Prop :=
| CompatStatic : forall f,
Compat h (StaticField f)
| CompatObject : forall cn loc f,
typeof h loc = Some (LocationObject cn) ->
Compat h (DynamicField loc f)
| CompatArray : forall length tp loc i a,
0 <= i < Int.toZ length ->
typeof h loc = Some (LocationArray length tp a) ->
Compat h (ArrayElement loc i).
Definition check_compat (h:t) (am:DEX_AdressingMode) : bool :=
match am with
| StaticField f => true
| DynamicField loc f =>
match typeof h loc with
| Some (LocationObject _) => true
| _ => false
end
| ArrayElement loc i =>
match typeof h loc with
| Some (LocationArray length _ _ ) =>
match i ?= 0 with
| Lt => false
| _ =>
match i ?= Int.toZ length with
| Lt => true
| _ => false
end
end
| _ => false
end
end.
Lemma bounded_compare : forall i t:Z,
i < t -> i ?= t = Lt.
Proof.
intros.
generalize (Zlt_compare _ _ H); rewrite H; auto.
Qed.
Lemma case_compare : forall i t:Z,
(i ?= t = Lt /\ i<t) \/ (i ?=t = Gt) \/ (i ?=t = Eq).
Proof.
intros.
generalize (Zge_compare i t0).
destruct (i ?= t0); simpl; auto.
left; split; auto; omega.
Qed.
Lemma check_compat_correct : forall h am,
if check_compat h am then Compat h am else ~ Compat h am.
Proof.
unfold check_compat; intros.
destruct am.
constructor.
case_eq (typeof h d); intros.
destruct d1.
econstructor; eauto.
intro T; inversion T; subst.
rewrite H1 in H; discriminate.
intro T; inversion T; subst.
rewrite H1 in H; discriminate.
case_eq (typeof h d); intros.
destruct d0.
intro T; inversion T; subst.
rewrite H3 in H; discriminate.
generalize (Zge_compare z 0).
generalize (Zlt_compare z 0).
destruct (z ?= 0).
generalize (Zge_compare z (Int.toZ t0)).
generalize (Zlt_compare z (Int.toZ t0)).
destruct (z ?= (Int.toZ t0)).
repeat intro.
inversion H4; subst.
rewrite H8 in H; inversion H; subst; omega.
intros; econstructor; eauto.
omega.
repeat intro.
inversion H4; subst.
rewrite H8 in H; inversion H; subst; omega.
repeat intro.
inversion H2; subst.
rewrite H6 in H; inversion H; subst; omega.
generalize (Zge_compare z (Int.toZ t0)).
generalize (Zlt_compare z (Int.toZ t0)).
destruct (z ?= (Int.toZ t0)).
repeat intro.
inversion H4; subst.
rewrite H8 in H; inversion H; subst; omega.
intros; econstructor; eauto.
omega.
repeat intro.
inversion H4; subst.
rewrite H8 in H; inversion H; subst; omega.
intro.
inversion H0; subst.
rewrite H4 in H; inversion H.
Qed.
Definition get (h:t) (am:DEX_AdressingMode) : option DEX_value :=
if check_compat h am then
match am with
| StaticField f => DEX_MapFieldSignature.get _ h.(statics) f
| DynamicField l f => DEX_Object.op_get (DEX_LocMap.get _ h.(objects) l) f
| ArrayElement l i => DEX_Array.op_get (DEX_LocMap.get _ h.(arrays) l) i
end
else None.
Definition update (h:t) (am:DEX_AdressingMode) (v:DEX_value) : t :=
if check_compat h am then
match am with
| StaticField f =>
hp
(DEX_MapFieldSignature.update _ h.(statics) f v)
h.(objects)
h.(arrays)
h.(next_loc)
| DynamicField l f =>
hp
h.(statics)
(DEX_LocMap.modify _ h.(objects) l (DEX_Object.op_update f v))
h.(arrays)
h.(next_loc)
| ArrayElement l i =>
hp
h.(statics)
h.(objects)
(DEX_LocMap.modify _ h.(arrays) l (DEX_Array.op_update i v))
h.(next_loc)
end else h.
Definition Int_to_nat (i:Int.t) : option nat :=
match Int.toZ i with
| Zpos p => Some (nat_of_P p)
| Z0 => Some O
| Zneg _ => None
end.
Definition check_fresh (h:t) (l:DEX_Location) : bool :=
match DEX_LocMap.get _ h.(objects) l with
| Some _ => false
| _ =>
match DEX_LocMap.get _ h.(arrays) l with
| Some _ => false
| _ => true
end
end.
Lemma check_fresh_correct : forall h l,
check_fresh h l = true ->
(DEX_LocMap.get _ h.(objects) l = None
/\ DEX_LocMap.get _ h.(arrays) l = None).
Proof.
unfold check_fresh; intros.
destruct (DEX_LocMap.get _ (objects h) l); try discriminate.
destruct (DEX_LocMap.get _ (arrays h) l); try discriminate.
auto.
Qed.
Definition new (h:t) (p:DEX_Program) (ltp:DEX_LocationType) : option (DEX_Location * t) :=
if check_fresh h h.(next_loc) then
match ltp with
| LocationObject cn =>
match DEX_PROG.class p cn with
| None => None
| Some c =>
Some
(h.(next_loc),
hp
h.(statics)
(DEX_LocMap.update _ h.(objects) h.(next_loc) (DEX_Object.default c))
h.(arrays)
(Nsucc h.(next_loc)))
end
| LocationArray lgth tp a =>
match Int_to_nat lgth with
| None => None
| Some n =>
Some
(h.(next_loc),
hp
h.(statics)
h.(objects)
(DEX_LocMap.update _ h.(arrays) h.(next_loc) (DEX_Array.default lgth n tp a))
(Nsucc h.(next_loc)))
end
end
else None.
Lemma pos_Ncase : forall i:Z,
0<=i -> exists n, DEX_Array.Ncase i = Some n.
Proof.
destruct i; simpl; intros; eauto.
elim H; auto.
Qed.
Lemma case_Ncase : forall i:Z,
DEX_Array.Ncase i = None \/
exists n, DEX_Array.Ncase i = Some n /\ 0<=i.
Proof.
destruct i; simpl; eauto with zarith.
Qed.
Lemma Ncase_inj : forall i j,
DEX_Array.Ncase i = DEX_Array.Ncase j -> DEX_Array.Ncase i <> None -> i = j.
Proof.
destruct i; destruct j; intros H; inversion H; auto.
simpl; intuition.
Qed.
Lemma typeof_update_same : forall h loc am v,
typeof (update h am v) loc = typeof h loc.
Proof.
unfold typeof, update; intros.
generalize (check_compat_correct h am); destruct (check_compat h am); simpl; intros.
destruct H; simpl.
auto.
destruct (DEX_PC_eq_dec loc0 loc); subst.
unfold typeof in H.
rewrite DEX_LocMap.get_modify1; auto.
unfold DEX_LocMap.get in H.
unfold DEX_LocMap.get in *.
case_eq (BinNatMap_Base.get DEX_Object.t (objects h) loc); intros.
rewrite H0 in H.
reflexivity.
rewrite H0 in H.
reflexivity.
rewrite DEX_LocMap.get_modify2; auto.
destruct (DEX_PC_eq_dec loc0 loc); subst.
rewrite DEX_LocMap.get_modify1; auto.
unfold typeof in *.
destruct (DEX_LocMap.get DEX_Object.t (objects h) loc); try reflexivity.
unfold DEX_LocMap.get in *.
case_eq (BinNatMap_Base.get _ (arrays h) loc); simpl; intros.
rewrite H1 in H0.
inversion H0; clear H0; subst.
elim (@pos_Ncase i); [intros n H2|omega].
rewrite H2.
rewrite bounded_compare; try omega.
reflexivity.
reflexivity.
rewrite DEX_LocMap.get_modify2; auto.
reflexivity.
Qed.
Lemma Compat_update1 : forall h am am0 v0,
Compat h am -> Compat (update h am0 v0) am.
Proof.
destruct 1; econstructor; eauto.
rewrite typeof_update_same; eauto.
rewrite typeof_update_same; eauto.
Qed.
Lemma Compat_update2 : forall h am am0 v0,
Compat (update h am0 v0) am -> Compat h am.
Proof.
destruct 1; econstructor; eauto.
rewrite typeof_update_same in H; eauto.
rewrite typeof_update_same in H0; eauto.
Qed.
Lemma check_compat_update : forall h am v am',
check_compat (update h am v) am' = check_compat h am'.
Proof.
intros.
generalize (check_compat_correct (update h am v) am').
generalize (check_compat_correct h am').
destruct (check_compat (update h am v) am');
destruct (check_compat h am'); auto.
intros.
elim H; eapply Compat_update2; eauto.
intros.
elim H0; eapply Compat_update1; eauto.
Qed.
Lemma get_update_same : forall h am v, Compat h am -> get (update h am v) am = Some v.
Proof.
unfold get; intros.
rewrite check_compat_update.
unfold update.
generalize (check_compat_correct h am); destruct (check_compat h am).
clear H.
destruct 1; simpl.
rewrite DEX_MapFieldSignature.get_update1; auto.
rewrite DEX_LocMap.get_modify1; auto.
caseeq (BinNatMap_Base.get DEX_Object.t (objects h) loc); intros; simpl.
unfold DEX_Object.get; simpl.
rewrite DEX_MapFieldSignature.get_update1; auto.
unfold typeof in H.
replace DEX_LocMap.get with BinNatMap_Base.get in H; auto.
rewrite H0 in H.
destruct (BinNatMap_Base.get (DEX_Array.t) (arrays h) loc); discriminate.
unfold typeof in H0.
replace DEX_LocMap.get with BinNatMap_Base.get in *; auto.
caseeq (BinNatMap_Base.get DEX_Object.t (objects h) loc); intros; simpl.
rewrite H1 in H0.
inversion H0.
rewrite H1 in *.
rewrite DEX_LocMap.get_modify1; auto.
unfold DEX_Array.op_update, DEX_Array.op_get, DEX_Array.get.
caseeq (BinNatMap_Base.get _ (arrays h) loc); intros.
rewrite H2 in H0.
injection H0; intros; subst; clear H0.
destruct (DEX_Array.elements t0).
elim (@pos_Ncase i); [intros n Hn|omega].
rewrite Hn.
rewrite bounded_compare; [idtac|omega].
rewrite Hn.
simpl.
rewrite bounded_compare; [idtac|omega].
destruct n; simpl; auto.
rewrite BinMap_Base.get_modify1; auto.
rewrite H2 in H0; discriminate.
intuition.
Qed.
Lemma get_update_old : forall h am1 am2 v, am1<>am2 -> get (update h am1 v) am2 = get h am2.
Proof.
unfold get; intros.
rewrite check_compat_update.
unfold update; intros.
generalize (check_compat_correct h am1); destruct (check_compat h am1); auto.
destruct 1; destruct am2; simpl; intros; auto.
rewrite DEX_MapFieldSignature.get_update2; auto; congruence.
destruct (DEX_PC_eq_dec loc d); subst.
rewrite DEX_LocMap.get_modify1; auto.
unfold typeof,DEX_LocMap.get in *.
destruct (BinNatMap_Base.get DEX_Object.t (objects h) d); simpl in *.
unfold DEX_Object.get; simpl.
rewrite DEX_MapFieldSignature.get_update2; auto; congruence.
auto.
rewrite DEX_LocMap.get_modify2; auto.
destruct (DEX_PC_eq_dec loc d); subst.
unfold typeof in H1.
caseeq (DEX_LocMap.get _ (objects h) d); intros; simpl; auto.
rewrite H2 in H1; inversion H1.
rewrite H2 in H1.
unfold DEX_LocMap.get in *.
rewrite DEX_LocMap.get_modify1; auto.
clear H2.
caseeq (BinNatMap_Base.get DEX_Array.t (arrays h) d); intros; simpl; auto.
rewrite H2 in H1; try discriminate.
inversion H1; subst; clear H1.
destruct (case_Ncase i).
rewrite H1; auto.
destruct H1 as [n [H1 H3]]; rewrite H1.
destruct (case_compare i (Int.toZ (DEX_Array.length t0))).