Add a seq
[darcs-mirrors/vector.git] / Data / Vector / Fusion / Stream / Monadic.hs
1 {-# LANGUAGE ExistentialQuantification, Rank2Types, BangPatterns #-}
2
3 -- |
4 -- Module : Data.Vector.Fusion.Stream.Monadic
5 -- Copyright : (c) Roman Leshchinskiy 2008-2010
6 -- License : BSD-style
7 --
8 -- Maintainer : Roman Leshchinskiy <rl@cse.unsw.edu.au>
9 -- Stability : experimental
10 -- Portability : non-portable
11 --
12 -- Monadic stream combinators.
13 --
14
15 module Data.Vector.Fusion.Stream.Monadic (
16 Stream(..), Step(..),
17
18 -- * Size hints
19 size, sized,
20
21 -- * Length
22 length, null,
23
24 -- * Construction
25 empty, singleton, cons, snoc, replicate, replicateM, generate, generateM, (++),
26
27 -- * Accessing elements
28 head, last, (!!), (!?),
29
30 -- * Substreams
31 slice, init, tail, take, drop,
32
33 -- * Mapping
34 map, mapM, mapM_, trans, unbox, concatMap, flatten,
35
36 -- * Zipping
37 indexed, indexedR, zipWithM_,
38 zipWithM, zipWith3M, zipWith4M, zipWith5M, zipWith6M,
39 zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
40 zip, zip3, zip4, zip5, zip6,
41
42 -- * Filtering
43 filter, filterM, takeWhile, takeWhileM, dropWhile, dropWhileM,
44
45 -- * Searching
46 elem, notElem, find, findM, findIndex, findIndexM,
47
48 -- * Folding
49 foldl, foldlM, foldl1, foldl1M, foldM, fold1M,
50 foldl', foldlM', foldl1', foldl1M', foldM', fold1M',
51 foldr, foldrM, foldr1, foldr1M,
52
53 -- * Specialised folds
54 and, or, concatMapM,
55
56 -- * Unfolding
57 unfoldr, unfoldrM,
58 unfoldrN, unfoldrNM,
59 iterateN, iterateNM,
60
61 -- * Scans
62 prescanl, prescanlM, prescanl', prescanlM',
63 postscanl, postscanlM, postscanl', postscanlM',
64 scanl, scanlM, scanl', scanlM',
65 scanl1, scanl1M, scanl1', scanl1M',
66
67 -- * Enumerations
68 enumFromStepN, enumFromTo, enumFromThenTo,
69
70 -- * Conversions
71 toList, fromList, fromListN, unsafeFromList
72 ) where
73
74 import Data.Vector.Fusion.Stream.Size
75 import Data.Vector.Fusion.Util ( Box(..), delay_inline )
76
77 import Data.Char ( ord )
78 import GHC.Base ( unsafeChr )
79 import Control.Monad ( liftM )
80 import Prelude hiding ( length, null,
81 replicate, (++),
82 head, last, (!!),
83 init, tail, take, drop,
84 map, mapM, mapM_, concatMap,
85 zipWith, zipWith3, zip, zip3,
86 filter, takeWhile, dropWhile,
87 elem, notElem,
88 foldl, foldl1, foldr, foldr1,
89 and, or,
90 scanl, scanl1,
91 enumFromTo, enumFromThenTo )
92
93 import Data.Int ( Int8, Int16, Int32, Int64 )
94 import Data.Word ( Word8, Word16, Word32, Word, Word64 )
95
96 #if __GLASGOW_HASKELL__ >= 700
97 import GHC.Exts ( SpecConstrAnnotation(..) )
98 #endif
99
100 #include "vector.h"
101
102 data SPEC = SPEC | SPEC2
103 #if __GLASGOW_HASKELL__ >= 700
104 {-# ANN type SPEC ForceSpecConstr #-}
105 #endif
106
107
108 -- | Result of taking a single step in a stream
109 data Step s a = Yield a s -- ^ a new element and a new seed
110 | Skip s -- ^ just a new seed
111 | Done -- ^ end of stream
112
113 -- | Monadic streams
114 data Stream m a = forall s. Stream (s -> m (Step s a)) s Size
115
116 -- | 'Size' hint of a 'Stream'
117 size :: Stream m a -> Size
118 {-# INLINE size #-}
119 size (Stream _ _ sz) = sz
120
121 -- | Attach a 'Size' hint to a 'Stream'
122 sized :: Stream m a -> Size -> Stream m a
123 {-# INLINE_STREAM sized #-}
124 sized (Stream step s _) sz = Stream step s sz
125
126 -- Length
127 -- ------
128
129 -- | Length of a 'Stream'
130 length :: Monad m => Stream m a -> m Int
131 {-# INLINE_STREAM length #-}
132 length s = foldl' (\n _ -> n+1) 0 s
133
134 -- | Check if a 'Stream' is empty
135 null :: Monad m => Stream m a -> m Bool
136 {-# INLINE_STREAM null #-}
137 null s = foldr (\_ _ -> False) True s
138
139
140 -- Construction
141 -- ------------
142
143 -- | Empty 'Stream'
144 empty :: Monad m => Stream m a
145 {-# INLINE_STREAM empty #-}
146 empty = Stream (const (return Done)) () (Exact 0)
147
148 -- | Singleton 'Stream'
149 singleton :: Monad m => a -> Stream m a
150 {-# INLINE_STREAM singleton #-}
151 singleton x = Stream (return . step) True (Exact 1)
152 where
153 {-# INLINE_INNER step #-}
154 step True = Yield x False
155 step False = Done
156
157 -- | Replicate a value to a given length
158 replicate :: Monad m => Int -> a -> Stream m a
159 {-# INLINE replicate #-}
160 replicate n x = replicateM n (return x)
161
162 -- | Yield a 'Stream' of values obtained by performing the monadic action the
163 -- given number of times
164 replicateM :: Monad m => Int -> m a -> Stream m a
165 {-# INLINE_STREAM replicateM #-}
166 -- NOTE: We delay inlining max here because GHC will create a join point for
167 -- the call to newArray# otherwise which is not really nice.
168 replicateM n p = Stream step n (Exact (delay_inline max n 0))
169 where
170 {-# INLINE_INNER step #-}
171 step i | i <= 0 = return Done
172 | otherwise = do { x <- p; return $ Yield x (i-1) }
173
174 generate :: Monad m => Int -> (Int -> a) -> Stream m a
175 {-# INLINE generate #-}
176 generate n f = generateM n (return . f)
177
178 -- | Generate a stream from its indices
179 generateM :: Monad m => Int -> (Int -> m a) -> Stream m a
180 {-# INLINE_STREAM generateM #-}
181 generateM n f = n `seq` Stream step 0 (Exact (delay_inline max n 0))
182 where
183 {-# INLINE_INNER step #-}
184 step i | i < n = do
185 x <- f i
186 return $ Yield x (i+1)
187 | otherwise = return Done
188
189 -- | Prepend an element
190 cons :: Monad m => a -> Stream m a -> Stream m a
191 {-# INLINE cons #-}
192 cons x s = singleton x ++ s
193
194 -- | Append an element
195 snoc :: Monad m => Stream m a -> a -> Stream m a
196 {-# INLINE snoc #-}
197 snoc s x = s ++ singleton x
198
199 infixr 5 ++
200 -- | Concatenate two 'Stream's
201 (++) :: Monad m => Stream m a -> Stream m a -> Stream m a
202 {-# INLINE_STREAM (++) #-}
203 Stream stepa sa na ++ Stream stepb sb nb = Stream step (Left sa) (na + nb)
204 where
205 {-# INLINE_INNER step #-}
206 step (Left sa) = do
207 r <- stepa sa
208 case r of
209 Yield x sa' -> return $ Yield x (Left sa')
210 Skip sa' -> return $ Skip (Left sa')
211 Done -> return $ Skip (Right sb)
212 step (Right sb) = do
213 r <- stepb sb
214 case r of
215 Yield x sb' -> return $ Yield x (Right sb')
216 Skip sb' -> return $ Skip (Right sb')
217 Done -> return $ Done
218
219 -- Accessing elements
220 -- ------------------
221
222 -- | First element of the 'Stream' or error if empty
223 head :: Monad m => Stream m a -> m a
224 {-# INLINE_STREAM head #-}
225 head (Stream step s _) = head_loop SPEC s
226 where
227 head_loop !sPEC s
228 = do
229 r <- step s
230 case r of
231 Yield x _ -> return x
232 Skip s' -> head_loop SPEC s'
233 Done -> BOUNDS_ERROR(emptyStream) "head"
234
235
236
237 -- | Last element of the 'Stream' or error if empty
238 last :: Monad m => Stream m a -> m a
239 {-# INLINE_STREAM last #-}
240 last (Stream step s _) = last_loop0 SPEC s
241 where
242 last_loop0 !sPEC s
243 = do
244 r <- step s
245 case r of
246 Yield x s' -> last_loop1 SPEC x s'
247 Skip s' -> last_loop0 SPEC s'
248 Done -> BOUNDS_ERROR(emptyStream) "last"
249
250 last_loop1 !sPEC x s
251 = do
252 r <- step s
253 case r of
254 Yield y s' -> last_loop1 SPEC y s'
255 Skip s' -> last_loop1 SPEC x s'
256 Done -> return x
257
258 infixl 9 !!
259 -- | Element at the given position
260 (!!) :: Monad m => Stream m a -> Int -> m a
261 {-# INLINE (!!) #-}
262 Stream step s _ !! i | i < 0 = BOUNDS_ERROR(error) "!!" "negative index"
263 | otherwise = index_loop SPEC s i
264 where
265 index_loop !sPEC s i
266 = i `seq`
267 do
268 r <- step s
269 case r of
270 Yield x s' | i == 0 -> return x
271 | otherwise -> index_loop SPEC s' (i-1)
272 Skip s' -> index_loop SPEC s' i
273 Done -> BOUNDS_ERROR(emptyStream) "!!"
274
275 infixl 9 !?
276 -- | Element at the given position or 'Nothing' if out of bounds
277 (!?) :: Monad m => Stream m a -> Int -> m (Maybe a)
278 {-# INLINE (!?) #-}
279 Stream step s _ !? i = index_loop SPEC s i
280 where
281 index_loop !sPEC s i
282 = i `seq`
283 do
284 r <- step s
285 case r of
286 Yield x s' | i == 0 -> return (Just x)
287 | otherwise -> index_loop SPEC s' (i-1)
288 Skip s' -> index_loop SPEC s' i
289 Done -> return Nothing
290
291 -- Substreams
292 -- ----------
293
294 -- | Extract a substream of the given length starting at the given position.
295 slice :: Monad m => Int -- ^ starting index
296 -> Int -- ^ length
297 -> Stream m a
298 -> Stream m a
299 {-# INLINE slice #-}
300 slice i n s = take n (drop i s)
301
302 -- | All but the last element
303 init :: Monad m => Stream m a -> Stream m a
304 {-# INLINE_STREAM init #-}
305 init (Stream step s sz) = Stream step' (Nothing, s) (sz - 1)
306 where
307 {-# INLINE_INNER step' #-}
308 step' (Nothing, s) = liftM (\r ->
309 case r of
310 Yield x s' -> Skip (Just x, s')
311 Skip s' -> Skip (Nothing, s')
312 Done -> BOUNDS_ERROR(emptyStream) "init"
313 ) (step s)
314
315 step' (Just x, s) = liftM (\r ->
316 case r of
317 Yield y s' -> Yield x (Just y, s')
318 Skip s' -> Skip (Just x, s')
319 Done -> Done
320 ) (step s)
321
322 -- | All but the first element
323 tail :: Monad m => Stream m a -> Stream m a
324 {-# INLINE_STREAM tail #-}
325 tail (Stream step s sz) = Stream step' (Left s) (sz - 1)
326 where
327 {-# INLINE_INNER step' #-}
328 step' (Left s) = liftM (\r ->
329 case r of
330 Yield x s' -> Skip (Right s')
331 Skip s' -> Skip (Left s')
332 Done -> BOUNDS_ERROR(emptyStream) "tail"
333 ) (step s)
334
335 step' (Right s) = liftM (\r ->
336 case r of
337 Yield x s' -> Yield x (Right s')
338 Skip s' -> Skip (Right s')
339 Done -> Done
340 ) (step s)
341
342 -- | The first @n@ elements
343 take :: Monad m => Int -> Stream m a -> Stream m a
344 {-# INLINE_STREAM take #-}
345 take n (Stream step s sz) = Stream step' (s, 0) (smaller (Exact n) sz)
346 where
347 {-# INLINE_INNER step' #-}
348 step' (s, i) | i < n = liftM (\r ->
349 case r of
350 Yield x s' -> Yield x (s', i+1)
351 Skip s' -> Skip (s', i)
352 Done -> Done
353 ) (step s)
354 step' (s, i) = return Done
355
356 -- | All but the first @n@ elements
357 drop :: Monad m => Int -> Stream m a -> Stream m a
358 {-# INLINE_STREAM drop #-}
359 drop n (Stream step s sz) = Stream step' (s, Just n) (sz - Exact n)
360 where
361 {-# INLINE_INNER step' #-}
362 step' (s, Just i) | i > 0 = liftM (\r ->
363 case r of
364 Yield x s' -> Skip (s', Just (i-1))
365 Skip s' -> Skip (s', Just i)
366 Done -> Done
367 ) (step s)
368 | otherwise = return $ Skip (s, Nothing)
369
370 step' (s, Nothing) = liftM (\r ->
371 case r of
372 Yield x s' -> Yield x (s', Nothing)
373 Skip s' -> Skip (s', Nothing)
374 Done -> Done
375 ) (step s)
376
377 -- Mapping
378 -- -------
379
380 instance Monad m => Functor (Stream m) where
381 {-# INLINE fmap #-}
382 fmap = map
383
384 -- | Map a function over a 'Stream'
385 map :: Monad m => (a -> b) -> Stream m a -> Stream m b
386 {-# INLINE map #-}
387 map f = mapM (return . f)
388
389
390 -- | Map a monadic function over a 'Stream'
391 mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
392 {-# INLINE_STREAM mapM #-}
393 mapM f (Stream step s n) = Stream step' s n
394 where
395 {-# INLINE_INNER step' #-}
396 step' s = do
397 r <- step s
398 case r of
399 Yield x s' -> liftM (`Yield` s') (f x)
400 Skip s' -> return (Skip s')
401 Done -> return Done
402
403 consume :: Monad m => Stream m a -> m ()
404 {-# INLINE_STREAM consume #-}
405 consume (Stream step s _) = consume_loop SPEC s
406 where
407 consume_loop !sPEC s
408 = do
409 r <- step s
410 case r of
411 Yield _ s' -> consume_loop SPEC s'
412 Skip s' -> consume_loop SPEC s'
413 Done -> return ()
414
415 -- | Execute a monadic action for each element of the 'Stream'
416 mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
417 {-# INLINE_STREAM mapM_ #-}
418 mapM_ m = consume . mapM m
419
420 -- | Transform a 'Stream' to use a different monad
421 trans :: (Monad m, Monad m') => (forall a. m a -> m' a)
422 -> Stream m a -> Stream m' a
423 {-# INLINE_STREAM trans #-}
424 trans f (Stream step s n) = Stream (f . step) s n
425
426 unbox :: Monad m => Stream m (Box a) -> Stream m a
427 {-# INLINE_STREAM unbox #-}
428 unbox (Stream step s n) = Stream step' s n
429 where
430 {-# INLINE_INNER step' #-}
431 step' s = do
432 r <- step s
433 case r of
434 Yield (Box x) s' -> return $ Yield x s'
435 Skip s' -> return $ Skip s'
436 Done -> return $ Done
437
438 -- Zipping
439 -- -------
440
441 -- | Pair each element in a 'Stream' with its index
442 indexed :: Monad m => Stream m a -> Stream m (Int,a)
443 {-# INLINE_STREAM indexed #-}
444 indexed (Stream step s n) = Stream step' (s,0) n
445 where
446 {-# INLINE_INNER step' #-}
447 step' (s,i) = i `seq`
448 do
449 r <- step s
450 case r of
451 Yield x s' -> return $ Yield (i,x) (s', i+1)
452 Skip s' -> return $ Skip (s', i)
453 Done -> return Done
454
455 -- | Pair each element in a 'Stream' with its index, starting from the right
456 -- and counting down
457 indexedR :: Monad m => Int -> Stream m a -> Stream m (Int,a)
458 {-# INLINE_STREAM indexedR #-}
459 indexedR m (Stream step s n) = Stream step' (s,m) n
460 where
461 {-# INLINE_INNER step' #-}
462 step' (s,i) = i `seq`
463 do
464 r <- step s
465 case r of
466 Yield x s' -> let i' = i-1
467 in
468 return $ Yield (i',x) (s', i')
469 Skip s' -> return $ Skip (s', i)
470 Done -> return Done
471
472 -- | Zip two 'Stream's with the given monadic function
473 zipWithM :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
474 {-# INLINE_STREAM zipWithM #-}
475 zipWithM f (Stream stepa sa na) (Stream stepb sb nb)
476 = Stream step (sa, sb, Nothing) (smaller na nb)
477 where
478 {-# INLINE_INNER step #-}
479 step (sa, sb, Nothing) = liftM (\r ->
480 case r of
481 Yield x sa' -> Skip (sa', sb, Just x)
482 Skip sa' -> Skip (sa', sb, Nothing)
483 Done -> Done
484 ) (stepa sa)
485
486 step (sa, sb, Just x) = do
487 r <- stepb sb
488 case r of
489 Yield y sb' ->
490 do
491 z <- f x y
492 return $ Yield z (sa, sb', Nothing)
493 Skip sb' -> return $ Skip (sa, sb', Just x)
494 Done -> return $ Done
495
496 -- FIXME: This might expose an opportunity for inplace execution.
497 {-# RULES
498
499 "zipWithM xs xs [Vector.Stream]" forall f xs.
500 zipWithM f xs xs = mapM (\x -> f x x) xs
501
502 #-}
503
504 zipWithM_ :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> m ()
505 {-# INLINE zipWithM_ #-}
506 zipWithM_ f sa sb = consume (zipWithM f sa sb)
507
508 zipWith3M :: Monad m => (a -> b -> c -> m d) -> Stream m a -> Stream m b -> Stream m c -> Stream m d
509 {-# INLINE_STREAM zipWith3M #-}
510 zipWith3M f (Stream stepa sa na) (Stream stepb sb nb) (Stream stepc sc nc)
511 = Stream step (sa, sb, sc, Nothing) (smaller na (smaller nb nc))
512 where
513 {-# INLINE_INNER step #-}
514 step (sa, sb, sc, Nothing) = do
515 r <- stepa sa
516 return $ case r of
517 Yield x sa' -> Skip (sa', sb, sc, Just (x, Nothing))
518 Skip sa' -> Skip (sa', sb, sc, Nothing)
519 Done -> Done
520
521 step (sa, sb, sc, Just (x, Nothing)) = do
522 r <- stepb sb
523 return $ case r of
524 Yield y sb' -> Skip (sa, sb', sc, Just (x, Just y))
525 Skip sb' -> Skip (sa, sb', sc, Just (x, Nothing))
526 Done -> Done
527
528 step (sa, sb, sc, Just (x, Just y)) = do
529 r <- stepc sc
530 case r of
531 Yield z sc' -> f x y z >>= (\res -> return $ Yield res (sa, sb, sc', Nothing))
532 Skip sc' -> return $ Skip (sa, sb, sc', Just (x, Just y))
533 Done -> return $ Done
534
535 zipWith4M :: Monad m => (a -> b -> c -> d -> m e)
536 -> Stream m a -> Stream m b -> Stream m c -> Stream m d
537 -> Stream m e
538 {-# INLINE zipWith4M #-}
539 zipWith4M f sa sb sc sd
540 = zipWithM (\(a,b) (c,d) -> f a b c d) (zip sa sb) (zip sc sd)
541
542 zipWith5M :: Monad m => (a -> b -> c -> d -> e -> m f)
543 -> Stream m a -> Stream m b -> Stream m c -> Stream m d
544 -> Stream m e -> Stream m f
545 {-# INLINE zipWith5M #-}
546 zipWith5M f sa sb sc sd se
547 = zipWithM (\(a,b,c) (d,e) -> f a b c d e) (zip3 sa sb sc) (zip sd se)
548
549 zipWith6M :: Monad m => (a -> b -> c -> d -> e -> f -> m g)
550 -> Stream m a -> Stream m b -> Stream m c -> Stream m d
551 -> Stream m e -> Stream m f -> Stream m g
552 {-# INLINE zipWith6M #-}
553 zipWith6M fn sa sb sc sd se sf
554 = zipWithM (\(a,b,c) (d,e,f) -> fn a b c d e f) (zip3 sa sb sc)
555 (zip3 sd se sf)
556
557 zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
558 {-# INLINE zipWith #-}
559 zipWith f = zipWithM (\a b -> return (f a b))
560
561 zipWith3 :: Monad m => (a -> b -> c -> d)
562 -> Stream m a -> Stream m b -> Stream m c -> Stream m d
563 {-# INLINE zipWith3 #-}
564 zipWith3 f = zipWith3M (\a b c -> return (f a b c))
565
566 zipWith4 :: Monad m => (a -> b -> c -> d -> e)
567 -> Stream m a -> Stream m b -> Stream m c -> Stream m d
568 -> Stream m e
569 {-# INLINE zipWith4 #-}
570 zipWith4 f = zipWith4M (\a b c d -> return (f a b c d))
571
572 zipWith5 :: Monad m => (a -> b -> c -> d -> e -> f)
573 -> Stream m a -> Stream m b -> Stream m c -> Stream m d
574 -> Stream m e -> Stream m f
575 {-# INLINE zipWith5 #-}
576 zipWith5 f = zipWith5M (\a b c d e -> return (f a b c d e))
577
578 zipWith6 :: Monad m => (a -> b -> c -> d -> e -> f -> g)
579 -> Stream m a -> Stream m b -> Stream m c -> Stream m d
580 -> Stream m e -> Stream m f -> Stream m g
581 {-# INLINE zipWith6 #-}
582 zipWith6 fn = zipWith6M (\a b c d e f -> return (fn a b c d e f))
583
584 zip :: Monad m => Stream m a -> Stream m b -> Stream m (a,b)
585 {-# INLINE zip #-}
586 zip = zipWith (,)
587
588 zip3 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m (a,b,c)
589 {-# INLINE zip3 #-}
590 zip3 = zipWith3 (,,)
591
592 zip4 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d
593 -> Stream m (a,b,c,d)
594 {-# INLINE zip4 #-}
595 zip4 = zipWith4 (,,,)
596
597 zip5 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d
598 -> Stream m e -> Stream m (a,b,c,d,e)
599 {-# INLINE zip5 #-}
600 zip5 = zipWith5 (,,,,)
601
602 zip6 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d
603 -> Stream m e -> Stream m f -> Stream m (a,b,c,d,e,f)
604 {-# INLINE zip6 #-}
605 zip6 = zipWith6 (,,,,,)
606
607 -- Filtering
608 -- ---------
609
610 -- | Drop elements which do not satisfy the predicate
611 filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
612 {-# INLINE filter #-}
613 filter f = filterM (return . f)
614
615 -- | Drop elements which do not satisfy the monadic predicate
616 filterM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
617 {-# INLINE_STREAM filterM #-}
618 filterM f (Stream step s n) = Stream step' s (toMax n)
619 where
620 {-# INLINE_INNER step' #-}
621 step' s = do
622 r <- step s
623 case r of
624 Yield x s' -> do
625 b <- f x
626 return $ if b then Yield x s'
627 else Skip s'
628 Skip s' -> return $ Skip s'
629 Done -> return $ Done
630
631 -- | Longest prefix of elements that satisfy the predicate
632 takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
633 {-# INLINE takeWhile #-}
634 takeWhile f = takeWhileM (return . f)
635
636 -- | Longest prefix of elements that satisfy the monadic predicate
637 takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
638 {-# INLINE_STREAM takeWhileM #-}
639 takeWhileM f (Stream step s n) = Stream step' s (toMax n)
640 where
641 {-# INLINE_INNER step' #-}
642 step' s = do
643 r <- step s
644 case r of
645 Yield x s' -> do
646 b <- f x
647 return $ if b then Yield x s' else Done
648 Skip s' -> return $ Skip s'
649 Done -> return $ Done
650
651 -- | Drop the longest prefix of elements that satisfy the predicate
652 dropWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
653 {-# INLINE dropWhile #-}
654 dropWhile f = dropWhileM (return . f)
655
656 data DropWhile s a = DropWhile_Drop s | DropWhile_Yield a s | DropWhile_Next s
657
658 -- | Drop the longest prefix of elements that satisfy the monadic predicate
659 dropWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
660 {-# INLINE_STREAM dropWhileM #-}
661 dropWhileM f (Stream step s n) = Stream step' (DropWhile_Drop s) (toMax n)
662 where
663 -- NOTE: we jump through hoops here to have only one Yield; local data
664 -- declarations would be nice!
665
666 {-# INLINE_INNER step' #-}
667 step' (DropWhile_Drop s)
668 = do
669 r <- step s
670 case r of
671 Yield x s' -> do
672 b <- f x
673 return $ if b then Skip (DropWhile_Drop s')
674 else Skip (DropWhile_Yield x s')
675 Skip s' -> return $ Skip (DropWhile_Drop s')
676 Done -> return $ Done
677
678 step' (DropWhile_Yield x s) = return $ Yield x (DropWhile_Next s)
679
680 step' (DropWhile_Next s)
681 = liftM (\r ->
682 case r of
683 Yield x s' -> Skip (DropWhile_Yield x s')
684 Skip s' -> Skip (DropWhile_Next s')
685 Done -> Done
686 ) (step s)
687
688 -- Searching
689 -- ---------
690
691 infix 4 `elem`
692 -- | Check whether the 'Stream' contains an element
693 elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
694 {-# INLINE_STREAM elem #-}
695 elem x (Stream step s _) = elem_loop SPEC s
696 where
697 elem_loop !sPEC s
698 = do
699 r <- step s
700 case r of
701 Yield y s' | x == y -> return True
702 | otherwise -> elem_loop SPEC s'
703 Skip s' -> elem_loop SPEC s'
704 Done -> return False
705
706 infix 4 `notElem`
707 -- | Inverse of `elem`
708 notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
709 {-# INLINE notElem #-}
710 notElem x s = liftM not (elem x s)
711
712 -- | Yield 'Just' the first element that satisfies the predicate or 'Nothing'
713 -- if no such element exists.
714 find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
715 {-# INLINE find #-}
716 find f = findM (return . f)
717
718 -- | Yield 'Just' the first element that satisfies the monadic predicate or
719 -- 'Nothing' if no such element exists.
720 findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)
721 {-# INLINE_STREAM findM #-}
722 findM f (Stream step s _) = find_loop SPEC s
723 where
724 find_loop !sPEC s
725 = do
726 r <- step s
727 case r of
728 Yield x s' -> do
729 b <- f x
730 if b then return $ Just x
731 else find_loop SPEC s'
732 Skip s' -> find_loop SPEC s'
733 Done -> return Nothing
734
735 -- | Yield 'Just' the index of the first element that satisfies the predicate
736 -- or 'Nothing' if no such element exists.
737 findIndex :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe Int)
738 {-# INLINE_STREAM findIndex #-}
739 findIndex f = findIndexM (return . f)
740
741 -- | Yield 'Just' the index of the first element that satisfies the monadic
742 -- predicate or 'Nothing' if no such element exists.
743 findIndexM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe Int)
744 {-# INLINE_STREAM findIndexM #-}
745 findIndexM f (Stream step s _) = findIndex_loop SPEC s 0
746 where
747 findIndex_loop !sPEC s i
748 = do
749 r <- step s
750 case r of
751 Yield x s' -> do
752 b <- f x
753 if b then return $ Just i
754 else findIndex_loop SPEC s' (i+1)
755 Skip s' -> findIndex_loop SPEC s' i
756 Done -> return Nothing
757
758 -- Folding
759 -- -------
760
761 -- | Left fold
762 foldl :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
763 {-# INLINE foldl #-}
764 foldl f = foldlM (\a b -> return (f a b))
765
766 -- | Left fold with a monadic operator
767 foldlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
768 {-# INLINE_STREAM foldlM #-}
769 foldlM m z (Stream step s _) = foldlM_loop SPEC z s
770 where
771 foldlM_loop !sPEC z s
772 = do
773 r <- step s
774 case r of
775 Yield x s' -> do { z' <- m z x; foldlM_loop SPEC z' s' }
776 Skip s' -> foldlM_loop SPEC z s'
777 Done -> return z
778
779 -- | Same as 'foldlM'
780 foldM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
781 {-# INLINE foldM #-}
782 foldM = foldlM
783
784 -- | Left fold over a non-empty 'Stream'
785 foldl1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
786 {-# INLINE foldl1 #-}
787 foldl1 f = foldl1M (\a b -> return (f a b))
788
789 -- | Left fold over a non-empty 'Stream' with a monadic operator
790 foldl1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
791 {-# INLINE_STREAM foldl1M #-}
792 foldl1M f (Stream step s sz) = foldl1M_loop SPEC s
793 where
794 foldl1M_loop !sPEC s
795 = do
796 r <- step s
797 case r of
798 Yield x s' -> foldlM f x (Stream step s' (sz - 1))
799 Skip s' -> foldl1M_loop SPEC s'
800 Done -> BOUNDS_ERROR(emptyStream) "foldl1M"
801
802 -- | Same as 'foldl1M'
803 fold1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
804 {-# INLINE fold1M #-}
805 fold1M = foldl1M
806
807 -- | Left fold with a strict accumulator
808 foldl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
809 {-# INLINE foldl' #-}
810 foldl' f = foldlM' (\a b -> return (f a b))
811
812 -- | Left fold with a strict accumulator and a monadic operator
813 foldlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
814 {-# INLINE_STREAM foldlM' #-}
815 foldlM' m z (Stream step s _) = foldlM'_loop SPEC z s
816 where
817 foldlM'_loop !sPEC z s
818 = z `seq`
819 do
820 r <- step s
821 case r of
822 Yield x s' -> do { z' <- m z x; foldlM'_loop SPEC z' s' }
823 Skip s' -> foldlM'_loop SPEC z s'
824 Done -> return z
825
826 -- | Same as 'foldlM''
827 foldM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
828 {-# INLINE foldM' #-}
829 foldM' = foldlM'
830
831 -- | Left fold over a non-empty 'Stream' with a strict accumulator
832 foldl1' :: Monad m => (a -> a -> a) -> Stream m a -> m a
833 {-# INLINE foldl1' #-}
834 foldl1' f = foldl1M' (\a b -> return (f a b))
835
836 -- | Left fold over a non-empty 'Stream' with a strict accumulator and a
837 -- monadic operator
838 foldl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m a
839 {-# INLINE_STREAM foldl1M' #-}
840 foldl1M' f (Stream step s sz) = foldl1M'_loop SPEC s
841 where
842 foldl1M'_loop !sPEC s
843 = do
844 r <- step s
845 case r of
846 Yield x s' -> foldlM' f x (Stream step s' (sz - 1))
847 Skip s' -> foldl1M'_loop SPEC s'
848 Done -> BOUNDS_ERROR(emptyStream) "foldl1M'"
849
850 -- | Same as 'foldl1M''
851 fold1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m a
852 {-# INLINE fold1M' #-}
853 fold1M' = foldl1M'
854
855 -- | Right fold
856 foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
857 {-# INLINE foldr #-}
858 foldr f = foldrM (\a b -> return (f a b))
859
860 -- | Right fold with a monadic operator
861 foldrM :: Monad m => (a -> b -> m b) -> b -> Stream m a -> m b
862 {-# INLINE_STREAM foldrM #-}
863 foldrM f z (Stream step s _) = foldrM_loop SPEC s
864 where
865 foldrM_loop !sPEC s
866 = do
867 r <- step s
868 case r of
869 Yield x s' -> f x =<< foldrM_loop SPEC s'
870 Skip s' -> foldrM_loop SPEC s'
871 Done -> return z
872
873 -- | Right fold over a non-empty stream
874 foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
875 {-# INLINE foldr1 #-}
876 foldr1 f = foldr1M (\a b -> return (f a b))
877
878 -- | Right fold over a non-empty stream with a monadic operator
879 foldr1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
880 {-# INLINE_STREAM foldr1M #-}
881 foldr1M f (Stream step s _) = foldr1M_loop0 SPEC s
882 where
883 foldr1M_loop0 !sPEC s
884 = do
885 r <- step s
886 case r of
887 Yield x s' -> foldr1M_loop1 SPEC x s'
888 Skip s' -> foldr1M_loop0 SPEC s'
889 Done -> BOUNDS_ERROR(emptyStream) "foldr1M"
890
891 foldr1M_loop1 !sPEC x s
892 = do
893 r <- step s
894 case r of
895 Yield y s' -> f x =<< foldr1M_loop1 SPEC y s'
896 Skip s' -> foldr1M_loop1 SPEC x s'
897 Done -> return x
898
899 -- Specialised folds
900 -- -----------------
901
902 and :: Monad m => Stream m Bool -> m Bool
903 {-# INLINE_STREAM and #-}
904 and (Stream step s _) = and_loop SPEC s
905 where
906 and_loop !sPEC s
907 = do
908 r <- step s
909 case r of
910 Yield False _ -> return False
911 Yield True s' -> and_loop SPEC s'
912 Skip s' -> and_loop SPEC s'
913 Done -> return True
914
915 or :: Monad m => Stream m Bool -> m Bool
916 {-# INLINE_STREAM or #-}
917 or (Stream step s _) = or_loop SPEC s
918 where
919 or_loop !sPEC s
920 = do
921 r <- step s
922 case r of
923 Yield False s' -> or_loop SPEC s'
924 Yield True _ -> return True
925 Skip s' -> or_loop SPEC s'
926 Done -> return False
927
928 concatMap :: Monad m => (a -> Stream m b) -> Stream m a -> Stream m b
929 {-# INLINE concatMap #-}
930 concatMap f = concatMapM (return . f)
931
932 concatMapM :: Monad m => (a -> m (Stream m b)) -> Stream m a -> Stream m b
933 {-# INLINE_STREAM concatMapM #-}
934 concatMapM f (Stream step s _) = Stream concatMap_go (Left s) Unknown
935 where
936 concatMap_go (Left s) = do
937 r <- step s
938 case r of
939 Yield a s' -> do
940 b_stream <- f a
941 return $ Skip (Right (b_stream, s'))
942 Skip s' -> return $ Skip (Left s')
943 Done -> return Done
944 concatMap_go (Right (Stream inner_step inner_s sz, s)) = do
945 r <- inner_step inner_s
946 case r of
947 Yield b inner_s' -> return $ Yield b (Right (Stream inner_step inner_s' sz, s))
948 Skip inner_s' -> return $ Skip (Right (Stream inner_step inner_s' sz, s))
949 Done -> return $ Skip (Left s)
950
951 -- | Create a 'Stream' of values from a 'Stream' of streamable things
952 flatten :: Monad m => (a -> m s) -> (s -> m (Step s b)) -> Size
953 -> Stream m a -> Stream m b
954 {-# INLINE_STREAM flatten #-}
955 flatten mk istep sz (Stream ostep t _) = Stream step (Left t) sz
956 where
957 {-# INLINE_INNER step #-}
958 step (Left t) = do
959 r <- ostep t
960 case r of
961 Yield a t' -> do
962 s <- mk a
963 s `seq` return (Skip (Right (s,t')))
964 Skip t' -> return $ Skip (Left t')
965 Done -> return $ Done
966
967
968 step (Right (s,t)) = do
969 r <- istep s
970 case r of
971 Yield x s' -> return $ Yield x (Right (s',t))
972 Skip s' -> return $ Skip (Right (s',t))
973 Done -> return $ Skip (Left t)
974
975 -- Unfolding
976 -- ---------
977
978 -- | Unfold
979 unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
980 {-# INLINE_STREAM unfoldr #-}
981 unfoldr f = unfoldrM (return . f)
982
983 -- | Unfold with a monadic function
984 unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m a
985 {-# INLINE_STREAM unfoldrM #-}
986 unfoldrM f s = Stream step s Unknown
987 where
988 {-# INLINE_INNER step #-}
989 step s = liftM (\r ->
990 case r of
991 Just (x, s') -> Yield x s'
992 Nothing -> Done
993 ) (f s)
994
995 -- | Unfold at most @n@ elements
996 unfoldrN :: Monad m => Int -> (s -> Maybe (a, s)) -> s -> Stream m a
997 {-# INLINE_STREAM unfoldrN #-}
998 unfoldrN n f = unfoldrNM n (return . f)
999
1000 -- | Unfold at most @n@ elements with a monadic functions
1001 unfoldrNM :: Monad m => Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
1002 {-# INLINE_STREAM unfoldrNM #-}
1003 unfoldrNM n f s = Stream step (s,n) (Max (delay_inline max n 0))
1004 where
1005 {-# INLINE_INNER step #-}
1006 step (s,n) | n <= 0 = return Done
1007 | otherwise = liftM (\r ->
1008 case r of
1009 Just (x,s') -> Yield x (s',n-1)
1010 Nothing -> Done
1011 ) (f s)
1012
1013 -- | Apply monadic function n times to value. Zeroth element is original value.
1014 iterateNM :: Monad m => Int -> (a -> m a) -> a -> Stream m a
1015 {-# INLINE_STREAM iterateNM #-}
1016 iterateNM n f x0 = Stream step (x0,n) (Exact (delay_inline max n 0))
1017 where
1018 {-# INLINE_INNER step #-}
1019 step (x,i) | i <= 0 = return Done
1020 | i == n = return $ Yield x (x,i-1)
1021 | otherwise = do a <- f x
1022 return $ Yield a (a,i-1)
1023
1024 -- | Apply function n times to value. Zeroth element is original value.
1025 iterateN :: Monad m => Int -> (a -> a) -> a -> Stream m a
1026 {-# INLINE_STREAM iterateN #-}
1027 iterateN n f x0 = iterateNM n (return . f) x0
1028
1029 -- Scans
1030 -- -----
1031
1032 -- | Prefix scan
1033 prescanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
1034 {-# INLINE prescanl #-}
1035 prescanl f = prescanlM (\a b -> return (f a b))
1036
1037 -- | Prefix scan with a monadic operator
1038 prescanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
1039 {-# INLINE_STREAM prescanlM #-}
1040 prescanlM f z (Stream step s sz) = Stream step' (s,z) sz
1041 where
1042 {-# INLINE_INNER step' #-}
1043 step' (s,x) = do
1044 r <- step s
1045 case r of
1046 Yield y s' -> do
1047 z <- f x y
1048 return $ Yield x (s', z)
1049 Skip s' -> return $ Skip (s', x)
1050 Done -> return Done
1051
1052 -- | Prefix scan with strict accumulator
1053 prescanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
1054 {-# INLINE prescanl' #-}
1055 prescanl' f = prescanlM' (\a b -> return (f a b))
1056
1057 -- | Prefix scan with strict accumulator and a monadic operator
1058 prescanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
1059 {-# INLINE_STREAM prescanlM' #-}
1060 prescanlM' f z (Stream step s sz) = Stream step' (s,z) sz
1061 where
1062 {-# INLINE_INNER step' #-}
1063 step' (s,x) = x `seq`
1064 do
1065 r <- step s
1066 case r of
1067 Yield y s' -> do
1068 z <- f x y
1069 return $ Yield x (s', z)
1070 Skip s' -> return $ Skip (s', x)
1071 Done -> return Done
1072
1073 -- | Suffix scan
1074 postscanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
1075 {-# INLINE postscanl #-}
1076 postscanl f = postscanlM (\a b -> return (f a b))
1077
1078 -- | Suffix scan with a monadic operator
1079 postscanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
1080 {-# INLINE_STREAM postscanlM #-}
1081 postscanlM f z (Stream step s sz) = Stream step' (s,z) sz
1082 where
1083 {-# INLINE_INNER step' #-}
1084 step' (s,x) = do
1085 r <- step s
1086 case r of
1087 Yield y s' -> do
1088 z <- f x y
1089 return $ Yield z (s',z)
1090 Skip s' -> return $ Skip (s',x)
1091 Done -> return Done
1092
1093 -- | Suffix scan with strict accumulator
1094 postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
1095 {-# INLINE postscanl' #-}
1096 postscanl' f = postscanlM' (\a b -> return (f a b))
1097
1098 -- | Suffix scan with strict acccumulator and a monadic operator
1099 postscanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
1100 {-# INLINE_STREAM postscanlM' #-}
1101 postscanlM' f z (Stream step s sz) = z `seq` Stream step' (s,z) sz
1102 where
1103 {-# INLINE_INNER step' #-}
1104 step' (s,x) = x `seq`
1105 do
1106 r <- step s
1107 case r of
1108 Yield y s' -> do
1109 z <- f x y
1110 z `seq` return (Yield z (s',z))
1111 Skip s' -> return $ Skip (s',x)
1112 Done -> return Done
1113
1114 -- | Haskell-style scan
1115 scanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
1116 {-# INLINE scanl #-}
1117 scanl f = scanlM (\a b -> return (f a b))
1118
1119 -- | Haskell-style scan with a monadic operator
1120 scanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
1121 {-# INLINE scanlM #-}
1122 scanlM f z s = z `cons` postscanlM f z s
1123
1124 -- | Haskell-style scan with strict accumulator
1125 scanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
1126 {-# INLINE scanl' #-}
1127 scanl' f = scanlM' (\a b -> return (f a b))
1128
1129 -- | Haskell-style scan with strict accumulator and a monadic operator
1130 scanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
1131 {-# INLINE scanlM' #-}
1132 scanlM' f z s = z `seq` (z `cons` postscanlM f z s)
1133
1134 -- | Scan over a non-empty 'Stream'
1135 scanl1 :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
1136 {-# INLINE scanl1 #-}
1137 scanl1 f = scanl1M (\x y -> return (f x y))
1138
1139 -- | Scan over a non-empty 'Stream' with a monadic operator
1140 scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
1141 {-# INLINE_STREAM scanl1M #-}
1142 scanl1M f (Stream step s sz) = Stream step' (s, Nothing) sz
1143 where
1144 {-# INLINE_INNER step' #-}
1145 step' (s, Nothing) = do
1146 r <- step s
1147 case r of
1148 Yield x s' -> return $ Yield x (s', Just x)
1149 Skip s' -> return $ Skip (s', Nothing)
1150 Done -> BOUNDS_ERROR(emptyStream) "scanl1M"
1151
1152 step' (s, Just x) = do
1153 r <- step s
1154 case r of
1155 Yield y s' -> do
1156 z <- f x y
1157 return $ Yield z (s', Just z)
1158 Skip s' -> return $ Skip (s', Just x)
1159 Done -> return Done
1160
1161 -- | Scan over a non-empty 'Stream' with a strict accumulator
1162 scanl1' :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
1163 {-# INLINE scanl1' #-}
1164 scanl1' f = scanl1M' (\x y -> return (f x y))
1165
1166 -- | Scan over a non-empty 'Stream' with a strict accumulator and a monadic
1167 -- operator
1168 scanl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
1169 {-# INLINE_STREAM scanl1M' #-}
1170 scanl1M' f (Stream step s sz) = Stream step' (s, Nothing) sz
1171 where
1172 {-# INLINE_INNER step' #-}
1173 step' (s, Nothing) = do
1174 r <- step s
1175 case r of
1176 Yield x s' -> x `seq` return (Yield x (s', Just x))
1177 Skip s' -> return $ Skip (s', Nothing)
1178 Done -> BOUNDS_ERROR(emptyStream) "scanl1M"
1179
1180 step' (s, Just x) = x `seq`
1181 do
1182 r <- step s
1183 case r of
1184 Yield y s' -> do
1185 z <- f x y
1186 z `seq` return (Yield z (s', Just z))
1187 Skip s' -> return $ Skip (s', Just x)
1188 Done -> return Done
1189
1190 -- Enumerations
1191 -- ------------
1192
1193 -- The Enum class is broken for this, there just doesn't seem to be a
1194 -- way to implement this generically. We have to specialise for as many types
1195 -- as we can but this doesn't help in polymorphic loops.
1196
1197 -- | Yield a 'Stream' of the given length containing the values @x@, @x+y@,
1198 -- @x+y+y@ etc.
1199 enumFromStepN :: (Num a, Monad m) => a -> a -> Int -> Stream m a
1200 {-# INLINE_STREAM enumFromStepN #-}
1201 enumFromStepN x y n = x `seq` y `seq` n `seq`
1202 Stream step (x,n) (Exact (delay_inline max n 0))
1203 where
1204 {-# INLINE_INNER step #-}
1205 step (x,n) | n > 0 = return $ Yield x (x+y,n-1)
1206 | otherwise = return $ Done
1207
1208 -- | Enumerate values
1209 --
1210 -- /WARNING:/ This operation can be very inefficient. If at all possible, use
1211 -- 'enumFromStepN' instead.
1212 enumFromTo :: (Enum a, Monad m) => a -> a -> Stream m a
1213 {-# INLINE_STREAM enumFromTo #-}
1214 enumFromTo x y = fromList [x .. y]
1215
1216 -- NOTE: We use (x+1) instead of (succ x) below because the latter checks for
1217 -- overflow which can't happen here.
1218
1219 -- FIXME: add "too large" test for Int
1220 enumFromTo_small :: (Integral a, Monad m) => a -> a -> Stream m a
1221 {-# INLINE_STREAM enumFromTo_small #-}
1222 enumFromTo_small x y = x `seq` y `seq` Stream step x (Exact n)
1223 where
1224 n = delay_inline max (fromIntegral y - fromIntegral x + 1) 0
1225
1226 {-# INLINE_INNER step #-}
1227 step x | x <= y = return $ Yield x (x+1)
1228 | otherwise = return $ Done
1229
1230 {-# RULES
1231
1232 "enumFromTo<Int8> [Stream]"
1233 enumFromTo = enumFromTo_small :: Monad m => Int8 -> Int8 -> Stream m Int8
1234
1235 "enumFromTo<Int16> [Stream]"
1236 enumFromTo = enumFromTo_small :: Monad m => Int16 -> Int16 -> Stream m Int16
1237
1238 "enumFromTo<Word8> [Stream]"
1239 enumFromTo = enumFromTo_small :: Monad m => Word8 -> Word8 -> Stream m Word8
1240
1241 "enumFromTo<Word16> [Stream]"
1242 enumFromTo = enumFromTo_small :: Monad m => Word16 -> Word16 -> Stream m Word16
1243
1244 #-}
1245
1246 #if WORD_SIZE_IN_BITS > 32
1247
1248 {-# RULES
1249
1250 "enumFromTo<Int32> [Stream]"
1251 enumFromTo = enumFromTo_small :: Monad m => Int32 -> Int32 -> Stream m Int32
1252
1253 "enumFromTo<Word32> [Stream]"
1254 enumFromTo = enumFromTo_small :: Monad m => Word32 -> Word32 -> Stream m Word32
1255
1256 #-}
1257
1258 #endif
1259
1260 -- NOTE: We could implement a generic "too large" test:
1261 --
1262 -- len x y | x > y = 0
1263 -- | n > 0 && n <= fromIntegral (maxBound :: Int) = fromIntegral n
1264 -- | otherwise = error
1265 -- where
1266 -- n = y-x+1
1267 --
1268 -- Alas, GHC won't eliminate unnecessary comparisons (such as n >= 0 for
1269 -- unsigned types). See http://hackage.haskell.org/trac/ghc/ticket/3744
1270 --
1271
1272 enumFromTo_int :: (Integral a, Monad m) => a -> a -> Stream m a
1273 {-# INLINE_STREAM enumFromTo_int #-}
1274 enumFromTo_int x y = x `seq` y `seq` Stream step x (Exact (len x y))
1275 where
1276 {-# INLINE [0] len #-}
1277 len x y | x > y = 0
1278 | otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
1279 (n > 0)
1280 $ fromIntegral n
1281 where
1282 n = y-x+1
1283
1284 {-# INLINE_INNER step #-}
1285 step x | x <= y = return $ Yield x (x+1)
1286 | otherwise = return $ Done
1287
1288 {-# RULES
1289
1290 "enumFromTo<Int> [Stream]"
1291 enumFromTo = enumFromTo_int :: Monad m => Int -> Int -> Stream m Int
1292
1293 #if WORD_SIZE_IN_BITS > 32
1294
1295 "enumFromTo<Int64> [Stream]"
1296 enumFromTo = enumFromTo_int :: Monad m => Int64 -> Int64 -> Stream m Int64
1297
1298 #else
1299
1300 "enumFromTo<Int32> [Stream]"
1301 enumFromTo = enumFromTo_int :: Monad m => Int32 -> Int32 -> Stream m Int32
1302
1303 #endif
1304
1305 #-}
1306
1307 enumFromTo_big_word :: (Integral a, Monad m) => a -> a -> Stream m a
1308 {-# INLINE_STREAM enumFromTo_big_word #-}
1309 enumFromTo_big_word x y = x `seq` y `seq` Stream step x (Exact (len x y))
1310 where
1311 {-# INLINE [0] len #-}
1312 len x y | x > y = 0
1313 | otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
1314 (n < fromIntegral (maxBound :: Int))
1315 $ fromIntegral (n+1)
1316 where
1317 n = y-x
1318
1319 {-# INLINE_INNER step #-}
1320 step x | x <= y = return $ Yield x (x+1)
1321 | otherwise = return $ Done
1322
1323 {-# RULES
1324
1325 "enumFromTo<Word> [Stream]"
1326 enumFromTo = enumFromTo_big_word :: Monad m => Word -> Word -> Stream m Word
1327
1328 "enumFromTo<Word64> [Stream]"
1329 enumFromTo = enumFromTo_big_word
1330 :: Monad m => Word64 -> Word64 -> Stream m Word64
1331
1332 #if WORD_SIZE_IN_BITS == 32
1333
1334 "enumFromTo<Word32> [Stream]"
1335 enumFromTo = enumFromTo_big_word
1336 :: Monad m => Word32 -> Word32 -> Stream m Word32
1337
1338 #endif
1339
1340 "enumFromTo<Integer> [Stream]"
1341 enumFromTo = enumFromTo_big_word
1342 :: Monad m => Integer -> Integer -> Stream m Integer
1343
1344 #-}
1345
1346 -- FIXME: the "too large" test is totally wrong
1347 enumFromTo_big_int :: (Integral a, Monad m) => a -> a -> Stream m a
1348 {-# INLINE_STREAM enumFromTo_big_int #-}
1349 enumFromTo_big_int x y = x `seq` y `seq` Stream step x (Exact (len x y))
1350 where
1351 {-# INLINE [0] len #-}
1352 len x y | x > y = 0
1353 | otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
1354 (n > 0 && n <= fromIntegral (maxBound :: Int))
1355 $ fromIntegral n
1356 where
1357 n = y-x+1
1358
1359 {-# INLINE_INNER step #-}
1360 step x | x <= y = return $ Yield x (x+1)
1361 | otherwise = return $ Done
1362
1363 #if WORD_SIZE_IN_BITS > 32
1364
1365 {-# RULES
1366
1367 "enumFromTo<Int64> [Stream]"
1368 enumFromTo = enumFromTo_big :: Monad m => Int64 -> Int64 -> Stream m Int64
1369
1370 #-}
1371
1372 #endif
1373
1374 enumFromTo_char :: Monad m => Char -> Char -> Stream m Char
1375 {-# INLINE_STREAM enumFromTo_char #-}
1376 enumFromTo_char x y = x `seq` y `seq` Stream step xn (Exact n)
1377 where
1378 xn = ord x
1379 yn = ord y
1380
1381 n = delay_inline max 0 (yn - xn + 1)
1382
1383 {-# INLINE_INNER step #-}
1384 step xn | xn <= yn = return $ Yield (unsafeChr xn) (xn+1)
1385 | otherwise = return $ Done
1386
1387 {-# RULES
1388
1389 "enumFromTo<Char> [Stream]"
1390 enumFromTo = enumFromTo_char
1391
1392 #-}
1393
1394 ------------------------------------------------------------------------
1395
1396 -- Specialise enumFromTo for Float and Double.
1397 -- Also, try to do something about pairs?
1398
1399 enumFromTo_double :: (Monad m, Ord a, RealFrac a) => a -> a -> Stream m a
1400 {-# INLINE_STREAM enumFromTo_double #-}
1401 enumFromTo_double n m = n `seq` m `seq` Stream step n (Max (len n m))
1402 where
1403 lim = m + 1/2 -- important to float out
1404
1405 {-# INLINE [0] len #-}
1406 len x y | x > y = 0
1407 | otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
1408 (n > 0)
1409 $ fromIntegral n
1410 where
1411 n = truncate (y-x)+2
1412
1413 {-# INLINE_INNER step #-}
1414 step x | x <= lim = return $ Yield x (x+1)
1415 | otherwise = return $ Done
1416
1417 {-# RULES
1418
1419 "enumFromTo<Double> [Stream]"
1420 enumFromTo = enumFromTo_double :: Monad m => Double -> Double -> Stream m Double
1421
1422 "enumFromTo<Float> [Stream]"
1423 enumFromTo = enumFromTo_double :: Monad m => Float -> Float -> Stream m Float
1424
1425 #-}
1426
1427 ------------------------------------------------------------------------
1428
1429 -- | Enumerate values with a given step.
1430 --
1431 -- /WARNING:/ This operation is very inefficient. If at all possible, use
1432 -- 'enumFromStepN' instead.
1433 enumFromThenTo :: (Enum a, Monad m) => a -> a -> a -> Stream m a
1434 {-# INLINE_STREAM enumFromThenTo #-}
1435 enumFromThenTo x y z = fromList [x, y .. z]
1436
1437 -- FIXME: Specialise enumFromThenTo.
1438
1439 -- Conversions
1440 -- -----------
1441
1442 -- | Convert a 'Stream' to a list
1443 toList :: Monad m => Stream m a -> m [a]
1444 {-# INLINE toList #-}
1445 toList = foldr (:) []
1446
1447 -- | Convert a list to a 'Stream'
1448 fromList :: Monad m => [a] -> Stream m a
1449 {-# INLINE fromList #-}
1450 fromList xs = unsafeFromList Unknown xs
1451
1452 -- | Convert the first @n@ elements of a list to a 'Stream'
1453 fromListN :: Monad m => Int -> [a] -> Stream m a
1454 {-# INLINE_STREAM fromListN #-}
1455 fromListN n xs = Stream step (xs,n) (Max (delay_inline max n 0))
1456 where
1457 {-# INLINE_INNER step #-}
1458 step (xs,n) | n <= 0 = return Done
1459 step (x:xs,n) = return (Yield x (xs,n-1))
1460 step ([],n) = return Done
1461
1462 -- | Convert a list to a 'Stream' with the given 'Size' hint.
1463 unsafeFromList :: Monad m => Size -> [a] -> Stream m a
1464 {-# INLINE_STREAM unsafeFromList #-}
1465 unsafeFromList sz xs = Stream step xs sz
1466 where
1467 step (x:xs) = return (Yield x xs)
1468 step [] = return Done
1469