[GHC] #12106: Wrong type inferenced for locally defined function

#12106: Wrong type inferenced for locally defined function -------------------------------------+------------------------------------- Reporter: syd | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- Consider the following example. {{{#!hs 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'. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12106 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12106: Wrong type inferenced for locally defined function -------------------------------------+------------------------------------- Reporter: syd | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by rwbarton): * status: new => closed * resolution: => invalid Comment: The [http://stackoverflow.com/questions/32496864/what-is-the-monomorphism- restriction monomorphism restriction] applies to `myPrint`, so its inferred type cannot be `Show a => a -> IO ()`. It has nothing to do with the absense of a `Show` instance for `B` (as you can check by adding that instance). In GHCi the monomorphism restriction is disabled by default (as of a recent version of GHC). You can also turn it off for a module with `{-# LANGUAGE NoMonomorphismRestriction #-}`. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12106#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#12106: Wrong type inferenced for locally defined function -------------------------------------+------------------------------------- Reporter: syd | Owner: Type: bug | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 8.0.1 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by syd): Replying to [comment:1 rwbarton]:
The [http://stackoverflow.com/questions/32496864/what-is-the- monomorphism-restriction monomorphism restriction] applies to `myPrint`, so its inferred type cannot be `Show a => a -> IO ()`. It has nothing to do with the absense of a `Show` instance for `B` (as you can check by adding that instance).
In GHCi the monomorphism restriction is disabled by default (as of a recent version of GHC). You can also turn it off for a module with `{-# LANGUAGE NoMonomorphismRestriction #-}`.
Woops, did not know about this. Sorry to bother you. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/12106#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC