tla-ts 0.0.10-dev.4a2a53c48 → 0.0.10-dev.5ffc55ce5

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 (82) hide show
  1. package/dist/deciders.cjs +35 -35
  2. package/dist/deciders.js +29 -29
  3. package/dist/index.d.cts +1 -1
  4. package/dist/index.d.ts +1 -1
  5. package/dist/parallel-worker.cjs +1 -1
  6. package/dist/parallel-worker.js +1 -1
  7. package/dist/prove.cjs +25 -25
  8. package/dist/prove.js +57 -57
  9. package/dist/run.cjs +26 -28
  10. package/dist/run.js +42 -44
  11. package/dist/transfer.cjs +35 -35
  12. package/dist/transfer.js +28 -28
  13. package/dist/worker.cjs +1 -1
  14. package/dist/worker.js +1 -1
  15. package/package.json +1 -1
  16. package/src/bags.ts +47 -28
  17. package/src/check-trace.ts +2 -16
  18. package/src/check.ts +143 -61
  19. package/src/cli/runtime-invariants.ts +20 -56
  20. package/src/cli/tlc-arguments.ts +2 -2
  21. package/src/cli/tlc-output.ts +1 -2
  22. package/src/cli/tlc.ts +13 -8
  23. package/src/dfid.ts +9 -2
  24. package/src/environment.ts +18 -0
  25. package/src/evaluate.ts +9 -3
  26. package/src/exit-codes.ts +2 -0
  27. package/src/liveness-checker.ts +45 -4
  28. package/src/parallel-worker.ts +8 -2
  29. package/src/parallel.ts +5 -0
  30. package/src/prover/smt/solver/ast.ts +1 -1
  31. package/src/prover/smt/solver/context.ts +6 -2
  32. package/src/prover/smt/solver/euf/egraph.ts +1 -1
  33. package/src/prover/smt/solver/expr2polynomial.ts +204 -79
  34. package/src/prover/smt/solver/factor.ts +36 -14
  35. package/src/prover/smt/solver/fpa-rewriter.ts +1 -2
  36. package/src/prover/smt/solver/fpa2bv-converter.ts +3 -1
  37. package/src/prover/smt/solver/goal.ts +2 -0
  38. package/src/prover/smt/solver/goal2nlsat.ts +57 -30
  39. package/src/prover/smt/solver/lp/containers.ts +7 -1
  40. package/src/prover/smt/solver/lp/cross-nested.ts +1 -3
  41. package/src/prover/smt/solver/lp/nex-creator.ts +3 -2
  42. package/src/prover/smt/solver/lp/nex.ts +2 -1
  43. package/src/prover/smt/solver/lp/nla-basics.ts +46 -1
  44. package/src/prover/smt/solver/lp/nla-core.ts +26 -6
  45. package/src/prover/smt/solver/lp/nla-grobner.ts +156 -79
  46. package/src/prover/smt/solver/lp/nra-solver.ts +138 -95
  47. package/src/prover/smt/solver/lp/pdd-interval.ts +6 -2
  48. package/src/prover/smt/solver/lp/pdd-simplifier.ts +60 -20
  49. package/src/prover/smt/solver/lp/pdd-solver.ts +72 -39
  50. package/src/prover/smt/solver/lp/pdd.ts +725 -289
  51. package/src/prover/smt/solver/muz/dl-context.ts +8 -2
  52. package/src/prover/smt/solver/muz/spacer-context.ts +1 -0
  53. package/src/prover/smt/solver/muz/spacer-pdr.ts +1 -0
  54. package/src/prover/smt/solver/nlsat/algebraic.ts +194 -89
  55. package/src/prover/smt/solver/nlsat/atoms.ts +52 -33
  56. package/src/prover/smt/solver/nlsat/explain.ts +375 -217
  57. package/src/prover/smt/solver/nlsat/levelwise.ts +464 -240
  58. package/src/prover/smt/solver/nlsat/polynomial.ts +2400 -680
  59. package/src/prover/smt/solver/nlsat/solver.ts +74 -71
  60. package/src/prover/smt/solver/nlsat/transcendentals.ts +41 -17
  61. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +8 -4
  62. package/src/prover/smt/solver/nlsat/upolynomial.ts +40 -17
  63. package/src/prover/smt/solver/opt-nlsat.ts +29 -18
  64. package/src/prover/smt/solver/opt-solver.ts +2 -3
  65. package/src/prover/smt/solver/rlimit.ts +13 -0
  66. package/src/prover/smt/solver/sat/sat-tactic.ts +2 -1
  67. package/src/prover/smt/solver/seq-derive.ts +3 -6
  68. package/src/prover/smt/solver/smtlib-parser.ts +10 -10
  69. package/src/prover/smt/solver/smtlib-script.ts +91 -28
  70. package/src/prover/smt/solver/tactic.ts +14 -5
  71. package/src/prover/smt/solver/theory-lra-internalize.ts +8 -0
  72. package/src/prover/zenon/equality-rules.ts +24 -0
  73. package/src/prover/zenon/expr.ts +21 -0
  74. package/src/prover/zenon/index-table.ts +2 -0
  75. package/src/prover/zenon/rules.ts +2 -1
  76. package/src/resolve.ts +1 -1
  77. package/src/runtime.ts +2 -0
  78. package/src/sany/generator-expressions.ts +6 -13
  79. package/src/search.ts +40 -11
  80. package/src/tlc/order.ts +14 -3
  81. package/src/tlc/tlcext-operators.ts +18 -5
  82. package/src/tlc/trace-spec.ts +2 -3
@@ -38,9 +38,13 @@ const ENGINE_NAMES = new Map([
38
38
  ["ddnf", ENGINE_DDNF],
39
39
  ]);
40
40
 
41
- // The fp module's parameters, which Z3 keeps as global parameters across resets.
41
+ // As in Z3, these are copied when the context is made, so a later fp option doesn't apply.
42
42
  export class FpParams {
43
- constructor(private parameters: ReadonlyMap<string, string>) {}
43
+ private parameters: ReadonlyMap<string, string>;
44
+
45
+ constructor(parameters: ReadonlyMap<string, string>) {
46
+ this.parameters = new Map(parameters);
47
+ }
44
48
 
45
49
  getBool(name: string, defaultValue: boolean) {
46
50
  const value = this.parameters.get(`fp.${name}`);
@@ -117,6 +121,7 @@ export class DatalogContext {
117
121
  readonly rewriter: Rewriter;
118
122
  // m_mc: the converters since the last query, or undefined before one.
119
123
  modelConverters: ModelConverter[] | undefined;
124
+ isBounded = false;
120
125
 
121
126
  constructor(
122
127
  readonly manager: AstManager,
@@ -328,6 +333,7 @@ export class DatalogContext {
328
333
 
329
334
  query(query: number) {
330
335
  this.modelConverters = [];
336
+ this.isBounded = false;
331
337
  this.getEngine(query);
332
338
  this.flushAddRules();
333
339
  return this.ensureEngine(query).query(query);
@@ -1142,6 +1142,7 @@ export class SpacerContext {
1142
1142
  this.pobQueue.incLevel();
1143
1143
  level = this.pobQueue.maxLevel;
1144
1144
  }
1145
+ this.datalogContext.isBounded = true;
1145
1146
  return L_UNDEF;
1146
1147
  }
1147
1148
 
@@ -210,6 +210,7 @@ export const gpdrSolveCore = (context: SpacerContext) => {
210
210
  if (reachable !== L_FALSE) return reachable;
211
211
  if (level > 0 && context.propagate(context.expandedLevel, level)) return L_FALSE;
212
212
  }
213
+ context.datalogContext.isBounded = true;
213
214
  return L_UNDEF;
214
215
  };
215
216
 
@@ -1,7 +1,8 @@
1
- import { SmtInvariantError } from "../../../../errors";
1
+ import { SmtCanceledError, SmtInvariantError } from "../../../../errors";
2
2
  import { Rational } from "../rational";
3
+ import type { ResourceLimit } from "../rlimit";
3
4
  import { getLog2, Mpbq } from "./mpbq";
4
- import { PolynomialManager, type Polynomial, type ValueOps } from "./polynomial";
5
+ import { PolynomialManager, type Polynomial, PolynomialRef, type ValueOps } from "./polynomial";
5
6
  import { UpolynomialManager, type Upolynomial } from "./upolynomial";
6
7
  import { factorUpolynomial, type FactorParams } from "./upolynomial-factorization";
7
8
 
@@ -224,8 +225,8 @@ export const mergeSortIndices = (
224
225
  };
225
226
 
226
227
  export class AlgebraicManager {
227
- private um = new UpolynomialManager();
228
- private pm = new PolynomialManager();
228
+ private um: UpolynomialManager;
229
+ private pm: PolynomialManager;
229
230
  private anumOps: ValueOps<Anum> = {
230
231
  fromInteger: (value) => new Anum(Rational.of(value)),
231
232
  add: (first, second) => this.add(first, second),
@@ -233,6 +234,11 @@ export class AlgebraicManager {
233
234
  power: (base, exponent) => this.power(base, exponent),
234
235
  };
235
236
 
237
+ constructor(private limit: ResourceLimit) {
238
+ this.um = new UpolynomialManager(limit);
239
+ this.pm = new PolynomialManager(limit);
240
+ }
241
+
236
242
  isZero(anum: Anum) {
237
243
  return anum.value instanceof Rational && anum.value.isZero();
238
244
  }
@@ -421,7 +427,7 @@ export class AlgebraicManager {
421
427
  }
422
428
 
423
429
  private sortRoots(roots: Anum[]) {
424
- if (roots.length < 2) return;
430
+ if (!this.limit.inc() || roots.length < 2) return;
425
431
  const sorted = mergeSortIndices(
426
432
  roots.length,
427
433
  (first, second) => this.compare(roots[first], roots[second]) < 0,
@@ -458,6 +464,7 @@ export class AlgebraicManager {
458
464
  let highVariations = isNonPositive ? atZero : this.um.signVariationsAtPlusInf(sequence);
459
465
  let target = isNonPositive ? rootIndex : rootIndex - nonPositiveCount;
460
466
  while (lowVariations > highVariations + 1) {
467
+ this.limit.checkpoint();
461
468
  const middle = low.add(high).div2();
462
469
  const middleVariations = this.um.signVariationsAt(sequence, middle);
463
470
  const leftCount = lowVariations - middleVariations;
@@ -528,29 +535,43 @@ export class AlgebraicManager {
528
535
  ): Anum[] {
529
536
  if (pm.isConst(polynomial)) return [];
530
537
  if (pm.isUnivariate(polynomial)) return this.isolateUnivariateRoots(pm.toCoeffs(polynomial));
531
- const reduced = pm.substituteRational(polynomial, getRationalValues(values));
532
- if (pm.isConst(reduced)) return [];
533
- if (pm.isUnivariate(reduced)) {
534
- if (values[pm.maxVar(reduced)] !== undefined) return [];
535
- return this.isolateUnivariateRoots(pm.toCoeffs(reduced));
536
- }
537
- const xs = this.sortByDegree(pm.getVars(reduced), values);
538
- const x = xs[xs.length - 1];
539
- if (values[x] !== undefined) return [];
540
- let q = reduced;
541
- for (const y of xs.slice(0, -1)) {
542
- q = pm.resultant(q, pm.fromCoeffs(getCell(values[y]).p, y), y);
543
- if (q.size === 0) break;
544
- }
545
- if (q.size > 0) {
546
- if (pm.isConst(q)) return [];
547
- return this.isolateUnivariateRoots(pm.toCoeffs(q)).filter(
548
- (root) => this.evalSignAt(pm, reduced, getExtended(values, x, root)) === 0,
549
- );
538
+ const pPrime = new PolynomialRef(pm);
539
+ const q = new PolynomialRef(pm);
540
+ const pY = new PolynomialRef(pm);
541
+ try {
542
+ pPrime.set(pm.substituteRational(polynomial, getRationalValues(values)));
543
+ const reduced = pPrime.get();
544
+ if (pm.isConst(reduced)) return [];
545
+ if (pm.isUnivariate(reduced)) {
546
+ if (values[pm.maxVar(reduced)] !== undefined) return [];
547
+ return this.isolateUnivariateRoots(pm.toCoeffs(reduced));
548
+ }
549
+ const xs = this.sortByDegree(pm.getVars(reduced), values);
550
+ const x = xs[xs.length - 1];
551
+ if (values[x] !== undefined) return [];
552
+ q.copy(pPrime);
553
+ for (let index = 0; index + 1 < xs.length; index++) {
554
+ this.limit.checkpoint();
555
+ const y = xs[index];
556
+ pY.set(pm.toPolynomial(getCell(values[y]).p, y));
557
+ pm.resultant(q.get(), pY.get(), y, q);
558
+ if (pm.isZero(q.get())) {
559
+ if (isNested)
560
+ throw new SmtInvariantError("resultant vanished during nested isolate_roots call");
561
+ break;
562
+ }
563
+ }
564
+ if (pm.isZero(q.get())) return this.isolateVanishedRoots(pm, reduced, values, xs, x);
565
+ if (pm.isConst(q.get())) return [];
566
+ return this.isolateUnivariateRoots(pm.toCoeffs(q.get())).filter((root) => {
567
+ this.limit.checkpoint();
568
+ return this.evalSignAt(pm, reduced, getExtended(values, x, root)) === 0;
569
+ });
570
+ } finally {
571
+ pY.reset();
572
+ q.reset();
573
+ pPrime.reset();
550
574
  }
551
- if (isNested)
552
- throw new SmtInvariantError("resultant vanished during nested isolate_roots call");
553
- return this.isolateVanishedRoots(pm, reduced, values, xs, x);
554
575
  }
555
576
 
556
577
  private isolateVanishedRoots(
@@ -564,26 +585,50 @@ export class AlgebraicManager {
564
585
  pm.evaluateWith(polynomial, this.anumOps, (variable) => getAssigned(values, variable));
565
586
  const degree = pm.degree(reduced, x);
566
587
  if (degree === 1) {
567
- const constant = evaluate(pm.coeff(reduced, x, 0));
568
- const leading = evaluate(pm.coeff(reduced, x, 1));
588
+ const c0 = new PolynomialRef(pm);
589
+ const c1 = new PolynomialRef(pm);
590
+ c0.set(pm.coeff(reduced, x, 0));
591
+ c1.set(pm.coeff(reduced, x, 1));
592
+ const constant = evaluate(c0.get());
593
+ const leading = evaluate(c1.get());
594
+ c1.reset();
595
+ c0.reset();
569
596
  if (this.isZero(leading)) return [];
570
597
  const root = this.div(constant, leading);
571
598
  this.neg(root);
572
599
  return [root];
573
600
  }
601
+ const c = new PolynomialRef(pm);
574
602
  let power = degree;
575
603
  let leading = new Anum(Rational.zero);
576
604
  for (; power >= 1; power--) {
577
- leading = evaluate(pm.coeff(reduced, x, power));
605
+ c.set(pm.coeff(reduced, x, power));
606
+ leading = evaluate(c.get());
578
607
  if (!this.isZero(leading)) break;
579
608
  }
580
- if (power === 0) return [];
609
+ if (power === 0) {
610
+ c.reset();
611
+ return [];
612
+ }
581
613
  const z = Math.max(...xs) + 1;
582
- const q = this.pm.add(
583
- this.pm.mul(this.pm.mkVar(z), this.pm.mkVar(x, power)),
584
- this.convert(pm, reduced, x, power - 1),
585
- );
586
- return this.isolateRoots(this.pm, q, getExtended(values, z, leading), true);
614
+ const q2 = new PolynomialRef(this.pm);
615
+ const zP = new PolynomialRef(this.pm);
616
+ const xiP = new PolynomialRef(this.pm);
617
+ const product = new PolynomialRef(this.pm);
618
+ q2.set(this.convert(pm, reduced, x, power - 1));
619
+ xiP.set(this.pm.mkVar(x, power));
620
+ zP.set(this.pm.mkVar(z));
621
+ product.set(this.pm.mul(zP.get(), xiP.get()));
622
+ q2.set(this.pm.add(product.get(), q2.get()));
623
+ product.reset();
624
+ try {
625
+ return this.isolateRoots(this.pm, q2.get(), getExtended(values, z, leading), true);
626
+ } finally {
627
+ xiP.reset();
628
+ zP.reset();
629
+ q2.reset();
630
+ c.reset();
631
+ }
587
632
  }
588
633
 
589
634
  private convert(pm: PolynomialManager, polynomial: Polynomial, x = -1, maxDegree = Infinity) {
@@ -617,13 +662,20 @@ export class AlgebraicManager {
617
662
  if (pm.isConst(polynomial)) return result;
618
663
  if (pm.isUnivariate(polynomial))
619
664
  return this.isolateRootsClosestUnivariate(pm, polynomial, sample);
620
- const reduced = pm.substituteRational(polynomial, getRationalValues(values));
621
- if (pm.isConst(reduced)) return result;
622
- if (pm.isUnivariate(reduced)) {
623
- if (values[pm.maxVar(reduced)] !== undefined) return result;
624
- return this.isolateRootsClosestUnivariate(pm, reduced, sample);
665
+ const pPrime = new PolynomialRef(pm);
666
+ let all: Anum[];
667
+ try {
668
+ pPrime.set(pm.substituteRational(polynomial, getRationalValues(values)));
669
+ const reduced = pPrime.get();
670
+ if (pm.isConst(reduced)) return result;
671
+ if (pm.isUnivariate(reduced)) {
672
+ if (values[pm.maxVar(reduced)] !== undefined) return result;
673
+ return this.isolateRootsClosestUnivariate(pm, reduced, sample);
674
+ }
675
+ all = this.isolateRoots(pm, polynomial, values);
676
+ } finally {
677
+ pPrime.reset();
625
678
  }
626
- const all = this.isolateRoots(pm, polynomial, values);
627
679
  const sampleAnum = new Anum(sample);
628
680
  let lower = -1;
629
681
  let upper = -1;
@@ -671,37 +723,47 @@ export class AlgebraicManager {
671
723
  evalSignAt(pm: PolynomialManager, polynomial: Polynomial, values: readonly (Anum | undefined)[]) {
672
724
  while (true) {
673
725
  const rationalValues = getRationalValues(values);
674
- if (pm.getVars(polynomial).every((variable) => rationalValues[variable] !== undefined))
675
- return pm.getSign(polynomial, rationalValues);
676
- const reduced = pm.substituteRational(polynomial, rationalValues);
677
- if (reduced.size === 0) return 0;
678
- if (pm.isConst(reduced)) return reduced.as[0] > 0n ? 1 : -1;
679
- const xs = pm.getVars(reduced);
680
- const getInterval = (variable: number) => {
681
- const cell = getCell(values[variable]);
682
- return { lower: cell.lower, upper: cell.upper };
683
- };
684
- const evaluate = () => pm.evaluateWith(reduced, INTERVAL_OPS, getInterval);
685
- let interval = evaluate();
686
- let isRestarted = false;
687
- while (true) {
688
- if (!containsZero(interval)) return interval.lower.isNeg ? -1 : 1;
689
- let isRefined = false;
690
- for (const variable of xs) {
691
- const value = getAssigned(values, variable);
692
- if (this.getCellMagnitude(getCell(value)) <= MIN_MAGNITUDE) continue;
693
- if (!this.refine(value)) {
694
- isRestarted = true;
695
- break;
726
+ const rationalSign = pm.getRationalSign(polynomial, rationalValues);
727
+ if (rationalSign !== null) return rationalSign;
728
+ const pPrime = new PolynomialRef(pm);
729
+ try {
730
+ pPrime.set(pm.substituteRational(polynomial, rationalValues));
731
+ const reduced = pPrime.get();
732
+ if (reduced.size === 0) return 0;
733
+ if (pm.isConst(reduced)) return reduced.as[0] > 0n ? 1 : -1;
734
+ const xs = pm.getVars(reduced);
735
+ const getInterval = (variable: number) => {
736
+ const cell = getCell(values[variable]);
737
+ return { lower: cell.lower, upper: cell.upper };
738
+ };
739
+ const evaluate = () => pm.evaluateWith(reduced, INTERVAL_OPS, getInterval);
740
+ let interval: BqInterval;
741
+ let isRestarted = false;
742
+ while (true) {
743
+ this.limit.checkpoint();
744
+ interval = evaluate();
745
+ if (!containsZero(interval)) return interval.lower.isNeg ? -1 : 1;
746
+ let isRefined = false;
747
+ for (const variable of xs) {
748
+ const value = getAssigned(values, variable);
749
+ if (this.getCellMagnitude(getCell(value)) <= MIN_MAGNITUDE) continue;
750
+ if (!this.refine(value)) {
751
+ isRestarted = true;
752
+ break;
753
+ }
754
+ isRefined = true;
696
755
  }
697
- isRefined = true;
756
+ if (!isRefined || isRestarted) break;
757
+ }
758
+ if (isRestarted) {
759
+ this.limit.checkpoint();
760
+ continue;
698
761
  }
699
- if (!isRefined || isRestarted) break;
700
- interval = evaluate();
762
+ const sign = this.evalSignByResultant(pm, reduced, values, xs, interval, evaluate);
763
+ if (sign !== null) return sign;
764
+ } finally {
765
+ pPrime.reset();
701
766
  }
702
- if (isRestarted) continue;
703
- const sign = this.evalSignByResultant(pm, reduced, values, xs, interval, evaluate);
704
- if (sign !== null) return sign;
705
767
  }
706
768
  }
707
769
 
@@ -714,14 +776,23 @@ export class AlgebraicManager {
714
776
  evaluate: () => BqInterval,
715
777
  ) {
716
778
  const sorted = this.sortByDegree(xs, values);
717
- let resultant = this.pm.sub(this.pm.mkVar(Math.max(...xs) + 1), this.convert(pm, reduced));
718
- for (const variable of sorted)
719
- resultant = this.pm.resultant(
720
- resultant,
721
- this.pm.fromCoeffs(getCell(values[variable]).p, variable),
722
- variable,
723
- );
724
- const k = this.um.nonzeroRootLowerBound(this.pm.toCoeffs(resultant));
779
+ const yp = new PolynomialRef(this.pm);
780
+ const pPrimeAux = new PolynomialRef(this.pm);
781
+ const r = new PolynomialRef(this.pm);
782
+ const qi = new PolynomialRef(this.pm);
783
+ yp.set(this.pm.mkVar(Math.max(...xs) + 1));
784
+ pPrimeAux.set(this.convert(pm, reduced));
785
+ r.set(this.pm.sub(yp.get(), pPrimeAux.get()));
786
+ for (const variable of sorted) {
787
+ this.limit.checkpoint();
788
+ qi.set(this.pm.toPolynomial(getCell(values[variable]).p, variable));
789
+ this.pm.resultant(r.get(), qi.get(), variable, r);
790
+ }
791
+ const k = this.um.nonzeroRootLowerBound(this.pm.toCoeffs(r.get()));
792
+ qi.reset();
793
+ r.reset();
794
+ pPrimeAux.reset();
795
+ yp.reset();
725
796
  const bound = Mpbq.of(1n, k);
726
797
  const isInsideBound = (interval: BqInterval) =>
727
798
  bound.neg().compare(interval.lower) < 0 && interval.upper.compare(bound) < 0;
@@ -729,6 +800,7 @@ export class AlgebraicManager {
729
800
  const saved = sorted.map((variable) => this.saveInterval(getAssigned(values, variable)));
730
801
  let sign: number | null = null;
731
802
  while (sign === null) {
803
+ this.limit.checkpoint();
732
804
  const interval = evaluate();
733
805
  if (!containsZero(interval)) sign = interval.lower.isNeg ? -1 : 1;
734
806
  else if (isInsideBound(interval)) sign = 0;
@@ -889,14 +961,12 @@ export class AlgebraicManager {
889
961
  // Res_y(x^k - y, p(y)) has a^(1/k) as a root.
890
962
  private mkRoot(anum: Anum, k: number): Anum {
891
963
  const cell = getCell(anum);
892
- const x = this.pm.sub(this.pm.mkVar(X, k), this.pm.mkVar(Y));
893
- const factored = this.factor(
894
- this.pm.toCoeffs(this.pm.resultant(x, this.pm.fromCoeffs(cell.p, Y), Y)),
895
- );
964
+ const factored = this.factor(this.getResultantCoeffs(this.getVarDifference(k, 1), cell.p));
896
965
  const factors = factored.factors.factors;
897
966
  const sequences: (Upolynomial[] | null)[] = factors.map((factor) => this.um.sturmSeq(factor));
898
967
  const saved = this.saveInterval(anum);
899
968
  while (true) {
969
+ this.limit.checkpoint();
900
970
  const interval = { lower: cell.lower.rootLower(k), upper: cell.upper.rootUpper(k) };
901
971
  const found = this.findIsolatingFactor(factors, sequences, interval);
902
972
  if (found !== null) {
@@ -940,14 +1010,47 @@ export class AlgebraicManager {
940
1010
  return (refined.isInterval ? refined.upper : refined.lower).toDecimal(precision);
941
1011
  }
942
1012
 
1013
+ // Releases first.
1014
+ private getResultantCoeffs(first: PolynomialRef, p: Upolynomial) {
1015
+ const q = new PolynomialRef(this.pm);
1016
+ const r = new PolynomialRef(this.pm);
1017
+ q.set(this.pm.toPolynomial(p, Y));
1018
+ this.pm.resultant(first.get(), q.get(), Y, r);
1019
+ const coeffs = this.pm.toCoeffs(r.get());
1020
+ r.reset();
1021
+ q.reset();
1022
+ first.reset();
1023
+ return coeffs;
1024
+ }
1025
+
1026
+ private getVarDifference(xDegree: number, yDegree: number) {
1027
+ const x = new PolynomialRef(this.pm);
1028
+ const y = new PolynomialRef(this.pm);
1029
+ const difference = new PolynomialRef(this.pm);
1030
+ x.set(this.pm.mkVar(X, xDegree));
1031
+ y.set(this.pm.mkVar(Y, yDegree));
1032
+ difference.set(this.pm.sub(x.get(), y.get()));
1033
+ y.reset();
1034
+ x.reset();
1035
+ return difference;
1036
+ }
1037
+
943
1038
  private getSumPolynomial(first: AlgebraicCell, second: AlgebraicCell) {
944
- const composed = this.pm.composeXMinusY(this.pm.fromCoeffs(first.p, X), Y);
945
- return this.pm.toCoeffs(this.pm.resultant(composed, this.pm.fromCoeffs(second.p, Y), Y));
1039
+ const p = new PolynomialRef(this.pm);
1040
+ const composed = new PolynomialRef(this.pm);
1041
+ p.set(this.pm.toPolynomial(first.p, X));
1042
+ this.pm.composeXMinusY(p.get(), Y, composed);
1043
+ p.reset();
1044
+ return this.getResultantCoeffs(composed, second.p);
946
1045
  }
947
1046
 
948
1047
  private getProductPolynomial(first: AlgebraicCell, second: AlgebraicCell) {
949
- const composed = this.pm.composeXDivY(this.pm.fromCoeffs(first.p, X), Y);
950
- return this.pm.toCoeffs(this.pm.resultant(composed, this.pm.fromCoeffs(second.p, Y), Y));
1048
+ const p = new PolynomialRef(this.pm);
1049
+ const composed = new PolynomialRef(this.pm);
1050
+ p.set(this.pm.toPolynomial(first.p, X));
1051
+ composed.set(this.pm.composeXDivY(p.get(), Y));
1052
+ p.reset();
1053
+ return this.getResultantCoeffs(composed, second.p);
951
1054
  }
952
1055
 
953
1056
  private findIsolatingFactor(
@@ -990,6 +1093,7 @@ export class AlgebraicManager {
990
1093
  const savedA = this.saveInterval(first);
991
1094
  const savedB = this.saveInterval(second);
992
1095
  while (true) {
1096
+ this.limit.checkpoint();
993
1097
  const interval = isAdd ? INTERVAL_OPS.add(cellA, cellB) : INTERVAL_OPS.mul(cellA, cellB);
994
1098
  const found = this.findIsolatingFactor(factors, sequences, interval);
995
1099
  if (found !== null) {
@@ -1009,8 +1113,7 @@ export class AlgebraicManager {
1009
1113
  }
1010
1114
 
1011
1115
  private getPowerPolynomial(cell: AlgebraicCell, exponent: number) {
1012
- const x = this.pm.sub(this.pm.mkVar(X), this.pm.mkVar(Y, exponent));
1013
- return this.pm.toCoeffs(this.pm.resultant(x, this.pm.fromCoeffs(cell.p, Y), Y));
1116
+ return this.getResultantCoeffs(this.getVarDifference(1, exponent), cell.p);
1014
1117
  }
1015
1118
 
1016
1119
  private mkPower(anum: Anum, exponent: number): Anum {
@@ -1020,6 +1123,7 @@ export class AlgebraicManager {
1020
1123
  const sequences: (Upolynomial[] | null)[] = factors.map((factor) => this.um.sturmSeq(factor));
1021
1124
  const saved = this.saveInterval(anum);
1022
1125
  while (true) {
1126
+ this.limit.checkpoint();
1023
1127
  const interval = getIntervalPower(cell, exponent);
1024
1128
  const found = this.findIsolatingFactor(factors, sequences, interval);
1025
1129
  if (found !== null) {
@@ -1074,12 +1178,13 @@ export class AlgebraicManager {
1074
1178
  if (sign !== 0) return sign;
1075
1179
  if (hasSameCoeffs(cellA.p, cellB.p)) return 0;
1076
1180
  if (cellA.isMinimal && cellB.isMinimal)
1077
- while (true) {
1181
+ while (this.limit.inc()) {
1078
1182
  this.refine(first);
1079
1183
  this.refine(second);
1080
1184
  sign = compareIntervals();
1081
1185
  if (sign !== 0) return sign;
1082
1186
  }
1187
+ if (!this.limit.inc()) throw new SmtCanceledError();
1083
1188
  const magnitudeA = this.getCellMagnitude(cellA);
1084
1189
  const magnitudeB = this.getCellMagnitude(cellB);
1085
1190
  const target = Math.max(MIN_MAGNITUDE, Math.min(magnitudeA, magnitudeB));
@@ -3,9 +3,11 @@ import {
3
3
  type Polynomial,
4
4
  type PolynomialCache,
5
5
  type PolynomialManager,
6
+ PolynomialRef,
6
7
  } from "./polynomial";
7
8
  import {
8
9
  type Atom,
10
+ type Clause,
9
11
  type IneqAtom,
10
12
  type IneqKind,
11
13
  type RootAtom,
@@ -60,21 +62,27 @@ export class AtomTable {
60
62
  return max;
61
63
  }
62
64
 
63
- reinitCache() {
64
- for (const atom of this.atoms) {
65
- if (atom === null) continue;
66
- if (isIneqAtom(atom)) {
67
- let max = 0;
68
- for (const polynomial of atom.polynomials) {
69
- this.cache.mkUnique(polynomial);
70
- max = Math.max(max, this.pm.maxVar(polynomial));
71
- }
72
- atom.maxVar = max;
73
- continue;
65
+ reinitCache(clauses: readonly Clause[], learned: readonly Clause[]) {
66
+ for (const clause of clauses)
67
+ for (const literal of clause.literals) this.reinitAtomCache(this.atoms[literal >> 1]);
68
+ for (const clause of learned)
69
+ for (const literal of clause.literals) this.reinitAtomCache(this.atoms[literal >> 1]);
70
+ for (const atom of this.atoms) this.reinitAtomCache(atom);
71
+ }
72
+
73
+ private reinitAtomCache(atom: Atom | null) {
74
+ if (atom === null) return;
75
+ if (isIneqAtom(atom)) {
76
+ let max = 0;
77
+ for (const polynomial of atom.polynomials) {
78
+ this.cache.mkUnique(polynomial);
79
+ max = Math.max(max, this.pm.maxVar(polynomial));
74
80
  }
75
- this.cache.mkUnique(atom.polynomial);
76
- atom.maxVar = this.pm.maxVar(atom.polynomial);
81
+ atom.maxVar = max;
82
+ return;
77
83
  }
84
+ this.cache.mkUnique(atom.polynomial);
85
+ atom.maxVar = this.pm.maxVar(atom.polynomial);
78
86
  }
79
87
 
80
88
  getAtom(literal: number) {
@@ -113,6 +121,8 @@ export class AtomTable {
113
121
  this.atoms[boolVar] = null;
114
122
  this.hooks?.onDelete(boolVar);
115
123
  this.freeBoolVars.push(boolVar);
124
+ if (isIneqAtom(atom)) for (const polynomial of atom.polynomials) this.pm.decRef(polynomial);
125
+ else this.pm.decRef(atom.polynomial);
116
126
  }
117
127
 
118
128
  private getIneqKey(atom: Pick<IneqAtom, "kind" | "polynomials" | "isEven">) {
@@ -126,13 +136,15 @@ export class AtomTable {
126
136
  mkIneqAtom(kind: IneqKind, polynomials: readonly Polynomial[], isEven: readonly boolean[]) {
127
137
  let sign = 1;
128
138
  let maxVar = NULL_VAR;
129
- const unique = polynomials.map((polynomial, index) => {
130
- const normalized = this.pm.flipSignIfLmNeg(polynomial);
131
- if (normalized !== polynomial && !isEven[index]) sign = -sign;
132
- const variable = this.pm.maxVar(normalized);
139
+ const p = new PolynomialRef(this.pm);
140
+ const unique: Polynomial[] = [];
141
+ for (let index = 0; index < polynomials.length; index++) {
142
+ p.set(this.pm.flipSignIfLmNeg(polynomials[index]));
143
+ if (p.get() !== polynomials[index] && !isEven[index]) sign = -sign;
144
+ const variable = this.pm.maxVar(p.get());
133
145
  if (variable > maxVar || maxVar === NULL_VAR) maxVar = variable;
134
- return this.cache.mkUnique(normalized);
135
- });
146
+ unique.push(this.cache.mkUnique(p.get()));
147
+ }
136
148
  const atom = {
137
149
  kind: sign < 0 ? flipKind(kind) : kind,
138
150
  boolVar: 0,
@@ -142,7 +154,12 @@ export class AtomTable {
142
154
  };
143
155
  const key = this.getIneqKey(atom);
144
156
  const existing = this.ineqAtoms.get(key);
145
- if (existing !== undefined) return existing.boolVar;
157
+ if (existing !== undefined) {
158
+ p.reset();
159
+ return existing.boolVar;
160
+ }
161
+ for (const polynomial of unique) this.pm.incRef(polynomial);
162
+ p.reset();
146
163
  atom.boolVar = this.mkBoolVar();
147
164
  this.atoms[atom.boolVar] = atom;
148
165
  this.ineqAtoms.set(key, atom);
@@ -177,21 +194,23 @@ export class AtomTable {
177
194
  }
178
195
 
179
196
  mkRootAtom(kind: RootKind, variable: number, index: number, polynomial: Polynomial) {
180
- const atom = {
181
- kind,
182
- boolVar: 0,
183
- maxVar: variable,
184
- index,
185
- polynomial: this.cache.mkUnique(this.pm.flipSignIfLmNeg(polynomial)),
186
- };
197
+ const p1 = new PolynomialRef(this.pm);
198
+ const uniqP = new PolynomialRef(this.pm);
199
+ p1.set(this.pm.flipSignIfLmNeg(polynomial));
200
+ uniqP.set(this.cache.mkUnique(p1.get()));
201
+ const atom = { kind, boolVar: 0, maxVar: variable, index, polynomial: uniqP.get() };
187
202
  const key = this.getRootKey(atom);
188
203
  const existing = this.rootAtoms.get(key);
189
- if (existing !== undefined) return existing.boolVar;
190
- atom.boolVar = this.mkBoolVar();
191
- this.atoms[atom.boolVar] = atom;
192
- this.rootAtoms.set(key, atom);
193
- this.rootKeys.set(atom.boolVar, key);
194
- return atom.boolVar;
204
+ if (existing === undefined) {
205
+ atom.boolVar = this.mkBoolVar();
206
+ this.atoms[atom.boolVar] = atom;
207
+ this.rootAtoms.set(key, atom);
208
+ this.rootKeys.set(atom.boolVar, key);
209
+ this.pm.incRef(atom.polynomial);
210
+ }
211
+ uniqP.reset();
212
+ p1.reset();
213
+ return existing === undefined ? atom.boolVar : existing.boolVar;
195
214
  }
196
215
  }
197
216