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

View File

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

View File

@ -1,4 +1,5 @@
type t = ExpressionTypes.Program.program; 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 => // let run = (p:program) => p |> E.A.last |> E.O.fmap(r =>
// ) // )

View File

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