我正在尝试使用 Monad Transformers 两次嵌套 writer monad。这是一个草图:
import Control.Monad.Identity
import Control.Monad.Writer
data Struct = S Bool
instance Monoid Struct where
mempty = S True
mappend (S a) (S b) = S (a && b)
data Collision = C Bool
instance Monoid Collision where
mempty = C False
mappend (C a) (C b) = C (a || b)
type CSInt = WriterT Collision (WriterT Struct Identity) Int
foo :: Int -> CSInt
foo x = do (tell (S False)) ; return x
该foo
函数无法编译,因为我需要tell
在Struct
monad 上使用,而不是Collision
. 有可能吗?