tla-ts 0.0.3-dev.3cf2ad0 → 0.0.3-dev.3ff2308

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 (51) hide show
  1. package/README.md +16 -2
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +54 -22
  4. package/dist/index.d.ts +54 -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/builtins.ts +9 -3
  17. package/src/check.ts +511 -99
  18. package/src/cli-options.ts +11 -5
  19. package/src/codegen.ts +15 -22
  20. package/src/config.ts +224 -170
  21. package/src/dependencies.ts +2 -2
  22. package/src/environment.ts +121 -29
  23. package/src/errors.ts +87 -3
  24. package/src/evaluate.ts +255 -159
  25. package/src/exit-codes.ts +39 -42
  26. package/src/instances.ts +16 -15
  27. package/src/levels.ts +62 -34
  28. package/src/lexer.ts +5 -1
  29. package/src/live-check.ts +772 -0
  30. package/src/liveness.ts +120 -543
  31. package/src/parallel-worker.ts +7 -3
  32. package/src/parallel.ts +20 -4
  33. package/src/parser.ts +33 -11
  34. package/src/resolve.ts +122 -44
  35. package/src/run-cli.ts +112 -20
  36. package/src/runtime.ts +3 -1
  37. package/src/spec-description.ts +19 -8
  38. package/src/standard-modules.ts +52 -43
  39. package/src/state-graph.ts +157 -0
  40. package/src/subexpressions.ts +10 -10
  41. package/src/successors.ts +183 -39
  42. package/src/symmetry.ts +25 -7
  43. package/src/tableau.ts +51 -24
  44. package/src/tlc-ext.ts +29 -12
  45. package/src/tlc-module.ts +14 -33
  46. package/src/tlc-modules.ts +4 -0
  47. package/src/tlc-order.ts +25 -7
  48. package/src/tlc-random.ts +9 -6
  49. package/src/tlc-summary.ts +20 -5
  50. package/src/value-sources.ts +29 -0
  51. package/src/values.ts +207 -53
@@ -1,23 +1,30 @@
1
- import type {
2
- Assumption,
3
- Declaration,
4
- Definition,
5
- Expression,
6
- Instance,
7
- Module,
8
- OperatorParam,
9
- SourceLocation,
1
+ import {
2
+ getChildren,
3
+ type Assumption,
4
+ type Declaration,
5
+ type Definition,
6
+ type Expression,
7
+ type Instance,
8
+ type Module,
9
+ type OperatorParam,
10
+ type SourceLocation,
10
11
  } from "./ast";
11
12
  import { BUNDLED_MODULES } from "./bundled-modules";
12
13
  import type { ConfigValue, ModelConfig } from "./config";
13
14
  import type { FileSystem } from "./json";
14
- import { TlaConfigError, TlaEvaluationError, TlaSyntaxError } from "./errors";
15
+ import {
16
+ createSanyError,
17
+ TlaConfigError,
18
+ TlaEvaluationError,
19
+ TlaModuleError,
20
+ TlaSyntaxError,
21
+ } from "./errors";
15
22
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
16
23
  import {
17
24
  createBigNumberError,
18
25
  createRealNumberError,
19
- getChildren,
20
- isConstantLevel,
26
+ evaluate,
27
+ getDefinition,
21
28
  isTlcInteger,
22
29
  type Builtin,
23
30
  type Environment,
@@ -44,8 +51,9 @@ import {
44
51
  import { createRuntime } from "./runtime";
45
52
  import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
46
53
  import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
47
- import { checkModuleLevels } from "./levels";
54
+ import { checkModuleLevels, createLevelAnalyzer } from "./levels";
48
55
  import { checkInstances, type InstanceCheck } from "./substitution-levels";
56
+ import { getTlcDeclarationOrder } from "./tlc-order";
49
57
  import {
50
58
  checkInteger,
51
59
  createModelValue,
@@ -212,6 +220,8 @@ export const createEnvironment = (
212
220
  const root = parseModule(options.source);
213
221
  const definitions = new Map<string, Definition>();
214
222
  const parameterized = new Map<string, boolean>();
223
+ // The definition each instance copy came from, and the instance parameters the copy adds.
224
+ const copySources = new Map<string, { source: string; prefix: number }>();
215
225
  const builtins = new Map<string, Builtin>(
216
226
  CORE_OPERATORS.map((operator) => [operator.name, operator]),
217
227
  );
@@ -256,7 +266,11 @@ export const createEnvironment = (
256
266
 
257
267
  const getStandardSymbols = (name: string) => {
258
268
  const symbols = addStandardModule(name);
259
- if (symbols) useModule(name, STANDARD_DEPENDENCIES.get(name) ?? []);
269
+ const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
270
+ if (symbols) useModule(name, dependencies);
271
+ // TLC loads the Java class of each module a standard module uses, so one that uses Integers
272
+ // overrides the arithmetic of Naturals.
273
+ if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
260
274
  return symbols;
261
275
  };
262
276
 
@@ -285,13 +299,25 @@ export const createEnvironment = (
285
299
  BUNDLED_MODULES.get(name) ??
286
300
  extractModule(options.source, name);
287
301
  if (source === undefined)
288
- throw new TlaSyntaxError(
289
- `Cannot find source file for module ${name} imported in module ${importer}.`,
302
+ throw createSanyError(
303
+ [
304
+ {
305
+ location: `In module ${importer}`,
306
+ message: `Cannot find source file for module ${name} imported in module ${importer}.`,
307
+ },
308
+ ],
309
+ true,
290
310
  );
291
311
  const module = parseModule(source);
292
312
  if (module.name !== name) {
293
- throw new TlaSyntaxError(
294
- `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
313
+ throw createSanyError(
314
+ [
315
+ {
316
+ location: "Unknown location",
317
+ message: `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
318
+ },
319
+ ],
320
+ true,
295
321
  );
296
322
  }
297
323
  return rejectModuleLiterals(module);
@@ -379,10 +405,11 @@ export const createEnvironment = (
379
405
  variables: new Set(linked.variables),
380
406
  getDefinition: (key) => definitions.get(key),
381
407
  isParameterized: (key) => parameterized.get(key) === true,
382
- addDefinition: (name, create) => {
408
+ addDefinition: (source, name, create) => {
383
409
  const key = createName(`${name}@${module.name}`);
384
410
  definitions.set(key, create(key));
385
411
  parameterized.set(key, !isParameterFree);
412
+ copySources.set(key, { source, prefix: instance.params.length });
386
413
  return key;
387
414
  },
388
415
  createName,
@@ -544,7 +571,7 @@ export const createEnvironment = (
544
571
  variables: new Set(linked.variables),
545
572
  getDefinition: (key) => definitions.get(key),
546
573
  isParameterized: (key) => parameterized.get(key) === true,
547
- addDefinition: (name, create) => {
574
+ addDefinition: (_source, name, create) => {
548
575
  const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
549
576
  const definition = create(key);
550
577
  copies.push(
@@ -872,11 +899,51 @@ export const createEnvironment = (
872
899
  );
873
900
  }
874
901
  });
875
- declaredConstants.forEach((name) => {
876
- if (!constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name)) {
877
- throw new TlaConfigError(`The constant ${name} is not assigned a value by the configuration`);
878
- }
902
+ // TLC sets a module-scoped entry on the body of the module's definition, which every instance of
903
+ // the module shares.
904
+ const scopedKeys = new Set(
905
+ [...config.constants, ...config.substitutions]
906
+ .filter((item) => item.module !== undefined)
907
+ .flatMap(getScopedKeys),
908
+ );
909
+ const getOverride = (
910
+ key: string,
911
+ prefix: number,
912
+ ): { key: string; prefix: number } | undefined => {
913
+ if (scopedKeys.has(key)) return { key, prefix };
914
+ const copy = copySources.get(key);
915
+ return copy && getOverride(copy.source, prefix + copy.prefix);
916
+ };
917
+ copySources.forEach((copy, key) => {
918
+ const override = getOverride(copy.source, copy.prefix);
919
+ const definition = definitions.get(key);
920
+ if (!override || !definition) return;
921
+ const params = definition.kind === "Operator" ? definition.params : [];
922
+ const alias = createLocalAlias(
923
+ key,
924
+ override.key,
925
+ params,
926
+ params.slice(override.prefix),
927
+ definition.location,
928
+ );
929
+ definitions.set(key, alias);
930
+ linkedRoot.visible.forEach((symbol, name) => {
931
+ if (symbol.key === key && name !== key) definitions.set(name, alias);
932
+ });
879
933
  });
934
+ const unassigned = declaredConstants.filter(
935
+ (name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
936
+ );
937
+ if (unassigned.length > 0) {
938
+ // TLC reports the first constant it finds unassigned in SANY's order of declarations.
939
+ const name =
940
+ getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
941
+ unassigned.includes(constant),
942
+ ) ?? unassigned[0];
943
+ throw new TlaConfigError(
944
+ `The constant parameter ${name} is not assigned a value by the configuration file.`,
945
+ );
946
+ }
880
947
 
881
948
  const environment: Environment = {
882
949
  runtime,
@@ -907,18 +974,43 @@ export const createEnvironment = (
907
974
  getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
908
975
  };
909
976
  });
977
+ // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
978
+ // computes is above constant. The bound counts the operators that TLC's Java code overrides with
979
+ // a minimum level as state-level, so the analyzer takes them for variables.
980
+ const analyzer = createLevelAnalyzer({
981
+ constants: new Set(constants.keys()),
982
+ variables: new Set([
983
+ ...variables,
984
+ ...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
985
+ ]),
986
+ getDefinition: (name) => getDefinition(name, environment),
987
+ });
988
+ const getEvaluationError = (body: Expression) => {
989
+ try {
990
+ evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
991
+ return undefined;
992
+ } catch (error) {
993
+ if (error instanceof TlaEvaluationError) return error;
994
+ throw error;
995
+ }
996
+ };
910
997
  substitutions.forEach((target, name) => {
911
998
  const definition = definitions.get(target);
912
- if (
999
+ const error =
913
1000
  declaredConstants.includes(name) &&
914
- definition &&
915
- !isConstantLevel(definition, environment)
916
- ) {
1001
+ definition?.kind === "Operator" &&
1002
+ definition.params.length === 0 &&
1003
+ analyzer.analyzeDefinition(definition, new Map()).level > 0
1004
+ ? getEvaluationError(definition.body)
1005
+ : undefined;
1006
+ // TLC adds the reason unless a standard module's Java code failed.
1007
+ if (error) {
917
1008
  throw new TlaConfigError(
918
- `The configuration file substitutes constant ${name} with non-constant ${target}`,
1009
+ `The configuration file substitutes constant ${name} with non-constant ${target}${error instanceof TlaModuleError ? "" : ` - specifically: ${error.message}`}`,
919
1010
  );
920
1011
  }
921
1012
  });
1013
+ runtime.isStarted = true;
922
1014
  return {
923
1015
  environment,
924
1016
  assumptions,
package/src/errors.ts CHANGED
@@ -1,4 +1,18 @@
1
- import { EXIT_STATUS, getExitCode, setModuleExitCode } from "./exit-codes";
1
+ import type { SourceRange } from "./ast";
2
+ import { EXIT_STATUS, getExitCode } from "./exit-codes";
3
+ import type { Value } from "./values";
4
+
5
+ // How TLC reports a RuntimeException it did not expect, such as one raised while computing next
6
+ // states.
7
+ export const UNEXPECTED_EXCEPTION =
8
+ "TLC threw an unexpected exception.\nThis was probably caused by an error in the spec or model.\nSee the User Output or TLC Console for clues to what happened.\nThe exception was a java.lang.RuntimeException\n: ";
9
+
10
+ // How TLC reports a Java StackOverflowError, which V8 raises as this RangeError.
11
+ export const STACK_OVERFLOW =
12
+ "This was a Java StackOverflowError. It was probably the result\nof an incorrect recursive function definition that caused TLC to enter\nan infinite loop when trying to compute the function or its application\nto an element in its putative domain.";
13
+
14
+ export const isStackOverflow = (error: unknown) =>
15
+ error instanceof RangeError && error.message === "Maximum call stack size exceeded";
2
16
 
3
17
  export class TlaError extends Error {
4
18
  constructor(message: string, cause?: unknown) {
@@ -14,16 +28,44 @@ export interface SyntaxFailure {
14
28
  isEnd: boolean;
15
29
  }
16
30
 
31
+ // An error SANY reports at a location, such as "In module M" or a range in a module.
32
+ export interface SanyError {
33
+ location: string;
34
+ message: string;
35
+ }
36
+
37
+ // The errors SANY reports for a spec. A fatal error stops it before semantic analysis.
38
+ export interface SanyReport {
39
+ errors: SanyError[];
40
+ isFatal: boolean;
41
+ }
42
+
17
43
  export class TlaSyntaxError extends TlaError {
18
44
  failure: SyntaxFailure | undefined;
45
+ sany: SanyReport | undefined;
19
46
 
20
- constructor(message: string, cause?: unknown, failure?: SyntaxFailure) {
47
+ constructor(message: string, cause?: unknown, failure?: SyntaxFailure, sany?: SanyReport) {
21
48
  super(message, cause);
22
49
  this.name = "TlaSyntaxError";
23
50
  this.failure = failure;
51
+ this.sany = sany;
24
52
  }
25
53
  }
26
54
 
55
+ export const createSanyError = (errors: SanyError[], isFatal = false) =>
56
+ new TlaSyntaxError(
57
+ errors.map((error) => `${error.location}\n\n${error.message}`).join("\n\n"),
58
+ undefined,
59
+ undefined,
60
+ { errors, isFatal },
61
+ );
62
+
63
+ export const formatRange = (range: SourceRange) =>
64
+ `line ${range.start.line}, col ${range.start.column} to line ${range.end.line}, col ${range.end.column} of module ${range.module}`;
65
+
66
+ export const createSemanticError = (message: string, range: SourceRange) =>
67
+ createSanyError([{ location: formatRange(range), message }]);
68
+
27
69
  export class TlaConfigError extends TlaError {
28
70
  constructor(message: string, cause?: unknown) {
29
71
  super(message, cause);
@@ -41,10 +83,52 @@ export class TlaEvaluationError extends TlaError {
41
83
  // An evaluation error that TLC raises as an EvalException from a standard module's Java code. It
42
84
  // keeps the name TlaEvaluationError, and index.ts doesn't export it.
43
85
  export class TlaModuleError extends TlaEvaluationError {
86
+ exitCode: number;
87
+
44
88
  constructor(message: string, exitCode: number = EXIT_STATUS.error) {
45
89
  super(message);
46
90
  this.name = "TlaEvaluationError";
47
- setModuleExitCode(this, exitCode);
91
+ this.exitCode = exitCode;
92
+ }
93
+ }
94
+
95
+ // A variable that a partial state leaves without a value.
96
+ export class TlaUnassignedError extends TlaEvaluationError {
97
+ variable: string;
98
+
99
+ constructor(message: string, variable: string) {
100
+ super(message);
101
+ this.name = "TlaEvaluationError";
102
+ this.variable = variable;
103
+ }
104
+ }
105
+
106
+ // TLC reports a successor state that the next-state relation leaves incomplete as an error of its
107
+ // own, not as an exception. It keeps the name TlaEvaluationError, and index.ts doesn't export it.
108
+ export class TlaIncompleteStateError extends TlaEvaluationError {
109
+ state: (Value | undefined)[];
110
+ action: string | undefined;
111
+ missing: string[];
112
+
113
+ constructor(state: (Value | undefined)[], action: string | undefined, missing: string[]) {
114
+ super(
115
+ `Successor state is not completely specified by the next-state action. The following variable${missing.length > 1 ? "s are" : " is"} not defined: ${missing.join(", ")}.\n`,
116
+ );
117
+ this.name = "TlaEvaluationError";
118
+ this.state = state;
119
+ this.action = action;
120
+ this.missing = missing;
121
+ }
122
+ }
123
+
124
+ // TLC reports an initial state that the initial predicate leaves incomplete as an illegal state.
125
+ export class TlaIllegalStateError extends TlaEvaluationError {
126
+ state: (Value | undefined)[];
127
+
128
+ constructor(state: (Value | undefined)[]) {
129
+ super("current state is not a legal state");
130
+ this.name = "TlaEvaluationError";
131
+ this.state = state;
48
132
  }
49
133
  }
50
134