<html><body><div>Thanks for the links, I am able to get my head around this a bit. </div><div><br></div><div>Here's my simple question</div><div>1. In the stack example what is the state. As per my understanding the state is the new stack.</div><div>If above statement is correct than by definition of >>=</div><div><br></div><div><blockquote type="cite"><div class="msg-quote"><div dir="ltr"><div data-mce-style="font-size: 12.8000001907349px;" style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"> (>>=) :: State s a -> (a -> State s b) -> State s b</span></div></div></div></blockquote></div><div><div class="msg-quote"><div dir="ltr"><div data-mce-style="font-size: 12.8000001907349px;" style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"><br></span></div></div></div></div><div>push 3 stack </div><div>This returns ((), newstack1)</div><div>Now in this case the state or context is newstack1 which has to be passed to the next function if i apply >>=.</div><div>push 3 stack >>= pop newstack1</div><div>This might make sense coz I am shoving (or binding) the state to the pop function. State in this case is the stack which is being passed around or shoved around. Problem is pop doesn't need "a" argument like the definition of (>>=) indicates. It can very well produce a new state or return a new state. </div><div><br></div><div>So if state = stack then this makes sense to me, where am i wrong...??</div><div><br></div><div>push 3 stack >>= pop newstack1</div><div><br>On Mar 06, 2015, at 09:09 PM, "Sumit Sahrawat, Maths & Computing, IIT (BHU)" <sumit.sahrawat.apm13@iitbhu.ac.in> wrote:<br><br></div><div><blockquote type="cite"><div class="msg-quote"><div dir="ltr">I won't comment on what state exactly is, but you can read up on that and gain some intuition here: <a href="https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State" data-mce-href="https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State">https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State</a><div>It's helpful to implement it using a pen and paper, and consider how the state flows and gets transformed.</div><div><br></div><div>According to the below example,</div><div><br></div><div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"> stackManip stack =</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"> let</span><span style="font-family:monospace,monospace;font-size:12.8000001907349px" data-mce-style="font-family: monospace,monospace; font-size: 12.8000001907349px;"> ((), newStack1) = push 3 stack</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"> (a, newStack2) = pop newStack1</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"> in pop newStack2</span></div></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"><br></span></div>We get,<div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"><br></span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"> push :: a -> Stack a -> ((), Stack a) -- Assuming 'Stack a' is a defined datatype</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"> pop :: Stack a -> (a, Stack a) -- Representing a stack with elements of type 'a'</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"><br></span></div>Thus,<div><br><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"> push 3 >>= pop</span></div></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;">~~ (Stack a -> ((), Stack a)) >>= (Stack a -> (a, Stack a)) { Replacing by types }</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"><br></span></div>Bind (>>=) has the type, (for "State s")<div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><br></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"> (>>=) :: State s a -> (a -> State s b) -> State s b</span></div><div style="font-size:12.8000001907349px" data-mce-style="font-size: 12.8000001907349px;"><span face="monospace, monospace" data-mce-style="font-family: monospace, monospace;" style="font-family: monospace, monospace;"><br></span></div>This is a type mismatch. The conversion to do syntax is at fault here.<div>First, you must write the computation using bind (>>=), and then convert to do-notation.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 7 March 2015 at 10:12, Animesh Saxena <span dir="ltr"><<a href="mailto:animeshsaxena@icloud.com" data-mce-href="mailto:animeshsaxena@icloud.com">animeshsaxena@icloud.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex" data-mce-style="margin: 0 0 0 .8ex; border-left: 1px #ccc solid; padding-left: 1ex;"><div><div>I am trying to relate the state monad to a stack example and somehow found it easy to get recursively confused!</div><div><br></div><div>instance Monad (State s) where</div><div> return x = State $ \s -> (x,s)</div><div> (State h) >>= f = State $ \s -> let (a, newState) = h s</div><div> (State g) = f a</div><div> in g newState</div><div><br></div><div>Considering the stack computation</div><div><br></div><div>stackManip stack = let </div><div> ((), newStack1) = push 3 stack</div><div> (a, newStack2) = pop newStack1</div><div> in pop newStack2</div><div><br></div><div>in do notation this would become </div><div>do </div><div> push 3 </div><div> a <- pop </div><div> pop</div><div><br></div><div>If I consider the first computation push 3 >>= pop and try to translate it to the definition there are problems....</div><div>Copy paste again, I have </div><div><div> (State h) >>= f = State $ \s -> let (a, newState) = h s</div><div> (State g) = f a</div><div> in g newState</div></div><div><br></div><div>f is the push function to which we are shoving the old state. I can't exactly get around to what exactly is the state computation h? Ok assuming it's something which gives me a new state, but then thats push which is function f. </div><div>Then push is applied to a which is assuming 3 in this case. This gives me a new state, which I would say newStack1 from the stockManip above.</div><div><br></div><div>Then somehow I apply g to newState?? All the more confusion. Back to the question what exactly is state computation and how is it different from f? It seems to be the same function?</div><div><br></div><div><br></div><div>-Animesh</div><div><br></div><div><br></div><div><br></div><div><br></div><div> </div></div><br>_______________________________________________<br> Beginners mailing list<br> <a href="mailto:Beginners@haskell.org" data-mce-href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" data-mce-href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br> <br></blockquote></div><br><br><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div>Regards</div><div dir="ltr"><div><br></div><div>Sumit Sahrawat</div></div></div></div></div></div></div></div><div class="_stretch"><span class="body-text-content">_______________________________________________<br>Beginners mailing list<br><a href="mailto:Beginners@haskell.org" data-mce-href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" data-mce-href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br></span></div></div></blockquote></div></body></html>