src/HOL/Library/Heap_Monad.thy
author haftmann
Wed Feb 27 21:41:08 2008 +0100 (2008-02-27)
changeset 26170 66e6b967ccf1
child 26182 8262ec0e8782
permissions -rw-r--r--
added theories for imperative HOL
haftmann@26170
     1
(*  Title:      HOL/Library/Heap_Monad.thy
haftmann@26170
     2
    ID:         $Id$
haftmann@26170
     3
    Author:     John Matthews, Galois Connections; Alexander Krauss, Lukas Bulwahn & Florian Haftmann, TU Muenchen
haftmann@26170
     4
*)
haftmann@26170
     5
haftmann@26170
     6
header {* A monad with a polymorphic heap *}
haftmann@26170
     7
haftmann@26170
     8
theory Heap_Monad
haftmann@26170
     9
imports Heap
haftmann@26170
    10
begin
haftmann@26170
    11
haftmann@26170
    12
subsection {* The monad *}
haftmann@26170
    13
haftmann@26170
    14
subsubsection {* Monad combinators *}
haftmann@26170
    15
haftmann@26170
    16
datatype exception = Exn
haftmann@26170
    17
haftmann@26170
    18
text {* Monadic heap actions either produce values
haftmann@26170
    19
  and transform the heap, or fail *}
haftmann@26170
    20
datatype 'a Heap = Heap "heap \<Rightarrow> ('a + exception) \<times> heap"
haftmann@26170
    21
haftmann@26170
    22
primrec
haftmann@26170
    23
  execute :: "'a Heap \<Rightarrow> heap \<Rightarrow> ('a + exception) \<times> heap" where
haftmann@26170
    24
  "execute (Heap f) = f"
haftmann@26170
    25
lemmas [code del] = execute.simps
haftmann@26170
    26
haftmann@26170
    27
lemma Heap_execute [simp]:
haftmann@26170
    28
  "Heap (execute f) = f" by (cases f) simp_all
haftmann@26170
    29
haftmann@26170
    30
lemma Heap_eqI:
haftmann@26170
    31
  "(\<And>h. execute f h = execute g h) \<Longrightarrow> f = g"
haftmann@26170
    32
    by (cases f, cases g) (auto simp: expand_fun_eq)
haftmann@26170
    33
haftmann@26170
    34
lemma Heap_eqI':
haftmann@26170
    35
  "(\<And>h. (\<lambda>x. execute (f x) h) = (\<lambda>y. execute (g y) h)) \<Longrightarrow> f = g"
haftmann@26170
    36
    by (auto simp: expand_fun_eq intro: Heap_eqI)
haftmann@26170
    37
haftmann@26170
    38
lemma Heap_strip: "(\<And>f. PROP P f) \<equiv> (\<And>g. PROP P (Heap g))"
haftmann@26170
    39
proof
haftmann@26170
    40
  fix g :: "heap \<Rightarrow> ('a + exception) \<times> heap" 
haftmann@26170
    41
  assume "\<And>f. PROP P f"
haftmann@26170
    42
  then show "PROP P (Heap g)" .
haftmann@26170
    43
next
haftmann@26170
    44
  fix f :: "'a Heap" 
haftmann@26170
    45
  assume assm: "\<And>g. PROP P (Heap g)"
haftmann@26170
    46
  then have "PROP P (Heap (execute f))" .
haftmann@26170
    47
  then show "PROP P f" by simp
haftmann@26170
    48
qed
haftmann@26170
    49
haftmann@26170
    50
definition
haftmann@26170
    51
  heap :: "(heap \<Rightarrow> 'a \<times> heap) \<Rightarrow> 'a Heap" where
haftmann@26170
    52
  [code del]: "heap f = Heap (\<lambda>h. apfst Inl (f h))"
haftmann@26170
    53
haftmann@26170
    54
lemma execute_heap [simp]:
haftmann@26170
    55
  "execute (heap f) h = apfst Inl (f h)"
haftmann@26170
    56
  by (simp add: heap_def)
haftmann@26170
    57
haftmann@26170
    58
definition
haftmann@26170
    59
  run :: "'a Heap \<Rightarrow> 'a Heap" where
haftmann@26170
    60
  run_drop [code del]: "run f = f"
haftmann@26170
    61
haftmann@26170
    62
definition
haftmann@26170
    63
  bindM :: "'a Heap \<Rightarrow> ('a \<Rightarrow> 'b Heap) \<Rightarrow> 'b Heap" (infixl ">>=" 54) where
haftmann@26170
    64
  [code del]: "f >>= g = Heap (\<lambda>h. case execute f h of
haftmann@26170
    65
                  (Inl x, h') \<Rightarrow> execute (g x) h'
haftmann@26170
    66
                | r \<Rightarrow> r)"
haftmann@26170
    67
haftmann@26170
    68
notation
haftmann@26170
    69
  bindM (infixl "\<guillemotright>=" 54)
haftmann@26170
    70
haftmann@26170
    71
abbreviation
haftmann@26170
    72
  chainM :: "'a Heap \<Rightarrow> 'b Heap \<Rightarrow> 'b Heap"  (infixl ">>" 54) where
haftmann@26170
    73
  "f >> g \<equiv> f >>= (\<lambda>_. g)"
haftmann@26170
    74
haftmann@26170
    75
notation
haftmann@26170
    76
  chainM (infixl "\<guillemotright>" 54)
haftmann@26170
    77
haftmann@26170
    78
definition
haftmann@26170
    79
  return :: "'a \<Rightarrow> 'a Heap" where
haftmann@26170
    80
  [code del]: "return x = heap (Pair x)"
haftmann@26170
    81
haftmann@26170
    82
lemma execute_return [simp]:
haftmann@26170
    83
  "execute (return x) h = apfst Inl (x, h)"
haftmann@26170
    84
  by (simp add: return_def)
haftmann@26170
    85
haftmann@26170
    86
definition
haftmann@26170
    87
  raise :: "string \<Rightarrow> 'a Heap" where -- {* the string is just decoration *}
haftmann@26170
    88
  [code del]: "raise s = Heap (Pair (Inr Exn))"
haftmann@26170
    89
haftmann@26170
    90
notation (latex output)
haftmann@26170
    91
  "raise" ("\<^raw:{\textsf{raise}}>")
haftmann@26170
    92
haftmann@26170
    93
lemma execute_raise [simp]:
haftmann@26170
    94
  "execute (raise s) h = (Inr Exn, h)"
haftmann@26170
    95
  by (simp add: raise_def)
haftmann@26170
    96
haftmann@26170
    97
haftmann@26170
    98
subsubsection {* do-syntax *}
haftmann@26170
    99
haftmann@26170
   100
text {*
haftmann@26170
   101
  We provide a convenient do-notation for monadic expressions
haftmann@26170
   102
  well-known from Haskell.  @{const Let} is printed
haftmann@26170
   103
  specially in do-expressions.
haftmann@26170
   104
*}
haftmann@26170
   105
haftmann@26170
   106
nonterminals do_expr
haftmann@26170
   107
haftmann@26170
   108
syntax
haftmann@26170
   109
  "_do" :: "do_expr \<Rightarrow> 'a"
haftmann@26170
   110
    ("(do (_)//done)" [12] 100)
haftmann@26170
   111
  "_bindM" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
haftmann@26170
   112
    ("_ <- _;//_" [1000, 13, 12] 12)
haftmann@26170
   113
  "_chainM" :: "'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
haftmann@26170
   114
    ("_;//_" [13, 12] 12)
haftmann@26170
   115
  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
haftmann@26170
   116
    ("let _ = _;//_" [1000, 13, 12] 12)
haftmann@26170
   117
  "_nil" :: "'a \<Rightarrow> do_expr"
haftmann@26170
   118
    ("_" [12] 12)
haftmann@26170
   119
haftmann@26170
   120
syntax (xsymbols)
haftmann@26170
   121
  "_bindM" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
haftmann@26170
   122
    ("_ \<leftarrow> _;//_" [1000, 13, 12] 12)
haftmann@26170
   123
syntax (latex output)
haftmann@26170
   124
  "_do" :: "do_expr \<Rightarrow> 'a"
haftmann@26170
   125
    ("(\<^raw:{\textsf{do}}> (_))" [12] 100)
haftmann@26170
   126
  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
haftmann@26170
   127
    ("\<^raw:\textsf{let}> _ = _;//_" [1000, 13, 12] 12)
haftmann@26170
   128
notation (latex output)
haftmann@26170
   129
  "return" ("\<^raw:{\textsf{return}}>")
haftmann@26170
   130
haftmann@26170
   131
translations
haftmann@26170
   132
  "_do f" => "CONST run f"
haftmann@26170
   133
  "_bindM x f g" => "f \<guillemotright>= (\<lambda>x. g)"
haftmann@26170
   134
  "_chainM f g" => "f \<guillemotright> g"
haftmann@26170
   135
  "_let x t f" => "CONST Let t (\<lambda>x. f)"
haftmann@26170
   136
  "_nil f" => "f"
haftmann@26170
   137
haftmann@26170
   138
print_translation {*
haftmann@26170
   139
let
haftmann@26170
   140
  fun dest_abs_eta (Abs (abs as (_, ty, _))) =
haftmann@26170
   141
        let
haftmann@26170
   142
          val (v, t) = Syntax.variant_abs abs;
haftmann@26170
   143
        in ((v, ty), t) end
haftmann@26170
   144
    | dest_abs_eta t =
haftmann@26170
   145
        let
haftmann@26170
   146
          val (v, t) = Syntax.variant_abs ("", dummyT, t $ Bound 0);
haftmann@26170
   147
        in ((v, dummyT), t) end
haftmann@26170
   148
  fun unfold_monad (Const (@{const_syntax bindM}, _) $ f $ g) =
haftmann@26170
   149
        let
haftmann@26170
   150
          val ((v, ty), g') = dest_abs_eta g;
haftmann@26170
   151
          val v_used = fold_aterms
haftmann@26170
   152
            (fn Free (w, _) => (fn s => s orelse v = w) | _ => I) g' false;
haftmann@26170
   153
        in if v_used then
haftmann@26170
   154
          Const ("_bindM", dummyT) $ Free (v, ty) $ f $ unfold_monad g'
haftmann@26170
   155
        else
haftmann@26170
   156
          Const ("_chainM", dummyT) $ f $ unfold_monad g'
haftmann@26170
   157
        end
haftmann@26170
   158
    | unfold_monad (Const (@{const_syntax chainM}, _) $ f $ g) =
haftmann@26170
   159
        Const ("_chainM", dummyT) $ f $ unfold_monad g
haftmann@26170
   160
    | unfold_monad (Const (@{const_syntax Let}, _) $ f $ g) =
haftmann@26170
   161
        let
haftmann@26170
   162
          val ((v, ty), g') = dest_abs_eta g;
haftmann@26170
   163
        in Const ("_let", dummyT) $ Free (v, ty) $ f $ unfold_monad g' end
haftmann@26170
   164
    | unfold_monad (Const (@{const_syntax Pair}, _) $ f) =
haftmann@26170
   165
        Const ("return", dummyT) $ f
haftmann@26170
   166
    | unfold_monad f = f;
haftmann@26170
   167
  fun tr' (f::ts) =
haftmann@26170
   168
    list_comb (Const ("_do", dummyT) $ unfold_monad f, ts)
haftmann@26170
   169
in [(@{const_syntax "run"}, tr')] end;
haftmann@26170
   170
*}
haftmann@26170
   171
haftmann@26170
   172
subsubsection {* Plain evaluation *}
haftmann@26170
   173
haftmann@26170
   174
definition
haftmann@26170
   175
  evaluate :: "'a Heap \<Rightarrow> 'a"
haftmann@26170
   176
where
haftmann@26170
   177
  [code del]: "evaluate f = (case execute f Heap.empty
haftmann@26170
   178
    of (Inl x, _) \<Rightarrow> x)"
haftmann@26170
   179
haftmann@26170
   180
haftmann@26170
   181
subsection {* Monad properties *}
haftmann@26170
   182
haftmann@26170
   183
subsubsection {* Superfluous runs *}
haftmann@26170
   184
haftmann@26170
   185
text {* @{term run} is just a doodle *}
haftmann@26170
   186
haftmann@26170
   187
lemma run_simp [simp]:
haftmann@26170
   188
  "\<And>f. run (run f) = run f"
haftmann@26170
   189
  "\<And>f g. run f \<guillemotright>= g = f \<guillemotright>= g"
haftmann@26170
   190
  "\<And>f g. run f \<guillemotright> g = f \<guillemotright> g"
haftmann@26170
   191
  "\<And>f g. f \<guillemotright>= (\<lambda>x. run g) = f \<guillemotright>= (\<lambda>x. g)"
haftmann@26170
   192
  "\<And>f g. f \<guillemotright> run g = f \<guillemotright> g"
haftmann@26170
   193
  "\<And>f. f = run g \<longleftrightarrow> f = g"
haftmann@26170
   194
  "\<And>f. run f = g \<longleftrightarrow> f = g"
haftmann@26170
   195
  unfolding run_drop by rule+
haftmann@26170
   196
haftmann@26170
   197
subsubsection {* Monad laws *}
haftmann@26170
   198
haftmann@26170
   199
lemma return_bind: "return x \<guillemotright>= f = f x"
haftmann@26170
   200
  by (simp add: bindM_def return_def)
haftmann@26170
   201
haftmann@26170
   202
lemma bind_return: "f \<guillemotright>= return = f"
haftmann@26170
   203
proof (rule Heap_eqI)
haftmann@26170
   204
  fix h
haftmann@26170
   205
  show "execute (f \<guillemotright>= return) h = execute f h"
haftmann@26170
   206
    by (auto simp add: bindM_def return_def split: sum.splits prod.splits)
haftmann@26170
   207
qed
haftmann@26170
   208
haftmann@26170
   209
lemma bind_bind: "(f \<guillemotright>= g) \<guillemotright>= h = f \<guillemotright>= (\<lambda>x. g x \<guillemotright>= h)"
haftmann@26170
   210
  by (rule Heap_eqI) (auto simp add: bindM_def split: split: sum.splits prod.splits)
haftmann@26170
   211
haftmann@26170
   212
lemma bind_bind': "f \<guillemotright>= (\<lambda>x. g x \<guillemotright>= h x) = f \<guillemotright>= (\<lambda>x. g x \<guillemotright>= (\<lambda>y. return (x, y))) \<guillemotright>= (\<lambda>(x, y). h x y)"
haftmann@26170
   213
  by (rule Heap_eqI) (auto simp add: bindM_def split: split: sum.splits prod.splits)
haftmann@26170
   214
haftmann@26170
   215
lemma raise_bind: "raise e \<guillemotright>= f = raise e"
haftmann@26170
   216
  by (simp add: raise_def bindM_def)
haftmann@26170
   217
haftmann@26170
   218
haftmann@26170
   219
lemmas monad_simp = return_bind bind_return bind_bind raise_bind
haftmann@26170
   220
haftmann@26170
   221
haftmann@26170
   222
subsection {* Generic combinators *}
haftmann@26170
   223
haftmann@26170
   224
definition
haftmann@26170
   225
  liftM :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b Heap"
haftmann@26170
   226
where
haftmann@26170
   227
  "liftM f = return o f"
haftmann@26170
   228
haftmann@26170
   229
definition
haftmann@26170
   230
  compM :: "('a \<Rightarrow> 'b Heap) \<Rightarrow> ('b \<Rightarrow> 'c Heap) \<Rightarrow> 'a \<Rightarrow> 'c Heap" (infixl ">>==" 54)
haftmann@26170
   231
where
haftmann@26170
   232
  "(f >>== g) = (\<lambda>x. f x \<guillemotright>= g)"
haftmann@26170
   233
haftmann@26170
   234
notation
haftmann@26170
   235
  compM (infixl "\<guillemotright>==" 54)
haftmann@26170
   236
haftmann@26170
   237
lemma liftM_collapse: "liftM f x = return (f x)"
haftmann@26170
   238
  by (simp add: liftM_def)
haftmann@26170
   239
haftmann@26170
   240
lemma liftM_compM: "liftM f \<guillemotright>== g = g o f"
haftmann@26170
   241
  by (auto intro: Heap_eqI' simp add: expand_fun_eq liftM_def compM_def bindM_def)
haftmann@26170
   242
haftmann@26170
   243
lemma compM_return: "f \<guillemotright>== return = f"
haftmann@26170
   244
  by (simp add: compM_def monad_simp)
haftmann@26170
   245
haftmann@26170
   246
lemma compM_compM: "(f \<guillemotright>== g) \<guillemotright>== h = f \<guillemotright>== (g \<guillemotright>== h)"
haftmann@26170
   247
  by (simp add: compM_def monad_simp)
haftmann@26170
   248
haftmann@26170
   249
lemma liftM_bind:
haftmann@26170
   250
  "(\<lambda>x. liftM f x \<guillemotright>= liftM g) = liftM (\<lambda>x. g (f x))"
haftmann@26170
   251
  by (rule Heap_eqI') (simp add: monad_simp liftM_def bindM_def)
haftmann@26170
   252
haftmann@26170
   253
lemma liftM_comp:
haftmann@26170
   254
  "liftM f o g = liftM (f o g)"
haftmann@26170
   255
  by (rule Heap_eqI') (simp add: liftM_def)
haftmann@26170
   256
haftmann@26170
   257
lemmas monad_simp' = monad_simp liftM_compM compM_return
haftmann@26170
   258
  compM_compM liftM_bind liftM_comp
haftmann@26170
   259
haftmann@26170
   260
primrec 
haftmann@26170
   261
  mapM :: "('a \<Rightarrow> 'b Heap) \<Rightarrow> 'a list \<Rightarrow> 'b list Heap"
haftmann@26170
   262
where
haftmann@26170
   263
  "mapM f [] = return []"
haftmann@26170
   264
  | "mapM f (x#xs) = do y \<leftarrow> f x;
haftmann@26170
   265
                        ys \<leftarrow> mapM f xs;
haftmann@26170
   266
                        return (y # ys)
haftmann@26170
   267
                     done"
haftmann@26170
   268
haftmann@26170
   269
primrec
haftmann@26170
   270
  foldM :: "('a \<Rightarrow> 'b \<Rightarrow> 'b Heap) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'b Heap"
haftmann@26170
   271
where
haftmann@26170
   272
  "foldM f [] s = return s"
haftmann@26170
   273
  | "foldM f (x#xs) s = f x s \<guillemotright>= foldM f xs"
haftmann@26170
   274
haftmann@26170
   275
hide (open) const heap execute
haftmann@26170
   276
haftmann@26170
   277
end