This is a Typescript package that embeds Scryer Prolog.
Experimental, API will change. Currently it's (roughly) based on trealla-js's API.
npm install scryer
import { init, Prolog } from "scryer";
await init();
const pl = new Prolog();
const query = pl.query("X = 1 ; X = 2.");
for (const answer of query) {
console.log(answer.bindings);
}
For browsers, you can use esm.sh or other CDNs to import it directly:
<script type="module">
import { init, Prolog } from "https://esm.sh/scryer"; // ideally add version info to the URL, e.g. scryer@0.1.0
await init();
// query stuff
</script>
const answer = pl.queryOnce("X is Y * 2.", { bind: { Y: 21 } });
console.log(answer.bindings); // { X: 42, Y: 21 }
The prolog
string template literal is an easy way to escape terms.
Each ${value}
will be interpreted as a Prolog term.
import { prolog, Variable } from "scryer";
const answer = pl.queryOnce(
prolog`atom_chars(${new Variable("X")}, ${"Hello!"}).`,
);
console.log(answer.bindings); // { X: Atom('Hello!') }