Haskell: `==' is not a (visible) method of class

5.3k views Asked by At

So, when I compile the following piece of code edited:

instance (Eq a) => PartOrd a where 
        [] == []         = True
        (x:xs) == (y:ys) = x==y && xs==ys
        _ == _           = False
        xs /= ys         = not (xs == ys)

I get:

  `==' is not a (visible) method of class 'PartOrd'

  `/=' is not a (visible) method of class 'PartOrd'
Failed, modules loaded: none.

I have looked at How to properly instantiate classes in Haskell? for some clarifications but even though I wasn`t able to fix it.

Also, is it the same when I use custom-made operators such as =~ for == and /~ for /=, because I get the same error?

EDIT: As requested:

class Eq a => PartOrd a where

   partcmp :: a -> a -> Maybe Ordering 
   partcmp x y = case bigger x y of
                   True   -> Just LT
                   False  -> case smaller x y of
                               True  -> Just GT
                               False -> case x == y of
                                          True  -> Just EQ
                                          False -> Nothing      
1

There are 1 answers

0
Alec On BEST ANSWER

It isn't clear what you are trying to achieve. Here are some thoughts:

  1. When you declare an instance of a class like instance (Eq a) => PartOrd a, you are expected to provide implementations for the functions in PartOrd a (ie partcmp, not == and /=). The compiler is telling you exactly that: == and /= are in the class Eq, not PartOrd, and you have no business defining functions not in PartOrd. In fact, no matter whether you use ==, or =~ or /~, the problem persists: you shouldn't be defining anything other that partcmp in instance (Eq a) => PartOrd a. The (Eq a) => part just says that in these definitions you can assume functions == and /= of type signatures (==), (/=) :: a -> a -> a have been defined.

  2. Why are you using lists in the instance declaration? Did you maybe mean to say instance (Eq a) => PartOrd [a]? If you really meant to say instance (Eq a) => PartOrd a, you will need to turn on the FlexibleInstances (and maybe something more too...) in GHC. (However, it doesn't really make sense to have instance (Eq a) => PartOrd a.)

  3. Finally, where do the function bigger and smaller come from? You can't use them in class (Eq a) => PartOrd a, since a is a general type. You would need functions bigger, smaller :: a -> a -> a, in which case you wouldn't even really need the PartOrd class.