diff --git a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res index 2418b4d8..7ddb57d4 100644 --- a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res +++ b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res @@ -63,6 +63,9 @@ describe("FunctionRegistry Library", () => { testEvalToBe("SampleSet.fromList([3,5,2,3,5,2,3,5,2,3,3,5])", "Ok(Sample Set Distribution)") testEvalToBe("SampleSet.fromList([3,5,2,3,5,2,3,5,2,3,3,5])", "Ok(Sample Set Distribution)") testEvalToBe("SampleSet.fromFn({|| sample(normal(5,2))})", "Ok(Sample Set Distribution)") + testEvalToBe("SampleSet.min(SampleSet.fromDist(normal(50,2)), 2)", "Ok(Sample Set Distribution)") + testEvalToBe("mean(SampleSet.min(SampleSet.fromDist(normal(50,2)), 2))", "Ok(2)") + testEvalToBe("SampleSet.max(SampleSet.fromDist(normal(50,2)), 10)", "Ok(Sample Set Distribution)") testEvalToBe( "addOne(t)=t+1; SampleSet.toList(SampleSet.map(SampleSet.fromList([1,2,3,4,5,6]), addOne))", "Ok([2,3,4,5,6,7])", diff --git a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res index 364ecc56..41856882 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res @@ -159,3 +159,9 @@ let truncate = (t, ~leftCutoff: option, ~rightCutoff: option) => { let withTruncatedRight = t => rightCutoff |> E.O.dimap(left => truncateRight(t, left), _ => Ok(t)) t->withTruncatedLeft |> E.R2.bind(withTruncatedRight) } + +let minOfTwo = (t1: t, t2: t) => map2(~fn=(a, b) => Ok(Js.Math.min_float(a, b)), ~t1, ~t2) +let maxOfTwo = (t1: t, t2: t) => map2(~fn=(a, b) => Ok(Js.Math.max_float(a, b)), ~t1, ~t2) + +let minOfFloat = (t: t, f: float) => samplesMap(~fn=a => Ok(Js.Math.min_float(a, f)), t) +let maxOfFloat = (t: t, f: float) => samplesMap(~fn=a => Ok(Js.Math.max_float(a, f)), t) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res index c40fe349..a11742db 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res @@ -75,7 +75,7 @@ module Internal = { } } -let library = [ +let libaryBase = [ Function.make( ~name="fromDist", ~nameSpace, @@ -274,3 +274,63 @@ let library = [ (), ), ] + +module Comparison = { + let template = (name, inputs, run) => { + FnDefinition.make( + ~name, + ~inputs, + ~run=(inputs, _, _, _) => { + run(inputs) + }, + (), + ) + } + + let wrapper = r => + r + ->E.R2.fmap(r => r->Wrappers.sampleSet->Wrappers.evDistribution) + ->E.R2.errMap(SampleSetDist.Error.toString) + + let mkBig = (name, withDist, withFloat) => + Function.make( + ~name, + ~nameSpace, + ~requiresNamespace=false, + ~examples=[ + `SampleSet.${name}(SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(6,2)))`, + `SampleSet.${name}(SampleSet.fromDist(normal(5,2)), 3.0)`, + `SampleSet.${name}(4.0, SampleSet.fromDist(normal(6,2)))`, + ], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, + ~definitions=[ + template(name, [FRTypeDist, FRTypeDist], inputs => { + switch inputs { + | [IEvDistribution(SampleSet(dist1)), IEvDistribution(SampleSet(dist2))] => + withDist(dist1, dist2)->wrapper + | _ => Error(impossibleError) + } + }), + template(name, [FRTypeDist, FRTypeNumber], inputs => { + switch inputs { + | [IEvDistribution(SampleSet(dist)), IEvNumber(f)] => withFloat(dist, f)->wrapper + | _ => Error(impossibleError) + } + }), + template(name, [FRTypeNumber, FRTypeDist], inputs => { + switch inputs { + | [IEvNumber(f), IEvDistribution(SampleSet(dist))] => withFloat(dist, f)->wrapper + | _ => Error(impossibleError) + } + }), + ], + (), + ) + + let library = [ + mkBig("min", SampleSetDist.minOfTwo, SampleSetDist.minOfFloat), + mkBig("max", SampleSetDist.maxOfTwo, SampleSetDist.maxOfFloat), + ] +} + +let library = E.A.append(libaryBase, Comparison.library)