Writer Monad 是否保证正确的关联连接?
Haskell 中的验证声称使用 Writer
可以保证正确 -关联串联。然而,这个例子似乎表明情况并非如此。正确答案是什么?
{-# 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
生产:
"Hello" ++ ("World" ++ "World" ++ "") ++ "!"
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" ++ "") ++ "!"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这确实是不真实的。来自源代码 :
因此
mappend
链将镜像(>>=)
链。Yes, this is indeed untrue. From the source code:
So the chain of
mappend
s will mirror the chain of(>>=)
s.Writer [a]
不保证右关联连接,但您可以使用Writer (Endo [a])
获得有保证的右关联连接。Writer [a]
doesn't guarantee right-associative concatenation, but you can get guaranteed right-associative concatenation withWriter (Endo [a])
.