diff --git a/index.d.ts b/index.d.ts index 5073226..0b72f17 100644 --- a/index.d.ts +++ b/index.d.ts @@ -167,6 +167,8 @@ export function RBTreeWrapper(...args: any[]): any; export function UnifyError(...args: any[]): any; +export function NotAFunctionError(...args: any[]): any; + export function addDouble(x: any): any; export function addInt(x: any): any; diff --git a/lib/generics/generics.js b/lib/generics/generics.js index df92250..63ce9e4 100644 --- a/lib/generics/generics.js +++ b/lib/generics/generics.js @@ -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));