I have some code that I would like to use to append an edge to a Node data structure:
import Data.Set (Set)
import qualified Data.Set as Set
data Node = Vertex String (Set Node)
deriving Show
addEdge :: Node -> Node -> Node
addEdge (Vertex name neighbors) destination
| Set.null neighbors = Vertex name (Set.singleton destination)
| otherwise = Vertex name (Set.insert destination neighbors)
However when I try to compile I get this error:
No instance for (Ord Node)
arising from a use of `Set.insert'
As far as I can tell, Set.insert expects nothing but a value and a set to insert it into. What is this Ord?
In GHCi:
So yes, it does expect
Ord. As for whatOrdmeans, it’s a type class for ordered values. It’s required in this case becauseData.Setuses a search tree, and so needs to be able to compare values to see which is larger or if they’re equal.Nearly all of the standard built-in data types are instances of
Ord, as well as things like lists, tuples,Maybe, etc. being instances ofOrdwhen their type parameter(s) are. The most notable exception, of course, are functions, where no sensible concept of ordering (or even equality) can be defined.In many cases, you can automatically create instances of type classes for your own data types using a
derivingclause after the declaration:For parameterized types, the automatic derivation depends on the type parameter also being an instance, as is the case with lists, tuples, and such.
Besides
Ord, some important type classes areEq(equality comparisons, but not less/greater than),Enum(types you can enumerate values of, such as countingIntegers), andRead/Show(simple serialization/deserialization with strings). To learn more about type classes, try this chapter in Real World Haskell or, for a more general overview, there’s a Wikipedia article.