First part of refactor

This commit is contained in:
Ozzie Gooen 2020-07-23 11:17:39 +01:00
parent e86d959c1b
commit bfd9ca5533

View File

@ -239,26 +239,31 @@ module MathAdtToDistDst = {
let toOkPointwise = r => Ok(`PointwiseCombination(r)); let toOkPointwise = r => Ok(`PointwiseCombination(r));
let toOkTruncate = r => Ok(`Truncate(r)); let toOkTruncate = r => Ok(`Truncate(r));
let toOkFloatFromDist = r => Ok(`FloatFromDist(r)); let toOkFloatFromDist = r => Ok(`FloatFromDist(r));
E.A.R.firstErrorOrOpen(args)
|> E.R.bind(_, args => {
switch (name, args) { switch (name, args) {
| ("add", [|Ok(l), Ok(r)|]) => toOkAlgebraic((`Add, l, r)) | ("add", [|l, r|]) => toOkAlgebraic((`Add, l, r))
| ("add", _) => Error("Addition needs two operands") | ("add", _) => Error("Addition needs two operands")
| ("subtract", [|Ok(l), Ok(r)|]) => toOkAlgebraic((`Subtract, l, r)) | ("subtract", [|l, r|]) => toOkAlgebraic((`Subtract, l, r))
| ("subtract", _) => Error("Subtraction needs two operands") | ("subtract", _) => Error("Subtraction needs two operands")
| ("multiply", [|Ok(l), Ok(r)|]) => toOkAlgebraic((`Multiply, l, r)) | ("multiply", [|l, r|]) => toOkAlgebraic((`Multiply, l, r))
| ("multiply", _) => Error("Multiplication needs two operands") | ("multiply", _) => Error("Multiplication needs two operands")
| ("dotMultiply", [|Ok(l), Ok(r)|]) => toOkPointwise((`Multiply, l, r)) | ("dotMultiply", [|l, r|]) => toOkPointwise((`Multiply, l, r))
| ("dotMultiply", _) => | ("dotMultiply", _) =>
Error("Dotwise multiplication needs two operands") Error("Dotwise multiplication needs two operands")
| ("rightLogShift", [|Ok(l), Ok(r)|]) => toOkPointwise((`Add, l, r)) | ("rightLogShift", [|l, r|]) => toOkPointwise((`Add, l, r))
| ("rightLogShift", _) => Error("Dotwise addition needs two operands") | ("rightLogShift", _) =>
| ("divide", [|Ok(l), Ok(r)|]) => toOkAlgebraic((`Divide, l, r)) Error("Dotwise addition needs two operands")
| ("divide", [|l, r|]) => toOkAlgebraic((`Divide, l, r))
| ("divide", _) => Error("Division needs two operands") | ("divide", _) => Error("Division needs two operands")
| ("pow", _) => Error("Exponentiation is not yet supported.") | ("pow", _) => Error("Exponentiation is not yet supported.")
| ("leftTruncate", [|Ok(d), Ok(`SymbolicDist(`Float(lc)))|]) => | ("leftTruncate", [|d, `SymbolicDist(`Float(lc))|]) =>
toOkTruncate((Some(lc), None, d)) toOkTruncate((Some(lc), None, d))
| ("leftTruncate", _) => | ("leftTruncate", _) =>
Error("leftTruncate needs two arguments: the expression and the cutoff") Error(
| ("rightTruncate", [|Ok(d), Ok(`SymbolicDist(`Float(rc)))|]) => "leftTruncate needs two arguments: the expression and the cutoff",
)
| ("rightTruncate", [|d, `SymbolicDist(`Float(rc))|]) =>
toOkTruncate((None, Some(rc), d)) toOkTruncate((None, Some(rc), d))
| ("rightTruncate", _) => | ("rightTruncate", _) =>
Error( Error(
@ -266,29 +271,29 @@ module MathAdtToDistDst = {
) )
| ( | (
"truncate", "truncate",
[| [|d, `SymbolicDist(`Float(lc)), `SymbolicDist(`Float(rc))|],
Ok(d),
Ok(`SymbolicDist(`Float(lc))),
Ok(`SymbolicDist(`Float(rc))),
|],
) => ) =>
toOkTruncate((Some(lc), Some(rc), d)) toOkTruncate((Some(lc), Some(rc), d))
| ("truncate", _) => | ("truncate", _) =>
Error("truncate needs three arguments: the expression and both cutoffs") Error(
| ("pdf", [|Ok(d), Ok(`SymbolicDist(`Float(v)))|]) => "truncate needs three arguments: the expression and both cutoffs",
)
| ("pdf", [|d, `SymbolicDist(`Float(v))|]) =>
toOkFloatFromDist((`Pdf(v), d)) toOkFloatFromDist((`Pdf(v), d))
| ("cdf", [|Ok(d), Ok(`SymbolicDist(`Float(v)))|]) => | ("cdf", [|d, `SymbolicDist(`Float(v))|]) =>
toOkFloatFromDist((`Cdf(v), d)) toOkFloatFromDist((`Cdf(v), d))
| ("inv", [|Ok(d), Ok(`SymbolicDist(`Float(v)))|]) => | ("inv", [|d, `SymbolicDist(`Float(v))|]) =>
toOkFloatFromDist((`Inv(v), d)) toOkFloatFromDist((`Inv(v), d))
| ("mean", [|Ok(d)|]) => toOkFloatFromDist((`Mean, d)) | ("mean", [|d|]) => toOkFloatFromDist((`Mean, d))
| ("sample", [|Ok(d)|]) => toOkFloatFromDist((`Sample, d)) | ("sample", [|d|]) => toOkFloatFromDist((`Sample, d))
| _ => Error("This type not currently supported") | _ => Error("This type not currently supported")
}; }
});
}; };
let functionParser = (nodeParser, name, args) => { let functionParser = (nodeParser, name, args) => {
let parseArgs = () => args |> E.A.fmap(nodeParser); let parseArgs = () => args |> E.A.fmap(nodeParser);
Js.log2("Parseargs", parseArgs);
switch (name) { switch (name) {
| "normal" => normal(args) | "normal" => normal(args)
| "lognormal" => lognormal(args) | "lognormal" => lognormal(args)
@ -379,6 +384,7 @@ let fromString = 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.log(mathJsToJson);
let mathJsParse = let mathJsParse =
E.R.bind(mathJsToJson, r => { E.R.bind(mathJsToJson, r => {
switch (MathJsonToMathJsAdt.run(r)) { switch (MathJsonToMathJsAdt.run(r)) {
@ -387,6 +393,7 @@ let fromString = str => {
} }
}); });
Js.log(mathJsParse);
let value = E.R.bind(mathJsParse, MathAdtToDistDst.run); let value = E.R.bind(mathJsParse, MathAdtToDistDst.run);
value; value;
}; };