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