I'm trying to implement a simple one Setin Haskell, and I'm stuck on how to express class restrictions on the elements it contains.
The type class is Setpretty simple:
class Set s where
empty :: s a
isEmpty :: s a -> Bool
insert :: s a -> a -> s a
contains :: s a -> a -> Bool
A trivial implementation Setis a binary search tree:
data BinarySearchTree a = Node a (BinarySearchTree a) (BinarySearchTree a) | Leaf
I get a little stuck when it comes to declaring the correct class constraints:
Setit requires at least the ability to decide whether two elements are equal - its values must have an instance Eq.BinarySearchTreerequires its elements to have an instance Ord, since each node must have a smaller element on the left and more on the right.
, - Set, a Eq:
class Set s where
empty :: Eq a => s a
isEmpty :: Eq a => s a -> Bool
insert :: Eq a => s a -> a -> s a
contains :: Eq a => s a -> a -> Bool
Set BinarySearchTree : Ord Eq, "" .
, , , BinarySearchTree , , Eq? ?
, , - BinarySearchTree, - :
data Ord a => BinarySearchTree a = Node a (BinarySearchTree a) (BinarySearchTree a) | Leaf
, : , BinarySearchTree Ord, , , , BinarySearchTree - , .
? , , ?