2022-03-30 01:28:14 +00:00
|
|
|
type operation = GenericDist_Types.Operation.genericFunctionCallInfo
|
2022-03-27 20:59:46 +00:00
|
|
|
type genericDist = GenericDist_Types.genericDist
|
|
|
|
type error = GenericDist_Types.error
|
|
|
|
|
|
|
|
// TODO: It could be great to use a cache for some calculations (basically, do memoization). Also, better analytics/tracking could go a long way.
|
2022-03-27 18:22:26 +00:00
|
|
|
|
|
|
|
type params = {
|
|
|
|
sampleCount: int,
|
|
|
|
xyPointLength: int,
|
|
|
|
}
|
|
|
|
|
2022-03-31 13:27:36 +00:00
|
|
|
type outputType =
|
|
|
|
| Dist(GenericDist_Types.genericDist)
|
|
|
|
| Float(float)
|
|
|
|
| String(string)
|
|
|
|
| GenDistError(GenericDist_Types.error)
|
2022-03-27 18:22:26 +00:00
|
|
|
|
2022-03-28 19:14:39 +00:00
|
|
|
module Output = {
|
|
|
|
let toDist = (o: outputType) =>
|
|
|
|
switch o {
|
2022-03-31 13:27:36 +00:00
|
|
|
| Dist(d) => Some(d)
|
2022-03-28 19:14:39 +00:00
|
|
|
| _ => None
|
|
|
|
}
|
|
|
|
|
|
|
|
let toFloat = (o: outputType) =>
|
|
|
|
switch o {
|
2022-03-31 13:27:36 +00:00
|
|
|
| Float(d) => Some(d)
|
2022-03-28 19:14:39 +00:00
|
|
|
| _ => None
|
|
|
|
}
|
|
|
|
|
|
|
|
let toString = (o: outputType) =>
|
|
|
|
switch o {
|
2022-03-31 13:27:36 +00:00
|
|
|
| String(d) => Some(d)
|
2022-03-28 19:14:39 +00:00
|
|
|
| _ => None
|
|
|
|
}
|
|
|
|
|
|
|
|
let toError = (o: outputType) =>
|
|
|
|
switch o {
|
2022-03-31 13:27:36 +00:00
|
|
|
| GenDistError(d) => Some(d)
|
2022-03-28 19:14:39 +00:00
|
|
|
| _ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 18:22:26 +00:00
|
|
|
let fromResult = (r: result<outputType, error>): outputType =>
|
|
|
|
switch r {
|
|
|
|
| Ok(o) => o
|
2022-03-31 13:27:36 +00:00
|
|
|
| Error(e) => GenDistError(e)
|
2022-03-27 18:22:26 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 13:19:27 +00:00
|
|
|
//This is used to catch errors in other switch statements.
|
|
|
|
let _errorMap = (o: outputType): error =>
|
|
|
|
switch o {
|
2022-03-31 13:27:36 +00:00
|
|
|
| GenDistError(r) => r
|
2022-03-31 13:19:27 +00:00
|
|
|
| _ => Unreachable
|
|
|
|
}
|
|
|
|
|
2022-03-31 12:37:04 +00:00
|
|
|
let outputToDistResult = (o: outputType): result<genericDist, error> =>
|
|
|
|
switch o {
|
2022-03-31 13:27:36 +00:00
|
|
|
| Dist(r) => Ok(r)
|
2022-03-31 13:19:27 +00:00
|
|
|
| r => Error(_errorMap(r))
|
2022-03-27 21:37:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 20:59:46 +00:00
|
|
|
let rec run = (extra, fnName: operation): outputType => {
|
|
|
|
let {sampleCount, xyPointLength} = extra
|
2022-03-27 21:37:27 +00:00
|
|
|
|
2022-03-27 20:59:46 +00:00
|
|
|
let reCall = (~extra=extra, ~fnName=fnName, ()) => {
|
|
|
|
run(extra, fnName)
|
2022-03-27 18:22:26 +00:00
|
|
|
}
|
2022-03-27 21:37:27 +00:00
|
|
|
|
2022-03-31 13:19:27 +00:00
|
|
|
let toPointSetFn = r => {
|
2022-03-27 20:59:46 +00:00
|
|
|
switch reCall(~fnName=#fromDist(#toDist(#toPointSet), r), ()) {
|
2022-03-31 13:27:36 +00:00
|
|
|
| Dist(#PointSet(p)) => Ok(p)
|
2022-03-31 13:19:27 +00:00
|
|
|
| r => Error(_errorMap(r))
|
2022-03-27 18:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-27 21:37:27 +00:00
|
|
|
|
2022-03-31 13:19:27 +00:00
|
|
|
let toSampleSetFn = r => {
|
2022-03-27 20:59:46 +00:00
|
|
|
switch reCall(~fnName=#fromDist(#toDist(#toSampleSet(sampleCount)), r), ()) {
|
2022-03-31 13:27:36 +00:00
|
|
|
| Dist(#SampleSet(p)) => Ok(p)
|
2022-03-31 13:19:27 +00:00
|
|
|
| r => Error(_errorMap(r))
|
2022-03-27 18:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-27 20:59:46 +00:00
|
|
|
|
2022-03-27 21:37:27 +00:00
|
|
|
let scaleMultiply = (r, weight) =>
|
|
|
|
reCall(
|
|
|
|
~fnName=#fromDist(#toDistCombination(#Pointwise, #Multiply, #Float(weight)), r),
|
|
|
|
(),
|
2022-03-29 19:21:38 +00:00
|
|
|
)->outputToDistResult
|
2022-03-27 21:37:27 +00:00
|
|
|
|
|
|
|
let pointwiseAdd = (r1, r2) =>
|
|
|
|
reCall(
|
|
|
|
~fnName=#fromDist(#toDistCombination(#Pointwise, #Add, #Dist(r2)), r1),
|
|
|
|
(),
|
2022-03-29 19:21:38 +00:00
|
|
|
)->outputToDistResult
|
2022-03-27 21:37:27 +00:00
|
|
|
|
2022-03-31 12:37:04 +00:00
|
|
|
let fromDistFn = (subFnName: GenericDist_Types.Operation.fromDist, dist: genericDist) =>
|
|
|
|
switch subFnName {
|
2022-03-27 20:59:46 +00:00
|
|
|
| #toFloat(fnName) =>
|
2022-03-31 13:19:27 +00:00
|
|
|
GenericDist.operationToFloat(dist, ~toPointSetFn, ~operation=fnName)
|
2022-03-31 13:27:36 +00:00
|
|
|
->E.R2.fmap(r => Float(r))
|
2022-03-31 13:19:27 +00:00
|
|
|
->fromResult
|
2022-03-31 13:27:36 +00:00
|
|
|
| #toString => dist->GenericDist.toString->String
|
2022-03-31 13:19:27 +00:00
|
|
|
| #toDist(#inspect) => {
|
2022-03-28 19:14:39 +00:00
|
|
|
Js.log2("Console log requested: ", dist)
|
2022-03-31 13:27:36 +00:00
|
|
|
Dist(dist)
|
2022-03-28 19:14:39 +00:00
|
|
|
}
|
2022-03-31 13:27:36 +00:00
|
|
|
| #toDist(#normalize) => dist->GenericDist.normalize->Dist
|
2022-03-31 13:19:27 +00:00
|
|
|
| #toDist(#truncate(leftCutoff, rightCutoff)) =>
|
|
|
|
GenericDist.truncate(~toPointSetFn, ~leftCutoff, ~rightCutoff, dist, ())
|
2022-03-31 13:27:36 +00:00
|
|
|
->E.R2.fmap(r => Dist(r))
|
2022-03-31 13:19:27 +00:00
|
|
|
->fromResult
|
2022-03-27 20:59:46 +00:00
|
|
|
| #toDist(#toPointSet) =>
|
2022-03-31 13:27:36 +00:00
|
|
|
dist->GenericDist.toPointSet(xyPointLength)->E.R2.fmap(r => Dist(#PointSet(r)))->fromResult
|
2022-03-27 20:59:46 +00:00
|
|
|
| #toDist(#toSampleSet(n)) =>
|
2022-03-31 13:27:36 +00:00
|
|
|
dist->GenericDist.sampleN(n)->E.R2.fmap(r => Dist(#SampleSet(r)))->fromResult
|
|
|
|
| #toDistCombination(#Algebraic, _, #Float(_)) => GenDistError(NotYetImplemented)
|
2022-03-31 13:19:27 +00:00
|
|
|
| #toDistCombination(#Algebraic, operation, #Dist(t2)) =>
|
2022-03-27 20:59:46 +00:00
|
|
|
dist
|
2022-03-31 13:19:27 +00:00
|
|
|
->GenericDist.algebraicCombination(~toPointSetFn, ~toSampleSetFn, ~operation, ~t2)
|
2022-03-31 13:27:36 +00:00
|
|
|
->E.R2.fmap(r => Dist(r))
|
2022-03-29 19:47:32 +00:00
|
|
|
->fromResult
|
2022-03-31 13:19:27 +00:00
|
|
|
| #toDistCombination(#Pointwise, operation, #Dist(t2)) =>
|
2022-03-27 20:59:46 +00:00
|
|
|
dist
|
2022-03-31 13:19:27 +00:00
|
|
|
->GenericDist.pointwiseCombination(~toPointSetFn, ~operation, ~t2)
|
2022-03-31 13:27:36 +00:00
|
|
|
->E.R2.fmap(r => Dist(r))
|
2022-03-29 19:47:32 +00:00
|
|
|
->fromResult
|
2022-03-31 13:19:27 +00:00
|
|
|
| #toDistCombination(#Pointwise, operation, #Float(float)) =>
|
2022-03-27 20:59:46 +00:00
|
|
|
dist
|
2022-03-31 13:19:27 +00:00
|
|
|
->GenericDist.pointwiseCombinationFloat(~toPointSetFn, ~operation, ~float)
|
2022-03-31 13:27:36 +00:00
|
|
|
->E.R2.fmap(r => Dist(r))
|
2022-03-29 19:47:32 +00:00
|
|
|
->fromResult
|
2022-03-27 20:59:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-27 18:22:26 +00:00
|
|
|
switch fnName {
|
2022-03-31 12:37:04 +00:00
|
|
|
| #fromDist(subFnName, dist) => fromDistFn(subFnName, dist)
|
2022-03-31 13:19:27 +00:00
|
|
|
| #fromFloat(subFnName, float) =>
|
|
|
|
reCall(~fnName=#fromDist(subFnName, GenericDist.fromFloat(float)), ())
|
2022-03-27 21:37:27 +00:00
|
|
|
| #mixture(dists) =>
|
2022-03-31 13:19:27 +00:00
|
|
|
dists
|
|
|
|
->GenericDist.mixture(~scaleMultiplyFn=scaleMultiply, ~pointwiseAddFn=pointwiseAdd)
|
2022-03-31 13:27:36 +00:00
|
|
|
->E.R2.fmap(r => Dist(r))
|
2022-03-31 13:19:27 +00:00
|
|
|
->fromResult
|
2022-03-27 18:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 19:14:39 +00:00
|
|
|
|
|
|
|
let runFromDist = (extra, fnName, dist) => run(extra, #fromDist(fnName, dist))
|
|
|
|
let runFromFloat = (extra, fnName, float) => run(extra, #fromFloat(fnName, float))
|
|
|
|
|
2022-03-31 12:41:50 +00:00
|
|
|
let outputMap = (
|
2022-03-28 19:14:39 +00:00
|
|
|
extra,
|
|
|
|
input: outputType,
|
2022-03-29 18:36:54 +00:00
|
|
|
fn: GenericDist_Types.Operation.singleParamaterFunction,
|
2022-03-28 19:14:39 +00:00
|
|
|
): outputType => {
|
|
|
|
let newFnCall: result<operation, error> = switch (fn, input) {
|
2022-03-31 13:27:36 +00:00
|
|
|
| (#fromDist(fromDist), Dist(o)) => Ok(#fromDist(fromDist, o))
|
|
|
|
| (#fromFloat(fromDist), Float(o)) => Ok(#fromFloat(fromDist, o))
|
|
|
|
| (_, GenDistError(r)) => Error(r)
|
2022-03-28 19:14:39 +00:00
|
|
|
| (#fromDist(_), _) => Error(Other("Expected dist, got something else"))
|
|
|
|
| (#fromFloat(_), _) => Error(Other("Expected float, got something else"))
|
|
|
|
}
|
2022-03-29 21:35:33 +00:00
|
|
|
newFnCall->E.R2.fmap(r => run(extra, r))->fromResult
|
2022-03-31 13:19:27 +00:00
|
|
|
}
|