Overflow in Ratio

David Benbennick dbenbenn at gmail.com
Sat Jul 21 17:03:38 EDT 2007


The Ratio data type suffers from numeric overflow.  For example,
consider the Ord declaration in the Haskell 98 Report (I'm looking at
page 153, section 12.1 of
http://haskell.org/definition/haskell98-report.pdf):

    (x:%y) <= (x':%y')  =  x * y' <= x' * y

It is easy to see that Ratio Int is not well-ordered under this
definition, because of overflow.  This is a self-contradiction in the
Report, since page 84 (section 6.3.2) says that "The Ord class is used
for totally ordered datatypes".

I think the best way to resolve this contradiction is to avoid
overflow.  For example, the following definition works:

    (x:%y) <= (x':%y')  =  (toInteger x) * (toInteger y') <=
(toInteger x') * (toInteger y)

I submitted a complete patch at
http://hackage.haskell.org/trac/ghc/ticket/1517, but that was
apparently the wrong place, since the bug was closed as wontfix.

Is there a chance of getting this contradiction resolved?  I'll be
happy to provide unit tests for the change if that will help get it
applied.


More information about the Libraries mailing list