[Haskell-cafe] StateWriter: a monad-writing exercise
Henning Thielemann
lemming at henning-thielemann.de
Thu Sep 25 10:34:18 EDT 2008
On Thu, 25 Sep 2008, Anthony LODI wrote:
> Hello haskell-cafe,
>
> In my application I have a complex state threaded through long
> computation chains, and I need a way to log all state changes (so that
> the evolving state can be animated/replayed somewhere else).
> Initially I tried combining State and Writer monads but this allows
> for the possibility to change the state, and forget to write a log
> entry, etc.
Instead of rewriting from scratch, you can also define
newtype StateWriter l s a = StateWriter (StateT s (Writer l) a)
and then the monad instance is just about wrapping and unwrapping. Or you
use {-# LANGUAGE GeneralizedNewtypeDeriving #-} and 'deriving Monad'.
More information about the Haskell-Cafe
mailing list