add extra check and error type

This commit is contained in:
Joeri Exelmans 2025-05-13 17:04:19 +02:00
parent e631f11aa5
commit 1e10393e76
2 changed files with 7 additions and 1 deletions

View file

@ -4,6 +4,7 @@ import { zip } from "../util/util.js";
import { pretty, prettyT } from '../util/pretty.js';
import { isTypeVar, TYPE_VARS } from "../primitives/typevars.js";
import { inspectType } from "../meta/type_constructor.js";
import { symbolFunction } from "../structures/type_constructors.js";
// helper for creating generic types
// for instance, the type:
@ -78,6 +79,7 @@ export const mergeTwoWay = (m1, m2) => {
};
export class UnifyError extends Error {}
export class NotAFunctionError extends Error {}
// Thanks to Hans for pointing out that this algorithm exactly like "Unification" in Prolog (hence the function name):
// https://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/node12.html
@ -184,8 +186,10 @@ export const substitute = (type, substitutions, stack=[]) => {
};
};
export const assignFn = (funType, paramType) => {
if (getSymbol(funType) !== symbolFunction) {
throw NotAFunctionError(`${prettyT(funType)} is not a function type!`);
}
[funType, paramType] = recomputeTypeVars([funType, paramType]);
// console.log(prettyT(funType), prettyT(paramType));
const [inType, outType] = funType.params.map(p => p(funType));