Merge pull request #483 from quantified-uncertainty/reducer-dev

Reducer: Foreign Function Interface
This commit is contained in:
Sam Nolan 2022-05-04 16:59:30 -04:00 committed by GitHub
commit 29a2ba4e21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 0 deletions

View File

@ -9,3 +9,8 @@ describe("map reduce", () => {
testEvalToBe("arr=[1,2,3]; reverse(arr)", "Ok([3,2,1])")
testEvalToBe("check(x)=(x==2);arr=[1,2,3]; keep(arr,check)", "Ok([2])")
})
Skip.describe("map reduce (sam)", () => {
testEvalToBe("addone(x)=x+1; map(2, addone)", "Error???")
testEvalToBe("addone(x)=x+1; map(2, {x: addone})", "Error???")
})

View File

@ -1,11 +1,27 @@
module ErrorValue = Reducer_ErrorValue
module Expression = Reducer_Expression
module ExpressionValue = ReducerInterface_ExpressionValue
module Lambda = Reducer_Expression_Lambda
type environment = ReducerInterface_ExpressionValue.environment
type errorValue = Reducer_ErrorValue.errorValue
type expressionValue = ReducerInterface_ExpressionValue.expressionValue
type externalBindings = ReducerInterface_ExpressionValue.externalBindings
type lambdaValue = ExpressionValue.lambdaValue
let evaluate = Expression.evaluate
let evaluateUsingOptions = Expression.evaluateUsingOptions
let evaluatePartialUsingExternalBindings = Expression.evaluatePartialUsingExternalBindings
let parse = Expression.parse
let foreignFunctionInterface = (
lambdaValue: lambdaValue,
argArray: array<expressionValue>,
environment: ExpressionValue.environment,
) => {
Lambda.foreignFunctionInterface(lambdaValue, argArray, environment, Expression.reduceExpression)
}
let defaultEnvironment = ExpressionValue.defaultEnvironment
let defaultExternalBindings = ExpressionValue.defaultExternalBindings

View File

@ -9,6 +9,8 @@ type errorValue = Reducer_ErrorValue.errorValue
type expressionValue = ReducerInterface_ExpressionValue.expressionValue
@genType
type externalBindings = ReducerInterface_ExpressionValue.externalBindings
@genType
type lambdaValue = ReducerInterface_ExpressionValue.lambdaValue
@genType
let evaluateUsingOptions: (
@ -26,3 +28,16 @@ let evaluatePartialUsingExternalBindings: (
let evaluate: string => result<expressionValue, errorValue>
let parse: string => result<Expression.expression, errorValue>
@genType
let foreignFunctionInterface: (
QuriSquiggleLang.ReducerInterface_ExpressionValue.lambdaValue,
array<QuriSquiggleLang.ReducerInterface_ExpressionValue.expressionValue>,
QuriSquiggleLang.ReducerInterface_ExpressionValue.environment,
) => result<expressionValue, errorValue>
@genType
let defaultEnvironment: environment
@genType
let defaultExternalBindings: externalBindings

View File

@ -57,3 +57,13 @@ let applyParametersToLambda = (
let doLambdaCall = (lambdaValue: ExpressionValue.lambdaValue, args, environment, reducer) =>
applyParametersToLambda(lambdaValue, args, environment, reducer)
let foreignFunctionInterface = (
lambdaValue: ExpressionValue.lambdaValue,
argArray: array<expressionValue>,
environment: ExpressionValue.environment,
reducer: ExpressionT.reducerFn,
): result<expressionValue, 'e> => {
let args = argArray->Belt.List.fromArray
applyParametersToLambda(lambdaValue, args, environment, reducer)
}