Skip to content

Instantly share code, notes, and snippets.

@cameronpresley
Created June 20, 2018 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronpresley/a91287cfcf4ccee5b00b2c98da6045f7 to your computer and use it in GitHub Desktop.
Save cameronpresley/a91287cfcf4ccee5b00b2c98da6045f7 to your computer and use it in GitHub Desktop.
Deriving Example Eq instances from Haskell Book
data TisAnInteger = TisAn Integer
instance Eq TisAnInteger where
(==) (TisAn a) (TisAn b) = a == b
data TwoIntegers = Two Integer Integer
instance Eq TwoIntegers where
(==) (Two a b) (Two c d) = (a == c) && (b == d)
data StringOrInt = TisAnInt Int | TisAString String
instance Eq StringOrInt where
(==) (TisAnInt a) (TisAnInt b) = a == b
(==) (TisAString a) (TisAString b) = a == b
(==) _ _ = False
data Pair a = Pair a a
instance Eq a => Eq (Pair a) where
(==) (Pair x y) (Pair x' y') = (x == x') && (y == y')
data Tuple a b = Tuple a b
instance (Eq a, Eq b) => Eq (Tuple a b) where
(==) (Tuple x y) (Tuple x' y') = (x == x') && (y == y')
data Which a = ThisOne a | ThatOne a
instance Eq a => Eq (Which a) where
(==) (ThisOne x) (ThisOne y) = x == y
(==) (ThatOne x) (ThatOne y) = x == y
(==) _ _ = False
data EitherOr a b = Hello a | Goodbye b
instance (Eq a, Eq b) => Eq (EitherOr a b) where
(==) (Hello x) (Hello y) = x == y
(==) (Goodbye x) (Goodbye y) = x == y
(==) _ _ = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment