Re: [Haskell] Views in Haskell

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

On 1/24/07, Brian Hulley
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 maybe (Small = prodSize ?), etc., to be consistent with let bindings?

On Wednesday, January 24, 2007 10:02 AM, Dinko Tenev wrote:
On 1/24/07, Brian Hulley
wrote: 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 maybe (Small = prodSize ?), etc., to be consistent with let bindings?
I like it! Just to fix a minor error in one of my previous examples, and to show it in let-compatible form: -- "n+3" pattern matching against 2nd element j (_ : (n = ? - 3) : _ = toList ?) = ... Also, perhaps the "binding" could be optional for "True = ", so that: test (True = isAlpha ?) = ... could just be written as: test (isAlpha ?) = ... (The presence of '?' in the pattern is enough to specify that it's an "active" pattern) Here is another example, in current syntax and in "new" syntax: old (n + 3) | 0 < n = ... -- using idiosyncratic n+k pattern new (n = ? - 3) | 0 < n = ... -- rational reconstruction or new (n @ (0 < ?) = ? - 3) = ... -- moving guard into pattern Brian. -- http://www.metamilk.com
participants (2)
-
Brian Hulley
-
Dinko Tenev