Move error types to types modules

This commit is contained in:
Sam Nolan 2022-04-23 09:56:47 -04:00
parent 79af95ed78
commit ad593e659b
8 changed files with 57 additions and 53 deletions

View File

@ -88,10 +88,7 @@ describe("eval on distribution functions", () => {
describe("log", () => { describe("log", () => {
testEval("log(2, uniform(5,8))", "Ok(Sample Set Distribution)") testEval("log(2, uniform(5,8))", "Ok(Sample Set Distribution)")
testEval( testEval("log(normal(5,2), 3)", "Error(Math Error: Operation returned complex result)")
"log(normal(5,2), 3)",
"Error(Math Error: Operation returned complex result)",
)
testEval( testEval(
"log(normal(5,2), normal(10,1))", "log(normal(5,2), normal(10,1))",
"Error(Math Error: Operation returned complex result)", "Error(Math Error: Operation returned complex result)",

View File

@ -11,16 +11,11 @@ type error =
| DistributionVerticalShiftIsInvalid | DistributionVerticalShiftIsInvalid
| TooFewSamples | TooFewSamples
| ArgumentError(string) | ArgumentError(string)
| OperationError(Operation.invalidOperationError) | OperationError(Operation.Error.invalidOperationError)
| PointSetConversionError(SampleSetDist.pointsetConversionError) | PointSetConversionError(SampleSetDist.pointsetConversionError)
| SparklineError(PointSetDist.sparklineError) // This type of error is for when we find a sparkline of a discrete distribution. This should probably at some point be actually implemented | SparklineError(PointSetTypes.sparklineError) // This type of error is for when we find a sparkline of a discrete distribution. This should probably at some point be actually implemented
| Other(string) | Other(string)
let sampleErrorToDistErr = (err: SampleSetDist.sampleSetError): error =>
switch err {
| TooFewSamples => TooFewSamples
}
@genType @genType
module Error = { module Error = {
type t = error type t = error
@ -35,14 +30,19 @@ module Error = {
| DistributionVerticalShiftIsInvalid => "Distribution Vertical Shift is Invalid" | DistributionVerticalShiftIsInvalid => "Distribution Vertical Shift is Invalid"
| ArgumentError(s) => `Argument Error ${s}` | ArgumentError(s) => `Argument Error ${s}`
| TooFewSamples => "Too Few Samples" | TooFewSamples => "Too Few Samples"
| OperationError(err) => Operation.invalidOperationErrorToString(err) | OperationError(err) => Operation.Error.invalidOperationErrorToString(err)
| PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err) | PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err)
| SparklineError(err) => PointSetDist.sparklineErrorToString(err) | SparklineError(err) => PointSetTypes.sparklineErrorToString(err)
| Other(s) => s | Other(s) => s
} }
let resultStringToResultError: result<'a, string> => result<'a, error> = n => let resultStringToResultError: result<'a, string> => result<'a, error> = n =>
n->E.R2.errMap(r => r->fromString) n->E.R2.errMap(r => r->fromString)
let sampleErrorToDistErr = (err: SampleSetDist.sampleSetError): error =>
switch err {
| TooFewSamples => TooFewSamples
}
} }
@genType @genType

View File

@ -14,7 +14,7 @@ let sampleN = (t: t, n) =>
} }
let toSampleSetDist = (t: t, n) => let toSampleSetDist = (t: t, n) =>
SampleSetDist.make(sampleN(t, n))->E.R2.errMap(DistributionTypes.sampleErrorToDistErr) SampleSetDist.make(sampleN(t, n))->E.R2.errMap(DistributionTypes.Error.sampleErrorToDistErr)
let fromFloat = (f: float): t => Symbolic(SymbolicDist.Float.make(f)) let fromFloat = (f: float): t => Symbolic(SymbolicDist.Float.make(f))
@ -151,7 +151,7 @@ module AlgebraicCombination = {
arithmeticOperation: DistributionTypes.Operation.arithmeticOperation, arithmeticOperation: DistributionTypes.Operation.arithmeticOperation,
t1: t, t1: t,
t2: t, t2: t,
): option<result<SymbolicDistTypes.symbolicDist, Operation.invalidOperationError>> => ): option<result<SymbolicDistTypes.symbolicDist, Operation.Error.invalidOperationError>> =>
switch (arithmeticOperation, t1, t2) { switch (arithmeticOperation, t1, t2) {
| (arithmeticOperation, Symbolic(d1), Symbolic(d2)) => | (arithmeticOperation, Symbolic(d1), Symbolic(d2)) =>
switch SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation) { switch SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation) {

View File

@ -215,16 +215,8 @@ let operate = (distToFloatOp: Operation.distToFloatOperation, s): float =>
| #Mean => T.mean(s) | #Mean => T.mean(s)
} }
@genType let toSparkline = (t: t, bucketCount): result<string, PointSetTypes.sparklineError> =>
type sparklineError = CannotSparklineDiscrete
let sparklineErrorToString = (err: sparklineError): string =>
switch err {
| CannotSparklineDiscrete => "Cannot find the sparkline of a discrete distribution"
}
let toSparkline = (t: t, bucketCount): result<string, sparklineError> =>
T.toContinuous(t) T.toContinuous(t)
->E.O2.fmap(Continuous.downsampleEquallyOverX(bucketCount)) ->E.O2.fmap(Continuous.downsampleEquallyOverX(bucketCount))
->E.O2.toResult(CannotSparklineDiscrete) ->E.O2.toResult(PointSetTypes.CannotSparklineDiscrete)
->E.R2.fmap(r => Continuous.getShape(r).ys->Sparklines.create()) ->E.R2.fmap(r => Continuous.getShape(r).ys->Sparklines.create())

View File

@ -94,3 +94,11 @@ module MixedPoint = {
let add = combine2((a, b) => a +. b) let add = combine2((a, b) => a +. b)
} }
@genType
type sparklineError = CannotSparklineDiscrete
let sparklineErrorToString = (err: sparklineError): string =>
switch err {
| CannotSparklineDiscrete => "Cannot find the sparkline of a discrete distribution"
}

View File

@ -1,10 +1,23 @@
@genType @genType
type sampleSetError = TooFewSamples module Error = {
@genType
type sampleSetError = TooFewSamples
let sampleSetErrorToString = (err: sampleSetError): string => let sampleSetErrorToString = (err: sampleSetError): string =>
switch err { switch err {
| TooFewSamples => "Too few samples when constructing sample set" | TooFewSamples => "Too few samples when constructing sample set"
} }
@genType
type pointsetConversionError = TooFewSamplesForConversionToPointSet
let pointsetConversionErrorToString = (err: pointsetConversionError) =>
switch err {
| TooFewSamplesForConversionToPointSet => "Too Few Samples to convert to point set"
}
}
include Error
/* /*
This is used as a smart constructor. The only way to create a SampleSetDist.t is to call This is used as a smart constructor. The only way to create a SampleSetDist.t is to call
@ -33,14 +46,6 @@ include T
let length = (t: t) => get(t)->E.A.length let length = (t: t) => get(t)->E.A.length
@genType
type pointsetConversionError = TooFewSamplesForConversionToPointSet
let pointsetConversionErrorToString = (err: pointsetConversionError) =>
switch err {
| TooFewSamplesForConversionToPointSet => "Too Few Samples to convert to point set"
}
/* /*
TODO: Refactor to get a more precise estimate. Also, this code is just fairly messy, could use TODO: Refactor to get a more precise estimate. Also, this code is just fairly messy, could use
some refactoring. some refactoring.
@ -79,10 +84,10 @@ let sampleN = (t: t, n) => {
//TODO: Figure out what to do if distributions are different lengths. ``zip`` is kind of inelegant for this. //TODO: Figure out what to do if distributions are different lengths. ``zip`` is kind of inelegant for this.
let map2 = ( let map2 = (
~fn: (float, float) => result<float, Operation.invalidOperationError>, ~fn: (float, float) => result<float, Operation.Error.invalidOperationError>,
~t1: t, ~t1: t,
~t2: t, ~t2: t,
): result<t, Operation.invalidOperationError> => { ): result<t, Operation.Error.invalidOperationError> => {
let samples = Belt.Array.zip(get(t1), get(t2))->E.A2.fmap(((a, b)) => fn(a, b)) let samples = Belt.Array.zip(get(t1), get(t2))->E.A2.fmap(((a, b)) => fn(a, b))
// This assertion should never be reached. In order for it to be reached, one // This assertion should never be reached. In order for it to be reached, one

View File

@ -45,6 +45,6 @@ type symbolicDist = [
type analyticalSimplificationResult = [ type analyticalSimplificationResult = [
| #AnalyticalSolution(symbolicDist) | #AnalyticalSolution(symbolicDist)
| #Error(Operation.invalidOperationError) | #Error(Operation.Error.invalidOperationError)
| #NoSolution | #NoSolution
] ]

View File

@ -38,19 +38,22 @@ module Convolution = {
} }
@genType @genType
type invalidOperationError = module Error = {
| DivisionByZeroError @genType
| ComplexNumberError type invalidOperationError =
| DivisionByZeroError
| ComplexNumberError
let invalidOperationErrorToString = (err: invalidOperationError): string => let invalidOperationErrorToString = (err: invalidOperationError): string =>
switch err { switch err {
| DivisionByZeroError => "Cannot divide by zero" | DivisionByZeroError => "Cannot divide by zero"
| ComplexNumberError => "Operation returned complex result" | ComplexNumberError => "Operation returned complex result"
} }
}
module Algebraic = { module Algebraic = {
type t = algebraicOperation type t = algebraicOperation
let toFn: (t, float, float) => result<float, invalidOperationError> = (x, a, b) => let toFn: (t, float, float) => result<float, Error.invalidOperationError> = (x, a, b) =>
switch x { switch x {
| #Add => Ok(a +. b) | #Add => Ok(a +. b)
| #Subtract => Ok(a -. b) | #Subtract => Ok(a -. b)
@ -70,8 +73,7 @@ module Algebraic = {
| #Logarithm => | #Logarithm =>
if b == 1. { if b == 1. {
Error(DivisionByZeroError) Error(DivisionByZeroError)
} } else if a > 0.0 && b > 0.0 {
else if a > 0.0 && b > 0.0 {
Ok(log(a) /. log(b)) Ok(log(a) /. log(b))
} else { } else {
Error(ComplexNumberError) Error(ComplexNumberError)
@ -119,7 +121,7 @@ module DistToFloat = {
// Note that different logarithms don't really do anything. // Note that different logarithms don't really do anything.
module Scale = { module Scale = {
type t = scaleOperation type t = scaleOperation
let toFn = (x: t, a: float, b: float): result<float, invalidOperationError> => let toFn = (x: t, a: float, b: float): result<float, Error.invalidOperationError> =>
switch x { switch x {
| #Multiply => Ok(a *. b) | #Multiply => Ok(a *. b)
| #Divide => | #Divide =>