progress with type classes, type inference still very ad-hoc

This commit is contained in:
Joeri Exelmans 2025-03-20 18:34:58 +01:00
parent 5283be608b
commit c5ac55b0ff
10 changed files with 64 additions and 19 deletions

View file

@ -1,8 +1,9 @@
import { lsType } from "../structures/list_common.js";
import { fnType } from "../metacircular.js";
import { deepEqual, pretty } from "../util.js";
import { deepEqual, DefaultMap, pretty } from "../util.js";
import { numDictType } from "../typeclasses/num_type.js";
const genericTypeRegistry = new DefaultMap(underlyingType => ({generic: underlyingType}));
const genericTypeRegistry = new DefaultMap(underlyingType => ({ generic: underlyingType }));
// type constructor for generic kinds,
// for instance:
@ -23,15 +24,15 @@ export const makeGeneric = callback => {
const c = Symbol('c');
const d = Symbol('d');
const e = Symbol('e');
const type = callback(a,b,c,d,e);
const type = callback(a, b, c, d, e);
return {
typeVars: occurring(type, new Set([a,b,c,d,e])),
typeVars: occurring(type, new Set([a, b, c, d, e])),
type,
};
};
// From the given set of type variables, return only those that occur in the given type.
const occurring = (type, typeVars) => {
export const occurring = (type, typeVars) => {
if (typeVars.has(type)) {
return new Set([type]);
}
@ -130,9 +131,24 @@ export const unify = (
}
if (formalType.numDict !== undefined) {
if (actualType.numDict === undefined) {
throw new Error(`cannot assign ${pretty(actualType)} to ${pretty(formalType)}`);
}
else {
// both are NumDict type
const underlyingType = unify(
{typeVars: formalTypeVars, type: formalType.numDict},
{typeVars: actualTypeVars, type: actualType.numDict});
return {
substitutions: underlyingType.substitutions,
typeVars: new Set([
...actualTypeVars,
...formalTypeVars].filter(a => !underlyingType.substitutions.has(a))),
type: numDictType(underlyingType.type),
};
}
}
throw new Error("i don't know what to do :(")
throw new Error("i don't know what to do :(");
};
// export const matchConcrete = ({typeVars, type: formalType}, actualType) => {
@ -167,4 +183,4 @@ export const assign = (genFnType, paramType) => {
typeVars: matchedInType.typeVars,
type: substitutedOutType,
};
}
};