src/HOL/Imperative_HOL/Heap_Monad.thy
author haftmann
Fri, 09 Jul 2010 16:58:44 +0200
changeset 37758 bf86a65403a8
parent 37756 59caa6180fff
child 37771 1bec64044b5e
permissions -rw-r--r--
pervasive success combinator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Heap_Monad.thy
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
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     5
header {* A monad with a polymorphic heap *}
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
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
     8
imports Heap
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
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    43
lemma execute_Let [simp, execute_simps]:
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
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    53
lemma execute_tap [simp, execute_simps]:
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
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    60
lemma execute_heap [simp, 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"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    84
  obtains r h' where "execute f h = Some (r, h')"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    85
  using assms by (auto simp add: success_def)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    86
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    87
ML {* structure Success_Intros = Named_Thms(
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    88
  val name = "success_intros"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    89
  val description = "introduction rules for success"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    90
) *}
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    91
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    92
setup Success_Intros.setup
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    93
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    94
lemma success_tapI [iff, success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    95
  "success (tap f) h"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    96
  by (rule successI) simp
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    97
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    98
lemma success_heapI [iff, success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
    99
  "success (heap f) h"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   100
  by (rule successI) simp
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   101
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   102
lemma success_guardI [success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   103
  "P h \<Longrightarrow> success (guard P f) h"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   104
  by (rule successI) (simp add: execute_guard)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   105
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   106
lemma success_LetI [success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   107
  "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
   108
  by (simp add: Let_def)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   109
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   110
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   111
subsubsection {* Monad combinators *}
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   112
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   113
definition return :: "'a \<Rightarrow> 'a Heap" where
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   114
  [code del]: "return x = heap (Pair x)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   115
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   116
lemma execute_return [simp, execute_simps]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   117
  "execute (return x) = Some \<circ> Pair x"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   118
  by (simp add: return_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   119
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   120
lemma success_returnI [iff, success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   121
  "success (return x) h"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   122
  by (rule successI) simp
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   123
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   124
definition raise :: "string \<Rightarrow> 'a Heap" where -- {* the string is just decoration *}
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   125
  [code del]: "raise s = Heap (\<lambda>_. None)"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   126
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   127
lemma execute_raise [simp, execute_simps]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   128
  "execute (raise s) = (\<lambda>_. None)"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   129
  by (simp add: raise_def)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   130
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
   131
definition bind :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" (infixl ">>=" 54) where
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   132
  [code del]: "f >>= g = Heap (\<lambda>h. case execute f h of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   133
                  Some (x, h') \<Rightarrow> execute (g x) h'
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   134
                | None \<Rightarrow> None)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   135
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
   136
notation bind (infixl "\<guillemotright>=" 54)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   137
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   138
lemma execute_bind [execute_simps]:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   139
  "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
   140
  "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
   141
  by (simp_all add: bind_def)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   142
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   143
lemma success_bindI [success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   144
  "success f h \<Longrightarrow> success (g (fst (the (execute f h)))) (snd (the (execute f h))) \<Longrightarrow> success (f \<guillemotright>= g) h"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   145
  by (auto intro!: successI elim!: successE simp add: bind_def)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   146
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   147
lemma execute_bind_successI [execute_simps]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   148
  "success f h \<Longrightarrow> execute (f \<guillemotright>= g) h = execute (g (fst (the (execute f h)))) (snd (the (execute f h)))"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   149
  by (cases f h rule: Heap_cases) (auto elim!: successE simp add: bind_def)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   150
  
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   151
lemma execute_eq_SomeI:
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   152
  assumes "Heap_Monad.execute f h = Some (x, h')"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   153
    and "Heap_Monad.execute (g x) h' = Some (y, h'')"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   154
  shows "Heap_Monad.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
   155
  using assms by (simp add: bind_def)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   156
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   157
lemma return_bind [simp]: "return x \<guillemotright>= f = f x"
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   158
  by (rule Heap_eqI) (simp add: execute_bind)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   159
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   160
lemma bind_return [simp]: "f \<guillemotright>= return = f"
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
   161
  by (rule Heap_eqI) (simp add: bind_def split: option.splits)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   162
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   163
lemma bind_bind [simp]: "(f \<guillemotright>= g) \<guillemotright>= k = f \<guillemotright>= (\<lambda>x. g x \<guillemotright>= k)"
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
   164
  by (rule Heap_eqI) (simp add: bind_def split: option.splits)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   165
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   166
lemma raise_bind [simp]: "raise e \<guillemotright>= f = raise e"
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   167
  by (rule Heap_eqI) (simp add: execute_bind)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   168
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   169
abbreviation chain :: "'a Heap \<Rightarrow> 'b Heap \<Rightarrow> 'b Heap"  (infixl ">>" 54) where
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   170
  "f >> g \<equiv> f >>= (\<lambda>_. g)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   171
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   172
notation chain (infixl "\<guillemotright>" 54)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   173
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   174
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   175
subsubsection {* do-syntax *}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   176
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   177
text {*
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   178
  We provide a convenient do-notation for monadic expressions
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   179
  well-known from Haskell.  @{const Let} is printed
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   180
  specially in do-expressions.
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   181
*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   182
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   183
nonterminals do_expr
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   184
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   185
syntax
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   186
  "_do" :: "do_expr \<Rightarrow> 'a"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   187
    ("(do (_)//done)" [12] 100)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   188
  "_bind" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   189
    ("_ <- _;//_" [1000, 13, 12] 12)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   190
  "_chain" :: "'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   191
    ("_;//_" [13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   192
  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   193
    ("let _ = _;//_" [1000, 13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   194
  "_nil" :: "'a \<Rightarrow> do_expr"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   195
    ("_" [12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   196
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   197
syntax (xsymbols)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   198
  "_bind" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   199
    ("_ \<leftarrow> _;//_" [1000, 13, 12] 12)
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   200
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   201
translations
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   202
  "_do f" => "f"
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   203
  "_bind x f g" => "f \<guillemotright>= (\<lambda>x. g)"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   204
  "_chain f g" => "f \<guillemotright> g"
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   205
  "_let x t f" => "CONST Let t (\<lambda>x. f)"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   206
  "_nil f" => "f"
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   207
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   208
print_translation {*
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   209
let
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   210
  fun dest_abs_eta (Abs (abs as (_, ty, _))) =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   211
        let
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   212
          val (v, t) = Syntax.variant_abs abs;
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   213
        in (Free (v, ty), t) end
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   214
    | dest_abs_eta t =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   215
        let
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   216
          val (v, t) = Syntax.variant_abs ("", dummyT, t $ Bound 0);
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   217
        in (Free (v, dummyT), t) end;
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
   218
  fun unfold_monad (Const (@{const_syntax bind}, _) $ f $ g) =
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   219
        let
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   220
          val (v, g') = dest_abs_eta g;
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   221
          val vs = fold_aterms (fn Free (v, _) => insert (op =) v | _ => I) v [];
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   222
          val v_used = fold_aterms
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   223
            (fn Free (w, _) => (fn s => s orelse member (op =) vs w) | _ => I) g' false;
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   224
        in if v_used then
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   225
          Const (@{syntax_const "_bind"}, dummyT) $ v $ f $ unfold_monad g'
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   226
        else
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   227
          Const (@{syntax_const "_chain"}, dummyT) $ f $ unfold_monad g'
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   228
        end
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   229
    | unfold_monad (Const (@{const_syntax chain}, _) $ f $ g) =
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   230
        Const (@{syntax_const "_chain"}, dummyT) $ f $ unfold_monad g
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   231
    | unfold_monad (Const (@{const_syntax Let}, _) $ f $ g) =
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   232
        let
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   233
          val (v, g') = dest_abs_eta g;
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   234
        in Const (@{syntax_const "_let"}, dummyT) $ v $ f $ unfold_monad g' end
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   235
    | unfold_monad (Const (@{const_syntax Pair}, _) $ f) =
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   236
        Const (@{const_syntax return}, dummyT) $ f
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   237
    | unfold_monad f = f;
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
   238
  fun contains_bind (Const (@{const_syntax bind}, _) $ _ $ _) = true
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   239
    | contains_bind (Const (@{const_syntax Let}, _) $ _ $ Abs (_, _, t)) =
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   240
        contains_bind t;
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
   241
  fun bind_monad_tr' (f::g::ts) = list_comb
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   242
    (Const (@{syntax_const "_do"}, dummyT) $
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
   243
      unfold_monad (Const (@{const_syntax bind}, dummyT) $ f $ g), ts);
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   244
  fun Let_monad_tr' (f :: (g as Abs (_, _, g')) :: ts) =
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   245
    if contains_bind g' then list_comb
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   246
      (Const (@{syntax_const "_do"}, dummyT) $
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   247
        unfold_monad (Const (@{const_syntax Let}, dummyT) $ f $ g), ts)
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   248
    else raise Match;
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   249
in
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
   250
 [(@{const_syntax bind}, bind_monad_tr'),
35113
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   251
  (@{const_syntax Let}, Let_monad_tr')]
1a0c129bb2e0 modernized translations;
wenzelm
parents: 34051
diff changeset
   252
end;
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   253
*}
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   254
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   255
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   256
subsection {* Generic combinators *}
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   257
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   258
subsubsection {* Assertions *}
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   259
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   260
definition assert :: "('a \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'a Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   261
  "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
   262
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   263
lemma execute_assert [execute_simps]:
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   264
  "P x \<Longrightarrow> execute (assert P x) h = Some (x, h)"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   265
  "\<not> P x \<Longrightarrow> execute (assert P x) h = None"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   266
  by (simp_all add: assert_def)
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   267
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   268
lemma success_assertI [success_intros]:
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   269
  "P x \<Longrightarrow> success (assert P x) h"
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   270
  by (rule successI) (simp add: execute_assert)
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   271
28742
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   272
lemma assert_cong [fundef_cong]:
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   273
  assumes "P = P'"
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   274
  assumes "\<And>x. P' x \<Longrightarrow> f x = f' x"
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   275
  shows "(assert P x >>= f) = (assert P' x >>= f')"
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   276
  by (rule Heap_eqI) (insert assms, simp add: assert_def)
28742
07073b1087dd moved assert to Heap_Monad.thy
haftmann
parents: 28663
diff changeset
   277
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   278
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   279
subsubsection {* Plain lifting *}
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   280
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   281
definition lift :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b Heap" where
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   282
  "lift f = return o f"
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   283
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   284
lemma lift_collapse [simp]:
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   285
  "lift f x = return (f x)"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   286
  by (simp add: lift_def)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   287
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   288
lemma bind_lift:
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   289
  "(f \<guillemotright>= lift g) = (f \<guillemotright>= (\<lambda>x. return (g x)))"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   290
  by (simp add: lift_def comp_def)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   291
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   292
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   293
subsubsection {* Iteration -- warning: this is rarely useful! *}
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   294
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
   295
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
   296
  "fold_map f [] = return []"
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37754
diff changeset
   297
| "fold_map f (x # xs) = do
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   298
     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
   299
     ys \<leftarrow> fold_map f xs;
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   300
     return (y # ys)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   301
   done"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   302
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
   303
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
   304
  "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
   305
  by (induct xs) simp_all
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   306
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   307
lemma execute_fold_map_unchanged_heap [execute_simps]:
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   308
  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
   309
  shows "execute (fold_map f xs) h =
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   310
    Some (List.map (\<lambda>x. fst (the (execute (f x) h))) xs, h)"
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   311
using assms proof (induct xs)
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   312
  case Nil show ?case by simp
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   313
next
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   314
  case (Cons x xs)
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   315
  from Cons.prems obtain y
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   316
    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
   317
  moreover from Cons.prems Cons.hyps have "execute (fold_map f xs) h =
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   318
    Some (map (\<lambda>x. fst (the (execute (f x) h))) xs, h)" by auto
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   319
  ultimately show ?case by (simp, simp only: execute_bind(1), simp)
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   320
qed
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   321
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   322
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   323
subsubsection {* A monadic combinator for simple recursive functions *}
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   324
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   325
text {* Using a locale to fix arguments f and g of MREC *}
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   326
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   327
locale mrec =
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   328
  fixes f :: "'a \<Rightarrow> ('b + 'a) Heap"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   329
  and g :: "'a \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'b Heap"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   330
begin
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   331
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   332
function (default "\<lambda>(x, h). None") mrec :: "'a \<Rightarrow> heap \<Rightarrow> ('b \<times> heap) option" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   333
  "mrec x h = (case execute (f x) h of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   334
     Some (Inl r, h') \<Rightarrow> Some (r, h')
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   335
   | Some (Inr s, h') \<Rightarrow> (case mrec s h' of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   336
             Some (z, h'') \<Rightarrow> execute (g x s z) h''
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   337
           | None \<Rightarrow> None)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   338
   | None \<Rightarrow> None)"
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   339
by auto
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   340
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   341
lemma graph_implies_dom:
35423
6ef9525a5727 eliminated hard tabs;
wenzelm
parents: 35113
diff changeset
   342
  "mrec_graph x y \<Longrightarrow> mrec_dom x"
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   343
apply (induct rule:mrec_graph.induct) 
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   344
apply (rule accpI)
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   345
apply (erule mrec_rel.cases)
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   346
by simp
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   347
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   348
lemma mrec_default: "\<not> mrec_dom (x, h) \<Longrightarrow> mrec x h = None"
35423
6ef9525a5727 eliminated hard tabs;
wenzelm
parents: 35113
diff changeset
   349
  unfolding mrec_def 
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   350
  by (rule fundef_default_value[OF mrec_sumC_def graph_implies_dom, of _ _ "(x, h)", simplified])
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   351
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   352
lemma mrec_di_reverse: 
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   353
  assumes "\<not> mrec_dom (x, h)"
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   354
  shows "
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   355
   (case execute (f x) h of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   356
     Some (Inl r, h') \<Rightarrow> False
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   357
   | Some (Inr s, h') \<Rightarrow> \<not> mrec_dom (s, h')
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   358
   | None \<Rightarrow> False
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   359
   )" 
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   360
using assms apply (auto split: option.split sum.split)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   361
apply (rule ccontr)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   362
apply (erule notE, rule accpI, elim mrec_rel.cases, auto)+
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   363
done
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   364
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   365
lemma mrec_rule:
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   366
  "mrec x h = 
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   367
   (case execute (f x) h of
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   368
     Some (Inl r, h') \<Rightarrow> Some (r, h')
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   369
   | Some (Inr s, h') \<Rightarrow> 
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   370
          (case mrec s h' of
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   371
             Some (z, h'') \<Rightarrow> execute (g x s z) h''
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   372
           | None \<Rightarrow> None)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   373
   | None \<Rightarrow> None
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   374
   )"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   375
apply (cases "mrec_dom (x,h)", simp)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   376
apply (frule mrec_default)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   377
apply (frule mrec_di_reverse, simp)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   378
by (auto split: sum.split option.split simp: mrec_default)
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   379
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   380
definition
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   381
  "MREC x = Heap (mrec x)"
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   382
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   383
lemma MREC_rule:
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   384
  "MREC x = 
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   385
  (do y \<leftarrow> f x;
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   386
                (case y of 
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   387
                Inl r \<Rightarrow> return r
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   388
              | Inr s \<Rightarrow> 
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   389
                do z \<leftarrow> MREC s ;
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   390
                   g x s z
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   391
                done) done)"
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   392
  unfolding MREC_def
37756
59caa6180fff avoid slightly odd "M" suffix; rename mapM to fold_map (fold_map_abort would be more correct, though)
haftmann
parents: 37754
diff changeset
   393
  unfolding bind_def return_def
34051
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   394
  apply simp
1a82e2e29d67 added Imperative_HOL examples; added tail-recursive combinator for monadic heap functions; adopted code generation of references; added lemmas
bulwahn
parents: 32069
diff changeset
   395
  apply (rule ext)
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   396
  apply (unfold mrec_rule[of x])
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   397
  by (auto split: option.splits prod.splits sum.splits)
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   398
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   399
lemma MREC_pinduct:
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   400
  assumes "execute (MREC x) h = Some (r, h')"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   401
  assumes non_rec_case: "\<And> x h h' r. execute (f x) h = Some (Inl r, h') \<Longrightarrow> P x h h' r"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   402
  assumes rec_case: "\<And> x h h1 h2 h' s z r. execute (f x) h = Some (Inr s, h1) \<Longrightarrow> execute (MREC s) h1 = Some (z, h2) \<Longrightarrow> P s h1 h2 z
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   403
    \<Longrightarrow> execute (g x s z) h2 = Some (r, h') \<Longrightarrow> P x h h' r"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   404
  shows "P x h h' r"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   405
proof -
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   406
  from assms(1) have mrec: "mrec x h = Some (r, h')"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   407
    unfolding MREC_def execute.simps .
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   408
  from mrec have dom: "mrec_dom (x, h)"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   409
    apply -
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   410
    apply (rule ccontr)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   411
    apply (drule mrec_default) by auto
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   412
  from mrec have h'_r: "h' = snd (the (mrec x h))" "r = fst (the (mrec x h))"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   413
    by auto
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   414
  from mrec have "P x h (snd (the (mrec x h))) (fst (the (mrec x h)))"
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   415
  proof (induct arbitrary: r h' rule: mrec.pinduct[OF dom])
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   416
    case (1 x h)
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   417
    obtain rr h' where "the (mrec x h) = (rr, h')" by fastsimp
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   418
    show ?case
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   419
    proof (cases "execute (f x) h")
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   420
      case (Some result)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   421
      then obtain a h1 where exec_f: "execute (f x) h = Some (a, h1)" by fastsimp
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   422
      note Inl' = this
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   423
      show ?thesis
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   424
      proof (cases a)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   425
        case (Inl aa)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   426
        from this Inl' 1(1) exec_f mrec non_rec_case show ?thesis
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   427
          by auto
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   428
      next
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   429
        case (Inr b)
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   430
        note Inr' = this
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   431
        show ?thesis
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   432
        proof (cases "mrec b h1")
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   433
          case (Some result)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   434
          then obtain aaa h2 where mrec_rec: "mrec b h1 = Some (aaa, h2)" by fastsimp
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   435
          moreover from this have "P b h1 (snd (the (mrec b h1))) (fst (the (mrec b h1)))"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   436
            apply (intro 1(2))
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   437
            apply (auto simp add: Inr Inl')
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   438
            done
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   439
          moreover note mrec mrec_rec exec_f Inl' Inr' 1(1) 1(3)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   440
          ultimately show ?thesis
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   441
            apply auto
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   442
            apply (rule rec_case)
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   443
            apply auto
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   444
            unfolding MREC_def by auto
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   445
        next
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   446
          case None
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   447
          from this 1(1) exec_f mrec Inr' 1(3) show ?thesis by auto
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   448
        qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   449
      qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   450
    next
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   451
      case None
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   452
      from this 1(1) mrec 1(3) show ?thesis by simp
36057
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   453
    qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   454
  qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   455
  from this h'_r show ?thesis by simp
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   456
qed
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   457
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   458
end
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   459
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   460
text {* Providing global versions of the constant and the theorems *}
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   461
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   462
abbreviation "MREC == mrec.MREC"
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   463
lemmas MREC_rule = mrec.MREC_rule
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   464
lemmas MREC_pinduct = mrec.MREC_pinduct
ca6610908ae9 adding MREC induction rule in Imperative HOL
bulwahn
parents: 35423
diff changeset
   465
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   466
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   467
subsection {* Code generator setup *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   468
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   469
subsubsection {* Logical intermediate layer *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   470
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   471
primrec raise' :: "String.literal \<Rightarrow> 'a Heap" where
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   472
  [code del, code_post]: "raise' (STR s) = raise s"
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   473
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   474
lemma raise_raise' [code_inline]:
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   475
  "raise s = raise' (STR s)"
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   476
  by simp
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   477
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   478
code_datatype raise' -- {* avoid @{const "Heap"} formally *}
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   479
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   480
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   481
subsubsection {* SML and OCaml *}
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   482
26752
6b276119139b corrected ML semantics
haftmann
parents: 26182
diff changeset
   483
code_type Heap (SML "unit/ ->/ _")
27826
4e50590ea9bc changed code setup
haftmann
parents: 27707
diff changeset
   484
code_const "op \<guillemotright>=" (SML "!(fn/ f'_/ =>/ fn/ ()/ =>/ f'_/ (_/ ())/ ())")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   485
code_const return (SML "!(fn/ ()/ =>/ _)")
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   486
code_const Heap_Monad.raise' (SML "!(raise/ Fail/ _)")
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   487
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   488
code_type Heap (OCaml "unit/ ->/ _")
27826
4e50590ea9bc changed code setup
haftmann
parents: 27707
diff changeset
   489
code_const "op \<guillemotright>=" (OCaml "!(fun/ f'_/ ()/ ->/ f'_/ (_/ ())/ ())")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   490
code_const return (OCaml "!(fun/ ()/ ->/ _)")
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   491
code_const Heap_Monad.raise' (OCaml "failwith/ _")
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   492
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   493
setup {*
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   494
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   495
let
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   496
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   497
open Code_Thingol;
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   498
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   499
fun imp_program naming =
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   500
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   501
  let
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   502
    fun is_const c = case lookup_const naming c
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   503
     of SOME c' => (fn c'' => c' = c'')
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   504
      | 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
   505
    val is_bind = is_const @{const_name bind};
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   506
    val is_return = is_const @{const_name return};
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   507
    val dummy_name = "";
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   508
    val dummy_type = ITyVar dummy_name;
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   509
    val dummy_case_term = IVar NONE;
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   510
    (*assumption: dummy values are not relevant for serialization*)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   511
    val unitt = case lookup_const naming @{const_name Unity}
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   512
     of SOME unit' => IConst (unit', (([], []), []))
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   513
      | NONE => error ("Must include " ^ @{const_name Unity} ^ " in generated constants.");
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   514
    fun dest_abs ((v, ty) `|=> t, _) = ((v, ty), t)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   515
      | dest_abs (t, ty) =
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   516
          let
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   517
            val vs = fold_varnames cons t [];
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   518
            val v = Name.variant vs "x";
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   519
            val ty' = (hd o fst o unfold_fun) ty;
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   520
          in ((SOME v, ty'), t `$ IVar (SOME v)) end;
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   521
    fun force (t as IConst (c, _) `$ t') = if is_return c
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   522
          then t' else t `$ unitt
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   523
      | force t = t `$ unitt;
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   524
    fun tr_bind' [(t1, _), (t2, ty2)] =
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   525
      let
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   526
        val ((v, ty), t) = dest_abs (t2, ty2);
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   527
      in ICase (((force t1, ty), [(IVar v, tr_bind'' t)]), dummy_case_term) end
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   528
    and tr_bind'' t = case unfold_app t
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   529
         of (IConst (c, (_, ty1 :: ty2 :: _)), [x1, x2]) => if is_bind c
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   530
              then tr_bind' [(x1, ty1), (x2, ty2)]
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   531
              else force t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   532
          | _ => force t;
31893
7d8a89390cbf adaptated to changes in term representation
haftmann
parents: 31871
diff changeset
   533
    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
   534
      [(unitt, tr_bind' ts)]), dummy_case_term)
37754
683d1e1bc234 guard combinator
haftmann
parents: 37724
diff changeset
   535
    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
   536
       of ([t1, t2], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)]
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   537
        | ([t1, t2, t3], ty1 :: ty2 :: _) => imp_monad_bind'' [(t1, ty1), (t2, ty2)] `$ t3
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   538
        | (ts, _) => imp_monad_bind (eta_expand 2 (const, ts))
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   539
      else IConst const `$$ map imp_monad_bind ts
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   540
    and imp_monad_bind (IConst const) = imp_monad_bind' const []
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   541
      | imp_monad_bind (t as IVar _) = t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   542
      | imp_monad_bind (t as _ `$ _) = (case unfold_app t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   543
         of (IConst const, ts) => imp_monad_bind' const ts
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   544
          | (t, ts) => imp_monad_bind t `$$ map imp_monad_bind ts)
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   545
      | imp_monad_bind (v_ty `|=> t) = v_ty `|=> imp_monad_bind t
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   546
      | imp_monad_bind (ICase (((t, ty), pats), t0)) = ICase
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   547
          (((imp_monad_bind t, ty),
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   548
            (map o pairself) imp_monad_bind pats),
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   549
              imp_monad_bind t0);
28663
bd8438543bf2 code identifier namings are no longer imperative
haftmann
parents: 28562
diff changeset
   550
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   551
  in (Graph.map_nodes o map_terms_stmt) imp_monad_bind end;
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   552
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   553
in
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   554
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   555
Code_Target.extend_target ("SML_imp", ("SML", imp_program))
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   556
#> Code_Target.extend_target ("OCaml_imp", ("OCaml", imp_program))
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   557
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   558
end
31871
cc1486840914 streamlined code
haftmann
parents: 31724
diff changeset
   559
27707
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   560
*}
54bf1fea9252 SML_imp, OCaml_imp
haftmann
parents: 27695
diff changeset
   561
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   562
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   563
subsubsection {* Haskell *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   564
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   565
text {* Adaption layer *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   566
29793
86cac1fab613 changed name space policy for Haskell includes
haftmann
parents: 29399
diff changeset
   567
code_include Haskell "Heap"
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   568
{*import qualified Control.Monad;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   569
import qualified Control.Monad.ST;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   570
import qualified Data.STRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   571
import qualified Data.Array.ST;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   572
27695
033732c90ebd Haskell now living in the RealWorld
haftmann
parents: 27673
diff changeset
   573
type RealWorld = Control.Monad.ST.RealWorld;
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   574
type ST s a = Control.Monad.ST.ST s a;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   575
type STRef s a = Data.STRef.STRef s a;
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   576
type STArray s a = Data.Array.ST.STArray s Int a;
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   577
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   578
newSTRef = Data.STRef.newSTRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   579
readSTRef = Data.STRef.readSTRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   580
writeSTRef = Data.STRef.writeSTRef;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   581
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   582
newArray :: (Int, Int) -> a -> ST s (STArray s a);
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   583
newArray = Data.Array.ST.newArray;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   584
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   585
newListArray :: (Int, Int) -> [a] -> ST s (STArray s a);
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   586
newListArray = Data.Array.ST.newListArray;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   587
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   588
lengthArray :: STArray s a -> ST s Int;
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   589
lengthArray a = Control.Monad.liftM snd (Data.Array.ST.getBounds a);
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   590
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   591
readArray :: STArray s a -> Int -> ST s a;
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   592
readArray = Data.Array.ST.readArray;
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   593
27673
52056ddac194 fixed code generator setup
haftmann
parents: 26753
diff changeset
   594
writeArray :: STArray s a -> Int -> a -> ST s ();
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   595
writeArray = Data.Array.ST.writeArray;*}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   596
29793
86cac1fab613 changed name space policy for Haskell includes
haftmann
parents: 29399
diff changeset
   597
code_reserved Haskell Heap
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   598
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   599
text {* Monad *}
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   600
29793
86cac1fab613 changed name space policy for Haskell includes
haftmann
parents: 29399
diff changeset
   601
code_type Heap (Haskell "Heap.ST/ Heap.RealWorld/ _")
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28054
diff changeset
   602
code_monad "op \<guillemotright>=" Haskell
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   603
code_const return (Haskell "return")
37709
70fafefbcc98 simplified representation of monad type
haftmann
parents: 37591
diff changeset
   604
code_const Heap_Monad.raise' (Haskell "error/ _")
26182
8262ec0e8782 added code generator setup
haftmann
parents: 26170
diff changeset
   605
37758
bf86a65403a8 pervasive success combinator
haftmann
parents: 37756
diff changeset
   606
hide_const (open) Heap heap guard raise' fold_map
37724
haftmann
parents: 37709
diff changeset
   607
26170
66e6b967ccf1 added theories for imperative HOL
haftmann
parents:
diff changeset
   608
end