Non-Exhaustive Pattern Matching #2426
Replies: 3 comments 7 replies
This comment was marked as off-topic.
This comment was marked as off-topic.
-
Let's prepend this reply by saying: no, it's currently not possible to inform GHC that your program covers all bases. What follows is an explanation on why this is the way it is, and what we could do in the mid-term to improve this. Let's first have a look at a (seemingly equivalent) program that doesn't generate these warnings: module D2426A where
import Clash.Prelude
data AB = A | B
isA :: AB -> Bool
isA ab =
case ab of
A -> True
B -> False By passing isA :: AB -> Bool
[GblId, Arity=1, Unf=OtherCon []]
isA
= \ (ab_a3yx :: AB) ->
case ab_a3yx of {
A -> GHC.Types.True;
B -> GHC.Types.False
} So, plain and simple! GHC can do exhaustiveness checking on this as it can checker whether the constructors module D2426 where
import Clash.Prelude
top :: BitVector 1 -> BitVector 1
top bv =
case bv of
0 -> 1
1 -> 0 This generates: top :: BitVector 1 -> BitVector 1
[GblId, Arity=1, Unf=OtherCon []]
top
= \ (bv_a3JG :: BitVector 1) ->
case ==
@(BitVector 1)
$dEq_r4A2
bv_a3JG
(fromInteger @(BitVector 1) $dNum_r4A1 0)
of {
False ->
case ==
@(BitVector 1)
$dEq_r4A2
bv_a3JG
(fromInteger @(BitVector 1) $dNum_r4A1 1)
of {
False ->
Control.Exception.Base.patError
@'GHC.Types.LiftedRep
@(BitVector 1)
"D2426.hs:(9,3)-(11,10)|case"#;
True -> fromInteger @(BitVector 1) $dNum_r4A1 0
};
True -> fromInteger @(BitVector 1) $dNum_r4A1 1
} If we squint our eyes a bit we can see that it generates: top bv =
case bv == 0 of
False ->
case bv == 1 of
False -> error "hey something is wrong"
True -> 0
True -> 1 In the end, this is because In theory we should be able to come up with a GHC plugin to rewrite (exhaustive) patterns in such a way that GHC ignores it. E.g., we could inject our own Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
I'm restating one thing from my own reply, and then I will hide that original thread because it accidentally derailed the discussion.
The HDL's variant of unkown/X/undefined/.... So |
Beta Was this translation helpful? Give feedback.
-
The haskell language server warns about the following pattern - that it is non-exhaustive, however, we know that it is:
I'm not familiar with the extent of dependent typing in Clash, but shouldn't it be possible to somehow inform the compiler about the possible domain of (BitVector 1)?
I genuinely appreciate when the compiler warns me about non-exhaustive cases, but false positive can decrease signal-to-noise ratio. What HDL would the compiler emit in a non-exhaustive case statement? For example, what would the compiler emit if we only had the
0 -> 1
case arm?Yehowshua
Beta Was this translation helpful? Give feedback.
All reactions