为什么 Control.Monad.Morph.hoist 有 Monad 约束?
Why does Control.Monad.Morph.hoist have a Monad constraint?
Control.Monad.Morph
包括
class MFunctor t where
hoist :: Monad m => (forall a. m a -> n a) -> t m b -> t n b
据我所知,none 个包含的实例使用 Monad m
约束。怎么可能这样做呢?是否存在使用约束的有效实例(考虑到 hoist id = id
,我很难想象如何使用约束)? m
而不是 n
上的约束有什么意义?
Control.Monad.Morph
是 pipes, so I'd guess it's there because the MFunctor
instance for Proxy
needs it... And sure enough it's used there.
的衍生产品
instance MFunctor (Proxy a' a b' b) where
hoist nat p0 = go (observe p0)
where
go p = case p of
Request a' fa -> Request a' (\a -> go (fa a ))
Respond b fb' -> Respond b (\b' -> go (fb' b'))
M m -> M (nat (m >>= \p' -> return (go p')))
Pure r -> Pure r
不过我认为没有必要。 m >>= return . f
是 fmap f m
。它可能应该是一个 Functor
约束,并且该代码早于 monad-applicative 提案的实施。
Control.Monad.Morph
包括
class MFunctor t where
hoist :: Monad m => (forall a. m a -> n a) -> t m b -> t n b
据我所知,none 个包含的实例使用 Monad m
约束。怎么可能这样做呢?是否存在使用约束的有效实例(考虑到 hoist id = id
,我很难想象如何使用约束)? m
而不是 n
上的约束有什么意义?
Control.Monad.Morph
是 pipes, so I'd guess it's there because the MFunctor
instance for Proxy
needs it... And sure enough it's used there.
instance MFunctor (Proxy a' a b' b) where hoist nat p0 = go (observe p0) where go p = case p of Request a' fa -> Request a' (\a -> go (fa a )) Respond b fb' -> Respond b (\b' -> go (fb' b')) M m -> M (nat (m >>= \p' -> return (go p'))) Pure r -> Pure r
不过我认为没有必要。 m >>= return . f
是 fmap f m
。它可能应该是一个 Functor
约束,并且该代码早于 monad-applicative 提案的实施。