Gaussian Integers

dominic.j.steinitz@britishairways.com dominic.j.steinitz@britishairways.com
Tue, 19 Mar 2002 11:33:11 +0000


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