simplify suggestions ordering

This commit is contained in:
Joeri Exelmans 2025-05-20 16:53:04 +02:00
parent fdbf43a4e9
commit bb6e742f5f
8 changed files with 109 additions and 110 deletions

View file

@ -4,7 +4,7 @@ import type { ExprBlockState } from "./ExprBlock";
import type { InputValueType } from "./InputBlock";
const IS_DEV = (import.meta.env.MODE === "development");
const VERBOSE = false;
const VERBOSE = true;
export interface Environment {
names: any;
@ -46,7 +46,7 @@ export type ResolvedType = Dynamic | DeepError | Unknown;
class NotFoundError extends Error {}
export const evalEditorBlock = (s: ExprBlockState, env: Environment): [ResolvedType,Environment] => {
export const evalExprBlock = (s: ExprBlockState, env: Environment): [ResolvedType,Environment] => {
if (s.kind === "input") {
return evalInputBlock(s.text, s.value, env);
}
@ -88,8 +88,8 @@ export function evalInputBlock(text: string, value: InputValueType, env: Environ
}
export function evalCallBlock(fn: ExprBlockState, input: ExprBlockState, env: Environment): [ResolvedType,Environment] {
const [fnResolved, env2] = evalEditorBlock(fn, env);
const [inputResolved, env3] = evalEditorBlock(input, env2);
const [fnResolved, env2] = evalExprBlock(fn, env);
const [inputResolved, env3] = evalExprBlock(input, env2);
if (VERBOSE) {
console.log('==== evalCallBlock ====');
console.log('env :', env);
@ -103,6 +103,9 @@ export function evalCallBlock(fn: ExprBlockState, input: ExprBlockState, env: En
}
export function evalCallBlock2(fnResolved: ResolvedType, inputResolved: ResolvedType, env: Environment): [ResolvedType,Environment] {
if (occurring(fnResolved.t).intersection(occurring(inputResolved.t)).size > 0) {
throw new Error(`Precondition failed: function (${prettyT(fnResolved.t)}) and its input (${prettyT(inputResolved.t)}) have overlapping typevars!`);
}
if (getSymbol(fnResolved.t) !== symbolFunction) {
// not a function...
if (isTypeVar(fnResolved.t)) {
@ -143,7 +146,7 @@ const inverseUnification = (uni, inverse) => {
);
}
function recomputeTypeVarsForEnv(name: string, resolved: ResolvedType, env: Environment): [ResolvedType,Environment] {
export function recomputeTypeVarsForEnv(name: string, resolved: ResolvedType, env: Environment): [ResolvedType,Environment] {
const [[newType], inverse] = recomputeTypeVarsWithInverse([resolved.t], env.nextFreeTypeVar);
const newResolved: ResolvedType = {
...resolved,
@ -281,9 +284,9 @@ function evalCallBlock3(fnResolved: ResolvedType, inputResolved: ResolvedType, e
}
export function evalLetInBlock(value: ExprBlockState, name: string, inner: ExprBlockState, env: Environment): [ResolvedType,Environment] {
const [valueResolved] = evalEditorBlock(value, env);
const [valueResolved] = evalExprBlock(value, env);
const innerEnv = makeInnerEnv(env, name, valueResolved);
return evalEditorBlock(inner, innerEnv);
return evalExprBlock(inner, innerEnv);
}
const prettyRU = (rUni: Map<string, Type>) => {
@ -301,7 +304,7 @@ export function evalLambdaBlock(paramName: string, expr: ExprBlockState, env: En
console.log('===================================')
}
const [exprResolved] = evalEditorBlock(expr, staticInnerEnv);
const [exprResolved] = evalExprBlock(expr, staticInnerEnv);
const lambdaT = fnType(_ => paramType)(_ => exprResolved.t);
// This is the only place in the code where we actually do something with the 'substitutions'. We compute the type of our lambda function:
const reduced = reduceUnification(exprResolved.unification);
@ -331,7 +334,7 @@ export function evalLambdaBlock(paramName: string, expr: ExprBlockState, env: En
t: paramTypeSubstituted,
unification: new Map(),
});
const [result] = evalEditorBlock(expr, innerEnv);
const [result] = evalExprBlock(expr, innerEnv);
if (result.kind === "value") {
return result.i;
}
@ -410,17 +413,23 @@ export function attemptParseLiteral(text: string, env: Environment): Dynamic[] {
export function scoreResolved(resolved: ResolvedType, outPriority: (s:ResolvedType) => number) {
const bias = outPriority(resolved);
console.log('scoreResolved...', resolved);
if (resolved.kind === "value") {
console.log('best:', 2, bias);
return 2 + bias;
}
else if (resolved.kind === "unknown") {
console.log('ok:', 1, bias);
return 1 + bias;
}
if (resolved.e instanceof UnifyError) {
console.log('bad:', -1, bias);
return -1 + bias; // parameter doesn't match
}
else {
console.log('worst:', -2, bias);
return -2 + bias; // even worse: fn is not a function!
}
}