module Control.Monad.Trans.MSF.Maybe
( module Control.Monad.Trans.MSF.Maybe
, module Control.Monad.Trans.Maybe
) where
import Control.Monad.Trans.Maybe
hiding (liftCallCC, liftCatch, liftListen, liftPass)
import Control.Monad.Trans.MSF.GenLift
import Data.MonadicStreamFunction
runMaybeS'' :: Monad m => MSF (MaybeT m) a b -> MSF m a (Maybe b)
runMaybeS'' = transG transformInput transformOutput
where
transformInput = return
transformOutput _ m1 = do r <- runMaybeT m1
case r of
Nothing -> return (Nothing, Nothing)
Just (b, c) -> return (Just b, Just c)
exit :: Monad m => MSF (MaybeT m) a b
exit = MSF $ const $ MaybeT $ return Nothing
exitWhen :: Monad m => (a -> Bool) -> MSF (MaybeT m) a a
exitWhen condition = go where
go = MSF $ \a -> MaybeT $
if condition a
then return Nothing
else return $ Just (a, go)
exitIf :: Monad m => MSF (MaybeT m) Bool ()
exitIf = MSF $ \b -> MaybeT $ return $ if b then Nothing else Just ((), exitIf)
maybeExit :: Monad m => MSF (MaybeT m) (Maybe a) a
maybeExit = MSF $ MaybeT . return . fmap (\x -> (x, maybeExit))
inMaybeT :: Monad m => MSF (MaybeT m) (Maybe a) a
inMaybeT = arrM $ MaybeT . return
untilMaybe :: Monad m => MSF m a b -> MSF m b Bool -> MSF (MaybeT m) a b
untilMaybe msf cond = proc a -> do
b <- liftMSFTrans msf -< a
c <- liftMSFTrans cond -< b
inMaybeT -< if c then Nothing else Just b
catchMaybe :: Monad m => MSF (MaybeT m) a b -> MSF m a b -> MSF m a b
catchMaybe msf1 msf2 = MSF $ \a -> do
cont <- runMaybeT $ unMSF msf1 a
case cont of
Just (b, msf1') -> return (b, msf1' `catchMaybe` msf2)
Nothing -> unMSF msf2 a
runMaybeS :: Monad m => MSF (MaybeT m) a b -> MSF m a (Maybe b)
runMaybeS msf = go
where
go = MSF $ \a -> do
bmsf <- runMaybeT $ unMSF msf a
case bmsf of
Just (b, msf') -> return (Just b, runMaybeS msf')
Nothing -> return (Nothing, go)