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

Current behavior

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 _

Desired behavior

It would be nice if GHC would also tell me that the typed hole has the Show type class constraint.

Misc

GHC Version 7.8.1


This is now fixed in GHC 8.0 thanks to @DominiqueDevriese's GHC ticket.

Due to extended type defaulting, this isn't immediately obvious in GHCi. With your example,

> 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)

the type of the hole is defaulted to () . This is apparently the desired behavior, though there's an argument to be made that extended defaulting shouldn't apply to holes (as a common use for them is to get the compiler to tell you the inferred type).

Nevertheless, if you compile with GHC or disable extended default rules in GHCi (via :set -XNoExtendedDefaultRules ), we see the result of the improvements:

<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/33210.html

上一篇: 通过包容来减弱乙烯基的RecAll约束

下一篇: 有没有办法让GHC提供类型孔的类型约束?