Consider the following:
do putStr ""
return $ map read ["2"]
Run in GHCI, this gives
[*** Exception: Prelude.read: no parse
I expected an “ambigious type variable” compilation error, but instead this comes up at runtime. Why?
For comparison, the following three examples do give “ambiguous type variable” errors:
map read ["2"]
do putStr ""
return $ read "2"
do [1]
return $ map read ["2"]
By the way, here’s the original code where I encountered this.
readInts :: String -> IO [Integer]
readInts f = do s <- readFile f
return $ map read $ splitOneOf " \n" s
This has been resolved. My original code attempted to read from “” at the end of the list. I received the same exception for a different reason when I investigated in GHCI.
This happens because of GHCi’s extended default rules. If you put the same code in a file without a signature, you should see the error you expected.
(In this case it’s probably picking
()as the type, which is why you get a parse error.)