【发布时间】:2013-10-11 21:49:46
【问题描述】:
最近我看了一下 Haskell,使用 LYAH。
我在搞乱类型类并编写了这个快速测试函数:
foo :: (Num x) => x -> String
foo x = show x ++ "!"
但这会产生这个错误:
test.hs:2:9:
Could not deduce (Show x) arising from a use of `show'
from the context (Num x)
bound by the type signature for foo :: Num x => x -> String
at test.hs:1:8-29
Possible fix:
add (Show x) to the context of
the type signature for foo :: Num x => x -> String
但根据 LYAH 的说法:
要加入 Num,类型必须已经是 Show 和 Eq 的朋友。
如果Num 中的所有内容都是Show 和Eq 的子集,为什么我需要将类型签名更改为foo :: (Num x, Show x) => x -> String 才能正常工作?难道不应该可以推断出Num 也可以显示吗?
【问题讨论】:
标签: haskell