This commit is contained in:
Joeri Exelmans 2025-03-23 09:15:37 +01:00
parent afd78c3b3e
commit 29d20b2273
25 changed files with 369 additions and 469 deletions

View file

@ -1,20 +1,16 @@
import { DefaultMap } from "./util.js";
import { Bool } from "./primitives/symbols.js";
import { typedFnType } from "./structures/function.js";
import { deepEqual } from "./util.js";
// 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 fnTypeRegistry = new DefaultMap(inType => new DefaultMap(outType => ({ in: inType, out: outType })));
// TODO: 'Type' (and its instances) are itself instances of (String,[Type]) (=the product type of String and list of Type)
// so is 'Type' just an alias for (String, [Type])
export const Type = { symbol: Symbol('Type'), params: [] };
// type constructor for function types
export const fnType = ({ in: inType, out: outType }) => fnTypeRegistry.getdefault(inType, true).getdefault(outType, true);
export const getSymbol = type => type.symbol;
export const getParams = type => type.params;
export const Type = Symbol('Type');
export const Function = Symbol('Function');
// Implementation of 'in' and 'out' functions,
// to get input/output type of a function signature:
export const getIn = fn => fn.in;
export const getOut = fn => fn.out;
// we can test whether types are equal:
export const eqType = deepEqual;
// a module is just a set of typed objects
// each 'typed object' is implicitly an instance of TypeLink (defined below)
@ -28,38 +24,14 @@ export const ModuleMetaCircular = {l:[
// Type : Type
{i: Type, t: Type},
// Function : Type
{i: Function, t: Type},
// ...typedFnType(getSymbol, fnType => fnType({in: Type, out: Int})),
// (Function -> Type) : Function
{i: fnType({in: Function, out: Type}), t: Function},
// ...typedFnType(getParams, fnType => fnType({in: Type, out: lsType(Type)})),
{i: getIn , t: fnType({in: Function, out: Type})},
{i: getOut, t: fnType({in: Function, out: Type})},
...typedFnType(eqType, fnType => fnType({
in: Type,
out: fnType({
in: Type,
out: Bool,
})})),
]};
// 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 }) => {
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 })),
];
};