[Haskell] Re: Views in Haskell
Benjamin Franksen
benjamin.franksen at bessy.de
Mon Jan 22 18:49:15 EST 2007
David Roundy wrote:
> Another idea is whether the syntax could be extended to indicate a failure
> to match? This would actually be useful even without views, but it's
> particularly useful with views (and especially so in the context of the
> above warnings). I'd imagine something like (with stupidly chosen syntax
> of !!!)
>
> foo (_:_) = True
> foo _ = False
>
> foo' !!![] = True
> foo' _ = False
>
> Here I've defined two identical functions to describe what I mean by
> "!!!". I didn't gain anything in this case, but might gain some clarity if
> there
> are multiple constructors. But more to the point, if we're using views
> (of the vanilla Maybe-always variety), we could gain some efficiency this
> way.
>
> foo ([], view -> a, []) = foo1 a
> foo (x, !!! view ->, []) = foo2 x
> foo (_, view -> a, y) = foo3 a y
>
> This isn't a very good example, but the point is I'd like to be able to
> match on Nothing and get the same benefits you mention about the compiler
> being assumed to optimize by calling view only once. We could achieve
> this by reordering the patterns, but I believe (although I failed to come
> up with one above) that there are sets of pattern matches that aren't
> reducible in that way, which it'd be nice to be able to express succinctly
> by matching on failure to match a pattern.
>
> Maybe this should be
>
> foo (x, view /->, []) = foo2 x
>
> or something like that, to indicate failure, that view doesn't match?
AFAIU, this would be superseded by the "Possible extension 2" (which I
prefer anyway), i.e. drop the requirement that result type must be 'Maybe
a'. The 'cost' of explicitly mentioning constructors becomes an asset in
this case. For instance, your 2nd example becomes:
> foo ([], view -> Just a, []) = foo1 a
> foo (x, view -> Nothing, []) = foo2 x
> foo (_, view -> Just a, y) = foo3 a y
Clearer, IMHO.
Cheers
Ben
More information about the Haskell
mailing list