tla-ts 0.0.10-dev.5ffc55ce5 → 0.0.10-dev.6232a1403

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.
@@ -1,8 +1,7 @@
1
- import { SmtCanceledError, SmtInvariantError } from "../../../../errors";
1
+ import { SmtInvariantError } from "../../../../errors";
2
2
  import { Rational } from "../rational";
3
- import type { ResourceLimit } from "../rlimit";
4
3
  import { getLog2, Mpbq } from "./mpbq";
5
- import { PolynomialManager, type Polynomial, PolynomialRef, type ValueOps } from "./polynomial";
4
+ import { PolynomialManager, type Polynomial, type ValueOps } from "./polynomial";
6
5
  import { UpolynomialManager, type Upolynomial } from "./upolynomial";
7
6
  import { factorUpolynomial, type FactorParams } from "./upolynomial-factorization";
8
7
 
@@ -225,8 +224,8 @@ export const mergeSortIndices = (
225
224
  };
226
225
 
227
226
  export class AlgebraicManager {
228
- private um: UpolynomialManager;
229
- private pm: PolynomialManager;
227
+ private um = new UpolynomialManager();
228
+ private pm = new PolynomialManager();
230
229
  private anumOps: ValueOps<Anum> = {
231
230
  fromInteger: (value) => new Anum(Rational.of(value)),
232
231
  add: (first, second) => this.add(first, second),
@@ -234,11 +233,6 @@ export class AlgebraicManager {
234
233
  power: (base, exponent) => this.power(base, exponent),
235
234
  };
236
235
 
237
- constructor(private limit: ResourceLimit) {
238
- this.um = new UpolynomialManager(limit);
239
- this.pm = new PolynomialManager(limit);
240
- }
241
-
242
236
  isZero(anum: Anum) {
243
237
  return anum.value instanceof Rational && anum.value.isZero();
244
238
  }
@@ -427,7 +421,7 @@ export class AlgebraicManager {
427
421
  }
428
422
 
429
423
  private sortRoots(roots: Anum[]) {
430
- if (!this.limit.inc() || roots.length < 2) return;
424
+ if (roots.length < 2) return;
431
425
  const sorted = mergeSortIndices(
432
426
  roots.length,
433
427
  (first, second) => this.compare(roots[first], roots[second]) < 0,
@@ -464,7 +458,6 @@ export class AlgebraicManager {
464
458
  let highVariations = isNonPositive ? atZero : this.um.signVariationsAtPlusInf(sequence);
465
459
  let target = isNonPositive ? rootIndex : rootIndex - nonPositiveCount;
466
460
  while (lowVariations > highVariations + 1) {
467
- this.limit.checkpoint();
468
461
  const middle = low.add(high).div2();
469
462
  const middleVariations = this.um.signVariationsAt(sequence, middle);
470
463
  const leftCount = lowVariations - middleVariations;
@@ -535,43 +528,29 @@ export class AlgebraicManager {
535
528
  ): Anum[] {
536
529
  if (pm.isConst(polynomial)) return [];
537
530
  if (pm.isUnivariate(polynomial)) return this.isolateUnivariateRoots(pm.toCoeffs(polynomial));
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();
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));
574
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
+ );
550
+ }
551
+ if (isNested)
552
+ throw new SmtInvariantError("resultant vanished during nested isolate_roots call");
553
+ return this.isolateVanishedRoots(pm, reduced, values, xs, x);
575
554
  }
576
555
 
577
556
  private isolateVanishedRoots(
@@ -585,50 +564,26 @@ export class AlgebraicManager {
585
564
  pm.evaluateWith(polynomial, this.anumOps, (variable) => getAssigned(values, variable));
586
565
  const degree = pm.degree(reduced, x);
587
566
  if (degree === 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();
567
+ const constant = evaluate(pm.coeff(reduced, x, 0));
568
+ const leading = evaluate(pm.coeff(reduced, x, 1));
596
569
  if (this.isZero(leading)) return [];
597
570
  const root = this.div(constant, leading);
598
571
  this.neg(root);
599
572
  return [root];
600
573
  }
601
- const c = new PolynomialRef(pm);
602
574
  let power = degree;
603
575
  let leading = new Anum(Rational.zero);
604
576
  for (; power >= 1; power--) {
605
- c.set(pm.coeff(reduced, x, power));
606
- leading = evaluate(c.get());
577
+ leading = evaluate(pm.coeff(reduced, x, power));
607
578
  if (!this.isZero(leading)) break;
608
579
  }
609
- if (power === 0) {
610
- c.reset();
611
- return [];
612
- }
580
+ if (power === 0) return [];
613
581
  const z = Math.max(...xs) + 1;
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
- }
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);
632
587
  }
633
588
 
634
589
  private convert(pm: PolynomialManager, polynomial: Polynomial, x = -1, maxDegree = Infinity) {
@@ -662,20 +617,13 @@ export class AlgebraicManager {
662
617
  if (pm.isConst(polynomial)) return result;
663
618
  if (pm.isUnivariate(polynomial))
664
619
  return this.isolateRootsClosestUnivariate(pm, polynomial, 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();
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);
678
625
  }
626
+ const all = this.isolateRoots(pm, polynomial, values);
679
627
  const sampleAnum = new Anum(sample);
680
628
  let lower = -1;
681
629
  let upper = -1;
@@ -723,47 +671,37 @@ export class AlgebraicManager {
723
671
  evalSignAt(pm: PolynomialManager, polynomial: Polynomial, values: readonly (Anum | undefined)[]) {
724
672
  while (true) {
725
673
  const rationalValues = getRationalValues(values);
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;
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;
755
696
  }
756
- if (!isRefined || isRestarted) break;
757
- }
758
- if (isRestarted) {
759
- this.limit.checkpoint();
760
- continue;
697
+ isRefined = true;
761
698
  }
762
- const sign = this.evalSignByResultant(pm, reduced, values, xs, interval, evaluate);
763
- if (sign !== null) return sign;
764
- } finally {
765
- pPrime.reset();
699
+ if (!isRefined || isRestarted) break;
700
+ interval = evaluate();
766
701
  }
702
+ if (isRestarted) continue;
703
+ const sign = this.evalSignByResultant(pm, reduced, values, xs, interval, evaluate);
704
+ if (sign !== null) return sign;
767
705
  }
768
706
  }
769
707
 
@@ -776,23 +714,14 @@ export class AlgebraicManager {
776
714
  evaluate: () => BqInterval,
777
715
  ) {
778
716
  const sorted = this.sortByDegree(xs, values);
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();
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));
796
725
  const bound = Mpbq.of(1n, k);
797
726
  const isInsideBound = (interval: BqInterval) =>
798
727
  bound.neg().compare(interval.lower) < 0 && interval.upper.compare(bound) < 0;
@@ -800,7 +729,6 @@ export class AlgebraicManager {
800
729
  const saved = sorted.map((variable) => this.saveInterval(getAssigned(values, variable)));
801
730
  let sign: number | null = null;
802
731
  while (sign === null) {
803
- this.limit.checkpoint();
804
732
  const interval = evaluate();
805
733
  if (!containsZero(interval)) sign = interval.lower.isNeg ? -1 : 1;
806
734
  else if (isInsideBound(interval)) sign = 0;
@@ -961,12 +889,14 @@ export class AlgebraicManager {
961
889
  // Res_y(x^k - y, p(y)) has a^(1/k) as a root.
962
890
  private mkRoot(anum: Anum, k: number): Anum {
963
891
  const cell = getCell(anum);
964
- const factored = this.factor(this.getResultantCoeffs(this.getVarDifference(k, 1), cell.p));
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
+ );
965
896
  const factors = factored.factors.factors;
966
897
  const sequences: (Upolynomial[] | null)[] = factors.map((factor) => this.um.sturmSeq(factor));
967
898
  const saved = this.saveInterval(anum);
968
899
  while (true) {
969
- this.limit.checkpoint();
970
900
  const interval = { lower: cell.lower.rootLower(k), upper: cell.upper.rootUpper(k) };
971
901
  const found = this.findIsolatingFactor(factors, sequences, interval);
972
902
  if (found !== null) {
@@ -1010,47 +940,14 @@ export class AlgebraicManager {
1010
940
  return (refined.isInterval ? refined.upper : refined.lower).toDecimal(precision);
1011
941
  }
1012
942
 
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
-
1038
943
  private getSumPolynomial(first: AlgebraicCell, second: AlgebraicCell) {
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);
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));
1045
946
  }
1046
947
 
1047
948
  private getProductPolynomial(first: AlgebraicCell, second: AlgebraicCell) {
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);
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));
1054
951
  }
1055
952
 
1056
953
  private findIsolatingFactor(
@@ -1093,7 +990,6 @@ export class AlgebraicManager {
1093
990
  const savedA = this.saveInterval(first);
1094
991
  const savedB = this.saveInterval(second);
1095
992
  while (true) {
1096
- this.limit.checkpoint();
1097
993
  const interval = isAdd ? INTERVAL_OPS.add(cellA, cellB) : INTERVAL_OPS.mul(cellA, cellB);
1098
994
  const found = this.findIsolatingFactor(factors, sequences, interval);
1099
995
  if (found !== null) {
@@ -1113,7 +1009,8 @@ export class AlgebraicManager {
1113
1009
  }
1114
1010
 
1115
1011
  private getPowerPolynomial(cell: AlgebraicCell, exponent: number) {
1116
- return this.getResultantCoeffs(this.getVarDifference(1, exponent), cell.p);
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));
1117
1014
  }
1118
1015
 
1119
1016
  private mkPower(anum: Anum, exponent: number): Anum {
@@ -1123,7 +1020,6 @@ export class AlgebraicManager {
1123
1020
  const sequences: (Upolynomial[] | null)[] = factors.map((factor) => this.um.sturmSeq(factor));
1124
1021
  const saved = this.saveInterval(anum);
1125
1022
  while (true) {
1126
- this.limit.checkpoint();
1127
1023
  const interval = getIntervalPower(cell, exponent);
1128
1024
  const found = this.findIsolatingFactor(factors, sequences, interval);
1129
1025
  if (found !== null) {
@@ -1178,13 +1074,12 @@ export class AlgebraicManager {
1178
1074
  if (sign !== 0) return sign;
1179
1075
  if (hasSameCoeffs(cellA.p, cellB.p)) return 0;
1180
1076
  if (cellA.isMinimal && cellB.isMinimal)
1181
- while (this.limit.inc()) {
1077
+ while (true) {
1182
1078
  this.refine(first);
1183
1079
  this.refine(second);
1184
1080
  sign = compareIntervals();
1185
1081
  if (sign !== 0) return sign;
1186
1082
  }
1187
- if (!this.limit.inc()) throw new SmtCanceledError();
1188
1083
  const magnitudeA = this.getCellMagnitude(cellA);
1189
1084
  const magnitudeB = this.getCellMagnitude(cellB);
1190
1085
  const target = Math.max(MIN_MAGNITUDE, Math.min(magnitudeA, magnitudeB));
@@ -3,11 +3,9 @@ import {
3
3
  type Polynomial,
4
4
  type PolynomialCache,
5
5
  type PolynomialManager,
6
- PolynomialRef,
7
6
  } from "./polynomial";
8
7
  import {
9
8
  type Atom,
10
- type Clause,
11
9
  type IneqAtom,
12
10
  type IneqKind,
13
11
  type RootAtom,
@@ -62,27 +60,21 @@ export class AtomTable {
62
60
  return max;
63
61
  }
64
62
 
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));
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;
80
74
  }
81
- atom.maxVar = max;
82
- return;
75
+ this.cache.mkUnique(atom.polynomial);
76
+ atom.maxVar = this.pm.maxVar(atom.polynomial);
83
77
  }
84
- this.cache.mkUnique(atom.polynomial);
85
- atom.maxVar = this.pm.maxVar(atom.polynomial);
86
78
  }
87
79
 
88
80
  getAtom(literal: number) {
@@ -121,8 +113,6 @@ export class AtomTable {
121
113
  this.atoms[boolVar] = null;
122
114
  this.hooks?.onDelete(boolVar);
123
115
  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);
126
116
  }
127
117
 
128
118
  private getIneqKey(atom: Pick<IneqAtom, "kind" | "polynomials" | "isEven">) {
@@ -136,15 +126,13 @@ export class AtomTable {
136
126
  mkIneqAtom(kind: IneqKind, polynomials: readonly Polynomial[], isEven: readonly boolean[]) {
137
127
  let sign = 1;
138
128
  let maxVar = NULL_VAR;
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());
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);
145
133
  if (variable > maxVar || maxVar === NULL_VAR) maxVar = variable;
146
- unique.push(this.cache.mkUnique(p.get()));
147
- }
134
+ return this.cache.mkUnique(normalized);
135
+ });
148
136
  const atom = {
149
137
  kind: sign < 0 ? flipKind(kind) : kind,
150
138
  boolVar: 0,
@@ -154,12 +142,7 @@ export class AtomTable {
154
142
  };
155
143
  const key = this.getIneqKey(atom);
156
144
  const existing = this.ineqAtoms.get(key);
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();
145
+ if (existing !== undefined) return existing.boolVar;
163
146
  atom.boolVar = this.mkBoolVar();
164
147
  this.atoms[atom.boolVar] = atom;
165
148
  this.ineqAtoms.set(key, atom);
@@ -194,23 +177,21 @@ export class AtomTable {
194
177
  }
195
178
 
196
179
  mkRootAtom(kind: RootKind, variable: number, index: number, polynomial: Polynomial) {
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() };
180
+ const atom = {
181
+ kind,
182
+ boolVar: 0,
183
+ maxVar: variable,
184
+ index,
185
+ polynomial: this.cache.mkUnique(this.pm.flipSignIfLmNeg(polynomial)),
186
+ };
202
187
  const key = this.getRootKey(atom);
203
188
  const existing = this.rootAtoms.get(key);
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;
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;
214
195
  }
215
196
  }
216
197