Consider the following two functions in Haskell (a minimal example of my real code):
printSequence :: (Show a, Show b) => a -> b -> IO ()
printSequence x y = (putStr . show) x >> (putStr . show) y
printSequence' :: (Show a, Show b) => a -> b -> IO ()
printSequence' x y = print' x >> print' y
where print' = putStr . show
The first compiles fine, but the second produces the error:
Could not deduce (a ~ b)
from the context (Show a, Show b)
bound by the type signature for
printSequence' :: (Show a, Show b) => a -> b -> IO ()
at test.hs:8:19-53
`a' is a rigid type variable bound by
the type signature for
printSequence' :: (Show a, Show b) => a -> b -> IO ()
at test.hs:8:19
`b' is a rigid type variable bound by
the type signature for
printSequence' :: (Show a, Show b) => a -> b -> IO ()
at test.hs:8:19
In the first argument of print', namely `y'
In the second argument of `(>>)', namely `(print' y)'
In the expression: (print' x) >> (print' y)
I understand this error to mean that GHC is requiring x
and y
to be of equivalent type. What I do not understand is WHY. Statements like print "fish" >> print 3.14
work perfectly fine in the interpreter, so why does GHC complain about x
and y
being different types when I call my print'
function two separate times?