Further use of provisional types

node v18.11.0
version: 1.0.0
endpointsharetweet
const ObjectOwnPropertyKeys = Reflect.ownKeys; const given = f => f(); const { assign: ObjectAssign, create: ObjectCreate, keys: ObjectKeys, entries: ObjectEntries, freeze: ObjectFreeze, fromEntries: ObjectFromEntries, hasOwn: ObjectHasOwn, getPrototypeOf: ObjectGetPrototypeOf, setPrototypeOf: ObjectSetPrototypeOf, } = Object; const ObjectGroupBy = (array, f) => array.reduce( (groups, item) => given(( group = f(item)) => ((groups[group] = (groups[group] || [])).push(item), groups)), { }); const { isArray: ArrayIsArray } = Array; const { join: ArrayPrototypeJoin, map: ArrayPrototypeMap } = Array.prototype; const ArrayF = ObjectFromEntries(["filter", "map", "join", "reduce"] .map(name => given(( f = Array.prototype[name]) => [name, (array, ...args) => Call(f, array, ...args)]))); const { toString: ObjectPrototypeToString } = Object.prototype; const ObjectFilter = (object, f) => ObjectFilterEntries(object, ([key, value]) => f(value, key)); const ObjectFilterKeys = (object, f) => ObjectFilter(object, (_, key) => f(key)); const ObjectFilterEntries = (object, f) => ObjectFromEntries(ObjectEntries(object).filter(f)); const ObjectMap = (object, f) => ObjectMapEntries(object, ([key, value]) => [key, f(value, key)]); const ObjectMapEntries = (object, f) => ObjectFromEntries(ObjectOwnPropertyKeys(object) .map(key => f([key, object[key]]))); const { has: WeakMapProrotypeHas, get: WeakMapPrototypeGet, set: WeakMapPrototypeSet } = WeakMap.prototype; const { entries: MapPrototypeEntries, get: MapPrototypeGet, has: MapPrototypeHas, keys: MapPrototypeKeys, values: MapPrototypeValues } = Map.prototype;
const IsNull = V => V === null; const IsString = V => typeof V === "string"; const IsCallable = V => typeof V === "function"; const IsNonCallableObject = V => !IsNull(V) && typeof V === "object"; const IsObject = value => !IsNull(value) && typeof value === "object" || typeof value === "function"; const Call = Function.prototype.call.bind(Function.prototype.call); const IsNotNull = value => value !== null;
const { IsReifiedObject, GetInternalSlot, GetInstanceData, ReifiedObjectClaim, } = given(( InternalSlotsForReifiedObjects = new WeakMap(), WeakMapHas = (WM, K, V) => WM.has(K, V), WeakMapKey = (WM, K, V) => (WM.set(K, V), K), WeakMapSet = (WM, K, V) => WM.set(K, V), WeakMapGet = (WM, K, f) => WM.get(K)) => ({ IsReifiedObject: O => WeakMapHas(InternalSlotsForReifiedObjects, O), GetInternalSlot: (ReifiedObject, InternalSlot) => WeakMapGet(InternalSlotsForReifiedObjects, ReifiedObject)[InternalSlot], GetInstanceData: ReifiedObject => GetInternalSlot(ReifiedObject, "[[InstanceData]]"), ReifiedObjectClaim: ( ImmutablePlatformPrototype, /* :: Immutable<PlatformPrototype> */ [Type, TypeId], /* :: Left [Type] | Right [false, TypeId] */ CaseName, /* :: string */ InstanceData, /* :: Immutable<PlatformObject> | null */ UnreachablePlatformNucleus /* :: Unreachable<PlatformObject> */, OptionalPropertiesToCopy /* :: PlatformObject | null | undefined */) => given(( nucleusPrototype = ObjectGetPrototypeOf(UnreachablePlatformNucleus), needsSetPrototypeOf = nucleusPrototype !== ImmutablePlatformPrototype, targetPrototyped = needsSetPrototypeOf ? ObjectSetPrototypeOf( UnreachablePlatformNucleus, ImmutablePlatformPrototype) : UnreachablePlatformNucleus, targetDetailed = !!OptionalPropertiesToCopy ? ObjectAssign( targetPrototyped, OptionalPropertiesToCopy) : targetPrototyped, targetImmutable = ObjectFreeze(targetDetailed)) => WeakMapKey( InternalSlotsForReifiedObjects, targetImmutable, ObjectFreeze(ObjectSetPrototypeOf( { "[[Type]]": [Type, TypeId], "[[Case]]": CaseName, "[[InstanceData]]": InstanceData }, null)))) }));
const ToUnreachablePlatformFunction = given(( AdditionalParameterNames = { "LEXICAL": [], "STRICT": ["Call"], "CONSTRUCTOR": ["Call"] }, TargetFunctionSourceText = { "LEXICAL": `: (...args) => FunctionBody(...args)`, "STRICT": `(...args) { return Call(FunctionBody, this, ...args) }`, "CONSTRUCTOR": `: function (...args) { return Call( FunctionBody, this, new.target, ...args) }` }, FIXME_quote = x => `"${x}"`) => (ThisMode, FunctionName, FunctionBody, PropertiesToCopy) => given(( quotedFunctionName = FIXME_quote(FunctionName), toUnreachablePlatformFunction = new Function( "FunctionBody", ...AdditionalParameterNames[ThisMode], `return { ${quotedFunctionName}` + TargetFunctionSourceText[ThisMode] + `}[${quotedFunctionName}]`), platformFunction = ThisMode === "LEXICAL" ? toUnreachablePlatformFunction(FunctionBody) : toUnreachablePlatformFunction(FunctionBody, Call), propertiedFunction = PropertiesToCopy ? ObjectAssign(platformFunction, PropertiesToCopy) : platformFunction) => propertiedFunction));
const ToImmutableFunction = ( FunctionName, FunctionBody, PropertiesToCopy) => ToUnreachablePlatformFunction( "LEXICAL", FunctionName, FunctionBody, PropertiesToCopy);
const ToBridgePrototype = given(( toString = ToUnreachablePlatformFunction( "STRICT", "toString", ObjectPrototypeToString), toStringTag = Symbol.toStringTag, toPrototypeProperties = (ToStringTag, PropertiesToCopy) => PropertiesToCopy ? { [toStringTag]: ToStringTag, toString, ...PropertiesToCopy } : { [toStringTag]: ToStringTag, toString }) => ( ConstructorName, PropertiesToCopy, BasePrototype) =>//(console.log(ConstructorName)) || ObjectFreeze( ObjectAssign( ObjectSetPrototypeOf( ToUnreachablePlatformFunction( "CONSTRUCTOR", ConstructorName, () => { throw "OH NO!" }).prototype, null), toPrototypeProperties( ConstructorName, PropertiesToCopy)))); const IsSelfApplicable = value => IsCallable(value) && /^(?:\(\s*)?(?:self|callee)(?=[,\s\)])/.test(value + "");
const PrimitiveTypeNames = [ "bigint", "boolean", "number", "null", "string", "symbol", "undefined" ]; const BootstrapTypeSchemas = { "Dictionary": { "parameters": ["key", "value"], "cases": { "": (U𝑝, L) => U𝑝(L + 1).CaseDefinition({ "count": U𝑝(L + 1).FieldDefinition.Parameter({ "domain": () => U𝑝(0).number, "initializer": () => 0, "variadic": false }), /*"platformDictionary": { "domain": }*/ }) }, "prototype": { get: (self, key) => self.platformDictionary.get(key), has: (self, key) => self.platformDictionary.has(key), entries: self => self.platformDictionary.entries(), keys: self => self.platformDictionary.keys(self), map: (self, f) => Universe(L).CaseDefinitionDictionary( self.entries().map(([key, value]) => [key, f(value, key)])) } }, "FieldDefinition": { "cases": { "Parameter": (U𝑝, L) => U𝑝(L + 1).CaseDefinition({ "domain": U𝑝(L + 1).FieldDefinition.Parameter({ // For some U(L).Field(L), the domain will be Type(L) // Which means that the *domain of the domain* needs to // be able to store Type(L), which requires Type(L + 1). // // [1, "Function", [[1, "Type"], [1, "Type"]], () => ] // (I𝑝, L) => I𝑝.ToFunction( // L + 1, [[L + 1, "Type"]], () => Universe(L).Type), // domain: Type(0). We always defer, so that means the VALUE is // () => Type(0). // "domain": [1, "Function", [[1, "Type"], [1, "Type"]], "", [0, "Type", 0]], "domain": () => T𝑝.ƒ(T𝑝.Type), // Someone making a different domain field would still have to return // SOMETHING of Type(1), so we provide no fallback function. Thus: // Maybe(Function(Type(1))).None // You must provide a type of type Type(0), so we have no fallback. // Thus, Maybe(() -> Type(0)).None // "fallback": [1, "Maybe", [[1, "Function", [[1, "Type"], [1, "Type"]]], "None"], "fallback": () => T𝑝.Maybe.None(T𝑝.ƒ(T𝑝.Type0)), "variadic": false }), "fallback": U𝑝(L + 1).FieldDefinition.Parameter({ // () => T𝑝.Maybe(T𝑝.ƒ(domain())) // domain: Maybe(() -> domain.domain()T) "domain": ({ domain }) => T𝑝.Maybe(T𝑝.ƒ(domain())), // T𝑝.Maybe.Just(({ domain }) => "fallback": ({ domain }) => T𝑝.Maybe.Just(T𝑝.Maybe.None(T𝑝.ƒ(domain()))), //T𝑝.Maybe.Just(T𝑝.Maybe.None(T𝑝.ƒ(T𝑝.Type0))), "variadic": false }), "variadic": U𝑝(L + 1).FieldDefinition.Parameter({ "domain": () => U𝑝(0).boolean, "fallback": () => T𝑝.Maybe.Just(false), "variadic": false }) }), "Computed": (U𝑝, L) => U𝑝(L + 1).CaseDefinition.Parameter({ "domain": U𝑝(L + 1).FieldDefinition({ // [1, "Function", [[1, "Type"], [1, "Type"]], "", [0, "Type", 0]], "domain": () => T𝑝.ƒ(T𝑝.Type0), // [1, "Maybe", [[1, "Function", [[1, "Type"], [1, "Type"]]], "None"] "fallback": () => T𝑝.Maybe.None(T𝑝.ƒ(T𝑝.Type0)), "variadic": false }), "initializer": U𝑝(L + 1).FieldDefinition.Parameter({ // Since we returned Type(0) for our domain above, the domain of our initializer is // any function that returns Type(0). Again, we have to "thunk" this, so we wrap it in // a function... The type of that function is Function(Function(Type(1))) and thus returns // the TYPE () -> Type(0) "domain": ({ domain }) => T𝑝.ƒ(domain()), // [1, "Function", [[1, "Function", [[1, "Type"], []]]], "", // [1, "Function", [[0, "Type"], [1, "Type"]], ""]] "fallback": ({ domain }) => T𝑝.Maybe.None(T𝑝.ƒ(domain())), //"fallback": [1, "Maybe", [[1, "Function", [[1, "Function", [[1, "Type"], []]]]]], "None"], "variadic": false }) }) }, "prototype": { } }, "CaseDefinition": { "cases": { "": (U𝑝, L) => U𝑝(L + 1).CaseDefinition({ "fields": U𝑝(L + 1).FieldDefinition.Parameter({ // The domain of FieldDefiniton0's domain is Type(0), meaning it can // hold any Type(0) type. What the means is that the TYPE of domain // is Type(1), since it needs to be able to hold Type(0). We additionally // then "thunk" it, so it's actually () => Type(0), so the type is // () => Type(1), since () => Type(0) ISA Type(1) // // The fields field in CaseDefinitions must hold FieldDefinition of the same // level. CaseDefinition0 holds a Dictionary of FieldDefiniton0's. // CaseDefinition0 ISA Type(1) and is itself of course described by // FieldDefinition1's. So, domain's type is () => Type(1). However, it // still returns Dictionary0(string, FieldDefinition0) // [1, "Function", [[1, "Function", [[1, "Type"]]]]] // T𝑝.ƒ1 // T𝑝.ƒ1(T𝑝.Type(1))(T𝑝.Dictionary0(T𝑝.string, T𝑝.FieldDefinition0)), /* T𝑝.Maybe(T𝑝.ƒ(T𝑝.Dictionary0(T𝑝.string, T𝑝.FieldDefinition0))) .Just(T𝑝.ƒ(T𝑝.Dictionary0(T𝑝.string, T𝑝.FieldDefinition0))( T𝑝.Dictionary0(T𝑝.string, T𝑝.FieldDefinition0))()),*/ "domain": () => U𝑝(L).Dictionary(U𝑝(0).string, U𝑝(L).FieldDefinition), "fallback": () => U𝑝(L).Maybe( U𝑝(L).Dictionary(U𝑝(0).string, U𝑝(L).FieldDefinition)).Just({ value: U𝑝(L).Dictionary(U𝑝(0).string, U𝑝(0).FieldDefinition)() }), "variadic": false }) }) }, "prototype": { } }, "TypeDefinition": { "cases": { "": (U𝑝, L) => U𝑝(L + 1).CaseDefinition({ "name": U𝑝(L + 1).FieldDefinition.Parameter({ "domain": () => U𝑝(L + 1).Maybe(U𝑝(0).string), "fallback": () => U𝑝(L + 1).Maybe.Just(U𝑝(L + 1).Maybe(U𝑝(0).string).None), "variadic": false }), "cases": U𝑝(L + 1).FieldDefinition.Parameter({ "domain": () => U𝑝(L).Dictionary(U𝑝(0).string, U𝑝(L).CaseDefinition), "fallback": () => U𝑝(0).Maybe.Just(() => U𝑝(L).Dictionary(U𝑝(0).string, U𝑝(L).CaseDefinition)()), "variadic": false }) }) }, "prototype": { } }, };
const GetOrSetOwnProperty = (O, P, fallback) => ObjectHasOwn(O, P) ? O[P] : (O[P] = fallback(P));
const FAIL = message => { throw Error(message); }; const ReifiedObjectOrdinaryCreateFromCase = ( TargetTypeOrDeferredType, TargetTypeDefinition, TargetCaseName, TargetCaseDefinition, CalleeArguments) => /*(console.log("HERE",TargetTypeOrDeferredType, TargetTypeDefinition, TargetCaseName, TargetCaseDefinition, CalleeArguments), */ReifiedObjectClaim( ToBridgePrototype("BLAH"), /*GetInstanceData(TargetTypeDefinition) .casePrototypes .get(TargetCaseName),*/ TargetTypeOrDeferredType, null,//TargetTypeDefinition, TargetCaseName, { I_AM_A_TYPE: "hello" }, //CalleeArguments[0], /* ToUnreachablePlatformNucleusFromCase( TargetCaseDefinition, CalleeArguments),*/ null, null);//); const ReifiedObjectCreateFromCase = ( TargetType, TargetCaseName, CalleeNewTarget, CalleeArguments) => given(( TargetTypeDefinition = GetInstanceData(TargetType).definition, { name, cases } = GetInstanceData(TargetType).definition, TypeCaseName = TargetCaseName ? `${name}.${TargetCaseName}` : name, isInstantiable = cases.size > 0 || FAIL(`type ${name} is not an instantiable type.`), //cases.count > 0 || FAIL(`type ${name} is not an instantiable type.`), hasConstructor = cases.has(TargetCaseName) || FAIL(`type ${name} has no case named "${TargetCaseName}"`), invokedCorrectly = !CalleeNewTarget || FAIL(`Don't invoke ${TypeCaseName} with "new".`), TargetCaseConstructor = ReifiedObjectOrdinaryCreateFromCase) => TargetCaseConstructor( [TargetType], TargetTypeDefinition, TargetCaseName, null,//TargetCaseDefinition, CalleeArguments)); // REMEMBER, NONE OF THE TYPES EXIST AT PROVISION TIME!!!! WE CREATE WITHOUT THEM const [Type, PrimitiveTypes] = given(( // Name convenience functions. ToTypeDisplayName = given(( DigitSubscripts = "₀₁₂₃₄₅₆₇₈₉", ToLevelSubscript = L => (L + "") .replace(/\d/g, d => DigitSubscripts[d])) => (TypeLevel, TypeName) => TypeLevel === 0 ? TypeName : `${TypeName}${ToLevelSubscript(TypeLevel)}`), ToTypeCaseDisplayName = (TypeLevel, TypeName, CaseName) => CaseName === "" ? ToTypeDisplayName(TypeLevel, TypeName) : `${ToTypeDisplayName(TypeLevel, TypeName)}.${CaseName}`, ReifiedObjectBootstrapCreateFromCase = ( TypeLevel, TypeName, CaseName, InternalData, Nucleus) => ReifiedObjectClaim( ToBridgePrototype("YIKES_" + TypeName + " " + CaseName + " " + TypeLevel), [false, [TypeLevel, TypeName]], CaseName, InternalData, Nucleus), // ToType𝑝 = (TypeLevel, TypeSchema) => // Begin provisional constructors. IsNotPrimaryCase = CaseName => CaseName !== "", ToExpandedTypeSchema = schema => given(( { primaryCaseEntry, namedCasesEntries } = ObjectGroupBy( ObjectEntries(schema.cases), ([key]) => IsNotPrimaryCase(key) ? "namedCasesEntries" : "primaryCaseEntry"), primaryCase = !!primaryCaseEntry && primaryCaseEntry[0][1], namedCases = ObjectFromEntries(namedCasesEntries || [])) => ({ ...schema, primaryCase, namedCases })), { TypeSchemas, TypeFunctionSchemas } = ObjectMap(ObjectGroupBy( ObjectEntries( ObjectMap(BootstrapTypeSchemas, ToExpandedTypeSchema)), ([, TypeSchema]) => ObjectHasOwn(TypeSchema, "parameters") ? "TypeFunctionSchemas" : "TypeSchemas"), ObjectFromEntries), ToEncodedCaseConstructor = (EncodedType, CaseName, PropertiesToCopy) => ToImmutableFunction(`${EncodedType[1]}.${CaseName}`, Nucleus => [...EncodedType, CaseName, Nucleus], PropertiesToCopy), ToEncodedTypeShorthand = (EncodedType, cases) => given(( NamedConstructors = ObjectFilterKeys(cases, IsNotPrimaryCase)) => ToEncodedCaseConstructor(EncodedType, "", ObjectMap(NamedConstructors, (_, CaseName) => ToEncodedCaseConstructor(EncodedType, CaseName)))), ToTypeNoPrimaryCaseErrorConstructor𝑝 = (TypeLevel, TypeName, CaseName, PropertiesToCopy) => ToImmutableFunction( ToTypeCaseDisplayName(TypeLevel, TypeName, ""), () => FAIL(ToTypeCaseDisplayName(TypeLevel, TypeName, "") + "HAS NO PRIMARY CASE"), PropertiesToCopy), ToTypeCaseConstructor𝑝 = (TypeLevel, TypeName, CaseName, PropertiesToCopy) => ToImmutableFunction( ToTypeCaseDisplayName(TypeLevel, TypeName, CaseName), RawObjectNucleus => ReifiedObjectClaim( // We nest this because we want to simulate the // CasePrototype/TypePrototype structure you normally get. ToBridgePrototype(ToTypeCaseDisplayName(TypeLevel, TypeName, CaseName)), [false, [TypeLevel, TypeName]], CaseName, null, RawObjectNucleus), PropertiesToCopy), ToType𝑝 = TypeLevel => (TypeSchema, TypeName) => (TypeSchema.primaryCase ? ToTypeCaseConstructor𝑝 : ToTypeNoPrimaryCaseErrorConstructor𝑝) (TypeLevel, TypeName, "", ObjectMap(TypeSchema.namedCases, (_, CaseName) => ToTypeCaseConstructor𝑝(TypeLevel, TypeName, CaseName))), GetTypeUniverse𝑝 = given(( TypeUniverses𝑝 = ObjectCreate(null), TypeUniverseCreate𝑝 = TypeLevel => ObjectMap(TypeSchemas, ToType𝑝(TypeLevel)), GetTypeUniverse𝑝 = TypeLevel => GetOrSetOwnProperty( TypeUniverses𝑝, TypeLevel, TypeUniverseCreate𝑝)) => GetTypeUniverse𝑝), [GetTypeUniverse, PrimitiveTypes] = given(( U𝑝 = GetTypeUniverse𝑝, TypeUniverses = ObjectCreate(null), TypeUniverseCreate = TypeLevel => given(( TypeDisplayName = ToTypeDisplayName(TypeLevel, "Type"), Type = ToUnreachablePlatformFunction( "CONSTRUCTOR", ToTypeDisplayName(TypeLevel, "Type"), (NewTarget, ...args) => (console.log(`IN ${TypeDisplayName} constructor`), ReifiedObjectCreateFromCase(Type, "", NewTarget, args)))) => ReifiedObjectClaim( // We nest this because we want to simulate the // CasePrototype/TypePrototype structure you normally get. ToBridgePrototype(TypeDisplayName), [false, [TypeLevel + 1, "Type"]], "", { definition: U𝑝(TypeLevel).TypeDefinition({ name: "Type", cases: new Map([["", {}]]) }) }, Type)), Type0 = TypeUniverseCreate(0), PrimitiveTypes = ObjectFromEntries(ArrayF.map( PrimitiveTypeNames, PrimitiveTypeName => [ PrimitiveTypeName, Type0(U𝑝(0).TypeDefinition({ name: PrimitiveTypeName, cases: new Map() }))])), GetTypeUniverse = TypeLevel => GetOrSetOwnProperty( TypeUniverses, TypeLevel, TypeUniverseCreate)) => [GetTypeUniverse, PrimitiveTypes])) => [GetTypeUniverse, PrimitiveTypes]); x = Type(0)("hi")
PrimitiveTypes
Loading…

no comments

    sign in to comment