haftmann@37787
|
1 |
(* Title: HOL/Imperative_HOL/Ref.thy
|
haftmann@26170
|
2 |
Author: John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
|
haftmann@26170
|
3 |
*)
|
haftmann@26170
|
4 |
|
haftmann@26170
|
5 |
header {* Monadic references *}
|
haftmann@26170
|
6 |
|
haftmann@26170
|
7 |
theory Ref
|
haftmann@37716
|
8 |
imports Array
|
haftmann@26170
|
9 |
begin
|
haftmann@26170
|
10 |
|
haftmann@26170
|
11 |
text {*
|
haftmann@26170
|
12 |
Imperative reference operations; modeled after their ML counterparts.
|
haftmann@26170
|
13 |
See http://caml.inria.fr/pub/docs/manual-caml-light/node14.15.html
|
haftmann@26170
|
14 |
and http://www.smlnj.org/doc/Conversion/top-level-comparison.html
|
haftmann@26170
|
15 |
*}
|
haftmann@26170
|
16 |
|
haftmann@37752
|
17 |
subsection {* Primitives *}
|
haftmann@37716
|
18 |
|
haftmann@37722
|
19 |
definition present :: "heap \<Rightarrow> 'a\<Colon>heap ref \<Rightarrow> bool" where
|
haftmann@37722
|
20 |
"present h r \<longleftrightarrow> addr_of_ref r < lim h"
|
haftmann@37716
|
21 |
|
haftmann@37722
|
22 |
definition get :: "heap \<Rightarrow> 'a\<Colon>heap ref \<Rightarrow> 'a" where
|
haftmann@37722
|
23 |
"get h = from_nat \<circ> refs h TYPEREP('a) \<circ> addr_of_ref"
|
haftmann@37716
|
24 |
|
haftmann@37722
|
25 |
definition set :: "'a\<Colon>heap ref \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where
|
haftmann@37722
|
26 |
"set r x = refs_update
|
haftmann@37722
|
27 |
(\<lambda>h. h(TYPEREP('a) := ((h (TYPEREP('a))) (addr_of_ref r := to_nat x))))"
|
haftmann@37716
|
28 |
|
haftmann@37722
|
29 |
definition alloc :: "'a \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap ref \<times> heap" where
|
haftmann@37722
|
30 |
"alloc x h = (let
|
haftmann@37716
|
31 |
l = lim h;
|
haftmann@37722
|
32 |
r = Ref l
|
haftmann@37722
|
33 |
in (r, set r x (h\<lparr>lim := l + 1\<rparr>)))"
|
haftmann@37716
|
34 |
|
haftmann@37722
|
35 |
definition noteq :: "'a\<Colon>heap ref \<Rightarrow> 'b\<Colon>heap ref \<Rightarrow> bool" (infix "=!=" 70) where
|
haftmann@37716
|
36 |
"r =!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_ref r \<noteq> addr_of_ref s"
|
haftmann@37716
|
37 |
|
haftmann@37752
|
38 |
|
haftmann@37752
|
39 |
subsection {* Monad operations *}
|
haftmann@37752
|
40 |
|
haftmann@37752
|
41 |
definition ref :: "'a\<Colon>heap \<Rightarrow> 'a ref Heap" where
|
haftmann@37752
|
42 |
[code del]: "ref v = Heap_Monad.heap (alloc v)"
|
haftmann@37752
|
43 |
|
haftmann@37752
|
44 |
definition lookup :: "'a\<Colon>heap ref \<Rightarrow> 'a Heap" ("!_" 61) where
|
haftmann@37757
|
45 |
[code del]: "lookup r = Heap_Monad.tap (\<lambda>h. get h r)"
|
haftmann@37752
|
46 |
|
haftmann@37752
|
47 |
definition update :: "'a ref \<Rightarrow> 'a\<Colon>heap \<Rightarrow> unit Heap" ("_ := _" 62) where
|
haftmann@37752
|
48 |
[code del]: "update r v = Heap_Monad.heap (\<lambda>h. ((), set r v h))"
|
haftmann@37752
|
49 |
|
haftmann@37752
|
50 |
definition change :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a ref \<Rightarrow> 'a Heap" where
|
haftmann@37752
|
51 |
"change f r = (do
|
haftmann@37752
|
52 |
x \<leftarrow> ! r;
|
haftmann@37752
|
53 |
let y = f x;
|
haftmann@37752
|
54 |
r := y;
|
haftmann@37752
|
55 |
return y
|
haftmann@37752
|
56 |
done)"
|
haftmann@37752
|
57 |
|
haftmann@37752
|
58 |
|
haftmann@37752
|
59 |
subsection {* Properties *}
|
haftmann@37752
|
60 |
|
haftmann@37757
|
61 |
text {* Primitives *}
|
haftmann@37757
|
62 |
|
haftmann@37722
|
63 |
lemma noteq_sym: "r =!= s \<Longrightarrow> s =!= r"
|
haftmann@37722
|
64 |
and unequal [simp]: "r \<noteq> r' \<longleftrightarrow> r =!= r'" -- "same types!"
|
haftmann@37722
|
65 |
by (auto simp add: noteq_def)
|
haftmann@37716
|
66 |
|
haftmann@37722
|
67 |
lemma noteq_irrefl: "r =!= r \<Longrightarrow> False"
|
haftmann@37722
|
68 |
by (auto simp add: noteq_def)
|
haftmann@37716
|
69 |
|
haftmann@37722
|
70 |
lemma present_alloc_neq: "present h r \<Longrightarrow> r =!= fst (alloc v h)"
|
haftmann@37722
|
71 |
by (simp add: present_def alloc_def noteq_def Let_def)
|
haftmann@37716
|
72 |
|
haftmann@37722
|
73 |
lemma next_fresh [simp]:
|
haftmann@37722
|
74 |
assumes "(r, h') = alloc x h"
|
haftmann@37722
|
75 |
shows "\<not> present h r"
|
haftmann@37722
|
76 |
using assms by (cases h) (auto simp add: alloc_def present_def Let_def)
|
haftmann@37716
|
77 |
|
haftmann@37722
|
78 |
lemma next_present [simp]:
|
haftmann@37722
|
79 |
assumes "(r, h') = alloc x h"
|
haftmann@37722
|
80 |
shows "present h' r"
|
haftmann@37722
|
81 |
using assms by (cases h) (auto simp add: alloc_def set_def present_def Let_def)
|
haftmann@37716
|
82 |
|
haftmann@37722
|
83 |
lemma get_set_eq [simp]:
|
haftmann@37722
|
84 |
"get (set r x h) r = x"
|
haftmann@37722
|
85 |
by (simp add: get_def set_def)
|
haftmann@37716
|
86 |
|
haftmann@37722
|
87 |
lemma get_set_neq [simp]:
|
haftmann@37722
|
88 |
"r =!= s \<Longrightarrow> get (set s x h) r = get h r"
|
haftmann@37722
|
89 |
by (simp add: noteq_def get_def set_def)
|
haftmann@37716
|
90 |
|
haftmann@37722
|
91 |
lemma set_same [simp]:
|
haftmann@37722
|
92 |
"set r x (set r y h) = set r x h"
|
haftmann@37722
|
93 |
by (simp add: set_def)
|
haftmann@37716
|
94 |
|
haftmann@37771
|
95 |
lemma not_present_alloc [simp]:
|
haftmann@37771
|
96 |
"\<not> present h (fst (alloc v h))"
|
haftmann@37771
|
97 |
by (simp add: present_def alloc_def Let_def)
|
haftmann@37771
|
98 |
|
haftmann@37722
|
99 |
lemma set_set_swap:
|
haftmann@37722
|
100 |
"r =!= r' \<Longrightarrow> set r x (set r' x' h) = set r' x' (set r x h)"
|
haftmann@37722
|
101 |
by (simp add: noteq_def set_def expand_fun_eq)
|
haftmann@37716
|
102 |
|
haftmann@37722
|
103 |
lemma alloc_set:
|
haftmann@37722
|
104 |
"fst (alloc x (set r x' h)) = fst (alloc x h)"
|
haftmann@37722
|
105 |
by (simp add: alloc_def set_def Let_def)
|
haftmann@37716
|
106 |
|
haftmann@37722
|
107 |
lemma get_alloc [simp]:
|
haftmann@37722
|
108 |
"get (snd (alloc x h)) (fst (alloc x' h)) = x"
|
haftmann@37722
|
109 |
by (simp add: alloc_def Let_def)
|
haftmann@37716
|
110 |
|
haftmann@37722
|
111 |
lemma set_alloc [simp]:
|
haftmann@37722
|
112 |
"set (fst (alloc v h)) v' (snd (alloc v h)) = snd (alloc v' h)"
|
haftmann@37722
|
113 |
by (simp add: alloc_def Let_def)
|
haftmann@37716
|
114 |
|
haftmann@37722
|
115 |
lemma get_alloc_neq: "r =!= fst (alloc v h) \<Longrightarrow>
|
haftmann@37722
|
116 |
get (snd (alloc v h)) r = get h r"
|
haftmann@37722
|
117 |
by (simp add: get_def set_def alloc_def Let_def noteq_def)
|
haftmann@37716
|
118 |
|
haftmann@37722
|
119 |
lemma lim_set [simp]:
|
haftmann@37722
|
120 |
"lim (set r v h) = lim h"
|
haftmann@37722
|
121 |
by (simp add: set_def)
|
haftmann@37716
|
122 |
|
haftmann@37722
|
123 |
lemma present_alloc [simp]:
|
haftmann@37722
|
124 |
"present h r \<Longrightarrow> present (snd (alloc v h)) r"
|
haftmann@37722
|
125 |
by (simp add: present_def alloc_def Let_def)
|
haftmann@37716
|
126 |
|
haftmann@37722
|
127 |
lemma present_set [simp]:
|
haftmann@37722
|
128 |
"present (set r v h) = present h"
|
haftmann@37722
|
129 |
by (simp add: present_def expand_fun_eq)
|
haftmann@37716
|
130 |
|
haftmann@37722
|
131 |
lemma noteq_I:
|
haftmann@37722
|
132 |
"present h r \<Longrightarrow> \<not> present h r' \<Longrightarrow> r =!= r'"
|
haftmann@37722
|
133 |
by (auto simp add: noteq_def present_def)
|
haftmann@37716
|
134 |
|
haftmann@37757
|
135 |
|
haftmann@37757
|
136 |
text {* Monad operations *}
|
haftmann@37757
|
137 |
|
haftmann@37787
|
138 |
lemma execute_ref [execute_simps]:
|
haftmann@37757
|
139 |
"execute (ref v) h = Some (alloc v h)"
|
haftmann@37787
|
140 |
by (simp add: ref_def execute_simps)
|
haftmann@37716
|
141 |
|
haftmann@37787
|
142 |
lemma success_refI [success_intros]:
|
haftmann@37757
|
143 |
"success (ref v) h"
|
haftmann@37787
|
144 |
by (auto intro: success_intros simp add: ref_def)
|
haftmann@37757
|
145 |
|
haftmann@37771
|
146 |
lemma crel_refI [crel_intros]:
|
haftmann@37771
|
147 |
assumes "(r, h') = alloc v h"
|
haftmann@37771
|
148 |
shows "crel (ref v) h h' r"
|
haftmann@37787
|
149 |
by (rule crelI) (insert assms, simp add: execute_simps)
|
haftmann@37771
|
150 |
|
haftmann@37771
|
151 |
lemma crel_refE [crel_elims]:
|
haftmann@37771
|
152 |
assumes "crel (ref v) h h' r"
|
haftmann@37796
|
153 |
obtains "get h' r = v" and "present h' r" and "\<not> present h r"
|
haftmann@37787
|
154 |
using assms by (rule crelE) (simp add: execute_simps)
|
haftmann@37771
|
155 |
|
haftmann@37787
|
156 |
lemma execute_lookup [execute_simps]:
|
haftmann@37752
|
157 |
"Heap_Monad.execute (lookup r) h = Some (get h r, h)"
|
haftmann@37787
|
158 |
by (simp add: lookup_def execute_simps)
|
haftmann@26170
|
159 |
|
haftmann@37787
|
160 |
lemma success_lookupI [success_intros]:
|
haftmann@37757
|
161 |
"success (lookup r) h"
|
haftmann@37787
|
162 |
by (auto intro: success_intros simp add: lookup_def)
|
haftmann@37757
|
163 |
|
haftmann@37771
|
164 |
lemma crel_lookupI [crel_intros]:
|
haftmann@37796
|
165 |
assumes "h' = h" "x = get h r"
|
haftmann@37771
|
166 |
shows "crel (!r) h h' x"
|
haftmann@37787
|
167 |
by (rule crelI) (insert assms, simp add: execute_simps)
|
haftmann@37771
|
168 |
|
haftmann@37771
|
169 |
lemma crel_lookupE [crel_elims]:
|
haftmann@37771
|
170 |
assumes "crel (!r) h h' x"
|
haftmann@37796
|
171 |
obtains "h' = h" "x = get h r"
|
haftmann@37787
|
172 |
using assms by (rule crelE) (simp add: execute_simps)
|
haftmann@37771
|
173 |
|
haftmann@37787
|
174 |
lemma execute_update [execute_simps]:
|
haftmann@37752
|
175 |
"Heap_Monad.execute (update r v) h = Some ((), set r v h)"
|
haftmann@37787
|
176 |
by (simp add: update_def execute_simps)
|
haftmann@26170
|
177 |
|
haftmann@37787
|
178 |
lemma success_updateI [success_intros]:
|
haftmann@37757
|
179 |
"success (update r v) h"
|
haftmann@37787
|
180 |
by (auto intro: success_intros simp add: update_def)
|
haftmann@37757
|
181 |
|
haftmann@37771
|
182 |
lemma crel_updateI [crel_intros]:
|
haftmann@37796
|
183 |
assumes "h' = set r v h"
|
haftmann@37771
|
184 |
shows "crel (r := v) h h' x"
|
haftmann@37787
|
185 |
by (rule crelI) (insert assms, simp add: execute_simps)
|
haftmann@37771
|
186 |
|
haftmann@37771
|
187 |
lemma crel_updateE [crel_elims]:
|
haftmann@37771
|
188 |
assumes "crel (r' := v) h h' r"
|
haftmann@37796
|
189 |
obtains "h' = set r' v h"
|
haftmann@37787
|
190 |
using assms by (rule crelE) (simp add: execute_simps)
|
haftmann@37771
|
191 |
|
haftmann@37787
|
192 |
lemma execute_change [execute_simps]:
|
haftmann@37752
|
193 |
"Heap_Monad.execute (change f r) h = Some (f (get h r), set r (f (get h r)) h)"
|
haftmann@37787
|
194 |
by (simp add: change_def bind_def Let_def execute_simps)
|
haftmann@37757
|
195 |
|
haftmann@37787
|
196 |
lemma success_changeI [success_intros]:
|
haftmann@37757
|
197 |
"success (change f r) h"
|
haftmann@37771
|
198 |
by (auto intro!: success_intros crel_intros simp add: change_def)
|
haftmann@37771
|
199 |
|
haftmann@37771
|
200 |
lemma crel_changeI [crel_intros]:
|
haftmann@37796
|
201 |
assumes "h' = set r (f (get h r)) h" "x = f (get h r)"
|
haftmann@37796
|
202 |
shows "crel (change f r) h h' x"
|
haftmann@37787
|
203 |
by (rule crelI) (insert assms, simp add: execute_simps)
|
haftmann@37771
|
204 |
|
haftmann@37771
|
205 |
lemma crel_changeE [crel_elims]:
|
haftmann@37796
|
206 |
assumes "crel (change f r') h h' r"
|
haftmann@37796
|
207 |
obtains "h' = set r' (f (get h r')) h" "r = f (get h r')"
|
haftmann@37787
|
208 |
using assms by (rule crelE) (simp add: execute_simps)
|
haftmann@26170
|
209 |
|
haftmann@26170
|
210 |
lemma lookup_chain:
|
haftmann@26170
|
211 |
"(!r \<guillemotright> f) = f"
|
haftmann@37771
|
212 |
by (rule Heap_eqI) (auto simp add: lookup_def execute_simps intro: execute_bind)
|
haftmann@26170
|
213 |
|
haftmann@28562
|
214 |
lemma update_change [code]:
|
haftmann@37722
|
215 |
"r := e = change (\<lambda>_. e) r \<guillemotright> return ()"
|
haftmann@37722
|
216 |
by (rule Heap_eqI) (simp add: change_def lookup_chain)
|
haftmann@26170
|
217 |
|
haftmann@26182
|
218 |
|
haftmann@37716
|
219 |
text {* Non-interaction between imperative array and imperative references *}
|
haftmann@37716
|
220 |
|
haftmann@37722
|
221 |
lemma get_array_set [simp]:
|
haftmann@37722
|
222 |
"get_array a (set r v h) = get_array a h"
|
haftmann@37722
|
223 |
by (simp add: get_array_def set_def)
|
haftmann@37716
|
224 |
|
haftmann@37722
|
225 |
lemma nth_set [simp]:
|
haftmann@37722
|
226 |
"get_array a (set r v h) ! i = get_array a h ! i"
|
haftmann@37716
|
227 |
by simp
|
haftmann@37716
|
228 |
|
haftmann@37796
|
229 |
lemma get_update [simp]:
|
haftmann@37796
|
230 |
"get (Array.update a i v h) r = get h r"
|
haftmann@37796
|
231 |
by (simp add: get_def Array.update_def set_array_def)
|
haftmann@37716
|
232 |
|
haftmann@37796
|
233 |
lemma alloc_update:
|
haftmann@37796
|
234 |
"fst (alloc v (Array.update a i v' h)) = fst (alloc v h)"
|
haftmann@37796
|
235 |
by (simp add: Array.update_def get_array_def set_array_def alloc_def Let_def)
|
haftmann@37716
|
236 |
|
haftmann@37796
|
237 |
lemma update_set_swap:
|
haftmann@37796
|
238 |
"Array.update a i v (set r v' h) = set r v' (Array.update a i v h)"
|
haftmann@37796
|
239 |
by (simp add: Array.update_def get_array_def set_array_def set_def)
|
haftmann@37716
|
240 |
|
haftmann@37722
|
241 |
lemma length_alloc [simp]:
|
haftmann@37722
|
242 |
"Array.length a (snd (alloc v h)) = Array.length a h"
|
haftmann@37722
|
243 |
by (simp add: Array.length_def get_array_def alloc_def set_def Let_def)
|
haftmann@37716
|
244 |
|
haftmann@37722
|
245 |
lemma get_array_alloc [simp]:
|
haftmann@37722
|
246 |
"get_array a (snd (alloc v h)) = get_array a h"
|
haftmann@37722
|
247 |
by (simp add: get_array_def alloc_def set_def Let_def)
|
haftmann@37716
|
248 |
|
haftmann@37796
|
249 |
lemma present_update [simp]:
|
haftmann@37796
|
250 |
"present (Array.update a i v h) = present h"
|
haftmann@37796
|
251 |
by (simp add: Array.update_def set_array_def expand_fun_eq present_def)
|
haftmann@37716
|
252 |
|
haftmann@37722
|
253 |
lemma array_present_set [simp]:
|
haftmann@37722
|
254 |
"array_present a (set r v h) = array_present a h"
|
haftmann@37722
|
255 |
by (simp add: array_present_def set_def)
|
haftmann@37716
|
256 |
|
haftmann@37722
|
257 |
lemma array_present_alloc [simp]:
|
haftmann@37722
|
258 |
"array_present a h \<Longrightarrow> array_present a (snd (alloc v h))"
|
haftmann@37722
|
259 |
by (simp add: array_present_def alloc_def Let_def)
|
haftmann@37716
|
260 |
|
haftmann@37722
|
261 |
lemma set_array_set_swap:
|
haftmann@37722
|
262 |
"set_array a xs (set r x' h) = set r x' (set_array a xs h)"
|
haftmann@37722
|
263 |
by (simp add: set_array_def set_def)
|
haftmann@37722
|
264 |
|
haftmann@37796
|
265 |
hide_const (open) present get set alloc noteq lookup update change
|
haftmann@37716
|
266 |
|
haftmann@37716
|
267 |
|
haftmann@26182
|
268 |
subsection {* Code generator setup *}
|
haftmann@26182
|
269 |
|
haftmann@37752
|
270 |
text {* SML *}
|
haftmann@26182
|
271 |
|
bulwahn@34047
|
272 |
code_type ref (SML "_/ Unsynchronized.ref")
|
haftmann@26182
|
273 |
code_const Ref (SML "raise/ (Fail/ \"bare Ref\")")
|
haftmann@37722
|
274 |
code_const ref (SML "(fn/ ()/ =>/ Unsynchronized.ref/ _)")
|
haftmann@26752
|
275 |
code_const Ref.lookup (SML "(fn/ ()/ =>/ !/ _)")
|
haftmann@26752
|
276 |
code_const Ref.update (SML "(fn/ ()/ =>/ _/ :=/ _)")
|
haftmann@26182
|
277 |
|
haftmann@26182
|
278 |
code_reserved SML ref
|
haftmann@26182
|
279 |
|
haftmann@26182
|
280 |
|
haftmann@37752
|
281 |
text {* OCaml *}
|
haftmann@26182
|
282 |
|
haftmann@26182
|
283 |
code_type ref (OCaml "_/ ref")
|
haftmann@26752
|
284 |
code_const Ref (OCaml "failwith/ \"bare Ref\")")
|
haftmann@37722
|
285 |
code_const ref (OCaml "(fn/ ()/ =>/ ref/ _)")
|
haftmann@26752
|
286 |
code_const Ref.lookup (OCaml "(fn/ ()/ =>/ !/ _)")
|
haftmann@26752
|
287 |
code_const Ref.update (OCaml "(fn/ ()/ =>/ _/ :=/ _)")
|
haftmann@26182
|
288 |
|
haftmann@26182
|
289 |
code_reserved OCaml ref
|
haftmann@26182
|
290 |
|
haftmann@26182
|
291 |
|
haftmann@37752
|
292 |
text {* Haskell *}
|
haftmann@26182
|
293 |
|
haftmann@29730
|
294 |
code_type ref (Haskell "Heap.STRef/ Heap.RealWorld/ _")
|
haftmann@26182
|
295 |
code_const Ref (Haskell "error/ \"bare Ref\"")
|
haftmann@37722
|
296 |
code_const ref (Haskell "Heap.newSTRef")
|
haftmann@29730
|
297 |
code_const Ref.lookup (Haskell "Heap.readSTRef")
|
haftmann@29730
|
298 |
code_const Ref.update (Haskell "Heap.writeSTRef")
|
haftmann@26182
|
299 |
|
haftmann@37757
|
300 |
end
|