From 688cf0b19e3e141b839239a704716ab9049bba22 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 2 Apr 2022 11:01:53 -0400 Subject: [PATCH] Changes in response to CR --- .../ReducerInterface_Distribution_test.res | 40 +++++++++---------- .../ReducerInterface_GenericDistribution.res | 30 +++++++++----- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res b/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res index ada50f3f..f57a4d7b 100644 --- a/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res +++ b/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res @@ -1,28 +1,28 @@ open Jest open Reducer_TestHelpers -let makeTest = (str, result) => test(str, () => expectEvalToBe(str, result)) +let testEval = (str, result) => test(str, () => expectEvalToBe(str, result)) describe("eval", () => { Only.describe("expressions", () => { - makeTest("normal(5,2)", "Ok(Normal(5,2))") - makeTest("5 to 2", "Error(TODO: Low value must be less than high value.)") - makeTest("to(2,5)", "Ok(Lognormal(1.1512925464970227,0.278507821238345))") - makeTest("to(-2,2)", "Ok(Normal(0,1.215913388057542))") - makeTest("lognormal(5,2)", "Ok(Lognormal(5,2))") - makeTest("mean(normal(5,2))", "Ok(5)") - makeTest("mean(lognormal(1,2))", "Ok(20.085536923187668)") - makeTest("normalize(normal(5,2))", "Ok(Normal(5,2))") - makeTest("toPointSet(normal(5,2))", "Ok(Point Set Distribution)") - makeTest("toSampleSet(normal(5,2), 100)", "Ok(Sample Set Distribution)") - makeTest("add(normal(5,2), normal(10,2))", "Ok(Normal(15,2.8284271247461903))") - makeTest("add(normal(5,2), lognormal(10,2))", "Ok(Sample Set Distribution)") - makeTest("dotAdd(normal(5,2), lognormal(10,2))", "Ok(Point Set Distribution)") - makeTest("dotAdd(normal(5,2), 3)", "Ok(Point Set Distribution)") - makeTest("add(normal(5,2), 3)", "Ok(Point Set Distribution)") - makeTest("add(3, normal(5,2))", "Ok(Point Set Distribution)") - makeTest("3+normal(5,2)", "Ok(Point Set Distribution)") - makeTest("add(3, 3)", "Ok(6)") - makeTest("truncateLeft(normal(5,2), 3)", "Ok(Point Set Distribution)") + testEval("normal(5,2)", "Ok(Normal(5,2))") + testEval("5 to 2", "Error(TODO: Low value must be less than high value.)") + testEval("to(2,5)", "Ok(Lognormal(1.1512925464970227,0.278507821238345))") + testEval("to(-2,2)", "Ok(Normal(0,1.215913388057542))") + testEval("lognormal(5,2)", "Ok(Lognormal(5,2))") + testEval("mean(normal(5,2))", "Ok(5)") + testEval("mean(lognormal(1,2))", "Ok(20.085536923187668)") + testEval("normalize(normal(5,2))", "Ok(Normal(5,2))") + testEval("toPointSet(normal(5,2))", "Ok(Point Set Distribution)") + testEval("toSampleSet(normal(5,2), 100)", "Ok(Sample Set Distribution)") + testEval("add(normal(5,2), normal(10,2))", "Ok(Normal(15,2.8284271247461903))") + testEval("add(normal(5,2), lognormal(10,2))", "Ok(Sample Set Distribution)") + testEval("dotAdd(normal(5,2), lognormal(10,2))", "Ok(Point Set Distribution)") + testEval("dotAdd(normal(5,2), 3)", "Ok(Point Set Distribution)") + testEval("add(normal(5,2), 3)", "Ok(Point Set Distribution)") + testEval("add(3, normal(5,2))", "Ok(Point Set Distribution)") + testEval("3+normal(5,2)", "Ok(Point Set Distribution)") + testEval("add(3, 3)", "Ok(6)") + testEval("truncateLeft(normal(5,2), 3)", "Ok(Point Set Distribution)") }) }) diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res index 9970c340..12d0ddb4 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res @@ -12,17 +12,17 @@ module Helpers = { let arithmeticMap = r => switch r { | "add" => #Add - | "dotAdd" => #Add + | "pointwiseAdd" => #Add | "subtract" => #Subtract - | "dotSubtract" => #Subtract + | "pointwiseSubtract" => #Subtract | "divide" => #Divide | "logarithm" => #Logarithm - | "dotDivide" => #Divide + | "pointwiseDivide" => #Divide | "exponentiate" => #Exponentiate - | "dotExponentiate" => #Exponentiate + | "pointwiseExponentiate" => #Exponentiate | "multiply" => #Multiply - | "dotMultiply" => #Multiply - | "dotLogarithm" => #Logarithm + | "pointwiseMultiply" => #Multiply + | "pointwiseLogarithm" => #Logarithm | _ => #Multiply } @@ -65,7 +65,7 @@ module SymbolicConstructors = { let oneFloat = name => switch name { | "exponential" => Ok(SymbolicDist.Exponential.make) - | _ => Error("impossible path") + | _ => Error("Unreachable state") } let twoFloat = name => @@ -75,13 +75,13 @@ module SymbolicConstructors = { | "beta" => Ok(SymbolicDist.Beta.make) | "lognormal" => Ok(SymbolicDist.Lognormal.make) | "to" => Ok(SymbolicDist.From90thPercentile.make) - | _ => Error("impossible path") + | _ => Error("Unreachable state") } let threeFloat = name => switch name { | "triangular" => Ok(SymbolicDist.Triangular.make) - | _ => Error("impossible path") + | _ => Error("Unreachable state") } let symbolicResultToOutput = ( @@ -128,12 +128,20 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option< Helpers.toDistFn(Truncate(None, Some(float)), dist) | ("truncate", [EvDistribution(dist), EvNumber(float1), EvNumber(float2)]) => Helpers.toDistFn(Truncate(Some(float1), Some(float2)), dist) - | (("add" | "multiply" | "subtract" | "divide" | "exponentiate" | "log") as arithmetic, [a, b] as args) => + | ( + ("add" | "multiply" | "subtract" | "divide" | "exponentiate" | "log") as arithmetic, + [a, b] as args, + ) => Helpers.catchAndConvertTwoArgsToDists(args)->E.O2.fmap(((fst, snd)) => Helpers.twoDiststoDistFn(Algebraic, arithmetic, fst, snd) ) | ( - ("dotAdd" | "dotMultiply" | "dotSubtract" | "dotDivide" | "dotExponentiate" | "dotLogarithm") as arithmetic, + ("pointwiseAdd" + | "pointwiseMultiply" + | "pointwiseSubtract" + | "pointwiseDivide" + | "pointwiseExponentiate" + | "pointwiseLogarithm") as arithmetic, [a, b] as args, ) => Helpers.catchAndConvertTwoArgsToDists(args)->E.O2.fmap(((fst, snd)) =>