when chaining functions, properly carry over read-dependencies the functions

This commit is contained in:
Joeri Exelmans 2025-04-16 15:44:34 +02:00
parent 248c41e99f
commit 7a5b9d4f38

View file

@ -16,17 +16,23 @@ const newValue = val => ({
// [inspect.custom]: (depth, options, inspect) => options.stylize(`newValue(${val})`, 'special'), // [inspect.custom]: (depth, options, inspect) => options.stylize(`newValue(${val})`, 'special'),
}); });
const id = x => x;
// Slot<a> -> Computation<a> // Slot<a> -> Computation<a>
const read = slot => ({ const read = slot => ({
kind: "transformation", kind: "transformation",
in: slot, in: slot,
fn: `${x => x}`, fn: {
kind: "value",
out: id,
},
out: slot.computation.out, out: slot.computation.out,
// [inspect.custom]: (depth, options, inspect) => options.stylize(`read(${inspect(slot)})`, 'special'), // [inspect.custom]: (depth, options, inspect) => options.stylize(`read(${inspect(slot)})`, 'special'),
}); });
// Computation<a> -> Computation<a -> b> -> Computation<b> // Computation<a> -> Computation<a -> b> -> Computation<b>
const transform = input => fn => { const transform = input => fn => {
// console.log("transform...", "input:", pretty(input), "fn:", pretty(fn));
const output = fn.out(input.out); const output = fn.out(input.out);
if (input.kind === "value") { if (input.kind === "value") {
// our input had no read dependency, thus likewise our output does not: // our input had no read dependency, thus likewise our output does not:
@ -36,31 +42,58 @@ const transform = input => fn => {
}; };
} }
else { else {
// console.log("transforming the transformation...");
return { return {
kind: "transformation", kind: "transformation",
in: input.in, in: input.in,
fn, // transform the transformation:
fn: transform(input.fn)(newValue(origFn => {
const descr = `${fn.out.name}${origFn.name}`;
return { [descr]: x => fn.out(origFn(x)) }[descr];
})),
out: output, out: output,
}; };
} }
}; };
const counterOne = newSlot(Symbol('counter'))(newValue(1));
const valueOne = read(counterOne);
const price = newSlot(Symbol('price'))(newValue(5)); console.log(pretty({valueOne}));
const tax = newSlot(Symbol('tax'))(newValue(1));
const total = const inc = x => x + 1;
transform(read(price))(
transform(read(tax))(
newValue(tax => price => price + tax)));
console.log(pretty({total,})); const onePlusOne = transform(valueOne)(newValue(inc));
console.log(pretty({onePlusOne}));
const onePlusOnePlusOne = transform(onePlusOne)(newValue(inc));
console.log(pretty({onePlusOnePlusOne}));
const getReadDependencies = computation => new Set([ // const price = newSlot(Symbol('price'))(newValue(20.66));
...(computation.kind === "value" ? [] : [computation.in, ...getReadDependencies(computation.fn)]), // const tax = newSlot(Symbol('tax'))(newValue(4.34));
]);
console.log("getReadDependencies(total):", getReadDependencies(total)); // console.log(pretty({price}));
// const total =
// transform(read(price))(
// transform(read(tax))(
// newValue(tax => price => price + tax)));
// console.log(pretty({total}))
// const totalPlusOne = transform(total)(newValue(x => x + 1));
// console.log(pretty({totalPlusOne}));
// const getReadDependencies = computation => new Set([
// ...(computation.kind === "value" ? [] : [computation.in, ...getReadDependencies(computation.fn)]),
// ]);
// console.log("getReadDependencies(total):", getReadDependencies(total));