tla-ts 0.0.10-dev.3a85c3a9e → 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 (84) 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 +32 -34
  10. package/dist/run.js +31 -33
  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 +4 -5
  22. package/src/cli/tlc.ts +26 -7
  23. package/src/dfid.ts +9 -2
  24. package/src/environment.ts +28 -5
  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 +2 -3
  77. package/src/runtime.ts +6 -0
  78. package/src/sany/generator-expressions.ts +6 -13
  79. package/src/search.ts +67 -29
  80. package/src/simulate.ts +3 -0
  81. package/src/standard-definitions.ts +4 -0
  82. package/src/tlc/order.ts +14 -3
  83. package/src/tlc/tlcext-operators.ts +61 -16
  84. package/src/tlc/trace-spec.ts +2 -3
@@ -1,7 +1,7 @@
1
1
  // Of Z3's nlsat parameters only seed and factor are ported, so nlsat_simplify and the bounds of
2
2
  // eliminated variables are not. The search counts its steps against the resource limit, but not the
3
3
  // memory, which Z3 also bounds by max_memory.
4
- import { SmtCanceledError, SmtInvariantError } from "../../../../errors";
4
+ import { SmtInvariantError } from "../../../../errors";
5
5
  import { L_FALSE, L_TRUE, L_UNDEF } from "../lbool";
6
6
  import { type Dependency, linearize, mkJoin, mkLeaf } from "../lp/dependency";
7
7
  import { RandomGen } from "../random-gen";
@@ -11,8 +11,14 @@ import { AlgebraicManager, Anum } from "./algebraic";
11
11
  import { AtomTable, FALSE_LITERAL, ScopedLiterals, TRUE_LITERAL } from "./atoms";
12
12
  import { Evaluator } from "./evaluator";
13
13
  import { Explain } from "./explain";
14
- import type { IntervalSet } from "./interval-set";
15
- import { NULL_VAR, type Polynomial, PolynomialCache, PolynomialManager } from "./polynomial";
14
+ import type { IntervalSet, IntervalSetManager } from "./interval-set";
15
+ import {
16
+ NULL_VAR,
17
+ type Polynomial,
18
+ PolynomialCache,
19
+ PolynomialManager,
20
+ PolynomialRef,
21
+ } from "./polynomial";
16
22
  import { NlsatTranscendentals } from "./transcendentals";
17
23
  import { type Clause, type IneqAtom, type IneqKind, isIneqAtom, type RootKind } from "./types";
18
24
 
@@ -35,30 +41,18 @@ type Trail =
35
41
  | { kind: "eq"; old: IneqAtom | null };
36
42
 
37
43
  export class NlsatSolver {
38
- readonly pm = new PolynomialManager();
39
- readonly am = new AlgebraicManager();
40
- private cache = new PolynomialCache(this.pm);
44
+ readonly pm: PolynomialManager;
45
+ readonly am: AlgebraicManager;
46
+ private cache: PolynomialCache;
41
47
  private bvalues: number[] = [];
42
48
  private levels: number[] = [];
43
49
  private justifications: (Justification | null)[] = [];
44
50
  private bwatches: Clause[][] = [];
45
51
  private isDead: boolean[] = [];
46
- readonly atoms = new AtomTable(this.pm, this.cache, {
47
- onMk: (boolVar) => {
48
- this.bvalues[boolVar] = L_UNDEF;
49
- this.levels[boolVar] = Infinity;
50
- this.justifications[boolVar] = null;
51
- this.bwatches[boolVar] = [];
52
- this.isDead[boolVar] = false;
53
- },
54
- onDelete: (boolVar) => {
55
- this.isDead[boolVar] = true;
56
- this.bvalues[boolVar] = L_UNDEF;
57
- },
58
- });
52
+ readonly atoms: AtomTable;
59
53
  private assignment: (Anum | undefined)[] = [];
60
- private evaluator = new Evaluator(this.pm, this.am, this.assignment);
61
- private ism = this.evaluator.ism;
54
+ private evaluator: Evaluator;
55
+ private ism: IntervalSetManager;
62
56
  private clauses: Clause[] = [];
63
57
  private learned: Clause[] = [];
64
58
  private nextClauseId = 0;
@@ -68,24 +62,16 @@ export class NlsatSolver {
68
62
  private var2eq: (IneqAtom | null)[] = [];
69
63
  private perm: number[] = [];
70
64
  private invPerm: number[] = [];
71
- readonly explain = new Explain(this.pm, this.am, this.cache, {
72
- atoms: this.atoms,
73
- sample: this.assignment,
74
- getValue: (literal) => this.value(literal),
75
- var2eq: this.var2eq,
76
- getNumVars: () => this.isInt.length,
77
- reorder: (permutation) => this.reorder(permutation),
78
- restoreOrder: () => this.restoreOrder(),
79
- });
80
- readonly transcendentals = new NlsatTranscendentals(this);
65
+ readonly explain: Explain;
66
+ readonly transcendentals: NlsatTranscendentals;
81
67
  private bk = 0;
82
68
  private xk = NULL_VAR;
83
69
  private scopeLevel = 0;
84
70
  private trail: Trail[] = [];
85
71
  private marks: boolean[] = [];
86
72
  private markCount = 0;
87
- private lemma = new ScopedLiterals(this.atoms);
88
- private lazyClause = new ScopedLiterals(this.atoms);
73
+ private lemma: ScopedLiterals;
74
+ private lazyClause: ScopedLiterals;
89
75
  private lemmaAssumptions: Dependency = null;
90
76
  private randomSeed = 0;
91
77
  private isReordered = false;
@@ -102,12 +88,37 @@ export class NlsatSolver {
102
88
  irrationalAssignments: 0,
103
89
  };
104
90
 
105
- steps = 0;
106
-
107
- constructor(
108
- private limit: ResourceLimit | null = null,
109
- private budget = Infinity,
110
- ) {
91
+ constructor(private limit: ResourceLimit) {
92
+ this.pm = new PolynomialManager(limit);
93
+ this.am = new AlgebraicManager(limit);
94
+ this.cache = new PolynomialCache(this.pm);
95
+ this.atoms = new AtomTable(this.pm, this.cache, {
96
+ onMk: (boolVar) => {
97
+ this.bvalues[boolVar] = L_UNDEF;
98
+ this.levels[boolVar] = Infinity;
99
+ this.justifications[boolVar] = null;
100
+ this.bwatches[boolVar] = [];
101
+ this.isDead[boolVar] = false;
102
+ },
103
+ onDelete: (boolVar) => {
104
+ this.isDead[boolVar] = true;
105
+ this.bvalues[boolVar] = L_UNDEF;
106
+ },
107
+ });
108
+ this.evaluator = new Evaluator(this.pm, this.am, this.assignment);
109
+ this.ism = this.evaluator.ism;
110
+ this.explain = new Explain(this.pm, this.am, this.cache, {
111
+ atoms: this.atoms,
112
+ sample: this.assignment,
113
+ getValue: (literal) => this.value(literal),
114
+ var2eq: this.var2eq,
115
+ getNumVars: () => this.isInt.length,
116
+ reorder: (permutation) => this.reorder(permutation),
117
+ restoreOrder: () => this.restoreOrder(),
118
+ });
119
+ this.transcendentals = new NlsatTranscendentals(this);
120
+ this.lemma = new ScopedLiterals(this.atoms);
121
+ this.lazyClause = new ScopedLiterals(this.atoms);
111
122
  this.mkClause([TRUE_LITERAL], false, null);
112
123
  }
113
124
 
@@ -117,12 +128,6 @@ export class NlsatSolver {
117
128
  this.explain.isFactoring = isFactoring;
118
129
  }
119
130
 
120
- private checkpoint() {
121
- this.steps++;
122
- if ((this.limit !== null && !this.limit.inc()) || this.steps > this.budget)
123
- throw new SmtCanceledError();
124
- }
125
-
126
131
  mkVar(isInt: boolean) {
127
132
  const variable = this.isInt.length;
128
133
  this.isInt.push(isInt);
@@ -401,7 +406,7 @@ export class NlsatSolver {
401
406
  let selectedSet: IntervalSet | null = null;
402
407
  const xkSet = this.infeasible[this.xk];
403
408
  for (const [index, literal] of clause.literals.entries()) {
404
- this.checkpoint();
409
+ this.limit.checkpoint();
405
410
  if (this.value(literal) === L_FALSE) continue;
406
411
  if (this.value(literal) === L_TRUE) return true;
407
412
  const atom = this.atoms.getAtom(literal);
@@ -489,7 +494,7 @@ export class NlsatSolver {
489
494
  } else this.newStage();
490
495
  if (this.bk === NULL_BOOL_VAR && this.xk >= this.isInt.length) return L_TRUE;
491
496
  while (true) {
492
- this.checkpoint();
497
+ this.limit.checkpoint();
493
498
  const conflict = this.processClauses(
494
499
  this.xk === NULL_VAR ? this.bwatches[this.bk] : this.watches[this.xk],
495
500
  );
@@ -509,7 +514,7 @@ export class NlsatSolver {
509
514
  private gc() {
510
515
  if (this.learned.length <= 4 * this.clauses.length) return;
511
516
  this.resetWatches();
512
- this.atoms.reinitCache();
517
+ this.atoms.reinitCache(this.clauses, this.learned);
513
518
  let kept = 0;
514
519
  for (const [index, clause] of this.learned.entries()) {
515
520
  if (index - kept < this.clauses.length && clause.literals.length > 1 && !clause.isActive)
@@ -576,12 +581,14 @@ export class NlsatSolver {
576
581
  }
577
582
  this.initSearch();
578
583
  for (const bound of bounds) {
584
+ const p = new PolynomialRef(this.pm);
579
585
  this.lemma.reset();
580
- const below = this.pm.mkLinear([1n], [bound[0]], -bound[1].num);
581
- this.lemma.push(this.mkIneqLiteral("GT", [below], [false]) ^ 1);
582
- const above = this.pm.mkLinear([1n], [bound[0]], -bound[1].num - 1n);
583
- this.lemma.push(this.mkIneqLiteral("LT", [above], [false]) ^ 1);
586
+ p.set(this.pm.mkLinear([1n], [bound[0]], -bound[1].num));
587
+ this.lemma.push(this.mkIneqLiteral("GT", [p.get()], [false]) ^ 1);
588
+ p.set(this.pm.mkLinear([1n], [bound[0]], -bound[1].num - 1n));
589
+ this.lemma.push(this.mkIneqLiteral("LT", [p.get()], [false]) ^ 1);
584
590
  this.mkClause(this.lemma.literals, true, null);
591
+ p.reset();
585
592
  }
586
593
  }
587
594
  }
@@ -661,7 +668,7 @@ export class NlsatSolver {
661
668
  private projectModel(cube: ScopedLiterals) {
662
669
  const seen = new Set<number>();
663
670
  for (const clause of this.clauses) {
664
- this.checkpoint();
671
+ this.limit.checkpoint();
665
672
  const witness = clause.literals.find((literal) => this.value(literal) === L_TRUE);
666
673
  if (witness === undefined) throw new SmtInvariantError("nlsat's model falsifies a clause");
667
674
  if (this.atoms.getAtom(witness) === null || seen.has(witness >> 1)) continue;
@@ -673,7 +680,7 @@ export class NlsatSolver {
673
680
  const projected = new ScopedLiterals(this.atoms);
674
681
  try {
675
682
  for (let variable = this.isInt.length - 1; variable >= 1 && cube.size > 0; variable--) {
676
- this.checkpoint();
683
+ this.limit.checkpoint();
677
684
  projected.reset();
678
685
  this.explain.projectVariable(variable, cube.literals, projected);
679
686
  [cube.literals, projected.literals] = [projected.literals, cube.literals];
@@ -709,14 +716,16 @@ export class NlsatSolver {
709
716
  try {
710
717
  if (limit !== null) {
711
718
  const isRational = this.am.isRational(limit);
712
- const polynomial = this.pm.fromCoeffs(this.am.getPolynomial(limit), variable);
719
+ const p = new PolynomialRef(this.pm);
720
+ p.set(this.pm.toPolynomial(this.am.getPolynomial(limit), variable));
713
721
  const upper = isRational
714
- ? this.mkIneqLiteral("LT", [polynomial], [false])
715
- : 2 * this.mkRootAtom("ROOT_LT", variable, this.am.getRootIndex(limit), polynomial);
722
+ ? this.mkIneqLiteral("LT", [p.get()], [false])
723
+ : 2 * this.mkRootAtom("ROOT_LT", variable, this.am.getRootIndex(limit), p.get());
716
724
  this.mkExternalClause([upper], tag);
725
+ p.reset();
717
726
  }
718
727
  while (true) {
719
- this.checkpoint();
728
+ this.limit.checkpoint();
720
729
  const status = this.check();
721
730
  if (status !== L_TRUE) return status;
722
731
  const cube = new ScopedLiterals(this.atoms);
@@ -799,10 +808,10 @@ export class NlsatSolver {
799
808
  }
800
809
 
801
810
  private processAntecedent(antecedent: number) {
802
- this.checkpoint();
811
+ this.limit.checkpoint();
803
812
  const boolVar = antecedent >> 1;
804
813
  if (this.getAssignedValue(antecedent) === L_UNDEF) {
805
- this.checkpoint();
814
+ this.limit.checkpoint();
806
815
  if (!this.marks[boolVar]) {
807
816
  this.marks[boolVar] = true;
808
817
  this.lemma.push(antecedent);
@@ -830,7 +839,7 @@ export class NlsatSolver {
830
839
  this.lazyClause.reset();
831
840
  this.explain.computeConflictExplanation(literals, this.lazyClause);
832
841
  for (const literal of literals) this.lazyClause.push(literal ^ 1);
833
- this.checkpoint();
842
+ this.limit.checkpoint();
834
843
  this.resolveLiterals(boolVar, this.lazyClause.literals);
835
844
  for (const clause of clauses)
836
845
  this.lemmaAssumptions = mkJoin(clause.assumptions, this.lemmaAssumptions);
@@ -871,7 +880,7 @@ export class NlsatSolver {
871
880
  let isDecision = false;
872
881
  while (true) {
873
882
  while (this.markCount > 0) {
874
- this.checkpoint();
883
+ this.limit.checkpoint();
875
884
  const entry = this.trail[top - 1];
876
885
  if (entry.kind === "assignment" && this.marks[entry.boolVar]) {
877
886
  const boolVar = entry.boolVar;
@@ -1005,7 +1014,7 @@ export class NlsatSolver {
1005
1014
  else this.learned[kept++] = clause;
1006
1015
  }
1007
1016
  this.learned.length = kept;
1008
- this.atoms.reinitCache();
1017
+ this.atoms.reinitCache(this.clauses, this.learned);
1009
1018
  this.resetWatches();
1010
1019
  const assignment: (Anum | undefined)[] = [];
1011
1020
  for (const [variable, value] of this.assignment.entries())
@@ -1018,15 +1027,9 @@ export class NlsatSolver {
1018
1027
  for (const [variable, isIntVariable] of isInt.entries())
1019
1028
  this.isInt[permutation[variable]] = isIntVariable;
1020
1029
  this.invPerm = invPerm;
1021
- const polynomials = new Set<Polynomial>();
1022
- for (const atom of this.atoms.atoms) {
1023
- if (atom === null) continue;
1024
- if (isIneqAtom(atom)) for (const polynomial of atom.polynomials) polynomials.add(polynomial);
1025
- else polynomials.add(atom.polynomial);
1026
- }
1027
- this.pm.rename(permutation, polynomials);
1030
+ this.pm.rename(permutation);
1028
1031
  this.transcendentals.rename(permutation);
1029
- this.atoms.reinitCache();
1032
+ this.atoms.reinitCache(this.clauses, this.learned);
1030
1033
  this.setAssignment(assignment);
1031
1034
  this.reattachArithClauses(this.clauses);
1032
1035
  this.reattachArithClauses(this.learned);
@@ -1,6 +1,6 @@
1
1
  import { getLcm, Rational } from "../rational";
2
2
  import { Anum } from "./algebraic";
3
- import { NULL_VAR } from "./polynomial";
3
+ import { NULL_VAR, PolynomialRef } from "./polynomial";
4
4
  import type { NlsatSolver } from "./solver";
5
5
  import {
6
6
  getAtanTaylorBracket,
@@ -49,8 +49,11 @@ export class NlsatTranscendentals {
49
49
  }
50
50
 
51
51
  private mkBoundLiteral(variable: number, kind: "LT" | "GT", bound: Rational) {
52
- const polynomial = this.solver.pm.mkLinear([bound.den], [variable], -bound.num);
53
- return this.solver.mkIneqLiteral(kind, [polynomial], [false]);
52
+ const p = new PolynomialRef(this.solver.pm);
53
+ p.set(this.solver.pm.mkLinear([bound.den], [variable], -bound.num));
54
+ const literal = this.solver.mkIneqLiteral(kind, [p.get()], [false]);
55
+ p.reset();
56
+ return literal;
54
57
  }
55
58
 
56
59
  // first * firstCoeff + second * secondCoeff kind bound.
@@ -63,12 +66,17 @@ export class NlsatTranscendentals {
63
66
  bound: Rational,
64
67
  ) {
65
68
  const den = Rational.of(getLcm(firstCoeff.den, getLcm(secondCoeff.den, bound.den)));
66
- const polynomial = this.solver.pm.mkLinear(
67
- [firstCoeff.mul(den).num, secondCoeff.mul(den).num],
68
- [first, second],
69
- bound.mul(den).neg().num,
69
+ const p = new PolynomialRef(this.solver.pm);
70
+ p.set(
71
+ this.solver.pm.mkLinear(
72
+ [firstCoeff.mul(den).num, secondCoeff.mul(den).num],
73
+ [first, second],
74
+ bound.mul(den).neg().num,
75
+ ),
70
76
  );
71
- return this.solver.mkIneqLiteral(kind, [polynomial], [false]);
77
+ const literal = this.solver.mkIneqLiteral(kind, [p.get()], [false]);
78
+ p.reset();
79
+ return literal;
72
80
  }
73
81
 
74
82
  private mkClause(literals: readonly number[]) {
@@ -160,16 +168,32 @@ export class NlsatTranscendentals {
160
168
 
161
169
  private addIdentityAxiom(kind: "sin" | "sinh" | "tanh", first: number, second: number) {
162
170
  const pm = this.solver.pm;
163
- const firstSquare = pm.mul(pm.mkVar(first), pm.mkVar(first));
164
- const secondSquare = pm.mul(pm.mkVar(second), pm.mkVar(second));
165
- const one = pm.mkConst(1n);
166
- const equation =
171
+ const v1 = new PolynomialRef(pm);
172
+ const v2 = new PolynomialRef(pm);
173
+ const v1sq = new PolynomialRef(pm);
174
+ const v2sq = new PolynomialRef(pm);
175
+ const eq = new PolynomialRef(pm);
176
+ const product = new PolynomialRef(pm);
177
+ const difference = new PolynomialRef(pm);
178
+ v1.set(pm.mkVar(first));
179
+ v2.set(pm.mkVar(second));
180
+ v1sq.set(pm.mul(v1.get(), v1.get()));
181
+ v2sq.set(pm.mul(v2.get(), v2.get()));
182
+ if (kind === "tanh") product.set(pm.mul(v1sq.get(), v2sq.get()));
183
+ difference.set(
167
184
  kind === "sin"
168
- ? pm.sub(pm.add(firstSquare, secondSquare), one)
169
- : kind === "sinh"
170
- ? pm.sub(pm.sub(firstSquare, secondSquare), one)
171
- : pm.sub(pm.sub(firstSquare, pm.mul(firstSquare, secondSquare)), one);
172
- this.mkClause([this.solver.mkIneqLiteral("EQ", [equation], [false])]);
185
+ ? pm.add(v1sq.get(), v2sq.get())
186
+ : pm.sub(v1sq.get(), kind === "sinh" ? v2sq.get() : product.get()),
187
+ );
188
+ eq.set(pm.sub(difference.get(), pm.one));
189
+ difference.reset();
190
+ product.reset();
191
+ this.mkClause([this.solver.mkIneqLiteral("EQ", [eq.get()], [false])]);
192
+ eq.reset();
193
+ v2sq.reset();
194
+ v1sq.reset();
195
+ v2.reset();
196
+ v1.reset();
173
197
  }
174
198
 
175
199
  // Exact Taylor brackets: exp, log and atan are increasing where their series apply, and the
@@ -221,7 +221,7 @@ const henselLiftOnce = (
221
221
  r: bigint,
222
222
  polynomials: { u: Upolynomial; a: Upolynomial; v: Upolynomial; b: Upolynomial; c: Upolynomial },
223
223
  ) => {
224
- const zr = new UpolynomialManager(r);
224
+ const zr = new UpolynomialManager(z.limit, r);
225
225
  const f = zr.set(z.divConst(z.sub(polynomials.c, z.mul(polynomials.a, polynomials.b)), b));
226
226
  const division = zr.divRem(zr.mul(polynomials.v, f), polynomials.a);
227
227
  const t = zr.add(zr.mul(polynomials.u, f), zr.mul(polynomials.b, division.quotient));
@@ -239,13 +239,14 @@ const henselLiftQuadratic = (
239
239
  b: Upolynomial,
240
240
  e: number,
241
241
  ) => {
242
- const zp = new UpolynomialManager(zpe.zp);
242
+ const zp = new UpolynomialManager(z.limit, zpe.zp);
243
243
  const ext = zp.extGcd(a, b);
244
244
  let u = ext.u;
245
245
  let v = ext.v;
246
246
  let currentA = [...a];
247
247
  let currentB = [...b];
248
248
  for (let k = 1; k < e; k *= 2) {
249
+ z.limit.checkpoint();
249
250
  const modulus = zpe.zp as bigint;
250
251
  const lifted = henselLiftOnce(z, modulus, modulus, {
251
252
  u,
@@ -451,12 +452,13 @@ export const factorSquareFree = (
451
452
  fPp = fPp.map((coeff) => -coeff);
452
453
  if (k % 2 === 1) fs.constant = -fs.constant;
453
454
  }
454
- const zp = new UpolynomialManager(2n);
455
+ const zp = new UpolynomialManager(z.limit, 2n);
455
456
  let zpFs = mkFactors();
456
457
  let zpFsPrime = 2n;
457
458
  let degreeSet = [true];
458
459
  const getNextPrime = mkPrimeIterator();
459
460
  for (let trials = 0; trials <= params.primeTrials;) {
461
+ z.limit.checkpoint();
460
462
  const nextPrime = getNextPrime();
461
463
  if (nextPrime > params.maxPrime) {
462
464
  pushFactor(z, fs, fPp, k);
@@ -488,7 +490,7 @@ export const factorSquareFree = (
488
490
  }
489
491
  }
490
492
  zp.setZp(zpFsPrime);
491
- const zpe = new UpolynomialManager(zpFsPrime);
493
+ const zpe = new UpolynomialManager(z.limit, zpFsPrime);
492
494
  const zpeFs = henselLiftFactors(z, fPp, zp, zpFs, getMignotteBound(fPp, zpFsPrime), zpe);
493
495
  let leading = zpe.normalizeNumeral(fPp[fPp.length - 1]);
494
496
  fPp = z.mulConst(fPp, leading);
@@ -497,6 +499,7 @@ export const factorSquareFree = (
497
499
  let result = true;
498
500
  let isRemoving = false;
499
501
  for (let counter = 1; iterator.next(isRemoving); counter++) {
502
+ z.limit.checkpoint();
500
503
  if (counter > params.maxSearchSize) {
501
504
  result = false;
502
505
  break;
@@ -584,6 +587,7 @@ export const factorUpolynomial = (
584
587
  let isComplete = true;
585
588
  let product = z.exactDiv(primitive, square) as bigint[];
586
589
  for (let multiplicity = 1; product.length > 1; multiplicity++) {
590
+ z.limit.checkpoint();
587
591
  const next = z.gcd(product, square);
588
592
  const factor = z.exactDiv(product, next) as bigint[];
589
593
  if (factor.length > 1) {
@@ -1,10 +1,11 @@
1
- import { SmtInvariantError } from "../../../../errors";
1
+ import { SmtCanceledError, SmtInvariantError } from "../../../../errors";
2
2
  import { getGcd, Rational } from "../rational";
3
+ import type { ResourceLimit } from "../rlimit";
3
4
  import { getLog2, Mpbq, refineLower, refineUpper } from "./mpbq";
4
5
 
5
6
  export type Upolynomial = readonly bigint[];
6
7
 
7
- const BIG_PRIMES = [
8
+ export const BIG_PRIMES = [
8
9
  39103, 39107, 39113, 39119, 39133, 39139, 39157, 39161, 39163, 39181, 39191, 39199, 39209, 39217,
9
10
  39227, 39229, 39233, 39239, 39241, 39251, 39293, 39301, 39313, 39317, 39323, 39341, 39343, 39359,
10
11
  39367, 39371, 39373, 39383, 39397, 39409, 39419, 39439, 39443, 39451, 39461, 39499, 39503, 39509,
@@ -31,7 +32,7 @@ export const floorDiv = (dividend: bigint, divisor: bigint) => {
31
32
  return dividend % divisor !== 0n && dividend < 0n !== divisor < 0n ? quotient - 1n : quotient;
32
33
  };
33
34
 
34
- const getExtendedGcd = (first: bigint, second: bigint) => {
35
+ export const getExtendedGcd = (first: bigint, second: bigint) => {
35
36
  let [oldR, r] = [first, second];
36
37
  let [oldS, s] = [1n, 0n];
37
38
  let [oldT, t] = [0n, 1n];
@@ -73,7 +74,10 @@ interface DrsFrame {
73
74
  export class UpolynomialManager {
74
75
  private isPrimeModulus = true;
75
76
 
76
- constructor(private modulus: bigint | null = null) {}
77
+ constructor(
78
+ readonly limit: ResourceLimit,
79
+ private modulus: bigint | null = null,
80
+ ) {}
77
81
 
78
82
  get zp() {
79
83
  return this.modulus;
@@ -170,12 +174,15 @@ export class UpolynomialManager {
170
174
  mul(first: Upolynomial, second: Upolynomial) {
171
175
  if (first.length === 0 || second.length === 0) return [];
172
176
  const result = Array.from({ length: first.length + second.length - 1 }, () => 0n);
173
- for (let index = 0; index < first.length; index++) {
174
- if (first[index] === 0n) continue;
175
- for (let other = 0; other < second.length; other++)
176
- if (second[other] !== 0n)
177
+ const outer = first.length < second.length ? second : first;
178
+ const inner = first.length < second.length ? first : second;
179
+ for (let index = 0; index < outer.length; index++) {
180
+ this.limit.checkpoint();
181
+ if (outer[index] === 0n) continue;
182
+ for (let other = 0; other < inner.length; other++)
183
+ if (inner[other] !== 0n)
177
184
  result[index + other] = this.normalizeNumeral(
178
- result[index + other] + first[index] * second[other],
185
+ result[index + other] + outer[index] * inner[other],
179
186
  );
180
187
  }
181
188
  return trim(result);
@@ -231,8 +238,10 @@ export class UpolynomialManager {
231
238
  const quotient = Array.from({ length: quotientSize }, () => 0n);
232
239
  const leading = divisor[divisor.length - 1];
233
240
  let power = 0;
234
- while (remainder.length >= divisor.length) {
241
+ while (true) {
242
+ this.limit.checkpoint();
235
243
  const size = remainder.length;
244
+ if (size < divisor.length) return { quotient: trim(quotient), remainder, power };
236
245
  const shift = size - divisor.length;
237
246
  if (isField) {
238
247
  const ratio = this.divNumeral(remainder[size - 1], leading);
@@ -257,7 +266,6 @@ export class UpolynomialManager {
257
266
  remainder.length = size - 1;
258
267
  trim(remainder);
259
268
  }
260
- return { quotient: trim(quotient), remainder, power };
261
269
  }
262
270
 
263
271
  div(dividend: Upolynomial, divisor: Upolynomial) {
@@ -271,8 +279,9 @@ export class UpolynomialManager {
271
279
  const isField = this.isField;
272
280
  const leading = divisor[divisor.length - 1];
273
281
  let power = 0;
274
- while (remainder.length >= divisor.length) {
282
+ while (this.limit.inc()) {
275
283
  const size = remainder.length;
284
+ if (size < divisor.length) break;
276
285
  const shift = size - divisor.length;
277
286
  if (isField) {
278
287
  const ratio = this.divNumeral(remainder[size - 1], leading);
@@ -357,13 +366,16 @@ export class UpolynomialManager {
357
366
  if (second.length === 0) return this.flipSignIfLmNeg(this.set(first));
358
367
  let a = this.set(first);
359
368
  let b = this.set(second);
360
- while (b.length > 0) {
369
+ while (this.limit.inc()) {
370
+ if (b.length === 0) {
371
+ const normalized = this.normalize(a);
372
+ return this.isField ? this.mkMonic(normalized) : this.flipSignIfLmNeg(normalized);
373
+ }
361
374
  const remainder = this.normalize(this.rem(a, b));
362
375
  a = b;
363
376
  b = remainder;
364
377
  }
365
- const normalized = this.normalize(a);
366
- return this.isField ? this.mkMonic(normalized) : this.flipSignIfLmNeg(normalized);
378
+ throw new SmtCanceledError();
367
379
  }
368
380
 
369
381
  private combineImages(image: Upolynomial, prime: bigint, combined: Upolynomial, bound: bigint) {
@@ -497,6 +509,7 @@ export class UpolynomialManager {
497
509
  let previous = 0;
498
510
  let changes = 0;
499
511
  for (let index = 0; index < work.length; index++) {
512
+ this.limit.checkpoint();
500
513
  let position = 1;
501
514
  for (; position < work.length - index; position++) work[position] += work[position - 1];
502
515
  const current = getSign(work[position - 1]);
@@ -513,9 +526,11 @@ export class UpolynomialManager {
513
526
  translate(coeffs: Upolynomial) {
514
527
  const result = [...coeffs];
515
528
  const degree = result.length - 1;
516
- for (let step = 1; step <= degree; step++)
529
+ for (let step = 1; step <= degree; step++) {
530
+ this.limit.checkpoint();
517
531
  for (let index = degree - step; index <= degree - 1; index++)
518
532
  result[index] += result[index + 1];
533
+ }
519
534
  return result;
520
535
  }
521
536
 
@@ -524,6 +539,7 @@ export class UpolynomialManager {
524
539
  const result = this.composeAnPxDivA(coeffs, value.den);
525
540
  const degree = result.length - 1;
526
541
  for (let step = 1; step <= degree; step++) {
542
+ this.limit.checkpoint();
527
543
  result[degree - step] += value.num * result[degree - step + 1];
528
544
  for (let index = degree - step + 1; index <= degree - 1; index++)
529
545
  result[index] = result[index] * value.den + value.num * result[index + 1];
@@ -696,6 +712,7 @@ export class UpolynomialManager {
696
712
  this.pushChildFrames(this.div(coeffs, twoXMinusOne), frames);
697
713
  } else this.pushChildFrames(coeffs, frames);
698
714
  while (frames.length > 0) {
715
+ this.limit.checkpoint();
699
716
  const frame = frames[frames.length - 1];
700
717
  if (!frame.isFirst) {
701
718
  frames.pop();
@@ -758,11 +775,12 @@ export class UpolynomialManager {
758
775
  }
759
776
 
760
777
  private sturmSeqCore(sequence: bigint[][]) {
761
- while (true) {
778
+ while (this.limit.inc()) {
762
779
  const remainder = this.srem(sequence[sequence.length - 2], sequence[sequence.length - 1]);
763
780
  if (remainder.length === 0) return sequence;
764
781
  sequence.push(this.normalize(remainder));
765
782
  }
783
+ return sequence;
766
784
  }
767
785
 
768
786
  sturmSeq(coeffs: Upolynomial) {
@@ -774,6 +792,7 @@ export class UpolynomialManager {
774
792
  }
775
793
 
776
794
  isolating2refinable(coeffs: Upolynomial, lower: Mpbq, upper: Mpbq): Refinement {
795
+ this.limit.checkpoint();
777
796
  const signLower = this.evalSignAtBq(coeffs, lower);
778
797
  const signUpper = this.evalSignAtBq(coeffs, upper);
779
798
  if (signLower !== 0 && signUpper !== 0) return { isInterval: true, lower, upper };
@@ -782,6 +801,7 @@ export class UpolynomialManager {
782
801
  let b = upper;
783
802
  let newA = a.add(b).div2();
784
803
  while (true) {
804
+ this.limit.checkpoint();
785
805
  const signNewA = this.evalSignAtBq(coeffs, newA);
786
806
  if (signNewA !== signUpper) return { isInterval: signNewA !== 0, lower: newA, upper: b };
787
807
  b = newA;
@@ -793,6 +813,7 @@ export class UpolynomialManager {
793
813
  let b = upper;
794
814
  let newB = a.add(b).div2();
795
815
  while (true) {
816
+ this.limit.checkpoint();
796
817
  const signNewB = this.evalSignAtBq(coeffs, newB);
797
818
  if (signNewB !== signLower)
798
819
  return signNewB === 0
@@ -812,6 +833,7 @@ export class UpolynomialManager {
812
833
  let newA1 = a1.add(b1).div2();
813
834
  let newB2 = a2.add(b2).div2();
814
835
  while (true) {
836
+ this.limit.checkpoint();
815
837
  const signNewA1 = this.evalSignAtBq(coeffs, newA1);
816
838
  if (signNewA1 === 0) return { isInterval: false, lower: newA1, upper };
817
839
  if (signNewA1 === -signB1) return { isInterval: true, lower: newA1, upper: b1 };
@@ -838,6 +860,7 @@ export class UpolynomialManager {
838
860
  const signLower = this.evalSignAtBq(coeffs, lower);
839
861
  let current: Refinement = { isInterval: true, lower, upper };
840
862
  while (true) {
863
+ this.limit.checkpoint();
841
864
  if (current.upper.sub(current.lower).isLessThanOneDiv2k(precision)) return current;
842
865
  current = this.refineCore(coeffs, signLower, current.lower, current.upper);
843
866
  if (!current.isInterval) return current;