I have a case
expression with a relatively large number of patterns:
case x of
... -> ...
... -> ...
... -> ...
... -> ...
...
_ -> ...
One of these cases has a guard:
case x of
... -> ...
... -> ...
... | condition -> ...
-- If condition is false, fall through to “Rest”.
-- Rest:
... -> ...
... -> ...
...
_ -> ...
If the guard doesn’t match, we just fall through to the remaining cases, no problem. But now I need to test the condition monadically, so I do:
case x of
... -> ...
... -> ...
... -> do
condition <- action
if condition
then ...
else ... -- How to fall through?
-- Rest:
... -> ...
... -> ...
...
_ -> ...
However, I think I’ve made a misstep. There doesn’t seem to be a way of having the else
branch proceed to the remaining cases without duplicating those branches or factoring them into a function. And either way messes with exhaustivity checking: if I want to add a case after the guard, the compiler doesn’t know whether the matches are exhaustive.
How can I change this function, or parameterise/wrap the datatype, to get exhaustiveness checking using a monadic guard?