Overriding the standard definition of Eq for a specific template

Suppose I have defined some data type that outputs Eq, but wants to insert my own definition (==) for some template. Is there a way to do this or do I need to define (==) for each template?

eg.

data Asdf = One Char | Two Char Char --(deriving Eq)
instance Eq Asdf where
  (==) (One _) (One _) = True
  --otherwise use what the derived definition would have done
  --can I do this without defining these patterns myself?
+4
source share
2 answers

This question discusses how to do something very similar for an instance Showusing the https://hackage.haskell.org/package/generic-deriving package : Accessing the "default reading" in Haskell?

See this answer in particular: fooobar.com/questions/982868 / ...

Show, . Eq , .

+5

, , , , .

data MyType x = A x | B x x deriving (Eq) , ,

instance Eq x => Eq (MyType x) where
    A x1    == A x2     = x1 == x2
    B x1 x2 == B x3 x4  = x1 == x3 && x2 == x4
    _       == _        = False

, ( Eq x => ), - n 2 .

, " ", , - ; , , deriving (Eq) , , , - , , Eq , - .

, - . . , , , n :

newtype EverythingIsEqual x = E x deriving (Show)
instance Eq (EverythingIsEqual x) where
    _ == _ = True

data MyType x = A (EverythingIsEqual x) | B x x deriving (Show, Eq, Ord)

newtype , Eq - - ; , " , , ", " " Haskell Ord, - , , . "

+8

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


All Articles