Removed inputVars from Parser

This commit is contained in:
Ozzie Gooen 2020-07-31 11:43:56 +01:00
parent 4f08533055
commit 7aac191059
2 changed files with 27 additions and 38 deletions

View File

@ -1,5 +1,3 @@
type inputVars = Belt.Map.String.t(ExpressionTypes.ExpressionTree.node);
module MathJsonToMathJsAdt = { module MathJsonToMathJsAdt = {
type arg = type arg =
| Symbol(string) | Symbol(string)
@ -79,7 +77,7 @@ module MathJsonToMathJsAdt = {
module MathAdtToDistDst = { module MathAdtToDistDst = {
open MathJsonToMathJsAdt; open MathJsonToMathJsAdt;
let handleSymbol = (inputVars: inputVars, sym) => { let handleSymbol = sym => {
Ok(`Symbol(sym)); Ok(`Symbol(sym));
}; };
@ -123,7 +121,9 @@ module MathAdtToDistDst = {
switch (args) { switch (args) {
| [|Object(o)|] => | [|Object(o)|] =>
let g = s => let g = s =>
Js.Dict.get(o, s) |> E.O.toResult("Variable was empty") |> E.R.bind(_, nodeParser); Js.Dict.get(o, s)
|> E.O.toResult("Variable was empty")
|> E.R.bind(_, nodeParser);
switch (g("mean"), g("stdev"), g("mu"), g("sigma")) { switch (g("mean"), g("stdev"), g("mu"), g("sigma")) {
| (Ok(mean), Ok(stdev), _, _) => | (Ok(mean), Ok(stdev), _, _) =>
Ok( Ok(
@ -304,55 +304,47 @@ module MathAdtToDistDst = {
}; };
let rec nodeParser: let rec nodeParser:
(inputVars, MathJsonToMathJsAdt.arg) => MathJsonToMathJsAdt.arg =>
result(ExpressionTypes.ExpressionTree.node, string) = result(ExpressionTypes.ExpressionTree.node, string) =
inputVars => fun
fun | Value(f) => Ok(`SymbolicDist(`Float(f)))
| Value(f) => Ok(`SymbolicDist(`Float(f))) | Symbol(sym) => Ok(`Symbol(sym))
| Symbol(sym) => Ok(`Symbol(sym)) | Fn({name, args}) => functionParser(nodeParser, name, args)
| Fn({name, args}) => | _ => {
functionParser(nodeParser(inputVars), name, args) Error("This type not currently supported")
| _ => { };
Error("This type not currently supported");
};
// | FunctionAssignment({name, args, expression}) => { // | FunctionAssignment({name, args, expression}) => {
// let evaluatedExpression = run(expression); // let evaluatedExpression = run(expression);
// `Function(_ => Ok(evaluatedExpression)); // `Function(_ => Ok(evaluatedExpression));
// } // }
let rec topLevel = let rec topLevel = (r): result(ExpressionTypes.Program.program, string) =>
(inputVars: inputVars, r)
: result(ExpressionTypes.Program.program, string) =>
switch (r) { switch (r) {
| FunctionAssignment({name, args, expression}) => | FunctionAssignment({name, args, expression}) =>
switch (nodeParser(inputVars, expression)) { switch (nodeParser(expression)) {
| Ok(r) => Ok([|`Assignment((name, `Function(args, r)))|]) | Ok(r) => Ok([|`Assignment((name, `Function((args, r))))|])
| Error(r) => Error(r) | Error(r) => Error(r)
} }
| Value(_) as r => | Value(_) as r => nodeParser(r) |> E.R.fmap(r => [|`Expression(r)|])
nodeParser(inputVars, r) |> E.R.fmap(r => [|`Expression(r)|]) | Fn(_) as r => nodeParser(r) |> E.R.fmap(r => [|`Expression(r)|])
| Fn(_) as r =>
nodeParser(inputVars, r) |> E.R.fmap(r => [|`Expression(r)|])
| Array(_) => Error("Array not valid as top level") | Array(_) => Error("Array not valid as top level")
| Symbol(s) => | Symbol(s) => handleSymbol(s) |> E.R.fmap(r => [|`Expression(r)|])
handleSymbol(inputVars, s) |> E.R.fmap(r => [|`Expression(r)|])
| Object(_) => Error("Object not valid as top level") | Object(_) => Error("Object not valid as top level")
| Assignment(name, value) => | Assignment(name, value) =>
switch (name) { switch (name) {
| Symbol(symbol) => | Symbol(symbol) =>
nodeParser(inputVars, value) nodeParser(value) |> E.R.fmap(r => [|`Assignment((symbol, r))|])
|> E.R.fmap(r => [|`Assignment((symbol, r))|])
| _ => Error("Symbol not a string") | _ => Error("Symbol not a string")
} }
| Blocks(blocks) => | Blocks(blocks) =>
blocks blocks
|> E.A.fmap(b => topLevel(inputVars, b)) |> E.A.fmap(b => topLevel(b))
|> E.A.R.firstErrorOrOpen |> E.A.R.firstErrorOrOpen
|> E.R.fmap(E.A.concatMany) |> E.R.fmap(E.A.concatMany)
}; };
let run = (inputVars, r): result(ExpressionTypes.Program.program, string) => let run = (r): result(ExpressionTypes.Program.program, string) =>
r |> MathAdtCleaner.run |> topLevel(inputVars); r |> MathAdtCleaner.run |> topLevel;
}; };
/* The MathJs parser doesn't support '.+' syntax, but we want it because it /* The MathJs parser doesn't support '.+' syntax, but we want it because it
@ -362,7 +354,7 @@ module MathAdtToDistDst = {
*/ */
let pointwiseToRightLogShift = Js.String.replaceByRe([%re "/\.\+/g"], ">>>"); let pointwiseToRightLogShift = Js.String.replaceByRe([%re "/\.\+/g"], ">>>");
let fromString2 = (inputVars: inputVars, str) => { let fromString2 = str => {
/* We feed the user-typed string into Mathjs.parseMath, /* We feed the user-typed string into Mathjs.parseMath,
which returns a JSON with (hopefully) a single-element array. which returns a JSON with (hopefully) a single-element array.
This array element is the top-level node of a nested-object tree This array element is the top-level node of a nested-object tree
@ -372,7 +364,6 @@ let fromString2 = (inputVars: inputVars, str) => {
Inside of this function, MathAdtToDistDst is called whenever a distribution function is encountered. Inside of this function, MathAdtToDistDst is called whenever a distribution function is encountered.
*/ */
let mathJsToJson = str |> pointwiseToRightLogShift |> Mathjs.parseMath; let mathJsToJson = str |> pointwiseToRightLogShift |> Mathjs.parseMath;
Js.log2("toJson", mathJsToJson);
let mathJsParse = let mathJsParse =
E.R.bind(mathJsToJson, r => { E.R.bind(mathJsToJson, r => {
switch (MathJsonToMathJsAdt.run(r)) { switch (MathJsonToMathJsAdt.run(r)) {
@ -381,11 +372,10 @@ let fromString2 = (inputVars: inputVars, str) => {
} }
}); });
let value = E.R.bind(mathJsParse, MathAdtToDistDst.run(inputVars)); let value = E.R.bind(mathJsParse, MathAdtToDistDst.run);
Js.log3("Parsed", mathJsParse, value);
value; value;
}; };
let fromString = (str, vars: inputVars) => { let fromString = str => {
fromString2(vars, str); fromString2(str);
}; };

View File

@ -94,7 +94,6 @@ module Internals = {
let makeOutputs = (graph, shape): outputs => {graph, shape}; let makeOutputs = (graph, shape): outputs => {graph, shape};
let runNode = (inputs, node) => { let runNode = (inputs, node) => {
Js.log2("Inputs", inputs);
ExpressionTree.toShape( ExpressionTree.toShape(
{ {
sampleCount: inputs.samplingInputs.sampleCount |> E.O.default(10000), sampleCount: inputs.samplingInputs.sampleCount |> E.O.default(10000),
@ -124,7 +123,7 @@ module Internals = {
}; };
let inputsToShape = (inputs: inputs) => { let inputsToShape = (inputs: inputs) => {
MathJsParser.fromString(inputs.guesstimatorString, inputs.environment) MathJsParser.fromString(inputs.guesstimatorString)
|> E.R.bind(_, g => runProgram(inputs, g)) |> E.R.bind(_, g => runProgram(inputs, g))
|> E.R.bind(_, r => E.A.last(r) |> E.O.toResult("No rendered lines")); |> E.R.bind(_, r => E.A.last(r) |> E.O.toResult("No rendered lines"));
}; };