Changes between Version 19 and Version 20 of PatternSynonyms
 Timestamp:
 Sep 22, 2013 6:24:00 AM (18 months ago)
Legend:
 Unmodified
 Added
 Removed
 Modified

PatternSynonyms
v19 v20 1 2  1 3 = Pattern Synonyms = 2 4 Most language entities in Haskell can be named so that they can be abbreviated instead of written out in full. … … 66 68 The simplest form of pattern synonyms is the one from the examples above. The grammar rule is: 67 69 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'' 71 73 72 74 * Each of the variables on the left hand side must occur exactly once on the right hand side 73 75 * 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 79 There have been several proposals for the syntax of defining patternonly 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 75 85 76 86 Pattern synonyms can be exported and imported by prefixing the ''conid'' with the keyword `pattern`: … … 94 104 {{{ 95 105 pattern Arrow :: Type > Type > Type 96 pattern Arrow t1 t2 =App ">" [t1, t2]106 pattern Arrow t1 t2 > App ">" [t1, t2] 97 107 }}} 98 108 … … 102 112 import qualified Data.Sequence as Seq 103 113 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 == Implicitlybidirectional 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: 114 pattern Empty > (Seq.viewl > Seq.EmptyL) 115 pattern x :< xs > (Seq.viewl > x Seq.:< xs) 116 pattern xs :> x > (Seq.viewr > xs Seq.:> x) 117 }}} 118 119 120 == Simplybidirectional pattern synonyms == 121 122 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 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 130 For example, the following two pattern synonym definitions are rejected, because they are not bidirectional (but they would be valid as patternonly synonyms) 115 131 116 132 {{{ … … 131 147 == Explicitlybidirectional pattern synonyms == 132 148 133 What if you want to use `Plus1` from the earlier example in an expression? 149 What if you want to use `Succ` in an expression: 150 151 {{{ 152 pattern Succ n > n1  let n = n1 1, n >= 0 153 }}} 154 134 155 It's clearly impossible since its expansion is a pattern that has no meaning as an expression. 135 156 Nevertheless, 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. 136 157 This is the rationale for the most complicated synonyms, the bidirectional ones. They provide two expansions, one for patterns and one for expressions. 137 158 138 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' ` =` ''pat'' `where` ''cfunlhs'' ''rhs''159 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `>` ''pat'' `where` ''cfunlhs'' ''rhs'' 139 160 140 161 where ''cfunlhs'' is like ''funlhs'', except that the functions symbol is a ''conid'' instead of a ''varid''. … … 142 163 Example: 143 164 {{{ 144 pattern Plus1 n = n1  let n = n11, n >= 0 where 145 Plus1 n = n + 1 146 }}} 165 pattern Succ n > n1  let n = n11, n >= 0 where 166 Succ n = n + 1 167 }}} 168 169 '''TODO''': Rewrite this example to not use ViewPatternsAlternative 170 147 171 The first part as is before and describes the expansion of the synonym in patterns. The second part describes the expansion in expressions. 148 172 149 173 {{{ 150 174 fac 0 = 0 151 fac ( Plus1 n) = Plus1n * fac n175 fac (Succ n) = Succ n * fac n 152 176 }}} 153 177