greatly simplify and improve type inferencing - suggestions broken
This commit is contained in:
parent
c3f7cea310
commit
c5c5def598
12 changed files with 1022 additions and 694 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import { useContext } from "react";
|
||||
|
||||
import { EnvContext } from "./EnvContext";
|
||||
import { addFocusRightMost, evalCallBlock2, evalExprBlock, recomputeTypeVarsForEnv, scoreResolved, type Environment, 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";
|
||||
|
|
@ -9,6 +9,8 @@ import { Value } from "./Value";
|
|||
import { getActions } from "./actions";
|
||||
import "./CallBlock.css";
|
||||
import { CallContext } from "./CallContext";
|
||||
import { inferType, inferTypeCall, type Environment } from "./infer_type";
|
||||
import { Type } from "./Type";
|
||||
|
||||
export interface CallBlockState {
|
||||
kind: "call";
|
||||
|
|
@ -21,20 +23,19 @@ export interface CallBlockProps<
|
|||
InputState=ExprBlockState,
|
||||
> extends State2Props<CallBlockState,ExprBlockState> {}
|
||||
|
||||
function nestedFnProperties({state, setState, score}: CallBlockProps, env) {
|
||||
function nestedFnProperties({state, setState, score}: CallBlockProps, env: Environment) {
|
||||
const setFn = (callback: SetStateFn) => {
|
||||
setState(state => ({...state, fn: callback(state.fn)}));
|
||||
}
|
||||
};
|
||||
const onFnCancel = () => {
|
||||
setState(state => state.input); // we become our input
|
||||
}
|
||||
const scoreFn = (fnSuggestion: ResolvedType) => {
|
||||
return computePriority(
|
||||
fnSuggestion,
|
||||
evalExprBlock(state.input, env)[0],
|
||||
score,
|
||||
env,
|
||||
);
|
||||
};
|
||||
const scoreFn = (fnSuggestion: ExprBlockState) => {
|
||||
return score({
|
||||
kind: "call",
|
||||
fn: fnSuggestion,
|
||||
input: state.input,
|
||||
});
|
||||
};
|
||||
return {state: state.fn, setState: setFn, onCancel: onFnCancel, score: scoreFn};
|
||||
}
|
||||
|
|
@ -42,36 +43,37 @@ function nestedFnProperties({state, setState, score}: 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 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,
|
||||
);
|
||||
}
|
||||
setState(state => /*addFocusRightMost*/(state.fn)); // we become our function
|
||||
};
|
||||
const scoreInput = (inputSuggestion: ExprBlockState) => {
|
||||
return score({
|
||||
kind: "call",
|
||||
fn: state.fn,
|
||||
input: inputSuggestion,
|
||||
});
|
||||
};
|
||||
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;
|
||||
}
|
||||
// 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] = evalExprBlock(props.state, env);
|
||||
return <span className={"functionBlock" + ((resolved.kind === "error") ? " unifyError" : "")}>
|
||||
// const [resolved] = evalExprBlock(props.state, env);
|
||||
// return <span className={"functionBlock" + ((resolved.kind === "error") ? " unifyError" : "")}>
|
||||
const typeInfo = inferTypeCall(props.state, env);
|
||||
return <span className={"functionBlock"}>
|
||||
<CallContext value={{addParam}}>
|
||||
<FunctionHeader {...props} addParam={addParam} />
|
||||
<div className="functionParams">
|
||||
|
|
@ -80,12 +82,14 @@ export function CallBlock(props: CallBlockProps) {
|
|||
<InputParams
|
||||
{...props}
|
||||
depth={0}
|
||||
errorDepth={(resolved.kind === "error") ? (resolved.depth) : -1}
|
||||
// errorDepth={(resolved.kind === "error") ? (resolved.depth) : -1}
|
||||
errorDepth={-1}
|
||||
addParam={addParam}
|
||||
/>
|
||||
{ (resolved.kind === "error") && resolved.e.toString()
|
||||
{/* { (resolved.kind === "error") && resolved.e.toString()
|
||||
|| (resolved.kind === "value") && <Value dynamic={resolved} />
|
||||
|| "unknown" }
|
||||
|| "unknown" } */}
|
||||
:: <Type type={typeInfo.type} />
|
||||
</div>
|
||||
</div>
|
||||
</CallContext>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue