From 835ce6b81fb0081065caf47b43576fce4f972e0f Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Tue, 19 Jul 2022 08:42:08 -0700 Subject: [PATCH] Added reducer to Function Registry --- .../FunctionRegistry/FunctionRegistry_Core.res | 17 +++++++++++++---- .../FunctionRegistry/Library/FR_Dict.res | 12 ++++++------ .../FunctionRegistry/Library/FR_Dist.res | 10 ++++++---- .../rescript/FunctionRegistry/Library/FR_Fn.res | 2 +- .../FunctionRegistry/Library/FR_List.res | 10 +++++----- .../FunctionRegistry/Library/FR_Number.res | 6 +++--- .../FunctionRegistry/Library/FR_Pointset.res | 4 ++-- .../FunctionRegistry/Library/FR_Scoring.res | 6 +++--- .../ReducerInterface_ExternalLibrary.res | 12 +++++++----- 9 files changed, 46 insertions(+), 33 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res index 25eb87b7..ea43561e 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res @@ -47,6 +47,7 @@ type fnDefinition = { array, array, GenericDist.env, + Reducer_Expression_T.reducerFn, ) => result, } @@ -342,10 +343,15 @@ module FnDefinition = { } } - let run = (t: t, args: array, env: GenericDist.env) => { + let run = ( + t: t, + args: array, + env: GenericDist.env, + reducer: Reducer_Expression_T.reducerFn, + ) => { let argValues = FRType.matchWithExpressionValueArray(t.inputs, args) switch argValues { - | Some(values) => t.run(args, values, env) + | Some(values) => t.run(args, values, env, reducer) | None => Error("Incorrect Types") } } @@ -452,6 +458,7 @@ module Registry = { ~fnName: string, ~args: array, ~env: GenericDist.env, + ~reducer: Reducer_Expression_T.reducerFn, ) => { let relevantFunctions = Js.Dict.get(registry.fnNameDict, fnName) |> E.O.default([]) let modified = {functions: relevantFunctions, fnNameDict: registry.fnNameDict} @@ -468,7 +475,8 @@ module Registry = { } switch Matcher.Registry.findMatches(modified, fnName, args) { - | Matcher.Match.FullMatch(match) => match->matchToDef->E.O2.fmap(FnDefinition.run(_, args, env)) + | Matcher.Match.FullMatch(match) => + match->matchToDef->E.O2.fmap(FnDefinition.run(_, args, env, reducer)) | SameNameDifferentArguments(m) => Some(Error(showNameMatchDefinitions(m))) | _ => None } @@ -478,8 +486,9 @@ module Registry = { registry, (fnName, args): ReducerInterface_InternalExpressionValue.functionCall, env, + reducer: Reducer_Expression_T.reducerFn, ) => { - _matchAndRun(~registry, ~fnName, ~args, ~env)->E.O2.fmap( + _matchAndRun(~registry, ~fnName, ~args, ~env, ~reducer)->E.O2.fmap( E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s)), ) } diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dict.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dict.res index 4a52f187..69ceb9c6 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dict.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dict.res @@ -52,7 +52,7 @@ let library = [ FnDefinition.make( ~name="merge", ~inputs=[FRTypeDict(FRTypeAny), FRTypeDict(FRTypeAny)], - ~run=(inputs, _, _) => { + ~run=(inputs, _, _, _) => { switch inputs { | [IEvRecord(d1), IEvRecord(d2)] => Internals.merge(d1, d2)->Ok | _ => Error(impossibleError) @@ -74,7 +74,7 @@ let library = [ FnDefinition.make( ~name="mergeMany", ~inputs=[FRTypeArray(FRTypeDict(FRTypeAny))], - ~run=(_, inputs, _) => + ~run=(_, inputs, _, _) => inputs ->Prepare.ToTypedArray.dicts ->E.R2.fmap(E.Dict.concatMany) @@ -96,7 +96,7 @@ let library = [ FnDefinition.make( ~name="keys", ~inputs=[FRTypeDict(FRTypeAny)], - ~run=(inputs, _, _) => + ~run=(inputs, _, _, _) => switch inputs { | [IEvRecord(d1)] => Internals.keys(d1)->Ok | _ => Error(impossibleError) @@ -116,7 +116,7 @@ let library = [ FnDefinition.make( ~name="values", ~inputs=[FRTypeDict(FRTypeAny)], - ~run=(inputs, _, _) => + ~run=(inputs, _, _, _) => switch inputs { | [IEvRecord(d1)] => Internals.values(d1)->Ok | _ => Error(impossibleError) @@ -136,7 +136,7 @@ let library = [ FnDefinition.make( ~name="toList", ~inputs=[FRTypeDict(FRTypeAny)], - ~run=(inputs, _, _) => + ~run=(inputs, _, _, _) => switch inputs { | [IEvRecord(dict)] => dict->Internals.toList->Ok | _ => Error(impossibleError) @@ -156,7 +156,7 @@ let library = [ FnDefinition.make( ~name="fromList", ~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))], - ~run=(inputs, _, _) => + ~run=(inputs, _, _, _) => switch inputs { | [IEvArray(items)] => Internals.fromList(items) | _ => Error(impossibleError) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dist.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dist.res index 424983d8..9e10577b 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dist.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dist.res @@ -21,7 +21,8 @@ module DistributionCreation = { FnDefinition.make( ~name, ~inputs=[FRTypeDistOrNumber, FRTypeDistOrNumber], - ~run=(_, inputs, env) => inputs->Prepare.ToValueTuple.twoDistOrNumber->process(~fn, ~env), + ~run=(_, inputs, env, _) => + inputs->Prepare.ToValueTuple.twoDistOrNumber->process(~fn, ~env), (), ) } @@ -30,7 +31,7 @@ module DistributionCreation = { FnDefinition.make( ~name, ~inputs=[FRTypeRecord([("p5", FRTypeDistOrNumber), ("p95", FRTypeDistOrNumber)])], - ~run=(_, inputs, env) => + ~run=(_, inputs, env, _) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), (), ) @@ -40,7 +41,7 @@ module DistributionCreation = { FnDefinition.make( ~name, ~inputs=[FRTypeRecord([("mean", FRTypeDistOrNumber), ("stdev", FRTypeDistOrNumber)])], - ~run=(_, inputs, env) => + ~run=(_, inputs, env, _) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), (), ) @@ -57,7 +58,8 @@ module DistributionCreation = { FnDefinition.make( ~name, ~inputs=[FRTypeDistOrNumber], - ~run=(_, inputs, env) => inputs->Prepare.ToValueTuple.oneDistOrNumber->process(~fn, ~env), + ~run=(_, inputs, env, _) => + inputs->Prepare.ToValueTuple.oneDistOrNumber->process(~fn, ~env), (), ) } diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Fn.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Fn.res index 512d564a..ee2ef343 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Fn.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Fn.res @@ -51,7 +51,7 @@ let library = [ FnDefinition.make( ~name="declare", ~inputs=[Declaration.frType], - ~run=(_, inputs, _) => { + ~run=(_, inputs, _, _) => { inputs->getOrError(0)->E.R.bind(Declaration.fromExpressionValue) }, (), 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 25924232..a181fb33 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res @@ -37,7 +37,7 @@ let library = [ FnDefinition.make( ~name="make", ~inputs=[FRTypeNumber, FRTypeAny], - ~run=(inputs, _, _) => { + ~run=(inputs, _, _, _) => { switch inputs { | [IEvNumber(number), value] => Internals.makeFromNumber(number, value)->Ok | _ => Error(impossibleError) @@ -58,7 +58,7 @@ let library = [ FnDefinition.make( ~name="upTo", ~inputs=[FRTypeNumber, FRTypeNumber], - ~run=(_, inputs, _) => + ~run=(_, inputs, _, _) => inputs ->Prepare.ToValueTuple.twoNumbers ->E.R2.fmap(((low, high)) => Internals.upTo(low, high)), @@ -76,7 +76,7 @@ let library = [ FnDefinition.make( ~name="first", ~inputs=[FRTypeArray(FRTypeAny)], - ~run=(inputs, _, _) => + ~run=(inputs, _, _, _) => switch inputs { | [IEvArray(array)] => Internals.first(array) | _ => Error(impossibleError) @@ -95,7 +95,7 @@ let library = [ FnDefinition.make( ~name="last", ~inputs=[FRTypeArray(FRTypeAny)], - ~run=(inputs, _, _) => + ~run=(inputs, _, _, _) => switch inputs { | [IEvArray(array)] => Internals.last(array) | _ => Error(impossibleError) @@ -115,7 +115,7 @@ let library = [ FnDefinition.make( ~name="reverse", ~inputs=[FRTypeArray(FRTypeAny)], - ~run=(inputs, _, _) => + ~run=(inputs, _, _, _) => switch inputs { | [IEvArray(array)] => Internals.reverse(array)->Ok | _ => Error(impossibleError) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Number.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Number.res index c7027f06..472ae60b 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Number.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Number.res @@ -9,7 +9,7 @@ module NumberToNumber = { FnDefinition.make( ~name, ~inputs=[FRTypeNumber], - ~run=(_, inputs, _) => { + ~run=(_, inputs, _, _) => { inputs ->getOrError(0) ->E.R.bind(Prepare.oneNumber) @@ -25,7 +25,7 @@ module ArrayNumberDist = { FnDefinition.make( ~name, ~inputs=[FRTypeArray(FRTypeNumber)], - ~run=(_, inputs, _) => + ~run=(_, inputs, _, _) => Prepare.ToTypedArray.numbers(inputs) ->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r)) ->E.R.bind(fn), @@ -36,7 +36,7 @@ module ArrayNumberDist = { FnDefinition.make( ~name, ~inputs=[FRTypeArray(FRTypeAny)], - ~run=(_, inputs, _) => + ~run=(_, inputs, _, _) => Prepare.ToTypedArray.numbers(inputs) ->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r)) ->E.R.bind(fn), 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 dc4daead..2c41a475 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res @@ -41,7 +41,7 @@ let library = [ FnDefinition.make( ~name="makeContinuous", ~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], - ~run=(_, inputs, _) => inputsTodist(inputs, r => Continuous(Continuous.make(r))), + ~run=(_, inputs, _, _) => inputsTodist(inputs, r => Continuous(Continuous.make(r))), (), ), ], @@ -64,7 +64,7 @@ let library = [ FnDefinition.make( ~name="makeDiscrete", ~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], - ~run=(_, inputs, _) => inputsTodist(inputs, r => Discrete(Discrete.make(r))), + ~run=(_, inputs, _, _) => inputsTodist(inputs, r => Discrete(Discrete.make(r))), (), ), ], diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Scoring.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Scoring.res index d8d5ddd0..972501e9 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Scoring.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Scoring.res @@ -30,7 +30,7 @@ let library = [ ("prior", FRTypeDist), ]), ], - ~run=(_, inputs, env) => { + ~run=(_, inputs, env, _) => { switch FunctionRegistry_Helpers.Prepare.ToValueArray.Record.threeArgs(inputs) { | Ok([FRValueDist(estimate), FRValueDistOrNumber(FRValueDist(d)), FRValueDist(prior)]) => runScoring(estimate, Score_Dist(d), Some(prior), env) @@ -49,7 +49,7 @@ let library = [ FnDefinition.make( ~name="logScore", ~inputs=[FRTypeRecord([("estimate", FRTypeDist), ("answer", FRTypeDistOrNumber)])], - ~run=(_, inputs, env) => { + ~run=(_, inputs, env, _) => { switch FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs(inputs) { | Ok([FRValueDist(estimate), FRValueDistOrNumber(FRValueDist(d))]) => runScoring(estimate, Score_Dist(d), None, env) @@ -74,7 +74,7 @@ let library = [ FnDefinition.make( ~name="klDivergence", ~inputs=[FRTypeDist, FRTypeDist], - ~run=(_, inputs, env) => { + ~run=(_, inputs, env, _) => { switch inputs { | [FRValueDist(estimate), FRValueDist(d)] => runScoring(estimate, Score_Dist(d), None, env) diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res index d6a14dea..fc0a2821 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res @@ -4,16 +4,18 @@ type internalExpressionValue = InternalExpressionValue.t /* Map external calls of Reducer */ -let dispatch = (call: InternalExpressionValue.functionCall, environment, reducer, chain): result< - internalExpressionValue, - 'e, -> => { +let dispatch = ( + call: InternalExpressionValue.functionCall, + environment, + reducer: Reducer_Expression_T.reducerFn, + chain, +): result => { E.A.O.firstSomeFn([ () => ReducerInterface_GenericDistribution.dispatch(call, environment), () => ReducerInterface_Date.dispatch(call, environment), () => ReducerInterface_Duration.dispatch(call, environment), () => ReducerInterface_Number.dispatch(call, environment), - () => FunctionRegistry_Library.dispatch(call, environment), + () => FunctionRegistry_Library.dispatch(call, environment, reducer), ])->E.O2.default(chain(call, environment, reducer)) }