Opened 5 years ago

Closed 4 years ago

Last modified 17 months ago

#9951 closed bug (duplicate)

OverloadedLists breaks exhaustiveness check

Reported by: Feuerbach Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.8.4
Keywords: PatternMatchWarnings Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:

Description

{-# LANGUAGE OverloadedLists #-}

module Bug where

f :: [a] -> ()
f x = case x of
  [] -> ()
  (_:_) -> ()

GHC reports:

bug.hs:6:7: Warning:
    Pattern match(es) are non-exhaustive
    In a case alternative: Patterns not matched: []

This is reproducible with 7.8 and 7.10rc1.

Change History (6)

comment:1 Changed 5 years ago by MartinF

I note patterns using OverloadedLists desugar to view patterns - from that wiki page:

f [] = ...
g [x,y,z] = ...

will be treated as

f (toList -> []) = ...
g (toList -> [x,y,z]) = ...

So I'd guess this has the same underlying cause as the known problem with exhaustiveness checking of view patterns - see #5762.

For instance, this:

$ ghci -Wall -XOverloadedLists
GHCi, ...
Prelude> let f [] = (); f (_:_) = ()

<interactive>:2:5: Warning:
    Pattern match(es) are non-exhaustive
    In an equation for `f': Patterns not matched: []

and its one-step-desugared equivalent:

$ ghci -Wall -XViewPatterns
GHCi, ...
Prelude> :module +GHC.Exts
Prelude GHC.Exts> let f (fromList -> []) = (); f (fromList -> _:_) = ()

<interactive>:3:5: Warning:
    Pattern match(es) are non-exhaustive
    In an equation for `f': Patterns not matched: _

... both produce spurious exhaustiveness warnings.

Slightly different ones, though. How curious.

comment:2 Changed 5 years ago by Feuerbach

Thanks Martin, that desugaring explains everything. I didn't know it was so simple.

The cause of the difference you point out is that the cons pattern, _:_, is not overloaded.

The question then becomes, should there be some way to write the non-overloaded empty list constructor even when OverloadedLists is on?

Last edited 5 years ago by Feuerbach (previous) (diff)

comment:3 Changed 4 years ago by thomie

Also reported as #10393.

comment:4 Changed 4 years ago by thomie

Resolution: duplicate
Status: newclosed

Because the existence of duplicate tickets makes doing a BugSweep of the bug tracker more cumbersome, I'm closing these tickets as duplicate. Don't worry, they're still listed on PatternMatchCheck, and will hopefully all be addressed by the work on #595 ("Overhaul GHC's overlapping/non-exhaustive pattern checking").

comment:5 Changed 4 years ago by George Karachalias <george.karachalias@…>

In 8a506104/ghc:

Major Overhaul of Pattern Match Checking (Fixes #595)

This patch adresses several problems concerned with exhaustiveness and
redundancy checking of pattern matching. The list of improvements includes:

* Making the check type-aware (handles GADTs, Type Families, DataKinds, etc.).
  This fixes #4139, #3927, #8970 and other related tickets.

* Making the check laziness-aware. Cases that are overlapped but affect
  evaluation are issued now with "Patterns have inaccessible right hand side".
  Additionally, "Patterns are overlapped" is now replaced by "Patterns are
  redundant".

* Improved messages for literals. This addresses tickets #5724, #2204, etc.

* Improved reasoning concerning cases where simple and overloaded
  patterns are matched (See #322).

* Substantially improved reasoning for pattern guards. Addresses #3078.

* OverloadedLists extension does not break exhaustiveness checking anymore
  (addresses #9951). Note that in general this cannot be handled but if we know
  that an argument has type '[a]', we treat it as a list since, the instance of
  'IsList' gives the identity for both 'fromList' and 'toList'. If the type is
  not clear or is not the list type, then the check cannot do much still. I am
  a bit concerned about OverlappingInstances though, since one may override the
  '[a]' instance with e.g. an '[Int]' instance that is not the identity.

* Improved reasoning for nested pattern matching (partial solution). Now we
  propagate type and (some) term constraints deeper when checking, so we can
  detect more inconsistencies. For example, this is needed for #4139.

I am still not satisfied with several things but I would like to address at
least the following before the next release:
    Term constraints are too many and not printed for non-exhaustive matches
(with the exception of literals). This sometimes results in two identical (in
appearance) uncovered warnings. Unless we actually show their difference, I
would like to have a single warning.

comment:6 Changed 17 months ago by simonpj

Keywords: PatternMatchWarnings added
Note: See TracTickets for help on using tickets.