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
@@ -14,10 +14,12 @@ import {
14
14
  import { mkNot } from "./ast-util";
15
15
  import { simplifyInjAxiom } from "./inj-axiom";
16
16
  import { MacroManager } from "./macro-finder";
17
+ import { ElimBoundsRw } from "./elim-bounds";
17
18
  import { DefinedNames, Nnf } from "./nnf";
18
19
  import { PatternInference } from "./pattern-inference";
19
20
  import { MaxBvSharing } from "./maximize-ac-sharing";
20
21
  import { PullQuant } from "./pull-quant";
22
+ import { NgPushAppIteRw } from "./push-app-ite";
21
23
  import { RefVector } from "./ref-vector";
22
24
  import { KERNEL_REWRITER_OPTIONS, Rewriter } from "./rewriter";
23
25
  import { RewriterTpl } from "./rewriter-tpl";
@@ -194,10 +196,14 @@ export class AssertedFormulas {
194
196
  private isNnfCnf = true;
195
197
  private isEq2Ineq = false;
196
198
  private isEliminatingTermIte = false;
199
+ private isLiftingNonGroundIte = false;
200
+ private isEliminatingBounds = false;
197
201
  private names: DefinedNames;
198
202
  private termIteElim: ElimTermIteRw;
199
203
  private patternInference: PatternInferenceRw;
200
204
  private bvSharing: MaxBvSharing;
205
+ private ngLiftIte: NgPushAppIteRw;
206
+ private boundsElim: ElimBoundsRw;
201
207
  hasQuantifiers = false;
202
208
  readonly macros: MacroManager;
203
209
  isInconsistent = false;
@@ -219,6 +225,8 @@ export class AssertedFormulas {
219
225
  this.termIteElim = new ElimTermIteRw(manager, this.names);
220
226
  this.patternInference = new PatternInferenceRw(manager, new PatternInference(manager));
221
227
  this.bvSharing = new MaxBvSharing(manager);
228
+ this.ngLiftIte = new NgPushAppIteRw(manager);
229
+ this.boundsElim = new ElimBoundsRw(manager);
222
230
  }
223
231
 
224
232
  push() {
@@ -275,6 +283,8 @@ export class AssertedFormulas {
275
283
  this.isNnfCnf = config.isNnfCnf;
276
284
  this.isEq2Ineq = config.isEq2Ineq;
277
285
  this.isEliminatingTermIte = config.isEliminatingTermIte;
286
+ this.isLiftingNonGroundIte = config.isLiftingNonGroundIte;
287
+ this.isEliminatingBounds = config.isEliminatingBounds;
278
288
  this.isFindingMacros ||= config.isFindingMacros;
279
289
  }
280
290
 
@@ -282,6 +292,8 @@ export class AssertedFormulas {
282
292
  this.isNnfCnf = config.isNnfCnf;
283
293
  this.isEq2Ineq = config.isEq2Ineq;
284
294
  this.isEliminatingTermIte = config.isEliminatingTermIte;
295
+ this.isLiftingNonGroundIte = config.isLiftingNonGroundIte;
296
+ this.isEliminatingBounds = config.isEliminatingBounds;
285
297
  this.isFindingMacros = config.isFindingMacros;
286
298
  this.isReducePending = true;
287
299
  }
@@ -294,59 +306,51 @@ export class AssertedFormulas {
294
306
  return this.formulas.items;
295
307
  }
296
308
 
297
- // Like internalize_assertions, nothing is committed once the formulas are inconsistent.
309
+ getQhead() {
310
+ return this.qhead;
311
+ }
312
+
313
+ commit(qhead = this.formulas.size) {
314
+ const committed = this.formulas.items.slice(this.qhead, qhead);
315
+ this.macros.markForbidden(committed);
316
+ for (const formula of committed) this.substitution.update(formula);
317
+ this.qhead = qhead;
318
+ }
319
+
298
320
  reduce() {
299
- if (this.qhead === this.formulas.size) return [];
300
- if (!this.isInconsistent && (this.hasQuantifiers || this.isPreprocessing)) this.reduceCore();
301
- if (this.isInconsistent) return [];
302
- const result = this.formulas.items.slice(this.qhead);
303
- this.macros.markForbidden(result);
304
- for (const formula of result) this.substitution.update(formula);
305
- this.qhead = this.formulas.size;
306
- return result;
307
- }
308
-
309
- private reduceCore() {
310
- if (this.macros.hasMacros()) this.findMacros();
321
+ if (this.isInconsistent || this.isCanceled() || this.qhead === this.formulas.size) return;
322
+ if (!this.hasQuantifiers && !this.isPreprocessing) return;
323
+ const isFindingMacros = this.isFindingMacros && this.hasQuantifiers;
324
+ if (this.macros.hasMacros()) this.invoke(isFindingMacros, () => this.findMacros());
311
325
  this.setEliminateAnd(false);
312
- this.propagateValues();
313
- if (this.isInconsistent) return;
314
- this.findMacros();
315
- if (this.isInconsistent) return;
316
- if (this.isNnfCnf || this.hasQuantifiers) {
317
- this.isReducePending = true;
318
- this.nnfCnf();
319
- if (this.isInconsistent) return;
320
- }
326
+ if (!this.invoke(true, () => this.propagateValues())) return;
327
+ if (!this.invoke(isFindingMacros, () => this.findMacros())) return;
328
+ if (!this.invoke(this.isNnfCnf || this.hasQuantifiers, () => this.nnfCnf())) return;
321
329
  this.setEliminateAnd(true);
322
- this.reduceAsserted();
323
- if (this.isInconsistent) return;
324
- if (this.isEliminatingTermIte) {
325
- this.elimTermIte();
326
- if (this.isInconsistent) return;
327
- }
328
- if (this.hasQuantifiers && this.simplifyFormulas((formula) => this.refineInjAxiom(formula)))
329
- this.isReducePending = true;
330
- if (this.isInconsistent) return;
331
- this.findMacros();
332
- if (this.isInconsistent) return;
333
- if (
334
- this.hasQuantifiers &&
335
- this.simplifyFormulas((formula) => this.patternInference.rewrite(formula))
336
- )
337
- this.isReducePending = true;
338
- if (this.isInconsistent) return;
339
- if (this.simplifyFormulas((formula) => this.bvSharing.rewrite(formula)))
340
- this.isReducePending = true;
341
- if (this.isInconsistent) return;
342
- this.reduceAsserted();
343
- if (this.isInconsistent) return;
344
- this.isReducePending = true;
345
- this.flattenClauses();
346
- if (this.isInconsistent) return;
330
+ if (!this.invoke(true, () => this.reduceAsserted())) return;
331
+ if (!this.invoke(this.isLiftingNonGroundIte, () => this.liftNonGroundIte())) return;
332
+ if (!this.invoke(this.isEliminatingTermIte, () => this.elimTermIte())) return;
333
+ if (!this.invoke(this.hasQuantifiers, () => this.refineInjAxioms())) return;
334
+ if (!this.invoke(isFindingMacros, () => this.findMacros())) return;
335
+ if (!this.invoke(this.isEliminatingBounds && this.hasQuantifiers, () => this.elimBounds()))
336
+ return;
337
+ if (!this.invoke(this.hasQuantifiers, () => this.inferPatterns())) return;
338
+ if (!this.invoke(true, () => this.maximizeBvSharing())) return;
339
+ if (!this.invoke(true, () => this.reduceAsserted())) return;
340
+ if (!this.invoke(true, () => this.flattenClauses())) return;
347
341
  this.flushCache();
348
342
  }
349
343
 
344
+ private isCanceled() {
345
+ return !this.manager.limit.inc();
346
+ }
347
+
348
+ private invoke(isApplying: boolean, step: () => void) {
349
+ if (!isApplying) return true;
350
+ step();
351
+ return !this.isInconsistent && !this.isCanceled();
352
+ }
353
+
350
354
  private pushAssertion(expr: number, result: RefVector) {
351
355
  const manager = this.manager;
352
356
  if (this.isInconsistent || manager.isTrue(expr)) return;
@@ -387,7 +391,7 @@ export class AssertedFormulas {
387
391
 
388
392
  private elimTermIte() {
389
393
  const elim = this.termIteElim;
390
- if (this.simplifyFormulas((formula) => elim.rewrite(formula))) this.isReducePending = true;
394
+ if (!this.simplifyFormulas((formula) => elim.rewrite(formula))) return;
391
395
  if (elim.definitions.size > 0) this.isReducePending = true;
392
396
  for (const definition of elim.definitions.items) this.formulas.push(definition);
393
397
  this.flushCache();
@@ -395,8 +399,33 @@ export class AssertedFormulas {
395
399
  elim.reset();
396
400
  }
397
401
 
402
+ private simplifyAndReduce(simplify: (formula: number) => number) {
403
+ if (!this.simplifyFormulas(simplify)) return;
404
+ this.flushCache();
405
+ this.reduceAsserted();
406
+ }
407
+
408
+ private liftNonGroundIte() {
409
+ this.simplifyAndReduce((formula) => this.ngLiftIte.rewrite(formula));
410
+ }
411
+
412
+ private refineInjAxioms() {
413
+ this.simplifyFormulas((formula) => this.refineInjAxiom(formula));
414
+ }
415
+
416
+ private elimBounds() {
417
+ this.simplifyAndReduce((formula) => this.boundsElim.rewrite(formula));
418
+ }
419
+
420
+ private inferPatterns() {
421
+ this.simplifyFormulas((formula) => this.patternInference.rewrite(formula));
422
+ }
423
+
424
+ private maximizeBvSharing() {
425
+ if (this.simplifyFormulas((formula) => this.bvSharing.rewrite(formula))) this.reduceAsserted();
426
+ }
427
+
398
428
  private findMacros() {
399
- if (!this.isFindingMacros || !this.hasQuantifiers) return;
400
429
  this.isReducePending = true;
401
430
  this.swapFormulas(this.macros.find(this.formulas.items.slice(this.qhead)));
402
431
  this.flushCache();
@@ -425,19 +454,22 @@ export class AssertedFormulas {
425
454
  private simplifyFormulas(simplify: (formula: number) => number) {
426
455
  const manager = this.manager;
427
456
  const formulas = new RefVector(manager);
428
- let isChanged = false;
429
457
  for (let index = this.qhead; index < this.formulas.size; index++) {
430
458
  const formula = this.formulas.items[index];
431
459
  const result = simplify(formula);
432
460
  if (result === formula) formulas.push(formula);
433
461
  else {
434
462
  this.pushAssertion(result, formulas);
435
- isChanged = true;
463
+ this.isReducePending = true;
436
464
  }
437
465
  manager.decRef(result);
466
+ if (this.isCanceled()) {
467
+ formulas.reset();
468
+ return false;
469
+ }
438
470
  }
439
471
  this.swapFormulas(formulas);
440
- return isChanged;
472
+ return true;
441
473
  }
442
474
 
443
475
  private reduceAsserted() {
@@ -475,28 +507,42 @@ export class AssertedFormulas {
475
507
  }
476
508
 
477
509
  private nnfCnf() {
510
+ this.isReducePending = true;
478
511
  const manager = this.manager;
479
512
  const nnf = new Nnf(manager, this.names);
480
513
  const formulas = new RefVector(manager);
481
514
  const todo = new RefVector(manager);
482
- for (let index = this.qhead; index < this.formulas.size; index++) {
483
- todo.reset();
484
- let result = nnf.apply(this.formulas.items[index], todo);
485
- todo.push(result);
486
- for (let position = 0; position < todo.size; position++) {
487
- const rewritten = this.rewriter.rewrite(todo.items[position]);
515
+ const isFinished = (() => {
516
+ for (let index = this.qhead; index < this.formulas.size; index++) {
517
+ todo.reset();
518
+ let result = nnf.apply(this.formulas.items[index], todo);
519
+ todo.push(result);
520
+ if (this.isCanceled()) {
521
+ manager.decRef(result);
522
+ return false;
523
+ }
524
+ for (let position = 0; position < todo.size; position++) {
525
+ const rewritten = this.rewriter.rewrite(todo.items[position]);
526
+ manager.decRef(result);
527
+ result = rewritten;
528
+ if (this.isCanceled()) {
529
+ manager.decRef(result);
530
+ return false;
531
+ }
532
+ this.pushAssertion(result, formulas);
533
+ }
488
534
  manager.decRef(result);
489
- result = rewritten;
490
- this.pushAssertion(result, formulas);
491
535
  }
492
- manager.decRef(result);
493
- }
494
- this.swapFormulas(formulas);
536
+ return true;
537
+ })();
538
+ if (isFinished) this.swapFormulas(formulas);
495
539
  todo.reset();
540
+ formulas.reset();
496
541
  nnf.release();
497
542
  }
498
543
 
499
544
  private flattenClauses() {
545
+ this.isReducePending = true;
500
546
  const manager = this.manager;
501
547
  const mkNegation = (expr: number) =>
502
548
  manager.isNot(expr) ? manager.getArg(expr, 0) : manager.mkApp(OP_NOT, [expr], SORT_BOOL);
@@ -1007,9 +1007,11 @@ export class AstManager {
1007
1007
  withoutTemporaries<T>(body: () => T) {
1008
1008
  const outer = this.temporariesMark;
1009
1009
  this.temporariesMark = -1;
1010
- const result = body();
1011
- this.temporariesMark = outer;
1012
- return result;
1010
+ try {
1011
+ return body();
1012
+ } finally {
1013
+ this.temporariesMark = outer;
1014
+ }
1013
1015
  }
1014
1016
 
1015
1017
  releaseLater(expr: number) {
@@ -1,5 +1,5 @@
1
1
  import { SmtInvariantError } from "../../../errors";
2
- import { OP_ITE, OP_LT, SORT_BOOL, SORT_INT, isArithSort, type AstManager } from "./ast";
2
+ import { KIND_APP, OP_ITE, OP_LT, SORT_BOOL, SORT_INT, isArithSort, type AstManager } from "./ast";
3
3
  import { combineHash } from "./hash";
4
4
  import { ObjHashtable, ObjMap } from "./hashtable";
5
5
  import { FuncInterp, type ProtoModel } from "./proto-model";
@@ -245,14 +245,30 @@ export class AufSolver {
245
245
 
246
246
  private cleanupInstantiationSets() {
247
247
  const manager = this.manager;
248
+ const containsArray = (expr: number) => {
249
+ const visited = new Set<number>();
250
+ const todo = [expr];
251
+ for (let term = todo.pop(); term !== undefined; term = todo.pop()) {
252
+ if (visited.has(term)) continue;
253
+ visited.add(term);
254
+ if (manager.isArraySort(manager.sorts[term])) return true;
255
+ manager.pushArgs(term, todo);
256
+ }
257
+ return false;
258
+ };
259
+ const shouldCleanup = (value: number | null) =>
260
+ value === null ||
261
+ (!manager.isValue(value) &&
262
+ !manager.isArraySort(manager.sorts[value]) &&
263
+ (manager.kinds[value] !== KIND_APP ||
264
+ manager.getNumArgs(value) === 0 ||
265
+ !containsArray(value)));
248
266
  for (const node of this.nodes) {
249
267
  if (!node.isRoot()) continue;
250
268
  const set = node.getInstantiationSet();
251
269
  const toDelete: number[] = [];
252
- for (const term of set.elems.keys()) {
253
- const value = this.eval(term, true);
254
- if (value === null || !manager.isUniqueValue(value)) toDelete.push(term);
255
- }
270
+ for (const term of set.elems.keys())
271
+ if (shouldCleanup(this.eval(term, true))) toDelete.push(term);
256
272
  for (const term of toDelete) set.remove(term);
257
273
  this.resetEvalCache();
258
274
  }
@@ -285,18 +301,13 @@ export class AufSolver {
285
301
  return result;
286
302
  }
287
303
 
288
- private areDistinct(first: number, second: number) {
289
- const manager = this.manager;
290
- return first !== second && manager.isUniqueValue(first) && manager.isUniqueValue(second);
291
- }
292
-
293
304
  private pickInstanceDiffExceptions(node: Node, exceptionValues: number[]) {
294
305
  let result: number | null = null;
295
306
  let resultGeneration = Number.POSITIVE_INFINITY;
296
307
  for (const entry of node.getInstantiationSet().elems) {
297
308
  const value = this.eval(entry[0], true);
298
309
  if (value === null) return result;
299
- const isFound = exceptionValues.some((other) => !this.areDistinct(value, other));
310
+ const isFound = exceptionValues.some((other) => !this.manager.areDistinct(value, other));
300
311
  if (!isFound && (result === null || entry[1] < resultGeneration)) {
301
312
  result = entry[0];
302
313
  resultGeneration = entry[1];
@@ -331,7 +342,7 @@ export class AufSolver {
331
342
  if (kValue === null) return false;
332
343
  for (const exception of exceptions) {
333
344
  const value = this.eval(exception, true);
334
- if (value !== null && !this.areDistinct(kValue, value))
345
+ if (value !== null && !this.manager.areDistinct(kValue, value))
335
346
  this.newConstraints.push(this.manager.mkNot(this.manager.mkEq(k, exception)));
336
347
  }
337
348
  return true;
@@ -88,6 +88,8 @@ export class BitBlasterRewriter extends RewriterTpl {
88
88
  readonly keys: number[] = [];
89
89
  readonly values: RefVector;
90
90
  readonly newbits: number[] = [];
91
+ private keyLimits: number[] = [];
92
+ private newbitLimits: number[] = [];
91
93
 
92
94
  constructor(
93
95
  manager: AstManager,
@@ -123,6 +125,30 @@ export class BitBlasterRewriter extends RewriterTpl {
123
125
  return this.const2bits.get(decl);
124
126
  }
125
127
 
128
+ getNumScopes() {
129
+ return this.keyLimits.length;
130
+ }
131
+
132
+ push() {
133
+ this.keyLimits.push(this.keys.length);
134
+ this.newbitLimits.push(this.newbits.length);
135
+ }
136
+
137
+ pop(numScopes: number) {
138
+ if (numScopes === 0) return;
139
+ const manager = this.manager;
140
+ const size = this.keyLimits.length - numScopes;
141
+ const keyLimit = this.keyLimits[size];
142
+ for (const decl of this.keys.splice(keyLimit)) {
143
+ this.const2bits.delete(decl);
144
+ manager.decDeclRef(decl);
145
+ }
146
+ this.values.shrink(keyLimit);
147
+ this.keyLimits.length = size;
148
+ for (const decl of this.newbits.splice(this.newbitLimits[size])) manager.decDeclRef(decl);
149
+ this.newbitLimits.length = size;
150
+ }
151
+
126
152
  private mkConst(decl: number) {
127
153
  const manager = this.manager;
128
154
  const existing = this.const2bits.get(decl);
@@ -29,7 +29,7 @@ import { Rational } from "./rational";
29
29
  import { RefVector } from "./ref-vector";
30
30
  import { Rewriter, TACTIC_REWRITER_OPTIONS } from "./rewriter";
31
31
  import { RewriterTpl } from "./rewriter-tpl";
32
- import { finishSimplifier } from "./dependent-expr-state";
32
+ import { DependentExprState, finishSimplifier } from "./dependent-expr-state";
33
33
 
34
34
  interface Bound {
35
35
  value: Rational;
@@ -374,22 +374,21 @@ class BoundSimplifier {
374
374
 
375
375
  reduce() {
376
376
  const goal = this.goal;
377
+ const state = new DependentExprState(this.manager, goal);
377
378
  let isUpdated = true;
378
379
  for (let round = 0; round < 5 && isUpdated; round++) {
379
380
  isUpdated = false;
380
381
  let isBoundFound = false;
381
382
  this.reset();
382
- const size = goal.forms.size;
383
- for (let index = 0; index < size && !goal.isInconsistent; index++)
384
- if (this.insertBound(goal.forms.items[index])) {
385
- goal.update(index, this.manager.trueExpr);
386
- isBoundFound = true;
387
- }
383
+ state.forEachIndex((index) => {
384
+ if (!this.insertBound(goal.forms.items[index])) return;
385
+ goal.update(index, this.manager.trueExpr);
386
+ isBoundFound = true;
387
+ });
388
388
  if (!isBoundFound) break;
389
- for (let index = 0; index < size && !goal.isInconsistent; index++)
390
- this.tightenBound(goal.forms.items[index]);
389
+ state.forEachIndex((index) => this.tightenBound(goal.forms.items[index]));
391
390
  const rewriter = new BoundRewriter(this.manager, this);
392
- for (let index = 0; index < size && !goal.isInconsistent; index++) {
391
+ state.forEachIndex((index) => {
393
392
  const form = goal.forms.items[index];
394
393
  const result = rewriter.rewrite(form);
395
394
  if (result !== form) {
@@ -397,7 +396,7 @@ class BoundSimplifier {
397
396
  isUpdated = true;
398
397
  }
399
398
  this.manager.decRef(result);
400
- }
399
+ });
401
400
  rewriter.release();
402
401
  this.restoreBounds();
403
402
  }
@@ -17,7 +17,8 @@ import {
17
17
  import type { Goal } from "./goal";
18
18
  import { Rational } from "./rational";
19
19
  import { Rewriter, type RewriterOptions } from "./rewriter";
20
- import { finishSimplifier } from "./dependent-expr-state";
20
+ import { type DependentExprState, runSimplifier } from "./dependent-expr-state";
21
+ import { RewriterTpl } from "./rewriter-tpl";
21
22
 
22
23
  interface Bound {
23
24
  value: Rational;
@@ -91,18 +92,36 @@ export const checkPbFree = (manager: AstManager, form: number) => {
91
92
  }
92
93
  };
93
94
 
94
- export const card2bv = (manager: AstManager, goal: Goal, options: RewriterOptions) => {
95
- if (!goal.isProofsEnabled) {
96
- const rewriter = new Rewriter(manager, options);
97
- const size = goal.forms.size;
98
- for (let index = 0; index < size && !goal.isInconsistent; index++) {
99
- const form = goal.forms.items[index];
100
- const rewritten = rewriter.rewrite(form);
101
- checkPbFree(manager, rewritten);
102
- if (rewritten !== form) goal.update(index, rewritten);
103
- manager.decRef(rewritten);
104
- }
105
- rewriter.release();
95
+ // The port has no encoding to rewrite to, but the walk still counts its steps of the limit.
96
+ class CardPbRewriter extends RewriterTpl {
97
+ isRewritingPatterns() {
98
+ return false;
106
99
  }
107
- finishSimplifier(goal);
108
- };
100
+ }
101
+
102
+ export const createCard2Bv =
103
+ (manager: AstManager, options: RewriterOptions) => (state: DependentExprState) => {
104
+ const rewriter = new Rewriter(manager, options);
105
+ const pbRewriter = new CardPbRewriter(manager);
106
+ let simplified = -1;
107
+ return {
108
+ reduce: () => {
109
+ state.rewriteEach((form) => {
110
+ const next = rewriter.rewrite(form);
111
+ if (simplified !== -1) manager.decRef(simplified);
112
+ simplified = next;
113
+ const rewritten = pbRewriter.rewrite(simplified);
114
+ checkPbFree(manager, rewritten);
115
+ return rewritten;
116
+ });
117
+ if (simplified !== -1) manager.decRef(simplified);
118
+ },
119
+ release: () => {
120
+ rewriter.release();
121
+ pbRewriter.release();
122
+ },
123
+ };
124
+ };
125
+
126
+ export const card2bv = (manager: AstManager, goal: Goal, options: RewriterOptions) =>
127
+ runSimplifier(goal, false, createCard2Bv(manager, options));
@@ -18,7 +18,7 @@ import { ObjMap } from "./hashtable";
18
18
  import { RefVector } from "./ref-vector";
19
19
  import { Rewriter, type RewriterOptions } from "./rewriter";
20
20
  import { RewriterTpl } from "./rewriter-tpl";
21
- import { finishSimplifier } from "./dependent-expr-state";
21
+ import { runSimplifier } from "./dependent-expr-state";
22
22
 
23
23
  const SIMPLIFIED_FAMILIES = [
24
24
  FAMILY_BASIC,
@@ -49,6 +49,10 @@ class CofactorRewriter extends RewriterTpl {
49
49
  return false;
50
50
  }
51
51
 
52
+ checkMaxSteps() {
53
+ this.manager.limit.checkpoint();
54
+ }
55
+
52
56
  setCofactorAtom(expr: number) {
53
57
  const manager = this.manager;
54
58
  this.term = -1;
@@ -122,16 +126,30 @@ class CofactorElimTermIte {
122
126
  const manager = this.manager;
123
127
  const occs = new ObjMap<number>((key) => manager.hashes[key]);
124
128
  const visited = new Set<number>();
125
- const todo = [expr];
126
- for (let curr = todo.pop(); curr !== undefined; curr = todo.pop()) {
127
- if (manager.kinds[curr] !== KIND_APP) continue;
128
- const args = manager.getArgs(curr);
129
- if (this.isTermIte(curr)) occs.set(args[0], (occs.get(args[0]) ?? 0) + 1);
130
- for (let index = args.length - 1; index >= 0; index--) {
131
- const arg = args[index];
129
+ const frames = [expr];
130
+ const childIndexes = [0];
131
+ while (frames.length > 0) {
132
+ manager.limit.checkpoint();
133
+ const top = frames.length - 1;
134
+ const curr = frames[top];
135
+ const numArgs = manager.kinds[curr] === KIND_APP ? manager.getNumArgs(curr) : 0;
136
+ const isTermIte = this.isTermIte(curr);
137
+ let isPushed = false;
138
+ while (!isPushed && childIndexes[top] < numArgs) {
139
+ const arg = manager.getArg(curr, childIndexes[top]);
140
+ if (childIndexes[top] === 0 && isTermIte) occs.set(arg, (occs.get(arg) ?? 0) + 1);
141
+ childIndexes[top]++;
132
142
  if (visited.has(arg)) continue;
133
143
  visited.add(arg);
134
- if (manager.kinds[arg] === KIND_APP && manager.getNumArgs(arg) > 0) todo.push(arg);
144
+ if (manager.kinds[arg] === KIND_APP && manager.getNumArgs(arg) > 0) {
145
+ frames.push(arg);
146
+ childIndexes.push(0);
147
+ isPushed = true;
148
+ }
149
+ }
150
+ if (!isPushed) {
151
+ frames.pop();
152
+ childIndexes.pop();
135
153
  }
136
154
  }
137
155
  let best = -1;
@@ -201,6 +219,7 @@ class CofactorElimTermIte {
201
219
  const manager = this.manager;
202
220
  const frames = [{ expr: root, isFirst: true }];
203
221
  while (frames.length > 0) {
222
+ manager.limit.checkpoint();
204
223
  const frame = frames[frames.length - 1];
205
224
  const expr = frame.expr;
206
225
  if (manager.kinds[expr] !== KIND_APP) {
@@ -261,17 +280,14 @@ export const cofactorTermIte = (
261
280
  goal: Goal,
262
281
  options: RewriterOptions,
263
282
  isCofactoringEqualities: boolean,
264
- ) => {
265
- if (!goal.isProofsEnabled) {
266
- const size = goal.forms.size;
267
- for (let index = 0; index < size && !goal.isInconsistent; index++) {
268
- const eliminator = new CofactorElimTermIte(manager, options, isCofactoringEqualities);
269
- const form = goal.forms.items[index];
270
- const result = eliminator.apply(form);
271
- if (result !== form) goal.update(index, result);
272
- manager.decRef(result);
273
- eliminator.release();
274
- }
275
- }
276
- finishSimplifier(goal);
277
- };
283
+ ) =>
284
+ runSimplifier(goal, false, (state) => ({
285
+ reduce: () =>
286
+ state.rewriteEach((form) => {
287
+ const eliminator = new CofactorElimTermIte(manager, options, isCofactoringEqualities);
288
+ const result = eliminator.apply(form);
289
+ eliminator.release();
290
+ return result;
291
+ }),
292
+ release: () => {},
293
+ }));