Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes "irrefutable pattern failed for pattern" and what does it mean?

Consider this example:

foo ~(Just x) = "hello"
main = putStrLn $ foo Nothing

This uses an irrefutable pattern (the ~ part). Irrefutable patterns always "match", so this prints hello.

foo ~(Just x) = x
main = putStrLn $ foo Nothing

Now, the pattern still matched, but when we tried to use x when it wasn't actually there there we got an irrefutable pattern match error:

Irr.hs: /tmp/Irr.hs:2:1-17: Irrefutable pattern failed for pattern (Data.Maybe.Just x)

This is subtly distinct from the error you get when there's no matching pattern:

foo (Just x) = x
main = putStrLn $ foo Nothing

This outputs

Irr.hs: /tmp/Irr.hs:2:1-16: Non-exhaustive patterns in function foo

Of course, this is a somewhat contrived example. The more likely explanation is that it came from a pattern in a let binding, as chrisdb suggested.


Well, I assume it means what it says - that a pattern doesn't match but there is no alternative. This example:

But for the program:

g x = let Just y = f x in h y 

GHC reports:

Main: M1.hs:9:11-22:
    Irrefutable pattern failed for pattern Data.Maybe.Just y 

Indicating the source of the failure.

Comes from http://www.haskell.org/haskellwiki/Debugging

The point of the example is that if f x returns Nothing then there is no way GHC can assign a value to y.


To add what others have said, you could technically get it if you're disconnecting a list that's smaller than what you're intending. For example (in GHCi):

Prelude> let l = [1,2,3]
Prelude> let (x:x1:xs) = l
Prelude> x
1

Works fine, but if you did:

Prelude> let l2 = [1]
Prelude> let (x:x1:xs) = l2
Prelude> x
*** Exception: <interactive>:294:5-18: Irrefutable pattern failed for pattern (x : x1 : xs)