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