Changes between Version 4 and Version 5 of PatternSynonyms


Ignore:
Timestamp:
Aug 15, 2011 9:57:07 PM (3 years ago)
Author:
augustss
Comment:

Update syntax, add associated patterns.

Legend:

Unmodified
Added
Removed
Modified
  • PatternSynonyms

    v4 v5  
    6767The pattern only synonyms can have any pattern on the right hand side, but may only be used in patterns. 
    6868 
    69 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `~` ''pat'' 
     69`pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''pat'' 
    7070 
    71 Note the use of `~` instead of `=` as the equality symbol.  This serves as a syntactic cue that this is a pattern only synonym. 
    7271Again, 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. 
    7372 
    7473Examples: 
    7574{{{ 
    76    pattern ThirdElem x ~ _:_:x:_ 
     75   pattern ThirdElem x = _:_:x:_ 
    7776   pattern LazySecond a b ~ (a, ~b) 
    7877 
     
    9493Together with ViewPatternsAlternative we can now create patterns that look like regular patterns to match on existing (perhaps abstract) types in new ways. 
    9594{{{ 
    96    pattern Plus1 n ~ n1 | let n = n1-1, n >= 0 
     95   pattern Plus1 n = n1 | let n = n1-1, n >= 0 
    9796 
    9897   fac 0 = 0 
     
    107106This is the rational for the most complicated synonyms, the bidirectional ones.  They provide two expansions, one for patterns and one for expressions. 
    108107 
    109 `pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `~` ''pat'' `where` ''cfunlhs'' ''rhs'' 
     108`pattern` ''conid'' ''varid,,1,,'' ... ''varid,,n,,'' `=` ''pat'' `where` ''cfunlhs'' ''rhs'' 
    110109 
    111110where ''cfunlhs'' is like ''funlhs'', except that the functions symbol is a ''conid'' instead of a ''varid''. 
     
    113112Example: 
    114113{{{ 
    115    pattern Plus1 n ~ n1 | let n = n1-1, n >= 0 where 
     114   pattern Plus1 n = n1 | let n = n1-1, n >= 0 where 
    116115      Plus1 n = n + 1 
    117116}}} 
     
    123122}}} 
    124123 
     124== Associated Patterns Synonyms == 
     125Just like data types and type synonyms can be part of a class declaration, it would be possible to have pattern synonyms as well. 
    125126 
     127Example: 
     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}}}