#include "inline.hs"
module Streamly.Internal.Data.Unfold
(
Step(..)
, Unfold
, mkUnfoldM
, mkUnfoldrM
, unfoldrM
, unfoldr
, functionM
, function
, identity
, nilM
, consM
, fromEffect
, fromPure
, repeatM
, replicateM
, fromIndicesM
, iterateM
, Enumerable (..)
, enumerateFromNum
, enumerateFromThenNum
, enumerateFromStepNum
, enumerateFromIntegralBounded
, enumerateFromThenIntegralBounded
, enumerateFromToIntegralBounded
, enumerateFromThenToIntegralBounded
, enumerateFromIntegral
, enumerateFromThenIntegral
, enumerateFromToIntegral
, enumerateFromThenToIntegral
, enumerateFromSmallBounded
, enumerateFromThenSmallBounded
, enumerateFromToSmall
, enumerateFromThenToSmall
, enumerateFromFractional
, enumerateFromThenFractional
, enumerateFromToFractional
, enumerateFromThenToFractional
, fromList
, fromListM
, fromStream
, fromStreamK
, fromStreamD
, lmap
, lmapM
, supply
, supplyFirst
, supplySecond
, discardFirst
, discardSecond
, swap
, fold
, map
, mapM
, mapMWithInput
, scanlM'
, scan
, foldMany
, either
, takeWhileM
, takeWhile
, take
, filter
, filterM
, drop
, dropWhile
, dropWhileM
, zipWithM
, zipWith
, crossWithM
, crossWith
, cross
, apply
, ConcatState (..)
, many
, concatMapM
, bind
, gbracket_
, gbracket
, before
, after
, after_
, finally
, finally_
, bracket
, bracket_
, onException
, handle
)
where
import Control.Exception (Exception, mask_)
import Control.Monad.Catch (MonadCatch)
import Data.Functor (($>))
import GHC.Types (SPEC(..))
import Streamly.Internal.Control.Concurrent (MonadRunInIO, MonadAsync, withRunInIO)
import Streamly.Internal.Data.Fold.Type (Fold(..))
import Streamly.Internal.Data.IOFinalizer
(newIOFinalizer, runIOFinalizer, clearingIOFinalizer)
import Streamly.Internal.Data.Stream.Serial (SerialT(..))
import Streamly.Internal.Data.Stream.StreamD.Type (Stream(..), Step(..))
import Streamly.Internal.Data.SVar.Type (defState)
import qualified Control.Monad.Catch as MC
import qualified Data.Tuple as Tuple
import qualified Streamly.Internal.Data.Fold.Type as FL
import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
import Streamly.Internal.Data.Unfold.Enumeration
import Streamly.Internal.Data.Unfold.Type
import Prelude
hiding (map, mapM, takeWhile, take, filter, const, zipWith
, drop, dropWhile, either)
{-# INLINE_NORMAL discardFirst #-}
discardFirst :: Unfold m a b -> Unfold m (c, a) b
discardFirst :: forall (m :: * -> *) a b c. Unfold m a b -> Unfold m (c, a) b
discardFirst = forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
lmap forall a b. (a, b) -> b
snd
{-# INLINE_NORMAL discardSecond #-}
discardSecond :: Unfold m a b -> Unfold m (a, c) b
discardSecond :: forall (m :: * -> *) a b c. Unfold m a b -> Unfold m (a, c) b
discardSecond = forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
lmap forall a b. (a, b) -> a
fst
{-# INLINE_NORMAL swap #-}
swap :: Unfold m (a, c) b -> Unfold m (c, a) b
swap :: forall (m :: * -> *) a c b. Unfold m (a, c) b -> Unfold m (c, a) b
swap = forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
lmap forall a b. (a, b) -> (b, a)
Tuple.swap
{-# INLINE_NORMAL fold #-}
fold :: Monad m => Fold m b c -> Unfold m a b -> a -> m c
fold :: forall (m :: * -> *) b c a.
Monad m =>
Fold m b c -> Unfold m a b -> a -> m c
fold (Fold s -> b -> m (Step s c)
fstep m (Step s c)
initial s -> m c
extract) (Unfold s -> m (Step s b)
ustep a -> m s
inject) a
a = do
Step s c
res <- m (Step s c)
initial
case Step s c
res of
FL.Partial s
x -> a -> m s
inject a
a forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= SPEC -> s -> s -> m c
go SPEC
SPEC s
x
FL.Done c
b -> forall (m :: * -> *) a. Monad m => a -> m a
return c
b
where
{-# INLINE_LATE go #-}
go :: SPEC -> s -> s -> m c
go !SPEC
_ !s
fs s
st = do
Step s b
r <- s -> m (Step s b)
ustep s
st
case Step s b
r of
Yield b
x s
s -> do
Step s c
res <- s -> b -> m (Step s c)
fstep s
fs b
x
case Step s c
res of
FL.Partial s
fs1 -> SPEC -> s -> s -> m c
go SPEC
SPEC s
fs1 s
s
FL.Done c
c -> forall (m :: * -> *) a. Monad m => a -> m a
return c
c
Skip s
s -> SPEC -> s -> s -> m c
go SPEC
SPEC s
fs s
s
Step s b
Stop -> s -> m c
extract s
fs
foldMany ::
Fold m b c -> Unfold m a b -> Unfold m a c
foldMany :: forall (m :: * -> *) b c a.
Fold m b c -> Unfold m a b -> Unfold m a c
foldMany = forall a. HasCallStack => a
undefined
{-# INLINE_NORMAL mapM #-}
mapM :: Monad m => (b -> m c) -> Unfold m a b -> Unfold m a c
mapM :: forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> Unfold m a b -> Unfold m a c
mapM b -> m c
f (Unfold s -> m (Step s b)
ustep a -> m s
uinject) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold s -> m (Step s c)
step a -> m s
uinject
where
{-# INLINE_LATE step #-}
step :: s -> m (Step s c)
step s
st = do
Step s b
r <- s -> m (Step s b)
ustep s
st
case Step s b
r of
Yield b
x s
s -> b -> m c
f b
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c
a -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield c
a s
s
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip s
s
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL mapMWithInput #-}
mapMWithInput :: Monad m => (a -> b -> m c) -> Unfold m a b -> Unfold m a c
mapMWithInput :: forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> m c) -> Unfold m a b -> Unfold m a c
mapMWithInput a -> b -> m c
f (Unfold s -> m (Step s b)
ustep a -> m s
uinject) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (a, s) -> m (Step (a, s) c)
step a -> m (a, s)
inject
where
inject :: a -> m (a, s)
inject a
a = do
s
r <- a -> m s
uinject a
a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, s
r)
{-# INLINE_LATE step #-}
step :: (a, s) -> m (Step (a, s) c)
step (a
inp, s
st) = do
Step s b
r <- s -> m (Step s b)
ustep s
st
case Step s b
r of
Yield b
x s
s -> a -> b -> m c
f a
inp b
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \c
a -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield c
a (a
inp, s
s)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (a
inp, s
s)
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL either #-}
either :: Applicative m => Unfold m a b -> Unfold m (Either a a) (Either b b)
either :: forall (m :: * -> *) a b.
Applicative m =>
Unfold m a b -> Unfold m (Either a a) (Either b b)
either (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {a}. (s, b -> a) -> m (Step (s, b -> a) a)
step forall {b}. Either a a -> m (s, b -> Either b b)
inject
where
inject :: Either a a -> m (s, b -> Either b b)
inject (Left a
a) = (, forall a b. a -> Either a b
Left) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m s
inject1 a
a
inject (Right a
a) = (, forall a b. b -> Either a b
Right) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m s
inject1 a
a
{-# INLINE_LATE step #-}
step :: (s, b -> a) -> m (Step (s, b -> a) a)
step (s
st, b -> a
f) = do
(\case
Yield b
x s
s -> forall s a. a -> s -> Step s a
Yield (b -> a
f b
x) (s
s, b -> a
f)
Skip s
s -> forall s a. s -> Step s a
Skip (s
s, b -> a
f)
Step s b
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m (Step s b)
step1 s
st
{-# INLINE_NORMAL scan #-}
scan :: Monad m => Fold m b c -> Unfold m a b -> Unfold m a c
scan :: forall (m :: * -> *) b c a.
Monad m =>
Fold m b c -> Unfold m a b -> Unfold m a c
scan (Fold s -> b -> m (Step s c)
stepF m (Step s c)
initial s -> m c
extract) (Unfold s -> m (Step s b)
stepU a -> m s
injectU) =
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Maybe (s, s) -> m (Step (Maybe (s, s)) c)
step a -> m (Maybe (s, s))
inject
where
inject :: a -> m (Maybe (s, s))
inject a
a = do
Step s c
r <- m (Step s c)
initial
case Step s c
r of
FL.Partial s
fs -> forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s
fs,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m s
injectU a
a
FL.Done c
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
{-# INLINE_LATE step #-}
step :: Maybe (s, s) -> m (Step (Maybe (s, s)) c)
step (Just (s
fs, s
us)) = do
Step s b
ru <- s -> m (Step s b)
stepU s
us
case Step s b
ru of
Yield b
x s
s -> do
Step s c
rf <- s -> b -> m (Step s c)
stepF s
fs b
x
case Step s c
rf of
FL.Done c
v -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield c
v forall a. Maybe a
Nothing
FL.Partial s
fs1 -> do
c
v <- s -> m c
extract s
fs1
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield c
v (forall a. a -> Maybe a
Just (s
fs1, s
s))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a. a -> Maybe a
Just (s
fs, s
s))
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step Maybe (s, s)
Nothing = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL scanlM' #-}
scanlM' :: Monad m => (b -> a -> m b) -> m b -> Unfold m c a -> Unfold m c b
scanlM' :: forall (m :: * -> *) b a c.
Monad m =>
(b -> a -> m b) -> m b -> Unfold m c a -> Unfold m c b
scanlM' b -> a -> m b
f m b
z = forall (m :: * -> *) b c a.
Monad m =>
Fold m b c -> Unfold m a b -> Unfold m a c
scan (forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> m b -> Fold m a b
FL.foldlM' b -> a -> m b
f m b
z)
{-# INLINE_NORMAL fromStreamD #-}
fromStreamD :: Applicative m => Unfold m (Stream m a) a
fromStreamD :: forall (m :: * -> *) a. Applicative m => Unfold m (Stream m a) a
fromStreamD = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {m :: * -> *} {a}.
Functor m =>
Stream m a -> m (Step (Stream m a) a)
step forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
{-# INLINE_LATE step #-}
step :: Stream m a -> m (Step (Stream m a) a)
step (UnStream State Stream m a -> s -> m (Step s a)
step1 s
state1) =
(\case
Yield a
x s
s -> forall s a. a -> s -> Step s a
Yield a
x (forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State Stream m a -> s -> m (Step s a)
step1 s
s)
Skip s
s -> forall s a. s -> Step s a
Skip (forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State Stream m a -> s -> m (Step s a)
step1 s
s)
Step s a
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> State Stream m a -> s -> m (Step s a)
step1 forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
state1
{-# INLINE_NORMAL fromStreamK #-}
fromStreamK :: Applicative m => Unfold m (K.Stream m a) a
fromStreamK :: forall (m :: * -> *) a. Applicative m => Unfold m (Stream m a) a
fromStreamK = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {f :: * -> *} {a}.
Applicative f =>
Stream f a -> f (Step (Stream f a) a)
step forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
{-# INLINE_LATE step #-}
step :: Stream f a -> f (Step (Stream f a) a)
step Stream f a
stream = do
(\case
Just (a
x, Stream f a
xs) -> forall s a. a -> s -> Step s a
Yield a
x Stream f a
xs
Maybe (a, Stream f a)
Nothing -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
Applicative m =>
Stream m a -> m (Maybe (a, Stream m a))
K.uncons Stream f a
stream
{-# INLINE_NORMAL fromStream #-}
fromStream :: Monad m => Unfold m (SerialT m a) a
fromStream :: forall (m :: * -> *) a. Monad m => Unfold m (SerialT m a) a
fromStream = forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
lmap forall (m :: * -> *) a. SerialT m a -> Stream m a
getSerialT forall (m :: * -> *) a. Applicative m => Unfold m (Stream m a) a
fromStreamK
{-# INLINE nilM #-}
nilM :: Applicative m => (a -> m c) -> Unfold m a b
nilM :: forall (m :: * -> *) a c b.
Applicative m =>
(a -> m c) -> Unfold m a b
nilM a -> m c
f = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {s} {a}. a -> m (Step s a)
step forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
{-# INLINE_LATE step #-}
step :: a -> m (Step s a)
step a
x = a -> m c
f a
x forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> forall s a. Step s a
Stop
{-# INLINE_NORMAL consM #-}
consM :: Applicative m => (a -> m b) -> Unfold m a b -> Unfold m a b
consM :: forall (m :: * -> *) a b.
Applicative m =>
(a -> m b) -> Unfold m a b -> Unfold m a b
consM a -> m b
action Unfold m a b
unf = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {a}.
Either a (Stream m b) -> m (Step (Either a (Stream m b)) b)
step forall {a} {b}. a -> m (Either a b)
inject
where
inject :: a -> m (Either a b)
inject = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left
{-# INLINE_LATE step #-}
step :: Either a (Stream m b) -> m (Step (Either a (Stream m b)) b)
step (Left a
a) = (forall s a. a -> s -> Step s a
`Yield` forall a b. b -> Either a b
Right (forall (m :: * -> *) a b.
Applicative m =>
Unfold m a b -> a -> Stream m b
D.unfold Unfold m a b
unf a
a)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m b
action a
a
step (Right (UnStream State Stream m b -> s -> m (Step s b)
step1 s
st)) = do
(\case
Yield b
x s
s -> forall s a. a -> s -> Step s a
Yield b
x (forall a b. b -> Either a b
Right (forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State Stream m b -> s -> m (Step s b)
step1 s
s))
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (forall (m :: * -> *) a s.
(State Stream m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State Stream m b -> s -> m (Step s b)
step1 s
s))
Step s b
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> State Stream m b -> s -> m (Step s b)
step1 forall (t :: (* -> *) -> * -> *) (m :: * -> *) a. State t m a
defState s
st
{-# INLINE_LATE fromList #-}
fromList :: Monad m => Unfold m [a] a
fromList :: forall (m :: * -> *) a. Monad m => Unfold m [a] a
fromList = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {f :: * -> *} {a}. Applicative f => [a] -> f (Step [a] a)
step forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
{-# INLINE_LATE step #-}
step :: [a] -> f (Step [a] a)
step (a
x:[a]
xs) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x [a]
xs
step [] = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE_LATE fromListM #-}
fromListM :: Monad m => Unfold m [m a] a
fromListM :: forall (m :: * -> *) a. Monad m => Unfold m [m a] a
fromListM = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {f :: * -> *} {a}.
Applicative f =>
[f a] -> f (Step [f a] a)
step forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
{-# INLINE_LATE step #-}
step :: [f a] -> f (Step [f a] a)
step (f a
x:[f a]
xs) = (forall s a. a -> s -> Step s a
`Yield` [f a]
xs) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
x
step [] = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE replicateM #-}
replicateM :: Monad m => Int -> Unfold m (m a) a
replicateM :: forall (m :: * -> *) a. Monad m => Int -> Unfold m (m a) a
replicateM Int
n = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {b} {f :: * -> *} {a}.
(Ord b, Num b, Applicative f) =>
(f a, b) -> f (Step (f a, b) a)
step forall {f :: * -> *} {a}. Applicative f => a -> f (a, Int)
inject
where
inject :: a -> f (a, Int)
inject a
action = forall (f :: * -> *) a. Applicative f => a -> f a
pure (a
action, Int
n)
{-# INLINE_LATE step #-}
step :: (f a, b) -> f (Step (f a, b) a)
step (f a
action, b
i) =
if b
i forall a. Ord a => a -> a -> Bool
<= b
0
then forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
else (\a
x -> forall s a. a -> s -> Step s a
Yield a
x (f a
action, b
i forall a. Num a => a -> a -> a
- b
1)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
action
{-# INLINE repeatM #-}
repeatM :: Monad m => Unfold m (m a) a
repeatM :: forall (m :: * -> *) a. Monad m => Unfold m (m a) a
repeatM = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {f :: * -> *} {a}. Functor f => f a -> f (Step (f a) a)
step forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
{-# INLINE_LATE step #-}
step :: f a -> f (Step (f a) a)
step f a
action = (forall s a. a -> s -> Step s a
`Yield` f a
action) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
action
{-# INLINE iterateM #-}
iterateM :: Monad m => (a -> m a) -> Unfold m (m a) a
iterateM :: forall (m :: * -> *) a. Monad m => (a -> m a) -> Unfold m (m a) a
iterateM a -> m a
f = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold a -> m (Step a a)
step forall a. a -> a
id
where
{-# INLINE_LATE step #-}
step :: a -> m (Step a a)
step a
x = forall s a. a -> s -> Step s a
Yield a
x forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m a
f a
x
{-# INLINE_NORMAL fromIndicesM #-}
fromIndicesM :: Applicative m => (Int -> m a) -> Unfold m Int a
fromIndicesM :: forall (m :: * -> *) a.
Applicative m =>
(Int -> m a) -> Unfold m Int a
fromIndicesM Int -> m a
gen = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Int -> m (Step Int a)
step forall (f :: * -> *) a. Applicative f => a -> f a
pure
where
{-# INLINE_LATE step #-}
step :: Int -> m (Step Int a)
step Int
i = (forall s a. a -> s -> Step s a
`Yield` (Int
i forall a. Num a => a -> a -> a
+ Int
1)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> m a
gen Int
i
{-# INLINE_NORMAL take #-}
take :: Monad m => Int -> Unfold m a b -> Unfold m a b
take :: forall (m :: * -> *) a b.
Monad m =>
Int -> Unfold m a b -> Unfold m a b
take Int
n (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (s, Int) -> m (Step (s, Int) b)
step forall {t}. Num t => a -> m (s, t)
inject
where
inject :: a -> m (s, t)
inject a
x = (, t
0) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m s
inject1 a
x
{-# INLINE_LATE step #-}
step :: (s, Int) -> m (Step (s, Int) b)
step (s
st, Int
i) | Int
i forall a. Ord a => a -> a -> Bool
< Int
n = do
(\case
Yield b
x s
s -> forall s a. a -> s -> Step s a
Yield b
x (s
s, Int
i forall a. Num a => a -> a -> a
+ Int
1)
Skip s
s -> forall s a. s -> Step s a
Skip (s
s, Int
i)
Step s b
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m (Step s b)
step1 s
st
step (s
_, Int
_) = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop
{-# INLINE_NORMAL filterM #-}
filterM :: Monad m => (b -> m Bool) -> Unfold m a b -> Unfold m a b
filterM :: forall (m :: * -> *) b a.
Monad m =>
(b -> m Bool) -> Unfold m a b -> Unfold m a b
filterM b -> m Bool
f (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold s -> m (Step s b)
step a -> m s
inject1
where
{-# INLINE_LATE step #-}
step :: s -> m (Step s b)
step s
st = do
Step s b
r <- s -> m (Step s b)
step1 s
st
case Step s b
r of
Yield b
x s
s -> do
Bool
b <- b -> m Bool
f b
x
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if Bool
b then forall s a. a -> s -> Step s a
Yield b
x s
s else forall s a. s -> Step s a
Skip s
s
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip s
s
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE filter #-}
filter :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b
filter :: forall (m :: * -> *) b a.
Monad m =>
(b -> Bool) -> Unfold m a b -> Unfold m a b
filter b -> Bool
f = forall (m :: * -> *) b a.
Monad m =>
(b -> m Bool) -> Unfold m a b -> Unfold m a b
filterM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Bool
f)
{-# INLINE_NORMAL drop #-}
drop :: Monad m => Int -> Unfold m a b -> Unfold m a b
drop :: forall (m :: * -> *) a b.
Monad m =>
Int -> Unfold m a b -> Unfold m a b
drop Int
n (Unfold s -> m (Step s b)
step a -> m s
inject) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold forall {b}. (Ord b, Num b) => (s, b) -> m (Step (s, b) b)
step' a -> m (s, Int)
inject'
where
inject' :: a -> m (s, Int)
inject' a
a = (, Int
n) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> m s
inject a
a
{-# INLINE_LATE step' #-}
step' :: (s, b) -> m (Step (s, b) b)
step' (s
st, b
i)
| b
i forall a. Ord a => a -> a -> Bool
> b
0 = do
(\case
Yield b
_ s
s -> forall s a. s -> Step s a
Skip (s
s, b
i forall a. Num a => a -> a -> a
- b
1)
Skip s
s -> forall s a. s -> Step s a
Skip (s
s, b
i)
Step s b
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m (Step s b)
step s
st
| Bool
otherwise = do
(\case
Yield b
x s
s -> forall s a. a -> s -> Step s a
Yield b
x (s
s, b
0)
Skip s
s -> forall s a. s -> Step s a
Skip (s
s, b
0)
Step s b
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m (Step s b)
step s
st
{-# INLINE_NORMAL dropWhileM #-}
dropWhileM :: Monad m => (b -> m Bool) -> Unfold m a b -> Unfold m a b
dropWhileM :: forall (m :: * -> *) b a.
Monad m =>
(b -> m Bool) -> Unfold m a b -> Unfold m a b
dropWhileM b -> m Bool
f (Unfold s -> m (Step s b)
step a -> m s
inject) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Either s s -> m (Step (Either s s) b)
step' forall {b}. a -> m (Either s b)
inject'
where
inject' :: a -> m (Either s b)
inject' a
a = do
s
b <- a -> m s
inject a
a
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left s
b
{-# INLINE_LATE step' #-}
step' :: Either s s -> m (Step (Either s s) b)
step' (Left s
st) = do
Step s b
r <- s -> m (Step s b)
step s
st
case Step s b
r of
Yield b
x s
s -> do
Bool
b <- b -> m Bool
f b
x
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ if Bool
b
then forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s)
else forall s a. a -> s -> Step s a
Yield b
x (forall a b. b -> Either a b
Right s
s)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s)
Step s b
Stop -> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
step' (Right s
st) = do
Step s b
r <- s -> m (Step s b)
step s
st
forall (m :: * -> *) a. Monad m => a -> m a
return
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield b
x s
s -> forall s a. a -> s -> Step s a
Yield b
x (forall a b. b -> Either a b
Right s
s)
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right s
s)
Step s b
Stop -> forall s a. Step s a
Stop
{-# INLINE dropWhile #-}
dropWhile :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b
dropWhile :: forall (m :: * -> *) b a.
Monad m =>
(b -> Bool) -> Unfold m a b -> Unfold m a b
dropWhile b -> Bool
f = forall (m :: * -> *) b a.
Monad m =>
(b -> m Bool) -> Unfold m a b -> Unfold m a b
dropWhileM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Bool
f)
{-# INLINE_NORMAL gbracket_ #-}
gbracket_
:: Monad m
=> (a -> m c)
-> (forall s. m s -> m (Either e s))
-> (c -> m d)
-> Unfold m (c, e) b
-> Unfold m c b
-> Unfold m a b
gbracket_ :: forall (m :: * -> *) a c e d b.
Monad m =>
(a -> m c)
-> (forall s. m s -> m (Either e s))
-> (c -> m d)
-> Unfold m (c, e) b
-> Unfold m c b
-> Unfold m a b
gbracket_ a -> m c
bef forall s. m s -> m (Either e s)
exc c -> m d
aft (Unfold s -> m (Step s b)
estep (c, e) -> m s
einject) (Unfold s -> m (Step s b)
step1 c -> m s
inject1) =
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Either s (s, c) -> m (Step (Either s (s, c)) b)
step forall {a}. a -> m (Either a (s, c))
inject
where
inject :: a -> m (Either a (s, c))
inject a
x = do
c
r <- a -> m c
bef a
x
s
s <- c -> m s
inject1 c
r
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right (s
s, c
r)
{-# INLINE_LATE step #-}
step :: Either s (s, c) -> m (Step (Either s (s, c)) b)
step (Right (s
st, c
v)) = do
Either e (Step s b)
res <- forall s. m s -> m (Either e s)
exc forall a b. (a -> b) -> a -> b
$ s -> m (Step s b)
step1 s
st
case Either e (Step s b)
res of
Right Step s b
r -> case Step s b
r of
Yield b
x s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
x (forall a b. b -> Either a b
Right (s
s, c
v))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (s
s, c
v))
Step s b
Stop -> c -> m d
aft c
v forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
Left e
e -> do
s
r <- (c, e) -> m s
einject (c
v, e
e)
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
r)
step (Left s
st) = do
Step s b
res <- s -> m (Step s b)
estep s
st
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s b
res of
Yield b
x s
s -> forall s a. a -> s -> Step s a
Yield b
x (forall a b. a -> Either a b
Left s
s)
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s)
Step s b
Stop -> forall s a. Step s a
Stop
{-# INLINE_NORMAL gbracket #-}
gbracket
:: MonadRunInIO m
=> (a -> m c)
-> (forall s. m s -> m (Either e s))
-> (c -> m d)
-> Unfold m (c, e) b
-> Unfold m c b
-> Unfold m a b
gbracket :: forall (m :: * -> *) a c e d b.
MonadRunInIO m =>
(a -> m c)
-> (forall s. m s -> m (Either e s))
-> (c -> m d)
-> Unfold m (c, e) b
-> Unfold m c b
-> Unfold m a b
gbracket a -> m c
bef forall s. m s -> m (Either e s)
exc c -> m d
aft (Unfold s -> m (Step s b)
estep (c, e) -> m s
einject) (Unfold s -> m (Step s b)
step1 c -> m s
inject1) =
forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold Either s (s, c, IOFinalizer)
-> m (Step (Either s (s, c, IOFinalizer)) b)
step forall {a}. a -> m (Either a (s, c, IOFinalizer))
inject
where
inject :: a -> m (Either a (s, c, IOFinalizer))
inject a
x = do
(c
r, IOFinalizer
ref) <- forall (m :: * -> *) b.
MonadRunInIO m =>
((forall a. m a -> IO (StM m a)) -> IO (StM m b)) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO (StM m a)
run -> forall a. IO a -> IO a
mask_ forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO (StM m a)
run forall a b. (a -> b) -> a -> b
$ do
c
r <- a -> m c
bef a
x
IOFinalizer
ref <- forall (m :: * -> *) a. MonadRunInIO m => m a -> m IOFinalizer
newIOFinalizer (c -> m d
aft c
r)
forall (m :: * -> *) a. Monad m => a -> m a
return (c
r, IOFinalizer
ref)
s
s <- c -> m s
inject1 c
r
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. b -> Either a b
Right (s
s, c
r, IOFinalizer
ref)
{-# INLINE_LATE step #-}
step :: Either s (s, c, IOFinalizer)
-> m (Step (Either s (s, c, IOFinalizer)) b)
step (Right (s
st, c
v, IOFinalizer
ref)) = do
Either e (Step s b)
res <- forall s. m s -> m (Either e s)
exc forall a b. (a -> b) -> a -> b
$ s -> m (Step s b)
step1 s
st
case Either e (Step s b)
res of
Right Step s b
r -> case Step s b
r of
Yield b
x s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
x (forall a b. b -> Either a b
Right (s
s, c
v, IOFinalizer
ref))
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. b -> Either a b
Right (s
s, c
v, IOFinalizer
ref))
Step s b
Stop -> do
forall (m :: * -> *). MonadIO m => IOFinalizer -> m ()
runIOFinalizer IOFinalizer
ref
forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
Left e
e -> do
s
r <- forall (m :: * -> *) a. MonadRunInIO m => IOFinalizer -> m a -> m a
clearingIOFinalizer IOFinalizer
ref ((c, e) -> m s
einject (c
v, e
e))
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
r)
step (Left s
st) = do
Step s b
res <- s -> m (Step s b)
estep s
st
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s b
res of
Yield b
x s
s -> forall s a. a -> s -> Step s a
Yield b
x (forall a b. a -> Either a b
Left s
s)
Skip s
s -> forall s a. s -> Step s a
Skip (forall a b. a -> Either a b
Left s
s)
Step s b
Stop -> forall s a. Step s a
Stop
{-# INLINE_NORMAL before #-}
before :: (a -> m c) -> Unfold m a b -> Unfold m a b
before :: forall a (m :: * -> *) c b.
(a -> m c) -> Unfold m a b -> Unfold m a b
before a -> m c
action (Unfold s -> m (Step s b)
step a -> m s
inject) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold s -> m (Step s b)
step (a -> m c
action forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> a -> m s
inject)
{-# INLINE_NORMAL _after #-}
_after :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
_after :: forall (m :: * -> *) a c b.
Monad m =>
(a -> m c) -> Unfold m a b -> Unfold m a b
_after a -> m c
aft = forall (m :: * -> *) a c e d b.
Monad m =>
(a -> m c)
-> (forall s. m s -> m (Either e s))
-> (c -> m d)
-> Unfold m (c, e) b
-> Unfold m c b
-> Unfold m a b
gbracket_ forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. b -> Either a b
Right) a -> m c
aft forall a. HasCallStack => a
undefined
{-# INLINE_NORMAL after_ #-}
after_ :: Monad m => (a -> m c) -> Unfold m a b -> Unfold m a b
after_ :: forall (m :: * -> *) a c b.
Monad m =>
(a -> m c) -> Unfold m a b -> Unfold m a b
after_ a -> m c
action (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (s, a) -> m (Step (s, a) b)
step a -> m (s, a)
inject
where
inject :: a -> m (s, a)
inject a
x = do
s
s <- a -> m s
inject1 a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, a
x)
{-# INLINE_LATE step #-}
step :: (s, a) -> m (Step (s, a) b)
step (s
st, a
v) = do
Step s b
res <- s -> m (Step s b)
step1 s
st
case Step s b
res of
Yield b
x s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
x (s
s, a
v)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s, a
v)
Step s b
Stop -> a -> m c
action a
v forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL after #-}
after :: MonadRunInIO m
=> (a -> m c) -> Unfold m a b -> Unfold m a b
after :: forall (m :: * -> *) a c b.
MonadRunInIO m =>
(a -> m c) -> Unfold m a b -> Unfold m a b
after a -> m c
action (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (s, IOFinalizer) -> m (Step (s, IOFinalizer) b)
step a -> m (s, IOFinalizer)
inject
where
inject :: a -> m (s, IOFinalizer)
inject a
x = do
s
s <- a -> m s
inject1 a
x
IOFinalizer
ref <- forall (m :: * -> *) a. MonadRunInIO m => m a -> m IOFinalizer
newIOFinalizer (a -> m c
action a
x)
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, IOFinalizer
ref)
{-# INLINE_LATE step #-}
step :: (s, IOFinalizer) -> m (Step (s, IOFinalizer) b)
step (s
st, IOFinalizer
ref) = do
Step s b
res <- s -> m (Step s b)
step1 s
st
case Step s b
res of
Yield b
x s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
x (s
s, IOFinalizer
ref)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s, IOFinalizer
ref)
Step s b
Stop -> do
forall (m :: * -> *). MonadIO m => IOFinalizer -> m ()
runIOFinalizer IOFinalizer
ref
forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL _onException #-}
_onException :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
_onException :: forall (m :: * -> *) a c b.
MonadCatch m =>
(a -> m c) -> Unfold m a b -> Unfold m a b
_onException a -> m c
action =
forall (m :: * -> *) a c e d b.
Monad m =>
(a -> m c)
-> (forall s. m s -> m (Either e s))
-> (c -> m d)
-> Unfold m (c, e) b
-> Unfold m c b
-> Unfold m a b
gbracket_ forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
MC.try
(\a
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ())
(forall (m :: * -> *) a c b.
Applicative m =>
(a -> m c) -> Unfold m a b
nilM (\(a
a, SomeException
e :: MC.SomeException) -> a -> m c
action a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
MC.throwM SomeException
e))
{-# INLINE_NORMAL onException #-}
onException :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
onException :: forall (m :: * -> *) a c b.
MonadCatch m =>
(a -> m c) -> Unfold m a b -> Unfold m a b
onException a -> m c
action (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (s, a) -> m (Step (s, a) b)
step a -> m (s, a)
inject
where
inject :: a -> m (s, a)
inject a
x = do
s
s <- a -> m s
inject1 a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, a
x)
{-# INLINE_LATE step #-}
step :: (s, a) -> m (Step (s, a) b)
step (s
st, a
v) = do
Step s b
res <- s -> m (Step s b)
step1 s
st forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`MC.onException` a -> m c
action a
v
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s b
res of
Yield b
x s
s -> forall s a. a -> s -> Step s a
Yield b
x (s
s, a
v)
Skip s
s -> forall s a. s -> Step s a
Skip (s
s, a
v)
Step s b
Stop -> forall s a. Step s a
Stop
{-# INLINE_NORMAL _finally #-}
_finally :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
_finally :: forall (m :: * -> *) a c b.
MonadCatch m =>
(a -> m c) -> Unfold m a b -> Unfold m a b
_finally a -> m c
action =
forall (m :: * -> *) a c e d b.
Monad m =>
(a -> m c)
-> (forall s. m s -> m (Either e s))
-> (c -> m d)
-> Unfold m (c, e) b
-> Unfold m c b
-> Unfold m a b
gbracket_ forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
MC.try a -> m c
action
(forall (m :: * -> *) a c b.
Applicative m =>
(a -> m c) -> Unfold m a b
nilM (\(a
a, SomeException
e :: MC.SomeException) -> a -> m c
action a
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
MC.throwM SomeException
e))
{-# INLINE_NORMAL finally_ #-}
finally_ :: MonadCatch m => (a -> m c) -> Unfold m a b -> Unfold m a b
finally_ :: forall (m :: * -> *) a c b.
MonadCatch m =>
(a -> m c) -> Unfold m a b -> Unfold m a b
finally_ a -> m c
action (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (s, a) -> m (Step (s, a) b)
step a -> m (s, a)
inject
where
inject :: a -> m (s, a)
inject a
x = do
s
s <- a -> m s
inject1 a
x
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, a
x)
{-# INLINE_LATE step #-}
step :: (s, a) -> m (Step (s, a) b)
step (s
st, a
v) = do
Step s b
res <- s -> m (Step s b)
step1 s
st forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`MC.onException` a -> m c
action a
v
case Step s b
res of
Yield b
x s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
x (s
s, a
v)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s, a
v)
Step s b
Stop -> a -> m c
action a
v forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL finally #-}
finally :: (MonadAsync m, MonadCatch m)
=> (a -> m c) -> Unfold m a b -> Unfold m a b
finally :: forall (m :: * -> *) a c b.
(MonadAsync m, MonadCatch m) =>
(a -> m c) -> Unfold m a b -> Unfold m a b
finally a -> m c
action (Unfold s -> m (Step s b)
step1 a -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (s, IOFinalizer) -> m (Step (s, IOFinalizer) b)
step a -> m (s, IOFinalizer)
inject
where
inject :: a -> m (s, IOFinalizer)
inject a
x = do
s
s <- a -> m s
inject1 a
x
IOFinalizer
ref <- forall (m :: * -> *) a. MonadRunInIO m => m a -> m IOFinalizer
newIOFinalizer (a -> m c
action a
x)
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, IOFinalizer
ref)
{-# INLINE_LATE step #-}
step :: (s, IOFinalizer) -> m (Step (s, IOFinalizer) b)
step (s
st, IOFinalizer
ref) = do
Step s b
res <- s -> m (Step s b)
step1 s
st forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`MC.onException` forall (m :: * -> *). MonadIO m => IOFinalizer -> m ()
runIOFinalizer IOFinalizer
ref
case Step s b
res of
Yield b
x s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
x (s
s, IOFinalizer
ref)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s, IOFinalizer
ref)
Step s b
Stop -> do
forall (m :: * -> *). MonadIO m => IOFinalizer -> m ()
runIOFinalizer IOFinalizer
ref
forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL _bracket #-}
_bracket :: MonadCatch m
=> (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
_bracket :: forall (m :: * -> *) a c d b.
MonadCatch m =>
(a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
_bracket a -> m c
bef c -> m d
aft =
forall (m :: * -> *) a c e d b.
Monad m =>
(a -> m c)
-> (forall s. m s -> m (Either e s))
-> (c -> m d)
-> Unfold m (c, e) b
-> Unfold m c b
-> Unfold m a b
gbracket_ a -> m c
bef forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
MC.try c -> m d
aft (forall (m :: * -> *) a c b.
Applicative m =>
(a -> m c) -> Unfold m a b
nilM (\(c
a, SomeException
e :: MC.SomeException) -> c -> m d
aft c
a forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
MC.throwM SomeException
e))
{-# INLINE_NORMAL bracket_ #-}
bracket_ :: MonadCatch m
=> (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
bracket_ :: forall (m :: * -> *) a c d b.
MonadCatch m =>
(a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
bracket_ a -> m c
bef c -> m d
aft (Unfold s -> m (Step s b)
step1 c -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (s, c) -> m (Step (s, c) b)
step a -> m (s, c)
inject
where
inject :: a -> m (s, c)
inject a
x = do
c
r <- a -> m c
bef a
x
s
s <- c -> m s
inject1 c
r
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, c
r)
{-# INLINE_LATE step #-}
step :: (s, c) -> m (Step (s, c) b)
step (s
st, c
v) = do
Step s b
res <- s -> m (Step s b)
step1 s
st forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`MC.onException` c -> m d
aft c
v
case Step s b
res of
Yield b
x s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
x (s
s, c
v)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s, c
v)
Step s b
Stop -> c -> m d
aft c
v forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL bracket #-}
bracket :: (MonadAsync m, MonadCatch m)
=> (a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
bracket :: forall (m :: * -> *) a c d b.
(MonadAsync m, MonadCatch m) =>
(a -> m c) -> (c -> m d) -> Unfold m c b -> Unfold m a b
bracket a -> m c
bef c -> m d
aft (Unfold s -> m (Step s b)
step1 c -> m s
inject1) = forall (m :: * -> *) a b s.
(s -> m (Step s b)) -> (a -> m s) -> Unfold m a b
Unfold (s, IOFinalizer) -> m (Step (s, IOFinalizer) b)
step a -> m (s, IOFinalizer)
inject
where
inject :: a -> m (s, IOFinalizer)
inject a
x = do
(c
r, IOFinalizer
ref) <- forall (m :: * -> *) b.
MonadRunInIO m =>
((forall a. m a -> IO (StM m a)) -> IO (StM m b)) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO (StM m a)
run -> forall a. IO a -> IO a
mask_ forall a b. (a -> b) -> a -> b
$ forall a. m a -> IO (StM m a)
run forall a b. (a -> b) -> a -> b
$ do
c
r <- a -> m c
bef a
x
IOFinalizer
ref <- forall (m :: * -> *) a. MonadRunInIO m => m a -> m IOFinalizer
newIOFinalizer (c -> m d
aft c
r)
forall (m :: * -> *) a. Monad m => a -> m a
return (c
r, IOFinalizer
ref)
s
s <- c -> m s
inject1 c
r
forall (m :: * -> *) a. Monad m => a -> m a
return (s
s, IOFinalizer
ref)
{-# INLINE_LATE step #-}
step :: (s, IOFinalizer) -> m (Step (s, IOFinalizer) b)
step (s
st, IOFinalizer
ref) = do
Step s b
res <- s -> m (Step s b)
step1 s
st forall (m :: * -> *) a b. MonadCatch m => m a -> m b -> m a
`MC.onException` forall (m :: * -> *). MonadIO m => IOFinalizer -> m ()
runIOFinalizer IOFinalizer
ref
case Step s b
res of
Yield b
x s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield b
x (s
s, IOFinalizer
ref)
Skip s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip (s
s, IOFinalizer
ref)
Step s b
Stop -> do
forall (m :: * -> *). MonadIO m => IOFinalizer -> m ()
runIOFinalizer IOFinalizer
ref
forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL handle #-}
handle :: (MonadCatch m, Exception e)
=> Unfold m e b -> Unfold m a b -> Unfold m a b
handle :: forall (m :: * -> *) e b a.
(MonadCatch m, Exception e) =>
Unfold m e b -> Unfold m a b -> Unfold m a b
handle Unfold m e b
exc =
forall (m :: * -> *) a c e d b.
Monad m =>
(a -> m c)
-> (forall s. m s -> m (Either e s))
-> (c -> m d)
-> Unfold m (c, e) b
-> Unfold m c b
-> Unfold m a b
gbracket_ forall (m :: * -> *) a. Monad m => a -> m a
return forall (m :: * -> *) e a.
(MonadCatch m, Exception e) =>
m a -> m (Either e a)
MC.try (\a
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()) (forall (m :: * -> *) a b c. Unfold m a b -> Unfold m (c, a) b
discardFirst Unfold m e b
exc)