author | haftmann |
Tue, 06 Jul 2010 09:21:13 +0200 | |
changeset 37724 | 6607ccf77946 |
parent 37719 | 271ecd4fb9f9 |
child 37725 | 6d28a2aea936 |
permissions | -rw-r--r-- |
26170 | 1 |
(* Title: HOL/Library/Ref.thy |
2 |
Author: John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen |
|
3 |
*) |
|
4 |
||
5 |
header {* Monadic references *} |
|
6 |
||
7 |
theory Ref |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
8 |
imports Array |
26170 | 9 |
begin |
10 |
||
11 |
text {* |
|
12 |
Imperative reference operations; modeled after their ML counterparts. |
|
13 |
See http://caml.inria.fr/pub/docs/manual-caml-light/node14.15.html |
|
14 |
and http://www.smlnj.org/doc/Conversion/top-level-comparison.html |
|
15 |
*} |
|
16 |
||
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
17 |
subsection {* Primitive layer *} |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
18 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
19 |
definition |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
20 |
ref_present :: "'a\<Colon>heap ref \<Rightarrow> heap \<Rightarrow> bool" where |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
21 |
"ref_present r h \<longleftrightarrow> addr_of_ref r < lim h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
22 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
23 |
definition |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
24 |
get_ref :: "'a\<Colon>heap ref \<Rightarrow> heap \<Rightarrow> 'a" where |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
25 |
"get_ref r h = from_nat (refs h (TYPEREP('a)) (addr_of_ref r))" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
26 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
27 |
definition |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
28 |
set_ref :: "'a\<Colon>heap ref \<Rightarrow> 'a \<Rightarrow> heap \<Rightarrow> heap" where |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
29 |
"set_ref r x = |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
30 |
refs_update (\<lambda>h. h(TYPEREP('a) := ((h (TYPEREP('a))) (addr_of_ref r:=to_nat x))))" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
31 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
32 |
definition ref :: "'a \<Rightarrow> heap \<Rightarrow> 'a\<Colon>heap ref \<times> heap" where |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
33 |
"ref x h = (let |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
34 |
l = lim h; |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
35 |
r = Ref l; |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
36 |
h'' = set_ref r x (h\<lparr>lim := l + 1\<rparr>) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
37 |
in (r, h''))" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
38 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
39 |
definition noteq_refs :: "('a\<Colon>heap) ref \<Rightarrow> ('b\<Colon>heap) ref \<Rightarrow> bool" (infix "=!=" 70) where |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
40 |
"r =!= s \<longleftrightarrow> TYPEREP('a) \<noteq> TYPEREP('b) \<or> addr_of_ref r \<noteq> addr_of_ref s" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
41 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
42 |
lemma noteq_refs_sym: "r =!= s \<Longrightarrow> s =!= r" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
43 |
and unequal_refs [simp]: "r \<noteq> r' \<longleftrightarrow> r =!= r'" -- "same types!" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
44 |
unfolding noteq_refs_def by auto |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
45 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
46 |
lemma noteq_refs_irrefl: "r =!= r \<Longrightarrow> False" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
47 |
unfolding noteq_refs_def by auto |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
48 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
49 |
lemma present_new_ref: "ref_present r h \<Longrightarrow> r =!= fst (ref v h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
50 |
by (simp add: ref_present_def ref_def Let_def noteq_refs_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
51 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
52 |
lemma next_ref_fresh [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
53 |
assumes "(r, h') = ref x h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
54 |
shows "\<not> ref_present r h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
55 |
using assms by (cases h) (auto simp add: ref_def ref_present_def Let_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
56 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
57 |
lemma next_ref_present [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
58 |
assumes "(r, h') = ref x h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
59 |
shows "ref_present r h'" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
60 |
using assms by (cases h) (auto simp add: ref_def set_ref_def ref_present_def Let_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
61 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
62 |
lemma ref_get_set_eq [simp]: "get_ref r (set_ref r x h) = x" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
63 |
by (simp add: get_ref_def set_ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
64 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
65 |
lemma ref_get_set_neq [simp]: "r =!= s \<Longrightarrow> get_ref r (set_ref s x h) = get_ref r h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
66 |
by (simp add: noteq_refs_def get_ref_def set_ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
67 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
68 |
(* FIXME: We need some infrastructure to infer that locally generated |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
69 |
new refs (by new_ref(_no_init), new_array(')) are distinct |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
70 |
from all existing refs. |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
71 |
*) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
72 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
73 |
lemma ref_set_get: "set_ref r (get_ref r h) h = h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
74 |
apply (simp add: set_ref_def get_ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
75 |
oops |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
76 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
77 |
lemma set_ref_same[simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
78 |
"set_ref r x (set_ref r y h) = set_ref r x h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
79 |
by (simp add: set_ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
80 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
81 |
lemma ref_set_set_swap: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
82 |
"r =!= r' \<Longrightarrow> set_ref r x (set_ref r' x' h) = set_ref r' x' (set_ref r x h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
83 |
by (simp add: Let_def expand_fun_eq noteq_refs_def set_ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
84 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
85 |
lemma ref_new_set: "fst (ref v (set_ref r v' h)) = fst (ref v h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
86 |
by (simp add: ref_def set_ref_def Let_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
87 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
88 |
lemma ref_get_new [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
89 |
"get_ref (fst (ref v h)) (snd (ref v' h)) = v'" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
90 |
by (simp add: ref_def Let_def split_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
91 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
92 |
lemma ref_set_new [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
93 |
"set_ref (fst (ref v h)) new_v (snd (ref v h)) = snd (ref new_v h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
94 |
by (simp add: ref_def Let_def split_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
95 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
96 |
lemma ref_get_new_neq: "r =!= (fst (ref v h)) \<Longrightarrow> |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
97 |
get_ref r (snd (ref v h)) = get_ref r h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
98 |
by (simp add: get_ref_def set_ref_def ref_def Let_def noteq_refs_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
99 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
100 |
lemma lim_set_ref [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
101 |
"lim (set_ref r v h) = lim h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
102 |
by (simp add: set_ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
103 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
104 |
lemma ref_present_new_ref [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
105 |
"ref_present r h \<Longrightarrow> ref_present r (snd (ref v h))" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
106 |
by (simp add: ref_present_def ref_def Let_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
107 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
108 |
lemma ref_present_set_ref [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
109 |
"ref_present r (set_ref r' v h) = ref_present r h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
110 |
by (simp add: set_ref_def ref_present_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
111 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
112 |
lemma noteq_refsI: "\<lbrakk> ref_present r h; \<not>ref_present r' h \<rbrakk> \<Longrightarrow> r =!= r'" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
113 |
unfolding noteq_refs_def ref_present_def |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
114 |
by auto |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
115 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
116 |
|
26170 | 117 |
subsection {* Primitives *} |
118 |
||
119 |
definition |
|
120 |
new :: "'a\<Colon>heap \<Rightarrow> 'a ref Heap" where |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
121 |
[code del]: "new v = Heap_Monad.heap (Ref.ref v)" |
26170 | 122 |
|
123 |
definition |
|
124 |
lookup :: "'a\<Colon>heap ref \<Rightarrow> 'a Heap" ("!_" 61) where |
|
125 |
[code del]: "lookup r = Heap_Monad.heap (\<lambda>h. (get_ref r h, h))" |
|
126 |
||
127 |
definition |
|
128 |
update :: "'a ref \<Rightarrow> ('a\<Colon>heap) \<Rightarrow> unit Heap" ("_ := _" 62) where |
|
129 |
[code del]: "update r e = Heap_Monad.heap (\<lambda>h. ((), set_ref r e h))" |
|
130 |
||
26182 | 131 |
|
26170 | 132 |
subsection {* Derivates *} |
133 |
||
134 |
definition |
|
135 |
change :: "('a\<Colon>heap \<Rightarrow> 'a) \<Rightarrow> 'a ref \<Rightarrow> 'a Heap" |
|
136 |
where |
|
137 |
"change f r = (do x \<leftarrow> ! r; |
|
138 |
let y = f x; |
|
139 |
r := y; |
|
140 |
return y |
|
141 |
done)" |
|
142 |
||
36176
3fe7e97ccca8
replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents:
34051
diff
changeset
|
143 |
hide_const (open) new lookup update change |
26170 | 144 |
|
26182 | 145 |
|
26170 | 146 |
subsection {* Properties *} |
147 |
||
148 |
lemma lookup_chain: |
|
149 |
"(!r \<guillemotright> f) = f" |
|
150 |
by (cases f) |
|
151 |
(auto simp add: Let_def bindM_def lookup_def expand_fun_eq) |
|
152 |
||
28562 | 153 |
lemma update_change [code]: |
26170 | 154 |
"r := e = Ref.change (\<lambda>_. e) r \<guillemotright> return ()" |
37709 | 155 |
by (auto simp add: change_def lookup_chain) |
26170 | 156 |
|
26182 | 157 |
|
37719
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
158 |
text {* Non-interaction between imperative array and imperative references *} |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
159 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
160 |
lemma get_array_set_ref [simp]: "get_array a (set_ref r v h) = get_array a h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
161 |
by (simp add: get_array_def set_ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
162 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
163 |
lemma nth_set_ref [simp]: "get_array a (set_ref r v h) ! i = get_array a h ! i" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
164 |
by simp |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
165 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
166 |
lemma get_ref_upd [simp]: "get_ref r (Array.change a i v h) = get_ref r h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
167 |
by (simp add: get_ref_def set_array_def Array.change_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
168 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
169 |
lemma new_ref_upd: "fst (ref v (Array.change a i v' h)) = fst (ref v h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
170 |
by (simp add: set_array_def get_array_def Let_def ref_new_set Array.change_def ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
171 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
172 |
lemma upd_set_ref_swap: "Array.change a i v (set_ref r v' h) = set_ref r v' (Array.change a i v h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
173 |
by (simp add: set_ref_def Array.change_def get_array_def set_array_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
174 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
175 |
lemma length_new_ref[simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
176 |
"length a (snd (ref v h)) = length a h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
177 |
by (simp add: get_array_def set_ref_def length_def ref_def Let_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
178 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
179 |
lemma get_array_new_ref [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
180 |
"get_array a (snd (ref v h)) = get_array a h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
181 |
by (simp add: ref_def set_ref_def get_array_def Let_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
182 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
183 |
lemma ref_present_upd [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
184 |
"ref_present r (Array.change a i v h) = ref_present r h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
185 |
by (simp add: Array.change_def ref_present_def set_array_def get_array_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
186 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
187 |
lemma array_present_set_ref [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
188 |
"array_present a (set_ref r v h) = array_present a h" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
189 |
by (simp add: array_present_def set_ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
190 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
191 |
lemma array_present_new_ref [simp]: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
192 |
"array_present a h \<Longrightarrow> array_present a (snd (ref v h))" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
193 |
by (simp add: array_present_def ref_def Let_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
194 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
195 |
lemma array_ref_set_set_swap: |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
196 |
"set_array r x (set_ref r' x' h) = set_ref r' x' (set_array r x h)" |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
197 |
by (simp add: Let_def expand_fun_eq set_array_def set_ref_def) |
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
198 |
|
271ecd4fb9f9
moved "open" operations from Heap.thy to Array.thy and Ref.thy
haftmann
parents:
37709
diff
changeset
|
199 |
|
26182 | 200 |
subsection {* Code generator setup *} |
201 |
||
202 |
subsubsection {* SML *} |
|
203 |
||
34051
1a82e2e29d67
added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents:
29793
diff
changeset
|
204 |
code_type ref (SML "_/ Unsynchronized.ref") |
26182 | 205 |
code_const Ref (SML "raise/ (Fail/ \"bare Ref\")") |
34051
1a82e2e29d67
added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents:
29793
diff
changeset
|
206 |
code_const Ref.new (SML "(fn/ ()/ =>/ Unsynchronized.ref/ _)") |
26752 | 207 |
code_const Ref.lookup (SML "(fn/ ()/ =>/ !/ _)") |
208 |
code_const Ref.update (SML "(fn/ ()/ =>/ _/ :=/ _)") |
|
26182 | 209 |
|
210 |
code_reserved SML ref |
|
211 |
||
212 |
||
213 |
subsubsection {* OCaml *} |
|
214 |
||
215 |
code_type ref (OCaml "_/ ref") |
|
26752 | 216 |
code_const Ref (OCaml "failwith/ \"bare Ref\")") |
217 |
code_const Ref.new (OCaml "(fn/ ()/ =>/ ref/ _)") |
|
218 |
code_const Ref.lookup (OCaml "(fn/ ()/ =>/ !/ _)") |
|
219 |
code_const Ref.update (OCaml "(fn/ ()/ =>/ _/ :=/ _)") |
|
26182 | 220 |
|
221 |
code_reserved OCaml ref |
|
222 |
||
223 |
||
224 |
subsubsection {* Haskell *} |
|
225 |
||
29793 | 226 |
code_type ref (Haskell "Heap.STRef/ Heap.RealWorld/ _") |
26182 | 227 |
code_const Ref (Haskell "error/ \"bare Ref\"") |
29793 | 228 |
code_const Ref.new (Haskell "Heap.newSTRef") |
229 |
code_const Ref.lookup (Haskell "Heap.readSTRef") |
|
230 |
code_const Ref.update (Haskell "Heap.writeSTRef") |
|
26182 | 231 |
|
26170 | 232 |
end |