Ticket #5560 (closed bug: invalid)

Opened 19 months ago

Last modified 19 months ago

case and type synonym

Reported by: sleepyMonad Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.0.2
Keywords: Cc:
Operating System: Linux Architecture: x86
Type of failure: Incorrect result at runtime Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description (last modified by igloo) (diff)

Observed on arch linux, 7.0.3 ghc. (package version 7.0.3-2) The warning is suspicious; the result for switchOnEvent2 bp is wrong. It works however if no type synonym is used (as illustrated).

Thanks

-- patrick

[patrick@eee2 launcher]$ ghci
GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> :l testme
[1 of 1] Compiling Main             ( testme.hs, interpreted )

testme.hs:10:1:
    Warning: Pattern match(es) are overlapped
             In an equation for `switchOnEvent2':
                 switchOnEvent2 bp = ...
                 switchOnEvent2 _ = ...
Ok, modules loaded: Main.
*Main> switchOnEvent2 kp
KP
*Main> switchOnEvent2 bp
KP
*Main> switchOnEvent3 202
KP
*Main> switchOnEvent3 204
BP
*Main> switchOnEvent3 203
??
*Main> >
-- testme.hs:

import Data.Word

type MyEventType = Word32
kp :: MyEventType
kp = 102
bp :: MyEventType
bp = 104

switchOnEvent2 :: MyEventType -> IO ()
switchOnEvent2 kp = do putStrLn "KP"
switchOnEvent2 bp = do putStrLn "BP"
switchOnEvent2 _ = do putStrLn "??"

switchOnEvent3 :: Word32 -> IO ()
switchOnEvent3 202 = do putStrLn "KP"
switchOnEvent3 204 = do putStrLn "BP"
switchOnEvent3 _ = do putStrLn "??"

Change History

Changed 19 months ago by igloo

  • description modified (diff)

Changed 19 months ago by igloo

  • status changed from new to closed
  • resolution set to invalid

Pattern matching doesn't work like that I'm afraid. If you use the -Wall flag then you will get some extra warnings like

q.hs:12:16:
    Warning: This binding for `kp' shadows the existing binding
               defined at q.hs:7:1

q.hs:12:16: Warning: Defined but not used: `kp'

Your definition of switchOnEvent2 is equivalent to

switchOnEvent2 :: MyEventType -> IO ()
switchOnEvent2 x = do putStrLn "KP"
switchOnEvent2 y = do putStrLn "BP"
switchOnEvent2 _ = do putStrLn "??"

You may want something like

switchOnEvent2 :: MyEventType -> IO ()
switchOnEvent2 e
 | e == kp   = do putStrLn "KP"
 | e == bp   = do putStrLn "BP"
 | otherwise = do putStrLn "??"

Changed 19 months ago by sleepyMonad

The Haskell community is known as very open and friendly. You answered my "bug report" on a Sunday, within less than 2 hours, and took the effort to explain why it wasn't a bug at all. QED Thank you.

Note: See TracTickets for help on using tickets.