From c200259c79265bf555997960d69a768bb03ca0fa Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 21 May 2022 13:52:17 -0400 Subject: [PATCH] Added simpe README and resi for FunctionRegistry --- .../FunctionRegistry_Core.res | 12 ++-- .../FunctionRegistry_Core.resi | 59 +++++++++++++++++++ .../FunctionRegistry_Helpers.res | 1 + .../src/rescript/FunctionRegistry/README.md | 46 +++++++++++++++ .../ReducerInterface_GenericDistribution.res | 4 +- 5 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.resi create mode 100644 packages/squiggle-lang/src/rescript/FunctionRegistry/README.md diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res index 3559fb5c..92c575f0 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res @@ -279,12 +279,12 @@ module Registry = { called. However, for now, we could just call the registry last. */ let matchAndRun = ( - r: registry, - fnName: string, - args: array, - env: DistributionOperation.env, + ~registry: registry, + ~fnName: string, + ~args: array, + ~env: DistributionOperation.env, ) => { - let matchToDef = m => Matcher.Registry.matchToDef(r, m) + let matchToDef = m => Matcher.Registry.matchToDef(registry, m) let showNameMatchDefinitions = matches => { let defs = matches @@ -295,7 +295,7 @@ module Registry = { ->E.A2.joinWith("; ") `There are function matches for ${fnName}(), but with different arguments: ${defs}` } - switch Matcher.Registry.findMatches(r, fnName, args) { + switch Matcher.Registry.findMatches(registry, fnName, 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_Core.resi b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.resi new file mode 100644 index 00000000..55c060be --- /dev/null +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.resi @@ -0,0 +1,59 @@ +type expressionValue = ReducerInterface_ExpressionValue.expressionValue + +type rec frType = + | FRTypeNumber + | FRTypeNumeric + | FRTypeDistOrNumber + | FRTypeRecord(frTypeRecord) + | FRTypeArray(array) + | FRTypeOption(frType) +and frTypeRecord = array +and frTypeRecordParam = (string, frType) + +type rec frValue = + | FRValueNumber(float) + | FRValueDist(DistributionTypes.genericDist) + | FRValueOption(option) + | FRValueDistOrNumber(frValueDistOrNumber) + | FRValueRecord(frValueRecord) +and frValueRecord = array +and frValueRecordParam = (string, frValue) +and frValueDistOrNumber = FRValueNumber(float) | FRValueDist(DistributionTypes.genericDist) + +type fnDefinition = { + name: string, + inputs: array, + run: (array, DistributionOperation.env) => result, +} + +type function = { + name: string, + definitions: array, +} + +type registry = array + + +// Note: The function "name" is just used for documentation purposes +module Function: { + type t = function + let make: (~name: string, ~definitions: array) => t +} + +module FnDefinition: { + type t = fnDefinition + let make: ( + ~name: string, + ~inputs: array, + ~run: (array, DistributionOperation.env) => result, + ) => t +} + +module Registry: { + let matchAndRun: ( + ~registry: registry, + ~fnName: string, + ~args: array, + ~env: QuriSquiggleLang.DistributionOperation.env, + ) => option> +} diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res index 799d119c..a9644756 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res @@ -117,4 +117,5 @@ module TwoArgDist = { ~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), ) } + } diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/README.md b/packages/squiggle-lang/src/rescript/FunctionRegistry/README.md new file mode 100644 index 00000000..3bdc387a --- /dev/null +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/README.md @@ -0,0 +1,46 @@ +# Function Registry + +The function registry is a library for organizing function definitions. + +The main interface is fairly constrained. Basically, write functions like the following, and add them to a big array. + +```rescript + Function.make( + ~name="Normal", + ~definitions=[ + FnDefinition.make( + ~name="Normal", + ~definitions=[ + FnDefinition.make(~name="normal", ~inputs=[FRTypeDistOrNumber, FRTypeDistOrNumber], ~run=( + inputs, + env, + ) => + inputs + ->Prepare.ToValueTuple.twoDistOrNumber + ->E.R.bind( + Process.twoDistsOrNumbersToDistUsingSymbolicDist( + ~fn=E.Tuple2.toFnCall(SymbolicDist.Normal.make), + ~env, + ~values=_, + ), + ) + ->E.R2.fmap(Wrappers.evDistribution) + ), + ], + ) + ], + ) +``` + +The Function name is just there for future documentation. The function defintions + +## Key Files + +**FunctionRegistry_Core** +Key types, internal functionality, and a ``Registry`` module with a ``matchAndRun`` function to call function definitions. + +**FunctionRegistry_Library** +A list of all the Functions defined in the Function Registry. + +**FunctionRegistry_Helpers** +A list of helper functions for the FunctionRegistry_Library. \ No newline at end of file diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res index d7aaeee9..c8aeb01c 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res @@ -361,10 +361,10 @@ let genericOutputToReducerValue = (o: DistributionOperation.outputType): result< | GenDistError(err) => Error(REDistributionError(err)) } -let registered = FunctionRegistry_Library.allFunctions +let registry = FunctionRegistry_Library.allFunctions let tryRegistry = ((fnName, args): ExpressionValue.functionCall, env) => { - FunctionRegistry_Core.Registry.matchAndRun(registered, fnName, args, env)->E.O2.fmap( + FunctionRegistry_Core.Registry.matchAndRun(~registry, ~fnName, ~args, ~env)->E.O2.fmap( E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s)), ) }