ExpressionTree -> AST
This commit is contained in:
parent
98779c127b
commit
eb5f5245b6
|
@ -383,9 +383,9 @@ let shape: PointSetTypes.xyShape = {xs: [1., 4., 8.], ys: [8., 9., 2.]}
|
||||||
// let numSamples = 10000;
|
// let numSamples = 10000;
|
||||||
// open Distributions.Shape;
|
// open Distributions.Shape;
|
||||||
// let normal: SymbolicDistTypes.symbolicDist = `Normal({mean, stdev});
|
// 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 lognormal = SymbolicDist.Lognormal.fromMeanAndStdev(mean, stdev);
|
||||||
// let lognormalShape = ExpressionTree.toShape(numSamples, `SymbolicDist(lognormal));
|
// let lognormalShape = AST.toShape(numSamples, `SymbolicDist(lognormal));
|
||||||
|
|
||||||
// makeTestCloseEquality(
|
// makeTestCloseEquality(
|
||||||
// "Mean of a normal",
|
// "Mean of a normal",
|
||||||
|
|
|
@ -10,7 +10,7 @@ let makeTest = (~only=false, str, item1, item2) =>
|
||||||
expect(item1) |> toEqual(item2)
|
expect(item1) |> toEqual(item2)
|
||||||
);
|
);
|
||||||
|
|
||||||
let evalParams: ExpressionTypes.ExpressionTree.evaluationParams = {
|
let evalParams: ASTTypes.AST.evaluationParams = {
|
||||||
samplingInputs: {
|
samplingInputs: {
|
||||||
sampleCount: 1000,
|
sampleCount: 1000,
|
||||||
outputXYPoints: 10000,
|
outputXYPoints: 10000,
|
||||||
|
@ -25,7 +25,7 @@ let evalParams: ExpressionTypes.ExpressionTree.evaluationParams = {
|
||||||
("T", `SymbolicDist(`Float(1000000000000.0))),
|
("T", `SymbolicDist(`Float(1000000000000.0))),
|
||||||
|]
|
|]
|
||||||
->Belt.Map.String.fromArray,
|
->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|]};
|
let shape1: PointSetTypes.xyShape = {xs: [|1., 4., 8.|], ys: [|0.2, 0.4, 0.8|]};
|
||||||
|
|
20
packages/squiggle-lang/dist/index.js
vendored
20
packages/squiggle-lang/dist/index.js
vendored
File diff suppressed because one or more lines are too long
2
packages/squiggle-lang/dist/report.html
vendored
2
packages/squiggle-lang/dist/report.html
vendored
File diff suppressed because one or more lines are too long
|
@ -14,7 +14,7 @@ module Inputs = {
|
||||||
type inputs = {
|
type inputs = {
|
||||||
squiggleString: string,
|
squiggleString: string,
|
||||||
samplingInputs: SamplingInputs.t,
|
samplingInputs: SamplingInputs.t,
|
||||||
environment: ExpressionTypes.ExpressionTree.environment,
|
environment: ASTTypes.AST.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=ExpressionTypes.ExpressionTree.Environment.empty,
|
~environment=ASTTypes.AST.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>, ExpressionTypes.ExpressionTree.node),
|
(array<string>, ASTTypes.AST.node),
|
||||||
ExpressionTypes.ExpressionTree.environment,
|
ASTTypes.AST.environment,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -53,18 +53,18 @@ module Internals = {
|
||||||
): Inputs.inputs => {
|
): Inputs.inputs => {
|
||||||
samplingInputs: samplingInputs,
|
samplingInputs: samplingInputs,
|
||||||
squiggleString: squiggleString,
|
squiggleString: squiggleString,
|
||||||
environment: ExpressionTypes.ExpressionTree.Environment.update(environment, str, _ => Some(
|
environment: ASTTypes.AST.Environment.update(environment, str, _ => Some(
|
||||||
node,
|
node,
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
|
|
||||||
type outputs = {
|
type outputs = {
|
||||||
graph: ExpressionTypes.ExpressionTree.node,
|
graph: ASTTypes.AST.node,
|
||||||
shape: PointSetTypes.shape,
|
shape: PointSetTypes.shape,
|
||||||
}
|
}
|
||||||
let makeOutputs = (graph, shape): outputs => {graph: graph, shape: 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),
|
sampleCount: inputs.samplingInputs.sampleCount |> E.O.default(10000),
|
||||||
outputXYPoints: inputs.samplingInputs.outputXYPoints |> E.O.default(10000),
|
outputXYPoints: inputs.samplingInputs.outputXYPoints |> E.O.default(10000),
|
||||||
kernelWidth: inputs.samplingInputs.kernelWidth,
|
kernelWidth: inputs.samplingInputs.kernelWidth,
|
||||||
|
@ -72,9 +72,9 @@ module Internals = {
|
||||||
}
|
}
|
||||||
|
|
||||||
let runNode = (inputs, node) =>
|
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)
|
let ins = ref(inputs)
|
||||||
p
|
p
|
||||||
|> E.A.fmap(x =>
|
|> E.A.fmap(x =>
|
||||||
|
@ -91,14 +91,14 @@ module Internals = {
|
||||||
}
|
}
|
||||||
|
|
||||||
let inputsToLeaf = (inputs: Inputs.inputs) =>
|
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) =>
|
let outputToDistPlus = (inputs: Inputs.inputs, shape: PointSetTypes.shape) =>
|
||||||
DistPlus.make(~shape, ~squiggleString=Some(inputs.squiggleString), ())
|
DistPlus.make(~shape, ~squiggleString=Some(inputs.squiggleString), ())
|
||||||
}
|
}
|
||||||
|
|
||||||
let renderIfNeeded = (inputs: Inputs.inputs, node: ExpressionTypes.ExpressionTree.node): result<
|
let renderIfNeeded = (inputs: Inputs.inputs, node: ASTTypes.AST.node): result<
|
||||||
ExpressionTypes.ExpressionTree.node,
|
ASTTypes.AST.node,
|
||||||
string,
|
string,
|
||||||
> =>
|
> =>
|
||||||
node |> (
|
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 = (
|
let coersionToExportedTypes = (
|
||||||
inputs,
|
inputs,
|
||||||
env: ExpressionTypes.ExpressionTree.environment,
|
env: ASTTypes.AST.environment,
|
||||||
node: ExpressionTypes.ExpressionTree.node,
|
node: ASTTypes.AST.node,
|
||||||
): result<\"export", string> =>
|
): result<\"export", string> =>
|
||||||
node
|
node
|
||||||
|> renderIfNeeded(inputs)
|
|> renderIfNeeded(inputs)
|
||||||
|
@ -135,7 +135,7 @@ let coersionToExportedTypes = (
|
||||||
| #SymbolicDist(#Float(x)) => Ok(#Float(x))
|
| #SymbolicDist(#Float(x)) => Ok(#Float(x))
|
||||||
| #RenderedDist(n) => Ok(#DistPlus(Internals.outputToDistPlus(inputs, n)))
|
| #RenderedDist(n) => Ok(#DistPlus(Internals.outputToDistPlus(inputs, n)))
|
||||||
| #Function(n) => Ok(#Function(n, env))
|
| #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 = (
|
let evaluateFunction = (
|
||||||
inputs: Inputs.inputs,
|
inputs: Inputs.inputs,
|
||||||
fn: (array<string>, ExpressionTypes.ExpressionTree.node),
|
fn: (array<string>, ASTTypes.AST.node),
|
||||||
fnInputs,
|
fnInputs,
|
||||||
) => {
|
) => {
|
||||||
let output = ExpressionTree.runFunction(
|
let output = AST.runFunction(
|
||||||
Internals.makeInputs(inputs),
|
Internals.makeInputs(inputs),
|
||||||
inputs.environment,
|
inputs.environment,
|
||||||
fnInputs,
|
fnInputs,
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
open ExpressionTypes.ExpressionTree
|
open ASTTypes.AST
|
||||||
|
|
||||||
let toString = ExpressionTreeBasic.toString
|
let toString = ASTBasic.toString
|
||||||
let envs = (samplingInputs, environment) => {
|
let envs = (samplingInputs, environment) => {
|
||||||
samplingInputs: samplingInputs,
|
samplingInputs: samplingInputs,
|
||||||
environment: environment,
|
environment: environment,
|
||||||
evaluateNode: ExpressionTreeEvaluator.toLeaf,
|
evaluateNode: ASTEvaluator.toLeaf,
|
||||||
}
|
}
|
||||||
|
|
||||||
let toLeaf = (samplingInputs, environment, node: node) =>
|
let toLeaf = (samplingInputs, environment, node: node) =>
|
||||||
ExpressionTreeEvaluator.toLeaf(envs(samplingInputs, environment), node)
|
ASTEvaluator.toLeaf(envs(samplingInputs, environment), node)
|
||||||
let toShape = (samplingInputs, environment, node: node) =>
|
let toShape = (samplingInputs, environment, node: node) =>
|
||||||
switch toLeaf(samplingInputs, environment, node) {
|
switch toLeaf(samplingInputs, environment, node) {
|
||||||
| Ok(#RenderedDist(shape)) => Ok(shape)
|
| Ok(#RenderedDist(shape)) => Ok(shape)
|
|
@ -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.
|
// This file exists to manage a dependency cycle. It would be good to refactor later.
|
||||||
|
|
||||||
let rec toString: node => string = x =>
|
let rec toString: node => string = x =>
|
|
@ -1,5 +1,5 @@
|
||||||
open ExpressionTypes
|
open ASTTypes
|
||||||
open ExpressionTypes.ExpressionTree
|
open ASTTypes.AST
|
||||||
|
|
||||||
type t = node
|
type t = node
|
||||||
type tResult = node => result<node, string>
|
type tResult = node => result<node, string>
|
||||||
|
@ -56,7 +56,7 @@ module AlgebraicCombination = {
|
||||||
|
|
||||||
let operationToLeaf = (
|
let operationToLeaf = (
|
||||||
evaluationParams: evaluationParams,
|
evaluationParams: evaluationParams,
|
||||||
algebraicOp: ExpressionTypes.algebraicOperation,
|
algebraicOp: ASTTypes.algebraicOperation,
|
||||||
t1: t,
|
t1: t,
|
||||||
t2: t,
|
t2: t,
|
||||||
): result<node, string> =>
|
): result<node, string> =>
|
||||||
|
@ -174,9 +174,9 @@ module FunctionCall = {
|
||||||
)
|
)
|
||||||
|
|
||||||
let _runWithEvaluatedInputs = (
|
let _runWithEvaluatedInputs = (
|
||||||
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
|
evaluationParams: ASTTypes.AST.evaluationParams,
|
||||||
name,
|
name,
|
||||||
args: array<ExpressionTypes.ExpressionTree.node>,
|
args: array<ASTTypes.AST.node>,
|
||||||
) =>
|
) =>
|
||||||
_runHardcodedFunction(name, evaluationParams, args) |> E.O.default(
|
_runHardcodedFunction(name, evaluationParams, args) |> E.O.default(
|
||||||
_runLocalFunction(name, evaluationParams, args),
|
_runLocalFunction(name, evaluationParams, args),
|
||||||
|
@ -208,7 +208,7 @@ module Render = {
|
||||||
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 = (
|
let rec toLeaf = (
|
||||||
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
|
evaluationParams: ASTTypes.AST.evaluationParams,
|
||||||
node: t,
|
node: t,
|
||||||
): result<t, string> =>
|
): result<t, string> =>
|
||||||
switch node {
|
switch node {
|
||||||
|
@ -236,7 +236,7 @@ let rec toLeaf = (
|
||||||
|> E.A.R.firstErrorOrOpen
|
|> E.A.R.firstErrorOrOpen
|
||||||
|> E.R.fmap(r => #Hash(r))
|
|> E.R.fmap(r => #Hash(r))
|
||||||
| #Symbol(r) =>
|
| #Symbol(r) =>
|
||||||
ExpressionTypes.ExpressionTree.Environment.get(evaluationParams.environment, r)
|
ASTTypes.AST.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) =>
|
|
@ -15,7 +15,7 @@ type distToFloatOperation = [
|
||||||
| #Sample
|
| #Sample
|
||||||
]
|
]
|
||||||
|
|
||||||
module ExpressionTree = {
|
module AST = {
|
||||||
type rec hash = array<(string, node)>
|
type rec hash = array<(string, node)>
|
||||||
and node = [
|
and node = [
|
||||||
| #SymbolicDist(SymbolicDistTypes.symbolicDist)
|
| #SymbolicDist(SymbolicDistTypes.symbolicDist)
|
||||||
|
@ -160,15 +160,15 @@ module ExpressionTree = {
|
||||||
}
|
}
|
||||||
|
|
||||||
type simplificationResult = [
|
type simplificationResult = [
|
||||||
| #Solution(ExpressionTree.node)
|
| #Solution(AST.node)
|
||||||
| #Error(string)
|
| #Error(string)
|
||||||
| #NoSolution
|
| #NoSolution
|
||||||
]
|
]
|
||||||
|
|
||||||
module Program = {
|
module Program = {
|
||||||
type statement = [
|
type statement = [
|
||||||
| #Assignment(string, ExpressionTree.node)
|
| #Assignment(string, AST.node)
|
||||||
| #Expression(ExpressionTree.node)
|
| #Expression(AST.node)
|
||||||
]
|
]
|
||||||
type program = array<statement>
|
type program = array<statement>
|
||||||
}
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
open ExpressionTypes
|
open ASTTypes
|
||||||
|
|
||||||
module Algebraic = {
|
module Algebraic = {
|
||||||
type t = algebraicOperation
|
type t = algebraicOperation
|
||||||
|
@ -103,5 +103,5 @@ module T = {
|
||||||
| #Truncate(lc, rc, t) => truncateToString(lc, rc, nodeToString(t))
|
| #Truncate(lc, rc, t) => truncateToString(lc, rc, nodeToString(t))
|
||||||
| #Render(t) => nodeToString(t)
|
| #Render(t) => nodeToString(t)
|
||||||
| _ => ""
|
| _ => ""
|
||||||
} // SymbolicDist and RenderedDist are handled in ExpressionTree.toString.
|
} // SymbolicDist and RenderedDist are handled in AST.toString.
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
open ExpressionTypes.ExpressionTree
|
open ASTTypes.AST
|
||||||
|
|
||||||
module Function = {
|
module Function = {
|
||||||
type t = (array<string>, node)
|
type t = (array<string>, node)
|
||||||
|
@ -10,7 +10,7 @@ module Function = {
|
||||||
let argumentNames = ((a, _): t) => a
|
let argumentNames = ((a, _): t) => a
|
||||||
let internals = ((_, b): t) => b
|
let internals = ((_, b): t) => b
|
||||||
let run = (
|
let run = (
|
||||||
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
|
evaluationParams: ASTTypes.AST.evaluationParams,
|
||||||
args: array<node>,
|
args: array<node>,
|
||||||
t: t,
|
t: t,
|
||||||
) =>
|
) =>
|
||||||
|
@ -19,10 +19,10 @@ module Function = {
|
||||||
Belt.Array.zip(
|
Belt.Array.zip(
|
||||||
argumentNames(t),
|
argumentNames(t),
|
||||||
args,
|
args,
|
||||||
) |> ExpressionTypes.ExpressionTree.Environment.fromArray
|
) |> ASTTypes.AST.Environment.fromArray
|
||||||
let newEvaluationParams: ExpressionTypes.ExpressionTree.evaluationParams = {
|
let newEvaluationParams: ASTTypes.AST.evaluationParams = {
|
||||||
samplingInputs: evaluationParams.samplingInputs,
|
samplingInputs: evaluationParams.samplingInputs,
|
||||||
environment: ExpressionTypes.ExpressionTree.Environment.mergeKeepSecond(
|
environment: ASTTypes.AST.Environment.mergeKeepSecond(
|
||||||
evaluationParams.environment,
|
evaluationParams.environment,
|
||||||
newEnvironment,
|
newEnvironment,
|
||||||
),
|
),
|
||||||
|
|
|
@ -84,7 +84,7 @@ let makeDist = (name, fn) =>
|
||||||
)
|
)
|
||||||
|
|
||||||
let floatFromDist = (
|
let floatFromDist = (
|
||||||
distToFloatOp: ExpressionTypes.distToFloatOperation,
|
distToFloatOp: ASTTypes.distToFloatOperation,
|
||||||
t: TypeSystem.samplingDist,
|
t: TypeSystem.samplingDist,
|
||||||
): result<node, string> =>
|
): result<node, string> =>
|
||||||
switch t {
|
switch t {
|
||||||
|
@ -111,7 +111,7 @@ let verticalScaling = (scaleOp, rs, scaleBy) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
module Multimodal = {
|
module Multimodal = {
|
||||||
let getByNameResult = ExpressionTypes.ExpressionTree.Hash.getByNameResult
|
let getByNameResult = ASTTypes.AST.Hash.getByNameResult
|
||||||
|
|
||||||
let _paramsToDistsAndWeights = (r: array<typedValue>) =>
|
let _paramsToDistsAndWeights = (r: array<typedValue>) =>
|
||||||
switch r {
|
switch r {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
type node = ExpressionTypes.ExpressionTree.node
|
type node = ASTTypes.AST.node
|
||||||
let getFloat = ExpressionTypes.ExpressionTree.getFloat
|
let getFloat = ASTTypes.AST.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: " ++ ExpressionTreeBasic.toString(e))
|
| e => Error("Wrong type: " ++ ASTBasic.toString(e))
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: Arrays and hashes
|
// todo: Arrays and hashes
|
||||||
|
@ -78,7 +78,7 @@ module TypedValue = {
|
||||||
node,
|
node,
|
||||||
) |> E.R.bind(_, fromNode)
|
) |> E.R.bind(_, fromNode)
|
||||||
| (#RenderedDistribution, _) =>
|
| (#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)) =>
|
| (#Array(_type), #Array(b)) =>
|
||||||
b
|
b
|
||||||
|> E.A.fmap(fromNodeWithTypeCoercion(evaluationParams, _type))
|
|> E.A.fmap(fromNodeWithTypeCoercion(evaluationParams, _type))
|
||||||
|
@ -89,7 +89,7 @@ module TypedValue = {
|
||||||
named |> E.A.fmap(((name, intendedType)) => (
|
named |> E.A.fmap(((name, intendedType)) => (
|
||||||
name,
|
name,
|
||||||
intendedType,
|
intendedType,
|
||||||
ExpressionTypes.ExpressionTree.Hash.getByName(r, name),
|
ASTTypes.AST.Hash.getByName(r, name),
|
||||||
))
|
))
|
||||||
let typedHash =
|
let typedHash =
|
||||||
keyValues
|
keyValues
|
||||||
|
@ -172,7 +172,7 @@ module Function = {
|
||||||
|> E.A.R.firstErrorOrOpen
|
|> E.A.R.firstErrorOrOpen
|
||||||
|
|
||||||
let inputsToTypedValues = (
|
let inputsToTypedValues = (
|
||||||
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
|
evaluationParams: ASTTypes.AST.evaluationParams,
|
||||||
inputNodes: inputNodes,
|
inputNodes: inputNodes,
|
||||||
t: t,
|
t: t,
|
||||||
) =>
|
) =>
|
||||||
|
@ -181,7 +181,7 @@ module Function = {
|
||||||
)
|
)
|
||||||
|
|
||||||
let run = (
|
let run = (
|
||||||
evaluationParams: ExpressionTypes.ExpressionTree.evaluationParams,
|
evaluationParams: ASTTypes.AST.evaluationParams,
|
||||||
inputNodes: inputNodes,
|
inputNodes: inputNodes,
|
||||||
t: t,
|
t: t,
|
||||||
) =>
|
) =>
|
||||||
|
|
|
@ -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<ExpressionTypes.ExpressionTree.node>) =>
|
parseArgs() |> E.R.fmap((args: array<ASTTypes.AST.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<ExpressionTypes.ExpressionTree.node>, string>,
|
args: result<array<ASTTypes.AST.node>, string>,
|
||||||
): result<ExpressionTypes.ExpressionTree.node, string> => {
|
): result<ASTTypes.AST.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<
|
||||||
ExpressionTypes.ExpressionTree.node,
|
ASTTypes.AST.node,
|
||||||
string,
|
string,
|
||||||
>,
|
>,
|
||||||
name: string,
|
name: string,
|
||||||
args: array<MathJsonToMathJsAdt.arg>,
|
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 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: ExpressionTypes.ExpressionTree.node = #FunctionCall(
|
let hash: ASTTypes.AST.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: ExpressionTypes.ExpressionTree.node = #FunctionCall(
|
let hash: ASTTypes.AST.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<ExpressionTypes.ExpressionTree.node>) =>
|
parseArgs() |> E.R.fmap((args: array<ASTTypes.AST.node>) =>
|
||||||
#FunctionCall(name, args)
|
#FunctionCall(name, args)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let rec nodeParser: MathJsonToMathJsAdt.arg => result<
|
let rec nodeParser: MathJsonToMathJsAdt.arg => result<
|
||||||
ExpressionTypes.ExpressionTree.node,
|
ASTTypes.AST.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<ExpressionTypes.Program.program, string> =>
|
let rec topLevel = (r): result<ASTTypes.Program.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<ExpressionTypes.Program.program, string> =>
|
let run = (r): result<ASTTypes.Program.program, string> =>
|
||||||
r |> MathAdtCleaner.run |> topLevel
|
r |> MathAdtCleaner.run |> topLevel
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,7 @@ let toDiscretePointMassesFromTriangulars = (
|
||||||
}
|
}
|
||||||
|
|
||||||
let combineShapesContinuousContinuous = (
|
let combineShapesContinuousContinuous = (
|
||||||
op: ExpressionTypes.algebraicOperation,
|
op: ASTTypes.algebraicOperation,
|
||||||
s1: PointSetTypes.xyShape,
|
s1: PointSetTypes.xyShape,
|
||||||
s2: PointSetTypes.xyShape,
|
s2: PointSetTypes.xyShape,
|
||||||
): PointSetTypes.xyShape => {
|
): PointSetTypes.xyShape => {
|
||||||
|
@ -200,7 +200,7 @@ let toDiscretePointMassesFromDiscrete = (s: PointSetTypes.xyShape): pointMassesW
|
||||||
}
|
}
|
||||||
|
|
||||||
let combineShapesContinuousDiscrete = (
|
let combineShapesContinuousDiscrete = (
|
||||||
op: ExpressionTypes.algebraicOperation,
|
op: ASTTypes.algebraicOperation,
|
||||||
continuousShape: PointSetTypes.xyShape,
|
continuousShape: PointSetTypes.xyShape,
|
||||||
discreteShape: PointSetTypes.xyShape,
|
discreteShape: PointSetTypes.xyShape,
|
||||||
): PointSetTypes.xyShape => {
|
): PointSetTypes.xyShape => {
|
||||||
|
|
|
@ -211,7 +211,7 @@ module T = Dist({
|
||||||
/* This simply creates multiple copies of the continuous distribution, scaled and shifted according to
|
/* This simply creates multiple copies of the continuous distribution, scaled and shifted according to
|
||||||
each discrete data point, and then adds them all together. */
|
each discrete data point, and then adds them all together. */
|
||||||
let combineAlgebraicallyWithDiscrete = (
|
let combineAlgebraicallyWithDiscrete = (
|
||||||
op: ExpressionTypes.algebraicOperation,
|
op: ASTTypes.algebraicOperation,
|
||||||
t1: t,
|
t1: t,
|
||||||
t2: PointSetTypes.discreteShape,
|
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 s1 = t1 |> getShape
|
||||||
let s2 = t2 |> getShape
|
let s2 = t2 |> getShape
|
||||||
let t1n = s1 |> XYShape.T.length
|
let t1n = s1 |> XYShape.T.length
|
||||||
|
|
|
@ -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.
|
/* 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. */
|
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 t1s = t1 |> getShape
|
||||||
let t2s = t2 |> getShape
|
let t2s = t2 |> getShape
|
||||||
let t1n = t1s |> XYShape.T.length
|
let t1n = t1s |> XYShape.T.length
|
||||||
|
|
|
@ -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,
|
// Discrete convolution can cause a huge increase in the number of samples,
|
||||||
// so we'll first downsample.
|
// so we'll first downsample.
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
switch (t1, t2) {
|
||||||
| (Continuous(m1), Continuous(m2)) =>
|
| (Continuous(m1), Continuous(m2)) =>
|
||||||
Continuous.combineAlgebraically(op, m1, m2) |> Continuous.T.toShape
|
Continuous.combineAlgebraically(op, m1, m2) |> Continuous.T.toShape
|
||||||
|
@ -197,7 +197,7 @@ let sampleNRendered = (n, dist) => {
|
||||||
doN(n, () => sample(distWithUpdatedIntegralCache))
|
doN(n, () => sample(distWithUpdatedIntegralCache))
|
||||||
}
|
}
|
||||||
|
|
||||||
let operate = (distToFloatOp: ExpressionTypes.distToFloatOperation, s): float =>
|
let operate = (distToFloatOp: ASTTypes.distToFloatOperation, s): float =>
|
||||||
switch distToFloatOp {
|
switch distToFloatOp {
|
||||||
| #Pdf(f) => pdf(f, s)
|
| #Pdf(f) => pdf(f, s)
|
||||||
| #Cdf(f) => pdf(f, s)
|
| #Cdf(f) => pdf(f, s)
|
||||||
|
|
|
@ -80,7 +80,7 @@ module Internals = {
|
||||||
|
|
||||||
let toShape = (
|
let toShape = (
|
||||||
~samples: Internals.T.t,
|
~samples: Internals.T.t,
|
||||||
~samplingInputs: ExpressionTypes.ExpressionTree.samplingInputs,
|
~samplingInputs: ASTTypes.AST.samplingInputs,
|
||||||
(),
|
(),
|
||||||
) => {
|
) => {
|
||||||
Array.fast_sort(compare, samples)
|
Array.fast_sort(compare, samples)
|
||||||
|
|
|
@ -272,7 +272,7 @@ module T = {
|
||||||
| #Float(n) => Float.mean(n)
|
| #Float(n) => Float.mean(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
let operate = (distToFloatOp: ExpressionTypes.distToFloatOperation, s) =>
|
let operate = (distToFloatOp: ASTTypes.distToFloatOperation, s) =>
|
||||||
switch distToFloatOp {
|
switch distToFloatOp {
|
||||||
| #Cdf(f) => Ok(cdf(f, s))
|
| #Cdf(f) => Ok(cdf(f, s))
|
||||||
| #Pdf(f) => Ok(pdf(f, s))
|
| #Pdf(f) => Ok(pdf(f, s))
|
||||||
|
@ -302,7 +302,7 @@ module T = {
|
||||||
let tryAnalyticalSimplification = (
|
let tryAnalyticalSimplification = (
|
||||||
d1: symbolicDist,
|
d1: symbolicDist,
|
||||||
d2: symbolicDist,
|
d2: symbolicDist,
|
||||||
op: ExpressionTypes.algebraicOperation,
|
op: ASTTypes.algebraicOperation,
|
||||||
): analyticalSimplificationResult =>
|
): analyticalSimplificationResult =>
|
||||||
switch (d1, d2) {
|
switch (d1, d2) {
|
||||||
| (#Float(v1), #Float(v2)) =>
|
| (#Float(v1), #Float(v2)) =>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user