tweak a bit

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

2
index.d.ts vendored
View file

@ -177,7 +177,7 @@ export function apply(input: any): any;
export function assignFn(funType: any, paramType: any): any; export function assignFn(funType: any, paramType: any): any;
export function assignFnSubstitutions(funType: any, paramType: any): [any, any]; export function assignFnSubstitutions(funType: any, paramType: any, skip: number): [any, any, any, any];
export function capitalizeFirstLetter(val: any): any; export function capitalizeFirstLetter(val: any): any;

View file

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