3
私はyou can add constraints on associated type families and data familiesを認識しています。これが行うことは、あなたのクラスのインスタンスすべてに制約を適用することです。関連する型ファミリを使用するときの型のクラス制約を推測する
しかし、私はこれらの制約をどのように推論するのかを把握することはできませんインスタンスの導出または関数の宣言。たとえば、このコードはチェックを入力して失敗します。
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
import Data.Proxy (Proxy)
class Eq (FooT a) => Foo a where
type FooT a :: *
-- Can't infer it in an instance derivation
data CantInferEq a = CantInferEq (FooT a) deriving Eq
-- Also can't infer it in a function declaration.
-- The Proxy is there to avoid non-injectivity issues.
cantInferEq :: Proxy a -> FooT a -> FooT a -> Bool
cantInferEq _ x y = x == y
エラーメッセージは以下のとおりです。
Test.hs:11:52: No instance for (Eq (FooT a)) …
arising from the first field of ‘CantInferEq’ (type ‘FooT a’)
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
When deriving the instance for (Eq (CantInferEq a))
Test.hs:16:23: No instance for (Eq (FooT a)) arising from a use of ‘==’ …
In the expression: x == y
In an equation for ‘cantInferEq’: cantInferEq _ x y = x == y
Compilation failed.
ここで何が起こっていますか?希望の動作を得るための回避策はありますか?