我有一个协程变压器
data Step y m a = Done a | Yield y (CoT y m a)
data CoT y m a = CoT (m (Step y m a))
with Monad
实例
unCoT :: CoT y m a -> m (Step y m a)
unCoT (CoT m) = m
instance Monad m => Monad (CoT y m) where
return = CoT . return . Done
CoT x >>= f = CoT $ do
x' <- x
case x' of
Done a -> unCoT (f a)
Yield y x' -> return (Yield y (x' >>= f))
如果我定义一个MFunctor
与 一起上课Monad m
and Monad n
我可以定义的约束hoist
class MFunctor t where
hoist :: (Monad n, Monad m) => (forall a. m a -> n a) -> t m b -> t n b
instance MFunctor (CoT y) where
hoist f (CoT m) = CoT $ do
step <- f m
return (case step of Done x -> Done x
Yield y m' -> Yield y (hoist f m'))
But mmorph
's hoist
只有一个Monad m
约束。我可以定义我的hoist
没有它,或者这是缺乏通用性MFunctor
?
编辑:我解决了is可能的!但我的问题仍然存在:我们确定这里不缺乏普遍性吗?
instance MFunctor (CoT y) where
hoist f (CoT m) = CoT $ f $ do
step <- m
return (case step of Done x -> Done x
Yield y m' -> Yield y (hoist f m'))
mmorph
是在以下背景下开发的pipes-3.*
系列(以前是一个内部的pipes module http://hackage.haskell.org/package/pipes-3.1.0/docs/Control-MFunctor.html),其功能如下:
raise
:: (Monad m, MFunctor t1, MonadTrans t2)
=> t1 m r -> t1 (t2 m) r
raise = hoist lift
如果您添加Monad n
约束到hoist
那么你必须添加一个Monad (t2 m)
约束到raise
。我通常会尝试尽量减少图书馆中的限制,但我找不到任何限制MFunctor
需要的实例Monad n
约束,所以我把它删除了。
边注:CoT y m a
是一样的Producer y m a
from pipes
,其中已经有一个MFunctor
实例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)