haskell - Can a monad be a comonad? -
i know monad is. think have correctly wrapped mind around comonad is. (or rather, 1 is seems simple enough; tricky part comprehending what's useful this...)
my question is: can monad and comonad?
i foresee 2 possible answers:
- yes, common , useful.
- no, such different jobs there no reason want both.
so, it?
yes. turning comments answer:
newtype identity = identity {runidenity :: a} deriving functor instance monad identity return = identity join = runidentity instance comonad identity coreturn = runidentity cojoin = identity
reader , writer exact duals, shown by
class comonoid m comempty :: (m,a) -> comappend :: m -> (m,m) --every haskell type comonoid --that because cccs boring! instance monoid => monad ((,) a) return x = (mempty,x) join (a,(b,x)) = (a <> b, x) instance comonoid => comonad ((,) a) coreturn = comempty cojoin = associate . first comappend instance comonoid => monad ((->) a) return = flip (curry comempty) join f = uncurry f . comappend instance monoid => comonad ((->) a) coreturn f = f mempty cojoin f b = f (a <> b)
Comments
Post a Comment