1
- module Ch15.Semigroups where
1
+ module Ch15.Exercise where
2
2
3
3
import Test.QuickCheck hiding (Failure , Success )
4
4
@@ -10,17 +10,20 @@ data Trivial = Trivial
10
10
instance Semigroup Trivial where
11
11
_ <> _ = Trivial
12
12
13
+ instance Monoid Trivial where
14
+ mempty = Trivial
15
+
13
16
instance Arbitrary Trivial where
14
17
arbitrary = pure Trivial
15
18
16
- semigroupAssocProp :: (Eq a , Semigroup a ) => a -> a -> a -> Bool
17
- semigroupAssocProp a b c = a <> (b <> c) == (a <> b) <> c
18
-
19
19
newtype Identity a = Identity a deriving (Eq , Show )
20
20
21
21
instance (Semigroup a ) => Semigroup (Identity a ) where
22
22
(Identity a) <> (Identity b) = Identity $ a <> b
23
23
24
+ instance (Monoid a ) => Monoid (Identity a ) where
25
+ mempty = Identity mempty
26
+
24
27
instance (Arbitrary a ) => Arbitrary (Identity a ) where
25
28
arbitrary = Identity <$> arbitrary
26
29
@@ -29,6 +32,9 @@ data Two a b = Two a b deriving (Eq, Show)
29
32
instance (Semigroup a , Semigroup b ) => Semigroup (Two a b ) where
30
33
(Two a b) <> (Two a' b') = Two (a <> a') (b <> b')
31
34
35
+ instance (Monoid a , Monoid b ) => Monoid (Two a b ) where
36
+ mempty = Two mempty mempty
37
+
32
38
instance (Arbitrary a , Arbitrary b ) => Arbitrary (Two a b ) where
33
39
arbitrary = Two <$> arbitrary <*> arbitrary
34
40
@@ -37,6 +43,9 @@ data Three a b c = Three a b c deriving (Eq, Show)
37
43
instance (Semigroup a , Semigroup b , Semigroup c ) => Semigroup (Three a b c ) where
38
44
(Three a b c) <> (Three a' b' c') = Three (a <> a') (b <> b') (c <> c')
39
45
46
+ instance (Monoid a , Monoid b , Monoid c ) => Monoid (Three a b c ) where
47
+ mempty = Three mempty mempty mempty
48
+
40
49
instance (Arbitrary a , Arbitrary b , Arbitrary c ) => Arbitrary (Three a b c ) where
41
50
arbitrary = Three <$> arbitrary <*> arbitrary <*> arbitrary
42
51
@@ -45,6 +54,9 @@ data Four a b c d = Four a b c d deriving (Eq, Show)
45
54
instance (Semigroup a , Semigroup b , Semigroup c , Semigroup d ) => Semigroup (Four a b c d ) where
46
55
(Four a b c d) <> (Four a' b' c' d') = Four (a <> a') (b <> b') (c <> c') (d <> d')
47
56
57
+ instance (Monoid a , Monoid b , Monoid c , Monoid d ) => Monoid (Four a b c d ) where
58
+ mempty = Four mempty mempty mempty mempty
59
+
48
60
instance (Arbitrary a , Arbitrary b , Arbitrary c , Arbitrary d ) => Arbitrary (Four a b c d ) where
49
61
arbitrary = Four <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
50
62
@@ -53,6 +65,9 @@ newtype BoolConj = BoolConj Bool deriving (Eq, Show)
53
65
instance Semigroup BoolConj where
54
66
(BoolConj b) <> (BoolConj b') = BoolConj $ b && b'
55
67
68
+ instance Monoid BoolConj where
69
+ mempty = BoolConj $ getAll (mempty :: All )
70
+
56
71
instance Arbitrary BoolConj where
57
72
arbitrary = BoolConj <$> arbitrary
58
73
@@ -61,6 +76,9 @@ newtype BoolDisj = BoolDisj Bool deriving (Eq, Show)
61
76
instance Semigroup BoolDisj where
62
77
(BoolDisj b) <> (BoolDisj b') = BoolDisj $ b || b'
63
78
79
+ instance Monoid BoolDisj where
80
+ mempty = BoolDisj $ getAny (mempty :: Any )
81
+
64
82
instance Arbitrary BoolDisj where
65
83
arbitrary = BoolDisj <$> arbitrary
66
84
@@ -88,9 +106,6 @@ instance (CoArbitrary a, Arbitrary b) => Arbitrary (Combine a b) where
88
106
instance Show (Combine a b ) where
89
107
show _ = " Combine (a -> b)"
90
108
91
- combineSemigroupAssocProp :: (Eq b , Semigroup b ) => Combine a b -> Combine a b -> Combine a b -> a -> Bool
92
- combineSemigroupAssocProp f g h a = unCombine (f <> (g <> h)) a == unCombine ((f <> g) <> h) a
93
-
94
109
newtype Comp a = Comp { unComp :: a -> a }
95
110
96
111
instance (Semigroup a ) => Semigroup (Comp a ) where
@@ -102,9 +117,6 @@ instance (CoArbitrary a, Arbitrary a) => Arbitrary (Comp a) where
102
117
instance Show (Comp a ) where
103
118
show _ = " Comp (a -> a)"
104
119
105
- compSemigroupAssocProp :: (Eq a , Semigroup a ) => Comp a -> Comp a -> Comp a -> a -> Bool
106
- compSemigroupAssocProp f g h a = unComp (f <> (g <> h)) a == unComp ((f <> g) <> h) a
107
-
108
120
data Validation a b = Failure a | Success b deriving (Eq , Show )
109
121
110
122
instance (Semigroup a ) => Semigroup (Validation a b ) where
@@ -124,51 +136,3 @@ failure = Failure
124
136
125
137
success :: Int -> Validation String Int
126
138
success = Success
127
-
128
- -- Move this to the test directory
129
- main :: IO ()
130
- main = do
131
- putStr " Trivial Semigroup Assoc Prop: "
132
- quickCheck (semigroupAssocProp @ Trivial )
133
-
134
- putStr " Identity a Assoc Prop: "
135
- quickCheck (semigroupAssocProp @ (Identity (Sum Int )))
136
-
137
- putStr " Identity a 2 Assoc Prop: "
138
- quickCheck (semigroupAssocProp @ (Identity (Product Int )))
139
-
140
- putStr " Identity a 3 Assoc Prop: "
141
- quickCheck (semigroupAssocProp @ (Identity (First (Maybe Int ))))
142
-
143
- putStr " Two Assoc Prop: "
144
- quickCheck (semigroupAssocProp @ (Two (Sum Int ) String ))
145
-
146
- putStr " Two Assoc Prop: "
147
- quickCheck (semigroupAssocProp @ (Two (Sum Int ) (Product Int )))
148
-
149
- putStr " Three Assoc Prop: "
150
- quickCheck (semigroupAssocProp @ (Three Any (Sum Int ) (Product Int )))
151
-
152
- putStr " Four Assoc Prop: "
153
- quickCheck (semigroupAssocProp @ (Four (First (Maybe String )) (Last (Maybe Bool )) All (Product Int )))
154
-
155
- putStr " BoolConj Assoc Prop: "
156
- quickCheck (semigroupAssocProp @ BoolConj )
157
-
158
- putStr " BoolDisj Assoc Prop: "
159
- quickCheck (semigroupAssocProp @ BoolDisj )
160
-
161
- putStr " Or Assoc Prop: "
162
- quickCheck (semigroupAssocProp @ (Or Int String ))
163
-
164
- putStr " Combine Assoc Prop: "
165
- quickCheck (combineSemigroupAssocProp @ (Sum Int ) @ Int )
166
-
167
- putStr " Comp Assoc Prop: "
168
- quickCheck (compSemigroupAssocProp @ (Sum Int ))
169
-
170
- putStrLn " "
171
- print $ success 1 <> failure " blah"
172
- print $ failure " woot" <> failure " blah"
173
- print $ success 1 <> success 2
174
- print $ failure " woot" <> success 2
0 commit comments