4

For Vec2, Vec3, and others from Data.Vector.Fixed.Boxed, I see a Functor instance, so something like this type-checks:

fff :: B.Vec2 Int -> B.Vec2 Int
fff = fmap (+1)

and fmap is just Data.Vector.Fixed.map.

However, something doesn't

ggg :: V.Vector v Int => v Int -> v Int
ggg = fmap (+1)

unless I explicitly require that v also be a Functor, like this

ggg :: (Functor v, V.Vector v Int) => v Int -> v Int
ggg = fmap (+1)

even though the function Data.Vector.Fixed.map can be used successfully,

ggg :: V.Vector v Int => v Int -> v Int
ggg = V.map (+1)

So my question is the following: since Data.Vector.Fixed.Vector represents a list-like thing (although of fixed size), why doesn't any instance of it also implement Functor? I would expect it not to implement Monad (even though...), but Functor seems to be doable.

Is there a fundamental reason why Vector is not also Functor?


Assumed

import qualified Data.Vector.Fixed as V       
import qualified Data.Vector.Fixed.Boxed as B
1
  • 1
    The reason is pretty much the same as the one discussed in the other questions: some types of vector (e.g. storable) just can't store arbitrary Haskell values. Commented Jun 25 at 13:20

1 Answer 1

0

It's mentioned in the comments and the other questions, but Functor requires that the container type work with any type argument, as the signature of fmap is

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

and the forall there is crucial.

In contrast, implementations like Data.Vector.Fixed.Storable.Vec only work for Storable types, which certainly not every Haskell type will be. So that particular implementation certainly can't implement the Functor typeclass.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.