Search code examples
haskellcompiler-errorscross-platformghchugs

Are arithmetic patterns legal Haskell?


Patterns like this:

front :: [a] -> a
front (x:_) = x
front _ = error "Empty list"

seem to be common in Haskell, but I distinctively remember learning the following when I started learning Haskell:

dec :: (Integral a) => a -> a
dec (x+1) = x
dec _ = error "Bottom"

However, ghc appears to reject that piece of code, stating:

Parse error in pattern: x + 1

While hugs accepts it just fine. So, is this valid Haskell or not and why do these compilers behave differently.


Solution

  • This is what is known as the n+k pattern. It was disliked in general and was removed from the Haskell2010 spec and GHC no longer enables it by default unlike Hugs which hasn't been updated to the latest spec. It should compile with GHCI with the -XNPlusKPatterns flag enabled.

    See this for more details.