[Haskell-beginners] Sections in Class definition

Brent Yorgey byorgey at seas.upenn.edu
Mon Aug 23 05:21:52 EDT 2010


On Mon, Aug 23, 2010 at 10:28:38AM +0300, John Smith wrote:
> I tried to reproduce the definition of Eq with
> 
> class Eq a where
>     (==), (/=) :: a -> a -> Bool
> 
>     (/=) = not (==)
>     (==) = not (/=)

Consider the type of not:

  not :: Bool -> Bool

And the type of (==):

  (==) :: Eq a => a -> a -> Bool

You are providing (==) as the argument to not, but this will not work,
since not expects an argument of type Bool and (==) does not have type
Bool (it has type a -> a -> Bool).  This is what the error message is
telling you.

It is possible to define these in a point-free style, but I would not
recommend it:

  (/=) = (not .) . (==)

Is it obvious to you what this definition does, and how it works?  Me
neither.  A little better would be something like

  oo f g x y = f (g x y)
  (/=) = not `oo` (==)

But I still prefer the nice and simple

  x /= y = not (x == y)

-Brent


More information about the Beginners mailing list