[Haskell-cafe] Redundant/overlapping patterns?

Viktor Dukhovni ietf-dane at dukhovni.org
Thu Jul 20 04:53:03 UTC 2023


On Wed, Jul 19, 2023 at 09:24:20PM -0700, Todd Wilson wrote:

> Can someone please explain this:
> 
> ghci> case 1 of 2 -> 3
> 
> <interactive>:1:11: warning: [-Woverlapping-patterns]
>     Pattern match is redundant
>     In a case alternative: 2 -> ...
> *** Exception: <interactive>:1:1-16: Non-exhaustive patterns in case
> 
> The non-exhaustive patterns part is obvious, but what is redundant about
> this match? And how can there be overlapping patterns when there's only one?

The error message changed betweek 8.8 and 8.10:

    GHCi, version 8.8.4: https://www.haskell.org/ghc/  :? for help
    λ> data X = A | B
    λ> case A of B -> 42

    *** Exception: <interactive>:2:1-18: Non-exhaustive patterns in case

  vs.

    GHCi, version 8.10.5: https://www.haskell.org/ghc/  :? for help
    λ> data X = A | B
    λ> case A of B -> 42

    <interactive>:2:11: warning: [-Woverlapping-patterns]
        Pattern match is redundant
        In a case alternative: B -> ...
    *** Exception: <interactive>:2:1-17: Non-exhaustive patterns in case

The exception part is the same, and I hope non-controversial, the
pattern match is indeed non-exhaustive.  What's new is the compile-time
warning.  It is documented at, e.g.:

    https://ghc.gitlab.haskell.org/ghc/doc/users_guide/using-warnings.html#ghc-flag--Woverlapping-patterns

where it is explained that a pattern is considered "overlapping" when it
is unreachable, which is true in this case, though for reasons other
than redundancy wrt. a prior pattern.  And indeed we see the true
issue/criterion is "reachability":

   λ> case A of B | GHC.Exts.considerAccessible -> 42

    *** Exception: <interactive>:3:1-47: Non-exhaustive patterns in case

The same is of course seen if the warning is disabled:

    λ> :set -Wno-overlapping-patterns
    λ> case A of B -> 42

    *** Exception: <interactive>:6:1-17: Non-exhaustive patterns in case

The pattern match is redundant given the specific scrutinee.  Perhaps
non-reachability in this case could be reported via a different warning,
but we have what we have.

-- 
    Viktor.


More information about the Haskell-Cafe mailing list