/usr/share/doc/libghc-monads-tf-doc/html/Control-Monad-State-Strict.html is in libghc-monads-tf-doc 0.1.0.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Control.Monad.State.Strict</title><link href="ocean.css" rel="stylesheet" type="text/css" title="Ocean" /><script src="haddock-util.js" type="text/javascript"></script><script type="text/javascript">//<![CDATA[
window.onload = function () {pageLoad();setSynopsis("mini_Control-Monad-State-Strict.html");};
//]]>
</script></head><body><div id="package-header"><ul class="links" id="page-menu"><li><a href="src/Control-Monad-State-Strict.html">Source</a></li><li><a href="index.html">Contents</a></li><li><a href="doc-index.html">Index</a></li></ul><p class="caption">monads-tf-0.1.0.1: Monad classes, using type families</p></div><div id="content"><div id="module-header"><table class="info"><tr><th>Portability</th><td>non-portable (type families)</td></tr><tr><th>Stability</th><td>experimental</td></tr><tr><th>Maintainer</th><td>ross@soi.city.ac.uk</td></tr><tr><th>Safe Haskell</th><td>Safe-Inferred</td></tr></table><p class="caption">Control.Monad.State.Strict</p></div><div id="table-of-contents"><p class="caption">Contents</p><ul><li><a href="#g:1">MonadState class
</a></li><li><a href="#g:2">The State monad
</a></li><li><a href="#g:3">The StateT monad transformer
</a></li><li><a href="#g:4">Examples
</a></li></ul></div><div id="description"><p class="caption">Description</p><div class="doc"><p>Strict state monads.
</p><p>This module is inspired by the paper
/Functional Programming with Overloading and
Higher-Order Polymorphism/,
Mark P Jones (<a href="http://web.cecs.pdx.edu/~mpj/">http://web.cecs.pdx.edu/~mpj/</a>)
Advanced School of Functional Programming, 1995.
</p></div></div><div id="synopsis"><p id="control.syn" class="caption expander" onclick="toggleSection('syn')">Synopsis</p><ul id="section.syn" class="hide" onclick="toggleSection('syn')"><li class="src short"><span class="keyword">class</span> <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a href="#t:MonadState">MonadState</a> m <span class="keyword">where</span><ul class="subs"><li><span class="keyword">type</span> <a href="#t:StateType">StateType</a> m </li><li><a href="#v:get">get</a> :: m (<a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m)</li><li><a href="#v:put">put</a> :: <a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m -> m ()</li></ul></li><li class="src short"><a href="#v:modify">modify</a> :: <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m => (<a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m -> <a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m) -> m ()</li><li class="src short"><a href="#v:gets">gets</a> :: <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m => (<a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m -> a) -> m a</li><li class="src short"><span class="keyword">type</span> <a href="#t:State">State</a> s = <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s <a href="/usr/share/doc/libghc-transformers-doc/html/Data-Functor-Identity.html#t:Identity">Identity</a></li><li class="src short"><a href="#v:runState">runState</a> :: <a href="Control-Monad-State-Strict.html#t:State">State</a> s a -> s -> (a, s)</li><li class="src short"><a href="#v:evalState">evalState</a> :: <a href="Control-Monad-State-Strict.html#t:State">State</a> s a -> s -> a</li><li class="src short"><a href="#v:execState">execState</a> :: <a href="Control-Monad-State-Strict.html#t:State">State</a> s a -> s -> s</li><li class="src short"><a href="#v:mapState">mapState</a> :: ((a, s) -> (b, s)) -> <a href="Control-Monad-State-Strict.html#t:State">State</a> s a -> <a href="Control-Monad-State-Strict.html#t:State">State</a> s b</li><li class="src short"><a href="#v:withState">withState</a> :: (s -> s) -> <a href="Control-Monad-State-Strict.html#t:State">State</a> s a -> <a href="Control-Monad-State-Strict.html#t:State">State</a> s a</li><li class="src short"><span class="keyword">newtype</span> <a href="#t:StateT">StateT</a> s m a = <a href="#v:StateT">StateT</a> {<ul class="subs"><li><a href="#v:runStateT">runStateT</a> :: s -> m (a, s)</li></ul>}</li><li class="src short"><a href="#v:evalStateT">evalStateT</a> :: <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a -> s -> m a</li><li class="src short"><a href="#v:execStateT">execStateT</a> :: <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a -> s -> m s</li><li class="src short"><a href="#v:mapStateT">mapStateT</a> :: (m (a, s) -> n (b, s)) -> <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a -> <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s n b</li><li class="src short"><a href="#v:withStateT">withStateT</a> :: (s -> s) -> <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a -> <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a</li><li class="src short">module <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html">Control.Monad</a></li><li class="src short">module <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad-Fix.html">Control.Monad.Fix</a></li><li class="src short">module <a href="Control-Monad-Trans.html">Control.Monad.Trans</a></li></ul></div><div id="interface"><h1 id="g:1">MonadState class
</h1><div class="top"><p class="src"><span class="keyword">class</span> <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a name="t:MonadState" class="def">MonadState</a> m <span class="keyword">where</span><a href="src/Control-Monad-State-Class.html#MonadState" class="link">Source</a></p><div class="doc"><p><em>get</em> returns the state from the internals of the monad.
</p><p><em>put</em> replaces the state inside the monad.
</p></div><div class="subs associated-types"><p class="caption">Associated Types</p><p class="src"><span class="keyword">type</span> <a name="t:StateType" class="def">StateType</a> m <a href="src/Control-Monad-State-Class.html#StateType" class="link">Source</a></p></div><div class="subs methods"><p class="caption">Methods</p><p class="src"><a name="v:get" class="def">get</a> :: m (<a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m)<a href="src/Control-Monad-State-Class.html#get" class="link">Source</a></p><p class="src"><a name="v:put" class="def">put</a> :: <a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m -> m ()<a href="src/Control-Monad-State-Class.html#put" class="link">Source</a></p></div><div class="subs instances"><p id="control.i:MonadState" class="caption collapser" onclick="toggleSection('i:MonadState')">Instances</p><div id="section.i:MonadState" class="show"><table><tr><td class="src"><a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-Trans-Maybe.html#t:MaybeT">MaybeT</a> m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-List.html#t:ListT">ListT</a> m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-Trans-Identity.html#t:IdentityT">IdentityT</a> m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m) => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-Writer-Strict.html#t:WriterT">WriterT</a> w m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Data-Monoid.html#t:Monoid">Monoid</a> w, <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m) => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-Writer-Lazy.html#t:WriterT">WriterT</a> w m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-State-Lazy.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-Reader.html#t:ReaderT">ReaderT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="Control-Monad-Error-Class.html#t:Error">Error</a> e, <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m) => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-Error.html#t:ErrorT">ErrorT</a> e m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-Cont.html#t:ContT">ContT</a> r m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m, <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Data-Monoid.html#t:Monoid">Monoid</a> w) => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-RWS-Strict.html#t:RWST">RWST</a> r w s m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m, <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Data-Monoid.html#t:Monoid">Monoid</a> w) => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-RWS-Lazy.html#t:RWST">RWST</a> r w s m)</td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:modify" class="def">modify</a> :: <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m => (<a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m -> <a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m) -> m ()<a href="src/Control-Monad-State-Class.html#modify" class="link">Source</a></p><div class="doc"><p>Monadic state transformer.
</p><p>Maps an old state to a new state inside a state monad.
The old state is thrown away.
</p><pre> Main> :t modify ((+1) :: Int -> Int)
modify (...) :: (MonadState Int a) => a ()
</pre><p>This says that <code>modify (+1)</code> acts over any
Monad that is a member of the <code>MonadState</code> class,
with an <code>Int</code> state.
</p></div></div><div class="top"><p class="src"><a name="v:gets" class="def">gets</a> :: <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> m => (<a href="Control-Monad-State-Strict.html#t:StateType">StateType</a> m -> a) -> m a<a href="src/Control-Monad-State-Class.html#gets" class="link">Source</a></p><div class="doc"><p>Gets specific component of the state, using a projection function
supplied.
</p></div></div><h1 id="g:2">The State monad
</h1><div class="top"><p class="src"><span class="keyword">type</span> <a name="t:State" class="def">State</a> s = <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s <a href="/usr/share/doc/libghc-transformers-doc/html/Data-Functor-Identity.html#t:Identity">Identity</a></p><div class="doc"><p>A state monad parameterized by the type <code>s</code> of the state to carry.
</p><p>The <code><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#v:return">return</a></code> function leaves the state unchanged, while <code>>>=</code> uses
the final state of the first computation as the initial state of
the second.
</p></div></div><div class="top"><p class="src"><a name="v:runState" class="def">runState</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="Control-Monad-State-Strict.html#t:State">State</a> s a</td><td class="doc"><p>state-passing computation to execute
</p></td></tr><tr><td class="src">-> s</td><td class="doc"><p>initial state
</p></td></tr><tr><td class="src">-> (a, s)</td><td class="doc"><p>return value and final state
</p></td></tr></table></div><div class="doc"><p>Unwrap a state monad computation as a function.
(The inverse of <code><a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-Trans-State-Strict.html#v:state">state</a></code>.)
</p></div></div><div class="top"><p class="src"><a name="v:evalState" class="def">evalState</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="Control-Monad-State-Strict.html#t:State">State</a> s a</td><td class="doc"><p>state-passing computation to execute
</p></td></tr><tr><td class="src">-> s</td><td class="doc"><p>initial value
</p></td></tr><tr><td class="src">-> a</td><td class="doc"><p>return value of the state computation
</p></td></tr></table></div><div class="doc"><p>Evaluate a state computation with the given initial state
and return the final value, discarding the final state.
</p><ul><li><pre><code><a href="Control-Monad-State-Strict.html#v:evalState">evalState</a></code> m s = <code><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Data-Tuple.html#v:fst">fst</a></code> (<code><a href="Control-Monad-State-Strict.html#v:runState">runState</a></code> m s)</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:execState" class="def">execState</a></p><div class="subs arguments"><p class="caption">Arguments</p><table><tr><td class="src">:: <a href="Control-Monad-State-Strict.html#t:State">State</a> s a</td><td class="doc"><p>state-passing computation to execute
</p></td></tr><tr><td class="src">-> s</td><td class="doc"><p>initial value
</p></td></tr><tr><td class="src">-> s</td><td class="doc"><p>final state
</p></td></tr></table></div><div class="doc"><p>Evaluate a state computation with the given initial state
and return the final state, discarding the final value.
</p><ul><li><pre><code><a href="Control-Monad-State-Strict.html#v:execState">execState</a></code> m s = <code><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Data-Tuple.html#v:snd">snd</a></code> (<code><a href="Control-Monad-State-Strict.html#v:runState">runState</a></code> m s)</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:mapState" class="def">mapState</a> :: ((a, s) -> (b, s)) -> <a href="Control-Monad-State-Strict.html#t:State">State</a> s a -> <a href="Control-Monad-State-Strict.html#t:State">State</a> s b</p><div class="doc"><p>Map both the return value and final state of a computation using
the given function.
</p><ul><li><pre><code><a href="Control-Monad-State-Strict.html#v:runState">runState</a></code> (<code><a href="Control-Monad-State-Strict.html#v:mapState">mapState</a></code> f m) = f . <code><a href="Control-Monad-State-Strict.html#v:runState">runState</a></code> m</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:withState" class="def">withState</a> :: (s -> s) -> <a href="Control-Monad-State-Strict.html#t:State">State</a> s a -> <a href="Control-Monad-State-Strict.html#t:State">State</a> s a</p><div class="doc"><p><code><code><a href="Control-Monad-State-Strict.html#v:withState">withState</a></code> f m</code> executes action <code>m</code> on a state modified by
applying <code>f</code>.
</p><ul><li><pre><code><a href="Control-Monad-State-Strict.html#v:withState">withState</a></code> f m = <code><a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-Trans-State-Strict.html#v:modify">modify</a></code> f >> m</pre></li></ul></div></div><h1 id="g:3">The StateT monad transformer
</h1><div class="top"><p class="src"><span class="keyword">newtype</span> <a name="t:StateT" class="def">StateT</a> s m a </p><div class="doc"><p>A state transformer monad parameterized by:
</p><ul><li> <code>s</code> - The state.
</li><li> <code>m</code> - The inner monad.
</li></ul><p>The <code><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#v:return">return</a></code> function leaves the state unchanged, while <code>>>=</code> uses
the final state of the first computation as the initial state of
the second.
</p></div><div class="subs constructors"><p class="caption">Constructors</p><table><tr><td class="src"><a name="v:StateT" class="def">StateT</a></td><td class="doc empty"> </td></tr><tr><td colspan="2"><div class="subs fields"><p class="caption">Fields</p><dl><dt class="src"><a name="v:runStateT" class="def">runStateT</a> :: s -> m (a, s)</dt><dd class="doc empty"> </dd></dl><div class="clear"></div></div></td></tr></table></div><div class="subs instances"><p id="control.i:StateT" class="caption collapser" onclick="toggleSection('i:StateT')">Instances</p><div id="section.i:StateT" class="show"><table><tr><td class="src"><a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-Trans-Class.html#t:MonadTrans">MonadTrans</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Functor">Functor</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Functor">Functor</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad-Fix.html#t:MonadFix">MonadFix</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad-Fix.html#t:MonadFix">MonadFix</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:MonadPlus">MonadPlus</a> m => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:MonadPlus">MonadPlus</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Functor">Functor</a> m, <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m) => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Applicative.html#t:Applicative">Applicative</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src">(<a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Functor">Functor</a> m, <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:MonadPlus">MonadPlus</a> m) => <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Applicative.html#t:Alternative">Alternative</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> m => <a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-IO-Class.html#t:MonadIO">MonadIO</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Cont-Class.html#t:MonadCont">MonadCont</a> m => <a href="Control-Monad-Cont-Class.html#t:MonadCont">MonadCont</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Error-Class.html#t:MonadError">MonadError</a> m => <a href="Control-Monad-Error-Class.html#t:MonadError">MonadError</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Reader-Class.html#t:MonadReader">MonadReader</a> m => <a href="Control-Monad-Reader-Class.html#t:MonadReader">MonadReader</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-State-Strict.html#t:MonadState">MonadState</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr><tr><td class="src"><a href="Control-Monad-Writer-Class.html#t:MonadWriter">MonadWriter</a> m => <a href="Control-Monad-Writer-Class.html#t:MonadWriter">MonadWriter</a> (<a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m)</td><td class="doc empty"> </td></tr></table></div></div></div><div class="top"><p class="src"><a name="v:evalStateT" class="def">evalStateT</a> :: <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a -> s -> m a</p><div class="doc"><p>Evaluate a state computation with the given initial state
and return the final value, discarding the final state.
</p><ul><li><pre><code><a href="Control-Monad-State-Strict.html#v:evalStateT">evalStateT</a></code> m s = <code><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#v:liftM">liftM</a></code> <code><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Data-Tuple.html#v:fst">fst</a></code> (<code><a href="Control-Monad-State-Strict.html#v:runStateT">runStateT</a></code> m s)</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:execStateT" class="def">execStateT</a> :: <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#t:Monad">Monad</a> m => <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a -> s -> m s</p><div class="doc"><p>Evaluate a state computation with the given initial state
and return the final state, discarding the final value.
</p><ul><li><pre><code><a href="Control-Monad-State-Strict.html#v:execStateT">execStateT</a></code> m s = <code><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html#v:liftM">liftM</a></code> <code><a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Data-Tuple.html#v:snd">snd</a></code> (<code><a href="Control-Monad-State-Strict.html#v:runStateT">runStateT</a></code> m s)</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:mapStateT" class="def">mapStateT</a> :: (m (a, s) -> n (b, s)) -> <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a -> <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s n b</p><div class="doc"><p>Map both the return value and final state of a computation using
the given function.
</p><ul><li><pre><code><a href="Control-Monad-State-Strict.html#v:runStateT">runStateT</a></code> (<code><a href="Control-Monad-State-Strict.html#v:mapStateT">mapStateT</a></code> f m) = f . <code><a href="Control-Monad-State-Strict.html#v:runStateT">runStateT</a></code> m</pre></li></ul></div></div><div class="top"><p class="src"><a name="v:withStateT" class="def">withStateT</a> :: (s -> s) -> <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a -> <a href="Control-Monad-State-Strict.html#t:StateT">StateT</a> s m a</p><div class="doc"><p><code><code><a href="Control-Monad-State-Strict.html#v:withStateT">withStateT</a></code> f m</code> executes action <code>m</code> on a state modified by
applying <code>f</code>.
</p><ul><li><pre><code><a href="Control-Monad-State-Strict.html#v:withStateT">withStateT</a></code> f m = <code><a href="/usr/share/doc/libghc-transformers-doc/html/Control-Monad-Trans-State-Strict.html#v:modify">modify</a></code> f >> m</pre></li></ul></div></div><div class="top"><p class="src">module <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad.html">Control.Monad</a></p></div><div class="top"><p class="src">module <a href="/usr/share/doc/ghc-doc/html/libraries/base-4.6.0.1/Control-Monad-Fix.html">Control.Monad.Fix</a></p></div><div class="top"><p class="src">module <a href="Control-Monad-Trans.html">Control.Monad.Trans</a></p></div><h1 id="g:4">Examples
</h1><div class="doc"><p>A function to increment a counter. Taken from the paper
<em>Generalising Monads to Arrows</em>, John
Hughes (<a href="http://www.math.chalmers.se/~rjmh/">http://www.math.chalmers.se/~rjmh/</a>), November 1998:
</p><pre> tick :: State Int Int
tick = do n <- get
put (n+1)
return n
</pre><p>Add one to the given number using the state monad:
</p><pre> plusOne :: Int -> Int
plusOne n = execState tick n
</pre><p>A contrived addition example. Works only with positive numbers:
</p><pre> plus :: Int -> Int -> Int
plus n x = execState (sequence $ replicate n tick) x
</pre><p>An example from <em>The Craft of Functional Programming</em>, Simon
Thompson (<a href="http://www.cs.kent.ac.uk/people/staff/sjt/">http://www.cs.kent.ac.uk/people/staff/sjt/</a>),
Addison-Wesley 1999: "Given an arbitrary tree, transform it to a
tree of integers in which the original elements are replaced by
natural numbers, starting from 0. The same element has to be
replaced by the same number at every occurrence, and when we meet
an as-yet-unvisited element we have to find a 'new' number to match
it with:"
</p><pre> data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Eq)
type Table a = [a]
</pre><pre> numberTree :: Eq a => Tree a -> State (Table a) (Tree Int)
numberTree Nil = return Nil
numberTree (Node x t1 t2)
= do num <- numberNode x
nt1 <- numberTree t1
nt2 <- numberTree t2
return (Node num nt1 nt2)
where
numberNode :: Eq a => a -> State (Table a) Int
numberNode x
= do table <- get
(newTable, newPos) <- return (nNode x table)
put newTable
return newPos
nNode:: (Eq a) => a -> Table a -> (Table a, Int)
nNode x table
= case (findIndexInList (== x) table) of
Nothing -> (table ++ [x], length table)
Just i -> (table, i)
findIndexInList :: (a -> Bool) -> [a] -> Maybe Int
findIndexInList = findIndexInListHelp 0
findIndexInListHelp _ _ [] = Nothing
findIndexInListHelp count f (h:t)
= if (f h)
then Just count
else findIndexInListHelp (count+1) f t
</pre><p>numTree applies numberTree with an initial state:
</p><pre> numTree :: (Eq a) => Tree a -> Tree Int
numTree t = evalState (numberTree t) []
</pre><pre> testTree = Node "Zero" (Node "One" (Node "Two" Nil Nil) (Node "One" (Node "Zero" Nil Nil) Nil)) Nil
numTree testTree => Node 0 (Node 1 (Node 2 Nil Nil) (Node 1 (Node 0 Nil Nil) Nil)) Nil
</pre><p>sumTree is a little helper function that does not use the State monad:
</p><pre> sumTree :: (Num a) => Tree a -> a
sumTree Nil = 0
sumTree (Node e t1 t2) = e + (sumTree t1) + (sumTree t2)
</pre></div></div></div><div id="footer"><p>Produced by <a href="http://www.haskell.org/haddock/">Haddock</a> version 2.13.2</p></div></body></html>
|