progress with versioning lib

This commit is contained in:
Joeri Exelmans 2025-06-06 15:30:35 +02:00
parent 3623988f86
commit 618cdf7314
7 changed files with 236 additions and 91 deletions

View file

@ -1,9 +1,15 @@
import { compareDynamic } from "../compare/dynamic.js";
import { compareInts, compareSymbols } from "../compare/primitives.js";
import { makeTypeConstructor } from "../meta/type_constructor.js";
import { getDefaultTypeParser, makeTypeParser } from "../parser/type_parser.js";
import { newDynamic } from "../primitives/dynamic.js";
import { makeTypeParser } from "../parser/type_parser.js";
import { apply, newDynamic } from "../primitives/dynamic.js";
import { Dynamic, Int, UUID } from "../primitives/primitive_types.js";
import { makeEnumCompareFn } from "../structures/enum.js";
import { makeModuleEnum } from "../structures/enum.types.js";
import { add, emptySet } from "../structures/set.js";
import { makeStructCompareFn } from "../structures/struct.js";
import { makeModuleStruct } from "../structures/struct.types.js";
import { pretty } from "../util/pretty.js";
export const Slot = makeTypeConstructor("Slot__318d1c1a9336c141336c461c6a3207b0")(0);
export const Value = makeTypeConstructor("Value__23fc00a2db1374bd3dc1a0ad2d8517ab")(0);
@ -20,7 +26,7 @@ const [
[, {i: matchValue}],
] = makeModuleEnum(Value)([
{l: "literal" , r: Dynamic},
{l: "read" , r: Slot},
{l: "read" , r: Write}, // a 'read' reads the result of a Write
{l: "transform", r: Transformation},
]);
@ -32,7 +38,7 @@ const [
// match function
[, {i: matchSlot}],
] = makeModuleEnum(Slot)([
{l: "new" , r: UUID},
{l: "new" , r: UUID },
{l: "write", r: Write},
]);
@ -45,9 +51,9 @@ const [
// [, {i: getFn}],
// [, {i: getOutput}],
] = makeModuleStruct(Transformation)([
{l: "input" , r: Value},
{l: "fn" , r: Value},
{l: "output", r: Value},
{l: "input" , r: Value },
{l: "fn" , r: Value },
{l: "output", r: Dynamic},
]);
// Struct: Write
@ -55,41 +61,133 @@ const [
// constructor
[, {i: newWrite}],
// getters
// [, {i: getDepth}],
// [, {i: getSlot}],
// [, {i: getValue}],
// [, {i: getDepth}],
] = makeModuleStruct(Write)([
{l: "slot", r: Slot},
{l: "depth", r: Int }, // <- depth comes first, for performance
{l: "slot" , r: Slot },
{l: "value", r: Value},
{l: "depth", r: Int},
]);
// shorthands
export const getDepth = slot =>
matchSlot(slot)
(_slotNew => 0)
(_slotNew => 0n)
(slotWrite => slotWrite.depth);
export const overwrite = slot => value =>
// get the value from a Value
export const getVal = value =>
matchValue(value)
(literal => literal)
(read => read.value.value)
(transform => transform.output)
export const write = slot => value =>
newSlotWrite(
newWrite(slot)(value)(getDepth(slot)+1)
newWrite
(getDepth(slot)+1n)
(slot)
(value)
);
export const read = newValueRead;
export const transform = inValue => fnValue => newValueTransform(newTransformation(inValue)(fnValue));
export const transform = inValue => fnValue =>
newValueTransform
(newTransformation
(inValue)
(fnValue)
(apply(getVal(fnValue))(getVal(inValue))) // <- call function
);
export const read = slot =>
matchSlot(slot)
(_slotNew => { throw new Error("cannot read empty slot"); })
(write => write.value);
export const newSlot = newSlotNew;
export const toSlot = newSlotWrite;
export const newLiteral = newValueLiteral;
// comparison functions are mutually recursive...
const compareSlot = makeEnumCompareFn([
{l: "new" , r: () => compareSymbols},
{l: "write", r: () => compareWrite},
]);
const compareWrite = makeStructCompareFn([
{l: "slot" , r: () => compareSlot},
{l: "value", r: () => compareValue},
{l: "depth", r: () => compareInts},
]);
const compareTransformation = makeStructCompareFn([
{l: "input" , r: () => compareValue},
{l: "fn" , r: () => compareValue},
{l: "output", r: () => compareDynamic},
]);
const compareValue = makeEnumCompareFn([
{l: "literal" , r: () => compareDynamic},
{l: "read" , r: () => compareWrite},
{l: "transform", r: () => compareTransformation},
]);
export const findLCA = slotA => slotB => {
if (compareSlot(slotA)(slotB) === 0) {
return slotA;
}
if (getDepth(slotA) > getDepth(slotB)) {
// we can assume that slotA.variant === "write"
return findLCA(slotA.value.slot)(slotB);
}
else {
// we can assume that slotB.variant === "write"
return findLCA(slotA)(slotB.value.slot);
}
}
const emptySetOfSlots = emptySet(compareSlot);
export const merge = slotA => slotB => {
const lca = findLCA(slotA)(slotB);
if (compareSlot(lca)(slotA) === 0) {
return add(emptySetOfSlots)(slotB);
}
if (compareSlot(lca)(slotB) === 0) {
return add(emptySetOfSlots)(slotA);
}
const setWithA = add(emptySetOfSlots)(slotA);
const setWithAandB = add(setWithA)(slotB);
return setWithAandB;
}
const mkType = makeTypeParser({
extraPrimitives: [
["Value", Value],
["Slot", Slot],
]
["Slot" , Slot ],
["Write", Write],
],
});
export const ModuleVersioning = [
// slots
["newSlot" , newDynamic(newSlot )(mkType("UUID -> Slot" ))],
["write" , newDynamic(write )(mkType("Slot -> Value -> Slot" ))],
// utilty
["getDepth" , newDynamic(getDepth )(mkType("Slot -> Int" ))],
["overwrite" , newDynamic(overwrite )(mkType("Slot -> Value -> Slot" ))],
// values
["newLiteral", newDynamic(newLiteral)(mkType("Dynamic -> Value" ))],
["read" , newDynamic(read )(mkType("Slot -> Value" ))],
["transform" , newDynamic(transform )(mkType("Value -> Value -> Value"))],
["newSlot" , newDynamic(newSlot )(mkType("UUID -> Slot" ))],
["newLiteral", newDynamic(newLiteral)(mkType("Dynamic -> Value" ))],
// comparison
["compareSlot" , newDynamic(compareSlot )(mkType("Slot -> Slot -> Ordering"))],
["compareValue", newDynamic(compareValue)(mkType("Value -> Value -> Ordering"))],
// ["compareWrite", newDynamic(compareWrite)(mkType("Write -> Write -> Ordering"))],
// ["compareTransformation", newDynamic(compareTransformation)(mkType("Transformation -> Transformation -> Ordering"))],
];