branching and very basic merging of slots
This commit is contained in:
parent
614e6c0fdb
commit
3978f7f835
32 changed files with 684 additions and 420 deletions
95
versioning/value.js
Normal file
95
versioning/value.js
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
import { deepEqual } from "../util/util.js";
|
||||
import {inspect} from "node:util";
|
||||
|
||||
// A Value is either:
|
||||
// - a literal, without any dependencies.
|
||||
// - read from a slot. the Value then has a read-dependency on that slot.
|
||||
// - a transformation of another Value, by a function. the function is also a Value.
|
||||
|
||||
// a -> Value<a>
|
||||
export const newLiteral = val => ({
|
||||
kind: "literal",
|
||||
out: val,
|
||||
[inspect.custom]: (depth, options, inspect) => `newLiteral(${inspect(val)})`,
|
||||
});
|
||||
|
||||
// Slot<a> -> Value<a>
|
||||
export const read = slot => ({
|
||||
kind: "read",
|
||||
slot,
|
||||
out: slot.value.out,
|
||||
[inspect.custom]: (depth, options, inspect) => `read(${inspect(slot)})`,
|
||||
});
|
||||
|
||||
// Value<a> -> Value<a -> b> -> Value<b>
|
||||
export const transform = input => fn => {
|
||||
const output = fn.out(input.out);
|
||||
const _inspect = (depth, options, inspect) => `transform(${inspect(input)}, ${inspect(fn)})`;
|
||||
if (input.kind === "literal") {
|
||||
// optimization: sandwich everything together
|
||||
return {
|
||||
kind: "literal",
|
||||
out: output,
|
||||
// [inspect.custom]: _inspect,
|
||||
};
|
||||
}
|
||||
else {
|
||||
return {
|
||||
kind: "transformation",
|
||||
in: input,
|
||||
fn,
|
||||
out: output,
|
||||
// [inspect.custom]: _inspect,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// Value<a> -> Set<Slot<Any>>
|
||||
export const getReadDependencies = value => {
|
||||
if (value.kind === "literal") {
|
||||
return new Set();
|
||||
}
|
||||
else if (value.kind === "read") {
|
||||
return new Set([value.slot]);
|
||||
}
|
||||
else if (value.kind === "transformation") {
|
||||
return new Set([
|
||||
...getReadDependencies(value.in),
|
||||
...getReadDependencies(value.fn),
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
// for debugging
|
||||
export const verifyValue = (value, indent = 0) => {
|
||||
let success = true;
|
||||
const printIndent = (...args) => {
|
||||
console.log(" ".repeat(indent * 2), ...args);
|
||||
};
|
||||
const compare = (a, b, kind) => {
|
||||
if (typeof a === 'function' && typeof b === 'function') {
|
||||
printIndent("note: cannot compare functions", `(${kind})`);
|
||||
}
|
||||
else if (deepEqual(a, b)) {
|
||||
printIndent(`ok (${kind})`);
|
||||
}
|
||||
else {
|
||||
printIndent(`bad (${kind})`);
|
||||
success = false;
|
||||
}
|
||||
};
|
||||
if (value.kind === "literal") {
|
||||
compare(1, 1, "literal");
|
||||
}
|
||||
else if (value.kind === "read") {
|
||||
compare(value.out, value.slot.value.out, "read");
|
||||
}
|
||||
else if (value.kind === "transformation") {
|
||||
compare(value.fn.out(value.in.out),
|
||||
value.out, "transformation");
|
||||
|
||||
success &= verifyValue(value.in, indent + 1);
|
||||
success &= verifyValue(value.fn, indent + 1);
|
||||
}
|
||||
return success;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue