From 86e3d5865e033fa0ddc64d5bc1ac5fab5077394b Mon Sep 17 00:00:00 2001 From: Sam Nolan Date: Sat, 23 Apr 2022 15:02:44 -0400 Subject: [PATCH 1/3] Change components to return result rather than environment --- packages/components/.prettierignore | 2 ++ .../components/src/components/SquiggleChart.tsx | 7 +++---- .../components/src/components/SquiggleEditor.tsx | 13 +++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 packages/components/.prettierignore diff --git a/packages/components/.prettierignore b/packages/components/.prettierignore new file mode 100644 index 00000000..5e03c80e --- /dev/null +++ b/packages/components/.prettierignore @@ -0,0 +1,2 @@ +dist/ +storybook-static diff --git a/packages/components/src/components/SquiggleChart.tsx b/packages/components/src/components/SquiggleChart.tsx index 9553d329..534d164d 100644 --- a/packages/components/src/components/SquiggleChart.tsx +++ b/packages/components/src/components/SquiggleChart.tsx @@ -131,7 +131,7 @@ export interface SquiggleChartProps { /** variables declared before this expression */ environment?: unknown; /** When the environment changes */ - onEnvChange?(env: unknown): void; + onChange?(expr: squiggleExpression): void; /** CSS width of the element */ width?: number; height?: number; @@ -141,8 +141,7 @@ export const SquiggleChart: React.FC = ({ squiggleString = "", sampleCount = 1000, outputXYPoints = 1000, - environment = [], - onEnvChange = () => {}, + onChange = () => {}, height = 60, width = NaN, }: SquiggleChartProps) => { @@ -158,8 +157,8 @@ export const SquiggleChart: React.FC = ({ let expressionResult = run(squiggleString, samplingInputs); let internal: JSX.Element; if (expressionResult.tag === "Ok") { - onEnvChange(environment); let expression = expressionResult.value; + onChange(expression); internal = ( ); diff --git a/packages/components/src/components/SquiggleEditor.tsx b/packages/components/src/components/SquiggleEditor.tsx index 4ede7891..580db580 100644 --- a/packages/components/src/components/SquiggleEditor.tsx +++ b/packages/components/src/components/SquiggleEditor.tsx @@ -3,6 +3,7 @@ import * as ReactDOM from "react-dom"; import { SquiggleChart } from "./SquiggleChart"; import { CodeEditor } from "./CodeEditor"; import styled from "styled-components"; +import type { squiggleExpression } from "@quri/squiggle-lang"; export interface SquiggleEditorProps { /** The input string for squiggle */ @@ -22,7 +23,7 @@ export interface SquiggleEditorProps { /** The environment, other variables that were already declared */ environment?: unknown; /** when the environment changes. Used again for notebook magic*/ - onEnvChange?(env: unknown): void; + onChange?(expr: squiggleExpression): void; /** The width of the element */ width: number; } @@ -43,7 +44,7 @@ export let SquiggleEditor: React.FC = ({ diagramStart, diagramStop, diagramCount, - onEnvChange, + onChange, environment, }: SquiggleEditorProps) => { let [expression, setExpression] = React.useState(initialSquiggleString); @@ -69,7 +70,7 @@ export let SquiggleEditor: React.FC = ({ diagramStop={diagramStop} diagramCount={diagramCount} environment={environment} - onEnvChange={onEnvChange} + onChange={onChange} /> ); @@ -80,7 +81,7 @@ export function renderSquiggleEditorToDom(props: SquiggleEditorProps) { ReactDOM.render( { + onChange={(expr) => { // Typescript complains on two levels here. // - Div elements don't have a value property // - Even if it did (like it was an input element), it would have to @@ -96,10 +97,10 @@ export function renderSquiggleEditorToDom(props: SquiggleEditorProps) { // viewof env = cell('normal(0,1)') // to work // @ts-ignore - parent.value = env; + parent.value = expr; parent.dispatchEvent(new CustomEvent("input")); - if (props.onEnvChange) props.onEnvChange(env); + if (props.onChange) props.onChange(expr); }} />, parent From ee6551a6941e7e4b358ffe21ce7f594c4400e2f7 Mon Sep 17 00:00:00 2001 From: Sam Nolan Date: Sat, 23 Apr 2022 16:57:06 -0400 Subject: [PATCH 2/3] Prevent negative standard deviation in symbolic multiplication Introduced in #242 --- .../ReducerInterface/ReducerInterface_Distribution_test.res | 1 + .../rescript/Distributions/SymbolicDist/SymbolicDist.res | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res b/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res index d4199f89..605797b9 100644 --- a/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res +++ b/packages/squiggle-lang/__tests__/ReducerInterface/ReducerInterface_Distribution_test.res @@ -20,6 +20,7 @@ describe("eval on distribution functions", () => { }) describe("unaryMinus", () => { testEval("mean(-normal(5,2))", "Ok(-5)") + testEval("-normal(5,2)", "Ok(Normal(-5,2))") }) describe("to", () => { testEval("5 to 2", "Error(Math Error: Low value must be less than high value.)") diff --git a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res index 7ce95721..92249eae 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res +++ b/packages/squiggle-lang/src/rescript/Distributions/SymbolicDist/SymbolicDist.res @@ -52,7 +52,7 @@ module Normal = { switch operation { | #Add => Some(#Normal({mean: n1 +. n2.mean, stdev: n2.stdev})) | #Subtract => Some(#Normal({mean: n1 -. n2.mean, stdev: n2.stdev})) - | #Multiply => Some(#Normal({mean: n1 *. n2.mean, stdev: n1 *. n2.stdev})) + | #Multiply => Some(#Normal({mean: n1 *. n2.mean, stdev: Js.Math.abs_float(n1) *. n2.stdev})) | _ => None } @@ -60,8 +60,8 @@ module Normal = { switch operation { | #Add => Some(#Normal({mean: n1.mean +. n2, stdev: n1.stdev})) | #Subtract => Some(#Normal({mean: n1.mean -. n2, stdev: n1.stdev})) - | #Multiply => Some(#Normal({mean: n1.mean *. n2, stdev: n1.stdev *. n2})) - | #Divide => Some(#Normal({mean: n1.mean /. n2, stdev: n1.stdev /. n2})) + | #Multiply => Some(#Normal({mean: n1.mean *. n2, stdev: n1.stdev *. Js.Math.abs_float(n2)})) + | #Divide => Some(#Normal({mean: n1.mean /. n2, stdev: n1.stdev /. Js.Math.abs_float(n2)})) | _ => None } } From 8cb138b84aaf3ed2ecee5408ff1d7ad0ce1ad932 Mon Sep 17 00:00:00 2001 From: Sam Nolan Date: Sat, 23 Apr 2022 17:48:00 -0400 Subject: [PATCH 3/3] Fix convolution for negative multiplication --- .../PointSetDist/AlgebraicShapeCombination.res | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/AlgebraicShapeCombination.res b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/AlgebraicShapeCombination.res index fd7871cd..d6ff1d31 100644 --- a/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/AlgebraicShapeCombination.res +++ b/packages/squiggle-lang/src/rescript/Distributions/PointSetDist/AlgebraicShapeCombination.res @@ -230,13 +230,12 @@ let combineShapesContinuousDiscrete = ( i, ( fn(continuousShape.xs[i], discreteShape.xs[j]), - continuousShape.ys[i] *. discreteShape.ys[j] /. discreteShape.xs[j], + continuousShape.ys[i] *. discreteShape.ys[j] /. Js.Math.abs_float(discreteShape.xs[j]), ), ) |> ignore () } Belt.Array.set(outXYShapes, j, dxyShape) |> ignore - () } } @@ -244,12 +243,11 @@ let combineShapesContinuousDiscrete = ( |> E.A.fmap(XYShape.T.fromZippedArray) |> E.A.fold_left( (acc, x) => - XYShape.PointwiseCombination.combine( - (a, b) => Ok(a +. b), + XYShape.PointwiseCombination.addCombine( XYShape.XtoY.continuousInterpolator(#Linear, #UseZero), acc, x, - )->E.R.toExn("Error, unexpected failure", _), + ), XYShape.T.empty, ) }