我正在努力理解多参数类型类,但我不理解实例声明。我开始尝试为向量类型创建一个InnerProductSpace类型类,以便我可以对两个向量执行点积运算。首先我只想看看能否将每个向量的第一个元素相乘。以下是我的代码:
class InnerProductSpace a b c where
dot :: a -> b -> c
data Vector = Vector [Double]
deriving (Show)
instance InnerProductSpace Vector Vector Double where
dot (Vector a) (Vector b) = (head a * head b)
试图使用dot函数后的错误如下:
No instance for (InnerProductSpace Vector Vector c0)
arising from a use of `dot'
The type variable `c0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there is a potential instance available:
instance InnerProductSpace Vector Vector Double
-- Defined at Vector.hs:8:10
Possible fix:
add an instance declaration for
(InnerProductSpace Vector Vector c0)
In the expression: dot a b
In an equation for `it': it = dot a b
我做错了什么?谢谢!