Tôi đang cố gắng đại diện cho các cạnh có trọng số. Cuối cùng tôi muốn có OutE là một thể hiện của phương trình và phương trình, với ràng buộc rằng etype là một thể hiện của phương trình và phương trình. tập tin giả sử tôi có sau khi temp.hs:Thêm các ràng buộc kiểu vào ngữ cảnh của khai báo cá thể trong Haskell
data (Ord etype)=> OutE vtype etype = OutE {destVertex:: vtype, edgeValue::etype}
applyFunBy accessor ordfun = (\x y -> (ordfun (accessor x) (accessor y)))
instance Eq (OutE vtype etype) where
--(==) :: Ord etype => (OutE vtype etype) -> (OutE vtype etype) -> Bool
--(/=) :: Ord etype => (OutE vtype etype) -> (OutE vtype etype) -> Bool
(==) = applyFunBy edgeValue (==)
(/=) = applyFunBy edgeValue (/=)
khi tôi tải này trong ghci, tôi nhận được các lỗi sau đây:
temp.hs:10:19:
Could not deduce (Ord etype)
from the context (Eq (OutE vtype etype))
arising from a use of `edgeValue' at temp.hs:10:19-27
Possible fix:
add (Ord etype) to the context of the instance declaration
In the first argument of `applyFunBy', namely `edgeValue'
In the expression: applyFunBy edgeValue (==)
In the definition of `==': == = applyFunBy edgeValue (==)
temp.hs:11:19:
Could not deduce (Ord etype)
from the context (Eq (OutE vtype etype))
arising from a use of `edgeValue' at temp.hs:11:19-27
Possible fix:
add (Ord etype) to the context of the instance declaration
In the first argument of `applyFunBy', namely `edgeValue'
In the expression: applyFunBy edgeValue (/=)
In the definition of `/=': /= = applyFunBy edgeValue (/=)
Failed, modules loaded: none.
Nếu bao gồm các dòng cho chữ ký kiểu cho (==) và (\ =), tôi nhận được:
temp.hs:6:1:
Misplaced type signature:
== ::
(Ord etype) => (OutE vtype etype) -> (OutE vtype etype) -> Bool
temp.hs:7:1:
Misplaced type signature:
/= ::
(Ord etype) => (OutE vtype etype) -> (OutE vtype etype) -> Bool
'deriving (Eq)' sẽ tạo các toán tử bình đẳng dựa trên ** tất cả các trường ghi ** (và do đó tạo ra một thể hiện 'Eq' phức tạp với' Eq vtype') trong khi cá thể rõ ràng được đưa ra trong câu hỏi chỉ so sánh dựa trên 'edgeValue '. – Dario
Phải, tôi đã không nhận thấy anh ta đã làm điều đó cho đến khi viết xong. Cảm ơn bạn đã chỉ ra điều đó. –