Hi,
I believe one problem with non-linear patterns would be that the
compiler has to figure out what notion of equality you want here. An
obvious choice is (==), but the Eq instances might not do what you want.
Using pattern guards or view patterns explicates this choice.
> Hi all,
>
> consider this simple reimplementation of 'elem' function:
>
> member :: Eq a => a -> [a] -> Bool
> member _ [] = False
> member y (x:xs) | x == y = True
> | otherwise = member y xs
>
> If Haskell allowed to write pattern matching similar to Prolog then we could write this function
> like this:
>
> member :: Eq a => a -> [a] -> Bool
> member _ [] = False
> member x (x:_) = True
> member x (_:xs) = member x xs
This kind of pattern matching was considered and rejected by the
very first Haskell committee. Others have given some of the
reasoning, and I don’t recall the rest so won’t attempt to
rehearse them here. What I would like to say (without meaning to
attack you personally, Jan) is that we really need to make a
rule that this sort of small convenience should never be
considered.
Every now and a language feature will be invented that really
makes a large difference to a large number of programmes (do
notation was a prime example), so the language should evolve.
But against that, there is considerable value in having the
basic syntax remain unchanged for as long as possible.
I don’t know how to formulate it formally, but the rule should
be something like, unless a new feature shortens programmes that
can use it by a significant factor, it shouldn’t be included.
--
Jón Fairbairn Jon.Fa...@cl.cam.ac.uk
One should interpret consecutive function arguments as being in the
nested scopes, too, rather than in one flat scope. Otherwise, in
x = 2
f x ((x ==) -> True) = True
the 'x' in the view pattern would refer to the global x, rather than the
function parameter. (And it would, indeed, if you swap the patterns.)
The same applies to scoped type variables.
(Haskell2010 does not have either of these extensions, so there the two
interpretations — nested scopes and one flat scope — are equivalent.)
> Otherwise you would also expect a different behavior for:
>
> x = 2
>
> myEq x x = True
In fact, lots of Haskell newcomers are surprised that
f 10 = 42
is not the same as
n = 10
f n = 42
And this proposal further reinforces the impression that
pattern-matching against a bound variable is interpreted as an equality
test.
Roman
Oh, don't get me wrong — I'd never actually suggest that semantics.
Just saying that allowing one and not other would confuse the newcomers
about how pattern matching actually works. So I prefer to stay on the
simple and consistent side.
Roman