tla-ts 0.0.3-dev.3c70a97 → 0.0.3-dev.3ce2f00

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 +5 -1
  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 -17
  11. package/dist/run-cli.js +20 -18
  12. package/dist/transfer.cjs +33 -17
  13. package/dist/transfer.js +33 -17
  14. package/package.json +1 -1
  15. package/src/ast.ts +3 -0
  16. package/src/builtins.ts +9 -3
  17. package/src/check.ts +455 -93
  18. package/src/cli-options.ts +11 -5
  19. package/src/codegen.ts +6 -3
  20. package/src/config.ts +224 -170
  21. package/src/environment.ts +115 -19
  22. package/src/errors.ts +78 -1
  23. package/src/evaluate.ts +182 -26
  24. package/src/exit-codes.ts +22 -3
  25. package/src/fingerprints.ts +2 -1
  26. package/src/instances.ts +4 -4
  27. package/src/levels.ts +82 -36
  28. package/src/live-check.ts +132 -70
  29. package/src/liveness.ts +117 -22
  30. package/src/parallel-worker.ts +2 -2
  31. package/src/parallel.ts +5 -2
  32. package/src/parser.ts +57 -16
  33. package/src/resolve.ts +106 -29
  34. package/src/run-cli.ts +103 -18
  35. package/src/runtime.ts +2 -0
  36. package/src/spec-description.ts +12 -6
  37. package/src/standard-modules.ts +54 -45
  38. package/src/state-graph.ts +157 -0
  39. package/src/substitution-levels.ts +35 -11
  40. package/src/successors.ts +169 -33
  41. package/src/symmetry.ts +16 -6
  42. package/src/tableau.ts +51 -24
  43. package/src/tlc-ext.ts +27 -2
  44. package/src/tlc-module.ts +14 -33
  45. package/src/tlc-modules.ts +4 -0
  46. package/src/tlc-order.ts +8 -4
  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 +242 -61
@@ -12,12 +12,19 @@ import {
12
12
  import { BUNDLED_MODULES } from "./bundled-modules";
13
13
  import type { ConfigValue, ModelConfig } from "./config";
14
14
  import type { FileSystem } from "./json";
15
- import { TlaConfigError, TlaEvaluationError, TlaSyntaxError } from "./errors";
15
+ import {
16
+ createSanyError,
17
+ TlaConfigError,
18
+ TlaEvaluationError,
19
+ TlaModuleError,
20
+ TlaSyntaxError,
21
+ } from "./errors";
16
22
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
17
23
  import {
18
24
  createBigNumberError,
19
25
  createRealNumberError,
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
  );
@@ -234,6 +244,7 @@ export const createEnvironment = (
234
244
  const createName = (name: string) => `${name}#${copyCount.value++}`;
235
245
  const instanceChecks: InstanceCheck[] = [];
236
246
  const instanceActions = new Map<string, InstanceAction>();
247
+ const qualifiedNames = new Map<string, string>();
237
248
 
238
249
  const addStandardModule = (name: string): Symbols | undefined => {
239
250
  const cached = standardExports.get(name);
@@ -256,7 +267,11 @@ export const createEnvironment = (
256
267
 
257
268
  const getStandardSymbols = (name: string) => {
258
269
  const symbols = addStandardModule(name);
259
- if (symbols) useModule(name, STANDARD_DEPENDENCIES.get(name) ?? []);
270
+ const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
271
+ if (symbols) useModule(name, dependencies);
272
+ // TLC loads the Java class of each module a standard module uses, so one that uses Integers
273
+ // overrides the arithmetic of Naturals.
274
+ if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
260
275
  return symbols;
261
276
  };
262
277
 
@@ -285,13 +300,25 @@ export const createEnvironment = (
285
300
  BUNDLED_MODULES.get(name) ??
286
301
  extractModule(options.source, name);
287
302
  if (source === undefined)
288
- throw new TlaSyntaxError(
289
- `Cannot find source file for module ${name} imported in module ${importer}.`,
303
+ throw createSanyError(
304
+ [
305
+ {
306
+ location: `In module ${importer}`,
307
+ message: `Cannot find source file for module ${name} imported in module ${importer}.`,
308
+ },
309
+ ],
310
+ true,
290
311
  );
291
312
  const module = parseModule(source);
292
313
  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.`,
314
+ throw createSanyError(
315
+ [
316
+ {
317
+ location: "Unknown location",
318
+ message: `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
319
+ },
320
+ ],
321
+ true,
295
322
  );
296
323
  }
297
324
  return rejectModuleLiterals(module);
@@ -379,10 +406,11 @@ export const createEnvironment = (
379
406
  variables: new Set(linked.variables),
380
407
  getDefinition: (key) => definitions.get(key),
381
408
  isParameterized: (key) => parameterized.get(key) === true,
382
- addDefinition: (name, create) => {
409
+ addDefinition: (source, name, create) => {
383
410
  const key = createName(`${name}@${module.name}`);
384
411
  definitions.set(key, create(key));
385
412
  parameterized.set(key, !isParameterFree);
413
+ copySources.set(key, { source, prefix: instance.params.length });
386
414
  return key;
387
415
  },
388
416
  createName,
@@ -402,6 +430,7 @@ export const createEnvironment = (
402
430
  if (declarations.length > 0) {
403
431
  instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
404
432
  }
433
+ qualifiedNames.set(key, `${instance.name}!${name}`);
405
434
  symbols.set(`${instance.name}!${name}`, {
406
435
  key,
407
436
  arity: symbol.arity + instance.params.length,
@@ -544,7 +573,7 @@ export const createEnvironment = (
544
573
  variables: new Set(linked.variables),
545
574
  getDefinition: (key) => definitions.get(key),
546
575
  isParameterized: (key) => parameterized.get(key) === true,
547
- addDefinition: (name, create) => {
576
+ addDefinition: (_source, name, create) => {
548
577
  const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
549
578
  const definition = create(key);
550
579
  copies.push(
@@ -728,6 +757,7 @@ export const createEnvironment = (
728
757
  constants: new Set(constants.map((constant) => constant.name)),
729
758
  variables: new Set(variables),
730
759
  getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
760
+ getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
731
761
  });
732
762
  const linked = {
733
763
  module,
@@ -872,11 +902,51 @@ export const createEnvironment = (
872
902
  );
873
903
  }
874
904
  });
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
- }
905
+ // TLC sets a module-scoped entry on the body of the module's definition, which every instance of
906
+ // the module shares.
907
+ const scopedKeys = new Set(
908
+ [...config.constants, ...config.substitutions]
909
+ .filter((item) => item.module !== undefined)
910
+ .flatMap(getScopedKeys),
911
+ );
912
+ const getOverride = (
913
+ key: string,
914
+ prefix: number,
915
+ ): { key: string; prefix: number } | undefined => {
916
+ if (scopedKeys.has(key)) return { key, prefix };
917
+ const copy = copySources.get(key);
918
+ return copy && getOverride(copy.source, prefix + copy.prefix);
919
+ };
920
+ copySources.forEach((copy, key) => {
921
+ const override = getOverride(copy.source, copy.prefix);
922
+ const definition = definitions.get(key);
923
+ if (!override || !definition) return;
924
+ const params = definition.kind === "Operator" ? definition.params : [];
925
+ const alias = createLocalAlias(
926
+ key,
927
+ override.key,
928
+ params,
929
+ params.slice(override.prefix),
930
+ definition.location,
931
+ );
932
+ definitions.set(key, alias);
933
+ linkedRoot.visible.forEach((symbol, name) => {
934
+ if (symbol.key === key && name !== key) definitions.set(name, alias);
935
+ });
879
936
  });
937
+ const unassigned = declaredConstants.filter(
938
+ (name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
939
+ );
940
+ if (unassigned.length > 0) {
941
+ // TLC reports the first constant it finds unassigned in SANY's order of declarations.
942
+ const name =
943
+ getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
944
+ unassigned.includes(constant),
945
+ ) ?? unassigned[0];
946
+ throw new TlaConfigError(
947
+ `The constant parameter ${name} is not assigned a value by the configuration file.`,
948
+ );
949
+ }
880
950
 
881
951
  const environment: Environment = {
882
952
  runtime,
@@ -904,21 +974,47 @@ export const createEnvironment = (
904
974
  ),
905
975
  ],
906
976
  assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
977
+ theorems: linked.module.theorems,
907
978
  getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
908
979
  };
909
980
  });
981
+ // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
982
+ // computes is above constant. The bound counts the operators that TLC's Java code overrides with
983
+ // a minimum level as state-level, so the analyzer takes them for variables.
984
+ const analyzer = createLevelAnalyzer({
985
+ constants: new Set(constants.keys()),
986
+ variables: new Set([
987
+ ...variables,
988
+ ...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
989
+ ]),
990
+ getDefinition: (name) => getDefinition(name, environment),
991
+ });
992
+ const getEvaluationError = (body: Expression) => {
993
+ try {
994
+ evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
995
+ return undefined;
996
+ } catch (error) {
997
+ if (error instanceof TlaEvaluationError) return error;
998
+ throw error;
999
+ }
1000
+ };
910
1001
  substitutions.forEach((target, name) => {
911
1002
  const definition = definitions.get(target);
912
- if (
1003
+ const error =
913
1004
  declaredConstants.includes(name) &&
914
- definition &&
915
- !isConstantLevel(definition, environment)
916
- ) {
1005
+ definition?.kind === "Operator" &&
1006
+ definition.params.length === 0 &&
1007
+ analyzer.analyzeDefinition(definition, new Map()).level > 0
1008
+ ? getEvaluationError(definition.body)
1009
+ : undefined;
1010
+ // TLC adds the reason unless a standard module's Java code failed.
1011
+ if (error) {
917
1012
  throw new TlaConfigError(
918
- `The configuration file substitutes constant ${name} with non-constant ${target}`,
1013
+ `The configuration file substitutes constant ${name} with non-constant ${target}${error instanceof TlaModuleError ? "" : ` - specifically: ${error.message}`}`,
919
1014
  );
920
1015
  }
921
1016
  });
1017
+ runtime.isStarted = true;
922
1018
  return {
923
1019
  environment,
924
1020
  assumptions,
package/src/errors.ts CHANGED
@@ -1,10 +1,19 @@
1
+ import type { SourceRange } from "./ast";
1
2
  import { EXIT_STATUS, getExitCode } from "./exit-codes";
3
+ import type { Value } from "./values";
2
4
 
3
5
  // How TLC reports a RuntimeException it did not expect, such as one raised while computing next
4
6
  // states.
5
7
  export const UNEXPECTED_EXCEPTION =
6
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: ";
7
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";
16
+
8
17
  export class TlaError extends Error {
9
18
  constructor(message: string, cause?: unknown) {
10
19
  super(message, { cause });
@@ -19,16 +28,44 @@ export interface SyntaxFailure {
19
28
  isEnd: boolean;
20
29
  }
21
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
+
22
43
  export class TlaSyntaxError extends TlaError {
23
44
  failure: SyntaxFailure | undefined;
45
+ sany: SanyReport | undefined;
24
46
 
25
- constructor(message: string, cause?: unknown, failure?: SyntaxFailure) {
47
+ constructor(message: string, cause?: unknown, failure?: SyntaxFailure, sany?: SanyReport) {
26
48
  super(message, cause);
27
49
  this.name = "TlaSyntaxError";
28
50
  this.failure = failure;
51
+ this.sany = sany;
29
52
  }
30
53
  }
31
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
+
32
69
  export class TlaConfigError extends TlaError {
33
70
  constructor(message: string, cause?: unknown) {
34
71
  super(message, cause);
@@ -55,6 +92,46 @@ export class TlaModuleError extends TlaEvaluationError {
55
92
  }
56
93
  }
57
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;
132
+ }
133
+ }
134
+
58
135
  export interface TlapmReport {
59
136
  failure?: string;
60
137
  exception?: string;