proof of concept for records as arguments

Value: [1e-3 to 8e-1]
This commit is contained in:
Quinn Dougherty 2022-05-13 13:18:52 -04:00
parent 2ab395b4e5
commit 3eef57f855
2 changed files with 27 additions and 2 deletions

View File

@ -1,5 +1,5 @@
module ExpressionValue = ReducerInterface_ExpressionValue module ExpressionValue = ReducerInterface_ExpressionValue
type expressionValue = ReducerInterface_ExpressionValue.expressionValue type expressionValue = ExpressionValue.expressionValue
let defaultEnv: DistributionOperation.env = { let defaultEnv: DistributionOperation.env = {
sampleCount: MagicNumbers.Environment.defaultSampleCount, sampleCount: MagicNumbers.Environment.defaultSampleCount,
@ -210,7 +210,7 @@ module SymbolicConstructors = {
} }
} }
let dispatchToGenericOutput = (call: ExpressionValue.functionCall, _environment): option< let rec dispatchToGenericOutput = (call: ExpressionValue.functionCall, _environment): option<
DistributionOperation.outputType, DistributionOperation.outputType,
> => { > => {
let (fnName, args) = call let (fnName, args) = call
@ -257,6 +257,10 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall, _environment)
[EvDistribution(prior), EvDistribution(prediction), EvDistribution(Symbolic(#Float(answer)))], [EvDistribution(prior), EvDistribution(prediction), EvDistribution(Symbolic(#Float(answer)))],
) => ) =>
runGenericOperation(FromDist(ToScore(LogScore(prediction, answer)), prior))->Some runGenericOperation(FromDist(ToScore(LogScore(prediction, answer)), prior))->Some
| ("logScore", [EvRecord(r)]) =>
recurRecordArgs("logScore", ["prior", "prediction", "answer"], r, _environment)
| ("increment", [EvNumber(x)]) => (x +. 1.0)->DistributionOperation.Float->Some
| ("increment", [EvRecord(r)]) => recurRecordArgs("increment", ["incrementee"], r, _environment)
| ("logScoreAgainstImproperPrior", [EvDistribution(prediction), EvNumber(answer)]) | ("logScoreAgainstImproperPrior", [EvDistribution(prediction), EvNumber(answer)])
| ( | (
"logScoreAgainstImproperPrior", "logScoreAgainstImproperPrior",
@ -340,6 +344,16 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall, _environment)
| _ => None | _ => None
} }
} }
and recurRecordArgs = (
fnName: string,
argNames: array<string>,
args: ExpressionValue.record,
_environment: 'a,
): option<DistributionOperation.outputType> =>
// argNames -> E.A2.fmap(x => Js.Dict.get(args, x)) -> E.A.O.arrSomeToSomeArr -> E.O.bind(a => dispatchToGenericOutput((fnName, a), _environment))
argNames
->E.A2.fmap(x => Js.Dict.unsafeGet(args, x))
->(a => dispatchToGenericOutput((fnName, a), _environment))
let genericOutputToReducerValue = (o: DistributionOperation.outputType): result< let genericOutputToReducerValue = (o: DistributionOperation.outputType): result<
expressionValue, expressionValue,

View File

@ -620,6 +620,17 @@ module A = {
| Some(o) => o | Some(o) => o
| None => [] | None => []
} }
let rec arrSomeToSomeArr = (optionals: array<option<'a>>): option<array<'a>> => {
let optionals' = optionals->Belt.List.fromArray
switch optionals' {
| list{} => []->Some
| list{x, ...xs} =>
switch x {
| Some(_) => xs->Belt.List.toArray->arrSomeToSomeArr
| None => None
}
}
}
} }
module R = { module R = {