[Haskell-cafe] Annoying problem when pattern matching negative integer literals and wildcards

Vanessa McHale vanessa.mchale at iohk.io
Fri Mar 15 03:54:58 UTC 2019


I have the following program:

module Bug ( encryptionResult ) where

data EncryptionResult = HasEncryption
                      | EncryptionUnknown

encryptionResult :: Int -> EncryptionResult
encryptionResult 1 = HasEncryption
encryptionResult -1 = EncryptionUnknown
encryptionResult _ = error "Internal error."

When I try to compile it with GHC I get

[1 of 1] Compiling Bug              ( Bug.hs, Bug.o )

Bug.hs:9:1: error:
    Multiple declarations of ‘encryptionResult’
    Declared at: Bug.hs:7:1
                 Bug.hs:9:1
  |
9 | encryptionResult _ = error "Internal error."
  | ^^^^^^^^^^^^^^^^

I can replicate this in Hugs, viz.

ERROR "Bug.hs":7 - "encryptionResult" multiply defined

However, everything compiles fine when I write

module Bug ( encryptionResult ) where

data EncryptionResult = HasEncryption
                      | EncryptionUnknown

encryptionResult :: Int -> EncryptionResult
encryptionResult 1 = HasEncryption
encryptionResult -1 = EncryptionUnknown

or

module Bug ( encryptionResult ) where

data EncryptionResult = HasEncryption
                      | EncryptionUnknown

encryptionResult :: Int -> EncryptionResult
encryptionResult 1 = HasEncryption
encryptionResult 0 = EncryptionUnknown
encryptionResult _ = error "Internal error."

Am I doing something obviously screwy? This seems like a pretty annoying
feature on the language (to the point where I assumed it was a GHC bug
until I got the same behavior with Hugs) and I can't figure out why it
exists.

Cheers,
Vanessa McHale
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190314/4a5ed053/attachment.sig>


More information about the Haskell-Cafe mailing list