tla-ts 0.0.10-dev.d618a2d79 → 0.0.10-dev.f08db3b1f

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.
@@ -223,8 +223,12 @@ export class Grobner {
223
223
  if (!poly.isOffset()) return false;
224
224
  const variable = poly.var();
225
225
  if (this.core.varIsFixed(variable)) return false;
226
- const coeff = poly.hi().val();
227
- 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();
228
232
  const lcm = getRationalLcm(getDenominator(coeff), getDenominator(offset));
229
233
  const ineq = mkIneq(new LarTerm([[variable, coeff.mul(lcm)]]), KIND_EQ, offset.mul(lcm));
230
234
  return this.addLemmaUnlessHolds(equation, ineq);
@@ -238,31 +242,49 @@ export class Grobner {
238
242
 
239
243
  private propagateFactorizationOf(equation: Equation) {
240
244
  const factors = equation.poly.varFactors();
241
- 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
+ }
242
249
  const linear = this.linearToTerm(factors.poly);
243
250
  const ineqs = factors.vars.map((variable) => mkIneq(variable, KIND_EQ, 0));
244
251
  ineqs.push(mkIneq(linear.term, KIND_EQ, linear.offset.neg()));
245
- 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
+ }
246
256
  const lemma = this.addDependencies(new LemmaBuilder(this.core), equation);
247
257
  for (const ineq of ineqs) lemma.addIneq(ineq);
258
+ factors.poly.release();
248
259
  return true;
249
260
  }
250
261
 
251
262
  private linearToTerm(poly: Pdd): LinearTerm {
263
+ const termPoly = poly.copy();
252
264
  let lcm = Rational.one;
253
- let current = poly;
254
- while (!current.isVal()) {
255
- lcm = getRationalLcm(lcm, getDenominator(current.hi().val()));
256
- current = current.lo();
257
- }
258
- 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);
259
275
  const term = new LarTerm();
260
- current = poly;
261
- while (!current.isVal()) {
262
- term.addMonomial(lcm.mul(current.hi().val()), current.var());
263
- 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();
264
283
  }
265
- return { term, offset: lcm.mul(current.val()) };
284
+ const offset = lcm.mul(termPoly.val());
285
+ lcmPoly.release();
286
+ termPoly.release();
287
+ return { term, offset };
266
288
  }
267
289
 
268
290
  private getValue(poly: Pdd) {
@@ -277,42 +299,61 @@ export class Grobner {
277
299
  }
278
300
 
279
301
  private propagateGcdTestOf(equation: Equation) {
280
- let poly = equation.poly;
281
- if (poly.isLinear() || poly.isVal() || !this.isIntPoly(poly)) return false;
282
- if (this.getValue(poly).isZero()) return false;
283
- const powers = poly.getPowers();
284
- if (powers.length === 0) return false;
285
- let lcm = Rational.one;
286
- for (const value of poly.coefficients()) lcm = getRationalLcm(lcm, getDenominator(value.coeff));
287
- if (!lcm.equals(Rational.one)) poly = poly.mul(lcm);
288
- const manager = this.manager;
289
- const failsGcdTest = (substituted: Pdd) => {
290
- let gcd = Rational.zero;
291
- let constant = Rational.zero;
292
- for (const value of substituted.coefficients()) {
293
- if (value.isConstant) {
294
- constant = constant.add(value.coeff);
295
- continue;
296
- }
297
- gcd = getRationalGcd(gcd, value.coeff);
298
- if (gcd.equals(Rational.one)) return false;
299
- }
300
- return !constant.isZero() && !divides(gcd, constant);
301
- };
302
- for (const entry of powers) {
303
- if (entry.degree > 4) continue;
304
- const scaled = manager.mkVar(entry.var).mul(manager.mkVal(Rational.fromNumber(entry.degree)));
305
- let isGcdFailing = true;
306
- for (let residue = 0; isGcdFailing && residue < entry.degree; residue++) {
307
- const replacement = scaled.add(manager.mkVal(Rational.fromNumber(residue)));
308
- 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));
309
311
  }
310
- if (isGcdFailing) {
311
- this.addDependencies(new LemmaBuilder(this.core), equation);
312
- 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
+ }
313
352
  }
353
+ return false;
354
+ } finally {
355
+ poly.release();
314
356
  }
315
- return false;
316
357
  }
317
358
 
318
359
  private propagateQuotientsOf(equation: Equation) {
@@ -329,11 +370,18 @@ export class Grobner {
329
370
  if (this.propagateModResidue(equation)) return true;
330
371
  let isLemmaFound = false;
331
372
  for (const variable of nlVars) {
332
- const factor = poly.factor(variable, 1);
333
- if (!factor.rest.isLinear()) continue;
334
- const lc = lcm.equals(Rational.one) ? factor.lc : factor.lc.mul(lcm);
335
- const rest = lcm.equals(Rational.one) ? factor.rest : factor.rest.mul(lcm);
336
- 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();
337
385
  }
338
386
  return isLemmaFound;
339
387
  }
@@ -747,28 +795,38 @@ export class Grobner {
747
795
  }
748
796
 
749
797
  private getTermPdd(term: LarTerm, ref: DepRef) {
750
- let result = this.manager.mkVal(Rational.zero);
751
- 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
+ }
752
804
  return result;
753
805
  }
754
806
 
755
807
  private getPddExpr(coeff: Rational, lpVar: number, ref: DepRef) {
756
808
  const core = this.core;
757
- let result = this.manager.mkVal(coeff);
809
+ const result = this.manager.mkVal(coeff);
758
810
  const vars = [lpVar];
759
811
  const zeroRef = { dep: ref.dep };
760
812
  for (let current = vars.pop(); current !== undefined; current = vars.pop()) {
761
813
  if (NLA_PARAMS.grobnerSubsFixed > 0 && core.varIsFixedToZero(current)) {
762
- 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();
763
817
  ref.dep = zeroRef.dep;
764
818
  return result;
765
819
  }
766
820
  if (NLA_PARAMS.grobnerSubsFixed === 1 && core.varIsFixed(current)) {
767
- result = result.mul(this.getValOfFixedVarWithDeps(current, ref));
821
+ result.mulAssign(this.getValOfFixedVarWithDeps(current, ref));
768
822
  } else if (this.expandsTerms && core.lra.hasTerm(current)) {
769
- 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();
770
826
  } else if (!core.isMonicVar(current)) {
771
- result = result.mul(this.manager.mkVar(current));
827
+ const variable = this.manager.mkVar(current);
828
+ result.mulAssign(variable);
829
+ variable.release();
772
830
  } else {
773
831
  vars.push(...core.emons.get(current).vars);
774
832
  }
@@ -776,17 +834,20 @@ export class Grobner {
776
834
  return result;
777
835
  }
778
836
 
779
- private getSolvedForm(poly: Pdd) {
837
+ private getSolvedForm(poly: Pdd, solution: Pdd) {
780
838
  if (!poly.isLinear()) return null;
839
+ solution.assign(poly);
781
840
  let numLo = 0;
782
841
  let numHi = 0;
783
842
  let lo = 0;
784
843
  let hi = 0;
785
844
  let loCoeff = Rational.zero;
786
845
  let hiCoeff = Rational.zero;
787
- for (let current = poly; !current.isVal(); current = current.lo()) {
788
- const variable = current.var();
789
- 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();
790
851
  const type = this.core.lra.core.getColumnType(variable);
791
852
  if (type === COLUMN_LOWER || type === COLUMN_UPPER) {
792
853
  if (value.isPositive() === (type === COLUMN_LOWER)) {
@@ -802,40 +863,57 @@ export class Grobner {
802
863
  return null;
803
864
  }
804
865
  if (numLo > 1 && numHi > 1) return null;
866
+ const loPoly = solution.lo();
867
+ solution.assign(loPoly);
868
+ loPoly.release();
805
869
  }
806
870
  const isLo = numLo === 1 && numHi > 1;
807
871
  if (!isLo && !(numHi === 1 && numLo > 1)) return null;
808
872
  const variable = isLo ? lo : hi;
809
873
  const coeff = isLo ? loCoeff : hiCoeff;
810
- const solution = this.manager.mkVar(variable).mul(coeff).sub(poly);
811
- return {
812
- variable,
813
- poly: coeff.equals(Rational.one) ? solution : solution.mul(Rational.one.div(coeff)),
814
- };
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;
815
883
  }
816
884
 
817
885
  private addEq(poly: Pdd, dep: Dependency) {
818
- const simplified = this.solver.simplify(poly, dep);
819
- const solved = this.getSolvedForm(simplified.poly);
820
- if (solved === null) this.solver.add(simplified.poly, simplified.dep);
821
- 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();
822
892
  }
823
893
 
824
894
  private addFixedMonic(lpVar: number) {
825
895
  const ref: DepRef = { dep: null };
826
- let product = this.manager.mkVal(Rational.one);
896
+ const product = this.manager.mkVal(Rational.one);
827
897
  for (const factor of this.core.emons.get(lpVar).vars) {
828
- 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();
829
901
  }
830
- product = product.sub(this.getValOfFixedVarWithDeps(lpVar, ref));
902
+ product.subAssign(this.getValOfFixedVarWithDeps(lpVar, ref));
831
903
  this.addEq(product, ref.dep);
904
+ product.release();
832
905
  }
833
906
 
834
907
  private addRow(row: RowCell[]) {
835
908
  const ref: DepRef = { dep: null };
836
- let sum = this.manager.mkVal(Rational.zero);
837
- 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
+ }
838
915
  this.addEq(sum, ref.dep);
916
+ sum.release();
839
917
  }
840
918
 
841
919
  private setLevel2var() {
@@ -19,8 +19,12 @@ export const getPddInterval = (
19
19
  return result;
20
20
  }
21
21
  const varInterval = getVarInterval(poly.var(), mode);
22
- const hi = getPddInterval(poly.hi(), mode, getVarInterval);
23
- const lo = getPddInterval(poly.lo(), mode, getVarInterval);
22
+ const hiPoly = poly.hi();
23
+ const hi = getPddInterval(hiPoly, mode, getVarInterval);
24
+ hiPoly.release();
25
+ const loPoly = poly.lo();
26
+ const lo = getPddInterval(loPoly, mode, getVarInterval);
27
+ loPoly.release();
24
28
  const product = mkDepInterval();
25
29
  mul(hi, varInterval, product, mode);
26
30
  add(product, lo, result, mode);
@@ -1,4 +1,5 @@
1
1
  import { mkJoin } from "./dependency";
2
+ import type { Pdd } from "./pdd";
2
3
  import { type Equation, type PddSolver, SetUpdate, SOLVED, TO_SIMPLIFY } from "./pdd-solver";
3
4
  import { shrink } from "../array";
4
5
 
@@ -17,9 +18,12 @@ export class PddSimplifier {
17
18
  }
18
19
 
19
20
  private simplifyLinearStep(isBinary: boolean) {
20
- const linear = this.solver.toSimplify.filter((equation) =>
21
- isBinary ? equation.poly.isBinary() : equation.poly.isLinear(),
22
- );
21
+ const linear: Equation[] = [];
22
+ for (const equation of this.solver.toSimplify) {
23
+ const poly = equation.poly.copy();
24
+ if (isBinary ? poly.isBinary() : poly.isLinear()) linear.push(equation);
25
+ poly.release();
26
+ }
23
27
  if (linear.length === 0) return false;
24
28
  return this.simplifyLinear(linear);
25
29
  }
@@ -40,8 +44,10 @@ export class PddSimplifier {
40
44
  for (let index = 0, length = uses.length; index < length; index++) {
41
45
  const target = uses[index];
42
46
  if (source === target || solver.isTrivial(target)) continue;
43
- if (!source.poly.isBinary() && !target.poly.isLinear()) {
47
+ const targetPoly = target.poly.copy();
48
+ if (!source.poly.isBinary() && !targetPoly.isLinear()) {
44
49
  isAllReduced = false;
50
+ targetPoly.release();
45
51
  continue;
46
52
  }
47
53
  this.removeFromUse(target, useList, variable);
@@ -57,6 +63,7 @@ export class PddSimplifier {
57
63
  solver.pushEquation(TO_SIMPLIFY, target);
58
64
  }
59
65
  this.addToUse(target, useList);
66
+ targetPoly.release();
60
67
  }
61
68
  if (isAllReduced) linear[kept++] = source;
62
69
  }
@@ -67,7 +74,7 @@ export class PddSimplifier {
67
74
  solver.pushEquation(SOLVED, source);
68
75
  }
69
76
  }
70
- for (const equation of trivial) solver.popEquation(equation);
77
+ for (const equation of trivial) solver.delEquation(equation);
71
78
  return kept > 0 || hasConflict;
72
79
  }
73
80
 
@@ -78,32 +85,55 @@ export class PddSimplifier {
78
85
  const update = new SetUpdate(solver.toSimplify);
79
86
  for (; update.i < update.size; update.i++) {
80
87
  const first = update.get();
81
- const poly = first.poly;
82
- const second = los.get(poly.lo().root) ?? first;
83
- if (second === first) los.set(poly.lo().root, first);
84
- if (
85
- second !== first &&
86
- (poly.hi().isVal() || second.poly.hi().isVal()) &&
87
- !poly.lo().isVal()
88
- ) {
89
- first.poly = poly.sub(second.poly);
88
+ const poly = first.poly.copy();
89
+ const lo = poly.lo();
90
+ const second = los.get(lo.root) ?? first;
91
+ if (second === first) los.set(lo.root, first);
92
+ lo.release();
93
+ const secondPoly = second.poly.copy();
94
+ if (second !== first && this.isCcReducible(poly, secondPoly)) {
95
+ const difference = poly.sub(second.poly);
96
+ first.poly.assign(difference);
97
+ difference.release();
90
98
  first.dep = mkJoin(first.dep, second.dep);
91
99
  isReduced = true;
92
- if (solver.isTrivial(first) || solver.checkConflict(first)) continue;
100
+ const isTrivial = solver.isTrivial(first);
101
+ if (isTrivial) solver.retire(first);
102
+ if (isTrivial || solver.checkConflict(first)) {
103
+ secondPoly.release();
104
+ poly.release();
105
+ continue;
106
+ }
93
107
  }
94
108
  update.nextj();
109
+ secondPoly.release();
110
+ poly.release();
95
111
  }
96
112
  update.finish();
97
113
  return isReduced;
98
114
  }
99
115
 
116
+ private isCcReducible(poly: Pdd, secondPoly: Pdd) {
117
+ const hi = poly.hi();
118
+ const secondHi = hi.isVal() ? null : secondPoly.hi();
119
+ const lo = secondHi === null || secondHi.isVal() ? poly.lo() : null;
120
+ const isReducible = lo !== null && !lo.isVal();
121
+ lo?.release();
122
+ secondHi?.release();
123
+ hi.release();
124
+ return isReducible;
125
+ }
126
+
100
127
  private simplifyLeafStep() {
101
128
  const solver = this.solver;
102
129
  const useList = this.getUseList();
103
130
  for (let index = 0; index < solver.toSimplify.length; index++) {
104
131
  const source = solver.toSimplify[index];
105
- const poly = source.poly;
106
- if (poly.isVal() || !poly.hi().isVal()) continue;
132
+ const poly = source.poly.copy();
133
+ if (poly.isVal() || !this.isHiVal(poly)) {
134
+ poly.release();
135
+ continue;
136
+ }
107
137
  const leaves = useList[poly.var()].filter(
108
138
  (target) => target !== source && target.poly.varIsLeaf(poly.var()),
109
139
  );
@@ -112,16 +142,18 @@ export class PddSimplifier {
112
142
  const isLeadingTermChanged = solver.simplifyUsing(target, source);
113
143
  this.addToUse(target, useList);
114
144
  if (solver.isTrivial(target)) {
115
- solver.popEquation(target);
145
+ solver.delEquation(target);
116
146
  } else if (target.poly.isVal()) {
117
147
  solver.popEquation(target);
118
148
  solver.setConflict(target);
149
+ poly.release();
119
150
  return true;
120
151
  } else if (isLeadingTermChanged) {
121
152
  solver.popEquation(target);
122
153
  solver.pushEquation(TO_SIMPLIFY, target);
123
154
  }
124
155
  }
156
+ poly.release();
125
157
  }
126
158
  return false;
127
159
  }
@@ -133,18 +165,26 @@ export class PddSimplifier {
133
165
  let hasSolved = false;
134
166
  for (; update.i < update.size; update.i++) {
135
167
  const equation = update.get();
136
- const poly = equation.poly;
137
- if (!poly.isVal() && poly.hi().isVal() && useList[poly.var()].length === 1) {
168
+ const poly = equation.poly.copy();
169
+ if (!poly.isVal() && this.isHiVal(poly) && useList[poly.var()].length === 1) {
138
170
  solver.pushEquation(SOLVED, equation);
139
171
  hasSolved = true;
140
172
  } else {
141
173
  update.nextj();
142
174
  }
175
+ poly.release();
143
176
  }
144
177
  update.finish();
145
178
  return hasSolved;
146
179
  }
147
180
 
181
+ private isHiVal(poly: Pdd) {
182
+ const hi = poly.hi();
183
+ const isVal = hi.isVal();
184
+ hi.release();
185
+ return isVal;
186
+ }
187
+
148
188
  private addToUse(equation: Equation, useList: Equation[][]) {
149
189
  for (const variable of equation.poly.freeVars()) {
150
190
  while (useList.length <= variable) useList.push([]);