experiment with encoding of read- and write-dependencies

This commit is contained in:
Joeri Exelmans 2025-04-16 14:51:12 +02:00
parent c6c49a0966
commit 248c41e99f

66
scripts/versioning2.js Normal file
View file

@ -0,0 +1,66 @@
import { pretty } from "../util/pretty.js";
import { inspect } from "node:util";
// UUID -> Computation<a> -> Slot<a>
const newSlot = uuid => computation => ({
overwrites: uuid,
computation,
// depth: 1,
// [inspect.custom]: (depth, options, inspect) => options.stylize(`newSlot(${inspect(uuid)})(${inspect(computation)})`, 'special'),
});
// a -> Computation<a>
const newValue = val => ({
kind: "value",
out: val,
// [inspect.custom]: (depth, options, inspect) => options.stylize(`newValue(${val})`, 'special'),
});
// Slot<a> -> Computation<a>
const read = slot => ({
kind: "transformation",
in: slot,
fn: `${x => x}`,
out: slot.computation.out,
// [inspect.custom]: (depth, options, inspect) => options.stylize(`read(${inspect(slot)})`, 'special'),
});
// Computation<a> -> Computation<a -> b> -> Computation<b>
const transform = input => fn => {
const output = fn.out(input.out);
if (input.kind === "value") {
// our input had no read dependency, thus likewise our output does not:
return {
kind: "value",
out: output,
};
}
else {
return {
kind: "transformation",
in: input.in,
fn,
out: output,
};
}
};
const price = newSlot(Symbol('price'))(newValue(5));
const tax = newSlot(Symbol('tax'))(newValue(1));
const total =
transform(read(price))(
transform(read(tax))(
newValue(tax => price => price + tax)));
console.log(pretty({total,}));
const getReadDependencies = computation => new Set([
...(computation.kind === "value" ? [] : [computation.in, ...getReadDependencies(computation.fn)]),
]);
console.log("getReadDependencies(total):", getReadDependencies(total));