摘要:在使用 Writer monad 时,我希望能够在 2 个不同版本之间切换而mappend
不会丢失状态。
我使用两个布尔标志来跟踪某些状态:
data Flags = F Bool Bool
现在我定义了两个Monoid
实例,它们在组合标志的方式上有所不同mappend
:
newtype XFlags = XF Flags
instance Monoid XFlags where
mempty = XF (F True False)
(XF (F s0 c0)) `mappend` (XF (F s1 c1)) = XF (F (s0 && s1)
(c0 || c1 || not (s0 || s1)))
newtype SFlags = SF Flags
instance Monoid SFlags where
mempty = SF (F True False)
(SF (F s0 c0)) `mappend` (SF (F s1 c1)) = SF (F (s0 && s1) (c0 || c1))
现在我可以拥有 2 个具有不同标志处理的 Writer monad:
type XInt = WriterT XFlags Identity Int
type SInt = WriterT SFlags Identity Int
现在我可以进行如下操作:
xplus :: XInt -> XInt -> XInt
xplus = liftM2 (+)
splus :: SInt -> SInt -> SInt
splus = liftM2 (+)
现在我想构建如下表达式:
foo = splus (return 1) (xplus (return 2) (return 3))
为此,我需要能够在不丢失任何标志的情况下在两者之间进行转换,最好不要打开 monad(使用runWriter
)。这部分我还没有弄清楚。看起来有点像我可以尝试使用 monad 转换器嵌套 Writers,但我不确定它是否直接适用于这里。我将不胜感激有关实施此类事情的最佳方法的一些指导。