型付きホールの型クラス制約をGHCに提供させる方法はありますか?


103

現在の行動

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 _

望ましい行動

型付きの穴にShow型クラスの制約があることもGHCが教えてくれるといいですね。

その他

GHCバージョン7.8.1


16
私の知る限り、これは現在可能ではありませんが、確かに役立ちます。これについては、GHCバグトラッカーで機能リクエストを開く価値があるかもしれません。
— kosmikus 14

11
これが役立つと私は同意します。私はそれをGHC トラックの
— Dominique Devriese

4
今のところ、型前穴のトリックを使用できますshow (undefined :: () -> ())。GHCは型チェックエラーで詳細を通知します。
— phadej 14

1
これは機能のリクエストですか、それとも実際の質問ですか?つまり、望みどおりにGHCを作成する方法がないこと、または現在のコンパイラーで必要なものを取得できる可能性があることは確かにわかっていますが、その方法はわかりませんか?
— stakx-2015年

1
@stakx両方のビットです。もともとこの質問を書いたとき、GHCが型クラスの制約を提供しなかった理由が混乱し、型付きホールを間違って使用していると思っていました。次に、現在これを行うことは不可能ですが、GHCに追加できる可能性があると一部の人は私に言いました。それで、それがすぐに追加されることを望んでいました。多くの人がそれを使いたいようです。phadejのトリックは当面はうまくいくようですが、型付きの穴ベースのソリューションほどエレガントではなく使いやすいものではありません。
— Wizek 2015

回答:


2

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


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.