| author | blanchet | 
| Thu, 26 Sep 2013 13:42:14 +0200 | |
| changeset 53917 | bf74357f91f8 | 
| parent 53361 | 1cb7d3c0cf31 | 
| child 54630 | 9061af4d5ebc | 
| permissions | -rw-r--r-- | 
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
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: 
37758diff
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: 
40671diff
changeset | 8 | imports | 
| 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
40671diff
changeset | 9 | Heap | 
| 
64cd30d6b0b8
explicit file specifications -- avoid secondary load path;
 wenzelm parents: 
40671diff
changeset | 10 | "~~/src/HOL/Library/Monad_Syntax" | 
| 26170 | 11 | begin | 
| 12 | ||
| 13 | subsection {* The monad *}
 | |
| 14 | ||
| 37758 | 15 | subsubsection {* Monad construction *}
 | 
| 26170 | 16 | |
| 17 | text {* Monadic heap actions either produce values
 | |
| 18 | and transform the heap, or fail *} | |
| 37709 | 19 | datatype 'a Heap = Heap "heap \<Rightarrow> ('a \<times> heap) option"
 | 
| 26170 | 20 | |
| 40266 
d72f1f734e5a
remove term_of equations for Heap type explicitly
 haftmann parents: 
40173diff
changeset | 21 | lemma [code, code del]: | 
| 
d72f1f734e5a
remove term_of equations for Heap type explicitly
 haftmann parents: 
40173diff
changeset | 22 | "(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: 
40173diff
changeset | 23 | .. | 
| 
d72f1f734e5a
remove term_of equations for Heap type explicitly
 haftmann parents: 
40173diff
changeset | 24 | |
| 37709 | 25 | primrec execute :: "'a Heap \<Rightarrow> heap \<Rightarrow> ('a \<times> heap) option" where
 | 
| 26 | [code del]: "execute (Heap f) = f" | |
| 26170 | 27 | |
| 37758 | 28 | lemma Heap_cases [case_names succeed fail]: | 
| 29 | fixes f and h | |
| 30 | assumes succeed: "\<And>x h'. execute f h = Some (x, h') \<Longrightarrow> P" | |
| 31 | assumes fail: "execute f h = None \<Longrightarrow> P" | |
| 32 | shows P | |
| 33 | using assms by (cases "execute f h") auto | |
| 34 | ||
| 26170 | 35 | lemma Heap_execute [simp]: | 
| 36 | "Heap (execute f) = f" by (cases f) simp_all | |
| 37 | ||
| 38 | lemma Heap_eqI: | |
| 39 | "(\<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: 
39250diff
changeset | 40 | by (cases f, cases g) (auto simp: fun_eq_iff) | 
| 26170 | 41 | |
| 45294 | 42 | ML {* structure Execute_Simps = Named_Thms
 | 
| 43 | ( | |
| 44 |   val name = @{binding execute_simps}
 | |
| 37758 | 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: 
37772diff
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: 
37772diff
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: 
37772diff
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: 
37758diff
changeset | 91 | obtains r h' where "r = fst (the (execute c h))" | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 92 | and "h' = snd (the (execute c h))" | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 93 | and "execute f h \<noteq> None" | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 94 | using assms by (simp add: success_def) | 
| 37758 | 95 | |
| 45294 | 96 | ML {* structure Success_Intros = Named_Thms
 | 
| 97 | ( | |
| 98 |   val name = @{binding success_intros}
 | |
| 37758 | 99 | val description = "introduction rules for success" | 
| 100 | ) *} | |
| 101 | ||
| 102 | setup Success_Intros.setup | |
| 103 | ||
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 104 | lemma success_tapI [success_intros]: | 
| 37758 | 105 | "success (tap f) h" | 
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 106 | by (rule successI) (simp add: execute_simps) | 
| 37758 | 107 | |
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 108 | lemma success_heapI [success_intros]: | 
| 37758 | 109 | "success (heap f) h" | 
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 110 | by (rule successI) (simp add: execute_simps) | 
| 37758 | 111 | |
| 112 | lemma success_guardI [success_intros]: | |
| 113 | "P h \<Longrightarrow> success (guard P f) h" | |
| 114 | by (rule successI) (simp add: execute_guard) | |
| 115 | ||
| 116 | lemma success_LetI [success_intros]: | |
| 117 | "x = t \<Longrightarrow> success (f x) h \<Longrightarrow> success (let x = t in f x) h" | |
| 118 | by (simp add: Let_def) | |
| 119 | ||
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 120 | lemma success_ifI: | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 121 | "(c \<Longrightarrow> success t h) \<Longrightarrow> (\<not> c \<Longrightarrow> success e h) \<Longrightarrow> | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 122 | success (if c then t else e) h" | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 123 | by (simp add: success_def) | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 124 | |
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 125 | |
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 126 | subsubsection {* Predicate for a simple relational calculus *}
 | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 127 | |
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 128 | text {*
 | 
| 40671 | 129 |   The @{text effect} predicate states that when a computation @{text c}
 | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 130 |   runs with the heap @{text h} will result in return value @{text r}
 | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 131 |   and a heap @{text "h'"}, i.e.~no exception occurs.
 | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 132 | *} | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 133 | |
| 40671 | 134 | definition effect :: "'a Heap \<Rightarrow> heap \<Rightarrow> heap \<Rightarrow> 'a \<Rightarrow> bool" where | 
| 135 | 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: 
37758diff
changeset | 136 | |
| 40671 | 137 | lemma effectI: | 
| 138 | "execute c h = Some (r, h') \<Longrightarrow> effect c h h' r" | |
| 139 | by (simp add: effect_def) | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 140 | |
| 40671 | 141 | lemma effectE: | 
| 142 | assumes "effect c h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 143 | obtains "r = fst (the (execute c h))" | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 144 | and "h' = snd (the (execute c h))" | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 145 | and "success c h" | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 146 | proof (rule that) | 
| 40671 | 147 | 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: 
37758diff
changeset | 148 | then show "success c h" by (simp add: success_def) | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 149 | 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: 
37758diff
changeset | 150 | by simp_all | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 151 | then show "r = fst (the (execute c h))" | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 152 | and "h' = snd (the (execute c h))" by simp_all | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 153 | qed | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 154 | |
| 40671 | 155 | lemma effect_success: | 
| 156 | "effect c h h' r \<Longrightarrow> success c h" | |
| 157 | by (simp add: effect_def success_def) | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 158 | |
| 40671 | 159 | lemma success_effectE: | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 160 | assumes "success c h" | 
| 40671 | 161 | obtains r h' where "effect c h h' r" | 
| 162 | using assms by (auto simp add: effect_def success_def) | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 163 | |
| 40671 | 164 | lemma effect_deterministic: | 
| 165 | assumes "effect f h h' a" | |
| 166 | and "effect f h h'' b" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 167 | shows "a = b" and "h' = h''" | 
| 40671 | 168 | using assms unfolding effect_def by auto | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 169 | |
| 46029 
4a19e3d147c3
attribute code_abbrev superseedes code_unfold_post; tuned names and spacing
 haftmann parents: 
45294diff
changeset | 170 | ML {* structure Effect_Intros = Named_Thms
 | 
| 45294 | 171 | ( | 
| 172 |   val name = @{binding effect_intros}
 | |
| 40671 | 173 | val description = "introduction rules for effect" | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 174 | ) *} | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 175 | |
| 46029 
4a19e3d147c3
attribute code_abbrev superseedes code_unfold_post; tuned names and spacing
 haftmann parents: 
45294diff
changeset | 176 | ML {* structure Effect_Elims = Named_Thms
 | 
| 45294 | 177 | ( | 
| 178 |   val name = @{binding effect_elims}
 | |
| 40671 | 179 | val description = "elimination rules for effect" | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 180 | ) *} | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 181 | |
| 46029 
4a19e3d147c3
attribute code_abbrev superseedes code_unfold_post; tuned names and spacing
 haftmann parents: 
45294diff
changeset | 182 | setup "Effect_Intros.setup #> Effect_Elims.setup" | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 183 | |
| 40671 | 184 | lemma effect_LetI [effect_intros]: | 
| 185 | assumes "x = t" "effect (f x) h h' r" | |
| 186 | shows "effect (let x = t in f x) h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 187 | using assms by simp | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 188 | |
| 40671 | 189 | lemma effect_LetE [effect_elims]: | 
| 190 | assumes "effect (let x = t in f x) h h' r" | |
| 191 | obtains "effect (f t) h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 192 | using assms by simp | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 193 | |
| 40671 | 194 | lemma effect_ifI: | 
| 195 | assumes "c \<Longrightarrow> effect t h h' r" | |
| 196 | and "\<not> c \<Longrightarrow> effect e h h' r" | |
| 197 | shows "effect (if c then t else e) h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 198 | by (cases c) (simp_all add: assms) | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 199 | |
| 40671 | 200 | lemma effect_ifE: | 
| 201 | assumes "effect (if c then t else e) h h' r" | |
| 202 | obtains "c" "effect t h h' r" | |
| 203 | | "\<not> c" "effect e h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 204 | using assms by (cases c) simp_all | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 205 | |
| 40671 | 206 | lemma effect_tapI [effect_intros]: | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 207 | assumes "h' = h" "r = f h" | 
| 40671 | 208 | shows "effect (tap f) h h' r" | 
| 209 | by (rule effectI) (simp add: assms execute_simps) | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 210 | |
| 40671 | 211 | lemma effect_tapE [effect_elims]: | 
| 212 | assumes "effect (tap f) h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 213 | obtains "h' = h" and "r = f h" | 
| 40671 | 214 | using assms by (rule effectE) (auto simp add: execute_simps) | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 215 | |
| 40671 | 216 | lemma effect_heapI [effect_intros]: | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 217 | assumes "h' = snd (f h)" "r = fst (f h)" | 
| 40671 | 218 | shows "effect (heap f) h h' r" | 
| 219 | by (rule effectI) (simp add: assms execute_simps) | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 220 | |
| 40671 | 221 | lemma effect_heapE [effect_elims]: | 
| 222 | assumes "effect (heap f) h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 223 | obtains "h' = snd (f h)" and "r = fst (f h)" | 
| 40671 | 224 | using assms by (rule effectE) (simp add: execute_simps) | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 225 | |
| 40671 | 226 | lemma effect_guardI [effect_intros]: | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 227 | assumes "P h" "h' = snd (f h)" "r = fst (f h)" | 
| 40671 | 228 | shows "effect (guard P f) h h' r" | 
| 229 | by (rule effectI) (simp add: assms execute_simps) | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 230 | |
| 40671 | 231 | lemma effect_guardE [effect_elims]: | 
| 232 | assumes "effect (guard P f) h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 233 | obtains "h' = snd (f h)" "r = fst (f h)" "P h" | 
| 40671 | 234 | using assms by (rule effectE) | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 235 | (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: 
37758diff
changeset | 236 | |
| 37758 | 237 | |
| 238 | subsubsection {* Monad combinators *}
 | |
| 26170 | 239 | |
| 37709 | 240 | definition return :: "'a \<Rightarrow> 'a Heap" where | 
| 26170 | 241 | [code del]: "return x = heap (Pair x)" | 
| 242 | ||
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 243 | lemma execute_return [execute_simps]: | 
| 37709 | 244 | "execute (return x) = Some \<circ> Pair x" | 
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 245 | by (simp add: return_def execute_simps) | 
| 26170 | 246 | |
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 247 | lemma success_returnI [success_intros]: | 
| 37758 | 248 | "success (return x) h" | 
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 249 | by (rule successI) (simp add: execute_simps) | 
| 37758 | 250 | |
| 40671 | 251 | lemma effect_returnI [effect_intros]: | 
| 252 | "h = h' \<Longrightarrow> effect (return x) h h' x" | |
| 253 | by (rule effectI) (simp add: execute_simps) | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 254 | |
| 40671 | 255 | lemma effect_returnE [effect_elims]: | 
| 256 | assumes "effect (return x) h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 257 | obtains "r = x" "h' = h" | 
| 40671 | 258 | using assms by (rule effectE) (simp add: execute_simps) | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 259 | |
| 37709 | 260 | definition raise :: "string \<Rightarrow> 'a Heap" where -- {* the string is just decoration *}
 | 
| 261 | [code del]: "raise s = Heap (\<lambda>_. None)" | |
| 26170 | 262 | |
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 263 | lemma execute_raise [execute_simps]: | 
| 37709 | 264 | "execute (raise s) = (\<lambda>_. None)" | 
| 26170 | 265 | by (simp add: raise_def) | 
| 266 | ||
| 40671 | 267 | lemma effect_raiseE [effect_elims]: | 
| 268 | assumes "effect (raise x) h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 269 | obtains "False" | 
| 40671 | 270 | using assms by (rule effectE) (simp add: success_def execute_simps) | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 271 | |
| 37792 | 272 | definition bind :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" where
 | 
| 273 | [code del]: "bind f g = Heap (\<lambda>h. case execute f h of | |
| 37709 | 274 | Some (x, h') \<Rightarrow> execute (g x) h' | 
| 275 | | None \<Rightarrow> None)" | |
| 276 | ||
| 52622 
e0ff1625e96d
localized and modernized adhoc-overloading (patch by Christian Sternagel);
 wenzelm parents: 
52435diff
changeset | 277 | adhoc_overloading | 
| 
e0ff1625e96d
localized and modernized adhoc-overloading (patch by Christian Sternagel);
 wenzelm parents: 
52435diff
changeset | 278 | Monad_Syntax.bind Heap_Monad.bind | 
| 37792 | 279 | |
| 37758 | 280 | lemma execute_bind [execute_simps]: | 
| 37709 | 281 | "execute f h = Some (x, h') \<Longrightarrow> execute (f \<guillemotright>= g) h = execute (g x) h'" | 
| 282 | "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: 
37754diff
changeset | 283 | by (simp_all add: bind_def) | 
| 37709 | 284 | |
| 38409 | 285 | lemma execute_bind_case: | 
| 286 | "execute (f \<guillemotright>= g) h = (case (execute f h) of | |
| 287 | Some (x, h') \<Rightarrow> execute (g x) h' | None \<Rightarrow> None)" | |
| 288 | by (simp add: bind_def) | |
| 289 | ||
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 290 | lemma execute_bind_success: | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 291 | "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: 
37758diff
changeset | 292 | 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: 
37758diff
changeset | 293 | |
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 294 | lemma success_bind_executeI: | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 295 | "execute f h = Some (x, h') \<Longrightarrow> success (g x) h' \<Longrightarrow> success (f \<guillemotright>= g) h" | 
| 37758 | 296 | by (auto intro!: successI elim!: successE simp add: bind_def) | 
| 297 | ||
| 40671 | 298 | lemma success_bind_effectI [success_intros]: | 
| 299 | "effect f h h' x \<Longrightarrow> success (g x) h' \<Longrightarrow> success (f \<guillemotright>= g) h" | |
| 300 | by (auto simp add: effect_def success_def bind_def) | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 301 | |
| 40671 | 302 | lemma effect_bindI [effect_intros]: | 
| 303 | assumes "effect f h h' r" "effect (g r) h' h'' r'" | |
| 304 | shows "effect (f \<guillemotright>= g) h h'' r'" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 305 | using assms | 
| 40671 | 306 | apply (auto intro!: effectI elim!: effectE successE) | 
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 307 | apply (subst execute_bind, simp_all) | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 308 | done | 
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 309 | |
| 40671 | 310 | lemma effect_bindE [effect_elims]: | 
| 311 | assumes "effect (f \<guillemotright>= g) h h'' r'" | |
| 312 | obtains h' r where "effect f h h' r" "effect (g r) h' h'' r'" | |
| 313 | 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: 
37758diff
changeset | 314 | |
| 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 315 | lemma execute_bind_eq_SomeI: | 
| 37878 | 316 | assumes "execute f h = Some (x, h')" | 
| 317 | and "execute (g x) h' = Some (y, h'')" | |
| 318 | 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: 
37754diff
changeset | 319 | using assms by (simp add: bind_def) | 
| 37754 | 320 | |
| 37709 | 321 | lemma return_bind [simp]: "return x \<guillemotright>= f = f x" | 
| 51485 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 322 | by (rule Heap_eqI) (simp add: execute_simps) | 
| 37709 | 323 | |
| 324 | lemma bind_return [simp]: "f \<guillemotright>= return = f" | |
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 325 | by (rule Heap_eqI) (simp add: bind_def execute_simps split: option.splits) | 
| 37709 | 326 | |
| 37828 | 327 | 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: 
37772diff
changeset | 328 | by (rule Heap_eqI) (simp add: bind_def execute_simps split: option.splits) | 
| 37709 | 329 | |
| 330 | lemma raise_bind [simp]: "raise e \<guillemotright>= f = raise e" | |
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 331 | by (rule Heap_eqI) (simp add: execute_simps) | 
| 37709 | 332 | |
| 26170 | 333 | |
| 37758 | 334 | subsection {* Generic combinators *}
 | 
| 26170 | 335 | |
| 37758 | 336 | subsubsection {* Assertions *}
 | 
| 26170 | 337 | |
| 37709 | 338 | definition assert :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a Heap" where
 | 
| 339 | "assert P x = (if P x then return x else raise ''assert'')" | |
| 28742 | 340 | |
| 37758 | 341 | lemma execute_assert [execute_simps]: | 
| 37754 | 342 | "P x \<Longrightarrow> execute (assert P x) h = Some (x, h)" | 
| 343 | "\<not> P x \<Longrightarrow> execute (assert P x) h = None" | |
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 344 | by (simp_all add: assert_def execute_simps) | 
| 37754 | 345 | |
| 37758 | 346 | lemma success_assertI [success_intros]: | 
| 347 | "P x \<Longrightarrow> success (assert P x) h" | |
| 348 | by (rule successI) (simp add: execute_assert) | |
| 349 | ||
| 40671 | 350 | lemma effect_assertI [effect_intros]: | 
| 351 | "P x \<Longrightarrow> h' = h \<Longrightarrow> r = x \<Longrightarrow> effect (assert P x) h h' r" | |
| 352 | by (rule effectI) (simp add: execute_assert) | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 353 | |
| 40671 | 354 | lemma effect_assertE [effect_elims]: | 
| 355 | assumes "effect (assert P x) h h' r" | |
| 37771 
1bec64044b5e
spelt out relational framework in a consistent way
 haftmann parents: 
37758diff
changeset | 356 | obtains "P x" "r = x" "h' = h" | 
| 40671 | 357 | 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: 
37758diff
changeset | 358 | |
| 28742 | 359 | lemma assert_cong [fundef_cong]: | 
| 360 | assumes "P = P'" | |
| 361 | assumes "\<And>x. P' x \<Longrightarrow> f x = f' x" | |
| 362 | shows "(assert P x >>= f) = (assert P' x >>= f')" | |
| 37754 | 363 | by (rule Heap_eqI) (insert assms, simp add: assert_def) | 
| 28742 | 364 | |
| 37758 | 365 | |
| 366 | subsubsection {* Plain lifting *}
 | |
| 367 | ||
| 37754 | 368 | definition lift :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b Heap" where
 | 
| 369 | "lift f = return o f" | |
| 37709 | 370 | |
| 37754 | 371 | lemma lift_collapse [simp]: | 
| 372 | "lift f x = return (f x)" | |
| 373 | by (simp add: lift_def) | |
| 37709 | 374 | |
| 37754 | 375 | lemma bind_lift: | 
| 376 | "(f \<guillemotright>= lift g) = (f \<guillemotright>= (\<lambda>x. return (g x)))" | |
| 377 | by (simp add: lift_def comp_def) | |
| 37709 | 378 | |
| 37758 | 379 | |
| 380 | subsubsection {* Iteration -- warning: this is rarely useful! *}
 | |
| 381 | ||
| 37756 
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
 haftmann parents: 
37754diff
changeset | 382 | 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: 
37754diff
changeset | 383 | "fold_map f [] = return []" | 
| 37792 | 384 | | "fold_map f (x # xs) = do {
 | 
| 37709 | 385 | 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: 
37754diff
changeset | 386 | ys \<leftarrow> fold_map f xs; | 
| 37709 | 387 | return (y # ys) | 
| 37792 | 388 | }" | 
| 37709 | 389 | |
| 37756 
59caa6180fff
avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
 haftmann parents: 
37754diff
changeset | 390 | 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: 
37754diff
changeset | 391 | "fold_map f (xs @ ys) = fold_map f xs \<guillemotright>= (\<lambda>xs. fold_map f ys \<guillemotright>= (\<lambda>ys. return (xs @ ys)))" | 
| 37754 | 392 | by (induct xs) simp_all | 
| 393 | ||
| 37758 | 394 | lemma execute_fold_map_unchanged_heap [execute_simps]: | 
| 37754 | 395 | 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: 
37754diff
changeset | 396 | shows "execute (fold_map f xs) h = | 
| 37754 | 397 | Some (List.map (\<lambda>x. fst (the (execute (f x) h))) xs, h)" | 
| 398 | using assms proof (induct xs) | |
| 37787 
30dc3abf4a58
theorem collections do not contain default rules any longer
 haftmann parents: 
37772diff
changeset | 399 | case Nil show ?case by (simp add: execute_simps) | 
| 37754 | 400 | next | 
| 401 | case (Cons x xs) | |
| 402 | from Cons.prems obtain y | |
| 403 | 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: 
37754diff
changeset | 404 | moreover from Cons.prems Cons.hyps have "execute (fold_map f xs) h = | 
| 37754 | 405 | 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: 
37772diff
changeset | 406 | ultimately show ?case by (simp, simp only: execute_bind(1), simp add: execute_simps) | 
| 37754 | 407 | qed | 
| 408 | ||
| 40267 | 409 | |
| 410 | subsection {* Partial function definition setup *}
 | |
| 411 | ||
| 412 | definition Heap_ord :: "'a Heap \<Rightarrow> 'a Heap \<Rightarrow> bool" where | |
| 413 | "Heap_ord = img_ord execute (fun_ord option_ord)" | |
| 414 | ||
| 44174 
d1d79f0e1ea6
make more HOL theories work with separate set type
 huffman parents: 
43324diff
changeset | 415 | definition Heap_lub :: "'a Heap set \<Rightarrow> 'a Heap" where | 
| 40267 | 416 | "Heap_lub = img_lub execute Heap (fun_lub (flat_lub None))" | 
| 417 | ||
| 51485 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 418 | lemma heap_interpretation: "partial_function_definitions Heap_ord Heap_lub" | 
| 40267 | 419 | proof - | 
| 420 | have "partial_function_definitions (fun_ord option_ord) (fun_lub (flat_lub None))" | |
| 421 | by (rule partial_function_lift) (rule flat_interpretation) | |
| 422 | then have "partial_function_definitions (img_ord execute (fun_ord option_ord)) | |
| 423 | (img_lub execute Heap (fun_lub (flat_lub None)))" | |
| 424 | by (rule partial_function_image) (auto intro: Heap_eqI) | |
| 425 | then show "partial_function_definitions Heap_ord Heap_lub" | |
| 426 | by (simp only: Heap_ord_def Heap_lub_def) | |
| 427 | qed | |
| 428 | ||
| 51485 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 429 | interpretation heap!: partial_function_definitions Heap_ord Heap_lub | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 430 | by (fact heap_interpretation) | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 431 | |
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 432 | lemma heap_step_admissible: | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 433 | "option.admissible | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 434 |       (\<lambda>f:: 'a => ('b * 'c) option. \<forall>h h' r. f h = Some (r, h') \<longrightarrow> P x h h' r)"
 | 
| 53361 
1cb7d3c0cf31
move admissible out of class ccpo to avoid unnecessary class predicate in foundational theorems
 Andreas Lochbihler parents: 
52728diff
changeset | 435 | proof (rule ccpo.admissibleI) | 
| 51485 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 436 |   fix A :: "('a \<Rightarrow> ('b * 'c) option) set"
 | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 437 | assume ch: "Complete_Partial_Order.chain option.le_fun A" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 438 | and IH: "\<forall>f\<in>A. \<forall>h h' r. f h = Some (r, h') \<longrightarrow> P x h h' r" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 439 |   from ch have ch': "\<And>x. Complete_Partial_Order.chain option_ord {y. \<exists>f\<in>A. y = f x}" by (rule chain_fun)
 | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 440 | show "\<forall>h h' r. option.lub_fun A h = Some (r, h') \<longrightarrow> P x h h' r" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 441 | proof (intro allI impI) | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 442 | fix h h' r assume "option.lub_fun A h = Some (r, h')" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 443 | from flat_lub_in_chain[OF ch' this[unfolded fun_lub_def]] | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 444 |     have "Some (r, h') \<in> {y. \<exists>f\<in>A. y = f h}" by simp
 | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 445 | then have "\<exists>f\<in>A. f h = Some (r, h')" by auto | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 446 | with IH show "P x h h' r" by auto | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 447 | qed | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 448 | qed | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 449 | |
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 450 | lemma admissible_heap: | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 451 | "heap.admissible (\<lambda>f. \<forall>x h h' r. effect (f x) h h' r \<longrightarrow> P x h h' r)" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 452 | proof (rule admissible_fun[OF heap_interpretation]) | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 453 | fix x | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 454 | show "ccpo.admissible Heap_lub Heap_ord (\<lambda>a. \<forall>h h' r. effect a h h' r \<longrightarrow> P x h h' r)" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 455 | unfolding Heap_ord_def Heap_lub_def | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 456 | proof (intro admissible_image partial_function_lift flat_interpretation) | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 457 | show "option.admissible ((\<lambda>a. \<forall>h h' r. effect a h h' r \<longrightarrow> P x h h' r) \<circ> Heap)" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 458 | unfolding comp_def effect_def execute.simps | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 459 | by (rule heap_step_admissible) | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 460 | qed (auto simp add: Heap_eqI) | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 461 | qed | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 462 | |
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 463 | lemma fixp_induct_heap: | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 464 | fixes F :: "'c \<Rightarrow> 'c" and | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 465 | U :: "'c \<Rightarrow> 'b \<Rightarrow> 'a Heap" and | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 466 |     C :: "('b \<Rightarrow> 'a Heap) \<Rightarrow> 'c" and
 | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 467 | P :: "'b \<Rightarrow> heap \<Rightarrow> heap \<Rightarrow> 'a \<Rightarrow> bool" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 468 | assumes mono: "\<And>x. monotone (fun_ord Heap_ord) Heap_ord (\<lambda>f. U (F (C f)) x)" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 469 | assumes eq: "f \<equiv> C (ccpo.fixp (fun_lub Heap_lub) (fun_ord Heap_ord) (\<lambda>f. U (F (C f))))" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 470 | assumes inverse2: "\<And>f. U (C f) = f" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 471 | assumes step: "\<And>f x h h' r. (\<And>x h h' r. effect (U f x) h h' r \<Longrightarrow> P x h h' r) | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 472 | \<Longrightarrow> effect (U (F f) x) h h' r \<Longrightarrow> P x h h' r" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 473 | assumes defined: "effect (U f x) h h' r" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 474 | shows "P x h h' r" | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 475 | using step defined heap.fixp_induct_uc[of U F C, OF mono eq inverse2 admissible_heap, of P] | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 476 | by blast | 
| 
637aa1649ac7
added rudimentary induction rule for partial_function (heap)
 krauss parents: 
51143diff
changeset | 477 | |
| 42949 
618adb3584e5
separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
 krauss parents: 
41413diff
changeset | 478 | declaration {* Partial_Function.init "heap" @{term heap.fixp_fun}
 | 
| 52728 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52622diff
changeset | 479 |   @{term heap.mono_body} @{thm heap.fixp_rule_uc} @{thm heap.fixp_induct_uc}
 | 
| 
470b579f35d2
derive specialized version of full fixpoint induction (with admissibility)
 krauss parents: 
52622diff
changeset | 480 |   (SOME @{thm fixp_induct_heap}) *}
 | 
| 42949 
618adb3584e5
separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
 krauss parents: 
41413diff
changeset | 481 | |
| 
618adb3584e5
separate initializations for different modes of partial_function -- generation of induction rules will be non-uniform
 krauss parents: 
41413diff
changeset | 482 | |
| 40267 | 483 | abbreviation "mono_Heap \<equiv> monotone (fun_ord Heap_ord) Heap_ord" | 
| 484 | ||
| 485 | lemma Heap_ordI: | |
| 486 | assumes "\<And>h. execute x h = None \<or> execute x h = execute y h" | |
| 487 | shows "Heap_ord x y" | |
| 488 | using assms unfolding Heap_ord_def img_ord_def fun_ord_def flat_ord_def | |
| 489 | by blast | |
| 490 | ||
| 491 | lemma Heap_ordE: | |
| 492 | assumes "Heap_ord x y" | |
| 493 | obtains "execute x h = None" | "execute x h = execute y h" | |
| 494 | using assms unfolding Heap_ord_def img_ord_def fun_ord_def flat_ord_def | |
| 495 | by atomize_elim blast | |
| 496 | ||
| 46029 
4a19e3d147c3
attribute code_abbrev superseedes code_unfold_post; tuned names and spacing
 haftmann parents: 
45294diff
changeset | 497 | lemma bind_mono [partial_function_mono]: | 
| 40267 | 498 | assumes mf: "mono_Heap B" and mg: "\<And>y. mono_Heap (\<lambda>f. C y f)" | 
| 499 | shows "mono_Heap (\<lambda>f. B f \<guillemotright>= (\<lambda>y. C y f))" | |
| 500 | proof (rule monotoneI) | |
| 501 | fix f g :: "'a \<Rightarrow> 'b Heap" assume fg: "fun_ord Heap_ord f g" | |
| 502 | from mf | |
| 503 | have 1: "Heap_ord (B f) (B g)" by (rule monotoneD) (rule fg) | |
| 504 | from mg | |
| 505 | have 2: "\<And>y'. Heap_ord (C y' f) (C y' g)" by (rule monotoneD) (rule fg) | |
| 506 | ||
| 507 | have "Heap_ord (B f \<guillemotright>= (\<lambda>y. C y f)) (B g \<guillemotright>= (\<lambda>y. C y f))" | |
| 508 | (is "Heap_ord ?L ?R") | |
| 509 | proof (rule Heap_ordI) | |
| 510 | fix h | |
| 511 | from 1 show "execute ?L h = None \<or> execute ?L h = execute ?R h" | |
| 512 | by (rule Heap_ordE[where h = h]) (auto simp: execute_bind_case) | |
| 513 | qed | |
| 514 | also | |
| 515 | have "Heap_ord (B g \<guillemotright>= (\<lambda>y'. C y' f)) (B g \<guillemotright>= (\<lambda>y'. C y' g))" | |
| 516 | (is "Heap_ord ?L ?R") | |
| 517 | proof (rule Heap_ordI) | |
| 518 | fix h | |
| 519 | show "execute ?L h = None \<or> execute ?L h = execute ?R h" | |
| 520 | proof (cases "execute (B g) h") | |
| 521 | case None | |
| 522 | then have "execute ?L h = None" by (auto simp: execute_bind_case) | |
| 523 | thus ?thesis .. | |
| 524 | next | |
| 525 | case Some | |
| 526 | then obtain r h' where "execute (B g) h = Some (r, h')" | |
| 527 | by (metis surjective_pairing) | |
| 528 | then have "execute ?L h = execute (C r f) h'" | |
| 529 | "execute ?R h = execute (C r g) h'" | |
| 530 | by (auto simp: execute_bind_case) | |
| 531 | with 2[of r] show ?thesis by (auto elim: Heap_ordE) | |
| 532 | qed | |
| 533 | qed | |
| 534 | finally (heap.leq_trans) | |
| 535 | show "Heap_ord (B f \<guillemotright>= (\<lambda>y. C y f)) (B g \<guillemotright>= (\<lambda>y'. C y' g))" . | |
| 536 | qed | |
| 537 | ||
| 538 | ||
| 26182 | 539 | subsection {* Code generator setup *}
 | 
| 540 | ||
| 541 | subsubsection {* Logical intermediate layer *}
 | |
| 542 | ||
| 39250 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 543 | definition raise' :: "String.literal \<Rightarrow> 'a Heap" where | 
| 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 544 | [code del]: "raise' s = raise (explode s)" | 
| 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 545 | |
| 46029 
4a19e3d147c3
attribute code_abbrev superseedes code_unfold_post; tuned names and spacing
 haftmann parents: 
45294diff
changeset | 546 | lemma [code_abbrev]: "raise' (STR s) = raise s" | 
| 
4a19e3d147c3
attribute code_abbrev superseedes code_unfold_post; tuned names and spacing
 haftmann parents: 
45294diff
changeset | 547 | unfolding raise'_def by (simp add: STR_inverse) | 
| 26182 | 548 | |
| 46029 
4a19e3d147c3
attribute code_abbrev superseedes code_unfold_post; tuned names and spacing
 haftmann parents: 
45294diff
changeset | 549 | lemma raise_raise': (* FIXME delete candidate *) | 
| 37709 | 550 | "raise s = raise' (STR s)" | 
| 39250 
548a3e5521ab
changing String.literal to a type instead of a datatype
 bulwahn parents: 
39198diff
changeset | 551 | unfolding raise'_def by (simp add: STR_inverse) | 
| 26182 | 552 | |
| 37709 | 553 | code_datatype raise' -- {* avoid @{const "Heap"} formally *}
 | 
| 26182 | 554 | |
| 555 | ||
| 27707 | 556 | subsubsection {* SML and OCaml *}
 | 
| 26182 | 557 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 558 | code_printing type_constructor Heap \<rightharpoonup> (SML) "(unit/ ->/ _)" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 559 | code_printing constant bind \<rightharpoonup> (SML) "!(fn/ f'_/ =>/ fn/ ()/ =>/ f'_/ (_/ ())/ ())" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 560 | code_printing constant return \<rightharpoonup> (SML) "!(fn/ ()/ =>/ _)" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 561 | code_printing constant Heap_Monad.raise' \<rightharpoonup> (SML) "!(raise/ Fail/ _)" | 
| 26182 | 562 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 563 | code_printing type_constructor Heap \<rightharpoonup> (OCaml) "(unit/ ->/ _)" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 564 | code_printing constant bind \<rightharpoonup> (OCaml) "!(fun/ f'_/ ()/ ->/ f'_/ (_/ ())/ ())" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 565 | code_printing constant return \<rightharpoonup> (OCaml) "!(fun/ ()/ ->/ _)" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 566 | code_printing constant Heap_Monad.raise' \<rightharpoonup> (OCaml) "failwith" | 
| 27707 | 567 | |
| 37838 | 568 | |
| 569 | subsubsection {* Haskell *}
 | |
| 570 | ||
| 571 | text {* Adaption layer *}
 | |
| 572 | ||
| 573 | code_include Haskell "Heap" | |
| 574 | {*import qualified Control.Monad;
 | |
| 575 | import qualified Control.Monad.ST; | |
| 576 | import qualified Data.STRef; | |
| 577 | import qualified Data.Array.ST; | |
| 578 | ||
| 579 | type RealWorld = Control.Monad.ST.RealWorld; | |
| 580 | type ST s a = Control.Monad.ST.ST s a; | |
| 581 | type STRef s a = Data.STRef.STRef s a; | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 582 | type STArray s a = Data.Array.ST.STArray s Integer a; | 
| 37838 | 583 | |
| 584 | newSTRef = Data.STRef.newSTRef; | |
| 585 | readSTRef = Data.STRef.readSTRef; | |
| 586 | writeSTRef = Data.STRef.writeSTRef; | |
| 587 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 588 | newArray :: Integer -> a -> ST s (STArray s a); | 
| 37838 | 589 | newArray k = Data.Array.ST.newArray (0, k); | 
| 590 | ||
| 591 | newListArray :: [a] -> ST s (STArray s a); | |
| 37964 | 592 | newListArray xs = Data.Array.ST.newListArray (0, (fromInteger . toInteger . length) xs) xs; | 
| 37838 | 593 | |
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 594 | newFunArray :: Integer -> (Integer -> a) -> ST s (STArray s a); | 
| 37838 | 595 | newFunArray k f = Data.Array.ST.newListArray (0, k) (map f [0..k-1]); | 
| 596 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 597 | lengthArray :: STArray s a -> ST s Integer; | 
| 37838 | 598 | lengthArray a = Control.Monad.liftM snd (Data.Array.ST.getBounds a); | 
| 599 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 600 | readArray :: STArray s a -> Integer -> ST s a; | 
| 37838 | 601 | readArray = Data.Array.ST.readArray; | 
| 602 | ||
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 603 | writeArray :: STArray s a -> Integer -> a -> ST s (); | 
| 37838 | 604 | writeArray = Data.Array.ST.writeArray;*} | 
| 605 | ||
| 606 | code_reserved Haskell Heap | |
| 607 | ||
| 608 | text {* Monad *}
 | |
| 609 | ||
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 610 | code_printing type_constructor Heap \<rightharpoonup> (Haskell) "Heap.ST/ Heap.RealWorld/ _" | 
| 37838 | 611 | code_monad bind Haskell | 
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 612 | code_printing constant return \<rightharpoonup> (Haskell) "return" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 613 | code_printing constant Heap_Monad.raise' \<rightharpoonup> (Haskell) "error" | 
| 37838 | 614 | |
| 615 | ||
| 616 | subsubsection {* Scala *}
 | |
| 617 | ||
| 37842 | 618 | code_include Scala "Heap" | 
| 38968 
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
 haftmann parents: 
38773diff
changeset | 619 | {*object Heap {
 | 
| 
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
 haftmann parents: 
38773diff
changeset | 620 | 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: 
38773diff
changeset | 621 | } | 
| 37842 | 622 | |
| 623 | class Ref[A](x: A) {
 | |
| 624 | var value = x | |
| 625 | } | |
| 626 | ||
| 627 | object Ref {
 | |
| 38771 | 628 | def apply[A](x: A): Ref[A] = | 
| 629 | new Ref[A](x) | |
| 630 | def lookup[A](r: Ref[A]): A = | |
| 631 | r.value | |
| 632 | def update[A](r: Ref[A], x: A): Unit = | |
| 633 |     { r.value = x }
 | |
| 37842 | 634 | } | 
| 635 | ||
| 37964 | 636 | object Array {
 | 
| 38968 
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
 haftmann parents: 
38773diff
changeset | 637 | import collection.mutable.ArraySeq | 
| 51143 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 638 | def alloc[A](n: BigInt)(x: A): ArraySeq[A] = | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 639 | ArraySeq.fill(n.toInt)(x) | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 640 | def make[A](n: BigInt)(f: BigInt => A): ArraySeq[A] = | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 641 | ArraySeq.tabulate(n.toInt)((k: Int) => f(BigInt(k))) | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 642 | def len[A](a: ArraySeq[A]): BigInt = | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 643 | BigInt(a.length) | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 644 | def nth[A](a: ArraySeq[A], n: BigInt): A = | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 645 | a(n.toInt) | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 646 | def upd[A](a: ArraySeq[A], n: BigInt, x: A): Unit = | 
| 
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
 haftmann parents: 
48073diff
changeset | 647 | a.update(n.toInt, x) | 
| 38771 | 648 | def freeze[A](a: ArraySeq[A]): List[A] = | 
| 649 | a.toList | |
| 38968 
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
 haftmann parents: 
38773diff
changeset | 650 | } | 
| 
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
 haftmann parents: 
38773diff
changeset | 651 | *} | 
| 37842 | 652 | |
| 38968 
e55deaa22fff
do not print object frame around Scala includes -- this is in the responsibility of the user
 haftmann parents: 
38773diff
changeset | 653 | code_reserved Scala Heap Ref Array | 
| 37838 | 654 | |
| 52435 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 655 | code_printing type_constructor Heap \<rightharpoonup> (Scala) "(Unit/ =>/ _)" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 656 | code_printing constant bind \<rightharpoonup> (Scala) "Heap.bind" | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 657 | code_printing constant return \<rightharpoonup> (Scala) "('_: Unit)/ =>/ _"
 | 
| 
6646bb548c6b
migration from code_(const|type|class|instance) to code_printing and from code_module to code_identifier
 haftmann parents: 
52388diff
changeset | 658 | code_printing constant Heap_Monad.raise' \<rightharpoonup> (Scala) "!sys.error((_))" | 
| 37838 | 659 | |
| 660 | ||
| 661 | subsubsection {* Target variants with less units *}
 | |
| 662 | ||
| 31871 | 663 | setup {*
 | 
| 664 | ||
| 665 | let | |
| 27707 | 666 | |
| 31871 | 667 | open Code_Thingol; | 
| 668 | ||
| 669 | fun imp_program naming = | |
| 670 | let | |
| 671 | fun is_const c = case lookup_const naming c | |
| 672 | of SOME c' => (fn c'' => c' = c'') | |
| 673 | | 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: 
37754diff
changeset | 674 |     val is_bind = is_const @{const_name bind};
 | 
| 31871 | 675 |     val is_return = is_const @{const_name return};
 | 
| 31893 | 676 | val dummy_name = ""; | 
| 677 | val dummy_case_term = IVar NONE; | |
| 31871 | 678 | (*assumption: dummy values are not relevant for serialization*) | 
| 38057 | 679 |     val (unitt, unitT) = case lookup_const naming @{const_name Unity}
 | 
| 44794 
d3fdd0a24e15
adapting Imperative HOL serializer to changes of the iterm datatype in the code generator
 bulwahn parents: 
44174diff
changeset | 680 | of SOME unit' => | 
| 48072 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 681 | let | 
| 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 682 |             val unitT = the (lookup_tyco naming @{type_name unit}) `%% []
 | 
| 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 683 | in | 
| 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 684 |             (IConst { name = unit', typargs = [], dicts = [], dom = [],
 | 
| 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 685 | range = unitT, annotate = false }, unitT) | 
| 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 686 | end | 
| 31871 | 687 |       | NONE => error ("Must include " ^ @{const_name Unity} ^ " in generated constants.");
 | 
| 688 | fun dest_abs ((v, ty) `|=> t, _) = ((v, ty), t) | |
| 689 | | dest_abs (t, ty) = | |
| 690 | let | |
| 691 | val vs = fold_varnames cons t []; | |
| 43324 
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
 wenzelm parents: 
43080diff
changeset | 692 | val v = singleton (Name.variant_list vs) "x"; | 
| 31871 | 693 | val ty' = (hd o fst o unfold_fun) ty; | 
| 31893 | 694 | in ((SOME v, ty'), t `$ IVar (SOME v)) end; | 
| 48072 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 695 |     fun force (t as IConst { name = c, ... } `$ t') = if is_return c
 | 
| 31871 | 696 | then t' else t `$ unitt | 
| 697 | | force t = t `$ unitt; | |
| 38385 | 698 | fun tr_bind'' [(t1, _), (t2, ty2)] = | 
| 31871 | 699 | let | 
| 700 | val ((v, ty), t) = dest_abs (t2, ty2); | |
| 48072 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 701 |       in ICase { term = force t1, typ = ty, clauses = [(IVar v, tr_bind' t)], primitive = dummy_case_term } end
 | 
| 38385 | 702 | and tr_bind' t = case unfold_app t | 
| 48072 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 703 |      of (IConst { name = c, dom = ty1 :: ty2 :: _, ... }, [x1, x2]) => if is_bind c
 | 
| 38386 | 704 | then tr_bind'' [(x1, ty1), (x2, ty2)] | 
| 705 | else force t | |
| 706 | | _ => force t; | |
| 48072 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 707 | fun imp_monad_bind'' ts = (SOME dummy_name, unitT) `|=> | 
| 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 708 |       ICase { term = IVar (SOME dummy_name), typ = unitT, clauses = [(unitt, tr_bind'' ts)], primitive = dummy_case_term }
 | 
| 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 709 |     fun imp_monad_bind' (const as { name = c, dom = dom, ... }) ts = if is_bind c then case (ts, dom)
 | 
| 31871 | 710 | of ([t1, t2], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)] | 
| 711 | | ([t1, t2, t3], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)] `$ t3 | |
| 712 | | (ts, _) => imp_monad_bind (eta_expand 2 (const, ts)) | |
| 713 | else IConst const `$$ map imp_monad_bind ts | |
| 714 | and imp_monad_bind (IConst const) = imp_monad_bind' const [] | |
| 715 | | imp_monad_bind (t as IVar _) = t | |
| 716 | | imp_monad_bind (t as _ `$ _) = (case unfold_app t | |
| 717 | of (IConst const, ts) => imp_monad_bind' const ts | |
| 718 | | (t, ts) => imp_monad_bind t `$$ map imp_monad_bind ts) | |
| 719 | | imp_monad_bind (v_ty `|=> t) = v_ty `|=> imp_monad_bind t | |
| 48072 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 720 |       | imp_monad_bind (ICase { term = t, typ = ty, clauses = clauses, primitive = t0 }) =
 | 
| 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 721 |           ICase { term = imp_monad_bind t, typ = ty,
 | 
| 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 722 | clauses = (map o pairself) imp_monad_bind clauses, primitive = imp_monad_bind t0 }; | 
| 28663 
bd8438543bf2
code identifier namings are no longer imperative
 haftmann parents: 
28562diff
changeset | 723 | |
| 39021 | 724 | in (Graph.map o K o map_terms_stmt) imp_monad_bind end; | 
| 27707 | 725 | |
| 726 | in | |
| 727 | ||
| 31871 | 728 | Code_Target.extend_target ("SML_imp", ("SML", imp_program))
 | 
| 729 | #> Code_Target.extend_target ("OCaml_imp", ("OCaml", imp_program))
 | |
| 37838 | 730 | #> Code_Target.extend_target ("Scala_imp", ("Scala", imp_program))
 | 
| 27707 | 731 | |
| 732 | end | |
| 31871 | 733 | |
| 27707 | 734 | *} | 
| 735 | ||
| 37758 | 736 | hide_const (open) Heap heap guard raise' fold_map | 
| 37724 | 737 | |
| 26170 | 738 | end | 
| 48072 
ace701efe203
prefer records with speaking labels over deeply nested tuples
 haftmann parents: 
46029diff
changeset | 739 |