Re: [Haskell-cafe] generalizing the writer monad
On 17/10/2012, Petr P <petr.mvd@gmail.com> wrote:
Hi,
(this is a literate Haskell post.)
lately I was playing with the Writer monad and it seems to me that it is too tightly coupled with monoids. Currently, MonadWriter makes the following assumptions:
(1) The written value can be read again later. (2) For that to be possible it has to be monoid so that multiple (or zero) values can be combined.
I fell say that this is a bit restricting. Sometimes, the written value can be lost - either used to compute something else or for example sent out using some IO action to a file, network etc. For example, I'd like to create an IO-based writer monad whose `tell` logs its argument somewhere - prints it, stores to a file etc.
No need: newtype SequenceM m a = SequenceM (m a); instance (Monad m, Monoid a) => Monoid (SequenceM m a) where { mempty = SequenceM (return mempty); SequenceM mx `mappend` SequenceM my = SequenceM (liftM2 mappend mx my); } whatever :: (MonadWriter (SequenceM IO ()) m) => m (); whatever = tell (SequenceM (someIO :: IO ())); Cheers, Strake
Hi, thanks for your interesting ideas an inspiring answers. However I meant something a bit different. My point wasn't how to implement this problem with the current set of tools. Instead, I was wondering if it'd be worth expanding the current Reader/Writer library. Best regards, Petr 2012/10/18 Strake <strake888@gmail.com>:
On 17/10/2012, Petr P <petr.mvd@gmail.com> wrote:
Hi,
(this is a literate Haskell post.)
lately I was playing with the Writer monad and it seems to me that it is too tightly coupled with monoids. Currently, MonadWriter makes the following assumptions:
(1) The written value can be read again later. (2) For that to be possible it has to be monoid so that multiple (or zero) values can be combined.
I fell say that this is a bit restricting. Sometimes, the written value can be lost - either used to compute something else or for example sent out using some IO action to a file, network etc. For example, I'd like to create an IO-based writer monad whose `tell` logs its argument somewhere - prints it, stores to a file etc.
No need:
newtype SequenceM m a = SequenceM (m a);
instance (Monad m, Monoid a) => Monoid (SequenceM m a) where { mempty = SequenceM (return mempty); SequenceM mx `mappend` SequenceM my = SequenceM (liftM2 mappend mx my); }
whatever :: (MonadWriter (SequenceM IO ()) m) => m (); whatever = tell (SequenceM (someIO :: IO ()));
Cheers, Strake
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (2)
-
Petr P -
Strake