# What is a Comonad and how are they useful?

Recently I've been dusting off my knowledge on how Monads work. I've also been introduced to the concept of a 'Comonad', which is described as the inverse dual of a monad. However, I am impossible to wrap my head around it.

Monads can be seen as 'a blueprint to build conveyor belts of expressions'.

To define a new Monad(a new kind of conveyor-belt system) you need to define:

1. A way to put something on a conveyor belt, e.g. 'start' a conveyor belt. (Known as `unit` or `return`)
2. A way to connect a machine (an expression) that will be part of a conveyor belt to a conveyor belt. (Known as `join` or `bind` or `>>=`).

(There is a third operation that takes the current conveyor belt, throws its contents away and starts a new conveyor belt known as `>>`, but it is used very rarely.)

For the machines and conveyors to work properly together, you will need to make sure that:

1. If you put something on a conveyor belt and pass it through a machine, the output should be the same as when you pass it through the machine manually. (Left Identity)
2. If you want to put a conveyor-belt in-between an already existing conveyor belt, you should not end up with a conveyor belt that has a conveyor belt on top, but rather a single, longer conveyor belt. (Right Identity)
3. It should not matter for the output if you manually use machine A, and then pass the result through the conveyor-connected B-C, or if you use conveyor-connected A-B and then pass the result manually through C. In other words: ((a >>= b) >>= c) should be the same as (a >>= (b >>= c)) (Associativity)

The most simple conveyor-belt would be the one that just takes the input and always continues on to the next expression. This is what a 'pipeline' is.

Another possibility, is to only let it go through the next machine if some condition is met for the value. This means that if at some of the expressions in-between, the value changes to something that is no longer allowed, then the rest of the expressions will be skipped. This is what the 'Maybe' monad does in Haskell.

You can also do other fancy conditional copy/change rules on the values before or after you pass them to a machine. An example: Parsers (Here, if an expression returns a 'failure' result, the value from before the expression is used as output).

Of course the analogy is not perfect, but I hope it gives an okay representation of how monads work.

However, I am having a lot of trouble to turn this analogy on its head to understand Comonads. I know from the small amounts of information I've found on the internet that a Comonad defines:

• `extract`, which is sort of the reverse of `return`, that is, it takes a value out of a Comonad.
• `duplicate`, which is sort of the inverse of `join`, that is, it creates two Comonads from a single one.

But how can a Comonad be instantiated if we're only able to extract from them or duplicate them? And how can they actually be used? I've seen this very amazing project and the talk about it (which I unfortunately understood very little of), but I am not sure what part of the functionality is provided by a Comonad exactly.

What is a Comonad? What are they useful for? How can they be used? Are they edible?

• "how can a Comonad be instantiated if we're only able to extract from them or duplicate them?" - I'll answer your question with a question: how can a Monad be consumed if you're only able to lift values into them and sequence computations? Commented Jun 16, 2016 at 13:29
• The "machine at the end of the conveyor belt" (aside: I don't find analogies all that helpful when talking about monads) of the `IO` monad is the Haskell runtime system, which invokes `main`. There's also `unsafePerformIO`, of course. If you want to think of the `Maybe` monad as having a "machine at the end of the conveyor belt" you can use `maybe`. Commented Jun 16, 2016 at 14:00
• But, turning your explanation around, when you want to produce a comonadic value at the start of a chain of `cobind` applications, there must be some function which does something useful with the internal representation of your comonad. Commented Jun 16, 2016 at 14:01
• specific instance of comonad or monad can clearly have more functionality than required just to implement the typeclasses
– jk.
Commented Jun 16, 2016 at 14:13
• Not that this is going to be helpful if you don't approach this question from the category-theoretical / mathematical side, but I wanted to point out that a comonad is not the inverse but rather the dual of a monad. Commented Jun 16, 2016 at 16:27

A CoMonad `w` is a Functor with additional behaviour:

• `extract : w a -> a`
• `(>>-) : w a -> (w a -> b) -> w b`

Notice how this is similar to the definition of a Monad `m` where:

• `return : a -> m a`
• `(>>=) : m a -> (a -> m b) -> m b`

Categorically a Monad is the required structure for functions of the form `a -> m b` to compose and form the Kleisli category.

Similarly a CoMonad is the required structure for functions of the form `w a -> b` to compose and form the CoKleisli category.

Both generalise function composition.

# What are they useful for?

When programming the following definitions may be more intuitive:

• A Monad is a generic data type which can be converted into but complicates data retrieval.
• A CoMonad is a generic data type which can be converted out of but complicates data storage.

One common Monad is `Maybe` which is used when handling data that may or may not be present, similarly one may define a CoMonad `Drop` to handle data that you may choose to be present or not, this is helpful in linear programming.

Side note: `Drop` is the linear dual of `Maybe`, in general the linear dual of a Monad is a CoMonad and visa-versa.

Intuitively where Monads give those who create the data choice in exchange for giving those who manage the data work, CoMonads give those who create the data work in exchange for giving those who manage the data choice.

# How can they be used?

The CoMonad `Drop` defined above can be used to optionally drop data that would otherwise not be droppable such as a message channel, for example:

``````  wobj
>>- \obj ->
if x
then Just obj.extract
else None -- dropped
``````

# Are they edible?

As you can always `extract` from a CoMonad, they are just as consumable as their contents, so I would definitely call them edible!