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.
@@ -22,10 +22,9 @@ import {
22
22
  SORT_INT,
23
23
  SORT_REAL,
24
24
  } from "./ast";
25
- import { Expr2Polynomial } from "./expr2polynomial";
25
+ import { toPolynomial } from "./expr2polynomial";
26
26
  import { getLcm } from "./rational";
27
27
  import { flipKind } from "./nlsat/atoms";
28
- import { Factors, type Polynomial, PolynomialRef } from "./nlsat/polynomial";
29
28
  import type { NlsatSolver } from "./nlsat/solver";
30
29
  import { TRANSCENDENTAL_OPS } from "./nlsat/transcendental-eval";
31
30
  import type { IneqKind } from "./nlsat/types";
@@ -40,54 +39,38 @@ export const goal2nlsat = (
40
39
  isFactoring = true,
41
40
  ) => {
42
41
  const pm = solver.pm;
43
- const expr2poly = new Expr2Polynomial(manager, pm, t2x, (isInt) => solver.mkVar(isInt));
44
42
 
45
- const factorAtom = (polynomial: Polynomial, kind: IneqKind) => {
46
- const factors = new Factors(pm);
47
- pm.factor(polynomial, factors);
48
- const literal = solver.mkIneqLiteral(
49
- factors.constant < 0n ? flipKind(kind) : kind,
50
- factors.factors,
51
- factors.degrees.map((degree) => degree % 2 === 0),
52
- );
53
- factors.reset();
54
- return literal;
43
+ const mkVar = (term: number) => {
44
+ let variable = t2x.get(term);
45
+ if (variable === undefined) {
46
+ variable = solver.mkVar(manager.sorts[term] === SORT_INT);
47
+ t2x.set(term, variable);
48
+ }
49
+ return variable;
55
50
  };
56
51
 
57
- const processIneqPolynomial = (p: PolynomialRef, kind: IneqKind) => {
58
- const polynomial = p.get();
52
+ const processIneq = (atom: number, kind: IneqKind) => {
53
+ const lhs = toPolynomial(manager, pm, manager.getArg(atom, 0), mkVar);
54
+ const rhs = toPolynomial(manager, pm, manager.getArg(atom, 1), mkVar);
55
+ const lcm = getLcm(lhs.denominator, rhs.denominator);
56
+ const polynomial = pm.addmul(
57
+ lcm / lhs.denominator,
58
+ lhs.polynomial,
59
+ -(lcm / rhs.denominator),
60
+ rhs.polynomial,
61
+ );
59
62
  if (pm.isConst(polynomial)) {
60
63
  const sign = pm.isZero(polynomial) ? 0 : polynomial.as[0] > 0n ? 1 : -1;
61
64
  const isTrue = kind === "EQ" ? sign === 0 : kind === "LT" ? sign < 0 : sign > 0;
62
65
  return isTrue ? 0 : 1;
63
66
  }
64
- return isFactoring
65
- ? factorAtom(polynomial, kind)
66
- : solver.mkIneqLiteral(kind, [polynomial], [false]);
67
- };
68
-
69
- const processIneq = (atom: number, kind: IneqKind) => {
70
- const first = new PolynomialRef(pm);
71
- const second = new PolynomialRef(pm);
72
- const p = new PolynomialRef(pm);
73
- const firstDenominator = expr2poly.toPolynomial(manager.getArg(atom, 0), first);
74
- const secondDenominator = expr2poly.toPolynomial(manager.getArg(atom, 1), second);
75
- const lcm = getLcm(firstDenominator, secondDenominator);
76
- p.set(
77
- pm.addmulTerms(
78
- lcm / firstDenominator,
79
- pm.unit,
80
- first.get(),
81
- -(lcm / secondDenominator),
82
- pm.unit,
83
- second.get(),
84
- ),
67
+ if (!isFactoring) return solver.mkIneqLiteral(kind, [polynomial], [false]);
68
+ const factors = pm.factor(polynomial);
69
+ return solver.mkIneqLiteral(
70
+ factors.constant < 0n ? flipKind(kind) : kind,
71
+ factors.factors,
72
+ factors.degrees.map((degree) => degree % 2 === 0),
85
73
  );
86
- const literal = processIneqPolynomial(p, kind);
87
- p.reset();
88
- second.reset();
89
- first.reset();
90
- return literal;
91
74
  };
92
75
 
93
76
  const processBoolVar = (atom: number) => {
@@ -146,29 +129,20 @@ export const goal2nlsat = (
146
129
  // The polynomial conversion treats a transcendental application, argument included, as one
147
130
  // variable, so its argument gets a variable here, through an equation unless it is one already.
148
131
  const mkArgVar = (term: number) => {
149
- const p = new PolynomialRef(pm);
150
- const denominator = expr2poly.toPolynomial(term, p);
151
- const polynomial = p.get();
132
+ const result = toPolynomial(manager, pm, term, mkVar);
133
+ const polynomial = result.polynomial;
152
134
  const monomial = polynomial.ms[0];
153
135
  if (
154
- denominator === 1n &&
136
+ result.denominator === 1n &&
155
137
  polynomial.ms.length === 1 &&
156
138
  polynomial.as[0] === 1n &&
157
139
  monomial.size === 1 &&
158
140
  monomial.degrees[0] === 1
159
- ) {
160
- p.reset();
141
+ )
161
142
  return monomial.vars[0];
162
- }
163
143
  const variable = solver.mkVar(false);
164
- const vp = new PolynomialRef(pm);
165
- const equation = new PolynomialRef(pm);
166
- vp.set(pm.mkVar(variable));
167
- equation.set(pm.addmulTerms(denominator, pm.unit, vp.get(), -1n, pm.unit, polynomial));
168
- solver.mkExternalClause([solver.mkIneqLiteral("EQ", [equation.get()], [false])], null);
169
- equation.reset();
170
- vp.reset();
171
- p.reset();
144
+ const equation = pm.addmul(result.denominator, pm.mkVar(variable), -1n, polynomial);
145
+ solver.mkExternalClause([solver.mkIneqLiteral("EQ", [equation], [false])], null);
172
146
  return variable;
173
147
  };
174
148
 
@@ -200,7 +174,6 @@ export const goal2nlsat = (
200
174
  }
201
175
  }
202
176
  if (piVar !== undefined) solver.transcendentals.addPi(piVar);
203
- expr2poly.release();
204
177
  };
205
178
 
206
179
  const hasTermIte = (manager: AstManager, form: number) => {
@@ -795,7 +795,7 @@ export class NlaCore {
795
795
  this.limit.popChild(this.nraLimit);
796
796
  stats.nraCalls++;
797
797
  }
798
- if (result === L_UNDEF && !hasNoEffect() && this.limit.inc()) result = L_FALSE;
798
+ if (result === L_UNDEF && !hasNoEffect()) result = L_FALSE;
799
799
  stats.nlaLemmas += this.lemmas.length;
800
800
  return result;
801
801
  }
@@ -1,9 +1,9 @@
1
- import { SmtCanceledError, SmtInvariantError } from "../../../../errors";
1
+ import { NlsatUnsupportedError, SmtCanceledError, SmtInvariantError } from "../../../../errors";
2
2
  import { L_FALSE, L_TRUE, L_UNDEF } from "../lbool";
3
3
  import { Rational, getLcm } from "../rational";
4
4
  import { Anum } from "../nlsat/algebraic";
5
5
  import { NlsatSolver } from "../nlsat/solver";
6
- import { type Polynomial, PolynomialRef, PolynomialRefVector } from "../nlsat/polynomial";
6
+ import type { Polynomial } from "../nlsat/polynomial";
7
7
  import { isIneqAtom } from "../nlsat/types";
8
8
  import {
9
9
  KIND_EQ,
@@ -92,15 +92,13 @@ const isDyadicArtifact = (lra: LarSolver, constraint: LarConstraint) =>
92
92
  getConstraintCoeffs(lra, constraint).some((entry) => hasLargeDyadicDenominator(entry[1])));
93
93
 
94
94
  export class NraSolver {
95
- private solver: NlsatSolver;
95
+ private solver = new NlsatSolver();
96
96
  private values: Anum[] = [];
97
97
  private lp2nl = new Map<number, number>();
98
98
  private literal2constraint = new Map<number, number>();
99
99
  private vars2mon = new Map<string, number>();
100
100
 
101
- constructor(private core: NlaCore) {
102
- this.solver = new NlsatSolver(core.nraLimit);
103
- }
101
+ constructor(private core: NlaCore) {}
104
102
 
105
103
  get am() {
106
104
  return this.solver.am;
@@ -117,48 +115,53 @@ export class NraSolver {
117
115
  const lra = core.lra;
118
116
  const solver = this.solver;
119
117
  const pm = solver.pm;
120
- const definitions = new PolynomialRefVector(pm);
118
+ const definitions: (Polynomial | null)[] = [];
121
119
  const denominators: bigint[] = [];
122
- const p = new PolynomialRef(pm);
123
- const constant = new PolynomialRef(pm);
124
- const product = new PolynomialRef(pm);
125
120
  for (let lpVar = 0; lpVar < lra.columns.length; lpVar++) {
126
121
  if (!isIncluded(lpVar)) {
127
- definitions.push(pm.zero);
122
+ definitions.push(null);
128
123
  denominators.push(0n);
129
124
  continue;
130
125
  }
131
126
  const variable = solver.mkVar(lra.isInt(lpVar));
132
127
  this.lp2nl.set(lpVar, variable);
133
- let denominator = 1n;
134
128
  if (core.emons.isMonicVar(lpVar)) {
135
- for (const [index, factor] of core.emons.get(lpVar).vars.entries()) {
129
+ let definition: Polynomial | null = null;
130
+ let denominator = 1n;
131
+ for (const factor of core.emons.get(lpVar).vars) {
136
132
  denominator = denominators[factor] * denominator;
137
- p.set(index === 0 ? definitions.get(factor) : pm.mul(p.get(), definitions.get(factor)));
133
+ definition =
134
+ definition === null ? definitions[factor] : pm.mul(definition, definitions[factor]!);
138
135
  }
139
- } else if (lra.hasTerm(lpVar)) {
136
+ definitions.push(definition ?? pm.mkVar(variable));
137
+ denominators.push(denominator);
138
+ continue;
139
+ }
140
+ if (lra.hasTerm(lpVar)) {
140
141
  const term = [...lra.getTerm(lpVar).coeffs];
141
- for (const entry of term)
142
- denominator = getLcm(entry[1].div(Rational.of(denominators[entry[0]])).den, denominator);
143
- for (const [index, entry] of term.entries()) {
142
+ const denominator = term.reduce(
143
+ (result, entry) => getLcm(entry[1].div(Rational.of(denominators[entry[0]])).den, result),
144
+ 1n,
145
+ );
146
+ let definition: Polynomial | null = null;
147
+ for (const entry of term) {
144
148
  const coeff = entry[1].mul(Rational.of(denominator, denominators[entry[0]]));
145
- constant.set(pm.mkConst(coeff.num));
146
- product.set(pm.mul(constant.get(), definitions.get(entry[0])));
147
- p.set(index === 0 ? product.get() : pm.add(p.get(), product.get()));
148
- product.reset();
149
- constant.reset();
149
+ const scaled = pm.mul(pm.mkConst(coeff.num), definitions[entry[0]]!);
150
+ definition = definition === null ? scaled : pm.add(definition, scaled);
150
151
  }
151
- } else p.set(pm.mkVar(variable));
152
- definitions.push(p.getOrNull() ?? pm.zero);
153
- denominators.push(denominator);
154
- p.reset();
152
+ definitions.push(definition ?? pm.zero);
153
+ denominators.push(denominator);
154
+ continue;
155
+ }
156
+ definitions.push(pm.mkVar(variable));
157
+ denominators.push(1n);
155
158
  }
156
159
  return { definitions, denominators };
157
160
  }
158
161
 
159
162
  private addConstraint(
160
163
  constraintIndex: number,
161
- definitions: PolynomialRefVector,
164
+ definitions: readonly (Polynomial | null)[],
162
165
  denominators: readonly bigint[],
163
166
  ) {
164
167
  const lra = this.core.lra;
@@ -171,32 +174,23 @@ export class NraSolver {
171
174
  (result, entry) => getLcm(result, entry[1].div(Rational.of(denominators[entry[0]])).den),
172
175
  rhs.den,
173
176
  );
174
- const p = new PolynomialRef(pm);
175
- const poly = new PolynomialRef(pm);
176
- const constant = new PolynomialRef(pm);
177
- p.set(pm.mkConst(-(rhs.num * denominator) / rhs.den));
177
+ let polynomial = pm.mkConst(-(rhs.num * denominator) / rhs.den);
178
178
  for (const entry of coeffs) {
179
179
  const coeff = entry[1].mul(Rational.of(denominator, denominators[entry[0]]));
180
- poly.set(definitions.get(entry[0]));
181
- constant.set(pm.mkConst(coeff.num));
182
- poly.set(pm.mul(poly.get(), constant.get()));
183
- constant.reset();
184
- p.set(pm.add(p.get(), poly.get()));
185
- poly.reset();
180
+ polynomial = pm.add(polynomial, pm.mul(definitions[entry[0]]!, pm.mkConst(coeff.num)));
186
181
  }
187
182
  const kind = constraint.kind;
188
183
  const literal =
189
184
  kind === KIND_LE
190
- ? solver.mkIneqLiteral("GT", [p.get()], [false]) ^ 1
185
+ ? solver.mkIneqLiteral("GT", [polynomial], [false]) ^ 1
191
186
  : kind === KIND_GE
192
- ? solver.mkIneqLiteral("LT", [p.get()], [false]) ^ 1
187
+ ? solver.mkIneqLiteral("LT", [polynomial], [false]) ^ 1
193
188
  : solver.mkIneqLiteral(
194
189
  kind === KIND_LT ? "LT" : kind === KIND_GT ? "GT" : "EQ",
195
- [p.get()],
190
+ [polynomial],
196
191
  [false],
197
192
  );
198
193
  solver.mkExternalClause([literal], constraintIndex);
199
- p.reset();
200
194
  return literal;
201
195
  }
202
196
 
@@ -240,7 +234,6 @@ export class NraSolver {
240
234
  }
241
235
  this.addConstraint(constraintIndex, definitions.definitions, definitions.denominators);
242
236
  }
243
- definitions.definitions.reset();
244
237
  this.registerTranscendentals();
245
238
  const result = this.solver.check();
246
239
  if (result === L_TRUE) {
@@ -351,7 +344,6 @@ export class NraSolver {
351
344
  );
352
345
  this.literal2constraint.set(literal, constraintIndex);
353
346
  }
354
- definitions.definitions.reset();
355
347
  this.registerTranscendentals();
356
348
  }
357
349
 
@@ -364,7 +356,7 @@ export class NraSolver {
364
356
  for (let index = 0; index < polynomial.size; index++) {
365
357
  const monomial = polynomial.ms[index];
366
358
  const coeff = Rational.of(polynomial.as[index]);
367
- // Like Z3, this drops degrees: only an axiom's ¬best can be nonlinear, and ¬cell is valid alone.
359
+ // A linear cell leaves every new lemma polynomial linear, so like Z3 this ignores degrees.
368
360
  if (monomial.size === 0) bound = bound.sub(coeff);
369
361
  else if (monomial.size === 1) term.addMonomial(coeff, nl2lp[monomial.vars[0]]);
370
362
  else {
@@ -385,6 +377,8 @@ export class NraSolver {
385
377
  try {
386
378
  return this.checkAssignmentCore();
387
379
  } catch (error) {
380
+ // Deviation from Z3: a linear cell around an irrational section answers unknown, where Z3 exits.
381
+ if (error instanceof NlsatUnsupportedError) return L_UNDEF;
388
382
  if (error instanceof SmtCanceledError && this.core.nraLimit.isExhausted()) return L_UNDEF;
389
383
  throw error;
390
384
  }