Proposal: Better recip for Ratio a

Daniel Fischer daniel.is.fischer at web.de
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. http://hackage.haskell.org/trac/ghc/ticket/4335.)

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

Ticket: http://hackage.haskell.org/trac/ghc/ticket/4336

For Rationals with large numerators and denominators, the proposed 
implementation

    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