[Haskell-cafe] Prolog-style patterns

Roman Cheplyaka roma at ro-che.info
Tue Apr 9 12:27:51 CEST 2013


* Daniel Trstenjak <daniel.trstenjak at gmail.com> [2013-04-09 09:37:46+0200]
> 
> Hi Roman,
> 
> > One issue with it in Haskell is that it'd lead to inconsistent
> > semantics:
> > 
> >   myEq x x = True
> > 
> > is not the same as
> > 
> >   myEq x y =
> >     case y of
> >       x -> True
> 
> I don't think that it's inconsistent, because the 'case' defines a new name
> scope, like the function does for its arguments.

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



More information about the Haskell-Cafe mailing list