On holiday, I started reading about Gaussian integers (as you do) and I thought this should be a piece of cake for Haskell. I get the following error in Hugs: ERROR "D:\Temp\comptst2.hs" (line 4): Cannot build superclass instance *** Instance : Num (Gaussian a) *** Context supplied : () *** Required superclass : Eq (Gaussian a) and a similar one using GHCi: Could not deduce `Eq b' from the context () Probable fix: Add `Eq b' to the instance declaration context arising from an instance declaration at comptst1.hs:4 I've already declared Gaussian a as being of class Eq so why does it need to be told again? data Integral a => Gaussian a = Gaussian a a deriving (Eq, Show) instance Num (Gaussian a) where Gaussian a b - Gaussian a' b' = Gaussian (a-a') (b-b') Gaussian a b + Gaussian a' b' = Gaussian (a+a') (b+b') Gaussian a b * Gaussian a' b' = Gaussian (a*a' - b*b') (a*b' + b*a') negate (Gaussian a b) = Gaussian (negate a) (negate b) fromInteger a = Gaussian (fromIntegral a) 0
dominic.j.steinitz@britishairways.com wrote:
I've already declared Gaussian a as being of class Eq so why does it need to be told again?
data Integral a => Gaussian a = Gaussian a a deriving (Eq, Show)
instance Num (Gaussian a) where Gaussian a b - Gaussian a' b' = Gaussian (a-a') (b-b') Gaussian a b + Gaussian a' b' = Gaussian (a+a') (b+b') Gaussian a b * Gaussian a' b' = Gaussian (a*a' - b*b') (a*b' + b*a') negate (Gaussian a b) = Gaussian (negate a) (negate b) fromInteger a = Gaussian (fromIntegral a) 0
You have only declared Gaussian a as being of class Eq (by deriving) if "a" is of class Integral (as a context in the data declaration). Adding Integral a to your instance should fix the problem: instance Integral a => Num (Gaussian a) where Gaussian a b - Gaussian a' b' = Gaussian (a-a') (b-b') Gaussian a b + Gaussian a' b' = Gaussian (a+a') (b+b') Gaussian a b * Gaussian a' b' = Gaussian (a*a' - b*b') (a*b' + b*a') negate (Gaussian a b) = Gaussian (negate a) (negate b) fromInteger a = Gaussian (fromIntegral a) 0 -- Janis Voigtlaender http://wwwtcs.inf.tu-dresden.de/~voigt/ mailto:voigt@tcs.inf.tu-dresden.de
participants (2)
-
dominic.j.steinitzļ¼ britishairways.com -
Janis Voigtlaender