3
votes

I got the following:

data Pair a =
  Pair a a

And want to instantiate the Eq typeclass to it.

instance Eq (Pair a) where
  (==) (Pair x x') = x == x'

I get the error:

Couldn't match expected type Pair a -> Bool' with actual type Bool'

What did I write wrong?

2

2 Answers

6
votes

Your implementation doesn't work because

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

You are assuming (==) takes one argument ((Pair x x') is actually one argument) and it actually needs two. Hence the error,

Couldn't match expected type Pair a -> Bool' with actual type Bool'

(==) was partially applied so it returned Pair a -> Bool while it was expected to return a Bool.

4
votes

I think this is the correct one but I am not 100% sure.

instance Eq a => Eq (Pair a) where
  (==) (Pair x x') (Pair y y') = x == y && x' == y'