Applicative for State

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Applicative for State

Mike Houghton
Hi,

I have a State by another name, Stat,  just to experiment and learn.

newtype Stat s a = Stat { runStat :: s -> (a, s) }

instance Functor (Stat s) where
    fmap f (Stat g) = Stat $ \s -> (f a, s) where
        (a, s) =  g s
             

instance Applicative (Stat s) where
    pure a = Stat $ \s -> (a, s)

    (Stat f) <*> (Stat g) = Stat $ \s -> (a, s) where
        (a, s) = undefined

I really can’t get what the <*> in the Applicative should be!
I just do see how I ‘get the f out of the Stat’ and then apply it.

I’d be really grateful if someone would explain what it should be and the steps/reasoning needed to get there.


Many thanks

Mike

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Applicative for State

Francesco Ariis
On Mon, Feb 06, 2017 at 07:40:12PM +0000, mike h wrote:
> I have a State by another name, Stat,  just to experiment and learn.
> [...]
> I really can’t get what the <*> in the Applicative should be!
> I just do see how I ‘get the f out of the Stat’ and then apply it.
>
> I’d be really grateful if someone would explain what it should be and
> the steps/reasoning needed to get there.

Hello Mike,

    when writing an instance, you always have to keep in mind: (a) the
signature of the function you are writing and (b) what the instance
is designed to do.

In our case, (<*>) is:

    (<*>) :: Applicative f => f (a -> b) -> f a -> f b

    -- which we could 'rewrite' as
    (<*>) ::  Stat s (a -> b) -> Stat s a -> Stat s b

so we grab the results, one being a function and the other a value,
and apply the first to the second.

(b) is "pass the state around in the background". Good, let's put this
in action:

    (Stat f) <*> (Stat g) = Stat $ \s ->
        let (h, s')  = f s     -- h is a function :: a -> b
            (a, s'') = g s'    -- state passing
            b        = h a in  -- the application
        (b, s'') -- we're not returning just the tuple, we're returning
                 -- even the bit before the 'let' statement

And that is that. Was this clear?
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Applicative for State

Mike Houghton
Thanks again Francesco.
Part of my problem was confusing the data and type constructors.
With your solution and my renaming of the data constructors it all became much clearer!

:)

Mike


> On 6 Feb 2017, at 20:15, Francesco Ariis <[hidden email]> wrote:
>
> On Mon, Feb 06, 2017 at 07:40:12PM +0000, mike h wrote:
>> I have a State by another name, Stat,  just to experiment and learn.
>> [...]
>> I really can’t get what the <*> in the Applicative should be!
>> I just do see how I ‘get the f out of the Stat’ and then apply it.
>>
>> I’d be really grateful if someone would explain what it should be and
>> the steps/reasoning needed to get there.
>
> Hello Mike,
>
>    when writing an instance, you always have to keep in mind: (a) the
> signature of the function you are writing and (b) what the instance
> is designed to do.
>
> In our case, (<*>) is:
>
>    (<*>) :: Applicative f => f (a -> b) -> f a -> f b
>
>    -- which we could 'rewrite' as
>    (<*>) ::  Stat s (a -> b) -> Stat s a -> Stat s b
>
> so we grab the results, one being a function and the other a value,
> and apply the first to the second.
>
> (b) is "pass the state around in the background". Good, let's put this
> in action:
>
>    (Stat f) <*> (Stat g) = Stat $ \s ->
>        let (h, s')  = f s     -- h is a function :: a -> b
>            (a, s'') = g s'    -- state passing
>            b        = h a in  -- the application
>        (b, s'') -- we're not returning just the tuple, we're returning
>                 -- even the bit before the 'let' statement
>
> And that is that. Was this clear?
> _______________________________________________
> 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