Próbuję reprezentować ważone krawędzie. W końcu chcę, aby OutE był instancją Eq i Ord, z ograniczeniem, że etype jest przykładem Eq i Ord. Przyjmijmy, mam następujący plik jako temp.hs:Dodawanie ograniczeń typu do kontekstu deklaracji instancji w 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 (/=)
gdy załadować to w ghci, otrzymuję następujące błędy:
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.
Jeśli obejmują linie do podpisu dla typu (==) i (\ =), otrzymuję:
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
'wyprowadzenie (Eq)' spowoduje wygenerowanie operatorów równościowych w oparciu o ** wszystkie ** pola rekordów (iw ten sposób wygeneruje niewygodną instancję 'Eq' z' Eq vtype'), podczas gdy jawna instancja podana w pytaniu porównuje wartość bazując na 'edgeValue '. – Dario
Racja, nie zauważyłem, że to robił, póki nie napisał tego. Dziękuję za wskazanie tego. –