2022-04-08 18:42:14 +00:00
|
|
|
import { runAll } from "../rescript/ProgramEvaluator.gen";
|
|
|
|
import type {
|
|
|
|
Inputs_SamplingInputs_t as SamplingInputs,
|
|
|
|
exportEnv,
|
|
|
|
exportType,
|
|
|
|
exportDistribution,
|
|
|
|
} from "../rescript/ProgramEvaluator.gen";
|
|
|
|
export type { SamplingInputs, exportEnv, exportDistribution };
|
|
|
|
export type { t as DistPlus } from "../rescript/OldInterpreter/DistPlus.gen";
|
2022-04-11 00:51:43 +00:00
|
|
|
import { genericDist, env, error } from "../rescript/TypescriptInterface.gen";
|
|
|
|
export { makeSampleSetDist } from "../rescript/TypescriptInterface.gen";
|
2022-04-08 19:55:04 +00:00
|
|
|
import {
|
2022-04-08 23:48:53 +00:00
|
|
|
Constructors_mean,
|
|
|
|
Constructors_sample,
|
|
|
|
Constructors_pdf,
|
|
|
|
Constructors_cdf,
|
|
|
|
Constructors_inv,
|
|
|
|
Constructors_normalize,
|
|
|
|
Constructors_toPointSet,
|
|
|
|
Constructors_toSampleSet,
|
|
|
|
Constructors_truncate,
|
|
|
|
Constructors_inspect,
|
|
|
|
Constructors_toString,
|
|
|
|
Constructors_toSparkline,
|
|
|
|
Constructors_algebraicAdd,
|
|
|
|
Constructors_algebraicMultiply,
|
|
|
|
Constructors_algebraicDivide,
|
|
|
|
Constructors_algebraicSubtract,
|
|
|
|
Constructors_algebraicLogarithm,
|
2022-04-09 16:37:26 +00:00
|
|
|
Constructors_algebraicPower,
|
2022-04-08 23:48:53 +00:00
|
|
|
Constructors_pointwiseAdd,
|
|
|
|
Constructors_pointwiseMultiply,
|
|
|
|
Constructors_pointwiseDivide,
|
|
|
|
Constructors_pointwiseSubtract,
|
|
|
|
Constructors_pointwiseLogarithm,
|
2022-04-09 16:37:26 +00:00
|
|
|
Constructors_pointwisePower,
|
2022-04-08 18:42:14 +00:00
|
|
|
} from "../rescript/Distributions/DistributionOperation/DistributionOperation.gen";
|
|
|
|
|
|
|
|
export let defaultSamplingInputs: SamplingInputs = {
|
|
|
|
sampleCount: 10000,
|
|
|
|
outputXYPoints: 10000,
|
|
|
|
pointDistLength: 1000,
|
|
|
|
};
|
|
|
|
|
|
|
|
export function run(
|
|
|
|
squiggleString: string,
|
|
|
|
samplingInputs?: SamplingInputs,
|
|
|
|
environment?: exportEnv
|
2022-04-11 00:48:45 +00:00
|
|
|
): result<exportType, string> {
|
2022-04-08 18:42:14 +00:00
|
|
|
let si: SamplingInputs = samplingInputs
|
|
|
|
? samplingInputs
|
|
|
|
: defaultSamplingInputs;
|
|
|
|
let env: exportEnv = environment ? environment : [];
|
|
|
|
return runAll(squiggleString, si, env);
|
2022-03-01 07:43:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:48:45 +00:00
|
|
|
type result<a, b> =
|
2022-04-08 23:48:53 +00:00
|
|
|
| {
|
|
|
|
tag: "Ok";
|
2022-04-11 00:48:45 +00:00
|
|
|
value: a;
|
2022-04-08 23:48:53 +00:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
tag: "Error";
|
2022-04-11 00:48:45 +00:00
|
|
|
value: b;
|
2022-04-09 02:55:06 +00:00
|
|
|
};
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
export function resultMap<a, b, c>(
|
|
|
|
r: result<a, c>,
|
|
|
|
mapFn: (x: a) => b
|
|
|
|
): result<b, c> {
|
2022-04-08 23:48:53 +00:00
|
|
|
if (r.tag === "Ok") {
|
|
|
|
return { tag: "Ok", value: mapFn(r.value) };
|
|
|
|
} else {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
export function resultExn<a, c>(r: result<a, c>): a | c {
|
|
|
|
return r.value;
|
2022-04-10 01:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:48:45 +00:00
|
|
|
export class Distribution {
|
2022-04-08 18:42:14 +00:00
|
|
|
t: genericDist;
|
|
|
|
env: env;
|
|
|
|
|
|
|
|
constructor(t: genericDist, env: env) {
|
|
|
|
this.t = t;
|
2022-04-08 23:48:53 +00:00
|
|
|
this.env = env;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
mapResultDist(r: result<genericDist, error>): result<Distribution, error> {
|
2022-04-11 00:48:45 +00:00
|
|
|
return resultMap(r, (v: genericDist) => new Distribution(v, this.env));
|
2022-04-08 18:42:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
mean(): result<number, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return Constructors_mean({ env: this.env }, this.t);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
sample(): result<number, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return Constructors_sample({ env: this.env }, this.t);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
pdf(n: number): result<number, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return Constructors_pdf({ env: this.env }, this.t, n);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
cdf(n: number): result<number, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return Constructors_cdf({ env: this.env }, this.t, n);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
inv(n: number): result<number, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return Constructors_inv({ env: this.env }, this.t, n);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
normalize(): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_normalize({ env: this.env }, this.t)
|
|
|
|
);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
toPointSet(): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_toPointSet({ env: this.env }, this.t)
|
|
|
|
);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
toSampleSet(n: number): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_toSampleSet({ env: this.env }, this.t, n)
|
|
|
|
);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
truncate(left: number, right: number): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_truncate({ env: this.env }, this.t, left, right)
|
2022-04-08 19:55:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
inspect(): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(Constructors_inspect({ env: this.env }, this.t));
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:48:45 +00:00
|
|
|
toString(): result<string, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return Constructors_toString({ env: this.env }, this.t);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:48:45 +00:00
|
|
|
toSparkline(n: number): result<string, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return Constructors_toSparkline({ env: this.env }, this.t, n);
|
2022-04-08 19:55:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
algebraicAdd(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_algebraicAdd({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
algebraicMultiply(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_algebraicMultiply({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
algebraicDivide(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_algebraicDivide({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
algebraicSubtract(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_algebraicSubtract({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
algebraicLogarithm(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_algebraicLogarithm({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
algebraicPower(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
2022-04-09 16:37:26 +00:00
|
|
|
Constructors_algebraicPower({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:48:45 +00:00
|
|
|
pointwiseAdd(d2: Distribution) {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_pointwiseAdd({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
pointwiseMultiply(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_pointwiseMultiply({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
pointwiseDivide(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_pointwiseDivide({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
pointwiseSubtract(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_pointwiseSubtract({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
pointwiseLogarithm(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
|
|
|
Constructors_pointwiseLogarithm({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-11 00:51:43 +00:00
|
|
|
pointwisePower(d2: Distribution): result<Distribution, error> {
|
2022-04-08 23:48:53 +00:00
|
|
|
return this.mapResultDist(
|
2022-04-09 16:37:26 +00:00
|
|
|
Constructors_pointwisePower({ env: this.env }, this.t, d2.t)
|
2022-04-08 18:42:14 +00:00
|
|
|
);
|
|
|
|
}
|
2022-03-01 07:43:35 +00:00
|
|
|
}
|