-- Type classes -- (*) :: Int -> Int -> Int a :: Int a = 1 * 2 -- (*) :: Float -> Float -> Float a1 :: Float a1 = 1.1 * 2.2 data Color = Red | Blue | Green deriving (Eq) eqColor :: Color -> Color -> Bool eqColor Red Red = True eqColor Blue Blue = True eqColor Green Green = True eqColor _ _ = False eqListColor :: [Color] -> [Color] -> Bool eqListColor [] [] = True eqListColor (x:xs) (y:ys) = eqColor x y && eqListColor xs ys eqListColor _ _ = False eqListChar :: [Char] -> [Char] -> Bool eqListChar = undefined class MyEq a where eq :: a -> a -> Bool data MyEq' a = E (a -> a -> Bool) eq' :: MyEq' a -> (a -> a -> Bool) eq' (E f) = f notEq :: (MyEq a) => a -> a -> Bool notEq x y = not (eq x y) instance MyEq Color where eq Red Red = True eq Blue Blue = True eq Green Green = True eq _ _ = False dictColor :: MyEq' Color dictColor = E (\ x y -> case (x, y) of (Red, Red) -> True (Blue, Blue) -> True (Green, Green) -> True _ -> False ) test1 = eq Red Blue test2 = eq' dictColor Red Blue instance (MyEq a) => MyEq [a] where eq [] [] = True eq (x:xs) (y : ys) = eq x y && eq xs ys eq _ _ = False dictList :: MyEq' a -> MyEq' [a] dictList e = E ( \ x y -> case (x , y) of ([], []) -> True (z:zs, u:us) -> eq' e z u && eq' (dictList e) zs us _ -> False ) -- eq [Red, Red] [Red, Red] test3 = eq' (dictList dictColor) [Red, Red] [Red, Blue] test4 = eq [Red, Red] [Red, Blue] instance MyEq Char where eq x y = x == y data Tree a = Leaf | Node a (Tree a) (Tree a) instance (Eq a) => Eq (Tree a) where Leaf == Leaf = True (Node x l r) == (Node y l' r') = x == y && l == l' && r == r' _ == _ = False