The problem with your code is that the number literals themselves are already overloaded. Thus, letter 6 is of type Num a => a , and my_fun 5 is of type F_third_arg b => b -> Integer . Thus, during type inference, it combines these two type variables. But since they have no other requirements, the GHC cannot find a specific type to use here and gives the corresponding error message:
test.hs: 16: 26:
No instance for (F_third_arg a0) arising from a use of `my_fun '
The type variable `a0 'is ambiguous
Possible fix: add a type signature that fixes these type variable (s)
Note: there are several potential instances:
instance F_third_arg String - Defined at test.hs: 9: 10
instance F_third_arg Integer - Defined at test.hs: 6: 10
In the expression: (my_fun 5)
In the first argument of `show ', namely` ((my_fun 5) $ 6)'
In the second argument of `($) ', namely` show ((my_fun 5) $ 6)'
test.hs: 16: 38:
No instance for (Num a0) arising from the literal `6 '
The type variable `a0 'is ambiguous
Possible fix: add a type signature that fixes these type variable (s)
Note: there are several potential instances:
instance Num Double - Defined in `GHC.Float '
instance Num Float - Defined in `GHC.Float '
instance Integral a => Num (GHC.Real.Ratio a)
- Defined in `GHC.Real '
... plus three others
In the second argument of `($) ', namely` 6'
In the first argument of `show ', namely` ((my_fun 5) $ 6)'
In the second argument of `($) ', namely` show ((my_fun 5) $ 6)'
One would expect the compiler to notice that Integer is the only type that fulfills both requirements, but such heuristics will make your code relatively fragile, i.e. it will break only because you add a new instance (e.g. F_third_arg Double ), therefore the compiler rejects the code and asks you to be explicit about the type in question.
You found one way to fix this, but the @leftroundaboutss suggestion of using 6::Integer bit nicer.
Joachim breitner
source share