yon*_*ong 2 haskell ghc instances
我有这样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 #-}
Run Code Online (Sandbox Code Playgroud)
当试图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’
Run Code Online (Sandbox Code Playgroud)
这对我来说没有意义,因为[]不匹配G.Vector [] a,所以实例如何重叠?
lef*_*out 11
实例仅与实例头匹配.所以,为了重叠或不重叠的目的,你所写的并不比
instance ToAVector [] a
instance ToAVector v a
Run Code Online (Sandbox Code Playgroud)
这显然是重叠的.
争论另一种方式:
......因为
[]不匹配G.Vector [] a......
这在Haskell中永远不是有效的推理,因为类型类是开放的.你永远不会知道某些东西不是某个特定类的实例,因为任何人都可以在以后使它成为一个实例.