Opened 9 years ago

Closed 8 years ago

Last modified 6 years ago

#281 closed bug (fixed)

Wrong overlapped/missing pattern warnings

Reported by: nobody Owned by: nobody
Priority: lowest Milestone:
Component: Compiler Version: 6.2.2
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description (last modified by simonmar)

compiling:

  module Overlap where

  f (n+1) = 2
  f 0     = 1

emits wrongly:

    Warning: Pattern match(es) are overlapped
             In the definition of `f': f 0 = ...


The Patterns are disjoint, aren't they? At least  "f 0"
yields "1" when evaluated and negative inputs for f are
rejected. However the warning is not emitted if the two
equations are given in reversed order.

Christian (maeder@tzi.de)

Change History (8)

comment:1 Changed 9 years ago by simonpj

  • Summary changed from Wrong overlapped pattern warning for n+k Pattern to Wrong overlapped/missing pattern warnings
Logged In: YES 
user_id=50165

Here's another example, from Peter White

When I compile the following module with the -Wall option on 
ghc v6.2.1  
I get warnings:
Warning: Pattern match(es) are non-exhaustive
     In a record-update construct: Patterns not matched D2.
The warnings occur at both of the indicated places in the 
module.
Since the functions both handle all the cases of the data 
type D, it  
seems the warning should not be given.


data D = D1 { f1 :: Int } | D2

-- Use pattern matching in the argument
f :: D -> D
f  d1@(D1 {f1 = n}) = d1 { f1 = f1 d1 + 1 } -- Warning here
f  d = d

-- Use case pattern matching
g :: D -> D
g  d1 = case d1 of
           D1 { f1 = n } -> d1 { f1 = n + 1 } -- Warning here 
also
           D2            -> d1


comment:2 Changed 8 years ago by simonpj

Logged In: YES 
user_id=50165

Another example from Neil Mitchell

I have been playing around with -fwarn-incomplete-patterns 
under GHC
6.4.1 on Windows.

-- no warning
ex1 x = ss
    where (s:ss) = x

-- no warning
ex2 x = let (s:ss) = x in ss

--    Warning: Pattern match(es) are non-exhaustive
--             In a case alternative: Patterns not matched: 
[]
ex3 x = case x of ~(s:ss) -> ss

I have translated all 3 functions using the rules supplied 
in the Haskell 98 report, so they all have the same 
meaning, but only one gives an error. Is it intentional to 
ignore where/let pattern matches?

comment:3 Changed 8 years ago by ginge

Logged In: YES 
user_id=618575

When adding -fwarn-simple-patterns to the command line, all
3 of the previous examples give an additional 2 warning's,
i.e. ex1 and ex2 give two identical warnings, and ex3 gives
3 identical warnings.

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

comment:4 Changed 8 years ago by simonpj

Logged In: YES 
user_id=50165

Another example, this time from John Meacham

the following gives a 'non-exhaustive pattern matching' 
error when it shouldn't. the ~-pattern always matches. It 
is not the function that is non-exhausitive, but the 
irrefutable binding, which are necessarily always non-
exhausive so warning about it is the wrong thing to do.

f :: [a] -> a
f [x] = x
f ~(_:xs) = f xs   

comment:5 Changed 8 years ago by simonmar

  • Architecture set to Unknown
  • Component changed from Compiler (Parser) to Compiler
  • Description modified (diff)
  • Difficulty set to Unknown
  • Operating System set to Unknown

The problems with irrefutable patterns are now fixed (see also #827).

comment:6 Changed 8 years ago by simonpj

  • Resolution changed from None to fixed
  • Status changed from assigned to closed

All of these are fixed, except the first. I'm going to open a new bug report for that, since fixing n+k patterns isn't going to happen soon. And I'll close this.

ds059 checks for the cases that now work ok.

Simon

comment:7 Changed 6 years ago by simonmar

  • Architecture changed from Unknown to Unknown/Multiple

comment:8 Changed 6 years ago by simonmar

  • Operating System changed from Unknown to Unknown/Multiple
Note: See TracTickets for help on using tickets.