more improvements

This commit is contained in:
Joeri Exelmans 2025-05-19 15:43:11 +02:00
parent d877b42a12
commit ec2944cdb7
5 changed files with 63 additions and 214 deletions

View file

@ -1,8 +1,10 @@
import { dict, Double, fnType, getHumanReadableName, getSymbol, Int, mergeUnifications, NotAFunctionError, occurring, prettyT, prettyU, recomputeTypeVars, reduceUnification, substitute, symbolFunction, trie, TYPE_VARS, UNBOUND_SYMBOLS, UnifyError, unifyLL } from "dope2";
import { Double, fnType, getHumanReadableName, getSymbol, Int, mergeUnifications, NotAFunctionError, occurring, prettyT, prettyU, recomputeTypeVars, reduceUnification, substitute, symbolFunction, trie, TYPE_VARS, UNBOUND_SYMBOLS, UnifyError, unifyLL, transitivelyGrow } from "dope2";
import type { ExprBlockState } from "./ExprBlock";
import type { InputValueType } from "./InputBlock";
const IS_DEV = (import.meta.env.MODE === "development");
export interface Environment {
names: any;
nextFreeTypeVar: number;
@ -88,30 +90,38 @@ export function evalCallBlock2(fnResolved: ResolvedType, inputResolved: Resolved
try {
// fn is a function...
const [rewrittenFnType] = recomputeTypeVars([fnResolved.t], env.nextFreeTypeVar);
const unification = unifyLL(rewrittenFnType.params[0](rewrittenFnType), inputResolved.t);
const unification = (unifyLL(rewrittenFnType.params[0](rewrittenFnType), inputResolved.t));
const inputTypeVars = occurring(inputResolved.t);
const fnTypeVars = occurring(fnResolved.t);
const subsetOfUnification = new Map([...unification].filter(([typeVar]) => inputTypeVars.has(typeVar)));
const otherSubSetOfUnification = new Map([...unification].filter(([typeVar]) => fnTypeVars.has(typeVar)));
const outType = substitute(
rewrittenFnType.params[1](rewrittenFnType),
reduceUnification(subsetOfUnification),
reduceUnification(unification),
[]); // <- not important
console.log('========= evalCallBlock2 =========')
console.log('fnType :', prettyT(fnResolved.t));
console.log('rewrittenFnType :', prettyT(rewrittenFnType));
console.log('inputType :', prettyT(inputResolved.t));
console.log('outType :', prettyT(outType));
console.log('unification :', prettyU(unification));
console.log('inputTypeVars :', [...inputTypeVars].map(getHumanReadableName));
console.log('fn.unification :', prettyU(fnResolved.unification));
console.log('input.unification :', prettyU(inputResolved.unification));
console.log('subsetOfUnification:', prettyU(subsetOfUnification));
console.log('==================================')
const grandUnification = [fnResolved.unification, inputResolved.unification]
.reduce(mergeUnifications, subsetOfUnification);
.reduce(mergeUnifications, unification);
if (IS_DEV) {
console.log('========= evalCallBlock2 =========')
console.log('fnType :', prettyT(fnResolved.t));
console.log('rewrittenFnType :', prettyT(rewrittenFnType));
console.log('inputType :', prettyT(inputResolved.t));
console.log('unification :', prettyU(unification));
console.log('subsetOfUnification :', prettyU(subsetOfUnification));
console.log('otherSubSetOfUnification:', prettyU(otherSubSetOfUnification));
console.log('outType :', prettyT(outType));
// console.log('inputTypeVars :', `{${[...inputTypeVars].map(getHumanReadableName).join(', ')}}`);
// console.log('fnTypeVars :', `{${[...fnTypeVars].map(getHumanReadableName).join(', ')}}`);
console.log('fn.unification :', prettyU(fnResolved.unification));
console.log('input.unification :', prettyU(inputResolved.unification));
console.log('grandUnification :', prettyU(grandUnification));
console.log('==================================')
}
if (inputResolved.kind === "error") {
return {
@ -155,17 +165,25 @@ export function evalCallBlock2(fnResolved: ResolvedType, inputResolved: Resolved
if ((e instanceof UnifyError)) {
// even though fn was incompatible with the given parameter, we can still suppose that our output-type will be that of fn...?
const outType = fnResolved.t.params[1](fnResolved.t);
return {
kind: "error",
e,
depth: 0,
t: outType,
unification: mergeUnifications(fnResolved.unification, inputResolved.unification),
};
try {
return {
kind: "error",
e,
depth: 0,
t: outType,
unification: mergeUnifications(fnResolved.unification, inputResolved.unification), // may throw!
};
}
catch (e) {
if ((e instanceof UnifyError)) {
return makeError(env, e);
}
throw e;
}
}
throw e;
}
}
};
export function evalCallBlock(fn: ExprBlockState, input: ExprBlockState, env): ResolvedType {
const fnResolved = evalEditorBlock(fn, env);
@ -190,12 +208,14 @@ export function evalLambdaBlock(paramName: string, expr: ExprBlockState, env): R
reduceUnification(exprResolved.unification),
[]); // <- not important
console.log('========= evalLambdaBlock =========')
console.log('paramType :', prettyT(paramType));
console.log('exprType :', prettyT(exprResolved.t));
console.log('lambdaType :', prettyT(lambdaT));
console.log('lambdaTypeSubsituted:', prettyT(lambdaTSubstituted));
console.log('===================================')
if (IS_DEV) {
console.log('========= evalLambdaBlock =========')
console.log('paramType :', prettyT(paramType));
console.log('exprType :', prettyT(exprResolved.t));
console.log('lambdaType :', prettyT(lambdaT));
console.log('lambdaTypeSubsituted:', prettyT(lambdaTSubstituted));
console.log('===================================')
}
// console.log('inner kind', exprResolved.kind, paramName);
if (exprResolved.kind === "error") {