# Grappling with State Monad get

 Classic List Threaded
7 messages
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

## Grappling with State Monad get

 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? Regards, - Olumide _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Grappling with State Monad get

 '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" <[hidden email]> wrote: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? Regards, - Olumide _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Grappling with State Monad get

 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" <[hidden email] > > wrote: > >     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? > >     Regards, > >     - Olumide >     _______________________________________________ >     Beginners mailing list >     [hidden email] >     http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>     > > > > > _______________________________________________ > Beginners mailing list > [hidden email] > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners> _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Grappling with State Monad get

 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 aWe 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 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" <[hidden email] > wrote:     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?     Regards,     - Olumide     _______________________________________________     Beginners mailing list     [hidden email]     http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners     _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Grappling with State Monad get

 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 <[hidden email] > > 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" <[hidden email] >                 >> wrote: > >              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? > >              Regards, > >              - Olumide >              _______________________________________________ >              Beginners mailing list >         [hidden email] >         > >         http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>         >                       > > > > > >         _______________________________________________ >         Beginners mailing list >         [hidden email] >         http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>         > >     _______________________________________________ >     Beginners mailing list >     [hidden email] >     http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>     > > > > > _______________________________________________ > Beginners mailing list > [hidden email] > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners> _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Grappling with State Monad get

 In reply to this post by Olumide On Tue, Aug 1, 2017 at 5:14 PM Olumide <[hidden email]> wrote:Of course 's' is not a type variable as its lowercase.But type variables are lowercase.  _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Grappling with State Monad get

 Type I meant to say. Thank for the correction. On 08/08/17 07:12, Rein Henrichs wrote: > On Tue, Aug 1, 2017 at 5:14 PM Olumide <[hidden email] > > wrote: > >     Of course 's' is not a type variable as its lowercase. > > > But type variables *are* lowercase. > > > _______________________________________________ > Beginners mailing list > [hidden email] > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners> _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Loading...