squiggle/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Helpers.res

157 lines
5.1 KiB
Plaintext
Raw Normal View History

open FunctionRegistry_Core
let impossibleError = "Wrong inputs / Logically impossible"
module Wrappers = {
let symbolic = r => DistributionTypes.Symbolic(r)
let evDistribution = r => ReducerInterface_ExpressionValue.EvDistribution(r)
2022-05-23 18:28:32 +00:00
let symbolicEvDistribution = r => r->DistributionTypes.Symbolic->evDistribution
}
module Prepare = {
2022-05-21 15:41:12 +00:00
type ts = array<frValue>
type err = string
2022-05-21 15:41:12 +00:00
module ToValueArray = {
module Record = {
let twoArgs = (inputs: ts): result<ts, err> =>
switch inputs {
| [FRValueRecord([(_, n1), (_, n2)])] => Ok([n1, n2])
| _ => Error(impossibleError)
}
}
}
2022-05-21 15:41:12 +00:00
module ToValueTuple = {
let twoDistOrNumber = (values: ts): result<(frValueDistOrNumber, frValueDistOrNumber), err> => {
switch values {
| [FRValueDistOrNumber(a1), FRValueDistOrNumber(a2)] => Ok(a1, a2)
| _ => Error(impossibleError)
}
}
2022-05-23 18:28:32 +00:00
let oneDistOrNumber = (values: ts): result<frValueDistOrNumber, err> => {
switch values {
| [FRValueDistOrNumber(a1)] => Ok(a1)
| _ => Error(impossibleError)
}
}
2022-05-21 15:41:12 +00:00
module Record = {
let twoDistOrNumber = (values: ts): result<(frValueDistOrNumber, frValueDistOrNumber), err> =>
values->ToValueArray.Record.twoArgs->E.R.bind(twoDistOrNumber)
}
}
}
module Process = {
2022-05-23 18:28:32 +00:00
module DistOrNumberToDist = {
module Helpers = {
let toSampleSet = (r, env: DistributionOperation.env) =>
GenericDist.toSampleSetDist(r, env.sampleCount)
let mapFnResult = r =>
switch r {
| Ok(r) => Ok(GenericDist.sample(r))
| Error(r) => Error(Operation.Other(r))
}
let wrapSymbolic = (fn, r) => r->fn->E.R2.fmap(Wrappers.symbolic)
let singleVarSample = (dist, fn, env) => {
switch toSampleSet(dist, env) {
| Ok(dist) =>
switch SampleSetDist.samplesMap(~fn=f => fn(f)->mapFnResult, dist) {
| Ok(r) => Ok(DistributionTypes.SampleSet(r))
| Error(r) => Error(DistributionTypes.Error.toString(DistributionTypes.SampleSetError(r)))
}
| Error(r) => Error(DistributionTypes.Error.toString(r))
}
}
2022-05-23 18:28:32 +00:00
let twoVarSample = (dist1, dist2, fn, env) => {
let altFn = (a, b) => fn((a, b))->mapFnResult
switch E.R.merge(toSampleSet(dist1, env), toSampleSet(dist2, env)) {
| Ok((t1, t2)) =>
switch SampleSetDist.map2(~fn=altFn, ~t1, ~t2) {
| Ok(r) => Ok(DistributionTypes.SampleSet(r))
| Error(r) => Error(Operation.Error.toString(r))
}
| Error(r) => Error(DistributionTypes.Error.toString(r))
2022-05-21 16:09:49 +00:00
}
}
}
2022-05-23 18:28:32 +00:00
let oneValue = (
~fn: float => result<DistributionTypes.genericDist, string>,
~value: frValueDistOrNumber,
~env: DistributionOperation.env,
): result<DistributionTypes.genericDist, string> => {
switch value {
| FRValueNumber(a1) => fn(a1)
| FRValueDist(a1) => Helpers.singleVarSample(a1, r => fn(r), env)
2022-05-21 16:09:49 +00:00
}
}
2022-05-23 18:28:32 +00:00
let oneValueUsingSymbolicDist = (~fn, ~value) => oneValue(~fn=Helpers.wrapSymbolic(fn), ~value)
let twoValues = (
~fn: ((float, float)) => result<DistributionTypes.genericDist, string>,
~values: (frValueDistOrNumber, frValueDistOrNumber),
~env: DistributionOperation.env,
): result<DistributionTypes.genericDist, string> => {
switch values {
| (FRValueNumber(a1), FRValueNumber(a2)) => fn((a1, a2))
| (FRValueDist(a1), FRValueNumber(a2)) => Helpers.singleVarSample(a1, r => fn((r, a2)), env)
| (FRValueNumber(a1), FRValueDist(a2)) => Helpers.singleVarSample(a2, r => fn((a1, r)), env)
| (FRValueDist(a1), FRValueDist(a2)) => Helpers.twoVarSample(a1, a2, fn, env)
}
}
2022-05-23 18:28:32 +00:00
let twoValuesUsingSymbolicDist = (~fn, ~values) =>
twoValues(~fn=Helpers.wrapSymbolic(fn), ~values)
}
2022-05-20 21:36:40 +00:00
}
module TwoArgDist = {
2022-05-21 16:18:54 +00:00
let process = (~fn, ~env, r) =>
2022-05-21 15:41:12 +00:00
r
2022-05-23 18:28:32 +00:00
->E.R.bind(Process.DistOrNumberToDist.twoValuesUsingSymbolicDist(~fn, ~values=_, ~env))
2022-05-21 15:41:12 +00:00
->E.R2.fmap(Wrappers.evDistribution)
2022-05-20 21:36:40 +00:00
2022-05-22 14:38:17 +00:00
let make = (name, fn) => {
2022-05-21 16:18:54 +00:00
FnDefinition.make(~name, ~inputs=[FRTypeDistOrNumber, FRTypeDistOrNumber], ~run=(inputs, env) =>
inputs->Prepare.ToValueTuple.twoDistOrNumber->process(~fn, ~env)
2022-05-20 21:36:40 +00:00
)
}
2022-05-22 14:38:17 +00:00
let makeRecordP5P95 = (name, fn) => {
2022-05-21 02:53:53 +00:00
FnDefinition.make(
2022-05-20 21:36:40 +00:00
~name,
2022-05-21 02:53:53 +00:00
~inputs=[FRTypeRecord([("p5", FRTypeDistOrNumber), ("p95", FRTypeDistOrNumber)])],
2022-05-21 16:18:54 +00:00
~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env),
2022-05-20 21:36:40 +00:00
)
}
2022-05-22 14:38:17 +00:00
let makeRecordMeanStdev = (name, fn) => {
2022-05-21 02:53:53 +00:00
FnDefinition.make(
2022-05-20 21:36:40 +00:00
~name,
2022-05-21 02:53:53 +00:00
~inputs=[FRTypeRecord([("mean", FRTypeDistOrNumber), ("stdev", FRTypeDistOrNumber)])],
2022-05-21 16:18:54 +00:00
~run=(inputs, env) => inputs->Prepare.ToValueTuple.Record.twoDistOrNumber->process(~fn, ~env),
2022-05-20 21:36:40 +00:00
)
}
2022-05-23 18:28:32 +00:00
}
2022-05-23 18:28:32 +00:00
module OneArgDist = {
let process = (~fn, ~env, r) =>
r
->E.R.bind(Process.DistOrNumberToDist.oneValueUsingSymbolicDist(~fn, ~value=_, ~env))
->E.R2.fmap(Wrappers.evDistribution)
let make = (name, fn) => {
FnDefinition.make(~name, ~inputs=[FRTypeDistOrNumber], ~run=(inputs, env) =>
inputs->Prepare.ToValueTuple.oneDistOrNumber->process(~fn, ~env)
)
}
2022-05-20 21:36:40 +00:00
}