-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStream.hs
223 lines (176 loc) · 6.17 KB
/
Stream.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
{-# LANGUAGE RankNTypes #-}
module Stream where
import Prelude
import Control.Monad (join)
newtype Stream m a =
Stream {
unStream :: forall r. m r -> (a -> Stream m a -> m r) -> m r
}
sNil :: Stream m a
sNil = Stream $ \inNil _ -> inNil
sCons :: a -> Stream m a -> Stream m a
sCons x xs = Stream $ \_ inCons -> inCons x xs
sConsM :: Monad m => m a -> Stream m a -> Stream m a
sConsM mx xs = Stream $ \_ inCons -> join $ inCons <$> mx <*> pure xs
(|:) :: Monad m => m a -> Stream m a -> Stream m a
(|:) = sConsM
infixr 5 |:
sCons' :: a -> Stream m a
sCons' x = sCons x sNil
sConsM' :: Monad m => m a -> Stream m a
sConsM' mx = sConsM mx sNil
instance Semigroup (Stream m a) where
x <> y =
Stream $ \inNil inCons ->
unStream x (unStream y inNil inCons) $ \xv xs ->
inCons xv (xs <> y)
instance Monoid (Stream m a) where
mempty = sNil
instance Functor (Stream m) where
fmap f x =
Stream $ \inNil inCons ->
unStream x inNil $ \xv xs ->
inCons (f xv) (fmap f xs)
instance Applicative (Stream m) where
pure x = sCons x sNil
f <*> x =
Stream $ \inNil inCons ->
unStream f inNil $ \fv fs ->
unStream (fmap fv x <> (fs <*> x)) inNil inCons
instance Monad (Stream m) where
x >>= f =
Stream $ \inNil inCons ->
unStream x inNil $ \xv xs ->
unStream (f xv <> (xs >>= f)) inNil inCons
sRun :: Stream IO () -> IO ()
sRun x = unStream x (return ()) (\_ xs -> sRun xs)
newtype Iteratee s m a =
Iteratee {
unIteratee :: forall r.
m r ->
(a -> Iteratee s m a -> m r) ->
((s -> Iteratee s m a) -> m r) ->
m r
}
iDone :: Iteratee s m a
iDone = Iteratee $ \done _ _ -> done
iYield :: a -> Iteratee s m a -> Iteratee s m a
iYield xv xs = Iteratee $ \_ yield _ -> yield xv xs
iAwait :: (s -> Iteratee s m a) -> Iteratee s m a
iAwait xw = Iteratee $ \_ _ await -> await xw
iYieldM :: Monad m => m a -> Iteratee s m a -> Iteratee s m a
iYieldM mxv xs = Iteratee $ \_ yield _ -> join $ yield <$> mxv <*> pure xs
iMapSrc :: (s -> t) -> Iteratee t m a -> Iteratee s m a
iMapSrc f x =
Iteratee $ \done yield await ->
unIteratee x
done
(\xv xs -> yield xv (iMapSrc f xs))
(\xw -> await (\s -> iMapSrc f (xw (f s))))
instance Semigroup (Iteratee s m a) where
x <> y =
Iteratee $ \done yield await ->
unIteratee x
(unIteratee y done yield await)
(\xv xs -> yield xv (xs <> y))
(\xw -> await (\s -> xw s <> y))
instance Monoid (Iteratee s m a) where
mempty = iDone
instance Functor (Iteratee s m) where
fmap f x =
Iteratee $ \done yield await ->
unIteratee x
done
(\xv xs -> yield (f xv) (fmap f xs))
(\xw -> await (\s -> fmap f (xw s)))
instance Applicative (Iteratee s m) where
pure x = iYield x iDone
f <*> x =
Iteratee $ \done yield await ->
unIteratee f
done
(\fv fs -> unIteratee (fmap fv x <> (fs <*> x)) done yield await)
(\fw -> await (\s -> fw s <*> x))
instance Monad (Iteratee s m) where
x >>= f =
Iteratee $ \done yield await ->
unIteratee x
done
(\xv xs -> unIteratee (f xv <> (xs >>= f)) done yield await)
(\xw -> await (\s -> xw s >>= f))
iCompose :: Iteratee a m b -> Iteratee b m c -> Iteratee a m c
iCompose x y =
Iteratee $ \done yield await ->
unIteratee y
done
(\yv ys -> yield yv (iCompose x ys))
(\yw -> unIteratee (iCompose' x yw) done yield await)
iCompose' :: Iteratee a m b -> (b -> Iteratee b m c) -> Iteratee a m c
iCompose' x yw =
Iteratee $ \done yield await ->
unIteratee x
done
(\xv xs -> unIteratee (iCompose xs (yw xv)) done yield await)
(\xw -> await (\s -> iCompose' (xw s) yw))
iRun :: Iteratee () IO () -> IO ()
iRun x = unIteratee x (return ()) (\_ xs -> iRun xs) (\xw -> iRun (xw ()))
newtype Conduit i o u m a =
Conduit {
unConduit :: forall r.
(a -> m r) ->
(o -> Conduit i o u m a -> m r) ->
((i -> Conduit i o u m a) -> (u -> Conduit i o u m a) -> m r) ->
m r
}
cDone :: a -> Conduit i o u m a
cDone xv = Conduit $ \done _ _ -> done xv
cYield :: o -> Conduit i o u m a -> Conduit i o u m a
cYield xo xs = Conduit $ \_ yield _ -> yield xo xs
cAwait :: (i -> Conduit i o u m a) -> (u -> Conduit i o u m a) -> Conduit i o u m a
cAwait xp xc = Conduit $ \_ _ await -> await xp xc
cYieldM :: Monad m => m o -> Conduit i o u m a -> Conduit i o u m a
cYieldM mxo xs = Conduit $ \_ yield _ -> join $ yield <$> mxo <*> pure xs
cYield' :: o -> Conduit i o u m ()
cYield' xo = cYield xo (cDone ())
cAwait' :: (i -> Conduit i o () m ()) -> Conduit i o () m ()
cAwait' xp = cAwait xp (\_ -> cDone ())
cYieldM' :: Monad m => m o -> Conduit i o u m ()
cYieldM' mxo = cYieldM mxo (cDone ())
instance Functor (Conduit i o u m) where
fmap f x =
Conduit $ \done yield await ->
unConduit x
(\xv -> done (f xv))
(\xo xs -> yield xo (fmap f xs))
(\xp xc -> await (\i -> fmap f (xp i)) (\u -> fmap f (xc u)))
instance Applicative (Conduit i o u m) where
pure x = cDone x
f <*> x =
Conduit $ \done yield await ->
unConduit f
(\fv -> unConduit (fmap fv x) done yield await)
(\fo fs -> yield fo (fs <*> x))
(\fp fc -> await (\i -> fp i <*> x) (\u -> fc u <*> x))
instance Monad (Conduit i o u m) where
x >>= f =
Conduit $ \done yield await ->
unConduit x
(\xv -> unConduit (f xv) done yield await)
(\xo xs -> yield xo (xs >>= f))
(\xp xc -> await (\i -> xp i >>= f) (\i -> xc i >>= f))
cCompose :: Conduit a c b m d -> Conduit c e d m f -> Conduit a e b m f
cCompose x y =
Conduit $ \done yield await ->
unConduit y
(\yv -> done yv)
(\yo ys -> yield yo (cCompose x ys))
(\yp yc -> unConduit (cCompose' x yp yc) done yield await)
cCompose' :: Conduit a c b m d -> (c -> Conduit c e d m f) -> (d -> Conduit c e d m f) -> Conduit a e b m f
cCompose' x yp yc =
Conduit $ \done yield await ->
unConduit x
(\xv -> unConduit (cCompose (cDone xv) (yc xv)) done yield await)
(\xo xs -> unConduit (cCompose xs (yp xo)) done yield await)
(\xp xc -> await (\i -> cCompose' (xp i) yp yc) (\u -> cCompose' (xc u) yp yc))
cRun :: Conduit () () () IO () -> IO ()
cRun x = unConduit x (\xv -> return xv) (\_ xs -> cRun xs) (\xp _ -> cRun (xp ()))