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. Re: Grappling with State Monad get (Olumide)
----------------------------------------------------------------------
Message: 1
Date: Sat, 5 Aug 2017 00:12:17 +0100
From: Olumide <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Grappling with State Monad get
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed
Thanks Theodore.
I'm still pondering your answer and I've tried to write desugar the
do-notation like so:
stackyStack :: State Stack ()
stackyStack = get >>= (\stackNow -> ... )
I can _sort of_ see how the type of stackyStack 'imposes' a type on the
polymorphic function get, even though I'm not sure that I can precisely
explain *exactly* how it happens. Here is my attempt:
Comparing the definition of bind
(>>=) :: m a -> (a -> m b) -> m b
with the desugared form of stackyStack, it seems to me that a is
stackNow is an [Int], and m is State [Int]. Is this correct?
On a not-too-unrelated note, why is the State (with upper case S) used
in the following instance of the state monad
instance Monad (State s) where
return x = State $ \s -> (x,s)
(State h) >>= f = State $ \s ...
bearing in mind that Control.Monad.State does not expose its value
constructor.
Thanks,
- Olumide
On 02/08/17 03:26, Theodore Lief Gannon wrote:
> Actually, get is constrained immediately to be State Stack Stack. If
> later statements tried to use it as something else, it would be a type
> error.
>
> The types in a do block can only vary in their final parameter. The type
> of stackyStack is State Stack (), so in that do block all the statements
> must be forall a. State Stack a.
>
> Now, check the type of get in GHCi:
> get :: State a a
>
> We know that the first type parameter here is Stack, and the second
> position is the same type, thus State Stack Stack.
>
>
>
> On Tue, Aug 1, 2017 at 5:13 PM, Olumide <[email protected]
> <mailto:[email protected]>> wrote:
>
> Of course 's' is not a type variable as its lowercase.
>
> Therefore, get is a monad 'constructed' by the state function, correct?
>
> So, in the do notation the lambda is extracted and used as per the
> definition of bind. The context of which we speak therefore must
> derive from the next expression(?) in the do notation, which is
> somewhat confusing to determine in the example
>
> stackyStack :: State Stack ()
> stackyStack = do
> stackNow <- get
> if stackNow == [1,2,3]
> then put [8,3,1]
> else put [9,2,1]
>
> Regards,
>
> - Olumide
>
> On 02/08/17 00:35, Theodore Lief Gannon wrote:
>
> 's' here is not a type variable, it's an actual variable. \s ->
> (s, s) defines a lambda function which takes any value, and
> returns a tuple with that value in both positions.
>
> So yes, get is point-free, but the missing argument is right
> there in-line. And yes, its type is simply implied from context.
>
>
> On Aug 1, 2017 4:17 PM, "Olumide" <[email protected]
> <mailto:[email protected]> <mailto:[email protected]
> <mailto:[email protected]>>> wrote:
>
> Ahoy Haskellers,
>
> In the section "Getting and Setting State"
> (http://learnyouahaskell.com/for-a-few-monads-more#state
> <http://learnyouahaskell.com/for-a-few-monads-more#state>
> <http://learnyouahaskell.com/for-a-few-monads-more#state
> <http://learnyouahaskell.com/for-a-few-monads-more#state>>) in LYH
> get is defined as
>
> get = state $ \s -> (s, s)
>
> How does does get determine the type s, is considering that
> it has
> no argument as per the definition given above? Or is the
> definition
> written in some sort of point-free notation where an
> argument has
> been dropped?
>
> I find the line stackNow <- get in the the function(?)
> stackyStack
> confusing for the same reason. I guess my difficulty is
> that state $
> \s ->(s , s) has a generic type (s) whereas the stackyStack
> has a
> concrete type. Is the type of s determined from the type of the
> stateful computation/do notation?
>
> Regards,
>
> - Olumide
> _______________________________________________
> Beginners mailing list
> [email protected] <mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected] <mailto:[email protected]>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
>
> _______________________________________________
> Beginners mailing list
> [email protected] <mailto:[email protected]>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 110, Issue 10
******************************************