{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- |
-- Module      : Data.Vector.Fusion.Bundle.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 bundles.
--

module Data.Vector.Fusion.Bundle.Monadic (
  Bundle(..), Chunk(..), lift,

  -- * Size hints
  size, sized,

  -- * 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, mapMaybeM, 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, unsafeFromList,
  fromVector, reVector, fromVectors, concatVectors,
  fromStream, chunks, elements
) where

import Data.Vector.Generic.Base
import qualified Data.Vector.Generic.Mutable.Base as M
import Data.Vector.Fusion.Bundle.Size
import Data.Vector.Fusion.Util ( Box(..), delay_inline, Id(..) )
import Data.Vector.Fusion.Stream.Monadic ( Stream(..), Step(..) )
import qualified Data.Vector.Fusion.Stream.Monadic as S
import Data.Vector.Internal.Check (check, Checks(..), HasCallStack)
import Control.Monad.Primitive

import qualified Data.List as List
import Data.Char      ( ord )
import GHC.Base       ( unsafeChr )
import Control.Monad  ( liftM )
import Prelude
  ( Eq, Ord, Num, Enum, Functor, Monad, Bool(..), Ordering, Char, Int, Word, Integer, Float, Double, Maybe(..), Either(..), Integral, RealFrac
  , return, fmap, otherwise, id, const, seq, max, maxBound, fromIntegral, truncate
  , (+), (-), (<), (<=), (>), (>=), (==), (/=), (&&), (.), ($), (<$), (/) )

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

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

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

data Chunk v a = Chunk Int (forall m. (PrimMonad m, Vector v a) => Mutable v (PrimState m) a -> m ())

-- | Monadic streams
data Bundle m v a = Bundle { forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems  :: Stream m a
                           , forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
sChunks :: Stream m (Chunk v a)
                           , forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Maybe (v a)
sVector :: Maybe (v a)
                           , forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize   :: Size
                           }

-- | Convert a pure stream to a monadic stream
lift :: Monad m => Bundle Id v a -> Bundle m v a
{-# INLINE_FUSED lift #-}
lift :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle Id v a -> Bundle m v a
lift (Bundle (Stream s -> Id (Step s a)
step s
s) (Stream s -> Id (Step s (Chunk v a))
vstep s
t) Maybe (v a)
v Size
sz)
    = forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Id (Step s a)
step) s
s)
             (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Id a -> a
unId forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Id (Step s (Chunk v a))
vstep) s
t) Maybe (v a)
v Size
sz

fromStream :: Monad m => Stream m a -> Size -> Bundle m v a
{-# INLINE fromStream #-}
fromStream :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Stream s -> m (Step s a)
step s
t) Size
sz = forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step s
t) (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s (Chunk v a))
step' s
t) forall a. Maybe a
Nothing Size
sz
  where
    step' :: s -> m (Step s (Chunk v a))
step' s
s = do Step s a
r <- s -> m (Step s a)
step s
s
                 forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
x -> forall (v :: * -> *) a.
Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
Chunk Int
1 (\Mutable v (PrimState m) a
v -> forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s.
MVector v a =>
v s a -> Int -> a -> ST s ()
M.basicUnsafeWrite Mutable v (PrimState m) a
v Int
0 a
x)) Step s a
r

chunks :: Bundle m v a -> Stream m (Chunk v a)
{-# INLINE chunks #-}
chunks :: forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
chunks = forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
sChunks

elements :: Bundle m v a -> Stream m a
{-# INLINE elements #-}
elements :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
elements = forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

-- | 'Size' hint of a 'Bundle'
size :: Bundle m v a -> Size
{-# INLINE size #-}
size :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
size = forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize

-- | Attach a 'Size' hint to a 'Bundle'
sized :: Bundle m v a -> Size -> Bundle m v a
{-# INLINE_FUSED sized #-}
sized :: forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Size -> Bundle m v a
sized Bundle m v a
s Size
sz = Bundle m v a
s { sSize :: Size
sSize = Size
sz }

-- Length
-- ------

-- | Length of a 'Bundle'
length :: Monad m => Bundle m v a -> m Int
{-# INLINE_FUSED length #-}
length :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> m Int
length Bundle{sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Exact Int
n}  = forall (m :: * -> *) a. Monad m => a -> m a
return Int
n
length Bundle{sChunks :: forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
sChunks = Stream m (Chunk v a)
s} = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
S.foldl' (\Int
n (Chunk Int
k forall (m :: * -> *).
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m ()
_) -> Int
nforall a. Num a => a -> a -> a
+Int
k) Int
0 Stream m (Chunk v a)
s

-- | Check if a 'Bundle' is empty
null :: Monad m => Bundle m v a -> m Bool
{-# INLINE_FUSED null #-}
null :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> m Bool
null Bundle{sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Exact Int
n} = forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n forall a. Eq a => a -> a -> Bool
== Int
0)
null Bundle{sChunks :: forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
sChunks = Stream m (Chunk v a)
s} = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
S.foldr (\(Chunk Int
n forall (m :: * -> *).
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m ()
_) Bool
z -> Int
n forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& Bool
z) Bool
True Stream m (Chunk v a)
s

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

-- | Empty 'Bundle'
empty :: Monad m => Bundle m v a
{-# INLINE_FUSED empty #-}
empty :: forall (m :: * -> *) (v :: * -> *) a. Monad m => Bundle m v a
empty = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream forall (m :: * -> *) a. Monad m => Stream m a
S.empty (Int -> Size
Exact Int
0)

-- | Singleton 'Bundle'
singleton :: Monad m => a -> Bundle m v a
{-# INLINE_FUSED singleton #-}
singleton :: forall (m :: * -> *) a (v :: * -> *). Monad m => a -> Bundle m v a
singleton a
x = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a. Monad m => a -> Stream m a
S.singleton a
x) (Int -> Size
Exact Int
1)

-- | Replicate a value to a given length
replicate :: Monad m => Int -> a -> Bundle m v a
{-# INLINE_FUSED replicate #-}
replicate :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> a -> Bundle m v a
replicate Int
n a
x = forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle (forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
S.replicate Int
n a
x)
                       (forall (m :: * -> *) a. Monad m => a -> Stream m a
S.singleton forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a.
Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
Chunk Int
len (\Mutable v (PrimState m) a
v -> forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (v :: * -> * -> *) a s. MVector v a => v s a -> a -> ST s ()
M.basicSet Mutable v (PrimState m) a
v a
x))
                       forall a. Maybe a
Nothing
                       (Int -> Size
Exact Int
len)
  where
    len :: Int
len = forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max Int
n Int
0

-- | Yield a 'Bundle' of values obtained by performing the monadic action the
-- given number of times
replicateM :: Monad m => Int -> m a -> Bundle m v a
{-# INLINE_FUSED replicateM #-}
-- NOTE: We delay inlining max here because GHC will create a join point for
-- the call to newArray# otherwise which is not really nice.
replicateM :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> m a -> Bundle m v a
replicateM Int
n m a
p = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
S.replicateM Int
n m a
p) (Int -> Size
Exact (forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max Int
n Int
0))

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

-- | Generate a stream from its indices
generateM :: Monad m => Int -> (Int -> m a) -> Bundle m v a
{-# INLINE_FUSED generateM #-}
generateM :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> (Int -> m a) -> Bundle m v a
generateM Int
n Int -> m a
f = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
S.generateM Int
n Int -> m a
f) (Int -> Size
Exact (forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max Int
n Int
0))

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

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

infixr 5 ++
-- | Concatenate two 'Bundle's
(++) :: Monad m => Bundle m v a -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED (++) #-}
Bundle Stream m a
sa Stream m (Chunk v a)
ta Maybe (v a)
_ Size
na ++ :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Bundle m v a -> Bundle m v a
++ Bundle Stream m a
sb Stream m (Chunk v a)
tb Maybe (v a)
_ Size
nb = forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle (Stream m a
sa forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
S.++ Stream m a
sb) (Stream m (Chunk v a)
ta forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
S.++ Stream m (Chunk v a)
tb) forall a. Maybe a
Nothing (Size
na forall a. Num a => a -> a -> a
+ Size
nb)

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

-- | First element of the 'Bundle' or error if empty
head :: Monad m => Bundle m v a -> m a
{-# INLINE_FUSED head #-}
head :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> m a
head = forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> m a
S.head forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

-- | Last element of the 'Bundle' or error if empty
last :: Monad m => Bundle m v a -> m a
{-# INLINE_FUSED last #-}
last :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> m a
last = forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> m a
S.last forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

infixl 9 !!
-- | Element at the given position
(!!) :: Monad m => Bundle m v a -> Int -> m a
{-# INLINE (!!) #-}
Bundle m v a
b !! :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Int -> m a
!! Int
i = forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
b forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> Int -> m a
S.!! Int
i

infixl 9 !?
-- | Element at the given position or 'Nothing' if out of bounds
(!?) :: Monad m => Bundle m v a -> Int -> m (Maybe a)
{-# INLINE (!?) #-}
Bundle m v a
b !? :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Int -> m (Maybe a)
!? Int
i = forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
b forall (m :: * -> *) a. Monad m => Stream m a -> Int -> m (Maybe a)
S.!? Int
i

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

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

-- | All but the last element
init :: Monad m => Bundle m v a -> Bundle m v a
{-# INLINE_FUSED init #-}
init :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Bundle m v a
init Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> Stream m a
S.init Stream m a
s) (Size
szforall a. Num a => a -> a -> a
-Size
1)

-- | All but the first element
tail :: Monad m => Bundle m v a -> Bundle m v a
{-# INLINE_FUSED tail #-}
tail :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Bundle m v a
tail Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> Stream m a
S.tail Stream m a
s) (Size
szforall a. Num a => a -> a -> a
-Size
1)

-- | The first @n@ elements
take :: Monad m => Int -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED take #-}
take :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Int -> Bundle m v a -> Bundle m v a
take Int
n Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
S.take Int
n Stream m a
s) (Int -> Size -> Size
smallerThan Int
n Size
sz)

-- | All but the first @n@ elements
drop :: Monad m => Int -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED drop #-}
drop :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Int -> Bundle m v a -> Bundle m v a
drop Int
n Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} =
  forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
S.drop Int
n Stream m a
s) (Size -> Size -> Size
clampedSubtract Size
sz (Int -> Size
Exact Int
n))

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

instance Monad m => Functor (Bundle m v) where
  {-# INLINE fmap #-}
  fmap :: forall a b. (a -> b) -> Bundle m v a -> Bundle m v b
fmap = forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b) -> Bundle m v a -> Bundle m v b
map
  {-# INLINE (<$) #-}
  <$ :: forall a b. a -> Bundle m v b -> Bundle m v a
(<$) = forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b) -> Bundle m v a -> Bundle m v b
map forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const

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

-- | Map a monadic function over a 'Bundle'
mapM :: Monad m => (a -> m b) -> Bundle m v a -> Bundle m v b
{-# INLINE_FUSED mapM #-}
mapM :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle m v a -> Bundle m v b
mapM a -> m b
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
S.mapM a -> m b
f Stream m a
s) Size
n

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

-- | Transform a 'Bundle' to use a different monad
trans :: (Monad m, Monad m') => (forall z. m z -> m' z)
                             -> Bundle m v a -> Bundle m' v a
{-# INLINE_FUSED trans #-}
trans :: forall (m :: * -> *) (m' :: * -> *) (v :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Bundle m v a -> Bundle m' v a
trans forall z. m z -> m' z
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sChunks :: forall (m :: * -> *) (v :: * -> *) a.
Bundle m v a -> Stream m (Chunk v a)
sChunks = Stream m (Chunk v a)
cs, sVector :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Maybe (v a)
sVector = Maybe (v a)
v, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n}
  = Bundle { sElems :: Stream m' a
sElems = forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
S.trans forall z. m z -> m' z
f Stream m a
s, sChunks :: Stream m' (Chunk v a)
sChunks = forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
S.trans forall z. m z -> m' z
f Stream m (Chunk v a)
cs, sVector :: Maybe (v a)
sVector = Maybe (v a)
v, sSize :: Size
sSize = Size
n }

unbox :: Monad m => Bundle m v (Box a) -> Bundle m v a
{-# INLINE_FUSED unbox #-}
unbox :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v (Box a) -> Bundle m v a
unbox Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m (Box a)
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a. Monad m => Stream m (Box a) -> Stream m a
S.unbox Stream m (Box a)
s) Size
n

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

-- | Pair each element in a 'Bundle' with its index
indexed :: Monad m => Bundle m v a -> Bundle m v (Int,a)
{-# INLINE_FUSED indexed #-}
indexed :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Bundle m v (Int, a)
indexed Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
S.indexed Stream m a
s) Size
n

-- | Pair each element in a 'Bundle' with its index, starting from the right
-- and counting down
indexedR :: Monad m => Int -> Bundle m v a -> Bundle m v (Int,a)
{-# INLINE_FUSED indexedR #-}
indexedR :: forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Int -> Bundle m v a -> Bundle m v (Int, a)
indexedR Int
m Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
Monad m =>
Int -> Stream m a -> Stream m (Int, a)
S.indexedR Int
m Stream m a
s) Size
n

-- | Zip two 'Bundle's with the given monadic function
zipWithM :: Monad m => (a -> b -> m c) -> Bundle m v a -> Bundle m v b -> Bundle m v c
{-# INLINE_FUSED zipWithM #-}
zipWithM :: forall (m :: * -> *) a b c (v :: * -> *).
Monad m =>
(a -> b -> m c) -> Bundle m v a -> Bundle m v b -> Bundle m v c
zipWithM a -> b -> m c
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
sa, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
na}
           Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m b
sb, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
nb} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
S.zipWithM a -> b -> m c
f Stream m a
sa Stream m b
sb) (Size -> Size -> Size
smaller Size
na Size
nb)

-- FIXME: This might expose an opportunity for inplace execution.
{-# RULES

"zipWithM xs xs [Vector.Bundle]" forall f xs.
  zipWithM f (lift xs) (lift xs) = mapM (\x -> f x x) (lift xs) #-}


zipWithM_ :: Monad m => (a -> b -> m c) -> Bundle m v a -> Bundle m v b -> m ()
{-# INLINE zipWithM_ #-}
zipWithM_ :: forall (m :: * -> *) a b c (v :: * -> *).
Monad m =>
(a -> b -> m c) -> Bundle m v a -> Bundle m v b -> m ()
zipWithM_ a -> b -> m c
f Bundle m v a
sa Bundle m v b
sb = forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> m ()
S.zipWithM_ a -> b -> m c
f (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
sa) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v b
sb)

zipWith3M :: Monad m => (a -> b -> c -> m d) -> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
{-# INLINE_FUSED zipWith3M #-}
zipWith3M :: forall (m :: * -> *) a b c d (v :: * -> *).
Monad m =>
(a -> b -> c -> m d)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
zipWith3M a -> b -> c -> m d
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
sa, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
na}
            Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m b
sb, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
nb}
            Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m c
sc, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
nc}
  = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (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
S.zipWith3M a -> b -> c -> m d
f Stream m a
sa Stream m b
sb Stream m c
sc) (Size -> Size -> Size
smaller Size
na (Size -> Size -> Size
smaller Size
nb Size
nc))

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

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

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

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

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

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

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

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

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

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

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

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

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

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

-- | Check if two 'Bundle's are equal
eqBy :: (Monad m) => (a -> b -> Bool) -> Bundle m v a -> Bundle m v b -> m Bool
{-# INLINE_FUSED eqBy #-}
eqBy :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> Bool) -> Bundle m v a -> Bundle m v b -> m Bool
eqBy a -> b -> Bool
eq Bundle m v a
x Bundle m v b
y
  | Size -> Size -> Bool
sizesAreDifferent (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle m v a
x) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle m v b
y) = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
  | Bool
otherwise                             = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
S.eqBy a -> b -> Bool
eq (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
x) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v b
y)
  where
    sizesAreDifferent :: Size -> Size -> Bool
    sizesAreDifferent :: Size -> Size -> Bool
sizesAreDifferent (Exact Int
a) (Exact Int
b) = Int
a forall a. Eq a => a -> a -> Bool
/= Int
b
    sizesAreDifferent (Exact Int
a) (Max Int
b)   = Int
a forall a. Ord a => a -> a -> Bool
> Int
b
    sizesAreDifferent (Max Int
a)   (Exact Int
b) = Int
a forall a. Ord a => a -> a -> Bool
< Int
b
    sizesAreDifferent Size
_         Size
_         = Bool
False

-- | Lexicographically compare two 'Bundle's
cmpBy :: (Monad m) => (a -> b -> Ordering) -> Bundle m v a -> Bundle m v b -> m Ordering
{-# INLINE_FUSED cmpBy #-}
cmpBy :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> Ordering) -> Bundle m v a -> Bundle m v b -> m Ordering
cmpBy a -> b -> Ordering
cmp Bundle m v a
x Bundle m v b
y = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
S.cmpBy a -> b -> Ordering
cmp (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
x) (forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v b
y)

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

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

-- | Drop elements which do not satisfy the monadic predicate
filterM :: Monad m => (a -> m Bool) -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED filterM #-}
filterM :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> m Bool) -> Bundle m v a -> Bundle m v a
filterM a -> m Bool
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
S.filterM a -> m Bool
f Stream m a
s) (Size -> Size
toMax Size
n)

-- | Apply monadic function to each element and drop all Nothings
--
-- @since 0.12.2.0
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Bundle m v a -> Bundle m v b
{-# INLINE_FUSED mapMaybeM #-}
mapMaybeM :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m (Maybe b)) -> Bundle m v a -> Bundle m v b
mapMaybeM a -> m (Maybe b)
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a b.
Monad m =>
(a -> m (Maybe b)) -> Stream m a -> Stream m b
S.mapMaybeM a -> m (Maybe b)
f Stream m a
s) (Size -> Size
toMax Size
n)

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

-- | Longest prefix of elements that satisfy the monadic predicate
takeWhileM :: Monad m => (a -> m Bool) -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED takeWhileM #-}
takeWhileM :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> m Bool) -> Bundle m v a -> Bundle m v a
takeWhileM a -> m Bool
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
S.takeWhileM a -> m Bool
f Stream m a
s) (Size -> Size
toMax Size
n)

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

-- | Drop the longest prefix of elements that satisfy the monadic predicate
dropWhileM :: Monad m => (a -> m Bool) -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED dropWhileM #-}
dropWhileM :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> m Bool) -> Bundle m v a -> Bundle m v a
dropWhileM a -> m Bool
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
S.dropWhileM a -> m Bool
f Stream m a
s) (Size -> Size
toMax Size
n)

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

infix 4 `elem`
-- | Check whether the 'Bundle' contains an element
elem :: (Monad m, Eq a) => a -> Bundle m v a -> m Bool
{-# INLINE_FUSED elem #-}
elem :: forall (m :: * -> *) a (v :: * -> *).
(Monad m, Eq a) =>
a -> Bundle m v a -> m Bool
elem a
x = forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
S.elem a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

infix 4 `notElem`
-- | Inverse of `elem`
notElem :: (Monad m, Eq a) => a -> Bundle m v a -> m Bool
{-# INLINE notElem #-}
notElem :: forall (m :: * -> *) a (v :: * -> *).
(Monad m, Eq a) =>
a -> Bundle m v a -> m Bool
notElem a
x = forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
S.notElem a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

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

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

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

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

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

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

-- | Left fold with a monadic operator
foldlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> m a
{-# INLINE_FUSED foldlM #-}
foldlM :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
foldlM a -> b -> m a
m a
z = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
S.foldlM a -> b -> m a
m a
z forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

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

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

-- | Left fold over a non-empty 'Bundle' with a monadic operator
foldl1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
{-# INLINE_FUSED foldl1M #-}
foldl1M :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> m a
foldl1M a -> a -> m a
f = forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
S.foldl1M a -> a -> m a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

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

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

-- | Left fold with a strict accumulator and a monadic operator
foldlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> m a
{-# INLINE_FUSED foldlM' #-}
foldlM' :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
foldlM' a -> b -> m a
m a
z = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
S.foldlM' a -> b -> m a
m a
z forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

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

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

-- | Left fold over a non-empty 'Bundle' with a strict accumulator and a
-- monadic operator
foldl1M' :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
{-# INLINE_FUSED foldl1M' #-}
foldl1M' :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> m a
foldl1M' a -> a -> m a
f = forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
S.foldl1M' a -> a -> m a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

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

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

-- | Right fold with a monadic operator
foldrM :: Monad m => (a -> b -> m b) -> b -> Bundle m v a -> m b
{-# INLINE_FUSED foldrM #-}
foldrM :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m b) -> b -> Bundle m v a -> m b
foldrM a -> b -> m b
f b
z = forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Stream m a -> m b
S.foldrM a -> b -> m b
f b
z forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

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

-- | Right fold over a non-empty stream with a monadic operator
foldr1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
{-# INLINE_FUSED foldr1M #-}
foldr1M :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> m a
foldr1M a -> a -> m a
f = forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
S.foldr1M a -> a -> m a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

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

and :: Monad m => Bundle m v Bool -> m Bool
{-# INLINE_FUSED and #-}
and :: forall (m :: * -> *) (v :: * -> *).
Monad m =>
Bundle m v Bool -> m Bool
and = forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
S.and forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

or :: Monad m => Bundle m v Bool -> m Bool
{-# INLINE_FUSED or #-}
or :: forall (m :: * -> *) (v :: * -> *).
Monad m =>
Bundle m v Bool -> m Bool
or = forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
S.or forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems

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

concatMapM :: Monad m => (a -> m (Bundle m v b)) -> Bundle m v a -> Bundle m v b
{-# INLINE_FUSED concatMapM #-}
concatMapM :: forall (m :: * -> *) a (v :: * -> *) b.
Monad m =>
(a -> m (Bundle m v b)) -> Bundle m v a -> Bundle m v b
concatMapM a -> m (Bundle m v b)
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a b.
Monad m =>
(a -> m (Stream m b)) -> Stream m a -> Stream m b
S.concatMapM (forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m (Bundle m v b)
f) Stream m a
s) Size
Unknown

-- | Create a 'Bundle' of values from a 'Bundle' of streamable things
flatten :: Monad m => (a -> m s) -> (s -> m (Step s b)) -> Size
                   -> Bundle m v a -> Bundle m v b
{-# INLINE_FUSED flatten #-}
flatten :: forall (m :: * -> *) a s b (v :: * -> *).
Monad m =>
(a -> m s)
-> (s -> m (Step s b)) -> Size -> Bundle m v a -> Bundle m v b
flatten a -> m s
mk s -> m (Step s b)
istep Size
sz Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a s b.
Monad m =>
(a -> m s) -> (s -> m (Step s b)) -> Stream m a -> Stream m b
S.flatten a -> m s
mk s -> m (Step s b)
istep Stream m a
s) Size
sz

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

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

-- | Unfold with a monadic function
unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Bundle m u a
{-# INLINE_FUSED unfoldrM #-}
unfoldrM :: forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Bundle m u a
unfoldrM s -> m (Maybe (a, s))
f s
s = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrM s -> m (Maybe (a, s))
f s
s) Size
Unknown

-- | Unfold at most @n@ elements
unfoldrN :: Monad m => Int -> (s -> Maybe (a, s)) -> s -> Bundle m u a
{-# INLINE_FUSED unfoldrN #-}
unfoldrN :: forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
Int -> (s -> Maybe (a, s)) -> s -> Bundle m u a
unfoldrN Int
n s -> Maybe (a, s)
f = forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Bundle m u a
unfoldrNM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
f)

-- | Unfold at most @n@ elements with a monadic function.
unfoldrNM :: Monad m => Int -> (s -> m (Maybe (a, s))) -> s -> Bundle m u a
{-# INLINE_FUSED unfoldrNM #-}
unfoldrNM :: forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Bundle m u a
unfoldrNM Int
n s -> m (Maybe (a, s))
f s
s = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrNM Int
n s -> m (Maybe (a, s))
f s
s) Size
Unknown

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

-- | Unfold exactly @n@ elements with a monadic function.
--
-- @since 0.12.2.0
unfoldrExactNM :: Monad m => Int -> (s -> m (a, s)) -> s -> Bundle m u a
{-# INLINE_FUSED unfoldrExactNM #-}
unfoldrExactNM :: forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
Int -> (s -> m (a, s)) -> s -> Bundle m u a
unfoldrExactNM Int
n s -> m (a, s)
f s
s = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (a, s)) -> s -> Stream m a
S.unfoldrExactNM Int
n s -> m (a, s)
f s
s) (Int -> Size
Max (forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max Int
n Int
0))

-- | /O(n)/ Apply monadic function \(\max(n - 1, 0)\) times to an initial value, producing
-- a monadic bundle of exact length \(\max(n, 0)\). Zeroth element will contain the initial
-- value.
iterateNM :: Monad m => Int -> (a -> m a) -> a -> Bundle m u a
{-# INLINE_FUSED iterateNM #-}
iterateNM :: forall (m :: * -> *) a (u :: * -> *).
Monad m =>
Int -> (a -> m a) -> a -> Bundle m u a
iterateNM Int
n a -> m a
f a
x0 = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m a) -> a -> Stream m a
S.iterateNM Int
n a -> m a
f a
x0) (Int -> Size
Exact (forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max Int
n Int
0))

-- | /O(n)/ Apply function \(\max(n - 1, 0)\) times to an initial value, producing a
-- monadic bundle of exact length \(\max(n, 0)\). Zeroth element will contain the initial
-- value.
iterateN :: Monad m => Int -> (a -> a) -> a -> Bundle m u a
{-# INLINE_FUSED iterateN #-}
iterateN :: forall (m :: * -> *) a (u :: * -> *).
Monad m =>
Int -> (a -> a) -> a -> Bundle m u a
iterateN Int
n a -> a
f a
x0 = forall (m :: * -> *) a (u :: * -> *).
Monad m =>
Int -> (a -> m a) -> a -> Bundle m u a
iterateNM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f) a
x0

-- Scans
-- -----

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

-- | Prefix scan with a monadic operator
prescanlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE_FUSED prescanlM #-}
prescanlM :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
prescanlM a -> b -> m a
f a
z Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m b
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
S.prescanlM a -> b -> m a
f a
z Stream m b
s) Size
sz

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

-- | Prefix scan with strict accumulator and a monadic operator
prescanlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE_FUSED prescanlM' #-}
prescanlM' :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
prescanlM' a -> b -> m a
f a
z Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m b
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
S.prescanlM' a -> b -> m a
f a
z Stream m b
s) Size
sz

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

-- | Suffix scan with a monadic operator
postscanlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE_FUSED postscanlM #-}
postscanlM :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
postscanlM a -> b -> m a
f a
z Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m b
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
S.postscanlM a -> b -> m a
f a
z Stream m b
s) Size
sz

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

-- | Suffix scan with strict accumulator and a monadic operator
postscanlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE_FUSED postscanlM' #-}
postscanlM' :: forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
postscanlM' a -> b -> m a
f a
z Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m b
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
S.postscanlM' a -> b -> m a
f a
z Stream m b
s) Size
sz

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

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

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

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

-- | Initial-value free scan over a 'Bundle'
scanl1 :: Monad m => (a -> a -> a) -> Bundle m v a -> Bundle m v a
{-# INLINE scanl1 #-}
scanl1 :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> a) -> Bundle m v a -> Bundle m v a
scanl1 a -> a -> a
f = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> Bundle m v a
scanl1M (\a
x a
y -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-- | Initial-value free scan over a 'Bundle' with a monadic operator
scanl1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED scanl1M #-}
scanl1M :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> Bundle m v a
scanl1M a -> a -> m a
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
S.scanl1M a -> a -> m a
f Stream m a
s) Size
sz

-- | Initial-value free scan over a 'Bundle' with a strict accumulator
scanl1' :: Monad m => (a -> a -> a) -> Bundle m v a -> Bundle m v a
{-# INLINE scanl1' #-}
scanl1' :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> a) -> Bundle m v a -> Bundle m v a
scanl1' a -> a -> a
f = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> Bundle m v a
scanl1M' (\a
x a
y -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-- | Initial-value free scan over a 'Bundle' with a strict accumulator
-- and a monadic operator
scanl1M' :: Monad m => (a -> a -> m a) -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED scanl1M' #-}
scanl1M' :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> Bundle m v a
scanl1M' a -> a -> m a
f Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
sz} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
S.scanl1M' a -> a -> m a
f Stream m a
s) Size
sz

-- 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 'Bundle' of the given length containing the values @x@, @x+y@,
-- @x+y+y@ etc.
enumFromStepN :: (Num a, Monad m) => a -> a -> Int -> Bundle m v a
{-# INLINE_FUSED enumFromStepN #-}
enumFromStepN :: forall a (m :: * -> *) (v :: * -> *).
(Num a, Monad m) =>
a -> a -> Int -> Bundle m v a
enumFromStepN a
x a
y Int
n = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall a (m :: * -> *).
(Num a, Monad m) =>
a -> a -> Int -> Stream m a
S.enumFromStepN a
x a
y Int
n) (Int -> Size
Exact (forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max Int
n Int
0))

-- | Enumerate values
--
-- /WARNING:/ This operation can be very inefficient. If at all possible, use
-- 'enumFromStepN' instead.
enumFromTo :: (Enum a, Monad m) => a -> a -> Bundle m v a
{-# INLINE_FUSED enumFromTo #-}
enumFromTo :: forall a (m :: * -> *) (v :: * -> *).
(Enum a, Monad m) =>
a -> a -> Bundle m v a
enumFromTo a
x a
y = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
[a] -> Bundle m v 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 -> Bundle m v a
{-# INLINE_FUSED enumFromTo_small #-}
enumFromTo_small :: forall a (m :: * -> *) (v :: * -> *).
(Integral a, Monad m) =>
a -> a -> Bundle m v a
enumFromTo_small a
x a
y = a
x seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (forall a. a -> Maybe a
Just a
x)) (Int -> Size
Exact Int
n)
  where
    n :: Int
n = forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max (forall a b. (Integral a, Num b) => a -> b
fromIntegral a
y forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral a
x forall a. Num a => a -> a -> a
+ Int
1) Int
0

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

{-# RULES

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

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

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

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



#if WORD_SIZE_IN_BITS > 32

{-# RULES

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

"enumFromTo<Word32> [Bundle]"
  enumFromTo = enumFromTo_small :: Monad m => Word32 -> Word32 -> Bundle m v 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 v. (HasCallStack, Monad m) => Int -> Int -> Bundle m v Int
{-# INLINE_FUSED enumFromTo_int #-}
enumFromTo_int :: forall (m :: * -> *) (v :: * -> *).
(HasCallStack, Monad m) =>
Int -> Int -> Bundle m v Int
enumFromTo_int Int
x Int
y = Int
x seq :: forall a b. a -> b -> b
`seq` Int
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe Int -> m (Step (Maybe Int) Int)
step (forall a. a -> Maybe a
Just Int
x)) (Int -> Size
Exact (HasCallStack => Int -> Int -> Int
len Int
x Int
y))
  where
    {-# INLINE [0] len #-}
    len :: HasCallStack => Int -> Int -> Int
    len :: HasCallStack => Int -> Int -> Int
len Int
u Int
v | Int
u forall a. Ord a => a -> a -> Bool
> Int
v     = Int
0
            | Bool
otherwise = forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Bounds String
"vector too large" (Int
n forall a. Ord a => a -> a -> Bool
> Int
0) Int
n
      where
        n :: Int
n = Int
vforall a. Num a => a -> a -> a
-Int
uforall a. Num a => a -> a -> a
+Int
1

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

enumFromTo_intlike :: forall m v a. (HasCallStack, Integral a, Monad m) => a -> a -> Bundle m v a
{-# INLINE_FUSED enumFromTo_intlike #-}
enumFromTo_intlike :: forall (m :: * -> *) (v :: * -> *) a.
(HasCallStack, Integral a, Monad m) =>
a -> a -> Bundle m v a
enumFromTo_intlike a
x a
y = a
x seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (forall a. a -> Maybe a
Just a
x)) (Int -> Size
Exact (HasCallStack => a -> a -> Int
len a
x a
y))
  where
    {-# INLINE [0] len #-}
    len :: HasCallStack => a -> a -> Int
    len :: HasCallStack => a -> a -> Int
len a
u a
v | a
u forall a. Ord a => a -> a -> Bool
> a
v     = Int
0
            | Bool
otherwise = forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Bounds String
"vector too large"
                          (a
n forall a. Ord a => a -> a -> Bool
> a
0)
                        forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n
      where
        n :: a
n = a
vforall a. Num a => a -> a -> a
-a
uforall a. Num a => a -> a -> a
+a
1

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

{-# RULES

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

#if WORD_SIZE_IN_BITS > 32

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

#else

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

#endif



enumFromTo_big_word :: forall m v a. (HasCallStack, Integral a, Monad m) => a -> a -> Bundle m v a
{-# INLINE_FUSED enumFromTo_big_word #-}
enumFromTo_big_word :: forall (m :: * -> *) (v :: * -> *) a.
(HasCallStack, Integral a, Monad m) =>
a -> a -> Bundle m v a
enumFromTo_big_word a
x a
y = a
x seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (forall a. a -> Maybe a
Just a
x)) (Int -> Size
Exact (HasCallStack => a -> a -> Int
len a
x a
y))
  where
    {-# INLINE [0] len #-}
    len :: HasCallStack => a -> a -> Int
    len :: HasCallStack => a -> a -> Int
len a
u a
v | a
u forall a. Ord a => a -> a -> Bool
> a
v     = Int
0
            | Bool
otherwise = forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Bounds String
"vector too large"
                          (a
n forall a. Ord a => a -> a -> Bool
< forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bounded a => a
maxBound :: Int))
                        forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral (a
nforall a. Num a => a -> a -> a
+a
1)
      where
        n :: a
n = a
vforall a. Num a => a -> a -> a
-a
u

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

{-# RULES

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

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

#if WORD_SIZE_IN_BITS == 32

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

#endif

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


#if WORD_SIZE_IN_BITS > 32

-- FIXME: the "too large" test is totally wrong
enumFromTo_big_int :: forall m v a. (HasCallStack, Integral a, Monad m) => a -> a -> Bundle m v a
{-# INLINE_FUSED enumFromTo_big_int #-}
enumFromTo_big_int :: forall (m :: * -> *) (v :: * -> *) a.
(HasCallStack, Integral a, Monad m) =>
a -> a -> Bundle m v a
enumFromTo_big_int a
x a
y = a
x seq :: forall a b. a -> b -> b
`seq` a
y seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (forall a. a -> Maybe a
Just a
x)) (Int -> Size
Exact (HasCallStack => a -> a -> Int
len a
x a
y))
  where
    {-# INLINE [0] len #-}
    len :: HasCallStack => a -> a -> Int
    len :: HasCallStack => a -> a -> Int
len a
u a
v | a
u forall a. Ord a => a -> a -> Bool
> a
v     = Int
0
            | Bool
otherwise = forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Bounds String
"vector too large"
                          (a
n forall a. Ord a => a -> a -> Bool
> a
0 Bool -> Bool -> Bool
&& a
n forall a. Ord a => a -> a -> Bool
<= forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bounded a => a
maxBound :: Int))
                        forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral a
n
      where
        n :: a
n = a
vforall a. Num a => a -> a -> a
-a
uforall a. Num a => a -> a -> a
+a
1

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


{-# RULES

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



#endif

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

    n :: Int
n = forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max Int
0 (Int
yn forall a. Num a => a -> a -> a
- Int
xn forall a. Num a => a -> a -> a
+ Int
1)

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

{-# RULES

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



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

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

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

    {-# INLINE [0] len #-}
    len :: HasCallStack => a -> a -> Int
    len :: HasCallStack => a -> a -> Int
len a
x a
y | a
x forall a. Ord a => a -> a -> Bool
> a
y     = Int
0
            | Bool
otherwise = forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Bounds String
"vector too large" (Integer
l forall a. Ord a => a -> a -> Bool
> Integer
0) forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
l
      where
        l :: Integer
        l :: Integer
l = forall a b. (RealFrac a, Integral b) => a -> b
truncate (a
yforall a. Num a => a -> a -> a
-a
x)forall a. Num a => a -> a -> a
+Integer
2

    {-# INLINE_INNER step #-}
-- 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' forall a. Ord a => a -> a -> Bool
<= a
lim = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x' (a
xforall a. Num a => a -> a -> a
+a
1)
           | Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. Step s a
Done
           where
             x' :: a
x' = a
x forall a. Num a => a -> a -> a
+ a
n
#else
    ini = n
    step x | x <= lim  = return $ Yield x (x+1)
           | otherwise = return $ Done
#endif

{-# RULES

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

"enumFromTo<Float> [Bundle]"
  enumFromTo = enumFromTo_double :: Monad m => Float -> Float -> Bundle m v 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 -> Bundle m v a
{-# INLINE_FUSED enumFromThenTo #-}
enumFromThenTo :: forall a (m :: * -> *) (v :: * -> *).
(Enum a, Monad m) =>
a -> a -> a -> Bundle m v a
enumFromThenTo a
x a
y a
z = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
[a] -> Bundle m v a
fromList [a
x, a
y .. a
z]

-- FIXME: Specialise enumFromThenTo.

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

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

-- | Convert a list to a 'Bundle'
fromList :: Monad m => [a] -> Bundle m v a
{-# INLINE fromList #-}
fromList :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
[a] -> Bundle m v a
fromList [a]
xs = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Size -> [a] -> Bundle m v a
unsafeFromList Size
Unknown [a]
xs

-- | Convert the first @n@ elements of a list to a 'Bundle'
fromListN :: Monad m => Int -> [a] -> Bundle m v a
{-# INLINE_FUSED fromListN #-}
fromListN :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> [a] -> Bundle m v a
fromListN Int
n [a]
xs = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
S.fromListN Int
n [a]
xs) (Int -> Size
Max (forall a b. (a -> b) -> a -> b
delay_inline forall a. Ord a => a -> a -> a
max Int
n Int
0))

-- | Convert a list to a 'Bundle' with the given 'Size' hint.
unsafeFromList :: Monad m => Size -> [a] -> Bundle m v a
{-# INLINE_FUSED unsafeFromList #-}
unsafeFromList :: forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Size -> [a] -> Bundle m v a
unsafeFromList Size
sz [a]
xs = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [a]
xs) Size
sz

fromVector :: (Monad m, Vector v a) => v a -> Bundle m v a
{-# INLINE_FUSED fromVector #-}
fromVector :: forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
v a -> Bundle m v a
fromVector v a
v = v a
v seq :: forall a b. a -> b -> b
`seq` Int
n seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Int -> m (Step Int a)
step Int
0)
                                      (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Bool -> m (Step Bool (Chunk v a))
vstep Bool
True)
                                      (forall a. a -> Maybe a
Just v a
v)
                                      (Int -> Size
Exact Int
n)
  where
    n :: Int
n = forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v

    {-# INLINE step #-}
    step :: Int -> m (Step Int a)
step Int
i | Int
i forall a. Ord a => a -> a -> Bool
>= Int
n = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done
           | Bool
otherwise = case forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
basicUnsafeIndexM v a
v Int
i of
                           Box a
x -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (Int
iforall a. Num a => a -> a -> a
+Int
1)


    {-# INLINE vstep #-}
    vstep :: Bool -> m (Step Bool (Chunk v a))
vstep Bool
True  = forall (m :: * -> *) a. Monad m => a -> m a
return (forall a s. a -> s -> Step s a
Yield (forall (v :: * -> *) a.
Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
Chunk (forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v) (\Mutable v (PrimState m) a
mv -> forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a s.
Vector v a =>
Mutable v s a -> v a -> ST s ()
basicUnsafeCopy Mutable v (PrimState m) a
mv v a
v)) Bool
False)
    vstep Bool
False = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

fromVectors :: forall m v a. (Monad m, Vector v a) => [v a] -> Bundle m v a
{-# INLINE_FUSED fromVectors #-}
fromVectors :: forall (m :: * -> *) (v :: * -> *) a.
(Monad m, Vector v a) =>
[v a] -> Bundle m v a
fromVectors [v a]
us = forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {v :: * -> *} {a}.
(Monad m, Vector v a) =>
Either [v a] (v a, Int, [v a])
-> m (Step (Either [v a] (v a, Int, [v a])) a)
pstep (forall a b. a -> Either a b
Left [v a]
us))
                        (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream HasCallStack => [v a] -> m (Step [v a] (Chunk v a))
vstep [v a]
us)
                        forall a. Maybe a
Nothing
                        (Int -> Size
Exact Int
n)
  where
    n :: Int
n = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' (\Int
k v a
v -> Int
k forall a. Num a => a -> a -> a
+ forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v) Int
0 [v a]
us

    pstep :: Either [v a] (v a, Int, [v a])
-> m (Step (Either [v a] (v a, Int, [v a])) a)
pstep (Left []) = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done
    pstep (Left (v a
v:[v a]
vs)) = forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (v a
v,Int
0,[v a]
vs)))

    pstep (Right (v a
v,Int
i,[v a]
vs))
      | Int
i forall a. Ord a => a -> a -> Bool
>= forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left [v a]
vs)
      | Bool
otherwise          = case forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
basicUnsafeIndexM v a
v Int
i of
                               Box a
x -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield a
x (forall a b. b -> Either a b
Right (v a
v,Int
iforall a. Num a => a -> a -> a
+Int
1,[v a]
vs))

    -- FIXME: work around bug in GHC 7.6.1
    vstep :: HasCallStack => [v a] -> m (Step [v a] (Chunk v a))
    vstep :: HasCallStack => [v a] -> m (Step [v a] (Chunk v a))
vstep [] = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done
    vstep (v a
v:[v a]
vs) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a s. a -> s -> Step s a
Yield (forall (v :: * -> *) a.
Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
Chunk (forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v)
                                         (\Mutable v (PrimState m) a
mv -> forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check
                                                 Checks
Internal
                                                 String
"length mismatch"
                                                 (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
M.basicLength Mutable v (PrimState m) a
mv forall a. Eq a => a -> a -> Bool
== forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v)
                                                 forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a s.
Vector v a =>
Mutable v s a -> v a -> ST s ()
basicUnsafeCopy Mutable v (PrimState m) a
mv v a
v)) [v a]
vs


concatVectors :: (Monad m, Vector v a) => Bundle m u (v a) -> Bundle m v a
{-# INLINE_FUSED concatVectors #-}
concatVectors :: forall (m :: * -> *) (v :: * -> *) a (u :: * -> *).
(Monad m, Vector v a) =>
Bundle m u (v a) -> Bundle m v a
concatVectors Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream s -> m (Step s (v a))
step s
t}
  = forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either s (v a, Int, s) -> m (Step (Either s (v a, Int, s)) a)
pstep (forall a b. a -> Either a b
Left s
t))
           (forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s (Chunk v a))
vstep s
t)
           forall a. Maybe a
Nothing
           Size
Unknown
  where
    pstep :: Either s (v a, Int, s) -> m (Step (Either s (v a, Int, s)) a)
pstep (Left s
s) = do
      Step s (v a)
r <- s -> m (Step s (v a))
step s
s
      case Step s (v a)
r of
        Yield v a
v s
s' -> forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (v a
v,Int
0,s
s')))
        Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s'))
        Step s (v a)
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

    pstep (Right (v a
v,Int
i,s
s))
      | Int
i forall a. Ord a => a -> a -> Bool
>= forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v = forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s))
      | Bool
otherwise          = case forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
basicUnsafeIndexM v a
v Int
i of
                               Box a
x -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a s. a -> s -> Step s a
Yield a
x (forall a b. b -> Either a b
Right (v a
v,Int
iforall a. Num a => a -> a -> a
+Int
1,s
s)))


    vstep :: s -> m (Step s (Chunk v a))
vstep s
s = do
      Step s (v a)
r <- s -> m (Step s (v a))
step s
s
      case Step s (v a)
r of
        Yield v a
v s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a s. a -> s -> Step s a
Yield (forall (v :: * -> *) a.
Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
Chunk (forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v)
                                           (\Mutable v (PrimState m) a
mv -> forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check
                                                   Checks
Internal
                                                   String
"length mismatch"
                                                   (forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
M.basicLength Mutable v (PrimState m) a
mv forall a. Eq a => a -> a -> Bool
== forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v)
                                                   forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim forall a b. (a -> b) -> a -> b
$ forall (v :: * -> *) a s.
Vector v a =>
Mutable v s a -> v a -> ST s ()
basicUnsafeCopy Mutable v (PrimState m) a
mv v a
v)) s
s')
        Skip    s
s' -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall s a. s -> Step s a
Skip s
s')
        Step s (v a)
Done       -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Done

reVector :: Monad m => Bundle m u a -> Bundle m v a
{-# INLINE_FUSED reVector #-}
reVector :: forall (m :: * -> *) (u :: * -> *) a (v :: * -> *).
Monad m =>
Bundle m u a -> Bundle m v a
reVector Bundle{sElems :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems = Stream m a
s, sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Size
n} = forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream Stream m a
s Size
n

{-# RULES

"reVector [Vector]"
  reVector = id

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