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