【发布时间】:2019-06-28 14:46:27
【问题描述】:
我是 Haskell 的新手,现在正在学习类型课程。我创建了一个简单的类型类,但不幸的是我无法修复
无法匹配预期类型
-- So a can be Int, Float etc
data MyShape a = Circle a -- etc like | Rectangle a a
deriving(Show)
class Geo a where
inflate :: Num b => a -> b -> a
area :: Num b => a -> b
instance Num a => Geo (MyShape a) where
inflate (Circle r) x = Circle (r * x)
area (Circle r) = 3 * r * r
它不喜欢膨胀和面积函数。
错误:
• Couldn't match expected type ‘a’ with actual type ‘b’
‘b’ is a rigid type variable bound by
the type signature for:
inflate :: forall b. Num b => MyShape a -> b -> MyShape a
at test2.hs:122:3-9
‘a’ is a rigid type variable bound by
the instance declaration
at test2.hs:121:10-33
• In the second argument of ‘(*)’, namely ‘x’
In the first argument of ‘Circle’, namely ‘(r * x)’
In the expression: Circle (r * x)
• Relevant bindings include
x :: b (bound at test2.hs:122:22)
r :: a (bound at test2.hs:122:19)
inflate :: MyShape a -> b -> MyShape a (bound at test2.hs:122:3)
|
122 | inflate (Circle r) x = Circle (r * x)
| ^
【问题讨论】:
-
你能显示 full 错误吗?
-
无法将预期类型“a”与实际类型“b”匹配“b”是受类型签名约束的刚性类型变量:inflate :: forall b。 Num b => MyShape a -> b -> MyShape a at test2.hs:122:3-9 'a' 是一个刚性类型变量,由 test2.hs:121:10-33 处的实例声明绑定 • 在第二个'(*)' 的参数,即'x' 在'Circle' 的第一个参数中,即'(r * x)' 在表达式中:Circle (r * x)
-
请把错误写在你上面的帖子里。如您所见,未格式化的版本很难阅读。
标签: haskell