Search code examples
haskellmonadsmonad-transformers

Redundancy in the MonadWriter class


I have been reading the documentation for the MonadWriter class, and I see that writer and tell can be written in terms of each other. It seems to me that if the class methods behave as the documentation describes, then pass can be written in terms of tell and listen:

pass :: m (a, w -> w) -> m a
pass m = do
    ((a, f), w) <- listen m
    tell $ f w
    return a

Is my understanding correct?


Solution

  • This ends up writing w <> f w, but it should write f w.

    ghci
    > import Control.Monad.Writer as W
    > :{
    pass m = do
      ((a, f), w) <- listen m
      tell (f w)
      return a
    :}
    > pass (writer ((0, map (+ 10)), [1])) :: Writer [Integer] Int
    WriterT (Identity (0,[1,11]))
    > W.pass (writer ((0, map (+ 10)), [1])) :: Writer [Integer] Int
    WriterT (Identity (0,[11]))