nuprl / Stopify

A JS-to-JS compiler that makes it easier to build Web IDEs and compile to JS.
https://zenodo.org/records/10408254
BSD 3-Clause "New" or "Revised" License
168 stars 12 forks source link

Restart computation with control operators #241

Closed jpolitz closed 6 years ago

jpolitz commented 6 years ago

When Pyret pauses the stack, you can restart it in more than one way.

The important one is that the client of pauseStack can choose to call resume or error callbacks; the latter simulates a throw on the original stack.

With Stopify, it's "easy" to simulate it is by looking for a special return value (e.g. turn all resumes into Either-returning evaluations). This requires rewriting more code in Pyret than is particularly nice to do, and is a fundamentally different interface.

In the limit for Stopify this could mean supporting throw, continue, break, etc as restart options.

I suspect in practice throw is the only one that matters, but it does matter.

arjunguha commented 6 years ago

We can simulate throwing with something like this:

function throwableControl(f) {
  const r = control(
    k =>  ({ type: 'normal', value: f(r => k({ type: 'normal', value: r})),
    r => k({ type: 'exception', value: r}))});
  const r = control(k => f(k));
  if (r.type === 'exception') {
    throw r.value;
  }
  else {
    return r.value;
  }
}

throwableControl((returnK, throwK) => ... );

I'm not sure what it means to do continue and break.

rachitnigam commented 6 years ago

This is addressed by 33b9f6212240d45dfe8cddd44df9619604980786.