Made formal Declaration type

This commit is contained in:
Ozzie Gooen 2022-05-24 17:02:27 -04:00
parent 9185719641
commit d1f2f71912
3 changed files with 49 additions and 28 deletions

View File

@ -3,39 +3,26 @@ open FunctionRegistry_Helpers
let twoArgs = E.Tuple2.toFnCall
module FnDeclaration = {
type range = {min: float, max: float}
let makeRange = (min, max) => {min: min, max: max}
type t = {
fn: ReducerInterface_ExpressionValue.lambdaValue,
args: array<range>,
}
let validate = (def: t) => {
let {parameters, _} = def.fn
E.A.length(parameters) == E.A.length(def.args)
}
module Declaration = {
let frType = FRTypeRecord([
("fn", FRTypeLambda),
("inputs", FRTypeArray(FRTypeRecord([("min", FRTypeNumber), ("max", FRTypeNumber)]))),
])
let fromExpressionValue = (e: expressionValue) => {
let values = FunctionRegistry_Core.FRType.matchWithExpressionValue(frType, e)
switch values->E.O2.fmap(r =>
FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs([r])
) {
| Some(Ok([FRValueLambda(lambda), FRValueArray(inputs)])) => {
let fromExpressionValue = (e: frValue): result<expressionValue, string> => {
switch FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs([e]) {
| Ok([FRValueLambda(lambda), FRValueArray(inputs)]) => {
open FunctionRegistry_Helpers.Prepare
let getMinMax = arg =>
ToValueArray.Record.toArgs([arg])
->E.R.bind(ToValueTuple.twoNumbers)
->E.R2.fmap(((min, max)) => makeRange(min, max))
->E.R2.fmap(((min, max)) => Declaration.ContinuousFloatArg.make(min, max))
inputs
->E.A2.fmap(getMinMax)
->E.A.R.firstErrorOrOpen
->E.R2.fmap(args => {fn: lambda, args: args})
->E.R2.fmap(args => ReducerInterface_ExpressionValue.EvDeclaration(
Declaration.ContinuousDeclaration.make(lambda, args),
))
}
| _ => Error("Error")
}
@ -46,12 +33,9 @@ let registry = [
Function.make(
~name="FnMake",
~definitions=[
FnDefinition.make(~name="declareFn", ~inputs=[FnDeclaration.frType], ~run=(inputs, _) => {
let result = inputs->E.A.unsafe_get(0)->FunctionRegistry_Core.FRType.matchReverse->Ok
let foo = result->E.R2.fmap(FnDeclaration.fromExpressionValue)
Js.log2("HIHIHI", foo)
result
}),
FnDefinition.make(~name="declareFn", ~inputs=[Declaration.frType], ~run=(inputs, _) =>
inputs->E.A.unsafe_get(0)->Declaration.fromExpressionValue
),
],
),
Function.make(

View File

@ -22,6 +22,7 @@ type rec expressionValue =
| EvSymbol(string)
| EvDate(Js.Date.t)
| EvTimeDuration(float)
| EvDeclaration(lambdaDeclaration)
and record = Js.Dict.t<expressionValue>
and externalBindings = record
and lambdaValue = {
@ -29,6 +30,7 @@ and lambdaValue = {
context: externalBindings,
body: internalCode,
}
and lambdaDeclaration = Declaration.declaration<lambdaValue>
@genType
let defaultExternalBindings: externalBindings = Js.Dict.empty()
@ -55,6 +57,7 @@ let rec toString = aValue =>
| EvDistribution(dist) => GenericDist.toString(dist)
| EvDate(date) => DateTime.Date.toString(date)
| EvTimeDuration(t) => DateTime.Duration.toString(t)
| EvDeclaration(t) => "Declaration"
}
and toStringRecord = aRecord => {
let pairs =
@ -79,6 +82,7 @@ let toStringWithType = aValue =>
| EvSymbol(_) => `Symbol::${toString(aValue)}`
| EvDate(_) => `Date::${toString(aValue)}`
| EvTimeDuration(_) => `Date::${toString(aValue)}`
| EvDeclaration(_) => `Declaration::${toString(aValue)}`
}
let argsToString = (args: array<expressionValue>): string => {

View File

@ -0,0 +1,33 @@
@genType
type continuousArg = Float({min: float, max: float}) | Time({min: Js.Date.t, max: Js.Date.t})
@genType
type continuousDeclaration<'a> = {fn: 'a, args: array<continuousArg>}
@genType
type relativeComparisonDeclaration<'a> = {fn: 'a, options: array<string>}
@genType
type declaration<'a> =
Continuous(continuousDeclaration<'a>) | RelativeComparison(relativeComparisonDeclaration<'a>)
module ContinuousFloatArg = {
let make = (min: float, max: float): continuousArg => {
Float({min: min, max: max})
}
}
module ContinuousTimeArg = {
let make = (min: Js.Date.t, max: Js.Date.t): continuousArg => {
Time({min: min, max: max})
}
}
module ContinuousDeclaration = {
let make = (fn: 'a, args: array<continuousArg>): declaration<'a> => {
Continuous({fn: fn, args: args})
}
}
module RelativeComparisonDeclaration = {
let make = (fn: 'a, options: array<string>): declaration<'a> => {
RelativeComparison({fn: fn, options: options})
}
}