|
| 1 | +module Applicative.ChapterExercises.ApplicativeInstances ( |
| 2 | + Identity (..) |
| 3 | + , Pair (..) |
| 4 | + , Two (..) |
| 5 | + , Three (..) |
| 6 | + , Three' (..) |
| 7 | + , Four (..) |
| 8 | + , Four' (..) |
| 9 | +) where |
| 10 | + |
| 11 | +import Control.Applicative (liftA2, liftA3) |
| 12 | +import Test.QuickCheck |
| 13 | +import Test.QuickCheck.Checkers |
| 14 | + |
| 15 | +-- Question 1 |
| 16 | +newtype Identity a = Identity a |
| 17 | + deriving (Show, Eq) |
| 18 | + |
| 19 | +instance Applicative Identity where |
| 20 | + pure = Identity |
| 21 | + Identity f <*> Identity x = Identity $ f x |
| 22 | + |
| 23 | +instance Functor Identity where |
| 24 | + fmap f (Identity a) = Identity $ f a |
| 25 | + |
| 26 | +instance (Arbitrary a) => Arbitrary (Identity a) where |
| 27 | + arbitrary = Identity <$> arbitrary |
| 28 | + |
| 29 | +instance (Eq a) => EqProp (Identity a) where (=-=) = eq |
| 30 | + |
| 31 | +-- Question 2 |
| 32 | +data Pair a = Pair a a |
| 33 | + deriving (Show, Eq) |
| 34 | + |
| 35 | +instance Functor Pair where |
| 36 | + fmap f (Pair a b) = Pair (f a) (f b) |
| 37 | + |
| 38 | +instance Applicative Pair where |
| 39 | + pure a = Pair a a |
| 40 | + Pair f g <*> Pair x y = Pair (f x) (g y) |
| 41 | + |
| 42 | +instance (Arbitrary a) => Arbitrary (Pair a) where |
| 43 | + arbitrary = liftA2 Pair arbitrary arbitrary |
| 44 | + |
| 45 | +instance (Eq a) => EqProp (Pair a) where (=-=) = eq |
| 46 | + |
| 47 | +-- Question 3 |
| 48 | +data Two a b = Two a b |
| 49 | + deriving (Show, Eq) |
| 50 | + |
| 51 | +instance Functor (Two a) where |
| 52 | + fmap f (Two a b) = Two a (f b) |
| 53 | + |
| 54 | +instance (Monoid a) => Applicative (Two a) where |
| 55 | + pure = Two mempty |
| 56 | + Two f g <*> Two x y = Two (f <> x) (g y) |
| 57 | + |
| 58 | +instance (Arbitrary a, Arbitrary b) => Arbitrary (Two a b) where |
| 59 | + arbitrary = liftA2 Two arbitrary arbitrary |
| 60 | + |
| 61 | +instance (Eq a, Eq b) => EqProp (Two a b) where (=-=) = eq |
| 62 | + |
| 63 | +-- Question 4 |
| 64 | +data Three a b c = Three a b c |
| 65 | + deriving (Show, Eq) |
| 66 | + |
| 67 | +instance Functor (Three a b) where |
| 68 | + fmap f (Three a b c) = Three a b (f c) |
| 69 | + |
| 70 | +instance (Monoid a, Monoid b) => Applicative (Three a b) where |
| 71 | + pure = Three mempty mempty |
| 72 | + Three f g h <*> Three x y z = Three (f <> x) (g <> y) (h z) |
| 73 | + |
| 74 | +instance (Arbitrary a, Arbitrary b, Arbitrary c) => Arbitrary (Three a b c) where |
| 75 | + arbitrary = liftA3 Three arbitrary arbitrary arbitrary |
| 76 | + |
| 77 | +instance (Eq a, Eq b, Eq c) => EqProp (Three a b c) where (=-=) = eq |
| 78 | + |
| 79 | + |
| 80 | +-- Question 5 |
| 81 | +data Three' a b = Three' a b b |
| 82 | + deriving (Show, Eq) |
| 83 | + |
| 84 | +instance Functor (Three' a) where |
| 85 | + fmap f (Three' a b b') = Three' a (f b) (f b') |
| 86 | + |
| 87 | +instance (Monoid a) => Applicative (Three' a) where |
| 88 | + pure b = Three' mempty b b |
| 89 | + Three' f g h <*> Three' x y z = Three' (f <> x) (g y) (h z) |
| 90 | + |
| 91 | +instance (Arbitrary a, Arbitrary b) => Arbitrary (Three' a b) where |
| 92 | + arbitrary = liftA3 Three' arbitrary arbitrary arbitrary |
| 93 | + |
| 94 | +instance (Eq a, Eq b) => EqProp (Three' a b) where (=-=) = eq |
| 95 | + |
| 96 | + |
| 97 | +-- Question 6 |
| 98 | +data Four a b c d = Four a b c d |
| 99 | + deriving (Show, Eq) |
| 100 | + |
| 101 | +instance Functor (Four a b c) where |
| 102 | + fmap f (Four a b c d) = Four a b c (f d) |
| 103 | + |
| 104 | +instance (Monoid a, Monoid b, Monoid c) => Applicative (Four a b c) where |
| 105 | + pure = Four mempty mempty mempty |
| 106 | + Four f g h i <*> Four x y z w = Four (f <> x) (g <> y) (h <> z) (i w) |
| 107 | + |
| 108 | +instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d) => Arbitrary (Four a b c d) where |
| 109 | + arbitrary = Four <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary |
| 110 | + |
| 111 | +instance (Eq a, Eq b, Eq c, Eq d) => EqProp (Four a b c d) where (=-=) = eq |
| 112 | + |
| 113 | + |
| 114 | +-- Question 7 |
| 115 | +data Four' a b = Four' a a a b |
| 116 | + deriving (Show, Eq) |
| 117 | + |
| 118 | +instance Functor (Four' a) where |
| 119 | + fmap f (Four' a a' a'' b) = Four' a a' a'' (f b) |
| 120 | + |
| 121 | +instance (Monoid a) => Applicative (Four' a) where |
| 122 | + pure = Four' mempty mempty mempty |
| 123 | + Four' f g h i <*> Four' x y z w = Four' (f <> x) (g <> y) (h <> z) (i w) |
| 124 | + |
| 125 | +instance (Arbitrary a, Arbitrary b) => Arbitrary (Four' a b) where |
| 126 | + arbitrary = Four' <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary |
| 127 | + |
| 128 | +instance (Eq a, Eq b) => EqProp (Four' a b) where (=-=) = eq |
0 commit comments