First attempt at function declaration

This commit is contained in:
Ozzie Gooen 2022-05-23 20:49:10 -04:00
parent c916f12bce
commit 6248818531
5 changed files with 57 additions and 60 deletions

View File

@ -8,9 +8,12 @@ type rec frType =
| FRTypeNumber
| FRTypeNumeric
| FRTypeDistOrNumber
| FRTLambda
| FRTypeRecord(frTypeRecord)
| FRTypeArray(array<frType>)
| FRTypeArray(frType)
| FRTypeOption(frType)
| FRTypeString
| FRTypeVariant(array<string>)
and frTypeRecord = array<frTypeRecordParam>
and frTypeRecordParam = (string, frType)
@ -22,8 +25,12 @@ type rec frValue =
| FRValueNumber(float)
| FRValueDist(DistributionTypes.genericDist)
| FRValueOption(option<frValue>)
| FRValueArray(array<frValue>)
| FRValueDistOrNumber(frValueDistOrNumber)
| FRValueRecord(frValueRecord)
| FRValueLambda(ReducerInterface_ExpressionValue.lambdaValue)
| FRValueString(string)
| FRValueVariant(string)
and frValueRecord = array<frValueRecordParam>
and frValueRecordParam = (string, frValue)
and frValueDistOrNumber = FRValueNumber(float) | FRValueDist(DistributionTypes.genericDist)
@ -52,8 +59,9 @@ module FRType = {
let input = ((name, frType): frTypeRecordParam) => `${name}: ${toString(frType)}`
`record({${r->E.A2.fmap(input)->E.A2.joinWith(", ")}})`
}
| FRTypeArray(r) => `record(${r->E.A2.fmap(toString)->E.A2.joinWith(", ")})`
| FRTypeArray(r) => `record(${toString(r)})`
| FRTypeOption(v) => `option(${toString(v)})`
| FRTLambda => `lambda`
}
let rec matchWithExpressionValue = (t: t, r: expressionValue): option<frValue> =>
@ -66,6 +74,7 @@ module FRType = {
| (FRTypeNumeric, EvNumber(f)) => Some(FRValueNumber(f))
| (FRTypeNumeric, EvDistribution(Symbolic(#Float(f)))) => Some(FRValueNumber(f))
| (FRTypeOption(v), _) => Some(FRValueOption(matchWithExpressionValue(v, r)))
| (FRTLambda, EvLambda(f)) => Some(FRValueLambda(f))
| (FRTypeRecord(recordParams), EvRecord(record)) => {
let getAndMatch = (name, input) =>
E.Dict.get(record, name)->E.O.bind(matchWithExpressionValue(input))
@ -80,6 +89,24 @@ module FRType = {
| _ => None
}
let rec matchReverse = (e: frValue): expressionValue =>
switch(e){
| FRValueNumber(f) => (EvNumber(f))
| FRValueDistOrNumber(FRValueNumber(n)) => EvNumber(n)
| FRValueDistOrNumber(FRValueDist(n)) => EvDistribution(n)
| FRValueDist(dist) => EvDistribution(dist)
| FRValueOption(Some(r)) => matchReverse(r)
| FRValueArray(elements) => EvArray(elements->E.A2.fmap(matchReverse))
| FRValueRecord(frValueRecord) => {
let record = frValueRecord->E.A2.fmap(((name, value)) => (name, matchReverse(value)))->E.Dict.fromArray
EvRecord(record)
}
| FRValueLambda(l) => EvLambda(l)
| FRValueString(string) => EvString(string)
| FRValueVariant(string) => EvString(string)
}
// | FRValueOption(None) => break
let matchWithExpressionValueArray = (inputs: array<t>, args: array<expressionValue>): option<
array<frValue>,
> => {

View File

@ -1,58 +0,0 @@
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

@ -30,6 +30,13 @@ module Prepare = {
}
}
let twoNumbers = (values: ts): result<(float, float), err> => {
switch values {
| [FRValueNumber(a1), FRValueNumber(a2)] => Ok(a1, a2)
| _ => Error(impossibleError)
}
}
let oneDistOrNumber = (values: ts): result<frValueDistOrNumber, err> => {
switch values {
| [FRValueDistOrNumber(a1)] => Ok(a1)

View File

@ -3,7 +3,27 @@ open FunctionRegistry_Helpers
let twoArgs = E.Tuple2.toFnCall
// ~run=(inputs, env) => switch(inputs->FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs){
// | (FRTypeArray(records), FRValueLambdaValue(fn)) => {
// records->E.A.fmap2(r => r->FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs->FunctionRegistry_Helpers.Prepare)
// })
// }
// let variant = FRTypeVariant(["Numeric", "Date"])
let recordType = FRTypeRecord([("min", FRTypeNumber), ("max", FRTypeNumber)])
let registry = [
Function.make(
~name="FnMake",
~definitions=[
FnDefinition.make(
~name="declareFn",
~inputs=[FRTypeRecord([("inputs", FRTypeArray(recordType))])],
~run=(inputs, _) => {
let foo = FunctionRegistry_Core.FRType.matchReverse(inputs->E.A.unsafe_get(0))
foo->Ok
}
),
],
),
Function.make(
~name="Normal",
~definitions=[

View File

@ -864,4 +864,5 @@ module Dict = {
type t<'a> = Js.Dict.t<'a>
let get = Js.Dict.get
let keys = Js.Dict.keys
let fromArray = Js.Dict.fromArray
}