23

I just wondered whether it's possible to match against the same values for multiple times with the pattern matching facilities of functional programming languages (Haskell/F#/Caml).

Just think of the following example:

plus a a = 2 * a
plus a b = a + b

The first variant would be called when the function is invoked with two similar values (which would be stored in a).

A more useful application would be this (simplifying an AST).

simplify (Add a a) = Mult 2 a

But Haskell rejects these codes and warns me of conflicting definitions for a - I have to do explicit case/if-checks instead to find out whether the function got identical values. Is there any trick to indicate that a variable I want to match against will occur multiple times?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Dario
  • 48,658
  • 8
  • 97
  • 130

4 Answers4

43

This is called a nonlinear pattern. There have been several threads on the haskell-cafe mailing list about this, not long ago. Here are two:

http://www.mail-archive.com/haskell-cafe@haskell.org/msg59617.html

http://www.mail-archive.com/haskell-cafe@haskell.org/msg62491.html

Bottom line: it's not impossible to implement, but was decided against for sake of simplicity.

By the way, you do not need if or case to work around this; the (slightly) cleaner way is to use a guard:

a `plus` b
  | a == b = 2*a
  | otherwise = a+b
Thomas
  • 174,939
  • 50
  • 355
  • 478
15

You can't have two parameters with the same name to indicate that they should be equal, but you can use guards to distinguish cases like this:

plus a b
  | a == b    = 2 * a
  | otherwise = a + b

This is more flexible since it also works for more complicated conditions than simple equality.

sth
  • 222,467
  • 53
  • 283
  • 367
0

I have just looked up the mailing list threads given in Thomas's answer, and the very first reply in one of them makes good sense, and explains why such a "pattern" would not make much sense in general: what if a is a function? (It is impossible in general to check it two functions are equal.)

Alexey
  • 3,843
  • 6
  • 30
  • 44
  • 1
    Couldn't it just constrain `a` to be `Eq`? – gdejohn Jul 23 '15 at 22:10
  • 2
    @gdejohn, i suspect that the semantics would not be right. When applying a definition of the form `f x x = x`, one could reasonably expect IMO that the two submitted arguments are *the same value*, and not just "equal" in the sense of some typeclass (otherwise it is not even clear which of the two values of `x` should `f` return). – Alexey Jul 24 '15 at 10:47
-4

I have implemented a new functional programming language that can handle non-linear patterns in Haskell.

https://github.com/egison/egison

In my language, your plus function in written as follow.

(define $plus
  (match-lambda [integer integer]
    {[[$a ,a] (* a 2)]
     [[$a $b] (+ a b)]}))
egi
  • 41
  • 3