author | wenzelm |
Fri, 22 Apr 2011 13:58:13 +0200 | |
changeset 42455 | 6702c984bf5a |
parent 41413 | 64cd30d6b0b8 |
child 42949 | 618adb3584e5 |
permissions | -rw-r--r-- |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
1 |
(* Title: HOL/Imperative_HOL/Heap_Monad.thy |
26170 | 2 |
Author: John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen |
3 |
*) |
|
4 |
||
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
5 |
header {* A monad with a polymorphic heap and primitive reasoning infrastructure *} |
26170 | 6 |
|
7 |
theory Heap_Monad |
|
41413
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
40671
diff
changeset
|
8 |
imports |
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
40671
diff
changeset
|
9 |
Heap |
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
40671
diff
changeset
|
10 |
"~~/src/HOL/Library/Monad_Syntax" |
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
wenzelm
parents:
40671
diff
changeset
|
11 |
"~~/src/HOL/Library/Code_Natural" |
26170 | 12 |
begin |
13 |
||
14 |
subsection {* The monad *} |
|
15 |
||
37758 | 16 |
subsubsection {* Monad construction *} |
26170 | 17 |
|
18 |
text {* Monadic heap actions either produce values |
|
19 |
and transform the heap, or fail *} |
|
37709 | 20 |
datatype 'a Heap = Heap "heap \<Rightarrow> ('a \<times> heap) option" |
26170 | 21 |
|
40266
d72f1f734e5a
remove term_of equations for Heap type explicitly
haftmann
parents:
40173
diff
changeset
|
22 |
lemma [code, code del]: |
d72f1f734e5a
remove term_of equations for Heap type explicitly
haftmann
parents:
40173
diff
changeset
|
23 |
"(Code_Evaluation.term_of :: 'a::typerep Heap \<Rightarrow> Code_Evaluation.term) = Code_Evaluation.term_of" |
d72f1f734e5a
remove term_of equations for Heap type explicitly
haftmann
parents:
40173
diff
changeset
|
24 |
.. |
d72f1f734e5a
remove term_of equations for Heap type explicitly
haftmann
parents:
40173
diff
changeset
|
25 |
|
37709 | 26 |
primrec execute :: "'a Heap \<Rightarrow> heap \<Rightarrow> ('a \<times> heap) option" where |
27 |
[code del]: "execute (Heap f) = f" |
|
26170 | 28 |
|
37758 | 29 |
lemma Heap_cases [case_names succeed fail]: |
30 |
fixes f and h |
|
31 |
assumes succeed: "\<And>x h'. execute f h = Some (x, h') \<Longrightarrow> P" |
|
32 |
assumes fail: "execute f h = None \<Longrightarrow> P" |
|
33 |
shows P |
|
34 |
using assms by (cases "execute f h") auto |
|
35 |
||
26170 | 36 |
lemma Heap_execute [simp]: |
37 |
"Heap (execute f) = f" by (cases f) simp_all |
|
38 |
||
39 |
lemma Heap_eqI: |
|
40 |
"(\<And>h. execute f h = execute g h) \<Longrightarrow> f = g" |
|
39302
d7728f65b353
renamed lemmas: ext_iff -> fun_eq_iff, set_ext_iff -> set_eq_iff, set_ext -> set_eqI
nipkow
parents:
39250
diff
changeset
|
41 |
by (cases f, cases g) (auto simp: fun_eq_iff) |
26170 | 42 |
|
37758 | 43 |
ML {* structure Execute_Simps = Named_Thms( |
44 |
val name = "execute_simps" |
|
45 |
val description = "simplification rules for execute" |
|
46 |
) *} |
|
47 |
||
48 |
setup Execute_Simps.setup |
|
49 |
||
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
50 |
lemma execute_Let [execute_simps]: |
37758 | 51 |
"execute (let x = t in f x) = (let x = t in execute (f x))" |
52 |
by (simp add: Let_def) |
|
53 |
||
54 |
||
55 |
subsubsection {* Specialised lifters *} |
|
56 |
||
57 |
definition tap :: "(heap \<Rightarrow> 'a) \<Rightarrow> 'a Heap" where |
|
58 |
[code del]: "tap f = Heap (\<lambda>h. Some (f h, h))" |
|
59 |
||
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
60 |
lemma execute_tap [execute_simps]: |
37758 | 61 |
"execute (tap f) h = Some (f h, h)" |
62 |
by (simp add: tap_def) |
|
26170 | 63 |
|
37709 | 64 |
definition heap :: "(heap \<Rightarrow> 'a \<times> heap) \<Rightarrow> 'a Heap" where |
65 |
[code del]: "heap f = Heap (Some \<circ> f)" |
|
26170 | 66 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
67 |
lemma execute_heap [execute_simps]: |
37709 | 68 |
"execute (heap f) = Some \<circ> f" |
26170 | 69 |
by (simp add: heap_def) |
70 |
||
37754 | 71 |
definition guard :: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> 'a \<times> heap) \<Rightarrow> 'a Heap" where |
72 |
[code del]: "guard P f = Heap (\<lambda>h. if P h then Some (f h) else None)" |
|
73 |
||
37758 | 74 |
lemma execute_guard [execute_simps]: |
37754 | 75 |
"\<not> P h \<Longrightarrow> execute (guard P f) h = None" |
76 |
"P h \<Longrightarrow> execute (guard P f) h = Some (f h)" |
|
77 |
by (simp_all add: guard_def) |
|
78 |
||
37758 | 79 |
|
80 |
subsubsection {* Predicate classifying successful computations *} |
|
81 |
||
82 |
definition success :: "'a Heap \<Rightarrow> heap \<Rightarrow> bool" where |
|
83 |
"success f h \<longleftrightarrow> execute f h \<noteq> None" |
|
84 |
||
85 |
lemma successI: |
|
86 |
"execute f h \<noteq> None \<Longrightarrow> success f h" |
|
87 |
by (simp add: success_def) |
|
88 |
||
89 |
lemma successE: |
|
90 |
assumes "success f h" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
91 |
obtains r h' where "r = fst (the (execute c h))" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
92 |
and "h' = snd (the (execute c h))" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
93 |
and "execute f h \<noteq> None" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
94 |
using assms by (simp add: success_def) |
37758 | 95 |
|
96 |
ML {* structure Success_Intros = Named_Thms( |
|
97 |
val name = "success_intros" |
|
98 |
val description = "introduction rules for success" |
|
99 |
) *} |
|
100 |
||
101 |
setup Success_Intros.setup |
|
102 |
||
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
103 |
lemma success_tapI [success_intros]: |
37758 | 104 |
"success (tap f) h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
105 |
by (rule successI) (simp add: execute_simps) |
37758 | 106 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
107 |
lemma success_heapI [success_intros]: |
37758 | 108 |
"success (heap f) h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
109 |
by (rule successI) (simp add: execute_simps) |
37758 | 110 |
|
111 |
lemma success_guardI [success_intros]: |
|
112 |
"P h \<Longrightarrow> success (guard P f) h" |
|
113 |
by (rule successI) (simp add: execute_guard) |
|
114 |
||
115 |
lemma success_LetI [success_intros]: |
|
116 |
"x = t \<Longrightarrow> success (f x) h \<Longrightarrow> success (let x = t in f x) h" |
|
117 |
by (simp add: Let_def) |
|
118 |
||
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
119 |
lemma success_ifI: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
120 |
"(c \<Longrightarrow> success t h) \<Longrightarrow> (\<not> c \<Longrightarrow> success e h) \<Longrightarrow> |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
121 |
success (if c then t else e) h" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
122 |
by (simp add: success_def) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
123 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
124 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
125 |
subsubsection {* Predicate for a simple relational calculus *} |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
126 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
127 |
text {* |
40671 | 128 |
The @{text effect} predicate states that when a computation @{text c} |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
129 |
runs with the heap @{text h} will result in return value @{text r} |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
130 |
and a heap @{text "h'"}, i.e.~no exception occurs. |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
131 |
*} |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
132 |
|
40671 | 133 |
definition effect :: "'a Heap \<Rightarrow> heap \<Rightarrow> heap \<Rightarrow> 'a \<Rightarrow> bool" where |
134 |
effect_def: "effect c h h' r \<longleftrightarrow> execute c h = Some (r, h')" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
135 |
|
40671 | 136 |
lemma effectI: |
137 |
"execute c h = Some (r, h') \<Longrightarrow> effect c h h' r" |
|
138 |
by (simp add: effect_def) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
139 |
|
40671 | 140 |
lemma effectE: |
141 |
assumes "effect c h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
142 |
obtains "r = fst (the (execute c h))" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
143 |
and "h' = snd (the (execute c h))" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
144 |
and "success c h" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
145 |
proof (rule that) |
40671 | 146 |
from assms have *: "execute c h = Some (r, h')" by (simp add: effect_def) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
147 |
then show "success c h" by (simp add: success_def) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
148 |
from * have "fst (the (execute c h)) = r" and "snd (the (execute c h)) = h'" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
149 |
by simp_all |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
150 |
then show "r = fst (the (execute c h))" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
151 |
and "h' = snd (the (execute c h))" by simp_all |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
152 |
qed |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
153 |
|
40671 | 154 |
lemma effect_success: |
155 |
"effect c h h' r \<Longrightarrow> success c h" |
|
156 |
by (simp add: effect_def success_def) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
157 |
|
40671 | 158 |
lemma success_effectE: |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
159 |
assumes "success c h" |
40671 | 160 |
obtains r h' where "effect c h h' r" |
161 |
using assms by (auto simp add: effect_def success_def) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
162 |
|
40671 | 163 |
lemma effect_deterministic: |
164 |
assumes "effect f h h' a" |
|
165 |
and "effect f h h'' b" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
166 |
shows "a = b" and "h' = h''" |
40671 | 167 |
using assms unfolding effect_def by auto |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
168 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
169 |
ML {* structure Crel_Intros = Named_Thms( |
40671 | 170 |
val name = "effect_intros" |
171 |
val description = "introduction rules for effect" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
172 |
) *} |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
173 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
174 |
ML {* structure Crel_Elims = Named_Thms( |
40671 | 175 |
val name = "effect_elims" |
176 |
val description = "elimination rules for effect" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
177 |
) *} |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
178 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
179 |
setup "Crel_Intros.setup #> Crel_Elims.setup" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
180 |
|
40671 | 181 |
lemma effect_LetI [effect_intros]: |
182 |
assumes "x = t" "effect (f x) h h' r" |
|
183 |
shows "effect (let x = t in f x) h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
184 |
using assms by simp |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
185 |
|
40671 | 186 |
lemma effect_LetE [effect_elims]: |
187 |
assumes "effect (let x = t in f x) h h' r" |
|
188 |
obtains "effect (f t) h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
189 |
using assms by simp |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
190 |
|
40671 | 191 |
lemma effect_ifI: |
192 |
assumes "c \<Longrightarrow> effect t h h' r" |
|
193 |
and "\<not> c \<Longrightarrow> effect e h h' r" |
|
194 |
shows "effect (if c then t else e) h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
195 |
by (cases c) (simp_all add: assms) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
196 |
|
40671 | 197 |
lemma effect_ifE: |
198 |
assumes "effect (if c then t else e) h h' r" |
|
199 |
obtains "c" "effect t h h' r" |
|
200 |
| "\<not> c" "effect e h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
201 |
using assms by (cases c) simp_all |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
202 |
|
40671 | 203 |
lemma effect_tapI [effect_intros]: |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
204 |
assumes "h' = h" "r = f h" |
40671 | 205 |
shows "effect (tap f) h h' r" |
206 |
by (rule effectI) (simp add: assms execute_simps) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
207 |
|
40671 | 208 |
lemma effect_tapE [effect_elims]: |
209 |
assumes "effect (tap f) h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
210 |
obtains "h' = h" and "r = f h" |
40671 | 211 |
using assms by (rule effectE) (auto simp add: execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
212 |
|
40671 | 213 |
lemma effect_heapI [effect_intros]: |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
214 |
assumes "h' = snd (f h)" "r = fst (f h)" |
40671 | 215 |
shows "effect (heap f) h h' r" |
216 |
by (rule effectI) (simp add: assms execute_simps) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
217 |
|
40671 | 218 |
lemma effect_heapE [effect_elims]: |
219 |
assumes "effect (heap f) h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
220 |
obtains "h' = snd (f h)" and "r = fst (f h)" |
40671 | 221 |
using assms by (rule effectE) (simp add: execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
222 |
|
40671 | 223 |
lemma effect_guardI [effect_intros]: |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
224 |
assumes "P h" "h' = snd (f h)" "r = fst (f h)" |
40671 | 225 |
shows "effect (guard P f) h h' r" |
226 |
by (rule effectI) (simp add: assms execute_simps) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
227 |
|
40671 | 228 |
lemma effect_guardE [effect_elims]: |
229 |
assumes "effect (guard P f) h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
230 |
obtains "h' = snd (f h)" "r = fst (f h)" "P h" |
40671 | 231 |
using assms by (rule effectE) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
232 |
(auto simp add: execute_simps elim!: successE, cases "P h", auto simp add: execute_simps) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
233 |
|
37758 | 234 |
|
235 |
subsubsection {* Monad combinators *} |
|
26170 | 236 |
|
37709 | 237 |
definition return :: "'a \<Rightarrow> 'a Heap" where |
26170 | 238 |
[code del]: "return x = heap (Pair x)" |
239 |
||
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
240 |
lemma execute_return [execute_simps]: |
37709 | 241 |
"execute (return x) = Some \<circ> Pair x" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
242 |
by (simp add: return_def execute_simps) |
26170 | 243 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
244 |
lemma success_returnI [success_intros]: |
37758 | 245 |
"success (return x) h" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
246 |
by (rule successI) (simp add: execute_simps) |
37758 | 247 |
|
40671 | 248 |
lemma effect_returnI [effect_intros]: |
249 |
"h = h' \<Longrightarrow> effect (return x) h h' x" |
|
250 |
by (rule effectI) (simp add: execute_simps) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
251 |
|
40671 | 252 |
lemma effect_returnE [effect_elims]: |
253 |
assumes "effect (return x) h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
254 |
obtains "r = x" "h' = h" |
40671 | 255 |
using assms by (rule effectE) (simp add: execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
256 |
|
37709 | 257 |
definition raise :: "string \<Rightarrow> 'a Heap" where -- {* the string is just decoration *} |
258 |
[code del]: "raise s = Heap (\<lambda>_. None)" |
|
26170 | 259 |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
260 |
lemma execute_raise [execute_simps]: |
37709 | 261 |
"execute (raise s) = (\<lambda>_. None)" |
26170 | 262 |
by (simp add: raise_def) |
263 |
||
40671 | 264 |
lemma effect_raiseE [effect_elims]: |
265 |
assumes "effect (raise x) h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
266 |
obtains "False" |
40671 | 267 |
using assms by (rule effectE) (simp add: success_def execute_simps) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
268 |
|
37792 | 269 |
definition bind :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" where |
270 |
[code del]: "bind f g = Heap (\<lambda>h. case execute f h of |
|
37709 | 271 |
Some (x, h') \<Rightarrow> execute (g x) h' |
272 |
| None \<Rightarrow> None)" |
|
273 |
||
37792 | 274 |
setup {* |
275 |
Adhoc_Overloading.add_variant |
|
37816 | 276 |
@{const_name Monad_Syntax.bind} @{const_name Heap_Monad.bind} |
37792 | 277 |
*} |
278 |
||
37758 | 279 |
lemma execute_bind [execute_simps]: |
37709 | 280 |
"execute f h = Some (x, h') \<Longrightarrow> execute (f \<guillemotright>= g) h = execute (g x) h'" |
281 |
"execute f h = None \<Longrightarrow> execute (f \<guillemotright>= g) h = None" |
|
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
282 |
by (simp_all add: bind_def) |
37709 | 283 |
|
38409 | 284 |
lemma execute_bind_case: |
285 |
"execute (f \<guillemotright>= g) h = (case (execute f h) of |
|
286 |
Some (x, h') \<Rightarrow> execute (g x) h' | None \<Rightarrow> None)" |
|
287 |
by (simp add: bind_def) |
|
288 |
||
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
289 |
lemma execute_bind_success: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
290 |
"success f h \<Longrightarrow> execute (f \<guillemotright>= g) h = execute (g (fst (the (execute f h)))) (snd (the (execute f h)))" |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
291 |
by (cases f h rule: Heap_cases) (auto elim!: successE simp add: bind_def) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
292 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
293 |
lemma success_bind_executeI: |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
294 |
"execute f h = Some (x, h') \<Longrightarrow> success (g x) h' \<Longrightarrow> success (f \<guillemotright>= g) h" |
37758 | 295 |
by (auto intro!: successI elim!: successE simp add: bind_def) |
296 |
||
40671 | 297 |
lemma success_bind_effectI [success_intros]: |
298 |
"effect f h h' x \<Longrightarrow> success (g x) h' \<Longrightarrow> success (f \<guillemotright>= g) h" |
|
299 |
by (auto simp add: effect_def success_def bind_def) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
300 |
|
40671 | 301 |
lemma effect_bindI [effect_intros]: |
302 |
assumes "effect f h h' r" "effect (g r) h' h'' r'" |
|
303 |
shows "effect (f \<guillemotright>= g) h h'' r'" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
304 |
using assms |
40671 | 305 |
apply (auto intro!: effectI elim!: effectE successE) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
306 |
apply (subst execute_bind, simp_all) |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
307 |
done |
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
308 |
|
40671 | 309 |
lemma effect_bindE [effect_elims]: |
310 |
assumes "effect (f \<guillemotright>= g) h h'' r'" |
|
311 |
obtains h' r where "effect f h h' r" "effect (g r) h' h'' r'" |
|
312 |
using assms by (auto simp add: effect_def bind_def split: option.split_asm) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
313 |
|
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
314 |
lemma execute_bind_eq_SomeI: |
37878 | 315 |
assumes "execute f h = Some (x, h')" |
316 |
and "execute (g x) h' = Some (y, h'')" |
|
317 |
shows "execute (f \<guillemotright>= g) h = Some (y, h'')" |
|
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
318 |
using assms by (simp add: bind_def) |
37754 | 319 |
|
37709 | 320 |
lemma return_bind [simp]: "return x \<guillemotright>= f = f x" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
321 |
by (rule Heap_eqI) (simp add: execute_bind execute_simps) |
37709 | 322 |
|
323 |
lemma bind_return [simp]: "f \<guillemotright>= return = f" |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
324 |
by (rule Heap_eqI) (simp add: bind_def execute_simps split: option.splits) |
37709 | 325 |
|
37828 | 326 |
lemma bind_bind [simp]: "(f \<guillemotright>= g) \<guillemotright>= k = (f :: 'a Heap) \<guillemotright>= (\<lambda>x. g x \<guillemotright>= k)" |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
327 |
by (rule Heap_eqI) (simp add: bind_def execute_simps split: option.splits) |
37709 | 328 |
|
329 |
lemma raise_bind [simp]: "raise e \<guillemotright>= f = raise e" |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
330 |
by (rule Heap_eqI) (simp add: execute_simps) |
37709 | 331 |
|
26170 | 332 |
|
37758 | 333 |
subsection {* Generic combinators *} |
26170 | 334 |
|
37758 | 335 |
subsubsection {* Assertions *} |
26170 | 336 |
|
37709 | 337 |
definition assert :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a Heap" where |
338 |
"assert P x = (if P x then return x else raise ''assert'')" |
|
28742 | 339 |
|
37758 | 340 |
lemma execute_assert [execute_simps]: |
37754 | 341 |
"P x \<Longrightarrow> execute (assert P x) h = Some (x, h)" |
342 |
"\<not> P x \<Longrightarrow> execute (assert P x) h = None" |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
343 |
by (simp_all add: assert_def execute_simps) |
37754 | 344 |
|
37758 | 345 |
lemma success_assertI [success_intros]: |
346 |
"P x \<Longrightarrow> success (assert P x) h" |
|
347 |
by (rule successI) (simp add: execute_assert) |
|
348 |
||
40671 | 349 |
lemma effect_assertI [effect_intros]: |
350 |
"P x \<Longrightarrow> h' = h \<Longrightarrow> r = x \<Longrightarrow> effect (assert P x) h h' r" |
|
351 |
by (rule effectI) (simp add: execute_assert) |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
352 |
|
40671 | 353 |
lemma effect_assertE [effect_elims]: |
354 |
assumes "effect (assert P x) h h' r" |
|
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
355 |
obtains "P x" "r = x" "h' = h" |
40671 | 356 |
using assms by (rule effectE) (cases "P x", simp_all add: execute_assert success_def) |
37771
1bec64044b5e
spelt out relational framework in a consistent way
haftmann
parents:
37758
diff
changeset
|
357 |
|
28742 | 358 |
lemma assert_cong [fundef_cong]: |
359 |
assumes "P = P'" |
|
360 |
assumes "\<And>x. P' x \<Longrightarrow> f x = f' x" |
|
361 |
shows "(assert P x >>= f) = (assert P' x >>= f')" |
|
37754 | 362 |
by (rule Heap_eqI) (insert assms, simp add: assert_def) |
28742 | 363 |
|
37758 | 364 |
|
365 |
subsubsection {* Plain lifting *} |
|
366 |
||
37754 | 367 |
definition lift :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b Heap" where |
368 |
"lift f = return o f" |
|
37709 | 369 |
|
37754 | 370 |
lemma lift_collapse [simp]: |
371 |
"lift f x = return (f x)" |
|
372 |
by (simp add: lift_def) |
|
37709 | 373 |
|
37754 | 374 |
lemma bind_lift: |
375 |
"(f \<guillemotright>= lift g) = (f \<guillemotright>= (\<lambda>x. return (g x)))" |
|
376 |
by (simp add: lift_def comp_def) |
|
37709 | 377 |
|
37758 | 378 |
|
379 |
subsubsection {* Iteration -- warning: this is rarely useful! *} |
|
380 |
||
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
381 |
primrec fold_map :: "('a \<Rightarrow> 'b Heap) \<Rightarrow> 'a list \<Rightarrow> 'b list Heap" where |
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
382 |
"fold_map f [] = return []" |
37792 | 383 |
| "fold_map f (x # xs) = do { |
37709 | 384 |
y \<leftarrow> f x; |
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
385 |
ys \<leftarrow> fold_map f xs; |
37709 | 386 |
return (y # ys) |
37792 | 387 |
}" |
37709 | 388 |
|
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
389 |
lemma fold_map_append: |
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
390 |
"fold_map f (xs @ ys) = fold_map f xs \<guillemotright>= (\<lambda>xs. fold_map f ys \<guillemotright>= (\<lambda>ys. return (xs @ ys)))" |
37754 | 391 |
by (induct xs) simp_all |
392 |
||
37758 | 393 |
lemma execute_fold_map_unchanged_heap [execute_simps]: |
37754 | 394 |
assumes "\<And>x. x \<in> set xs \<Longrightarrow> \<exists>y. execute (f x) h = Some (y, h)" |
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
395 |
shows "execute (fold_map f xs) h = |
37754 | 396 |
Some (List.map (\<lambda>x. fst (the (execute (f x) h))) xs, h)" |
397 |
using assms proof (induct xs) |
|
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
398 |
case Nil show ?case by (simp add: execute_simps) |
37754 | 399 |
next |
400 |
case (Cons x xs) |
|
401 |
from Cons.prems obtain y |
|
402 |
where y: "execute (f x) h = Some (y, h)" by auto |
|
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
403 |
moreover from Cons.prems Cons.hyps have "execute (fold_map f xs) h = |
37754 | 404 |
Some (map (\<lambda>x. fst (the (execute (f x) h))) xs, h)" by auto |
37787
30dc3abf4a58
theorem collections do not contain default rules any longer
haftmann
parents:
37772
diff
changeset
|
405 |
ultimately show ?case by (simp, simp only: execute_bind(1), simp add: execute_simps) |
37754 | 406 |
qed |
407 |
||
40267 | 408 |
|
409 |
subsection {* Partial function definition setup *} |
|
410 |
||
411 |
definition Heap_ord :: "'a Heap \<Rightarrow> 'a Heap \<Rightarrow> bool" where |
|
412 |
"Heap_ord = img_ord execute (fun_ord option_ord)" |
|
413 |
||
414 |
definition Heap_lub :: "('a Heap \<Rightarrow> bool) \<Rightarrow> 'a Heap" where |
|
415 |
"Heap_lub = img_lub execute Heap (fun_lub (flat_lub None))" |
|
416 |
||
417 |
interpretation heap!: partial_function_definitions Heap_ord Heap_lub |
|
418 |
proof - |
|
419 |
have "partial_function_definitions (fun_ord option_ord) (fun_lub (flat_lub None))" |
|
420 |
by (rule partial_function_lift) (rule flat_interpretation) |
|
421 |
then have "partial_function_definitions (img_ord execute (fun_ord option_ord)) |
|
422 |
(img_lub execute Heap (fun_lub (flat_lub None)))" |
|
423 |
by (rule partial_function_image) (auto intro: Heap_eqI) |
|
424 |
then show "partial_function_definitions Heap_ord Heap_lub" |
|
425 |
by (simp only: Heap_ord_def Heap_lub_def) |
|
426 |
qed |
|
427 |
||
428 |
abbreviation "mono_Heap \<equiv> monotone (fun_ord Heap_ord) Heap_ord" |
|
429 |
||
430 |
lemma Heap_ordI: |
|
431 |
assumes "\<And>h. execute x h = None \<or> execute x h = execute y h" |
|
432 |
shows "Heap_ord x y" |
|
433 |
using assms unfolding Heap_ord_def img_ord_def fun_ord_def flat_ord_def |
|
434 |
by blast |
|
435 |
||
436 |
lemma Heap_ordE: |
|
437 |
assumes "Heap_ord x y" |
|
438 |
obtains "execute x h = None" | "execute x h = execute y h" |
|
439 |
using assms unfolding Heap_ord_def img_ord_def fun_ord_def flat_ord_def |
|
440 |
by atomize_elim blast |
|
441 |
||
442 |
lemma bind_mono[partial_function_mono]: |
|
443 |
assumes mf: "mono_Heap B" and mg: "\<And>y. mono_Heap (\<lambda>f. C y f)" |
|
444 |
shows "mono_Heap (\<lambda>f. B f \<guillemotright>= (\<lambda>y. C y f))" |
|
445 |
proof (rule monotoneI) |
|
446 |
fix f g :: "'a \<Rightarrow> 'b Heap" assume fg: "fun_ord Heap_ord f g" |
|
447 |
from mf |
|
448 |
have 1: "Heap_ord (B f) (B g)" by (rule monotoneD) (rule fg) |
|
449 |
from mg |
|
450 |
have 2: "\<And>y'. Heap_ord (C y' f) (C y' g)" by (rule monotoneD) (rule fg) |
|
451 |
||
452 |
have "Heap_ord (B f \<guillemotright>= (\<lambda>y. C y f)) (B g \<guillemotright>= (\<lambda>y. C y f))" |
|
453 |
(is "Heap_ord ?L ?R") |
|
454 |
proof (rule Heap_ordI) |
|
455 |
fix h |
|
456 |
from 1 show "execute ?L h = None \<or> execute ?L h = execute ?R h" |
|
457 |
by (rule Heap_ordE[where h = h]) (auto simp: execute_bind_case) |
|
458 |
qed |
|
459 |
also |
|
460 |
have "Heap_ord (B g \<guillemotright>= (\<lambda>y'. C y' f)) (B g \<guillemotright>= (\<lambda>y'. C y' g))" |
|
461 |
(is "Heap_ord ?L ?R") |
|
462 |
proof (rule Heap_ordI) |
|
463 |
fix h |
|
464 |
show "execute ?L h = None \<or> execute ?L h = execute ?R h" |
|
465 |
proof (cases "execute (B g) h") |
|
466 |
case None |
|
467 |
then have "execute ?L h = None" by (auto simp: execute_bind_case) |
|
468 |
thus ?thesis .. |
|
469 |
next |
|
470 |
case Some |
|
471 |
then obtain r h' where "execute (B g) h = Some (r, h')" |
|
472 |
by (metis surjective_pairing) |
|
473 |
then have "execute ?L h = execute (C r f) h'" |
|
474 |
"execute ?R h = execute (C r g) h'" |
|
475 |
by (auto simp: execute_bind_case) |
|
476 |
with 2[of r] show ?thesis by (auto elim: Heap_ordE) |
|
477 |
qed |
|
478 |
qed |
|
479 |
finally (heap.leq_trans) |
|
480 |
show "Heap_ord (B f \<guillemotright>= (\<lambda>y. C y f)) (B g \<guillemotright>= (\<lambda>y'. C y' g))" . |
|
481 |
qed |
|
482 |
||
483 |
||
26182 | 484 |
subsection {* Code generator setup *} |
485 |
||
486 |
subsubsection {* Logical intermediate layer *} |
|
487 |
||
39250
548a3e5521ab
changing String.literal to a type instead of a datatype
bulwahn
parents:
39198
diff
changeset
|
488 |
definition raise' :: "String.literal \<Rightarrow> 'a Heap" where |
548a3e5521ab
changing String.literal to a type instead of a datatype
bulwahn
parents:
39198
diff
changeset
|
489 |
[code del]: "raise' s = raise (explode s)" |
548a3e5521ab
changing String.literal to a type instead of a datatype
bulwahn
parents:
39198
diff
changeset
|
490 |
|
548a3e5521ab
changing String.literal to a type instead of a datatype
bulwahn
parents:
39198
diff
changeset
|
491 |
lemma [code_post]: "raise' (STR s) = raise s" |
548a3e5521ab
changing String.literal to a type instead of a datatype
bulwahn
parents:
39198
diff
changeset
|
492 |
unfolding raise'_def by (simp add: STR_inverse) |
26182 | 493 |
|
37709 | 494 |
lemma raise_raise' [code_inline]: |
495 |
"raise s = raise' (STR s)" |
|
39250
548a3e5521ab
changing String.literal to a type instead of a datatype
bulwahn
parents:
39198
diff
changeset
|
496 |
unfolding raise'_def by (simp add: STR_inverse) |
26182 | 497 |
|
37709 | 498 |
code_datatype raise' -- {* avoid @{const "Heap"} formally *} |
26182 | 499 |
|
500 |
||
27707 | 501 |
subsubsection {* SML and OCaml *} |
26182 | 502 |
|
26752 | 503 |
code_type Heap (SML "unit/ ->/ _") |
37828 | 504 |
code_const bind (SML "!(fn/ f'_/ =>/ fn/ ()/ =>/ f'_/ (_/ ())/ ())") |
27707 | 505 |
code_const return (SML "!(fn/ ()/ =>/ _)") |
37709 | 506 |
code_const Heap_Monad.raise' (SML "!(raise/ Fail/ _)") |
26182 | 507 |
|
37754 | 508 |
code_type Heap (OCaml "unit/ ->/ _") |
37828 | 509 |
code_const bind (OCaml "!(fun/ f'_/ ()/ ->/ f'_/ (_/ ())/ ())") |
27707 | 510 |
code_const return (OCaml "!(fun/ ()/ ->/ _)") |
37828 | 511 |
code_const Heap_Monad.raise' (OCaml "failwith") |
27707 | 512 |
|
37838 | 513 |
|
514 |
subsubsection {* Haskell *} |
|
515 |
||
516 |
text {* Adaption layer *} |
|
517 |
||
518 |
code_include Haskell "Heap" |
|
519 |
{*import qualified Control.Monad; |
|
520 |
import qualified Control.Monad.ST; |
|
521 |
import qualified Data.STRef; |
|
522 |
import qualified Data.Array.ST; |
|
523 |
||
37964 | 524 |
import Natural; |
525 |
||
37838 | 526 |
type RealWorld = Control.Monad.ST.RealWorld; |
527 |
type ST s a = Control.Monad.ST.ST s a; |
|
528 |
type STRef s a = Data.STRef.STRef s a; |
|
37964 | 529 |
type STArray s a = Data.Array.ST.STArray s Natural a; |
37838 | 530 |
|
531 |
newSTRef = Data.STRef.newSTRef; |
|
532 |
readSTRef = Data.STRef.readSTRef; |
|
533 |
writeSTRef = Data.STRef.writeSTRef; |
|
534 |
||
37964 | 535 |
newArray :: Natural -> a -> ST s (STArray s a); |
37838 | 536 |
newArray k = Data.Array.ST.newArray (0, k); |
537 |
||
538 |
newListArray :: [a] -> ST s (STArray s a); |
|
37964 | 539 |
newListArray xs = Data.Array.ST.newListArray (0, (fromInteger . toInteger . length) xs) xs; |
37838 | 540 |
|
37964 | 541 |
newFunArray :: Natural -> (Natural -> a) -> ST s (STArray s a); |
37838 | 542 |
newFunArray k f = Data.Array.ST.newListArray (0, k) (map f [0..k-1]); |
543 |
||
37964 | 544 |
lengthArray :: STArray s a -> ST s Natural; |
37838 | 545 |
lengthArray a = Control.Monad.liftM snd (Data.Array.ST.getBounds a); |
546 |
||
37964 | 547 |
readArray :: STArray s a -> Natural -> ST s a; |
37838 | 548 |
readArray = Data.Array.ST.readArray; |
549 |
||
37964 | 550 |
writeArray :: STArray s a -> Natural -> a -> ST s (); |
37838 | 551 |
writeArray = Data.Array.ST.writeArray;*} |
552 |
||
553 |
code_reserved Haskell Heap |
|
554 |
||
555 |
text {* Monad *} |
|
556 |
||
557 |
code_type Heap (Haskell "Heap.ST/ Heap.RealWorld/ _") |
|
558 |
code_monad bind Haskell |
|
559 |
code_const return (Haskell "return") |
|
560 |
code_const Heap_Monad.raise' (Haskell "error") |
|
561 |
||
562 |
||
563 |
subsubsection {* Scala *} |
|
564 |
||
37842 | 565 |
code_include Scala "Heap" |
38968
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
566 |
{*object Heap { |
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
567 |
def bind[A, B](f: Unit => A, g: A => Unit => B): Unit => B = (_: Unit) => g (f ()) () |
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
568 |
} |
37842 | 569 |
|
570 |
class Ref[A](x: A) { |
|
571 |
var value = x |
|
572 |
} |
|
573 |
||
574 |
object Ref { |
|
38771 | 575 |
def apply[A](x: A): Ref[A] = |
576 |
new Ref[A](x) |
|
577 |
def lookup[A](r: Ref[A]): A = |
|
578 |
r.value |
|
579 |
def update[A](r: Ref[A], x: A): Unit = |
|
580 |
{ r.value = x } |
|
37842 | 581 |
} |
582 |
||
37964 | 583 |
object Array { |
38968
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
584 |
import collection.mutable.ArraySeq |
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
585 |
def alloc[A](n: Natural)(x: A): ArraySeq[A] = |
38771 | 586 |
ArraySeq.fill(n.as_Int)(x) |
38968
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
587 |
def make[A](n: Natural)(f: Natural => A): ArraySeq[A] = |
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
588 |
ArraySeq.tabulate(n.as_Int)((k: Int) => f(Natural(k))) |
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
589 |
def len[A](a: ArraySeq[A]): Natural = |
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
590 |
Natural(a.length) |
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
591 |
def nth[A](a: ArraySeq[A], n: Natural): A = |
38771 | 592 |
a(n.as_Int) |
38968
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
593 |
def upd[A](a: ArraySeq[A], n: Natural, x: A): Unit = |
38771 | 594 |
a.update(n.as_Int, x) |
595 |
def freeze[A](a: ArraySeq[A]): List[A] = |
|
596 |
a.toList |
|
38968
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
597 |
} |
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
598 |
*} |
37842 | 599 |
|
38968
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
haftmann
parents:
38773
diff
changeset
|
600 |
code_reserved Scala Heap Ref Array |
37838 | 601 |
|
602 |
code_type Heap (Scala "Unit/ =>/ _") |
|
38771 | 603 |
code_const bind (Scala "Heap.bind") |
37842 | 604 |
code_const return (Scala "('_: Unit)/ =>/ _") |
37845
b70d7a347964
first roughly working version of Imperative HOL for Scala
haftmann
parents:
37842
diff
changeset
|
605 |
code_const Heap_Monad.raise' (Scala "!error((_))") |
37838 | 606 |
|
607 |
||
608 |
subsubsection {* Target variants with less units *} |
|
609 |
||
31871 | 610 |
setup {* |
611 |
||
612 |
let |
|
27707 | 613 |
|
31871 | 614 |
open Code_Thingol; |
615 |
||
616 |
fun imp_program naming = |
|
27707 | 617 |
|
31871 | 618 |
let |
619 |
fun is_const c = case lookup_const naming c |
|
620 |
of SOME c' => (fn c'' => c' = c'') |
|
621 |
| NONE => K false; |
|
37756
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents:
37754
diff
changeset
|
622 |
val is_bind = is_const @{const_name bind}; |
31871 | 623 |
val is_return = is_const @{const_name return}; |
31893 | 624 |
val dummy_name = ""; |
625 |
val dummy_case_term = IVar NONE; |
|
31871 | 626 |
(*assumption: dummy values are not relevant for serialization*) |
38057 | 627 |
val (unitt, unitT) = case lookup_const naming @{const_name Unity} |
628 |
of SOME unit' => (IConst (unit', (([], []), [])), the (lookup_tyco naming @{type_name unit}) `%% []) |
|
31871 | 629 |
| NONE => error ("Must include " ^ @{const_name Unity} ^ " in generated constants."); |
630 |
fun dest_abs ((v, ty) `|=> t, _) = ((v, ty), t) |
|
631 |
| dest_abs (t, ty) = |
|
632 |
let |
|
633 |
val vs = fold_varnames cons t []; |
|
634 |
val v = Name.variant vs "x"; |
|
635 |
val ty' = (hd o fst o unfold_fun) ty; |
|
31893 | 636 |
in ((SOME v, ty'), t `$ IVar (SOME v)) end; |
31871 | 637 |
fun force (t as IConst (c, _) `$ t') = if is_return c |
638 |
then t' else t `$ unitt |
|
639 |
| force t = t `$ unitt; |
|
38385 | 640 |
fun tr_bind'' [(t1, _), (t2, ty2)] = |
31871 | 641 |
let |
642 |
val ((v, ty), t) = dest_abs (t2, ty2); |
|
38385 | 643 |
in ICase (((force t1, ty), [(IVar v, tr_bind' t)]), dummy_case_term) end |
644 |
and tr_bind' t = case unfold_app t |
|
38386 | 645 |
of (IConst (c, (_, ty1 :: ty2 :: _)), [x1, x2]) => if is_bind c |
646 |
then tr_bind'' [(x1, ty1), (x2, ty2)] |
|
647 |
else force t |
|
648 |
| _ => force t; |
|
38057 | 649 |
fun imp_monad_bind'' ts = (SOME dummy_name, unitT) `|=> ICase (((IVar (SOME dummy_name), unitT), |
38385 | 650 |
[(unitt, tr_bind'' ts)]), dummy_case_term) |
651 |
fun imp_monad_bind' (const as (c, (_, tys))) ts = if is_bind c then case (ts, tys) |
|
31871 | 652 |
of ([t1, t2], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)] |
653 |
| ([t1, t2, t3], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)] `$ t3 |
|
654 |
| (ts, _) => imp_monad_bind (eta_expand 2 (const, ts)) |
|
655 |
else IConst const `$$ map imp_monad_bind ts |
|
656 |
and imp_monad_bind (IConst const) = imp_monad_bind' const [] |
|
657 |
| imp_monad_bind (t as IVar _) = t |
|
658 |
| imp_monad_bind (t as _ `$ _) = (case unfold_app t |
|
659 |
of (IConst const, ts) => imp_monad_bind' const ts |
|
660 |
| (t, ts) => imp_monad_bind t `$$ map imp_monad_bind ts) |
|
661 |
| imp_monad_bind (v_ty `|=> t) = v_ty `|=> imp_monad_bind t |
|
662 |
| imp_monad_bind (ICase (((t, ty), pats), t0)) = ICase |
|
663 |
(((imp_monad_bind t, ty), |
|
664 |
(map o pairself) imp_monad_bind pats), |
|
665 |
imp_monad_bind t0); |
|
28663
bd8438543bf2
code identifier namings are no longer imperative
haftmann
parents:
28562
diff
changeset
|
666 |
|
39021 | 667 |
in (Graph.map o K o map_terms_stmt) imp_monad_bind end; |
27707 | 668 |
|
669 |
in |
|
670 |
||
31871 | 671 |
Code_Target.extend_target ("SML_imp", ("SML", imp_program)) |
672 |
#> Code_Target.extend_target ("OCaml_imp", ("OCaml", imp_program)) |
|
37838 | 673 |
#> Code_Target.extend_target ("Scala_imp", ("Scala", imp_program)) |
27707 | 674 |
|
675 |
end |
|
31871 | 676 |
|
27707 | 677 |
*} |
678 |
||
37758 | 679 |
hide_const (open) Heap heap guard raise' fold_map |
37724 | 680 |
|
26170 | 681 |
end |