simplify 'enum'

This commit is contained in:
Joeri Exelmans 2025-06-03 10:42:36 +02:00
parent 366b1ec4e0
commit aee8d5b5e1
6 changed files with 113 additions and 59 deletions

View file

@ -1,6 +1,4 @@
import { capitalizeFirstLetter } from "../util/util.js";
import { newProduct as newProduct, getLeft } from "./product.js";
import { newLeft, newRight, match } from "./sum.js";
const eatParameters = (numParams, result) => {
if (numParams === 0) {
@ -9,29 +7,38 @@ const eatParameters = (numParams, result) => {
else return () => eatParameters(numParams-1, result);
}
export const makeMatchFn = variants => {
if (variants.length === 0) {
throw new Error("Bottom!");
export const makeMatchFn = variantNames => {
if (variantNames.length === 0) {
return _ => {
throw new Error("Bottom!");
};
}
const [_, ...remainingVariants] = variants;
return sum => handler => {
return match(sum)
(leftValue => eatParameters(remainingVariants.length, handler(leftValue)))
(rightValue => makeMatchFn(remainingVariants)(rightValue));
return enumm => {
return matchFn(enumm, variantNames);
}
};
const matchFn = (enumm, variantNames) => {
const [variantName, ...remaining] = variantNames;
return handler => {
if (enumm.variant === variantName) {
return eatParameters(
remaining.length,
handler(enumm.value));
}
else {
return matchFn(enumm, remaining);
}
};
};
export const makeConstructors = variants => {
if (variants.length === 0) {
return [];
export const makeConstructors = variantNames => {
if (new Set(variantNames).size !== variantNames.length) {
throw new Error("precondition failed: non-unique variant names");
}
const [variant, ...remainingVariants] = variants;
const name = getLeft(variant);
const ctorName = `new${capitalizeFirstLetter(name)}`;
const constructor = { [ctorName]: val => newLeft(val) }[ctorName];
return [
constructor,
...makeConstructors(remainingVariants).map(ctor =>
({[ctor.name]: val => newRight(ctor(val))}[ctor.name])),
];
return variantNames.map(variantName => {
const ctorName = `new${capitalizeFirstLetter(variantName)}`;
const ctor = { [ctorName]: value => ({variant: variantName, value}) }[ctorName];
return ctor;
})
};

View file

@ -1,10 +1,9 @@
import { makeCompareFn } from "../compare/dynamic.js";
import { makeGeneric } from "../generics/generics.js";
import { newDynamic } from "../primitives/dynamic.js";
import { Bottom } from "../primitives/primitive_types.js";
import { zip } from "../util/util.js";
import { makeConstructors, makeMatchFn } from "./enum.js";
import { getRight } from "./product.js";
import { fnType, sumType } from "./type_constructors.types.js";
import { getLeft, getRight } from "./product.js";
import { fnType } from "./type_constructors.types.js";
// 'variants' is an array of (name: string, type: Type) pairs.
// e.g., the list of variants:
@ -14,39 +13,39 @@ import { fnType, sumType } from "./type_constructors.types.js";
// results in the type:
// (Int | ([Int] | (Unit | ⊥)))
const _enumType = rootSelf => variants => {
// console.log("enumType..", variants);
if (variants.length === 0) {
return Bottom; // empty enum is equal to Bottom-type (cannot be instantiated)
}
const [variant, ...rest] = variants;
const variantType = getRight(variant);
return sumType
(self => {
// console.log("requested left type (of enumType)")
return variantType(rootSelf || self);
})
(self => {
// console.log("requested right type (of enumType)")
return _enumType(self)(rest)
});
};
// const _enumType = rootSelf => variants => {
// // console.log("enumType..", variants);
// if (variants.length === 0) {
// return Bottom; // empty enum is equal to Bottom-type (cannot be instantiated)
// }
// const [variant, ...rest] = variants;
// const variantType = getRight(variant);
// return sumType
// (self => {
// // console.log("requested left type (of enumType)")
// return variantType(rootSelf || self);
// })
// (self => {
// // console.log("requested right type (of enumType)")
// return _enumType(self)(rest)
// });
// };
export const enumType = _enumType(null);
// export const enumType = _enumType(null);
export const makeConstructorTypes = type => variants => {
return variants.map(variant => {
const variantType = getRight(variant);
return fnType(_ => variantType)(_ => type);
});
}
};
export const makeMatchFnType = type => variants => {
return makeGeneric(resultType =>
return makeGeneric(a =>
fnType
(_ => type)
(_ => handlerType(resultType)(type)(variants)));
}
(_ => handlerType(a)(type)(variants)));
};
const handlerType = resultType => type => variants => {
if (variants.length === 0) {
@ -57,25 +56,24 @@ const handlerType = resultType => type => variants => {
return fnType
(_ => fnType(_ => variantType)(_ => resultType)), // handler
(_ => handlerType(resultType)(type)(rest)); // rest
}
};
export const makeModuleEnum = type => variants => {
const ctors = makeConstructors(variants);
const variantNames = variants.map(getLeft);
const ctors = makeConstructors(variantNames);
const ctorTypes = makeConstructorTypes(type)(variants);
const matchFn = makeMatchFn(variants);
const matchFn = makeMatchFn(variantNames);
const matchFnType = makeMatchFnType(type)(variants);
const module = [
// ["type", newDynamic(type)(Type)],
// constructors:
...zip(ctors, ctorTypes)
.map(([ctor, ctorType]) => ["ctor", newDynamic(ctor)(ctorType)]),
// match-function:
newDynamic(matchFn, matchFnType),
["match", newDynamic(matchFn)(matchFnType)],
// compare-function:
newDynamic(makeCompareFn(enumType(variants)))
// // compare-function:
// newDynamic(makeCompareFn(enumType(variants)))
];
return module;
}
};

View file

@ -4,6 +4,6 @@
// Product-types of more fields (called Structs) can be constructed by nesting Product-types.
// In JS, all products are encoded in the same way:
export const newProduct = l => r => ({l, r});
export const newProduct = l => r => ({l, r}); // <-- apparently, this object-based encoding is faster than array-based, in both FF and Chrome.
export const getLeft = product => product.l;
export const getRight = product => product.r;