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 being handled?
The Question
An idiom I use sometimes is to write a function where the first definition(s) tests for some predicate(s) and returns Left, and the other definitions consider arguments where the operation actually makes sense. Whenever I do that, I get a "Pattern match(es) are non-exhaustive" error, but I really am checking for every condition.
Example
(For the real-world code motivating this toy example, see e.g. the definition of pExprToHExpr
here.)
This code:
{-# LANGUAGE ViewPatterns #-}
data Cowbell = Cowbell
deriving Show
data Instrument = Rattle
| Drums (Maybe Cowbell)
| Violin
| Oboe
deriving Show
pitched :: Instrument -> Bool
pitched Rattle = False
pitched (Drums Nothing) = False
pitched (Drums (Just Cowbell)) = True
pitched Violin = True
pitched Oboe = True
highestPitch :: Instrument -> Either String Float
highestPitch i@(pitched -> False) =
Left $ "Instrument " ++ show i ++ " has no pitch."
highestPitch (Drums (Just Cowbell)) = Right 800
highestPitch Violin = Right 5000
highestPitch Oboe = Right 2000
generates this error:
example.hs:19:1: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for ‘highestPitch’:
Patterns not matched:
Rattle
(Drums Nothing)
Under other conditions, I would just subdivide the Instrument
type:
data Percussive = Rattle | Drums
data Pitched = Violin | Oboe
data Instrument = Percussive Percussive
| Pitched Pitched
But (in this imaginary physics) a set of Drums
might have a highest pitch, if it includes a Cowbell
, so it doesn't fit into either the Percussive
or the Pitched
type.