Functor instance for FunPB

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

Functor instance for FunPB

Alex Wede
Hey Everyone,


ich have a algebraic data type:


data FunPB a b = FunPB { runFunPB :: a -> (a,[b]) }


I got a Monoid Instance

instance Monoid (FunPB a b) where
   mempty  = FunPB $ \k -> (k,mempty)
   mappend pb1 pb2 = FunPB $ \n -> (,) n $ msum . (<$>) (uncurry (flip
const).((flip runFunPB) n)) $ [pb1,pb2]

Now I need to write a functor instance

instance Functor (FunPB a) where

     fmap f b = ?


How can I map over a->(a,[b])?


Lg

Alex

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Functor instance for FunPB

MarLinn

>   mappend pb1 pb2 = FunPB $ \n -> (,) n $ msum . (<$>) (uncurry (flip
> const).((flip runFunPB) n)) $ [pb1,pb2]

This cracks me up. Well played. Well played indeed.

Well all the things you need are right here in this line, right? I mean
you sent it because you want to show us what you know, right? There's
higher order functions, there's mapping and folding and currying of
different kinds and everything. So what's missing?

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Functor instance for FunPB

Ruben Astudillo
In reply to this post by Alex Wede
On 06/05/17 14:50, Alex Wede wrote:
> Now I need to write a functor instance
>
> instance Functor (FunPB a) where
>
>     fmap f b = ?

It will help to see the specific types of the instance

    instance Functor (FunPB a) where

        fmap :: (c -> d) -> FunPB a c -> FunPB a d

ie we need to change only the second argument of FunPB a c. By you
definition, that would be the [b] in

    data FunPB a b = FunPB { runFunPB :: a -> (a,[b]) }

so you can remove the constructor to get the inner function, create a
new function that uses the inner one and fmap the second part of the
tuple

    instance Functor (FunPB a) where

        fmap f (FunPB lambda) =
            FunPB $ \a1 -> let (a2, bs) = lambda a1
                           in  (a2 , fmap f bs)

see that we have a `fmap f bs` in the second part, we use that we
already have a Functor instance for [b], making our job easier.


--
-- Ruben
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Functor instance for FunPB

Alex Wede
In reply to this post by MarLinn
Hey,


No, I do not want to show anything.

The monoid instance is from my teacher. It is an exercise. Sry for the
misunderstanding.


LG


Alex


Am 06.05.2017 um 20:25 schrieb MarLinn:

>
>>   mappend pb1 pb2 = FunPB $ \n -> (,) n $ msum . (<$>) (uncurry (flip
>> const).((flip runFunPB) n)) $ [pb1,pb2]
>
> This cracks me up. Well played. Well played indeed.
>
> Well all the things you need are right here in this line, right? I
> mean you sent it because you want to show us what you know, right?
> There's higher order functions, there's mapping and folding and
> currying of different kinds and everything. So what's missing?
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Functor instance for FunPB

David Kraeutmann
In reply to this post by Alex Wede
The only lawful instance I can think of is:
instance Functor (FunPB a) where
  fmap f (FunPB g) = FunPB $ \k -> let (a,b) = g k in (a, fmap f b)

which is fairly straightforward IMO.


On 2017-05-06 19:50, Alex Wede wrote:

> Hey Everyone,
>
>
> ich have a algebraic data type:
>
>
> data FunPB a b = FunPB { runFunPB :: a -> (a,[b]) }
>
>
> I got a Monoid Instance
>
> instance Monoid (FunPB a b) where
>   mempty  = FunPB $ \k -> (k,mempty)
>   mappend pb1 pb2 = FunPB $ \n -> (,) n $ msum . (<$>) (uncurry (flip
> const).((flip runFunPB) n)) $ [pb1,pb2]
>
> Now I need to write a functor instance
>
> instance Functor (FunPB a) where
>
>     fmap f b = ?
>
>
> How can I map over a->(a,[b])?
>
>
> Lg
>
> Alex
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Functor instance for FunPB

Joachim Breitner-2
In reply to this post by Alex Wede
Hi,

Am Samstag, den 06.05.2017, 20:32 +0200 schrieb Alex Wede:
> The monoid instance is from my teacher.

it is hard to tell with confidence, given that we do not know what
FunFB is supposed to “mean”, but

data FunPB a b = FunPB { runFunPB :: a -> (a,[b]) }
instance Monoid (FunPB a b) where
   mempty  = FunPB $ \k -> (k,mempty)
   mappend pb1 pb2 = FunPB $ \n -> (,) n $ msum . (<$>) (uncurry (flip const).((flip runFunPB) n)) $ [pb1,pb2]

looks very fishy, as the return value of of type a from pb1 and pb2 are ignored in mappend

The latter means that this is not a lawful monoid, because for

  pb = (\n -> (n+1, [])

we have

  mempty `mappend` pb == mempty /= pb


Did you maybe mean

   mappend pb1 pb2 = FunPB  $ \n0 ->
        let (n1,xs1) = runFunFB n1
            (n2,xs2) = runFunFB n2
        in (n2, xs1 ++ xs2)

Greetings,
Joachim


--
Joachim Breitner
  [hidden email]
  http://www.joachim-breitner.de/

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Functor instance for FunPB

MarLinn
In reply to this post by David Kraeutmann

On 2017-05-06 20:38, David Kraeutmann wrote:
> The only lawful instance I can think of is:
> instance Functor (FunPB a) where
>    fmap f (FunPB g) = FunPB $ \k -> let (a,b) = g k in (a, fmap f b)
>
> which is fairly straightforward IMO.
And because it's closer to the original style:

        fmap f = FunPB .((.)((<$>).(<$>)$f)). runFunPB

Now it's STARRING INTO YOU SOUL. And it's also so much easier to read.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Functor instance for FunPB

Daniel Díaz Casanueva
Hallo.

You can try reusing fmap.

>>>

data FunPB a b = FunPB { runFunPB :: a -> (a,[b]) 

fmap f (FunPB g) = FunPB $ fmap (fmap (fmap f)) g

>>>

This works because (a->), (a,), and [] are all functors.

Freundliche Grüße,
Daniel.

On Sat, May 6, 2017 at 9:04 PM, MarLinn <[hidden email]> wrote:

On 2017-05-06 20:38, David Kraeutmann wrote:
The only lawful instance I can think of is:
instance Functor (FunPB a) where
   fmap f (FunPB g) = FunPB $ \k -> let (a,b) = g k in (a, fmap f b)

which is fairly straightforward IMO.
And because it's closer to the original style:

        fmap f = FunPB .((.)((<$>).(<$>)$f)). runFunPB

Now it's STARRING INTO YOU SOUL. And it's also so much easier to read.


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.