Ich bin mir bewusst, dass you can add constraints on associated type families and data families. Damit werden die Einschränkungen für alle Instanzen Ihrer Klasse erzwungen.Ableitungsklasse Klassenbeschränkung bei Verwendung der zugehörigen Familien
Aber ich kann nicht herausfinden, wie diese Einschränkungen in Instanz Ableitung oder Funktion Deklarationen. Zum Beispiel versagt diese Codeprüfung eingeben:
{-# 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
die Fehlermeldungen sind:
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.
Was ist hier los? Gibt es einen Workaround, um mein gewünschtes Verhalten zu erhalten?