tla-ts 0.0.3-dev.2032e26 → 0.0.3-dev.252cf06
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 +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/run-cli.cjs +3 -2
- package/dist/run-cli.js +3 -2
- package/dist/transfer.cjs +14 -14
- package/dist/transfer.js +14 -14
- package/package.json +1 -1
- package/src/ast.ts +90 -0
- package/src/check.ts +70 -28
- package/src/codegen.ts +1 -3
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +10 -10
- package/src/evaluate.ts +51 -112
- package/src/exit-codes.ts +20 -30
- package/src/instances.ts +12 -11
- package/src/levels.ts +8 -8
- package/src/live-check.ts +710 -0
- package/src/liveness.ts +5 -523
- package/src/resolve.ts +16 -15
- package/src/run-cli.ts +9 -2
- package/src/runtime.ts +1 -1
- package/src/spec-description.ts +7 -2
- package/src/subexpressions.ts +10 -10
- package/src/successors.ts +12 -7
- package/src/tlc-ext.ts +2 -10
package/package.json
CHANGED
package/src/ast.ts
CHANGED
|
@@ -486,3 +486,93 @@ export interface Module {
|
|
|
486
486
|
uses: UseOrHide[];
|
|
487
487
|
units: ModuleUnit[];
|
|
488
488
|
}
|
|
489
|
+
|
|
490
|
+
export const getChildren = (expression: Expression): Expression[] => {
|
|
491
|
+
switch (expression.kind) {
|
|
492
|
+
case "Number":
|
|
493
|
+
case "Decimal":
|
|
494
|
+
case "String":
|
|
495
|
+
case "Identifier":
|
|
496
|
+
case "OperatorReference":
|
|
497
|
+
case "ExceptAt":
|
|
498
|
+
return [];
|
|
499
|
+
case "Apply":
|
|
500
|
+
return expression.args;
|
|
501
|
+
case "Lambda":
|
|
502
|
+
return [expression.body];
|
|
503
|
+
case "And":
|
|
504
|
+
case "Or":
|
|
505
|
+
return expression.operands;
|
|
506
|
+
case "Prime":
|
|
507
|
+
case "Unchanged":
|
|
508
|
+
case "Enabled":
|
|
509
|
+
case "Always":
|
|
510
|
+
case "Eventually":
|
|
511
|
+
return [expression.expression];
|
|
512
|
+
case "If":
|
|
513
|
+
return [expression.condition, expression.consequent, expression.alternative];
|
|
514
|
+
case "Case":
|
|
515
|
+
return [
|
|
516
|
+
...expression.arms.flatMap((arm) => [arm.condition, arm.body]),
|
|
517
|
+
...(expression.other ? [expression.other] : []),
|
|
518
|
+
];
|
|
519
|
+
case "Let":
|
|
520
|
+
return [
|
|
521
|
+
...expression.definitions.flatMap((definition) =>
|
|
522
|
+
definition.kind === "Function"
|
|
523
|
+
? [...definition.bounds.map((bound) => bound.set), definition.body]
|
|
524
|
+
: [definition.body],
|
|
525
|
+
),
|
|
526
|
+
expression.body,
|
|
527
|
+
];
|
|
528
|
+
case "Forall":
|
|
529
|
+
case "Exists":
|
|
530
|
+
return [...expression.bounds.map((bound) => bound.set), expression.body];
|
|
531
|
+
case "UnboundedQuantifier":
|
|
532
|
+
return [expression.body];
|
|
533
|
+
case "Choose":
|
|
534
|
+
return [...(expression.set ? [expression.set] : []), expression.body];
|
|
535
|
+
case "SetEnumeration":
|
|
536
|
+
case "Tuple":
|
|
537
|
+
return expression.elements;
|
|
538
|
+
case "SetFilter":
|
|
539
|
+
return [expression.bound.set, expression.predicate];
|
|
540
|
+
case "SetMap":
|
|
541
|
+
return [expression.expression, ...expression.bounds.map((bound) => bound.set)];
|
|
542
|
+
case "FunctionConstruction":
|
|
543
|
+
return [...expression.bounds.map((bound) => bound.set), expression.body];
|
|
544
|
+
case "FunctionSet":
|
|
545
|
+
return [expression.domain, expression.range];
|
|
546
|
+
case "Record":
|
|
547
|
+
case "RecordSet":
|
|
548
|
+
return expression.fields.map((field) => field.value);
|
|
549
|
+
case "Except":
|
|
550
|
+
return [
|
|
551
|
+
expression.function,
|
|
552
|
+
...expression.updates.flatMap((update) => [
|
|
553
|
+
...update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])),
|
|
554
|
+
update.value,
|
|
555
|
+
]),
|
|
556
|
+
];
|
|
557
|
+
case "FunctionApplication":
|
|
558
|
+
return [expression.function, ...expression.args];
|
|
559
|
+
case "FieldAccess":
|
|
560
|
+
return [expression.record];
|
|
561
|
+
case "Product":
|
|
562
|
+
return expression.factors;
|
|
563
|
+
case "BoxAction":
|
|
564
|
+
case "AngleAction":
|
|
565
|
+
return [expression.action, expression.subscript];
|
|
566
|
+
case "WeakFairness":
|
|
567
|
+
case "StrongFairness":
|
|
568
|
+
return [expression.subscript, expression.action];
|
|
569
|
+
case "Label":
|
|
570
|
+
return [expression.body];
|
|
571
|
+
case "Subexpression":
|
|
572
|
+
return expression.selectors.flatMap((selector) => ("args" in selector ? selector.args : []));
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
export const isLocallyResolved = (expression: Expression) =>
|
|
577
|
+
(expression.kind === "Identifier" || expression.kind === "Apply") &&
|
|
578
|
+
expression.resolution?.kind === "Local";
|
package/src/check.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Assumption,
|
|
5
|
+
type Definition,
|
|
6
|
+
type Expression,
|
|
7
|
+
type SourceLocation,
|
|
8
|
+
} from "./ast";
|
|
2
9
|
import { parseConfig, type ConfigValue, type ModelConfig } from "./config";
|
|
3
10
|
import { getReadVariables } from "./dependencies";
|
|
4
11
|
import { createEnvironment } from "./environment";
|
|
@@ -15,24 +22,20 @@ import {
|
|
|
15
22
|
evaluate,
|
|
16
23
|
expectBoolean,
|
|
17
24
|
formatRange,
|
|
18
|
-
getChildren,
|
|
19
25
|
getDefinition,
|
|
20
26
|
isConstantLevel,
|
|
21
27
|
isImpure,
|
|
22
|
-
isLocallyResolved,
|
|
23
28
|
type Context,
|
|
24
29
|
type Environment,
|
|
25
30
|
type Scope,
|
|
26
31
|
} from "./evaluate";
|
|
27
32
|
import {
|
|
28
33
|
createLivenessChecker,
|
|
29
|
-
expandTemporal,
|
|
30
|
-
getLevel,
|
|
31
34
|
type Lasso,
|
|
35
|
+
type LivenessChecker,
|
|
32
36
|
type LiveSuccessor,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
} from "./liveness";
|
|
37
|
+
} from "./live-check";
|
|
38
|
+
import { expandTemporal, getLevel, type ScopedExpression, type TemporalProperty } from "./liveness";
|
|
36
39
|
import { ACTION_LEVEL, createLevelAnalyzer, TEMPORAL_LEVEL } from "./levels";
|
|
37
40
|
import { prettyPrint } from "./pretty-print";
|
|
38
41
|
import {
|
|
@@ -101,6 +104,8 @@ export interface TraceStep {
|
|
|
101
104
|
|
|
102
105
|
export interface Violation {
|
|
103
106
|
name: string;
|
|
107
|
+
// Every temporal property that a liveness counterexample violates, in the configuration's order.
|
|
108
|
+
properties?: string[];
|
|
104
109
|
trace: TraceStep[];
|
|
105
110
|
loopStart: number | undefined;
|
|
106
111
|
loopLabel?: string;
|
|
@@ -967,8 +972,36 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
967
972
|
const getFingerprintState = search.getFingerprintState;
|
|
968
973
|
const nextActions = { value: undefined as SpecAction[] | undefined };
|
|
969
974
|
const isReducing = config.view !== undefined || config.symmetry !== undefined;
|
|
975
|
+
const tlcFingerprints: number[] = [];
|
|
970
976
|
const liveness = withLivenessExitCode(() =>
|
|
971
|
-
createLivenessChecker(search.properties.liveness, spec.temporals, environment
|
|
977
|
+
createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
|
|
978
|
+
getFingerprint: (index) =>
|
|
979
|
+
(tlcFingerprints[index] ??= Number(
|
|
980
|
+
BigInt.asIntN(32, runtime.getFingerprint(states[index])),
|
|
981
|
+
)),
|
|
982
|
+
getSuccessors: (state) => {
|
|
983
|
+
const successors: LiveSuccessor[] = [];
|
|
984
|
+
if (spec.next === undefined) return successors;
|
|
985
|
+
forEachSuccessor(
|
|
986
|
+
spec.next,
|
|
987
|
+
state,
|
|
988
|
+
environment,
|
|
989
|
+
(successor) => {
|
|
990
|
+
canonicalizeState(successor.state);
|
|
991
|
+
const isInModel =
|
|
992
|
+
search.satisfiesConstraints(successor.state, state) &&
|
|
993
|
+
search.satisfiesActionConstraints(state, successor.state);
|
|
994
|
+
if (!isInModel) return false;
|
|
995
|
+
const fingerprintState = getFingerprintState(successor.state);
|
|
996
|
+
const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
|
|
997
|
+
successors.push({ index, state: successor.state });
|
|
998
|
+
return false;
|
|
999
|
+
},
|
|
1000
|
+
spec.scope,
|
|
1001
|
+
);
|
|
1002
|
+
return successors;
|
|
1003
|
+
},
|
|
1004
|
+
}),
|
|
972
1005
|
);
|
|
973
1006
|
const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
|
|
974
1007
|
|
|
@@ -1109,7 +1142,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1109
1142
|
outDegrees: failureOutDegrees,
|
|
1110
1143
|
};
|
|
1111
1144
|
};
|
|
1112
|
-
const
|
|
1145
|
+
const getPathSteps = (path: number[], loopStart?: number) => {
|
|
1113
1146
|
const steps = getPathStates(path, NEXT_RECOVERY_FAILURE);
|
|
1114
1147
|
const last = steps[steps.length - 1].state;
|
|
1115
1148
|
const isClosed =
|
|
@@ -1118,7 +1151,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1118
1151
|
loopStart === path.length - 1 ||
|
|
1119
1152
|
findSuccessorWithFingerprint(last, path[loopStart]) !== undefined;
|
|
1120
1153
|
if (!isClosed) throw new TlaEvaluationError(NEXT_RECOVERY_FAILURE);
|
|
1121
|
-
return
|
|
1154
|
+
return steps;
|
|
1122
1155
|
};
|
|
1123
1156
|
const getStateHash = (state: Value[]) => {
|
|
1124
1157
|
let hash = 0x811c9dc5;
|
|
@@ -1184,7 +1217,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1184
1217
|
};
|
|
1185
1218
|
runtime.getCurrentState = () => (current.index === -1 ? undefined : states[current.index]);
|
|
1186
1219
|
runtime.setLivenessSource = (index) => {
|
|
1187
|
-
current.livenessSource = index;
|
|
1220
|
+
current.livenessSource = index ?? -1;
|
|
1188
1221
|
};
|
|
1189
1222
|
runtime.getLevel = (state) => {
|
|
1190
1223
|
const source = current.livenessSource;
|
|
@@ -1340,32 +1373,43 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1340
1373
|
}
|
|
1341
1374
|
try {
|
|
1342
1375
|
const accepted = liveness?.addState(index, state, liveSuccessors);
|
|
1343
|
-
|
|
1344
|
-
|
|
1376
|
+
if (!liveness || !accepted) return undefined;
|
|
1377
|
+
const steps = getPathSteps(accepted);
|
|
1378
|
+
const properties = liveness.getViolatedProperties(
|
|
1379
|
+
steps.map((step) => step.state),
|
|
1380
|
+
steps.length - 1,
|
|
1381
|
+
);
|
|
1345
1382
|
return reportViolation("property-violation", {
|
|
1346
|
-
name:
|
|
1347
|
-
|
|
1383
|
+
name: properties[0],
|
|
1384
|
+
properties,
|
|
1385
|
+
trace: toTrace(steps, accepted),
|
|
1348
1386
|
});
|
|
1349
1387
|
} catch (error) {
|
|
1350
1388
|
return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
|
|
1351
1389
|
}
|
|
1352
1390
|
};
|
|
1353
|
-
|
|
1391
|
+
// TLC names the properties and the step back into the cycle from the states it prints.
|
|
1392
|
+
const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
|
|
1354
1393
|
const path = [...lasso.prefix, ...lasso.cycle];
|
|
1355
|
-
const
|
|
1356
|
-
const
|
|
1394
|
+
const loopStart = lasso.prefix.length;
|
|
1395
|
+
const steps = getPathSteps(path, loopStart);
|
|
1396
|
+
const trace = toTrace(steps, path, loopStart);
|
|
1397
|
+
const properties = checker.getViolatedProperties(
|
|
1398
|
+
steps.map((step) => step.state),
|
|
1399
|
+
loopStart,
|
|
1400
|
+
);
|
|
1401
|
+
const back = path[loopStart];
|
|
1357
1402
|
const last = path[path.length - 1];
|
|
1403
|
+
const lastState = steps[steps.length - 1].state;
|
|
1358
1404
|
const loopLabel =
|
|
1359
|
-
back === last ? undefined : withoutOutput(() => getLabel(last,
|
|
1405
|
+
back === last ? undefined : withoutOutput(() => getLabel(last, lastState, states[back]));
|
|
1360
1406
|
return reportViolation(
|
|
1361
1407
|
"liveness-violation",
|
|
1362
|
-
{ name:
|
|
1408
|
+
{ name: properties[0], properties, trace, loopStart, loopLabel },
|
|
1363
1409
|
() =>
|
|
1364
1410
|
back === last
|
|
1365
1411
|
? trace[trace.length - 1].action
|
|
1366
|
-
:
|
|
1367
|
-
isSameState(successor.state, states[back]),
|
|
1368
|
-
)?.action,
|
|
1412
|
+
: findSuccessorWithFingerprint(lastState, back)?.action,
|
|
1369
1413
|
);
|
|
1370
1414
|
};
|
|
1371
1415
|
const isPeriodic = liveness !== undefined && options.livenessCheck !== "final";
|
|
@@ -1378,7 +1422,6 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1378
1422
|
if (!isGrown || periodic.time > (now - startedAt) * LIVENESS_RATIO) return undefined;
|
|
1379
1423
|
periodic.size = expanded;
|
|
1380
1424
|
const lasso = liveness?.findViolation();
|
|
1381
|
-
current.livenessSource = -1;
|
|
1382
1425
|
periodic.time += performance.now() - now;
|
|
1383
1426
|
return lasso;
|
|
1384
1427
|
};
|
|
@@ -1398,15 +1441,14 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
|
|
|
1398
1441
|
expansion.newStates = 0;
|
|
1399
1442
|
if (isPeriodic && performance.now() >= periodic.at) {
|
|
1400
1443
|
const lasso = findPeriodicViolation(index + 1);
|
|
1401
|
-
if (lasso) return reportLasso(lasso);
|
|
1444
|
+
if (liveness && lasso) return reportLasso(liveness, lasso);
|
|
1402
1445
|
}
|
|
1403
1446
|
}
|
|
1404
1447
|
|
|
1405
1448
|
current.index = -1;
|
|
1406
1449
|
current.candidate = undefined;
|
|
1407
1450
|
const lasso = liveness?.findViolation();
|
|
1408
|
-
|
|
1409
|
-
if (lasso) return reportLasso(lasso);
|
|
1451
|
+
if (liveness && lasso) return reportLasso(liveness, lasso);
|
|
1410
1452
|
|
|
1411
1453
|
const postconditionViolation = search.findPostconditionViolation();
|
|
1412
1454
|
if (postconditionViolation) {
|
package/src/codegen.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { getChildren, isLocallyResolved, type Expression } from "./ast";
|
|
2
2
|
import { TlaEvaluationError } from "./errors";
|
|
3
3
|
import {
|
|
4
4
|
describeLocation,
|
|
5
5
|
bindValue,
|
|
6
6
|
expectBoolean,
|
|
7
7
|
findGlobalName,
|
|
8
|
-
getChildren,
|
|
9
8
|
getEnumeratedElements,
|
|
10
9
|
getConditionError,
|
|
11
10
|
getConjunctError,
|
|
@@ -14,7 +13,6 @@ import {
|
|
|
14
13
|
getEvaluator,
|
|
15
14
|
getFieldError,
|
|
16
15
|
isClosedConstant,
|
|
17
|
-
isLocallyResolved,
|
|
18
16
|
isTlcInteger,
|
|
19
17
|
isValueArgument,
|
|
20
18
|
isWorthCaching,
|
package/src/dependencies.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {
|
|
1
|
+
import { getChildren, type Definition, type Expression } from "./ast";
|
|
2
|
+
import { isImpureBuiltin, type Environment } from "./evaluate";
|
|
3
3
|
|
|
4
4
|
const STATE_INDEPENDENT_KINDS = new Set([
|
|
5
5
|
"Prime",
|
package/src/environment.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
type Assumption,
|
|
4
|
+
type Declaration,
|
|
5
|
+
type Definition,
|
|
6
|
+
type Expression,
|
|
7
|
+
type Instance,
|
|
8
|
+
type Module,
|
|
9
|
+
type OperatorParam,
|
|
10
|
+
type SourceLocation,
|
|
10
11
|
} from "./ast";
|
|
11
12
|
import { BUNDLED_MODULES } from "./bundled-modules";
|
|
12
13
|
import type { ConfigValue, ModelConfig } from "./config";
|
|
@@ -16,7 +17,6 @@ import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
|
16
17
|
import {
|
|
17
18
|
createBigNumberError,
|
|
18
19
|
createRealNumberError,
|
|
19
|
-
getChildren,
|
|
20
20
|
isConstantLevel,
|
|
21
21
|
isTlcInteger,
|
|
22
22
|
type Builtin,
|
package/src/evaluate.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Bound,
|
|
5
|
+
type BoundPattern,
|
|
6
|
+
type DecimalExpression,
|
|
7
|
+
type Definition,
|
|
8
|
+
type Expression,
|
|
9
|
+
type FunctionDefinition,
|
|
10
|
+
type NumberExpression,
|
|
11
|
+
type OperatorParam,
|
|
12
|
+
type RecordField,
|
|
13
|
+
type SourceRange,
|
|
12
14
|
} from "./ast";
|
|
13
15
|
import { getKindString } from "./builtins";
|
|
14
16
|
import { TlaEvaluationError } from "./errors";
|
|
@@ -262,96 +264,6 @@ export const expectBoolean = (value: Value, description: string) => {
|
|
|
262
264
|
return value;
|
|
263
265
|
};
|
|
264
266
|
|
|
265
|
-
export const getChildren = (expression: Expression): Expression[] => {
|
|
266
|
-
switch (expression.kind) {
|
|
267
|
-
case "Number":
|
|
268
|
-
case "Decimal":
|
|
269
|
-
case "String":
|
|
270
|
-
case "Identifier":
|
|
271
|
-
case "OperatorReference":
|
|
272
|
-
case "ExceptAt":
|
|
273
|
-
return [];
|
|
274
|
-
case "Apply":
|
|
275
|
-
return expression.args;
|
|
276
|
-
case "Lambda":
|
|
277
|
-
return [expression.body];
|
|
278
|
-
case "And":
|
|
279
|
-
case "Or":
|
|
280
|
-
return expression.operands;
|
|
281
|
-
case "Prime":
|
|
282
|
-
case "Unchanged":
|
|
283
|
-
case "Enabled":
|
|
284
|
-
case "Always":
|
|
285
|
-
case "Eventually":
|
|
286
|
-
return [expression.expression];
|
|
287
|
-
case "If":
|
|
288
|
-
return [expression.condition, expression.consequent, expression.alternative];
|
|
289
|
-
case "Case":
|
|
290
|
-
return [
|
|
291
|
-
...expression.arms.flatMap((arm) => [arm.condition, arm.body]),
|
|
292
|
-
...(expression.other ? [expression.other] : []),
|
|
293
|
-
];
|
|
294
|
-
case "Let":
|
|
295
|
-
return [
|
|
296
|
-
...expression.definitions.flatMap((definition) =>
|
|
297
|
-
definition.kind === "Function"
|
|
298
|
-
? [...definition.bounds.map((bound) => bound.set), definition.body]
|
|
299
|
-
: [definition.body],
|
|
300
|
-
),
|
|
301
|
-
expression.body,
|
|
302
|
-
];
|
|
303
|
-
case "Forall":
|
|
304
|
-
case "Exists":
|
|
305
|
-
return [...expression.bounds.map((bound) => bound.set), expression.body];
|
|
306
|
-
case "UnboundedQuantifier":
|
|
307
|
-
return [expression.body];
|
|
308
|
-
case "Choose":
|
|
309
|
-
return [...(expression.set ? [expression.set] : []), expression.body];
|
|
310
|
-
case "SetEnumeration":
|
|
311
|
-
case "Tuple":
|
|
312
|
-
return expression.elements;
|
|
313
|
-
case "SetFilter":
|
|
314
|
-
return [expression.bound.set, expression.predicate];
|
|
315
|
-
case "SetMap":
|
|
316
|
-
return [expression.expression, ...expression.bounds.map((bound) => bound.set)];
|
|
317
|
-
case "FunctionConstruction":
|
|
318
|
-
return [...expression.bounds.map((bound) => bound.set), expression.body];
|
|
319
|
-
case "FunctionSet":
|
|
320
|
-
return [expression.domain, expression.range];
|
|
321
|
-
case "Record":
|
|
322
|
-
case "RecordSet":
|
|
323
|
-
return expression.fields.map((field) => field.value);
|
|
324
|
-
case "Except":
|
|
325
|
-
return [
|
|
326
|
-
expression.function,
|
|
327
|
-
...expression.updates.flatMap((update) => [
|
|
328
|
-
...update.path.flatMap((step) => (step.kind === "Index" ? step.args : [])),
|
|
329
|
-
update.value,
|
|
330
|
-
]),
|
|
331
|
-
];
|
|
332
|
-
case "FunctionApplication":
|
|
333
|
-
return [expression.function, ...expression.args];
|
|
334
|
-
case "FieldAccess":
|
|
335
|
-
return [expression.record];
|
|
336
|
-
case "Product":
|
|
337
|
-
return expression.factors;
|
|
338
|
-
case "BoxAction":
|
|
339
|
-
case "AngleAction":
|
|
340
|
-
return [expression.action, expression.subscript];
|
|
341
|
-
case "WeakFairness":
|
|
342
|
-
case "StrongFairness":
|
|
343
|
-
return [expression.subscript, expression.action];
|
|
344
|
-
case "Label":
|
|
345
|
-
return [expression.body];
|
|
346
|
-
case "Subexpression":
|
|
347
|
-
return expression.selectors.flatMap((selector) => ("args" in selector ? selector.args : []));
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
|
|
351
|
-
export const isLocallyResolved = (expression: Expression) =>
|
|
352
|
-
(expression.kind === "Identifier" || expression.kind === "Apply") &&
|
|
353
|
-
expression.resolution?.kind === "Local";
|
|
354
|
-
|
|
355
267
|
export const getDefinition = (name: string, environment: Environment) =>
|
|
356
268
|
environment.definitions.get(environment.substitutions.get(name) ?? name);
|
|
357
269
|
|
|
@@ -889,14 +801,29 @@ const IMPURE_BUILTINS = new Set([
|
|
|
889
801
|
export const isImpureBuiltin = (name: string, environment: Environment) =>
|
|
890
802
|
IMPURE_BUILTINS.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
891
803
|
|
|
892
|
-
|
|
804
|
+
interface Impurity {
|
|
805
|
+
builtins: Set<string>;
|
|
806
|
+
definitions: WeakMap<Definition, boolean>;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const ANY_EFFECT: Impurity = { builtins: IMPURE_BUILTINS, definitions: new WeakMap() };
|
|
810
|
+
|
|
811
|
+
// An Assert that held holds again, so evaluating it twice looks the same as evaluating it once.
|
|
812
|
+
const REPEATED_EFFECT: Impurity = {
|
|
813
|
+
builtins: new Set([...IMPURE_BUILTINS].filter((name) => name !== "Assert")),
|
|
814
|
+
definitions: new WeakMap(),
|
|
815
|
+
};
|
|
893
816
|
|
|
894
817
|
const isEvaluatedOnce = (definition: Definition, environment: Environment) =>
|
|
895
818
|
definition.kind === "Operator" &&
|
|
896
819
|
definition.params.length === 0 &&
|
|
897
820
|
isConstantLevel(definition, environment);
|
|
898
821
|
|
|
899
|
-
const callsImpureBuiltin = (
|
|
822
|
+
const callsImpureBuiltin = (
|
|
823
|
+
expression: Expression,
|
|
824
|
+
environment: Environment,
|
|
825
|
+
impurity: Impurity,
|
|
826
|
+
): boolean => {
|
|
900
827
|
const name =
|
|
901
828
|
expression.kind === "Identifier"
|
|
902
829
|
? expression.name
|
|
@@ -905,29 +832,35 @@ const callsImpureBuiltin = (expression: Expression, environment: Environment): b
|
|
|
905
832
|
: undefined;
|
|
906
833
|
if (name !== undefined && !isLocallyResolved(expression)) {
|
|
907
834
|
const definition = getDefinition(name, environment);
|
|
908
|
-
|
|
835
|
+
const isImpureCall =
|
|
836
|
+
impurity.builtins.has(name) || environment.builtins.get(name)?.isStateLevel === true;
|
|
837
|
+
if (!definition && isImpureCall) return true;
|
|
909
838
|
if (
|
|
910
839
|
definition &&
|
|
911
840
|
!isEvaluatedOnce(definition, environment) &&
|
|
912
|
-
isImpureDefinition(definition, environment)
|
|
841
|
+
isImpureDefinition(definition, environment, impurity)
|
|
913
842
|
) {
|
|
914
843
|
return true;
|
|
915
844
|
}
|
|
916
845
|
}
|
|
917
|
-
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment));
|
|
846
|
+
return getChildren(expression).some((child) => callsImpureBuiltin(child, environment, impurity));
|
|
918
847
|
};
|
|
919
848
|
|
|
920
849
|
// TLC evaluates a constant definition without parameters once, so only other definitions pass
|
|
921
850
|
// on the side effects and random choices of the builtins they call.
|
|
922
|
-
const isImpureDefinition = (
|
|
923
|
-
|
|
851
|
+
const isImpureDefinition = (
|
|
852
|
+
definition: Definition,
|
|
853
|
+
environment: Environment,
|
|
854
|
+
impurity = ANY_EFFECT,
|
|
855
|
+
): boolean => {
|
|
856
|
+
const cached = impurity.definitions.get(definition);
|
|
924
857
|
if (cached !== undefined) return cached;
|
|
925
|
-
|
|
858
|
+
impurity.definitions.set(definition, false);
|
|
926
859
|
const isImpure =
|
|
927
|
-
callsImpureBuiltin(definition.body, environment) ||
|
|
860
|
+
callsImpureBuiltin(definition.body, environment, impurity) ||
|
|
928
861
|
(definition.kind === "Function" &&
|
|
929
|
-
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment)));
|
|
930
|
-
|
|
862
|
+
definition.bounds.some((bound) => callsImpureBuiltin(bound.set, environment, impurity)));
|
|
863
|
+
impurity.definitions.set(definition, isImpure);
|
|
931
864
|
return isImpure;
|
|
932
865
|
};
|
|
933
866
|
|
|
@@ -941,6 +874,12 @@ export const isImpure = (environment: Environment) =>
|
|
|
941
874
|
isImpureDefinition(definition, environment),
|
|
942
875
|
);
|
|
943
876
|
|
|
877
|
+
// Whether evaluating a definition again can show, such as by printing twice.
|
|
878
|
+
export const hasRepeatedEffects = (environment: Environment) =>
|
|
879
|
+
[...environment.definitions.values()].some((definition) =>
|
|
880
|
+
isImpureDefinition(definition, environment, REPEATED_EFFECT),
|
|
881
|
+
);
|
|
882
|
+
|
|
944
883
|
const closedConstants = new WeakMap<Expression, boolean>();
|
|
945
884
|
|
|
946
885
|
export const isClosedConstant = (expression: Expression, environment: Environment): boolean => {
|
package/src/exit-codes.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LivenessChecker } from "./
|
|
1
|
+
import type { LivenessChecker } from "./live-check";
|
|
2
2
|
|
|
3
3
|
// TLC's exit statuses, from EC.ExitStatus.
|
|
4
4
|
export const EXIT_STATUS = {
|
|
@@ -39,37 +39,27 @@ export const setModuleExitCode = (error: Error, code: number) => {
|
|
|
39
39
|
export const getExitCode = (error: Error) => exitCodes.get(error) ?? moduleExitCodes.get(error);
|
|
40
40
|
|
|
41
41
|
// TLC exits with 255 when it fails to process or evaluate a liveness property, in any phase.
|
|
42
|
-
|
|
43
|
-
create: () => LivenessChecker | undefined,
|
|
44
|
-
): LivenessChecker | undefined => {
|
|
42
|
+
const withErrorExitCode = <Result>(run: () => Result) => {
|
|
45
43
|
try {
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
checker && {
|
|
49
|
-
addInitialState: (index, state) => {
|
|
50
|
-
try {
|
|
51
|
-
checker.addInitialState(index, state);
|
|
52
|
-
} catch (error) {
|
|
53
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
54
|
-
}
|
|
55
|
-
},
|
|
56
|
-
addState: (index, state, successors) => {
|
|
57
|
-
try {
|
|
58
|
-
return checker.addState(index, state, successors);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
findViolation: () => {
|
|
64
|
-
try {
|
|
65
|
-
return checker.findViolation();
|
|
66
|
-
} catch (error) {
|
|
67
|
-
throw setExitCode(error, EXIT_STATUS.error);
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
}
|
|
71
|
-
);
|
|
44
|
+
return run();
|
|
72
45
|
} catch (error) {
|
|
73
46
|
throw setExitCode(error, EXIT_STATUS.error);
|
|
74
47
|
}
|
|
75
48
|
};
|
|
49
|
+
|
|
50
|
+
export const withLivenessExitCode = (
|
|
51
|
+
create: () => LivenessChecker | undefined,
|
|
52
|
+
): LivenessChecker | undefined => {
|
|
53
|
+
const checker = withErrorExitCode(create);
|
|
54
|
+
return (
|
|
55
|
+
checker && {
|
|
56
|
+
addInitialState: (index, state) =>
|
|
57
|
+
withErrorExitCode(() => checker.addInitialState(index, state)),
|
|
58
|
+
addState: (index, state, successors) =>
|
|
59
|
+
withErrorExitCode(() => checker.addState(index, state, successors)),
|
|
60
|
+
findViolation: () => withErrorExitCode(() => checker.findViolation()),
|
|
61
|
+
getViolatedProperties: (path, cycleStart) =>
|
|
62
|
+
withErrorExitCode(() => checker.getViolatedProperties(path, cycleStart)),
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
};
|
package/src/instances.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import {
|
|
2
|
+
getChildren,
|
|
3
|
+
isLocallyResolved,
|
|
4
|
+
type Bound,
|
|
5
|
+
type BoundPattern,
|
|
6
|
+
type Definition,
|
|
7
|
+
type Expression,
|
|
8
|
+
type Instance,
|
|
9
|
+
type Module,
|
|
10
|
+
type OperatorParam,
|
|
11
|
+
type SourceLocation,
|
|
12
|
+
type Theorem,
|
|
11
13
|
} from "./ast";
|
|
12
14
|
import { TlaSyntaxError } from "./errors";
|
|
13
|
-
import { getChildren, isLocallyResolved } from "./evaluate";
|
|
14
15
|
import { getArity, getBoundNames, getPatternNames, isBefore } from "./resolve";
|
|
15
16
|
|
|
16
17
|
export const substitutedExpressions = new WeakSet<Expression>();
|