有没有办法让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票证,现在已经在GHC 8.0中修复了。

由于扩展类型违约,这在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)

孔的类型默认为() 。 这显然是期望的行为,尽管有一个论点需要做出延期违约不应该适用于漏洞(因为他们通常使用的是让编译器告诉你推断的类型)。

不过,如果您使用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)

目前没有这种可能。但根据推测它可能会被添加到GHC。

链接地址: http://www.djcxy.com/p/33209.html

上一篇: Is there a way to make GHC provide the type class constraints of typed holes?

下一篇: Debugging compile time performance issues caused by GHC's constraint solver