Made formal Declaration type
This commit is contained in:
parent
9185719641
commit
d1f2f71912
|
@ -3,39 +3,26 @@ open FunctionRegistry_Helpers
|
||||||
|
|
||||||
let twoArgs = E.Tuple2.toFnCall
|
let twoArgs = E.Tuple2.toFnCall
|
||||||
|
|
||||||
module FnDeclaration = {
|
module Declaration = {
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
let frType = FRTypeRecord([
|
let frType = FRTypeRecord([
|
||||||
("fn", FRTypeLambda),
|
("fn", FRTypeLambda),
|
||||||
("inputs", FRTypeArray(FRTypeRecord([("min", FRTypeNumber), ("max", FRTypeNumber)]))),
|
("inputs", FRTypeArray(FRTypeRecord([("min", FRTypeNumber), ("max", FRTypeNumber)]))),
|
||||||
])
|
])
|
||||||
|
|
||||||
let fromExpressionValue = (e: expressionValue) => {
|
let fromExpressionValue = (e: frValue): result<expressionValue, string> => {
|
||||||
let values = FunctionRegistry_Core.FRType.matchWithExpressionValue(frType, e)
|
switch FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs([e]) {
|
||||||
switch values->E.O2.fmap(r =>
|
| Ok([FRValueLambda(lambda), FRValueArray(inputs)]) => {
|
||||||
FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs([r])
|
|
||||||
) {
|
|
||||||
| Some(Ok([FRValueLambda(lambda), FRValueArray(inputs)])) => {
|
|
||||||
open FunctionRegistry_Helpers.Prepare
|
open FunctionRegistry_Helpers.Prepare
|
||||||
let getMinMax = arg =>
|
let getMinMax = arg =>
|
||||||
ToValueArray.Record.toArgs([arg])
|
ToValueArray.Record.toArgs([arg])
|
||||||
->E.R.bind(ToValueTuple.twoNumbers)
|
->E.R.bind(ToValueTuple.twoNumbers)
|
||||||
->E.R2.fmap(((min, max)) => makeRange(min, max))
|
->E.R2.fmap(((min, max)) => Declaration.ContinuousFloatArg.make(min, max))
|
||||||
inputs
|
inputs
|
||||||
->E.A2.fmap(getMinMax)
|
->E.A2.fmap(getMinMax)
|
||||||
->E.A.R.firstErrorOrOpen
|
->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")
|
| _ => Error("Error")
|
||||||
}
|
}
|
||||||
|
@ -46,12 +33,9 @@ let registry = [
|
||||||
Function.make(
|
Function.make(
|
||||||
~name="FnMake",
|
~name="FnMake",
|
||||||
~definitions=[
|
~definitions=[
|
||||||
FnDefinition.make(~name="declareFn", ~inputs=[FnDeclaration.frType], ~run=(inputs, _) => {
|
FnDefinition.make(~name="declareFn", ~inputs=[Declaration.frType], ~run=(inputs, _) =>
|
||||||
let result = inputs->E.A.unsafe_get(0)->FunctionRegistry_Core.FRType.matchReverse->Ok
|
inputs->E.A.unsafe_get(0)->Declaration.fromExpressionValue
|
||||||
let foo = result->E.R2.fmap(FnDeclaration.fromExpressionValue)
|
),
|
||||||
Js.log2("HIHIHI", foo)
|
|
||||||
result
|
|
||||||
}),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
Function.make(
|
Function.make(
|
||||||
|
|
|
@ -22,6 +22,7 @@ type rec expressionValue =
|
||||||
| EvSymbol(string)
|
| EvSymbol(string)
|
||||||
| EvDate(Js.Date.t)
|
| EvDate(Js.Date.t)
|
||||||
| EvTimeDuration(float)
|
| EvTimeDuration(float)
|
||||||
|
| EvDeclaration(lambdaDeclaration)
|
||||||
and record = Js.Dict.t<expressionValue>
|
and record = Js.Dict.t<expressionValue>
|
||||||
and externalBindings = record
|
and externalBindings = record
|
||||||
and lambdaValue = {
|
and lambdaValue = {
|
||||||
|
@ -29,6 +30,7 @@ and lambdaValue = {
|
||||||
context: externalBindings,
|
context: externalBindings,
|
||||||
body: internalCode,
|
body: internalCode,
|
||||||
}
|
}
|
||||||
|
and lambdaDeclaration = Declaration.declaration<lambdaValue>
|
||||||
|
|
||||||
@genType
|
@genType
|
||||||
let defaultExternalBindings: externalBindings = Js.Dict.empty()
|
let defaultExternalBindings: externalBindings = Js.Dict.empty()
|
||||||
|
@ -55,6 +57,7 @@ let rec toString = aValue =>
|
||||||
| EvDistribution(dist) => GenericDist.toString(dist)
|
| EvDistribution(dist) => GenericDist.toString(dist)
|
||||||
| EvDate(date) => DateTime.Date.toString(date)
|
| EvDate(date) => DateTime.Date.toString(date)
|
||||||
| EvTimeDuration(t) => DateTime.Duration.toString(t)
|
| EvTimeDuration(t) => DateTime.Duration.toString(t)
|
||||||
|
| EvDeclaration(t) => "Declaration"
|
||||||
}
|
}
|
||||||
and toStringRecord = aRecord => {
|
and toStringRecord = aRecord => {
|
||||||
let pairs =
|
let pairs =
|
||||||
|
@ -79,6 +82,7 @@ let toStringWithType = aValue =>
|
||||||
| EvSymbol(_) => `Symbol::${toString(aValue)}`
|
| EvSymbol(_) => `Symbol::${toString(aValue)}`
|
||||||
| EvDate(_) => `Date::${toString(aValue)}`
|
| EvDate(_) => `Date::${toString(aValue)}`
|
||||||
| EvTimeDuration(_) => `Date::${toString(aValue)}`
|
| EvTimeDuration(_) => `Date::${toString(aValue)}`
|
||||||
|
| EvDeclaration(_) => `Declaration::${toString(aValue)}`
|
||||||
}
|
}
|
||||||
|
|
||||||
let argsToString = (args: array<expressionValue>): string => {
|
let argsToString = (args: array<expressionValue>): string => {
|
||||||
|
|
33
packages/squiggle-lang/src/rescript/Utility/Declaration.res
Normal file
33
packages/squiggle-lang/src/rescript/Utility/Declaration.res
Normal 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})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user