Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monitor computation process in Haskell

Tags:

haskell

I have a function in my main block

map anyHeavyFunction [list]

I'd like to show a progress bar during the computation process or add additional actions (pause, stop process etc.), but because map is a pure function I can't do it directly. I can guess I have to use monads, but what monad is appropriate? IO, State?

like image 403
Sergey Sosnin Avatar asked Sep 06 '13 19:09

Sergey Sosnin


1 Answers

I know there is at least one library on hackage that has some pre-made monad transformers for this task, but I normally turn to the pipes package to roll my own when I need one. I am using pipes-4.0.0 it is going to be on hackage this weekend, but you can grab it form the github repo before that.

I also used terminal-progress-bar package so that it makes a nice terminal animation as well.

{-# language BangPatterns #-}

import Pipes
import qualified Pipes.Prelude as P

import Control.Monad.IO.Class

import System.ProgressBar
import System.IO ( hSetBuffering, BufferMode(NoBuffering), stdout )

-- | Takes the total size of the stream to be processed as l and the function
-- to map as fn
progress l = loop 0
  where
    loop n = do
        liftIO $ progressBar (msg "Working") percentage 40 n l
        !x <- await -- bang pattern to make strict
        yield x
        loop (n+1)

main = do
    -- Force progress bar to print immediately 
    hSetBuffering stdout NoBuffering
    let n = 10^6
    let heavy x = last . replicate n $ x -- time wasting function
    r <- P.toListM $ each [1..100] >-> P.map heavy >-> progress 100
    putStrLn ""
    return r

This animates:

> Working [=>.......................]   7%

> Working [=====>...................]  20%

Every update erases the last bar so it only take up one line on the terminal. Then it finishes like so:

> main
Working [=========================] 100%
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
like image 139
Davorak Avatar answered Oct 06 '22 23:10

Davorak