Skip to content

Wrong type inferenced for locally defined function

Consider the following example.

data A = A
    deriving Show
data B = B
    -- deriving Show is missing here.

main :: IO ()
main = do
    let myPrint = putStrLn . show
    myPrint A
    myPrint B

The Show instance for B is missing and the most general type for myPrint is Show a => a -> IO ().

Ghc, however, reports the following error:

file.hs:10:13: error:
    • Couldn't match expected type ‘A’ with actual type ‘B’
    • In the first argument of ‘myPrint’, namely ‘B’
      In a stmt of a 'do' block: myPrint B
      In the expression:
        do { let myPrint = putStrLn . show;
             myPrint A;
             myPrint B }

This error won't help the programmer find the missing deriving Show declaration. Instead, the error should read something like No instance for (Show B) arising from a use of ‘myPrint’.

Weirdly, when I type this main function into ghci, the error is 'correct'.

Trac metadata
Trac field Value
Version 8.0.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information