A Comonad and how are they useful

functional programmingmonad

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.

To understand Monads, I made the own analogy for myself:

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?

Best Answer

From my little understanding, a comonad is a Rube Goldberg machine to do post-docs:

http://www.willamette.edu/~fruehr/haskell/evolution.html

...sorry, I couldn't resist it.