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