tla-ts 0.0.4-dev.702eb98 → 0.0.4-dev.7b5087a

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.
@@ -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 { decide } from "./deciders";
8
9
  import { createDecidePool } from "./decide-pool";
9
- import { encodeIsabelle } from "./isabelle/encode";
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 { DecideOptions, Decision, Verdict } from "./verdict";
25
26
 
26
27
  interface ObligationBase {
27
28
  id: number;
@@ -70,12 +71,22 @@ export interface ProveOptions {
70
71
  workers?: number;
71
72
  }
72
73
 
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 {
74
+ // What one method concluded about an obligation, as tlapm's toolbox messages report it. tlapm's
75
+ // reason for a failure is false when the backend answered, timeout when it ran out of time, or why
76
+ // tla-ts couldn't run it. A result reused from an earlier obligation with the same fingerprint, as
77
+ // Fpfile keeps it, has no time used.
78
+ export interface ProofAttempt {
79
+ method: Method;
76
80
  isProved: boolean;
77
- timeLimitMs: number;
78
- tactic: string | undefined;
81
+ isReused: boolean;
82
+ reason?: string;
83
+ verdict?: Verdict;
84
+ timeUsedMs?: number;
85
+ }
86
+
87
+ export interface ProofReport {
88
+ obligation: ProofObligation;
89
+ attempts: ProofAttempt[];
79
90
  }
80
91
 
81
92
  // An obligation being proved. The SMT encoder numbers the CASE errors it standardizes across the
@@ -88,17 +99,20 @@ interface ObligationTask {
88
99
 
89
100
  // What tlapm keeps across the obligations of one run: the SMT encoder's counter, and what each
90
101
  // 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.
102
+ // fingerprint, so obligations prove concurrently as they would in turn. Fpfile keeps one Isabelle
103
+ // result, which only a method with the same tactic reuses.
92
104
  interface ProveRun {
93
105
  tasks: ObligationTask[];
94
- results: Map<string, Map<string, PriorResult>>;
106
+ results: Map<string, Map<string, ProofAttempt>>;
107
+ isFingerprinted: (obligation: PrintedObligation) => boolean;
108
+ isProving: boolean;
95
109
  lastByFingerprint: Map<string, Promise<void>>;
96
110
  decide: (
97
111
  prover: string,
98
112
  text: string,
99
113
  options: DecideOptions,
100
114
  priority: number,
101
- ) => Promise<Verdict>;
115
+ ) => Promise<Decision>;
102
116
  }
103
117
 
104
118
  interface Backend {
@@ -176,6 +190,36 @@ const getUnproved = (
176
190
  const normalizeExpand = (sequent: Sequent, frontends: Set<string>) =>
177
191
  expandEnabledCdot(normalizeSequent(expandDefs(sequent)), frontends);
178
192
 
193
+ // The obligation as tlapm prints it in a toolbox message about a failure: Prep.normalize_expand
194
+ // for a new one, and Toolbox.normalize, which keeps pragmas, for one it reused.
195
+ export const formatNormalized = (obligation: PrintedObligation, isReused: boolean) =>
196
+ `${formatObligation(isReused ? normalizeSequent(expandDefs(obligation.sequent, false)) : normalizeExpand(obligation.sequent, findMethods(obligation).frontends))}\n`;
197
+
198
+ // The theory tlapm writes of the proofs Isabelle found for a module's obligations, in the order
199
+ // it found them. tlapm also writes there each proof Zenon found, which tla-ts leaves out.
200
+ export const encodeProofTheory = (
201
+ name: string,
202
+ reports: ProofReport[],
203
+ obligations: PrintedObligation[],
204
+ ) =>
205
+ encodeIsabelleTheory(
206
+ name,
207
+ reports.flatMap((report, index) =>
208
+ report.attempts
209
+ .filter(
210
+ (attempt) =>
211
+ attempt.method.prover === "isabelle" && attempt.isProved && !attempt.isReused,
212
+ )
213
+ .map((attempt) => ({
214
+ id: report.obligation.id,
215
+ sequent: shipFirstOrder(
216
+ normalizeExpand(obligations[index].sequent, findMethods(obligations[index]).frontends),
217
+ ),
218
+ tactic: attempt.method.tactic ?? "auto",
219
+ })),
220
+ ),
221
+ );
222
+
179
223
  // The text one backend receives for an obligation, for comparing with tlapm's.
180
224
  export const encodeForBackend = (
181
225
  obligation: PrintedObligation,
@@ -223,50 +267,71 @@ const proveWithBackends = async (
223
267
  index: number,
224
268
  run: ProveRun,
225
269
  signal: AbortSignal | undefined,
226
- ): Promise<ProofObligation> => {
270
+ ): Promise<ProofReport> => {
227
271
  if (obligation.kind.kind === "Error")
228
- return getUnproved(obligation, "failed", obligation.kind.message);
272
+ return { obligation: getUnproved(obligation, "failed", obligation.kind.message), attempts: [] };
229
273
  const methods = findMethods(obligation);
230
274
  const fingerprint = getFingerprint(obligation.sequent);
231
275
  const earlier = run.lastByFingerprint.get(fingerprint);
232
276
  run.lastByFingerprint.set(fingerprint, run.tasks[index].done);
233
277
  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,
278
+ const attempts: ProofAttempt[] = [];
279
+ const getProved = (prover: string): ProofReport => ({
280
+ obligation: { ...getBase(obligation), status: "proved", prover },
281
+ attempts,
240
282
  });
283
+ // Prep.already_processed: --nofp forgets the fingerprint's results before each method.
241
284
  const getPrior = (method: Method) => {
242
- const prior = results.get(method.prover);
243
- return prior?.tactic === method.tactic ? prior : undefined;
285
+ if (!run.isFingerprinted(obligation)) run.results.delete(fingerprint);
286
+ const prior = run.results.get(fingerprint)?.get(method.prover);
287
+ return prior?.method.tactic === method.tactic ? prior : undefined;
244
288
  };
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);
289
+ // Fpfile.get_timeout: a method with an rlimit stores it in place of its time limit.
290
+ const getBudget = (method: Method) => method.rlimit ?? method.timeLimitMs / 1000;
291
+ const reuse = (prior: ProofAttempt) =>
292
+ attempts.push({ ...prior, isReused: true, timeUsedMs: undefined });
293
+ const firstPrior = getPrior(methods.provers[0]);
294
+ if (firstPrior?.isProved) {
295
+ reuse(firstPrior);
296
+ return getProved(firstPrior.method.prover);
297
+ }
252
298
  const sequent = normalizeExpand(obligation.sequent, methods.frontends);
253
- if (obligation.kind.kind === "Support" && isTrivial(sequent))
254
- return { ...getBase(obligation), status: "trivial" };
299
+ // Fpfile.fp_writes keeps no trivial result, but adds the fingerprint.
300
+ if (obligation.kind.kind === "Support" && isTrivial(sequent)) {
301
+ run.results.set(fingerprint, run.results.get(fingerprint) ?? new Map());
302
+ return { obligation: { ...getBase(obligation), status: "trivial" }, attempts };
303
+ }
255
304
  const reasons: string[] = [];
256
305
  const state = { isDecided: false };
306
+ const decideAttempt = (attempt: ProofAttempt) => {
307
+ attempts.push(attempt);
308
+ run.results.set(
309
+ fingerprint,
310
+ (run.results.get(fingerprint) ?? new Map()).set(attempt.method.prover, attempt),
311
+ );
312
+ state.isDecided = true;
313
+ };
257
314
  for (const method of methods.provers) {
258
315
  const prior = getPrior(method);
259
- if (prior?.isProved) return getProved(method.prover);
260
- if (prior && prior.timeLimitMs >= method.timeLimitMs) {
316
+ if (prior?.isProved) {
317
+ reuse(prior);
318
+ return getProved(method.prover);
319
+ }
320
+ if (prior && getBudget(prior.method) >= getBudget(method)) {
321
+ reuse(prior);
261
322
  state.isDecided = true;
262
323
  reasons.push(`${method.prover} failed on an obligation with the same fingerprint`);
263
324
  continue;
264
325
  }
326
+ // Prep.really_ship: --noproving sends nothing to a backend.
327
+ if (!run.isProving) continue;
265
328
  // Prep.prove_with: the Trivial method proves anything.
266
329
  if (method.prover === "trivial") return getProved(method.prover);
267
330
  const backend = BACKENDS[method.prover];
268
331
  if (!backend) {
269
- reasons.push(`tla-ts has no ${method.prover} backend`);
332
+ const reason = `tla-ts has no ${method.prover} backend`;
333
+ attempts.push({ method, isProved: false, isReused: false, reason });
334
+ reasons.push(reason);
270
335
  continue;
271
336
  }
272
337
  try {
@@ -275,46 +340,140 @@ const proveWithBackends = async (
275
340
  (sequent.context.some(isIncompleteCaseHyp) || hasIncompleteCase(sequent.active));
276
341
  const session = isCaseNumbered ? await getSession(run, index) : { caseErrors: 0 };
277
342
  const text = backend.encode(obligation.id, sequent, method, session);
278
- const verdict = await run.decide(
343
+ const decision = await run.decide(
279
344
  method.prover,
280
345
  text,
281
346
  { timeLimitMs: method.timeLimitMs, signal },
282
347
  index,
283
348
  );
284
- setPrior(method, verdict === "unsat");
285
- if (verdict === "unsat") return getProved(method.prover);
286
- state.isDecided = true;
287
- reasons.push(`${method.prover} answered ${verdict}`);
349
+ const isProved = decision.verdict === "unsat";
350
+ decideAttempt({
351
+ method,
352
+ isProved,
353
+ isReused: false,
354
+ ...(!isProved && {
355
+ reason: decision.timeUsedMs >= method.timeLimitMs ? "timeout" : "false",
356
+ }),
357
+ verdict: decision.verdict,
358
+ timeUsedMs: decision.timeUsedMs,
359
+ });
360
+ if (isProved) return getProved(method.prover);
361
+ reasons.push(`${method.prover} answered ${decision.verdict}`);
288
362
  } catch (error) {
289
363
  if (error instanceof TlaProverError) {
290
- setPrior(method, false);
291
- state.isDecided = true;
364
+ decideAttempt({ method, isProved: false, isReused: false, reason: "false" });
292
365
  reasons.push(`${method.prover} failed: ${error.message}`);
293
366
  continue;
294
367
  }
295
368
  if (!(error instanceof TlaUnsupportedError)) throw error;
296
- reasons.push(`${method.prover} can't encode ${error.message}`);
369
+ const reason = `${method.prover} can't encode ${error.message}`;
370
+ attempts.push({ method, isProved: false, isReused: false, reason });
371
+ reasons.push(reason);
297
372
  }
298
373
  }
299
- return getUnproved(obligation, state.isDecided ? "failed" : "unsupported", reasons.join(", "));
374
+ return {
375
+ obligation: getUnproved(
376
+ obligation,
377
+ state.isDecided ? "failed" : "unsupported",
378
+ reasons.join(", "),
379
+ ),
380
+ attempts,
381
+ };
300
382
  };
301
383
 
302
384
  // The one place an error becomes a result: what tla-ts can't handle is unsupported, and what tlapm
303
385
  // refuses, as a frontend's soundness check, fails.
304
386
  const getResult = async (
305
387
  obligation: PrintedObligation,
306
- getProved: () => Promise<ProofObligation>,
307
- ) => {
388
+ getReport: () => Promise<ProofReport>,
389
+ ): Promise<ProofReport> => {
308
390
  try {
309
- return await getProved();
391
+ return await getReport();
310
392
  } catch (error) {
311
393
  if (error instanceof TlaUnsupportedError)
312
- return getUnproved(obligation, "unsupported", error.message);
313
- if (error instanceof TlaProverError) return getUnproved(obligation, "failed", error.message);
394
+ return { obligation: getUnproved(obligation, "unsupported", error.message), attempts: [] };
395
+ if (error instanceof TlaProverError)
396
+ return { obligation: getUnproved(obligation, "failed", error.message), attempts: [] };
314
397
  throw error;
315
398
  }
316
399
  };
317
400
 
401
+ interface ObligationsOptions {
402
+ workers?: number;
403
+ signal?: AbortSignal;
404
+ onReport?: (report: ProofReport, obligation: PrintedObligation) => void;
405
+ // What each prover concluded for each fingerprint, which tla-ts tlapm loads from tlapm's
406
+ // fingerprints file and saves back, as the run updates it.
407
+ results?: Map<string, Map<string, ProofAttempt>>;
408
+ // tlapm's --nofp and --nofpl: whether to reuse what was concluded for an obligation.
409
+ isFingerprinted?: (obligation: PrintedObligation) => boolean;
410
+ // tlapm's --noproving: reuse what was concluded, and run no backend.
411
+ isProving?: boolean;
412
+ }
413
+
414
+ // Proves obligations generated elsewhere, as tla-ts tlapm generates them like tlapm, reporting each
415
+ // with what each method concluded.
416
+ export const proveObligations = async (
417
+ obligations: PrintedObligation[],
418
+ options: ObligationsOptions,
419
+ ) => {
420
+ const count = options.workers ?? 1;
421
+ const pool = count > 1 && isWorkerSupported() ? await createDecidePool(count) : undefined;
422
+ const onAbort = () => pool?.terminate(options.signal?.reason);
423
+ options.signal?.addEventListener("abort", onAbort);
424
+ const run: ProveRun = {
425
+ tasks: [],
426
+ results: options.results ?? new Map(),
427
+ isFingerprinted: options.isFingerprinted ?? (() => true),
428
+ isProving: options.isProving ?? true,
429
+ lastByFingerprint: new Map(),
430
+ decide:
431
+ pool?.decide ?? (async (prover, text, decideOptions) => decide(prover, text, decideOptions)),
432
+ };
433
+ // A few obligations per worker prove at once, and each reports once every earlier one has.
434
+ const limit = pool ? count * 4 : 1;
435
+ const pending: Promise<ProofReport>[] = [];
436
+ const isSettled: boolean[] = [];
437
+ const running = new Set<Promise<void>>();
438
+ const reports: ProofReport[] = [];
439
+ const reportSettled = async () => {
440
+ while (isSettled[reports.length]) {
441
+ options.signal?.throwIfAborted();
442
+ const report = await pending[reports.length];
443
+ reports.push(report);
444
+ options.onReport?.(report, obligations[reports.length - 1]);
445
+ // HACK: backends on this thread run synchronously, so yield to let an abort land.
446
+ await new Promise((resolve) => setTimeout(resolve));
447
+ }
448
+ };
449
+ try {
450
+ for (const [index, obligation] of obligations.entries()) {
451
+ options.signal?.throwIfAborted();
452
+ const done = Promise.withResolvers<void>();
453
+ run.tasks.push({ done: done.promise, session: undefined });
454
+ const report = getResult(obligation, () =>
455
+ proveWithBackends(obligation, index, run, options.signal),
456
+ );
457
+ pending.push(report);
458
+ running.add(done.promise);
459
+ const settle = () => {
460
+ isSettled[index] = true;
461
+ running.delete(done.promise);
462
+ done.resolve();
463
+ };
464
+ void report.then(settle, settle);
465
+ if (running.size >= limit) await Promise.race(running);
466
+ await reportSettled();
467
+ }
468
+ await Promise.all(running);
469
+ await reportSettled();
470
+ } finally {
471
+ options.signal?.removeEventListener("abort", onAbort);
472
+ pool?.terminate(new TlaProverError("prove stopped"));
473
+ }
474
+ return reports;
475
+ };
476
+
318
477
  const getFromTlapm = (obligation: PrintedObligation, result: TlapmResult | undefined) =>
319
478
  result
320
479
  ? { ...getBase(obligation), ...result }
@@ -360,58 +519,10 @@ export const prove = async (options: ProveOptions): Promise<ProveResult> => {
360
519
  ...counts,
361
520
  };
362
521
  }
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 };
522
+ const reports = await proveObligations(obligations, {
523
+ workers: options.workers,
524
+ signal: options.signal,
525
+ onReport: (report) => options.onObligation?.(report.obligation),
526
+ });
527
+ return { obligations: reports.map((report) => report.obligation), ...counts };
417
528
  };
@@ -2,6 +2,12 @@
2
2
  // proves it.
3
3
  export type Verdict = "unsat" | "sat" | "unknown";
4
4
 
5
+ // A verdict, and how long the backend took, which tlapm reports as the time used.
6
+ export interface Decision {
7
+ verdict: Verdict;
8
+ timeUsedMs: number;
9
+ }
10
+
5
11
  export interface DecideOptions {
6
12
  timeLimitMs: number;
7
13
  signal?: AbortSignal;