As an example of Eq without output

I'm sorry for my bad english. The title may not explain what I mean.

In Data.Tree Treeis defined as follows:

-- | 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 derivingfor instance ==and /=for Tree(date).

Can I do the same without receiving? I try things like this:

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

But he throws an exception. I think the reason is because types a and b.

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

I know what I can use Functorwith fmapfor this. But I want to use ==how a == b, where a = Test 1and b = Test "a". Is it possible?

+4
1

instance Eq Tree Test, .

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

-, Test Eq Test . , data Test a = ..., , . :

instance Eq (Test a) where
    (Test y) == (Test x) = x == y

, Eq Test a. a b x y. , " " " " , .

: x == y. guantee, a Eq. , :

instance Eq a => Eq (Test a) where
    (Test y) == (Test x) = x == y

, Test a Eq, a Eq.

Tree instance Eq :

instance (Eq a, Eq (Forest a)) => Eq (Tree a) where
    (Tree x1 y1) == (Tree x2 y2) = x1 == x2 && y1 == y2

(, , , , () -, ).

, - @luqui - if type Forest a = [Tree a], Eq (Forest a), instance Eq a => Eq [a] . , :

instance Eq a => Eq (Tree a) where
    (Tree x1 y1) == (Tree x2 y2) = x1 == x2 && y1 == y2
+10

Source: https://habr.com/ru/post/1677150/


All Articles