Changes between Version 4 and Version 5 of PatternSynonyms
- Timestamp:
- 08/15/11 14:57:07 (22 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
PatternSynonyms
v4 v5 67 67 The pattern only synonyms can have any pattern on the right hand side, but may only be used in patterns. 68 68 69 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' ` ~` ''pat''69 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''pat'' 70 70 71 Note the use of `~` instead of `=` as the equality symbol. This serves as a syntactic cue that this is a pattern only synonym.72 71 Again, each of the variables on the left hand side must be mentioned exactly once of 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. 73 72 74 73 Examples: 75 74 {{{ 76 pattern ThirdElem x ~_:_:x:_75 pattern ThirdElem x = _:_:x:_ 77 76 pattern LazySecond a b ~ (a, ~b) 78 77 … … 94 93 Together with ViewPatternsAlternative we can now create patterns that look like regular patterns to match on existing (perhaps abstract) types in new ways. 95 94 {{{ 96 pattern Plus1 n ~n1 | let n = n1-1, n >= 095 pattern Plus1 n = n1 | let n = n1-1, n >= 0 97 96 98 97 fac 0 = 0 … … 107 106 This is the rational for the most complicated synonyms, the bidirectional ones. They provide two expansions, one for patterns and one for expressions. 108 107 109 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' ` ~` ''pat'' `where` ''cfunlhs'' ''rhs''108 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''pat'' `where` ''cfunlhs'' ''rhs'' 110 109 111 110 where ''cfunlhs'' is like ''funlhs'', except that the functions symbol is a ''conid'' instead of a ''varid''. … … 113 112 Example: 114 113 {{{ 115 pattern Plus1 n ~n1 | let n = n1-1, n >= 0 where114 pattern Plus1 n = n1 | let n = n1-1, n >= 0 where 116 115 Plus1 n = n + 1 117 116 }}} … … 123 122 }}} 124 123 124 == Associated Patterns Synonyms == 125 Just like data types and type synonyms can be part of a class declaration, it would be possible to have pattern synonyms as well. 125 126 127 Example: 128 {{{ 129 class ListLike l where 130 pattern Nil :: l a 131 pattern Cons :: a -> l a -> a 132 isNil :: l a -> Bool 133 isNil Nil = True 134 isNil (Cons _ _) = False 135 append :: l a -> l a -> l a 136 137 instance ListLike [] where 138 pattern Nil = [] 139 pattern Cons x xs = x:xs 140 append = (++) 141 }}}
