signum (recip (-1 :% 1)) should not be 1 % 1

Ralf Welter i_see@macnews.de
Thu, 5 Dec 2002 19:04:34 +0100


Hi!

I think I have found a bug in the hugs prelude:
Bug: definition of recip in the instance declaration of breaks silent 
assumptions about the internal data in Ratio types.

Descriptiion:
data Integral a => Ratio a = !a :% !a deriving (Eq)
by deriving Eq a normal form is assumed. Further places do at least 
assume a positive denominator:

> instance Integral a => Num (Ratio a) where
>     ...
>     abs (x :% y)      = abs x :% y
>     signum (x :% y)   = signum x :% 1
silently assume the denominator is positive

unforturtunately the definition of recip
> instance Integral a => Fractional (Ratio a) where
>     ...
>     recip (x:%y)        = y :% x
will break these properties.
fix: remove the colon on the right hand side of the recip definition.

Hope this helps - I havn't had the time to check if ghc and other 
implementations suffer from the same issue.

regards,
	ralf welter