class R r where
sum_ :: Integer -> r
instance R Integer where
sum_ x = x
instance (Integral a, R r) => R (a -> r) where
sum_ x = \y -> sum (x + toIngeter y)
The code above is used to sum a sequence of numbers and it can accept variable number of arguments. It runs correctly.
However, if I change the second instance to this:
instance (R r) => R (Integer -> r) where
sum_ x = \y -> sum (x + y)
and enable FlexibleInstances (otherwise a compile error will be raised in ghc 7.10.2), the function cannot run for multiple parameters.
For example, if I run
sum_ 1 2
an error would be raised:
Non type-variable argument in the constraint: R (a -> t)
But if I add an explicit type signature to parameters it runs correctly:
sum_ 1 (2::Integer)
Why couldn't 2 match the type Integer?