[Haskell-cafe] Redundant/overlapping patterns?

Jeff Clites jclites at mac.com
Fri Jul 21 22:20:03 UTC 2023


Well, I think you don't have referential transparency of warnings. My interpretation is that if you write "case A of ..." (where A is a constructor), then it's probably not the code you meant to write, so the warning is there to let you know you might be making a mistake (even though it's a mistake that would compile).

I could image a mistake like this:

   let a = f x in case A of ...

where you meant to write:

   let a = f x in case a of ...

Possibly the thinking it that having a literal constructor as a scrutinee is weird enough that it's more likely to be a mistake than intentional. But once the expression is any more complicated than that it's less obviously a mistake.


Also, one other comment, about Ben Franksen's message:

>> So the error message should talk about "unreachable pattern" instead of "redundant pattern".

Ah but the message is actually:

>     Pattern match is redundant

So I think it's trying to say not that the one pattern is redundant, but the whole case expression is redundant. Perhaps it should say something like "unnecessary case expression" instead.

Jeff

> On Jul 21, 2023, at 12:21 PM, Todd Wilson <twilson at csufresno.edu> wrote:
> 
> λ: data X = A|B
> λ: let x = A in case x of A -> True; B -> False
> True
> λ: case A of A -> True ; B -> False
> 
> <interactive>:3:23: warning: [-Woverlapping-patterns]
>     Pattern match is redundant
>     In a case alternative: B -> ...
> True
> 
> Isn't this violating referential transparency (in a way)?
> 
> --Todd
> 
> On Fri, Jul 21, 2023 at 3:15 AM Jeff Clites via Haskell-Cafe <haskell-cafe at haskell.org> wrote:
> I think it’s saying it’s redundant because you already known what constructor it is; I’m guessing you’d get the same error from “case A of A -> …”.
> 
> Jeff
> 
> > On Jul 21, 2023, at 2:57 AM, Ben Franksen <ben.franksen at online.de> wrote:
> > 
> > So the error message should talk about "unreachable pattern" instead of "redundant pattern". That would cover all situations and would be less confusing in this special one.
> > 
> > Cheers
> > Ben
> > 
> >> Am 20.07.23 um 06:53 schrieb Viktor Dukhovni:
> >>> 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.
> > 
> > -- 
> > I would rather have questions that cannot be answered, than answers that
> > cannot be questioned.  -- Richard Feynman
> > 
> > 
> > _______________________________________________
> > Haskell-Cafe mailing list
> > To (un)subscribe, modify options or view archives go to:
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> > Only members subscribed via the mailman list are allowed to post.
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.



More information about the Haskell-Cafe mailing list