-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh16.hs
More file actions
280 lines (189 loc) · 6.28 KB
/
Ch16.hs
File metadata and controls
280 lines (189 loc) · 6.28 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
-- the flag is used for higher-kinded types in Nat
module Ch16 where
import GHC.Arr
import Test.QuickCheck
import Test.QuickCheck.Function
-- class Functor f where
-- fmap :: (a -> b) -> f a -> f b
-- f has kind: * -> *
-- fmap operator: <$>
-- because at the end of the day fmap is a kind of func app.
-- laws:
-- identity
-- fmap id = id
-- composition
-- fmap (f . g) = fmap f . fmap g
-- g is a function
-- fmap f g = f . g
-- you can stack fmaps if you're stacking data constructors
-- by composing them, i.e., fmap . fmap . fmap . ...
a :: [Int]
a = fmap (+1) $ read "[1]"
b :: Maybe [String]
b = (fmap . fmap) (++ "lol") (Just ["Hi,", "Hello"])
c :: Int -> Int
c = (*2) . (\x -> x - 2)
d :: Int -> String
d = fmap ((return '1' ++ ) . show) (\x -> [x, 1..3])
-- e :: IO Integer
e = let ioi = readIO "1" :: IO Integer
changed :: IO Integer
-- changed = fmap read $ (fmap ("123" ++) $ fmap show ioi)
changed = fmap read $ fmap (("123" ++) . show) ioi
in fmap (*3) changed
-- the original:
-- e = let ioi = readIO "1" :: IO Integer
-- changed = read ("123" ++) show ioi
-- in (*3) changed
functorIdentity :: (Functor f, Eq (f a)) => f a -> Bool
functorIdentity f = fmap id f == f
-- how to test:
-- *Ch16> :{
-- *Ch16| let f :: [Int] -> Bool
-- *Ch16| f x = functorIdentity x
-- *Ch16| :}
-- *Ch16> quickCheck f
functorCompose :: (Functor f, Eq (f c)) =>
(a -> b) -> (b -> c) -> f a -> Bool
functorCompose f g x = (fmap g (fmap f x)) == (fmap (g . f) x)
-- how to test:
-- *Ch16> let c = functorCompose (+1) (*2)
-- *Ch16> let li x = c (x :: [Int])
-- *Ch16> quickCheck li
functorCompose' :: (Functor f, Eq (f c)) =>
f a -> Fun a b -> Fun b c -> Bool
functorCompose' x (Fun _ f) (Fun _ g) =
(fmap (g . f) x) == (fmap g . fmap f $ x)
-- how to test:
type IntToInt = Fun Int Int
type IntFC = [Int] -> IntToInt -> IntToInt -> Bool
-- *Ch16> quickCheck (fc' :: In
-- *Ch16> quickCheck (fc' :: IntFC )
-- impl functor inst for following:
-- use quickcheck properties to validate them:
newtype Identity a = Identity a
instance Functor Identity where
fmap f (Identity a) = Identity $ f a
data Pair a = Pair a a
instance Functor Pair where
fmap f (Pair x y) = Pair (f x) (f y)
data Two a b = Two a b
instance Functor (Two a) where
fmap f (Two a b) = Two a (f b)
data Three a b c = Three a b c
instance Functor (Three a b) where
fmap f (Three a b c) = Three a b $ f c
data Three' a b = Three' a b b
instance Functor (Three' a) where
fmap f (Three' a b b') = Three' a (f b) (f b')
data Four a b c d = Four a b c d
deriving (Eq, Show)
instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d) =>
Arbitrary (Four a b c d) where
arbitrary = do
a <- arbitrary
b <- arbitrary
c <- arbitrary
d <- arbitrary
return (Four a b c d)
-- *Ch16> :{
-- *Ch16| let ff :: Four Int Bool String Int -> Bool
-- *Ch16| ff x = functorIdentity x
-- *Ch16| :}
-- *Ch16> quickCheck ff
-- +++ OK, passed 100 tests.
instance Functor (Four a b c) where
fmap f (Four a b c d) = Four a b c (f d)
data Four' a b = Four' a a a b
instance Functor (Four' a) where
fmap f (Four' x y z b) = Four' x y z $ f b
data Possibly a = LolNope
| Yeppers a
deriving (Eq, Show)
instance Functor Possibly where
fmap f (Yeppers a) = Yeppers $ f a
fmap _ LolNope = LolNope
data Sum a b = First a
| Second b
deriving (Eq, Show)
instance Functor (Sum a) where
fmap = sumMap
sumMap :: (b -> c) -> Sum a b -> Sum a c
sumMap f (Second b) = Second $ f b
sumMap _ (First a) = First a
-- newtype Constant a b = Constant {getConstant :: a}
-- deriving (Eq, Show)
-- instance Functor (Constant m) where
-- fmap _ (Constant v) = Constant v
data Wrap f a = Wrap (f a)
deriving (Eq, Show)
instance Functor f => Functor (Wrap f) where
fmap f (Wrap fa) = Wrap (fmap f fa)
-- fmap (+1) (Wrap (Just 1)) ==> Wrap (Just 2)
-- if we don't want to change the values in a structure and just
-- change the structure then we basically want natural
-- transformation. which can be simulated by the following:
-- in natural trans we care as much about what we don't want as we
-- care about what we do want. we'll get to this in applicatives.
type Nat f g = forall a . f a -> g a
maybeToList :: Nat Maybe []
maybeToList Nothing = []
maybeToList (Just a) = [a]
-- if you really want to get around the fact that functors are
-- unique to a data type:
-- however flip tup a b is different than tup a b even if it's
-- only there to provide for different functor behavior!
data Tup a b = Tup a b
deriving (Eq, Show)
newtype Flip f a b = Flip (f b a)
deriving (Eq, Show)
instance Functor (Flip Tup a) where
fmap f (Flip (Tup a b)) = Flip $ Tup (f a) b
newtype Mu f = InF {outF :: f (Mu f)}
data D = D (Array Word Word) Int Int
data Quant a b = Finance
| Desk a
| Bloor b
instance Functor (Quant a) where
fmap f (Bloor x) = Bloor $ f x
fmap _ (Desk x) = Desk x
fmap _ Finance = Finance
data K a b = K a
instance Functor (K a) where
fmap _ (K x) = K x
-- newtype Flip f a b = Flip (f b a)
-- deriving (Eq, Show)
instance Functor (Flip K a) where
fmap f (Flip (K x)) = Flip (K (f x))
data LiftItOut f a = LiftItOut (f a)
-- instance Functor (LiftItOut f) where
-- fmap g (LiftItOut x) = LiftItOut $ g x
data Parappa f g a = DaWrappa (f a) (g a)
instance Functor (Parappa f g) where
fmap = undefined
data IgnoreOne f g a b = IgnoringSth (f a) (g b)
instance Functor (IgnoreOne f g a) where
fmap = undefined
data Notorious g o a t = Notorious (g o) (g a) (g t)
instance Functor (Notorious g o a) where
fmap = undefined
data List a = Nil | Cons a (List a)
instance Functor List where
fmap f Nil = Nil
fmap f (Cons x l) = Cons (f x) $ fmap f l
data GoatLord a = NoGoat
| OneGoat a
| MoreGoats (GoatLord a) (GoatLord a) (GoatLord a)
instance Functor GoatLord where
fmap _ NoGoat = NoGoat
fmap f (OneGoat x) = OneGoat $ f x
fmap f (MoreGoats x y z) = MoreGoats (fmap f x) (fmap f y) (fmap f z)
data TalkToMe a = Halt
| Print String a
| Read (String -> a)
instance Functor TalkToMe where
fmap _ Halt = Halt
fmap f (Print s x) = Print s $ f x
fmap f (Read g) = Read $ (f . g)