0
我写一个“简单”的例子执行但我有一个很难搞清楚为什么这个不能编译类型类:无法比拟预期类型的实例
class Euclidean a where
norm :: (Euclidean a, Floating b) => a -> b
data Point a b = Point a b
instance (Floating x, Floating y) => Euclidean (Point x y) where
norm (Point x y) = x
它失败:
Couldn't match expected type ‘b’ with actual type ‘x’
‘x’ is a rigid type variable bound by
the instance declaration at src/Simple.hs:10:10
‘b’ is a rigid type variable bound by
the type signature for
norm :: (Euclidean (Point x y), Floating b) => Point x y -> b
at src/Simple.hs:11:3
Relevant bindings include
x :: x (bound at src/Simple.hs:11:15)
norm :: Point x y -> b (bound at src/Simple.hs:11:3)
In the expression: x
In an equation for ‘norm’: norm (Point x y) = x
注意:所需的功能实现当然是 sqrt $ (x * x) + (y * y)
。
感谢您的及时答复。现在还有一个问题:我怎样才能将'x'的类型与'y'的类型相匹配? 我写了实现为 'norm(Point xy)= realToFrac $ sqrt $(x * x)+(y * y)' (在实例头文件中增加了Real约束到两种类型),但现在它抱怨它不能与'y'匹配'x' – Midiparse
尝试类似于'instance(Real x,Floating x)=> Euclidean(Point xx)其中' – redneb
它抱怨它需要不同的类型变量,并且添加FlexibleConstraints以允许这个 – Midiparse