Fixed major bug for module functions

This commit is contained in:
Ozzie Gooen 2022-07-19 23:06:10 -07:00
parent 90d3f89c0a
commit 9173b103f5
7 changed files with 94 additions and 50 deletions

View File

@ -59,6 +59,7 @@ describe("FunctionRegistry Library", () => {
) )
testEvalToBe("Dist.logScore({estimate: normal(5,2), answer: 4.5})", "Ok(1.6433360626394853)") testEvalToBe("Dist.logScore({estimate: normal(5,2), answer: 4.5})", "Ok(1.6433360626394853)")
testEvalToBe("Dist.klDivergence(normal(5,2), normal(5,1.5))", "Ok(0.06874342818671068)") testEvalToBe("Dist.klDivergence(normal(5,2), normal(5,1.5))", "Ok(0.06874342818671068)")
testEvalToBe("Sampleset.fromList([3,5,2,3,5,2,3,5,2,3,3,5])", "Ok(Sample Set Distribution)")
}) })
describe("Fn auto-testing", () => { describe("Fn auto-testing", () => {

View File

@ -15,6 +15,7 @@
"start": "rescript build -w -with-deps", "start": "rescript build -w -with-deps",
"clean": "rescript clean && rm -rf dist", "clean": "rescript clean && rm -rf dist",
"test:reducer": "jest __tests__/Reducer*/", "test:reducer": "jest __tests__/Reducer*/",
"test:current": "jest __tests__/SquiggleLibrary/SquiggleLibrary_FunctionRegistryLibrary_test.bs.js",
"benchmark": "ts-node benchmark/conversion_tests.ts", "benchmark": "ts-node benchmark/conversion_tests.ts",
"test": "jest", "test": "jest",
"test:ts": "jest __tests__/TS/", "test:ts": "jest __tests__/TS/",

View File

@ -170,6 +170,7 @@ 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
@ -240,7 +241,15 @@ module Matcher = {
type definitionId = int type definitionId = int
type match = Match.t<array<definitionId>, definitionId> type match = Match.t<array<definitionId>, definitionId>
let match = (f: function, fnName: string, args: array<internalExpressionValue>): match => { let match = (
f: function,
namespace: option<string>,
fnName: string,
args: array<internalExpressionValue>,
): match => {
switch namespace {
| Some(ns) if ns !== f.nameSpace => Match.DifferentName
| _ => {
let matchedDefinition = () => let matchedDefinition = () =>
E.A.getIndexBy(f.definitions, r => E.A.getIndexBy(f.definitions, r =>
MatchSimple.isFullMatch(FnDefinition.match(r, fnName, args)) MatchSimple.isFullMatch(FnDefinition.match(r, fnName, args))
@ -249,7 +258,9 @@ module Matcher = {
let nameMatchIndexes = let nameMatchIndexes =
f.definitions f.definitions
->E.A2.fmapi((index, r) => ->E.A2.fmapi((index, r) =>
MatchSimple.isNameMatchOnly(FnDefinition.match(r, fnName, args)) ? Some(index) : None MatchSimple.isNameMatchOnly(FnDefinition.match(r, fnName, args))
? Some(index)
: None
) )
->E.A.O.concatSomes ->E.A.O.concatSomes
switch nameMatchIndexes { switch nameMatchIndexes {
@ -264,29 +275,48 @@ module Matcher = {
) )
} }
} }
}
}
module RegistryMatch = { module RegistryMatch = {
type match = { type match = {
namespace: option<string>,
fnName: string, fnName: string,
inputIndex: int, inputIndex: int,
} }
let makeMatch = (fnName: string, inputIndex: int) => {fnName: fnName, inputIndex: inputIndex} let makeMatch = (namespace: option<string>, fnName: string, inputIndex: int) => {
namespace: namespace,
fnName: fnName,
inputIndex: inputIndex,
}
} }
module Registry = { module Registry = {
let _findExactMatches = (r: registry, fnName: string, args: array<internalExpressionValue>) => { let _findExactMatches = (
let functionMatchPairs = r.functions->E.A2.fmap(l => (l, Function.match(l, fnName, args))) r: registry,
namespace: option<string>,
fnName: string,
args: array<internalExpressionValue>,
) => {
let functionMatchPairs =
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 {
| FullMatch(index) => Some(RegistryMatch.makeMatch(fn.name, index)) | FullMatch(index) => Some(RegistryMatch.makeMatch(namespace, fn.name, index))
| _ => None | _ => None
} }
) )
} }
let _findNameMatches = (r: registry, fnName: string, args: array<internalExpressionValue>) => { let _findNameMatches = (
let functionMatchPairs = r.functions->E.A2.fmap(l => (l, Function.match(l, fnName, args))) r: registry,
namespace: option<string>,
fnName: string,
args: array<internalExpressionValue>,
) => {
let functionMatchPairs =
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)
@ -296,7 +326,7 @@ module Matcher = {
->E.A2.fmap(((fn, match)) => ->E.A2.fmap(((fn, match)) =>
switch match { switch match {
| SameNameDifferentArguments(indexes) => | SameNameDifferentArguments(indexes) =>
indexes->E.A2.fmap(index => RegistryMatch.makeMatch(fn.name, index)) indexes->E.A2.fmap(index => RegistryMatch.makeMatch(namespace, fn.name, index))
| _ => [] | _ => []
} }
) )
@ -307,22 +337,29 @@ module Matcher = {
let findMatches = (r: registry, fnName: string, args: array<internalExpressionValue>) => { let findMatches = (r: registry, fnName: string, args: array<internalExpressionValue>) => {
let fnNameInParts = Js.String.split(".", fnName) let fnNameInParts = Js.String.split(".", fnName)
let fnToSearch = E.A.get(fnNameInParts, 1) |> E.O.default(fnNameInParts[0]) let fnToSearch = E.A.get(fnNameInParts, 1) |> E.O.default(fnNameInParts[0])
let nameSpace = E.A.length(fnNameInParts) > 1 ? Some(fnNameInParts[0]) : None
switch _findExactMatches(r, fnToSearch, args) { switch _findExactMatches(r, nameSpace, fnToSearch, args) {
| Some(r) => Match.FullMatch(r) | Some(r) => Match.FullMatch(r)
| None => | None =>
switch _findNameMatches(r, fnToSearch, args) { switch _findNameMatches(r, nameSpace, fnToSearch, args) {
| Some(r) => Match.SameNameDifferentArguments(r) | Some(r) => Match.SameNameDifferentArguments(r)
| None => Match.DifferentName | None => Match.DifferentName
} }
} }
} }
let matchToDef = (registry: registry, {fnName, inputIndex}: RegistryMatch.match): option< let matchToDef = (
fnDefinition, registry: registry,
> => {namespace, fnName, inputIndex}: RegistryMatch.match,
): option<fnDefinition> =>
registry.functions registry.functions
->E.A.getBy(fn => fn.name === fnName) ->E.A.getBy(fn => {
switch namespace {
| Some(ns) => ns === fn.nameSpace && fnName === fn.name
| _ => fnName === fn.name
}
})
->E.O.bind(fn => E.A.get(fn.definitions, inputIndex)) ->E.O.bind(fn => E.A.get(fn.definitions, inputIndex))
} }
} }
@ -474,6 +511,8 @@ 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)
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

@ -2,10 +2,10 @@ let fnList = Belt.Array.concatMany([
FR_Dict.library, FR_Dict.library,
FR_Dist.library, FR_Dist.library,
FR_Fn.library, FR_Fn.library,
FR_Sampleset.library,
FR_List.library, FR_List.library,
FR_Number.library, FR_Number.library,
FR_Pointset.library, FR_Pointset.library,
FR_Sampleset.library,
FR_Scoring.library, FR_Scoring.library,
]) ])

View File

@ -156,10 +156,11 @@ 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

@ -74,7 +74,7 @@ let library = [
~name="fromDist", ~name="fromDist",
~nameSpace, ~nameSpace,
~requiresNamespace=true, ~requiresNamespace=true,
~examples=[`PointSet.fromDist(normal(5,2))`], ~examples=[`Pointset.fromDist(normal(5,2))`],
~output=ReducerInterface_InternalExpressionValue.EvtDistribution, ~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[ ~definitions=[
FnDefinition.make( FnDefinition.make(

View File

@ -90,19 +90,19 @@ let library = [
(), (),
), ),
Function.make( Function.make(
~name="fromLlist", ~name="fromList",
~nameSpace, ~nameSpace,
~requiresNamespace=true, ~requiresNamespace=true,
~examples=[`Sampleset.fromLlist([3,5,2,3,5,2,3,5,2,3,3,5,3,2,3,1,1,3])`], ~examples=[`Sampleset.fromList([3,5,2,3,5,2,3,5,2,3,3,5,3,2,3,1,1,3])`],
~output=ReducerInterface_InternalExpressionValue.EvtDistribution, ~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[ ~definitions=[
FnDefinition.make( FnDefinition.make(
~name="fromLlist", ~name="fromList",
~inputs=[FRTypeArray(FRTypeNumber)], ~inputs=[FRTypeArray(FRTypeNumber)],
~run=(_, inputs, _, _) => { ~run=(_, inputs, _, _) => {
let sampleSet = let sampleSet =
Prepare.ToTypedArray.numbers(inputs) |> E.R2.bind(r => Prepare.ToTypedArray.numbers(inputs) |> E.R2.bind(r =>
SampleSetDist.make(r)->E.R2.errMap(_ => "") SampleSetDist.make(r)->E.R2.errMap(_ => "AM I HERE? WHYERE AMI??")
) )
sampleSet->E.R2.fmap(Wrappers.sampleSet)->E.R2.fmap(Wrappers.evDistribution) sampleSet->E.R2.fmap(Wrappers.sampleSet)->E.R2.fmap(Wrappers.evDistribution)
}, },
@ -112,14 +112,14 @@ let library = [
(), (),
), ),
Function.make( Function.make(
~name="toLlist", ~name="toList",
~nameSpace, ~nameSpace,
~requiresNamespace=false, ~requiresNamespace=false,
~examples=[`Sampleset.toLlist(Sampleset.maker(normal(5,2))`], ~examples=[`Sampleset.toList(Sampleset.fromDist(normal(5,2)))`],
~output=ReducerInterface_InternalExpressionValue.EvtArray, ~output=ReducerInterface_InternalExpressionValue.EvtArray,
~definitions=[ ~definitions=[
FnDefinition.make( FnDefinition.make(
~name="toLlist", ~name="toList",
~inputs=[FRTypeDist], ~inputs=[FRTypeDist],
~run=(inputs, _, _, _) => ~run=(inputs, _, _, _) =>
switch inputs { switch inputs {
@ -133,14 +133,14 @@ let library = [
(), (),
), ),
Function.make( Function.make(
~name="mapp", ~name="map",
~nameSpace, ~nameSpace,
~requiresNamespace, ~requiresNamespace,
~examples=[`Sampleset.mapp(Sampleset.maker(normal(5,2)), {|x| x + 1})`], ~examples=[`Sampleset.map(Sampleset.fromDist(normal(5,2)), {|x| x + 1})`],
~output=ReducerInterface_InternalExpressionValue.EvtDistribution, ~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[ ~definitions=[
FnDefinition.make( FnDefinition.make(
~name="mapp", ~name="map",
~inputs=[FRTypeDist, FRTypeLambda], ~inputs=[FRTypeDist, FRTypeLambda],
~run=(inputs, _, env, reducer) => ~run=(inputs, _, env, reducer) =>
switch inputs { switch inputs {
@ -158,7 +158,7 @@ let library = [
~nameSpace, ~nameSpace,
~requiresNamespace, ~requiresNamespace,
~examples=[ ~examples=[
`Sampleset.map2(Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2)), {|x, y| x + y})`, `Sampleset.map2(Sampleset.fromDist(normal(5,2)), Sampleset.fromDist(normal(5,2)), {|x, y| x + y})`,
], ],
~output=ReducerInterface_InternalExpressionValue.EvtDistribution, ~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[ ~definitions=[
@ -186,7 +186,7 @@ let library = [
~nameSpace, ~nameSpace,
~requiresNamespace, ~requiresNamespace,
~examples=[ ~examples=[
`Sampleset.map3(Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2)), {|x, y, z| max([x,y,z]))`, `Sampleset.map3(Sampleset.fromDist(normal(5,2)), Sampleset.fromDist(normal(5,2)), Sampleset.fromDist(normal(5,2)), {|x, y, z| max([x,y,z])})`,
], ],
~output=ReducerInterface_InternalExpressionValue.EvtDistribution, ~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[ ~definitions=[
@ -214,7 +214,7 @@ let library = [
~nameSpace, ~nameSpace,
~requiresNamespace, ~requiresNamespace,
~examples=[ ~examples=[
`Sampleset.mapN([Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2)), Sampleset.maker(normal(5,2))], {|x| max(x)})`, `Sampleset.mapN([Sampleset.fromDist(normal(5,2)), Sampleset.fromDist(normal(5,2)), Sampleset.fromDist(normal(5,2))], {|x| max(x)})`,
], ],
~output=ReducerInterface_InternalExpressionValue.EvtDistribution, ~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[ ~definitions=[
@ -224,7 +224,9 @@ let library = [
~run=(inputs, _, env, reducer) => ~run=(inputs, _, env, reducer) =>
switch inputs { switch inputs {
| [IEvArray(dists), IEvLambda(lambda)] => | [IEvArray(dists), IEvLambda(lambda)] =>
Internal.mapN(dists, lambda, env, reducer)->E.R2.errMap(e => {Js.log2("HI", e); "AHHH doesn't work"}) Internal.mapN(dists, lambda, env, reducer)->E.R2.errMap(e => {
"AHHH doesn't work"
})
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),