From 5e8b46b7df7d7d5e724f44a263260308457e0306 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 16 Jul 2022 19:06:48 -0700 Subject: [PATCH] Cleaning up Function Registry to Reducer interface --- .../__tests__/FunctionRegistry_test.res | 3 +- .../FunctionRegistry_Core.res | 12 ++++-- .../FunctionRegistry_Library.res | 2 + .../ReducerInterface_ExternalLibrary.res | 40 +------------------ .../ReducerInterface_StdLib.res | 5 ++- .../SquiggleLibrary/SquiggleLibrary_Math.res | 4 +- 6 files changed, 18 insertions(+), 48 deletions(-) diff --git a/packages/squiggle-lang/__tests__/FunctionRegistry_test.res b/packages/squiggle-lang/__tests__/FunctionRegistry_test.res index 2909f7d1..376f8c86 100644 --- a/packages/squiggle-lang/__tests__/FunctionRegistry_test.res +++ b/packages/squiggle-lang/__tests__/FunctionRegistry_test.res @@ -50,8 +50,7 @@ module FooImplementation = { let library = [fn] } -let makeBindings = (previousBindings: Bindings.t): Bindings.t => - previousBindings->FunctionRegistry_Core.Registry.makeModules(FooImplementation.library) +let makeBindings = FunctionRegistry_Core.Registry.makeBindings(_, FooImplementation.library) let stdLibWithFoo = Bindings.emptyBindings->makeBindings diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res index 887ac3f7..1af459f9 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res @@ -429,7 +429,7 @@ module NameSpace = { module Registry = { let toJson = (r: registry) => r->E.A2.fmap(Function.toJson) - let exportedSubset = (r: registry): registry => r |> E.A.filter(r => !r.requiresNamespace) + let _exportedSubset = (r: registry): registry => r |> E.A.filter(r => !r.requiresNamespace) let definitionsWithFunctions = (r: registry) => r->E.A2.fmap(fn => fn.definitions->E.A2.fmap(def => (def, fn)))->E.A.concatMany @@ -439,7 +439,7 @@ 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 = ( + let _matchAndRun = ( ~registry: registry, ~fnName: string, ~args: array, @@ -463,9 +463,15 @@ module Registry = { } } + let dispatch = (registry, (fnName, args): ReducerInterface_InternalExpressionValue.functionCall, env) => { + _matchAndRun(~registry=_exportedSubset(registry), ~fnName, ~args, ~env)->E.O2.fmap( + E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s)), + ) + } + let allNamespaces = (t: registry) => t->E.A2.fmap(r => r.nameSpace)->E.A.uniq - let makeModules = (prevBindings: Reducer_Module.t, t: registry): Reducer_Module.t => { + let makeBindings = (prevBindings: Reducer_Module.t, t: registry): Reducer_Module.t => { let nameSpaces = allNamespaces(t) let nameSpaceBindings = nameSpaces->E.A2.fmap(nameSpace => { let namespaceModule: NameSpace.t = { diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res index 3c74398a..6c8d1bb0 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res @@ -7,3 +7,5 @@ let registry = Belt.Array.concatMany([ FR_Pointset.library, FR_Scoring.library, ]) + +let dispatch = FunctionRegistry_Core.Registry.dispatch(registry) \ No newline at end of file diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res index c9b72b69..312dfd38 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res @@ -1,31 +1,9 @@ module InternalExpressionValue = ReducerInterface_InternalExpressionValue - type internalExpressionValue = InternalExpressionValue.t -// module Sample = { -// // In real life real libraries should be somewhere else -// /* -// For an example of mapping polymorphic custom functions. To be deleted after real integration -// */ -// let customAdd = (a: float, b: float): float => {a +. b} -// } - /* Map external calls of Reducer */ - -// I expect that it's important to build this first, so it doesn't get recalculated for each tryRegistry() call. -let registry = FunctionRegistry_Library.registry - -let tryRegistry = ((fnName, args): InternalExpressionValue.functionCall, env) => { - FunctionRegistry_Core.Registry.matchAndRun( - ~registry=FunctionRegistry_Core.Registry.exportedSubset(registry), - ~fnName, - ~args, - ~env, - )->E.O2.fmap(E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s))) -} - let dispatch = (call: InternalExpressionValue.functionCall, environment, chain): result< internalExpressionValue, 'e, @@ -35,22 +13,6 @@ let dispatch = (call: InternalExpressionValue.functionCall, environment, chain): () => ReducerInterface_Date.dispatch(call, environment), () => ReducerInterface_Duration.dispatch(call, environment), () => ReducerInterface_Number.dispatch(call, environment), - () => tryRegistry(call, environment), + () => FunctionRegistry_Library.dispatch(call, environment), ])->E.O2.default(chain(call, environment)) } -/* -If your dispatch is too big you can divide it into smaller dispatches and pass the call so that it gets called finally. - -The final chain(call) invokes the builtin default functions of the interpreter. - -Via chain(call), all MathJs operators and functions are available for string, number , boolean, array and record - .e.g + - / * > >= < <= == /= not and or sin cos log ln concat, etc. - -// See https://mathjs.org/docs/expressions/syntax.html -// See https://mathjs.org/docs/reference/functions.html - -Remember from the users point of view, there are no different modules: -// "doSth( constructorType1 )" -// "doSth( constructorType2 )" -doSth gets dispatched to the correct module because of the type signature. You get function and operator abstraction for free. You don't need to combine different implementations into one type. That would be duplicating the repsonsibility of the dispatcher. -*/ diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res index c08b7cb1..ae9d4c55 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res @@ -1,6 +1,9 @@ module Module = Reducer_Module -let internalStdLib = Module.emptyModule->SquiggleLibrary_Math.makeBindings +let internalStdLib = + Module.emptyModule + ->SquiggleLibrary_Math.makeBindings + ->FunctionRegistry_Core.Registry.makeBindings(FunctionRegistry_Library.registry) @genType let externalStdLib = internalStdLib->Module.toTypeScriptBindings diff --git a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res index b7b925ed..c96a8f82 100644 --- a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res +++ b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res @@ -21,6 +21,4 @@ let mathBindings: Bindings.t = //TODO: This should be in a different place. let makeBindings = (previousBindings: Bindings.t): Bindings.t => - previousBindings - ->Bindings.defineModule("Math", mathBindings) - ->FunctionRegistry_Core.Registry.makeModules(FunctionRegistry_Library.registry) + previousBindings->Bindings.defineModule("Math", mathBindings) \ No newline at end of file