diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res index 115d5307..1c06d3c0 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res @@ -4,6 +4,8 @@ let impossibleError = "Wrong inputs / Logically impossible" module Wrappers = { let symbolic = r => DistributionTypes.Symbolic(r) + let pointSet = r => DistributionTypes.PointSet(r) + let sampleSet = r => DistributionTypes.SampleSet(r) let evDistribution = r => ReducerInterface_InternalExpressionValue.IEvDistribution(r) let evNumber = r => ReducerInterface_InternalExpressionValue.IEvNumber(r) let evArray = r => ReducerInterface_InternalExpressionValue.IEvArray(r) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res index 9b0f6737..9236ab84 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res @@ -5,6 +5,7 @@ let fnList = Belt.Array.concatMany([ FR_List.library, FR_Number.library, FR_Pointset.library, + FR_Sampleset.library, FR_Scoring.library, ]) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res index ba9918da..762fe31b 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res @@ -67,12 +67,12 @@ module Internals = { environment, reducer, ) - rNewElem->E.R2.fmap(newElem => + rNewElem->E.R2.fmap(newElem => { switch newElem { | IEvBool(true) => list{elem, ...acc} | _ => acc } - ) + }) }) ) rMappedList->E.R2.fmap(mappedList => mappedList->Belt.List.toArray->Wrappers.evArray) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res index 2c41a475..b90bc96c 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res @@ -70,4 +70,33 @@ let library = [ ], (), ), + Function.make( + ~name="maked", + ~nameSpace, + ~requiresNamespace, + ~examples=[`Pointset.maked(normal(5,2))`], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, + ~definitions=[ + FnDefinition.make( + ~name="maked", + ~inputs=[FRTypeDist], + ~run=(_, inputs, env, _) => + switch inputs { + | [FRValueDist(dist)] => + GenericDist.toPointSet( + dist, + ~xyPointLength=env.xyPointLength, + ~sampleCount=env.sampleCount, + (), + ) + ->E.R2.fmap(Wrappers.pointSet) + ->E.R2.fmap(Wrappers.evDistribution) + ->E.R2.errMap(_ => "") + | _ => Error(impossibleError) + }, + (), + ), + ], + (), + ), ] diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res new file mode 100644 index 00000000..e92551da --- /dev/null +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res @@ -0,0 +1,193 @@ +open FunctionRegistry_Core +open FunctionRegistry_Helpers + +let nameSpace = "Sampleset" +let requiresNamespace = true + +module Internal = { + type t = SampleSetDist.t + let doLambdaCall = (aLambdaValue, list, environment, reducer) => + switch Reducer_Expression_Lambda.doLambdaCall(aLambdaValue, list, environment, reducer) { + | Ok(IEvNumber(f)) => Ok(f) + | _ => Error(Operation.SampleMapNeedsNtoNFunction) + } + + let toType = (r): result< + ReducerInterface_InternalExpressionValue.t, + Reducer_ErrorValue.errorValue, + > => + switch r { + | Ok(r) => Ok(Wrappers.evDistribution(SampleSet(r))) + | Error(r) => Error(REDistributionError(SampleSetError(r))) + } + + let map1 = (sampleSetDist: t, aLambdaValue, env, reducer) => { + let fn = r => doLambdaCall(aLambdaValue, list{IEvNumber(r)}, env, reducer) + SampleSetDist.samplesMap(~fn, sampleSetDist)->toType + } + + let map2 = (t1: t, t2: t, aLambdaValue, env, reducer) => { + let fn = (a, b) => doLambdaCall(aLambdaValue, list{IEvNumber(a), IEvNumber(b)}, env, reducer) + SampleSetDist.map2(~fn, ~t1, ~t2)->toType + } + + let map3 = (t1: t, t2: t, t3: t, aLambdaValue, env, reducer) => { + let fn = (a, b, c) => + doLambdaCall(aLambdaValue, list{IEvNumber(a), IEvNumber(b), IEvNumber(c)}, env, reducer) + SampleSetDist.map3(~fn, ~t1, ~t2, ~t3)->toType + } + + let parseSampleSetArray = (arr: array): option< + array, + > => { + let parseSampleSet = (value: internalExpressionValue): option => + switch value { + | IEvDistribution(SampleSet(dist)) => Some(dist) + | _ => None + } + E.A.O.openIfAllSome(E.A.fmap(parseSampleSet, arr)) + } + + let mapN = (aValueArray: array, aLambdaValue, env, reducer) => { + switch parseSampleSetArray(aValueArray) { + | Some(t1) => + let fn = a => + doLambdaCall( + aLambdaValue, + list{IEvArray(E.A.fmap(x => Wrappers.evNumber(x), a))}, + env, + reducer, + ) + SampleSetDist.mapN(~fn, ~t1)->toType + | None => Error(REFunctionNotFound("")) + } + } +} + +let library = [ + Function.make( + ~name="maker", + ~nameSpace, + ~requiresNamespace, + ~examples=[`Sampleset.maker(normal(5,2))`], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, + ~definitions=[ + FnDefinition.make( + ~name="maker", + ~inputs=[FRTypeDist], + ~run=(_, inputs, env, _) => + switch inputs { + | [FRValueDist(dist)] => + GenericDist.toSampleSetDist(dist, env.sampleCount) + ->E.R2.fmap(Wrappers.sampleSet) + ->E.R2.fmap(Wrappers.evDistribution) + ->E.R2.errMap(_ => "") + | _ => Error(impossibleError) + }, + (), + ), + ], + (), + ), + Function.make( + ~name="map", + ~nameSpace, + ~requiresNamespace, + ~examples=[`Sampleset.map(Sampleset.maker(normal(5,2)), {|x| x + 1})`], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, + ~definitions=[ + FnDefinition.make( + ~name="map", + ~inputs=[FRTypeDist, FRTypeLambda], + ~run=(inputs, _, env, reducer) => + switch inputs { + | [IEvDistribution(SampleSet(dist)), IEvLambda(lambda)] => + Internal.map1(dist, lambda, env, reducer)->E.R2.errMap(_ => "") + | _ => Error(impossibleError) + }, + (), + ), + ], + (), + ), + Function.make( + ~name="map2", + ~nameSpace, + ~requiresNamespace, + ~examples=[ + `Sampleset.map2(Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2)), {|x, y| x + y})`, + ], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, + ~definitions=[ + FnDefinition.make( + ~name="map2", + ~inputs=[FRTypeDist, FRTypeDist, FRTypeLambda], + ~run=(inputs, _, env, reducer) => { + Js.log2("WHY DIDNT IT MATCH", inputs) + switch inputs { + | [ + IEvDistribution(SampleSet(dist1)), + IEvDistribution(SampleSet(dist2)), + IEvLambda(lambda), + ] => + Internal.map2(dist1, dist2, lambda, env, reducer)->E.R2.errMap(_ => "") + | _ => Error(impossibleError) + } + }, + (), + ), + ], + (), + ), + Function.make( + ~name="map3", + ~nameSpace, + ~requiresNamespace, + ~examples=[ + `Sampleset.map3(Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2)), {|x, y, z| max([x,y,z]))`, + ], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, + ~definitions=[ + FnDefinition.make( + ~name="map3", + ~inputs=[FRTypeDist, FRTypeDist, FRTypeDist, FRTypeLambda], + ~run=(inputs, _, env, reducer) => + switch inputs { + | [ + IEvDistribution(SampleSet(dist1)), + IEvDistribution(SampleSet(dist2)), + IEvDistribution(SampleSet(dist3)), + IEvLambda(lambda), + ] => + Internal.map3(dist1, dist2, dist3, lambda, env, reducer)->E.R2.errMap(_ => "") + | _ => Error(impossibleError) + }, + (), + ), + ], + (), + ), + Function.make( + ~name="mapN", + ~nameSpace, + ~requiresNamespace, + ~examples=[ + `Sampleset.mapN([Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2))], {|x| max(x)`, + ], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, + ~definitions=[ + FnDefinition.make( + ~name="mapN", + ~inputs=[FRTypeArray(FRTypeDist), FRTypeLambda], + ~run=(inputs, _, env, reducer) => + switch inputs { + | [IEvArray(dists), IEvLambda(lambda)] => + Internal.mapN(dists, lambda, env, reducer)->E.R2.errMap(_ => "") + | _ => Error(impossibleError) + }, + (), + ), + ], + (), + ), +] diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res index 0f3e1e4b..a2e66298 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res @@ -109,22 +109,6 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce | Error(r) => Error(REDistributionError(SampleSetError(r))) } - let map1 = (sampleSetDist: t, aLambdaValue) => { - let fn = r => doLambdaCall(aLambdaValue, list{IEvNumber(r)}) - toType(SampleSetDist.samplesMap(~fn, sampleSetDist)) - } - - let map2 = (t1: t, t2: t, aLambdaValue) => { - let fn = (a, b) => doLambdaCall(aLambdaValue, list{IEvNumber(a), IEvNumber(b)}) - SampleSetDist.map2(~fn, ~t1, ~t2)->toType - } - - let map3 = (t1: t, t2: t, t3: t, aLambdaValue) => { - let fn = (a, b, c) => - doLambdaCall(aLambdaValue, list{IEvNumber(a), IEvNumber(b), IEvNumber(c)}) - SampleSetDist.map3(~fn, ~t1, ~t2, ~t3)->toType - } - let parseSampleSetArray = (arr: array): option< array, > => { @@ -185,29 +169,6 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce doAddString(aValueString, bValueString) | ("inspect", [value, IEvString(label)]) => inspectLabel(value, label) | ("inspect", [value]) => inspect(value) - | ("mapSamples", [IEvDistribution(SampleSet(dist)), IEvLambda(aLambdaValue)]) => - SampleMap.map1(dist, aLambdaValue) - | ( - "mapSamples2", - [ - IEvDistribution(SampleSet(dist1)), - IEvDistribution(SampleSet(dist2)), - IEvLambda(aLambdaValue), - ], - ) => - SampleMap.map2(dist1, dist2, aLambdaValue) - | ( - "mapSamples3", - [ - IEvDistribution(SampleSet(dist1)), - IEvDistribution(SampleSet(dist2)), - IEvDistribution(SampleSet(dist3)), - IEvLambda(aLambdaValue), - ], - ) => - SampleMap.map3(dist1, dist2, dist3, aLambdaValue) - | ("mapSamplesN", [IEvArray(aValueArray), IEvLambda(aLambdaValue)]) => - SampleMap.mapN(aValueArray, aLambdaValue) | (_, [IEvBool(_)]) | (_, [IEvNumber(_)]) | (_, [IEvString(_)])