{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- |
-- Module      : Data.Stream.Monadic
-- Copyright   : (c) Roman Leshchinskiy 2008-2010
--                   Alexey Kuleshevich 2020-2022
--                   Aleksey Khudyakov 2020-2022
--                   Andrew Lelechenko 2020-2022
-- License     : BSD-style
--
-- Maintainer  : Haskell Libraries Team <libraries@haskell.org>
-- Stability   : experimental
-- Portability : non-portable
--
-- Monadic stream combinators.
--

module Data.Stream.Monadic (
  -- * Box monad
  Box(..), liftBox,
  -- * Stream
  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.Char      ( ord )
import GHC.Base       ( unsafeChr )
import Control.Monad  ( liftM )
import qualified Prelude
import Prelude
  ( Functor, Applicative, Monad, Char, String, Int, Word, Integer, Float, Double
  , Bool(..), Ordering(..), Maybe(..), Either(..), Eq, Ord, Enum, Num, Integral
  , RealFrac, return, pure, otherwise, seq, error, not, id, show, const, fmap
  , (==), (<), (<=), (>), (+), (-), (/), ($), (.), (=<<), (>>=) )

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

import GHC.Stack (HasCallStack)
import GHC.Types ( SPEC(..) )

#include "MachDeps.h"

#define INLINE_FUSED INLINE [1]
#define INLINE_INNER INLINE [0]


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


-- | Box monad
data Box a = Box { forall a. Box a -> a
unBox :: a }

instance Functor Box where
  fmap :: forall a b. (a -> b) -> Box a -> Box b
fmap a -> b
f (Box a
x) = b -> Box b
forall a. a -> Box a
Box (a -> b
f a
x)

instance Applicative Box where
  pure :: forall a. a -> Box a
pure = a -> Box a
forall a. a -> Box a
Box
  Box a -> b
f <*> :: forall a b. Box (a -> b) -> Box a -> Box b
<*> Box a
x = b -> Box b
forall a. a -> Box a
Box (a -> b
f a
x)

instance Monad Box where
  return :: forall a. a -> Box a
return = a -> Box a
forall a. a -> Box a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  Box a
x >>= :: forall a b. Box a -> (a -> Box b) -> Box b
>>= a -> Box b
f = a -> Box b
f a
x

liftBox :: Monad m => Box a -> m a
liftBox :: forall (m :: * -> *) a. Monad m => Box a -> m a
liftBox (Box a
a) = a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
{-# INLINE liftBox #-}


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


-- | 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) = b -> s -> Step s b
forall a s. a -> s -> Step s a
Yield (a -> b
f a
x) s
s
  fmap a -> b
_ (Skip s
s) = s -> Step s b
forall s a. s -> Step s a
Skip s
s
  fmap a -> b
_ Step s a
Done = Step s b
forall s a. Step s a
Done
  {-# INLINE (<$) #-}
  <$ :: forall a b. a -> Step s b -> Step s a
(<$) = (b -> a) -> Step s b -> Step s a
forall a b. (a -> b) -> Step s a -> Step s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> a) -> Step s b -> Step s a)
-> (a -> b -> a) -> a -> Step s b -> Step s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> a
forall a b. a -> b -> a
const

-- | 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 = (Int -> a -> Int) -> Int -> Stream m a -> m Int
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
foldl' (\Int
n a
_ -> Int
nInt -> Int -> Int
forall 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
_ -> Bool -> m Bool
forall a. a -> m a
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      -> Bool -> m Bool
forall a. a -> m a
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 = (() -> m (Step () a)) -> () -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (m (Step () a) -> () -> m (Step () a)
forall a b. a -> b -> a
const (Step () a -> m (Step () a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step () a
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 = (Bool -> m (Step Bool a)) -> Bool -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Step Bool a -> m (Step Bool a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Bool a -> m (Step Bool a))
-> (Bool -> Step Bool a) -> Bool -> m (Step Bool a)
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  = a -> Bool -> Step Bool a
forall a s. a -> s -> Step s a
Yield a
x Bool
False
    step Bool
False = Step Bool a
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 = Int -> m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
replicateM Int
n (a -> m a
forall a. a -> m a
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 = (Int -> m (Step Int a)) -> Int -> Stream m a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0    = Step Int a -> m (Step Int a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int a
forall s a. Step s a
Done
           | Bool
otherwise = do { a
x <- m a
p; Step Int a -> m (Step Int a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int a -> m (Step Int a)) -> Step Int a -> m (Step Int a)
forall a b. (a -> b) -> a -> b
$ a -> Int -> Step Int a
forall a s. a -> s -> Step s a
Yield a
x (Int
iInt -> Int -> Int
forall 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 = Int -> (Int -> m a) -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
generateM Int
n (a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (Int -> a) -> Int -> m a
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 Int -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` (Int -> m (Step Int a)) -> Int -> Stream m a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n     = do
                           a
x <- Int -> m a
f Int
i
                           Step Int a -> m (Step Int a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int a -> m (Step Int a)) -> Step Int a -> m (Step Int a)
forall a b. (a -> b) -> a -> b
$ a -> Int -> Step Int a
forall a s. a -> s -> Step s a
Yield a
x (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
           | Bool
otherwise = Step Int a -> m (Step Int a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int a
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 = a -> Stream m a
forall (m :: * -> *) a. Monad m => a -> Stream m a
singleton a
x Stream m a -> Stream m a -> Stream m a
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 Stream m a -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
++ 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 = (Either s s -> m (Step (Either s s) a)) -> Either s s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s s -> m (Step (Either s s) a)
step (s -> Either s s
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' -> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ a -> Either s s -> Step (Either s s) a
forall a s. a -> s -> Step s a
Yield a
x (s -> Either s s
forall a b. a -> Either a b
Left  s
sa')
                          Skip    s
sa' -> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip    (s -> Either s s
forall a b. a -> Either a b
Left  s
sa')
                          Step s a
Done        -> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip    (s -> Either s s
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' -> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ a -> Either s s -> Step (Either s s) a
forall a s. a -> s -> Step s a
Yield a
x (s -> Either s s
forall a b. b -> Either a b
Right s
sb')
                          Skip    s
sb' -> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip    (s -> Either s s
forall a b. b -> Either a b
Right s
sb')
                          Step s a
Done        -> Step (Either s s) a -> m (Step (Either s s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s s) a -> m (Step (Either s s) a))
-> Step (Either s s) a -> m (Step (Either s s) a)
forall a b. (a -> b) -> a -> b
$ Step (Either s s) a
forall s a. Step s a
Done

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

-- | First element of the 'Stream' or error if empty
head :: (HasCallStack, Monad m) => Stream m a -> m a
{-# INLINE_FUSED head #-}
head :: forall (m :: * -> *) a.
(HasCallStack, 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
_  -> a -> m a
forall a. a -> m a
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       -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream



-- | Last element of the 'Stream' or error if empty
last :: (HasCallStack, Monad m) => Stream m a -> m a
{-# INLINE_FUSED last #-}
last :: forall (m :: * -> *) a.
(HasCallStack, 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       -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream

    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       -> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x

infixl 9 !!
-- | Element at the given position
(!!) :: (HasCallStack, Monad m) => Stream m a -> Int -> m a
{-# INLINE (!!) #-}
Stream s -> m (Step s a)
step s
t !! :: forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> Int -> m a
!! Int
j | Int
j Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0     = String -> m a
forall a. HasCallStack => String -> a
error (String -> m a) -> String -> m a
forall a b. (a -> b) -> a -> b
$ String
"negative index (" String -> String -> String
forall a. [a] -> [a] -> [a]
Prelude.++ Int -> String
forall a. Show a => a -> String
show Int
j String -> String -> String
forall a. [a] -> [a] -> [a]
Prelude.++ String
")"
                   | 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 Int -> m a -> m a
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 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0    -> a -> m a
forall a. a -> m a
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
iInt -> Int -> Int
forall 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                   -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream

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 Int -> m (Maybe a) -> m (Maybe a)
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 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0    -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
                       | Bool
otherwise -> SPEC -> s -> Int -> m (Maybe a)
index_loop SPEC
SPEC s
s' (Int
iInt -> Int -> Int
forall 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                   -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
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 = Int -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
take Int
n (Int -> Stream m a -> Stream m a
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 :: (HasCallStack, Monad m) => Stream m a -> Stream m a
{-# INLINE_FUSED init #-}
init :: forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> Stream m a
init (Stream s -> m (Step s a)
step s
t) = ((Maybe a, s) -> m (Step (Maybe a, s) a))
-> (Maybe a, s) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (Maybe a
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) = (Step s a -> Step (Maybe a, s) a)
-> m (Step s a) -> m (Step (Maybe a, s) a)
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' -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x,  s
s')
                             Skip    s
s' -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (Maybe a
forall a. Maybe a
Nothing, s
s')
                             Step s a
Done       -> String -> Step (Maybe a, s) a
forall a. HasCallStack => String -> a
error String
emptyStream
                         ) (s -> m (Step s a)
step s
s)

    step' (Just a
x,  s
s) = (Step s a -> Step (Maybe a, s) a)
-> m (Step s a) -> m (Step (Maybe a, s) a)
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' -> a -> (Maybe a, s) -> Step (Maybe a, s) a
forall a s. a -> s -> Step s a
Yield a
x (a -> Maybe a
forall a. a -> Maybe a
Just a
y, s
s')
                             Skip    s
s' -> (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip    (a -> Maybe a
forall a. a -> Maybe a
Just a
x, s
s')
                             Step s a
Done       -> Step (Maybe a, s) a
forall s a. Step s a
Done
                         ) (s -> m (Step s a)
step s
s)

-- | All but the first element
tail :: (HasCallStack, Monad m) => Stream m a -> Stream m a
{-# INLINE_FUSED tail #-}
tail :: forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> Stream m a
tail (Stream s -> m (Step s a)
step s
t) = (Either s s -> m (Step (Either s s) a)) -> Either s s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s s -> m (Step (Either s s) a)
step' (s -> Either s s
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) = (Step s a -> Step (Either s s) a)
-> m (Step s a) -> m (Step (Either s s) a)
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' -> Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (s -> Either s s
forall a b. b -> Either a b
Right s
s')
                          Skip    s
s' -> Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip (s -> Either s s
forall a b. a -> Either a b
Left  s
s')
                          Step s a
Done       -> String -> Step (Either s s) a
forall a. HasCallStack => String -> a
error String
emptyStream
                      ) (s -> m (Step s a)
step s
s)

    step' (Right s
s) = (Step s a -> Step (Either s s) a)
-> m (Step s a) -> m (Step (Either s s) a)
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' -> a -> Either s s -> Step (Either s s) a
forall a s. a -> s -> Step s a
Yield a
x (s -> Either s s
forall a b. b -> Either a b
Right s
s')
                          Skip    s
s' -> Either s s -> Step (Either s s) a
forall s a. s -> Step s a
Skip    (s -> Either s s
forall a b. b -> Either a b
Right s
s')
                          Step s a
Done       -> Step (Either s s) a
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 Int -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` ((s, Int) -> m (Step (s, Int) a)) -> (s, Int) -> Stream m a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = (Step s a -> Step (s, Int) a)
-> m (Step s a) -> m (Step (s, Int) a)
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' -> a -> (s, Int) -> Step (s, Int) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
                               Skip    s
s' -> (s, Int) -> Step (s, Int) a
forall s a. s -> Step s a
Skip    (s
s', Int
i)
                               Step s a
Done       -> Step (s, Int) a
forall s a. Step s a
Done
                           ) (s -> m (Step s a)
step s
s)
    step' (s
_, Int
_) = Step (s, Int) a -> m (Step (s, Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) a
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) = ((s, Maybe Int) -> m (Step (s, Maybe Int) a))
-> (s, Maybe Int) -> Stream m a
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, Int -> Maybe Int
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = (Step s a -> Step (s, Maybe Int) a)
-> m (Step s a) -> m (Step (s, Maybe Int) a)
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' -> (s, Maybe Int) -> Step (s, Maybe Int) a
forall s a. s -> Step s a
Skip (s
s', Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
                                   Skip    s
s' -> (s, Maybe Int) -> Step (s, Maybe Int) a
forall s a. s -> Step s a
Skip (s
s', Int -> Maybe Int
forall a. a -> Maybe a
Just Int
i)
                                   Step s a
Done       -> Step (s, Maybe Int) a
forall s a. Step s a
Done
                                ) (s -> m (Step s a)
step s
s)
                      | Bool
otherwise = Step (s, Maybe Int) a -> m (Step (s, Maybe Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe Int) a -> m (Step (s, Maybe Int) a))
-> Step (s, Maybe Int) a -> m (Step (s, Maybe Int) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe Int) -> Step (s, Maybe Int) a
forall s a. s -> Step s a
Skip (s
s, Maybe Int
forall a. Maybe a
Nothing)

    step' (s
s, Maybe Int
Nothing) = (Step s a -> Step (s, Maybe Int) a)
-> m (Step s a) -> m (Step (s, Maybe Int) a)
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' -> a -> (s, Maybe Int) -> Step (s, Maybe Int) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', Maybe Int
forall a. Maybe a
Nothing)
                             Skip    s
s' -> (s, Maybe Int) -> Step (s, Maybe Int) a
forall s a. s -> Step s a
Skip    (s
s', Maybe Int
forall a. Maybe a
Nothing)
                             Step s a
Done       -> Step (s, Maybe Int) a
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 = (a -> b) -> Stream m a -> Stream m b
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 = (a -> m b) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
mapM (b -> m b
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (a -> b) -> a -> m b
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) = (s -> m (Step s b)) -> s -> Stream m b
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' -> (b -> Step s b) -> m b -> m (Step s b)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM  (b -> s -> Step s b
forall a s. a -> s -> Step s a
`Yield` s
s') (a -> m b
f a
x)
                  Skip    s
s' -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (s -> Step s b
forall s a. s -> Step s a
Skip    s
s')
                  Step s a
Done       -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s b
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       -> () -> m ()
forall a. a -> m a
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 = Stream m b -> m ()
forall (m :: * -> *) a. Monad m => Stream m a -> m ()
consume (Stream m b -> m ())
-> (Stream m a -> Stream m b) -> Stream m a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> m b) -> Stream m a -> Stream m b
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) = (s -> m' (Step s a)) -> s -> Stream m' a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (m (Step s a) -> m' (Step s a)
forall z. m z -> m' z
f (m (Step s a) -> m' (Step s a))
-> (s -> m (Step s a)) -> s -> m' (Step s a)
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) = (s -> m (Step s a)) -> s -> Stream m a
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' -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ a -> s -> Step s a
forall a s. a -> s -> Step s a
Yield a
x s
s'
                  Skip          s
s' -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ s -> Step s a
forall s a. s -> Step s a
Skip    s
s'
                  Step s (Box a)
Done             -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step s a
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) = ((s, Int) -> m (Step (s, Int) (Int, a)))
-> (s, Int) -> Stream m (Int, a)
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 Int -> m (Step (s, Int) (Int, a)) -> m (Step (s, Int) (Int, a))
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' -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a)))
-> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a b. (a -> b) -> a -> b
$ (Int, a) -> (s, Int) -> Step (s, Int) (Int, a)
forall a s. a -> s -> Step s a
Yield (Int
i,a
x) (s
s', Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
                      Skip    s
s' -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a)))
-> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a b. (a -> b) -> a -> b
$ (s, Int) -> Step (s, Int) (Int, a)
forall s a. s -> Step s a
Skip        (s
s', Int
i)
                      Step s a
Done       -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) (Int, a)
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) = ((s, Int) -> m (Step (s, Int) (Int, a)))
-> (s, Int) -> Stream m (Int, a)
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 Int -> m (Step (s, Int) (Int, a)) -> m (Step (s, Int) (Int, a))
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
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1
                                    in
                                    Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a)))
-> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a b. (a -> b) -> a -> b
$ (Int, a) -> (s, Int) -> Step (s, Int) (Int, a)
forall a s. a -> s -> Step s a
Yield (Int
i',a
x) (s
s', Int
i')
                      Skip    s
s' -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a)))
-> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a b. (a -> b) -> a -> b
$ (s, Int) -> Step (s, Int) (Int, a)
forall s a. s -> Step s a
Skip         (s
s', Int
i)
                      Step s a
Done       -> Step (s, Int) (Int, a) -> m (Step (s, Int) (Int, a))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) (Int, a)
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) = ((s, s, Maybe a) -> m (Step (s, s, Maybe a) c))
-> (s, s, Maybe a) -> Stream m c
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, Maybe a
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) = (Step s a -> Step (s, s, Maybe a) c)
-> m (Step s a) -> m (Step (s, s, Maybe a) c)
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' -> (s, s, Maybe a) -> Step (s, s, Maybe a) c
forall s a. s -> Step s a
Skip (s
sa', s
sb, a -> Maybe a
forall a. a -> Maybe a
Just a
x)
                                 Skip    s
sa' -> (s, s, Maybe a) -> Step (s, s, Maybe a) c
forall s a. s -> Step s a
Skip (s
sa', s
sb, Maybe a
forall a. Maybe a
Nothing)
                                 Step s a
Done        -> Step (s, s, Maybe a) c
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
                                     Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c))
-> Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall a b. (a -> b) -> a -> b
$ c -> (s, s, Maybe a) -> Step (s, s, Maybe a) c
forall a s. a -> s -> Step s a
Yield c
z (s
sa, s
sb', Maybe a
forall a. Maybe a
Nothing)
                                 Skip    s
sb' -> Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c))
-> Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall a b. (a -> b) -> a -> b
$ (s, s, Maybe a) -> Step (s, s, Maybe a) c
forall s a. s -> Step s a
Skip (s
sa, s
sb', a -> Maybe a
forall a. a -> Maybe a
Just a
x)
                                 Step s b
Done        -> Step (s, s, Maybe a) c -> m (Step (s, s, Maybe a) c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, s, Maybe a) c
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 = Stream m c -> m ()
forall (m :: * -> *) a. Monad m => Stream m a -> m ()
consume ((a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
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) = ((s, s, s, Maybe (a, Maybe b))
 -> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> (s, s, s, Maybe (a, Maybe b)) -> Stream m d
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, Maybe (a, Maybe b)
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
        Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
 -> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ case Step s a
r of
            Yield a
x s
sa' -> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa', s
sb, s
sc, (a, Maybe b) -> Maybe (a, Maybe b)
forall a. a -> Maybe a
Just (a
x, Maybe b
forall a. Maybe a
Nothing))
            Skip    s
sa' -> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa', s
sb, s
sc, Maybe (a, Maybe b)
forall a. Maybe a
Nothing)
            Step s a
Done        -> Step (s, s, s, Maybe (a, Maybe b)) d
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
        Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
 -> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
            Yield b
y s
sb' -> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa, s
sb', s
sc, (a, Maybe b) -> Maybe (a, Maybe b)
forall a. a -> Maybe a
Just (a
x, b -> Maybe b
forall a. a -> Maybe a
Just b
y))
            Skip    s
sb' -> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa, s
sb', s
sc, (a, Maybe b) -> Maybe (a, Maybe b)
forall a. a -> Maybe a
Just (a
x, Maybe b
forall a. Maybe a
Nothing))
            Step s b
Done        -> Step (s, s, s, Maybe (a, Maybe b)) d
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 m d
-> (d -> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\d
res -> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
 -> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ d
-> (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall a s. a -> s -> Step s a
Yield d
res (s
sa, s
sb, s
sc', Maybe (a, Maybe b)
forall a. Maybe a
Nothing))
            Skip    s
sc' -> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
 -> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ (s, s, s, Maybe (a, Maybe b))
-> Step (s, s, s, Maybe (a, Maybe b)) d
forall s a. s -> Step s a
Skip (s
sa, s
sb, s
sc', (a, Maybe b) -> Maybe (a, Maybe b)
forall a. a -> Maybe a
Just (a
x, b -> Maybe b
forall a. a -> Maybe a
Just b
y))
            Step s c
Done        -> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, s, s, Maybe (a, Maybe b)) d
 -> m (Step (s, s, s, Maybe (a, Maybe b)) d))
-> Step (s, s, s, Maybe (a, Maybe b)) d
-> m (Step (s, s, s, Maybe (a, Maybe b)) d)
forall a b. (a -> b) -> a -> b
$ Step (s, s, s, Maybe (a, Maybe b)) d
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
  = ((a, b) -> (c, d) -> m e)
-> Stream m (a, b) -> Stream m (c, d) -> Stream m e
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) (Stream m a -> Stream m b -> Stream m (a, b)
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) (Stream m c -> Stream m d -> Stream m (c, d)
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
  = ((a, b, c) -> (d, e) -> m f)
-> Stream m (a, b, c) -> Stream m (d, e) -> Stream m 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,c
c) (d
d,e
e) -> a -> b -> c -> d -> e -> m f
f a
a b
b c
c d
d e
e) (Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
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) (Stream m d -> Stream m e -> Stream m (d, e)
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
  = ((a, b, c) -> (d, e, f) -> m g)
-> Stream m (a, b, c) -> Stream m (d, e, f) -> Stream m g
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) (Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
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)
                                                  (Stream m d -> Stream m e -> Stream m f -> Stream m (d, e, 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 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 = (a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
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 -> m c
forall a. a -> m a
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 = (a -> b -> c -> m d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
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 -> d -> m d
forall a. a -> m a
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 = (a -> b -> c -> d -> m e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
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 -> e -> m e
forall a. a -> m a
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 = (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
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 -> f -> m f
forall a. a -> m a
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 = (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
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 -> g -> m g
forall a. a -> m a
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 = (a -> b -> (a, b)) -> Stream m a -> Stream m b -> Stream m (a, b)
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 = (a -> b -> c -> (a, b, c))
-> Stream m a -> Stream m b -> Stream m c -> Stream m (a, b, c)
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 = (a -> b -> c -> d -> (a, b, c, d))
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m (a, b, c, d)
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 = (a -> b -> c -> d -> e -> (a, b, c, d, e))
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m (a, b, c, d, e)
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 = (a -> b -> c -> d -> e -> f -> (a, b, c, d, e, f))
-> 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)
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 -> Bool -> m Bool
forall a. a -> m a
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          -> Bool -> m Bool
forall a. a -> m a
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
_ -> Bool -> m Bool
forall a. a -> m a
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      -> Bool -> m Bool
forall a. a -> m a
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  -> Ordering -> m Ordering
forall a. a -> m a
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        -> Ordering -> m Ordering
forall a. a -> m a
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
_ -> Ordering -> m Ordering
forall a. a -> m a
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      -> Ordering -> m Ordering
forall a. a -> m a
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 = (a -> m Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
filterM (Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
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) = (s -> m (Step s b)) -> s -> Stream m b
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
                                  Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ case a -> Maybe b
f a
x of
                                    Maybe b
Nothing -> s -> Step s b
forall s a. s -> Step s a
Skip s
s'
                                    Just b
b' -> b -> s -> Step s b
forall a s. a -> s -> Step s a
Yield b
b' s
s'
                  Skip    s
s' -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ s -> Step s b
forall s a. s -> Step s a
Skip s
s'
                  Step s a
Done       -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ Step s 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 = (Maybe a -> Maybe a) -> Stream m (Maybe a) -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> Maybe b) -> Stream m a -> Stream m b
mapMaybe Maybe a -> Maybe a
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) = (s -> m (Step s a)) -> s -> Stream m a
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
                                  Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ if Bool
b then a -> s -> Step s a
forall a s. a -> s -> Step s a
Yield a
x s
s'
                                                else s -> Step s a
forall s a. s -> Step s a
Skip    s
s'
                  Skip    s
s' -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ s -> Step s a
forall s a. s -> Step s a
Skip s
s'
                  Step s a
Done       -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ Step s a
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) = (s -> m (Step s b)) -> s -> Stream m b
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
                                  Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ case Maybe b
fx of
                                    Maybe b
Nothing -> s -> Step s b
forall s a. s -> Step s a
Skip s
s'
                                    Just b
b  -> b -> s -> Step s b
forall a s. a -> s -> Step s a
Yield b
b s
s'
                  Skip    s
s' -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ s -> Step s b
forall s a. s -> Step s a
Skip s
s'
                  Step s a
Done       -> Step s b -> m (Step s b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ Step s 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) = ((Maybe a, s) -> m (Step (Maybe a, s) a))
-> (Maybe a, s) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Maybe a, s) -> m (Step (Maybe a, s) a)
step' (Maybe a
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' -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ a -> (Maybe a, s) -> Step (Maybe a, s) a
forall a s. a -> s -> Step s a
Yield a
x (a -> Maybe a
forall a. a -> Maybe a
Just a
x , s
s')
                              Skip  s
s'   -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip  (Maybe a
forall a. Maybe a
Nothing, s
s')
                              Step s a
Done       -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return   Step (Maybe a, s) a
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 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x0   -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip    (a -> Maybe a
forall a. a -> Maybe a
Just a
x0, s
s')
                                         | Bool
otherwise -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ a -> (Maybe a, s) -> Step (Maybe a, s) a
forall a s. a -> s -> Step s a
Yield a
x (a -> Maybe a
forall a. a -> Maybe a
Just a
x , s
s')
                              Skip  s
s'   -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a, s) a -> m (Step (Maybe a, s) a))
-> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a b. (a -> b) -> a -> b
$ (Maybe a, s) -> Step (Maybe a, s) a
forall s a. s -> Step s a
Skip (a -> Maybe a
forall a. a -> Maybe a
Just a
x0, s
s')
                              Step s a
Done       -> Step (Maybe a, s) a -> m (Step (Maybe a, s) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return   Step (Maybe a, s) a
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 = (a -> m Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
takeWhileM (Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
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) = (s -> m (Step s a)) -> s -> Stream m a
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
                                  Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ if Bool
b then a -> s -> Step s a
forall a s. a -> s -> Step s a
Yield a
x s
s' else Step s a
forall s a. Step s a
Done
                  Skip    s
s' -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ s -> Step s a
forall s a. s -> Step s a
Skip s
s'
                  Step s a
Done       -> Step s a -> m (Step s a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$ Step s a
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 = (a -> m Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
dropWhileM (Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
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) = (DropWhile s a -> m (Step (DropWhile s a) a))
-> DropWhile s a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream DropWhile s a -> m (Step (DropWhile s a) a)
step' (s -> DropWhile s a
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
                            Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhile s a) a -> m (Step (DropWhile s a) a))
-> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a b. (a -> b) -> a -> b
$ if Bool
b then DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip (s -> DropWhile s a
forall s a. s -> DropWhile s a
DropWhile_Drop    s
s')
                                          else DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip (a -> s -> DropWhile s a
forall s a. a -> s -> DropWhile s a
DropWhile_Yield a
x s
s')
            Skip    s
s' -> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhile s a) a -> m (Step (DropWhile s a) a))
-> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a b. (a -> b) -> a -> b
$ DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip (s -> DropWhile s a
forall s a. s -> DropWhile s a
DropWhile_Drop    s
s')
            Step s a
Done       -> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (DropWhile s a) a -> m (Step (DropWhile s a) a))
-> Step (DropWhile s a) a -> m (Step (DropWhile s a) a)
forall a b. (a -> b) -> a -> b
$ Step (DropWhile s a) a
forall s a. Step s a
Done

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

    step' (DropWhile_Next s
s)
      = (Step s a -> Step (DropWhile s a) a)
-> m (Step s a) -> m (Step (DropWhile s a) a)
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' -> DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip    (a -> s -> DropWhile s a
forall s a. a -> s -> DropWhile s a
DropWhile_Yield a
x s
s')
            Skip    s
s' -> DropWhile s a -> Step (DropWhile s a) a
forall s a. s -> Step s a
Skip    (s -> DropWhile s a
forall s a. s -> DropWhile s a
DropWhile_Next    s
s')
            Step s a
Done       -> Step (DropWhile s a) a
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 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y    -> Bool -> m Bool
forall a. a -> m a
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                   -> Bool -> m Bool
forall a. a -> m a
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 = (Bool -> Bool) -> m Bool -> m Bool
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Bool -> Bool
not (a -> Stream m a -> m Bool
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 = (a -> m Bool) -> Stream m a -> m (Maybe a)
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe a)
findM (Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
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 Maybe a -> m (Maybe a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
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       -> Maybe a -> m (Maybe a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
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 = (a -> m Bool) -> Stream m a -> m (Maybe Int)
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> m (Maybe Int)
findIndexM (Bool -> m Bool
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
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 Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Int -> m (Maybe Int)) -> Maybe Int -> m (Maybe Int)
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int
forall a. a -> Maybe a
Just Int
i
                                 else SPEC -> s -> Int -> m (Maybe Int)
findIndex_loop SPEC
SPEC s
s' (Int
iInt -> Int -> Int
forall 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       -> Maybe Int -> m (Maybe Int)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Int
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 = (a -> b -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM (\a
a b
b -> a -> m a
forall a. a -> m a
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       -> a -> m a
forall a. a -> m a
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 = (a -> b -> m a) -> a -> Stream m b -> m a
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 = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M (\a
a a
b -> a -> m a
forall a. a -> m a
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 :: (HasCallStack, Monad m) => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE_FUSED foldl1M #-}
foldl1M :: forall (m :: * -> *) a.
(HasCallStack, 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' -> (a -> a -> m a) -> a -> Stream m a -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM a -> a -> m a
f a
x ((s -> m (Step s a)) -> s -> Stream m a
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       -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream

-- | 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 = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, 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 = (a -> b -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM' (\a
a b
b -> a -> m a
forall a. a -> m a
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 a -> m a -> m a
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       -> a -> m a
forall a. a -> m a
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' = (a -> b -> m a) -> a -> Stream m b -> m a
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 = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
foldl1M' (\a
a a
b -> a -> m a
forall a. a -> m a
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' :: (HasCallStack, Monad m) => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE_FUSED foldl1M' #-}
foldl1M' :: forall (m :: * -> *) a.
(HasCallStack, 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' -> (a -> a -> m a) -> a -> Stream m a -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
foldlM' a -> a -> m a
f a
x ((s -> m (Step s a)) -> s -> Stream m a
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       -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream

-- | 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' = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, 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 = (a -> b -> m b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Stream m a -> m b
foldrM (\a
a b
b -> b -> m b
forall a. a -> m a
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 (b -> m b) -> m b -> m b
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       -> b -> m b
forall a. a -> m a
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 = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
foldr1M (\a
a a
b -> a -> m a
forall a. a -> m a
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 :: (HasCallStack, Monad m) => (a -> a -> m a) -> Stream m a -> m a
{-# INLINE_FUSED foldr1M #-}
foldr1M :: forall (m :: * -> *) a.
(HasCallStack, 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       -> String -> m a
forall a. HasCallStack => String -> a
error String
emptyStream

    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 (a -> m a) -> m a -> m a
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       -> a -> m a
forall a. a -> m a
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
_  -> Bool -> m Bool
forall a. a -> m a
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           -> Bool -> m Bool
forall a. a -> m a
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
_  -> Bool -> m Bool
forall a. a -> m a
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           -> Bool -> m Bool
forall a. a -> m a
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 = (a -> m (Stream m b)) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> m (Stream m b)) -> Stream m a -> Stream m b
concatMapM (Stream m b -> m (Stream m b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Stream m b -> m (Stream m b))
-> (a -> Stream m b) -> a -> m (Stream m b)
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) = (Either s (Stream m b, s) -> m (Step (Either s (Stream m b, s)) b))
-> Either s (Stream m b, s) -> Stream m b
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 (s -> Either s (Stream m b, s)
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
                Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
 -> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall s a. s -> Step s a
Skip ((Stream m b, s) -> Either s (Stream m b, s)
forall a b. b -> Either a b
Right (Stream m b
b_stream, s
s'))
            Skip    s
s' -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
 -> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall s a. s -> Step s a
Skip (s -> Either s (Stream m b, s)
forall a b. a -> Either a b
Left s
s')
            Step s a
Done       -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Either s (Stream m b, s)) b
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' -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
 -> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ b -> Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall a s. a -> s -> Step s a
Yield b
b ((Stream m b, s) -> Either s (Stream m b, s)
forall a b. b -> Either a b
Right ((s -> m (Step s b)) -> s -> Stream m b
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' -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
 -> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall s a. s -> Step s a
Skip ((Stream m b, s) -> Either s (Stream m b, s)
forall a b. b -> Either a b
Right ((s -> m (Step s b)) -> s -> Stream m b
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             -> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (Stream m b, s)) b
 -> m (Step (Either s (Stream m b, s)) b))
-> Step (Either s (Stream m b, s)) b
-> m (Step (Either s (Stream m b, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (Stream m b, s) -> Step (Either s (Stream m b, s)) b
forall s a. s -> Step s a
Skip (s -> Either s (Stream m b, s)
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) = (Either s (s, s) -> m (Step (Either s (s, s)) b))
-> Either s (s, s) -> Stream m b
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 (s -> Either s (s, s)
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 s -> m (Step (Either s (s, s)) b) -> m (Step (Either s (s, s)) b)
forall a b. a -> b -> b
`seq` Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Either s (s, s) -> Step (Either s (s, s)) b
forall s a. s -> Step s a
Skip ((s, s) -> Either s (s, s)
forall a b. b -> Either a b
Right (s
s,s
t')))
                        Skip    s
t' -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (s, s) -> Step (Either s (s, s)) b
forall s a. s -> Step s a
Skip (s -> Either s (s, s)
forall a b. a -> Either a b
Left s
t')
                        Step s a
Done       -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ Step (Either s (s, s)) 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' -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ b -> Either s (s, s) -> Step (Either s (s, s)) b
forall a s. a -> s -> Step s a
Yield b
x ((s, s) -> Either s (s, s)
forall a b. b -> Either a b
Right (s
s',s
t))
                             Skip    s
s' -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (s, s) -> Step (Either s (s, s)) b
forall s a. s -> Step s a
Skip    ((s, s) -> Either s (s, s)
forall a b. b -> Either a b
Right (s
s',s
t))
                             Step s b
Done       -> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b))
-> Step (Either s (s, s)) b -> m (Step (Either s (s, s)) b)
forall a b. (a -> b) -> a -> b
$ Either s (s, s) -> Step (Either s (s, s)) b
forall s a. s -> Step s a
Skip    (s -> Either s (s, s)
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 = (s -> m (Maybe (a, s))) -> s -> Stream m a
forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrM (Maybe (a, s) -> m (Maybe (a, s))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, s) -> m (Maybe (a, s)))
-> (s -> Maybe (a, s)) -> s -> m (Maybe (a, s))
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 = (s -> m (Step s a)) -> s -> Stream m a
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 = (Maybe (a, s) -> Step s a) -> m (Maybe (a, s)) -> m (Step s a)
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') -> a -> s -> Step s a
forall a s. a -> s -> Step s a
Yield a
x s
s'
                 Maybe (a, s)
Nothing      -> Step s a
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 = Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrNM Int
n (Maybe (a, s) -> m (Maybe (a, s))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, s) -> m (Maybe (a, s)))
-> (s -> Maybe (a, s)) -> s -> m (Maybe (a, s))
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 = ((s, Int) -> m (Step (s, Int) a)) -> (s, Int) -> Stream m a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0    = Step (s, Int) a -> m (Step (s, Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) a
forall s a. Step s a
Done
               | Bool
otherwise = (Maybe (a, s) -> Step (s, Int) a)
-> m (Maybe (a, s)) -> m (Step (s, Int) a)
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') -> a -> (s, Int) -> Step (s, Int) a
forall a s. a -> s -> Step s a
Yield a
x (s
s',Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
                                 Maybe (a, s)
Nothing     -> Step (s, Int) a
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 = Int -> (s -> m (a, s)) -> s -> Stream m a
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (a, s)) -> s -> Stream m a
unfoldrExactNM Int
n ((a, s) -> m (a, s)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, s) -> m (a, s)) -> (s -> (a, s)) -> s -> m (a, s)
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 = ((s, Int) -> m (Step (s, Int) a)) -> (s, Int) -> Stream m a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0    = Step (s, Int) a -> m (Step (s, Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Int) a
forall s a. Step s a
Done
               | Bool
otherwise = do (a
x,s
s') <- s -> m (a, s)
f s
s
                                Step (s, Int) a -> m (Step (s, Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Int) a -> m (Step (s, Int) a))
-> Step (s, Int) a -> m (Step (s, Int) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Int) -> Step (s, Int) a
forall a s. a -> s -> Step s a
Yield a
x (s
s',Int
nInt -> Int -> Int
forall 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 = ((a, Int) -> m (Step (a, Int) a)) -> (a, Int) -> Stream m a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0    = Step (a, Int) a -> m (Step (a, Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (a, Int) a
forall s a. Step s a
Done
               | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n    = Step (a, Int) a -> m (Step (a, Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, Int) a -> m (Step (a, Int) a))
-> Step (a, Int) a -> m (Step (a, Int) a)
forall a b. (a -> b) -> a -> b
$ a -> (a, Int) -> Step (a, Int) a
forall a s. a -> s -> Step s a
Yield a
x (a
x,Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
               | Bool
otherwise = do a
a <- a -> m a
f a
x
                                Step (a, Int) a -> m (Step (a, Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, Int) a -> m (Step (a, Int) a))
-> Step (a, Int) a -> m (Step (a, Int) a)
forall a b. (a -> b) -> a -> b
$ a -> (a, Int) -> Step (a, Int) a
forall a s. a -> s -> Step s a
Yield a
a (a
a,Int
iInt -> Int -> Int
forall 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 = Int -> (a -> m a) -> a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m a) -> a -> Stream m a
iterateNM Int
n (a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (a -> a) -> a -> m a
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 = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM (\a
a b
b -> a -> m a
forall a. a -> m a
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) = ((s, a) -> m (Step (s, a) a)) -> (s, a) -> Stream m a
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
                                      Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, a) -> Step (s, a) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', a
z)
                      Skip    s
s' -> Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ (s, a) -> Step (s, a) a
forall s a. s -> Step s a
Skip (s
s', a
x)
                      Step s b
Done       -> Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, a) a
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 = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
prescanlM' (\a
a b
b -> a -> m a
forall a. a -> m a
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) = ((s, a) -> m (Step (s, a) a)) -> (s, a) -> Stream m a
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 a -> m (Step (s, a) a) -> m (Step (s, a) a)
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
                                      Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, a) -> Step (s, a) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', a
z)
                      Skip    s
s' -> Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ (s, a) -> Step (s, a) a
forall s a. s -> Step s a
Skip (s
s', a
x)
                      Step s b
Done       -> Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, a) a
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 = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM (\a
a b
b -> a -> m a
forall a. a -> m a
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) = ((s, a) -> m (Step (s, a) a)) -> (s, a) -> Stream m a
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
                                      Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, a) -> Step (s, a) a
forall a s. a -> s -> Step s a
Yield a
z (s
s',a
z)
                      Skip    s
s' -> Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ (s, a) -> Step (s, a) a
forall s a. s -> Step s a
Skip (s
s',a
x)
                      Step s b
Done       -> Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, a) a
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 = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
postscanlM' (\a
a b
b -> a -> m a
forall a. a -> m a
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 a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` ((s, a) -> m (Step (s, a) a)) -> (s, a) -> Stream m a
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 a -> m (Step (s, a) a) -> m (Step (s, a) a)
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 a -> m (Step (s, a) a) -> m (Step (s, a) a)
forall a b. a -> b -> b
`seq` Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> (s, a) -> Step (s, a) a
forall a s. a -> s -> Step s a
Yield a
z (s
s',a
z))
                      Skip    s
s' -> Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, a) a -> m (Step (s, a) a))
-> Step (s, a) a -> m (Step (s, a) a)
forall a b. (a -> b) -> a -> b
$ (s, a) -> Step (s, a) a
forall s a. s -> Step s a
Skip (s
s',a
x)
                      Step s b
Done       -> Step (s, a) a -> m (Step (s, a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, a) a
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 = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM (\a
a b
b -> a -> m a
forall a. a -> m a
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 a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
`cons` (a -> b -> m a) -> a -> Stream m b -> Stream m a
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 = (a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
scanlM' (\a
a b
b -> a -> m a
forall a. a -> m a
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 a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` (a
z a -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
`cons` (a -> b -> m a) -> a -> Stream m b -> Stream m a
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)

-- | Initial-value free scan over a '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 = (a -> a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M (\a
x a
y -> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-- | Initial-value free scan over a '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) = ((s, Maybe a) -> m (Step (s, Maybe a) a))
-> (s, Maybe a) -> Stream m a
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, Maybe a
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' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Maybe a) -> Step (s, Maybe a) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
x)
                             Skip    s
s' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s', Maybe a
forall a. Maybe a
Nothing)
                             Step s a
Done       -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Done

    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
                                            Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> (s, Maybe a) -> Step (s, Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
z)
                            Skip    s
s' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
x)
                            Step s a
Done       -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Done

-- | Initial-value free scan over a '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 = (a -> a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
scanl1M' (\a
x a
y -> a -> m a
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-- | Initial-value free scan over a '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) = ((s, Maybe a) -> m (Step (s, Maybe a) a))
-> (s, Maybe a) -> Stream m a
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, Maybe a
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 a -> m (Step (s, Maybe a) a) -> m (Step (s, Maybe a) a)
forall a b. a -> b -> b
`seq` Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> (s, Maybe a) -> Step (s, Maybe a) a
forall a s. a -> s -> Step s a
Yield a
x (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
x))
                             Skip    s
s' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s', Maybe a
forall a. Maybe a
Nothing)
                             Step s a
Done       -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
forall s a. Step s a
Done

    step' (s
s, Just a
x) = a
x a -> m (Step (s, Maybe a) a) -> m (Step (s, Maybe a) a)
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 a -> m (Step (s, Maybe a) a) -> m (Step (s, Maybe a) a)
forall a b. a -> b -> b
`seq` Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> (s, Maybe a) -> Step (s, Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
z))
                            Skip    s
s' -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (s, Maybe a) a -> m (Step (s, Maybe a) a))
-> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a b. (a -> b) -> a -> b
$ (s, Maybe a) -> Step (s, Maybe a) a
forall s a. s -> Step s a
Skip (s
s', a -> Maybe a
forall a. a -> Maybe a
Just a
x)
                            Step s a
Done       -> Step (s, Maybe a) a -> m (Step (s, Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step (s, Maybe a) a
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 a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` a
y a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` Int
n Int -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` ((a, Int) -> m (Step (a, Int) a)) -> (a, Int) -> Stream m a
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0     = Step (a, Int) a -> m (Step (a, Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, Int) a -> m (Step (a, Int) a))
-> Step (a, Int) a -> m (Step (a, Int) a)
forall a b. (a -> b) -> a -> b
$ a -> (a, Int) -> Step (a, Int) a
forall a s. a -> s -> Step s a
Yield a
w (a
wa -> a -> a
forall a. Num a => a -> a -> a
+a
y,Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1)
               | Bool
otherwise = Step (a, Int) a -> m (Step (a, Int) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (a, Int) a -> m (Step (a, Int) a))
-> Step (a, Int) a -> m (Step (a, Int) a)
forall a b. (a -> b) -> a -> b
$ Step (a, Int) a
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 = [a] -> Stream m a
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 a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` a
y a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` (Maybe a -> m (Step (Maybe a) a)) -> Maybe a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
  where
    {-# INLINE_INNER step #-}
    step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing              = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
    step (Just a
z) | a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z Maybe a
forall a. Maybe a
Nothing
                  | a
z a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<  a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (a -> Maybe a
forall a. a -> Maybe a
Just (a
za -> a -> a
forall a. Num a => a -> a -> a
+a
1))
                  | Bool
otherwise = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
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 Int -> Stream m Int -> Stream m Int
forall a b. a -> b -> b
`seq` Int
y Int -> Stream m Int -> Stream m Int
forall a b. a -> b -> b
`seq` (Maybe Int -> m (Step (Maybe Int) Int))
-> Maybe Int -> Stream m Int
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe Int -> m (Step (Maybe Int) Int)
step (Int -> Maybe Int
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              = Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe Int) Int -> m (Step (Maybe Int) Int))
-> Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a b. (a -> b) -> a -> b
$ Step (Maybe Int) Int
forall s a. Step s a
Done
    step (Just Int
z) | Int
z Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
y    = Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe Int) Int -> m (Step (Maybe Int) Int))
-> Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int -> Step (Maybe Int) Int
forall a s. a -> s -> Step s a
Yield Int
z Maybe Int
forall a. Maybe a
Nothing
                  | Int
z Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<  Int
y    = Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe Int) Int -> m (Step (Maybe Int) Int))
-> Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a b. (a -> b) -> a -> b
$ Int -> Maybe Int -> Step (Maybe Int) Int
forall a s. a -> s -> Step s a
Yield Int
z (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
zInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1))
                  | Bool
otherwise = Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe Int) Int -> m (Step (Maybe Int) Int))
-> Step (Maybe Int) Int -> m (Step (Maybe Int) Int)
forall a b. (a -> b) -> a -> b
$ Step (Maybe Int) Int
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 a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` a
y a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` (Maybe a -> m (Step (Maybe a) a)) -> Maybe a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
  where
    {-# INLINE_INNER step #-}
    step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing              = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
    step (Just a
z) | a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z Maybe a
forall a. Maybe a
Nothing
                  | a
z a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<  a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (a -> Maybe a
forall a. a -> Maybe a
Just (a
za -> a -> a
forall a. Num a => a -> a -> a
+a
1))
                  | Bool
otherwise = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
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 a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` a
y a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` (Maybe a -> m (Step (Maybe a) a)) -> Maybe a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
  where
    {-# INLINE_INNER step #-}
    step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing              = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
    step (Just a
z) | a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z Maybe a
forall a. Maybe a
Nothing
                  | a
z a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<  a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (a -> Maybe a
forall a. a -> Maybe a
Just (a
za -> a -> a
forall a. Num a => a -> a -> a
+a
1))
                  | Bool
otherwise = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
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 a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` a
y a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` (Maybe a -> m (Step (Maybe a) a)) -> Maybe a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (a -> Maybe a
forall a. a -> Maybe a
Just a
x)
  where
    {-# INLINE_INNER step #-}
    step :: Maybe a -> m (Step (Maybe a) a)
step Maybe a
Nothing              = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
    step (Just a
z) | a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z Maybe a
forall a. Maybe a
Nothing
                  | a
z a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<  a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (a -> Maybe a
forall a. a -> Maybe a
Just (a
za -> a -> a
forall a. Num a => a -> a -> a
+a
1))
                  | Bool
otherwise = Step (Maybe a) a -> m (Step (Maybe a) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
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 Char -> Stream m Char -> Stream m Char
forall a b. a -> b -> b
`seq` Char
y Char -> Stream m Char -> Stream m Char
forall a b. a -> b -> b
`seq` (Int -> m (Step Int Char)) -> Int -> Stream m Char
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
yn  = Step Int Char -> m (Step Int Char)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int Char -> m (Step Int Char))
-> Step Int Char -> m (Step Int Char)
forall a b. (a -> b) -> a -> b
$ Char -> Int -> Step Int Char
forall a s. a -> s -> Step s a
Yield (Int -> Char
unsafeChr Int
zn) (Int
znInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
            | Bool
otherwise = Step Int Char -> m (Step Int Char)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int Char -> m (Step Int Char))
-> Step Int Char -> m (Step Int Char)
forall a b. (a -> b) -> a -> b
$ Step Int Char
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 a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` a
m a -> Stream m a -> Stream m a
forall a b. a -> b -> b
`seq` (a -> m (Step a a)) -> a -> Stream m a
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 a -> a -> a
forall a. Num a => a -> a -> a
+ a
1a -> a -> a
forall 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
-- accommodate 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' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
lim = Step a a -> m (Step a a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a -> m (Step a a)) -> Step a a -> m (Step a a)
forall a b. (a -> b) -> a -> b
$ a -> a -> Step a a
forall a s. a -> s -> Step s a
Yield a
x' (a
xa -> a -> a
forall a. Num a => a -> a -> a
+a
1)
           | Bool
otherwise = Step a a -> m (Step a a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a -> m (Step a a)) -> Step a a -> m (Step a a)
forall a b. (a -> b) -> a -> b
$ Step a a
forall s a. Step s a
Done
           where
             x' :: a
x' = a
x a -> a -> a
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 = [a] -> Stream m a
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 = (a -> [a] -> [a]) -> [a] -> Stream m a -> m [a]
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 = ([a] -> m (Step [a] a)) -> [a] -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream [a] -> m (Step [a] a)
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) = Step [a] a -> m (Step [a] a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> [a] -> Step [a] a
forall a s. a -> s -> Step s a
Yield a
x [a]
xs)
    step []     = Step [a] a -> m (Step [a] a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step [a] a
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 = (([a], Int) -> m (Step ([a], Int) a)) -> ([a], Int) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream ([a], Int) -> m (Step ([a], Int) a)
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 b -> b -> Bool
forall a. Ord a => a -> a -> Bool
<= b
0 = Step ([a], b) a -> m (Step ([a], b) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step ([a], b) a
forall s a. Step s a
Done
    step (a
x:[a]
xs,b
n)        = Step ([a], b) a -> m (Step ([a], b) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ([a], b) -> Step ([a], b) a
forall a s. a -> s -> Step s a
Yield a
x ([a]
xs,b
nb -> b -> b
forall a. Num a => a -> a -> a
-b
1))
    step ([],b
_)          = Step ([a], b) a -> m (Step ([a], b) a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Step ([a], b) a
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   #-}


-}