tla-ts 0.0.3-dev.037ac3b → 0.0.3-dev.0e500e5
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.cjs +1 -1
- package/dist/index.d.cts +58 -22
- package/dist/index.d.ts +58 -22
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +14 -12
- package/dist/run-cli.js +22 -20
- package/dist/transfer.cjs +39 -21
- package/dist/transfer.js +39 -21
- package/package.json +1 -1
- package/src/ast.ts +26 -4
- package/src/builtins.ts +9 -3
- package/src/check.ts +309 -61
- package/src/codegen.ts +6 -3
- package/src/config.ts +224 -170
- package/src/environment.ts +223 -84
- package/src/errors.ts +100 -1
- package/src/evaluate.ts +172 -19
- package/src/exit-codes.ts +22 -3
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +1 -1
- package/src/levels.ts +176 -36
- package/src/live-check.ts +129 -49
- package/src/liveness.ts +68 -10
- package/src/parallel-worker.ts +2 -2
- package/src/parallel.ts +2 -2
- package/src/parser.ts +152 -48
- package/src/resolve.ts +878 -338
- package/src/run-cli.ts +61 -11
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +1 -2
- package/src/standard-modules.ts +52 -38
- package/src/standard-ranges.ts +116 -0
- package/src/subexpressions.ts +25 -13
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +111 -31
- 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/value-sources.ts +29 -0
- package/src/values.ts +184 -51
package/src/levels.ts
CHANGED
|
@@ -5,11 +5,19 @@ import {
|
|
|
5
5
|
type Expression,
|
|
6
6
|
type Module,
|
|
7
7
|
type OperatorParam,
|
|
8
|
+
type Proof,
|
|
9
|
+
type ProofStep,
|
|
8
10
|
type SourceLocation,
|
|
9
11
|
} from "./ast";
|
|
10
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
createSanyError,
|
|
14
|
+
createSemanticError,
|
|
15
|
+
formatRange,
|
|
16
|
+
TlaSyntaxError,
|
|
17
|
+
type SanyError,
|
|
18
|
+
} from "./errors";
|
|
11
19
|
import { mapChildren } from "./instances";
|
|
12
|
-
import { getBoundNames, getPatternNames } from "./resolve";
|
|
20
|
+
import { collectSemanticErrors, getBoundNames, getPatternNames } from "./resolve";
|
|
13
21
|
import { STANDARD_LEVELS } from "./standard-levels";
|
|
14
22
|
|
|
15
23
|
interface LevelData {
|
|
@@ -34,6 +42,7 @@ interface LevelContext {
|
|
|
34
42
|
constants: Set<string>;
|
|
35
43
|
variables: Set<string>;
|
|
36
44
|
getDefinition: (name: string) => Definition | undefined;
|
|
45
|
+
getQualifiedName?: (name: string) => string | undefined;
|
|
37
46
|
}
|
|
38
47
|
|
|
39
48
|
type ScopeEntry =
|
|
@@ -176,8 +185,13 @@ export const ACTION_LEVEL = 2;
|
|
|
176
185
|
|
|
177
186
|
export const TEMPORAL_LEVEL = 3;
|
|
178
187
|
|
|
179
|
-
|
|
180
|
-
|
|
188
|
+
// SANY reports a level error at the range of the expression it rejects.
|
|
189
|
+
const createLevelError = (message: string, expression: Expression) =>
|
|
190
|
+
expression.location.range
|
|
191
|
+
? createSemanticError(`${message}.`, expression.location.range)
|
|
192
|
+
: new TlaSyntaxError(
|
|
193
|
+
`${message} at line ${expression.location.line}, column ${expression.location.column}`,
|
|
194
|
+
);
|
|
181
195
|
|
|
182
196
|
const LOGICAL_OPERATORS = new Set(["/\\", "\\/", "=>", "<=>"]);
|
|
183
197
|
|
|
@@ -201,7 +215,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
201
215
|
if (context.variables.has(name)) return { kind: "Declared", name, level: 1, arity: 0 };
|
|
202
216
|
const definition = context.getDefinition(name);
|
|
203
217
|
if (definition) {
|
|
204
|
-
|
|
218
|
+
const levels = analyzeDefinition(definition, new Map());
|
|
219
|
+
// SANY names a definition of an instance by its qualified name, as in I!Foo.
|
|
220
|
+
const qualifiedName = context.getQualifiedName?.(name);
|
|
221
|
+
return {
|
|
222
|
+
kind: "Defined",
|
|
223
|
+
levels: qualifiedName ? { ...levels, name: qualifiedName } : levels,
|
|
224
|
+
};
|
|
205
225
|
}
|
|
206
226
|
const signature = CORE_SIGNATURES.get(name) ?? STANDARD_LEVELS.get(name);
|
|
207
227
|
if (signature) return { kind: "Defined", levels: signature };
|
|
@@ -237,13 +257,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
237
257
|
if (operand.operator || operand.declared) return;
|
|
238
258
|
if (operand.data.level > getMaxLevel(operator, index)) {
|
|
239
259
|
throw new TlaSyntaxError(
|
|
240
|
-
`Level error in applying operator ${operator.name}
|
|
260
|
+
`Level error in applying operator ${operator.name}:\nThe level of argument ${index + 1} exceeds the maximum level allowed by the operator`,
|
|
241
261
|
);
|
|
242
262
|
}
|
|
243
263
|
});
|
|
244
264
|
if (ranges.some((range) => range.level > ACTION_LEVEL)) {
|
|
245
265
|
throw new TlaSyntaxError(
|
|
246
|
-
`Level error in applying operator ${operator.name}
|
|
266
|
+
`Level error in applying operator ${operator.name}:\nThe level of the range for a bounded variable exceeds the maximum level allowed by the operator`,
|
|
247
267
|
);
|
|
248
268
|
}
|
|
249
269
|
const getOperand = (formal: string) => {
|
|
@@ -444,10 +464,13 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
444
464
|
? applyDeclared(resolved.name, resolved.level, operands)
|
|
445
465
|
: applyDefined(resolved.levels, operands);
|
|
446
466
|
|
|
447
|
-
const checkJunction = (operands: Operand[],
|
|
467
|
+
const checkJunction = (operands: Operand[], expression: Expression) => {
|
|
448
468
|
const levels = operands.map((operand) => operand.data.level);
|
|
449
469
|
if (levels.includes(ACTION_LEVEL) && levels.includes(TEMPORAL_LEVEL)) {
|
|
450
|
-
throw createLevelError(
|
|
470
|
+
throw createLevelError(
|
|
471
|
+
"Formula has both temporal formula and action as arguments",
|
|
472
|
+
expression,
|
|
473
|
+
);
|
|
451
474
|
}
|
|
452
475
|
};
|
|
453
476
|
|
|
@@ -456,10 +479,10 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
456
479
|
if (isLeadsTo && operands.some((operand) => operand.data.level === ACTION_LEVEL)) {
|
|
457
480
|
throw createLevelError(
|
|
458
481
|
"Action used where only temporal formula or state predicate allowed",
|
|
459
|
-
expression
|
|
482
|
+
expression,
|
|
460
483
|
);
|
|
461
484
|
}
|
|
462
|
-
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression
|
|
485
|
+
if (LOGICAL_OPERATORS.has(expression.operator)) checkJunction(operands, expression);
|
|
463
486
|
};
|
|
464
487
|
|
|
465
488
|
const checkTemporal = (
|
|
@@ -473,7 +496,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
473
496
|
expression.kind === "Always"
|
|
474
497
|
? "[] followed by action not of form [A]_v"
|
|
475
498
|
: "<> followed by action not of form <<A>>_v",
|
|
476
|
-
expression
|
|
499
|
+
expression,
|
|
477
500
|
);
|
|
478
501
|
};
|
|
479
502
|
|
|
@@ -501,7 +524,17 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
501
524
|
return data;
|
|
502
525
|
};
|
|
503
526
|
|
|
527
|
+
// SANY reports a level error of an operator's arguments at the application.
|
|
504
528
|
const analyze = (expression: Expression, scope: Scope): LevelData => {
|
|
529
|
+
try {
|
|
530
|
+
return analyzeNode(expression, scope);
|
|
531
|
+
} catch (error) {
|
|
532
|
+
if (!(error instanceof TlaSyntaxError) || error.sany) throw error;
|
|
533
|
+
throw createLevelError(error.message, expression);
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
const analyzeNode = (expression: Expression, scope: Scope): LevelData => {
|
|
505
538
|
switch (expression.kind) {
|
|
506
539
|
case "Number":
|
|
507
540
|
case "Decimal":
|
|
@@ -521,7 +554,7 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
521
554
|
case "And":
|
|
522
555
|
case "Or": {
|
|
523
556
|
const operands = toOperands(expression.operands, scope);
|
|
524
|
-
checkJunction(operands, expression
|
|
557
|
+
checkJunction(operands, expression);
|
|
525
558
|
return applyDefined(JUNCTION_LEVELS, operands);
|
|
526
559
|
}
|
|
527
560
|
case "Prime":
|
|
@@ -561,13 +594,14 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
561
594
|
expression.bounds.map((bound) => bound.set),
|
|
562
595
|
getBoundNames(expression.bounds),
|
|
563
596
|
);
|
|
564
|
-
|
|
565
|
-
data.level === TEMPORAL_LEVEL
|
|
566
|
-
|
|
567
|
-
|
|
597
|
+
const actionBound =
|
|
598
|
+
data.level === TEMPORAL_LEVEL
|
|
599
|
+
? expression.bounds.find((bound) => analyze(bound.set, scope).level === ACTION_LEVEL)
|
|
600
|
+
: undefined;
|
|
601
|
+
if (actionBound) {
|
|
568
602
|
throw createLevelError(
|
|
569
603
|
"Action-level bound of quantified temporal formula",
|
|
570
|
-
|
|
604
|
+
actionBound.set,
|
|
571
605
|
);
|
|
572
606
|
}
|
|
573
607
|
return data;
|
|
@@ -817,34 +851,140 @@ export const createLevelAnalyzer = (context: LevelContext) => {
|
|
|
817
851
|
return { analyze, analyzeDefinition, toOperand };
|
|
818
852
|
};
|
|
819
853
|
|
|
820
|
-
const analyzeAssumeProve = (
|
|
854
|
+
export const analyzeAssumeProve = (
|
|
821
855
|
assumeProve: AssumeProve,
|
|
822
856
|
scope: Scope,
|
|
823
857
|
analyzer: ReturnType<typeof createLevelAnalyzer>,
|
|
824
|
-
) => {
|
|
858
|
+
): LevelData => {
|
|
825
859
|
const inner = new Map(scope);
|
|
826
|
-
assumeProve.assumptions.
|
|
860
|
+
const parts = assumeProve.assumptions.flatMap((assumption) => {
|
|
827
861
|
if (assumption.kind === "NewSymbol") {
|
|
828
|
-
|
|
862
|
+
const set = assumption.set && analyzer.analyze(assumption.set, inner);
|
|
829
863
|
inner.set(assumption.name, { kind: "Formal", arity: assumption.arity });
|
|
830
|
-
return;
|
|
864
|
+
return set ? [set] : [];
|
|
831
865
|
}
|
|
832
|
-
|
|
833
|
-
|
|
866
|
+
return [
|
|
867
|
+
assumption.kind === "AssumeProve"
|
|
868
|
+
? analyzeAssumeProve(assumption, inner, analyzer)
|
|
869
|
+
: analyzer.analyze(assumption, inner),
|
|
870
|
+
];
|
|
834
871
|
});
|
|
835
|
-
analyzer.analyze(assumeProve.conclusion, inner);
|
|
872
|
+
parts.push(analyzer.analyze(assumeProve.conclusion, inner));
|
|
873
|
+
const data = createData(Math.max(...parts.map((part) => part.level)));
|
|
874
|
+
parts.forEach((part) => {
|
|
875
|
+
part.params.forEach((param) => data.params.add(param));
|
|
876
|
+
part.constraints.forEach((value, key) => putConstraint(data.constraints, key, value));
|
|
877
|
+
part.argConstraints.forEach((value, key) => putArgConstraint(data.argConstraints, key, value));
|
|
878
|
+
part.argParams.forEach((key) => data.argParams.add(key));
|
|
879
|
+
});
|
|
880
|
+
return data;
|
|
836
881
|
};
|
|
837
882
|
|
|
838
|
-
// SANY checks the level of every definition, assumption, and
|
|
839
|
-
export const checkModuleLevels = (
|
|
883
|
+
// SANY checks the level of every definition, assumption, theorem, and instance of a module.
|
|
884
|
+
export const checkModuleLevels = (
|
|
885
|
+
module: Module,
|
|
886
|
+
context: LevelContext,
|
|
887
|
+
instances: { location: SourceLocation; check: () => void }[],
|
|
888
|
+
warnings: SanyError[],
|
|
889
|
+
) => {
|
|
840
890
|
const analyzer = createLevelAnalyzer(context);
|
|
841
|
-
|
|
842
|
-
.
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
891
|
+
const getLevel = (expressions: Expression[]) =>
|
|
892
|
+
Math.max(0, ...expressions.map((expression) => analyzer.analyze(expression, new Map()).level));
|
|
893
|
+
const getStepLevel = (step: ProofStep) => {
|
|
894
|
+
const body = step.body;
|
|
895
|
+
if (body.kind === "Have" || body.kind === "Case") return getLevel([body.expression]);
|
|
896
|
+
if (body.kind === "Witness") return getLevel(body.expressions);
|
|
897
|
+
if (body.kind === "Take") return getLevel(body.bounds.map((bound) => bound.set));
|
|
898
|
+
if (body.kind === "Pick") {
|
|
899
|
+
return getLevel([body.body, ...body.bounds.map((bound) => bound.set)]);
|
|
900
|
+
}
|
|
901
|
+
if (body.kind !== "Assert") return 0;
|
|
902
|
+
return body.statement.kind === "AssumeProve"
|
|
903
|
+
? analyzeAssumeProve(body.statement, new Map(), analyzer).level
|
|
904
|
+
: getLevel([body.statement]);
|
|
905
|
+
};
|
|
906
|
+
// SANY's TheoremNode.LevelCheckTemporal: the proof of a temporal goal may only assume constants.
|
|
907
|
+
const checkTemporalGoal = (proof: Proof | undefined): SanyError[] =>
|
|
908
|
+
proof?.kind !== "Steps"
|
|
909
|
+
? []
|
|
910
|
+
: proof.steps.flatMap((step) => {
|
|
911
|
+
const location = formatRange(step.statement);
|
|
912
|
+
const isConstant = () => getStepLevel(step) === 0;
|
|
913
|
+
if (
|
|
914
|
+
step.body.kind === "Have" ||
|
|
915
|
+
step.body.kind === "Take" ||
|
|
916
|
+
step.body.kind === "Witness"
|
|
917
|
+
) {
|
|
918
|
+
return isConstant()
|
|
919
|
+
? []
|
|
920
|
+
: [{ location, message: "Non-constant TAKE, WITNESS, or HAVE for temporal goal." }];
|
|
921
|
+
}
|
|
922
|
+
if (step.body.kind === "Case") {
|
|
923
|
+
return [
|
|
924
|
+
...(isConstant()
|
|
925
|
+
? []
|
|
926
|
+
: [{ location, message: "Non-constant CASE for temporal goal." }]),
|
|
927
|
+
...checkTemporalGoal(step.proof),
|
|
928
|
+
];
|
|
929
|
+
}
|
|
930
|
+
return step.body.kind === "Qed" ? checkTemporalGoal(step.proof) : [];
|
|
931
|
+
});
|
|
932
|
+
// SANY checks each step as a theorem of its own after the steps of its proof.
|
|
933
|
+
const checkProof = (proof: Proof | undefined): SanyError[] =>
|
|
934
|
+
proof?.kind !== "Steps"
|
|
935
|
+
? []
|
|
936
|
+
: proof.steps.flatMap((step) => {
|
|
937
|
+
const body = step.body;
|
|
938
|
+
const isTemporal = () => getStepLevel(step) === TEMPORAL_LEVEL;
|
|
939
|
+
const pickErrors =
|
|
940
|
+
body.kind === "Pick" && body.bounds.length > 0 && isTemporal()
|
|
941
|
+
? body.bounds
|
|
942
|
+
.filter((bound) => getLevel([bound.set]) !== 0)
|
|
943
|
+
.flatMap((bound) =>
|
|
944
|
+
bound.set.location.range
|
|
945
|
+
? [
|
|
946
|
+
{
|
|
947
|
+
location: formatRange(bound.set.location.range),
|
|
948
|
+
message: "Non-constant bound of temporal PICK.",
|
|
949
|
+
},
|
|
950
|
+
]
|
|
951
|
+
: [],
|
|
952
|
+
)
|
|
953
|
+
: [];
|
|
954
|
+
return [
|
|
955
|
+
...checkProof(step.proof),
|
|
956
|
+
...pickErrors,
|
|
957
|
+
...(step.proof?.kind === "Steps" && isTemporal() ? checkTemporalGoal(step.proof) : []),
|
|
958
|
+
];
|
|
959
|
+
});
|
|
960
|
+
collectSemanticErrors(
|
|
961
|
+
[
|
|
962
|
+
...module.definitions
|
|
963
|
+
.filter((definition) => !definition.name.includes("!"))
|
|
964
|
+
.map((definition) => ({
|
|
965
|
+
location: definition.location,
|
|
966
|
+
check: () => analyzer.analyzeDefinition(definition, new Map()),
|
|
967
|
+
})),
|
|
968
|
+
...module.assumptions.map((assumption) => ({
|
|
969
|
+
location: assumption.location,
|
|
970
|
+
check: () => analyzer.analyze(assumption.expression, new Map()),
|
|
971
|
+
})),
|
|
972
|
+
...module.theorems.map((theorem) => ({
|
|
973
|
+
location: theorem.location,
|
|
974
|
+
check: () => {
|
|
975
|
+
const data =
|
|
976
|
+
theorem.statement.kind === "AssumeProve"
|
|
977
|
+
? analyzeAssumeProve(theorem.statement, new Map(), analyzer)
|
|
978
|
+
: analyzer.analyze(theorem.statement, new Map());
|
|
979
|
+
const errors = [
|
|
980
|
+
...checkProof(theorem.proof),
|
|
981
|
+
...(data.level === TEMPORAL_LEVEL ? checkTemporalGoal(theorem.proof) : []),
|
|
982
|
+
];
|
|
983
|
+
if (errors.length > 0) throw createSanyError(errors);
|
|
984
|
+
},
|
|
985
|
+
})),
|
|
986
|
+
...instances,
|
|
987
|
+
],
|
|
988
|
+
warnings,
|
|
849
989
|
);
|
|
850
990
|
};
|
package/src/live-check.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TlaEvaluationError } from "./errors";
|
|
2
|
+
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
2
3
|
import { hasRepeatedEffects, type Environment } from "./evaluate";
|
|
3
4
|
import {
|
|
4
5
|
evaluateLive,
|
|
@@ -49,13 +50,17 @@ interface Check {
|
|
|
49
50
|
actionIds: Int32Array;
|
|
50
51
|
nodeStates: number[];
|
|
51
52
|
nodeTableaux: number[];
|
|
52
|
-
successors
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
// The successors a node gathers until it is written.
|
|
54
|
+
pending: (number[] | undefined)[];
|
|
55
|
+
// Each written node's state results, then its action results edge by edge, from the node's
|
|
56
|
+
// offset in one buffer, so a node costs no objects of its own. An unwritten node has offset -1.
|
|
57
|
+
offsets: number[];
|
|
58
|
+
results: { bytes: Uint8Array; length: number };
|
|
59
|
+
// Each written node's successor count, then its successors, from the node's edge offset.
|
|
60
|
+
edgeOffsets: number[];
|
|
61
|
+
edges: { ids: Int32Array; length: number };
|
|
56
62
|
initial: number[];
|
|
57
|
-
nodeIds:
|
|
58
|
-
nodesByState: Map<number, number[]>;
|
|
63
|
+
nodeIds: NodeTable;
|
|
59
64
|
isDone: boolean[];
|
|
60
65
|
errorNode: number | undefined;
|
|
61
66
|
}
|
|
@@ -70,13 +75,45 @@ const removeRepeats = (path: number[]) =>
|
|
|
70
75
|
const pushNode = (check: Check, state: number, tableauNode: number) => {
|
|
71
76
|
check.nodeStates.push(state);
|
|
72
77
|
check.nodeTableaux.push(tableauNode);
|
|
73
|
-
check.
|
|
74
|
-
check.
|
|
75
|
-
check.
|
|
76
|
-
check.isWritten.push(false);
|
|
78
|
+
check.pending.push(undefined);
|
|
79
|
+
check.offsets.push(-1);
|
|
80
|
+
check.edgeOffsets.push(-1);
|
|
77
81
|
return check.nodeStates.length - 1;
|
|
78
82
|
};
|
|
79
83
|
|
|
84
|
+
// A Map holds at most 2^24 entries, so each tableau node of a state finds its node in an
|
|
85
|
+
// open-addressing table of (state plus one, tableau node, node) triples.
|
|
86
|
+
interface NodeTable {
|
|
87
|
+
slots: Int32Array;
|
|
88
|
+
size: number;
|
|
89
|
+
shift: number;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const getNodeSlot = (table: NodeTable, state: number, tableauNode: number) => {
|
|
93
|
+
const mask = table.slots.length / 3 - 1;
|
|
94
|
+
const hash = Math.imul(Math.imul(state, 0x9e3779b1) ^ tableauNode, 0x85ebca6b) >>> table.shift;
|
|
95
|
+
for (let index = hash; ; index = (index + 1) & mask) {
|
|
96
|
+
const slot = index * 3;
|
|
97
|
+
const stored = table.slots[slot];
|
|
98
|
+
if (stored === 0 || (stored === state + 1 && table.slots[slot + 1] === tableauNode))
|
|
99
|
+
return slot;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// Without a tableau a node is a state, so a successor the search hasn't expanded yet may lie past
|
|
104
|
+
// the per-node arrays.
|
|
105
|
+
const isWritten = (check: Check, node: number) => (check.offsets[node] ?? -1) !== -1;
|
|
106
|
+
|
|
107
|
+
const getPending = (check: Check, node: number) => (check.pending[node] ??= []);
|
|
108
|
+
|
|
109
|
+
const getSuccessors = (check: Check, node: number) => {
|
|
110
|
+
const offset = check.edgeOffsets[node];
|
|
111
|
+
return check.edges.ids.subarray(offset + 1, offset + 1 + check.edges.ids[offset]);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const getStateResult = (check: Check, node: number, position: number) =>
|
|
115
|
+
check.results.bytes[check.offsets[node] + position] === 1;
|
|
116
|
+
|
|
80
117
|
const SUCCESSOR_CAPACITY = 16;
|
|
81
118
|
|
|
82
119
|
const SCC_MARKER = -42;
|
|
@@ -103,7 +140,10 @@ export const createLivenessChecker = (
|
|
|
103
140
|
fairness.length === 0 ? violation : createJunction("And", [...fairness, violation]);
|
|
104
141
|
const solutions = getSolutions(formula);
|
|
105
142
|
if (solutions.length === 0) {
|
|
106
|
-
throw
|
|
143
|
+
throw setExitCode(
|
|
144
|
+
new TlaEvaluationError(TAUTOLOGY_MESSAGE),
|
|
145
|
+
EXIT_STATUS.livenessEvaluationFailure,
|
|
146
|
+
);
|
|
107
147
|
}
|
|
108
148
|
const actionIds = new Map<string, number>();
|
|
109
149
|
const checks = solutions.map((solution): Check => ({
|
|
@@ -116,13 +156,13 @@ export const createLivenessChecker = (
|
|
|
116
156
|
}),
|
|
117
157
|
nodeStates: [],
|
|
118
158
|
nodeTableaux: [],
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
159
|
+
pending: [],
|
|
160
|
+
offsets: [],
|
|
161
|
+
results: { bytes: new Uint8Array(1024), length: 0 },
|
|
162
|
+
edgeOffsets: [],
|
|
163
|
+
edges: { ids: new Int32Array(1024), length: 0 },
|
|
123
164
|
initial: [],
|
|
124
|
-
nodeIds: new
|
|
125
|
-
nodesByState: new Map(),
|
|
165
|
+
nodeIds: { slots: new Int32Array(3 << 10), size: 0, shift: 22 },
|
|
126
166
|
isDone: [],
|
|
127
167
|
errorNode: undefined,
|
|
128
168
|
}));
|
|
@@ -208,18 +248,35 @@ export const createLivenessChecker = (
|
|
|
208
248
|
model.infinitelyOftenActions.length === 0,
|
|
209
249
|
);
|
|
210
250
|
|
|
211
|
-
const getNodeId = (check: Check, state: number, tableauNode: number) =>
|
|
212
|
-
check.solution.tableau
|
|
213
|
-
|
|
214
|
-
|
|
251
|
+
const getNodeId = (check: Check, state: number, tableauNode: number) => {
|
|
252
|
+
if (!check.solution.tableau) return state;
|
|
253
|
+
const slots = check.nodeIds.slots;
|
|
254
|
+
const slot = getNodeSlot(check.nodeIds, state, tableauNode);
|
|
255
|
+
return slots[slot] === 0 ? undefined : slots[slot + 2];
|
|
256
|
+
};
|
|
215
257
|
const recordNode = (check: Check, state: number, tableauNode: number) => {
|
|
216
258
|
const node = pushNode(check, state, tableauNode);
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
259
|
+
const table = check.nodeIds;
|
|
260
|
+
if (++table.size * 2 > table.slots.length / 3) {
|
|
261
|
+
const old = table.slots;
|
|
262
|
+
table.slots = new Int32Array(old.length * 2);
|
|
263
|
+
table.shift--;
|
|
264
|
+
for (let slot = 0; slot < old.length; slot += 3) {
|
|
265
|
+
if (old[slot] !== 0)
|
|
266
|
+
table.slots.set(
|
|
267
|
+
old.subarray(slot, slot + 3),
|
|
268
|
+
getNodeSlot(table, old[slot] - 1, old[slot + 1]),
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
table.slots.set([state + 1, tableauNode, node], getNodeSlot(table, state, tableauNode));
|
|
221
273
|
return node;
|
|
222
274
|
};
|
|
275
|
+
// A state's nodes in the order they were recorded, which is the order of their ids.
|
|
276
|
+
const getStateNodes = (check: Check, tableau: Tableau, state: number) =>
|
|
277
|
+
tableau.nodes
|
|
278
|
+
.flatMap((_, tableauNode) => getNodeId(check, state, tableauNode) ?? [])
|
|
279
|
+
.sort((left, right) => left - right);
|
|
223
280
|
const writeNode = (
|
|
224
281
|
check: Check,
|
|
225
282
|
node: number,
|
|
@@ -227,11 +284,32 @@ export const createLivenessChecker = (
|
|
|
227
284
|
stateResults: Uint8Array,
|
|
228
285
|
) => {
|
|
229
286
|
const actionCount = check.solution.actionChecks.length;
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
287
|
+
const results = check.results;
|
|
288
|
+
const offset = results.length;
|
|
289
|
+
results.length += stateResults.length + actions.length * actionCount;
|
|
290
|
+
if (results.length > results.bytes.length) {
|
|
291
|
+
const grown = new Uint8Array(Math.max(2 * results.bytes.length, results.length));
|
|
292
|
+
grown.set(results.bytes);
|
|
293
|
+
results.bytes = grown;
|
|
294
|
+
}
|
|
295
|
+
results.bytes.set(stateResults, offset);
|
|
296
|
+
actions.forEach((edge, position) =>
|
|
297
|
+
results.bytes.set(edge, offset + stateResults.length + position * actionCount),
|
|
298
|
+
);
|
|
299
|
+
check.offsets[node] = offset;
|
|
300
|
+
const successors = check.pending[node] ?? [];
|
|
301
|
+
const edges = check.edges;
|
|
302
|
+
const edgeOffset = edges.length;
|
|
303
|
+
edges.length += successors.length + 1;
|
|
304
|
+
if (edges.length > edges.ids.length) {
|
|
305
|
+
const grown = new Int32Array(Math.max(2 * edges.ids.length, edges.length));
|
|
306
|
+
grown.set(edges.ids);
|
|
307
|
+
edges.ids = grown;
|
|
308
|
+
}
|
|
309
|
+
edges.ids[edgeOffset] = successors.length;
|
|
310
|
+
edges.ids.set(successors, edgeOffset + 1);
|
|
311
|
+
check.edgeOffsets[node] = edgeOffset;
|
|
312
|
+
check.pending[node] = undefined;
|
|
235
313
|
};
|
|
236
314
|
|
|
237
315
|
// TLC collects a state's successors and the state itself in a SetOfStates, a hash set it
|
|
@@ -284,7 +362,7 @@ export const createLivenessChecker = (
|
|
|
284
362
|
const stateResults = checkState(check, index, state);
|
|
285
363
|
const tableauNode = tableau.nodes[check.nodeTableaux[node]];
|
|
286
364
|
const actions: Uint8Array[] = [];
|
|
287
|
-
const successors = check
|
|
365
|
+
const successors = getPending(check, node);
|
|
288
366
|
const selfActions =
|
|
289
367
|
tableauNode.successors.length > 0
|
|
290
368
|
? checkAction(check, index, state, state)
|
|
@@ -333,8 +411,8 @@ export const createLivenessChecker = (
|
|
|
333
411
|
);
|
|
334
412
|
check.isDone[index] = true;
|
|
335
413
|
// TLC expands the nodes recorded before the loop, not the ones the loop records.
|
|
336
|
-
for (const node of (check
|
|
337
|
-
const successors = check
|
|
414
|
+
for (const node of getStateNodes(check, tableau, index)) {
|
|
415
|
+
const successors = getPending(check, node);
|
|
338
416
|
const actions: Uint8Array[] = [];
|
|
339
417
|
set.forEach((successor, position) => {
|
|
340
418
|
const isDone = check.isDone[successor.index] === true;
|
|
@@ -368,10 +446,10 @@ export const createLivenessChecker = (
|
|
|
368
446
|
index: number,
|
|
369
447
|
) => {
|
|
370
448
|
while (check.nodeStates.length <= index) pushNode(check, check.nodeStates.length, -1);
|
|
371
|
-
const successors = check
|
|
449
|
+
const successors = getPending(check, index);
|
|
372
450
|
const actions: Uint8Array[] = [];
|
|
373
451
|
set.forEach((successor, position) => {
|
|
374
|
-
if (check
|
|
452
|
+
if (isWritten(check, successor.index) && successors.includes(successor.index)) return;
|
|
375
453
|
successors.push(successor.index);
|
|
376
454
|
actions.push(actionResults[position]);
|
|
377
455
|
});
|
|
@@ -385,20 +463,20 @@ export const createLivenessChecker = (
|
|
|
385
463
|
const parents = new Map<number, number>();
|
|
386
464
|
const queue: number[] = [];
|
|
387
465
|
for (const node of check.initial) {
|
|
388
|
-
if (!check
|
|
466
|
+
if (!isWritten(check, node)) continue;
|
|
389
467
|
queue.push(node);
|
|
390
468
|
parents.set(node, -1);
|
|
391
469
|
}
|
|
392
470
|
for (let position = 0; position < queue.length; position++) {
|
|
393
471
|
const current = queue[position];
|
|
394
|
-
for (const next of check
|
|
472
|
+
for (const next of getSuccessors(check, current)) {
|
|
395
473
|
if (next === current && check.solution.tableau) continue;
|
|
396
474
|
if (next === target) {
|
|
397
475
|
const path = [target];
|
|
398
476
|
for (let node = current; node !== -1; node = parents.get(node) ?? -1) path.push(node);
|
|
399
477
|
return path;
|
|
400
478
|
}
|
|
401
|
-
if (!check
|
|
479
|
+
if (!isWritten(check, next) || parents.has(next)) continue;
|
|
402
480
|
parents.set(next, current);
|
|
403
481
|
queue.push(next);
|
|
404
482
|
}
|
|
@@ -410,16 +488,17 @@ export const createLivenessChecker = (
|
|
|
410
488
|
const findInModel = (check: Check, model: Solution["models"][number]): Lasso | undefined => {
|
|
411
489
|
const solution = check.solution;
|
|
412
490
|
const actionCount = solution.actionChecks.length;
|
|
491
|
+
const stateCount = solution.stateChecks.length;
|
|
413
492
|
const getAction = (node: number, edge: number, position: number) =>
|
|
414
|
-
check.
|
|
493
|
+
check.results.bytes[check.offsets[node] + stateCount + edge * actionCount + position] === 1;
|
|
415
494
|
const isAllowed = (node: number, edge: number) =>
|
|
416
495
|
model.eventuallyAlways.every((position) => getAction(node, edge, position));
|
|
417
496
|
const links = new Float64Array(check.nodeStates.length).fill(UNVISITED);
|
|
418
|
-
const queue = check.initial.filter((node) => check
|
|
497
|
+
const queue = check.initial.filter((node) => isWritten(check, node));
|
|
419
498
|
const components: number[] = [];
|
|
420
499
|
|
|
421
500
|
const isStuttering = (node: number) => {
|
|
422
|
-
const edge = check
|
|
501
|
+
const edge = getSuccessors(check, node).indexOf(node);
|
|
423
502
|
return edge !== -1 && isAllowed(node, edge);
|
|
424
503
|
};
|
|
425
504
|
const isFulfilled = (node: number, promise: Solution["promises"][number]) =>
|
|
@@ -443,11 +522,11 @@ export const createLivenessChecker = (
|
|
|
443
522
|
if (member === root) break;
|
|
444
523
|
}
|
|
445
524
|
const isStateSatisfied = model.infinitelyOftenStates.every((position) =>
|
|
446
|
-
[...members].some((node) => check
|
|
525
|
+
[...members].some((node) => getStateResult(check, node, position)),
|
|
447
526
|
);
|
|
448
527
|
const isActionSatisfied = model.infinitelyOftenActions.every((position) =>
|
|
449
528
|
[...members].some((node) =>
|
|
450
|
-
check
|
|
529
|
+
getSuccessors(check, node).some(
|
|
451
530
|
(next, edge) =>
|
|
452
531
|
members.has(next) && isAllowed(node, edge) && getAction(node, edge, position),
|
|
453
532
|
),
|
|
@@ -488,7 +567,7 @@ export const createLivenessChecker = (
|
|
|
488
567
|
const countAtStart = remaining.count;
|
|
489
568
|
while (true) {
|
|
490
569
|
model.infinitelyOftenStates.forEach((position, slot) => {
|
|
491
|
-
if (!stateDone[slot] && check
|
|
570
|
+
if (!stateDone[slot] && getStateResult(check, current.node, position)) {
|
|
492
571
|
stateDone[slot] = true;
|
|
493
572
|
remaining.count--;
|
|
494
573
|
}
|
|
@@ -502,7 +581,7 @@ export const createLivenessChecker = (
|
|
|
502
581
|
if (remaining.count <= 0) break;
|
|
503
582
|
const step = { last: -1, unvisited: -1 };
|
|
504
583
|
const countBeforeActions = remaining.count;
|
|
505
|
-
const successors = check
|
|
584
|
+
const successors = getSuccessors(check, current.node);
|
|
506
585
|
const taken = successors.some((next, edge) => {
|
|
507
586
|
if (membersByState.has(check.nodeStates[next])) {
|
|
508
587
|
if (!isMember(next) || !isAllowed(current.node, edge)) return false;
|
|
@@ -537,7 +616,8 @@ export const createLivenessChecker = (
|
|
|
537
616
|
}
|
|
538
617
|
current.node = previous;
|
|
539
618
|
step.unvisited =
|
|
540
|
-
check
|
|
619
|
+
getSuccessors(check, previous).find((next) => isMember(next) && !seen.has(next)) ??
|
|
620
|
+
-1;
|
|
541
621
|
}
|
|
542
622
|
take(step.unvisited);
|
|
543
623
|
seen.add(step.unvisited);
|
|
@@ -567,7 +647,7 @@ export const createLivenessChecker = (
|
|
|
567
647
|
const queue: [number, number][] = [];
|
|
568
648
|
for (let state = start, parent = -1, position = 0; ; position++) {
|
|
569
649
|
for (const node of membersByState.get(state) ?? []) {
|
|
570
|
-
for (const [edge, next] of check
|
|
650
|
+
for (const [edge, next] of getSuccessors(check, node).entries()) {
|
|
571
651
|
if (next === node || !isAllowedEdge(node, edge)) continue;
|
|
572
652
|
if (next === root) {
|
|
573
653
|
const path: number[] = [];
|
|
@@ -620,8 +700,8 @@ export const createLivenessChecker = (
|
|
|
620
700
|
stack.push(lowLink, node, SCC_MARKER);
|
|
621
701
|
components.push(node);
|
|
622
702
|
const nextLowLink = { value: counter.link++ };
|
|
623
|
-
check
|
|
624
|
-
if (!check
|
|
703
|
+
getSuccessors(check, node).forEach((next, edge) => {
|
|
704
|
+
if (!isWritten(check, next)) return;
|
|
625
705
|
if (!isAllowed(node, edge)) {
|
|
626
706
|
if (links[next] === UNVISITED) queue.push(next);
|
|
627
707
|
return;
|