Removed AST module from ASTTypes file

This commit is contained in:
Ozzie Gooen 2022-02-17 08:51:24 -05:00
parent f76eaf6d03
commit a324f8a7d6
7 changed files with 290 additions and 305 deletions

View File

@ -10,7 +10,7 @@ let makeTest = (~only=false, str, item1, item2) =>
expect(item1) |> toEqual(item2) expect(item1) |> toEqual(item2)
); );
let evalParams: ASTTypes.AST.evaluationParams = { let evalParams: ASTTypes.evaluationParams = {
samplingInputs: { samplingInputs: {
sampleCount: 1000, sampleCount: 1000,
outputXYPoints: 10000, outputXYPoints: 10000,

View File

@ -14,7 +14,7 @@ module Inputs = {
type inputs = { type inputs = {
squiggleString: string, squiggleString: string,
samplingInputs: SamplingInputs.t, samplingInputs: SamplingInputs.t,
environment: ASTTypes.AST.environment, environment: ASTTypes.environment,
} }
let empty: SamplingInputs.t = { let empty: SamplingInputs.t = {
@ -27,7 +27,7 @@ module Inputs = {
let make = ( let make = (
~samplingInputs=empty, ~samplingInputs=empty,
~squiggleString, ~squiggleString,
~environment=ASTTypes.AST.Environment.empty, ~environment=ASTTypes.Environment.empty,
(), (),
): inputs => { ): inputs => {
samplingInputs: samplingInputs, samplingInputs: samplingInputs,
@ -40,8 +40,8 @@ type \"export" = [
| #DistPlus(DistPlus.t) | #DistPlus(DistPlus.t)
| #Float(float) | #Float(float)
| #Function( | #Function(
(array<string>, ASTTypes.AST.node), (array<string>, ASTTypes.node),
ASTTypes.AST.environment, ASTTypes.environment,
) )
] ]
@ -53,13 +53,13 @@ module Internals = {
): Inputs.inputs => { ): Inputs.inputs => {
samplingInputs: samplingInputs, samplingInputs: samplingInputs,
squiggleString: squiggleString, squiggleString: squiggleString,
environment: ASTTypes.AST.Environment.update(environment, str, _ => Some( environment: ASTTypes.Environment.update(environment, str, _ => Some(
node, node,
)), )),
} }
type outputs = { type outputs = {
graph: ASTTypes.AST.node, graph: ASTTypes.node,
pointSetDist: PointSetTypes.pointSetDist, pointSetDist: PointSetTypes.pointSetDist,
} }
let makeOutputs = (graph, pointSetDist): outputs => {graph: graph, pointSetDist: pointSetDist} let makeOutputs = (graph, pointSetDist): outputs => {graph: graph, pointSetDist: pointSetDist}
@ -74,7 +74,7 @@ module Internals = {
let runNode = (inputs, node) => let runNode = (inputs, node) =>
AST.toLeaf(makeInputs(inputs), inputs.environment, node) AST.toLeaf(makeInputs(inputs), inputs.environment, node)
let runProgram = (inputs: Inputs.inputs, p: ASTTypes.AST.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 =>
@ -97,8 +97,8 @@ module Internals = {
DistPlus.make(~pointSetDist, ~squiggleString=Some(inputs.squiggleString), ()) DistPlus.make(~pointSetDist, ~squiggleString=Some(inputs.squiggleString), ())
} }
let renderIfNeeded = (inputs: Inputs.inputs, node: ASTTypes.AST.node): result< let renderIfNeeded = (inputs: Inputs.inputs, node: ASTTypes.node): result<
ASTTypes.AST.node, ASTTypes.node,
string, string,
> => > =>
node |> ( node |> (
@ -121,11 +121,11 @@ let renderIfNeeded = (inputs: Inputs.inputs, node: ASTTypes.AST.node): result<
} }
) )
// TODO: Consider using ASTTypes.AST.getFloat or similar in this function // TODO: Consider using ASTTypes.getFloat or similar in this function
let coersionToExportedTypes = ( let coersionToExportedTypes = (
inputs, inputs,
env: ASTTypes.AST.environment, env: ASTTypes.environment,
node: ASTTypes.AST.node, node: ASTTypes.node,
): result<\"export", string> => ): result<\"export", string> =>
node node
|> renderIfNeeded(inputs) |> renderIfNeeded(inputs)
@ -160,7 +160,7 @@ let evaluateProgram = (inputs: Inputs.inputs) =>
let evaluateFunction = ( let evaluateFunction = (
inputs: Inputs.inputs, inputs: Inputs.inputs,
fn: (array<string>, ASTTypes.AST.node), fn: (array<string>, ASTTypes.node),
fnInputs, fnInputs,
) => { ) => {
let output = AST.runFunction( let output = AST.runFunction(

View File

@ -1,6 +1,6 @@
open ASTTypes.AST open ASTTypes
let toString = ASTTypes.AST.Node.toString let toString = ASTTypes.Node.toString
let envs = (samplingInputs, environment) => { let envs = (samplingInputs, environment) => {
samplingInputs: samplingInputs, samplingInputs: samplingInputs,
@ -18,7 +18,7 @@ let toPointSetDist = (samplingInputs, environment, node: node) =>
| Error(e) => Error(e) | Error(e) => Error(e)
} }
let runFunction = (samplingInputs, environment, inputs, fn: ASTTypes.AST.Function.t) => { let runFunction = (samplingInputs, environment, inputs, fn: ASTTypes.Function.t) => {
let params = envs(samplingInputs, environment) let params = envs(samplingInputs, environment)
ASTTypes.AST.Function.run(params, inputs, fn) ASTTypes.Function.run(params, inputs, fn)
} }

View File

@ -1,4 +1,4 @@
open ASTTypes.AST open ASTTypes
type t = node type t = node
type tResult = node => result<node, string> type tResult = node => result<node, string>
@ -43,12 +43,12 @@ module AlgebraicCombination = {
let combine = (evaluationParams, algebraicOp, t1: node, t2: node): result<node, string> => let combine = (evaluationParams, algebraicOp, t1: node, t2: node): result<node, string> =>
E.R.merge( E.R.merge(
ASTTypes.AST.SamplingDistribution.renderIfIsNotSamplingDistribution(evaluationParams, t1), ASTTypes.SamplingDistribution.renderIfIsNotSamplingDistribution(evaluationParams, t1),
ASTTypes.AST.SamplingDistribution.renderIfIsNotSamplingDistribution(evaluationParams, t2), ASTTypes.SamplingDistribution.renderIfIsNotSamplingDistribution(evaluationParams, t2),
) |> E.R.bind(_, ((a, b)) => ) |> E.R.bind(_, ((a, b)) =>
switch choose(a, b) { switch choose(a, b) {
| #Sampling => | #Sampling =>
ASTTypes.AST.SamplingDistribution.combineShapesUsingSampling( ASTTypes.SamplingDistribution.combineShapesUsingSampling(
evaluationParams, evaluationParams,
algebraicOp, algebraicOp,
a, a,
@ -123,7 +123,7 @@ module PointwiseCombination = {
module Truncate = { module Truncate = {
type simplificationResult = [ type simplificationResult = [
| #Solution(ASTTypes.AST.node) | #Solution(ASTTypes.node)
| #Error(string) | #Error(string)
| #NoSolution | #NoSolution
] ]
@ -171,7 +171,7 @@ module Normalize = {
switch t { switch t {
| #RenderedDist(s) => Ok(#RenderedDist(PointSetDist.T.normalize(s))) | #RenderedDist(s) => Ok(#RenderedDist(PointSetDist.T.normalize(s)))
| #SymbolicDist(_) => Ok(t) | #SymbolicDist(_) => Ok(t)
| _ => ASTTypes.AST.Node.evaluateAndRetry(evaluationParams, operationToLeaf, t) | _ => ASTTypes.Node.evaluateAndRetry(evaluationParams, operationToLeaf, t)
} }
} }
@ -181,13 +181,13 @@ module FunctionCall = {
let _runLocalFunction = (name, evaluationParams: evaluationParams, args) => let _runLocalFunction = (name, evaluationParams: evaluationParams, args) =>
Environment.getFunction(evaluationParams.environment, name) |> E.R.bind(_, ((argNames, fn)) => Environment.getFunction(evaluationParams.environment, name) |> E.R.bind(_, ((argNames, fn)) =>
ASTTypes.AST.Function.run(evaluationParams, args, (argNames, fn)) ASTTypes.Function.run(evaluationParams, args, (argNames, fn))
) )
let _runWithEvaluatedInputs = ( let _runWithEvaluatedInputs = (
evaluationParams: ASTTypes.AST.evaluationParams, evaluationParams: ASTTypes.evaluationParams,
name, name,
args: array<ASTTypes.AST.node>, args: array<ASTTypes.node>,
) => ) =>
_runHardcodedFunction(name, evaluationParams, args) |> E.O.default( _runHardcodedFunction(name, evaluationParams, args) |> E.O.default(
_runLocalFunction(name, evaluationParams, args), _runLocalFunction(name, evaluationParams, args),
@ -212,7 +212,7 @@ module Render = {
), ),
) )
| #RenderedDist(_) as t => Ok(t) // already a rendered pointSetDist, we're done here | #RenderedDist(_) as t => Ok(t) // already a rendered pointSetDist, we're done here
| _ => ASTTypes.AST.Node.evaluateAndRetry(evaluationParams, operationToLeaf, t) | _ => ASTTypes.Node.evaluateAndRetry(evaluationParams, operationToLeaf, t)
} }
} }
@ -222,7 +222,7 @@ module Render = {
but most often it will produce a RenderedDist. but most often it will produce a RenderedDist.
This function is used mainly to turn a parse tree into a single RenderedDist This function is used mainly to turn a parse tree into a single RenderedDist
that can then be displayed to the user. */ that can then be displayed to the user. */
let rec toLeaf = (evaluationParams: ASTTypes.AST.evaluationParams, node: t): result<t, string> => let rec toLeaf = (evaluationParams: ASTTypes.evaluationParams, node: t): result<t, string> =>
switch node { switch node {
// Leaf nodes just stay leaf nodes // Leaf nodes just stay leaf nodes
| #SymbolicDist(_) | #SymbolicDist(_)
@ -248,7 +248,7 @@ let rec toLeaf = (evaluationParams: ASTTypes.AST.evaluationParams, node: t): res
|> E.A.R.firstErrorOrOpen |> E.A.R.firstErrorOrOpen
|> E.R.fmap(r => #Hash(r)) |> E.R.fmap(r => #Hash(r))
| #Symbol(r) => | #Symbol(r) =>
ASTTypes.AST.Environment.get(evaluationParams.environment, r) ASTTypes.Environment.get(evaluationParams.environment, r)
|> E.O.toResult("Undeclared variable " ++ r) |> E.O.toResult("Undeclared variable " ++ r)
|> E.R.bind(_, toLeaf(evaluationParams)) |> E.R.bind(_, toLeaf(evaluationParams))
| #FunctionCall(name, args) => | #FunctionCall(name, args) =>

View File

@ -1,4 +1,3 @@
module AST = {
type rec hash = array<(string, node)> type rec hash = array<(string, node)>
and node = [ and node = [
| #SymbolicDist(SymbolicDistTypes.symbolicDist) | #SymbolicDist(SymbolicDistTypes.symbolicDist)
@ -145,10 +144,7 @@ module AST = {
let newEnvironment = Belt.Array.zip(argumentNames(t), args) |> Environment.fromArray let newEnvironment = Belt.Array.zip(argumentNames(t), args) |> Environment.fromArray
let newEvaluationParams: evaluationParams = { let newEvaluationParams: evaluationParams = {
samplingInputs: evaluationParams.samplingInputs, samplingInputs: evaluationParams.samplingInputs,
environment: Environment.mergeKeepSecond( environment: Environment.mergeKeepSecond(evaluationParams.environment, newEnvironment),
evaluationParams.environment,
newEnvironment,
),
evaluateNode: evaluationParams.evaluateNode, evaluateNode: evaluationParams.evaluateNode,
} }
evaluationParams.evaluateNode(newEvaluationParams, internals(t)) evaluationParams.evaluateNode(newEvaluationParams, internals(t))
@ -221,10 +217,7 @@ module AST = {
) )
let sampleN = n => let sampleN = n =>
map( map(~renderedDistFn=PointSetDist.sampleNRendered(n), ~symbolicDistFn=SymbolicDist.T.sampleN(n))
~renderedDistFn=PointSetDist.sampleNRendered(n),
~symbolicDistFn=SymbolicDist.T.sampleN(n),
)
let getCombinationSamples = (n, algebraicOp, t1: node, t2: node) => let getCombinationSamples = (n, algebraicOp, t1: node, t2: node) =>
switch (sampleN(n, t1), sampleN(n, t2)) { switch (sampleN(n, t1), sampleN(n, t2)) {
@ -254,19 +247,11 @@ module AST = {
let pointSetDist = let pointSetDist =
samples samples
|> E.O.fmap(r => |> E.O.fmap(r =>
SampleSet.toPointSetDist( SampleSet.toPointSetDist(~samplingInputs=evaluationParams.samplingInputs, ~samples=r, ())
~samplingInputs=evaluationParams.samplingInputs,
~samples=r,
(),
)
) )
|> E.O.bind(_, r => r.pointSetDist) |> E.O.bind(_, r => r.pointSetDist)
|> E.O.toResult("No response") |> E.O.toResult("No response")
pointSetDist |> E.R.fmap(r => #Normalize(#RenderedDist(r))) pointSetDist |> E.R.fmap(r => #Normalize(#RenderedDist(r)))
}) })
// todo: This bottom part should probably be somewhere else.
// todo: REFACTOR: I'm not sure about the SampleSet line.
}
} }
} }

View File

@ -1,5 +1,5 @@
type node = ASTTypes.AST.node type node = ASTTypes.node
let getFloat = ASTTypes.AST.Node.getFloat let getFloat = ASTTypes.Node.getFloat
type samplingDist = [ type samplingDist = [
| #SymbolicDist(SymbolicDistTypes.symbolicDist) | #SymbolicDist(SymbolicDistTypes.symbolicDist)
@ -61,7 +61,7 @@ module TypedValue = {
|> E.A.fmap(((name, t)) => fromNode(t) |> E.R.fmap(r => (name, r))) |> E.A.fmap(((name, t)) => fromNode(t) |> E.R.fmap(r => (name, r)))
|> E.A.R.firstErrorOrOpen |> E.A.R.firstErrorOrOpen
|> E.R.fmap(r => #Hash(r)) |> E.R.fmap(r => #Hash(r))
| e => Error("Wrong type: " ++ ASTTypes.AST.Node.toString(e)) | e => Error("Wrong type: " ++ ASTTypes.Node.toString(e))
} }
// todo: Arrays and hashes // todo: Arrays and hashes
@ -73,12 +73,12 @@ module TypedValue = {
| _ => Error("Type Error: Expected float.") | _ => Error("Type Error: Expected float.")
} }
| (#SamplingDistribution, _) => | (#SamplingDistribution, _) =>
ASTTypes.AST.SamplingDistribution.renderIfIsNotSamplingDistribution( ASTTypes.SamplingDistribution.renderIfIsNotSamplingDistribution(
evaluationParams, evaluationParams,
node, node,
) |> E.R.bind(_, fromNode) ) |> E.R.bind(_, fromNode)
| (#RenderedDistribution, _) => | (#RenderedDistribution, _) =>
ASTTypes.AST.Node.render(evaluationParams, node) |> E.R.bind(_, fromNode) ASTTypes.Node.render(evaluationParams, node) |> E.R.bind(_, fromNode)
| (#Array(_type), #Array(b)) => | (#Array(_type), #Array(b)) =>
b b
|> E.A.fmap(fromNodeWithTypeCoercion(evaluationParams, _type)) |> E.A.fmap(fromNodeWithTypeCoercion(evaluationParams, _type))
@ -172,7 +172,7 @@ module Function = {
|> E.A.R.firstErrorOrOpen |> E.A.R.firstErrorOrOpen
let inputsToTypedValues = ( let inputsToTypedValues = (
evaluationParams: ASTTypes.AST.evaluationParams, evaluationParams: ASTTypes.evaluationParams,
inputNodes: inputNodes, inputNodes: inputNodes,
t: t, t: t,
) => ) =>
@ -181,7 +181,7 @@ module Function = {
) )
let run = ( let run = (
evaluationParams: ASTTypes.AST.evaluationParams, evaluationParams: ASTTypes.evaluationParams,
inputNodes: inputNodes, inputNodes: inputNodes,
t: t, t: t,
) => ) =>

View File

@ -122,7 +122,7 @@ module MathAdtToDistDst = {
| _ => Error("Lognormal distribution needs either mean and stdev or mu and sigma") | _ => Error("Lognormal distribution needs either mean and stdev or mu and sigma")
} }
| _ => | _ =>
parseArgs() |> E.R.fmap((args: array<ASTTypes.AST.node>) => parseArgs() |> E.R.fmap((args: array<ASTTypes.node>) =>
#FunctionCall("lognormal", args) #FunctionCall("lognormal", args)
) )
} }
@ -130,8 +130,8 @@ module MathAdtToDistDst = {
// Error("Dotwise exponentiation needs two operands") // Error("Dotwise exponentiation needs two operands")
let operationParser = ( let operationParser = (
name: string, name: string,
args: result<array<ASTTypes.AST.node>, string>, args: result<array<ASTTypes.node>, string>,
): result<ASTTypes.AST.node, string> => { ): result<ASTTypes.node, string> => {
let toOkAlgebraic = r => Ok(#AlgebraicCombination(r)) let toOkAlgebraic = r => Ok(#AlgebraicCombination(r))
let toOkPointwise = r => Ok(#PointwiseCombination(r)) let toOkPointwise = r => Ok(#PointwiseCombination(r))
let toOkTruncate = r => Ok(#Truncate(r)) let toOkTruncate = r => Ok(#Truncate(r))
@ -170,12 +170,12 @@ module MathAdtToDistDst = {
let functionParser = ( let functionParser = (
nodeParser: MathJsonToMathJsAdt.arg => Belt.Result.t< nodeParser: MathJsonToMathJsAdt.arg => Belt.Result.t<
ASTTypes.AST.node, ASTTypes.node,
string, string,
>, >,
name: string, name: string,
args: array<MathJsonToMathJsAdt.arg>, args: array<MathJsonToMathJsAdt.arg>,
): result<ASTTypes.AST.node, string> => { ): result<ASTTypes.node, string> => {
let parseArray = ags => ags |> E.A.fmap(nodeParser) |> E.A.R.firstErrorOrOpen let parseArray = ags => ags |> E.A.fmap(nodeParser) |> E.A.R.firstErrorOrOpen
let parseArgs = () => parseArray(args) let parseArgs = () => parseArray(args)
switch name { switch name {
@ -212,27 +212,27 @@ module MathAdtToDistDst = {
| (Some(Error(r)), _) => Error(r) | (Some(Error(r)), _) => Error(r)
| (_, Error(r)) => Error(r) | (_, Error(r)) => Error(r)
| (None, Ok(dists)) => | (None, Ok(dists)) =>
let hash: ASTTypes.AST.node = #FunctionCall( let hash: ASTTypes.node = #FunctionCall(
"multimodal", "multimodal",
[#Hash([("dists", #Array(dists)), ("weights", #Array([]))])], [#Hash([("dists", #Array(dists)), ("weights", #Array([]))])],
) )
Ok(hash) Ok(hash)
| (Some(Ok(weights)), Ok(dists)) => | (Some(Ok(weights)), Ok(dists)) =>
let hash: ASTTypes.AST.node = #FunctionCall( let hash: ASTTypes.node = #FunctionCall(
"multimodal", "multimodal",
[#Hash([("dists", #Array(dists)), ("weights", #Array(weights))])], [#Hash([("dists", #Array(dists)), ("weights", #Array(weights))])],
) )
Ok(hash) Ok(hash)
} }
| name => | name =>
parseArgs() |> E.R.fmap((args: array<ASTTypes.AST.node>) => parseArgs() |> E.R.fmap((args: array<ASTTypes.node>) =>
#FunctionCall(name, args) #FunctionCall(name, args)
) )
} }
} }
let rec nodeParser: MathJsonToMathJsAdt.arg => result< let rec nodeParser: MathJsonToMathJsAdt.arg => result<
ASTTypes.AST.node, ASTTypes.node,
string, string,
> = x => > = x =>
switch x { switch x {
@ -246,7 +246,7 @@ module MathAdtToDistDst = {
// let evaluatedExpression = run(expression); // let evaluatedExpression = run(expression);
// `Function(_ => Ok(evaluatedExpression)); // `Function(_ => Ok(evaluatedExpression));
// } // }
let rec topLevel = (r): result<ASTTypes.AST.program, string> => let rec topLevel = (r): result<ASTTypes.program, string> =>
switch r { switch r {
| FunctionAssignment({name, args, expression}) => | FunctionAssignment({name, args, expression}) =>
switch nodeParser(expression) { switch nodeParser(expression) {
@ -267,7 +267,7 @@ module MathAdtToDistDst = {
blocks |> E.A.fmap(b => topLevel(b)) |> E.A.R.firstErrorOrOpen |> E.R.fmap(E.A.concatMany) blocks |> E.A.fmap(b => topLevel(b)) |> E.A.R.firstErrorOrOpen |> E.R.fmap(E.A.concatMany)
} }
let run = (r): result<ASTTypes.AST.program, string> => let run = (r): result<ASTTypes.program, string> =>
r |> MathAdtCleaner.run |> topLevel r |> MathAdtCleaner.run |> topLevel
} }