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