State is a data type. As any other data type it can be instantiated. State instance is a structure of one record that contains (\s -&gt;(a,s)) lambda function. This function can be parametrized by types of its arguments &#39;s&#39; and &#39;a&#39;. I don&#39;t see magic here :)<br>
<br>Ok, then from declaration:<br><br>getAny :: (Random a) =&gt; State StdGen a<br>getAny = do g &lt;- get<br><br>we can say that looking at type &#39;State StdGen a&#39; compiler concludes that later on in the &#39;do&#39; block statements like:<br>
<br>g &lt;- get<br><br>will resolve into bind function (&gt;&gt;=) *as bind is defined for State monad*.<br>Fine, I assume compiler is capable of such reasoning.<br><br>Then<br>g &lt;- get<br>may be written as:<br><br>get &gt;&gt;= \g -&gt; ...<br>
<br>To understand how State monad work, I wrote MyState data type that emulates State and (&gt;=&gt;) &#39;bind&#39; function that emulates &#39;real&#39; bind (&gt;&gt;=) implementation for State monad:<br><br>(&gt;=&gt;) :: MyState StdGen Int -&gt; (Int -&gt; MyState StdGen Int) -&gt;&nbsp; MyState StdGen Int<br>
(MyState ms) &gt;=&gt; fn =&nbsp; MyState(\seed -&gt; let(v1, newSeed) = ms seed<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ms2 = fn v1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in (runState ms2) newSeed)<br><br>Inserting &#39;get&#39; into &gt;&gt;= (or &gt;=&gt; in my code) will in fact result in thinking about State instance that &#39;get&#39; returns as denoted by &#39;ms&#39; in this code of mine.<br>
>From &#39;get&#39; definition follows that function hiding behind &#39;ms&#39; State instance is:<br><br>\s -&gt; (s,s) <br><br>So when later we will feed generator &#39;g1&#39; into this function will get: (g1,g1)<br>And we also will get:<br>
v1 = g1 <br>newSeed = g1<br>ms2 = fn g1<br><br>and finally &#39;g&#39; in expression &#39;g &lt;- get&#39; will be equal to &#39;g1&#39; that will be later fed in through the function call:<br><br>&#39;makeRandomValueST g1&#39;<br>
<br>But how will &#39;g1&#39; actually get delivered from &#39;makeRandomValueST g1&#39; to invocation of &#39;getAny&#39; I don&#39;t yet understand!<br><br><div class="gmail_quote"><br>On Wed, May 21, 2008 at 5:55 PM, Olivier Boudry &lt;<a href="mailto:olivier.boudry@gmail.com">olivier.boudry@gmail.com</a>&gt; wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d">On Wed, May 21, 2008 at 8:42 AM, Dmitri O.Kondratiev &lt;<a href="mailto:dokondr@gmail.com" target="_blank">dokondr@gmail.com</a>&gt; wrote:<br>
</div><div class="gmail_quote"><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
So let&#39;s start with fundamental and most intriguing&nbsp;  (to me) things:<div><br><br>getAny :: (Random a) =&gt; State StdGen a<br>getAny = do g &lt;- get -- magically get the current StdGen<br><br></div>First line above declares a data type:<br>


<br>State StdGen a<br><br>which is constructed with the function:<br><br>State {runState :: (StdGen -&gt; (a, StdGen))}<br><br>Q1: Where in the example (<a href="http://www.haskell.org/all_about_monads/examples/example15.hs" target="_blank">http://www.haskell.org/all_about_monads/examples/example15.hs</a>) data of this type *actually gets constructed* ?</blockquote>

</div><div><br>In getAny and getOne. Their signature has type `State StdGen a`. The use of the do notation to chain the actions and the use of get and put from the State Monad make this function a `State StdGen a`.<br><br>
</div><div class="Ih2E3d">
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>Looking at example15.hs code we see the following sequence:<br><br>1) makeRandomValue g -- where g is a StdGen instance, ok<br><br>2) makeRandomValue g ~&gt; expands into ~&gt;<br><br>~&gt;&nbsp; (runState (do { ...; b &lt;- getAny;...})) g<br>


<br><br>This last expression puzzles me. I can understand, for example, this:<br><br>State StdGen a :: aState<br>StdGen:: g1<br><br>(v, g2) = (runStae aState) g1 -- this returns a state function which is then passed a generator g1, and as result returns pair (value, new generaor)<br>


<br>But &#39;(runState (do ...)) g&#39; implies that expression (do ...)&nbsp; must be somehow of type &#39;State StdGen a&#39; ?<br>Yet, when we call &#39;makeRandomValue g&#39; we just pass to this function g::StgGen<br><br>


So, my next question:<br>Q2: How (do {...;b &lt;- getAny;...}) becomes an *instance* of type &#39;State StdGen a&#39; ?<div><div></div><div></div></div></blockquote></div><div><br>In 2) I suppose you&#39;re talking of `makeRandomValueST` as `makeRandomValue` is the function that runs without the State Monad.<br>

<br>makeRandomValueST does not build a `State StdGen a` it uses `runState` to run the (do block) which has type `State StdGen a`.<br><br>Using `runState` will run an action which has `State s a` type on an initial state `s` and return a `(a, s)` tuple.<br>

<br>`makeRandomValueST` does just the same using its parameter `g :: StdGen` as initial state and returning a tuple of type `(MyType, StdGen)`. Now what makes the do-block used in `runState` an instance of type `State StdGen a` is type inference. `runState` expects a `State s a` as first argument and `s` as second argument. The function signature, the use of `&gt;&gt;=` and `return` (desugared do-block) to combine actions and the use of actions already having that type like `getAny` and `getOne` will make your do block a `State StdGen a`.<br>

<br>I&#39;m not sure we can talk of building an instance of `State s a`. It&#39;s a &quot;parameterized variant&quot; of `State s a` which itself is an instance of the Monad class. We&#39;re just assigning types to the `s` and `a` type variables in `State s a`.<br>

</div></div><br>In short `runState` takes the value (s -&gt; (a, s)) out of the State monad. In the case of the State Monad that value is a function and it is run on the initial state. Its usually what runXXXXX functions do. They have type `(Monad m) =&gt; m a -&gt; a`.<br>

<br>Actions in the State Monad have type `State (s -&gt; (a, s))`. The value stored in the State constructor is a function. Combining two actions using the `&gt;&gt;=` and `&gt;&gt;` functions (hidden or not in a do-block) just create a bigger `s -&gt; (a, s)` function. The function is &quot;hidden&quot; in a `State` constructor just to ensure you don&#39;t run it when you don&#39;t want to. When you whant to run the &quot;big function&quot; you first have to take it out of the State constructor using the accessor `runState` and then run it on the initial state. The end result is of course a (a, s) tuple.<br>

<br>Clear as mud, isn&#39;t it? It tooks me lots of time to understand how the State Monad works. I read many tutorial and still understood nothing about it. Its only by looking at the source code, playing with it and trying to rewrite the State Monad that I finally got an understanding of it. So I&#39;m not sure you&#39;ll get it before you go through the same kind of path.<br>

<br>The key to understand this Monad, at least based on my experience, is to keep in mind that `&gt;&gt;=` just assembles small state passing functions into bigger ones, but does not run the built function until you explicitly use the `runState` function on it.<br>
<font color="#888888">
<br>Olivier.<br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Dmitri O. Kondratiev<br><a href="mailto:dokondr@gmail.com">dokondr@gmail.com</a><br><a href="http://www.geocities.com/dkondr">http://www.geocities.com/dkondr</a>