From 76bbfb2ef18a6b71a5ae8411fe3e474e37d7f79d Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Wed, 18 May 2022 18:42:28 -0400 Subject: [PATCH] Added lognormal fn definitions --- .../src/rescript/FunctionRegistry.res | 49 +++++++++++++------ .../ReducerInterface_GenericDistribution.res | 22 ++++----- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry.res b/packages/squiggle-lang/src/rescript/FunctionRegistry.res index f0d142c1..d01d5736 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry.res @@ -95,13 +95,9 @@ module FnDefinition = { if E.A.length(f.inputs) !== E.A.length(args) { None } else { - let foo = E.A.zip(inputTypes, args) - ->(e => {Js.log2("Here", e); e}) + E.A.zip(inputTypes, args) ->E.A2.fmap(((input, arg)) => matchInput(input, arg)) - ->(e => {Js.log2("Here2", e); e}) ->E.A.O.openIfAllSome - ->(e => {Js.log2("Here3", e); e}); - foo } } @@ -121,9 +117,7 @@ module FnDefinition = { } let run = (f: fnDefinition, args: array) => { - Js.log3("Run", f, args) let argValues = getArgValues(f, args) - Js.log2("RunArgValues", argValues) switch argValues { | Some(values) => f.run(values) | None => Error("Impossible") @@ -233,16 +227,16 @@ module Registry = { let matchAndRun = (r: registry, fnName: string, args: array) => { switch findMatches(r, fnName, args) { - | Match.FullMatch(m) => fullMatchToDef(r, m)->E.O2.fmap(r => { + | Match.FullMatch(m) => + fullMatchToDef(r, m)->E.O2.fmap(r => { FnDefinition.run(r, args) - }) + }) | _ => None } } } -let twoNumberInputs = (inputs: array) =>{ - Js.log2("HII",inputs); +let twoNumberInputs = (inputs: array) => { switch inputs { | [Number(n1), Number(n2)] => Ok(n1, n2) | _ => Error("Wrong inputs / Logically impossible") @@ -279,9 +273,36 @@ let normal = Function.make( ), ), Function.makeDefinition("normal", [I_Record([("p5", I_Numeric), ("p95", I_Numeric)])], inputs => - twoNumberInputsRecord("p5", "p95", inputs)->E.R.bind(((mean, stdev)) => - Ok(p5and95(mean, stdev)) - ) + twoNumberInputsRecord("p5", "p95", inputs)->E.R.bind(((v1, v2)) => Ok(p5and95(v1, v2))) ), ], ) + +let logNormal = Function.make( + "Lognormal", + [ + Function.makeDefinition("lognormal", [I_Numeric, I_Numeric], inputs => + twoNumberInputs(inputs)->E.R.bind(((mu, sigma)) => + SymbolicDist.Lognormal.make(mu, sigma)->E.R2.fmap(contain) + ) + ), + Function.makeDefinition( + "lognormal", + [I_Record([("p5", I_Numeric), ("p95", I_Numeric)])], + inputs => + twoNumberInputsRecord("p5", "p95", inputs)->E.R.bind(((p5, p95)) => Ok( + contain(SymbolicDist.Lognormal.from90PercentCI(p5, p95)), + )), + ), + Function.makeDefinition( + "lognormal", + [I_Record([("mean", I_Numeric), ("stdev", I_Numeric)])], + inputs => + twoNumberInputsRecord("mean", "stdev", inputs)->E.R.bind(((mean, stdev)) => + SymbolicDist.Lognormal.fromMeanAndStdev(mean, stdev)->E.R2.fmap(contain) + ), + ), + ], +) + +let allFunctions = [normal, logNormal] diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res index 824b5b32..d2e9b65e 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res @@ -227,8 +227,7 @@ let dispatchToGenericOutput = ( | ("delta", [EvNumber(f)]) => SymbolicDist.Float.makeSafe(f)->SymbolicConstructors.symbolicResultToOutput | ( - ( - "uniform" + ("uniform" | "beta" | "lognormal" | "cauchy" @@ -386,19 +385,20 @@ let genericOutputToReducerValue = (o: DistributionOperation.outputType): result< | GenDistError(err) => Error(REDistributionError(err)) } -let registered = [FunctionRegistry.normal] +let registered = FunctionRegistry.allFunctions -let tryRegistry = (call:ExpressionValue.functionCall) => { - let (fnName, args) = call; +let tryRegistry = (call: ExpressionValue.functionCall) => { + let (fnName, args) = call let response = FunctionRegistry.Registry.matchAndRun(registered, fnName, args) - let foo = response -> E.O2.fmap(r => r->E.R2.errMap(s => Reducer_ErrorValue.RETodo(s))) + let foo = response->E.O2.fmap(r => r->E.R2.errMap(s => Reducer_ErrorValue.RETodo(s))) foo } -let dispatch = (call:ExpressionValue.functionCall, environment) => { - let regularDispatch = dispatchToGenericOutput(call, environment)->E.O2.fmap(genericOutputToReducerValue) - switch(regularDispatch){ - | Some(x) => Some(x) - | None => tryRegistry(call) +let dispatch = (call: ExpressionValue.functionCall, environment) => { + let regularDispatch = + dispatchToGenericOutput(call, environment)->E.O2.fmap(genericOutputToReducerValue) + switch regularDispatch { + | Some(x) => Some(x) + | None => tryRegistry(call) } }