Can a monad be a comonad?

前端 未结 5 1970
孤独总比滥情好
孤独总比滥情好 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:40

    Yes. Turning some comments into an answer:

    newtype Identity a = Identity {runIdenity :: a} deriving Functor
    instance Monad Identity where
      return = Identity
      join = runIdentity
    instance CoMonad Identity where
      coreturn = runIdentity
      cojoin = Identity
    

    Reader and Writer are exact duals, as shown by

    class CoMonoid m where
      comempty :: (m,a) -> a
      comappend :: m -> (m,m)
    --every haskell type is a CoMonoid
    --that is because CCCs are boring!
    
    instance Monoid a => Monad ((,) a) where
      return x = (mempty,x)
      join (a,(b,x)) = (a <> b, x)
    instance CoMonoid a => CoMonad ((,) a) where
      coreturn = comempty
      cojoin = associate . first comappend
    
    instance CoMonoid a => Monad ((->) a) where
      return = flip (curry comempty)
      join f = uncurry f . comappend
    instance Monoid a => CoMonad ((->) a)  where
      coreturn f = f mempty
      cojoin f a b = f (a <> b)
    

提交回复
热议问题