[Haskell-beginners] Functor instance
Francesco Ariis
fa-ml at ariis.it
Mon Jan 9 16:00:55 UTC 2017
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?
More information about the Beginners
mailing list