tla-ts 0.0.9 → 0.0.10-dev.63a6693bd

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 (400) hide show
  1. package/README.md +29 -1
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +29 -29
  5. package/dist/deciders.js +42 -42
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +106 -10
  8. package/dist/index.d.ts +106 -10
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +117 -37
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +2 -2
  20. package/dist/transfer.cjs +129 -104
  21. package/dist/transfer.js +129 -104
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +48 -62
  26. package/src/bags.ts +99 -102
  27. package/src/builtins.ts +28 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check-failure.ts +116 -0
  30. package/src/check-trace.ts +282 -0
  31. package/src/check.ts +599 -754
  32. package/src/cli/options.ts +12 -32
  33. package/src/cli/run.ts +0 -2
  34. package/src/cli/runtime-invariants.ts +136 -0
  35. package/src/cli/shared.ts +19 -12
  36. package/src/cli/tlapm.ts +14 -25
  37. package/src/cli/tlc-arguments.ts +652 -0
  38. package/src/cli/tlc-output.ts +687 -0
  39. package/src/cli/tlc-usage.ts +361 -0
  40. package/src/cli/tlc.ts +309 -658
  41. package/src/cli/typescript.ts +23 -0
  42. package/src/cli/xml-exporter.ts +27 -49
  43. package/src/cli.ts +0 -1
  44. package/src/codegen.ts +196 -74
  45. package/src/config.ts +140 -109
  46. package/src/coverage.ts +830 -0
  47. package/src/dependencies.ts +14 -53
  48. package/src/dfid.ts +618 -0
  49. package/src/environment-config.ts +315 -0
  50. package/src/environment.ts +169 -335
  51. package/src/errors.ts +71 -55
  52. package/src/evaluate.ts +598 -340
  53. package/src/exit-codes.ts +37 -3
  54. package/src/fingerprints.ts +75 -11
  55. package/src/index.ts +3 -0
  56. package/src/instances.ts +126 -21
  57. package/src/java-hash.ts +7 -0
  58. package/src/java-overrides.ts +4 -19
  59. package/src/json-reader.ts +455 -0
  60. package/src/json.ts +252 -139
  61. package/src/levels.ts +46 -58
  62. package/src/lexer.ts +80 -100
  63. package/src/liveness-checker.ts +103 -153
  64. package/src/liveness.ts +150 -109
  65. package/src/obligations/action.ts +8 -39
  66. package/src/obligations/anonymize.ts +88 -82
  67. package/src/obligations/equality.ts +1 -1
  68. package/src/obligations/expression.ts +20 -21
  69. package/src/obligations/format.ts +1 -9
  70. package/src/obligations/generate.ts +46 -49
  71. package/src/obligations/index.ts +2 -13
  72. package/src/obligations/levels.ts +4 -8
  73. package/src/obligations/module.ts +28 -53
  74. package/src/obligations/normalize.ts +7 -25
  75. package/src/obligations/operators.ts +1 -2
  76. package/src/obligations/pragma.ts +1 -10
  77. package/src/obligations/printer.ts +47 -46
  78. package/src/obligations/proof.ts +1 -5
  79. package/src/obligations/regroup.ts +1 -4
  80. package/src/obligations/simplify.ts +42 -58
  81. package/src/obligations/source.ts +48 -78
  82. package/src/obligations/subexpression.ts +5 -11
  83. package/src/obligations/substitution.ts +1 -3
  84. package/src/obligations/temporal.ts +13 -19
  85. package/src/obligations/tuples.ts +3 -9
  86. package/src/parallel-worker.ts +30 -60
  87. package/src/parallel.ts +57 -71
  88. package/src/parser.ts +584 -316
  89. package/src/pcal/ast.ts +53 -86
  90. package/src/pcal/parse.ts +144 -219
  91. package/src/pcal/tokenize.ts +91 -131
  92. package/src/pcal/translate.ts +83 -130
  93. package/src/pcal/validate.ts +48 -68
  94. package/src/pretty-print.ts +7 -8
  95. package/src/prover/decide-pool.ts +8 -13
  96. package/src/prover/deciders.ts +3 -7
  97. package/src/prover/first-order.ts +0 -11
  98. package/src/prover/isabelle/decide.ts +0 -6
  99. package/src/prover/isabelle/encode.ts +11 -23
  100. package/src/prover/isabelle/isar/inner-syntax.ts +114 -115
  101. package/src/prover/isabelle/isar/method.ts +29 -53
  102. package/src/prover/isabelle/isar/proof.ts +1 -10
  103. package/src/prover/isabelle/isar/statement.ts +1 -6
  104. package/src/prover/isabelle/isar/theory.ts +26 -36
  105. package/src/prover/isabelle/isar/type-check.ts +17 -31
  106. package/src/prover/isabelle/provers/blast/blast.ts +17 -104
  107. package/src/prover/isabelle/provers/blast/branch.ts +1 -32
  108. package/src/prover/isabelle/provers/blast/prove.ts +1 -35
  109. package/src/prover/isabelle/provers/blast/rules.ts +6 -44
  110. package/src/prover/isabelle/provers/blast/state.ts +2 -28
  111. package/src/prover/isabelle/provers/blast/term.ts +9 -36
  112. package/src/prover/isabelle/provers/clasimp.ts +1 -13
  113. package/src/prover/isabelle/provers/classical.ts +14 -42
  114. package/src/prover/isabelle/provers/hypsubst.ts +28 -51
  115. package/src/prover/isabelle/pure/bicompose.ts +25 -38
  116. package/src/prover/isabelle/pure/consts.ts +1 -7
  117. package/src/prover/isabelle/pure/conv.ts +3 -17
  118. package/src/prover/isabelle/pure/drule.ts +33 -42
  119. package/src/prover/isabelle/pure/envir.ts +3 -52
  120. package/src/prover/isabelle/pure/goal.ts +1 -7
  121. package/src/prover/isabelle/pure/interrupt.ts +0 -5
  122. package/src/prover/isabelle/pure/list.ts +0 -3
  123. package/src/prover/isabelle/pure/logic.ts +2 -15
  124. package/src/prover/isabelle/pure/name.ts +2 -9
  125. package/src/prover/isabelle/pure/net.ts +3 -59
  126. package/src/prover/isabelle/pure/object-logic.ts +3 -10
  127. package/src/prover/isabelle/pure/pattern.ts +2 -19
  128. package/src/prover/isabelle/pure/raw-simplifier.ts +17 -62
  129. package/src/prover/isabelle/pure/rewriter.ts +31 -67
  130. package/src/prover/isabelle/pure/search.ts +3 -10
  131. package/src/prover/isabelle/pure/seq.ts +1 -10
  132. package/src/prover/isabelle/pure/simplifier.ts +4 -12
  133. package/src/prover/isabelle/pure/table.ts +3 -32
  134. package/src/prover/isabelle/pure/tactic.ts +12 -22
  135. package/src/prover/isabelle/pure/tactical.ts +48 -47
  136. package/src/prover/isabelle/pure/term-ord.ts +2 -6
  137. package/src/prover/isabelle/pure/term.ts +2 -43
  138. package/src/prover/isabelle/pure/thm.ts +27 -47
  139. package/src/prover/isabelle/pure/type.ts +2 -15
  140. package/src/prover/isabelle/pure/unify.ts +20 -30
  141. package/src/prover/isabelle/pure/variable.ts +3 -7
  142. package/src/prover/isabelle/tla/classical-setup.ts +17 -24
  143. package/src/prover/isabelle/tla/context.ts +15 -18
  144. package/src/prover/isabelle/tla/logic.ts +14 -32
  145. package/src/prover/isabelle/tla/simpdata.ts +46 -43
  146. package/src/prover/isabelle/tools/intuitionistic.ts +1 -10
  147. package/src/prover/ocaml.ts +0 -6
  148. package/src/prover/prep/action.ts +14 -54
  149. package/src/prover/prep/enabled-axioms.ts +12 -19
  150. package/src/prover/prep/fingerprint.ts +2 -21
  151. package/src/prover/prep/level-comparison.ts +8 -12
  152. package/src/prover/prep/md5.ts +0 -2
  153. package/src/prover/prep/methods.ts +33 -39
  154. package/src/prover/prep/normalize.ts +4 -28
  155. package/src/prover/prove-worker.ts +0 -2
  156. package/src/prover/prove.ts +87 -100
  157. package/src/prover/ptl/decide.ts +7 -12
  158. package/src/prover/ptl/encode.ts +14 -36
  159. package/src/prover/ptl/sat.ts +24 -32
  160. package/src/prover/ptl/tableau.ts +12 -26
  161. package/src/prover/smt/axioms.ts +0 -7
  162. package/src/prover/smt/context.ts +20 -28
  163. package/src/prover/smt/data.ts +433 -424
  164. package/src/prover/smt/encode.ts +43 -52
  165. package/src/prover/smt/extensionality.ts +0 -10
  166. package/src/prover/smt/flatten.ts +3 -22
  167. package/src/prover/smt/format.ts +5 -10
  168. package/src/prover/smt/instances.ts +0 -10
  169. package/src/prover/smt/rewrite.ts +663 -667
  170. package/src/prover/smt/solver/act-cache.ts +0 -15
  171. package/src/prover/smt/solver/arith-rewriter.ts +3 -48
  172. package/src/prover/smt/solver/array.ts +1 -3
  173. package/src/prover/smt/solver/asserted-formulas.ts +1 -43
  174. package/src/prover/smt/solver/ast-lt.ts +1 -11
  175. package/src/prover/smt/solver/ast-util.ts +1 -6
  176. package/src/prover/smt/solver/ast.ts +1 -165
  177. package/src/prover/smt/solver/auf-solver.ts +8 -44
  178. package/src/prover/smt/solver/case-split-queue.ts +1 -9
  179. package/src/prover/smt/solver/cg-table.ts +1 -18
  180. package/src/prover/smt/solver/clause.ts +1 -27
  181. package/src/prover/smt/solver/conflict-resolution.ts +0 -9
  182. package/src/prover/smt/solver/context.ts +7 -64
  183. package/src/prover/smt/solver/ctx-simplify.ts +14 -41
  184. package/src/prover/smt/solver/der.ts +2 -21
  185. package/src/prover/smt/solver/dyn-ack.ts +1 -11
  186. package/src/prover/smt/solver/e-matching.ts +4 -35
  187. package/src/prover/smt/solver/enode.ts +1 -21
  188. package/src/prover/smt/solver/expr-ref.ts +1 -5
  189. package/src/prover/smt/solver/expr-safe-replace.ts +1 -6
  190. package/src/prover/smt/solver/fingerprints.ts +1 -12
  191. package/src/prover/smt/solver/flat-literals.ts +1 -9
  192. package/src/prover/smt/solver/goal.ts +2 -14
  193. package/src/prover/smt/solver/hash.ts +0 -8
  194. package/src/prover/smt/solver/hashtable.ts +1 -12
  195. package/src/prover/smt/solver/internalizer.ts +7 -100
  196. package/src/prover/smt/solver/lbool.ts +1 -2
  197. package/src/prover/smt/solver/literal.ts +0 -3
  198. package/src/prover/smt/solver/lp/bound-propagator.ts +8 -26
  199. package/src/prover/smt/solver/lp/constraints.ts +2 -8
  200. package/src/prover/smt/solver/lp/containers.ts +12 -30
  201. package/src/prover/smt/solver/lp/core-solver.ts +10 -31
  202. package/src/prover/smt/solver/lp/cross-nested.ts +6 -20
  203. package/src/prover/smt/solver/lp/dep-intervals.ts +7 -22
  204. package/src/prover/smt/solver/lp/dependency.ts +1 -4
  205. package/src/prover/smt/solver/lp/dioph-eq.ts +9 -102
  206. package/src/prover/smt/solver/lp/explanation.ts +1 -6
  207. package/src/prover/smt/solver/lp/gomory.ts +9 -22
  208. package/src/prover/smt/solver/lp/hnf-cutter.ts +17 -23
  209. package/src/prover/smt/solver/lp/hnf.ts +11 -26
  210. package/src/prover/smt/solver/lp/int-cube.ts +15 -36
  211. package/src/prover/smt/solver/lp/int-solver.ts +4 -34
  212. package/src/prover/smt/solver/lp/lar-solver.ts +32 -81
  213. package/src/prover/smt/solver/lp/lar-term.ts +1 -5
  214. package/src/prover/smt/solver/lp/lp-settings.ts +1 -14
  215. package/src/prover/smt/solver/lp/maximize-term.ts +20 -45
  216. package/src/prover/smt/solver/lp/nex-creator.ts +5 -28
  217. package/src/prover/smt/solver/lp/nex.ts +1 -6
  218. package/src/prover/smt/solver/lp/nla-backoff.ts +1 -4
  219. package/src/prover/smt/solver/lp/nla-basics.ts +5 -30
  220. package/src/prover/smt/solver/lp/nla-core.ts +36 -135
  221. package/src/prover/smt/solver/lp/nla-divisions.ts +32 -42
  222. package/src/prover/smt/solver/lp/nla-emonics.ts +4 -21
  223. package/src/prover/smt/solver/lp/nla-factorization.ts +1 -4
  224. package/src/prover/smt/solver/lp/nla-grobner.ts +15 -69
  225. package/src/prover/smt/solver/lp/nla-horner.ts +3 -11
  226. package/src/prover/smt/solver/lp/nla-intervals.ts +2 -12
  227. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +16 -41
  228. package/src/prover/smt/solver/lp/nla-monotone.ts +2 -6
  229. package/src/prover/smt/solver/lp/nla-order.ts +2 -13
  230. package/src/prover/smt/solver/lp/nla-patcher.ts +4 -7
  231. package/src/prover/smt/solver/lp/nla-tangents.ts +1 -9
  232. package/src/prover/smt/solver/lp/nla-throttle.ts +1 -6
  233. package/src/prover/smt/solver/lp/nla-types.ts +1 -11
  234. package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -25
  235. package/src/prover/smt/solver/lp/nra-solver.ts +5 -52
  236. package/src/prover/smt/solver/lp/numeric-pair.ts +1 -4
  237. package/src/prover/smt/solver/lp/pdd-interval.ts +1 -3
  238. package/src/prover/smt/solver/lp/pdd-simplifier.ts +2 -14
  239. package/src/prover/smt/solver/lp/pdd-solver.ts +4 -33
  240. package/src/prover/smt/solver/lp/pdd.ts +7 -70
  241. package/src/prover/smt/solver/lp/random-updater.ts +1 -8
  242. package/src/prover/smt/solver/lp/static-matrix.ts +9 -19
  243. package/src/prover/smt/solver/lp/union-find.ts +1 -6
  244. package/src/prover/smt/solver/mam-compiler.ts +18 -58
  245. package/src/prover/smt/solver/mam-interpreter.ts +1 -21
  246. package/src/prover/smt/solver/maximize-ac-sharing.ts +2 -17
  247. package/src/prover/smt/solver/model-checker.ts +3 -37
  248. package/src/prover/smt/solver/model-evaluator.ts +3 -29
  249. package/src/prover/smt/solver/model-finder.ts +15 -32
  250. package/src/prover/smt/solver/model-generator.ts +1 -12
  251. package/src/prover/smt/solver/model-macro-solver.ts +23 -88
  252. package/src/prover/smt/solver/nlsat/algebraic.ts +63 -159
  253. package/src/prover/smt/solver/nlsat/atoms.ts +25 -23
  254. package/src/prover/smt/solver/nlsat/evaluator.ts +17 -22
  255. package/src/prover/smt/solver/nlsat/explain.ts +10 -69
  256. package/src/prover/smt/solver/nlsat/interval-set.ts +9 -50
  257. package/src/prover/smt/solver/nlsat/levelwise.ts +49 -135
  258. package/src/prover/smt/solver/nlsat/mpbq.ts +3 -14
  259. package/src/prover/smt/solver/nlsat/polynomial.ts +29 -159
  260. package/src/prover/smt/solver/nlsat/solver.ts +18 -90
  261. package/src/prover/smt/solver/nlsat/types.ts +1 -8
  262. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +2 -45
  263. package/src/prover/smt/solver/nlsat/upolynomial.ts +11 -92
  264. package/src/prover/smt/solver/nnf.ts +3 -38
  265. package/src/prover/smt/solver/pattern-inference.ts +2 -15
  266. package/src/prover/smt/solver/poly-rewriter.ts +3 -23
  267. package/src/prover/smt/solver/propagate-values.ts +1 -10
  268. package/src/prover/smt/solver/proto-model.ts +3 -52
  269. package/src/prover/smt/solver/pull-quant.ts +1 -12
  270. package/src/prover/smt/solver/qe-lite-der.ts +1 -29
  271. package/src/prover/smt/solver/qe-lite-fm.ts +2 -25
  272. package/src/prover/smt/solver/qe-lite.ts +3 -33
  273. package/src/prover/smt/solver/qi-queue.ts +1 -16
  274. package/src/prover/smt/solver/quant-tactics.ts +5 -22
  275. package/src/prover/smt/solver/quantifier-analyzer.ts +27 -47
  276. package/src/prover/smt/solver/quantifier-manager.ts +2 -13
  277. package/src/prover/smt/solver/random-gen.ts +1 -2
  278. package/src/prover/smt/solver/rational.ts +1 -8
  279. package/src/prover/smt/solver/ref-vector.ts +1 -6
  280. package/src/prover/smt/solver/relevancy.ts +2 -15
  281. package/src/prover/smt/solver/rewriter-tpl.ts +3 -147
  282. package/src/prover/smt/solver/rewriter.ts +3 -85
  283. package/src/prover/smt/solver/rlimit.ts +2 -20
  284. package/src/prover/smt/solver/smtlib-parser.ts +80 -131
  285. package/src/prover/smt/solver/solver.ts +23 -34
  286. package/src/prover/smt/solver/sort.ts +1 -5
  287. package/src/prover/smt/solver/sticky-generations.ts +0 -5
  288. package/src/prover/smt/solver/theory-lra-axioms.ts +3 -24
  289. package/src/prover/smt/solver/theory-lra-bounds.ts +1 -7
  290. package/src/prover/smt/solver/theory-lra-final-check.ts +4 -22
  291. package/src/prover/smt/solver/theory-lra-internalize.ts +6 -43
  292. package/src/prover/smt/solver/theory-lra-model.ts +4 -19
  293. package/src/prover/smt/solver/theory-lra-propagate.ts +1 -15
  294. package/src/prover/smt/solver/theory-lra-state.ts +3 -8
  295. package/src/prover/smt/solver/theory-lra.ts +1 -8
  296. package/src/prover/smt/solver/var-subst.ts +2 -26
  297. package/src/prover/smt/table.ts +0 -8
  298. package/src/prover/tlapm/cache.ts +45 -40
  299. package/src/prover/tlapm/ocaml-marshal.ts +19 -21
  300. package/src/prover/tlapm/tlapm.ts +21 -20
  301. package/src/prover/verdict.ts +7 -2
  302. package/src/prover/zenon/check/check.ts +1 -11
  303. package/src/prover/zenon/check/definitions.ts +5 -15
  304. package/src/prover/zenon/check/equality.ts +0 -8
  305. package/src/prover/zenon/check/functions.ts +0 -4
  306. package/src/prover/zenon/check/keys.ts +0 -7
  307. package/src/prover/zenon/check/propositional.ts +0 -3
  308. package/src/prover/zenon/check/records.ts +1 -6
  309. package/src/prover/zenon/check/relations.ts +2 -14
  310. package/src/prover/zenon/check/rewrites.ts +2 -21
  311. package/src/prover/zenon/check/sets.ts +40 -61
  312. package/src/prover/zenon/check/terms.ts +1 -19
  313. package/src/prover/zenon/check/tla.ts +0 -6
  314. package/src/prover/zenon/decide.ts +0 -12
  315. package/src/prover/zenon/encode.ts +1 -8
  316. package/src/prover/zenon/eqrel.ts +2 -13
  317. package/src/prover/zenon/equality-rules.ts +0 -4
  318. package/src/prover/zenon/expr.ts +8 -26
  319. package/src/prover/zenon/index-table.ts +26 -57
  320. package/src/prover/zenon/isar-case.ts +9 -19
  321. package/src/prover/zenon/isar.ts +36 -64
  322. package/src/prover/zenon/limits.ts +0 -4
  323. package/src/prover/zenon/llproof.ts +2 -20
  324. package/src/prover/zenon/mltoll.ts +66 -104
  325. package/src/prover/zenon/node.ts +1 -11
  326. package/src/prover/zenon/ocaml.ts +1 -17
  327. package/src/prover/zenon/parse.ts +4 -12
  328. package/src/prover/zenon/phrase.ts +1 -11
  329. package/src/prover/zenon/proof.ts +0 -9
  330. package/src/prover/zenon/propositional-rules.ts +0 -5
  331. package/src/prover/zenon/rules.ts +2 -6
  332. package/src/prover/zenon/search.ts +5 -31
  333. package/src/prover/zenon/tla/functions.ts +59 -31
  334. package/src/prover/zenon/tla/llproof.ts +67 -76
  335. package/src/prover/zenon/tla/records.ts +57 -33
  336. package/src/prover/zenon/tla/rewrites.ts +12 -19
  337. package/src/prover/zenon/tla/sets.ts +83 -53
  338. package/src/prover/zenon/tla/terms.ts +0 -6
  339. package/src/prover/zenon/tla/tla.ts +66 -64
  340. package/src/randomization.ts +81 -35
  341. package/src/resolve.ts +67 -184
  342. package/src/runtime.ts +22 -22
  343. package/src/sany/builtins.ts +2 -5
  344. package/src/sany/generation.ts +394 -0
  345. package/src/sany/generator-def-entries.ts +210 -0
  346. package/src/sany/generator-definitions.ts +686 -0
  347. package/src/sany/generator-expressions.ts +1100 -0
  348. package/src/sany/generator-proofs.ts +662 -0
  349. package/src/sany/generator.ts +191 -2764
  350. package/src/sany/levels.ts +6 -34
  351. package/src/sany/lexer.ts +58 -77
  352. package/src/sany/linter.ts +1 -9
  353. package/src/sany/modules.ts +1 -5
  354. package/src/sany/nodes.ts +5 -43
  355. package/src/sany/parse-error.ts +39 -117
  356. package/src/sany/report.ts +32 -69
  357. package/src/sany/selectors.ts +49 -76
  358. package/src/sany/source.ts +1 -17
  359. package/src/sany/xml.ts +35 -117
  360. package/src/search.ts +541 -163
  361. package/src/simulate.ts +709 -0
  362. package/src/spec-description.ts +192 -100
  363. package/src/standard-definitions.ts +0 -4
  364. package/src/standard-modules.ts +264 -95
  365. package/src/state-graph.ts +52 -31
  366. package/src/subexpressions.ts +69 -52
  367. package/src/substitution-levels.ts +7 -7
  368. package/src/successors.ts +266 -99
  369. package/src/symmetry.ts +33 -25
  370. package/src/tableau.ts +27 -24
  371. package/src/tlc/fingerprint.ts +183 -67
  372. package/src/tlc/jar-data.ts +112 -6
  373. package/src/tlc/messages.ts +125 -54
  374. package/src/tlc/order.ts +39 -69
  375. package/src/tlc/random.ts +37 -49
  376. package/src/tlc/replay.ts +553 -228
  377. package/src/tlc/summary.ts +36 -47
  378. package/src/tlc/tlc-operators.ts +78 -44
  379. package/src/tlc/tlcext-operators.ts +139 -122
  380. package/src/tlc/trace-spec.ts +249 -0
  381. package/src/tlc/value-stream.ts +139 -0
  382. package/src/tlc/version.ts +1 -4
  383. package/src/transfer.ts +22 -25
  384. package/src/typescript/actions.ts +479 -0
  385. package/src/typescript/collections.ts +614 -0
  386. package/src/typescript/compile.ts +63 -0
  387. package/src/typescript/context.ts +94 -0
  388. package/src/typescript/emit.ts +121 -0
  389. package/src/typescript/expressions.ts +496 -0
  390. package/src/typescript/formula.ts +154 -0
  391. package/src/typescript/functions.ts +284 -0
  392. package/src/typescript/model.ts +162 -0
  393. package/src/typescript/names.ts +114 -0
  394. package/src/typescript/program.ts +48 -0
  395. package/src/typescript/properties.ts +139 -0
  396. package/src/typescript/scope.ts +112 -0
  397. package/src/typescript/steps.ts +167 -0
  398. package/src/typescript/types.ts +171 -0
  399. package/src/values.ts +832 -484
  400. package/src/worker.ts +7 -8
@@ -1,14 +1,19 @@
1
1
  // Ported from Z3 (src/nlsat/nlsat_solver.cpp, src/nlsat/nlsat_scoped_literal_vector.h,
2
- // src/util/id_gen.h), MIT license, see z3-license.txt. The atoms of nlsat, hash-consed, with
3
- // reference counts. An atom whose count drops to zero is deleted and its Boolean variable reused,
4
- // last freed first, so literals are numbered as in Z3. Boolean variable 0 is true and has no atom.
2
+ // src/util/id_gen.h), MIT license, see z3-license.txt.
5
3
  import {
6
4
  NULL_VAR,
7
5
  type Polynomial,
8
6
  type PolynomialCache,
9
7
  type PolynomialManager,
10
8
  } from "./polynomial";
11
- import type { Atom, IneqAtom, IneqKind, RootAtom, RootKind } from "./types";
9
+ import {
10
+ type Atom,
11
+ type IneqAtom,
12
+ type IneqKind,
13
+ type RootAtom,
14
+ type RootKind,
15
+ isIneqAtom,
16
+ } from "./types";
12
17
 
13
18
  export const TRUE_BOOL_VAR = 0;
14
19
  export const TRUE_LITERAL = 0;
@@ -18,7 +23,6 @@ const FLIPPED_KIND: Record<IneqKind, IneqKind> = { EQ: "EQ", LT: "GT", GT: "LT"
18
23
 
19
24
  export const flipKind = (kind: IneqKind) => FLIPPED_KIND[kind];
20
25
 
21
- // What the solver keeps per Boolean variable, reset when a variable is made or deleted.
22
26
  interface BoolVarHooks {
23
27
  onMk: (boolVar: number) => void;
24
28
  onDelete: (boolVar: number) => void;
@@ -30,8 +34,7 @@ export class AtomTable {
30
34
  private nextBoolVar = 0;
31
35
  private freeBoolVars: number[] = [];
32
36
  private ineqAtoms = new Map<string, IneqAtom>();
33
- // Keyed by the variable at creation: like Z3's root atom table, renaming the variables leaves the
34
- // key alone.
37
+ // Keyed by the variable at creation: Z3's root atom table also ignores renamed variables.
35
38
  private rootAtoms = new Map<string, RootAtom>();
36
39
  private rootKeys = new Map<number, string>();
37
40
 
@@ -43,7 +46,6 @@ export class AtomTable {
43
46
  this.mkBoolVar();
44
47
  }
45
48
 
46
- // mk_bool_var_core with id_gen: the last freed variable first.
47
49
  mkBoolVar() {
48
50
  const boolVar = this.freeBoolVars.pop() ?? this.nextBoolVar++;
49
51
  this.atoms[boolVar] = null;
@@ -52,21 +54,18 @@ export class AtomTable {
52
54
  return boolVar;
53
55
  }
54
56
 
55
- // degree: the degree of the atom's maximal variable.
56
57
  getDegree(atom: Atom) {
57
- if (!("polynomials" in atom)) return this.pm.degree(atom.polynomial, atom.maxVar);
58
+ if (!isIneqAtom(atom)) return this.pm.degree(atom.polynomial, atom.maxVar);
58
59
  let max = 0;
59
60
  for (const polynomial of atom.polynomials)
60
61
  max = Math.max(max, this.pm.degree(polynomial, atom.maxVar));
61
62
  return max;
62
63
  }
63
64
 
64
- // reinit_cache: each polynomial back in the cache after a reset, and the maximal variables after
65
- // a renaming.
66
65
  reinitCache() {
67
66
  for (const atom of this.atoms) {
68
- if (atom === null || atom === undefined) continue;
69
- if ("polynomials" in atom) {
67
+ if (atom === null) continue;
68
+ if (isIneqAtom(atom)) {
70
69
  let max = 0;
71
70
  for (const polynomial of atom.polynomials) {
72
71
  this.cache.mkUnique(polynomial);
@@ -88,6 +87,15 @@ export class AtomTable {
88
87
  return this.getAtom(literal)?.maxVar ?? NULL_VAR;
89
88
  }
90
89
 
90
+ getMaxVarOfLiterals(literals: readonly number[]) {
91
+ let result = NULL_VAR;
92
+ for (const literal of literals) {
93
+ const atom = this.getAtom(literal);
94
+ if (atom !== null && (result === NULL_VAR || atom.maxVar > result)) result = atom.maxVar;
95
+ }
96
+ return result;
97
+ }
98
+
91
99
  incRef(literal: number) {
92
100
  const boolVar = literal >> 1;
93
101
  if (this.atoms[boolVar] === null || this.atoms[boolVar] === undefined) return;
@@ -99,7 +107,7 @@ export class AtomTable {
99
107
  const atom = this.atoms[boolVar];
100
108
  if (atom === null || atom === undefined) return;
101
109
  if (--this.refCounts[boolVar] > 0) return;
102
- if ("polynomials" in atom) this.ineqAtoms.delete(this.getIneqKey(atom));
110
+ if (isIneqAtom(atom)) this.ineqAtoms.delete(this.getIneqKey(atom));
103
111
  else {
104
112
  this.rootAtoms.delete(this.rootKeys.get(boolVar)!);
105
113
  this.rootKeys.delete(boolVar);
@@ -117,8 +125,6 @@ export class AtomTable {
117
125
  return `${atom.kind},${atom.maxVar},${atom.index},${atom.polynomial.id}`;
118
126
  }
119
127
 
120
- // mk_ineq_atom: each factor sign-normalized and hash-consed, the kind flipped when an odd
121
- // factor changed sign.
122
128
  mkIneqAtom(kind: IneqKind, polynomials: readonly Polynomial[], isEven: readonly boolean[]) {
123
129
  let sign = 1;
124
130
  let maxVar = NULL_VAR;
@@ -129,7 +135,7 @@ export class AtomTable {
129
135
  if (variable > maxVar || maxVar === NULL_VAR) maxVar = variable;
130
136
  return this.cache.mkUnique(normalized);
131
137
  });
132
- const atom: IneqAtom = {
138
+ const atom = {
133
139
  kind: sign < 0 ? flipKind(kind) : kind,
134
140
  boolVar: 0,
135
141
  maxVar,
@@ -145,7 +151,6 @@ export class AtomTable {
145
151
  return atom.boolVar;
146
152
  }
147
153
 
148
- // mk_ineq_literal: true or false for a product of constants.
149
154
  mkIneqLiteral(kind: IneqKind, polynomials: readonly Polynomial[], isEven: readonly boolean[]) {
150
155
  let constant = 1n;
151
156
  let isConst = true;
@@ -173,9 +178,8 @@ export class AtomTable {
173
178
  return 2 * this.mkIneqAtom(kind, polynomials, isEven);
174
179
  }
175
180
 
176
- // mk_root_atom: p sign-normalized, which keeps its roots, and hash-consed.
177
181
  mkRootAtom(kind: RootKind, variable: number, index: number, polynomial: Polynomial) {
178
- const atom: RootAtom = {
182
+ const atom = {
179
183
  kind,
180
184
  boolVar: 0,
181
185
  maxVar: variable,
@@ -193,7 +197,6 @@ export class AtomTable {
193
197
  }
194
198
  }
195
199
 
196
- // scoped_literal_vector: literals that hold a reference to their atoms.
197
200
  export class ScopedLiterals {
198
201
  literals: number[] = [];
199
202
 
@@ -224,7 +227,6 @@ export class ScopedLiterals {
224
227
  }
225
228
  }
226
229
 
227
- // scoped_literal: a literal that holds a reference to its atom.
228
230
  export class ScopedLiteral {
229
231
  literal = TRUE_LITERAL;
230
232
 
@@ -1,13 +1,9 @@
1
- // Ported from Z3 (src/nlsat/nlsat_evaluator.cpp), MIT license, see z3-license.txt. Evaluates atoms
2
- // at the assignment, and computes the intervals of the maximal variable of an atom that falsify a
3
- // literal at the values of the variables below it. The assignment is the solver's, shared and
4
- // refined in place.
1
+ // Ported from Z3 (src/nlsat/nlsat_evaluator.cpp), MIT license, see z3-license.txt.
5
2
  import type { Anum, AlgebraicManager } from "./algebraic";
6
3
  import { type IntervalSet, IntervalSetManager } from "./interval-set";
7
4
  import type { Polynomial, PolynomialManager } from "./polynomial";
8
5
  import { type Atom, type Clause, type IneqAtom, type RootAtom, isIneqAtom } from "./types";
9
6
 
10
- // Z3's LINEAR_SEARCH_THRESHOLD.
11
7
  const LINEAR_SEARCH_THRESHOLD = 8;
12
8
 
13
9
  interface Section {
@@ -21,8 +17,6 @@ interface PolynomialInfo {
21
17
  firstSign: number;
22
18
  }
23
19
 
24
- // sign_table: the roots of the factors of an atom merged into one sorted list of sections. Cell 2i
25
- // is the sector below sorted section i, cell 2i + 1 the section itself.
26
20
  class SignTable {
27
21
  private sections: Section[] = [];
28
22
  private sortedSections: number[] = [];
@@ -55,9 +49,9 @@ class SignTable {
55
49
  section.position = sorted.length - 1;
56
50
  sortedIndex++;
57
51
  } else {
58
- const id = this.mkSection(roots[rootIndex], sorted.length);
59
- sorted.push(id);
60
- sectionIds.push(id);
52
+ const newSectionId = this.mkSection(roots[rootIndex], sorted.length);
53
+ sorted.push(newSectionId);
54
+ sectionIds.push(newSectionId);
61
55
  rootIndex++;
62
56
  }
63
57
  }
@@ -66,9 +60,9 @@ class SignTable {
66
60
  sorted.push(this.sortedSections[sortedIndex]);
67
61
  }
68
62
  for (; rootIndex < roots.length; rootIndex++) {
69
- const id = this.mkSection(roots[rootIndex], sorted.length);
70
- sorted.push(id);
71
- sectionIds.push(id);
63
+ const newSectionId = this.mkSection(roots[rootIndex], sorted.length);
64
+ sorted.push(newSectionId);
65
+ sectionIds.push(newSectionId);
72
66
  }
73
67
  this.sortedSections = sorted;
74
68
  return sectionIds;
@@ -114,7 +108,6 @@ class SignTable {
114
108
  return this.polynomialSigns[info.firstSign + index];
115
109
  }
116
110
 
117
- // sign_at: the sign of the infoId-th polynomial in the cell.
118
111
  signAt(infoId: number, cell: number) {
119
112
  const info = this.infos[infoId];
120
113
  const rootCount = info.rootCount;
@@ -215,7 +208,6 @@ export class Evaluator {
215
208
  table.add(signed.roots, signed.signs);
216
209
  }
217
210
 
218
- // The maximal runs of cells where the literal is false, as a union of intervals.
219
211
  private getIneqInfeasibleIntervals(atom: IneqAtom, isNeg: boolean, clause: Clause | null) {
220
212
  const table = new SignTable(this.am);
221
213
  for (const polynomial of atom.polynomials) this.addToTable(polynomial, atom.maxVar, table);
@@ -279,7 +271,7 @@ export class Evaluator {
279
271
  if (atom.index > roots.length)
280
272
  return isNeg ? null : this.ism.mk(true, null, true, null, justification, clause);
281
273
  const root = roots[atom.index - 1];
282
- const mk = (
274
+ const mkInterval = (
283
275
  isLowerOpen: boolean,
284
276
  lower: Anum | null,
285
277
  isUpperOpen: boolean,
@@ -288,16 +280,19 @@ export class Evaluator {
288
280
  switch (atom.kind) {
289
281
  case "ROOT_EQ":
290
282
  return isNeg
291
- ? mk(false, root, false, root)
292
- : this.ism.mkUnion(mk(true, null, true, root), mk(true, root, true, null));
283
+ ? mkInterval(false, root, false, root)
284
+ : this.ism.mkUnion(
285
+ mkInterval(true, null, true, root),
286
+ mkInterval(true, root, true, null),
287
+ );
293
288
  case "ROOT_LT":
294
- return isNeg ? mk(true, null, true, root) : mk(false, root, true, null);
289
+ return isNeg ? mkInterval(true, null, true, root) : mkInterval(false, root, true, null);
295
290
  case "ROOT_GT":
296
- return isNeg ? mk(true, root, true, null) : mk(true, null, false, root);
291
+ return isNeg ? mkInterval(true, root, true, null) : mkInterval(true, null, false, root);
297
292
  case "ROOT_LE":
298
- return isNeg ? mk(true, null, false, root) : mk(true, root, true, null);
293
+ return isNeg ? mkInterval(true, null, false, root) : mkInterval(true, root, true, null);
299
294
  case "ROOT_GE":
300
- return isNeg ? mk(false, root, true, null) : mk(true, null, true, root);
295
+ return isNeg ? mkInterval(false, root, true, null) : mkInterval(true, null, true, root);
301
296
  }
302
297
  }
303
298
  }
@@ -1,9 +1,4 @@
1
- // Ported from Z3 (src/nlsat/nlsat_explain.cpp), MIT license, see z3-license.txt. Explains a set of
2
- // literals that is infeasible at the sample: the literals of a lemma that exclude a cell around
3
- // the sample in which the set stays infeasible. Only Z3's default nlsat parameters are ported:
4
- // simplify_conflicts and factor on, minimize_conflicts, add_all_coeffs and zero_disc off, and lws
5
- // on. So the cell always comes from the levelwise construction, and Z3's fallback projection
6
- // (project with cac_add_cell_lits, add_lcs and the psc_* helpers) is not ported.
1
+ // Ported from Z3 (src/nlsat/nlsat_explain.cpp), MIT license, see z3-license.txt.
7
2
  import type { AlgebraicManager, Anum } from "./algebraic";
8
3
  import {
9
4
  type AtomTable,
@@ -23,22 +18,15 @@ import {
23
18
  } from "./polynomial";
24
19
  import { type IneqAtom, type IneqKind, type RootKind, isIneqAtom } from "./types";
25
20
 
26
- // The solver state an explanation reads.
27
21
  interface ExplainContext {
28
22
  atoms: AtomTable;
29
- // sample(): the solver's assignment, refined in place.
30
23
  sample: (Anum | undefined)[];
31
- // value(l): l_true, l_false or l_undef, as 1, -1 and 0.
32
24
  getValue: (literal: number) => number;
33
- // m_var2eq: the equation of least degree assigned for each variable.
34
25
  var2eq: (IneqAtom | null)[];
35
26
  }
36
27
 
37
- // add_all_coeffs_restart: every coefficient of a polynomial vanished, so the explanation starts
38
- // over adding all coefficients.
39
28
  class AddAllCoeffsRestart {}
40
29
 
41
- // eq_info: an equation used to rewrite the core, and what its leading coefficient must satisfy.
42
30
  interface EqInfo {
43
31
  eq: Polynomial;
44
32
  variable: number;
@@ -79,8 +67,6 @@ export class Explain {
79
67
  private core: ScopedLiterals;
80
68
  private isAddAllCoeffs = false;
81
69
 
82
- // m_full_dimensional: the solver sets it when every clause is full dimensional, and the cell
83
- // bounds are then closed.
84
70
  isFullDimensional = false;
85
71
 
86
72
  constructor(
@@ -119,31 +105,11 @@ export class Explain {
119
105
  });
120
106
  }
121
107
 
122
- private getMaxVarOfPolys(polynomials: readonly Polynomial[]) {
123
- let result = NULL_VAR;
124
- for (const polynomial of polynomials) {
125
- const variable = this.pm.maxVar(polynomial);
126
- if (result === NULL_VAR || variable > result) result = variable;
127
- }
128
- return result;
129
- }
130
-
131
- private getMaxVarOfLiterals(literals: readonly number[]) {
132
- let result = NULL_VAR;
133
- for (const literal of literals) {
134
- const atom = this.context.atoms.getAtom(literal);
135
- if (atom !== null && (result === NULL_VAR || atom.maxVar > result)) result = atom.maxVar;
136
- }
137
- return result;
138
- }
139
-
140
- // add_assumption: the negation of p kind 0, or of p != 0 when isNeg, into the lemma.
141
108
  private addAssumption(kind: IneqKind, polynomial: Polynomial, isNeg = false) {
142
109
  const boolVar = this.context.atoms.mkIneqAtom(kind, [polynomial], [false]);
143
110
  this.addLiteral(2 * boolVar + (isNeg ? 0 : 1));
144
111
  }
145
112
 
146
- // find_cell_roots for one polynomial: the root equal to the sample of its maximal variable.
147
113
  private findSectionRoot(polynomial: Polynomial, variable: number) {
148
114
  const value = this.context.sample[variable]!;
149
115
  const values = this.context.sample.map((current, index) =>
@@ -158,8 +124,6 @@ export class Explain {
158
124
  return null;
159
125
  }
160
126
 
161
- // add_zero_assumption: p vanishes at the sample, so the sample lies on a root of its square-free
162
- // part, which becomes a section literal.
163
127
  private addZeroAssumption(polynomial: Polynomial) {
164
128
  const squareFree = this.pm.squareFree(polynomial);
165
129
  if (this.pm.isConst(squareFree)) return;
@@ -168,8 +132,6 @@ export class Explain {
168
132
  if (index !== null) this.addRootLiteral("ROOT_EQ", variable, index, squareFree);
169
133
  }
170
134
 
171
- // elim_vanishing: p without its leading coefficients that vanish at the sample, each of which
172
- // becomes an assumption. Zero when all do.
173
135
  private elimVanishing(polynomial: Polynomial) {
174
136
  let p = polynomial;
175
137
  let variable = this.pm.maxVar(p);
@@ -181,8 +143,7 @@ export class Explain {
181
143
  degree = this.pm.degree(p, variable);
182
144
  }
183
145
  if (this.pm.isNonzeroConstCoeff(p, variable, degree)) return p;
184
- // Z3 passes lc to insert_fresh_factors_in_todo by reference, so from here on lc is without
185
- // its own vanishing coefficients, which equals it given the assumptions that removed them.
146
+ // Z3's insert_fresh_factors_in_todo writes lc back without its vanishing coefficients.
186
147
  const lc = this.insertFreshFactorsInTodo(this.pm.coeff(p, variable, degree));
187
148
  const reduct = this.pm.reduct(p, variable, degree);
188
149
  if (!this.pm.isZero(lc) && this.getSign(lc) !== 0) return p;
@@ -206,8 +167,6 @@ export class Explain {
206
167
  .filter((polynomial) => !this.pm.isConst(polynomial));
207
168
  }
208
169
 
209
- // insert_fresh_factors_in_todo: the factors of p, without their vanishing coefficients. Returns
210
- // p without its vanishing coefficients, which Z3 writes back to its argument.
211
170
  private insertFreshFactorsInTodo(polynomial: Polynomial) {
212
171
  if (this.pm.isConst(polynomial)) return polynomial;
213
172
  const p = this.elimVanishing(polynomial);
@@ -219,8 +178,6 @@ export class Explain {
219
178
  return p;
220
179
  }
221
180
 
222
- // normalize: the literal without the vanishing coefficients of max and the factors of lower
223
- // stages, which become assumptions. True or false when nothing is left.
224
181
  private normalizeLiteral(literal: number, max: number) {
225
182
  if (literal >> 1 === TRUE_BOOL_VAR) return literal;
226
183
  const atom = this.getAtom(literal);
@@ -272,8 +229,6 @@ export class Explain {
272
229
  core.shrink(kept);
273
230
  }
274
231
 
275
- // simplify(l, info, max, new_lit): rewrites the factors of degree at least the equation's by
276
- // their pseudo-remainders, recording the condition on the equation's leading coefficient.
277
232
  private simplifyLiteral(literal: number, info: EqInfo, max: number, newLiteral: ScopedLiteral) {
278
233
  const atom = this.getAtom(literal);
279
234
  if (!isIneqAtom(atom) || (atom.polynomials.length === 1 && atom.polynomials[0] === info.eq)) {
@@ -336,13 +291,11 @@ export class Explain {
336
291
  newLiteral.set(TRUE_LITERAL);
337
292
  }
338
293
 
339
- // simplify(C, eq, max): the core rewritten by eq. Like Z3's scoped_literal, newLiteral keeps a
340
- // reference until the next literal or the end, also when an elimination restarts.
341
294
  private simplifyWithEq(core: ScopedLiterals, eq: Polynomial, max: number) {
342
295
  const variable = this.pm.maxVar(eq);
343
296
  const degree = this.pm.degree(eq, variable);
344
297
  const lc = this.pm.coeff(eq, variable, degree);
345
- const info: EqInfo = {
298
+ const info = {
346
299
  eq,
347
300
  variable,
348
301
  degree,
@@ -382,7 +335,6 @@ export class Explain {
382
335
  }
383
336
  }
384
337
 
385
- // select_eq: the positive single-factor odd equation of least degree in max.
386
338
  private selectEq(core: ScopedLiterals, max: number) {
387
339
  let result: Polynomial | null = null;
388
340
  let minDegree = Infinity;
@@ -401,8 +353,6 @@ export class Explain {
401
353
  return result;
402
354
  }
403
355
 
404
- // select_lower_stage_eq: an assigned equation of a lower variable with a constant leading
405
- // coefficient that can rewrite a literal of the core.
406
356
  private selectLowerStageEq(core: ScopedLiterals, max: number) {
407
357
  for (const literal of core.literals) {
408
358
  const atom = this.getAtom(literal);
@@ -436,8 +386,6 @@ export class Explain {
436
386
  }
437
387
  }
438
388
 
439
- // add_root_literal: the negation of y kind root[i](p), as an inequality when p is linear or
440
- // quadratic in y, else as a root atom.
441
389
  private addRootLiteral(kind: RootKind, variable: number, index: number, polynomial: Polynomial) {
442
390
  if (this.mkLinearRoot(kind, variable, polynomial)) return;
443
391
  if (this.mkQuadraticRoot(kind, variable, index, polynomial)) return;
@@ -457,7 +405,6 @@ export class Explain {
457
405
  this.addAssumption(ineq.kind, isNegated ? this.pm.neg(polynomial) : polynomial, ineq.isNeg);
458
406
  }
459
407
 
460
- // mk_plinear_root: a root of p linear in y whose coefficient keeps its sign.
461
408
  private mkPlinearRoot(kind: RootKind, variable: number, polynomial: Polynomial) {
462
409
  if (this.pm.degree(polynomial, variable) !== 1) return false;
463
410
  const coeff = this.pm.coeff(polynomial, variable, 1);
@@ -468,8 +415,6 @@ export class Explain {
468
415
  return true;
469
416
  }
470
417
 
471
- // mk_quadratic_root: by Thom's lemma, the signs of the discriminant, the leading coefficient, the
472
- // derivative and p characterize the sample's position among the roots of a quadratic.
473
418
  private mkQuadraticRoot(kind: RootKind, variable: number, index: number, polynomial: Polynomial) {
474
419
  if (this.pm.degree(polynomial, variable) !== 2 || (index !== 1 && index !== 2)) return false;
475
420
  const a = this.pm.coeff(polynomial, variable, 2);
@@ -501,7 +446,6 @@ export class Explain {
501
446
  return sign;
502
447
  }
503
448
 
504
- // levelwise_single_cell: the literals that bound the cell.
505
449
  private addCellLiterals(polynomials: readonly Polynomial[], maxVar: number, isLinear: boolean) {
506
450
  const cell = getSingleCell(
507
451
  this.pm,
@@ -536,7 +480,6 @@ export class Explain {
536
480
  }
537
481
  }
538
482
 
539
- // project: the factors of the polynomials, then the cell around the sample.
540
483
  private project(polynomials: readonly Polynomial[], maxVar: number) {
541
484
  if (polynomials.length === 0) return;
542
485
  this.todo.reset();
@@ -547,28 +490,27 @@ export class Explain {
547
490
  private explainCore(literals: readonly number[]) {
548
491
  if (literals.length === 0) return;
549
492
  const polynomials = [...this.collectPolys(literals), ...this.lowerStagePolys];
550
- const maxVar = this.getMaxVarOfPolys(polynomials);
493
+ const maxVar = this.pm.getMaxVarOfPolys(polynomials);
551
494
  this.project(this.elimVanishingAll(polynomials), maxVar);
552
495
  }
553
496
 
554
- // process2: with simplify_conflicts, the core normalized and rewritten by its equations first.
555
- // As in Z3, a restart leaves the core holding its literals until the next attempt.
556
497
  private process(literals: readonly number[]) {
557
498
  this.lowerStagePolys = [];
558
499
  const core = this.core;
559
500
  core.reset();
560
501
  for (const literal of literals) core.push(literal);
561
- const max = this.getMaxVarOfLiterals(literals);
502
+ const max = this.context.atoms.getMaxVarOfLiterals(literals);
562
503
  this.normalizeCore(core, max);
563
504
  this.simplifyCore(core, max);
564
505
  this.explainCore(core.literals);
565
506
  core.reset();
566
507
  }
567
508
 
568
- // compute_conflict_explanation: the literals, appended to result, that with the negations of
569
- // literals make a valid lemma.
570
509
  computeConflictExplanation(literals: readonly number[], result: ScopedLiterals) {
571
- if (this.getMaxVarOfLiterals(literals) === 0 && this.context.sample[0] === undefined) {
510
+ if (
511
+ this.context.atoms.getMaxVarOfLiterals(literals) === 0 &&
512
+ this.context.sample[0] === undefined
513
+ ) {
572
514
  result.reset();
573
515
  return;
574
516
  }
@@ -592,14 +534,13 @@ export class Explain {
592
534
  }
593
535
  }
594
536
 
595
- // compute_linear_explanation: the literals of a linear cell around a sample of every variable.
596
537
  computeLinearExplanation(literals: readonly number[], result: ScopedLiterals) {
597
538
  this.result = result;
598
539
  this.lowerStagePolys = [];
599
540
  const polynomials = this.collectPolys(literals);
600
541
  try {
601
542
  if (polynomials.length > 0)
602
- this.addCellLiterals(polynomials, this.getMaxVarOfPolys(polynomials) + 1, true);
543
+ this.addCellLiterals(polynomials, this.pm.getMaxVarOfPolys(polynomials) + 1, true);
603
544
  } finally {
604
545
  this.resetResult();
605
546
  }
@@ -1,26 +1,9 @@
1
- // Ported from Z3 (src/nlsat/nlsat_interval_set.cpp), MIT license, see z3-license.txt. Sets of
2
- // disjoint infeasible intervals of the values of one variable, each justified by the literal that
3
- // excludes it. A null set is empty. The bounds are copies of the numbers they were built from, so
4
- // refining a bound while comparing it narrows only that copy, as in Z3.
5
- //
6
- // A union drops the clauses of the intervals it copies, as Z3's push_back does, and this is
7
- // sound. An interval's clause is read only by get_justifications, for R_propagate's lazy
8
- // justification (nlsat_solver.cpp). Its literals come from the justifications alone, and the lazy
9
- // clause they give is valid in the reals by itself. The clauses are used only for three things:
10
- // - resolve_lazy_justification joins their assumptions into the lemma's. Resolution already
11
- // collects these: each core literal's own justification is resolved, or the literal stays in
12
- // the lemma.
13
- // - update_var2eq skips a lazy justification with clauses. It also skips one with literals, and
14
- // every interval brings a literal.
15
- // - Display.
16
- // So a dropped clause can only shrink an over-approximated assumption set, and never removes a
17
- // literal from an explanation.
1
+ // Ported from Z3 (src/nlsat/nlsat_interval_set.cpp), MIT license, see z3-license.txt.
18
2
  import { RandomGen } from "../random-gen";
19
3
  import { Rational } from "../rational";
20
4
  import { Anum, type AlgebraicManager } from "./algebraic";
21
5
  import type { Clause } from "./types";
22
6
 
23
- // A null bound is infinite, and an infinite end is always open.
24
7
  export interface Interval {
25
8
  isLowerOpen: boolean;
26
9
  lower: Anum | null;
@@ -97,8 +80,6 @@ export class IntervalSetManager {
97
80
  return !first.isUpperOpen && !second.isLowerOpen ? 0 : -1;
98
81
  }
99
82
 
100
- // adjacent: curr ends where next starts and one of them holds the shared point. Both bounds are
101
- // finite.
102
83
  private isAdjacent(current: Interval, next: Interval) {
103
84
  return (
104
85
  this.am.compare(current.upper!, next.lower!) === 0 &&
@@ -109,13 +90,14 @@ export class IntervalSetManager {
109
90
  // push_back drops the clause, so a union only keeps the clauses of a set it returns unchanged.
110
91
  private copyInterval(
111
92
  interval: Interval,
112
- upper = { isOpen: interval.isUpperOpen, value: interval.upper },
93
+ isUpperOpen = interval.isUpperOpen,
94
+ upper = interval.upper,
113
95
  ): Interval {
114
96
  return {
115
97
  isLowerOpen: interval.isLowerOpen,
116
98
  lower: interval.lower === null ? null : this.am.copy(interval.lower),
117
- isUpperOpen: upper.isOpen,
118
- upper: upper.value === null ? null : this.am.copy(upper.value),
99
+ isUpperOpen,
100
+ upper: upper === null ? null : this.am.copy(upper),
119
101
  justification: interval.justification,
120
102
  clause: null,
121
103
  };
@@ -145,15 +127,10 @@ export class IntervalSetManager {
145
127
  if (touchSign < 0) result.push(this.copyInterval(firstInterval));
146
128
  else if (touchSign === 0) {
147
129
  if (lowerSign !== 0)
148
- result.push(
149
- this.copyInterval(firstInterval, { isOpen: true, value: firstInterval.upper }),
150
- );
130
+ result.push(this.copyInterval(firstInterval, true, firstInterval.upper));
151
131
  } else if (lowerSign !== 0)
152
132
  result.push(
153
- this.copyInterval(firstInterval, {
154
- isOpen: !secondInterval.isLowerOpen,
155
- value: secondInterval.lower,
156
- }),
133
+ this.copyInterval(firstInterval, !secondInterval.isLowerOpen, secondInterval.lower),
157
134
  );
158
135
  firstIndex++;
159
136
  }
@@ -166,15 +143,10 @@ export class IntervalSetManager {
166
143
  const touchSign = this.compareUpperLower(secondInterval, firstInterval);
167
144
  if (touchSign < 0) result.push(this.copyInterval(secondInterval));
168
145
  else if (touchSign === 0)
169
- result.push(
170
- this.copyInterval(secondInterval, { isOpen: true, value: secondInterval.upper }),
171
- );
146
+ result.push(this.copyInterval(secondInterval, true, secondInterval.upper));
172
147
  else
173
148
  result.push(
174
- this.copyInterval(secondInterval, {
175
- isOpen: !firstInterval.isLowerOpen,
176
- value: firstInterval.lower,
177
- }),
149
+ this.copyInterval(secondInterval, !firstInterval.isLowerOpen, firstInterval.lower),
178
150
  );
179
151
  secondIndex++;
180
152
  }
@@ -183,7 +155,6 @@ export class IntervalSetManager {
183
155
  result.push(this.copyInterval(first.intervals[firstIndex]));
184
156
  for (; secondIndex < second.intervals.length; secondIndex++)
185
157
  result.push(this.copyInterval(second.intervals[secondIndex]));
186
- // Merge adjacent intervals with the same justification.
187
158
  const merged = [result[0]];
188
159
  for (const next of result.slice(1)) {
189
160
  const current = merged[merged.length - 1];
@@ -202,7 +173,6 @@ export class IntervalSetManager {
202
173
  return set !== null && set.isFull;
203
174
  }
204
175
 
205
- // subset: every interval of first lies in a run of adjacent intervals of second.
206
176
  subset(first: IntervalSet | null, second: IntervalSet | null) {
207
177
  if (first === second || first === null) return true;
208
178
  if (second === null) return false;
@@ -237,8 +207,6 @@ export class IntervalSetManager {
237
207
  return firstIndex === first.intervals.length;
238
208
  }
239
209
 
240
- // get_justifications: the distinct justifications in order, and the clauses of the intervals
241
- // that first bring each.
242
210
  getJustifications(set: IntervalSet | null) {
243
211
  const literals: number[] = [];
244
212
  const clauses: Clause[] = [];
@@ -250,10 +218,6 @@ export class IntervalSetManager {
250
218
  return { literals, clauses };
251
219
  }
252
220
 
253
- // pick_in_complement: zero when it is outside the set. Otherwise an integer above the last
254
- // interval, an integer below the first, or a number in a gap, picked uniformly at random among
255
- // them when randomizing, else the first found. With no gap, the shared open end of two
256
- // intervals, preferring a rational one. Z3 also takes is_int, and ignores it.
257
221
  pickInComplement(set: IntervalSet | null, isRandomized: boolean) {
258
222
  if (set === null) return new Anum(Rational.zero);
259
223
  const zero = new Anum(Rational.zero);
@@ -301,7 +265,6 @@ export class IntervalSetManager {
301
265
  return this.am.copy(irrational!);
302
266
  }
303
267
 
304
- // compare_interval_with_zero: -1 when the interval lies below zero, 1 above, 0 when it holds it.
305
268
  private compareIntervalWithZero(interval: Interval, zero: Anum) {
306
269
  if (interval.upper !== null) {
307
270
  const sign = this.am.compare(interval.upper, zero);
@@ -314,8 +277,6 @@ export class IntervalSetManager {
314
277
  return 0;
315
278
  }
316
279
 
317
- // find_complement_sup: the interval whose lower bound is the supremum of the complement, when
318
- // the set reaches +oo and the complement is not empty.
319
280
  private findComplementSup(set: IntervalSet | null) {
320
281
  if (set === null || set.intervals[set.intervals.length - 1].upper !== null) return null;
321
282
  for (let index = set.intervals.length - 1; ; index--) {
@@ -331,7 +292,6 @@ export class IntervalSetManager {
331
292
  }
332
293
  }
333
294
 
334
- // pick_max_in_complement: the largest number of the complement, or a number just below it.
335
295
  pickMaxInComplement(set: IntervalSet | null) {
336
296
  const index = this.findComplementSup(set);
337
297
  if (index === null) return null;
@@ -341,7 +301,6 @@ export class IntervalSetManager {
341
301
  return this.am.select(set!.intervals[index - 1].upper!, current.lower!);
342
302
  }
343
303
 
344
- // query_max_in_complement: the supremum of the complement, and whether it belongs to it.
345
304
  queryMaxInComplement(set: IntervalSet | null): ComplementMax | null {
346
305
  const index = this.findComplementSup(set);
347
306
  if (index === null) return null;