{-# LANGUAGE Arrows #-}
{-# LANGUAGE Rank2Types #-}
module Control.Monad.Trans.MSF.Maybe
( module Control.Monad.Trans.MSF.Maybe
, module Control.Monad.Trans.Maybe
, maybeToExceptS
) where
import Control.Monad.Trans.Maybe
hiding (liftCallCC, liftCatch, liftListen, liftPass)
import Control.Monad.Trans.MSF.Except
import Data.MonadicStreamFunction
exit :: Monad m => MSF (MaybeT m) a b
exit = constM $ MaybeT $ return Nothing
exitWhen :: Monad m => (a -> Bool) -> MSF (MaybeT m) a a
exitWhen condition = proc a -> do
_ <- exitIf -< condition a
returnA -< a
exitIf :: Monad m => MSF (MaybeT m) Bool ()
exitIf = proc condition -> if condition
then exit -< ()
else returnA -< ()
maybeExit :: Monad m => MSF (MaybeT m) (Maybe a) a
maybeExit = inMaybeT
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 <- liftTransS msf -< a
c <- liftTransS cond -< b
inMaybeT -< if c then Nothing else Just b
catchMaybe
:: (Functor m, Monad m)
=> MSF (MaybeT m) a b -> MSF m a b -> MSF m a b
catchMaybe msf1 msf2 = safely $ do
_ <- try $ maybeToExceptS msf1
safe msf2
listToMaybeS :: Monad m => [b] -> MSF (MaybeT m) a b
listToMaybeS = foldr iPost exit
runMaybeS :: (Functor m, Monad m) => MSF (MaybeT m) a b -> MSF m a (Maybe b)
runMaybeS msf = exceptS (maybeToExceptS msf) >>> arr eitherToMaybe
where
eitherToMaybe (Left ()) = Nothing
eitherToMaybe (Right b) = Just b
reactimateMaybe
:: (Functor m, Monad m)
=> MSF (MaybeT m) () () -> m ()
reactimateMaybe msf = reactimateExcept $ try $ maybeToExceptS msf
embed_ :: (Functor m, Monad m) => MSF m a () -> [a] -> m ()
embed_ msf as = reactimateMaybe $ listToMaybeS as >>> liftTransS msf