Reputation: 10324
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?
Upvotes: 18
Views: 716
Reputation: 102276
The problem is the type variable b
can'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 type a -> 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:
{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}
class Summable a result | a -> result where
(+-+) :: a -> a -> result
instance Summable Char String where
a +-+ b = [a] ++ [b]
instance Summable Integer Integer where
a +-+ b = a + b
For type families, it would be like:
{-# LANGUAGE TypeFamilies #-}
class Summable a where
type Result a
(+-+) :: a -> a -> Result a
instance Summable Char where
type Result Char = String
a +-+ b = [a] ++ [b]
instance Summable Integer where
type Result Integer = Integer
a +-+ b = a + b
(Thank you to Vitus and Vladimir Matveev for fixing the various errors I had made! :) )
Upvotes: 25