tla-ts 0.0.3-dev.366c320 → 0.0.3-dev.367c92f
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.
- package/README.md +5 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +53 -22
- package/dist/index.d.ts +53 -22
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +19 -17
- package/dist/run-cli.js +20 -18
- package/dist/transfer.cjs +33 -17
- package/dist/transfer.js +33 -17
- package/package.json +1 -1
- package/src/ast.ts +3 -0
- package/src/builtins.ts +9 -3
- package/src/check.ts +455 -93
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +6 -3
- package/src/config.ts +224 -170
- package/src/environment.ts +114 -19
- package/src/errors.ts +78 -1
- package/src/evaluate.ts +182 -26
- package/src/exit-codes.ts +22 -3
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +4 -4
- package/src/levels.ts +63 -28
- package/src/live-check.ts +132 -70
- package/src/liveness.ts +117 -22
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +5 -2
- package/src/parser.ts +57 -16
- package/src/resolve.ts +106 -29
- package/src/run-cli.ts +103 -18
- package/src/runtime.ts +2 -0
- package/src/spec-description.ts +12 -6
- package/src/standard-modules.ts +54 -45
- package/src/state-graph.ts +157 -0
- package/src/substitution-levels.ts +17 -9
- package/src/successors.ts +169 -33
- package/src/symmetry.ts +16 -6
- package/src/tableau.ts +51 -24
- package/src/tlc-module.ts +11 -5
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +8 -4
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/value-sources.ts +29 -0
- package/src/values.ts +242 -61
package/src/environment.ts
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
289
|
-
|
|
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
|
|
294
|
-
|
|
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
|
-
|
|
876
|
-
|
|
877
|
-
|
|
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,
|
|
@@ -907,18 +977,43 @@ export const createEnvironment = (
|
|
|
907
977
|
getDefinition: (symbol) => definitions.get(linked.visible.get(symbol)?.key ?? symbol),
|
|
908
978
|
};
|
|
909
979
|
});
|
|
980
|
+
// TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
|
|
981
|
+
// computes is above constant. The bound counts the operators that TLC's Java code overrides with
|
|
982
|
+
// a minimum level as state-level, so the analyzer takes them for variables.
|
|
983
|
+
const analyzer = createLevelAnalyzer({
|
|
984
|
+
constants: new Set(constants.keys()),
|
|
985
|
+
variables: new Set([
|
|
986
|
+
...variables,
|
|
987
|
+
...["TLCGet", "Trace", "CounterExample"].filter((name) => builtins.has(name)),
|
|
988
|
+
]),
|
|
989
|
+
getDefinition: (name) => getDefinition(name, environment),
|
|
990
|
+
});
|
|
991
|
+
const getEvaluationError = (body: Expression) => {
|
|
992
|
+
try {
|
|
993
|
+
evaluate(body, { environment, scope: undefined, state: undefined, next: undefined });
|
|
994
|
+
return undefined;
|
|
995
|
+
} catch (error) {
|
|
996
|
+
if (error instanceof TlaEvaluationError) return error;
|
|
997
|
+
throw error;
|
|
998
|
+
}
|
|
999
|
+
};
|
|
910
1000
|
substitutions.forEach((target, name) => {
|
|
911
1001
|
const definition = definitions.get(target);
|
|
912
|
-
|
|
1002
|
+
const error =
|
|
913
1003
|
declaredConstants.includes(name) &&
|
|
914
|
-
definition &&
|
|
915
|
-
|
|
916
|
-
|
|
1004
|
+
definition?.kind === "Operator" &&
|
|
1005
|
+
definition.params.length === 0 &&
|
|
1006
|
+
analyzer.analyzeDefinition(definition, new Map()).level > 0
|
|
1007
|
+
? getEvaluationError(definition.body)
|
|
1008
|
+
: undefined;
|
|
1009
|
+
// TLC adds the reason unless a standard module's Java code failed.
|
|
1010
|
+
if (error) {
|
|
917
1011
|
throw new TlaConfigError(
|
|
918
|
-
`The configuration file substitutes constant ${name} with non-constant ${target}`,
|
|
1012
|
+
`The configuration file substitutes constant ${name} with non-constant ${target}${error instanceof TlaModuleError ? "" : ` - specifically: ${error.message}`}`,
|
|
919
1013
|
);
|
|
920
1014
|
}
|
|
921
1015
|
});
|
|
1016
|
+
runtime.isStarted = true;
|
|
922
1017
|
return {
|
|
923
1018
|
environment,
|
|
924
1019
|
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;
|