{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Control.Monad.Module where
import Bound (Scope (..), (>>>=))
import Control.Monad.Trans.Class (MonadTrans (..))
import Data.Functor.Compose (Compose (..))
import Data.Functor.Identity (Identity (..))
class (Functor f, Monad m) => Module f m where
(>>==) :: f a -> (a -> m b) -> f b
infixl 1 >>==
mjoin :: Module f m => f (m a) -> f a
mjoin fma = fma >>== id
transAction :: (MonadTrans t, Monad m, Monad (t m)) => t m a -> (a -> m b) -> t m b
transAction tma amb = tma >>= lift . amb
composeAction :: (Functor f, Monad m) => Compose f m a -> (a -> m b) -> Compose f m b
composeAction (Compose fma) amb = Compose (fmap (>>= amb) fma)
instance Functor f => Module f Identity where
fa >>== k = fmap (runIdentity . k) fa
instance Monad m => Module (Scope b m) m where
(>>==) = (>>>=)