Copyright | (C) 2021 Isaac Elliott |
---|---|
License | BSD-3 (see the file LICENSE) |
Maintainer | Isaac Elliott <isaace71295@gmail.com> |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- class (Monoid w, Monad m) => MonadTell w m | m -> w where
- tell :: w -> m ()
- newtype WrappedMonadWriter m a = WrappedMonadWriter {
- getWrappedMonadWriter :: m a
Documentation
class (Monoid w, Monad m) => MonadTell w m | m -> w where Source #
Laws
tell mempty ≡ pure ()
tell (a <> b) ≡ tell a *> tell b
How does this relate to MonadWriter
?
MonadTell
is a generalisation of MonadWriter
. It only provides tell
;
a function that 'appends' a monoidal value to some output. Morally, we have
class
. See MonadTell
w m => MonadWriter
w m where ...WrappedMonadWriter
for the witness of this.
MonadWriter
's listen
and pass
functions require the monad to hold onto
the output for an arbitrarily long time. This can cause applications to use memory
linear in the number of tell
s, when constant memory usage would suffice.
A motivating example is writing monoidal results to a file. Using MonadWriter
(via a WriterT
), you would have to accumulate the the entire output and then write it
to the file. In contrast, MonadTell
allows you to write each result to the file
as it's obtained, allowing the result to be freed while the rest of the program runs.
Nothing
default tell :: (m ~ t n, MonadTrans t, MonadTell w n) => w -> m () Source #
Instances
newtype WrappedMonadWriter m a Source #
A proof that a MonadWriter
instance implies a MonadTell
instance.