dope2/type_registry.js

47 lines
2.2 KiB
JavaScript

// This module ensures that we never accidentally create more than one JS object for the same type twice.
// We do so by creating (nested) DefaultMap(s) for all non-nullary type constructors
// It is a cheap workaround for JS lacking customizable hash-functions and equality-testing-functions.
import { Function } from "./metacircular.js";
import { DefaultMap } from "./util.js";
const listTypeRegistry = new DefaultMap(elementType => ({listOf: elementType}));
const genericTypeRegistry = new DefaultMap(underlyingType => ({generic: underlyingType}));
const fnTypeRegistry = new DefaultMap(inType => new DefaultMap(outType => ({in: inType, out: outType})));
const productTypeRegistry = new DefaultMap(leftType => new DefaultMap(rightType => ({operator: "product", leftType, rightType})));
const sumTypeRegistry = new DefaultMap(leftType => new DefaultMap(rightType => ({operator: "sum", leftType, rightType})));
export const lsType = listTypeRegistry.getdefault.bind(listTypeRegistry);
export const genericType = genericTypeRegistry.getdefault.bind(genericTypeRegistry);
export const fnType = ({in: inType, out: outType}) => fnTypeRegistry.getdefault(inType, true).getdefault(outType, true);
export const sumType = (leftType, rightType) => sumTypeRegistry.getdefault(leftType, true).getdefault(rightType, true);
export const prodType = (leftType, rightType) => productTypeRegistry.getdefault(leftType, true).getdefault(rightType, true);
// Wrapper around function below.
export const typedFnType = (instance, callback) => {
const [t, typesOfFns] = typedFnType2(callback);
const res = [
{i: instance, t},
...typesOfFns,
];
return res;
}
// Create a function type, and also create Type-links for the function type (being typed by Function) and for all the nested function types. Saves a lot of code writing.
export const typedFnType2 = callback => {
const fnTs = [];
const wrappedFnType = ({in: inType, out: outType}) => {
console.log('wrappedFnType called')
const fnT = fnType({in: inType, out: outType});
fnTs.push(fnT);
return fnT;
}
const t = callback(wrappedFnType); // force evaluation
return [
t,
fnTs.map(fnT => ({i: fnT, t: Function})),
];
}