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

11,245

Solution 1

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.

Solution 2

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.

Solution 3

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)
Share:
11,245

Related videos on Youtube

Will Ness
Author by

Will Ness

Empirical orders of growth, please! primes = [2..] \ [[p², p²+p ..] | p &lt;- primes] ((g&lt;=&lt;f)=&lt;&lt;) = (g=&lt;&lt;).(f=&lt;&lt;) = join.(g&lt;$&gt;).join.(f&lt;$&gt;) Monads as generalized function application condU:(g ~~&gt;! h) f x = case g x of [] -&gt; f x; (y:_) -&gt; h y apply (FUNARG lambda env) xs a = apply lambda xs env hammingSlice hi w = (c, sortBy (compare `on` fst) b) where 1:foldr (\n s-&gt;fix (merge s . (n:) . map (n*))) [] [2,3,5] ordfactors = foldr g [1] . reverse . primePowers where in a declarative language, length(a) == 0 is the same as null(a), and [y | x &lt;- [1..], y &lt;- []] is just []. What's in a powerset? A set's subsets... (define (call/cc&amp; proc&amp; k) (proc&amp; k k)) (define (list . xs) xs) How Monads are considered Pure? Monads are EDSLs are Nested Loops are Trees: do { [1,2,3] ; [4,5] } =&gt; do { x &lt;- [1,2,3] ; do { y &lt;- [4,5] ; return y }} =&gt; for x from [1,2,3] { for y from [4,5] { yield y }} =&gt; [ 4,5, 4,5, 4,5 ] Is your ( programming ) language ( high-level ) enough?

Updated on December 25, 2020

Comments

  • Will Ness
    Will Ness over 3 years

    What does

    irrefutable pattern failed for pattern

    mean? What cases will cause this runtime error?

  • Random Dev
    Random Dev almost 7 years
    I think that might be mistaken ~xyz (IMO it's called lazy pattern) is one example of an irrefutable pattern but not the only one - I just remark because I saw people calling ~ the irrefutable pattern
  • Will Ness
    Will Ness over 6 years
    *deconstructing.