Defining an instance to a restricted a type in Haskell

Viewed 55

I am working with the following Data Type:

data Exp a =
|Const a
|Simetrico (Exp a)
|Mais (Exp a) (Exp a)
|Menos (Exp a) (Exp a)
|Mult (Exp a) (Exp a)

but a is supposed to be a Numeric type. I would define Eq like this:

instance Eq (Exp a) where
         a == b | ... = True
                | otherwise = False

but I am saying nowhere that my a is a numeric type, so ghci complains, how do I solve this ?

1 Answers

You add a type constraint to the instance clause:

instance Num a => Eq (Exp a) where
         a == b | ... = True
                | otherwise = False

So now you can assume (in the scope of the instance) that a is an instance of the Num typeclass.

Related