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

@ -1,15 +1,14 @@
import { useContext } from "react";
import { ExprBlock, type ExprBlockState, type SetStateFn, type State2Props } from "./ExprBlock";
import { EnvContext } from "./EnvContext";
import { addFocusRightMost, evalCallBlock2, evalEditorBlock, removeFocus, scoreResolved, type ResolvedType } from "./eval";
import { addFocusRightMost, evalCallBlock2, evalExprBlock, recomputeTypeVarsForEnv, scoreResolved, type Environment, type ResolvedType } from "./eval";
import { ExprBlock, type ExprBlockState, type SetStateFn, type State2Props } from "./ExprBlock";
import { GlobalContext } from "./GlobalContext";
import { Value } from "./Value";
import "./CallBlock.css";
import { initialEditorState } from "./configurations";
import { CallContext } from "./CallContext";
import { getActions } from "./actions";
import "./CallBlock.css";
import { CallContext } from "./CallContext";
export interface CallBlockState {
kind: "call";
@ -17,48 +16,61 @@ export interface CallBlockState {
input: ExprBlockState;
}
interface CallBlockProps<
export interface CallBlockProps<
FnState=ExprBlockState,
InputState=ExprBlockState,
> extends State2Props<CallBlockState,ExprBlockState> {}
function nestedFnProperties({state, setState, suggestionPriority}: CallBlockProps, env) {
function nestedFnProperties({state, setState, score}: CallBlockProps, env) {
const setFn = (callback: SetStateFn) => {
setState(state => ({...state, fn: callback(state.fn)}));
}
const onFnCancel = () => {
setState(state => state.input); // we become our input
}
const fnSuggestionPriority = (fnSuggestion: ResolvedType) => computePriority(
const scoreFn = (fnSuggestion: ResolvedType) => {
return computePriority(
fnSuggestion,
evalEditorBlock(state.input, env)[0],
suggestionPriority,
evalExprBlock(state.input, env)[0],
score,
env,
);
return {state: state.fn, setState: setFn, onCancel: onFnCancel, suggestionPriority: fnSuggestionPriority};
};
return {state: state.fn, setState: setFn, onCancel: onFnCancel, score: scoreFn};
}
function nestedInputProperties({state, setState, suggestionPriority}: CallBlockProps, env) {
function nestedInputProperties({state, setState, score}: CallBlockProps, env: Environment) {
const setInput = (callback: SetStateFn) => {
setState(state => ({...state, input: callback(state.input)}));
}
const onInputCancel = () => {
setState(state => addFocusRightMost(state.fn)); // we become our function
}
const inputSuggestionPriorirty = (inputSuggestion: ResolvedType) => computePriority(
evalEditorBlock(state.fn, env)[0], // fn *may* be set
inputSuggestion, // suggestions will be for input
suggestionPriority, // priority function we get from parent block
env,
)
return {state: state.input, setState: setInput, onCancel: onInputCancel, suggestionPriority: inputSuggestionPriorirty};
const scoreInput = (inputSuggestion: ResolvedType) => {
return computePriority(
evalExprBlock(state.fn, env)[0], // fn *may* be set
inputSuggestion, // suggestions will be for input
score, // priority function we get from parent block
env,
);
}
return {state: state.input, setState: setInput, onCancel: onInputCancel, score: scoreInput};
}
function computePriority(fn: ResolvedType, input: ResolvedType, outPriority: (s: ResolvedType) => number, env) {
// dirty, but works:
const [fnR, env2] = recomputeTypeVarsForEnv('<fn>', fn, env);
const [inR, env3] = recomputeTypeVarsForEnv('<in>', input, env2);
const [resolved] = evalCallBlock2(fnR, inR, env3);
const score = scoreResolved(resolved, outPriority);
return score;
}
export function CallBlock(props: CallBlockProps) {
const env = useContext(EnvContext);
const globalContext = useContext(GlobalContext);
const addParam = getActions(globalContext, props.setState).c;
const [resolved] = evalEditorBlock(props.state, env);
const [resolved] = evalExprBlock(props.state, env);
return <span className={"functionBlock" + ((resolved.kind === "error") ? " unifyError" : "")}>
<CallContext value={{addParam}}>
<FunctionHeader {...props} addParam={addParam} />
@ -80,12 +92,6 @@ export function CallBlock(props: CallBlockProps) {
</span>;
}
function computePriority(fn: ResolvedType, input: ResolvedType, outPriority: (s: ResolvedType) => number, env) {
const [resolved] = evalCallBlock2(fn, input, env);
const score = scoreResolved(resolved, outPriority);
return score;
}
function FunctionHeader(props) {
const env = useContext(EnvContext);
const globalContext = useContext(GlobalContext);