I wrote the following code.
sqrSum x = sum . map (^2) $ [1..x]
mbFact x
| x < 0 = Nothing
| otherwise = Just . product $ [1..x]
main =
let --print' :: (Show a) => a -> IO () -- why need this?
print' =
putStrLn . show
in do
print' $ mbFact (-11)
print' $ mbFact 0
print' $ sqrSum 3
If I uncomment the "why need this?" line the code compiles and works.
After commenting that line, I expected the Haskell compiler to infer the type of print'. But it didn't. Why?
error:
* Ambiguous type variable `a0' arising from a use of `show'
prevents the constraint `(Show a0)' from being solved.
Relevant bindings include
print' :: Maybe a0 -> IO () (bound at b.hs:9:9)
Probable fix: use a type annotation to specify what `a0' should be.
These potential instances exist:
instance Show Ordering -- Defined in `GHC.Show'
instance Show Integer -- Defined in `GHC.Show'
instance Show a => Show (Maybe a) -- Defined in `GHC.Show'
...plus 22 others
...plus 13 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In the second argument of `(.)', namely `show'
In the expression: putStrLn . show
In an equation for print': print' = putStrLn . show
|
10 | putStrLn . show
| ^^^^
Why can't GHC infer the type of print'?