I used the type context when creating the instance declaration for the data type I created.
data Set a = Insert a (Set a) | EmptySet instance (Show a) => Show (Set a) where show x = "{" ++ show' x ++ "}" where show' (Insert x EmptySet) = show x show' (Insert x xs) = show x ++ ", " ++ show' xs instance Eq a => Eq (Set a) where (Insert x xs) == (Insert y ys) = (x == y) && (xs == ys)
So now I have to add an Eq type context to all the functions that I define that use my Set type, for example, or I get an error like:
memberSet::Eq a =>a->Set a->Bool memberSet _ EmptySet = False memberSet x (Insert y ys) | x == y = True | otherwise = memberSet x ys subSet::Eq a=>Set a->Set a->Bool subSet EmptySet _ = True subSet (Insert a as) bs | memberSet a bs = subSet as bs | otherwise = False
The error I get looks like this:
No instance for (Eq a) arising from a use of `==' In the expression: (x == y) In a stmt of a pattern guard for an equation for `memberSet': (x == y) In an equation for `memberSet': memberSet x (Insert y ys) | (x == y) = True | otherwise = memberSet x ys Failed, modules loaded: none.
What does it mean? Why am I getting this error? I realized that as soon as I make an instance declaration, Haskell will be able to automatically verify that things that compare with "==" in my memberSet and subSet functions will automatically check for Eq?
Edit for clarity:
My problem is that I do not understand why type contexts are needed for "memberSet" and "subSet". If I delete them like this, it will not compile.
memberSet::a->Set a->Bool memberSet _ EmptySet = False memberSet x (Insert y ys) | x == y = True | otherwise = memberSet x ys subSet::Set a->Set a->Bool subSet EmptySet _ = True subSet (Insert a as) bs | memberSet a bs = subSet as bs | otherwise = False
source share