[Proposal] add view function for NPlusKPatterns

Stijn van Drongelen rhymoid at gmail.com
Mon Sep 30 14:37:44 UTC 2013


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

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20130930/4ad51796/attachment.html>

More information about the Libraries mailing list