tla-ts 0.0.3-dev.4b28a29 → 0.0.3-dev.4cf9a33
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/dist/index.d.cts +38 -22
- package/dist/index.d.ts +38 -22
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +1 -1
- package/dist/run-cli.cjs +5 -3
- package/dist/run-cli.js +20 -18
- package/dist/transfer.cjs +30 -21
- package/dist/transfer.js +30 -21
- package/package.json +1 -1
- package/src/ast.ts +3 -0
- package/src/check.ts +192 -32
- package/src/codegen.ts +6 -3
- package/src/config.ts +224 -170
- package/src/environment.ts +47 -18
- package/src/errors.ts +49 -3
- package/src/evaluate.ts +80 -16
- package/src/exit-codes.ts +22 -3
- package/src/instances.ts +1 -1
- package/src/levels.ts +55 -27
- package/src/live-check.ts +8 -2
- package/src/liveness.ts +68 -10
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +2 -2
- package/src/parser.ts +21 -10
- package/src/resolve.ts +106 -29
- package/src/run-cli.ts +35 -8
- package/src/spec-description.ts +1 -2
- package/src/successors.ts +69 -15
- package/src/symmetry.ts +16 -6
- package/src/tableau.ts +1 -1
- package/src/tlc-module.ts +6 -6
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +8 -4
- package/src/values.ts +62 -9
package/src/parser.ts
CHANGED
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
StepBody,
|
|
23
23
|
UseOrHide,
|
|
24
24
|
} from "./ast";
|
|
25
|
-
import { TlaSyntaxError } from "./errors";
|
|
25
|
+
import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
|
|
26
26
|
import { getNumeralValue, tokenize, type Token } from "./lexer";
|
|
27
27
|
|
|
28
28
|
interface InfixOperator {
|
|
@@ -631,11 +631,15 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
631
631
|
return parseExpression();
|
|
632
632
|
};
|
|
633
633
|
|
|
634
|
+
const lastArguments: { range: SourceRange | undefined } = { range: undefined };
|
|
635
|
+
|
|
634
636
|
const parseArguments = () =>
|
|
635
637
|
withoutOffside(() => {
|
|
638
|
+
const start = cursor.index;
|
|
636
639
|
expect("(");
|
|
637
640
|
const args = parseSeparated(parseArgument);
|
|
638
641
|
expect(")");
|
|
642
|
+
lastArguments.range = getRange(start);
|
|
639
643
|
return args;
|
|
640
644
|
});
|
|
641
645
|
|
|
@@ -662,9 +666,12 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
662
666
|
if (peek().kind === "Identifier" && (isAt("|->", 1) || isAt(":", 1))) {
|
|
663
667
|
const kind = isAt("|->", 1) ? "Record" : "RecordSet";
|
|
664
668
|
const fields = parseSeparated(() => {
|
|
669
|
+
const fieldStart = cursor.index;
|
|
670
|
+
const fieldLocation = getLocation(peek());
|
|
665
671
|
const name = expectIdentifier();
|
|
672
|
+
fieldLocation.range = getRange(fieldStart);
|
|
666
673
|
expect(kind === "Record" ? "|->" : ":");
|
|
667
|
-
return { name, value: parseExpression() };
|
|
674
|
+
return { name, value: parseExpression(), location: fieldLocation };
|
|
668
675
|
});
|
|
669
676
|
expect("]");
|
|
670
677
|
return { kind, fields, location };
|
|
@@ -856,7 +863,7 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
856
863
|
const args = hasArguments ? parseArguments() : [];
|
|
857
864
|
if (!isSelectorAhead()) {
|
|
858
865
|
return hasArguments
|
|
859
|
-
? { kind: "Apply", operator: name, args, location }
|
|
866
|
+
? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
|
|
860
867
|
: { kind: "Identifier", name, location };
|
|
861
868
|
}
|
|
862
869
|
const instance = openModules
|
|
@@ -885,9 +892,10 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
885
892
|
const argumentCounts = selectors.map((selector) =>
|
|
886
893
|
"args" in selector ? selector.args.length : 0,
|
|
887
894
|
);
|
|
895
|
+
const argumentsRange = lastArguments.range;
|
|
888
896
|
return argumentCounts.slice(0, -1).some((count) => count > 0)
|
|
889
|
-
? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, location }
|
|
890
|
-
: { kind: "Apply", operator, args: qualifiedArgs, location };
|
|
897
|
+
? { kind: "Apply", operator, args: qualifiedArgs, argumentCounts, argumentsRange, location }
|
|
898
|
+
: { kind: "Apply", operator, args: qualifiedArgs, argumentsRange, location };
|
|
891
899
|
}
|
|
892
900
|
if (token.kind === "Operator" && isOperatorName(token.text) && isAt("("))
|
|
893
901
|
return { kind: "Apply", operator: token.text, args: parseArguments(), location };
|
|
@@ -1423,11 +1431,14 @@ const parse = (source: string, isTlapm: boolean): Module => {
|
|
|
1423
1431
|
location: getLocation(keyword),
|
|
1424
1432
|
};
|
|
1425
1433
|
const invalid =
|
|
1426
|
-
use.kind === "Hide" && !isTlapm ? use.facts.
|
|
1427
|
-
if (invalid) {
|
|
1428
|
-
throw
|
|
1429
|
-
|
|
1430
|
-
|
|
1434
|
+
use.kind === "Hide" && !isTlapm ? use.facts.filter((fact) => !isHideFact(fact)) : [];
|
|
1435
|
+
if (invalid.length > 0) {
|
|
1436
|
+
throw createSanyError(
|
|
1437
|
+
invalid.map((fact) => ({
|
|
1438
|
+
location: formatRange(fact.location.range ?? getRange(start)),
|
|
1439
|
+
message:
|
|
1440
|
+
"The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
|
|
1441
|
+
})),
|
|
1431
1442
|
);
|
|
1432
1443
|
}
|
|
1433
1444
|
return withRange(use, start);
|
package/src/resolve.ts
CHANGED
|
@@ -13,9 +13,16 @@ import {
|
|
|
13
13
|
type Proof,
|
|
14
14
|
type ProofStep,
|
|
15
15
|
type SourceLocation,
|
|
16
|
+
type SourceRange,
|
|
16
17
|
type Theorem,
|
|
17
18
|
} from "./ast";
|
|
18
|
-
import {
|
|
19
|
+
import {
|
|
20
|
+
createSanyError,
|
|
21
|
+
createSemanticError,
|
|
22
|
+
formatRange,
|
|
23
|
+
TlaSyntaxError,
|
|
24
|
+
type SanyError,
|
|
25
|
+
} from "./errors";
|
|
19
26
|
|
|
20
27
|
export interface ModuleSymbol {
|
|
21
28
|
key: string;
|
|
@@ -103,6 +110,21 @@ const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
|
|
|
103
110
|
: unit.imported.get(name);
|
|
104
111
|
};
|
|
105
112
|
|
|
113
|
+
// SANY reports a name at the range of its text.
|
|
114
|
+
const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
|
|
115
|
+
module: location.range?.module ?? "",
|
|
116
|
+
start: location,
|
|
117
|
+
end: { line: location.line, column: location.column + name.length - 1 },
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// SANY reports an operator symbol applied to operands as one it cannot resolve.
|
|
121
|
+
const getFixity = (expression: Expression) => {
|
|
122
|
+
if (expression.kind !== "Apply") return undefined;
|
|
123
|
+
if (expression.args.length === 2) return "infix";
|
|
124
|
+
if (expression.args.length !== 1) return undefined;
|
|
125
|
+
return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
|
|
126
|
+
};
|
|
127
|
+
|
|
106
128
|
// SANY names an undefined operator symbol such as + differently from an undefined identifier.
|
|
107
129
|
const isOperatorSymbol = (name: string) =>
|
|
108
130
|
name[0].toLowerCase() === name[0].toUpperCase() &&
|
|
@@ -135,7 +157,7 @@ const getSymbolArity = (
|
|
|
135
157
|
}
|
|
136
158
|
// A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
|
|
137
159
|
// arguments.
|
|
138
|
-
const prefix = name.slice(0, name.indexOf("!"));
|
|
160
|
+
const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
|
|
139
161
|
const isSelected =
|
|
140
162
|
prefix !== "" &&
|
|
141
163
|
(lookupLocal(scope, prefix) !== undefined ||
|
|
@@ -144,11 +166,23 @@ const getSymbolArity = (
|
|
|
144
166
|
if (isSelected) {
|
|
145
167
|
return expression.kind === "Apply" ? expression.args.length : 0;
|
|
146
168
|
}
|
|
147
|
-
const
|
|
148
|
-
throw
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
169
|
+
const range = getNameRange(location, name === "-." ? "-" : name);
|
|
170
|
+
if (!isOperatorSymbol(name)) throw createSemanticError(`Unknown operator: \`${name}'.`, range);
|
|
171
|
+
const missing = {
|
|
172
|
+
location: formatRange(range),
|
|
173
|
+
message: `Could not find declaration or definition of symbol '${name}'.`,
|
|
174
|
+
};
|
|
175
|
+
const fixity = getFixity(expression);
|
|
176
|
+
throw createSanyError(
|
|
177
|
+
fixity && location.range
|
|
178
|
+
? [
|
|
179
|
+
missing,
|
|
180
|
+
{
|
|
181
|
+
location: formatRange(location.range),
|
|
182
|
+
message: `Couldn't resolve ${fixity} operator symbol \`${name}'.`,
|
|
183
|
+
},
|
|
184
|
+
]
|
|
185
|
+
: [missing],
|
|
152
186
|
);
|
|
153
187
|
};
|
|
154
188
|
|
|
@@ -235,13 +269,13 @@ const resolveApply = (
|
|
|
235
269
|
scope: LocalName | undefined,
|
|
236
270
|
unit: Unit,
|
|
237
271
|
) => {
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const arity = getSymbolArity(expression, expression.operator, scope, unit);
|
|
272
|
+
const operator = expression.operator;
|
|
273
|
+
const callee = lookupLocal(scope, operator) ? undefined : getVisibleDefinition(operator, unit);
|
|
274
|
+
const arity = getSymbolArity(expression, operator, scope, unit);
|
|
242
275
|
if (arity !== expression.args.length) {
|
|
243
|
-
throw
|
|
244
|
-
`The operator ${
|
|
276
|
+
throw createSemanticError(
|
|
277
|
+
`The operator ${operator} requires ${arity} arguments.`,
|
|
278
|
+
expression.argumentsRange ?? getNameRange(expression.location, operator),
|
|
245
279
|
);
|
|
246
280
|
}
|
|
247
281
|
expression.args.forEach((arg, index) => {
|
|
@@ -275,10 +309,12 @@ const resolveExpression = (
|
|
|
275
309
|
case "ExceptAt":
|
|
276
310
|
return;
|
|
277
311
|
case "Identifier": {
|
|
278
|
-
const
|
|
312
|
+
const name = expression.name;
|
|
313
|
+
const arity = getSymbolArity(expression, name, scope, unit);
|
|
279
314
|
if (arity > 0) {
|
|
280
|
-
throw
|
|
281
|
-
`The operator ${
|
|
315
|
+
throw createSemanticError(
|
|
316
|
+
`The operator ${name} requires ${arity} arguments.`,
|
|
317
|
+
getNameRange(expression.location, name),
|
|
282
318
|
);
|
|
283
319
|
}
|
|
284
320
|
return;
|
|
@@ -347,14 +383,21 @@ const resolveExpression = (
|
|
|
347
383
|
resolveAll([expression.domain, expression.range]);
|
|
348
384
|
return;
|
|
349
385
|
case "Record":
|
|
350
|
-
case "RecordSet":
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
386
|
+
case "RecordSet": {
|
|
387
|
+
const repeated = expression.fields.filter((field, index) =>
|
|
388
|
+
expression.fields.slice(0, index).some((earlier) => earlier.name === field.name),
|
|
389
|
+
);
|
|
390
|
+
if (repeated.length > 0) {
|
|
391
|
+
throw createSanyError(
|
|
392
|
+
repeated.map((field) => ({
|
|
393
|
+
location: formatRange(field.location.range ?? getNameRange(field.location, field.name)),
|
|
394
|
+
message: "Non-unique fields in constructor.",
|
|
395
|
+
})),
|
|
354
396
|
);
|
|
355
397
|
}
|
|
356
398
|
resolveAll(expression.fields.map((field) => field.value));
|
|
357
399
|
return;
|
|
400
|
+
}
|
|
358
401
|
case "Except":
|
|
359
402
|
resolveAll([
|
|
360
403
|
expression.function,
|
|
@@ -769,6 +812,29 @@ export const resolveSubstitutions = (
|
|
|
769
812
|
);
|
|
770
813
|
};
|
|
771
814
|
|
|
815
|
+
// SANY reports the errors of every unit it checks, in the order of the units in the module.
|
|
816
|
+
export const collectSemanticErrors = (units: { location: SourceLocation; check: () => void }[]) => {
|
|
817
|
+
const errors: SanyError[] = [];
|
|
818
|
+
units
|
|
819
|
+
.toSorted((left, right) => (isBefore(left.location, right.location) ? -1 : 1))
|
|
820
|
+
.forEach((unit) => {
|
|
821
|
+
try {
|
|
822
|
+
unit.check();
|
|
823
|
+
} catch (error) {
|
|
824
|
+
if (!(error instanceof TlaSyntaxError) || !error.sany) throw error;
|
|
825
|
+
error.sany.errors
|
|
826
|
+
.filter((item) =>
|
|
827
|
+
errors.every(
|
|
828
|
+
(existing) =>
|
|
829
|
+
existing.location !== item.location || existing.message !== item.message,
|
|
830
|
+
),
|
|
831
|
+
)
|
|
832
|
+
.forEach((item) => errors.push(item));
|
|
833
|
+
}
|
|
834
|
+
});
|
|
835
|
+
if (errors.length > 0) throw createSanyError(errors);
|
|
836
|
+
};
|
|
837
|
+
|
|
772
838
|
export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
773
839
|
const symbols = createSymbols(module, unit.imported);
|
|
774
840
|
const facts = new Set(
|
|
@@ -782,13 +848,24 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
|
|
|
782
848
|
self,
|
|
783
849
|
facts,
|
|
784
850
|
});
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
851
|
+
collectSemanticErrors([
|
|
852
|
+
...module.definitions.map((definition) => ({
|
|
853
|
+
location: definition.location,
|
|
854
|
+
check: () =>
|
|
855
|
+
resolveDefinition(definition, undefined, createUnit(definition.location, definition)),
|
|
856
|
+
})),
|
|
857
|
+
...module.assumptions.map((assumption) => ({
|
|
858
|
+
location: assumption.location,
|
|
859
|
+
check: () =>
|
|
860
|
+
resolveExpression(
|
|
861
|
+
assumption.expression,
|
|
862
|
+
undefined,
|
|
863
|
+
createUnit(assumption.location, undefined),
|
|
864
|
+
),
|
|
865
|
+
})),
|
|
866
|
+
...module.theorems.map((theorem) => ({
|
|
867
|
+
location: theorem.location,
|
|
868
|
+
check: () => resolveTheorem(theorem, createUnit(theorem.location, undefined)),
|
|
869
|
+
})),
|
|
870
|
+
]);
|
|
794
871
|
};
|
package/src/run-cli.ts
CHANGED
|
@@ -14,12 +14,14 @@ import type { CheckResult, CheckStatus, TraceStep } from "./check";
|
|
|
14
14
|
import { defineCommand, type ParsedArgs } from "./cli-options";
|
|
15
15
|
import { parseConfig } from "./config";
|
|
16
16
|
import {
|
|
17
|
+
createSanyError,
|
|
17
18
|
getErrorExitCode,
|
|
18
19
|
TlaConfigError,
|
|
19
20
|
TlaError,
|
|
20
21
|
TlaEvaluationError,
|
|
21
22
|
TlaProofError,
|
|
22
23
|
TlaSyntaxError,
|
|
24
|
+
type SanyReport,
|
|
23
25
|
} from "./errors";
|
|
24
26
|
import { EXIT_STATUS } from "./exit-codes";
|
|
25
27
|
import type { LineRange } from "./obligation-generate";
|
|
@@ -143,6 +145,14 @@ const printResult = (result: CheckResult) => {
|
|
|
143
145
|
printState(result.failure.initialState);
|
|
144
146
|
}
|
|
145
147
|
printTrace(result.failure.trace);
|
|
148
|
+
const replay = result.failure.replay;
|
|
149
|
+
replay?.output.forEach((line) => console.log(line));
|
|
150
|
+
if (replay?.callStack) {
|
|
151
|
+
const frames = replay.callStack.map((frame, index) => `${index}. ${frame}\n`).join("");
|
|
152
|
+
console.log(
|
|
153
|
+
`Error: The error occurred when TLC was evaluating the nested\nexpressions at the following positions:\n${frames ? `${frames}\n` : " The error call stack is empty.\n"}`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
146
156
|
} else if (result.violation) {
|
|
147
157
|
const violation = result.violation;
|
|
148
158
|
const name = violation.name;
|
|
@@ -219,8 +229,14 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
|
|
|
219
229
|
const fileName = basename(specPath, ".tla");
|
|
220
230
|
const moduleName = parseModule(source).name;
|
|
221
231
|
if (moduleName !== fileName) {
|
|
222
|
-
throw
|
|
223
|
-
|
|
232
|
+
throw createSanyError(
|
|
233
|
+
[
|
|
234
|
+
{
|
|
235
|
+
location: "Unknown location",
|
|
236
|
+
message: `File name '${fileName}' does not match the name '${moduleName}' of the top level module it contains.`,
|
|
237
|
+
},
|
|
238
|
+
],
|
|
239
|
+
true,
|
|
224
240
|
);
|
|
225
241
|
}
|
|
226
242
|
const configPath =
|
|
@@ -286,21 +302,32 @@ const checkSpec = async (specPath: string, args: ParsedArgs) => {
|
|
|
286
302
|
return result.failure?.exitCode ?? EXIT_CODES[result.status];
|
|
287
303
|
};
|
|
288
304
|
|
|
289
|
-
|
|
290
|
-
|
|
305
|
+
const formatSanyErrors = (report: SanyReport) =>
|
|
306
|
+
`*** Errors: ${report.errors.length}\n\n${report.errors.map((error) => `${error.location}\n\n${error.message}\n\n`).join("\n")}`;
|
|
307
|
+
|
|
308
|
+
// TLC prints the errors it raises itself, and SANY's report of a spec it rejects. tla-ts names the
|
|
309
|
+
// others, such as a module it cannot parse.
|
|
310
|
+
const printError = (error: TlaError, path?: string) => {
|
|
291
311
|
if (error instanceof TlaConfigError || error instanceof TlaEvaluationError) {
|
|
292
312
|
return console.log(`Error: ${error.message}`);
|
|
293
313
|
}
|
|
294
|
-
|
|
314
|
+
const report = error instanceof TlaSyntaxError ? error.sany : undefined;
|
|
315
|
+
if (!report || path === undefined) return console.error(`${error.name}: ${error.message}`);
|
|
316
|
+
console.log(
|
|
317
|
+
report.isFatal
|
|
318
|
+
? `\nFatal errors while parsing TLA+ spec in file ${basename(path, ".tla")}\n\n${report.errors.map((item) => `${item.location}\n\n${item.message}\n`).join("")}${formatSanyErrors(report)}\n`
|
|
319
|
+
: `Semantic errors:\n\n${formatSanyErrors(report)}`,
|
|
320
|
+
);
|
|
321
|
+
console.log("Error: Parsing or semantic analysis failed.");
|
|
295
322
|
};
|
|
296
323
|
|
|
297
324
|
// Prints a TlaError and returns its exit code.
|
|
298
|
-
const reportErrors = async (run: () => number | Promise<number
|
|
325
|
+
const reportErrors = async (run: () => number | Promise<number>, path?: string) => {
|
|
299
326
|
try {
|
|
300
327
|
return await run();
|
|
301
328
|
} catch (error) {
|
|
302
329
|
if (!(error instanceof TlaError)) throw error;
|
|
303
|
-
printError(error);
|
|
330
|
+
printError(error, path);
|
|
304
331
|
return getErrorExitCode(error);
|
|
305
332
|
}
|
|
306
333
|
};
|
|
@@ -311,7 +338,7 @@ const runEach = async (paths: string[], runPath: (path: string) => number | Prom
|
|
|
311
338
|
const exitCodes: number[] = [];
|
|
312
339
|
for (const [index, path] of paths.entries()) {
|
|
313
340
|
if (index > 0) console.log("");
|
|
314
|
-
exitCodes.push(await reportErrors(() => runPath(path)));
|
|
341
|
+
exitCodes.push(await reportErrors(() => runPath(path), path));
|
|
315
342
|
}
|
|
316
343
|
return exitCodes.find((exitCode) => exitCode !== EXIT_STATUS.success) ?? EXIT_STATUS.success;
|
|
317
344
|
};
|
package/src/spec-description.ts
CHANGED
|
@@ -7,13 +7,12 @@ import {
|
|
|
7
7
|
} from "./ast";
|
|
8
8
|
import type { ModelConfig } from "./config";
|
|
9
9
|
import type { InstanceAction } from "./environment";
|
|
10
|
-
import { TlaError } from "./errors";
|
|
10
|
+
import { formatRange, TlaError } from "./errors";
|
|
11
11
|
import {
|
|
12
12
|
bindDefinitions,
|
|
13
13
|
bindOperatorArguments,
|
|
14
14
|
evaluate,
|
|
15
15
|
forEachBinding,
|
|
16
|
-
formatRange,
|
|
17
16
|
getDefinition,
|
|
18
17
|
isConstantLevel,
|
|
19
18
|
lookupScope,
|
package/src/successors.ts
CHANGED
|
@@ -89,24 +89,54 @@ const assign = (partial: State, index: number, value: Value) => {
|
|
|
89
89
|
return copy;
|
|
90
90
|
};
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
// TLC's Tool.getInitStates and Tool.getNextStates report a value where they expect a formula.
|
|
93
|
+
const expectFormula = (expression: Expression, value: Value, isInit: boolean) => {
|
|
93
94
|
if (typeof value === "boolean") return value;
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
95
|
+
throw new TlaEvaluationError(
|
|
96
|
+
`In computing ${isInit ? "initial" : "next"} states, TLC expected a boolean expression,\nbut instead found ${formatValue(value)}.\n${describeRange(expression)}`,
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const holds = (expression: Expression, context: Context, value = evaluate(expression, context)) => {
|
|
101
|
+
// TLC rejects a number in the next-state relation when it splits the relation into actions.
|
|
102
|
+
if (context.next !== undefined && expression.kind === "Number") {
|
|
103
|
+
throw setExitCode(
|
|
104
|
+
new TlaEvaluationError(
|
|
105
|
+
`The next state relation is not a boolean expression.\n${expression.text ?? expression.value}`,
|
|
106
|
+
),
|
|
107
|
+
EXIT_STATUS.error,
|
|
108
|
+
);
|
|
99
109
|
}
|
|
110
|
+
return expectFormula(expression, value, context.next === undefined);
|
|
100
111
|
};
|
|
101
112
|
|
|
102
113
|
const isAssignmentOperator = (operator: string) =>
|
|
103
114
|
operator === "=" || operator === "\\in" || operator === "\\subseteq";
|
|
104
115
|
|
|
116
|
+
const tracedRunners = new WeakMap<Environment, WeakMap<Expression, Runner>[]>();
|
|
117
|
+
|
|
118
|
+
const getRunners = (environment: Environment, isInit: boolean) => {
|
|
119
|
+
if (!environment.trace) return isInit ? initRunners : nextRunners;
|
|
120
|
+
const runners = tracedRunners.get(environment) ?? [new WeakMap(), new WeakMap()];
|
|
121
|
+
tracedRunners.set(environment, runners);
|
|
122
|
+
return runners[isInit ? 0 : 1];
|
|
123
|
+
};
|
|
124
|
+
|
|
105
125
|
const getRunner = (expression: Expression, environment: Environment, isInit: boolean): Runner => {
|
|
106
|
-
const runners = isInit
|
|
126
|
+
const runners = getRunners(environment, isInit);
|
|
107
127
|
const cached = runners.get(expression);
|
|
108
128
|
if (cached) return cached;
|
|
109
|
-
const
|
|
129
|
+
const compiled = compileAction(expression, environment, isInit);
|
|
130
|
+
const trace = environment.trace;
|
|
131
|
+
// TLC's CallStackTool keeps an action's expression on the stack while the rest of the action runs.
|
|
132
|
+
const runner: Runner = trace
|
|
133
|
+
? (run, scope, partial, action, isFixed, onComplete) => {
|
|
134
|
+
const depth = trace.length;
|
|
135
|
+
trace.push(expression);
|
|
136
|
+
compiled(run, scope, partial, action, isFixed, onComplete);
|
|
137
|
+
trace.length = depth;
|
|
138
|
+
}
|
|
139
|
+
: compiled;
|
|
110
140
|
runners.set(expression, runner);
|
|
111
141
|
return runner;
|
|
112
142
|
};
|
|
@@ -190,6 +220,23 @@ const getUnchangedIndices = (
|
|
|
190
220
|
return getUnchangedIndices(definition.body, environment, visited);
|
|
191
221
|
};
|
|
192
222
|
|
|
223
|
+
// TLC's CallStackTool keeps each expression that UNCHANGED takes apart on the stack.
|
|
224
|
+
const getUnchangedFrames = (expression: Expression, environment: Environment): Expression[] => {
|
|
225
|
+
if (expression.kind === "Tuple") {
|
|
226
|
+
return [
|
|
227
|
+
expression,
|
|
228
|
+
...expression.elements.flatMap((element) => getUnchangedFrames(element, environment)),
|
|
229
|
+
];
|
|
230
|
+
}
|
|
231
|
+
const definition =
|
|
232
|
+
expression.kind === "Identifier" && !environment.variableIndex.has(expression.name)
|
|
233
|
+
? getZeroArityBody(expression.name, environment)
|
|
234
|
+
: undefined;
|
|
235
|
+
return definition
|
|
236
|
+
? [expression, ...getUnchangedFrames(definition.body, environment)]
|
|
237
|
+
: [expression];
|
|
238
|
+
};
|
|
239
|
+
|
|
193
240
|
const ACTION_KINDS = new Set([
|
|
194
241
|
"Label",
|
|
195
242
|
"And",
|
|
@@ -322,6 +369,7 @@ const compileConjunction = (
|
|
|
322
369
|
): Runner => {
|
|
323
370
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
324
371
|
const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
|
|
372
|
+
const trace = environment.trace;
|
|
325
373
|
const runFrom = (
|
|
326
374
|
start: number,
|
|
327
375
|
run: Run,
|
|
@@ -342,6 +390,12 @@ const compileConjunction = (
|
|
|
342
390
|
return;
|
|
343
391
|
}
|
|
344
392
|
const conjunct = conjuncts[index];
|
|
393
|
+
if (trace) {
|
|
394
|
+
trace.push(conjunct.expression);
|
|
395
|
+
if (conjunct.expression.kind === "Unchanged") {
|
|
396
|
+
trace.push(...getUnchangedFrames(conjunct.expression.expression, environment));
|
|
397
|
+
}
|
|
398
|
+
}
|
|
345
399
|
if (conjunct.kind === "Guard") {
|
|
346
400
|
context ??= getContext(run, scope, current);
|
|
347
401
|
const evaluator = (evaluators[index] ??= getEntryEvaluator(
|
|
@@ -391,10 +445,7 @@ const compileConjunction = (
|
|
|
391
445
|
};
|
|
392
446
|
|
|
393
447
|
const holdsInInit = (expression: Expression, context: Context) =>
|
|
394
|
-
|
|
395
|
-
evaluate(expression, context),
|
|
396
|
-
`the initial predicate at ${describeLocation(expression)}`,
|
|
397
|
-
);
|
|
448
|
+
expectFormula(expression, evaluate(expression, context), true);
|
|
398
449
|
|
|
399
450
|
const compileGuard =
|
|
400
451
|
(expression: Expression): Runner =>
|
|
@@ -419,7 +470,7 @@ const compileMembership = (
|
|
|
419
470
|
expression.operator === "\\subseteq" ? { kind: "Subset", base: evaluated } : evaluated;
|
|
420
471
|
if (!isEnumerable(value)) {
|
|
421
472
|
throw new TlaEvaluationError(
|
|
422
|
-
`In computing next states, the right side of \\
|
|
473
|
+
`In computing ${run.isInit ? "initial" : "next"} states, the right side of \\IN is not enumerable.\n${describeRange(expression)}`,
|
|
423
474
|
);
|
|
424
475
|
}
|
|
425
476
|
const elements = getStreamedElements(value);
|
|
@@ -763,7 +814,8 @@ const compileAction = (
|
|
|
763
814
|
}
|
|
764
815
|
};
|
|
765
816
|
}
|
|
766
|
-
const hoisted =
|
|
817
|
+
const hoisted =
|
|
818
|
+
isInit || environment.trace ? undefined : getHoistedGuards(expression, environment);
|
|
767
819
|
if (hoisted) {
|
|
768
820
|
const rest = compileConjunction(hoisted.rest, environment, isInit);
|
|
769
821
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
@@ -1058,7 +1110,9 @@ export const forEachSuccessor = (
|
|
|
1058
1110
|
if (run.isStopped) return;
|
|
1059
1111
|
if (!isComplete(partial)) {
|
|
1060
1112
|
throw new TlaIncompleteStateError(
|
|
1061
|
-
|
|
1113
|
+
partial,
|
|
1114
|
+
action,
|
|
1115
|
+
getMissingVariables(partial, environment).sort(),
|
|
1062
1116
|
);
|
|
1063
1117
|
}
|
|
1064
1118
|
counter.successors++;
|
package/src/symmetry.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { TlaConfigError } from "./errors";
|
|
1
|
+
import { TlaConfigError, TlaEvaluationError, UNEXPECTED_EXCEPTION } from "./errors";
|
|
2
2
|
import { evaluate, type Environment } from "./evaluate";
|
|
3
|
+
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
3
4
|
import {
|
|
4
5
|
compareValues,
|
|
5
6
|
createFunction,
|
|
@@ -47,9 +48,13 @@ const composePermutations = (first: Images, second: Images) => {
|
|
|
47
48
|
return composed;
|
|
48
49
|
};
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
// TLC checks the symmetry set outside any phase that reports an error itself.
|
|
52
|
+
const createSymmetryError = (message: string) =>
|
|
53
|
+
setExitCode(new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${message}`), EXIT_STATUS.error);
|
|
54
|
+
|
|
55
|
+
const toImages = (value: Value) => {
|
|
51
56
|
if (!isFunctionValue(value)) {
|
|
52
|
-
throw
|
|
57
|
+
throw createSymmetryError("The symmetry values must be function records.");
|
|
53
58
|
}
|
|
54
59
|
const functionValue = toFiniteFunction(value);
|
|
55
60
|
const permutation: Images = new Map();
|
|
@@ -65,8 +70,13 @@ const toImages = (value: Value, name: string) => {
|
|
|
65
70
|
|
|
66
71
|
export const getSymmetryGroup = (name: string, environment: Environment): Permutation[] => {
|
|
67
72
|
const definition = environment.definitions.get(name);
|
|
73
|
+
if (definition === undefined && !environment.constants.has(name)) {
|
|
74
|
+
throw new TlaConfigError(
|
|
75
|
+
`The symmetry function ${name} specified in the configuration file\nis not defined in the specification.`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
68
78
|
if (definition?.kind !== "Operator" || definition.params.length > 0) {
|
|
69
|
-
throw
|
|
79
|
+
throw createSymmetryError(`The symmetry function ${name} must specify a set of permutations.`);
|
|
70
80
|
}
|
|
71
81
|
const value = evaluate(definition.body, {
|
|
72
82
|
environment,
|
|
@@ -75,9 +85,9 @@ export const getSymmetryGroup = (name: string, environment: Environment): Permut
|
|
|
75
85
|
next: undefined,
|
|
76
86
|
});
|
|
77
87
|
if (!isSetValue(value) || !isEnumerable(value)) {
|
|
78
|
-
throw
|
|
88
|
+
throw createSymmetryError("The symmetry operator must specify a set of functions.");
|
|
79
89
|
}
|
|
80
|
-
const permutations = toFiniteSet(value).elements.map(
|
|
90
|
+
const permutations = toFiniteSet(value).elements.map(toImages);
|
|
81
91
|
const isDisabled =
|
|
82
92
|
definition.body.kind === "Apply" && definition.body.args.length >= permutations.length;
|
|
83
93
|
if (isDisabled) return [];
|
package/src/tableau.ts
CHANGED
|
@@ -289,7 +289,7 @@ const toDisjunctiveNormalForm = (expression: LiveExpression): LiveExpression =>
|
|
|
289
289
|
total.value *= normal.operands.length;
|
|
290
290
|
if (total.value > MAXIMUM_DISJUNCTS) {
|
|
291
291
|
throw new TlaEvaluationError(
|
|
292
|
-
"TLC cannot handle the temporal formula because it exceeds the maximum supported size in disjunctive normal form",
|
|
292
|
+
"TLC cannot handle the temporal formula because it exceeds the maximum supported size in disjunctive normal form.",
|
|
293
293
|
);
|
|
294
294
|
}
|
|
295
295
|
}
|
package/src/tlc-module.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TlaModuleError } from "./errors";
|
|
1
|
+
import { TlaEvaluationError, TlaModuleError } from "./errors";
|
|
2
2
|
import { EXIT_STATUS } from "./exit-codes";
|
|
3
3
|
import type { Builtin } from "./evaluate";
|
|
4
4
|
import { prettyPrint } from "./pretty-print";
|
|
@@ -48,7 +48,7 @@ const getRuntimeValue = (
|
|
|
48
48
|
state: (Value | undefined)[] | undefined,
|
|
49
49
|
) => {
|
|
50
50
|
if (!runtime.isStarted && CHECKER_KEYS.has(key)) {
|
|
51
|
-
throw new
|
|
51
|
+
throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
52
52
|
}
|
|
53
53
|
const duration = Math.floor((Date.now() - runtime.startedAt) / 1000);
|
|
54
54
|
switch (key) {
|
|
@@ -104,11 +104,11 @@ const getRuntimeValue = (
|
|
|
104
104
|
}
|
|
105
105
|
if (key.startsWith(NAMED_REGISTER_PREFIX)) {
|
|
106
106
|
const value = getNamedRegisters(runtime).get(key);
|
|
107
|
-
if (value === undefined) throw new
|
|
107
|
+
if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
108
108
|
return value;
|
|
109
109
|
}
|
|
110
110
|
if (key.startsWith("-D")) return rankString(runtime.properties.get(key.slice(2)) ?? key);
|
|
111
|
-
throw new
|
|
111
|
+
throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
112
112
|
};
|
|
113
113
|
|
|
114
114
|
const getPermutations = (elements: Value[]): Value[][] =>
|
|
@@ -197,11 +197,11 @@ export const createTlcOperators = (
|
|
|
197
197
|
const key = getValue(args, 0, "TLCGet");
|
|
198
198
|
if (typeof key === "number" && key >= 0) {
|
|
199
199
|
const value = runtime.registers.get(key);
|
|
200
|
-
if (value === undefined) throw new
|
|
200
|
+
if (value === undefined) throw new TlaEvaluationError(`TLCGet(${key}) was undefined.`);
|
|
201
201
|
return value;
|
|
202
202
|
}
|
|
203
203
|
if (typeof key !== "string") {
|
|
204
|
-
throw new
|
|
204
|
+
throw new TlaEvaluationError(
|
|
205
205
|
`The argument of TLCGet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
|
|
206
206
|
);
|
|
207
207
|
}
|
package/src/tlc-modules.ts
CHANGED
|
@@ -1013,6 +1013,10 @@ export const TLC_JAVA_METHODS = new Map([
|
|
|
1013
1013
|
"RandomElement",
|
|
1014
1014
|
"public static tlc2.value.impl.Value tlc2.module.TLC.RandomElement(tlc2.value.impl.Value)",
|
|
1015
1015
|
],
|
|
1016
|
+
[
|
|
1017
|
+
"TLCGet",
|
|
1018
|
+
"public static tlc2.value.impl.Value tlc2.module.TLCGetSet.TLCGetEval(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
|
|
1019
|
+
],
|
|
1016
1020
|
[
|
|
1017
1021
|
"TLCEval",
|
|
1018
1022
|
"public static tlc2.value.impl.Value tlc2.module.TLCEval.tlcEval(tlc2.tool.impl.Tool,tla2sany.semantic.ExprOrOpArgNode[],tlc2.util.Context,tlc2.tool.TLCState,tlc2.tool.TLCState,int,tlc2.tool.coverage.CostModel)",
|