Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  State Monad stack example (Animesh Saxena)
   2. Re:  State Monad stack example
      (Sumit Sahrawat, Maths & Computing, IIT (BHU))
   3. Re:  State Monad stack example (Benjamin Edwards)


----------------------------------------------------------------------

Message: 1
Date: Sat, 07 Mar 2015 04:42:21 +0000 (GMT)
From: Animesh Saxena <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] State Monad stack example
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

I am trying to relate the state monad to a stack example and somehow found it 
easy to get recursively confused!

instance Monad (State s) where
? ? return x = State $ \s -> (x,s)
? ? (State h) >>= f = State $ \s -> let (a, newState) = h s
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (State g) = f a
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?in g newState

Considering the stack computation

stackManip stack = let?
? ? ? ? ? ? ? ((), newStack1) = push 3 stack
? ? ? ? ? ? ? (a, newStack2) = pop newStack1
? ? ? ? ? ? ? ?in pop newStack2

in do notation this would become?
do ?
? ?push 3 ?
? ?a <- pop ?
? ?pop

If I consider the first computation push 3 >>= pop and try to translate it to 
the definition there are problems....
Copy paste again, I have?
? ? (State h) >>= f = State $ \s -> let (a, newState) = h s
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (State g) = f a
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?in g newState

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.?
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.

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?


-Animesh




? ? ? ? ? ? ? ? ? ? ? ? ? ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150307/e2a74aa2/attachment-0001.html>

------------------------------

Message: 2
Date: Sat, 7 Mar 2015 10:39:00 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
        <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] State Monad stack example
Message-ID:
        <cajbew8mcyux_41bpq+bmhqoo7khlk5av126jxwmvumebhvu...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I won't comment on what state exactly is, but you can read up on that and
gain some intuition here:
https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State
It's helpful to implement it using a pen and paper, and consider how the
state flows and gets transformed.

According to the below example,

  stackManip stack =
    let ((), newStack1) = push 3 stack
        (a, newStack2)  = pop newStack1
    in pop newStack2

We get,

  push :: a -> Stack a -> ((), Stack a)    -- Assuming 'Stack a' is a
defined datatype
  pop  :: Stack a -> (a, Stack a)          -- Representing a stack with
elements of type 'a'

Thus,

    push 3 >>= pop
~~  (Stack a -> ((), Stack a)) >>= (Stack a -> (a, Stack a))
{ Replacing by types }

Bind (>>=) has the type, (for "State s")

  (>>=) :: State s a -> (a -> State s b) -> State s b

This is a type mismatch. The conversion to do syntax is at fault here.
First, you must write the computation using bind (>>=), and then convert to
do-notation.

On 7 March 2015 at 10:12, Animesh Saxena <[email protected]> wrote:

> I am trying to relate the state monad to a stack example and somehow found
> it easy to get recursively confused!
>
> instance Monad (State s) where
>     return x = State $ \s -> (x,s)
>     (State h) >>= f = State $ \s -> let (a, newState) = h s
>                                                     (State g) = f a
>                                                      in g newState
>
> Considering the stack computation
>
> stackManip stack = let
>               ((), newStack1) = push 3 stack
>               (a, newStack2) = pop newStack1
>                in pop newStack2
>
> in do notation this would become
> do
>    push 3
>    a <- pop
>    pop
>
> If I consider the first computation push 3 >>= pop and try to translate it
> to the definition there are problems....
> Copy paste again, I have
>     (State h) >>= f = State $ \s -> let (a, newState) = h s
>                                                     (State g) = f a
>                                                      in g newState
>
> 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.
> 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.
>
> 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?
>
>
> -Animesh
>
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>


-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150307/3eaff99d/attachment-0001.html>

------------------------------

Message: 3
Date: Sat, 07 Mar 2015 09:45:00 +0000
From: Benjamin Edwards <[email protected]>
To: [email protected],  The Haskell-Beginners Mailing
        List - Discussion of primarily beginner-level topics related to
        Haskell <[email protected]>
Subject: Re: [Haskell-beginners] State Monad stack example
Message-ID:
        <CAN6k4nhfPpzRE0g-X_dN7mFBqpmyi_Zg9xO=7=l5al1-ccu...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I gave an expansion of the state monad for a different computation on
Reddit some time ago. Perhaps it will be useful to you:

http://www.reddit.com/r/haskell/comments/25fnrj/nicta_course_help_with_state_exercise/

Best,
Ben

On Sat, 7 Mar 2015 5:09 am Sumit Sahrawat, Maths & Computing, IIT (BHU) <
[email protected]> wrote:

> I won't comment on what state exactly is, but you can read up on that and
> gain some intuition here:
> https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State
> It's helpful to implement it using a pen and paper, and consider how the
> state flows and gets transformed.
>
> According to the below example,
>
>   stackManip stack =
>     let ((), newStack1) = push 3 stack
>         (a, newStack2)  = pop newStack1
>     in pop newStack2
>
> We get,
>
>   push :: a -> Stack a -> ((), Stack a)    -- Assuming 'Stack a' is a
> defined datatype
>   pop  :: Stack a -> (a, Stack a)          -- Representing a stack with
> elements of type 'a'
>
> Thus,
>
>     push 3 >>= pop
> ~~  (Stack a -> ((), Stack a)) >>= (Stack a -> (a, Stack a))
> { Replacing by types }
>
> Bind (>>=) has the type, (for "State s")
>
>   (>>=) :: State s a -> (a -> State s b) -> State s b
>
> This is a type mismatch. The conversion to do syntax is at fault here.
> First, you must write the computation using bind (>>=), and then convert
> to do-notation.
>
> On 7 March 2015 at 10:12, Animesh Saxena <[email protected]> wrote:
>
>> I am trying to relate the state monad to a stack example and somehow
>> found it easy to get recursively confused!
>>
>> instance Monad (State s) where
>>     return x = State $ \s -> (x,s)
>>     (State h) >>= f = State $ \s -> let (a, newState) = h s
>>                                                     (State g) = f a
>>                                                      in g newState
>>
>> Considering the stack computation
>>
>> stackManip stack = let
>>               ((), newStack1) = push 3 stack
>>               (a, newStack2) = pop newStack1
>>                in pop newStack2
>>
>> in do notation this would become
>> do
>>    push 3
>>    a <- pop
>>    pop
>>
>> If I consider the first computation push 3 >>= pop and try to translate
>> it to the definition there are problems....
>> Copy paste again, I have
>>     (State h) >>= f = State $ \s -> let (a, newState) = h s
>>                                                     (State g) = f a
>>                                                      in g newState
>>
>> 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.
>> 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.
>>
>> 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?
>>
>>
>> -Animesh
>>
>>
>>
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
>
> --
> Regards
>
> Sumit Sahrawat
>  _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150307/db5198a9/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 81, Issue 27
*****************************************

Reply via email to