tla-ts 0.0.4-dev.0be9567 → 0.0.4-dev.2a873ee

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 (320) hide show
  1. package/README.md +37 -126
  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 +33 -1
  6. package/dist/index.d.ts +33 -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/ast.ts +2 -2
  26. package/src/check.ts +1 -0
  27. package/src/cli-options.ts +1 -1
  28. package/src/errors.ts +88 -29
  29. package/src/evaluate.ts +56 -21
  30. package/src/index.ts +10 -0
  31. package/src/live-check.ts +1 -1
  32. package/src/{obligation-action.ts → obligations/action.ts} +247 -69
  33. package/src/{obligation-anon.ts → obligations/anon.ts} +49 -47
  34. package/src/obligations/ctx.ts +64 -0
  35. package/src/{obligation-deref.ts → obligations/deref.ts} +51 -92
  36. package/src/{obligation-expression.ts → obligations/expression.ts} +117 -57
  37. package/src/{obligation-format.ts → obligations/format.ts} +7 -8
  38. package/src/{obligation-generate.ts → obligations/generate.ts} +170 -192
  39. package/src/{obligations.ts → obligations/index.ts} +32 -16
  40. package/src/{obligation-levels.ts → obligations/levels.ts} +26 -28
  41. package/src/{obligation-module.ts → obligations/module.ts} +113 -121
  42. package/src/obligations/pickle.ts +98 -0
  43. package/src/obligations/pragma.ts +189 -0
  44. package/src/{obligation-printer.ts → obligations/printer.ts} +22 -80
  45. package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
  46. package/src/{obligation-source.ts → obligations/source.ts} +154 -99
  47. package/src/obligations/temporal.ts +321 -0
  48. package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
  49. package/src/ocaml-marshal.ts +166 -0
  50. package/src/parallel-worker.ts +2 -18
  51. package/src/parallel.ts +10 -43
  52. package/src/prove/decide-pool.ts +95 -0
  53. package/src/prove/deciders.ts +33 -0
  54. package/src/prove/first-order.ts +78 -0
  55. package/src/prove/isabelle/LICENSE +36 -0
  56. package/src/prove/isabelle/LICENSE-tlapm +26 -0
  57. package/src/prove/isabelle/decide.ts +47 -0
  58. package/src/prove/isabelle/encode.ts +446 -0
  59. package/src/prove/isabelle/isar/inner-syntax.ts +579 -0
  60. package/src/prove/isabelle/isar/method.ts +367 -0
  61. package/src/prove/isabelle/isar/proof.ts +28 -0
  62. package/src/prove/isabelle/isar/statement.ts +57 -0
  63. package/src/prove/isabelle/isar/theory.ts +192 -0
  64. package/src/prove/isabelle/isar/type-check.ts +278 -0
  65. package/src/prove/isabelle/provers/blast/blast.ts +258 -0
  66. package/src/prove/isabelle/provers/blast/branch.ts +322 -0
  67. package/src/prove/isabelle/provers/blast/prove.ts +347 -0
  68. package/src/prove/isabelle/provers/blast/rules.ts +330 -0
  69. package/src/prove/isabelle/provers/blast/state.ts +180 -0
  70. package/src/prove/isabelle/provers/blast/term.ts +546 -0
  71. package/src/prove/isabelle/provers/clasimp.ts +150 -0
  72. package/src/prove/isabelle/provers/classical.ts +389 -0
  73. package/src/prove/isabelle/provers/hypsubst.ts +433 -0
  74. package/src/prove/isabelle/pure/bicompose.ts +174 -0
  75. package/src/prove/isabelle/pure/consts.ts +32 -0
  76. package/src/prove/isabelle/pure/conv.ts +79 -0
  77. package/src/prove/isabelle/pure/drule.ts +264 -0
  78. package/src/prove/isabelle/pure/envir.ts +331 -0
  79. package/src/prove/isabelle/pure/goal.ts +53 -0
  80. package/src/prove/isabelle/pure/interrupt.ts +16 -0
  81. package/src/prove/isabelle/pure/list.ts +12 -0
  82. package/src/prove/isabelle/pure/logic.ts +330 -0
  83. package/src/prove/isabelle/pure/name.ts +98 -0
  84. package/src/prove/isabelle/pure/net.ts +215 -0
  85. package/src/prove/isabelle/pure/object-logic.ts +41 -0
  86. package/src/prove/isabelle/pure/pattern.ts +631 -0
  87. package/src/prove/isabelle/pure/raw-simplifier.ts +338 -0
  88. package/src/prove/isabelle/pure/rewriter.ts +602 -0
  89. package/src/prove/isabelle/pure/search.ts +68 -0
  90. package/src/prove/isabelle/pure/seq.ts +133 -0
  91. package/src/prove/isabelle/pure/simplifier.ts +106 -0
  92. package/src/prove/isabelle/pure/table.ts +201 -0
  93. package/src/prove/isabelle/pure/tactic.ts +168 -0
  94. package/src/prove/isabelle/pure/tactical.ts +264 -0
  95. package/src/prove/isabelle/pure/term-ord.ts +105 -0
  96. package/src/prove/isabelle/pure/term.ts +688 -0
  97. package/src/prove/isabelle/pure/thm.ts +783 -0
  98. package/src/prove/isabelle/pure/type.ts +121 -0
  99. package/src/prove/isabelle/pure/unify.ts +753 -0
  100. package/src/prove/isabelle/pure/variable.ts +25 -0
  101. package/src/prove/isabelle/tla/classical-setup.ts +101 -0
  102. package/src/prove/isabelle/tla/context.ts +74 -0
  103. package/src/prove/isabelle/tla/logic-data.ts +5 -0
  104. package/src/prove/isabelle/tla/logic.ts +215 -0
  105. package/src/prove/isabelle/tla/simpdata.ts +241 -0
  106. package/src/prove/isabelle/tools/intuitionistic.ts +132 -0
  107. package/src/prove/ocaml.ts +22 -0
  108. package/src/prove/prep/action.ts +508 -0
  109. package/src/prove/prep/defs.ts +33 -0
  110. package/src/prove/prep/elab.ts +156 -0
  111. package/src/prove/prep/enabled-axioms.ts +189 -0
  112. package/src/prove/prep/enabled.ts +43 -0
  113. package/src/prove/prep/fingerprint.ts +388 -0
  114. package/src/prove/prep/flatten.ts +70 -0
  115. package/src/prove/prep/level-comparison.ts +529 -0
  116. package/src/prove/prep/md5.ts +67 -0
  117. package/src/prove/prep/pragma.ts +221 -0
  118. package/src/prove/prep/trivial.ts +22 -0
  119. package/src/prove/prove-worker.ts +22 -0
  120. package/src/prove/prove.ts +498 -0
  121. package/src/prove/ptl/decide.ts +68 -0
  122. package/src/prove/ptl/encode.ts +255 -0
  123. package/src/prove/ptl/sat.ts +151 -0
  124. package/src/prove/ptl/tableau.ts +276 -0
  125. package/src/prove/smt/axiomatize.ts +69 -0
  126. package/src/prove/smt/axioms.ts +209 -0
  127. package/src/prove/smt/context.ts +108 -0
  128. package/src/prove/smt/data.ts +636 -0
  129. package/src/prove/smt/encode.ts +164 -0
  130. package/src/prove/smt/extensionality.ts +65 -0
  131. package/src/prove/smt/flatten.ts +112 -0
  132. package/src/prove/smt/format.ts +154 -0
  133. package/src/prove/smt/instances.ts +181 -0
  134. package/src/prove/smt/rewrite.ts +772 -0
  135. package/src/prove/smt/simplify.ts +57 -0
  136. package/src/prove/smt/solver/act-cache.ts +265 -0
  137. package/src/prove/smt/solver/arith-rewriter.ts +962 -0
  138. package/src/prove/smt/solver/array.ts +22 -0
  139. package/src/prove/smt/solver/asserted-formulas.ts +469 -0
  140. package/src/prove/smt/solver/ast-lt.ts +157 -0
  141. package/src/prove/smt/solver/ast-util.ts +91 -0
  142. package/src/prove/smt/solver/ast.ts +1315 -0
  143. package/src/prove/smt/solver/auf-solver.ts +572 -0
  144. package/src/prove/smt/solver/case-split-queue.ts +178 -0
  145. package/src/prove/smt/solver/cg-table.ts +175 -0
  146. package/src/prove/smt/solver/clause.ts +276 -0
  147. package/src/prove/smt/solver/conflict-resolution.ts +343 -0
  148. package/src/prove/smt/solver/context.ts +1292 -0
  149. package/src/prove/smt/solver/ctx-simplify.ts +334 -0
  150. package/src/prove/smt/solver/der.ts +224 -0
  151. package/src/prove/smt/solver/dyn-ack.ts +141 -0
  152. package/src/prove/smt/solver/e-matching.ts +659 -0
  153. package/src/prove/smt/solver/enode.ts +259 -0
  154. package/src/prove/smt/solver/expr-ref.ts +21 -0
  155. package/src/prove/smt/solver/expr-safe-replace.ts +172 -0
  156. package/src/prove/smt/solver/fingerprints.ts +92 -0
  157. package/src/prove/smt/solver/flat-literals.ts +51 -0
  158. package/src/prove/smt/solver/goal.ts +138 -0
  159. package/src/prove/smt/solver/hash.ts +173 -0
  160. package/src/prove/smt/solver/hashtable.ts +166 -0
  161. package/src/prove/smt/solver/internalizer.ts +1381 -0
  162. package/src/prove/smt/solver/lbool.ts +5 -0
  163. package/src/prove/smt/solver/literal.ts +24 -0
  164. package/src/prove/smt/solver/lp/bound-propagator.ts +391 -0
  165. package/src/prove/smt/solver/lp/constraints.ts +78 -0
  166. package/src/prove/smt/solver/lp/containers.ts +296 -0
  167. package/src/prove/smt/solver/lp/core-solver.ts +504 -0
  168. package/src/prove/smt/solver/lp/cross-nested.ts +224 -0
  169. package/src/prove/smt/solver/lp/dep-intervals.ts +642 -0
  170. package/src/prove/smt/solver/lp/dependency.ts +51 -0
  171. package/src/prove/smt/solver/lp/dioph-eq.ts +1181 -0
  172. package/src/prove/smt/solver/lp/explanation.ts +46 -0
  173. package/src/prove/smt/solver/lp/gomory.ts +255 -0
  174. package/src/prove/smt/solver/lp/hnf-cutter.ts +125 -0
  175. package/src/prove/smt/solver/lp/hnf.ts +281 -0
  176. package/src/prove/smt/solver/lp/int-cube.ts +295 -0
  177. package/src/prove/smt/solver/lp/int-solver.ts +514 -0
  178. package/src/prove/smt/solver/lp/lar-solver.ts +816 -0
  179. package/src/prove/smt/solver/lp/lar-term.ts +62 -0
  180. package/src/prove/smt/solver/lp/lp-settings.ts +91 -0
  181. package/src/prove/smt/solver/lp/maximize-term.ts +347 -0
  182. package/src/prove/smt/solver/lp/nex-creator.ts +381 -0
  183. package/src/prove/smt/solver/lp/nex.ts +120 -0
  184. package/src/prove/smt/solver/lp/nla-backoff.ts +43 -0
  185. package/src/prove/smt/solver/lp/nla-basics.ts +398 -0
  186. package/src/prove/smt/solver/lp/nla-core.ts +968 -0
  187. package/src/prove/smt/solver/lp/nla-divisions.ts +332 -0
  188. package/src/prove/smt/solver/lp/nla-emonics.ts +373 -0
  189. package/src/prove/smt/solver/lp/nla-factorization.ts +30 -0
  190. package/src/prove/smt/solver/lp/nla-grobner.ts +904 -0
  191. package/src/prove/smt/solver/lp/nla-horner.ts +126 -0
  192. package/src/prove/smt/solver/lp/nla-intervals.ts +265 -0
  193. package/src/prove/smt/solver/lp/nla-monomial-bounds.ts +715 -0
  194. package/src/prove/smt/solver/lp/nla-monotone.ts +59 -0
  195. package/src/prove/smt/solver/lp/nla-order.ts +307 -0
  196. package/src/prove/smt/solver/lp/nla-patcher.ts +112 -0
  197. package/src/prove/smt/solver/lp/nla-tangents.ts +251 -0
  198. package/src/prove/smt/solver/lp/nla-throttle.ts +85 -0
  199. package/src/prove/smt/solver/lp/nla-types.ts +124 -0
  200. package/src/prove/smt/solver/lp/nla-var-eqs.ts +141 -0
  201. package/src/prove/smt/solver/lp/nra-solver.ts +440 -0
  202. package/src/prove/smt/solver/lp/numeric-pair.ts +65 -0
  203. package/src/prove/smt/solver/lp/pdd-interval.ts +31 -0
  204. package/src/prove/smt/solver/lp/pdd-simplifier.ts +184 -0
  205. package/src/prove/smt/solver/lp/pdd-solver.ts +408 -0
  206. package/src/prove/smt/solver/lp/pdd.ts +971 -0
  207. package/src/prove/smt/solver/lp/random-updater.ts +138 -0
  208. package/src/prove/smt/solver/lp/static-matrix.ts +246 -0
  209. package/src/prove/smt/solver/lp/union-find.ts +96 -0
  210. package/src/prove/smt/solver/mam-compiler.ts +847 -0
  211. package/src/prove/smt/solver/mam-interpreter.ts +400 -0
  212. package/src/prove/smt/solver/maximize-ac-sharing.ts +118 -0
  213. package/src/prove/smt/solver/model-checker.ts +347 -0
  214. package/src/prove/smt/solver/model-evaluator.ts +262 -0
  215. package/src/prove/smt/solver/model-finder.ts +155 -0
  216. package/src/prove/smt/solver/model-generator.ts +97 -0
  217. package/src/prove/smt/solver/model-macro-solver.ts +728 -0
  218. package/src/prove/smt/solver/nlsat/algebraic.ts +1161 -0
  219. package/src/prove/smt/solver/nlsat/atoms.ts +238 -0
  220. package/src/prove/smt/solver/nlsat/evaluator.ts +303 -0
  221. package/src/prove/smt/solver/nlsat/explain.ts +607 -0
  222. package/src/prove/smt/solver/nlsat/interval-set.ts +351 -0
  223. package/src/prove/smt/solver/nlsat/levelwise.ts +737 -0
  224. package/src/prove/smt/solver/nlsat/mpbq.ts +161 -0
  225. package/src/prove/smt/solver/nlsat/polynomial.ts +1722 -0
  226. package/src/prove/smt/solver/nlsat/solver.ts +911 -0
  227. package/src/prove/smt/solver/nlsat/types.ts +43 -0
  228. package/src/prove/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
  229. package/src/prove/smt/solver/nlsat/upolynomial.ts +996 -0
  230. package/src/prove/smt/solver/nnf.ts +469 -0
  231. package/src/prove/smt/solver/pattern-inference.ts +351 -0
  232. package/src/prove/smt/solver/poly-rewriter.ts +477 -0
  233. package/src/prove/smt/solver/propagate-values.ts +83 -0
  234. package/src/prove/smt/solver/proto-model.ts +551 -0
  235. package/src/prove/smt/solver/pull-quant.ts +139 -0
  236. package/src/prove/smt/solver/qe-lite-der.ts +526 -0
  237. package/src/prove/smt/solver/qe-lite-fm.ts +576 -0
  238. package/src/prove/smt/solver/qe-lite.ts +381 -0
  239. package/src/prove/smt/solver/qi-queue.ts +362 -0
  240. package/src/prove/smt/solver/quant-tactics.ts +117 -0
  241. package/src/prove/smt/solver/quantifier-analyzer.ts +562 -0
  242. package/src/prove/smt/solver/quantifier-manager.ts +186 -0
  243. package/src/prove/smt/solver/random-gen.ts +10 -0
  244. package/src/prove/smt/solver/rational.ts +165 -0
  245. package/src/prove/smt/solver/ref-vector.ts +62 -0
  246. package/src/prove/smt/solver/relevancy.ts +247 -0
  247. package/src/prove/smt/solver/rewriter-tpl.ts +1159 -0
  248. package/src/prove/smt/solver/rewriter.ts +843 -0
  249. package/src/prove/smt/solver/rlimit.ts +79 -0
  250. package/src/prove/smt/solver/smtlib-parser.ts +526 -0
  251. package/src/prove/smt/solver/solver.ts +71 -0
  252. package/src/prove/smt/solver/sort.ts +35 -0
  253. package/src/prove/smt/solver/sticky-generations.ts +69 -0
  254. package/src/prove/smt/solver/theory-lra-axioms.ts +340 -0
  255. package/src/prove/smt/solver/theory-lra-bounds.ts +226 -0
  256. package/src/prove/smt/solver/theory-lra-final-check.ts +252 -0
  257. package/src/prove/smt/solver/theory-lra-internalize.ts +476 -0
  258. package/src/prove/smt/solver/theory-lra-model.ts +243 -0
  259. package/src/prove/smt/solver/theory-lra-propagate.ts +405 -0
  260. package/src/prove/smt/solver/theory-lra-state.ts +43 -0
  261. package/src/prove/smt/solver/theory-lra.ts +56 -0
  262. package/src/prove/smt/solver/var-subst.ts +258 -0
  263. package/src/prove/smt/solver/z3-license.txt +10 -0
  264. package/src/prove/smt/table.ts +170 -0
  265. package/src/prove/tlapm/tlapm.ts +96 -0
  266. package/src/prove/tlaps-module.ts +510 -0
  267. package/src/prove/verdict.ts +18 -0
  268. package/src/prove/zenon/LICENSE +32 -0
  269. package/src/prove/zenon/check/check.ts +105 -0
  270. package/src/prove/zenon/check/definitions.ts +144 -0
  271. package/src/prove/zenon/check/equality.ts +182 -0
  272. package/src/prove/zenon/check/functions.ts +62 -0
  273. package/src/prove/zenon/check/keys.ts +125 -0
  274. package/src/prove/zenon/check/propositional.ts +104 -0
  275. package/src/prove/zenon/check/records.ts +188 -0
  276. package/src/prove/zenon/check/relations.ts +121 -0
  277. package/src/prove/zenon/check/rewrites.ts +224 -0
  278. package/src/prove/zenon/check/sets.ts +302 -0
  279. package/src/prove/zenon/check/terms.ts +127 -0
  280. package/src/prove/zenon/check/tla.ts +126 -0
  281. package/src/prove/zenon/decide.ts +113 -0
  282. package/src/prove/zenon/encode.ts +379 -0
  283. package/src/prove/zenon/eqrel.ts +300 -0
  284. package/src/prove/zenon/equality-rules.ts +435 -0
  285. package/src/prove/zenon/expr.ts +551 -0
  286. package/src/prove/zenon/index-table.ts +350 -0
  287. package/src/prove/zenon/isar-case.ts +513 -0
  288. package/src/prove/zenon/isar.ts +1249 -0
  289. package/src/prove/zenon/limits.ts +15 -0
  290. package/src/prove/zenon/llproof.ts +173 -0
  291. package/src/prove/zenon/mltoll.ts +1566 -0
  292. package/src/prove/zenon/node.ts +297 -0
  293. package/src/prove/zenon/ocaml.ts +169 -0
  294. package/src/prove/zenon/parse.ts +247 -0
  295. package/src/prove/zenon/phrase.ts +191 -0
  296. package/src/prove/zenon/proof.ts +198 -0
  297. package/src/prove/zenon/propositional-rules.ts +387 -0
  298. package/src/prove/zenon/rules.ts +94 -0
  299. package/src/prove/zenon/search.ts +288 -0
  300. package/src/prove/zenon/tla/functions.ts +151 -0
  301. package/src/prove/zenon/tla/llproof.ts +480 -0
  302. package/src/prove/zenon/tla/records.ts +225 -0
  303. package/src/prove/zenon/tla/rewrites.ts +447 -0
  304. package/src/prove/zenon/tla/sets.ts +304 -0
  305. package/src/prove/zenon/tla/terms.ts +162 -0
  306. package/src/prove/zenon/tla/tla.ts +506 -0
  307. package/src/resolve.ts +1 -1
  308. package/src/run-cli.ts +283 -81
  309. package/src/sany-generator.ts +1 -1
  310. package/src/sany-report.ts +1 -1
  311. package/src/successors.ts +24 -5
  312. package/src/tlapm-cache.ts +236 -0
  313. package/src/tlc-random.ts +55 -32
  314. package/src/tlc-replay.ts +1 -1
  315. package/src/tlc-version.ts +8 -7
  316. package/src/values.ts +47 -4
  317. package/src/worker.ts +65 -0
  318. package/dist/parallel.cjs +0 -12
  319. package/dist/parallel.js +0 -12
  320. package/src/value-sources.ts +0 -29
@@ -0,0 +1,343 @@
1
+ // Ported from Z3 (src/smt/smt_conflict_resolution.cpp), MIT license, see z3-license.txt.
2
+ // First-UIP conflict resolution with lemma minimization. Equalities in a conflict are explained
3
+ // by walking the transitivity branches of the E-graph down to literals.
4
+ import { BinJustification, type BJustification } from "./clause";
5
+ import type { Context } from "./context";
6
+ import { Enode, EqJustification, Justification } from "./enode";
7
+ import { BoolVar, getVar, Literal, negate } from "./literal";
8
+ import { RefVector } from "./ref-vector";
9
+ import { shrink } from "./array";
10
+
11
+ export class ConflictResolution {
12
+ private antecedents: Literal[] = [];
13
+ private todoJustifications: Justification[] = [];
14
+ private todoJustificationsHead = 0;
15
+ private todoEqs: Enode[] = [];
16
+ private processedEqs = new Set<string>();
17
+ lemma: Literal[] = [];
18
+ // m_lemma_atoms: the atoms of the lemma, which it holds until the lemma is a clause.
19
+ readonly lemmaAtoms: RefVector;
20
+ private conflictLevel = 0;
21
+ newScopeLevel = 0;
22
+ lemmaIscopeLevel = 0;
23
+ private unmark: BoolVar[] = [];
24
+ // m_lvl_set: a level_approx_set, whose bit level mod 32 stands for each level of the lemma, so
25
+ // other levels can pass for them.
26
+ private levelSet = 0;
27
+ private minimizationStack: BoolVar[] = [];
28
+
29
+ constructor(private context: Context) {
30
+ this.lemmaAtoms = new RefVector(context.manager);
31
+ }
32
+
33
+ markLiteral(literal: Literal) {
34
+ this.antecedents.push(literal);
35
+ }
36
+
37
+ // mark_eq: the pair starts with the node whose owner has the smaller id, which fixes the order in
38
+ // which eq2literals walks the two branches.
39
+ markEq(first: Enode, second: Enode) {
40
+ if (first === second) return;
41
+ const ids = this.context.manager.ids;
42
+ const isSwapped = ids[first.owner] > ids[second.owner];
43
+ const lower = isSwapped ? second : first;
44
+ const upper = isSwapped ? first : second;
45
+ const key = `${lower.owner}:${upper.owner}`;
46
+ if (this.processedEqs.has(key)) return;
47
+ this.processedEqs.add(key);
48
+ this.todoEqs.push(lower, upper);
49
+ }
50
+
51
+ markJustification(justification: Justification) {
52
+ if (justification.isMarked) return;
53
+ justification.isMarked = true;
54
+ this.todoJustifications.push(justification);
55
+ }
56
+
57
+ markJustifiedEq(first: Enode, second: Enode, justification: EqJustification) {
58
+ this.eqJustificationToLiterals(first, second, justification);
59
+ }
60
+
61
+ private findCommonAncestor(first: Enode, second: Enode) {
62
+ for (let node: Enode | null = first; node !== null; node = node.transTarget)
63
+ node.isMarked2 = true;
64
+ let result = second;
65
+ while (!result.isMarked2) {
66
+ const target = result.transTarget;
67
+ if (target === null) break;
68
+ result = target;
69
+ }
70
+ for (let node: Enode | null = first; node !== null; node = node.transTarget)
71
+ node.isMarked2 = false;
72
+ return result;
73
+ }
74
+
75
+ private eqJustificationToLiterals(lhs: Enode, rhs: Enode, justification: EqJustification) {
76
+ if (justification.kind === "equation") this.antecedents.push(justification.literal);
77
+ else if (justification.kind === "justification")
78
+ this.markJustification(justification.justification);
79
+ else if (justification.kind === "congruence") {
80
+ if (justification.isCommutative) {
81
+ this.markEq(lhs.getArg(0), rhs.getArg(1));
82
+ this.markEq(lhs.getArg(1), rhs.getArg(0));
83
+ } else
84
+ for (let index = 0; index < lhs.numArgs; index++)
85
+ this.markEq(lhs.getArg(index), rhs.getArg(index));
86
+ }
87
+ }
88
+
89
+ private eqBranchToLiterals(start: Enode, ancestor: Enode) {
90
+ let node = start;
91
+ while (node !== ancestor) {
92
+ const target = node.transTarget;
93
+ if (target === null) break;
94
+ this.eqJustificationToLiterals(node, target, node.transJustification);
95
+ node = target;
96
+ }
97
+ }
98
+
99
+ private processJustifications() {
100
+ for (;;) {
101
+ while (this.todoJustificationsHead < this.todoJustifications.length) {
102
+ const justification = this.todoJustifications[this.todoJustificationsHead];
103
+ this.todoJustificationsHead++;
104
+ justification.getAntecedents(this);
105
+ }
106
+ while (this.todoEqs.length > 0) {
107
+ const second = this.todoEqs.pop();
108
+ const first = this.todoEqs.pop();
109
+ if (first === undefined || second === undefined) break;
110
+ const ancestor = this.findCommonAncestor(first, second);
111
+ this.eqBranchToLiterals(first, ancestor);
112
+ this.eqBranchToLiterals(second, ancestor);
113
+ }
114
+ if (this.todoJustificationsHead === this.todoJustifications.length) return;
115
+ }
116
+ }
117
+
118
+ private unmarkJustifications(oldHead: number) {
119
+ for (let index = oldHead; index < this.todoJustifications.length; index++)
120
+ this.todoJustifications[index].isMarked = false;
121
+ shrink(this.todoJustifications, oldHead);
122
+ this.todoJustificationsHead = oldHead;
123
+ this.todoEqs.length = 0;
124
+ this.processedEqs.clear();
125
+ }
126
+
127
+ private justificationToLiteralsCore(justification: Justification) {
128
+ const saved = this.antecedents;
129
+ this.antecedents = [];
130
+ this.markJustification(justification);
131
+ this.processJustifications();
132
+ const result = this.antecedents;
133
+ this.antecedents = saved;
134
+ return result;
135
+ }
136
+
137
+ justificationToLiterals(justification: Justification) {
138
+ const result = this.justificationToLiteralsCore(justification);
139
+ this.unmarkJustifications(0);
140
+ return result;
141
+ }
142
+
143
+ private getJustificationMaxLevel(justification: Justification) {
144
+ let result = 0;
145
+ for (const literal of this.justificationToLiterals(justification))
146
+ result = Math.max(result, this.context.getAssignLevel(literal));
147
+ return result;
148
+ }
149
+
150
+ private getMaxLevel(consequent: Literal, justification: BJustification) {
151
+ const context = this.context;
152
+ let result = consequent !== context.falseLiteral ? context.getAssignLevel(consequent) : 0;
153
+ if (justification instanceof Justification)
154
+ return Math.max(result, this.getJustificationMaxLevel(justification));
155
+ if (justification instanceof BinJustification)
156
+ return Math.max(result, context.getAssignLevel(justification.literal));
157
+ if (justification === null || justification === "axiom") return result;
158
+ const clauses = context.clauses;
159
+ const numLiterals = clauses.getNumLiterals(justification);
160
+ let start = 0;
161
+ if (consequent !== context.falseLiteral) {
162
+ if (clauses.getLiteral(justification, 0) === consequent) start = 1;
163
+ else {
164
+ result = Math.max(result, context.getAssignLevel(clauses.getLiteral(justification, 0)));
165
+ start = 2;
166
+ }
167
+ }
168
+ for (let index = start; index < numLiterals; index++)
169
+ result = Math.max(result, context.getAssignLevel(clauses.getLiteral(justification, index)));
170
+ const clauseJustification = clauses.getJustification(justification);
171
+ if (clauseJustification !== null)
172
+ result = Math.max(result, this.getJustificationMaxLevel(clauseJustification));
173
+ return result;
174
+ }
175
+
176
+ private processAntecedent(antecedent: Literal, marks: { count: number }) {
177
+ const context = this.context;
178
+ const boolVar = getVar(antecedent);
179
+ const level = context.getLevel(boolVar);
180
+ if (context.isMarked(boolVar) || level <= context.baseLevel) return;
181
+ context.setMark(boolVar, true);
182
+ context.incBvarActivity(boolVar);
183
+ if (level === this.conflictLevel) marks.count++;
184
+ else {
185
+ this.lemma.push(negate(antecedent));
186
+ this.lemmaAtoms.push(context.boolVar2Expr[boolVar]);
187
+ }
188
+ }
189
+
190
+ private processJustification(justification: Justification, marks: { count: number }) {
191
+ for (const literal of this.justificationToLiteralsCore(justification))
192
+ this.processAntecedent(literal, marks);
193
+ }
194
+
195
+ // Returns false when the conflict is at the search level, so the problem is unsat.
196
+ resolve(conflict: BJustification, notLiteral: Literal | null) {
197
+ const context = this.context;
198
+ this.lemma = [];
199
+ this.lemmaAtoms.reset();
200
+ let justification = conflict;
201
+ let consequent = notLiteral !== null ? negate(notLiteral) : context.falseLiteral;
202
+ this.conflictLevel = this.getMaxLevel(consequent, justification);
203
+ if (this.conflictLevel <= context.searchLevel) return false;
204
+ const assigned = context.assignedLiterals;
205
+ let index = assigned.length - 1;
206
+ while (index > 0 && context.getAssignLevel(assigned[index]) > this.conflictLevel) index--;
207
+ // The first slot holds the negated first unique implication point, found below.
208
+ this.lemma.push(context.falseLiteral);
209
+ this.lemmaAtoms.push(-1);
210
+ const marks = { count: 0 };
211
+ if (notLiteral !== null) this.processAntecedent(notLiteral, marks);
212
+ do {
213
+ if (justification instanceof Justification) this.processJustification(justification, marks);
214
+ else if (justification instanceof BinJustification)
215
+ this.processAntecedent(justification.literal, marks);
216
+ else if (justification !== null && justification !== "axiom") {
217
+ const clause = justification;
218
+ const clauses = context.clauses;
219
+ if (clauses.isLemma(clause)) clauses.incActivity(clause);
220
+ const numLiterals = clauses.getNumLiterals(clause);
221
+ let start = 0;
222
+ if (consequent !== context.falseLiteral) {
223
+ if (clauses.getLiteral(clause, 0) === consequent) start = 1;
224
+ else {
225
+ this.processAntecedent(negate(clauses.getLiteral(clause, 0)), marks);
226
+ start = 2;
227
+ }
228
+ }
229
+ for (let literalIndex = start; literalIndex < numLiterals; literalIndex++)
230
+ this.processAntecedent(negate(clauses.getLiteral(clause, literalIndex)), marks);
231
+ const clauseJustification = clauses.getJustification(clause);
232
+ if (clauseJustification !== null) this.processJustification(clauseJustification, marks);
233
+ }
234
+ while (!context.isMarked(getVar(assigned[index]))) index--;
235
+ consequent = assigned[index];
236
+ const consequentVar = getVar(consequent);
237
+ justification = context.justifications[consequentVar];
238
+ index--;
239
+ marks.count--;
240
+ context.setMark(consequentVar, false);
241
+ } while (marks.count > 0);
242
+ this.lemma[0] = negate(consequent);
243
+ this.lemmaAtoms.set(0, context.boolVar2Expr[getVar(consequent)]);
244
+ this.finalizeResolve();
245
+ return true;
246
+ }
247
+
248
+ private finalizeResolve() {
249
+ const context = this.context;
250
+ this.unmarkJustifications(0);
251
+ this.minimizeLemma();
252
+ this.newScopeLevel = context.searchLevel;
253
+ this.lemmaIscopeLevel = context.getInternLevel(getVar(this.lemma[0]));
254
+ for (let index = 1; index < this.lemma.length; index++) {
255
+ const boolVar = getVar(this.lemma[index]);
256
+ context.setMark(boolVar, false);
257
+ this.newScopeLevel = Math.max(this.newScopeLevel, context.getLevel(boolVar));
258
+ this.lemmaIscopeLevel = Math.max(this.lemmaIscopeLevel, context.getInternLevel(boolVar));
259
+ }
260
+ }
261
+
262
+ private processAntecedentForMinimization(antecedent: Literal) {
263
+ const context = this.context;
264
+ const boolVar = getVar(antecedent);
265
+ const level = context.getLevel(boolVar);
266
+ if (context.isMarked(boolVar) || level <= context.baseLevel) return true;
267
+ if ((this.levelSet & (1 << (level & 31))) === 0) return false;
268
+ context.setMark(boolVar, true);
269
+ this.unmark.push(boolVar);
270
+ this.minimizationStack.push(boolVar);
271
+ return true;
272
+ }
273
+
274
+ private processJustificationForMinimization(justification: Justification) {
275
+ for (const literal of this.justificationToLiteralsCore(justification))
276
+ if (!this.processAntecedentForMinimization(literal)) return false;
277
+ return true;
278
+ }
279
+
280
+ private resetUnmark(oldSize: number, oldHead: number) {
281
+ for (let index = oldSize; index < this.unmark.length; index++)
282
+ this.context.setMark(this.unmark[index], false);
283
+ shrink(this.unmark, oldSize);
284
+ this.unmarkJustifications(oldHead);
285
+ }
286
+
287
+ private isImpliedByMarked(literal: Literal) {
288
+ const context = this.context;
289
+ this.minimizationStack = [getVar(literal)];
290
+ const oldSize = this.unmark.length;
291
+ const oldHead = this.todoJustificationsHead;
292
+ while (this.minimizationStack.length > 0) {
293
+ const boolVar = this.minimizationStack.pop() ?? -1;
294
+ const justification = context.justifications[boolVar];
295
+ let isImplied = true;
296
+ if (justification instanceof Justification)
297
+ isImplied = this.processJustificationForMinimization(justification);
298
+ else if (justification instanceof BinJustification)
299
+ isImplied = this.processAntecedentForMinimization(justification.literal);
300
+ else if (justification === null || justification === "axiom")
301
+ isImplied = context.getLevel(boolVar) <= context.baseLevel;
302
+ else {
303
+ const clauses = context.clauses;
304
+ const numLiterals = clauses.getNumLiterals(justification);
305
+ const position = getVar(clauses.getLiteral(justification, 1)) === boolVar ? 1 : 0;
306
+ for (let index = 0; index < numLiterals && isImplied; index++)
307
+ if (index !== position)
308
+ isImplied = this.processAntecedentForMinimization(
309
+ negate(clauses.getLiteral(justification, index)),
310
+ );
311
+ const clauseJustification = clauses.getJustification(justification);
312
+ if (isImplied && clauseJustification !== null)
313
+ isImplied = this.processJustificationForMinimization(clauseJustification);
314
+ }
315
+ if (!isImplied) {
316
+ this.resetUnmark(oldSize, oldHead);
317
+ return false;
318
+ }
319
+ }
320
+ return true;
321
+ }
322
+
323
+ private minimizeLemma() {
324
+ const context = this.context;
325
+ this.unmark = [];
326
+ this.levelSet = 0;
327
+ for (const literal of this.lemma)
328
+ this.levelSet |= 1 << (context.getLevel(getVar(literal)) & 31);
329
+ let kept = 1;
330
+ for (let index = 1; index < this.lemma.length; index++) {
331
+ const literal = this.lemma[index];
332
+ if (this.isImpliedByMarked(literal)) this.unmark.push(getVar(literal));
333
+ else {
334
+ this.lemma[kept] = literal;
335
+ if (kept !== index) this.lemmaAtoms.set(kept, this.lemmaAtoms.items[index]);
336
+ kept++;
337
+ }
338
+ }
339
+ this.resetUnmark(0, 0);
340
+ shrink(this.lemma, kept);
341
+ this.lemmaAtoms.shrink(kept);
342
+ }
343
+ }