-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch21traversable.hs
228 lines (162 loc) · 5.41 KB
/
ch21traversable.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
module Chapter21Traversable where
import Data.Foldable
import Test.QuickCheck
import Test.QuickCheck.Checkers
import Test.QuickCheck.Classes
-- 21.12 Chapter Exercises (page 863)
-- Traversable instances + QuickCheck
newtype Identity a = Identity a deriving (Eq, Ord, Show)
instance Functor Identity where
fmap f (Identity a) = Identity (f a)
instance Foldable Identity where
foldr f z (Identity a) = f a z
instance Traversable Identity where
traverse f (Identity a) = Identity <$> f a
instance (Arbitrary a) => Arbitrary (Identity a) where
arbitrary = fmap Identity arbitrary
instance (Eq a) => EqProp (Identity a) where
(=-=) = eq
identityCheck :: IO ()
identityCheck = quickBatch $ traversable (undefined :: Identity (Int, Int, [Int]))
--
newtype Constant a b = Constant { getConstant :: a } deriving (Eq, Show)
instance Functor (Constant a) where
fmap _ (Constant a) = Constant a
instance Foldable (Constant a) where
foldr _ z (Constant _) = z
instance Traversable (Constant a) where
traverse _ (Constant a) = pure $ Constant a
instance (Arbitrary a) => Arbitrary (Constant a b) where
arbitrary = fmap Constant arbitrary
instance (Eq a) => EqProp (Constant a b) where
(=-=) = eq
constantCheck :: IO ()
constantCheck = quickBatch $ traversable (undefined :: Constant Int (Int, Int, [Int]))
--
data Talvez a =
No
| Si a
deriving (Eq, Show)
instance Functor Talvez where
fmap _ No = No
fmap f (Si a) = Si (f a)
instance Foldable Talvez where
foldr _ z No = z
foldr f z (Si a) = f a z
instance Traversable Talvez where
traverse _ No = pure No
traverse f (Si a) = Si <$> f a
instance (Arbitrary a) => Arbitrary (Talvez a) where
arbitrary = oneof [return No, fmap Si arbitrary]
instance (Eq a) => EqProp (Talvez a) where
(=-=) = eq
talvezCheck :: IO ()
talvezCheck = quickBatch $ traversable (undefined :: Talvez (Int, Int, [Int]))
--
data List a =
Nil
| Cons a (List a)
deriving (Eq, Show)
instance Functor List where
fmap _ Nil = Nil
fmap f (Cons x xs) = Cons (f x) (fmap f xs)
instance Foldable List where
foldr _ z Nil = z
foldr f z (Cons x xs) = f x (foldr f z xs)
instance Traversable List where
traverse f = foldr (\x acc -> fmap Cons (f x) <*> acc) (pure Nil)
instance Arbitrary a => Arbitrary (List a) where
arbitrary = oneof
[ return Nil
, do
x <- arbitrary
xs <- arbitrary
return $ Cons x xs
]
instance Eq a => EqProp (List a) where
(=-=) xs ys = toList xs `eq` toList ys
listCheck :: IO ()
listCheck = quickBatch $ traversable (undefined :: List (Int, Int, [Int]))
--
data Three a b c = Three a b c deriving (Eq, Show)
instance Functor (Three a b) where
fmap f (Three a b c) = Three a b (f c)
instance Foldable (Three a b) where
foldr f z (Three _ _ c) = f c z
instance Traversable (Three a b) where
traverse f (Three a b c) = Three a b <$> f c
instance (Arbitrary a, Arbitrary b, Arbitrary c) => Arbitrary (Three a b c) where
arbitrary = do
a <- arbitrary
b <- arbitrary
c <- arbitrary
return $ Three a b c
instance (Eq a, Eq b, Eq c) => EqProp (Three a b c) where
(=-=) = eq
threeCheck :: IO ()
threeCheck = quickBatch $ traversable (undefined :: Three Int Int (Int, Int, [Int]))
--
data Three' a b = Three' a b b deriving (Eq, Show)
instance Functor (Three' a) where
fmap f (Three' a b1 b2) = Three' a (f b1) (f b2)
instance Foldable (Three' a) where
foldr f z (Three' _ b1 b2) = f b1 $ f b2 z
instance Traversable (Three' a) where
traverse f (Three' a b1 b2) = Three' a <$> f b1 <*> f b2
instance (Arbitrary a, Arbitrary b) => Arbitrary (Three' a b) where
arbitrary = do
a <- arbitrary
b1 <- arbitrary
b2 <- arbitrary
return $ Three' a b1 b2
instance (Eq a, Eq b) => EqProp (Three' a b) where
(=-=) = eq
three'Check :: IO ()
three'Check = quickBatch $ traversable (undefined :: Three' Int (Int, Int, [Int]))
--
data S n a = S (n a) a deriving (Eq, Show)
instance (Functor n) => Functor (S n) where
fmap f (S na a) = S (fmap f na) (f a)
instance (Foldable n) => Foldable (S n) where
foldr f z (S na a) = foldr f (f a z) na
instance (Traversable n) => Traversable (S n) where
traverse f (S na a) = S <$> traverse f na <*> f a
instance (Applicative n, Arbitrary a) => Arbitrary (S n a) where
arbitrary = do
a <- arbitrary
return $ S (pure a) a
instance (Foldable n, Eq a) => EqProp (S n a) where
(=-=) (S na1 a1) (S na2 a2) = (a1 `eq` a2) .&&. (toList na1 `eq` toList na2)
sCheck :: IO ()
sCheck = quickBatch $ traversable (undefined :: S [] (Int, Int, [Int]))
--
data Tree a =
Empty
| Leaf a
| Node (Tree a) (Tree a)
deriving (Eq, Show)
instance Functor Tree where
fmap _ Empty = Empty
fmap f (Leaf a) = Leaf (f a)
fmap f (Node l r) = Node (fmap f l) (fmap f r)
instance Foldable Tree where
foldMap _ Empty = mempty
foldMap f (Leaf a) = f a
foldMap f (Node l r) = foldMap f l `mappend` foldMap f r
instance Traversable Tree where
traverse _ Empty = pure Empty
traverse f (Leaf a) = Leaf <$> f a
traverse f (Node l r) = Node <$> traverse f l <*> traverse f r
instance (Arbitrary a) => Arbitrary (Tree a) where
arbitrary = oneof
[ return Empty
, fmap Leaf arbitrary
, do
l <- arbitrary
r <- arbitrary
return $ Node l r
]
instance (Eq a) => EqProp (Tree a) where
(=-=) = eq
treeCheck :: IO ()
treeCheck = quickBatch $ traversable (undefined :: Tree (Int, Int, [Int]))