eval lambda block: bubble up substitutions

This commit is contained in:
Joeri Exelmans 2025-05-17 12:06:27 +02:00
parent 496463bbac
commit 51ff4d24b0
3 changed files with 23 additions and 15 deletions

View file

@ -98,6 +98,9 @@ export function evalCallBlock2(fnResolved: ResolvedType, inputResolved: Resolved
try {
// fn is a function...
const [outType, substitutions] = assignFnSubstitutions(fnResolved.t, inputResolved.t); // may throw
// console.log('assignFn...', prettyT(fnResolved.t), prettyT(inputResolved.t), '\nout =', prettyT(outType), substitutions);
const mergedSubstitutions = mergeMaps(substitutions, fnResolved.substitutions, inputResolved.substitutions);
@ -177,11 +180,21 @@ export function getUnusedTypeVar(env) {
export function evalLambdaBlock(paramName: string, expr: EditorState, env): ResolvedType {
const paramType = getUnusedTypeVar(env);
// static env: we only know the name and the type
const staticInnerEnv = makeInnerEnv(env, paramName, {
kind: "unknown", // parameter value is not statically known
t: paramType,
substitutions: new Map(),
});
const exprResolved = evalEditorBlock(expr, staticInnerEnv);
const lambdaT = fnType(_ => paramType)(_ => exprResolved.t);
const lambdaTSubstituted = substitute(lambdaT, exprResolved.substitutions, []);
const paramTypeSubstituted = lambdaTSubstituted.params[0](lambdaTSubstituted);
const fn = (x: any) => {
const innerEnv = makeInnerEnv(env, paramName, {
kind: "value",
i: x,
t: paramType,
t: paramTypeSubstituted,
substitutions: new Map(),
});
const result = evalEditorBlock(expr, innerEnv);
@ -189,20 +202,11 @@ export function evalLambdaBlock(paramName: string, expr: EditorState, env): Reso
return result.i;
}
}
// static env: we only know the name and the type
const staticInnerEnv = makeInnerEnv(env, paramName, {
kind: "unknown", // parameter value is not statically known
t: paramType,
substitutions: new Map(),
});
const abstractOutput = evalEditorBlock(expr, staticInnerEnv);
const t = fnType(_ => paramType)(_ => abstractOutput.t);
const T = substitute(t, abstractOutput.substitutions, [])
return {
kind: "value",
t: T,
t: lambdaTSubstituted,
i: fn,
substitutions: new Map(),
substitutions: exprResolved.substitutions,
};
}