Change typescript interface to reducer
This commit is contained in:
		
							parent
							
								
									6565d97f53
								
							
						
					
					
						commit
						61b589d0bd
					
				|  | @ -1,11 +1,10 @@ | |||
| import { run, Distribution, resultMap } from "../src/js/index"; | ||||
| import { run, Distribution, resultMap, squiggleExpression } from "../src/js/index"; | ||||
| 
 | ||||
| let testRun = (x: string) => { | ||||
|   let result = run(x); | ||||
|   if (result.tag == "Ok") { | ||||
|     return { tag: "Ok", value: result.value.exports }; | ||||
|   } else { | ||||
|     return result; | ||||
| let testRun = (x: string): squiggleExpression => { | ||||
|   let result = run(x, {sampleCount: 100, xyPointLength: 100}); | ||||
|   expect(result.tag).toEqual("Ok") | ||||
|   if(result.tag === "Ok") { | ||||
|     return result.value | ||||
|   } | ||||
| }; | ||||
| 
 | ||||
|  | @ -16,29 +15,19 @@ function Ok<b>(x: b) { | |||
| describe("Simple calculations and results", () => { | ||||
|   test("mean(normal(5,2))", () => { | ||||
|     expect(testRun("mean(normal(5,2))")).toEqual({ | ||||
|       tag: "Ok", | ||||
|       value: [{ NAME: "Float", VAL: 5 }], | ||||
|       tag: "number", | ||||
|       value: 5 | ||||
|     }); | ||||
|   }); | ||||
|   test("10+10", () => { | ||||
|     let foo = testRun("10 + 10"); | ||||
|     expect(foo).toEqual({ tag: "Ok", value: [{ NAME: "Float", VAL: 20 }] }); | ||||
|     expect(foo).toEqual({ tag: "number", value: 20 }); | ||||
|   }); | ||||
| }); | ||||
| describe("Log function", () => { | ||||
|   test("log(1) = 0", () => { | ||||
|     let foo = testRun("log(1)"); | ||||
|     expect(foo).toEqual({ tag: "Ok", value: [{ NAME: "Float", VAL: 0 }] }); | ||||
|   }); | ||||
| }); | ||||
| 
 | ||||
| describe("Multimodal too many weights error", () => { | ||||
|   test("mm(0,0,[0,0,0])", () => { | ||||
|     let foo = testRun("mm(0,0,[0,0,0])"); | ||||
|     expect(foo).toEqual({ | ||||
|       tag: "Error", | ||||
|       value: "Function multimodal error: Too many weights provided", | ||||
|     }); | ||||
|     expect(foo).toEqual({ tag: "number", value: 0 }); | ||||
|   }); | ||||
| }); | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,13 +1,10 @@ | |||
| import { runAll } from "../rescript/ProgramEvaluator.gen"; | ||||
| import * as _ from 'lodash' | ||||
| 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"; | ||||
| import { genericDist, env, error } from "../rescript/TypescriptInterface.gen"; | ||||
| export type {  exportEnv, exportDistribution }; | ||||
| import { genericDist, samplingParams, evaluate, expressionValue, errorValue, distributionError } from "../rescript/TypescriptInterface.gen"; | ||||
| export { makeSampleSetDist } from "../rescript/TypescriptInterface.gen"; | ||||
| import { | ||||
|   Constructors_mean, | ||||
|  | @ -36,23 +33,16 @@ import { | |||
|   Constructors_pointwisePower, | ||||
| } from "../rescript/Distributions/DistributionOperation/DistributionOperation.gen"; | ||||
| 
 | ||||
| export let defaultSamplingInputs: SamplingInputs = { | ||||
|   sampleCount: 10000, | ||||
|   outputXYPoints: 10000, | ||||
|   pointDistLength: 1000, | ||||
| export type SamplingInputs = { | ||||
|   readonly sampleCount?: number;  | ||||
|   readonly outputXYPoints?: number;  | ||||
|   readonly kernelWidth?: number;  | ||||
|   readonly pointDistLength?: number | ||||
| }; | ||||
| export let defaultSamplingInputs: samplingParams = { | ||||
|   sampleCount: 10000, | ||||
|   xyPointLength: 10000 | ||||
| }; | ||||
| 
 | ||||
| export function run( | ||||
|   squiggleString: string, | ||||
|   samplingInputs?: SamplingInputs, | ||||
|   environment?: exportEnv | ||||
| ): result<exportType, string> { | ||||
|   let si: SamplingInputs = samplingInputs | ||||
|     ? samplingInputs | ||||
|     : defaultSamplingInputs; | ||||
|   let env: exportEnv = environment ? environment : []; | ||||
|   return runAll(squiggleString, si, env); | ||||
| } | ||||
| 
 | ||||
| type result<a, b> = | ||||
|   | { | ||||
|  | @ -75,147 +65,186 @@ export function resultMap<a, b, c>( | |||
|   } | ||||
| } | ||||
| 
 | ||||
| type tagged<a, b> = {tag: a, value: b} | ||||
| 
 | ||||
| function tag<a,b>(x: a, y: b) : tagged<a, b>{ | ||||
|   return { tag: x, value: y} | ||||
| } | ||||
| 
 | ||||
| export type squiggleExpression = tagged<"symbol", string> | tagged<"string", string> | tagged<"array", squiggleExpression[]> | tagged<"boolean", boolean> | tagged<"distribution", Distribution> | tagged<"number", number> | tagged<"record", {[key: string]: squiggleExpression }> | ||||
| export function run( | ||||
|   squiggleString: string, | ||||
|   samplingInputs?: samplingParams, | ||||
|   _environment?: exportEnv | ||||
| ): result<squiggleExpression, errorValue> { | ||||
|   let si: samplingParams = samplingInputs | ||||
|     ? samplingInputs | ||||
|     : defaultSamplingInputs; | ||||
|   let result : result<expressionValue, errorValue> = evaluate(squiggleString); | ||||
|   return resultMap(result, x => createTsExport(x, si)); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| function createTsExport(x: expressionValue, sampEnv: samplingParams): squiggleExpression { | ||||
|   switch (x.tag) { | ||||
|     case "EvArray": | ||||
|       return tag("array", x.value.map(x => createTsExport(x, sampEnv))); | ||||
|     case "EvBool": | ||||
|       return tag("boolean", x.value); | ||||
|     case "EvDistribution": | ||||
|       return tag("distribution", new Distribution(x.value, sampEnv)); | ||||
|     case "EvNumber": | ||||
|       return tag("number", x.value); | ||||
|     case "EvRecord": | ||||
|       return tag("record", _.mapValues(x.value, x => createTsExport(x, sampEnv))) | ||||
| 
 | ||||
|        | ||||
|        | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| export function resultExn<a, c>(r: result<a, c>): a | c { | ||||
|   return r.value; | ||||
| } | ||||
| 
 | ||||
| export class Distribution { | ||||
|   t: genericDist; | ||||
|   env: env; | ||||
|   env: samplingParams; | ||||
| 
 | ||||
|   constructor(t: genericDist, env: env) { | ||||
|   constructor(t: genericDist, env: samplingParams) { | ||||
|     this.t = t; | ||||
|     this.env = env; | ||||
|     return this; | ||||
|   } | ||||
| 
 | ||||
|   mapResultDist(r: result<genericDist, error>): result<Distribution, error> { | ||||
|   mapResultDist(r: result<genericDist, distributionError>): result<Distribution, distributionError> { | ||||
|     return resultMap(r, (v: genericDist) => new Distribution(v, this.env)); | ||||
|   } | ||||
| 
 | ||||
|   mean(): result<number, error> { | ||||
|   mean(): result<number, distributionError> { | ||||
|     return Constructors_mean({ env: this.env }, this.t); | ||||
|   } | ||||
| 
 | ||||
|   sample(): result<number, error> { | ||||
|   sample(): result<number, distributionError> { | ||||
|     return Constructors_sample({ env: this.env }, this.t); | ||||
|   } | ||||
| 
 | ||||
|   pdf(n: number): result<number, error> { | ||||
|   pdf(n: number): result<number, distributionError> { | ||||
|     return Constructors_pdf({ env: this.env }, this.t, n); | ||||
|   } | ||||
| 
 | ||||
|   cdf(n: number): result<number, error> { | ||||
|   cdf(n: number): result<number, distributionError> { | ||||
|     return Constructors_cdf({ env: this.env }, this.t, n); | ||||
|   } | ||||
| 
 | ||||
|   inv(n: number): result<number, error> { | ||||
|   inv(n: number): result<number, distributionError> { | ||||
|     return Constructors_inv({ env: this.env }, this.t, n); | ||||
|   } | ||||
| 
 | ||||
|   normalize(): result<Distribution, error> { | ||||
|   normalize(): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_normalize({ env: this.env }, this.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   toPointSet(): result<Distribution, error> { | ||||
|   toPointSet(): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_toPointSet({ env: this.env }, this.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   toSampleSet(n: number): result<Distribution, error> { | ||||
|   toSampleSet(n: number): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_toSampleSet({ env: this.env }, this.t, n) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   truncate(left: number, right: number): result<Distribution, error> { | ||||
|   truncate(left: number, right: number): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_truncate({ env: this.env }, this.t, left, right) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   inspect(): result<Distribution, error> { | ||||
|   inspect(): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist(Constructors_inspect({ env: this.env }, this.t)); | ||||
|   } | ||||
| 
 | ||||
|   toString(): result<string, error> { | ||||
|   toString(): result<string, distributionError> { | ||||
|     return Constructors_toString({ env: this.env }, this.t); | ||||
|   } | ||||
| 
 | ||||
|   toSparkline(n: number): result<string, error> { | ||||
|   toSparkline(n: number): result<string, distributionError> { | ||||
|     return Constructors_toSparkline({ env: this.env }, this.t, n); | ||||
|   } | ||||
| 
 | ||||
|   algebraicAdd(d2: Distribution): result<Distribution, error> { | ||||
|   algebraicAdd(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_algebraicAdd({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   algebraicMultiply(d2: Distribution): result<Distribution, error> { | ||||
|   algebraicMultiply(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_algebraicMultiply({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   algebraicDivide(d2: Distribution): result<Distribution, error> { | ||||
|   algebraicDivide(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_algebraicDivide({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   algebraicSubtract(d2: Distribution): result<Distribution, error> { | ||||
|   algebraicSubtract(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_algebraicSubtract({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   algebraicLogarithm(d2: Distribution): result<Distribution, error> { | ||||
|   algebraicLogarithm(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_algebraicLogarithm({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   algebraicPower(d2: Distribution): result<Distribution, error> { | ||||
|   algebraicPower(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_algebraicPower({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   pointwiseAdd(d2: Distribution): result<Distribution, error> { | ||||
|   pointwiseAdd(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_pointwiseAdd({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   pointwiseMultiply(d2: Distribution): result<Distribution, error> { | ||||
|   pointwiseMultiply(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_pointwiseMultiply({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   pointwiseDivide(d2: Distribution): result<Distribution, error> { | ||||
|   pointwiseDivide(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_pointwiseDivide({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   pointwiseSubtract(d2: Distribution): result<Distribution, error> { | ||||
|   pointwiseSubtract(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_pointwiseSubtract({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   pointwiseLogarithm(d2: Distribution): result<Distribution, error> { | ||||
|   pointwiseLogarithm(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_pointwiseLogarithm({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|   } | ||||
| 
 | ||||
|   pointwisePower(d2: Distribution): result<Distribution, error> { | ||||
|   pointwisePower(d2: Distribution): result<Distribution, distributionError> { | ||||
|     return this.mapResultDist( | ||||
|       Constructors_pointwisePower({ env: this.env }, this.t, d2.t) | ||||
|     ); | ||||
|  |  | |||
|  | @ -5,5 +5,6 @@ module Extra = Reducer_Extra | |||
| module Js = Reducer_Js | ||||
| module MathJs = Reducer_MathJs | ||||
| 
 | ||||
| type expressionValue = Reducer_Expression.expressionValue | ||||
| let evaluate = Expression.eval | ||||
| let parse = Expression.parse | ||||
|  |  | |||
|  | @ -6,5 +6,8 @@ module Js = Reducer_Js | |||
| module MathJs = Reducer_MathJs | ||||
| 
 | ||||
| @genType | ||||
| let evaluate: string => result<Expression.expressionValue, ErrorValue.errorValue> | ||||
| type expressionValue = Reducer_Expression.expressionValue | ||||
| 
 | ||||
| @genType | ||||
| let evaluate: string => result<expressionValue, Reducer_ErrorValue.errorValue> | ||||
| let parse: string => result<Expression.expression, ErrorValue.errorValue> | ||||
|  |  | |||
|  | @ -1,3 +1,4 @@ | |||
| @genType | ||||
| type errorValue = | ||||
|   | REArrayIndexNotFound(string, int) | ||||
|   | REFunctionExpected(string) | ||||
|  |  | |||
|  | @ -1,7 +1,8 @@ | |||
| module Result = Belt.Result | ||||
| module T = Reducer_Expression_T | ||||
| type expression = T.expression | ||||
| type expressionValue = ReducerInterface.ExpressionValue.expressionValue | ||||
| @genType | ||||
| type expressionValue = ReducerInterface_ExpressionValue.expressionValue | ||||
| type t = expression | ||||
| let toString: T.expression => Js.String.t | ||||
| let toStringResult: result<T.expression, 'a> => string | ||||
|  |  | |||
|  | @ -5,6 +5,7 @@ | |||
| module Extra_Array = Reducer_Extra_Array | ||||
| module ErrorValue = Reducer_ErrorValue | ||||
| 
 | ||||
| @genType | ||||
| type rec expressionValue = | ||||
|   | EvBool(bool) | ||||
|   | EvNumber(float) | ||||
|  |  | |||
|  | @ -8,20 +8,31 @@ The below few seem to work fine. In the future there's definitely more work to d | |||
| */ | ||||
| 
 | ||||
| @genType | ||||
| type env = DistributionOperation.env | ||||
| type samplingParams = DistributionOperation.env | ||||
| 
 | ||||
| @genType | ||||
| type genericDist = GenericDist_Types.genericDist | ||||
| 
 | ||||
| @genType | ||||
| type error = GenericDist_Types.error | ||||
| type distributionError = GenericDist_Types.error | ||||
| 
 | ||||
| @genType | ||||
| type resultDist = result<genericDist, error> | ||||
| type resultDist = result<genericDist, distributionError> | ||||
| 
 | ||||
| @genType | ||||
| type resultFloat = result<float, error> | ||||
| type resultFloat = result<float, distributionError> | ||||
| 
 | ||||
| @genType | ||||
| type resultString = result<string, error> | ||||
| type resultString = result<string, distributionError> | ||||
| 
 | ||||
| @genType | ||||
| let makeSampleSetDist = SampleSetDist.make | ||||
| 
 | ||||
| @genType | ||||
| let evaluate = Reducer.evaluate | ||||
| 
 | ||||
| @genType | ||||
| type expressionValue = Reducer_Expression.expressionValue | ||||
| 
 | ||||
| @genType | ||||
| type errorValue = Reducer_ErrorValue.errorValue | ||||
|  |  | |||
							
								
								
									
										1
									
								
								packages/squiggle-lang/src/rescript/shims/Js.shim.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								packages/squiggle-lang/src/rescript/shims/Js.shim.ts
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1 @@ | |||
| export type Dict_t<T> = { [key: string]: T } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user