diff --git a/packages/squiggle-lang/src/rescript/ProgramEvaluator.res b/packages/squiggle-lang/src/rescript/ProgramEvaluator.res index b2317050..96a765d8 100644 --- a/packages/squiggle-lang/src/rescript/ProgramEvaluator.res +++ b/packages/squiggle-lang/src/rescript/ProgramEvaluator.res @@ -39,17 +39,16 @@ module Inputs = { type exportDistribution = [ | #DistPlus(DistPlus.t) | #Float(float) - | #Function((float) => Belt.Result.t) + | #Function(float => Belt.Result.t) ] type exportEnv = array<(string, ASTTypes.node)> type exportType = { - environment : exportEnv, - exports: array + environment: exportEnv, + exports: array, } - module Internals = { let addVariable = ( {samplingInputs, squiggleString, environment}: Inputs.inputs, @@ -58,9 +57,7 @@ module Internals = { ): Inputs.inputs => { samplingInputs: samplingInputs, squiggleString: squiggleString, - environment: ASTTypes.Environment.update(environment, str, _ => Some( - node, - )), + environment: ASTTypes.Environment.update(environment, str, _ => Some(node)), } type outputs = { @@ -76,8 +73,7 @@ module Internals = { pointSetDistLength: inputs.samplingInputs.pointDistLength |> E.O.default(10000), } - let runNode = (inputs, node) => - AST.toLeaf(makeInputs(inputs), inputs.environment, node) + let runNode = (inputs, node) => AST.toLeaf(makeInputs(inputs), inputs.environment, node) let renderIfNeeded = (inputs: Inputs.inputs, node: ASTTypes.node): result< ASTTypes.node, @@ -106,16 +102,14 @@ module Internals = { let outputToDistPlus = (inputs: Inputs.inputs, pointSetDist: PointSetTypes.pointSetDist) => DistPlus.make(~pointSetDist, ~squiggleString=Some(inputs.squiggleString), ()) - let rec returnDist = (functionInfo : (array, ASTTypes.node), - inputs : Inputs.inputs, - env : ASTTypes.environment) => { - (input : float) => { - let foo: Inputs.inputs = {...inputs, environment: env}; - evaluateFunction( - foo, - functionInfo, - [#SymbolicDist(#Float(input))], - ) |> E.R.bind(_, a => + let rec returnDist = ( + functionInfo: (array, ASTTypes.node), + inputs: Inputs.inputs, + env: ASTTypes.environment, + ) => { + (input: float) => { + let foo: Inputs.inputs = {...inputs, environment: env} + evaluateFunction(foo, functionInfo, [#SymbolicDist(#Float(input))]) |> E.R.bind(_, a => switch a { | #DistPlus(d) => Ok(DistPlus.T.normalize(d)) | n => @@ -126,11 +120,10 @@ module Internals = { } } // TODO: Consider using ExpressionTypes.ExpressionTree.getFloat or similar in this function - and coersionToExportedTypes = ( - inputs, - env: ASTTypes.environment, - ex: ASTTypes.node, - ): result => + and coersionToExportedTypes = (inputs, env: ASTTypes.environment, ex: ASTTypes.node): result< + exportDistribution, + string, + > => ex |> renderIfNeeded(inputs) |> E.R.bind(_, x => @@ -143,56 +136,45 @@ module Internals = { } ) - and evaluateFunction = ( - inputs: Inputs.inputs, - fn: (array, ASTTypes.node), - fnInputs, - ) => { - let output = AST.runFunction( - makeInputs(inputs), - inputs.environment, - fnInputs, - fn, - ) + and evaluateFunction = (inputs: Inputs.inputs, fn: (array, ASTTypes.node), fnInputs) => { + let output = AST.runFunction(makeInputs(inputs), inputs.environment, fnInputs, fn) output |> E.R.bind(_, coersionToExportedTypes(inputs, inputs.environment)) } let runProgram = (inputs: Inputs.inputs, p: ASTTypes.program) => { let ins = ref(inputs) p - |> E.A.fmap(x => - switch x { - | #Assignment(name, node) => - ins := addVariable(ins.contents, name, node) - None - | #Expression(node) => - Some(runNode(ins.contents, node)) - } - ) - |> E.A.O.concatSomes + |> E.A.fmap(x => + switch x { + | #Assignment(name, node) => + ins := addVariable(ins.contents, name, node) + None + | #Expression(node) => Some(runNode(ins.contents, node)) + } + ) + |> E.A.O.concatSomes + |> E.A.R.firstErrorOrOpen + |> E.R.bind(_, d => + d + |> E.A.fmap(x => coersionToExportedTypes(inputs, ins.contents.environment, x)) |> E.A.R.firstErrorOrOpen - |> E.R.bind(_, d => - d - |> E.A.fmap(x => coersionToExportedTypes(inputs, ins.contents.environment, x)) - |> E.A.R.firstErrorOrOpen - ) - |> E.R.fmap(ex => - { - environment: Belt.Map.String.toArray(ins.contents.environment), - exports: ex - } - ) + ) + |> E.R.fmap(ex => { + environment: Belt.Map.String.toArray(ins.contents.environment), + exports: ex, + }) } let inputsToLeaf = (inputs: Inputs.inputs) => Parser.fromString(inputs.squiggleString) |> E.R.bind(_, g => runProgram(inputs, g)) - } - @genType -let runAll : (string, Inputs.SamplingInputs.t, exportEnv) => result = - (squiggleString, samplingInputs, environment) => { +let runAll: (string, Inputs.SamplingInputs.t, exportEnv) => result = ( + squiggleString, + samplingInputs, + environment, +) => { let inputs = Inputs.make( ~samplingInputs, ~squiggleString,