{-|
Utilities for 'FRP.Rhine.ClSF.Except' that need not be exported.
-}

module FRP.Rhine.ClSF.Except.Util where

-- transformers
import Control.Monad.Trans.Except
import Control.Monad.Trans.Reader

-- | Commute a 'ReaderT' layer past an 'ExceptT' layer.
commuteExceptReader :: ExceptT e (ReaderT r m) a -> ReaderT r (ExceptT e m) a
commuteExceptReader :: ExceptT e (ReaderT r m) a -> ReaderT r (ExceptT e m) a
commuteExceptReader ExceptT e (ReaderT r m) a
a = (r -> ExceptT e m a) -> ReaderT r (ExceptT e m) a
forall r (m :: Type -> Type) a. (r -> m a) -> ReaderT r m a
ReaderT ((r -> ExceptT e m a) -> ReaderT r (ExceptT e m) a)
-> (r -> ExceptT e m a) -> ReaderT r (ExceptT e m) a
forall a b. (a -> b) -> a -> b
$ \r
r -> m (Either e a) -> ExceptT e m a
forall e (m :: Type -> Type) a. m (Either e a) -> ExceptT e m a
ExceptT (m (Either e a) -> ExceptT e m a)
-> m (Either e a) -> ExceptT e m a
forall a b. (a -> b) -> a -> b
$ ReaderT r m (Either e a) -> r -> m (Either e a)
forall r (m :: Type -> Type) a. ReaderT r m a -> r -> m a
runReaderT (ExceptT e (ReaderT r m) a -> ReaderT r m (Either e a)
forall e (m :: Type -> Type) a. ExceptT e m a -> m (Either e a)
runExceptT ExceptT e (ReaderT r m) a
a) r
r

-- | Commute the effects of the 'ReaderT' and the 'ExceptT' monad.
commuteReaderExcept :: ReaderT r (ExceptT e m) a -> ExceptT e (ReaderT r m) a
commuteReaderExcept :: ReaderT r (ExceptT e m) a -> ExceptT e (ReaderT r m) a
commuteReaderExcept ReaderT r (ExceptT e m) a
a = ReaderT r m (Either e a) -> ExceptT e (ReaderT r m) a
forall e (m :: Type -> Type) a. m (Either e a) -> ExceptT e m a
ExceptT (ReaderT r m (Either e a) -> ExceptT e (ReaderT r m) a)
-> ReaderT r m (Either e a) -> ExceptT e (ReaderT r m) a
forall a b. (a -> b) -> a -> b
$ (r -> m (Either e a)) -> ReaderT r m (Either e a)
forall r (m :: Type -> Type) a. (r -> m a) -> ReaderT r m a
ReaderT ((r -> m (Either e a)) -> ReaderT r m (Either e a))
-> (r -> m (Either e a)) -> ReaderT r m (Either e a)
forall a b. (a -> b) -> a -> b
$ \r
r -> ExceptT e m a -> m (Either e a)
forall e (m :: Type -> Type) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT e m a -> m (Either e a))
-> ExceptT e m a -> m (Either e a)
forall a b. (a -> b) -> a -> b
$ ReaderT r (ExceptT e m) a -> r -> ExceptT e m a
forall r (m :: Type -> Type) a. ReaderT r m a -> r -> m a
runReaderT ReaderT r (ExceptT e m) a
a r
r