Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping/Pickling/Unpickling/Resuming a computation

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.

like image 339
Sibi Avatar asked Oct 21 '22 17:10

Sibi


1 Answers

One (partial) method to do this is to operate in a Partiality 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 Steps 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 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 Steps somewhat easier.

like image 176
J. Abrahamson Avatar answered Dec 18 '22 19:12

J. Abrahamson