Functor instance

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

Functor instance

sasa bogicevic
Hi all,
Can someone explain to me why exactly when defining the Functor instance for type that has polimorphic parameter constraint I am not allowed to put that parameter in definition. So basically:

data Four a b c d = Four a b c d

instance Functor (Four a b c) where  <-- why can't I specify also param d here ??
    fmap f (Four a b c d) = Four a b c (f d)

I asked the same question on IRC and got the answer that it is because of Functor has kind * -> *. I thought I understand it but it is not clear to me completely. I understand type kinds on examples I looked at before but
when looking at this Functor instance why exactly I don't specify all params for the type and while we are at it why don't I also specify f param for the Functor like this "instance Functor f (Four a b c d) where ...".

Thanks, Sasa
{
        name: Bogicevic Sasa
        phone: +381606006200
}



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

Re: Functor instance

Francesco Ariis
On Mon, Jan 09, 2017 at 04:41:39PM +0100, sasa bogicevic wrote:

> Hi all,
> Can someone explain to me why exactly when defining the Functor
> instance for type that has polimorphic parameter constraint I
> am not allowed to put that parameter in definition. So basically:
>
> data Four a b c d = Four a b c d
>
> instance Functor (Four a b c) where  <-- why can't I specify also
> param d here ??
>     fmap f (Four a b c d) = Four a b c (f d)

Hello Sasa,
    think for a moment about functors you already know: Maybe a, [a],
(Either e) a, etc. In every case there is an `a` and something preceding
a (we can call it `f`).
Now let's look at the class-definition of functor.

    class Functor f where -- etc. etc.

Here you have it, `f`; so for the instance you should only place
the `f`-part in there, like

    instance Functor Maybe where        -- not Maybe a!
    instance Functor (Four a b c) where -- without the a too!

It makes sense as `f` will stay the same and `a` will be mapped over
(and change).
Indeed you can very this with ghci (using :k Functor, :k Maybe etc.),
but after a while it sinks in.

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

Re: Functor instance

Mihai Maruseac
In reply to this post by sasa bogicevic
If you imagine a type as a box with some slots (each slot for each
type variable), Functor only allows you to map on the last slot. That
and the currying of functions lead to the intuition behind "Functor
has kind * -> *" (aka: you need the type for the last slot to fully
build the type that would be an instance of Functor)

On Mon, Jan 9, 2017 at 7:41 AM, sasa bogicevic <[hidden email]> wrote:

> Hi all,
> Can someone explain to me why exactly when defining the Functor instance for type that has polimorphic parameter constraint I am not allowed to put that parameter in definition. So basically:
>
> data Four a b c d = Four a b c d
>
> instance Functor (Four a b c) where  <-- why can't I specify also param d here ??
>     fmap f (Four a b c d) = Four a b c (f d)
>
> I asked the same question on IRC and got the answer that it is because of Functor has kind * -> *. I thought I understand it but it is not clear to me completely. I understand type kinds on examples I looked at before but
> when looking at this Functor instance why exactly I don't specify all params for the type and while we are at it why don't I also specify f param for the Functor like this "instance Functor f (Four a b c d) where ...".
>
> Thanks, Sasa
> {
>         name: Bogicevic Sasa
>         phone: +381606006200
> }
>
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



--
Mihai Maruseac (MM)
"If you can't solve a problem, then there's an easier problem you can
solve: find it." -- George Polya
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Functor instance

sasa bogicevic
In reply to this post by Francesco Ariis
Good explanation! Thanks I understand it now

Sasa Bogicevic
{
    phone: +381606006200
}

> On Jan 9, 2017, at 17:00, Francesco Ariis <[hidden email]> wrote:
>
>> On Mon, Jan 09, 2017 at 04:41:39PM +0100, sasa bogicevic wrote:
>> Hi all,
>> Can someone explain to me why exactly when defining the Functor
>> instance for type that has polimorphic parameter constraint I
>> am not allowed to put that parameter in definition. So basically:
>>
>> data Four a b c d = Four a b c d
>>
>> instance Functor (Four a b c) where  <-- why can't I specify also
>> param d here ??
>>    fmap f (Four a b c d) = Four a b c (f d)
>
> Hello Sasa,
>    think for a moment about functors you already know: Maybe a, [a],
> (Either e) a, etc. In every case there is an `a` and something preceding
> a (we can call it `f`).
> Now let's look at the class-definition of functor.
>
>    class Functor f where -- etc. etc.
>
> Here you have it, `f`; so for the instance you should only place
> the `f`-part in there, like
>
>    instance Functor Maybe where        -- not Maybe a!
>    instance Functor (Four a b c) where -- without the a too!
>
> It makes sense as `f` will stay the same and `a` will be mapped over
> (and change).
> Indeed you can very this with ghci (using :k Functor, :k Maybe etc.),
> but after a while it sinks in.
>
> Does this help?
> _______________________________________________
> 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
|

Re: Functor instance

David McBride
There is also a BiFunctor in base which allows you to fmap over the
last two variables in your type.

bimap :: (a -> b) -> (c -> d) -> p a c -> p b d

And it conveniently has methods to map over only one at a time, if you want.

first :: (a -> b) -> p a c -> p b c
second :: (b -> c) -> p a b -> p a c

This makes sense for types like Either a b, where sometimes you might
want to fmap over one or both values.

However, going beyond two would cause the number of options for
mapping over such a type to blow up.  At that point it is better to
just use your own simple function to map over your own type.

On Mon, Jan 9, 2017 at 11:26 AM, sasa bogicevic <[hidden email]> wrote:

> Good explanation! Thanks I understand it now
>
> Sasa Bogicevic
> {
>     phone: +381606006200
> }
>
>> On Jan 9, 2017, at 17:00, Francesco Ariis <[hidden email]> wrote:
>>
>>> On Mon, Jan 09, 2017 at 04:41:39PM +0100, sasa bogicevic wrote:
>>> Hi all,
>>> Can someone explain to me why exactly when defining the Functor
>>> instance for type that has polimorphic parameter constraint I
>>> am not allowed to put that parameter in definition. So basically:
>>>
>>> data Four a b c d = Four a b c d
>>>
>>> instance Functor (Four a b c) where  <-- why can't I specify also
>>> param d here ??
>>>    fmap f (Four a b c d) = Four a b c (f d)
>>
>> Hello Sasa,
>>    think for a moment about functors you already know: Maybe a, [a],
>> (Either e) a, etc. In every case there is an `a` and something preceding
>> a (we can call it `f`).
>> Now let's look at the class-definition of functor.
>>
>>    class Functor f where -- etc. etc.
>>
>> Here you have it, `f`; so for the instance you should only place
>> the `f`-part in there, like
>>
>>    instance Functor Maybe where        -- not Maybe a!
>>    instance Functor (Four a b c) where -- without the a too!
>>
>> It makes sense as `f` will stay the same and `a` will be mapped over
>> (and change).
>> Indeed you can very this with ghci (using :k Functor, :k Maybe etc.),
>> but after a while it sinks in.
>>
>> Does this help?
>> _______________________________________________
>> 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
|

Re: Functor instance

sasa bogicevic
I will look at bimap too, sounds usefull, thanks!

Sasa Bogicevic
{
    phone: +381606006200
}

> On Jan 9, 2017, at 17:40, David McBride <[hidden email]> wrote:
>
> There is also a BiFunctor in base which allows you to fmap over the
> last two variables in your type.
>
> bimap :: (a -> b) -> (c -> d) -> p a c -> p b d
>
> And it conveniently has methods to map over only one at a time, if you want.
>
> first :: (a -> b) -> p a c -> p b c
> second :: (b -> c) -> p a b -> p a c
>
> This makes sense for types like Either a b, where sometimes you might
> want to fmap over one or both values.
>
> However, going beyond two would cause the number of options for
> mapping over such a type to blow up.  At that point it is better to
> just use your own simple function to map over your own type.
>
>> On Mon, Jan 9, 2017 at 11:26 AM, sasa bogicevic <[hidden email]> wrote:
>> Good explanation! Thanks I understand it now
>>
>> Sasa Bogicevic
>> {
>>    phone: +381606006200
>> }
>>
>>>> On Jan 9, 2017, at 17:00, Francesco Ariis <[hidden email]> wrote:
>>>>
>>>> On Mon, Jan 09, 2017 at 04:41:39PM +0100, sasa bogicevic wrote:
>>>> Hi all,
>>>> Can someone explain to me why exactly when defining the Functor
>>>> instance for type that has polimorphic parameter constraint I
>>>> am not allowed to put that parameter in definition. So basically:
>>>>
>>>> data Four a b c d = Four a b c d
>>>>
>>>> instance Functor (Four a b c) where  <-- why can't I specify also
>>>> param d here ??
>>>>   fmap f (Four a b c d) = Four a b c (f d)
>>>
>>> Hello Sasa,
>>>   think for a moment about functors you already know: Maybe a, [a],
>>> (Either e) a, etc. In every case there is an `a` and something preceding
>>> a (we can call it `f`).
>>> Now let's look at the class-definition of functor.
>>>
>>>   class Functor f where -- etc. etc.
>>>
>>> Here you have it, `f`; so for the instance you should only place
>>> the `f`-part in there, like
>>>
>>>   instance Functor Maybe where        -- not Maybe a!
>>>   instance Functor (Four a b c) where -- without the a too!
>>>
>>> It makes sense as `f` will stay the same and `a` will be mapped over
>>> (and change).
>>> Indeed you can very this with ghci (using :k Functor, :k Maybe etc.),
>>> but after a while it sinks in.
>>>
>>> Does this help?
>>> _______________________________________________
>>> 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