关于haskell:有没有办法让GHC提供类型类的类型类限制?

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

当前行为

1
2
3
4
5
6
7
8
9
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 _

期望的行为

如果ghc也告诉我类型化的hole有Show类型的类约束,那就太好了。

误码率

GHC版本7.8.1


多亏了@dominiquedevriese的GHC票,现在这张卡已在GHC 8.0中修复。

由于扩展类型默认,这在GHCI中并不明显。举个例子,

1
2
3
4
5
6
7
8
9
10
> show _

  <interactive>:7:6: error:
    ? Found hole: _h :: ()
      Or perhaps ‘_h’ is mis-spelled, or not in scope
    ? In 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)

孔的类型默认为()。这显然是所需的行为,尽管有一个参数需要说明扩展默认不应应用于孔(它们的常见用法是让编译器告诉您推断的类型)。

但是,如果您使用ghc编译或禁用ghci中的扩展默认规则(通过:set -XNoExtendedDefaultRules),我们会看到改进的结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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)


不,目前是不可能的,但可以根据推测加入温室气体控制。