Simple function with no params

This commit is contained in:
Ozzie Gooen 2020-07-31 10:41:34 +01:00
parent d79c4baa9d
commit 7ba49f7219
5 changed files with 44 additions and 16 deletions

View File

@ -268,7 +268,7 @@ let callableFunction = (evaluationParams, name, args) => {
|> E.R.bind(_, Render.toFloat)
)
|> E.A.R.firstErrorOrOpen;
b |> E.R.bind(_, Functions.fnn(name));
b |> E.R.bind(_, Functions.fnn(evaluationParams, name));
};
module Render = {

View File

@ -49,15 +49,16 @@ let to_: array(node) => result(node, string) =
Error("Low value must be less than high value.")
| _ => Error("Requires 2 variables");
let fnn = (name, args: array(node)) => {
switch (name) {
| "normal" => apply2(twoFloatsToOkSym(SymbolicDist.Normal.make), args)
| "uniform" => apply2(twoFloatsToOkSym(SymbolicDist.Uniform.make), args)
| "beta" => apply2(twoFloatsToOkSym(SymbolicDist.Beta.make), args)
| "cauchy" => apply2(twoFloatsToOkSym(SymbolicDist.Cauchy.make), args)
| "lognormal" => apply2(twoFloatsToOkSym(SymbolicDist.Lognormal.make), args)
| "lognormalFromMeanAndStdDev" => apply2(twoFloatsToOkSym(SymbolicDist.Lognormal.fromMeanAndStdev), args)
| "exponential" =>
let fnn = (evaluationParams:ExpressionTypes.ExpressionTree.evaluationParams, name, args: array(node)) => {
switch (name, ExpressionTypes.ExpressionTree.Environment.get(evaluationParams.environment, name)) {
| (_, Some(`Function(t))) => t(`Function(t));
| ("normal", _) => apply2(twoFloatsToOkSym(SymbolicDist.Normal.make), args)
| ("uniform", _) => apply2(twoFloatsToOkSym(SymbolicDist.Uniform.make), args)
| ("beta", _) => apply2(twoFloatsToOkSym(SymbolicDist.Beta.make), args)
| ("cauchy", _) => apply2(twoFloatsToOkSym(SymbolicDist.Cauchy.make), args)
| ("lognormal", _) => apply2(twoFloatsToOkSym(SymbolicDist.Lognormal.make), args)
| ("lognormalFromMeanAndStdDev", _) => apply2(twoFloatsToOkSym(SymbolicDist.Lognormal.fromMeanAndStdev), args)
| ("exponential", _) =>
switch (args) {
| [|
`SymbolicDist(`Float(a)),
@ -65,7 +66,7 @@ let fnn = (name, args: array(node)) => {
Ok(`SymbolicDist(SymbolicDist.Exponential.make(a)));
| _ => Error("Needs 3 valid arguments")
}
| "triangular" =>
| ("triangular", _) =>
switch (args) {
| [|
`SymbolicDist(`Float(a)),
@ -76,7 +77,7 @@ let fnn = (name, args: array(node)) => {
|> E.R.fmap(r => `SymbolicDist(r))
| _ => Error("Needs 3 valid arguments")
}
| "to" => to_(args)
| ("to", _) => to_(args)
| _ => Error("Function not found")
};
};

View File

@ -9,9 +9,15 @@ module MathJsonToMathJsAdt = {
| Blocks(array(arg))
| Object(Js.Dict.t(arg))
| Assignment(arg, arg)
| FunctionAssignment(fnAssignment)
and fn = {
name: string,
args: array(arg),
}
and fnAssignment = {
name: string,
args: array(string),
expression: arg,
};
let rec run = (j: Js.Json.t) =>
@ -55,6 +61,14 @@ module MathJsonToMathJsAdt = {
let block = r => r |> field("node", run);
let args = j |> field("blocks", array(block)) |> E.A.O.concatSomes;
Some(Blocks(args));
| "FunctionAssignmentNode" =>
let name = j |> field("name", string);
let args = j |> field("params", array(field("name", string)));
let expression = j |> field("expr", run);
expression
|> E.O.fmap(expression =>
FunctionAssignment({name, args, expression})
);
| n =>
Js.log3("Couldn't parse mathjs node", j, n);
None;
@ -66,7 +80,7 @@ module MathAdtToDistDst = {
open MathJsonToMathJsAdt;
let handleSymbol = (inputVars: inputVars, sym) => {
Ok(`Symbol(sym))
Ok(`Symbol(sym));
};
module MathAdtCleaner = {
@ -82,7 +96,6 @@ module MathAdtToDistDst = {
| "t" => Some(f *. 1000000000000.)
| _ => None
};
let rec run =
fun
| Fn({name: "multiply", args: [|Value(f), Symbol(s)|]}) as doNothing =>
@ -96,6 +109,7 @@ module MathAdtToDistDst = {
| Value(v) => Value(v)
| Blocks(args) => Blocks(args |> E.A.fmap(run))
| Assignment(a, b) => Assignment(a, run(b))
| FunctionAssignment(a) => FunctionAssignment(a)
| Object(v) =>
Object(
v
@ -281,7 +295,11 @@ module MathAdtToDistDst = {
| "sample"
| "cdf"
| "pdf" => operationParser(name, parseArgs())
| n => Error(n ++ "(...) is not currently supported")
| name =>
parseArgs()
|> E.R.fmap((args: array(ExpressionTypes.ExpressionTree.node)) =>
`CallableFunction((name, args))
)
};
};
@ -298,10 +316,19 @@ module MathAdtToDistDst = {
Error("This type not currently supported");
};
// | FunctionAssignment({name, args, expression}) => {
// let evaluatedExpression = run(expression);
// `Function(_ => Ok(evaluatedExpression));
// }
let rec topLevel =
(inputVars: inputVars, r)
: result(ExpressionTypes.Program.program, string) =>
switch (r) {
| FunctionAssignment({name, args, expression}) =>
switch (nodeParser(inputVars, expression)) {
| Ok(r) => Ok([|`Assignment((name, `Function(_ => Ok(r))))|])
| _ => Error("")
}
| Value(_) as r =>
nodeParser(inputVars, r) |> E.R.fmap(r => [|`Expression(r)|])
| Fn(_) as r =>

View File

@ -1,4 +1,5 @@
type t = ExpressionTypes.Program.program;
let last = (r:t) => E.A.last(r) |> E.O.toResult("No rendered lines");
// let run = (p:program) => p |> E.A.last |> E.O.fmap(r =>
// )

View File

@ -115,7 +115,6 @@ module Internals = {
switch (statement) {
| `Assignment(name, node) =>
ins := addVariable(ins^, name, node);
Js.log4("HIHI", ins, name, node);
None;
| `Expression(node) => Some(runNode(ins^, node))
}