【发布时间】:2016-09-23 16:40:09
【问题描述】:
我正在编写一个用于实现类型类的“简单”示例,但是我很难弄清楚为什么它不能编译:
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).
【问题讨论】: