Changes between Version 20 and Version 21 of PatternSynonyms

Sep 22, 2013 6:42:13 AM (2 years ago)



  • PatternSynonyms

    v20 v21  
    2 ----
    31= Pattern Synonyms =
    42Most language entities in Haskell can be named so that they can be abbreviated instead of written out in full.
    201199One could go one step further and leave out the `pattern` keyword to obtain ''associated constructors'', which are required to be bidirectional. The capitalized identifier would indicate that a pattern synonym is being defined. For complicated cases one could resort to the `where` syntax (shown above).
     201'''TODO''': Syntax for associated pattern synonym declarations to discern between pattern-only and bidirectional pattern synonyms
    203203== Typed pattern synonyms ==
    205205So far patterns only had ''syntactic'' meaning. In comparison [ Ωmega] has ''typed'' pattern synonyms, so they become first class values. (I am not suggesting this for Haskell, yet.)
     207== Semantics ==
     209It might seem tempting to just define pattern synonym semantics as 'textual substitution'. On the other hand, just like with any other surface language feature, we don't want to normalize away pattern synonyms before typechecking happens, since we want to report type error occurrences from user-written code.
     212These two goals are incompatible once you start to factor in patterns containing typeclass-polymorphic parts. For example, let's say we have these two GADTs:
     215data S a where
     216  MkS:: Num a -> a > S a
     217data T a where
     218  MkT :: Eq a => a -> T a
     221and we define this pattern synonym:
     224pattern P :: (Eq a, Num a) => a -> a -> (P a, S a)
     225pattern P x y = (MkT x, MkS y)
     228we can then write a function:
     231f (P 1 2) = ...
     234which needs to use `fromInteger` from the `Num` instance provided by the `MkS` constructor to be able to pattern-match on the argument of the `MkT` constructor.
     236This means when we desugar a pattern synonym occurrence, the whole of the right-hand side needs to be matched before the arguments are matched. So the previous definition of `f` is desugared corresponding to the following Haskell code:
     239f = \a -> case a of
     240  (MkT x, MkS y) -> case x of
     241    1 -> case y of
     242      2 -> ...
     245Of course, we don't actually generate Haskell code for that; instead, the implementation directly emits Core, in the same way Core is emitted for other pattern matchings (in `DsUtils.mkCoAlgCaseMatchResult`)