tla-ts 0.0.4-dev.8fefa42 → 0.0.4-dev.a11a30a

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 (60) hide show
  1. package/README.md +27 -130
  2. package/dist/deciders.cjs +1 -1
  3. package/dist/deciders.js +1 -1
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +1 -3
  6. package/dist/index.d.ts +1 -3
  7. package/dist/index.js +1 -1
  8. package/dist/prove-worker.cjs +1 -1
  9. package/dist/prove-worker.js +1 -1
  10. package/dist/prove.cjs +506 -25
  11. package/dist/prove.js +506 -25
  12. package/dist/run-cli.cjs +35 -28
  13. package/dist/run-cli.js +35 -28
  14. package/dist/tlapm.cjs +2 -2
  15. package/dist/tlapm.js +2 -2
  16. package/package.json +1 -1
  17. package/src/obligations/action.ts +56 -99
  18. package/src/obligations/anon.ts +37 -41
  19. package/src/obligations/deref.ts +49 -90
  20. package/src/obligations/expression.ts +63 -49
  21. package/src/obligations/format.ts +7 -8
  22. package/src/obligations/generate.ts +60 -155
  23. package/src/obligations/index.ts +6 -4
  24. package/src/obligations/levels.ts +20 -32
  25. package/src/obligations/module.ts +93 -127
  26. package/src/obligations/pickle.ts +1 -3
  27. package/src/obligations/pragma.ts +14 -14
  28. package/src/obligations/printer.ts +17 -21
  29. package/src/obligations/proof.ts +0 -1
  30. package/src/obligations/source.ts +72 -73
  31. package/src/obligations/temporal.ts +29 -27
  32. package/src/obligations/tuples.ts +37 -53
  33. package/src/ocaml-marshal.ts +166 -0
  34. package/src/prove/decide-pool.ts +12 -17
  35. package/src/prove/deciders.ts +7 -1
  36. package/src/prove/first-order.ts +78 -0
  37. package/src/prove/isabelle/encode.ts +96 -127
  38. package/src/prove/ocaml.ts +1 -1
  39. package/src/prove/prep/action.ts +39 -51
  40. package/src/prove/prep/defs.ts +19 -13
  41. package/src/prove/prep/elab.ts +1 -1
  42. package/src/prove/prep/level-comparison.ts +15 -38
  43. package/src/prove/prep/pragma.ts +4 -4
  44. package/src/prove/prove-worker.ts +4 -11
  45. package/src/prove/prove.ts +230 -149
  46. package/src/prove/ptl/encode.ts +5 -6
  47. package/src/prove/smt/axioms.ts +78 -79
  48. package/src/prove/smt/data.ts +16 -45
  49. package/src/prove/smt/encode.ts +18 -23
  50. package/src/prove/smt/extensionality.ts +14 -12
  51. package/src/prove/smt/flatten.ts +3 -18
  52. package/src/prove/smt/format.ts +14 -3
  53. package/src/prove/smt/instances.ts +4 -5
  54. package/src/prove/smt/rewrite.ts +36 -48
  55. package/src/prove/smt/simplify.ts +10 -10
  56. package/src/prove/tlapm/tlapm.ts +13 -13
  57. package/src/prove/verdict.ts +5 -0
  58. package/src/prove/zenon/encode.ts +36 -101
  59. package/src/run-cli.ts +264 -115
  60. package/src/tlapm-cache.ts +236 -0
@@ -3,10 +3,11 @@
3
3
  import { TlaProverError, TlaUnsupportedError } from "../errors";
4
4
  import { getChildren, mapHyp, type Expr, type Hyp, type Sequent } from "../obligations/expression";
5
5
  import { getObligations, type PrintedObligation } from "../obligations";
6
+ import { formatObligation } from "../obligations/printer";
6
7
  import { isWorkerSupported } from "../worker";
7
- import { DECIDERS } from "./deciders";
8
- import { createDecidePool } from "./decide-pool";
9
- import { encodeIsabelle } from "./isabelle/encode";
8
+ import { decide } from "./deciders";
9
+ import { createDecidePool, type DecidePool } from "./decide-pool";
10
+ import { encodeIsabelle, encodeIsabelleTheory } from "./isabelle/encode";
10
11
  import { processObligation } from "./prep/action";
11
12
  import { expandDefs } from "./prep/defs";
12
13
  import { normalizeSequent } from "./prep/elab";
@@ -21,7 +22,7 @@ import type { EncodingSession } from "./smt/rewrite";
21
22
  import { encodeZenon } from "./zenon/encode";
22
23
  import type { TlapmResult } from "./tlapm/tlapm";
23
24
  import { TLAPS_MODULE } from "./tlaps-module";
24
- import type { DecideOptions, Verdict } from "./verdict";
25
+ import type { Verdict } from "./verdict";
25
26
 
26
27
  interface ObligationBase {
27
28
  id: number;
@@ -53,58 +54,57 @@ export interface ProveResult {
53
54
  omittedProofCount: number;
54
55
  }
55
56
 
56
- // tla-ts runs its own backends, and tlapm runs tlapm on the module.
57
- export const PROVE_BACKENDS = ["tla-ts", "tlapm"] as const;
58
-
59
- export type ProveBackend = (typeof PROVE_BACKENDS)[number];
60
-
61
- export const isProveBackend = (name: string | undefined): name is ProveBackend =>
62
- PROVE_BACKENDS.some((backend) => backend === name);
63
-
64
57
  export interface ProveOptions {
65
58
  source: string;
66
59
  loadModule?: (name: string) => string | undefined;
67
- backend?: ProveBackend;
60
+ backend?: "tla-ts" | "tlapm";
68
61
  signal?: AbortSignal;
69
62
  onObligation?: (obligation: ProofObligation) => void;
70
63
  workers?: number;
71
64
  }
72
65
 
73
- // What one prover concluded about an obligation, as Fpfile keeps it. Fpfile keeps one Isabelle
74
- // result, which only a method with the same tactic reuses.
75
- interface PriorResult {
66
+ // What one method concluded about an obligation, as tlapm's toolbox messages report it.
67
+ export interface ProofAttempt {
68
+ method: Method;
76
69
  isProved: boolean;
77
- timeLimitMs: number;
78
- tactic: string | undefined;
70
+ isReused: boolean;
71
+ reason?: string;
72
+ verdict?: Verdict;
73
+ timeUsedMs?: number;
74
+ }
75
+
76
+ interface ProofReport {
77
+ obligation: ProofObligation;
78
+ attempts: ProofAttempt[];
79
+ }
80
+
81
+ // Why a method left an obligation unproved: a prover decided so, or tla-ts couldn't try it.
82
+ interface MethodFailure {
83
+ reason: string;
84
+ isDecided: boolean;
79
85
  }
80
86
 
81
- // An obligation being proved. The SMT encoder numbers the CASE errors it standardizes across the
82
- // run, so an obligation it encodes one in waits for every earlier obligation and keeps a session
83
- // from the count they leave.
87
+ // The SMT encoder numbers the CASE errors it standardizes across the run, so an obligation it
88
+ // encodes one in waits for every earlier obligation and starts from the count they leave.
84
89
  interface ObligationTask {
85
90
  done: Promise<void>;
86
91
  session: { initial: number; current: EncodingSession } | undefined;
87
92
  }
88
93
 
89
- // What tlapm keeps across the obligations of one run: the SMT encoder's counter, and what each
90
- // prover concluded for each fingerprint. An obligation waits for the last earlier one with its
91
- // fingerprint, so obligations prove concurrently as they would in turn.
94
+ // What tlapm keeps across one run's obligations. An obligation waits for the last earlier one with
95
+ // its fingerprint, so obligations prove concurrently as they would in turn.
92
96
  interface ProveRun {
93
97
  tasks: ObligationTask[];
94
- results: Map<string, Map<string, PriorResult>>;
98
+ results: Map<string, Map<string, ProofAttempt>>;
99
+ isFingerprinted: (obligation: PrintedObligation) => boolean;
100
+ isProving: boolean;
95
101
  lastByFingerprint: Map<string, Promise<void>>;
96
- decide: (
97
- prover: string,
98
- text: string,
99
- options: DecideOptions,
100
- priority: number,
101
- ) => Promise<Verdict>;
102
+ decide: DecidePool["decide"];
102
103
  }
103
104
 
104
105
  interface Backend {
105
- // The text tlapm sends the backend for an obligation after Prep.normalize_expand, through the
106
- // frontend and Prep.flatten that Prep.ship applies first. It throws a TlaUnsupportedError for
107
- // what tla-ts can't encode.
106
+ // The text Prep.ship sends the backend after Prep.normalize_expand. It throws a
107
+ // TlaUnsupportedError for what tla-ts can't encode.
108
108
  encode: (id: number, sequent: Sequent, method: Method, session: EncodingSession) => string;
109
109
  }
110
110
 
@@ -119,7 +119,6 @@ const SMT: Backend = {
119
119
  encodeSmt(shipFirstOrder(sequent), session),
120
120
  };
121
121
 
122
- // tla-ts's backends, by the prover of the method that selects each.
123
122
  const BACKENDS: Record<string, Backend> = {
124
123
  smt: SMT,
125
124
  z3: SMT,
@@ -127,14 +126,14 @@ const BACKENDS: Record<string, Backend> = {
127
126
  // tlapm names the theory after its temporary file, and tla-ts after the obligation.
128
127
  isabelle: {
129
128
  encode: (id, sequent, method) =>
130
- encodeIsabelle(id, shipFirstOrder(sequent), method.tactic ?? "auto", `tlapm_${id}`),
129
+ encodeIsabelle(id, shipFirstOrder(sequent), method.tactic ?? "auto"),
131
130
  },
132
131
  // Pltl.process_obligation is LS4's frontend.
133
132
  ls4: { encode: (_id, sequent) => encodePtl(sequent) },
134
133
  };
135
134
 
136
- // Whether an expression has a CASE with no OTHER. The frontends and Prep.flatten add none, so the
137
- // SMT encoder standardizes one only in an obligation that has one after Prep.normalize_expand.
135
+ // The frontends and Prep.flatten add no CASE without OTHER, so the SMT encoder standardizes one
136
+ // only in an obligation that has one after Prep.normalize_expand.
138
137
  const hasIncompleteCase = (expression: Expr): boolean =>
139
138
  (expression.kind === "Case" && expression.other === undefined) ||
140
139
  getChildren(expression).some(hasIncompleteCase);
@@ -170,11 +169,43 @@ const getUnproved = (
170
169
  obligation: PrintedObligation,
171
170
  status: "failed" | "unsupported",
172
171
  reason: string,
173
- ): ProofObligation => ({ ...getBase(obligation), status, reason });
172
+ attempts: ProofAttempt[] = [],
173
+ ): ProofReport => ({ obligation: { ...getBase(obligation), status, reason }, attempts });
174
174
 
175
175
  // Prep.normalize_expand: expand_defs, Elab.normalize, then the frontends the step cites.
176
- const normalizeExpand = (sequent: Sequent, frontends: Set<string>) =>
177
- expandEnabledCdot(normalizeSequent(expandDefs(sequent)), frontends);
176
+ const normalizeExpand = (obligation: PrintedObligation, methods = findMethods(obligation)) =>
177
+ expandEnabledCdot(normalizeSequent(expandDefs(obligation.sequent)), methods.frontends);
178
+
179
+ // A failed obligation as tlapm prints it: Prep.normalize_expand for a new failure, and
180
+ // Toolbox.normalize, which keeps pragmas, for a reused one.
181
+ export const formatNormalized = (obligation: PrintedObligation, isReused: boolean) =>
182
+ `${formatObligation(
183
+ isReused
184
+ ? normalizeSequent(expandDefs(obligation.sequent, false))
185
+ : normalizeExpand(obligation),
186
+ )}\n`;
187
+
188
+ // The theory of the proofs Isabelle found. tlapm also writes Zenon's proofs there.
189
+ export const encodeProofTheory = (
190
+ name: string,
191
+ reports: ProofReport[],
192
+ obligations: PrintedObligation[],
193
+ ) =>
194
+ encodeIsabelleTheory(
195
+ name,
196
+ reports.flatMap((report, index) =>
197
+ report.attempts
198
+ .filter(
199
+ (attempt) =>
200
+ attempt.method.prover === "isabelle" && attempt.isProved && !attempt.isReused,
201
+ )
202
+ .map((attempt) => ({
203
+ id: report.obligation.id,
204
+ sequent: shipFirstOrder(normalizeExpand(obligations[index])),
205
+ tactic: attempt.method.tactic ?? "auto",
206
+ })),
207
+ ),
208
+ );
178
209
 
179
210
  // The text one backend receives for an obligation, for comparing with tlapm's.
180
211
  export const encodeForBackend = (
@@ -189,12 +220,7 @@ export const encodeForBackend = (
189
220
  prover,
190
221
  timeLimitMs: Infinity,
191
222
  };
192
- return backend.encode(
193
- obligation.id,
194
- normalizeExpand(obligation.sequent, methods.frontends),
195
- method,
196
- session,
197
- );
223
+ return backend.encode(obligation.id, normalizeExpand(obligation, methods), method, session);
198
224
  };
199
225
 
200
226
  // The SMT encoder's session for an obligation, from the count of CASE errors every earlier
@@ -223,7 +249,7 @@ const proveWithBackends = async (
223
249
  index: number,
224
250
  run: ProveRun,
225
251
  signal: AbortSignal | undefined,
226
- ): Promise<ProofObligation> => {
252
+ ): Promise<ProofReport> => {
227
253
  if (obligation.kind.kind === "Error")
228
254
  return getUnproved(obligation, "failed", obligation.kind.message);
229
255
  const methods = findMethods(obligation);
@@ -231,42 +257,64 @@ const proveWithBackends = async (
231
257
  const earlier = run.lastByFingerprint.get(fingerprint);
232
258
  run.lastByFingerprint.set(fingerprint, run.tasks[index].done);
233
259
  await earlier;
234
- const results = run.results.get(fingerprint) ?? new Map<string, PriorResult>();
235
- run.results.set(fingerprint, results);
236
- const getProved = (prover: string): ProofObligation => ({
237
- ...getBase(obligation),
238
- status: "proved",
239
- prover,
260
+ const attempts: ProofAttempt[] = [];
261
+ const failures: MethodFailure[] = [];
262
+ const getProved = (prover: string): ProofReport => ({
263
+ obligation: { ...getBase(obligation), status: "proved", prover },
264
+ attempts,
240
265
  });
266
+ // Prep.already_processed, which forgets the fingerprint's results on each call under --nofp.
241
267
  const getPrior = (method: Method) => {
242
- const prior = results.get(method.prover);
243
- return prior?.tactic === method.tactic ? prior : undefined;
268
+ if (!run.isFingerprinted(obligation)) run.results.delete(fingerprint);
269
+ const prior = run.results.get(fingerprint)?.get(method.prover);
270
+ return prior?.method.tactic === method.tactic ? prior : undefined;
271
+ };
272
+ // Fpfile.get_timeout.
273
+ const getBudget = (method: Method) => method.rlimit ?? method.timeLimitMs / 1000;
274
+ const reuse = (prior: ProofAttempt) =>
275
+ attempts.push({ ...prior, isReused: true, timeUsedMs: undefined });
276
+ const record = (attempt: ProofAttempt) => {
277
+ attempts.push(attempt);
278
+ run.results.set(
279
+ fingerprint,
280
+ (run.results.get(fingerprint) ?? new Map()).set(attempt.method.prover, attempt),
281
+ );
282
+ };
283
+ const skip = (method: Method, reason: string) => {
284
+ attempts.push({ method, isProved: false, isReused: false, reason });
285
+ failures.push({ reason, isDecided: false });
244
286
  };
245
- const setPrior = (method: Method, isProved: boolean) =>
246
- results.set(method.prover, {
247
- isProved,
248
- timeLimitMs: method.timeLimitMs,
249
- tactic: method.tactic,
250
- });
251
- if (getPrior(methods.provers[0])?.isProved) return getProved(methods.provers[0].prover);
252
- const sequent = normalizeExpand(obligation.sequent, methods.frontends);
253
- if (obligation.kind.kind === "Support" && isTrivial(sequent))
254
- return { ...getBase(obligation), status: "trivial" };
255
- const reasons: string[] = [];
256
- const state = { isDecided: false };
287
+ const firstPrior = getPrior(methods.provers[0]);
288
+ if (firstPrior?.isProved) {
289
+ reuse(firstPrior);
290
+ return getProved(firstPrior.method.prover);
291
+ }
292
+ const sequent = normalizeExpand(obligation, methods);
293
+ // Fpfile.fp_writes keeps no trivial result, but adds the fingerprint.
294
+ if (obligation.kind.kind === "Support" && isTrivial(sequent)) {
295
+ run.results.set(fingerprint, run.results.get(fingerprint) ?? new Map());
296
+ return { obligation: { ...getBase(obligation), status: "trivial" }, attempts };
297
+ }
257
298
  for (const method of methods.provers) {
258
299
  const prior = getPrior(method);
259
- if (prior?.isProved) return getProved(method.prover);
260
- if (prior && prior.timeLimitMs >= method.timeLimitMs) {
261
- state.isDecided = true;
262
- reasons.push(`${method.prover} failed on an obligation with the same fingerprint`);
300
+ if (prior?.isProved) {
301
+ reuse(prior);
302
+ return getProved(method.prover);
303
+ }
304
+ if (prior && getBudget(prior.method) >= getBudget(method)) {
305
+ reuse(prior);
306
+ failures.push({
307
+ reason: `${method.prover} failed on an obligation with the same fingerprint`,
308
+ isDecided: true,
309
+ });
263
310
  continue;
264
311
  }
312
+ if (!run.isProving) continue;
265
313
  // Prep.prove_with: the Trivial method proves anything.
266
314
  if (method.prover === "trivial") return getProved(method.prover);
267
315
  const backend = BACKENDS[method.prover];
268
316
  if (!backend) {
269
- reasons.push(`tla-ts has no ${method.prover} backend`);
317
+ skip(method, `tla-ts has no ${method.prover} backend`);
270
318
  continue;
271
319
  }
272
320
  try {
@@ -275,50 +323,131 @@ const proveWithBackends = async (
275
323
  (sequent.context.some(isIncompleteCaseHyp) || hasIncompleteCase(sequent.active));
276
324
  const session = isCaseNumbered ? await getSession(run, index) : { caseErrors: 0 };
277
325
  const text = backend.encode(obligation.id, sequent, method, session);
278
- const verdict = await run.decide(
326
+ const decision = await run.decide(
279
327
  method.prover,
280
328
  text,
281
329
  { timeLimitMs: method.timeLimitMs, signal },
282
330
  index,
283
331
  );
284
- setPrior(method, verdict === "unsat");
285
- if (verdict === "unsat") return getProved(method.prover);
286
- state.isDecided = true;
287
- reasons.push(`${method.prover} answered ${verdict}`);
332
+ const isProved = decision.verdict === "unsat";
333
+ record({
334
+ method,
335
+ isProved,
336
+ isReused: false,
337
+ ...(!isProved && {
338
+ reason: decision.timeUsedMs >= method.timeLimitMs ? "timeout" : "false",
339
+ }),
340
+ verdict: decision.verdict,
341
+ timeUsedMs: decision.timeUsedMs,
342
+ });
343
+ if (isProved) return getProved(method.prover);
344
+ failures.push({ reason: `${method.prover} answered ${decision.verdict}`, isDecided: true });
288
345
  } catch (error) {
289
346
  if (error instanceof TlaProverError) {
290
- setPrior(method, false);
291
- state.isDecided = true;
292
- reasons.push(`${method.prover} failed: ${error.message}`);
347
+ record({ method, isProved: false, isReused: false, reason: "false" });
348
+ failures.push({ reason: `${method.prover} failed: ${error.message}`, isDecided: true });
293
349
  continue;
294
350
  }
295
351
  if (!(error instanceof TlaUnsupportedError)) throw error;
296
- reasons.push(`${method.prover} can't encode ${error.message}`);
352
+ skip(method, `${method.prover} can't encode ${error.message}`);
297
353
  }
298
354
  }
299
- return getUnproved(obligation, state.isDecided ? "failed" : "unsupported", reasons.join(", "));
355
+ return getUnproved(
356
+ obligation,
357
+ failures.some((failure) => failure.isDecided) ? "failed" : "unsupported",
358
+ failures.map((failure) => failure.reason).join(", "),
359
+ attempts,
360
+ );
300
361
  };
301
362
 
302
- // The one place an error becomes a result: what tla-ts can't handle is unsupported, and what tlapm
363
+ // An error that escapes proveWithBackends: what tla-ts can't handle is unsupported, and what tlapm
303
364
  // refuses, as a frontend's soundness check, fails.
304
- const getResult = async (
305
- obligation: PrintedObligation,
306
- getProved: () => Promise<ProofObligation>,
365
+ const getErrorReport = (obligation: PrintedObligation, error: unknown) => {
366
+ if (error instanceof TlaUnsupportedError)
367
+ return getUnproved(obligation, "unsupported", error.message);
368
+ if (error instanceof TlaProverError) return getUnproved(obligation, "failed", error.message);
369
+ throw error;
370
+ };
371
+
372
+ interface ObligationsOptions {
373
+ workers?: number;
374
+ signal?: AbortSignal;
375
+ onReport?: (report: ProofReport, obligation: PrintedObligation) => void;
376
+ results?: Map<string, Map<string, ProofAttempt>>;
377
+ // --nofp and --nofpl.
378
+ isFingerprinted?: (obligation: PrintedObligation) => boolean;
379
+ // --noproving.
380
+ isProving?: boolean;
381
+ }
382
+
383
+ export const proveObligations = async (
384
+ obligations: PrintedObligation[],
385
+ options: ObligationsOptions,
307
386
  ) => {
387
+ const count = options.workers ?? 1;
388
+ const pool = count > 1 && isWorkerSupported() ? await createDecidePool(count) : undefined;
389
+ const onAbort = () => pool?.terminate(options.signal?.reason);
390
+ options.signal?.addEventListener("abort", onAbort);
391
+ const run: ProveRun = {
392
+ tasks: [],
393
+ results: options.results ?? new Map(),
394
+ isFingerprinted: options.isFingerprinted ?? (() => true),
395
+ isProving: options.isProving ?? true,
396
+ lastByFingerprint: new Map(),
397
+ decide:
398
+ pool?.decide ?? (async (prover, text, decideOptions) => decide(prover, text, decideOptions)),
399
+ };
400
+ // A few obligations per worker prove at once, and each reports once every earlier one has.
401
+ const limit = pool ? count * 4 : 1;
402
+ const pending: Promise<ProofReport>[] = [];
403
+ const isSettled: boolean[] = [];
404
+ const running = new Set<Promise<void>>();
405
+ const reports: ProofReport[] = [];
406
+ const reportSettled = async () => {
407
+ while (isSettled[reports.length]) {
408
+ options.signal?.throwIfAborted();
409
+ const report = await pending[reports.length];
410
+ reports.push(report);
411
+ options.onReport?.(report, obligations[reports.length - 1]);
412
+ // HACK: backends on this thread run synchronously, so yield to let an abort land.
413
+ await new Promise((resolve) => setTimeout(resolve));
414
+ }
415
+ };
308
416
  try {
309
- return await getProved();
310
- } catch (error) {
311
- if (error instanceof TlaUnsupportedError)
312
- return getUnproved(obligation, "unsupported", error.message);
313
- if (error instanceof TlaProverError) return getUnproved(obligation, "failed", error.message);
314
- throw error;
417
+ for (const [index, obligation] of obligations.entries()) {
418
+ options.signal?.throwIfAborted();
419
+ const done = Promise.withResolvers<void>();
420
+ run.tasks.push({ done: done.promise, session: undefined });
421
+ const report = proveWithBackends(obligation, index, run, options.signal).catch(
422
+ (error: unknown) => getErrorReport(obligation, error),
423
+ );
424
+ pending.push(report);
425
+ running.add(done.promise);
426
+ const settle = () => {
427
+ isSettled[index] = true;
428
+ running.delete(done.promise);
429
+ done.resolve();
430
+ };
431
+ void report.then(settle, settle);
432
+ if (running.size >= limit) await Promise.race(running);
433
+ await reportSettled();
434
+ }
435
+ await Promise.all(running);
436
+ await reportSettled();
437
+ } finally {
438
+ options.signal?.removeEventListener("abort", onAbort);
439
+ pool?.terminate(new TlaProverError("prove stopped"));
315
440
  }
441
+ return reports;
316
442
  };
317
443
 
318
- const getFromTlapm = (obligation: PrintedObligation, result: TlapmResult | undefined) =>
444
+ const getFromTlapm = (
445
+ obligation: PrintedObligation,
446
+ result: TlapmResult | undefined,
447
+ ): ProofObligation =>
319
448
  result
320
449
  ? { ...getBase(obligation), ...result }
321
- : getUnproved(obligation, "unsupported", "tlapm reported no result");
450
+ : { ...getBase(obligation), status: "unsupported", reason: "tlapm reported no result" };
322
451
 
323
452
  export const prove = async (options: ProveOptions): Promise<ProveResult> => {
324
453
  const name = getModuleName(options.source);
@@ -360,58 +489,10 @@ export const prove = async (options: ProveOptions): Promise<ProveResult> => {
360
489
  ...counts,
361
490
  };
362
491
  }
363
- const count = options.workers ?? 1;
364
- const pool = count > 1 && isWorkerSupported() ? await createDecidePool(count) : undefined;
365
- const onAbort = () => pool?.terminate(options.signal?.reason);
366
- options.signal?.addEventListener("abort", onAbort);
367
- const run: ProveRun = {
368
- tasks: [],
369
- results: new Map(),
370
- lastByFingerprint: new Map(),
371
- decide:
372
- pool?.decide ??
373
- (async (prover, text, decideOptions) => DECIDERS[prover](text, decideOptions)),
374
- };
375
- // A few obligations per worker prove at once, and each reports once every earlier one has.
376
- const limit = pool ? count * 4 : 1;
377
- const pending: Promise<ProofObligation>[] = [];
378
- const isSettled: boolean[] = [];
379
- const running = new Set<Promise<void>>();
380
- const proved: ProofObligation[] = [];
381
- const reportSettled = async () => {
382
- while (isSettled[proved.length]) {
383
- options.signal?.throwIfAborted();
384
- const result = await pending[proved.length];
385
- proved.push(result);
386
- options.onObligation?.(result);
387
- // HACK: backends on this thread run synchronously, so yield to let an abort land.
388
- await new Promise((resolve) => setTimeout(resolve));
389
- }
390
- };
391
- try {
392
- for (const [index, obligation] of obligations.entries()) {
393
- options.signal?.throwIfAborted();
394
- const done = Promise.withResolvers<void>();
395
- run.tasks.push({ done: done.promise, session: undefined });
396
- const result = getResult(obligation, () =>
397
- proveWithBackends(obligation, index, run, options.signal),
398
- );
399
- pending.push(result);
400
- running.add(done.promise);
401
- const settle = () => {
402
- isSettled[index] = true;
403
- running.delete(done.promise);
404
- done.resolve();
405
- };
406
- void result.then(settle, settle);
407
- if (running.size >= limit) await Promise.race(running);
408
- await reportSettled();
409
- }
410
- await Promise.all(running);
411
- await reportSettled();
412
- } finally {
413
- options.signal?.removeEventListener("abort", onAbort);
414
- pool?.terminate(new TlaProverError("prove stopped"));
415
- }
416
- return { obligations: proved, ...counts };
492
+ const reports = await proveObligations(obligations, {
493
+ workers: options.workers,
494
+ signal: options.signal,
495
+ onReport: (report) => options.onObligation?.(report.obligation),
496
+ });
497
+ return { obligations: reports.map((report) => report.obligation), ...counts };
417
498
  };
@@ -126,13 +126,12 @@ const prepareHyps = (scope: Scope | undefined, expression: Expr): Expr => {
126
126
  };
127
127
  for (const original of expression.sequent.context) {
128
128
  const hyp = substituteHyp(state.substitution, original);
129
- if (hyp.kind === "Fact") {
130
- push({ ...hyp, expression: prepareHyps(state.scope, hyp.expression) });
131
- state.substitution = bumpBy(1, state.substitution);
132
- continue;
133
- }
134
129
  if (hyp.kind !== "Fresh" || !hyp.domain || original.kind !== "Fresh" || !original.domain) {
135
- push(hyp);
130
+ push(
131
+ hyp.kind === "Fact"
132
+ ? { ...hyp, expression: prepareHyps(state.scope, hyp.expression) }
133
+ : hyp,
134
+ );
136
135
  state.substitution = bumpBy(1, state.substitution);
137
136
  continue;
138
137
  }