Added enviornment to Function Registry

This commit is contained in:
Ozzie Gooen 2022-05-21 12:18:54 -04:00
parent ce58cf1bb3
commit 3531005a2b
3 changed files with 21 additions and 15 deletions

View File

@ -31,7 +31,7 @@ and frValueDistOrNumber = FRValueNumber(float) | FRValueDist(DistributionTypes.g
type fnDefinition = {
name: string,
inputs: array<frType>,
run: array<frValue> => result<expressionValue, string>,
run: (array<frValue>, DistributionOperation.env) => result<expressionValue, string>,
}
type function = {
@ -248,10 +248,10 @@ module FnDefinition = {
t.name ++ `(${inputs})`
}
let run = (t: t, args: array<expressionValue>) => {
let run = (t: t, args: array<expressionValue>, 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<expressionValue>) => {
let matchAndRun = (
r: registry,
fnName: string,
args: array<expressionValue>,
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
}

View File

@ -41,8 +41,9 @@ module Process = {
let twoDistsOrNumbersToDist = (
~fn: ((float, float)) => result<DistributionTypes.genericDist, string>,
~values: (frValueDistOrNumber, frValueDistOrNumber),
~env: DistributionOperation.env,
): result<DistributionTypes.genericDist, string> => {
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),
)
}
}

View File

@ -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)
}
}