haskell - 有没有办法让 GHC 提供类型孔的类型类约束?

标签 haskell types ghc

当前行为

Prelude> show _

<interactive>:7:6:
    Found hole ‘_’ with type: a0
    Where: ‘a0’ is an ambiguous type variable
    Relevant bindings include it :: String (bound at <interactive>:7:1)
    In the first argument of ‘show’, namely ‘_’
    In the expression: show _
    In an equation for ‘it’: it = show _

期望的行为

如果 GHC 也能告诉我类型化的洞具有 Show 类型类约束,那就太好了。

其他

GHC 版本 7.8.1

最佳答案

感谢 @DominiqueDevriese 的 GHC ticket,该问题现已在 GHC 8.0 中修复。 .

由于 extended type defaulting ,这在 GHCi 中并不是立即显而易见的。以你的例子来说,

> show _

  <interactive>:7:6: error:
    • Found hole: _h :: ()
      Or perhaps ‘_h’ is mis-spelled, or not in scope
    • In the first argument of ‘show’, namely ‘_h’
      In the expression: show _h
      In an equation for ‘it’: it = show _h
    • Relevant bindings include
        it :: String (bound at <interactive>:7:1)

孔的类型默认为()。这显然是desired behavior ,尽管有一个论点认为扩展默认值不应应用于漏洞(因为它们的常见用途是让编译器告诉您推断的类型)。

尽管如此,如果您使用 GHC 进行编译或在 GHCi 中禁用扩展默认规则(通过 :set -XNoExtendedDefaultRules),我们会看到改进的结果:

<interactive>:3:1: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘show’
      prevents the constraint ‘(Show a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Show Ordering -- Defined in ‘GHC.Show’
        instance Show Integer -- Defined in ‘GHC.Show’
        instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
        ...plus 22 others
        ...plus 11 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the expression: show _
      In an equation for ‘it’: it = show _

<interactive>:3:6: error:
    • Found hole: _ :: a0
      Where: ‘a0’ is an ambiguous type variable
    • In the first argument of ‘show’, namely ‘_’
      In the expression: show _
      In an equation for ‘it’: it = show _
    • Relevant bindings include
        it :: String (bound at <interactive>:3:1)

关于haskell - 有没有办法让 GHC 提供类型孔的类型类约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23028124/

相关文章:

haskell - 如何使用 Cabal 将 Haskell 依赖项的版本固定到底层 native 依赖项的版本?

security - 如何安全地执行来自不受信任的第三方的确定性代码?

c++ - 警告 C4244 : 'argument' : conversion from 'SIZE_T' to 'DWORD' , 可能丢失数据

haskell - 查找函数的 GHC 程序集

haskell - 在 Haskell 中运行和编译 'Hello, World!'

haskell - 我可以将类型类字典显式传递给函数吗?

haskell - 使 ReadArgs 1.0 使用单个参数

haskell - 将带有break-s/continue-s的命令式控制流转换为haskell

理解期间列表中的python类型

c++ - 如何解决 MSVC 2012 中的 `using K = ...`?