diff --git a/packages/squiggle-lang/src/rescript/Distributions/DistributionTypes.res b/packages/squiggle-lang/src/rescript/Distributions/DistributionTypes.res index a9f7dfbe..5a6865fc 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/DistributionTypes.res +++ b/packages/squiggle-lang/src/rescript/Distributions/DistributionTypes.res @@ -37,6 +37,7 @@ module Error = { | LogarithmOfDistributionError(s) => `Logarithm of input error: ${s}` | SampleSetError(TooFewSamples) => "Too Few Samples" | SampleSetError(NonNumericInput(err)) => `Found a non-number in input: ${err}` + | SampleSetError(OperationError(err)) => Operation.Error.toString(err) | OperationError(err) => Operation.Error.toString(err) | PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err) | SparklineError(err) => PointSetTypes.sparklineErrorToString(err) diff --git a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res index 001c581b..834008f5 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SampleSetDist/SampleSetDist.res @@ -1,12 +1,14 @@ @genType module Error = { @genType - type sampleSetError = TooFewSamples | NonNumericInput(string) + type sampleSetError = + TooFewSamples | NonNumericInput(string) | OperationError(Operation.operationError) let sampleSetErrorToString = (err: sampleSetError): string => switch err { | TooFewSamples => "Too few samples when constructing sample set" | NonNumericInput(err) => `Found a non-number in input: ${err}` + | OperationError(err) => Operation.Error.toString(err) } @genType @@ -16,6 +18,8 @@ module Error = { switch err { | TooFewSamplesForConversionToPointSet => "Too Few Samples to convert to point set" } + + let fromOperationError = e => OperationError(e) } include Error @@ -85,13 +89,10 @@ let sampleN = (t: t, n) => { let samplesMap = (~fn: float => result, t: t): result< t, - Operation.Error.t, + sampleSetError, > => { let samples = T.get(t)->E.A2.fmap(fn) - - E.A.R.firstErrorOrOpen(samples)->E.R2.fmap(x => - E.R.toExn("Input of samples should be larger than 5", make(x)) - ) + E.A.R.firstErrorOrOpen(samples)->E.R2.errMap(Error.fromOperationError) |> E.R2.bind(make) } //TODO: Figure out what to do if distributions are different lengths. ``zip`` is kind of inelegant for this. @@ -107,7 +108,7 @@ let map2 = (~fn: (float, float) => result, ~t1: t, ~t2 // I could prove this to the type system (say, creating a {first: float, second: float, ..., fifth: float, rest: array} // But doing so would take too much time, so I'll leave it as an assertion E.A.R.firstErrorOrOpen(samples)->E.R2.fmap(x => - E.R.toExn("Input of samples should be larger than 5", make(x)) + E.R.toExnFnString(Error.sampleSetErrorToString, make(x)) ) } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res index 232c4364..cd16e1fe 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res @@ -107,7 +107,7 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce } switch SampleSetDist.samplesMap(~fn, sampleSetDist) { | Ok(r) => Ok(EvDistribution(SampleSet(r))) - | Error(r) => Error(REOperationError(r)) + | Error(r) => Error(REDistributionError(SampleSetError(r))) } } diff --git a/packages/squiggle-lang/src/rescript/Utility/E.res b/packages/squiggle-lang/src/rescript/Utility/E.res index 15678e1a..fb682f78 100644 --- a/packages/squiggle-lang/src/rescript/Utility/E.res +++ b/packages/squiggle-lang/src/rescript/Utility/E.res @@ -235,13 +235,16 @@ module R = { | Ok(a) => f(a) | Error(err) => Error(err) } - let toExn = (msg: string, x: result<'a, 'b>): 'a => switch x { | Ok(r) => r | Error(_) => raise(Assertion(msg)) } - + let toExnFnString = (errorToStringFn, o) => + switch o { + | Ok(r) => r + | Error(r) => raise(Assertion(errorToStringFn(r))) + } let default = (default, res: Belt.Result.t<'a, 'b>) => switch res { | Ok(r) => r