Ord instance for Data.Map?

Henning Thielemann lemming at henning-thielemann.de
Tue Apr 17 08:08:00 EDT 2007


On Tue, 17 Apr 2007, Bertram Felgenhauer wrote:

> Henning Thielemann wrote:
>
> > I wonder whether the Ord instance is a good choice as constraint for
> > Data.Map, Data.Set and so on.
>
> In the case where there are Ord instances they are a good choice. Adding
> another type class for this purpose has the disadvantage that you have
> to write class instances for them, for all existing types that have Ord
> instances; while trivial this still adds up to quite a few lines of
> boilerplate code. This has to be weighted against the added boilerplate
> code for cases where a total order can be defined but is rather
> artificial. Right now I think that this is the exception rather than the
> rule. Changing the library interface should also not be done lightly.

I always think in terms of "How would it be done correctly if we would
start from scratch?" :-)

> Personally I'd do it without an Indexable class; just
>
> > newtype Index a = Index a
>
> > instance Ord x => Ord (Index (Complex x)) where
> >    (Index (Complex a b)) `compare` (Index (Complex c d)) =
> >        (a, b) `compare` (c, d)
>
> should be good enough. 'Index' marks the purpose for the type.

That requires extended instance declarations, doesn't it?


More information about the Libraries mailing list