Tôi có các lớp loại, cho tất cả những gì tôi muốn có một số hành vi phổ biến. Vấn đề của tôi được giải thích trong đoạn mã sau:Haskell Không thể suy ra ... từ lỗi Ngữ cảnh
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
Khi tôi nạp mô-đun này, tôi nhận được lỗi sau:
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ôi đang ở mất phải làm gì trong trường hợp này. Ngay cả khi T và U là trường hợp của A và B, tôi nhận được lỗi này. Nếu tôi không thể trả lại giá trị loại cứng nhắc từ method
, làm cách nào để mã hóa phần này?
có thể trùng lặp của [danh sách loại dữ liệu: "không thể suy ra (a ~ SomeType) từ ngữ cảnh (SomeTypeclass a)"] (http://stackoverflow.com/questions/5453514/lists-of-data-types -có-không-deduce-a-sometype-từ-the-context-sometype) – Landei