{-# LANGUAGE CPP, ExistentialQuantification, MultiParamTypeClasses, FlexibleInstances, Rank2Types, BangPatterns, KindSignatures, GADTs, ScopedTypeVariables #-}

-- |
-- Module      : Data.Vector.Fusion.Stream.Monadic
-- Copyright   : (c) Roman Leshchinskiy 2008-2010
-- License     : BSD-style
--
-- Maintainer  : Roman Leshchinskiy <rl@cse.unsw.edu.au>
-- Stability   : experimental
-- Portability : non-portable
--
-- Monadic stream combinators.
--

module Data.Vector.Fusion.Stream.Monadic (
  Stream(..), Step(..), SPEC(..),

  -- * Length
  length, null,

  -- * Construction
  empty, singleton, cons, snoc, replicate, replicateM, generate, generateM, (++),

  -- * Accessing elements
  head, last, (!!), (!?),

  -- * Substreams
  slice, init, tail, take, drop,

  -- * Mapping
  map, mapM, mapM_, trans, unbox, concatMap, flatten,

  -- * Zipping
  indexed, indexedR, zipWithM_,
  zipWithM, zipWith3M, zipWith4M, zipWith5M, zipWith6M,
  zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
  zip, zip3, zip4, zip5, zip6,

  -- * Comparisons
  eqBy, cmpBy,

  -- * Filtering
  filter, filterM, uniq, mapMaybe, mapMaybeM, catMaybes, takeWhile, takeWhileM, dropWhile, dropWhileM,

  -- * Searching
  elem, notElem, find, findM, findIndex, findIndexM,

  -- * Folding
  foldl, foldlM, foldl1, foldl1M, foldM, fold1M,
  foldl', foldlM', foldl1', foldl1M', foldM', fold1M',
  foldr, foldrM, foldr1, foldr1M,

  -- * Specialised folds
  and, or, concatMapM,

  -- * Unfolding
  unfoldr, unfoldrM,
  unfoldrN, unfoldrNM,
  unfoldrExactN, unfoldrExactNM,
  iterateN, iterateNM,

  -- * Scans
  prescanl, prescanlM, prescanl', prescanlM',
  postscanl, postscanlM, postscanl', postscanlM',
  scanl, scanlM, scanl', scanlM',
  scanl1, scanl1M, scanl1', scanl1M',

  -- * Enumerations
  enumFromStepN, enumFromTo, enumFromThenTo,

  -- * Conversions
  toList, fromList, fromListN
) where

import Data.Vector.Fusion.Util ( Box(..) )

import Data.Char      ( ord )
import GHC.Base       ( unsafeChr )
import Control.Monad  ( liftM )
import Prelude hiding ( length, null,
                        replicate, (++),
                        head, last, (!!),
                        init, tail, take, drop,
                        map, mapM, mapM_, concatMap,
                        zipWith, zipWith3, zip, zip3,
                        filter, takeWhile, dropWhile,
                        elem, notElem,
                        foldl, foldl1, foldr, foldr1,
                        and, or,
                        scanl, scanl1,
                        enumFromTo, enumFromThenTo )

import Data.Int  ( Int8, Int16, Int32 )
import Data.Word ( Word8, Word16, Word32, Word64 )

#if !MIN_VERSION_base(4,8,0)
import Data.Word ( Word8, Word16, Word32, Word, Word64 )
#endif

#if __GLASGOW_HASKELL__ >= 708
import GHC.Types ( SPEC(..) )
#elif __GLASGOW_HASKELL__ >= 700
import GHC.Exts ( SpecConstrAnnotation(..) )
#endif

#include "vector.h"
#include "MachDeps.h"

#if WORD_SIZE_IN_BITS > 32
import Data.Int  ( Int64 )
#endif

#if __GLASGOW_HASKELL__ < 708
data SPEC = SPEC | SPEC2
#if __GLASGOW_HASKELL__ >= 700
{-# ANN type SPEC ForceSpecConstr #-}
#endif
#endif

emptyStream :: String
{-# NOINLINE emptyStream #-}
emptyStream :: String
emptyStream = String
"empty stream"

#define EMPTY_STREAM (\state -> ERROR state emptyStream)

-- | Result of taking a single step in a stream
data Step s a where
  Yield :: a -> s -> Step s a
  Skip  :: s -> Step s a
  Done  :: Step s a

instance Functor (Step s) where
  {-# INLINE fmap #-}
  fmap :: forall a b. (a -> b) -> Step s a -> Step s b
fmap a -> b
f (Yield a
x s
s) = forall a s. a -> s -> Step s a
Yield (a -> b
f a
x) s
s
  fmap a -> b
_ (Skip s
s) = forall s a. s -> Step s a
Skip s
s
  fmap a -> b
_ Step s a
Done = forall s a. Step s a
Done
#if MIN_VERSION_base(4,8,0)
  {-# INLINE (<$) #-}
  <$ :: forall a b. a -> Step s b -> Step s a
(<$) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const
#endif

-- | Monadic streams
data Stream m a = forall s. Stream (s -> m (Step s a)) s

-- Length
-- ------

-- | Length of a 'Stream'
length :: Monad m => Stream m a -> m Int
{-# INLINE_FUSED length #-}
length :: forall (m :: * -> *) a. Monad m => Stream m a -> m Int
length = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
foldl' (\Int
n a
_ -> Int
nforall a. Num a => a -> a -> a
+Int
1) Int
0

-- | Check if a 'Stream' is empty
null :: Monad m => Stream m a -> m Bool
{-# INLINE_FUSED null #-}
null :: forall (m :: * -> *) a. Monad m => Stream m a -> m Bool
null (Stream s -> m (Step s a)
step s
t) = s -> m Bool
null_loop s
t
  where
    null_loop :: s -> m Bool
null_loop s
s = do
      Step s a
r <- s -> m (Step s a)
step s
s
      case Step s a
r of
        Yield a
_ s
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
        Skip s
s'   -> s -> m Bool
null_loop s
s'
        Step s a
Done      -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True

-- Construction
-- ------------

-- | Empty 'Stream'
empty :: Monad m => Stream m a
{-# INLINE_FUSED empty #-}
empty :: forall (m :: * -> *) a. Monad m => Stream m a
empty = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (forall a b. a -> b -> a
const (forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done)) ()

-- | Singleton 'Stream'
singleton :: Monad m => a -> Stream m a
{-# INLINE_FUSED singleton #-}
singleton :: forall (m :: * -> *) a. Monad m => a -> Stream m a
singleton a
x = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Step Bool a
step) Bool
True
  where
    {-# INLINE_INNER step #-}
    step :: Bool -> Step Bool a
step Bool
True  = forall a s. a -> s -> Step s a
Yield a
x Bool
False
    step Bool
False = forall s a. Step s a
Done

-- | Replicate a value to a given length
replicate :: Monad m => Int -> a -> Stream m a
{-# INLINE_FUSED replicate #-}
replicate :: forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
replicate Int
n a
x = forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
replicateM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return a
x)

-- | Yield a 'Stream' of values obtained by performing the monadic action the
-- given number of times
replicateM :: Monad m => Int -> m a -> Stream m a
{-# INLINE_FUSED replicateM #-}
replicateM :: forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
replicateM Int
n m a
p = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Int -> m (Step Int a)
step Int
n
  where
    {-# INLINE_INNER step #-}
    step :: Int -> m (Step Int a)
step Int
i | Int
i forall a. Ord a => a -> a -> Bool
<= Int
0    = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done
           | Bool
otherwise = do { a
x <- m a
p; forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (Int
iforall a. Num a => a -> a -> a
-Int
1) }

generate :: Monad m => Int -> (Int -> a) -> Stream m a
{-# INLINE generate #-}
generate :: forall (m :: * -> *) a. Monad m => Int -> (Int -> a) -> Stream m a
generate Int
n Int -> a
f = forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
generateM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
f)

-- | Generate a stream from its indices
generateM :: Monad m => Int -> (Int -> m a) -> Stream m a
{-# INLINE_FUSED generateM #-}
generateM :: forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
generateM Int
n Int -> m a
f = Int
n seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Int -> m (Step Int a)
step Int
0
  where
    {-# INLINE_INNER step #-}
    step :: Int -> m (Step Int a)
step Int
i | Int
i forall a. Ord a => a -> a -> Bool
< Int
n     = do
                           a
x <- Int -> m a
f Int
i
                           forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (Int
iforall a. Num a => a -> a -> a
+Int
1)
           | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | Prepend an element
cons :: Monad m => a -> Stream m a -> Stream m a
{-# INLINE cons #-}
cons :: forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
cons a
x Stream m a
s = forall (m :: * -> *) a. Monad m => a -> Stream m a
singleton a
x forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
++ Stream m a
s

-- | Append an element
snoc :: Monad m => Stream m a -> a -> Stream m a
{-# INLINE snoc #-}
snoc :: forall (m :: * -> *) a. Monad m => Stream m a -> a -> Stream m a
snoc Stream m a
s a
x = Stream m a
s forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
++ forall (m :: * -> *) a. Monad m => a -> Stream m a
singleton a
x

infixr 5 ++
-- | Concatenate two 'Stream's
(++) :: Monad m => Stream m a -> Stream m a -> Stream m a
{-# INLINE_FUSED (++) #-}
Stream s -> m (Step s a)
stepa s
ta ++ :: forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
++ Stream s -> m (Step s a)
stepb s
tb = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s s -> m (Step (Either s s) a)
step (forall a b. a -> Either a b
Left s
ta)
  where
    {-# INLINE_INNER step #-}
    step :: Either s s -> m (Step (Either s s) a)
step (Left  s
sa) = do
                        Step s a
r <- s -> m (Step s a)
stepa s
sa
                        case Step s a
r of
                          Yield a
x s
sa' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (forall a b. a -> Either a b
Left  s
sa')
                          Skip    s
sa' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip    (forall a b. a -> Either a b
Left  s
sa')
                          Step s a
Done        -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip    (forall a b. b -> Either a b
Right s
tb)
    step (Right s
sb) = do
                        Step s a
r <- s -> m (Step s a)
stepb s
sb
                        case Step s a
r of
                          Yield a
x s
sb' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (forall a b. b -> Either a b
Right s
sb')
                          Skip    s
sb' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip    (forall a b. b -> Either a b
Right s
sb')
                          Step s a
Done        -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

-- Accessing elements
-- ------------------

-- | First element of the 'Stream' or error if empty
head :: Monad m => Stream m a -> m a
{-# INLINE_FUSED head #-}
head :: forall (m :: * -> *) a. Monad m => Stream m a -> m a
head (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
head_loop SPEC
SPEC s
t
  where
    head_loop :: SPEC -> s -> m a
head_loop !SPEC
_ s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
_  -> forall (m :: * -> *) a. Monad m => a -> m a
return a
x
            Skip    s
s' -> SPEC -> s -> m a
head_loop SPEC
SPEC s
s'
            Step s a
Done       -> EMPTY_STREAM "head"



-- | Last element of the 'Stream' or error if empty
last :: Monad m => Stream m a -> m a
{-# INLINE_FUSED last #-}
last :: forall (m :: * -> *) a. Monad m => Stream m a -> m a
last (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
last_loop0 SPEC
SPEC s
t
  where
    last_loop0 :: SPEC -> s -> m a
last_loop0 !SPEC
_ s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' -> SPEC -> a -> s -> m a
last_loop1 SPEC
SPEC a
x s
s'
            Skip    s
s' -> SPEC -> s -> m a
last_loop0 SPEC
SPEC   s
s'
            Step s a
Done       -> EMPTY_STREAM "last"

    last_loop1 :: SPEC -> a -> s -> m a
last_loop1 !SPEC
_ a
x s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
y s
s' -> SPEC -> a -> s -> m a
last_loop1 SPEC
SPEC a
y s
s'
            Skip    s
s' -> SPEC -> a -> s -> m a
last_loop1 SPEC
SPEC a
x s
s'
            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return a
x

infixl 9 !!
-- | Element at the given position
(!!) :: Monad m => Stream m a -> Int -> m a
{-# INLINE (!!) #-}
Stream s -> m (Step s a)
step s
t !! :: forall (m :: * -> *) a. Monad m => Stream m a -> Int -> m a
!! Int
j | Int
j forall a. Ord a => a -> a -> Bool
< Int
0     = ERROR "!!" "negative index"
                   | Bool
otherwise = SPEC -> s -> Int -> m a
index_loop SPEC
SPEC s
t Int
j
  where
    index_loop :: SPEC -> s -> Int -> m a
index_loop !SPEC
_ s
s Int
i
      = Int
i seq :: forall a b. a -> b -> b
`seq`
        do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' | Int
i forall a. Eq a => a -> a -> Bool
== Int
0    -> forall (m :: * -> *) a. Monad m => a -> m a
return a
x
                       | Bool
otherwise -> SPEC -> s -> Int -> m a
index_loop SPEC
SPEC s
s' (Int
iforall a. Num a => a -> a -> a
-Int
1)
            Skip    s
s'             -> SPEC -> s -> Int -> m a
index_loop SPEC
SPEC s
s' Int
i
            Step s a
Done                   -> EMPTY_STREAM "!!"

infixl 9 !?
-- | Element at the given position or 'Nothing' if out of bounds
(!?) :: Monad m => Stream m a -> Int -> m (Maybe a)
{-# INLINE (!?) #-}
Stream s -> m (Step s a)
step s
t !? :: forall (m :: * -> *) a. Monad m => Stream m a -> Int -> m (Maybe a)
!? Int
j = SPEC -> s -> Int -> m (Maybe a)
index_loop SPEC
SPEC s
t Int
j
  where
    index_loop :: SPEC -> s -> Int -> m (Maybe a)
index_loop !SPEC
_ s
s Int
i
      = Int
i seq :: forall a b. a -> b -> b
`seq`
        do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' | Int
i forall a. Eq a => a -> a -> Bool
== Int
0    -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
x)
                       | Bool
otherwise -> SPEC -> s -> Int -> m (Maybe a)
index_loop SPEC
SPEC s
s' (Int
iforall a. Num a => a -> a -> a
-Int
1)
            Skip    s
s'             -> SPEC -> s -> Int -> m (Maybe a)
index_loop SPEC
SPEC s
s' Int
i
            Step s a
Done                   -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing

-- Substreams
-- ----------

-- | Extract a substream of the given length starting at the given position.
slice :: Monad m => Int   -- ^ starting index
                 -> Int   -- ^ length
                 -> Stream m a
                 -> Stream m a
{-# INLINE slice #-}
slice :: forall (m :: * -> *) a.
Monad m =>
Int -> Int -> Stream m a -> Stream m a
slice Int
i Int
n Stream m a
s = forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
take Int
n (forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
drop Int
i Stream m a
s)

-- | All but the last element
init :: Monad m => Stream m a -> Stream m a
{-# INLINE_FUSED init #-}
init :: forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
init (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (forall a. Maybe a
Nothing, s
t)
  where
    {-# INLINE_INNER step' #-}
    step' :: (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (Maybe a
Nothing, s
s) = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
                           case Step s a
r of
                             Yield a
x s
s' -> forall s a. s -> Step s a
Skip (forall a. a -> Maybe a
Just a
x,  s
s')
                             Skip    s
s' -> forall s a. s -> Step s a
Skip (forall a. Maybe a
Nothing, s
s')
                             Step s a
Done       -> EMPTY_STREAM "init"
                         ) (s -> m (Step s a)
step s
s)

    step' (Just a
x,  s
s) = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
                           case Step s a
r of
                             Yield a
y s
s' -> forall a s. a -> s -> Step s a
Yield a
x (forall a. a -> Maybe a
Just a
y, s
s')
                             Skip    s
s' -> forall s a. s -> Step s a
Skip    (forall a. a -> Maybe a
Just a
x, s
s')
                             Step s a
Done       -> forall s a. Step s a
Done
                         ) (s -> m (Step s a)
step s
s)

-- | All but the first element
tail :: Monad m => Stream m a -> Stream m a
{-# INLINE_FUSED tail #-}
tail :: forall (m :: * -> *) a. Monad m => Stream m a -> Stream m a
tail (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s s -> m (Step (Either s s) a)
step' (forall a b. a -> Either a b
Left s
t)
  where
    {-# INLINE_INNER step' #-}
    step' :: Either s s -> m (Step (Either s s) a)
step' (Left  s
s) = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
                        case Step s a
r of
                          Yield a
_ s
s' -> forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right s
s')
                          Skip    s
s' -> forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left  s
s')
                          Step s a
Done       -> EMPTY_STREAM "tail"
                      ) (s -> m (Step s a)
step s
s)

    step' (Right s
s) = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
                        case Step s a
r of
                          Yield a
x s
s' -> forall a s. a -> s -> Step s a
Yield a
x (forall a b. b -> Either a b
Right s
s')
                          Skip    s
s' -> forall s a. s -> Step s a
Skip    (forall a b. b -> Either a b
Right s
s')
                          Step s a
Done       -> forall s a. Step s a
Done
                      ) (s -> m (Step s a)
step s
s)

-- | The first @n@ elements
take :: Monad m => Int -> Stream m a -> Stream m a
{-# INLINE_FUSED take #-}
take :: forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
take Int
n (Stream s -> m (Step s a)
step s
t) = Int
n seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) a)
step' (s
t, Int
0)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, Int) -> m (Step (s, Int) a)
step' (s
s, Int
i) | Int
i forall a. Ord a => a -> a -> Bool
< Int
n = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
                             case Step s a
r of
                               Yield a
x s
s' -> forall a s. a -> s -> Step s a
Yield a
x (s
s', Int
iforall a. Num a => a -> a -> a
+Int
1)
                               Skip    s
s' -> forall s a. s -> Step s a
Skip    (s
s', Int
i)
                               Step s a
Done       -> forall s a. Step s a
Done
                           ) (s -> m (Step s a)
step s
s)
    step' (s
_, Int
_) = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | All but the first @n@ elements
drop :: Monad m => Int -> Stream m a -> Stream m a
{-# INLINE_FUSED drop #-}
drop :: forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
drop Int
n (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Maybe Int) -> m (Step (s, Maybe Int) a)
step' (s
t, forall a. a -> Maybe a
Just Int
n)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, Maybe Int) -> m (Step (s, Maybe Int) a)
step' (s
s, Just Int
i) | Int
i forall a. Ord a => a -> a -> Bool
> Int
0 = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
                                case Step s a
r of
                                   Yield a
_ s
s' -> forall s a. s -> Step s a
Skip (s
s', forall a. a -> Maybe a
Just (Int
iforall a. Num a => a -> a -> a
-Int
1))
                                   Skip    s
s' -> forall s a. s -> Step s a
Skip (s
s', forall a. a -> Maybe a
Just Int
i)
                                   Step s a
Done       -> forall s a. Step s a
Done
                                ) (s -> m (Step s a)
step s
s)
                      | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s, forall a. Maybe a
Nothing)

    step' (s
s, Maybe Int
Nothing) = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
                           case Step s a
r of
                             Yield a
x s
s' -> forall a s. a -> s -> Step s a
Yield a
x (s
s', forall a. Maybe a
Nothing)
                             Skip    s
s' -> forall s a. s -> Step s a
Skip    (s
s', forall a. Maybe a
Nothing)
                             Step s a
Done       -> forall s a. Step s a
Done
                           ) (s -> m (Step s a)
step s
s)

-- Mapping
-- -------

instance Monad m => Functor (Stream m) where
  {-# INLINE fmap #-}
  fmap :: forall a b. (a -> b) -> Stream m a -> Stream m b
fmap = forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
map

-- | Map a function over a 'Stream'
map :: Monad m => (a -> b) -> Stream m a -> Stream m b
{-# INLINE map #-}
map :: forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
map a -> b
f = forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)


-- | Map a monadic function over a 'Stream'
mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
{-# INLINE_FUSED mapM #-}
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM a -> m b
f (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
step' s
t
  where
    {-# INLINE_INNER step' #-}
    step' :: s -> m (Step s b)
step' s
s = do
                Step s a
r <- s -> m (Step s a)
step s
s
                case Step s a
r of
                  Yield a
x s
s' -> forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM  (forall a s. a -> s -> Step s a
`Yield` s
s') (a -> m b
f a
x)
                  Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. s -> Step s a
Skip    s
s')
                  Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

consume :: Monad m => Stream m a -> m ()
{-# INLINE_FUSED consume #-}
consume :: forall (m :: * -> *) a. Monad m => Stream m a -> m ()
consume (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m ()
consume_loop SPEC
SPEC s
t
  where
    consume_loop :: SPEC -> s -> m ()
consume_loop !SPEC
_ s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
_ s
s' -> SPEC -> s -> m ()
consume_loop SPEC
SPEC s
s'
            Skip    s
s' -> SPEC -> s -> m ()
consume_loop SPEC
SPEC s
s'
            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Execute a monadic action for each element of the 'Stream'
mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
{-# INLINE_FUSED mapM_ #-}
mapM_ :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> m ()
mapM_ a -> m b
m = forall (m :: * -> *) a. Monad m => Stream m a -> m ()
consume forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM a -> m b
m

-- | Transform a 'Stream' to use a different monad
trans :: (Monad m, Monad m')
      => (forall z. m z -> m' z) -> Stream m a -> Stream m' a
{-# INLINE_FUSED trans #-}
trans :: forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
trans forall z. m z -> m' z
f (Stream s -> m (Step s a)
step s
s) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (forall z. m z -> m' z
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> m (Step s a)
step) s
s

unbox :: Monad m => Stream m (Box a) -> Stream m a
{-# INLINE_FUSED unbox #-}
unbox :: forall (m :: * -> *) a. Monad m => Stream m (Box a) -> Stream m a
unbox (Stream s -> m (Step s (Box a))
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step' s
t
  where
    {-# INLINE_INNER step' #-}
    step' :: s -> m (Step s a)
step' s
s = do
                Step s (Box a)
r <- s -> m (Step s (Box a))
step s
s
                case Step s (Box a)
r of
                  Yield (Box a
x) s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x s
s'
                  Skip          s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip    s
s'
                  Step s (Box a)
Done             -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

-- Zipping
-- -------

-- | Pair each element in a 'Stream' with its index
indexed :: Monad m => Stream m a -> Stream m (Int,a)
{-# INLINE_FUSED indexed #-}
indexed :: forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
indexed (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) (Int, a))
step' (s
t,Int
0)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, Int) -> m (Step (s, Int) (Int, a))
step' (s
s,Int
i) = Int
i seq :: forall a b. a -> b -> b
`seq`
                  do
                    Step s a
r <- s -> m (Step s a)
step s
s
                    case Step s a
r of
                      Yield a
x s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield (Int
i,a
x) (s
s', Int
iforall a. Num a => a -> a -> a
+Int
1)
                      Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip        (s
s', Int
i)
                      Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | Pair each element in a 'Stream' with its index, starting from the right
-- and counting down
indexedR :: Monad m => Int -> Stream m a -> Stream m (Int,a)
{-# INLINE_FUSED indexedR #-}
indexedR :: forall (m :: * -> *) a.
Monad m =>
Int -> Stream m a -> Stream m (Int, a)
indexedR Int
m (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) (Int, a))
step' (s
t,Int
m)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, Int) -> m (Step (s, Int) (Int, a))
step' (s
s,Int
i) = Int
i seq :: forall a b. a -> b -> b
`seq`
                  do
                    Step s a
r <- s -> m (Step s a)
step s
s
                    case Step s a
r of
                      Yield a
x s
s' -> let i' :: Int
i' = Int
iforall a. Num a => a -> a -> a
-Int
1
                                    in
                                    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield (Int
i',a
x) (s
s', Int
i')
                      Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip         (s
s', Int
i)
                      Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | Zip two 'Stream's with the given monadic function
zipWithM :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
{-# INLINE_FUSED zipWithM #-}
zipWithM :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM a -> b -> m c
f (Stream s -> m (Step s a)
stepa s
ta) (Stream s -> m (Step s b)
stepb s
tb) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, s, Maybe a) -> m (Step (s, s, Maybe a) c)
step (s
ta, s
tb, forall a. Maybe a
Nothing)
  where
    {-# INLINE_INNER step #-}
    step :: (s, s, Maybe a) -> m (Step (s, s, Maybe a) c)
step (s
sa, s
sb, Maybe a
Nothing) = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
                               case Step s a
r of
                                 Yield a
x s
sa' -> forall s a. s -> Step s a
Skip (s
sa', s
sb, forall a. a -> Maybe a
Just a
x)
                                 Skip    s
sa' -> forall s a. s -> Step s a
Skip (s
sa', s
sb, forall a. Maybe a
Nothing)
                                 Step s a
Done        -> forall s a. Step s a
Done
                             ) (s -> m (Step s a)
stepa s
sa)

    step (s
sa, s
sb, Just a
x)  = do
                               Step s b
r <- s -> m (Step s b)
stepb s
sb
                               case Step s b
r of
                                 Yield b
y s
sb' ->
                                   do
                                     c
z <- a -> b -> m c
f a
x b
y
                                     forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield c
z (s
sa, s
sb', forall a. Maybe a
Nothing)
                                 Skip    s
sb' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
sa, s
sb', forall a. a -> Maybe a
Just a
x)
                                 Step s b
Done        -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

zipWithM_ :: Monad m => (a -> b -> m c) -> Stream m a -> Stream m b -> m ()
{-# INLINE zipWithM_ #-}
zipWithM_ :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> m ()
zipWithM_ a -> b -> m c
f Stream m a
sa Stream m b
sb = forall (m :: * -> *) a. Monad m => Stream m a -> m ()
consume (forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM a -> b -> m c
f Stream m a
sa Stream m b
sb)

zipWith3M :: Monad m => (a -> b -> c -> m d) -> Stream m a -> Stream m b -> Stream m c -> Stream m d
{-# INLINE_FUSED zipWith3M #-}
zipWith3M :: forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3M a -> b -> c -> m d
f (Stream s -> m (Step s a)
stepa s
ta)
            (Stream s -> m (Step s b)
stepb s
tb)
            (Stream s -> m (Step s c)
stepc s
tc) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, s, s, Maybe (a, Maybe b))
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
step (s
ta, s
tb, s
tc, forall a. Maybe a
Nothing)
  where
    {-# INLINE_INNER step #-}
    step :: (s, s, s, Maybe (a, Maybe b))
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
step (s
sa, s
sb, s
sc, Maybe (a, Maybe b)
Nothing) = do
        Step s a
r <- s -> m (Step s a)
stepa s
sa
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s a
r of
            Yield a
x s
sa' -> forall s a. s -> Step s a
Skip (s
sa', s
sb, s
sc, forall a. a -> Maybe a
Just (a
x, forall a. Maybe a
Nothing))
            Skip    s
sa' -> forall s a. s -> Step s a
Skip (s
sa', s
sb, s
sc, forall a. Maybe a
Nothing)
            Step s a
Done        -> forall s a. Step s a
Done

    step (s
sa, s
sb, s
sc, Just (a
x, Maybe b
Nothing)) = do
        Step s b
r <- s -> m (Step s b)
stepb s
sb
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s b
r of
            Yield b
y s
sb' -> forall s a. s -> Step s a
Skip (s
sa, s
sb', s
sc, forall a. a -> Maybe a
Just (a
x, forall a. a -> Maybe a
Just b
y))
            Skip    s
sb' -> forall s a. s -> Step s a
Skip (s
sa, s
sb', s
sc, forall a. a -> Maybe a
Just (a
x, forall a. Maybe a
Nothing))
            Step s b
Done        -> forall s a. Step s a
Done

    step (s
sa, s
sb, s
sc, Just (a
x, Just b
y)) = do
        Step s c
r <- s -> m (Step s c)
stepc s
sc
        case Step s c
r of
            Yield c
z s
sc' -> a -> b -> c -> m d
f a
x b
y c
z forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\d
res -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield d
res (s
sa, s
sb, s
sc', forall a. Maybe a
Nothing))
            Skip    s
sc' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
sa, s
sb, s
sc', forall a. a -> Maybe a
Just (a
x, forall a. a -> Maybe a
Just b
y))
            Step s c
Done        -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

zipWith4M :: Monad m => (a -> b -> c -> d -> m e)
                     -> Stream m a -> Stream m b -> Stream m c -> Stream m d
                     -> Stream m e
{-# INLINE zipWith4M #-}
zipWith4M :: forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> m e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
zipWith4M a -> b -> c -> d -> m e
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd
  = forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM (\(a
a,b
b) (c
c,d
d) -> a -> b -> c -> d -> m e
f a
a b
b c
c d
d) (forall (m :: * -> *) a b.
Monad m =>
Stream m a -> Stream m b -> Stream m (a, b)
zip Stream m a
sa Stream m b
sb) (forall (m :: * -> *) a b.
Monad m =>
Stream m a -> Stream m b -> Stream m (a, b)
zip Stream m c
sc Stream m d
sd)

zipWith5M :: Monad m => (a -> b -> c -> d -> e -> m f)
                     -> Stream m a -> Stream m b -> Stream m c -> Stream m d
                     -> Stream m e -> Stream m f
{-# INLINE zipWith5M #-}
zipWith5M :: forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> m f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
zipWith5M a -> b -> c -> d -> e -> m f
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se
  = forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM (\(a
a,b
b,c
c) (d
d,e
e) -> a -> b -> c -> d -> e -> m f
f a
a b
b c
c d
d e
e) (forall (m :: * -> *) a b c.
Monad m =>
Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
zip3 Stream m a
sa Stream m b
sb Stream m c
sc) (forall (m :: * -> *) a b.
Monad m =>
Stream m a -> Stream m b -> Stream m (a, b)
zip Stream m d
sd Stream m e
se)

zipWith6M :: Monad m => (a -> b -> c -> d -> e -> f -> m g)
                     -> Stream m a -> Stream m b -> Stream m c -> Stream m d
                     -> Stream m e -> Stream m f -> Stream m g
{-# INLINE zipWith6M #-}
zipWith6M :: forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> m g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
zipWith6M a -> b -> c -> d -> e -> f -> m g
fn Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se Stream m f
sf
  = forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM (\(a
a,b
b,c
c) (d
d,e
e,f
f) -> a -> b -> c -> d -> e -> f -> m g
fn a
a b
b c
c d
d e
e f
f) (forall (m :: * -> *) a b c.
Monad m =>
Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
zip3 Stream m a
sa Stream m b
sb Stream m c
sc)
                                                  (forall (m :: * -> *) a b c.
Monad m =>
Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
zip3 Stream m d
sd Stream m e
se Stream m f
sf)

zipWith :: Monad m => (a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
{-# INLINE zipWith #-}
zipWith :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith a -> b -> c
f = forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
zipWithM (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c
f a
a b
b))

zipWith3 :: Monad m => (a -> b -> c -> d)
                    -> Stream m a -> Stream m b -> Stream m c -> Stream m d
{-# INLINE zipWith3 #-}
zipWith3 :: forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3 a -> b -> c -> d
f = forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3M (\a
a b
b c
c -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c -> d
f a
a b
b c
c))

zipWith4 :: Monad m => (a -> b -> c -> d -> e)
                    -> Stream m a -> Stream m b -> Stream m c -> Stream m d
                    -> Stream m e
{-# INLINE zipWith4 #-}
zipWith4 :: forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
zipWith4 a -> b -> c -> d -> e
f = forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> m e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
zipWith4M (\a
a b
b c
c d
d -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c -> d -> e
f a
a b
b c
c d
d))

zipWith5 :: Monad m => (a -> b -> c -> d -> e -> f)
                    -> Stream m a -> Stream m b -> Stream m c -> Stream m d
                    -> Stream m e -> Stream m f
{-# INLINE zipWith5 #-}
zipWith5 :: forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
zipWith5 a -> b -> c -> d -> e -> f
f = forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> m f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
zipWith5M (\a
a b
b c
c d
d e
e -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c -> d -> e -> f
f a
a b
b c
c d
d e
e))

zipWith6 :: Monad m => (a -> b -> c -> d -> e -> f -> g)
                    -> Stream m a -> Stream m b -> Stream m c -> Stream m d
                    -> Stream m e -> Stream m f -> Stream m g
{-# INLINE zipWith6 #-}
zipWith6 :: forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
zipWith6 a -> b -> c -> d -> e -> f -> g
fn = forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> m g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
zipWith6M (\a
a b
b c
c d
d e
e f
f -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c -> d -> e -> f -> g
fn a
a b
b c
c d
d e
e f
f))

zip :: Monad m => Stream m a -> Stream m b -> Stream m (a,b)
{-# INLINE zip #-}
zip :: forall (m :: * -> *) a b.
Monad m =>
Stream m a -> Stream m b -> Stream m (a, b)
zip = forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
zipWith (,)

zip3 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m (a,b,c)
{-# INLINE zip3 #-}
zip3 :: forall (m :: * -> *) a b c.
Monad m =>
Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
zip3 = forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
zipWith3 (,,)

zip4 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d
                -> Stream m (a,b,c,d)
{-# INLINE zip4 #-}
zip4 :: forall (m :: * -> *) a b c d.
Monad m =>
Stream m a
-> Stream m b -> Stream m c -> Stream m d -> Stream m (a, b, c, d)
zip4 = forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
zipWith4 (,,,)

zip5 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d
                -> Stream m e -> Stream m (a,b,c,d,e)
{-# INLINE zip5 #-}
zip5 :: forall (m :: * -> *) a b c d e.
Monad m =>
Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m (a, b, c, d, e)
zip5 = forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
zipWith5 (,,,,)

zip6 :: Monad m => Stream m a -> Stream m b -> Stream m c -> Stream m d
                -> Stream m e -> Stream m f -> Stream m (a,b,c,d,e,f)
{-# INLINE zip6 #-}
zip6 :: forall (m :: * -> *) a b c d e f.
Monad m =>
Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m (a, b, c, d, e, f)
zip6 = forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
zipWith6 (,,,,,)

-- Comparisons
-- -----------

-- | Check if two 'Stream's are equal
eqBy :: (Monad m) => (a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
{-# INLINE_FUSED eqBy #-}
eqBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
eqBy a -> b -> Bool
eq (Stream s -> m (Step s a)
step1 s
t1) (Stream s -> m (Step s b)
step2 s
t2) = SPEC -> s -> s -> m Bool
eq_loop0 SPEC
SPEC s
t1 s
t2
  where
    eq_loop0 :: SPEC -> s -> s -> m Bool
eq_loop0 !SPEC
_ s
s1 s
s2 = do
      Step s a
r <- s -> m (Step s a)
step1 s
s1
      case Step s a
r of
        Yield a
x s
s1' -> SPEC -> a -> s -> s -> m Bool
eq_loop1 SPEC
SPEC a
x s
s1' s
s2
        Skip    s
s1' -> SPEC -> s -> s -> m Bool
eq_loop0 SPEC
SPEC   s
s1' s
s2
        Step s a
Done        -> s -> m Bool
eq_null s
s2

    eq_loop1 :: SPEC -> a -> s -> s -> m Bool
eq_loop1 !SPEC
_ a
x s
s1 s
s2 = do
      Step s b
r <- s -> m (Step s b)
step2 s
s2
      case Step s b
r of
        Yield b
y s
s2'
          | a -> b -> Bool
eq a
x b
y    -> SPEC -> s -> s -> m Bool
eq_loop0 SPEC
SPEC   s
s1 s
s2'
          | Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
        Skip    s
s2'   -> SPEC -> a -> s -> s -> m Bool
eq_loop1 SPEC
SPEC a
x s
s1 s
s2'
        Step s b
Done          -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

    eq_null :: s -> m Bool
eq_null s
s2 = do
      Step s b
r <- s -> m (Step s b)
step2 s
s2
      case Step s b
r of
        Yield b
_ s
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
        Skip s
s2'  -> s -> m Bool
eq_null s
s2'
        Step s b
Done      -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True

-- | Lexicographically compare two 'Stream's
cmpBy :: (Monad m) => (a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
{-# INLINE_FUSED cmpBy #-}
cmpBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
cmpBy a -> b -> Ordering
cmp (Stream s -> m (Step s a)
step1 s
t1) (Stream s -> m (Step s b)
step2 s
t2) = SPEC -> s -> s -> m Ordering
cmp_loop0 SPEC
SPEC s
t1 s
t2
  where
    cmp_loop0 :: SPEC -> s -> s -> m Ordering
cmp_loop0 !SPEC
_ s
s1 s
s2 = do
      Step s a
r <- s -> m (Step s a)
step1 s
s1
      case Step s a
r of
        Yield a
x s
s1' -> SPEC -> a -> s -> s -> m Ordering
cmp_loop1 SPEC
SPEC a
x s
s1' s
s2
        Skip    s
s1' -> SPEC -> s -> s -> m Ordering
cmp_loop0 SPEC
SPEC   s
s1' s
s2
        Step s a
Done        -> s -> m Ordering
cmp_null s
s2

    cmp_loop1 :: SPEC -> a -> s -> s -> m Ordering
cmp_loop1 !SPEC
_ a
x s
s1 s
s2 = do
      Step s b
r <- s -> m (Step s b)
step2 s
s2
      case Step s b
r of
        Yield b
y s
s2' -> case a
x a -> b -> Ordering
`cmp` b
y of
                         Ordering
EQ -> SPEC -> s -> s -> m Ordering
cmp_loop0 SPEC
SPEC s
s1 s
s2'
                         Ordering
c  -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
c
        Skip    s
s2' -> SPEC -> a -> s -> s -> m Ordering
cmp_loop1 SPEC
SPEC a
x s
s1 s
s2'
        Step s b
Done        -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
GT

    cmp_null :: s -> m Ordering
cmp_null s
s2 = do
      Step s b
r <- s -> m (Step s b)
step2 s
s2
      case Step s b
r of
        Yield b
_ s
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
LT
        Skip s
s2'  -> s -> m Ordering
cmp_null s
s2'
        Step s b
Done      -> forall (m :: * -> *) a. Monad m => a -> m a
return Ordering
EQ

-- Filtering
-- ---------

-- | Drop elements which do not satisfy the predicate
filter :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
{-# INLINE filter #-}
filter :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
filter a -> Bool
f = forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
filterM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

mapMaybe :: Monad m => (a -> Maybe b) -> Stream m a -> Stream m b
{-# INLINE_FUSED mapMaybe #-}
mapMaybe :: forall (m :: * -> *) a b.
Monad m =>
(a -> Maybe b) -> Stream m a -> Stream m b
mapMaybe a -> Maybe b
f (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
step' s
t
  where
    {-# INLINE_INNER step' #-}
    step' :: s -> m (Step s b)
step' s
s = do
                Step s a
r <- s -> m (Step s a)
step s
s
                case Step s a
r of
                  Yield a
x s
s' -> do
                                  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case a -> Maybe b
f a
x of
                                    Maybe b
Nothing -> forall s a. s -> Step s a
Skip s
s'
                                    Just b
b' -> forall a s. a -> s -> Step s a
Yield b
b' s
s'
                  Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip s
s'
                  Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

catMaybes :: Monad m => Stream m (Maybe a) -> Stream m a
catMaybes :: forall (m :: * -> *) a. Monad m => Stream m (Maybe a) -> Stream m a
catMaybes = forall (m :: * -> *) a b.
Monad m =>
(a -> Maybe b) -> Stream m a -> Stream m b
mapMaybe forall a. a -> a
id

-- | Drop elements which do not satisfy the monadic predicate
filterM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
{-# INLINE_FUSED filterM #-}
filterM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
filterM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step' s
t
  where
    {-# INLINE_INNER step' #-}
    step' :: s -> m (Step s a)
step' s
s = do
                Step s a
r <- s -> m (Step s a)
step s
s
                case Step s a
r of
                  Yield a
x s
s' -> do
                                  Bool
b <- a -> m Bool
f a
x
                                  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if Bool
b then forall a s. a -> s -> Step s a
Yield a
x s
s'
                                                else forall s a. s -> Step s a
Skip    s
s'
                  Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip s
s'
                  Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

-- | Apply monadic function to each element and drop all Nothings
--
-- @since 0.12.2.0
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
{-# INLINE_FUSED mapMaybeM #-}
mapMaybeM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> Stream m a -> Stream m b
mapMaybeM a -> m (Maybe b)
f (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
step' s
t
  where
    {-# INLINE_INNER step' #-}
    step' :: s -> m (Step s b)
step' s
s = do
                Step s a
r <- s -> m (Step s a)
step s
s
                case Step s a
r of
                  Yield a
x s
s' -> do
                                  Maybe b
fx <- a -> m (Maybe b)
f a
x
                                  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe b
fx of
                                    Maybe b
Nothing -> forall s a. s -> Step s a
Skip s
s'
                                    Just b
b  -> forall a s. a -> s -> Step s a
Yield b
b s
s'
                  Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip s
s'
                  Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

-- | Drop repeated adjacent elements.
uniq :: (Eq a, Monad m) => Stream m a -> Stream m a
{-# INLINE_FUSED uniq #-}
uniq :: forall a (m :: * -> *). (Eq a, Monad m) => Stream m a -> Stream m a
uniq (Stream s -> m (Step s a)
step s
st) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (forall a. Maybe a
Nothing,s
st)
  where
    {-# INLINE_INNER step' #-}
    step' :: (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (Maybe a
Nothing, s
s) = do Step s a
r <- s -> m (Step s a)
step s
s
                            case Step s a
r of
                              Yield a
x s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (forall a. a -> Maybe a
Just a
x , s
s')
                              Skip  s
s'   -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip  (forall a. Maybe a
Nothing, s
s')
                              Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return   forall s a. Step s a
Done
    step' (Just a
x0, s
s) = do Step s a
r <- s -> m (Step s a)
step s
s
                            case Step s a
r of
                              Yield a
x s
s' | a
x forall a. Eq a => a -> a -> Bool
== a
x0   -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip    (forall a. a -> Maybe a
Just a
x0, s
s')
                                         | Bool
otherwise -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (forall a. a -> Maybe a
Just a
x , s
s')
                              Skip  s
s'   -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a. a -> Maybe a
Just a
x0, s
s')
                              Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return   forall s a. Step s a
Done

-- | Longest prefix of elements that satisfy the predicate
takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
{-# INLINE takeWhile #-}
takeWhile :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
takeWhile a -> Bool
f = forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
takeWhileM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

-- | Longest prefix of elements that satisfy the monadic predicate
takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
{-# INLINE_FUSED takeWhileM #-}
takeWhileM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
takeWhileM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step' s
t
  where
    {-# INLINE_INNER step' #-}
    step' :: s -> m (Step s a)
step' s
s = do
                Step s a
r <- s -> m (Step s a)
step s
s
                case Step s a
r of
                  Yield a
x s
s' -> do
                                  Bool
b <- a -> m Bool
f a
x
                                  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if Bool
b then forall a s. a -> s -> Step s a
Yield a
x s
s' else forall s a. Step s a
Done
                  Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip s
s'
                  Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

-- | Drop the longest prefix of elements that satisfy the predicate
dropWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
{-# INLINE dropWhile #-}
dropWhile :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
dropWhile a -> Bool
f = forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
dropWhileM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

data DropWhile s a = DropWhile_Drop s | DropWhile_Yield a s | DropWhile_Next s

-- | Drop the longest prefix of elements that satisfy the monadic predicate
dropWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
{-# INLINE_FUSED dropWhileM #-}
dropWhileM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
dropWhileM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream DropWhile s a -> m (Step (DropWhile s a) a)
step' (forall s a. s -> DropWhile s a
DropWhile_Drop s
t)
  where
    -- NOTE: we jump through hoops here to have only one Yield; local data
    -- declarations would be nice!

    {-# INLINE_INNER step' #-}
    step' :: DropWhile s a -> m (Step (DropWhile s a) a)
step' (DropWhile_Drop s
s)
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' -> do
                            Bool
b <- a -> m Bool
f a
x
                            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if Bool
b then forall s a. s -> Step s a
Skip (forall s a. s -> DropWhile s a
DropWhile_Drop    s
s')
                                          else forall s a. s -> Step s a
Skip (forall s a. a -> s -> DropWhile s a
DropWhile_Yield a
x s
s')
            Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall s a. s -> DropWhile s a
DropWhile_Drop    s
s')
            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

    step' (DropWhile_Yield a
x s
s) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (forall s a. s -> DropWhile s a
DropWhile_Next s
s)

    step' (DropWhile_Next s
s)
      = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Step s a
r ->
          case Step s a
r of
            Yield a
x s
s' -> forall s a. s -> Step s a
Skip    (forall s a. a -> s -> DropWhile s a
DropWhile_Yield a
x s
s')
            Skip    s
s' -> forall s a. s -> Step s a
Skip    (forall s a. s -> DropWhile s a
DropWhile_Next    s
s')
            Step s a
Done       -> forall s a. Step s a
Done
        ) (s -> m (Step s a)
step s
s)

-- Searching
-- ---------

infix 4 `elem`
-- | Check whether the 'Stream' contains an element
elem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
{-# INLINE_FUSED elem #-}
elem :: forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
elem a
x (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m Bool
elem_loop SPEC
SPEC s
t
  where
    elem_loop :: SPEC -> s -> m Bool
elem_loop !SPEC
_ s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
y s
s' | a
x forall a. Eq a => a -> a -> Bool
== a
y    -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                       | Bool
otherwise -> SPEC -> s -> m Bool
elem_loop SPEC
SPEC s
s'
            Skip    s
s'             -> SPEC -> s -> m Bool
elem_loop SPEC
SPEC s
s'
            Step s a
Done                   -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

infix 4 `notElem`
-- | Inverse of `elem`
notElem :: (Monad m, Eq a) => a -> Stream m a -> m Bool
{-# INLINE notElem #-}
notElem :: forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
notElem a
x Stream m a
s = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Bool -> Bool
not (forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
elem a
x Stream m a
s)

-- | Yield 'Just' the first element that satisfies the predicate or 'Nothing'
-- if no such element exists.
find :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe a)
{-# INLINE find #-}
find :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> m (Maybe a)
find a -> Bool
f = forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe a)
findM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

-- | Yield 'Just' the first element that satisfies the monadic predicate or
-- 'Nothing' if no such element exists.
findM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe a)
{-# INLINE_FUSED findM #-}
findM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe a)
findM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m (Maybe a)
find_loop SPEC
SPEC s
t
  where
    find_loop :: SPEC -> s -> m (Maybe a)
find_loop !SPEC
_ s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' -> do
                            Bool
b <- a -> m Bool
f a
x
                            if Bool
b then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just a
x
                                 else SPEC -> s -> m (Maybe a)
find_loop SPEC
SPEC s
s'
            Skip    s
s' -> SPEC -> s -> m (Maybe a)
find_loop SPEC
SPEC s
s'
            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing

-- | Yield 'Just' the index of the first element that satisfies the predicate
-- or 'Nothing' if no such element exists.
findIndex :: Monad m => (a -> Bool) -> Stream m a -> m (Maybe Int)
{-# INLINE_FUSED findIndex #-}
findIndex :: forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> m (Maybe Int)
findIndex a -> Bool
f = forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe Int)
findIndexM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

-- | Yield 'Just' the index of the first element that satisfies the monadic
-- predicate or 'Nothing' if no such element exists.
findIndexM :: Monad m => (a -> m Bool) -> Stream m a -> m (Maybe Int)
{-# INLINE_FUSED findIndexM #-}
findIndexM :: forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe Int)
findIndexM a -> m Bool
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> Int -> m (Maybe Int)
findIndex_loop SPEC
SPEC s
t Int
0
  where
    findIndex_loop :: SPEC -> s -> Int -> m (Maybe Int)
findIndex_loop !SPEC
_ s
s Int
i
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' -> do
                            Bool
b <- a -> m Bool
f a
x
                            if Bool
b then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Int
i
                                 else SPEC -> s -> Int -> m (Maybe Int)
findIndex_loop SPEC
SPEC s
s' (Int
iforall a. Num a => a -> a -> a
+Int
1)
            Skip    s
s' -> SPEC -> s -> Int -> m (Maybe Int)
findIndex_loop SPEC
SPEC s
s' Int
i
            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing

-- Folding
-- -------

-- | Left fold
foldl :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
{-# INLINE foldl #-}
foldl :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
foldl a -> b -> a
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Left fold with a monadic operator
foldlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
{-# INLINE_FUSED foldlM #-}
foldlM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM a -> b -> m a
m a
w (Stream s -> m (Step s b)
step s
t) = SPEC -> a -> s -> m a
foldlM_loop SPEC
SPEC a
w s
t
  where
    foldlM_loop :: SPEC -> a -> s -> m a
foldlM_loop !SPEC
_ a
z s
s
      = do
          Step s b
r <- s -> m (Step s b)
step s
s
          case Step s b
r of
            Yield b
x s
s' -> do { a
z' <- a -> b -> m a
m a
z b
x; SPEC -> a -> s -> m a
foldlM_loop SPEC
SPEC a
z' s
s' }
            Skip    s
s' -> SPEC -> a -> s -> m a
foldlM_loop SPEC
SPEC a
z s
s'
            Step s b
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return a
z

-- | Same as 'foldlM'
foldM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
{-# INLINE foldM #-}
foldM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldM = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM

-- | Left fold over a non-empty 'Stream'
foldl1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
{-# INLINE foldl1 #-}
foldl1 :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
foldl1 a -> a -> a
f = forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M (\a
a a
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
a a
b))

-- | Left fold over a non-empty 'Stream' with a monadic operator
foldl1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE_FUSED foldl1M #-}
foldl1M :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
foldl1M_loop SPEC
SPEC s
t
  where
    foldl1M_loop :: SPEC -> s -> m a
foldl1M_loop !SPEC
_ s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' -> forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM a -> a -> m a
f a
x (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step s
s')
            Skip    s
s' -> SPEC -> s -> m a
foldl1M_loop SPEC
SPEC s
s'
            Step s a
Done       -> EMPTY_STREAM "foldl1M"

-- | Same as 'foldl1M'
fold1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE fold1M #-}
fold1M :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
fold1M = forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M

-- | Left fold with a strict accumulator
foldl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> m a
{-# INLINE foldl' #-}
foldl' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
foldl' a -> b -> a
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM' (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Left fold with a strict accumulator and a monadic operator
foldlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
{-# INLINE_FUSED foldlM' #-}
foldlM' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM' a -> b -> m a
m a
w (Stream s -> m (Step s b)
step s
t) = SPEC -> a -> s -> m a
foldlM'_loop SPEC
SPEC a
w s
t
  where
    foldlM'_loop :: SPEC -> a -> s -> m a
foldlM'_loop !SPEC
_ a
z s
s
      = a
z seq :: forall a b. a -> b -> b
`seq`
        do
          Step s b
r <- s -> m (Step s b)
step s
s
          case Step s b
r of
            Yield b
x s
s' -> do { a
z' <- a -> b -> m a
m a
z b
x; SPEC -> a -> s -> m a
foldlM'_loop SPEC
SPEC a
z' s
s' }
            Skip    s
s' -> SPEC -> a -> s -> m a
foldlM'_loop SPEC
SPEC a
z s
s'
            Step s b
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return a
z

-- | Same as 'foldlM''
foldM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> m a
{-# INLINE foldM' #-}
foldM' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldM' = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM'

-- | Left fold over a non-empty 'Stream' with a strict accumulator
foldl1' :: Monad m => (a -> a -> a) -> Stream m a -> m a
{-# INLINE foldl1' #-}
foldl1' :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
foldl1' a -> a -> a
f = forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M' (\a
a a
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
a a
b))

-- | Left fold over a non-empty 'Stream' with a strict accumulator and a
-- monadic operator
foldl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE_FUSED foldl1M' #-}
foldl1M' :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M' a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
foldl1M'_loop SPEC
SPEC s
t
  where
    foldl1M'_loop :: SPEC -> s -> m a
foldl1M'_loop !SPEC
_ s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' -> forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM' a -> a -> m a
f a
x (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step s
s')
            Skip    s
s' -> SPEC -> s -> m a
foldl1M'_loop SPEC
SPEC s
s'
            Step s a
Done       -> EMPTY_STREAM "foldl1M'"

-- | Same as 'foldl1M''
fold1M' :: Monad m => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE fold1M' #-}
fold1M' :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
fold1M' = forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M'

-- | Right fold
foldr :: Monad m => (a -> b -> b) -> b -> Stream m a -> m b
{-# INLINE foldr #-}
foldr :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
foldr a -> b -> b
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Stream m a -> m b
foldrM (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> b
f a
a b
b))

-- | Right fold with a monadic operator
foldrM :: Monad m => (a -> b -> m b) -> b -> Stream m a -> m b
{-# INLINE_FUSED foldrM #-}
foldrM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Stream m a -> m b
foldrM a -> b -> m b
f b
z (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m b
foldrM_loop SPEC
SPEC s
t
  where
    foldrM_loop :: SPEC -> s -> m b
foldrM_loop !SPEC
_ s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' -> a -> b -> m b
f a
x forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< SPEC -> s -> m b
foldrM_loop SPEC
SPEC s
s'
            Skip    s
s' -> SPEC -> s -> m b
foldrM_loop SPEC
SPEC s
s'
            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return b
z

-- | Right fold over a non-empty stream
foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m a
{-# INLINE foldr1 #-}
foldr1 :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
foldr1 a -> a -> a
f = forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
foldr1M (\a
a a
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
a a
b))

-- | Right fold over a non-empty stream with a monadic operator
foldr1M :: Monad m => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE_FUSED foldr1M #-}
foldr1M :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
foldr1M a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = SPEC -> s -> m a
foldr1M_loop0 SPEC
SPEC s
t
  where
    foldr1M_loop0 :: SPEC -> s -> m a
foldr1M_loop0 !SPEC
_ s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
x s
s' -> SPEC -> a -> s -> m a
foldr1M_loop1 SPEC
SPEC a
x s
s'
            Skip    s
s' -> SPEC -> s -> m a
foldr1M_loop0 SPEC
SPEC   s
s'
            Step s a
Done       -> EMPTY_STREAM "foldr1M"

    foldr1M_loop1 :: SPEC -> a -> s -> m a
foldr1M_loop1 !SPEC
_ a
x s
s
      = do
          Step s a
r <- s -> m (Step s a)
step s
s
          case Step s a
r of
            Yield a
y s
s' -> a -> a -> m a
f a
x forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< SPEC -> a -> s -> m a
foldr1M_loop1 SPEC
SPEC a
y s
s'
            Skip    s
s' -> SPEC -> a -> s -> m a
foldr1M_loop1 SPEC
SPEC a
x s
s'
            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return a
x

-- Specialised folds
-- -----------------

and :: Monad m => Stream m Bool -> m Bool
{-# INLINE_FUSED and #-}
and :: forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
and (Stream s -> m (Step s Bool)
step s
t) = SPEC -> s -> m Bool
and_loop SPEC
SPEC s
t
  where
    and_loop :: SPEC -> s -> m Bool
and_loop !SPEC
_ s
s
      = do
          Step s Bool
r <- s -> m (Step s Bool)
step s
s
          case Step s Bool
r of
            Yield Bool
False s
_  -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
            Yield Bool
True  s
s' -> SPEC -> s -> m Bool
and_loop SPEC
SPEC s
s'
            Skip        s
s' -> SPEC -> s -> m Bool
and_loop SPEC
SPEC s
s'
            Step s Bool
Done           -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True

or :: Monad m => Stream m Bool -> m Bool
{-# INLINE_FUSED or #-}
or :: forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
or (Stream s -> m (Step s Bool)
step s
t) = SPEC -> s -> m Bool
or_loop SPEC
SPEC s
t
  where
    or_loop :: SPEC -> s -> m Bool
or_loop !SPEC
_ s
s
      = do
          Step s Bool
r <- s -> m (Step s Bool)
step s
s
          case Step s Bool
r of
            Yield Bool
False s
s' -> SPEC -> s -> m Bool
or_loop SPEC
SPEC s
s'
            Yield Bool
True  s
_  -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
            Skip        s
s' -> SPEC -> s -> m Bool
or_loop SPEC
SPEC s
s'
            Step s Bool
Done           -> forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

concatMap :: Monad m => (a -> Stream m b) -> Stream m a -> Stream m b
{-# INLINE concatMap #-}
concatMap :: forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b) -> Stream m a -> Stream m b
concatMap a -> Stream m b
f = forall (m :: * -> *) a b.
Monad m =>
(a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Stream m b
f)

concatMapM :: Monad m => (a -> m (Stream m b)) -> Stream m a -> Stream m b
{-# INLINE_FUSED concatMapM #-}
concatMapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM a -> m (Stream m b)
f (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s (Stream m b, s) -> m (Step (Either s (Stream m b, s)) b)
concatMap_go (forall a b. a -> Either a b
Left s
t)
  where
    concatMap_go :: Either s (Stream m b, s) -> m (Step (Either s (Stream m b, s)) b)
concatMap_go (Left s
s) = do
        Step s a
r <- s -> m (Step s a)
step s
s
        case Step s a
r of
            Yield a
a s
s' -> do
                Stream m b
b_stream <- a -> m (Stream m b)
f a
a
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (Stream m b
b_stream, s
s'))
            Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s')
            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done
    concatMap_go (Right (Stream s -> m (Step s b)
inner_step s
inner_s, s
s)) = do
        Step s b
r <- s -> m (Step s b)
inner_step s
inner_s
        case Step s b
r of
            Yield b
b s
inner_s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield b
b (forall a b. b -> Either a b
Right (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
inner_step s
inner_s', s
s))
            Skip    s
inner_s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s b)
inner_step s
inner_s', s
s))
            Step s b
Done             -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s)

-- | Create a 'Stream' of values from a 'Stream' of streamable things
flatten :: Monad m => (a -> m s) -> (s -> m (Step s b)) -> Stream m a -> Stream m b
{-# INLINE_FUSED flatten #-}
flatten :: forall (m :: * -> *) a s b.
Monad m =>
(a -> m s) -> (s -> m (Step s b)) -> Stream m a -> Stream m b
flatten a -> m s
mk s -> m (Step s b)
istep (Stream s -> m (Step s a)
ostep s
u) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s (s, s) -> m (Step (Either s (s, s)) b)
step (forall a b. a -> Either a b
Left s
u)
  where
    {-# INLINE_INNER step #-}
    step :: Either s (s, s) -> m (Step (Either s (s, s)) b)
step (Left s
t) = do
                      Step s a
r <- s -> m (Step s a)
ostep s
t
                      case Step s a
r of
                        Yield a
a s
t' -> do
                                        s
s <- a -> m s
mk a
a
                                        s
s seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (s
s,s
t')))
                        Skip    s
t' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
t')
                        Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done


    step (Right (s
s,s
t)) = do
                           Step s b
r <- s -> m (Step s b)
istep s
s
                           case Step s b
r of
                             Yield b
x s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield b
x (forall a b. b -> Either a b
Right (s
s',s
t))
                             Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip    (forall a b. b -> Either a b
Right (s
s',s
t))
                             Step s b
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip    (forall a b. a -> Either a b
Left s
t)

-- Unfolding
-- ---------

-- | Unfold
unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
{-# INLINE_FUSED unfoldr #-}
unfoldr :: forall (m :: * -> *) s a.
Monad m =>
(s -> Maybe (a, s)) -> s -> Stream m a
unfoldr s -> Maybe (a, s)
f = forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
f)

-- | Unfold with a monadic function
unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m a
{-# INLINE_FUSED unfoldrM #-}
unfoldrM :: forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrM s -> m (Maybe (a, s))
f s
t = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step s
t
  where
    {-# INLINE_INNER step #-}
    step :: s -> m (Step s a)
step s
s = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Maybe (a, s)
r ->
               case Maybe (a, s)
r of
                 Just (a
x, s
s') -> forall a s. a -> s -> Step s a
Yield a
x s
s'
                 Maybe (a, s)
Nothing      -> forall s a. Step s a
Done
             ) (s -> m (Maybe (a, s))
f s
s)

unfoldrN :: Monad m => Int -> (s -> Maybe (a, s)) -> s -> Stream m a
{-# INLINE_FUSED unfoldrN #-}
unfoldrN :: forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> Maybe (a, s)) -> s -> Stream m a
unfoldrN Int
n s -> Maybe (a, s)
f = forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrNM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
f)

-- | Unfold at most @n@ elements with a monadic function.
unfoldrNM :: Monad m => Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
{-# INLINE_FUSED unfoldrNM #-}
unfoldrNM :: forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrNM Int
m s -> m (Maybe (a, s))
f s
t = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) a)
step (s
t,Int
m)
  where
    {-# INLINE_INNER step #-}
    step :: (s, Int) -> m (Step (s, Int) a)
step (s
s,Int
n) | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0    = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done
               | Bool
otherwise = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (\Maybe (a, s)
r ->
                               case Maybe (a, s)
r of
                                 Just (a
x,s
s') -> forall a s. a -> s -> Step s a
Yield a
x (s
s',Int
nforall a. Num a => a -> a -> a
-Int
1)
                                 Maybe (a, s)
Nothing     -> forall s a. Step s a
Done
                             ) (s -> m (Maybe (a, s))
f s
s)

-- | Unfold exactly @n@ elements
--
-- @since 0.12.2.0
unfoldrExactN :: Monad m => Int -> (s -> (a, s)) -> s -> Stream m a
{-# INLINE_FUSED unfoldrExactN #-}
unfoldrExactN :: forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> (a, s)) -> s -> Stream m a
unfoldrExactN Int
n s -> (a, s)
f = forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (a, s)) -> s -> Stream m a
unfoldrExactNM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> (a, s)
f)

-- | Unfold exactly @n@ elements with a monadic function.
--
-- @since 0.12.2.0
unfoldrExactNM :: Monad m => Int -> (s -> m (a, s)) -> s -> Stream m a
{-# INLINE_FUSED unfoldrExactNM #-}
unfoldrExactNM :: forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (a, s)) -> s -> Stream m a
unfoldrExactNM Int
m s -> m (a, s)
f s
t = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Int) -> m (Step (s, Int) a)
step (s
t,Int
m)
  where
    {-# INLINE_INNER step #-}
    step :: (s, Int) -> m (Step (s, Int) a)
step (s
s,Int
n) | Int
n forall a. Ord a => a -> a -> Bool
<= Int
0    = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done
               | Bool
otherwise = do (a
x,s
s') <- s -> m (a, s)
f s
s
                                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (s
s',Int
nforall a. Num a => a -> a -> a
-Int
1)

-- | /O(n)/ Apply monadic function \(\max(n - 1, 0)\) times to an initial value,
-- producing a stream of \(\max(n, 0)\) values.
iterateNM :: Monad m => Int -> (a -> m a) -> a -> Stream m a
{-# INLINE_FUSED iterateNM #-}
iterateNM :: forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m a) -> a -> Stream m a
iterateNM Int
n a -> m a
f a
x0 = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (a, Int) -> m (Step (a, Int) a)
step (a
x0,Int
n)
  where
    {-# INLINE_INNER step #-}
    step :: (a, Int) -> m (Step (a, Int) a)
step (a
x,Int
i) | Int
i forall a. Ord a => a -> a -> Bool
<= Int
0    = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done
               | Int
i forall a. Eq a => a -> a -> Bool
== Int
n    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (a
x,Int
iforall a. Num a => a -> a -> a
-Int
1)
               | Bool
otherwise = do a
a <- a -> m a
f a
x
                                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
a (a
a,Int
iforall a. Num a => a -> a -> a
-Int
1)

-- | /O(n)/ Apply function \(\max(n - 1, 0)\) times to an initial value,
-- producing a stream of \(\max(n, 0)\) values.
iterateN :: Monad m => Int -> (a -> a) -> a -> Stream m a
{-# INLINE_FUSED iterateN #-}
iterateN :: forall (m :: * -> *) a.
Monad m =>
Int -> (a -> a) -> a -> Stream m a
iterateN Int
n a -> a
f a
x0 = forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m a) -> a -> Stream m a
iterateNM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f) a
x0

-- Scans
-- -----

-- | Prefix scan
prescanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE prescanl #-}
prescanl :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> Stream m a
prescanl a -> b -> a
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Prefix scan with a monadic operator
prescanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE_FUSED prescanlM #-}
prescanlM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM a -> b -> m a
f a
w (Stream s -> m (Step s b)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, a) -> m (Step (s, a) a)
step' (s
t,a
w)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, a) -> m (Step (s, a) a)
step' (s
s,a
x) = do
                    Step s b
r <- s -> m (Step s b)
step s
s
                    case Step s b
r of
                      Yield b
y s
s' -> do
                                      a
z <- a -> b -> m a
f a
x b
y
                                      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (s
s', a
z)
                      Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s', a
x)
                      Step s b
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | Prefix scan with strict accumulator
prescanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE prescanl' #-}
prescanl' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> Stream m a
prescanl' a -> b -> a
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM' (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Prefix scan with strict accumulator and a monadic operator
prescanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE_FUSED prescanlM' #-}
prescanlM' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM' a -> b -> m a
f a
w (Stream s -> m (Step s b)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, a) -> m (Step (s, a) a)
step' (s
t,a
w)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, a) -> m (Step (s, a) a)
step' (s
s,a
x) = a
x seq :: forall a b. a -> b -> b
`seq`
                  do
                    Step s b
r <- s -> m (Step s b)
step s
s
                    case Step s b
r of
                      Yield b
y s
s' -> do
                                      a
z <- a -> b -> m a
f a
x b
y
                                      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (s
s', a
z)
                      Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s', a
x)
                      Step s b
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | Suffix scan
postscanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE postscanl #-}
postscanl :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> Stream m a
postscanl a -> b -> a
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Suffix scan with a monadic operator
postscanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE_FUSED postscanlM #-}
postscanlM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM a -> b -> m a
f a
w (Stream s -> m (Step s b)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, a) -> m (Step (s, a) a)
step' (s
t,a
w)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, a) -> m (Step (s, a) a)
step' (s
s,a
x) = do
                    Step s b
r <- s -> m (Step s b)
step s
s
                    case Step s b
r of
                      Yield b
y s
s' -> do
                                      a
z <- a -> b -> m a
f a
x b
y
                                      forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z (s
s',a
z)
                      Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s',a
x)
                      Step s b
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | Suffix scan with strict accumulator
postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE postscanl' #-}
postscanl' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> Stream m a
postscanl' a -> b -> a
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM' (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Suffix scan with strict acccumulator and a monadic operator
postscanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE_FUSED postscanlM' #-}
postscanlM' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM' a -> b -> m a
f a
w (Stream s -> m (Step s b)
step s
t) = a
w seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, a) -> m (Step (s, a) a)
step' (s
t,a
w)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, a) -> m (Step (s, a) a)
step' (s
s,a
x) = a
x seq :: forall a b. a -> b -> b
`seq`
                  do
                    Step s b
r <- s -> m (Step s b)
step s
s
                    case Step s b
r of
                      Yield b
y s
s' -> do
                                      a
z <- a -> b -> m a
f a
x b
y
                                      a
z seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall a s. a -> s -> Step s a
Yield a
z (s
s',a
z))
                      Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s',a
x)
                      Step s b
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | Haskell-style scan
scanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE scanl #-}
scanl :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> Stream m a
scanl a -> b -> a
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Haskell-style scan with a monadic operator
scanlM :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE scanlM #-}
scanlM :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM a -> b -> m a
f a
z Stream m b
s = a
z forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
`cons` forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM a -> b -> m a
f a
z Stream m b
s

-- | Haskell-style scan with strict accumulator
scanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
{-# INLINE scanl' #-}
scanl' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> Stream m a
scanl' a -> b -> a
f = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM' (\a
a b
b -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Haskell-style scan with strict accumulator and a monadic operator
scanlM' :: Monad m => (a -> b -> m a) -> a -> Stream m b -> Stream m a
{-# INLINE scanlM' #-}
scanlM' :: forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM' a -> b -> m a
f a
z Stream m b
s = a
z seq :: forall a b. a -> b -> b
`seq` (a
z forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
`cons` forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM a -> b -> m a
f a
z Stream m b
s)

-- | Scan over a non-empty 'Stream'
scanl1 :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
{-# INLINE scanl1 #-}
scanl1 :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> Stream m a
scanl1 a -> a -> a
f = forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M (\a
x a
y -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-- | Scan over a non-empty 'Stream' with a monadic operator
scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
{-# INLINE_FUSED scanl1M #-}
scanl1M :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
t, forall a. Maybe a
Nothing)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
s, Maybe a
Nothing) = do
                           Step s a
r <- s -> m (Step s a)
step s
s
                           case Step s a
r of
                             Yield a
x s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (s
s', forall a. a -> Maybe a
Just a
x)
                             Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s', forall a. Maybe a
Nothing)
                             Step s a
Done       -> EMPTY_STREAM "scanl1M"

    step' (s
s, Just a
x) = do
                          Step s a
r <- s -> m (Step s a)
step s
s
                          case Step s a
r of
                            Yield a
y s
s' -> do
                                            a
z <- a -> a -> m a
f a
x a
y
                                            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z (s
s', forall a. a -> Maybe a
Just a
z)
                            Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s', forall a. a -> Maybe a
Just a
x)
                            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | Scan over a non-empty 'Stream' with a strict accumulator
scanl1' :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
{-# INLINE scanl1' #-}
scanl1' :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> Stream m a
scanl1' a -> a -> a
f = forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M' (\a
x a
y -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-- | Scan over a non-empty 'Stream' with a strict accumulator and a monadic
-- operator
scanl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
{-# INLINE_FUSED scanl1M' #-}
scanl1M' :: forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M' a -> a -> m a
f (Stream s -> m (Step s a)
step s
t) = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
t, forall a. Maybe a
Nothing)
  where
    {-# INLINE_INNER step' #-}
    step' :: (s, Maybe a) -> m (Step (s, Maybe a) a)
step' (s
s, Maybe a
Nothing) = do
                           Step s a
r <- s -> m (Step s a)
step s
s
                           case Step s a
r of
                             Yield a
x s
s' -> a
x seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall a s. a -> s -> Step s a
Yield a
x (s
s', forall a. a -> Maybe a
Just a
x))
                             Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s', forall a. Maybe a
Nothing)
                             Step s a
Done       -> EMPTY_STREAM "scanl1M"

    step' (s
s, Just a
x) = a
x seq :: forall a b. a -> b -> b
`seq`
                        do
                          Step s a
r <- s -> m (Step s a)
step s
s
                          case Step s a
r of
                            Yield a
y s
s' -> do
                                            a
z <- a -> a -> m a
f a
x a
y
                                            a
z seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall a s. a -> s -> Step s a
Yield a
z (s
s', forall a. a -> Maybe a
Just a
z))
                            Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s', forall a. a -> Maybe a
Just a
x)
                            Step s a
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- Enumerations
-- ------------

-- The Enum class is broken for this, there just doesn't seem to be a
-- way to implement this generically. We have to specialise for as many types
-- as we can but this doesn't help in polymorphic loops.

-- | Yield a 'Stream' of the given length containing the values @x@, @x+y@,
-- @x+y+y@ etc.
enumFromStepN :: (Num a, Monad m) => a -> a -> Int -> Stream m a
{-# INLINE_FUSED enumFromStepN #-}
enumFromStepN :: forall a (m :: * -> *).
(Num a, Monad m) =>
a -> a -> Int -> Stream m a
enumFromStepN a
x a
y Int
n = a
x seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` Int
n seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (a, Int) -> m (Step (a, Int) a)
step (a
x,Int
n)
  where
    {-# INLINE_INNER step #-}
    step :: (a, Int) -> m (Step (a, Int) a)
step (a
w,Int
m) | Int
m forall a. Ord a => a -> a -> Bool
> Int
0     = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
w (a
wforall a. Num a => a -> a -> a
+a
y,Int
mforall a. Num a => a -> a -> a
-Int
1)
               | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

-- | Enumerate values
--
-- /WARNING:/ This operation can be very inefficient. If at all possible, use
-- 'enumFromStepN' instead.
enumFromTo :: (Enum a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo #-}
enumFromTo :: forall a (m :: * -> *). (Enum a, Monad m) => a -> a -> Stream m a
enumFromTo a
x a
y = forall (m :: * -> *) a. Monad m => [a] -> Stream m a
fromList [a
x .. a
y]

-- NOTE: We use (x+1) instead of (succ x) below because the latter checks for
-- overflow which can't happen here.

-- FIXME: add "too large" test for Int
enumFromTo_small :: (Integral a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_small #-}
enumFromTo_small :: forall a (m :: * -> *).
(Integral a, Monad m) =>
a -> a -> Stream m a
enumFromTo_small a
x a
y = a
x seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (forall a. a -> Maybe a
Just a
x)
  where
    {-# INLINE_INNER step #-}
    step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing              = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done
    step (Just a
z) | a
z forall a. Eq a => a -> a -> Bool
== a
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z forall a. Maybe a
Nothing
                  | a
z forall a. Ord a => a -> a -> Bool
<  a
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z (forall a. a -> Maybe a
Just (a
zforall a. Num a => a -> a -> a
+a
1))
                  | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

{-# RULES

"enumFromTo<Int8> [Stream]"
  enumFromTo = enumFromTo_small :: Monad m => Int8 -> Int8 -> Stream m Int8

"enumFromTo<Int16> [Stream]"
  enumFromTo = enumFromTo_small :: Monad m => Int16 -> Int16 -> Stream m Int16

"enumFromTo<Word8> [Stream]"
  enumFromTo = enumFromTo_small :: Monad m => Word8 -> Word8 -> Stream m Word8

"enumFromTo<Word16> [Stream]"
  enumFromTo = enumFromTo_small :: Monad m => Word16 -> Word16 -> Stream m Word16   #-}


#if WORD_SIZE_IN_BITS > 32

{-# RULES

"enumFromTo<Int32> [Stream]"
  enumFromTo = enumFromTo_small :: Monad m => Int32 -> Int32 -> Stream m Int32

"enumFromTo<Word32> [Stream]"
  enumFromTo = enumFromTo_small :: Monad m => Word32 -> Word32 -> Stream m Word32   #-}


#endif

-- NOTE: We could implement a generic "too large" test:
--
-- len x y | x > y = 0
--         | n > 0 && n <= fromIntegral (maxBound :: Int) = fromIntegral n
--         | otherwise = error
--   where
--     n = y-x+1
--
-- Alas, GHC won't eliminate unnecessary comparisons (such as n >= 0 for
-- unsigned types). See http://hackage.haskell.org/trac/ghc/ticket/3744
--

enumFromTo_int :: forall m. Monad m => Int -> Int -> Stream m Int
{-# INLINE_FUSED enumFromTo_int #-}
enumFromTo_int :: forall (m :: * -> *). Monad m => Int -> Int -> Stream m Int
enumFromTo_int Int
x Int
y = Int
x seq :: forall a b. a -> b -> b
`seq` Int
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe Int -> m (Step (Maybe Int) Int)
step (forall a. a -> Maybe a
Just Int
x)
  where
    -- {-# INLINE [0] len #-}
    -- len :: Int -> Int -> Int
    -- len u v | u > v     = 0
    --         | otherwise = BOUNDS_CHECK(check) "enumFromTo" "vector too large"
    --                       (n > 0)
    --                     $ n
    --   where
    --     n = v-u+1

    {-# INLINE_INNER step #-}
    step :: Maybe Int -> m (Step (Maybe Int) Int)
step Maybe Int
Nothing              = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done
    step (Just Int
z) | Int
z forall a. Eq a => a -> a -> Bool
== Int
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield Int
z forall a. Maybe a
Nothing
                  | Int
z forall a. Ord a => a -> a -> Bool
<  Int
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield Int
z (forall a. a -> Maybe a
Just (Int
zforall a. Num a => a -> a -> a
+Int
1))
                  | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done


enumFromTo_intlike :: (Integral a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_intlike #-}
enumFromTo_intlike :: forall a (m :: * -> *).
(Integral a, Monad m) =>
a -> a -> Stream m a
enumFromTo_intlike a
x a
y = a
x seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (forall a. a -> Maybe a
Just a
x)
  where
    {-# INLINE_INNER step #-}
    step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing              = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done
    step (Just a
z) | a
z forall a. Eq a => a -> a -> Bool
== a
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z forall a. Maybe a
Nothing
                  | a
z forall a. Ord a => a -> a -> Bool
<  a
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z (forall a. a -> Maybe a
Just (a
zforall a. Num a => a -> a -> a
+a
1))
                  | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

{-# RULES

"enumFromTo<Int> [Stream]"
  enumFromTo = enumFromTo_int :: Monad m => Int -> Int -> Stream m Int

#if WORD_SIZE_IN_BITS > 32

"enumFromTo<Int64> [Stream]"
  enumFromTo = enumFromTo_intlike :: Monad m => Int64 -> Int64 -> Stream m Int64 #-}

#else

"enumFromTo<Int32> [Stream]"
  enumFromTo = enumFromTo_intlike :: Monad m => Int32 -> Int32 -> Stream m Int32 #-}

#endif

enumFromTo_big_word :: (Integral a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_big_word #-}
enumFromTo_big_word :: forall a (m :: * -> *).
(Integral a, Monad m) =>
a -> a -> Stream m a
enumFromTo_big_word a
x a
y = a
x seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (forall a. a -> Maybe a
Just a
x)
  where
    {-# INLINE_INNER step #-}
    step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing              = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done
    step (Just a
z) | a
z forall a. Eq a => a -> a -> Bool
== a
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z forall a. Maybe a
Nothing
                  | a
z forall a. Ord a => a -> a -> Bool
<  a
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z (forall a. a -> Maybe a
Just (a
zforall a. Num a => a -> a -> a
+a
1))
                  | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

{-# RULES

"enumFromTo<Word> [Stream]"
  enumFromTo = enumFromTo_big_word :: Monad m => Word -> Word -> Stream m Word

"enumFromTo<Word64> [Stream]"
  enumFromTo = enumFromTo_big_word
                        :: Monad m => Word64 -> Word64 -> Stream m Word64

#if WORD_SIZE_IN_BITS == 32

"enumFromTo<Word32> [Stream]"
  enumFromTo = enumFromTo_big_word
                        :: Monad m => Word32 -> Word32 -> Stream m Word32

#endif

"enumFromTo<Integer> [Stream]"
  enumFromTo = enumFromTo_big_word
                        :: Monad m => Integer -> Integer -> Stream m Integer   #-}



#if WORD_SIZE_IN_BITS > 32

-- FIXME: the "too large" test is totally wrong
enumFromTo_big_int :: (Integral a, Monad m) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_big_int #-}
enumFromTo_big_int :: forall a (m :: * -> *).
(Integral a, Monad m) =>
a -> a -> Stream m a
enumFromTo_big_int a
x a
y = a
x seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (forall a. a -> Maybe a
Just a
x)
  where
    {-# INLINE_INNER step #-}
    step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing              = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done
    step (Just a
z) | a
z forall a. Eq a => a -> a -> Bool
== a
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z forall a. Maybe a
Nothing
                  | a
z forall a. Ord a => a -> a -> Bool
<  a
y    = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
z (forall a. a -> Maybe a
Just (a
zforall a. Num a => a -> a -> a
+a
1))
                  | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

{-# RULES

"enumFromTo<Int64> [Stream]"
  enumFromTo = enumFromTo_big_int :: Monad m => Int64 -> Int64 -> Stream m Int64   #-}



#endif

enumFromTo_char :: Monad m => Char -> Char -> Stream m Char
{-# INLINE_FUSED enumFromTo_char #-}
enumFromTo_char :: forall (m :: * -> *). Monad m => Char -> Char -> Stream m Char
enumFromTo_char Char
x Char
y = Char
x seq :: forall a b. a -> b -> b
`seq` Char
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Int -> m (Step Int Char)
step Int
xn
  where
    xn :: Int
xn = Char -> Int
ord Char
x
    yn :: Int
yn = Char -> Int
ord Char
y

    {-# INLINE_INNER step #-}
    step :: Int -> m (Step Int Char)
step Int
zn | Int
zn forall a. Ord a => a -> a -> Bool
<= Int
yn  = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield (Int -> Char
unsafeChr Int
zn) (Int
znforall a. Num a => a -> a -> a
+Int
1)
            | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done

{-# RULES

"enumFromTo<Char> [Stream]"
  enumFromTo = enumFromTo_char   #-}



------------------------------------------------------------------------

-- Specialise enumFromTo for Float and Double.
-- Also, try to do something about pairs?

enumFromTo_double :: (Monad m, Ord a, RealFrac a) => a -> a -> Stream m a
{-# INLINE_FUSED enumFromTo_double #-}
enumFromTo_double :: forall (m :: * -> *) a.
(Monad m, Ord a, RealFrac a) =>
a -> a -> Stream m a
enumFromTo_double a
n a
m = a
n seq :: forall a b. a -> b -> b
`seq` a
m seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream a -> m (Step a a)
step a
ini
  where
    lim :: a
lim = a
m forall a. Num a => a -> a -> a
+ a
1forall a. Fractional a => a -> a -> a
/a
2 -- important to float out

-- GHC changed definition of Enum for Double in GHC8.6 so we have to
-- accomodate both definitions in order to preserve validity of
-- rewrite rule
--
--  ISSUE:  https://gitlab.haskell.org/ghc/ghc/issues/15081
--  COMMIT: https://gitlab.haskell.org/ghc/ghc/commit/4ffaf4b67773af4c72d92bb8b6c87b1a7d34ac0f
#if MIN_VERSION_base(4,12,0)
    ini :: a
ini = a
0
    step :: a -> m (Step a a)
step a
x | a
x' forall a. Ord a => a -> a -> Bool
<= a
lim = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x' (a
xforall a. Num a => a -> a -> a
+a
1)
           | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done
           where
             x' :: a
x' = a
x forall a. Num a => a -> a -> a
+ a
n
#else
    ini = n
    step x | x <= lim  = return $ Yield x (x+1)
           | otherwise = return $ Done
#endif

{-# RULES

"enumFromTo<Double> [Stream]"
  enumFromTo = enumFromTo_double :: Monad m => Double -> Double -> Stream m Double

"enumFromTo<Float> [Stream]"
  enumFromTo = enumFromTo_double :: Monad m => Float -> Float -> Stream m Float   #-}



------------------------------------------------------------------------

-- | Enumerate values with a given step.
--
-- /WARNING:/ This operation is very inefficient. If at all possible, use
-- 'enumFromStepN' instead.
enumFromThenTo :: (Enum a, Monad m) => a -> a -> a -> Stream m a
{-# INLINE_FUSED enumFromThenTo #-}
enumFromThenTo :: forall a (m :: * -> *).
(Enum a, Monad m) =>
a -> a -> a -> Stream m a
enumFromThenTo a
x a
y a
z = forall (m :: * -> *) a. Monad m => [a] -> Stream m a
fromList [a
x, a
y .. a
z]

-- FIXME: Specialise enumFromThenTo.

-- Conversions
-- -----------

-- | Convert a 'Stream' to a list
toList :: Monad m => Stream m a -> m [a]
{-# INLINE toList #-}
toList :: forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
toList = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
foldr (:) []

-- | Convert a list to a 'Stream'
fromList :: Monad m => [a] -> Stream m a
{-# INLINE fromList #-}
fromList :: forall (m :: * -> *) a. Monad m => [a] -> Stream m a
fromList [a]
zs = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a}. Monad m => [a] -> m (Step [a] a)
step [a]
zs
  where
    step :: [a] -> m (Step [a] a)
step (a
x:[a]
xs) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a s. a -> s -> Step s a
Yield a
x [a]
xs)
    step []     = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

-- | Convert the first @n@ elements of a list to a 'Bundle'
fromListN :: Monad m => Int -> [a] -> Stream m a
{-# INLINE_FUSED fromListN #-}
fromListN :: forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
fromListN Int
m [a]
zs = forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream forall {b} {m :: * -> *} {a}.
(Ord b, Num b, Monad m) =>
([a], b) -> m (Step ([a], b) a)
step ([a]
zs,Int
m)
  where
    {-# INLINE_INNER step #-}
    step :: ([a], b) -> m (Step ([a], b) a)
step ([a]
_, b
n) | b
n forall a. Ord a => a -> a -> Bool
<= b
0 = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done
    step (a
x:[a]
xs,b
n)        = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a s. a -> s -> Step s a
Yield a
x ([a]
xs,b
nforall a. Num a => a -> a -> a
-b
1))
    step ([],b
_)          = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

{-
fromVector :: (Monad m, Vector v a) => v a -> Stream m a
{-# INLINE_FUSED fromVector #-}
fromVector v = v `seq` n `seq` Stream (Unf step 0)
                                      (Unf vstep True)
                                      (Just v)
                                      (Exact n)
  where
    n = basicLength v

    {-# INLINE step #-}
    step i | i >= n = return Done
           | otherwise = case basicUnsafeIndexM v i of
                           Box x -> return $ Yield x (i+1)


    {-# INLINE vstep #-}
    vstep True  = return (Yield (Chunk (basicLength v) (\mv -> basicUnsafeCopy mv v)) False)
    vstep False = return Done

fromVectors :: forall m a. (Monad m, Vector v a) => [v a] -> Stream m a
{-# INLINE_FUSED fromVectors #-}
fromVectors vs = Stream (Unf pstep (Left vs))
                        (Unf vstep vs)
                        Nothing
                        (Exact n)
  where
    n = List.foldl' (\k v -> k + basicLength v) 0 vs

    pstep (Left []) = return Done
    pstep (Left (v:vs)) = basicLength v `seq` return (Skip (Right (v,0,vs)))

    pstep (Right (v,i,vs))
      | i >= basicLength v = return $ Skip (Left vs)
      | otherwise          = case basicUnsafeIndexM v i of
                               Box x -> return $ Yield x (Right (v,i+1,vs))

    -- FIXME: work around bug in GHC 7.6.1
    vstep :: [v a] -> m (Step [v a] (Chunk v a))
    vstep [] = return Done
    vstep (v:vs) = return $ Yield (Chunk (basicLength v)
                                         (\mv -> INTERNAL_CHECK(check) "concatVectors" "length mismatch"
                                                                       (M.basicLength mv == basicLength v)
                                                 $ basicUnsafeCopy mv v)) vs


concatVectors :: (Monad m, Vector v a) => Stream m (v a) -> Stream m a
{-# INLINE_FUSED concatVectors #-}
concatVectors (Stream step s}
  = Stream (Unf pstep (Left s))
           (Unf vstep s)
           Nothing
           Unknown
  where
    pstep (Left s) = do
      r <- step s
      case r of
        Yield v s' -> basicLength v `seq` return (Skip (Right (v,0,s')))
        Skip    s' -> return (Skip (Left s'))
        Done       -> return Done

    pstep (Right (v,i,s))
      | i >= basicLength v = return (Skip (Left s))
      | otherwise          = case basicUnsafeIndexM v i of
                               Box x -> return (Yield x (Right (v,i+1,s)))


    vstep s = do
      r <- step s
      case r of
        Yield v s' -> return (Yield (Chunk (basicLength v)
                                           (\mv -> INTERNAL_CHECK(check) "concatVectors" "length mismatch"
                                                                          (M.basicLength mv == basicLength v)
                                                   $ basicUnsafeCopy mv v)) s')
        Skip    s' -> return (Skip s')
        Done       -> return Done

reVector :: Monad m => Stream m a -> Stream m a
{-# INLINE_FUSED reVector #-}
reVector (Stream step s, sSize = n} = Stream step s n

{-# RULES

"reVector [Vector]"
  reVector = id

"reVector/reVector [Vector]" forall s.
  reVector (reVector s) = s   #-}


-}