Changes between Version 1 and Version 2 of PatternSynonyms/Implementation


Ignore:
Timestamp:
Dec 9, 2013 4:44:04 AM (21 months ago)
Author:
cactus
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PatternSynonyms/Implementation

    v1 v2  
    1 = Frontend representation
     1== Implementation notes for pattern synonyms ==
     2
     3== Frontend representation ==
    24
    35After parsing, and during renaming, pattern synonyms are stored as
    4 `TyClDecl`s.
     6`HsBind`s.
    57
    6 = Typechecking
     8== Renaming ==
    79
    8 The typechecking pass turns `PatSynDecl`s into a `PatSyn` and several
     10During renaming, pattern synonyms are put in recursive binding groups
     11together with function bindings. Other pattern synonyms and
     12ViewPatterns can cause recursive pattern synonym declarations, which
     13are rejected.
     14
     15
     16== Typechecking ==
     17
     18The typechecking pass turns `PatSynBind`s into a `PatSyn` and several
    919`HsBind`s. To fill in the `PatSyn`, we typecheck the right-hand side
    1020of the pattern synonym declaration, then do some extra processing on
     
    1828  be consulted when typechecking pattern synonym usage sites.
    1929
    20 * The first `HsBind` is the binder for the matcher function generated from
    21   the pattern synonym. The matcher is used when desugaring pattern
    22   synonym usage sites (see below).
     30* The first `HsBind` is the binder for the //matcher// function
     31  generated from the pattern synonym. The matcher is used when
     32  desugaring pattern synonym usage sites (see below).
    2333
    2434* For bidirectional pattern synonyms, another `HsBind` called a
    25   wrapper is created to be used for pattern synonym usages in
     35  //wrapper// is created to be used for pattern synonym usages in
    2636  expression contexts. It is a wrapper in the same sense as a
    2737  constructor wrapper.
     
    3444
    3545
    36 = Desugaring
     46== Desugaring ==
    3747
    38 `ConLike`s are handled uniformly all the way until
    39 `mkCoAlgCaseMatchResult`. There, we have a mixed list of `DataCon` and
    40 `PatSyn`-based patterns.
     48=== Grouping ===
    4149
    42 
    43 === Grouping
    44 
    45 This list is grouped so that subsequent `DataCon` patterns are put in
    46 the same group, and `PatSyn` patterns are all in their own
    47 groups. This is needed so that when doing pattern matching per column,
    48 given e.g.
    49 
     50During match grouping, subsequent `PatSyn` patterns are combined into
     51one group per pattern synonym. For example, given the following code:
    5052
    5153{{{
    52 data T = MkT1 | MkT2 Bool | MkT3
    53 pattern P x = MkT2 x
     54pattern Single x <- [x]
     55pattern Pair x y <- [x, y]
     56
     57f []             = 0
     58f (Single True)  = 1
     59f (Single False) = 2
     60f (Pair _ _)     = 3
     61f (_:_:_)        = 4
    5462}}}
    5563
    56 and a list of cases
     64the two `Single` patterns are put in one `PgSyn` `PatGroup`.
    5765
    58 
    59 {{{
    60 MkT1 _ -> alt1
    61 P True -> alt2
    62 MkT2 _ -> alt3
    63 }}}
    64 
    65 we don't compile that into
    66 
    67 
    68 {{{
    69 DEFAULT -> ... P ...
    70 MkT1 -> alt1
    71 MkT2 _ -> alt3
    72 }}}
    73 
    74 since we can't see into P (and we don't want to, since it might be
    75 imported from another module). The correct thing to do is to compile
    76 that into
    77 
    78 
    79 {{{
    80 DEFAULT -> ... (P, alt2) and (MkT2 _, alt3) ...
    81 MkT1 -> alt1
    82 }}}
    83 
    84 Consecutive occurances of the same pattern synonym (e.g. if we had `P
    85 True` and `P False` in the previous example) are compiled into a
    86 single match; the arguments are then matched in a sub-case.
    87 
    88 
    89 == Matching
     66=== Matching ===
    9067
    9168For each pattern synonym, a matcher function is generated which gets a
     
    9976and a pattern synonym
    10077
    101 
    10278{{{
    103 pattern P x y = MkT x y
     79pattern Pat x y = MkT x y
    10480}}}
    10581
    10682we generate the matcher function
    10783
    108 
    10984{{{
    110 P :: forall r a. T a -> (forall b. Cls b => b -> a -> r) -> r -> r
    111 P scrutinee pass fail = case scrutinee of
     85$mPat :: forall r a. T a -> (forall b. Cls b => b -> a -> r) -> r -> r
     86$mPat scrutinee pass fail = case scrutinee of
    11287  MkT x y -> pass x y
    11388  _ -> fail