diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res index 752cc9c7..329f30bc 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res @@ -19,7 +19,7 @@ let testMacro_ = ( let bindings = Bindings.fromArray(bindArray) tester(expr->T.toString, () => expr - ->Macro.expandMacroCall( + ->Macro.expandMacroCallRs( bindings, ProjectAccessorsT.identityAccessors, Expression.reduceExpressionInProject, @@ -44,6 +44,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/__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/FunctionRegistry/Library/FR_Danger.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Danger.res index 2a8b8773..2fcfcdaf 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, ) @@ -309,14 +308,10 @@ module DiminishingReturns = { reducer, ) switch resultAsInternalExpression { - | Ok(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", - ) + | IEvNumber(x) => Ok(x) | _ => Error( - "Error 2 in Danger.optimalAllocationGivenDiminishingMarginalReturnsForManyFunctions", + "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", ) } } 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..1def1e36 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,7 @@ let library = [ ~run=(inputs, _, accessors: ProjectAccessorsT.t, reducer: ProjectReducerFnT.t) => switch inputs { | [IEvArray(array), initialValue, IEvLambda(lambda)] => - Internals.reduceReverse( - array, - initialValue, - lambda, - accessors, - reducer, - )->E.R2.errMap(_ => "Error!") + Ok(Internals.reduceReverse(array, initialValue, lambda, accessors, reducer)) | _ => Error(impossibleError) }, (), @@ -284,7 +264,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..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 @@ -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) + ExternalLibrary.dispatch( + (Js.String.make(fn), args), + accessors, + reducer, + callInternal, + )->InternalExpressionValue.resultToValue } catch { - | Js.Exn.Error(obj) => REJavaScriptExn(Js.Exn.message(obj), Js.Exn.name(obj))->Error - | _ => RETodo("unhandled rescript exception")->Error + | 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 cee664bf..db77a0a2 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,9 @@ 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 type expression = ExpressionT.expression type expressionWithContext = ExpressionWithContext.expressionWithContext @@ -25,21 +23,15 @@ 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 +44,7 @@ let dispatchMacroCall = ( } let doBindStatement = (bindingExpr: expression, statement: expression, accessors) => { - let defaultStatement = ErrorValue.REAssignmentExpected->Error + let defaultStatement = ErrorValue.REAssignmentExpected switch statement { | ExpressionT.EList(list{ExpressionT.EValue(IEvCall(callName)), symbolExpr, statement}) => { let setBindingsFn = correspondingSetBindingsFn(callName) @@ -62,17 +54,18 @@ let dispatchMacroCall = ( boundStatement, ) => eFunction(setBindingsFn, list{newBindingsExpr, symbolExpr, boundStatement})) } else { - defaultStatement + defaultStatement->Reducer_ErrorValue.toException } } - | _ => defaultStatement + | _ => defaultStatement->Reducer_ErrorValue.toException } } - 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,11 @@ 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 +113,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,25 +128,23 @@ 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) + } + | _ => REExpectedType("Boolean", "")->Reducer_ErrorValue.toException + } } - let doEnvironment = (accessors: ProjectAccessorsT.t) => { + let doEnvironment = (accessors: ProjectAccessorsT.t): expressionWithContext => { let environment = accessors.environment let environmentPairs = [ ("sampleCount", environment.sampleCount->Js.Int.toFloat->InternalExpressionValue.IEvNumber), @@ -162,7 +154,7 @@ let dispatchMacroCall = ( ), ] let environmentMap = Belt.Map.String.fromArray(environmentPairs) - ExpressionWithContext.noContext(ExpressionBuilder.eRecord(environmentMap))->Ok + ExpressionWithContext.noContext(ExpressionBuilder.eRecord(environmentMap)) } let doWithEnvironmentSampleCount = ( @@ -170,27 +162,25 @@ let dispatchMacroCall = ( expr: expression, bindings: ExpressionT.bindings, accessors: ProjectAccessorsT.t, - ) => { + ): expressionWithContext => { let blockSampleCount = ExpressionBuilder.eBlock(list{sampleCountExpr}) - let rSampleCount = reduceExpression(blockSampleCount, bindings, accessors) - rSampleCount->Result.flatMap(sampleCountValue => - switch sampleCountValue { - | InternalExpressionValue.IEvNumber(sampleCount) => { - let newEnvironment = {...accessors.environment, sampleCount: Js.Math.floor(sampleCount)} - let newAccessors = {...accessors, environment: newEnvironment} - reduceExpression(expr, bindings, newAccessors)->Belt.Result.map(value => - value->ExpressionT.EValue->ExpressionWithContext.noContext - ) - } - | _ => REExpectedType("Number", "")->Error + let sampleCount = reduceExpression(blockSampleCount, bindings, accessors) + switch sampleCount { + | InternalExpressionValue.IEvNumber(sampleCount) => { + let newEnvironment = {...accessors.environment, sampleCount: Js.Math.floor(sampleCount)} + let newAccessors = {...accessors, environment: newEnvironment} + reduceExpression(expr, bindings, newAccessors) + ->ExpressionT.EValue + ->ExpressionWithContext.noContext } - ) + | _ => REExpectedType("Number", "")->Reducer_ErrorValue.toException + } } - - 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_$$")), @@ -227,11 +217,11 @@ let dispatchMacroCall = ( sampleCountExpr, } => doWithEnvironmentSampleCount(sampleCountExpr, expr, 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..b6baeaf5 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) => @@ -62,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 614ba3a8..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 @@ -21,10 +21,10 @@ let rec reduceExpressionInProject = ( expression: t, continuation: T.bindings, accessors: ProjectAccessorsT.t, -): result => { - // `reduce: ${T.toString(expression)}`->Js.log +): 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 +41,12 @@ 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) - }) - }) - ) - racc->Result.flatMap(acc => acc->reduceValueList(accessors)) +): InternalExpressionValue.t => { + let acc: list = + expressions->Belt.List.reduceReverse(list{}, (acc, each: t) => + acc->Belt.List.add(each->reduceExpressionInProject(continuation, accessors)) + ) + acc->reduceValueList(accessors) } /* @@ -63,48 +55,34 @@ 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 - ) + valueList->Lambda.checkIfReduced->Belt.List.toArray->InternalExpressionValue.IEvArray | list{IEvLambda(lambdaCall), ...args} => args ->Lambda.checkIfReduced - ->Result.flatMap(checkedArgs => - Lambda.doLambdaCall(lambdaCall, checkedArgs, accessors, reduceExpressionInProject) - ) - - | _ => - valueList - ->Lambda.checkIfReduced - ->Result.flatMap(reducedValueList => - reducedValueList->Belt.List.toArray->InternalExpressionValue.IEvArray->Ok - ) + ->Lambda.doLambdaCall(lambdaCall, _, accessors, reduceExpressionInProject) + | _ => valueList->Lambda.checkIfReduced->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 +96,11 @@ module BackCompatible = { let evaluate = (expression: t): result => { let accessors = ProjectAccessorsT.identityAccessors - expression->reduceExpressionInProject(accessors.stdLib, accessors) + try { + expression->reduceExpressionInProject(accessors.stdLib, accessors)->Ok + } catch { + | exn => Reducer_ErrorValue.fromException(exn)->Error + } } 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..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,12 +18,14 @@ type expressionWithContext = | ExpressionWithContext(expression, context) | ExpressionNoContext(expression) +type t = expressionWithContext + let callReducer = ( expressionWithContext: expressionWithContext, 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)}`) @@ -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 6e38f833..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 @@ -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,15 @@ 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) + | ExpressionT.EValue(value) => 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 +25,21 @@ 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 - }) - }) - ) - racc->Result.map(acc => acc->ExpressionT.EList) + 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) => 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 + | _ => + ErrorValue.RENotAFunction(InternalExpressionValue.toString(evValue))->ErrorValue.toException } 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..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,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 + ErrorValue.REArityError(None, parametersLength, argsLength)->ErrorValue.toException } 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 => - switch arg { - | IEvSymbol(symbol) => ErrorValue.RESymbolNotFound(symbol)->Error - | _ => list{arg, ...acc}->Ok - } - ) + args->Belt.List.reduceReverse(list{}, (acc, arg) => + switch arg { + | IEvSymbol(symbol) => ErrorValue.RESymbolNotFound(symbol)->ErrorValue.toException + | _ => 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) => value->ErrorValue.toException + } } 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..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,32 +10,34 @@ type expression = ExpressionT.expression type internalExpressionValue = InternalExpressionValue.t type expressionWithContext = ExpressionWithContext.expressionWithContext -let expandMacroCall = ( +let expandMacroCallRs = ( macroExpression: expression, bindings: ExpressionT.bindings, accessors: ProjectAccessorsT.t, reduceExpression: ProjectReducerFnT.t, ): result => - Reducer_Dispatch_BuiltInMacros.dispatchMacroCall( - macroExpression, - bindings, - accessors, - reduceExpression, - ) + try { + Reducer_Dispatch_BuiltInMacros.dispatchMacroCall( + macroExpression, + bindings, + accessors, + reduceExpression, + )->Ok + } catch { + | exn => Reducer_ErrorValue.fromException(exn)->Error + } 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_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 79153801..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,16 +15,12 @@ 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 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/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 8eb915de..7676ddef 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, @@ -202,9 +191,12 @@ let doBuildResult = ( this ->getExpression ->Belt.Option.map( - Belt.Result.flatMap( - _, - Reducer_Expression.reduceExpressionInProject(_, aContinuation, accessors), + Belt.Result.flatMap(_, expression => + try { + Reducer_Expression.reduceExpressionInProject(expression, aContinuation, accessors)->Ok + } catch { + | exn => Reducer_ErrorValue.fromException(exn)->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..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, -) => result +type t = (ExpressionT.t, ExpressionT.bindings, ProjectAccessorsT.t) => InternalExpressionValue.t