[英]GHC complains about overlapping instances when in fact they are not
我的函数toAVector
定义如下:
class Elt a => ToAVector v a where
toAVector :: v a -> A.Array DIM1 a
instance Elt a => ToAVector [] a where
toAVector l =
A.fromList (Z :. P.length l) l
instance (Elt a, G.Vector v a) => ToAVector v a where
toAVector v =
A.fromFunction (Z :. G.length v) (\(Z :. i) -> v G.! i)
{-# INLINE toAVector #-}
当尝试在另一个库中使用toAVector
时,我收到错误:
Overlapping instances for ToAVector [] Double
arising from a use of ‘toAVector’
Matching instances:
instance (A.Elt a, G.Vector v a) => ToAVector v a
-- Defined in ‘Data.Array.Accelerate.Utils’
instance A.Elt a => ToAVector [] a
-- Defined in ‘Data.Array.Accelerate.Utils’
这对我来说没有意义,因为[]
与G.Vector [] a
不匹配,那么实例怎么可能重叠?
实例仅与实例头匹配 。 所以,为了重叠或不重叠的目的,你所写的并不比
instance ToAVector [] a
instance ToAVector v a
这显然是重叠的。
争论另一种方式:
......因为
[]
不匹配G.Vector [] a
...
这在Haskell中永远不是有效的推理,因为类型类是开放的 。 你永远不会知道某些东西不是某个特定类的实例,因为任何人都可以在以后使它成为一个实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.