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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With