I am implementing an union-find data structure in Haskell.
I wanted to use parametrized types but I am facing a little issue when I try to compare the objects I use as parameters.
data UnionFindElement valueType =
RootElement valueType |
ElementWithParent valueType (UnionFindElement valueType)
holds :: UnionFindElement valueType -> valueType -> Bool
holds (RootElement v) value = v == value
It seems that equality is not defined.
No instance for (Eq valueType)
arising from a use of `=='
In the expression: v == value
How can I restrict valueType to consider only types with a defined equality relationship?
holds :: (Eq valueType) => UnionFindElement valueType -> valueType -> Bool(Eq valueType) =>meansvalueTypeis from classEq(equatable values), and the construct exists exactly to be able to restrict types of generic parameters.Please note this also works on data declarations and several other places. You can learn more on http://en.wikibooks.org/wiki/Haskell/Classes_and_types#Type_constraints
For a more general overview, http://en.wikipedia.org/wiki/Type_class and http://en.wikipedia.org/wiki/Ad-hoc_polymorphism