tla-ts 0.0.3-dev.bfd303f → 0.0.3-dev.c8c988d
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 +25 -1
- package/dist/index.d.ts +25 -1
- package/dist/run-cli.cjs +5 -3
- package/dist/run-cli.js +20 -18
- package/dist/transfer.cjs +22 -17
- package/dist/transfer.js +22 -17
- package/package.json +1 -1
- package/src/ast.ts +6 -1
- package/src/builtins.ts +9 -3
- package/src/check.ts +234 -53
- package/src/config.ts +224 -170
- package/src/environment.ts +32 -6
- package/src/errors.ts +52 -1
- package/src/evaluate.ts +131 -13
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +1 -1
- package/src/levels.ts +166 -36
- package/src/live-check.ts +3 -1
- package/src/liveness.ts +5 -1
- package/src/parser.ts +60 -17
- package/src/resolve.ts +108 -29
- package/src/run-cli.ts +56 -9
- package/src/spec-description.ts +1 -2
- package/src/standard-modules.ts +52 -38
- package/src/substitution-levels.ts +35 -11
- package/src/successors.ts +87 -17
- package/src/value-sources.ts +29 -0
- package/src/values.ts +122 -42
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { Declaration, Definition, Expression, SourceLocation } from "./ast";
|
|
2
|
-
import {
|
|
1
|
+
import type { Declaration, Definition, Expression, SourceLocation, Theorem } from "./ast";
|
|
2
|
+
import { createSanyError, formatRange } from "./errors";
|
|
3
3
|
import {
|
|
4
|
+
ACTION_LEVEL,
|
|
5
|
+
analyzeAssumeProve,
|
|
4
6
|
createLevelAnalyzer,
|
|
5
7
|
getArgumentKey,
|
|
6
8
|
MAX_LEVEL,
|
|
@@ -24,6 +26,7 @@ interface ModuleContext {
|
|
|
24
26
|
variables: string[];
|
|
25
27
|
definitions: Definition[];
|
|
26
28
|
assumptions: Expression[];
|
|
29
|
+
theorems: Theorem[];
|
|
27
30
|
getDefinition: (name: string) => Definition | undefined;
|
|
28
31
|
}
|
|
29
32
|
|
|
@@ -101,17 +104,38 @@ export const checkInstances = (
|
|
|
101
104
|
definition.formals.every((formal) => formal.name !== operator && formal.name !== param),
|
|
102
105
|
),
|
|
103
106
|
);
|
|
107
|
+
const addAssumedLevels = (data: ReturnType<typeof analyzeAssumeProve>) => {
|
|
108
|
+
addLevels(data, () => true);
|
|
109
|
+
module.constants.forEach((constant) => {
|
|
110
|
+
if (data.params.has(constant.name)) {
|
|
111
|
+
putConstraint(levels.constraints, constant.name, ACTION_LEVEL);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
};
|
|
104
115
|
module.assumptions.forEach((assumption) =>
|
|
105
|
-
|
|
116
|
+
addAssumedLevels(analyzer.analyze(assumption, new Map())),
|
|
117
|
+
);
|
|
118
|
+
module.theorems.forEach((theorem) =>
|
|
119
|
+
theorem.statement.kind === "AssumeProve"
|
|
120
|
+
? addAssumedLevels(analyzeAssumeProve(theorem.statement, new Map(), analyzer))
|
|
121
|
+
: addLevels(analyzer.analyze(theorem.statement, new Map()), () => true),
|
|
106
122
|
);
|
|
107
123
|
moduleLevels.set(name, levels);
|
|
108
124
|
return levels;
|
|
109
125
|
};
|
|
110
126
|
|
|
111
|
-
const
|
|
127
|
+
const errors = new Map<string, { location: string; message: string }>();
|
|
112
128
|
checks.forEach((check) => {
|
|
113
|
-
|
|
114
|
-
const
|
|
129
|
+
// SANY reports each error at the whole instance.
|
|
130
|
+
const location = formatRange(
|
|
131
|
+
check.location.range ?? {
|
|
132
|
+
module: check.instancer,
|
|
133
|
+
start: check.location,
|
|
134
|
+
end: check.location,
|
|
135
|
+
},
|
|
136
|
+
);
|
|
137
|
+
const addMessage = (message: string) =>
|
|
138
|
+
errors.set(`${location}\n${message}`, { location, message });
|
|
115
139
|
const instancee = getModule(check.module);
|
|
116
140
|
const levels = getModuleLevels(check.module);
|
|
117
141
|
const analyzer = getAnalyzer(check.instancer);
|
|
@@ -141,14 +165,14 @@ export const checkInstances = (
|
|
|
141
165
|
];
|
|
142
166
|
});
|
|
143
167
|
const getLevelMessage = (name: string, level: number) =>
|
|
144
|
-
`Level error in instantiating module '${check.module}'
|
|
168
|
+
`Level error in instantiating module '${check.module}':\nThe level of the expression or operator substituted for '${name}' \nmust be at most ${level}.`;
|
|
145
169
|
substituted.forEach((item) => {
|
|
146
170
|
if (!levels.isConstant && item.level > item.maximumLevel) {
|
|
147
171
|
addMessage(getLevelMessage(item.name, item.maximumLevel));
|
|
148
172
|
}
|
|
149
173
|
if (!item.isLeibniz) {
|
|
150
174
|
addMessage(
|
|
151
|
-
`Error in instantiating module '${check.module}'
|
|
175
|
+
`Error in instantiating module '${check.module}':\n A non-Leibniz operator substituted for '${item.name}'.`,
|
|
152
176
|
);
|
|
153
177
|
}
|
|
154
178
|
});
|
|
@@ -162,7 +186,7 @@ export const checkInstances = (
|
|
|
162
186
|
const minimum = levels.argConstraints.get(getArgumentKey(item.name, index));
|
|
163
187
|
if (minimum !== undefined && getMaxLevel(operator, index) < minimum) {
|
|
164
188
|
addMessage(
|
|
165
|
-
`Level error in instantiating module '${check.module}'
|
|
189
|
+
`Level error in instantiating module '${check.module}':\nThe level of the argument ${index + 1} of the operator ${operator.name} \nmust be at least ${minimum}.`,
|
|
166
190
|
);
|
|
167
191
|
}
|
|
168
192
|
});
|
|
@@ -175,10 +199,10 @@ export const checkInstances = (
|
|
|
175
199
|
const maximum = getMaxLevel(operator, parameter.index);
|
|
176
200
|
if (argument.level > maximum) {
|
|
177
201
|
addMessage(
|
|
178
|
-
`Level error when instantiating module '${check.module}'
|
|
202
|
+
`Level error when instantiating module '${check.module}':\nThe level of the argument ${parameter.index} of the operator ${parameter.operator}' \nmust be at most ${maximum}.`,
|
|
179
203
|
);
|
|
180
204
|
}
|
|
181
205
|
});
|
|
182
206
|
});
|
|
183
|
-
if (
|
|
207
|
+
if (errors.size > 0) throw createSanyError([...errors.values()]);
|
|
184
208
|
};
|
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,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Expression } from "./ast";
|
|
2
|
+
import { TlaEvaluationError } from "./errors";
|
|
3
|
+
import type { Value } from "./values";
|
|
4
|
+
|
|
5
|
+
// While TLC replays a failure, it gives each set, tuple or record it builds the expression that
|
|
6
|
+
// built it. A method of such a value that fails prepends that expression to the error's trace, and
|
|
7
|
+
// TLC reports the error as one it was unable to fingerprint.
|
|
8
|
+
export const valueSources: { current: WeakMap<object, Expression> | undefined } = {
|
|
9
|
+
current: undefined,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const fingerprintTraces = new WeakMap<TlaEvaluationError, Expression[]>();
|
|
13
|
+
|
|
14
|
+
export const getFingerprintTrace = (error: unknown) =>
|
|
15
|
+
error instanceof TlaEvaluationError ? fingerprintTraces.get(error) : undefined;
|
|
16
|
+
|
|
17
|
+
export const withSource = <T>(value: Value, run: () => T): T => {
|
|
18
|
+
const sources = valueSources.current;
|
|
19
|
+
if (!sources || typeof value !== "object") return run();
|
|
20
|
+
try {
|
|
21
|
+
return run();
|
|
22
|
+
} catch (error) {
|
|
23
|
+
const source = sources.get(value);
|
|
24
|
+
if (source && error instanceof TlaEvaluationError) {
|
|
25
|
+
fingerprintTraces.set(error, [source, ...(fingerprintTraces.get(error) ?? [])]);
|
|
26
|
+
}
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
};
|
package/src/values.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TlaEvaluationError, TlaModuleError } from "./errors";
|
|
2
2
|
import { prettyPrint } from "./pretty-print";
|
|
3
|
+
import { valueSources, withSource } from "./value-sources";
|
|
3
4
|
|
|
4
5
|
export interface ModelValue {
|
|
5
6
|
kind: "ModelValue";
|
|
@@ -733,16 +734,17 @@ const compareSetLikeTlc = (left: ObjectValue, right: Value): number => {
|
|
|
733
734
|
);
|
|
734
735
|
};
|
|
735
736
|
|
|
736
|
-
const compareLikeTlc = (left: Value, right: Value): number =>
|
|
737
|
-
|
|
738
|
-
if (typeof
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
737
|
+
const compareLikeTlc = (left: Value, right: Value): number =>
|
|
738
|
+
withSource(left, () => {
|
|
739
|
+
if (typeof left !== "object") {
|
|
740
|
+
if (typeof right === typeof left) return compareValues(left, right);
|
|
741
|
+
if (isModelValue(right)) return compareModelValueWith(right, left);
|
|
742
|
+
throw new TlaEvaluationError(describeScalarMismatch(left, right, false));
|
|
743
|
+
}
|
|
744
|
+
if (left.kind === "ModelValue") return compareModelValue(left, right);
|
|
745
|
+
if (isFunctionValue(left)) return compareFunctionLikeTlc(left, right);
|
|
746
|
+
return compareSetLikeTlc(left, right);
|
|
747
|
+
});
|
|
746
748
|
|
|
747
749
|
const equalsElementsLikeTlc = (left: Value[], right: Value[]) =>
|
|
748
750
|
left.every((element, index) => equalsLikeTlc(element, right[index]));
|
|
@@ -830,16 +832,17 @@ const equalsSetLikeTlc = (left: ObjectValue, right: Value): boolean => {
|
|
|
830
832
|
);
|
|
831
833
|
};
|
|
832
834
|
|
|
833
|
-
const equalsLikeTlc = (left: Value, right: Value): boolean =>
|
|
834
|
-
|
|
835
|
-
if (typeof
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
835
|
+
const equalsLikeTlc = (left: Value, right: Value): boolean =>
|
|
836
|
+
withSource(left, () => {
|
|
837
|
+
if (typeof left !== "object") {
|
|
838
|
+
if (typeof right === typeof left) return left === right;
|
|
839
|
+
if (isModelValue(right)) return equalsModelValueWith(right, left);
|
|
840
|
+
throw new TlaEvaluationError(describeScalarMismatch(left, right, true));
|
|
841
|
+
}
|
|
842
|
+
if (left.kind === "ModelValue") return equalsModelValue(left, right);
|
|
843
|
+
if (isFunctionValue(left)) return equalsFunctionLikeTlc(left, right);
|
|
844
|
+
return equalsSetLikeTlc(left, right);
|
|
845
|
+
});
|
|
843
846
|
|
|
844
847
|
// TLC's ValueVec.sort, which inserts each element into the sorted prefix by binary search.
|
|
845
848
|
const sortLikeTlc = (elements: Value[]) => {
|
|
@@ -1196,6 +1199,41 @@ const advanceCombination = (indices: number[], length: number) => {
|
|
|
1196
1199
|
return false;
|
|
1197
1200
|
};
|
|
1198
1201
|
|
|
1202
|
+
// TLC enumerates SUBSET S lazily, by size and then in the order of S, so a quantifier that finds
|
|
1203
|
+
// its witness early never builds the rest. For a power set too large to build, the elements are
|
|
1204
|
+
// computed in that order as a loop reads them.
|
|
1205
|
+
export const getLazySubsets = (value: Value): Value[] | undefined => {
|
|
1206
|
+
if (typeof value !== "object" || value.kind !== "Subset" || isOverriddenSet(value.base)) {
|
|
1207
|
+
return undefined;
|
|
1208
|
+
}
|
|
1209
|
+
const base = toFiniteSet(value.base).elements;
|
|
1210
|
+
if (base.length <= MAXIMUM_ENUMERATED_SUBSET_BASE) return undefined;
|
|
1211
|
+
const cursor = { index: -1, indices: [0] };
|
|
1212
|
+
const getSubset = (index: number) => {
|
|
1213
|
+
if (index <= cursor.index) cursor.index = -1;
|
|
1214
|
+
while (cursor.index < index) {
|
|
1215
|
+
if (cursor.index === -1) cursor.indices = [];
|
|
1216
|
+
else if (!advanceCombination(cursor.indices, base.length)) {
|
|
1217
|
+
cursor.indices = Array.from({ length: cursor.indices.length + 1 }, (_, item) => item);
|
|
1218
|
+
}
|
|
1219
|
+
cursor.index++;
|
|
1220
|
+
}
|
|
1221
|
+
return createSortedSet(cursor.indices.map((item) => base[item]));
|
|
1222
|
+
};
|
|
1223
|
+
const length = 2 ** base.length;
|
|
1224
|
+
const isIndex = (property: string | symbol) =>
|
|
1225
|
+
typeof property === "string" && /^\d+$/.test(property) && Number(property) < length;
|
|
1226
|
+
return new Proxy<Value[]>([], {
|
|
1227
|
+
get: (target, property, receiver) => {
|
|
1228
|
+
if (property === "length") return length;
|
|
1229
|
+
return isIndex(property)
|
|
1230
|
+
? getSubset(Number(property))
|
|
1231
|
+
: Reflect.get(target, property, receiver);
|
|
1232
|
+
},
|
|
1233
|
+
has: (target, property) => isIndex(property) || Reflect.has(target, property),
|
|
1234
|
+
});
|
|
1235
|
+
};
|
|
1236
|
+
|
|
1199
1237
|
const getSortedSubsets = (base: Value[]) => {
|
|
1200
1238
|
const subsets: Value[] = [];
|
|
1201
1239
|
for (let size = 0; size <= base.length; size++) {
|
|
@@ -1288,7 +1326,7 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1288
1326
|
case "SetOperation": {
|
|
1289
1327
|
if (
|
|
1290
1328
|
value.operator === "\\cup" &&
|
|
1291
|
-
(
|
|
1329
|
+
(!isTlcEnumerable(value.left) || !isTlcEnumerable(value.right))
|
|
1292
1330
|
) {
|
|
1293
1331
|
throw new TlaEvaluationError(
|
|
1294
1332
|
`Attempted to enumerate S \\cup T when S:\n${formatValue(value.left)}\nand T:\n${formatValue(value.right)}\nare not both enumerable`,
|
|
@@ -1296,14 +1334,14 @@ const enumerateSet = (value: Value): Value[] => {
|
|
|
1296
1334
|
}
|
|
1297
1335
|
if (
|
|
1298
1336
|
value.operator === "\\cap" &&
|
|
1299
|
-
|
|
1300
|
-
|
|
1337
|
+
!isTlcEnumerable(value.left) &&
|
|
1338
|
+
!isTlcEnumerable(value.right)
|
|
1301
1339
|
) {
|
|
1302
1340
|
throw new TlaEvaluationError(
|
|
1303
1341
|
`Attempted to enumerate S \\cap T when neither S:\n${formatValue(value.left)}\nnor T:\n${formatValue(value.right)}\nis enumerable`,
|
|
1304
1342
|
);
|
|
1305
1343
|
}
|
|
1306
|
-
if (value.operator === "\\" &&
|
|
1344
|
+
if (value.operator === "\\" && !isTlcEnumerable(value.left)) {
|
|
1307
1345
|
throw new TlaEvaluationError(
|
|
1308
1346
|
`Attempted to enumerate S \\ T when S:\n${formatValue(value.left)}\nis not enumerable.`,
|
|
1309
1347
|
);
|
|
@@ -1338,7 +1376,10 @@ export const toFiniteSet = (value: Value): SetValue => {
|
|
|
1338
1376
|
const cached = typeof value === "object" ? finiteSets.get(value) : undefined;
|
|
1339
1377
|
if (cached) return cached;
|
|
1340
1378
|
const set =
|
|
1341
|
-
(typeof value === "object" ? comparedSets.get(value) : undefined) ??
|
|
1379
|
+
(typeof value === "object" ? comparedSets.get(value) : undefined) ??
|
|
1380
|
+
(valueSources.current
|
|
1381
|
+
? withSource(value, () => enumerateFiniteSet(value))
|
|
1382
|
+
: enumerateFiniteSet(value));
|
|
1342
1383
|
if (typeof value === "object") finiteSets.set(value, set);
|
|
1343
1384
|
return set;
|
|
1344
1385
|
};
|
|
@@ -1387,14 +1428,14 @@ export const getStreamedElements = (value: Value): Value[] => {
|
|
|
1387
1428
|
}
|
|
1388
1429
|
if (value.kind !== "SetOperation") return toFiniteSet(value).elements;
|
|
1389
1430
|
if (value.operator === "\\cup") {
|
|
1390
|
-
return
|
|
1431
|
+
return !isTlcEnumerable(value.left) || !isTlcEnumerable(value.right)
|
|
1391
1432
|
? toFiniteSet(value).elements
|
|
1392
1433
|
: [...getStreamedElements(value.left), ...getStreamedElements(value.right)];
|
|
1393
1434
|
}
|
|
1394
1435
|
const isLeftEnumerated = value.operator === "\\" || isEnumerable(value.left);
|
|
1395
1436
|
const enumerated = isLeftEnumerated ? value.left : value.right;
|
|
1396
1437
|
const other = isLeftEnumerated ? value.right : value.left;
|
|
1397
|
-
if (
|
|
1438
|
+
if (!isTlcEnumerable(enumerated)) return toFiniteSet(value).elements;
|
|
1398
1439
|
return getStreamedElements(enumerated).filter(
|
|
1399
1440
|
(element) => isElementOf(element, other) === (value.operator === "\\cap"),
|
|
1400
1441
|
);
|
|
@@ -1597,6 +1638,12 @@ const describeNonSetMembership = (element: Value, value: Value) => {
|
|
|
1597
1638
|
return `${prefix} function ${formatValue(value)}`;
|
|
1598
1639
|
};
|
|
1599
1640
|
|
|
1641
|
+
// TLC checks membership in a set that another set holds through the inner set's own method.
|
|
1642
|
+
const isElementOfSet = (element: Value, set: Value) =>
|
|
1643
|
+
valueSources.current
|
|
1644
|
+
? withSource(set, () => isElementOf(element, set))
|
|
1645
|
+
: isElementOf(element, set);
|
|
1646
|
+
|
|
1600
1647
|
export const isElementOf = (element: Value, set: Value): boolean => {
|
|
1601
1648
|
if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
|
|
1602
1649
|
switch (set.kind) {
|
|
@@ -1605,10 +1652,10 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1605
1652
|
return findIndex(set.elements, element) !== -1;
|
|
1606
1653
|
} catch (error) {
|
|
1607
1654
|
if (!(error instanceof TlaEvaluationError)) throw error;
|
|
1608
|
-
return isElementOfLikeTlc(element, set.elements);
|
|
1655
|
+
return withSource(set, () => isElementOfLikeTlc(element, set.elements));
|
|
1609
1656
|
}
|
|
1610
1657
|
case "MixedSet":
|
|
1611
|
-
return isElementOfLikeTlc(element, set.elements);
|
|
1658
|
+
return withSource(set, () => isElementOfLikeTlc(element, set.elements));
|
|
1612
1659
|
case "Interval":
|
|
1613
1660
|
if (typeof element === "number") return element >= set.low && element <= set.high;
|
|
1614
1661
|
if (set.low <= set.high && !isModelValue(element)) {
|
|
@@ -1630,12 +1677,18 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1630
1677
|
return set.name !== "Nat" || (typeof element === "number" && element >= 0);
|
|
1631
1678
|
}
|
|
1632
1679
|
case "Subset":
|
|
1633
|
-
if (!
|
|
1634
|
-
|
|
1680
|
+
if (!isTlcEnumerable(element)) {
|
|
1681
|
+
throw new TlaEvaluationError(
|
|
1682
|
+
`Attempted to check if the non-enumerable value\n${prettyPrint(formatValue(element))}\nis element of\n${prettyPrint(formatValue(set))}`,
|
|
1683
|
+
);
|
|
1684
|
+
}
|
|
1685
|
+
return toFiniteSet(element).elements.every((item) => isElementOfSet(item, set.base));
|
|
1635
1686
|
case "FunctionSet": {
|
|
1636
1687
|
if (!isFunctionValue(element)) {
|
|
1637
1688
|
if (isModelValue(element)) return false;
|
|
1638
|
-
throw new TlaEvaluationError(
|
|
1689
|
+
throw new TlaEvaluationError(
|
|
1690
|
+
`Attempted to check if \n${formatValue(element)}\nwhich is not a TLC function value, is in the set of functions:\n${prettyPrint(formatValue(set))}`,
|
|
1691
|
+
);
|
|
1639
1692
|
}
|
|
1640
1693
|
const functionValue = toFiniteFunction(element);
|
|
1641
1694
|
const low = getIntervalLow(functionValue.domain);
|
|
@@ -1646,7 +1699,7 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1646
1699
|
{ kind: "Interval", low, high: low + functionValue.domain.length - 1 },
|
|
1647
1700
|
set.domain,
|
|
1648
1701
|
);
|
|
1649
|
-
return isDomain && functionValue.range.every((item) =>
|
|
1702
|
+
return isDomain && functionValue.range.every((item) => isElementOfSet(item, set.range));
|
|
1650
1703
|
}
|
|
1651
1704
|
case "RecordSet": {
|
|
1652
1705
|
const functionValue = isFunctionValue(element) ? toFiniteFunction(element) : undefined;
|
|
@@ -1660,7 +1713,7 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1660
1713
|
functionValue.domain.length === set.names.length &&
|
|
1661
1714
|
set.names.every((name, index) => {
|
|
1662
1715
|
const position = findIndex(functionValue.domain, name);
|
|
1663
|
-
return position !== -1 &&
|
|
1716
|
+
return position !== -1 && isElementOfSet(functionValue.range[position], set.sets[index]);
|
|
1664
1717
|
})
|
|
1665
1718
|
);
|
|
1666
1719
|
}
|
|
@@ -1677,24 +1730,24 @@ export const isElementOf = (element: Value, set: Value): boolean => {
|
|
|
1677
1730
|
}
|
|
1678
1731
|
return (
|
|
1679
1732
|
elements.length === set.factors.length &&
|
|
1680
|
-
elements.every((item, index) =>
|
|
1733
|
+
elements.every((item, index) => isElementOfSet(item, set.factors[index]))
|
|
1681
1734
|
);
|
|
1682
1735
|
}
|
|
1683
1736
|
case "FilteredSet":
|
|
1684
|
-
return
|
|
1737
|
+
return isElementOfSet(element, set.base) && set.test(element);
|
|
1685
1738
|
case "Union":
|
|
1686
1739
|
if (!isTlcEnumerable(set.base)) {
|
|
1687
1740
|
throw new TlaEvaluationError(
|
|
1688
1741
|
`Attempted to check if:\n ${formatValue(element)}\nis an element of the non-enumerable set:\n ${formatUnion(set)}`,
|
|
1689
1742
|
);
|
|
1690
1743
|
}
|
|
1691
|
-
return toFiniteSet(set.base).elements.some((inner) =>
|
|
1744
|
+
return toFiniteSet(set.base).elements.some((inner) => isElementOfSet(element, inner));
|
|
1692
1745
|
case "SetOperation":
|
|
1693
1746
|
if (set.operator === "\\cup")
|
|
1694
|
-
return
|
|
1747
|
+
return isElementOfSet(element, set.left) || isElementOfSet(element, set.right);
|
|
1695
1748
|
return (
|
|
1696
|
-
|
|
1697
|
-
|
|
1749
|
+
isElementOfSet(element, set.left) &&
|
|
1750
|
+
isElementOfSet(element, set.right) === (set.operator === "\\cap")
|
|
1698
1751
|
);
|
|
1699
1752
|
case "SequenceSet": {
|
|
1700
1753
|
const elements = getSequenceElements(element);
|
|
@@ -1941,10 +1994,15 @@ const getElementContribution = (element: Value) =>
|
|
|
1941
1994
|
const getEntryContribution = (key: Value, value: Value) =>
|
|
1942
1995
|
finalizeHash((Math.imul(getValueHash(key), 0x27d4eb2d) + getValueHash(value)) | 0);
|
|
1943
1996
|
|
|
1997
|
+
const unenumeratedSets = { isHashed: false };
|
|
1998
|
+
|
|
1944
1999
|
const computeValueHash = (value: ObjectValue) => {
|
|
1945
2000
|
if (value.kind === "ModelValue") return finalizeHash(combineHash(hashString(value.name), 0x6d));
|
|
1946
2001
|
if (isSetValue(value)) {
|
|
1947
|
-
if (!isEnumerable(value))
|
|
2002
|
+
if (!isEnumerable(value)) {
|
|
2003
|
+
unenumeratedSets.isHashed = true;
|
|
2004
|
+
return hashString(getValueKey(value));
|
|
2005
|
+
}
|
|
1948
2006
|
const elements = toFiniteSet(value).elements;
|
|
1949
2007
|
let hash = SET_HASH_SEED;
|
|
1950
2008
|
for (let index = 0; index < elements.length; index++) {
|
|
@@ -1976,6 +2034,25 @@ export const getValueHash = (value: Value): number => {
|
|
|
1976
2034
|
return hash;
|
|
1977
2035
|
};
|
|
1978
2036
|
|
|
2037
|
+
const fingerprintLikeTlc = (value: Value): void => {
|
|
2038
|
+
if (typeof value !== "object" || value.kind === "ModelValue") return;
|
|
2039
|
+
if (isOverriddenSet(value)) {
|
|
2040
|
+
throw new TlaEvaluationError(
|
|
2041
|
+
`TLC has found a state in which the value of a variable contains ${formatValue(value)}`,
|
|
2042
|
+
);
|
|
2043
|
+
}
|
|
2044
|
+
if (isSetValue(value)) return toFiniteSet(value).elements.forEach(fingerprintLikeTlc);
|
|
2045
|
+
const functionValue = toFiniteFunction(value);
|
|
2046
|
+
functionValue.domain.forEach(fingerprintLikeTlc);
|
|
2047
|
+
functionValue.range.forEach(fingerprintLikeTlc);
|
|
2048
|
+
};
|
|
2049
|
+
|
|
2050
|
+
// TLC enumerates every set in a state to fingerprint it, so it fails on a set it can't enumerate.
|
|
2051
|
+
// A state holds one only if hashing it met one, so states go unchecked until then.
|
|
2052
|
+
export const assertFingerprintable = (state: Value[]) => {
|
|
2053
|
+
if (unenumeratedSets.isHashed) state.forEach(fingerprintLikeTlc);
|
|
2054
|
+
};
|
|
2055
|
+
|
|
1979
2056
|
const valueFingerprints = new WeakMap<ObjectValue, number>();
|
|
1980
2057
|
|
|
1981
2058
|
const finalizeFingerprint = (hash: number) => {
|
|
@@ -2002,7 +2079,10 @@ const getEntryFingerprint = (key: Value, value: Value) =>
|
|
|
2002
2079
|
const computeValueFingerprint = (value: ObjectValue) => {
|
|
2003
2080
|
if (value.kind === "ModelValue") return finalizeFingerprint(fingerprintString(value.name) ^ 0x3c);
|
|
2004
2081
|
if (isSetValue(value)) {
|
|
2005
|
-
if (!isEnumerable(value))
|
|
2082
|
+
if (!isEnumerable(value)) {
|
|
2083
|
+
unenumeratedSets.isHashed = true;
|
|
2084
|
+
return fingerprintString(getValueKey(value));
|
|
2085
|
+
}
|
|
2006
2086
|
const elements = toFiniteSet(value).elements;
|
|
2007
2087
|
let fingerprint = 0x1b873593;
|
|
2008
2088
|
for (let index = 0; index < elements.length; index++) {
|