-- SPDX-License-Identifier: GPL-3.0-or-later
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DeriveTraversable #-}
module Zerem
  (
  -- * Purpose
  -- $purpose
  -- * How to write a stream.
  -- $how-to-write-a-stream
    Zerem(..)
  , Step(..)
  -- , mapState
  -- , mapYield
  -- , mapYieldM
  -- , mapYieldF
  -- , mapYieldFM
  , next
  , next_
  , InitZerem(..)
  , initZerem
  , ZipZerem(..)
  , ListZerem(..)
  , Pipe(..)
  -- * Mapping
  , map
  , maps
  , mapM
  , mapsM
  , flatLift
  , flatMapLift
  , (>>^)
  , hoistAny
  -- * Folding
  , runZerem
  , runZerem_
  , map_
  , maps_
  , mapM_
  , mapsM_
  , Zerem.foldr
  , foldr_
  , Zerem.foldrM
  , foldrM_
  , Zerem.foldl
  , foldl_
  , Zerem.foldlM
  , foldlM_
  -- * Scanning
  , Zerem.scanl
  , scanl_
  , scansl
  , scansl_
  -- * Filtering
  , scanMaybel
  , scanMaybel_
  , catMaybes
  , mapMaybe
  , Zerem.concat
  , Zerem.concatMap
  , Zerem.filter
  , Zerem.take
  , Zerem.drop
  -- * Generating
  , yield
  , yieldMany
  , yieldM
  , yieldManyM
  , concatStream
  , mtimes
  ) where

import Prelude hiding (id, (.), map, mapM, mapM_)
import Control.Category
-- import Data.Profunctor
import Control.Applicative
import Data.Functor
import Data.Functor.Identity
import Data.Foldable hiding (mapM_)
import Data.Void
import Data.Bifunctor
import Data.Bitraversable
import Control.Monad hiding (mapM, mapM_)
import Data.Semigroup ( Semigroup(..) )
import Data.List.NonEmpty (NonEmpty(..))
import Control.Exception ( assert )
import Control.Monad.Morph ( MonadTrans(..), MFunctor(..) )
import GHC.Exts ( coerce, Coercible )
import Numeric.Natural ( Natural )
import Control.Monad.Logic.Class ( MonadLogic(..) )
import Control.Comonad ( Comonad(..), ComonadApply(..) )
import Data.Profunctor
import Data.Profunctor.Monad

{-$how-to-write-a-stream
.
-}

( #. ) :: Coercible c b => (b -> c) -> (a -> b) -> (a -> c)
( #. ) b -> c
_ = (b -> b) -> a -> b
forall a b. Coercible a b => a -> b
coerce (\b
x -> b
x :: b) :: forall a b. Coercible b a => a -> b
{-# INLINE ( #. ) #-}

(.#) :: Coercible b a => (b -> c) -> (a -> b) -> (a -> c)
.# :: forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
(.#) b -> c
pbc a -> b
_ = (b -> c) -> a -> c
forall a b. Coercible a b => a -> b
coerce b -> c
pbc
{-# INLINE (.#) #-}

infixr 9 #.
infixl 8 .#

data Zerem f m r = forall s. Zerem (s -> m (Step s f r)) (m s)
{-^
The stream datatype. It consists of a state machine, and an action
that yields the initial state.
-}

data Step s f r
  = Done r -- ^ Stream is over and @r@ is the result.
  | Skip s -- ^ Stream is transitioning state without yielding.
  | Yield !(f s) -- ^ Stream yields a functor. Usually @(a, s)@.
  deriving ((forall a b. (a -> b) -> Step s f a -> Step s f b)
-> (forall a b. a -> Step s f b -> Step s f a)
-> Functor (Step s f)
forall a b. a -> Step s f b -> Step s f a
forall a b. (a -> b) -> Step s f a -> Step s f b
forall s (f :: * -> *) a b. a -> Step s f b -> Step s f a
forall s (f :: * -> *) a b. (a -> b) -> Step s f a -> Step s f b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall s (f :: * -> *) a b. (a -> b) -> Step s f a -> Step s f b
fmap :: forall a b. (a -> b) -> Step s f a -> Step s f b
$c<$ :: forall s (f :: * -> *) a b. a -> Step s f b -> Step s f a
<$ :: forall a b. a -> Step s f b -> Step s f a
Functor, (forall m. Monoid m => Step s f m -> m)
-> (forall m a. Monoid m => (a -> m) -> Step s f a -> m)
-> (forall m a. Monoid m => (a -> m) -> Step s f a -> m)
-> (forall a b. (a -> b -> b) -> b -> Step s f a -> b)
-> (forall a b. (a -> b -> b) -> b -> Step s f a -> b)
-> (forall b a. (b -> a -> b) -> b -> Step s f a -> b)
-> (forall b a. (b -> a -> b) -> b -> Step s f a -> b)
-> (forall a. (a -> a -> a) -> Step s f a -> a)
-> (forall a. (a -> a -> a) -> Step s f a -> a)
-> (forall a. Step s f a -> [a])
-> (forall a. Step s f a -> Bool)
-> (forall a. Step s f a -> Int)
-> (forall a. Eq a => a -> Step s f a -> Bool)
-> (forall a. Ord a => Step s f a -> a)
-> (forall a. Ord a => Step s f a -> a)
-> (forall a. Num a => Step s f a -> a)
-> (forall a. Num a => Step s f a -> a)
-> Foldable (Step s f)
forall a. Eq a => a -> Step s f a -> Bool
forall a. Num a => Step s f a -> a
forall a. Ord a => Step s f a -> a
forall m. Monoid m => Step s f m -> m
forall a. Step s f a -> Bool
forall a. Step s f a -> Int
forall a. Step s f a -> [a]
forall a. (a -> a -> a) -> Step s f a -> a
forall m a. Monoid m => (a -> m) -> Step s f a -> m
forall b a. (b -> a -> b) -> b -> Step s f a -> b
forall a b. (a -> b -> b) -> b -> Step s f a -> b
forall s (f :: * -> *) a. Eq a => a -> Step s f a -> Bool
forall s (f :: * -> *) a. Num a => Step s f a -> a
forall s (f :: * -> *) a. Ord a => Step s f a -> a
forall s (f :: * -> *) m. Monoid m => Step s f m -> m
forall s (f :: * -> *) a. Step s f a -> Bool
forall s (f :: * -> *) a. Step s f a -> Int
forall s (f :: * -> *) a. Step s f a -> [a]
forall s (f :: * -> *) a. (a -> a -> a) -> Step s f a -> a
forall s (f :: * -> *) m a. Monoid m => (a -> m) -> Step s f a -> m
forall s (f :: * -> *) b a. (b -> a -> b) -> b -> Step s f a -> b
forall s (f :: * -> *) a b. (a -> b -> b) -> b -> Step s f a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall s (f :: * -> *) m. Monoid m => Step s f m -> m
fold :: forall m. Monoid m => Step s f m -> m
$cfoldMap :: forall s (f :: * -> *) m a. Monoid m => (a -> m) -> Step s f a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Step s f a -> m
$cfoldMap' :: forall s (f :: * -> *) m a. Monoid m => (a -> m) -> Step s f a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Step s f a -> m
$cfoldr :: forall s (f :: * -> *) a b. (a -> b -> b) -> b -> Step s f a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Step s f a -> b
$cfoldr' :: forall s (f :: * -> *) a b. (a -> b -> b) -> b -> Step s f a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Step s f a -> b
$cfoldl :: forall s (f :: * -> *) b a. (b -> a -> b) -> b -> Step s f a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Step s f a -> b
$cfoldl' :: forall s (f :: * -> *) b a. (b -> a -> b) -> b -> Step s f a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Step s f a -> b
$cfoldr1 :: forall s (f :: * -> *) a. (a -> a -> a) -> Step s f a -> a
foldr1 :: forall a. (a -> a -> a) -> Step s f a -> a
$cfoldl1 :: forall s (f :: * -> *) a. (a -> a -> a) -> Step s f a -> a
foldl1 :: forall a. (a -> a -> a) -> Step s f a -> a
$ctoList :: forall s (f :: * -> *) a. Step s f a -> [a]
toList :: forall a. Step s f a -> [a]
$cnull :: forall s (f :: * -> *) a. Step s f a -> Bool
null :: forall a. Step s f a -> Bool
$clength :: forall s (f :: * -> *) a. Step s f a -> Int
length :: forall a. Step s f a -> Int
$celem :: forall s (f :: * -> *) a. Eq a => a -> Step s f a -> Bool
elem :: forall a. Eq a => a -> Step s f a -> Bool
$cmaximum :: forall s (f :: * -> *) a. Ord a => Step s f a -> a
maximum :: forall a. Ord a => Step s f a -> a
$cminimum :: forall s (f :: * -> *) a. Ord a => Step s f a -> a
minimum :: forall a. Ord a => Step s f a -> a
$csum :: forall s (f :: * -> *) a. Num a => Step s f a -> a
sum :: forall a. Num a => Step s f a -> a
$cproduct :: forall s (f :: * -> *) a. Num a => Step s f a -> a
product :: forall a. Num a => Step s f a -> a
Foldable, Functor (Step s f)
Foldable (Step s f)
Functor (Step s f)
-> Foldable (Step s f)
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Step s f a -> f (Step s f b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Step s f (f a) -> f (Step s f a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Step s f a -> m (Step s f b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Step s f (m a) -> m (Step s f a))
-> Traversable (Step s f)
forall s (f :: * -> *). Functor (Step s f)
forall s (f :: * -> *). Foldable (Step s f)
forall s (f :: * -> *) (m :: * -> *) a.
Monad m =>
Step s f (m a) -> m (Step s f a)
forall s (f :: * -> *) (f :: * -> *) a.
Applicative f =>
Step s f (f a) -> f (Step s f a)
forall s (f :: * -> *) (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Step s f a -> m (Step s f b)
forall s (f :: * -> *) (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Step s f a -> f (Step s f b)
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Step s f (m a) -> m (Step s f a)
forall (f :: * -> *) a.
Applicative f =>
Step s f (f a) -> f (Step s f a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Step s f a -> m (Step s f b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Step s f a -> f (Step s f b)
$ctraverse :: forall s (f :: * -> *) (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Step s f a -> f (Step s f b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Step s f a -> f (Step s f b)
$csequenceA :: forall s (f :: * -> *) (f :: * -> *) a.
Applicative f =>
Step s f (f a) -> f (Step s f a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Step s f (f a) -> f (Step s f a)
$cmapM :: forall s (f :: * -> *) (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Step s f a -> m (Step s f b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Step s f a -> m (Step s f b)
$csequence :: forall s (f :: * -> *) (m :: * -> *) a.
Monad m =>
Step s f (m a) -> m (Step s f a)
sequence :: forall (m :: * -> *) a. Monad m => Step s f (m a) -> m (Step s f a)
Traversable)

type ZeremOf a = Zerem ((,) a)
type Stepper s f m u = s -> m (Step s f u)

mapState :: Functor f => (s -> s') -> Step s f u -> Step s' f u
mapState :: forall (f :: * -> *) s s' u.
Functor f =>
(s -> s') -> Step s f u -> Step s' f u
mapState s -> s'
f = \case
  Done u
u -> u -> Step s' f u
forall s (f :: * -> *) r. r -> Step s f r
Done u
u
  Skip s
s -> s' -> Step s' f u
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> s'
f s
s)
  Yield f s
fs -> f s' -> Step s' f u
forall s (f :: * -> *) r. f s -> Step s f r
Yield (s -> s'
f (s -> s') -> f s -> f s'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f s
fs)
{-# INLINE mapState #-}

mapYield :: (f s -> g s) -> Step s f u -> Step s g u
mapYield :: forall (f :: * -> *) s (g :: * -> *) u.
(f s -> g s) -> Step s f u -> Step s g u
mapYield f s -> g s
phi = \case
  Done u
u -> u -> Step s g u
forall s (f :: * -> *) r. r -> Step s f r
Done u
u
  Skip s
s -> s -> Step s g u
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s
  Yield f s
fs -> g s -> Step s g u
forall s (f :: * -> *) r. f s -> Step s f r
Yield (g s -> Step s g u) -> g s -> Step s g u
forall a b. (a -> b) -> a -> b
$ f s -> g s
phi f s
fs
{-# INLINE mapYield #-}

mapYieldM :: Monad m => (f s -> m (g s)) -> Step s f u -> m (Step s g u)
mapYieldM :: forall (m :: * -> *) (f :: * -> *) s (g :: * -> *) u.
Monad m =>
(f s -> m (g s)) -> Step s f u -> m (Step s g u)
mapYieldM f s -> m (g s)
phi = \case
  Done u
u -> Step s g u -> m (Step s g u)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step s g u -> m (Step s g u)) -> Step s g u -> m (Step s g u)
forall a b. (a -> b) -> a -> b
$ u -> Step s g u
forall s (f :: * -> *) r. r -> Step s f r
Done u
u
  Skip s
s -> Step s g u -> m (Step s g u)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step s g u -> m (Step s g u)) -> Step s g u -> m (Step s g u)
forall a b. (a -> b) -> a -> b
$ s -> Step s g u
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s
  Yield f s
fs -> g s -> Step s g u
forall s (f :: * -> *) r. f s -> Step s f r
Yield (g s -> Step s g u) -> m (g s) -> m (Step s g u)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f s -> m (g s)
phi f s
fs
{-# INLINE mapYieldM #-}

mapYieldF :: (forall x. f x -> g x) -> Step s f u -> Step s g u
mapYieldF :: forall (f :: * -> *) (g :: * -> *) s u.
(forall x. f x -> g x) -> Step s f u -> Step s g u
mapYieldF forall x. f x -> g x
phi = \case
  Done u
u -> u -> Step s g u
forall s (f :: * -> *) r. r -> Step s f r
Done u
u
  Skip s
s -> s -> Step s g u
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s
  Yield f s
fs -> g s -> Step s g u
forall s (f :: * -> *) r. f s -> Step s f r
Yield (g s -> Step s g u) -> g s -> Step s g u
forall a b. (a -> b) -> a -> b
$ f s -> g s
forall x. f x -> g x
phi f s
fs
{-# INLINE mapYieldF #-}

mapYieldFM :: Monad m => (forall x. f x -> m (g x)) -> Step s f u -> m (Step s g u)
mapYieldFM :: forall (m :: * -> *) (f :: * -> *) (g :: * -> *) s u.
Monad m =>
(forall x. f x -> m (g x)) -> Step s f u -> m (Step s g u)
mapYieldFM forall x. f x -> m (g x)
phi = \case
  Done u
u -> Step s g u -> m (Step s g u)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step s g u -> m (Step s g u)) -> Step s g u -> m (Step s g u)
forall a b. (a -> b) -> a -> b
$ u -> Step s g u
forall s (f :: * -> *) r. r -> Step s f r
Done u
u
  Skip s
s -> Step s g u -> m (Step s g u)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step s g u -> m (Step s g u)) -> Step s g u -> m (Step s g u)
forall a b. (a -> b) -> a -> b
$ s -> Step s g u
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s
  Yield f s
fs -> g s -> Step s g u
forall s (f :: * -> *) r. f s -> Step s f r
Yield (g s -> Step s g u) -> m (g s) -> m (Step s g u)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f s -> m (g s)
forall x. f x -> m (g x)
phi f s
fs
{-# INLINE mapYieldFM #-}

next :: (Functor f, Monad m) => Zerem f m r -> m (Either r (f (Zerem f m r)))
next :: forall (f :: * -> *) (m :: * -> *) r.
(Functor f, Monad m) =>
Zerem f m r -> m (Either r (f (Zerem f m r)))
next (Zerem s -> m (Step s f r)
next m s
s) = s -> m (Either r (f (Zerem f m r)))
loop (s -> m (Either r (f (Zerem f m r))))
-> m s -> m (Either r (f (Zerem f m r)))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
  loop :: s -> m (Either r (f (Zerem f m r)))
loop s
s =
    s -> m (Step s f r)
next s
s m (Step s f r)
-> (Step s f r -> m (Either r (f (Zerem f m r))))
-> m (Either r (f (Zerem f m r)))
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
r -> Either r (f (Zerem f m r)) -> m (Either r (f (Zerem f m r)))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either r (f (Zerem f m r)) -> m (Either r (f (Zerem f m r))))
-> Either r (f (Zerem f m r)) -> m (Either r (f (Zerem f m r)))
forall a b. (a -> b) -> a -> b
$ r -> Either r (f (Zerem f m r))
forall a b. a -> Either a b
Left r
r
      Skip s
s' -> s -> m (Either r (f (Zerem f m r)))
loop s
s'
      Yield f s
fs' -> Either r (f (Zerem f m r)) -> m (Either r (f (Zerem f m r)))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either r (f (Zerem f m r)) -> m (Either r (f (Zerem f m r))))
-> (f (Zerem f m r) -> Either r (f (Zerem f m r)))
-> f (Zerem f m r)
-> m (Either r (f (Zerem f m r)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f (Zerem f m r) -> Either r (f (Zerem f m r))
forall a b. b -> Either a b
Right (f (Zerem f m r) -> m (Either r (f (Zerem f m r))))
-> f (Zerem f m r) -> m (Either r (f (Zerem f m r)))
forall a b. (a -> b) -> a -> b
$ (s -> Zerem f m r) -> f s -> f (Zerem f m r)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((s -> m (Step s f r)) -> m s -> Zerem f m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem s -> m (Step s f r)
next (m s -> Zerem f m r) -> (s -> m s) -> s -> Zerem f m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> m s
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure) f s
fs'

next_ :: (Functor f, Monad m) => Zerem f m r -> m (Maybe (f (Zerem f m r)))
next_ :: forall (f :: * -> *) (m :: * -> *) r.
(Functor f, Monad m) =>
Zerem f m r -> m (Maybe (f (Zerem f m r)))
next_ (Zerem s -> m (Step s f r)
next m s
s) = s -> m (Maybe (f (Zerem f m r)))
loop (s -> m (Maybe (f (Zerem f m r))))
-> m s -> m (Maybe (f (Zerem f m r)))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
  loop :: s -> m (Maybe (f (Zerem f m r)))
loop s
s =
    s -> m (Step s f r)
next s
s m (Step s f r)
-> (Step s f r -> m (Maybe (f (Zerem f m r))))
-> m (Maybe (f (Zerem f m r)))
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
_ -> Maybe (f (Zerem f m r)) -> m (Maybe (f (Zerem f m r)))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (f (Zerem f m r))
forall a. Maybe a
Nothing
      Skip s
s' -> s -> m (Maybe (f (Zerem f m r)))
loop s
s'
      Yield f s
fs' -> Maybe (f (Zerem f m r)) -> m (Maybe (f (Zerem f m r)))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (f (Zerem f m r)) -> m (Maybe (f (Zerem f m r))))
-> (f (Zerem f m r) -> Maybe (f (Zerem f m r)))
-> f (Zerem f m r)
-> m (Maybe (f (Zerem f m r)))
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f (Zerem f m r) -> Maybe (f (Zerem f m r))
forall a. a -> Maybe a
Just (f (Zerem f m r) -> m (Maybe (f (Zerem f m r))))
-> f (Zerem f m r) -> m (Maybe (f (Zerem f m r)))
forall a b. (a -> b) -> a -> b
$ (s -> Zerem f m r) -> f s -> f (Zerem f m r)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((s -> m (Step s f r)) -> m s -> Zerem f m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem s -> m (Step s f r)
next (m s -> Zerem f m r) -> (s -> m s) -> s -> Zerem f m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> m s
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure) f s
fs'

deriving instance Functor m => Functor (Zerem f m)

{-|
Not lazy on its arguments. If you use something like:

@
as = yield 'a' *> as
@

the program will go into an infinite loop.

For that kind of construction, consider using functions
from the `Generating` section, or simply writing a stream
by hand:

@
as = Zerem next (pure a) where
  next a = pure $ Z.Yield (a, a)
@
-}
instance (Functor f, Monad m) => Applicative (Zerem f m) where
  pure :: forall a. a -> Zerem f m a
pure a
r = (a -> m (Step a f a)) -> m a -> Zerem f m a
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Step a f a -> m (Step a f a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step a f a -> m (Step a f a))
-> (a -> Step a f a) -> a -> m (Step a f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Step a f a
forall s (f :: * -> *) r. r -> Step s f r
Done) (a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
r)

  (Zerem s -> m (Step s f (a -> b))
nextf m s
sf) <*> :: forall a b. Zerem f m (a -> b) -> Zerem f m a -> Zerem f m b
<*> (Zerem s -> m (Step s f a)
nexta m s
sa) = (Either s (a -> b, s) -> m (Step (Either s (a -> b, s)) f b))
-> m (Either s (a -> b, s)) -> Zerem f m b
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either s (a -> b, s) -> m (Step (Either s (a -> b, s)) f b)
next' (s -> Either s (a -> b, s)
forall a b. a -> Either a b
Left (s -> Either s (a -> b, s)) -> m s -> m (Either s (a -> b, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
sf) where
    next' :: Either s (a -> b, s) -> m (Step (Either s (a -> b, s)) f b)
next' (Left s
s) =
      s -> m (Step s f (a -> b))
nextf s
s m (Step s f (a -> b))
-> (Step s f (a -> b) -> m (Step (Either s (a -> b, s)) f b))
-> m (Step (Either s (a -> b, s)) f b)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done a -> b
f -> m s
sa m s
-> (s -> Step (Either s (a -> b, s)) f b)
-> m (Step (Either s (a -> b, s)) f b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sa -> Either s (a -> b, s) -> Step (Either s (a -> b, s)) f b
forall s (f :: * -> *) r. s -> Step s f r
Skip ((a -> b, s) -> Either s (a -> b, s)
forall a b. b -> Either a b
Right (a -> b
f, s
sa))
        Skip s
s' -> Step (Either s (a -> b, s)) f b
-> m (Step (Either s (a -> b, s)) f b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a -> b, s)) f b
 -> m (Step (Either s (a -> b, s)) f b))
-> Step (Either s (a -> b, s)) f b
-> m (Step (Either s (a -> b, s)) f b)
forall a b. (a -> b) -> a -> b
$ Either s (a -> b, s) -> Step (Either s (a -> b, s)) f b
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Either s (a -> b, s)
forall a b. a -> Either a b
Left s
s')
        Yield f s
fs' -> Step (Either s (a -> b, s)) f b
-> m (Step (Either s (a -> b, s)) f b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a -> b, s)) f b
 -> m (Step (Either s (a -> b, s)) f b))
-> Step (Either s (a -> b, s)) f b
-> m (Step (Either s (a -> b, s)) f b)
forall a b. (a -> b) -> a -> b
$ f (Either s (a -> b, s)) -> Step (Either s (a -> b, s)) f b
forall s (f :: * -> *) r. f s -> Step s f r
Yield ((s -> Either s (a -> b, s)) -> f s -> f (Either s (a -> b, s))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap s -> Either s (a -> b, s)
forall a b. a -> Either a b
Left f s
fs')
    next' (Right (a -> b
f, s
s)) =
      s -> m (Step s f a)
nexta s
s m (Step s f a)
-> (Step s f a -> Step (Either s (a -> b, s)) f b)
-> m (Step (Either s (a -> b, s)) f b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done a
a -> b -> Step (Either s (a -> b, s)) f b
forall s (f :: * -> *) r. r -> Step s f r
Done (b -> Step (Either s (a -> b, s)) f b)
-> b -> Step (Either s (a -> b, s)) f b
forall a b. (a -> b) -> a -> b
$ a -> b
f a
a
        Skip s
s' -> Either s (a -> b, s) -> Step (Either s (a -> b, s)) f b
forall s (f :: * -> *) r. s -> Step s f r
Skip ((a -> b, s) -> Either s (a -> b, s)
forall a b. b -> Either a b
Right (a -> b
f, s
s'))
        Yield f s
fs' -> f (Either s (a -> b, s)) -> Step (Either s (a -> b, s)) f b
forall s (f :: * -> *) r. f s -> Step s f r
Yield ((s -> Either s (a -> b, s)) -> f s -> f (Either s (a -> b, s))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b, s) -> Either s (a -> b, s)
forall a b. b -> Either a b
Right ((a -> b, s) -> Either s (a -> b, s))
-> (s -> (a -> b, s)) -> s -> Either s (a -> b, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a -> b
f,)) f s
fs')

  liftA2 :: forall a b c.
(a -> b -> c) -> Zerem f m a -> Zerem f m b -> Zerem f m c
liftA2 a -> b -> c
f (Zerem s -> m (Step s f a)
nexta m s
sa) (Zerem s -> m (Step s f b)
nextb m s
sb) = (Either s (a, s) -> m (Step (Either s (a, s)) f c))
-> m (Either s (a, s)) -> Zerem f m c
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either s (a, s) -> m (Step (Either s (a, s)) f c)
next' (s -> Either s (a, s)
forall a b. a -> Either a b
Left (s -> Either s (a, s)) -> m s -> m (Either s (a, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
sa) where
    next' :: Either s (a, s) -> m (Step (Either s (a, s)) f c)
next' (Left s
s) =
      s -> m (Step s f a)
nexta s
s m (Step s f a)
-> (Step s f a -> m (Step (Either s (a, s)) f c))
-> m (Step (Either s (a, s)) f c)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done a
a -> m s
sb m s
-> (s -> Step (Either s (a, s)) f c)
-> m (Step (Either s (a, s)) f c)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sb -> Either s (a, s) -> Step (Either s (a, s)) f c
forall s (f :: * -> *) r. s -> Step s f r
Skip ((a, s) -> Either s (a, s)
forall a b. b -> Either a b
Right (a
a, s
sb))
        Skip s
s' -> Step (Either s (a, s)) f c -> m (Step (Either s (a, s)) f c)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a, s)) f c -> m (Step (Either s (a, s)) f c))
-> Step (Either s (a, s)) f c -> m (Step (Either s (a, s)) f c)
forall a b. (a -> b) -> a -> b
$ Either s (a, s) -> Step (Either s (a, s)) f c
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Either s (a, s)
forall a b. a -> Either a b
Left s
s')
        Yield f s
fs' -> Step (Either s (a, s)) f c -> m (Step (Either s (a, s)) f c)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a, s)) f c -> m (Step (Either s (a, s)) f c))
-> Step (Either s (a, s)) f c -> m (Step (Either s (a, s)) f c)
forall a b. (a -> b) -> a -> b
$ f (Either s (a, s)) -> Step (Either s (a, s)) f c
forall s (f :: * -> *) r. f s -> Step s f r
Yield ((s -> Either s (a, s)) -> f s -> f (Either s (a, s))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap s -> Either s (a, s)
forall a b. a -> Either a b
Left f s
fs')
    next' (Right (a
a, s
s)) =
      s -> m (Step s f b)
nextb s
s m (Step s f b)
-> (Step s f b -> Step (Either s (a, s)) f c)
-> m (Step (Either s (a, s)) f c)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done b
b -> c -> Step (Either s (a, s)) f c
forall s (f :: * -> *) r. r -> Step s f r
Done (a -> b -> c
f a
a b
b)
        Skip s
s' -> Either s (a, s) -> Step (Either s (a, s)) f c
forall s (f :: * -> *) r. s -> Step s f r
Skip ((a, s) -> Either s (a, s)
forall a b. b -> Either a b
Right (a
a, s
s'))
        Yield f s
fs' -> f (Either s (a, s)) -> Step (Either s (a, s)) f c
forall s (f :: * -> *) r. f s -> Step s f r
Yield ((s -> Either s (a, s)) -> f s -> f (Either s (a, s))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a, s) -> Either s (a, s)
forall a b. b -> Either a b
Right ((a, s) -> Either s (a, s))
-> (s -> (a, s)) -> s -> Either s (a, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a
a,)) f s
fs')

  (Zerem s -> m (Step s f a)
nextf m s
sf) *> :: forall a b. Zerem f m a -> Zerem f m b -> Zerem f m b
*> (Zerem s -> m (Step s f b)
nexta m s
sa) = (Either s s -> m (Step (Either s s) f b))
-> m (Either s s) -> Zerem f m b
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either s s -> m (Step (Either s s) f b)
next' (s -> Either s s
forall a b. a -> Either a b
Left (s -> Either s s) -> m s -> m (Either s s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
sf) where
    next' :: Either s s -> m (Step (Either s s) f b)
next' (Left s
sf) =
      s -> m (Step s f a)
nextf s
sf m (Step s f a)
-> (Step s f a -> m (Step (Either s s) f b))
-> m (Step (Either s s) f b)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done a
_ -> m s
sa m s -> (s -> Step (Either s s) f b) -> m (Step (Either s s) f b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sa -> Either s s -> Step (Either s s) f b
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Either s s
forall a b. b -> Either a b
Right s
sa)
        Skip s
s' -> Step (Either s s) f b -> m (Step (Either s s) f b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s s) f b -> m (Step (Either s s) f b))
-> Step (Either s s) f b -> m (Step (Either s s) f b)
forall a b. (a -> b) -> a -> b
$ Either s s -> Step (Either s s) f b
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Either s s
forall a b. a -> Either a b
Left s
s')
        Yield f s
fs' -> Step (Either s s) f b -> m (Step (Either s s) f b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s s) f b -> m (Step (Either s s) f b))
-> Step (Either s s) f b -> m (Step (Either s s) f b)
forall a b. (a -> b) -> a -> b
$ f (Either s s) -> Step (Either s s) f b
forall s (f :: * -> *) r. f s -> Step s f r
Yield ((s -> Either s s) -> f s -> f (Either s s)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap s -> Either s s
forall a b. a -> Either a b
Left f s
fs')
    next' (Right s
sa) =
      s -> m (Step s f b)
nexta s
sa m (Step s f b)
-> (Step s f b -> Step (Either s s) f b)
-> m (Step (Either s s) f b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done b
u -> b -> Step (Either s s) f b
forall s (f :: * -> *) r. r -> Step s f r
Done b
u
        Skip s
s' -> Either s s -> Step (Either s s) f b
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Either s s
forall a b. b -> Either a b
Right s
s')
        Yield f s
fs' -> f (Either s s) -> Step (Either s s) f b
forall s (f :: * -> *) r. f s -> Step s f r
Yield ((s -> Either s s) -> f s -> f (Either s s)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap s -> Either s s
forall a b. b -> Either a b
Right f s
fs')

  {-# INLINE pure #-}
  {-# INLINE (<*>) #-}
  {-# INLINE liftA2 #-}

data InitZerem f m r = forall s. InitZerem (s -> m (Step s f r)) s
-- deriving instance Functor m => Functor (InitZerem f m)

initZerem :: Monad m => Zerem f m r -> m (InitZerem f m r)
initZerem :: forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m r -> m (InitZerem f m r)
initZerem (Zerem s -> m (Step s f r)
next m s
ms) = m s
ms m s -> (s -> InitZerem f m r) -> m (InitZerem f m r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
s -> (s -> m (Step s f r)) -> s -> InitZerem f m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> m (Step s f r)
next s
s

instance (Functor f, Monad m) => Monad (Zerem f m) where
  (Zerem s -> m (Step s f a)
nexta m s
sa) >>= :: forall a b. Zerem f m a -> (a -> Zerem f m b) -> Zerem f m b
>>= a -> Zerem f m b
k = (Either s (InitZerem f m b)
 -> m (Step (Either s (InitZerem f m b)) f b))
-> m (Either s (InitZerem f m b)) -> Zerem f m b
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either s (InitZerem f m b)
-> m (Step (Either s (InitZerem f m b)) f b)
next' (s -> Either s (InitZerem f m b)
forall a b. a -> Either a b
Left (s -> Either s (InitZerem f m b))
-> m s -> m (Either s (InitZerem f m b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
sa) where
    next' :: Either s (InitZerem f m b)
-> m (Step (Either s (InitZerem f m b)) f b)
next' (Left s
s) =
      s -> m (Step s f a)
nexta s
s m (Step s f a)
-> (Step s f a -> m (Step (Either s (InitZerem f m b)) f b))
-> m (Step (Either s (InitZerem f m b)) f b)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        -- Bye bye fusion ☹️
        Done a
a -> Either s (InitZerem f m b) -> Step (Either s (InitZerem f m b)) f b
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (InitZerem f m b)
 -> Step (Either s (InitZerem f m b)) f b)
-> (InitZerem f m b -> Either s (InitZerem f m b))
-> InitZerem f m b
-> Step (Either s (InitZerem f m b)) f b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. InitZerem f m b -> Either s (InitZerem f m b)
forall a b. b -> Either a b
Right (InitZerem f m b -> Step (Either s (InitZerem f m b)) f b)
-> m (InitZerem f m b) -> m (Step (Either s (InitZerem f m b)) f b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Zerem f m b -> m (InitZerem f m b)
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m r -> m (InitZerem f m r)
initZerem (a -> Zerem f m b
k a
a)
        Skip s
s' -> Step (Either s (InitZerem f m b)) f b
-> m (Step (Either s (InitZerem f m b)) f b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (InitZerem f m b)) f b
 -> m (Step (Either s (InitZerem f m b)) f b))
-> Step (Either s (InitZerem f m b)) f b
-> m (Step (Either s (InitZerem f m b)) f b)
forall a b. (a -> b) -> a -> b
$ Either s (InitZerem f m b) -> Step (Either s (InitZerem f m b)) f b
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Either s (InitZerem f m b)
forall a b. a -> Either a b
Left s
s')
        Yield f s
fs' -> Step (Either s (InitZerem f m b)) f b
-> m (Step (Either s (InitZerem f m b)) f b)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (InitZerem f m b)) f b
 -> m (Step (Either s (InitZerem f m b)) f b))
-> Step (Either s (InitZerem f m b)) f b
-> m (Step (Either s (InitZerem f m b)) f b)
forall a b. (a -> b) -> a -> b
$ f (Either s (InitZerem f m b))
-> Step (Either s (InitZerem f m b)) f b
forall s (f :: * -> *) r. f s -> Step s f r
Yield ((s -> Either s (InitZerem f m b))
-> f s -> f (Either s (InitZerem f m b))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap s -> Either s (InitZerem f m b)
forall a b. a -> Either a b
Left f s
fs')
    next' (Right (InitZerem s -> m (Step s f b)
next s
s)) =
      s -> m (Step s f b)
next s
s m (Step s f b)
-> (Step s f b -> Step (Either s (InitZerem f m b)) f b)
-> m (Step (Either s (InitZerem f m b)) f b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done b
r -> b -> Step (Either s (InitZerem f m b)) f b
forall s (f :: * -> *) r. r -> Step s f r
Done b
r
        Skip s
s' -> Either s (InitZerem f m b) -> Step (Either s (InitZerem f m b)) f b
forall s (f :: * -> *) r. s -> Step s f r
Skip (InitZerem f m b -> Either s (InitZerem f m b)
forall a b. b -> Either a b
Right (InitZerem f m b -> Either s (InitZerem f m b))
-> InitZerem f m b -> Either s (InitZerem f m b)
forall a b. (a -> b) -> a -> b
$ (s -> m (Step s f b)) -> s -> InitZerem f m b
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> m (Step s f b)
next s
s')
        Yield f s
fs' -> f (Either s (InitZerem f m b))
-> Step (Either s (InitZerem f m b)) f b
forall s (f :: * -> *) r. f s -> Step s f r
Yield (f (Either s (InitZerem f m b))
 -> Step (Either s (InitZerem f m b)) f b)
-> f (Either s (InitZerem f m b))
-> Step (Either s (InitZerem f m b)) f b
forall a b. (a -> b) -> a -> b
$ (s -> Either s (InitZerem f m b))
-> f s -> f (Either s (InitZerem f m b))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (InitZerem f m b -> Either s (InitZerem f m b)
forall a b. b -> Either a b
Right (InitZerem f m b -> Either s (InitZerem f m b))
-> (s -> InitZerem f m b) -> s -> Either s (InitZerem f m b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (s -> m (Step s f b)) -> s -> InitZerem f m b
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> m (Step s f b)
next) f s
fs'

  >> :: forall a b. Zerem f m a -> Zerem f m b -> Zerem f m b
(>>) = Zerem f m a -> Zerem f m b -> Zerem f m b
forall a b. Zerem f m a -> Zerem f m b -> Zerem f m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)

  {-# INLINE (>>=) #-}

instance MonadTrans (Zerem a) where
  lift :: forall (m :: * -> *) a. Monad m => m a -> Zerem a m a
lift = (a -> m (Step a a a)) -> m a -> Zerem a m a
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Step a a a -> m (Step a a a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step a a a -> m (Step a a a))
-> (a -> Step a a a) -> a -> m (Step a a a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Step a a a
forall s (f :: * -> *) r. r -> Step s f r
Done)

zliftA :: Applicative m => m r -> Zerem a m r
zliftA :: forall (m :: * -> *) r (a :: * -> *).
Applicative m =>
m r -> Zerem a m r
zliftA = (r -> m (Step r a r)) -> m r -> Zerem a m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Step r a r -> m (Step r a r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step r a r -> m (Step r a r))
-> (r -> Step r a r) -> r -> m (Step r a r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. r -> Step r a r
forall s (f :: * -> *) r. r -> Step s f r
Done)

instance (Functor f, Monad m) => Semigroup (Zerem f m r) where
  (Zerem s -> m (Step s f r)
nextl m s
sl) <> :: Zerem f m r -> Zerem f m r -> Zerem f m r
<> (Zerem s -> m (Step s f r)
nextr m s
sr) = (Either (s, s) s -> m (Step (Either (s, s) s) f r))
-> m (Either (s, s) s) -> Zerem f m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (s, s) s -> m (Step (Either (s, s) s) f r)
next' ((s -> s -> Either (s, s) s) -> m s -> m s -> m (Either (s, s) s)
forall a b c. (a -> b -> c) -> m a -> m b -> m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (((s, s) -> Either (s, s) s) -> s -> s -> Either (s, s) s
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (s, s) -> Either (s, s) s
forall a b. a -> Either a b
Left) m s
sl m s
sr) where
    next' :: Either (s, s) s -> m (Step (Either (s, s) s) f r)
next' (Left (s
sl, s
sr)) =
      s -> m (Step s f r)
nextl s
sl m (Step s f r)
-> (Step s f r -> Step (Either (s, s) s) f r)
-> m (Step (Either (s, s) s) f r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done r
_ -> Either (s, s) s -> Step (Either (s, s) s) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Either (s, s) s
forall a b. b -> Either a b
Right s
sr)
        Skip s
s' -> Either (s, s) s -> Step (Either (s, s) s) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip ((s, s) -> Either (s, s) s
forall a b. a -> Either a b
Left (s
s', s
sr))
        Yield f s
fs' -> f (Either (s, s) s) -> Step (Either (s, s) s) f r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (f (Either (s, s) s) -> Step (Either (s, s) s) f r)
-> f (Either (s, s) s) -> Step (Either (s, s) s) f r
forall a b. (a -> b) -> a -> b
$ (s -> Either (s, s) s) -> f s -> f (Either (s, s) s)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\s
s' -> (s, s) -> Either (s, s) s
forall a b. a -> Either a b
Left (s
s', s
sr)) f s
fs'
    next' (Right s
sr) =
      s -> m (Step s f r)
nextr s
sr m (Step s f r)
-> (Step s f r -> Step (Either (s, s) s) f r)
-> m (Step (Either (s, s) s) f r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done r
u -> r -> Step (Either (s, s) s) f r
forall s (f :: * -> *) r. r -> Step s f r
Done r
u
        Skip s
s' -> Either (s, s) s -> Step (Either (s, s) s) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Either (s, s) s
forall a b. b -> Either a b
Right s
s')
        Yield f s
fs' -> f (Either (s, s) s) -> Step (Either (s, s) s) f r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (f (Either (s, s) s) -> Step (Either (s, s) s) f r)
-> f (Either (s, s) s) -> Step (Either (s, s) s) f r
forall a b. (a -> b) -> a -> b
$ (s -> Either (s, s) s) -> f s -> f (Either (s, s) s)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap s -> Either (s, s) s
forall a b. b -> Either a b
Right f s
fs'

  sconcat :: NonEmpty (Zerem f m r) -> Zerem f m r
sconcat NonEmpty (Zerem f m r)
nonempty = (Either (NonEmpty (Zerem f m r)) (InitZerem f m r, [Zerem f m r])
 -> m (Step
         (Either (NonEmpty (Zerem f m r)) (InitZerem f m r, [Zerem f m r]))
         f
         r))
-> m (Either
        (NonEmpty (Zerem f m r)) (InitZerem f m r, [Zerem f m r]))
-> Zerem f m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (NonEmpty (Zerem f m r)) (InitZerem f m r, [Zerem f m r])
-> m (Step
        (Either (NonEmpty (Zerem f m r)) (InitZerem f m r, [Zerem f m r]))
        f
        r)
forall {f :: * -> *} {f :: * -> *} {r} {a}.
(Monad f, Functor f) =>
Either (NonEmpty (Zerem f f r)) (InitZerem f f r, [Zerem f f r])
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
next' (Either (NonEmpty (Zerem f m r)) (InitZerem f m r, [Zerem f m r])
-> m (Either
        (NonEmpty (Zerem f m r)) (InitZerem f m r, [Zerem f m r]))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NonEmpty (Zerem f m r)
-> Either (NonEmpty (Zerem f m r)) (InitZerem f m r, [Zerem f m r])
forall a b. a -> Either a b
Left NonEmpty (Zerem f m r)
nonempty)) where
    next' :: Either (NonEmpty (Zerem f f r)) (InitZerem f f r, [Zerem f f r])
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
next' (Left (Zerem f f r
z :| [Zerem f f r]
rest)) =
      (\InitZerem f f r
z -> Either a (InitZerem f f r, [Zerem f f r])
-> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip ((InitZerem f f r, [Zerem f f r])
-> Either a (InitZerem f f r, [Zerem f f r])
forall a b. b -> Either a b
Right (InitZerem f f r
z, [Zerem f f r]
rest))) (InitZerem f f r
 -> Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
-> f (InitZerem f f r)
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Zerem f f r -> f (InitZerem f f r)
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m r -> m (InitZerem f m r)
initZerem Zerem f f r
z
    next' (Right (InitZerem s -> f (Step s f r)
next s
s, [Zerem f f r]
rest)) =
      s -> f (Step s f r)
next s
s f (Step s f r)
-> (Step s f r
    -> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r))
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall a b. f a -> (a -> f b) -> f b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done r
u -> case [Zerem f f r]
rest of
          [] -> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either a (InitZerem f f r, [Zerem f f r])) f r
 -> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r))
-> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall a b. (a -> b) -> a -> b
$ r -> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
forall s (f :: * -> *) r. r -> Step s f r
Done r
u
          (Zerem f f r
z : [Zerem f f r]
zs) -> (\InitZerem f f r
z -> Either a (InitZerem f f r, [Zerem f f r])
-> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip ((InitZerem f f r, [Zerem f f r])
-> Either a (InitZerem f f r, [Zerem f f r])
forall a b. b -> Either a b
Right (InitZerem f f r
z, [Zerem f f r]
zs))) (InitZerem f f r
 -> Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
-> f (InitZerem f f r)
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Zerem f f r -> f (InitZerem f f r)
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m r -> m (InitZerem f m r)
initZerem Zerem f f r
z
        Skip s
s' -> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either a (InitZerem f f r, [Zerem f f r])) f r
 -> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r))
-> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall a b. (a -> b) -> a -> b
$ Either a (InitZerem f f r, [Zerem f f r])
-> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip ((InitZerem f f r, [Zerem f f r])
-> Either a (InitZerem f f r, [Zerem f f r])
forall a b. b -> Either a b
Right ((s -> f (Step s f r)) -> s -> InitZerem f f r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> f (Step s f r)
next s
s', [Zerem f f r]
rest))
        Yield f s
fs' -> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either a (InitZerem f f r, [Zerem f f r])) f r
 -> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r))
-> (f (Either a (InitZerem f f r, [Zerem f f r]))
    -> Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
-> f (Either a (InitZerem f f r, [Zerem f f r]))
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f (Either a (InitZerem f f r, [Zerem f f r]))
-> Step (Either a (InitZerem f f r, [Zerem f f r])) f r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (f (Either a (InitZerem f f r, [Zerem f f r]))
 -> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r))
-> f (Either a (InitZerem f f r, [Zerem f f r]))
-> f (Step (Either a (InitZerem f f r, [Zerem f f r])) f r)
forall a b. (a -> b) -> a -> b
$ (s -> Either a (InitZerem f f r, [Zerem f f r]))
-> f s -> f (Either a (InitZerem f f r, [Zerem f f r]))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\s
s' -> (InitZerem f f r, [Zerem f f r])
-> Either a (InitZerem f f r, [Zerem f f r])
forall a b. b -> Either a b
Right ((s -> f (Step s f r)) -> s -> InitZerem f f r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> f (Step s f r)
next s
s', [Zerem f f r]
rest)) f s
fs'

  stimes :: forall b. Integral b => b -> Zerem f m r -> Zerem f m r
stimes b
n (Zerem s -> m (Step s f r)
next m s
s0) = Bool -> Zerem f m r -> Zerem f m r
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
>= b
1) (Zerem f m r -> Zerem f m r) -> Zerem f m r -> Zerem f m r
forall a b. (a -> b) -> a -> b
$ ((b, s) -> m (Step (b, s) f r)) -> m (b, s) -> Zerem f m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (b, s) -> m (Step (b, s) f r)
next' ((b
n,) (s -> (b, s)) -> m s -> m (b, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s0) where
    next' :: (b, s) -> m (Step (b, s) f r)
next' (b
n, s
s) =
      s -> m (Step s f r)
next s
s m (Step s f r)
-> (Step s f r -> m (Step (b, s) f r)) -> m (Step (b, s) f r)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        -- Using Monad constraint to re-run the initialization action
        -- which might be creating a mutable reference that we don't want
        -- to re-use. Also, it's good to behave like (sconcat . replicate n).
        Done r
u -> if b
n b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
1 then Step (b, s) f r -> m (Step (b, s) f r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (b, s) f r -> m (Step (b, s) f r))
-> Step (b, s) f r -> m (Step (b, s) f r)
forall a b. (a -> b) -> a -> b
$ r -> Step (b, s) f r
forall s (f :: * -> *) r. r -> Step s f r
Done r
u else (b, s) -> Step (b, s) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip ((b, s) -> Step (b, s) f r)
-> (s -> (b, s)) -> s -> Step (b, s) f r
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (b -> b
forall a. Enum a => a -> a
pred b
n,) (s -> Step (b, s) f r) -> m s -> m (Step (b, s) f r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s0
        Skip s
s' -> Step (b, s) f r -> m (Step (b, s) f r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (b, s) f r -> m (Step (b, s) f r))
-> Step (b, s) f r -> m (Step (b, s) f r)
forall a b. (a -> b) -> a -> b
$ (b, s) -> Step (b, s) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip (b
n, s
s')
        Yield f s
fs' -> Step (b, s) f r -> m (Step (b, s) f r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (b, s) f r -> m (Step (b, s) f r))
-> (f (b, s) -> Step (b, s) f r) -> f (b, s) -> m (Step (b, s) f r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f (b, s) -> Step (b, s) f r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (f (b, s) -> m (Step (b, s) f r))
-> f (b, s) -> m (Step (b, s) f r)
forall a b. (a -> b) -> a -> b
$ (s -> (b, s)) -> f s -> f (b, s)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (b
n,) f s
fs'

instance (Functor f, Monad m) => Monoid (Zerem f m ()) where
  mempty :: Zerem f m ()
mempty = (() -> m (Step () f ())) -> m () -> Zerem f m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Step () f () -> m (Step () f ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step () f () -> m (Step () f ()))
-> (() -> Step () f ()) -> () -> m (Step () f ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. () -> Step () f ()
forall s (f :: * -> *) r. r -> Step s f r
Done) (() -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())

  mconcat :: [Zerem f m ()] -> Zerem f m ()
mconcat [Zerem f m ()]
list = (Either [Zerem f m ()] (InitZerem f m (), [Zerem f m ()])
 -> m (Step
         (Either [Zerem f m ()] (InitZerem f m (), [Zerem f m ()])) f ()))
-> m (Either [Zerem f m ()] (InitZerem f m (), [Zerem f m ()]))
-> Zerem f m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either [Zerem f m ()] (InitZerem f m (), [Zerem f m ()])
-> m (Step
        (Either [Zerem f m ()] (InitZerem f m (), [Zerem f m ()])) f ())
forall {f :: * -> *} {f :: * -> *} {a}.
(Monad f, Functor f) =>
Either [Zerem f f ()] (InitZerem f f (), [Zerem f f ()])
-> f (Step (Either a (InitZerem f f (), [Zerem f f ()])) f ())
next' (Either [Zerem f m ()] (InitZerem f m (), [Zerem f m ()])
-> m (Either [Zerem f m ()] (InitZerem f m (), [Zerem f m ()]))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Zerem f m ()]
-> Either [Zerem f m ()] (InitZerem f m (), [Zerem f m ()])
forall a b. a -> Either a b
Left [Zerem f m ()]
list)) where
    next' :: Either [Zerem f f ()] (InitZerem f f (), [Zerem f f ()])
-> f (Step (Either a (InitZerem f f (), [Zerem f f ()])) f ())
next' (Left []) = Step (Either a (InitZerem f f (), [Zerem f f ()])) f ()
-> f (Step (Either a (InitZerem f f (), [Zerem f f ()])) f ())
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (() -> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ()
forall s (f :: * -> *) r. r -> Step s f r
Done ())
    next' (Left (Zerem f f ()
z : [Zerem f f ()]
rest)) =
      -- Using Monad constraint to run initialization actions.
      Either a (InitZerem f f (), [Zerem f f ()])
-> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either a (InitZerem f f (), [Zerem f f ()])
 -> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ())
-> (InitZerem f f ()
    -> Either a (InitZerem f f (), [Zerem f f ()]))
-> InitZerem f f ()
-> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (InitZerem f f (), [Zerem f f ()])
-> Either a (InitZerem f f (), [Zerem f f ()])
forall a b. b -> Either a b
Right ((InitZerem f f (), [Zerem f f ()])
 -> Either a (InitZerem f f (), [Zerem f f ()]))
-> (InitZerem f f () -> (InitZerem f f (), [Zerem f f ()]))
-> InitZerem f f ()
-> Either a (InitZerem f f (), [Zerem f f ()])
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (, [Zerem f f ()]
rest) (InitZerem f f ()
 -> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ())
-> f (InitZerem f f ())
-> f (Step (Either a (InitZerem f f (), [Zerem f f ()])) f ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Zerem f f () -> f (InitZerem f f ())
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m r -> m (InitZerem f m r)
initZerem Zerem f f ()
z
    next' (Right (InitZerem s -> f (Step s f ())
next s
s, [Zerem f f ()]
rest)) =
      s -> f (Step s f ())
next s
s f (Step s f ())
-> (Step s f ()
    -> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ())
-> f (Step (Either a (InitZerem f f (), [Zerem f f ()])) f ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done ()
u -> () -> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
u
        Skip s
s' -> Either a (InitZerem f f (), [Zerem f f ()])
-> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ()
forall s (f :: * -> *) r. s -> Step s f r
Skip ((InitZerem f f (), [Zerem f f ()])
-> Either a (InitZerem f f (), [Zerem f f ()])
forall a b. b -> Either a b
Right ((s -> f (Step s f ())) -> s -> InitZerem f f ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> f (Step s f ())
next s
s', [Zerem f f ()]
rest))
        Yield f s
fs' -> f (Either a (InitZerem f f (), [Zerem f f ()]))
-> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (f (Either a (InitZerem f f (), [Zerem f f ()]))
 -> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ())
-> f (Either a (InitZerem f f (), [Zerem f f ()]))
-> Step (Either a (InitZerem f f (), [Zerem f f ()])) f ()
forall a b. (a -> b) -> a -> b
$ (s -> Either a (InitZerem f f (), [Zerem f f ()]))
-> f s -> f (Either a (InitZerem f f (), [Zerem f f ()]))
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\s
s' -> (InitZerem f f (), [Zerem f f ()])
-> Either a (InitZerem f f (), [Zerem f f ()])
forall a b. b -> Either a b
Right ((s -> f (Step s f ())) -> s -> InitZerem f f ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> f (Step s f ())
next s
s', [Zerem f f ()]
rest)) f s
fs'

instance MFunctor (Zerem f) where
  hoist :: forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
(forall a. m a -> n a) -> Zerem f m b -> Zerem f n b
hoist forall a. m a -> n a
f (Zerem s -> m (Step s f b)
next m s
s) = (s -> n (Step s f b)) -> n s -> Zerem f n b
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (m (Step s f b) -> n (Step s f b)
forall a. m a -> n a
f (m (Step s f b) -> n (Step s f b))
-> (s -> m (Step s f b)) -> s -> n (Step s f b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> m (Step s f b)
next) (m s -> n s
forall a. m a -> n a
f m s
s)

newtype ZipZerem r m a = ZipZerem { forall r (m :: * -> *) a. ZipZerem r m a -> Zerem ((,) a) m r
runZipZerem :: Zerem ((,) a) m r }

instance Functor m => Functor (ZipZerem r m) where
  fmap :: forall a b. (a -> b) -> ZipZerem r m a -> ZipZerem r m b
fmap a -> b
f (ZipZerem Zerem ((,) a) m r
z) = Zerem ((,) b) m r -> ZipZerem r m b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem (Zerem ((,) b) m r -> ZipZerem r m b)
-> Zerem ((,) b) m r -> ZipZerem r m b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> Zerem ((,) a) m r -> Zerem ((,) b) m r
forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map a -> b
f Zerem ((,) a) m r
z

instance Monad m => Applicative (ZipZerem r m) where
  pure :: forall a. a -> ZipZerem r m a
pure a
a = Zerem ((,) a) m r -> ZipZerem r m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((a -> m (Step a ((,) a) r)) -> m a -> Zerem ((,) a) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (\a
a -> Step a ((,) a) r -> m (Step a ((,) a) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step a ((,) a) r -> m (Step a ((,) a) r))
-> Step a ((,) a) r -> m (Step a ((,) a) r)
forall a b. (a -> b) -> a -> b
$ (a, a) -> Step a ((,) a) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, a
a)) (a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a))

  (ZipZerem (Zerem s -> m (Step s ((,) (a -> b)) r)
nextf m s
msf)) <*> :: forall a b.
ZipZerem r m (a -> b) -> ZipZerem r m a -> ZipZerem r m b
<*> (ZipZerem (Zerem s -> m (Step s ((,) a) r)
nexta m s
msa))
    = Zerem ((,) b) m r -> ZipZerem r m b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((Either (s, m s) (a -> b, s, s)
 -> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r))
-> m (Either (s, m s) (a -> b, s, s)) -> Zerem ((,) b) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (s, m s) (a -> b, s, s)
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
next' ((\s
sf -> (s, m s) -> Either (s, m s) (a -> b, s, s)
forall a b. a -> Either a b
Left (s
sf, m s
msa)) (s -> Either (s, m s) (a -> b, s, s))
-> m s -> m (Either (s, m s) (a -> b, s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msf)) where
    next' :: Either (s, m s) (a -> b, s, s)
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
next' (Left (s
sf, m s
msa)) =
      s -> m (Step s ((,) (a -> b)) r)
nextf s
sf m (Step s ((,) (a -> b)) r)
-> (Step s ((,) (a -> b)) r
    -> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r))
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done r
r -> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
 -> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r))
-> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
forall a b. (a -> b) -> a -> b
$ r -> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
        Skip s
s' -> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
 -> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r))
-> (Either (s, m s) (a -> b, s, s)
    -> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
-> Either (s, m s) (a -> b, s, s)
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either (s, m s) (a -> b, s, s)
-> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (a -> b, s, s)
 -> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r))
-> Either (s, m s) (a -> b, s, s)
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
forall a b. (a -> b) -> a -> b
$ (s, m s) -> Either (s, m s) (a -> b, s, s)
forall a b. a -> Either a b
Left (s
s', m s
msa)
        Yield (a -> b
f, s
s') -> m s
msa m s
-> (s -> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sa -> Either (s, m s) (a -> b, s, s)
-> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (a -> b, s, s)
 -> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
-> Either (s, m s) (a -> b, s, s)
-> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
forall a b. (a -> b) -> a -> b
$ (a -> b, s, s) -> Either (s, m s) (a -> b, s, s)
forall a b. b -> Either a b
Right (a -> b
f, s
s', s
sa)
    next' (Right (a -> b
f, s
sf, s
sa)) =
      s -> m (Step s ((,) a) r)
nexta s
sa m (Step s ((,) a) r)
-> (Step s ((,) a) r
    -> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
-> m (Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done r
r -> r -> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
        Skip s
s' -> Either (s, m s) (a -> b, s, s)
-> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (a -> b, s, s)
 -> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r)
-> Either (s, m s) (a -> b, s, s)
-> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
forall a b. (a -> b) -> a -> b
$ (a -> b, s, s) -> Either (s, m s) (a -> b, s, s)
forall a b. b -> Either a b
Right (a -> b
f, s
sf, s
s')
        Yield (a
a, s
s') -> (b, Either (s, m s) (a -> b, s, s))
-> Step (Either (s, m s) (a -> b, s, s)) ((,) b) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a -> b
f a
a, (s, m s) -> Either (s, m s) (a -> b, s, s)
forall a b. a -> Either a b
Left (s
sf, s -> m s
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure s
s'))

  liftA2 :: forall a b c.
(a -> b -> c) -> ZipZerem r m a -> ZipZerem r m b -> ZipZerem r m c
liftA2 a -> b -> c
f (ZipZerem (Zerem s -> m (Step s ((,) a) r)
nexta m s
msa)) (ZipZerem (Zerem s -> m (Step s ((,) b) r)
nextb m s
msb))
    = Zerem ((,) c) m r -> ZipZerem r m c
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((Either (s, m s) (a, s, s)
 -> m (Step (Either (s, m s) (a, s, s)) ((,) c) r))
-> m (Either (s, m s) (a, s, s)) -> Zerem ((,) c) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (s, m s) (a, s, s)
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
next' ((\s
sa -> (s, m s) -> Either (s, m s) (a, s, s)
forall a b. a -> Either a b
Left (s
sa, m s
msb)) (s -> Either (s, m s) (a, s, s))
-> m s -> m (Either (s, m s) (a, s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msa)) where
    next' :: Either (s, m s) (a, s, s)
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
next' (Left (s
sa, m s
msb)) =
      s -> m (Step s ((,) a) r)
nexta s
sa m (Step s ((,) a) r)
-> (Step s ((,) a) r
    -> m (Step (Either (s, m s) (a, s, s)) ((,) c) r))
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done r
r -> Step (Either (s, m s) (a, s, s)) ((,) c) r
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, m s) (a, s, s)) ((,) c) r
 -> m (Step (Either (s, m s) (a, s, s)) ((,) c) r))
-> Step (Either (s, m s) (a, s, s)) ((,) c) r
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
forall a b. (a -> b) -> a -> b
$ r -> Step (Either (s, m s) (a, s, s)) ((,) c) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
        Skip s
s' -> Step (Either (s, m s) (a, s, s)) ((,) c) r
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, m s) (a, s, s)) ((,) c) r
 -> m (Step (Either (s, m s) (a, s, s)) ((,) c) r))
-> (Either (s, m s) (a, s, s)
    -> Step (Either (s, m s) (a, s, s)) ((,) c) r)
-> Either (s, m s) (a, s, s)
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) c) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (a, s, s)
 -> m (Step (Either (s, m s) (a, s, s)) ((,) c) r))
-> Either (s, m s) (a, s, s)
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
forall a b. (a -> b) -> a -> b
$ (s, m s) -> Either (s, m s) (a, s, s)
forall a b. a -> Either a b
Left (s
s', m s
msb)
        Yield (a
a, s
s') -> m s
msb m s
-> (s -> Step (Either (s, m s) (a, s, s)) ((,) c) r)
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sb -> Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) c) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (a, s, s)
 -> Step (Either (s, m s) (a, s, s)) ((,) c) r)
-> Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) c) r
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either (s, m s) (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
s', s
sb)
    next' (Right (a
a, s
sa, s
sb)) =
      s -> m (Step s ((,) b) r)
nextb s
sb m (Step s ((,) b) r)
-> (Step s ((,) b) r -> Step (Either (s, m s) (a, s, s)) ((,) c) r)
-> m (Step (Either (s, m s) (a, s, s)) ((,) c) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done r
r -> r -> Step (Either (s, m s) (a, s, s)) ((,) c) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
        Skip s
s' -> Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) c) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (a, s, s)
 -> Step (Either (s, m s) (a, s, s)) ((,) c) r)
-> Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) c) r
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either (s, m s) (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
sa, s
s')
        Yield (b
b, s
s') -> (c, Either (s, m s) (a, s, s))
-> Step (Either (s, m s) (a, s, s)) ((,) c) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a -> b -> c
f a
a b
b, (s, m s) -> Either (s, m s) (a, s, s)
forall a b. a -> Either a b
Left (s
sa, s -> m s
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure s
s'))

  (ZipZerem (Zerem s -> m (Step s ((,) a) r)
next_ m s
ms_)) *> :: forall a b. ZipZerem r m a -> ZipZerem r m b -> ZipZerem r m b
*> (ZipZerem (Zerem s -> m (Step s ((,) b) r)
nexta m s
msa))
    = Zerem ((,) b) m r -> ZipZerem r m b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((Either (s, m s) (s, s)
 -> m (Step (Either (s, m s) (s, s)) ((,) b) r))
-> m (Either (s, m s) (s, s)) -> Zerem ((,) b) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (s, m s) (s, s)
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
next' ((\s
s_ -> (s, m s) -> Either (s, m s) (s, s)
forall a b. a -> Either a b
Left (s
s_, m s
msa)) (s -> Either (s, m s) (s, s)) -> m s -> m (Either (s, m s) (s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
ms_)) where
    next' :: Either (s, m s) (s, s)
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
next' (Left (s
s_, m s
msa)) =
      s -> m (Step s ((,) a) r)
next_ s
s_ m (Step s ((,) a) r)
-> (Step s ((,) a) r
    -> m (Step (Either (s, m s) (s, s)) ((,) b) r))
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done r
r -> Step (Either (s, m s) (s, s)) ((,) b) r
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, m s) (s, s)) ((,) b) r
 -> m (Step (Either (s, m s) (s, s)) ((,) b) r))
-> Step (Either (s, m s) (s, s)) ((,) b) r
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
forall a b. (a -> b) -> a -> b
$ r -> Step (Either (s, m s) (s, s)) ((,) b) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
        Skip s
s' -> Step (Either (s, m s) (s, s)) ((,) b) r
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, m s) (s, s)) ((,) b) r
 -> m (Step (Either (s, m s) (s, s)) ((,) b) r))
-> (Either (s, m s) (s, s)
    -> Step (Either (s, m s) (s, s)) ((,) b) r)
-> Either (s, m s) (s, s)
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either (s, m s) (s, s) -> Step (Either (s, m s) (s, s)) ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (s, s)
 -> m (Step (Either (s, m s) (s, s)) ((,) b) r))
-> Either (s, m s) (s, s)
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
forall a b. (a -> b) -> a -> b
$ (s, m s) -> Either (s, m s) (s, s)
forall a b. a -> Either a b
Left (s
s', m s
msa)
        Yield (a
_, s
s') -> m s
msa m s
-> (s -> Step (Either (s, m s) (s, s)) ((,) b) r)
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sa -> Either (s, m s) (s, s) -> Step (Either (s, m s) (s, s)) ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (s, s) -> Step (Either (s, m s) (s, s)) ((,) b) r)
-> Either (s, m s) (s, s)
-> Step (Either (s, m s) (s, s)) ((,) b) r
forall a b. (a -> b) -> a -> b
$ (s, s) -> Either (s, m s) (s, s)
forall a b. b -> Either a b
Right (s
s', s
sa)
    next' (Right (s
s_, s
sa)) =
      s -> m (Step s ((,) b) r)
nexta s
sa m (Step s ((,) b) r)
-> (Step s ((,) b) r -> Step (Either (s, m s) (s, s)) ((,) b) r)
-> m (Step (Either (s, m s) (s, s)) ((,) b) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done r
r -> r -> Step (Either (s, m s) (s, s)) ((,) b) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
        Skip s
s' -> Either (s, m s) (s, s) -> Step (Either (s, m s) (s, s)) ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (s, s) -> Step (Either (s, m s) (s, s)) ((,) b) r)
-> Either (s, m s) (s, s)
-> Step (Either (s, m s) (s, s)) ((,) b) r
forall a b. (a -> b) -> a -> b
$ (s, s) -> Either (s, m s) (s, s)
forall a b. b -> Either a b
Right (s
s_, s
s')
        Yield (b
a, s
s') -> (b, Either (s, m s) (s, s))
-> Step (Either (s, m s) (s, s)) ((,) b) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (b
a, (s, m s) -> Either (s, m s) (s, s)
forall a b. a -> Either a b
Left (s
s_, s -> m s
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure s
s'))

  (ZipZerem (Zerem s -> m (Step s ((,) a) r)
nexta m s
msa)) <* :: forall a b. ZipZerem r m a -> ZipZerem r m b -> ZipZerem r m a
<* (ZipZerem (Zerem s -> m (Step s ((,) b) r)
next_ m s
ms_))
    = Zerem ((,) a) m r -> ZipZerem r m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((Either (s, m s) (a, s, s)
 -> m (Step (Either (s, m s) (a, s, s)) ((,) a) r))
-> m (Either (s, m s) (a, s, s)) -> Zerem ((,) a) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (s, m s) (a, s, s)
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
next' ((\s
sa -> (s, m s) -> Either (s, m s) (a, s, s)
forall a b. a -> Either a b
Left (s
sa, m s
ms_)) (s -> Either (s, m s) (a, s, s))
-> m s -> m (Either (s, m s) (a, s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msa)) where
    next' :: Either (s, m s) (a, s, s)
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
next' (Left (s
sa, m s
ms_)) =
      s -> m (Step s ((,) a) r)
nexta s
sa m (Step s ((,) a) r)
-> (Step s ((,) a) r
    -> m (Step (Either (s, m s) (a, s, s)) ((,) a) r))
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done r
r -> Step (Either (s, m s) (a, s, s)) ((,) a) r
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, m s) (a, s, s)) ((,) a) r
 -> m (Step (Either (s, m s) (a, s, s)) ((,) a) r))
-> Step (Either (s, m s) (a, s, s)) ((,) a) r
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
forall a b. (a -> b) -> a -> b
$ r -> Step (Either (s, m s) (a, s, s)) ((,) a) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
        Skip s
s' -> Step (Either (s, m s) (a, s, s)) ((,) a) r
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, m s) (a, s, s)) ((,) a) r
 -> m (Step (Either (s, m s) (a, s, s)) ((,) a) r))
-> (Either (s, m s) (a, s, s)
    -> Step (Either (s, m s) (a, s, s)) ((,) a) r)
-> Either (s, m s) (a, s, s)
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) a) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (a, s, s)
 -> m (Step (Either (s, m s) (a, s, s)) ((,) a) r))
-> Either (s, m s) (a, s, s)
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
forall a b. (a -> b) -> a -> b
$ (s, m s) -> Either (s, m s) (a, s, s)
forall a b. a -> Either a b
Left (s
s', m s
ms_)
        Yield (a
a, s
s') -> m s
ms_ m s
-> (s -> Step (Either (s, m s) (a, s, s)) ((,) a) r)
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
s_ -> Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) a) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (a, s, s)
 -> Step (Either (s, m s) (a, s, s)) ((,) a) r)
-> Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) a) r
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either (s, m s) (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
s', s
s_)
    next' (Right (a
a, s
sa, s
s_)) =
      s -> m (Step s ((,) b) r)
next_ s
s_ m (Step s ((,) b) r)
-> (Step s ((,) b) r -> Step (Either (s, m s) (a, s, s)) ((,) a) r)
-> m (Step (Either (s, m s) (a, s, s)) ((,) a) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done r
r -> r -> Step (Either (s, m s) (a, s, s)) ((,) a) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
        Skip s
s' -> Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) a) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, m s) (a, s, s)
 -> Step (Either (s, m s) (a, s, s)) ((,) a) r)
-> Either (s, m s) (a, s, s)
-> Step (Either (s, m s) (a, s, s)) ((,) a) r
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either (s, m s) (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
sa, s
s')
        Yield (b
_, s
s') -> (a, Either (s, m s) (a, s, s))
-> Step (Either (s, m s) (a, s, s)) ((,) a) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, (s, m s) -> Either (s, m s) (a, s, s)
forall a b. a -> Either a b
Left (s
sa, s -> m s
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure s
s'))

instance Monad m => Alternative (ZipZerem () m) where
  empty :: forall a. ZipZerem () m a
empty = Zerem ((,) a) m () -> ZipZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem (Zerem ((,) a) m () -> ZipZerem () m a)
-> Zerem ((,) a) m () -> ZipZerem () m a
forall a b. (a -> b) -> a -> b
$ (() -> m (Step () ((,) a) ())) -> m () -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (m (Step () ((,) a) ()) -> () -> m (Step () ((,) a) ())
forall a b. a -> b -> a
const (Step () ((,) a) () -> m (Step () ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step () ((,) a) () -> m (Step () ((,) a) ()))
-> Step () ((,) a) () -> m (Step () ((,) a) ())
forall a b. (a -> b) -> a -> b
$ () -> Step () ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ())) (() -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())

  (ZipZerem (Zerem s -> m (Step s ((,) a) ())
nextl m s
msl)) <|> :: forall a. ZipZerem () m a -> ZipZerem () m a -> ZipZerem () m a
<|> (ZipZerem (Zerem s -> m (Step s ((,) a) ())
nextr m s
msr))
    = Zerem ((,) a) m () -> ZipZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem (Zerem ((,) a) m () -> ZipZerem () m a)
-> Zerem ((,) a) m () -> ZipZerem () m a
forall a b. (a -> b) -> a -> b
$ (Either (Natural, s) (Natural, s)
 -> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ()))
-> m (Either (Natural, s) (Natural, s)) -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (Natural, s) (Natural, s)
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
next ((Natural, s) -> Either (Natural, s) (Natural, s)
forall a b. a -> Either a b
Left ((Natural, s) -> Either (Natural, s) (Natural, s))
-> (s -> (Natural, s)) -> s -> Either (Natural, s) (Natural, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Natural
0 :: Natural,) (s -> Either (Natural, s) (Natural, s))
-> m s -> m (Either (Natural, s) (Natural, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msl) where
    next :: Either (Natural, s) (Natural, s)
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
next (Left (Natural
n, s
sl)) =
      s -> m (Step s ((,) a) ())
nextl s
sl m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ()))
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done () -> m s
msr m s
-> (s -> Step (Either (Natural, s) (Natural, s)) ((,) a) ())
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sr -> Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (Natural, s) (Natural, s)
 -> Step (Either (Natural, s) (Natural, s)) ((,) a) ())
-> Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (Natural, s) -> Either (Natural, s) (Natural, s)
forall a b. b -> Either a b
Right (Natural
n, s
sr)
        Skip s
s' -> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (Natural, s) (Natural, s)) ((,) a) ()
 -> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ()))
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
forall a b. (a -> b) -> a -> b
$ Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (Natural, s) (Natural, s)
 -> Step (Either (Natural, s) (Natural, s)) ((,) a) ())
-> Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (Natural, s) -> Either (Natural, s) (Natural, s)
forall a b. a -> Either a b
Left (Natural
n, s
s')
        Yield (a
a, s
s') -> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (Natural, s) (Natural, s)) ((,) a) ()
 -> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ()))
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
forall a b. (a -> b) -> a -> b
$ (a, Either (Natural, s) (Natural, s))
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, (Natural, s) -> Either (Natural, s) (Natural, s)
forall a b. a -> Either a b
Left (Natural
nNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
+Natural
1, s
s'))
    next (Right (Natural
0, s
sr)) =
      s -> m (Step s ((,) a) ())
nextr s
sr m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> Step (Either (Natural, s) (Natural, s)) ((,) a) ())
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> () -> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
        Skip s
s' -> Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (Natural, s) (Natural, s)
 -> Step (Either (Natural, s) (Natural, s)) ((,) a) ())
-> Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (Natural, s) -> Either (Natural, s) (Natural, s)
forall a b. b -> Either a b
Right (Natural
0, s
s')
        Yield (a
a, s
s') -> (a, Either (Natural, s) (Natural, s))
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, (Natural, s) -> Either (Natural, s) (Natural, s)
forall a b. b -> Either a b
Right (Natural
0, s
s'))
    next (Right (Natural
n, s
sr)) =
      s -> m (Step s ((,) a) ())
nextr s
sr m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> Step (Either (Natural, s) (Natural, s)) ((,) a) ())
-> m (Step (Either (Natural, s) (Natural, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> () -> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
        Skip s
s' -> Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (Natural, s) (Natural, s)
 -> Step (Either (Natural, s) (Natural, s)) ((,) a) ())
-> Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (Natural, s) -> Either (Natural, s) (Natural, s)
forall a b. b -> Either a b
Right (Natural
n, s
s')
        Yield (a
_, s
s') -> Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (Natural, s) (Natural, s)
 -> Step (Either (Natural, s) (Natural, s)) ((,) a) ())
-> Either (Natural, s) (Natural, s)
-> Step (Either (Natural, s) (Natural, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (Natural, s) -> Either (Natural, s) (Natural, s)
forall a b. b -> Either a b
Right (Natural
nNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
-Natural
1, s
s')

instance {-# OVERLAPPING #-} Applicative (ZipZerem Void Identity) where
  pure :: forall a. a -> ZipZerem Void Identity a
pure a
a = Zerem ((,) a) Identity Void -> ZipZerem Void Identity a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((a -> Identity (Step a ((,) a) Void))
-> Identity a -> Zerem ((,) a) Identity Void
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (\a
a -> Step a ((,) a) Void -> Identity (Step a ((,) a) Void)
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step a ((,) a) Void -> Identity (Step a ((,) a) Void))
-> Step a ((,) a) Void -> Identity (Step a ((,) a) Void)
forall a b. (a -> b) -> a -> b
$ (a, a) -> Step a ((,) a) Void
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, a
a)) (a -> Identity a
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a))

  (ZipZerem (Zerem s -> Identity (Step s ((,) (a -> b)) Void)
nextf Identity s
msf)) <*> :: forall a b.
ZipZerem Void Identity (a -> b)
-> ZipZerem Void Identity a -> ZipZerem Void Identity b
<*> (ZipZerem (Zerem s -> Identity (Step s ((,) a) Void)
nexta Identity s
msa))
    = Zerem ((,) b) Identity Void -> ZipZerem Void Identity b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((Either (s, Identity s) (a -> b, s, s)
 -> Identity
      (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void))
-> Identity (Either (s, Identity s) (a -> b, s, s))
-> Zerem ((,) b) Identity Void
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (s, Identity s) (a -> b, s, s)
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
next' ((\s
sf -> (s, Identity s) -> Either (s, Identity s) (a -> b, s, s)
forall a b. a -> Either a b
Left (s
sf, Identity s
msa)) (s -> Either (s, Identity s) (a -> b, s, s))
-> Identity s -> Identity (Either (s, Identity s) (a -> b, s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identity s
msf)) where
    next' :: Either (s, Identity s) (a -> b, s, s)
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
next' (Left (s
sf, Identity s
msa)) =
      s -> Identity (Step s ((,) (a -> b)) Void)
nextf s
sf Identity (Step s ((,) (a -> b)) Void)
-> (Step s ((,) (a -> b)) Void
    -> Identity
         (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void))
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
forall a b. Identity a -> (a -> Identity b) -> Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done Void
r -> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
 -> Identity
      (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void))
-> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
forall a b. (a -> b) -> a -> b
$ Void -> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
forall s (f :: * -> *) r. r -> Step s f r
Done Void
r
        Skip s
s' -> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
 -> Identity
      (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void))
-> (Either (s, Identity s) (a -> b, s, s)
    -> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
-> Either (s, Identity s) (a -> b, s, s)
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either (s, Identity s) (a -> b, s, s)
-> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Identity s) (a -> b, s, s)
 -> Identity
      (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void))
-> Either (s, Identity s) (a -> b, s, s)
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
forall a b. (a -> b) -> a -> b
$ (s, Identity s) -> Either (s, Identity s) (a -> b, s, s)
forall a b. a -> Either a b
Left (s
s', Identity s
msa)
        Yield (a -> b
f, s
s') -> Identity s
msa Identity s
-> (s -> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sa -> Either (s, Identity s) (a -> b, s, s)
-> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Identity s) (a -> b, s, s)
 -> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
-> Either (s, Identity s) (a -> b, s, s)
-> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
forall a b. (a -> b) -> a -> b
$ (a -> b, s, s) -> Either (s, Identity s) (a -> b, s, s)
forall a b. b -> Either a b
Right (a -> b
f, s
s', s
sa)
    next' (Right (a -> b
f, s
sf, s
sa)) =
      s -> Identity (Step s ((,) a) Void)
nexta s
sa Identity (Step s ((,) a) Void)
-> (Step s ((,) a) Void
    -> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
-> Identity
     (Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done Void
r -> Void -> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
forall s (f :: * -> *) r. r -> Step s f r
Done Void
r
        Skip s
s' -> Either (s, Identity s) (a -> b, s, s)
-> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Identity s) (a -> b, s, s)
 -> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void)
-> Either (s, Identity s) (a -> b, s, s)
-> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
forall a b. (a -> b) -> a -> b
$ (a -> b, s, s) -> Either (s, Identity s) (a -> b, s, s)
forall a b. b -> Either a b
Right (a -> b
f, s
sf, s
s')
        Yield (a
a, s
s') -> (b, Either (s, Identity s) (a -> b, s, s))
-> Step (Either (s, Identity s) (a -> b, s, s)) ((,) b) Void
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a -> b
f a
a, (s, Identity s) -> Either (s, Identity s) (a -> b, s, s)
forall a b. a -> Either a b
Left (s
sf, s -> Identity s
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure s
s'))

  liftA2 :: forall a b c.
(a -> b -> c)
-> ZipZerem Void Identity a
-> ZipZerem Void Identity b
-> ZipZerem Void Identity c
liftA2 a -> b -> c
f (ZipZerem (Zerem s -> Identity (Step s ((,) a) Void)
nexta Identity s
msa)) (ZipZerem (Zerem s -> Identity (Step s ((,) b) Void)
nextb Identity s
msb))
    = Zerem ((,) c) Identity Void -> ZipZerem Void Identity c
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((Either (s, Identity s) (a, s, s)
 -> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void))
-> Identity (Either (s, Identity s) (a, s, s))
-> Zerem ((,) c) Identity Void
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (s, Identity s) (a, s, s)
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
next' ((\s
sa -> (s, Identity s) -> Either (s, Identity s) (a, s, s)
forall a b. a -> Either a b
Left (s
sa, Identity s
msb)) (s -> Either (s, Identity s) (a, s, s))
-> Identity s -> Identity (Either (s, Identity s) (a, s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identity s
msa)) where
    next' :: Either (s, Identity s) (a, s, s)
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
next' (Left (s
sa, Identity s
msb)) =
      s -> Identity (Step s ((,) a) Void)
nexta s
sa Identity (Step s ((,) a) Void)
-> (Step s ((,) a) Void
    -> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void))
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
forall a b. Identity a -> (a -> Identity b) -> Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done Void
r -> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
 -> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void))
-> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
forall a b. (a -> b) -> a -> b
$ Void -> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
forall s (f :: * -> *) r. r -> Step s f r
Done Void
r
        Skip s
s' -> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
 -> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void))
-> (Either (s, Identity s) (a, s, s)
    -> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
-> Either (s, Identity s) (a, s, s)
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either (s, Identity s) (a, s, s)
-> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Identity s) (a, s, s)
 -> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void))
-> Either (s, Identity s) (a, s, s)
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
forall a b. (a -> b) -> a -> b
$ (s, Identity s) -> Either (s, Identity s) (a, s, s)
forall a b. a -> Either a b
Left (s
s', Identity s
msb)
        Yield (a
a, s
s') -> Identity s
msb Identity s
-> (s -> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sb -> Either (s, Identity s) (a, s, s)
-> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Identity s) (a, s, s)
 -> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
-> Either (s, Identity s) (a, s, s)
-> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either (s, Identity s) (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
s', s
sb)
    next' (Right (a
a, s
sa, s
sb)) =
      s -> Identity (Step s ((,) b) Void)
nextb s
sb Identity (Step s ((,) b) Void)
-> (Step s ((,) b) Void
    -> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
-> Identity (Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done Void
r -> Void -> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
forall s (f :: * -> *) r. r -> Step s f r
Done Void
r
        Skip s
s' -> Either (s, Identity s) (a, s, s)
-> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Identity s) (a, s, s)
 -> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void)
-> Either (s, Identity s) (a, s, s)
-> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either (s, Identity s) (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
sa, s
s')
        Yield (b
b, s
s') -> (c, Either (s, Identity s) (a, s, s))
-> Step (Either (s, Identity s) (a, s, s)) ((,) c) Void
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a -> b -> c
f a
a b
b, (s, Identity s) -> Either (s, Identity s) (a, s, s)
forall a b. a -> Either a b
Left (s
sa, s -> Identity s
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure s
s'))

  ZipZerem Void Identity a
_ *> :: forall a b.
ZipZerem Void Identity a
-> ZipZerem Void Identity b -> ZipZerem Void Identity b
*> ZipZerem Void Identity b
z = ZipZerem Void Identity b
z

  ZipZerem Void Identity a
z <* :: forall a b.
ZipZerem Void Identity a
-> ZipZerem Void Identity b -> ZipZerem Void Identity a
<* ZipZerem Void Identity b
_ = ZipZerem Void Identity a
z

instance Monad (ZipZerem Void Identity) where
  (ZipZerem (Zerem s -> Identity (Step s ((,) a) Void)
nexta Identity s
msa)) >>= :: forall a b.
ZipZerem Void Identity a
-> (a -> ZipZerem Void Identity b) -> ZipZerem Void Identity b
>>= a -> ZipZerem Void Identity b
k = Zerem ((,) b) Identity Void -> ZipZerem Void Identity b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((Either
   (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
 -> Identity
      (Step
         (Either
            (Natural, s)
            (Natural, s, Natural, InitZerem ((,) b) Identity Void))
         ((,) b)
         Void))
-> Identity
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
-> Zerem ((,) b) Identity Void
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either
  (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Identity
     (Step
        (Either
           (Natural, s)
           (Natural, s, Natural, InitZerem ((,) b) Identity Void))
        ((,) b)
        Void)
next ((Natural, s)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
forall a b. a -> Either a b
Left ((Natural, s)
 -> Either
      (Natural, s)
      (Natural, s, Natural, InitZerem ((,) b) Identity Void))
-> (s -> (Natural, s))
-> s
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Natural
0 :: Natural,) (s
 -> Either
      (Natural, s)
      (Natural, s, Natural, InitZerem ((,) b) Identity Void))
-> Identity s
-> Identity
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identity s
msa)) where
    next :: Either
  (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Identity
     (Step
        (Either
           (Natural, s)
           (Natural, s, Natural, InitZerem ((,) b) Identity Void))
        ((,) b)
        Void)
next (Left (Natural
n, s
sa)) =
      s -> Identity (Step s ((,) a) Void)
nexta s
sa Identity (Step s ((,) a) Void)
-> (Step s ((,) a) Void
    -> Identity
         (Step
            (Either
               (Natural, s)
               (Natural, s, Natural, InitZerem ((,) b) Identity Void))
            ((,) b)
            Void))
-> Identity
     (Step
        (Either
           (Natural, s)
           (Natural, s, Natural, InitZerem ((,) b) Identity Void))
        ((,) b)
        Void)
forall a b. Identity a -> (a -> Identity b) -> Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done Void
v -> Void
-> Identity
     (Step
        (Either
           (Natural, s)
           (Natural, s, Natural, InitZerem ((,) b) Identity Void))
        ((,) b)
        Void)
forall a. Void -> a
absurd Void
v
        Skip s
s' -> Step
  (Either
     (Natural, s)
     (Natural, s, Natural, InitZerem ((,) b) Identity Void))
  ((,) b)
  Void
-> Identity
     (Step
        (Either
           (Natural, s)
           (Natural, s, Natural, InitZerem ((,) b) Identity Void))
        ((,) b)
        Void)
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step
   (Either
      (Natural, s)
      (Natural, s, Natural, InitZerem ((,) b) Identity Void))
   ((,) b)
   Void
 -> Identity
      (Step
         (Either
            (Natural, s)
            (Natural, s, Natural, InitZerem ((,) b) Identity Void))
         ((,) b)
         Void))
-> (Either
      (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
    -> Step
         (Either
            (Natural, s)
            (Natural, s, Natural, InitZerem ((,) b) Identity Void))
         ((,) b)
         Void)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Identity
     (Step
        (Either
           (Natural, s)
           (Natural, s, Natural, InitZerem ((,) b) Identity Void))
        ((,) b)
        Void)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either
  (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Step
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
     ((,) b)
     Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either
   (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
 -> Identity
      (Step
         (Either
            (Natural, s)
            (Natural, s, Natural, InitZerem ((,) b) Identity Void))
         ((,) b)
         Void))
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Identity
     (Step
        (Either
           (Natural, s)
           (Natural, s, Natural, InitZerem ((,) b) Identity Void))
        ((,) b)
        Void)
forall a b. (a -> b) -> a -> b
$ (Natural, s)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
forall a b. a -> Either a b
Left (Natural
n, s
s')
        Yield (a
a, s
s') -> case a -> ZipZerem Void Identity b
k a
a of -- RIP Fusion
          ZipZerem Zerem ((,) b) Identity Void
zb -> (\InitZerem ((,) b) Identity Void
zb -> Either
  (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Step
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
     ((,) b)
     Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either
   (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
 -> Step
      (Either
         (Natural, s)
         (Natural, s, Natural, InitZerem ((,) b) Identity Void))
      ((,) b)
      Void)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Step
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
     ((,) b)
     Void
forall a b. (a -> b) -> a -> b
$ (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
forall a b. b -> Either a b
Right (Natural
nNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
+Natural
1, s
s', Natural
n, InitZerem ((,) b) Identity Void
zb)) (InitZerem ((,) b) Identity Void
 -> Step
      (Either
         (Natural, s)
         (Natural, s, Natural, InitZerem ((,) b) Identity Void))
      ((,) b)
      Void)
-> Identity (InitZerem ((,) b) Identity Void)
-> Identity
     (Step
        (Either
           (Natural, s)
           (Natural, s, Natural, InitZerem ((,) b) Identity Void))
        ((,) b)
        Void)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Zerem ((,) b) Identity Void
-> Identity (InitZerem ((,) b) Identity Void)
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m r -> m (InitZerem f m r)
initZerem Zerem ((,) b) Identity Void
zb
    next (Right (Natural
n, s
sa, Natural
c, InitZerem s -> Identity (Step s ((,) b) Void)
nextb s
sb)) =
      s -> Identity (Step s ((,) b) Void)
nextb s
sb Identity (Step s ((,) b) Void)
-> (Step s ((,) b) Void
    -> Step
         (Either
            (Natural, s)
            (Natural, s, Natural, InitZerem ((,) b) Identity Void))
         ((,) b)
         Void)
-> Identity
     (Step
        (Either
           (Natural, s)
           (Natural, s, Natural, InitZerem ((,) b) Identity Void))
        ((,) b)
        Void)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done Void
v -> Void
-> Step
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
     ((,) b)
     Void
forall a. Void -> a
absurd Void
v
        Skip s
s' -> Either
  (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Step
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
     ((,) b)
     Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either
   (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
 -> Step
      (Either
         (Natural, s)
         (Natural, s, Natural, InitZerem ((,) b) Identity Void))
      ((,) b)
      Void)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Step
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
     ((,) b)
     Void
forall a b. (a -> b) -> a -> b
$ (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
forall a b. b -> Either a b
Right (Natural
n, s
sa, Natural
c, (s -> Identity (Step s ((,) b) Void))
-> s -> InitZerem ((,) b) Identity Void
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> Identity (Step s ((,) b) Void)
nextb s
s')
        Yield (b
b, s
s') -> case Natural
c of
          Natural
0 -> (b,
 Either
   (Natural, s)
   (Natural, s, Natural, InitZerem ((,) b) Identity Void))
-> Step
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
     ((,) b)
     Void
forall s (f :: * -> *) r. f s -> Step s f r
Yield (b
b, (Natural, s)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
forall a b. a -> Either a b
Left (Natural
n, s
sa))
          Natural
_ -> Either
  (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Step
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
     ((,) b)
     Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either
   (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
 -> Step
      (Either
         (Natural, s)
         (Natural, s, Natural, InitZerem ((,) b) Identity Void))
      ((,) b)
      Void)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Step
     (Either
        (Natural, s)
        (Natural, s, Natural, InitZerem ((,) b) Identity Void))
     ((,) b)
     Void
forall a b. (a -> b) -> a -> b
$ (Natural, s, Natural, InitZerem ((,) b) Identity Void)
-> Either
     (Natural, s) (Natural, s, Natural, InitZerem ((,) b) Identity Void)
forall a b. b -> Either a b
Right (Natural
n, s
sa, Natural
cNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
-Natural
1, (s -> Identity (Step s ((,) b) Void))
-> s -> InitZerem ((,) b) Identity Void
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> Identity (Step s ((,) b) Void)
nextb s
s')

instance MonadTrans (ZipZerem Void) where
  lift :: forall (m :: * -> *) a. Monad m => m a -> ZipZerem Void m a
lift m a
ma = Zerem ((,) a) m Void -> ZipZerem Void m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem ((a -> m (Step a ((,) a) Void)) -> m a -> Zerem ((,) a) m Void
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (\a
a -> Step a ((,) a) Void -> m (Step a ((,) a) Void)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step a ((,) a) Void -> m (Step a ((,) a) Void))
-> Step a ((,) a) Void -> m (Step a ((,) a) Void)
forall a b. (a -> b) -> a -> b
$ (a, a) -> Step a ((,) a) Void
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, a
a)) m a
ma)

instance MFunctor (ZipZerem r) where
  hoist :: forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
(forall a. m a -> n a) -> ZipZerem r m b -> ZipZerem r n b
hoist forall a. m a -> n a
f (ZipZerem (Zerem s -> m (Step s ((,) b) r)
next m s
s)) = Zerem ((,) b) n r -> ZipZerem r n b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem (Zerem ((,) b) n r -> ZipZerem r n b)
-> Zerem ((,) b) n r -> ZipZerem r n b
forall a b. (a -> b) -> a -> b
$ (s -> n (Step s ((,) b) r)) -> n s -> Zerem ((,) b) n r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (m (Step s ((,) b) r) -> n (Step s ((,) b) r)
forall a. m a -> n a
f (m (Step s ((,) b) r) -> n (Step s ((,) b) r))
-> (s -> m (Step s ((,) b) r)) -> s -> n (Step s ((,) b) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> m (Step s ((,) b) r)
next) (m s -> n s
forall a. m a -> n a
f m s
s)

instance Comonad (ZipZerem Void Identity) where
  extract :: forall a. ZipZerem Void Identity a -> a
extract (ZipZerem (Zerem s -> Identity (Step s ((,) a) Void)
next Identity s
ms)) = Identity a -> a
forall a. Identity a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (Identity a -> a) -> Identity a -> a
forall a b. (a -> b) -> a -> b
$ s -> Identity a
loop (s -> Identity a) -> Identity s -> Identity a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Identity s
ms where
    loop :: s -> Identity a
loop s
s =
      s -> Identity (Step s ((,) a) Void)
next s
s Identity (Step s ((,) a) Void)
-> (Step s ((,) a) Void -> Identity a) -> Identity a
forall a b. Identity a -> (a -> Identity b) -> Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done Void
v -> Void -> Identity a
forall a. Void -> a
absurd Void
v
        Skip s
s' -> s -> Identity a
loop s
s'
        Yield (a
a, s
_) -> a -> Identity a
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a

  duplicate :: forall a.
ZipZerem Void Identity a
-> ZipZerem Void Identity (ZipZerem Void Identity a)
duplicate (ZipZerem z :: Zerem ((,) a) Identity Void
z@(Zerem s -> Identity (Step s ((,) a) Void)
next Identity s
ms)) = Zerem ((,) (ZipZerem Void Identity a)) Identity Void
-> ZipZerem Void Identity (ZipZerem Void Identity a)
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem (Zerem ((,) (ZipZerem Void Identity a)) Identity Void
 -> ZipZerem Void Identity (ZipZerem Void Identity a))
-> Zerem ((,) (ZipZerem Void Identity a)) Identity Void
-> ZipZerem Void Identity (ZipZerem Void Identity a)
forall a b. (a -> b) -> a -> b
$ ((Natural, s)
 -> Identity
      (Step (Natural, s) ((,) (ZipZerem Void Identity a)) Void))
-> Identity (Natural, s)
-> Zerem ((,) (ZipZerem Void Identity a)) Identity Void
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Natural, s)
-> Identity
     (Step (Natural, s) ((,) (ZipZerem Void Identity a)) Void)
next' ((Natural
0 :: Natural,) (s -> (Natural, s)) -> Identity s -> Identity (Natural, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identity s
ms) where
    next' :: (Natural, s)
-> Identity
     (Step (Natural, s) ((,) (ZipZerem Void Identity a)) Void)
next' (Natural
n, s
s) =
      s -> Identity (Step s ((,) a) Void)
next s
s Identity (Step s ((,) a) Void)
-> (Step s ((,) a) Void
    -> Step (Natural, s) ((,) (ZipZerem Void Identity a)) Void)
-> Identity
     (Step (Natural, s) ((,) (ZipZerem Void Identity a)) Void)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done Void
v -> Void -> Step (Natural, s) ((,) (ZipZerem Void Identity a)) Void
forall a. Void -> a
absurd Void
v
        Skip s
s' -> (Natural, s)
-> Step (Natural, s) ((,) (ZipZerem Void Identity a)) Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Natural
n, s
s')
        Yield (a
_, s
s') -> (ZipZerem Void Identity a, (Natural, s))
-> Step (Natural, s) ((,) (ZipZerem Void Identity a)) Void
forall s (f :: * -> *) r. f s -> Step s f r
Yield (Zerem ((,) a) Identity Void -> ZipZerem Void Identity a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem (Zerem ((,) a) Identity Void -> ZipZerem Void Identity a)
-> Zerem ((,) a) Identity Void -> ZipZerem Void Identity a
forall a b. (a -> b) -> a -> b
$ Int -> Zerem ((,) a) Identity Void -> Zerem ((,) a) Identity Void
forall (f :: * -> *) (m :: * -> *) r.
(Comonad f, Functor m) =>
Int -> Zerem f m r -> Zerem f m r
Zerem.drop (Natural -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Natural
n) Zerem ((,) a) Identity Void
z, (Natural
nNatural -> Natural -> Natural
forall a. Num a => a -> a -> a
+Natural
1, s
s'))

instance ComonadApply (ZipZerem Void Identity) where
  <@> :: forall a b.
ZipZerem Void Identity (a -> b)
-> ZipZerem Void Identity a -> ZipZerem Void Identity b
(<@>) = ZipZerem Void Identity (a -> b)
-> ZipZerem Void Identity a -> ZipZerem Void Identity b
forall a b.
ZipZerem Void Identity (a -> b)
-> ZipZerem Void Identity a -> ZipZerem Void Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)

instance Foldable (ZipZerem r Identity) where
  foldr :: forall a b. (a -> b -> b) -> b -> ZipZerem r Identity a -> b
foldr a -> b -> b
f b
z (ZipZerem Zerem ((,) a) Identity r
stream) = Identity b -> b
forall a. Identity a -> a
runIdentity (Identity b -> b) -> Identity b -> b
forall a b. (a -> b) -> a -> b
$ (a -> b -> b) -> b -> Zerem ((,) a) Identity r -> Identity b
forall (m :: * -> *) a b r.
Monad m =>
(a -> b -> b) -> b -> Zerem ((,) a) m r -> m b
foldr_ a -> b -> b
f b
z Zerem ((,) a) Identity r
stream
  {-# INLINE foldr #-}

  foldl' :: forall b a. (b -> a -> b) -> b -> ZipZerem r Identity a -> b
foldl' b -> a -> b
f b
z (ZipZerem Zerem ((,) a) Identity r
stream) = Identity b -> b
forall a. Identity a -> a
runIdentity (Identity b -> b) -> Identity b -> b
forall a b. (a -> b) -> a -> b
$ (b -> a -> b) -> b -> Zerem ((,) a) Identity r -> Identity b
forall (m :: * -> *) b a r.
Monad m =>
(b -> a -> b) -> b -> Zerem ((,) a) m r -> m b
foldl_ b -> a -> b
f b
z Zerem ((,) a) Identity r
stream
  {-# INLINE foldl' #-}

instance Traversable (ZipZerem () Identity) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ZipZerem () Identity a -> f (ZipZerem () Identity b)
traverse a -> f b
f = ([b] -> ZipZerem () Identity b)
-> f [b] -> f (ZipZerem () Identity b)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Zerem ((,) b) Identity () -> ZipZerem () Identity b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem (Zerem ((,) b) Identity () -> ZipZerem () Identity b)
-> ([b] -> Zerem ((,) b) Identity ())
-> [b]
-> ZipZerem () Identity b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [b] -> Zerem ((,) b) Identity ()
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Applicative m) =>
f a -> Zerem ((,) a) m ()
yieldMany) (f [b] -> f (ZipZerem () Identity b))
-> (ZipZerem () Identity a -> f [b])
-> ZipZerem () Identity a
-> f (ZipZerem () Identity b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a -> f b) -> [a] -> f [b]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse a -> f b
f ([a] -> f [b])
-> (ZipZerem () Identity a -> [a])
-> ZipZerem () Identity a
-> f [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ZipZerem () Identity a -> [a]
forall a. ZipZerem () Identity a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
  {-# INLINE traverse #-}

newtype ListZerem r m a = ListZerem { forall r (m :: * -> *) a. ListZerem r m a -> Zerem ((,) a) m r
runListZerem :: Zerem ((,) a) m r }

instance Functor m => Functor (ListZerem r m) where
  fmap :: forall a b. (a -> b) -> ListZerem r m a -> ListZerem r m b
fmap a -> b
f (ListZerem Zerem ((,) a) m r
z) = Zerem ((,) b) m r -> ListZerem r m b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) b) m r -> ListZerem r m b)
-> Zerem ((,) b) m r -> ListZerem r m b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> Zerem ((,) a) m r -> Zerem ((,) b) m r
forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map a -> b
f Zerem ((,) a) m r
z

instance Monad m => Applicative (ListZerem () m) where
  pure :: forall a. a -> ListZerem () m a
pure a
a = Zerem ((,) a) m () -> ListZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem ((Maybe a -> m (Step (Maybe a) ((,) a) ()))
-> m (Maybe a) -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Step (Maybe a) ((,) a) () -> m (Step (Maybe a) ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Maybe a) ((,) a) () -> m (Step (Maybe a) ((,) a) ()))
-> (Maybe a -> Step (Maybe a) ((,) a) ())
-> Maybe a
-> m (Step (Maybe a) ((,) a) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Maybe a -> Step (Maybe a) ((,) a) ()
forall {a} {a}. Maybe a -> Step (Maybe a) ((,) a) ()
next) (Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Maybe a
forall a. a -> Maybe a
Just a
a))) where
    next :: Maybe a -> Step (Maybe a) ((,) a) ()
next Maybe a
Nothing = () -> Step (Maybe a) ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
    next (Just a
a) = (a, Maybe a) -> Step (Maybe a) ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, Maybe a
forall a. Maybe a
Nothing)

  (ListZerem (Zerem s -> m (Step s ((,) (a -> b)) ())
nextf m s
msf)) <*> :: forall a b.
ListZerem () m (a -> b) -> ListZerem () m a -> ListZerem () m b
<*> (ListZerem (Zerem s -> m (Step s ((,) a) ())
nexta m s
msa))
    = Zerem ((,) b) m () -> ListZerem () m b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem ((Either s (a -> b, s, s)
 -> m (Step (Either s (a -> b, s, s)) ((,) b) ()))
-> m (Either s (a -> b, s, s)) -> Zerem ((,) b) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either s (a -> b, s, s)
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
next' (s -> Either s (a -> b, s, s)
forall a b. a -> Either a b
Left (s -> Either s (a -> b, s, s))
-> m s -> m (Either s (a -> b, s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msf)) where
    next' :: Either s (a -> b, s, s)
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
next' (Left s
sf) =
      s -> m (Step s ((,) (a -> b)) ())
nextf s
sf m (Step s ((,) (a -> b)) ())
-> (Step s ((,) (a -> b)) ()
    -> m (Step (Either s (a -> b, s, s)) ((,) b) ()))
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done () -> Step (Either s (a -> b, s, s)) ((,) b) ()
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a -> b, s, s)) ((,) b) ()
 -> m (Step (Either s (a -> b, s, s)) ((,) b) ()))
-> Step (Either s (a -> b, s, s)) ((,) b) ()
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
forall a b. (a -> b) -> a -> b
$ () -> Step (Either s (a -> b, s, s)) ((,) b) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
        Skip s
s' -> Step (Either s (a -> b, s, s)) ((,) b) ()
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a -> b, s, s)) ((,) b) ()
 -> m (Step (Either s (a -> b, s, s)) ((,) b) ()))
-> (Either s (a -> b, s, s)
    -> Step (Either s (a -> b, s, s)) ((,) b) ())
-> Either s (a -> b, s, s)
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either s (a -> b, s, s)
-> Step (Either s (a -> b, s, s)) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a -> b, s, s)
 -> m (Step (Either s (a -> b, s, s)) ((,) b) ()))
-> Either s (a -> b, s, s)
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
forall a b. (a -> b) -> a -> b
$ s -> Either s (a -> b, s, s)
forall a b. a -> Either a b
Left s
s'
        Yield (a -> b
f, s
s') -> m s
msa m s
-> (s -> Step (Either s (a -> b, s, s)) ((,) b) ())
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sa -> Either s (a -> b, s, s)
-> Step (Either s (a -> b, s, s)) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a -> b, s, s)
 -> Step (Either s (a -> b, s, s)) ((,) b) ())
-> Either s (a -> b, s, s)
-> Step (Either s (a -> b, s, s)) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ (a -> b, s, s) -> Either s (a -> b, s, s)
forall a b. b -> Either a b
Right (a -> b
f, s
s', s
sa)
    next' (Right (a -> b
f, s
sf, s
sa)) =
      s -> m (Step s ((,) a) ())
nexta s
sa m (Step s ((,) a) ())
-> (Step s ((,) a) () -> Step (Either s (a -> b, s, s)) ((,) b) ())
-> m (Step (Either s (a -> b, s, s)) ((,) b) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done ()
_ -> Either s (a -> b, s, s)
-> Step (Either s (a -> b, s, s)) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a -> b, s, s)
 -> Step (Either s (a -> b, s, s)) ((,) b) ())
-> Either s (a -> b, s, s)
-> Step (Either s (a -> b, s, s)) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ s -> Either s (a -> b, s, s)
forall a b. a -> Either a b
Left s
sf
        Skip s
s' -> Either s (a -> b, s, s)
-> Step (Either s (a -> b, s, s)) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a -> b, s, s)
 -> Step (Either s (a -> b, s, s)) ((,) b) ())
-> Either s (a -> b, s, s)
-> Step (Either s (a -> b, s, s)) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ (a -> b, s, s) -> Either s (a -> b, s, s)
forall a b. b -> Either a b
Right (a -> b
f, s
sf, s
s')
        Yield (a
a, s
s') -> (b, Either s (a -> b, s, s))
-> Step (Either s (a -> b, s, s)) ((,) b) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a -> b
f a
a, (a -> b, s, s) -> Either s (a -> b, s, s)
forall a b. b -> Either a b
Right (a -> b
f, s
sf, s
s'))

  liftA2 :: forall a b c.
(a -> b -> c)
-> ListZerem () m a -> ListZerem () m b -> ListZerem () m c
liftA2 a -> b -> c
f (ListZerem (Zerem s -> m (Step s ((,) a) ())
nexta m s
msa)) (ListZerem (Zerem s -> m (Step s ((,) b) ())
nextb m s
msb))
    = Zerem ((,) c) m () -> ListZerem () m c
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem ((Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) c) ()))
-> m (Either s (a, s, s)) -> Zerem ((,) c) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) c) ())
next' (s -> Either s (a, s, s)
forall a b. a -> Either a b
Left (s -> Either s (a, s, s)) -> m s -> m (Either s (a, s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msa)) where
    next' :: Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) c) ())
next' (Left s
sa) =
      s -> m (Step s ((,) a) ())
nexta s
sa m (Step s ((,) a) ())
-> (Step s ((,) a) () -> m (Step (Either s (a, s, s)) ((,) c) ()))
-> m (Step (Either s (a, s, s)) ((,) c) ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done () -> Step (Either s (a, s, s)) ((,) c) ()
-> m (Step (Either s (a, s, s)) ((,) c) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a, s, s)) ((,) c) ()
 -> m (Step (Either s (a, s, s)) ((,) c) ()))
-> Step (Either s (a, s, s)) ((,) c) ()
-> m (Step (Either s (a, s, s)) ((,) c) ())
forall a b. (a -> b) -> a -> b
$ () -> Step (Either s (a, s, s)) ((,) c) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
        Skip s
s' -> Step (Either s (a, s, s)) ((,) c) ()
-> m (Step (Either s (a, s, s)) ((,) c) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a, s, s)) ((,) c) ()
 -> m (Step (Either s (a, s, s)) ((,) c) ()))
-> (Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ())
-> Either s (a, s, s)
-> m (Step (Either s (a, s, s)) ((,) c) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) c) ()))
-> Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) c) ())
forall a b. (a -> b) -> a -> b
$ s -> Either s (a, s, s)
forall a b. a -> Either a b
Left s
s'
        Yield (a
a, s
s') -> m s
msb m s
-> (s -> Step (Either s (a, s, s)) ((,) c) ())
-> m (Step (Either s (a, s, s)) ((,) c) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sb -> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ())
-> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ()
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either s (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
s', s
sb)
    next' (Right (a
a, s
sa, s
sb)) =
      s -> m (Step s ((,) b) ())
nextb s
sb m (Step s ((,) b) ())
-> (Step s ((,) b) () -> Step (Either s (a, s, s)) ((,) c) ())
-> m (Step (Either s (a, s, s)) ((,) c) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ())
-> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ()
forall a b. (a -> b) -> a -> b
$ s -> Either s (a, s, s)
forall a b. a -> Either a b
Left s
sa
        Skip s
s' -> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ())
-> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) c) ()
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either s (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
sa, s
s')
        Yield (b
b, s
s') -> (c, Either s (a, s, s)) -> Step (Either s (a, s, s)) ((,) c) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a -> b -> c
f a
a b
b, (a, s, s) -> Either s (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
sa, s
s'))

  (ListZerem (Zerem s -> m (Step s ((,) a) ())
next_ m s
ms_)) *> :: forall a b.
ListZerem () m a -> ListZerem () m b -> ListZerem () m b
*> (ListZerem (Zerem s -> m (Step s ((,) b) ())
nexta m s
msa))
    = Zerem ((,) b) m () -> ListZerem () m b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem ((Either s (s, s) -> m (Step (Either s (s, s)) ((,) b) ()))
-> m (Either s (s, s)) -> Zerem ((,) b) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either s (s, s) -> m (Step (Either s (s, s)) ((,) b) ())
next' (s -> Either s (s, s)
forall a b. a -> Either a b
Left (s -> Either s (s, s)) -> m s -> m (Either s (s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
ms_)) where
    next' :: Either s (s, s) -> m (Step (Either s (s, s)) ((,) b) ())
next' (Left s
s_) =
      s -> m (Step s ((,) a) ())
next_ s
s_ m (Step s ((,) a) ())
-> (Step s ((,) a) () -> m (Step (Either s (s, s)) ((,) b) ()))
-> m (Step (Either s (s, s)) ((,) b) ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done () -> Step (Either s (s, s)) ((,) b) ()
-> m (Step (Either s (s, s)) ((,) b) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (s, s)) ((,) b) ()
 -> m (Step (Either s (s, s)) ((,) b) ()))
-> Step (Either s (s, s)) ((,) b) ()
-> m (Step (Either s (s, s)) ((,) b) ())
forall a b. (a -> b) -> a -> b
$ () -> Step (Either s (s, s)) ((,) b) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
        Skip s
s' -> Step (Either s (s, s)) ((,) b) ()
-> m (Step (Either s (s, s)) ((,) b) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (s, s)) ((,) b) ()
 -> m (Step (Either s (s, s)) ((,) b) ()))
-> (Either s (s, s) -> Step (Either s (s, s)) ((,) b) ())
-> Either s (s, s)
-> m (Step (Either s (s, s)) ((,) b) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either s (s, s) -> Step (Either s (s, s)) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (s, s) -> m (Step (Either s (s, s)) ((,) b) ()))
-> Either s (s, s) -> m (Step (Either s (s, s)) ((,) b) ())
forall a b. (a -> b) -> a -> b
$ s -> Either s (s, s)
forall a b. a -> Either a b
Left s
s'
        Yield (a
_, s
s') -> m s
msa m s
-> (s -> Step (Either s (s, s)) ((,) b) ())
-> m (Step (Either s (s, s)) ((,) b) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sa -> Either s (s, s) -> Step (Either s (s, s)) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (s, s) -> Step (Either s (s, s)) ((,) b) ())
-> Either s (s, s) -> Step (Either s (s, s)) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ (s, s) -> Either s (s, s)
forall a b. b -> Either a b
Right (s
s', s
sa)
    next' (Right (s
s_, s
sa)) =
      s -> m (Step s ((,) b) ())
nexta s
sa m (Step s ((,) b) ())
-> (Step s ((,) b) () -> Step (Either s (s, s)) ((,) b) ())
-> m (Step (Either s (s, s)) ((,) b) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> Either s (s, s) -> Step (Either s (s, s)) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (s, s) -> Step (Either s (s, s)) ((,) b) ())
-> Either s (s, s) -> Step (Either s (s, s)) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ s -> Either s (s, s)
forall a b. a -> Either a b
Left s
s_
        Skip s
s' -> Either s (s, s) -> Step (Either s (s, s)) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (s, s) -> Step (Either s (s, s)) ((,) b) ())
-> Either s (s, s) -> Step (Either s (s, s)) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ (s, s) -> Either s (s, s)
forall a b. b -> Either a b
Right (s
s_, s
s')
        Yield (b
a, s
s') -> (b, Either s (s, s)) -> Step (Either s (s, s)) ((,) b) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (b
a, (s, s) -> Either s (s, s)
forall a b. b -> Either a b
Right (s
s_, s
s'))

  (ListZerem (Zerem s -> m (Step s ((,) a) ())
nexta m s
msa)) <* :: forall a b.
ListZerem () m a -> ListZerem () m b -> ListZerem () m a
<* (ListZerem (Zerem s -> m (Step s ((,) b) ())
next_ m s
ms_))
    = Zerem ((,) a) m () -> ListZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem ((Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) a) ()))
-> m (Either s (a, s, s)) -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) a) ())
next' (s -> Either s (a, s, s)
forall a b. a -> Either a b
Left (s -> Either s (a, s, s)) -> m s -> m (Either s (a, s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msa)) where
    next' :: Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) a) ())
next' (Left s
sa) =
      s -> m (Step s ((,) a) ())
nexta s
sa m (Step s ((,) a) ())
-> (Step s ((,) a) () -> m (Step (Either s (a, s, s)) ((,) a) ()))
-> m (Step (Either s (a, s, s)) ((,) a) ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done ()
r -> Step (Either s (a, s, s)) ((,) a) ()
-> m (Step (Either s (a, s, s)) ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a, s, s)) ((,) a) ()
 -> m (Step (Either s (a, s, s)) ((,) a) ()))
-> Step (Either s (a, s, s)) ((,) a) ()
-> m (Step (Either s (a, s, s)) ((,) a) ())
forall a b. (a -> b) -> a -> b
$ () -> Step (Either s (a, s, s)) ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
r
        Skip s
s' -> Step (Either s (a, s, s)) ((,) a) ()
-> m (Step (Either s (a, s, s)) ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (a, s, s)) ((,) a) ()
 -> m (Step (Either s (a, s, s)) ((,) a) ()))
-> (Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) a) ())
-> Either s (a, s, s)
-> m (Step (Either s (a, s, s)) ((,) a) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) a) ()))
-> Either s (a, s, s) -> m (Step (Either s (a, s, s)) ((,) a) ())
forall a b. (a -> b) -> a -> b
$ s -> Either s (a, s, s)
forall a b. a -> Either a b
Left s
s'
        Yield (a
a, s
s') -> m s
ms_ m s
-> (s -> Step (Either s (a, s, s)) ((,) a) ())
-> m (Step (Either s (a, s, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
s_ -> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) a) ())
-> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either s (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
s', s
s_)
    next' (Right (a
a, s
sa, s
s_)) =
      s -> m (Step s ((,) b) ())
next_ s
s_ m (Step s ((,) b) ())
-> (Step s ((,) b) () -> Step (Either s (a, s, s)) ((,) a) ())
-> m (Step (Either s (a, s, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done ()
r -> () -> Step (Either s (a, s, s)) ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
r
        Skip s
s' -> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) a) ())
-> Either s (a, s, s) -> Step (Either s (a, s, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (a, s, s) -> Either s (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
sa, s
s')
        Yield (b
_, s
s') -> (a, Either s (a, s, s)) -> Step (Either s (a, s, s)) ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, (a, s, s) -> Either s (a, s, s)
forall a b. b -> Either a b
Right (a
a, s
sa, s
s'))

instance Monad m => Alternative (ListZerem () m) where
  empty :: forall a. ListZerem () m a
empty = Zerem ((,) a) m () -> ListZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) a) m () -> ListZerem () m a)
-> Zerem ((,) a) m () -> ListZerem () m a
forall a b. (a -> b) -> a -> b
$ (() -> m (Step () ((,) a) ())) -> m () -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (m (Step () ((,) a) ()) -> () -> m (Step () ((,) a) ())
forall a b. a -> b -> a
const (Step () ((,) a) () -> m (Step () ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step () ((,) a) () -> m (Step () ((,) a) ()))
-> Step () ((,) a) () -> m (Step () ((,) a) ())
forall a b. (a -> b) -> a -> b
$ () -> Step () ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ())) (() -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())
  (ListZerem Zerem ((,) a) m ()
zl) <|> :: forall a. ListZerem () m a -> ListZerem () m a -> ListZerem () m a
<|> (ListZerem Zerem ((,) a) m ()
zr) = Zerem ((,) a) m () -> ListZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) a) m () -> ListZerem () m a)
-> Zerem ((,) a) m () -> ListZerem () m a
forall a b. (a -> b) -> a -> b
$ Zerem ((,) a) m ()
zl Zerem ((,) a) m () -> Zerem ((,) a) m () -> Zerem ((,) a) m ()
forall a b.
Zerem ((,) a) m a -> Zerem ((,) a) m b -> Zerem ((,) a) m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Zerem ((,) a) m ()
zr

instance Monad m => Monad (ListZerem () m) where
  (ListZerem (Zerem s -> m (Step s ((,) a) ())
nexta m s
msa)) >>= :: forall a b.
ListZerem () m a -> (a -> ListZerem () m b) -> ListZerem () m b
>>= a -> ListZerem () m b
k = Zerem ((,) b) m () -> ListZerem () m b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem ((Either s (s, InitZerem ((,) b) m ())
 -> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()))
-> m (Either s (s, InitZerem ((,) b) m ())) -> Zerem ((,) b) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either s (s, InitZerem ((,) b) m ())
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
next (s -> Either s (s, InitZerem ((,) b) m ())
forall a b. a -> Either a b
Left (s -> Either s (s, InitZerem ((,) b) m ()))
-> m s -> m (Either s (s, InitZerem ((,) b) m ()))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msa)) where
    next :: Either s (s, InitZerem ((,) b) m ())
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
next (Left s
sa) =
      s -> m (Step s ((,) a) ())
nexta s
sa m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()))
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done () -> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
 -> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()))
-> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
forall a b. (a -> b) -> a -> b
$ () -> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
        Skip s
s' -> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
 -> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()))
-> (Either s (s, InitZerem ((,) b) m ())
    -> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
-> Either s (s, InitZerem ((,) b) m ())
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either s (s, InitZerem ((,) b) m ())
-> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (s, InitZerem ((,) b) m ())
 -> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()))
-> Either s (s, InitZerem ((,) b) m ())
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
forall a b. (a -> b) -> a -> b
$ s -> Either s (s, InitZerem ((,) b) m ())
forall a b. a -> Either a b
Left s
s'
        Yield (a
a, s
s') -> case a -> ListZerem () m b
k a
a of -- RIP Fusion
          ListZerem Zerem ((,) b) m ()
zb -> (\InitZerem ((,) b) m ()
zb -> Either s (s, InitZerem ((,) b) m ())
-> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (s, InitZerem ((,) b) m ())
 -> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
-> Either s (s, InitZerem ((,) b) m ())
-> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ (s, InitZerem ((,) b) m ()) -> Either s (s, InitZerem ((,) b) m ())
forall a b. b -> Either a b
Right (s
s', InitZerem ((,) b) m ()
zb)) (InitZerem ((,) b) m ()
 -> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
-> m (InitZerem ((,) b) m ())
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Zerem ((,) b) m () -> m (InitZerem ((,) b) m ())
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m r -> m (InitZerem f m r)
initZerem Zerem ((,) b) m ()
zb
    next (Right (s
sa, InitZerem s -> m (Step s ((,) b) ())
nextb s
sb)) =
      s -> m (Step s ((,) b) ())
nextb s
sb m (Step s ((,) b) ())
-> (Step s ((,) b) ()
    -> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
-> m (Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> Either s (s, InitZerem ((,) b) m ())
-> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (s, InitZerem ((,) b) m ())
 -> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
-> Either s (s, InitZerem ((,) b) m ())
-> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ s -> Either s (s, InitZerem ((,) b) m ())
forall a b. a -> Either a b
Left s
sa
        Skip s
s' -> Either s (s, InitZerem ((,) b) m ())
-> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either s (s, InitZerem ((,) b) m ())
 -> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ())
-> Either s (s, InitZerem ((,) b) m ())
-> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ (s, InitZerem ((,) b) m ()) -> Either s (s, InitZerem ((,) b) m ())
forall a b. b -> Either a b
Right (s
sa, (s -> m (Step s ((,) b) ())) -> s -> InitZerem ((,) b) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> m (Step s ((,) b) ())
nextb s
s')
        Yield (b
b, s
s') -> (b, Either s (s, InitZerem ((,) b) m ()))
-> Step (Either s (s, InitZerem ((,) b) m ())) ((,) b) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (b
b, (s, InitZerem ((,) b) m ()) -> Either s (s, InitZerem ((,) b) m ())
forall a b. b -> Either a b
Right (s
sa, (s -> m (Step s ((,) b) ())) -> s -> InitZerem ((,) b) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> m (Step s ((,) b) ())
nextb s
s'))

  >> :: forall a b.
ListZerem () m a -> ListZerem () m b -> ListZerem () m b
(>>) = ListZerem () m a -> ListZerem () m b -> ListZerem () m b
forall a b.
ListZerem () m a -> ListZerem () m b -> ListZerem () m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)

instance Monad m => MonadPlus (ListZerem () m)

instance Monad m => MonadLogic (ListZerem () m) where
  msplit :: forall a.
ListZerem () m a -> ListZerem () m (Maybe (a, ListZerem () m a))
msplit (ListZerem (Zerem s -> m (Step s ((,) a) ())
next m s
s)) = m (Maybe (a, ListZerem () m a))
-> ListZerem () m (Maybe (a, ListZerem () m a))
forall (m :: * -> *) a. Monad m => m a -> ListZerem () m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Maybe (a, ListZerem () m a))
 -> ListZerem () m (Maybe (a, ListZerem () m a)))
-> m (Maybe (a, ListZerem () m a))
-> ListZerem () m (Maybe (a, ListZerem () m a))
forall a b. (a -> b) -> a -> b
$ s -> m (Maybe (a, ListZerem () m a))
loop (s -> m (Maybe (a, ListZerem () m a)))
-> m s -> m (Maybe (a, ListZerem () m a))
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
    loop :: s -> m (Maybe (a, ListZerem () m a))
loop s
s =
      s -> m (Step s ((,) a) ())
next s
s m (Step s ((,) a) ())
-> (Step s ((,) a) () -> m (Maybe (a, ListZerem () m a)))
-> m (Maybe (a, ListZerem () m a))
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done () -> Maybe (a, ListZerem () m a) -> m (Maybe (a, ListZerem () m a))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (a, ListZerem () m a)
forall a. Maybe a
Nothing
        Skip s
s' -> s -> m (Maybe (a, ListZerem () m a))
loop s
s'
        Yield (a, s)
fs' -> Maybe (a, ListZerem () m a) -> m (Maybe (a, ListZerem () m a))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (a, ListZerem () m a) -> m (Maybe (a, ListZerem () m a)))
-> ((a, ListZerem () m a) -> Maybe (a, ListZerem () m a))
-> (a, ListZerem () m a)
-> m (Maybe (a, ListZerem () m a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a, ListZerem () m a) -> Maybe (a, ListZerem () m a)
forall a. a -> Maybe a
Just ((a, ListZerem () m a) -> m (Maybe (a, ListZerem () m a)))
-> (a, ListZerem () m a) -> m (Maybe (a, ListZerem () m a))
forall a b. (a -> b) -> a -> b
$
          (s -> ListZerem () m a) -> (a, s) -> (a, ListZerem () m a)
forall a b. (a -> b) -> (a, a) -> (a, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Zerem ((,) a) m () -> ListZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) a) m () -> ListZerem () m a)
-> (s -> Zerem ((,) a) m ()) -> s -> ListZerem () m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (s -> m (Step s ((,) a) ())) -> m s -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem s -> m (Step s ((,) a) ())
next (m s -> Zerem ((,) a) m ())
-> (s -> m s) -> s -> Zerem ((,) a) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> m s
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure) (a, s)
fs'
  
  interleave :: forall a. ListZerem () m a -> ListZerem () m a -> ListZerem () m a
interleave (ListZerem (Zerem s -> m (Step s ((,) a) ())
nextl m s
msl)) (ListZerem (Zerem s -> m (Step s ((,) a) ())
nextr m s
msr))
    = Zerem ((,) a) m () -> ListZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) a) m () -> ListZerem () m a)
-> Zerem ((,) a) m () -> ListZerem () m a
forall a b. (a -> b) -> a -> b
$ (Either (s, Maybe (m s)) (Maybe s, s)
 -> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()))
-> m (Either (s, Maybe (m s)) (Maybe s, s)) -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Either (s, Maybe (m s)) (Maybe s, s)
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
next ((\s
sl -> (s, Maybe (m s)) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. a -> Either a b
Left (s
sl, m s -> Maybe (m s)
forall a. a -> Maybe a
Just m s
msr)) (s -> Either (s, Maybe (m s)) (Maybe s, s))
-> m s -> m (Either (s, Maybe (m s)) (Maybe s, s))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msl) where
    next :: Either (s, Maybe (m s)) (Maybe s, s)
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
next (Left (s
sl, Just m s
msr)) =
      s -> m (Step s ((,) a) ())
nextl s
sl m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()))
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
       Done () -> m s
msr m s
-> (s -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sr -> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Maybe (m s)) (Maybe s, s)
 -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (Maybe s, s) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. b -> Either a b
Right (Maybe s
forall a. Maybe a
Nothing, s
sr)
       Skip s
s' -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
 -> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()))
-> (Either (s, Maybe (m s)) (Maybe s, s)
    -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> Either (s, Maybe (m s)) (Maybe s, s)
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Maybe (m s)) (Maybe s, s)
 -> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()))
-> Either (s, Maybe (m s)) (Maybe s, s)
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
forall a b. (a -> b) -> a -> b
$ (s, Maybe (m s)) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. a -> Either a b
Left (s
s', m s -> Maybe (m s)
forall a. a -> Maybe a
Just m s
msr)
       Yield (a
a, s
s') -> m s
msr m s
-> (s -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sr -> (a, Either (s, Maybe (m s)) (Maybe s, s))
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, (Maybe s, s) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. b -> Either a b
Right (s -> Maybe s
forall a. a -> Maybe a
Just s
s', s
sr))
    next (Left (s
sl, Maybe (m s)
Nothing)) =
      s -> m (Step s ((,) a) ())
nextl s
sl m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
       Done () -> () -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
       Skip s
s' -> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Maybe (m s)) (Maybe s, s)
 -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (s, Maybe (m s)) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. a -> Either a b
Left (s
s', Maybe (m s)
forall a. Maybe a
Nothing)
       Yield (a
a, s
s') -> (a, Either (s, Maybe (m s)) (Maybe s, s))
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, (s, Maybe (m s)) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. a -> Either a b
Left (s
s', Maybe (m s)
forall a. Maybe a
Nothing))
    next (Right (Just s
sl, s
sr)) =
      s -> m (Step s ((,) a) ())
nextr s
sr m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Maybe (m s)) (Maybe s, s)
 -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (s, Maybe (m s)) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. a -> Either a b
Left (s
sl, Maybe (m s)
forall a. Maybe a
Nothing)
        Skip s
s' -> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Maybe (m s)) (Maybe s, s)
 -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (Maybe s, s) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. b -> Either a b
Right (s -> Maybe s
forall a. a -> Maybe a
Just s
sl, s
s')
        Yield (a
a, s
s') -> (a, Either (s, Maybe (m s)) (Maybe s, s))
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, (s, Maybe (m s)) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. a -> Either a b
Left (s
sl, m s -> Maybe (m s)
forall a. a -> Maybe a
Just (m s -> Maybe (m s)) -> m s -> Maybe (m s)
forall a b. (a -> b) -> a -> b
$ s -> m s
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure s
s'))
    next (Right (Maybe s
Nothing, s
sr)) =
      s -> m (Step s ((,) a) ())
nextr s
sr m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> m (Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> () -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
        Skip s
s' -> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Either (s, Maybe (m s)) (Maybe s, s)
 -> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ())
-> Either (s, Maybe (m s)) (Maybe s, s)
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall a b. (a -> b) -> a -> b
$ (Maybe s, s) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. b -> Either a b
Right (Maybe s
forall a. Maybe a
Nothing, s
s')
        Yield (a
a, s
s') -> (a, Either (s, Maybe (m s)) (Maybe s, s))
-> Step (Either (s, Maybe (m s)) (Maybe s, s)) ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, (Maybe s, s) -> Either (s, Maybe (m s)) (Maybe s, s)
forall a b. b -> Either a b
Right (Maybe s
forall a. Maybe a
Nothing, s
s'))

  once :: forall a. ListZerem () m a -> ListZerem () m a
once (ListZerem Zerem ((,) a) m ()
z) = Zerem ((,) a) m () -> ListZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) a) m () -> ListZerem () m a)
-> Zerem ((,) a) m () -> ListZerem () m a
forall a b. (a -> b) -> a -> b
$ Int -> Zerem ((,) a) m () -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r.
(Functor f, Applicative m) =>
Int -> Zerem f m r -> Zerem f m ()
Zerem.take Int
1 Zerem ((,) a) m ()
z

  lnot :: forall a. ListZerem () m a -> ListZerem () m ()
lnot (ListZerem (Zerem s -> m (Step s ((,) a) ())
next m s
s)) = Zerem ((,) ()) m () -> ListZerem () m ()
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) ()) m () -> ListZerem () m ())
-> Zerem ((,) ()) m () -> ListZerem () m ()
forall a b. (a -> b) -> a -> b
$ (Maybe s -> m (Step (Maybe s) ((,) ()) ()))
-> m (Maybe s) -> Zerem ((,) ()) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem Maybe s -> m (Step (Maybe s) ((,) ()) ())
next' (s -> Maybe s
forall a. a -> Maybe a
Just (s -> Maybe s) -> m s -> m (Maybe s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s) where
    next' :: Maybe s -> m (Step (Maybe s) ((,) ()) ())
next' (Just s
s) =
      s -> m (Step s ((,) a) ())
next s
s m (Step s ((,) a) ())
-> (Step s ((,) a) () -> Step (Maybe s) ((,) ()) ())
-> m (Step (Maybe s) ((,) ()) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> ((), Maybe s) -> Step (Maybe s) ((,) ()) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield ((), Maybe s
forall a. Maybe a
Nothing)
        Skip s
s' -> Maybe s -> Step (Maybe s) ((,) ()) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Maybe s
forall a. a -> Maybe a
Just s
s')
        Yield (a, s)
_ -> () -> Step (Maybe s) ((,) ()) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
    next' Maybe s
Nothing = Step (Maybe s) ((,) ()) () -> m (Step (Maybe s) ((,) ()) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Maybe s) ((,) ()) () -> m (Step (Maybe s) ((,) ()) ()))
-> Step (Maybe s) ((,) ()) () -> m (Step (Maybe s) ((,) ()) ())
forall a b. (a -> b) -> a -> b
$ () -> Step (Maybe s) ((,) ()) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()

  ifte :: forall a b.
ListZerem () m a
-> (a -> ListZerem () m b) -> ListZerem () m b -> ListZerem () m b
ifte (ListZerem (Zerem s -> m (Step s ((,) a) ())
nexta m s
msa)) a -> ListZerem () m b
k (ListZerem (Zerem s -> m (Step s ((,) b) ())
nextb m s
msb))
    = Zerem ((,) b) m () -> ListZerem () m b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) b) m () -> ListZerem () m b)
-> Zerem ((,) b) m () -> ListZerem () m b
forall a b. (a -> b) -> a -> b
$ (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
 -> m (Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()))
-> m (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any)
-> Zerem ((,) b) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
next (s -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sa -> ListZeremIfteStates sa sb zb a
IfteInit (s -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any)
-> m s -> m (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
msa) where
    next :: ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
next (IfteInit s
sa) =
      s -> m (Step s ((,) a) ())
nexta s
sa m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> m (Step
            (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()))
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done () -> m s
msb m s
-> (s
    -> Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \s
sb -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
 -> Step
      (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ s -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sb -> ListZeremIfteStates sa sb zb a
IfteFail s
sb
        Skip s
s' -> Step
  (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step
   (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
 -> m (Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()))
-> (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
    -> Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
 -> m (Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()))
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall a b. (a -> b) -> a -> b
$ s -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sa -> ListZeremIfteStates sa sb zb a
IfteInit s
s'
        Yield (a
a, s
s') -> case a -> ListZerem () m b
k a
a of -- RIP fusion
          ListZerem Zerem ((,) b) m ()
z -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
 -> Step
      (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> (InitZerem ((,) b) m ()
    -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any)
-> InitZerem ((,) b) m ()
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s
-> InitZerem ((,) b) m ()
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sa -> zb -> ListZeremIfteStates sa sb zb a
IfteSucc s
s' (InitZerem ((,) b) m ()
 -> Step
      (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> m (InitZerem ((,) b) m ())
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Zerem ((,) b) m () -> m (InitZerem ((,) b) m ())
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m r -> m (InitZerem f m r)
initZerem Zerem ((,) b) m ()
z
    next (IfteFail s
sb) =
      s -> m (Step s ((,) b) ())
nextb s
sb m (Step s ((,) b) ())
-> (Step s ((,) b) ()
    -> Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> ()
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
        Skip s
s' -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
 -> Step
      (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ s -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sb -> ListZeremIfteStates sa sb zb a
IfteFail s
s'
        Yield (b
b, s
s') -> (b, ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any)
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (b
b, s -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sb -> ListZeremIfteStates sa sb zb a
IfteFail s
s')
    next (IfteSucc s
sa (InitZerem s -> m (Step s ((,) b) ())
nextb s
sb)) =
      s -> m (Step s ((,) b) ())
nextb s
sb m (Step s ((,) b) ())
-> (Step s ((,) b) ()
    -> Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done () -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
 -> Step
      (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ s -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sa -> ListZeremIfteStates sa sb zb a
IfteCont s
sa
        Skip s
s' -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
 -> Step
      (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall a b. (a -> b) -> a -> b
$ s
-> InitZerem ((,) b) m ()
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sa -> zb -> ListZeremIfteStates sa sb zb a
IfteSucc s
sa ((s -> m (Step s ((,) b) ())) -> s -> InitZerem ((,) b) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> m (Step s ((,) b) ())
nextb s
s')
        Yield (b
b, s
s') -> (b, ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any)
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (b
b, s
-> InitZerem ((,) b) m ()
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sa -> zb -> ListZeremIfteStates sa sb zb a
IfteSucc s
sa ((s -> m (Step s ((,) b) ())) -> s -> InitZerem ((,) b) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> s -> InitZerem f m r
InitZerem s -> m (Step s ((,) b) ())
nextb s
s'))
    next (IfteCont s
sa) =
      s -> m (Step s ((,) a) ())
nexta s
sa m (Step s ((,) a) ())
-> (Step s ((,) a) ()
    -> m (Step
            (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()))
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done () -> Step
  (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step
   (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
 -> m (Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()))
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall a b. (a -> b) -> a -> b
$ ()
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
        Skip s
s' -> Step
  (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step
   (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
 -> m (Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()))
-> (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
    -> Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
 -> m (Step
         (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()))
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall a b. (a -> b) -> a -> b
$ s -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sa -> ListZeremIfteStates sa sb zb a
IfteCont s
s'
        Yield (a
a, s
s') -> case a -> ListZerem () m b
k a
a of
          ListZerem Zerem ((,) b) m ()
z -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
 -> Step
      (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> (InitZerem ((,) b) m ()
    -> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any)
-> InitZerem ((,) b) m ()
-> Step
     (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s
-> InitZerem ((,) b) m ()
-> ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any
forall sa sb zb a. sa -> zb -> ListZeremIfteStates sa sb zb a
IfteSucc s
s' (InitZerem ((,) b) m ()
 -> Step
      (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
-> m (InitZerem ((,) b) m ())
-> m (Step
        (ListZeremIfteStates s s (InitZerem ((,) b) m ()) Any) ((,) b) ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Zerem ((,) b) m () -> m (InitZerem ((,) b) m ())
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m r -> m (InitZerem f m r)
initZerem Zerem ((,) b) m ()
z

data ListZeremIfteStates sa sb zb a
  = IfteInit sa
  | IfteFail sb
  | IfteSucc sa zb
  | IfteCont sa

instance MonadTrans (ListZerem ()) where
  lift :: forall (m :: * -> *) a. Monad m => m a -> ListZerem () m a
lift m a
ma = Zerem ((,) a) m () -> ListZerem () m a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem ((Maybe a -> m (Step (Maybe a) ((,) a) ()))
-> m (Maybe a) -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Step (Maybe a) ((,) a) () -> m (Step (Maybe a) ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Maybe a) ((,) a) () -> m (Step (Maybe a) ((,) a) ()))
-> (Maybe a -> Step (Maybe a) ((,) a) ())
-> Maybe a
-> m (Step (Maybe a) ((,) a) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Maybe a -> Step (Maybe a) ((,) a) ()
forall {a} {a}. Maybe a -> Step (Maybe a) ((,) a) ()
next) (a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> m a -> m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
ma)) where
    next :: Maybe a -> Step (Maybe a) ((,) a) ()
next Maybe a
Nothing = () -> Step (Maybe a) ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
    next (Just a
a) = (a, Maybe a) -> Step (Maybe a) ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, Maybe a
forall a. Maybe a
Nothing)

instance MFunctor (ListZerem r) where
  hoist :: forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
(forall a. m a -> n a) -> ListZerem r m b -> ListZerem r n b
hoist forall a. m a -> n a
f (ListZerem (Zerem s -> m (Step s ((,) b) r)
next m s
s)) = Zerem ((,) b) n r -> ListZerem r n b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) b) n r -> ListZerem r n b)
-> Zerem ((,) b) n r -> ListZerem r n b
forall a b. (a -> b) -> a -> b
$ (s -> n (Step s ((,) b) r)) -> n s -> Zerem ((,) b) n r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (m (Step s ((,) b) r) -> n (Step s ((,) b) r)
forall a. m a -> n a
f (m (Step s ((,) b) r) -> n (Step s ((,) b) r))
-> (s -> m (Step s ((,) b) r)) -> s -> n (Step s ((,) b) r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> m (Step s ((,) b) r)
next) (m s -> n s
forall a. m a -> n a
f m s
s)

instance Comonad (ListZerem Void Identity) where
  extract :: forall a. ListZerem Void Identity a -> a
extract (ListZerem (Zerem s -> Identity (Step s ((,) a) Void)
next Identity s
ms)) = Identity a -> a
forall a. Identity a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (Identity a -> a) -> Identity a -> a
forall a b. (a -> b) -> a -> b
$ s -> Identity a
loop (s -> Identity a) -> Identity s -> Identity a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Identity s
ms where
    loop :: s -> Identity a
loop s
s =
      s -> Identity (Step s ((,) a) Void)
next s
s Identity (Step s ((,) a) Void)
-> (Step s ((,) a) Void -> Identity a) -> Identity a
forall a b. Identity a -> (a -> Identity b) -> Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Done Void
v -> Void -> Identity a
forall a. Void -> a
absurd Void
v
        Skip s
s' -> s -> Identity a
loop s
s'
        Yield (a
a, s
_) -> a -> Identity a
forall a. a -> Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a

  duplicate :: forall a.
ListZerem Void Identity a
-> ListZerem Void Identity (ListZerem Void Identity a)
duplicate (ListZerem z :: Zerem ((,) a) Identity Void
z@(Zerem s -> Identity (Step s ((,) a) Void)
next Identity s
ms)) = Zerem ((,) (ListZerem Void Identity a)) Identity Void
-> ListZerem Void Identity (ListZerem Void Identity a)
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) (ListZerem Void Identity a)) Identity Void
 -> ListZerem Void Identity (ListZerem Void Identity a))
-> Zerem ((,) (ListZerem Void Identity a)) Identity Void
-> ListZerem Void Identity (ListZerem Void Identity a)
forall a b. (a -> b) -> a -> b
$ ((Int, s)
 -> Identity (Step (Int, s) ((,) (ListZerem Void Identity a)) Void))
-> Identity (Int, s)
-> Zerem ((,) (ListZerem Void Identity a)) Identity Void
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Int, s)
-> Identity (Step (Int, s) ((,) (ListZerem Void Identity a)) Void)
next' ((Int
0 :: Int,) (s -> (Int, s)) -> Identity s -> Identity (Int, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Identity s
ms) where
    next' :: (Int, s)
-> Identity (Step (Int, s) ((,) (ListZerem Void Identity a)) Void)
next' (Int
n, s
s) =
      s -> Identity (Step s ((,) a) Void)
next s
s Identity (Step s ((,) a) Void)
-> (Step s ((,) a) Void
    -> Step (Int, s) ((,) (ListZerem Void Identity a)) Void)
-> Identity (Step (Int, s) ((,) (ListZerem Void Identity a)) Void)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
        Done Void
v -> Void -> Step (Int, s) ((,) (ListZerem Void Identity a)) Void
forall a. Void -> a
absurd Void
v
        Skip s
s' -> (Int, s) -> Step (Int, s) ((,) (ListZerem Void Identity a)) Void
forall s (f :: * -> *) r. s -> Step s f r
Skip (Int
n, s
s')
        Yield (a
_, s
s') -> (ListZerem Void Identity a, (Int, s))
-> Step (Int, s) ((,) (ListZerem Void Identity a)) Void
forall s (f :: * -> *) r. f s -> Step s f r
Yield (Zerem ((,) a) Identity Void -> ListZerem Void Identity a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) a) Identity Void -> ListZerem Void Identity a)
-> Zerem ((,) a) Identity Void -> ListZerem Void Identity a
forall a b. (a -> b) -> a -> b
$ Int -> Zerem ((,) a) Identity Void -> Zerem ((,) a) Identity Void
forall (f :: * -> *) (m :: * -> *) r.
(Comonad f, Functor m) =>
Int -> Zerem f m r -> Zerem f m r
Zerem.drop Int
n Zerem ((,) a) Identity Void
z, (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1, s
s'))

instance Foldable (ListZerem r Identity) where
  foldr :: forall a b. (a -> b -> b) -> b -> ListZerem r Identity a -> b
foldr a -> b -> b
f b
z (ListZerem Zerem ((,) a) Identity r
stream) = Identity b -> b
forall a. Identity a -> a
runIdentity (Identity b -> b) -> Identity b -> b
forall a b. (a -> b) -> a -> b
$ (a -> b -> b) -> b -> Zerem ((,) a) Identity r -> Identity b
forall (m :: * -> *) a b r.
Monad m =>
(a -> b -> b) -> b -> Zerem ((,) a) m r -> m b
foldr_ a -> b -> b
f b
z Zerem ((,) a) Identity r
stream
  {-# INLINE foldr #-}

  foldl' :: forall b a. (b -> a -> b) -> b -> ListZerem r Identity a -> b
foldl' b -> a -> b
f b
z (ListZerem Zerem ((,) a) Identity r
stream) = Identity b -> b
forall a. Identity a -> a
runIdentity (Identity b -> b) -> Identity b -> b
forall a b. (a -> b) -> a -> b
$ (b -> a -> b) -> b -> Zerem ((,) a) Identity r -> Identity b
forall (m :: * -> *) b a r.
Monad m =>
(b -> a -> b) -> b -> Zerem ((,) a) m r -> m b
foldl_ b -> a -> b
f b
z Zerem ((,) a) Identity r
stream
  {-# INLINE foldl' #-}

instance Traversable (ListZerem () Identity) where
  traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b)
-> ListZerem () Identity a -> f (ListZerem () Identity b)
traverse a -> f b
f = ([b] -> ListZerem () Identity b)
-> f [b] -> f (ListZerem () Identity b)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Zerem ((,) b) Identity () -> ListZerem () Identity b
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ListZerem r m a
ListZerem (Zerem ((,) b) Identity () -> ListZerem () Identity b)
-> ([b] -> Zerem ((,) b) Identity ())
-> [b]
-> ListZerem () Identity b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [b] -> Zerem ((,) b) Identity ()
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Applicative m) =>
f a -> Zerem ((,) a) m ()
yieldMany) (f [b] -> f (ListZerem () Identity b))
-> (ListZerem () Identity a -> f [b])
-> ListZerem () Identity a
-> f (ListZerem () Identity b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a -> f b) -> [a] -> f [b]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse a -> f b
f ([a] -> f [b])
-> (ListZerem () Identity a -> [a])
-> ListZerem () Identity a
-> f [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ListZerem () Identity a -> [a]
forall a. ListZerem () Identity a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
  {-# INLINE traverse #-}

newtype Pipe u v f g m p a b = Pipe { forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe :: p (Zerem (f a) m u) (Zerem (g b) m v) }

-- type Producer u v g m p b = forall f a. Pipe u v f g m p a b
-- type Consumer u v f m p a = forall g b. Pipe u v f g m p a b

instance Category p => Category (Pipe u u f f m p) where
  id :: forall a. Pipe u u f f m p a a
id = p (Zerem (f a) m u) (Zerem (f a) m u) -> Pipe u u f f m p a a
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe p (Zerem (f a) m u) (Zerem (f a) m u)
forall a. p a a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
  (Pipe p (Zerem (f b) m u) (Zerem (f c) m u)
p2) . :: forall b c a.
Pipe u u f f m p b c
-> Pipe u u f f m p a b -> Pipe u u f f m p a c
. (Pipe p (Zerem (f a) m u) (Zerem (f b) m u)
p1) = p (Zerem (f a) m u) (Zerem (f c) m u) -> Pipe u u f f m p a c
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe (p (Zerem (f a) m u) (Zerem (f c) m u) -> Pipe u u f f m p a c)
-> p (Zerem (f a) m u) (Zerem (f c) m u) -> Pipe u u f f m p a c
forall a b. (a -> b) -> a -> b
$ p (Zerem (f b) m u) (Zerem (f c) m u)
p2 p (Zerem (f b) m u) (Zerem (f c) m u)
-> p (Zerem (f a) m u) (Zerem (f b) m u)
-> p (Zerem (f a) m u) (Zerem (f c) m u)
forall b c a. p b c -> p a b -> p a c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. p (Zerem (f a) m u) (Zerem (f b) m u)
p1

instance (Profunctor p, Bifunctor g, Functor m) => Functor (Pipe u v f g m p a) where
  fmap :: forall a b.
(a -> b) -> Pipe u v f g m p a a -> Pipe u v f g m p a b
fmap a -> b
f = p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe (p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b)
-> (p (Zerem (f a) m u) (Zerem (g a) m v)
    -> p (Zerem (f a) m u) (Zerem (g b) m v))
-> p (Zerem (f a) m u) (Zerem (g a) m v)
-> Pipe u v f g m p a b
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. (Zerem (g a) m v -> Zerem (g b) m v)
-> p (Zerem (f a) m u) (Zerem (g a) m v)
-> p (Zerem (f a) m u) (Zerem (g b) m v)
forall b c a. (b -> c) -> p a b -> p a c
forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap ((a -> b) -> Zerem (g a) m v -> Zerem (g b) m v
forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map a -> b
f) (p (Zerem (f a) m u) (Zerem (g a) m v) -> Pipe u v f g m p a b)
-> (Pipe u v f g m p a a -> p (Zerem (f a) m u) (Zerem (g a) m v))
-> Pipe u v f g m p a a
-> Pipe u v f g m p a b
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Pipe u v f g m p a a -> p (Zerem (f a) m u) (Zerem (g a) m v)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe

instance (Profunctor p, Bifunctor f, Bifunctor g, Functor m) => Profunctor (Pipe u v f g m p) where
  dimap :: forall a b c d.
(a -> b)
-> (c -> d) -> Pipe u v f g m p b c -> Pipe u v f g m p a d
dimap a -> b
lf c -> d
rf = p (Zerem (f a) m u) (Zerem (g d) m v) -> Pipe u v f g m p a d
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe (p (Zerem (f a) m u) (Zerem (g d) m v) -> Pipe u v f g m p a d)
-> (p (Zerem (f b) m u) (Zerem (g c) m v)
    -> p (Zerem (f a) m u) (Zerem (g d) m v))
-> p (Zerem (f b) m u) (Zerem (g c) m v)
-> Pipe u v f g m p a d
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. (Zerem (f a) m u -> Zerem (f b) m u)
-> (Zerem (g c) m v -> Zerem (g d) m v)
-> p (Zerem (f b) m u) (Zerem (g c) m v)
-> p (Zerem (f a) m u) (Zerem (g d) m v)
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap ((a -> b) -> Zerem (f a) m u -> Zerem (f b) m u
forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map a -> b
lf) ((c -> d) -> Zerem (g c) m v -> Zerem (g d) m v
forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map c -> d
rf) (p (Zerem (f b) m u) (Zerem (g c) m v) -> Pipe u v f g m p a d)
-> (Pipe u v f g m p b c -> p (Zerem (f b) m u) (Zerem (g c) m v))
-> Pipe u v f g m p b c
-> Pipe u v f g m p a d
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Pipe u v f g m p b c -> p (Zerem (f b) m u) (Zerem (g c) m v)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe
  lmap :: forall a b c.
(a -> b) -> Pipe u v f g m p b c -> Pipe u v f g m p a c
lmap a -> b
lf = p (Zerem (f a) m u) (Zerem (g c) m v) -> Pipe u v f g m p a c
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe (p (Zerem (f a) m u) (Zerem (g c) m v) -> Pipe u v f g m p a c)
-> (p (Zerem (f b) m u) (Zerem (g c) m v)
    -> p (Zerem (f a) m u) (Zerem (g c) m v))
-> p (Zerem (f b) m u) (Zerem (g c) m v)
-> Pipe u v f g m p a c
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. (Zerem (f a) m u -> Zerem (f b) m u)
-> p (Zerem (f b) m u) (Zerem (g c) m v)
-> p (Zerem (f a) m u) (Zerem (g c) m v)
forall a b c. (a -> b) -> p b c -> p a c
forall (p :: * -> * -> *) a b c.
Profunctor p =>
(a -> b) -> p b c -> p a c
lmap ((a -> b) -> Zerem (f a) m u -> Zerem (f b) m u
forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map a -> b
lf) (p (Zerem (f b) m u) (Zerem (g c) m v) -> Pipe u v f g m p a c)
-> (Pipe u v f g m p b c -> p (Zerem (f b) m u) (Zerem (g c) m v))
-> Pipe u v f g m p b c
-> Pipe u v f g m p a c
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Pipe u v f g m p b c -> p (Zerem (f b) m u) (Zerem (g c) m v)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe
  rmap :: forall b c a.
(b -> c) -> Pipe u v f g m p a b -> Pipe u v f g m p a c
rmap b -> c
rf = p (Zerem (f a) m u) (Zerem (g c) m v) -> Pipe u v f g m p a c
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe (p (Zerem (f a) m u) (Zerem (g c) m v) -> Pipe u v f g m p a c)
-> (p (Zerem (f a) m u) (Zerem (g b) m v)
    -> p (Zerem (f a) m u) (Zerem (g c) m v))
-> p (Zerem (f a) m u) (Zerem (g b) m v)
-> Pipe u v f g m p a c
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. (Zerem (g b) m v -> Zerem (g c) m v)
-> p (Zerem (f a) m u) (Zerem (g b) m v)
-> p (Zerem (f a) m u) (Zerem (g c) m v)
forall b c a. (b -> c) -> p a b -> p a c
forall (p :: * -> * -> *) b c a.
Profunctor p =>
(b -> c) -> p a b -> p a c
rmap ((b -> c) -> Zerem (g b) m v -> Zerem (g c) m v
forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map b -> c
rf) (p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a c)
-> (Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v))
-> Pipe u v f g m p a b
-> Pipe u v f g m p a c
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe

instance (Profunctor p, Bifunctor g, Functor m) => Choice (Pipe u v (,) g m p) where
  left' :: forall a b c.
Pipe u v (,) g m p a b
-> Pipe u v (,) g m p (Either a c) (Either b c)
left' = p (Zerem ((,) (Either a c)) m u) (Zerem (g (Either b c)) m v)
-> Pipe u v (,) g m p (Either a c) (Either b c)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe (p (Zerem ((,) (Either a c)) m u) (Zerem (g (Either b c)) m v)
 -> Pipe u v (,) g m p (Either a c) (Either b c))
-> (p (Zerem ((,) a) m u) (Zerem (g b) m v)
    -> p (Zerem ((,) (Either a c)) m u) (Zerem (g (Either b c)) m v))
-> p (Zerem ((,) a) m u) (Zerem (g b) m v)
-> Pipe u v (,) g m p (Either a c) (Either b c)
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. (Zerem ((,) (Either a c)) m u -> Zerem ((,) a) m u)
-> (Zerem (g b) m v -> Zerem (g (Either b c)) m v)
-> p (Zerem ((,) a) m u) (Zerem (g b) m v)
-> p (Zerem ((,) (Either a c)) m u) (Zerem (g (Either b c)) m v)
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap ((Either a c -> Maybe a)
-> Zerem ((,) (Either a c)) m u -> Zerem ((,) a) m u
forall (m :: * -> *) a b r.
Functor m =>
(a -> Maybe b) -> Zerem ((,) a) m r -> Zerem ((,) b) m r
mapMaybe Either a c -> Maybe a
forall a c. Either a c -> Maybe a
eitherToMaybe) ((b -> Either b c) -> Zerem (g b) m v -> Zerem (g (Either b c)) m v
forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map b -> Either b c
forall a b. a -> Either a b
Left) (p (Zerem ((,) a) m u) (Zerem (g b) m v)
 -> Pipe u v (,) g m p (Either a c) (Either b c))
-> (Pipe u v (,) g m p a b
    -> p (Zerem ((,) a) m u) (Zerem (g b) m v))
-> Pipe u v (,) g m p a b
-> Pipe u v (,) g m p (Either a c) (Either b c)
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Pipe u v (,) g m p a b -> p (Zerem ((,) a) m u) (Zerem (g b) m v)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe where
    eitherToMaybe :: Either a c -> Maybe a
    eitherToMaybe :: forall a c. Either a c -> Maybe a
eitherToMaybe = \case
      Left a
a -> a -> Maybe a
forall a. a -> Maybe a
Just a
a
      Right c
_c -> Maybe a
forall a. Maybe a
Nothing

  right' :: forall a b c.
Pipe u v (,) g m p a b
-> Pipe u v (,) g m p (Either c a) (Either c b)
right' = p (Zerem ((,) (Either c a)) m u) (Zerem (g (Either c b)) m v)
-> Pipe u v (,) g m p (Either c a) (Either c b)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe (p (Zerem ((,) (Either c a)) m u) (Zerem (g (Either c b)) m v)
 -> Pipe u v (,) g m p (Either c a) (Either c b))
-> (p (Zerem ((,) a) m u) (Zerem (g b) m v)
    -> p (Zerem ((,) (Either c a)) m u) (Zerem (g (Either c b)) m v))
-> p (Zerem ((,) a) m u) (Zerem (g b) m v)
-> Pipe u v (,) g m p (Either c a) (Either c b)
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. (Zerem ((,) (Either c a)) m u -> Zerem ((,) a) m u)
-> (Zerem (g b) m v -> Zerem (g (Either c b)) m v)
-> p (Zerem ((,) a) m u) (Zerem (g b) m v)
-> p (Zerem ((,) (Either c a)) m u) (Zerem (g (Either c b)) m v)
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap ((Either c a -> Maybe a)
-> Zerem ((,) (Either c a)) m u -> Zerem ((,) a) m u
forall (m :: * -> *) a b r.
Functor m =>
(a -> Maybe b) -> Zerem ((,) a) m r -> Zerem ((,) b) m r
mapMaybe Either c a -> Maybe a
forall c a. Either c a -> Maybe a
eitherToMaybe) ((b -> Either c b) -> Zerem (g b) m v -> Zerem (g (Either c b)) m v
forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map b -> Either c b
forall a b. b -> Either a b
Right) (p (Zerem ((,) a) m u) (Zerem (g b) m v)
 -> Pipe u v (,) g m p (Either c a) (Either c b))
-> (Pipe u v (,) g m p a b
    -> p (Zerem ((,) a) m u) (Zerem (g b) m v))
-> Pipe u v (,) g m p a b
-> Pipe u v (,) g m p (Either c a) (Either c b)
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Pipe u v (,) g m p a b -> p (Zerem ((,) a) m u) (Zerem (g b) m v)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe where
    eitherToMaybe :: Either c a -> Maybe a
    eitherToMaybe :: forall c a. Either c a -> Maybe a
eitherToMaybe = \case
      Left c
_c -> Maybe a
forall a. Maybe a
Nothing
      Right a
a -> a -> Maybe a
forall a. a -> Maybe a
Just a
a

instance ProfunctorFunctor (Pipe u v f g m) where
  promap :: forall (p :: * -> * -> *) (q :: * -> * -> *).
Profunctor p =>
(p :-> q) -> Pipe u v f g m p :-> Pipe u v f g m q
promap p :-> q
f = q (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m q a b
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe (q (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m q a b)
-> (p (Zerem (f a) m u) (Zerem (g b) m v)
    -> q (Zerem (f a) m u) (Zerem (g b) m v))
-> p (Zerem (f a) m u) (Zerem (g b) m v)
-> Pipe u v f g m q a b
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. p (Zerem (f a) m u) (Zerem (g b) m v)
-> q (Zerem (f a) m u) (Zerem (g b) m v)
p :-> q
f (p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m q a b)
-> (Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v))
-> Pipe u v f g m p a b
-> Pipe u v f g m q a b
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe

instance ProfunctorMonad (Pipe Void Void (,) (,) Identity) where
  proreturn :: forall (p :: * -> * -> *).
Profunctor p =>
p :-> Pipe Void Void (,) (,) Identity p
proreturn = p (Zerem ((,) a) Identity Void) (Zerem ((,) b) Identity Void)
-> Pipe Void Void (,) (,) Identity p a b
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe (p (Zerem ((,) a) Identity Void) (Zerem ((,) b) Identity Void)
 -> Pipe Void Void (,) (,) Identity p a b)
-> (p a b
    -> p (Zerem ((,) a) Identity Void) (Zerem ((,) b) Identity Void))
-> p a b
-> Pipe Void Void (,) (,) Identity p a b
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. (Zerem ((,) a) Identity Void -> a)
-> (b -> Zerem ((,) b) Identity Void)
-> p a b
-> p (Zerem ((,) a) Identity Void) (Zerem ((,) b) Identity Void)
forall a b c d. (a -> b) -> (c -> d) -> p b c -> p a d
forall (p :: * -> * -> *) a b c d.
Profunctor p =>
(a -> b) -> (c -> d) -> p b c -> p a d
dimap (ZipZerem Void Identity a -> a
forall a. ZipZerem Void Identity a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (ZipZerem Void Identity a -> a)
-> (Zerem ((,) a) Identity Void -> ZipZerem Void Identity a)
-> Zerem ((,) a) Identity Void
-> a
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Zerem ((,) a) Identity Void -> ZipZerem Void Identity a
forall r (m :: * -> *) a. Zerem ((,) a) m r -> ZipZerem r m a
ZipZerem) (ZipZerem Void Identity b -> Zerem ((,) b) Identity Void
forall r (m :: * -> *) a. ZipZerem r m a -> Zerem ((,) a) m r
runZipZerem (ZipZerem Void Identity b -> Zerem ((,) b) Identity Void)
-> (b -> ZipZerem Void Identity b)
-> b
-> Zerem ((,) b) Identity Void
forall c b a. Coercible c b => (b -> c) -> (a -> b) -> a -> c
#. b -> ZipZerem Void Identity b
forall a. a -> ZipZerem Void Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)

  projoin :: forall (p :: * -> * -> *).
Profunctor p =>
Pipe Void Void (,) (,) Identity (Pipe Void Void (,) (,) Identity p)
:-> Pipe Void Void (,) (,) Identity p
projoin =
    p (Zerem ((,) a) Identity Void) (Zerem ((,) b) Identity Void)
-> Pipe Void Void (,) (,) Identity p a b
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
p (Zerem (f a) m u) (Zerem (g b) m v) -> Pipe u v f g m p a b
Pipe
    #. dimap
      (map $ runZipZerem #. pure)
      ((runZipZerem #. join)
      . (ZipZerem #. map ZipZerem))
    (p (Zerem ((,) (Zerem ((,) a) Identity Void)) Identity Void)
   (Zerem ((,) (Zerem ((,) b) Identity Void)) Identity Void)
 -> Pipe Void Void (,) (,) Identity p a b)
-> (Pipe
      Void
      Void
      (,)
      (,)
      Identity
      p
      (Zerem ((,) a) Identity Void)
      (Zerem ((,) b) Identity Void)
    -> p (Zerem ((,) (Zerem ((,) a) Identity Void)) Identity Void)
         (Zerem ((,) (Zerem ((,) b) Identity Void)) Identity Void))
-> Pipe
     Void
     Void
     (,)
     (,)
     Identity
     p
     (Zerem ((,) a) Identity Void)
     (Zerem ((,) b) Identity Void)
-> Pipe Void Void (,) (,) Identity p a b
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Pipe
  Void
  Void
  (,)
  (,)
  Identity
  p
  (Zerem ((,) a) Identity Void)
  (Zerem ((,) b) Identity Void)
-> p (Zerem ((,) (Zerem ((,) a) Identity Void)) Identity Void)
     (Zerem ((,) (Zerem ((,) b) Identity Void)) Identity Void)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe
    (Pipe
   Void
   Void
   (,)
   (,)
   Identity
   p
   (Zerem ((,) a) Identity Void)
   (Zerem ((,) b) Identity Void)
 -> Pipe Void Void (,) (,) Identity p a b)
-> (Pipe
      Void Void (,) (,) Identity (Pipe Void Void (,) (,) Identity p) a b
    -> Pipe
         Void
         Void
         (,)
         (,)
         Identity
         p
         (Zerem ((,) a) Identity Void)
         (Zerem ((,) b) Identity Void))
-> Pipe
     Void Void (,) (,) Identity (Pipe Void Void (,) (,) Identity p) a b
-> Pipe Void Void (,) (,) Identity p a b
forall b a c. Coercible b a => (b -> c) -> (a -> b) -> a -> c
.# Pipe
  Void Void (,) (,) Identity (Pipe Void Void (,) (,) Identity p) a b
-> Pipe
     Void
     Void
     (,)
     (,)
     Identity
     p
     (Zerem ((,) a) Identity Void)
     (Zerem ((,) b) Identity Void)
forall u v (f :: * -> * -> *) (g :: * -> * -> *) (m :: * -> *)
       (p :: * -> * -> *) a b.
Pipe u v f g m p a b -> p (Zerem (f a) m u) (Zerem (g b) m v)
runPipe

map :: (Bifunctor f, Functor m) => (a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map :: forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bifunctor f, Functor m) =>
(a -> b) -> Zerem (f a) m r -> Zerem (f b) m r
map a -> b
f = (forall x. f a x -> f b x) -> Zerem (f a) m r -> Zerem (f b) m r
forall (m :: * -> *) (f :: * -> *) (g :: * -> *) r.
Functor m =>
(forall x. f x -> g x) -> Zerem f m r -> Zerem g m r
maps ((a -> b) -> f a x -> f b x
forall a b c. (a -> b) -> f a c -> f b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first a -> b
f)
{-# INLINE map #-}

maps :: Functor m => (forall x. f x -> g x) -> Zerem f m r -> Zerem g m r
maps :: forall (m :: * -> *) (f :: * -> *) (g :: * -> *) r.
Functor m =>
(forall x. f x -> g x) -> Zerem f m r -> Zerem g m r
maps forall x. f x -> g x
f (Zerem s -> m (Step s f r)
next m s
s) = (s -> m (Step s g r)) -> m s -> Zerem g m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem ((Step s f r -> Step s g r) -> m (Step s f r) -> m (Step s g r)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((forall x. f x -> g x) -> Step s f r -> Step s g r
forall (f :: * -> *) (g :: * -> *) s u.
(forall x. f x -> g x) -> Step s f u -> Step s g u
mapYieldF f x -> g x
forall x. f x -> g x
f) (m (Step s f r) -> m (Step s g r))
-> (s -> m (Step s f r)) -> s -> m (Step s g r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> m (Step s f r)
next) m s
s
{-# INLINE maps #-}
-- {-# INLINE [0] maps #-}
-- {-# RULES "maps/coerce" [1] maps coerce = coerce #-}
-- {-# RULES "maps/maps"
--   forall
--     (phiA :: forall x. g x -> h x)
--     (phiB :: forall x. f x -> g x)
--     x.
--     maps phiA (maps phiB x) = maps (phiA . phiB) x #-}

mapM :: (Bitraversable f, Monad m) => (a -> m b) -> Zerem (f a) m r -> Zerem (f b) m r
mapM :: forall (f :: * -> * -> *) (m :: * -> *) a b r.
(Bitraversable f, Monad m) =>
(a -> m b) -> Zerem (f a) m r -> Zerem (f b) m r
mapM a -> m b
f = (forall x. f a x -> m (f b x))
-> Zerem (f a) m r -> Zerem (f b) m r
forall (m :: * -> *) (f :: * -> *) (g :: * -> *) r.
Monad m =>
(forall x. f x -> m (g x)) -> Zerem f m r -> Zerem g m r
mapsM ((a -> m b) -> (x -> m x) -> f a x -> m (f b x)
forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> f a b -> f (f c d)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> m b
f x -> m x
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
{-# INLINE mapM #-}

mapsM :: Monad m => (forall x. f x -> m (g x)) -> Zerem f m r -> Zerem g m r
mapsM :: forall (m :: * -> *) (f :: * -> *) (g :: * -> *) r.
Monad m =>
(forall x. f x -> m (g x)) -> Zerem f m r -> Zerem g m r
mapsM forall x. f x -> m (g x)
f (Zerem s -> m (Step s f r)
next m s
s) = (s -> m (Step s g r)) -> m s -> Zerem g m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem ((forall x. f x -> m (g x)) -> Step s f r -> m (Step s g r)
forall (m :: * -> *) (f :: * -> *) (g :: * -> *) s u.
Monad m =>
(forall x. f x -> m (g x)) -> Step s f u -> m (Step s g u)
mapYieldFM f x -> m (g x)
forall x. f x -> m (g x)
f (Step s f r -> m (Step s g r))
-> (s -> m (Step s f r)) -> s -> m (Step s g r)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< s -> m (Step s f r)
next) m s
s
{-# INLINE mapsM #-}

flatLift :: Monad m => Zerem f m (m r) -> Zerem f m r
flatLift :: forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
Zerem f m (m r) -> Zerem f m r
flatLift = (m r -> m r) -> Zerem f m (m r) -> Zerem f m r
forall (m :: * -> *) r r' (f :: * -> *).
Monad m =>
(r -> m r') -> Zerem f m r -> Zerem f m r'
flatMapLift m r -> m r
forall a. a -> a
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id

flatMapLift :: Monad m => (r -> m r') -> Zerem f m r -> Zerem f m r'
flatMapLift :: forall (m :: * -> *) r r' (f :: * -> *).
Monad m =>
(r -> m r') -> Zerem f m r -> Zerem f m r'
flatMapLift r -> m r'
k (Zerem s -> m (Step s f r)
next m s
s) = (s -> m (Step s f r')) -> m s -> Zerem f m r'
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem s -> m (Step s f r')
next' m s
s where
  next' :: s -> m (Step s f r')
next' s
s =
    s -> m (Step s f r)
next s
s m (Step s f r)
-> (Step s f r -> m (Step s f r')) -> m (Step s f r')
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
r -> r' -> Step s f r'
forall s (f :: * -> *) r. r -> Step s f r
Done (r' -> Step s f r') -> m r' -> m (Step s f r')
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> r -> m r'
k r
r
      Skip s
s' -> Step s f r' -> m (Step s f r')
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step s f r' -> m (Step s f r')) -> Step s f r' -> m (Step s f r')
forall a b. (a -> b) -> a -> b
$ s -> Step s f r'
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s'
      Yield f s
fs' -> Step s f r' -> m (Step s f r')
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step s f r' -> m (Step s f r')) -> Step s f r' -> m (Step s f r')
forall a b. (a -> b) -> a -> b
$ f s -> Step s f r'
forall s (f :: * -> *) r. f s -> Step s f r
Yield f s
fs'

(>>^) :: Monad m => Zerem f m r -> (r -> m r') -> Zerem f m r'
>>^ :: forall (m :: * -> *) (f :: * -> *) r r'.
Monad m =>
Zerem f m r -> (r -> m r') -> Zerem f m r'
(>>^) = ((r -> m r') -> Zerem f m r -> Zerem f m r')
-> Zerem f m r -> (r -> m r') -> Zerem f m r'
forall a b c. (a -> b -> c) -> b -> a -> c
flip (r -> m r') -> Zerem f m r -> Zerem f m r'
forall (m :: * -> *) r r' (f :: * -> *).
Monad m =>
(r -> m r') -> Zerem f m r -> Zerem f m r'
flatMapLift

hoistAny :: (forall x. m x -> n x) -> Zerem f m r -> Zerem f n r
hoistAny :: forall (m :: * -> *) (n :: * -> *) (f :: * -> *) r.
(forall x. m x -> n x) -> Zerem f m r -> Zerem f n r
hoistAny forall x. m x -> n x
f (Zerem s -> m (Step s f r)
next m s
s) = (s -> n (Step s f r)) -> n s -> Zerem f n r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (m (Step s f r) -> n (Step s f r)
forall x. m x -> n x
f (m (Step s f r) -> n (Step s f r))
-> (s -> m (Step s f r)) -> s -> n (Step s f r)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> m (Step s f r)
next) (m s -> n s
forall x. m x -> n x
f m s
s)

runZerem :: Monad m => Zerem m m r -> m r
runZerem :: forall (m :: * -> *) r. Monad m => Zerem m m r -> m r
runZerem (Zerem s -> m (Step s m r)
next m s
s) = s -> m r
loop (s -> m r) -> m s -> m r
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
  loop :: s -> m r
loop s
s =
    s -> m (Step s m r)
next s
s m (Step s m r) -> (Step s m r -> m r) -> m r
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
u    -> r -> m r
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure r
u
      Skip s
s'   -> s -> m r
loop s
s'
      Yield m s
fs' -> m s
fs' m s -> (s -> m r) -> m r
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m r
loop

runZerem_ :: Monad m => Zerem m m r -> m ()
runZerem_ :: forall (m :: * -> *) r. Monad m => Zerem m m r -> m ()
runZerem_ (Zerem s -> m (Step s m r)
next m s
s) = s -> m ()
loop (s -> m ()) -> m s -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
  loop :: s -> m ()
loop s
s =
    s -> m (Step s m r)
next s
s m (Step s m r) -> (Step s m r -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
_    -> () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Skip s
s'   -> s -> m ()
loop s
s'
      Yield m s
fs' -> m s
fs' m s -> (s -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m ()
loop

map_ :: (Comonad f, Monad m) => Zerem f m r -> m r
map_ :: forall (f :: * -> *) (m :: * -> *) r.
(Comonad f, Monad m) =>
Zerem f m r -> m r
map_ = (forall x. f x -> x) -> Zerem f m r -> m r
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
(forall x. f x -> x) -> Zerem f m r -> m r
maps_ f x -> x
forall x. f x -> x
forall (w :: * -> *) a. Comonad w => w a -> a
extract
{-# INLINE map_ #-}

maps_ :: Monad m => (forall x. f x -> x) -> Zerem f m r -> m r
maps_ :: forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
(forall x. f x -> x) -> Zerem f m r -> m r
maps_ forall x. f x -> x
phi (Zerem s -> m (Step s f r)
next m s
s) = s -> m r
loop (s -> m r) -> m s -> m r
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
  loop :: s -> m r
loop s
s =
    s -> m (Step s f r)
next s
s m (Step s f r) -> (Step s f r -> m r) -> m r
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
r -> r -> m r
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure r
r
      Skip s
s' -> s -> m r
loop s
s'
      Yield f s
fs' -> s -> m r
loop (f s -> s
forall x. f x -> x
phi f s
fs')
{-# INLINE maps_ #-}

mapM_ :: (Bitraversable f, Comonad (f b), Monad m) => (a -> m b) -> Zerem (f a) m r -> m r
mapM_ :: forall (f :: * -> * -> *) b (m :: * -> *) a r.
(Bitraversable f, Comonad (f b), Monad m) =>
(a -> m b) -> Zerem (f a) m r -> m r
mapM_ a -> m b
f = (forall x. f a x -> m x) -> Zerem (f a) m r -> m r
forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
(forall x. f x -> m x) -> Zerem f m r -> m r
mapsM_ ((f b x -> x) -> m (f b x) -> m x
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap f b x -> x
forall a. f b a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract (m (f b x) -> m x) -> (f a x -> m (f b x)) -> f a x -> m x
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (a -> m b) -> (x -> m x) -> f a x -> m (f b x)
forall (f :: * -> *) a c b d.
Applicative f =>
(a -> f c) -> (b -> f d) -> f a b -> f (f c d)
forall (t :: * -> * -> *) (f :: * -> *) a c b d.
(Bitraversable t, Applicative f) =>
(a -> f c) -> (b -> f d) -> t a b -> f (t c d)
bitraverse a -> m b
f x -> m x
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
{-# INLINE mapM_ #-}

mapsM_ :: Monad m => (forall x. f x -> m x) -> Zerem f m r -> m r
mapsM_ :: forall (m :: * -> *) (f :: * -> *) r.
Monad m =>
(forall x. f x -> m x) -> Zerem f m r -> m r
mapsM_ forall x. f x -> m x
phi (Zerem s -> m (Step s f r)
next m s
s) = s -> m r
loop (s -> m r) -> m s -> m r
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
  loop :: s -> m r
loop s
s =
    s -> m (Step s f r)
next s
s m (Step s f r) -> (Step s f r -> m r) -> m r
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
r -> r -> m r
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure r
r
      Skip s
s' -> s -> m r
loop s
s'
      Yield f s
fs' -> f s -> m s
forall x. f x -> m x
phi f s
fs' m s -> (s -> m r) -> m r
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m r
loop
{-# INLINE mapsM_ #-}

-- embed :: Functor m => (forall x. m x -> Zerem f n x) -> Zerem f m r -> Zerem f n r
-- embed f (Zerem next s done) = Zerem next' (s, Nothing) done where

foldr :: Monad m => (a -> b -> b) -> (r -> b) -> Zerem ((,) a) m r -> m b
foldr :: forall (m :: * -> *) a b r.
Monad m =>
(a -> b -> b) -> (r -> b) -> Zerem ((,) a) m r -> m b
foldr a -> b -> b
f !r -> b
g (Zerem s -> m (Step s ((,) a) r)
next m s
s) = s -> m b
loop (s -> m b) -> m s -> m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
  loop :: s -> m b
loop s
s =
    s -> m (Step s ((,) a) r)
next s
s m (Step s ((,) a) r) -> (Step s ((,) a) r -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
r -> b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (r -> b
g r
r)
      Skip s
s' -> s -> m b
loop s
s'
      Yield (a
a, s
s') -> a -> b -> b
f a
a (b -> b) -> m b -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
loop s
s'

foldr_ :: Monad m => (a -> b -> b) -> b -> Zerem ((,) a) m r -> m b
foldr_ :: forall (m :: * -> *) a b r.
Monad m =>
(a -> b -> b) -> b -> Zerem ((,) a) m r -> m b
foldr_ a -> b -> b
f b
z = (a -> b -> b) -> (r -> b) -> Zerem ((,) a) m r -> m b
forall (m :: * -> *) a b r.
Monad m =>
(a -> b -> b) -> (r -> b) -> Zerem ((,) a) m r -> m b
Zerem.foldr a -> b -> b
f (b -> r -> b
forall a b. a -> b -> a
const b
z)
{-# INLINE foldr_ #-}

foldrM :: Monad m => (a -> b -> m b) -> (r -> m b) -> Zerem ((,) a) m r -> m b
foldrM :: forall (m :: * -> *) a b r.
Monad m =>
(a -> b -> m b) -> (r -> m b) -> Zerem ((,) a) m r -> m b
foldrM a -> b -> m b
f !r -> m b
g (Zerem s -> m (Step s ((,) a) r)
next m s
s) = s -> m b
loop (s -> m b) -> m s -> m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
  loop :: s -> m b
loop s
s =
    s -> m (Step s ((,) a) r)
next s
s m (Step s ((,) a) r) -> (Step s ((,) a) r -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
r -> r -> m b
g r
r
      Skip s
s' -> s -> m b
loop s
s'
      Yield (a
a, s
s') -> a -> b -> m b
f a
a (b -> m b) -> m b -> m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< s -> m b
loop s
s'

foldrM_ :: Monad m => (a -> b -> m b) -> m b -> Zerem ((,) a) m r -> m b
foldrM_ :: forall (m :: * -> *) a b r.
Monad m =>
(a -> b -> m b) -> m b -> Zerem ((,) a) m r -> m b
foldrM_ a -> b -> m b
f m b
z = (a -> b -> m b) -> (r -> m b) -> Zerem ((,) a) m r -> m b
forall (m :: * -> *) a b r.
Monad m =>
(a -> b -> m b) -> (r -> m b) -> Zerem ((,) a) m r -> m b
Zerem.foldrM a -> b -> m b
f (m b -> r -> m b
forall a b. a -> b -> a
const m b
z)
{-# INLINE foldrM_ #-}

foldl :: Monad m => (b -> a -> b) -> b -> (b -> r -> b') -> Zerem ((,) a) m r -> m b'
foldl :: forall (m :: * -> *) b a r b'.
Monad m =>
(b -> a -> b) -> b -> (b -> r -> b') -> Zerem ((,) a) m r -> m b'
foldl b -> a -> b
f b
z !b -> r -> b'
g (Zerem s -> m (Step s ((,) a) r)
next m s
s) = b -> s -> m b'
loop b
z (s -> m b') -> m s -> m b'
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m s
s where
  loop :: b -> s -> m b'
loop !b
z s
s =
    s -> m (Step s ((,) a) r)
next s
s m (Step s ((,) a) r) -> (Step s ((,) a) r -> m b') -> m b'
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
r -> b' -> m b'
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (b' -> m b') -> b' -> m b'
forall a b. (a -> b) -> a -> b
$! b -> r -> b'
g b
z r
r
      Skip s
s' -> b -> s -> m b'
loop b
z s
s'
      Yield (a
a, s
s') -> b -> s -> m b'
loop (b -> a -> b
f b
z a
a) s
s'

foldl_ :: Monad m => (b -> a -> b) -> b -> Zerem ((,) a) m r -> m b
foldl_ :: forall (m :: * -> *) b a r.
Monad m =>
(b -> a -> b) -> b -> Zerem ((,) a) m r -> m b
foldl_ b -> a -> b
f b
z = (b -> a -> b) -> b -> (b -> r -> b) -> Zerem ((,) a) m r -> m b
forall (m :: * -> *) b a r b'.
Monad m =>
(b -> a -> b) -> b -> (b -> r -> b') -> Zerem ((,) a) m r -> m b'
Zerem.foldl b -> a -> b
f b
z b -> r -> b
forall a b. a -> b -> a
const
{-# INLINE foldl_ #-}

foldlM :: Monad m => (b -> a -> m b) -> m b -> (b -> r -> m b') -> Zerem ((,) a) m r -> m b'
foldlM :: forall (m :: * -> *) b a r b'.
Monad m =>
(b -> a -> m b)
-> m b -> (b -> r -> m b') -> Zerem ((,) a) m r -> m b'
foldlM b -> a -> m b
f m b
z !b -> r -> m b'
g (Zerem s -> m (Step s ((,) a) r)
next m s
s) = (b -> s -> m b') -> (b, s) -> m b'
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry b -> s -> m b'
loop ((b, s) -> m b') -> m (b, s) -> m b'
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (b -> s -> (b, s)) -> m b -> m s -> m (b, s)
forall a b c. (a -> b -> c) -> m a -> m b -> m c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) m b
z m s
s where
  loop :: b -> s -> m b'
loop !b
z s
s =
    s -> m (Step s ((,) a) r)
next s
s m (Step s ((,) a) r) -> (Step s ((,) a) r -> m b') -> m b'
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Done r
r -> b -> r -> m b'
g b
z r
r
      Skip s
s' -> b -> s -> m b'
loop b
z s
s'
      Yield (a
a, s
s') -> (\b
z' -> b -> s -> m b'
loop b
z' s
s') (b -> m b') -> m b -> m b'
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< b -> a -> m b
f b
z a
a

foldlM_ :: Monad m => (b -> a -> m b) -> m b -> Zerem ((,) a) m r -> m b
foldlM_ :: forall (m :: * -> *) b a r.
Monad m =>
(b -> a -> m b) -> m b -> Zerem ((,) a) m r -> m b
foldlM_ b -> a -> m b
f m b
z = (b -> a -> m b)
-> m b -> (b -> r -> m b) -> Zerem ((,) a) m r -> m b
forall (m :: * -> *) b a r b'.
Monad m =>
(b -> a -> m b)
-> m b -> (b -> r -> m b') -> Zerem ((,) a) m r -> m b'
Zerem.foldlM b -> a -> m b
f m b
z (m b -> r -> m b
forall a b. a -> b -> a
const (m b -> r -> m b) -> (b -> m b) -> b -> r -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. b -> m b
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure)
{-# INLINE foldlM_ #-}

scanl :: (Functor m, Functor g) => (a -> x -> g x) -> x -> (r -> x -> x') -> Zerem ((,) a) m r -> Zerem g m x'
scanl :: forall (m :: * -> *) (g :: * -> *) a x r x'.
(Functor m, Functor g) =>
(a -> x -> g x)
-> x -> (r -> x -> x') -> Zerem ((,) a) m r -> Zerem g m x'
scanl a -> x -> g x
f x
x r -> x -> x'
g (Zerem s -> m (Step s ((,) a) r)
next m s
s) = ((s, x) -> m (Step (s, x) g x')) -> m (s, x) -> Zerem g m x'
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (s, x) -> m (Step (s, x) g x')
next' ((,x
x) (s -> (s, x)) -> m s -> m (s, x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s) where
  next' :: (s, x) -> m (Step (s, x) g x')
next' (s
s, !x
x) =
    s -> m (Step s ((,) a) r)
next s
s m (Step s ((,) a) r)
-> (Step s ((,) a) r -> Step (s, x) g x') -> m (Step (s, x) g x')
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> x' -> Step (s, x) g x'
forall s (f :: * -> *) r. r -> Step s f r
Done (r -> x -> x'
g r
r x
x)
      Skip s
s' -> (s, x) -> Step (s, x) g x'
forall s (f :: * -> *) r. s -> Step s f r
Skip (s
s', x
x)
      Yield (a
a, s
s') -> g (s, x) -> Step (s, x) g x'
forall s (f :: * -> *) r. f s -> Step s f r
Yield (g (s, x) -> Step (s, x) g x') -> g (s, x) -> Step (s, x) g x'
forall a b. (a -> b) -> a -> b
$ (s
s',) (x -> (s, x)) -> g x -> g (s, x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> x -> g x
f a
a x
x

scanl_ :: (Functor m, Functor g) => (a -> x -> g x) -> x -> Zerem ((,) a) m r -> Zerem g m x
scanl_ :: forall (m :: * -> *) (g :: * -> *) a x r.
(Functor m, Functor g) =>
(a -> x -> g x) -> x -> Zerem ((,) a) m r -> Zerem g m x
scanl_ a -> x -> g x
f x
x = (a -> x -> g x)
-> x -> (r -> x -> x) -> Zerem ((,) a) m r -> Zerem g m x
forall (m :: * -> *) (g :: * -> *) a x r x'.
(Functor m, Functor g) =>
(a -> x -> g x)
-> x -> (r -> x -> x') -> Zerem ((,) a) m r -> Zerem g m x'
Zerem.scanl a -> x -> g x
f x
x (\r
_ x
x -> x
x)
{-# INLINE scanl_ #-}

scansl :: (Functor f, Functor m) => (forall s. f (s, x) -> g (s, x)) -> x -> (r -> x -> x') -> Zerem f m r -> Zerem g m x'
scansl :: forall (f :: * -> *) (m :: * -> *) x (g :: * -> *) r x'.
(Functor f, Functor m) =>
(forall s. f (s, x) -> g (s, x))
-> x -> (r -> x -> x') -> Zerem f m r -> Zerem g m x'
scansl forall s. f (s, x) -> g (s, x)
f x
x r -> x -> x'
g (Zerem s -> m (Step s f r)
next m s
s) = ((s, x) -> m (Step (s, x) g x')) -> m (s, x) -> Zerem g m x'
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (s, x) -> m (Step (s, x) g x')
next' ((,x
x) (s -> (s, x)) -> m s -> m (s, x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s) where
  next' :: (s, x) -> m (Step (s, x) g x')
next' (s
s, !x
x) =
    s -> m (Step s f r)
next s
s m (Step s f r)
-> (Step s f r -> Step (s, x) g x') -> m (Step (s, x) g x')
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> x' -> Step (s, x) g x'
forall s (f :: * -> *) r. r -> Step s f r
Done (r -> x -> x'
g r
r x
x)
      Skip s
s' -> (s, x) -> Step (s, x) g x'
forall s (f :: * -> *) r. s -> Step s f r
Skip (s
s', x
x)
      Yield f s
fs' -> g (s, x) -> Step (s, x) g x'
forall s (f :: * -> *) r. f s -> Step s f r
Yield (g (s, x) -> Step (s, x) g x')
-> (f (s, x) -> g (s, x)) -> f (s, x) -> Step (s, x) g x'
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f (s, x) -> g (s, x)
forall s. f (s, x) -> g (s, x)
f (f (s, x) -> Step (s, x) g x') -> f (s, x) -> Step (s, x) g x'
forall a b. (a -> b) -> a -> b
$ (s -> (s, x)) -> f s -> f (s, x)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (,x
x) f s
fs'

scansl_ :: (Functor f, Functor m) => (forall s. f (s, x) -> g (s, x)) -> x -> Zerem f m r -> Zerem g m x
scansl_ :: forall (f :: * -> *) (m :: * -> *) x (g :: * -> *) r.
(Functor f, Functor m) =>
(forall s. f (s, x) -> g (s, x)) -> x -> Zerem f m r -> Zerem g m x
scansl_ forall s. f (s, x) -> g (s, x)
f x
x = (forall s. f (s, x) -> g (s, x))
-> x -> (r -> x -> x) -> Zerem f m r -> Zerem g m x
forall (f :: * -> *) (m :: * -> *) x (g :: * -> *) r x'.
(Functor f, Functor m) =>
(forall s. f (s, x) -> g (s, x))
-> x -> (r -> x -> x') -> Zerem f m r -> Zerem g m x'
scansl f (s, x) -> g (s, x)
forall s. f (s, x) -> g (s, x)
f x
x (\r
_ x
x -> x
x)
{-# INLINE scansl_ #-}

scanMaybel :: (Functor m) => (a -> x -> (Maybe b, x)) -> x -> (r -> x -> x') -> Zerem ((,) a) m r -> Zerem ((,) b) m x'
scanMaybel :: forall (m :: * -> *) a x b r x'.
Functor m =>
(a -> x -> (Maybe b, x))
-> x -> (r -> x -> x') -> Zerem ((,) a) m r -> Zerem ((,) b) m x'
scanMaybel a -> x -> (Maybe b, x)
f x
x r -> x -> x'
g (Zerem s -> m (Step s ((,) a) r)
next m s
s) = ((s, x) -> m (Step (s, x) ((,) b) x'))
-> m (s, x) -> Zerem ((,) b) m x'
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (s, x) -> m (Step (s, x) ((,) b) x')
next' ((,x
x) (s -> (s, x)) -> m s -> m (s, x)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s) where
  next' :: (s, x) -> m (Step (s, x) ((,) b) x')
next' (s
s, !x
x) =
    s -> m (Step s ((,) a) r)
next s
s m (Step s ((,) a) r)
-> (Step s ((,) a) r -> Step (s, x) ((,) b) x')
-> m (Step (s, x) ((,) b) x')
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> x' -> Step (s, x) ((,) b) x'
forall s (f :: * -> *) r. r -> Step s f r
Done (r -> x -> x'
g r
r x
x)
      Skip s
s' -> (s, x) -> Step (s, x) ((,) b) x'
forall s (f :: * -> *) r. s -> Step s f r
Skip (s
s', x
x)
      Yield (a
a, s
s') -> case a -> x -> (Maybe b, x)
f a
a x
x of
        (Maybe b
Nothing, x
x') -> (s, x) -> Step (s, x) ((,) b) x'
forall s (f :: * -> *) r. s -> Step s f r
Skip  (s
s', x
x')
        (Just b
b , x
x') -> (b, (s, x)) -> Step (s, x) ((,) b) x'
forall s (f :: * -> *) r. f s -> Step s f r
Yield (b
b, (s
s', x
x'))

scanMaybel_ :: (Functor m) => (a -> x -> (Maybe b, x)) -> x -> Zerem ((,) a) m r -> Zerem ((,) b) m x
scanMaybel_ :: forall (m :: * -> *) a x b r.
Functor m =>
(a -> x -> (Maybe b, x))
-> x -> Zerem ((,) a) m r -> Zerem ((,) b) m x
scanMaybel_ a -> x -> (Maybe b, x)
f x
x = (a -> x -> (Maybe b, x))
-> x -> (r -> x -> x) -> Zerem ((,) a) m r -> Zerem ((,) b) m x
forall (m :: * -> *) a x b r x'.
Functor m =>
(a -> x -> (Maybe b, x))
-> x -> (r -> x -> x') -> Zerem ((,) a) m r -> Zerem ((,) b) m x'
scanMaybel a -> x -> (Maybe b, x)
f x
x (\r
_ x
x -> x
x)
{-# INLINE scanMaybel_ #-}

catMaybes :: Functor m => Zerem ((,) (Maybe a)) m r -> Zerem ((,) a) m r
catMaybes :: forall (m :: * -> *) a r.
Functor m =>
Zerem ((,) (Maybe a)) m r -> Zerem ((,) a) m r
catMaybes (Zerem s -> m (Step s ((,) (Maybe a)) r)
next m s
s) = (s -> m (Step s ((,) a) r)) -> m s -> Zerem ((,) a) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem s -> m (Step s ((,) a) r)
next' m s
s where
  next' :: s -> m (Step s ((,) a) r)
next' s
s =
    s -> m (Step s ((,) (Maybe a)) r)
next s
s m (Step s ((,) (Maybe a)) r)
-> (Step s ((,) (Maybe a)) r -> Step s ((,) a) r)
-> m (Step s ((,) a) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> r -> Step s ((,) a) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
      Skip s
s' -> s -> Step s ((,) a) r
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s'
      Yield (Maybe a
Nothing, s
s') -> s -> Step s ((,) a) r
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s'
      Yield (Just a
a , s
s') -> (a, s) -> Step s ((,) a) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, s
s')
{-# INLINE catMaybes #-}

mapMaybe :: Functor m => (a -> Maybe b) -> Zerem ((,) a) m r -> Zerem ((,) b) m r
mapMaybe :: forall (m :: * -> *) a b r.
Functor m =>
(a -> Maybe b) -> Zerem ((,) a) m r -> Zerem ((,) b) m r
mapMaybe a -> Maybe b
f (Zerem s -> m (Step s ((,) a) r)
next m s
s) = (s -> m (Step s ((,) b) r)) -> m s -> Zerem ((,) b) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem s -> m (Step s ((,) b) r)
next' m s
s where
  next' :: s -> m (Step s ((,) b) r)
next' s
s =
    s -> m (Step s ((,) a) r)
next s
s m (Step s ((,) a) r)
-> (Step s ((,) a) r -> Step s ((,) b) r) -> m (Step s ((,) b) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> r -> Step s ((,) b) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
      Skip s
s' -> s -> Step s ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s'
      Yield (a
a, s
s') -> case a -> Maybe b
f a
a of
        Maybe b
Nothing -> s -> Step s ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s'
        Just b
b  -> (b, s) -> Step s ((,) b) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (b
b, s
s')
{-# INLINE mapMaybe #-}

concat :: Applicative m => Zerem ((,) [a]) m r -> Zerem ((,) a) m r
concat :: forall (m :: * -> *) a r.
Applicative m =>
Zerem ((,) [a]) m r -> Zerem ((,) a) m r
concat (Zerem s -> m (Step s ((,) [a]) r)
next m s
ms) = (([a], s) -> m (Step ([a], s) ((,) a) r))
-> m ([a], s) -> Zerem ((,) a) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem ([a], s) -> m (Step ([a], s) ((,) a) r)
next' (([],) (s -> ([a], s)) -> m s -> m ([a], s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
ms) where
  next' :: ([a], s) -> m (Step ([a], s) ((,) a) r)
next' ([], s
s) =
    s -> m (Step s ((,) [a]) r)
next s
s m (Step s ((,) [a]) r)
-> (Step s ((,) [a]) r -> Step ([a], s) ((,) a) r)
-> m (Step ([a], s) ((,) a) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> r -> Step ([a], s) ((,) a) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
      Skip s
s' -> ([a], s) -> Step ([a], s) ((,) a) r
forall s (f :: * -> *) r. s -> Step s f r
Skip ([], s
s')
      Yield ([a]
as, s
s') -> ([a], s) -> Step ([a], s) ((,) a) r
forall s (f :: * -> *) r. s -> Step s f r
Skip ([a]
as, s
s')
  next' (a
a:[a]
as, s
s) = Step ([a], s) ((,) a) r -> m (Step ([a], s) ((,) a) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step ([a], s) ((,) a) r -> m (Step ([a], s) ((,) a) r))
-> Step ([a], s) ((,) a) r -> m (Step ([a], s) ((,) a) r)
forall a b. (a -> b) -> a -> b
$ (a, ([a], s)) -> Step ([a], s) ((,) a) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, ([a]
as, s
s))
{-# INLINE concat #-}

concatMap :: Applicative m => (a -> [b]) -> Zerem ((,) a) m r -> Zerem ((,) b) m r
concatMap :: forall (m :: * -> *) a b r.
Applicative m =>
(a -> [b]) -> Zerem ((,) a) m r -> Zerem ((,) b) m r
concatMap a -> [b]
f (Zerem s -> m (Step s ((,) a) r)
next m s
ms) = (([b], s) -> m (Step ([b], s) ((,) b) r))
-> m ([b], s) -> Zerem ((,) b) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem ([b], s) -> m (Step ([b], s) ((,) b) r)
next' (([],) (s -> ([b], s)) -> m s -> m ([b], s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
ms) where
  next' :: ([b], s) -> m (Step ([b], s) ((,) b) r)
next' ([], s
s) =
    s -> m (Step s ((,) a) r)
next s
s m (Step s ((,) a) r)
-> (Step s ((,) a) r -> Step ([b], s) ((,) b) r)
-> m (Step ([b], s) ((,) b) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> r -> Step ([b], s) ((,) b) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
      Skip s
s' -> ([b], s) -> Step ([b], s) ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip ([], s
s')
      Yield (a
a, s
s') -> ([b], s) -> Step ([b], s) ((,) b) r
forall s (f :: * -> *) r. s -> Step s f r
Skip (a -> [b]
f a
a, s
s')
  next' (b
b:[b]
bs, s
s) = Step ([b], s) ((,) b) r -> m (Step ([b], s) ((,) b) r)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step ([b], s) ((,) b) r -> m (Step ([b], s) ((,) b) r))
-> Step ([b], s) ((,) b) r -> m (Step ([b], s) ((,) b) r)
forall a b. (a -> b) -> a -> b
$ (b, ([b], s)) -> Step ([b], s) ((,) b) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (b
b, ([b]
bs, s
s))
{-# INLINE concatMap #-}

filter :: Functor m => (a -> Bool) -> Zerem ((,) a) m r -> Zerem ((,) a) m r
filter :: forall (m :: * -> *) a r.
Functor m =>
(a -> Bool) -> Zerem ((,) a) m r -> Zerem ((,) a) m r
filter a -> Bool
p (Zerem s -> m (Step s ((,) a) r)
next m s
s) = (s -> m (Step s ((,) a) r)) -> m s -> Zerem ((,) a) m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem s -> m (Step s ((,) a) r)
next' m s
s where
  next' :: s -> m (Step s ((,) a) r)
next' s
s =
    s -> m (Step s ((,) a) r)
next s
s m (Step s ((,) a) r)
-> (Step s ((,) a) r -> Step s ((,) a) r) -> m (Step s ((,) a) r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> r -> Step s ((,) a) r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
      Skip s
s' -> s -> Step s ((,) a) r
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s'
      Yield (a
a, s
s') -> if a -> Bool
p a
a then (a, s) -> Step s ((,) a) r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, s
s') else s -> Step s ((,) a) r
forall s (f :: * -> *) r. s -> Step s f r
Skip s
s'
{-# INLINE filter #-}

take :: (Functor f, Applicative m) => Int -> Zerem f m r -> Zerem f m ()
take :: forall (f :: * -> *) (m :: * -> *) r.
(Functor f, Applicative m) =>
Int -> Zerem f m r -> Zerem f m ()
take Int
n (Zerem s -> m (Step s f r)
next m s
s) = ((Int, s) -> m (Step (Int, s) f ())) -> m (Int, s) -> Zerem f m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Int, s) -> m (Step (Int, s) f ())
next' ((Int
n,) (s -> (Int, s)) -> m s -> m (Int, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s) where
  next' :: (Int, s) -> m (Step (Int, s) f ())
next' (Int
0, s
_) = Step (Int, s) f () -> m (Step (Int, s) f ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (() -> Step (Int, s) f ()
forall s (f :: * -> *) r. r -> Step s f r
Done ())
  next' (Int
n, s
s) =
    s -> m (Step s f r)
next s
s m (Step s f r)
-> (Step s f r -> Step (Int, s) f ()) -> m (Step (Int, s) f ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
_ -> () -> Step (Int, s) f ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
      Skip s
s' -> (Int, s) -> Step (Int, s) f ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Int
n, s
s')
      Yield f s
fs' -> f (Int, s) -> Step (Int, s) f ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (f (Int, s) -> Step (Int, s) f ())
-> f (Int, s) -> Step (Int, s) f ()
forall a b. (a -> b) -> a -> b
$ (Int -> Int
forall a. Enum a => a -> a
pred Int
n,) (s -> (Int, s)) -> f s -> f (Int, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f s
fs'

drop :: (Comonad f, Functor m) => Int -> Zerem f m r -> Zerem f m r
drop :: forall (f :: * -> *) (m :: * -> *) r.
(Comonad f, Functor m) =>
Int -> Zerem f m r -> Zerem f m r
drop Int
n z :: Zerem f m r
z@(Zerem s -> m (Step s f r)
next m s
s) = if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 then Zerem f m r
z else ((Int, s) -> m (Step (Int, s) f r)) -> m (Int, s) -> Zerem f m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Int, s) -> m (Step (Int, s) f r)
next' ((Int
n,) (s -> (Int, s)) -> m s -> m (Int, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s) where
  next' :: (Int, s) -> m (Step (Int, s) f r)
next' (Int
0, s
s) =
    s -> m (Step s f r)
next s
s m (Step s f r)
-> (Step s f r -> Step (Int, s) f r) -> m (Step (Int, s) f r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> r -> Step (Int, s) f r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
      Skip s
s' -> (Int, s) -> Step (Int, s) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Int
0, s
s')
      Yield f s
fs' -> f (Int, s) -> Step (Int, s) f r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (f (Int, s) -> Step (Int, s) f r)
-> f (Int, s) -> Step (Int, s) f r
forall a b. (a -> b) -> a -> b
$ (s -> (Int, s)) -> f s -> f (Int, s)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int
0,) f s
fs'
  next' (Int
n, s
s) =
    s -> m (Step s f r)
next s
s m (Step s f r)
-> (Step s f r -> Step (Int, s) f r) -> m (Step (Int, s) f r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
r -> r -> Step (Int, s) f r
forall s (f :: * -> *) r. r -> Step s f r
Done r
r
      Skip s
s' -> (Int, s) -> Step (Int, s) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Int
n, s
s')
      Yield f s
fs' -> (Int, s) -> Step (Int, s) f r
forall s (f :: * -> *) r. s -> Step s f r
Skip ((Int, s) -> Step (Int, s) f r) -> (Int, s) -> Step (Int, s) f r
forall a b. (a -> b) -> a -> b
$ (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1, f s -> s
forall a. f a -> a
forall (w :: * -> *) a. Comonad w => w a -> a
extract f s
fs')

yield :: Applicative m => a -> Zerem ((,) a) m ()
yield :: forall (m :: * -> *) a. Applicative m => a -> Zerem ((,) a) m ()
yield = Identity a -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Applicative m) =>
f a -> Zerem ((,) a) m ()
yieldMany (Identity a -> Zerem ((,) a) m ())
-> (a -> Identity a) -> a -> Zerem ((,) a) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Identity a
forall a. a -> Identity a
Identity
{-# INLINE yield #-}

yieldMany :: (Foldable f, Applicative m) => f a -> Zerem ((,) a) m ()
yieldMany :: forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Applicative m) =>
f a -> Zerem ((,) a) m ()
yieldMany = ([a] -> m (Step [a] ((,) a) ())) -> m [a] -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Step [a] ((,) a) () -> m (Step [a] ((,) a) ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step [a] ((,) a) () -> m (Step [a] ((,) a) ()))
-> ([a] -> Step [a] ((,) a) ()) -> [a] -> m (Step [a] ((,) a) ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [a] -> Step [a] ((,) a) ()
forall {a}. [a] -> Step [a] ((,) a) ()
next) (m [a] -> Zerem ((,) a) m ())
-> (f a -> m [a]) -> f a -> Zerem ((,) a) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [a] -> m [a]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([a] -> m [a]) -> (f a -> [a]) -> f a -> m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f a -> [a]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList where
  next :: [a] -> Step [a] ((,) a) ()
next [] = () -> Step [a] ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
  next (a
a:[a]
as) = (a, [a]) -> Step [a] ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (a
a, [a]
as)
{-# INLINE yieldMany #-}

yieldM :: Applicative m => m a -> Zerem ((,) a) m ()
yieldM :: forall (m :: * -> *) a. Applicative m => m a -> Zerem ((,) a) m ()
yieldM = Identity (m a) -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Applicative m) =>
f (m a) -> Zerem ((,) a) m ()
yieldManyM (Identity (m a) -> Zerem ((,) a) m ())
-> (m a -> Identity (m a)) -> m a -> Zerem ((,) a) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. m a -> Identity (m a)
forall a. a -> Identity a
Identity
{-# INLINE yieldM #-}

yieldManyM :: (Foldable f, Applicative m) => f (m a) -> Zerem ((,) a) m ()
yieldManyM :: forall (f :: * -> *) (m :: * -> *) a.
(Foldable f, Applicative m) =>
f (m a) -> Zerem ((,) a) m ()
yieldManyM = ([m a] -> m (Step [m a] ((,) a) ()))
-> m [m a] -> Zerem ((,) a) m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem [m a] -> m (Step [m a] ((,) a) ())
forall {f :: * -> *} {a}.
Applicative f =>
[f a] -> f (Step [f a] ((,) a) ())
next (m [m a] -> Zerem ((,) a) m ())
-> (f (m a) -> m [m a]) -> f (m a) -> Zerem ((,) a) m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [m a] -> m [m a]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([m a] -> m [m a]) -> (f (m a) -> [m a]) -> f (m a) -> m [m a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f (m a) -> [m a]
forall a. f a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList where
  next :: [f a] -> f (Step [f a] ((,) a) ())
next [] = Step [f a] ((,) a) () -> f (Step [f a] ((,) a) ())
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step [f a] ((,) a) () -> f (Step [f a] ((,) a) ()))
-> Step [f a] ((,) a) () -> f (Step [f a] ((,) a) ())
forall a b. (a -> b) -> a -> b
$ () -> Step [f a] ((,) a) ()
forall s (f :: * -> *) r. r -> Step s f r
Done ()
  next (f a
ma:[f a]
as) = f a
ma f a -> (a -> Step [f a] ((,) a) ()) -> f (Step [f a] ((,) a) ())
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (a, [f a]) -> Step [f a] ((,) a) ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield ((a, [f a]) -> Step [f a] ((,) a) ())
-> (a -> (a, [f a])) -> a -> Step [f a] ((,) a) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (,[f a]
as)
{-# INLINE yieldManyM #-}

concatStream
  :: (Functor m, Functor g)
  => (s -> m (Step s g u))
  -> Zerem ((,) s) m r
  -> Zerem g m r
concatStream :: forall (m :: * -> *) (g :: * -> *) s u r.
(Functor m, Functor g) =>
(s -> m (Step s g u)) -> Zerem ((,) s) m r -> Zerem g m r
concatStream s -> m (Step s g u)
inner (Zerem s -> m (Step s ((,) s) r)
next m s
s) = ((Maybe s, s) -> m (Step (Maybe s, s) g r))
-> m (Maybe s, s) -> Zerem g m r
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Maybe s, s) -> m (Step (Maybe s, s) g r)
next' ((Maybe s
forall a. Maybe a
Nothing,) (s -> (Maybe s, s)) -> m s -> m (Maybe s, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s) where
  next' :: (Maybe s, s) -> m (Step (Maybe s, s) g r)
next' (Maybe s
Nothing, s
s) =
    s -> m (Step s ((,) s) r)
next s
s m (Step s ((,) s) r)
-> (Step s ((,) s) r -> Step (Maybe s, s) g r)
-> m (Step (Maybe s, s) g r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done r
u -> r -> Step (Maybe s, s) g r
forall s (f :: * -> *) r. r -> Step s f r
Done r
u
      Skip s
s' -> (Maybe s, s) -> Step (Maybe s, s) g r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Maybe s
forall a. Maybe a
Nothing, s
s')
      Yield (s
si, s
s') -> (Maybe s, s) -> Step (Maybe s, s) g r
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Maybe s
forall a. a -> Maybe a
Just s
si, s
s')
  next' (Just s
si, s
s) =
    s -> m (Step s g u)
inner s
si m (Step s g u)
-> (Step s g u -> Step (Maybe s, s) g r)
-> m (Step (Maybe s, s) g r)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
      Done u
_ -> (Maybe s, s) -> Step (Maybe s, s) g r
forall s (f :: * -> *) r. s -> Step s f r
Skip (Maybe s
forall a. Maybe a
Nothing, s
s)
      Skip s
si' -> (Maybe s, s) -> Step (Maybe s, s) g r
forall s (f :: * -> *) r. s -> Step s f r
Skip (s -> Maybe s
forall a. a -> Maybe a
Just s
si', s
s)
      Yield g s
gsi' -> g (Maybe s, s) -> Step (Maybe s, s) g r
forall s (f :: * -> *) r. f s -> Step s f r
Yield (g (Maybe s, s) -> Step (Maybe s, s) g r)
-> g (Maybe s, s) -> Step (Maybe s, s) g r
forall a b. (a -> b) -> a -> b
$ (s -> (Maybe s, s)) -> g s -> g (Maybe s, s)
forall a b. (a -> b) -> g a -> g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((,s
s) (Maybe s -> (Maybe s, s)) -> (s -> Maybe s) -> s -> (Maybe s, s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. s -> Maybe s
forall a. a -> Maybe a
Just) g s
gsi'

mtimes
  :: (Monad m, Functor f)
  => Int -> Zerem f m r -> Zerem f m ()
mtimes :: forall (m :: * -> *) (f :: * -> *) r.
(Monad m, Functor f) =>
Int -> Zerem f m r -> Zerem f m ()
mtimes Int
n (Zerem s -> m (Step s f r)
next m s
s0) =
  if Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 then Zerem f m ()
forall a. Monoid a => a
mempty
  else ((Int, s) -> m (Step (Int, s) f ())) -> m (Int, s) -> Zerem f m ()
forall (f :: * -> *) (m :: * -> *) r s.
(s -> m (Step s f r)) -> m s -> Zerem f m r
Zerem (Int, s) -> m (Step (Int, s) f ())
next' ((Int
n,) (s -> (Int, s)) -> m s -> m (Int, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s0) where
  next' :: (Int, s) -> m (Step (Int, s) f ())
next' (Int
n, s
s) =
    s -> m (Step s f r)
next s
s m (Step s f r)
-> (Step s f r -> m (Step (Int, s) f ())) -> m (Step (Int, s) f ())
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      -- Using Monad constraint to re-run the initialization action
      -- which might be creating a mutable reference that we don't want
      -- to re-use. Also, it's good to behave like (sconcat . replicate n).
      Done r
_ -> if Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 then Step (Int, s) f () -> m (Step (Int, s) f ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Int, s) f () -> m (Step (Int, s) f ()))
-> Step (Int, s) f () -> m (Step (Int, s) f ())
forall a b. (a -> b) -> a -> b
$ () -> Step (Int, s) f ()
forall s (f :: * -> *) r. r -> Step s f r
Done () else (Int, s) -> Step (Int, s) f ()
forall s (f :: * -> *) r. s -> Step s f r
Skip ((Int, s) -> Step (Int, s) f ())
-> (s -> (Int, s)) -> s -> Step (Int, s) f ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Int -> Int
forall a. Enum a => a -> a
pred Int
n,) (s -> Step (Int, s) f ()) -> m s -> m (Step (Int, s) f ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
s0
      Skip s
s' -> Step (Int, s) f () -> m (Step (Int, s) f ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Int, s) f () -> m (Step (Int, s) f ()))
-> Step (Int, s) f () -> m (Step (Int, s) f ())
forall a b. (a -> b) -> a -> b
$ (Int, s) -> Step (Int, s) f ()
forall s (f :: * -> *) r. s -> Step s f r
Skip (Int
n, s
s')
      Yield f s
fs' -> Step (Int, s) f () -> m (Step (Int, s) f ())
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Step (Int, s) f () -> m (Step (Int, s) f ()))
-> (f (Int, s) -> Step (Int, s) f ())
-> f (Int, s)
-> m (Step (Int, s) f ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. f (Int, s) -> Step (Int, s) f ()
forall s (f :: * -> *) r. f s -> Step s f r
Yield (f (Int, s) -> m (Step (Int, s) f ()))
-> f (Int, s) -> m (Step (Int, s) f ())
forall a b. (a -> b) -> a -> b
$ (s -> (Int, s)) -> f s -> f (Int, s)
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int
n,) f s
fs'