问题描述
尝试熟悉,我注意到 newtype似乎会承认一个Functor实例,例如:
/ p>
fmap fa = a>>>但是不可能声明 Functor因为它们的种类不同( Functor 需要 *),所以成为 Arrow的超类 > * ,而箭头需要 * - > * - > * )。所以每个箭头都需要分别定义实例。
您可以用 ArrowMonad 包装任何箭头,然后给出 Applicative 实例(因此也是一个 Functor ):实例Arrow a => Applicative(ArrowMonad a)其中... 。
我没有看到为什么 Kleisli 缺少 Functor 实例。最可能的似乎是你不需要它。如果你想使用函子(或者应用或monadic)操作,你可以使用原始monad。当你需要箭头界面时,你只能将monad包装到 Kleisli 中。
While trying to familiarize myself with Control.Arrow, I have noticed that the Kleisli newtype would seem to admit a Functor instance, something like:
instance Monad m => Functor (Kleisli m a) where fmap f (Kleisli k) = Kleisli $ liftM f . kIs there a reason why this instance isn't provided? Does it exist in some package as an orphan instance?
解决方案Every arrow can be made into a valid Functor by defining
fmap f a = a >>> arr fHowever it's not possible to declare a Functor to be a superclass of Arrow because of their different kinds (Functor needs * -> * while Arrow needs * -> * -> *). So every arrow needs to define the instance separately.
You can wrap any arrow with ArrowMonad, which then gives an Applicative instance (and therefore also a Functor): instance Arrow a => Applicative (ArrowMonad a) where ....
I don't see any particular reason why Kleisli lacks the Functor instance. The most probable seems to be that you don't need it. If you want to use functorial (or applicative or monadic) operations, you do it on the original monad. You only wrap the monad into Kleisli when you need the arrow interface.
这篇关于为什么在Control.Arrow中没有Kleisli的Functor实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!