tla-ts 0.0.8 → 0.0.9-dev.0dd11f695

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.
Files changed (119) hide show
  1. package/README.md +30 -0
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +15 -15
  5. package/dist/deciders.js +40 -40
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +98 -19
  8. package/dist/index.d.ts +98 -19
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +116 -36
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +1 -1
  20. package/dist/transfer.cjs +128 -106
  21. package/dist/transfer.js +128 -106
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +7 -21
  26. package/src/bags.ts +99 -99
  27. package/src/builtins.ts +44 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check.ts +605 -311
  30. package/src/cli/options.ts +12 -3
  31. package/src/cli/run.ts +2 -2
  32. package/src/cli/runtime-invariants.ts +135 -0
  33. package/src/cli/shared.ts +20 -9
  34. package/src/cli/tlc-arguments.ts +646 -0
  35. package/src/cli/tlc-usage.ts +362 -0
  36. package/src/cli/tlc.ts +571 -185
  37. package/src/cli/typescript.ts +26 -0
  38. package/src/cli/xml-exporter.ts +4 -10
  39. package/src/codegen.ts +190 -51
  40. package/src/config.ts +101 -68
  41. package/src/coverage.ts +855 -0
  42. package/src/dependencies.ts +340 -24
  43. package/src/dfid.ts +636 -0
  44. package/src/environment.ts +289 -109
  45. package/src/errors.ts +142 -45
  46. package/src/evaluate.ts +734 -980
  47. package/src/exit-codes.ts +37 -1
  48. package/src/index.ts +3 -0
  49. package/src/instances.ts +85 -9
  50. package/src/java-overrides.ts +2 -12
  51. package/src/json-reader.ts +456 -0
  52. package/src/json.ts +262 -139
  53. package/src/levels.ts +2 -3
  54. package/src/lexer.ts +150 -70
  55. package/src/liveness-checker.ts +10 -18
  56. package/src/liveness.ts +122 -64
  57. package/src/parallel-worker.ts +14 -10
  58. package/src/parallel.ts +41 -17
  59. package/src/parser.ts +436 -90
  60. package/src/pcal/ast.ts +7 -28
  61. package/src/pcal/parse.ts +11 -39
  62. package/src/pcal/tokenize.ts +4 -20
  63. package/src/pcal/translate.ts +10 -42
  64. package/src/pcal/validate.ts +3 -21
  65. package/src/pretty-print.ts +1 -2
  66. package/src/prover/smt/solver/solver.ts +31 -27
  67. package/src/randomization.ts +84 -35
  68. package/src/resolve.ts +18 -60
  69. package/src/runtime.ts +11 -4
  70. package/src/sany/builtins.ts +3 -4
  71. package/src/sany/generator.ts +128 -176
  72. package/src/sany/levels.ts +6 -30
  73. package/src/sany/lexer.ts +7 -23
  74. package/src/sany/linter.ts +31 -29
  75. package/src/sany/modules.ts +1 -5
  76. package/src/sany/nodes.ts +4 -28
  77. package/src/sany/parse-error.ts +44 -100
  78. package/src/sany/report.ts +25 -40
  79. package/src/sany/selectors.ts +8 -22
  80. package/src/sany/source.ts +8 -15
  81. package/src/sany/xml.ts +11 -42
  82. package/src/search.ts +550 -150
  83. package/src/simulate.ts +694 -0
  84. package/src/spec-description.ts +167 -49
  85. package/src/standard-definitions.ts +2 -3
  86. package/src/standard-modules.ts +264 -68
  87. package/src/state-graph.ts +51 -18
  88. package/src/subexpressions.ts +31 -5
  89. package/src/successors.ts +269 -85
  90. package/src/symmetry.ts +59 -147
  91. package/src/tableau.ts +1 -2
  92. package/src/tlc/fingerprint.ts +226 -151
  93. package/src/tlc/jar-data.ts +113 -6
  94. package/src/tlc/messages.ts +122 -37
  95. package/src/tlc/order.ts +56 -82
  96. package/src/tlc/random.ts +16 -16
  97. package/src/tlc/replay.ts +564 -212
  98. package/src/tlc/summary.ts +2 -4
  99. package/src/tlc/tlc-operators.ts +81 -36
  100. package/src/tlc/tlcext-operators.ts +138 -117
  101. package/src/tlc/trace-spec.ts +273 -0
  102. package/src/tlc/value-stream.ts +143 -0
  103. package/src/tlc/version.ts +2 -4
  104. package/src/typescript/actions.ts +494 -0
  105. package/src/typescript/collections.ts +615 -0
  106. package/src/typescript/compile.ts +70 -0
  107. package/src/typescript/context.ts +106 -0
  108. package/src/typescript/emit.ts +122 -0
  109. package/src/typescript/expressions.ts +508 -0
  110. package/src/typescript/formula.ts +160 -0
  111. package/src/typescript/functions.ts +295 -0
  112. package/src/typescript/model.ts +164 -0
  113. package/src/typescript/names.ts +116 -0
  114. package/src/typescript/program.ts +51 -0
  115. package/src/typescript/properties.ts +148 -0
  116. package/src/typescript/scope.ts +117 -0
  117. package/src/typescript/steps.ts +175 -0
  118. package/src/typescript/types.ts +178 -0
  119. package/src/values.ts +910 -318
package/src/symmetry.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Expression } from "./ast";
1
+ import type { BoundPattern, Expression } from "./ast";
2
2
  import type { Substitution } from "./config";
3
3
  import { TlaConfigError, TlaEvaluationError, UNEXPECTED_EXCEPTION } from "./errors";
4
4
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
@@ -13,41 +13,15 @@ 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
 
25
23
  type Images = Map<string, ModelValue>;
26
24
 
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
25
  const getPermutationKey = (permutation: Images) =>
52
26
  [...permutation]
53
27
  .map(([name, image]) => `${name}>${image.name}`)
@@ -84,8 +58,32 @@ const toImages = (value: Value) => {
84
58
  };
85
59
 
86
60
  // SyntaxTreeNode.getHumanReadableImage, which joins the tokens of an expression without spaces.
61
+ const getBoundImage = (pattern: BoundPattern, set: Expression | undefined) => {
62
+ const names = pattern.kind === "Name" ? pattern.name : `<<${pattern.names.join(",")}>>`;
63
+ return set ? `${names}\\in${getExpressionImage(set)}` : names;
64
+ };
65
+
87
66
  const getExpressionImage = (expression: Expression): string => {
88
67
  if (expression.kind === "Identifier") return expression.name;
68
+ if (expression.kind === "Number") return expression.text ?? String(expression.value);
69
+ if (expression.kind === "SetEnumeration") {
70
+ return `{${expression.elements.map(getExpressionImage).join(",")}}`;
71
+ }
72
+ if (expression.kind === "Choose") {
73
+ return `CHOOSE${getBoundImage(expression.pattern, expression.set)}:${getExpressionImage(expression.body)}`;
74
+ }
75
+ if (expression.kind === "Tuple")
76
+ return `<<${expression.elements.map(getExpressionImage).join(",")}>>`;
77
+ if (expression.kind === "Record") {
78
+ const fields = expression.fields.map(
79
+ (field) => `${field.name}|->${getExpressionImage(field.value)}`,
80
+ );
81
+ return `[${fields.join(",")}]`;
82
+ }
83
+ if (expression.kind === "FunctionConstruction") {
84
+ const bounds = expression.bounds.map((bound) => getBoundImage(bound.pattern, bound.set));
85
+ return `[${bounds.join(",")}|->${getExpressionImage(expression.body)}]`;
86
+ }
89
87
  if (expression.kind !== "Apply") return "";
90
88
  const args = expression.args.map(getExpressionImage);
91
89
  return /^[A-Za-z_]/.test(expression.operator)
@@ -104,7 +102,7 @@ export const getSymmetryGroup = (
104
102
  name: string,
105
103
  environment: Environment,
106
104
  substitutions: Substitution[],
107
- ): SymmetryGroup | undefined => {
105
+ ): Images[] | undefined => {
108
106
  const definition = environment.definitions.get(name);
109
107
  if (definition === undefined && !environment.constants.has(name)) {
110
108
  throw new TlaConfigError(
@@ -124,10 +122,11 @@ export const getSymmetryGroup = (
124
122
  if (!elements.every(isFunctionValue)) {
125
123
  throw createSymmetryError("The symmetry values must be function records.");
126
124
  }
127
- const permutations = elements.map(toImages);
128
125
  // Tool#getSymmetryPerms warns about each permuted set with fewer than two elements, by the name
129
126
  // the config substitutes for it. As many permuted sets as permutations means none has two.
130
- const permutedSets = definition.body.kind === "Apply" ? definition.body.args : [];
127
+ const body = definition.body;
128
+ const permutedSets =
129
+ body.kind === "Apply" ? body.args : body.kind === "SetEnumeration" ? body.elements : [];
131
130
  const configNames = new Map(
132
131
  substitutions
133
132
  .filter((substitution) => substitution.module === undefined)
@@ -150,7 +149,7 @@ export const getSymmetryGroup = (
150
149
  return undefined;
151
150
  }
152
151
  const group = new Map<string, Images>();
153
- permutations.forEach((permutation) => {
152
+ elements.map(toImages).forEach((permutation) => {
154
153
  if (permutation.size > 0) group.set(getPermutationKey(permutation), permutation);
155
154
  });
156
155
  const generators = [...group.values()];
@@ -187,29 +186,18 @@ export const getSymmetryGroup = (
187
186
  }),
188
187
  );
189
188
  if (group.size === 0) return undefined;
190
- const cacheLimit = Math.max(256, Math.floor(CACHE_LIMIT / group.size));
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
- };
189
+ return [...group.values()];
202
190
  };
203
191
 
204
- const permuteElements = (elements: Value[], permutation: Permutation) => {
192
+ const permuteElements = (elements: Value[], images: Images) => {
205
193
  for (let index = 0; index < elements.length; index++) {
206
194
  const element = elements[index];
207
- const permuted = permuteValue(element, permutation);
195
+ const permuted = permuteValue(element, images);
208
196
  if (permuted === element) continue;
209
197
  const result = elements.slice(0, index);
210
198
  result.push(permuted);
211
199
  for (let rest = index + 1; rest < elements.length; rest++) {
212
- result.push(permuteValue(elements[rest], permutation));
200
+ result.push(permuteValue(elements[rest], images));
213
201
  }
214
202
  return result;
215
203
  }
@@ -218,11 +206,11 @@ const permuteElements = (elements: Value[], permutation: Permutation) => {
218
206
 
219
207
  // A permutation maps distinct elements to distinct elements and keeps the elements it doesn't move
220
208
  // in order, so it sorts only the moved ones and merges them back.
221
- const permuteSortedElements = (elements: Value[], permutation: Permutation) => {
209
+ const permuteSortedElements = (elements: Value[], images: Images) => {
222
210
  const kept: Value[] = [];
223
211
  const moved: Value[] = [];
224
212
  for (const element of elements) {
225
- const permuted = permuteValue(element, permutation);
213
+ const permuted = permuteValue(element, images);
226
214
  if (permuted === element) kept.push(element);
227
215
  else moved.push(permuted);
228
216
  }
@@ -243,43 +231,20 @@ const permuteSortedElements = (elements: Value[], permutation: Permutation) => {
243
231
  return merged;
244
232
  };
245
233
 
246
- const getSize = (value: ObjectValue) =>
247
- value.kind === "Set"
248
- ? value.elements.length
249
- : value.kind === "Function"
250
- ? value.domain.length
251
- : 0;
252
-
253
- const permuteValue = (value: Value, permutation: Permutation): Value => {
234
+ // Value#permute: the value with each model value replaced by its image. A value the permutation
235
+ // doesn't move is returned itself, so that comparing it with itself stops at identity.
236
+ const permuteValue = (value: Value, images: Images): Value => {
254
237
  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
238
  const kind = value.kind;
239
+ if (kind === "ModelValue") return images.get(value.name) ?? value;
275
240
  if (kind === "Set") {
276
- const elements = permuteSortedElements(value.elements, permutation);
241
+ const elements = permuteSortedElements(value.elements, images);
277
242
  return elements ? createSortedSet(elements) : value;
278
243
  }
279
244
  if (kind === "Function" || (kind === "LazyFunction" && isEnumerable(value.domain))) {
280
245
  const functionValue = toFiniteFunction(value);
281
- const domain = permuteElements(functionValue.domain, permutation);
282
- const range = permuteElements(functionValue.range, permutation) ?? functionValue.range;
246
+ const domain = permuteElements(functionValue.domain, images);
247
+ const range = permuteElements(functionValue.range, images) ?? functionValue.range;
283
248
  if (domain) {
284
249
  const permuted = createFunction(
285
250
  domain.map((key, index): [Value, Value] => [key, range[index]]),
@@ -297,80 +262,27 @@ const computePermutedValue = (value: ObjectValue, permutation: Permutation) => {
297
262
  }
298
263
  if (kind === "Interval" || !isSetValue(value) || !isEnumerable(value)) return value;
299
264
  const set = toFiniteSet(value);
300
- const elements = permuteElements(set.elements, permutation);
265
+ const elements = permuteElements(set.elements, images);
301
266
  return elements ? createSet(elements) : set;
302
267
  };
303
268
 
304
- const getOrbit = (value: ObjectValue, group: SymmetryGroup) => {
305
- const cached = group.orbits.get(value);
306
- if (cached !== undefined) return cached;
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.
269
+ // TLC permutes the state under each permutation, variable by variable, and moves on to the next
270
+ // permutation as soon as a variable's image is larger than the smallest state's.
361
271
  export const getRepresentative = (
362
272
  state: Value[],
363
- group: SymmetryGroup,
273
+ permutations: Images[],
364
274
  order: number[],
365
275
  ): Value[] => {
366
- const minimum = group.permutations.reduce(
367
- (smallest, _, index) =>
368
- isSmallerImage(state, index + 1, smallest, group, order) ? index + 1 : smallest,
369
- 0,
370
- );
371
- return minimum === 0
372
- ? state
373
- : state.map((value) =>
374
- typeof value === "object" ? getImage(getOrbit(value, group), minimum, group) : value,
375
- );
276
+ let smallest = state;
277
+ for (const images of permutations) {
278
+ const permuted = state.slice();
279
+ let comparison = 0;
280
+ for (const index of order) {
281
+ permuted[index] = permuteValue(state[index], images);
282
+ if (comparison === 0) comparison = compareValues(permuted[index], smallest[index]);
283
+ if (comparison > 0) break;
284
+ }
285
+ if (comparison < 0) smallest = permuted;
286
+ }
287
+ return smallest;
376
288
  };
package/src/tableau.ts CHANGED
@@ -23,7 +23,7 @@ export interface LiveEnabled {
23
23
  interface LiveAction {
24
24
  kind: "Action";
25
25
  expression: Expression;
26
- unchanged: Expression | undefined;
26
+ subscript: Expression | undefined;
27
27
  scope: Scope | undefined;
28
28
  enabled?: LiveEnabled;
29
29
  }
@@ -164,7 +164,6 @@ const containsAction = (expression: LiveExpression): boolean => {
164
164
  const termKeys = new WeakMap<LiveExpression, string>();
165
165
  const atoms = { count: 0 };
166
166
 
167
- // A term's structure as a string in which each atom stands for itself, so equal terms share a key.
168
167
  export const getTermKey = (expression: LiveExpression): string => {
169
168
  const cached = termKeys.get(expression);
170
169
  if (cached !== undefined) return cached;