src/HOL/Imperative_HOL/Heap_Monad.thy
author haftmann
Mon, 19 Jul 2010 11:55:42 +0200
changeset 37878 d016aaead7a2
parent 37845 b70d7a347964
child 37947 844977c7abeb
permissions -rw-r--r--
dropped superfluous prefixes
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
37792
ba0bc31b90d7 Heap_Monad uses Monad_Syntax
krauss
parents: 37787
diff changeset
     8
imports Heap Monad_Syntax
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     9
begin
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    10
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    11
subsection {* The monad *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    12
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    13
subsubsection {* Monad construction *}
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    14
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    15
text {* Monadic heap actions either produce values
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    16
  and transform the heap, or fail *}
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    17
datatype 'a Heap = Heap "heap \<Rightarrow> ('a \<times> heap) option"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    18
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    19
primrec execute :: "'a Heap \<Rightarrow> heap \<Rightarrow> ('a \<times> heap) option" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    20
  [code del]: "execute (Heap f) = f"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    21
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    22
lemma Heap_cases [case_names succeed fail]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    23
  fixes f and h
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    24
  assumes succeed: "\<And>x h'. execute f h = Some (x, h') \<Longrightarrow> P"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    25
  assumes fail: "execute f h = None \<Longrightarrow> P"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    26
  shows P
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    27
  using assms by (cases "execute f h") auto
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    28
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    29
lemma Heap_execute [simp]:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    30
  "Heap (execute f) = f" by (cases f) simp_all
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    31
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    32
lemma Heap_eqI:
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    33
  "(\<And>h. execute f h = execute g h) \<Longrightarrow> f = g"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    34
    by (cases f, cases g) (auto simp: expand_fun_eq)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    35
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    36
ML {* structure Execute_Simps = Named_Thms(
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    37
  val name = "execute_simps"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    38
  val description = "simplification rules for execute"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    39
) *}
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    40
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    41
setup Execute_Simps.setup
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    42
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
    43
lemma execute_Let [execute_simps]:
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    44
  "execute (let x = t in f x) = (let x = t in execute (f x))"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    45
  by (simp add: Let_def)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    46
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    47
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    48
subsubsection {* Specialised lifters *}
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    49
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    50
definition tap :: "(heap \<Rightarrow> 'a) \<Rightarrow> 'a Heap" where
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    51
  [code del]: "tap f = Heap (\<lambda>h. Some (f h, h))"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    52
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
    53
lemma execute_tap [execute_simps]:
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    54
  "execute (tap f) h = Some (f h, h)"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    55
  by (simp add: tap_def)
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    56
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    57
definition heap :: "(heap \<Rightarrow> 'a \<times> heap) \<Rightarrow> 'a Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    58
  [code del]: "heap f = Heap (Some \<circ> f)"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    59
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
    60
lemma execute_heap [execute_simps]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
    61
  "execute (heap f) = Some \<circ> f"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    62
  by (simp add: heap_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
    63
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
    64
definition guard :: "(heap \<Rightarrow> bool) \<Rightarrow> (heap \<Rightarrow> 'a \<times> heap) \<Rightarrow> 'a Heap" where
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
    65
  [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
    66
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    67
lemma execute_guard [execute_simps]:
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
    68
  "\<not> P h \<Longrightarrow> execute (guard P f) h = None"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
    69
  "P h \<Longrightarrow> execute (guard P f) h = Some (f h)"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
    70
  by (simp_all add: guard_def)
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
    71
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    72
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    73
subsubsection {* Predicate classifying successful computations *}
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    74
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    75
definition success :: "'a Heap \<Rightarrow> heap \<Rightarrow> bool" where
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    76
  "success f h \<longleftrightarrow> execute f h \<noteq> None"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    77
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    78
lemma successI:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    79
  "execute f h \<noteq> None \<Longrightarrow> success f h"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    80
  by (simp add: success_def)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    81
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    82
lemma successE:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    83
  assumes "success f h"
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
    84
  obtains r h' where "r = fst (the (execute c h))"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
    85
    and "h' = snd (the (execute c h))"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
    86
    and "execute f h \<noteq> None"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
    87
  using assms by (simp add: success_def)
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    88
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    89
ML {* structure Success_Intros = Named_Thms(
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    90
  val name = "success_intros"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    91
  val description = "introduction rules for success"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    92
) *}
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    93
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    94
setup Success_Intros.setup
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    95
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
    96
lemma success_tapI [success_intros]:
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    97
  "success (tap f) h"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
    98
  by (rule successI) (simp add: execute_simps)
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    99
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   100
lemma success_heapI [success_intros]:
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   101
  "success (heap f) h"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   102
  by (rule successI) (simp add: execute_simps)
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   103
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   104
lemma success_guardI [success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   105
  "P h \<Longrightarrow> success (guard P f) h"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   106
  by (rule successI) (simp add: execute_guard)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   107
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   108
lemma success_LetI [success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   109
  "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
   110
  by (simp add: Let_def)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   111
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   112
lemma success_ifI:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   113
  "(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
   114
    success (if c then t else e) h"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   115
  by (simp add: success_def)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   116
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   117
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   118
subsubsection {* Predicate for a simple relational calculus *}
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   119
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   120
text {*
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   121
  The @{text crel} predicate states that when a computation @{text c}
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   122
  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
   123
  and a heap @{text "h'"}, i.e.~no exception occurs.
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   124
*}  
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
definition crel :: "'a Heap \<Rightarrow> heap \<Rightarrow> heap \<Rightarrow> 'a \<Rightarrow> bool" where
37878
d016aaead7a2 dropped superfluous prefixes
haftmann
parents: 37845
diff changeset
   127
  crel_def: "crel 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
   128
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   129
lemma crelI:
37878
d016aaead7a2 dropped superfluous prefixes
haftmann
parents: 37845
diff changeset
   130
  "execute c h = Some (r, h') \<Longrightarrow> crel c h h' r"
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   131
  by (simp add: crel_def)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   132
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   133
lemma crelE:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   134
  assumes "crel c h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   135
  obtains "r = fst (the (execute c h))"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   136
    and "h' = snd (the (execute c h))"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   137
    and "success c h"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   138
proof (rule that)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   139
  from assms have *: "execute c h = Some (r, h')" by (simp add: crel_def)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   140
  then show "success c h" by (simp add: success_def)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   141
  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
   142
    by simp_all
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   143
  then show "r = fst (the (execute c h))"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   144
    and "h' = snd (the (execute c h))" by simp_all
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   145
qed
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   146
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   147
lemma crel_success:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   148
  "crel c h h' r \<Longrightarrow> success c h"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   149
  by (simp add: crel_def success_def)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   150
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   151
lemma success_crelE:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   152
  assumes "success c h"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   153
  obtains r h' where "crel c h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   154
  using assms by (auto simp add: crel_def success_def)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   155
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   156
lemma crel_deterministic:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   157
  assumes "crel f h h' a"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   158
    and "crel f h h'' b"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   159
  shows "a = b" and "h' = h''"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   160
  using assms unfolding crel_def by auto
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   161
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   162
ML {* structure Crel_Intros = Named_Thms(
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   163
  val name = "crel_intros"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   164
  val description = "introduction rules for crel"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   165
) *}
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   166
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   167
ML {* structure Crel_Elims = Named_Thms(
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   168
  val name = "crel_elims"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   169
  val description = "elimination rules for crel"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   170
) *}
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   171
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   172
setup "Crel_Intros.setup #> Crel_Elims.setup"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   173
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   174
lemma crel_LetI [crel_intros]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   175
  assumes "x = t" "crel (f x) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   176
  shows "crel (let x = t in f x) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   177
  using assms by simp
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   178
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   179
lemma crel_LetE [crel_elims]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   180
  assumes "crel (let x = t in f x) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   181
  obtains "crel (f t) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   182
  using assms by simp
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   183
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   184
lemma crel_ifI:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   185
  assumes "c \<Longrightarrow> crel t h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   186
    and "\<not> c \<Longrightarrow> crel e h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   187
  shows "crel (if c then t else e) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   188
  by (cases c) (simp_all add: assms)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   189
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   190
lemma crel_ifE:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   191
  assumes "crel (if c then t else e) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   192
  obtains "c" "crel t h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   193
    | "\<not> c" "crel e h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   194
  using assms by (cases c) simp_all
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   195
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   196
lemma crel_tapI [crel_intros]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   197
  assumes "h' = h" "r = f h"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   198
  shows "crel (tap f) h h' r"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   199
  by (rule crelI) (simp add: assms execute_simps)
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   200
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   201
lemma crel_tapE [crel_elims]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   202
  assumes "crel (tap f) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   203
  obtains "h' = h" and "r = f h"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   204
  using assms by (rule crelE) (auto simp add: execute_simps)
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   205
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   206
lemma crel_heapI [crel_intros]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   207
  assumes "h' = snd (f h)" "r = fst (f h)"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   208
  shows "crel (heap f) h h' r"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   209
  by (rule crelI) (simp add: assms execute_simps)
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   210
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   211
lemma crel_heapE [crel_elims]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   212
  assumes "crel (heap f) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   213
  obtains "h' = snd (f h)" and "r = fst (f h)"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   214
  using assms by (rule crelE) (simp add: execute_simps)
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   215
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   216
lemma crel_guardI [crel_intros]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   217
  assumes "P h" "h' = snd (f h)" "r = fst (f h)"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   218
  shows "crel (guard P f) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   219
  by (rule crelI) (simp add: assms execute_simps)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   220
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   221
lemma crel_guardE [crel_elims]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   222
  assumes "crel (guard P f) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   223
  obtains "h' = snd (f h)" "r = fst (f h)" "P h"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   224
  using assms by (rule crelE)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   225
    (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
   226
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   227
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   228
subsubsection {* Monad combinators *}
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   229
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   230
definition return :: "'a \<Rightarrow> 'a Heap" where
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   231
  [code del]: "return x = heap (Pair x)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   232
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   233
lemma execute_return [execute_simps]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   234
  "execute (return x) = Some \<circ> Pair x"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   235
  by (simp add: return_def execute_simps)
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   236
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   237
lemma success_returnI [success_intros]:
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   238
  "success (return x) h"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   239
  by (rule successI) (simp add: execute_simps)
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   240
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   241
lemma crel_returnI [crel_intros]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   242
  "h = h' \<Longrightarrow> crel (return x) h h' x"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   243
  by (rule crelI) (simp add: execute_simps)
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   244
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   245
lemma crel_returnE [crel_elims]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   246
  assumes "crel (return x) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   247
  obtains "r = x" "h' = h"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   248
  using assms by (rule crelE) (simp add: execute_simps)
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   249
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   250
definition raise :: "string \<Rightarrow> 'a Heap" where -- {* the string is just decoration *}
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   251
  [code del]: "raise s = Heap (\<lambda>_. None)"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   252
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   253
lemma execute_raise [execute_simps]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   254
  "execute (raise s) = (\<lambda>_. None)"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   255
  by (simp add: raise_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   256
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   257
lemma crel_raiseE [crel_elims]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   258
  assumes "crel (raise x) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   259
  obtains "False"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   260
  using assms by (rule crelE) (simp add: success_def execute_simps)
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   261
37792
ba0bc31b90d7 Heap_Monad uses Monad_Syntax
krauss
parents: 37787
diff changeset
   262
definition bind :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" where
ba0bc31b90d7 Heap_Monad uses Monad_Syntax
krauss
parents: 37787
diff changeset
   263
  [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
   264
                  Some (x, h') \<Rightarrow> execute (g x) h'
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   265
                | None \<Rightarrow> None)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   266
37792
ba0bc31b90d7 Heap_Monad uses Monad_Syntax
krauss
parents: 37787
diff changeset
   267
setup {*
ba0bc31b90d7 Heap_Monad uses Monad_Syntax
krauss
parents: 37787
diff changeset
   268
  Adhoc_Overloading.add_variant 
37816
e550439d4422 dropped M suffix; added predicate monad bind
haftmann
parents: 37792
diff changeset
   269
    @{const_name Monad_Syntax.bind} @{const_name Heap_Monad.bind}
37792
ba0bc31b90d7 Heap_Monad uses Monad_Syntax
krauss
parents: 37787
diff changeset
   270
*}
ba0bc31b90d7 Heap_Monad uses Monad_Syntax
krauss
parents: 37787
diff changeset
   271
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   272
lemma execute_bind [execute_simps]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   273
  "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
   274
  "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
   275
  by (simp_all add: bind_def)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   276
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   277
lemma execute_bind_success:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   278
  "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
   279
  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
   280
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   281
lemma success_bind_executeI:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   282
  "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
   283
  by (auto intro!: successI elim!: successE simp add: bind_def)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   284
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   285
lemma success_bind_crelI [success_intros]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   286
  "crel f h h' x \<Longrightarrow> success (g x) h' \<Longrightarrow> success (f \<guillemotright>= g) h"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   287
  by (auto simp add: crel_def success_def bind_def)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   288
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   289
lemma crel_bindI [crel_intros]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   290
  assumes "crel f h h' r" "crel (g r) h' h'' r'"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   291
  shows "crel (f \<guillemotright>= g) h h'' r'"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   292
  using assms
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   293
  apply (auto intro!: crelI elim!: crelE successE)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   294
  apply (subst execute_bind, simp_all)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   295
  done
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 crel_bindE [crel_elims]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   298
  assumes "crel (f \<guillemotright>= g) h h'' r'"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   299
  obtains h' r where "crel f h h' r" "crel (g r) h' h'' r'"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   300
  using assms by (auto simp add: crel_def bind_def split: option.split_asm)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   301
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   302
lemma execute_bind_eq_SomeI:
37878
d016aaead7a2 dropped superfluous prefixes
haftmann
parents: 37845
diff changeset
   303
  assumes "execute f h = Some (x, h')"
d016aaead7a2 dropped superfluous prefixes
haftmann
parents: 37845
diff changeset
   304
    and "execute (g x) h' = Some (y, h'')"
d016aaead7a2 dropped superfluous prefixes
haftmann
parents: 37845
diff changeset
   305
  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
   306
  using assms by (simp add: bind_def)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   307
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   308
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
   309
  by (rule Heap_eqI) (simp add: execute_bind execute_simps)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   310
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   311
lemma bind_return [simp]: "f \<guillemotright>= return = f"
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   312
  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
   313
37828
9e1758c7ff06 avoid ambiguities; tuned
haftmann
parents: 37816
diff changeset
   314
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
   315
  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
   316
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   317
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
   318
  by (rule Heap_eqI) (simp add: execute_simps)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   319
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   320
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   321
subsection {* Generic combinators *}
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   322
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   323
subsubsection {* Assertions *}
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   324
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   325
definition assert :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   326
  "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
   327
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   328
lemma execute_assert [execute_simps]:
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   329
  "P x \<Longrightarrow> execute (assert P x) h = Some (x, h)"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   330
  "\<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
   331
  by (simp_all add: assert_def execute_simps)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   332
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   333
lemma success_assertI [success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   334
  "P x \<Longrightarrow> success (assert P x) h"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   335
  by (rule successI) (simp add: execute_assert)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   336
37771
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   337
lemma crel_assertI [crel_intros]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   338
  "P x \<Longrightarrow> h' = h \<Longrightarrow> r = x \<Longrightarrow> crel (assert P x) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   339
  by (rule crelI) (simp add: execute_assert)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   340
 
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   341
lemma crel_assertE [crel_elims]:
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   342
  assumes "crel (assert P x) h h' r"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   343
  obtains "P x" "r = x" "h' = h"
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   344
  using assms by (rule crelE) (cases "P x", simp_all add: execute_assert success_def)
1bec64044b5e spelt out relational framework in a consistent way
haftmann
parents: 37758
diff changeset
   345
28742
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   346
lemma assert_cong [fundef_cong]:
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   347
  assumes "P = P'"
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   348
  assumes "\<And>x. P' x \<Longrightarrow> f x = f' x"
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   349
  shows "(assert P x >>= f) = (assert P' x >>= f')"
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   350
  by (rule Heap_eqI) (insert assms, simp add: assert_def)
28742
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   351
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   352
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   353
subsubsection {* Plain lifting *}
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   354
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   355
definition lift :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b Heap" where
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   356
  "lift f = return o f"
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   357
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   358
lemma lift_collapse [simp]:
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   359
  "lift f x = return (f x)"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   360
  by (simp add: lift_def)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   361
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   362
lemma bind_lift:
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   363
  "(f \<guillemotright>= lift g) = (f \<guillemotright>= (\<lambda>x. return (g x)))"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   364
  by (simp add: lift_def comp_def)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   365
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   366
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   367
subsubsection {* Iteration -- warning: this is rarely useful! *}
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   368
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
   369
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
   370
  "fold_map f [] = return []"
37792
ba0bc31b90d7 Heap_Monad uses Monad_Syntax
krauss
parents: 37787
diff changeset
   371
| "fold_map f (x # xs) = do {
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   372
     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
   373
     ys \<leftarrow> fold_map f xs;
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   374
     return (y # ys)
37792
ba0bc31b90d7 Heap_Monad uses Monad_Syntax
krauss
parents: 37787
diff changeset
   375
   }"
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   376
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
   377
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
   378
  "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
   379
  by (induct xs) simp_all
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   380
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   381
lemma execute_fold_map_unchanged_heap [execute_simps]:
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   382
  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
   383
  shows "execute (fold_map f xs) h =
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   384
    Some (List.map (\<lambda>x. fst (the (execute (f x) h))) xs, h)"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   385
using assms proof (induct xs)
37787
30dc3abf4a58 theorem collections do not contain default rules any longer
haftmann
parents: 37772
diff changeset
   386
  case Nil show ?case by (simp add: execute_simps)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   387
next
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   388
  case (Cons x xs)
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   389
  from Cons.prems obtain y
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   390
    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
   391
  moreover from Cons.prems Cons.hyps have "execute (fold_map f xs) h =
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   392
    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
   393
  ultimately show ?case by (simp, simp only: execute_bind(1), simp add: execute_simps)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   394
qed
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   395
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   396
subsection {* Code generator setup *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   397
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   398
subsubsection {* Logical intermediate layer *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   399
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   400
primrec raise' :: "String.literal \<Rightarrow> 'a Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   401
  [code del, code_post]: "raise' (STR s) = raise s"
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   402
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   403
lemma raise_raise' [code_inline]:
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   404
  "raise s = raise' (STR s)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   405
  by simp
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   406
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   407
code_datatype raise' -- {* avoid @{const "Heap"} formally *}
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   408
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   409
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   410
subsubsection {* SML and OCaml *}
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   411
26752
6b276119139b corrected ML semantics
haftmann
parents: 26182
diff changeset
   412
code_type Heap (SML "unit/ ->/ _")
37828
9e1758c7ff06 avoid ambiguities; tuned
haftmann
parents: 37816
diff changeset
   413
code_const bind (SML "!(fn/ f'_/ =>/ fn/ ()/ =>/ f'_/ (_/ ())/ ())")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   414
code_const return (SML "!(fn/ ()/ =>/ _)")
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   415
code_const Heap_Monad.raise' (SML "!(raise/ Fail/ _)")
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   416
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   417
code_type Heap (OCaml "unit/ ->/ _")
37828
9e1758c7ff06 avoid ambiguities; tuned
haftmann
parents: 37816
diff changeset
   418
code_const bind (OCaml "!(fun/ f'_/ ()/ ->/ f'_/ (_/ ())/ ())")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   419
code_const return (OCaml "!(fun/ ()/ ->/ _)")
37828
9e1758c7ff06 avoid ambiguities; tuned
haftmann
parents: 37816
diff changeset
   420
code_const Heap_Monad.raise' (OCaml "failwith")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   421
37838
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   422
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   423
subsubsection {* Haskell *}
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   424
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   425
text {* Adaption layer *}
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   426
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   427
code_include Haskell "Heap"
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   428
{*import qualified Control.Monad;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   429
import qualified Control.Monad.ST;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   430
import qualified Data.STRef;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   431
import qualified Data.Array.ST;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   432
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   433
type RealWorld = Control.Monad.ST.RealWorld;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   434
type ST s a = Control.Monad.ST.ST s a;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   435
type STRef s a = Data.STRef.STRef s a;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   436
type STArray s a = Data.Array.ST.STArray s Int a;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   437
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   438
newSTRef = Data.STRef.newSTRef;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   439
readSTRef = Data.STRef.readSTRef;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   440
writeSTRef = Data.STRef.writeSTRef;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   441
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   442
newArray :: Int -> a -> ST s (STArray s a);
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   443
newArray k = Data.Array.ST.newArray (0, k);
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   444
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   445
newListArray :: [a] -> ST s (STArray s a);
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   446
newListArray xs = Data.Array.ST.newListArray (0, length xs) xs;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   447
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   448
newFunArray :: Int -> (Int -> a) -> ST s (STArray s a);
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   449
newFunArray k f = Data.Array.ST.newListArray (0, k) (map f [0..k-1]);
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   450
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   451
lengthArray :: STArray s a -> ST s Int;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   452
lengthArray a = Control.Monad.liftM snd (Data.Array.ST.getBounds a);
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   453
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   454
readArray :: STArray s a -> Int -> ST s a;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   455
readArray = Data.Array.ST.readArray;
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   456
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   457
writeArray :: STArray s a -> Int -> a -> ST s ();
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   458
writeArray = Data.Array.ST.writeArray;*}
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   459
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   460
code_reserved Haskell Heap
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   461
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   462
text {* Monad *}
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   463
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   464
code_type Heap (Haskell "Heap.ST/ Heap.RealWorld/ _")
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   465
code_monad bind Haskell
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   466
code_const return (Haskell "return")
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   467
code_const Heap_Monad.raise' (Haskell "error")
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   468
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   469
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   470
subsubsection {* Scala *}
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   471
37842
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   472
code_include Scala "Heap"
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   473
{*def bind[A, B](f: Unit => A, g: A => Unit => B): Unit => B = (_: Unit) => g (f ()) ()
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   474
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   475
class Ref[A](x: A) {
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   476
  var value = x
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   477
}
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   478
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   479
object Ref {
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   480
  def apply[A](x: A): Ref[A] = new Ref[A](x)
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   481
}
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   482
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   483
def lookup[A](r: Ref[A]): A = r.value
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   484
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   485
def update[A](r: Ref[A], x: A): Unit = { r.value = x }*}
37838
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   486
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   487
code_reserved Scala Heap
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   488
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   489
code_type Heap (Scala "Unit/ =>/ _")
37878
d016aaead7a2 dropped superfluous prefixes
haftmann
parents: 37845
diff changeset
   490
code_const bind (Scala "bind")
37842
27e7047d9ae6 a first sketch for Imperative HOL witht Scala
haftmann
parents: 37838
diff changeset
   491
code_const return (Scala "('_: Unit)/ =>/ _")
37845
b70d7a347964 first roughly working version of Imperative HOL for Scala
haftmann
parents: 37842
diff changeset
   492
code_const Heap_Monad.raise' (Scala "!error((_))")
37838
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   493
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   494
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   495
subsubsection {* Target variants with less units *}
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   496
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   497
setup {*
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   498
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   499
let
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   500
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   501
open Code_Thingol;
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   502
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   503
fun imp_program naming =
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   504
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   505
  let
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   506
    fun is_const c = case lookup_const naming c
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   507
     of SOME c' => (fn c'' => c' = c'')
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   508
      | 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
   509
    val is_bind = is_const @{const_name bind};
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   510
    val is_return = is_const @{const_name return};
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   511
    val dummy_name = "";
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   512
    val dummy_type = ITyVar dummy_name;
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   513
    val dummy_case_term = IVar NONE;
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   514
    (*assumption: dummy values are not relevant for serialization*)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   515
    val unitt = case lookup_const naming @{const_name Unity}
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   516
     of SOME unit' => IConst (unit', (([], []), []))
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   517
      | NONE => error ("Must include " ^ @{const_name Unity} ^ " in generated constants.");
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   518
    fun dest_abs ((v, ty) `|=> t, _) = ((v, ty), t)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   519
      | dest_abs (t, ty) =
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   520
          let
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   521
            val vs = fold_varnames cons t [];
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   522
            val v = Name.variant vs "x";
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   523
            val ty' = (hd o fst o unfold_fun) ty;
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   524
          in ((SOME v, ty'), t `$ IVar (SOME v)) end;
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   525
    fun force (t as IConst (c, _) `$ t') = if is_return c
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   526
          then t' else t `$ unitt
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   527
      | force t = t `$ unitt;
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   528
    fun tr_bind' [(t1, _), (t2, ty2)] =
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   529
      let
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   530
        val ((v, ty), t) = dest_abs (t2, ty2);
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   531
      in ICase (((force t1, ty), [(IVar v, tr_bind'' t)]), dummy_case_term) end
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   532
    and tr_bind'' t = case unfold_app t
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   533
         of (IConst (c, (_, ty1 :: ty2 :: _)), [x1, x2]) => if is_bind c
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   534
              then tr_bind' [(x1, ty1), (x2, ty2)]
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   535
              else force t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   536
          | _ => force t;
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   537
    fun imp_monad_bind'' ts = (SOME dummy_name, dummy_type) `|=> ICase (((IVar (SOME dummy_name), dummy_type),
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   538
      [(unitt, tr_bind' ts)]), dummy_case_term)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   539
    and 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
   540
       of ([t1, t2], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)]
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   541
        | ([t1, t2, t3], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)] `$ t3
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   542
        | (ts, _) => imp_monad_bind (eta_expand 2 (const, ts))
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   543
      else IConst const `$$ map imp_monad_bind ts
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   544
    and imp_monad_bind (IConst const) = imp_monad_bind' const []
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   545
      | imp_monad_bind (t as IVar _) = t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   546
      | imp_monad_bind (t as _ `$ _) = (case unfold_app t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   547
         of (IConst const, ts) => imp_monad_bind' const ts
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   548
          | (t, ts) => imp_monad_bind t `$$ map imp_monad_bind ts)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   549
      | imp_monad_bind (v_ty `|=> t) = v_ty `|=> imp_monad_bind t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   550
      | imp_monad_bind (ICase (((t, ty), pats), t0)) = ICase
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   551
          (((imp_monad_bind t, ty),
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   552
            (map o pairself) imp_monad_bind pats),
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   553
              imp_monad_bind t0);
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28562
diff changeset
   554
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   555
  in (Graph.map_nodes o map_terms_stmt) imp_monad_bind end;
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   556
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   557
in
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   558
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   559
Code_Target.extend_target ("SML_imp", ("SML", imp_program))
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   560
#> Code_Target.extend_target ("OCaml_imp", ("OCaml", imp_program))
37838
28848d338261 fragments of Scala
haftmann
parents: 37835
diff changeset
   561
#> Code_Target.extend_target ("Scala_imp", ("Scala", imp_program))
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   562
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   563
end
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   564
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   565
*}
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   566
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   567
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   568
hide_const (open) Heap heap guard raise' fold_map
37724
haftmann
parents: 37709
diff changeset
   569
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   570
end