这是我的 GADT:
data Vector v where
Zero :: (Num a, Eq a, Show a) => Vector a
Scalar :: (Num a, Eq a, Show a) => a -> Vector a
Vector :: (Num a, Eq a, Show a) => [a] -> Vector [a]
TVector :: (Num a, Eq a, Show a) => [a] -> Vector [a]
基本上,它表示一个向量,其中包含属于类型类 Num、Eq 和 Show 的元素。
我希望 Vector 本身是一个 Num,所以我执行以下操作:
instance Num (Vector v) where
(Zero) + (Zero) = Zero
-- etc
(Vector a) * (Scalar b) = Vector $ map (* b) a -- This does not work
当我运行代码时,我得到:
Could not deduce (a ~ [a])
from the context (v ~ [a], Num a, Eq a, Show a)
bound by a pattern with constructor
Vector :: forall a. (Num a, Eq a, Show a) => [a] -> Vector [a],
in an equation for `*'
为什么会发生这种情况,我该如何解决?