Abstract monad composition as a transformer

Sorry if the question seems a little trivial ... this is not for me. I happily wrote the following monad:

type SB i a = ReaderT ( AlgRO i ) (State ( AlgState i ) ) a

which, well, is a good monad. ReaderT is a monad transformer, and State is the state monad, and AlgRO and AlgState are data types parameterized in I for a mutable and read-only state, respectively. Now, if I want to make a neat monad transformer with newtype out of this, something like this:

newtype SbT m i a = SbT {
    runSbT:: m ( SB i a )
}

How can I continue? I can’t even compose a binding method (like Monad typeclass), and even more so an “elevator” (from MonadTrans) ... I think that automatic output can help, but I want to understand how it works in this case.

Thanks in advance.

+5
source share
2 answers

I do not think that the definition of for SbTis what you want. This defines functorial composition, and if the parameter mis equal to Functoror Applicative, this should preserve these properties. But such a composition, in general, does not create a new monad from the other two. See this question for more details .

, , ? , . , , . newtype , m , .

, , - , "" - , " " , , , - , , c. , , , . (f . g . h) x h, f "" .

, , , , , ,... ... , SB . , . . ? State - , , , . , - State , ? :

type State s = StateT s Identity

, . , Identity . :

type SB i a = ReaderT ( AlgRO i ) (State ( AlgState i ) ) a

:

type SB i a = ReaderT ( AlgRO i ) ( StateT ( AlgState i ) Identity ) a

:

type SB' i m a = ReaderT ( AlgRO i ) ( StateT ( AlgState i ) m ) a
type SB i a = SB' i Identity a

SB' , , , . newtype :

newtype SbT i m a = SbT { getSB :: ReaderT ( AlgRO i ) ( StateT ( AlgState i ) m ) a }

instance (Functor m) => Functor (SbT i m) where
    fmap f (SbT sb) = SbT (fmap f sb)

instance (Monad m) => Monad (SbT i m) where
    return x = SbT (return x)
    SbT m >>= k = SbT (m >>= (getSB . k))

instance MonadTrans (SbT i) where
    lift = SbT . lift . lift

runSbT :: SbT i m a -> AlgRO i -> AlgState i -> m (a, AlgState t)
runSbT (SbT m) e s = runStateT (runReaderT m e) s

, : runSbT , "" , . , lift , newtype. , , .

, MonadReader MonadState, .

+10

m ? :

newtype Sb i a = Sb { runSb :: SB i a }

... instance Monad (Sb i) . , ; ,

type SBT m i a = ReaderT (AlgRO i) (StateT (AlgState i) m) a
newtype SbT m i a = SbT { runSbT :: SBT m i a }

η- type ( " " ); SB SBT :

type SB i = ReaderT (AlgRO i) (State (AlgState i))
type SBT m i = ReaderT (AlgRO i) (StateT (AlgState i) m)
+2

All Articles