Changes between Version 20 and Version 21 of PatternSynonyms


Ignore:
Timestamp:
Sep 22, 2013 6:42:13 AM (7 months ago)
Author:
cactus
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PatternSynonyms

    v20 v21  
    1  
    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). 
    202200 
     201'''TODO''': Syntax for associated pattern synonym declarations to discern between pattern-only and bidirectional pattern synonyms 
     202 
    203203== Typed pattern synonyms == 
    204204 
    205205So far patterns only had ''syntactic'' meaning. In comparison [http://code.google.com/p/omega Ωmega] has ''typed'' pattern synonyms, so they become first class values. (I am not suggesting this for Haskell, yet.) 
     206 
     207== Semantics == 
     208 
     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. 
     210 
     211 
     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: 
     213 
     214{{{ 
     215data S a where 
     216  MkS:: Num a -> a > S a 
     217data T a where 
     218  MkT :: Eq a => a -> T a 
     219}}} 
     220 
     221and we define this pattern synonym: 
     222 
     223{{{ 
     224pattern P :: (Eq a, Num a) => a -> a -> (P a, S a) 
     225pattern P x y = (MkT x, MkS y) 
     226}}} 
     227 
     228we can then write a function: 
     229 
     230{{{ 
     231f (P 1 2) = ... 
     232}}} 
     233 
     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.  
     235 
     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: 
     237 
     238{{{ 
     239f = \a -> case a of 
     240  (MkT x, MkS y) -> case x of 
     241    1 -> case y of 
     242      2 -> ... 
     243}}} 
     244 
     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`)