Fixed bug caused by bad genType format of Declaration

This commit is contained in:
Ozzie Gooen 2022-05-25 19:27:15 -04:00
parent 3404534b10
commit 298d3923bc
7 changed files with 68 additions and 61 deletions

View File

@ -11,14 +11,15 @@ import {
defaultImports,
defaultBindings,
defaultEnvironment,
continuousDeclaration,
declarationArg,
declaration
} from "@quri/squiggle-lang";
import { NumberShower } from "./NumberShower";
import { DistributionChart } from "./DistributionChart";
import { ErrorBox } from "./ErrorBox";
import { FunctionChart, FunctionChartSettings } from "./FunctionChart";
function getRange<a>(x: continuousDeclaration<a>) {
function getRange<a>(x: declaration<a>) {
let first = x.args[0];
switch (first.tag) {
case "Float": {
@ -30,7 +31,7 @@ function getRange<a>(x: continuousDeclaration<a>) {
}
}
function getChartSettings<a>(
x: continuousDeclaration<a>
x: declaration<a>
): FunctionChartSettings {
let range = getRange(x);
let min = range.floats ? range.floats.min : 0;
@ -240,27 +241,17 @@ const SquiggleItem: React.FC<SquiggleItemProps> = ({
/>
);
case "lambdaDeclaration": {
switch (expression.value.tag) {
case "Continuous": {
return (
<FunctionChart
fn={expression.value.value.fn}
chartSettings={getChartSettings(expression.value.value)}
fn={expression.value.fn}
chartSettings={getChartSettings(expression.value)}
environment={{
sampleCount: environment.sampleCount / 10,
xyPointLength: environment.xyPointLength / 10,
}}
/>
);
}
case "RelativeComparison": {
return <>"Relative"</>;
}
default: {
return <>LambdaDeclaration: Should be unreachable</>;
}
}
}
default: {
return <>Should be unreachable</>;
}

View File

@ -14,9 +14,8 @@ export {
errorValueToString,
distributionErrorToString,
distributionError,
continuousDeclaration,
relativeComparisonDeclaration,
declarationContinuousArg,
declarationArg,
declaration,
} from "../rescript/TypescriptInterface.gen";
export type { errorValue, externalBindings as bindings, jsImports };
import {

View File

@ -9,7 +9,8 @@ import {
discreteShape,
continuousShape,
lambdaValue,
lambdaDeclaration
lambdaDeclaration,
declarationArg,
} from "../rescript/TypescriptInterface.gen";
import { Distribution } from "./distribution";
import { tagged, tag } from "./types";
@ -67,7 +68,7 @@ export type rescriptExport =
}
| {
TAG: 12; // EvDeclaration
_0: lambdaDeclaration;
_0: rescriptLambdaDeclaration;
};
type rescriptDist =
@ -89,6 +90,23 @@ type rescriptPointSetDist =
_0: continuousShape;
};
type rescriptLambdaDeclaration = {
readonly fn: lambdaValue;
readonly args: rescriptDeclarationArg[];
};
type rescriptDeclarationArg =
| {
TAG: 0; // Float
min: number;
max: number;
}
| {
TAG: 1; // Float
min: Date;
max: Date;
};
export type squiggleExpression =
| tagged<"symbol", string>
| tagged<"string", string>
@ -148,7 +166,21 @@ export function convertRawToTypescript(
case 11: // EvTimeDuration
return tag("number", result._0);
case 12: // EvDeclaration
return tag("lambdaDeclaration", result._0);
return tag("lambdaDeclaration", {
fn: result._0.fn,
args: result._0.args.map(convertDeclaration),
});
}
}
function convertDeclaration(
declarationArg: rescriptDeclarationArg
): declarationArg {
switch (declarationArg.TAG) {
case 0: // Float
return tag("Float", { min: declarationArg.min, max: declarationArg.max });
case 1: // Date
return tag("Date", { min: declarationArg.min, max: declarationArg.max });
}
}

View File

@ -78,9 +78,9 @@ module FRType = {
| (FRTypeOption(v), _) => Some(FRValueOption(matchWithExpressionValue(v, r)))
| (FRTypeLambda, EvLambda(f)) => Some(FRValueLambda(f))
| (FRTypeArray(intendedType), EvArray(elements)) => {
let el = elements->E.A2.fmap(matchWithExpressionValue(intendedType))
E.A.O.openIfAllSome(el)->E.O2.fmap(r => FRValueArray(r))
}
let el = elements->E.A2.fmap(matchWithExpressionValue(intendedType))
E.A.O.openIfAllSome(el)->E.O2.fmap(r => FRValueArray(r))
}
| (FRTypeRecord(recordParams), EvRecord(record)) => {
let getAndMatch = (name, input) =>
E.Dict.get(record, name)->E.O.bind(matchWithExpressionValue(input))
@ -96,23 +96,24 @@ module FRType = {
}
let rec matchReverse = (e: frValue): expressionValue =>
switch(e){
| FRValueNumber(f) => (EvNumber(f))
switch e {
| FRValueNumber(f) => EvNumber(f)
| FRValueDistOrNumber(FRValueNumber(n)) => EvNumber(n)
| FRValueDistOrNumber(FRValueDist(n)) => EvDistribution(n)
| FRValueDist(dist) => EvDistribution(dist)
| FRValueOption(Some(r)) => matchReverse(r)
| FRValueArray(elements) => EvArray(elements->E.A2.fmap(matchReverse))
| FRValueRecord(frValueRecord) => {
let record = frValueRecord->E.A2.fmap(((name, value)) => (name, matchReverse(value)))->E.Dict.fromArray
EvRecord(record)
}
let record =
frValueRecord->E.A2.fmap(((name, value)) => (name, matchReverse(value)))->E.Dict.fromArray
EvRecord(record)
}
| FRValueLambda(l) => EvLambda(l)
| FRValueString(string) => EvString(string)
| FRValueVariant(string) => EvString(string)
}
}
// | FRValueOption(None) => break
// | FRValueOption(None) => break
let matchWithExpressionValueArray = (inputs: array<t>, args: array<expressionValue>): option<
array<frValue>,
> => {

View File

@ -21,7 +21,7 @@ module Declaration = {
->E.A2.fmap(getMinMax)
->E.A.R.firstErrorOrOpen
->E.R2.fmap(args => ReducerInterface_ExpressionValue.EvDeclaration(
Declaration.ContinuousDeclaration.make(lambda, args),
Declaration.make(lambda, args),
))
}
| _ => Error("Error")
@ -35,8 +35,7 @@ let registry = [
~definitions=[
FnDefinition.make(~name="declareFn", ~inputs=[Declaration.frType], ~run=(inputs, _) => {
inputs->E.A.unsafe_get(0)->Declaration.fromExpressionValue
}
),
}),
],
),
Function.make(

View File

@ -92,13 +92,7 @@ let defaultEnvironment = ReducerInterface_ExpressionValue.defaultEnvironment
let foreignFunctionInterface = Reducer.foreignFunctionInterface
@genType
type declarationContinuousArg = Declaration.continuousArg
@genType
type continuousDeclaration<'a> = Declaration.continuousDeclaration<'a>
@genType
type relativeComparisonDeclaration<'a> = Declaration.relativeComparisonDeclaration<'a>
type declarationArg = Declaration.arg
@genType
type declaration<'a> = Declaration.declaration<'a>

View File

@ -1,33 +1,24 @@
@genType
type continuousArg = Float({min: float, max: float}) | Time({min: Js.Date.t, max: Js.Date.t})
type arg = Float({min: float, max: float}) | Date({min: Js.Date.t, max: Js.Date.t})
@genType
type continuousDeclaration<'a> = {fn: 'a, args: array<continuousArg>}
@genType
type relativeComparisonDeclaration<'a> = {fn: 'a, options: array<string>}
@genType
type declaration<'a> =
Continuous(continuousDeclaration<'a>) | RelativeComparison(relativeComparisonDeclaration<'a>)
type declaration<'a> = {
fn: 'a,
args: array<arg>,
}
module ContinuousFloatArg = {
let make = (min: float, max: float): continuousArg => {
let make = (min: float, max: float): arg => {
Float({min: min, max: max})
}
}
module ContinuousTimeArg = {
let make = (min: Js.Date.t, max: Js.Date.t): continuousArg => {
Time({min: min, max: max})
let make = (min: Js.Date.t, max: Js.Date.t): arg => {
Date({min: min, max: max})
}
}
module ContinuousDeclaration = {
let make = (fn: 'a, args: array<continuousArg>): declaration<'a> => {
Continuous({fn: fn, args: args})
}
}
module RelativeComparisonDeclaration = {
let make = (fn: 'a, options: array<string>): declaration<'a> => {
RelativeComparison({fn: fn, options: options})
}
let make = (fn: 'a, args: array<arg>): declaration<'a> => {
{fn: fn, args: args}
}