Record pattern synonyms for fields covering multiple constructors
If a field belongs to multiple constructors of a datatype, it is not possible to replace the constructors with pattern synonyms, because each pattern synonym has independent fields. For example:
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE PatternSynonyms #-}
{-# OPTIONS_GHC -Wall #-}
module PatSynMulti where
data S = MkS1 { f :: Int } | MkS2 { f :: Int }
pattern MkT1 { g } = MkS1 { f = g }
pattern MkT2 { g } = MkS2 { f = g }
{-# COMPLETE MkT1, MkT2 #-}
bar = f -- accepted
baz = g -- rejected
(Here DuplicateRecordFields
is necessary to allow g
to be defined twice; without it the pattern synonym definitions are rejected.)
Similar problems also arise with record updates, and the HasField
typeclass.
Ideally, it would be possible to replace a datatype with an equivalent collection of pattern synonyms, and this issue prevents that. It seems to me that resolving this would need some notion of "pattern synonym collections" where fields cover all the pattern synonyms in the collection, rather like souped-up COMPLETE
pragmas.