The Haskell Wiki   MonadWriter UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View


The following describes Control.Monad.Writer of the hierarchical libraries (and specifically, from GHC).

1 Control.Monad.Writer

1.1 Description

The Writer monad adds output to a computation. Some straightforward uses are adding tracing and logging. However if your computation produces any output "on the side" then the Writer monad may be useful.

There is also a transformer version, WriterT.

1.2 Details

 runWriter :: (Monoid w) => Writer w a -> (a,w) 
This runs a Writer computation and returns a pair of the result and output.

 tell :: (Monoid w, MonadWriter w m) => w -> m () 
This outputs something, kind of like putStr, but more general.

 pass :: (Monoid w, MonadWriter w m) => m (a,w -> w) -> m a 
pass allows one to manipulate the output that occurs in its argument. For example,  runWriter (tell "hi" >> pass (return ((),const "there")))  gives you ((),"hithere"), but  runWriter (pass (tell "hi" >> return ((),const "there")))  gives you ((),"there").

 listen :: (Monoid w, MonadWriter w m) => m a -> m (a,w) 
This runs its first argument and returns its result and output. The output of the first argument is added to the final output of the computation.

 listens :: (MonadWriter w m) => (w -> w) -> m a -> m (a,w) 
listens works like listen, but provides a selector function

 censor :: (MonadWriter w m) => (w -> w) -> m a -> m a 
censor is a more convenient interface to pass when you want to pass the result straight through

1.3 Possible Implementation

Note: this implementation uses MultiParameterTypeClasses and FunDeps which are not Haskell 98

newtype Writer w a = Writer { runWriter :: (a,w) } 
 
instance (Monoid w) => Monad (Writer w) where 
    return a = Writer (a,mempty) 
    (Writer (a,w)) >>= f = let (a',w') = runWriter $ f a in Writer (a',w `mappend` w') 
 
class (Monoid w, Monad m) => MonadWriter w m | m -> w where 
    pass :: m (a,w -> w) -> m a 
    listen :: m a -> m (a,w) 
    tell :: w -> m () 
 
instance (Monoid w) => MonadWriter (Writer w) where 
    pass (Writer ((a,f),w)) = Writer (a,f w) 
    listen (Writer (a,w)) = Writer ((a,w),w) 
    tell s = Writer ((),s) 
 
listens :: (MonadWriter w m) => (w -> w) -> m a -> m (a,w) 
listens f m = do (a,w) <- m; return (a,f w) 
 
censor :: (MonadWriter w m) => (w -> w) -> m a -> m a 
censor f m = pass $ do a <- m; return (a,f) 

1.4 Examples

foo x = do 
    tell "foo entered" 
    r <- bar x 
    tell "foo left" 
    return r 

1.5 References

http://www.haskell.org/ghc/docs/latest/html/base/Control.Monad.Writer.html

1.6 Notes

Monoid is a class that represents mathematical monoids. The characteristics of a monoid are a collection of elements, a distinguished identity element, and an associative way to combine two elements into one. The most useful example of a monoid for the Writer monad is the monoid of lists, with [] as the identity and (++} as the binary associative operation. Another example of a monoid is the monoid of functions with id as the identity and (.) as the binary operation.


CategoryMonad
PythonPowered
EditText of this page (last modified 2003-07-24 05:20:21)
FindPage by browsing, title search , text search or an index
Or try one of these actions: LikePages, LocalSiteMap, SpellCheck