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

HaskellTypesGhc

Haskell Problem Overview


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 ofshow’, 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

Haskell Solutions


Solution 1 - Haskell

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 scopeIn the first argument ofshow’, 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 ofshow’
      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 ofshow’, namely ‘_’
      In the expression: show _
      In an equation for ‘it’: it = show _
    • Relevant bindings include
        it :: String (bound at <interactive>:3:1)

Solution 2 - Haskell

No currently its not possible.But it may be added to GHC as per the speculations.

Solution 3 - Haskell

Try it :: _ => _ in GHC 8.8+.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWizekView Question on Stackoverflow
Solution 1 - HaskellcrockeeaView Answer on Stackoverflow
Solution 2 - HaskellVikas AnandView Answer on Stackoverflow
Solution 3 - HaskellMichal GajdaView Answer on Stackoverflow