[Haskell-cafe] instance Enum Double considered not entirely great?

Evan Laforge qdunkan at gmail.com
Tue Sep 20 22:42:18 CEST 2011


> This makes me wonder if maybe the reason this discussion is happening
> at all is that we don't have a well-defined meaning for what Enum
> *is*. At this point, it seems like the only answer is that it's

I agree, that's why I suggested a separate 'range' function.  I think
the "all values in this range" definition is reasonable (and
incidentally, Data.Fixed.Binary's Enum coincides with Data.Fixed, so I
think its the floats that are the odd ones out).  Enum is explicitly
not a numeric notion because it's meant to work for any enumeration
style ADT, so it seems clear to me it's about enumeration rather than
a numeric range.  There's clearly a lot of overlap between the two
interpretations, but we have Double taking Enum as a numeric range,
Data.Fixed taking it as an enumeration, integral types overlap so you
can't tell, and 'data C = Red | Green | Blue deriving Enum' is clearly
taking it as enumeration.

Some people have suggested that it's simply an ambiguous class which
has multiple interpretations, like Monoid, and thus you just need to
know which one you're dealing with.  Fair enough, but is that a good
thing?  I think it makes it harder to move between numeric types.  I
don't mind the Monoid thing because I haven't had to move between two
Monoid types and expect them to work similarly, but float -> fixed is
kind of like String -> Text in that its a likely axis of change.  I
would also be pretty surprised if Text came up with a different
interpretation of Monoid than String :)


On Tue, Sep 20, 2011 at 9:16 AM, Daniel Fischer
<daniel.is.fischer at googlemail.com> wrote:
>> - but it is reality, so we need to deal with it.
>
> Like Eq and Ord, it's just too damn convenient to have it.
> So much nicer to write [0, 0.25 .. 1000] instead of
> numericEnumFromThenTo 0 0.25 1000
> and (x /= y) instead of
> doublesDifferentOrNaN x y

Interestingly, "too damn convenient" was one of my motivations
*against* the instance.  It's not even that much more convenient, if
you write it 'range' then 'range 0 1000 0.25' is not much longer than
[0, 0.25 .. 1000], but, I'd argue the latter is a trap, and has a good
chance of causing a subtle bug in your app eventually if it gets big
enough.  The problem is that the list syntax is built-in and looks
nice and so is very tempting.



More information about the Haskell-Cafe mailing list