Proposal: Better recip for Ratio a

Daniel Fischer at
Fri Sep 24 18:12:06 EDT 2010

I propose to change the implementation of recip in the Fractional instance 
of Ratio a to make it faster.

Currently, the instance defines

    recip (x:%y)        =  y % x

which means that the gcd of numerator and denominator is recalculated, 
although for all properly generated values, they are known to be coprime.

(Invalid vlaues can be obtained via direct use of the constructor or via 
fromRational cf.

Period of discussion: Three weeks, unitl 15th October (because of ICFP).


For Rationals with large numerators and denominators, the proposed 

    recip (0:%_)        = error "Ratio.%: zero denominator"
    recip (x:%y)
        | x < 0         = negate y :% negate x
        | otherwise     = y :% x

gives a huge speedup.

More information about the Libraries mailing list