113
votes

This is a follow-up to the answer to my previous question.

Suppose I need to map each item a:A of List[A] to b:B with function def f(a:A, leftNeighbors:List[A]): B and generate List[B].

Obviously I cannot just call map on the list but I can use the list zipper. The zipper is a cursor to move around a list. It provides access to the current element (focus) and its neighbors.

Now I can replace my f with def f'(z:Zipper[A]):B = f(z.focus, z.left) and pass this new function f' to cobind method of Zipper[A].

The cobind works like this: it calls that f' with the zipper, then moves the zipper, callsf' with the new "moved" zipper, moves the zipper again and so on, and so on ... until the zipper reaches the end of the list.

Finally, the cobind returns a new zipper of type Zipper[B], which can be transformed to the list and so the problem is solved.

Now note the symmetry between cobind[A](f:Zipper[A] => B):Zipper[B] and bind[A](f:A => List[B]):List[B] That's why List is a Monad and Zipper is a Comonad.

Does it make sense ?

1
I'm not an expert, but that makes sense to me. I had an epiphany while reading your explanation. Thanks! - acjay
Your question is very difficult to answer in the SO format... but you're absolutely correct. Element-focused Zippers are always comonads. - J. Abrahamson
List can be viewed as a comonad just as well (in multiple ways), while a Zipper can be cast as a monad (also in many ways). The difference is in whether you are conceptually focused on "appending" data constructively to a state machine (that's what the Monad interface is about), or "extracting" state from it "deconstructively" (that's what the Comonad does). It is not easy to answer the question, stated as "does this understanding make sense", however. In some sense it does, in another it does not. - KT.
To cast something into a comonad you need to provide two operations: 1) Extraction of a value (e.g. it could be the head of the list) and 2) Application of a list-processing operation (e.g. you could apply it in a sliding-window manner along the list or in an element-wise manner or the like, assuming an appropriate unit transform won't change the list). Whether such a way of processing a list makes any sense is a separate issue. Note that a bare comonad interface does not provide neither a way to construct the list nor to traverse it. It only knows how to consume list-aware operations. - KT.
@eenblam You are right. I will add an answer and it will get this question off the unanswered list, I hope - Michael

1 Answers

2
votes

As this question is popping up regularly in the top of the "unanswered" list, let me just copy my comment as an answer here - nothing considerably more constructive has appeared since a year ago anyway.

A List can be viewed as a comonad just as well (in multiple ways), while a Zipper can be cast as a monad (also in many ways). The difference is in whether you are conceptually focused on "appending" data constructively to a state machine (that's what the Monad interface is about), or "extracting" state from it "deconstructively" (that's what the Comonad does).

It is not easy to answer the question, stated as "does this understanding make sense", however. In some sense it does, in another it does not.