[Haskell] Views in Haskell

Brian Hulley brianh at metamilk.com
Tue Jan 23 23:15:49 EST 2007


Simon Peyton-Jones wrote:
>
> http://hackage.haskell.org/trac/haskell-prime/wiki/ViewPatterns
>
> I'm thinking of implementing it in GHC, so I'd be interested in
> feedback of the form
>         - how desirable is it to have a feature of this general form?
>         - can this particular proposal be improved?

Regarding the syntax, I don't like the use of ->, because I think it is 
confusing to write

    even -> n

when even is the function itself. In all other places in Haskell, A -> B 
represents a function from A to B whereas in the proposal A -> B represents 
the function A applied to the thing currently being matched which may return 
a value wrapped up in Maybe which matches B.

There are 2 things - the inconsistent use of -> and the implicit Maybe.

I'd rather the Maybe was not implicit (as described in extension 2), and a 
more consistent syntax was found. A possible syntax could represent the 
value being matched explicitly, say using ? to represent the value currently 
being matched, then the pattern could be written as an equation:

    f (prodSize ? = Small) = ...
    f (prodSize ? = Medium) = ...
    f (prodSize ? = Big) = ...

or simulating an n + k pattern:

    g (? - 3 = n) = ...

or a function which checks its arg satisfies an arbitrary expression:

    h (? - (3 * ?) = 10) = ...

or an "n+k" against the second element of a list

    j ( toList ? = (_ : (? - 3 = n : _))) = ...

Regarding the problem itself, I'm still not sure why views are needed. For 
example in (f) above, why not just write as:

    f :: Product -> ...
    f prod = k (prodSize prod) where k = ...

Best regards, Brian.
-- 
http://www.metamilk.com 



More information about the Haskell-prime mailing list