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
接受某些论点而无法接受其他论点,甚至非常相似?
最佳答案
原因与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
也就是说,ghci 的扩展默认规则允许它解决
Show
的歧义。约束,但不适用于 Enum
约束。如果您尝试使用 foo = polyTypeOf show
编译源文件,你也会得到一个模棱两可的类型变量错误(除非你使用 {-# LANGUAGE ExtendedDefaultRules #-}
)。
关于haskell - polyTypeOf 很神秘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7947987/