[Haskell-cafe] ANN: convertible (first release)

wren ng thornton wren at freegeek.org
Thu Jan 29 01:08:10 EST 2009


Duncan Coutts wrote:
> Michael D. Adams wrote:
> > Is there a good reason why Rational is defined in a way that it can
> > not represent Nan, Inf and -Inf? (Any other exceptional values I
> > forgot?)  Would fixing the definition so that it can represent those
> > values be sufficient to fix this entire problem?
> 
> It may well help conversions of floating point numbers through Rational
> but the contrary argument is that Rational is a well defined concept and
> that values like those do not belong in it.
> 
> The problem seems to be that the various real/float classes in the
> Prelude cannot provide a class that allows conversions directly between
> Double and Float without going via a universal type because the class
> would either have to be multi-parameter or mention one of the types in
> the class definition. For integral types it's no problem to go via
> Integer but floating point types are a good deal more tricky.

Exactly. Floating numbers also have other issues because of these 
values, e.g. they aren't Ord[1] and may not be Eq[2]. If we added the 
exceptional values to Rational we'd re-introduce all the problems they 
cause. As Duncan says, Rational is a well-defined concept by itself--- 
it's just a different concept than floating numbers.

For what it's worth, GHC.Real does provide literals for 1%0 and 0%0. 
However, since these values cannot be constructed by the smart 
constructor (%), functions on Ratios assume they cannot exist and 
therefore treat them incorrectly or raise errors.


[1] For those who think this is trivial, consider what value the 
`compare` function should return when one of the arguments is NaN. We 
don't even have a consistent lie for this! In Hugs it always returns EQ, 
whereas in GHC it always returns GT. Thus, not only is NaN not handled 
properly, but programs will behave differently depending on the compiler.

[2] Since NaN are defined to be unequal to everything including 
themselves. This isn't damning per se, but it does go against the law of 
the excluded middle, which people typically assume of Eq. Whether Eq 
must obey this law or not is unspecified. (Not to mention the fuzziness 
issues.)

-- 
Live well,
~wren


More information about the Haskell-Cafe mailing list