有没有逆变单子? [英] Are there contravariant monads?

查看:73
本文介绍了有没有逆变单子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数可以是协变和逆变的。这种协变/逆变二元性是否也适用于monads?

类似于:

  class Monad m其中
return :: a - > m a
(>> =):: m a - > (a - > m b) - > m b

class ContraMonad m其中
return :: a - > m a
contrabind :: m a - > (b→m a)→> m



类是否有意义?任何示例?

解决方案

当然,可以定义它,但我怀疑它会有什么用处。 / p>

有一句流行的说法:monad只是一个endofunctors类中的monoid。它的意思是,首先,我们有一类endofunctors(意思是,(协变)函子从某种类别到它自己),更重要的是,我们在这个函数的尾端(在这种情况下是成分)有一些乘法。然后monad适合一些我们现在不必担心的通用框架。重点是,不存在逆变函数的乘法。两个协变函数的构成又是一个协变函子;但是两个逆变函数的组合不是一个逆变函数(相反它是一个协变函数,所以是一个完全不同的野兽)。因此,逆变单体并不真正有道理。


Functors can be covariant and contravariant. Can this covariant/contravariant duality also be applied to monads?

Something like:

class Monad m where
  return :: a -> m a
  (>>=) :: m a -> (a -> m b) -> m b    

class ContraMonad m where
  return :: a -> m a
  contrabind :: m a -> (b -> m a) -> m b

Does ContraMonad class make sense? Any examples?

解决方案

Well, of course, it's possible to define it, but I doubt it would be of any use.

There is a popular saying that "monad is just a monoid in a category of endofunctors". What it means is, first of all, that we have a category of endofunctors (meaning, (covariant) functors from some category to itself), and what's more, we have some multiplication on this endofunctors (in this case — composition). And then monad fits into some general framework that we don't have to worry about right now. The point is, there is no "multiplication" of contravariant functors. Composition of two covariant functors is again a covariant functor; but composition of two contravariant functors is not a contravariant functor (rather it's a covariant functor, so, a totally different beast).

So, "contravariant monads" do not really make sense.

这篇关于有没有逆变单子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆