-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh18.hs
More file actions
139 lines (105 loc) · 2.85 KB
/
Ch18.hs
File metadata and controls
139 lines (105 loc) · 2.85 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
module Ch18 where
import Control.Monad (join, ap)
bind :: Monad m => m a -> (a -> m b) -> m b
bind x f = join $ fmap f x
twiceWhenEven :: [Integer] -> [Integer]
twiceWhenEven xs = do
x <- xs
if even x
then [x*x,x*x]
else []
data Sum a b = Fst a
| Snd b
deriving (Eq, Show)
instance Functor (Sum a) where
fmap f (Fst a) = Fst a
fmap f (Snd b) = Snd (f b)
instance Applicative (Sum a) where
pure x = Snd x
Fst f <*> Fst x = Fst x
Snd f <*> Snd x = Snd $ f x
Snd f <*> Fst x = Fst x
Fst f <*> Snd x = Fst f
instance Monad (Sum a) where
return = pure
Fst x >>= f = Fst x
Snd x >>= f = f x
data Nope a = NopeDotJpg
instance Functor Nope where
fmap _ _ = NopeDotJpg
instance Applicative Nope where
pure _ = NopeDotJpg
_ <*> _ = NopeDotJpg
instance Monad Nope where
return _ = NopeDotJpg
_ >>= _ = NopeDotJpg
data PhbtEither b a = L a
| R b
instance Functor (PhbtEither b) where
fmap f (L x) = L $ f x
fmap _ (R x) = R x
instance Applicative (PhbtEither b) where
pure x = L x
L f <*> L x = L $ f x
L f <*> R x = R x
R f <*> R x = R x
R f <*> L x = R f
instance Monad (PhbtEither b) where
return = pure
L x >>= f = f x
R x >>= f = R x
data Identity a = Identity a
deriving (Eq, Show, Ord)
instance Functor Identity where
fmap f (Identity x) = Identity $ f x
instance Applicative Identity where
pure = Identity
Identity f <*> Identity x = Identity $ f x
instance Monad Identity where
return = pure
Identity x >>= f = f x
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
instance Applicative List where
pure x = Cons x Nil
_ <*> Nil = Nil
Nil <*> _ = Nil
Cons f fs <*> Cons x xs = Cons (f x) (app (fmap f xs) (fs <*> xs))
-- fs <*> xs = tolist [f x | f <- fs, x <- xs]
app :: List a -> List a -> List a
app xs Nil = xs
app Nil ys = ys
app (Cons x xs) ys = Cons x (app xs ys)
tolist :: [a] -> List a
tolist [] = Nil
tolist (x:xs) = Cons x $ tolist xs
instance Monad List where
return = pure
Nil >>= f = Nil
xs >>= f = lconcat $ fmap f xs
lconcat :: List (List a) -> List a
lconcat Nil = Nil
lconcat (Cons x xs) = app x (lconcat xs)
joinm :: Monad m => m (m a) -> m a
joinm ms = ms >>= id
-- do s <- ms
-- s
-- do s <- ms
-- s' <- s
-- return s'
l1 :: Monad m => (a -> b) -> m a -> m b
l1 f mx = mx >>= return . f
-- l1 f mx = mx >>= (\x -> return $ f x)
l2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
l2 f mx my = mx >>= (\x -> my >>= (\y -> return $ f x y))
am :: Monad m => m a -> m (a -> b) -> m b
am ma f = f <*> ma
meh :: Monad m => [a] -> (a -> m b) -> m [b]
meh [] f = return []
meh (x:xs) f = do r <- f x
rs <- meh xs f
return (r:rs)
flipType :: Monad m => [m a] -> m [a]
flipType xs = meh xs id