Gaussian Integers

Janis Voigtlaender voigt@orchid.inf.tu-dresden.de
Tue, 19 Mar 2002 12:46:43 +0100


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