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

@ -27,7 +27,6 @@ const typeSymbolToCmp = new Map([
[symbolUnit , compareUnits ],
[symbolBottom , _ => _ => { throw new Error("Bottom!"); }],
[symbolUUID , compareSymbols ],
// [SymbolGenericType, ?] TODO
[symbolType , compareTypes ],
[symbolDynamic , compareDynamic ],
[symbolOrdering, compareOrderings],

View file

@ -1,10 +1,10 @@
import { eqType } from "../primitives/type.js";
import { isTypeVar } from "../primitives/typevars.js";
import { prettyT } from "../util/pretty.js";
import { pretty, prettyT } from "../util/pretty.js";
import { indent, zip } from "../util/util.js";
import { compareStrings } from "../compare/primitives.js";
import { getHumanReadableName } from "../primitives/symbol.js";
import { occurring, substitute } from "./generics.js";
import { occurring, recomputeTypeVars, substitute } from "./generics.js";
export const prettyS = (typevar, type) => {
return `${getHumanReadableName(typevar)}${prettyT(type)}`;
@ -180,3 +180,13 @@ export const unify = (typeA, typeB) => {
throw e;
}
};
export const assignFn = (functionType, parameterType) => {
const [rFunctionType, rParameterType] = recomputeTypeVars([functionType, parameterType]);
console.log(pretty(rFunctionType));
const rFunctionInType = rFunctionType.params[0](rFunctionType);
const rFunctionOutType = rFunctionType.params[1](rFunctionType);
const subs = unify(rFunctionInType, rParameterType);
const result = substitute(rFunctionOutType, subs);
return recomputeTypeVars([result])[0];
}

View file

@ -1,4 +1,5 @@
import { inspect } from "node:util";
import { assignFn } from "../generics/unify.js";
// import { assignFn } from "../generics/generics.js";
function inspectDynamic(_depth, options, inspect) {
@ -13,14 +14,14 @@ export const newDynamic = i => t => ({
export const getInst = lnk => lnk.i;
export const getType = lnk => lnk.t;
// export const apply = input => fun => {
// const inputType = getType(input)
// const funType = getType(fun);
// const outputType = assignFn(funType, inputType);
export const apply = fun => input => {
const inputType = getType(input)
const funType = getType(fun);
const outputType = assignFn(funType, inputType);
// const inputValue = getInst(input);
// const funValue = getInst(fun);
// const outputValue = funValue(inputValue);
const inputValue = getInst(input);
const funValue = getInst(fun);
const outputValue = funValue(inputValue);
// return newDynamic(outputValue)(outputType);
// };
return newDynamic(outputValue)(outputType);
};

View file

@ -1,3 +1,4 @@
import { compareStrings } from "../compare/primitives.js";
import { capitalizeFirstLetter } from "../util/util.js";
const eatParameters = (numParams, result) => {
@ -42,3 +43,14 @@ export const makeConstructors = variantNames => {
return ctor;
})
};
export const makeEnumCompareFn = variantCompares => {
return enumA => enumB => {
const cmp = compareStrings(enumA.variant)(enumB.variant);
if (cmp !== 0) {
return cmp;
}
const getCompareFn = variantCompares.find(({l: variantName}) => variantName === enumA.variant).r;
return getCompareFn()(enumA.value)(enumB.value);
}
};

View file

@ -28,3 +28,13 @@ export const makeGetters = fieldNames => {
}[getterName];
})
};
export const makeStructCompareFn = compares => {
return a => b => {
for (const {l: fieldName, r: getCompareFn} of compares) {
const cmp = getCompareFn()(a[fieldName])(b[fieldName]);
if (cmp !== 0) return cmp;
}
return 0;
}
}

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"))],
];