On Wed, May 21, 2008 at 8:42 AM, Dmitri O.Kondratiev &lt;<a href="mailto:dokondr@gmail.com">dokondr@gmail.com</a>&gt; wrote:<br><div class="gmail_quote"><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 class="Ih2E3d"><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><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>
<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 class="Wj3C7c"></div></div></blockquote><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>
<br>Olivier.<br>