From c0ccdbc1e9d98a89eb09f0d6b67d64d799a999f6 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Wed, 7 Sep 2022 22:45:19 +0400 Subject: [PATCH 1/5] experimental: replace result with exceptions in reducer --- .../Reducer/Reducer_TestMacroHelpers.res | 16 ++-- ...educer_Type_TypeChecker_arguments_test.res | 2 +- .../Reducer_Type_TypeChecker_test.res | 2 +- .../FunctionRegistry/Library/FR_Danger.res | 16 ++-- .../FunctionRegistry/Library/FR_List.res | 80 ++++++++---------- .../FunctionRegistry/Library/FR_Pointset.res | 2 +- .../FunctionRegistry/Library/FR_Sampleset.res | 2 +- .../Reducer_Dispatch_BuiltIn.res | 15 ++-- .../Reducer_Dispatch_BuiltInMacros.res | 81 ++++++++----------- .../rescript/Reducer/Reducer_ErrorValue.res | 2 + .../Reducer_Expression/Reducer_Expression.res | 61 ++++++-------- .../Reducer_ExpressionWithContext.res | 2 +- .../Reducer_Expression_BindingsReplacer.res | 28 +++---- .../Reducer_Expression_Lambda.res | 38 +++++---- .../Reducer_Expression_Macro.res | 21 +---- .../Reducer_Type/Reducer_Type_Compile.res | 10 +-- .../ReducerProject_ProjectItem.res | 28 ++++--- .../ReducerProject_ReducerFn_T.res | 2 +- 18 files changed, 178 insertions(+), 230 deletions(-) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res index 752cc9c7..363d33c7 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res @@ -17,17 +17,22 @@ let testMacro_ = ( expectedCode: string, ) => { let bindings = Bindings.fromArray(bindArray) - tester(expr->T.toString, () => - expr - ->Macro.expandMacroCall( + tester(expr->T.toString, () => { + let result = switch expr + ->Reducer_Dispatch_BuiltInMacros.dispatchMacroCall( bindings, ProjectAccessorsT.identityAccessors, Expression.reduceExpressionInProject, - ) + ) { + | v => Ok(v) + | exception Reducer_ErrorValue.ErrorException(e) => Error(e) + } + + result ->ExpressionWithContext.toStringResult ->expect ->toEqual(expectedCode) - ) + }) } let testMacroEval_ = ( @@ -44,6 +49,7 @@ let testMacroEval_ = ( ProjectAccessorsT.identityAccessors, Expression.reduceExpressionInProject, ) + ->Ok ->InternalExpressionValue.toStringResult ->expect ->toEqual(expectedValue) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_Type/Reducer_Type_TypeChecker_arguments_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_Type/Reducer_Type_TypeChecker_arguments_test.res index 29fa8249..957024cb 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_Type/Reducer_Type_TypeChecker_arguments_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_Type/Reducer_Type_TypeChecker_arguments_test.res @@ -16,7 +16,7 @@ let checkArgumentsSourceCode = (aTypeSourceCode: string, sourceCode: string): re > => { let reducerFn = Expression.reduceExpressionInProject let rResult = - Expression.BackCompatible.parse(sourceCode)->Belt.Result.flatMap(expr => + Expression.BackCompatible.parse(sourceCode)->Belt.Result.map(expr => reducerFn(expr, Bindings.emptyBindings, ProjectAccessorsT.identityAccessors) ) rResult->Belt.Result.flatMap(result => diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_Type/Reducer_Type_TypeChecker_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_Type/Reducer_Type_TypeChecker_test.res index fccff738..d5726d5d 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_Type/Reducer_Type_TypeChecker_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_Type/Reducer_Type_TypeChecker_test.res @@ -19,7 +19,7 @@ let isTypeOfSourceCode = (aTypeSourceCode: string, sourceCode: string): result< > => { let reducerFn = Expression.reduceExpressionInProject let rResult = - Expression.BackCompatible.parse(sourceCode)->Belt.Result.flatMap(expr => + Expression.BackCompatible.parse(sourceCode)->Belt.Result.map(expr => reducerFn(expr, Bindings.emptyBindings, ProjectAccessorsT.identityAccessors) ) rResult->Belt.Result.flatMap(result => TypeChecker.isTypeOf(aTypeSourceCode, result, reducerFn)) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res index 95f9ba44..df3a91de 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res @@ -74,12 +74,11 @@ module Integration = { reducer, ) let result = switch resultAsInternalExpression { - | Ok(IEvNumber(x)) => Ok(x) - | Error(_) => + | IEvNumber(x) => Ok(x) + | _ => Error( "Error 1 in Danger.integrate. It's possible that your function doesn't return a number, try definining auxiliaryFunction(x) = mean(yourFunction(x)) and integrate auxiliaryFunction instead", ) - | _ => Error("Error 2 in Danger.integrate") } result } @@ -143,7 +142,7 @@ module Integration = { } | Error(b) => Error( - "Integration error 3 in Danger.integrate. It's possible that your function doesn't return a number, try definining auxiliaryFunction(x) = mean(yourFunction(x)) and integrate auxiliaryFunction instead." ++ + "Integration error 2 in Danger.integrate. It's possible that your function doesn't return a number, try definining auxiliaryFunction(x) = mean(yourFunction(x)) and integrate auxiliaryFunction instead." ++ "Original error: " ++ b, ) @@ -310,15 +309,10 @@ module DiminishingReturns = { reducer, ) switch resultAsInternalExpression { - | Ok(IEvNumber(x)) => Ok(x) - | Error(_) => - Error( + | IEvNumber(x) => Ok(x) + | _ => Error( "Error 1 in Danger.optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions. It's possible that your function doesn't return a number, try definining auxiliaryFunction(x) = mean(yourFunction(x)) and integrate auxiliaryFunction instead", ) - | _ => - Error( - "Error 2 in Danger.optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions", - ) } } diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res index 63b7b3f4..733c2f86 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res @@ -32,19 +32,17 @@ module Internals = { accessors: ProjectAccessorsT.t, eLambdaValue, reducer: ProjectReducerFnT.t, - ): result => { - let rMappedList = array->E.A.reduceReverse(Ok(list{}), (rAcc, elem) => - rAcc->E.R.bind(_, acc => { - let rNewElem = Reducer_Expression_Lambda.doLambdaCall( - eLambdaValue, - list{elem}, - (accessors: ProjectAccessorsT.t), - (reducer: ProjectReducerFnT.t), - ) - rNewElem->E.R2.fmap(newElem => list{newElem, ...acc}) - }) - ) - rMappedList->E.R2.fmap(mappedList => mappedList->Belt.List.toArray->Wrappers.evArray) + ): ReducerInterface_InternalExpressionValue.t => { + let mappedList = array->E.A.reduceReverse(list{}, (acc, elem) => { + let newElem = Reducer_Expression_Lambda.doLambdaCall( + eLambdaValue, + list{elem}, + (accessors: ProjectAccessorsT.t), + (reducer: ProjectReducerFnT.t), + ) + list{newElem, ...acc} + }) + mappedList->Belt.List.toArray->Wrappers.evArray } let reduce = ( @@ -54,10 +52,8 @@ module Internals = { accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t, ) => { - aValueArray->E.A.reduce(Ok(initialValue), (rAcc, elem) => - rAcc->E.R.bind(_, acc => - Reducer_Expression_Lambda.doLambdaCall(aLambdaValue, list{acc, elem}, accessors, reducer) - ) + aValueArray->E.A.reduce(initialValue, (acc, elem) => + Reducer_Expression_Lambda.doLambdaCall(aLambdaValue, list{acc, elem}, accessors, reducer) ) } @@ -68,10 +64,8 @@ module Internals = { accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t, ) => { - aValueArray->Belt.Array.reduceReverse(Ok(initialValue), (rAcc, elem) => - rAcc->Belt.Result.flatMap(acc => - Reducer_Expression_Lambda.doLambdaCall(aLambdaValue, list{acc, elem}, accessors, reducer) - ) + aValueArray->Belt.Array.reduceReverse(initialValue, (acc, elem) => + Reducer_Expression_Lambda.doLambdaCall(aLambdaValue, list{acc, elem}, accessors, reducer) ) } @@ -81,25 +75,19 @@ module Internals = { accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t, ) => { - let rMappedList = aValueArray->Belt.Array.reduceReverse(Ok(list{}), (rAcc, elem) => - rAcc->E.R.bind(_, acc => { - let rNewElem = Reducer_Expression_Lambda.doLambdaCall( - aLambdaValue, - list{elem}, - accessors, - reducer, - ) - rNewElem->E.R2.fmap(newElem => { - switch newElem { - | IEvBool(true) => list{elem, ...acc} - | _ => acc - } - }) - }) - ) - let result = - rMappedList->E.R2.fmap(mappedList => mappedList->Belt.List.toArray->Wrappers.evArray) - result + let mappedList = aValueArray->Belt.Array.reduceReverse(list{}, (acc, elem) => { + let newElem = Reducer_Expression_Lambda.doLambdaCall( + aLambdaValue, + list{elem}, + accessors, + reducer, + ) + switch newElem { + | IEvBool(true) => list{elem, ...acc} + | _ => acc + } + }) + mappedList->Belt.List.toArray->Wrappers.evArray } } @@ -216,7 +204,7 @@ let library = [ ~run=(inputs, _, accessors: ProjectAccessorsT.t, reducer) => switch inputs { | [IEvArray(array), IEvLambda(lambda)] => - Internals.map(array, accessors, lambda, reducer)->E.R2.errMap(_ => "Error!") + Ok(Internals.map(array, accessors, lambda, reducer)) | _ => Error(impossibleError) }, (), @@ -236,9 +224,7 @@ let library = [ ~run=(inputs, _, accessors: ProjectAccessorsT.t, reducer) => switch inputs { | [IEvArray(array), initialValue, IEvLambda(lambda)] => - Internals.reduce(array, initialValue, lambda, accessors, reducer)->E.R2.errMap(_ => - "Error!" - ) + Ok(Internals.reduce(array, initialValue, lambda, accessors, reducer)) | _ => Error(impossibleError) }, (), @@ -258,13 +244,13 @@ let library = [ ~run=(inputs, _, accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t) => switch inputs { | [IEvArray(array), initialValue, IEvLambda(lambda)] => - Internals.reduceReverse( + Ok(Internals.reduceReverse( array, initialValue, lambda, accessors, reducer, - )->E.R2.errMap(_ => "Error!") + )) | _ => Error(impossibleError) }, (), @@ -284,7 +270,7 @@ let library = [ ~run=(inputs, _, accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t) => switch inputs { | [IEvArray(array), IEvLambda(lambda)] => - Internals.filter(array, lambda, accessors, reducer)->E.R2.errMap(_ => "Error!") + Ok(Internals.filter(array, lambda, accessors, reducer)) | _ => Error(impossibleError) }, (), diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res index 0d950ce5..ccc9a4a4 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Pointset.res @@ -37,7 +37,7 @@ module Internal = { let doLambdaCall = (aLambdaValue, list, environment, reducer) => switch Reducer_Expression_Lambda.doLambdaCall(aLambdaValue, list, environment, reducer) { - | Ok(IEvNumber(f)) => Ok(f) + | IEvNumber(f) => Ok(f) | _ => Error(Operation.SampleMapNeedsNtoNFunction) } diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res index c3f56458..7d724a0f 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res @@ -16,7 +16,7 @@ module Internal = { reducer: ProjectReducerFnT.t, ) => switch Reducer_Expression_Lambda.doLambdaCall(aLambdaValue, list, accessors, reducer) { - | Ok(IEvNumber(f)) => Ok(f) + | IEvNumber(f) => Ok(f) | _ => Error(Operation.SampleMapNeedsNtoNFunction) } 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 bec4a0da..4e0b60e7 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 @@ -13,6 +13,7 @@ module TypeBuilder = Reducer_Type_TypeBuilder open ReducerInterface_InternalExpressionValue open Reducer_ErrorValue + /* MathJs provides default implementations for built-ins This is where all the expected built-ins like + = * / sin cos log ln etc are handled @@ -111,7 +112,7 @@ let callInternal = ( module SampleMap = { let doLambdaCall = (aLambdaValue, list) => switch Lambda.doLambdaCall(aLambdaValue, list, accessors, reducer) { - | Ok(IEvNumber(f)) => Ok(f) + | IEvNumber(f) => Ok(f) | _ => Error(Operation.SampleMapNeedsNtoNFunction) } @@ -201,13 +202,17 @@ let dispatch = ( call: functionCall, accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t, -): result => +): internalExpressionValue => try { let (fn, args) = call // There is a bug that prevents string match in patterns // So we have to recreate a copy of the string - ExternalLibrary.dispatch((Js.String.make(fn), args), accessors, reducer, callInternal) + switch ExternalLibrary.dispatch((Js.String.make(fn), args), accessors, reducer, callInternal) { + | Ok(v) => v + | Error(e) => raise(ErrorException(e)) + } } catch { - | Js.Exn.Error(obj) => REJavaScriptExn(Js.Exn.message(obj), Js.Exn.name(obj))->Error - | _ => RETodo("unhandled rescript exception")->Error + | ErrorException(e) => raise(ErrorException(e)) + | Js.Exn.Error(obj) => raise(ErrorException(REJavaScriptExn(Js.Exn.message(obj), Js.Exn.name(obj)))) + | _ => raise(ErrorException(RETodo("unhandled rescript exception"))) } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res index 978ca399..25f70fd8 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res @@ -12,11 +12,10 @@ module ExpressionWithContext = Reducer_ExpressionWithContext module InternalExpressionValue = ReducerInterface_InternalExpressionValue module ProjectAccessorsT = ReducerProject_ProjectAccessors_T module ProjectReducerFnT = ReducerProject_ReducerFn_T -module Result = Belt.Result open Reducer_Expression_ExpressionBuilder -type errorValue = ErrorValue.errorValue +exception ErrorException = ErrorValue.ErrorException type expression = ExpressionT.expression type expressionWithContext = ExpressionWithContext.expressionWithContext @@ -25,21 +24,18 @@ let dispatchMacroCall = ( bindings: ExpressionT.bindings, accessors: ProjectAccessorsT.t, reduceExpression: ProjectReducerFnT.t, -): result => { +): expressionWithContext => { let useExpressionToSetBindings = (bindingExpr: expression, accessors, statement, newCode) => { - let rExternalBindingsValue = reduceExpression(bindingExpr, bindings, accessors) + let nameSpaceValue = reduceExpression(bindingExpr, bindings, accessors) - rExternalBindingsValue->Result.flatMap(nameSpaceValue => { - let newBindings = Bindings.fromExpressionValue(nameSpaceValue) + let newBindings = Bindings.fromExpressionValue(nameSpaceValue) - let rNewStatement = BindingsReplacer.replaceSymbols(newBindings, statement) - rNewStatement->Result.map(boundStatement => - ExpressionWithContext.withContext( - newCode(newBindings->eModule, boundStatement), - newBindings, - ) - ) - }) + let boundStatement = BindingsReplacer.replaceSymbols(newBindings, statement) + + ExpressionWithContext.withContext( + newCode(newBindings->eModule, boundStatement), + newBindings, + ) } let correspondingSetBindingsFn = (fnName: string): string => @@ -52,7 +48,7 @@ let dispatchMacroCall = ( } let doBindStatement = (bindingExpr: expression, statement: expression, accessors) => { - let defaultStatement = ErrorValue.REAssignmentExpected->Error + let defaultStatement = ErrorValue.REAssignmentExpected->ErrorException switch statement { | ExpressionT.EList(list{ExpressionT.EValue(IEvCall(callName)), symbolExpr, statement}) => { let setBindingsFn = correspondingSetBindingsFn(callName) @@ -62,17 +58,14 @@ let dispatchMacroCall = ( boundStatement, ) => eFunction(setBindingsFn, list{newBindingsExpr, symbolExpr, boundStatement})) } else { - defaultStatement + raise(defaultStatement) } } - | _ => defaultStatement + | _ => raise(defaultStatement) } } - let doBindExpression = (bindingExpr: expression, statement: expression, accessors): result< - expressionWithContext, - errorValue, - > => { + let doBindExpression = (bindingExpr: expression, statement: expression, accessors): expressionWithContext => { let defaultStatement = () => useExpressionToSetBindings(bindingExpr, accessors, statement, ( _newBindingsExpr, @@ -100,10 +93,7 @@ let dispatchMacroCall = ( } } - let doBlock = (exprs: list, _bindings: ExpressionT.bindings, _accessors): result< - expressionWithContext, - errorValue, - > => { + let doBlock = (exprs: list, _bindings: ExpressionT.bindings, _accessors): expressionWithContext => { let exprsArray = Belt.List.toArray(exprs) let maxIndex = Js.Array2.length(exprsArray) - 1 let newStatement = exprsArray->Js.Array2.reducei((acc, statement, index) => @@ -119,14 +109,14 @@ let dispatchMacroCall = ( eBindStatement(acc, statement) } , eSymbol("undefined block")) - ExpressionWithContext.noContext(newStatement)->Ok + ExpressionWithContext.noContext(newStatement) } let doLambdaDefinition = ( bindings: ExpressionT.bindings, parameters: array, lambdaDefinition: ExpressionT.expression, - ) => ExpressionWithContext.noContext(eLambda(parameters, bindings, lambdaDefinition))->Ok + ) => ExpressionWithContext.noContext(eLambda(parameters, bindings, lambdaDefinition)) let doTernary = ( condition: expression, @@ -134,28 +124,25 @@ let dispatchMacroCall = ( ifFalse: expression, bindings: ExpressionT.bindings, accessors, - ): result => { + ): expressionWithContext => { let blockCondition = ExpressionBuilder.eBlock(list{condition}) - let rCondition = reduceExpression(blockCondition, bindings, accessors) - rCondition->Result.flatMap(conditionValue => - switch conditionValue { - | InternalExpressionValue.IEvBool(false) => { - let ifFalseBlock = eBlock(list{ifFalse}) - ExpressionWithContext.withContext(ifFalseBlock, bindings)->Ok - } - | InternalExpressionValue.IEvBool(true) => { - let ifTrueBlock = eBlock(list{ifTrue}) - ExpressionWithContext.withContext(ifTrueBlock, bindings)->Ok - } - | _ => REExpectedType("Boolean", "")->Error + let conditionValue = reduceExpression(blockCondition, bindings, accessors) + + switch conditionValue { + | InternalExpressionValue.IEvBool(false) => { + let ifFalseBlock = eBlock(list{ifFalse}) + ExpressionWithContext.withContext(ifFalseBlock, bindings) } - ) + | InternalExpressionValue.IEvBool(true) => { + let ifTrueBlock = eBlock(list{ifTrue}) + ExpressionWithContext.withContext(ifTrueBlock, bindings) + } + | _ => raise(ErrorException(REExpectedType("Boolean", ""))) + } } - let expandExpressionList = (aList, bindings: ExpressionT.bindings, accessors): result< - expressionWithContext, - errorValue, - > => + let expandExpressionList = (aList, bindings: ExpressionT.bindings, accessors): expressionWithContext + => switch aList { | list{ ExpressionT.EValue(IEvCall("$$_bindStatement_$$")), @@ -185,11 +172,11 @@ let dispatchMacroCall = ( doLambdaDefinition(bindings, parameters, lambdaDefinition) | list{ExpressionT.EValue(IEvCall("$$_ternary_$$")), condition, ifTrue, ifFalse} => doTernary(condition, ifTrue, ifFalse, bindings, accessors) - | _ => ExpressionWithContext.noContext(ExpressionT.EList(aList))->Ok + | _ => ExpressionWithContext.noContext(ExpressionT.EList(aList)) } switch macroExpression { | EList(aList) => expandExpressionList(aList, bindings, accessors) - | _ => ExpressionWithContext.noContext(macroExpression)->Ok + | _ => ExpressionWithContext.noContext(macroExpression) } } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res index 46da2216..e6404b18 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res @@ -26,6 +26,8 @@ type errorValue = type t = errorValue +exception ErrorException(errorValue) + let errorToString = err => switch err { | REArityError(_oFnName, arity, usedArity) => 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 f2b05036..4950ffaa 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 @@ -14,6 +14,8 @@ module T = Reducer_Expression_T type errorValue = Reducer_ErrorValue.errorValue type t = T.t +exception ErrorException = Reducer_ErrorValue.ErrorException + /* Recursively evaluate/reduce the expression (Lisp AST/Lambda calculus) */ @@ -21,10 +23,10 @@ let rec reduceExpressionInProject = ( expression: t, continuation: T.bindings, accessors: ProjectAccessorsT.t, -): result => { +): InternalExpressionValue.t => { // Js.log(`reduce: ${T.toString(expression)} bindings: ${bindings->Bindings.toString}`) switch expression { - | T.EValue(value) => value->Ok + | T.EValue(value) => value | T.EList(list) => switch list { | list{EValue(IEvCall(fName)), ..._args} => @@ -41,20 +43,13 @@ and reduceExpressionList = ( expressions: list, continuation: T.bindings, accessors: ProjectAccessorsT.t, -): result => { - let racc: result< - list, - 'e, - > = expressions->Belt.List.reduceReverse(Ok(list{}), (racc, each: t) => - racc->Result.flatMap(acc => { - each - ->reduceExpressionInProject(continuation, accessors) - ->Result.map(newNode => { - acc->Belt.List.add(newNode) - }) - }) +): InternalExpressionValue.t => { + let acc: list = expressions->Belt.List.reduceReverse(list{}, (acc, each: t) => + acc->Belt.List.add( + each->reduceExpressionInProject(continuation, accessors) + ) ) - racc->Result.flatMap(acc => acc->reduceValueList(accessors)) + acc->reduceValueList(accessors) } /* @@ -63,48 +58,39 @@ and reduceExpressionList = ( and reduceValueList = ( valueList: list, accessors: ProjectAccessorsT.t, -): result => +): InternalExpressionValue.t => switch valueList { | list{IEvCall(fName), ...args} => { - let rCheckedArgs = switch fName { - | "$_setBindings_$" | "$_setTypeOfBindings_$" | "$_setTypeAliasBindings_$" => args->Ok + let checkedArgs = switch fName { + | "$_setBindings_$" | "$_setTypeOfBindings_$" | "$_setTypeAliasBindings_$" => args | _ => args->Lambda.checkIfReduced } - rCheckedArgs->Result.flatMap(checkedArgs => - (fName, checkedArgs->Belt.List.toArray)->BuiltIn.dispatch( - accessors, - reduceExpressionInProject, - ) + (fName, checkedArgs->Belt.List.toArray)->BuiltIn.dispatch( + accessors, + reduceExpressionInProject, ) } | list{IEvLambda(_)} => // TODO: remove on solving issue#558 valueList ->Lambda.checkIfReduced - ->Result.flatMap(reducedValueList => - reducedValueList->Belt.List.toArray->InternalExpressionValue.IEvArray->Ok - ) + ->Belt.List.toArray->InternalExpressionValue.IEvArray | list{IEvLambda(lambdaCall), ...args} => args ->Lambda.checkIfReduced - ->Result.flatMap(checkedArgs => - Lambda.doLambdaCall(lambdaCall, checkedArgs, accessors, reduceExpressionInProject) - ) - + ->Lambda.doLambdaCall(lambdaCall, _, accessors, reduceExpressionInProject) | _ => valueList ->Lambda.checkIfReduced - ->Result.flatMap(reducedValueList => - reducedValueList->Belt.List.toArray->InternalExpressionValue.IEvArray->Ok - ) + ->Belt.List.toArray->InternalExpressionValue.IEvArray } let reduceReturningBindings = ( expression: t, continuation: T.bindings, accessors: ProjectAccessorsT.t, -): (result, T.bindings) => { +): (InternalExpressionValue.t, T.bindings) => { let states = accessors.states let result = reduceExpressionInProject(expression, continuation, accessors) (result, states.continuation) @@ -118,7 +104,12 @@ module BackCompatible = { let evaluate = (expression: t): result => { let accessors = ProjectAccessorsT.identityAccessors - expression->reduceExpressionInProject(accessors.stdLib, accessors) + try { + expression->reduceExpressionInProject(accessors.stdLib, accessors)->Ok + } catch { + | ErrorException(e) => Error(e) + | _ => raise(ErrorException(RETodo("internal exception"))) + } } let evaluateString = (peggyCode: string): result => diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_ExpressionWithContext.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_ExpressionWithContext.res index 808a2dcd..23b7e2c8 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_ExpressionWithContext.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_ExpressionWithContext.res @@ -23,7 +23,7 @@ let callReducer = ( bindings: bindings, accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t, -): result => { +): internalExpressionValue => { switch expressionWithContext { | ExpressionNoContext(expr) => // Js.log(`callReducer: bindings ${Bindings.toString(bindings)} expr ${ExpressionT.toString(expr)}`) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res index 6e38f833..09bd8ae8 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res @@ -1,7 +1,6 @@ module ErrorValue = Reducer_ErrorValue module ExpressionT = Reducer_Expression_T module InternalExpressionValue = ReducerInterface_InternalExpressionValue -module Result = Belt.Result module Bindings = Reducer_Bindings type errorValue = Reducer_ErrorValue.errorValue @@ -10,19 +9,16 @@ type internalExpressionValue = InternalExpressionValue.t let isMacroName = (fName: string): bool => fName->Js.String2.startsWith("$$") -let rec replaceSymbols = (bindings: ExpressionT.bindings, expression: expression): result< - expression, - errorValue, -> => +let rec replaceSymbols = (bindings: ExpressionT.bindings, expression: expression): expression => switch expression { | ExpressionT.EValue(value) => - replaceSymbolOnValue(bindings, value)->Result.map(evValue => evValue->ExpressionT.EValue) + replaceSymbolOnValue(bindings, value)->ExpressionT.EValue | ExpressionT.EList(list) => switch list { | list{EValue(IEvCall(fName)), ..._args} => switch isMacroName(fName) { // A macro reduces itself so we dont dive in it - | true => expression->Ok + | true => expression | false => replaceSymbolsOnExpressionList(bindings, list) } | _ => replaceSymbolsOnExpressionList(bindings, list) @@ -30,23 +26,19 @@ let rec replaceSymbols = (bindings: ExpressionT.bindings, expression: expression } and replaceSymbolsOnExpressionList = (bindings, list) => { - let racc = list->Belt.List.reduceReverse(Ok(list{}), (racc, each: expression) => - racc->Result.flatMap(acc => { - replaceSymbols(bindings, each)->Result.flatMap(newNode => { - acc->Belt.List.add(newNode)->Ok - }) - }) + let racc = list->Belt.List.reduceReverse(list{}, (acc, each: expression) => + replaceSymbols(bindings, each)->Belt.List.add(acc, _) ) - racc->Result.map(acc => acc->ExpressionT.EList) + ExpressionT.EList(racc) } and replaceSymbolOnValue = (bindings, evValue: internalExpressionValue) => switch evValue { - | IEvSymbol(symbol) => Bindings.getWithDefault(bindings, symbol, evValue)->Ok + | IEvSymbol(symbol) => Bindings.getWithDefault(bindings, symbol, evValue) | IEvCall(symbol) => Bindings.getWithDefault(bindings, symbol, evValue)->checkIfCallable - | _ => evValue->Ok + | _ => evValue } and checkIfCallable = (evValue: internalExpressionValue) => switch evValue { - | IEvCall(_) | IEvLambda(_) => evValue->Ok - | _ => ErrorValue.RENotAFunction(InternalExpressionValue.toString(evValue))->Error + | IEvCall(_) | IEvLambda(_) => evValue + | _ => raise(ErrorValue.ErrorException(ErrorValue.RENotAFunction(InternalExpressionValue.toString(evValue)))) } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res index 5ab7761d..8864c4d8 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res @@ -23,26 +23,24 @@ let checkArity = ( let argsLength = Belt.List.length(args) let parametersLength = Js.Array2.length(lambdaValue.parameters) if argsLength !== parametersLength { - ErrorValue.REArityError(None, parametersLength, argsLength)->Error + raise(ErrorValue.ErrorException(ErrorValue.REArityError(None, parametersLength, argsLength))) } else { - args->Ok + args } } let exprOrFFI = castInternalCodeToExpression(lambdaValue.body) switch exprOrFFI { | NotFFI(_) => reallyCheck - | FFI(_) => args->Ok + | FFI(_) => args } } let checkIfReduced = (args: list) => - args->Belt.List.reduceReverse(Ok(list{}), (rAcc, arg) => - rAcc->Result.flatMap(acc => + args->Belt.List.reduceReverse(list{}, (acc, arg) => switch arg { - | IEvSymbol(symbol) => ErrorValue.RESymbolNotFound(symbol)->Error - | _ => list{arg, ...acc}->Ok + | IEvSymbol(symbol) => raise(ErrorValue.ErrorException(ErrorValue.RESymbolNotFound(symbol))) + | _ => list{arg, ...acc} } - ) ) let caseNotFFI = ( @@ -63,7 +61,10 @@ let caseNotFFI = ( } let caseFFI = (ffiFn: ExpressionT.ffiFn, args, accessors: ProjectAccessorsT.t) => { - ffiFn(args->Belt.List.toArray, accessors.environment) + switch ffiFn(args->Belt.List.toArray, accessors.environment) { + | Ok(value) => value + | Error(value) => raise(ErrorValue.ErrorException(value)) + } } let applyParametersToLambda = ( @@ -71,16 +72,13 @@ let applyParametersToLambda = ( args, accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t, -): result => { - checkArity(lambdaValue, args)->Result.flatMap(args => - checkIfReduced(args)->Result.flatMap(args => { - let exprOrFFI = castInternalCodeToExpression(lambdaValue.body) - switch exprOrFFI { - | NotFFI(expr) => caseNotFFI(lambdaValue, expr, args, accessors, reducer) - | FFI(ffiFn) => caseFFI(ffiFn, args, accessors) - } - }) - ) +): internalExpressionValue => { + let args = checkArity(lambdaValue, args)->checkIfReduced + let exprOrFFI = castInternalCodeToExpression(lambdaValue.body) + switch exprOrFFI { + | NotFFI(expr) => caseNotFFI(lambdaValue, expr, args, accessors, reducer) + | FFI(ffiFn) => caseFFI(ffiFn, args, accessors) + } } let doLambdaCall = ( @@ -95,7 +93,7 @@ let foreignFunctionInterface = ( argArray: array, accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t, -): result => { +): internalExpressionValue => { let args = argArray->Belt.List.fromArray applyParametersToLambda(lambdaValue, args, accessors, reducer) } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Macro.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Macro.res index 003d3170..7426be18 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Macro.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Macro.res @@ -10,32 +10,17 @@ type expression = ExpressionT.expression type internalExpressionValue = InternalExpressionValue.t type expressionWithContext = ExpressionWithContext.expressionWithContext -let expandMacroCall = ( - macroExpression: expression, - bindings: ExpressionT.bindings, - accessors: ProjectAccessorsT.t, - reduceExpression: ProjectReducerFnT.t, -): result => - Reducer_Dispatch_BuiltInMacros.dispatchMacroCall( - macroExpression, - bindings, - accessors, - reduceExpression, - ) - let doMacroCall = ( macroExpression: expression, bindings: ExpressionT.bindings, accessors: ProjectAccessorsT.t, reduceExpression: ProjectReducerFnT.t, -): result => - expandMacroCall( +): internalExpressionValue => + Reducer_Dispatch_BuiltInMacros.dispatchMacroCall( macroExpression, bindings, (accessors: ProjectAccessorsT.t), (reduceExpression: ProjectReducerFnT.t), - )->Result.flatMap(expressionWithContext => - ExpressionWithContext.callReducer(expressionWithContext, bindings, accessors, reduceExpression) - ) + )->ExpressionWithContext.callReducer(bindings, accessors, reduceExpression) let isMacroName = (fName: string): bool => fName->Js.String2.startsWith("$$") diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Type/Reducer_Type_Compile.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Type/Reducer_Type_Compile.res index 79153801..73696831 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Type/Reducer_Type_Compile.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Type/Reducer_Type_Compile.res @@ -18,13 +18,9 @@ let ievFromTypeExpression = ( let result = reducerFn(expr, Bindings.emptyBindings, accessors) let nameSpace = accessors.states.continuation - switch result { - | Ok(_) => - switch Bindings.getType(nameSpace, sIndex) { - | Some(value) => value->Ok - | None => raise(Reducer_Exception.ImpossibleException("Reducer_Type_Compile-none")) - } - | err => err + switch Bindings.getType(nameSpace, sIndex) { + | Some(value) => value->Ok + | None => raise(Reducer_Exception.ImpossibleException("Reducer_Type_Compile-none")) } }) } diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res index 8eb915de..07210232 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res @@ -183,16 +183,16 @@ let buildExpression = (this: t): t => { } } -let wrappedReducer = ( - rExpression: T.expressionArgumentType, - aContinuation: T.continuation, - accessors: ProjectAccessorsT.t, -): T.resultArgumentType => { - Belt.Result.flatMap( - rExpression, - Reducer_Expression.reduceExpressionInProject(_, aContinuation, accessors), - ) -} +// let wrappedReducer = ( +// rExpression: T.expressionArgumentType, +// aContinuation: T.continuation, +// accessors: ProjectAccessorsT.t, +// ): T.resultArgumentType => { +// Belt.Result.flatMap( +// rExpression, +// Reducer_Expression.reduceExpressionInProject(_, aContinuation, accessors), +// ) +// } let doBuildResult = ( this: t, @@ -204,7 +204,13 @@ let doBuildResult = ( ->Belt.Option.map( Belt.Result.flatMap( _, - Reducer_Expression.reduceExpressionInProject(_, aContinuation, accessors), + expression => + try { + Reducer_Expression.reduceExpressionInProject(expression, aContinuation, accessors)->Ok + } catch { + | Reducer_ErrorValue.ErrorException(e) => e->Error + | _ => RETodo("unhandled rescript exception")->Error + } ), ) diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ReducerFn_T.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ReducerFn_T.res index 0b401fcb..2b948a02 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ReducerFn_T.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ReducerFn_T.res @@ -6,4 +6,4 @@ type t = ( ExpressionT.t, ExpressionT.bindings, ProjectAccessorsT.t, -) => result +) => InternalExpressionValue.t From 06ec2caae03f0c79ef8b4f99539d09660f3a4665 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Wed, 7 Sep 2022 22:50:24 +0400 Subject: [PATCH 2/5] remove unused code --- .../ReducerProject/ReducerProject_ProjectItem.res | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res index 07210232..573d82c1 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res @@ -183,17 +183,6 @@ let buildExpression = (this: t): t => { } } -// let wrappedReducer = ( -// rExpression: T.expressionArgumentType, -// aContinuation: T.continuation, -// accessors: ProjectAccessorsT.t, -// ): T.resultArgumentType => { -// Belt.Result.flatMap( -// rExpression, -// Reducer_Expression.reduceExpressionInProject(_, aContinuation, accessors), -// ) -// } - let doBuildResult = ( this: t, aContinuation: T.continuation, From ecc82ba8f7cab30bc2a4231d097190d328a09c29 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Wed, 7 Sep 2022 23:04:07 +0400 Subject: [PATCH 3/5] reformat for lint --- .../Reducer/Reducer_TestMacroHelpers.res | 12 +- .../FunctionRegistry/Library/FR_Danger.res | 10 +- .../FunctionRegistry/Library/FR_List.res | 14 +- .../Reducer_Dispatch_BuiltIn.res | 4 +- .../Reducer_Dispatch_BuiltInMacros.res | 24 +- .../Reducer_Expression/Reducer_Expression.res | 16 +- .../Reducer_Expression_BindingsReplacer.res | 17 +- .../Reducer_Expression_Lambda.res | 12 +- .../ReducerProject_IncludeParser.js | 298 ++++++++++++------ .../ReducerProject_ProjectItem.res | 4 +- .../ReducerProject_ReducerFn_T.res | 6 +- 11 files changed, 265 insertions(+), 152 deletions(-) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res index 363d33c7..a1a3399e 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res @@ -18,20 +18,16 @@ let testMacro_ = ( ) => { let bindings = Bindings.fromArray(bindArray) tester(expr->T.toString, () => { - let result = switch expr - ->Reducer_Dispatch_BuiltInMacros.dispatchMacroCall( + let result = switch expr->Reducer_Dispatch_BuiltInMacros.dispatchMacroCall( bindings, ProjectAccessorsT.identityAccessors, Expression.reduceExpressionInProject, ) { - | v => Ok(v) - | exception Reducer_ErrorValue.ErrorException(e) => Error(e) + | v => Ok(v) + | exception Reducer_ErrorValue.ErrorException(e) => Error(e) } - result - ->ExpressionWithContext.toStringResult - ->expect - ->toEqual(expectedCode) + result->ExpressionWithContext.toStringResult->expect->toEqual(expectedCode) }) } diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res index df3a91de..2fcfcdaf 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res @@ -261,13 +261,13 @@ module DiminishingReturns = { This is currently being done with a reducer, that keeps track of: - Value of marginal spending for each function - How much has been assigned to each function. - */ + */ /* Two possible algorithms (n=funds/increment, m=num lambdas) 1. O(n): Iterate through value on next n dollars. At each step, only compute the new marginal return of the function which is spent. (This is what we are doing.) 2. O(n*(m-1)): Iterate through all possible spending combinations. The advantage of this option is that it wouldn't assume that the returns of marginal spending are diminishing. - */ + */ let optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions = ( lambdas, funds, @@ -275,7 +275,6 @@ module DiminishingReturns = { environment, reducer, ) => { - switch ( E.A.length(lambdas) > 1, funds > 0.0, @@ -310,7 +309,8 @@ module DiminishingReturns = { ) switch resultAsInternalExpression { | IEvNumber(x) => Ok(x) - | _ => Error( + | _ => + Error( "Error 1 in Danger.optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions. It's possible that your function doesn't return a number, try definining auxiliaryFunction(x) = mean(yourFunction(x)) and integrate auxiliaryFunction instead", ) } @@ -452,5 +452,5 @@ let library = [ // will only depend on num points and the complexity of the function // Diminishing marginal return functions - DiminishingReturns.Lib.optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions + DiminishingReturns.Lib.optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions, ] diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res index 733c2f86..1def1e36 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_List.res @@ -41,7 +41,7 @@ module Internals = { (reducer: ProjectReducerFnT.t), ) list{newElem, ...acc} - }) + }) mappedList->Belt.List.toArray->Wrappers.evArray } @@ -83,8 +83,8 @@ module Internals = { reducer, ) switch newElem { - | IEvBool(true) => list{elem, ...acc} - | _ => acc + | IEvBool(true) => list{elem, ...acc} + | _ => acc } }) mappedList->Belt.List.toArray->Wrappers.evArray @@ -244,13 +244,7 @@ let library = [ ~run=(inputs, _, accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t) => switch inputs { | [IEvArray(array), initialValue, IEvLambda(lambda)] => - Ok(Internals.reduceReverse( - array, - initialValue, - lambda, - accessors, - reducer, - )) + Ok(Internals.reduceReverse(array, initialValue, lambda, accessors, reducer)) | _ => Error(impossibleError) }, (), 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 4e0b60e7..b7fed530 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 @@ -13,7 +13,6 @@ module TypeBuilder = Reducer_Type_TypeBuilder open ReducerInterface_InternalExpressionValue open Reducer_ErrorValue - /* MathJs provides default implementations for built-ins This is where all the expected built-ins like + = * / sin cos log ln etc are handled @@ -213,6 +212,7 @@ let dispatch = ( } } catch { | ErrorException(e) => raise(ErrorException(e)) - | Js.Exn.Error(obj) => raise(ErrorException(REJavaScriptExn(Js.Exn.message(obj), Js.Exn.name(obj)))) + | Js.Exn.Error(obj) => + raise(ErrorException(REJavaScriptExn(Js.Exn.message(obj), Js.Exn.name(obj)))) | _ => raise(ErrorException(RETodo("unhandled rescript exception"))) } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res index 25f70fd8..67ed732c 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res @@ -32,10 +32,7 @@ let dispatchMacroCall = ( let boundStatement = BindingsReplacer.replaceSymbols(newBindings, statement) - ExpressionWithContext.withContext( - newCode(newBindings->eModule, boundStatement), - newBindings, - ) + ExpressionWithContext.withContext(newCode(newBindings->eModule, boundStatement), newBindings) } let correspondingSetBindingsFn = (fnName: string): string => @@ -65,7 +62,11 @@ let dispatchMacroCall = ( } } - let doBindExpression = (bindingExpr: expression, statement: expression, accessors): expressionWithContext => { + let doBindExpression = ( + bindingExpr: expression, + statement: expression, + accessors, + ): expressionWithContext => { let defaultStatement = () => useExpressionToSetBindings(bindingExpr, accessors, statement, ( _newBindingsExpr, @@ -93,7 +94,11 @@ let dispatchMacroCall = ( } } - let doBlock = (exprs: list, _bindings: ExpressionT.bindings, _accessors): expressionWithContext => { + let doBlock = ( + exprs: list, + _bindings: ExpressionT.bindings, + _accessors, + ): expressionWithContext => { let exprsArray = Belt.List.toArray(exprs) let maxIndex = Js.Array2.length(exprsArray) - 1 let newStatement = exprsArray->Js.Array2.reducei((acc, statement, index) => @@ -141,8 +146,11 @@ let dispatchMacroCall = ( } } - let expandExpressionList = (aList, bindings: ExpressionT.bindings, accessors): expressionWithContext - => + let expandExpressionList = ( + aList, + bindings: ExpressionT.bindings, + accessors, + ): expressionWithContext => switch aList { | list{ ExpressionT.EValue(IEvCall("$$_bindStatement_$$")), 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 4950ffaa..4b957e33 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 @@ -44,11 +44,10 @@ and reduceExpressionList = ( continuation: T.bindings, accessors: ProjectAccessorsT.t, ): InternalExpressionValue.t => { - let acc: list = expressions->Belt.List.reduceReverse(list{}, (acc, each: t) => - acc->Belt.List.add( - each->reduceExpressionInProject(continuation, accessors) + let acc: list = + expressions->Belt.List.reduceReverse(list{}, (acc, each: t) => + acc->Belt.List.add(each->reduceExpressionInProject(continuation, accessors)) ) - ) acc->reduceValueList(accessors) } @@ -73,17 +72,12 @@ and reduceValueList = ( } | list{IEvLambda(_)} => // TODO: remove on solving issue#558 - valueList - ->Lambda.checkIfReduced - ->Belt.List.toArray->InternalExpressionValue.IEvArray + valueList->Lambda.checkIfReduced->Belt.List.toArray->InternalExpressionValue.IEvArray | list{IEvLambda(lambdaCall), ...args} => args ->Lambda.checkIfReduced ->Lambda.doLambdaCall(lambdaCall, _, accessors, reduceExpressionInProject) - | _ => - valueList - ->Lambda.checkIfReduced - ->Belt.List.toArray->InternalExpressionValue.IEvArray + | _ => valueList->Lambda.checkIfReduced->Belt.List.toArray->InternalExpressionValue.IEvArray } let reduceReturningBindings = ( diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res index 09bd8ae8..8a729405 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res @@ -11,8 +11,7 @@ let isMacroName = (fName: string): bool => fName->Js.String2.startsWith("$$") let rec replaceSymbols = (bindings: ExpressionT.bindings, expression: expression): expression => switch expression { - | ExpressionT.EValue(value) => - replaceSymbolOnValue(bindings, value)->ExpressionT.EValue + | ExpressionT.EValue(value) => replaceSymbolOnValue(bindings, value)->ExpressionT.EValue | ExpressionT.EList(list) => switch list { | list{EValue(IEvCall(fName)), ..._args} => @@ -26,9 +25,10 @@ let rec replaceSymbols = (bindings: ExpressionT.bindings, expression: expression } and replaceSymbolsOnExpressionList = (bindings, list) => { - let racc = list->Belt.List.reduceReverse(list{}, (acc, each: expression) => - replaceSymbols(bindings, each)->Belt.List.add(acc, _) - ) + let racc = + list->Belt.List.reduceReverse(list{}, (acc, each: expression) => + replaceSymbols(bindings, each)->Belt.List.add(acc, _) + ) ExpressionT.EList(racc) } and replaceSymbolOnValue = (bindings, evValue: internalExpressionValue) => @@ -40,5 +40,10 @@ and replaceSymbolOnValue = (bindings, evValue: internalExpressionValue) => and checkIfCallable = (evValue: internalExpressionValue) => switch evValue { | IEvCall(_) | IEvLambda(_) => evValue - | _ => raise(ErrorValue.ErrorException(ErrorValue.RENotAFunction(InternalExpressionValue.toString(evValue)))) + | _ => + raise( + ErrorValue.ErrorException( + ErrorValue.RENotAFunction(InternalExpressionValue.toString(evValue)), + ), + ) } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res index 8864c4d8..c4ef011e 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res @@ -37,10 +37,10 @@ let checkArity = ( let checkIfReduced = (args: list) => args->Belt.List.reduceReverse(list{}, (acc, arg) => - switch arg { - | IEvSymbol(symbol) => raise(ErrorValue.ErrorException(ErrorValue.RESymbolNotFound(symbol))) - | _ => list{arg, ...acc} - } + switch arg { + | IEvSymbol(symbol) => raise(ErrorValue.ErrorException(ErrorValue.RESymbolNotFound(symbol))) + | _ => list{arg, ...acc} + } ) let caseNotFFI = ( @@ -62,8 +62,8 @@ let caseNotFFI = ( let caseFFI = (ffiFn: ExpressionT.ffiFn, args, accessors: ProjectAccessorsT.t) => { switch ffiFn(args->Belt.List.toArray, accessors.environment) { - | Ok(value) => value - | Error(value) => raise(ErrorValue.ErrorException(value)) + | Ok(value) => value + | Error(value) => raise(ErrorValue.ErrorException(value)) } } diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_IncludeParser.js b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_IncludeParser.js index 7f9c0418..06e710d6 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_IncludeParser.js +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_IncludeParser.js @@ -5,7 +5,9 @@ "use strict"; function peg$subclass(child, parent) { - function C() { this.constructor = child; } + function C() { + this.constructor = child; + } C.prototype = parent.prototype; child.prototype = new C(); } @@ -27,13 +29,15 @@ peg$subclass(peg$SyntaxError, Error); function peg$padEnd(str, targetLength, padString) { padString = padString || " "; - if (str.length > targetLength) { return str; } + if (str.length > targetLength) { + return str; + } targetLength -= str.length; padString += padString.repeat(targetLength); return str + padString.slice(0, targetLength); } -peg$SyntaxError.prototype.format = function(sources) { +peg$SyntaxError.prototype.format = function (sources) { var str = "Error: " + this.message; if (this.location) { var src = null; @@ -48,15 +52,24 @@ peg$SyntaxError.prototype.format = function(sources) { var loc = this.location.source + ":" + s.line + ":" + s.column; if (src) { var e = this.location.end; - var filler = peg$padEnd("", s.line.toString().length, ' '); + var filler = peg$padEnd("", s.line.toString().length, " "); var line = src[s.line - 1]; var last = s.line === e.line ? e.column : line.length + 1; - var hatLen = (last - s.column) || 1; - str += "\n --> " + loc + "\n" - + filler + " |\n" - + s.line + " | " + line + "\n" - + filler + " | " + peg$padEnd("", s.column - 1, ' ') - + peg$padEnd("", hatLen, "^"); + var hatLen = last - s.column || 1; + str += + "\n --> " + + loc + + "\n" + + filler + + " |\n" + + s.line + + " | " + + line + + "\n" + + filler + + " | " + + peg$padEnd("", s.column - 1, " ") + + peg$padEnd("", hatLen, "^"); } else { str += "\n at " + loc; } @@ -64,33 +77,35 @@ peg$SyntaxError.prototype.format = function(sources) { return str; }; -peg$SyntaxError.buildMessage = function(expected, found) { +peg$SyntaxError.buildMessage = function (expected, found) { var DESCRIBE_EXPECTATION_FNS = { - literal: function(expectation) { - return "\"" + literalEscape(expectation.text) + "\""; + literal: function (expectation) { + return '"' + literalEscape(expectation.text) + '"'; }, - class: function(expectation) { - var escapedParts = expectation.parts.map(function(part) { + class: function (expectation) { + var escapedParts = expectation.parts.map(function (part) { return Array.isArray(part) ? classEscape(part[0]) + "-" + classEscape(part[1]) : classEscape(part); }); - return "[" + (expectation.inverted ? "^" : "") + escapedParts.join("") + "]"; + return ( + "[" + (expectation.inverted ? "^" : "") + escapedParts.join("") + "]" + ); }, - any: function() { + any: function () { return "any character"; }, - end: function() { + end: function () { return "end of input"; }, - other: function(expectation) { + other: function (expectation) { return expectation.description; - } + }, }; function hex(ch) { @@ -100,13 +115,17 @@ peg$SyntaxError.buildMessage = function(expected, found) { function literalEscape(s) { return s .replace(/\\/g, "\\\\") - .replace(/"/g, "\\\"") + .replace(/"/g, '\\"') .replace(/\0/g, "\\0") .replace(/\t/g, "\\t") .replace(/\n/g, "\\n") .replace(/\r/g, "\\r") - .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); + .replace(/[\x00-\x0F]/g, function (ch) { + return "\\x0" + hex(ch); + }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) { + return "\\x" + hex(ch); + }); } function classEscape(s) { @@ -114,13 +133,17 @@ peg$SyntaxError.buildMessage = function(expected, found) { .replace(/\\/g, "\\\\") .replace(/\]/g, "\\]") .replace(/\^/g, "\\^") - .replace(/-/g, "\\-") + .replace(/-/g, "\\-") .replace(/\0/g, "\\0") .replace(/\t/g, "\\t") .replace(/\n/g, "\\n") .replace(/\r/g, "\\r") - .replace(/[\x00-\x0F]/g, function(ch) { return "\\x0" + hex(ch); }) - .replace(/[\x10-\x1F\x7F-\x9F]/g, function(ch) { return "\\x" + hex(ch); }); + .replace(/[\x00-\x0F]/g, function (ch) { + return "\\x0" + hex(ch); + }) + .replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) { + return "\\x" + hex(ch); + }); } function describeExpectation(expectation) { @@ -151,17 +174,25 @@ peg$SyntaxError.buildMessage = function(expected, found) { return descriptions[0] + " or " + descriptions[1]; default: - return descriptions.slice(0, -1).join(", ") - + ", or " - + descriptions[descriptions.length - 1]; + return ( + descriptions.slice(0, -1).join(", ") + + ", or " + + descriptions[descriptions.length - 1] + ); } } function describeFound(found) { - return found ? "\"" + literalEscape(found) + "\"" : "end of input"; + return found ? '"' + literalEscape(found) + '"' : "end of input"; } - return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found."; + return ( + "Expected " + + describeExpected(expected) + + " but " + + describeFound(found) + + " found." + ); }; function peg$parse(input, options) { @@ -177,7 +208,7 @@ function peg$parse(input, options) { var peg$c1 = "#include"; var peg$c2 = "as"; var peg$c3 = "'"; - var peg$c4 = "\""; + var peg$c4 = '"'; var peg$c5 = "//"; var peg$c6 = "/*"; var peg$c7 = "*/"; @@ -197,8 +228,8 @@ function peg$parse(input, options) { var peg$e3 = peg$otherExpectation("string"); var peg$e4 = peg$literalExpectation("'", false); var peg$e5 = peg$classExpectation(["'"], true, false); - var peg$e6 = peg$literalExpectation("\"", false); - var peg$e7 = peg$classExpectation(["\""], true, false); + var peg$e6 = peg$literalExpectation('"', false); + var peg$e7 = peg$classExpectation(['"'], true, false); var peg$e8 = peg$otherExpectation("comment"); var peg$e9 = peg$literalExpectation("//", false); var peg$e10 = peg$literalExpectation("/*", false); @@ -212,16 +243,36 @@ function peg$parse(input, options) { var peg$e18 = peg$classExpectation(["\r", "\n"], true, false); var peg$e19 = peg$otherExpectation("identifier"); var peg$e20 = peg$classExpectation(["_", ["a", "z"]], false, false); - var peg$e21 = peg$classExpectation(["_", ["a", "z"], ["0", "9"]], false, true); + var peg$e21 = peg$classExpectation( + ["_", ["a", "z"], ["0", "9"]], + false, + true + ); - var peg$f0 = function(head, tail) {return [head, ...tail].filter( e => e != '');}; - var peg$f1 = function() {return [];}; - var peg$f2 = function(file, variable) {return [!variable ? '' : variable, file]}; - var peg$f3 = function(characters) {return characters.join('');}; - var peg$f4 = function(characters) {return characters.join('');}; - var peg$f5 = function() { return '';}; - var peg$f6 = function() { return '';}; - var peg$f7 = function() {return text();}; + var peg$f0 = function (head, tail) { + return [head, ...tail].filter((e) => e != ""); + }; + var peg$f1 = function () { + return []; + }; + var peg$f2 = function (file, variable) { + return [!variable ? "" : variable, file]; + }; + var peg$f3 = function (characters) { + return characters.join(""); + }; + var peg$f4 = function (characters) { + return characters.join(""); + }; + var peg$f5 = function () { + return ""; + }; + var peg$f6 = function () { + return ""; + }; + var peg$f7 = function () { + return text(); + }; var peg$currPos = 0; var peg$savedPos = 0; var peg$posDetailsCache = [{ line: 1, column: 1 }]; @@ -235,7 +286,9 @@ function peg$parse(input, options) { if ("startRule" in options) { if (!(options.startRule in peg$startRuleFunctions)) { - throw new Error("Can't start parsing from rule \"" + options.startRule + "\"."); + throw new Error( + "Can't start parsing from rule \"" + options.startRule + '".' + ); } peg$startRuleFunction = peg$startRuleFunctions[options.startRule]; @@ -253,7 +306,7 @@ function peg$parse(input, options) { return { source: peg$source, start: peg$savedPos, - end: peg$currPos + end: peg$currPos, }; } @@ -262,9 +315,10 @@ function peg$parse(input, options) { } function expected(description, location) { - location = location !== undefined - ? location - : peg$computeLocation(peg$savedPos, peg$currPos); + location = + location !== undefined + ? location + : peg$computeLocation(peg$savedPos, peg$currPos); throw peg$buildStructuredError( [peg$otherExpectation(description)], @@ -274,9 +328,10 @@ function peg$parse(input, options) { } function error(message, location) { - location = location !== undefined - ? location - : peg$computeLocation(peg$savedPos, peg$currPos); + location = + location !== undefined + ? location + : peg$computeLocation(peg$savedPos, peg$currPos); throw peg$buildSimpleError(message, location); } @@ -286,7 +341,12 @@ function peg$parse(input, options) { } function peg$classExpectation(parts, inverted, ignoreCase) { - return { type: "class", parts: parts, inverted: inverted, ignoreCase: ignoreCase }; + return { + type: "class", + parts: parts, + inverted: inverted, + ignoreCase: ignoreCase, + }; } function peg$anyExpectation() { @@ -316,7 +376,7 @@ function peg$parse(input, options) { details = peg$posDetailsCache[p]; details = { line: details.line, - column: details.column + column: details.column, }; while (p < pos) { @@ -345,18 +405,20 @@ function peg$parse(input, options) { start: { offset: startPos, line: startPosDetails.line, - column: startPosDetails.column + column: startPosDetails.column, }, end: { offset: endPos, line: endPosDetails.line, - column: endPosDetails.column - } + column: endPosDetails.column, + }, }; } function peg$fail(expected) { - if (peg$currPos < peg$maxFailPos) { return; } + if (peg$currPos < peg$maxFailPos) { + return; + } if (peg$currPos > peg$maxFailPos) { peg$maxFailPos = peg$currPos; @@ -516,7 +578,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e0); } + if (peg$silentFails === 0) { + peg$fail(peg$e0); + } } peg$silentFails--; if (s2 === peg$FAILED) { @@ -586,7 +650,9 @@ function peg$parse(input, options) { peg$currPos += 8; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e1); } + if (peg$silentFails === 0) { + peg$fail(peg$e1); + } } if (s2 !== peg$FAILED) { s3 = []; @@ -619,7 +685,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s7 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e2); } + if (peg$silentFails === 0) { + peg$fail(peg$e2); + } } if (s7 !== peg$FAILED) { s8 = []; @@ -716,7 +784,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e4); } + if (peg$silentFails === 0) { + peg$fail(peg$e4); + } } if (s2 !== peg$FAILED) { s3 = []; @@ -725,7 +795,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e5); } + if (peg$silentFails === 0) { + peg$fail(peg$e5); + } } while (s4 !== peg$FAILED) { s3.push(s4); @@ -734,7 +806,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e5); } + if (peg$silentFails === 0) { + peg$fail(peg$e5); + } } } if (input.charCodeAt(peg$currPos) === 39) { @@ -742,7 +816,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e4); } + if (peg$silentFails === 0) { + peg$fail(peg$e4); + } } if (s4 !== peg$FAILED) { s1 = s3; @@ -767,7 +843,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s2 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e6); } + if (peg$silentFails === 0) { + peg$fail(peg$e6); + } } if (s2 !== peg$FAILED) { s3 = []; @@ -776,7 +854,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e7); } + if (peg$silentFails === 0) { + peg$fail(peg$e7); + } } while (s4 !== peg$FAILED) { s3.push(s4); @@ -785,7 +865,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e7); } + if (peg$silentFails === 0) { + peg$fail(peg$e7); + } } } if (input.charCodeAt(peg$currPos) === 34) { @@ -793,7 +875,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e6); } + if (peg$silentFails === 0) { + peg$fail(peg$e6); + } } if (s4 !== peg$FAILED) { s1 = s3; @@ -814,7 +898,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e3); } + if (peg$silentFails === 0) { + peg$fail(peg$e3); + } } peg$resultsCache[key] = { nextPos: peg$currPos, result: s0 }; @@ -877,7 +963,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e9); } + if (peg$silentFails === 0) { + peg$fail(peg$e9); + } } if (s1 !== peg$FAILED) { s2 = []; @@ -910,7 +998,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e8); } + if (peg$silentFails === 0) { + peg$fail(peg$e8); + } } peg$resultsCache[key] = { nextPos: peg$currPos, result: s0 }; @@ -937,7 +1027,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e10); } + if (peg$silentFails === 0) { + peg$fail(peg$e10); + } } if (s1 !== peg$FAILED) { s2 = []; @@ -946,7 +1038,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e11); } + if (peg$silentFails === 0) { + peg$fail(peg$e11); + } } while (s3 !== peg$FAILED) { s2.push(s3); @@ -955,7 +1049,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e11); } + if (peg$silentFails === 0) { + peg$fail(peg$e11); + } } } if (input.substr(peg$currPos, 2) === peg$c7) { @@ -963,7 +1059,9 @@ function peg$parse(input, options) { peg$currPos += 2; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e12); } + if (peg$silentFails === 0) { + peg$fail(peg$e12); + } } if (s3 !== peg$FAILED) { peg$savedPos = s0; @@ -979,7 +1077,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e8); } + if (peg$silentFails === 0) { + peg$fail(peg$e8); + } } peg$resultsCache[key] = { nextPos: peg$currPos, result: s0 }; @@ -1005,12 +1105,16 @@ function peg$parse(input, options) { peg$currPos++; } else { s0 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e14); } + if (peg$silentFails === 0) { + peg$fail(peg$e14); + } } peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e13); } + if (peg$silentFails === 0) { + peg$fail(peg$e13); + } } peg$resultsCache[key] = { nextPos: peg$currPos, result: s0 }; @@ -1036,12 +1140,16 @@ function peg$parse(input, options) { peg$currPos++; } else { s0 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e16); } + if (peg$silentFails === 0) { + peg$fail(peg$e16); + } } peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e15); } + if (peg$silentFails === 0) { + peg$fail(peg$e15); + } } peg$resultsCache[key] = { nextPos: peg$currPos, result: s0 }; @@ -1067,12 +1175,16 @@ function peg$parse(input, options) { peg$currPos++; } else { s0 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e18); } + if (peg$silentFails === 0) { + peg$fail(peg$e18); + } } peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e17); } + if (peg$silentFails === 0) { + peg$fail(peg$e17); + } } peg$resultsCache[key] = { nextPos: peg$currPos, result: s0 }; @@ -1101,7 +1213,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e20); } + if (peg$silentFails === 0) { + peg$fail(peg$e20); + } } if (s3 !== peg$FAILED) { while (s3 !== peg$FAILED) { @@ -1111,7 +1225,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s3 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e20); } + if (peg$silentFails === 0) { + peg$fail(peg$e20); + } } } } else { @@ -1124,7 +1240,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e21); } + if (peg$silentFails === 0) { + peg$fail(peg$e21); + } } while (s4 !== peg$FAILED) { s3.push(s4); @@ -1133,7 +1251,9 @@ function peg$parse(input, options) { peg$currPos++; } else { s4 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e21); } + if (peg$silentFails === 0) { + peg$fail(peg$e21); + } } } s2 = [s2, s3]; @@ -1150,7 +1270,9 @@ function peg$parse(input, options) { peg$silentFails--; if (s0 === peg$FAILED) { s1 = peg$FAILED; - if (peg$silentFails === 0) { peg$fail(peg$e19); } + if (peg$silentFails === 0) { + peg$fail(peg$e19); + } } peg$resultsCache[key] = { nextPos: peg$currPos, result: s0 }; @@ -1179,5 +1301,5 @@ function peg$parse(input, options) { module.exports = { SyntaxError: peg$SyntaxError, - parse: peg$parse + parse: peg$parse, }; diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res index 573d82c1..8517dd95 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res @@ -191,9 +191,7 @@ let doBuildResult = ( this ->getExpression ->Belt.Option.map( - Belt.Result.flatMap( - _, - expression => + Belt.Result.flatMap(_, expression => try { Reducer_Expression.reduceExpressionInProject(expression, aContinuation, accessors)->Ok } catch { diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ReducerFn_T.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ReducerFn_T.res index 2b948a02..2dc926be 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ReducerFn_T.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ReducerFn_T.res @@ -2,8 +2,4 @@ module ExpressionT = Reducer_Expression_T module InternalExpressionValue = ReducerInterface_InternalExpressionValue module ProjectAccessorsT = ReducerProject_ProjectAccessors_T -type t = ( - ExpressionT.t, - ExpressionT.bindings, - ProjectAccessorsT.t, -) => InternalExpressionValue.t +type t = (ExpressionT.t, ExpressionT.bindings, ProjectAccessorsT.t) => InternalExpressionValue.t From 1c98aaa3e20abf4fe284f4ea2263907ab4a3459c Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Thu, 8 Sep 2022 23:13:28 +0200 Subject: [PATCH 4/5] value vs result conversion functions --- .../Reducer/Reducer_TestMacroHelpers.res | 17 ++++++++--------- .../__tests__/Reducer/Reducer_test.res | 2 +- .../Reducer_Dispatch_BuiltIn.res | 16 ++++++++-------- .../Reducer_Dispatch_BuiltInMacros.res | 9 ++++----- .../src/rescript/Reducer/Reducer_ErrorValue.res | 17 +++++++++++++++++ .../Reducer_Expression/Reducer_Expression.res | 3 +-- .../Reducer_ExpressionWithContext.res | 9 +++++++++ .../Reducer_Expression_BindingsReplacer.res | 6 +----- .../Reducer_Expression_Macro.res | 17 +++++++++++++++++ .../Reducer_Expression/Reducer_Expression_T.res | 6 ++++++ .../Reducer_Type/Reducer_Type_Compile.res | 2 +- ...ReducerInterface_InternalExpressionValue.res | 6 ++++++ .../ReducerProject_ProjectItem.res | 3 +-- 13 files changed, 80 insertions(+), 33 deletions(-) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res index a1a3399e..329f30bc 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res @@ -17,18 +17,17 @@ let testMacro_ = ( expectedCode: string, ) => { let bindings = Bindings.fromArray(bindArray) - tester(expr->T.toString, () => { - let result = switch expr->Reducer_Dispatch_BuiltInMacros.dispatchMacroCall( + tester(expr->T.toString, () => + expr + ->Macro.expandMacroCallRs( bindings, ProjectAccessorsT.identityAccessors, Expression.reduceExpressionInProject, - ) { - | v => Ok(v) - | exception Reducer_ErrorValue.ErrorException(e) => Error(e) - } - - result->ExpressionWithContext.toStringResult->expect->toEqual(expectedCode) - }) + ) + ->ExpressionWithContext.toStringResult + ->expect + ->toEqual(expectedCode) + ) } let testMacroEval_ = ( diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_test.res index f70aa934..f6b9ad77 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_test.res @@ -56,7 +56,7 @@ describe("test exceptions", () => { testDescriptionEvalToBe( "javascript exception", "javascriptraise('div by 0')", - "Error(JS Exception: Error: 'div by 0')", + "Error(Error: 'div by 0')", ) // testDescriptionEvalToBe( // "rescript exception", 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 b7fed530..985558e7 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 @@ -3,6 +3,7 @@ module BindingsReplacer = Reducer_Expression_BindingsReplacer module Continuation = ReducerInterface_Value_Continuation module ExpressionT = Reducer_Expression_T module ExternalLibrary = ReducerInterface.ExternalLibrary +module InternalExpressionValue = ReducerInterface_InternalExpressionValue module Lambda = Reducer_Expression_Lambda module MathJs = Reducer_MathJs module ProjectAccessorsT = ReducerProject_ProjectAccessors_T @@ -206,13 +207,12 @@ let dispatch = ( let (fn, args) = call // There is a bug that prevents string match in patterns // So we have to recreate a copy of the string - switch ExternalLibrary.dispatch((Js.String.make(fn), args), accessors, reducer, callInternal) { - | Ok(v) => v - | Error(e) => raise(ErrorException(e)) - } + ExternalLibrary.dispatch( + (Js.String.make(fn), args), + accessors, + reducer, + callInternal, + )->InternalExpressionValue.resultToValue } catch { - | ErrorException(e) => raise(ErrorException(e)) - | Js.Exn.Error(obj) => - raise(ErrorException(REJavaScriptExn(Js.Exn.message(obj), Js.Exn.name(obj)))) - | _ => raise(ErrorException(RETodo("unhandled rescript exception"))) + | exn => Reducer_ErrorValue.fromException(exn)->Reducer_ErrorValue.toException } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res index 67ed732c..73b7bbd1 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltInMacros.res @@ -15,7 +15,6 @@ module ProjectReducerFnT = ReducerProject_ReducerFn_T open Reducer_Expression_ExpressionBuilder -exception ErrorException = ErrorValue.ErrorException type expression = ExpressionT.expression type expressionWithContext = ExpressionWithContext.expressionWithContext @@ -45,7 +44,7 @@ let dispatchMacroCall = ( } let doBindStatement = (bindingExpr: expression, statement: expression, accessors) => { - let defaultStatement = ErrorValue.REAssignmentExpected->ErrorException + let defaultStatement = ErrorValue.REAssignmentExpected switch statement { | ExpressionT.EList(list{ExpressionT.EValue(IEvCall(callName)), symbolExpr, statement}) => { let setBindingsFn = correspondingSetBindingsFn(callName) @@ -55,10 +54,10 @@ let dispatchMacroCall = ( boundStatement, ) => eFunction(setBindingsFn, list{newBindingsExpr, symbolExpr, boundStatement})) } else { - raise(defaultStatement) + defaultStatement->Reducer_ErrorValue.toException } } - | _ => raise(defaultStatement) + | _ => defaultStatement->Reducer_ErrorValue.toException } } @@ -142,7 +141,7 @@ let dispatchMacroCall = ( let ifTrueBlock = eBlock(list{ifTrue}) ExpressionWithContext.withContext(ifTrueBlock, bindings) } - | _ => raise(ErrorException(REExpectedType("Boolean", ""))) + | _ => REExpectedType("Boolean", "")->Reducer_ErrorValue.toException } } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res index e6404b18..b6baeaf5 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res @@ -64,3 +64,20 @@ let errorToString = err => | RENeedToRun => "Need to run" | REOther(msg) => `Error: ${msg}` } + +let fromException = exn => + switch exn { + | ErrorException(e) => e + | Js.Exn.Error(e) => + switch Js.Exn.message(e) { + | Some(message) => REOther(message) + | None => + switch Js.Exn.name(e) { + | Some(name) => REOther(name) + | None => REOther("Unknown error") + } + } + | _e => REOther("Unknown error") + } + +let toException = (errorValue: t) => raise(ErrorException(errorValue)) 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 4b957e33..ea810c88 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 @@ -101,8 +101,7 @@ module BackCompatible = { try { expression->reduceExpressionInProject(accessors.stdLib, accessors)->Ok } catch { - | ErrorException(e) => Error(e) - | _ => raise(ErrorException(RETodo("internal exception"))) + | exn => Reducer_ErrorValue.fromException(exn)->Error } } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_ExpressionWithContext.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_ExpressionWithContext.res index 23b7e2c8..539585b4 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_ExpressionWithContext.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_ExpressionWithContext.res @@ -18,6 +18,8 @@ type expressionWithContext = | ExpressionWithContext(expression, context) | ExpressionNoContext(expression) +type t = expressionWithContext + let callReducer = ( expressionWithContext: expressionWithContext, bindings: bindings, @@ -51,3 +53,10 @@ let toStringResult = rExpressionWithContext => | Ok(expressionWithContext) => `Ok(${toString(expressionWithContext)})` | Error(errorValue) => ErrorValue.errorToString(errorValue) } + +let resultToValue = (rExpressionWithContext: result): t => { + switch rExpressionWithContext { + | Ok(expressionWithContext) => expressionWithContext + | Error(errorValue) => ErrorValue.toException(errorValue) + } +} diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res index 8a729405..f1bf0135 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_BindingsReplacer.res @@ -41,9 +41,5 @@ and checkIfCallable = (evValue: internalExpressionValue) => switch evValue { | IEvCall(_) | IEvLambda(_) => evValue | _ => - raise( - ErrorValue.ErrorException( - ErrorValue.RENotAFunction(InternalExpressionValue.toString(evValue)), - ), - ) + ErrorValue.RENotAFunction(InternalExpressionValue.toString(evValue))->ErrorValue.toException } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Macro.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Macro.res index 7426be18..11a7cb0b 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Macro.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Macro.res @@ -10,6 +10,23 @@ type expression = ExpressionT.expression type internalExpressionValue = InternalExpressionValue.t type expressionWithContext = ExpressionWithContext.expressionWithContext +let expandMacroCallRs = ( + macroExpression: expression, + bindings: ExpressionT.bindings, + accessors: ProjectAccessorsT.t, + reduceExpression: ProjectReducerFnT.t, +): result => + try { + Reducer_Dispatch_BuiltInMacros.dispatchMacroCall( + macroExpression, + bindings, + accessors, + reduceExpression, + )->Ok + } catch { + | exn => Reducer_ErrorValue.fromException(exn)->Error + } + let doMacroCall = ( macroExpression: expression, bindings: ExpressionT.bindings, diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_T.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_T.res index 61f723df..be2755fe 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_T.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_T.res @@ -82,3 +82,9 @@ type optionFfiFnReturningResult = ( type expressionOrFFI = | NotFFI(expression) | FFI(ffiFn) + +let resultToValue = (rExpression: result): t => + switch rExpression { + | Ok(expression) => expression + | Error(errorValue) => Reducer_ErrorValue.toException(errorValue) + } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Type/Reducer_Type_Compile.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Type/Reducer_Type_Compile.res index 73696831..b2e0be9b 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Type/Reducer_Type_Compile.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Type/Reducer_Type_Compile.res @@ -15,7 +15,7 @@ let ievFromTypeExpression = ( let sourceCode = `type ${sIndex}=${typeExpressionSourceCode}` Reducer_Expression.BackCompatible.parse(sourceCode)->Belt.Result.flatMap(expr => { let accessors = ProjectAccessorsT.identityAccessors - let result = reducerFn(expr, Bindings.emptyBindings, accessors) + let _result = reducerFn(expr, Bindings.emptyBindings, accessors) let nameSpace = accessors.states.continuation switch Bindings.getType(nameSpace, sIndex) { diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res index 34eb6f40..5531cbdf 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res @@ -244,3 +244,9 @@ let nameSpaceGet = (nameSpace: nameSpace, key: string): option => { let NameSpace(container) = nameSpace container->Belt.Map.String.get(key) } + +let resultToValue = (rExpression: result): t => + switch rExpression { + | Ok(expression) => expression + | Error(errorValue) => Reducer_ErrorValue.toException(errorValue) + } diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res index 8517dd95..7676ddef 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res @@ -195,8 +195,7 @@ let doBuildResult = ( try { Reducer_Expression.reduceExpressionInProject(expression, aContinuation, accessors)->Ok } catch { - | Reducer_ErrorValue.ErrorException(e) => e->Error - | _ => RETodo("unhandled rescript exception")->Error + | exn => Reducer_ErrorValue.fromException(exn)->Error } ), ) From 5d1730101612a8575fcba7c3151f1fac16faab1a Mon Sep 17 00:00:00 2001 From: Umur Ozkul Date: Fri, 9 Sep 2022 01:14:42 +0200 Subject: [PATCH 5/5] remove raise --- .../Reducer/Reducer_Expression/Reducer_Expression.res | 2 -- .../Reducer_Expression/Reducer_Expression_Lambda.res | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) 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 ea810c88..e3fdf168 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 @@ -14,8 +14,6 @@ module T = Reducer_Expression_T type errorValue = Reducer_ErrorValue.errorValue type t = T.t -exception ErrorException = Reducer_ErrorValue.ErrorException - /* Recursively evaluate/reduce the expression (Lisp AST/Lambda calculus) */ diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res index c4ef011e..aa0745ae 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res @@ -23,7 +23,7 @@ let checkArity = ( let argsLength = Belt.List.length(args) let parametersLength = Js.Array2.length(lambdaValue.parameters) if argsLength !== parametersLength { - raise(ErrorValue.ErrorException(ErrorValue.REArityError(None, parametersLength, argsLength))) + ErrorValue.REArityError(None, parametersLength, argsLength)->ErrorValue.toException } else { args } @@ -38,7 +38,7 @@ let checkArity = ( let checkIfReduced = (args: list) => args->Belt.List.reduceReverse(list{}, (acc, arg) => switch arg { - | IEvSymbol(symbol) => raise(ErrorValue.ErrorException(ErrorValue.RESymbolNotFound(symbol))) + | IEvSymbol(symbol) => ErrorValue.RESymbolNotFound(symbol)->ErrorValue.toException | _ => list{arg, ...acc} } ) @@ -63,7 +63,7 @@ let caseNotFFI = ( let caseFFI = (ffiFn: ExpressionT.ffiFn, args, accessors: ProjectAccessorsT.t) => { switch ffiFn(args->Belt.List.toArray, accessors.environment) { | Ok(value) => value - | Error(value) => raise(ErrorValue.ErrorException(value)) + | Error(value) => value->ErrorValue.toException } }