<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Thanks very much for both replies.</div><div><br></div><div>I think I get this now.</div><div><br></div><div>Simply, my choice of evaluation functions (evalStateT, execStateT and execState) ensured that the states are not returned. &nbsp;It was obvious.</div><div><br></div><div>I can get this working, but I have one more more question to make sure I actually understand this.</div><div><br></div><div>Below is a very simple and pointless example I wrote to grasp the concept.&nbsp;&nbsp;This&nbsp;returns&nbsp;((1,23),21)&nbsp;which&nbsp;is&nbsp;clear&nbsp;to&nbsp;me.</div><div><br></div><div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;">import Control.Monad.State</span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;"><br></span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;">myOuter :: StateT Int (State Int) Int</span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;">myOuter = StateT $ \s -&gt; do p &lt;- get</span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;">&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;return (s,p+s+1)</span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;"><br></span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;">main :: IO()</span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;">main = do let innerMonad = runStateT myOuter 1</span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = runState innerMonad 21</span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print y</span></font></div></div><div><br></div><div>Thus we are saying that a=(1,23) and s=21 for the state monad, and that a=1 and s=23 for the state transformer.&nbsp;&nbsp;That&nbsp;is&nbsp;the&nbsp;return&nbsp;value&nbsp;of&nbsp;the&nbsp;state&nbsp;monad&nbsp;is&nbsp;the&nbsp;(a,s)&nbsp;tuple&nbsp;of&nbsp;the&nbsp;transformer&nbsp;and&nbsp;it's&nbsp;own&nbsp;state&nbsp;is&nbsp;of&nbsp;course&nbsp;21.</div><div><br></div><div>This&nbsp;got&nbsp;me&nbsp;thinking&nbsp;-&nbsp;the&nbsp;return&nbsp;value's&nbsp;type&nbsp;of&nbsp;the&nbsp;state&nbsp;monad&nbsp;is&nbsp;dictated&nbsp;by&nbsp;the&nbsp;evaluation&nbsp;function&nbsp;used on&nbsp;the&nbsp;state&nbsp;transformer&nbsp;-&nbsp;it&nbsp;could&nbsp;be&nbsp;a,&nbsp;s,&nbsp;or&nbsp;(a,s)&nbsp;depending&nbsp;which&nbsp;function&nbsp;is&nbsp;used.&nbsp;&nbsp;Thus&nbsp;if&nbsp;I&nbsp;edit&nbsp;the&nbsp;code&nbsp;to&nbsp;to:</div><div><br></div><div>do let innerMonad = evalStateT myOuter 1</div><div><br></div><div>I get back (1,21) - which is the problem I had - we've lost the transformer's state.</div><div><br></div><div>Look at the Haskell docs I get:</div><div><br></div><div><a href="http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State-Lazy.html#v%3AevalStateT">evalStateT</a> :: <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html#t%3AMonad">Monad</a> m =&gt; <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State-Lazy.html#t%3AStateT">StateT</a> s m a -&gt; s -&gt; m a</div><div><a href="http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State-Lazy.html#v%3ArunStateT">runStateT</a> :: s -&gt; m (a, s)</div><div><br></div><div>So the transformer valuation functions are returning a State monad initialized with either a or (a,s).</div><div><br></div><div>Now I know from messing around with this that the initialization is the return value, from the constructor:</div><div><br></div><div><span class="keyword">newtype</span>  <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State-Lazy.html#t%3AState">State</a> s a = <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State-Lazy.html#v%3AState">State</a> {<table class="vanilla" cellpadding="0" cellspacing="0"><tbody><tr><td class="recfield"><a href="http://www.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State-Lazy.html#v%3ArunState">runState</a> :: s -&gt; (a, s)</td></tr></tbody></table>}</div><div><div><br></div><div>Am I right in assuming that I can read this as:</div><div><br></div><div>m (a,s_outer) returned from runStateT is equivalent to calling the constructor as (State s_inner) (a,s_outer)</div><div><br></div><div>This makes sense because in the definition of myOuter we don't specify the return value type of the inner monad:</div><div><br></div><div><span class="Apple-style-span" style="font-size: 10px; ">myOuter :: StateT Int (State Int) Int</span></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;"><br></span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;"><br></span></font></div><div><font class="Apple-style-span" size="2"><span class="Apple-style-span" style="font-size: 10px;"><span class="Apple-style-span" style="font-size: medium; ">The problem is whilst I can see that we've defined the inner monad's return value to equal the *type* of the transformer's evaluation function, I'm loosing the plot trying to see how the *values* returned by the transformer are ending up there. &nbsp;We haven't specified what the state monad actually does?</span></span></font></div><div><br></div><div>If I look at a very simple example:</div><div><br></div><div><div>simple :: State Int Int</div><div>simple = State $ \s -&gt; (s,s+1)</div><div><br></div></div><div>This is blindly obvious, is I call 'runState simple 8', I will get back (8,9). &nbsp;Because I've specified that the return value is just the state.</div><div><br></div><div>In the more original example, I can see that the 'return (s,p+s+1)' must produce a state monad where a=(1,23), and the state of this monad is just hardcoded in the code = 21.</div><div><br></div><div>I guess what I'm trying to say is - where is the plumbing that ensures that this returned value in the state/transformer stack is just the (a,s) of the transformer?&nbsp;</div><div><br></div><div><br></div><div>I have a terrible feeling this is a blindly obvious question - apologies if it is!</div><div><br></div><div><br></div><div>Thanks again!</div><div><br></div><div><br></div><div>Phil.</div><div><br></div><div><br></div><div><br></div></div><div><div>On 31 Jul 2009, at 04:39, Ryan Ingram wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>StateT is really simple, so you should be able to figure it out:<br><br>runStateT :: StateT s m a -&gt; s -&gt; m (a,s)<br>runState :: State s a -&gt; s -&gt; (a,s)<br><br>So if you have<br>m :: StateT s1 (StateT s2 (State s3)) a<br><br>runStateT m :: s1 -&gt; StateT s2 (State s3) (a,s)<br><br>\s1 s2 s3 -&gt; runState (runStateT (runStateT m s1) s2) s3)<br> :: s1 -&gt; s2 -&gt; s3 -&gt; (((a,s1), s2), s3)<br><br></div></blockquote></div><br></body></html>