Story cleanup

This commit is contained in:
Ozzie Gooen 2022-07-21 11:29:59 -07:00
parent acebaa517b
commit 47f1be0702
8 changed files with 30 additions and 43 deletions

View File

@ -68,7 +68,7 @@ describe("FunctionRegistry Library", () => {
"Ok([2,3,4,5,6,7])", "Ok([2,3,4,5,6,7])",
) )
testEvalToBe( testEvalToBe(
"toList(SampleSet.mapN([SampleSet.fromList([1,2,3,4,5,6]), SampleSet.fromList([6, 5, 4, 3, 2, 1])], {|x| x[0] > x[1] ? x[0] : x[1]}))", "SampleSet.toList(SampleSet.mapN([SampleSet.fromList([1,2,3,4,5,6]), SampleSet.fromList([6, 5, 4, 3, 2, 1])], {|x| x[0] > x[1] ? x[0] : x[1]}))",
"Ok([6,5,4,4,5,6])", "Ok([6,5,4,4,5,6])",
) )
}) })

View File

@ -17,10 +17,10 @@
"test:reducer": "jest __tests__/Reducer*/", "test:reducer": "jest __tests__/Reducer*/",
"benchmark": "ts-node benchmark/conversion_tests.ts", "benchmark": "ts-node benchmark/conversion_tests.ts",
"test": "jest", "test": "jest",
"test:lib": "jest __tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.bs.js",
"test:ts": "jest __tests__/TS/", "test:ts": "jest __tests__/TS/",
"test:rescript": "jest --modulePathIgnorePatterns=__tests__/TS/*", "test:rescript": "jest --modulePathIgnorePatterns=__tests__/TS/*",
"test:watch": "jest --watchAll", "test:watch": "jest --watchAll",
"test:fnRegistry": "jest __tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.bs.js",
"coverage:rescript": "rm -f *.coverage && yarn clean && BISECT_ENABLE=yes yarn build && yarn test:rescript && bisect-ppx-report html", "coverage:rescript": "rm -f *.coverage && yarn clean && BISECT_ENABLE=yes yarn build && yarn test:rescript && bisect-ppx-report html",
"coverage:ts": "yarn clean && yarn build && nyc --reporter=lcov yarn test:ts", "coverage:ts": "yarn clean && yarn build && nyc --reporter=lcov yarn test:ts",
"coverage:rescript:ci": "yarn clean && BISECT_ENABLE=yes yarn build:rescript && yarn test:rescript && bisect-ppx-report send-to Codecov", "coverage:rescript:ci": "yarn clean && BISECT_ENABLE=yes yarn build:rescript && yarn test:rescript && bisect-ppx-report send-to Codecov",

View File

@ -170,7 +170,6 @@ module FRType = {
inputs: array<t>, inputs: array<t>,
args: array<internalExpressionValue>, args: array<internalExpressionValue>,
): option<array<frValue>> => { ): option<array<frValue>> => {
// Js.log3("Matching", inputs, args)
let isSameLength = E.A.length(inputs) == E.A.length(args) let isSameLength = E.A.length(inputs) == E.A.length(args)
if !isSameLength { if !isSameLength {
None None
@ -186,6 +185,9 @@ module FRType = {
This module, Matcher, is fairly lengthy. However, only two functions from it This module, Matcher, is fairly lengthy. However, only two functions from it
are meant to be used outside of it. These are findMatches and matchToDef in Matches.Registry. are meant to be used outside of it. These are findMatches and matchToDef in Matches.Registry.
The rest of it is just called from those two functions. The rest of it is just called from those two functions.
Update: This really should be completely re-done sometime, and tested. It works, but it's pretty messy. I'm sure
there are internal bugs, but the end functionality works, so I'm not too worried.
*/ */
module Matcher = { module Matcher = {
module MatchSimple = { module MatchSimple = {
@ -243,11 +245,11 @@ module Matcher = {
let match = ( let match = (
f: function, f: function,
namespace: option<string>, nameSpace: option<string>,
fnName: string, fnName: string,
args: array<internalExpressionValue>, args: array<internalExpressionValue>,
): match => { ): match => {
switch namespace { switch nameSpace {
| Some(ns) if ns !== f.nameSpace => Match.DifferentName | Some(ns) if ns !== f.nameSpace => Match.DifferentName
| _ => { | _ => {
let matchedDefinition = () => let matchedDefinition = () =>
@ -280,12 +282,12 @@ module Matcher = {
module RegistryMatch = { module RegistryMatch = {
type match = { type match = {
namespace: string, nameSpace: string,
fnName: string, fnName: string,
inputIndex: int, inputIndex: int,
} }
let makeMatch = (namespace: string, fnName: string, inputIndex: int) => { let makeMatch = (nameSpace: string, fnName: string, inputIndex: int) => {
namespace: namespace, nameSpace: nameSpace,
fnName: fnName, fnName: fnName,
inputIndex: inputIndex, inputIndex: inputIndex,
} }
@ -294,12 +296,12 @@ module Matcher = {
module Registry = { module Registry = {
let _findExactMatches = ( let _findExactMatches = (
r: registry, r: registry,
namespace: option<string>, nameSpace: option<string>,
fnName: string, fnName: string,
args: array<internalExpressionValue>, args: array<internalExpressionValue>,
) => { ) => {
let functionMatchPairs = let functionMatchPairs =
r.functions->E.A2.fmap(l => (l, Function.match(l, namespace, fnName, args))) r.functions->E.A2.fmap(l => (l, Function.match(l, nameSpace, fnName, args)))
let fullMatch = functionMatchPairs->E.A.getBy(((_, match)) => Match.isFullMatch(match)) let fullMatch = functionMatchPairs->E.A.getBy(((_, match)) => Match.isFullMatch(match))
fullMatch->E.O.bind(((fn, match)) => fullMatch->E.O.bind(((fn, match)) =>
switch match { switch match {
@ -311,12 +313,12 @@ module Matcher = {
let _findNameMatches = ( let _findNameMatches = (
r: registry, r: registry,
namespace: option<string>, nameSpace: option<string>,
fnName: string, fnName: string,
args: array<internalExpressionValue>, args: array<internalExpressionValue>,
) => { ) => {
let functionMatchPairs = let functionMatchPairs =
r.functions->E.A2.fmap(l => (l, Function.match(l, namespace, fnName, args))) r.functions->E.A2.fmap(l => (l, Function.match(l, nameSpace, fnName, args)))
let getNameMatches = let getNameMatches =
functionMatchPairs functionMatchPairs
->E.A2.fmap(((fn, match)) => Match.isNameMatchOnly(match) ? Some((fn, match)) : None) ->E.A2.fmap(((fn, match)) => Match.isNameMatchOnly(match) ? Some((fn, match)) : None)
@ -351,11 +353,11 @@ module Matcher = {
let matchToDef = ( let matchToDef = (
registry: registry, registry: registry,
{namespace, fnName, inputIndex}: RegistryMatch.match, {nameSpace, fnName, inputIndex}: RegistryMatch.match,
): option<fnDefinition> => ): option<fnDefinition> =>
registry.functions registry.functions
->E.A.getBy(fn => { ->E.A.getBy(fn => {
namespace === fn.nameSpace && fnName === fn.name nameSpace === fn.nameSpace && fnName === fn.name
}) })
->E.O.bind(fn => E.A.get(fn.definitions, inputIndex)) ->E.O.bind(fn => E.A.get(fn.definitions, inputIndex))
} }
@ -508,8 +510,6 @@ module Registry = {
`There are function matches for ${fnName}(), but with different arguments: ${defs}` `There are function matches for ${fnName}(), but with different arguments: ${defs}`
} }
// let match = Matcher.Registry.findMatches(modified, fnName, args); Js.log2("Match", match)
switch Matcher.Registry.findMatches(modified, fnName, args) { switch Matcher.Registry.findMatches(modified, fnName, args) {
| Matcher.Match.FullMatch(match) => | Matcher.Match.FullMatch(match) =>
match->matchToDef->E.O2.fmap(FnDefinition.run(_, args, env, reducer)) match->matchToDef->E.O2.fmap(FnDefinition.run(_, args, env, reducer))

View File

@ -156,7 +156,7 @@ let library = [
FnDefinition.make( FnDefinition.make(
~name="fromList", ~name="fromList",
~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))], ~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))],
~run=(inputs, _, _, _) =>{ ~run=(inputs, _, _, _) => {
switch inputs { switch inputs {
| [IEvArray(items)] => Internals.fromList(items) | [IEvArray(items)] => Internals.fromList(items)
| _ => Error(impossibleError) | _ => Error(impossibleError)

View File

@ -28,8 +28,8 @@ module Internal = {
reducer, reducer,
) => { ) => {
let sampleCount = env.sampleCount let sampleCount = env.sampleCount
let fn = (r) => doLambdaCall(aLambdaValue, list{IEvNumber(r)}, env, reducer) let fn = r => doLambdaCall(aLambdaValue, list{IEvNumber(r)}, env, reducer)
Belt_Array.makeBy(sampleCount, (r) => fn(r->Js.Int.toFloat))->E.A.R.firstErrorOrOpen Belt_Array.makeBy(sampleCount, r => fn(r->Js.Int.toFloat))->E.A.R.firstErrorOrOpen
} }
let map1 = (sampleSetDist: t, aLambdaValue, env, reducer) => { let map1 = (sampleSetDist: t, aLambdaValue, env, reducer) => {
@ -125,7 +125,7 @@ let library = [
Function.make( Function.make(
~name="toList", ~name="toList",
~nameSpace, ~nameSpace,
~requiresNamespace=false, ~requiresNamespace=true,
~examples=[`SampleSet.toList(SampleSet.fromDist(normal(5,2)))`], ~examples=[`SampleSet.toList(SampleSet.fromDist(normal(5,2)))`],
~output=ReducerInterface_InternalExpressionValue.EvtArray, ~output=ReducerInterface_InternalExpressionValue.EvtArray,
~definitions=[ ~definitions=[
@ -146,9 +146,9 @@ let library = [
Function.make( Function.make(
~name="fromFn", ~name="fromFn",
~nameSpace, ~nameSpace,
~requiresNamespace=false, ~requiresNamespace=true,
~examples=[`SampleSet.fromFn(sample(normal(5,2)))`], ~examples=[`SampleSet.fromFn({|| sample(normal(5,2))})`],
~output=ReducerInterface_InternalExpressionValue.EvtArray, ~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[ ~definitions=[
FnDefinition.make( FnDefinition.make(
~name="fromFn", ~name="fromFn",

View File

@ -96,7 +96,6 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce
let doExportBindings = (bindings: nameSpace) => bindings->Bindings.toExpressionValue->Ok let doExportBindings = (bindings: nameSpace) => bindings->Bindings.toExpressionValue->Ok
module SampleMap = { module SampleMap = {
type t = SampleSetDist.t
let doLambdaCall = (aLambdaValue, list) => let doLambdaCall = (aLambdaValue, list) =>
switch Lambda.doLambdaCall(aLambdaValue, list, environment, reducer) { switch Lambda.doLambdaCall(aLambdaValue, list, environment, reducer) {
| Ok(IEvNumber(f)) => Ok(f) | Ok(IEvNumber(f)) => Ok(f)

View File

@ -233,19 +233,6 @@ let dispatchToGenericOutput = (call: IEV.functionCall, env: GenericDist.env): op
| ("inv", [IEvDistribution(dist), IEvNumber(float)]) => Helpers.toFloatFn(#Inv(float), dist, ~env) | ("inv", [IEvDistribution(dist), IEvNumber(float)]) => Helpers.toFloatFn(#Inv(float), dist, ~env)
| ("quantile", [IEvDistribution(dist), IEvNumber(float)]) => | ("quantile", [IEvDistribution(dist), IEvNumber(float)]) =>
Helpers.toFloatFn(#Inv(float), dist, ~env) Helpers.toFloatFn(#Inv(float), dist, ~env)
| ("toSampleSet", [IEvDistribution(dist), IEvNumber(float)]) =>
Helpers.toDistFn(ToSampleSet(Belt.Int.fromFloat(float)), dist, ~env)
| ("toSampleSet", [IEvDistribution(dist)]) =>
Helpers.toDistFn(ToSampleSet(env.sampleCount), dist, ~env)
| ("toList", [IEvDistribution(SampleSet(dist))]) => Some(FloatArray(SampleSetDist.T.get(dist)))
| ("fromSamples", [IEvArray(inputArray)]) => {
let _wrapInputErrors = x => SampleSetDist.NonNumericInput(x)
let parsedArray = Helpers.parseNumberArray(inputArray)->E.R2.errMap(_wrapInputErrors)
switch parsedArray {
| Ok(array) => DistributionOperation.run(FromSamples(array), ~env)
| Error(e) => GenDistError(SampleSetError(e))
}->Some
}
| ("inspect", [IEvDistribution(dist)]) => Helpers.toDistFn(Inspect, dist, ~env) | ("inspect", [IEvDistribution(dist)]) => Helpers.toDistFn(Inspect, dist, ~env)
| ("truncateLeft", [IEvDistribution(dist), IEvNumber(float)]) => | ("truncateLeft", [IEvDistribution(dist), IEvNumber(float)]) =>
Helpers.toDistFn(Truncate(Some(float), None), dist, ~env) Helpers.toDistFn(Truncate(Some(float), None), dist, ~env)

View File

@ -3,10 +3,6 @@ sidebar_position: 5
title: Sample Set Distribution title: Sample Set Distribution
--- ---
:::danger
These functions aren't yet implemented with these specific names. This should be added soon.
:::
Sample set distributions are one of the three distribution formats. Internally, they are stored as a list of numbers. It's useful to distinguish point set distributions from arbitrary lists of numbers to make it clear which functions are applicable. Sample set distributions are one of the three distribution formats. Internally, they are stored as a list of numbers. It's useful to distinguish point set distributions from arbitrary lists of numbers to make it clear which functions are applicable.
Monte Carlo calculations typically result in sample set distributions. Monte Carlo calculations typically result in sample set distributions.
@ -25,9 +21,8 @@ Sampleset.fromList: (list<number>) => sampleSet
### fromFn ### fromFn
(Not yet implemented)
``` ```
Sampleset.fromFn: (() => number) => sampleSet Sampleset.fromFn: ((float) => number) => sampleSet
``` ```
### toList ### toList
@ -61,3 +56,9 @@ Sampleset.map2: (sampleSet, sampleSet, ((number, number) => number)) => sampleSe
``` ```
Sampleset.map3: (sampleSet, sampleSet, sampleSet, ((number, number, number) => number)) => sampleSet Sampleset.map3: (sampleSet, sampleSet, sampleSet, ((number, number, number) => number)) => sampleSet
``` ```
### mapN
```
Sampleset.mapN: (list<sampleSet>, (list<sampleSet> => number)) => sampleSet
```