I want to define an operator (call it +-+) that works in this:
if a,b are Char => a +-+ b = [a][b]
if a,b are Integer => a +-+ b = a+b
I tried with:
class Summable a where
(+-+) :: a -> a -> b
instance Summable Integer where
a +-+ b = a + b
instance Summable Char where
a +-+ b = [a] ++ [b]
but I get the errors:
Couldn't match type `b' with `Integer'....
Couldn't match type `b' with `[Char]' ....
Is it possible to do this? How?
The problem is the type variable
bcan’t be determined from the instance, even though it is fixed. (Having a free variable like that requires the function returns something that has any type, i.e.undefined.)Can you give
+-+the typea -> a -> a? If so, do that. (But it looks like this isn’t possible.)Otherwise, you could use functional dependencies, so that the instance specifies the result type, or type families, so that one of the attributes of the instantiation is the result type.
For functional dependencies, the code might look like:
For type families, it would be like:
(Thank you to Vitus and Vladimir Matveev for fixing the various errors I had made! 🙂 )