Using Ord for keys of maps and sets

Henning Thielemann lemming at
Mon Dec 3 08:42:11 EST 2007

On Mon, 3 Dec 2007, Yitzchak Gale wrote:

> Henning Thielemann wrote:
> > I already told about by scepticism about using Ord for keys of maps and
> > sets:
> >
> Yes. There are a number of different scenarios for the
> ordering that you want to use for indexing:
> 1. A default ordering for the type that is a natural ordering.
> 2. A default ordering for indexing that is not a natural
>    ordering.
> 3. An application-specific ordering.
> 4. More than one ordering for the same type, determined
>    statically.
> 5. More than one ordering for the same type, determined
>    dynamically, i.e., parameterized orderings.
> My experience has been that all of these cases do come
> up quite often in real life.
> One solution would be to have Data.Map.Indexable, etc.
> as alternatives using Indexable instead of Ord, so that
> we could avoid newtype wrapping in case 2.
> We could allow for case 3 by not providing any default
> instances for Indexable.

This would obstruct 'start GHCi and play around' sessions, since you
cannot create a set of something on the fly. (However, GHCi could still be

> Or we could provide default instances in a separate module,
> Data.Indexable.Instances (or something), that you could choose whether
> or not to import.
> Newtype wrapping is still required in case 4, so
> Indexable doesn't add much here.
> We have not yet said anything helpful about case 5.

Still, later in the above thread, Johannes Waldmann throws in Local

More information about the Libraries mailing list