Is there any Haskell way of stopping/pickling/unpickling/resuming a computation ?
Some relevant discussion about this seemed to happen here but there was no proper solution presented. Also that discussion is old.
It would also be good, if there is some type of Event system for triggering the stopping and resuming state of the computation.
One (partial) method to do this is to operate in a
Partiality monad.Using this, we can create computations which return in the
Partialmonad and control their evaluation.Here we can use
runNto partially perform a computation, halting after at mostnsteps and returning the new, more-computed thunk or the actual result. We can also throw caution to the wind and userunto wait forever for aPartialmonad to execute.Interestingly, writing things in the
Partialmonad allows you to get some control over termination of programs. So long as eachStepin some computationfis terminating, thenrunN n fis always terminating as well. This is a kind of property we'd want for function pausing.The primary challenge of using the
Partialmonad is that it must infect every step of a computation in order to work---any pure computation inside of it will take at most oneStep. Another closely-tied problem is that you have to annotate your code withSteps manually. Finally,Steps have no particular guarantee to be similarly sized or have any relation to clocktime.The last problem at least could be solved by some concurrent programming of a worker thread which can receive signals to "pause as soon as possible" in its execution of a
Partialcomputation.Finally, it's very worth noting that
Partial ~ Free Identitywhich provides a nice generalization of the power ofPartialthat makes annotation ofSteps somewhat easier.