85 lines
2.7 KiB
JavaScript
85 lines
2.7 KiB
JavaScript
// to break up dependency cycles, type constructors are defined in their own JS module
|
|
|
|
import { Type } from "../primitives/types.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, ...)
|
|
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) => {
|
|
const [t, typesOfFns] = typedFnType2(callback);
|
|
const res = [
|
|
{ i: instance, t },
|
|
...typesOfFns,
|
|
];
|
|
return res;
|
|
};
|
|
// Convenience function. Creates 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 = inType => outType => {
|
|
const fnT = fnType(inType)(outType);
|
|
fnTs.push(fnT);
|
|
return fnT;
|
|
};
|
|
const t = callback(wrappedFnType); // force evaluation
|
|
return [
|
|
t,
|
|
fnTs.map(fnT => ({ i: fnT, t: Type })),
|
|
];
|
|
};
|
|
|
|
// Sum type
|
|
|
|
const symbolSum = Symbol("Sum");
|
|
export const sumType = makeTypeConstructor(symbolSum)(2);
|
|
|
|
// Product type
|
|
|
|
const symbolProduct = Symbol("Product");
|
|
export const prodType = makeTypeConstructor(symbolProduct)(2);
|
|
|
|
// List type
|
|
|
|
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) {
|
|
// console.log("pretty:", 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(", ")})`;
|
|
}
|