tla-ts 0.0.3-dev.3504663 → 0.0.3-dev.3539317

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/src/symmetry.ts CHANGED
@@ -31,6 +31,20 @@ interface Permutation {
31
31
  cacheLimit: number;
32
32
  }
33
33
 
34
+ // The images of one value under every permutation, the identity first, and how each image compares
35
+ // with the image of each permutation the search has held as the smallest.
36
+ interface Orbit {
37
+ images: Value[];
38
+ comparisons: (Int8Array | undefined)[];
39
+ }
40
+
41
+ interface SymmetryGroup {
42
+ permutations: Permutation[];
43
+ orbits: WeakMap<ObjectValue, Orbit>;
44
+ orbitCount: number;
45
+ orbitLimit: number;
46
+ }
47
+
34
48
  const getPermutationKey = (permutation: Images) =>
35
49
  [...permutation]
36
50
  .map(([name, image]) => `${name}>${image.name}`)
@@ -68,7 +82,10 @@ const toImages = (value: Value) => {
68
82
  return permutation;
69
83
  };
70
84
 
71
- export const getSymmetryGroup = (name: string, environment: Environment): Permutation[] => {
85
+ export const getSymmetryGroup = (
86
+ name: string,
87
+ environment: Environment,
88
+ ): SymmetryGroup | undefined => {
72
89
  const definition = environment.definitions.get(name);
73
90
  if (definition === undefined && !environment.constants.has(name)) {
74
91
  throw new TlaConfigError(
@@ -90,7 +107,7 @@ export const getSymmetryGroup = (name: string, environment: Environment): Permut
90
107
  const permutations = toFiniteSet(value).elements.map(toImages);
91
108
  const isDisabled =
92
109
  definition.body.kind === "Apply" && definition.body.args.length >= permutations.length;
93
- if (isDisabled) return [];
110
+ if (isDisabled) return undefined;
94
111
  const group = new Map<string, Images>();
95
112
  permutations.forEach((permutation) => {
96
113
  if (permutation.size > 0) group.set(getPermutationKey(permutation), permutation);
@@ -109,13 +126,19 @@ export const getSymmetryGroup = (name: string, environment: Environment): Permut
109
126
  );
110
127
  frontier.start = end;
111
128
  }
129
+ if (group.size === 0) return undefined;
112
130
  const cacheLimit = Math.max(256, Math.floor(CACHE_LIMIT / group.size));
113
- return [...group.values()].map((images) => ({
114
- images,
115
- cache: new WeakMap<ObjectValue, Value>(),
116
- cacheSize: 0,
117
- cacheLimit,
118
- }));
131
+ return {
132
+ permutations: [...group.values()].map((images) => ({
133
+ images,
134
+ cache: new WeakMap<ObjectValue, Value>(),
135
+ cacheSize: 0,
136
+ cacheLimit,
137
+ })),
138
+ orbits: new WeakMap(),
139
+ orbitCount: 0,
140
+ orbitLimit: cacheLimit,
141
+ };
119
142
  };
120
143
 
121
144
  const permuteElements = (elements: Value[], permutation: Permutation) => {
@@ -218,41 +241,76 @@ const computePermutedValue = (value: ObjectValue, permutation: Permutation) => {
218
241
  return elements ? createSet(elements) : set;
219
242
  };
220
243
 
221
- const permuteRemaining = (state: Value[], permuted: Value[], permutation: Permutation) => {
222
- for (let index = permuted.length; index < state.length; index++) {
223
- permuted.push(permuteValue(state[index], permutation));
244
+ const getOrbit = (value: ObjectValue, group: SymmetryGroup) => {
245
+ const cached = group.orbits.get(value);
246
+ if (cached !== undefined) return cached;
247
+ if (group.orbitCount >= group.orbitLimit) {
248
+ group.orbits = new WeakMap();
249
+ group.orbitCount = 0;
224
250
  }
225
- return permuted;
251
+ const orbit: Orbit = { images: [value], comparisons: [] };
252
+ group.orbits.set(value, orbit);
253
+ group.orbitCount++;
254
+ return orbit;
255
+ };
256
+
257
+ const getImage = (orbit: Orbit, permutation: number, group: SymmetryGroup) =>
258
+ (orbit.images[permutation] ??= permuteValue(
259
+ orbit.images[0],
260
+ group.permutations[permutation - 1],
261
+ ));
262
+
263
+ // Two images of a value compare the same way every time, and comparing them again changes nothing,
264
+ // so an orbit compares each pair once.
265
+ const compareImages = (
266
+ value: ObjectValue,
267
+ permutation: number,
268
+ minimum: number,
269
+ group: SymmetryGroup,
270
+ ) => {
271
+ const orbit = getOrbit(value, group);
272
+ const comparisons = (orbit.comparisons[minimum] ??= new Int8Array(group.permutations.length + 1));
273
+ if (comparisons[permutation] === 0) {
274
+ const comparison = compareValues(
275
+ getImage(orbit, permutation, group),
276
+ getImage(orbit, minimum, group),
277
+ );
278
+ comparisons[permutation] = Math.sign(comparison) + 2;
279
+ }
280
+ return comparisons[permutation] - 2;
226
281
  };
227
282
 
228
- const getSmallerPermutation = (
283
+ const isSmallerImage = (
229
284
  state: Value[],
230
- minimum: Value[],
231
- permutation: Permutation,
232
- order: number[] | undefined,
285
+ permutation: number,
286
+ minimum: number,
287
+ group: SymmetryGroup,
288
+ order: number[],
233
289
  ) => {
234
- const permuted: Value[] = [];
235
- for (let position = 0; position < state.length; position++) {
236
- const index = order ? order[position] : position;
237
- const value = permuteValue(state[index], permutation);
238
- const comparison = compareValues(value, minimum[index]);
239
- if (comparison > 0) return undefined;
240
- if (!order) permuted.push(value);
241
- if (comparison < 0) {
242
- return order
243
- ? state.map((item) => permuteValue(item, permutation))
244
- : permuteRemaining(state, permuted, permutation);
245
- }
290
+ for (const index of order) {
291
+ const value = state[index];
292
+ const comparison =
293
+ typeof value === "object" ? compareImages(value, permutation, minimum, group) : 0;
294
+ if (comparison !== 0) return comparison < 0;
246
295
  }
247
- return undefined;
296
+ return false;
248
297
  };
249
298
 
299
+ // TLC keeps the smallest image of the state in its variable order, replacing it only with a
300
+ // strictly smaller one.
250
301
  export const getRepresentative = (
251
302
  state: Value[],
252
- group: Permutation[],
253
- order?: number[],
254
- ): Value[] =>
255
- group.reduce(
256
- (minimum, permutation) => getSmallerPermutation(state, minimum, permutation, order) ?? minimum,
257
- state,
303
+ group: SymmetryGroup,
304
+ order: number[],
305
+ ): Value[] => {
306
+ const minimum = group.permutations.reduce(
307
+ (smallest, _, index) =>
308
+ isSmallerImage(state, index + 1, smallest, group, order) ? index + 1 : smallest,
309
+ 0,
258
310
  );
311
+ return minimum === 0
312
+ ? state
313
+ : state.map((value) =>
314
+ typeof value === "object" ? getImage(getOrbit(value, group), minimum, group) : value,
315
+ );
316
+ };