ExpressionTree -> AST

This commit is contained in:
Ozzie Gooen 2022-02-15 15:58:43 -05:00
parent 98779c127b
commit eb5f5245b6
21 changed files with 87 additions and 87 deletions

View File

@ -383,9 +383,9 @@ let shape: PointSetTypes.xyShape = {xs: [1., 4., 8.], ys: [8., 9., 2.]}
// let numSamples = 10000;
// open Distributions.Shape;
// let normal: SymbolicDistTypes.symbolicDist = `Normal({mean, stdev});
// let normalShape = ExpressionTree.toShape(numSamples, `SymbolicDist(normal));
// let normalShape = AST.toShape(numSamples, `SymbolicDist(normal));
// let lognormal = SymbolicDist.Lognormal.fromMeanAndStdev(mean, stdev);
// let lognormalShape = ExpressionTree.toShape(numSamples, `SymbolicDist(lognormal));
// let lognormalShape = AST.toShape(numSamples, `SymbolicDist(lognormal));
// makeTestCloseEquality(
// "Mean of a normal",

View File

@ -10,7 +10,7 @@ let makeTest = (~only=false, str, item1, item2) =>
expect(item1) |> toEqual(item2)
);
let evalParams: ExpressionTypes.ExpressionTree.evaluationParams = {
let evalParams: ASTTypes.AST.evaluationParams = {
samplingInputs: {
sampleCount: 1000,
outputXYPoints: 10000,
@ -25,7 +25,7 @@ let evalParams: ExpressionTypes.ExpressionTree.evaluationParams = {
("T", `SymbolicDist(`Float(1000000000000.0))),
|]
->Belt.Map.String.fromArray,
evaluateNode: ExpressionTreeEvaluator.toLeaf,
evaluateNode: ASTEvaluator.toLeaf,
};
let shape1: PointSetTypes.xyShape = {xs: [|1., 4., 8.|], ys: [|0.2, 0.4, 0.8|]};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -14,7 +14,7 @@ module Inputs = {
type inputs = {
squiggleString: string,
samplingInputs: SamplingInputs.t,
environment: ExpressionTypes.ExpressionTree.environment,
environment: ASTTypes.AST.environment,
}
let empty: SamplingInputs.t = {
@ -27,7 +27,7 @@ module Inputs = {
let make = (
~samplingInputs=empty,
~squiggleString,
~environment=ExpressionTypes.ExpressionTree.Environment.empty,
~environment=ASTTypes.AST.Environment.empty,
(),
): inputs => {
samplingInputs: samplingInputs,
@ -40,8 +40,8 @@ type \"export" = [
| #DistPlus(DistPlus.t)
| #Float(float)
| #Function(
(array<string>, ExpressionTypes.ExpressionTree.node),
ExpressionTypes.ExpressionTree.environment,
(array<string>, ASTTypes.AST.node),
ASTTypes.AST.environment,
)
]
@ -53,18 +53,18 @@ module Internals = {
): Inputs.inputs => {
samplingInputs: samplingInputs,
squiggleString: squiggleString,
environment: ExpressionTypes.ExpressionTree.Environment.update(environment, str, _ => Some(
environment: ASTTypes.AST.Environment.update(environment, str, _ => Some(
node,
)),
}
type outputs = {
graph: ExpressionTypes.ExpressionTree.node,
graph: ASTTypes.AST.node,
shape: PointSetTypes.shape,
}
let makeOutputs = (graph, shape): outputs => {graph: graph, shape: shape}
let makeInputs = (inputs: Inputs.inputs): ExpressionTypes.ExpressionTree.samplingInputs => {
let makeInputs = (inputs: Inputs.inputs): ASTTypes.AST.samplingInputs => {
sampleCount: inputs.samplingInputs.sampleCount |> E.O.default(10000),
outputXYPoints: inputs.samplingInputs.outputXYPoints |> E.O.default(10000),
kernelWidth: inputs.samplingInputs.kernelWidth,
@ -72,9 +72,9 @@ module Internals = {
}
let runNode = (inputs, node) =>
ExpressionTree.toLeaf(makeInputs(inputs), inputs.environment, node)
AST.toLeaf(makeInputs(inputs), inputs.environment, node)
let runProgram = (inputs: Inputs.inputs, p: ExpressionTypes.Program.program) => {
let runProgram = (inputs: Inputs.inputs, p: ASTTypes.Program.program) => {
let ins = ref(inputs)
p
|> E.A.fmap(x =>
@ -91,14 +91,14 @@ module Internals = {
}
let inputsToLeaf = (inputs: Inputs.inputs) =>
MathJsParser.fromString(inputs.squiggleString) |> E.R.bind(_, g => runProgram(inputs, g))
Parser.fromString(inputs.squiggleString) |> E.R.bind(_, g => runProgram(inputs, g))
let outputToDistPlus = (inputs: Inputs.inputs, shape: PointSetTypes.shape) =>
DistPlus.make(~shape, ~squiggleString=Some(inputs.squiggleString), ())
}
let renderIfNeeded = (inputs: Inputs.inputs, node: ExpressionTypes.ExpressionTree.node): result<
ExpressionTypes.ExpressionTree.node,
let renderIfNeeded = (inputs: Inputs.inputs, node: ASTTypes.AST.node): result<
ASTTypes.AST.node,
string,
> =>
node |> (
@ -121,11 +121,11 @@ let renderIfNeeded = (inputs: Inputs.inputs, node: ExpressionTypes.ExpressionTre
}
)
// TODO: Consider using ExpressionTypes.ExpressionTree.getFloat or similar in this function
// TODO: Consider using ASTTypes.AST.getFloat or similar in this function
let coersionToExportedTypes = (
inputs,
env: ExpressionTypes.ExpressionTree.environment,
node: ExpressionTypes.ExpressionTree.node,
env: ASTTypes.AST.environment,
node: ASTTypes.AST.node,
): result<\"export", string> =>
node
|> renderIfNeeded(inputs)
@ -135,7 +135,7 @@ let coersionToExportedTypes = (
| #SymbolicDist(#Float(x)) => Ok(#Float(x))
| #RenderedDist(n) => Ok(#DistPlus(Internals.outputToDistPlus(inputs, n)))
| #Function(n) => Ok(#Function(n, env))
| n => Error("Didn't output a rendered distribution. Format:" ++ ExpressionTree.toString(n))
| n => Error("Didn't output a rendered distribution. Format:" ++ AST.toString(n))
}
)
@ -160,10 +160,10 @@ let evaluateProgram = (inputs: Inputs.inputs) =>
let evaluateFunction = (
inputs: Inputs.inputs,
fn: (array<string>, ExpressionTypes.ExpressionTree.node),
fn: (array<string>, ASTTypes.AST.node),
fnInputs,
) => {
let output = ExpressionTree.runFunction(
let output = AST.runFunction(
Internals.makeInputs(inputs),
inputs.environment,
fnInputs,

View File

@ -1,14 +1,14 @@
open ExpressionTypes.ExpressionTree
open ASTTypes.AST
let toString = ExpressionTreeBasic.toString
let toString = ASTBasic.toString
let envs = (samplingInputs, environment) => {
samplingInputs: samplingInputs,
environment: environment,
evaluateNode: ExpressionTreeEvaluator.toLeaf,
evaluateNode: ASTEvaluator.toLeaf,
}
let toLeaf = (samplingInputs, environment, node: node) =>
ExpressionTreeEvaluator.toLeaf(envs(samplingInputs, environment), node)
ASTEvaluator.toLeaf(envs(samplingInputs, environment), node)
let toShape = (samplingInputs, environment, node: node) =>
switch toLeaf(samplingInputs, environment, node) {
| Ok(#RenderedDist(shape)) => Ok(shape)

View File

@ -1,4 +1,4 @@
open ExpressionTypes.ExpressionTree
open ASTTypes.AST
// This file exists to manage a dependency cycle. It would be good to refactor later.
let rec toString: node => string = x =>

View File

@ -1,5 +1,5 @@
open ExpressionTypes
open ExpressionTypes.ExpressionTree
open ASTTypes
open ASTTypes.AST
type t = node
type tResult = node => result<node, string>
@ -56,7 +56,7 @@ module AlgebraicCombination = {
let operationToLeaf = (
evaluationParams: evaluationParams,
algebraicOp: ExpressionTypes.algebraicOperation,
algebraicOp: ASTTypes.algebraicOperation,
t1: t,
t2: t,
): result<node, string> =>
@ -174,9 +174,9 @@ module FunctionCall = {
)
let _runWithEvaluatedInputs = (
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
evaluationParams: ASTTypes.AST.evaluationParams,
name,
args: array<ExpressionTypes.ExpressionTree.node>,
args: array<ASTTypes.AST.node>,
) =>
_runHardcodedFunction(name, evaluationParams, args) |> E.O.default(
_runLocalFunction(name, evaluationParams, args),
@ -208,7 +208,7 @@ module Render = {
This function is used mainly to turn a parse tree into a single RenderedDist
that can then be displayed to the user. */
let rec toLeaf = (
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
evaluationParams: ASTTypes.AST.evaluationParams,
node: t,
): result<t, string> =>
switch node {
@ -236,7 +236,7 @@ let rec toLeaf = (
|> E.A.R.firstErrorOrOpen
|> E.R.fmap(r => #Hash(r))
| #Symbol(r) =>
ExpressionTypes.ExpressionTree.Environment.get(evaluationParams.environment, r)
ASTTypes.AST.Environment.get(evaluationParams.environment, r)
|> E.O.toResult("Undeclared variable " ++ r)
|> E.R.bind(_, toLeaf(evaluationParams))
| #FunctionCall(name, args) =>

View File

@ -15,7 +15,7 @@ type distToFloatOperation = [
| #Sample
]
module ExpressionTree = {
module AST = {
type rec hash = array<(string, node)>
and node = [
| #SymbolicDist(SymbolicDistTypes.symbolicDist)
@ -160,15 +160,15 @@ module ExpressionTree = {
}
type simplificationResult = [
| #Solution(ExpressionTree.node)
| #Solution(AST.node)
| #Error(string)
| #NoSolution
]
module Program = {
type statement = [
| #Assignment(string, ExpressionTree.node)
| #Expression(ExpressionTree.node)
| #Assignment(string, AST.node)
| #Expression(AST.node)
]
type program = array<statement>
}

View File

@ -1,4 +1,4 @@
open ExpressionTypes
open ASTTypes
module Algebraic = {
type t = algebraicOperation
@ -103,5 +103,5 @@ module T = {
| #Truncate(lc, rc, t) => truncateToString(lc, rc, nodeToString(t))
| #Render(t) => nodeToString(t)
| _ => ""
} // SymbolicDist and RenderedDist are handled in ExpressionTree.toString.
} // SymbolicDist and RenderedDist are handled in AST.toString.
}

View File

@ -1,4 +1,4 @@
open ExpressionTypes.ExpressionTree
open ASTTypes.AST
module Function = {
type t = (array<string>, node)
@ -10,7 +10,7 @@ module Function = {
let argumentNames = ((a, _): t) => a
let internals = ((_, b): t) => b
let run = (
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
evaluationParams: ASTTypes.AST.evaluationParams,
args: array<node>,
t: t,
) =>
@ -19,10 +19,10 @@ module Function = {
Belt.Array.zip(
argumentNames(t),
args,
) |> ExpressionTypes.ExpressionTree.Environment.fromArray
let newEvaluationParams: ExpressionTypes.ExpressionTree.evaluationParams = {
) |> ASTTypes.AST.Environment.fromArray
let newEvaluationParams: ASTTypes.AST.evaluationParams = {
samplingInputs: evaluationParams.samplingInputs,
environment: ExpressionTypes.ExpressionTree.Environment.mergeKeepSecond(
environment: ASTTypes.AST.Environment.mergeKeepSecond(
evaluationParams.environment,
newEnvironment,
),

View File

@ -84,7 +84,7 @@ let makeDist = (name, fn) =>
)
let floatFromDist = (
distToFloatOp: ExpressionTypes.distToFloatOperation,
distToFloatOp: ASTTypes.distToFloatOperation,
t: TypeSystem.samplingDist,
): result<node, string> =>
switch t {
@ -111,7 +111,7 @@ let verticalScaling = (scaleOp, rs, scaleBy) => {
}
module Multimodal = {
let getByNameResult = ExpressionTypes.ExpressionTree.Hash.getByNameResult
let getByNameResult = ASTTypes.AST.Hash.getByNameResult
let _paramsToDistsAndWeights = (r: array<typedValue>) =>
switch r {

View File

@ -1,5 +1,5 @@
type node = ExpressionTypes.ExpressionTree.node
let getFloat = ExpressionTypes.ExpressionTree.getFloat
type node = ASTTypes.AST.node
let getFloat = ASTTypes.AST.getFloat
type samplingDist = [
| #SymbolicDist(SymbolicDistTypes.symbolicDist)
@ -61,7 +61,7 @@ module TypedValue = {
|> E.A.fmap(((name, t)) => fromNode(t) |> E.R.fmap(r => (name, r)))
|> E.A.R.firstErrorOrOpen
|> E.R.fmap(r => #Hash(r))
| e => Error("Wrong type: " ++ ExpressionTreeBasic.toString(e))
| e => Error("Wrong type: " ++ ASTBasic.toString(e))
}
// todo: Arrays and hashes
@ -78,7 +78,7 @@ module TypedValue = {
node,
) |> E.R.bind(_, fromNode)
| (#RenderedDistribution, _) =>
ExpressionTypes.ExpressionTree.Render.render(evaluationParams, node) |> E.R.bind(_, fromNode)
ASTTypes.AST.Render.render(evaluationParams, node) |> E.R.bind(_, fromNode)
| (#Array(_type), #Array(b)) =>
b
|> E.A.fmap(fromNodeWithTypeCoercion(evaluationParams, _type))
@ -89,7 +89,7 @@ module TypedValue = {
named |> E.A.fmap(((name, intendedType)) => (
name,
intendedType,
ExpressionTypes.ExpressionTree.Hash.getByName(r, name),
ASTTypes.AST.Hash.getByName(r, name),
))
let typedHash =
keyValues
@ -172,7 +172,7 @@ module Function = {
|> E.A.R.firstErrorOrOpen
let inputsToTypedValues = (
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
evaluationParams: ASTTypes.AST.evaluationParams,
inputNodes: inputNodes,
t: t,
) =>
@ -181,7 +181,7 @@ module Function = {
)
let run = (
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
evaluationParams: ASTTypes.AST.evaluationParams,
inputNodes: inputNodes,
t: t,
) =>

View File

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

View File

@ -96,7 +96,7 @@ let toDiscretePointMassesFromTriangulars = (
}
let combineShapesContinuousContinuous = (
op: ExpressionTypes.algebraicOperation,
op: ASTTypes.algebraicOperation,
s1: PointSetTypes.xyShape,
s2: PointSetTypes.xyShape,
): PointSetTypes.xyShape => {
@ -200,7 +200,7 @@ let toDiscretePointMassesFromDiscrete = (s: PointSetTypes.xyShape): pointMassesW
}
let combineShapesContinuousDiscrete = (
op: ExpressionTypes.algebraicOperation,
op: ASTTypes.algebraicOperation,
continuousShape: PointSetTypes.xyShape,
discreteShape: PointSetTypes.xyShape,
): PointSetTypes.xyShape => {

View File

@ -211,7 +211,7 @@ module T = Dist({
/* This simply creates multiple copies of the continuous distribution, scaled and shifted according to
each discrete data point, and then adds them all together. */
let combineAlgebraicallyWithDiscrete = (
op: ExpressionTypes.algebraicOperation,
op: ASTTypes.algebraicOperation,
t1: t,
t2: PointSetTypes.discreteShape,
) => {
@ -244,7 +244,7 @@ let combineAlgebraicallyWithDiscrete = (
}
}
let combineAlgebraically = (op: ExpressionTypes.algebraicOperation, t1: t, t2: t) => {
let combineAlgebraically = (op: ASTTypes.algebraicOperation, t1: t, t2: t) => {
let s1 = t1 |> getShape
let s2 = t2 |> getShape
let t1n = s1 |> XYShape.T.length

View File

@ -85,7 +85,7 @@ let updateIntegralCache = (integralCache, t: t): t => {
/* This multiples all of the data points together and creates a new discrete distribution from the results.
Data points at the same xs get added together. It may be a good idea to downsample t1 and t2 before and/or the result after. */
let combineAlgebraically = (op: ExpressionTypes.algebraicOperation, t1: t, t2: t): t => {
let combineAlgebraically = (op: ASTTypes.algebraicOperation, t1: t, t2: t): t => {
let t1s = t1 |> getShape
let t2s = t2 |> getShape
let t1n = t1s |> XYShape.T.length

View File

@ -227,7 +227,7 @@ module T = Dist({
}
})
let combineAlgebraically = (op: ExpressionTypes.algebraicOperation, t1: t, t2: t): t => {
let combineAlgebraically = (op: ASTTypes.algebraicOperation, t1: t, t2: t): t => {
// Discrete convolution can cause a huge increase in the number of samples,
// so we'll first downsample.

View File

@ -33,7 +33,7 @@ let toMixed = mapToAll((
),
))
let combineAlgebraically = (op: ExpressionTypes.algebraicOperation, t1: t, t2: t): t =>
let combineAlgebraically = (op: ASTTypes.algebraicOperation, t1: t, t2: t): t =>
switch (t1, t2) {
| (Continuous(m1), Continuous(m2)) =>
Continuous.combineAlgebraically(op, m1, m2) |> Continuous.T.toShape
@ -197,7 +197,7 @@ let sampleNRendered = (n, dist) => {
doN(n, () => sample(distWithUpdatedIntegralCache))
}
let operate = (distToFloatOp: ExpressionTypes.distToFloatOperation, s): float =>
let operate = (distToFloatOp: ASTTypes.distToFloatOperation, s): float =>
switch distToFloatOp {
| #Pdf(f) => pdf(f, s)
| #Cdf(f) => pdf(f, s)

View File

@ -80,7 +80,7 @@ module Internals = {
let toShape = (
~samples: Internals.T.t,
~samplingInputs: ExpressionTypes.ExpressionTree.samplingInputs,
~samplingInputs: ASTTypes.AST.samplingInputs,
(),
) => {
Array.fast_sort(compare, samples)

View File

@ -272,7 +272,7 @@ module T = {
| #Float(n) => Float.mean(n)
}
let operate = (distToFloatOp: ExpressionTypes.distToFloatOperation, s) =>
let operate = (distToFloatOp: ASTTypes.distToFloatOperation, s) =>
switch distToFloatOp {
| #Cdf(f) => Ok(cdf(f, s))
| #Pdf(f) => Ok(pdf(f, s))
@ -302,7 +302,7 @@ module T = {
let tryAnalyticalSimplification = (
d1: symbolicDist,
d2: symbolicDist,
op: ExpressionTypes.algebraicOperation,
op: ASTTypes.algebraicOperation,
): analyticalSimplificationResult =>
switch (d1, d2) {
| (#Float(v1), #Float(v2)) =>