cd77330815cd97c13383efe742040c6a9c191bd7
[darcs-mirrors/vector.git] / Data / Vector / Stream.hs
1 {-# LANGUAGE ExistentialQuantification #-}
2
3 -- |
4 -- Module : Data.Vector.Stream.Size
5 -- Copyright : (c) Roman Leshchinskiy 2008
6 -- License : BSD-style
7 --
8 -- Maintainer : rl@cse.unsw.edu.au
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- Fusible streams
13 --
14
15 #include "phases.h"
16
17 module Data.Vector.Stream (
18 -- * Types
19 Step(..), Stream(..),
20
21 -- * Size hints
22 size, sized,
23
24 -- * Length information
25 length, null,
26
27 -- * Construction
28 empty, singleton, cons, snoc, replicate, (++),
29
30 -- * Accessing individual elements
31 head, last, (!!),
32
33 -- * Substreams
34 init, tail, take, drop,
35
36 -- * Mapping and zipping
37 map, zipWith,
38
39 -- * Filtering
40 filter, takeWhile, dropWhile,
41
42 -- * Searching
43 elem, notElem, find, findIndex,
44
45 -- * Folding
46 foldl, foldl1, foldl', foldl1', foldr, foldr1,
47
48 -- * Unfolding
49 unfold,
50
51 -- * Conversion to/from lists
52 toList, fromList,
53
54 -- * Monadic combinators
55 mapM_, foldM
56 ) where
57
58 import Data.Vector.Stream.Size
59
60 import Prelude hiding ( length, null,
61 replicate, (++),
62 head, last, (!!),
63 init, tail, take, drop,
64 map, zipWith,
65 filter, takeWhile, dropWhile,
66 elem, notElem,
67 foldl, foldl1, foldr, foldr1,
68 mapM_ )
69
70 data Step s a = Yield a s
71 | Skip s
72 | Done
73
74 -- | The type of fusible streams
75 data Stream a = forall s. Stream (s -> Step s a) s Size
76
77 -- | 'Size' hint of a 'Stream'
78 size :: Stream a -> Size
79 {-# INLINE size #-}
80 size (Stream _ _ sz) = sz
81
82 -- | Attach a 'Size' hint to a 'Stream'
83 sized :: Stream a -> Size -> Stream a
84 {-# INLINE_STREAM sized #-}
85 sized (Stream step s _) sz = Stream step s sz
86
87 -- | Unfold
88 unfold :: (s -> Maybe (a, s)) -> s -> Stream a
89 {-# INLINE_STREAM unfold #-}
90 unfold f s = Stream step s Unknown
91 where
92 {-# INLINE step #-}
93 step s = case f s of
94 Just (x, s') -> Yield x s'
95 Nothing -> Done
96
97 -- | Convert a 'Stream' to a list
98 toList :: Stream a -> [a]
99 {-# INLINE toList #-}
100 toList s = foldr (:) [] s
101
102 -- | Create a 'Stream' from a list
103 fromList :: [a] -> Stream a
104 {-# INLINE_STREAM fromList #-}
105 fromList xs = Stream step xs Unknown
106 where
107 step (x:xs) = Yield x xs
108 step [] = Done
109
110 -- Length
111 -- ------
112
113 -- | Length of a 'Stream'
114 length :: Stream a -> Int
115 {-# INLINE_STREAM length #-}
116 length s = foldl' (\n _ -> n+1) 0 s
117
118 -- | Check if a 'Stream' is empty
119 null :: Stream a -> Bool
120 {-# INLINE_STREAM null #-}
121 null s = foldr (\_ _ -> False) True s
122
123 -- Construction
124 -- ------------
125
126 -- | Empty 'Stream'
127 empty :: Stream a
128 {-# INLINE_STREAM empty #-}
129 empty = Stream (const Done) () (Exact 0)
130
131 -- | Singleton 'Stream'
132 singleton :: a -> Stream a
133 {-# INLINE_STREAM singleton #-}
134 singleton x = Stream step True (Exact 1)
135 where
136 {-# INLINE step #-}
137 step True = Yield x False
138 step False = Done
139
140 -- | Replicate a value to a given length
141 replicate :: Int -> a -> Stream a
142 {-# INLINE_STREAM replicate #-}
143 replicate n x = Stream step n (Exact (max n 0))
144 where
145 {-# INLINE step #-}
146 step i | i > 0 = Yield x (i-1)
147 | otherwise = Done
148
149 -- | Prepend an element
150 cons :: a -> Stream a -> Stream a
151 {-# INLINE cons #-}
152 cons x s = singleton x ++ s
153
154 -- | Append an element
155 snoc :: Stream a -> a -> Stream a
156 {-# INLINE snoc #-}
157 snoc s x = s ++ singleton x
158
159 infixr 5 ++
160 -- | Concatenate two 'Stream's
161 (++) :: Stream a -> Stream a -> Stream a
162 {-# INLINE_STREAM (++) #-}
163 Stream stepa sa na ++ Stream stepb sb nb = Stream step (Left sa) (na + nb)
164 where
165 step (Left sa) = case stepa sa of
166 Yield x sa' -> Yield x (Left sa')
167 Skip sa' -> Skip (Left sa')
168 Done -> Skip (Right sb)
169 step (Right sb) = case stepb sb of
170 Yield x sb' -> Yield x (Right sb')
171 Skip sb' -> Skip (Right sb')
172 Done -> Done
173
174 -- Accessing elements
175 -- ------------------
176
177 -- | First element of the 'Stream' or error if empty
178 head :: Stream a -> a
179 {-# INLINE_STREAM head #-}
180 head (Stream step s _) = head_loop s
181 where
182 head_loop s = case step s of
183 Yield x _ -> x
184 Skip s' -> head_loop s'
185 Done -> error "Data.Vector.Stream.head: empty stream"
186
187 -- | Last element of the 'Stream' or error if empty
188 last :: Stream a -> a
189 {-# INLINE_STREAM last #-}
190 last (Stream step s _) = last_loop0 s
191 where
192 last_loop0 s = case step s of
193 Yield x s' -> last_loop1 x s'
194 Skip s' -> last_loop0 s'
195 Done -> error "Data.Vector.Stream.last: empty stream"
196
197 last_loop1 x s = case step s of
198 Yield y s' -> last_loop1 y s'
199 Skip s' -> last_loop1 x s'
200 Done -> x
201
202 -- | Element at the given position
203 (!!) :: Stream a -> Int -> a
204 {-# INLINE (!!) #-}
205 s !! i = head (drop i s)
206
207 -- Substreams
208 -- ----------
209
210 -- | All but the last element
211 init :: Stream a -> Stream a
212 {-# INLINE_STREAM init #-}
213 init (Stream step s sz) = Stream step' (Nothing, s) (sz - 1)
214 where
215 {-# INLINE step' #-}
216 step' (Nothing, s) = case step s of
217 Yield x s' -> Skip (Just x, s')
218 Skip s' -> Skip (Nothing, s')
219 Done -> Done -- FIXME: should be an error
220
221 step' (Just x, s) = case step s of
222 Yield y s' -> Yield x (Just y, s')
223 Skip s' -> Skip (Just x, s')
224 Done -> Done
225
226 -- | All but the first element
227 tail :: Stream a -> Stream a
228 {-# INLINE_STREAM tail #-}
229 tail (Stream step s sz) = Stream step' (Left s) (sz - 1)
230 where
231 {-# INLINE step' #-}
232 step' (Left s) = case step s of
233 Yield x s' -> Skip (Right s')
234 Skip s' -> Skip (Left s')
235 Done -> Done -- FIXME: should be error?
236
237 step' (Right s) = case step s of
238 Yield x s' -> Yield x (Right s')
239 Skip s' -> Skip (Right s')
240 Done -> Done
241
242 -- | The first @n@ elements
243 take :: Int -> Stream a -> Stream a
244 {-# INLINE_STREAM take #-}
245 take n (Stream step s sz) = Stream step' (s, 0) (smaller (Exact n) sz)
246 where
247 {-# INLINE step' #-}
248 step' (s, i) | i < n = case step s of
249 Yield x s' -> Yield x (s', i+1)
250 Skip s' -> Skip (s', i)
251 Done -> Done
252 step' (s, i) = Done
253
254 data Drop s = Drop_Drop s Int | Drop_Keep s
255
256 -- | All but the first @n@ elements
257 drop :: Int -> Stream a -> Stream a
258 {-# INLINE_STREAM drop #-}
259 drop n (Stream step s sz) = Stream step' (Drop_Drop s 0) (sz - Exact n)
260 where
261 {-# INLINE step' #-}
262 step' (Drop_Drop s i) | i < n = case step s of
263 Yield x s' -> Skip (Drop_Drop s' (i+1))
264 Skip s' -> Skip (Drop_Drop s' i)
265 Done -> Done
266 | otherwise = Skip (Drop_Keep s)
267
268 step' (Drop_Keep s) = case step s of
269 Yield x s' -> Yield x (Drop_Keep s')
270 Skip s' -> Skip (Drop_Keep s')
271 Done -> Done
272
273
274 -- Mapping/zipping
275 -- ---------------
276
277 instance Functor Stream where
278 {-# INLINE_STREAM fmap #-}
279 fmap = map
280
281 -- | Map a function over a 'Stream'
282 map :: (a -> b) -> Stream a -> Stream b
283 {-# INLINE_STREAM map #-}
284 map f (Stream step s n) = Stream step' s n
285 where
286 {-# INLINE step' #-}
287 step' s = case step s of
288 Yield x s' -> Yield (f x) s'
289 Skip s' -> Skip s'
290 Done -> Done
291
292 -- | Zip two 'Stream's with the given function
293 zipWith :: (a -> b -> c) -> Stream a -> Stream b -> Stream c
294 {-# INLINE_STREAM zipWith #-}
295 zipWith f (Stream stepa sa na) (Stream stepb sb nb)
296 = Stream step (sa, sb, Nothing) (smaller na nb)
297 where
298 {-# INLINE step #-}
299 step (sa, sb, Nothing) = case stepa sa of
300 Yield x sa' -> Skip (sa', sb, Just x)
301 Skip sa' -> Skip (sa', sb, Nothing)
302 Done -> Done
303
304 step (sa, sb, Just x) = case stepb sb of
305 Yield y sb' -> Yield (f x y) (sa, sb', Nothing)
306 Skip sb' -> Skip (sa, sb', Just x)
307 Done -> Done
308
309 -- Filtering
310 -- ---------
311
312 -- | Drop elements which do not satisfy the predicate
313 filter :: (a -> Bool) -> Stream a -> Stream a
314 {-# INLINE_STREAM filter #-}
315 filter f (Stream step s n) = Stream step' s (toMax n)
316 where
317 {-# INLINE step' #-}
318 step' s = case step s of
319 Yield x s' | f x -> Yield x s'
320 | otherwise -> Skip s'
321 Skip s' -> Skip s'
322 Done -> Done
323
324 -- | Longest prefix of elements that satisfy the predicate
325 takeWhile :: (a -> Bool) -> Stream a -> Stream a
326 {-# INLINE_STREAM takeWhile #-}
327 takeWhile f (Stream step s n) = Stream step' s (toMax n)
328 where
329 {-# INLINE step' #-}
330 step' s = case step s of
331 Yield x s' | f x -> Yield x s'
332 | otherwise -> Done
333 Skip s' -> Skip s'
334 Done -> Done
335
336
337 data DropWhile s a = DropWhile_Drop s | DropWhile_Yield a s | DropWhile_Next s
338
339 -- | Drop the longest prefix of elements that satisfy the predicate
340 dropWhile :: (a -> Bool) -> Stream a -> Stream a
341 {-# INLINE_STREAM dropWhile #-}
342 dropWhile f (Stream step s n) = Stream step' (DropWhile_Drop s) (toMax n)
343 where
344 -- NOTE: we jump through hoops here to have only one Yield; local data
345 -- declarations would be nice!
346
347 {-# INLINE step' #-}
348 step' (DropWhile_Drop s)
349 = case step s of
350 Yield x s' | f x -> Skip (DropWhile_Drop s')
351 | otherwise -> Skip (DropWhile_Yield x s')
352 Skip s' -> Skip (DropWhile_Drop s')
353 Done -> Done
354
355 step' (DropWhile_Yield x s) = Yield x (DropWhile_Next s)
356
357 step' (DropWhile_Next s) = case step s of
358 Yield x s' -> Skip (DropWhile_Yield x s')
359 Skip s' -> Skip (DropWhile_Next s')
360 Done -> Done
361
362 -- Searching
363 -- ---------
364
365 infix 4 `elem`
366 -- | Check whether the 'Stream' contains an element
367 elem :: Eq a => a -> Stream a -> Bool
368 {-# INLINE_STREAM elem #-}
369 elem x (Stream step s _) = elem_loop s
370 where
371 elem_loop s = case step s of
372 Yield y s' | x == y -> True
373 | otherwise -> elem_loop s'
374 Skip s' -> elem_loop s'
375 Done -> False
376
377 infix 4 `notElem`
378 -- | Inverse of `elem`
379 notElem :: Eq a => a -> Stream a -> Bool
380 {-# INLINE notElem #-}
381 notElem x = not . elem x
382
383 -- | Yield 'Just' the first element matching the predicate or 'Nothing' if no
384 -- such element exists.
385 find :: (a -> Bool) -> Stream a -> Maybe a
386 {-# INLINE_STREAM find #-}
387 find f (Stream step s _) = find_loop s
388 where
389 find_loop s = case step s of
390 Yield x s' | f x -> Just x
391 | otherwise -> find_loop s'
392 Skip s' -> find_loop s'
393 Done -> Nothing
394
395 -- | Yield 'Just' the index of the first element matching the predicate or
396 -- 'Nothing' if no such element exists.
397 findIndex :: (a -> Bool) -> Stream a -> Maybe Int
398 {-# INLINE_STREAM findIndex #-}
399 findIndex f (Stream step s _) = findIndex_loop s 0
400 where
401 findIndex_loop s i = case step s of
402 Yield x s' | f x -> Just i
403 | otherwise -> findIndex_loop s' (i+1)
404 Skip s' -> findIndex_loop s' i
405 Done -> Nothing
406
407 -- Folding
408 -- -------
409
410 -- | Left fold
411 foldl :: (a -> b -> a) -> a -> Stream b -> a
412 {-# INLINE_STREAM foldl #-}
413 foldl f z (Stream step s _) = foldl_go z s
414 where
415 foldl_go z s = case step s of
416 Yield x s' -> foldl_go (f z x) s'
417 Skip s' -> foldl_go z s'
418 Done -> z
419
420 -- | Left fold on non-empty 'Stream's
421 foldl1 :: (a -> a -> a) -> Stream a -> a
422 {-# INLINE_STREAM foldl1 #-}
423 foldl1 f (Stream step s sz) = foldl1_loop s
424 where
425 foldl1_loop s = case step s of
426 Yield x s' -> foldl f x (Stream step s' (sz - 1))
427 Skip s' -> foldl1_loop s'
428 Done -> error "Data.Vector.Stream.foldl1: empty stream"
429
430 -- | Left fold with strict accumulator
431 foldl' :: (a -> b -> a) -> a -> Stream b -> a
432 {-# INLINE_STREAM foldl' #-}
433 foldl' f z (Stream step s _) = foldl_go z s
434 where
435 foldl_go z s = z `seq`
436 case step s of
437 Yield x s' -> foldl_go (f z x) s'
438 Skip s' -> foldl_go z s'
439 Done -> z
440
441 -- | Left fold on non-empty 'Stream's with strict accumulator
442 foldl1' :: (a -> a -> a) -> Stream a -> a
443 {-# INLINE_STREAM foldl1' #-}
444 foldl1' f (Stream step s sz) = foldl1'_loop s
445 where
446 foldl1'_loop s = case step s of
447 Yield x s' -> foldl' f x (Stream step s' (sz - 1))
448 Skip s' -> foldl1'_loop s'
449 Done -> error "Data.Vector.Stream.foldl1': empty stream"
450
451 -- | Right fold
452 foldr :: (a -> b -> b) -> b -> Stream a -> b
453 {-# INLINE_STREAM foldr #-}
454 foldr f z (Stream step s _) = foldr_go s
455 where
456 foldr_go s = case step s of
457 Yield x s' -> f x (foldr_go s')
458 Skip s' -> foldr_go s'
459 Done -> z
460
461 -- | Right fold on non-empty 'Stream's
462 foldr1 :: (a -> a -> a) -> Stream a -> a
463 {-# INLINE_STREAM foldr1 #-}
464 foldr1 f (Stream step s sz) = foldr1_loop s
465 where
466 foldr1_loop s = case step s of
467 Yield x s' -> foldr f x (Stream step s' (sz - 1))
468 Skip s' -> foldr1_loop s'
469 Done -> error "Data.Vector.Stream.foldr1: empty stream"
470
471 -- | Apply a monadic action to each element of the stream
472 mapM_ :: Monad m => (a -> m ()) -> Stream a -> m ()
473 {-# INLINE_STREAM mapM_ #-}
474 mapM_ m (Stream step s _) = mapM_go s
475 where
476 mapM_go s = case step s of
477 Yield x s' -> do { m x; mapM_go s' }
478 Skip s' -> mapM_go s'
479 Done -> return ()
480
481 -- | Monadic fold
482 foldM :: Monad m => (a -> b -> m a) -> a -> Stream b -> m a
483 {-# INLINE_STREAM foldM #-}
484 foldM m z (Stream step s _) = foldM_go z s
485 where
486 foldM_go z s = case step s of
487 Yield x s' -> do { z' <- m z x; foldM_go z' s' }
488 Skip s' -> foldM_go z s'
489 Done -> return z
490