diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res index fcb27cbe..3559fb5c 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res @@ -31,7 +31,7 @@ and frValueDistOrNumber = FRValueNumber(float) | FRValueDist(DistributionTypes.g type fnDefinition = { name: string, inputs: array, - run: array => result, + run: (array, DistributionOperation.env) => result, } type function = { @@ -248,10 +248,10 @@ module FnDefinition = { t.name ++ `(${inputs})` } - let run = (t: t, args: array) => { + let run = (t: t, args: array, env: DistributionOperation.env) => { let argValues = FRType.matchWithExpressionValueArray(t.inputs, args) switch argValues { - | Some(values) => t.run(values) + | Some(values) => t.run(values, env) | None => Error("Incorrect Types") } } @@ -278,7 +278,12 @@ module Registry = { to the registry, then it's possible that there could be a match after the registry is called. However, for now, we could just call the registry last. */ - let matchAndRun = (r: registry, fnName: string, args: array) => { + let matchAndRun = ( + r: registry, + fnName: string, + args: array, + env: DistributionOperation.env, + ) => { let matchToDef = m => Matcher.Registry.matchToDef(r, m) let showNameMatchDefinitions = matches => { let defs = @@ -291,7 +296,7 @@ module Registry = { `There are function matches for ${fnName}(), but with different arguments: ${defs}` } switch Matcher.Registry.findMatches(r, fnName, args) { - | Matcher.Match.FullMatch(match) => match->matchToDef->E.O2.fmap(FnDefinition.run(_, args)) + | Matcher.Match.FullMatch(match) => match->matchToDef->E.O2.fmap(FnDefinition.run(_, args, env)) | SameNameDifferentArguments(m) => Some(Error(showNameMatchDefinitions(m))) | _ => None } diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res index fd0acb9b..799d119c 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res @@ -41,8 +41,9 @@ module Process = { let twoDistsOrNumbersToDist = ( ~fn: ((float, float)) => result, ~values: (frValueDistOrNumber, frValueDistOrNumber), + ~env: DistributionOperation.env, ): result => { - let toSampleSet = r => GenericDist.toSampleSetDist(r, 1000) + let toSampleSet = r => GenericDist.toSampleSetDist(r, env.sampleCount) let mapFnResult = r => switch r { | Ok(r) => Ok(GenericDist.sample(r)) @@ -90,14 +91,14 @@ module Process = { } module TwoArgDist = { - let process = (~fn, r) => + let process = (~fn, ~env, r) => r - ->E.R.bind(Process.twoDistsOrNumbersToDistUsingSymbolicDist(~fn, ~values=_)) + ->E.R.bind(Process.twoDistsOrNumbersToDistUsingSymbolicDist(~fn, ~values=_, ~env)) ->E.R2.fmap(Wrappers.evDistribution) let mkRegular = (name, fn) => { - FnDefinition.make(~name, ~inputs=[FRTypeDistOrNumber, FRTypeDistOrNumber], ~run=inputs => - inputs->Prepare.ToValueTuple.twoDistOrNumber->process(~fn) + FnDefinition.make(~name, ~inputs=[FRTypeDistOrNumber, FRTypeDistOrNumber], ~run=(inputs, env) => + inputs->Prepare.ToValueTuple.twoDistOrNumber->process(~fn, ~env) ) } @@ -105,7 +106,7 @@ module TwoArgDist = { FnDefinition.make( ~name, ~inputs=[FRTypeRecord([("p5", FRTypeDistOrNumber), ("p95", FRTypeDistOrNumber)])], - ~run=inputs => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn), + ~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), ) } @@ -113,7 +114,7 @@ module TwoArgDist = { FnDefinition.make( ~name, ~inputs=[FRTypeRecord([("mean", FRTypeDistOrNumber), ("stdev", FRTypeDistOrNumber)])], - ~run=inputs => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn), + ~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), ) } } diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res index 714ad47b..d7aaeee9 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res @@ -363,8 +363,8 @@ let genericOutputToReducerValue = (o: DistributionOperation.outputType): result< let registered = FunctionRegistry_Library.allFunctions -let tryRegistry = ((fnName, args): ExpressionValue.functionCall) => { - FunctionRegistry_Core.Registry.matchAndRun(registered, fnName, args)->E.O2.fmap( +let tryRegistry = ((fnName, args): ExpressionValue.functionCall, env) => { + FunctionRegistry_Core.Registry.matchAndRun(registered, fnName, args, env)->E.O2.fmap( E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s)), ) } @@ -374,6 +374,6 @@ let dispatch = (call: ExpressionValue.functionCall, environment) => { dispatchToGenericOutput(call, environment)->E.O2.fmap(genericOutputToReducerValue) switch regularDispatch { | Some(x) => Some(x) - | None => tryRegistry(call) + | None => tryRegistry(call, environment) } }