-- Jules, Oliver, thanks! Things are getting clarified, I hope.<br>-- Let me summarize how I now understand getAny operation, please correct me if I am wrong. <br><br>getAny :: (Random a) =&gt; State StdGen a<br>getAny = do g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;- get<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (x,g&#39;) &lt;- return $ random g<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; put g&#39;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return x<br><br>{--<br>getAny operation may be abbreviated as:<br><br>do { <br>-- 1) x calculation, equivalent to (x,g2) = random g1<br>-- 2) return x ~&gt; State $ \s -&gt; (x,s) -- puts x into State container&nbsp; <br>

<br>Thus getAny returns a State instantiated with a function which is a composition of several binds &lt;&lt;= from the above &#39;do&#39; block and which calculates &#39;x&#39;<br>--}<br><br>-- Then we can use&nbsp; this State object (returned by getAny) in a function generating random values such as: <br>

<br>makeRnd :: StdGen -&gt; (Int, StdGen)<br>makeRnd = runState (do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y &lt;- getAny<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return y)<br><br>{-- <br>where:<br><br>y &lt;- getAny <br>return y<br><br>passes a first value from the tuple generated by getAny State function&nbsp; into &#39;y&#39; and puts &#39;y&#39; into a new State object.<br>

After that &#39;runState&#39; in makeRnd extracts from this new State a function parametrized by &#39;y&#39; value.<br>As a result we get curried &#39;makeRnd&#39; which we can call with some generator instance and get a random value.<br>

--}<br><br><div class="gmail_quote">On Wed, May 21, 2008 at 10:31 PM, Olivier Boudry &lt;<a href="mailto:olivier.boudry@gmail.com" target="_blank">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>On Wed, May 21, 2008 at 11:10 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><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">


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!<div><div></div><br></div></blockquote></div><div><br>It may be easier to understand the state passing if you remove the do notation and replace get, put and return with their definition in the instance declarations (Monad and MonadState).<div>

<br>
<br>getAny :: (Random a) =&gt; State StdGen a<br>getAny = do g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;- get<br></div><div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (x,g&#39;) &lt;- return $ random g<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; put g&#39;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return x<br><br></div>get = State $ \s -&gt; (s, s) -- copy the state as a return value and pass state<br>


put s = State $ \_ -&gt; ((), s) -- return unit, ignore the passed state and replace it with the state given as parameter.<br>return a = State $ \s -&gt; (a, s) -- return given value and pass state.<br><br>getAnyNoSugar :: (Random a) =&gt; State StdGen a<br>


getAnyNoSugar = (State $ \s -&gt; (s, s)) &gt;&gt;= \g -&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (State $ \s -&gt; (random g, s)) &gt;&gt;= \(x,g&#39;) -&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (State $ \_ -&gt; ((), g&#39;)) &gt;&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (State $ \s -&gt; (x, s))<br>


<br>The function is still useable this way and the state transformations should be a bit more visible. The first element of the tuple is the value that will be used to call the next function (of type Monad m =&gt; a -&gt; m b). The second element of the tuple is the state and the (&gt;&gt;=) operator will handle passing it between actions.<br>


<br>Desugaring the (&gt;&gt;=) and (&gt;&gt;) operators would give you something like this (I replaced `s` with `y` in the `put` and `return` desugaring and simplified it):<br><br>State $ \s = let<br>&nbsp; (g, s&#39;) = (\y -&gt; (y,y)) s<br>


&nbsp; ((x,g&#39;), s&#39;&#39;) = (\y -&gt; (random g, y)) s&#39;<br>&nbsp; (_, s&#39;&#39;&#39;) = (\_ -&gt; ((), g&#39;)) s&#39;&#39;<br>&nbsp; in (x, s&#39;&#39;&#39;)<br><br>Which is explict state passing between function calls. Extract the State using `runState`, run it with an initial state and it should give you the expected result.<br>


<br>Regards,<br><br>Olivier.<br></div></div>
</blockquote></div><br><br clear="all"><br>-- <br>Dmitri O. Kondratiev<br><a href="mailto:dokondr@gmail.com" target="_blank">dokondr@gmail.com</a><br><a href="http://www.geocities.com/dkondr" target="_blank">http://www.geocities.com/dkondr</a>