{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_HADDOCK hide, not-home #-}
-- |
-- Module      : Data.Massiv.Vector.Stream
-- Copyright   : (c) Alexey Kuleshevich 2019-2021
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Massiv.Vector.Stream
  ( -- | This module has a similar purpose as the 'Data.Vector.Fusion.Bundle.Monadic', but
    -- quite a bit simpler.
    --
    -- __Important__ - This module is still experimental, as such it is considered
    -- internal and exported for the curious users only.
    Steps(..)
  , Stream(..)
  -- * Conversion
  , steps
  , isteps
  , consume
  , fromStream
  , fromStreamM
  , fromStreamExactM
  , unstreamExact
  , unstreamMax
  , unstreamMaxM
  , unstreamUnknown
  , unstreamUnknownM
  , unstreamIntoM
  -- * Bundle
  , toBundle
  , fromBundle
  , fromBundleM
  -- * Operations on Steps
  , length
  , null
  , empty
  , singleton
  , generate
  , headMaybe
  , last
  , cons
  , uncons
  , snoc
  , drop
  , take
  , slice
  , iterateN
  , iterateNM
  , replicate
  , replicateM
  , generateM
  , traverse
  , map
  , mapM
  , mapM_
  , indexed
  , concatMap
  , append
  , zipWith
  , zipWith3
  , zipWith4
  , zipWith5
  , zipWith6
  , zipWithM
  , zipWith3M
  , zipWith4M
  , zipWith5M
  , zipWith6M
  , zipWithM_
  , zipWith3M_
  , zipWith4M_
  , zipWith5M_
  , zipWith6M_
  -- ** Folding
  , foldl
  , foldl1
  , foldlM
  , foldl1M
  , foldlLazy
  , foldl1Lazy
  , foldlLazyM
  , foldl1LazyM
  , foldrLazy
  , foldr1Lazy
  , foldrLazyM
  , foldr1LazyM

  , or
  , and
  -- ** Unfolding
  , unfoldr
  , unfoldrN
  , unsafeUnfoldrN
  , unfoldrM
  , unfoldrNM
  , unsafeUnfoldrNM
  , unfoldrExactN
  , unfoldrExactNM
  -- ** Enumeration
  , enumFromStepN
  -- * Lists
  , toList
  , fromList
  , fromListN
  , unsafeFromListN
  -- ** Filter
  , mapMaybe
  , mapMaybeA
  , mapMaybeM
  , filter
  , filterA
  , filterM
  -- * Transformations
  , transSteps
  , transStepsId
  -- * Useful re-exports
  , module Data.Vector.Fusion.Util
  , Id(..)
  ) where

import qualified Control.Monad as M
import Control.Monad.ST
import qualified Data.Foldable as F
import Data.Massiv.Core.Common hiding (empty, singleton, replicate)
import Data.Coerce
import Data.Maybe (catMaybes)
import qualified Data.Traversable as Traversable (traverse)
import qualified Data.Vector.Fusion.Bundle.Monadic as B
import qualified Data.Vector.Fusion.Bundle.Size as B
import qualified Data.Vector.Fusion.Stream.Monadic as S
import Data.Vector.Fusion.Util
import Prelude hiding (and, concatMap, drop, filter, foldl, foldl1, foldr,
                foldr1, length, map, mapM, mapM_, null, or, replicate, take,
                traverse, zipWith, zipWith3)
import qualified GHC.Exts (IsList(..))

instance Monad m => Functor (Steps m) where
  fmap :: (a -> b) -> Steps m a -> Steps m b
fmap a -> b
f Steps m a
str = Steps m a
str {stepsStream :: Stream m b
stepsStream = (a -> b) -> Stream m a -> Stream m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
S.map a -> b
f (Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream Steps m a
str)}
  {-# INLINE fmap #-}
  <$ :: a -> Steps m b -> Steps m a
(<$) a
e Steps m b
str =
    case Steps m b -> LengthHint
forall (m :: * -> *) e. Steps m e -> LengthHint
stepsSize Steps m b
str of
      LengthExact Sz1
n -> Steps m b
str {stepsStream :: Stream m a
stepsStream = Int -> a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
S.replicate (Sz1 -> Int
coerce Sz1
n) a
e}
      LengthHint
_             -> (b -> a) -> Steps m b -> Steps m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a -> b -> a
forall a b. a -> b -> a
const a
e) Steps m b
str
  {-# INLINE (<$) #-}

instance Monad m => Semigroup (Steps m e) where
  <> :: Steps m e -> Steps m e -> Steps m e
(<>) = Steps m e -> Steps m e -> Steps m e
forall (m :: * -> *) e.
Monad m =>
Steps m e -> Steps m e -> Steps m e
append
  {-# INLINE (<>) #-}

instance Monad m => Monoid (Steps m e) where
  mempty :: Steps m e
mempty = Steps m e
forall (m :: * -> *) e. Monad m => Steps m e
empty
  {-# INLINE mempty #-}
  mappend :: Steps m e -> Steps m e -> Steps m e
mappend = Steps m e -> Steps m e -> Steps m e
forall (m :: * -> *) e.
Monad m =>
Steps m e -> Steps m e -> Steps m e
append
  {-# INLINE mappend #-}


instance GHC.Exts.IsList (Steps Id e) where
  type Item (Steps Id e) = e
  toList :: Steps Id e -> [Item (Steps Id e)]
toList = Steps Id e -> [Item (Steps Id e)]
forall e. Steps Id e -> [e]
toList
  {-# INLINE toList #-}
  fromList :: [Item (Steps Id e)] -> Steps Id e
fromList = [Item (Steps Id e)] -> Steps Id e
forall (m :: * -> *) e. Monad m => [e] -> Steps m e
fromList
  {-# INLINE fromList #-}
  fromListN :: Int -> [Item (Steps Id e)] -> Steps Id e
fromListN Int
n = (Stream Id e -> LengthHint -> Steps Id e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` Sz1 -> LengthHint
LengthMax (Int -> Sz1
forall ix. Index ix => ix -> Sz ix
Sz Int
n)) (Stream Id e -> Steps Id e)
-> ([e] -> Stream Id e) -> [e] -> Steps Id e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [e] -> Stream Id e
forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
S.fromListN Int
n
  {-# INLINE fromListN #-}

instance Foldable (Steps Id) where
  foldr :: (a -> b -> b) -> b -> Steps Id a -> b
foldr a -> b -> b
f b
acc = Id b -> b
forall a. Id a -> a
unId (Id b -> b) -> (Steps Id a -> Id b) -> Steps Id a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b -> b) -> b -> Steps Id a -> Id b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Steps m a -> m b
foldrLazy a -> b -> b
f b
acc
  {-# INLINE foldr #-}
  foldl :: (b -> a -> b) -> b -> Steps Id a -> b
foldl b -> a -> b
f b
acc = Id b -> b
forall a. Id a -> a
unId (Id b -> b) -> (Steps Id a -> Id b) -> Steps Id a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> a -> b) -> b -> Steps Id a -> Id b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldlLazy b -> a -> b
f b
acc
  {-# INLINE foldl #-}
  foldl' :: (b -> a -> b) -> b -> Steps Id a -> b
foldl' b -> a -> b
f b
acc = Id b -> b
forall a. Id a -> a
unId (Id b -> b) -> (Steps Id a -> Id b) -> Steps Id a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (b -> a -> b) -> b -> Steps Id a -> Id b
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldl b -> a -> b
f b
acc
  {-# INLINE foldl' #-}
  foldr1 :: (a -> a -> a) -> Steps Id a -> a
foldr1 a -> a -> a
f = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Steps Id a -> Id a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldr1Lazy a -> a -> a
f
  {-# INLINE foldr1 #-}
  foldl1 :: (a -> a -> a) -> Steps Id a -> a
foldl1 a -> a -> a
f = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Steps Id a -> Id a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1Lazy a -> a -> a
f
  {-# INLINE foldl1 #-}
  toList :: Steps Id a -> [a]
toList = Steps Id a -> [a]
forall e. Steps Id e -> [e]
toList
  {-# INLINE toList #-}
  length :: Steps Id a -> Int
length = Id Int -> Int
forall a. Id a -> a
unId (Id Int -> Int) -> (Steps Id a -> Id Int) -> Steps Id a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps Id a -> Id Int
forall (m :: * -> *) a. Monad m => Steps m a -> m Int
length
  {-# INLINE length #-}
  null :: Steps Id a -> Bool
null = Id Bool -> Bool
forall a. Id a -> a
unId (Id Bool -> Bool) -> (Steps Id a -> Id Bool) -> Steps Id a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps Id a -> Id Bool
forall (m :: * -> *) a. Monad m => Steps m a -> m Bool
null
  {-# INLINE null #-}
  sum :: Steps Id a -> a
sum = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> a -> Steps Id a -> Id a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldl a -> a -> a
forall a. Num a => a -> a -> a
(+) a
0
  {-# INLINE sum #-}
  product :: Steps Id a -> a
product = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> a -> Steps Id a -> Id a
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> b) -> b -> Steps m a -> m b
foldl a -> a -> a
forall a. Num a => a -> a -> a
(*) a
1
  {-# INLINE product #-}
  maximum :: Steps Id a -> a
maximum = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Steps Id a -> Id a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1 a -> a -> a
forall a. Ord a => a -> a -> a
max
  {-# INLINE maximum #-}
  minimum :: Steps Id a -> a
minimum = Id a -> a
forall a. Id a -> a
unId (Id a -> a) -> (Steps Id a -> Id a) -> Steps Id a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> Steps Id a -> Id a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Steps m a -> m a
foldl1 a -> a -> a
forall a. Ord a => a -> a -> a
min
  {-# INLINE minimum #-}


-- TODO: benchmark: `fmap snd . isteps`
steps :: forall r ix e m . (Monad m, Index ix, Source r e) => Array r ix e -> Steps m e
steps :: Array r ix e -> Steps m e
steps Array r ix e
arr = Int
k Int -> Steps m e -> Steps m e
`seq` Array r ix e
arr Array r ix e -> Steps m e -> Steps m e
`seq` Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((Int -> m (Step Int e)) -> Int -> Stream m e
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
S.Stream Int -> m (Step Int e)
step Int
0) (Sz1 -> LengthHint
LengthExact (Int -> Sz1
coerce Int
k))
  where
    k :: Int
k = Sz ix -> Int
forall ix. Index ix => Sz ix -> Int
totalElem (Sz ix -> Int) -> Sz ix -> Int
forall a b. (a -> b) -> a -> b
$ Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr
    step :: Int -> m (Step Int e)
step Int
i
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
k =
        let e :: e
e = Array r ix e -> Int -> e
forall r e ix. (Source r e, Index ix) => Array r ix e -> Int -> e
unsafeLinearIndex Array r ix e
arr Int
i
         in e
e e -> m (Step Int e) -> m (Step Int e)
`seq` Step Int e -> m (Step Int e)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int e -> m (Step Int e)) -> Step Int e -> m (Step Int e)
forall a b. (a -> b) -> a -> b
$ e -> Int -> Step Int e
forall a s. a -> s -> Step s a
S.Yield e
e (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      | Bool
otherwise = Step Int e -> m (Step Int e)
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int e
forall s a. Step s a
S.Done
    {-# INLINE step #-}
{-# INLINE steps #-}


isteps :: forall r ix e m . (Monad m, Index ix, Source r e) => Array r ix e -> Steps m (ix, e)
isteps :: Array r ix e -> Steps m (ix, e)
isteps Array r ix e
arr = Int
k Int -> Steps m (ix, e) -> Steps m (ix, e)
`seq` Array r ix e
arr Array r ix e -> Steps m (ix, e) -> Steps m (ix, e)
`seq` Stream m (ix, e) -> LengthHint -> Steps m (ix, e)
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((Int -> m (Step Int (ix, e))) -> Int -> Stream m (ix, e)
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
S.Stream Int -> m (Step Int (ix, e))
step Int
0) (Sz1 -> LengthHint
LengthExact (Int -> Sz1
coerce Int
k))
  where
    sz :: Sz ix
sz = Array r ix e -> Sz ix
forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr
    k :: Int
k = Sz ix -> Int
forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
sz
    step :: Int -> m (Step Int (ix, e))
step Int
i
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
k =
        let e :: e
e = Array r ix e -> Int -> e
forall r e ix. (Source r e, Index ix) => Array r ix e -> Int -> e
unsafeLinearIndex Array r ix e
arr Int
i
         in e
e e -> m (Step Int (ix, e)) -> m (Step Int (ix, e))
`seq` Step Int (ix, e) -> m (Step Int (ix, e))
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Int (ix, e) -> m (Step Int (ix, e)))
-> Step Int (ix, e) -> m (Step Int (ix, e))
forall a b. (a -> b) -> a -> b
$ (ix, e) -> Int -> Step Int (ix, e)
forall a s. a -> s -> Step s a
S.Yield (Sz ix -> Int -> ix
forall ix. Index ix => Sz ix -> Int -> ix
fromLinearIndex Sz ix
sz Int
i, e
e) (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      | Bool
otherwise = Step Int (ix, e) -> m (Step Int (ix, e))
forall (m :: * -> *) a. Monad m => a -> m a
return Step Int (ix, e)
forall s a. Step s a
S.Done
    {-# INLINE step #-}
{-# INLINE isteps #-}

toBundle :: (Monad m, Index ix, Source r e) => Array r ix e -> B.Bundle m v e
toBundle :: Array r ix e -> Bundle m v e
toBundle Array r ix e
arr =
  let Steps Stream m e
str LengthHint
k = Array r ix e -> Steps m e
forall r ix e (m :: * -> *).
(Monad m, Index ix, Source r e) =>
Array r ix e -> Steps m e
steps Array r ix e
arr
   in Stream m e -> Size -> Bundle m v e
forall (m :: * -> *) a (v :: * -> *).
Monad m =>
Stream m a -> Size -> Bundle m v a
B.fromStream Stream m e
str (LengthHint -> Size
sizeHintToBundleSize LengthHint
k)
{-# INLINE toBundle #-}

fromBundle :: Manifest r e => B.Bundle Id v e -> Vector r e
fromBundle :: Bundle Id v e -> Vector r e
fromBundle Bundle Id v e
bundle = Size -> Stream Id e -> Vector r e
forall r e. Manifest r e => Size -> Stream Id e -> Vector r e
fromStream (Bundle Id v e -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
B.sSize Bundle Id v e
bundle) (Bundle Id v e -> Stream Id e
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
B.sElems Bundle Id v e
bundle)
{-# INLINE fromBundle #-}


fromBundleM :: (Monad m, Manifest r e) => B.Bundle m v e -> m (Vector r e)
fromBundleM :: Bundle m v e -> m (Vector r e)
fromBundleM Bundle m v e
bundle = Size -> Stream m e -> m (Vector r e)
forall r e (m :: * -> *).
(Monad m, Manifest r e) =>
Size -> Stream m e -> m (Vector r e)
fromStreamM (Bundle m v e -> Size
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Size
B.sSize Bundle m v e
bundle) (Bundle m v e -> Stream m e
forall (m :: * -> *) (v :: * -> *) a. Bundle m v a -> Stream m a
B.sElems Bundle m v e
bundle)
{-# INLINE fromBundleM #-}


fromStream :: forall r e . Manifest r e => B.Size -> S.Stream Id e -> Vector r e
fromStream :: Size -> Stream Id e -> Vector r e
fromStream Size
sz Stream Id e
str =
  case Size -> Maybe Int
B.upperBound Size
sz of
    Maybe Int
Nothing -> Stream Id e -> Vector r e
forall r a. Manifest r a => Stream Id a -> Vector r a
unstreamUnknown Stream Id e
str
    Just Int
k  -> Int -> Stream Id e -> Vector r e
forall r e. Manifest r e => Int -> Stream Id e -> Vector r e
unstreamMax Int
k Stream Id e
str
{-# INLINE fromStream #-}

fromStreamM :: forall r e m. (Monad m, Manifest r e) => B.Size -> S.Stream m e -> m (Vector r e)
fromStreamM :: Size -> Stream m e -> m (Vector r e)
fromStreamM Size
sz Stream m e
str = do
  [e]
xs <- Stream m e -> m [e]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream m e
str
  case Size -> Maybe Int
B.upperBound Size
sz of
    Maybe Int
Nothing -> Vector r e -> m (Vector r e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vector r e -> m (Vector r e)) -> Vector r e -> m (Vector r e)
forall a b. (a -> b) -> a -> b
$! Stream Id e -> Vector r e
forall r a. Manifest r a => Stream Id a -> Vector r a
unstreamUnknown ([e] -> Stream Id e
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [e]
xs)
    Just Int
k  -> Vector r e -> m (Vector r e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vector r e -> m (Vector r e)) -> Vector r e -> m (Vector r e)
forall a b. (a -> b) -> a -> b
$! Int -> Stream Id e -> Vector r e
forall r e. Manifest r e => Int -> Stream Id e -> Vector r e
unstreamMax Int
k ([e] -> Stream Id e
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [e]
xs)
{-# INLINE fromStreamM #-}

fromStreamExactM ::
     forall r ix e m. (Monad m, Manifest r e, Index ix)
  => Sz ix
  -> S.Stream m e
  -> m (Array r ix e)
fromStreamExactM :: Sz ix -> Stream m e -> m (Array r ix e)
fromStreamExactM Sz ix
sz Stream m e
str = do
  [e]
xs <- Stream m e -> m [e]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream m e
str
  Array r ix e -> m (Array r ix e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Array r ix e -> m (Array r ix e))
-> Array r ix e -> m (Array r ix e)
forall a b. (a -> b) -> a -> b
$! Sz ix -> Stream Id e -> Array r ix e
forall r ix e.
(Manifest r e, Index ix) =>
Sz ix -> Stream Id e -> Array r ix e
unstreamExact Sz ix
sz ([e] -> Stream Id e
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [e]
xs)
{-# INLINE fromStreamExactM #-}


unstreamIntoM ::
     (Manifest r a, PrimMonad m)
  => MVector (PrimState m) r a
  -> LengthHint
  -> S.Stream Id a
  -> m (MVector (PrimState m) r a)
unstreamIntoM :: MVector (PrimState m) r a
-> LengthHint -> Stream Id a -> m (MVector (PrimState m) r a)
unstreamIntoM MVector (PrimState m) r a
marr LengthHint
sz Stream Id a
str =
  case LengthHint
sz of
    LengthExact Sz1
_ -> MVector (PrimState m) r a
marr MVector (PrimState m) r a -> m Int -> m (MVector (PrimState m) r a)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ MVector (PrimState m) r a -> Stream Id a -> m Int
forall r a ix (m :: * -> *).
(Manifest r a, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MVector (PrimState m) r a
marr Stream Id a
str
    LengthMax Sz1
_   -> MVector (PrimState m) r a -> Sz1 -> m (MVector (PrimState m) r a)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearShrink MVector (PrimState m) r a
marr (Sz1 -> m (MVector (PrimState m) r a))
-> (Int -> Sz1) -> Int -> m (MVector (PrimState m) r a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Sz1
forall ix. ix -> Sz ix
SafeSz (Int -> m (MVector (PrimState m) r a))
-> m Int -> m (MVector (PrimState m) r a)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< MVector (PrimState m) r a -> Stream Id a -> m Int
forall r a ix (m :: * -> *).
(Manifest r a, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MVector (PrimState m) r a
marr Stream Id a
str
    LengthHint
LengthUnknown -> MVector (PrimState m) r a
-> Stream Id a -> m (MVector (PrimState m) r a)
forall r a (m :: * -> *).
(Manifest r a, PrimMonad m) =>
MVector (PrimState m) r a
-> Stream Id a -> m (MVector (PrimState m) r a)
unstreamUnknownM MVector (PrimState m) r a
marr Stream Id a
str
{-# INLINE unstreamIntoM #-}



unstreamMax ::
     forall r e. (Manifest r e)
  => Int
  -> S.Stream Id e
  -> Vector r e
unstreamMax :: Int -> Stream Id e -> Vector r e
unstreamMax Int
kMax Stream Id e
str =
  (forall s. ST s (Vector r e)) -> Vector r e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Vector r e)) -> Vector r e)
-> (forall s. ST s (Vector r e)) -> Vector r e
forall a b. (a -> b) -> a -> b
$ do
    MArray s r Int e
marr <- Sz1 -> ST s (MArray (PrimState (ST s)) r Int e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew (Int -> Sz1
forall ix. ix -> Sz ix
SafeSz Int
kMax)
    Int
k <- MArray (PrimState (ST s)) r Int e -> Stream Id e -> ST s Int
forall r a ix (m :: * -> *).
(Manifest r a, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MArray s r Int e
MArray (PrimState (ST s)) r Int e
marr Stream Id e
str
    MArray (PrimState (ST s)) r Int e
-> Sz1 -> ST s (MArray (PrimState (ST s)) r Int e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearShrink MArray s r Int e
MArray (PrimState (ST s)) r Int e
marr (Int -> Sz1
forall ix. ix -> Sz ix
SafeSz Int
k) ST s (MArray s r Int e)
-> (MArray s r Int e -> ST s (Vector r e)) -> ST s (Vector r e)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Comp -> MArray (PrimState (ST s)) r Int e -> ST s (Vector r e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq
{-# INLINE unstreamMax #-}


unstreamMaxM ::
     (Manifest r a, Index ix, PrimMonad m) => MArray (PrimState m) r ix a -> S.Stream Id a -> m Int
unstreamMaxM :: MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MArray (PrimState m) r ix a
marr (S.Stream s -> Id (Step s a)
step s
s) = s -> Int -> m Int
stepLoad s
s Int
0
  where
    stepLoad :: s -> Int -> m Int
stepLoad s
t Int
i =
      case Id (Step s a) -> Step s a
forall a. Id a -> a
unId (s -> Id (Step s a)
step s
t) of
        S.Yield a
e' s
t' -> do
          MArray (PrimState m) r ix a -> Int -> a -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MArray (PrimState m) r ix a
marr Int
i a
e'
          s -> Int -> m Int
stepLoad s
t' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
        S.Skip s
t' -> s -> Int -> m Int
stepLoad s
t' Int
i
        Step s a
S.Done -> Int -> m Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
i
    {-# INLINE stepLoad #-}
{-# INLINE unstreamMaxM #-}


unstreamUnknown :: Manifest r a => S.Stream Id a -> Vector r a
unstreamUnknown :: Stream Id a -> Vector r a
unstreamUnknown Stream Id a
str =
  (forall s. ST s (Vector r a)) -> Vector r a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Vector r a)) -> Vector r a)
-> (forall s. ST s (Vector r a)) -> Vector r a
forall a b. (a -> b) -> a -> b
$ do
    MArray s r Int a
marr <- Sz1 -> ST s (MArray (PrimState (ST s)) r Int a)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz1
forall ix. Index ix => Sz ix
zeroSz
    MArray (PrimState (ST s)) r Int a
-> Stream Id a -> ST s (MArray (PrimState (ST s)) r Int a)
forall r a (m :: * -> *).
(Manifest r a, PrimMonad m) =>
MVector (PrimState m) r a
-> Stream Id a -> m (MVector (PrimState m) r a)
unstreamUnknownM MArray s r Int a
MArray (PrimState (ST s)) r Int a
marr Stream Id a
str ST s (MArray s r Int a)
-> (MArray s r Int a -> ST s (Vector r a)) -> ST s (Vector r a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Comp -> MArray (PrimState (ST s)) r Int a -> ST s (Vector r a)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq
{-# INLINE unstreamUnknown #-}


unstreamUnknownM ::
     (Manifest r a, PrimMonad m)
  => MVector (PrimState m) r a
  -> S.Stream Id a
  -> m (MVector (PrimState m) r a)
unstreamUnknownM :: MVector (PrimState m) r a
-> Stream Id a -> m (MVector (PrimState m) r a)
unstreamUnknownM MVector (PrimState m) r a
marrInit (S.Stream s -> Id (Step s a)
step s
s) = s
-> Int
-> Int
-> MVector (PrimState m) r a
-> m (MVector (PrimState m) r a)
stepLoad s
s Int
0 (Sz1 -> Int
forall ix. Sz ix -> ix
unSz (MVector (PrimState m) r a -> Sz1
forall r e ix s.
(Manifest r e, Index ix) =>
MArray s r ix e -> Sz ix
sizeOfMArray MVector (PrimState m) r a
marrInit)) MVector (PrimState m) r a
marrInit
  where
    stepLoad :: s
-> Int
-> Int
-> MVector (PrimState m) r a
-> m (MVector (PrimState m) r a)
stepLoad s
t Int
i Int
kMax MVector (PrimState m) r a
marr
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
kMax =
        case Id (Step s a) -> Step s a
forall a. Id a -> a
unId (s -> Id (Step s a)
step s
t) of
          S.Yield a
e' s
t' -> do
            MVector (PrimState m) r a -> Int -> a -> m ()
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e -> Int -> e -> m ()
unsafeLinearWrite MVector (PrimState m) r a
marr Int
i a
e'
            s
-> Int
-> Int
-> MVector (PrimState m) r a
-> m (MVector (PrimState m) r a)
stepLoad s
t' (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int
kMax MVector (PrimState m) r a
marr
          S.Skip s
t' -> s
-> Int
-> Int
-> MVector (PrimState m) r a
-> m (MVector (PrimState m) r a)
stepLoad s
t' Int
i Int
kMax MVector (PrimState m) r a
marr
          Step s a
S.Done -> MVector (PrimState m) r a -> Sz1 -> m (MVector (PrimState m) r a)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearShrink MVector (PrimState m) r a
marr (Int -> Sz1
forall ix. ix -> Sz ix
SafeSz Int
i)
      | Bool
otherwise = do
        let kMax' :: Int
kMax' = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
1 (Int
kMax Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)
        MVector (PrimState m) r a
marr' <- MVector (PrimState m) r a -> Sz1 -> m (MVector (PrimState m) r a)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix e
-> Sz ix -> m (MArray (PrimState m) r ix e)
unsafeLinearGrow MVector (PrimState m) r a
marr (Int -> Sz1
forall ix. ix -> Sz ix
SafeSz Int
kMax')
        s
-> Int
-> Int
-> MVector (PrimState m) r a
-> m (MVector (PrimState m) r a)
stepLoad s
t Int
i Int
kMax' MVector (PrimState m) r a
marr'
    {-# INLINE stepLoad #-}
{-# INLINE unstreamUnknownM #-}


unstreamExact ::
     forall r ix e. (Manifest r e, Index ix)
  => Sz ix
  -> S.Stream Id e
  -> Array r ix e
unstreamExact :: Sz ix -> Stream Id e -> Array r ix e
unstreamExact Sz ix
sz Stream Id e
str =
  (forall s. ST s (Array r ix e)) -> Array r ix e
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Array r ix e)) -> Array r ix e)
-> (forall s. ST s (Array r ix e)) -> Array r ix e
forall a b. (a -> b) -> a -> b
$ do
    MArray s r ix e
marr <- Sz ix -> ST s (MArray (PrimState (ST s)) r ix e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Sz ix -> m (MArray (PrimState m) r ix e)
unsafeNew Sz ix
sz
    Int
_ <- MArray (PrimState (ST s)) r ix e -> Stream Id e -> ST s Int
forall r a ix (m :: * -> *).
(Manifest r a, Index ix, PrimMonad m) =>
MArray (PrimState m) r ix a -> Stream Id a -> m Int
unstreamMaxM MArray s r ix e
MArray (PrimState (ST s)) r ix e
marr Stream Id e
str
    Comp -> MArray (PrimState (ST s)) r ix e -> ST s (Array r ix e)
forall r e ix (m :: * -> *).
(Manifest r e, Index ix, PrimMonad m) =>
Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
unsafeFreeze Comp
Seq MArray s r ix e
MArray (PrimState (ST s)) r ix e
marr
{-# INLINE unstreamExact #-}

length :: Monad m => Steps m a -> m Int
length :: Steps m a -> m Int
length (Steps Stream m a
str LengthHint
sz) =
  case LengthHint
sz of
    LengthExact Sz1
k -> Int -> m Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$ Sz1 -> Int
coerce Sz1
k
    LengthHint
_             -> Stream m a -> m Int
forall (m :: * -> *) a. Monad m => Stream m a -> m Int
S.length Stream m a
str
{-# INLINE length #-}


null :: Monad m => Steps m a -> m Bool
null :: Steps m a -> m Bool
null (Steps Stream m a
str LengthHint
sz) =
  case LengthHint
sz of
    LengthExact Sz1
k -> Bool -> m Bool
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Sz1
k Sz1 -> Sz1 -> Bool
forall a. Eq a => a -> a -> Bool
== Sz1
forall ix. Index ix => Sz ix
zeroSz)
    LengthHint
_         -> Stream m a -> m Bool
forall (m :: * -> *) a. Monad m => Stream m a -> m Bool
S.null Stream m a
str
{-# INLINE null #-}

empty :: Monad m => Steps m e
empty :: Steps m e
empty = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps Stream m e
forall (m :: * -> *) a. Monad m => Stream m a
S.empty (Sz1 -> LengthHint
LengthExact Sz1
forall ix. Index ix => Sz ix
zeroSz)
{-# INLINE empty #-}

singleton :: Monad m => e -> Steps m e
singleton :: e -> Steps m e
singleton e
e = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (e -> Stream m e
forall (m :: * -> *) a. Monad m => a -> Stream m a
S.singleton e
e) (Sz1 -> LengthHint
LengthExact Sz1
forall ix. Index ix => Sz ix
oneSz)
{-# INLINE singleton #-}

generate :: Monad m => Sz1 -> (Int -> e) -> Steps m e
generate :: Sz1 -> (Int -> e) -> Steps m e
generate Sz1
k Int -> e
f = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (Int -> e) -> Stream m e
forall (m :: * -> *) a. Monad m => Int -> (Int -> a) -> Stream m a
S.generate (Sz1 -> Int
coerce Sz1
k) Int -> e
f) (Sz1 -> LengthHint
LengthExact Sz1
k)
{-# INLINE generate #-}

-- | First element of the 'Stream' or error if empty
headMaybe :: Monad m => Steps m a -> m (Maybe a)
headMaybe :: Steps m a -> m (Maybe a)
headMaybe (Steps (S.Stream s -> m (Step s a)
step s
t) LengthHint
_) = SPEC -> s -> m (Maybe a)
headMaybeLoop SPEC
S.SPEC s
t
  where
    headMaybeLoop :: SPEC -> s -> m (Maybe a)
headMaybeLoop !SPEC
_ s
s = do
      Step s a
r <- s -> m (Step s a)
step s
s
      case Step s a
r of
        S.Yield a
x s
_ -> Maybe a -> m (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> m (Maybe a)) -> Maybe a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
x
        S.Skip s
s'   -> SPEC -> s -> m (Maybe a)
headMaybeLoop SPEC
S.SPEC s
s'
        Step s a
S.Done      -> Maybe a -> m (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
    {-# INLINE [0] headMaybeLoop #-}
{-# INLINE headMaybe #-}


cons :: Monad m => e -> Steps m e -> Steps m e
cons :: e -> Steps m e -> Steps m e
cons e
e (Steps Stream m e
str LengthHint
k) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (e -> Stream m e -> Stream m e
forall (m :: * -> *) a. Monad m => a -> Stream m a -> Stream m a
S.cons e
e Stream m e
str) (LengthHint
k LengthHint -> Int -> LengthHint
`addInt` Int
1)
{-# INLINE cons #-}

-- | First element of the `Steps` or `Nothing` if empty
uncons :: Monad m => Steps m e -> m (Maybe (e, Steps m e))
uncons :: Steps m e -> m (Maybe (e, Steps m e))
uncons Steps m e
sts = (\Maybe e
mx -> (, Sz1 -> Steps m e -> Steps m e
forall (m :: * -> *) a. Monad m => Sz1 -> Steps m a -> Steps m a
drop Sz1
forall ix. Index ix => Sz ix
oneSz Steps m e
sts) (e -> (e, Steps m e)) -> Maybe e -> Maybe (e, Steps m e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe e
mx) (Maybe e -> Maybe (e, Steps m e))
-> m (Maybe e) -> m (Maybe (e, Steps m e))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Steps m e -> m (Maybe e)
forall (m :: * -> *) a. Monad m => Steps m a -> m (Maybe a)
headMaybe Steps m e
sts
{-# INLINE uncons #-}

snoc :: Monad m => Steps m e -> e -> Steps m e
snoc :: Steps m e -> e -> Steps m e
snoc (Steps Stream m e
str LengthHint
k) e
e = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Stream m e -> e -> Stream m e
forall (m :: * -> *) a. Monad m => Stream m a -> a -> Stream m a
S.snoc Stream m e
str e
e) (LengthHint
k LengthHint -> Int -> LengthHint
`addInt` Int
1)
{-# INLINE snoc #-}

traverse :: (Monad m, Applicative f) => (e -> f a) -> Steps Id e -> f (Steps m a)
traverse :: (e -> f a) -> Steps Id e -> f (Steps m a)
traverse e -> f a
f (Steps Stream Id e
str LengthHint
k) = (Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
k) (Stream m a -> Steps m a) -> f (Stream m a) -> f (Steps m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([e] -> f [a]) -> Stream Id e -> f (Stream m a)
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA ((e -> f a) -> [e] -> f [a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
Traversable.traverse e -> f a
f) Stream Id e
str
{-# INLINE traverse #-}

append :: Monad m => Steps m e -> Steps m e -> Steps m e
append :: Steps m e -> Steps m e -> Steps m e
append (Steps Stream m e
str1 LengthHint
k1) (Steps Stream m e
str2 LengthHint
k2) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Stream m e
str1 Stream m e -> Stream m e -> Stream m e
forall (m :: * -> *) a.
Monad m =>
Stream m a -> Stream m a -> Stream m a
S.++ Stream m e
str2) (LengthHint
k1 LengthHint -> LengthHint -> LengthHint
`addLengthHint` LengthHint
k2)
{-# INLINE append #-}

map :: Monad m => (e -> a) -> Steps m e -> Steps m a
map :: (e -> a) -> Steps m e -> Steps m a
map e -> a
f (Steps Stream m e
str LengthHint
k) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((e -> a) -> Stream m e -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b) -> Stream m a -> Stream m b
S.map e -> a
f Stream m e
str) LengthHint
k
{-# INLINE map #-}

indexed :: Monad m => Steps m e -> Steps m (Int, e)
indexed :: Steps m e -> Steps m (Int, e)
indexed (Steps Stream m e
str LengthHint
k) = Stream m (Int, e) -> LengthHint -> Steps m (Int, e)
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Stream m e -> Stream m (Int, e)
forall (m :: * -> *) a. Monad m => Stream m a -> Stream m (Int, a)
S.indexed Stream m e
str) LengthHint
k
{-# INLINE indexed #-}

mapM :: Monad m => (e -> m a) -> Steps m e -> Steps m a
mapM :: (e -> m a) -> Steps m e -> Steps m a
mapM e -> m a
f (Steps Stream m e
str LengthHint
k) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((e -> m a) -> Stream m e -> Stream m a
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> Stream m b
S.mapM e -> m a
f Stream m e
str) LengthHint
k
{-# INLINE mapM #-}

mapM_ :: Monad m => (e -> m a) -> Steps m e -> m ()
mapM_ :: (e -> m a) -> Steps m e -> m ()
mapM_ e -> m a
f (Steps Stream m e
str LengthHint
_) = (e -> m a) -> Stream m e -> m ()
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream m a -> m ()
S.mapM_ e -> m a
f Stream m e
str
{-# INLINE mapM_ #-}

zipWith :: Monad m => (a -> b -> e) -> Steps m a -> Steps m b -> Steps m e
zipWith :: (a -> b -> e) -> Steps m a -> Steps m b -> Steps m e
zipWith a -> b -> e
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> e) -> Stream m a -> Stream m b -> Stream m e
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> Stream m a -> Stream m b -> Stream m c
S.zipWith a -> b -> e
f Stream m a
sa Stream m b
sb) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka LengthHint
kb)
{-# INLINE zipWith #-}

zipWith3 :: Monad m => (a -> b -> c -> d) -> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3 :: (a -> b -> c -> d)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3 a -> b -> c -> d
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) =
  Stream m d -> LengthHint -> Steps m d
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> d)
-> Stream m a -> Stream m b -> Stream m c -> Stream m d
S.zipWith3 a -> b -> c -> d
f Stream m a
sa Stream m b
sb Stream m c
sc) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb LengthHint
kc))
{-# INLINE zipWith3 #-}

zipWith4 ::
  Monad m => (a -> b -> c -> d -> e) -> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
zipWith4 :: (a -> b -> c -> d -> e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
zipWith4 a -> b -> c -> d -> e
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) =
  Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> c -> d -> e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
S.zipWith4 a -> b -> c -> d -> e
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc LengthHint
kd)))
{-# INLINE zipWith4 #-}

zipWith5 ::
     Monad m
  => (a -> b -> c -> d -> e -> f)
  -> Steps m a
  -> Steps m b
  -> Steps m c
  -> Steps m d
  -> Steps m e
  -> Steps m f
zipWith5 :: (a -> b -> c -> d -> e -> f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
zipWith5 a -> b -> c -> d -> e -> f
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) (Steps Stream m e
se LengthHint
ke) =
  Stream m f -> LengthHint -> Steps m f
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> c -> d -> e -> f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
S.zipWith5 a -> b -> c -> d -> e -> f
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kd LengthHint
ke))))
{-# INLINE zipWith5 #-}

zipWith6 ::
     Monad m
  => (a -> b -> c -> d -> e -> f -> g)
  -> Steps m a
  -> Steps m b
  -> Steps m c
  -> Steps m d
  -> Steps m e
  -> Steps m f
  -> Steps m g
zipWith6 :: (a -> b -> c -> d -> e -> f -> g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
zipWith6 a -> b -> c -> d -> e -> f -> g
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) (Steps Stream m e
se LengthHint
ke) (Steps Stream m f
sf LengthHint
kf) =
  Stream m g -> LengthHint -> Steps m g
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps
    ((a -> b -> c -> d -> e -> f -> g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
S.zipWith6 a -> b -> c -> d -> e -> f -> g
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se Stream m f
sf)
    (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kd (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ke LengthHint
kf)))))
{-# INLINE zipWith6 #-}

zipWithM :: Monad m => (a -> b -> m c) -> Steps m a -> Steps m b -> Steps m c
zipWithM :: (a -> b -> m c) -> Steps m a -> Steps m b -> Steps m c
zipWithM a -> b -> m c
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) = Stream m c -> LengthHint -> Steps m c
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((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) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka LengthHint
kb)
{-# INLINE zipWithM #-}


zipWith3M :: Monad m => (a -> b -> c -> m d) -> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3M :: (a -> b -> c -> m d)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3M a -> b -> c -> m d
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) =
  Stream m d -> LengthHint -> Steps m d
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((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) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb LengthHint
kc))
{-# INLINE zipWith3M #-}

zipWith4M ::
     Monad m
  => (a -> b -> c -> d -> m e)
  -> Steps m a
  -> Steps m b
  -> Steps m c
  -> Steps m d
  -> Steps m e
zipWith4M :: (a -> b -> c -> d -> m e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
zipWith4M a -> b -> c -> d -> m e
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) =
  Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> c -> d -> m e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> m e)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
S.zipWith4M a -> b -> c -> d -> m e
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc LengthHint
kd)))
{-# INLINE zipWith4M #-}

zipWith5M ::
     Monad m
  => (a -> b -> c -> d -> e -> m f)
  -> Steps m a
  -> Steps m b
  -> Steps m c
  -> Steps m d
  -> Steps m e
  -> Steps m f
zipWith5M :: (a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
zipWith5M a -> b -> c -> d -> e -> m f
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) (Steps Stream m e
se LengthHint
ke) =
  Stream m f -> LengthHint -> Steps m f
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> b -> c -> d -> e -> m f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> m f)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
S.zipWith5M a -> b -> c -> d -> e -> m f
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se) (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kd LengthHint
ke))))
{-# INLINE zipWith5M #-}

zipWith6M ::
     Monad m
  => (a -> b -> c -> d -> e -> f -> m g)
  -> Steps m a
  -> Steps m b
  -> Steps m c
  -> Steps m d
  -> Steps m e
  -> Steps m f
  -> Steps m g
zipWith6M :: (a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
zipWith6M a -> b -> c -> d -> e -> f -> m g
f (Steps Stream m a
sa LengthHint
ka) (Steps Stream m b
sb LengthHint
kb) (Steps Stream m c
sc LengthHint
kc) (Steps Stream m d
sd LengthHint
kd) (Steps Stream m e
se LengthHint
ke) (Steps Stream m f
sf LengthHint
kf) =
  Stream m g -> LengthHint -> Steps m g
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps
    ((a -> b -> c -> d -> e -> f -> m g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> m g)
-> Stream m a
-> Stream m b
-> Stream m c
-> Stream m d
-> Stream m e
-> Stream m f
-> Stream m g
S.zipWith6M a -> b -> c -> d -> e -> f -> m g
f Stream m a
sa Stream m b
sb Stream m c
sc Stream m d
sd Stream m e
se Stream m f
sf)
    (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ka (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kb (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kc (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
kd (LengthHint -> LengthHint -> LengthHint
minLengthHint LengthHint
ke LengthHint
kf)))))
{-# INLINE zipWith6M #-}


zipWithM_ :: Monad m => (a -> b -> m c) -> Steps m a -> Steps m b -> m ()
zipWithM_ :: (a -> b -> m c) -> Steps m a -> Steps m b -> m ()
zipWithM_ a -> b -> m c
f (Steps Stream m a
str1 LengthHint
_) (Steps Stream m b
str2 LengthHint
_) = (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 Stream m a
str1 Stream m b
str2
{-# INLINE zipWithM_ #-}

zipWith3M_ :: Monad m => (a -> b -> c -> m d) -> Steps m a -> Steps m b -> Steps m c -> m ()
zipWith3M_ :: (a -> b -> c -> m d) -> Steps m a -> Steps m b -> Steps m c -> m ()
zipWith3M_ a -> b -> c -> m d
f Steps m a
sa Steps m b
sb Steps m c
sc = Steps m d -> m ()
forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume (Steps m d -> m ()) -> Steps m d -> m ()
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> m d)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d
forall (m :: * -> *) a b c d.
Monad m =>
(a -> b -> c -> m d)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d
zipWith3M a -> b -> c -> m d
f Steps m a
sa Steps m b
sb Steps m c
sc
{-# INLINE zipWith3M_ #-}


zipWith4M_ ::
     Monad m
  => (a -> b -> c -> d -> m e)
  -> Steps m a
  -> Steps m b
  -> Steps m c
  -> Steps m d
  -> m ()
zipWith4M_ :: (a -> b -> c -> d -> m e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> m ()
zipWith4M_ a -> b -> c -> d -> m e
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd = Steps m e -> m ()
forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume (Steps m e -> m ()) -> Steps m e -> m ()
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> d -> m e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
forall (m :: * -> *) a b c d e.
Monad m =>
(a -> b -> c -> d -> m e)
-> Steps m a -> Steps m b -> Steps m c -> Steps m d -> Steps m e
zipWith4M a -> b -> c -> d -> m e
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd
{-# INLINE zipWith4M_ #-}

zipWith5M_ ::
     Monad m
  => (a -> b -> c -> d -> e -> m f)
  -> Steps m a
  -> Steps m b
  -> Steps m c
  -> Steps m d
  -> Steps m e
  -> m ()
zipWith5M_ :: (a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> m ()
zipWith5M_ a -> b -> c -> d -> e -> m f
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd Steps m e
se = Steps m f -> m ()
forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume (Steps m f -> m ()) -> Steps m f -> m ()
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
forall (m :: * -> *) a b c d e f.
Monad m =>
(a -> b -> c -> d -> e -> m f)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
zipWith5M a -> b -> c -> d -> e -> m f
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd Steps m e
se
{-# INLINE zipWith5M_ #-}

zipWith6M_ ::
     Monad m
  => (a -> b -> c -> d -> e -> f -> m g)
  -> Steps m a
  -> Steps m b
  -> Steps m c
  -> Steps m d
  -> Steps m e
  -> Steps m f
  -> m ()
zipWith6M_ :: (a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> m ()
zipWith6M_ a -> b -> c -> d -> e -> f -> m g
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd Steps m e
se Steps m f
sf = Steps m g -> m ()
forall (m :: * -> *) a. Monad m => Steps m a -> m ()
consume (Steps m g -> m ()) -> Steps m g -> m ()
forall a b. (a -> b) -> a -> b
$ (a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
forall (m :: * -> *) a b c d e f g.
Monad m =>
(a -> b -> c -> d -> e -> f -> m g)
-> Steps m a
-> Steps m b
-> Steps m c
-> Steps m d
-> Steps m e
-> Steps m f
-> Steps m g
zipWith6M a -> b -> c -> d -> e -> f -> m g
f Steps m a
sa Steps m b
sb Steps m c
sc Steps m d
sd Steps m e
se Steps m f
sf
{-# INLINE zipWith6M_ #-}



consume :: Monad m => Steps m a -> m ()
consume :: Steps m a -> m ()
consume (Steps (S.Stream s -> m (Step s a)
step s
t) LengthHint
_) = SPEC -> s -> m ()
consumeLoop SPEC
S.SPEC s
t
  where
    consumeLoop :: SPEC -> s -> m ()
consumeLoop !SPEC
_ s
s = do
      Step s a
r <- s -> m (Step s a)
step s
s
      case Step s a
r of
        S.Yield a
_ s
s' -> SPEC -> s -> m ()
consumeLoop SPEC
S.SPEC s
s'
        S.Skip s
s' -> SPEC -> s -> m ()
consumeLoop SPEC
S.SPEC s
s'
        Step s a
S.Done -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# INLINE consume #-}

transStepsId :: Monad m => Steps Id e -> Steps m e
transStepsId :: Steps Id e -> Steps m e
transStepsId (Steps Stream Id e
sts LengthHint
k) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((forall z. Id z -> m z) -> Stream Id e -> Stream m e
forall (m :: * -> *) (m' :: * -> *) a.
(Monad m, Monad m') =>
(forall z. m z -> m' z) -> Stream m a -> Stream m' a
S.trans (z -> m z
forall (f :: * -> *) a. Applicative f => a -> f a
pure (z -> m z) -> (Id z -> z) -> Id z -> m z
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Id z -> z
forall a. Id a -> a
unId) Stream Id e
sts) LengthHint
k
{-# INLINE transStepsId #-}

transSteps :: (Monad m, Monad n) => Steps m e -> m (Steps n e)
transSteps :: Steps m e -> m (Steps n e)
transSteps (Steps Stream m e
strM sz :: LengthHint
sz@(LengthExact Sz1
_)) = (Stream n e -> LengthHint -> Steps n e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
sz) (Stream n e -> Steps n e) -> m (Stream n e) -> m (Steps n e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Stream m e -> m (Stream n e)
forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
Stream m a -> m (Stream n a)
transListM Stream m e
strM
transSteps (Steps Stream m e
strM LengthHint
_) = do
  (Sz1
n, Stream n e
strN) <- Stream m e -> m (Sz1, Stream n e)
forall (m :: * -> *) (n :: * -> *) a.
(Monad m, Monad n) =>
Stream m a -> m (Sz1, Stream n a)
transListNM Stream m e
strM
  Steps n e -> m (Steps n e)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Stream n e -> LengthHint -> Steps n e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps Stream n e
strN (Sz1 -> LengthHint
LengthExact Sz1
n))
{-# INLINE transSteps #-}


foldl :: Monad m => (b -> a -> b) -> b -> Steps m a -> m b
foldl :: (b -> a -> b) -> b -> Steps m a -> m b
foldl b -> a -> b
f b
acc = (b -> a -> b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
S.foldl' b -> a -> b
f b
acc (Stream m a -> m b)
-> (Steps m a -> Stream m a) -> Steps m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldl #-}

foldl1 :: Monad m => (a -> a -> a) -> Steps m a -> m a
foldl1 :: (a -> a -> a) -> Steps m a -> m a
foldl1 a -> a -> a
f = (a -> a -> a) -> Stream m a -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
S.foldl1' a -> a -> a
f (Stream m a -> m a)
-> (Steps m a -> Stream m a) -> Steps m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldl1 #-}


foldlM :: Monad m => (a -> b -> m a) -> a -> Steps m b -> m a
foldlM :: (a -> b -> m a) -> a -> Steps m b -> m a
foldlM a -> b -> m a
f a
acc = (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
f a
acc (Stream m b -> m a)
-> (Steps m b -> Stream m b) -> Steps m b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m b -> Stream m b
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldlM #-}


foldl1M :: Monad m => (a -> a -> m a) -> Steps m a -> m a
foldl1M :: (a -> a -> m a) -> Steps m a -> m a
foldl1M a -> a -> m a
f (Steps Stream m a
sts LengthHint
_) = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
S.foldl1M' a -> a -> m a
f Stream m a
sts
{-# INLINE foldl1M #-}


foldrLazy :: Monad m => (a -> b -> b) -> b -> Steps m a -> m b
foldrLazy :: (a -> b -> b) -> b -> Steps m a -> m b
foldrLazy a -> b -> b
f b
acc = (a -> b -> b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
S.foldr a -> b -> b
f b
acc (Stream m a -> m b)
-> (Steps m a -> Stream m a) -> Steps m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldrLazy #-}

foldr1Lazy :: Monad m => (a -> a -> a) -> Steps m a -> m a
foldr1Lazy :: (a -> a -> a) -> Steps m a -> m a
foldr1Lazy a -> a -> a
f = (a -> a -> a) -> Stream m a -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
S.foldr1 a -> a -> a
f (Stream m a -> m a)
-> (Steps m a -> Stream m a) -> Steps m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldr1Lazy #-}

foldlLazy :: Monad m => (b -> a -> b) -> b -> Steps m a -> m b
foldlLazy :: (b -> a -> b) -> b -> Steps m a -> m b
foldlLazy b -> a -> b
f b
acc = (b -> a -> b) -> b -> Stream m a -> m b
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> a) -> a -> Stream m b -> m a
S.foldl b -> a -> b
f b
acc (Stream m a -> m b)
-> (Steps m a -> Stream m a) -> Steps m a -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldlLazy #-}

foldl1Lazy :: Monad m => (a -> a -> a) -> Steps m a -> m a
foldl1Lazy :: (a -> a -> a) -> Steps m a -> m a
foldl1Lazy a -> a -> a
f = (a -> a -> a) -> Stream m a -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> a) -> Stream m a -> m a
S.foldl1 a -> a -> a
f (Stream m a -> m a)
-> (Steps m a -> Stream m a) -> Steps m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldl1Lazy #-}


foldlLazyM :: Monad m => (a -> b -> m a) -> a -> Steps m b -> m a
foldlLazyM :: (a -> b -> m a) -> a -> Steps m b -> m a
foldlLazyM a -> b -> m a
f a
acc = (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
f a
acc (Stream m b -> m a)
-> (Steps m b -> Stream m b) -> Steps m b -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m b -> Stream m b
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldlLazyM #-}


foldl1LazyM :: Monad m => (a -> a -> m a) -> Steps m a -> m a
foldl1LazyM :: (a -> a -> m a) -> Steps m a -> m a
foldl1LazyM a -> a -> m a
f (Steps Stream m a
sts LengthHint
_) = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
S.foldl1M a -> a -> m a
f Stream m a
sts
{-# INLINE foldl1LazyM #-}


foldrLazyM :: Monad m => (b -> a -> m a) -> a -> Steps m b -> m a
foldrLazyM :: (b -> a -> m a) -> a -> Steps m b -> m a
foldrLazyM b -> a -> m a
f a
acc (Steps Stream m b
sts LengthHint
_) = (b -> a -> m a) -> a -> Stream m b -> m a
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m b) -> b -> Stream m a -> m b
S.foldrM b -> a -> m a
f a
acc Stream m b
sts
{-# INLINE foldrLazyM #-}


foldr1LazyM :: Monad m => (a -> a -> m a) -> Steps m a -> m a
foldr1LazyM :: (a -> a -> m a) -> Steps m a -> m a
foldr1LazyM a -> a -> m a
f = (a -> a -> m a) -> Stream m a -> m a
forall (m :: * -> *) a.
Monad m =>
(a -> a -> m a) -> Stream m a -> m a
S.foldr1M a -> a -> m a
f (Stream m a -> m a)
-> (Steps m a -> Stream m a) -> Steps m a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m a -> Stream m a
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE foldr1LazyM #-}


or :: Monad m => Steps m Bool -> m Bool
or :: Steps m 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)
-> (Steps m Bool -> Stream m Bool) -> Steps m Bool -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m Bool -> Stream m Bool
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE or #-}

and :: Monad m => Steps m Bool -> m Bool
and :: Steps m 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)
-> (Steps m Bool -> Stream m Bool) -> Steps m Bool -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Steps m Bool -> Stream m Bool
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream
{-# INLINE and #-}


mapMaybe :: Monad m => (a -> Maybe e) -> Steps m a -> Steps m e
mapMaybe :: (a -> Maybe e) -> Steps m a -> Steps m e
mapMaybe a -> Maybe e
f (Steps Stream m a
str LengthHint
k) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> Maybe e) -> Stream m a -> Stream m e
forall (m :: * -> *) a b.
Monad m =>
(a -> Maybe b) -> Stream m a -> Stream m b
S.mapMaybe a -> Maybe e
f Stream m a
str) (LengthHint -> LengthHint
toLengthMax LengthHint
k)
{-# INLINE mapMaybe #-}

concatMap :: Monad m => (a -> Steps m e) -> Steps m a -> Steps m e
concatMap :: (a -> Steps m e) -> Steps m a -> Steps m e
concatMap a -> Steps m e
f (Steps Stream m a
str LengthHint
_) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> Stream m e) -> Stream m a -> Stream m e
forall (m :: * -> *) a b.
Monad m =>
(a -> Stream m b) -> Stream m a -> Stream m b
S.concatMap (Steps m e -> Stream m e
forall (m :: * -> *) e. Steps m e -> Stream m e
stepsStream (Steps m e -> Stream m e) -> (a -> Steps m e) -> a -> Stream m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Steps m e
f) Stream m a
str) LengthHint
LengthUnknown
{-# INLINE concatMap #-}


mapMaybeA :: (Monad m, Applicative f) => (a -> f (Maybe e)) -> Steps Id a -> f (Steps m e)
mapMaybeA :: (a -> f (Maybe e)) -> Steps Id a -> f (Steps m e)
mapMaybeA a -> f (Maybe e)
f (Steps Stream Id a
str LengthHint
k) = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint -> LengthHint
toLengthMax LengthHint
k) (Stream m e -> Steps m e) -> f (Stream m e) -> f (Steps m e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([a] -> f [e]) -> Stream Id a -> f (Stream m e)
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA ((a -> f (Maybe e)) -> [a] -> f [e]
forall (f :: * -> *) a b.
Applicative f =>
(a -> f (Maybe b)) -> [a] -> f [b]
mapMaybeListA a -> f (Maybe e)
f) Stream Id a
str
{-# INLINE mapMaybeA #-}

mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Steps m a -> Steps m b
mapMaybeM :: (a -> m (Maybe b)) -> Steps m a -> Steps m b
mapMaybeM a -> m (Maybe b)
f (Steps Stream m a
str LengthHint
k) = Stream m b -> LengthHint -> Steps m b
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((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
mapMaybeStreamM a -> m (Maybe b)
f Stream m a
str) (LengthHint -> LengthHint
toLengthMax LengthHint
k)
{-# INLINE mapMaybeM #-}

mapMaybeListA :: Applicative f => (a -> f (Maybe b)) -> [a] -> f [b]
mapMaybeListA :: (a -> f (Maybe b)) -> [a] -> f [b]
mapMaybeListA a -> f (Maybe b)
f = ([Maybe b] -> [b]) -> f [Maybe b] -> f [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Maybe b] -> [b]
forall a. [Maybe a] -> [a]
catMaybes (f [Maybe b] -> f [b]) -> ([a] -> f [Maybe b]) -> [a] -> f [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> f (Maybe b)) -> [a] -> f [Maybe b]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
Traversable.traverse a -> f (Maybe b)
f
{-# INLINE mapMaybeListA #-}

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

filter :: Monad m => (a -> Bool) -> Steps m a -> Steps m a
filter :: (a -> Bool) -> Steps m a -> Steps m a
filter a -> Bool
f (Steps Stream m a
str LengthHint
k) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((a -> Bool) -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
S.filter a -> Bool
f Stream m a
str) (LengthHint -> LengthHint
toLengthMax LengthHint
k)
{-# INLINE filter #-}


filterA :: (Monad m, Applicative f) => (e -> f Bool) -> Steps Id e -> f (Steps m e)
filterA :: (e -> f Bool) -> Steps Id e -> f (Steps m e)
filterA e -> f Bool
f (Steps Stream Id e
str LengthHint
k) = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint -> LengthHint
toLengthMax LengthHint
k) (Stream m e -> Steps m e) -> f (Stream m e) -> f (Steps m e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([e] -> f [e]) -> Stream Id e -> f (Stream m e)
forall (m :: * -> *) (f :: * -> *) a b.
(Monad m, Functor f) =>
([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA ((e -> f Bool) -> [e] -> f [e]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
M.filterM e -> f Bool
f) Stream Id e
str
{-# INLINE filterA #-}

filterM :: Monad m => (e -> m Bool) -> Steps m e -> Steps m e
filterM :: (e -> m Bool) -> Steps m e -> Steps m e
filterM e -> m Bool
f (Steps Stream m e
str LengthHint
k) = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((e -> m Bool) -> Stream m e -> Stream m e
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Stream m a -> Stream m a
S.filterM e -> m Bool
f Stream m e
str) (LengthHint -> LengthHint
toLengthMax LengthHint
k)
{-# INLINE filterM #-}

take :: Monad m => Sz1 -> Steps m a -> Steps m a
take :: Sz1 -> Steps m a -> Steps m a
take Sz1
n (Steps Stream m a
str LengthHint
sz) =
  Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
S.take (Sz1 -> Int
coerce Sz1
n) Stream m a
str) (LengthHint -> Steps m a) -> LengthHint -> Steps m a
forall a b. (a -> b) -> a -> b
$!
  case LengthHint
sz of
    LengthExact Sz1
k -> Sz1 -> LengthHint
LengthExact ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
n Sz1
k)
    LengthMax Sz1
k -> Sz1 -> LengthHint
LengthMax ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
n Sz1
k)
    LengthHint
LengthUnknown -> LengthHint
LengthUnknown
{-# INLINE take #-}

drop :: Monad m => Sz1 -> Steps m a -> Steps m a
drop :: Sz1 -> Steps m a -> Steps m a
drop Sz1
n (Steps Stream m a
str LengthHint
k) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> Stream m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> Stream m a -> Stream m a
S.drop (Sz1 -> Int
coerce Sz1
n) Stream m a
str) (LengthHint
k LengthHint -> LengthHint -> LengthHint
`subtractLengthHint` Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE drop #-}

slice :: Monad m => Int -> Sz1 -> Steps m a -> Steps m a
slice :: Int -> Sz1 -> Steps m a -> Steps m a
slice Int
i Sz1
k (Steps Stream m a
str LengthHint
_) = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> Int -> Stream m a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> Int -> Stream m a -> Stream m a
S.slice Int
i (Sz1 -> Int
coerce Sz1
k) Stream m a
str) (Sz1 -> LengthHint
LengthMax Sz1
k)
{-# INLINE slice #-}

iterateN :: Monad m => Sz1 -> (a -> a) -> a -> Steps m a
iterateN :: Sz1 -> (a -> a) -> a -> Steps m a
iterateN Sz1
n a -> a
f a
a = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (a -> a) -> a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (a -> a) -> a -> Stream m a
S.iterateN (Sz1 -> Int
coerce Sz1
n) a -> a
f a
a) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE iterateN #-}

iterateNM :: Monad m => Sz1 -> (a -> m a) -> a -> Steps m a
iterateNM :: Sz1 -> (a -> m a) -> a -> Steps m a
iterateNM Sz1
n a -> m a
f a
a = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (a -> m a) -> a -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (a -> m a) -> a -> Stream m a
S.iterateNM (Sz1 -> Int
coerce Sz1
n) a -> m a
f a
a) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE iterateNM #-}

replicate :: Monad m => Sz1 -> a -> Steps m a
replicate :: Sz1 -> a -> Steps m a
replicate Sz1
n a
a = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
S.replicate (Sz1 -> Int
coerce Sz1
n) a
a) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE replicate #-}

replicateM :: Monad m => Sz1 -> m a -> Steps m a
replicateM :: Sz1 -> m a -> Steps m a
replicateM Sz1
n m a
f = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> m a -> Stream m a
forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
S.replicateM (Sz1 -> Int
coerce Sz1
n) m a
f) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE replicateM #-}


generateM :: Monad m => Sz1 -> (Int -> m a) -> Steps m a
generateM :: Sz1 -> (Int -> m a) -> Steps m a
generateM Sz1
n Int -> m a
f = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (Int -> m a) -> Stream m a
forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
S.generateM (Sz1 -> Int
coerce Sz1
n) Int -> m a
f) (Sz1 -> LengthHint
LengthExact Sz1
n)
{-# INLINE generateM #-}


unfoldr :: Monad m => (s -> Maybe (e, s)) -> s -> Steps m e
unfoldr :: (s -> Maybe (e, s)) -> s -> Steps m e
unfoldr s -> Maybe (e, s)
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((s -> Maybe (e, s)) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
(s -> Maybe (a, s)) -> s -> Stream m a
S.unfoldr s -> Maybe (e, s)
f s
e0) LengthHint
LengthUnknown
{-# INLINE unfoldr #-}

unfoldrN :: Monad m => Sz1 -> (s -> Maybe (e, s)) -> s -> Steps m e
unfoldrN :: Sz1 -> (s -> Maybe (e, s)) -> s -> Steps m e
unfoldrN Sz1
n s -> Maybe (e, s)
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (s -> Maybe (e, s)) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> Maybe (a, s)) -> s -> Stream m a
S.unfoldrN (Sz1 -> Int
coerce Sz1
n) s -> Maybe (e, s)
f s
e0) LengthHint
LengthUnknown
{-# INLINE unfoldrN #-}

unsafeUnfoldrN :: Monad m => Sz1 -> (s -> Maybe (e, s)) -> s -> Steps m e
unsafeUnfoldrN :: Sz1 -> (s -> Maybe (e, s)) -> s -> Steps m e
unsafeUnfoldrN Sz1
n s -> Maybe (e, s)
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (s -> Maybe (e, s)) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> Maybe (a, s)) -> s -> Stream m a
S.unfoldrN (Sz1 -> Int
coerce Sz1
n) s -> Maybe (e, s)
f s
e0) (Sz1 -> LengthHint
LengthMax Sz1
n)
{-# INLINE unsafeUnfoldrN #-}

unfoldrM :: Monad m => (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrM :: (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrM s -> m (Maybe (e, s))
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps ((s -> m (Maybe (e, s))) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrM s -> m (Maybe (e, s))
f s
e0) LengthHint
LengthUnknown
{-# INLINE unfoldrM #-}

unfoldrNM :: Monad m => Int -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrNM :: Int -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unfoldrNM Int
n s -> m (Maybe (e, s))
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (s -> m (Maybe (e, s))) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrNM Int
n s -> m (Maybe (e, s))
f s
e0) LengthHint
LengthUnknown
{-# INLINE unfoldrNM #-}

unsafeUnfoldrNM :: Monad m => Sz1 -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unsafeUnfoldrNM :: Sz1 -> (s -> m (Maybe (e, s))) -> s -> Steps m e
unsafeUnfoldrNM Sz1
n s -> m (Maybe (e, s))
f s
e0 = Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (Int -> (s -> m (Maybe (e, s))) -> s -> Stream m e
forall (m :: * -> *) s a.
Monad m =>
Int -> (s -> m (Maybe (a, s))) -> s -> Stream m a
S.unfoldrNM (Sz1 -> Int
coerce Sz1
n) s -> m (Maybe (e, s))
f s
e0) (Sz1 -> LengthHint
LengthMax Sz1
n)
{-# INLINE unsafeUnfoldrNM #-}

unfoldrExactN :: Monad m => Sz1 -> (s -> (a, s)) -> s -> Steps m a
unfoldrExactN :: Sz1 -> (s -> (a, s)) -> s -> Steps m a
unfoldrExactN Sz1
n s -> (a, s)
f = Sz1 -> (s -> m (a, s)) -> s -> Steps m a
forall (m :: * -> *) s a.
Monad m =>
Sz1 -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM Sz1
n ((a, s) -> m (a, s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((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)
{-# INLINE unfoldrExactN #-}

unfoldrExactNM :: Monad m => Sz1 -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM :: Sz1 -> (s -> m (a, s)) -> s -> Steps m a
unfoldrExactNM Sz1
n s -> m (a, s)
f s
t = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (((s, Int) -> m (Step (s, Int) a)) -> (s, Int) -> Stream m a
forall (m :: * -> *) a s. (s -> m (Step s a)) -> s -> Stream m a
S.Stream (s, Int) -> m (Step (s, Int) a)
step (s
t, Sz1 -> Int
forall ix. Sz ix -> ix
unSz Sz1
n)) (Sz1 -> LengthHint
LengthExact Sz1
n)
  where
    step :: (s, Int) -> m (Step (s, Int) a)
step (s
s, Int
i)
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Step (s, Int) a -> m (Step (s, Int) a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Step (s, Int) a
forall s a. Step s a
S.Done
      | Bool
otherwise = ((a, s) -> Step (s, Int) a) -> m (a, s) -> m (Step (s, Int) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(a
x, s
s') -> a -> (s, Int) -> Step (s, Int) a
forall a s. a -> s -> Step s a
S.Yield a
x (s
s', Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) (s -> m (a, s)
f s
s)
    {-# INLINE [0] step #-}
{-# INLINE unfoldrExactNM #-}


enumFromStepN :: (Num a, Monad m) => a -> a -> Sz1 -> Steps m a
enumFromStepN :: a -> a -> Sz1 -> Steps m a
enumFromStepN a
x a
step Sz1
k = Stream m a -> LengthHint -> Steps m a
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
Steps (a -> a -> Int -> Stream m a
forall a (m :: * -> *).
(Num a, Monad m) =>
a -> a -> Int -> Stream m a
S.enumFromStepN a
x a
step (Sz1 -> Int
coerce Sz1
k)) (Sz1 -> LengthHint
LengthExact Sz1
k)
{-# INLINE enumFromStepN #-}




toList :: Steps Id e -> [e]
toList :: Steps Id e -> [e]
toList (Steps Stream Id e
str LengthHint
_) = Id [e] -> [e]
forall a. Id a -> a
unId (Stream Id e -> Id [e]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream Id e
str)
{-# INLINE toList #-}

fromList :: Monad m => [e] -> Steps m e
fromList :: [e] -> Steps m e
fromList = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
LengthUnknown) (Stream m e -> Steps m e)
-> ([e] -> Stream m e) -> [e] -> Steps m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [e] -> Stream m e
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList
{-# INLINE fromList #-}

fromListN :: Monad m => Int -> [e] -> Steps m e
fromListN :: Int -> [e] -> Steps m e
fromListN Int
n  = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` LengthHint
LengthUnknown) (Stream m e -> Steps m e)
-> ([e] -> Stream m e) -> [e] -> Steps m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [e] -> Stream m e
forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
S.fromListN Int
n
{-# INLINE fromListN #-}

unsafeFromListN :: Monad m => Sz1 -> [e] -> Steps m e
unsafeFromListN :: Sz1 -> [e] -> Steps m e
unsafeFromListN Sz1
n  = (Stream m e -> LengthHint -> Steps m e
forall (m :: * -> *) e. Stream m e -> LengthHint -> Steps m e
`Steps` Sz1 -> LengthHint
LengthMax Sz1
n) (Stream m e -> Steps m e)
-> ([e] -> Stream m e) -> [e] -> Steps m e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [e] -> Stream m e
forall (m :: * -> *) a. Monad m => Int -> [a] -> Stream m a
S.fromListN (Sz1 -> Int
coerce Sz1
n)
{-# INLINE unsafeFromListN #-}

liftListA :: (Monad m, Functor f) => ([a] -> f [b]) -> S.Stream Id a -> f (S.Stream m b)
liftListA :: ([a] -> f [b]) -> Stream Id a -> f (Stream m b)
liftListA [a] -> f [b]
f Stream Id a
str = [b] -> Stream m b
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList ([b] -> Stream m b) -> f [b] -> f (Stream m b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> f [b]
f (Id [a] -> [a]
forall a. Id a -> a
unId (Stream Id a -> Id [a]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream Id a
str))
{-# INLINE liftListA #-}


transListM :: (Monad m, Monad n) => S.Stream m a -> m (S.Stream n a)
transListM :: Stream m a -> m (Stream n a)
transListM Stream m a
str = do
  [a]
xs <- Stream m a -> m [a]
forall (m :: * -> *) a. Monad m => Stream m a -> m [a]
S.toList Stream m a
str
  Stream n a -> m (Stream n a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Stream n a -> m (Stream n a)) -> Stream n a -> m (Stream n a)
forall a b. (a -> b) -> a -> b
$ [a] -> Stream n a
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [a]
xs
{-# INLINE transListM #-}

transListNM :: (Monad m, Monad n) => S.Stream m a -> m (Sz1, S.Stream n a)
transListNM :: Stream m a -> m (Sz1, Stream n a)
transListNM Stream m a
str = do
  (Int
n, [a]
xs) <- Stream m a -> m (Int, [a])
forall (m :: * -> *) a. Monad m => Stream m a -> m (Int, [a])
toListN Stream m a
str
  (Sz1, Stream n a) -> m (Sz1, Stream n a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> Sz1
coerce Int
n, [a] -> Stream n a
forall (m :: * -> *) a. Monad m => [a] -> Stream m a
S.fromList [a]
xs)
{-# INLINE transListNM #-}


toListN :: Monad m => S.Stream m a -> m (Int, [a])
toListN :: Stream m a -> m (Int, [a])
toListN = (a -> (Int, [a]) -> (Int, [a]))
-> (Int, [a]) -> Stream m a -> m (Int, [a])
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> b) -> b -> Stream m a -> m b
S.foldr (\a
x (Int
i, [a]
xs) -> (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)) (Int
0, [])
{-# INLINE toListN #-}


sizeHintToBundleSize :: LengthHint -> B.Size
sizeHintToBundleSize :: LengthHint -> Size
sizeHintToBundleSize =
  \case
    LengthExact Sz1
k -> Int -> Size
B.Exact (Sz1 -> Int
coerce Sz1
k)
    LengthMax Sz1
k   -> Int -> Size
B.Max (Sz1 -> Int
coerce Sz1
k)
    LengthHint
LengthUnknown -> Size
B.Unknown
{-# INLINE sizeHintToBundleSize #-}

addHint :: (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint :: (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
hint Int
m Int
n
  | Int
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Sz1 -> Int
coerce Sz1
sz = Sz1 -> LengthHint
hint Sz1
sz
  | Bool
otherwise = LengthHint
LengthUnknown -- overflow
  where
    k :: Int
k = Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n
    sz :: Sz1
sz = Int -> Sz1
forall ix. Index ix => ix -> Sz ix
Sz Int
k
{-# INLINE addHint #-}



addInt :: LengthHint -> Int -> LengthHint
addInt :: LengthHint -> Int -> LengthHint
addInt (LengthExact Sz1
m) Int
n = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthExact (Sz1 -> Int
coerce Sz1
m) (Int -> Int
coerce Int
n)
addInt (LengthMax   Sz1
m) Int
n = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthExact (Sz1 -> Int
coerce Sz1
m) Int
n
addInt LengthHint
_               Int
_ = LengthHint
LengthUnknown
{-# INLINE addInt #-}

addLengthHint :: LengthHint -> LengthHint -> LengthHint
addLengthHint :: LengthHint -> LengthHint -> LengthHint
addLengthHint (LengthExact Sz1
m) (LengthExact Sz1
n) = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthExact (Sz1 -> Int
coerce Sz1
m) (Sz1 -> Int
coerce Sz1
n)
addLengthHint (LengthMax   Sz1
m) (LengthExact Sz1
n) = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthMax (Sz1 -> Int
coerce Sz1
m) (Sz1 -> Int
coerce Sz1
n)
addLengthHint (LengthExact Sz1
m) (LengthMax   Sz1
n) = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthMax (Sz1 -> Int
coerce Sz1
m) (Sz1 -> Int
coerce Sz1
n)
addLengthHint (LengthMax   Sz1
m) (LengthMax   Sz1
n) = (Sz1 -> LengthHint) -> Int -> Int -> LengthHint
addHint Sz1 -> LengthHint
LengthMax (Sz1 -> Int
coerce Sz1
m) (Sz1 -> Int
coerce Sz1
n)
addLengthHint LengthHint
_               LengthHint
_               = LengthHint
LengthUnknown
{-# INLINE addLengthHint #-}

subtractLengthHint :: LengthHint -> LengthHint -> LengthHint
subtractLengthHint :: LengthHint -> LengthHint -> LengthHint
subtractLengthHint (LengthExact Sz1
m) (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthExact (Sz1
m Sz1 -> Sz1 -> Sz1
forall a. Num a => a -> a -> a
- Sz1
n)
subtractLengthHint (LengthMax   Sz1
m) (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthMax (Sz1
m Sz1 -> Sz1 -> Sz1
forall a. Num a => a -> a -> a
- Sz1
n)
subtractLengthHint (LengthExact Sz1
m) (LengthMax   Sz1
_) = Sz1 -> LengthHint
LengthMax Sz1
m
subtractLengthHint (LengthMax   Sz1
m) (LengthMax   Sz1
_) = Sz1 -> LengthHint
LengthMax Sz1
m
subtractLengthHint LengthHint
_               LengthHint
_               = LengthHint
LengthUnknown
{-# INLINE subtractLengthHint #-}


minLengthHint :: LengthHint -> LengthHint -> LengthHint
minLengthHint :: LengthHint -> LengthHint -> LengthHint
minLengthHint (LengthExact Sz1
m) (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthExact ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthExact Sz1
m) (LengthMax   Sz1
n) = Sz1 -> LengthHint
LengthMax   ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthExact Sz1
m) LengthHint
LengthUnknown   = Sz1 -> LengthHint
LengthMax   Sz1
m
minLengthHint (LengthMax   Sz1
m) (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthMax   ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthMax   Sz1
m) (LengthMax   Sz1
n) = Sz1 -> LengthHint
LengthMax   ((Sz1 -> Sz1 -> Sz1) -> Sz1 -> Sz1 -> Sz1
forall a b. (a -> b) -> a -> b
inline0 Sz1 -> Sz1 -> Sz1
forall a. Ord a => a -> a -> a
min Sz1
m Sz1
n)
minLengthHint (LengthMax   Sz1
m) LengthHint
LengthUnknown   = Sz1 -> LengthHint
LengthMax   Sz1
m
minLengthHint LengthHint
LengthUnknown   (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthMax   Sz1
n
minLengthHint LengthHint
LengthUnknown   (LengthMax   Sz1
n) = Sz1 -> LengthHint
LengthMax   Sz1
n
minLengthHint LengthHint
LengthUnknown   LengthHint
LengthUnknown   = LengthHint
LengthUnknown
{-# INLINE minLengthHint #-}

toLengthMax :: LengthHint -> LengthHint
toLengthMax :: LengthHint -> LengthHint
toLengthMax (LengthExact Sz1
n) = Sz1 -> LengthHint
LengthMax Sz1
n
toLengthMax (LengthMax   Sz1
n) = Sz1 -> LengthHint
LengthMax Sz1
n
toLengthMax LengthHint
LengthUnknown   = LengthHint
LengthUnknown
{-# INLINE toLengthMax #-}