#ifndef MIN_VERSION_base
#define MIN_VERSION_base(x,y,z) 0
#endif
module Data.Machine.Runner
( foldrT
, foldlT
, foldMapT
, foldT
, runT1
, runT
, runT_ ) where
import Data.Machine.Type
import Control.Monad (liftM)
#if !MIN_VERSION_base (4,8,0)
import Data.Monoid (Monoid (..))
#endif
foldrT :: Monad m => (o -> b -> b) -> b -> MachineT m k o -> m b
foldrT c n = go
where
go m = do
step <- runMachineT m
case step of
Stop -> return n
Yield o m' -> c o `liftM` go m'
Await _ _ m' -> go m'
foldlT :: Monad m => (b -> o -> b) -> b -> MachineT m k o -> m b
foldlT f = go
where
go !b m = do
step <- runMachineT m
case step of
Stop -> return b
Yield o m' -> go (f b o) m'
Await _ _ m' -> go b m'
foldMapT :: (Monad m, Monoid r) => (o -> r) -> MachineT m k o -> m r
foldMapT f = foldlT (\b o -> mappend b (f o)) mempty
foldT :: (Monad m, Monoid o) => MachineT m k o -> m o
foldT = foldlT mappend mempty
runT1 :: Monad m => MachineT m k o -> m (Maybe o)
runT1 m = do
step <- runMachineT m
case step of
Stop -> return Nothing
Yield o _ -> return $ Just o
Await _ _ m' -> runT1 m'