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
@@ -1,5 +1,6 @@
1
1
  import { Rational } from "../rational";
2
2
  import { KIND_EQ, KIND_GE, KIND_GT, KIND_LE, KIND_LT, KIND_NE } from "./constraints";
3
+ import { NULL_LPVAR } from "./lar-solver";
3
4
  import { LarTerm } from "./lar-term";
4
5
  import type { NlaCore } from "./nla-core";
5
6
  import type { Factorization } from "./nla-factorization";
@@ -305,8 +306,52 @@ export class Basics {
305
306
  );
306
307
  }
307
308
 
308
- // Deviation from Z3: the monic-to-factor neutral lemma is left out because it never fires (nla_basics_lemmas.cpp).
309
+ private basicLemmaForMonNeutralMonicToFactorModelBased(
310
+ rooted: Monic,
311
+ factorization: Factorization,
312
+ ) {
313
+ const core = this.core;
314
+ const monicVar = core.emons.get(rooted.variable).variable;
315
+ const absMonicValue = core.val(monicVar).abs();
316
+ if (absMonicValue.isZero()) return;
317
+ let sameAbsVar = NULL_LPVAR;
318
+ let nonUnitVar = NULL_LPVAR;
319
+ let isAllInt = true;
320
+ for (const factor of factorization.factors) {
321
+ const lpVar = factor.variable;
322
+ isAllInt &&= core.varIsInt(lpVar);
323
+ // Z3 tests the factor's variable against null_lpvar, which never holds, so the lemma never fires.
324
+ if (lpVar === NULL_LPVAR && core.valFactor(factor).abs().equals(absMonicValue))
325
+ sameAbsVar = lpVar;
326
+ else if (!core.valFactor(factor).abs().equals(Rational.one)) nonUnitVar = lpVar;
327
+ }
328
+ if (sameAbsVar === NULL_LPVAR || nonUnitVar === NULL_LPVAR) return;
329
+ if (!isAllInt && factorization.factors.length > 2) return;
330
+ const lemma = new LemmaBuilder(core);
331
+ lemma.addIneq(mkIneq(monicVar, KIND_EQ, 0));
332
+ lemma.addIneq(
333
+ mkIneq(
334
+ new LarTerm([
335
+ [sameAbsVar, Rational.one],
336
+ [
337
+ monicVar,
338
+ core.val(sameAbsVar).equals(core.val(monicVar).neg())
339
+ ? Rational.one
340
+ : Rational.minusOne,
341
+ ],
342
+ ]),
343
+ KIND_NE,
344
+ 0,
345
+ ),
346
+ );
347
+ lemma.addIneq(mkIneq(nonUnitVar, KIND_EQ, 1));
348
+ lemma.addIneq(mkIneq(nonUnitVar, KIND_EQ, -1));
349
+ lemma.explainMonic(rooted);
350
+ lemma.explainFactorization(factorization);
351
+ }
352
+
309
353
  private basicLemmaForMonNeutralModelBased(rooted: Monic, factorization: Factorization) {
354
+ this.basicLemmaForMonNeutralMonicToFactorModelBased(rooted, factorization);
310
355
  if (factorization.mon !== null)
311
356
  this.basicLemmaForMonNeutralFromFactorsToMonicModelBasedFm(factorization.mon);
312
357
  else this.basicLemmaForMonNeutralFromFactorsToMonicModelBased(rooted, factorization);
@@ -1,5 +1,6 @@
1
+ import { SmtInvariantError } from "../../../../errors";
1
2
  import { L_FALSE, L_TRUE, L_UNDEF } from "../lbool";
2
- import type { ResourceLimit } from "../rlimit";
3
+ import { ResourceLimit } from "../rlimit";
3
4
  import { Rational } from "../rational";
4
5
  import { IndexedUintSet } from "./containers";
5
6
  import { COLUMN_BOXED, COLUMN_FIXED, COLUMN_FREE, COLUMN_LOWER, COLUMN_UPPER } from "./core-solver";
@@ -143,6 +144,7 @@ export class NlaCore {
143
144
  private readonly patcher: Patcher;
144
145
  private readonly horner: Horner;
145
146
  readonly grobner: Grobner;
147
+ readonly nraLimit = new ResourceLimit(Infinity);
146
148
  readonly nra = new NraSolver(this);
147
149
  readonly transcendentals = new NlaTranscendentals(this);
148
150
  isUsingNraModel = false;
@@ -150,6 +152,9 @@ export class NlaCore {
150
152
  private checkAssignmentFailCount = 0;
151
153
  private squeezeSchedule = new SqueezeSchedule();
152
154
  isRelevant: (lpVar: number) => boolean = () => true;
155
+ addMulDefHook: (vars: number[]) => number = () => {
156
+ throw new SmtInvariantError("nla has no hook to define a product");
157
+ };
153
158
 
154
159
  constructor(
155
160
  readonly lra: LarSolver,
@@ -344,6 +349,12 @@ export class NlaCore {
344
349
  this.monicsWithChangedBounds.add(lpVar);
345
350
  }
346
351
 
352
+ addMulDef(vars: number[]) {
353
+ const lpVar = this.addMulDefHook(vars);
354
+ this.addMonic(lpVar, vars);
355
+ return lpVar;
356
+ }
357
+
347
358
  checkMonic(monic: Monic) {
348
359
  const value = this.lra.getValue(monic.variable);
349
360
  if (this.lra.isInt(monic.variable) && !value.x.isInt()) return true;
@@ -757,7 +768,9 @@ export class NlaCore {
757
768
  NLA_PARAMS.nraCheckAssignment &&
758
769
  this.checkAssignmentFailCount < NLA_PARAMS.nraCheckAssignmentMaxFail
759
770
  ) {
771
+ this.limit.pushChild(this.nraLimit);
760
772
  result = this.nra.checkAssignment();
773
+ this.limit.popChild(this.nraLimit);
761
774
  if (result !== L_TRUE) this.checkAssignmentFailCount++;
762
775
  }
763
776
  if (hasNoEffect() && this.shouldRunBoundedNlsat()) result = this.boundedNlsat();
@@ -777,10 +790,12 @@ export class NlaCore {
777
790
  result = this.boundedNlsat();
778
791
  }
779
792
  if (hasNoEffect() && NLA_PARAMS.nra && level >= 2) {
780
- result = this.nra.check(Infinity, Infinity);
793
+ this.limit.pushChild(this.nraLimit);
794
+ result = this.nra.check(Infinity);
795
+ this.limit.popChild(this.nraLimit);
781
796
  stats.nraCalls++;
782
797
  }
783
- if (result === L_UNDEF && !hasNoEffect()) result = L_FALSE;
798
+ if (result === L_UNDEF && !hasNoEffect() && this.limit.inc()) result = L_FALSE;
784
799
  stats.nlaLemmas += this.lemmas.length;
785
800
  return result;
786
801
  }
@@ -790,10 +805,15 @@ export class NlaCore {
790
805
  }
791
806
 
792
807
  private boundedNlsat() {
793
- const budget = 100000;
794
- const result = this.nra.check(100, budget);
808
+ const rlimitBudget = 100000;
809
+ this.limit.pushChild(this.nraLimit);
810
+ this.nraLimit.push(rlimitBudget);
811
+ const result = this.nra.check(100);
812
+ const rlimitConsumed = this.nraLimit.count;
813
+ this.nraLimit.pop();
814
+ this.limit.popChild(this.nraLimit);
795
815
  this.stats.nraCalls++;
796
- this.nlsatBackoff.update(result === L_FALSE || this.nra.steps < budget / 10);
816
+ this.nlsatBackoff.update(result === L_FALSE || rlimitConsumed < rlimitBudget / 10);
797
817
  if (result === L_TRUE) this.clear();
798
818
  return result;
799
819
  }
@@ -1,4 +1,3 @@
1
- // Deviation from Z3: its nlsat conflict check (is_nla_conflict) is not ported.
2
1
  import { PddMemoryOutError } from "../../../../errors";
3
2
  import { getEuclideanMod, getGcd, getLcm, Rational } from "../rational";
4
3
  import { KIND_EQ, KIND_GE, KIND_LE, KIND_LT, KIND_NE } from "./constraints";
@@ -224,8 +223,12 @@ export class Grobner {
224
223
  if (!poly.isOffset()) return false;
225
224
  const variable = poly.var();
226
225
  if (this.core.varIsFixed(variable)) return false;
227
- const coeff = poly.hi().val();
228
- const offset = poly.lo().val().neg();
226
+ const hi = poly.hi();
227
+ const coeff = hi.val();
228
+ hi.release();
229
+ const lo = poly.lo();
230
+ const offset = lo.val().neg();
231
+ lo.release();
229
232
  const lcm = getRationalLcm(getDenominator(coeff), getDenominator(offset));
230
233
  const ineq = mkIneq(new LarTerm([[variable, coeff.mul(lcm)]]), KIND_EQ, offset.mul(lcm));
231
234
  return this.addLemmaUnlessHolds(equation, ineq);
@@ -239,31 +242,49 @@ export class Grobner {
239
242
 
240
243
  private propagateFactorizationOf(equation: Equation) {
241
244
  const factors = equation.poly.varFactors();
242
- if (factors.vars.length === 0 || !factors.poly.isLinear()) return false;
245
+ if (factors.vars.length === 0 || !factors.poly.isLinear()) {
246
+ factors.poly.release();
247
+ return false;
248
+ }
243
249
  const linear = this.linearToTerm(factors.poly);
244
250
  const ineqs = factors.vars.map((variable) => mkIneq(variable, KIND_EQ, 0));
245
251
  ineqs.push(mkIneq(linear.term, KIND_EQ, linear.offset.neg()));
246
- if (ineqs.some((ineq) => this.core.ineqHolds(ineq))) return false;
252
+ if (ineqs.some((ineq) => this.core.ineqHolds(ineq))) {
253
+ factors.poly.release();
254
+ return false;
255
+ }
247
256
  const lemma = this.addDependencies(new LemmaBuilder(this.core), equation);
248
257
  for (const ineq of ineqs) lemma.addIneq(ineq);
258
+ factors.poly.release();
249
259
  return true;
250
260
  }
251
261
 
252
262
  private linearToTerm(poly: Pdd): LinearTerm {
263
+ const termPoly = poly.copy();
253
264
  let lcm = Rational.one;
254
- let current = poly;
255
- while (!current.isVal()) {
256
- lcm = getRationalLcm(lcm, getDenominator(current.hi().val()));
257
- current = current.lo();
258
- }
259
- lcm = getRationalLcm(getDenominator(current.val()), lcm);
265
+ const lcmPoly = termPoly.copy();
266
+ while (!lcmPoly.isVal()) {
267
+ const hi = lcmPoly.hi();
268
+ lcm = getRationalLcm(lcm, getDenominator(hi.val()));
269
+ hi.release();
270
+ const lo = lcmPoly.lo();
271
+ lcmPoly.assign(lo);
272
+ lo.release();
273
+ }
274
+ lcm = getRationalLcm(getDenominator(lcmPoly.val()), lcm);
260
275
  const term = new LarTerm();
261
- current = poly;
262
- while (!current.isVal()) {
263
- term.addMonomial(lcm.mul(current.hi().val()), current.var());
264
- current = current.lo();
276
+ while (!termPoly.isVal()) {
277
+ const hi = termPoly.hi();
278
+ term.addMonomial(lcm.mul(hi.val()), termPoly.var());
279
+ hi.release();
280
+ const lo = termPoly.lo();
281
+ termPoly.assign(lo);
282
+ lo.release();
265
283
  }
266
- return { term, offset: lcm.mul(current.val()) };
284
+ const offset = lcm.mul(termPoly.val());
285
+ lcmPoly.release();
286
+ termPoly.release();
287
+ return { term, offset };
267
288
  }
268
289
 
269
290
  private getValue(poly: Pdd) {
@@ -278,42 +299,61 @@ export class Grobner {
278
299
  }
279
300
 
280
301
  private propagateGcdTestOf(equation: Equation) {
281
- let poly = equation.poly;
282
- if (poly.isLinear() || poly.isVal() || !this.isIntPoly(poly)) return false;
283
- if (this.getValue(poly).isZero()) return false;
284
- const powers = poly.getPowers();
285
- if (powers.length === 0) return false;
286
- let lcm = Rational.one;
287
- for (const value of poly.coefficients()) lcm = getRationalLcm(lcm, getDenominator(value.coeff));
288
- if (!lcm.equals(Rational.one)) poly = poly.mul(lcm);
289
- const manager = this.manager;
290
- const failsGcdTest = (substituted: Pdd) => {
291
- let gcd = Rational.zero;
292
- let constant = Rational.zero;
293
- for (const value of substituted.coefficients()) {
294
- if (value.isConstant) {
295
- constant = constant.add(value.coeff);
296
- continue;
297
- }
298
- gcd = getRationalGcd(gcd, value.coeff);
299
- if (gcd.equals(Rational.one)) return false;
300
- }
301
- return !constant.isZero() && !divides(gcd, constant);
302
- };
303
- for (const entry of powers) {
304
- if (entry.degree > 4) continue;
305
- const scaled = manager.mkVar(entry.var).mul(manager.mkVal(Rational.fromNumber(entry.degree)));
306
- let isGcdFailing = true;
307
- for (let residue = 0; isGcdFailing && residue < entry.degree; residue++) {
308
- const replacement = scaled.add(manager.mkVal(Rational.fromNumber(residue)));
309
- if (!failsGcdTest(poly.substPdd(entry.var, replacement))) isGcdFailing = false;
302
+ const poly = equation.poly.copy();
303
+ try {
304
+ if (poly.isLinear() || poly.isVal() || !this.isIntPoly(poly)) return false;
305
+ if (this.getValue(poly).isZero()) return false;
306
+ const powers = poly.getPowers();
307
+ if (powers.length === 0) return false;
308
+ let lcm = Rational.one;
309
+ for (const value of poly.coefficients()) {
310
+ lcm = getRationalLcm(lcm, getDenominator(value.coeff));
310
311
  }
311
- if (isGcdFailing) {
312
- this.addDependencies(new LemmaBuilder(this.core), equation);
313
- return true;
312
+ if (!lcm.equals(Rational.one)) poly.mulAssign(lcm);
313
+ const manager = this.manager;
314
+ const failsGcdTest = (substituted: Pdd) => {
315
+ let gcd = Rational.zero;
316
+ let constant = Rational.zero;
317
+ for (const value of substituted.coefficients()) {
318
+ if (value.isConstant) {
319
+ constant = constant.add(value.coeff);
320
+ continue;
321
+ }
322
+ gcd = getRationalGcd(gcd, value.coeff);
323
+ if (gcd.equals(Rational.one)) return false;
324
+ }
325
+ return !constant.isZero() && !divides(gcd, constant);
326
+ };
327
+ for (const entry of powers) {
328
+ if (entry.degree > 4) continue;
329
+ const variable = manager.mkVar(entry.var);
330
+ const degree = manager.mkVal(Rational.fromNumber(entry.degree));
331
+ const scaled = variable.mul(degree);
332
+ degree.release();
333
+ variable.release();
334
+ try {
335
+ let isGcdFailing = true;
336
+ for (let residue = 0; isGcdFailing && residue < entry.degree; residue++) {
337
+ const residuePdd = manager.mkVal(Rational.fromNumber(residue));
338
+ const replacement = scaled.add(residuePdd);
339
+ residuePdd.release();
340
+ const substituted = poly.substPdd(entry.var, replacement);
341
+ if (!failsGcdTest(substituted)) isGcdFailing = false;
342
+ substituted.release();
343
+ replacement.release();
344
+ }
345
+ if (isGcdFailing) {
346
+ this.addDependencies(new LemmaBuilder(this.core), equation);
347
+ return true;
348
+ }
349
+ } finally {
350
+ scaled.release();
351
+ }
314
352
  }
353
+ return false;
354
+ } finally {
355
+ poly.release();
315
356
  }
316
- return false;
317
357
  }
318
358
 
319
359
  private propagateQuotientsOf(equation: Equation) {
@@ -330,11 +370,18 @@ export class Grobner {
330
370
  if (this.propagateModResidue(equation)) return true;
331
371
  let isLemmaFound = false;
332
372
  for (const variable of nlVars) {
333
- const factor = poly.factor(variable, 1);
334
- if (!factor.rest.isLinear()) continue;
335
- const lc = lcm.equals(Rational.one) ? factor.lc : factor.lc.mul(lcm);
336
- const rest = lcm.equals(Rational.one) ? factor.rest : factor.rest.mul(lcm);
337
- if (this.addQuotientLemma(equation, variable, lc, rest)) isLemmaFound = true;
373
+ const lc = this.manager.zero();
374
+ const rest = this.manager.zero();
375
+ poly.factor(variable, 1, lc, rest);
376
+ if (rest.isLinear()) {
377
+ if (!lcm.equals(Rational.one)) {
378
+ lc.mulAssign(lcm);
379
+ rest.mulAssign(lcm);
380
+ }
381
+ if (this.addQuotientLemma(equation, variable, lc, rest)) isLemmaFound = true;
382
+ }
383
+ rest.release();
384
+ lc.release();
338
385
  }
339
386
  return isLemmaFound;
340
387
  }
@@ -748,28 +795,38 @@ export class Grobner {
748
795
  }
749
796
 
750
797
  private getTermPdd(term: LarTerm, ref: DepRef) {
751
- let result = this.manager.mkVal(Rational.zero);
752
- for (const entry of term.coeffs) result = result.add(this.getPddExpr(entry[1], entry[0], ref));
798
+ const result = this.manager.mkVal(Rational.zero);
799
+ for (const entry of term.coeffs) {
800
+ const expr = this.getPddExpr(entry[1], entry[0], ref);
801
+ result.addAssign(expr);
802
+ expr.release();
803
+ }
753
804
  return result;
754
805
  }
755
806
 
756
807
  private getPddExpr(coeff: Rational, lpVar: number, ref: DepRef) {
757
808
  const core = this.core;
758
- let result = this.manager.mkVal(coeff);
809
+ const result = this.manager.mkVal(coeff);
759
810
  const vars = [lpVar];
760
811
  const zeroRef = { dep: ref.dep };
761
812
  for (let current = vars.pop(); current !== undefined; current = vars.pop()) {
762
813
  if (NLA_PARAMS.grobnerSubsFixed > 0 && core.varIsFixedToZero(current)) {
763
- result = this.manager.mkVal(this.getValOfFixedVarWithDeps(current, zeroRef));
814
+ const fixed = this.manager.mkVal(this.getValOfFixedVarWithDeps(current, zeroRef));
815
+ result.assign(fixed);
816
+ fixed.release();
764
817
  ref.dep = zeroRef.dep;
765
818
  return result;
766
819
  }
767
820
  if (NLA_PARAMS.grobnerSubsFixed === 1 && core.varIsFixed(current)) {
768
- result = result.mul(this.getValOfFixedVarWithDeps(current, ref));
821
+ result.mulAssign(this.getValOfFixedVarWithDeps(current, ref));
769
822
  } else if (this.expandsTerms && core.lra.hasTerm(current)) {
770
- result = result.mul(this.getTermPdd(core.lra.getTerm(current), ref));
823
+ const termPdd = this.getTermPdd(core.lra.getTerm(current), ref);
824
+ result.mulAssign(termPdd);
825
+ termPdd.release();
771
826
  } else if (!core.isMonicVar(current)) {
772
- result = result.mul(this.manager.mkVar(current));
827
+ const variable = this.manager.mkVar(current);
828
+ result.mulAssign(variable);
829
+ variable.release();
773
830
  } else {
774
831
  vars.push(...core.emons.get(current).vars);
775
832
  }
@@ -777,17 +834,20 @@ export class Grobner {
777
834
  return result;
778
835
  }
779
836
 
780
- private getSolvedForm(poly: Pdd) {
837
+ private getSolvedForm(poly: Pdd, solution: Pdd) {
781
838
  if (!poly.isLinear()) return null;
839
+ solution.assign(poly);
782
840
  let numLo = 0;
783
841
  let numHi = 0;
784
842
  let lo = 0;
785
843
  let hi = 0;
786
844
  let loCoeff = Rational.zero;
787
845
  let hiCoeff = Rational.zero;
788
- for (let current = poly; !current.isVal(); current = current.lo()) {
789
- const variable = current.var();
790
- const value = current.hi().val();
846
+ while (!solution.isVal()) {
847
+ const variable = solution.var();
848
+ const hiPoly = solution.hi();
849
+ const value = hiPoly.val();
850
+ hiPoly.release();
791
851
  const type = this.core.lra.core.getColumnType(variable);
792
852
  if (type === COLUMN_LOWER || type === COLUMN_UPPER) {
793
853
  if (value.isPositive() === (type === COLUMN_LOWER)) {
@@ -803,40 +863,57 @@ export class Grobner {
803
863
  return null;
804
864
  }
805
865
  if (numLo > 1 && numHi > 1) return null;
866
+ const loPoly = solution.lo();
867
+ solution.assign(loPoly);
868
+ loPoly.release();
806
869
  }
807
870
  const isLo = numLo === 1 && numHi > 1;
808
871
  if (!isLo && !(numHi === 1 && numLo > 1)) return null;
809
872
  const variable = isLo ? lo : hi;
810
873
  const coeff = isLo ? loCoeff : hiCoeff;
811
- const solution = this.manager.mkVar(variable).mul(coeff).sub(poly);
812
- return {
813
- variable,
814
- poly: coeff.equals(Rational.one) ? solution : solution.mul(Rational.one.div(coeff)),
815
- };
874
+ const variablePdd = this.manager.mkVar(variable);
875
+ const scaled = variablePdd.mul(coeff);
876
+ const difference = scaled.sub(poly);
877
+ solution.assign(difference);
878
+ difference.release();
879
+ scaled.release();
880
+ variablePdd.release();
881
+ if (!coeff.equals(Rational.one)) solution.mulAssign(Rational.one.div(coeff));
882
+ return variable;
816
883
  }
817
884
 
818
885
  private addEq(poly: Pdd, dep: Dependency) {
819
- const simplified = this.solver.simplify(poly, dep);
820
- const solved = this.getSolvedForm(simplified.poly);
821
- if (solved === null) this.solver.add(simplified.poly, simplified.dep);
822
- else this.solver.addSubst(solved.variable, solved.poly, simplified.dep);
886
+ const solution = this.manager.zero();
887
+ const simplifiedDep = this.solver.simplify(poly, dep);
888
+ const variable = this.getSolvedForm(poly, solution);
889
+ if (variable === null) this.solver.add(poly, simplifiedDep);
890
+ else this.solver.addSubst(variable, solution, simplifiedDep);
891
+ solution.release();
823
892
  }
824
893
 
825
894
  private addFixedMonic(lpVar: number) {
826
895
  const ref: DepRef = { dep: null };
827
- let product = this.manager.mkVal(Rational.one);
896
+ const product = this.manager.mkVal(Rational.one);
828
897
  for (const factor of this.core.emons.get(lpVar).vars) {
829
- product = product.mul(this.getPddExpr(Rational.one, factor, ref));
898
+ const expr = this.getPddExpr(Rational.one, factor, ref);
899
+ product.mulAssign(expr);
900
+ expr.release();
830
901
  }
831
- product = product.sub(this.getValOfFixedVarWithDeps(lpVar, ref));
902
+ product.subAssign(this.getValOfFixedVarWithDeps(lpVar, ref));
832
903
  this.addEq(product, ref.dep);
904
+ product.release();
833
905
  }
834
906
 
835
907
  private addRow(row: RowCell[]) {
836
908
  const ref: DepRef = { dep: null };
837
- let sum = this.manager.mkVal(Rational.zero);
838
- for (const cell of row) sum = sum.add(this.getPddExpr(cell.coeff, cell.column, ref));
909
+ const sum = this.manager.mkVal(Rational.zero);
910
+ for (const cell of row) {
911
+ const expr = this.getPddExpr(cell.coeff, cell.column, ref);
912
+ sum.addAssign(expr);
913
+ expr.release();
914
+ }
839
915
  this.addEq(sum, ref.dep);
916
+ sum.release();
840
917
  }
841
918
 
842
919
  private setLevel2var() {