Search code examples
haskellghci

Syntax for lazy pattern matching with `as` syntax


In (vanilla) GHCi 8.6.5 following function was prefectly valid:

f xs@ ~(x:xt) = xs

If I now do the same in 9.0.1 I get an error

Suffix occurrence of @. For an as-pattern, remove the leading whitespace.

Just removing the white space between @ and ~ doesn't seem to suffice, as then @~ would be interpreted as an operator, so the only valid variation I found was

f xs@(~(x:xt)) = xs

I'd like to know the following, for which I couldn't find answers in the change notes:

  1. What exactly changed from 8.6.5 to 9.0.1 that introduced this incompatibility?
  2. Is xs@(~(x:xt)) really the best way to write this pattern, or is there a preferred way over this?

Solution

  • The changes to the handling of ~ and @ in GHC 9.0 are described here. Quoting from the migration guide:

    GHC 9.0 implements Proposal 229, which means that the !, ~, and @ characters are more sensitive to preceding and trailing whitespace than they were before. As a result, some things which used to parse one way will now parse differently (or throw a parse error).