Pattern Match(es) are Overlapped - Pattern matching on Operators

haskell, pattern-matching

Solution

`(+)` and `(-)` are not constructors of the type `Integer -> Integer -> Integer`:

- They are not constructor names

- `Integer -> Integer -> Integer` is not an algebraic datatype

And so your code is equivalent to using any other variable names to bind the first argument, e.g.

test foo = "plus"
test bar = "minus"
test _   = "other"

which hopefully makes it clear that all three patterns actually match anything (and the first two bind some names). In other words, there is no way for the first pattern (`foo`, or `(+)` in your example) to fall through, which is why it overlaps with the remaining two.

Problem

I've come across a situation where I would like to pattern match on operators. However, this throws a `Pattern match(es) are overlapped` error with GHC. I can't figure out why. Is pattern matching on operators not allowed? I assume that since enclosing an operator symbol in parentheses converts it into an identifier, this should have worked. ``` test :: (Integer -> Integer -> Integer) -> String test (+) = "plus" test (-) = "minus" test _ = "other" ``` There are other ways I can accomplish what I want to do. I'm just curious as to why this doesn't work.

Original source