From cfcf3cb144ce532754e9c2063bc7b860d3f3992f Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 9 Jun 2022 14:30:17 +0200 Subject: [PATCH 01/12] module grammar type correction --- .../Reducer_Peggy/Reducer_Peggy_GeneratedParser.peggy | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_GeneratedParser.peggy b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_GeneratedParser.peggy index 897cf536..8ed0cf52 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_GeneratedParser.peggy +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_GeneratedParser.peggy @@ -64,6 +64,7 @@ return {type: 'KeyValue', key: key, value: value}} function nodeLambda(args, body) {return {type: 'Lambda', args: args, body: body}} function nodeLetStatment(variable, value) {return {type: 'LetStatement', variable: variable, value: value}} + function nodeModuleIdentifier(value) {return {type: 'ModuleIdentifier', value: value}} function nodeString(value) {return {type: 'String', value: value}} function nodeTernary(condition, trueExpression, falseExpression) {return {type: 'Ternary', condition: condition, trueExpression: trueExpression, falseExpression: falseExpression}} @@ -256,11 +257,11 @@ basicLiteral dollarIdentifierWithModule 'identifier' = head:moduleIdentifier - tail:('.' _nl @moduleIdentifier)* '.' _nl - final:dollarIdentifier + tail:('.' _nl @$moduleIdentifier)* '.' _nl + final:$dollarIdentifier { tail.push(final); return tail.reduce(function(result, element) { - return makeFunctionCall(postOperatorToFunction['[]'], [result, element]) + return makeFunctionCall(postOperatorToFunction['[]'], [result, nodeString(element)]) }, head)} identifier 'identifier' @@ -273,7 +274,7 @@ dollarIdentifier '$identifier' = ([\$_a-z]+[\$_a-z0-9]i*) {return nodeIdentifier(text())} moduleIdentifier 'identifier' - = ([A-Z]+[_a-z0-9]i*) {return nodeIdentifier(text())} + = ([A-Z]+[_a-z0-9]i*) {return nodeModuleIdentifier(text())} string 'string' From 346c83f2be56ec5f5f8703024c42e9ad2c43851b Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 9 Jun 2022 14:35:44 +0200 Subject: [PATCH 02/12] module parse --- .../rescript/Reducer/Reducer_Peggy/Reducer_Peggy_Parse.res | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_Parse.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_Parse.res index c53fab7a..5bdcda14 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_Parse.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_Parse.res @@ -22,6 +22,7 @@ type nodeInteger = {...node, "value": int} type nodeKeyValue = {...node, "key": node, "value": node} type nodeLambda = {...node, "args": array, "body": nodeBlock} type nodeLetStatement = {...node, "variable": nodeIdentifier, "value": node} +type nodeModuleIdentifier = {...node, "value": string} type nodeString = {...node, "value": string} type nodeTernary = {...node, "condition": node, "trueExpression": node, "falseExpression": node} type nodeTypeIdentifier = {...node, "value": string} @@ -37,6 +38,7 @@ type peggyNode = | PgNodeKeyValue(nodeKeyValue) | PgNodeLambda(nodeLambda) | PgNodeLetStatement(nodeLetStatement) + | PgNodeModuleIdentifier(nodeModuleIdentifier) | PgNodeString(nodeString) | PgNodeTernary(nodeTernary) | PgNodeTypeIdentifier(nodeTypeIdentifier) @@ -51,6 +53,7 @@ external castNodeInteger: node => nodeInteger = "%identity" external castNodeKeyValue: node => nodeKeyValue = "%identity" external castNodeLambda: node => nodeLambda = "%identity" external castNodeLetStatement: node => nodeLetStatement = "%identity" +external castNodeModuleIdentifier: node => nodeModuleIdentifier = "%identity" external castNodeString: node => nodeString = "%identity" external castNodeTernary: node => nodeTernary = "%identity" external castNodeTypeIdentifier: node => nodeTypeIdentifier = "%identity" @@ -68,6 +71,7 @@ let castNodeType = (node: node) => | "KeyValue" => node->castNodeKeyValue->PgNodeKeyValue | "Lambda" => node->castNodeLambda->PgNodeLambda | "LetStatement" => node->castNodeLetStatement->PgNodeLetStatement + | "ModuleIdentifier" => node->castNodeModuleIdentifier->PgNodeModuleIdentifier | "String" => node->castNodeString->PgNodeString | "Ternary" => node->castNodeTernary->PgNodeTernary | "TypeIdentifier" => node->castNodeTypeIdentifier->PgNodeTypeIdentifier @@ -94,6 +98,7 @@ let rec pgToString = (peggyNode: peggyNode): string => { "{|" ++ node["args"]->argsToString ++ "| " ++ pgToString(PgNodeBlock(node["body"])) ++ "}" | PgNodeLetStatement(node) => pgToString(PgNodeIdentifier(node["variable"])) ++ " = " ++ toString(node["value"]) + | PgNodeModuleIdentifier(node) => `@${node["value"]}` | PgNodeString(node) => `'${node["value"]->Js.String.make}'` | PgNodeTernary(node) => "(::$$_ternary_$$ " ++ From 5f6feb26ba19b650efa5e4cc958253ce60d9b7dc Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 9 Jun 2022 14:39:06 +0200 Subject: [PATCH 03/12] Module parse tested --- .../Reducer/Reducer_Peggy/Reducer_Peggy_Parse_test.res | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_Parse_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_Parse_test.res index 2e7b3ec5..4bf3747f 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_Parse_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_Parse_test.res @@ -235,6 +235,9 @@ describe("Peggy parse", () => { testParse("1M", "{(::fromUnit_M 1)}") testParse("1m+2cm", "{(::add (::fromUnit_m 1) (::fromUnit_cm 2))}") }) + describe("Module", () => { + testParse("Math.pi", "{(::$_atIndex_$ @Math 'pi')}") + }) }) describe("parsing new line", () => { From 02c06b638e82b422fc036209dbed1b0537d07d34 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 9 Jun 2022 14:57:25 +0200 Subject: [PATCH 04/12] module access to expression --- .../Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression_test.res | 4 ++++ .../Reducer_Expression_ExpressionBuilder.res | 2 ++ .../Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression.res | 2 ++ 3 files changed, 8 insertions(+) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression_test.res index baa9a5f0..dbe16837 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression_test.res @@ -181,4 +181,8 @@ describe("Peggy to Expression", () => { (), ) }) + + describe("module", () => { + testToExpression("Math.pi", "{(:$_atIndex_$ :Math 'pi')}", ()) + }) }) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res index 39d49655..22afadba 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res @@ -65,5 +65,7 @@ let eBindExpression = (bindingExpr: expression, expression: expression): express let eBindExpressionDefault = (expression: expression): expression => eFunction("$$_bindExpression_$$", list{expression}) +let eIdentifier = (name: string): expression => name->BExpressionValue.EvSymbol->BExpressionT.EValue + let eTypeIdentifier = (name: string): expression => name->BExpressionValue.EvTypeIdentifier->BExpressionT.EValue diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression.res index ce265b41..6e04a55d 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression.res @@ -34,6 +34,8 @@ let rec fromNode = (node: Parse.node): expression => { nodeLetStatement["variable"]["value"], fromNode(nodeLetStatement["value"]), ) + | PgNodeModuleIdentifier(nodeModuleIdentifier) => + ExpressionBuilder.eIdentifier(nodeModuleIdentifier["value"]) | PgNodeString(nodeString) => ExpressionBuilder.eString(nodeString["value"]) | PgNodeTernary(nodeTernary) => ExpressionBuilder.eFunction( From b623be5f9bd68eb013288cf7c6c921177c6e8d2f Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 9 Jun 2022 14:57:35 +0200 Subject: [PATCH 05/12] format --- .../src/rescript/ReducerInterface/ReducerInterface_Date.res | 2 +- .../src/rescript/ReducerInterface/ReducerInterface_Duration.res | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Date.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Date.res index dc04a474..02c3affc 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Date.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Date.res @@ -24,4 +24,4 @@ let dispatch = (call: EV.functionCall, _: DistributionOperation.env): option< EV.EvDate(DateTime.Date.addDuration(d1, d2))->Ok->Some | _ => None } -} \ No newline at end of file +} diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Duration.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Duration.res index a11aa745..4e10dfa0 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Duration.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Duration.res @@ -29,4 +29,4 @@ let dispatch = (call: EV.functionCall, _: DistributionOperation.env): option< EV.EvTimeDuration(DateTime.Duration.divide(d1, d2))->Ok->Some | _ => None } -} \ No newline at end of file +} From 710756bc50c34dd4c5cf614c23324c3a0b8de743 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 9 Jun 2022 15:27:37 +0200 Subject: [PATCH 06/12] EvModule defined --- .../ReducerInterface/ReducerInterface_ExpressionValue.res | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res index 628dfc8e..61928683 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res @@ -24,6 +24,7 @@ type rec expressionValue = | EvTimeDuration(float) | EvDeclaration(lambdaDeclaration) | EvTypeIdentifier(string) + | EvModule(record) and record = Js.Dict.t and externalBindings = record and lambdaValue = { @@ -60,6 +61,7 @@ let rec toString = aValue => | EvTimeDuration(t) => DateTime.Duration.toString(t) | EvDeclaration(d) => Declaration.toString(d, r => toString(EvLambda(r))) | EvTypeIdentifier(id) => `#${id}` + | EvModule(m) => `@${m->toStringRecord}` } and toStringRecord = aRecord => { let pairs = @@ -86,6 +88,7 @@ let toStringWithType = aValue => | EvTimeDuration(_) => `Date::${toString(aValue)}` | EvDeclaration(_) => `Declaration::${toString(aValue)}` | EvTypeIdentifier(_) => `TypeIdentifier::${toString(aValue)}` + | EvModule(_) => `Module::${toString(aValue)}` } let argsToString = (args: array): string => { @@ -133,6 +136,7 @@ type expressionValueType = | EvtTimeDuration | EvtDeclaration | EvtTypeIdentifier + | EvtModule type functionCallSignature = CallSignature(string, array) type functionDefinitionSignature = @@ -154,6 +158,7 @@ let valueToValueType = value => | EvTimeDuration(_) => EvtTimeDuration | EvDeclaration(_) => EvtDeclaration | EvTypeIdentifier(_) => EvtTypeIdentifier + | EvModule(_) => EvtModule } let functionCallToCallSignature = (functionCall: functionCall): functionCallSignature => { @@ -177,6 +182,7 @@ let valueTypeToString = (valueType: expressionValueType): string => | EvtTimeDuration => `Duration` | EvtDeclaration => `Declaration` | EvtTypeIdentifier => `TypeIdentifier` + | EvtModule => `Module` } let functionCallSignatureToString = (functionCallSignature: functionCallSignature): string => { From afffdd85598c74aabca3777274979f4d87d17cab Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 9 Jun 2022 15:27:51 +0200 Subject: [PATCH 07/12] typescript work in progress --- packages/squiggle-lang/src/js/index.ts | 2 ++ packages/squiggle-lang/src/js/rescript_interop.ts | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/squiggle-lang/src/js/index.ts b/packages/squiggle-lang/src/js/index.ts index 555d141b..d963b134 100644 --- a/packages/squiggle-lang/src/js/index.ts +++ b/packages/squiggle-lang/src/js/index.ts @@ -187,5 +187,7 @@ function createTsExport( return tag("lambdaDeclaration", x.value); case "EvTypeIdentifier": return tag("typeIdentifier", x.value); + case "EvModule": + return tag("module", x.value); } } diff --git a/packages/squiggle-lang/src/js/rescript_interop.ts b/packages/squiggle-lang/src/js/rescript_interop.ts index 61b011c3..4c862b96 100644 --- a/packages/squiggle-lang/src/js/rescript_interop.ts +++ b/packages/squiggle-lang/src/js/rescript_interop.ts @@ -73,7 +73,11 @@ export type rescriptExport = | { TAG: 13; // EvTypeIdentifier _0: string; - }; + } + | { + TAG: 14; // EvModule + _0: { [key: string]: rescriptExport }; + }; type rescriptDist = | { TAG: 0; _0: rescriptPointSetDist } @@ -125,7 +129,8 @@ export type squiggleExpression = | tagged<"timeDuration", number> | tagged<"lambdaDeclaration", lambdaDeclaration> | tagged<"record", { [key: string]: squiggleExpression }> - | tagged<"typeIdentifier", string>; + | tagged<"typeIdentifier", string> + | tagged<"module", { [key: string]: squiggleExpression }>; export { lambdaValue }; @@ -177,6 +182,11 @@ export function convertRawToTypescript( }); case 13: // EvSymbol return tag("typeIdentifier", result._0); + case 14: // EvModule + return tag( + "module", + _.mapValues(result._0, (x) => convertRawToTypescript(x, environment)) + ); } } From e4eceb9cd3b4d6dcc8093641292e05a37949a522 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Thu, 9 Jun 2022 06:44:03 -0700 Subject: [PATCH 08/12] Minor fix for EvModule support --- packages/squiggle-lang/src/js/index.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/squiggle-lang/src/js/index.ts b/packages/squiggle-lang/src/js/index.ts index d963b134..0c241e1f 100644 --- a/packages/squiggle-lang/src/js/index.ts +++ b/packages/squiggle-lang/src/js/index.ts @@ -188,6 +188,12 @@ function createTsExport( case "EvTypeIdentifier": return tag("typeIdentifier", x.value); case "EvModule": - return tag("module", x.value); + let moduleResult: tagged<"module", { [key: string]: squiggleExpression }> = tag( + "module", + _.mapValues(x.value, (x: unknown) => + convertRawToTypescript(x as rescriptExport, environment) + ) + ); + return moduleResult; } } From 2f4db3230df93ee0da5ed323fc7f554b5db4f3ce Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 9 Jun 2022 15:53:07 +0200 Subject: [PATCH 09/12] format --- packages/squiggle-lang/src/js/index.ts | 5 ++++- packages/squiggle-lang/src/js/rescript_interop.ts | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/squiggle-lang/src/js/index.ts b/packages/squiggle-lang/src/js/index.ts index 0c241e1f..592de8e3 100644 --- a/packages/squiggle-lang/src/js/index.ts +++ b/packages/squiggle-lang/src/js/index.ts @@ -188,7 +188,10 @@ function createTsExport( case "EvTypeIdentifier": return tag("typeIdentifier", x.value); case "EvModule": - let moduleResult: tagged<"module", { [key: string]: squiggleExpression }> = tag( + let moduleResult: tagged< + "module", + { [key: string]: squiggleExpression } + > = tag( "module", _.mapValues(x.value, (x: unknown) => convertRawToTypescript(x as rescriptExport, environment) diff --git a/packages/squiggle-lang/src/js/rescript_interop.ts b/packages/squiggle-lang/src/js/rescript_interop.ts index 4c862b96..41baa2e3 100644 --- a/packages/squiggle-lang/src/js/rescript_interop.ts +++ b/packages/squiggle-lang/src/js/rescript_interop.ts @@ -75,9 +75,9 @@ export type rescriptExport = _0: string; } | { - TAG: 14; // EvModule - _0: { [key: string]: rescriptExport }; - }; + TAG: 14; // EvModule + _0: { [key: string]: rescriptExport }; + }; type rescriptDist = | { TAG: 0; _0: rescriptPointSetDist } @@ -184,7 +184,7 @@ export function convertRawToTypescript( return tag("typeIdentifier", result._0); case 14: // EvModule return tag( - "module", + "module", _.mapValues(result._0, (x) => convertRawToTypescript(x, environment)) ); } From 3ca209a53dbd3540d078b141dea57007804c21af Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Fri, 10 Jun 2022 13:24:36 +0200 Subject: [PATCH 10/12] bindings and module managers bindings are a module --- .../Reducer_Expression_Bindings.res | 70 +++---------------- .../Reducer_Manager_Bindings.res | 12 ++++ .../Reducer_Manager_Module.res | 29 ++++++++ .../SquiggleLibrary_DefaultBindings.res | 1 + .../SquiggleLibrary/SquiggleLibrary_Math.res | 1 + 5 files changed, 52 insertions(+), 61 deletions(-) create mode 100644 packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Bindings.res create mode 100644 packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Module.res create mode 100644 packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_DefaultBindings.res create mode 100644 packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Bindings.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Bindings.res index abc636be..53de170f 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Bindings.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Bindings.res @@ -2,77 +2,25 @@ module ErrorValue = Reducer_ErrorValue module ExpressionT = Reducer_Expression_T module ExpressionValue = ReducerInterface.ExpressionValue module Result = Belt.Result +module BindingsManager = Reducer_Manager_Bindings type errorValue = Reducer_ErrorValue.errorValue type expression = ExpressionT.expression type expressionValue = ExpressionValue.expressionValue type externalBindings = ReducerInterface_ExpressionValue.externalBindings -let defaultBindings: ExpressionT.bindings = Belt.Map.String.empty +let emptyBindings = Reducer_Manager_Bindings.emptyBindings -let typeAliasesKey = "_typeAliases_" -let typeReferencesKey = "_typeReferences_" +let typeAliasesKey = BindingsManager.typeAliasesKey +let typeReferencesKey = BindingsManager.typeReferencesKey -let toExternalBindings = (bindings: ExpressionT.bindings): externalBindings => { - let keys = Belt.Map.String.keysToArray(bindings) - keys->Belt.Array.reduce(Js.Dict.empty(), (acc, key) => { - let value = bindings->Belt.Map.String.getExn(key) - Js.Dict.set(acc, key, value) - acc - }) -} +let toExternalBindings = (bindings: ExpressionT.bindings): externalBindings => + BindingsManager.toRecord(bindings) -let fromExternalBindings_ = (externalBindings: externalBindings): ExpressionT.bindings => { - let keys = Js.Dict.keys(externalBindings) - keys->Belt.Array.reduce(defaultBindings, (acc, key) => { - let value = Js.Dict.unsafeGet(externalBindings, key) - acc->Belt.Map.String.set(key, value) - }) -} +let fromExternalBindings = (externalBindings: externalBindings): ExpressionT.bindings => + BindingsManager.fromRecord(externalBindings) -let fromExternalBindings = (externalBindings: externalBindings): ExpressionT.bindings => { - // TODO: This code will be removed in the future when maps are used instead of records. Please don't mind this function for now. - - let internalBindings0 = fromExternalBindings_(externalBindings) - - let oExistingTypeAliases = Belt.Map.String.get(internalBindings0, typeAliasesKey) - let internalBindings1 = Belt.Option.mapWithDefault( - oExistingTypeAliases, - internalBindings0, - existingTypeAliases => { - let newTypeAliases = switch existingTypeAliases { - | EvRecord(actualTypeAliases) => - actualTypeAliases->fromExternalBindings_->toExternalBindings->ExpressionValue.EvRecord - | _ => existingTypeAliases - } - Belt.Map.String.set(internalBindings0, typeAliasesKey, newTypeAliases) - }, - ) - - let oExistingTypeReferences = Belt.Map.String.get(internalBindings1, typeReferencesKey) - let internalBindings2 = Belt.Option.mapWithDefault( - oExistingTypeReferences, - internalBindings1, - existingTypeReferences => { - let newTypeReferences = switch existingTypeReferences { - | EvRecord(actualTypeReferences) => - actualTypeReferences->fromExternalBindings_->toExternalBindings->ExpressionValue.EvRecord - | _ => existingTypeReferences - } - Belt.Map.String.set(internalBindings0, typeReferencesKey, newTypeReferences) - }, - ) - - internalBindings2 -} - -let fromValue = (aValue: expressionValue) => - switch aValue { - | EvRecord(externalBindings) => fromExternalBindings(externalBindings) - | _ => defaultBindings - } - -let externalFromArray = anArray => Js.Dict.fromArray(anArray) +let fromValue = (aValue: expressionValue) => BindingsManager.fromExpressionValue(aValue) let isMacroName = (fName: string): bool => fName->Js.String2.startsWith("$$") diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Bindings.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Bindings.res new file mode 100644 index 00000000..1973fe89 --- /dev/null +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Bindings.res @@ -0,0 +1,12 @@ +include Reducer_Manager_Module + +open ReducerInterface_ExpressionValue + +let emptyBindings = emptyModule + +let toExpressionValue = (container: t): expressionValue => EvRecord(toRecord(container)) +let fromExpressionValue = (aValue: expressionValue): t => + switch aValue { + | EvRecord(r) => fromRecord(r) + | _ => emptyBindings + } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Module.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Module.res new file mode 100644 index 00000000..6d63f695 --- /dev/null +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Module.res @@ -0,0 +1,29 @@ +module ExpressionT = Reducer_Expression_T +open ReducerInterface_ExpressionValue +let expressionValueToString = toString + +type t = ExpressionT.bindings + +let typeAliasesKey = "_typeAliases_" +let typeReferencesKey = "_typeReferences_" + +let define = (container: t, identifier: string, ev: expressionValue): t => + Belt.Map.String.set(container, identifier, ev) // TODO build lambda for polymorphic functions here + +let defineNumber = (container: t, identifier: string, value: float): t => + container->define(identifier, EvNumber(value)) + +let cloneRecord = (r: record): record => r->Js.Dict.entries->Js.Dict.fromArray +let fromRecord = (r: record): t => Js.Dict.entries(r)->Belt.Map.String.fromArray +let toRecord = (container: t): record => Belt.Map.String.toArray(container)->Js.Dict.fromArray + +let emptyModule: t = Belt.Map.String.empty + +let toExpressionValue = (container: t): expressionValue => EvModule(toRecord(container)) +let fromExpressionValue = (aValue: expressionValue): t => + switch aValue { + | EvModule(r) => fromRecord(r) + | _ => emptyModule + } + +let toString = (container: t): string => container->toRecord->EvRecord->expressionValueToString diff --git a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_DefaultBindings.res b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_DefaultBindings.res new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_DefaultBindings.res @@ -0,0 +1 @@ + diff --git a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res @@ -0,0 +1 @@ + From 69bab173317ef6a97f42a4f9c7bc5e91eb78a889 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Fri, 10 Jun 2022 14:38:31 +0200 Subject: [PATCH 11/12] Math module tests defined (failing) rename modules define module Math module test helper fixed for defaults and test defined --- .../__tests__/Reducer/Reducer_Helpers.res | 19 +++++++++++++++++++ .../Reducer_Peggy_TestHelpers.res | 7 ++++++- .../Reducer_Peggy_ToExpression_test.res | 2 +- .../__tests__/Reducer/Reducer_TestHelpers.res | 7 ++++++- .../SquiggleLibrary_Math_test.res | 7 +++++++ .../src/rescript/Reducer/Reducer.res | 2 +- .../Reducer_Category_Bindings.res} | 2 +- .../Reducer_Category_Module.res} | 18 +++++++++++------- .../Reducer_Expression/Reducer_Expression.res | 2 +- .../Reducer_Expression_Bindings.res | 14 +++++++------- ...ducerInterface_DefaultExternalBindings.res | 6 ++++++ .../ReducerInterface_ExpressionValue.res | 4 ---- .../SquiggleLibrary_DefaultBindings.res | 1 - .../SquiggleLibrary/SquiggleLibrary_Math.res | 7 +++++++ 14 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 packages/squiggle-lang/__tests__/Reducer/Reducer_Helpers.res create mode 100644 packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_Math_test.res rename packages/squiggle-lang/src/rescript/Reducer/{Reducer_Manager/Reducer_Manager_Bindings.res => Reducer_Category/Reducer_Category_Bindings.res} (82%) rename packages/squiggle-lang/src/rescript/Reducer/{Reducer_Manager/Reducer_Manager_Module.res => Reducer_Category/Reducer_Category_Module.res} (88%) create mode 100644 packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_DefaultExternalBindings.res delete mode 100644 packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_DefaultBindings.res diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_Helpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_Helpers.res new file mode 100644 index 00000000..47fd5895 --- /dev/null +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_Helpers.res @@ -0,0 +1,19 @@ +module ExpressionT = Reducer_Expression_T +module ExpressionValue = ReducerInterface.ExpressionValue +module ErrorValue = Reducer_ErrorValue +module Bindings = Reducer_Category_Bindings + +let removeDefaults = (ev: ExpressionT.expressionValue): ExpressionT.expressionValue => + switch ev { + | EvRecord(extbindings) => { + let bindings: Bindings.t = Bindings.fromRecord(extbindings) + let keys = Js.Dict.keys(Reducer.defaultExternalBindings) + Belt.Map.String.keep(bindings, (key, _value) => { + let removeThis = Js.Array2.includes(keys, key) + !removeThis + })->Bindings.toExpressionValue + } + | value => value + } + +let rRemoveDefaults = r => Belt.Result.map(r, ev => removeDefaults(ev)) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_TestHelpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_TestHelpers.res index 7f2160ee..44218a4b 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_TestHelpers.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_TestHelpers.res @@ -23,8 +23,13 @@ let expectToExpressionToBe = (expr, answer, ~v="_", ()) => { let a2 = rExpr ->Result.flatMap(expr => - Expression.reduceExpression(expr, Belt.Map.String.empty, ExpressionValue.defaultEnvironment) + Expression.reduceExpression( + expr, + ReducerInterface_DefaultExternalBindings.defaultInternalBindings, + ExpressionValue.defaultEnvironment, + ) ) + ->Reducer_Helpers.rRemoveDefaults ->ExpressionValue.toStringResultOkless (a1, a2)->expect->toEqual((answer, v)) } diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression_test.res index dbe16837..92491e8f 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_Peggy/Reducer_Peggy_ToExpression_test.res @@ -183,6 +183,6 @@ describe("Peggy to Expression", () => { }) describe("module", () => { - testToExpression("Math.pi", "{(:$_atIndex_$ :Math 'pi')}", ()) + testToExpression("Math.pi", "{(:$_atIndex_$ :Math 'pi')}", ~v="3.141592653589793", ()) }) }) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestHelpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestHelpers.res index 6ec9930c..092ccc9f 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestHelpers.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestHelpers.res @@ -1,6 +1,7 @@ module ExpressionT = Reducer_Expression_T module ExpressionValue = ReducerInterface.ExpressionValue module ErrorValue = Reducer_ErrorValue +module Bindings = Reducer_Category_Bindings open Jest open Expect @@ -17,7 +18,11 @@ let expectParseToBe = (expr: string, answer: string) => Reducer.parse(expr)->ExpressionT.toStringResult->expect->toBe(answer) let expectEvalToBe = (expr: string, answer: string) => - Reducer.evaluate(expr)->ExpressionValue.toStringResult->expect->toBe(answer) + Reducer.evaluate(expr) + ->Reducer_Helpers.rRemoveDefaults + ->ExpressionValue.toStringResult + ->expect + ->toBe(answer) let expectEvalError = (expr: string) => Reducer.evaluate(expr)->ExpressionValue.toStringResult->expect->toMatch("Error\(") diff --git a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_Math_test.res b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_Math_test.res new file mode 100644 index 00000000..964df490 --- /dev/null +++ b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_Math_test.res @@ -0,0 +1,7 @@ +open Jest +open Reducer_TestHelpers + +describe("Math Library", () => { + testEvalToBe("Math.e", "") + testEvalToBe("Math.pi", "") +}) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer.res index 3b3450c3..36fe3cd6 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer.res @@ -24,4 +24,4 @@ let foreignFunctionInterface = ( let defaultEnvironment = ExpressionValue.defaultEnvironment -let defaultExternalBindings = ExpressionValue.defaultExternalBindings +let defaultExternalBindings = ReducerInterface_DefaultExternalBindings.defaultExternalBindings diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Bindings.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Bindings.res similarity index 82% rename from packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Bindings.res rename to packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Bindings.res index 1973fe89..5a8e570d 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Bindings.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Bindings.res @@ -1,4 +1,4 @@ -include Reducer_Manager_Module +include Reducer_Category_Module // Bindings inherit from Module open ReducerInterface_ExpressionValue diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Module.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module.res similarity index 88% rename from packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Module.res rename to packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module.res index 6d63f695..092d0690 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Manager/Reducer_Manager_Module.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module.res @@ -7,18 +7,12 @@ type t = ExpressionT.bindings let typeAliasesKey = "_typeAliases_" let typeReferencesKey = "_typeReferences_" -let define = (container: t, identifier: string, ev: expressionValue): t => - Belt.Map.String.set(container, identifier, ev) // TODO build lambda for polymorphic functions here - -let defineNumber = (container: t, identifier: string, value: float): t => - container->define(identifier, EvNumber(value)) +let emptyModule: t = Belt.Map.String.empty let cloneRecord = (r: record): record => r->Js.Dict.entries->Js.Dict.fromArray let fromRecord = (r: record): t => Js.Dict.entries(r)->Belt.Map.String.fromArray let toRecord = (container: t): record => Belt.Map.String.toArray(container)->Js.Dict.fromArray -let emptyModule: t = Belt.Map.String.empty - let toExpressionValue = (container: t): expressionValue => EvModule(toRecord(container)) let fromExpressionValue = (aValue: expressionValue): t => switch aValue { @@ -27,3 +21,13 @@ let fromExpressionValue = (aValue: expressionValue): t => } let toString = (container: t): string => container->toRecord->EvRecord->expressionValueToString + +// -- Module definition +let define = (container: t, identifier: string, ev: expressionValue): t => + Belt.Map.String.set(container, identifier, ev) // TODO build lambda for polymorphic functions here + +let defineNumber = (container: t, identifier: string, value: float): t => + container->define(identifier, EvNumber(value)) + +let defineModule = (container: t, identifier: string, value: t): t => + container->define(identifier, toExpressionValue(value)) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res index c62201ce..066a68b2 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression.res @@ -123,7 +123,7 @@ let evaluateUsingOptions = ( let anExternalBindings = switch externalBindings { | Some(bindings) => bindings - | None => ReducerInterface_ExpressionValue.defaultExternalBindings + | None => ReducerInterface_DefaultExternalBindings.defaultExternalBindings } let bindings = anExternalBindings->Bindings.fromExternalBindings diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Bindings.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Bindings.res index 53de170f..c637581f 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Bindings.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Bindings.res @@ -2,25 +2,25 @@ module ErrorValue = Reducer_ErrorValue module ExpressionT = Reducer_Expression_T module ExpressionValue = ReducerInterface.ExpressionValue module Result = Belt.Result -module BindingsManager = Reducer_Manager_Bindings +module Bindings = Reducer_Category_Bindings type errorValue = Reducer_ErrorValue.errorValue type expression = ExpressionT.expression type expressionValue = ExpressionValue.expressionValue type externalBindings = ReducerInterface_ExpressionValue.externalBindings -let emptyBindings = Reducer_Manager_Bindings.emptyBindings +let emptyBindings = Reducer_Category_Bindings.emptyBindings -let typeAliasesKey = BindingsManager.typeAliasesKey -let typeReferencesKey = BindingsManager.typeReferencesKey +let typeAliasesKey = Bindings.typeAliasesKey +let typeReferencesKey = Bindings.typeReferencesKey let toExternalBindings = (bindings: ExpressionT.bindings): externalBindings => - BindingsManager.toRecord(bindings) + Bindings.toRecord(bindings) let fromExternalBindings = (externalBindings: externalBindings): ExpressionT.bindings => - BindingsManager.fromRecord(externalBindings) + Bindings.fromRecord(externalBindings) -let fromValue = (aValue: expressionValue) => BindingsManager.fromExpressionValue(aValue) +let fromValue = (aValue: expressionValue) => Bindings.fromExpressionValue(aValue) let isMacroName = (fName: string): bool => fName->Js.String2.startsWith("$$") diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_DefaultExternalBindings.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_DefaultExternalBindings.res new file mode 100644 index 00000000..2e8abee4 --- /dev/null +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_DefaultExternalBindings.res @@ -0,0 +1,6 @@ +module Bindings = Reducer_Category_Bindings + +let defaultInternalBindings = Bindings.emptyBindings->SquiggleLibrary_Math.makeBindings + +@genType +let defaultExternalBindings = defaultInternalBindings->Bindings.toRecord diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res index 61928683..1313207a 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res @@ -4,7 +4,6 @@ */ module Extra_Array = Reducer_Extra_Array module ErrorValue = Reducer_ErrorValue - @genType.opaque type internalCode = Object @@ -34,9 +33,6 @@ and lambdaValue = { } and lambdaDeclaration = Declaration.declaration -@genType -let defaultExternalBindings: externalBindings = Js.Dict.empty() - type functionCall = (string, array) let rec toString = aValue => diff --git a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_DefaultBindings.res b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_DefaultBindings.res deleted file mode 100644 index 8b137891..00000000 --- a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_DefaultBindings.res +++ /dev/null @@ -1 +0,0 @@ - diff --git a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res index 8b137891..a29fafcc 100644 --- a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res +++ b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res @@ -1 +1,8 @@ +module Bindings = Reducer_Category_Bindings +module Module = Reducer_Category_Module +let m = + Module.emptyModule->Module.defineNumber("pi", Js.Math._PI)->Module.defineNumber("e", Js.Math._E) + +let makeBindings = (previousBindings: Bindings.t): Bindings.t => + previousBindings->Bindings.defineModule("Math", m) From 0704c8d9f5d9caacda1f110218cd28e4b0536b21 Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Sat, 11 Jun 2022 15:15:25 +0200 Subject: [PATCH 12/12] module tests pass --- .../__tests__/SquiggleLibrary/SquiggleLibrary_Math_test.res | 4 ++-- .../Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_Math_test.res b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_Math_test.res index 964df490..a8db2854 100644 --- a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_Math_test.res +++ b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_Math_test.res @@ -2,6 +2,6 @@ open Jest open Reducer_TestHelpers describe("Math Library", () => { - testEvalToBe("Math.e", "") - testEvalToBe("Math.pi", "") + testEvalToBe("Math.e", "Ok(2.718281828459045)") + testEvalToBe("Math.pi", "Ok(3.141592653589793)") }) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res index 218aa673..33e63496 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res @@ -256,6 +256,7 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce switch call { | ("$_atIndex_$", [EvArray(aValueArray), EvNumber(fIndex)]) => arrayAtIndex(aValueArray, fIndex) + | ("$_atIndex_$", [EvModule(dict), EvString(sIndex)]) => recordAtIndex(dict, sIndex) | ("$_atIndex_$", [EvRecord(dict), EvString(sIndex)]) => recordAtIndex(dict, sIndex) | ("$_constructArray_$", [EvArray(aValueArray)]) => EvArray(aValueArray)->Ok | ("$_constructRecord_$", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)