I have added support the engines to the WASM version as well as by default enabled engine support in the single threaded version. Engines can be used for implementing coroutining (not to be confused with this term as it is used in the context of constraints).
-
Prolog.query()
has an additionaloptions
parameter that can be used to run the query in a separate engine. See below. This way, JavaScript can open multiple queries and generate answers from them in any order.Prolog.query("between(1,10,X)", {engine:true})
-
Prolog.forEach()
also got this option. As this function return aPromise
, we can create a thread using code as below. The new engine executes my_goal(X) and calls handle(X) on each answer. By default it yields back the the browser event loop every 10k inferences, allowing other tasks to proceed.
setTimeout(async () => {
await Prolog.forEach("my_goal(X)",
(a) => handle(a.X)),
{engine:true});
});
Enjoy the demos at SWI-Prolog WASM demos You can use the browser to see the source or download the git repo and check out the src/wasm
directory.
As is, everything uses the initial engine by default. I wonder whether or not it is a good idea to use a fresh engine by default for several tasks. The SWI-Prolog WASM benchmarks indicate that creating and destroying engines is fast (~8μs)