Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Writer Monad guarantee right associative concatenation?

It was claimed in Validations in Haskell that use of a Writer guarantees right-associative concatenation. However, this example seems to show otherwise. What's the correct answer?

{-# LANGUAGE OverloadedStrings #-}

import Control.Monad.Writer
import Data.String

data TM = TMempty
        | TMappend TM TM
        | TMfromString String

instance IsString TM where
  fromString = TMfromString

instance Monoid TM where
  mempty  = TMempty
  mappend = TMappend

instance Show TM where
  showsPrec d TMempty = showString "\"\""
  showsPrec d (TMfromString s) = showString $ show s
  showsPrec d (TMappend a b) = showParen (d > 0) $
    showsPrec 1 a .
    showString " ++ " .
    showsPrec 0 b

theWriter :: Writer TM ()
theWriter = do
  tell "Hello"
  replicateM_ 2 $ tell "World"
  tell "!"

main = print $ execWriter theWriter

Produces:

"Hello" ++ ("World" ++ "World" ++ "") ++ "!"
like image 387
pat Avatar asked Jan 04 '12 18:01

pat


2 Answers

Yes, this is indeed untrue. From the source code:

m >>= k  = WriterT $ do
    ~(a, w)  <- runWriterT m
    ~(b, w') <- runWriterT (k a)
    return (b, w `mappend` w')

...

-- | @'tell' w@ is an action that produces the output @w@.
tell :: (Monoid w, Monad m) => w -> WriterT w m ()
tell w = WriterT $ return ((), w)

So the chain of mappends will mirror the chain of (>>=)s.

like image 127
ehird Avatar answered Nov 20 '22 11:11

ehird


Writer [a] doesn't guarantee right-associative concatenation, but you can get guaranteed right-associative concatenation with Writer (Endo [a]).

like image 42
Jeremy List Avatar answered Nov 20 '22 10:11

Jeremy List