Haskell(嵌套)ReaderT
如何访问 ReaderT 的内部 monad。
就我而言,我的类型是:
newtype VCSSetupAction a = VCSSetupAction (ReaderT (Maybe VCSConf) IDEM a)
deriving (Monad, MonadIO, MonadReader (Maybe VCSConf))
我无法访问在这个 Monad 中运行的函数中的(也许是 VCSConf)
commitAction' :: Common.VCSAction ()
commitAction' = do
config <- ask
...
,但我也应该能够访问内部 IDEM,它的类型是:
type IDEM = ReaderT IDERef IO
所以我希望能够做一些像
commitAction' :: Common.VCSAction ()
commitAction' = do
config <- ask
ideRef <- lift $ ask -- this does not compile/work
我仍然不太了解 Monad 的事情。感谢您的任何帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要直接使用
lift
,您的newtype
必须派生MonadTrans
,但由于它不是转换器,因此在本例中这是不合适的。相反,您可以将操作包装在数据构造函数中。现在您直接与ReaderT
打交道,因此您可以使用它的lift
。您可能想为此定义一个助手以使事情变得更清晰。
To use
lift
directly, yournewtype
would have to deriveMonadTrans
, 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 theReaderT
, so you can use itslift
.You might want to define a helper for this to make things cleaner.