turn one example into a test + fix bug in type variable substition function

This commit is contained in:
Joeri Exelmans 2025-05-08 17:30:42 +02:00
parent a664ddac8a
commit 35d682429b
4 changed files with 148 additions and 102 deletions

View file

@ -163,17 +163,24 @@ export const unify = (fType, aType) => {
export const substitute = (type, substitutions, stack=[]) => {
// console.log('substitute...', {type, substitutions, stack});
return substitutions.get(getSymbol(type))
|| {
const found = substitutions.get(getSymbol(type));
if (found) {
return found;
}
return {
symbol: getSymbol(type),
params: type.params.map(getParam => parent => {
const param = getParam(stack.length);
return stack[param]
|| substitute(param, substitutions, [...stack, parent]);
const param = getParam(parent);
if (stack.includes(param)) {
// param points back up - that's ok - means we don't have to recurse
return param;
}
return substitute(param, substitutions, [...stack, parent]);
}),
};
};
export const assignFn = (funType, paramType) => {
[funType, paramType] = recomputeTypeVars([funType, paramType]);
// console.log(prettyT(funType), prettyT(paramType));