#include "phases.h"
module Data.Vector.Fusion.Stream.Monadic (
Stream(..), Step(..),
size, sized,
length, null,
empty, singleton, cons, snoc, replicate, (++),
head, last, (!!),
extract, init, tail, take, drop,
map, mapM, mapM_, trans, concatMap,
zipWith, zipWithM, zipWith3, zipWith3M,
filter, filterM, takeWhile, takeWhileM, dropWhile, dropWhileM,
elem, notElem, find, findM, findIndex, findIndexM,
foldl, foldlM, foldM, foldl1, foldl1M,
foldl', foldlM', foldl1', foldl1M',
foldr, foldrM, foldr1, foldr1M,
and, or, concatMapM,
unfoldr, unfoldrM,
prescanl, prescanlM, prescanl', prescanlM',
toList, fromList
) where
import Data.Vector.Fusion.Stream.Size
import Control.Monad ( liftM )
import Prelude hiding ( length, null,
replicate, (++),
head, last, (!!),
init, tail, take, drop,
map, mapM, mapM_, concatMap,
zipWith, zipWith3,
filter, takeWhile, dropWhile,
elem, notElem,
foldl, foldl1, foldr, foldr1,
and, or )
import qualified Prelude
data Step s a = Yield a s
| Skip s
| Done
data Stream m a = forall s. Stream (s -> m (Step s a)) s Size
size :: Stream m a -> Size
size (Stream _ _ sz) = sz
sized :: Stream m a -> Size -> Stream m a
sized (Stream step s _) sz = Stream step s sz
length :: Monad m => Stream m a -> m Int
length s = foldl' (\n _ -> n+1) 0 s
null :: Monad m => Stream m a -> m Bool
null s = foldr (\_ _ -> False) True s
empty :: Monad m => Stream m a
empty = Stream (const (return Done)) () (Exact 0)
singleton :: Monad m => a -> Stream m a
singleton x = Stream (return . step) True (Exact 1)
where
step True = Yield x False
step False = Done
replicate :: Monad m => Int -> a -> Stream m a
replicate n x = Stream (return . step) n (Exact (max n 0))
where
step i | i > 0 = Yield x (i1)
| otherwise = Done
cons :: Monad m => a -> Stream m a -> Stream m a
cons x s = singleton x ++ s
snoc :: Monad m => Stream m a -> a -> Stream m a
snoc s x = s ++ singleton x
infixr 5 ++
(++) :: Monad m => Stream m a -> Stream m a -> Stream m a
Stream stepa sa na ++ Stream stepb sb nb = Stream step (Left sa) (na + nb)
where
step (Left sa) = do
r <- stepa sa
case r of
Yield x sa' -> return $ Yield x (Left sa')
Skip sa' -> return $ Skip (Left sa')
Done -> return $ Skip (Right sb)
step (Right sb) = do
r <- stepb sb
case r of
Yield x sb' -> return $ Yield x (Right sb')
Skip sb' -> return $ Skip (Right sb')
Done -> return $ Done
head :: Monad m => Stream m a -> m a
head (Stream step s _) = head_loop s
where
head_loop s = do
r <- step s
case r of
Yield x _ -> return x
Skip s' -> head_loop s'
Done -> errorEmptyStream "head"
last :: Monad m => Stream m a -> m a
last (Stream step s _) = last_loop0 s
where
last_loop0 s = do
r <- step s
case r of
Yield x s' -> last_loop1 x s'
Skip s' -> last_loop0 s'
Done -> errorEmptyStream "last"
last_loop1 x s = do
r <- step s
case r of
Yield y s' -> last_loop1 y s'
Skip s' -> last_loop1 x s'
Done -> return x
(!!) :: Monad m => Stream m a -> Int -> m a
Stream step s _ !! i | i < 0 = errorNegativeIndex "!!"
| otherwise = loop s i
where
loop s i = i `seq`
do
r <- step s
case r of
Yield x s' | i == 0 -> return x
| otherwise -> loop s' (i1)
Skip s' -> loop s' i
Done -> errorIndexOutOfRange "!!"
extract :: Monad m => Stream m a -> Int
-> Int
-> Stream m a
extract s i n = take n (drop i s)
init :: Monad m => Stream m a -> Stream m a
init (Stream step s sz) = Stream step' (Nothing, s) (sz 1)
where
step' (Nothing, s) = liftM (\r ->
case r of
Yield x s' -> Skip (Just x, s')
Skip s' -> Skip (Nothing, s')
Done -> errorEmptyStream "init"
) (step s)
step' (Just x, s) = liftM (\r ->
case r of
Yield y s' -> Yield x (Just y, s')
Skip s' -> Skip (Just x, s')
Done -> Done
) (step s)
tail :: Monad m => Stream m a -> Stream m a
tail (Stream step s sz) = Stream step' (Left s) (sz 1)
where
step' (Left s) = liftM (\r ->
case r of
Yield x s' -> Skip (Right s')
Skip s' -> Skip (Left s')
Done -> errorEmptyStream "tail"
) (step s)
step' (Right s) = liftM (\r ->
case r of
Yield x s' -> Yield x (Right s')
Skip s' -> Skip (Right s')
Done -> Done
) (step s)
take :: Monad m => Int -> Stream m a -> Stream m a
take n (Stream step s sz) = Stream step' (s, 0) (smaller (Exact n) sz)
where
step' (s, i) | i < n = liftM (\r ->
case r of
Yield x s' -> Yield x (s', i+1)
Skip s' -> Skip (s', i)
Done -> Done
) (step s)
step' (s, i) = return Done
drop :: Monad m => Int -> Stream m a -> Stream m a
drop n (Stream step s sz) = Stream step' (s, Just n) (sz Exact n)
where
step' (s, Just i) | i > 0 = liftM (\r ->
case r of
Yield x s' -> Skip (s', Just (i1))
Skip s' -> Skip (s', Just i)
Done -> Done
) (step s)
| otherwise = return $ Skip (s, Nothing)
step' (s, Nothing) = liftM (\r ->
case r of
Yield x s' -> Yield x (s', Nothing)
Skip s' -> Skip (s', Nothing)
Done -> Done
) (step s)
instance Monad m => Functor (Stream m) where
fmap = map
map :: Monad m => (a -> b) -> Stream m a -> Stream m b
map f = mapM (return . f)
mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
mapM f (Stream step s n) = Stream step' s n
where
step' s = do
r <- step s
case r of
Yield x s' -> liftM (`Yield` s') (f x)
Skip s' -> return (Skip s')
Done -> return Done
mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
mapM_ m (Stream step s _) = mapM_go s
where
mapM_go s = do
r <- step s
case r of
Yield x s' -> do { m x; mapM_go s' }
Skip s' -> mapM_go s'
Done -> return ()
trans :: (Monad m, Monad m') => (forall a. m a -> m' a)
-> Stream m a -> Stream m' a
trans f (Stream step s n) = Stream (f . step) s n
zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith f = zipWithM (\a b -> return (f a b))
zipWithM :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM f (Stream stepa sa na) (Stream stepb sb nb)
= Stream step (sa, sb, Nothing) (smaller na nb)
where
step (sa, sb, Nothing) = liftM (\r ->
case r of
Yield x sa' -> Skip (sa', sb, Just x)
Skip sa' -> Skip (sa', sb, Nothing)
Done -> Done
) (stepa sa)
step (sa, sb, Just x) = do
r <- stepb sb
case r of
Yield y sb' ->
do
z <- f x y
return $ Yield z (sa, sb', Nothing)
Skip sb' -> return $ Skip (sa, sb', Just x)
Done -> return $ Done
zipWith3 :: Monad m => (a -> b -> c -> d) -> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3 f = zipWith3M (\a b c -> return (f a b c))
zipWith3M :: Monad m => (a -> b -> c -> m d) -> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3M f (Stream stepa sa na) (Stream stepb sb nb) (Stream stepc sc nc)
= Stream step (sa, sb, sc, Nothing) (smaller na (smaller nb nc))
where
step (sa, sb, sc, Nothing) = do
r <- stepa sa
return $ case r of
Yield x sa' -> Skip (sa', sb, sc, Just (x, Nothing))
Skip sa' -> Skip (sa', sb, sc, Nothing)
Done -> Done
step (sa, sb, sc, Just (x, Nothing)) = do
r <- stepb sb
return $ case r of
Yield y sb' -> Skip (sa, sb', sc, Just (x, Just y))
Skip sb' -> Skip (sa, sb', sc, Just (x, Nothing))
Done -> Done
step (sa, sb, sc, Just (x, Just y)) = do
r <- stepc sc
case r of
Yield z sc' -> f x y z >>= (\res -> return $ Yield res (sa, sb, sc', Nothing))
Skip sc' -> return $ Skip (sa, sb, sc', Just (x, Just y))
Done -> return $ Done
filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
filter f = filterM (return . f)
filterM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
filterM f (Stream step s n) = Stream step' s (toMax n)
where
step' s = do
r <- step s
case r of
Yield x s' -> do
b <- f x
return $ if b then Yield x s'
else Skip s'
Skip s' -> return $ Skip s'
Done -> return $ Done
takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
takeWhile f = takeWhileM (return . f)
takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
takeWhileM f (Stream step s n) = Stream step' s (toMax n)
where
step' s = do
r <- step s
case r of
Yield x s' -> do
b <- f x
return $ if b then Yield x s' else Done
Skip s' -> return $ Skip s'
Done -> return $ Done
dropWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
dropWhile f = dropWhileM (return . f)
data DropWhile s a = DropWhile_Drop s | DropWhile_Yield a s | DropWhile_Next s
dropWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
dropWhileM f (Stream step s n) = Stream step' (DropWhile_Drop s) (toMax n)
where
step' (DropWhile_Drop s)
= do
r <- step s
case r of
Yield x s' -> do
b <- f x
return $ if b then Skip (DropWhile_Drop s')
else Skip (DropWhile_Yield x s')
Skip s' -> return $ Skip (DropWhile_Drop s')
Done -> return $ Done
step' (DropWhile_Yield x s) = return $ Yield x (DropWhile_Next s)
step' (DropWhile_Next s)
= liftM (\r ->
case r of
Yield x s' -> Skip (DropWhile_Yield x s')
Skip s' -> Skip (DropWhile_Next s')
Done -> Done
) (step s)
infix 4 `elem`
elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
elem x (Stream step s _) = elem_loop s
where
elem_loop s = do
r <- step s
case r of
Yield y s' | x == y -> return True
| otherwise -> elem_loop s'
Skip s' -> elem_loop s'
Done -> return False
infix 4 `notElem`
notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
notElem x s = liftM not (elem x s)
find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
find f = findM (return . f)
findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)
findM f (Stream step s _) = find_loop s
where
find_loop s = do
r <- step s
case r of
Yield x s' -> do
b <- f x
if b then return $ Just x
else find_loop s'
Skip s' -> find_loop s'
Done -> return Nothing
findIndex :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe Int)
findIndex f = findIndexM (return . f)
findIndexM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe Int)
findIndexM f (Stream step s _) = findIndex_loop s 0
where
findIndex_loop s i = do
r <- step s
case r of
Yield x s' -> do
b <- f x
if b then return $ Just i
else findIndex_loop s' (i+1)
Skip s' -> findIndex_loop s' i
Done -> return Nothing
foldl :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
foldl f = foldlM (\a b -> return (f a b))
foldlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
foldlM m z (Stream step s _) = foldlM_go z s
where
foldlM_go z s = do
r <- step s
case r of
Yield x s' -> do { z' <- m z x; foldlM_go z' s' }
Skip s' -> foldlM_go z s'
Done -> return z
foldM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
foldM = foldlM
foldl1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
foldl1 f = foldl1M (\a b -> return (f a b))
foldl1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
foldl1M f (Stream step s sz) = foldl1M_go s
where
foldl1M_go s = do
r <- step s
case r of
Yield x s' -> foldlM f x (Stream step s' (sz 1))
Skip s' -> foldl1M_go s'
Done -> errorEmptyStream "foldl1M"
foldl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
foldl' f = foldlM' (\a b -> return (f a b))
foldlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
foldlM' m z (Stream step s _) = foldlM'_go z s
where
foldlM'_go z s = z `seq`
do
r <- step s
case r of
Yield x s' -> do { z' <- m z x; foldlM'_go z' s' }
Skip s' -> foldlM'_go z s'
Done -> return z
foldl1' :: Monad m => (a -> a -> a) -> Stream m a -> m a
foldl1' f = foldl1M' (\a b -> return (f a b))
foldl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m a
foldl1M' f (Stream step s sz) = foldl1M'_go s
where
foldl1M'_go s = do
r <- step s
case r of
Yield x s' -> foldlM' f x (Stream step s' (sz 1))
Skip s' -> foldl1M'_go s'
Done -> errorEmptyStream "foldl1M'"
foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
foldr f = foldrM (\a b -> return (f a b))
foldrM :: Monad m => (a -> b -> m b) -> b -> Stream m a -> m b
foldrM f z (Stream step s _) = foldrM_go s
where
foldrM_go s = do
r <- step s
case r of
Yield x s' -> f x =<< foldrM_go s'
Skip s' -> foldrM_go s'
Done -> return z
foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
foldr1 f = foldr1M (\a b -> return (f a b))
foldr1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
foldr1M f (Stream step s _) = foldr1M_go0 s
where
foldr1M_go0 s = do
r <- step s
case r of
Yield x s' -> foldr1M_go1 x s'
Skip s' -> foldr1M_go0 s'
Done -> errorEmptyStream "foldr1M"
foldr1M_go1 x s = do
r <- step s
case r of
Yield y s' -> f x =<< foldr1M_go1 y s'
Skip s' -> foldr1M_go1 x s'
Done -> return x
and :: Monad m => Stream m Bool -> m Bool
and (Stream step s _) = and_go s
where
and_go s = do
r <- step s
case r of
Yield False _ -> return False
Yield True s' -> and_go s'
Skip s' -> and_go s'
Done -> return True
or :: Monad m => Stream m Bool -> m Bool
or (Stream step s _) = or_go s
where
or_go s = do
r <- step s
case r of
Yield False s' -> or_go s'
Yield True _ -> return True
Skip s' -> or_go s'
Done -> return False
concatMap :: Monad m => (a -> Stream m b) -> Stream m a -> Stream m b
concatMap f = concatMapM (return . f)
concatMapM :: Monad m => (a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM f (Stream step s _) = Stream concatMap_go (Left s) Unknown
where
concatMap_go (Left s) = do
r <- step s
case r of
Yield a s' -> do
b_stream <- f a
return $ Skip (Right (b_stream, s'))
Skip s' -> return $ Skip (Left s')
Done -> return Done
concatMap_go (Right (Stream inner_step inner_s sz, s)) = do
r <- inner_step inner_s
case r of
Yield b inner_s' -> return $ Yield b (Right (Stream inner_step inner_s' sz, s))
Skip inner_s' -> return $ Skip (Right (Stream inner_step inner_s' sz, s))
Done -> return $ Skip (Left s)
unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
unfoldr f = unfoldrM (return . f)
unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrM f s = Stream step s Unknown
where
step s = liftM (\r ->
case r of
Just (x, s') -> Yield x s'
Nothing -> Done
) (f s)
prescanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
prescanl f = prescanlM (\a b -> return (f a b))
prescanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM f z (Stream step s sz) = Stream step' (s,z) sz
where
step' (s,x) = do
r <- step s
case r of
Yield y s' -> do
z <- f x y
return $ Yield x (s', z)
Skip s' -> return $ Skip (s', x)
Done -> return Done
prescanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
prescanl' f = prescanlM' (\a b -> return (f a b))
prescanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM' f z (Stream step s sz) = Stream step' (s,z) sz
where
step' (s,x) = x `seq`
do
r <- step s
case r of
Yield y s' -> do
z <- f x y
return $ Yield x (s', z)
Skip s' -> return $ Skip (s', x)
Done -> return Done
toList :: Monad m => Stream m a -> m [a]
toList = foldr (:) []
fromList :: Monad m => [a] -> Stream m a
fromList xs = Stream step xs Unknown
where
step (x:xs) = return (Yield x xs)
step [] = return Done
streamError :: String -> String -> a
streamError fn msg = error $ "Data.Vector.Fusion.Stream.Monadic."
Prelude.++ fn Prelude.++ ": " Prelude.++ msg
errorEmptyStream :: String -> a
errorEmptyStream fn = streamError fn "empty stream"
errorNegativeIndex :: String -> a
errorNegativeIndex fn = streamError fn "negative index"
errorIndexOutOfRange :: String -> a
errorIndexOutOfRange fn = streamError fn "index out of range"