src/HOL/Library/State_Monad.thy
changeset 26260 23ce0d32de11
parent 26141 e1b3a6953cdc
child 26266 35ae83ca190a
equal deleted inserted replaced
26259:d30f4a509361 26260:23ce0d32de11
   113   not need to introduce distinguished type constructors
   113   not need to introduce distinguished type constructors
   114   for different kinds of state.  This has two consequences:
   114   for different kinds of state.  This has two consequences:
   115   \begin{itemize}
   115   \begin{itemize}
   116     \item The monad model does not state anything about
   116     \item The monad model does not state anything about
   117        the kind of state; the model for the state is
   117        the kind of state; the model for the state is
   118        completely orthogonal and has to (or may) be
   118        completely orthogonal and may be
   119        specified completely independent.
   119        specified completely independently.
   120     \item There is no distinguished type constructor
   120     \item There is no distinguished type constructor
   121        encapsulating away the state transformation, i.e.~transformations
   121        encapsulating away the state transformation, i.e.~transformations
   122        may be applied directly without using any lifting
   122        may be applied directly without using any lifting
   123        or providing and dropping units (``open monad'').
   123        or providing and dropping units (``open monad'').
   124     \item The type of states may change due to a transformation.
   124     \item The type of states may change due to a transformation.
   188   Evaluation of monadic expressions by force:
   188   Evaluation of monadic expressions by force:
   189 *}
   189 *}
   190 
   190 
   191 lemmas monad_collapse = monad_simp o_apply o_assoc split_Pair split_comp
   191 lemmas monad_collapse = monad_simp o_apply o_assoc split_Pair split_comp
   192   mbind_def fcomp_def run_def
   192   mbind_def fcomp_def run_def
       
   193 
       
   194 subsection {* ML abstract operations *}
       
   195 
       
   196 ML {*
       
   197 structure StateMonad =
       
   198 struct
       
   199 
       
   200 fun liftT T sT = sT --> HOLogic.mk_prodT (T, sT);
       
   201 fun liftT' sT = sT --> sT;
       
   202 
       
   203 fun return T sT x = Const (@{const_name return}, T --> liftT T sT) $ x;
       
   204 
       
   205 fun mbind T1 T2 sT f g = Const (@{const_name mbind},
       
   206   liftT T1 sT --> (T1 --> liftT T2 sT) --> liftT T2 sT) $ f $ g;
       
   207 
       
   208 fun run T sT f = Const (@{const_name run}, liftT' (liftT T sT)) $ f;
       
   209 
       
   210 end;
       
   211 *}
       
   212 
   193 
   213 
   194 subsection {* Syntax *}
   214 subsection {* Syntax *}
   195 
   215 
   196 text {*
   216 text {*
   197   We provide a convenient do-notation for monadic expressions
   217   We provide a convenient do-notation for monadic expressions
   268   list_yield :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<times> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'c list \<times> 'b" where
   288   list_yield :: "('a \<Rightarrow> 'b \<Rightarrow> 'c \<times> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b \<Rightarrow> 'c list \<times> 'b" where
   269   "list_yield f [] = return []"
   289   "list_yield f [] = return []"
   270 | "list_yield f (x#xs) = (do y \<leftarrow> f x; ys \<leftarrow> list_yield f xs; return (y#ys) done)"
   290 | "list_yield f (x#xs) = (do y \<leftarrow> f x; ys \<leftarrow> list_yield f xs; return (y#ys) done)"
   271 
   291 
   272 definition
   292 definition
   273   collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a"
   293   collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
   274   where
       
   275   "collapse f = (do g \<leftarrow> f; g done)"
   294   "collapse f = (do g \<leftarrow> f; g done)"
   276 
   295 
   277 text {* combinator properties *}
   296 text {* combinator properties *}
   278 
   297 
   279 lemma list_append [simp]:
   298 lemma list_append [simp]: