Bad monomorphism-restriction-related type error message
Consider the following program:
module Small2 where
printFormatted :: IO ()
printFormatted =
let myPrint :: (a -> String) -> a -> IO ()
myPrint f s = putStr (f s)
-- myShow :: Show a => a -> IO ()
myShow = myPrint show in
myShow 1 >> myShow "foo"
If I compile this I get:
> ghc --make -no-recomp -c small2.hs
[1 of 1] Compiling Small2 ( small2.hs, small2.o )
small2.hs:9:19:
No instance for (Num [Char])
arising from the literal `1' at small2.hs:9:19
Possible fix: add an instance declaration for (Num [Char])
In the first argument of `myShow', namely `1'
In the first argument of `(>>)', namely `myShow 1'
In the expression:
let
myPrint :: (a -> String) -> a -> IO ()
myPrint f s = putStr (f s)
myShow = myPrint show
in (myShow 1) >> (myShow "foo")
The fix, of course, is to either add -fno-monomorphism-restriction or to uncomment the type signature for myShow
, but it took me a while to figure that out. It would be much better if the error message would suggest adding a type signature for myShow
. As it is, the suggestion to add an instance for Num [Char]
is none too helpful.
Trac metadata
Trac field | Value |
---|---|
Version | 6.6 |
Type | Bug |
TypeOfFailure | OtherFailure |
Priority | lowest |
Resolution | Unresolved |
Component | Compiler (Type checker) |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | Multiple |
Architecture | Multiple |