I\'ve been getting into the nitty gritty of the haskell typesystem and trying to get at the fine points of type classes. I\'ve learned a heap, but I\'m hitting a wall on the fol
This is where a fun toy comes into play. Consider the standard Prelude function asTypeOf
.
asTypeOf :: a -> a -> a
asTypeOf = const
It just returns its first argument, no matter what the second argument is. But the type signature on it places the additional constraint that both of its arguments must be the same type.
g :: C a => a -> Int -> String
g x y = show (f y `asTypeOf` x)
Now, GHC knows what the type of f y
is. It's the same as the type of the first argument to g
. Without that information, well, you get the error message you saw. There just wasn't sufficient information to determine the type of f y
. And because the type is important (it's used to determine which instance to use for show
), GHC needs to know what type you mean to generate code.