From 62488185312c48e433cea495f0990a4cae6a63c9 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Mon, 23 May 2022 20:49:10 -0400 Subject: [PATCH] First attempt at function declaration --- .../FunctionRegistry_Core.res | 31 +++++++++- .../FunctionRegistry_Core.resi | 58 ------------------- .../FunctionRegistry_Helpers.res | 7 +++ .../FunctionRegistry_Library.res | 20 +++++++ .../squiggle-lang/src/rescript/Utility/E.res | 1 + 5 files changed, 57 insertions(+), 60 deletions(-) delete mode 100644 packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.resi diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res index 99ecc78f..a88ce239 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res @@ -8,9 +8,12 @@ type rec frType = | FRTypeNumber | FRTypeNumeric | FRTypeDistOrNumber + | FRTLambda | FRTypeRecord(frTypeRecord) - | FRTypeArray(array) + | FRTypeArray(frType) | FRTypeOption(frType) + | FRTypeString + | FRTypeVariant(array) and frTypeRecord = array and frTypeRecordParam = (string, frType) @@ -22,8 +25,12 @@ type rec frValue = | FRValueNumber(float) | FRValueDist(DistributionTypes.genericDist) | FRValueOption(option) + | FRValueArray(array) | FRValueDistOrNumber(frValueDistOrNumber) | FRValueRecord(frValueRecord) + | FRValueLambda(ReducerInterface_ExpressionValue.lambdaValue) + | FRValueString(string) + | FRValueVariant(string) and frValueRecord = array 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 => @@ -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, args: array): option< array, > => { diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.resi b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.resi deleted file mode 100644 index 5ca8c708..00000000 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.resi +++ /dev/null @@ -1,58 +0,0 @@ -type expressionValue = ReducerInterface_ExpressionValue.expressionValue - -type rec frType = - | FRTypeNumber - | FRTypeNumeric - | FRTypeDistOrNumber - | FRTypeRecord(frTypeRecord) - | FRTypeArray(array) - | FRTypeOption(frType) -and frTypeRecord = array -and frTypeRecordParam = (string, frType) - -type rec frValue = - | FRValueNumber(float) - | FRValueDist(DistributionTypes.genericDist) - | FRValueOption(option) - | FRValueDistOrNumber(frValueDistOrNumber) - | FRValueRecord(frValueRecord) -and frValueRecord = array -and frValueRecordParam = (string, frValue) -and frValueDistOrNumber = FRValueNumber(float) | FRValueDist(DistributionTypes.genericDist) - -type fnDefinition = { - name: string, - inputs: array, - run: (array, DistributionOperation.env) => result, -} - -type function = { - name: string, - definitions: array, -} - -type registry = array - -// Note: The function "name" is just used for documentation purposes -module Function: { - type t = function - let make: (~name: string, ~definitions: array) => t -} - -module FnDefinition: { - type t = fnDefinition - let make: ( - ~name: string, - ~inputs: array, - ~run: (array, DistributionOperation.env) => result, - ) => t -} - -module Registry: { - let matchAndRun: ( - ~registry: registry, - ~fnName: string, - ~args: array, - ~env: QuriSquiggleLang.DistributionOperation.env, - ) => option> -} diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res index 118a15d2..2d21d07a 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res @@ -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 => { switch values { | [FRValueDistOrNumber(a1)] => Ok(a1) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res index 43be7118..5a12e496 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res @@ -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=[ diff --git a/packages/squiggle-lang/src/rescript/Utility/E.res b/packages/squiggle-lang/src/rescript/Utility/E.res index 3357f4f4..35d9fe4d 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E.res +++ b/packages/squiggle-lang/src/rescript/Utility/E.res @@ -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 }