Quantcast

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

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

ARJANEN Loïc Jean David
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.

Deadline: 12 July 2012.

P.S. : A conversation about that took place here:
http://haskell.1045720.n5.nabble.com/A-Down-newtype-in-Data-Ord-td5005373.html

--
ARJANEN Loïc Jean David
http://blog.luigiscorner.com
---
"Computer science is no more about computers than astronomy is about
telescopes, biology is about microscopes, or chemistry is about
beakers and test tubes. Science is not about tools. It is about how we
use them, and what we find out when we do."
Michael R. Fellows and Ian Parberry

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Twan van Laarhoven
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

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

ARJANEN Loïc Jean David
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 ?

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Edward Kmett
I would be happy enough with just re-exporting Down and calling it a day. Last time this topic came up it was bikeshedded to death without resolution.

-Edward

On Mon, Jul 9, 2012 at 5:51 PM, ARJANEN Loïc Jean David <[hidden email]> wrote:
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 ?

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries


_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

wren ng thornton
On 7/9/12 7:29 PM, Edward Kmett wrote:
> I would be happy enough with just re-exporting Down and calling it a day.
> Last time this topic came up it was bikeshedded to death without resolution.

Indeed. I'd rather use "Reverse" than "Down", but I'd rather have Down
in the standard library than have this bikeshedded to death again.

--
Live well,
~wren

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Strake
In reply to this post by Edward Kmett
On 09/07/2012, Edward Kmett <[hidden email]> wrote:
> I would be happy enough with just re-exporting Down and calling it a day.
> Last time this topic came up it was bikeshedded to death without
> resolution.

Why is it called "Down"? Is this a mathematical term?

Its name ought to say what it does. So far, the best seems to be
"DualOrd", for it quite clearly affects only the order.

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Brent Yorgey-2
In reply to this post by wren ng thornton
On Tue, Jul 10, 2012 at 03:07:38AM -0400, wren ng thornton wrote:
> On 7/9/12 7:29 PM, Edward Kmett wrote:
> >I would be happy enough with just re-exporting Down and calling it a day.
> >Last time this topic came up it was bikeshedded to death without resolution.
>
> Indeed. I'd rather use "Reverse" than "Down", but I'd rather have
> Down in the standard library than have this bikeshedded to death
> again.

+1 for re-exporting Down and no bikeshedding.

-Brent

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Twan van Laarhoven
In reply to this post by Strake
On 10/07/12 14:31, Strake wrote:
> On 09/07/2012, Edward Kmett <[hidden email]> wrote:
>> I would be happy enough with just re-exporting Down and calling it a day.
>> Last time this topic came up it was bikeshedded to death without
>> resolution.
>
> Why is it called "Down"? Is this a mathematical term?

I believe the Down newtype was introduced for use with extended list
comprehension, things like
     [ name u | u <- users, then sort (Down (salary x)) ].

FWIW, I'll settle for reexporting Down if that avoids bikeshedding.


Twan

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Conrad Parker
On 10 July 2012 23:28, Twan van Laarhoven <[hidden email]> wrote:

> On 10/07/12 14:31, Strake wrote:
>>
>> On 09/07/2012, Edward Kmett <[hidden email]> wrote:
>>>
>>> I would be happy enough with just re-exporting Down and calling it a day.
>>> Last time this topic came up it was bikeshedded to death without
>>> resolution.
>>
>>
>> Why is it called "Down"? Is this a mathematical term?
>
>
> I believe the Down newtype was introduced for use with extended list
> comprehension, things like
>     [ name u | u <- users, then sort (Down (salary x)) ].

That is illustrative; it reads much better than Reverse or DualOrd
would, and I expect that sort is the most common use of an Ord
instance.

+1 for re-exporting Down.

Conrad.

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Andreas Abel
On 11.07.12 4:31 AM, Conrad Parker wrote:

> On 10 July 2012 23:28, Twan van Laarhoven<[hidden email]>  wrote:
>> On 10/07/12 14:31, Strake wrote:
>>>
>>> On 09/07/2012, Edward Kmett<[hidden email]>  wrote:
>>>>
>>>> I would be happy enough with just re-exporting Down and calling it a day.
>>>> Last time this topic came up it was bikeshedded to death without
>>>> resolution.
>>>
>>>
>>> Why is it called "Down"? Is this a mathematical term?
>>
>>
>> I believe the Down newtype was introduced for use with extended list
>> comprehension, things like
>>      [ name u | u<- users, then sort (Down (salary x)) ].
>
> That is illustrative; it reads much better than Reverse or DualOrd
> would, and I expect that sort is the most common use of an Ord
> instance.
>
> +1 for re-exporting Down.

+1 for Down.   (DualOrd would be also ok, but sounds artifical.  Reverse
has too much connotation of list-reverse for my taste.)

--
Andreas Abel  <><      Du bist der geliebte Mensch.

Theoretical Computer Science, University of Munich
Oettingenstr. 67, D-80538 Munich, GERMANY

[hidden email]
http://www2.tcs.ifi.lmu.de/~abel/

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Yitzchak Gale
+1
Any name, no bikeshedding.

Thanks,
Yitz

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

ARJANEN Loïc Jean David
In reply to this post by ARJANEN Loïc Jean David
I have created a ticket for the proposal on GHC's Trac (see here:
http://hackage.haskell.org/trac/ghc/ticket/7077). I didn't asked for the
maintainer's advice before opening a ticket given that the maintener is
[hidden email] and there wasn't any opposition.
Based on this list advice, I finally decided to just re-export
GHC.Exts's newtype for the proposal's purposes, so that this proposal
doesn't get bikeshedded to death on the newtype's name.

Regards,
ARJANEN Loïc

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.
>
> Deadline: 12 July 2012.
>
> P.S. : A conversation about that took place here:
> http://haskell.1045720.n5.nabble.com/A-Down-newtype-in-Data-Ord-td5005373.html
>
--
ARJANEN Loïc Jean David
http://blog.luigiscorner.com
---
"Computer science is no more about computers than astronomy is about
telescopes, biology is about microscopes, or chemistry is about
beakers and test tubes. Science is not about tools. It is about how we
use them, and what we find out when we do."
 Michael R. Fellows and Ian Parberry


_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

ARJANEN Loïc Jean David
Hello,

I am writing to say that the feature request was accepted (cf. here:
http://hackage.haskell.org/trac/ghc/ticket/7077#comment:2).
The maintainer's final decision was to move Down from GHC.Exts to Data.Ord.
Thanks for all of your suggestions.

Regards.

--
ARJANEN Loïc Jean David
http://blog.luigiscorner.com
---
"Computer science is no more about computers than astronomy is about
telescopes, biology is about microscopes, or chemistry is about
beakers and test tubes. Science is not about tools. It is about how we
use them, and what we find out when we do."
 Michael R. Fellows and Ian Parberry

_______________________________________________
Libraries mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/libraries
Loading...