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