diff --git a/packages/squiggle-lang/bsconfig.json b/packages/squiggle-lang/bsconfig.json index a05df290..d9c352a5 100644 --- a/packages/squiggle-lang/bsconfig.json +++ b/packages/squiggle-lang/bsconfig.json @@ -5,11 +5,11 @@ "dir": "src/rescript", "subdirs": true }, - { - "dir": "__tests__", - "type": "dev", - "subdirs": true - }, + // { + // "dir": "__tests__", + // "type": "dev", + // "subdirs": true + // }, { "dir": "benchmark", "type": "dev", diff --git a/packages/squiggle-lang/scripts/run.js b/packages/squiggle-lang/scripts/run.js index e77b953b..97ae76bc 100755 --- a/packages/squiggle-lang/scripts/run.js +++ b/packages/squiggle-lang/scripts/run.js @@ -15,4 +15,4 @@ const result = p.getResult("a"); console.log(result.tag, result.value.toString()); const bindings = p.getBindings("a"); -console.log(bindings.asValue().toString()); +console.log(bindings.toString()); diff --git a/packages/squiggle-lang/src/js/SqModule.ts b/packages/squiggle-lang/src/js/SqModule.ts deleted file mode 100644 index c50d3be3..00000000 --- a/packages/squiggle-lang/src/js/SqModule.ts +++ /dev/null @@ -1,30 +0,0 @@ -import * as RSModuleValue from "../rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Module.gen"; -import { SqModuleValue, wrapValue } from "./SqValue"; -import { SqValueLocation } from "./SqValueLocation"; - -export class SqModule { - constructor( - private _value: RSModuleValue.squiggleValue_Module, - public location: SqValueLocation - ) {} - - entries() { - return RSModuleValue.getKeyValuePairs(this._value).map( - ([k, v]) => [k, wrapValue(v, this.location.extend(k))] as const - ); - } - - asValue() { - return new SqModuleValue( - RSModuleValue.toSquiggleValue(this._value), - this.location - ); - } - - get(k: string) { - const v = RSModuleValue.get(this._value, k); - return v === undefined || v === null - ? undefined - : wrapValue(v, this.location.extend(k)); - } -} diff --git a/packages/squiggle-lang/src/js/SqProject.ts b/packages/squiggle-lang/src/js/SqProject.ts index 4c2dea7c..eb54bc4a 100644 --- a/packages/squiggle-lang/src/js/SqProject.ts +++ b/packages/squiggle-lang/src/js/SqProject.ts @@ -2,7 +2,7 @@ import * as RSProject from "../rescript/ForTS/ForTS_ReducerProject.gen"; import { reducerErrorValue } from "../rescript/ForTS/ForTS_Reducer_ErrorValue.gen"; import { environment } from "../rescript/ForTS/ForTS_Distribution/ForTS_Distribution_Environment.gen"; import { SqError } from "./SqError"; -import { SqModule } from "./SqModule"; +import { SqRecord } from "./SqRecord"; import { wrapValue } from "./SqValue"; import { resultMap2 } from "./types"; import { SqValueLocation } from "./SqValueLocation"; @@ -83,7 +83,7 @@ export class SqProject { } getBindings(sourceId: string) { - return new SqModule( + return new SqRecord( RSProject.getBindings(this._value, sourceId), new SqValueLocation(this, sourceId, { root: "bindings", diff --git a/packages/squiggle-lang/src/js/SqValue.ts b/packages/squiggle-lang/src/js/SqValue.ts index fbde4f80..7207b859 100644 --- a/packages/squiggle-lang/src/js/SqValue.ts +++ b/packages/squiggle-lang/src/js/SqValue.ts @@ -3,11 +3,9 @@ import { squiggleValueTag as Tag } from "../rescript/ForTS/ForTS_SquiggleValue/F import { wrapDistribution } from "./SqDistribution"; import { SqLambda } from "./SqLambda"; import { SqLambdaDeclaration } from "./SqLambdaDeclaration"; -import { SqModule } from "./SqModule"; import { SqRecord } from "./SqRecord"; import { SqArray } from "./SqArray"; import { SqType } from "./SqType"; -import { SqProject } from "./SqProject"; import { SqValueLocation } from "./SqValueLocation"; export { Tag as SqValueTag }; @@ -62,14 +60,6 @@ export class SqBoolValue extends SqAbstractValue { } } -// export class SqCallValue extends SqAbstractValue { -// tag = Tag.Call as const; - -// get value() { -// return this.valueMethod(RSValue.getCall); -// } -// } - export class SqDateValue extends SqAbstractValue { tag = Tag.Date as const; @@ -102,14 +92,6 @@ export class SqLambdaValue extends SqAbstractValue { } } -export class SqModuleValue extends SqAbstractValue { - tag = Tag.Module as const; - - get value() { - return new SqModule(this.valueMethod(RSValue.getModule), this.location); - } -} - export class SqNumberValue extends SqAbstractValue { tag = Tag.Number as const; @@ -134,14 +116,6 @@ export class SqStringValue extends SqAbstractValue { } } -// export class SqSymbolValue extends SqAbstractValue { -// tag = Tag.Symbol as const; - -// get value(): string { -// return this.valueMethod(RSValue.getSymbol); -// } -// } - export class SqTimeDurationValue extends SqAbstractValue { tag = Tag.TimeDuration as const; @@ -178,16 +152,13 @@ const tagToClass = { [Tag.Array]: SqArrayValue, [Tag.ArrayString]: SqArrayStringValue, [Tag.Bool]: SqBoolValue, - // [Tag.Call]: SqCallValue, [Tag.Date]: SqDateValue, [Tag.Declaration]: SqDeclarationValue, [Tag.Distribution]: SqDistributionValue, [Tag.Lambda]: SqLambdaValue, - [Tag.Module]: SqModuleValue, [Tag.Number]: SqNumberValue, [Tag.Record]: SqRecordValue, [Tag.String]: SqStringValue, - // [Tag.Symbol]: SqSymbolValue, [Tag.TimeDuration]: SqTimeDurationValue, [Tag.Type]: SqTypeValue, [Tag.TypeIdentifier]: SqTypeIdentifierValue, @@ -200,16 +171,13 @@ export type SqValue = | SqArrayValue | SqArrayStringValue | SqBoolValue - // | SqCallValue | SqDateValue | SqDeclarationValue | SqDistributionValue | SqLambdaValue - | SqModuleValue | SqNumberValue | SqRecordValue | SqStringValue - // | SqSymbolValue | SqTimeDurationValue | SqTypeValue | SqTypeIdentifierValue diff --git a/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res b/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res index 68a143bd..5b48b8f7 100644 --- a/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res +++ b/packages/squiggle-lang/src/rescript/ForTS/ForTS_ReducerProject.res @@ -3,7 +3,7 @@ type reducerErrorValue = ForTS_Reducer_ErrorValue.reducerErrorValue //use type squiggleValue = ForTS_SquiggleValue.squiggleValue //use -type squiggleValue_Module = ForTS_SquiggleValue_Module.squiggleValue_Module //use +type squiggleValue_Record = ForTS_SquiggleValue.squiggleValue_Record //use type environment = ForTS_Distribution_Environment.environment //use @@ -184,8 +184,8 @@ let runAll = (project: reducerProject): unit => project->Private.runAll Get the bindings after running this source fil. The bindings are local to the source */ @genType -let getBindings = (project: reducerProject, sourceId: string): squiggleValue_Module => - project->Private.getBindings(sourceId) +let getBindings = (project: reducerProject, sourceId: string): squiggleValue_Record => + project->Private.getBindings(sourceId)->Reducer_Namespace.toMap /* Get the result after running this source file or the project @@ -204,7 +204,7 @@ The source has to be include free @genType let evaluate = (sourceCode: string): ( result, - squiggleValue_Module, + squiggleValue_Record, ) => Private.evaluate(sourceCode) @genType 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 1dd3887d..de3cced2 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 @@ -2,7 +2,6 @@ type reducerErrorValue = ForTS_Reducer_ErrorValue.reducerErrorValue //use @genType type squiggleValue_Array = Reducer_T.arrayValue //re-export recursive type -@genType type squiggleValue_Module = Reducer_T.nameSpace //re-export recursive type @genType type squiggleValue_Record = Reducer_T.map //re-export recursive type @genType type squiggleValue_Type = Reducer_T.map //re-export recursive type type squiggleValue_Declaration = ForTS_SquiggleValue_Declaration.squiggleValue_Declaration //use @@ -20,9 +19,6 @@ external svtArrayString_: string = "ArrayString" @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") external svtBool_: string = "Bool" -// @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") -// external svtCall_: string = "Call" - @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") external svtDate_: string = "Date" @@ -35,9 +31,6 @@ external svtDistribution_: string = "Distribution" @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") external svtLambda_: string = "Lambda" -@module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") -external svtModule_: string = "Module" - @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") external svtNumber_: string = "Number" @@ -47,9 +40,6 @@ external svtRecord_: string = "Record" @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") external svtString_: string = "String" -// @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") -// external svtSymbol_: string = "Symbol" - @module("./ForTS_SquiggleValue_tag") @scope("squiggleValueTag") external svtTimeDuration_: string = "TimeDuration" @@ -73,16 +63,13 @@ let getTag = (variant: squiggleValue): squiggleValueTag => | IEvArray(_) => svtArray_->castEnum | IEvArrayString(_) => svtArrayString_->castEnum | IEvBool(_) => svtBool_->castEnum - // | IEvCall(_) => svtCall_->castEnum //Impossible | IEvDate(_) => svtDate_->castEnum | IEvDeclaration(_) => svtDeclaration_->castEnum | IEvDistribution(_) => svtDistribution_->castEnum | IEvLambda(_) => svtLambda_->castEnum - | IEvBindings(_) => svtModule_->castEnum //Impossible | IEvNumber(_) => svtNumber_->castEnum | IEvRecord(_) => svtRecord_->castEnum | IEvString(_) => svtString_->castEnum - // | IEvSymbol(_) => svtSymbol_->castEnum | IEvTimeDuration(_) => svtTimeDuration_->castEnum | IEvType(_) => svtType_->castEnum | IEvTypeIdentifier(_) => svtTypeIdentifier_->castEnum @@ -121,13 +108,6 @@ let getBool = (variant: squiggleValue): option => | _ => None } -// @genType -// let getCall = (variant: squiggleValue): option => -// switch variant { -// | IEvCall(value) => value->Some -// | _ => None -// } - @genType let getDate = (variant: squiggleValue): option => switch variant { @@ -156,13 +136,6 @@ let getLambda = (variant: squiggleValue): option => | _ => None } -@genType -let getModule = (variant: squiggleValue): option => - switch variant { - | IEvBindings(value) => value->Some - | _ => None - } - @genType let getNumber = (variant: squiggleValue): option => switch variant { @@ -184,13 +157,6 @@ let getString = (variant: squiggleValue): option => | _ => None } -// @genType -// let getSymbol = (variant: squiggleValue): option => -// switch variant { -// | IEvSymbol(value) => value->Some -// | _ => None -// } - @genType let getTimeDuration = (variant: squiggleValue): option => switch variant { diff --git a/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Module.res b/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Module.res deleted file mode 100644 index 12021fbf..00000000 --- a/packages/squiggle-lang/src/rescript/ForTS/ForTS_SquiggleValue/ForTS_SquiggleValue_Module.res +++ /dev/null @@ -1,16 +0,0 @@ -type squiggleValue = ForTS_SquiggleValue.squiggleValue //use -@genType type squiggleValue_Module = ForTS_SquiggleValue.squiggleValue_Module //re-export recursive type - -@genType -let getKeyValuePairs = (v: squiggleValue_Module): array<(string, squiggleValue)> => - v->Reducer_Bindings.toKeyValuePairs - -@genType -let toString = (v: squiggleValue_Module): string => - ReducerInterface_InternalExpressionValue.toStringNameSpace(v) - -@genType -let toSquiggleValue = (v: squiggleValue_Module): squiggleValue => IEvBindings(v) - -@genType -let get = (v: squiggleValue_Module, k: string): option => Reducer_Bindings.get(v, k) 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 45957e8d..31174e84 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 @@ -2,16 +2,13 @@ export enum squiggleValueTag { Array = "Array", ArrayString = "ArrayString", Bool = "Bool", - // Call = "Call", Date = "Date", Declaration = "Declaration", Distribution = "Distribution", Lambda = "Lambda", - Module = "Module", Number = "Number", Record = "Record", String = "String", - // Symbol = "Symbol", TimeDuration = "TimeDuration", Type = "Type", TypeIdentifier = "TypeIdentifier", diff --git a/packages/squiggle-lang/src/rescript/ForTS/ForTS__Types.res b/packages/squiggle-lang/src/rescript/ForTS/ForTS__Types.res index b97c93e0..b36aaf81 100644 --- a/packages/squiggle-lang/src/rescript/ForTS/ForTS__Types.res +++ b/packages/squiggle-lang/src/rescript/ForTS/ForTS__Types.res @@ -6,7 +6,6 @@ @genType type squiggleValue_Array = ForTS_SquiggleValue_Array.squiggleValue_Array //re-export @genType type squiggleValue_Declaration = ForTS_SquiggleValue_Declaration.squiggleValue_Declaration //re-export @genType type squiggleValue_Lambda = ForTS_SquiggleValue_Lambda.squiggleValue_Lambda //re-export -@genType type squiggleValue_Module = ForTS_SquiggleValue_Module.squiggleValue_Module //re-export @genType type squiggleValue_Record = ForTS_SquiggleValue_Record.squiggleValue_Record //re-export @genType type squiggleValue_Type = ForTS_SquiggleValue_Type.squiggleValue_Type //re-export diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Bindings/Reducer_Bindings.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Bindings/Reducer_Bindings.res index d58a7abd..ddf250dc 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Bindings/Reducer_Bindings.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Bindings/Reducer_Bindings.res @@ -4,13 +4,11 @@ module ExpressionT = Reducer_Expression_T module T = Reducer_T -type t = Reducer_T.nameSpace +type t = Reducer_T.bindings type internalExpressionValue = Reducer_T.value -let rec get = (nameSpace: t, id: string) => { - let T.NameSpace(container, parent) = nameSpace - - switch container->Belt.MutableMap.String.get(id) { +let rec get = ({ namespace, parent }: t, id: string) => { + switch namespace->Reducer_Namespace.get(id) { | Some(v) => Some(v) | None => switch parent { @@ -20,64 +18,38 @@ let rec get = (nameSpace: t, id: string) => { } } -let getWithDefault = (nameSpace: t, id: string, default) => - switch get(nameSpace, id) { +let getWithDefault = (namespace: t, id: string, default) => + switch namespace->get(id) { | Some(v) => Some(v) | None => default } -let toString = ReducerInterface_InternalExpressionValue.toStringNameSpace - -let makeEmptyMap = () => Belt.MutableMap.String.make() - -let set = (nameSpace: t, id: string, value): t => { - let T.NameSpace(container, _) = nameSpace - Belt.MutableMap.String.set(container, id, value) - nameSpace +let set = ({ namespace } as bindings: t, id: string, value): t => { + let _ = namespace->Reducer_Namespace.set(id, value) + bindings } -let extend = (nameSpace: t) => T.NameSpace(makeEmptyMap(), nameSpace->Some) +let rec toString = ({ namespace, parent }: t) => { + let pairs = namespace->Reducer_Namespace.toString -let toKeyValuePairs = (T.NameSpace(container, _): t): array<(string, internalExpressionValue)> => { - container->Belt.MutableMap.String.toArray -} - -let makeEmptyBindings = (): t => T.NameSpace(makeEmptyMap(), None) - -let toExpressionValue = (nameSpace: t): internalExpressionValue => T.IEvBindings(nameSpace) -let fromExpressionValue = (aValue: internalExpressionValue): t => - switch aValue { - | IEvBindings(nameSpace) => nameSpace - | _ => makeEmptyBindings() + switch parent { + | Some(p) => `{${pairs}} / ${toString(p)}` + | None => `{${pairs}}` } - -let fromArray = a => T.NameSpace(Belt.MutableMap.String.fromArray(a), None) - -let mergeFrom = (T.NameSpace(container, _): t, T.NameSpace(newContainer, parent): t): t => { - NameSpace( - newContainer->Belt.MutableMap.String.reduce(container, (container, key, value) => { - if key != "__result__" { - Belt.MutableMap.String.set(container, key, value) - } - container - }), - parent, - ) } -let chainTo = (nameSpace: t, previousNameSpaces: array) => { - previousNameSpaces->Belt.Array.reduce(nameSpace, (topNameSpace, prevNameSpace) => - mergeFrom(prevNameSpace, topNameSpace) - ) +let extend = (bindings: t): t => { namespace: Reducer_Namespace.make(), parent: bindings->Some } + +let make = (): t => { namespace: Reducer_Namespace.make(), parent: None } + +let removeResult = ({ namespace } as bindings: t): t => { + namespace->Belt.MutableMap.String.remove("__result__") + bindings } -let removeResult = (nameSpace: t): t => { - let T.NameSpace(container, _) = nameSpace - container->Belt.MutableMap.String.remove("__result__") - nameSpace -} +let locals = ({ namespace }: t): Reducer_T.namespace => namespace -let locals = (T.NameSpace(container, _): t) => T.NameSpace(container, None) +let fromNamespace = (namespace: Reducer_Namespace.t): t => { namespace, parent: None } // let typeAliasesKey = "_typeAliases_" // let typeReferencesKey = "_typeReferences_" diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Context.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Context.res index 9fed751c..60adcb1e 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Context.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Context.res @@ -1,8 +1,8 @@ type t = Reducer_T.context -let createContext = (stdLib: Reducer_T.nameSpace, environment: Reducer_T.environment): t => { +let createContext = (stdLib: Reducer_Namespace.t, environment: Reducer_T.environment): t => { { - bindings: stdLib->Reducer_Bindings.extend, + bindings: stdLib->Reducer_Bindings.fromNamespace, environment: environment, } } 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 bec8bb44..6d032523 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 @@ -1,5 +1,5 @@ module Bindings = Reducer_Bindings -module Continuation = ReducerInterface_Value_Continuation +// module Continuation = ReducerInterface_Value_Continuation module ExpressionT = Reducer_Expression_T module ExternalLibrary = ReducerInterface.ExternalLibrary module InternalExpressionValue = ReducerInterface_InternalExpressionValue @@ -52,11 +52,11 @@ let callInternal = ( // | None => REArrayIndexNotFound("Array index not found", Belt.Int.fromFloat(fIndex))->Error // } - let moduleAtIndex = (nameSpace: Reducer_T.nameSpace, sIndex) => - switch Bindings.get(nameSpace, sIndex) { - | Some(value) => value->Ok - | None => RERecordPropertyNotFound("Bindings property not found", sIndex)->Error - } + // let moduleAtIndex = (nameSpace: Reducer_T.nameSpace, sIndex) => + // switch Bindings.get(nameSpace, sIndex) { + // | Some(value) => value->Ok + // | None => RERecordPropertyNotFound("Bindings property not found", sIndex)->Error + // } // let recordAtIndex = (dict: Belt.Map.String.t, sIndex) => // switch Belt.Map.String.get(dict, sIndex) { @@ -109,7 +109,7 @@ let callInternal = ( switch call { // | ("$_atIndex_$", [IEvArray(aValueArray), IEvNumber(fIndex)]) => arrayAtIndex(aValueArray, fIndex) - | ("$_atIndex_$", [IEvBindings(dict), IEvString(sIndex)]) => moduleAtIndex(dict, sIndex) + // | ("$_atIndex_$", [IEvBindings(dict), IEvString(sIndex)]) => moduleAtIndex(dict, sIndex) // | ("$_atIndex_$", [IEvRecord(dict), IEvString(sIndex)]) => recordAtIndex(dict, sIndex) // | ("$_constructArray_$", args) => IEvArray(args)->Ok // | ("$_constructRecord_$", [IEvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs) 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 08e51890..4d87e534 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 @@ -8,9 +8,6 @@ let eArray = (anArray: array) => anArray->T.EArray let eArrayString = anArray => anArray->T.IEvArrayString->T.EValue -let eBindings = (anArray: array<(string, T.value)>) => - anArray->Reducer_Bindings.fromArray->Reducer_Bindings.toExpressionValue->T.EValue - let eBool = aBool => aBool->T.IEvBool->T.EValue let eCall = (fn: expression, args: array): expression => T.ECall(fn, args) @@ -29,8 +26,6 @@ let eBlock = (exprs: array): expression => T.EBlock(exprs) let eProgram = (exprs: array): expression => T.EProgram(exprs) -let eModule = (nameSpace: T.nameSpace): expression => nameSpace->T.IEvBindings->T.EValue - let eLetStatement = (symbol: string, valueExpression: expression): expression => T.EAssign( symbol, valueExpression, diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res index 5f52fe12..49ca0cde 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Expression/Reducer_Expression_Lambda.res @@ -14,7 +14,7 @@ let doLambdaCall = ( let makeLambda = ( parameters: array, - bindings: Reducer_T.nameSpace, + bindings: Reducer_T.bindings, body: Reducer_T.expression, ): Reducer_T.lambdaValue => { // TODO - clone bindings to avoid later redefinitions affecting lambdas? diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_Namespace.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Namespace.res new file mode 100644 index 00000000..581f765b --- /dev/null +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_Namespace.res @@ -0,0 +1,38 @@ +type t = Reducer_T.namespace + +let make = (): t => Belt.MutableMap.String.make() + +let get = (namespace: t, id: string): option => + namespace->Belt.MutableMap.String.get(id) + +let set = (namespace: t, id: string, value): t => { + namespace->Belt.MutableMap.String.set(id, value) + namespace +} + +let mergeFrom = (from: t, to: t): t => { + to->Belt.MutableMap.String.reduce(from, (namespace, key, value) => { + if key != "__result__" { + namespace->Belt.MutableMap.String.set(key, value) + } + namespace + }) +} + +let mergeMany = (namespaces: array): t => + Belt.Array.reduce(namespaces, make(), (acc, ns) => acc->mergeFrom(ns)) + +let toString = (namespace: t) => + namespace + ->Belt.MutableMap.String.toArray + ->Belt.Array.map(((eachKey, eachValue)) => `${eachKey}: ${eachValue->ReducerInterface_InternalExpressionValue.toString}`) + ->Js.Array2.toString + +let fromArray = (a): t => + Belt.MutableMap.String.fromArray(a) + +let toMap = (namespace: t): Reducer_T.map => + namespace->Belt.MutableMap.String.toArray->Belt.Map.String.fromArray + +let toRecord = (namespace: t): Reducer_T.value => + namespace->toMap->IEvRecord diff --git a/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res b/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res index 7e71e343..08f0b74a 100644 --- a/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res +++ b/packages/squiggle-lang/src/rescript/Reducer/Reducer_T.res @@ -9,7 +9,6 @@ type rec value = | IEvDeclaration(lambdaDeclaration) | IEvDistribution(DistributionTypes.genericDist) | IEvLambda(lambdaValue) - | IEvBindings(nameSpace) | IEvNumber(float) | IEvRecord(map) | IEvString(string) @@ -19,17 +18,17 @@ type rec value = | IEvVoid @genType.opaque and arrayValue = array @genType.opaque and map = Belt.Map.String.t -@genType.opaque and nameSpace = NameSpace(Belt.MutableMap.String.t, option) and lambdaBody = (array, environment, reducerFn) => value @genType.opaque and lambdaValue = { parameters: array, - body: (array, environment, reducerFn) => value, + body: lambdaBody, } @genType.opaque and lambdaDeclaration = Declaration.declaration and expression = | EBlock(array) - | EProgram(array) // programs are similar to blocks, but don't create an inner scope. there can be only one program at the top level of the expression. + // programs are similar to blocks, but don't create an inner scope. there can be only one program at the top level of the expression. + | EProgram(array) | EArray(array) | ERecord(array<(expression, expression)>) | ESymbol(string) @@ -39,8 +38,14 @@ and expression = | ELambda(array, expression) | EValue(value) +and namespace = Belt.MutableMap.String.t +and bindings = { + namespace: namespace, + parent: option, +} + and context = { - bindings: nameSpace, + bindings: bindings, environment: environment, } diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res index 9ef0c3bf..5e880820 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_InternalExpressionValue.res @@ -18,9 +18,7 @@ let rec toString = (aValue: T.value) => switch aValue { | IEvArray(anArray) => toStringArray(anArray) | IEvArrayString(anArray) => toStringArrayString(anArray) - | IEvBindings(m) => toStringBindings(m) | IEvBool(aBool) => toStringBool(aBool) - // | IEvCall(fName) => toStringCall(fName) | IEvDate(date) => toStringDate(date) | IEvDeclaration(d) => toStringDeclaration(d) | IEvDistribution(dist) => toStringDistribution(dist) @@ -28,7 +26,6 @@ let rec toString = (aValue: T.value) => | IEvNumber(aNumber) => toStringNumber(aNumber) | IEvRecord(aMap) => aMap->toStringRecord | IEvString(aString) => toStringString(aString) - // | IEvSymbol(aString) => toStringSymbol(aString) | IEvTimeDuration(t) => toStringTimeDuration(t) | IEvType(aMap) => toStringType(aMap) | IEvTypeIdentifier(id) => toStringTypeIdentifier(id) @@ -42,7 +39,6 @@ and toStringArrayString = anArray => { let args = anArray->Js.Array2.toString `[${args}]` } -and toStringBindings = m => `@${m->toStringNameSpace}` and toStringBool = aBool => Js.String.make(aBool) and toStringCall = fName => `:${fName}` and toStringDate = date => DateTime.Date.toString(date) @@ -69,35 +65,19 @@ and toStringMap = aMap => { ->Js.Array2.toString `{${pairs}}` } -and toStringNameSpace = nameSpace => { - let T.NameSpace(container, parent) = nameSpace - let pairs = - container - ->Belt.MutableMap.String.toArray - ->Js.Array2.map(((eachKey, eachValue)) => `${eachKey}: ${toString(eachValue)}`) - ->Js.Array2.toString - - switch parent { - | Some(p) => `{${pairs}} / ${toStringNameSpace(p)}` - | None => `{${pairs}}` - } -} let toStringWithType = (aValue: T.value) => switch aValue { | IEvArray(_) => `Array::${toString(aValue)}` | IEvArrayString(_) => `ArrayString::${toString(aValue)}` | IEvBool(_) => `Bool::${toString(aValue)}` - // | IEvCall(_) => `Call::${toString(aValue)}` | IEvDate(_) => `Date::${toString(aValue)}` | IEvDeclaration(_) => `Declaration::${toString(aValue)}` | IEvDistribution(_) => `Distribution::${toString(aValue)}` | IEvLambda(_) => `Lambda::${toString(aValue)}` - | IEvBindings(_) => `Bindings::${toString(aValue)}` | IEvNumber(_) => `Number::${toString(aValue)}` | IEvRecord(_) => `Record::${toString(aValue)}` | IEvString(_) => `String::${toString(aValue)}` - // | IEvSymbol(_) => `Symbol::${toString(aValue)}` | IEvTimeDuration(_) => `Date::${toString(aValue)}` | IEvType(_) => `Type::${toString(aValue)}` | IEvTypeIdentifier(_) => `TypeIdentifier::${toString(aValue)}` @@ -138,16 +118,13 @@ type internalExpressionValueType = | EvtArray | EvtArrayString | EvtBool - // | EvtCall | EvtDate | EvtDeclaration | EvtDistribution | EvtLambda - | EvtModule | EvtNumber | EvtRecord | EvtString - // | EvtSymbol | EvtTimeDuration | EvtType | EvtTypeIdentifier @@ -162,16 +139,13 @@ let valueToValueType = (value: T.value) => | IEvArray(_) => EvtArray | IEvArrayString(_) => EvtArrayString | IEvBool(_) => EvtBool - // | IEvCall(_) => EvtCall | IEvDate(_) => EvtDate | IEvDeclaration(_) => EvtDeclaration | IEvDistribution(_) => EvtDistribution | IEvLambda(_) => EvtLambda - | IEvBindings(_) => EvtModule | IEvNumber(_) => EvtNumber | IEvRecord(_) => EvtRecord | IEvString(_) => EvtString - // | IEvSymbol(_) => EvtSymbol | IEvTimeDuration(_) => EvtTimeDuration | IEvType(_) => EvtType | IEvTypeIdentifier(_) => EvtTypeIdentifier @@ -193,7 +167,6 @@ let valueTypeToString = (valueType: internalExpressionValueType): string => | EvtDeclaration => `Declaration` | EvtDistribution => `Distribution` | EvtLambda => `Lambda` - | EvtModule => `Module` | EvtNumber => `Number` | EvtRecord => `Record` | EvtString => `String` diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res index d9bc517a..6b757b9a 100644 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res +++ b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_StdLib.res @@ -1,12 +1,12 @@ exception ErrorException = Reducer_ErrorValue.ErrorException -let internalStdLib: Reducer_Bindings.t = { +let internalStdLib: Reducer_T.namespace = { let res = - Reducer_Bindings.makeEmptyBindings() - ->SquiggleLibrary_Math.makeBindings - ->SquiggleLibrary_Versions.makeBindings + Reducer_Namespace.make() + ->Reducer_Namespace.mergeFrom(SquiggleLibrary_Math.make()) + ->Reducer_Namespace.mergeFrom(SquiggleLibrary_Versions.make()) - let _ = res->Reducer_Bindings.set( + let _ = res->Reducer_Namespace.set( "$_atIndex_$", Reducer_Expression_Lambda.makeFFILambda((inputs, _, _) => { switch inputs { @@ -31,7 +31,7 @@ let internalStdLib: Reducer_Bindings.t = { FunctionRegistry_Library.nonRegistryLambdas->Js.Array2.forEach( ((name, lambda)) => { - let _ = res->Reducer_Bindings.set(name, lambda->Reducer_T.IEvLambda) + let _ = res->Reducer_Namespace.set(name, lambda->Reducer_T.IEvLambda) } ) @@ -62,7 +62,7 @@ let internalStdLib: Reducer_Bindings.t = { FunctionRegistry_Library.registry.fnNameDict ->Js.Dict.keys ->Js.Array2.forEach(name => { - let _ = res->Reducer_Bindings.set( + let _ = res->Reducer_Namespace.set( name, Reducer_Expression_Lambda.makeFFILambda((arguments, environment, reducer) => { switch FunctionRegistry_Library.call(name, arguments, environment, reducer) { diff --git a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Value/ReducerInterface_Value_Continuation.res b/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Value/ReducerInterface_Value_Continuation.res deleted file mode 100644 index d22d75dd..00000000 --- a/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_Value/ReducerInterface_Value_Continuation.res +++ /dev/null @@ -1,21 +0,0 @@ -module InternalExpressionValue = ReducerInterface_InternalExpressionValue - -type t = Reducer_T.nameSpace - -let toValue = nameSpace => Reducer_T.IEvBindings(nameSpace) -let toString = nameSpace => InternalExpressionValue.toString(toValue(nameSpace)) -let toStringResult = rNameSpace => - Belt.Result.map(rNameSpace, toValue(_))->InternalExpressionValue.toStringResult -let toStringOptionResult = orNameSpace => - Belt.Option.map( - orNameSpace, - Belt.Result.map(_, toValue(_)), - )->InternalExpressionValue.toStringOptionResult - -let inspect = (nameSpace, label: string) => Js.log(`${label}: ${toString(nameSpace)}`) - -let inspectOption = (oNameSpace, label: string) => - switch oNameSpace { - | Some(nameSpace) => inspect(nameSpace, label) - | None => Js.log(`${label}: None`) - } diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res index 0730dee0..ea888d71 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject.res @@ -1,7 +1,6 @@ // TODO: Auto clean project based on topology module Bindings = Reducer_Bindings -module Continuation = ReducerInterface_Value_Continuation module ErrorValue = Reducer_ErrorValue module InternalExpressionValue = ReducerInterface_InternalExpressionValue module ProjectItem = ReducerProject_ProjectItem @@ -97,7 +96,7 @@ let setContinues = (project: t, sourceId: string, continues: array): uni handleNewTopology(project) } let getContinues = (project: t, sourceId: string): array => - ProjectItem.getContinues(project->getItem(sourceId)) + project->getItem(sourceId)->ProjectItem.getContinues let removeContinues = (project: t, sourceId: string): unit => { let newItem = project->getItem(sourceId)->ProjectItem.removeContinues @@ -105,9 +104,6 @@ let removeContinues = (project: t, sourceId: string): unit => { handleNewTopology(project) } -let getContinuation = (project: t, sourceId: string): ProjectItem.T.continuationArgumentType => - project->getItem(sourceId)->ProjectItem.getContinuation - let setContinuation = ( project: t, sourceId: string, @@ -142,8 +138,8 @@ let rawParse = (project: t, sourceId): unit => { project->setItem(sourceId, newItem) } -let getStdLib = (project: t): Reducer_Bindings.t => project.stdLib -let setStdLib = (project: t, value: Reducer_Bindings.t): unit => { +let getStdLib = (project: t): Reducer_T.namespace => project.stdLib +let setStdLib = (project: t, value: Reducer_T.namespace): unit => { project.stdLib = value } @@ -152,47 +148,41 @@ let setEnvironment = (project: t, value: InternalExpressionValue.environment): u project.environment = value } -let getBindings = (project: t, sourceId: string): ProjectItem.T.bindingsArgumentType => { - project->getContinuation(sourceId)->Reducer_Bindings.locals +let getBindings = (project: t, sourceId: string): Reducer_T.namespace => { + project->getItem(sourceId)->ProjectItem.getContinuation } -let getContinuationsBefore = (project: t, sourceId: string): array => { - let pastNameSpaces = project->getPastChain(sourceId)->Js.Array2.map(getBindings(project, _)) - let theLength = pastNameSpaces->Js.Array2.length - if theLength == 0 { - // `getContinuationBefore ${sourceId}: stdLib`->Js.log - [project->getStdLib] - } else { - // `getContinuationBefore ${sourceId}: ${lastOne} = ${InternalExpressionValue.toStringBindings( - // project->getBindings(lastOne), - // )}`->Js.log - pastNameSpaces - } +let getBindingsAsRecord = (project: t, sourceId: string): Reducer_T.value => { + project->getBindings(sourceId)->Reducer_Namespace.toRecord } -let linkDependencies = (project: t, sourceId: string): ProjectItem.T.continuation => { - let continuationsBefore = project->getContinuationsBefore(sourceId) - let nameSpace = - Reducer_Bindings.makeEmptyBindings()->Reducer_Bindings.chainTo(continuationsBefore) +let getContinuationsBefore = (project: t, sourceId: string): array => { + project->getPastChain(sourceId)->Belt.Array.map(project->getBindings) +} + +let linkDependencies = (project: t, sourceId: string): Reducer_T.namespace => { + let nameSpace = Reducer_Namespace.mergeMany( + Belt.Array.concat( + [project->getStdLib], + project->getContinuationsBefore(sourceId) + ) + ) + let includesAsVariables = project->getIncludesAsVariables(sourceId) - Belt.Array.reduce(includesAsVariables, nameSpace, (currentNameSpace, (variable, includeFile)) => - Bindings.set( - currentNameSpace, + Belt.Array.reduce(includesAsVariables, nameSpace, (acc, (variable, includeFile)) => + acc->Reducer_Namespace.set( variable, - getBindings(project, includeFile)->Reducer_T.IEvBindings, + project->getBindings(includeFile)->Reducer_Namespace.toRecord ) ) } let doLinkAndRun = (project: t, sourceId: string): unit => { - let context = Reducer_Context.createContext(project->getStdLib, project->getEnvironment) - // FIXME: fill context with dependencies - let continuation = linkDependencies(project, sourceId) - let contextWithContinuation = { - ...context, - bindings: continuation->Reducer_Bindings.extend, - } - let newItem = project->getItem(sourceId)->ProjectItem.run(contextWithContinuation) + let context = Reducer_Context.createContext( + project->linkDependencies(sourceId), + project->getEnvironment + ) + let newItem = project->getItem(sourceId)->ProjectItem.run(context) // Js.log("after run " ++ newItem.continuation->Reducer_Bindings.toString) project->setItem(sourceId, newItem) } @@ -243,6 +233,6 @@ let evaluate = (sourceCode: string) => { ( getResultOption(project, "main")->Belt.Option.getWithDefault(Reducer_T.IEvVoid->Ok), - project->getBindings("main"), + project->getBindings("main")->Reducer_Namespace.toMap, ) } diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res index e870bcfb..271b3ac9 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem.res @@ -8,7 +8,7 @@ let emptyItem: projectItem = { source: "", rawParse: None, expression: None, - continuation: Reducer_Bindings.makeEmptyBindings(), + continuation: Reducer_Namespace.make(), result: None, continues: [], includes: []->Ok, @@ -168,14 +168,14 @@ let buildExpression = (this: t): t => { } let failRun = (this: t, e: Reducer_ErrorValue.errorValue): t => - this->setResult(e->Error)->setContinuation(Reducer_Bindings.makeEmptyBindings()) + this->setResult(e->Error)->setContinuation(Reducer_Namespace.make()) let doRun = (this: t, context: Reducer_T.context): t => switch this->getExpression { | Some(expressionResult) => switch expressionResult { | Ok(expression) => try { let result = Reducer_Expression.evaluate(expression, context) - this->setResult(result->Ok)->setContinuation(context.bindings) + this->setResult(result->Ok)->setContinuation(context.bindings->Reducer_Bindings.locals) } catch { | Reducer_ErrorValue.ErrorException(e) => this->failRun(e) | _ => this->failRun(RETodo("unhandled rescript exception")) diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem_T.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem_T.res index e696534e..bf1cbfcb 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem_T.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_ProjectItem_T.res @@ -8,12 +8,9 @@ type rawParseArgumentType = result type rawParseType = option type expressionArgumentType = result type expressionType = option -type continuation = Reducer_T.nameSpace -type continuationArgumentType = Reducer_T.nameSpace +type continuationArgumentType = Reducer_T.namespace type continuationType = option type continuationResultType = option> -type bindingsArgumentType = Reducer_T.nameSpace -type bindingsType = option type resultArgumentType = result type resultType = option type continuesArgumentType = array diff --git a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_T.res b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_T.res index 78a3e2a3..8d53e7fe 100644 --- a/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_T.res +++ b/packages/squiggle-lang/src/rescript/ReducerProject/ReducerProject_T.res @@ -4,7 +4,7 @@ module ExpressionT = Reducer_Expression_T @genType.opaque type project = { items: Belt.MutableMap.String.t, - mutable stdLib: Reducer_Bindings.t, + mutable stdLib: Reducer_Namespace.t, mutable environment: ExpressionT.environment, mutable previousRunOrder: array, } diff --git a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res index 2a433c5a..4682fa01 100644 --- a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res +++ b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Math.res @@ -1,5 +1,3 @@ -module Bindings = Reducer_Bindings - let availableNumbers: array<(string, float)> = [ ("Math.pi", Js.Math._PI), ("Math.e", Js.Math._E), @@ -13,8 +11,5 @@ let availableNumbers: array<(string, float)> = [ ("Math.tau", 6.283185307179586), ] -let mathBindings: Bindings.t = - availableNumbers->E.A2.fmap(((name, v)) => (name, Reducer_T.IEvNumber(v)))->Bindings.fromArray - -let makeBindings = (previousBindings: Bindings.t): Bindings.t => - previousBindings->Bindings.mergeFrom(mathBindings) +let make = (): Reducer_Namespace.t => + availableNumbers->E.A2.fmap(((name, v)) => (name, Reducer_T.IEvNumber(v)))->Reducer_Namespace.fromArray diff --git a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Versions.res b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Versions.res index 30b64b81..70cf7b1e 100644 --- a/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Versions.res +++ b/packages/squiggle-lang/src/rescript/SquiggleLibrary/SquiggleLibrary_Versions.res @@ -1,7 +1,2 @@ -module Bindings = Reducer_Bindings - -let bindings: Reducer_T.nameSpace = - [("System.version", Reducer_T.IEvString("0.4.0-dev"))]->Bindings.fromArray - -let makeBindings = (previousBindings: Reducer_T.nameSpace): Reducer_T.nameSpace => - previousBindings->Bindings.mergeFrom(bindings) +let make = (): Reducer_Namespace.t => + [("System.version", Reducer_T.IEvString("0.4.0-dev"))]->Reducer_Namespace.fromArray