tla-ts 0.0.4-dev.a0009b7 → 0.0.4-dev.a297123

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tla-ts",
3
- "version": "0.0.4-dev.a0009b7",
3
+ "version": "0.0.4-dev.a297123",
4
4
  "description": "A TLA+ interpreter and model checker in TypeScript, 2× faster than TLC",
5
5
  "keywords": [
6
6
  "formal verification",
package/src/codegen.ts CHANGED
@@ -247,6 +247,23 @@ const isConstructionFree = (
247
247
  }
248
248
  };
249
249
 
250
+ // Whether the generated code evaluates an expression only in the context its frame names. A primed
251
+ // expression doesn't, since it shares the generated function's primed context.
252
+ const isReadOnly = (expression: Expression): boolean => {
253
+ switch (expression.kind) {
254
+ case "Identifier":
255
+ case "Number":
256
+ case "String":
257
+ return true;
258
+ case "FieldAccess":
259
+ return isReadOnly(expression.record);
260
+ case "FunctionApplication":
261
+ return [expression.function, ...expression.args].every(isReadOnly);
262
+ default:
263
+ return false;
264
+ }
265
+ };
266
+
250
267
  const generateExpression = (generator: Generator, expression: Expression, frame: Frame): string => {
251
268
  const generate = (node: Expression, nodeFrame = frame) =>
252
269
  generateExpression(generator, node, nodeFrame);
@@ -287,16 +304,16 @@ const generateExpression = (generator: Generator, expression: Expression, frame:
287
304
  case "FunctionApplication": {
288
305
  keepLazy(expression.function);
289
306
  // The interpreter evaluates a function that may construct one with EvalControl.KeepLazy.
307
+ const keepLazyFrame = { ...frame, context: `keepLazyContext(${frame.context})` };
290
308
  const evaluatedFunction = isConstructionFree(
291
309
  expression.function,
292
310
  frame,
293
311
  generator.environment,
294
312
  )
295
313
  ? generate(expression.function)
296
- : generateDelegate(generator, expression.function, {
297
- ...frame,
298
- context: `keepLazyContext(${frame.context})`,
299
- });
314
+ : isReadOnly(expression.function)
315
+ ? generate(expression.function, keepLazyFrame)
316
+ : generateDelegate(generator, expression.function, keepLazyFrame);
300
317
  const functionCode = `expectFunction(${evaluatedFunction}, ${addReference(generator, expression)})`;
301
318
  const describeMissing = getMissingArgumentDescriber(generator.environment.runtime.version);
302
319
  const error = describeMissing ? `, ${addReference(generator, describeMissing)}` : "";
package/src/evaluate.ts CHANGED
@@ -380,7 +380,15 @@ export const expectFunction = (value: Value, application: Expression) => {
380
380
  };
381
381
 
382
382
  export const toKeepLazyContext = (context: Context): Context =>
383
- context.isKeepLazy ? context : { ...context, isKeepLazy: true };
383
+ context.isKeepLazy
384
+ ? context
385
+ : {
386
+ environment: context.environment,
387
+ scope: context.scope,
388
+ state: context.state,
389
+ next: context.next,
390
+ isKeepLazy: true,
391
+ };
384
392
 
385
393
  const withScope = (context: Context, scope: Scope | undefined): Context => ({
386
394
  environment: context.environment,
package/src/values.ts CHANGED
@@ -14,6 +14,7 @@ export interface SetValue {
14
14
  elements: Value[];
15
15
  hash: number;
16
16
  fingerprint: number;
17
+ isFingerprinted: boolean;
17
18
  }
18
19
 
19
20
  // A set enumeration whose elements TLC can't sort, such as {1, "a"}. TLC sorts a set enumeration
@@ -31,6 +32,7 @@ export interface FunctionValue {
31
32
  fingerprint: number;
32
33
  writtenKeys: Value[] | undefined;
33
34
  functionClass: FunctionClass | undefined;
35
+ isFingerprinted: boolean;
34
36
  }
35
37
 
36
38
  export interface IntervalValue {
@@ -433,6 +435,7 @@ export const createSortedSet = (elements: Value[]): SetValue => ({
433
435
  elements,
434
436
  hash: 0,
435
437
  fingerprint: 0,
438
+ isFingerprinted: false,
436
439
  });
437
440
 
438
441
  export const createSortedFunction = (domain: Value[], range: Value[]): FunctionValue => ({
@@ -443,6 +446,7 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
443
446
  fingerprint: 0,
444
447
  writtenKeys: undefined,
445
448
  functionClass: undefined,
449
+ isFingerprinted: false,
446
450
  });
447
451
 
448
452
  // TLC words a failed function application by the Java class of the function: FcnLambdaValue for
@@ -512,14 +516,16 @@ const cacheSet = (value: Value) => {
512
516
  // FcnLambdaValue#fingerPrint caches the FcnRcdValue the function enumerates to, which applies it
513
517
  // from then on, so a function TLC has fingerprinted is no longer a lambda.
514
518
  export const markFingerprinted = (value: Value): void => {
515
- if (
516
- (!lambdaPresence.isPresent && !streamedSetPresence.isPresent) ||
517
- typeof value !== "object" ||
518
- fingerprintedValues.has(value)
519
- ) {
519
+ if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
520
520
  return;
521
521
  }
522
- fingerprintedValues.add(value);
522
+ if (value.kind === "Function" || value.kind === "Set") {
523
+ if (value.isFingerprinted) return;
524
+ value.isFingerprinted = true;
525
+ } else {
526
+ if (fingerprintedValues.has(value)) return;
527
+ fingerprintedValues.add(value);
528
+ }
523
529
  if (value.kind === "Function") {
524
530
  if (value.functionClass === "lambda") value.functionClass = undefined;
525
531
  value.domain.forEach(markFingerprinted);
@@ -2101,17 +2107,14 @@ const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
2101
2107
  : `Attempted to apply record to a non-string argument ${shown}.`;
2102
2108
  };
2103
2109
 
2104
- const findKeyIndex = (domain: Value[], argument: Value) => {
2110
+ // A key the domain holds itself, which TLC finds without a comparison that can fail.
2111
+ const findIdenticalKey = (domain: Value[], argument: Value) => {
2105
2112
  const first = domain[0];
2106
2113
  if (typeof argument === "number" && typeof first === "number") {
2107
2114
  const offset = argument - first;
2108
2115
  if (domain[offset] === argument) return offset;
2109
2116
  }
2110
- if (domain.length <= 8) {
2111
- for (let index = 0; index < domain.length; index++)
2112
- if (domain[index] === argument) return index;
2113
- }
2114
- return findIndex(domain, argument);
2117
+ return domain.length <= 8 ? domain.indexOf(argument) : -1;
2115
2118
  };
2116
2119
 
2117
2120
  export const createNonIntegerKeyError = (argument: Value) =>
@@ -2143,8 +2146,10 @@ const findKeyIndexLikeTlc = (
2143
2146
  throw createNonIntegerKeyError(argument);
2144
2147
  }
2145
2148
  }
2149
+ const identical = findIdenticalKey(domain, argument);
2150
+ if (identical !== -1) return identical;
2146
2151
  return computeLikeTlcOnFailure(
2147
- () => findKeyIndex(domain, argument),
2152
+ () => findIndex(domain, argument),
2148
2153
  (error) => {
2149
2154
  if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
2150
2155
  domain.forEach((key) => equalsLikeTlc(key, argument));