haskell报错反查

is applied to too many type arguments

fib :: (Integral n) => n -> n
fib 0 = 0
fib 1 = 1
fib x = fib(x - 1) + fib(x - 2)


将Integral 换成Integer将报以上的错!

中文解释是 定义的TypeClass的行为不支持以下动作

我对typeclasses的理解 是指具有这个行为.

TypeClass Eq 包含可判断相等的型号. Ord 包含可比较大小的型号。 Show 可用字符串表示的型号. Integral 同样是表示数字的 Typeclass Enum 的成员都是连续的型别 – 也就是可枚举

It’s a recent change proposed and accepted in September/October last year, in the latest version of the base package, Eq and Show are no longer superclasses of Num. Since that change, no new version of the language report has been published, so it’s not yet in the report. And since it’s recent, it has also not made it yet into many tutorials or books.

“Pattern matching” against a numeric literal is an implicit application of (==), so an Eq instance is required for it to work. That instance can now no longer be inferred from the Num constraint, so the (quite new :D) compiler rejects the code with only a Num constraint.

But Integral is a subclass of Real, which has Ord (and hence Eq) as a superclass, hence that works.

一些回答

http://stackoverflow.com/questions/18676468/could-not-deduce-eq-a-from-num-a-or-from-floating-a-but-can-deduce-eq-a http://stackoverflow.com/questions/26456179/haskell-error-could-not-deduce-eq-a-arising-from-the-literal-0

http://stackoverflow.com/questions/12768536/num-vs-integral

Loading Disqus comments...
Table of Contents