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
@@ -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
  );
@@ -285,13 +295,25 @@ export const createEnvironment = (
285
295
  BUNDLED_MODULES.get(name) ??
286
296
  extractModule(options.source, name);
287
297
  if (source === undefined)
288
- throw new TlaSyntaxError(
289
- `Cannot find source file for module ${name} imported in module ${importer}.`,
298
+ throw createSanyError(
299
+ [
300
+ {
301
+ location: `In module ${importer}`,
302
+ message: `Cannot find source file for module ${name} imported in module ${importer}.`,
303
+ },
304
+ ],
305
+ true,
290
306
  );
291
307
  const module = parseModule(source);
292
308
  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.`,
309
+ throw createSanyError(
310
+ [
311
+ {
312
+ location: "Unknown location",
313
+ message: `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
314
+ },
315
+ ],
316
+ true,
295
317
  );
296
318
  }
297
319
  return rejectModuleLiterals(module);
@@ -379,10 +401,11 @@ export const createEnvironment = (
379
401
  variables: new Set(linked.variables),
380
402
  getDefinition: (key) => definitions.get(key),
381
403
  isParameterized: (key) => parameterized.get(key) === true,
382
- addDefinition: (name, create) => {
404
+ addDefinition: (source, name, create) => {
383
405
  const key = createName(`${name}@${module.name}`);
384
406
  definitions.set(key, create(key));
385
407
  parameterized.set(key, !isParameterFree);
408
+ copySources.set(key, { source, prefix: instance.params.length });
386
409
  return key;
387
410
  },
388
411
  createName,
@@ -544,7 +567,7 @@ export const createEnvironment = (
544
567
  variables: new Set(linked.variables),
545
568
  getDefinition: (key) => definitions.get(key),
546
569
  isParameterized: (key) => parameterized.get(key) === true,
547
- addDefinition: (name, create) => {
570
+ addDefinition: (_source, name, create) => {
548
571
  const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
549
572
  const definition = create(key);
550
573
  copies.push(
@@ -872,11 +895,51 @@ export const createEnvironment = (
872
895
  );
873
896
  }
874
897
  });
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
- }
898
+ // TLC sets a module-scoped entry on the body of the module's definition, which every instance of
899
+ // the module shares.
900
+ const scopedKeys = new Set(
901
+ [...config.constants, ...config.substitutions]
902
+ .filter((item) => item.module !== undefined)
903
+ .flatMap(getScopedKeys),
904
+ );
905
+ const getOverride = (
906
+ key: string,
907
+ prefix: number,
908
+ ): { key: string; prefix: number } | undefined => {
909
+ if (scopedKeys.has(key)) return { key, prefix };
910
+ const copy = copySources.get(key);
911
+ return copy && getOverride(copy.source, prefix + copy.prefix);
912
+ };
913
+ copySources.forEach((copy, key) => {
914
+ const override = getOverride(copy.source, copy.prefix);
915
+ const definition = definitions.get(key);
916
+ if (!override || !definition) return;
917
+ const params = definition.kind === "Operator" ? definition.params : [];
918
+ const alias = createLocalAlias(
919
+ key,
920
+ override.key,
921
+ params,
922
+ params.slice(override.prefix),
923
+ definition.location,
924
+ );
925
+ definitions.set(key, alias);
926
+ linkedRoot.visible.forEach((symbol, name) => {
927
+ if (symbol.key === key && name !== key) definitions.set(name, alias);
928
+ });
879
929
  });
930
+ const unassigned = declaredConstants.filter(
931
+ (name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
932
+ );
933
+ if (unassigned.length > 0) {
934
+ // TLC reports the first constant it finds unassigned in SANY's order of declarations.
935
+ const name =
936
+ getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
937
+ unassigned.includes(constant),
938
+ ) ?? unassigned[0];
939
+ throw new TlaConfigError(
940
+ `The constant parameter ${name} is not assigned a value by the configuration file.`,
941
+ );
942
+ }
880
943
 
881
944
  const environment: Environment = {
882
945
  runtime,
@@ -907,18 +970,43 @@ export const createEnvironment = (
907
970
  getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
908
971
  };
909
972
  });
973
+ // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
974
+ // computes is above constant. The bound counts the operators that TLC's Java code overrides with
975
+ // a minimum level as state-level, so the analyzer takes them for variables.
976
+ const analyzer = createLevelAnalyzer({
977
+ constants: new Set(constants.keys()),
978
+ variables: new Set([
979
+ ...variables,
980
+ ...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
981
+ ]),
982
+ getDefinition: (name) => getDefinition(name, environment),
983
+ });
984
+ const getEvaluationError = (body: Expression) => {
985
+ try {
986
+ evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
987
+ return undefined;
988
+ } catch (error) {
989
+ if (error instanceof TlaEvaluationError) return error;
990
+ throw error;
991
+ }
992
+ };
910
993
  substitutions.forEach((target, name) => {
911
994
  const definition = definitions.get(target);
912
- if (
995
+ const error =
913
996
  declaredConstants.includes(name) &&
914
- definition &&
915
- !isConstantLevel(definition, environment)
916
- ) {
997
+ definition?.kind === "Operator" &&
998
+ definition.params.length === 0 &&
999
+ analyzer.analyzeDefinition(definition, new Map()).level > 0
1000
+ ? getEvaluationError(definition.body)
1001
+ : undefined;
1002
+ // TLC adds the reason unless a standard module's Java code failed.
1003
+ if (error) {
917
1004
  throw new TlaConfigError(
918
- `The configuration file substitutes constant ${name} with non-constant ${target}`,
1005
+ `The configuration file substitutes constant ${name} with non-constant ${target}${error instanceof TlaModuleError ? "" : ` - specifically: ${error.message}`}`,
919
1006
  );
920
1007
  }
921
1008
  });
1009
+ runtime.isStarted = true;
922
1010
  return {
923
1011
  environment,
924
1012
  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,41 @@ 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
+ // TLC reports a successor state that the next-state relation leaves incomplete as an error of its
96
+ // own, not as an exception. It keeps the name TlaEvaluationError, and index.ts doesn't export it.
97
+ export class TlaIncompleteStateError extends TlaEvaluationError {
98
+ state: (Value | undefined)[];
99
+ action: string | undefined;
100
+ missing: string[];
101
+
102
+ constructor(state: (Value | undefined)[], action: string | undefined, missing: string[]) {
103
+ super(
104
+ `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`,
105
+ );
106
+ this.name = "TlaEvaluationError";
107
+ this.state = state;
108
+ this.action = action;
109
+ this.missing = missing;
110
+ }
111
+ }
112
+
113
+ // TLC reports an initial state that the initial predicate leaves incomplete as an illegal state.
114
+ export class TlaIllegalStateError extends TlaEvaluationError {
115
+ state: (Value | undefined)[];
116
+
117
+ constructor(state: (Value | undefined)[]) {
118
+ super("current state is not a legal state");
119
+ this.name = "TlaEvaluationError";
120
+ this.state = state;
48
121
  }
49
122
  }
50
123