liveBook · Manning

WriterT

As explained in Learn Haskell by Example

WriterT is a Haskell monad transformer that accumulates output alongside the result of computations in an underlying monad.

WriterT is a monad transformer for accumulating output while an underlying monad performs other effectful computations. Its type, WriterT w m a, identifies the collected output w, underlying monad m, and computation result a. section 15.1.2

Use WriterT through a small set of practical choices:

  • Choose an output type with a Monoid instance so independently produced values can be combined. section 15.1.2
  • Call tell to add output without replacing the computation's ordinary result. section 15.1.2

Collecting directories during synchronization

A file-processing application can use type App a = WriterT [FilePath] IO a. After listing a directory, tell dirs records its subdirectories; recursive processing continues normally. Running runWriterT (example "foo") produces ((),["baz","bar"]).

For richer reports, define structured entries such as Success, Failure, and Skipped, wrap them in newtype Log = Log [LogEntry], and derive Semigroup and Monoid for the wrapper.

How Learn Haskell by Example covers it

How deep it goes
Explains type parameters, Monoid combination, tell, execution, and structured logs, but does not detail transformer internals.
Examples
Demonstrates directory-path collection and structured synchronization logs containing success, failure, and skipped entries.
What you can do after
Build file-processing computations that return ordinary results together with accumulated paths or structured reports.
How it connects
Connects WriterT with monad transformers, Monoid, StateT, ReaderT, and RWST.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.