<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LAMBDAPHONE &#187; tutorial</title>
	<atom:link href="http://coder.bsimmons.name/blog/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.bsimmons.name/blog</link>
	<description>fragmentary ideas  ䷿  intellectual what-nots  ䷷  and haskell programming  ䷴</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:24:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>A brief tutorial-introduction to &#8216;fclabels&#8217; 1.0</title>
		<link>http://coder.bsimmons.name/blog/2011/08/a-brief-tutorial-introduction-to-fclabels-1-0/</link>
		<comments>http://coder.bsimmons.name/blog/2011/08/a-brief-tutorial-introduction-to-fclabels-1-0/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 18:39:16 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[fclabels]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=575</guid>
		<description><![CDATA[<p>Sebastiaan Visser et al. last week announced the release of v1.0 of <a href="http://hackage.haskell.org/package/fclabels">fclabels</a>. This is a short introduction to the changes for people who are already somewhat familiar with the package.</p>
<blockquote><p><strong>Que?</strong> The fclabels package is a haskell library</p></blockquote><p>&#8230; <a href="http://coder.bsimmons.name/blog/2011/08/a-brief-tutorial-introduction-to-fclabels-1-0/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<p>Sebastiaan Visser et al. last week announced the release of v1.0 of <a href="http://hackage.haskell.org/package/fclabels">fclabels</a>. This is a short introduction to the changes for people who are already somewhat familiar with the package.</p>
<blockquote><p><strong>Que?</strong> The fclabels package is a haskell library providing &#8220;first class labels&#8221; for haskell data types and some Template Haskell code for automatically generating said labels. This makes for a more composable and much more powerful alternative to haskell&#8217;s built-in record syntactic sugar. <a href="http://blog.ezyang.com/2010/04/inessential-guide-to-fclabels/">Here</a> is a good introductory tutorial.</p></blockquote>
<p>The new version features (besides some better names) a type for <a href="http://hackage.haskell.org/packages/archive/fclabels/1.0.1/doc/html/Data-Label-Maybe.html">lenses that can fail</a>. S.V. et al. added this functionality in a brilliant and mostly (except for the name changes) backwards compatible way. We&#8217;ll see how it all works now.</p>
<p>We&#8217;ll prepare a module for fclabels in the usual way:</p>
<p><blockquote class="vimblock"><br />
<span class="lnr">1&nbsp;</span><span class="Special">{-# LANGUAGE TemplateHaskell, TypeOperators #-}</span><br />
<span class="lnr">2&nbsp;</span><span class="PreProc">import</span>&nbsp;<span class="Normal">Control.Category</span><br />
<span class="lnr">3&nbsp;</span><span class="PreProc">import</span>&nbsp;<span class="Normal">Data.Label</span><br />
<span class="lnr">4&nbsp;</span><span class="PreProc">import</span>&nbsp;<span class="PreProc">qualified</span>&nbsp;<span class="Normal">Data.Label.Maybe</span>&nbsp;<span class="PreProc">as</span>&nbsp;<span class="Normal">M</span><br />
<span class="lnr">5&nbsp;</span><span class="PreProc">import</span>&nbsp;<span class="Normal">Prelude</span>&nbsp;<span class="PreProc">hiding</span>&nbsp;(<span class="Identifier">(.)</span>,&nbsp;<span class="Identifier">id</span>)<br />
<br /></blockquote></p>
<p>The only odd thing above is our qualified import of <code>Data.Label.Maybe</code> which provides getters, setters etc. that can fail.</p>
<p>We want to generate lenses for the following type, so we do the underdash thing:</p>
<p><blockquote class="vimblock"><br />
<span class="lnr">36&nbsp;</span><span class="Type">data</span>&nbsp;Example&nbsp;<span class="Statement">=</span>&nbsp;X&nbsp;{&nbsp;_int&nbsp;<span class="Statement">::</span>&nbsp;Int&nbsp;,&nbsp;_char&nbsp;<span class="Statement">::</span>&nbsp;Char}<br />
<span class="lnr">37&nbsp;</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">|</span>&nbsp;Y&nbsp;{&nbsp;_int&nbsp;<span class="Statement">::</span>&nbsp;Int&nbsp;,&nbsp;_example&nbsp;<span class="Statement">::</span>&nbsp;Example&nbsp;}<br />
<br /></blockquote></p>
<p>Notice how <code>_int</code> is a valid record for <em>both</em> constructors <code>X</code> and <code>Y</code>, where the other two are partial functions.</p>
<p>Lastly, the usual splice for generating labels:</p>
<p><blockquote class="vimblock"><br />
<span class="lnr">39&nbsp;</span>$(<span class="PreProc">mkLabels[''Example])</span><br />
<br /></blockquote></p>
<p>Let&#8217;s load our file into GHCi and play a little:</p>
<blockquote><pre>
Prelude> :l test.hs
[1 of 1] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> get int $ X 1 'a'
1
*Main> M.get int $ X 1 'a'
Just 1
*Main> M.get char $ X 1 'a'
Just 'a'
*Main> M.get example $ X 1 'a'
Nothing
*Main> get example $ X 1 'a'

<interactive>:1:5:
    No instance for (Control.Arrow.ArrowZero (->))
      arising from a use of `example'
    Possible fix:
      add an instance declaration for (Control.Arrow.ArrowZero (->))
    In the first argument of `get', namely `example'
    In the expression: get example
    In the expression: get example $ X 1 'a'
</interactive></pre>
</blockquote>
<p>Above we saw that:</p>
<ul>
<li>the total <code>int</code> label could be used in both <code>get</code> and <code>Data.Label.Maybe.get</code>
</li>
<li>M.get on our &#8220;partial lenses&#8221; (<code>example</code> and <code>char</code>) safely returned a Maybe value.
</li>
<li>pure <code>get</code> used with partial lenses doesn&#8217;t type check
</li>
</ul>
<p>We need to look at some types (note I&#8217;ve cleaned up some of these type signatures, yours may look uglier):</p>
<blockquote><pre>
*Main> :t get
get :: (f :-> a) -> f -> a
*Main> :t M.get
M.get :: (f M.:~> a) -> f -> Maybe a
*Main> :info (:->)
type (:->) f a = Data.Label.Pure.PureLens f a
  	-- Defined in Data.Label.Pure
*Main> :info (M.:~>)
type (M.:~>) f a = Data.Label.Maybe.MaybeLens f a
  	-- Defined in Data.Label.Maybe
</pre>
</blockquote>
<p>You can see above that setters and getters are monomorphic. But how then were we able to use <code>int</code> in both <code>get</code> and <code>M.get</code>? Time to look at the types of our TH-generated lenses:</p>
<blockquote><pre>
*Main> :t int
int
  :: Control.Arrow.Arrow (~>) => Lens (~>) Example Int
*Main> :t char
char
  :: (Control.Arrow.ArrowZero (~>),
      Control.Arrow.ArrowChoice (~>)) =>
     Lens (~>) Example Char
*Main> :t example
example
  :: (Control.Arrow.ArrowZero (~>),
      Control.Arrow.ArrowChoice (~>)) =>
     Lens (~>) Example Example
</pre>
</blockquote>
<p>Whoah! Lenses themselves are polymorphic in the Arrow class; partial lenses have additional restrictions of <code>ArrowZero</code> and <code>ArrowChoice</code> allowing for failure and <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Arrow.html#t:ArrowChoice">choice</a>. </p>
<p>This is wizardry of the highest order.</p>
<p>Notice how we can still compose lenses freely, whether partial or total:</p>
<blockquote><p>*Main> :t (.)<br />
(.) :: Category cat => cat b c -> cat a b -> cat a c<br />
*Main> M.get (int . example) (Y 1 (X 2 &#8216;a&#8217;))<br />
Just 2</p></blockquote>
<h2>A note on <code>mkLabelsNoTypes</code></h2>
<p>I had been using the noTypes variation to generate lenses that I was exporting in a module, because the TH-assigned type sigs looked pretty nasty. If you do this with >1.0 you will run into the monomorphism restriction and ambiguous type variables. </p>
<p>Instead I would suggest defining <em>exported</em> lables by hand and giving them a nicer looking (or monomorphic if you prefer) type sig.</p>
<p>You can even let fclabels generate the code and just paste it in, e.g.:</p>
<blockquote><pre>
Prelude> :set -ddump-splices
Prelude> :l test.hs
[1 of 1] Compiling Main             ( test.hs, interpreted )
test.hs:1:1: Splicing declarations
    mkLabels ['Example]
  ======>
    test.hs:40:3-21
    char ::
      forall ~>[a1kk]. (Control.Arrow.ArrowChoice ~>[a1kk],
                        Control.Arrow.ArrowZero ~>[a1kk]) =>
      Lens ~>[a1kk] Example Char
    {-# INLINE[0] CONLIKE char #-}
    char
      = let
          c[a1kl]
            = (Control.Arrow.zeroArrow Control.Arrow.||| Control.Arrow.returnA)
        in
          Data.Label.Abstract.lens
            (c[a1kl]
           . Control.Arrow.arr
               (\ p[a1km]
                  -> case p[a1km] of {
                       X {} -> Right (_char p[a1km])
                       _ -> Left GHC.Unit.() }))
            (c[a1kl]
           . Control.Arrow.arr
               (\ (v[a1kn], p[a1ko])
                  -> case p[a1ko] of {
                       X {} -> Right (p[a1ko] {_char = v[a1kn]})
                       _ -> Left GHC.Unit.() }))
...
</pre>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2011/08/a-brief-tutorial-introduction-to-fclabels-1-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The State Monad: a tutorial for the confused?</title>
		<link>http://coder.bsimmons.name/blog/2009/10/the-state-monad-a-tutorial-for-the-confused/</link>
		<comments>http://coder.bsimmons.name/blog/2009/10/the-state-monad-a-tutorial-for-the-confused/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 00:16:57 +0000</pubDate>
		<dc:creator>jberryman</dc:creator>
				<category><![CDATA[haskell]]></category>
		<category><![CDATA[combinator]]></category>
		<category><![CDATA[monads]]></category>
		<category><![CDATA[state]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://coder.bsimmons.name/blog/?p=230</guid>
		<description><![CDATA[<p><em>I&#8217;ve written this <del>brief</del> tutorial on haskell&#8217;s <code>State</code> monad to help bridge some of the elusive gaps that I encountered in other explanations I&#8217;ve read, and to try to cut through all the sticky abstraction. This is written for someone</em>&#8230; <a href="http://coder.bsimmons.name/blog/2009/10/the-state-monad-a-tutorial-for-the-confused/" class="read_more">   [ R E A D &#124; M O R E ]</a></p>]]></description>
			<content:encoded><![CDATA[<p><em>I&#8217;ve written this <del>brief</del> tutorial on haskell&#8217;s <code>State</code> monad to help bridge some of the elusive gaps that I encountered in other explanations I&#8217;ve read, and to try to cut through all the sticky abstraction. This is written for someone who has a good understanding of the <code>Maybe</code> and <code>List</code> monads, but has gotten stuck trying to understand State. I hope it&#8217;s helpful!</em></p>
<h4>The Data Declaration:</h4>
<p>To understand a monad you look at it&#8217;s datatype and then at the definition for bind (<code>>>=</code>). Most monad tutorials start by showing you the data declaration of a <code>State s a</code> in passing, as if it needed no explanation:</p>
<p><blockquote class="vimblock"><br />
<span class="Type">newtype</span>&nbsp;State&nbsp;s&nbsp;a&nbsp;<span class="Statement">=</span>&nbsp;State&nbsp;{&nbsp;runState&nbsp;<span class="Statement">::</span>&nbsp;s&nbsp;<span class="Statement">-&gt;</span>&nbsp;(a,&nbsp;s)&nbsp;}<br />
<br /></blockquote></p>
<p>But this <em>does</em> need explanation! This is crazy stuff and nothing like what we&#8217;ve seen before in the list monad or the Maybe monad:</p>
<ol>
<li>The constructor <code>State</code> <strong>holds a function</strong>, not just a simple value like Maybe&#8217;s <code>Just</code>. This looks weird.</li>
<li>Furthermore there is an accessor function <code>runState</code> <strong>with a weirdly imperative-sounding name</strong>.</li>
<li>Finally, there are <strong>two free variables</strong> on the left side, not just one.</li>
</ol>
<p>Yikes! Let&#8217;s try to get our head on straight and figure this out:</p>
<p>First of all the State monad is just <strong>an abstraction for a function that takes a state and returns an intermediate value and some new state value</strong>. To formalize this abstraction in haskell, we wrap the function in the newtype <code>State</code> allowing us to define a <code>Monad</code> instance.</p>
<p>Stepping back from the abstract and conceptual, what we have is <strong>the <code>State</code> constructor acting as a container for a function <code>:: s -> (a,s)</code></strong>, while the definition for bind just provides <strong>a mechanism for &#8220;composing&#8221; a function <code>state -> (val,state)</code> within the <code>State</code> wrapper</strong>.</p>
<p>Just as you can chain together functions using <code>(.)</code> as in <code>(+1) . (*3) . head :: (Num a) => [a] -> a</code>, <strong>the state monad gives you <code>(>>=)</code> to chain together functions that look essentially like <code>:: a -> s -> (a,s) </code>into a single function <code>:: s -> (a,s)</code></strong>.</p>
<p>Let&#8217;s bring the discussion back to actual code and try to make sure we understand those three points of weirdness outlined above. Here&#8217;s a stupid example of a function that can be &#8220;contained&#8221; in our state type:</p>
<p><blockquote class="vimblock"><br />
<span class="Comment">-- look at our counter and return &quot;foo&quot; or &quot;bar&quot;</span><br />
<span class="Comment">-- along with the incremented counter:</span><br />
fromStoAandS&nbsp;<span class="Statement">::</span>&nbsp;<span class="Type">Int</span>&nbsp;<span class="Statement">-&gt;</span>&nbsp;(<span class="Type">String</span>,<span class="Type">Int</span>)<br />
fromStoAandS&nbsp;c&nbsp;<span class="Statement">|</span>&nbsp;c&nbsp;<span class="Statement">`mod`</span>&nbsp;<span class="Constant">5</span>&nbsp;<span class="Statement">==</span>&nbsp;<span class="Constant">0</span>&nbsp;<span class="Statement">=</span>&nbsp;(<span class="Constant">&quot;foo&quot;</span>,c<span class="Statement">+</span><span class="Constant">1</span>)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">|</span>&nbsp;<span class="Identifier">otherwise</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">=</span>&nbsp;(<span class="Constant">&quot;bar&quot;</span>,c<span class="Statement">+</span><span class="Constant">1</span>)<br />
<br /></blockquote></p>
<p>If we just wrap that in a <code>State</code> constructor, we&#8217;re in the State monad:</p>
<p><blockquote class="vimblock"><br />
stateIntString&nbsp;<span class="Statement">::</span>&nbsp;State&nbsp;<span class="Type">Int</span>&nbsp;<span class="Type">String</span><br />
stateIntString&nbsp;<span class="Statement">=</span>&nbsp;State&nbsp;fromStoAandS<br />
<br /></blockquote></p>
<p>But what about <code>runState</code>? <strong>All that does of course is give us the &#8220;contents&#8221; of our State constructor: i.e. a single function <code>:: s -> (a,s)</code></strong>. It could have been named <code>stateFunction</code> but someone thought it would be really clever to be able to write things like:</p>
<p><blockquote class="vimblock"><br />
runState&nbsp;stateIntString&nbsp;<span class="Constant">1</span><br />
<br /></blockquote></p>
<p>See, all we&#8217;ve done there is used <code>runState</code> to take our function (<code>fromStoAandS</code>) out of the State wrapper; it is then applied it to it&#8217;s initial state (<code>1</code>). We would do this <code>runState</code> business <strong>after building up our composed function with <code>(>>=)</code>, <code>mapM</code>, etc</strong>.</p>
<p>That leaves point 3 unanswered. Let&#8217;s start exploring the instance declaration for State.</p>
<h4>The Instance Declaration</h4>
<p>We&#8217;ll start with the first line:</p>
<p><blockquote class="vimblock"><br />
<span class="Type">instance</span>&nbsp;<span class="Type">Monad</span>&nbsp;(State&nbsp;s)&nbsp;<span class="Type">where</span><br />
<br /></blockquote></p>
<p><strong>We create a Monad instance for (State s) not State</strong>. You can think of this as a <strong>partially-applied type</strong>, which is equivalent to a partially applied function: </p>
<blockquote><p><code>(State) < ==> (+)</code><br />
<code>(State s) < ==> (1+)</code><br />
<code>(State s a) < == (1+2)</code></code></p></blockquote>
<p><strong>So (<code>State s</code>) is the <code>m</code> in our <code>m a</code>.</strong> This means the <em>type</em> of our state will remain the same as we compose our function with <code>(>>=)</code>, whereas the intermediate values (the <code>a</code>s) may well change type as they move through the chain.</p>
<p>Before we move on to the meat of the instance declaration, I&#8217;d like to get your mind calibrated to look at the definitions for <code>return</code> and <code>(>>=)</code>:</p>
<p>Whenever you see <code>m a</code>, as in </p>
<blockquote><p><code>return :: (Monad m) => a -> m a</code> </p></blockquote>
<p>&#8230;remember that <code>m a</code> is actually </p>
<blockquote><p><code>State s a</code></p></blockquote>
<p>&#8230;and when you remember <code>(State s a)</code>, <em>think</em> </p>
<blockquote><p><code>(s -> (s,a))</code>. </p></blockquote>
<p><strong>So in your mind, <code>m a</code> becomes <code>function :: s -> (a,s)</code> everywhere you see it</strong>. Just forget about the silly State wrapper (<a href="http://www.haskell.org/haskellwiki/Newtype">the compiler does</a>)!</p>
<h4>The definition of return and Bind</h4>
<p>Let&#8217;s wet our feet with the definition for <code>return</code>:</p>
<p><blockquote class="vimblock"><br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="Identifier">return</span>&nbsp;a&nbsp;<span class="Statement">=</span>&nbsp;State&nbsp;<span class="Statement">$</span>&nbsp;<span class="Statement">\</span>s&nbsp;<span class="Statement">-&gt;</span>&nbsp;(a,&nbsp;s)<br />
<br /></blockquote></p>
<p>All return does is take some value a and make a function that takes a state value and returns (value, state value). If we ignore the whole State wrapping business, then return is just <code>(,) :: a -> b -> (a, b)</code></p>
<p>Now recall the definition of bind:</p>
<p><blockquote class="vimblock"><br />
(<span class="Statement">&gt;&gt;=</span>)&nbsp;<span class="Statement">::</span>&nbsp;(<span class="Type">Monad</span>&nbsp;m)&nbsp;<span class="Statement">=&gt;</span>&nbsp;m&nbsp;a&nbsp;<span class="Statement">-&gt;</span>&nbsp;(a&nbsp;<span class="Statement">-&gt;</span>&nbsp;m&nbsp;b)&nbsp;<span class="Statement">-&gt;</span>&nbsp;m&nbsp;b<br />
<br /></blockquote></p>
<p>Which in our case is:</p>
<p><blockquote class="vimblock"><br />
(<span class="Statement">&gt;&gt;=</span>)&nbsp;<span class="Statement">::</span>&nbsp;&nbsp;State&nbsp;s&nbsp;a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">-&gt;</span>&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(a&nbsp;<span class="Statement">-&gt;</span>&nbsp;State&nbsp;s&nbsp;b)&nbsp;<span class="Statement">-&gt;</span>&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;State&nbsp;s&nbsp;b<br />
<br /></blockquote></p>
<p>And which is just a silly abstraction for the <strong>super special function composition</strong> that&#8217;s going on, which looks like:</p>
<p><blockquote class="vimblock"><br />
(<span class="Statement">&gt;&gt;=</span>)&nbsp;<span class="Statement">::</span>&nbsp;&nbsp;(s&nbsp;<span class="Statement">-&gt;</span>&nbsp;(a,s))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class="Statement">-&gt;</span>&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(a&nbsp;<span class="Statement">-&gt;</span>&nbsp;s&nbsp;<span class="Statement">-&gt;</span>&nbsp;(b,s))&nbsp;&nbsp;<span class="Statement">-&gt;</span>&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(s&nbsp;<span class="Statement">-&gt;</span>&nbsp;(b,s))<br />
<br /></blockquote></p>
<p>So on the left hand side of (<code>>>=</code>) is a function that takes some initial state and produces a <code>(value,new_state)</code>. On the right hand side is a function that takes that value and that new_state and generates it&#8217;s own <code>(new_value, newer_state)</code>. <strong>The job of bind is simply to combine those two functions into one bigger function from the initial state to <code>(new_value,newer_state)</code></strong>, just like the simple function composition operator<code> (.) :: (b -> c) -> (a -> b) -> a -> c</code></p>
<p>At this point, we can show you bind&#8217;s definition:</p>
<p><blockquote class="vimblock"><br />
&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;<span class="Statement">&gt;&gt;=</span>&nbsp;k&nbsp;&nbsp;<span class="Statement">=</span>&nbsp;State&nbsp;<span class="Statement">$</span>&nbsp;<span class="Statement">\</span>s&nbsp;<span class="Statement">-&gt;</span>&nbsp;<span class="Statement">let</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(a,&nbsp;s')&nbsp;<span class="Statement">=</span>&nbsp;runState&nbsp;m&nbsp;s<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="Statement">in</span>&nbsp;runState&nbsp;(k&nbsp;a)&nbsp;s'<br />
<br /></blockquote></p>
<p>You can work through that on your own, keeping in mind that we&#8217;re doing function composition here. The main thing to remember is that the <code>s</code> at the top, right after <code>State</code>, <strong>won&#8217;t actually be bound to a value until we unwrap the function with <code>runState</code> and pass it the initial state value</strong>, at which point we can evaluate the entire chain.</p>
<h4>A Final Note About The State Monad with <code>do</code> Notation</h4>
<p><code>State</code> is often used like this:</p>
<p><blockquote class="vimblock"><br />
stateFunction&nbsp;<span class="Statement">::</span>&nbsp;State&nbsp;[a]&nbsp;()<br />
stateFunction&nbsp;<span class="Statement">=</span>&nbsp;<span class="Statement">do</span>&nbsp;x&nbsp;<span class="Statement">&lt;-</span>&nbsp;pop<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pop<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; push&nbsp;x<br />
<br /></blockquote></p>
<p>Remember that the functions above are desugaring to <code>m >>= \a-> f... </code>or if there is no left arrow on the previous line: <code>m >>= \_-> f...</code> That <code>a</code> in there is an intermediate value, the <code>fst</code> in the tuple. The push function might look like:</p>
<p><blockquote class="vimblock"><br />
push&nbsp;<span class="Statement">::</span>&nbsp;State&nbsp;[a]&nbsp;()<br />
push&nbsp;a&nbsp;<span class="Statement">=</span>&nbsp;State&nbsp;<span class="Statement">$</span>&nbsp;<span class="Statement">\</span>as&nbsp;<span class="Statement">-&gt;</span>&nbsp;(()&nbsp;,&nbsp;a<span class="Statement">:</span>as)<br />
<br /></blockquote></p>
<p>The function doesn&#8217;t return any meaningful <code>a</code> value, so we don&#8217;t bind it by using the <code>< -</code>. For more work with do notation and some fine pictures, see <a href="http://forums.somethingawful.com/showthread.php?threadid=2841145&#038;pagenumber=6#post346173552">Bonus's post on something awful</a>.</p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://coder.bsimmons.name/blog/2009/10/the-state-monad-a-tutorial-for-the-confused/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk
Database Caching 8/15 queries in 0.036 seconds using disk

Served from: coder.bsimmons.name @ 2012-02-05 11:29:05 -->
