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:", () => { describe("expressions of normal distributions:", () => {
test("sum of two", () => { 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", () => { 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. which returns a JSON with (hopefully) a single-element array.
This array element is the top-level node of a nested-object tree This array element is the top-level node of a nested-object tree
representing the functions/arguments/values/etc. in the string. 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. 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. 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 type expressionValue = ReducerInterface_ExpressionValue.expressionValue
module Grammar = { module Grammar = {
type expressionValueOR = option< type expressionValueOR = option<result<expressionValue, Reducer_ErrorValue.errorValue>>
result<ReducerInterface_ExpressionValue.expressionValue, Reducer_ErrorValue.errorValue>,
>
let normalDist: t<expressionValueOR> = bind(Symbols.normalNode, _ => let normalDist: t<expressionValueOR> = bind(Symbols.normalNode, _ =>
bind(Symbols.openParen, _ => bind(Symbols.openParen, _ =>
bind(Primitive.natural, mean => bind(Primitive.natural, mean =>
@ -61,17 +59,16 @@ module Grammar = {
) )
and term: lazy_t<t<expressionValueOR>> = lazy bind(Lazy.force(factor), f => and term: lazy_t<t<expressionValueOR>> = lazy bind(Lazy.force(factor), f =>
choice( choice(
bind( bind(Primitive.symbol("*"), _ =>
Primitive.symbol("*"), bind(Lazy.force(term), t => {
_ => let f' = retGenericDistOrRaise(f)
bind(Lazy.force(term), t => { let t' = retGenericDistOrRaise(t)
let f' = retGenericDistOrRaise(f) returnP(dispatch(("multiply", [EvDistribution(f'), EvDistribution(t')])))
let t' = retGenericDistOrRaise(t) })
returnP(dispatch(("multiply", [EvDistribution(f'), EvDistribution(t')])))
})),
returnP(f),
), ),
returnP(f),
) )
)
and factor: lazy_t<t<expressionValueOR>> = lazy choice( and factor: lazy_t<t<expressionValueOR>> = lazy choice(
bind(Symbols.openParen, _ => bind(Symbols.openParen, _ =>
bind(Lazy.force(expr), e => bind(Symbols.closeParen, _ => returnP(e))) bind(Lazy.force(expr), e => bind(Symbols.closeParen, _ => returnP(e)))