Type the lang typescript interface

This commit is contained in:
Sam Nolan 2022-04-11 10:48:45 +10:00
parent e1d7a3ff7d
commit 1825b1f250

View File

@ -10,9 +10,7 @@ export type { t as DistPlus } from "../rescript/OldInterpreter/DistPlus.gen";
import { import {
genericDist, genericDist,
env, env,
resultDist, error,
resultFloat,
resultString,
} from "../rescript/TypescriptInterface.gen"; } from "../rescript/TypescriptInterface.gen";
export {makeSampleSetDist} from "../rescript/TypescriptInterface.gen"; export {makeSampleSetDist} from "../rescript/TypescriptInterface.gen";
import { import {
@ -52,7 +50,7 @@ export function run(
squiggleString: string, squiggleString: string,
samplingInputs?: SamplingInputs, samplingInputs?: SamplingInputs,
environment?: exportEnv environment?: exportEnv
): { tag: "Ok"; value: exportType } | { tag: "Error"; value: string } { ): result<exportType, string> {
let si: SamplingInputs = samplingInputs let si: SamplingInputs = samplingInputs
? samplingInputs ? samplingInputs
: defaultSamplingInputs; : defaultSamplingInputs;
@ -60,19 +58,17 @@ export function run(
return runAll(squiggleString, si, env); return runAll(squiggleString, si, env);
} }
//This is clearly not fully typed. I think later we should use a functional library to type result<a, b> =
// provide a better Either type and corresponding functions.
type result =
| { | {
tag: "Ok"; tag: "Ok";
value: any; value: a;
} }
| { | {
tag: "Error"; tag: "Error";
value: any; value: b;
}; };
export function resultMap(r: result, mapFn: any): result { export function resultMap<a,b,c>(r: result<a,c>, mapFn: ((x: a) => b)): result<b,c> {
if (r.tag === "Ok") { if (r.tag === "Ok") {
return { tag: "Ok", value: mapFn(r.value) }; return { tag: "Ok", value: mapFn(r.value) };
} else { } else {
@ -80,11 +76,11 @@ export function resultMap(r: result, mapFn: any): result {
} }
} }
export function resultExn(r: result): any { export function resultExn<a,c>(r: result<a,c>): a | c {
r.value return r.value
} }
export class GenericDist { export class Distribution {
t: genericDist; t: genericDist;
env: env; env: env;
@ -94,133 +90,133 @@ export class GenericDist {
return this; return this;
} }
mapResultDist(r: resultDist) { mapResultDist(r: result<genericDist, error>) : result<Distribution, error>{
return resultMap(r, (v: genericDist) => new GenericDist(v, this.env)); return resultMap(r, (v: genericDist) => new Distribution(v, this.env));
} }
mean() { mean() : result<number,error> {
return Constructors_mean({ env: this.env }, this.t); return Constructors_mean({ env: this.env }, this.t);
} }
sample(): resultFloat { sample(): result<number,error> {
return Constructors_sample({ env: this.env }, this.t); return Constructors_sample({ env: this.env }, this.t);
} }
pdf(n: number): resultFloat { pdf(n: number): result<number,error> {
return Constructors_pdf({ env: this.env }, this.t, n); return Constructors_pdf({ env: this.env }, this.t, n);
} }
cdf(n: number): resultFloat { cdf(n: number): result<number,error> {
return Constructors_cdf({ env: this.env }, this.t, n); return Constructors_cdf({ env: this.env }, this.t, n);
} }
inv(n: number): resultFloat { inv(n: number): result<number,error> {
return Constructors_inv({ env: this.env }, this.t, n); return Constructors_inv({ env: this.env }, this.t, n);
} }
normalize() { normalize() : result<Distribution, error> {
return this.mapResultDist( return this.mapResultDist(
Constructors_normalize({ env: this.env }, this.t) Constructors_normalize({ env: this.env }, this.t)
); );
} }
toPointSet() { toPointSet() : result<Distribution, error>{
return this.mapResultDist( return this.mapResultDist(
Constructors_toPointSet({ env: this.env }, this.t) Constructors_toPointSet({ env: this.env }, this.t)
); );
} }
toSampleSet(n: number) { toSampleSet(n: number) : result<Distribution, error>{
return this.mapResultDist( return this.mapResultDist(
Constructors_toSampleSet({ env: this.env }, this.t, n) Constructors_toSampleSet({ env: this.env }, this.t, n)
); );
} }
truncate(left: number, right: number) { truncate(left: number, right: number) : result<Distribution, error> {
return this.mapResultDist( return this.mapResultDist(
Constructors_truncate({ env: this.env }, this.t, left, right) Constructors_truncate({ env: this.env }, this.t, left, right)
); );
} }
inspect() { inspect() : result<Distribution, error> {
return this.mapResultDist(Constructors_inspect({ env: this.env }, this.t)); return this.mapResultDist(Constructors_inspect({ env: this.env }, this.t));
} }
toString(): resultString { toString(): result<string, error> {
return Constructors_toString({ env: this.env }, this.t); return Constructors_toString({ env: this.env }, this.t);
} }
toSparkline(n: number): resultString { toSparkline(n: number): result<string, error> {
return Constructors_toSparkline({ env: this.env }, this.t, n); return Constructors_toSparkline({ env: this.env }, this.t, n);
} }
algebraicAdd(d2: GenericDist) { algebraicAdd(d2: Distribution) : result<Distribution, error> {
return this.mapResultDist( return this.mapResultDist(
Constructors_algebraicAdd({ env: this.env }, this.t, d2.t) Constructors_algebraicAdd({ env: this.env }, this.t, d2.t)
); );
} }
algebraicMultiply(d2: GenericDist) { algebraicMultiply(d2: Distribution) : result<Distribution, error> {
return this.mapResultDist( return this.mapResultDist(
Constructors_algebraicMultiply({ env: this.env }, this.t, d2.t) Constructors_algebraicMultiply({ env: this.env }, this.t, d2.t)
); );
} }
algebraicDivide(d2: GenericDist) { algebraicDivide(d2: Distribution) : result<Distribution, error> {
return this.mapResultDist( return this.mapResultDist(
Constructors_algebraicDivide({ env: this.env }, this.t, d2.t) Constructors_algebraicDivide({ env: this.env }, this.t, d2.t)
); );
} }
algebraicSubtract(d2: GenericDist) { algebraicSubtract(d2: Distribution) : result<Distribution, error> {
return this.mapResultDist( return this.mapResultDist(
Constructors_algebraicSubtract({ env: this.env }, this.t, d2.t) Constructors_algebraicSubtract({ env: this.env }, this.t, d2.t)
); );
} }
algebraicLogarithm(d2: GenericDist) { algebraicLogarithm(d2: Distribution) : result<Distribution, error> {
return this.mapResultDist( return this.mapResultDist(
Constructors_algebraicLogarithm({ env: this.env }, this.t, d2.t) Constructors_algebraicLogarithm({ env: this.env }, this.t, d2.t)
); );
} }
algebraicPower(d2: GenericDist) { algebraicPower(d2: Distribution) : result<Distribution, error>{
return this.mapResultDist( return this.mapResultDist(
Constructors_algebraicPower({ env: this.env }, this.t, d2.t) Constructors_algebraicPower({ env: this.env }, this.t, d2.t)
); );
} }
pointwiseAdd(d2: GenericDist) { pointwiseAdd(d2: Distribution) {
return this.mapResultDist( return this.mapResultDist(
Constructors_pointwiseAdd({ env: this.env }, this.t, d2.t) Constructors_pointwiseAdd({ env: this.env }, this.t, d2.t)
); );
} }
pointwiseMultiply(d2: GenericDist) { pointwiseMultiply(d2: Distribution) : result<Distribution, error>{
return this.mapResultDist( return this.mapResultDist(
Constructors_pointwiseMultiply({ env: this.env }, this.t, d2.t) Constructors_pointwiseMultiply({ env: this.env }, this.t, d2.t)
); );
} }
pointwiseDivide(d2: GenericDist) { pointwiseDivide(d2: Distribution) : result<Distribution, error>{
return this.mapResultDist( return this.mapResultDist(
Constructors_pointwiseDivide({ env: this.env }, this.t, d2.t) Constructors_pointwiseDivide({ env: this.env }, this.t, d2.t)
); );
} }
pointwiseSubtract(d2: GenericDist) { pointwiseSubtract(d2: Distribution) : result<Distribution, error>{
return this.mapResultDist( return this.mapResultDist(
Constructors_pointwiseSubtract({ env: this.env }, this.t, d2.t) Constructors_pointwiseSubtract({ env: this.env }, this.t, d2.t)
); );
} }
pointwiseLogarithm(d2: GenericDist) { pointwiseLogarithm(d2: Distribution) : result<Distribution, error>{
return this.mapResultDist( return this.mapResultDist(
Constructors_pointwiseLogarithm({ env: this.env }, this.t, d2.t) Constructors_pointwiseLogarithm({ env: this.env }, this.t, d2.t)
); );
} }
pointwisePower(d2: GenericDist) { pointwisePower(d2: Distribution) : result<Distribution, error>{
return this.mapResultDist( return this.mapResultDist(
Constructors_pointwisePower({ env: this.env }, this.t, d2.t) Constructors_pointwisePower({ env: this.env }, this.t, d2.t)
); );