Changes between Version 4 and Version 5 of ViewPatterns

Jan 23, 2007 8:59:27 AM (11 years ago)
Simon Peyton Jones


  • ViewPatterns

    v4 v5  
    406406Still the proposal does support the value input feature.
     408=== [ Palao et al: active destructors (ICFP'96)] ===
     410Active Desctructors (ADs) are defined by a new form of top-level declaration.  Our
     411singleton example would look like this:
     413  Sing x match [x]
     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.
     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.
     423They also introduce a combining form for ADs, to make a kind of and-pattern.  For
     424example, suppose we had
     426  Head x match (x:_)
     427  Tail x match (_:xs)
     429  f :: [a] -> [a]
     430  f ((Head x)@(Tail ys)) = x:x:ys
     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:
     436  headV (x:xs) = Just x
     437  headV []     = Nothing
     439  tailV (x:xs) = Just xs
     440  tailV []     = Nothing
     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) }
     445  f :: [a] -> [a]
     446  f (headV @ tailV -> (x,ys)) = x:x:ys
     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`.
     452In exchange, although view patterns are a bit less convenient here, they
     453are a much, much smaller language change than ADs.
    408455=== [ Erwig/Peyton Jones: transformational patterns] ===