Published
Edited
Mar 9, 2021
Importers
1 star
Insert cell
Insert cell
demo = {
let db = [],
cache = {}, // we'll abuse this pointer to cache our compiled Prolog dictionary.
pre = ``; // if you want to inject method-like predicates into your Prolog, do so here.
let add = appendHook(db, cache, pre),
q = queryHook(cache);

add("predicate(value).");
return q("predicate(X).");
}
Insert cell
queryHook = compiled => {
return s => query(compiled['pl'])(s);
}
// even a stale query-function instance should hit the current database, due to our cache.
Insert cell
appendHook = (db, cache, pre = ``) => (...args) => {
db.unshift(...args); // putting new rows on top makes them easier to debug, shh
let str = `${pre} \n\n${db.join('\n')}`.trim();
cache['pl'] = pl`${str}`;

// mutable db.push(...args)
// mutable db = mutable db; // broadcast update
}
Insert cell
query = program => (
q,
// try to turn the 'links' returned by Tau Prolog into a clean dictionary of results.
// you can substitute your own munging function, but this is a sane default.
f = u =>
Object.fromEntries(
Object.keys(u)
.map(k => {
// handle Entity, Num, and _ by default.
if (u[k].id) return u[k].id == '_' ? null : [k, u[k].id];
else if (u[k].value || u[k].value === 0) return [k, u[k].value];
else return [k, u[k]];
})
.filter(k => k)
)
) => Array.from(program.query(q), s => f(s.links || s.args[0].args))
Insert cell
import {pl} from "@mbostock/hello-tau-prolog"
Insert cell
// mutable compiled = [pl`${mutable db.join('\n')}`]
Insert cell
/* dbCacheOp = {
compiled[0] = pl`${db.join('\n')}`; // cache the compiled history as it updates
} */
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more