Pattern synonyms

Pattern synonyms are an extension to the Haskell programming language provided by GHC since GHC 7.8.1, enabled by language extension PatternSynonyms. There are three forms: unidirectional, bidirectional and explicitly bidirectional.

Synonyms

A synonym is introduced with the keyword pattern. Synonyms have three forms: prefix, infix and record pattern.

The prefix form is \mathit{conid}\, \mathit{varid}_{\mathit{1}} \ldots \mathit{varid}_{\mathit{n}}. For example:

MyPatternSynonym

or

MyPatternSynonym x1 x2 x3.

The infix form is \mathit{varid}_{\mathit{1}}\, \mathit{conop}\, \mathit{varid}_{\mathit{2}}; that is, \mathit{varid}_{\mathit{1}}\, \mathtt{\char96}\mathit{conid}\mathtt{\char96}\, \mathit{varid}_{\mathit{2}} or \mathit{varid}_{\mathit{1}}\, \mathit{consym}\, \mathit{varid}_{\mathit{2}}. For example:

x1 `MyPatternSynonym` x2

or

x1 :=: x2.

The record pattern form is \mathit{conid}\, \mathtt{\{}\mathit{varid}_{\mathit{1}}\mathtt{,}\, \ldots \mathtt{,}\,\mathit{varid}_{\mathit{n}}\mathtt{\}}. For example:

MyPatternSynonym { x1, x2, x2 }.

A synonym can only be introduced at the top level of a module.

The name of the pattern synonym is in the same namespace as data constructors.

Unidirectional

The undirectional syntax is:

pattern synonym <- pat

where pat is valid as a pattern.

Bidirectional

The bidirectional syntax is:

pattern synonym = pat

where pat is valid both as a pattern and as an expression.

All the variables of the pattern must occur in the synonym. Wildcard patterns and view patterns are not allowed.

Explicitly bidirectional

The explicitly bidrectional syntax is:

pattern synonym <- pat where synonym = expr

where pat is valid as a pattern and expr is valid as an expression.

The synonym cannot have a record pattern form.

Recursion

Pattern synonyms cannot be defined recursively.

Export

For GHC versions before GHC 9.14.1, the keyword pattern can be used in an export list to export a pattern synonym.

From GHC 9.14.1, the keywords data (preferred) or pattern can be used. However, the keyword data requires the ExplicitNamespaces language extension to be enabled.

From GHC 8.0.1, a pattern synonym can be ‘bundled’ with an exported type constructor, if the pattern synonym and the type constructor are of the same type.

Import

For GHC versions before GHC 9.14.1, the keyword pattern can be used in an import statement to import a pattern synonym.

From GHC 9.14.1, the keywords data (preferred) or pattern can be used. However, the keyword data requires the ExplicitNamespaces language extension to be enabled.

From GHC 8.0.1, a pattern synonym ‘bundled’ with an exported type constructor can be imported with the type constructor.

Completeness checking

GHC’s pattern match checker assumes that any match involving a pattern synonym will fail. Consequently, unless there is a ‘catch all’ case, the pattern match checker will report that a match involving a pattern synonym is incomplete. This can be mitigated by use of GHC’s COMPLETE pragma.