ran rescript format on ProgramEvaluator.res

This commit is contained in:
Quinn Dougherty 2022-04-10 23:42:05 -04:00
parent b12f3f0c2d
commit e4e8e4b847

View File

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