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 Partial
ity monad.
data Partial a = Done a | Step (Partial a)
deriving Functor
instance Monad Partial where
return = Done
Done x >>= f = f x
Step p >>= f = Step (p >>= f)
Using this, we can create computations which return in the Partial
monad and control their evaluation.
reversePartially :: [a] -> Partial [a]
reversePartially = rev [] where
rev acc [] = Done acc
rev acc (x:xs) = Step (rev (x:acc) xs)
runN :: Int -> Partial a -> Either (Partial a) a
runN _ (Done a) = Right a
runN 0 (Step p) = Left p
runN n (Step p) = runN (pred n) p
run :: Partial a -> a
run (Step p) = run p
run (Done a) = a
Here we can use runN
to partially perform a computation, halting after at most n
steps and returning the new, more-computed thunk or the actual result. We can also throw caution to the wind and use run
to wait forever for a Partial
monad to execute.
Interestingly, writing things in the Partial
monad allows you to get some control over termination of programs. So long as each Step
in some computation f
is terminating, then runN n f
is always terminating as well. This is a kind of property we'd want for function pausing.
The primary challenge of using the Partial
monad is that it must infect every step of a computation in order to work---any pure computation inside of it will take at most one Step
. Another closely-tied problem is that you have to annotate your code with Step
s manually. Finally, Step
s 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 Partial
computation.
Finally, it's very worth noting that Partial ~ Free Identity
which provides a nice generalization of the power of Partial
that makes annotation of Step
s somewhat easier.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With