为何Data.Vector.Fixed.Vector的所有实例未实现Functor类型类?
Data.Vector.Fixed 为何不为所有 Vector 实例提供 Functor 实例?
已导入以下模块:
import qualified Data.Vector.Fixed as V import qualified Data.Vector.Fixed.Boxed as B
观察到的现象
对于
Data.Vector.Fixed.Boxed中的Vec2、Vec3等具体类型,它们已实现Functor实例,因此以下代码可正常通过类型检查:fff :: B.Vec2 Int -> B.Vec2 Int fff = fmap (+1)此处的
fmap等价于Data.Vector.Fixed.map。但如果用泛型的
Vector约束编写代码,直接使用fmap会无法通过类型检查:ggg :: V.Vector v Int => v Int -> v Int ggg = fmap (+1)必须显式添加
Functor约束才能通过:ggg :: (Functor v, V.Vector v Int) => v Int -> v Int ggg = fmap (+1)不过直接使用
Data.Vector.Fixed.map函数则可正常运行,无需额外约束:ggg :: V.Vector v Int => v Int -> v Int ggg = V.map (+1)
核心疑问
既然Data.Vector.Fixed.Vector表示固定大小的类列表结构,为什么它的所有实例没有统一实现Functor?我理解这类结构不需要实现Monad,但Functor看起来完全可行。是否存在根本原因导致Vector无法为所有实例关联Functor?
内容的提问来源于stack exchange,提问作者Enlico
相关产品推荐
相关产品推荐

