Match Match overlap - pattern matching by operator

I came across a situation where I would like to match a template for operators. However, this causes a Pattern match(es) are overlapped error with the GHC. I can’t understand why. Are pattern matching allowed on statements? I assume that since the operator character converts it into an identifier in parentheses, this should have worked.

 test :: (Integer -> Integer -> Integer) -> String test (+) = "plus" test (-) = "minus" test _ = "other" 

There are other ways that I can accomplish what I want. I'm just wondering why this is not working.

+6
source share
1 answer

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

  • They are not constructor names
  • Integer -> Integer -> Integer not an algebraic data type

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

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

which, we hope, makes it clear that all three patterns actually correspond to something (and the first two are linked by some names). In other words, for the first template ( foo or (+) in your example) there is no way to skip, so it overlaps with the remaining two.

+12
source

Source: https://habr.com/ru/post/972234/


All Articles