I would like to define a pattern synonym for a sum type to extract 1 field from both data constructors.
Looks like doable but GHC cannot compile following snippet:
data FooBar = Foo Int String | Bar String
pattern FooBarStr :: String → Foo
pattern FooBarStr s ← Foo _ s
pattern FooBarStr s ← Bar s
Originally Foo type was defined as:
data Foo = Foo (Maybe Int) String
But I have a problem with using a generated prism
( _Foo # Nothing ) – is not the same as _Bar of FooBar
pattern FooBarStr s <- Foo _ s
FooBarStr s <- Bar s
where ...
The compiler would need to see all the <- equations together, so it can desugar that to a single case statement.
Sadly, as @jaror says, the only way to achieve it today is with ViewPatterns – which I find particularly non-intuitive with that arrow from an invisible argument.