Skip to content

Strange behavior when pattern variables are duplicated on pattern synonym RHS

These might be two separate bugs, but they're similar enough that I'll bundle them under the same ticket.

If you take a pattern variable bound on the LHS of a unidirectional pattern synonym and use it multiple locations on the RHS, then GHC accepts it, surprisingly:

pattern Foo a <- (a,a)

What does Foo do? As it turns out, the right-most occurrence of a in the RHS seems to take precedence:

λ> :i Foo
pattern Foo :: b -> (a, b)      -- Defined at <interactive>:12:1
λ> case ('a', 'b') of Foo x -> x
'b'

I expect the definition of Foo to error.

When duplicate pattern variables are used in the RHS of an implicitly bidirectional pattern synonym, then it does error. However, the error message is quite misleading:

λ> pattern Foo a = (a,a)

<interactive>:16:17: error:
    Invalid right-hand side of bidirectional pattern synonym ‘Foo’:
      ‘a’ is not bound by the LHS of the pattern synonym
    RHS pattern: (a, a)
Trac metadata
Trac field Value
Version 8.2.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information