I was just playing around with Haskell typeclasses and I discovered an error that I am not able to understand.
Here is the code that reproduces it:
fun :: (Num a) => Integer -> a
fun a = a + 1
Error:
Couldn't match expected type ‘a’ with actual type ‘Integer’
Now, as I understand it, Integer is an instance of the Num Typeclass and Integer type meets all the requirements defined by Num. Is this kind of conversion not allowed? Isn't this the point of using creating a Typeclass, i.e. 'a' is any instance of typeclass Num.
Integeris not automatically promoted to, say,Double-- we need to callfromIntegerto explicitly convert the value. The only exception are numeric literals:123is automatically converted to the numeric type the context requires, essentially behaving as if the use wrotefromInteger (123::Integer). All the other expressions (including variables) are not subject to this automatic conversion.