What are CoMonads, and what are they good for?

As far as I can tell, a CoMonad? is an object which defines its method =>> such that

a =>> b :: (w a -> (w a -> b) -> w b) -- w is the comonad type.This translates (I think, CorrectMe? if I'm wrong) to the more C++ like syntax of

template<typename A> abstract class CoMonad? { template<typename B> Functor<CoMonad?<A>, B> operator =>>(CoMonad?<A> a, B b) { } }This is in a similar vein as a monad (OnMonads) define their method >>= as

a >>= b :: (m a -> (a -> m b) -> m b) --m is the monad.I cannot, however, even hope to understand why they are useful. I don't even know if the above translation is right.

A CoMonad? is a structure with three operations: duplicate :: w a -> w (w a), fmap :: (a -> b) -> w a -> w b and extract :: w a -> a. It is very like a Monad, it is the dual of a Monad.

*What does that mean?*

Wel a Monad is a structure with three operations: join :: m (m a) -> m a, fmap :: ( a -> b ) -> m a -> m b, return :: a -> m a. As you can see, if you flip the arrows we have the comonad.

*So every CoMonad? can be turned into a Monad?*

Yes, it can. Just flip the arrows and supply new function definitions.

*What is the difference between the two?*

Besides the arrows are flipped, monads and comonads have different capabilities. On a concrete level, Monadic action composition is a two stage process:

- fmap the monadic action over the monad.
- monadic_action :: (a -> m b) and fmap :: (a -> m b) -> m a -> m (m b) gives us m a -> m ( m b)
- the stacked structure (m (m b)) can contain hidden information (besides a and b) for the programmer.

- now we collapse the stacked structure with join :: m (m a) -> m a and thus merge the hidden information of the two monads together.

CoMonadic? action is quite different:

- duplicate the structure, so we have w (w a)
- fmap the comonadic action over the duplicated structure.
- comonadic_action :: (w a -> b) and fmap :: (w a -> b) -> w (w a) -> w b gives us w (w a) -> w b

CoMonads are always safe to destruct. eg: CoMonads are useful for implementing evaluation, iteration.

*It is not that different*

No, it is not. If we reverse the arrows we go from creation to destruction. From defining side effects to evaluating side effects.

Does comonad<T>(monad<T>(t)) == t?

No, (Co)Monads are Functors. If they are their dual, they will not annihilate their functionalities. Compare the store and the state monad. These are dual to each other, but composed they give some very interesting structure. Namely lenses.

Here is a contribution to the discussion: http://gelisam.blogspot.com/2007/04/i-understand-comonads.html and also this which goes into it in more depth: http://gelisam.blogspot.com/2007/04/of-comonads-and-nightclubs.html

Further inspection of the first of these references gives a different definition from the one *[formerly]* given above. Note that the final term is *w b* not just *b*.

a =>> b :: (w a -> (w a -> b) -> w b) -- w is the comonad type.This material leads on to HaskellArrows which can be both monads and comonads.

I am attempting to implement some of this using FC++ (FunctoidsInCpp). -- JohnFletcher

I was working on this some time ago and eventually gave up. I have now returned to it and have been digging around to get some more definitions. One page which I have found which has helped a lot is the following:

http://conal.net/blog/posts/sequences-streams-and-segments

To quote a short section:

The return method injects a pure value into a monadic value (having no effect).

return :: Monad m => a -> m aThe dual to monadic return is extract (sometimes called “counit” or “coreturn“), which extracts a value out of a comonadic value (discarding the value’s context). category-extras library splites this method out from Comonad into the Copointed class:

extract :: Copointed w => w a -> aMonadic values are typically produced in effectful computations:

a -> m bComonadic values are typically consumed in context-sensitive computations:

w a -> bMonads have a way to extend a monadic producer into one that consumes to an entire monadic value:

(=<<) :: (Monad m) => (a -> m b) -> (m a -> m b)Dually, comonads have a way to extend a comonadic consumer into one that produces an entire comonadic value:

extend :: (Comonad w) => (w a -> b) -> (w a -> w b)

That for me is a revelation. The last two expressions show the difference between a Monad and a Comonad is the different way in which the operations work. It gives a way to see what needs to be defined for something to be a Comonad.

**Example 1**

The operations *head* and *tail* on a list define a comonad extraction (one element) and extension (a new list without the head).

**Example 2**

Taking a book from a library is an comonadic extraction process. I have to choose which book. If I take out several I have made a new library as a comonadic extension process.

Taking a book back is monadic. Taking several back is monadic extension.

I hope this helps someone else as much as it has helped me. Comonads are around us all the time, invisibly. -- JohnFletcher

EditText of this page (last edited December 25, 2012) or FindPage with title or text search