tla-ts 0.0.10-dev.1ebc153bd → 0.0.10-dev.22f7435d3

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 (108) hide show
  1. package/dist/deciders.cjs +31 -31
  2. package/dist/deciders.js +30 -30
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +6 -2
  5. package/dist/index.d.ts +6 -2
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +1 -1
  9. package/dist/prove.cjs +68 -68
  10. package/dist/prove.js +61 -61
  11. package/dist/run.cjs +42 -42
  12. package/dist/run.js +44 -44
  13. package/dist/transfer.cjs +38 -38
  14. package/dist/transfer.js +29 -29
  15. package/dist/worker.cjs +7 -7
  16. package/dist/worker.js +7 -7
  17. package/package.json +1 -1
  18. package/src/bags.ts +5 -2
  19. package/src/builtins.ts +4 -3
  20. package/src/check-failure.ts +9 -6
  21. package/src/check-trace.ts +116 -64
  22. package/src/check.ts +343 -166
  23. package/src/cli/tlc-output.ts +1 -0
  24. package/src/cli/tlc.ts +3 -2
  25. package/src/codegen.ts +23 -8
  26. package/src/dependencies.ts +5 -4
  27. package/src/dfid.ts +13 -8
  28. package/src/environment.ts +1 -0
  29. package/src/evaluate.ts +58 -23
  30. package/src/exit-codes.ts +27 -13
  31. package/src/java-hash.ts +14 -0
  32. package/src/liveness-checker.ts +131 -81
  33. package/src/liveness.ts +48 -10
  34. package/src/parser.ts +6 -1
  35. package/src/prover/smt/solver/arith-rewriter.ts +11 -3
  36. package/src/prover/smt/solver/array-rewriter.ts +12 -2
  37. package/src/prover/smt/solver/asserted-formulas.ts +109 -63
  38. package/src/prover/smt/solver/ast.ts +5 -3
  39. package/src/prover/smt/solver/auf-solver.ts +23 -12
  40. package/src/prover/smt/solver/bit-blast-tactic.ts +26 -0
  41. package/src/prover/smt/solver/bound-simplifier.ts +10 -11
  42. package/src/prover/smt/solver/card2bv.ts +34 -15
  43. package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -23
  44. package/src/prover/smt/solver/context.ts +44 -11
  45. package/src/prover/smt/solver/dependent-expr-state.ts +19 -13
  46. package/src/prover/smt/solver/dt2bv-tactic.ts +36 -20
  47. package/src/prover/smt/solver/elim-bounds.ts +89 -0
  48. package/src/prover/smt/solver/elim-uncnstr.ts +1 -1
  49. package/src/prover/smt/solver/fd-solver.ts +146 -39
  50. package/src/prover/smt/solver/internalizer.ts +1 -1
  51. package/src/prover/smt/solver/lp/lp-settings.ts +4 -0
  52. package/src/prover/smt/solver/maxcore.ts +2 -1
  53. package/src/prover/smt/solver/maxsmt-base.ts +1 -0
  54. package/src/prover/smt/solver/model-checker.ts +1 -1
  55. package/src/prover/smt/solver/model-evaluator.ts +2 -0
  56. package/src/prover/smt/solver/model-generator.ts +82 -42
  57. package/src/prover/smt/solver/muz/dl-context.ts +12 -40
  58. package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +1 -1
  59. package/src/prover/smt/solver/muz/dl-rule.ts +6 -7
  60. package/src/prover/smt/solver/muz/hnf.ts +2 -2
  61. package/src/prover/smt/solver/muz/horn-tactic.ts +14 -1
  62. package/src/prover/smt/solver/muz/rule-properties.ts +22 -0
  63. package/src/prover/smt/solver/muz/spacer-prop-solver.ts +1 -1
  64. package/src/prover/smt/solver/opt-context.ts +12 -4
  65. package/src/prover/smt/solver/opt-pareto.ts +19 -0
  66. package/src/prover/smt/solver/opt-solver.ts +4 -3
  67. package/src/prover/smt/solver/parallel-tactic.ts +1 -1
  68. package/src/prover/smt/solver/poly-rewriter.ts +1 -0
  69. package/src/prover/smt/solver/proto-model.ts +4 -2
  70. package/src/prover/smt/solver/push-app-ite.ts +37 -0
  71. package/src/prover/smt/solver/qsat.ts +23 -8
  72. package/src/prover/smt/solver/rewriter-tpl.ts +7 -6
  73. package/src/prover/smt/solver/rewriter.ts +31 -25
  74. package/src/prover/smt/solver/sat/goal2sat.ts +0 -1
  75. package/src/prover/smt/solver/sat/inc-sat-solver.ts +56 -28
  76. package/src/prover/smt/solver/sat/sat-tactic.ts +13 -4
  77. package/src/prover/smt/solver/sat/sat2goal.ts +10 -7
  78. package/src/prover/smt/solver/smt-consequences.ts +1 -1
  79. package/src/prover/smt/solver/smt-kernel.ts +39 -4
  80. package/src/prover/smt/solver/smt-setup.ts +28 -4
  81. package/src/prover/smt/solver/smtlib-parser.ts +31 -4
  82. package/src/prover/smt/solver/smtlib-script.ts +219 -82
  83. package/src/prover/smt/solver/solve-eqs.ts +24 -17
  84. package/src/prover/smt/solver/solver.ts +18 -3
  85. package/src/prover/smt/solver/tactic.ts +132 -78
  86. package/src/prover/smt/solver/theory-bv.ts +11 -1
  87. package/src/prover/smt/solver/theory-fpa.ts +1 -1
  88. package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
  89. package/src/prover/smt/solver/theory-lra-internalize.ts +1 -0
  90. package/src/prover/smt/solver/theory-lra-propagate.ts +7 -1
  91. package/src/prover/smt/solver/theory-lra.ts +1 -0
  92. package/src/prover/smt/solver/tseitin-cnf.ts +1 -0
  93. package/src/resolve.ts +446 -167
  94. package/src/runtime.ts +4 -0
  95. package/src/search.ts +161 -92
  96. package/src/simulate.ts +165 -102
  97. package/src/spec-description.ts +3 -4
  98. package/src/standard-modules.ts +56 -22
  99. package/src/subexpressions.ts +168 -49
  100. package/src/successors.ts +6 -3
  101. package/src/symmetry.ts +1 -0
  102. package/src/tlc/random.ts +2 -1
  103. package/src/tlc/replay.ts +22 -14
  104. package/src/tlc/tlc-operators.ts +7 -7
  105. package/src/tlc/tlcext-operators.ts +26 -10
  106. package/src/tlc/trace-spec.ts +6 -20
  107. package/src/typed-tier.ts +9 -4
  108. package/src/values.ts +95 -57
@@ -2,6 +2,7 @@ import {
2
2
  getChildren,
3
3
  type AssumeProve,
4
4
  type Bound,
5
+ type Declaration,
5
6
  type Definition,
6
7
  type Expression,
7
8
  type Fact,
@@ -14,7 +15,7 @@ import {
14
15
  type SourceRange,
15
16
  type StepBody,
16
17
  } from "./ast";
17
- import { formatRange, TlaSyntaxError } from "./errors";
18
+ import { formatRange, TlaSyntaxError, type SanyError } from "./errors";
18
19
  import { getSourceName, mapChildren, mapDefinition } from "./instances";
19
20
  import { resolveSynonym } from "./tlc/jar-data";
20
21
  import {
@@ -27,8 +28,13 @@ import {
27
28
  isBefore,
28
29
  LOCAL_RESOLUTION,
29
30
  nullSelections,
31
+ operatorSelectionErrors,
30
32
  selectionErrors,
33
+ selectionArguments,
31
34
  selections,
35
+ selectionSources,
36
+ SELECTION_NAME,
37
+ factReferences,
32
38
  type NullSelection,
33
39
  type SelectionArguments,
34
40
  } from "./resolve";
@@ -53,7 +59,15 @@ interface SelectingName {
53
59
  argumentRanges?: (SourceRange | undefined)[];
54
60
  }
55
61
 
56
- const SELECTION_NAME = "$subexpression";
62
+ const declaredArities = new WeakMap<Definition, number>();
63
+
64
+ const setDeclaredArities = (declarations: Declaration[], definitions: Definition[]) =>
65
+ declarations.forEach((declaration) => {
66
+ const definition = definitions.find(
67
+ (candidate) => candidate.isRecursive && candidate.name === declaration.name,
68
+ );
69
+ if (definition) declaredArities.set(definition, declaration.arity);
70
+ });
57
71
 
58
72
  const getSelectorName = (selector: Selector) => {
59
73
  switch (selector.kind) {
@@ -108,7 +122,12 @@ const wrapLets = (body: Expression, lets: Definition[][], location: SourceLocati
108
122
  const createApplication = (selection: Selection, body: Expression): Expression => {
109
123
  const location = selection.location;
110
124
  const wrapped = wrapLets(body, selection.lets, location);
111
- if (selection.params.length === 0) return wrapped;
125
+ if (selection.params.length === 0 && wrapped !== body) return wrapped;
126
+ if (selection.params.length === 0) {
127
+ const copy = { ...body };
128
+ selectionSources.set(copy, selectionSources.get(body) ?? body);
129
+ return copy;
130
+ }
112
131
  if (selection.args.length === 0) {
113
132
  if (selection.params.some((param) => param.arity > 0)) {
114
133
  throw createError("Selected an operator with operator parameters as an argument", location);
@@ -192,15 +211,22 @@ const selectSubexpression = (
192
211
  let isFailed = false;
193
212
  const nulls: NullSelection[] = [];
194
213
  const applications: SelectionArguments[] = [];
195
- const checkNull = (node: Expression | undefined, message: string) => {
214
+ let expressionError: SanyError | undefined;
215
+ const deferToExpression = (range: SourceRange | undefined, message: string) => {
216
+ if (!expressionError && range) expressionError = { location: formatRange(range), message };
217
+ };
218
+ const checkNull = (node: Expression | undefined, message: string, isEmpty = false) => {
196
219
  const range = reference.selectorRanges?.[selectorIndex];
197
- if (node && range) nulls.push({ node, error: { location: formatRange(range), message } });
220
+ if (node && range) {
221
+ nulls.push({ node, error: { location: formatRange(range), message }, isEmpty });
222
+ }
198
223
  };
199
224
 
200
225
  const report = (range: SourceRange | undefined, message: string) => {
201
226
  if (!range) throw createError(message, location);
202
227
  selectionErrors.set(reference, {
203
228
  applications,
229
+ nulls,
204
230
  error: { location: formatRange(range), message },
205
231
  });
206
232
  isFailed = true;
@@ -234,22 +260,33 @@ const selectSubexpression = (
234
260
  return undefined;
235
261
  }
236
262
  const definition = found.definition;
237
- const arity = getArity(definition);
263
+ const arity = declaredArities.get(definition) ?? getArity(definition);
238
264
  const isLast = selectorIndex === selectors.length - 1;
239
- if (found.args.length !== arity && (!isOperator || found.args.length !== 0)) {
240
- return report(
241
- reference.argumentRanges?.[selectorIndex] ?? location.range,
242
- `The operator ${resolveSynonym(found.name)} requires ${arity - found.earlier} arguments.`,
243
- );
265
+ if (found.args.length !== arity) {
266
+ const range = reference.argumentRanges?.[selectorIndex] ?? location.range;
267
+ const message = `The operator ${resolveSynonym(found.name)} requires ${arity - found.earlier} arguments.`;
268
+ if (!isOperator || found.args.length !== 0) return report(range, message);
269
+ deferToExpression(range, message);
244
270
  }
271
+ found.args.forEach((arg, index) =>
272
+ selectionArguments.set(arg, {
273
+ operator: found.name,
274
+ index,
275
+ range: reference.selectorRanges?.[selectorIndex],
276
+ }),
277
+ );
245
278
  if (isLast) return { definition, args: found.args };
279
+ const range = definition.location.range;
280
+ const isUndefined =
281
+ declaredArities.has(definition) &&
282
+ range?.module === location.range?.module &&
283
+ isBefore(location, definition.location);
246
284
  applications.push({
247
285
  operator: found.name,
248
286
  range: reference.selectorRanges?.[selectorIndex],
249
287
  args: found.args,
250
- params: getParamArities(definition),
288
+ params: isUndefined ? Array(arity).fill(0) : getParamArities(definition),
251
289
  });
252
- const range = definition.location.range;
253
290
  if (
254
291
  definition.isRecursive &&
255
292
  range &&
@@ -279,8 +316,10 @@ const selectSubexpression = (
279
316
  const found = current && findLabel(current, selector.name, []);
280
317
  if (!found) return report(range, `Cannot find label \`${selector.name}'.`);
281
318
  const params = found.label.params;
282
- if (!isOperator && selector.args.length !== params.length) {
283
- return report(range, `Label \`${selector.name}' used with wrong number of arguments.`);
319
+ if (selector.args.length !== params.length) {
320
+ const message = `Label \`${selector.name}' used with wrong number of arguments.`;
321
+ if (!isOperator) return report(range, message);
322
+ deferToExpression(range, message);
284
323
  }
285
324
  selection.lets.push(...found.lets);
286
325
  selection.params.push(...params.map((name) => ({ name, arity: 0 })));
@@ -302,15 +341,16 @@ const selectSubexpression = (
302
341
  (current.kind === "Apply" && current.operator.includes("!"))) &&
303
342
  (currentRange?.module !== location.range?.module || isBefore(current.location, location))
304
343
  ) {
305
- const selected = resolveSubexpressions(current, lookup, isGoal);
344
+ const selected = selectionErrors.has(current)
345
+ ? current
346
+ : resolveSubexpressions(current, lookup, isGoal);
347
+ const noOperands = `Choosing operand \`${getSelectorName(selector)}' of subexpression with no operands.`;
306
348
  if (selectionErrors.has(selected)) {
307
- report(
308
- range,
309
- `Choosing operand \`${getSelectorName(selector)}' of subexpression with no operands.`,
310
- );
349
+ report(range, noOperands);
311
350
  return;
312
351
  }
313
352
  if (selections.has(selected) && selected.kind !== "Lambda") {
353
+ checkNull(selected, noOperands, true);
314
354
  current =
315
355
  selected.kind === "Let" &&
316
356
  selected.body.kind === "Apply" &&
@@ -337,6 +377,13 @@ const selectSubexpression = (
337
377
  }
338
378
  const binder = getBinder(current);
339
379
  if (binder && (selector.kind === "Bound" || selector.kind === "At")) {
380
+ if (selector.kind === "Bound" && selector.args.length !== binder.names.length) {
381
+ report(
382
+ range,
383
+ `Selector with ${selector.args.length} argument(s) used for quantifier with ${binder.names.length} bound identifier(s).`,
384
+ );
385
+ return;
386
+ }
340
387
  selection.params.push(...binder.names.map((name) => ({ name, arity: 0 })));
341
388
  if (selector.kind === "Bound") {
342
389
  selection.args.push(...selector.args);
@@ -381,6 +428,14 @@ const selectSubexpression = (
381
428
  current = chosen;
382
429
  return;
383
430
  }
431
+ if (
432
+ current.kind === "Except" &&
433
+ selector.kind === "Operand" &&
434
+ getOperandIndex(selector.operand, current.updates.length + 1) > 0
435
+ ) {
436
+ report(range, "Selecting subexpression of an EXCEPT not yet implemented.");
437
+ return;
438
+ }
384
439
  const operands = binder ? binder.sets : getOperands(current);
385
440
  if (!operands && current.kind !== "Identifier") {
386
441
  report(range, "Selecting subexpression of expression that has none.");
@@ -407,7 +462,11 @@ const selectSubexpression = (
407
462
  if (selector.kind === "Name") {
408
463
  while (current?.kind === "Label") current = current.body;
409
464
  if (current?.kind !== "Let") {
410
- throw createError("A name selector here must be from a LET clause", location);
465
+ report(
466
+ reference.selectorRanges?.[selectorIndex - 1],
467
+ "A name selector here must be from a LET clause.",
468
+ );
469
+ return reference;
411
470
  }
412
471
  selection.lets.push(current.definitions);
413
472
  const last = enterDefinition(current.definitions);
@@ -426,6 +485,11 @@ const selectSubexpression = (
426
485
  current,
427
486
  "Subexpression selection failed, probably due to an error in the selected expression.",
428
487
  );
488
+ checkNull(
489
+ current,
490
+ `Choosing operand \`${getSelectorName(selector)}' of subexpression with no operands.`,
491
+ true,
492
+ );
429
493
  }
430
494
  selectOperand(selector);
431
495
  checkNull(
@@ -438,12 +502,17 @@ const selectSubexpression = (
438
502
  if (!current) throw createError("Subexpression selection failed", location);
439
503
  const params = selection.params.length;
440
504
  const args = selection.args.length;
441
- if (params !== args && (!isOperator || args > 0)) {
442
- report(location.range, `Expected ${params} arguments but found ${args}.`);
443
- return reference;
505
+ if (params !== args) {
506
+ const message = `Expected ${params} arguments but found ${args}.`;
507
+ if (!isOperator || args > 0) {
508
+ report(location.range, message);
509
+ return reference;
510
+ }
511
+ deferToExpression(location.range, message);
444
512
  }
445
513
  const selected = createApplication(selection, current);
446
514
  nullSelections.set(selected, nulls);
515
+ if (expressionError) operatorSelectionErrors.set(selected, expressionError);
447
516
  return selected;
448
517
  };
449
518
 
@@ -473,18 +542,25 @@ const resolveSubexpressions = (
473
542
  ): Expression => {
474
543
  if (!containsReference(expression)) return expression;
475
544
  if (expression.kind === "Let") {
545
+ if (expression.recursive) setDeclaredArities(expression.recursive, expression.definitions);
476
546
  const local: Lookup = (name) =>
477
547
  expression.definitions.find((definition) => definition.name === name) ?? lookup(name);
478
- const mapped = mapChildren(expression, (child) => resolveSubexpressions(child, local, isGoal));
548
+ const isLocalGoal = (name: string) =>
549
+ !expression.definitions.some((definition) => definition.name === name) && isGoal(name);
550
+ const mapped = mapChildren(expression, (child) =>
551
+ resolveSubexpressions(child, local, isLocalGoal),
552
+ );
479
553
  return mapped.kind === "Let" && mapped.instances
480
554
  ? {
481
555
  ...mapped,
482
- instances: mapped.instances.map((instance) => resolveInstance(instance, local, isGoal)),
556
+ instances: mapped.instances.map((instance) =>
557
+ resolveInstance(instance, local, isLocalGoal),
558
+ ),
483
559
  }
484
560
  : mapped;
485
561
  }
486
562
  const callee = expression.kind === "Apply" ? lookup(expression.operator) : undefined;
487
- const mapped =
563
+ const rebuilt =
488
564
  expression.kind === "Apply"
489
565
  ? {
490
566
  ...expression,
@@ -498,6 +574,10 @@ const resolveSubexpressions = (
498
574
  ),
499
575
  }
500
576
  : mapChildren(expression, (child) => resolveSubexpressions(child, lookup, isGoal));
577
+ const children = getChildren(expression);
578
+ const mapped = getChildren(rebuilt).every((child, index) => child === children[index])
579
+ ? expression
580
+ : rebuilt;
501
581
  const selectors =
502
582
  mapped.kind === "Subexpression"
503
583
  ? mapped.selectors
@@ -507,7 +587,20 @@ const resolveSubexpressions = (
507
587
  ? getQualifiedSelectors(mapped.operator, mapped.args, mapped.argumentCounts, lookup)
508
588
  : undefined;
509
589
  const first = selectors?.[0];
510
- if (!selectors || (first?.kind === "Name" && isGoal(first.name))) return mapped;
590
+ if (!selectors) return mapped;
591
+ const nameCount = selectors.findIndex((selector) => selector.kind !== "Name");
592
+ const names = selectors.slice(0, nameCount === -1 ? selectors.length : nameCount);
593
+ const isQualified = names.some(
594
+ (_name, index) =>
595
+ index > 0 &&
596
+ lookup(
597
+ names
598
+ .slice(0, index + 1)
599
+ .map(getSelectorName)
600
+ .join("!"),
601
+ ),
602
+ );
603
+ if (first?.kind === "Name" && isGoal(first.name) && !isQualified) return mapped;
511
604
  if (first?.kind === "Name" && selectors[1].kind !== "Name" && !lookup(first.name)) return mapped;
512
605
  const selected = selectSubexpression(selectors, lookup, isGoal, mapped, isOperator);
513
606
  if (selected !== mapped) selections.add(selected);
@@ -564,6 +657,10 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
564
657
  ];
565
658
  });
566
659
  const own = new Set([...module.definitions, ...clauses]);
660
+ setDeclaredArities(
661
+ module.units.flatMap((unit) => (unit.kind === "Recursive" ? unit.declarations : [])),
662
+ module.definitions,
663
+ );
567
664
  const recursive = new Map(
568
665
  module.units.flatMap((unit): [string, SourceLocation][] =>
569
666
  unit.kind === "Recursive"
@@ -595,13 +692,17 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
595
692
  getVisibleLookup(definitionLookup, location),
596
693
  (name) => isGoal(name) && clauses.every((clause) => clause.name !== name),
597
694
  );
598
- const createProofResolver = (location: SourceLocation) => {
599
- const lookup = getVisibleLookup(proofLookup, location);
695
+ const createProofResolver = (lookup: Lookup) => {
600
696
  const isProofGoal = (name: string) => isGoal(name) || !lookup(name);
601
697
  const resolveInProof = (expression: Expression) =>
602
698
  resolveSubexpressions(expression, lookup, isProofGoal);
603
- const resolveFacts = (facts: Fact[]) =>
604
- facts.map((fact) => (fact.kind === "ModuleFact" ? fact : resolveInProof(fact)));
699
+ const resolveFacts = (facts: Fact[]) => {
700
+ const resolved = facts.map((fact) =>
701
+ fact.kind === "ModuleFact" ? fact : resolveInProof(fact),
702
+ );
703
+ factReferences.set(resolved, facts);
704
+ return resolved;
705
+ };
605
706
  const resolveBounds = (bounds: Bound[]) =>
606
707
  bounds.map((bound) => ({ ...bound, set: resolveInProof(bound.set) }));
607
708
  const resolveStatement = (statement: Expression | AssumeProve): Expression | AssumeProve =>
@@ -647,37 +748,55 @@ export const resolveModuleSubexpressions = (module: Module, available: Definitio
647
748
  return body;
648
749
  }
649
750
  };
650
- const resolveProof = (proof: Proof | undefined): Proof | undefined =>
651
- proof?.kind === "By"
652
- ? { ...proof, facts: resolveFacts(proof.facts) }
653
- : proof?.kind === "Steps"
654
- ? {
655
- ...proof,
656
- steps: proof.steps.map((step) => ({
657
- ...step,
658
- body: resolveStep(step.body),
659
- proof: resolveProof(step.proof),
660
- })),
661
- }
662
- : proof;
663
- return { resolveFacts, resolveStatement, resolveProof };
751
+ const resolveProof = (proof: Proof | undefined): Proof | undefined => {
752
+ if (proof?.kind === "By") return { ...proof, facts: resolveFacts(proof.facts) };
753
+ if (proof?.kind !== "Steps") return proof;
754
+ let resolver = { lookup, resolveStep, resolveProof };
755
+ return {
756
+ ...proof,
757
+ steps: proof.steps.map((step) => {
758
+ const resolved = {
759
+ ...step,
760
+ body: resolver.resolveStep(step.body),
761
+ proof: resolver.resolveProof(step.proof),
762
+ };
763
+ if (step.body.kind === "Define") {
764
+ const definitions = step.body.definitions.flatMap((definition) =>
765
+ definition.kind === "Instance" ? [] : [definition],
766
+ );
767
+ const outer = resolver.lookup;
768
+ resolver = createProofResolver(
769
+ (name) => definitions.find((definition) => definition.name === name) ?? outer(name),
770
+ );
771
+ }
772
+ return resolved;
773
+ }),
774
+ };
775
+ };
776
+ return { lookup, resolveFacts, resolveStatement, resolveStep, resolveProof };
664
777
  };
665
778
  const resolved = {
666
779
  ...module,
667
780
  theorems: module.theorems.map((theorem) => {
668
- const resolver = createProofResolver(theorem.location);
781
+ const resolver = createProofResolver(getVisibleLookup(proofLookup, theorem.location));
669
782
  return {
670
783
  ...theorem,
671
784
  statement:
672
785
  theorem.name !== undefined && theorem.statement.kind !== "AssumeProve"
673
786
  ? theorem.statement
674
787
  : resolver.resolveStatement(theorem.statement),
675
- proof: resolver.resolveProof(theorem.proof),
788
+ proof:
789
+ theorem.proof &&
790
+ createProofResolver(getVisibleLookup(proofLookup, theorem.proof.location)).resolveProof(
791
+ theorem.proof,
792
+ ),
676
793
  };
677
794
  }),
678
795
  uses: module.uses.map((use) => ({
679
796
  ...use,
680
- facts: createProofResolver(use.location).resolveFacts(use.facts),
797
+ facts: createProofResolver(getVisibleLookup(proofLookup, use.location)).resolveFacts(
798
+ use.facts,
799
+ ),
681
800
  })),
682
801
  };
683
802
  const hasReferences =
package/src/successors.ts CHANGED
@@ -1204,7 +1204,9 @@ export const isEnabled = (expression: Expression, context: Context): boolean =>
1204
1204
  );
1205
1205
  }
1206
1206
  const environment = context.environment;
1207
- if (environment.callStack) return replayEnabled(expression, context.scope, state, environment);
1207
+ if (environment.callStack || (environment.coverage && environment.coverage.current !== -1)) {
1208
+ return replayEnabled(expression, context.scope, state, environment);
1209
+ }
1208
1210
  const run: Run = {
1209
1211
  environment,
1210
1212
  isInit: false,
@@ -1341,6 +1343,7 @@ export const hasIncompleteSuccessor = (
1341
1343
  generator: undefined,
1342
1344
  successors: 0,
1343
1345
  };
1346
+ // TLC's Tool#getState generates every successor of the action before it compares them.
1344
1347
  getRunner(expression, environment, false)(
1345
1348
  run,
1346
1349
  scope,
@@ -1348,10 +1351,10 @@ export const hasIncompleteSuccessor = (
1348
1351
  undefined,
1349
1352
  false,
1350
1353
  (partial) => {
1351
- if (!isComplete(partial)) run.isStopped = true;
1354
+ if (!isComplete(partial)) run.successors++;
1352
1355
  },
1353
1356
  );
1354
- return run.isStopped;
1357
+ return run.successors > 0;
1355
1358
  };
1356
1359
 
1357
1360
  export const getSuccessors = (
package/src/symmetry.ts CHANGED
@@ -139,6 +139,7 @@ export const getSymmetryGroup = (
139
139
  `The set${isPlural ? "s" : ""} ${names.join(", and ")} ${isPlural ? "have" : "has"} been defined to be a symmetry set but contain${isPlural ? "" : "s"} less than two elements.`,
140
140
  );
141
141
  };
142
+ // Deviation from TLC: MVPerms#permutationSubgroup sizes a util.Set one below the number of permutations, which throws for fewer than two.
142
143
  if (permutedSets.length >= elements.length) {
143
144
  warnSmallSets(permutedSets);
144
145
  return undefined;
package/src/tlc/random.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { toEnumeratedSet } from "../builtins";
2
2
  import { TlaEvaluationError } from "../errors";
3
+ import { prettyPrint } from "../pretty-print";
3
4
  import type { TlcVersion } from "./version";
4
5
  import {
5
6
  createRecord,
@@ -226,7 +227,7 @@ export const getRandomElement = (set: Value, random: JavaRandom): Value => {
226
227
  const offset = Math.floor(random.nextDouble() * size);
227
228
  if (offset >= size) {
228
229
  throw new TlaEvaluationError(
229
- `Attempted to retrieve out-of-bounds element from the interval value ${formatValue(set)}.`,
230
+ `Attempted to retrieve out-of-bounds element from the interval value ${prettyPrint(formatValue(set))}.`,
230
231
  );
231
232
  }
232
233
  return set.low + offset;
package/src/tlc/replay.ts CHANGED
@@ -61,7 +61,7 @@ interface Replay {
61
61
  isEnabled: boolean;
62
62
  current: Value[] | undefined;
63
63
  onState: (state: Value[]) => void;
64
- onIncomplete: ((state: State) => void) | undefined;
64
+ onIncomplete: ((state: State) => boolean) | undefined;
65
65
  }
66
66
 
67
67
  const withFrame = <Result>(replay: Replay, expression: Expression, run: () => Result): Result => {
@@ -216,8 +216,7 @@ const proceedItems = (
216
216
  ): boolean => {
217
217
  if (items === undefined) {
218
218
  if (isComplete(partial)) return addState(replay, partial, node);
219
- replay.onIncomplete?.(partial);
220
- return false;
219
+ return replay.onIncomplete?.(partial) ?? false;
221
220
  }
222
221
  if (isComplete(partial) && isAssignedShortcut(replay)) {
223
222
  return proceedAssigned(replay, items, partial, node);
@@ -715,6 +714,7 @@ const visitUnchanged = (
715
714
  if (index !== undefined) {
716
715
  const value = replay.current?.[index];
717
716
  if (value === undefined) return false;
717
+ if (replay.isEnabled && replay.coverage) countCoverage(replay.coverage, node);
718
718
  const assigned = partial[index];
719
719
  if (assigned !== undefined && !isEqualLikeTlc(assigned, value)) {
720
720
  warnChangedUnchanged(replay.environment, index, expression);
@@ -779,11 +779,12 @@ const createReplay = (
779
779
  kind: "init" | "next" | "enabled",
780
780
  current: Value[] | undefined,
781
781
  onState: (state: Value[]) => void,
782
- onIncomplete?: (state: State) => void,
782
+ onIncomplete?: (state: State) => boolean,
783
783
  ): Replay => ({
784
784
  environment,
785
785
  callStack: environment.callStack,
786
- coverage: environment.coverage,
786
+ coverage:
787
+ kind === "enabled" && environment.coverage?.current === -1 ? undefined : environment.coverage,
787
788
  isInit: kind === "init",
788
789
  isEnabled: kind === "enabled",
789
790
  current,
@@ -799,17 +800,19 @@ export const replayEnabled = (
799
800
  state: Value[],
800
801
  environment: Environment,
801
802
  ) => {
802
- const replay = createReplay(environment, "enabled", state, () => {
803
+ const found = () => {
803
804
  throw ENABLED_FOUND;
804
- });
805
+ };
806
+ const node = environment.coverage?.current ?? -1;
807
+ const replay = createReplay(environment, "enabled", state, found, found);
805
808
  const callStack = replay.callStack;
806
809
  const depth = callStack?.expressions.length ?? 0;
807
810
  try {
808
811
  if (expression.kind === "AngleAction" && expression.location === expression.action.location) {
809
- const changed = createItem("Changed", expression.subscript, scope, -1, undefined);
810
- visit(replay, expression.action, changed, scope, getEmptyState(environment), -1);
812
+ const changed = createItem("Changed", expression.subscript, scope, node, undefined);
813
+ visit(replay, expression.action, changed, scope, getEmptyState(environment), node);
811
814
  } else {
812
- visit(replay, expression, undefined, scope, getEmptyState(environment), -1);
815
+ visit(replay, expression, undefined, scope, getEmptyState(environment), node);
813
816
  }
814
817
  return false;
815
818
  } catch (error) {
@@ -826,7 +829,7 @@ export const replayInitialStates = (
826
829
  inits: (ReplayAction & { node?: number })[],
827
830
  environment: Environment,
828
831
  onState: (state: Value[]) => void,
829
- onIncomplete?: (state: State) => void,
832
+ onIncomplete?: (state: State) => boolean,
830
833
  ) => {
831
834
  const [first, ...others] = inits;
832
835
  if (first === undefined) return;
@@ -838,16 +841,21 @@ export const replayInitialStates = (
838
841
  visit(replay, first.expression, rest, first.scope, getEmptyState(environment), first.node ?? -1);
839
842
  };
840
843
 
841
- export const replaySuccessors = <Action extends ReplayAction>(
844
+ export const replaySuccessors = <Action extends ReplayAction & { node?: number }>(
842
845
  actions: Action[],
843
846
  state: Value[],
844
847
  environment: Environment,
845
848
  onState: (state: Value[], action: Action) => void,
846
849
  ) => {
847
850
  if (actions.some((action) => LITERAL_KINDS.has(action.expression.kind))) return;
851
+ const coverage = environment.coverage;
848
852
  actions.forEach((action) => {
849
853
  const successors: Value[][] = [];
850
- walkSuccessors(action, -1, state, environment, (next) => successors.push(next));
854
+ const node = action.node ?? -1;
855
+ walkSuccessors(action, node, state, environment, (next) => {
856
+ if (coverage && node !== -1) coverage.primary[node]++;
857
+ successors.push(next);
858
+ });
851
859
  successors.forEach((successor) => onState(successor, action));
852
860
  });
853
861
  };
@@ -858,7 +866,7 @@ export const walkSuccessors = (
858
866
  state: Value[],
859
867
  environment: Environment,
860
868
  onState: (state: Value[]) => void,
861
- onIncomplete?: (state: State) => void,
869
+ onIncomplete?: (state: State) => boolean,
862
870
  ) => {
863
871
  const replay = createReplay(environment, "next", state, onState, onIncomplete);
864
872
  visit(replay, action.expression, undefined, action.scope, getEmptyState(environment), node);
@@ -16,12 +16,13 @@ import {
16
16
  toEnumeratedSet,
17
17
  } from "../builtins";
18
18
  import { getNamedRegisters, NAMED_REGISTER_PREFIX, type CheckerRuntime } from "../runtime";
19
- import { createActionRecord, setTlcValue } from "./tlcext-operators";
19
+ import { createActionRecord, createTlcRecord, setTlcValue } from "./tlcext-operators";
20
20
  import { getRandomElement } from "./random";
21
21
  import {
22
22
  ANY_SET,
23
23
  createFunction,
24
24
  createSet,
25
+ createSortedFunction,
25
26
  createTuple,
26
27
  createWrittenFunction,
27
28
  formatNormalizedCopies,
@@ -34,7 +35,6 @@ import {
34
35
  toFiniteSet,
35
36
  type Value,
36
37
  rankString,
37
- setFunctionClass,
38
38
  } from "../values";
39
39
  import { getFingerprintPolynomial } from "./fingerprint";
40
40
  import { zeroPad } from "./messages";
@@ -47,9 +47,6 @@ const getRegisterFunction = <Key extends Value>(registers: Map<Key, Value>) =>
47
47
  [...registers].map((entry): [Value, Value] => [entry[0], createTuple([entry[1]])]),
48
48
  );
49
49
 
50
- const createTlcRecord = (fields: [string, Value][]) =>
51
- setFunctionClass(createWrittenFunction(fields), "record");
52
-
53
50
  const CHECKER_KEYS = new Set(["diameter", "generated", "distinct", "queue", "stats", "config"]);
54
51
  const TLC_174_KEYS = new Set(["diameter", "distinct", "queue", "duration", "level"]);
55
52
  const CHECKER_KEYS_174 = new Set(["diameter", "distinct", "queue", "level"]);
@@ -135,7 +132,7 @@ const getTlcValue = (runtime: CheckerRuntime, key: Value, context: Context | und
135
132
  }
136
133
  if (typeof key !== "string") {
137
134
  throw new TlaModuleError(
138
- `The argument of TLCGet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
135
+ `The argument of TLCGet should be a nonnegative integer, but instead it is:\n${prettyPrint(formatValue(key))}`,
139
136
  );
140
137
  }
141
138
  return getRuntimeValue(runtime, key, context?.state);
@@ -162,8 +159,11 @@ const normalizeDeeply = (value: Value): void => {
162
159
  const convertLikeTlcEval = (value: Value): Value => {
163
160
  if (typeof value !== "object") return value;
164
161
  switch (value.kind) {
165
- case "ModelValue":
166
162
  case "Function":
163
+ return value.functionClass === undefined
164
+ ? value
165
+ : createSortedFunction(value.domain, value.range);
166
+ case "ModelValue":
167
167
  case "MixedSet":
168
168
  case "InfiniteSet":
169
169
  case "SequenceSet":