99 lines
2.6 KiB
JavaScript
99 lines
2.6 KiB
JavaScript
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'),
|
|
});
|
|
|
|
const id = x => x;
|
|
|
|
// Slot<a> -> Computation<a>
|
|
const read = slot => ({
|
|
kind: "transformation",
|
|
in: slot,
|
|
fn: {
|
|
kind: "value",
|
|
out: id,
|
|
},
|
|
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 => {
|
|
// console.log("transform...", "input:", pretty(input), "fn:", pretty(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 {
|
|
// console.log("transforming the transformation...");
|
|
return {
|
|
kind: "transformation",
|
|
in: input.in,
|
|
// 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,
|
|
};
|
|
}
|
|
};
|
|
|
|
const counterOne = newSlot(Symbol('counter'))(newValue(1));
|
|
|
|
const valueOne = read(counterOne);
|
|
|
|
console.log(pretty({valueOne}));
|
|
|
|
const inc = x => x + 1;
|
|
|
|
const onePlusOne = transform(valueOne)(newValue(inc));
|
|
|
|
console.log(pretty({onePlusOne}));
|
|
|
|
const onePlusOnePlusOne = transform(onePlusOne)(newValue(inc));
|
|
|
|
console.log(pretty({onePlusOnePlusOne}));
|
|
|
|
|
|
|
|
// const price = newSlot(Symbol('price'))(newValue(20.66));
|
|
// const tax = newSlot(Symbol('tax'))(newValue(4.34));
|
|
|
|
// 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));
|