how to instance Eq without deriving

Viewed 1706

Sorry about my poor English. The title may not explain what I mean.

In Data.Tree, Tree is defined as following:

-- | Multi-way trees, also known as /rose trees/.
data Tree a = Node {
        rootLabel :: a,         -- ^ label value
        subForest :: Forest a   -- ^ zero or more child trees
    }
#ifdef __GLASGOW_HASKELL__
  deriving (Eq, Read, Show, Data)
#else
  deriving (Eq, Read, Show)
#endif

It uses deriving to instance == and /= for Tree(date).

Could I do the same thing without deriving? I try things like this:

data Test a = Test a
instance Eq Test where
    (Test a) == (Test b) = a == b

But It throws an exception. I think the reason is about the types of a and b.

And what can I do whether I want to define a custom action for my data with ==.

I know I can use Functor with fmap to do it. But I want to use == like a == b where a = Test 1 and b = Test "a". Is it possible?

1 Answers
Related