Changes between Version 8 and Version 9 of PatternSynonyms
 Timestamp:
 Feb 26, 2012 9:18:59 PM (6 years ago)
Legend:
 Unmodified
 Added
 Removed
 Modified

PatternSynonyms
v8 v9 9 9 }}} 10 10 Using this representations the arrow type looks like `App ">" [t1, t2]`. 11 Here are functions t o collect are all argument types of nested arrows and recognizingthe `Int` type:11 Here are functions that collect all argument types of nested arrows and recognize the `Int` type: 12 12 {{{ 13 13 collectArgs :: Type > [Type] … … 18 18 isInt _ = False 19 19 }}} 20 Matching on the arrow typeis both hard to read and error prone to write.20 Matching on `App` directly is both hard to read and error prone to write. 21 21 22 The proposal is to introduce a way to give pattern names:22 The proposal is to introduce a way to give patterns names: 23 23 {{{ 24 24 pattern Arrow t1 t2 = App ">" [t1, t2] … … 35 35 }}} 36 36 37 Furthermore, the pattern synonymcan also be used in expressions, e.g.,37 Furthermore, pattern synonyms can also be used in expressions, e.g., 38 38 {{{ 39 39 arrows :: [Type] > Type > Type … … 42 42 43 43 == Simple pattern synonyms == 44 The simplest form of pattern synonyms is the one from the examples above. The grammar rule is simply:44 The simplest form of pattern synonyms is the one from the examples above. The grammar rule is: 45 45 46 46 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''patexp'' 47 47 48 where ''patexp'' is simply the intersection of the grammars for patterns and expression, i.e., those terms that are valid both as a pattern and as an expressions.49 * Each of the variables on the left hand side must occur exactly on e on the right hand side, and these are the only variables that can be mentionon the right hand side.48 where ''patexp'' is the intersection of the grammars for patterns and expression, i.e., those terms that are valid both as a pattern and as an expression. 49 * Each of the variables on the left hand side must occur exactly once on the right hand side, and these are the only variables that can occur on the right hand side. 50 50 * Pattern synonyms are not allowed to be recursive. Cf. type synonyms. 51 * The semantics is simply given byexpansion of the synonym.51 * The semantics is simply expansion of the synonym. 52 52 53 Pattern synonyms can be exported and imported by mentioning the ''conid'' in the export/import list. Note that this suffers from the same constructor vs type confusion that already exists in `hiding` list, i.e., given the mention of a ''conid'' you cannot tell if it refers to a constructor or a type.53 Pattern synonyms can be exported and imported by mentioning the ''conid'' in the export/import list. Note that this suffers from the same constructor vs type confusion that already exists in a `hiding` list, i.e., given the mention of a ''conid'' you cannot tell if it refers to a constructor or a type. 54 54 55 55 You may also give a type signature for a pattern, but as with most other type signatures in Haskell it is optional: … … 64 64 65 65 == Pattern only synonyms == 66 The simple patterns synonyms are restricted to having a right hand side that is also a valid expression.67 The pattern only synonyms can have any pattern on the right hand side, but may only be used in patterns.66 Simple patterns synonyms are restricted to having a right hand side that is also a valid expression. 67 Pattern only synonyms can have any pattern on the right hand side, but may only be used in patterns. 68 68 69 69 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''pat'' 70 70 71 Again, each of the variables on the left hand side must be mentioned exactly once o f the right hand side, but the right hand side can mention other variables as well. These variables will not be bound by using the pattern synonyms.71 Again, each of the variables on the left hand side must be mentioned exactly once on the right hand side, but now the right hand side can mention other variables as well. These variables will not be bound when using the pattern synonym. 72 72 73 73 Examples: … … 101 101 102 102 == Bidirectional pattern synonyms == 103 What if you want to use `Plus1` from the earlier example in an expression .103 What if you want to use `Plus1` from the earlier example in an expression? 104 104 It's clearly impossible since its expansion is a pattern that has no meaning as an expression. 105 Nevertheless, if we want to make what looks like constructors for a type we will often want to use themin both patterns and expressions.106 This is the rational for the most complicated synonyms, the bidirectional ones. They provide two expansions, one for patterns and one for expressions.105 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. 106 This is the rationale for the most complicated synonyms, the bidirectional ones. They provide two expansions, one for patterns and one for expressions. 107 107 108 108 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''pat'' `where` ''cfunlhs'' ''rhs'' … … 115 115 Plus1 n = n + 1 116 116 }}} 117 The first part as is before and describes the expansion of the synonym in patterns , whereas the seconddescribes the expansion in expressions.117 The first part as is before and describes the expansion of the synonym in patterns. The second part describes the expansion in expressions. 118 118 119 119 {{{