summaryrefslogtreecommitdiff
path: root/Haskell-book/21/instances/src/Lib.hs
blob: 1fbb25b5cb79d751cb426e701fb0d0a687b802e4 (plain)
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
module Lib where

import Data.Monoid
import Data.Traversable
import Test.QuickCheck
import Test.QuickCheck.Checkers

--
-- Identity
--
newtype Identity a = Identity a
    deriving (Eq, Ord, Show)

instance Functor Identity where
    fmap f (Identity x) = Identity $ f x

instance Applicative Identity where
    pure = Identity
    (Identity f) <*> x = fmap f x

instance Foldable Identity where
    foldr f acc (Identity x) = f x acc

instance Arbitrary a => Arbitrary (Identity a) where
    arbitrary = fmap Identity $ arbitrary

instance Eq a => EqProp (Identity a) where
    (=-=) = eq

instance Traversable Identity where
    traverse f (Identity x) = Identity <$> f x

--
-- Constant
--
newtype Constant a b = Constant { getConstant :: a }
    deriving (Eq, Ord, Show)

instance Functor (Constant a) where
    fmap _ (Constant x) = Constant x

instance Monoid a => Applicative (Constant a) where
    pure _ = Constant mempty
    (Constant x) <*> (Constant y) = Constant $ mappend x y

instance (Arbitrary a, Arbitrary b) => Arbitrary (Constant a b) where
    arbitrary = do
        x <- arbitrary
        return $ Constant x

instance (Eq a, Eq b) => EqProp (Constant a  b) where
    (=-=) = eq

instance Foldable (Constant a) where
    foldMap _ (Constant _) = mempty

instance Traversable (Constant a) where
    traverse f (Constant x) = pure $ Constant x

--
-- Maybe
--
data Optional a = Nada | Yep a deriving (Show, Eq, Ord)

instance Monoid a => Monoid (Optional a) where
    mempty = Nada
    mappend Nada _ = Nada
    mappend _ Nada = Nada
    mappend (Yep x) (Yep y) = Yep $ mappend x y

instance Applicative Optional where
    pure = Yep
    (Yep f) <*> (Yep x) = Yep $ f x
    Nada <*> (Yep x) = Nada
    _ <*> Nada = Nada

instance Functor Optional where
    fmap _ Nada = Nada
    fmap f (Yep x) = Yep $ f x

instance Foldable Optional where
    foldr f acc Nada = acc
    foldr f acc (Yep x) = f x acc

instance Traversable Optional where
    traverse f Nada = pure Nada
    traverse f (Yep x) = Yep <$> f x

instance (CoArbitrary a, Arbitrary a) => Arbitrary (Optional a) where
    arbitrary = do
        x <- arbitrary
        frequency [ (1, return Nada)
                  , (2, return $ Yep x)
                  ]

instance (Eq a) => EqProp (Optional a) where
    (=-=) = eq

--
-- List
--
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 _ acc Nil = acc
    foldr f acc (Cons x xs) = f x (foldr f acc xs)

instance Traversable List where
    sequenceA Nil = pure Nil
    sequenceA (Cons x xs) = Cons <$> x <*> (sequenceA xs)

instance Arbitrary a => Arbitrary (List a) where
    arbitrary = sized go
        where go 0 = pure Nil
              go n = do
                  xs <- go (n - 1)
                  x <- arbitrary
                  return $ Cons x xs

instance (Eq a) => EqProp (List a) where
    (=-=) = eq

--
-- Three
--
data Three a b c = Three a b c deriving (Show, Eq)

instance Functor (Three a b) where
    fmap f (Three x y z) = Three x y (f z)

instance Foldable (Three a b) where
    foldr f acc (Three x y z) = (f z acc)

instance Traversable (Three a b) where
    traverse f (Three x y z) = fmap (Three x y) (f z)

instance (Arbitrary a, Arbitrary b, Arbitrary c) => Arbitrary (Three a b c) where
    arbitrary = do
        x <- arbitrary
        y <- arbitrary
        z <- arbitrary
        return $ Three x y z

instance (Eq a, Eq b, Eq c) => EqProp (Three a b c) where
    (=-=) = eq

--
-- Pair
--
data Pair a b = Pair a b deriving (Eq, Show)

instance Functor (Pair a) where
    fmap f (Pair x y) = Pair x (f y)

instance Foldable (Pair a) where
    foldr f acc (Pair x y) = (f y acc)

instance Traversable (Pair a) where
    traverse f (Pair x y) = fmap (Pair x) (f y)

instance (Arbitrary a, Arbitrary b) => Arbitrary (Pair a b) where
    arbitrary = do
        x <- arbitrary
        y <- arbitrary
        return $ Pair x y

instance (Eq a, Eq b) => EqProp (Pair a b) where
    (=-=) = eq

--
-- Big
--
data Big a b = Big a b b deriving (Eq, Show)

instance Functor (Big a) where
    fmap f (Big x y z) = Big x (f y) (f z)

instance Foldable (Big a) where
    foldr f acc (Big _ y z) = (f y (f z acc)) 

instance Traversable (Big a) where
    traverse f (Big x y z) = (Big x) <$> (f y) <*> (f z)

instance (Arbitrary a, Arbitrary b) => Arbitrary (Big a b) where
    arbitrary = do
        x <- arbitrary
        y <- arbitrary
        z <- arbitrary
        return $ Big x y z

instance (Eq a, Eq b) => EqProp (Big a b) where
    (=-=) = eq

--
-- Bigger
--
data Bigger a b = Bigger a b b b deriving (Eq, Show)

instance Functor (Bigger a) where
    fmap f (Bigger x y z t) = Bigger x (f y) (f z) (f t)

instance Foldable (Bigger a) where
    foldr f acc (Bigger _ y z t) = f y $ f z $ f t acc

instance Traversable (Bigger a) where
    traverse f (Bigger x y z t) = (Bigger x) <$> (f y) <*> (f z) <*> (f t)

instance (Arbitrary a, Arbitrary b) => Arbitrary (Bigger a b) where
    arbitrary = do
        x <- arbitrary
        y <- arbitrary
        z <- arbitrary
        t <- arbitrary
        return $ Bigger x y z t

instance (Eq a, Eq b) => EqProp (Bigger a b) where
    (=-=) = eq