Bryan Vicknair | 25 Nov 23:54 2013
Picon

Writer + log each computation to stdout

Hello,

I have a bunch of little database IO functions.  Each does something to the
database, and returns a log string describing what it did, and possibly a
meaningful result from the database.

  query  :: IO (String, a)
  update :: a -> IO (String, ())

...and a few functions that orchestrate all the little functions into doing
useful work.

  syncWeek :: Week -> IO ()
  syncAll  : : IO ()

I don't want the individual functions to know what is done with the log string
describing what they did.  Top-level orchestrating functions should make that
decision, which can be one of:

1) Collect and print all to a log once all computations are done.
2) Print to stdout *as each computation is run*.
3) Ignore them.

Here is my understanding of how common monads would handle these requirements:

Writer: 1 and 3 are easy.  This is what I originally attempted to use, but I
        couldn't figure out how to accomplish #2.
Reader: 2 and 3 can be accomplished if each function reads a shouldLog config
        variable from the reader and does a putStrLn depending on the value.
        Very ugly, as now each function has to know how to log output.
(Continue reading)

Tillmann Rendel | 26 Nov 03:34 2013
Picon

Re: Writer + log each computation to stdout

Hi,

Bryan Vicknair wrote:
> I have a bunch of little database IO functions.  Each does something to the
> database, and returns a log string describing what it did, and possibly a
> meaningful result from the database.
>
>    query  :: IO (String, a)
>    update :: a -> IO (String, ())
>
> ...and a few functions that orchestrate all the little functions into doing
> useful work.
>
>    syncWeek :: Week -> IO ()
>    syncAll  : : IO ()
>
> I don't want the individual functions to know what is done with the log string
> describing what they did.  Top-level orchestrating functions should make that
> decision, which can be one of:
>
> 1) Collect and print all to a log once all computations are done.
> 2) Print to stdout *as each computation is run*.
> 3) Ignore them.

Instead of using an existing monad transformer, I would consider to 
write my own set of logging monad transformers. This could give you this 
interface:

   class MonadLog m where
     log :: String -> m ()
(Continue reading)

Alex Vieth | 26 Nov 06:27 2013

Re: Writer + log each computation to stdout

I have very little experience with monad transformers, so I took this
opportunity to learn by implementing one that will (hopefully!) solve
your problem. Here goes:

import Data.Monoid
import Control.Monad
import Control.Monad.Trans

-- Needed for an example stdout logger.
import System.IO.Unsafe

-- | LoggerT monad transformer.
-- runLoggerT demands a logging function of type a -> b (where b is a monoid).
-- It returns a monad-wrapped tuple, where the first entry is the logs and the
-- second is some value.
-- So, a is the log input type, b the log output type (probably [a]), m a monad,
-- and c some arbitrary type.
newtype LoggerT a b m c = LogT {
    runLoggerT :: (a -> b) -> m (b, c)
  }

instance (Monoid b, Monad m) => Monad (LoggerT a b m) where
  -- return is straightforward: ignore the log function and give mempty as the
  -- partial log.
  return x = LogT $ \_ -> return (mempty, x)
  -- Follow the steps for bind...
  (>>=) x k = LogT $ \l ->
    -- First we run the logger against the supplied logging function to get a
    -- base monad value
    let y = runLoggerT x l
(Continue reading)

Niklas Haas | 26 Nov 10:50 2013
Picon

Re: Writer + log each computation to stdout

On Mon, 25 Nov 2013 14:54:20 -0800, Bryan Vicknair <bryanvick <at> gmail.com> wrote:
> Hello,
> 
> I have a bunch of little database IO functions.  Each does something to the
> database, and returns a log string describing what it did, and possibly a
> meaningful result from the database.
> 
>   query  :: IO (String, a)
>   update :: a -> IO (String, ())
> 
> ...and a few functions that orchestrate all the little functions into doing
> useful work.
> 
>   syncWeek :: Week -> IO ()
>   syncAll  : : IO ()
> 
> 
> I don't want the individual functions to know what is done with the log string
> describing what they did.  Top-level orchestrating functions should make that
> decision, which can be one of:
> 
> 1) Collect and print all to a log once all computations are done.
> 2) Print to stdout *as each computation is run*.
> 3) Ignore them.
> 
> Here is my understanding of how common monads would handle these requirements:
> 
> Writer: 1 and 3 are easy.  This is what I originally attempted to use, but I
>         couldn't figure out how to accomplish #2.
> Reader: 2 and 3 can be accomplished if each function reads a shouldLog config
(Continue reading)

oleg | 26 Nov 11:25 2013

Re: Writer + log each computation to stdout


Bryan Vicknair wrote:

> I have a bunch of little database IO functions.  Each does something to the
> database, and returns a log string describing what it did, and possibly a
> meaningful result from the database.
> ...and a few functions that orchestrate all the little functions into doing
> useful work.
>
> I don't want the individual functions to know what is done with the log string
> describing what they did.  Top-level orchestrating functions should make that
> decision, which can be one of:
>
> 1) Collect and print all to a log once all computations are done.
> 2) Print to stdout *as each computation is run*.
> 3) Ignore them.

What an excellent case for extensible effects! Many thanks to Ben
Foppa for making the package available on hackage, as
extensible-effects.

Extensible Effects designed specifically to separate a client (the
requestor of an action) from a handler, the executor of an action. A
logging client merely requests a string to be logged. It has no idea
what happens when that request is executed. Therefore, the same
client can be used with several handlers, even within the same
expression.  One can also build various interceptors, such as
logIntercept, which is similar to Unix' tee.

The following file illustrates how to solve the problem with all three
(Continue reading)

Oliver Charles | 4 Dec 12:21 2013
Picon

Re: Writer + log each computation to stdout

On 11/26/2013 10:25 AM, oleg <at> okmij.org wrote:

> Extensible Effects designed specifically to separate a client (the
> requestor of an action) from a handler, the executor of an action. A
> logging client merely requests a string to be logged. It has no idea
> what happens when that request is executed. Therefore, the same
> client can be used with several handlers, even within the same
> expression.  One can also build various interceptors, such as
> logIntercept, which is similar to Unix' tee.
> 
> The following file illustrates how to solve the problem with all three
> scenarios. 
>         http://okmij.org/ftp/

Did you forget to attach a file to this email? Otherwise, I'm not sure
where to find the file you're talking about and I couldn't find it on
your homepage either.

- ocharles

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
oleg | 5 Dec 02:05 2013

Re: Writer + log each computation to stdout


> Extensible Effects designed specifically to separate a client (the
> requestor of an action) from a handler, the executor of an action. A
> logging client merely requests a string to be logged. It has no idea
> what happens when that request is executed. Therefore, the same
> client can be used with several handlers, even within the same
> expression.  One can also build various interceptors, such as
> logIntercept, which is similar to Unix' tee.
>
> The following file illustrates how to solve the problem with all three
> scenarios.
>         http://okmij.org/ftp/

I forgot to complete the link! I'm very sorry. Here is the correct
link to the code solving the posed problem:

        http://okmij.org/ftp/Haskell/extensible/WriterEx.hs
Alberto G. Corona | 4 Dec 12:51 2013
Picon

Re: Writer + log each computation to stdout

The Workflow package does logging (and recovery), and you can orchestrate the process using the normal do notation. You can also inspect the log after or during the process. You can also stop and restart it.

main= exec1 "weekllystuff"  $ do
     result  <- step   mylittleprocess1
     result2 <- step  mylittleprocess2
    . ...

At the  end you have a single log with the intermediate result  (in the folder .tcachedata/workflows) . If it is interrupted at any step, re-executing the program continue in the following step.

if you use read-show instances for the intermediate result, the log will be textual and you can inspect it with a text editor.


2013/11/25 Bryan Vicknair <bryanvick <at> gmail.com>
Hello,

I have a bunch of little database IO functions.  Each does something to the
database, and returns a log string describing what it did, and possibly a
meaningful result from the database.

  query  :: IO (String, a)
  update :: a -> IO (String, ())

...and a few functions that orchestrate all the little functions into doing
useful work.

  syncWeek :: Week -> IO ()
  syncAll  : : IO ()


I don't want the individual functions to know what is done with the log string
describing what they did.  Top-level orchestrating functions should make that
decision, which can be one of:

1) Collect and print all to a log once all computations are done.
2) Print to stdout *as each computation is run*.
3) Ignore them.

Here is my understanding of how common monads would handle these requirements:

Writer: 1 and 3 are easy.  This is what I originally attempted to use, but I
        couldn't figure out how to accomplish #2.
Reader: 2 and 3 can be accomplished if each function reads a shouldLog config
        variable from the reader and does a putStrLn depending on the value.
        Very ugly, as now each function has to know how to log output.
State:  Not sure, but the Writer docs in the transformers package points to
        this monad as maybe solving requirement #2 above.

The use case is that when I call the top-level functions from a command line
script, I want to see logging happening in real-time to stdout, but I may call
the same top-level functions from a larger application that may be logging to
somewhere other than stdout, and may call the top-level functions from yet
another larger application which doesn't want anything to be logged.

How can I glue together a bunch of smaller computations, which may call
each other, and decide at a higher level what to do with the logging result of
each computation?  Seems like a perfect fit for Writer, except for the
requirement to be able to print to stdout at each step.


Bryan Vicknair
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



--
Alberto.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane