Can a monad be a comonad?

前端 未结 5 1968
孤独总比滥情好
孤独总比滥情好 2020-12-23 17:31

I know what a monad is. I think I have correctly wrapped my mind around what a comonad is. (Or rather, what one is seems simple enough; the tricky part is

5条回答
  •  一向
    一向 (楼主)
    2020-12-23 17:58

    It depends on what you consider a "monad" to be. If you ask "is it possible for a type to be an instance of both Monad and Comonad at once?" then, yes. Here's a trivial example.

    newtype Id a = Id a
    
    instance Monad Identity where
      return       = Id
      (Id a) >>= f = f a
    
    instance Comonad Identity where
      extract (Id a) = a
      extend f ida = Id (f ida)
    

    If you mean it mathematically, then a monad is a triple (X, return, bind) where X is a type and return and bind follow the types and laws you expect. Similarly, a comonad is (X, extend, extract). I've just demonstrated that the Xs might be the same, but since the types of extend and return or extract and bind are different it's not possible for them to be the same functions. So a mathematical monad can never be a comonad.

提交回复
热议问题