add function

This commit is contained in:
Joeri Exelmans 2025-05-16 10:16:21 +02:00
parent d75bf9f0f2
commit 106bd0bfdb

View file

@ -187,6 +187,12 @@ export const substitute = (type, substitutions, stack=[]) => {
}; };
export const assignFn = (funType, paramType) => { export const assignFn = (funType, paramType) => {
const [result] = assignFnSubstitutions(funType, paramType);
return result;
};
// same as above, but also returns the substitutions that took place
export const assignFnSubstitutions = (funType, paramType) => {
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!`);
} }
@ -196,8 +202,8 @@ export const assignFn = (funType, paramType) => {
const {substitutions} = __unify(inType, paramType); const {substitutions} = __unify(inType, paramType);
// console.log(substitutions, prettyT(outType)); // console.log(substitutions, prettyT(outType));
const substitutedFnType = substitute(outType, substitutions); const substitutedFnType = substitute(outType, substitutions);
return recomputeTypeVars([substitutedFnType])[0]; return [recomputeTypeVars([substitutedFnType])[0], substitutions];
}; }
// Ensures that no type variables overlap // Ensures that no type variables overlap
export const recomputeTypeVars = types => { export const recomputeTypeVars = types => {