diff --git a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res index a7e98163..2418b4d8 100644 --- a/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res +++ b/packages/squiggle-lang/__tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.res @@ -68,7 +68,7 @@ describe("FunctionRegistry Library", () => { "Ok([2,3,4,5,6,7])", ) 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])", ) }) diff --git a/packages/squiggle-lang/package.json b/packages/squiggle-lang/package.json index 52c9f07c..e12b363c 100644 --- a/packages/squiggle-lang/package.json +++ b/packages/squiggle-lang/package.json @@ -17,10 +17,10 @@ "test:reducer": "jest __tests__/Reducer*/", "benchmark": "ts-node benchmark/conversion_tests.ts", "test": "jest", - "test:lib": "jest __tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.bs.js", "test:ts": "jest __tests__/TS/", "test:rescript": "jest --modulePathIgnorePatterns=__tests__/TS/*", "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: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", diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res index f4864531..4b27c221 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/FunctionRegistry_Core.res @@ -170,7 +170,6 @@ module FRType = { inputs: array, args: array, ): option> => { - // Js.log3("Matching", inputs, args) let isSameLength = E.A.length(inputs) == E.A.length(args) if !isSameLength { None @@ -186,6 +185,9 @@ module FRType = { 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. 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 MatchSimple = { @@ -243,11 +245,11 @@ module Matcher = { let match = ( f: function, - namespace: option, + nameSpace: option, fnName: string, args: array, ): match => { - switch namespace { + switch nameSpace { | Some(ns) if ns !== f.nameSpace => Match.DifferentName | _ => { let matchedDefinition = () => @@ -280,12 +282,12 @@ module Matcher = { module RegistryMatch = { type match = { - namespace: string, + nameSpace: string, fnName: string, inputIndex: int, } - let makeMatch = (namespace: string, fnName: string, inputIndex: int) => { - namespace: namespace, + let makeMatch = (nameSpace: string, fnName: string, inputIndex: int) => { + nameSpace: nameSpace, fnName: fnName, inputIndex: inputIndex, } @@ -294,12 +296,12 @@ module Matcher = { module Registry = { let _findExactMatches = ( r: registry, - namespace: option, + nameSpace: option, fnName: string, args: array, ) => { 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)) fullMatch->E.O.bind(((fn, match)) => switch match { @@ -311,12 +313,12 @@ module Matcher = { let _findNameMatches = ( r: registry, - namespace: option, + nameSpace: option, fnName: string, args: array, ) => { 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 = functionMatchPairs ->E.A2.fmap(((fn, match)) => Match.isNameMatchOnly(match) ? Some((fn, match)) : None) @@ -351,11 +353,11 @@ module Matcher = { let matchToDef = ( registry: registry, - {namespace, fnName, inputIndex}: RegistryMatch.match, + {nameSpace, fnName, inputIndex}: RegistryMatch.match, ): option => registry.functions ->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)) } @@ -508,8 +510,6 @@ module Registry = { `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) { | Matcher.Match.FullMatch(match) => match->matchToDef->E.O2.fmap(FnDefinition.run(_, args, env, reducer)) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dict.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dict.res index 10e84f92..0c85bbe1 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dict.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Dict.res @@ -156,7 +156,7 @@ let library = [ FnDefinition.make( ~name="fromList", ~inputs=[FRTypeArray(FRTypeArray(FRTypeAny))], - ~run=(inputs, _, _, _) =>{ + ~run=(inputs, _, _, _) => { switch inputs { | [IEvArray(items)] => Internals.fromList(items) | _ => Error(impossibleError) diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res index b2927b82..9995c3ef 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Sampleset.res @@ -28,8 +28,8 @@ module Internal = { reducer, ) => { let sampleCount = env.sampleCount - let fn = (r) => doLambdaCall(aLambdaValue, list{IEvNumber(r)}, env, reducer) - Belt_Array.makeBy(sampleCount, (r) => fn(r->Js.Int.toFloat))->E.A.R.firstErrorOrOpen + let fn = r => doLambdaCall(aLambdaValue, list{IEvNumber(r)}, env, reducer) + Belt_Array.makeBy(sampleCount, r => fn(r->Js.Int.toFloat))->E.A.R.firstErrorOrOpen } let map1 = (sampleSetDist: t, aLambdaValue, env, reducer) => { @@ -125,7 +125,7 @@ let library = [ Function.make( ~name="toList", ~nameSpace, - ~requiresNamespace=false, + ~requiresNamespace=true, ~examples=[`SampleSet.toList(SampleSet.fromDist(normal(5,2)))`], ~output=ReducerInterface_InternalExpressionValue.EvtArray, ~definitions=[ @@ -146,9 +146,9 @@ let library = [ Function.make( ~name="fromFn", ~nameSpace, - ~requiresNamespace=false, - ~examples=[`SampleSet.fromFn(sample(normal(5,2)))`], - ~output=ReducerInterface_InternalExpressionValue.EvtArray, + ~requiresNamespace=true, + ~examples=[`SampleSet.fromFn({|| sample(normal(5,2))})`], + ~output=ReducerInterface_InternalExpressionValue.EvtDistribution, ~definitions=[ FnDefinition.make( ~name="fromFn", diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res index a2e66298..dbec93c1 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res @@ -96,7 +96,6 @@ let callInternal = (call: functionCall, environment, reducer: ExpressionT.reduce let doExportBindings = (bindings: nameSpace) => bindings->Bindings.toExpressionValue->Ok module SampleMap = { - type t = SampleSetDist.t let doLambdaCall = (aLambdaValue, list) => switch Lambda.doLambdaCall(aLambdaValue, list, environment, reducer) { | Ok(IEvNumber(f)) => Ok(f) diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res index a65edf2f..addd4388 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_GenericDistribution.res @@ -233,19 +233,6 @@ let dispatchToGenericOutput = (call: IEV.functionCall, env: GenericDist.env): op | ("inv", [IEvDistribution(dist), IEvNumber(float)]) => Helpers.toFloatFn(#Inv(float), dist, ~env) | ("quantile", [IEvDistribution(dist), IEvNumber(float)]) => 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) | ("truncateLeft", [IEvDistribution(dist), IEvNumber(float)]) => Helpers.toDistFn(Truncate(Some(float), None), dist, ~env) diff --git a/packages/website/docs/Api/DistSampleSet.md b/packages/website/docs/Api/DistSampleSet.md index 2796a778..68e2e7a8 100644 --- a/packages/website/docs/Api/DistSampleSet.md +++ b/packages/website/docs/Api/DistSampleSet.md @@ -3,10 +3,6 @@ sidebar_position: 5 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. Monte Carlo calculations typically result in sample set distributions. @@ -25,9 +21,8 @@ Sampleset.fromList: (list) => sampleSet ### fromFn -(Not yet implemented) ``` -Sampleset.fromFn: (() => number) => sampleSet +Sampleset.fromFn: ((float) => number) => sampleSet ``` ### toList @@ -61,3 +56,9 @@ Sampleset.map2: (sampleSet, sampleSet, ((number, number) => number)) => sampleSe ``` Sampleset.map3: (sampleSet, sampleSet, sampleSet, ((number, number, number) => number)) => sampleSet ``` + +### mapN + +``` +Sampleset.mapN: (list, (list => number)) => sampleSet +``` \ No newline at end of file