Proposal: add an order-reversing newtype to Data.Ord

ARJANEN Loïc Jean David arjanen.loic at
Mon Jul 9 23:51:56 CEST 2012

On 2012-06-28 16:40, Twan van Laarhoven wrote :
> On 2012-06-28 12:01, ARJANEN Loïc Jean David wrote:
>> Hello all,
>> I recently noticed that despite conversations about that in this
>> mailing-list, there isn't an order-reversing newtype in Data.Ord.
>> That newtype would be quite useful, e.g. for reverse-sorting a list or
>> a set, so I propose we add a newtype defined thusly:
>>     newtype Reverse a = Reverse { fromReverse :: a } deriving (Eq)
>>     instance Ord a => Ord (Reverse a) where
>>        compare (Reverse x) (Reverse y) = compare y x
>> I took the implementation from the Down newtype in GHC.Exts, so we
>> could perhaps just re-export it.
>> This will cause breakage to code already defining a type named
>> Reverse, so I don't know if the damage will be important. Of course,
>> the name and definition are open to discussion.
> +1 with any sensible name except Dual.
> The name Dual is also used for dual monoids. I would prefer to be able
> to tell from a piece of code like `f (Dual x)` whether it reverses the
> order of the arguments to `mappend` or of the arguments to `compare`.
> As a good compromise, we could call the newtype "DualOrd" or something.
> The extraction function should maybe be called getReverse (or
> getWhatever), which is the convention used by the wrappers in
> Data.Monoid.
> Twan

So, what would people prefer between re-exporting GHC.Exts's
order-reversing newtype (and thus calling it Down) and moving it to
Data.Ord, provisionally calling it Reverse and with Twan's suggested
modification ?

If we move it to Data.Ord, we would have to provide the existing Down in
GHC.Exts (perhaps with a newtype, but that could become quite unwieldy)
or at least deprecate it.

So, what are your thoughts ?

More information about the Libraries mailing list