tla-ts 0.0.3 → 0.0.4-dev.66582d4

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 (86) hide show
  1. package/README.md +32 -12
  2. package/dist/cli.cjs +1 -1
  3. package/dist/cli.js +1 -1
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +160 -8
  6. package/dist/index.d.ts +160 -8
  7. package/dist/index.js +1 -1
  8. package/dist/parallel-worker.cjs +2 -2
  9. package/dist/parallel-worker.js +2 -2
  10. package/dist/parallel.cjs +1 -1
  11. package/dist/parallel.js +2 -2
  12. package/dist/run-cli.cjs +45 -18
  13. package/dist/run-cli.js +45 -18
  14. package/dist/transfer.cjs +1393 -18
  15. package/dist/transfer.js +1393 -18
  16. package/package.json +5 -1
  17. package/src/ast.ts +147 -6
  18. package/src/bags.ts +4 -7
  19. package/src/builtins.ts +29 -11
  20. package/src/bundled-modules.ts +3 -3
  21. package/src/canonical.ts +4 -3
  22. package/src/check.ts +1539 -235
  23. package/src/cli-options.ts +11 -5
  24. package/src/cli.ts +8 -1
  25. package/src/codegen.ts +77 -37
  26. package/src/config.ts +254 -181
  27. package/src/dependencies.ts +2 -2
  28. package/src/environment.ts +540 -140
  29. package/src/errors.ts +224 -3
  30. package/src/evaluate.ts +796 -246
  31. package/src/exit-codes.ts +22 -43
  32. package/src/fingerprints.ts +46 -11
  33. package/src/index.ts +1 -0
  34. package/src/instances.ts +19 -16
  35. package/src/java-overrides.ts +104 -25
  36. package/src/levels.ts +191 -45
  37. package/src/lexer.ts +127 -30
  38. package/src/live-check.ts +867 -0
  39. package/src/liveness.ts +218 -572
  40. package/src/obligation-expression.ts +2 -2
  41. package/src/obligation-source.ts +2 -0
  42. package/src/parallel-worker.ts +174 -213
  43. package/src/parallel.ts +131 -39
  44. package/src/parser.ts +456 -147
  45. package/src/pcal-ast.ts +770 -0
  46. package/src/pcal-parse.ts +1523 -0
  47. package/src/pcal-tokenize.ts +516 -0
  48. package/src/pcal-translate.ts +1230 -0
  49. package/src/pcal-validate.ts +454 -0
  50. package/src/randomization.ts +24 -7
  51. package/src/resolve.ts +1481 -426
  52. package/src/run-cli.ts +1212 -143
  53. package/src/runtime.ts +19 -1
  54. package/src/sany-builtins.ts +147 -0
  55. package/src/sany-generator.ts +3128 -0
  56. package/src/sany-levels.ts +1208 -0
  57. package/src/sany-lexer.ts +545 -0
  58. package/src/sany-linter.ts +122 -0
  59. package/src/sany-modules.ts +1398 -0
  60. package/src/sany-nodes.ts +463 -0
  61. package/src/sany-parse-error.ts +2161 -0
  62. package/src/sany-report.ts +356 -0
  63. package/src/sany-selectors.ts +738 -0
  64. package/src/sany-source.ts +248 -0
  65. package/src/sany-xml.ts +1017 -0
  66. package/src/spec-description.ts +42 -11
  67. package/src/standard-modules.ts +200 -148
  68. package/src/standard-ranges.ts +116 -0
  69. package/src/state-graph.ts +180 -0
  70. package/src/subexpressions.ts +231 -171
  71. package/src/substitution-levels.ts +38 -17
  72. package/src/successors.ts +219 -107
  73. package/src/symmetry.ts +194 -54
  74. package/src/tableau.ts +54 -25
  75. package/src/tlc-ext.ts +42 -29
  76. package/src/tlc-messages.ts +329 -0
  77. package/src/tlc-module.ts +52 -43
  78. package/src/tlc-modules.ts +4 -0
  79. package/src/tlc-order.ts +78 -10
  80. package/src/tlc-random.ts +37 -9
  81. package/src/tlc-replay.ts +655 -0
  82. package/src/tlc-summary.ts +20 -5
  83. package/src/tlc-version.ts +18 -0
  84. package/src/transfer.ts +15 -3
  85. package/src/value-sources.ts +29 -0
  86. package/src/values.ts +731 -184
package/src/parallel.ts CHANGED
@@ -4,19 +4,31 @@ import {
4
4
  checkSequentially,
5
5
  createSearch,
6
6
  findAssumptionViolation,
7
+ reportInitialStatesProgress,
8
+ reportStartup,
7
9
  type CheckOptions,
10
+ type CheckProgress,
8
11
  type CheckResult,
12
+ type InitialStates,
9
13
  } from "./check";
10
14
  import { parseConfig, type ModelConfig } from "./config";
11
15
  import { createEnvironment } from "./environment";
12
16
  import { TlaEvaluationError } from "./errors";
13
17
  import { isImpure } from "./evaluate";
14
- import { createFingerprintTable, getFingerprintOwner, writeStateFingerprint } from "./fingerprints";
18
+ import {
19
+ createSharedFingerprints,
20
+ growSharedFingerprints,
21
+ insertSharedFingerprint,
22
+ isSharedFingerprintsFull,
23
+ writeStateFingerprint,
24
+ type SharedFingerprints,
25
+ } from "./fingerprints";
15
26
  import { getInitialStates } from "./successors";
16
27
  import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
17
28
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
18
29
  import { createStateEncoder, type EncodedStates } from "./transfer";
19
- import { getRankedStringCount, setStringOrder, type Value } from "./values";
30
+ import type { TlcVersion } from "./tlc-version";
31
+ import { getRankedStringCount, setStringOrder, setValueVersion, type Value } from "./values";
20
32
 
21
33
  const WORKER_THREADS_MODULE = "node:worker_threads";
22
34
 
@@ -34,10 +46,15 @@ export interface StartMessage {
34
46
  modules: [string, string | undefined][];
35
47
  classes: [string, Uint8Array | undefined][];
36
48
  index: number;
37
- count: number;
38
49
  ports: (Port | undefined)[];
39
50
  seed: EncodedStates;
40
- seedFingerprints: Int32Array;
51
+ isTool: boolean | undefined;
52
+ isTraceRecorded: boolean | undefined;
53
+ isWarningEnabled: boolean | undefined;
54
+ version: TlcVersion | undefined;
55
+ fingerprints: SharedFingerprints;
56
+ requests: Int32Array;
57
+ remaining: Int32Array;
41
58
  }
42
59
 
43
60
  export interface LevelMessage {
@@ -49,7 +66,15 @@ export interface LevelMessage {
49
66
  fingerprints: BigInt64Array;
50
67
  }
51
68
 
52
- export type WorkerMessage = { type: "ready" } | { type: "abort" } | LevelMessage;
69
+ export type WorkerMessage =
70
+ | { type: "ready" }
71
+ | { type: "abort" }
72
+ | { type: "paused" }
73
+ | LevelMessage;
74
+
75
+ export type CoordinatorMessage =
76
+ | { type: "level"; level: number }
77
+ | { type: "resize"; fingerprints: SharedFingerprints };
53
78
 
54
79
  interface WorkerHandle {
55
80
  postMessage: (message: unknown, transfer?: unknown[]) => void;
@@ -85,11 +110,45 @@ const spawnWorker = async (onMessage: (message: WorkerMessage) => void): Promise
85
110
  };
86
111
 
87
112
  export const check = async (options: CheckOptions): Promise<CheckResult> => {
88
- const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
89
- const fallBack = () => checkSequentially({ ...options, config });
113
+ const config =
114
+ typeof options.config === "string"
115
+ ? parseConfig(options.config, options.version)
116
+ : options.config;
117
+ const print = options.onPrint ?? console.log;
118
+ const printed = { count: 0 };
119
+ // What each point the search reports is about, such as the invariant TLC warns about.
120
+ const getKey = (progress: CheckProgress) =>
121
+ [
122
+ progress.kind,
123
+ "name" in progress ? progress.name : "",
124
+ "count" in progress ? progress.count : "",
125
+ "text" in progress ? progress.text : "",
126
+ ].join(" ");
127
+ const reported = new Set<string>();
128
+ const report = (progress: CheckProgress) => {
129
+ reported.add(getKey(progress));
130
+ options.onProgress?.(progress);
131
+ };
132
+ // The search on the calling thread prints and reports what this one did while it started, so it
133
+ // skips that.
134
+ const fallBack = () => {
135
+ const skipped = { count: printed.count };
136
+ return checkSequentially({
137
+ ...options,
138
+ config,
139
+ onPrint: (text) => (skipped.count-- > 0 ? undefined : print(text)),
140
+ onProgress: (progress) => {
141
+ if (!reported.has(getKey(progress))) options.onProgress?.(progress);
142
+ },
143
+ });
144
+ };
90
145
  const count = options.workers ?? 1;
146
+ // TLC's workers write the -dump state graph in whichever order they find it, so one worker
147
+ // writes the graph a single TLC worker does.
91
148
  const isSequential =
92
149
  count < 2 ||
150
+ typeof SharedArrayBuffer !== "function" ||
151
+ options.dump !== undefined ||
93
152
  config.view !== undefined ||
94
153
  config.postconditions.length > 0 ||
95
154
  config.possible.length > 0;
@@ -107,17 +166,30 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
107
166
  return bytes;
108
167
  };
109
168
  setStringOrder(getTlcStringOrder(options.source, config, loadModule));
169
+ setValueVersion(options.version);
110
170
  const startupStrings = getRankedStringCount();
111
171
  const specification = createEnvironment(config, {
112
172
  source: options.source,
113
173
  loadModule,
114
174
  loadClass,
115
- print: options.onPrint ?? console.log,
175
+ print: (text) => {
176
+ printed.count++;
177
+ print(text);
178
+ },
116
179
  files: { readFile: options.readFile, writeFile: options.writeFile },
180
+ isTool: options.isTool,
181
+ isTraceRecorded: options.isTraceRecorded,
182
+ isSanyReported: options.isSanyReported,
183
+ isWarningEnabled: options.isWarningEnabled,
184
+ version: options.version,
117
185
  });
186
+ reportStartup(specification, config, report);
118
187
  const environment = specification.environment;
119
- const search = createSearch(config, environment, () =>
120
- getTlcVariableOrder(options.source, loadModule),
188
+ const search = createSearch(
189
+ config,
190
+ environment,
191
+ () => getTlcVariableOrder(options.source, loadModule),
192
+ report,
121
193
  );
122
194
  if (findAssumptionViolation(specification.assumptions, environment)) return fallBack();
123
195
  if (
@@ -131,17 +203,15 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
131
203
  }
132
204
 
133
205
  const canonicalize = createCanonicalizer();
134
- const table = createFingerprintTable();
206
+ const shared = { fingerprints: createSharedFingerprints() };
135
207
  const fingerprint = new Int32Array(2);
136
- const seeds = Array.from({ length: count }, () => ({
137
- encoder: createStateEncoder(),
138
- fingerprints: [] as number[],
139
- }));
208
+ const seeds = Array.from({ length: count }, createStateEncoder);
140
209
  const statistics = {
141
210
  generatedStates: 0,
142
211
  distinctStates: 0,
143
212
  queuedStates: 0,
144
213
  depth: 0,
214
+ initialStates: undefined as InitialStates | undefined,
145
215
  outDegrees: Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0),
146
216
  };
147
217
  const tlcFingerprints: BigInt64Array[] = [];
@@ -166,20 +236,19 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
166
236
  const seedInitialStates = (init: Expression) => {
167
237
  const seeded: Value[][] = [];
168
238
  for (const state of getInitialStates(init, environment, search.spec.scope)) {
239
+ reportInitialStatesProgress(statistics.generatedStates, report);
169
240
  canonicalize(state);
170
241
  statistics.generatedStates++;
171
242
  writeStateFingerprint(search.getFingerprintState(state), fingerprint);
172
- if (!table.insert(fingerprint[0], fingerprint[1])) continue;
243
+ if (isSharedFingerprintsFull(shared.fingerprints)) {
244
+ shared.fingerprints = growSharedFingerprints(shared.fingerprints);
245
+ }
246
+ if (!insertSharedFingerprint(shared.fingerprints, fingerprint[0], fingerprint[1])) continue;
173
247
  if (search.findViolatedInvariant(state) || search.findFailedInitialPredicate(state)) {
174
248
  return false;
175
249
  }
176
- if (!search.satisfiesConstraints(state)) {
177
- table.remove(fingerprint[0], fingerprint[1]);
178
- continue;
179
- }
180
- const seed = seeds[getFingerprintOwner(fingerprint[1], count)];
181
- if (!seed.encoder.encode(state)) return false;
182
- seed.fingerprints.push(fingerprint[0], fingerprint[1]);
250
+ if (!search.satisfiesConstraints(state)) continue;
251
+ if (!seeds[seeded.length % count].encode(state)) return false;
183
252
  seeded.push(state);
184
253
  statistics.distinctStates++;
185
254
  }
@@ -189,6 +258,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
189
258
  return true;
190
259
  };
191
260
  // The sequential search reports a violation or a failed evaluation in the initial states.
261
+ report({ kind: "initial-states-started" });
192
262
  try {
193
263
  if (!seedInitialStates(search.spec.init)) return fallBack();
194
264
  } catch (error) {
@@ -197,21 +267,29 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
197
267
  }
198
268
  if (getRankedStringCount() !== startupStrings) return fallBack();
199
269
  statistics.depth = statistics.generatedStates === 0 ? 0 : 1;
270
+ statistics.initialStates = {
271
+ generated: statistics.generatedStates,
272
+ distinct: statistics.distinctStates,
273
+ };
274
+ report({ kind: "initial-states-computed", initialStates: statistics.initialStates });
200
275
  if (statistics.distinctStates === 0) return createSuccess();
201
276
 
202
277
  const reports: WorkerMessage[] = [];
203
- const pending = { resolve: (_reports: WorkerMessage[]) => {} };
278
+ const pending = { count, resolve: (_reports: WorkerMessage[]) => {} };
204
279
  const onMessage = (message: WorkerMessage) => {
205
280
  reports.push(message);
206
- if (message.type === "abort" || reports.length === count) {
281
+ if (message.type === "abort" || reports.length === pending.count) {
207
282
  pending.resolve(reports.splice(0));
208
283
  }
209
284
  };
210
- const waitForReports = () =>
285
+ const waitForReports = (expected: number) =>
211
286
  new Promise<WorkerMessage[]>((resolve) => {
287
+ pending.count = expected;
212
288
  pending.resolve = resolve;
213
289
  });
214
- const ready = waitForReports();
290
+ const ready = waitForReports(count);
291
+ const requests = new Int32Array(new SharedArrayBuffer(count * 4));
292
+ const remaining = new Int32Array(new SharedArrayBuffer(count * 4));
215
293
  const workers = await Promise.all(Array.from({ length: count }, () => spawnWorker(onMessage)));
216
294
  const terminate = () => workers.forEach((worker) => worker.terminate());
217
295
  const channels = new Map<string, MessageChannel>();
@@ -225,8 +303,7 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
225
303
  const ports = Array.from({ length: count }, (_, peer) =>
226
304
  peer === index ? undefined : getPort(index, peer),
227
305
  );
228
- const seed = seeds[index].encoder.finish();
229
- const seedFingerprints = Int32Array.from(seeds[index].fingerprints);
306
+ const seed = seeds[index].finish();
230
307
  const message: StartMessage = {
231
308
  type: "start",
232
309
  source: options.source,
@@ -234,27 +311,42 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
234
311
  modules: [...modules],
235
312
  classes: [...classes],
236
313
  index,
237
- count,
238
314
  ports,
239
315
  seed,
240
- seedFingerprints,
316
+ isTool: options.isTool,
317
+ isTraceRecorded: options.isTraceRecorded,
318
+ isWarningEnabled: options.isWarningEnabled,
319
+ version: options.version,
320
+ fingerprints: shared.fingerprints,
321
+ requests,
322
+ remaining,
241
323
  };
242
- worker.postMessage(message, [
243
- ...ports.filter((port) => port !== undefined),
244
- seed.words.buffer,
245
- seedFingerprints.buffer,
246
- ]);
324
+ worker.postMessage(message, [...ports.filter((port) => port !== undefined), seed.words.buffer]);
247
325
  });
248
326
  const isAborted = (messages: WorkerMessage[]) =>
249
327
  messages.some((message) => message.type === "abort");
328
+ const send = (message: CoordinatorMessage) =>
329
+ workers.forEach((worker) => worker.postMessage(message));
330
+ // Workers pause when the shared table needs to grow, and resume once it has.
331
+ const collectLevels = async (messages: WorkerMessage[]): Promise<WorkerMessage[]> => {
332
+ const paused = messages.filter((message) => message.type === "paused").length;
333
+ if (paused === 0 || isAborted(messages)) return messages;
334
+ shared.fingerprints = growSharedFingerprints(shared.fingerprints);
335
+ const resumed = waitForReports(paused);
336
+ send({ type: "resize", fingerprints: shared.fingerprints });
337
+ const reported = messages.filter((message) => message.type !== "paused");
338
+ return [...reported, ...(await collectLevels(await resumed))];
339
+ };
250
340
  if (isAborted(await ready)) {
251
341
  terminate();
252
342
  return fallBack();
253
343
  }
254
344
  for (let level = 1; ; level++) {
255
- const reported = waitForReports();
256
- workers.forEach((worker) => worker.postMessage({ type: "level", level }));
257
- const messages = await reported;
345
+ const reported = waitForReports(count);
346
+ // Every worker is idle, so peers can ask any of them for states once the level starts.
347
+ requests.forEach((_, index) => Atomics.store(requests, index, 0));
348
+ send({ type: "level", level });
349
+ const messages = await collectLevels(await reported);
258
350
  if (isAborted(messages)) {
259
351
  terminate();
260
352
  return fallBack();