I just released mgree/trampoline 1.0.0 (github), a library for supporting long-running computations in Elm. Intensive, long-running computation can block the sending of messages, which makes the page unresponsive. My library offers support for writing such long-running computations and running them without blocking the UI or other parts of your program.
The key idea is cooperative multitasking: suspendable computations only work a few steps at a time and then “bounce off the trampoline” to restart by sending themselves a message. Other messages will then have a chance to be delivered and processed before
update function sees that it should continue running the computation.
There are two ways to write suspendable computations. One is to simply define your computation as a stepper, which takes an input of type
a and either yields another
a (for more stepping) or a result of some (possibly different) type
o. The other is to write your program in a monadic style, in what I’ve been calling the
I’ve hacked up several examples, with two examples deployed on mgree.github.io/trampoline (github): one of a diverging, upwards-counting computation, and one of an STLC interpreter. They are, of course, hideously ugly. But in both examples, the UI remains responsive even when running long computations.
This is my first Elm library, so feedback would be appreciated. I’m hopeful that this technology could, e.g., make the lambda calculus REPL posted recently able to handle long-running and diverging programs gracefully.