import { pretty } from "../util/pretty.js"; import { inspect } from "node:util"; // UUID -> Computation -> Slot const newSlot = uuid => computation => ({ overwrites: uuid, computation, // depth: 1, // [inspect.custom]: (depth, options, inspect) => options.stylize(`newSlot(${inspect(uuid)})(${inspect(computation)})`, 'special'), }); // a -> Computation const newValue = val => ({ kind: "value", out: val, // [inspect.custom]: (depth, options, inspect) => options.stylize(`newValue(${val})`, 'special'), }); const id = x => x; // Slot -> Computation 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 -> Computation b> -> Computation 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));