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