[Haskell-beginners] Grappling with State Monad get
Theodore Lief Gannon
tanuki at gmail.com
Wed Aug 2 02:26:57 UTC 2017
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 <50295 at web.de> 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]
> - 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" <50295 at web.de <mailto:50295 at web.de>>
>> Ahoy Haskellers,
>> In the section "Getting and Setting 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?
>> - Olumide
>> Beginners mailing list
>> Beginners at haskell.org <mailto:Beginners at haskell.org>
>> Beginners mailing list
>> Beginners at haskell.org
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners