tla-ts 0.0.8 → 0.0.9-dev.2813db5
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/README.md +2 -0
- package/dist/index.d.cts +26 -9
- package/dist/index.d.ts +26 -9
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove.cjs +7 -7
- package/dist/prove.js +32 -32
- package/dist/run.cjs +7 -7
- package/dist/run.js +25 -25
- package/dist/transfer.cjs +30 -33
- package/dist/transfer.js +30 -33
- package/package.json +1 -1
- package/src/check.ts +33 -15
- package/src/cli/options.ts +1 -0
- package/src/cli/tlc.ts +9 -0
- package/src/codegen.ts +0 -5
- package/src/dependencies.ts +348 -24
- package/src/environment.ts +23 -11
- package/src/evaluate.ts +125 -704
- package/src/lexer.ts +140 -51
- package/src/liveness.ts +1 -15
- package/src/parallel-worker.ts +6 -3
- package/src/parallel.ts +15 -7
- package/src/sany/generator.ts +9 -7
- package/src/sany/linter.ts +30 -21
- package/src/search.ts +48 -32
- package/src/spec-description.ts +1 -1
- package/src/successors.ts +3 -9
- package/src/symmetry.ts +32 -143
- package/src/tlc/fingerprint.ts +69 -116
- package/src/tlc/order.ts +27 -48
- package/src/tlc/replay.ts +1 -1
- package/src/values.ts +115 -73
package/src/search.ts
CHANGED
|
@@ -14,7 +14,7 @@ import type {
|
|
|
14
14
|
Violation,
|
|
15
15
|
} from "./check";
|
|
16
16
|
import type { ConfigValue, ModelConfig } from "./config";
|
|
17
|
-
import { getReadVariables } from "./dependencies";
|
|
17
|
+
import { getReadVariables, isConstantLevel } from "./dependencies";
|
|
18
18
|
import {
|
|
19
19
|
isStackOverflow,
|
|
20
20
|
TlaConfigError,
|
|
@@ -27,10 +27,10 @@ import {
|
|
|
27
27
|
describeRange,
|
|
28
28
|
evaluate,
|
|
29
29
|
evaluateStartupDefinitions,
|
|
30
|
+
getStartupValue,
|
|
30
31
|
expectBoolean,
|
|
31
32
|
formatRange,
|
|
32
33
|
getDefinition,
|
|
33
|
-
isConstantLevel,
|
|
34
34
|
type Context,
|
|
35
35
|
type Environment,
|
|
36
36
|
type Scope,
|
|
@@ -49,7 +49,7 @@ import { prettyPrint } from "./pretty-print";
|
|
|
49
49
|
import { getPossibleCounts, type CheckerRuntime } from "./runtime";
|
|
50
50
|
import { getRepresentative, getSymmetryGroup } from "./symmetry";
|
|
51
51
|
import { MESSAGE_CODE, MISSING_INIT_174, withHeadline } from "./tlc/messages";
|
|
52
|
-
import {
|
|
52
|
+
import { getTlcFingerprint, getTlcFingerprints } from "./tlc/fingerprint";
|
|
53
53
|
import { formatValue, isUpdatedElementOf, type Value } from "./values";
|
|
54
54
|
|
|
55
55
|
interface NamedExpression {
|
|
@@ -563,6 +563,8 @@ interface Search {
|
|
|
563
563
|
// TLC drops a constraint that is a true constant formula.
|
|
564
564
|
isConstrained: boolean;
|
|
565
565
|
findViolatedInvariant: (state: Value[], parent?: Value[]) => StateCheck | undefined;
|
|
566
|
+
// Every invariant an initial state violates, which TLC reports with -continue.
|
|
567
|
+
getViolatedInvariants: (state: Value[]) => StateCheck[];
|
|
566
568
|
// The checks of a state that TLC evaluates when it replays the computation of the initial states
|
|
567
569
|
// or of the successors of a state, with an environment that records the call stack. The first
|
|
568
570
|
// returns the check that stops the replay, and the second false where TLC stops it.
|
|
@@ -760,9 +762,33 @@ export const createSearch = (
|
|
|
760
762
|
: [getViewValue(view.expression, representative)],
|
|
761
763
|
);
|
|
762
764
|
};
|
|
765
|
+
// With -continue, TLC goes on to the successors of a state that violates an invariant.
|
|
766
|
+
const violatingStates = new WeakSet<Value[]>();
|
|
767
|
+
const isViolated = (invariant: StateCheck, state: Value[], parent: Value[] | undefined) => {
|
|
768
|
+
// A parent that satisfies every invariant leaves only what the step changed to fail.
|
|
769
|
+
const checkedParent = parent && !violatingStates.has(parent) ? parent : undefined;
|
|
770
|
+
if (isUnchangedFor(invariant, state, checkedParent)) return false;
|
|
771
|
+
try {
|
|
772
|
+
const context = getContext(environment, state, undefined, invariant.scope);
|
|
773
|
+
const membership = invariant.membership;
|
|
774
|
+
return checkedParent && membership
|
|
775
|
+
? !isUpdatedElementOf(
|
|
776
|
+
state[membership.variable],
|
|
777
|
+
checkedParent[membership.variable],
|
|
778
|
+
evaluate(membership.set, context),
|
|
779
|
+
)
|
|
780
|
+
: !holdsAsConjunct(invariant, context);
|
|
781
|
+
} catch (error) {
|
|
782
|
+
// TLC reports a failure only on a state after the initial states as an invariant failure.
|
|
783
|
+
if (!parent) throw error;
|
|
784
|
+
throw withHeadline(
|
|
785
|
+
setExitCode(error, EXIT_STATUS.safetyEvaluationFailure),
|
|
786
|
+
MESSAGE_CODE.invariantEvaluationFailed,
|
|
787
|
+
`Evaluating invariant ${invariant.name} failed.`,
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
};
|
|
763
791
|
const isFingerprintedLikeTlc = hasWideString(config, environment);
|
|
764
|
-
// One fingerprinter for every level keeps the values it cached for the states of earlier levels.
|
|
765
|
-
let storedFingerprinter: ReturnType<typeof createTlcFingerprinter> | undefined;
|
|
766
792
|
runtime.getFingerprint = getTlcStateFingerprint;
|
|
767
793
|
const findUnwitnessedPossible = () => {
|
|
768
794
|
const unwitnessed = possible.find((item) => !runtime.possibleCounts.get(item.name));
|
|
@@ -786,8 +812,7 @@ export const createSearch = (
|
|
|
786
812
|
if (view !== undefined || isFingerprintedLikeTlc) {
|
|
787
813
|
return BigInt64Array.from(states, getTlcStateFingerprint);
|
|
788
814
|
}
|
|
789
|
-
|
|
790
|
-
return storedFingerprinter(fingerprintStates);
|
|
815
|
+
return getTlcFingerprints(fingerprintStates, getTlcOrder());
|
|
791
816
|
},
|
|
792
817
|
getFingerprintState: (state) => {
|
|
793
818
|
if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
|
|
@@ -798,30 +823,21 @@ export const createSearch = (
|
|
|
798
823
|
: getRepresentative(state, symmetryGroup, getTlcOrder());
|
|
799
824
|
return view === undefined ? representative : [getViewValue(view.expression, representative)];
|
|
800
825
|
},
|
|
801
|
-
findViolatedInvariant: (state, parent) =>
|
|
802
|
-
invariants.find((invariant) =>
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
// TLC reports a failure only on a state after the initial states as an invariant failure.
|
|
817
|
-
if (!parent) throw error;
|
|
818
|
-
throw withHeadline(
|
|
819
|
-
setExitCode(error, EXIT_STATUS.safetyEvaluationFailure),
|
|
820
|
-
MESSAGE_CODE.invariantEvaluationFailed,
|
|
821
|
-
`Evaluating invariant ${invariant.name} failed.`,
|
|
822
|
-
);
|
|
823
|
-
}
|
|
824
|
-
}),
|
|
826
|
+
findViolatedInvariant: (state, parent) => {
|
|
827
|
+
const violated = invariants.find((invariant) => isViolated(invariant, state, parent));
|
|
828
|
+
if (violated) violatingStates.add(state);
|
|
829
|
+
return violated;
|
|
830
|
+
},
|
|
831
|
+
getViolatedInvariants: (state) => {
|
|
832
|
+
const violated = invariantPredicates.filter((predicate) =>
|
|
833
|
+
invariants.some(
|
|
834
|
+
(invariant) =>
|
|
835
|
+
invariant.predicate === predicate.predicate && isViolated(invariant, state, undefined),
|
|
836
|
+
),
|
|
837
|
+
);
|
|
838
|
+
if (violated.length > 0) violatingStates.add(state);
|
|
839
|
+
return violated;
|
|
840
|
+
},
|
|
825
841
|
checkReplayedInitialState: (state, tracing, addFingerprint) => {
|
|
826
842
|
const holdsIn = (expression: Expression, scope: Scope | undefined) =>
|
|
827
843
|
holdsLikeTlc(expression, getContext(tracing, state, undefined, scope));
|
|
@@ -925,7 +941,7 @@ export const createSearch = (
|
|
|
925
941
|
// a postcondition before it checks any of them.
|
|
926
942
|
const constant = config.postconditions.find((name) => {
|
|
927
943
|
const definition = getDefinition(name, environment);
|
|
928
|
-
return definition !== undefined &&
|
|
944
|
+
return definition !== undefined && getStartupValue(definition, environment) !== undefined;
|
|
929
945
|
});
|
|
930
946
|
if (constant !== undefined) {
|
|
931
947
|
if (isAfterViolation) return undefined;
|
package/src/spec-description.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isConstantLevel } from "./dependencies";
|
|
1
2
|
import {
|
|
2
3
|
isLocallyResolved,
|
|
3
4
|
type Definition,
|
|
@@ -14,7 +15,6 @@ import {
|
|
|
14
15
|
evaluate,
|
|
15
16
|
forEachBinding,
|
|
16
17
|
getDefinition,
|
|
17
|
-
isConstantLevel,
|
|
18
18
|
lookupScope,
|
|
19
19
|
resolveGlobalOperator,
|
|
20
20
|
type Closure,
|
package/src/successors.ts
CHANGED
|
@@ -1056,15 +1056,9 @@ const compileAction = (
|
|
|
1056
1056
|
}
|
|
1057
1057
|
};
|
|
1058
1058
|
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
const cached = emptyStates.get(environment);
|
|
1063
|
-
if (cached) return cached;
|
|
1064
|
-
const state = environment.variables.map((): Value | undefined => undefined);
|
|
1065
|
-
emptyStates.set(environment, state);
|
|
1066
|
-
return state;
|
|
1067
|
-
};
|
|
1059
|
+
// A partial state that assigns no variable, which a run fills in as it assigns them.
|
|
1060
|
+
const getEmptyState = (environment: Environment): State =>
|
|
1061
|
+
environment.variables.map((): Value | undefined => undefined);
|
|
1068
1062
|
|
|
1069
1063
|
const isComplete = (partial: State): partial is Value[] => {
|
|
1070
1064
|
for (let index = 0; index < partial.length; index++) {
|
package/src/symmetry.ts
CHANGED
|
@@ -13,41 +13,16 @@ import {
|
|
|
13
13
|
isEnumerable,
|
|
14
14
|
isFunctionValue,
|
|
15
15
|
isModelValue,
|
|
16
|
-
isSameValue,
|
|
17
16
|
isSetValue,
|
|
18
17
|
toFiniteFunction,
|
|
19
18
|
toFiniteSet,
|
|
20
19
|
type ModelValue,
|
|
21
|
-
type ObjectValue,
|
|
22
20
|
type Value,
|
|
23
21
|
} from "./values";
|
|
24
22
|
|
|
23
|
+
// The image of each model value a permutation moves, by name.
|
|
25
24
|
type Images = Map<string, ModelValue>;
|
|
26
25
|
|
|
27
|
-
const CACHE_LIMIT = 1 << 22;
|
|
28
|
-
const CACHED_SIZE_LIMIT = 64;
|
|
29
|
-
|
|
30
|
-
interface Permutation {
|
|
31
|
-
images: Images;
|
|
32
|
-
cache: WeakMap<ObjectValue, Value>;
|
|
33
|
-
cacheSize: number;
|
|
34
|
-
cacheLimit: number;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// The images of one value under every permutation, the identity first, and how each image compares
|
|
38
|
-
// with the image of each permutation the search has held as the smallest.
|
|
39
|
-
interface Orbit {
|
|
40
|
-
images: Value[];
|
|
41
|
-
comparisons: (Int8Array | undefined)[];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
interface SymmetryGroup {
|
|
45
|
-
permutations: Permutation[];
|
|
46
|
-
orbits: WeakMap<ObjectValue, Orbit>;
|
|
47
|
-
orbitCount: number;
|
|
48
|
-
orbitLimit: number;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
26
|
const getPermutationKey = (permutation: Images) =>
|
|
52
27
|
[...permutation]
|
|
53
28
|
.map(([name, image]) => `${name}>${image.name}`)
|
|
@@ -104,7 +79,7 @@ export const getSymmetryGroup = (
|
|
|
104
79
|
name: string,
|
|
105
80
|
environment: Environment,
|
|
106
81
|
substitutions: Substitution[],
|
|
107
|
-
):
|
|
82
|
+
): Images[] | undefined => {
|
|
108
83
|
const definition = environment.definitions.get(name);
|
|
109
84
|
if (definition === undefined && !environment.constants.has(name)) {
|
|
110
85
|
throw new TlaConfigError(
|
|
@@ -187,29 +162,18 @@ export const getSymmetryGroup = (
|
|
|
187
162
|
}),
|
|
188
163
|
);
|
|
189
164
|
if (group.size === 0) return undefined;
|
|
190
|
-
|
|
191
|
-
return {
|
|
192
|
-
permutations: [...group.values()].map((images) => ({
|
|
193
|
-
images,
|
|
194
|
-
cache: new WeakMap<ObjectValue, Value>(),
|
|
195
|
-
cacheSize: 0,
|
|
196
|
-
cacheLimit,
|
|
197
|
-
})),
|
|
198
|
-
orbits: new WeakMap(),
|
|
199
|
-
orbitCount: 0,
|
|
200
|
-
orbitLimit: cacheLimit,
|
|
201
|
-
};
|
|
165
|
+
return [...group.values()];
|
|
202
166
|
};
|
|
203
167
|
|
|
204
|
-
const permuteElements = (elements: Value[],
|
|
168
|
+
const permuteElements = (elements: Value[], images: Images) => {
|
|
205
169
|
for (let index = 0; index < elements.length; index++) {
|
|
206
170
|
const element = elements[index];
|
|
207
|
-
const permuted = permuteValue(element,
|
|
171
|
+
const permuted = permuteValue(element, images);
|
|
208
172
|
if (permuted === element) continue;
|
|
209
173
|
const result = elements.slice(0, index);
|
|
210
174
|
result.push(permuted);
|
|
211
175
|
for (let rest = index + 1; rest < elements.length; rest++) {
|
|
212
|
-
result.push(permuteValue(elements[rest],
|
|
176
|
+
result.push(permuteValue(elements[rest], images));
|
|
213
177
|
}
|
|
214
178
|
return result;
|
|
215
179
|
}
|
|
@@ -218,11 +182,11 @@ const permuteElements = (elements: Value[], permutation: Permutation) => {
|
|
|
218
182
|
|
|
219
183
|
// A permutation maps distinct elements to distinct elements and keeps the elements it doesn't move
|
|
220
184
|
// in order, so it sorts only the moved ones and merges them back.
|
|
221
|
-
const permuteSortedElements = (elements: Value[],
|
|
185
|
+
const permuteSortedElements = (elements: Value[], images: Images) => {
|
|
222
186
|
const kept: Value[] = [];
|
|
223
187
|
const moved: Value[] = [];
|
|
224
188
|
for (const element of elements) {
|
|
225
|
-
const permuted = permuteValue(element,
|
|
189
|
+
const permuted = permuteValue(element, images);
|
|
226
190
|
if (permuted === element) kept.push(element);
|
|
227
191
|
else moved.push(permuted);
|
|
228
192
|
}
|
|
@@ -243,43 +207,20 @@ const permuteSortedElements = (elements: Value[], permutation: Permutation) => {
|
|
|
243
207
|
return merged;
|
|
244
208
|
};
|
|
245
209
|
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
: value.kind === "Function"
|
|
250
|
-
? value.domain.length
|
|
251
|
-
: 0;
|
|
252
|
-
|
|
253
|
-
const permuteValue = (value: Value, permutation: Permutation): Value => {
|
|
210
|
+
// Value#permute: the value with each model value replaced by its image. A value the permutation
|
|
211
|
+
// doesn't move is returned itself, so that comparing it with itself stops at identity.
|
|
212
|
+
const permuteValue = (value: Value, images: Images): Value => {
|
|
254
213
|
if (typeof value !== "object") return value;
|
|
255
|
-
if (value.kind === "ModelValue") return permutation.images.get(value.name) ?? value;
|
|
256
|
-
const size = getSize(value);
|
|
257
|
-
if (size === 0 || size > CACHED_SIZE_LIMIT) return computePermutedValue(value, permutation);
|
|
258
|
-
const cached = permutation.cache.get(value);
|
|
259
|
-
if (cached !== undefined) return cached;
|
|
260
|
-
const computed = computePermutedValue(value, permutation);
|
|
261
|
-
// A permutation often maps a value to an equal one. Keeping the original lets the comparison
|
|
262
|
-
// with the smallest state so far stop at identity.
|
|
263
|
-
const permuted = computed !== value && isSameValue(computed, value) ? value : computed;
|
|
264
|
-
if (permutation.cacheSize >= permutation.cacheLimit) {
|
|
265
|
-
permutation.cache = new WeakMap();
|
|
266
|
-
permutation.cacheSize = 0;
|
|
267
|
-
}
|
|
268
|
-
permutation.cache.set(value, permuted);
|
|
269
|
-
permutation.cacheSize++;
|
|
270
|
-
return permuted;
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
const computePermutedValue = (value: ObjectValue, permutation: Permutation) => {
|
|
274
214
|
const kind = value.kind;
|
|
215
|
+
if (kind === "ModelValue") return images.get(value.name) ?? value;
|
|
275
216
|
if (kind === "Set") {
|
|
276
|
-
const elements = permuteSortedElements(value.elements,
|
|
217
|
+
const elements = permuteSortedElements(value.elements, images);
|
|
277
218
|
return elements ? createSortedSet(elements) : value;
|
|
278
219
|
}
|
|
279
220
|
if (kind === "Function" || (kind === "LazyFunction" && isEnumerable(value.domain))) {
|
|
280
221
|
const functionValue = toFiniteFunction(value);
|
|
281
|
-
const domain = permuteElements(functionValue.domain,
|
|
282
|
-
const range = permuteElements(functionValue.range,
|
|
222
|
+
const domain = permuteElements(functionValue.domain, images);
|
|
223
|
+
const range = permuteElements(functionValue.range, images) ?? functionValue.range;
|
|
283
224
|
if (domain) {
|
|
284
225
|
const permuted = createFunction(
|
|
285
226
|
domain.map((key, index): [Value, Value] => [key, range[index]]),
|
|
@@ -297,80 +238,28 @@ const computePermutedValue = (value: ObjectValue, permutation: Permutation) => {
|
|
|
297
238
|
}
|
|
298
239
|
if (kind === "Interval" || !isSetValue(value) || !isEnumerable(value)) return value;
|
|
299
240
|
const set = toFiniteSet(value);
|
|
300
|
-
const elements = permuteElements(set.elements,
|
|
241
|
+
const elements = permuteElements(set.elements, images);
|
|
301
242
|
return elements ? createSet(elements) : set;
|
|
302
243
|
};
|
|
303
244
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
if (group.orbitCount >= group.orbitLimit) {
|
|
308
|
-
group.orbits = new WeakMap();
|
|
309
|
-
group.orbitCount = 0;
|
|
310
|
-
}
|
|
311
|
-
const orbit: Orbit = { images: [value], comparisons: [] };
|
|
312
|
-
group.orbits.set(value, orbit);
|
|
313
|
-
group.orbitCount++;
|
|
314
|
-
return orbit;
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
const getImage = (orbit: Orbit, permutation: number, group: SymmetryGroup) =>
|
|
318
|
-
(orbit.images[permutation] ??= permuteValue(
|
|
319
|
-
orbit.images[0],
|
|
320
|
-
group.permutations[permutation - 1],
|
|
321
|
-
));
|
|
322
|
-
|
|
323
|
-
// Two images of a value compare the same way every time, and comparing them again changes nothing,
|
|
324
|
-
// so an orbit compares each pair once.
|
|
325
|
-
const compareImages = (
|
|
326
|
-
value: ObjectValue,
|
|
327
|
-
permutation: number,
|
|
328
|
-
minimum: number,
|
|
329
|
-
group: SymmetryGroup,
|
|
330
|
-
) => {
|
|
331
|
-
const orbit = getOrbit(value, group);
|
|
332
|
-
const comparisons = (orbit.comparisons[minimum] ??= new Int8Array(group.permutations.length + 1));
|
|
333
|
-
if (comparisons[permutation] === 0) {
|
|
334
|
-
const comparison = compareValues(
|
|
335
|
-
getImage(orbit, permutation, group),
|
|
336
|
-
getImage(orbit, minimum, group),
|
|
337
|
-
);
|
|
338
|
-
comparisons[permutation] = Math.sign(comparison) + 2;
|
|
339
|
-
}
|
|
340
|
-
return comparisons[permutation] - 2;
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
const isSmallerImage = (
|
|
344
|
-
state: Value[],
|
|
345
|
-
permutation: number,
|
|
346
|
-
minimum: number,
|
|
347
|
-
group: SymmetryGroup,
|
|
348
|
-
order: number[],
|
|
349
|
-
) => {
|
|
350
|
-
for (const index of order) {
|
|
351
|
-
const value = state[index];
|
|
352
|
-
const comparison =
|
|
353
|
-
typeof value === "object" ? compareImages(value, permutation, minimum, group) : 0;
|
|
354
|
-
if (comparison !== 0) return comparison < 0;
|
|
355
|
-
}
|
|
356
|
-
return false;
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
// TLC keeps the smallest image of the state in its variable order, replacing it only with a
|
|
360
|
-
// strictly smaller one.
|
|
245
|
+
// TLCStateMut#fingerPrint: the smallest image of the state under the group in TLC's variable
|
|
246
|
+
// order. TLC permutes the state under each permutation, variable by variable, and moves on to the
|
|
247
|
+
// next permutation as soon as a variable's image is larger than the smallest state's.
|
|
361
248
|
export const getRepresentative = (
|
|
362
249
|
state: Value[],
|
|
363
|
-
|
|
250
|
+
permutations: Images[],
|
|
364
251
|
order: number[],
|
|
365
252
|
): Value[] => {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
0
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
253
|
+
let smallest = state;
|
|
254
|
+
for (const images of permutations) {
|
|
255
|
+
const permuted = state.slice();
|
|
256
|
+
let comparison = 0;
|
|
257
|
+
for (const index of order) {
|
|
258
|
+
permuted[index] = permuteValue(state[index], images);
|
|
259
|
+
if (comparison === 0) comparison = compareValues(permuted[index], smallest[index]);
|
|
260
|
+
if (comparison > 0) break;
|
|
261
|
+
}
|
|
262
|
+
if (comparison < 0) smallest = permuted;
|
|
263
|
+
}
|
|
264
|
+
return smallest;
|
|
376
265
|
};
|
package/src/tlc/fingerprint.ts
CHANGED
|
@@ -43,8 +43,48 @@ const extendByte = (byte: number) => {
|
|
|
43
43
|
high = (high >>> 8) ^ BYTE_MOD_HIGH[index];
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
// Extending is linear over GF(2), so extending by four bytes at once shifts the fingerprint by a
|
|
47
|
+
// word and adds, for each of the four, the fingerprint from zero of that byte followed by the zero
|
|
48
|
+
// bytes that come after it: SLICE_HIGH[k] and SLICE_LOW[k] hold them for k zero bytes after.
|
|
49
|
+
const createSliceTables = () => {
|
|
50
|
+
const highs = [BYTE_MOD_HIGH];
|
|
51
|
+
const lows = [BYTE_MOD_LOW];
|
|
52
|
+
for (let zeros = 1; zeros < 4; zeros++) {
|
|
53
|
+
const previousHigh = highs[zeros - 1];
|
|
54
|
+
const previousLow = lows[zeros - 1];
|
|
55
|
+
const sliceHigh = new Int32Array(256);
|
|
56
|
+
const sliceLow = new Int32Array(256);
|
|
57
|
+
for (let byte = 0; byte < 256; byte++) {
|
|
58
|
+
const index = previousLow[byte] & 0xff;
|
|
59
|
+
sliceLow[byte] =
|
|
60
|
+
((previousLow[byte] >>> 8) | (previousHigh[byte] << 24)) ^ BYTE_MOD_LOW[index];
|
|
61
|
+
sliceHigh[byte] = (previousHigh[byte] >>> 8) ^ BYTE_MOD_HIGH[index];
|
|
62
|
+
}
|
|
63
|
+
highs.push(sliceHigh);
|
|
64
|
+
lows.push(sliceLow);
|
|
65
|
+
}
|
|
66
|
+
return { highs, lows };
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const SLICES = createSliceTables();
|
|
70
|
+
const SLICE_HIGH_0 = SLICES.highs[0];
|
|
71
|
+
const SLICE_HIGH_1 = SLICES.highs[1];
|
|
72
|
+
const SLICE_HIGH_2 = SLICES.highs[2];
|
|
73
|
+
const SLICE_HIGH_3 = SLICES.highs[3];
|
|
74
|
+
const SLICE_LOW_0 = SLICES.lows[0];
|
|
75
|
+
const SLICE_LOW_1 = SLICES.lows[1];
|
|
76
|
+
const SLICE_LOW_2 = SLICES.lows[2];
|
|
77
|
+
const SLICE_LOW_3 = SLICES.lows[3];
|
|
78
|
+
|
|
79
|
+
// Extends the fingerprint by the four bytes of a word, lowest first.
|
|
80
|
+
const extendInt = (word: number) => {
|
|
81
|
+
const mixed = low ^ word;
|
|
82
|
+
const first = mixed & 0xff;
|
|
83
|
+
const second = (mixed >>> 8) & 0xff;
|
|
84
|
+
const third = (mixed >>> 16) & 0xff;
|
|
85
|
+
const fourth = mixed >>> 24;
|
|
86
|
+
low = high ^ SLICE_LOW_3[first] ^ SLICE_LOW_2[second] ^ SLICE_LOW_1[third] ^ SLICE_LOW_0[fourth];
|
|
87
|
+
high = SLICE_HIGH_3[first] ^ SLICE_HIGH_2[second] ^ SLICE_HIGH_1[third] ^ SLICE_HIGH_0[fourth];
|
|
48
88
|
};
|
|
49
89
|
|
|
50
90
|
const BOOL_VALUE = 0;
|
|
@@ -71,9 +111,16 @@ const extendScalar = (value: boolean | number | string | ModelValue) => {
|
|
|
71
111
|
if (typeof value === "string") {
|
|
72
112
|
extendByte(STRING_VALUE);
|
|
73
113
|
extendInt(value.length);
|
|
74
|
-
|
|
75
|
-
|
|
114
|
+
const whole = value.length & ~3;
|
|
115
|
+
for (let index = 0; index < whole; index += 4) {
|
|
116
|
+
extendInt(
|
|
117
|
+
(value.charCodeAt(index) & 0xff) |
|
|
118
|
+
((value.charCodeAt(index + 1) & 0xff) << 8) |
|
|
119
|
+
((value.charCodeAt(index + 2) & 0xff) << 16) |
|
|
120
|
+
((value.charCodeAt(index + 3) & 0xff) << 24),
|
|
121
|
+
);
|
|
76
122
|
}
|
|
123
|
+
for (let index = whole; index < value.length; index++) extendByte(value.charCodeAt(index));
|
|
77
124
|
return 5 + value.length;
|
|
78
125
|
}
|
|
79
126
|
extendByte(MODEL_VALUE);
|
|
@@ -114,116 +161,22 @@ export const getTlcFingerprint = (state: Value[]) => {
|
|
|
114
161
|
return fingerprint;
|
|
115
162
|
};
|
|
116
163
|
|
|
117
|
-
//
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
// The functions and sets a fingerprinter caches before it starts over, which bounds the values it
|
|
137
|
-
// keeps alive.
|
|
138
|
-
const CACHED_VALUE_LIMIT = 1 << 22;
|
|
139
|
-
|
|
140
|
-
// Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
|
|
141
|
-
// XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
|
|
142
|
-
// The fingerprinter caches each distinct function or set it meets, nested ones included, across
|
|
143
|
-
// every batch of states it fingerprints, as states share most of their values, even states of
|
|
144
|
-
// different levels. It reads a state's variables in `order`.
|
|
145
|
-
export const createTlcFingerprinter = (order: number[], limit = CACHED_VALUE_LIMIT) => {
|
|
146
|
-
const shiftTables = new Map<number, Int32Array>();
|
|
147
|
-
const ids = new Map<Value, number>();
|
|
148
|
-
// The byte count and fingerprint from zero of each cached value, three numbers per id, and the
|
|
149
|
-
// shift table for its byte count.
|
|
150
|
-
const contributions: number[] = [];
|
|
151
|
-
const contributionShiftTables: Int32Array[] = [];
|
|
152
|
-
const getShiftTable = (length: number) => {
|
|
153
|
-
const cached = shiftTables.get(length);
|
|
154
|
-
if (cached) return cached;
|
|
155
|
-
const table = createShiftTable(length);
|
|
156
|
-
shiftTables.set(length, table);
|
|
157
|
-
return table;
|
|
158
|
-
};
|
|
159
|
-
// Extends the fingerprint by a value's bytes and returns how many there are.
|
|
160
|
-
const extendElement = (value: Value): number => {
|
|
161
|
-
if (typeof value !== "object" || isModelValue(value)) return extendScalar(value);
|
|
162
|
-
const id = getId(value);
|
|
163
|
-
const table = contributionShiftTables[id];
|
|
164
|
-
const shiftedHigh = high;
|
|
165
|
-
const shiftedLow = low;
|
|
166
|
-
high = contributions[3 * id + 1];
|
|
167
|
-
low = contributions[3 * id + 2];
|
|
168
|
-
for (let position = 0; position < 8; position++) {
|
|
169
|
-
const word = position < 4 ? shiftedLow : shiftedHigh;
|
|
170
|
-
const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
|
|
171
|
-
high ^= table[entry];
|
|
172
|
-
low ^= table[entry + 1];
|
|
173
|
-
}
|
|
174
|
-
return contributions[3 * id];
|
|
175
|
-
};
|
|
176
|
-
const getId = (value: Value) => {
|
|
177
|
-
const cached = ids.get(value);
|
|
178
|
-
if (cached !== undefined) return cached;
|
|
179
|
-
const outerHigh = high;
|
|
180
|
-
const outerLow = low;
|
|
181
|
-
high = 0;
|
|
182
|
-
low = 0;
|
|
183
|
-
const length = { value: 5 };
|
|
184
|
-
if (isFunctionValue(value)) {
|
|
185
|
-
const functionValue = toFiniteFunction(value);
|
|
186
|
-
extendByte(FCN_RCD_VALUE);
|
|
187
|
-
extendInt(functionValue.domain.length);
|
|
188
|
-
for (let index = 0; index < functionValue.domain.length; index++) {
|
|
189
|
-
length.value += extendElement(functionValue.domain[index]);
|
|
190
|
-
length.value += extendElement(functionValue.range[index]);
|
|
191
|
-
}
|
|
192
|
-
} else {
|
|
193
|
-
const elements = toFiniteSet(value).elements;
|
|
194
|
-
extendByte(SET_ENUM_VALUE);
|
|
195
|
-
extendInt(elements.length);
|
|
196
|
-
for (const element of elements) length.value += extendElement(element);
|
|
197
|
-
}
|
|
198
|
-
const id = contributionShiftTables.length;
|
|
199
|
-
ids.set(value, id);
|
|
200
|
-
contributions.push(length.value, high, low);
|
|
201
|
-
contributionShiftTables.push(getShiftTable(length.value));
|
|
202
|
-
high = outerHigh;
|
|
203
|
-
low = outerLow;
|
|
204
|
-
return id;
|
|
205
|
-
};
|
|
206
|
-
return (states: Value[][]) => {
|
|
207
|
-
const fingerprints = new BigInt64Array(states.length);
|
|
208
|
-
// Each fingerprint's low and high words, as a BigInt64Array is little-endian.
|
|
209
|
-
const words = new Int32Array(fingerprints.buffer);
|
|
210
|
-
const outerHigh = high;
|
|
211
|
-
const outerLow = low;
|
|
212
|
-
states.forEach((state, position) => {
|
|
213
|
-
if (ids.size >= limit) {
|
|
214
|
-
shiftTables.clear();
|
|
215
|
-
ids.clear();
|
|
216
|
-
contributions.length = 0;
|
|
217
|
-
contributionShiftTables.length = 0;
|
|
218
|
-
}
|
|
219
|
-
high = INITIAL_HIGH;
|
|
220
|
-
low = INITIAL_LOW;
|
|
221
|
-
for (const index of order) extendElement(state[index]);
|
|
222
|
-
words[2 * position] = low;
|
|
223
|
-
words[2 * position + 1] = high;
|
|
224
|
-
});
|
|
225
|
-
high = outerHigh;
|
|
226
|
-
low = outerLow;
|
|
227
|
-
return fingerprints;
|
|
228
|
-
};
|
|
164
|
+
// TLCStateMut#fingerPrint of each state's variables in TLC's variable order, as the fingerprint
|
|
165
|
+
// store reads them.
|
|
166
|
+
export const getTlcFingerprints = (states: Value[][], order: number[]) => {
|
|
167
|
+
const fingerprints = new BigInt64Array(states.length);
|
|
168
|
+
// Each fingerprint's low and high words, as a BigInt64Array is little-endian.
|
|
169
|
+
const words = new Int32Array(fingerprints.buffer);
|
|
170
|
+
const outerHigh = high;
|
|
171
|
+
const outerLow = low;
|
|
172
|
+
states.forEach((state, position) => {
|
|
173
|
+
high = INITIAL_HIGH;
|
|
174
|
+
low = INITIAL_LOW;
|
|
175
|
+
for (const index of order) extendValue(state[index]);
|
|
176
|
+
words[2 * position] = low;
|
|
177
|
+
words[2 * position + 1] = high;
|
|
178
|
+
});
|
|
179
|
+
high = outerHigh;
|
|
180
|
+
low = outerLow;
|
|
181
|
+
return fingerprints;
|
|
229
182
|
};
|