What causes "irrefutable pattern failed for pattern" and what does it mean?
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)
Related videos on Youtube
Will Ness
Empirical orders of growth, please! primes = [2..] \ [[p², p²+p ..] | p <- primes] ((g<=<f)=<<) = (g=<<).(f=<<) = join.(g<$>).join.(f<$>) Monads as generalized function application condU:(g ~~>! h) f x = case g x of [] -> f x; (y:_) -> 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->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 <- [1..], y <- []] is just []. What's in a powerset? A set's subsets... (define (call/cc& proc& k) (proc& 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] } => do { x <- [1,2,3] ; do { y <- [4,5] ; return y }} => for x from [1,2,3] { for y from [4,5] { yield y }} => [ 4,5, 4,5, 4,5 ] Is your ( programming ) language ( high-level ) enough?
Updated on December 25, 2020Comments
-
Will Ness over 3 years
What does
irrefutable pattern failed for pattern
mean? What cases will cause this runtime error?
-
Random Dev almost 7 yearsI 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 over 6 years*deconstructing.