Add and use extra pathway for FunctionRegistry to use internalValues directly

This commit is contained in:
Ozzie Gooen 2022-07-14 14:53:53 -07:00
parent cae59cb84b
commit feb198d205
2 changed files with 89 additions and 79 deletions

View File

@ -44,7 +44,7 @@ type fnDefinition = {
requiresNamespace: bool, requiresNamespace: bool,
name: string, name: string,
inputs: array<frType>, inputs: array<frType>,
run: (array<frValue>, GenericDist.env) => result<internalExpressionValue, string>, run: (array<internalExpressionValue>, array<frValue>, GenericDist.env)=> result<internalExpressionValue, string>
} }
type function = { type function = {
@ -327,7 +327,7 @@ module FnDefinition = {
let run = (t: t, args: array<internalExpressionValue>, env: GenericDist.env) => { let run = (t: t, args: array<internalExpressionValue>, env: GenericDist.env) => {
let argValues = FRType.matchWithExpressionValueArray(t.inputs, args) let argValues = FRType.matchWithExpressionValueArray(t.inputs, args)
switch argValues { switch argValues {
| Some(values) => t.run(values, env) | Some(values) => t.run(args, values, env)
| None => Error("Incorrect Types") | None => Error("Incorrect Types")
} }
} }

View File

@ -61,7 +61,7 @@ module PointSet = {
~requiresNamespace, ~requiresNamespace,
~name="makeContinuous", ~name="makeContinuous",
~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], ~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))),
(), (),
), ),
], ],
@ -75,7 +75,7 @@ module PointSet = {
~requiresNamespace, ~requiresNamespace,
~name="makeDiscrete", ~name="makeDiscrete",
~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], ~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))),
(), (),
), ),
], ],
@ -95,7 +95,7 @@ module Functionn = {
~requiresNamespace=true, ~requiresNamespace=true,
~name="declare", ~name="declare",
~inputs=[Declaration.frType], ~inputs=[Declaration.frType],
~run=(inputs, _) => { ~run=(_, inputs, _) => {
inputs->getOrError(0)->E.R.bind(Declaration.fromExpressionValue) inputs->getOrError(0)->E.R.bind(Declaration.fromExpressionValue)
}, },
(), (),
@ -120,7 +120,7 @@ module DistributionCreation = {
~requiresNamespace=false, ~requiresNamespace=false,
~name, ~name,
~inputs=[FRTypeDistOrNumber, FRTypeDistOrNumber], ~inputs=[FRTypeDistOrNumber, FRTypeDistOrNumber],
~run=(inputs, env) => inputs->Prepare.ToValueTuple.twoDistOrNumber->process(~fn, ~env), ~run=(_, inputs, env) => inputs->Prepare.ToValueTuple.twoDistOrNumber->process(~fn, ~env),
(), (),
) )
} }
@ -131,7 +131,7 @@ module DistributionCreation = {
~requiresNamespace=false, ~requiresNamespace=false,
~name, ~name,
~inputs=[FRTypeRecord([("p5", FRTypeDistOrNumber), ("p95", FRTypeDistOrNumber)])], ~inputs=[FRTypeRecord([("p5", FRTypeDistOrNumber), ("p95", FRTypeDistOrNumber)])],
~run=(inputs, env) => ~run=(_, inputs, env) =>
inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env),
(), (),
) )
@ -143,7 +143,7 @@ module DistributionCreation = {
~nameSpace, ~nameSpace,
~requiresNamespace=false, ~requiresNamespace=false,
~inputs=[FRTypeRecord([("mean", FRTypeDistOrNumber), ("stdev", FRTypeDistOrNumber)])], ~inputs=[FRTypeRecord([("mean", FRTypeDistOrNumber), ("stdev", FRTypeDistOrNumber)])],
~run=(inputs, env) => ~run=(_, inputs, env) =>
inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env), inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env),
(), (),
) )
@ -162,7 +162,7 @@ module DistributionCreation = {
~requiresNamespace=false, ~requiresNamespace=false,
~name, ~name,
~inputs=[FRTypeDistOrNumber], ~inputs=[FRTypeDistOrNumber],
~run=(inputs, env) => inputs->Prepare.ToValueTuple.oneDistOrNumber->process(~fn, ~env), ~run=(_, inputs, env) => inputs->Prepare.ToValueTuple.oneDistOrNumber->process(~fn, ~env),
(), (),
) )
} }
@ -281,7 +281,7 @@ let registryStart = [
FnDefinition.make( FnDefinition.make(
~name="toContinuousPointSet", ~name="toContinuousPointSet",
~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], ~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))),
(), (),
), ),
], ],
@ -300,7 +300,7 @@ let registryStart = [
FnDefinition.make( FnDefinition.make(
~name="toDiscretePointSet", ~name="toDiscretePointSet",
~inputs=[FRTypeArray(FRTypeRecord([("x", FRTypeNumeric), ("y", FRTypeNumeric)]))], ~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))),
(), (),
), ),
], ],
@ -320,7 +320,7 @@ let registryStart = [
FnDefinition.make( FnDefinition.make(
~name="declareFn", ~name="declareFn",
~inputs=[Declaration.frType], ~inputs=[Declaration.frType],
~run=(inputs, _) => { ~run=(_, inputs, _) => {
inputs->E.A.unsafe_get(0)->Declaration.fromExpressionValue inputs->E.A.unsafe_get(0)->Declaration.fromExpressionValue
}, },
(), (),
@ -342,7 +342,7 @@ module Number = {
~requiresNamespace, ~requiresNamespace,
~name, ~name,
~inputs=[FRTypeNumber], ~inputs=[FRTypeNumber],
~run=(inputs, _) => { ~run=(_, inputs, _) => {
inputs inputs
->getOrError(0) ->getOrError(0)
->E.R.bind(Prepare.oneNumber) ->E.R.bind(Prepare.oneNumber)
@ -360,7 +360,7 @@ module Number = {
~requiresNamespace=false, ~requiresNamespace=false,
~name, ~name,
~inputs=[FRTypeArray(FRTypeNumber)], ~inputs=[FRTypeArray(FRTypeNumber)],
~run=(inputs, _) => ~run=(_, inputs, _) =>
Prepare.ToTypedArray.numbers(inputs) Prepare.ToTypedArray.numbers(inputs)
->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r)) ->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r))
->E.R.bind(fn), ->E.R.bind(fn),
@ -371,7 +371,7 @@ module Number = {
FnDefinition.make( FnDefinition.make(
~name, ~name,
~inputs=[FRTypeArray(FRTypeAny)], ~inputs=[FRTypeArray(FRTypeAny)],
~run=(inputs, _) => ~run=(_, inputs, _) =>
Prepare.ToTypedArray.numbers(inputs) Prepare.ToTypedArray.numbers(inputs)
->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r)) ->E.R.bind(r => E.A.length(r) === 0 ? Error("List is empty") : Ok(r))
->E.R.bind(fn), ->E.R.bind(fn),
@ -492,6 +492,32 @@ module Number = {
module Dict = { module Dict = {
let nameSpace = Some("Dict") let nameSpace = Some("Dict")
module Internals = {
type t = ReducerInterface_InternalExpressionValue.map
let keys = (a: t): internalExpressionValue => IEvArray(
Belt.Map.String.keysToArray(a)->E.A2.fmap(Wrappers.evString),
)
let values = (a: t): internalExpressionValue => IEvArray(Belt.Map.String.valuesToArray(a))
let toList = (a: t): internalExpressionValue =>
Belt.Map.String.toArray(a)
->E.A2.fmap(((key, value)) => Wrappers.evArray([IEvString(key), value]))
->Wrappers.evArray
let merge = (a: t, b: t): internalExpressionValue => IEvRecord(
Belt.Map.String.merge(a, b, (_, _, c) => c),
)
//Belt.Map.String has a function for mergeMany, but I couldn't understand how to use it yet.
let mergeMany = (a: array<t>): internalExpressionValue => {
let mergedValues =
a->E.A2.fmap(Belt.Map.String.toArray)->Belt.Array.concatMany->Belt.Map.String.fromArray
IEvRecord(mergedValues)
}
}
let library = [ let library = [
Function.make( Function.make(
~name="merge", ~name="merge",
@ -501,15 +527,9 @@ module Dict = {
~requiresNamespace=true, ~requiresNamespace=true,
~name="merge", ~name="merge",
~inputs=[FRTypeDict(FRTypeAny), FRTypeDict(FRTypeAny)], ~inputs=[FRTypeDict(FRTypeAny), FRTypeDict(FRTypeAny)],
~run=(inputs, _) => { ~run=(inputs, _, _) => {
switch inputs { switch inputs {
| [FRValueDict(d1), FRValueDict(d2)] => { | [IEvRecord(d1), IEvRecord(d2)] => Internals.merge(d1, d2)->Ok
let newDict =
E.Dict.concat(d1, d2) |> Js.Dict.map((. r) =>
FunctionRegistry_Core.FRType.matchReverse(r)
)
newDict->Js.Dict.entries->Belt.Map.String.fromArray->Wrappers.evRecord->Ok
}
| _ => Error(impossibleError) | _ => Error(impossibleError)
} }
}, },
@ -518,7 +538,7 @@ module Dict = {
], ],
(), (),
), ),
//TODO: Make sure that two functions can't have the same name. This causes chaos elsewhere. //TODO: Change to use new mergeMany() function.
Function.make( Function.make(
~name="mergeMany", ~name="mergeMany",
~definitions=[ ~definitions=[
@ -527,7 +547,7 @@ module Dict = {
~requiresNamespace=true, ~requiresNamespace=true,
~name="mergeMany", ~name="mergeMany",
~inputs=[FRTypeArray(FRTypeDict(FRTypeAny))], ~inputs=[FRTypeArray(FRTypeDict(FRTypeAny))],
~run=(inputs, _) => ~run=(_, inputs, _) =>
inputs inputs
->Prepare.ToTypedArray.dicts ->Prepare.ToTypedArray.dicts
->E.R2.fmap(E.Dict.concatMany) ->E.R2.fmap(E.Dict.concatMany)
@ -547,10 +567,9 @@ module Dict = {
~requiresNamespace=true, ~requiresNamespace=true,
~name="keys", ~name="keys",
~inputs=[FRTypeDict(FRTypeAny)], ~inputs=[FRTypeDict(FRTypeAny)],
~run=(inputs, _) => ~run=(inputs, _, _) =>
switch inputs { switch inputs {
| [FRValueDict(d1)] => | [IEvRecord(d1)] => Internals.keys(d1)->Ok
Js.Dict.keys(d1)->E.A2.fmap(Wrappers.evString)->Wrappers.evArray->Ok
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),
@ -566,13 +585,9 @@ module Dict = {
~requiresNamespace=true, ~requiresNamespace=true,
~name="values", ~name="values",
~inputs=[FRTypeDict(FRTypeAny)], ~inputs=[FRTypeDict(FRTypeAny)],
~run=(inputs, _) => ~run=(inputs, _, _) =>
switch inputs { switch inputs {
| [FRValueDict(d1)] => | [IEvRecord(d1)] => Internals.values(d1)->Ok
Js.Dict.values(d1)
->E.A2.fmap(FunctionRegistry_Core.FRType.matchReverse)
->Wrappers.evArray
->Ok
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),
@ -588,19 +603,9 @@ module Dict = {
~requiresNamespace=true, ~requiresNamespace=true,
~name="toList", ~name="toList",
~inputs=[FRTypeDict(FRTypeAny)], ~inputs=[FRTypeDict(FRTypeAny)],
~run=(inputs, _) => ~run=(inputs, _, _) =>
switch inputs { switch inputs {
| [FRValueDict(dict)] => | [IEvRecord(dict)] => dict->Internals.toList->Ok
dict
->Js.Dict.entries
->E.A2.fmap(((key, value)) =>
Wrappers.evArray([
Wrappers.evString(key),
FunctionRegistry_Core.FRType.matchReverse(value),
])
)
->Wrappers.evArray
->Ok
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),
@ -616,7 +621,7 @@ module Dict = {
~requiresNamespace=true, ~requiresNamespace=true,
~name="fromList", ~name="fromList",
~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))], ~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))],
~run=(inputs, _) => { ~run=(_, inputs, _) => {
let convertInternalItems = items => let convertInternalItems = items =>
items items
->E.A2.fmap(item => { ->E.A2.fmap(item => {
@ -644,6 +649,29 @@ module List = {
let nameSpace = Some("List") let nameSpace = Some("List")
let requiresNamespace = true let requiresNamespace = true
module Internals = {
let makeFromNumber = (
n: float,
value: internalExpressionValue,
): internalExpressionValue => IEvArray(Belt.Array.make(E.Float.toInt(n), value))
let upTo = (low: float, high: float): internalExpressionValue => IEvArray(
E.A.Floats.range(low, high, (high -. low +. 1.0)->E.Float.toInt)->E.A2.fmap(
Wrappers.evNumber,
),
)
let first = (v: array<internalExpressionValue>): result<internalExpressionValue, string> =>
v->E.A.first |> E.O.toResult("No first element")
let last = (v: array<internalExpressionValue>): result<internalExpressionValue, string> =>
v->E.A.last |> E.O.toResult("No last element")
let reverse = (array: array<internalExpressionValue>): internalExpressionValue => IEvArray(
Belt.Array.reverse(array),
)
}
let library = [ let library = [
Function.make( Function.make(
~name="List.make", ~name="List.make",
@ -654,13 +682,9 @@ module List = {
~requiresNamespace, ~requiresNamespace,
~name="make", ~name="make",
~inputs=[FRTypeNumber, FRTypeAny], ~inputs=[FRTypeNumber, FRTypeAny],
~run=(inputs, _) => { ~run=(inputs, _, _) => {
switch inputs { switch inputs {
| [FRValueNumber(number), value] => | [IEvNumber(number), value] => Internals.makeFromNumber(number, value)->Ok
Belt.Array.make(E.Float.toInt(number), value)
->E.A2.fmap(FunctionRegistry_Core.FRType.matchReverse)
->Wrappers.evArray
->Ok
| _ => Error(impossibleError) | _ => Error(impossibleError)
} }
}, },
@ -677,14 +701,10 @@ module List = {
~requiresNamespace, ~requiresNamespace,
~name="upTo", ~name="upTo",
~inputs=[FRTypeNumber, FRTypeNumber], ~inputs=[FRTypeNumber, FRTypeNumber],
~run=(inputs, _) => ~run=(_, inputs, _) =>
inputs inputs
->Prepare.ToValueTuple.twoNumbers ->Prepare.ToValueTuple.twoNumbers
->E.R2.fmap(((low, high)) => ->E.R2.fmap(((low, high)) => Internals.upTo(low, high)),
E.A.Floats.range(low, high, (high -. low +. 1.0)->E.Float.toInt)
->E.A2.fmap(Wrappers.evNumber)
->Wrappers.evArray
),
(), (),
), ),
], ],
@ -698,12 +718,9 @@ module List = {
~requiresNamespace, ~requiresNamespace,
~name="first", ~name="first",
~inputs=[FRTypeArray(FRTypeAny)], ~inputs=[FRTypeArray(FRTypeAny)],
~run=(inputs, _) => ~run=(inputs, _, _) =>
switch inputs { switch inputs {
| [FRValueArray(array)] => | [IEvArray(array)] => Internals.first(array)
E.A.first(array)
|> E.O.toResult("No first element")
|> E.R.fmap(FunctionRegistry_Core.FRType.matchReverse)
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),
@ -719,12 +736,9 @@ module List = {
~requiresNamespace=false, ~requiresNamespace=false,
~name="last", ~name="last",
~inputs=[FRTypeArray(FRTypeAny)], ~inputs=[FRTypeArray(FRTypeAny)],
~run=(inputs, _) => ~run=(inputs, _, _) =>
switch inputs { switch inputs {
| [FRValueArray(array)] => | [IEvArray(array)] => Internals.last(array)
E.A.last(array)
|> E.O.toResult("No first element")
|> E.R.fmap(FunctionRegistry_Core.FRType.matchReverse)
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),
@ -738,15 +752,11 @@ module List = {
FnDefinition.make( FnDefinition.make(
~nameSpace, ~nameSpace,
~requiresNamespace=false, ~requiresNamespace=false,
~name="last", ~name="reverse",
~inputs=[FRTypeArray(FRTypeAny)], ~inputs=[FRTypeArray(FRTypeAny)],
~run=(inputs, _) => ~run=(inputs, _, _) =>
switch inputs { switch inputs {
| [FRValueArray(array)] => | [IEvArray(array)] => Internals.reverse(array)->Ok
Belt.Array.reverse(array)
->E.A2.fmap(FunctionRegistry_Core.FRType.matchReverse)
->Wrappers.evArray
->Ok
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),
@ -782,7 +792,7 @@ module Scoring = {
("prior", FRTypeDist), ("prior", FRTypeDist),
]), ]),
], ],
~run=(inputs, env) => { ~run=(_, inputs, env) => {
switch FunctionRegistry_Helpers.Prepare.ToValueArray.Record.threeArgs(inputs) { switch FunctionRegistry_Helpers.Prepare.ToValueArray.Record.threeArgs(inputs) {
| Ok([ | Ok([
FRValueDist(estimate), FRValueDist(estimate),
@ -807,7 +817,7 @@ module Scoring = {
~nameSpace, ~nameSpace,
~requiresNamespace, ~requiresNamespace,
~inputs=[FRTypeRecord([("estimate", FRTypeDist), ("answer", FRTypeDistOrNumber)])], ~inputs=[FRTypeRecord([("estimate", FRTypeDist), ("answer", FRTypeDistOrNumber)])],
~run=(inputs, env) => { ~run=(_, inputs, env) => {
switch FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs(inputs) { switch FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs(inputs) {
| Ok([FRValueDist(estimate), FRValueDistOrNumber(FRValueDist(d))]) => | Ok([FRValueDist(estimate), FRValueDistOrNumber(FRValueDist(d))]) =>
runScoring(estimate, Score_Dist(d), None, env) runScoring(estimate, Score_Dist(d), None, env)
@ -830,7 +840,7 @@ module Scoring = {
~nameSpace, ~nameSpace,
~requiresNamespace, ~requiresNamespace,
~inputs=[FRTypeDist, FRTypeDist], ~inputs=[FRTypeDist, FRTypeDist],
~run=(inputs, env) => { ~run=(_, inputs, env) => {
switch inputs { switch inputs {
| [FRValueDist(estimate), FRValueDist(d)] => | [FRValueDist(estimate), FRValueDist(d)] =>
runScoring(estimate, Score_Dist(d), None, env) runScoring(estimate, Score_Dist(d), None, env)