diff --git a/packages/squiggle-lang/src/js/index.ts b/packages/squiggle-lang/src/js/index.ts index 5f12bf56..a250c4fc 100644 --- a/packages/squiggle-lang/src/js/index.ts +++ b/packages/squiggle-lang/src/js/index.ts @@ -8,13 +8,39 @@ import type { export type { SamplingInputs, exportEnv, exportDistribution }; export type { t as DistPlus } from "../rescript/OldInterpreter/DistPlus.gen"; import type { Operation_genericFunctionCallInfo } from "../rescript/Distributions/GenericDist/GenericDist_Types.gen"; -import { genericDist } from "../rescript/TSInterface.gen"; import { - run as runR, + genericDist, + resultDist, + resultFloat, + resultString, +} from "../rescript/TSInterface.gen"; +import { env, - outputType, + Constructors_UsingDists_mean, + Constructors_UsingDists_sample, + Constructors_UsingDists_pdf, + Constructors_UsingDists_cdf, + Constructors_UsingDists_inv, + Constructors_UsingDists_normalize, + Constructors_UsingDists_toPointSet, + Constructors_UsingDists_toSampleSet, + Constructors_UsingDists_truncate, + Constructors_UsingDists_inspect, + Constructors_UsingDists_toString, + Constructors_UsingDists_toSparkline, + Constructors_UsingDists_algebraicAdd, + Constructors_UsingDists_algebraicMultiply, + Constructors_UsingDists_algebraicDivide, + Constructors_UsingDists_algebraicSubtract, + Constructors_UsingDists_algebraicLogarithm, + Constructors_UsingDists_algebraicExponentiate, + Constructors_UsingDists_pointwiseAdd, + Constructors_UsingDists_pointwiseMultiply, + Constructors_UsingDists_pointwiseDivide, + Constructors_UsingDists_pointwiseSubtract, + Constructors_UsingDists_pointwiseLogarithm, + Constructors_UsingDists_pointwiseExponentiate, } from "../rescript/Distributions/DistributionOperation/DistributionOperation.gen"; -import { add } from "lodash"; export let defaultSamplingInputs: SamplingInputs = { sampleCount: 10000, @@ -42,140 +68,152 @@ class GenericDist { this.t = t; } - mean(): outputType { - return runR( + mean(): resultFloat { + return Constructors_UsingDists_mean({ env: this.env }, this.t); + } + + sample(): resultFloat { + return Constructors_UsingDists_sample({ env: this.env }, this.t); + } + + pdf(n: number): resultFloat { + return Constructors_UsingDists_pdf({ env: this.env }, this.t, n); + } + + cdf(n: number): resultFloat { + return Constructors_UsingDists_cdf({ env: this.env }, this.t, n); + } + + inv(n: number): resultFloat { + return Constructors_UsingDists_inv({ env: this.env }, this.t, n); + } + + normalize(): resultDist { + return Constructors_UsingDists_normalize({ env: this.env }, this.t); + } + + toPointSet(): resultDist { + return Constructors_UsingDists_toPointSet({ env: this.env }, this.t); + } + + toSampleSet(n: number): resultDist { + return Constructors_UsingDists_toSampleSet({ env: this.env }, this.t, n); + } + + truncate(left: number, right: number): resultDist { + return Constructors_UsingDists_truncate( { env: this.env }, - { tag: "FromDist", value: [{ tag: "ToFloat", value: "Mean" }, this.t] } + this.t, + left, + right ); } - pdf(n: number): outputType { - return runR( + inspect(): resultDist { + return Constructors_UsingDists_inspect({ env: this.env }, this.t); + } + + toString(): resultString { + return Constructors_UsingDists_toString({ env: this.env }, this.t); + } + + toSparkline(n: number): resultString { + return Constructors_UsingDists_toSparkline({ env: this.env }, this.t, n); + } + + algebraicAdd(d2: GenericDist): resultDist { + return Constructors_UsingDists_algebraicAdd( { env: this.env }, - { - tag: "FromDist", - value: [{ tag: "ToFloat", value: { NAME: "Pdf", VAL: n } }, this.t], - } + this.t, + d2.t ); } - cdf(n: number): outputType { - return runR( + algebraicMultiply(d2: GenericDist): resultDist { + return Constructors_UsingDists_algebraicMultiply( { env: this.env }, - { - tag: "FromDist", - value: [{ tag: "ToFloat", value: { NAME: "Pdf", VAL: n } }, this.t], - } + this.t, + d2.t ); } - inv(n: number): outputType { - return runR( + algebraicDivide(d2: GenericDist): resultDist { + return Constructors_UsingDists_algebraicDivide( { env: this.env }, - { - tag: "FromDist", - value: [{ tag: "ToFloat", value: { NAME: "Pdf", VAL: n } }, this.t], - } + this.t, + d2.t ); } - normalize(n: number): outputType { - return runR( + algebraicSubtract(d2: GenericDist): resultDist { + return Constructors_UsingDists_algebraicSubtract( { env: this.env }, - { - tag: "FromDist", - value: [{ tag: "ToDist", value: "Normalize" }, this.t], - } + this.t, + d2.t ); } - toPointSet(): outputType { - return runR( + algebraicLogarithm(d2: GenericDist): resultDist { + return Constructors_UsingDists_algebraicLogarithm( { env: this.env }, - { - tag: "FromDist", - value: [{ tag: "ToDist", value: "ToPointSet" }, this.t], - } + this.t, + d2.t ); } - inspect(): outputType { - return runR( + algebraicExponentiate(d2: GenericDist): resultDist { + return Constructors_UsingDists_algebraicExponentiate( { env: this.env }, - { tag: "FromDist", value: [{ tag: "ToDist", value: "Inspect" }, this.t] } + this.t, + d2.t ); } - toSampleSet(n: number): outputType { - return runR( + pointwiseAdd(d2: GenericDist): resultDist { + return Constructors_UsingDists_pointwiseAdd( { env: this.env }, - { - tag: "FromDist", - value: [ - { tag: "ToDist", value: { tag: "ToSampleSet", value: n } }, - this.t, - ], - } + this.t, + d2.t ); } - truncate( - left: null | undefined | number, - right: null | undefined | number - ): outputType { - return runR( + pointwiseMultiply(d2: GenericDist): resultDist { + return Constructors_UsingDists_pointwiseMultiply( { env: this.env }, - { - tag: "FromDist", - value: [ - { tag: "ToDist", value: { tag: "Truncate", value: [left, right] } }, - this.t, - ], - } + this.t, + d2.t ); } - toString(): outputType { - return runR( + pointwiseDivide(d2: GenericDist): resultDist { + return Constructors_UsingDists_pointwiseDivide( { env: this.env }, - { - tag: "FromDist", - value: [{ tag: "ToString", value: "ToString" }, this.t], - } + this.t, + d2.t ); } - toSparkline(n: number): outputType { - return runR( + pointwiseSubtract(d2: GenericDist): resultDist { + return Constructors_UsingDists_pointwiseSubtract( { env: this.env }, - { - tag: "FromDist", - value: [ - { tag: "ToString", value: { tag: "ToSparkline", value: n } }, - this.t, - ], - } + this.t, + d2.t ); } - add(g2: genericDist): outputType { - return runR( + pointwiseLogarithm(d2: GenericDist): resultDist { + return Constructors_UsingDists_pointwiseLogarithm( { env: this.env }, - { - tag: "FromDist", - value: [ - { - tag: "ToDistCombination", - value: [ - { - tag: "ToDistCombination", - value: ["Algebraic", "Add", { NAME: "Dist", VAL: g2 }], - }, - ], - }, - this.t, - ], - } + this.t, + d2.t + ); + } + + pointwiseExponentiate(d2: GenericDist): resultDist { + return Constructors_UsingDists_pointwiseExponentiate( + { env: this.env }, + this.t, + d2.t ); } } diff --git a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.res b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.res index 1d25f64c..2014880f 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.res +++ b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.res @@ -190,10 +190,7 @@ module Constructors = { module UsingDists = { module C = GenericDist_Types.Constructors.UsingDists open OutputLocal - type dist = GenericDist_Types.genericDist - type fResult = result - type dResult = result - type sResult = result + type floatResult= result let mean = (~env, dist) => run(~env, C.mean(dist))->toFloatR let sample = (~env, dist) => run(~env, C.sample(dist))->toFloatR @@ -231,4 +228,4 @@ module Constructors = { let pointwiseExponentiate = (~env, dist1, dist2) => run(~env, C.pointwiseSubtract(dist1, dist2))->toDistR } -} +} \ No newline at end of file diff --git a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.resi b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.resi index e39d5848..b0799dc9 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.resi +++ b/packages/squiggle-lang/src/rescript/Distributions/DistributionOperation/DistributionOperation.resi @@ -4,19 +4,21 @@ type env = { xyPointLength: int, } +open GenericDist_Types + @genType type outputType = - | Dist(GenericDist_Types.genericDist) + | Dist(genericDist) | Float(float) | String(string) - | GenDistError(GenericDist_Types.error) + | GenDistError(error) @genType let run: (~env: env, GenericDist_Types.Operation.genericFunctionCallInfo) => outputType let runFromDist: ( ~env: env, ~functionCallInfo: GenericDist_Types.Operation.fromDist, - GenericDist_Types.genericDist, + genericDist, ) => outputType let runFromFloat: ( ~env: env, @@ -26,45 +28,70 @@ let runFromFloat: ( module Output: { type t = outputType - let toDist: t => option - let toDistR: t => result + let toDist: t => option + let toDistR: t => result let toFloat: t => option - let toFloatR: t => result + let toFloatR: t => result let toString: t => option - let toStringR: t => result - let toError: t => option + let toStringR: t => result + let toError: t => option let fmap: (~env: env, t, GenericDist_Types.Operation.singleParamaterFunction) => t } module Constructors: { module UsingDists: { - type dist = GenericDist_Types.genericDist - type fResult = result - type dResult = result - type sResult = result - let mean: (~env: env, dist) => fResult - let sample: (~env: env, dist) => fResult - let cdf: (~env: env, dist, float) => fResult - let inv: (~env: env, dist, float) => fResult - let pdf: (~env: env, dist, float) => fResult - let normalize: (~env: env, dist) => dResult - let toPointSet: (~env: env, dist) => dResult - let toSampleSet: (~env: env, dist, int) => dResult - let truncate: (~env: env, dist, option, option) => dResult - let inspect: (~env: env, dist) => dResult - let toString: (~env: env, dist) => sResult - let toSparkline: (~env: env, dist, int) => sResult - let algebraicAdd: (~env: env, dist, dist) => dResult - let algebraicMultiply: (~env: env, dist, dist) => dResult - let algebraicDivide: (~env: env, dist, dist) => dResult - let algebraicSubtract: (~env: env, dist, dist) => dResult - let algebraicLogarithm: (~env: env, dist, dist) => dResult - let algebraicExponentiate: (~env: env, dist, dist) => dResult - let pointwiseAdd: (~env: env, dist, dist) => dResult - let pointwiseMultiply: (~env: env, dist, dist) => dResult - let pointwiseDivide: (~env: env, dist, dist) => dResult - let pointwiseSubtract: (~env: env, dist, dist) => dResult - let pointwiseLogarithm: (~env: env, dist, dist) => dResult - let pointwiseExponentiate: (~env: env, dist, dist) => dResult + @genType + let mean: (~env: env, genericDist) => result + @genType + let sample: (~env: env, genericDist) => result + @genType + let cdf: (~env: env, genericDist, float) => result + @genType + let inv: (~env: env, genericDist, float) => result + @genType + let pdf: (~env: env, genericDist, float) => result + @genType + let normalize: (~env: env, genericDist) => result + @genType + let toPointSet: (~env: env, genericDist) => result + @genType + let toSampleSet: (~env: env, genericDist, int) => result + @genType + let truncate: ( + ~env: env, + genericDist, + option, + option, + ) => result + @genType + let inspect: (~env: env, genericDist) => result + @genType + let toString: (~env: env, genericDist) => result + @genType + let toSparkline: (~env: env, genericDist, int) => result + @genType + let algebraicAdd: (~env: env, genericDist, genericDist) => result + @genType + let algebraicMultiply: (~env: env, genericDist, genericDist) => result + @genType + let algebraicDivide: (~env: env, genericDist, genericDist) => result + @genType + let algebraicSubtract: (~env: env, genericDist, genericDist) => result + @genType + let algebraicLogarithm: (~env: env, genericDist, genericDist) => result + @genType + let algebraicExponentiate: (~env: env, genericDist, genericDist) => result + @genType + let pointwiseAdd: (~env: env, genericDist, genericDist) => result + @genType + let pointwiseMultiply: (~env: env, genericDist, genericDist) => result + @genType + let pointwiseDivide: (~env: env, genericDist, genericDist) => result + @genType + let pointwiseSubtract: (~env: env, genericDist, genericDist) => result + @genType + let pointwiseLogarithm: (~env: env, genericDist, genericDist) => result + @genType + let pointwiseExponentiate: (~env: env, genericDist, genericDist) => result } } diff --git a/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res b/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res index f472a6e0..90189d00 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res +++ b/packages/squiggle-lang/src/rescript/Distributions/GenericDist/GenericDist_Types.res @@ -114,7 +114,7 @@ module Constructors = { let inspect = (dist): t => FromDist(ToDist(Inspect), dist) let toString = (dist): t => FromDist(ToString(ToString), dist) let toSparkline = (dist, n): t => FromDist(ToString(ToSparkline(n)), dist) - let algebraicAdd = (dist1, dist2:genericDist): t => FromDist( + let algebraicAdd = (dist1, dist2: genericDist): t => FromDist( ToDistCombination(Algebraic, #Add, #Dist(dist2)), dist1, ) @@ -164,3 +164,63 @@ module Constructors = { ) } } + +module DistVariant = { + type t = + | Mean(genericDist) + | Sample(genericDist) + | Cdf(genericDist, float) + | Inv(genericDist, float) + | Pdf(genericDist, float) + | Normalize(genericDist) + | ToPointSet(genericDist) + | ToSampleSet(genericDist, int) + | Truncate(genericDist, option, option) + | Inspect(genericDist) + | ToString(genericDist) + | ToSparkline(genericDist, int) + | AlgebraicAdd(genericDist, genericDist) + | AlgebraicMultiply(genericDist, genericDist) + | AlgebraicDivide(genericDist, genericDist) + | AlgebraicSubtract(genericDist, genericDist) + | AlgebraicLogarithm(genericDist, genericDist) + | AlgebraicExponentiate(genericDist, genericDist) + | PointwiseAdd(genericDist, genericDist) + | PointwiseMultiply(genericDist, genericDist) + | PointwiseDivide(genericDist, genericDist) + | PointwiseSubtract(genericDist, genericDist) + | PointwiseLogarithm(genericDist, genericDist) + | PointwiseExponentiate(genericDist, genericDist) + + let toGenericFunctionCallInfo = (t: t) => + switch t { + | Mean(d) => Operation.FromDist(ToFloat(#Mean), d) + | Sample(d) => FromDist(ToFloat(#Mean), d) + | Cdf(d, f) => FromDist(ToFloat(#Cdf(f)), d) + | Inv(d, f) => FromDist(ToFloat(#Inv(f)), d) + | Pdf(d, f) => FromDist(ToFloat(#Pdf(f)), d) + | Normalize(d) => FromDist(ToDist(Normalize), d) + | ToPointSet(d) => FromDist(ToDist(ToPointSet), d) + | ToSampleSet(d, r) => FromDist(ToDist(ToSampleSet(r)), d) + | Truncate(d, left, right) => FromDist(ToDist(Truncate(left, right)), d) + | Inspect(d) => FromDist(ToDist(Inspect), d) + | ToString(d) => FromDist(ToString(ToString), d) + | ToSparkline(d, n) => FromDist(ToString(ToSparkline(n)), d) + | AlgebraicAdd(d1, d2) => FromDist(ToDistCombination(Algebraic, #Add, #Dist(d2)), d1) + | AlgebraicMultiply(d1, d2) => FromDist(ToDistCombination(Algebraic, #Multiply, #Dist(d2)), d1) + | AlgebraicDivide(d1, d2) => FromDist(ToDistCombination(Algebraic, #Divide, #Dist(d2)), d1) + | AlgebraicSubtract(d1, d2) => FromDist(ToDistCombination(Algebraic, #Subtract, #Dist(d2)), d1) + | AlgebraicLogarithm(d1, d2) => + FromDist(ToDistCombination(Algebraic, #Logarithm, #Dist(d2)), d1) + | AlgebraicExponentiate(d1, d2) => + FromDist(ToDistCombination(Algebraic, #Exponentiate, #Dist(d2)), d1) + | PointwiseAdd(d1, d2) => FromDist(ToDistCombination(Pointwise, #Add, #Dist(d2)), d1) + | PointwiseMultiply(d1, d2) => FromDist(ToDistCombination(Pointwise, #Multiply, #Dist(d2)), d1) + | PointwiseDivide(d1, d2) => FromDist(ToDistCombination(Pointwise, #Divide, #Dist(d2)), d1) + | PointwiseSubtract(d1, d2) => FromDist(ToDistCombination(Pointwise, #Subtract, #Dist(d2)), d1) + | PointwiseLogarithm(d1, d2) => + FromDist(ToDistCombination(Pointwise, #Logarithm, #Dist(d2)), d1) + | PointwiseExponentiate(d1, d2) => + FromDist(ToDistCombination(Pointwise, #Exponentiate, #Dist(d2)), d1) + } +} \ No newline at end of file diff --git a/packages/squiggle-lang/src/rescript/TSInterface.res b/packages/squiggle-lang/src/rescript/TSInterface.res index 8df300f8..26b694a5 100644 --- a/packages/squiggle-lang/src/rescript/TSInterface.res +++ b/packages/squiggle-lang/src/rescript/TSInterface.res @@ -11,4 +11,11 @@ type genericDist = GenericDist_Types.genericDist; type error = GenericDist_Types.error; @genType -let runDistributionOperation = DistributionOperation.run; \ No newline at end of file +let runDistributionOperation = DistributionOperation.run; + +@genType +type resultDist = result +@genType +type resultFloat = result +@genType +type resultString = result \ No newline at end of file