I am looking for a monad transformer that can be used to track the progress of a procedure. To explain how it would be used, consider the following code:
procedure :: ProgressT IO ()
procedure = task "Print some lines" 3 $ do
liftIO $ putStrLn "line1"
step
task "Print a complicated line" 2 $ do
liftIO $ putStr "li"
step
liftIO $ putStrLn "ne2"
step
liftIO $ putStrLn "line3"
-- Wraps an action in a task
task :: Monad m
=> String -- Name of task
-> Int -- Number of steps to complete task
-> ProgressT m a -- Action performing the task
-> ProgressT m a
-- Marks one step of the current task as completed
step :: Monad m => ProgressT m ()
I realize that step has to exist explicitly because of the monadic laws, and that task has to have an explicit step number parameter because of program determinism/the halting problem.
The monad as described above could, as I see it, be implemented in one of two ways:
- Via a function that would return the current task name/step index stack, and a continuation in the procedure at the point that it left off. Calling this function repeatedly on the returned continuation would complete the execution of the procedure.
- Via a function that took an action describing what to do when a task step has been completed. The procedure would run uncontrollably until it completed, “notifying” the environment about changes via the provided action.
For solution (1), I have looked at Control.Monad.Coroutine with the Yield suspension functor. For solution (2), I don’t know of any already available monad transformers that would be useful.
The solution I’m looking for should not have too much performance overhead and allow as much control over the procedure as possible (e.g. not require IO access or something).
Do one of these solutions sound viable, or are there other solutions to this problem somewhere already? Has this problem already been solved with a monad transformer that I’ve been unable to find?
EDIT: The goal isn’t to check whether all the steps have been performed. The goal is to be able to “monitor” the process while it is running, so that one can tell how much of it has been completed.
This is my pessimistic solution to this problem. It uses
Coroutines to suspend the computation on each step, which lets the user perform an arbitrary computation to report some progress.EDIT: The full implementation of this solution can be found here.
Can this solution be improved?
First, how it is used:
The above program outputs:
The actual implementation (See this for a commented version):