author | oheimb |
Thu, 30 Aug 2001 15:47:30 +0200 | |
changeset 11507 | 4b32a46ffd29 |
parent 11499 | 7a7bb59a05db |
child 11558 | 6539627881e8 |
permissions | -rw-r--r-- |
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 |
||
11507 | 13 |
body :: "cname \<times> mname => stmt" |
11497
0e66e0114d9a
corrected initialization of locals, streamlined Impl
oheimb
parents:
11376
diff
changeset
|
14 |
"body \<equiv> \<lambda>(C,m). bdy (the (method C m))" |
11376 | 15 |
|
16 |
text {* locations, i.e.\ abstract references to objects *} |
|
17 |
typedecl loc |
|
18 |
||
19 |
datatype val |
|
20 |
= Null (* null reference *) |
|
21 |
| Addr loc (* address, i.e. location of object *) |
|
22 |
||
23 |
types fields |
|
24 |
= "(vnam \<leadsto> val)" |
|
25 |
||
26 |
obj = "cname \<times> fields" |
|
27 |
||
28 |
translations |
|
29 |
||
30 |
"fields" \<leftharpoondown> (type)"vnam \<Rightarrow> val option" |
|
31 |
"obj" \<leftharpoondown> (type)"cname \<times> fields" |
|
32 |
||
33 |
constdefs |
|
34 |
||
35 |
init_vars:: "('a \<leadsto> 'b) => ('a \<leadsto> val)" |
|
36 |
"init_vars m == option_map (\<lambda>T. Null) o m" |
|
37 |
||
38 |
text {* private *} |
|
39 |
types heap = "loc \<leadsto> obj" |
|
40 |
locals = "vname \<leadsto> val" |
|
41 |
||
42 |
text {* private *} |
|
43 |
record state |
|
44 |
= heap :: heap |
|
45 |
locals :: locals |
|
46 |
||
47 |
translations |
|
48 |
||
49 |
"heap" \<leftharpoondown> (type)"loc => obj option" |
|
50 |
"locals" \<leftharpoondown> (type)"vname => val option" |
|
51 |
"state" \<leftharpoondown> (type)"(|heap :: heap, locals :: locals|)" |
|
52 |
||
53 |
constdefs |
|
54 |
||
11497
0e66e0114d9a
corrected initialization of locals, streamlined Impl
oheimb
parents:
11376
diff
changeset
|
55 |
reset_locs :: "state => state" |
0e66e0114d9a
corrected initialization of locals, streamlined Impl
oheimb
parents:
11376
diff
changeset
|
56 |
"reset_locs s \<equiv> s (| locals := empty |)" |
0e66e0114d9a
corrected initialization of locals, streamlined Impl
oheimb
parents:
11376
diff
changeset
|
57 |
|
11376 | 58 |
init_locs :: "cname => mname => state => state" |
11497
0e66e0114d9a
corrected initialization of locals, streamlined Impl
oheimb
parents:
11376
diff
changeset
|
59 |
"init_locs C m s \<equiv> s (| locals:=init_vars (map_of (lcl (the (method C m)))) |)" |
11376 | 60 |
|
61 |
text {* The first parameter of @{term set_locs} is of type @{typ state} |
|
62 |
rather than @{typ locals} in order to keep @{typ locals} private.*} |
|
63 |
constdefs |
|
64 |
set_locs :: "state => state => state" |
|
65 |
"set_locs s s' \<equiv> s' (| locals := locals s |)" |
|
66 |
||
67 |
get_local :: "state => vname => val" ("_<_>" [99,0] 99) |
|
68 |
"get_local s x \<equiv> the (locals s x)" |
|
69 |
||
70 |
(* local function: *) |
|
71 |
get_obj :: "state => loc => obj" |
|
72 |
"get_obj s a \<equiv> the (heap s a)" |
|
73 |
||
74 |
obj_class :: "state => loc => cname" |
|
75 |
"obj_class s a \<equiv> fst (get_obj s a)" |
|
76 |
||
77 |
get_field :: "state => loc => vnam => val" |
|
78 |
"get_field s a f \<equiv> the (snd (get_obj s a) f)" |
|
79 |
||
80 |
constdefs |
|
81 |
||
82 |
(* local function: *) |
|
83 |
hupd :: "loc \<Rightarrow> obj \<Rightarrow> state \<Rightarrow> state" ("hupd'(_|->_')" [10,10] 1000) |
|
84 |
"hupd a obj s \<equiv> s (| heap := ((heap s)(a\<mapsto>obj))|)" |
|
85 |
||
86 |
lupd :: "vname \<Rightarrow> val \<Rightarrow> state \<Rightarrow> state" ("lupd'(_|->_')" [10,10] 1000) |
|
87 |
"lupd x v s \<equiv> s (| locals := ((locals s)(x\<mapsto>v ))|)" |
|
88 |
||
89 |
syntax (xsymbols) |
|
90 |
hupd :: "loc \<Rightarrow> obj \<Rightarrow> state \<Rightarrow> state" ("hupd'(_\<mapsto>_')" [10,10] 1000) |
|
91 |
lupd :: "vname \<Rightarrow> val \<Rightarrow> state \<Rightarrow> state" ("lupd'(_\<mapsto>_')" [10,10] 1000) |
|
92 |
||
93 |
constdefs |
|
94 |
||
95 |
new_obj :: "loc \<Rightarrow> cname \<Rightarrow> state \<Rightarrow> state" |
|
96 |
"new_obj a C \<equiv> hupd(a\<mapsto>(C,init_vars (field C)))" |
|
97 |
||
98 |
upd_obj :: "loc \<Rightarrow> vnam \<Rightarrow> val \<Rightarrow> state \<Rightarrow> state" |
|
99 |
"upd_obj a f v s \<equiv> let (C,fs) = the (heap s a) in hupd(a\<mapsto>(C,fs(f\<mapsto>v))) s" |
|
100 |
||
101 |
new_Addr :: "state => val" |
|
102 |
"new_Addr s == SOME v. (\<exists>a. v = Addr a \<and> (heap s) a = None) | v = Null" |
|
103 |
||
104 |
lemma new_AddrD: |
|
105 |
"new_Addr s = v \<Longrightarrow> (\<exists>a. v = Addr a \<and> heap s a = None) | v = Null" |
|
106 |
apply (unfold new_Addr_def) |
|
107 |
apply (erule subst) |
|
108 |
apply (rule someI) |
|
109 |
apply (rule disjI2) |
|
110 |
apply (rule HOL.refl) |
|
111 |
done |
|
112 |
||
113 |
end |