11376
|
1 |
(* Title: HOL/NanoJava/State.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: David von Oheimb
|
|
4 |
Copyright 2001 Technische Universitaet Muenchen
|
|
5 |
*)
|
|
6 |
|
|
7 |
header "Program State"
|
|
8 |
|
|
9 |
theory State = TypeRel:
|
|
10 |
|
|
11 |
constdefs
|
|
12 |
|
|
13 |
body :: "cname => mname => stmt"
|
|
14 |
"body C m \<equiv> bdy (the (method C m))"
|
|
15 |
|
|
16 |
text {* locations, i.e.\ abstract references to objects *}
|
|
17 |
typedecl loc
|
|
18 |
arities loc :: "term"
|
|
19 |
|
|
20 |
datatype val
|
|
21 |
= Null (* null reference *)
|
|
22 |
| Addr loc (* address, i.e. location of object *)
|
|
23 |
|
|
24 |
types fields
|
|
25 |
= "(vnam \<leadsto> val)"
|
|
26 |
|
|
27 |
obj = "cname \<times> fields"
|
|
28 |
|
|
29 |
translations
|
|
30 |
|
|
31 |
"fields" \<leftharpoondown> (type)"vnam \<Rightarrow> val option"
|
|
32 |
"obj" \<leftharpoondown> (type)"cname \<times> fields"
|
|
33 |
|
|
34 |
constdefs
|
|
35 |
|
|
36 |
init_vars:: "('a \<leadsto> 'b) => ('a \<leadsto> val)"
|
|
37 |
"init_vars m == option_map (\<lambda>T. Null) o m"
|
|
38 |
|
|
39 |
text {* private *}
|
|
40 |
types heap = "loc \<leadsto> obj"
|
|
41 |
locals = "vname \<leadsto> val"
|
|
42 |
|
|
43 |
text {* private *}
|
|
44 |
record state
|
|
45 |
= heap :: heap
|
|
46 |
locals :: locals
|
|
47 |
|
|
48 |
translations
|
|
49 |
|
|
50 |
"heap" \<leftharpoondown> (type)"loc => obj option"
|
|
51 |
"locals" \<leftharpoondown> (type)"vname => val option"
|
|
52 |
"state" \<leftharpoondown> (type)"(|heap :: heap, locals :: locals|)"
|
|
53 |
|
|
54 |
constdefs
|
|
55 |
|
|
56 |
init_locs :: "cname => mname => state => state"
|
|
57 |
"init_locs C m s \<equiv> s (| locals:=init_vars (map_of (lcl (the (method C m))))|)"
|
|
58 |
|
|
59 |
text {* The first parameter of @{term set_locs} is of type @{typ state}
|
|
60 |
rather than @{typ locals} in order to keep @{typ locals} private.*}
|
|
61 |
constdefs
|
|
62 |
set_locs :: "state => state => state"
|
|
63 |
"set_locs s s' \<equiv> s' (| locals := locals s |)"
|
|
64 |
|
|
65 |
get_local :: "state => vname => val" ("_<_>" [99,0] 99)
|
|
66 |
"get_local s x \<equiv> the (locals s x)"
|
|
67 |
|
|
68 |
(* local function: *)
|
|
69 |
get_obj :: "state => loc => obj"
|
|
70 |
"get_obj s a \<equiv> the (heap s a)"
|
|
71 |
|
|
72 |
obj_class :: "state => loc => cname"
|
|
73 |
"obj_class s a \<equiv> fst (get_obj s a)"
|
|
74 |
|
|
75 |
get_field :: "state => loc => vnam => val"
|
|
76 |
"get_field s a f \<equiv> the (snd (get_obj s a) f)"
|
|
77 |
|
|
78 |
constdefs
|
|
79 |
|
|
80 |
(* local function: *)
|
|
81 |
hupd :: "loc \<Rightarrow> obj \<Rightarrow> state \<Rightarrow> state" ("hupd'(_|->_')" [10,10] 1000)
|
|
82 |
"hupd a obj s \<equiv> s (| heap := ((heap s)(a\<mapsto>obj))|)"
|
|
83 |
|
|
84 |
lupd :: "vname \<Rightarrow> val \<Rightarrow> state \<Rightarrow> state" ("lupd'(_|->_')" [10,10] 1000)
|
|
85 |
"lupd x v s \<equiv> s (| locals := ((locals s)(x\<mapsto>v ))|)"
|
|
86 |
|
|
87 |
syntax (xsymbols)
|
|
88 |
hupd :: "loc \<Rightarrow> obj \<Rightarrow> state \<Rightarrow> state" ("hupd'(_\<mapsto>_')" [10,10] 1000)
|
|
89 |
lupd :: "vname \<Rightarrow> val \<Rightarrow> state \<Rightarrow> state" ("lupd'(_\<mapsto>_')" [10,10] 1000)
|
|
90 |
|
|
91 |
constdefs
|
|
92 |
|
|
93 |
new_obj :: "loc \<Rightarrow> cname \<Rightarrow> state \<Rightarrow> state"
|
|
94 |
"new_obj a C \<equiv> hupd(a\<mapsto>(C,init_vars (field C)))"
|
|
95 |
|
|
96 |
upd_obj :: "loc \<Rightarrow> vnam \<Rightarrow> val \<Rightarrow> state \<Rightarrow> state"
|
|
97 |
"upd_obj a f v s \<equiv> let (C,fs) = the (heap s a) in hupd(a\<mapsto>(C,fs(f\<mapsto>v))) s"
|
|
98 |
|
|
99 |
new_Addr :: "state => val"
|
|
100 |
"new_Addr s == SOME v. (\<exists>a. v = Addr a \<and> (heap s) a = None) | v = Null"
|
|
101 |
|
|
102 |
lemma new_AddrD:
|
|
103 |
"new_Addr s = v \<Longrightarrow> (\<exists>a. v = Addr a \<and> heap s a = None) | v = Null"
|
|
104 |
apply (unfold new_Addr_def)
|
|
105 |
apply (erule subst)
|
|
106 |
apply (rule someI)
|
|
107 |
apply (rule disjI2)
|
|
108 |
apply (rule HOL.refl)
|
|
109 |
done
|
|
110 |
|
|
111 |
end
|