14

If I compare a string literal to a string literal using the case statement, I get the expected behavior: if they are the same - it matches, if they are not - it does not.

However, if I compare a string literal to a constant that is a string, I get "Pattern matches are overlapped" warning and the branch with the constant always matches.

Here's an example session:

Prelude> let var1 = "abc"
Prelude> let var2 = "def"
Prelude> case var1 of { var2 -> "Fail"; _ -> "Win" }

<interactive>:1:0:
    Warning: Pattern match(es) are overlapped
             In a case alternative: _ -> ...
"Fail"
Prelude> case "abc" of { var2 -> "Fail"; _ -> "Win" }

<interactive>:1:0:
    Warning: Pattern match(es) are overlapped
             In a case alternative: _ -> ...
"Fail"
Prelude> case "abc" of { "def" -> "Fail"; _ -> "Win" }
"Win"

Meanwhile, if behaves as expected:

> Prelude> if var1 == var2 then "Fail" else "Win" 
"Win"

What's going on here? How does this behavior make sense?

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
Max K
  • 306
  • 2
  • 6
  • 1
    I wish documentation and various tutorials where explicit that a) case statement is pattern matching with a different syntax and b) other than superficial similarity Haskell's case has nothing in common with C's switch. They solve different problems. – Max K Aug 14 '10 at 08:17
  • This is also good explained in the (free) book [Real World Haskell](http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html#id587485). – Flow Aug 22 '12 at 20:53

3 Answers3

30

See Don's answer for why. A common idiom for doing what you are trying to do is this:

var1 = "abc"
var2 = "def"

foo x = case () of
    () | x == var1 -> "Fail"
       | x == var2 -> "Failzor"
       | otherwise -> "WIN"

Of course in this case we would lose the case and just write the guards directly on the function:

foo x | x == var1 = "Fail"
      | ...

UPDATE

These days the MultiWayIf extension does this with slightly less syntactic noise.

{-# LANGUAGE MultiWayIf #-}

foo x = if | x == var1 -> "Fail"
           | x == var2 -> "Failzor"
           | otherwise -> "WIN"
luqui
  • 59,485
  • 12
  • 145
  • 204
22

Pattern matching in Haskell binds new variables. So when you write:

case x of
    y -> ...

you have now bound a new variable 'y' to the value of 'x'. This is the trivial "pattern". You can see more clearly how the binding works when a constructor is involved:

case x of 
    (a, b) -> ...

Now a and b bind to components of the tuple. And so on for deconstructing and binding other data types. Thus, to match a string literal, you would write:

case x of
    "def" -> ....
Don Stewart
  • 137,316
  • 36
  • 365
  • 468
9

That's because the "case" isn't doing what you think it is. The "var2" that was set to "def" is not being compared with "var1". Instead you are getting a new scope containing a new "var2" that is bound to the value of "var1".

The reason for the error message is that as far as the compiler is concerned there is no difference between "var2 ->..." and "_ -> ...". Both match all possible values of "var1".

Paul Johnson
  • 17,438
  • 3
  • 42
  • 59
  • I think it would be helpful for you to explain how to do it symbolically with variables, or to explain what the theory or reasoning is for choosing this behavior. – Evan Carroll Aug 13 '10 at 17:09
  • 2
    @Evan, I think the reasoning is about alpha conversion. That is, we don't want someone's decision to add a top level binding for `x` to drastically change the semantics of our pattern matches. – luqui Aug 13 '10 at 23:29