added test; fixed lint

This commit is contained in:
Quinn Dougherty 2022-04-21 22:54:25 -04:00
parent 6f9d083993
commit 60223156a8
3 changed files with 34 additions and 15 deletions

View File

@ -4,9 +4,32 @@ let {eval} = module(Parser_Squiggle)
describe("expressions of normal distributions:", () => {
test("sum of two", () => {
expect(eval(" normal (5 , 2 ) + normal(0,2)")) -> toEqual({mean: 5.0 +. 0.0, stdev: Js.Math.sqrt(2.0 ** 2.0 +. 2.0 ** 2.0)} -> #Normal -> Symbolic -> EvDistribution -> Ok -> Some)
expect(eval(" normal (5 , 2 ) + normal(0,2)"))->toEqual(
{mean: 5.0 +. 0.0, stdev: Js.Math.sqrt(2.0 ** 2.0 +. 2.0 ** 2.0)}
->#Normal
->Symbolic
->EvDistribution
->Ok
->Some,
)
})
test("difference of two", () => {
expect(eval("normal(5,3)-normal(2,1)")) -> toEqual({mean: 5.0 -. 2.0, stdev: Js.Math.sqrt(3.0 ** 2.0 +. 1.0 ** 2.0)} -> #Normal -> Symbolic -> EvDistribution -> Ok -> Some)
expect(eval("normal(5,3)-normal(2,1)"))->toEqual(
{mean: 5.0 -. 2.0, stdev: Js.Math.sqrt(3.0 ** 2.0 +. 1.0 ** 2.0)}
->#Normal
->Symbolic
->EvDistribution
->Ok
->Some,
)
})
test("product of two", () => {
let item = eval("normal ( 5 , 3 ) * normal(2,1)")
exception NotOk(string)
let item' = switch item {
| Some(Ok(EvDistribution(SampleSet(x)))) => x
| _ => raise(NotOk("something happened"))
}
expect(Js.Array.length(item'))->toEqual(10000)
})
})

View File

@ -270,7 +270,6 @@ let fromString2 = str => {
which returns a JSON with (hopefully) a single-element array.
This array element is the top-level node of a nested-object tree
representing the functions/arguments/values/etc. in the string.
The function MathJsonToMathJsAdt then recursively unpacks this JSON into a typed data structure we can use.
Inside of this function, MathAdtToDistDst is called whenever a distribution function is encountered.
*/

View File

@ -3,9 +3,7 @@ let {dispatch} = module(ReducerInterface_GenericDistribution)
type expressionValue = ReducerInterface_ExpressionValue.expressionValue
module Grammar = {
type expressionValueOR = option<
result<ReducerInterface_ExpressionValue.expressionValue, Reducer_ErrorValue.errorValue>,
>
type expressionValueOR = option<result<expressionValue, Reducer_ErrorValue.errorValue>>
let normalDist: t<expressionValueOR> = bind(Symbols.normalNode, _ =>
bind(Symbols.openParen, _ =>
bind(Primitive.natural, mean =>
@ -61,17 +59,16 @@ module Grammar = {
)
and term: lazy_t<t<expressionValueOR>> = lazy bind(Lazy.force(factor), f =>
choice(
bind(
Primitive.symbol("*"),
_ =>
bind(Lazy.force(term), t => {
let f' = retGenericDistOrRaise(f)
let t' = retGenericDistOrRaise(t)
returnP(dispatch(("multiply", [EvDistribution(f'), EvDistribution(t')])))
})),
returnP(f),
bind(Primitive.symbol("*"), _ =>
bind(Lazy.force(term), t => {
let f' = retGenericDistOrRaise(f)
let t' = retGenericDistOrRaise(t)
returnP(dispatch(("multiply", [EvDistribution(f'), EvDistribution(t')])))
})
),
returnP(f),
)
)
and factor: lazy_t<t<expressionValueOR>> = lazy choice(
bind(Symbols.openParen, _ =>
bind(Lazy.force(expr), e => bind(Symbols.closeParen, _ => returnP(e)))