Implement pattern synonyms
This patch implements Pattern Synonyms (enabled by -XPatternSynonyms), allowing y ou to assign names to a pattern and abstract over it. The rundown is this: * Named patterns are introduced by the new 'pattern' keyword, and can be either *unidirectional* or *bidirectional*. A unidirectional pattern is, in the simplest sense, simply an 'alias' for a pattern, where the LHS may mention variables to occur in the RHS. A bidirectional pattern synonym occurs when a pattern may also be used in expression context. * Unidirectional patterns are declared like thus: pattern P x <- x:_ The synonym 'P' may only occur in a pattern context: foo :: [Int] -> Maybe Int foo (P x) = Just x foo _ = Nothing * Bidirectional patterns are declared like thus: pattern P x y = [x, y] Here, P may not only occur as a pattern, but also as an expression when given values for 'x' and 'y', i.e. bar :: Int -> [Int] bar x = P x 10 * Patterns can't yet have their own type signatures; signatures are inferred. * Pattern synonyms may not be recursive, c.f. type synonyms. * Pattern synonyms are also exported/imported using the 'pattern' keyword in an import/export decl, i.e. module Foo (pattern Bar) where ... Note that pattern synonyms share the namespace of constructors, so this disambiguation is required as a there may also be a 'Bar' type in scope as well as the 'Bar' pattern. * The semantics of a pattern synonym differ slightly from a typical pattern: when using a synonym, the pattern itself is matched, followed by all the arguments. This means that the strictness differs slightly: pattern P x y <- [x, y] f (P True True) = True f _ = False g [True, True] = True g _ = False In the example, while `g (False:undefined)` evaluates to False, `f (False:undefined)` results in undefined as both `x` and `y` arguments are matched to `True`. For more information, see the wiki: https://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms https://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms/Implementation Reviewed-by:Simon Peyton Jones <simonpj@microsoft.com> Signed-off-by:
Austin Seipp <austin@well-typed.com>
Showing
- compiler/basicTypes/BasicTypes.lhs 20 additions, 0 deletionscompiler/basicTypes/BasicTypes.lhs
- compiler/basicTypes/ConLike.lhs 82 additions, 0 deletionscompiler/basicTypes/ConLike.lhs
- compiler/basicTypes/DataCon.lhs-boot 8 additions, 1 deletioncompiler/basicTypes/DataCon.lhs-boot
- compiler/basicTypes/OccName.lhs 3 additions, 2 deletionscompiler/basicTypes/OccName.lhs
- compiler/basicTypes/PatSyn.lhs 225 additions, 0 deletionscompiler/basicTypes/PatSyn.lhs
- compiler/basicTypes/PatSyn.lhs-boot 19 additions, 0 deletionscompiler/basicTypes/PatSyn.lhs-boot
- compiler/deSugar/Check.lhs 35 additions, 17 deletionscompiler/deSugar/Check.lhs
- compiler/deSugar/Coverage.lhs 7 additions, 2 deletionscompiler/deSugar/Coverage.lhs
- compiler/deSugar/Desugar.lhs 14 additions, 3 deletionscompiler/deSugar/Desugar.lhs
- compiler/deSugar/DsBinds.lhs 9 additions, 3 deletionscompiler/deSugar/DsBinds.lhs
- compiler/deSugar/DsExpr.lhs 8 additions, 5 deletionscompiler/deSugar/DsExpr.lhs
- compiler/deSugar/DsMeta.hs 2 additions, 2 deletionscompiler/deSugar/DsMeta.hs
- compiler/deSugar/DsMonad.lhs 17 additions, 1 deletioncompiler/deSugar/DsMonad.lhs
- compiler/deSugar/DsUtils.lhs 147 additions, 89 deletionscompiler/deSugar/DsUtils.lhs
- compiler/deSugar/Match.lhs 18 additions, 10 deletionscompiler/deSugar/Match.lhs
- compiler/deSugar/MatchCon.lhs 46 additions, 15 deletionscompiler/deSugar/MatchCon.lhs
- compiler/ghc.cabal.in 3 additions, 0 deletionscompiler/ghc.cabal.in
- compiler/ghc.mk 2 additions, 0 deletionscompiler/ghc.mk
- compiler/hsSyn/Convert.lhs 6 additions, 2 deletionscompiler/hsSyn/Convert.lhs
- compiler/hsSyn/HsBinds.lhs 97 additions, 3 deletionscompiler/hsSyn/HsBinds.lhs
Loading
Please register or sign in to comment