У меня есть классы типов, для всех из которых я хотел бы иметь какое-то общее поведение. Моя проблема объясняется в следующем коде:
class A a class B b class X x where method :: (A a, B b) => x -> a -> b data T = L | M | N data U = P | Q | R instance A T instance B U data Y = ZZZ instance X Y where method _ L = P method _ M = Q method _ N = R
Когда я загружаю этот модуль, я получаю следующую ошибку:
example.hs:19:14: Could not deduce (a ~ T) from the context (A a, B b) bound by the type signature for method :: (A a, B b) => Y -> a -> b at example.hs:(17,5)-(19,18) `a' is a rigid type variable bound by the type signature for method :: (A a, B b) => Y -> a -> b at example.hs:17:5 In the pattern: N In an equation for `method': method _ N = R In the instance declaration for `X Y' example.hs:19:18: Could not deduce (b ~ U) from the context (A a, B b) bound by the type signature for method :: (A a, B b) => Y -> a -> b at example.hs:(17,5)-(19,18) `b' is a rigid type variable bound by the type signature for method :: (A a, B b) => Y -> a -> b at example.hs:17:5 In the expression: R In an equation for `method': method _ N = R In the instance declaration for `X Y' Failed, modules loaded: none.
Я не понимаю, что делать в этом случае. Даже когда T и U являются экземплярами A и B, я получаю эту ошибку. Если я не могу вернуть значение жесткого типа из метода
, как мне закодировать эту часть?