18

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?

1
  • 1
    Note that [a] ++ [b] is just [a,b]... Aug 30, 2012 at 16:40

1 Answer 1

25

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! :) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.