PolyTypeable is an analog of Typeable for polymorphic types. But it works rather unpredictably:
ghci> :t show
show :: Show a => a -> String
ghci> polyTypeOf show
a1 -> [Char]
ghci> :t fromEnum
fromEnum :: Enum a => a -> Int
ghci> polyTypeOf fromEnum
<interactive>:1:12:
Ambiguous type variable `a0' in the constraint:
(Enum a0) arising from a use of `fromEnum'
Probable fix: add a type signature that fixes these type variable(s)
In the first argument of `polyTypeOf', namely `fromEnum'
In the expression: polyTypeOf fromEnum
In an equation for `it': it = polyTypeOf fromEnum
The library source code is quite hard to understand, could you explain why does polyTypeOf
accept certain set of arguments and fails to accept other, even very similar?
The reason is the same as for
Prelude> show undefined
"*** Exception: Prelude.undefined
Prelude> fromEnum undefined
<interactive>:0:1:
Ambiguous type variable `a0' in the constraint:
(Enum a0) arising from a use of `fromEnum'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: fromEnum undefined
In an equation for `it': it = fromEnum undefined
namely, ghci's extended defaulting rules allow it to resolve the ambiguity for a Show
constraint, but not for an Enum
constraint. If you try to compile a source file with foo = polyTypeOf show
, you also get an ambiguous type variable error (unless you use {-# LANGUAGE ExtendedDefaultRules #-}
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With