tla-ts 0.0.9 → 0.0.10-dev.094b9b512

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 (622) hide show
  1. package/README.md +30 -1
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +40 -37
  5. package/dist/deciders.js +47 -44
  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 +2658 -410
  15. package/dist/prove.js +2657 -409
  16. package/dist/run.cjs +144 -31
  17. package/dist/run.js +144 -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 +6 -6
  23. package/dist/worker.js +6 -6
  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 +208 -54
  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 +100 -57
  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 +83 -101
  63. package/src/liveness-checker.ts +103 -153
  64. package/src/liveness.ts +150 -109
  65. package/src/obligations/action.ts +18 -43
  66. package/src/obligations/anonymize.ts +119 -90
  67. package/src/obligations/enabled-axioms.ts +218 -0
  68. package/src/obligations/equality.ts +2 -2
  69. package/src/obligations/expression.ts +38 -25
  70. package/src/obligations/format.ts +1 -9
  71. package/src/obligations/generate.ts +67 -63
  72. package/src/obligations/index.ts +38 -20
  73. package/src/obligations/levels.ts +28 -17
  74. package/src/obligations/module.ts +29 -54
  75. package/src/obligations/normalize.ts +31 -36
  76. package/src/obligations/operators.ts +1 -2
  77. package/src/obligations/pragma.ts +53 -25
  78. package/src/obligations/printer.ts +90 -57
  79. package/src/obligations/proof.ts +4 -5
  80. package/src/obligations/regroup.ts +1 -4
  81. package/src/obligations/simplify.ts +58 -62
  82. package/src/obligations/source.ts +119 -95
  83. package/src/obligations/subexpression.ts +51 -27
  84. package/src/obligations/substitution.ts +2 -4
  85. package/src/obligations/temporal.ts +13 -19
  86. package/src/obligations/tuples.ts +3 -9
  87. package/src/parallel-worker.ts +30 -60
  88. package/src/parallel.ts +57 -71
  89. package/src/parser.ts +961 -387
  90. package/src/pcal/ast.ts +53 -86
  91. package/src/pcal/parse.ts +144 -219
  92. package/src/pcal/tokenize.ts +91 -131
  93. package/src/pcal/translate.ts +83 -130
  94. package/src/pcal/validate.ts +48 -68
  95. package/src/pretty-print.ts +7 -8
  96. package/src/prover/decide-pool.ts +8 -13
  97. package/src/prover/deciders.ts +19 -13
  98. package/src/prover/first-order.ts +0 -11
  99. package/src/prover/isabelle/decide.ts +3 -31
  100. package/src/prover/isabelle/encode.ts +82 -62
  101. package/src/prover/isabelle/isar/attrib.ts +219 -0
  102. package/src/prover/isabelle/isar/context.ts +336 -0
  103. package/src/prover/isabelle/isar/induct.ts +392 -0
  104. package/src/prover/isabelle/isar/inner-syntax.ts +262 -117
  105. package/src/prover/isabelle/isar/method.ts +513 -225
  106. package/src/prover/isabelle/isar/proof.ts +569 -23
  107. package/src/prover/isabelle/isar/rule-cases.ts +234 -0
  108. package/src/prover/isabelle/isar/theory.ts +490 -89
  109. package/src/prover/isabelle/isar/toplevel.ts +341 -0
  110. package/src/prover/isabelle/isar/type-check.ts +100 -37
  111. package/src/prover/isabelle/provers/blast/blast.ts +17 -104
  112. package/src/prover/isabelle/provers/blast/branch.ts +1 -32
  113. package/src/prover/isabelle/provers/blast/prove.ts +1 -35
  114. package/src/prover/isabelle/provers/blast/rules.ts +6 -44
  115. package/src/prover/isabelle/provers/blast/state.ts +2 -28
  116. package/src/prover/isabelle/provers/blast/term.ts +9 -36
  117. package/src/prover/isabelle/provers/clasimp.ts +1 -13
  118. package/src/prover/isabelle/provers/classical.ts +59 -60
  119. package/src/prover/isabelle/provers/hypsubst.ts +28 -51
  120. package/src/prover/isabelle/pure/bicompose.ts +25 -38
  121. package/src/prover/isabelle/pure/consts.ts +1 -7
  122. package/src/prover/isabelle/pure/conv.ts +3 -17
  123. package/src/prover/isabelle/pure/drule.ts +96 -44
  124. package/src/prover/isabelle/pure/envir.ts +3 -52
  125. package/src/prover/isabelle/pure/goal.ts +2 -8
  126. package/src/prover/isabelle/pure/interrupt.ts +0 -5
  127. package/src/prover/isabelle/pure/list.ts +0 -3
  128. package/src/prover/isabelle/pure/logic.ts +2 -15
  129. package/src/prover/isabelle/pure/name.ts +2 -9
  130. package/src/prover/isabelle/pure/net.ts +3 -59
  131. package/src/prover/isabelle/pure/object-logic.ts +16 -15
  132. package/src/prover/isabelle/pure/pattern.ts +2 -19
  133. package/src/prover/isabelle/pure/raw-simplifier.ts +17 -62
  134. package/src/prover/isabelle/pure/rewriter.ts +32 -73
  135. package/src/prover/isabelle/pure/search.ts +3 -10
  136. package/src/prover/isabelle/pure/seq.ts +1 -10
  137. package/src/prover/isabelle/pure/simplifier.ts +28 -15
  138. package/src/prover/isabelle/pure/table.ts +3 -32
  139. package/src/prover/isabelle/pure/tactic.ts +12 -22
  140. package/src/prover/isabelle/pure/tactical.ts +48 -47
  141. package/src/prover/isabelle/pure/term-ord.ts +2 -6
  142. package/src/prover/isabelle/pure/term.ts +2 -43
  143. package/src/prover/isabelle/pure/thm.ts +27 -47
  144. package/src/prover/isabelle/pure/type.ts +2 -15
  145. package/src/prover/isabelle/pure/unify.ts +20 -30
  146. package/src/prover/isabelle/pure/variable.ts +3 -7
  147. package/src/prover/isabelle/tla/classical-setup.ts +18 -24
  148. package/src/prover/isabelle/tla/context.ts +65 -26
  149. package/src/prover/isabelle/tla/logic-data.ts +1 -1
  150. package/src/prover/isabelle/tla/logic.ts +53 -32
  151. package/src/prover/isabelle/tla/simpdata.ts +46 -43
  152. package/src/prover/isabelle/tools/intuitionistic.ts +29 -20
  153. package/src/prover/ocaml.ts +0 -6
  154. package/src/prover/prep/action.ts +32 -66
  155. package/src/prover/prep/enabled-axioms.ts +22 -20
  156. package/src/prover/prep/fingerprint.ts +2 -21
  157. package/src/prover/prep/level-comparison.ts +8 -12
  158. package/src/prover/prep/md5.ts +0 -2
  159. package/src/prover/prep/methods.ts +137 -83
  160. package/src/prover/prep/normalize.ts +38 -36
  161. package/src/prover/prove-worker.ts +0 -2
  162. package/src/prover/prove.ts +371 -132
  163. package/src/prover/ptl/decide.ts +104 -44
  164. package/src/prover/ptl/encode.ts +15 -38
  165. package/src/prover/ptl/sat.ts +24 -32
  166. package/src/prover/ptl/tableau.ts +40 -28
  167. package/src/prover/smt/arithmetic.ts +310 -0
  168. package/src/prover/smt/axioms.ts +12 -15
  169. package/src/prover/smt/context.ts +61 -29
  170. package/src/prover/smt/data.ts +497 -387
  171. package/src/prover/smt/encode.ts +156 -85
  172. package/src/prover/smt/extensionality.ts +5 -13
  173. package/src/prover/smt/flatten.ts +7 -28
  174. package/src/prover/smt/format.ts +33 -14
  175. package/src/prover/smt/instances.ts +7 -11
  176. package/src/prover/smt/layout.ts +91 -0
  177. package/src/prover/smt/rewrite.ts +1025 -658
  178. package/src/prover/smt/simplify.ts +441 -0
  179. package/src/prover/smt/solver/ackermannize-bv-tactic.ts +63 -0
  180. package/src/prover/smt/solver/ackr-helper.ts +182 -0
  181. package/src/prover/smt/solver/ackr-model-converter.ts +112 -0
  182. package/src/prover/smt/solver/act-cache.ts +0 -15
  183. package/src/prover/smt/solver/add-bounds-tactic.ts +60 -0
  184. package/src/prover/smt/solver/aig-tactic.ts +43 -0
  185. package/src/prover/smt/solver/aig.ts +890 -0
  186. package/src/prover/smt/solver/arith-rewriter.ts +613 -48
  187. package/src/prover/smt/solver/arith-terms.ts +123 -0
  188. package/src/prover/smt/solver/array-decl-plugin.ts +176 -0
  189. package/src/prover/smt/solver/array-rewriter.ts +312 -0
  190. package/src/prover/smt/solver/array.ts +1 -3
  191. package/src/prover/smt/solver/asserted-formulas.ts +87 -57
  192. package/src/prover/smt/solver/ast-lt.ts +166 -21
  193. package/src/prover/smt/solver/ast-util.ts +161 -7
  194. package/src/prover/smt/solver/ast.ts +1466 -178
  195. package/src/prover/smt/solver/auf-solver.ts +8 -44
  196. package/src/prover/smt/solver/bit-blast-tactic.ts +591 -0
  197. package/src/prover/smt/solver/bit-blaster.ts +874 -0
  198. package/src/prover/smt/solver/bound-manager.ts +186 -0
  199. package/src/prover/smt/solver/bound-simplifier.ts +602 -0
  200. package/src/prover/smt/solver/bv-bound-chk.ts +500 -0
  201. package/src/prover/smt/solver/bv-decl-plugin.ts +339 -0
  202. package/src/prover/smt/solver/bv-divrem-bounds.ts +97 -0
  203. package/src/prover/smt/solver/bv-rewriter.ts +1905 -0
  204. package/src/prover/smt/solver/bv1-blast.ts +224 -0
  205. package/src/prover/smt/solver/bv2fpa-converter.ts +175 -0
  206. package/src/prover/smt/solver/bv2int-rewriter.ts +545 -0
  207. package/src/prover/smt/solver/bv2real-rewriter.ts +534 -0
  208. package/src/prover/smt/solver/card2bv.ts +112 -0
  209. package/src/prover/smt/solver/case-split-queue.ts +1 -9
  210. package/src/prover/smt/solver/cg-table.ts +28 -34
  211. package/src/prover/smt/solver/check-logic.ts +326 -0
  212. package/src/prover/smt/solver/clause.ts +1 -27
  213. package/src/prover/smt/solver/cofactor-elim-term-ite.ts +280 -0
  214. package/src/prover/smt/solver/conflict-resolution.ts +79 -12
  215. package/src/prover/smt/solver/context.ts +510 -175
  216. package/src/prover/smt/solver/ctx-simplify.ts +34 -50
  217. package/src/prover/smt/solver/ctx-solver-simplify.ts +124 -0
  218. package/src/prover/smt/solver/datatype-decl-plugin.ts +575 -0
  219. package/src/prover/smt/solver/datatype-rewriter.ts +83 -0
  220. package/src/prover/smt/solver/demodulator.ts +789 -0
  221. package/src/prover/smt/solver/dependent-expr-state.ts +68 -0
  222. package/src/prover/smt/solver/der.ts +40 -30
  223. package/src/prover/smt/solver/diff-neq.ts +207 -0
  224. package/src/prover/smt/solver/distribute-forall.ts +67 -0
  225. package/src/prover/smt/solver/dom-simplify.ts +513 -0
  226. package/src/prover/smt/solver/dt2bv-tactic.ts +267 -0
  227. package/src/prover/smt/solver/dyn-ack.ts +1 -11
  228. package/src/prover/smt/solver/e-matching.ts +4 -35
  229. package/src/prover/smt/solver/elim-small-bv-tactic.ts +130 -0
  230. package/src/prover/smt/solver/elim-uncnstr.ts +558 -0
  231. package/src/prover/smt/solver/enode.ts +93 -22
  232. package/src/prover/smt/solver/eq2bv-tactic.ts +219 -0
  233. package/src/prover/smt/solver/euf/arith-axioms.ts +404 -0
  234. package/src/prover/smt/solver/euf/arith-internalize.ts +659 -0
  235. package/src/prover/smt/solver/euf/arith-solver.ts +895 -0
  236. package/src/prover/smt/solver/euf/array-axioms.ts +528 -0
  237. package/src/prover/smt/solver/euf/array-internalize.ts +419 -0
  238. package/src/prover/smt/solver/euf/array-solver.ts +308 -0
  239. package/src/prover/smt/solver/euf/bv-ackerman.ts +164 -0
  240. package/src/prover/smt/solver/euf/bv-internalize.ts +750 -0
  241. package/src/prover/smt/solver/euf/bv-solver.ts +451 -0
  242. package/src/prover/smt/solver/euf/dt-solver.ts +620 -0
  243. package/src/prover/smt/solver/euf/egraph.ts +885 -0
  244. package/src/prover/smt/solver/euf/euf-solver.ts +1203 -0
  245. package/src/prover/smt/solver/euf/fpa-solver.ts +333 -0
  246. package/src/prover/smt/solver/euf/mam.ts +1094 -0
  247. package/src/prover/smt/solver/euf/q-ematch.ts +900 -0
  248. package/src/prover/smt/solver/euf/q-mbi.ts +587 -0
  249. package/src/prover/smt/solver/euf/q-model-fixer.ts +299 -0
  250. package/src/prover/smt/solver/euf/q-solver.ts +361 -0
  251. package/src/prover/smt/solver/euf/th-solver.ts +343 -0
  252. package/src/prover/smt/solver/euf/th-solvers.ts +31 -0
  253. package/src/prover/smt/solver/expr-ref.ts +1 -5
  254. package/src/prover/smt/solver/expr-safe-replace.ts +26 -6
  255. package/src/prover/smt/solver/expr2polynomial.ts +147 -0
  256. package/src/prover/smt/solver/factor.ts +152 -0
  257. package/src/prover/smt/solver/fd-solver.ts +269 -0
  258. package/src/prover/smt/solver/fingerprints.ts +16 -26
  259. package/src/prover/smt/solver/fix-dl-var.ts +149 -0
  260. package/src/prover/smt/solver/flat-literals.ts +1 -9
  261. package/src/prover/smt/solver/fm-tactic.ts +873 -0
  262. package/src/prover/smt/solver/fpa-decl-plugin.ts +619 -0
  263. package/src/prover/smt/solver/fpa-rewriter.ts +641 -0
  264. package/src/prover/smt/solver/fpa2bv-converter.ts +2246 -0
  265. package/src/prover/smt/solver/fpa2bv-rewriter.ts +349 -0
  266. package/src/prover/smt/solver/fpa2bv-tactic.ts +75 -0
  267. package/src/prover/smt/solver/goal.ts +177 -28
  268. package/src/prover/smt/solver/goal2nlsat.ts +191 -0
  269. package/src/prover/smt/solver/guard-set.ts +220 -0
  270. package/src/prover/smt/solver/hash.ts +0 -8
  271. package/src/prover/smt/solver/hashtable.ts +27 -23
  272. package/src/prover/smt/solver/inf-eps.ts +64 -0
  273. package/src/prover/smt/solver/inj-axiom.ts +102 -0
  274. package/src/prover/smt/solver/internalizer.ts +72 -130
  275. package/src/prover/smt/solver/lackr.ts +383 -0
  276. package/src/prover/smt/solver/lbool.ts +1 -2
  277. package/src/prover/smt/solver/lia2card.ts +269 -0
  278. package/src/prover/smt/solver/lia2pb.ts +141 -0
  279. package/src/prover/smt/solver/literal.ts +0 -3
  280. package/src/prover/smt/solver/logic-tactics.ts +349 -0
  281. package/src/prover/smt/solver/lp/bound-propagator.ts +8 -26
  282. package/src/prover/smt/solver/lp/constraints.ts +2 -8
  283. package/src/prover/smt/solver/lp/containers.ts +12 -30
  284. package/src/prover/smt/solver/lp/core-solver.ts +24 -27
  285. package/src/prover/smt/solver/lp/cross-nested.ts +6 -20
  286. package/src/prover/smt/solver/lp/dep-intervals.ts +7 -22
  287. package/src/prover/smt/solver/lp/dependency.ts +1 -4
  288. package/src/prover/smt/solver/lp/dioph-eq.ts +9 -102
  289. package/src/prover/smt/solver/lp/explanation.ts +1 -6
  290. package/src/prover/smt/solver/lp/gomory.ts +9 -22
  291. package/src/prover/smt/solver/lp/hnf-cutter.ts +17 -23
  292. package/src/prover/smt/solver/lp/hnf.ts +11 -26
  293. package/src/prover/smt/solver/lp/int-cube.ts +18 -39
  294. package/src/prover/smt/solver/lp/int-solver.ts +4 -34
  295. package/src/prover/smt/solver/lp/lar-solver.ts +70 -82
  296. package/src/prover/smt/solver/lp/lar-term.ts +1 -5
  297. package/src/prover/smt/solver/lp/lp-settings.ts +1 -14
  298. package/src/prover/smt/solver/lp/maximize-term.ts +79 -52
  299. package/src/prover/smt/solver/lp/nex-creator.ts +5 -28
  300. package/src/prover/smt/solver/lp/nex.ts +1 -6
  301. package/src/prover/smt/solver/lp/nla-backoff.ts +1 -4
  302. package/src/prover/smt/solver/lp/nla-basics.ts +5 -30
  303. package/src/prover/smt/solver/lp/nla-core.ts +69 -132
  304. package/src/prover/smt/solver/lp/nla-divisions.ts +32 -42
  305. package/src/prover/smt/solver/lp/nla-emonics.ts +4 -21
  306. package/src/prover/smt/solver/lp/nla-factorization.ts +1 -4
  307. package/src/prover/smt/solver/lp/nla-grobner.ts +24 -72
  308. package/src/prover/smt/solver/lp/nla-horner.ts +3 -11
  309. package/src/prover/smt/solver/lp/nla-intervals.ts +2 -12
  310. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +16 -41
  311. package/src/prover/smt/solver/lp/nla-monotone.ts +2 -6
  312. package/src/prover/smt/solver/lp/nla-order.ts +2 -13
  313. package/src/prover/smt/solver/lp/nla-patcher.ts +4 -7
  314. package/src/prover/smt/solver/lp/nla-powers.ts +184 -0
  315. package/src/prover/smt/solver/lp/nla-tangents.ts +1 -9
  316. package/src/prover/smt/solver/lp/nla-throttle.ts +1 -6
  317. package/src/prover/smt/solver/lp/nla-transcendentals.ts +293 -0
  318. package/src/prover/smt/solver/lp/nla-types.ts +1 -11
  319. package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -25
  320. package/src/prover/smt/solver/lp/nra-solver.ts +30 -54
  321. package/src/prover/smt/solver/lp/numeric-pair.ts +1 -4
  322. package/src/prover/smt/solver/lp/pdd-interval.ts +1 -3
  323. package/src/prover/smt/solver/lp/pdd-simplifier.ts +2 -14
  324. package/src/prover/smt/solver/lp/pdd-solver.ts +4 -33
  325. package/src/prover/smt/solver/lp/pdd.ts +7 -70
  326. package/src/prover/smt/solver/lp/random-updater.ts +1 -8
  327. package/src/prover/smt/solver/lp/static-matrix.ts +9 -19
  328. package/src/prover/smt/solver/lp/union-find.ts +6 -5
  329. package/src/prover/smt/solver/macro-finder.ts +533 -0
  330. package/src/prover/smt/solver/mam-compiler.ts +117 -126
  331. package/src/prover/smt/solver/mam-interpreter.ts +1 -21
  332. package/src/prover/smt/solver/max-bv-sharing-tactic.ts +29 -0
  333. package/src/prover/smt/solver/max-cliques.ts +107 -0
  334. package/src/prover/smt/solver/maxcore.ts +318 -0
  335. package/src/prover/smt/solver/maximize-ac-sharing.ts +9 -20
  336. package/src/prover/smt/solver/maxlex.ts +123 -0
  337. package/src/prover/smt/solver/maxsmt-base.ts +99 -0
  338. package/src/prover/smt/solver/maxsmt.ts +93 -0
  339. package/src/prover/smt/solver/mbp/mbp-arith.ts +477 -0
  340. package/src/prover/smt/solver/mbp/mbp-basic-tg.ts +102 -0
  341. package/src/prover/smt/solver/mbp/mbp-plugin.ts +280 -0
  342. package/src/prover/smt/solver/mbp/mbp-qel.ts +49 -0
  343. package/src/prover/smt/solver/mbp/mbp-solve-plugin.ts +280 -0
  344. package/src/prover/smt/solver/mbp/mbp-term-graph.ts +624 -0
  345. package/src/prover/smt/solver/model-based-opt.ts +845 -0
  346. package/src/prover/smt/solver/model-checker.ts +20 -62
  347. package/src/prover/smt/solver/model-evaluator.ts +186 -39
  348. package/src/prover/smt/solver/model-finder.ts +15 -32
  349. package/src/prover/smt/solver/model-generator.ts +57 -40
  350. package/src/prover/smt/solver/model-macro-solver.ts +58 -111
  351. package/src/prover/smt/solver/model-printer.ts +215 -0
  352. package/src/prover/smt/solver/mpf.ts +712 -0
  353. package/src/prover/smt/solver/mus.ts +52 -0
  354. package/src/prover/smt/solver/muz/dl-context.ts +362 -0
  355. package/src/prover/smt/solver/muz/dl-mk-bit-blast.ts +98 -0
  356. package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +121 -0
  357. package/src/prover/smt/solver/muz/dl-mk-elim-term-ite.ts +121 -0
  358. package/src/prover/smt/solver/muz/dl-mk-interp-tail-simplifier.ts +305 -0
  359. package/src/prover/smt/solver/muz/dl-mk-rule-inliner.ts +427 -0
  360. package/src/prover/smt/solver/muz/dl-mk-slice.ts +278 -0
  361. package/src/prover/smt/solver/muz/dl-mk-subsumption-checker.ts +158 -0
  362. package/src/prover/smt/solver/muz/dl-rule-set.ts +303 -0
  363. package/src/prover/smt/solver/muz/dl-rule-transformer.ts +127 -0
  364. package/src/prover/smt/solver/muz/dl-rule.ts +460 -0
  365. package/src/prover/smt/solver/muz/hnf.ts +201 -0
  366. package/src/prover/smt/solver/muz/horn-tactic.ts +192 -0
  367. package/src/prover/smt/solver/muz/rule-properties.ts +179 -0
  368. package/src/prover/smt/solver/muz/spacer-context.ts +1395 -0
  369. package/src/prover/smt/solver/muz/spacer-dl-interface.ts +41 -0
  370. package/src/prover/smt/solver/muz/spacer-generalizers.ts +244 -0
  371. package/src/prover/smt/solver/muz/spacer-manager.ts +133 -0
  372. package/src/prover/smt/solver/muz/spacer-pdr.ts +219 -0
  373. package/src/prover/smt/solver/muz/spacer-prop-solver.ts +344 -0
  374. package/src/prover/smt/solver/muz/spacer-util.ts +309 -0
  375. package/src/prover/smt/solver/muz/substitution.ts +154 -0
  376. package/src/prover/smt/solver/nla2bv-tactic.ts +286 -0
  377. package/src/prover/smt/solver/nlqsat.ts +484 -0
  378. package/src/prover/smt/solver/nlsat/algebraic.ts +179 -159
  379. package/src/prover/smt/solver/nlsat/atoms.ts +25 -23
  380. package/src/prover/smt/solver/nlsat/evaluator.ts +17 -22
  381. package/src/prover/smt/solver/nlsat/explain.ts +61 -69
  382. package/src/prover/smt/solver/nlsat/interval-set.ts +10 -51
  383. package/src/prover/smt/solver/nlsat/levelwise.ts +49 -135
  384. package/src/prover/smt/solver/nlsat/mpbq.ts +48 -14
  385. package/src/prover/smt/solver/nlsat/polynomial.ts +30 -160
  386. package/src/prover/smt/solver/nlsat/solver.ts +227 -99
  387. package/src/prover/smt/solver/nlsat/transcendental-eval.ts +311 -0
  388. package/src/prover/smt/solver/nlsat/transcendentals.ts +369 -0
  389. package/src/prover/smt/solver/nlsat/types.ts +1 -8
  390. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +2 -45
  391. package/src/prover/smt/solver/nlsat/upolynomial.ts +11 -92
  392. package/src/prover/smt/solver/nlsat-tactic.ts +83 -0
  393. package/src/prover/smt/solver/nnf.ts +108 -60
  394. package/src/prover/smt/solver/normalize-bounds.ts +54 -0
  395. package/src/prover/smt/solver/occf.ts +98 -0
  396. package/src/prover/smt/solver/opt-context.ts +787 -0
  397. package/src/prover/smt/solver/opt-nlsat.ts +366 -0
  398. package/src/prover/smt/solver/opt-pareto.ts +284 -0
  399. package/src/prover/smt/solver/opt-preprocess.ts +102 -0
  400. package/src/prover/smt/solver/opt-solver.ts +300 -0
  401. package/src/prover/smt/solver/opt-value.ts +56 -0
  402. package/src/prover/smt/solver/optsmt.ts +691 -0
  403. package/src/prover/smt/solver/parallel-tactic.ts +301 -0
  404. package/src/prover/smt/solver/pattern-inference.ts +19 -23
  405. package/src/prover/smt/solver/pb-rewriter.ts +296 -0
  406. package/src/prover/smt/solver/pb2bv-tactic.ts +542 -0
  407. package/src/prover/smt/solver/poly-rewriter.ts +203 -77
  408. package/src/prover/smt/solver/probes.ts +514 -0
  409. package/src/prover/smt/solver/propagate-values.ts +34 -30
  410. package/src/prover/smt/solver/proto-model.ts +648 -63
  411. package/src/prover/smt/solver/pull-quant.ts +29 -23
  412. package/src/prover/smt/solver/purify-arith.ts +598 -0
  413. package/src/prover/smt/solver/qe-arith-plugin.ts +1099 -0
  414. package/src/prover/smt/solver/qe-lite-der.ts +3 -31
  415. package/src/prover/smt/solver/qe-lite-fm.ts +5 -28
  416. package/src/prover/smt/solver/qe-lite.ts +40 -49
  417. package/src/prover/smt/solver/qe-mbp.ts +281 -0
  418. package/src/prover/smt/solver/qe.ts +1508 -0
  419. package/src/prover/smt/solver/qel.ts +26 -0
  420. package/src/prover/smt/solver/qi-queue.ts +1 -16
  421. package/src/prover/smt/solver/qsat.ts +840 -0
  422. package/src/prover/smt/solver/quant-hoist.ts +195 -0
  423. package/src/prover/smt/solver/quant-tactics.ts +18 -74
  424. package/src/prover/smt/solver/quantifier-analyzer.ts +27 -47
  425. package/src/prover/smt/solver/quantifier-manager.ts +9 -23
  426. package/src/prover/smt/solver/quasi-macros.ts +254 -0
  427. package/src/prover/smt/solver/random-gen.ts +1 -2
  428. package/src/prover/smt/solver/rational.ts +29 -8
  429. package/src/prover/smt/solver/recfun-decl-plugin.ts +248 -0
  430. package/src/prover/smt/solver/recfun-rewriter.ts +108 -0
  431. package/src/prover/smt/solver/reduce-args.ts +226 -0
  432. package/src/prover/smt/solver/reduce-bv-size.ts +120 -0
  433. package/src/prover/smt/solver/ref-vector.ts +1 -6
  434. package/src/prover/smt/solver/relevancy.ts +3 -16
  435. package/src/prover/smt/solver/rewriter-tpl.ts +49 -129
  436. package/src/prover/smt/solver/rewriter.ts +478 -120
  437. package/src/prover/smt/solver/rlimit.ts +27 -21
  438. package/src/prover/smt/solver/sat/asymm-branch.ts +314 -0
  439. package/src/prover/smt/solver/sat/big.ts +178 -0
  440. package/src/prover/smt/solver/sat/clause-use-list.ts +79 -0
  441. package/src/prover/smt/solver/sat/clause.ts +221 -0
  442. package/src/prover/smt/solver/sat/cleaner.ts +96 -0
  443. package/src/prover/smt/solver/sat/config.ts +125 -0
  444. package/src/prover/smt/solver/sat/consequences.ts +204 -0
  445. package/src/prover/smt/solver/sat/elim-eqs.ts +144 -0
  446. package/src/prover/smt/solver/sat/goal2sat.ts +528 -0
  447. package/src/prover/smt/solver/sat/inc-sat-solver.ts +388 -0
  448. package/src/prover/smt/solver/sat/model-converter.ts +156 -0
  449. package/src/prover/smt/solver/sat/pb-constraint.ts +264 -0
  450. package/src/prover/smt/solver/sat/pb-solver.ts +1553 -0
  451. package/src/prover/smt/solver/sat/probing.ts +169 -0
  452. package/src/prover/smt/solver/sat/sat-tactic.ts +44 -0
  453. package/src/prover/smt/solver/sat/sat2goal.ts +171 -0
  454. package/src/prover/smt/solver/sat/scc.ts +143 -0
  455. package/src/prover/smt/solver/sat/simplifier.ts +1662 -0
  456. package/src/prover/smt/solver/sat/solver.ts +2128 -0
  457. package/src/prover/smt/solver/sat/var-queue.ts +119 -0
  458. package/src/prover/smt/solver/sat/watch-list.ts +136 -0
  459. package/src/prover/smt/solver/search-tree.ts +285 -0
  460. package/src/prover/smt/solver/seq-axioms.ts +1013 -0
  461. package/src/prover/smt/solver/seq-decl-plugin.ts +823 -0
  462. package/src/prover/smt/solver/seq-derive.ts +1303 -0
  463. package/src/prover/smt/solver/seq-eq-approx.ts +238 -0
  464. package/src/prover/smt/solver/seq-eq-solver.ts +440 -0
  465. package/src/prover/smt/solver/seq-factory.ts +91 -0
  466. package/src/prover/smt/solver/seq-monadic.ts +730 -0
  467. package/src/prover/smt/solver/seq-range-predicate.ts +225 -0
  468. package/src/prover/smt/solver/seq-re-rewriter.ts +1296 -0
  469. package/src/prover/smt/solver/seq-re-util.ts +777 -0
  470. package/src/prover/smt/solver/seq-regex-bisim.ts +145 -0
  471. package/src/prover/smt/solver/seq-regex-live.ts +202 -0
  472. package/src/prover/smt/solver/seq-regex-witness.ts +100 -0
  473. package/src/prover/smt/solver/seq-rewriter.ts +2407 -0
  474. package/src/prover/smt/solver/seq-skolem.ts +318 -0
  475. package/src/prover/smt/solver/seq-subset.ts +166 -0
  476. package/src/prover/smt/solver/seq-view-witness.ts +355 -0
  477. package/src/prover/smt/solver/smt-consequences.ts +299 -0
  478. package/src/prover/smt/solver/smt-kernel.ts +85 -0
  479. package/src/prover/smt/solver/smt-logics.ts +67 -0
  480. package/src/prover/smt/solver/smt-setup.ts +492 -0
  481. package/src/prover/smt/solver/smt2-printer.ts +767 -0
  482. package/src/prover/smt/solver/smtlib-parser.ts +2423 -270
  483. package/src/prover/smt/solver/smtlib-script.ts +1493 -0
  484. package/src/prover/smt/solver/solve-eqs.ts +931 -0
  485. package/src/prover/smt/solver/solver.ts +120 -39
  486. package/src/prover/smt/solver/sort.ts +1 -5
  487. package/src/prover/smt/solver/static-features.ts +319 -0
  488. package/src/prover/smt/solver/sticky-generations.ts +0 -5
  489. package/src/prover/smt/solver/tactic.ts +1936 -0
  490. package/src/prover/smt/solver/term-enumeration.ts +206 -0
  491. package/src/prover/smt/solver/theory-array.ts +1275 -0
  492. package/src/prover/smt/solver/theory-bv.ts +1239 -0
  493. package/src/prover/smt/solver/theory-char.ts +356 -0
  494. package/src/prover/smt/solver/theory-datatype.ts +599 -0
  495. package/src/prover/smt/solver/theory-finite-set.ts +30 -0
  496. package/src/prover/smt/solver/theory-fpa.ts +422 -0
  497. package/src/prover/smt/solver/theory-lra-axioms.ts +56 -25
  498. package/src/prover/smt/solver/theory-lra-bounds.ts +31 -10
  499. package/src/prover/smt/solver/theory-lra-final-check.ts +60 -91
  500. package/src/prover/smt/solver/theory-lra-internalize.ts +87 -53
  501. package/src/prover/smt/solver/theory-lra-model.ts +24 -53
  502. package/src/prover/smt/solver/theory-lra-propagate.ts +25 -40
  503. package/src/prover/smt/solver/theory-lra-state.ts +7 -27
  504. package/src/prover/smt/solver/theory-lra.ts +194 -8
  505. package/src/prover/smt/solver/theory-pb.ts +932 -0
  506. package/src/prover/smt/solver/theory-polymorphism.ts +59 -0
  507. package/src/prover/smt/solver/theory-recfun.ts +308 -0
  508. package/src/prover/smt/solver/theory-seq-regex.ts +871 -0
  509. package/src/prover/smt/solver/theory-seq-solve.ts +1375 -0
  510. package/src/prover/smt/solver/theory-seq-state.ts +880 -0
  511. package/src/prover/smt/solver/theory-seq.ts +984 -0
  512. package/src/prover/smt/solver/theory.ts +175 -0
  513. package/src/prover/smt/solver/tseitin-cnf.ts +487 -0
  514. package/src/prover/smt/solver/var-subst.ts +33 -35
  515. package/src/prover/smt/table.ts +1 -9
  516. package/src/prover/smt/types.ts +146 -0
  517. package/src/prover/tlapm/cache.ts +67 -41
  518. package/src/prover/tlapm/ocaml-marshal.ts +19 -21
  519. package/src/prover/tlapm/tlapm.ts +21 -20
  520. package/src/prover/tlapm-library.ts +3208 -0
  521. package/src/prover/verdict.ts +9 -2
  522. package/src/prover/zenon/check/check.ts +1 -11
  523. package/src/prover/zenon/check/definitions.ts +5 -15
  524. package/src/prover/zenon/check/equality.ts +0 -8
  525. package/src/prover/zenon/check/functions.ts +0 -4
  526. package/src/prover/zenon/check/keys.ts +0 -7
  527. package/src/prover/zenon/check/propositional.ts +0 -3
  528. package/src/prover/zenon/check/records.ts +1 -6
  529. package/src/prover/zenon/check/relations.ts +2 -14
  530. package/src/prover/zenon/check/rewrites.ts +2 -21
  531. package/src/prover/zenon/check/sets.ts +40 -61
  532. package/src/prover/zenon/check/terms.ts +1 -19
  533. package/src/prover/zenon/check/tla.ts +0 -6
  534. package/src/prover/zenon/decide.ts +10 -15
  535. package/src/prover/zenon/encode.ts +1 -8
  536. package/src/prover/zenon/eqrel.ts +2 -13
  537. package/src/prover/zenon/equality-rules.ts +0 -4
  538. package/src/prover/zenon/expr.ts +8 -26
  539. package/src/prover/zenon/index-table.ts +26 -57
  540. package/src/prover/zenon/isar-case.ts +9 -19
  541. package/src/prover/zenon/isar.ts +36 -64
  542. package/src/prover/zenon/limits.ts +0 -4
  543. package/src/prover/zenon/llproof.ts +2 -20
  544. package/src/prover/zenon/mltoll.ts +66 -104
  545. package/src/prover/zenon/node.ts +20 -14
  546. package/src/prover/zenon/ocaml.ts +1 -17
  547. package/src/prover/zenon/parse.ts +10 -12
  548. package/src/prover/zenon/phrase.ts +1 -11
  549. package/src/prover/zenon/proof.ts +0 -9
  550. package/src/prover/zenon/propositional-rules.ts +0 -5
  551. package/src/prover/zenon/rules.ts +2 -6
  552. package/src/prover/zenon/search.ts +11 -33
  553. package/src/prover/zenon/tla/functions.ts +59 -31
  554. package/src/prover/zenon/tla/llproof.ts +67 -76
  555. package/src/prover/zenon/tla/records.ts +57 -33
  556. package/src/prover/zenon/tla/rewrites.ts +12 -19
  557. package/src/prover/zenon/tla/sets.ts +88 -57
  558. package/src/prover/zenon/tla/terms.ts +0 -6
  559. package/src/prover/zenon/tla/tla.ts +66 -64
  560. package/src/randomization.ts +81 -35
  561. package/src/resolve.ts +67 -184
  562. package/src/runtime.ts +22 -22
  563. package/src/sany/builtins.ts +2 -5
  564. package/src/sany/generation.ts +394 -0
  565. package/src/sany/generator-def-entries.ts +210 -0
  566. package/src/sany/generator-definitions.ts +686 -0
  567. package/src/sany/generator-expressions.ts +1100 -0
  568. package/src/sany/generator-proofs.ts +662 -0
  569. package/src/sany/generator.ts +191 -2764
  570. package/src/sany/levels.ts +6 -34
  571. package/src/sany/lexer.ts +58 -77
  572. package/src/sany/linter.ts +1 -9
  573. package/src/sany/modules.ts +1 -5
  574. package/src/sany/nodes.ts +5 -43
  575. package/src/sany/parse-error.ts +39 -117
  576. package/src/sany/report.ts +32 -69
  577. package/src/sany/selectors.ts +49 -76
  578. package/src/sany/source.ts +1 -17
  579. package/src/sany/xml.ts +35 -117
  580. package/src/search.ts +541 -163
  581. package/src/simulate.ts +709 -0
  582. package/src/spec-description.ts +192 -100
  583. package/src/standard-definitions.ts +0 -4
  584. package/src/standard-modules.ts +264 -95
  585. package/src/state-graph.ts +52 -31
  586. package/src/subexpressions.ts +69 -52
  587. package/src/substitution-levels.ts +7 -7
  588. package/src/successors.ts +266 -99
  589. package/src/symmetry.ts +33 -25
  590. package/src/tableau.ts +27 -24
  591. package/src/tlc/fingerprint.ts +183 -67
  592. package/src/tlc/jar-data.ts +112 -6
  593. package/src/tlc/messages.ts +125 -54
  594. package/src/tlc/order.ts +39 -69
  595. package/src/tlc/random.ts +37 -49
  596. package/src/tlc/replay.ts +553 -228
  597. package/src/tlc/summary.ts +36 -47
  598. package/src/tlc/tlc-operators.ts +78 -44
  599. package/src/tlc/tlcext-operators.ts +139 -122
  600. package/src/tlc/trace-spec.ts +249 -0
  601. package/src/tlc/value-stream.ts +139 -0
  602. package/src/tlc/version.ts +1 -4
  603. package/src/transfer.ts +22 -25
  604. package/src/typescript/actions.ts +479 -0
  605. package/src/typescript/collections.ts +614 -0
  606. package/src/typescript/compile.ts +63 -0
  607. package/src/typescript/context.ts +94 -0
  608. package/src/typescript/emit.ts +121 -0
  609. package/src/typescript/expressions.ts +496 -0
  610. package/src/typescript/formula.ts +154 -0
  611. package/src/typescript/functions.ts +284 -0
  612. package/src/typescript/model.ts +162 -0
  613. package/src/typescript/names.ts +114 -0
  614. package/src/typescript/program.ts +48 -0
  615. package/src/typescript/properties.ts +139 -0
  616. package/src/typescript/scope.ts +112 -0
  617. package/src/typescript/steps.ts +167 -0
  618. package/src/typescript/types.ts +171 -0
  619. package/src/values.ts +832 -484
  620. package/src/worker.ts +7 -8
  621. package/src/prover/isabelle/isar/statement.ts +0 -57
  622. package/src/prover/tlaps-module.ts +0 -510
@@ -1,17 +1,23 @@
1
1
  // Ported from Z3 (src/ast/ast.h, src/ast/ast.cpp), MIT license, see z3-license.txt.
2
- // A hash-consed DAG of applications, de Bruijn variables and quantifiers, as in Z3's ast_manager.
3
2
  import { SmtInvariantError } from "../../../errors";
4
3
  import { FlatLiterals } from "./flat-literals";
5
4
  import { astArrayHash, bytesHash, combineHash, getCompositeHash, mkMix, stringHash } from "./hash";
6
5
  import { ObjMap } from "./hashtable";
7
6
  import { Rational } from "./rational";
8
7
  import { grow, shrink } from "./array";
8
+ import type { DatatypeDef } from "./datatype-decl-plugin";
9
+ import { AlgebraicManager, Anum } from "./nlsat/algebraic";
10
+ import type { Mpf } from "./mpf";
9
11
  import type { ResourceLimit } from "./rlimit";
10
12
 
11
13
  export const KIND_APP = 0;
12
14
  export const KIND_VAR = 1;
13
15
  export const KIND_QUANTIFIER = 2;
14
16
 
17
+ export const QUANTIFIER_FORALL = 0;
18
+ export const QUANTIFIER_EXISTS = 1;
19
+ export const QUANTIFIER_LAMBDA = 2;
20
+
15
21
  export const SORT_BOOL = 0;
16
22
  export const SORT_INT = 1;
17
23
  export const SORT_REAL = 2;
@@ -19,8 +25,18 @@ export const SORT_REAL = 2;
19
25
  export const FAMILY_BASIC = 0;
20
26
  export const FAMILY_ARITH = 1;
21
27
  export const FAMILY_USER = 2;
22
- // Model values: the elements that a model gives to an uninterpreted sort.
23
28
  export const FAMILY_MODEL_VALUE = 3;
29
+ export const FAMILY_FINITE_SET = 4;
30
+ export const FAMILY_BV = 5;
31
+ export const FAMILY_ARRAY = 6;
32
+ export const FAMILY_DATATYPE = 7;
33
+ export const FAMILY_FPA = 8;
34
+ export const FAMILY_CHAR = 9;
35
+ export const FAMILY_SEQ = 10;
36
+ export const FAMILY_LABEL = 11;
37
+ export const FAMILY_PB = 12;
38
+ export const FAMILY_RECFUN = 13;
39
+ export const FAMILY_POLY = 14;
24
40
 
25
41
  export const OP_TRUE = 0;
26
42
  export const OP_FALSE = 1;
@@ -49,18 +65,281 @@ export const OP_DIV = 23;
49
65
  export const OP_TO_REAL = 24;
50
66
  export const OP_TO_INT = 25;
51
67
  export const OP_IS_INT = 26;
52
- // Z3's div0, idiv0 and mod0: division by zero, which theory_lra's found_underspecified equates with
53
- // the division it stands for.
54
68
  export const OP_DIV0 = 27;
55
69
  export const OP_IDIV0 = 28;
56
70
  export const OP_MOD0 = 29;
57
- export const OP_UNINTERPRETED = 30;
58
- const OP_MODEL_VALUE = 31;
71
+ export const OP_POWER = 30;
72
+ export const OP_POWER0 = 31;
73
+ export const OP_SIN = 32;
74
+ export const OP_COS = 33;
75
+ export const OP_TAN = 34;
76
+ export const OP_ASIN = 35;
77
+ export const OP_ACOS = 36;
78
+ export const OP_ATAN = 37;
79
+ export const OP_SINH = 38;
80
+ export const OP_COSH = 39;
81
+ export const OP_TANH = 40;
82
+ export const OP_ASINH = 41;
83
+ export const OP_ACOSH = 42;
84
+ export const OP_ATANH = 43;
85
+ export const OP_EXP = 44;
86
+ export const OP_ATAN2 = 45;
87
+ export const OP_LOG = 46;
88
+ export const OP_PI = 47;
89
+ export const OP_E = 48;
90
+ export const OP_ALGEBRAIC_NUM = 49;
91
+ export const OP_REM = 50;
92
+ export const OP_NEG_ROOT = 51;
93
+ export const OP_U_ASIN = 52;
94
+ export const OP_U_ACOS = 53;
95
+ export const OP_UNINTERPRETED = 60;
96
+ const OP_MODEL_VALUE = 61;
97
+
98
+ const BV_OP_BASE = 100;
99
+ export const OP_BV_NUM = BV_OP_BASE;
100
+ export const OP_BNEG = BV_OP_BASE + 3;
101
+ export const OP_BADD = BV_OP_BASE + 4;
102
+ export const OP_BSUB = BV_OP_BASE + 5;
103
+ export const OP_BMUL = BV_OP_BASE + 6;
104
+ export const OP_BSDIV = BV_OP_BASE + 7;
105
+ export const OP_BUDIV = BV_OP_BASE + 8;
106
+ export const OP_BSREM = BV_OP_BASE + 9;
107
+ export const OP_BUREM = BV_OP_BASE + 10;
108
+ export const OP_BSMOD = BV_OP_BASE + 11;
109
+ export const OP_BSDIV0 = BV_OP_BASE + 12;
110
+ export const OP_BUDIV0 = BV_OP_BASE + 13;
111
+ export const OP_BSREM0 = BV_OP_BASE + 14;
112
+ export const OP_BUREM0 = BV_OP_BASE + 15;
113
+ export const OP_BSMOD0 = BV_OP_BASE + 16;
114
+ export const OP_BSDIV_I = BV_OP_BASE + 17;
115
+ export const OP_BUDIV_I = BV_OP_BASE + 18;
116
+ export const OP_BSREM_I = BV_OP_BASE + 19;
117
+ export const OP_BUREM_I = BV_OP_BASE + 20;
118
+ export const OP_BSMOD_I = BV_OP_BASE + 21;
119
+ export const OP_ULEQ = BV_OP_BASE + 22;
120
+ export const OP_SLEQ = BV_OP_BASE + 23;
121
+ export const OP_UGEQ = BV_OP_BASE + 24;
122
+ export const OP_SGEQ = BV_OP_BASE + 25;
123
+ export const OP_ULT = BV_OP_BASE + 26;
124
+ export const OP_SLT = BV_OP_BASE + 27;
125
+ export const OP_UGT = BV_OP_BASE + 28;
126
+ export const OP_SGT = BV_OP_BASE + 29;
127
+ export const OP_BAND = BV_OP_BASE + 30;
128
+ export const OP_BOR = BV_OP_BASE + 31;
129
+ export const OP_BNOT = BV_OP_BASE + 32;
130
+ export const OP_BXOR = BV_OP_BASE + 33;
131
+ export const OP_BNAND = BV_OP_BASE + 34;
132
+ export const OP_BNOR = BV_OP_BASE + 35;
133
+ export const OP_BXNOR = BV_OP_BASE + 36;
134
+ export const OP_CONCAT = BV_OP_BASE + 37;
135
+ export const OP_SIGN_EXT = BV_OP_BASE + 38;
136
+ export const OP_ZERO_EXT = BV_OP_BASE + 39;
137
+ export const OP_EXTRACT = BV_OP_BASE + 40;
138
+ export const OP_REPEAT = BV_OP_BASE + 41;
139
+ export const OP_BREDOR = BV_OP_BASE + 42;
140
+ export const OP_BREDAND = BV_OP_BASE + 43;
141
+ export const OP_BCOMP = BV_OP_BASE + 44;
142
+ export const OP_BSHL = BV_OP_BASE + 45;
143
+ export const OP_BLSHR = BV_OP_BASE + 46;
144
+ export const OP_BASHR = BV_OP_BASE + 47;
145
+ export const OP_ROTATE_LEFT = BV_OP_BASE + 48;
146
+ export const OP_ROTATE_RIGHT = BV_OP_BASE + 49;
147
+ export const OP_EXT_ROTATE_LEFT = BV_OP_BASE + 50;
148
+ export const OP_EXT_ROTATE_RIGHT = BV_OP_BASE + 51;
149
+ export const OP_BUMUL_NO_OVFL = BV_OP_BASE + 52;
150
+ export const OP_BSMUL_NO_OVFL = BV_OP_BASE + 53;
151
+ export const OP_BSMUL_NO_UDFL = BV_OP_BASE + 54;
152
+ export const OP_BUMUL_OVFL = BV_OP_BASE + 55;
153
+ export const OP_BSMUL_OVFL = BV_OP_BASE + 56;
154
+ export const OP_BSDIV_OVFL = BV_OP_BASE + 57;
155
+ export const OP_BNEG_OVFL = BV_OP_BASE + 58;
156
+ export const OP_BUADD_OVFL = BV_OP_BASE + 59;
157
+ export const OP_BSADD_OVFL = BV_OP_BASE + 60;
158
+ export const OP_BUSUB_OVFL = BV_OP_BASE + 61;
159
+ export const OP_BSSUB_OVFL = BV_OP_BASE + 62;
160
+ export const OP_BIT2BOOL = BV_OP_BASE + 63;
161
+ export const OP_MKBV = BV_OP_BASE + 64;
162
+ export const OP_INT2BV = BV_OP_BASE + 65;
163
+ export const OP_UBV2INT = BV_OP_BASE + 66;
164
+ export const OP_SBV2INT = BV_OP_BASE + 67;
165
+ export const OP_CARRY = BV_OP_BASE + 68;
166
+ export const OP_XOR3 = BV_OP_BASE + 69;
167
+
168
+ const ARRAY_OP_BASE = 200;
169
+ export const OP_STORE = ARRAY_OP_BASE;
170
+ export const OP_SELECT = ARRAY_OP_BASE + 1;
171
+ export const OP_CONST_ARRAY = ARRAY_OP_BASE + 2;
172
+ export const OP_ARRAY_EXT = ARRAY_OP_BASE + 3;
173
+ export const OP_ARRAY_DEFAULT = ARRAY_OP_BASE + 4;
174
+ export const OP_ARRAY_MAP = ARRAY_OP_BASE + 5;
175
+ export const OP_AS_ARRAY = ARRAY_OP_BASE + 11;
176
+ const DATATYPE_OP_BASE = 300;
177
+ export const OP_DT_CONSTRUCTOR = DATATYPE_OP_BASE;
178
+ export const OP_DT_RECOGNISER = DATATYPE_OP_BASE + 1;
179
+ export const OP_DT_IS = DATATYPE_OP_BASE + 2;
180
+ export const OP_DT_ACCESSOR = DATATYPE_OP_BASE + 3;
181
+ export const OP_DT_UPDATE_FIELD = DATATYPE_OP_BASE + 4;
182
+ const FPA_OP_BASE = 400;
183
+ export const OP_FPA_RM_NEAREST_TIES_TO_EVEN = FPA_OP_BASE;
184
+ export const OP_FPA_RM_NEAREST_TIES_TO_AWAY = FPA_OP_BASE + 1;
185
+ export const OP_FPA_RM_TOWARD_POSITIVE = FPA_OP_BASE + 2;
186
+ export const OP_FPA_RM_TOWARD_NEGATIVE = FPA_OP_BASE + 3;
187
+ export const OP_FPA_RM_TOWARD_ZERO = FPA_OP_BASE + 4;
188
+ export const OP_FPA_NUM = FPA_OP_BASE + 5;
189
+ export const OP_FPA_PLUS_INF = FPA_OP_BASE + 6;
190
+ export const OP_FPA_MINUS_INF = FPA_OP_BASE + 7;
191
+ export const OP_FPA_NAN = FPA_OP_BASE + 8;
192
+ export const OP_FPA_PLUS_ZERO = FPA_OP_BASE + 9;
193
+ export const OP_FPA_MINUS_ZERO = FPA_OP_BASE + 10;
194
+ export const OP_FPA_ADD = FPA_OP_BASE + 11;
195
+ export const OP_FPA_SUB = FPA_OP_BASE + 12;
196
+ export const OP_FPA_NEG = FPA_OP_BASE + 13;
197
+ export const OP_FPA_MUL = FPA_OP_BASE + 14;
198
+ export const OP_FPA_DIV = FPA_OP_BASE + 15;
199
+ export const OP_FPA_REM = FPA_OP_BASE + 16;
200
+ export const OP_FPA_ABS = FPA_OP_BASE + 17;
201
+ export const OP_FPA_MIN = FPA_OP_BASE + 18;
202
+ export const OP_FPA_MAX = FPA_OP_BASE + 19;
203
+ export const OP_FPA_MIN_I = FPA_OP_BASE + 20;
204
+ export const OP_FPA_MAX_I = FPA_OP_BASE + 21;
205
+ export const OP_FPA_FMA = FPA_OP_BASE + 22;
206
+ export const OP_FPA_SQRT = FPA_OP_BASE + 23;
207
+ export const OP_FPA_ROUND_TO_INTEGRAL = FPA_OP_BASE + 24;
208
+ export const OP_FPA_EQ = FPA_OP_BASE + 25;
209
+ export const OP_FPA_LT = FPA_OP_BASE + 26;
210
+ export const OP_FPA_GT = FPA_OP_BASE + 27;
211
+ export const OP_FPA_LE = FPA_OP_BASE + 28;
212
+ export const OP_FPA_GE = FPA_OP_BASE + 29;
213
+ export const OP_FPA_IS_NAN = FPA_OP_BASE + 30;
214
+ export const OP_FPA_IS_INF = FPA_OP_BASE + 31;
215
+ export const OP_FPA_IS_ZERO = FPA_OP_BASE + 32;
216
+ export const OP_FPA_IS_NORMAL = FPA_OP_BASE + 33;
217
+ export const OP_FPA_IS_SUBNORMAL = FPA_OP_BASE + 34;
218
+ export const OP_FPA_IS_NEGATIVE = FPA_OP_BASE + 35;
219
+ export const OP_FPA_IS_POSITIVE = FPA_OP_BASE + 36;
220
+ export const OP_FPA_FP = FPA_OP_BASE + 37;
221
+ export const OP_FPA_TO_FP = FPA_OP_BASE + 38;
222
+ export const OP_FPA_TO_FP_UNSIGNED = FPA_OP_BASE + 39;
223
+ export const OP_FPA_TO_UBV = FPA_OP_BASE + 40;
224
+ export const OP_FPA_TO_SBV = FPA_OP_BASE + 41;
225
+ export const OP_FPA_TO_REAL = FPA_OP_BASE + 42;
226
+ export const OP_FPA_TO_REAL_I = FPA_OP_BASE + 43;
227
+ export const OP_FPA_TO_SBV_I = FPA_OP_BASE + 44;
228
+ export const OP_FPA_TO_UBV_I = FPA_OP_BASE + 45;
229
+ export const OP_FPA_TO_IEEE_BV = FPA_OP_BASE + 46;
230
+ export const OP_FPA_TO_IEEE_BV_I = FPA_OP_BASE + 47;
231
+ export const OP_FPA_BVWRAP = FPA_OP_BASE + 48;
232
+ export const OP_FPA_BV2RM = FPA_OP_BASE + 49;
233
+ const SEQ_OP_BASE = 500;
234
+ export const OP_SEQ_UNIT = SEQ_OP_BASE;
235
+ export const OP_SEQ_EMPTY = SEQ_OP_BASE + 1;
236
+ export const OP_SEQ_CONCAT = SEQ_OP_BASE + 2;
237
+ export const OP_SEQ_PREFIX = SEQ_OP_BASE + 3;
238
+ export const OP_SEQ_SUFFIX = SEQ_OP_BASE + 4;
239
+ export const OP_SEQ_CONTAINS = SEQ_OP_BASE + 5;
240
+ export const OP_SEQ_EXTRACT = SEQ_OP_BASE + 6;
241
+ export const OP_SEQ_REPLACE = SEQ_OP_BASE + 7;
242
+ export const OP_SEQ_AT = SEQ_OP_BASE + 8;
243
+ export const OP_SEQ_NTH = SEQ_OP_BASE + 9;
244
+ export const OP_SEQ_NTH_I = SEQ_OP_BASE + 10;
245
+ export const OP_SEQ_NTH_U = SEQ_OP_BASE + 11;
246
+ export const OP_SEQ_LENGTH = SEQ_OP_BASE + 12;
247
+ export const OP_SEQ_INDEX = SEQ_OP_BASE + 13;
248
+ export const OP_SEQ_LAST_INDEX = SEQ_OP_BASE + 14;
249
+ export const OP_SEQ_TO_RE = SEQ_OP_BASE + 15;
250
+ export const OP_SEQ_IN_RE = SEQ_OP_BASE + 16;
251
+ export const OP_SEQ_REPLACE_RE_ALL = SEQ_OP_BASE + 17;
252
+ export const OP_SEQ_REPLACE_RE = SEQ_OP_BASE + 18;
253
+ export const OP_SEQ_REPLACE_ALL = SEQ_OP_BASE + 19;
254
+ export const OP_SEQ_MAP = SEQ_OP_BASE + 20;
255
+ export const OP_SEQ_MAPI = SEQ_OP_BASE + 21;
256
+ export const OP_SEQ_FOLDL = SEQ_OP_BASE + 22;
257
+ export const OP_SEQ_FOLDLI = SEQ_OP_BASE + 23;
258
+ export const OP_SEQ_POWER = SEQ_OP_BASE + 24;
259
+ export const OP_RE_PLUS = SEQ_OP_BASE + 25;
260
+ export const OP_RE_STAR = SEQ_OP_BASE + 26;
261
+ export const OP_RE_OPTION = SEQ_OP_BASE + 27;
262
+ export const OP_RE_RANGE = SEQ_OP_BASE + 28;
263
+ export const OP_RE_CONCAT = SEQ_OP_BASE + 29;
264
+ export const OP_RE_UNION = SEQ_OP_BASE + 30;
265
+ export const OP_RE_DIFF = SEQ_OP_BASE + 31;
266
+ export const OP_RE_INTERSECT = SEQ_OP_BASE + 32;
267
+ export const OP_RE_XOR = SEQ_OP_BASE + 33;
268
+ export const OP_RE_LOOP = SEQ_OP_BASE + 34;
269
+ export const OP_RE_POWER = SEQ_OP_BASE + 35;
270
+ export const OP_RE_COMPLEMENT = SEQ_OP_BASE + 36;
271
+ export const OP_RE_EMPTY_SET = SEQ_OP_BASE + 37;
272
+ export const OP_RE_FULL_SEQ_SET = SEQ_OP_BASE + 38;
273
+ export const OP_RE_FULL_CHAR_SET = SEQ_OP_BASE + 39;
274
+ export const OP_RE_OF_PRED = SEQ_OP_BASE + 40;
275
+ export const OP_RE_REVERSE = SEQ_OP_BASE + 41;
276
+ export const OP_RE_DERIVATIVE = SEQ_OP_BASE + 42;
277
+ export const OP_STRING_CONST = SEQ_OP_BASE + 43;
278
+ export const OP_STRING_ITOS = SEQ_OP_BASE + 44;
279
+ export const OP_STRING_STOI = SEQ_OP_BASE + 45;
280
+ export const OP_STRING_UBVTOS = SEQ_OP_BASE + 46;
281
+ export const OP_STRING_SBVTOS = SEQ_OP_BASE + 47;
282
+ export const OP_STRING_LT = SEQ_OP_BASE + 48;
283
+ export const OP_STRING_LE = SEQ_OP_BASE + 49;
284
+ export const OP_STRING_IS_DIGIT = SEQ_OP_BASE + 50;
285
+ export const OP_STRING_TO_CODE = SEQ_OP_BASE + 51;
286
+ export const OP_STRING_FROM_CODE = SEQ_OP_BASE + 52;
287
+ export const OP_INTERNAL_STRING_FROM_CHAR = SEQ_OP_BASE + 53;
288
+ export const OP_INTERNAL_STRING_STRREPL = SEQ_OP_BASE + 54;
289
+ export const OP_INTERNAL_STRING_CONCAT = SEQ_OP_BASE + 55;
290
+ export const OP_INTERNAL_STRING_LENGTH = SEQ_OP_BASE + 56;
291
+ export const OP_INTERNAL_STRING_STRCTN = SEQ_OP_BASE + 57;
292
+ export const OP_INTERNAL_STRING_PREFIX = SEQ_OP_BASE + 58;
293
+ export const OP_INTERNAL_STRING_SUFFIX = SEQ_OP_BASE + 59;
294
+ export const OP_INTERNAL_STRING_IN_REGEXP = SEQ_OP_BASE + 60;
295
+ export const OP_INTERNAL_STRING_TO_REGEXP = SEQ_OP_BASE + 61;
296
+ export const OP_INTERNAL_STRING_CHARAT = SEQ_OP_BASE + 62;
297
+ export const OP_INTERNAL_STRING_SUBSTR = SEQ_OP_BASE + 63;
298
+ export const OP_INTERNAL_STRING_STRIDOF = SEQ_OP_BASE + 64;
299
+ export const OP_INTERNAL_STRING_POWER = SEQ_OP_BASE + 65;
300
+ export const OP_INTERNAL_REGEXP_EMPTY = SEQ_OP_BASE + 66;
301
+ export const OP_INTERNAL_REGEXP_FULL_CHAR = SEQ_OP_BASE + 67;
302
+ export const OP_RE_IS_NULLABLE = SEQ_OP_BASE + 68;
303
+ export const OP_SEQ_SKOLEM = SEQ_OP_BASE + 69;
304
+ const CHAR_OP_BASE = 600;
305
+ export const OP_CHAR_CONST = CHAR_OP_BASE;
306
+ export const OP_CHAR_LE = CHAR_OP_BASE + 1;
307
+ export const OP_CHAR_TO_INT = CHAR_OP_BASE + 2;
308
+ export const OP_CHAR_TO_BV = CHAR_OP_BASE + 3;
309
+ export const OP_CHAR_FROM_BV = CHAR_OP_BASE + 4;
310
+ export const OP_CHAR_IS_DIGIT = CHAR_OP_BASE + 5;
311
+ const LABEL_OP_BASE = 700;
312
+ export const OP_LABEL = LABEL_OP_BASE;
313
+ export const OP_LABEL_LIT = LABEL_OP_BASE + 1;
314
+ const PB_OP_BASE = 800;
315
+ export const OP_AT_MOST_K = PB_OP_BASE;
316
+ export const OP_AT_LEAST_K = PB_OP_BASE + 1;
317
+ export const OP_PB_LE = PB_OP_BASE + 2;
318
+ export const OP_PB_GE = PB_OP_BASE + 3;
319
+ export const OP_PB_EQ = PB_OP_BASE + 4;
320
+ const RECFUN_OP_BASE = 900;
321
+ export const OP_FUN_DEFINED = RECFUN_OP_BASE;
322
+ export const OP_FUN_CASE_PRED = RECFUN_OP_BASE + 1;
323
+ export const OP_NUM_ROUNDS = RECFUN_OP_BASE + 2;
324
+
325
+ // The number of elements of a sort, with -1 for an infinite sort and -2 for a very big one.
326
+ export const SORT_SIZE_INFINITE = -1n;
327
+ export const SORT_SIZE_VERY_BIG = -2n;
328
+
329
+ const UNCACHED_BV_OPS: ReadonlySet<number> = new Set([
330
+ OP_CONCAT,
331
+ OP_SIGN_EXT,
332
+ OP_ZERO_EXT,
333
+ OP_EXTRACT,
334
+ OP_REPEAT,
335
+ OP_ROTATE_LEFT,
336
+ OP_ROTATE_RIGHT,
337
+ ]);
59
338
 
60
- // A Z3 symbol: a string, or a number for a numerical symbol, which Z3 prints as k!n.
61
339
  export type SmtSymbol = string | number;
62
340
 
63
- const getSymbolName = (symbol: SmtSymbol) => (typeof symbol === "number" ? `k!${symbol}` : symbol);
341
+ export const getSymbolName = (symbol: SmtSymbol) =>
342
+ typeof symbol === "number" ? `k!${symbol}` : symbol;
64
343
 
65
344
  export interface FuncDecl {
66
345
  name: SmtSymbol;
@@ -69,20 +348,36 @@ export interface FuncDecl {
69
348
  domain: number[];
70
349
  range: number;
71
350
  isSkolem: boolean;
72
- // Z3's small id of the declaration, which its matcher hashes.
73
351
  smallId: number;
352
+ parameters: number[];
353
+ }
354
+
355
+ // The rhs of a definition is -1 until its body is parsed.
356
+ export interface RecFunDef {
357
+ decl: number;
358
+ rhs: number;
359
+ isMacro: boolean;
360
+ cases: RecFunCase[];
361
+ }
362
+
363
+ export interface RecFunCase {
364
+ pred: number;
365
+ guards: number[];
366
+ rhs: number;
367
+ isImmediate: boolean;
368
+ def: RecFunDef;
74
369
  }
75
370
 
76
371
  interface QuantifierData {
77
- isForall: boolean;
372
+ kind: number;
78
373
  sorts: number[];
79
- // The names of the variables, which tell apart quantifiers that differ only in them, as in Z3.
80
374
  names: SmtSymbol[];
81
375
  body: number;
82
- // Applications of the pattern declaration to the terms of each multi-pattern.
83
376
  patterns: number[];
377
+ noPatterns: number[];
84
378
  weight: number;
85
379
  qid: string;
380
+ skid?: string;
86
381
  }
87
382
 
88
383
  const BUILTIN_DECLS: [string, number, number, number][] = [
@@ -116,18 +411,51 @@ const BUILTIN_DECLS: [string, number, number, number][] = [
116
411
  ["/0", FAMILY_ARITH, OP_DIV0, SORT_REAL],
117
412
  ["div0", FAMILY_ARITH, OP_IDIV0, SORT_INT],
118
413
  ["mod0", FAMILY_ARITH, OP_MOD0, SORT_INT],
414
+ ["^", FAMILY_ARITH, OP_POWER, SORT_REAL],
415
+ ["^0", FAMILY_ARITH, OP_POWER0, SORT_REAL],
416
+ ["sin", FAMILY_ARITH, OP_SIN, SORT_REAL],
417
+ ["cos", FAMILY_ARITH, OP_COS, SORT_REAL],
418
+ ["tan", FAMILY_ARITH, OP_TAN, SORT_REAL],
419
+ ["asin", FAMILY_ARITH, OP_ASIN, SORT_REAL],
420
+ ["acos", FAMILY_ARITH, OP_ACOS, SORT_REAL],
421
+ ["atan", FAMILY_ARITH, OP_ATAN, SORT_REAL],
422
+ ["sinh", FAMILY_ARITH, OP_SINH, SORT_REAL],
423
+ ["cosh", FAMILY_ARITH, OP_COSH, SORT_REAL],
424
+ ["tanh", FAMILY_ARITH, OP_TANH, SORT_REAL],
425
+ ["asinh", FAMILY_ARITH, OP_ASINH, SORT_REAL],
426
+ ["acosh", FAMILY_ARITH, OP_ACOSH, SORT_REAL],
427
+ ["atanh", FAMILY_ARITH, OP_ATANH, SORT_REAL],
428
+ ["exp", FAMILY_ARITH, OP_EXP, SORT_REAL],
429
+ ["atan2", FAMILY_ARITH, OP_ATAN2, SORT_REAL],
430
+ ["log", FAMILY_ARITH, OP_LOG, SORT_REAL],
431
+ ["pi", FAMILY_ARITH, OP_PI, SORT_REAL],
432
+ ["euler", FAMILY_ARITH, OP_E, SORT_REAL],
433
+ ["RootObject", FAMILY_ARITH, OP_ALGEBRAIC_NUM, SORT_REAL],
434
+ ["rem", FAMILY_ARITH, OP_REM, SORT_INT],
435
+ ["neg-root", FAMILY_ARITH, OP_NEG_ROOT, SORT_REAL],
436
+ ["asin-u", FAMILY_ARITH, OP_U_ASIN, SORT_REAL],
437
+ ["acos-u", FAMILY_ARITH, OP_U_ACOS, SORT_REAL],
119
438
  ];
120
439
 
121
- // Z3's family ids and decl kinds, which its AST hashes include.
122
440
  const Z3_BASIC_FAMILY = 0;
441
+ const Z3_LABEL_FAMILY = 1;
123
442
  const Z3_PATTERN_FAMILY = 2;
124
443
  const Z3_MODEL_VALUE_FAMILY = 3;
125
444
  const Z3_USER_SORT_FAMILY = 4;
126
445
  const Z3_ARITH_FAMILY = 5;
446
+ const Z3_BV_FAMILY = 8;
447
+ const Z3_ARRAY_FAMILY = 9;
448
+ const Z3_DATATYPE_FAMILY = 7;
127
449
  const Z3_RECFUN_FAMILY = 10;
450
+ const Z3_CHAR_FAMILY = 11;
451
+ const Z3_SEQ_FAMILY = 12;
452
+ const Z3_PB_FAMILY = 13;
453
+ const Z3_FPA_FAMILY = 14;
454
+ const Z3_SEQ_SORT = 0;
455
+ const Z3_RE_SORT = 1;
456
+ const Z3_FLOATING_POINT_SORT = 0;
457
+ const Z3_ROUNDING_MODE_SORT = 1;
128
458
  const Z3_NULL = 0xffffffff;
129
- // The name, Z3 decl kind and arity of each builtin, whose Z3 declaration has this many arguments
130
- // whatever the number of arguments of the application (-1 for the arguments' own number).
131
459
  const Z3_BUILTINS = new Map<number, [string, number, number]>([
132
460
  [OP_TRUE, ["true", 0, 0]],
133
461
  [OP_FALSE, ["false", 1, 0]],
@@ -149,6 +477,7 @@ const Z3_BUILTINS = new Map<number, [string, number, number]>([
149
477
  [OP_MUL, ["*", 9, 2]],
150
478
  [OP_IDIV, ["div", 11, 2]],
151
479
  [OP_MOD, ["mod", 16, 2]],
480
+ [OP_REM, ["rem", 15, 2]],
152
481
  [OP_ABS, ["abs", 21, 1]],
153
482
  [OP_PATTERN, ["pattern", 0, -1]],
154
483
  [OP_DIV, ["/", 10, 2]],
@@ -158,13 +487,30 @@ const Z3_BUILTINS = new Map<number, [string, number, number]>([
158
487
  [OP_DIV0, ["/0", 12, 2]],
159
488
  [OP_IDIV0, ["div0", 13, 2]],
160
489
  [OP_MOD0, ["mod0", 17, 2]],
490
+ [OP_POWER, ["^", 22, 2]],
491
+ [OP_POWER0, ["^0", 23, 2]],
492
+ [OP_SIN, ["sin", 24, 1]],
493
+ [OP_COS, ["cos", 25, 1]],
494
+ [OP_TAN, ["tan", 26, 1]],
495
+ [OP_ASIN, ["asin", 27, 1]],
496
+ [OP_ACOS, ["acos", 28, 1]],
497
+ [OP_ATAN, ["atan", 29, 1]],
498
+ [OP_SINH, ["sinh", 30, 1]],
499
+ [OP_COSH, ["cosh", 31, 1]],
500
+ [OP_TANH, ["tanh", 32, 1]],
501
+ [OP_ASINH, ["asinh", 33, 1]],
502
+ [OP_ACOSH, ["acosh", 34, 1]],
503
+ [OP_ATANH, ["atanh", 35, 1]],
504
+ [OP_EXP, ["exp", 36, 1]],
505
+ [OP_ATAN2, ["atan2", 37, 2]],
506
+ [OP_LOG, ["log", 38, 1]],
507
+ [OP_PI, ["pi", 43, 0]],
508
+ [OP_E, ["euler", 44, 0]],
509
+ [OP_NEG_ROOT, ["neg-root", 45, 2]],
510
+ [OP_U_ASIN, ["asin-u", 46, 1]],
511
+ [OP_U_ACOS, ["acos-u", 47, 1]],
161
512
  ]);
162
513
 
163
- // The small ids of the Int declarations Z3 makes when it starts, from arith_decl_plugin::set_manager,
164
- // which Real terms share. The declarations of Real terms only have the ids of Real's /, Int's
165
- // to_real, Real's to_int and is_int. Z3 makes =, ite, distinct, pattern, numeral, /0, div0 and mod0
166
- // declarations per signature on first use, so they get -1 here, and their small ids are kept by
167
- // signature.
168
514
  const Z3_SMALL_IDS = new Map([
169
515
  [OP_TRUE, 1],
170
516
  [OP_FALSE, 2],
@@ -181,6 +527,7 @@ const Z3_SMALL_IDS = new Map([
181
527
  [OP_SUB, 37],
182
528
  [OP_MUL, 38],
183
529
  [OP_IDIV, 39],
530
+ [OP_REM, 40],
184
531
  [OP_MOD, 41],
185
532
  [OP_UMINUS, 42],
186
533
  [OP_ABS, 50],
@@ -188,28 +535,42 @@ const Z3_SMALL_IDS = new Map([
188
535
  [OP_TO_REAL, 43],
189
536
  [OP_TO_INT, 45],
190
537
  [OP_IS_INT, 47],
538
+ [OP_POWER, 48],
539
+ [OP_SIN, 52],
540
+ [OP_COS, 53],
541
+ [OP_TAN, 54],
542
+ [OP_ASIN, 55],
543
+ [OP_ACOS, 56],
544
+ [OP_ATAN, 57],
545
+ [OP_SINH, 58],
546
+ [OP_COSH, 59],
547
+ [OP_TANH, 60],
548
+ [OP_ASINH, 61],
549
+ [OP_ACOSH, 62],
550
+ [OP_ATANH, 63],
551
+ [OP_EXP, 64],
552
+ [OP_ATAN2, 65],
553
+ [OP_LOG, 66],
554
+ [OP_PI, 67],
555
+ [OP_E, 68],
556
+ [OP_NEG_ROOT, 69],
557
+ [OP_U_ASIN, 70],
558
+ [OP_U_ACOS, 71],
191
559
  ]);
192
560
 
193
- // The small id of the first sort or declaration a problem declares, after Z3's own.
194
561
  const FIRST_DECLARED_SMALL_ID = 143;
195
562
 
196
- // The ids of Z3's own expressions when it starts: the undef proof, true and false, then pi, euler, a
197
- // bit-vector numeral, fifteen real variables and a sum of two of them, which no problem shares.
198
563
  const FIRST_FREE_ID = 22;
199
- // MAX_SMALL_NUM_TO_CACHE: arith_decl_plugin holds the numerals below it once it makes them.
200
564
  const MAX_SMALL_NUM_TO_CACHE = 16;
201
- // The sorts that finite_set_decl_plugin::init makes.
202
565
  const NUM_FINITE_SET_PLUGIN_SORTS = 7;
203
566
 
204
- // symbol::hash: a numerical symbol hashes to its number.
205
- const getSymbolHash = (symbol: SmtSymbol) =>
567
+ const PB_OP_NAMES = ["at-most", "at-least", "pble", "pbge", "pbeq"];
568
+
569
+ export const getSymbolHash = (symbol: SmtSymbol) =>
206
570
  typeof symbol === "number" ? symbol : stringHash(symbol, 17);
207
571
 
208
- // Whether a sort is Int or Real.
209
572
  export const isArithSort = (sort: number) => sort === SORT_INT || sort === SORT_REAL;
210
573
 
211
- // mpz_manager::hash: the absolute value of an integer below 2^32, else the hash of its 32-bit digits,
212
- // least significant first.
213
574
  const getIntegerHash = (value: bigint) => {
214
575
  const magnitude = value < 0n ? -value : value;
215
576
  if (magnitude <= 0xffffffffn) return Number(magnitude);
@@ -218,115 +579,154 @@ const getIntegerHash = (value: bigint) => {
218
579
  return bytesHash(new Uint8Array(Uint32Array.from(digits).buffer), 17);
219
580
  };
220
581
 
221
- // decl_info::hash.
222
582
  const getInfoHash = (family: number, kind: number, parameterHashes: number[]) =>
223
583
  mkMix(family, kind, parameterHashes.length === 0 ? 0 : getCompositeHash(parameterHashes));
224
584
 
225
- // The kinds of an integer signature, in its low three bits.
226
585
  const SIGNATURE_EQ = 0;
227
586
  const SIGNATURE_ITE = 1;
228
587
  const SIGNATURE_DIV0 = 2;
229
588
  const SIGNATURE_MODEL_VALUE = 3;
230
589
  const SIGNATURE_NUMERAL = 4;
590
+ const SIGNATURE_ALGEBRAIC = 5;
591
+
592
+ interface AlgebraicNumeral {
593
+ value: Anum;
594
+ id: number;
595
+ }
231
596
 
232
- // A builtin declaration that Z3 makes once per signature: its small id, and the references that
233
- // applications and model values hold to it.
234
597
  interface SignatureDecl {
235
598
  smallId: number;
236
599
  refCount: number;
237
600
  }
238
601
 
602
+ const hasOwnDeclHash = (family: number) =>
603
+ family === FAMILY_USER ||
604
+ family === FAMILY_BV ||
605
+ family === FAMILY_ARRAY ||
606
+ family === FAMILY_DATATYPE ||
607
+ family === FAMILY_FPA ||
608
+ family === FAMILY_CHAR ||
609
+ family === FAMILY_SEQ ||
610
+ family === FAMILY_LABEL ||
611
+ family === FAMILY_PB ||
612
+ family === FAMILY_RECFUN;
613
+
614
+ // The declarations that Z3 makes once and counts references to, rather than on each use.
615
+ const isDeclaredFamily = (family: number) => family === FAMILY_USER || family === FAMILY_RECFUN;
616
+
239
617
  export class AstManager {
240
- // The tables of the expressions are typed arrays indexed by expression, which the collector does
241
- // not scan. They have room for more expressions than numNodes and double when full, which
242
- // replaces them, so a caller does not keep one across the creation of an expression.
243
618
  private numNodes = 0;
244
619
  kinds = new Uint8Array(1024);
245
- // The declaration of an application.
246
620
  decls = new Int32Array(1024);
247
- // The arguments of every expression end to end: those of expr are argPool[argStarts[expr]] up to
248
- // argPool[argStarts[expr + 1]]. Variables and constants take an empty range and a quantifier
249
- // takes its body. Both grow by doubling, which replaces the arrays.
250
621
  private argPool = new Int32Array(1024);
251
622
  private argStarts = new Int32Array(1024);
252
623
  sorts = new Int32Array(1024);
253
- // The index of a variable or model value, or of a numeral's value in numeralValues.
254
624
  values = new Int32Array(1024);
255
625
  private numeralValues: Rational[] = [];
256
- // The integer numerals by value, which finds one without building its key in the table.
626
+ // An irrational numeral is never shared: each takes a fresh id, which its deletion frees.
627
+ private algebraicNumerals: AlgebraicNumeral[] = [];
628
+ private freeAlgebraicIds: number[] = [];
629
+ private nextAlgebraicId = 0;
630
+ private algebraicManager: AlgebraicManager | undefined;
257
631
  private intNumerals = new Map<bigint, number>();
258
- // One more than the largest free de Bruijn index, so 0 means the expression is ground.
259
632
  freeVarBounds = new Int32Array(1024);
260
633
  hasQuantifierFlags = new Uint8Array(1024);
634
+ hasLabelFlags = new Uint8Array(1024);
261
635
  depths = new Int32Array(1024);
262
- // Z3's hash of each expression, which orders Z3's hash tables.
263
636
  hashes = new Uint32Array(1024);
264
637
  quantifiers = new Map<number, QuantifierData>();
265
- // The marks that the rewriters of this manager share to flatten conjunctions and disjunctions.
266
638
  readonly flatLiterals = new FlatLiterals(this);
267
639
  funcDecls: FuncDecl[] = [];
268
640
  sortNames: string[] = ["Bool", "Int", "Real"];
641
+ private sortFamilies: number[] = [FAMILY_BASIC, FAMILY_ARITH, FAMILY_ARITH];
642
+ private sortWidths: number[] = [0, 0, 0];
643
+ private sortSbits: number[] = [0, 0, 0];
644
+ private sortParameters: number[][] = [[], [], []];
645
+ private sortSizes: bigint[] = [2n, SORT_SIZE_INFINITE, SORT_SIZE_INFINITE];
646
+ private arraySorts = new Map<string, number>();
647
+ private arrayDecls = new Map<string, number>();
648
+ readonly datatypeDefs = new Map<string, DatatypeDef>();
649
+ hasNestedRecDatatype = false;
650
+ datatypeConstructors: number[][] = [];
651
+ constructorAccessors: number[][] = [];
652
+ constructorRecognizers: number[] = [];
653
+ nonRecConstructors: number[] = [];
654
+ nonRecDepths: number[] = [];
655
+ private parametricSorts = new Map<string, number>();
656
+ private datatypeDecls = new Map<string, number>();
657
+ private userSortKinds = new Map<SmtSymbol, number>();
658
+ private sortSmallIds: number[] = [];
659
+ private typeParameters: number[] = [];
660
+ private numUserSorts = 0;
661
+ private bvSorts = new Map<number, number>();
662
+ private bvDecls = new Map<string, number>();
663
+ private fpSorts = new Map<string, number>();
664
+ private rmSort = -1;
665
+ private fpaDecls = new Map<string, number>();
666
+ private labelDecls = new Map<string, number>();
667
+ private pbDecls = new Map<string, number>();
668
+ // The parameters of a pseudo-Boolean declaration: its bound, then a coefficient per argument.
669
+ private pbParameters = new Map<number, Rational[]>();
670
+ // The names of the labels, which the parameters of a label declaration index.
671
+ private labelNames: string[] = [];
672
+ private labelNameIds = new Map<string, number>();
673
+ private fpValues: Mpf[] = [];
674
+ private fpValueIds = new Map<string, number>();
675
+ private charSort = -1;
676
+ private stringSort = -1;
677
+ private reglanSort = -1;
678
+ private seqSorts = new Map<string, number>();
679
+ private seqDecls = new Map<string, number>();
680
+ private stringValues: number[][] = [];
681
+ private stringValueIds = new Map<string, number>();
269
682
  private sortHashes = [
270
683
  combineHash(getSymbolHash("Bool"), getInfoHash(Z3_BASIC_FAMILY, 0, [])),
271
684
  combineHash(getSymbolHash("Int"), getInfoHash(Z3_ARITH_FAMILY, 1, [])),
272
685
  combineHash(getSymbolHash("Real"), getInfoHash(Z3_ARITH_FAMILY, 0, [])),
273
686
  ];
274
- // The hash of each uninterpreted declaration, and of each builtin by its Z3 declaration.
275
687
  private declHashes: number[] = [];
276
- // The hash of each builtin declaration other than pattern by op, argument sort, range and number
277
- // of arguments: open addressing with linear probing, with the four keys of slot i at 4 i in
278
- // builtinKeys and -1 for an empty slot.
279
688
  private builtinKeys = new Int32Array(4 * 256).fill(-1);
280
689
  private builtinHashes = new Uint32Array(256);
281
690
  private numBuiltinHashes = 0;
282
- // The hash of each pattern declaration by its domain and range.
283
691
  private patternHashes = new Map<string, number>();
284
692
  private table = new Map<string, number>();
285
- // The hash-consing table of applications: open addressing with linear probing over the handles,
286
- // -1 for an empty slot, which takes a few bytes per application where a map of buckets takes
287
- // dozens. Applications stay in it when they are deleted, so a lookup can create them again.
288
693
  private appHandles = new Int32Array(1024).fill(-1);
289
694
  private appHashes = new Int32Array(1024);
290
695
  private numApps = 0;
291
- // id_gen for sorts and declarations: the small id of the next one, and the ids of deleted ones,
292
- // which Z3 reuses the last freed first. Z3 keeps the declarations of = and ite, and deletes the
293
- // others with their last reference.
294
696
  private nextSmallId = FIRST_DECLARED_SMALL_ID;
295
697
  private freeSmallIds: number[] = [];
296
698
  private numDecls = FIRST_DECLARED_SMALL_ID;
297
- // The small id and the number of live applications of each builtin declaration that Z3 makes
298
- // per signature, by its signature.
299
699
  private signatureDecls = new Map<number | string, SignatureDecl>();
300
- // The references to each user declaration: the script holds the declared ones, and applications
301
- // and models hold the fresh ones. A deleted declaration keeps its small id here, in case an
302
- // application of it is created again.
303
700
  private declRefCounts: number[] = [];
304
- // The named expressions of the recfun plugin, by declaration.
305
701
  readonly macros = new ObjMap<number>((decl) => this.declHashes[decl]);
702
+ // The recfun plugin's definitions by declaration, and their cases by case predicate.
703
+ readonly recFunDefs = new ObjMap<RecFunDef>((decl) => this.declHashes[decl]);
704
+ readonly recFunCases = new Map<number, RecFunCase>();
705
+ hasRecDefs = false;
706
+ private numRoundsDecls = new Map<number, number>();
306
707
  private deletedDecls = new Set<number>();
307
- // ast_manager::m_fresh_id, which numbers fresh declarations and variable names.
308
708
  private freshId = 0;
309
- // Z3's id of each expression, which orders Z3's sorts. Like Z3, the manager counts references and
310
- // gives the id of a deleted expression to the next expression it creates, so its ids follow
311
- // Z3's. A deleted expression stays in the tables with the id -1 until it is created again.
312
709
  ids = new Int32Array(1024);
313
710
  private refCounts = new Int32Array(1024);
314
711
  private freeIds: number[] = [];
315
712
  private nextId = 1;
316
713
  private numLive = 0;
317
- // The expressions that the open withTemporaries hold, like the expr_ref temporaries of Z3
318
- // functions on the C++ stack: the innermost holds those from temporariesMark on, and -1 marks that
319
- // no temporaries are held.
320
714
  private temporaries: number[] = [];
321
715
  private temporariesMark = -1;
322
- // The arguments mkApp copies out of a caller's array that starts past index 0.
323
716
  private argBuffer: number[] = [];
324
- // m_small_ints and m_small_reals: the numerals below MAX_SMALL_NUM_TO_CACHE, which are held.
325
717
  private smallInts: number[] = [];
326
718
  private smallReals: number[] = [];
327
- private isFiniteSetPluginInitialized = false;
719
+ // The finite set plugin makes the only type variables, so this is set once it is initialized.
720
+ hasTypeVars = false;
328
721
  readonly trueExpr: number;
329
722
  readonly falseExpr: number;
723
+ readonly piExpr: number;
724
+ readonly eulerExpr: number;
725
+ isHiDiv0 = true;
726
+ maxChar = 196607;
727
+ charBits = 18;
728
+ isSeqUsed = false;
729
+ isReUsed = false;
330
730
 
331
731
  constructor(readonly limit: ResourceLimit) {
332
732
  for (const builtin of BUILTIN_DECLS)
@@ -338,11 +738,16 @@ export class AstManager {
338
738
  range: builtin[3],
339
739
  isSkolem: false,
340
740
  smallId: Z3_SMALL_IDS.get(builtin[2]) ?? -1,
741
+ parameters: [],
341
742
  });
342
743
  this.trueExpr = this.mkApp(OP_TRUE, [], SORT_BOOL);
343
744
  this.falseExpr = this.mkApp(OP_FALSE, [], SORT_BOOL);
344
745
  this.incRef(this.trueExpr);
345
746
  this.incRef(this.falseExpr);
747
+ this.piExpr = this.mkApp(OP_PI, [], SORT_REAL);
748
+ this.eulerExpr = this.mkApp(OP_E, [], SORT_REAL);
749
+ this.incRef(this.piExpr);
750
+ this.incRef(this.eulerExpr);
346
751
  this.nextId = FIRST_FREE_ID;
347
752
  }
348
753
 
@@ -350,12 +755,10 @@ export class AstManager {
350
755
  return this.refCounts[expr];
351
756
  }
352
757
 
353
- // get_num_asts: the live expressions, declarations and sorts, with Z3's own expressions.
354
758
  getNumAsts() {
355
- return this.numLive + FIRST_FREE_ID - 2 + this.numDecls;
759
+ return this.numLive + FIRST_FREE_ID - 4 + this.numDecls;
356
760
  }
357
761
 
358
- // The declaration that a held signature names.
359
762
  private getSignatureDecl(key: number | string) {
360
763
  const signatureDecl = this.signatureDecls.get(key);
361
764
  if (signatureDecl === undefined)
@@ -368,20 +771,14 @@ export class AstManager {
368
771
  return this.freeSmallIds.pop() ?? this.nextSmallId++;
369
772
  }
370
773
 
371
- // finite_set_decl_plugin::init, which the first final check of theory_finite_set runs when it
372
- // makes a set.range declaration: the plugin makes the sorts A and B, three FiniteSet sorts and two
373
- // Array sorts, which the manager keeps. The set.range declaration is released at once.
374
774
  initFiniteSetPlugin() {
375
- if (this.isFiniteSetPluginInitialized) return;
376
- this.isFiniteSetPluginInitialized = true;
775
+ if (this.hasTypeVars) return;
776
+ this.hasTypeVars = true;
377
777
  for (let index = 0; index < NUM_FINITE_SET_PLUGIN_SORTS; index++) this.mkSmallId();
378
778
  this.freeSmallIds.push(this.mkSmallId());
379
779
  this.numDecls--;
380
780
  }
381
781
 
382
- // insert_macro: cmd_context keeps a named expression as a recursive function with one case, whose
383
- // declaration and case predicate it holds. The recfun plugin maps the declaration to the
384
- // expression, which a model of the context takes as the declaration's interpretation.
385
782
  mkMacro(name: string, expr: number) {
386
783
  const decl = this.funcDecls.length;
387
784
  this.declHashes[decl] = this.getFuncDeclHash(
@@ -398,29 +795,93 @@ export class AstManager {
398
795
  range: this.sorts[expr],
399
796
  isSkolem: false,
400
797
  smallId: this.mkSmallId(),
798
+ parameters: [],
401
799
  });
402
800
  this.declRefCounts[decl] = 1;
403
801
  this.mkSmallId();
404
802
  this.macros.set(decl, expr);
405
803
  }
406
804
 
407
- // The signature of a builtin declaration that Z3 makes on first use, or null for a declaration
408
- // that is not one. The low three bits of an integer signature tell its kind: an equality or ite
409
- // by sort, a division by zero or model value by declaration, or a numeral by its value, which a
410
- // numeral expression has alone. A distinct or pattern is keyed by its domain.
805
+ // recfun::def: the declaration of a defined function, which the definition holds.
806
+ mkRecFuncDecl(name: SmtSymbol, domain: number[], range: number) {
807
+ const decl = this.mkRecFunDecl(OP_FUN_DEFINED, name, [0], domain, range);
808
+ this.declRefCounts[decl] = 1;
809
+ this.recFunDefs.set(decl, { decl, rhs: -1, isMacro: false, cases: [] });
810
+ return decl;
811
+ }
812
+
813
+ // case_def: the predicate of a case, which the case holds.
814
+ mkRecFunCasePredDecl(def: number, index: number) {
815
+ const decl = this.mkRecFunDecl(
816
+ OP_FUN_CASE_PRED,
817
+ "case-def",
818
+ [index, def],
819
+ this.funcDecls[def].domain,
820
+ SORT_BOOL,
821
+ );
822
+ this.declRefCounts[decl] = 1;
823
+ return decl;
824
+ }
825
+
826
+ // mk_num_rounds_pred: Z3 makes the declaration again once its last application is deleted.
827
+ mkNumRoundsPred(round: number) {
828
+ let decl = this.numRoundsDecls.get(round);
829
+ if (decl === undefined) {
830
+ decl = this.mkRecFunDecl(OP_NUM_ROUNDS, "recfun-num-rounds", [round], [], SORT_BOOL);
831
+ this.declRefCounts[decl] = 0;
832
+ this.numRoundsDecls.set(round, decl);
833
+ }
834
+ return this.mkApp(decl, [], SORT_BOOL);
835
+ }
836
+
837
+ private mkRecFunDecl(
838
+ op: number,
839
+ name: SmtSymbol,
840
+ parameters: number[],
841
+ domain: number[],
842
+ range: number,
843
+ ) {
844
+ const decl = this.funcDecls.length;
845
+ this.declHashes[decl] = this.getFuncDeclHash(
846
+ name,
847
+ domain,
848
+ range,
849
+ getInfoHash(
850
+ Z3_RECFUN_FAMILY,
851
+ op - RECFUN_OP_BASE,
852
+ op === OP_FUN_CASE_PRED ? [parameters[0], this.declHashes[parameters[1]]] : parameters,
853
+ ),
854
+ );
855
+ this.funcDecls.push({
856
+ name,
857
+ family: FAMILY_RECFUN,
858
+ op,
859
+ domain,
860
+ range,
861
+ isSkolem: false,
862
+ smallId: this.mkSmallId(),
863
+ parameters,
864
+ });
865
+ return decl;
866
+ }
867
+
411
868
  private getSignature(expr: number) {
412
869
  const decl = this.decls[expr];
413
- if (decl === OP_NUM) return 8 * this.values[expr] + SIGNATURE_NUMERAL;
870
+ if (decl === OP_NUM || this.funcDecls[decl].op === OP_BV_NUM)
871
+ return 8 * this.values[expr] + SIGNATURE_NUMERAL;
414
872
  if (decl === OP_EQ) return 8 * this.sorts[this.getArg(expr, 0)] + SIGNATURE_EQ;
415
873
  if (decl === OP_ITE) return 8 * this.sorts[expr] + SIGNATURE_ITE;
416
874
  if (decl === OP_DIV0 || decl === OP_IDIV0 || decl === OP_MOD0) return 8 * decl + SIGNATURE_DIV0;
875
+ if (decl === OP_POWER0)
876
+ return 8 * (decl + 64 * this.sorts[this.getArg(expr, 0)]) + SIGNATURE_DIV0;
877
+ if (decl === OP_ALGEBRAIC_NUM) return 8 * this.getAlgebraicId(expr) + SIGNATURE_ALGEBRAIC;
417
878
  if (decl === OP_DISTINCT || decl === OP_PATTERN)
418
879
  return `${decl}:${this.mapArgs(expr, (arg) => this.sorts[arg]).join(",")}`;
419
880
  if (this.funcDecls[decl].family === FAMILY_MODEL_VALUE) return 8 * decl + SIGNATURE_MODEL_VALUE;
881
+ if (UNCACHED_BV_OPS.has(this.funcDecls[decl].op)) return `bv${decl}`;
420
882
  return null;
421
883
  }
422
884
 
423
- // The id that Z3 gives the declaration of an application.
424
885
  getDeclId(expr: number) {
425
886
  const signature = this.getSignature(expr);
426
887
  return signature === null
@@ -428,10 +889,9 @@ export class AstManager {
428
889
  : this.getSignatureDecl(signature).smallId;
429
890
  }
430
891
 
431
- // An application holds its declaration, which Z3 makes with its first application.
432
892
  private holdDecl(expr: number) {
433
893
  const decl = this.decls[expr];
434
- if (this.funcDecls[decl].family === FAMILY_USER) {
894
+ if (isDeclaredFamily(this.funcDecls[decl].family)) {
435
895
  this.incDeclRef(decl);
436
896
  return;
437
897
  }
@@ -443,17 +903,14 @@ export class AstManager {
443
903
  return;
444
904
  }
445
905
  const smallId = this.mkSmallId();
446
- // Z3 keeps the equality and ite declarations once it makes them.
447
906
  this.signatureDecls.set(key, { smallId, refCount: decl === OP_EQ || decl === OP_ITE ? 2 : 1 });
448
907
  if (decl !== OP_NUM && this.funcDecls[decl].family === FAMILY_MODEL_VALUE)
449
908
  this.funcDecls[decl].smallId = smallId;
450
909
  }
451
910
 
452
- // The small id of the declaration a deleted application releases for good, or -1 when the
453
- // declaration stays.
454
911
  private releaseDecl(expr: number) {
455
912
  const decl = this.decls[expr];
456
- if (this.funcDecls[decl].family === FAMILY_USER) {
913
+ if (isDeclaredFamily(this.funcDecls[decl].family)) {
457
914
  this.declRefCounts[decl]--;
458
915
  if (this.declRefCounts[decl] > 0) return -1;
459
916
  this.deletedDecls.add(decl);
@@ -472,7 +929,6 @@ export class AstManager {
472
929
  this.refCounts[expr]++;
473
930
  }
474
931
 
475
- // A model value's declaration is counted under its signature, like the applications that hold it.
476
932
  incDeclRef(decl: number) {
477
933
  if (this.funcDecls[decl].family === FAMILY_MODEL_VALUE) {
478
934
  const key = 8 * decl + SIGNATURE_MODEL_VALUE;
@@ -508,9 +964,6 @@ export class AstManager {
508
964
  this.numDecls--;
509
965
  }
510
966
 
511
- // Runs body, holding each expression it creates until it returns, then releases them, the last
512
- // created first. The caller gets a reference to each expression that getKept picks from the
513
- // result, by default the result itself, and releases it.
514
967
  withTemporaries<T>(
515
968
  body: () => T,
516
969
  getKept: (result: T) => number[] = (result) => [result as number],
@@ -522,8 +975,6 @@ export class AstManager {
522
975
  return result;
523
976
  }
524
977
 
525
- // withTemporaries without closures, for the hot paths: the caller passes what beginTemporaries
526
- // returns to endTemporaries, and takes its references before it.
527
978
  beginTemporaries() {
528
979
  const outer = this.temporariesMark;
529
980
  this.temporariesMark = this.temporaries.length;
@@ -539,7 +990,6 @@ export class AstManager {
539
990
  shrink(temporaries, mark);
540
991
  }
541
992
 
542
- // Runs body without temporaries, for code whose references are counted explicitly.
543
993
  withoutTemporaries<T>(body: () => T) {
544
994
  const outer = this.temporariesMark;
545
995
  this.temporariesMark = -1;
@@ -548,15 +998,11 @@ export class AstManager {
548
998
  return result;
549
999
  }
550
1000
 
551
- // Hands a reference the caller owns to the innermost withTemporaries, or releases it now.
552
1001
  releaseLater(expr: number) {
553
1002
  if (this.temporariesMark === -1) this.decRef(expr);
554
1003
  else this.temporaries.push(expr);
555
1004
  }
556
1005
 
557
- // dec_ref and delete_node: an expression without references is deleted, and so are its children
558
- // and declaration that lose their last reference, in the order of Z3's worklist. A released
559
- // declaration's small id goes on the worklist as -1 - id.
560
1006
  decRef(expr: number) {
561
1007
  if (this.ids[expr] === -1) return;
562
1008
  this.refCounts[expr]--;
@@ -571,10 +1017,10 @@ export class AstManager {
571
1017
  this.deleteNode(node);
572
1018
  if (this.kinds[node] === KIND_APP) {
573
1019
  const smallId = this.releaseDecl(node);
1020
+ if (this.decls[node] === OP_ALGEBRAIC_NUM)
1021
+ this.freeAlgebraicIds.push(this.getAlgebraicId(node));
574
1022
  if (smallId !== -1) worklist.push(-1 - smallId);
575
1023
  }
576
- // The children: the arguments of an application, and the body and then the patterns of a
577
- // quantifier.
578
1024
  if (this.kinds[node] !== KIND_QUANTIFIER) {
579
1025
  const end = this.argStarts[node + 1];
580
1026
  for (let index = this.argStarts[node]; index < end; index++)
@@ -584,6 +1030,7 @@ export class AstManager {
584
1030
  const data = this.getQuantifier(node);
585
1031
  this.releaseChild(data.body, worklist);
586
1032
  for (const pattern of data.patterns) this.releaseChild(pattern, worklist);
1033
+ for (const noPattern of data.noPatterns) this.releaseChild(noPattern, worklist);
587
1034
  }
588
1035
  }
589
1036
 
@@ -598,13 +1045,10 @@ export class AstManager {
598
1045
  this.ids[expr] = -1;
599
1046
  }
600
1047
 
601
- // register_node_core: a new expression takes an id and a reference to each child.
602
1048
  private registerNode(expr: number) {
603
1049
  this.ids[expr] = this.freeIds.pop() ?? this.nextId++;
604
1050
  this.numLive++;
605
1051
  this.refCounts[expr] = 0;
606
- // The children: the arguments of an application, and the body and then the patterns of a
607
- // quantifier.
608
1052
  if (this.kinds[expr] !== KIND_QUANTIFIER) {
609
1053
  const end = this.argStarts[expr + 1];
610
1054
  for (let index = this.argStarts[expr]; index < end; index++)
@@ -613,6 +1057,7 @@ export class AstManager {
613
1057
  const data = this.getQuantifier(expr);
614
1058
  this.incRef(this.revive(data.body));
615
1059
  for (const pattern of data.patterns) this.incRef(this.revive(pattern));
1060
+ for (const noPattern of data.noPatterns) this.incRef(this.revive(noPattern));
616
1061
  }
617
1062
  if (this.kinds[expr] === KIND_APP) this.holdDecl(expr);
618
1063
  if (this.temporariesMark === -1) return;
@@ -620,7 +1065,6 @@ export class AstManager {
620
1065
  this.temporaries.push(expr);
621
1066
  }
622
1067
 
623
- // The arguments of an application, and the body of a quantifier as its one argument.
624
1068
  getNumArgs(expr: number) {
625
1069
  return this.argStarts[expr + 1] - this.argStarts[expr];
626
1070
  }
@@ -629,20 +1073,17 @@ export class AstManager {
629
1073
  return this.argPool[this.argStarts[expr] + index];
630
1074
  }
631
1075
 
632
- // Appends the arguments to target, for a caller that needs them as an array.
633
1076
  pushArgs(expr: number, target: number[]) {
634
1077
  const end = this.argStarts[expr + 1];
635
1078
  for (let index = this.argStarts[expr]; index < end; index++) target.push(this.argPool[index]);
636
1079
  }
637
1080
 
638
- // A new array of the arguments.
639
1081
  getArgs(expr: number) {
640
1082
  const args: number[] = [];
641
1083
  this.pushArgs(expr, args);
642
1084
  return args;
643
1085
  }
644
1086
 
645
- // The callbacks may create expressions, which can replace argPool, so each argument is read anew.
646
1087
  mapArgs<T>(expr: number, map: (arg: number, index: number) => T) {
647
1088
  const result: T[] = [];
648
1089
  for (let index = 0; index < this.getNumArgs(expr); index++)
@@ -662,25 +1103,656 @@ export class AstManager {
662
1103
  return true;
663
1104
  }
664
1105
 
665
- // A lookup that finds a deleted expression creates it again.
666
1106
  private revive(expr: number) {
667
- if (this.ids[expr] === -1) this.registerNode(expr);
1107
+ if (this.ids[expr] !== -1) return expr;
1108
+ if (this.decls[expr] === OP_ALGEBRAIC_NUM)
1109
+ this.algebraicNumerals[this.values[expr]].id = this.mkAlgebraicId();
1110
+ this.registerNode(expr);
668
1111
  return expr;
669
1112
  }
670
1113
 
671
- mkSort(name: string) {
1114
+ mkSort(name: SmtSymbol) {
672
1115
  this.sortHashes.push(
673
1116
  combineHash(
674
1117
  getSymbolHash(name),
675
- getInfoHash(Z3_USER_SORT_FAMILY, this.sortNames.length - 3, []),
1118
+ getInfoHash(Z3_USER_SORT_FAMILY, this.getUserSortKind(name), []),
676
1119
  ),
677
1120
  );
678
- this.sortNames.push(name);
1121
+ this.sortNames.push(getSymbolName(name));
1122
+ this.sortFamilies.push(FAMILY_USER);
1123
+ this.sortWidths.push(0);
1124
+ this.sortSbits.push(0);
1125
+ this.sortParameters.push([]);
1126
+ this.sortSizes.push(SORT_SIZE_INFINITE);
679
1127
  this.mkSmallId();
680
1128
  return this.sortNames.length - 1;
681
1129
  }
682
1130
 
683
- // The hash of a func_decl node: its name, range, info and domain.
1131
+ mkBvSort(width: number) {
1132
+ const existing = this.bvSorts.get(width);
1133
+ if (existing !== undefined) return existing;
1134
+ this.sortHashes.push(combineHash(getSymbolHash("bv"), getInfoHash(Z3_BV_FAMILY, 0, [width])));
1135
+ this.sortNames.push("bv");
1136
+ this.sortFamilies.push(FAMILY_BV);
1137
+ this.sortWidths.push(width);
1138
+ this.sortSbits.push(0);
1139
+ this.sortParameters.push([]);
1140
+ this.sortSizes.push(width < 64 ? 1n << BigInt(width) : SORT_SIZE_VERY_BIG);
1141
+ // bv_decl_plugin makes the sorts up to 64 bits when Z3 starts.
1142
+ if (width > 64) this.mkSmallId();
1143
+ const sort = this.sortNames.length - 1;
1144
+ this.bvSorts.set(width, sort);
1145
+ return sort;
1146
+ }
1147
+
1148
+ mkFpSort(ebits: number, sbits: number) {
1149
+ const key = `${ebits}:${sbits}`;
1150
+ const existing = this.fpSorts.get(key);
1151
+ if (existing !== undefined) return existing;
1152
+ this.sortHashes.push(
1153
+ combineHash(
1154
+ getSymbolHash("FloatingPoint"),
1155
+ getInfoHash(Z3_FPA_FAMILY, Z3_FLOATING_POINT_SORT, [ebits, sbits]),
1156
+ ),
1157
+ );
1158
+ this.sortNames.push("FloatingPoint");
1159
+ this.sortFamilies.push(FAMILY_FPA);
1160
+ this.sortWidths.push(ebits);
1161
+ this.sortSbits.push(sbits);
1162
+ this.sortParameters.push([]);
1163
+ this.sortSizes.push(SORT_SIZE_VERY_BIG);
1164
+ this.mkSmallId();
1165
+ const sort = this.sortNames.length - 1;
1166
+ this.fpSorts.set(key, sort);
1167
+ return sort;
1168
+ }
1169
+
1170
+ mkRmSort() {
1171
+ if (this.rmSort !== -1) return this.rmSort;
1172
+ this.sortHashes.push(
1173
+ combineHash(
1174
+ getSymbolHash("RoundingMode"),
1175
+ getInfoHash(Z3_FPA_FAMILY, Z3_ROUNDING_MODE_SORT, []),
1176
+ ),
1177
+ );
1178
+ this.sortNames.push("RoundingMode");
1179
+ this.sortFamilies.push(FAMILY_FPA);
1180
+ this.sortWidths.push(0);
1181
+ this.sortSbits.push(0);
1182
+ this.sortParameters.push([]);
1183
+ this.sortSizes.push(SORT_SIZE_INFINITE);
1184
+ this.mkSmallId();
1185
+ this.rmSort = this.sortNames.length - 1;
1186
+ return this.rmSort;
1187
+ }
1188
+
1189
+ isFpSort(sort: number) {
1190
+ return this.sortFamilies[sort] === FAMILY_FPA && sort !== this.rmSort;
1191
+ }
1192
+
1193
+ isRmSort(sort: number) {
1194
+ return sort === this.rmSort;
1195
+ }
1196
+
1197
+ getEbits(sort: number) {
1198
+ return this.sortWidths[sort];
1199
+ }
1200
+
1201
+ getSbits(sort: number) {
1202
+ return this.sortSbits[sort];
1203
+ }
1204
+
1205
+ mkFpaDecl(op: number, name: string, parameters: number[], domain: number[], range: number) {
1206
+ const key = `${op}:${name}:${parameters.join(",")}:${domain.join(",")}:${range}`;
1207
+ const existing = this.fpaDecls.get(key);
1208
+ if (existing !== undefined) return existing;
1209
+ const decl = this.funcDecls.length;
1210
+ this.declHashes[decl] = this.getFuncDeclHash(
1211
+ name,
1212
+ domain,
1213
+ range,
1214
+ getInfoHash(Z3_FPA_FAMILY, op - FPA_OP_BASE, parameters),
1215
+ );
1216
+ this.funcDecls.push({
1217
+ name,
1218
+ family: FAMILY_FPA,
1219
+ op,
1220
+ domain,
1221
+ range,
1222
+ isSkolem: false,
1223
+ smallId: this.mkSmallId(),
1224
+ parameters,
1225
+ });
1226
+ this.fpaDecls.set(key, decl);
1227
+ return decl;
1228
+ }
1229
+
1230
+ // pb_decl_plugin::mk_func_decl: a parameter that fits in 32 bits is an integer, else a rational.
1231
+ mkPbDecl(op: number, parameters: Rational[], domain: number[]) {
1232
+ const key = `${op}:${parameters.join(",")}:${domain.join(",")}`;
1233
+ const existing = this.pbDecls.get(key);
1234
+ if (existing !== undefined) return existing;
1235
+ const decl = this.funcDecls.length;
1236
+ const name = PB_OP_NAMES[op - PB_OP_BASE];
1237
+ this.declHashes[decl] = this.getFuncDeclHash(
1238
+ name,
1239
+ domain,
1240
+ SORT_BOOL,
1241
+ getInfoHash(
1242
+ Z3_PB_FAMILY,
1243
+ op - PB_OP_BASE,
1244
+ parameters.map((parameter) =>
1245
+ parameter.isInt() && parameter.num >= -(2n ** 31n) && parameter.num < 2n ** 31n
1246
+ ? Number(BigInt.asUintN(32, parameter.num))
1247
+ : (getIntegerHash(parameter.num) + 3 * getIntegerHash(parameter.den)) >>> 0,
1248
+ ),
1249
+ ),
1250
+ );
1251
+ this.funcDecls.push({
1252
+ name,
1253
+ family: FAMILY_PB,
1254
+ op,
1255
+ domain,
1256
+ range: SORT_BOOL,
1257
+ isSkolem: false,
1258
+ smallId: this.mkSmallId(),
1259
+ parameters: [],
1260
+ });
1261
+ this.pbParameters.set(decl, parameters);
1262
+ this.pbDecls.set(key, decl);
1263
+ return decl;
1264
+ }
1265
+
1266
+ getPbParameters(decl: number) {
1267
+ return this.pbParameters.get(decl) as Rational[];
1268
+ }
1269
+
1270
+ mkFpValueId(value: Mpf) {
1271
+ const key = `${value.ebits}:${value.sbits}:${value.sign}:${value.exponent}:${value.significand}`;
1272
+ const existing = this.fpValueIds.get(key);
1273
+ if (existing !== undefined) return existing;
1274
+ this.fpValues.push({ ...value });
1275
+ this.fpValueIds.set(key, this.fpValues.length - 1);
1276
+ return this.fpValues.length - 1;
1277
+ }
1278
+
1279
+ getFpValue(id: number): Readonly<Mpf> {
1280
+ return this.fpValues[id];
1281
+ }
1282
+
1283
+ private addSort(name: string, family: number, parameters: number[], hash: number) {
1284
+ this.sortHashes.push(hash);
1285
+ this.sortNames.push(name);
1286
+ this.sortFamilies.push(family);
1287
+ this.sortWidths.push(0);
1288
+ this.sortSbits.push(0);
1289
+ this.sortParameters.push(parameters);
1290
+ this.sortSizes.push(SORT_SIZE_INFINITE);
1291
+ return this.sortNames.length - 1;
1292
+ }
1293
+
1294
+ // Z3 makes the Char, String and RegLan sorts at startup, so they take no small id.
1295
+ mkCharSort() {
1296
+ if (this.charSort === -1)
1297
+ this.charSort = this.addSort(
1298
+ "Unicode",
1299
+ FAMILY_CHAR,
1300
+ [],
1301
+ combineHash(getSymbolHash("Unicode"), getInfoHash(Z3_CHAR_FAMILY, 0, [])),
1302
+ );
1303
+ return this.charSort;
1304
+ }
1305
+
1306
+ // String keeps its element out of its parameters, since it prints as a plain name.
1307
+ mkStringSort() {
1308
+ if (this.stringSort !== -1) return this.stringSort;
1309
+ const charHash = this.sortHashes[this.mkCharSort()];
1310
+ this.stringSort = this.addSort(
1311
+ "String",
1312
+ FAMILY_SEQ,
1313
+ [],
1314
+ combineHash(getSymbolHash("String"), getInfoHash(Z3_SEQ_FAMILY, Z3_SEQ_SORT, [charHash])),
1315
+ );
1316
+ return this.stringSort;
1317
+ }
1318
+
1319
+ mkSeqSort(element: number) {
1320
+ if (element === this.mkCharSort()) return this.mkStringSort();
1321
+ const key = `s${element}`;
1322
+ const existing = this.seqSorts.get(key);
1323
+ if (existing !== undefined) return existing;
1324
+ const sort = this.addSort(
1325
+ "Seq",
1326
+ FAMILY_SEQ,
1327
+ [element],
1328
+ combineHash(
1329
+ getSymbolHash("Seq"),
1330
+ getInfoHash(Z3_SEQ_FAMILY, Z3_SEQ_SORT, [this.sortHashes[element]]),
1331
+ ),
1332
+ );
1333
+ this.mkSmallId();
1334
+ this.seqSorts.set(key, sort);
1335
+ return sort;
1336
+ }
1337
+
1338
+ mkReSort(seq: number) {
1339
+ const isReglan = seq === this.mkStringSort();
1340
+ if (isReglan && this.reglanSort !== -1) return this.reglanSort;
1341
+ const key = `r${seq}`;
1342
+ const existing = this.seqSorts.get(key);
1343
+ if (existing !== undefined) return existing;
1344
+ const sort = this.addSort(
1345
+ "RegEx",
1346
+ FAMILY_SEQ,
1347
+ [seq],
1348
+ combineHash(
1349
+ getSymbolHash("RegEx"),
1350
+ getInfoHash(Z3_SEQ_FAMILY, Z3_RE_SORT, [this.sortHashes[seq]]),
1351
+ ),
1352
+ );
1353
+ if (isReglan) this.reglanSort = sort;
1354
+ else {
1355
+ this.mkSmallId();
1356
+ this.seqSorts.set(key, sort);
1357
+ }
1358
+ return sort;
1359
+ }
1360
+
1361
+ isCharSort(sort: number) {
1362
+ return sort === this.charSort;
1363
+ }
1364
+
1365
+ isStringSort(sort: number) {
1366
+ return sort === this.stringSort;
1367
+ }
1368
+
1369
+ isSeqSort(sort: number) {
1370
+ return this.sortFamilies[sort] === FAMILY_SEQ && this.sortNames[sort] !== "RegEx";
1371
+ }
1372
+
1373
+ isReSort(sort: number) {
1374
+ return this.sortFamilies[sort] === FAMILY_SEQ && this.sortNames[sort] === "RegEx";
1375
+ }
1376
+
1377
+ getSeqElementSort(sort: number) {
1378
+ return sort === this.stringSort ? this.charSort : this.sortParameters[sort][0];
1379
+ }
1380
+
1381
+ getReSeqSort(sort: number) {
1382
+ return this.sortParameters[sort][0];
1383
+ }
1384
+
1385
+ mkSeqDecl(
1386
+ op: number,
1387
+ name: string,
1388
+ parameters: number[],
1389
+ parameterHashes: number[],
1390
+ domain: number[],
1391
+ range: number,
1392
+ ) {
1393
+ const key = `${op}:${name}:${parameters.join(",")}:${domain.join(",")}:${range}`;
1394
+ const existing = this.seqDecls.get(key);
1395
+ if (existing !== undefined) return existing;
1396
+ const isChar = op >= CHAR_OP_BASE;
1397
+ const decl = this.funcDecls.length;
1398
+ this.declHashes[decl] = this.getFuncDeclHash(
1399
+ name,
1400
+ domain,
1401
+ range,
1402
+ getInfoHash(
1403
+ isChar ? Z3_CHAR_FAMILY : Z3_SEQ_FAMILY,
1404
+ op - (isChar ? CHAR_OP_BASE : SEQ_OP_BASE),
1405
+ parameterHashes,
1406
+ ),
1407
+ );
1408
+ this.funcDecls.push({
1409
+ name,
1410
+ family: isChar ? FAMILY_CHAR : FAMILY_SEQ,
1411
+ op,
1412
+ domain,
1413
+ range,
1414
+ isSkolem: false,
1415
+ smallId: this.mkSmallId(),
1416
+ parameters,
1417
+ });
1418
+ this.seqDecls.set(key, decl);
1419
+ return decl;
1420
+ }
1421
+
1422
+ mkStringValueId(chars: readonly number[]) {
1423
+ const key = chars.join(",");
1424
+ const existing = this.stringValueIds.get(key);
1425
+ if (existing !== undefined) return existing;
1426
+ this.stringValues.push([...chars]);
1427
+ this.stringValueIds.set(key, this.stringValues.length - 1);
1428
+ return this.stringValues.length - 1;
1429
+ }
1430
+
1431
+ getStringHash(id: number) {
1432
+ return bytesHash(new Uint8Array(Uint32Array.from(this.stringValues[id]).buffer), 23);
1433
+ }
1434
+
1435
+ getSortHash(sort: number) {
1436
+ return this.sortHashes[sort];
1437
+ }
1438
+
1439
+ mkString(chars: readonly number[]) {
1440
+ const id = this.mkStringValueId(chars);
1441
+ const sort = this.mkStringSort();
1442
+ const decl = this.mkSeqDecl(
1443
+ OP_STRING_CONST,
1444
+ "String",
1445
+ [id],
1446
+ [this.getStringHash(id)],
1447
+ [],
1448
+ sort,
1449
+ );
1450
+ return this.mkApp(decl, [], sort);
1451
+ }
1452
+
1453
+ getString(expr: number): readonly number[] | undefined {
1454
+ if (this.getOp(expr) !== OP_STRING_CONST) return undefined;
1455
+ return this.stringValues[this.funcDecls[this.decls[expr]].parameters[0]];
1456
+ }
1457
+
1458
+ mkChar(code: number) {
1459
+ const sort = this.mkCharSort();
1460
+ return this.mkApp(this.mkSeqDecl(OP_CHAR_CONST, "Char", [code], [code], [], sort), [], sort);
1461
+ }
1462
+
1463
+ getChar(expr: number) {
1464
+ return this.getOp(expr) === OP_CHAR_CONST ? this.funcDecls[this.decls[expr]].parameters[0] : -1;
1465
+ }
1466
+
1467
+ mkArraySort(parameters: number[]) {
1468
+ const key = parameters.join(",");
1469
+ const existing = this.arraySorts.get(key);
1470
+ if (existing !== undefined) return existing;
1471
+ this.sortHashes.push(
1472
+ combineHash(
1473
+ getSymbolHash("Array"),
1474
+ getInfoHash(
1475
+ Z3_ARRAY_FAMILY,
1476
+ 0,
1477
+ parameters.map((parameter) => this.sortHashes[parameter]),
1478
+ ),
1479
+ ),
1480
+ );
1481
+ this.sortNames.push("Array");
1482
+ this.sortFamilies.push(FAMILY_ARRAY);
1483
+ this.sortWidths.push(0);
1484
+ this.sortSbits.push(0);
1485
+ this.sortParameters.push(parameters);
1486
+ this.sortSizes.push(this.getArraySortSize(parameters));
1487
+ this.mkSmallId();
1488
+ const sort = this.sortNames.length - 1;
1489
+ this.arraySorts.set(key, sort);
1490
+ return sort;
1491
+ }
1492
+
1493
+ private getArraySortSize(parameters: number[]) {
1494
+ const sizes = parameters.map((parameter) => this.sortSizes[parameter]);
1495
+ if (sizes[sizes.length - 1] === 1n) return 1n;
1496
+ if (sizes.includes(SORT_SIZE_INFINITE)) return SORT_SIZE_INFINITE;
1497
+ if (sizes.includes(SORT_SIZE_VERY_BIG)) return SORT_SIZE_VERY_BIG;
1498
+ const domainSize = sizes.slice(0, -1).reduce((product, size) => product * size, 1n);
1499
+ if (domainSize > 128n) return SORT_SIZE_VERY_BIG;
1500
+ const size = sizes[sizes.length - 1] ** domainSize;
1501
+ return size >= 1n << 64n ? SORT_SIZE_VERY_BIG : size;
1502
+ }
1503
+
1504
+ isArraySort(sort: number) {
1505
+ return this.sortFamilies[sort] === FAMILY_ARRAY;
1506
+ }
1507
+
1508
+ getSortParameters(sort: number): readonly number[] {
1509
+ return this.sortParameters[sort];
1510
+ }
1511
+
1512
+ getArrayArity(sort: number) {
1513
+ return this.sortParameters[sort].length - 1;
1514
+ }
1515
+
1516
+ getArrayRange(sort: number) {
1517
+ const parameters = this.sortParameters[sort];
1518
+ return parameters[parameters.length - 1];
1519
+ }
1520
+
1521
+ getSortName(sort: number): string {
1522
+ if (this.isBvSort(sort)) return `(_ BitVec ${this.getBvSize(sort)})`;
1523
+ if (this.isFpSort(sort))
1524
+ return `(_ FloatingPoint ${this.getEbits(sort)} ${this.getSbits(sort)})`;
1525
+ const parameters = this.sortParameters[sort].map((parameter) => this.getSortName(parameter));
1526
+ if (parameters.length === 0) return this.sortNames[sort];
1527
+ return `(${this.sortNames[sort]} ${parameters.join(" ")})`;
1528
+ }
1529
+
1530
+ getSortSize(sort: number) {
1531
+ return this.sortSizes[sort];
1532
+ }
1533
+
1534
+ mkArrayDecl(op: number, name: string, parameters: number[], domain: number[], range: number) {
1535
+ const key = `${op}:${parameters.join(",")}:${domain.join(",")}`;
1536
+ const existing = this.arrayDecls.get(key);
1537
+ if (existing !== undefined) return existing;
1538
+ const decl = this.funcDecls.length;
1539
+ const parameterHashes = parameters.map((parameter) =>
1540
+ op === OP_CONST_ARRAY
1541
+ ? this.sortHashes[parameter]
1542
+ : op === OP_ARRAY_MAP || op === OP_AS_ARRAY
1543
+ ? // A builtin declaration hashes by the signature of each application, which a map lacks.
1544
+ (this.getDeclHash(parameter) ?? 0)
1545
+ : parameter,
1546
+ );
1547
+ this.declHashes[decl] = this.getFuncDeclHash(
1548
+ name,
1549
+ domain,
1550
+ range,
1551
+ getInfoHash(Z3_ARRAY_FAMILY, op - ARRAY_OP_BASE, parameterHashes),
1552
+ );
1553
+ this.funcDecls.push({
1554
+ name,
1555
+ family: FAMILY_ARRAY,
1556
+ op,
1557
+ domain,
1558
+ range,
1559
+ isSkolem: false,
1560
+ smallId: this.mkSmallId(),
1561
+ parameters,
1562
+ });
1563
+ this.arrayDecls.set(key, decl);
1564
+ return decl;
1565
+ }
1566
+
1567
+ private mkParametricSort(
1568
+ family: number,
1569
+ name: SmtSymbol,
1570
+ parameters: number[],
1571
+ infoHash: number,
1572
+ ) {
1573
+ const key = `${family}:${typeof name}:${name}:${parameters.join(",")}`;
1574
+ const existing = this.parametricSorts.get(key);
1575
+ if (existing !== undefined) return existing;
1576
+ this.sortHashes.push(combineHash(getSymbolHash(name), infoHash));
1577
+ this.sortNames.push(getSymbolName(name));
1578
+ this.sortFamilies.push(family);
1579
+ this.sortWidths.push(0);
1580
+ this.sortSbits.push(0);
1581
+ this.sortParameters.push(parameters);
1582
+ this.sortSizes.push(SORT_SIZE_INFINITE);
1583
+ const sort = this.sortNames.length - 1;
1584
+ this.sortSmallIds[sort] = this.mkSmallId();
1585
+ this.parametricSorts.set(key, sort);
1586
+ return sort;
1587
+ }
1588
+
1589
+ private getUserSortKind(name: SmtSymbol) {
1590
+ const existing = this.userSortKinds.get(name);
1591
+ if (existing !== undefined) return existing;
1592
+ this.userSortKinds.set(name, this.numUserSorts);
1593
+ return this.numUserSorts++;
1594
+ }
1595
+
1596
+ mkUninterpretedSort(name: SmtSymbol, parameters: number[]) {
1597
+ return this.mkParametricSort(
1598
+ FAMILY_USER,
1599
+ name,
1600
+ parameters,
1601
+ getInfoHash(
1602
+ Z3_USER_SORT_FAMILY,
1603
+ this.getUserSortKind(name),
1604
+ parameters.map((parameter) => this.sortHashes[parameter]),
1605
+ ),
1606
+ );
1607
+ }
1608
+
1609
+ mkDatatypeSort(name: string, parameters: number[]) {
1610
+ return this.mkParametricSort(
1611
+ FAMILY_DATATYPE,
1612
+ name,
1613
+ parameters,
1614
+ getInfoHash(Z3_DATATYPE_FAMILY, 0, [
1615
+ getSymbolHash(name),
1616
+ ...parameters.map((parameter) => this.sortHashes[parameter]),
1617
+ ]),
1618
+ );
1619
+ }
1620
+
1621
+ setSortSize(sort: number, size: bigint) {
1622
+ this.sortSizes[sort] = size;
1623
+ }
1624
+
1625
+ // The sort named by the number index, which stands for a parameter of a datatype declaration.
1626
+ mkTypeParameter(index: number) {
1627
+ const sort = this.mkUninterpretedSort(index, []);
1628
+ this.typeParameters[index] = sort;
1629
+ return sort;
1630
+ }
1631
+
1632
+ getTypeParameterIndex(sort: number) {
1633
+ return this.typeParameters.indexOf(sort);
1634
+ }
1635
+
1636
+ getSortSmallId(sort: number) {
1637
+ return this.sortSmallIds[sort];
1638
+ }
1639
+
1640
+ isDatatypeSort(sort: number) {
1641
+ return this.sortFamilies[sort] === FAMILY_DATATYPE;
1642
+ }
1643
+
1644
+ // The parameters are those of the port, and parameterHashes hash Z3's.
1645
+ mkDatatypeDecl(
1646
+ op: number,
1647
+ name: string,
1648
+ parameters: number[],
1649
+ parameterHashes: number[],
1650
+ domain: number[],
1651
+ range: number,
1652
+ ) {
1653
+ const key = `${op}:${name}:${parameters.join(",")}:${domain.join(",")}:${range}`;
1654
+ const existing = this.datatypeDecls.get(key);
1655
+ if (existing !== undefined) return existing;
1656
+ const decl = this.funcDecls.length;
1657
+ this.declHashes[decl] = this.getFuncDeclHash(
1658
+ name,
1659
+ domain,
1660
+ range,
1661
+ getInfoHash(Z3_DATATYPE_FAMILY, op - DATATYPE_OP_BASE, parameterHashes),
1662
+ );
1663
+ this.funcDecls.push({
1664
+ name,
1665
+ family: FAMILY_DATATYPE,
1666
+ op,
1667
+ domain,
1668
+ range,
1669
+ isSkolem: false,
1670
+ smallId: this.mkSmallId(),
1671
+ parameters,
1672
+ });
1673
+ this.datatypeDecls.set(key, decl);
1674
+ return decl;
1675
+ }
1676
+
1677
+ getBvSize(sort: number) {
1678
+ return this.sortWidths[sort];
1679
+ }
1680
+
1681
+ isBvSort(sort: number) {
1682
+ return this.sortFamilies[sort] === FAMILY_BV;
1683
+ }
1684
+
1685
+ mkBvDecl(op: number, name: string, parameters: number[], domain: number[], range: number) {
1686
+ const key = `${op}:${parameters.join(",")}:${domain.join(",")}`;
1687
+ const existing = this.bvDecls.get(key);
1688
+ if (existing !== undefined) return existing;
1689
+ const decl = this.funcDecls.length;
1690
+ this.declHashes[decl] = this.getFuncDeclHash(
1691
+ name,
1692
+ domain,
1693
+ range,
1694
+ getInfoHash(Z3_BV_FAMILY, op - BV_OP_BASE, parameters),
1695
+ );
1696
+ this.funcDecls.push({
1697
+ name,
1698
+ family: FAMILY_BV,
1699
+ op,
1700
+ domain,
1701
+ range,
1702
+ isSkolem: false,
1703
+ smallId: UNCACHED_BV_OPS.has(op) ? -1 : this.mkSmallId(),
1704
+ parameters,
1705
+ });
1706
+ this.bvDecls.set(key, decl);
1707
+ return decl;
1708
+ }
1709
+
1710
+ mkBvNumeral(value: bigint, width: number) {
1711
+ const modulus = 1n << BigInt(width);
1712
+ const normalized = ((value % modulus) + modulus) % modulus;
1713
+ const sort = this.mkBvSort(width);
1714
+ const key = `b${width}:${normalized}`;
1715
+ const existing = this.table.get(key);
1716
+ if (existing !== undefined) return this.revive(existing);
1717
+ let decl = this.bvDecls.get(`n${width}`);
1718
+ if (decl === undefined) {
1719
+ decl = this.funcDecls.length;
1720
+ this.funcDecls.push({
1721
+ name: "bv",
1722
+ family: FAMILY_BV,
1723
+ op: OP_BV_NUM,
1724
+ domain: [],
1725
+ range: sort,
1726
+ isSkolem: false,
1727
+ smallId: -1,
1728
+ parameters: [width],
1729
+ });
1730
+ this.bvDecls.set(`n${width}`, decl);
1731
+ }
1732
+ this.numeralValues.push(Rational.of(normalized));
1733
+ const numeralDecl = decl;
1734
+ return this.intern(key, KIND_APP, numeralDecl, [], sort, this.numeralValues.length - 1, () =>
1735
+ this.getFuncDeclHash(
1736
+ "bv",
1737
+ [],
1738
+ sort,
1739
+ getInfoHash(Z3_BV_FAMILY, 0, [(getIntegerHash(normalized) + 3) >>> 0, width]),
1740
+ ),
1741
+ );
1742
+ }
1743
+
1744
+ isBvNumeral(expr: number) {
1745
+ return this.getOp(expr) === OP_BV_NUM;
1746
+ }
1747
+
1748
+ getBvNumeralValue(expr: number) {
1749
+ return this.numeralValues[this.values[expr]].num;
1750
+ }
1751
+
1752
+ getSortFamily(sort: number) {
1753
+ return this.sortFamilies[sort];
1754
+ }
1755
+
684
1756
  private getFuncDeclHash(name: SmtSymbol, domain: number[], range: number, infoHash: number) {
685
1757
  return astArrayHash(
686
1758
  domain,
@@ -689,7 +1761,6 @@ export class AstManager {
689
1761
  );
690
1762
  }
691
1763
 
692
- // The name, Z3 decl kind and domain of the Z3 declaration of a builtin application.
693
1764
  getBuiltinSignature(op: number, args: number[]) {
694
1765
  const builtin = Z3_BUILTINS.get(op);
695
1766
  if (builtin === undefined)
@@ -706,7 +1777,6 @@ export class AstManager {
706
1777
  return { name: builtin[0], kind: builtin[1], domain };
707
1778
  }
708
1779
 
709
- // The hash of a pattern's Z3 declaration.
710
1780
  private getPatternHash(args: number[], range: number) {
711
1781
  const key = `${args.map((arg) => this.sorts[arg]).join(",")}:${range}`;
712
1782
  const existing = this.patternHashes.get(key);
@@ -716,9 +1786,7 @@ export class AstManager {
716
1786
  return hash;
717
1787
  }
718
1788
 
719
- // The slot in builtinHashes of the hash of a builtin's Z3 declaration, other than a pattern's. The
720
- // domain depends only on the sort of an argument and the number of arguments, which key the cache
721
- // with the op and range. It returns the slot, a small integer, because V8 boxes a hash it returns.
1789
+ // Returns the slot, a small integer, because V8 boxes a hash it returns.
722
1790
  private getBuiltinSlot(op: number, args: number[], range: number) {
723
1791
  const argSort = args.length === 0 ? 0 : this.sorts[op === OP_ITE ? args[1] : args[0]];
724
1792
  const count = args.length;
@@ -744,7 +1812,6 @@ export class AstManager {
744
1812
  return this.insertBuiltinHash(op, argSort, range, count, hash);
745
1813
  }
746
1814
 
747
- // The slot of the builtin hash with these keys, or the free slot where it goes.
748
1815
  private findBuiltinSlot(op: number, argSort: number, range: number, count: number) {
749
1816
  const keys = this.builtinKeys;
750
1817
  const mask = this.builtinHashes.length - 1;
@@ -789,11 +1856,20 @@ export class AstManager {
789
1856
  );
790
1857
  }
791
1858
 
792
- // The hash of a user declaration, which Z3's tables of declarations hash.
793
1859
  getDeclHash(decl: number) {
794
1860
  return this.declHashes[decl];
795
1861
  }
796
1862
 
1863
+ getAppDeclHash(app: number) {
1864
+ const decl = this.decls[app];
1865
+ const funcDecl = this.funcDecls[decl];
1866
+ if (hasOwnDeclHash(funcDecl.family)) return this.declHashes[decl];
1867
+ const args = this.getArgs(app);
1868
+ return funcDecl.op === OP_PATTERN
1869
+ ? this.getPatternHash(args, this.sorts[app])
1870
+ : this.builtinHashes[this.getBuiltinSlot(funcDecl.op, args, this.sorts[app])];
1871
+ }
1872
+
797
1873
  mkFuncDecl(name: SmtSymbol, domain: number[], range: number, isSkolem = false) {
798
1874
  this.declHashes[this.funcDecls.length] = this.getFuncDeclHash(
799
1875
  name,
@@ -809,25 +1885,35 @@ export class AstManager {
809
1885
  range,
810
1886
  isSkolem,
811
1887
  smallId: this.mkSmallId(),
1888
+ parameters: [],
812
1889
  });
813
1890
  this.declRefCounts[this.funcDecls.length - 1] = 1;
814
1891
  return this.funcDecls.length - 1;
815
1892
  }
816
1893
 
817
- // mk_fresh_func_decl: without a prefix the name is the numerical symbol of the fresh id.
818
- mkFreshFuncDecl(prefix: SmtSymbol | null, domain: number[], range: number, isSkolem = true) {
819
- const name = prefix === null ? this.freshId : `${getSymbolName(prefix)}!${this.freshId}`;
1894
+ mkFreshFuncDecl(
1895
+ prefix: SmtSymbol | null,
1896
+ domain: number[],
1897
+ range: number,
1898
+ isSkolem = true,
1899
+ suffix = "",
1900
+ ) {
1901
+ const name =
1902
+ prefix === null && suffix === ""
1903
+ ? this.freshId
1904
+ : `${prefix === null ? "sk" : getSymbolName(prefix)}!${suffix === "" ? "" : `${suffix}!`}${this.freshId}`;
820
1905
  this.freshId++;
821
1906
  const decl = this.mkFuncDecl(name, domain, range, isSkolem);
822
1907
  this.declRefCounts[decl] = 0;
823
1908
  return decl;
824
1909
  }
825
1910
 
826
- // mk_fresh_var_name.
827
1911
  mkFreshVarName(prefix: string | null) {
828
- const name = `${prefix ?? "var"}!${this.freshId}`;
829
- this.freshId++;
830
- return name;
1912
+ return `${prefix ?? "var"}!${this.mkFreshId()}`;
1913
+ }
1914
+
1915
+ mkFreshId() {
1916
+ return this.freshId++;
831
1917
  }
832
1918
 
833
1919
  private intern(
@@ -863,6 +1949,7 @@ export class AstManager {
863
1949
  this.values = grow(this.values);
864
1950
  this.freeVarBounds = grow(this.freeVarBounds);
865
1951
  this.hasQuantifierFlags = grow(this.hasQuantifierFlags);
1952
+ this.hasLabelFlags = grow(this.hasLabelFlags);
866
1953
  this.depths = grow(this.depths);
867
1954
  this.hashes = grow(this.hashes);
868
1955
  this.ids = grow(this.ids);
@@ -871,7 +1958,6 @@ export class AstManager {
871
1958
  this.hashes[id] = hash;
872
1959
  this.kinds[id] = kind;
873
1960
  this.decls[id] = decl;
874
- // Like Z3, a node keeps its own copy of the arguments, so callers may reuse their arrays.
875
1961
  const start = this.argStarts[id];
876
1962
  const end = start + args.length;
877
1963
  if (end > this.argPool.length) {
@@ -890,25 +1976,30 @@ export class AstManager {
890
1976
  this.values[id] = value;
891
1977
  let freeVarBound = kind === KIND_VAR ? value + 1 : 0;
892
1978
  let hasQuantifier = kind === KIND_QUANTIFIER;
1979
+ let hasLabel = kind === KIND_APP && this.funcDecls[decl].op === OP_LABEL;
893
1980
  let depth = 1;
894
1981
  for (let index = 0; index < args.length; index++) {
895
1982
  const arg = args[index];
896
1983
  if (this.freeVarBounds[arg] > freeVarBound) freeVarBound = this.freeVarBounds[arg];
897
1984
  if (this.hasQuantifierFlags[arg]) hasQuantifier = true;
1985
+ if (this.hasLabelFlags[arg]) hasLabel = true;
898
1986
  if (this.depths[arg] + 1 > depth) depth = this.depths[arg] + 1;
899
1987
  }
900
1988
  this.freeVarBounds[id] = freeVarBound;
901
1989
  this.hasQuantifierFlags[id] = hasQuantifier ? 1 : 0;
1990
+ this.hasLabelFlags[id] = hasLabel ? 1 : 0;
902
1991
  this.depths[id] = depth;
903
1992
  return id;
904
1993
  }
905
1994
 
906
- // The op of a builtin declaration doubles as its index in funcDecls. The arguments are those of
907
- // args from start on, so that a rewriter passes its result stack without copying it.
908
1995
  mkApp(decl: number, args: number[], sort: number, start = 0) {
909
1996
  const funcDecl = this.funcDecls[decl];
910
- // Numerals and model values carry their value, so only mkNumeral and mkModelValue create them.
911
- if (decl === OP_NUM || funcDecl.family === FAMILY_MODEL_VALUE)
1997
+ if (
1998
+ decl === OP_NUM ||
1999
+ decl === OP_ALGEBRAIC_NUM ||
2000
+ funcDecl.family === FAMILY_MODEL_VALUE ||
2001
+ funcDecl.op === OP_BV_NUM
2002
+ )
912
2003
  throw new SmtInvariantError(`${funcDecl.name} needs a value`);
913
2004
  let hash = decl;
914
2005
  for (let index = start; index < args.length; index++)
@@ -931,17 +2022,16 @@ export class AstManager {
931
2022
  for (let index = start; index < args.length; index++) this.argBuffer.push(args[index]);
932
2023
  }
933
2024
  const nodeArgs = start > 0 ? this.argBuffer : args;
934
- // The slot comes first, since finding it may replace builtinHashes with one twice the size.
2025
+ const hasOwnHash = hasOwnDeclHash(funcDecl.family);
935
2026
  const builtinSlot =
936
- funcDecl.family === FAMILY_USER || funcDecl.op === OP_PATTERN
2027
+ hasOwnHash || funcDecl.op === OP_PATTERN
937
2028
  ? -1
938
2029
  : this.getBuiltinSlot(funcDecl.op, nodeArgs, sort);
939
- const declHash =
940
- funcDecl.family === FAMILY_USER
941
- ? this.declHashes[decl]
942
- : builtinSlot === -1
943
- ? this.getPatternHash(nodeArgs, sort)
944
- : this.builtinHashes[builtinSlot];
2030
+ const declHash = hasOwnHash
2031
+ ? this.declHashes[decl]
2032
+ : builtinSlot === -1
2033
+ ? this.getPatternHash(nodeArgs, sort)
2034
+ : this.builtinHashes[builtinSlot];
945
2035
  const id = this.create(
946
2036
  KIND_APP,
947
2037
  decl,
@@ -950,7 +2040,6 @@ export class AstManager {
950
2040
  0,
951
2041
  astArrayHash(nodeArgs, this.hashes, declHash),
952
2042
  );
953
- // The lookup ended at a free slot, which the application takes unless the table doubles.
954
2043
  if (2 * (this.numApps + 1) > this.appHandles.length) {
955
2044
  const handles = this.appHandles;
956
2045
  const hashes = this.appHashes;
@@ -978,8 +2067,6 @@ export class AstManager {
978
2067
  this.numApps++;
979
2068
  }
980
2069
 
981
- // A numeral's declaration has the parameters (value, is_int), and a rational hashes as
982
- // hash(numerator) + 3 hash(denominator).
983
2070
  private internNumeral(key: string, value: Rational, sort: number) {
984
2071
  const existing = this.table.get(key);
985
2072
  if (existing !== undefined) return this.revive(existing);
@@ -998,6 +2085,60 @@ export class AstManager {
998
2085
  );
999
2086
  }
1000
2087
 
2088
+ get am() {
2089
+ this.algebraicManager ??= new AlgebraicManager();
2090
+ return this.algebraicManager;
2091
+ }
2092
+
2093
+ mkAlgebraicNumeral(value: Anum, sort = SORT_REAL) {
2094
+ if (this.am.isRational(value)) return this.mkNumeralOfSort(this.am.toRational(value), sort);
2095
+ if (sort === SORT_INT)
2096
+ throw new SmtInvariantError("invalid irrational value passed as an integer");
2097
+ const id = this.mkAlgebraicId();
2098
+ this.algebraicNumerals.push({ value: this.am.copy(value), id });
2099
+ const expr = this.create(
2100
+ KIND_APP,
2101
+ OP_ALGEBRAIC_NUM,
2102
+ [],
2103
+ SORT_REAL,
2104
+ this.algebraicNumerals.length - 1,
2105
+ this.getFuncDeclHash("RootObject", [], SORT_REAL, getInfoHash(Z3_ARITH_FAMILY, 1, [id])),
2106
+ );
2107
+ this.registerNode(expr);
2108
+ return expr;
2109
+ }
2110
+
2111
+ private mkAlgebraicId() {
2112
+ return this.freeAlgebraicIds.pop() ?? this.nextAlgebraicId++;
2113
+ }
2114
+
2115
+ isAlgebraicNumeral(expr: number) {
2116
+ return this.getOp(expr) === OP_ALGEBRAIC_NUM;
2117
+ }
2118
+
2119
+ getAlgebraicValue(expr: number) {
2120
+ return this.algebraicNumerals[this.values[expr]].value;
2121
+ }
2122
+
2123
+ private getAnumOrNumeral(expr: number) {
2124
+ if (this.isAlgebraicNumeral(expr)) return this.getAlgebraicValue(expr);
2125
+ return this.isNumeral(expr) ? new Anum(this.getRationalValue(expr)) : undefined;
2126
+ }
2127
+
2128
+ compareArithValues(first: number, second: number) {
2129
+ if (this.isNumeral(first) && this.isNumeral(second))
2130
+ return this.getRationalValue(first).compare(this.getRationalValue(second));
2131
+ const firstValue = this.getAnumOrNumeral(first);
2132
+ const secondValue = this.getAnumOrNumeral(second);
2133
+ if (firstValue === undefined || secondValue === undefined)
2134
+ throw new SmtInvariantError("arith_value_order compares numerals");
2135
+ return this.am.compare(firstValue, secondValue);
2136
+ }
2137
+
2138
+ getAlgebraicId(expr: number) {
2139
+ return this.algebraicNumerals[this.values[expr]].id;
2140
+ }
2141
+
1001
2142
  mkRealNumeral(value: Rational) {
1002
2143
  const numeral = this.internNumeral(`r${value}`, value, SORT_REAL);
1003
2144
  return value.isInt() ? this.holdSmallNumeral(numeral, value.num, this.smallReals) : numeral;
@@ -1021,12 +2162,10 @@ export class AstManager {
1021
2162
  return numeral;
1022
2163
  }
1023
2164
 
1024
- // mk_numeral(value, is_int).
1025
2165
  mkNumeralOfSort(value: Rational, sort: number) {
1026
2166
  return sort === SORT_INT ? this.mkNumeral(value.num) : this.mkRealNumeral(value);
1027
2167
  }
1028
2168
 
1029
- // mk_model_value: the element with this index in the universe of an uninterpreted sort.
1030
2169
  mkModelValue(index: number, sort: number) {
1031
2170
  const key = `m${sort}:${index}`;
1032
2171
  const existing = this.table.get(key);
@@ -1039,6 +2178,7 @@ export class AstManager {
1039
2178
  range: sort,
1040
2179
  isSkolem: false,
1041
2180
  smallId: -1,
2181
+ parameters: [],
1042
2182
  });
1043
2183
  const decl = this.funcDecls.length - 1;
1044
2184
  this.declHashes[decl] = this.getFuncDeclHash(
@@ -1063,19 +2203,30 @@ export class AstManager {
1063
2203
  mkQuantifier(data: QuantifierData) {
1064
2204
  if (data.names.length !== data.sorts.length)
1065
2205
  throw new SmtInvariantError("a quantifier needs a name for each variable");
1066
- const key = `q${data.isForall ? 1 : 0}:${data.sorts.join(",")}:${JSON.stringify(data.names)}:${
2206
+ // Z3 keeps no-patterns only on a quantifier without patterns.
2207
+ const noPatterns = data.patterns.length > 0 ? [] : data.noPatterns;
2208
+ const key = `q${data.kind}:${data.sorts.join(",")}:${JSON.stringify(data.names)}:${
1067
2209
  data.body
1068
- }:${data.patterns.join(",")}:${data.weight}`;
2210
+ }:${data.patterns.join(",")}:${noPatterns.join(",")}:${data.weight}`;
1069
2211
  const existing = this.table.get(key);
1070
- if (existing !== undefined) return this.revive(existing);
2212
+ if (existing !== undefined) {
2213
+ // A dead quantifier comes back as the new one Z3 makes, with its skolem id.
2214
+ if (this.ids[existing] === -1) {
2215
+ const old = this.getQuantifier(existing);
2216
+ this.quantifiers.set(existing, { ...old, skid: data.skid });
2217
+ }
2218
+ return this.revive(existing);
2219
+ }
1071
2220
  const id = this.create(
1072
2221
  KIND_QUANTIFIER,
1073
2222
  -1,
1074
2223
  [data.body],
1075
- SORT_BOOL,
2224
+ data.kind === QUANTIFIER_LAMBDA
2225
+ ? this.mkArraySort([...data.sorts, this.sorts[data.body]])
2226
+ : SORT_BOOL,
1076
2227
  0,
1077
2228
  mkMix(
1078
- astArrayHash(data.sorts, this.sortHashes, data.isForall ? 31 : 19),
2229
+ astArrayHash(data.sorts, this.sortHashes, data.kind === QUANTIFIER_FORALL ? 31 : 19),
1079
2230
  data.patterns.length,
1080
2231
  this.hashes[data.body],
1081
2232
  ),
@@ -1088,12 +2239,73 @@ export class AstManager {
1088
2239
  sorts: [...data.sorts],
1089
2240
  names: [...data.names],
1090
2241
  patterns: [...data.patterns],
2242
+ noPatterns: [...noPatterns],
1091
2243
  });
1092
2244
  this.registerNode(id);
1093
2245
  return id;
1094
2246
  }
1095
2247
 
1096
- // mk_pattern: a multi-pattern of a quantifier.
2248
+ // A label takes its polarity and names as the parameters of its declaration, and a label literal
2249
+ // takes its names.
2250
+ mkLabel(isPositive: boolean, names: string[], expr: number) {
2251
+ return this.mkApp(this.mkLabelDecl(OP_LABEL, isPositive, names), [expr], SORT_BOOL);
2252
+ }
2253
+
2254
+ mkLabelLit(names: string[]) {
2255
+ return this.mkApp(this.mkLabelDecl(OP_LABEL_LIT, true, names), [], SORT_BOOL);
2256
+ }
2257
+
2258
+ private mkLabelDecl(op: number, isPositive: boolean, names: string[]) {
2259
+ const nameIds = names.map((name) => {
2260
+ const existing = this.labelNameIds.get(name);
2261
+ if (existing !== undefined) return existing;
2262
+ this.labelNames.push(name);
2263
+ this.labelNameIds.set(name, this.labelNames.length - 1);
2264
+ return this.labelNames.length - 1;
2265
+ });
2266
+ const parameters = op === OP_LABEL ? [isPositive ? 1 : 0, ...nameIds] : nameIds;
2267
+ const key = `${op}:${parameters.join(",")}`;
2268
+ const existing = this.labelDecls.get(key);
2269
+ if (existing !== undefined) return existing;
2270
+ const name = op === OP_LABEL_LIT ? "lbl-lit" : isPositive ? "lblpos" : "lblneg";
2271
+ const domain = op === OP_LABEL ? [SORT_BOOL] : [];
2272
+ const parameterHashes = names.map(getSymbolHash);
2273
+ const decl = this.funcDecls.length;
2274
+ this.declHashes[decl] = this.getFuncDeclHash(
2275
+ name,
2276
+ domain,
2277
+ SORT_BOOL,
2278
+ getInfoHash(
2279
+ Z3_LABEL_FAMILY,
2280
+ op - LABEL_OP_BASE,
2281
+ op === OP_LABEL ? [isPositive ? 1 : 0, ...parameterHashes] : parameterHashes,
2282
+ ),
2283
+ );
2284
+ this.funcDecls.push({
2285
+ name,
2286
+ family: FAMILY_LABEL,
2287
+ op,
2288
+ domain,
2289
+ range: SORT_BOOL,
2290
+ isSkolem: false,
2291
+ smallId: this.mkSmallId(),
2292
+ parameters,
2293
+ });
2294
+ this.labelDecls.set(key, decl);
2295
+ return decl;
2296
+ }
2297
+
2298
+ isLabelPositive(decl: number) {
2299
+ return this.funcDecls[decl].parameters[0] === 1;
2300
+ }
2301
+
2302
+ getLabelNames(decl: number) {
2303
+ const funcDecl = this.funcDecls[decl];
2304
+ return funcDecl.parameters
2305
+ .slice(funcDecl.op === OP_LABEL ? 1 : 0)
2306
+ .map((nameId) => this.labelNames[nameId]);
2307
+ }
2308
+
1097
2309
  mkPattern(terms: number[]) {
1098
2310
  return this.mkApp(OP_PATTERN, terms, SORT_BOOL);
1099
2311
  }
@@ -1104,6 +2316,12 @@ export class AstManager {
1104
2316
  return data;
1105
2317
  }
1106
2318
 
2319
+ isLambda(expr: number) {
2320
+ return (
2321
+ this.kinds[expr] === KIND_QUANTIFIER && this.getQuantifier(expr).kind === QUANTIFIER_LAMBDA
2322
+ );
2323
+ }
2324
+
1107
2325
  isApp(expr: number) {
1108
2326
  return this.kinds[expr] === KIND_APP;
1109
2327
  }
@@ -1116,7 +2334,6 @@ export class AstManager {
1116
2334
  return this.freeVarBounds[expr] === 0;
1117
2335
  }
1118
2336
 
1119
- // Z3's is_ground: an application without variables or quantifiers.
1120
2337
  isGroundApp(expr: number) {
1121
2338
  return (
1122
2339
  this.kinds[expr] === KIND_APP &&
@@ -1170,40 +2387,114 @@ export class AstManager {
1170
2387
  return this.getOp(expr) === OP_NUM;
1171
2388
  }
1172
2389
 
1173
- // The value of an integer numeral.
1174
2390
  getNumeral(expr: number) {
1175
2391
  return this.isNumeral(expr) && this.sorts[expr] === SORT_INT
1176
2392
  ? this.getNumeralValue(expr)
1177
2393
  : undefined;
1178
2394
  }
1179
2395
 
1180
- // The value of an expression that is an integer numeral.
1181
2396
  getNumeralValue(expr: number) {
1182
2397
  return this.numeralValues[this.values[expr]].num;
1183
2398
  }
1184
2399
 
1185
- // The value of an expression that is a numeral of either sort.
1186
2400
  getRationalValue(expr: number) {
1187
2401
  return this.numeralValues[this.values[expr]];
1188
2402
  }
1189
2403
 
1190
- // Unique values are interpreted constants that are distinct from each other, as in Z3.
1191
- isUniqueValue(expr: number) {
2404
+ isUniqueValue(expr: number): boolean {
1192
2405
  const op = this.getOp(expr);
1193
- return op === OP_NUM || op === OP_TRUE || op === OP_FALSE || op === OP_MODEL_VALUE;
2406
+ return (
2407
+ op === OP_NUM ||
2408
+ op === OP_PI ||
2409
+ op === OP_E ||
2410
+ op === OP_TRUE ||
2411
+ op === OP_FALSE ||
2412
+ op === OP_MODEL_VALUE ||
2413
+ op === OP_BV_NUM ||
2414
+ op === OP_CHAR_CONST ||
2415
+ (op >= OP_FPA_RM_NEAREST_TIES_TO_EVEN && op <= OP_FPA_RM_TOWARD_ZERO) ||
2416
+ (op === OP_CONST_ARRAY && this.isUniqueValue(this.getArg(expr, 0))) ||
2417
+ (op === OP_DT_CONSTRUCTOR &&
2418
+ this.mapArgs(expr, (arg) => this.isUniqueValue(arg)).every((isUnique) => isUnique)) ||
2419
+ (op === OP_FPA_FP && this.isUniqueFp(expr))
2420
+ );
2421
+ }
2422
+
2423
+ private isUniqueFp(expr: number) {
2424
+ if (!this.mapArgs(expr, (arg) => this.isValue(arg)).every((isValue) => isValue)) return false;
2425
+ const exponent = this.getArg(expr, 1);
2426
+ const value = this.getBvNumeralValue(exponent);
2427
+ return value !== 0n && value !== (1n << BigInt(this.getBvSize(this.sorts[exponent]))) - 1n;
2428
+ }
2429
+
2430
+ isValue(expr: number): boolean {
2431
+ let current = expr;
2432
+ while (this.getOp(current) === OP_STORE) {
2433
+ for (let index = 1; index < this.getNumArgs(current); index++)
2434
+ if (!this.isValue(this.getArg(current, index))) return false;
2435
+ current = this.getArg(current, 0);
2436
+ }
2437
+ if (this.getOp(current) === OP_CONST_ARRAY) return this.isValue(this.getArg(current, 0));
2438
+ if (this.getOp(current) === OP_DT_CONSTRUCTOR || this.getOp(current) === OP_FPA_FP)
2439
+ return this.mapArgs(current, (arg) => this.isValue(arg)).every((isValue) => isValue);
2440
+ if (this.getOp(current) >= OP_FPA_NUM && this.getOp(current) <= OP_FPA_MINUS_ZERO) return true;
2441
+ if (this.getOp(current) === OP_ALGEBRAIC_NUM) return true;
2442
+ if (this.getFamily(current) === FAMILY_SEQ) return this.isSeqValue(current);
2443
+ return current === expr && this.isUniqueValue(expr);
2444
+ }
2445
+
2446
+ private isSeqValue(expr: number) {
2447
+ const todo = [expr];
2448
+ for (let current = todo.pop(); current !== undefined; current = todo.pop()) {
2449
+ const op = this.getOp(current);
2450
+ if (op === OP_SEQ_EMPTY || op === OP_STRING_CONST) continue;
2451
+ if (op === OP_SEQ_UNIT && this.isValue(this.getArg(current, 0))) continue;
2452
+ if (op !== OP_SEQ_CONCAT || !this.everyArg(current, (arg) => this.isApp(arg))) return false;
2453
+ this.pushArgs(current, todo);
2454
+ }
2455
+ return true;
2456
+ }
2457
+
2458
+ private areSeqsDistinct(first: number, second: number): boolean {
2459
+ const firstOp = this.getOp(first);
2460
+ const secondOp = this.getOp(second);
2461
+ if (first === second) return false;
2462
+ if (firstOp === OP_STRING_CONST && secondOp === OP_STRING_CONST) return true;
2463
+ if (firstOp === OP_SEQ_UNIT && secondOp === OP_SEQ_UNIT)
2464
+ return this.areDistinct(this.getArg(first, 0), this.getArg(second, 0));
2465
+ return (
2466
+ (firstOp === OP_SEQ_EMPTY && secondOp === OP_SEQ_UNIT) ||
2467
+ (firstOp === OP_SEQ_UNIT && secondOp === OP_SEQ_EMPTY)
2468
+ );
1194
2469
  }
1195
2470
 
1196
- // are_distinct: the plugin of the first term's family, or of the second's for a user term, tells
1197
- // two terms apart. Every plugin separates distinct unique values, and arith also separates b from
1198
- // (+ b k) and (+ k b) for a nonzero numeral k.
1199
- areDistinct(first: number, second: number) {
2471
+ areDistinct(first: number, second: number): boolean {
1200
2472
  if (this.kinds[first] !== KIND_APP || this.kinds[second] !== KIND_APP) return false;
2473
+ const pluginFamily =
2474
+ this.getFamily(first) === FAMILY_USER ? this.getFamily(second) : this.getFamily(first);
2475
+ if (pluginFamily === FAMILY_SEQ) return this.areSeqsDistinct(first, second);
1201
2476
  if (first !== second && this.isUniqueValue(first) && this.isUniqueValue(second)) return true;
1202
2477
  const family = this.funcDecls[this.decls[first]].family;
2478
+ if (family === FAMILY_DATATYPE) {
2479
+ if (this.getOp(first) !== OP_DT_CONSTRUCTOR || this.getOp(second) !== OP_DT_CONSTRUCTOR)
2480
+ return false;
2481
+ if (this.decls[first] !== this.decls[second]) return true;
2482
+ for (let index = this.getNumArgs(first) - 1; index >= 0; index--)
2483
+ if (this.areDistinct(this.getArg(first, index), this.getArg(second, index))) return true;
2484
+ return false;
2485
+ }
1203
2486
  if (
1204
2487
  (family === FAMILY_USER ? this.funcDecls[this.decls[second]].family : family) !== FAMILY_ARITH
1205
2488
  )
1206
2489
  return false;
2490
+ const firstValue = this.getAnumOrNumeral(first);
2491
+ const secondValue = this.getAnumOrNumeral(second);
2492
+ if (
2493
+ firstValue !== undefined &&
2494
+ secondValue !== undefined &&
2495
+ (this.isAlgebraicNumeral(first) || this.isAlgebraicNumeral(second))
2496
+ )
2497
+ return this.am.compare(firstValue, secondValue) !== 0;
1207
2498
  const isOffsetOf = (sum: number, term: number) => {
1208
2499
  if (this.getOp(sum) !== OP_ADD || this.getNumArgs(sum) !== 2) return false;
1209
2500
  const first = this.getArg(sum, 0);
@@ -1220,10 +2511,9 @@ export class AstManager {
1220
2511
  }
1221
2512
 
1222
2513
  isUninterpretedSort(sort: number) {
1223
- return sort > SORT_REAL;
2514
+ return this.sortFamilies[sort] === FAMILY_USER;
1224
2515
  }
1225
2516
 
1226
- // An uninterpreted constant, such as a declared constant or a Skolem constant.
1227
2517
  isUninterpretedConst(expr: number) {
1228
2518
  return this.isUninterpreted(expr) && this.getNumArgs(expr) === 0;
1229
2519
  }
@@ -1232,7 +2522,6 @@ export class AstManager {
1232
2522
  return this.getFamily(expr) === FAMILY_USER;
1233
2523
  }
1234
2524
 
1235
- // arith_util::is_times_minus_one: the t of (* -1 t).
1236
2525
  getTimesMinusOneArg(expr: number) {
1237
2526
  if (this.getOp(expr) !== OP_MUL || this.getNumArgs(expr) !== 2) return undefined;
1238
2527
  const coefficient = this.getArg(expr, 0);
@@ -1242,7 +2531,6 @@ export class AstManager {
1242
2531
  : undefined;
1243
2532
  }
1244
2533
 
1245
- // occurs: decl is applied somewhere in expr.
1246
2534
  isOccurring(decl: number, expr: number) {
1247
2535
  return this.someSubterm(expr, (current) => this.decls[current] === decl);
1248
2536
  }
@@ -1251,8 +2539,6 @@ export class AstManager {
1251
2539
  return this.someSubterm(expr, (current) => this.isModelValue(current));
1252
2540
  }
1253
2541
 
1254
- // for_each_expr: visits the subterms with a stack of its own, so deep terms do not exhaust the call
1255
- // stack.
1256
2542
  private someSubterm(expr: number, predicate: (expr: number) => boolean) {
1257
2543
  const visited = new Set<number>();
1258
2544
  const todo = [expr];
@@ -1300,8 +2586,10 @@ export class AstManager {
1300
2586
  if (kind === KIND_VAR) return `(:var ${this.values[expr]})`;
1301
2587
  if (kind === KIND_QUANTIFIER) {
1302
2588
  const data = this.getQuantifier(expr);
1303
- return `(${data.isForall ? "forall" : "exists"} (${data.sorts.length}) ${this.toString(data.body)})`;
2589
+ return `(${["forall", "exists", "lambda"][data.kind]} (${data.sorts.length}) ${this.toString(data.body)})`;
1304
2590
  }
2591
+ if (this.getOp(expr) === OP_BV_NUM)
2592
+ return `(_ bv${this.getBvNumeralValue(expr)} ${this.getBvSize(this.sorts[expr])})`;
1305
2593
  if (this.getOp(expr) === OP_NUM) {
1306
2594
  const value = this.getRationalValue(expr);
1307
2595
  if (this.sorts[expr] === SORT_INT) return String(value.num);