progress and some refactoring

This commit is contained in:
Joeri Exelmans 2025-03-31 15:35:02 +02:00
parent d236eca5e5
commit d8ca2f3999
25 changed files with 376 additions and 163 deletions

View file

@ -1,21 +1,17 @@
// to break up dependency cycles, type constructors are defined in their own JS module
import { Type } from "../primitives/types.js";
import { DefaultMap } from "../util.js";
import { getSymbol, makeTypeConstructor } from "../type_constructor.js";
// Function type
// The registry ensures that we never accidentally create more than one JS object for the same function type.
// It is a cheap workaround for JS lacking customizable hash-functions and equality-testing-functions.
// This same pattern is repeated throughout the code for all non-nullary type constructors (list, sum, product, ...)
export const symbolFunction = Symbol('Function');
const fnTypeRegistry = new DefaultMap(inType => new DefaultMap(outType => ({
symbol: symbolFunction,
params: [inType, outType],
})));
// type constructor
export const fnType = inType => outType => fnTypeRegistry.getdefault(inType, true).getdefault(outType, true);
const symbolFunction = Symbol('Function');
export const fnType = makeTypeConstructor(symbolFunction, 2);
export const isFunction = type => getSymbol(type) === symbolFunction;
// Convenience function. Wrapper around function below.
export const typedFnType = (instance, callback) => {
@ -41,35 +37,48 @@ export const typedFnType2 = callback => {
];
};
// Sum type
export const symbolSum = Symbol("Sum");
const sumTypeRegistry = new DefaultMap(leftType => new DefaultMap(rightType => ({
symbol: symbolSum,
params: [leftType, rightType],
})));
// type constructor
export const sumType = leftType => rightType => sumTypeRegistry.getdefault(leftType, true).getdefault(rightType, true);
const symbolSum = Symbol("Sum");
export const sumType = makeTypeConstructor(symbolSum, 2);
// Product type
export const symbolProduct = Symbol("Product");
const productTypeRegistry = new DefaultMap(leftType => new DefaultMap(rightType => ({
symbol: symbolProduct,
params: [leftType, rightType],
})));
// type constructor
export const prodType = leftType => rightType => productTypeRegistry.getdefault(leftType, true).getdefault(rightType, true);
const symbolProduct = Symbol("Product");
export const prodType = makeTypeConstructor(symbolProduct, 2);
// List type
export const symbolList = Symbol('List');
const listTypeRegistry = new DefaultMap(elementType => ({
symbol: symbolList,
params: [elementType],
}));
// type constructor
export const lsType = elementType => listTypeRegistry.getdefault(elementType, true);
const symbolList = Symbol('List');
export const lsType = makeTypeConstructor(symbolList, 1);
// Set type
const symbolSet = Symbol('Set');
export const setType = makeTypeConstructor(symbolSet, 1);
// Pretty print type
export function prettyT(type) {
if (type.typeVars) {
if (type.typeVars.size > 0) {
return `${[...type.typeVars].map(prettyT).join(", ")}: ${prettyT(type.type)}`;
}
return prettyT(type.type);
}
if (type.symbol === symbolFunction) {
return `${prettyT(type.params[0])} -> ${prettyT(type.params[1])}`;
}
if (type.symbol === symbolList) {
return `[${prettyT(type.params[0])}]`;
}
if (type.symbol === symbolProduct) {
return `(${prettyT(type.params[0])}, ${prettyT(type.params[1])})`;
}
if (type.symbol === symbolSum) {
return `(${prettyT(type.params[0])} | ${prettyT(type.params[1])})`;
}
if (type.params.length === 0) {
return type.symbol.description;
}
return `${type.symbol.description}(${type.params.map(prettyT).join(", ")})`;
}