tla-ts 0.0.3-dev.b74f28c → 0.0.3-dev.bb5b28b

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 (50) hide show
  1. package/README.md +16 -2
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +53 -22
  4. package/dist/index.d.ts +53 -22
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +1 -1
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +19 -16
  11. package/dist/run-cli.js +20 -17
  12. package/dist/transfer.cjs +37 -16
  13. package/dist/transfer.js +37 -16
  14. package/package.json +1 -1
  15. package/src/ast.ts +93 -0
  16. package/src/check.ts +496 -98
  17. package/src/cli-options.ts +11 -5
  18. package/src/codegen.ts +15 -22
  19. package/src/config.ts +224 -170
  20. package/src/dependencies.ts +2 -2
  21. package/src/environment.ts +116 -28
  22. package/src/errors.ts +76 -3
  23. package/src/evaluate.ts +251 -156
  24. package/src/exit-codes.ts +39 -42
  25. package/src/instances.ts +16 -15
  26. package/src/levels.ts +62 -34
  27. package/src/lexer.ts +5 -1
  28. package/src/live-check.ts +772 -0
  29. package/src/liveness.ts +120 -543
  30. package/src/parallel-worker.ts +7 -3
  31. package/src/parallel.ts +20 -4
  32. package/src/parser.ts +33 -11
  33. package/src/resolve.ts +122 -44
  34. package/src/run-cli.ts +99 -19
  35. package/src/runtime.ts +3 -1
  36. package/src/spec-description.ts +19 -8
  37. package/src/standard-modules.ts +9 -8
  38. package/src/state-graph.ts +157 -0
  39. package/src/subexpressions.ts +10 -10
  40. package/src/successors.ts +146 -35
  41. package/src/symmetry.ts +16 -6
  42. package/src/tableau.ts +51 -24
  43. package/src/tlc-ext.ts +29 -12
  44. package/src/tlc-module.ts +14 -33
  45. package/src/tlc-modules.ts +4 -0
  46. package/src/tlc-order.ts +25 -7
  47. package/src/tlc-random.ts +9 -6
  48. package/src/tlc-summary.ts +20 -5
  49. package/src/value-sources.ts +29 -0
  50. package/src/values.ts +167 -52
package/src/exit-codes.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { LivenessChecker } from "./liveness";
1
+ import type { LivenessChecker } from "./live-check";
2
2
 
3
3
  // TLC's exit statuses, from EC.ExitStatus.
4
4
  export const EXIT_STATUS = {
@@ -17,12 +17,6 @@ export const EXIT_STATUS = {
17
17
  } as const;
18
18
 
19
19
  const exitCodes = new WeakMap<Error, number>();
20
- const moduleExitCodes = new WeakMap<Error, number>();
21
- const phase = { isNextState: false };
22
-
23
- export const setNextStatePhase = (isNextState: boolean) => {
24
- phase.isNextState = isNextState;
25
- };
26
20
 
27
21
  // The innermost site that knows TLC's exit status for an error sets it first and wins.
28
22
  export const setExitCode = <Thrown>(error: Thrown, code: number) => {
@@ -30,46 +24,49 @@ export const setExitCode = <Thrown>(error: Thrown, code: number) => {
30
24
  return error;
31
25
  };
32
26
 
33
- // TLC exits with the code of an exception from a standard module's Java code only when the exception
34
- // escapes the computation of next states. In every other phase, the phase decides the code.
35
- export const setModuleExitCode = (error: Error, code: number) => {
36
- if (phase.isNextState) moduleExitCodes.set(error, code);
27
+ export const getExitCode = (error: Error) => exitCodes.get(error);
28
+
29
+ const ownExitCodes = new WeakMap<Error, number>();
30
+
31
+ // The exit status of the error code TLC gives an EvalException, which TLC uses only where it
32
+ // doesn't report the error again as a nested expression.
33
+ export const setOwnExitCode = <Thrown extends Error>(error: Thrown, code: number) => {
34
+ ownExitCodes.set(error, code);
35
+ return error;
37
36
  };
38
37
 
39
- export const getExitCode = (error: Error) => exitCodes.get(error) ?? moduleExitCodes.get(error);
38
+ export const getOwnExitCode = (error: unknown) =>
39
+ error instanceof Error ? ownExitCodes.get(error) : undefined;
40
40
 
41
- // TLC exits with 255 when it fails to process or evaluate a liveness property, in any phase.
42
- export const withLivenessExitCode = (
43
- create: () => LivenessChecker | undefined,
44
- ): LivenessChecker | undefined => {
41
+ // TLC exits with 255 when it fails to process or evaluate a liveness property.
42
+ const withErrorExitCode = <Result>(run: () => Result) => {
45
43
  try {
46
- const checker = create();
47
- return (
48
- checker && {
49
- addInitialState: (index, state) => {
50
- try {
51
- checker.addInitialState(index, state);
52
- } catch (error) {
53
- throw setExitCode(error, EXIT_STATUS.error);
54
- }
55
- },
56
- addState: (index, state, successors) => {
57
- try {
58
- return checker.addState(index, state, successors);
59
- } catch (error) {
60
- throw setExitCode(error, EXIT_STATUS.error);
61
- }
62
- },
63
- findViolation: () => {
64
- try {
65
- return checker.findViolation();
66
- } catch (error) {
67
- throw setExitCode(error, EXIT_STATUS.error);
68
- }
69
- },
70
- }
71
- );
44
+ return run();
72
45
  } catch (error) {
73
46
  throw setExitCode(error, EXIT_STATUS.error);
74
47
  }
75
48
  };
49
+
50
+ export const withLivenessExitCode = (
51
+ create: () => LivenessChecker | undefined,
52
+ ): LivenessChecker | undefined => {
53
+ const checker = withErrorExitCode(create);
54
+ return (
55
+ checker && {
56
+ // TLC replays the initial states to report an error, but skips the liveness check of a
57
+ // state it has already seen, so it exits with the status of the error's own code.
58
+ addInitialState: (index, state) => {
59
+ try {
60
+ checker.addInitialState(index, state);
61
+ } catch (error) {
62
+ throw setExitCode(error, getOwnExitCode(error) ?? EXIT_STATUS.error);
63
+ }
64
+ },
65
+ addState: (index, state, successors) =>
66
+ withErrorExitCode(() => checker.addState(index, state, successors)),
67
+ findViolation: () => withErrorExitCode(() => checker.findViolation()),
68
+ getViolatedProperties: (path, cycleStart) =>
69
+ withErrorExitCode(() => checker.getViolatedProperties(path, cycleStart)),
70
+ }
71
+ );
72
+ };
package/src/instances.ts CHANGED
@@ -1,16 +1,17 @@
1
- import type {
2
- Bound,
3
- BoundPattern,
4
- Definition,
5
- Expression,
6
- Instance,
7
- Module,
8
- OperatorParam,
9
- SourceLocation,
10
- Theorem,
1
+ import {
2
+ getChildren,
3
+ isLocallyResolved,
4
+ type Bound,
5
+ type BoundPattern,
6
+ type Definition,
7
+ type Expression,
8
+ type Instance,
9
+ type Module,
10
+ type OperatorParam,
11
+ type SourceLocation,
12
+ type Theorem,
11
13
  } from "./ast";
12
14
  import { TlaSyntaxError } from "./errors";
13
- import { getChildren, isLocallyResolved } from "./evaluate";
14
15
  import { getArity, getBoundNames, getPatternNames, isBefore } from "./resolve";
15
16
 
16
17
  export const substitutedExpressions = new WeakSet<Expression>();
@@ -163,7 +164,7 @@ export const mapChildren = (
163
164
  case "RecordSet":
164
165
  return {
165
166
  ...expression,
166
- fields: expression.fields.map((field) => ({ name: field.name, value: plain(field.value) })),
167
+ fields: expression.fields.map((field) => ({ ...field, value: plain(field.value) })),
167
168
  };
168
169
  case "Except":
169
170
  return {
@@ -218,7 +219,7 @@ interface Instantiation {
218
219
  variables: Set<string>;
219
220
  getDefinition: (key: string) => Definition | undefined;
220
221
  isParameterized: (key: string) => boolean;
221
- addDefinition: (name: string, create: (key: string) => Definition) => string;
222
+ addDefinition: (source: string, name: string, create: (key: string) => Definition) => string;
222
223
  createName: (name: string) => string;
223
224
  }
224
225
 
@@ -639,7 +640,7 @@ const copyDefinition = (rewrite: Rewrite, key: string): string => {
639
640
  const instantiation = rewrite.instantiation;
640
641
  const definition = instantiation.getDefinition(key);
641
642
  if (!definition) return key;
642
- return instantiation.addDefinition(definition.name, (copyKey) => {
643
+ return instantiation.addDefinition(key, definition.name, (copyKey) => {
643
644
  rewrite.copies.set(key, copyKey);
644
645
  const rename = createRenamer(rewrite);
645
646
  const mapped = renameParams(
@@ -678,7 +679,7 @@ export const createInstantiation = (instantiation: Instantiation) => {
678
679
  if (copy !== undefined) return copy;
679
680
  const definition = instantiation.getDefinition(key);
680
681
  if (!definition) return key;
681
- return instantiation.addDefinition(definition.name, () =>
682
+ return instantiation.addDefinition(key, definition.name, () =>
682
683
  withInstanceParams(definition, instantiation.params, definition.location),
683
684
  );
684
685
  },
package/src/levels.ts CHANGED
@@ -1,15 +1,14 @@
1
- import type {
2
- AssumeProve,
3
- Definition,
4
- Expression,
5
- Module,
6
- OperatorParam,
7
- SourceLocation,
1
+ import {
2
+ getChildren,
3
+ type AssumeProve,
4
+ type Definition,
5
+ type Expression,
6
+ type Module,
7
+ type OperatorParam,
8
8
  } from "./ast";
9
- import { TlaSyntaxError } from "./errors";
10
- import { getChildren } from "./evaluate";
9
+ import { createSemanticError, TlaSyntaxError } from "./errors";
11
10
  import { mapChildren } from "./instances";
12
- import { getBoundNames, getPatternNames } from "./resolve";
11
+ import { collectSemanticErrors, getBoundNames, getPatternNames } from "./resolve";
13
12
  import { STANDARD_LEVELS } from "./standard-levels";
14
13
 
15
14
  interface LevelData {
@@ -176,8 +175,13 @@ export const ACTION_LEVEL = 2;
176
175
 
177
176
  export const TEMPORAL_LEVEL = 3;
178
177
 
179
- const createLevelError = (message: string, location: SourceLocation) =>
180
- new TlaSyntaxError(`${message} at line ${location.line}, column ${location.column}`);
178
+ // SANY reports a level error at the range of the expression it rejects.
179
+ const createLevelError = (message: string, expression: Expression) =>
180
+ expression.location.range
181
+ ? createSemanticError(`${message}.`, expression.location.range)
182
+ : new TlaSyntaxError(
183
+ `${message} at line ${expression.location.line}, column ${expression.location.column}`,
184
+ );
181
185
 
182
186
  const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
183
187
 
@@ -237,13 +241,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
237
241
  if (operand.operator || operand.declared) return;
238
242
  if (operand.data.level > getMaxLevel(operator, index)) {
239
243
  throw new TlaSyntaxError(
240
- `Level error in applying operator ${operator.name}: The level of argument ${index + 1} exceeds the maximum level allowed by the operator.`,
244
+ `Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
241
245
  );
242
246
  }
243
247
  });
244
248
  if (ranges.some((range) => range.level > ACTION_LEVEL)) {
245
249
  throw new TlaSyntaxError(
246
- `Level error in applying operator ${operator.name}: The level of the range for a bounded variable exceeds the maximum level allowed by the operator.`,
250
+ `Level error in applying operator ${operator.name}:\nThe level of the range for a bounded variable exceeds the maximum level allowed by the operator`,
247
251
  );
248
252
  }
249
253
  const getOperand = (formal: string) => {
@@ -444,10 +448,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
444
448
  ? applyDeclared(resolved.name, resolved.level, operands)
445
449
  : applyDefined(resolved.levels, operands);
446
450
 
447
- const checkJunction = (operands: Operand[], location: SourceLocation) => {
451
+ const checkJunction = (operands: Operand[], expression: Expression) => {
448
452
  const levels = operands.map((operand) => operand.data.level);
449
453
  if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
450
- throw createLevelError("Formula has both temporal formula and action as arguments", location);
454
+ throw createLevelError(
455
+ "Formula has both temporal formula and action as arguments",
456
+ expression,
457
+ );
451
458
  }
452
459
  };
453
460
 
@@ -456,10 +463,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
456
463
  if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
457
464
  throw createLevelError(
458
465
  "Action used where only temporal formula or state predicate allowed",
459
- expression.location,
466
+ expression,
460
467
  );
461
468
  }
462
- if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression.location);
469
+ if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
463
470
  };
464
471
 
465
472
  const checkTemporal = (
@@ -473,7 +480,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
473
480
  expression.kind === "Always"
474
481
  ? "[] followed by action not of form [A]_v"
475
482
  : "<> followed by action not of form <<A>>_v",
476
- expression.location,
483
+ expression,
477
484
  );
478
485
  };
479
486
 
@@ -501,7 +508,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
501
508
  return data;
502
509
  };
503
510
 
511
+ // SANY reports a level error of an operator's arguments at the application.
504
512
  const analyze = (expression: Expression, scope: Scope): LevelData => {
513
+ try {
514
+ return analyzeNode(expression, scope);
515
+ } catch (error) {
516
+ if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
517
+ throw createLevelError(error.message, expression);
518
+ }
519
+ };
520
+
521
+ const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
505
522
  switch (expression.kind) {
506
523
  case "Number":
507
524
  case "Decimal":
@@ -521,7 +538,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
521
538
  case "And":
522
539
  case "Or": {
523
540
  const operands = toOperands(expression.operands, scope);
524
- checkJunction(operands, expression.location);
541
+ checkJunction(operands, expression);
525
542
  return applyDefined(JUNCTION_LEVELS, operands);
526
543
  }
527
544
  case "Prime":
@@ -561,13 +578,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
561
578
  expression.bounds.map((bound) => bound.set),
562
579
  getBoundNames(expression.bounds),
563
580
  );
564
- if (
565
- data.level === TEMPORAL_LEVEL &&
566
- expression.bounds.some((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
567
- ) {
581
+ const actionBound =
582
+ data.level === TEMPORAL_LEVEL
583
+ ? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
584
+ : undefined;
585
+ if (actionBound) {
568
586
  throw createLevelError(
569
587
  "Action-level bound of quantified temporal formula",
570
- expression.location,
588
+ actionBound.set,
571
589
  );
572
590
  }
573
591
  return data;
@@ -838,13 +856,23 @@ const analyzeAssumeProve = (
838
856
  // SANY checks the level of every definition, assumption, and theorem of a module.
839
857
  export const checkModuleLevels = (module: Module, context: LevelContext) => {
840
858
  const analyzer = createLevelAnalyzer(context);
841
- module.definitions
842
- .filter((definition) => !definition.name.includes("!"))
843
- .forEach((definition) => analyzer.analyzeDefinition(definition, new Map()));
844
- module.assumptions.forEach((assumption) => analyzer.analyze(assumption.expression, new Map()));
845
- module.theorems.forEach((theorem) =>
846
- theorem.statement.kind === "AssumeProve"
847
- ? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
848
- : analyzer.analyze(theorem.statement, new Map()),
849
- );
859
+ collectSemanticErrors([
860
+ ...module.definitions
861
+ .filter((definition) => !definition.name.includes("!"))
862
+ .map((definition) => ({
863
+ location: definition.location,
864
+ check: () => analyzer.analyzeDefinition(definition, new Map()),
865
+ })),
866
+ ...module.assumptions.map((assumption) => ({
867
+ location: assumption.location,
868
+ check: () => analyzer.analyze(assumption.expression, new Map()),
869
+ })),
870
+ ...module.theorems.map((theorem) => ({
871
+ location: theorem.location,
872
+ check: () =>
873
+ theorem.statement.kind === "AssumeProve"
874
+ ? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
875
+ : analyzer.analyze(theorem.statement, new Map()),
876
+ })),
877
+ ]);
850
878
  };
package/src/lexer.ts CHANGED
@@ -345,6 +345,8 @@ export const tokenize = (source: string): Token[] => {
345
345
  if (!headerMatch)
346
346
  throw new TlaSyntaxError("Expected a module header such as ---- MODULE Name ----");
347
347
  const tokens: Token[] = [];
348
+ // Each name is one string object, so looking a name up in a scope compares pointers.
349
+ const names = new Map<string, string>();
348
350
  const prefixLines = source.slice(0, headerMatch.index).split("\n");
349
351
  const cursor = {
350
352
  index: headerMatch.index,
@@ -481,7 +483,9 @@ export const tokenize = (source: string): Token[] => {
481
483
  pushToken("Keyword", word.slice(0, 3), 3);
482
484
  continue;
483
485
  }
484
- pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", word, word.length);
486
+ const name = names.get(word) ?? word;
487
+ names.set(name, name);
488
+ pushToken(KEYWORDS.has(word) ? "Keyword" : "Identifier", name, word.length);
485
489
  continue;
486
490
  }
487
491
  const stepLength = matchStep(source, cursor.index);