tla-ts 0.0.3-dev.a57ca4f → 0.0.3-dev.a91da46

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.
Files changed (81) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +148 -8
  5. package/dist/index.d.ts +148 -8
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -20
  12. package/dist/run-cli.js +1351 -20
  13. package/dist/transfer.cjs +86 -28
  14. package/dist/transfer.js +86 -28
  15. package/package.json +5 -1
  16. package/src/ast.ts +52 -6
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -11
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1358 -219
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -18
  24. package/src/config.ts +252 -179
  25. package/src/environment.ts +451 -129
  26. package/src/errors.ts +217 -3
  27. package/src/evaluate.ts +723 -116
  28. package/src/exit-codes.ts +5 -4
  29. package/src/index.ts +1 -0
  30. package/src/instances.ts +4 -2
  31. package/src/java-overrides.ts +104 -25
  32. package/src/levels.ts +183 -37
  33. package/src/lexer.ts +32 -4
  34. package/src/live-check.ts +220 -103
  35. package/src/liveness.ts +192 -65
  36. package/src/obligation-expression.ts +2 -2
  37. package/src/obligation-source.ts +2 -0
  38. package/src/parallel-worker.ts +8 -3
  39. package/src/parallel.ts +56 -5
  40. package/src/parser.ts +436 -128
  41. package/src/pcal-ast.ts +770 -0
  42. package/src/pcal-parse.ts +1523 -0
  43. package/src/pcal-tokenize.ts +516 -0
  44. package/src/pcal-translate.ts +1230 -0
  45. package/src/pcal-validate.ts +454 -0
  46. package/src/randomization.ts +24 -7
  47. package/src/resolve.ts +1364 -405
  48. package/src/run-cli.ts +1167 -155
  49. package/src/runtime.ts +16 -0
  50. package/src/sany-builtins.ts +147 -0
  51. package/src/sany-generator.ts +3112 -0
  52. package/src/sany-levels.ts +1208 -0
  53. package/src/sany-lexer.ts +545 -0
  54. package/src/sany-linter.ts +122 -0
  55. package/src/sany-modules.ts +1398 -0
  56. package/src/sany-nodes.ts +463 -0
  57. package/src/sany-parse-error.ts +2161 -0
  58. package/src/sany-report.ts +354 -0
  59. package/src/sany-selectors.ts +738 -0
  60. package/src/sany-source.ts +248 -0
  61. package/src/sany-xml.ts +1017 -0
  62. package/src/spec-description.ts +26 -7
  63. package/src/standard-modules.ts +203 -146
  64. package/src/standard-ranges.ts +116 -0
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +221 -161
  67. package/src/substitution-levels.ts +38 -17
  68. package/src/successors.ts +160 -105
  69. package/src/symmetry.ts +180 -52
  70. package/src/tableau.ts +54 -25
  71. package/src/tlc-ext.ts +13 -17
  72. package/src/tlc-messages.ts +329 -0
  73. package/src/tlc-module.ts +46 -18
  74. package/src/tlc-order.ts +56 -6
  75. package/src/tlc-random.ts +37 -9
  76. package/src/tlc-replay.ts +655 -0
  77. package/src/tlc-summary.ts +20 -5
  78. package/src/tlc-version.ts +18 -0
  79. package/src/transfer.ts +15 -3
  80. package/src/value-sources.ts +29 -0
  81. package/src/values.ts +680 -178
package/src/liveness.ts CHANGED
@@ -1,14 +1,15 @@
1
- import { isLocallyResolved, type Definition, type Expression } from "./ast";
2
- import { TlaConfigError, TlaEvaluationError } from "./errors";
3
- import { EXIT_STATUS, setExitCode } from "./exit-codes";
1
+ import { isLocallyResolved, type Bound, type Definition, type Expression } from "./ast";
2
+ import { getUnexpectedException, TlaEvaluationError, TlaStackOverflowError } from "./errors";
4
3
  import {
5
- describeLocation,
4
+ describeNode,
6
5
  describeRange,
7
6
  bindDefinitions,
8
7
  bindOperatorArguments,
9
8
  evaluate,
10
9
  expectBoolean,
11
10
  forEachBinding,
11
+ forEachBoundElement,
12
+ getBoundElements,
12
13
  getDefinition,
13
14
  lookupScope,
14
15
  resolveApplyOperator,
@@ -22,6 +23,8 @@ import {
22
23
  import { mapChildren } from "./instances";
23
24
  import { ACTION_LEVEL, TEMPORAL_LEVEL } from "./levels";
24
25
  import { isEnabled } from "./successors";
26
+ import { EXIT_STATUS, setExitCode } from "./exit-codes";
27
+ import { isEvalException } from "./tlc-messages";
25
28
  import {
26
29
  createBoolean,
27
30
  createJunction,
@@ -38,8 +41,10 @@ export interface ScopedExpression {
38
41
  scope: Scope | undefined;
39
42
  }
40
43
 
41
- export interface NamedExpression extends ScopedExpression {
42
- name: string;
44
+ // A part of a temporal formula, with the name TLC gives the part when the formula is a property:
45
+ // a conjunct's location, or the name of the definition the part is the body of.
46
+ interface ExpandedExpression extends ScopedExpression {
47
+ name: string | undefined;
43
48
  }
44
49
 
45
50
  export interface TemporalProperty extends ScopedExpression {
@@ -48,6 +53,15 @@ export interface TemporalProperty extends ScopedExpression {
48
53
 
49
54
  const MAXIMUM_EXPANSION_DEPTH = 100;
50
55
 
56
+ export const createTautologyError = () =>
57
+ setExitCode(
58
+ new TlaEvaluationError("Temporal formula is a tautology (its negation is unsatisfiable)."),
59
+ EXIT_STATUS.livenessEvaluationFailure,
60
+ );
61
+
62
+ // TLC expands a temporal formula that refers to itself until Java's stack overflows.
63
+ const createStackOverflowError = () => setExitCode(new TlaStackOverflowError(), EXIT_STATUS.error);
64
+
51
65
  const TEMPORAL_OPERATORS = new Set(["~>", "-+->"]);
52
66
 
53
67
  const definitionLevels = new WeakMap<Definition, number>();
@@ -61,9 +75,10 @@ const getConstantContext = (environment: Environment, scope: Scope | undefined):
61
75
  next: undefined,
62
76
  });
63
77
 
64
- const createCannotHandleError = (expression: Expression) =>
78
+ // TLC adds the message of the error that stopped it, when it has one.
79
+ const createCannotHandleError = (expression: Expression, inner?: string) =>
65
80
  new TlaEvaluationError(
66
- `TLC cannot handle the temporal formula at ${describeLocation(expression)}`,
81
+ `TLC cannot handle the temporal formula ${describeRange(expression)}${inner === undefined ? "" : `:\n${inner}`}`,
67
82
  );
68
83
 
69
84
  export const getLevel = (
@@ -86,7 +101,7 @@ export const getLevel = (
86
101
  const definition = getDefinition(name, environment);
87
102
  if (definition) return getDefinitionLevel(definition, environment);
88
103
  const builtin = environment.builtins.get(environment.substitutions.get(name) ?? name);
89
- return builtin ? (builtin.isStateLevel ? 1 : 0) : undefined;
104
+ return builtin ? (builtin.isStateLevel && !builtin.isLevelConstant ? 1 : 0) : undefined;
90
105
  };
91
106
  const getNameLevel = (name: string, node: Expression) => {
92
107
  if (shadowed.has(name)) return 0;
@@ -150,25 +165,51 @@ const getDefinitionLevel = (definition: Definition, environment: Environment) =>
150
165
  return level;
151
166
  };
152
167
 
153
- export const TAUTOLOGY_MESSAGE = "Temporal formula is a tautology (its negation is unsatisfiable).";
168
+ // TLC leaves a formula whose sets it fails to enumerate, and doesn't expect an EvalException.
169
+ const getUnrolledElements = (bounds: Bound[], context: Context) => {
170
+ try {
171
+ return getBoundElements(bounds, context);
172
+ } catch (error) {
173
+ if (!(error instanceof TlaEvaluationError)) throw error;
174
+ if (!isEvalException(error)) return undefined;
175
+ throw setExitCode(
176
+ new TlaEvaluationError(
177
+ `${getUnexpectedException("tlc2.tool.EvalException")}${error.message}`,
178
+ ),
179
+ EXIT_STATUS.error,
180
+ );
181
+ }
182
+ };
154
183
 
155
- // TLC names each part of a property after the operator it expands, or after the location of the
156
- // conjunct or quantifier body it splits off.
157
184
  export const expandTemporal = (
158
185
  expression: Expression,
159
186
  scope: Scope | undefined,
160
187
  environment: Environment,
188
+ // Whether TLC expands the formula as a property, which unrolls each \A over a constant set.
161
189
  isProperty: boolean,
162
- name = "",
163
190
  expanding = new Set<Definition>(),
164
- ): NamedExpression[] => {
191
+ // Whether the expansion entered the body of an instance's definition. Like TLC, it then no
192
+ // longer expands an operator applied to arguments.
193
+ isSubstituted = false,
194
+ name?: string,
195
+ ): ExpandedExpression[] => {
165
196
  const expand = (
166
197
  child: Expression,
167
198
  childScope: Scope | undefined,
168
199
  childName = name,
169
200
  childExpanding = expanding,
170
- ): NamedExpression[] =>
171
- expandTemporal(child, childScope, environment, isProperty, childName, childExpanding);
201
+ definition?: Definition,
202
+ ): ExpandedExpression[] =>
203
+ expandTemporal(
204
+ child,
205
+ childScope,
206
+ environment,
207
+ isProperty,
208
+ childExpanding,
209
+ isSubstituted ||
210
+ (definition !== undefined && environment.substitutedDefinitions.has(definition)),
211
+ childName,
212
+ );
172
213
  if (expression.kind === "And") {
173
214
  return expression.operands.flatMap((operand) => expand(operand, scope, describeRange(operand)));
174
215
  }
@@ -191,22 +232,25 @@ export const expandTemporal = (
191
232
  getDefinitionLevel(definition, environment) > 0
192
233
  ) {
193
234
  if (expanding.has(definition)) {
194
- throw new TlaConfigError(`The definition of ${expression.name} refers to itself`);
235
+ throw createStackOverflowError();
195
236
  }
196
237
  return expand(
197
238
  definition.body,
198
239
  undefined,
199
- expression.name,
240
+ definition.name,
200
241
  new Set([...expanding, definition]),
242
+ definition,
201
243
  );
202
244
  }
203
245
  }
204
246
  const context = getConstantContext(environment, scope);
205
- // A property expands every operator it applies and unrolls every \A over constant sets, but a
206
- // specification only expands the temporal ones.
207
- const isExpanded = (child: Expression) =>
208
- isProperty || getLevel(child, scope, environment) === TEMPORAL_LEVEL;
209
- if (expression.kind === "Apply" && isExpanded(expression)) {
247
+ // TLC 1.7.4 never expands an operator applied to arguments. A property expands every one it
248
+ // applies, but a specification only the temporal ones.
249
+ if (
250
+ expression.kind === "Apply" &&
251
+ environment.runtime.version !== "1.7.4" &&
252
+ (isProperty || getLevel(expression, scope, environment) === TEMPORAL_LEVEL)
253
+ ) {
210
254
  const operator = resolveApplyOperator(expression, context);
211
255
  const definition = isLocallyResolved(expression)
212
256
  ? undefined
@@ -214,12 +258,15 @@ export const expandTemporal = (
214
258
  const isExpandable =
215
259
  operator?.kind === "Closure" &&
216
260
  operator.params.length === expression.args.length &&
217
- !definition?.isRecursive;
261
+ !definition?.isRecursive &&
262
+ !isSubstituted;
218
263
  if (isExpandable) {
219
264
  return expand(
220
265
  operator.body,
221
266
  bindOperatorArguments(operator, expression.args, context),
222
267
  expression.operator,
268
+ expanding,
269
+ definition,
223
270
  );
224
271
  }
225
272
  }
@@ -227,21 +274,14 @@ export const expandTemporal = (
227
274
  isProperty &&
228
275
  expression.kind === "Forall" &&
229
276
  expression.bounds.every((bound) => getLevel(bound.set, scope, environment) === 0);
230
- if (isUnrolled) {
231
- const expanded: NamedExpression[] = [];
232
- const bindings = { count: 0 };
233
- forEachBinding(expression.bounds, context, (boundScope) => {
234
- bindings.count++;
277
+ const sets = isUnrolled ? getUnrolledElements(expression.bounds, context) : undefined;
278
+ if (isUnrolled && sets) {
279
+ const expanded: ExpandedExpression[] = [];
280
+ forEachBoundElement(expression.bounds, sets, scope, (boundScope) => {
235
281
  expanded.push(...expand(expression.body, boundScope, describeRange(expression.body)));
236
282
  return false;
237
283
  });
238
- // TLC rejects a property that quantifies over no elements before it checks anything.
239
- if (bindings.count === 0) {
240
- throw setExitCode(
241
- new TlaEvaluationError(TAUTOLOGY_MESSAGE),
242
- EXIT_STATUS.livenessEvaluationFailure,
243
- );
244
- }
284
+ if (expanded.length === 0) throw createTautologyError();
245
285
  return expanded;
246
286
  }
247
287
  return [{ expression, scope, name }];
@@ -271,17 +311,19 @@ export const toLiveExpression = (
271
311
  rootScope: Scope | undefined,
272
312
  environment: Environment,
273
313
  ): LiveExpression => {
314
+ const is174 = environment.runtime.version === "1.7.4";
274
315
  const toAtom = (
275
316
  expression: Expression,
276
317
  scope: Scope | undefined,
277
318
  level: number,
278
319
  ): LiveExpression => {
279
- const atomLevel = level === 0 ? getLevel(expression, scope, environment) : level;
320
+ // TLC after 1.7.4 bounds the level of a formula whose parts are constant by the formula's own.
321
+ const atomLevel = level === 0 && !is174 ? getLevel(expression, scope, environment) : level;
280
322
  if (atomLevel === 0) {
281
323
  const value = evaluate(expression, getConstantContext(environment, scope));
282
324
  if (typeof value !== "boolean") {
283
325
  throw new TlaEvaluationError(
284
- `TLC expected a boolean in the temporal formula at ${describeLocation(expression)}`,
326
+ `TLC expected a boolean value, but did not find one. ${describeNode(expression)}`,
285
327
  );
286
328
  }
287
329
  return createBoolean(value);
@@ -299,9 +341,7 @@ export const toLiveExpression = (
299
341
  scope: Scope | undefined,
300
342
  ) => {
301
343
  if (expansion.depth >= MAXIMUM_EXPANSION_DEPTH) {
302
- throw new TlaEvaluationError(
303
- `TLC overflowed the stack while expanding the temporal formula at ${describeLocation(expression)}`,
304
- );
344
+ throw createStackOverflowError();
305
345
  }
306
346
  expansion.depth++;
307
347
  const result = convert(body, bodyScope);
@@ -321,7 +361,9 @@ export const toLiveExpression = (
321
361
  ? lookupScope(scope, name)
322
362
  : undefined;
323
363
  if (binding?.kind === "Thunk" && definitionThunks.has(binding)) {
324
- return expand(binding.expression, binding.scope, expression, scope);
364
+ return expandCall(expression, () =>
365
+ expand(binding.expression, binding.scope, expression, scope),
366
+ );
325
367
  }
326
368
  const operator =
327
369
  binding?.kind === "Operator"
@@ -333,32 +375,104 @@ export const toLiveExpression = (
333
375
  : resolveGlobalOperator(name, environment);
334
376
  if (operator?.kind !== "Closure") throw createCannotHandleError(expression);
335
377
  const args = expression.kind === "Apply" ? expression.args : [];
336
- return expand(operator.body, bindOperatorArguments(operator, args, context), expression, scope);
378
+ const isRecursive =
379
+ !isLocallyResolved(expression) && getDefinition(name, environment)?.isRecursive === true;
380
+ return expandCall(
381
+ expression,
382
+ () => {
383
+ // TLC 1.7.4 evaluates the arguments of an operator it expands.
384
+ if (is174) {
385
+ args
386
+ .filter((arg) => arg.kind !== "Lambda" && arg.kind !== "OperatorReference")
387
+ .forEach((arg) => evaluate(arg, context));
388
+ }
389
+ return expand(
390
+ operator.body,
391
+ bindOperatorArguments(operator, args, context),
392
+ expression,
393
+ scope,
394
+ );
395
+ },
396
+ isRecursive,
397
+ );
398
+ };
399
+
400
+ // TLC takes an application of an operator whose expansion fails for a formula it can't handle,
401
+ // except that TLC after 1.7.4 fails as the expansion of a recursive operator does.
402
+ const expandCall = (
403
+ expression: Expression,
404
+ expandBody: () => LiveExpression,
405
+ isRecursive = false,
406
+ ) => {
407
+ try {
408
+ return expandBody();
409
+ } catch (error) {
410
+ const isRethrown = error instanceof TlaStackOverflowError || (isRecursive && !is174);
411
+ if (!(error instanceof TlaEvaluationError) || isRethrown) throw error;
412
+ throw createCannotHandleError(expression);
413
+ }
337
414
  };
338
415
 
339
416
  const convertQuantifier = (
340
417
  expression: Expression & { kind: "Forall" | "Exists" },
341
418
  scope: Scope | undefined,
342
419
  ) => {
420
+ const level = getLevel(expression, scope, environment);
343
421
  const isConstant = expression.bounds.every(
344
422
  (bound) => getLevel(bound.set, scope, environment) === 0,
345
423
  );
346
- if (!isConstant) throw createCannotHandleError(expression);
424
+ if (!isConstant) {
425
+ if (level <= ACTION_LEVEL) return toAtom(expression, scope, level);
426
+ throw createCannotHandleError(expression);
427
+ }
347
428
  const operands: LiveExpression[] = [];
348
- forEachBinding(expression.bounds, getConstantContext(environment, scope), (boundScope) => {
349
- operands.push(convert(expression.body, boundScope));
350
- return false;
351
- });
352
429
  const isForall = expression.kind === "Forall";
430
+ // TLC takes a quantified formula whose body fails for one it can't handle, and under \A it adds
431
+ // the message of a failure that isn't an EvalException.
432
+ try {
433
+ forEachBinding(expression.bounds, getConstantContext(environment, scope), (boundScope) => {
434
+ operands.push(convert(expression.body, boundScope));
435
+ return false;
436
+ });
437
+ } catch (error) {
438
+ if (!(error instanceof TlaEvaluationError) || error instanceof TlaStackOverflowError) {
439
+ throw error;
440
+ }
441
+ if (level <= ACTION_LEVEL) return toAtom(expression, scope, level);
442
+ throw createCannotHandleError(
443
+ expression,
444
+ isForall && !isEvalException(error) ? error.message : undefined,
445
+ );
446
+ }
353
447
  if (operands.length === 0) return createBoolean(isForall);
354
- const result = createJunction(isForall ? "And" : "Or", operands);
355
- const level = getLiveLevel(result);
356
- return level > ACTION_LEVEL ? result : toAtom(expression, scope, level);
448
+ return toWhole(expression, scope, createJunction(isForall ? "And" : "Or", operands));
357
449
  };
358
450
 
451
+ // TLC takes a formula whose translated parts are below temporal level whole, at their level.
452
+ const toWhole = (expression: Expression, scope: Scope | undefined, parts: LiveExpression) => {
453
+ const level = getLiveLevel(parts);
454
+ return level > ACTION_LEVEL ? parts : toAtom(expression, scope, level);
455
+ };
456
+
457
+ // The formulas below temporal level whose parts TLC's Liveness.astToLive translates, as one
458
+ // that is constant, such as a quantifier over the empty set, can make the formula constant.
459
+ const isTranslatedWhole = (expression: Expression) =>
460
+ expression.kind === "Label" ||
461
+ expression.kind === "Let" ||
462
+ expression.kind === "And" ||
463
+ expression.kind === "Or" ||
464
+ expression.kind === "Forall" ||
465
+ expression.kind === "Exists" ||
466
+ expression.kind === "If" ||
467
+ (expression.kind === "Apply" &&
468
+ ((expression.operator === "~" && !isLocallyResolved(expression)) ||
469
+ expression.operator === "=>"));
470
+
359
471
  const convert = (expression: Expression, scope: Scope | undefined): LiveExpression => {
360
472
  const level = getLevel(expression, scope, environment);
361
- if (level <= ACTION_LEVEL) return toAtom(expression, scope, level);
473
+ if (level <= ACTION_LEVEL && !isTranslatedWhole(expression)) {
474
+ return toAtom(expression, scope, level);
475
+ }
362
476
  switch (expression.kind) {
363
477
  case "Label":
364
478
  return convert(expression.body, scope);
@@ -379,38 +493,51 @@ export const toLiveExpression = (
379
493
  );
380
494
  }
381
495
  if (expression.operator === "~" && !isLocallyResolved(expression)) {
382
- return createUnary("Not", convert(args[0], scope));
496
+ return toWhole(expression, scope, createUnary("Not", convert(args[0], scope)));
383
497
  }
384
498
  if (expression.operator === "=>") {
385
- return createPair(
386
- "Or",
387
- createUnary("Not", convert(args[0], scope)),
388
- convert(args[1], scope),
499
+ return toWhole(
500
+ expression,
501
+ scope,
502
+ createPair("Or", createUnary("Not", convert(args[0], scope)), convert(args[1], scope)),
389
503
  );
390
504
  }
391
505
  return convertCall(expression, scope);
392
506
  }
393
507
  case "And":
394
508
  case "Or":
395
- return createJunction(
396
- expression.kind,
397
- expression.operands.map((operand) => convert(operand, scope)),
509
+ return toWhole(
510
+ expression,
511
+ scope,
512
+ createJunction(
513
+ expression.kind,
514
+ expression.operands.map((operand) => convert(operand, scope)),
515
+ ),
398
516
  );
399
517
  case "Forall":
400
518
  case "Exists":
401
519
  return convertQuantifier(expression, scope);
402
520
  case "If": {
403
- if (getLevel(expression.condition, scope, environment) === 0) {
521
+ // TLC 1.7.4 translates both branches whatever the guard.
522
+ if (!is174 && getLevel(expression.condition, scope, environment) === 0) {
404
523
  const condition = evaluate(expression.condition, getConstantContext(environment, scope));
405
524
  if (typeof condition === "boolean") {
406
525
  return convert(condition ? expression.consequent : expression.alternative, scope);
407
526
  }
408
527
  }
409
528
  const condition = convert(expression.condition, scope);
410
- return createPair(
411
- "Or",
412
- createPair("And", condition, convert(expression.consequent, scope)),
413
- createPair("And", createUnary("Not", condition), convert(expression.alternative, scope)),
529
+ return toWhole(
530
+ expression,
531
+ scope,
532
+ createPair(
533
+ "Or",
534
+ createPair("And", condition, convert(expression.consequent, scope)),
535
+ createPair(
536
+ "And",
537
+ createUnary("Not", condition),
538
+ convert(expression.alternative, scope),
539
+ ),
540
+ ),
414
541
  );
415
542
  }
416
543
  case "Always":
@@ -874,7 +874,7 @@ export const mapSelectorArgs = (selector: Selector, map: (arg: Expr) => Expr): S
874
874
  ? { ...selector, args: selector.args.map(map) }
875
875
  : selector;
876
876
 
877
- export const mapExceptSpecs = (specs: ExceptSpec[], map: (inner: Expr) => Expr): ExceptSpec[] =>
877
+ const mapExceptSpecs = (specs: ExceptSpec[], map: (inner: Expr) => Expr): ExceptSpec[] =>
878
878
  specs.map((spec) => {
879
879
  const value = map(spec.value);
880
880
  const trail = spec.trail.map((point): ExceptPoint =>
@@ -883,7 +883,7 @@ export const mapExceptSpecs = (specs: ExceptSpec[], map: (inner: Expr) => Expr):
883
883
  return { trail, value };
884
884
  });
885
885
 
886
- export const mapDefnBody = (definition: Defn, map: (body: Expr) => Expr): Defn =>
886
+ const mapDefnBody = (definition: Defn, map: (body: Expr) => Expr): Defn =>
887
887
  definition.kind === "Operator" || definition.kind === "Bpragma"
888
888
  ? { ...definition, body: map(definition.body) }
889
889
  : definition;
@@ -609,6 +609,8 @@ export const createSource = (file: string, text: string, counter: Counter, isExp
609
609
  );
610
610
  case "ExceptAt":
611
611
  return at({ kind: "At", isExcept }, { location });
612
+ case "Bracket":
613
+ throw new TlaProofError("Unexpected [ without a function or subscript");
612
614
  case "FunctionApplication":
613
615
  return at(
614
616
  {
@@ -15,10 +15,10 @@ import type {
15
15
  WorkerMessage,
16
16
  } from "./parallel";
17
17
  import { getSuccessors, type Successor } from "./successors";
18
- import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
18
+ import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
19
19
  import { OUT_DEGREE_BUCKETS } from "./tlc-summary";
20
20
  import { createStateDecoder, createStateEncoder, type EncodedStates } from "./transfer";
21
- import { getRankedStringCount, setStringOrder, type Value } from "./values";
21
+ import { getRankedStringCount, setStringOrder, setValueVersion, type Value } from "./values";
22
22
 
23
23
  const WORKER_THREADS_MODULE = "node:worker_threads";
24
24
  const LOCKED = -1;
@@ -51,6 +51,7 @@ const run = (start: StartMessage, parent: Port) => {
51
51
  const loadModule = (name: string) => modules.get(name);
52
52
  const classes = new Map(start.classes);
53
53
  setStringOrder(getTlcStringOrder(start.source, start.config, loadModule));
54
+ setValueVersion(start.version);
54
55
  const startupStrings = getRankedStringCount();
55
56
  const specification = createEnvironment(start.config, {
56
57
  source: start.source,
@@ -58,10 +59,14 @@ const run = (start: StartMessage, parent: Port) => {
58
59
  loadClass: (name) => classes.get(name),
59
60
  print: () => {},
60
61
  files: { readFile: undefined, writeFile: undefined },
62
+ isTool: start.isTool,
63
+ isTraceRecorded: start.isTraceRecorded,
64
+ isWarningEnabled: start.isWarningEnabled,
65
+ version: start.version,
61
66
  });
62
67
  const environment = specification.environment;
63
68
  const search = createSearch(start.config, environment, () =>
64
- getTlcVariableOrder(start.source, loadModule),
69
+ getTlcDeclarationOrder(start.source, loadModule, "variable"),
65
70
  );
66
71
  const nextAction = search?.spec.next;
67
72
  if (!search || nextAction === undefined)
package/src/parallel.ts CHANGED
@@ -4,8 +4,12 @@ import {
4
4
  checkSequentially,
5
5
  createSearch,
6
6
  findAssumptionViolation,
7
+ reportInitialStatesProgress,
8
+ reportStartup,
7
9
  type CheckOptions,
10
+ type CheckProgress,
8
11
  type CheckResult,
12
+ type InitialStates,
9
13
  } from "./check";
10
14
  import { parseConfig, type ModelConfig } from "./config";
11
15
  import { createEnvironment } from "./environment";
@@ -23,7 +27,8 @@ import { getInitialStates } from "./successors";
23
27
  import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
24
28
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
25
29
  import { createStateEncoder, type EncodedStates } from "./transfer";
26
- import { getRankedStringCount, setStringOrder, type Value } from "./values";
30
+ import type { TlcVersion } from "./tlc-version";
31
+ import { getRankedStringCount, setStringOrder, setValueVersion, type Value } from "./values";
27
32
 
28
33
  const WORKER_THREADS_MODULE = "node:worker_threads";
29
34
 
@@ -43,6 +48,10 @@ export interface StartMessage {
43
48
  index: number;
44
49
  ports: (Port | undefined)[];
45
50
  seed: EncodedStates;
51
+ isTool: boolean | undefined;
52
+ isTraceRecorded: boolean | undefined;
53
+ isWarningEnabled: boolean | undefined;
54
+ version: TlcVersion | undefined;
46
55
  fingerprints: SharedFingerprints;
47
56
  requests: Int32Array;
48
57
  remaining: Int32Array;
@@ -101,16 +110,36 @@ const spawnWorker = async (onMessage: (message: WorkerMessage) => void): Promise
101
110
  };
102
111
 
103
112
  export const check = async (options: CheckOptions): Promise<CheckResult> => {
104
- const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
113
+ const config =
114
+ typeof options.config === "string"
115
+ ? parseConfig(options.config, options.version)
116
+ : options.config;
105
117
  const print = options.onPrint ?? console.log;
106
118
  const printed = { count: 0 };
107
- // The search on the calling thread prints what this one printed while it started, so it skips that.
119
+ // What each point the search reports is about, such as the invariant TLC warns about.
120
+ const getKey = (progress: CheckProgress) =>
121
+ [
122
+ progress.kind,
123
+ "name" in progress ? progress.name : "",
124
+ "count" in progress ? progress.count : "",
125
+ "text" in progress ? progress.text : "",
126
+ ].join(" ");
127
+ const reported = new Set<string>();
128
+ const report = (progress: CheckProgress) => {
129
+ reported.add(getKey(progress));
130
+ options.onProgress?.(progress);
131
+ };
132
+ // The search on the calling thread prints and reports what this one did while it started, so it
133
+ // skips that.
108
134
  const fallBack = () => {
109
135
  const skipped = { count: printed.count };
110
136
  return checkSequentially({
111
137
  ...options,
112
138
  config,
113
139
  onPrint: (text) => (skipped.count-- > 0 ? undefined : print(text)),
140
+ onProgress: (progress) => {
141
+ if (!reported.has(getKey(progress))) options.onProgress?.(progress);
142
+ },
114
143
  });
115
144
  };
116
145
  const count = options.workers ?? 1;
@@ -137,6 +166,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
137
166
  return bytes;
138
167
  };
139
168
  setStringOrder(getTlcStringOrder(options.source, config, loadModule));
169
+ setValueVersion(options.version);
140
170
  const startupStrings = getRankedStringCount();
141
171
  const specification = createEnvironment(config, {
142
172
  source: options.source,
@@ -147,10 +177,19 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
147
177
  print(text);
148
178
  },
149
179
  files: { readFile: options.readFile, writeFile: options.writeFile },
180
+ isTool: options.isTool,
181
+ isTraceRecorded: options.isTraceRecorded,
182
+ isSanyReported: options.isSanyReported,
183
+ isWarningEnabled: options.isWarningEnabled,
184
+ version: options.version,
150
185
  });
186
+ reportStartup(specification, config, report);
151
187
  const environment = specification.environment;
152
- const search = createSearch(config, environment, () =>
153
- getTlcVariableOrder(options.source, loadModule),
188
+ const search = createSearch(
189
+ config,
190
+ environment,
191
+ () => getTlcVariableOrder(options.source, loadModule),
192
+ report,
154
193
  );
155
194
  if (findAssumptionViolation(specification.assumptions, environment)) return fallBack();
156
195
  if (
@@ -172,6 +211,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
172
211
  distinctStates: 0,
173
212
  queuedStates: 0,
174
213
  depth: 0,
214
+ initialStates: undefined as InitialStates | undefined,
175
215
  outDegrees: Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0),
176
216
  };
177
217
  const tlcFingerprints: BigInt64Array[] = [];
@@ -196,6 +236,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
196
236
  const seedInitialStates = (init: Expression) => {
197
237
  const seeded: Value[][] = [];
198
238
  for (const state of getInitialStates(init, environment, search.spec.scope)) {
239
+ reportInitialStatesProgress(statistics.generatedStates, report);
199
240
  canonicalize(state);
200
241
  statistics.generatedStates++;
201
242
  writeStateFingerprint(search.getFingerprintState(state), fingerprint);
@@ -217,6 +258,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
217
258
  return true;
218
259
  };
219
260
  // The sequential search reports a violation or a failed evaluation in the initial states.
261
+ report({ kind: "initial-states-started" });
220
262
  try {
221
263
  if (!seedInitialStates(search.spec.init)) return fallBack();
222
264
  } catch (error) {
@@ -225,6 +267,11 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
225
267
  }
226
268
  if (getRankedStringCount() !== startupStrings) return fallBack();
227
269
  statistics.depth = statistics.generatedStates === 0 ? 0 : 1;
270
+ statistics.initialStates = {
271
+ generated: statistics.generatedStates,
272
+ distinct: statistics.distinctStates,
273
+ };
274
+ report({ kind: "initial-states-computed", initialStates: statistics.initialStates });
228
275
  if (statistics.distinctStates === 0) return createSuccess();
229
276
 
230
277
  const reports: WorkerMessage[] = [];
@@ -266,6 +313,10 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
266
313
  index,
267
314
  ports,
268
315
  seed,
316
+ isTool: options.isTool,
317
+ isTraceRecorded: options.isTraceRecorded,
318
+ isWarningEnabled: options.isWarningEnabled,
319
+ version: options.version,
269
320
  fingerprints: shared.fingerprints,
270
321
  requests,
271
322
  remaining,