This tag concerns pattern match coverage errors or warnings in functional programming languages with algebraic data types and pattern matching. Questions with this tag can be about why a compiler detects missing patterns or why missing patterns cause an error at run time.
Questions tagged [non-exhaustive-patterns]
85 questions
34
votes
2 answers
In Haskell, why non-exhaustive patterns are not compile-time errors?
This is a follow-up of Why am I getting "Non-exhaustive patterns in function..." when I invoke my Haskell substring function?
I understand that using -Wall, GHC can warn against non-exhaustive patterns. I'm wondering what's the reason behind not…

gawi
- 13,940
- 7
- 42
- 78
33
votes
2 answers
Non-exhaustive patterns in function
I've got a problem with this code, it should count the longest substring of the same letter in a string, but there is an error:
*** Exception: test.hs:(15,0)-(21,17):
Non-exhaustive patterns in function countLongest'
I know that is the wrong types…

kaz
- 1,943
- 1
- 13
- 19
29
votes
3 answers
Why is GHC complaining about non-exhaustive patterns?
When I compile the following code with GHC (using the -Wall flag):
module Main where
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
insert :: (Ord a) => a -> Tree a -> Tree a
insert x EmptyTree = Node x EmptyTree…

Alexandros
- 3,044
- 1
- 23
- 37
12
votes
2 answers
How to systematically avoid unsafe pattern matching in Scala?
Consider the following broken function:
def sum (list : Seq[Int]) : Int = list match {
case Nil => 0
case head :: tail => head + sum(tail)
}
Here, the function was supposed to work with a List[Int], but was refactored to accept Seq[Int]…

Rotsor
- 13,655
- 6
- 43
- 57
11
votes
2 answers
Haskell: non-exhaustive-patterns
I am training for a test tomorrow to complete my introduction to functional programming but there is one thing I don't understand.
Whenever I have a program like:
test [] = []
test (x:xs) = test (xs)
What he does is that he takes the first element…

Hans van der Laan
- 535
- 5
- 18
10
votes
1 answer
Why am I getting "Non-exhaustive patterns in function..." when I invoke my Haskell substring function?
I'm working my way through the book The Haskell Road to Logic, Maths and Programming. (I'm only mid-way through chapter 1, but I'm enjoying it so far and intend to continue.) I've read through the section 1.5 "Playing the Haskell Game" which…

Daryl Spitzer
- 143,156
- 76
- 154
- 173
8
votes
1 answer
GHC complains about non-exhaustive patterns that are enforced by the type checker
I have the following code
{-# LANGUAGE DataKinds, GADTs, TypeOperators #-}
data Vect v a where
Nil :: Vect '[] a
Vec :: a -> Vect v a -> Vect (() ': v) a
instance Eq a => Eq (Vect v a) where
(==) Nil Nil = True
(Vec…

user2407038
- 14,400
- 3
- 29
- 42
6
votes
2 answers
Match expression on Int is not exhaustive
I've started learning Scala.
I was surprised that next code compiles:
object Hello extends App {
def isOne(num: Int) = num match {
case 1 => "hello"
}
}
You can't do something similar in Rust for example.
Why Scala compiler does not force…

captain-yossarian from Ukraine
- 31,174
- 3
- 30
- 62
6
votes
2 answers
Surjectivity check when return type is sealed
Scala can warn when pattern match on a sealed type is not exhaustive, however can we check that a function returns all cases when the return type is sealed? For example, consider the following ADT
sealed trait Foo
case object Bar extends Foo
case…

Mario Galic
- 47,285
- 6
- 56
- 98
5
votes
2 answers
How do I relax the non-exhaustive patterns check for a nested match on known variants?
How do I persuade the Rust compiler that the internal match expression is fine here, as the outer match has already restricted the possible types?
enum Op {
LoadX,
LoadY,
Add,
}
fn test(o: Op) {
match o {
Op::LoadX |…

dying_sphynx
- 1,136
- 8
- 17
5
votes
3 answers
Why doesn't this definition cover all pattern cases?
So I'm trying to triplize an element, i.e. making 2 other copies of the element.
So I've written this:
triplize :: [a] -> [a]
triplize [x] = concatMap (replicate 3) [x]
But I've been getting this error:
Non-exhaustive patterns in function…

user32132321
- 135
- 1
- 1
- 9
4
votes
2 answers
Does Using Async Await Avoid Thread Exhaustion?
We are troubleshooting the following performance issues on a .NET Core API endpoint:
The endpoint consistently returns in less than 500MS under minor load.
When we hit the endpoint from 3 browsers, with one request a second, it gets progressively…

VSO
- 11,546
- 25
- 99
- 187
4
votes
1 answer
What does "(_:_:_)" mean in Haskell (non-exhaustive pattern matching error from GHCI)?
I'm getting the following error from GHCI when I run my Haskell program:
"Pattern match(es) are non-exhaustive
In an equation for `recaList': Patterns not matched: (_:_:_)"
I've been searching the web/SO, but can't seem to find an explanation for…

Caroline
- 85
- 7
3
votes
4 answers
Non-exhaustive patterns error when writing recursive list function
In Haskell I want to write a recursive function that for a given list of numbers changes the sign of each element in the sublists:
list = [[1, 3, 6.7, 7.0], [], [1, 8.22, 9, 0]]
multiply (x:xs) = [n * (-1) | n <- x] : multiply xs
but I get an…

bami77
- 51
- 2
3
votes
2 answers
Avoid inappropriate non-exhaustive pattern match warning in GHCI
Before you dismiss this as a duplicate
I see that at least as of September 2018, GHCI does not allow you to disable a warning locally (although you can in a whole file).
But maybe there's some other way to let GHCI know that every case is in fact…

Jeffrey Benjamin Brown
- 3,427
- 2
- 28
- 40