Changed GenericDist outputType from polymorphic variant to regular variant

This commit is contained in:
Ozzie Gooen 2022-03-31 09:27:36 -04:00
parent f2d03c8f11
commit d82615cef0
3 changed files with 42 additions and 40 deletions

View File

@ -22,14 +22,14 @@ let toExt: option<'a> => 'a = E.O.toExt(
describe("normalize", () => { describe("normalize", () => {
test("has no impact on normal dist", () => { test("has no impact on normal dist", () => {
let result = run(#fromDist(#toDist(#normalize), normalDist)) let result = run(#fromDist(#toDist(#normalize), normalDist))
expect(result)->toEqual(#Dist(normalDist)) expect(result)->toEqual(Dist(normalDist))
}) })
}) })
describe("mean", () => { describe("mean", () => {
test("for a normal distribution", () => { test("for a normal distribution", () => {
let result = GenericDist_GenericOperation.run(params, #fromDist(#toFloat(#Mean), normalDist)) let result = GenericDist_GenericOperation.run(params, #fromDist(#toFloat(#Mean), normalDist))
expect(result)->toEqual(#Float(5.0)) expect(result)->toEqual(Float(5.0))
}) })
}) })
@ -59,7 +59,7 @@ describe("toPointSet", () => {
run(#fromDist(#toDist(#toPointSet), #SampleSet([0.0, 1.0, 2.0, 3.0])))->outputMap( run(#fromDist(#toDist(#toPointSet), #SampleSet([0.0, 1.0, 2.0, 3.0])))->outputMap(
#fromDist(#toFloat(#Mean)), #fromDist(#toFloat(#Mean)),
) )
expect(result)->toEqual(#GenDistError(Other("Converting sampleSet to pointSet failed"))) expect(result)->toEqual(GenDistError(Other("Converting sampleSet to pointSet failed")))
}) })
Skip.test("on sample set", () => { Skip.test("on sample set", () => {

View File

@ -9,35 +9,34 @@ type params = {
xyPointLength: int, xyPointLength: int,
} }
type outputType = [ type outputType =
| #Dist(genericDist) | Dist(GenericDist_Types.genericDist)
| #Float(float) | Float(float)
| #String(string) | String(string)
| #GenDistError(error) | GenDistError(GenericDist_Types.error)
]
module Output = { module Output = {
let toDist = (o: outputType) => let toDist = (o: outputType) =>
switch o { switch o {
| #Dist(d) => Some(d) | Dist(d) => Some(d)
| _ => None | _ => None
} }
let toFloat = (o: outputType) => let toFloat = (o: outputType) =>
switch o { switch o {
| #Float(d) => Some(d) | Float(d) => Some(d)
| _ => None | _ => None
} }
let toString = (o: outputType) => let toString = (o: outputType) =>
switch o { switch o {
| #String(d) => Some(d) | String(d) => Some(d)
| _ => None | _ => None
} }
let toError = (o: outputType) => let toError = (o: outputType) =>
switch o { switch o {
| #GenDistError(d) => Some(d) | GenDistError(d) => Some(d)
| _ => None | _ => None
} }
} }
@ -45,19 +44,19 @@ module Output = {
let fromResult = (r: result<outputType, error>): outputType => let fromResult = (r: result<outputType, error>): outputType =>
switch r { switch r {
| Ok(o) => o | Ok(o) => o
| Error(e) => #GenDistError(e) | Error(e) => GenDistError(e)
} }
//This is used to catch errors in other switch statements. //This is used to catch errors in other switch statements.
let _errorMap = (o: outputType): error => let _errorMap = (o: outputType): error =>
switch o { switch o {
| #GenDistError(r) => r | GenDistError(r) => r
| _ => Unreachable | _ => Unreachable
} }
let outputToDistResult = (o: outputType): result<genericDist, error> => let outputToDistResult = (o: outputType): result<genericDist, error> =>
switch o { switch o {
| #Dist(r) => Ok(r) | Dist(r) => Ok(r)
| r => Error(_errorMap(r)) | r => Error(_errorMap(r))
} }
@ -70,14 +69,14 @@ let rec run = (extra, fnName: operation): outputType => {
let toPointSetFn = r => { let toPointSetFn = r => {
switch reCall(~fnName=#fromDist(#toDist(#toPointSet), r), ()) { switch reCall(~fnName=#fromDist(#toDist(#toPointSet), r), ()) {
| #Dist(#PointSet(p)) => Ok(p) | Dist(#PointSet(p)) => Ok(p)
| r => Error(_errorMap(r)) | r => Error(_errorMap(r))
} }
} }
let toSampleSetFn = r => { let toSampleSetFn = r => {
switch reCall(~fnName=#fromDist(#toDist(#toSampleSet(sampleCount)), r), ()) { switch reCall(~fnName=#fromDist(#toDist(#toSampleSet(sampleCount)), r), ()) {
| #Dist(#SampleSet(p)) => Ok(p) | Dist(#SampleSet(p)) => Ok(p)
| r => Error(_errorMap(r)) | r => Error(_errorMap(r))
} }
} }
@ -98,37 +97,37 @@ let rec run = (extra, fnName: operation): outputType => {
switch subFnName { switch subFnName {
| #toFloat(fnName) => | #toFloat(fnName) =>
GenericDist.operationToFloat(dist, ~toPointSetFn, ~operation=fnName) GenericDist.operationToFloat(dist, ~toPointSetFn, ~operation=fnName)
->E.R2.fmap(r => #Float(r)) ->E.R2.fmap(r => Float(r))
->fromResult ->fromResult
| #toString => dist->GenericDist.toString->(r => #String(r)) | #toString => dist->GenericDist.toString->String
| #toDist(#inspect) => { | #toDist(#inspect) => {
Js.log2("Console log requested: ", dist) Js.log2("Console log requested: ", dist)
#Dist(dist) Dist(dist)
} }
| #toDist(#normalize) => dist->GenericDist.normalize->(r => #Dist(r)) | #toDist(#normalize) => dist->GenericDist.normalize->Dist
| #toDist(#truncate(leftCutoff, rightCutoff)) => | #toDist(#truncate(leftCutoff, rightCutoff)) =>
GenericDist.truncate(~toPointSetFn, ~leftCutoff, ~rightCutoff, dist, ()) GenericDist.truncate(~toPointSetFn, ~leftCutoff, ~rightCutoff, dist, ())
->E.R2.fmap(r => #Dist(r)) ->E.R2.fmap(r => Dist(r))
->fromResult ->fromResult
| #toDist(#toPointSet) => | #toDist(#toPointSet) =>
dist->GenericDist.toPointSet(xyPointLength)->E.R2.fmap(r => #Dist(#PointSet(r)))->fromResult dist->GenericDist.toPointSet(xyPointLength)->E.R2.fmap(r => Dist(#PointSet(r)))->fromResult
| #toDist(#toSampleSet(n)) => | #toDist(#toSampleSet(n)) =>
dist->GenericDist.sampleN(n)->E.R2.fmap(r => #Dist(#SampleSet(r)))->fromResult dist->GenericDist.sampleN(n)->E.R2.fmap(r => Dist(#SampleSet(r)))->fromResult
| #toDistCombination(#Algebraic, _, #Float(_)) => #GenDistError(NotYetImplemented) | #toDistCombination(#Algebraic, _, #Float(_)) => GenDistError(NotYetImplemented)
| #toDistCombination(#Algebraic, operation, #Dist(t2)) => | #toDistCombination(#Algebraic, operation, #Dist(t2)) =>
dist dist
->GenericDist.algebraicCombination(~toPointSetFn, ~toSampleSetFn, ~operation, ~t2) ->GenericDist.algebraicCombination(~toPointSetFn, ~toSampleSetFn, ~operation, ~t2)
->E.R2.fmap(r => #Dist(r)) ->E.R2.fmap(r => Dist(r))
->fromResult ->fromResult
| #toDistCombination(#Pointwise, operation, #Dist(t2)) => | #toDistCombination(#Pointwise, operation, #Dist(t2)) =>
dist dist
->GenericDist.pointwiseCombination(~toPointSetFn, ~operation, ~t2) ->GenericDist.pointwiseCombination(~toPointSetFn, ~operation, ~t2)
->E.R2.fmap(r => #Dist(r)) ->E.R2.fmap(r => Dist(r))
->fromResult ->fromResult
| #toDistCombination(#Pointwise, operation, #Float(float)) => | #toDistCombination(#Pointwise, operation, #Float(float)) =>
dist dist
->GenericDist.pointwiseCombinationFloat(~toPointSetFn, ~operation, ~float) ->GenericDist.pointwiseCombinationFloat(~toPointSetFn, ~operation, ~float)
->E.R2.fmap(r => #Dist(r)) ->E.R2.fmap(r => Dist(r))
->fromResult ->fromResult
} }
@ -139,7 +138,7 @@ let rec run = (extra, fnName: operation): outputType => {
| #mixture(dists) => | #mixture(dists) =>
dists dists
->GenericDist.mixture(~scaleMultiplyFn=scaleMultiply, ~pointwiseAddFn=pointwiseAdd) ->GenericDist.mixture(~scaleMultiplyFn=scaleMultiply, ~pointwiseAddFn=pointwiseAdd)
->E.R2.fmap(r => #Dist(r)) ->E.R2.fmap(r => Dist(r))
->fromResult ->fromResult
} }
} }
@ -153,9 +152,9 @@ let outputMap = (
fn: GenericDist_Types.Operation.singleParamaterFunction, fn: GenericDist_Types.Operation.singleParamaterFunction,
): outputType => { ): outputType => {
let newFnCall: result<operation, error> = switch (fn, input) { let newFnCall: result<operation, error> = switch (fn, input) {
| (#fromDist(fromDist), #Dist(o)) => Ok(#fromDist(fromDist, o)) | (#fromDist(fromDist), Dist(o)) => Ok(#fromDist(fromDist, o))
| (#fromFloat(fromDist), #Float(o)) => Ok(#fromFloat(fromDist, o)) | (#fromFloat(fromDist), Float(o)) => Ok(#fromFloat(fromDist, o))
| (_, #GenDistError(r)) => Error(r) | (_, GenDistError(r)) => Error(r)
| (#fromDist(_), _) => Error(Other("Expected dist, got something else")) | (#fromDist(_), _) => Error(Other("Expected dist, got something else"))
| (#fromFloat(_), _) => Error(Other("Expected float, got something else")) | (#fromFloat(_), _) => Error(Other("Expected float, got something else"))
} }

View File

@ -3,12 +3,11 @@ type params = {
xyPointLength: int, xyPointLength: int,
} }
type outputType = [ type outputType =
| #Dist(GenericDist_Types.genericDist) | Dist(GenericDist_Types.genericDist)
| #GenDistError(GenericDist_Types.error) | Float(float)
| #Float(float) | String(string)
| #String(string) | GenDistError(GenericDist_Types.error)
]
let run: (params, GenericDist_Types.Operation.genericFunctionCallInfo) => outputType let run: (params, GenericDist_Types.Operation.genericFunctionCallInfo) => outputType
let runFromDist: ( let runFromDist: (
@ -17,7 +16,11 @@ let runFromDist: (
GenericDist_Types.genericDist, GenericDist_Types.genericDist,
) => outputType ) => outputType
let runFromFloat: (params, GenericDist_Types.Operation.fromDist, float) => outputType let runFromFloat: (params, GenericDist_Types.Operation.fromDist, float) => outputType
let outputMap: (params, outputType, GenericDist_Types.Operation.singleParamaterFunction) => outputType let outputMap: (
params,
outputType,
GenericDist_Types.Operation.singleParamaterFunction,
) => outputType
module Output: { module Output: {
let toDist: outputType => option<GenericDist_Types.genericDist> let toDist: outputType => option<GenericDist_Types.genericDist>