[Proposal] add view function for NPlusKPatterns

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Proposal] add view function for NPlusKPatterns

Stijn van Drongelen
Hello,

I'm working on a desugaring library for Haskell. Desugaring NPlusKPatterns currently takes up the most code, since a proper translation involves generating two guards. Meanwhile, most other pattern-related language extensions are defined in terms of ViewPatterns, which are trivial to desugar.

In my laziness to fix this otherwise, I propose that the following function is added to the Prelude:

> maySubtractIntegral :: Integral a => a -> a -> Maybe a
> maySubtractIntegral k nplusk = if n >= 0 then Just n else Nothing
>   where
>     n = nplusk - k

So that `NPlusKPatterns` like

< func (n+42) = expr

may be desugared to

< func (maySubtractIntegral 42 -> Just n) = expr

rather than

< func nplusk | n <- nplusk - 42, n >= 0 = expr

The name of this function is, of course, subject to bikeshedding. Note that the signature is correct; though (Ord a, Num a) is more general, it's too weak for the semantics of the language extension.

Discussion period: 2 weeks

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

Re: [Proposal] add view function for NPlusKPatterns

David Luposchainsky
On 2013-09-30 16:37, Stijn van Drongelen wrote:
> I'm working on a desugaring library for Haskell. Desugaring
> NPlusKPatterns currently takes up the most code, since a proper
> translation involves generating two guards. Meanwhile, most other
> pattern-related language extensions are defined in terms of
> ViewPatterns, which are trivial to desugar.
>
> In my laziness to fix this otherwise, I propose that the following
> function is added to the Prelude: [...]

1. Adding things to the Prelude is not something you can or should do
easily. Adding means it has to become part of the Haskell report (i.e.
Haskell the language), and removing it takes great effort because many
modules may depend on it.

2. If things *are* added to the current Prelude, they should be widely
used, universally accepted, and very useful. I don't think your
functionality matches any of these.

3. n+k-patterns are not part of the Haskell 2010 standard; if you really
need them for compatibility reasons or so, there's a legacy language
extension. Using these patterns is universally (?) disencouraged.

4. "My laziness" and golfing are not very good bases for a proposal.

David

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

Re: [Proposal] add view function for NPlusKPatterns

Henning Thielemann-4
Am 30.09.2013 16:56, schrieb David Luposchainsky:

> On 2013-09-30 16:37, Stijn van Drongelen wrote:
>> I'm working on a desugaring library for Haskell. Desugaring
>> NPlusKPatterns currently takes up the most code, since a proper
>> translation involves generating two guards. Meanwhile, most other
>> pattern-related language extensions are defined in terms of
>> ViewPatterns, which are trivial to desugar.
>>
>> In my laziness to fix this otherwise, I propose that the following
>> function is added to the Prelude: [...]
>
> 4. "My laziness" and golfing are not very good bases for a proposal.

Stijn, you might add the utility function to a custom library that is
bundled with your preprocessor.

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

Re: [Proposal] add view function for NPlusKPatterns

Stijn van Drongelen
On Mon, Sep 30, 2013 at 5:00 PM, Henning Thielemann <[hidden email]> wrote:
Am 30.09.2013 16:56, schrieb David Luposchainsky:
On 2013-09-30 16:37, Stijn van Drongelen wrote:
I'm working on a desugaring library for Haskell. Desugaring
NPlusKPatterns currently takes up the most code, since a proper
translation involves generating two guards. Meanwhile, most other
pattern-related language extensions are defined in terms of
ViewPatterns, which are trivial to desugar.

In my laziness to fix this otherwise, I propose that the following
function is added to the Prelude: [...]

4. "My laziness" and golfing are not very good bases for a proposal.

Stijn, you might add the utility function to a custom library that is bundled with your preprocessor.

I immediately regret my decision of mailing this proposal to libraries@. You're right :)

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