Writer monad is readily available in Haskell. But what about Writer comonad? Let us start with the basics. Comonad class is defined as:
The minimal implementation requires you to implement 'extract' and 'extend.' Additionally, it must satisfy the following comonad laws:
The 'extract' is pretty straightforward -- it allows safely extract value from the monad:
Our first intuition for 'extend' would be something like this:
Which is incorrect, as it losses the state accumulated so far and replaces it with 'mempty' and thus do not satisfy the 1st law above (extend extract = id).
Without further ado, the writer comonad definition below correctly preserves the state and meets all three laws: