create versioning module

This commit is contained in:
Joeri Exelmans 2025-06-06 11:30:38 +02:00
parent 62f85302fd
commit 7dafc1d659
2 changed files with 87 additions and 36 deletions

View file

@ -17,6 +17,7 @@ import { ModuleCompareTypes } from "./compare/type.types.js";
import { ModuleComparePrimitives } from "./compare/primitives.types.js"; import { ModuleComparePrimitives } from "./compare/primitives.types.js";
import { ModuleCompareStructures } from "./compare/structures.types.js"; import { ModuleCompareStructures } from "./compare/structures.types.js";
import { ModuleCompareDynamic } from "./compare/dynamic.types.js"; import { ModuleCompareDynamic } from "./compare/dynamic.types.js";
import { ModuleVersioning } from "./versioning/types.js";
export const ModuleStd = [ export const ModuleStd = [
// Symbols (for nominal types) // Symbols (for nominal types)
@ -47,4 +48,7 @@ export const ModuleStd = [
...ModuleComparePrimitives, ...ModuleComparePrimitives,
...ModuleCompareStructures, ...ModuleCompareStructures,
...ModuleCompareDynamic, ...ModuleCompareDynamic,
// Versioning
...ModuleVersioning,
]; ];

View file

@ -1,47 +1,94 @@
import { makeGeneric } from "../generics/generics.js";
import { makeTypeConstructor } from "../meta/type_constructor.js"; import { makeTypeConstructor } from "../meta/type_constructor.js";
import { Int, UUID } from "../primitives/primitive_types.js"; import { getDefaultTypeParser, makeTypeParser } from "../parser/type_parser.js";
import { enumType, makeModuleEnum } from "../structures/enum.types.js"; import { Dynamic, Int, UUID } from "../primitives/primitive_types.js";
import { newProduct } from "../structures/product.js"; import { makeModuleEnum } from "../structures/enum.types.js";
import { structType } from "../structures/struct.types.js"; import { makeModuleStruct } from "../structures/struct.types.js";
import { prettyT } from "../util/pretty.js";
const Slot = makeTypeConstructor("Slot__318d1c1a9336c141336c461c6a3207b0")(1); export const Slot = makeTypeConstructor("Slot__318d1c1a9336c141336c461c6a3207b0")(0);
const Value = makeTypeConstructor("Value__23fc00a2db1374bd3dc1a0ad2d8517ab")(1); export const Value = makeTypeConstructor("Value__23fc00a2db1374bd3dc1a0ad2d8517ab")(0);
export const Transformation = makeTypeConstructor("Transformation__9eac70d7020c7c45d5a16f3f14b13083")(0);
makeModuleEnum(Value)([ export const Write = makeTypeConstructor("Write__abaef8ddb5c167b5d2cedac111fcefd3")(0);
// Enum: Value
const [
// constructors
[, {i: newValueLiteral}],
[, {i: newValueRead}],
[, {i: newValueTransform}],
// match function
[, {i: matchValue}],
] = makeModuleEnum(Value)([
{l: "literal" , r: Dynamic},
{l: "read" , r: Slot},
{l: "transform", r: Transformation},
]); ]);
const _slotType = a => Value => structType([ // Enum: Slot
newProduct("value")(_ => Value || _valueType(a)(Slot)), const [
newProduct("rest" )(_ => enumType([ // constructors
newProduct("new")(Slot => structType([ [, {i: newSlotNew}],
newProduct("uuid" )(_ => UUID), [, {i: newSlotWrite}],
newProduct("value")(_ => Value || _valueType(a)(Slot)), // match function
])), [, {i: matchSlot}],
newProduct("overwrites")(Slot => structType([ ] = makeModuleEnum(Slot)([
newProduct("slot" )(_ => Slot), {l: "new" , r: UUID},
newProduct("value")(_ => Value || _valueType(a)(Slot)), {l: "write", r: Write},
newProduct("depth")(_ => Int),
])),
])),
]); ]);
const _valueType = a => Slot => enumType([ // Struct: Transformation
newProduct("literal" )(_ => a), const [
newProduct("read" )(Value => Slot || _slotType(a)(Value)), // constructor
newProduct("transform")(Value => structType([ [, {i: newTransformation}],
newProduct("in" )(_ => Value), // getters
newProduct("fn" )(_ => Value), // [, {i: getInput}],
newProduct("out")(_ => a), // [, {i: getFn}],
])), // [, {i: getOutput}],
] = makeModuleStruct(Transformation)([
{l: "input" , r: Value},
{l: "fn" , r: Value},
{l: "output", r: Value},
]); ]);
const slotType = makeGeneric(a => _slotType(a)(null)); // Struct: Write
const valueType = makeGeneric(a => _valueType(a)(null)); const [
// constructor
[, {i: newWrite}],
// getters
// [, {i: getSlot}],
// [, {i: getValue}],
// [, {i: getDepth}],
] = makeModuleStruct(Write)([
{l: "slot", r: Slot},
{l: "value", r: Value},
{l: "depth", r: Int},
]);
console.log("slotType:", prettyT(slotType)); // shorthands
console.log("valueType:", prettyT(valueType)); export const getDepth = slot =>
matchSlot(slot)
(_slotNew => 0)
(slotWrite => slotWrite.depth);
export const overwrite = slot => value =>
newSlotWrite(
newWrite(slot)(value)(getDepth(slot)+1)
);
export const read = newValueRead;
export const transform = inValue => fnValue => newValueTransform(newTransformation(inValue)(fnValue));
export const newSlot = newSlotNew;
export const newLiteral = newValueLiteral;
// const valueType = makeGeneric(a => _valueType(a)(_slotType)); const mkType = makeTypeParser({
extraPrimitives: [
["Value", Value],
["Slot", Slot],
]
});
export const ModuleVersioning = [
["getDepth", {i: getDepth, t: mkType("Slot -> Int")}],
["overwrite", {i: overwrite, t: mkType("Slot -> Value -> Slot")}],
["read", {i: read, t: mkType("Slot -> Value")}],
["transform", {i: transform, t: mkType("Value -> Value -> Value")}],
["newSlot", {i: newSlot, t: mkType("UUID -> Slot")}],
["newLiteral", {i: newLiteral, t: mkType("Dynamic -> Value")}],
];