
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