more improvements
This commit is contained in:
parent
d877b42a12
commit
ec2944cdb7
5 changed files with 63 additions and 214 deletions
|
|
@ -53,7 +53,7 @@ const computeSuggestions = (text, env, suggestionPriority: (s: ResolvedType) =>
|
|||
kind: type.kind || "value",
|
||||
}]),
|
||||
]
|
||||
return []; // <-- uncomment to disable suggestions (useful for debugging)
|
||||
// return []; // <-- uncomment to disable suggestions (useful for debugging)
|
||||
return ls
|
||||
.map(suggestion => [suggestionPriority(suggestion[2]), ...suggestion] as PrioritizedSuggestionType)
|
||||
.sort(([priorityA], [priorityB]) => priorityB - priorityA)
|
||||
|
|
@ -88,7 +88,12 @@ export function InputBlock({ state, setState, suggestionPriority, onCancel }: In
|
|||
}
|
||||
|
||||
const onTextChange = newText => {
|
||||
setState(state => ({...state, text: newText}));
|
||||
setState(state => ({...state,
|
||||
text: newText,
|
||||
value: (trie.get(env.names)(newText) ? {
|
||||
kind: "name",
|
||||
} : state.value),
|
||||
}));
|
||||
}
|
||||
|
||||
// fired before onInput
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ export function Value({dynamic}) {
|
|||
case symbolUUID:
|
||||
return <ValueUUID val={inst}/>
|
||||
default:
|
||||
console.log("don't know how to show value:", dynamic);
|
||||
return <>don't know how to show value</>;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,184 +75,7 @@ export const tripleFunctionCallEditorState: ExprBlockState = {
|
|||
},
|
||||
};
|
||||
|
||||
export const biggerExample: ExprBlockState = {
|
||||
"kind": "let",
|
||||
"inner": {
|
||||
"kind": "let",
|
||||
"inner": {
|
||||
"kind": "let",
|
||||
"inner": {
|
||||
"kind": "let",
|
||||
"inner": {
|
||||
"kind": "input",
|
||||
"text": "",
|
||||
"value": {
|
||||
"kind": "text"
|
||||
},
|
||||
"focus": false
|
||||
},
|
||||
"name": "myListInc",
|
||||
"value": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "input",
|
||||
"text": "list.map",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
},
|
||||
"input": {
|
||||
"kind": "input",
|
||||
"text": "myList",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
}
|
||||
},
|
||||
"input": {
|
||||
"kind": "input",
|
||||
"text": "inc",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"name": "myList",
|
||||
"value": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "input",
|
||||
"text": "list.push",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
},
|
||||
"input": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "input",
|
||||
"text": "list.push",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
},
|
||||
"input": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "input",
|
||||
"text": "list.push",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
},
|
||||
"input": {
|
||||
"kind": "input",
|
||||
"text": "list.emptyList",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
}
|
||||
},
|
||||
"input": {
|
||||
"kind": "input",
|
||||
"text": "1",
|
||||
"value": {
|
||||
"kind": "literal",
|
||||
"type": "Int"
|
||||
},
|
||||
"focus": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"input": {
|
||||
"kind": "input",
|
||||
"text": "2",
|
||||
"value": {
|
||||
"kind": "literal",
|
||||
"type": "Int"
|
||||
},
|
||||
"focus": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"input": {
|
||||
"kind": "input",
|
||||
"text": "3",
|
||||
"value": {
|
||||
"kind": "literal",
|
||||
"type": "Int"
|
||||
},
|
||||
"focus": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"name": "id",
|
||||
"value": {
|
||||
"kind": "lambda",
|
||||
"paramName": "x",
|
||||
"expr": {
|
||||
"kind": "input",
|
||||
"text": "x",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"name": "inc",
|
||||
"value": {
|
||||
"kind": "lambda",
|
||||
"paramName": "x",
|
||||
"expr": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "call",
|
||||
"fn": {
|
||||
"kind": "input",
|
||||
"text": "addInt",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
},
|
||||
"input": {
|
||||
"kind": "input",
|
||||
"text": "x",
|
||||
"value": {
|
||||
"kind": "name"
|
||||
},
|
||||
"focus": false
|
||||
}
|
||||
},
|
||||
"input": {
|
||||
"kind": "input",
|
||||
"text": "1",
|
||||
"value": {
|
||||
"kind": "literal",
|
||||
"type": "Int"
|
||||
},
|
||||
"focus": true
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
export const biggerExample: ExprBlockState = {"kind":"let","inner":{"kind":"let","inner":{"kind":"let","inner":{"kind":"let","inner":{"kind":"input","text":"","value":{"kind":"text"},"focus":false},"name":"myListInc","value":{"kind":"call","fn":{"kind":"call","fn":{"kind":"input","text":"list.map","value":{"kind":"name"},"focus":false},"input":{"kind":"call","fn":{"kind":"call","fn":{"kind":"input","text":"list.map","value":{"kind":"name"},"focus":false},"input":{"kind":"input","text":"myList","value":{"kind":"name"},"focus":false}},"input":{"kind":"input","text":"inc","value":{"kind":"name"},"focus":false}}},"input":{"kind":"input","text":"id","value":{"kind":"name"},"focus":true}}},"name":"myList","value":{"kind":"call","fn":{"kind":"call","fn":{"kind":"input","text":"list.push","value":{"kind":"name"},"focus":false},"input":{"kind":"call","fn":{"kind":"call","fn":{"kind":"input","text":"list.push","value":{"kind":"name"},"focus":false},"input":{"kind":"call","fn":{"kind":"call","fn":{"kind":"input","text":"list.push","value":{"kind":"name"},"focus":false},"input":{"kind":"input","text":"list.emptyList","value":{"kind":"name"},"focus":false}},"input":{"kind":"input","text":"1","value":{"kind":"literal","type":"Int"},"focus":false}}},"input":{"kind":"input","text":"2","value":{"kind":"literal","type":"Int"},"focus":false}}},"input":{"kind":"input","text":"3","value":{"kind":"literal","type":"Int"},"focus":false}}},"name":"id","value":{"kind":"lambda","paramName":"x","expr":{"kind":"input","text":"x","value":{"kind":"name"},"focus":false}}},"name":"inc","value":{"kind":"lambda","paramName":"x","expr":{"kind":"call","fn":{"kind":"call","fn":{"kind":"input","text":"addInt","value":{"kind":"name"},"focus":false},"input":{"kind":"input","text":"x","value":{"kind":"name"},"focus":false}},"input":{"kind":"input","text":"1","value":{"kind":"literal","type":"Int"},"focus":true}}}};
|
||||
|
||||
export const lambda2Params: ExprBlockState = {
|
||||
"kind": "let",
|
||||
|
|
|
|||
80
src/eval.ts
80
src/eval.ts
|
|
@ -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") {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue