tla-ts 0.0.4-dev.a0009b7 → 0.0.4-dev.b254db0

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 (313) hide show
  1. package/README.md +33 -143
  2. package/dist/deciders.cjs +366 -0
  3. package/dist/deciders.js +366 -0
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +37 -1
  6. package/dist/index.d.ts +37 -1
  7. package/dist/index.js +1 -1
  8. package/dist/parallel-worker.cjs +1 -4
  9. package/dist/parallel-worker.js +1 -4
  10. package/dist/prove-worker.cjs +9 -0
  11. package/dist/prove-worker.d.cts +9 -0
  12. package/dist/prove-worker.d.ts +9 -0
  13. package/dist/prove-worker.js +9 -0
  14. package/dist/prove.cjs +996 -0
  15. package/dist/prove.js +996 -0
  16. package/dist/run-cli.cjs +37 -47
  17. package/dist/run-cli.js +37 -47
  18. package/dist/tlapm.cjs +10 -0
  19. package/dist/tlapm.js +10 -0
  20. package/dist/transfer.cjs +38 -44
  21. package/dist/transfer.js +38 -44
  22. package/dist/worker.cjs +21 -0
  23. package/dist/worker.js +21 -0
  24. package/package.json +21 -12
  25. package/src/check.ts +1 -0
  26. package/src/codegen.ts +26 -7
  27. package/src/errors.ts +86 -0
  28. package/src/evaluate.ts +63 -20
  29. package/src/index.ts +10 -0
  30. package/src/{obligation-action.ts → obligations/action.ts} +265 -55
  31. package/src/{obligation-anon.ts → obligations/anon.ts} +47 -37
  32. package/src/obligations/ctx.ts +64 -0
  33. package/src/{obligation-deref.ts → obligations/deref.ts} +38 -61
  34. package/src/{obligation-expression.ts → obligations/expression.ts} +78 -45
  35. package/src/{obligation-format.ts → obligations/format.ts} +7 -8
  36. package/src/{obligation-generate.ts → obligations/generate.ts} +147 -74
  37. package/src/{obligations.ts → obligations/index.ts} +29 -13
  38. package/src/{obligation-levels.ts → obligations/levels.ts} +14 -5
  39. package/src/{obligation-module.ts → obligations/module.ts} +56 -33
  40. package/src/obligations/pickle.ts +98 -0
  41. package/src/obligations/pragma.ts +189 -0
  42. package/src/{obligation-printer.ts → obligations/printer.ts} +22 -80
  43. package/src/{obligation-proof.ts → obligations/proof.ts} +7 -1
  44. package/src/{obligation-source.ts → obligations/source.ts} +159 -97
  45. package/src/obligations/temporal.ts +319 -0
  46. package/src/{obligation-tuples.ts → obligations/tuples.ts} +1 -1
  47. package/src/ocaml-marshal.ts +166 -0
  48. package/src/parallel-worker.ts +2 -18
  49. package/src/parallel.ts +10 -43
  50. package/src/prove/decide-pool.ts +100 -0
  51. package/src/prove/deciders.ts +33 -0
  52. package/src/prove/isabelle/LICENSE +36 -0
  53. package/src/prove/isabelle/LICENSE-tlapm +26 -0
  54. package/src/prove/isabelle/decide.ts +47 -0
  55. package/src/prove/isabelle/encode.ts +503 -0
  56. package/src/prove/isabelle/isar/inner-syntax.ts +579 -0
  57. package/src/prove/isabelle/isar/method.ts +367 -0
  58. package/src/prove/isabelle/isar/proof.ts +28 -0
  59. package/src/prove/isabelle/isar/statement.ts +57 -0
  60. package/src/prove/isabelle/isar/theory.ts +192 -0
  61. package/src/prove/isabelle/isar/type-check.ts +278 -0
  62. package/src/prove/isabelle/provers/blast/blast.ts +258 -0
  63. package/src/prove/isabelle/provers/blast/branch.ts +322 -0
  64. package/src/prove/isabelle/provers/blast/prove.ts +347 -0
  65. package/src/prove/isabelle/provers/blast/rules.ts +330 -0
  66. package/src/prove/isabelle/provers/blast/state.ts +180 -0
  67. package/src/prove/isabelle/provers/blast/term.ts +547 -0
  68. package/src/prove/isabelle/provers/clasimp.ts +150 -0
  69. package/src/prove/isabelle/provers/classical.ts +389 -0
  70. package/src/prove/isabelle/provers/hypsubst.ts +441 -0
  71. package/src/prove/isabelle/pure/bicompose.ts +174 -0
  72. package/src/prove/isabelle/pure/consts.ts +32 -0
  73. package/src/prove/isabelle/pure/conv.ts +79 -0
  74. package/src/prove/isabelle/pure/drule.ts +264 -0
  75. package/src/prove/isabelle/pure/envir.ts +363 -0
  76. package/src/prove/isabelle/pure/goal.ts +53 -0
  77. package/src/prove/isabelle/pure/interrupt.ts +16 -0
  78. package/src/prove/isabelle/pure/list.ts +12 -0
  79. package/src/prove/isabelle/pure/logic.ts +402 -0
  80. package/src/prove/isabelle/pure/name.ts +110 -0
  81. package/src/prove/isabelle/pure/net.ts +262 -0
  82. package/src/prove/isabelle/pure/object-logic.ts +41 -0
  83. package/src/prove/isabelle/pure/pattern.ts +650 -0
  84. package/src/prove/isabelle/pure/raw-simplifier.ts +353 -0
  85. package/src/prove/isabelle/pure/rewriter.ts +602 -0
  86. package/src/prove/isabelle/pure/search.ts +68 -0
  87. package/src/prove/isabelle/pure/seq.ts +156 -0
  88. package/src/prove/isabelle/pure/simplifier.ts +112 -0
  89. package/src/prove/isabelle/pure/table.ts +208 -0
  90. package/src/prove/isabelle/pure/tactic.ts +276 -0
  91. package/src/prove/isabelle/pure/tactical.ts +347 -0
  92. package/src/prove/isabelle/pure/term-ord.ts +159 -0
  93. package/src/prove/isabelle/pure/term.ts +808 -0
  94. package/src/prove/isabelle/pure/thm.ts +806 -0
  95. package/src/prove/isabelle/pure/type.ts +133 -0
  96. package/src/prove/isabelle/pure/unify.ts +753 -0
  97. package/src/prove/isabelle/pure/variable.ts +25 -0
  98. package/src/prove/isabelle/tla/classical-setup.ts +101 -0
  99. package/src/prove/isabelle/tla/context.ts +74 -0
  100. package/src/prove/isabelle/tla/logic-data.ts +5 -0
  101. package/src/prove/isabelle/tla/logic.ts +215 -0
  102. package/src/prove/isabelle/tla/simpdata.ts +241 -0
  103. package/src/prove/isabelle/tools/intuitionistic.ts +132 -0
  104. package/src/prove/ocaml.ts +22 -0
  105. package/src/prove/prep/action.ts +521 -0
  106. package/src/prove/prep/defs.ts +33 -0
  107. package/src/prove/prep/elab.ts +156 -0
  108. package/src/prove/prep/enabled-axioms.ts +189 -0
  109. package/src/prove/prep/enabled.ts +43 -0
  110. package/src/prove/prep/fingerprint.ts +388 -0
  111. package/src/prove/prep/flatten.ts +70 -0
  112. package/src/prove/prep/level-comparison.ts +557 -0
  113. package/src/prove/prep/md5.ts +67 -0
  114. package/src/prove/prep/pragma.ts +221 -0
  115. package/src/prove/prep/trivial.ts +22 -0
  116. package/src/prove/prove-worker.ts +25 -0
  117. package/src/prove/prove.ts +505 -0
  118. package/src/prove/ptl/decide.ts +68 -0
  119. package/src/prove/ptl/encode.ts +256 -0
  120. package/src/prove/ptl/sat.ts +151 -0
  121. package/src/prove/ptl/tableau.ts +276 -0
  122. package/src/prove/smt/axiomatize.ts +69 -0
  123. package/src/prove/smt/axioms.ts +222 -0
  124. package/src/prove/smt/context.ts +108 -0
  125. package/src/prove/smt/data.ts +636 -0
  126. package/src/prove/smt/encode.ts +166 -0
  127. package/src/prove/smt/extensionality.ts +65 -0
  128. package/src/prove/smt/flatten.ts +112 -0
  129. package/src/prove/smt/format.ts +154 -0
  130. package/src/prove/smt/instances.ts +182 -0
  131. package/src/prove/smt/rewrite.ts +773 -0
  132. package/src/prove/smt/simplify.ts +57 -0
  133. package/src/prove/smt/solver/act-cache.ts +265 -0
  134. package/src/prove/smt/solver/arith-rewriter.ts +962 -0
  135. package/src/prove/smt/solver/array.ts +22 -0
  136. package/src/prove/smt/solver/asserted-formulas.ts +469 -0
  137. package/src/prove/smt/solver/ast-lt.ts +157 -0
  138. package/src/prove/smt/solver/ast-util.ts +147 -0
  139. package/src/prove/smt/solver/ast.ts +1315 -0
  140. package/src/prove/smt/solver/auf-solver.ts +572 -0
  141. package/src/prove/smt/solver/case-split-queue.ts +178 -0
  142. package/src/prove/smt/solver/cg-table.ts +175 -0
  143. package/src/prove/smt/solver/clause.ts +276 -0
  144. package/src/prove/smt/solver/conflict-resolution.ts +343 -0
  145. package/src/prove/smt/solver/context.ts +1292 -0
  146. package/src/prove/smt/solver/ctx-simplify.ts +334 -0
  147. package/src/prove/smt/solver/der.ts +224 -0
  148. package/src/prove/smt/solver/dyn-ack.ts +141 -0
  149. package/src/prove/smt/solver/e-matching.ts +659 -0
  150. package/src/prove/smt/solver/enode.ts +259 -0
  151. package/src/prove/smt/solver/expr-ref.ts +21 -0
  152. package/src/prove/smt/solver/expr-safe-replace.ts +172 -0
  153. package/src/prove/smt/solver/fingerprints.ts +92 -0
  154. package/src/prove/smt/solver/flat-literals.ts +51 -0
  155. package/src/prove/smt/solver/goal.ts +138 -0
  156. package/src/prove/smt/solver/hash.ts +173 -0
  157. package/src/prove/smt/solver/hashtable.ts +166 -0
  158. package/src/prove/smt/solver/internalizer.ts +1381 -0
  159. package/src/prove/smt/solver/lbool.ts +5 -0
  160. package/src/prove/smt/solver/literal.ts +24 -0
  161. package/src/prove/smt/solver/lp/bound-propagator.ts +391 -0
  162. package/src/prove/smt/solver/lp/constraints.ts +78 -0
  163. package/src/prove/smt/solver/lp/containers.ts +296 -0
  164. package/src/prove/smt/solver/lp/core-solver.ts +504 -0
  165. package/src/prove/smt/solver/lp/cross-nested.ts +224 -0
  166. package/src/prove/smt/solver/lp/dep-intervals.ts +656 -0
  167. package/src/prove/smt/solver/lp/dependency.ts +51 -0
  168. package/src/prove/smt/solver/lp/dioph-eq.ts +1181 -0
  169. package/src/prove/smt/solver/lp/explanation.ts +46 -0
  170. package/src/prove/smt/solver/lp/gomory.ts +255 -0
  171. package/src/prove/smt/solver/lp/hnf-cutter.ts +125 -0
  172. package/src/prove/smt/solver/lp/hnf.ts +281 -0
  173. package/src/prove/smt/solver/lp/int-cube.ts +295 -0
  174. package/src/prove/smt/solver/lp/int-solver.ts +514 -0
  175. package/src/prove/smt/solver/lp/lar-solver.ts +816 -0
  176. package/src/prove/smt/solver/lp/lar-term.ts +62 -0
  177. package/src/prove/smt/solver/lp/lp-settings.ts +91 -0
  178. package/src/prove/smt/solver/lp/maximize-term.ts +347 -0
  179. package/src/prove/smt/solver/lp/nex-creator.ts +381 -0
  180. package/src/prove/smt/solver/lp/nex.ts +120 -0
  181. package/src/prove/smt/solver/lp/nla-backoff.ts +43 -0
  182. package/src/prove/smt/solver/lp/nla-basics.ts +398 -0
  183. package/src/prove/smt/solver/lp/nla-core.ts +968 -0
  184. package/src/prove/smt/solver/lp/nla-divisions.ts +332 -0
  185. package/src/prove/smt/solver/lp/nla-emonics.ts +373 -0
  186. package/src/prove/smt/solver/lp/nla-factorization.ts +30 -0
  187. package/src/prove/smt/solver/lp/nla-grobner.ts +904 -0
  188. package/src/prove/smt/solver/lp/nla-horner.ts +126 -0
  189. package/src/prove/smt/solver/lp/nla-intervals.ts +265 -0
  190. package/src/prove/smt/solver/lp/nla-monomial-bounds.ts +715 -0
  191. package/src/prove/smt/solver/lp/nla-monotone.ts +59 -0
  192. package/src/prove/smt/solver/lp/nla-order.ts +307 -0
  193. package/src/prove/smt/solver/lp/nla-patcher.ts +112 -0
  194. package/src/prove/smt/solver/lp/nla-tangents.ts +251 -0
  195. package/src/prove/smt/solver/lp/nla-throttle.ts +85 -0
  196. package/src/prove/smt/solver/lp/nla-types.ts +124 -0
  197. package/src/prove/smt/solver/lp/nla-var-eqs.ts +141 -0
  198. package/src/prove/smt/solver/lp/nra-solver.ts +440 -0
  199. package/src/prove/smt/solver/lp/numeric-pair.ts +65 -0
  200. package/src/prove/smt/solver/lp/pdd-interval.ts +31 -0
  201. package/src/prove/smt/solver/lp/pdd-simplifier.ts +184 -0
  202. package/src/prove/smt/solver/lp/pdd-solver.ts +408 -0
  203. package/src/prove/smt/solver/lp/pdd.ts +971 -0
  204. package/src/prove/smt/solver/lp/random-updater.ts +138 -0
  205. package/src/prove/smt/solver/lp/static-matrix.ts +246 -0
  206. package/src/prove/smt/solver/lp/union-find.ts +96 -0
  207. package/src/prove/smt/solver/mam-compiler.ts +847 -0
  208. package/src/prove/smt/solver/mam-interpreter.ts +400 -0
  209. package/src/prove/smt/solver/maximize-ac-sharing.ts +118 -0
  210. package/src/prove/smt/solver/model-checker.ts +347 -0
  211. package/src/prove/smt/solver/model-evaluator.ts +262 -0
  212. package/src/prove/smt/solver/model-finder.ts +155 -0
  213. package/src/prove/smt/solver/model-generator.ts +97 -0
  214. package/src/prove/smt/solver/model-macro-solver.ts +728 -0
  215. package/src/prove/smt/solver/nlsat/algebraic.ts +1161 -0
  216. package/src/prove/smt/solver/nlsat/atoms.ts +238 -0
  217. package/src/prove/smt/solver/nlsat/evaluator.ts +303 -0
  218. package/src/prove/smt/solver/nlsat/explain.ts +607 -0
  219. package/src/prove/smt/solver/nlsat/interval-set.ts +351 -0
  220. package/src/prove/smt/solver/nlsat/levelwise.ts +740 -0
  221. package/src/prove/smt/solver/nlsat/mpbq.ts +161 -0
  222. package/src/prove/smt/solver/nlsat/polynomial.ts +1722 -0
  223. package/src/prove/smt/solver/nlsat/solver.ts +911 -0
  224. package/src/prove/smt/solver/nlsat/types.ts +43 -0
  225. package/src/prove/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
  226. package/src/prove/smt/solver/nlsat/upolynomial.ts +996 -0
  227. package/src/prove/smt/solver/nnf.ts +469 -0
  228. package/src/prove/smt/solver/pattern-inference.ts +351 -0
  229. package/src/prove/smt/solver/poly-rewriter.ts +477 -0
  230. package/src/prove/smt/solver/propagate-values.ts +83 -0
  231. package/src/prove/smt/solver/proto-model.ts +551 -0
  232. package/src/prove/smt/solver/pull-quant.ts +139 -0
  233. package/src/prove/smt/solver/qe-lite-der.ts +526 -0
  234. package/src/prove/smt/solver/qe-lite-fm.ts +576 -0
  235. package/src/prove/smt/solver/qe-lite.ts +381 -0
  236. package/src/prove/smt/solver/qi-queue.ts +362 -0
  237. package/src/prove/smt/solver/quant-tactics.ts +117 -0
  238. package/src/prove/smt/solver/quantifier-analyzer.ts +562 -0
  239. package/src/prove/smt/solver/quantifier-manager.ts +186 -0
  240. package/src/prove/smt/solver/random-gen.ts +10 -0
  241. package/src/prove/smt/solver/rational.ts +165 -0
  242. package/src/prove/smt/solver/ref-vector.ts +62 -0
  243. package/src/prove/smt/solver/relevancy.ts +247 -0
  244. package/src/prove/smt/solver/rewriter-tpl.ts +1159 -0
  245. package/src/prove/smt/solver/rewriter.ts +843 -0
  246. package/src/prove/smt/solver/rlimit.ts +79 -0
  247. package/src/prove/smt/solver/smtlib-parser.ts +526 -0
  248. package/src/prove/smt/solver/solver.ts +71 -0
  249. package/src/prove/smt/solver/sort.ts +35 -0
  250. package/src/prove/smt/solver/sticky-generations.ts +69 -0
  251. package/src/prove/smt/solver/theory-lra-axioms.ts +340 -0
  252. package/src/prove/smt/solver/theory-lra-bounds.ts +226 -0
  253. package/src/prove/smt/solver/theory-lra-final-check.ts +252 -0
  254. package/src/prove/smt/solver/theory-lra-internalize.ts +476 -0
  255. package/src/prove/smt/solver/theory-lra-model.ts +243 -0
  256. package/src/prove/smt/solver/theory-lra-propagate.ts +405 -0
  257. package/src/prove/smt/solver/theory-lra-state.ts +43 -0
  258. package/src/prove/smt/solver/theory-lra.ts +56 -0
  259. package/src/prove/smt/solver/var-subst.ts +258 -0
  260. package/src/prove/smt/solver/z3-license.txt +10 -0
  261. package/src/prove/smt/table.ts +170 -0
  262. package/src/prove/tlapm/tlapm.ts +96 -0
  263. package/src/prove/tlaps-module.ts +510 -0
  264. package/src/prove/verdict.ts +18 -0
  265. package/src/prove/zenon/LICENSE +32 -0
  266. package/src/prove/zenon/check/check.ts +105 -0
  267. package/src/prove/zenon/check/definitions.ts +144 -0
  268. package/src/prove/zenon/check/equality.ts +182 -0
  269. package/src/prove/zenon/check/functions.ts +62 -0
  270. package/src/prove/zenon/check/keys.ts +125 -0
  271. package/src/prove/zenon/check/propositional.ts +104 -0
  272. package/src/prove/zenon/check/records.ts +188 -0
  273. package/src/prove/zenon/check/relations.ts +121 -0
  274. package/src/prove/zenon/check/rewrites.ts +224 -0
  275. package/src/prove/zenon/check/sets.ts +302 -0
  276. package/src/prove/zenon/check/terms.ts +127 -0
  277. package/src/prove/zenon/check/tla.ts +126 -0
  278. package/src/prove/zenon/decide.ts +113 -0
  279. package/src/prove/zenon/encode.ts +437 -0
  280. package/src/prove/zenon/eqrel.ts +300 -0
  281. package/src/prove/zenon/equality-rules.ts +435 -0
  282. package/src/prove/zenon/expr.ts +551 -0
  283. package/src/prove/zenon/index-table.ts +350 -0
  284. package/src/prove/zenon/isar-case.ts +513 -0
  285. package/src/prove/zenon/isar.ts +1249 -0
  286. package/src/prove/zenon/limits.ts +15 -0
  287. package/src/prove/zenon/llproof.ts +173 -0
  288. package/src/prove/zenon/mltoll.ts +1566 -0
  289. package/src/prove/zenon/node.ts +297 -0
  290. package/src/prove/zenon/ocaml.ts +169 -0
  291. package/src/prove/zenon/parse.ts +247 -0
  292. package/src/prove/zenon/phrase.ts +191 -0
  293. package/src/prove/zenon/proof.ts +198 -0
  294. package/src/prove/zenon/propositional-rules.ts +387 -0
  295. package/src/prove/zenon/rules.ts +94 -0
  296. package/src/prove/zenon/search.ts +288 -0
  297. package/src/prove/zenon/tla/functions.ts +151 -0
  298. package/src/prove/zenon/tla/llproof.ts +480 -0
  299. package/src/prove/zenon/tla/records.ts +225 -0
  300. package/src/prove/zenon/tla/rewrites.ts +447 -0
  301. package/src/prove/zenon/tla/sets.ts +304 -0
  302. package/src/prove/zenon/tla/terms.ts +181 -0
  303. package/src/prove/zenon/tla/tla.ts +506 -0
  304. package/src/run-cli.ts +334 -75
  305. package/src/standard-modules.ts +5 -2
  306. package/src/successors.ts +24 -5
  307. package/src/tlapm-cache.ts +236 -0
  308. package/src/tlc-random.ts +55 -32
  309. package/src/tlc-version.ts +8 -7
  310. package/src/values.ts +97 -47
  311. package/src/worker.ts +65 -0
  312. package/dist/parallel.cjs +0 -12
  313. package/dist/parallel.js +0 -12
@@ -0,0 +1,1292 @@
1
+ // Ported from Z3 (src/smt/smt_context.cpp), MIT license, see z3-license.txt. The SMT kernel's
2
+ // search: a CDCL loop over Boolean variables combined with an E-graph for congruence closure,
3
+ // relevancy propagation, the arithmetic theory and quantifier instantiation, with Z3's default
4
+ // parameters for UFNIA.
5
+ import type { AssertedFormulas } from "./asserted-formulas";
6
+ import { AstManager, FAMILY_ARITH, KIND_APP, KIND_QUANTIFIER, KIND_VAR } from "./ast";
7
+ import { getCongruenceJustification, isCongruent } from "./cg-table";
8
+ import { BJustification, type Clause, CLS_LEARNED, CLS_TH_LEMMA, HEADER_SIZE } from "./clause";
9
+ import { ConflictResolution } from "./conflict-resolution";
10
+ import { DynAckManager } from "./dyn-ack";
11
+ import {
12
+ Enode,
13
+ EqConflictJustification,
14
+ EqJustification,
15
+ EqPropagationJustification,
16
+ EqRootPropagationJustification,
17
+ MpIffJustification,
18
+ } from "./enode";
19
+ import {
20
+ BV_ENODE,
21
+ BV_EQ,
22
+ BV_PHASE,
23
+ BV_PHASE_AVAILABLE,
24
+ BV_QUANTIFIER,
25
+ BV_THEORY,
26
+ BV_TRUE_FIRST,
27
+ Internalizer,
28
+ UNDO_ADD_EQ,
29
+ UNDO_TH_VAR,
30
+ } from "./internalizer";
31
+ import { L_FALSE, L_TRUE, L_UNDEF } from "./lbool";
32
+ import { BoolVar, getVar, isNegated, Literal, mkLiteral, negate } from "./literal";
33
+ import { mkModel } from "./model-generator";
34
+ import type { GeneratedModel } from "./model-generator";
35
+ import type { ProtoModel } from "./proto-model";
36
+ import { DefaultQuantifierManager } from "./quantifier-manager";
37
+ import { TheoryLra } from "./theory-lra";
38
+ import { shrink } from "./array";
39
+
40
+ export const FC_DONE = 0;
41
+ export const FC_CONTINUE = 1;
42
+ export const FC_GIVEUP = 2;
43
+
44
+ const ACTIVITY_LIMIT = 1e100;
45
+ const INV_ACTIVITY_LIMIT = 1e-100;
46
+ const INV_DECAY = 1.052;
47
+ const RESTART_INITIAL = 100;
48
+ const RESTART_FACTOR = 1.1;
49
+ // phase_caching_on and phase_caching_off: the numbers of conflicts while phase caching is on and off.
50
+ const PHASE_CACHING_ON = 400;
51
+ const PHASE_CACHING_OFF = 100;
52
+ const AGILITY_FACTOR = 0.9999;
53
+ // The LGC_FIXED lemma garbage collection: lemma_gc_initial conflicts between collections, and the
54
+ // activity and unassigned literal thresholds of del_inactive_lemmas2.
55
+ const LEMMA_GC_INITIAL = 5000;
56
+ // theory_lra::num_final_check_levels: level 2 also runs nlsat's full check.
57
+ const NUM_ARITH_FINAL_CHECK_LEVELS = 2;
58
+ const NEW_OLD_RATIO = 16;
59
+ const OLD_CLAUSE_ACTIVITY = 500;
60
+ const NEW_CLAUSE_RELEVANCY = 45;
61
+ const OLD_CLAUSE_RELEVANCY = 6;
62
+ const RESTART_AGILITY_THRESHOLD = 0.18;
63
+
64
+ export const CHECK_MODEL_SAT = 0;
65
+ export const CHECK_MODEL_UNKNOWN = 1;
66
+ export const CHECK_MODEL_RESTART = 2;
67
+
68
+ // The reasons a search may stop without an answer that MBQI cares about.
69
+ const FAILURE_OK = 0;
70
+ const FAILURE_THEORY = 1;
71
+ const FAILURE_QUANTIFIERS = 2;
72
+ const FAILURE_CANCELED = 3;
73
+
74
+ type SearchFailure =
75
+ | typeof FAILURE_OK
76
+ | typeof FAILURE_THEORY
77
+ | typeof FAILURE_QUANTIFIERS
78
+ | typeof FAILURE_CANCELED;
79
+
80
+ // smt_params: the kernel runs model-based quantifier instantiation with relevancy level 2 and the
81
+ // CS_ACTIVITY_DELAY_NEW case split queue. The model checker's auxiliary context runs neither, as Z3
82
+ // sets its relevancy level to 0 and its case split strategy to CS_ACTIVITY.
83
+ // m_model_generator's model, m_proto_model and m_model. The model generator holds the last model it
84
+ // made. From mk_proto_model to reset_model the context holds that model too as its proto model, and
85
+ // get_model adds the model the proto model makes.
86
+ type ModelState =
87
+ | { kind: "none" }
88
+ | { kind: "generated"; generated: GeneratedModel }
89
+ | { kind: "proto"; generated: GeneratedModel }
90
+ | { kind: "final"; generated: GeneratedModel; model: ProtoModel };
91
+
92
+ interface SmtParams {
93
+ isMbqi: boolean;
94
+ relevancyLevel: 0 | 2;
95
+ }
96
+
97
+ export class Context extends Internalizer {
98
+ // setup_context makes the theories after the first formulas are asserted, so the field starts
99
+ // unassigned.
100
+ theory!: TheoryLra;
101
+ readonly qmanager: DefaultQuantifierManager;
102
+ readonly conflictResolution: ConflictResolution = new ConflictResolution(this);
103
+ private readonly dynAck = new DynAckManager(this);
104
+ private qhead = 0;
105
+ private scopeAssignedLimits: number[] = [];
106
+ private scopeAuxClauseLimits: number[] = [];
107
+ private scopeTrailLimits: number[] = [];
108
+ // The number of lemmas when each user scope began.
109
+ private baseScopeLemmaLimits: number[] = [];
110
+ private atomPropagationQueue: Literal[] = [];
111
+ private thEqQueue: number[] = [];
112
+ private thDiseqQueue: number[] = [];
113
+ private bvarInc = 1;
114
+ private agility = 0;
115
+ private numConflicts = 0;
116
+ private numConflictsSinceRestart = 0;
117
+ private numConflictsSinceLemmaGc = 0;
118
+ private restartThreshold = RESTART_INITIAL;
119
+ private restartOuterThreshold = RESTART_INITIAL;
120
+ private isPhaseCacheOn = true;
121
+ private phaseCounter = 0;
122
+ private finalCheckIndex = 0;
123
+ private r1ParentGenerations = new Map<Enode, number>();
124
+ private erasedGenerations = new Map<Enode, number>();
125
+ private lastSearchFailure: SearchFailure = FAILURE_OK;
126
+ private modelState: ModelState = { kind: "none" };
127
+
128
+ constructor(manager: AstManager, params: SmtParams) {
129
+ super(manager, params.relevancyLevel > 0, params.relevancyLevel > 0);
130
+ this.qmanager = new DefaultQuantifierManager(this, params.isMbqi);
131
+ }
132
+
133
+ // register_plugin initializes each theory it registers.
134
+ setupContext() {
135
+ this.theory = new TheoryLra(this);
136
+ this.theory.init();
137
+ }
138
+
139
+ // The search stops with unknown when the manager's resource limit cancels it.
140
+ checkCanceled() {
141
+ return !this.manager.limit.inc();
142
+ }
143
+
144
+ // Assignment and propagation.
145
+
146
+ assign(literal: Literal, justification: BJustification, isDecision = false) {
147
+ const value = this.assignment[literal];
148
+ if (value === L_FALSE) this.setConflict(justification, negate(literal));
149
+ else if (value === L_UNDEF) this.assignCore(literal, justification, isDecision);
150
+ }
151
+
152
+ private assignCore(literal: Literal, justification: BJustification, isDecision: boolean) {
153
+ const boolVar = getVar(literal);
154
+ this.assignedLiterals.push(literal);
155
+ this.assignment[literal] = L_TRUE;
156
+ this.assignment[negate(literal)] = L_FALSE;
157
+ this.justifications[boolVar] = justification;
158
+ this.setLevel(boolVar, this.scopeLevel);
159
+ const isNegative = isNegated(literal);
160
+ if (this.hasBoolVarFlag(boolVar, BV_PHASE_AVAILABLE)) {
161
+ this.agility *= AGILITY_FACTOR;
162
+ if (!isDecision && this.hasBoolVarFlag(boolVar, BV_PHASE) === isNegative)
163
+ this.agility += 1 - AGILITY_FACTOR;
164
+ }
165
+ this.setBoolVarFlag(boolVar, BV_PHASE_AVAILABLE, true);
166
+ this.setBoolVarFlag(boolVar, BV_PHASE, !isNegative);
167
+ if (this.isAtom(boolVar) && this.isRelevant(this.boolVar2Expr[boolVar]))
168
+ this.atomPropagationQueue.push(literal);
169
+ }
170
+
171
+ private bcp() {
172
+ while (this.qhead < this.assignedLiterals.length) {
173
+ if (this.checkCanceled()) return true;
174
+ const literal = this.assignedLiterals[this.qhead];
175
+ this.qhead++;
176
+ const notLiteral = negate(literal);
177
+ const binary = this.binaryWatches[literal];
178
+ if (binary !== undefined && binary.length > 0) {
179
+ const justification = this.getBinJustification(literal);
180
+ for (let index = binary.length - 1; index >= 0; index--) {
181
+ const other = binary[index];
182
+ const value = this.assignment[other];
183
+ if (value === L_FALSE) {
184
+ this.setConflict(justification, negate(other));
185
+ return false;
186
+ }
187
+ if (value === L_UNDEF) this.assignCore(other, justification, false);
188
+ }
189
+ }
190
+ const list = this.watches[literal];
191
+ if (list === undefined) continue;
192
+ // Nothing below makes a clause, so the clause data stays in place.
193
+ const data = this.clauses.data;
194
+ let kept = 0;
195
+ let index = 0;
196
+ for (; index < list.length; index++) {
197
+ const clause: Clause = list[index];
198
+ const first = clause + HEADER_SIZE;
199
+ if (data[first] === notLiteral) {
200
+ data[first] = data[first + 1];
201
+ data[first + 1] = notLiteral;
202
+ }
203
+ const firstLiteral = data[first] as Literal;
204
+ const firstValue = this.assignment[firstLiteral];
205
+ if (firstValue === L_TRUE) {
206
+ list[kept++] = clause;
207
+ continue;
208
+ }
209
+ let isFound = false;
210
+ const end = first + data[clause];
211
+ for (let position = first + 2; position < end; position++) {
212
+ const candidate = data[position] as Literal;
213
+ if (this.assignment[candidate] !== L_FALSE) {
214
+ this.addWatch(negate(candidate), clause);
215
+ data[first + 1] = candidate;
216
+ data[position] = notLiteral;
217
+ isFound = true;
218
+ break;
219
+ }
220
+ }
221
+ if (isFound) continue;
222
+ list[kept++] = clause;
223
+ if (firstValue === L_FALSE) {
224
+ for (index++; index < list.length; index++) list[kept++] = list[index];
225
+ shrink(list, kept);
226
+ this.setConflict(clause, null);
227
+ return false;
228
+ }
229
+ this.assignCore(firstLiteral, clause, false);
230
+ }
231
+ shrink(list, kept);
232
+ }
233
+ return true;
234
+ }
235
+
236
+ private propagateEqs() {
237
+ for (let index = 0; index < this.eqPropagationQueue.length && !this.checkCanceled(); index++) {
238
+ const entry = this.eqPropagationQueue[index];
239
+ this.addEq(entry.lhs, entry.rhs, entry.justification);
240
+ if (this.isInconsistent()) {
241
+ this.eqPropagationQueue = [];
242
+ return false;
243
+ }
244
+ }
245
+ this.eqPropagationQueue = [];
246
+ return true;
247
+ }
248
+
249
+ private propagateAtoms() {
250
+ const queue = this.atomPropagationQueue;
251
+ for (let index = 0; index < queue.length && !this.checkCanceled(); index++) {
252
+ const literal = queue[index];
253
+ const boolVar = getVar(literal);
254
+ const value = this.assignment[mkLiteral(boolVar)];
255
+ if (this.hasBoolVarFlag(boolVar, BV_ENODE)) this.propagateBoolVarEnode(boolVar);
256
+ if (this.isInconsistent()) return false;
257
+ if (this.hasBoolVarFlag(boolVar, BV_EQ)) {
258
+ const eq = this.boolVar2Expr[boolVar];
259
+ if (!this.manager.isBool(this.manager.getArg(eq, 0))) {
260
+ const lhs = this.exprToEnode[this.manager.getArg(eq, 0)];
261
+ const rhs = this.exprToEnode[this.manager.getArg(eq, 1)];
262
+ if (lhs !== undefined && rhs !== undefined) {
263
+ if (value === L_TRUE) this.addEq(lhs, rhs, { kind: "equation", literal: literal });
264
+ else if (!this.addDiseq(lhs, rhs) && !this.isInconsistent())
265
+ this.setConflict(new EqPropagationJustification(lhs, rhs), mkLiteral(boolVar, true));
266
+ }
267
+ }
268
+ } else if (this.hasBoolVarFlag(boolVar, BV_THEORY))
269
+ this.theory.assignEh(boolVar, value === L_TRUE);
270
+ else if (this.hasBoolVarFlag(boolVar, BV_QUANTIFIER) && value === L_TRUE)
271
+ this.qmanager.assignEh(this.boolVar2Expr[boolVar]);
272
+ if (this.isInconsistent()) return false;
273
+ }
274
+ this.atomPropagationQueue = [];
275
+ return true;
276
+ }
277
+
278
+ protected propagateBoolVarEnode(boolVar: BoolVar) {
279
+ const value = this.assignment[mkLiteral(boolVar)];
280
+ let node = this.exprToEnode[this.boolVar2Expr[boolVar]];
281
+ if (node === undefined) return;
282
+ const isNegative = value === L_FALSE;
283
+ if (node.isMergeTf)
284
+ this.addEq(node, isNegative ? this.falseEnode : this.trueEnode, {
285
+ kind: "equation",
286
+ literal: mkLiteral(boolVar, isNegative),
287
+ });
288
+ const root = node.root;
289
+ if (root === this.trueEnode || root === this.falseEnode) return;
290
+ if (
291
+ node === root ||
292
+ !this.isRelevant(root.owner) ||
293
+ this.assignment[mkLiteral(this.getBoolVar(root.owner))] !== value
294
+ ) {
295
+ const first = node;
296
+ node = node.next;
297
+ while (node !== first) {
298
+ const otherVar = this.getBoolVar(node.owner);
299
+ if (this.assignment[mkLiteral(otherVar)] !== value)
300
+ this.assign(mkLiteral(otherVar, isNegative), this.mkMpIff(first, node));
301
+ node = node.next;
302
+ }
303
+ }
304
+ }
305
+
306
+ private mkMpIff(first: Enode, second: Enode) {
307
+ return new MpIffJustification(first, second, (node) => this.enodeToLiteral(node));
308
+ }
309
+
310
+ relevantEh(expr: number) {
311
+ const boolVar = this.findBoolVar(expr);
312
+ if (boolVar !== undefined && this.isAtom(boolVar)) {
313
+ const value = this.assignment[mkLiteral(boolVar)];
314
+ if (value !== L_UNDEF) this.atomPropagationQueue.push(mkLiteral(boolVar, value === L_FALSE));
315
+ }
316
+ if (this.manager.kinds[expr] !== KIND_APP) return;
317
+ const node = this.exprToEnode[expr];
318
+ if (node !== undefined) this.qmanager.relevantEh(node);
319
+ if (this.manager.getFamily(expr) === FAMILY_ARITH || (node !== undefined && node.thVar >= 0))
320
+ this.theory.relevantEh(expr);
321
+ }
322
+
323
+ private propagateRelevancy(qhead: number) {
324
+ for (let index = qhead; index < this.assignedLiterals.length; index++) {
325
+ const literal = this.assignedLiterals[index];
326
+ this.relevancy.assignEh(this.boolVar2Expr[getVar(literal)], !isNegated(literal));
327
+ }
328
+ this.relevancy.propagate();
329
+ }
330
+
331
+ private propagateThEqs() {
332
+ const queue = this.thEqQueue;
333
+ for (let index = 0; index < queue.length && !this.isInconsistent(); index += 2)
334
+ this.theory.newEqEh(queue[index], queue[index + 1]);
335
+ this.thEqQueue = [];
336
+ }
337
+
338
+ private propagateThDiseqs() {
339
+ const queue = this.thDiseqQueue;
340
+ for (let index = 0; index < queue.length && !this.isInconsistent(); index += 2)
341
+ this.theory.newDiseqEh(queue[index], queue[index + 1]);
342
+ this.thDiseqQueue = [];
343
+ }
344
+
345
+ canPropagate() {
346
+ return (
347
+ this.qhead !== this.assignedLiterals.length ||
348
+ this.relevancy.canPropagate() ||
349
+ this.atomPropagationQueue.length > 0 ||
350
+ this.qmanager.canPropagate() ||
351
+ this.theory.canPropagate() ||
352
+ this.eqPropagationQueue.length > 0 ||
353
+ this.thEqQueue.length > 0 ||
354
+ this.thDiseqQueue.length > 0
355
+ );
356
+ }
357
+
358
+ propagate() {
359
+ for (;;) {
360
+ if (this.isInconsistent()) return false;
361
+ const qhead = this.qhead;
362
+ if (!this.bcp()) return false;
363
+ this.propagateRelevancy(qhead);
364
+ if (this.isInconsistent()) return false;
365
+ if (!this.propagateAtoms()) return false;
366
+ if (!this.propagateEqs()) return false;
367
+ this.propagateThEqs();
368
+ this.propagateThDiseqs();
369
+ if (this.isInconsistent()) return false;
370
+ this.theory.propagate();
371
+ if (this.isInconsistent()) return false;
372
+ if (!this.checkCanceled()) this.qmanager.propagate();
373
+ if (this.isInconsistent()) return false;
374
+ if (this.isResourceLimitExceeded()) {
375
+ this.qhead = qhead;
376
+ return true;
377
+ }
378
+ if (!this.canPropagate()) return true;
379
+ }
380
+ }
381
+
382
+ // Equalities.
383
+
384
+ private getClosestVar(start: Enode) {
385
+ for (let node: Enode | null = start; node !== null; node = node.transTarget)
386
+ if (node.thVar >= 0) return node.thVar;
387
+ return -1;
388
+ }
389
+
390
+ private invertTrans(start: Enode) {
391
+ let current = start.transTarget;
392
+ let justification = start.transJustification;
393
+ let previous = start;
394
+ start.transTarget = null;
395
+ while (current !== null) {
396
+ const next = current.transTarget;
397
+ const nextJustification = current.transJustification;
398
+ current.transTarget = previous;
399
+ current.transJustification = justification;
400
+ previous = current;
401
+ current = next;
402
+ justification = nextJustification;
403
+ }
404
+ }
405
+
406
+ private pushNewThEq(first: number, second: number) {
407
+ this.thEqQueue.push(first, second);
408
+ }
409
+
410
+ private pushNewThDiseq(first: number, second: number) {
411
+ const theory = this.theory;
412
+ if (theory.getEnode(first).isInterpreted && theory.getEnode(second).isInterpreted) return;
413
+ this.thDiseqQueue.push(first, second);
414
+ }
415
+
416
+ private pushNewThDiseqs(root: Enode, thVar: number) {
417
+ for (const parent of root.parents) {
418
+ if (!parent.isEq) continue;
419
+ if (this.assignment[mkLiteral(this.getBoolVar(parent.owner))] !== L_FALSE) continue;
420
+ let lhs = parent.getArg(0);
421
+ let rhs = parent.getArg(1);
422
+ if (rhs.root === root.root) {
423
+ const saved = lhs;
424
+ lhs = rhs;
425
+ rhs = saved;
426
+ }
427
+ const rhsVar = this.getClosestVar(rhs);
428
+ const lhsVar = this.getClosestVar(lhs);
429
+ const finalVar = lhsVar >= 0 ? lhsVar : thVar;
430
+ if (rhsVar >= 0 && finalVar !== rhsVar) this.pushNewThDiseq(finalVar, rhsVar);
431
+ }
432
+ }
433
+
434
+ private mergeTheoryVars(n2: Enode, n1: Enode, justification: EqJustification) {
435
+ const r2 = n2.root;
436
+ const r1 = n1.root;
437
+ if (r1.thVar < 0 && r2.thVar < 0) return;
438
+ const isFromTheory = justification.kind === "justification";
439
+ const v2 = this.getClosestVar(n2);
440
+ const v1 = this.getClosestVar(n1);
441
+ if (v2 >= 0 && v1 >= 0) {
442
+ if (!isFromTheory) this.pushNewThEq(v2, v1);
443
+ } else if (v1 < 0 && v2 >= 0) this.pushNewThDiseqs(r1, v2);
444
+ else if (v1 >= 0 && v2 < 0) {
445
+ r2.thVar = v1;
446
+ this.pushNewThDiseqs(r2, v1);
447
+ }
448
+ }
449
+
450
+ attachThVar(node: Enode, thVar: number) {
451
+ const oldVar = node.thVar;
452
+ // A variable that an equality moved to the node's list, which the node's own replaces.
453
+ if (oldVar >= 0) {
454
+ node.thVar = thVar;
455
+ this.pushTrail(() => {
456
+ node.thVar = oldVar;
457
+ });
458
+ this.pushNewThEq(thVar, oldVar);
459
+ return;
460
+ }
461
+ const root = node.root;
462
+ const rootVar = root.thVar;
463
+ node.thVar = thVar;
464
+ this.trailInts.push(thVar);
465
+ this.trailNodes.push(node);
466
+ this.trailKinds.push(UNDO_TH_VAR);
467
+ if (rootVar < 0) {
468
+ if (root !== node) root.thVar = thVar;
469
+ this.pushNewThDiseqs(root, thVar);
470
+ } else if (root !== node) this.pushNewThEq(rootVar, thVar);
471
+ }
472
+
473
+ addEq(inputN1: Enode, inputN2: Enode, justification: EqJustification) {
474
+ let n1 = inputN1;
475
+ let n2 = inputN2;
476
+ let r1 = n1.root;
477
+ let r2 = n2.root;
478
+ if (r1 === r2) return;
479
+ if (r1.isInterpreted && r2.isInterpreted) {
480
+ this.setConflict(new EqConflictJustification(n1, n2, justification), null);
481
+ return;
482
+ }
483
+ if ((r1.classSize > r2.classSize && !r2.isInterpreted) || r1.isInterpreted) {
484
+ const savedNode = n1;
485
+ n1 = n2;
486
+ n2 = savedNode;
487
+ const savedRoot = r1;
488
+ r1 = r2;
489
+ r2 = savedRoot;
490
+ }
491
+ if (this.isRelevant(r1.owner)) this.markAsRelevant(r2.owner);
492
+ else if (this.isRelevant(r2.owner)) this.markAsRelevant(r1.owner);
493
+ const r2NumParents = r2.parents.length;
494
+ this.qmanager.addEqEh(r1, r2);
495
+ this.mergeTheoryVars(n2, n1, justification);
496
+ this.invertTrans(n1);
497
+ n1.transTarget = n2;
498
+ n1.transJustification = justification;
499
+ this.removeParentsFromCgTable(r1);
500
+ let current = r1;
501
+ do {
502
+ current.root = r2;
503
+ current = current.next;
504
+ } while (current !== r1);
505
+ this.reinsertParentsIntoCgTable(r1, r2, n1, n2, justification);
506
+ this.trailInts.push(r2NumParents);
507
+ this.trailNodes.push(r1, n1);
508
+ this.trailKinds.push(UNDO_ADD_EQ);
509
+ if (n2.isBool) this.propagateBoolEnodeAssignment(r1, r2, n1, n2);
510
+ const savedNext = r1.next;
511
+ r1.next = r2.next;
512
+ r2.next = savedNext;
513
+ r2.classSize += r1.classSize;
514
+ }
515
+
516
+ private removeParentsFromCgTable(r1: Enode) {
517
+ for (const parent of r1.parents) {
518
+ if (parent.isMarked || !parent.isCgr() || parent.isTrueEq()) continue;
519
+ parent.isMarked = true;
520
+ if (parent.isCgcEnabled) {
521
+ if (!parent.isEq) this.r1ParentGenerations.set(parent, parent.generation);
522
+ this.cgTable.erase(parent);
523
+ }
524
+ }
525
+ }
526
+
527
+ // n1 = n2, justified by the justification, implied r1 = r2. A congruence that makes a false
528
+ // equality true is the root of a conflict, which dynamic Ackermannization counts.
529
+ private reinsertParentsIntoCgTable(
530
+ r1: Enode,
531
+ r2: Enode,
532
+ n1: Enode,
533
+ n2: Enode,
534
+ justification: EqJustification,
535
+ ) {
536
+ const r1Parents = r1.parents;
537
+ for (let index = 0; index < r1Parents.length; index++) {
538
+ const parent = r1Parents[index];
539
+ if (!parent.isMarked) continue;
540
+ parent.isMarked = false;
541
+ if (parent.isEq) {
542
+ const lhs = parent.getArg(0);
543
+ const rhs = parent.getArg(1);
544
+ if (lhs.root === rhs.root) {
545
+ const literal = mkLiteral(this.getBoolVar(parent.owner));
546
+ const value = this.assignment[literal];
547
+ if (value !== L_TRUE) {
548
+ if (value === L_FALSE && justification.kind === "congruence")
549
+ this.dynAck.cgConflictEh(n1.owner, n2.owner);
550
+ this.assign(literal, new EqPropagationJustification(lhs, rhs));
551
+ }
552
+ continue;
553
+ }
554
+ }
555
+ if (!parent.isCgcEnabled) {
556
+ r2.addParent(parent);
557
+ continue;
558
+ }
559
+ const parentGeneration = parent.isEq
560
+ ? 0
561
+ : (this.r1ParentGenerations.get(parent) ?? this.getGeneration(parent));
562
+ const congruent = this.cgTable.insert(parent);
563
+ if (congruent === parent) {
564
+ parent.generation = parentGeneration;
565
+ r2.addParent(parent);
566
+ continue;
567
+ }
568
+ parent.cg = congruent;
569
+ this.mergeCgcGenerations(parent, parentGeneration, congruent);
570
+ if (congruent.root !== parent.root)
571
+ this.pushEq(parent, congruent, getCongruenceJustification(parent, congruent));
572
+ }
573
+ this.r1ParentGenerations.clear();
574
+ }
575
+
576
+ protected override undoTrailEntry(kind: number) {
577
+ switch (kind) {
578
+ case UNDO_ADD_EQ: {
579
+ const n1 = this.popTrailNode();
580
+ this.undoAddEq(this.popTrailNode(), n1, this.popTrailInt());
581
+ return;
582
+ }
583
+ case UNDO_TH_VAR: {
584
+ const node = this.popTrailNode();
585
+ const thVar = this.popTrailInt();
586
+ node.thVar = -1;
587
+ if (node.root !== node && node.root.thVar === thVar) node.root.thVar = -1;
588
+ return;
589
+ }
590
+ default:
591
+ super.undoTrailEntry(kind);
592
+ }
593
+ }
594
+
595
+ private undoAddEq(r1: Enode, n1: Enode, r2NumParents: number) {
596
+ const r2 = r1.root;
597
+ r2.classSize -= r1.classSize;
598
+ const savedNext = r1.next;
599
+ r1.next = r2.next;
600
+ r2.next = savedNext;
601
+ const erasedGenerations = this.erasedGenerations;
602
+ erasedGenerations.clear();
603
+ for (let index = r2NumParents; index < r2.parents.length; index++) {
604
+ const parent = r2.parents[index];
605
+ if (parent.isCgcEnabled) {
606
+ if (!parent.isEq) erasedGenerations.set(parent, parent.generation);
607
+ this.cgTable.erase(parent);
608
+ }
609
+ }
610
+ let current = r1;
611
+ do {
612
+ current.root = r1;
613
+ current = current.next;
614
+ } while (current !== r1);
615
+ shrink(r2.parents, r2NumParents);
616
+ for (const parent of r1.parents) {
617
+ if (!parent.isCgcEnabled) continue;
618
+ const cg = parent.cg;
619
+ if (parent.isTrueEq() || (parent !== cg && isCongruent(parent, cg))) continue;
620
+ let generation = 100000;
621
+ if (parent.isEq) generation = 0;
622
+ else if (parent === cg) {
623
+ const cached = erasedGenerations.get(parent);
624
+ if (cached === undefined) continue;
625
+ generation = cached;
626
+ }
627
+ const congruent = this.cgTable.insert(parent);
628
+ parent.cg = congruent;
629
+ if (congruent === parent) parent.generation = generation;
630
+ }
631
+ if (r2.thVar >= 0 && this.theory.getEnode(r2.thVar).root !== r2) r2.thVar = -1;
632
+ n1.transTarget = null;
633
+ this.invertTrans(r1);
634
+ }
635
+
636
+ private propagateBoolEnodeAssignment(r1: Enode, r2: Enode, n1: Enode, n2: Enode) {
637
+ if (r2 === this.falseEnode || r2 === this.trueEnode) {
638
+ const isFalse = r2 === this.falseEnode;
639
+ let current = r1;
640
+ do {
641
+ const literal = mkLiteral(this.getBoolVar(current.owner), isFalse);
642
+ if (this.assignment[literal] !== L_TRUE)
643
+ this.assign(literal, new EqRootPropagationJustification(current));
644
+ current = current.next;
645
+ } while (current !== r1);
646
+ return;
647
+ }
648
+ const first = this.assignment[mkLiteral(this.getBoolVar(n1.owner))];
649
+ const second = this.assignment[mkLiteral(this.getBoolVar(n2.owner))];
650
+ if (first === second) return;
651
+ if (second === L_UNDEF) this.propagateBoolEnodeAssignmentCore(n1, n2);
652
+ else this.propagateBoolEnodeAssignmentCore(n2, n1);
653
+ }
654
+
655
+ private propagateBoolEnodeAssignmentCore(source: Enode, start: Enode) {
656
+ const value = this.assignment[mkLiteral(this.getBoolVar(source.owner))];
657
+ let target = start;
658
+ do {
659
+ const boolVar = this.getBoolVar(target.owner);
660
+ const targetValue = this.assignment[mkLiteral(boolVar)];
661
+ if (targetValue !== value) {
662
+ if (targetValue !== L_UNDEF && isCongruent(source, target) && source.numArgs > 0)
663
+ this.dynAck.cgConflictEh(source.owner, target.owner);
664
+ this.assign(mkLiteral(boolVar, value === L_FALSE), this.mkMpIff(source, target));
665
+ }
666
+ target = target.next;
667
+ } while (target !== start);
668
+ }
669
+
670
+ private addDiseq(n1: Enode, n2: Enode) {
671
+ const r1 = n1.root;
672
+ const r2 = n2.root;
673
+ if (r1 === r2) return r1.thVar >= 0;
674
+ const v1 = this.getClosestVar(n1);
675
+ const v2 = this.getClosestVar(n2);
676
+ if (v1 >= 0 && v2 >= 0) this.pushNewThDiseq(v1, v2);
677
+ return true;
678
+ }
679
+
680
+ // Theory combination: assume that two shared terms are equal.
681
+ assumeEq(lhs: Enode, rhs: Enode) {
682
+ if (lhs.root === rhs.root) return false;
683
+ const eq = this.mkEqAtom(lhs.owner, rhs.owner);
684
+ let isNew = false;
685
+ if (!this.isBInternalized(eq)) {
686
+ this.internalizeFormulaCore(eq, true);
687
+ const boolVar = this.getBoolVar(eq);
688
+ this.setBoolVarFlag(boolVar, BV_EQ, true);
689
+ this.setTrueFirstFlag(boolVar);
690
+ this.theory.internalizeEqEh(eq);
691
+ isNew = true;
692
+ }
693
+ const boolVar = this.getBoolVar(eq);
694
+ if (!this.hasBoolVarFlag(boolVar, BV_TRUE_FIRST)) {
695
+ this.setTrueFirstFlag(boolVar);
696
+ isNew = true;
697
+ }
698
+ if (this.assignment[mkLiteral(boolVar)] === L_UNDEF) isNew = true;
699
+ if (!this.isRelevant(eq)) {
700
+ this.markAsRelevant(eq);
701
+ isNew = true;
702
+ }
703
+ return isNew;
704
+ }
705
+
706
+ // Search.
707
+
708
+ incBvarActivity(boolVar: BoolVar) {
709
+ this.activity[boolVar] += this.bvarInc;
710
+ if (this.activity[boolVar] > ACTIVITY_LIMIT) {
711
+ for (let index = 0; index < this.numBoolVars; index++)
712
+ this.activity[index] *= INV_ACTIVITY_LIMIT;
713
+ this.bvarInc *= INV_ACTIVITY_LIMIT;
714
+ }
715
+ this.caseSplitQueue.activityIncreased(boolVar);
716
+ }
717
+
718
+ private pushScope() {
719
+ this.scopeLevel++;
720
+ this.scopeAssignedLimits.push(this.assignedLiterals.length);
721
+ this.scopeAuxClauseLimits.push(this.auxClauses.length);
722
+ this.scopeTrailLimits.push(this.trailKinds.length);
723
+ this.relevancy.push();
724
+ this.qmanager.push();
725
+ this.theory.pushScopeEh();
726
+ }
727
+
728
+ private cacheGenerationOfExpr(expr: number, newLevel: number, visited: Set<number>) {
729
+ const manager = this.manager;
730
+ const todo = [expr];
731
+ while (todo.length > 0) {
732
+ const current = todo.pop() ?? -1;
733
+ if (visited.has(current)) continue;
734
+ visited.add(current);
735
+ if (manager.kinds[current] === KIND_APP) {
736
+ const node = this.exprToEnode[current];
737
+ if (node !== undefined) {
738
+ if (node.iscopeLevel <= newLevel) continue;
739
+ this.cachedGeneration.set(current, this.getGeneration(node));
740
+ }
741
+ for (let index = 0; index < manager.getNumArgs(current); index++) {
742
+ const arg = manager.getArg(current, index);
743
+ if (manager.kinds[arg] !== KIND_VAR) todo.push(arg);
744
+ }
745
+ } else if (manager.kinds[current] === KIND_QUANTIFIER && this.isBInternalized(current)) {
746
+ this.cachedGeneration.set(current, this.qmanager.getGeneration(current));
747
+ todo.push(manager.getQuantifier(current).body);
748
+ }
749
+ }
750
+ }
751
+
752
+ private cacheGenerationOfLiterals(literals: Literal[], newLevel: number, visited: Set<number>) {
753
+ for (const literal of literals) {
754
+ const boolVar = getVar(literal);
755
+ if (this.getInternLevel(boolVar) > newLevel)
756
+ this.cacheGenerationOfExpr(this.boolVar2Expr[boolVar], newLevel, visited);
757
+ }
758
+ }
759
+
760
+ private cacheGeneration(newLevel: number, visited: Set<number>) {
761
+ const limit = Math.min(this.scopeLevel, this.clausesToReinit.length - 1);
762
+ for (let level = newLevel; level <= limit; level++)
763
+ for (const clause of this.clausesToReinit[level])
764
+ this.cacheGenerationOfLiterals(this.clauses.getLiterals(clause), newLevel, visited);
765
+ }
766
+
767
+ private unassignVars(oldLimit: number) {
768
+ for (let index = this.assignedLiterals.length - 1; index >= oldLimit; index--) {
769
+ const literal = this.assignedLiterals[index];
770
+ this.assignment[literal] = L_UNDEF;
771
+ this.assignment[negate(literal)] = L_UNDEF;
772
+ this.justifications[getVar(literal)] = null;
773
+ this.caseSplitQueue.unassignVar(getVar(literal));
774
+ }
775
+ shrink(this.assignedLiterals, oldLimit);
776
+ this.qhead = oldLimit;
777
+ }
778
+
779
+ private popScopeCore(numScopes: number, visited: Set<number>) {
780
+ const newLevel = this.scopeLevel - numScopes;
781
+ this.cacheGeneration(newLevel, visited);
782
+ this.qmanager.pop(numScopes);
783
+ if (newLevel < this.baseLevel) {
784
+ this.deleteClauses(this.lemmas, this.baseScopeLemmaLimits[newLevel]);
785
+ shrink(this.baseScopeLemmaLimits, newLevel);
786
+ }
787
+ this.conflict = null;
788
+ this.deleteClauses(this.auxClauses, this.scopeAuxClauseLimits[newLevel]);
789
+ this.relevancy.pop(numScopes);
790
+ this.unassignVars(this.scopeAssignedLimits[newLevel]);
791
+ this.popTrail(this.scopeTrailLimits[newLevel]);
792
+ this.theory.popScopeEh(numScopes);
793
+ this.eqPropagationQueue = [];
794
+ this.thEqQueue = [];
795
+ this.thDiseqQueue = [];
796
+ this.atomPropagationQueue = [];
797
+ shrink(this.scopeAssignedLimits, newLevel);
798
+ shrink(this.scopeAuxClauseLimits, newLevel);
799
+ shrink(this.scopeTrailLimits, newLevel);
800
+ this.scopeLevel = newLevel;
801
+ if (newLevel < this.baseLevel) {
802
+ this.baseLevel = newLevel;
803
+ this.searchLevel = newLevel;
804
+ }
805
+ const numBoolVars = this.numBoolVars;
806
+ this.reinitClauses(numScopes, numBoolVars);
807
+ return numBoolVars;
808
+ }
809
+
810
+ private popScope(numScopes: number) {
811
+ this.popScopeCore(numScopes, new Set());
812
+ this.cachedGeneration.clear();
813
+ }
814
+
815
+ private reinitClauses(numScopes: number, numBoolVars: number) {
816
+ if (this.clausesToReinit.length === 0) return;
817
+ const store = this.clauses;
818
+ const limit = Math.min(this.scopeLevel + numScopes, this.clausesToReinit.length - 1);
819
+ for (let level = this.scopeLevel + 1; level <= limit; level++) {
820
+ const clauses = this.clausesToReinit[level];
821
+ for (const clause of clauses) {
822
+ store.checkLive(clause);
823
+ if (store.isDeleted(clause)) {
824
+ store.setReinit(clause, false);
825
+ store.setReinternalizeAtoms(clause, false);
826
+ store.free(clause);
827
+ continue;
828
+ }
829
+ let isKept = false;
830
+ const atoms = store.getAtoms(clause);
831
+ if (store.isReinternalizeAtoms(clause) && atoms.length > 0) {
832
+ for (let index = 0; index < 2; index++)
833
+ if (getVar(store.getLiteral(clause, index)) < numBoolVars)
834
+ this.removeWatchLiteral(clause, index);
835
+ const isThLemma = store.getKind(clause) === CLS_TH_LEMMA;
836
+ for (let index = 0; index < atoms.length; index++) {
837
+ const atom = atoms[index];
838
+ this.internalize(atom, !this.manager.isNot(atom));
839
+ const literal = mkLiteral(this.getBoolVar(atom), store.getAtomSign(clause, index));
840
+ store.setLiteral(clause, index, literal);
841
+ if (isThLemma) this.markLiteralAsRelevant(literal);
842
+ }
843
+ store.swapLiterals(clause, 0, this.selectWatchLiteral(clause, 0));
844
+ store.swapLiterals(clause, 1, this.selectWatchLiteral(clause, 1));
845
+ this.addWatchLiteral(clause, 0);
846
+ this.addWatchLiteral(clause, 1);
847
+ const first = store.getLiteral(clause, 0);
848
+ if (this.assignment[first] === L_FALSE) this.setConflict(clause, null);
849
+ else if (this.assignment[store.getLiteral(clause, 1)] === L_FALSE)
850
+ this.assign(first, clause);
851
+ isKept = true;
852
+ } else {
853
+ const first = store.getLiteral(clause, 0);
854
+ const second = store.getLiteral(clause, 1);
855
+ if (store.getKind(clause) === CLS_TH_LEMMA) {
856
+ this.markLiteralAsRelevant(first);
857
+ this.markLiteralAsRelevant(second);
858
+ }
859
+ if (this.assignment[first] === L_FALSE && this.isEmptyClause(clause)) {
860
+ this.setConflict(clause, null);
861
+ isKept = true;
862
+ } else if (
863
+ this.assignment[second] === L_FALSE &&
864
+ this.assignment[first] === L_UNDEF &&
865
+ this.isUnitClause(clause)
866
+ ) {
867
+ this.assign(first, clause);
868
+ isKept = true;
869
+ }
870
+ }
871
+ if (isKept && this.scopeLevel > this.baseLevel) {
872
+ while (this.clausesToReinit.length <= this.scopeLevel) this.clausesToReinit.push([]);
873
+ this.clausesToReinit[this.scopeLevel].push(clause);
874
+ } else {
875
+ if (store.isReinternalizeAtoms(clause)) this.releaseAtoms(clause);
876
+ store.setReinit(clause, false);
877
+ store.setReinternalizeAtoms(clause, false);
878
+ }
879
+ }
880
+ this.clausesToReinit[level] = [];
881
+ }
882
+ }
883
+
884
+ private isEmptyClause(clause: Clause) {
885
+ const store = this.clauses;
886
+ for (let index = 0; index < store.getNumLiterals(clause); index++)
887
+ if (this.assignment[store.getLiteral(clause, index)] !== L_FALSE) return false;
888
+ return true;
889
+ }
890
+
891
+ // is_unit_clause: exactly one literal is unassigned and the rest are false.
892
+ private isUnitClause(clause: Clause) {
893
+ const store = this.clauses;
894
+ let isFound = false;
895
+ for (let index = 0; index < store.getNumLiterals(clause); index++) {
896
+ const value = this.assignment[store.getLiteral(clause, index)];
897
+ if (value === L_TRUE) return false;
898
+ if (value === L_UNDEF) {
899
+ if (isFound) return false;
900
+ isFound = true;
901
+ }
902
+ }
903
+ return isFound;
904
+ }
905
+
906
+ // guess with PS_CACHING_CONSERVATIVE: the cached phase while phase caching is on, else false.
907
+ private guess(boolVar: BoolVar) {
908
+ if (this.manager.kinds[this.boolVar2Expr[boolVar]] === KIND_QUANTIFIER) return false;
909
+ if (this.hasBoolVarFlag(boolVar, BV_TRUE_FIRST)) return true;
910
+ return (
911
+ this.isPhaseCacheOn &&
912
+ this.hasBoolVarFlag(boolVar, BV_PHASE_AVAILABLE) &&
913
+ this.hasBoolVarFlag(boolVar, BV_PHASE)
914
+ );
915
+ }
916
+
917
+ private updatePhaseCacheCounter() {
918
+ this.phaseCounter++;
919
+ if (this.phaseCounter < (this.isPhaseCacheOn ? PHASE_CACHING_ON : PHASE_CACHING_OFF)) return;
920
+ this.phaseCounter = 0;
921
+ this.isPhaseCacheOn = !this.isPhaseCacheOn;
922
+ }
923
+
924
+ private decide() {
925
+ const boolVar = this.caseSplitQueue.nextCaseSplit();
926
+ if (boolVar === undefined) return false;
927
+ const isPositive = this.guess(boolVar);
928
+ this.pushScope();
929
+ this.assign(mkLiteral(boolVar, !isPositive), "axiom", true);
930
+ return true;
931
+ }
932
+
933
+ private resolveConflict() {
934
+ this.numConflicts++;
935
+ this.numConflictsSinceRestart++;
936
+ this.numConflictsSinceLemmaGc++;
937
+ this.atomPropagationQueue = [];
938
+ this.eqPropagationQueue = [];
939
+ this.thEqQueue = [];
940
+ this.thDiseqQueue = [];
941
+ const head = this.scopeLevel === 0 ? 0 : this.scopeAssignedLimits[this.scopeLevel - 1];
942
+ for (let index = head; index < this.assignedLiterals.length; index++)
943
+ this.setBoolVarFlag(getVar(this.assignedLiterals[index]), BV_PHASE_AVAILABLE, false);
944
+ const resolution = this.conflictResolution;
945
+ if (
946
+ this.conflict === null ||
947
+ !resolution.resolve(this.conflict.justification, this.conflict.notLiteral)
948
+ )
949
+ return false;
950
+ const newLevel = resolution.newScopeLevel;
951
+ const literals = resolution.lemma;
952
+ const atoms = resolution.lemmaAtoms.items;
953
+ const visited = new Set<number>();
954
+ if (resolution.lemmaIscopeLevel > newLevel)
955
+ this.cacheGenerationOfLiterals(literals, newLevel, visited);
956
+ const wasRelevant = literals.map((literal) =>
957
+ this.isRelevant(this.boolVar2Expr[getVar(literal)]),
958
+ );
959
+ const numBoolVars = this.popScopeCore(this.scopeLevel - newLevel, visited);
960
+ if (resolution.lemmaIscopeLevel > this.scopeLevel)
961
+ for (let index = 0; index < literals.length; index++) {
962
+ const literal = literals[index];
963
+ if (getVar(literal) < numBoolVars) continue;
964
+ this.internalize(atoms[index], true);
965
+ literals[index] = isNegated(literal)
966
+ ? negate(this.getLiteral(atoms[index]))
967
+ : this.getLiteral(atoms[index]);
968
+ }
969
+ // restore_relevancy: marking propagates, so it only marks the literals that lost relevancy.
970
+ for (let index = 0; index < literals.length; index++)
971
+ if (wasRelevant[index] && !this.isRelevant(this.boolVar2Expr[getVar(literals[index])]))
972
+ this.markLiteralAsRelevant(literals[index]);
973
+ this.cachedGeneration.clear();
974
+ this.mkClause([...literals], null, CLS_LEARNED);
975
+ resolution.lemmaAtoms.reset();
976
+ this.bvarInc *= INV_DECAY;
977
+ this.updatePhaseCacheCounter();
978
+ return true;
979
+ }
980
+
981
+ private finalCheck() {
982
+ const qmanager = this.qmanager;
983
+ const status = qmanager.finalCheckEh(false);
984
+ if (status !== FC_DONE) return status;
985
+ const oldIndex = this.finalCheckIndex;
986
+ let result = FC_DONE;
987
+ let failure: SearchFailure = FAILURE_OK;
988
+ let level = 1;
989
+ let maxLevel = 1;
990
+ for (;;) {
991
+ const isTheory = this.finalCheckIndex === 0;
992
+ // Z3 registers every theory for UFNIA, and only arithmetic acts on tlapm's obligations. Of
993
+ // the theories between arithmetic and the quantifiers, theory_finite_set makes a set.range
994
+ // declaration, and its assume_eqs draws a random value.
995
+ if (!isTheory) {
996
+ this.manager.initFiniteSetPlugin();
997
+ this.getRandomValue();
998
+ }
999
+ const check = isTheory ? this.theory.finalCheckEh(level) : qmanager.finalCheckEh(true);
1000
+ if (isTheory) maxLevel = Math.max(maxLevel, NUM_ARITH_FINAL_CHECK_LEVELS);
1001
+ // Z3 reads the cancel flag after each theory's final check, and a canceled check gives up.
1002
+ const isCanceled = isTheory && this.checkCanceled();
1003
+ if (isCanceled) failure = FAILURE_CANCELED;
1004
+ else if (isTheory && check === FC_GIVEUP) failure = FAILURE_THEORY;
1005
+ this.finalCheckIndex = (this.finalCheckIndex + 1) % 2;
1006
+ if (check === FC_CONTINUE) return FC_CONTINUE;
1007
+ if (check === FC_GIVEUP || isCanceled) result = FC_GIVEUP;
1008
+ if (this.finalCheckIndex !== oldIndex) continue;
1009
+ if (level >= maxLevel || result === FC_DONE || this.canPropagate()) break;
1010
+ // A higher level reruns every check with the give-up state of the lower one cleared.
1011
+ level++;
1012
+ result = FC_DONE;
1013
+ failure = FAILURE_OK;
1014
+ }
1015
+ if (this.canPropagate()) return FC_CONTINUE;
1016
+ if (result === FC_GIVEUP && failure !== FAILURE_OK) this.lastSearchFailure = failure;
1017
+ return result;
1018
+ }
1019
+
1020
+ private boundedSearch() {
1021
+ for (;;) {
1022
+ while (!this.propagate()) {
1023
+ if (!this.resolveConflict()) return L_FALSE;
1024
+ if (!this.isInconsistent()) {
1025
+ if (this.isResourceLimitExceeded() || this.checkCanceled()) return L_UNDEF;
1026
+ if (
1027
+ this.numConflictsSinceRestart > this.restartThreshold &&
1028
+ this.scopeLevel - this.baseLevel > 2
1029
+ )
1030
+ return L_UNDEF;
1031
+ }
1032
+ if (this.numConflictsSinceLemmaGc > LEMMA_GC_INITIAL) this.delInactiveLemmas();
1033
+ this.dynAck.propagateEh();
1034
+ }
1035
+ if (this.isResourceLimitExceeded() && !this.isInconsistent()) return L_UNDEF;
1036
+ if (this.checkCanceled()) return L_UNDEF;
1037
+ if (!this.decide()) {
1038
+ if (this.isInconsistent()) return L_FALSE;
1039
+ const status = this.finalCheck();
1040
+ if (status === FC_DONE) return L_TRUE;
1041
+ if (status === FC_GIVEUP) return L_UNDEF;
1042
+ }
1043
+ if (this.isResourceLimitExceeded() && !this.isInconsistent()) return L_UNDEF;
1044
+ }
1045
+ }
1046
+
1047
+ // resource_limits_exceeded: while searching, a failure stops the search, and the cancel flag is
1048
+ // read twice.
1049
+ private isResourceLimitExceeded() {
1050
+ if (this.isSearching && (this.lastSearchFailure !== FAILURE_OK || this.checkCanceled()))
1051
+ return true;
1052
+ return this.checkCanceled();
1053
+ }
1054
+
1055
+ // reset_model: the model generator still holds the proto model.
1056
+ private resetModel() {
1057
+ const state = this.modelState;
1058
+ if (state.kind === "none" || state.kind === "generated") return;
1059
+ if (state.kind === "final") state.model.decRef();
1060
+ state.generated.model.decRef();
1061
+ this.modelState = { kind: "generated", generated: state.generated };
1062
+ }
1063
+
1064
+ // mk_proto_model: the model of the current assignment, which the quantifier manager adjusts. The
1065
+ // model generator releases the last model first.
1066
+ private mkProtoModel() {
1067
+ if (this.modelState.kind !== "none") this.modelState.generated.model.decRef();
1068
+ const generated = mkModel(this);
1069
+ generated.model.incRef();
1070
+ this.modelState = { kind: "generated", generated };
1071
+ this.qmanager.adjustModel(generated.model);
1072
+ generated.model.completePartialFuncs(false);
1073
+ generated.model.cleanup();
1074
+ generated.model.incRef();
1075
+ this.modelState = { kind: "proto", generated };
1076
+ return generated;
1077
+ }
1078
+
1079
+ // get_model: the model that the proto model makes, which the caller holds too. add_rec_funs gives
1080
+ // each named expression without an interpretation its expression.
1081
+ getModel() {
1082
+ if (this.modelState.kind === "final") return this.modelState.model;
1083
+ const generated =
1084
+ this.modelState.kind === "proto" ? this.modelState.generated : this.mkProtoModel();
1085
+ const model = generated.model.mkModel();
1086
+ for (const entry of this.manager.macros)
1087
+ if (!model.constInterps.has(entry[0])) model.registerConst(entry[0], entry[1]);
1088
+ model.incRef();
1089
+ this.modelState = { kind: "final", generated, model };
1090
+ return model;
1091
+ }
1092
+
1093
+ // validate_model: every relevant ground literal of the assignment holds in the model.
1094
+ validateModel(model: ProtoModel) {
1095
+ const manager = this.manager;
1096
+ for (const literal of this.assignedLiterals) {
1097
+ const atom = this.boolVar2Expr[getVar(literal)];
1098
+ if (!this.isRelevant(atom)) continue;
1099
+ // n and res of Z3's loop hold the literal and its value.
1100
+ const expr = isNegated(literal) ? manager.mkNot(atom) : atom;
1101
+ manager.incRef(expr);
1102
+ const value = manager.isGround(expr) ? model.eval(expr, false) : -1;
1103
+ if (value !== -1) manager.decRef(value);
1104
+ manager.decRef(expr);
1105
+ if (value === manager.falseExpr) return false;
1106
+ }
1107
+ return true;
1108
+ }
1109
+
1110
+ // restart: decides whether the search goes on after a bounded search. A candidate model of a
1111
+ // problem with quantifiers goes to model-based quantifier instantiation, which may add instances
1112
+ // and force a restart.
1113
+ private restart(status: number, currentLevel: number): number | undefined {
1114
+ const qmanager = this.qmanager;
1115
+ this.resetModel();
1116
+ let isForcedRestart = false;
1117
+ if (this.lastSearchFailure !== FAILURE_OK) {
1118
+ if (
1119
+ this.lastSearchFailure !== FAILURE_THEORY ||
1120
+ status === L_FALSE ||
1121
+ this.isInconsistent() ||
1122
+ !qmanager.hasQuantifiers() ||
1123
+ !qmanager.isModelBased()
1124
+ )
1125
+ return status;
1126
+ const generated = this.mkProtoModel();
1127
+ if (qmanager.checkModel(generated.model, generated.root2value) !== CHECK_MODEL_RESTART)
1128
+ return status;
1129
+ this.lastSearchFailure = FAILURE_OK;
1130
+ status = L_UNDEF;
1131
+ isForcedRestart = true;
1132
+ }
1133
+ if (status === L_FALSE) return L_FALSE;
1134
+ if (status === L_TRUE && !qmanager.hasQuantifiers()) return L_TRUE;
1135
+ if (status === L_TRUE) {
1136
+ const generated = this.mkProtoModel();
1137
+ const result = qmanager.isModelBased()
1138
+ ? qmanager.checkModel(generated.model, generated.root2value)
1139
+ : CHECK_MODEL_UNKNOWN;
1140
+ if (result === CHECK_MODEL_SAT) return L_TRUE;
1141
+ if (result === CHECK_MODEL_UNKNOWN) {
1142
+ this.lastSearchFailure = FAILURE_QUANTIFIERS;
1143
+ return L_UNDEF;
1144
+ }
1145
+ }
1146
+ // inc_limits with RS_IN_OUT_GEOMETRIC.
1147
+ if (this.numConflictsSinceRestart >= this.restartThreshold) {
1148
+ this.restartThreshold = Math.trunc(this.restartThreshold * RESTART_FACTOR);
1149
+ if (this.restartThreshold > this.restartOuterThreshold) {
1150
+ this.restartThreshold = RESTART_INITIAL;
1151
+ this.restartOuterThreshold = Math.trunc(this.restartOuterThreshold * RESTART_FACTOR);
1152
+ }
1153
+ }
1154
+ this.numConflictsSinceRestart = 0;
1155
+ if (isForcedRestart || status === L_TRUE || this.agility < RESTART_AGILITY_THRESHOLD) {
1156
+ if (this.scopeLevel > currentLevel) this.popScope(this.scopeLevel - currentLevel);
1157
+ if (!this.isInconsistent()) this.theory.restartEh();
1158
+ if (!this.isInconsistent()) qmanager.restartEh();
1159
+ if (this.isInconsistent()) {
1160
+ this.resolveConflict();
1161
+ return L_FALSE;
1162
+ }
1163
+ }
1164
+ return undefined;
1165
+ }
1166
+
1167
+ // can_delete: a lemma that justifies neither of its watched literals.
1168
+ private isDeletable(clause: Clause) {
1169
+ const store = this.clauses;
1170
+ if (store.isReinit(clause)) return false;
1171
+ return (
1172
+ this.justifications[getVar(store.getLiteral(clause, 0))] !== clause &&
1173
+ this.justifications[getVar(store.getLiteral(clause, 1))] !== clause
1174
+ );
1175
+ }
1176
+
1177
+ private hasMoreThanKUnassignedLiterals(clause: Clause, limit: number) {
1178
+ const store = this.clauses;
1179
+ let remaining = limit;
1180
+ for (let index = 0; index < store.getNumLiterals(clause); index++)
1181
+ if (this.assignment[store.getLiteral(clause, index)] === L_UNDEF && --remaining === 0)
1182
+ return true;
1183
+ return false;
1184
+ }
1185
+
1186
+ // del_inactive_lemmas2: deletes the lemmas of the current user scope with low activity and many
1187
+ // unassigned literals. The newest sixteenth of them tolerates more unassigned literals.
1188
+ private delInactiveLemmas() {
1189
+ const lemmas = this.lemmas;
1190
+ const startAt = this.baseLevel === 0 ? 0 : this.baseScopeLemmaLimits[this.baseLevel - 1];
1191
+ const realSize = lemmas.length - startAt;
1192
+ const newFirstIndex = startAt + Math.trunc(realSize / NEW_OLD_RATIO) * (NEW_OLD_RATIO - 1);
1193
+ let kept = startAt;
1194
+ for (let index = startAt; index < lemmas.length; index++) {
1195
+ const clause = lemmas[index];
1196
+ this.clauses.checkLive(clause);
1197
+ if (
1198
+ this.isDeletable(clause) &&
1199
+ (this.clauses.isDeleted(clause) ||
1200
+ (this.clauses.getActivity(clause) < OLD_CLAUSE_ACTIVITY &&
1201
+ this.hasMoreThanKUnassignedLiterals(
1202
+ clause,
1203
+ index >= newFirstIndex ? NEW_CLAUSE_RELEVANCY : OLD_CLAUSE_RELEVANCY,
1204
+ )))
1205
+ ) {
1206
+ this.deleteClause(clause);
1207
+ continue;
1208
+ }
1209
+ lemmas[kept++] = clause;
1210
+ }
1211
+ shrink(lemmas, kept);
1212
+ this.numConflictsSinceLemmaGc = 0;
1213
+ }
1214
+
1215
+ // push: a user scope, which pop removes with everything asserted in it.
1216
+ push() {
1217
+ this.popToBaseLevel();
1218
+ this.pushScope();
1219
+ this.baseScopeLemmaLimits.push(this.lemmas.length);
1220
+ this.baseLevel++;
1221
+ this.searchLevel++;
1222
+ }
1223
+
1224
+ pop(numScopes: number) {
1225
+ this.popToBaseLevel();
1226
+ this.popScope(numScopes);
1227
+ }
1228
+
1229
+ // pop_to_base_lvl: undoes the decisions of the last search, before new assertions.
1230
+ popToBaseLevel() {
1231
+ if (this.scopeLevel > this.baseLevel) this.popScope(this.scopeLevel - this.baseLevel);
1232
+ }
1233
+
1234
+ // internalize_assertions: the formulas of the asserted formulas, after preprocessing. When they
1235
+ // are inconsistent, none is internalized and the context has an axiom conflict.
1236
+ internalizeAssertions(asserted: AssertedFormulas) {
1237
+ const formulas = asserted.reduce();
1238
+ if (asserted.isInconsistent) {
1239
+ this.setConflict("axiom", null);
1240
+ return;
1241
+ }
1242
+ for (const formula of formulas) {
1243
+ if (this.isInconsistent()) break;
1244
+ this.internalizeAssertion(formula, 0);
1245
+ }
1246
+ }
1247
+
1248
+ // init_assumptions: the search runs above a scope where the assumptions hold.
1249
+ initAssumptions(assumptions: number[]) {
1250
+ if (assumptions.length > 0) {
1251
+ this.propagate();
1252
+ if (this.isInconsistent()) return;
1253
+ this.delInactiveLemmas();
1254
+ this.pushScope();
1255
+ for (const assumption of assumptions) {
1256
+ if (this.isInconsistent()) break;
1257
+ this.internalizeAssertion(assumption, 0);
1258
+ }
1259
+ }
1260
+ this.searchLevel = this.scopeLevel;
1261
+ }
1262
+
1263
+ search() {
1264
+ if (this.isInconsistent()) return L_FALSE;
1265
+ this.resetModel();
1266
+ this.theory.initSearchEh();
1267
+ this.qmanager.initSearchEh();
1268
+ this.dynAck.initSearchEh();
1269
+ this.numConflicts = 0;
1270
+ this.numConflictsSinceRestart = 0;
1271
+ this.numConflictsSinceLemmaGc = 0;
1272
+ this.restartThreshold = RESTART_INITIAL;
1273
+ this.restartOuterThreshold = RESTART_INITIAL;
1274
+ this.agility = 0;
1275
+ this.finalCheckIndex = 0;
1276
+ this.lastSearchFailure = FAILURE_OK;
1277
+ this.isSearching = true;
1278
+ const currentLevel = this.scopeLevel;
1279
+ let result: number | undefined;
1280
+ while (result === undefined) {
1281
+ const status = this.boundedSearch();
1282
+ result =
1283
+ status !== L_FALSE && this.checkCanceled() ? L_UNDEF : this.restart(status, currentLevel);
1284
+ }
1285
+ this.isSearching = false;
1286
+ return result;
1287
+ }
1288
+
1289
+ getNumConflicts() {
1290
+ return this.numConflicts;
1291
+ }
1292
+ }