This function:
hola :: (Integral a) => a -> String
hola 1 = "OK"
hola _ = "asdf"
works fine. But this one:
hola :: (Num a) => a -> String
hola 1 = "OK"
hola _ = "asdf"
can’t be compiled: “Could not deduce (Eq a) arising from the literal `1′”
I really don’t get it. I am reading a tutorial where it is said
“Integral is also a numeric typeclass. Num includes all numbers, including real numbers and integral numbers, Integral includes only integral (whole) numbers. In this typeclass are Int and Integer.” http://learnyouahaskell.com/types-and-typeclasses
Why can’t I use Num?
It’s a recent change proposed and accepted in September/October last year, in the latest version of the base package,
EqandShoware no longer superclasses ofNum. 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 anEqinstance is required for it to work. That instance can now no longer be inferred from theNumconstraint, so the (quite new :D) compiler rejects the code with only aNumconstraint.But
Integralis a subclass ofReal, which hasOrd(and henceEq) as a superclass, hence that works.