Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making Vector.Generic an instance of Functor (and other type-classes

I run into trouble trying to make Vector.Generic.Vector an instance of other typeclasses (in my case - Functor).

I could settle for adding a Functor instance to Vector.Unboxed.Vector, but I couldn't figure out the syntax for that either. My best take was to try something like:

instance (U.Unbox a, U.Unbox b) => Functor U.Vector where
    fmap = U.map

But the compiler (justfully) complained that 'a' and 'b' where nowhere after '=>'. Can I even make this definition for Functor, as it assumes more restrictions on the types fmap is allowed to take?

Most stuff I found in SO was too advanced for me to figure out, so please be gentle :-)

like image 681
Uri Barenholz Avatar asked Oct 27 '11 16:10

Uri Barenholz


1 Answers

Think of the type of fmap:

fmap :: Functor f => (a -> b) -> f a -> f b

You are trying to add the constraint that a and b are both instances of Unbox. This is not doable because fmap is completely general while a map of an unboxed Vector is specific to instances Unbox.

It cannot be made a Functor.

like image 94
alternative Avatar answered Sep 24 '22 20:09

alternative