{-# 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 hiding ( length, null,
                        replicate, (++),
                        head, last, (!!),
                        init, tail, take, drop,
                        map, mapM, mapM_, concatMap,
                        zipWith, zipWith3, zip, zip3,
                        filter, takeWhile, dropWhile,
                        elem, notElem,
                        foldl, foldl1, foldr, foldr1,
                        and, or,
                        scanl, scanl1,
                        enumFromTo, enumFromThenTo )

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

#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 { Bundle m v a -> Stream m a
sElems  :: Stream m a
                           , Bundle m v a -> Stream m (Chunk v a)
sChunks :: Stream m (Chunk v a)
                           , Bundle m v a -> Maybe (v a)
sVector :: Maybe (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 :: 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)
    = Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle ((s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> (s -> Step s a) -> s -> m (Step s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id (Step s a) -> Step s a
forall a. Id a -> a
unId (Id (Step s a) -> Step s a)
-> (s -> Id (Step s a)) -> s -> Step s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Id (Step s a)
step) s
s)
             ((s -> m (Step s (Chunk v a))) -> s -> Stream m (Chunk v a)
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream (Step s (Chunk v a) -> m (Step s (Chunk v a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s (Chunk v a) -> m (Step s (Chunk v a)))
-> (s -> Step s (Chunk v a)) -> s -> m (Step s (Chunk v a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id (Step s (Chunk v a)) -> Step s (Chunk v a)
forall a. Id a -> a
unId (Id (Step s (Chunk v a)) -> Step s (Chunk v a))
-> (s -> Id (Step s (Chunk v a))) -> s -> Step s (Chunk v a)
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 :: Stream m a -> Size -> Bundle m v a
fromStream (Stream s -> m (Step s a)
step s
t) Size
sz = Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle ((s -> m (Step s a)) -> s -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s a)
step s
t) ((s -> m (Step s (Chunk v a))) -> s -> Stream m (Chunk v a)
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s (Chunk v a))
step' s
t) Maybe (v a)
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
                 Step s (Chunk v a) -> m (Step s (Chunk v a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s (Chunk v a) -> m (Step s (Chunk v a)))
-> Step s (Chunk v a) -> m (Step s (Chunk v a))
forall a b. (a -> b) -> a -> b
$ (a -> Chunk v a) -> Step s a -> Step s (Chunk v a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
x -> Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
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 -> ST (PrimState m) () -> m ()
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) () -> m ()) -> ST (PrimState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ Mutable v (PrimState m) a -> Int -> a -> ST (PrimState m) ()
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 :: Bundle m v a -> Stream m (Chunk v a)
chunks = Bundle m v a -> Stream m (Chunk v a)
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 :: Bundle m v a -> Stream m a
elements = Bundle m v a -> Stream m a
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 :: Bundle m v a -> Size
size = Bundle m v a -> 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 :: 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 :: Bundle m v a -> m Int
length Bundle{sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Exact Int
n}  = Int -> m Int
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} = (Int -> Chunk v a -> Int) -> Int -> Stream m (Chunk v a) -> m Int
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
nInt -> Int -> Int
forall 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 :: Bundle m v a -> m Bool
null Bundle{sSize :: forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize = Exact Int
n} = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n Int -> Int -> Bool
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} = (Chunk v a -> Bool -> Bool)
-> Bool -> Stream m (Chunk v a) -> m Bool
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 Int -> Int -> Bool
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 :: Bundle m v a
empty = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream Stream m a
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 :: a -> Bundle m v a
singleton a
x = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (a -> Stream m a
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 :: Int -> a -> Bundle m v a
replicate Int
n a
x = Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle (Int -> a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
S.replicate Int
n a
x)
                       (Chunk v a -> Stream m (Chunk v a)
forall (m :: * -> *) a. Monad m => a -> Stream m a
S.singleton (Chunk v a -> Stream m (Chunk v a))
-> Chunk v a -> Stream m (Chunk v a)
forall a b. (a -> b) -> a -> b
$ Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
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 -> ST (PrimState m) () -> m ()
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) () -> m ()) -> ST (PrimState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ Mutable v (PrimState m) a -> a -> ST (PrimState m) ()
forall (v :: * -> * -> *) a s. MVector v a => v s a -> a -> ST s ()
M.basicSet Mutable v (PrimState m) a
v a
x))
                       Maybe (v a)
forall a. Maybe a
Nothing
                       (Int -> Size
Exact Int
len)
  where
    len :: Int
len = (Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
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 :: Int -> m a -> Bundle m v a
replicateM Int
n m a
p = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Int -> m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
S.replicateM Int
n m a
p) (Int -> Size
Exact ((Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
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 :: Int -> (Int -> a) -> Bundle m v a
generate Int
n Int -> a
f = Int -> (Int -> m a) -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Int -> (Int -> m a) -> Bundle m v a
generateM Int
n (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (Int -> a) -> Int -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
f)

-- | Generate a stream from its indices
generateM :: Monad m => Int -> (Int -> m a) -> Bundle m v a
{-# INLINE_FUSED generateM #-}
generateM :: Int -> (Int -> m a) -> Bundle m v a
generateM Int
n Int -> m a
f = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Int -> (Int -> m a) -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
S.generateM Int
n Int -> m a
f) (Int -> Size
Exact ((Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
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 :: a -> Bundle m v a -> Bundle m v a
cons a
x Bundle m v a
s = a -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *). Monad m => a -> Bundle m v a
singleton a
x Bundle m v a -> Bundle m v a -> Bundle m v a
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 :: Bundle m v a -> a -> Bundle m v a
snoc Bundle m v a
s a
x = Bundle m v a
s Bundle m v a -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Bundle m v a -> Bundle m v a -> Bundle m v a
++ 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 ++ :: 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 = Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
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 Stream m a -> Stream m a -> Stream m a
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 Stream m (Chunk v a)
-> Stream m (Chunk v a) -> Stream m (Chunk v a)
forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
S.++ Stream m (Chunk v a)
tb) Maybe (v a)
forall a. Maybe a
Nothing (Size
na Size -> Size -> Size
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 :: Bundle m v a -> m a
head = Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> m a
S.head (Stream m a -> m a)
-> (Bundle m v a -> Stream m a) -> Bundle m v a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v a -> Stream m a
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 :: Bundle m v a -> m a
last = Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> m a
S.last (Stream m a -> m a)
-> (Bundle m v a -> Stream m a) -> Bundle m v a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v a -> Stream m a
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 !! :: Bundle m v a -> Int -> m a
!! Int
i = Bundle m v a -> Stream m a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
b Stream m a -> Int -> m a
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 !? :: Bundle m v a -> Int -> m (Maybe a)
!? Int
i = Bundle m v a -> Stream m a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
b Stream m a -> Int -> m (Maybe a)
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 :: Int -> Int -> Bundle m v a -> Bundle m v a
slice Int
i Int
n Bundle m v a
s = Int -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Monad m =>
Int -> Bundle m v a -> Bundle m v a
take Int
n (Int -> Bundle m v a -> Bundle m v a
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 :: 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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Stream m a -> Stream m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> Stream m a
S.init Stream m a
s) (Size
szSize -> Size -> Size
forall 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 :: 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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Stream m a -> Stream m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
Stream m a -> Stream m a
S.tail Stream m a
s) (Size
szSize -> Size -> Size
forall 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 :: 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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Int -> Stream m a -> Stream m a
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 :: 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} =
  Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Int -> Stream m a -> Stream m a
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 :: (a -> b) -> Bundle m v a -> Bundle m v b
fmap = (a -> b) -> Bundle m v a -> Bundle m v b
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b) -> Bundle m v a -> Bundle m v b
map
  {-# INLINE (<$) #-}
  <$ :: a -> Bundle m v b -> Bundle m v 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 ((b -> a) -> Bundle m v b -> Bundle m v a)
-> (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> a
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 :: (a -> b) -> Bundle m v a -> Bundle m v b
map a -> b
f = (a -> m b) -> Bundle m v a -> Bundle m v b
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> m b) -> Bundle m v a -> Bundle m v b
mapM (b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> m b) -> (a -> b) -> a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)

-- | Map a monadic function over a 'Bundle'
mapM :: Monad m => (a -> m b) -> Bundle m v a -> Bundle m v b
{-# INLINE_FUSED mapM #-}
mapM :: (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} = Stream m b -> Size -> Bundle m v b
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> m b) -> Stream m a -> Stream m b
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_ :: (a -> m b) -> Bundle m v a -> m ()
mapM_ a -> m b
m = (a -> m b) -> Stream m a -> m ()
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> m ()
S.mapM_ a -> m b
m (Stream m a -> m ())
-> (Bundle m v a -> Stream m a) -> Bundle m v a -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v a -> Stream m a
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 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 :: forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle { sElems :: Stream m' a
sElems = (forall z. m z -> m' z) -> Stream m a -> Stream m' a
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 z. m z -> m' z)
-> Stream m (Chunk v a) -> Stream m' (Chunk v a)
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 :: 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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Stream m (Box a) -> Stream m a
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 :: 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} = Stream m (Int, a) -> Size -> Bundle m v (Int, a)
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Stream m a -> Stream m (Int, a)
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 :: 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} = Stream m (Int, a) -> Size -> Bundle m v (Int, a)
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Int -> Stream m a -> Stream m (Int, a)
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 :: (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} = Stream m c -> Size -> Bundle m v c
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Stream m a -> Stream m b -> Stream m c
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_ :: (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 = (a -> b -> m c) -> Stream m a -> Stream m b -> m ()
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 (Bundle m v a -> Stream m a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
sa) (Bundle m v b -> Stream m b
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 :: (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}
  = Stream m d -> Size -> Bundle m v d
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> b -> c -> m d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
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 :: (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
  = ((a, b) -> (c, d) -> m e)
-> Bundle m v (a, b) -> Bundle m v (c, d) -> Bundle m v e
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) (Bundle m v a -> Bundle m v b -> Bundle m v (a, b)
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) (Bundle m v c -> Bundle m v d -> Bundle m v (c, 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 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 :: (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
  = ((a, b, c) -> (d, e) -> m f)
-> Bundle m v (a, b, c) -> Bundle m v (d, e) -> Bundle m v 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,c
c) (d
d,e
e) -> a -> b -> c -> d -> e -> m f
f a
a b
b c
c d
d e
e) (Bundle m v a
-> Bundle m v b -> Bundle m v c -> Bundle m v (a, b, c)
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) (Bundle m v d -> Bundle m v e -> Bundle m v (d, e)
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 :: (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
  = ((a, b, c) -> (d, e, f) -> m g)
-> Bundle m v (a, b, c) -> Bundle m v (d, e, f) -> Bundle m v g
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) (Bundle m v a
-> Bundle m v b -> Bundle m v c -> Bundle m v (a, b, c)
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)
                                                  (Bundle m v d
-> Bundle m v e -> Bundle m v f -> Bundle m v (d, e, 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 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 :: (a -> b -> c) -> Bundle m v a -> Bundle m v b -> Bundle m v c
zipWith a -> b -> c
f = (a -> b -> m c) -> Bundle m v a -> Bundle m v b -> Bundle m v c
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 -> m c
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 :: (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 = (a -> b -> c -> m d)
-> Bundle m v a -> Bundle m v b -> Bundle m v c -> Bundle m v d
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 -> d -> m d
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 :: (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 = (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
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 -> e -> m e
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 :: (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 = (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
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 -> f -> m f
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 :: (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 = (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
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 -> g -> m g
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 :: Bundle m v a -> Bundle m v b -> Bundle m v (a, b)
zip = (a -> b -> (a, b))
-> Bundle m v a -> Bundle m v b -> Bundle m v (a, b)
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 :: Bundle m v a
-> Bundle m v b -> Bundle m v c -> Bundle m v (a, b, c)
zip3 = (a -> b -> c -> (a, b, c))
-> Bundle m v a
-> Bundle m v b
-> Bundle m v c
-> Bundle m v (a, b, c)
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 :: Bundle m v a
-> Bundle m v b
-> Bundle m v c
-> Bundle m v d
-> Bundle m v (a, b, c, d)
zip4 = (a -> b -> c -> d -> (a, b, c, d))
-> Bundle m v a
-> Bundle m v b
-> Bundle m v c
-> Bundle m v d
-> Bundle m v (a, b, c, d)
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 :: 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 = (a -> b -> c -> d -> e -> (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
-> Bundle m v (a, b, c, d, e)
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 :: 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 = (a -> b -> c -> d -> e -> f -> (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
-> Bundle m v (a, b, c, d, e, f)
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 :: (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 (Bundle m v a -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle m v a
x) (Bundle m v b -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
sSize Bundle m v b
y) = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
  | Bool
otherwise                             = (a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
S.eqBy a -> b -> Bool
eq (Bundle m v a -> Stream m a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
x) (Bundle m v b -> Stream m b
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 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
b
    sizesAreDifferent (Exact Int
a) (Max Int
b)   = Int
a Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
b
    sizesAreDifferent (Max Int
a)   (Exact Int
b) = Int
a Int -> Int -> Bool
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 :: (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 = (a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
S.cmpBy a -> b -> Ordering
cmp (Bundle m v a -> Stream m a
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems Bundle m v a
x) (Bundle m v b -> Stream m b
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 :: (a -> Bool) -> Bundle m v a -> Bundle m v a
filter a -> Bool
f = (a -> m Bool) -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> m Bool) -> Bundle m v a -> Bundle m v a
filterM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

-- | 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 :: (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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> m Bool) -> Stream m a -> Stream m a
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 :: (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} = Stream m b -> Size -> Bundle m v b
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> m (Maybe b)) -> Stream m a -> Stream m b
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 :: (a -> Bool) -> Bundle m v a -> Bundle m v a
takeWhile a -> Bool
f = (a -> m Bool) -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> m Bool) -> Bundle m v a -> Bundle m v a
takeWhileM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

-- | Longest prefix of elements that satisfy the monadic predicate
takeWhileM :: Monad m => (a -> m Bool) -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED takeWhileM #-}
takeWhileM :: (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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> m Bool) -> Stream m a -> Stream m a
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 :: (a -> Bool) -> Bundle m v a -> Bundle m v a
dropWhile a -> Bool
f = (a -> m Bool) -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> m Bool) -> Bundle m v a -> Bundle m v a
dropWhileM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

-- | 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 :: (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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> m Bool) -> Stream m a -> Stream m a
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 :: a -> Bundle m v a -> m Bool
elem a
x = a -> Stream m a -> m Bool
forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
S.elem a
x (Stream m a -> m Bool)
-> (Bundle m v a -> Stream m a) -> Bundle m v a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v a -> Stream m a
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 :: a -> Bundle m v a -> m Bool
notElem a
x = a -> Stream m a -> m Bool
forall (m :: * -> *) a.
(Monad m, Eq a) =>
a -> Stream m a -> m Bool
S.notElem a
x (Stream m a -> m Bool)
-> (Bundle m v a -> Stream m a) -> Bundle m v a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v a -> Stream m a
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 :: (a -> Bool) -> Bundle m v a -> m (Maybe a)
find a -> Bool
f = (a -> m Bool) -> Bundle m v a -> m (Maybe a)
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> m Bool) -> Bundle m v a -> m (Maybe a)
findM (Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f)

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

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

-- | Left fold with a monadic operator
foldlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> m a
{-# INLINE_FUSED foldlM #-}
foldlM :: (a -> b -> m a) -> a -> Bundle m v b -> m a
foldlM a -> b -> m a
m a
z = (a -> b -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
S.foldlM a -> b -> m a
m a
z (Stream m b -> m a)
-> (Bundle m v b -> Stream m b) -> Bundle m v b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v b -> Stream m b
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 :: (a -> b -> m a) -> a -> Bundle m v b -> m a
foldM = (a -> b -> m a) -> a -> Bundle m v b -> m a
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 :: (a -> a -> a) -> Bundle m v a -> m a
foldl1 a -> a -> a
f = (a -> a -> m a) -> Bundle m v a -> m a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> m a
foldl1M (\a
a a
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
a a
b))

-- | Left fold over a non-empty 'Bundle' with a monadic operator
foldl1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
{-# INLINE_FUSED foldl1M #-}
foldl1M :: (a -> a -> m a) -> Bundle m v a -> m a
foldl1M a -> a -> m a
f = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
S.foldl1M a -> a -> m a
f (Stream m a -> m a)
-> (Bundle m v a -> Stream m a) -> Bundle m v a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v a -> Stream m a
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 :: (a -> a -> m a) -> Bundle m v a -> m a
fold1M = (a -> a -> m a) -> Bundle m v a -> m a
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' :: (a -> b -> a) -> a -> Bundle m v b -> m a
foldl' a -> b -> a
f = (a -> b -> m a) -> a -> Bundle m v b -> m a
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m a) -> a -> Bundle m v b -> m a
foldlM' (\a
a b
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Left fold with a strict accumulator and a monadic operator
foldlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> m a
{-# INLINE_FUSED foldlM' #-}
foldlM' :: (a -> b -> m a) -> a -> Bundle m v b -> m a
foldlM' a -> b -> m a
m a
z = (a -> b -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> m a
S.foldlM' a -> b -> m a
m a
z (Stream m b -> m a)
-> (Bundle m v b -> Stream m b) -> Bundle m v b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v b -> Stream m b
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' :: (a -> b -> m a) -> a -> Bundle m v b -> m a
foldM' = (a -> b -> m a) -> a -> Bundle m v b -> m a
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' :: (a -> a -> a) -> Bundle m v a -> m a
foldl1' a -> a -> a
f = (a -> a -> m a) -> Bundle m v a -> m a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> m a
foldl1M' (\a
a a
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
a a
b))

-- | Left fold over a non-empty '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' :: (a -> a -> m a) -> Bundle m v a -> m a
foldl1M' a -> a -> m a
f = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
S.foldl1M' a -> a -> m a
f (Stream m a -> m a)
-> (Bundle m v a -> Stream m a) -> Bundle m v a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v a -> Stream m a
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' :: (a -> a -> m a) -> Bundle m v a -> m a
fold1M' = (a -> a -> m a) -> Bundle m v a -> m a
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 :: (a -> b -> b) -> b -> Bundle m v a -> m b
foldr a -> b -> b
f = (a -> b -> m b) -> b -> Bundle m v a -> m b
forall (m :: * -> *) a b (v :: * -> *).
Monad m =>
(a -> b -> m b) -> b -> Bundle m v a -> m b
foldrM (\a
a b
b -> b -> m 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 :: (a -> b -> m b) -> b -> Bundle m v a -> m b
foldrM a -> b -> m b
f b
z = (a -> b -> m b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Stream m a -> m b
S.foldrM a -> b -> m b
f b
z (Stream m a -> m b)
-> (Bundle m v a -> Stream m a) -> Bundle m v a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v a -> Stream m a
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 :: (a -> a -> a) -> Bundle m v a -> m a
foldr1 a -> a -> a
f = (a -> a -> m a) -> Bundle m v a -> m a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> m a
foldr1M (\a
a a
b -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
a a
b))

-- | Right fold over a non-empty stream with a monadic operator
foldr1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> m a
{-# INLINE_FUSED foldr1M #-}
foldr1M :: (a -> a -> m a) -> Bundle m v a -> m a
foldr1M a -> a -> m a
f = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
(HasCallStack, Monad m) =>
(a -> a -> m a) -> Stream m a -> m a
S.foldr1M a -> a -> m a
f (Stream m a -> m a)
-> (Bundle m v a -> Stream m a) -> Bundle m v a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v a -> Stream m a
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 :: Bundle m v Bool -> m Bool
and = Stream m Bool -> m Bool
forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
S.and (Stream m Bool -> m Bool)
-> (Bundle m v Bool -> Stream m Bool) -> Bundle m v Bool -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v Bool -> Stream m Bool
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 :: Bundle m v Bool -> m Bool
or = Stream m Bool -> m Bool
forall (m :: * -> *). Monad m => Stream m Bool -> m Bool
S.or (Stream m Bool -> m Bool)
-> (Bundle m v Bool -> Stream m Bool) -> Bundle m v Bool -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle m v Bool -> Stream m Bool
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 :: (a -> Bundle m v b) -> Bundle m v a -> Bundle m v b
concatMap a -> Bundle m v b
f = (a -> m (Bundle m v b)) -> Bundle m v a -> Bundle m v b
forall (m :: * -> *) a (v :: * -> *) b.
Monad m =>
(a -> m (Bundle m v b)) -> Bundle m v a -> Bundle m v b
concatMapM (Bundle m v b -> m (Bundle m v b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Bundle m v b -> m (Bundle m v b))
-> (a -> Bundle m v b) -> a -> m (Bundle m v b)
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 :: (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} = Stream m b -> Size -> Bundle m v b
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> m (Stream m b)) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> m (Stream m b)) -> Stream m a -> Stream m b
S.concatMapM ((Bundle m v b -> Stream m b) -> m (Bundle m v b) -> m (Stream m b)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Bundle m v b -> Stream m b
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
sElems (m (Bundle m v b) -> m (Stream m b))
-> (a -> m (Bundle m v b)) -> a -> m (Stream m b)
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 :: (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} = Stream m b -> Size -> Bundle m v b
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> m s) -> (s -> m (Step s b)) -> Stream m a -> Stream m b
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 :: (s -> Maybe (a, s)) -> s -> Bundle m u a
unfoldr s -> Maybe (a, s)
f = (s -> m (Maybe (a, s))) -> s -> Bundle m u a
forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Bundle m u a
unfoldrM (Maybe (a, s) -> m (Maybe (a, s))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, s) -> m (Maybe (a, s)))
-> (s -> Maybe (a, s)) -> s -> m (Maybe (a, s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
f)

-- | Unfold with a monadic function
unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Bundle m u a
{-# INLINE_FUSED unfoldrM #-}
unfoldrM :: (s -> m (Maybe (a, s))) -> s -> Bundle m u a
unfoldrM s -> m (Maybe (a, s))
f s
s = Stream m a -> Size -> Bundle m u a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((s -> m (Maybe (a, s))) -> s -> Stream m a
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 :: Int -> (s -> Maybe (a, s)) -> s -> Bundle m u a
unfoldrN Int
n s -> Maybe (a, s)
f = Int -> (s -> m (Maybe (a, s))) -> s -> Bundle m u a
forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Bundle m u a
unfoldrNM Int
n (Maybe (a, s) -> m (Maybe (a, s))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, s) -> m (Maybe (a, s)))
-> (s -> Maybe (a, s)) -> s -> m (Maybe (a, s))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
f)

-- | Unfold at most @n@ elements with a monadic function.
unfoldrNM :: Monad m => Int -> (s -> m (Maybe (a, s))) -> s -> Bundle m u a
{-# INLINE_FUSED unfoldrNM #-}
unfoldrNM :: Int -> (s -> m (Maybe (a, s))) -> s -> Bundle m u a
unfoldrNM Int
n s -> m (Maybe (a, s))
f s
s = Stream m a -> Size -> Bundle m u a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
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 :: Int -> (s -> (a, s)) -> s -> Bundle m u a
unfoldrExactN Int
n s -> (a, s)
f = Int -> (s -> m (a, s)) -> s -> Bundle m u a
forall (m :: * -> *) s a (u :: * -> *).
Monad m =>
Int -> (s -> m (a, s)) -> s -> Bundle m u a
unfoldrExactNM Int
n ((a, s) -> m (a, s)
forall (m :: * -> *) a. Monad m => a -> m a
return ((a, s) -> m (a, s)) -> (s -> (a, s)) -> s -> m (a, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> (a, s)
f)

-- | Unfold exactly @n@ elements with a monadic function.
--
-- @since 0.12.2.0
unfoldrExactNM :: Monad m => Int -> (s -> m (a, s)) -> s -> Bundle m u a
{-# INLINE_FUSED unfoldrExactNM #-}
unfoldrExactNM :: Int -> (s -> m (a, s)) -> s -> Bundle m u a
unfoldrExactNM Int
n s -> m (a, s)
f s
s = Stream m a -> Size -> Bundle m u a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Int -> (s -> m (a, s)) -> s -> Stream m a
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 ((Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
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 :: Int -> (a -> m a) -> a -> Bundle m u a
iterateNM Int
n a -> m a
f a
x0 = Stream m a -> Size -> Bundle m u a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Int -> (a -> m a) -> a -> Stream m a
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 ((Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
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 :: Int -> (a -> a) -> a -> Bundle m u a
iterateN Int
n a -> a
f a
x0 = Int -> (a -> m a) -> a -> Bundle m u a
forall (m :: * -> *) a (u :: * -> *).
Monad m =>
Int -> (a -> m a) -> a -> Bundle m u a
iterateNM Int
n (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f) a
x0

-- Scans
-- -----

-- | Prefix scan
prescanl :: Monad m => (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE prescanl #-}
prescanl :: (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
prescanl a -> b -> a
f = (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
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 -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Prefix scan with a monadic operator
prescanlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE_FUSED prescanlM #-}
prescanlM :: (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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
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' :: (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
prescanl' a -> b -> a
f = (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
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 -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Prefix scan with strict accumulator and a monadic operator
prescanlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE_FUSED prescanlM' #-}
prescanlM' :: (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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
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 :: (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
postscanl a -> b -> a
f = (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
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 -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Suffix scan with a monadic operator
postscanlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE_FUSED postscanlM #-}
postscanlM :: (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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
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' :: (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
postscanl' a -> b -> a
f = (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
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 -> a -> m a
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' :: (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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> b -> m a) -> a -> Stream m b -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Stream m b -> Stream m a
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 :: (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
scanl a -> b -> a
f = (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
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 -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Haskell-style scan with a monadic operator
scanlM :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE scanlM #-}
scanlM :: (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 a -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
a -> Bundle m v a -> Bundle m v a
`cons` (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
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' :: (a -> b -> a) -> a -> Bundle m v b -> Bundle m v a
scanl' a -> b -> a
f = (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
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 -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> a
f a
a b
b))

-- | Haskell-style scan with strict accumulator and a monadic operator
scanlM' :: Monad m => (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
{-# INLINE scanlM' #-}
scanlM' :: (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 a -> Bundle m v a -> Bundle m v a
`seq` (a
z a -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
a -> Bundle m v a -> Bundle m v a
`cons` (a -> b -> m a) -> a -> Bundle m v b -> Bundle m v a
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 :: (a -> a -> a) -> Bundle m v a -> Bundle m v a
scanl1 a -> a -> a
f = (a -> a -> m a) -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> Bundle m v a
scanl1M (\a
x a
y -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-- | Initial-value free scan over a 'Bundle' with a monadic operator
scanl1M :: Monad m => (a -> a -> m a) -> Bundle m v a -> Bundle m v a
{-# INLINE_FUSED scanl1M #-}
scanl1M :: (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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
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' :: (a -> a -> a) -> Bundle m v a -> Bundle m v a
scanl1' a -> a -> a
f = (a -> a -> m a) -> Bundle m v a -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
(a -> a -> m a) -> Bundle m v a -> Bundle m v a
scanl1M' (\a
x a
y -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> a -> a
f a
x a
y))

-- | Initial-value free scan over a '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' :: (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} = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> a -> m a) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> Stream m a
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 :: a -> a -> Int -> Bundle m v a
enumFromStepN a
x a
y Int
n = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (a -> a -> Int -> Stream m a
forall a (m :: * -> *).
(Num a, Monad m) =>
a -> a -> Int -> Stream m a
S.enumFromStepN a
x a
y Int
n) (Int -> Size
Exact ((Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
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 :: a -> a -> Bundle m v a
enumFromTo a
x a
y = [a] -> Bundle m v a
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 :: a -> a -> Bundle m v a
enumFromTo_small a
x a
y = a
x a -> Bundle m v a -> Bundle m v a
`seq` a
y a -> Bundle m v a -> Bundle m v a
`seq` Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((Maybe a -> m (Step (Maybe a) a)) -> Maybe a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Maybe a -> m (Step (Maybe a) a)
step (a -> Maybe a
forall a. a -> Maybe a
Just a
x)) (Int -> Size
Exact Int
n)
  where
    n :: Int
n = (Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
y Int -> Int -> Int
forall a. Num a => a -> a -> a
- a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
x Int -> Int -> Int
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              = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done
    step (Just a
z) | a
z a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z Maybe a
forall a. Maybe a
Nothing
                  | a
z a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<  a
y    = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a -> Step (Maybe a) a
forall a s. a -> s -> Step s a
Yield a
z (a -> Maybe a
forall a. a -> Maybe a
Just (a
za -> a -> a
forall a. Num a => a -> a -> a
+a
1))
                  | Bool
otherwise = Step (Maybe a) a -> m (Step (Maybe a) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe a) a -> m (Step (Maybe a) a))
-> Step (Maybe a) a -> m (Step (Maybe a) a)
forall a b. (a -> b) -> a -> b
$ Step (Maybe a) a
forall s a. Step s a
Done

{-# RULES

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

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

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

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

{-# RULES

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

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

{-# RULES

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

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


{-# RULES

"enumFromTo<Int64> [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 :: Char -> Char -> Bundle m v Char
enumFromTo_char Char
x Char
y = Char
x Char -> Bundle m v Char -> Bundle m v Char
`seq` Char
y Char -> Bundle m v Char -> Bundle m v Char
`seq` Stream m Char -> Size -> Bundle m v Char
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((Int -> m (Step Int Char)) -> Int -> Stream m Char
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Int -> m (Step Int Char)
step Int
xn) (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 = (Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
yn Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
xn Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)

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

{-# RULES

"enumFromTo<Char> [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 :: a -> a -> Bundle m v a
enumFromTo_double a
n a
m = a
n a -> Bundle m v a -> Bundle m v a
`seq` a
m a -> Bundle m v a -> Bundle m v a
`seq` Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ((a -> m (Step a a)) -> a -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream a -> m (Step a a)
step a
ini) (Int -> Size
Max (a -> a -> Int
HasCallStack => a -> a -> Int
len a
n a
lim))
  where
    lim :: a
lim = a
m a -> a -> a
forall a. Num a => a -> a -> a
+ a
1a -> a -> a
forall a. Fractional a => a -> a -> a
/a
2 -- important to float out

    {-# INLINE [0] len #-}
    len :: HasCallStack => a -> a -> Int
    len :: a -> a -> Int
len a
x a
y | a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
y     = Int
0
            | Bool
otherwise = Checks -> String -> Bool -> Int -> Int
forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check Checks
Bounds String
"vector too large" (Integer
l Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0) (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
l
      where
        l :: Integer
        l :: Integer
l = a -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
truncate (a
ya -> a -> a
forall a. Num a => a -> a -> a
-a
x)Integer -> Integer -> Integer
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' a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
lim = Step a a -> m (Step a a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a -> m (Step a a)) -> Step a a -> m (Step a a)
forall a b. (a -> b) -> a -> b
$ a -> a -> Step a a
forall a s. a -> s -> Step s a
Yield a
x' (a
xa -> a -> a
forall a. Num a => a -> a -> a
+a
1)
           | Bool
otherwise = Step a a -> m (Step a a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step a a -> m (Step a a)) -> Step a a -> m (Step a a)
forall a b. (a -> b) -> a -> b
$ Step a a
forall s a. Step s a
Done
           where
             x' :: a
x' = a
x a -> a -> a
forall a. Num a => a -> a -> a
+ a
n
#else
    ini = n
    step x | x <= lim  = return $ Yield x (x+1)
           | otherwise = return $ Done
#endif

{-# RULES

"enumFromTo<Double> [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 :: a -> a -> a -> Bundle m v a
enumFromThenTo a
x a
y a
z = [a] -> Bundle m v a
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 :: Bundle m v a -> m [a]
toList = (a -> [a] -> [a]) -> [a] -> Bundle m v a -> m [a]
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 :: [a] -> Bundle m v a
fromList [a]
xs = Size -> [a] -> Bundle m v a
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 :: Int -> [a] -> Bundle m v a
fromListN Int
n [a]
xs = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream (Int -> [a] -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
S.fromListN Int
n [a]
xs) (Int -> Size
Max ((Int -> Int -> Int) -> Int -> Int -> Int
forall a b. (a -> b) -> a -> b
delay_inline Int -> Int -> Int
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 :: Size -> [a] -> Bundle m v a
unsafeFromList Size
sz [a]
xs = Stream m a -> Size -> Bundle m v a
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
fromStream ([a] -> Stream m a
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 :: v a -> Bundle m v a
fromVector v a
v = v a
v v a -> Bundle m v a -> Bundle m v a
`seq` Int
n Int -> Bundle m v a -> Bundle m v a
`seq` Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle ((Int -> m (Step Int a)) -> Int -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Int -> m (Step Int a)
step Int
0)
                                      ((Bool -> m (Step Bool (Chunk v a))) -> Bool -> Stream m (Chunk v a)
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Bool -> m (Step Bool (Chunk v a))
vstep Bool
True)
                                      (v a -> Maybe (v a)
forall a. a -> Maybe a
Just v a
v)
                                      (Int -> Size
Exact Int
n)
  where
    n :: Int
n = v a -> Int
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n = Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int a
forall s a. Step s a
Done
           | Bool
otherwise = case v a -> Int -> Box a
forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
basicUnsafeIndexM v a
v Int
i of
                           Box a
x -> Step Int a -> m (Step Int a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int a -> m (Step Int a)) -> Step Int a -> m (Step Int a)
forall a b. (a -> b) -> a -> b
$ a -> Int -> Step Int a
forall a s. a -> s -> Step s a
Yield a
x (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)


    {-# INLINE vstep #-}
    vstep :: Bool -> m (Step Bool (Chunk v a))
vstep Bool
True  = Step Bool (Chunk v a) -> m (Step Bool (Chunk v a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk v a -> Bool -> Step Bool (Chunk v a)
forall a s. a -> s -> Step s a
Yield (Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
forall (v :: * -> *) a.
Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
Chunk (v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v) (\Mutable v (PrimState m) a
mv -> ST (PrimState m) () -> m ()
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) () -> m ()) -> ST (PrimState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ Mutable v (PrimState m) a -> v a -> ST (PrimState m) ()
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 = Step Bool (Chunk v a) -> m (Step Bool (Chunk v a))
forall (m :: * -> *) a. Monad m => a -> m a
return Step Bool (Chunk v a)
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 :: [v a] -> Bundle m v a
fromVectors [v a]
us = Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle ((Either [v a] (v a, Int, [v a])
 -> m (Step (Either [v a] (v a, Int, [v a])) a))
-> Either [v a] (v a, Int, [v a]) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream Either [v a] (v a, Int, [v a])
-> m (Step (Either [v a] (v a, Int, [v a])) a)
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 ([v a] -> Either [v a] (v a, Int, [v a])
forall a b. a -> Either a b
Left [v a]
us))
                        (([v a] -> m (Step [v a] (Chunk v a)))
-> [v a] -> Stream m (Chunk v a)
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream HasCallStack => [v a] -> m (Step [v a] (Chunk v a))
[v a] -> m (Step [v a] (Chunk v a))
vstep [v a]
us)
                        Maybe (v a)
forall a. Maybe a
Nothing
                        (Int -> Size
Exact Int
n)
  where
    n :: Int
n = (Int -> v a -> Int) -> Int -> [v a] -> Int
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' (\Int
k v a
v -> Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ v a -> Int
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 []) = Step (Either [v a] (v a, Int, [v a])) a
-> m (Step (Either [v a] (v a, Int, [v a])) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Either [v a] (v a, Int, [v a])) a
forall s a. Step s a
Done
    pstep (Left (v a
v:[v a]
vs)) = v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v Int
-> m (Step (Either [v a] (v a, Int, [v a])) a)
-> m (Step (Either [v a] (v a, Int, [v a])) a)
`seq` Step (Either [v a] (v a, Int, [v a])) a
-> m (Step (Either [v a] (v a, Int, [v a])) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either [v a] (v a, Int, [v a])
-> Step (Either [v a] (v a, Int, [v a])) a
forall s a. s -> Step s a
Skip ((v a, Int, [v a]) -> Either [v a] (v a, Int, [v a])
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v = Step (Either [v a] (v a, Int, [v a])) a
-> m (Step (Either [v a] (v a, Int, [v a])) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either [v a] (v a, Int, [v a])) a
 -> m (Step (Either [v a] (v a, Int, [v a])) a))
-> Step (Either [v a] (v a, Int, [v a])) a
-> m (Step (Either [v a] (v a, Int, [v a])) a)
forall a b. (a -> b) -> a -> b
$ Either [v a] (v a, Int, [v a])
-> Step (Either [v a] (v a, Int, [v a])) a
forall s a. s -> Step s a
Skip ([v a] -> Either [v a] (v a, Int, [v a])
forall a b. a -> Either a b
Left [v a]
vs)
      | Bool
otherwise          = case v a -> Int -> Box a
forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
basicUnsafeIndexM v a
v Int
i of
                               Box a
x -> Step (Either [v a] (v a, Int, [v a])) a
-> m (Step (Either [v a] (v a, Int, [v a])) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Either [v a] (v a, Int, [v a])) a
 -> m (Step (Either [v a] (v a, Int, [v a])) a))
-> Step (Either [v a] (v a, Int, [v a])) a
-> m (Step (Either [v a] (v a, Int, [v a])) a)
forall a b. (a -> b) -> a -> b
$ a
-> Either [v a] (v a, Int, [v a])
-> Step (Either [v a] (v a, Int, [v a])) a
forall a s. a -> s -> Step s a
Yield a
x ((v a, Int, [v a]) -> Either [v a] (v a, Int, [v a])
forall a b. b -> Either a b
Right (v a
v,Int
iInt -> Int -> Int
forall 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 :: [v a] -> m (Step [v a] (Chunk v a))
vstep [] = Step [v a] (Chunk v a) -> m (Step [v a] (Chunk v a))
forall (m :: * -> *) a. Monad m => a -> m a
return Step [v a] (Chunk v a)
forall s a. Step s a
Done
    vstep (v a
v:[v a]
vs) = Step [v a] (Chunk v a) -> m (Step [v a] (Chunk v a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step [v a] (Chunk v a) -> m (Step [v a] (Chunk v a)))
-> Step [v a] (Chunk v a) -> m (Step [v a] (Chunk v a))
forall a b. (a -> b) -> a -> b
$ Chunk v a -> [v a] -> Step [v a] (Chunk v a)
forall a s. a -> s -> Step s a
Yield (Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
forall (v :: * -> *) a.
Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
Chunk (v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v)
                                         (\Mutable v (PrimState m) a
mv -> Checks -> String -> Bool -> m () -> m ()
forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check
                                                 Checks
Internal
                                                 String
"length mismatch"
                                                 (Mutable v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
M.basicLength Mutable v (PrimState m) a
mv Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v)
                                                 (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ ST (PrimState m) () -> m ()
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) () -> m ()) -> ST (PrimState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ Mutable v (PrimState m) a -> v a -> ST (PrimState m) ()
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 :: 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}
  = Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
forall (m :: * -> *) (v :: * -> *) a.
Stream m a
-> Stream m (Chunk v a) -> Maybe (v a) -> Size -> Bundle m v a
Bundle ((Either s (v a, Int, s) -> m (Step (Either s (v a, Int, s)) a))
-> Either s (v a, Int, s) -> Stream m a
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 (s -> Either s (v a, Int, s)
forall a b. a -> Either a b
Left s
t))
           ((s -> m (Step s (Chunk v a))) -> s -> Stream m (Chunk v a)
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
Stream s -> m (Step s (Chunk v a))
vstep s
t)
           Maybe (v a)
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' -> v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v Int
-> m (Step (Either s (v a, Int, s)) a)
-> m (Step (Either s (v a, Int, s)) a)
`seq` Step (Either s (v a, Int, s)) a
-> m (Step (Either s (v a, Int, s)) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either s (v a, Int, s) -> Step (Either s (v a, Int, s)) a
forall s a. s -> Step s a
Skip ((v a, Int, s) -> Either s (v a, Int, s)
forall a b. b -> Either a b
Right (v a
v,Int
0,s
s')))
        Skip    s
s' -> Step (Either s (v a, Int, s)) a
-> m (Step (Either s (v a, Int, s)) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either s (v a, Int, s) -> Step (Either s (v a, Int, s)) a
forall s a. s -> Step s a
Skip (s -> Either s (v a, Int, s)
forall a b. a -> Either a b
Left s
s'))
        Step s (v a)
Done       -> Step (Either s (v a, Int, s)) a
-> m (Step (Either s (v a, Int, s)) a)
forall (m :: * -> *) a. Monad m => a -> m a
return Step (Either s (v a, Int, s)) a
forall s a. Step s a
Done

    pstep (Right (v a
v,Int
i,s
s))
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v = Step (Either s (v a, Int, s)) a
-> m (Step (Either s (v a, Int, s)) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either s (v a, Int, s) -> Step (Either s (v a, Int, s)) a
forall s a. s -> Step s a
Skip (s -> Either s (v a, Int, s)
forall a b. a -> Either a b
Left s
s))
      | Bool
otherwise          = case v a -> Int -> Box a
forall (v :: * -> *) a. Vector v a => v a -> Int -> Box a
basicUnsafeIndexM v a
v Int
i of
                               Box a
x -> Step (Either s (v a, Int, s)) a
-> m (Step (Either s (v a, Int, s)) a)
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Either s (v a, Int, s) -> Step (Either s (v a, Int, s)) a
forall a s. a -> s -> Step s a
Yield a
x ((v a, Int, s) -> Either s (v a, Int, s)
forall a b. b -> Either a b
Right (v a
v,Int
iInt -> Int -> Int
forall 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' -> Step s (Chunk v a) -> m (Step s (Chunk v a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Chunk v a -> s -> Step s (Chunk v a)
forall a s. a -> s -> Step s a
Yield (Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
forall (v :: * -> *) a.
Int
-> (forall (m :: * -> *).
    (PrimMonad m, Vector v a) =>
    Mutable v (PrimState m) a -> m ())
-> Chunk v a
Chunk (v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v)
                                           (\Mutable v (PrimState m) a
mv -> Checks -> String -> Bool -> m () -> m ()
forall a. HasCallStack => Checks -> String -> Bool -> a -> a
check
                                                   Checks
Internal
                                                   String
"length mismatch"
                                                   (Mutable v (PrimState m) a -> Int
forall (v :: * -> * -> *) a s. MVector v a => v s a -> Int
M.basicLength Mutable v (PrimState m) a
mv Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
basicLength v a
v)
                                                   (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ ST (PrimState m) () -> m ()
forall (m :: * -> *) a. PrimMonad m => ST (PrimState m) a -> m a
stToPrim (ST (PrimState m) () -> m ()) -> ST (PrimState m) () -> m ()
forall a b. (a -> b) -> a -> b
$ Mutable v (PrimState m) a -> v a -> ST (PrimState m) ()
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' -> Step s (Chunk v a) -> m (Step s (Chunk v a))
forall (m :: * -> *) a. Monad m => a -> m a
return (s -> Step s (Chunk v a)
forall s a. s -> Step s a
Skip s
s')
        Step s (v a)
Done       -> Step s (Chunk v a) -> m (Step s (Chunk v a))
forall (m :: * -> *) a. Monad m => a -> m a
return Step s (Chunk v a)
forall s a. Step s a
Done

reVector :: Monad m => Bundle m u a -> Bundle m v a
{-# INLINE_FUSED reVector #-}
reVector :: 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} = Stream m a -> Size -> Bundle m v a
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   #-}