>= f" forall m f. lift m >>= f = ConduitM (Pi">
gpt4 book ai didi

haskell - 具有类约束的 GHC 重写规则

转载 作者:行者123 更新时间:2023-12-03 22:31:23 24 4
gpt4 key购买 nike

我已将以下重写规则添加到管道中,没有问题:

{-# RULES "ConduitM: lift x >>= f" forall m f.
lift m >>= f = ConduitM (PipeM (liftM (unConduitM . f) m))
#-}

我正在尝试为 liftIO 添加类似的重写规则以及
{-# RULES "ConduitM: liftIO x >>= f" forall m f.
liftIO m >>= f = ConduitM (PipeM (liftM (unConduitM . f) (liftIO m)))
#-}

但是,当我尝试这样做时,我从 GHC 收到以下错误消息:
Data/Conduit/Internal/Conduit.hs:1025:84:
Could not deduce (Monad m) arising from a use of ‘liftM’
from the context (Monad (ConduitM i o m), MonadIO (ConduitM i o m))
bound by the RULE "ConduitM: liftIO x >>= f"
at Data/Conduit/Internal/Conduit.hs:1025:11-118
Possible fix:
add (Monad m) to the context of the RULE "ConduitM: liftIO x >>= f"
In the first argument of ‘PipeM’, namely
‘(liftM (unConduitM . f) (liftIO m))’
In the first argument of ‘ConduitM’, namely
‘(PipeM (liftM (unConduitM . f) (liftIO m)))’
In the expression:
ConduitM (PipeM (liftM (unConduitM . f) (liftIO m)))

Data/Conduit/Internal/Conduit.hs:1025:108:
Could not deduce (MonadIO m) arising from a use of ‘liftIO’
from the context (Monad (ConduitM i o m), MonadIO (ConduitM i o m))
bound by the RULE "ConduitM: liftIO x >>= f"
at Data/Conduit/Internal/Conduit.hs:1025:11-118
Possible fix:
add (MonadIO m) to the context of
the RULE "ConduitM: liftIO x >>= f"
In the second argument of ‘liftM’, namely ‘(liftIO m)’
In the first argument of ‘PipeM’, namely
‘(liftM (unConduitM . f) (liftIO m))’
In the first argument of ‘ConduitM’, namely
‘(PipeM (liftM (unConduitM . f) (liftIO m)))’

我不知道有任何语法可以让我为重写规则指定这样的上下文。有没有办法做到这一点?

最佳答案

您可以在规则中使用约束指定参数的类型,例如

{-# RULES "ConduitM: liftIO x >>= f" forall m (f :: (Monad n, MonadIO n) => CounduitM i o n r).
liftIO m >>= f = ConduitM (PipeM (liftM (unConduitM . f) (liftIO m)))
#-}

(我没有测试过,因为我没有安装相关的包,但据我了解所涉及的类型,我认为应该可以。)

关于haskell - 具有类约束的 GHC 重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25324040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com