tla-ts 0.0.3-dev.31e0776 → 0.0.3-dev.3504663

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 (48) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.d.cts +50 -22
  3. package/dist/index.d.ts +50 -22
  4. package/dist/index.js +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +2 -2
  7. package/dist/parallel.cjs +1 -1
  8. package/dist/parallel.js +2 -2
  9. package/dist/run-cli.cjs +14 -12
  10. package/dist/run-cli.js +22 -20
  11. package/dist/transfer.cjs +38 -21
  12. package/dist/transfer.js +38 -21
  13. package/package.json +1 -1
  14. package/src/ast.ts +11 -1
  15. package/src/builtins.ts +9 -3
  16. package/src/check.ts +357 -79
  17. package/src/codegen.ts +6 -3
  18. package/src/config.ts +224 -170
  19. package/src/environment.ts +90 -40
  20. package/src/errors.ts +89 -1
  21. package/src/evaluate.ts +182 -26
  22. package/src/exit-codes.ts +22 -3
  23. package/src/fingerprints.ts +2 -1
  24. package/src/instances.ts +1 -1
  25. package/src/levels.ts +173 -37
  26. package/src/live-check.ts +132 -70
  27. package/src/liveness.ts +117 -22
  28. package/src/parallel-worker.ts +2 -2
  29. package/src/parallel.ts +2 -2
  30. package/src/parser.ts +71 -19
  31. package/src/resolve.ts +429 -52
  32. package/src/run-cli.ts +59 -11
  33. package/src/sany-lexer.ts +615 -0
  34. package/src/sany-parse-error.ts +3143 -0
  35. package/src/spec-description.ts +1 -2
  36. package/src/standard-modules.ts +54 -45
  37. package/src/subexpressions.ts +25 -13
  38. package/src/substitution-levels.ts +38 -17
  39. package/src/successors.ts +121 -34
  40. package/src/symmetry.ts +16 -6
  41. package/src/tableau.ts +51 -24
  42. package/src/tlc-module.ts +6 -6
  43. package/src/tlc-modules.ts +4 -0
  44. package/src/tlc-order.ts +8 -4
  45. package/src/tlc-random.ts +9 -6
  46. package/src/tlc-summary.ts +20 -5
  47. package/src/value-sources.ts +29 -0
  48. package/src/values.ts +222 -57
@@ -12,7 +12,13 @@ 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,
@@ -43,10 +49,12 @@ import {
43
49
  type Symbols,
44
50
  } from "./resolve";
45
51
  import { createRuntime } from "./runtime";
52
+ import { parseModuleFile } from "./sany-parse-error";
46
53
  import { CORE_OPERATORS, getStandardModule, STANDARD_DEPENDENCIES } from "./standard-modules";
47
54
  import { resolveInstanceSubexpressions, resolveModuleSubexpressions } from "./subexpressions";
48
55
  import { checkModuleLevels, createLevelAnalyzer } from "./levels";
49
- import { checkInstances, type InstanceCheck } from "./substitution-levels";
56
+ import { createInstanceChecker, type InstanceCheck } from "./substitution-levels";
57
+ import { getTlcDeclarationOrder } from "./tlc-order";
50
58
  import {
51
59
  checkInteger,
52
60
  createModelValue,
@@ -222,6 +230,8 @@ export const createEnvironment = (
222
230
  const runtime = createRuntime(config.checkDeadlock, options.seed);
223
231
  const linkedModules = new Map<string, LinkedModule>();
224
232
  const nestedModules = new Map<string, LinkedModule>();
233
+ // Each module by name, from the time its level checks run.
234
+ const checkedModules = new Map<string, LinkedModule>();
225
235
  const linkedList: LinkedModule[] = [];
226
236
  const dependencies = new Map<string, Set<string>>();
227
237
  const loading: Set<string>[] = [];
@@ -236,7 +246,23 @@ export const createEnvironment = (
236
246
  const copyCount = { value: 0 };
237
247
  const createName = (name: string) => `${name}#${copyCount.value++}`;
238
248
  const instanceChecks: InstanceCheck[] = [];
249
+ const checkInstance = createInstanceChecker((name) => {
250
+ const linked = checkedModules.get(name) ?? getLinkedModule(name, name);
251
+ return {
252
+ constants: linked.constants,
253
+ variables: linked.variables,
254
+ definitions: [
255
+ ...new Set(
256
+ [...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
257
+ ),
258
+ ],
259
+ assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
260
+ theorems: linked.module.theorems,
261
+ getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
262
+ };
263
+ });
239
264
  const instanceActions = new Map<string, InstanceAction>();
265
+ const qualifiedNames = new Map<string, string>();
240
266
 
241
267
  const addStandardModule = (name: string): Symbols | undefined => {
242
268
  const cached = standardExports.get(name);
@@ -259,7 +285,11 @@ export const createEnvironment = (
259
285
 
260
286
  const getStandardSymbols = (name: string) => {
261
287
  const symbols = addStandardModule(name);
262
- if (symbols) useModule(name, STANDARD_DEPENDENCIES.get(name) ?? []);
288
+ const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
289
+ if (symbols) useModule(name, dependencies);
290
+ // TLC loads the Java class of each module a standard module uses, so one that uses Integers
291
+ // overrides the arithmetic of Naturals.
292
+ if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
263
293
  return symbols;
264
294
  };
265
295
 
@@ -288,13 +318,25 @@ export const createEnvironment = (
288
318
  BUNDLED_MODULES.get(name) ??
289
319
  extractModule(options.source, name);
290
320
  if (source === undefined)
291
- throw new TlaSyntaxError(
292
- `Cannot find source file for module ${name} imported in module ${importer}.`,
321
+ throw createSanyError(
322
+ [
323
+ {
324
+ location: `In module ${importer}`,
325
+ message: `Cannot find source file for module ${name} imported in module ${importer}.`,
326
+ },
327
+ ],
328
+ true,
293
329
  );
294
- const module = parseModule(source);
330
+ const module = parseModuleFile(source, name);
295
331
  if (module.name !== name) {
296
- throw new TlaSyntaxError(
297
- `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
332
+ throw createSanyError(
333
+ [
334
+ {
335
+ location: "Unknown location",
336
+ message: `File name '${name}' does not match the name '${module.name}' of the top level module it contains.`,
337
+ },
338
+ ],
339
+ true,
298
340
  );
299
341
  }
300
342
  return rejectModuleLiterals(module);
@@ -406,6 +448,7 @@ export const createEnvironment = (
406
448
  if (declarations.length > 0) {
407
449
  instanceActions.set(key, { name: `${instance.name}!${name}`, location: instance.location });
408
450
  }
451
+ qualifiedNames.set(key, `${instance.name}!${name}`);
409
452
  symbols.set(`${instance.name}!${name}`, {
410
453
  key,
411
454
  arity: symbol.arity + instance.params.length,
@@ -586,6 +629,7 @@ export const createEnvironment = (
586
629
 
587
630
  const linkModule = (unlinked: Module, scope: ModuleScope): LinkedModule => {
588
631
  const parsed = overrideDefinitions(unlinked, options.loadClass?.(unlinked.name));
632
+ const firstCheck = instanceChecks.length;
589
633
  const visible: Symbols = new Map([
590
634
  ...CORE_OPERATORS.map((operator): [string, ModuleSymbol] => [
591
635
  operator.name,
@@ -728,11 +772,6 @@ export const createEnvironment = (
728
772
  visible.set(definition.name, symbol);
729
773
  if (!definition.isLocal) exported.set(definition.name, symbol);
730
774
  });
731
- checkModuleLevels(module, {
732
- constants: new Set(constants.map((constant) => constant.name)),
733
- variables: new Set(variables),
734
- getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
735
- });
736
775
  const linked = {
737
776
  module,
738
777
  constants,
@@ -743,6 +782,21 @@ export const createEnvironment = (
743
782
  modules,
744
783
  extended,
745
784
  };
785
+ if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
786
+ // SANY checks the instances of a module with its definitions and theorems, so it reports their
787
+ // errors together.
788
+ checkModuleLevels(
789
+ module,
790
+ {
791
+ constants: new Set(constants.map((constant) => constant.name)),
792
+ variables: new Set(variables),
793
+ getDefinition: (symbol) => definitions.get(visible.get(symbol)?.key ?? symbol),
794
+ getQualifiedName: (symbol) => qualifiedNames.get(visible.get(symbol)?.key ?? symbol),
795
+ },
796
+ instanceChecks
797
+ .splice(firstCheck)
798
+ .map((check) => ({ location: check.location, check: () => checkInstance(check) })),
799
+ );
746
800
  linkedList.push(linked);
747
801
  return linked;
748
802
  };
@@ -908,11 +962,19 @@ export const createEnvironment = (
908
962
  if (symbol.key === key && name !== key) definitions.set(name, alias);
909
963
  });
910
964
  });
911
- declaredConstants.forEach((name) => {
912
- if (!constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name)) {
913
- throw new TlaConfigError(`The constant ${name} is not assigned a value by the configuration`);
914
- }
915
- });
965
+ const unassigned = declaredConstants.filter(
966
+ (name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
967
+ );
968
+ if (unassigned.length > 0) {
969
+ // TLC reports the first constant it finds unassigned in SANY's order of declarations.
970
+ const name =
971
+ getTlcDeclarationOrder(options.source, options.loadModule, "constant").find((constant) =>
972
+ unassigned.includes(constant),
973
+ ) ?? unassigned[0];
974
+ throw new TlaConfigError(
975
+ `The constant parameter ${name} is not assigned a value by the configuration file.`,
976
+ );
977
+ }
916
978
 
917
979
  const environment: Environment = {
918
980
  runtime,
@@ -929,20 +991,6 @@ export const createEnvironment = (
929
991
  constantLevels: new Map(),
930
992
  evaluators: new WeakMap(),
931
993
  };
932
- checkInstances(instanceChecks, (name) => {
933
- const linked = linkedModules.get(name) ?? nestedModules.get(name) ?? linkedRoot;
934
- return {
935
- constants: linked.constants,
936
- variables: linked.variables,
937
- definitions: [
938
- ...new Set(
939
- [...linked.visible.values()].flatMap((symbol) => definitions.get(symbol.key) ?? []),
940
- ),
941
- ],
942
- assumptions: linked.module.assumptions.map((assumption) => assumption.expression),
943
- getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
944
- };
945
- });
946
994
  // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
947
995
  // computes is above constant. The bound counts the operators that TLC's Java code overrides with
948
996
  // a minimum level as state-level, so the analyzer takes them for variables.
@@ -954,26 +1002,28 @@ export const createEnvironment = (
954
1002
  ]),
955
1003
  getDefinition: (name) => getDefinition(name, environment),
956
1004
  });
957
- const isEvaluable = (body: Expression) => {
1005
+ const getEvaluationError = (body: Expression) => {
958
1006
  try {
959
1007
  evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
960
- return true;
1008
+ return undefined;
961
1009
  } catch (error) {
962
- if (error instanceof TlaEvaluationError) return false;
1010
+ if (error instanceof TlaEvaluationError) return error;
963
1011
  throw error;
964
1012
  }
965
1013
  };
966
1014
  substitutions.forEach((target, name) => {
967
1015
  const definition = definitions.get(target);
968
- if (
1016
+ const error =
969
1017
  declaredConstants.includes(name) &&
970
1018
  definition?.kind === "Operator" &&
971
1019
  definition.params.length === 0 &&
972
- analyzer.analyzeDefinition(definition, new Map()).level > 0 &&
973
- !isEvaluable(definition.body)
974
- ) {
1020
+ analyzer.analyzeDefinition(definition, new Map()).level > 0
1021
+ ? getEvaluationError(definition.body)
1022
+ : undefined;
1023
+ // TLC adds the reason unless a standard module's Java code failed.
1024
+ if (error) {
975
1025
  throw new TlaConfigError(
976
- `The configuration file substitutes constant ${name} with non-constant ${target}`,
1026
+ `The configuration file substitutes constant ${name} with non-constant ${target}${error instanceof TlaModuleError ? "" : ` - specifically: ${error.message}`}`,
977
1027
  );
978
1028
  }
979
1029
  });
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,55 @@ 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. SANY's
38
+ // parser prints its message for a module it rejects before the report.
39
+ export interface SanyReport {
40
+ errors: SanyError[];
41
+ isFatal: boolean;
42
+ parseError?: string;
43
+ }
44
+
22
45
  export class TlaSyntaxError extends TlaError {
23
46
  failure: SyntaxFailure | undefined;
47
+ sany: SanyReport | undefined;
24
48
 
25
- constructor(message: string, cause?: unknown, failure?: SyntaxFailure) {
49
+ constructor(message: string, cause?: unknown, failure?: SyntaxFailure, sany?: SanyReport) {
26
50
  super(message, cause);
27
51
  this.name = "TlaSyntaxError";
28
52
  this.failure = failure;
53
+ this.sany = sany;
29
54
  }
30
55
  }
31
56
 
57
+ export const createSanyError = (errors: SanyError[], isFatal = false, parseError?: string) =>
58
+ new TlaSyntaxError(
59
+ errors.map((error) => `${error.location}\n\n${error.message}`).join("\n\n"),
60
+ undefined,
61
+ undefined,
62
+ { errors, isFatal, parseError },
63
+ );
64
+
65
+ // SANY's message for a module its parser rejects, such as a "***Parse Error***" with the
66
+ // productions it was in.
67
+ export class TlaParseError extends TlaError {
68
+ constructor(message: string) {
69
+ super(message);
70
+ this.name = "TlaParseError";
71
+ }
72
+ }
73
+
74
+ export const formatRange = (range: SourceRange) =>
75
+ `line ${range.start.line}, col ${range.start.column} to line ${range.end.line}, col ${range.end.column} of module ${range.module}`;
76
+
77
+ export const createSemanticError = (message: string, range: SourceRange) =>
78
+ createSanyError([{ location: formatRange(range), message }]);
79
+
32
80
  export class TlaConfigError extends TlaError {
33
81
  constructor(message: string, cause?: unknown) {
34
82
  super(message, cause);
@@ -55,6 +103,46 @@ export class TlaModuleError extends TlaEvaluationError {
55
103
  }
56
104
  }
57
105
 
106
+ // A variable that a partial state leaves without a value.
107
+ export class TlaUnassignedError extends TlaEvaluationError {
108
+ variable: string;
109
+
110
+ constructor(message: string, variable: string) {
111
+ super(message);
112
+ this.name = "TlaEvaluationError";
113
+ this.variable = variable;
114
+ }
115
+ }
116
+
117
+ // TLC reports a successor state that the next-state relation leaves incomplete as an error of its
118
+ // own, not as an exception. It keeps the name TlaEvaluationError, and index.ts doesn't export it.
119
+ export class TlaIncompleteStateError extends TlaEvaluationError {
120
+ state: (Value | undefined)[];
121
+ action: string | undefined;
122
+ missing: string[];
123
+
124
+ constructor(state: (Value | undefined)[], action: string | undefined, missing: string[]) {
125
+ super(
126
+ `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`,
127
+ );
128
+ this.name = "TlaEvaluationError";
129
+ this.state = state;
130
+ this.action = action;
131
+ this.missing = missing;
132
+ }
133
+ }
134
+
135
+ // TLC reports an initial state that the initial predicate leaves incomplete as an illegal state.
136
+ export class TlaIllegalStateError extends TlaEvaluationError {
137
+ state: (Value | undefined)[];
138
+
139
+ constructor(state: (Value | undefined)[]) {
140
+ super("current state is not a legal state");
141
+ this.name = "TlaEvaluationError";
142
+ this.state = state;
143
+ }
144
+ }
145
+
58
146
  export interface TlapmReport {
59
147
  failure?: string;
60
148
  exception?: string;