forste
forste

Reputation: 1133

Haskell (nested) ReaderT

how can you access the inner monad of a ReaderT.

In my case I have the typ:

newtype VCSSetupAction a = VCSSetupAction (ReaderT (Maybe VCSConf) IDEM a)
    deriving (Monad, MonadIO, MonadReader (Maybe VCSConf))

I cann access the (Maybe VCSConf) in a function running in this Monad like

commitAction' :: Common.VCSAction ()
commitAction' = do
   config <- ask
   ...

but I should also be able to access the inner IDEM which turns out to be of type:

type IDEM = ReaderT IDERef IO

so I want to be able to do something like

commitAction' :: Common.VCSAction ()
commitAction' = do
   config <- ask
   ideRef <- lift $ ask -- this does not compile/work

I still do not understand Monads well enough for this. Thanks for any help.

Upvotes: 3

Views: 710

Answers (1)

hammar
hammar

Reputation: 139860

To use lift directly, your newtype would have to derive MonadTrans, but since it's not a transformer, this is not appropriate in this case. Instead, you can just wrap the action in your data constructor. Now you're dealing directly with the ReaderT, so you can use its lift.

ideRef <- VCSSetupAction $ lift $ ask

You might want to define a helper for this to make things cleaner.

Upvotes: 5

Related Questions