tla-ts 0.0.3-dev.14337b3 → 0.0.3-dev.19a7038
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 +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/run-cli.cjs +4 -3
- package/dist/run-cli.js +4 -3
- package/dist/transfer.cjs +15 -15
- package/dist/transfer.js +15 -15
- package/package.json +1 -1
- package/src/builtins.ts +9 -3
- package/src/check.ts +210 -40
- package/src/environment.ts +5 -1
- package/src/errors.ts +22 -0
- package/src/evaluate.ts +131 -9
- package/src/fingerprints.ts +2 -1
- package/src/liveness.ts +5 -1
- package/src/run-cli.ts +29 -1
- package/src/standard-modules.ts +52 -38
- package/src/successors.ts +87 -17
- package/src/value-sources.ts +29 -0
- package/src/values.ts +122 -42
package/src/evaluate.ts
CHANGED
|
@@ -12,13 +12,14 @@ import {
|
|
|
12
12
|
type RecordField,
|
|
13
13
|
} from "./ast";
|
|
14
14
|
import { getKindString } from "./builtins";
|
|
15
|
-
import { formatRange, TlaEvaluationError } from "./errors";
|
|
15
|
+
import { formatRange, TlaEvaluationError, TlaUnassignedError } from "./errors";
|
|
16
16
|
import { EXIT_STATUS, setExitCode, setOwnExitCode } from "./exit-codes";
|
|
17
17
|
import { generateEvaluator } from "./codegen";
|
|
18
18
|
import type { CheckerRuntime } from "./runtime";
|
|
19
19
|
import { getReadVariables } from "./dependencies";
|
|
20
20
|
import { contextualApplications, mapChildren, substitutedExpressions } from "./instances";
|
|
21
21
|
import { isEnabled } from "./successors";
|
|
22
|
+
import { getFingerprintTrace, valueSources, withSource } from "./value-sources";
|
|
22
23
|
import {
|
|
23
24
|
applyFunction,
|
|
24
25
|
combineHash,
|
|
@@ -34,6 +35,7 @@ import {
|
|
|
34
35
|
getDomain,
|
|
35
36
|
getFunctionDomain,
|
|
36
37
|
getIntervalElements,
|
|
38
|
+
getLazySubsets,
|
|
37
39
|
getSequenceElements,
|
|
38
40
|
getValueHash,
|
|
39
41
|
isFunctionValue,
|
|
@@ -48,6 +50,7 @@ import {
|
|
|
48
50
|
toFiniteSet,
|
|
49
51
|
updateFunction,
|
|
50
52
|
valuesEqual,
|
|
53
|
+
type FilteredSetValue,
|
|
51
54
|
type FunctionValue,
|
|
52
55
|
type LazyFunctionSource,
|
|
53
56
|
type LazyFunctionValue,
|
|
@@ -116,6 +119,7 @@ export interface Environment {
|
|
|
116
119
|
definitionValues: Map<Definition, Value>;
|
|
117
120
|
constantLevels: Map<Definition, boolean>;
|
|
118
121
|
evaluators: WeakMap<Expression, Evaluator>;
|
|
122
|
+
trace?: Expression[];
|
|
119
123
|
}
|
|
120
124
|
|
|
121
125
|
export interface Context {
|
|
@@ -339,9 +343,8 @@ export const getEnumeratedElements = (set: Value, expression: Expression) => {
|
|
|
339
343
|
: `TLC encountered the non-enumerable quantifier bound\n${text}\n${describeRange(expression)}\n${explanation}`,
|
|
340
344
|
);
|
|
341
345
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
: toFiniteSet(set).elements;
|
|
346
|
+
if (typeof set === "object" && set.kind === "Interval") return getIntervalElements(set);
|
|
347
|
+
return getLazySubsets(set) ?? toFiniteSet(set).elements;
|
|
345
348
|
};
|
|
346
349
|
|
|
347
350
|
export const forEachBinding = (
|
|
@@ -1019,16 +1022,80 @@ const isStateCached = (expression: Expression, environment: Environment) =>
|
|
|
1019
1022
|
getReadVariables(expression, environment) !== undefined &&
|
|
1020
1023
|
!hasFreeLocals(expression, NO_BOUND_NAMES);
|
|
1021
1024
|
|
|
1025
|
+
// CallStackTool also evaluates the predicate of a lazy set filter it returns, even one that TLC
|
|
1026
|
+
// made before the replay.
|
|
1027
|
+
const filteredSetRetracers = new WeakMap<
|
|
1028
|
+
FilteredSetValue,
|
|
1029
|
+
(environment: Environment) => FilteredSetValue
|
|
1030
|
+
>();
|
|
1031
|
+
|
|
1032
|
+
const SOURCED_KINDS = new Set<Expression["kind"]>([
|
|
1033
|
+
"SetEnumeration",
|
|
1034
|
+
"SetMap",
|
|
1035
|
+
"SetFilter",
|
|
1036
|
+
"Tuple",
|
|
1037
|
+
"Record",
|
|
1038
|
+
"RecordSet",
|
|
1039
|
+
"FunctionSet",
|
|
1040
|
+
"Product",
|
|
1041
|
+
]);
|
|
1042
|
+
|
|
1043
|
+
const SOURCED_OPERATORS = new Set(["SUBSET", "UNION", "DOMAIN", "\\cup", "\\cap", "\\"]);
|
|
1044
|
+
|
|
1045
|
+
// CallStackTool gives a set, tuple or record it builds the expression as its source, but not a lazy
|
|
1046
|
+
// set filter, which it returns as a copy.
|
|
1047
|
+
const isSourced = (expression: Expression, environment: Environment) =>
|
|
1048
|
+
SOURCED_KINDS.has(expression.kind) ||
|
|
1049
|
+
(expression.kind === "Apply" &&
|
|
1050
|
+
SOURCED_OPERATORS.has(expression.operator) &&
|
|
1051
|
+
!isLocallyResolved(expression) &&
|
|
1052
|
+
resolveGlobalOperator(expression.operator, environment)?.kind === "Builtin");
|
|
1053
|
+
|
|
1054
|
+
// When an evaluation fails, CallStackTool freezes the stack at the innermost expression and pushes
|
|
1055
|
+
// the source of each value whose method failed.
|
|
1056
|
+
const evaluateTraced = (
|
|
1057
|
+
evaluator: Evaluator,
|
|
1058
|
+
context: Context,
|
|
1059
|
+
trace: Expression[],
|
|
1060
|
+
depth: number,
|
|
1061
|
+
) => {
|
|
1062
|
+
try {
|
|
1063
|
+
return evaluator(context);
|
|
1064
|
+
} catch (error) {
|
|
1065
|
+
if (trace.length === depth + 1) trace.push(...(getFingerprintTrace(error) ?? []));
|
|
1066
|
+
throw error;
|
|
1067
|
+
}
|
|
1068
|
+
};
|
|
1069
|
+
|
|
1022
1070
|
export const getEvaluator = (expression: Expression, environment: Environment): Evaluator => {
|
|
1023
1071
|
const cached = environment.evaluators.get(expression);
|
|
1024
1072
|
if (cached) return cached;
|
|
1025
1073
|
const compiled = compile(expression, environment);
|
|
1026
|
-
const
|
|
1074
|
+
const cachedEvaluator =
|
|
1027
1075
|
isWorthCaching(expression) && isClosedConstant(expression, environment)
|
|
1028
1076
|
? cacheFirstValue(compiled)
|
|
1029
1077
|
: isStateCached(expression, environment)
|
|
1030
1078
|
? cacheStateValue(compiled)
|
|
1031
1079
|
: compiled;
|
|
1080
|
+
const trace = environment.trace;
|
|
1081
|
+
const isSourcedExpression = trace !== undefined && isSourced(expression, environment);
|
|
1082
|
+
// TLC's CallStackTool pushes each expression it evaluates and keeps the stack when one fails.
|
|
1083
|
+
const evaluator: Evaluator = trace
|
|
1084
|
+
? (context) => {
|
|
1085
|
+
const depth = trace.length;
|
|
1086
|
+
trace.push(expression);
|
|
1087
|
+
const value = evaluateTraced(cachedEvaluator, context, trace, depth);
|
|
1088
|
+
trace.length = depth;
|
|
1089
|
+
if (isSourcedExpression && typeof value === "object" && value.kind !== "FilteredSet") {
|
|
1090
|
+
valueSources.current?.set(value, expression);
|
|
1091
|
+
}
|
|
1092
|
+
const retrace =
|
|
1093
|
+
typeof value === "object" && value.kind === "FilteredSet"
|
|
1094
|
+
? filteredSetRetracers.get(value)
|
|
1095
|
+
: undefined;
|
|
1096
|
+
return retrace ? retrace(environment) : value;
|
|
1097
|
+
}
|
|
1098
|
+
: cachedEvaluator;
|
|
1032
1099
|
environment.evaluators.set(expression, evaluator);
|
|
1033
1100
|
return evaluator;
|
|
1034
1101
|
};
|
|
@@ -1036,6 +1103,7 @@ export const getEvaluator = (expression: Expression, environment: Environment):
|
|
|
1036
1103
|
const entryEvaluators = new WeakMap<Expression, Evaluator>();
|
|
1037
1104
|
|
|
1038
1105
|
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1106
|
+
if (environment.trace) return getEvaluator(expression, environment);
|
|
1039
1107
|
const cached = entryEvaluators.get(expression);
|
|
1040
1108
|
if (cached) return cached;
|
|
1041
1109
|
const evaluator =
|
|
@@ -1144,11 +1212,32 @@ const getLazyArguments = (args: Expression[], context: Context): OperatorValue[]
|
|
|
1144
1212
|
expression: arg,
|
|
1145
1213
|
}));
|
|
1146
1214
|
|
|
1215
|
+
const RECEIVING_OPERATORS = new Set(["=", "#", "\\in", "\\notin", "\\cup", "\\cap", "\\"]);
|
|
1216
|
+
|
|
1217
|
+
// The argument whose method TLC calls to apply a built-in operator.
|
|
1218
|
+
const getReceiver = (name: string, left: Value, right: Value) =>
|
|
1219
|
+
name === "\\in" ||
|
|
1220
|
+
name === "\\notin" ||
|
|
1221
|
+
((name === "\\cup" || name === "\\cap") && !isReducible(left))
|
|
1222
|
+
? right
|
|
1223
|
+
: left;
|
|
1224
|
+
|
|
1147
1225
|
const compileBuiltinCall = (
|
|
1148
1226
|
builtin: Builtin,
|
|
1149
1227
|
args: Expression[],
|
|
1150
1228
|
environment: Environment,
|
|
1151
1229
|
): Evaluator => {
|
|
1230
|
+
if (environment.trace && RECEIVING_OPERATORS.has(builtin.name) && args.length === 2) {
|
|
1231
|
+
const evaluateLeft = getEvaluator(args[0], environment);
|
|
1232
|
+
const evaluateRight = getEvaluator(args[1], environment);
|
|
1233
|
+
return (context) => {
|
|
1234
|
+
const left = evaluateLeft(context);
|
|
1235
|
+
const right = evaluateRight(context);
|
|
1236
|
+
return withSource(getReceiver(builtin.name, left, right), () =>
|
|
1237
|
+
builtin.evaluate([left, right], context),
|
|
1238
|
+
);
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1152
1241
|
if (builtin.isLazy) {
|
|
1153
1242
|
return (context) => builtin.evaluate(getLazyArguments(args, context), context);
|
|
1154
1243
|
}
|
|
@@ -1308,6 +1397,13 @@ const compileApply = (
|
|
|
1308
1397
|
if (global?.kind === "Builtin" && global.name === "DOMAIN") {
|
|
1309
1398
|
const operand = getEvaluator(args[0], environment);
|
|
1310
1399
|
const domain = getDomainOperator(expression);
|
|
1400
|
+
// TLC builds a new interval for the domain of a tuple, so the source it gives one stays its own.
|
|
1401
|
+
if (environment.trace) {
|
|
1402
|
+
return (context) => {
|
|
1403
|
+
const value = domain(operand(context));
|
|
1404
|
+
return typeof value === "object" && value.kind === "Interval" ? { ...value } : value;
|
|
1405
|
+
};
|
|
1406
|
+
}
|
|
1311
1407
|
return (context) => domain(operand(context));
|
|
1312
1408
|
}
|
|
1313
1409
|
// TLC sorts the value of a constant TLCEval argument only where no context binds a name.
|
|
@@ -1323,6 +1419,13 @@ const compileApply = (
|
|
|
1323
1419
|
const left = getEvaluator(args[0], environment);
|
|
1324
1420
|
const right = getEvaluator(args[1], environment);
|
|
1325
1421
|
const subset = getSubsetOperator(expression, global);
|
|
1422
|
+
if (environment.trace) {
|
|
1423
|
+
return (context) => {
|
|
1424
|
+
const subsetValue = left(context);
|
|
1425
|
+
const superset = right(context);
|
|
1426
|
+
return withSource(subsetValue, () => subset(subsetValue, superset));
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1326
1429
|
return (context) => subset(left(context), right(context));
|
|
1327
1430
|
}
|
|
1328
1431
|
if (global?.kind === "Builtin") return compileBuiltinCall(global, args, environment);
|
|
@@ -1528,13 +1631,21 @@ const compileSetComprehension = (
|
|
|
1528
1631
|
const pattern = expression.bound.pattern;
|
|
1529
1632
|
return (context) => {
|
|
1530
1633
|
const base = set(context);
|
|
1531
|
-
const
|
|
1634
|
+
const getTest = (evaluatePredicate: Evaluator) => (element: Value) =>
|
|
1532
1635
|
expectBoolean(
|
|
1533
|
-
|
|
1636
|
+
evaluatePredicate(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1534
1637
|
"a set filter predicate",
|
|
1535
1638
|
);
|
|
1639
|
+
const test = getTest(predicate);
|
|
1536
1640
|
if (!isReducible(base)) {
|
|
1537
|
-
if (isSetValue(base))
|
|
1641
|
+
if (isSetValue(base)) {
|
|
1642
|
+
const filtered: FilteredSetValue = { kind: "FilteredSet", base, test };
|
|
1643
|
+
filteredSetRetracers.set(filtered, (traced) => ({
|
|
1644
|
+
...filtered,
|
|
1645
|
+
test: getTest(getEvaluator(expression.predicate, traced)),
|
|
1646
|
+
}));
|
|
1647
|
+
return filtered;
|
|
1648
|
+
}
|
|
1538
1649
|
throw new TlaEvaluationError(
|
|
1539
1650
|
`TLC encountered a non-enumerable quantifier bound ${formatValue(base)} at ${describeLocation(expression.bound.set)}`,
|
|
1540
1651
|
);
|
|
@@ -1588,8 +1699,9 @@ export const hasDuplicateFields = (sortedFields: RecordField[]) =>
|
|
|
1588
1699
|
|
|
1589
1700
|
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
1590
1701
|
setOwnExitCode(
|
|
1591
|
-
new
|
|
1702
|
+
new TlaUnassignedError(
|
|
1592
1703
|
`In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
|
|
1704
|
+
name,
|
|
1593
1705
|
),
|
|
1594
1706
|
EXIT_STATUS.configParseError,
|
|
1595
1707
|
);
|
|
@@ -1781,6 +1893,16 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1781
1893
|
const evaluateFunction = child(expression.function);
|
|
1782
1894
|
const args = expression.args.map(child);
|
|
1783
1895
|
const describeNonFunction = getNonFunctionError(expression);
|
|
1896
|
+
if (args.length === 1 && environment.trace) {
|
|
1897
|
+
const argument = args[0];
|
|
1898
|
+
return (context) => {
|
|
1899
|
+
const functionValue = evaluateFunction(context);
|
|
1900
|
+
const argumentValue = argument(context);
|
|
1901
|
+
const apply = () =>
|
|
1902
|
+
applyFunction(functionValue, argumentValue, undefined, describeNonFunction);
|
|
1903
|
+
return isFunctionValue(functionValue) ? withSource(functionValue, apply) : apply();
|
|
1904
|
+
};
|
|
1905
|
+
}
|
|
1784
1906
|
if (args.length === 1) {
|
|
1785
1907
|
const argument = args[0];
|
|
1786
1908
|
return (context) =>
|
package/src/fingerprints.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getValueFingerprint, getValueHash, type Value } from "./values";
|
|
1
|
+
import { assertFingerprintable, getValueFingerprint, getValueHash, type Value } from "./values";
|
|
2
2
|
|
|
3
3
|
const EMPTY = 0;
|
|
4
4
|
const INITIAL_CAPACITY = 1 << 12;
|
|
@@ -20,6 +20,7 @@ export const writeStateFingerprint = (state: Value[], fingerprint: Int32Array) =
|
|
|
20
20
|
first = Math.imul(first ^ getValueHash(state[index]), 0x01000193);
|
|
21
21
|
second = Math.imul(second ^ getValueFingerprint(state[index]), 0x5bd1e995);
|
|
22
22
|
}
|
|
23
|
+
assertFingerprintable(state);
|
|
23
24
|
fingerprint[0] = mix(first) || 1;
|
|
24
25
|
fingerprint[1] = mix(second);
|
|
25
26
|
};
|
package/src/liveness.ts
CHANGED
|
@@ -210,12 +210,16 @@ export const expandTemporal = (
|
|
|
210
210
|
if (expanding.has(definition)) {
|
|
211
211
|
throw setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error);
|
|
212
212
|
}
|
|
213
|
-
|
|
213
|
+
const expanded = expand(
|
|
214
214
|
definition.body,
|
|
215
215
|
undefined,
|
|
216
216
|
expression.name,
|
|
217
217
|
new Set([...expanding, definition]),
|
|
218
218
|
);
|
|
219
|
+
// TLC takes the body of a state-level definition in a specification as one initial predicate.
|
|
220
|
+
return !isProperty && getDefinitionLevel(definition, environment) < ACTION_LEVEL
|
|
221
|
+
? [{ expression: definition.body, scope: undefined, name: expression.name }]
|
|
222
|
+
: expanded;
|
|
219
223
|
}
|
|
220
224
|
}
|
|
221
225
|
const context = getConstantContext(environment, scope);
|
package/src/run-cli.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
import { spawnSync } from "node:child_process";
|
|
11
11
|
import { basename, delimiter, dirname, join, resolve } from "node:path";
|
|
12
12
|
import { availableParallelism } from "node:os";
|
|
13
|
-
import type { CheckResult, CheckStatus, TraceStep } from "./check";
|
|
13
|
+
import type { CheckResult, CheckStatus, Replay, TraceStep } from "./check";
|
|
14
14
|
import { defineCommand, type ParsedArgs } from "./cli-options";
|
|
15
15
|
import { parseConfig } from "./config";
|
|
16
16
|
import {
|
|
@@ -137,14 +137,42 @@ const getTemporalHeadline = (names: string[]) =>
|
|
|
137
137
|
? `Error: Temporal property ${names[0]} was violated.\n`
|
|
138
138
|
: `Error: Temporal properties ${names.slice(0, -1).join(", ")}${names.length > 2 ? "," : ""} and ${names[names.length - 1]} were violated.\n`;
|
|
139
139
|
|
|
140
|
+
const getFrames = (callStack: string[]) =>
|
|
141
|
+
callStack.map((frame, index) => `${index}. ${frame}\n`).join("");
|
|
142
|
+
|
|
143
|
+
const printCallStack = (frames: string) =>
|
|
144
|
+
console.log(
|
|
145
|
+
`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"}`,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const printReplay = (replay: Replay) => {
|
|
149
|
+
replay.output.forEach((line) => console.log(line));
|
|
150
|
+
const frames = getFrames(replay.callStack ?? []);
|
|
151
|
+
const fingerprint = replay.fingerprint;
|
|
152
|
+
if (replay.isBeforeError) {
|
|
153
|
+
if (frames) printCallStack(frames);
|
|
154
|
+
} else if (replay.callStack && fingerprint) {
|
|
155
|
+
const sources = fingerprint.sources.map((source) => `${source}\n`).join("");
|
|
156
|
+
console.log(
|
|
157
|
+
`Error: TLC was unable to fingerprint.\n\nFingerprint Stack Trace:\n${frames ? `${frames}\n` : sources}\nReason:\n${fingerprint.reason}`,
|
|
158
|
+
);
|
|
159
|
+
} else if (replay.callStack) {
|
|
160
|
+
printCallStack(frames);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
140
164
|
const printResult = (result: CheckResult) => {
|
|
141
165
|
if (result.failure) {
|
|
166
|
+
const replay = result.failure.replay;
|
|
167
|
+
if (replay?.isBeforeError) printReplay(replay);
|
|
142
168
|
console.log(`Error: ${result.failure.message}`);
|
|
143
169
|
if (result.failure.initialState) {
|
|
144
170
|
console.log("While working on the initial state:");
|
|
145
171
|
printState(result.failure.initialState);
|
|
146
172
|
}
|
|
147
173
|
printTrace(result.failure.trace);
|
|
174
|
+
if (replay && !replay.isBeforeError) printReplay(replay);
|
|
175
|
+
if (result.failure.isWithoutStatistics) return;
|
|
148
176
|
} else if (result.violation) {
|
|
149
177
|
const violation = result.violation;
|
|
150
178
|
const name = violation.name;
|
package/src/standard-modules.ts
CHANGED
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
getNumber,
|
|
12
12
|
getOperator,
|
|
13
13
|
getSet,
|
|
14
|
-
|
|
14
|
+
getArithmeticSignature,
|
|
15
15
|
getJavaInteger,
|
|
16
16
|
getValue,
|
|
17
17
|
isOutOfRange,
|
|
@@ -123,7 +123,7 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
123
123
|
),
|
|
124
124
|
defineOperator("\\subseteq", 2, (args) => {
|
|
125
125
|
const left = getSet(args, 0, "\\subseteq");
|
|
126
|
-
const right =
|
|
126
|
+
const right = getValue(args, 1, "\\subseteq");
|
|
127
127
|
const isPowerSets =
|
|
128
128
|
typeof left === "object" &&
|
|
129
129
|
left.kind === "Subset" &&
|
|
@@ -135,8 +135,8 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
135
135
|
}),
|
|
136
136
|
...SET_OPERATORS.map((operator) =>
|
|
137
137
|
defineOperator(operator, 2, (args) => {
|
|
138
|
-
const left =
|
|
139
|
-
const right =
|
|
138
|
+
const left = getValue(args, 0, operator);
|
|
139
|
+
const right = getValue(args, 1, operator);
|
|
140
140
|
if (isReducible(left) && isReducible(right) && !isMixedSet(left) && !isMixedSet(right)) {
|
|
141
141
|
if (operator === "\\cup" && isEmptySet(left)) return right;
|
|
142
142
|
const merge =
|
|
@@ -145,7 +145,13 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
145
145
|
: operator === "\\cap"
|
|
146
146
|
? intersectSorted
|
|
147
147
|
: differenceSorted;
|
|
148
|
-
|
|
148
|
+
// Merging compares elements across the sets. Where that fails, TLC's element-by-element
|
|
149
|
+
// membership test fails or succeeds on its own.
|
|
150
|
+
try {
|
|
151
|
+
return merge(toFiniteSet(left), toFiniteSet(right));
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
154
|
+
}
|
|
149
155
|
}
|
|
150
156
|
if (isReducible(left)) return combineReducible(operator, left, right);
|
|
151
157
|
if (operator !== "\\" && isReducible(right)) return combineReducible(operator, right, left);
|
|
@@ -179,51 +185,59 @@ export const CORE_OPERATORS: Builtin[] = [
|
|
|
179
185
|
}),
|
|
180
186
|
];
|
|
181
187
|
|
|
182
|
-
const
|
|
188
|
+
const NEG_SIGNATURE = getArithmeticSignature("Integers", "Neg", "IntValue", 1);
|
|
183
189
|
|
|
184
|
-
|
|
190
|
+
// TLC overrides the operators of Naturals with the Java class of Integers when a spec loads
|
|
191
|
+
// Integers, which names it in their errors.
|
|
192
|
+
const getArithmeticOperators = (javaClass: "Naturals" | "Integers") => {
|
|
193
|
+
const dotDotSignature = getArithmeticSignature(javaClass, "DotDot", "IntervalValue", 2);
|
|
194
|
+
return [
|
|
195
|
+
defineArithmetic(javaClass, "+", "Plus", (left, right) => left + right),
|
|
196
|
+
defineArithmetic(javaClass, "-", "Minus", (left, right) => left - right),
|
|
197
|
+
defineArithmetic(javaClass, "*", "Times", (left, right) => left * right),
|
|
198
|
+
defineArithmetic(javaClass, "^", "Expt", (base, exponent) => {
|
|
199
|
+
if (exponent < 0) {
|
|
200
|
+
throw new TlaModuleError(
|
|
201
|
+
`The second argument of ^ should be a natural number, but instead it is:\n${exponent}`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
if (base === 0 && exponent === 0) throw new TlaModuleError("0^0 is undefined.");
|
|
205
|
+
return base ** exponent;
|
|
206
|
+
}),
|
|
207
|
+
defineArithmetic(javaClass, "\\div", "Divide", (left, right) => {
|
|
208
|
+
if (right === 0) throw new TlaModuleError("The second argument of \\div is 0.");
|
|
209
|
+
const quotient = Math.floor(left / right);
|
|
210
|
+
if (isOutOfRange(quotient)) throw createOverflowError(`${left} \\div ${right}`);
|
|
211
|
+
return quotient;
|
|
212
|
+
}),
|
|
213
|
+
defineArithmetic(javaClass, "%", "Mod", (left, right) => {
|
|
214
|
+
if (right <= 0) {
|
|
215
|
+
throw new TlaModuleError(
|
|
216
|
+
`The second argument of % should be a positive number, but instead it is:\n${right}`,
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
return ((left % right) + right) % right;
|
|
220
|
+
}),
|
|
221
|
+
defineOperator("..", 2, (args) => ({
|
|
222
|
+
kind: "Interval",
|
|
223
|
+
low: getJavaInteger(getValue(args, 0, ".."), dotDotSignature),
|
|
224
|
+
high: getJavaInteger(getValue(args, 1, ".."), dotDotSignature),
|
|
225
|
+
})),
|
|
226
|
+
];
|
|
227
|
+
};
|
|
185
228
|
|
|
186
229
|
const NATURALS_OPERATORS = [
|
|
187
230
|
defineConstant("Nat", { kind: "InfiniteSet", name: "Nat" }),
|
|
188
|
-
|
|
189
|
-
defineArithmetic("-", "Minus", (left, right) => left - right),
|
|
190
|
-
defineArithmetic("*", "Times", (left, right) => left * right),
|
|
191
|
-
defineArithmetic("^", "Expt", (base, exponent) => {
|
|
192
|
-
if (exponent < 0) {
|
|
193
|
-
throw new TlaModuleError(
|
|
194
|
-
`The second argument of ^ should be a natural number, but instead it is:\n${exponent}`,
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
if (base === 0 && exponent === 0) throw new TlaModuleError("0^0 is undefined.");
|
|
198
|
-
return base ** exponent;
|
|
199
|
-
}),
|
|
200
|
-
defineArithmetic("\\div", "Divide", (left, right) => {
|
|
201
|
-
if (right === 0) throw new TlaModuleError("The second argument of \\div is 0.");
|
|
202
|
-
const quotient = Math.floor(left / right);
|
|
203
|
-
if (isOutOfRange(quotient)) throw createOverflowError(`${left} \\div ${right}`);
|
|
204
|
-
return quotient;
|
|
205
|
-
}),
|
|
206
|
-
defineArithmetic("%", "Mod", (left, right) => {
|
|
207
|
-
if (right <= 0) {
|
|
208
|
-
throw new TlaModuleError(
|
|
209
|
-
`The second argument of % should be a positive number, but instead it is:\n${right}`,
|
|
210
|
-
);
|
|
211
|
-
}
|
|
212
|
-
return ((left % right) + right) % right;
|
|
213
|
-
}),
|
|
231
|
+
...getArithmeticOperators("Naturals"),
|
|
214
232
|
defineComparison("<", (left, right) => left < right),
|
|
215
233
|
defineComparison(">", (left, right) => left > right),
|
|
216
234
|
defineComparison("<=", (left, right) => left <= right),
|
|
217
235
|
defineComparison(">=", (left, right) => left >= right),
|
|
218
|
-
defineOperator("..", 2, (args) => ({
|
|
219
|
-
kind: "Interval",
|
|
220
|
-
low: getJavaInteger(getValue(args, 0, ".."), DOT_DOT_SIGNATURE),
|
|
221
|
-
high: getJavaInteger(getValue(args, 1, ".."), DOT_DOT_SIGNATURE),
|
|
222
|
-
})),
|
|
223
236
|
];
|
|
224
237
|
|
|
225
238
|
const INTEGERS_OPERATORS = [
|
|
226
239
|
defineConstant("Int", { kind: "InfiniteSet", name: "Int" }),
|
|
240
|
+
...getArithmeticOperators("Integers"),
|
|
227
241
|
defineOperator("-.", 1, (args) => {
|
|
228
242
|
const value = getJavaInteger(getValue(args, 0, "-"), NEG_SIGNATURE);
|
|
229
243
|
if (isOutOfRange(-value)) throw createOverflowError(`-${value}`);
|
package/src/successors.ts
CHANGED
|
@@ -7,7 +7,12 @@ import {
|
|
|
7
7
|
type QuantifierExpression,
|
|
8
8
|
} from "./ast";
|
|
9
9
|
import { getReadVariables } from "./dependencies";
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
TlaEvaluationError,
|
|
12
|
+
TlaIllegalStateError,
|
|
13
|
+
TlaIncompleteStateError,
|
|
14
|
+
TlaUnassignedError,
|
|
15
|
+
} from "./errors";
|
|
11
16
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
12
17
|
import {
|
|
13
18
|
describeLocation,
|
|
@@ -53,6 +58,7 @@ export interface Successor {
|
|
|
53
58
|
interface Run {
|
|
54
59
|
environment: Environment;
|
|
55
60
|
isInit: boolean;
|
|
61
|
+
isEnabled: boolean;
|
|
56
62
|
current: Value[] | undefined;
|
|
57
63
|
isStopped: boolean;
|
|
58
64
|
}
|
|
@@ -113,11 +119,30 @@ const holds = (expression: Expression, context: Context, value = evaluate(expres
|
|
|
113
119
|
const isAssignmentOperator = (operator: string) =>
|
|
114
120
|
operator === "=" || operator === "\\in" || operator === "\\subseteq";
|
|
115
121
|
|
|
122
|
+
const tracedRunners = new WeakMap<Environment, WeakMap<Expression, Runner>[]>();
|
|
123
|
+
|
|
124
|
+
const getRunners = (environment: Environment, isInit: boolean) => {
|
|
125
|
+
if (!environment.trace) return isInit ? initRunners : nextRunners;
|
|
126
|
+
const runners = tracedRunners.get(environment) ?? [new WeakMap(), new WeakMap()];
|
|
127
|
+
tracedRunners.set(environment, runners);
|
|
128
|
+
return runners[isInit ? 0 : 1];
|
|
129
|
+
};
|
|
130
|
+
|
|
116
131
|
const getRunner = (expression: Expression, environment: Environment, isInit: boolean): Runner => {
|
|
117
|
-
const runners = isInit
|
|
132
|
+
const runners = getRunners(environment, isInit);
|
|
118
133
|
const cached = runners.get(expression);
|
|
119
134
|
if (cached) return cached;
|
|
120
|
-
const
|
|
135
|
+
const compiled = compileAction(expression, environment, isInit);
|
|
136
|
+
const trace = environment.trace;
|
|
137
|
+
// TLC's CallStackTool keeps an action's expression on the stack while the rest of the action runs.
|
|
138
|
+
const runner: Runner = trace
|
|
139
|
+
? (run, scope, partial, action, isFixed, onComplete) => {
|
|
140
|
+
const depth = trace.length;
|
|
141
|
+
trace.push(expression);
|
|
142
|
+
compiled(run, scope, partial, action, isFixed, onComplete);
|
|
143
|
+
trace.length = depth;
|
|
144
|
+
}
|
|
145
|
+
: compiled;
|
|
121
146
|
runners.set(expression, runner);
|
|
122
147
|
return runner;
|
|
123
148
|
};
|
|
@@ -201,6 +226,23 @@ const getUnchangedIndices = (
|
|
|
201
226
|
return getUnchangedIndices(definition.body, environment, visited);
|
|
202
227
|
};
|
|
203
228
|
|
|
229
|
+
// TLC's CallStackTool keeps each expression that UNCHANGED takes apart on the stack.
|
|
230
|
+
const getUnchangedFrames = (expression: Expression, environment: Environment): Expression[] => {
|
|
231
|
+
if (expression.kind === "Tuple") {
|
|
232
|
+
return [
|
|
233
|
+
expression,
|
|
234
|
+
...expression.elements.flatMap((element) => getUnchangedFrames(element, environment)),
|
|
235
|
+
];
|
|
236
|
+
}
|
|
237
|
+
const definition =
|
|
238
|
+
expression.kind === "Identifier" && !environment.variableIndex.has(expression.name)
|
|
239
|
+
? getZeroArityBody(expression.name, environment)
|
|
240
|
+
: undefined;
|
|
241
|
+
return definition
|
|
242
|
+
? [expression, ...getUnchangedFrames(definition.body, environment)]
|
|
243
|
+
: [expression];
|
|
244
|
+
};
|
|
245
|
+
|
|
204
246
|
const ACTION_KINDS = new Set([
|
|
205
247
|
"Label",
|
|
206
248
|
"And",
|
|
@@ -333,6 +375,7 @@ const compileConjunction = (
|
|
|
333
375
|
): Runner => {
|
|
334
376
|
const conjuncts = operands.map((operand) => toConjunct(operand, environment, isInit));
|
|
335
377
|
const evaluators: (Evaluator | undefined)[] = conjuncts.map(() => undefined);
|
|
378
|
+
const trace = environment.trace;
|
|
336
379
|
const runFrom = (
|
|
337
380
|
start: number,
|
|
338
381
|
run: Run,
|
|
@@ -353,6 +396,12 @@ const compileConjunction = (
|
|
|
353
396
|
return;
|
|
354
397
|
}
|
|
355
398
|
const conjunct = conjuncts[index];
|
|
399
|
+
if (trace) {
|
|
400
|
+
trace.push(conjunct.expression);
|
|
401
|
+
if (conjunct.expression.kind === "Unchanged") {
|
|
402
|
+
trace.push(...getUnchangedFrames(conjunct.expression.expression, environment));
|
|
403
|
+
}
|
|
404
|
+
}
|
|
356
405
|
if (conjunct.kind === "Guard") {
|
|
357
406
|
context ??= getContext(run, scope, current);
|
|
358
407
|
const evaluator = (evaluators[index] ??= getEntryEvaluator(
|
|
@@ -771,7 +820,8 @@ const compileAction = (
|
|
|
771
820
|
}
|
|
772
821
|
};
|
|
773
822
|
}
|
|
774
|
-
const hoisted =
|
|
823
|
+
const hoisted =
|
|
824
|
+
isInit || environment.trace ? undefined : getHoistedGuards(expression, environment);
|
|
775
825
|
if (hoisted) {
|
|
776
826
|
const rest = compileConjunction(hoisted.rest, environment, isInit);
|
|
777
827
|
return (run, scope, partial, action, isFixed, onComplete) => {
|
|
@@ -906,8 +956,17 @@ const compileAction = (
|
|
|
906
956
|
const unchanged = getUnchangedWrapper(expression.subscript);
|
|
907
957
|
return (run, scope, partial, action, isFixed, onComplete) =>
|
|
908
958
|
body(run, scope, partial, action, isFixed, (nextPartial, nextAction) => {
|
|
909
|
-
|
|
910
|
-
|
|
959
|
+
// ENABLED evaluates the subscript on the successor that the action specifies. TLC names
|
|
960
|
+
// a variable that the action leaves without a value.
|
|
961
|
+
try {
|
|
962
|
+
if (!holds(unchanged, getContext(run, scope, nextPartial)))
|
|
963
|
+
onComplete(nextPartial, nextAction);
|
|
964
|
+
} catch (error) {
|
|
965
|
+
if (!run.isEnabled || !(error instanceof TlaUnassignedError)) throw error;
|
|
966
|
+
throw new TlaEvaluationError(
|
|
967
|
+
`The action formula A appearing in a WF_v(A) or SF_v(A) operator does not specify the primed value of the variable ${error.variable} occurring in the state formula v.\n`,
|
|
968
|
+
);
|
|
969
|
+
}
|
|
911
970
|
});
|
|
912
971
|
}
|
|
913
972
|
case "BoxAction":
|
|
@@ -984,7 +1043,13 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
|
|
|
984
1043
|
);
|
|
985
1044
|
}
|
|
986
1045
|
const environment = context.environment;
|
|
987
|
-
const run: Run = {
|
|
1046
|
+
const run: Run = {
|
|
1047
|
+
environment,
|
|
1048
|
+
isInit: false,
|
|
1049
|
+
isEnabled: true,
|
|
1050
|
+
current: state,
|
|
1051
|
+
isStopped: false,
|
|
1052
|
+
};
|
|
988
1053
|
// TLC stops computing ENABLED at the first successor, so it skips the rest of the action.
|
|
989
1054
|
getRunner(expression, environment, false)(
|
|
990
1055
|
run,
|
|
@@ -1005,7 +1070,13 @@ export const forEachInitialState = (
|
|
|
1005
1070
|
onState: (state: Value[]) => boolean,
|
|
1006
1071
|
scope?: Scope,
|
|
1007
1072
|
) => {
|
|
1008
|
-
const run: Run = {
|
|
1073
|
+
const run: Run = {
|
|
1074
|
+
environment,
|
|
1075
|
+
isInit: true,
|
|
1076
|
+
isEnabled: false,
|
|
1077
|
+
current: undefined,
|
|
1078
|
+
isStopped: false,
|
|
1079
|
+
};
|
|
1009
1080
|
getRunner(expression, environment, true)(
|
|
1010
1081
|
run,
|
|
1011
1082
|
scope,
|
|
@@ -1015,14 +1086,7 @@ export const forEachInitialState = (
|
|
|
1015
1086
|
(partial) => {
|
|
1016
1087
|
if (run.isStopped) return;
|
|
1017
1088
|
if (!isComplete(partial)) {
|
|
1018
|
-
|
|
1019
|
-
// an evaluation failure.
|
|
1020
|
-
throw setExitCode(
|
|
1021
|
-
new TlaEvaluationError(
|
|
1022
|
-
`The initial predicate does not assign ${getMissingVariables(partial, environment).join(", ")}`,
|
|
1023
|
-
),
|
|
1024
|
-
EXIT_STATUS.error,
|
|
1025
|
-
);
|
|
1089
|
+
throw setExitCode(new TlaIllegalStateError(partial), EXIT_STATUS.error);
|
|
1026
1090
|
}
|
|
1027
1091
|
run.isStopped = onState(partial);
|
|
1028
1092
|
},
|
|
@@ -1054,7 +1118,13 @@ export const forEachSuccessor = (
|
|
|
1054
1118
|
onSuccessor: (successor: Successor) => boolean,
|
|
1055
1119
|
scope?: Scope,
|
|
1056
1120
|
) => {
|
|
1057
|
-
const run: Run = {
|
|
1121
|
+
const run: Run = {
|
|
1122
|
+
environment,
|
|
1123
|
+
isInit: false,
|
|
1124
|
+
isEnabled: false,
|
|
1125
|
+
current: state,
|
|
1126
|
+
isStopped: false,
|
|
1127
|
+
};
|
|
1058
1128
|
const counter = { successors: 0 };
|
|
1059
1129
|
getRunner(expression, environment, false)(
|
|
1060
1130
|
run,
|