Changes between Version 2 and Version 3 of ViewPatternsAlternative


Ignore:
Timestamp:
Jun 12, 2011 9:16:38 AM (4 years ago)
Author:
augustss
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ViewPatternsAlternative

    v2 v3  
    3838That is, we add a new form of pattern, written
    3939{{{
    40    pattern | qual,,1,, , ..., qual,,n,,
     40   pattern | qual1 , ..., qualn
    4141}}}
    4242This has the same meaning as pattern guard at the top level, but here they are allowed to be part of a pattern, and thus nested inside other patterns.
     
    5353=== Semantics ===
    5454
    55 '''Scoping''' for ''pat `|` quals'':
     55'''Scoping''' for ''pat'' `|` ''quals'':
    5656 * The variables bound by the view pattern are the variables bound by ''pat'' and ''quals''.
    5757 * Variables bound by patterns to the left of a view pattern expression are in scope.  For example:
     
    6868
    6969'''Typing'''
    70 If ''pat `|` quals'' has same type as ''pat'', and the "quals" must be well typed in the same way as for pattern guards.
     70If ''pat'' `|` ''quals'' has same type as ''pat'', and the "quals" must be well typed in the same way as for pattern guards.
    7171
    7272'''Evaluation'''
     
    220220    errorVal :: Int -> Bool
    221221    errorVal = (== 4)
    222     f (x | errorVar x) =  ...
     222    f (x | errorVal x) =  ...
    223223}}}
    224224
     
    240240translates to
    241241[[BR]]
    242 ''x `|` pat `<-` exp x''  where ''x'' is a fresh variable.
    243 
     242''x'' `|` ''pat'' `<-` ''exp x''
     243[[BR]]
     244where ''x'' is a fresh variable.
     245
     246And in the other direction
     247[[BR]]
     248''pat'' `|` ''quals''
     249[[BR]]
     250translates to
     251[[BR]]
     252`Just` ''vs'' `<- let` ''f'' ''pat'' `|` ''quals'' `= Just` ''vs''`;` ''f'' `_ = Nothing in` ''f''
     253[[BR]]
     254where ''vs'' are all the variables bound in ''pat'' and ''quals'' and ''f'' is fresh.
     255
     256Notable differences:
     257  * The new style is a straightforward extension of existing syntax (pattern guards).
     258  * The new style allows more powerful matching without introducing an auxiliary type (typically a `Maybe`).  This leads to a more straightforward translation of pattern matching and better efficiency without sophisticated transformations.
     259  * The new style often has to introduce an extra variable to match the entire expression; a variable that is not needed on the right hand side.  This small pollution of the namespace can be avoided if the PatternSynonyms proposal is also implemented.
     260