Changes between Version 4 and Version 5 of ViewPatterns


Ignore:
Timestamp:
Jan 23, 2007 8:59:27 AM (7 years ago)
Author:
simonpj@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ViewPatterns

    v4 v5  
    406406Still the proposal does support the value input feature. 
    407407 
     408=== [http://portal.acm.org/citation.cfm?id=232641&coll=portal&dl=ACM Palao et al: active destructors (ICFP'96)] === 
     409 
     410Active Desctructors (ADs) are defined by a new form of top-level declaration.  Our 
     411singleton example would look like this: 
     412{{{ 
     413  Sing x match [x] 
     414}}} 
     415Here '''match''' is the keyword, and `Sing` is the AD.  ADs are quite like view patterns: 
     416the can do computation, and can fail to match.  But they are definitely not normal  
     417Haskell functions, and need their own form of top-level declaration.  They even have 
     418a special form of type to describe them. 
     419 
     420The value-input feature is supported, but only via a sort of mode declaration (indicated by a down-arrow) on the 
     421new form of type. 
     422 
     423They also introduce a combining form for ADs, to make a kind of and-pattern.  For 
     424example, suppose we had 
     425{{{ 
     426  Head x match (x:_) 
     427  Tail x match (_:xs) 
     428 
     429  f :: [a] -> [a] 
     430  f ((Head x)@(Tail ys)) = x:x:ys 
     431}}} 
     432Here `(Head x)@(Tail ys)` is a pattern that matches ''both'' `(Head x)` and `(Tail ys)` 
     433against the argument, binding `x` and `ys` respectively.  We can model that with view patterns, 
     434only a bit more clumsily: 
     435{{{ 
     436  headV (x:xs) = Just x 
     437  headV []     = Nothing 
     438 
     439  tailV (x:xs) = Just xs 
     440  tailV []     = Nothing 
     441 
     442  (@) :: (a -> Maybe b) -> (a -> Maybe c) -> a -> Maybe (b,c) 
     443  (f @ g) x = do { b <- f x; c <- g x; return (b,c) } 
     444 
     445  f :: [a] -> [a] 
     446  f (headV @ tailV -> (x,ys)) = x:x:ys 
     447}}} 
     448The clumsiness is that the "`@`" combines functions, with a kind of positional 
     449binding; the pattern `(x,ys)` is separated from the combiner so that it's less clear 
     450that `headV` binds `x` and `tailV` binds `y`. 
     451 
     452In exchange, although view patterns are a bit less convenient here, they  
     453are a much, much smaller language change than ADs. 
     454 
    408455=== [http://citeseer.ist.psu.edu/erwig00pattern.html Erwig/Peyton Jones: transformational patterns] === 
    409456