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