src/HOL/Library/State_Monad.thy
author haftmann
Mon Nov 06 16:28:33 2006 +0100 (2006-11-06)
changeset 21192 5fe5cd5fede7
child 21283 b15355b9a59d
permissions -rw-r--r--
added state monad to HOL library
haftmann@21192
     1
(*  Title:      HOL/Library/State_Monad.thy
haftmann@21192
     2
    ID:         $Id$
haftmann@21192
     3
    Author:     Florian Haftmann, TU Muenchen
haftmann@21192
     4
*)
haftmann@21192
     5
haftmann@21192
     6
header {* Combinators syntax for generic, open state monads (single threaded monads) *}
haftmann@21192
     7
haftmann@21192
     8
theory State_Monad
haftmann@21192
     9
imports Main
haftmann@21192
    10
begin
haftmann@21192
    11
haftmann@21192
    12
section {* Generic, open state monads *}
haftmann@21192
    13
haftmann@21192
    14
subsection {* Motivation *}
haftmann@21192
    15
haftmann@21192
    16
text {*
haftmann@21192
    17
  The logic HOL has no notion of constructor classes, so
haftmann@21192
    18
  it is not possible to model monads the Haskell way
haftmann@21192
    19
  in full genericity in Isabelle/HOL.
haftmann@21192
    20
  
haftmann@21192
    21
  However, this theory provides substantial support for
haftmann@21192
    22
  a very common class of monads: \emph{state monads}
haftmann@21192
    23
  (or \emph{single-threaded monads}, since a state
haftmann@21192
    24
  is transformed single-threaded).
haftmann@21192
    25
haftmann@21192
    26
  To enter from the Haskell world,
haftmann@21192
    27
  \url{http://www.engr.mun.ca/~theo/Misc/haskell_and_monads.htm}
haftmann@21192
    28
  makes a good motivating start.  Here we just sketch briefly
haftmann@21192
    29
  how those monads enter the game of Isabelle/HOL.
haftmann@21192
    30
*}
haftmann@21192
    31
haftmann@21192
    32
subsection {* State transformations and combinators *}
haftmann@21192
    33
haftmann@21192
    34
(*<*)
haftmann@21192
    35
typedecl \<alpha>
haftmann@21192
    36
typedecl \<beta>
haftmann@21192
    37
typedecl \<gamma>
haftmann@21192
    38
typedecl \<sigma>
haftmann@21192
    39
typedecl \<sigma>'
haftmann@21192
    40
(*>*)
haftmann@21192
    41
haftmann@21192
    42
text {*
haftmann@21192
    43
  We classify functions operating on states into two categories:
haftmann@21192
    44
haftmann@21192
    45
  \begin{description}
haftmann@21192
    46
    \item[transformations]
haftmann@21192
    47
      with type signature @{typ "\<sigma> \<Rightarrow> \<sigma>'"},
haftmann@21192
    48
      transforming a state.
haftmann@21192
    49
    \item[``yielding'' transformations]
haftmann@21192
    50
      with type signature @{typ "\<sigma> \<Rightarrow> \<alpha> \<times> \<sigma>'"},
haftmann@21192
    51
      ``yielding'' a side result while transforming a state.
haftmann@21192
    52
    \item[queries]
haftmann@21192
    53
      with type signature @{typ "\<sigma> \<Rightarrow> \<alpha>"},
haftmann@21192
    54
      computing a result dependent on a state.
haftmann@21192
    55
  \end{description}
haftmann@21192
    56
haftmann@21192
    57
  By convention we write @{typ "\<sigma>"} for types representing states
haftmann@21192
    58
  and @{typ "\<alpha>"}, @{typ "\<beta>"}, @{typ "\<gamma>"}, @{text "\<dots>"}
haftmann@21192
    59
  for types representing side results.  Type changes due
haftmann@21192
    60
  to transformations are not excluded in our scenario.
haftmann@21192
    61
haftmann@21192
    62
  We aim to assert that values of any state type @{typ "\<sigma>"}
haftmann@21192
    63
  are used in a single-threaded way: after application
haftmann@21192
    64
  of a transformation on a value of type @{typ "\<sigma>"}, the
haftmann@21192
    65
  former value should not be used again.  To achieve this,
haftmann@21192
    66
  we use a set of monad combinators:
haftmann@21192
    67
*}
haftmann@21192
    68
haftmann@21192
    69
definition
haftmann@21192
    70
  mbind :: "('a \<Rightarrow> 'b \<times> 'c) \<Rightarrow> ('b \<Rightarrow> 'c \<Rightarrow> 'd) \<Rightarrow> 'a \<Rightarrow> 'd"
haftmann@21192
    71
    (infixl "\<guillemotright>=" 60)
haftmann@21192
    72
  "f \<guillemotright>= g = split g \<circ> f"
haftmann@21192
    73
  fcomp :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'c"
haftmann@21192
    74
    (infixl "\<guillemotright>" 60)
haftmann@21192
    75
  "f \<guillemotright> g = g \<circ> f"
haftmann@21192
    76
  run :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
haftmann@21192
    77
  "run f = f"
haftmann@21192
    78
haftmann@21192
    79
syntax (input)
haftmann@21192
    80
  mbind :: "('a \<Rightarrow> 'b \<times> 'c) \<Rightarrow> ('b \<Rightarrow> 'c \<Rightarrow> 'd) \<Rightarrow> 'a \<Rightarrow> 'd"
haftmann@21192
    81
    (infixl ">>=" 60)
haftmann@21192
    82
  fcomp :: "('a \<Rightarrow> 'b) \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> 'a \<Rightarrow> 'c"
haftmann@21192
    83
    (infixl ">>" 60)
haftmann@21192
    84
haftmann@21192
    85
abbreviation (input)
haftmann@21192
    86
  "return \<equiv> Pair"
haftmann@21192
    87
haftmann@21192
    88
text {*
haftmann@21192
    89
  Given two transformations @{term f} and @{term g}, they
haftmann@21192
    90
  may be directly composed using the @{term "op \<guillemotright>"} combinator,
haftmann@21192
    91
  forming a forward composition: @{prop "(f \<guillemotright> g) s = f (g s)"}.
haftmann@21192
    92
haftmann@21192
    93
  After any yielding transformation, we bind the side result
haftmann@21192
    94
  immediately using a lambda abstraction.  This 
haftmann@21192
    95
  is the purpose of the @{term "op \<guillemotright>="} combinator:
haftmann@21192
    96
  @{prop "(f \<guillemotright>= (\<lambda>x. g)) s = (let (x, s') = f s in g s')"}.
haftmann@21192
    97
haftmann@21192
    98
  For queries, the existing @{term "Let"} is appropriate.
haftmann@21192
    99
haftmann@21192
   100
  Naturally, a computation may yield a side result by pairing
haftmann@21192
   101
  it to the state from the left;  we introduce the
haftmann@21192
   102
  suggestive abbreviation @{term return} for this purpose.
haftmann@21192
   103
haftmann@21192
   104
  The @{const run} ist just a marker.
haftmann@21192
   105
haftmann@21192
   106
  The most crucial distinction to Haskell is that we do
haftmann@21192
   107
  not need to introduce distinguished type constructors
haftmann@21192
   108
  for different kinds of state.  This has two consequences:
haftmann@21192
   109
  \begin{itemize}
haftmann@21192
   110
    \item The monad model does not state anything about
haftmann@21192
   111
       the kind of state; the model for the state is
haftmann@21192
   112
       completely orthogonal and has (or may) be
haftmann@21192
   113
       specified completely independent.
haftmann@21192
   114
    \item There is no distinguished type constructor
haftmann@21192
   115
       encapsulating away the state transformation, i.e.~transformations
haftmann@21192
   116
       may be applied directly without using any lifting
haftmann@21192
   117
       or providing and dropping units (``open monad'').
haftmann@21192
   118
    \item The type of states may change due to a transformation.
haftmann@21192
   119
  \end{itemize}
haftmann@21192
   120
*}
haftmann@21192
   121
haftmann@21192
   122
haftmann@21192
   123
subsection {* Obsolete runs *}
haftmann@21192
   124
haftmann@21192
   125
text {*
haftmann@21192
   126
  @{term run} is just a doodle and should not occur nested:
haftmann@21192
   127
*}
haftmann@21192
   128
haftmann@21192
   129
lemma run_simp [simp]:
haftmann@21192
   130
  "\<And>f. run (run f) = run f"
haftmann@21192
   131
  "\<And>f g. run f \<guillemotright>= g = f \<guillemotright>= g"
haftmann@21192
   132
  "\<And>f g. run f \<guillemotright> g = f \<guillemotright> g"
haftmann@21192
   133
  "\<And>f g. f \<guillemotright>= (\<lambda>x. run g) = f \<guillemotright>= (\<lambda>x. g)"
haftmann@21192
   134
  "\<And>f g. f \<guillemotright> run g = f \<guillemotright> g"
haftmann@21192
   135
  "\<And>f. f = run f \<longleftrightarrow> True"
haftmann@21192
   136
  "\<And>f. run f = f \<longleftrightarrow> True"
haftmann@21192
   137
  unfolding run_def by rule+
haftmann@21192
   138
haftmann@21192
   139
haftmann@21192
   140
subsection {* Monad laws *}
haftmann@21192
   141
haftmann@21192
   142
text {*
haftmann@21192
   143
  The common monadic laws hold and may also be used
haftmann@21192
   144
  as normalization rules for monadic expressions:
haftmann@21192
   145
*}
haftmann@21192
   146
haftmann@21192
   147
lemma
haftmann@21192
   148
  return_mbind [simp]: "return x \<guillemotright>= f = f x"
haftmann@21192
   149
  unfolding mbind_def by (simp add: expand_fun_eq)
haftmann@21192
   150
haftmann@21192
   151
lemma
haftmann@21192
   152
  mbind_return [simp]: "x \<guillemotright>= return = x"
haftmann@21192
   153
  unfolding mbind_def by (simp add: expand_fun_eq split_Pair)
haftmann@21192
   154
haftmann@21192
   155
lemma
haftmann@21192
   156
  mbind_mbind [simp]: "(f \<guillemotright>= g) \<guillemotright>= h = f \<guillemotright>= (\<lambda>x. g x \<guillemotright>= h)"
haftmann@21192
   157
  unfolding mbind_def by (simp add: split_def expand_fun_eq)
haftmann@21192
   158
haftmann@21192
   159
lemma
haftmann@21192
   160
  mbind_fcomp [simp]: "(f \<guillemotright>= g) \<guillemotright> h = f \<guillemotright>= (\<lambda>x. g x \<guillemotright> h)"
haftmann@21192
   161
  unfolding mbind_def fcomp_def by (simp add: split_def expand_fun_eq)
haftmann@21192
   162
haftmann@21192
   163
lemma
haftmann@21192
   164
  fcomp_mbind [simp]: "(f \<guillemotright> g) \<guillemotright>= h = f \<guillemotright> (g \<guillemotright>= h)"
haftmann@21192
   165
  unfolding mbind_def fcomp_def by (simp add: split_def expand_fun_eq)
haftmann@21192
   166
haftmann@21192
   167
lemma
haftmann@21192
   168
  fcomp_fcomp [simp]: "(f \<guillemotright> g) \<guillemotright> h = f \<guillemotright> (g \<guillemotright> h)"
haftmann@21192
   169
  unfolding fcomp_def o_assoc ..
haftmann@21192
   170
haftmann@21192
   171
lemmas monad_simp = run_simp return_mbind mbind_return
haftmann@21192
   172
  mbind_mbind mbind_fcomp fcomp_mbind fcomp_fcomp
haftmann@21192
   173
haftmann@21192
   174
text {*
haftmann@21192
   175
  Evaluation of monadic expressions by force:
haftmann@21192
   176
*}
haftmann@21192
   177
haftmann@21192
   178
lemmas monad_collapse = monad_simp o_apply o_assoc split_Pair split_comp
haftmann@21192
   179
  mbind_def fcomp_def run_def
haftmann@21192
   180
haftmann@21192
   181
subsection {* Syntax *}
haftmann@21192
   182
haftmann@21192
   183
text {*
haftmann@21192
   184
  We provide a convenient do-notation for monadic expressions
haftmann@21192
   185
  well-known from Haskell.  @{const Let} is printed
haftmann@21192
   186
  specially in do-expressions.
haftmann@21192
   187
*}
haftmann@21192
   188
haftmann@21192
   189
nonterminals do_expr
haftmann@21192
   190
haftmann@21192
   191
syntax
haftmann@21192
   192
  "_do" :: "do_expr \<Rightarrow> 'a"
haftmann@21192
   193
    ("do _ done" [12] 12)
haftmann@21192
   194
  "_mbind" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
haftmann@21192
   195
    ("_ <- _;// _" [1000, 13, 12] 12)
haftmann@21192
   196
  "_fcomp" :: "'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
haftmann@21192
   197
    ("_;// _" [13, 12] 12)
haftmann@21192
   198
  "_let" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
haftmann@21192
   199
    ("let _ = _;// _" [1000, 13, 12] 12)
haftmann@21192
   200
  "_nil" :: "'a \<Rightarrow> do_expr"
haftmann@21192
   201
    ("_" [12] 12)
haftmann@21192
   202
haftmann@21192
   203
syntax (xsymbols)
haftmann@21192
   204
  "_mbind" :: "pttrn \<Rightarrow> 'a \<Rightarrow> do_expr \<Rightarrow> do_expr"
haftmann@21192
   205
    ("_ \<leftarrow> _;// _" [1000, 13, 12] 12)
haftmann@21192
   206
haftmann@21192
   207
translations
haftmann@21192
   208
  "_do f" => "State_Monad.run f"
haftmann@21192
   209
  "_mbind x f g" => "f \<guillemotright>= (\<lambda>x. g)"
haftmann@21192
   210
  "_fcomp f g" => "f \<guillemotright> g"
haftmann@21192
   211
  "_let x t f" => "Let t (\<lambda>x. f)"
haftmann@21192
   212
  "_nil f" => "f"
haftmann@21192
   213
haftmann@21192
   214
print_translation {*
haftmann@21192
   215
let
haftmann@21192
   216
  val syntax_name = Sign.const_syntax_name (the_context ());
haftmann@21192
   217
  val name_mbind = syntax_name "State_Monad.mbind";
haftmann@21192
   218
  val name_fcomp = syntax_name "State_Monad.fcomp";
haftmann@21192
   219
  fun unfold_monad (t as Const (name, _) $ f $ g) =
haftmann@21192
   220
        if name = name_mbind then let
haftmann@21192
   221
            val ([(v, ty)], g') = Term.strip_abs_eta 1 g;
haftmann@21192
   222
          in Const ("_mbind", dummyT) $ Free (v, ty) $ f $ unfold_monad g' end
haftmann@21192
   223
        else if name = name_fcomp then
haftmann@21192
   224
          Const ("_fcomp", dummyT) $ f $ unfold_monad g
haftmann@21192
   225
        else t
haftmann@21192
   226
    | unfold_monad (Const ("Let", _) $ f $ g) =
haftmann@21192
   227
        let
haftmann@21192
   228
          val ([(v, ty)], g') = Term.strip_abs_eta 1 g;
haftmann@21192
   229
        in Const ("_let", dummyT) $ Free (v, ty) $ f $ unfold_monad g' end
haftmann@21192
   230
    | unfold_monad (Const ("Pair", _) $ f) =
haftmann@21192
   231
        Const ("return", dummyT) $ f
haftmann@21192
   232
    | unfold_monad f = f;
haftmann@21192
   233
  fun tr' (f::ts) =
haftmann@21192
   234
    list_comb (Const ("_do", dummyT) $ unfold_monad f, ts)
haftmann@21192
   235
in [
haftmann@21192
   236
  (syntax_name "State_Monad.run", tr')
haftmann@21192
   237
] end;
haftmann@21192
   238
*}
haftmann@21192
   239
haftmann@21192
   240
print_ast_translation {*[
haftmann@21192
   241
  (Sign.const_syntax_name (the_context ()) "State_Monad.run", fn (f::ts) => Syntax.mk_appl f ts)
haftmann@21192
   242
]*}
haftmann@21192
   243
haftmann@21192
   244
text {*
haftmann@21192
   245
  For an example, see HOL/ex/CodeRandom.thy (more examples coming soon).
haftmann@21192
   246
*}
haftmann@21192
   247
haftmann@21192
   248
end