Added simpe README and resi for FunctionRegistry

This commit is contained in:
Ozzie Gooen 2022-05-21 13:52:17 -04:00
parent 3531005a2b
commit c200259c79
5 changed files with 114 additions and 8 deletions

View File

@ -279,12 +279,12 @@ module Registry = {
called. However, for now, we could just call the registry last. called. However, for now, we could just call the registry last.
*/ */
let matchAndRun = ( let matchAndRun = (
r: registry, ~registry: registry,
fnName: string, ~fnName: string,
args: array<expressionValue>, ~args: array<expressionValue>,
env: DistributionOperation.env, ~env: DistributionOperation.env,
) => { ) => {
let matchToDef = m => Matcher.Registry.matchToDef(r, m) let matchToDef = m => Matcher.Registry.matchToDef(registry, m)
let showNameMatchDefinitions = matches => { let showNameMatchDefinitions = matches => {
let defs = let defs =
matches matches
@ -295,7 +295,7 @@ module Registry = {
->E.A2.joinWith("; ") ->E.A2.joinWith("; ")
`There are function matches for ${fnName}(), but with different arguments: ${defs}` `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)) | Matcher.Match.FullMatch(match) => match->matchToDef->E.O2.fmap(FnDefinition.run(_, args, env))
| SameNameDifferentArguments(m) => Some(Error(showNameMatchDefinitions(m))) | SameNameDifferentArguments(m) => Some(Error(showNameMatchDefinitions(m)))
| _ => None | _ => None

View File

@ -0,0 +1,59 @@
type expressionValue = ReducerInterface_ExpressionValue.expressionValue
type rec frType =
| FRTypeNumber
| FRTypeNumeric
| FRTypeDistOrNumber
| FRTypeRecord(frTypeRecord)
| FRTypeArray(array<frType>)
| FRTypeOption(frType)
and frTypeRecord = array<frTypeRecordParam>
and frTypeRecordParam = (string, frType)
type rec frValue =
| FRValueNumber(float)
| FRValueDist(DistributionTypes.genericDist)
| FRValueOption(option<frValue>)
| FRValueDistOrNumber(frValueDistOrNumber)
| FRValueRecord(frValueRecord)
and frValueRecord = array<frValueRecordParam>
and frValueRecordParam = (string, frValue)
and frValueDistOrNumber = FRValueNumber(float) | FRValueDist(DistributionTypes.genericDist)
type fnDefinition = {
name: string,
inputs: array<frType>,
run: (array<frValue>, DistributionOperation.env) => result<expressionValue, string>,
}
type function = {
name: string,
definitions: array<fnDefinition>,
}
type registry = array<function>
// Note: The function "name" is just used for documentation purposes
module Function: {
type t = function
let make: (~name: string, ~definitions: array<fnDefinition>) => t
}
module FnDefinition: {
type t = fnDefinition
let make: (
~name: string,
~inputs: array<frType>,
~run: (array<frValue>, DistributionOperation.env) => result<expressionValue, string>,
) => t
}
module Registry: {
let matchAndRun: (
~registry: registry,
~fnName: string,
~args: array<expressionValue>,
~env: QuriSquiggleLang.DistributionOperation.env,
) => option<result<expressionValue, string>>
}

View File

@ -117,4 +117,5 @@ module TwoArgDist = {
~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), ~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env),
) )
} }
} }

View File

@ -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.

View File

@ -361,10 +361,10 @@ let genericOutputToReducerValue = (o: DistributionOperation.outputType): result<
| GenDistError(err) => Error(REDistributionError(err)) | GenDistError(err) => Error(REDistributionError(err))
} }
let registered = FunctionRegistry_Library.allFunctions let registry = FunctionRegistry_Library.allFunctions
let tryRegistry = ((fnName, args): ExpressionValue.functionCall, env) => { 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)), E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s)),
) )
} }