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 { ModuleCompareStructures } from "./compare/structures.types.js";
import { ModuleCompareDynamic } from "./compare/dynamic.types.js";
import { ModuleVersioning } from "./versioning/types.js";
export const ModuleStd = [
// Symbols (for nominal types)
@ -47,4 +48,7 @@ export const ModuleStd = [
...ModuleComparePrimitives,
...ModuleCompareStructures,
...ModuleCompareDynamic,
// Versioning
...ModuleVersioning,
];

View file

@ -1,47 +1,94 @@
import { makeGeneric } from "../generics/generics.js";
import { makeTypeConstructor } from "../meta/type_constructor.js";
import { Int, UUID } from "../primitives/primitive_types.js";
import { enumType, makeModuleEnum } from "../structures/enum.types.js";
import { newProduct } from "../structures/product.js";
import { structType } from "../structures/struct.types.js";
import { prettyT } from "../util/pretty.js";
import { getDefaultTypeParser, makeTypeParser } from "../parser/type_parser.js";
import { Dynamic, Int, UUID } from "../primitives/primitive_types.js";
import { makeModuleEnum } from "../structures/enum.types.js";
import { makeModuleStruct } from "../structures/struct.types.js";
const Slot = makeTypeConstructor("Slot__318d1c1a9336c141336c461c6a3207b0")(1);
const Value = makeTypeConstructor("Value__23fc00a2db1374bd3dc1a0ad2d8517ab")(1);
makeModuleEnum(Value)([
export const Slot = makeTypeConstructor("Slot__318d1c1a9336c141336c461c6a3207b0")(0);
export const Value = makeTypeConstructor("Value__23fc00a2db1374bd3dc1a0ad2d8517ab")(0);
export const Transformation = makeTypeConstructor("Transformation__9eac70d7020c7c45d5a16f3f14b13083")(0);
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([
newProduct("value")(_ => Value || _valueType(a)(Slot)),
newProduct("rest" )(_ => enumType([
newProduct("new")(Slot => structType([
newProduct("uuid" )(_ => UUID),
newProduct("value")(_ => Value || _valueType(a)(Slot)),
])),
newProduct("overwrites")(Slot => structType([
newProduct("slot" )(_ => Slot),
newProduct("value")(_ => Value || _valueType(a)(Slot)),
newProduct("depth")(_ => Int),
])),
])),
// Enum: Slot
const [
// constructors
[, {i: newSlotNew}],
[, {i: newSlotWrite}],
// match function
[, {i: matchSlot}],
] = makeModuleEnum(Slot)([
{l: "new" , r: UUID},
{l: "write", r: Write},
]);
const _valueType = a => Slot => enumType([
newProduct("literal" )(_ => a),
newProduct("read" )(Value => Slot || _slotType(a)(Value)),
newProduct("transform")(Value => structType([
newProduct("in" )(_ => Value),
newProduct("fn" )(_ => Value),
newProduct("out")(_ => a),
])),
// Struct: Transformation
const [
// constructor
[, {i: newTransformation}],
// getters
// [, {i: getInput}],
// [, {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));
const valueType = makeGeneric(a => _valueType(a)(null));
// Struct: Write
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));
console.log("valueType:", prettyT(valueType));
// shorthands
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")}],
];