gpt4 book ai didi

haskell - polyTypeOf 很神秘

转载 作者:行者123 更新时间:2023-12-04 14:49:57 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com