Sto tentando di rappresentare i bordi ponderati. Alla fine voglio che OutE sia un'istanza di Eq e Ord, con il vincolo che etype sia un'istanza di Eq e Ord. Si supponga Ho seguente file come temp.hs:Aggiunta di vincoli di tipo al contesto delle dichiarazioni di istanza in 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 (/=)
quando carico in questo ghci, ottengo i seguenti errori:
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.
Se includere le righe per le firme di tipo per (==) e (\ =), ottengo:
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
'derivando (Eq)' genererà operatori di uguaglianza basati su ** tutti i campi di record ** (e quindi genererà una complessa istanza di 'Eq' con un' Eq vtype') mentre l'istanza esplicita fornita nella domanda si confronta solo in base a 'edgeValue '. – Dario
Giusto, non ho notato che lo stava facendo fino a dopo aver scritto quel pezzo. Grazie per la segnalazione. –