Safe Haskell | None |
---|---|

Language | Haskell2010 |

This module provides monad transformer similar to
`WriterT`

, implemented using `StateT`

, making it
tail recursive. (The traditional writer always leaks space: see
here
for more information).

Pattern Synonyms
are used to provide the same interface as
`WriterT`

. Unfortunately, current GHC warns
whenever these patterns are used that there are unmatched patterns: the
COMPLETE pragma should solve
this problem in future version of GHC.

A pattern synonym is also provided for a non-transformer version of writer.
Again, this is just `StateT`

underneath, but its interface looks as if it was
defined like so:

newtype Writer w a = Writer { runWriter :: (a, w) }

- data WriterT s m a
- runWriterT :: Monoid s => WriterT s m a -> m (a, s)
- pattern WriterT :: forall m s a. (Functor m, Monoid s) => m (a, s) -> WriterT s m a
- execWriterT :: (Monad m, Monoid s) => WriterT s m a -> m s
- evalWriterT :: (Monad m, Monoid s) => WriterT s m a -> m a
- type Writer s = WriterT s Identity
- runWriter :: Monoid s => Writer s a -> (a, s)
- pattern Writer :: forall s a. Monoid s => (a, s) -> Writer s a
- execWriter :: Monoid s => Writer s a -> s
- evalWriter :: Monoid s => Writer s a -> a

# Transformer

A monad transformer similar to `WriterT`

, except
that it does not leak space. It is implemented using a state monad, so that
`mappend`

is tail recursive. See
this
email to the Haskell libraries committee for more information.

Wherever possible, coercions are used to eliminate any overhead from the newtype wrapper.

runWriterT :: Monoid s => WriterT s m a -> m (a, s) Source #

Run a writer computation in the underlying monad.

execWriterT :: (Monad m, Monoid s) => WriterT s m a -> m s Source #

Run a writer computation in the underlying monad, and collect its output.

evalWriterT :: (Monad m, Monoid s) => WriterT s m a -> m a Source #

Run a writer computation in the underlying monad, and return its result.

# Plain

type Writer s = WriterT s Identity Source #

A type synonym for the plain (non-transformer) version of `WriterT`

. This
can be used as if it were defined as:

newtype Writer w a = Writer { runWriter :: (a, w) }

runWriter :: Monoid s => Writer s a -> (a, s) Source #

Run a writer computation.

`>>>`

(["1","2","3","4","5"],[1,2,3,4,5])`runWriter $ traverse (\x -> Writer (show x, [x])) [1..5]`

pattern Writer :: forall s a. Monoid s => (a, s) -> Writer s a Source #

This pattern gives the newtype wrapper around `StateT`

the same interface
as as if it was defined like so:

newtype Writer w a = Writer { runWriter :: (a, w) }

Unfortunately GHC warns that a function is incomplete wherever this pattern is used. This issue should be solved in a future version of GHC, when the COMPLETE pragma is implemented.

`>>>`

[1,2,3,4,5]`execWriter $ traverse (\x -> Writer (x, [x])) [1..5]`

execWriter :: Monoid s => Writer s a -> s Source #

Run a writer computation, and collect its output.

evalWriter :: Monoid s => Writer s a -> a Source #

Run a writer computation, and return its result.