tweak a bit

This commit is contained in:
Joeri Exelmans 2025-05-18 10:19:39 +02:00
parent 248d8ddef1
commit d3515d39a5
2 changed files with 13 additions and 11 deletions

View file

@ -189,22 +189,24 @@ export const substitute = (type, substitutions, stack=[]) => {
};
export const assignFn = (funType, paramType) => {
return assignFnSubstitutions(funType, paramType)[2];
const [inType, inSubst, outType, outSubst] = assignFnSubstitutions(funType, paramType);
// return recomputeTypeVars([outType])[0];
return outType;
};
// same as above, but also returns the substitutions that took place
export const assignFnSubstitutions = (funType, paramType) => {
export const assignFnSubstitutions = (funType, paramType, skip=0) => {
if (getSymbol(funType) !== symbolFunction) {
throw new NotAFunctionError(`${prettyT(funType)} is not a function type!`);
}
const [[refunType, funS], [reparamType, paramS]] = recomputeTypeVarsSubstitutions([funType, paramType]);
const recomputationSubstitutions = mergeTwoWay(funS, paramS);
const [[refunType, funS], [reparamType, paramS]] = recomputeTypeVarsSubstitutions([funType, paramType], skip);
const [inType, outType] = refunType.params.map(p => p(refunType));
const {type: newInType, substitutions} = __unify(inType, reparamType);
const inTypeSubst = mergeTwoWay(substitutions, recomputationSubstitutions);
const substitutedFnType = substitute(outType, substitutions);
const [computedOutType, outSubst] = recomputeTypeVarsSubstitutions([substitutedFnType])[0];
return [newInType, inTypeSubst, computedOutType, outSubst];
const totalParamSubstitutions = mergeTwoWay(substitutions, paramS);
const newOutType = substitute(outType, substitutions);
const [[finalOutType, outsubst]] = recomputeTypeVarsSubstitutions([newOutType], skip);
const totalOutSubstitutions = mergeTwoWay(funS, outsubst);
return [newInType, totalParamSubstitutions, finalOutType, totalOutSubstitutions];
};
// Ensures that no type variables overlap
@ -213,8 +215,8 @@ export const recomputeTypeVars = types => {
.map(([newType, _subst]) => newType);
};
export const recomputeTypeVarsSubstitutions = types => {
let nextIdx = 0;
export const recomputeTypeVarsSubstitutions = (types, skip=0) => {
let nextIdx = skip;
return types.map(type => {
const substitutions = new Map();
const typeVars = occurring(type);