örnek bildirimleri bağlamında tür kısıtlamaları ekleme Ağırlıklı kenarları temsil etmeye çalışıyorum. Sonunda, EQ ve Ord'nin bir örneği olması için OutE'nin Eq ve Ord örneğinin olmasını istiyorum. temp.hs olarak varsayalım ben şu var file: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 (/=)
ben GHCi bu yüklediğinizde, aşağıdaki hataları alıyorum: tip imzalar için çizgiler içeriyorsa
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.
(==) ve (\ =), alıyorum:
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
gibi bir işlevde gerçekten ihtiyacınız olduğunda typeclass eklerim '' türetme (Eq) '** tüm ** kayıt alanlarına dayalı eşitlik operatörleri oluşturacaktır (ve böylece“ Eq vtype ”ile hantal bir“ Eq ”örneği üretecektir), soruda verilen açık örnek sadece“ edgeValue ”ile karşılaştırılır. '. – Dario
Doğru, o yazdıktan sonra bunu yaptıklarını farketmedim. Bunu işaret ettiğin için teşekkürler. –