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 = (
inputs : Inputs.inputs, functionInfo: (array<string>, ASTTypes.node),
env : ASTTypes.environment) => { inputs: Inputs.inputs,
(input : float) => { env: ASTTypes.environment,
let foo: Inputs.inputs = {...inputs, environment: env}; ) => {
evaluateFunction( (input: float) => {
foo, let foo: Inputs.inputs = {...inputs, environment: env}
functionInfo, evaluateFunction(foo, functionInfo, [#SymbolicDist(#Float(input))]) |> E.R.bind(_, a =>
[#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,56 +136,45 @@ 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))
} }
let runProgram = (inputs: Inputs.inputs, p: ASTTypes.program) => { let runProgram = (inputs: Inputs.inputs, p: ASTTypes.program) => {
let ins = ref(inputs) let ins = ref(inputs)
p p
|> E.A.fmap(x => |> E.A.fmap(x =>
switch x { switch x {
| #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 |> E.A.R.firstErrorOrOpen
|> E.R.bind(_, d =>
d
|> E.A.fmap(x => coersionToExportedTypes(inputs, ins.contents.environment, x))
|> E.A.R.firstErrorOrOpen |> E.A.R.firstErrorOrOpen
|> E.R.bind(_, d => )
d |> E.R.fmap(ex => {
|> E.A.fmap(x => coersionToExportedTypes(inputs, ins.contents.environment, x)) environment: Belt.Map.String.toArray(ins.contents.environment),
|> E.A.R.firstErrorOrOpen exports: ex,
) })
|> E.R.fmap(ex =>
{
environment: Belt.Map.String.toArray(ins.contents.environment),
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,