I have made some custom Data types for number representation in Haskell, now I want to implement Eq instances for it, but I am somehow stuck. So I have already made:
data Digit = Zero | One | Two
type Digits = [Digit]
data Sign = Pos | Neg -- Pos fuer Positive, Neg fuer Negative
newtype Numeral = Num (Sign,Digits)
instance Eq Sign where
(==) Pos Pos = True
(==) Neg Neg = True
(==) _ _ = False
instance Eq Digit where
(==) Zero Zero = True
(==) One One = True
(==) Two Two = True
(==) _ _ = False
Now I want to check out the Sign in my custom type Numeral so I tried this:
instance (Eq Sign) => Eq (Numeral) where
(==) Num(x,_)== Num(y,_) = x==y
But I get this error: Parse error in pattern : (==)
(==) (Num (x,_)) (Num (y,_)) = x==y
orNum (x,_) == Num (y,_) = x==y
. – Monneynewtype
forNumeral
. Why notdata Numeral = Num Sign Digits
? – Monneyinstance Eq Numeral where
. You don't have to say that this instance is only valid with aEq
instance forSign
, becauseSign
is a concrete type and therefore either always or never has anEq
instance. In this case always, because you defined one. – Monney