From 43635bd39b9fec955f074aaae58e2709a00034c6 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Sun, 18 Sep 2022 03:15:58 +0400 Subject: [PATCH] remove IEvArrayString, implement __result__, cleanups --- .../SquiggleViewer/ExpressionViewer.tsx | 42 +++++++-------- .../Reducer/Reducer_ternaryOperator_test.res | 2 +- .../__tests__/Reducer/Reducer_test.res | 1 + packages/squiggle-lang/src/js/SqRecord.ts | 9 +++- packages/squiggle-lang/src/js/SqValue.ts | 10 ---- .../ForTS_SquiggleValue.res | 11 ---- .../ForTS_SquiggleValue_Record.res | 3 ++ .../ForTS_SquiggleValue_tag.ts | 1 - .../FunctionRegistry/Library/FR_Builtin.res | 53 +++++++++++++++++++ .../Reducer_Dispatch_BuiltIn.res | 26 ++++----- .../Reducer_Expression_ExpressionBuilder.res | 2 - .../rescript/Reducer/Reducer_Namespace.res | 6 +-- .../src/rescript/Reducer/Reducer_T.res | 1 - ...ducerInterface_InternalExpressionValue.res | 9 ---- .../ReducerInterface_StdLib.res | 2 +- .../ReducerProject/ReducerProject.res | 20 +++++-- 16 files changed, 114 insertions(+), 84 deletions(-) diff --git a/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx b/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx index 1ef5e150..1ae2a7b0 100644 --- a/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx +++ b/packages/components/src/components/SquiggleViewer/ExpressionViewer.tsx @@ -152,12 +152,6 @@ export const ExpressionViewer: React.FC = ({ value, width }) => { // {() => value.value} // // ); - case SqValueTag.ArrayString: - return ( - - {() => value.value.map((r) => `"${r}"`).join(", ")} - - ); case SqValueTag.Date: return ( @@ -242,24 +236,24 @@ export const ExpressionViewer: React.FC = ({ value, width }) => { ); } - case SqValueTag.Module: { - return ( - - {(_) => - value.value - .entries() - .filter(([key, _]) => !key.match(/^(__result__)$/)) - .map(([key, r]) => ( - - )) - } - - ); - } + // case SqValueTag.Module: { + // return ( + // + // {(_) => + // value.value + // .entries() + // .filter(([key, _]) => !key.match(/^(__result__)$/)) + // .map(([key, r]) => ( + // + // )) + // } + // + // ); + // } case SqValueTag.Record: const plot = makePlot(value.value); if (plot) { diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res index c1af1d59..3f66649b 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_ternaryOperator_test.res @@ -4,7 +4,7 @@ open Reducer_TestHelpers describe("Parse ternary operator", () => { testParseToBe( "true ? 'YES' : 'NO'", - "Ok({(:$$_ternary_$$ true 'YES' 'NO')})", + "Ok(true ? ('YES') : ('NO'))", ) }) diff --git a/packages/squiggle-lang/__tests__/Reducer/Reducer_test.res b/packages/squiggle-lang/__tests__/Reducer/Reducer_test.res index f6b9ad77..769937de 100644 --- a/packages/squiggle-lang/__tests__/Reducer/Reducer_test.res +++ b/packages/squiggle-lang/__tests__/Reducer/Reducer_test.res @@ -11,6 +11,7 @@ describe("eval", () => { testEvalToBe("(1+2)*3", "Ok(9)") testEvalToBe("2>1", "Ok(true)") testEvalToBe("concat('a ', 'b')", "Ok('a b')") + testEvalToBe("concat([3,4], [5,6,7])", "Ok([3,4,5,6,7])") testEvalToBe("log(10)", "Ok(2.302585092994046)") testEvalToBe("cos(10)", "Ok(-0.8390715290764524)") // TODO more built ins diff --git a/packages/squiggle-lang/src/js/SqRecord.ts b/packages/squiggle-lang/src/js/SqRecord.ts index 6ce3823e..27949220 100644 --- a/packages/squiggle-lang/src/js/SqRecord.ts +++ b/packages/squiggle-lang/src/js/SqRecord.ts @@ -1,5 +1,5 @@ import * as RSRecord from "../rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Record.gen"; -import { wrapValue } from "./SqValue"; +import { SqRecordValue, wrapValue } from "./SqValue"; import { SqValueLocation } from "./SqValueLocation"; type T = RSRecord.squiggleValue_Record; @@ -16,4 +16,11 @@ export class SqRecord { toString() { return RSRecord.toString(this._value); } + + asValue() { + return new SqRecordValue( + RSRecord.toSquiggleValue(this._value), + this.location + ); + } } diff --git a/packages/squiggle-lang/src/js/SqValue.ts b/packages/squiggle-lang/src/js/SqValue.ts index 7207b859..c0781937 100644 --- a/packages/squiggle-lang/src/js/SqValue.ts +++ b/packages/squiggle-lang/src/js/SqValue.ts @@ -44,14 +44,6 @@ export class SqArrayValue extends SqAbstractValue { } } -export class SqArrayStringValue extends SqAbstractValue { - tag = Tag.ArrayString as const; - - get value() { - return this.valueMethod(RSValue.getArrayString); - } -} - export class SqBoolValue extends SqAbstractValue { tag = Tag.Bool as const; @@ -150,7 +142,6 @@ export class SqVoidValue extends SqAbstractValue { const tagToClass = { [Tag.Array]: SqArrayValue, - [Tag.ArrayString]: SqArrayStringValue, [Tag.Bool]: SqBoolValue, [Tag.Date]: SqDateValue, [Tag.Declaration]: SqDeclarationValue, @@ -169,7 +160,6 @@ const tagToClass = { // type SqValue = typeof tagToClass[keyof typeof tagToClass]; export type SqValue = | SqArrayValue - | SqArrayStringValue | SqBoolValue | SqDateValue | SqDeclarationValue diff --git a/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue.res b/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue.res index de3cced2..07dccc50 100644 --- a/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue.res +++ b/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue.res @@ -13,9 +13,6 @@ type squiggleValue_Lambda = ForTS_SquiggleValue_Lambda.squiggleValue_Lambda //us @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") external svtArray_: string = "Array" -@module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") -external svtArrayString_: string = "ArrayString" - @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") external svtBool_: string = "Bool" @@ -61,7 +58,6 @@ external castEnum: string => squiggleValueTag = "%identity" let getTag = (variant: squiggleValue): squiggleValueTag => switch variant { | IEvArray(_) => svtArray_->castEnum - | IEvArrayString(_) => svtArrayString_->castEnum | IEvBool(_) => svtBool_->castEnum | IEvDate(_) => svtDate_->castEnum | IEvDeclaration(_) => svtDeclaration_->castEnum @@ -94,13 +90,6 @@ let getArray = (variant: squiggleValue): option => | _ => None } -@genType -let getArrayString = (variant: squiggleValue): option> => - switch variant { - | IEvArrayString(value) => value->Some - | _ => None - } - @genType let getBool = (variant: squiggleValue): option => switch variant { diff --git a/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Record.res b/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Record.res index 340abcfc..e145eefc 100644 --- a/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Record.res +++ b/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Record.res @@ -7,3 +7,6 @@ let getKeyValuePairs = (value: squiggleValue_Record): array<(string, squiggleVal @genType let toString = (v: squiggleValue_Record) => ReducerInterface_InternalExpressionValue.toStringMap(v) + +@genType +let toSquiggleValue = (v: squiggleValue_Record): squiggleValue => IEvRecord(v) diff --git a/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_tag.ts b/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_tag.ts index 31174e84..eec17c5b 100644 --- a/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_tag.ts +++ b/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_tag.ts @@ -1,6 +1,5 @@ export enum squiggleValueTag { Array = "Array", - ArrayString = "ArrayString", Bool = "Bool", Date = "Date", Declaration = "Declaration", diff --git a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Builtin.res b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Builtin.res index f5f7c818..d5b79fc8 100644 --- a/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Builtin.res +++ b/packages/squiggle-lang/src/rescript/FunctionRegistry/Library/FR_Builtin.res @@ -93,4 +93,57 @@ let library = [ } } ), + makeFn( + "concat", + [FRTypeString, FRTypeString], + inputs => { + switch inputs { + | [IEvString(a), IEvString(b)] => { + let answer = Js.String2.concat(a, b) + answer->Reducer_T.IEvString->Ok + } + | _ => Error(impossibleError) + } + } + ), + makeFn( + "concat", + [FRTypeArray(FRTypeAny), FRTypeArray(FRTypeAny)], + inputs => { + switch inputs { + | [IEvArray(originalA), IEvArray(b)] => { + let a = originalA->Js.Array2.copy + let _ = Js.Array2.pushMany(a, b) + a->Reducer_T.IEvArray->Ok + } + | _ => Error(impossibleError) + } + } + ), + makeFn( + "inspect", + [FRTypeAny], + inputs => { + switch inputs { + | [value] => { + Js.log(value->ReducerInterface_InternalExpressionValue.toString) + value->Ok + } + | _ => Error(impossibleError) + } + } + ), + makeFn( + "inspect", + [FRTypeAny, FRTypeString], + inputs => { + switch inputs { + | [value, IEvString(label)] => { + Js.log(`${label}: ${value->ReducerInterface_InternalExpressionValue.toString}`) + value->Ok + } + | _ => Error(impossibleError) + } + } + ), ] diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res index 62ec6cff..1ef4a543 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Dispatch/Reducer_Dispatch_BuiltIn.res @@ -34,15 +34,15 @@ let callInternal = ( | call => call->IEV.toStringFunctionCall->MathJs.Eval.eval } - let doAddArray = (originalA, b) => { - let a = originalA->Js.Array2.copy - let _ = Js.Array2.pushMany(a, b) - a->Reducer_T.IEvArray->Ok - } - let doAddString = (a, b) => { - let answer = Js.String2.concat(a, b) - answer->Reducer_T.IEvString->Ok - } + // let doAddArray = (originalA, b) => { + // let a = originalA->Js.Array2.copy + // let _ = Js.Array2.pushMany(a, b) + // a->Reducer_T.IEvArray->Ok + // } + // let doAddString = (a, b) => { + // let answer = Js.String2.concat(a, b) + // answer->Reducer_T.IEvString->Ok + // } let inspect = (value: Reducer_T.value) => { Js.log(value->IEV.toString) @@ -107,10 +107,10 @@ let callInternal = ( // | ("$_typeTuple_$", [IEvArray(elems)]) => TypeBuilder.typeTuple(elems) // | ("$_typeArray_$", [elem]) => TypeBuilder.typeArray(elem) // | ("$_typeRecord_$", [IEvRecord(propertyMap)]) => TypeBuilder.typeRecord(propertyMap) - | ("concat", [IEvArray(aValueArray), IEvArray(bValueArray)]) => - doAddArray(aValueArray, bValueArray) - | ("concat", [IEvString(aValueString), IEvString(bValueString)]) => - doAddString(aValueString, bValueString) + // | ("concat", [IEvArray(aValueArray), IEvArray(bValueArray)]) => + // doAddArray(aValueArray, bValueArray) + // | ("concat", [IEvString(aValueString), IEvString(bValueString)]) => + // doAddString(aValueString, bValueString) | ("inspect", [value, IEvString(label)]) => inspectLabel(value, label) | ("inspect", [value]) => inspect(value) | (_, [IEvBool(_)]) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res index 4d87e534..9f1ed5fb 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_ExpressionBuilder.res @@ -6,8 +6,6 @@ type expression = Reducer_T.expression let eArray = (anArray: array) => anArray->T.EArray -let eArrayString = anArray => anArray->T.IEvArrayString->T.EValue - let eBool = aBool => aBool->T.IEvBool->T.EValue let eCall = (fn: expression, args: array): expression => T.ECall(fn, args) diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Namespace.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Namespace.res index 7a95f58d..638bdbcd 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Namespace.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Namespace.res @@ -11,11 +11,7 @@ let set = (namespace: t, id: string, value): t => { let mergeFrom = (from: t, to: t): t => { to->Belt.Map.String.reduce(from, (namespace, key, value) => { - if key != "__result__" { - namespace->set(key, value) - } else { - namespace - } + namespace->set(key, value) }) } diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res index e7fe07f3..4fda2e34 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res @@ -3,7 +3,6 @@ type environment = GenericDist.env @genType.opaque type rec value = | IEvArray(arrayValue) - | IEvArrayString(array) | IEvBool(bool) | IEvDate(Js.Date.t) | IEvDeclaration(lambdaDeclaration) diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res index 5e880820..27b0a546 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res @@ -17,7 +17,6 @@ type functionCall = (string, array) let rec toString = (aValue: T.value) => switch aValue { | IEvArray(anArray) => toStringArray(anArray) - | IEvArrayString(anArray) => toStringArrayString(anArray) | IEvBool(aBool) => toStringBool(aBool) | IEvDate(date) => toStringDate(date) | IEvDeclaration(d) => toStringDeclaration(d) @@ -35,10 +34,6 @@ and toStringArray = anArray => { let args = anArray->Js.Array2.map(each => toString(each))->Js.Array2.toString `[${args}]` } -and toStringArrayString = anArray => { - let args = anArray->Js.Array2.toString - `[${args}]` -} and toStringBool = aBool => Js.String.make(aBool) and toStringCall = fName => `:${fName}` and toStringDate = date => DateTime.Date.toString(date) @@ -69,7 +64,6 @@ and toStringMap = aMap => { let toStringWithType = (aValue: T.value) => switch aValue { | IEvArray(_) => `Array::${toString(aValue)}` - | IEvArrayString(_) => `ArrayString::${toString(aValue)}` | IEvBool(_) => `Bool::${toString(aValue)}` | IEvDate(_) => `Date::${toString(aValue)}` | IEvDeclaration(_) => `Declaration::${toString(aValue)}` @@ -116,7 +110,6 @@ let toStringResultRecord = x => type internalExpressionValueType = | EvtArray - | EvtArrayString | EvtBool | EvtDate | EvtDeclaration @@ -137,7 +130,6 @@ type functionDefinitionSignature = let valueToValueType = (value: T.value) => switch value { | IEvArray(_) => EvtArray - | IEvArrayString(_) => EvtArrayString | IEvBool(_) => EvtBool | IEvDate(_) => EvtDate | IEvDeclaration(_) => EvtDeclaration @@ -160,7 +152,6 @@ let functionCallToCallSignature = (functionCall: functionCall): functionCallSign let valueTypeToString = (valueType: internalExpressionValueType): string => switch valueType { | EvtArray => `Array` - | EvtArrayString => `ArrayString` | EvtBool => `Bool` // | EvtCall => `Call` | EvtDate => `Date` diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res index c3ebf5e3..a3d41106 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res @@ -22,7 +22,7 @@ let internalStdLib: Reducer_T.namespace = { } | [IEvRecord(dict), IEvString(sIndex)] => switch Belt.Map.String.get(dict, sIndex) { | Some(value) => value - | None => RERecordPropertyNotFound("Record index not found", sIndex)->ErrorException->raise + | None => RERecordPropertyNotFound("Record property not found", sIndex)->ErrorException->raise } | _ => REOther("Trying to access key on wrong value")->ErrorException->raise } diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res index cfbd6ec9..12933e21 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res @@ -161,15 +161,25 @@ let getContinuationsBefore = (project: t, sourceId: string): array { - let nameSpace = Reducer_Namespace.mergeMany( - Belt.Array.concat( + let pastChain = project->getPastChain(sourceId) + let namespace = Reducer_Namespace.mergeMany( + Belt.Array.concatMany([ [project->getStdLib], - project->getContinuationsBefore(sourceId) - ) + pastChain->Belt.Array.map(project->getBindings), + pastChain->Belt.Array.map( + id => Reducer_Namespace.fromArray([ + ("__result__", + switch project->getResult(id) { + | Ok(result) => result + | Error(error) => error->Reducer_ErrorValue.ErrorException->raise + }) + ]) + ), + ]) ) let includesAsVariables = project->getIncludesAsVariables(sourceId) - Belt.Array.reduce(includesAsVariables, nameSpace, (acc, (variable, includeFile)) => + Belt.Array.reduce(includesAsVariables, namespace, (acc, (variable, includeFile)) => acc->Reducer_Namespace.set( variable, project->getBindings(includeFile)->Reducer_Namespace.toRecord