Changes between Version 19 and Version 20 of PatternSynonyms


Ignore:
Timestamp:
Sep 22, 2013 6:24:00 AM (2 years ago)
Author:
cactus
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PatternSynonyms

    v19 v20  
     1
     2----
    13= Pattern Synonyms =
    24Most language entities in Haskell can be named so that they can be abbreviated instead of written out in full.
     
    6668The simplest form of pattern synonyms is the one from the examples above.  The grammar rule is:
    6769
    68 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''pat''
    69 
    70 `pattern` ''varid,,1,,'' ''consym'' ''varid,,2,,'' `=` ''pat''
     70`pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `->` ''pat''
     71
     72`pattern` ''varid,,1,,'' ''consym'' ''varid,,2,,'' `->` ''pat''
    7173
    7274 * Each of the variables on the left hand side must occur exactly once on the right hand side
    7375 * Pattern synonyms are not allowed to be recursive.  Cf. type synonyms.
    74  * The semantics is simply expansion of the synonym.
     76
     77||='''TODO'''=||
     78{{{#!td
     79There have been several proposals for the syntax of defining pattern-only synonyms:
     80* `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `~` ''pat''
     81* `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `:=` ''pat''
     82* `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `->` ''pat''
     83}}}
     84
    7585
    7686Pattern synonyms can be exported and imported by prefixing the ''conid'' with the keyword `pattern`:
     
    94104{{{
    95105   pattern Arrow :: Type -> Type -> Type
    96    pattern Arrow t1 t2 = App "->" [t1, t2]
     106   pattern Arrow t1 t2 -> App "->" [t1, t2]
    97107}}}
    98108
     
    102112import qualified Data.Sequence as Seq
    103113
    104 pattern Empty = (Seq.viewl -> Seq.EmptyL)
    105 pattern x :< xs = (Seq.viewl -> x Seq.:< xs)
    106 pattern xs :> x = (Seq.viewr -> xs Seq.:> x)
    107 }}}
    108 
    109 
    110 == Implicitly-bidirectional pattern synonyms ==
    111 
    112 In cases where ''pat'' is in the intersection of the grammars for patterns and expressions (i.e. is valid both as an expression and a pattern), the pattern synonym is said to be bidirectional, and can be used in expression contexts as well.
    113 
    114 For example, the following two are not bidirectional:
     114pattern Empty -> (Seq.viewl -> Seq.EmptyL)
     115pattern x :< xs -> (Seq.viewl -> x Seq.:< xs)
     116pattern xs :> x -> (Seq.viewr -> xs Seq.:> x)
     117}}}
     118
     119
     120== Simply-bidirectional pattern synonyms ==
     121
     122In cases where ''pat'' is in the intersection of the grammars for patterns and expressions (i.e. is valid both as an expression and a pattern), the pattern synonym can be made bidirectional, and can be used in expression contexts as well. Bidirectional pattern synonyms have the following syntax:
     123
     124`pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''pat''
     125
     126`pattern` ''varid,,1,,'' ''consym'' ''varid,,2,,'' `=` ''pat''
     127
     128
     129
     130For example, the following two pattern synonym definitions are rejected, because they are not bidirectional (but they would be valid as pattern-only synonyms)
    115131
    116132{{{
     
    131147== Explicitly-bidirectional pattern synonyms ==
    132148
    133 What if you want to use `Plus1` from the earlier example in an expression?
     149What if you want to use `Succ` in an expression:
     150
     151{{{
     152    pattern Succ n -> n1 | let n = n1 -1, n >= 0
     153}}}
     154
    134155It's clearly impossible since its expansion is a pattern that has no meaning as an expression.
    135156Nevertheless, if we want to make what looks like a constructor for a type we will often want to use it in both patterns and expressions.
    136157This is the rationale for the most complicated synonyms, the bidirectional ones.  They provide two expansions, one for patterns and one for expressions.
    137158
    138 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''pat'' `where` ''cfunlhs'' ''rhs''
     159`pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `->` ''pat'' `where` ''cfunlhs'' ''rhs''
    139160
    140161where ''cfunlhs'' is like ''funlhs'', except that the functions symbol is a ''conid'' instead of a ''varid''.
     
    142163Example:
    143164{{{
    144    pattern Plus1 n = n1 | let n = n1-1, n >= 0 where
    145       Plus1 n = n + 1
    146 }}}
     165   pattern Succ n -> n1 | let n = n1-1, n >= 0 where
     166      Succ n = n + 1
     167}}}
     168
     169'''TODO''': Rewrite this example to not use ViewPatternsAlternative
     170
    147171The first part as is before and describes the expansion of the synonym in patterns. The second part describes the expansion in expressions.
    148172
    149173{{{
    150174   fac 0 = 0
    151    fac (Plus1 n) = Plus1 n * fac n
     175   fac (Succ n) = Succ n * fac n
    152176}}}
    153177