我试图理解多参数类型类,但我只是没有得到实例声明。我开始尝试为 Vector 类型创建一个 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)
尝试使用点函数后的错误是
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
我做错了什么?谢谢!