PolyTypeable类似于Typeable。但它的作用相当不可预测:
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
库源代码很难理解,你能解释为什么polyTypeOf
接受某些参数并且不接受其他参数,甚至非常相似吗?
答案 0 :(得分:7)
原因与
相同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
即,ghci的扩展默认规则允许它解决Show
约束的歧义,但不能解决Enum
约束的歧义。如果您尝试使用foo = polyTypeOf show
编译源文件,则还会出现模糊的类型变量错误(除非您使用{-# LANGUAGE ExtendedDefaultRules #-}
)。