Compile error for PatternSynonyms together with OverloadedLists
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE PatternSynonyms #-}
data Foo = Foo [Int]
pattern Bar :: Foo
pattern Bar = Foo []
This code results in a cryptic compilation error:
$ ghc main.hs
[1 of 1] Compiling Main ( main.hs, main.o )
main.hs:7:19: error:
• Couldn't match expected type ‘[a0] -> [Int]’
with actual type ‘[GHC.Exts.Item Int]’
• This rebindable syntax expects a function with two arguments,
but its type ‘Int -> [GHC.Exts.Item Int]’ has only one
In the first argument of ‘Foo’, namely ‘[]’
In the expression: Foo []
|
7 | pattern Bar = Foo []
| ^^
As soon as OverloadedLists
is removed, this error goes away. In itself the problem isn't too critical, but very unexpected.
I got above error with GHC 8.0 and 8.2, so decided to check it with current version as well:
$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.3.20171020
Looking a bit more into PatternSynonyms
extension I was able to work around this by declaring an explicit bidirectional pattern, which, surprisingly, solved the problem:
pattern Bar :: Foo
pattern Bar <- Foo [] where
Bar = Foo []
If fixing this issue is too much trouble than it is worth, than at least a mention about it in documentation will be helpful.
Out of curiosity I also tried it with String
, which did not trigger this error and compiled just fine:
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
data Foo = Foo [Char]
pattern Bar :: Foo
pattern Bar = Foo ""
PS. Even if this issue is closed, searching the internet for the error message will now at least return some info. :)
Trac metadata
Trac field | Value |
---|---|
Version | 8.3 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |