Haskell(嵌套)ReaderT

发布于 2024-12-18 12:36:16 字数 669 浏览 2 评论 0原文

如何访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2024-12-25 12:36:16

要直接使用 lift,您的 newtype 必须派生 MonadTrans,但由于它不是转换器,因此在本例中这是不合适的。相反,您可以将操作包装在数据构造函数中。现在您直接与 ReaderT 打交道,因此您可以使用它的lift

ideRef <- VCSSetupAction $ lift $ ask

您可能想为此定义一个助手以使事情变得更清晰。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文