While trying to compile the following code, which is enhanced version of read build on readMay from Safe package.
readI :: (Typeable a, Read a) => String -> a
readI str = case readMay str of
Just x -> x
Nothing -> error ("Prelude.read failed, expected type: " ++
(show (typeOf > (undefined :: a))) ++
"String was: " ++ str)
I get an error from GHC:
WavefrontSimple.hs:54:81:
Ambiguous type variable `a’ in the constraint:
`Typeable a’
arising from a use of `typeOf’ at src/WavefrontSimple.hs:54:81-103
Probable fix: add a type signature that fixes these type variable(s)`
I don’t understand why. What should be fixed to get what I meant?
EDIT: Ok, so the solution to use ScopedTypeVariables and forall a in type signature works. But why the following produces very similar error to the one above? The compiler should infer the right type since there is asTypeOf :: a -> a -> a used.
readI :: (Typeable a, Read a) => String -> a
readI str = let xx = undefined in
case readMay str of
Just x -> x `asTypeOf` xx
Nothing -> error ("Prelude.read failed, expected type: "
++ (show (typeOf xx)) ++
"String was: " ++ str)
The latter doesn’t work because the type of
xxis the same as the type ofundefined— i.e., “forall a. a.” The fact that you force xx to be used with one concrete type with the asTypeOf operator doesn’t mean that it is less polymorphic everywhere else.