Created
June 20, 2018 18:43
-
-
Save cameronpresley/a91287cfcf4ccee5b00b2c98da6045f7 to your computer and use it in GitHub Desktop.
Deriving Example Eq instances from Haskell Book
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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