diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_Helpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_Helpers.res index 569b187f..8e3d8257 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_Helpers.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_Helpers.res @@ -4,7 +4,7 @@ module ExternalExpressionValue = ReducerInterface.ExternalExpressionValue module InternalExpressionValue = ReducerInterface.InternalExpressionValue module Module = Reducer_Category_Module -let removeDefaultsInternal = (iev: InternalExpressionValue.expressionValue) => { +let removeDefaultsInternal = (iev: InternalExpressionValue.t) => { switch iev { | InternalExpressionValue.IEvModule(nameSpace) => Module.removeOther( @@ -15,9 +15,7 @@ let removeDefaultsInternal = (iev: InternalExpressionValue.expressionValue) => { } } -let removeDefaultsExternal = ( - ev: ExternalExpressionValue.expressionValue, -): ExternalExpressionValue.expressionValue => +let removeDefaultsExternal = (ev: ExternalExpressionValue.t): ExternalExpressionValue.t => ev->InternalExpressionValue.toInternal->removeDefaultsInternal->InternalExpressionValue.toExternal let rRemoveDefaultsInternal = r => Belt.Result.map(r, removeDefaultsInternal) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res index 1383d45e..103609a2 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_TestMacroHelpers.res @@ -12,7 +12,7 @@ module Module = Reducer_Category_Module let testMacro_ = ( tester, - bindArray: array<(string, InternalExpressionValue.expressionValue)>, + bindArray: array<(string, InternalExpressionValue.t)>, expr: T.expression, expectedCode: string, ) => { @@ -32,7 +32,7 @@ let testMacro_ = ( let testMacroEval_ = ( tester, - bindArray: array<(string, InternalExpressionValue.expressionValue)>, + bindArray: array<(string, InternalExpressionValue.t)>, expr: T.expression, expectedValue: string, ) => { @@ -51,24 +51,24 @@ let testMacroEval_ = ( } let testMacro = ( - bindArray: array<(string, InternalExpressionValue.expressionValue)>, + bindArray: array<(string, InternalExpressionValue.t)>, expr: T.expression, expectedExpr: string, ) => testMacro_(test, bindArray, expr, expectedExpr) let testMacroEval = ( - bindArray: array<(string, InternalExpressionValue.expressionValue)>, + bindArray: array<(string, InternalExpressionValue.t)>, expr: T.expression, expectedValue: string, ) => testMacroEval_(test, bindArray, expr, expectedValue) module MySkip = { let testMacro = ( - bindArray: array<(string, InternalExpressionValue.expressionValue)>, + bindArray: array<(string, InternalExpressionValue.t)>, expr: T.expression, expectedExpr: string, ) => testMacro_(Skip.test, bindArray, expr, expectedExpr) let testMacroEval = ( - bindArray: array<(string, InternalExpressionValue.expressionValue)>, + bindArray: array<(string, InternalExpressionValue.t)>, expr: T.expression, expectedValue: string, ) => testMacroEval_(Skip.test, bindArray, expr, expectedValue) @@ -76,12 +76,12 @@ module MySkip = { module MyOnly = { let testMacro = ( - bindArray: array<(string, InternalExpressionValue.expressionValue)>, + bindArray: array<(string, InternalExpressionValue.t)>, expr: T.expression, expectedExpr: string, ) => testMacro_(Only.test, bindArray, expr, expectedExpr) let testMacroEval = ( - bindArray: array<(string, InternalExpressionValue.expressionValue)>, + bindArray: array<(string, InternalExpressionValue.t)>, expr: T.expression, expectedValue: string, ) => testMacroEval_(Only.test, bindArray, expr, expectedValue) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res index e9d526bf..bbca7bc7 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res @@ -1,4 +1,4 @@ -type expressionValue = ReducerInterface_InternalExpressionValue.expressionValue +type internalExpressionValue = ReducerInterface_InternalExpressionValue.t /* Function Registry "Type". A type, without any other information. @@ -41,7 +41,7 @@ and frValueDistOrNumber = FRValueNumber(float) | FRValueDist(DistributionTypes.g type fnDefinition = { name: string, inputs: array, - run: (array, DistributionOperation.env) => result, + run: (array, DistributionOperation.env) => result, } type function = { @@ -73,7 +73,7 @@ module FRType = { | FRTypeAny => `any` } - let rec toFrValue = (r: expressionValue): option => + let rec toFrValue = (r: internalExpressionValue): option => switch r { | IEvNumber(f) => Some(FRValueNumber(f)) | IEvString(f) => Some(FRValueString(f)) @@ -89,7 +89,7 @@ module FRType = { | _ => None } - let rec matchWithExpressionValue = (t: t, r: expressionValue): option => + let rec matchWithExpressionValue = (t: t, r: internalExpressionValue): option => switch (t, r) { | (FRTypeAny, f) => toFrValue(f) | (FRTypeString, IEvString(f)) => Some(FRValueString(f)) @@ -125,7 +125,7 @@ module FRType = { | _ => None } - let rec matchReverse = (e: frValue): expressionValue => + let rec matchReverse = (e: frValue): internalExpressionValue => switch e { | FRValueNumber(f) => IEvNumber(f) | FRValueDistOrNumber(FRValueNumber(n)) => IEvNumber(n) @@ -153,9 +153,10 @@ module FRType = { | FRValueAny(f) => matchReverse(f) } - let matchWithExpressionValueArray = (inputs: array, args: array): option< - array, - > => { + let matchWithExpressionValueArray = ( + inputs: array, + args: array, + ): option> => { let isSameLength = E.A.length(inputs) == E.A.length(args) if !isSameLength { None @@ -206,14 +207,14 @@ module Matcher = { } module FnDefinition = { - let matchAssumingSameName = (f: fnDefinition, args: array) => { + let matchAssumingSameName = (f: fnDefinition, args: array) => { switch FRType.matchWithExpressionValueArray(f.inputs, args) { | Some(_) => MatchSimple.FullMatch | None => MatchSimple.SameNameDifferentArguments } } - let match = (f: fnDefinition, fnName: string, args: array) => { + let match = (f: fnDefinition, fnName: string, args: array) => { if f.name !== fnName { MatchSimple.DifferentName } else { @@ -226,7 +227,7 @@ module Matcher = { type definitionId = int type match = Match.t, definitionId> - let match = (f: function, fnName: string, args: array): match => { + let match = (f: function, fnName: string, args: array): match => { let matchedDefinition = () => E.A.getIndexBy(f.definitions, r => MatchSimple.isFullMatch(FnDefinition.match(r, fnName, args)) @@ -260,7 +261,7 @@ module Matcher = { } module Registry = { - let _findExactMatches = (r: registry, fnName: string, args: array) => { + let _findExactMatches = (r: registry, fnName: string, args: array) => { let functionMatchPairs = r->E.A2.fmap(l => (l, Function.match(l, fnName, args))) let fullMatch = functionMatchPairs->E.A.getBy(((_, match)) => Match.isFullMatch(match)) fullMatch->E.O.bind(((fn, match)) => @@ -271,7 +272,7 @@ module Matcher = { ) } - let _findNameMatches = (r: registry, fnName: string, args: array) => { + let _findNameMatches = (r: registry, fnName: string, args: array) => { let functionMatchPairs = r->E.A2.fmap(l => (l, Function.match(l, fnName, args))) let getNameMatches = functionMatchPairs @@ -290,7 +291,7 @@ module Matcher = { E.A.toNoneIfEmpty(matches) } - let findMatches = (r: registry, fnName: string, args: array) => { + let findMatches = (r: registry, fnName: string, args: array) => { switch _findExactMatches(r, fnName, args) { | Some(r) => Match.FullMatch(r) | None => @@ -318,7 +319,7 @@ module FnDefinition = { t.name ++ `(${inputs})` } - let run = (t: t, args: array, env: DistributionOperation.env) => { + let run = (t: t, args: array, env: DistributionOperation.env) => { let argValues = FRType.matchWithExpressionValueArray(t.inputs, args) switch argValues { | Some(values) => t.run(values, env) @@ -372,7 +373,7 @@ module Registry = { let matchAndRun = ( ~registry: registry, ~fnName: string, - ~args: array, + ~args: array, ~env: DistributionOperation.env, ) => { let matchToDef = m => Matcher.Registry.matchToDef(registry, m) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res index 68d06ae8..56315860 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Library.res @@ -9,7 +9,7 @@ module Declaration = { ("inputs", FRTypeArray(FRTypeRecord([("min", FRTypeNumber), ("max", FRTypeNumber)]))), ]) - let fromExpressionValue = (e: frValue): result => { + let fromExpressionValue = (e: frValue): result => { switch FunctionRegistry_Helpers.Prepare.ToValueArray.Record.twoArgs([e]) { | Ok([FRValueLambda(lambda), FRValueArray(inputs)]) => { open FunctionRegistry_Helpers.Prepare diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer.res index 34ca2595..bf8c89f9 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer.res @@ -1,13 +1,13 @@ module ErrorValue = Reducer_ErrorValue module Expression = Reducer_Expression -module ExternalExpressionValue = ReducerInterface_ExpressionValue +module ExternalExpressionValue = ReducerInterface_ExternalExpressionValue module InternalExpressionValue = ReducerInterface_InternalExpressionValue module Lambda = Reducer_Expression_Lambda type environment = ReducerInterface_InternalExpressionValue.environment type errorValue = Reducer_ErrorValue.errorValue -type expressionValue = ExternalExpressionValue.expressionValue -type externalBindings = ReducerInterface_ExpressionValue.externalBindings +type expressionValue = ExternalExpressionValue.t +type externalBindings = ReducerInterface_ExternalExpressionValue.externalBindings type lambdaValue = ExternalExpressionValue.lambdaValue let evaluate = Expression.evaluate diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer.resi b/packages/squiggle-lang/src/rescript/Reducer/Reducer.resi index d4e40a63..bd0c43fb 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer.resi +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer.resi @@ -2,27 +2,29 @@ module ErrorValue = Reducer_ErrorValue module Expression = Reducer_Expression @genType -type environment = ReducerInterface_ExpressionValue.environment +type environment = ReducerInterface_ExternalExpressionValue.environment @genType type errorValue = Reducer_ErrorValue.errorValue @genType -type expressionValue = ReducerInterface_ExpressionValue.expressionValue +type expressionValue = ReducerInterface_ExternalExpressionValue.t @genType -type externalBindings = ReducerInterface_ExpressionValue.externalBindings +type externalBindings = ReducerInterface_ExternalExpressionValue.externalBindings @genType -type lambdaValue = ReducerInterface_ExpressionValue.lambdaValue +type lambdaValue = ReducerInterface_ExternalExpressionValue.lambdaValue @genType let evaluateUsingOptions: ( - ~environment: option, - ~externalBindings: option, + ~environment: option, + ~externalBindings: option< + QuriSquiggleLang.ReducerInterface_ExternalExpressionValue.externalBindings, + >, string, ) => result @genType let evaluatePartialUsingExternalBindings: ( string, - QuriSquiggleLang.ReducerInterface_ExpressionValue.externalBindings, - QuriSquiggleLang.ReducerInterface_ExpressionValue.environment, + QuriSquiggleLang.ReducerInterface_ExternalExpressionValue.externalBindings, + QuriSquiggleLang.ReducerInterface_ExternalExpressionValue.environment, ) => result @genType let evaluate: string => result @@ -31,9 +33,9 @@ let parse: string => result @genType let foreignFunctionInterface: ( - QuriSquiggleLang.ReducerInterface_ExpressionValue.lambdaValue, - array, - QuriSquiggleLang.ReducerInterface_ExpressionValue.environment, + QuriSquiggleLang.ReducerInterface_ExternalExpressionValue.lambdaValue, + array, + QuriSquiggleLang.ReducerInterface_ExternalExpressionValue.environment, ) => result @genType diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module.res index 58174063..584f9089 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module.res @@ -71,8 +71,8 @@ let emptyModule: t = NameSpace(Belt.Map.String.empty) let fromTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceFromTypeScriptBindings let toTypeScriptBindings = ReducerInterface_InternalExpressionValue.nameSpaceToTypeScriptBindings -let toExpressionValue = (nameSpace: t): expressionValue => IEvModule(nameSpace) -let fromExpressionValue = (aValue: expressionValue): t => +let toExpressionValue = (nameSpace: t): internalExpressionValue => IEvModule(nameSpace) +let fromExpressionValue = (aValue: internalExpressionValue): t => switch aValue { | IEvModule(nameSpace) => nameSpace | _ => emptyModule @@ -101,7 +101,7 @@ let removeOther = (nameSpace: t, other: t): t => { } // -- Module definition -let define = (nameSpace: t, identifier: string, ev: expressionValue): t => { +let define = (nameSpace: t, identifier: string, ev: internalExpressionValue): t => { let NameSpace(container) = nameSpace Belt.Map.String.set(container, identifier, ev)->NameSpace // TODO build lambda for polymorphic functions here } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module_TypeChecker.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module_TypeChecker.res index fb5d2ef3..957b0dc8 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module_TypeChecker.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Category/Reducer_Category_Module_TypeChecker.res @@ -14,8 +14,3 @@ let isOfResolvedType = (aType, aValue) => { | _ => false } } - -// let compileTypeExpression = (typeExpression: string, bindings: ExpressionT.bindings, reducerFn: ExpressionT.reducerFn) => { -// statement = `type compiled=${typeExpression}` - -// } 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 f1664319..6012d179 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 @@ -41,7 +41,7 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce ->Ok } - let arrayAtIndex = (aValueArray: array, fIndex: float) => + let arrayAtIndex = (aValueArray: array, fIndex: float) => switch Belt.Array.get(aValueArray, Belt.Int.fromFloat(fIndex)) { | Some(value) => value->Ok | None => REArrayIndexNotFound("Array index not found", Belt.Int.fromFloat(fIndex))->Error @@ -53,7 +53,7 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce | None => RERecordPropertyNotFound("Module property not found", sIndex)->Error } - let recordAtIndex = (dict: Belt.Map.String.t, sIndex) => + let recordAtIndex = (dict: Belt.Map.String.t, sIndex) => switch Belt.Map.String.get(dict, sIndex) { | Some(value) => value->Ok | None => RERecordPropertyNotFound("Record property not found", sIndex)->Error @@ -69,24 +69,27 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce answer->IEvString->Ok } - let inspect = (value: expressionValue) => { + let inspect = (value: internalExpressionValue) => { Js.log(value->toString) value->Ok } - let inspectLabel = (value: expressionValue, label: string) => { + let inspectLabel = (value: internalExpressionValue, label: string) => { Js.log(`${label}: ${value->toString}`) value->Ok } - let doSetBindings = (bindings: nameSpace, symbol: string, value: expressionValue) => { + let doSetBindings = (bindings: nameSpace, symbol: string, value: internalExpressionValue) => { Module.set(bindings, symbol, value)->IEvModule->Ok } - let doSetTypeAliasBindings = (bindings: nameSpace, symbol: string, value: expressionValue) => - Module.setTypeAlias(bindings, symbol, value)->IEvModule->Ok + let doSetTypeAliasBindings = ( + bindings: nameSpace, + symbol: string, + value: internalExpressionValue, + ) => Module.setTypeAlias(bindings, symbol, value)->IEvModule->Ok - let doSetTypeOfBindings = (bindings: nameSpace, symbol: string, value: expressionValue) => + let doSetTypeOfBindings = (bindings: nameSpace, symbol: string, value: internalExpressionValue) => Module.setTypeOf(bindings, symbol, value)->IEvModule->Ok let doExportBindings = (bindings: nameSpace) => bindings->Module.toExpressionValue->Ok @@ -296,7 +299,7 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce Reducer uses Result monad while reducing expressions */ let dispatch = (call: functionCall, environment, reducer: ExpressionT.reducerFn): result< - expressionValue, + internalExpressionValue, errorValue, > => try { 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 0431457e..1467f815 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 @@ -7,16 +7,15 @@ module BindingsReplacer = Reducer_Expression_BindingsReplacer module ErrorValue = Reducer_ErrorValue module ExpressionBuilder = Reducer_Expression_ExpressionBuilder module ExpressionT = Reducer_Expression_T -module ExpressionValue = ReducerInterface_InternalExpressionValue +module InternalExpressionValue = ReducerInterface_InternalExpressionValue module ExpressionWithContext = Reducer_ExpressionWithContext module Module = Reducer_Category_Module module Result = Belt.Result open Reducer_Expression_ExpressionBuilder -type environment = ExpressionValue.environment +type environment = InternalExpressionValue.environment type errorValue = ErrorValue.errorValue type expression = ExpressionT.expression -type expressionValue = ExpressionValue.expressionValue type expressionWithContext = ExpressionWithContext.expressionWithContext let dispatchMacroCall = ( @@ -137,11 +136,11 @@ let dispatchMacroCall = ( let rCondition = reduceExpression(blockCondition, bindings, environment) rCondition->Result.flatMap(conditionValue => switch conditionValue { - | ExpressionValue.IEvBool(false) => { + | InternalExpressionValue.IEvBool(false) => { let ifFalseBlock = eBlock(list{ifFalse}) ExpressionWithContext.withContext(ifFalseBlock, bindings)->Ok } - | ExpressionValue.IEvBool(true) => { + | InternalExpressionValue.IEvBool(true) => { let ifTrueBlock = eBlock(list{ifTrue}) ExpressionWithContext.withContext(ifTrueBlock, bindings)->Ok } 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 99c8cdbb..4b551e38 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 @@ -13,9 +13,8 @@ module T = Reducer_Expression_T type environment = InternalExpressionValue.environment type errorValue = Reducer_ErrorValue.errorValue type expression = T.expression -type expressionValue = InternalExpressionValue.expressionValue -type externalExpressionValue = ReducerInterface_ExpressionValue.expressionValue -type tmpExternalBindings = InternalExpressionValue.tmpExternalBindings +type internalExpressionValue = InternalExpressionValue.t +type externalExpressionValue = ReducerInterface_ExternalExpressionValue.t type t = expression /* @@ -28,7 +27,7 @@ let parse = (peggyCode: string): result => Recursively evaluate/reduce the expression (Lisp AST) */ let rec reduceExpression = (expression: t, bindings: T.bindings, environment: environment): result< - expressionValue, + internalExpressionValue, 'e, > => { // Js.log(`reduce: ${T.toString(expression)} bindings: ${bindings->Bindings.toString}`) @@ -51,11 +50,11 @@ and reduceExpressionList = ( expressions: list, bindings: T.bindings, environment: environment, -): result => { - let racc: result, 'e> = expressions->Belt.List.reduceReverse(Ok(list{}), ( - racc, - each: expression, - ) => +): result => { + let racc: result< + list, + 'e, + > = expressions->Belt.List.reduceReverse(Ok(list{}), (racc, each: expression) => racc->Result.flatMap(acc => { each ->reduceExpression(bindings, environment) @@ -70,8 +69,8 @@ and reduceExpressionList = ( /* After reducing each level of expression(Lisp AST), we have a value list to evaluate */ -and reduceValueList = (valueList: list, environment): result< - expressionValue, +and reduceValueList = (valueList: list, environment): result< + internalExpressionValue, 'e, > => switch valueList { @@ -108,18 +107,18 @@ and reduceValueList = (valueList: list, environment): result< } let evalUsingBindingsExpression_ = (aExpression, bindings, environment): result< - expressionValue, + internalExpressionValue, 'e, > => reduceExpression(aExpression, bindings, environment) let evaluateUsingOptions = ( - ~environment: option, - ~externalBindings: option, + ~environment: option, + ~externalBindings: option, code: string, ): result => { let anEnvironment = Belt.Option.getWithDefault( environment, - ReducerInterface_ExpressionValue.defaultEnvironment, + ReducerInterface_ExternalExpressionValue.defaultEnvironment, ) let mergedBindings: InternalExpressionValue.nameSpace = Module.merge( @@ -142,9 +141,9 @@ let evaluate = (code: string): result => { } let evaluatePartialUsingExternalBindings = ( code: string, - externalBindings: ReducerInterface_ExpressionValue.externalBindings, - environment: ReducerInterface_ExpressionValue.environment, -): result => { + externalBindings: ReducerInterface_ExternalExpressionValue.externalBindings, + environment: ReducerInterface_ExternalExpressionValue.environment, +): result => { let rAnswer = evaluateUsingOptions( ~environment=Some(environment), ~externalBindings=Some(externalBindings), 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 defc6d02..1f6b18b7 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 @@ -1,16 +1,16 @@ module BindingsReplacer = Reducer_Expression_BindingsReplacer module ErrorValue = Reducer_ErrorValue module ExpressionT = Reducer_Expression_T -module ExpressionValue = ReducerInterface_InternalExpressionValue +module InternalExpressionValue = ReducerInterface_InternalExpressionValue module Result = Belt.Result module Module = Reducer_Category_Module type bindings = ExpressionT.bindings type context = bindings -type environment = ExpressionValue.environment +type environment = InternalExpressionValue.environment type errorValue = Reducer_ErrorValue.errorValue type expression = ExpressionT.expression -type expressionValue = ExpressionValue.expressionValue +type internalExpressionValue = InternalExpressionValue.t type reducerFn = ExpressionT.reducerFn type expressionWithContext = @@ -22,7 +22,7 @@ let callReducer = ( bindings: bindings, environment: environment, reducer: reducerFn, -): result => { +): result => { switch expressionWithContext { | ExpressionNoContext(expr) => // Js.log(`callReducer: bindings ${Bindings.toString(bindings)} expr ${ExpressionT.toString(expr)}`) @@ -42,7 +42,7 @@ let toString = expressionWithContext => | ExpressionWithContext(expr, context) => `${ExpressionT.toString(expr)} context: ${context ->Module.toExpressionValue - ->ExpressionValue.toString}` + ->InternalExpressionValue.toString}` } let toStringResult = rExpressionWithContext => 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 43109f0d..e96ae4a1 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,14 +1,13 @@ module ErrorValue = Reducer_ErrorValue module ExpressionT = Reducer_Expression_T -module ExpressionValue = ReducerInterface_InternalExpressionValue +module InternalExpressionValue = ReducerInterface_InternalExpressionValue module Result = Belt.Result module Module = Reducer_Category_Module type errorValue = Reducer_ErrorValue.errorValue type expression = ExpressionT.expression -type expressionValue = ExpressionValue.expressionValue -type tmpExternalBindings = ReducerInterface_InternalExpressionValue.tmpExternalBindings -type externalBindings = ReducerInterface_ExpressionValue.externalBindings +type internalExpressionValue = InternalExpressionValue.t +type externalBindings = ReducerInterface_ExternalExpressionValue.externalBindings let isMacroName = (fName: string): bool => fName->Js.String2.startsWith("$$") @@ -41,14 +40,14 @@ and replaceSymbolsOnExpressionList = (bindings, list) => { ) racc->Result.map(acc => acc->ExpressionT.EList) } -and replaceSymbolOnValue = (bindings, evValue: expressionValue) => +and replaceSymbolOnValue = (bindings, evValue: internalExpressionValue) => switch evValue { | IEvSymbol(symbol) => Module.getWithDefault(bindings, symbol, evValue)->Ok | IEvCall(symbol) => Module.getWithDefault(bindings, symbol, evValue)->checkIfCallable | _ => evValue->Ok } -and checkIfCallable = (evValue: expressionValue) => +and checkIfCallable = (evValue: internalExpressionValue) => switch evValue { | IEvCall(_) | IEvLambda(_) => evValue->Ok - | _ => ErrorValue.RENotAFunction(ExpressionValue.toString(evValue))->Error + | _ => ErrorValue.RENotAFunction(InternalExpressionValue.toString(evValue))->Error } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res index 04d5fb7b..9fd4a34a 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res @@ -1,7 +1,7 @@ module BBindingsReplacer = Reducer_Expression_BindingsReplacer module BErrorValue = Reducer_ErrorValue module BExpressionT = Reducer_Expression_T -module BExpressionValue = ReducerInterface_InternalExpressionValue +module BInternalExpressionValue = ReducerInterface_InternalExpressionValue module BModule = Reducer_Category_Module type errorValue = BErrorValue.errorValue @@ -12,16 +12,17 @@ type internalCode = ReducerInterface_InternalExpressionValue.internalCode external castExpressionToInternalCode: expressionOrFFI => internalCode = "%identity" -let eArray = anArray => anArray->BExpressionValue.IEvArray->BExpressionT.EValue +let eArray = anArray => anArray->BInternalExpressionValue.IEvArray->BExpressionT.EValue -let eArrayString = anArray => anArray->BExpressionValue.IEvArrayString->BExpressionT.EValue +let eArrayString = anArray => anArray->BInternalExpressionValue.IEvArrayString->BExpressionT.EValue -let eBindings = (anArray: array<(string, BExpressionValue.expressionValue)>) => +let eBindings = (anArray: array<(string, BInternalExpressionValue.t)>) => anArray->BModule.fromArray->BModule.toExpressionValue->BExpressionT.EValue -let eBool = aBool => aBool->BExpressionValue.IEvBool->BExpressionT.EValue +let eBool = aBool => aBool->BInternalExpressionValue.IEvBool->BExpressionT.EValue -let eCall = (name: string): expression => name->BExpressionValue.IEvCall->BExpressionT.EValue +let eCall = (name: string): expression => + name->BInternalExpressionValue.IEvCall->BExpressionT.EValue let eFunction = (fName: string, lispArgs: list): expression => { let fn = fName->eCall @@ -30,10 +31,10 @@ let eFunction = (fName: string, lispArgs: list): expression => { let eLambda = ( parameters: array, - context: BExpressionValue.nameSpace, + context: BInternalExpressionValue.nameSpace, expr: expression, ) => { - BExpressionValue.IEvLambda({ + BInternalExpressionValue.IEvLambda({ parameters: parameters, context: context, body: NotFFI(expr)->castExpressionToInternalCode, @@ -42,27 +43,28 @@ let eLambda = ( let eLambdaFFI = (parameters: array, ffiFn: ffiFn) => { let context = BModule.emptyModule - BExpressionValue.IEvLambda({ + BInternalExpressionValue.IEvLambda({ parameters: parameters, context: context, body: FFI(ffiFn)->castExpressionToInternalCode, })->BExpressionT.EValue } -let eNumber = aNumber => aNumber->BExpressionValue.IEvNumber->BExpressionT.EValue +let eNumber = aNumber => aNumber->BInternalExpressionValue.IEvNumber->BExpressionT.EValue -let eRecord = aMap => aMap->BExpressionValue.IEvRecord->BExpressionT.EValue +let eRecord = aMap => aMap->BInternalExpressionValue.IEvRecord->BExpressionT.EValue -let eString = aString => aString->BExpressionValue.IEvString->BExpressionT.EValue +let eString = aString => aString->BInternalExpressionValue.IEvString->BExpressionT.EValue -let eSymbol = (name: string): expression => name->BExpressionValue.IEvSymbol->BExpressionT.EValue +let eSymbol = (name: string): expression => + name->BInternalExpressionValue.IEvSymbol->BExpressionT.EValue let eList = (list: list): expression => list->BExpressionT.EList let eBlock = (exprs: list): expression => eFunction("$$_block_$$", exprs) -let eModule = (nameSpace: BExpressionValue.nameSpace): expression => - nameSpace->BExpressionValue.IEvModule->BExpressionT.EValue +let eModule = (nameSpace: BInternalExpressionValue.nameSpace): expression => + nameSpace->BInternalExpressionValue.IEvModule->BExpressionT.EValue let eLetStatement = (symbol: string, valueExpression: expression): expression => eFunction("$_let_$", list{eSymbol(symbol), valueExpression}) @@ -80,7 +82,7 @@ let eBindExpressionDefault = (expression: expression): expression => eFunction("$$_bindExpression_$$", list{expression}) let eIdentifier = (name: string): expression => - name->BExpressionValue.IEvSymbol->BExpressionT.EValue + name->BInternalExpressionValue.IEvSymbol->BExpressionT.EValue let eTypeIdentifier = (name: string): expression => - name->BExpressionValue.IEvTypeIdentifier->BExpressionT.EValue + name->BInternalExpressionValue.IEvTypeIdentifier->BExpressionT.EValue 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 6026a6d1..28bdbc02 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 @@ -9,13 +9,16 @@ module Result = Belt.Result type environment = ReducerInterface_InternalExpressionValue.environment type expression = ExpressionT.expression type expressionOrFFI = ExpressionT.expressionOrFFI -type expressionValue = ReducerInterface_InternalExpressionValue.expressionValue +type internalExpressionValue = ReducerInterface_InternalExpressionValue.t // type tmpExternalBindings = ReducerInterface_InternalExpressionValue.tmpExternalBindings type internalCode = ReducerInterface_InternalExpressionValue.internalCode external castInternalCodeToExpression: internalCode => expressionOrFFI = "%identity" -let checkArity = (lambdaValue: ExpressionValue.lambdaValue, args: list) => { +let checkArity = ( + lambdaValue: ExpressionValue.lambdaValue, + args: list, +) => { let argsLength = Belt.List.length(args) let parametersLength = Js.Array2.length(lambdaValue.parameters) if argsLength !== parametersLength { @@ -25,7 +28,7 @@ let checkArity = (lambdaValue: ExpressionValue.lambdaValue, args: list) => +let checkIfReduced = (args: list) => args->Belt.List.reduceReverse(Ok(list{}), (rAcc, arg) => rAcc->Result.flatMap(acc => switch arg { @@ -55,7 +58,7 @@ let applyParametersToLambda = ( args, environment, reducer: ExpressionT.reducerFn, -): result => { +): result => { checkArity(lambdaValue, args)->Result.flatMap(args => checkIfReduced(args)->Result.flatMap(args => { let exprOrFFI = castInternalCodeToExpression(lambdaValue.body) @@ -72,10 +75,10 @@ let doLambdaCall = (lambdaValue: ExpressionValue.lambdaValue, args, environment, let foreignFunctionInterface = ( lambdaValue: ExpressionValue.lambdaValue, - argArray: array, + argArray: array, environment: ExpressionValue.environment, reducer: ExpressionT.reducerFn, -): result => { +): result => { let args = argArray->Belt.List.fromArray applyParametersToLambda(lambdaValue, args, environment, 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 4ac95bd5..2598a9ed 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 @@ -1,11 +1,11 @@ module ExpressionT = Reducer_Expression_T -module ExpressionValue = ReducerInterface_InternalExpressionValue +module InternalExpressionValue = ReducerInterface_InternalExpressionValue module ExpressionWithContext = Reducer_ExpressionWithContext module Result = Belt.Result -type environment = ExpressionValue.environment +type environment = InternalExpressionValue.environment type expression = ExpressionT.expression -type expressionValue = ExpressionValue.expressionValue +type internalExpressionValue = InternalExpressionValue.t type expressionWithContext = ExpressionWithContext.expressionWithContext let expandMacroCall = ( @@ -26,7 +26,7 @@ let doMacroCall = ( bindings: ExpressionT.bindings, environment: environment, reduceExpression: ExpressionT.reducerFn, -): result => +): result => expandMacroCall( macroExpression, 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 3af9a4f7..38910da2 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 @@ -9,19 +9,19 @@ module Extra = Reducer_Extra module InternalExpressionValue = ReducerInterface_InternalExpressionValue -type expressionValue = InternalExpressionValue.expressionValue +type internalExpressionValue = InternalExpressionValue.t type environment = ReducerInterface_InternalExpressionValue.environment type rec expression = | EList(list) // A list to map-reduce - | EValue(expressionValue) // Irreducible built-in value. Reducer should not know the internals. External libraries are responsible + | EValue(internalExpressionValue) // Irreducible built-in value. Reducer should not know the internals. External libraries are responsible and bindings = InternalExpressionValue.nameSpace type reducerFn = ( expression, bindings, environment, -) => result +) => result /* Converts the expression to String @@ -67,9 +67,9 @@ let inspectResult = (r: result): resu } type ffiFn = ( - array, + array, environment, -) => result +) => result type expressionOrFFI = | NotFFI(expression) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Js/Reducer_Js_Gate.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Js/Reducer_Js_Gate.res index e13255e5..d7a267e2 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Js/Reducer_Js_Gate.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Js/Reducer_Js_Gate.res @@ -8,7 +8,7 @@ external castString: unit => string = "%identity" /* As JavaScript returns us any type, we need to type check and cast type propertype before using it */ -let jsToIEv = (jsValue): result => +let jsToIEv = (jsValue): result => switch Js.typeof(jsValue) { | "boolean" => jsValue->castBool->IEvBool->Ok | "number" => jsValue->castNumber->IEvNumber->Ok diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_MathJs/Reducer_MathJs_Eval.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_MathJs/Reducer_MathJs_Eval.res index 6c8bc8e7..30cb2014 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_MathJs/Reducer_MathJs_Eval.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_MathJs/Reducer_MathJs_Eval.res @@ -17,7 +17,7 @@ let eval__: string => 'a = %raw(`function (expr) { return {value: Mathjs.evaluat /* Call MathJs evaluate and return as a variant */ -let eval = (expr: string): result => { +let eval = (expr: string): result => { try { let answer = eval__(expr) answer["value"]->JavaScript.Gate.jsToIEv diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface.res index 4ab6fd35..ef27130b 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface.res @@ -1,4 +1,4 @@ -module ExternalExpressionValue = ReducerInterface_ExpressionValue +module ExternalExpressionValue = ReducerInterface_ExternalExpressionValue module ExternalLibrary = ReducerInterface_ExternalLibrary module InternalExpressionValue = ReducerInterface_InternalExpressionValue module StdLib = ReducerInterface_StdLib diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Date.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Date.res index 3d5d095d..e84c26f4 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Date.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Date.res @@ -1,27 +1,27 @@ -module EV = ReducerInterface_InternalExpressionValue -type expressionValue = EV.expressionValue +module IEV = ReducerInterface_InternalExpressionValue +type internalExpressionValue = IEV.t -let dispatch = (call: EV.functionCall, _: DistributionOperation.env): option< - result, +let dispatch = (call: IEV.functionCall, _: DistributionOperation.env): option< + result, > => { switch call { - | ("toString", [IEvDate(t)]) => EV.IEvString(DateTime.Date.toString(t))->Ok->Some + | ("toString", [IEvDate(t)]) => IEV.IEvString(DateTime.Date.toString(t))->Ok->Some | ("makeDateFromYear", [IEvNumber(year)]) => switch DateTime.Date.makeFromYear(year) { - | Ok(t) => EV.IEvDate(t)->Ok->Some + | Ok(t) => IEV.IEvDate(t)->Ok->Some | Error(e) => Reducer_ErrorValue.RETodo(e)->Error->Some } - | ("dateFromNumber", [IEvNumber(f)]) => EV.IEvDate(DateTime.Date.fromFloat(f))->Ok->Some - | ("toNumber", [IEvDate(f)]) => EV.IEvNumber(DateTime.Date.toFloat(f))->Ok->Some + | ("dateFromNumber", [IEvNumber(f)]) => IEV.IEvDate(DateTime.Date.fromFloat(f))->Ok->Some + | ("toNumber", [IEvDate(f)]) => IEV.IEvNumber(DateTime.Date.toFloat(f))->Ok->Some | ("subtract", [IEvDate(d1), IEvDate(d2)]) => switch DateTime.Date.subtract(d1, d2) { - | Ok(d) => EV.IEvTimeDuration(d)->Ok + | Ok(d) => IEV.IEvTimeDuration(d)->Ok | Error(e) => Error(RETodo(e)) }->Some | ("subtract", [IEvDate(d1), IEvTimeDuration(d2)]) => - EV.IEvDate(DateTime.Date.subtractDuration(d1, d2))->Ok->Some + IEV.IEvDate(DateTime.Date.subtractDuration(d1, d2))->Ok->Some | ("add", [IEvDate(d1), IEvTimeDuration(d2)]) => - EV.IEvDate(DateTime.Date.addDuration(d1, d2))->Ok->Some + IEV.IEvDate(DateTime.Date.addDuration(d1, d2))->Ok->Some | _ => None } } diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Duration.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Duration.res index f0b31ae2..838e4375 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Duration.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Duration.res @@ -1,35 +1,36 @@ -module EV = ReducerInterface_InternalExpressionValue -type expressionValue = EV.expressionValue +module IEV = ReducerInterface_InternalExpressionValue +type internalExpressionValue = IEV.t -let dispatch = (call: EV.functionCall, _: DistributionOperation.env): option< - result, +let dispatch = (call: IEV.functionCall, _: DistributionOperation.env): option< + result, > => { switch call { - | ("toString", [IEvTimeDuration(t)]) => EV.IEvString(DateTime.Duration.toString(t))->Ok->Some - | ("minutes", [IEvNumber(f)]) => EV.IEvTimeDuration(DateTime.Duration.fromMinutes(f))->Ok->Some + | ("toString", [IEvTimeDuration(t)]) => IEV.IEvString(DateTime.Duration.toString(t))->Ok->Some + | ("minutes", [IEvNumber(f)]) => IEV.IEvTimeDuration(DateTime.Duration.fromMinutes(f))->Ok->Some | ("fromUnit_minutes", [IEvNumber(f)]) => - EV.IEvTimeDuration(DateTime.Duration.fromMinutes(f))->Ok->Some - | ("hours", [IEvNumber(f)]) => EV.IEvTimeDuration(DateTime.Duration.fromHours(f))->Ok->Some + IEV.IEvTimeDuration(DateTime.Duration.fromMinutes(f))->Ok->Some + | ("hours", [IEvNumber(f)]) => IEV.IEvTimeDuration(DateTime.Duration.fromHours(f))->Ok->Some | ("fromUnit_hours", [IEvNumber(f)]) => - EV.IEvTimeDuration(DateTime.Duration.fromHours(f))->Ok->Some - | ("days", [IEvNumber(f)]) => EV.IEvTimeDuration(DateTime.Duration.fromDays(f))->Ok->Some - | ("fromUnit_days", [IEvNumber(f)]) => EV.IEvTimeDuration(DateTime.Duration.fromDays(f))->Ok->Some - | ("years", [IEvNumber(f)]) => EV.IEvTimeDuration(DateTime.Duration.fromYears(f))->Ok->Some + IEV.IEvTimeDuration(DateTime.Duration.fromHours(f))->Ok->Some + | ("days", [IEvNumber(f)]) => IEV.IEvTimeDuration(DateTime.Duration.fromDays(f))->Ok->Some + | ("fromUnit_days", [IEvNumber(f)]) => + IEV.IEvTimeDuration(DateTime.Duration.fromDays(f))->Ok->Some + | ("years", [IEvNumber(f)]) => IEV.IEvTimeDuration(DateTime.Duration.fromYears(f))->Ok->Some | ("fromUnit_years", [IEvNumber(f)]) => - EV.IEvTimeDuration(DateTime.Duration.fromYears(f))->Ok->Some - | ("toHours", [IEvTimeDuration(f)]) => EV.IEvNumber(DateTime.Duration.toHours(f))->Ok->Some - | ("toMinutes", [IEvTimeDuration(f)]) => EV.IEvNumber(DateTime.Duration.toMinutes(f))->Ok->Some - | ("toDays", [IEvTimeDuration(f)]) => EV.IEvNumber(DateTime.Duration.toDays(f))->Ok->Some - | ("toYears", [IEvTimeDuration(f)]) => EV.IEvNumber(DateTime.Duration.toYears(f))->Ok->Some + IEV.IEvTimeDuration(DateTime.Duration.fromYears(f))->Ok->Some + | ("toHours", [IEvTimeDuration(f)]) => IEV.IEvNumber(DateTime.Duration.toHours(f))->Ok->Some + | ("toMinutes", [IEvTimeDuration(f)]) => IEV.IEvNumber(DateTime.Duration.toMinutes(f))->Ok->Some + | ("toDays", [IEvTimeDuration(f)]) => IEV.IEvNumber(DateTime.Duration.toDays(f))->Ok->Some + | ("toYears", [IEvTimeDuration(f)]) => IEV.IEvNumber(DateTime.Duration.toYears(f))->Ok->Some | ("add", [IEvTimeDuration(d1), IEvTimeDuration(d2)]) => - EV.IEvTimeDuration(DateTime.Duration.add(d1, d2))->Ok->Some + IEV.IEvTimeDuration(DateTime.Duration.add(d1, d2))->Ok->Some | ("subtract", [IEvTimeDuration(d1), IEvTimeDuration(d2)]) => - EV.IEvTimeDuration(DateTime.Duration.subtract(d1, d2))->Ok->Some + IEV.IEvTimeDuration(DateTime.Duration.subtract(d1, d2))->Ok->Some | ("multiply", [IEvTimeDuration(d1), IEvNumber(d2)]) => - EV.IEvTimeDuration(DateTime.Duration.multiply(d1, d2))->Ok->Some + IEV.IEvTimeDuration(DateTime.Duration.multiply(d1, d2))->Ok->Some | ("divide", [IEvTimeDuration(d1), IEvNumber(d2)]) => - EV.IEvTimeDuration(DateTime.Duration.divide(d1, d2))->Ok->Some - | ("divide", [IEvTimeDuration(d1), IEvTimeDuration(d2)]) => EV.IEvNumber(d1 /. d2)->Ok->Some + IEV.IEvTimeDuration(DateTime.Duration.divide(d1, d2))->Ok->Some + | ("divide", [IEvTimeDuration(d1), IEvTimeDuration(d2)]) => IEV.IEvNumber(d1 /. d2)->Ok->Some | _ => None } } diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalExpressionValue.res similarity index 87% rename from packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res rename to packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalExpressionValue.res index 0f8b0249..bd5d8a35 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExpressionValue.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalExpressionValue.res @@ -8,8 +8,8 @@ module ErrorValue = Reducer_ErrorValue type internalCode = Object @genType -type rec expressionValue = - | EvArray(array) +type rec externalExpressionValue = + | EvArray(array) | EvArrayString(array) | EvBool(bool) | EvCall(string) // External function call @@ -24,7 +24,7 @@ type rec expressionValue = | EvDeclaration(lambdaDeclaration) | EvTypeIdentifier(string) | EvModule(record) -and record = Js.Dict.t +and record = Js.Dict.t and externalBindings = record and lambdaValue = { parameters: array, @@ -33,7 +33,10 @@ and lambdaValue = { } and lambdaDeclaration = Declaration.declaration -type functionCall = (string, array) +@genType +type t = externalExpressionValue + +type functionCall = (string, array) let rec toString = aValue => switch aValue { @@ -68,7 +71,7 @@ and toStringRecord = aRecord => { `{${pairs}}` } -let argsToString = (args: array): string => { +let argsToString = (args: array): string => { args->Js.Array2.map(arg => arg->toString)->Js.Array2.toString } @@ -80,18 +83,6 @@ let toStringResult = x => | Error(m) => `Error(${ErrorValue.errorToString(m)})` } -// let toStringResultOkless = (codeResult: result): string => -// switch codeResult { -// | Ok(a) => toString(a) -// | Error(m) => `Error(${ErrorValue.errorToString(m)})` -// } - -// let toStringResultRecord = x => -// switch x { -// | Ok(a) => `Ok(${toStringRecord(a)})` -// | Error(m) => `Error(${ErrorValue.errorToString(m)})` -// } - @genType type environment = DistributionOperation.env diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res index a003f7b5..3e6172c9 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res @@ -1,6 +1,6 @@ -module ExpressionValue = ReducerInterface_InternalExpressionValue +module InternalExpressionValue = ReducerInterface_InternalExpressionValue -type expressionValue = ExpressionValue.expressionValue +type internalExpressionValue = InternalExpressionValue.t // module Sample = { // // In real life real libraries should be somewhere else @@ -17,14 +17,14 @@ type expressionValue = ExpressionValue.expressionValue // I expect that it's important to build this first, so it doesn't get recalculated for each tryRegistry() call. let registry = FunctionRegistry_Library.registry -let tryRegistry = ((fnName, args): ExpressionValue.functionCall, env) => { +let tryRegistry = ((fnName, args): InternalExpressionValue.functionCall, env) => { FunctionRegistry_Core.Registry.matchAndRun(~registry, ~fnName, ~args, ~env)->E.O2.fmap( E.R2.errMap(_, s => Reducer_ErrorValue.RETodo(s)), ) } -let dispatch = (call: ExpressionValue.functionCall, environment, chain): result< - expressionValue, +let dispatch = (call: InternalExpressionValue.functionCall, environment, chain): result< + internalExpressionValue, 'e, > => { E.A.O.firstSomeFn([ diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res index fd3ebadf..6ca0a440 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res @@ -1,5 +1,5 @@ -module ExpressionValue = ReducerInterface_InternalExpressionValue -type expressionValue = ExpressionValue.expressionValue +module IEV = ReducerInterface_InternalExpressionValue +type internalExpressionValue = IEV.t module Helpers = { let arithmeticMap = r => @@ -18,7 +18,7 @@ module Helpers = { | _ => #Multiply } - let catchAndConvertTwoArgsToDists = (args: array): option<( + let catchAndConvertTwoArgsToDists = (args: array): option<( DistributionTypes.genericDist, DistributionTypes.genericDist, )> => @@ -80,23 +80,28 @@ module Helpers = { )->DistributionOperation.run(~env) } - let parseNumber = (args: expressionValue): Belt.Result.t => + let parseNumber = (args: internalExpressionValue): Belt.Result.t => switch args { | IEvNumber(x) => Ok(x) | _ => Error("Not a number") } - let parseNumberArray = (ags: array): Belt.Result.t, string> => - E.A.fmap(parseNumber, ags) |> E.A.R.firstErrorOrOpen + let parseNumberArray = (ags: array): Belt.Result.t< + array, + string, + > => E.A.fmap(parseNumber, ags) |> E.A.R.firstErrorOrOpen - let parseDist = (args: expressionValue): Belt.Result.t => + let parseDist = (args: internalExpressionValue): Belt.Result.t< + DistributionTypes.genericDist, + string, + > => switch args { | IEvDistribution(x) => Ok(x) | IEvNumber(x) => Ok(GenericDist.fromFloat(x)) | _ => Error("Not a distribution") } - let parseDistributionArray = (ags: array): Belt.Result.t< + let parseDistributionArray = (ags: array): Belt.Result.t< array, string, > => E.A.fmap(parseDist, ags) |> E.A.R.firstErrorOrOpen @@ -122,7 +127,7 @@ module Helpers = { } let mixture = ( - args: array, + args: array, ~env: DistributionOperation.env, ): DistributionOperation.outputType => { let error = (err: string): DistributionOperation.outputType => @@ -194,10 +199,9 @@ module SymbolicConstructors = { } } -let dispatchToGenericOutput = ( - call: ExpressionValue.functionCall, - env: DistributionOperation.env, -): option => { +let dispatchToGenericOutput = (call: IEV.functionCall, env: DistributionOperation.env): option< + DistributionOperation.outputType, +> => { let (fnName, args) = call switch (fnName, args) { | ("triangular" as fnName, [IEvNumber(f1), IEvNumber(f2), IEvNumber(f3)]) => @@ -357,7 +361,7 @@ let dispatchToGenericOutput = ( } let genericOutputToReducerValue = (o: DistributionOperation.outputType): result< - expressionValue, + internalExpressionValue, Reducer_ErrorValue.errorValue, > => switch o { @@ -370,5 +374,5 @@ let genericOutputToReducerValue = (o: DistributionOperation.outputType): result< | GenDistError(err) => Error(REDistributionError(err)) } -let dispatch = (call: ExpressionValue.functionCall, environment) => +let dispatch = (call: IEV.functionCall, environment) => dispatchToGenericOutput(call, environment)->E.O2.fmap(genericOutputToReducerValue) diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.resi b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.resi index 1c7948ad..6cccdf17 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.resi +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.resi @@ -1,6 +1,4 @@ let dispatch: ( ReducerInterface_InternalExpressionValue.functionCall, - ReducerInterface_ExpressionValue.environment, -) => option< - result, -> + ReducerInterface_ExternalExpressionValue.environment, +) => option> diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res index 7290c12e..30de55dd 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res @@ -1,13 +1,13 @@ module ErrorValue = Reducer_ErrorValue -module ExternalExpressionValue = ReducerInterface_ExpressionValue +module ExternalExpressionValue = ReducerInterface_ExternalExpressionValue module Extra_Array = Reducer_Extra_Array type internalCode = ExternalExpressionValue.internalCode type environment = ExternalExpressionValue.environment let defaultEnvironment = ExternalExpressionValue.defaultEnvironment -type rec expressionValue = - | IEvArray(array) // FIXME: Convert +type rec t = + | IEvArray(array) // FIXME: Convert | IEvArrayString(array) // FIXME: Convert | IEvBool(bool) | IEvCall(string) // External function call @@ -15,16 +15,15 @@ type rec expressionValue = | IEvDeclaration(lambdaDeclaration) | IEvDistribution(DistributionTypes.genericDist) | IEvLambda(lambdaValue) - | IEvModule(nameSpace) // FIXME: Convert + | IEvModule(nameSpace) | IEvNumber(float) | IEvRecord(map) | IEvString(string) | IEvSymbol(string) | IEvTimeDuration(float) | IEvTypeIdentifier(string) -and map = Belt.Map.String.t -and nameSpace = NameSpace(Belt.Map.String.t) -and tmpExternalBindings = Js.Dict.t // FIXME: Remove +and map = Belt.Map.String.t +and nameSpace = NameSpace(Belt.Map.String.t) and lambdaValue = { parameters: array, context: nameSpace, @@ -32,9 +31,9 @@ and lambdaValue = { } and lambdaDeclaration = Declaration.declaration -type t = expressionValue +type internalExpressionValue = t -type functionCall = (string, array) +type functionCall = (string, array) let rec toString = aValue => switch aValue { @@ -92,7 +91,7 @@ let toStringWithType = aValue => | IEvModule(_) => `Module::${toString(aValue)}` } -let argsToString = (args: array): string => { +let argsToString = (args: array): string => { args->Js.Array2.map(arg => arg->toString)->Js.Array2.toString } @@ -104,7 +103,7 @@ let toStringResult = x => | Error(m) => `Error(${ErrorValue.errorToString(m)})` } -let toStringResultOkless = (codeResult: result): string => +let toStringResultOkless = (codeResult: result): string => switch codeResult { | Ok(a) => toString(a) | Error(m) => `Error(${ErrorValue.errorToString(m)})` @@ -116,7 +115,7 @@ let toStringResultRecord = x => | Error(m) => `Error(${ErrorValue.errorToString(m)})` } -type expressionValueType = +type internalExpressionValueType = | EvtArray | EvtArrayString | EvtBool @@ -133,9 +132,9 @@ type expressionValueType = | EvtTypeIdentifier | EvtModule -type functionCallSignature = CallSignature(string, array) +type functionCallSignature = CallSignature(string, array) type functionDefinitionSignature = - FunctionDefinitionSignature(functionCallSignature, expressionValueType) + FunctionDefinitionSignature(functionCallSignature, internalExpressionValueType) let valueToValueType = value => switch value { @@ -161,7 +160,7 @@ let functionCallToCallSignature = (functionCall: functionCall): functionCallSign CallSignature(fn, args->Js.Array2.map(valueToValueType)) } -let valueTypeToString = (valueType: expressionValueType): string => +let valueTypeToString = (valueType: internalExpressionValueType): string => switch valueType { | EvtArray => `Array` | EvtArrayString => `ArrayString` @@ -185,7 +184,7 @@ let functionCallSignatureToString = (functionCallSignature: functionCallSignatur `${fn}(${args->Js.Array2.map(valueTypeToString)->Js.Array2.toString})` } -let rec toExternal = (iev: expressionValue): ExternalExpressionValue.expressionValue => { +let rec toExternal = (iev: t): ExternalExpressionValue.t => { switch iev { | IEvArray(v) => v->Belt.Array.map(e => toExternal(e))->EvArray | IEvArrayString(v) => EvArrayString(v) @@ -218,12 +217,12 @@ and lambdaValueToExternal = v => { } and nameSpaceToTypeScriptBindings = ( nameSpace: nameSpace, -): ReducerInterface_ExpressionValue.externalBindings => { +): ReducerInterface_ExternalExpressionValue.externalBindings => { let NameSpace(container) = nameSpace Belt.Map.String.map(container, e => toExternal(e))->Belt.Map.String.toArray->Js.Dict.fromArray } -let rec toInternal = (ev: ExternalExpressionValue.expressionValue): expressionValue => { +let rec toInternal = (ev: ExternalExpressionValue.t): t => { switch ev { | EvArray(v) => v->Belt.Array.map(e => toInternal(e))->IEvArray | EvArrayString(v) => IEvArrayString(v) @@ -255,6 +254,6 @@ and lambdaValueToInternal = v => { {parameters: p, context: c, body: b} } and nameSpaceFromTypeScriptBindings = ( - r: ReducerInterface_ExpressionValue.externalBindings, + r: ReducerInterface_ExternalExpressionValue.externalBindings, ): nameSpace => r->Js.Dict.entries->Belt.Map.String.fromArray->Belt.Map.String.map(e => toInternal(e))->NameSpace diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Number.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Number.res index 4787880d..900e6a2f 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Number.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Number.res @@ -1,5 +1,5 @@ -module EV = ReducerInterface_InternalExpressionValue -type expressionValue = EV.expressionValue +module IEV = ReducerInterface_InternalExpressionValue +type internalExpressionValue = IEV.t module ScientificUnit = { let nameToMultiplier = str => @@ -24,8 +24,8 @@ module ScientificUnit = { } } -let dispatch = (call: EV.functionCall, _: DistributionOperation.env): option< - result, +let dispatch = (call: IEV.functionCall, _: DistributionOperation.env): option< + result, > => { switch call { | ( @@ -39,7 +39,7 @@ let dispatch = (call: EV.functionCall, _: DistributionOperation.env): option< | "fromUnit_P") as op, [IEvNumber(f)], ) => - op->ScientificUnit.getMultiplier->E.O2.fmap(multiplier => EV.IEvNumber(f *. multiplier)->Ok) + op->ScientificUnit.getMultiplier->E.O2.fmap(multiplier => IEV.IEvNumber(f *. multiplier)->Ok) | _ => None } } diff --git a/packages/squiggle-lang/src/rescript/TypescriptInterface.res b/packages/squiggle-lang/src/rescript/TypescriptInterface.res index 932edaa1..387c5526 100644 --- a/packages/squiggle-lang/src/rescript/TypescriptInterface.res +++ b/packages/squiggle-lang/src/rescript/TypescriptInterface.res @@ -47,10 +47,10 @@ let evaluatePartialUsingExternalBindings = Reducer.evaluatePartialUsingExternalB type externalBindings = Reducer.externalBindings @genType -type expressionValue = ReducerInterface_ExpressionValue.expressionValue +type expressionValue = ReducerInterface_ExternalExpressionValue.t @genType -type recordEV = ReducerInterface_ExpressionValue.record +type recordEV = ReducerInterface_ExternalExpressionValue.record @genType type errorValue = Reducer_ErrorValue.errorValue @@ -74,19 +74,19 @@ let errorValueToString = Reducer_ErrorValue.errorToString let distributionErrorToString = DistributionTypes.Error.toString @genType -type lambdaValue = ReducerInterface_ExpressionValue.lambdaValue +type lambdaValue = ReducerInterface_ExternalExpressionValue.lambdaValue @genType -type lambdaDeclaration = ReducerInterface_ExpressionValue.lambdaDeclaration +type lambdaDeclaration = ReducerInterface_ExternalExpressionValue.lambdaDeclaration @genType let defaultSamplingEnv = DistributionOperation.defaultEnv @genType -type environment = ReducerInterface_ExpressionValue.environment +type environment = ReducerInterface_ExternalExpressionValue.environment @genType -let defaultEnvironment = ReducerInterface_ExpressionValue.defaultEnvironment +let defaultEnvironment = ReducerInterface_ExternalExpressionValue.defaultEnvironment @genType let foreignFunctionInterface = Reducer.foreignFunctionInterface