tla-ts 0.0.4-dev.6dec595 → 0.0.4-dev.7b5087a

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 (307) hide show
  1. package/README.md +27 -141
  2. package/dist/deciders.cjs +366 -0
  3. package/dist/deciders.js +366 -0
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +35 -1
  6. package/dist/index.d.ts +35 -1
  7. package/dist/index.js +1 -1
  8. package/dist/parallel-worker.cjs +1 -4
  9. package/dist/parallel-worker.js +1 -4
  10. package/dist/prove-worker.cjs +9 -0
  11. package/dist/prove-worker.d.cts +9 -0
  12. package/dist/prove-worker.d.ts +9 -0
  13. package/dist/prove-worker.js +9 -0
  14. package/dist/prove.cjs +996 -0
  15. package/dist/prove.js +996 -0
  16. package/dist/run-cli.cjs +517 -47
  17. package/dist/run-cli.js +517 -47
  18. package/dist/tlapm.cjs +10 -0
  19. package/dist/tlapm.js +10 -0
  20. package/dist/transfer.cjs +38 -44
  21. package/dist/transfer.js +38 -44
  22. package/dist/worker.cjs +21 -0
  23. package/dist/worker.js +21 -0
  24. package/package.json +21 -12
  25. package/src/errors.ts +86 -0
  26. package/src/evaluate.ts +28 -7
  27. package/src/index.ts +10 -0
  28. package/src/{obligation-action.ts → obligations/action.ts} +258 -37
  29. package/src/{obligation-anon.ts → obligations/anon.ts} +13 -7
  30. package/src/obligations/ctx.ts +64 -0
  31. package/src/{obligation-deref.ts → obligations/deref.ts} +2 -2
  32. package/src/{obligation-expression.ts → obligations/expression.ts} +54 -8
  33. package/src/{obligation-format.ts → obligations/format.ts} +6 -5
  34. package/src/{obligation-generate.ts → obligations/generate.ts} +134 -61
  35. package/src/{obligations.ts → obligations/index.ts} +28 -12
  36. package/src/{obligation-levels.ts → obligations/levels.ts} +12 -2
  37. package/src/{obligation-module.ts → obligations/module.ts} +46 -20
  38. package/src/obligations/pickle.ts +100 -0
  39. package/src/obligations/pragma.ts +189 -0
  40. package/src/{obligation-printer.ts → obligations/printer.ts} +9 -63
  41. package/src/{obligation-proof.ts → obligations/proof.ts} +7 -1
  42. package/src/{obligation-source.ts → obligations/source.ts} +91 -35
  43. package/src/obligations/temporal.ts +319 -0
  44. package/src/{obligation-tuples.ts → obligations/tuples.ts} +1 -1
  45. package/src/ocaml-marshal.ts +172 -0
  46. package/src/parallel-worker.ts +2 -18
  47. package/src/parallel.ts +10 -43
  48. package/src/prove/decide-pool.ts +100 -0
  49. package/src/prove/deciders.ts +33 -0
  50. package/src/prove/isabelle/LICENSE +36 -0
  51. package/src/prove/isabelle/LICENSE-tlapm +26 -0
  52. package/src/prove/isabelle/decide.ts +47 -0
  53. package/src/prove/isabelle/encode.ts +512 -0
  54. package/src/prove/isabelle/isar/inner-syntax.ts +579 -0
  55. package/src/prove/isabelle/isar/method.ts +367 -0
  56. package/src/prove/isabelle/isar/proof.ts +28 -0
  57. package/src/prove/isabelle/isar/statement.ts +57 -0
  58. package/src/prove/isabelle/isar/theory.ts +192 -0
  59. package/src/prove/isabelle/isar/type-check.ts +278 -0
  60. package/src/prove/isabelle/provers/blast/blast.ts +258 -0
  61. package/src/prove/isabelle/provers/blast/branch.ts +322 -0
  62. package/src/prove/isabelle/provers/blast/prove.ts +347 -0
  63. package/src/prove/isabelle/provers/blast/rules.ts +330 -0
  64. package/src/prove/isabelle/provers/blast/state.ts +180 -0
  65. package/src/prove/isabelle/provers/blast/term.ts +547 -0
  66. package/src/prove/isabelle/provers/clasimp.ts +150 -0
  67. package/src/prove/isabelle/provers/classical.ts +389 -0
  68. package/src/prove/isabelle/provers/hypsubst.ts +441 -0
  69. package/src/prove/isabelle/pure/bicompose.ts +174 -0
  70. package/src/prove/isabelle/pure/consts.ts +32 -0
  71. package/src/prove/isabelle/pure/conv.ts +79 -0
  72. package/src/prove/isabelle/pure/drule.ts +264 -0
  73. package/src/prove/isabelle/pure/envir.ts +363 -0
  74. package/src/prove/isabelle/pure/goal.ts +53 -0
  75. package/src/prove/isabelle/pure/interrupt.ts +16 -0
  76. package/src/prove/isabelle/pure/list.ts +12 -0
  77. package/src/prove/isabelle/pure/logic.ts +402 -0
  78. package/src/prove/isabelle/pure/name.ts +110 -0
  79. package/src/prove/isabelle/pure/net.ts +262 -0
  80. package/src/prove/isabelle/pure/object-logic.ts +41 -0
  81. package/src/prove/isabelle/pure/pattern.ts +650 -0
  82. package/src/prove/isabelle/pure/raw-simplifier.ts +353 -0
  83. package/src/prove/isabelle/pure/rewriter.ts +602 -0
  84. package/src/prove/isabelle/pure/search.ts +68 -0
  85. package/src/prove/isabelle/pure/seq.ts +156 -0
  86. package/src/prove/isabelle/pure/simplifier.ts +112 -0
  87. package/src/prove/isabelle/pure/table.ts +208 -0
  88. package/src/prove/isabelle/pure/tactic.ts +276 -0
  89. package/src/prove/isabelle/pure/tactical.ts +347 -0
  90. package/src/prove/isabelle/pure/term-ord.ts +159 -0
  91. package/src/prove/isabelle/pure/term.ts +808 -0
  92. package/src/prove/isabelle/pure/thm.ts +806 -0
  93. package/src/prove/isabelle/pure/type.ts +133 -0
  94. package/src/prove/isabelle/pure/unify.ts +753 -0
  95. package/src/prove/isabelle/pure/variable.ts +25 -0
  96. package/src/prove/isabelle/tla/classical-setup.ts +101 -0
  97. package/src/prove/isabelle/tla/context.ts +74 -0
  98. package/src/prove/isabelle/tla/logic-data.ts +5 -0
  99. package/src/prove/isabelle/tla/logic.ts +215 -0
  100. package/src/prove/isabelle/tla/simpdata.ts +241 -0
  101. package/src/prove/isabelle/tools/intuitionistic.ts +132 -0
  102. package/src/prove/ocaml.ts +22 -0
  103. package/src/prove/prep/action.ts +520 -0
  104. package/src/prove/prep/defs.ts +33 -0
  105. package/src/prove/prep/elab.ts +156 -0
  106. package/src/prove/prep/enabled-axioms.ts +189 -0
  107. package/src/prove/prep/enabled.ts +43 -0
  108. package/src/prove/prep/fingerprint.ts +388 -0
  109. package/src/prove/prep/flatten.ts +70 -0
  110. package/src/prove/prep/level-comparison.ts +552 -0
  111. package/src/prove/prep/md5.ts +67 -0
  112. package/src/prove/prep/pragma.ts +221 -0
  113. package/src/prove/prep/trivial.ts +22 -0
  114. package/src/prove/prove-worker.ts +27 -0
  115. package/src/prove/prove.ts +528 -0
  116. package/src/prove/ptl/decide.ts +68 -0
  117. package/src/prove/ptl/encode.ts +256 -0
  118. package/src/prove/ptl/sat.ts +151 -0
  119. package/src/prove/ptl/tableau.ts +276 -0
  120. package/src/prove/smt/axiomatize.ts +69 -0
  121. package/src/prove/smt/axioms.ts +210 -0
  122. package/src/prove/smt/context.ts +108 -0
  123. package/src/prove/smt/data.ts +665 -0
  124. package/src/prove/smt/encode.ts +169 -0
  125. package/src/prove/smt/extensionality.ts +63 -0
  126. package/src/prove/smt/flatten.ts +127 -0
  127. package/src/prove/smt/format.ts +143 -0
  128. package/src/prove/smt/instances.ts +182 -0
  129. package/src/prove/smt/rewrite.ts +784 -0
  130. package/src/prove/smt/simplify.ts +57 -0
  131. package/src/prove/smt/solver/act-cache.ts +265 -0
  132. package/src/prove/smt/solver/arith-rewriter.ts +962 -0
  133. package/src/prove/smt/solver/array.ts +22 -0
  134. package/src/prove/smt/solver/asserted-formulas.ts +469 -0
  135. package/src/prove/smt/solver/ast-lt.ts +157 -0
  136. package/src/prove/smt/solver/ast-util.ts +147 -0
  137. package/src/prove/smt/solver/ast.ts +1315 -0
  138. package/src/prove/smt/solver/auf-solver.ts +572 -0
  139. package/src/prove/smt/solver/case-split-queue.ts +178 -0
  140. package/src/prove/smt/solver/cg-table.ts +175 -0
  141. package/src/prove/smt/solver/clause.ts +276 -0
  142. package/src/prove/smt/solver/conflict-resolution.ts +343 -0
  143. package/src/prove/smt/solver/context.ts +1292 -0
  144. package/src/prove/smt/solver/ctx-simplify.ts +334 -0
  145. package/src/prove/smt/solver/der.ts +224 -0
  146. package/src/prove/smt/solver/dyn-ack.ts +141 -0
  147. package/src/prove/smt/solver/e-matching.ts +659 -0
  148. package/src/prove/smt/solver/enode.ts +259 -0
  149. package/src/prove/smt/solver/expr-ref.ts +21 -0
  150. package/src/prove/smt/solver/expr-safe-replace.ts +172 -0
  151. package/src/prove/smt/solver/fingerprints.ts +92 -0
  152. package/src/prove/smt/solver/flat-literals.ts +51 -0
  153. package/src/prove/smt/solver/goal.ts +138 -0
  154. package/src/prove/smt/solver/hash.ts +173 -0
  155. package/src/prove/smt/solver/hashtable.ts +166 -0
  156. package/src/prove/smt/solver/internalizer.ts +1381 -0
  157. package/src/prove/smt/solver/lbool.ts +5 -0
  158. package/src/prove/smt/solver/literal.ts +24 -0
  159. package/src/prove/smt/solver/lp/bound-propagator.ts +391 -0
  160. package/src/prove/smt/solver/lp/constraints.ts +78 -0
  161. package/src/prove/smt/solver/lp/containers.ts +296 -0
  162. package/src/prove/smt/solver/lp/core-solver.ts +504 -0
  163. package/src/prove/smt/solver/lp/cross-nested.ts +224 -0
  164. package/src/prove/smt/solver/lp/dep-intervals.ts +656 -0
  165. package/src/prove/smt/solver/lp/dependency.ts +51 -0
  166. package/src/prove/smt/solver/lp/dioph-eq.ts +1181 -0
  167. package/src/prove/smt/solver/lp/explanation.ts +46 -0
  168. package/src/prove/smt/solver/lp/gomory.ts +255 -0
  169. package/src/prove/smt/solver/lp/hnf-cutter.ts +125 -0
  170. package/src/prove/smt/solver/lp/hnf.ts +281 -0
  171. package/src/prove/smt/solver/lp/int-cube.ts +295 -0
  172. package/src/prove/smt/solver/lp/int-solver.ts +514 -0
  173. package/src/prove/smt/solver/lp/lar-solver.ts +816 -0
  174. package/src/prove/smt/solver/lp/lar-term.ts +62 -0
  175. package/src/prove/smt/solver/lp/lp-settings.ts +91 -0
  176. package/src/prove/smt/solver/lp/maximize-term.ts +347 -0
  177. package/src/prove/smt/solver/lp/nex-creator.ts +381 -0
  178. package/src/prove/smt/solver/lp/nex.ts +120 -0
  179. package/src/prove/smt/solver/lp/nla-backoff.ts +43 -0
  180. package/src/prove/smt/solver/lp/nla-basics.ts +398 -0
  181. package/src/prove/smt/solver/lp/nla-core.ts +968 -0
  182. package/src/prove/smt/solver/lp/nla-divisions.ts +332 -0
  183. package/src/prove/smt/solver/lp/nla-emonics.ts +373 -0
  184. package/src/prove/smt/solver/lp/nla-factorization.ts +30 -0
  185. package/src/prove/smt/solver/lp/nla-grobner.ts +904 -0
  186. package/src/prove/smt/solver/lp/nla-horner.ts +126 -0
  187. package/src/prove/smt/solver/lp/nla-intervals.ts +265 -0
  188. package/src/prove/smt/solver/lp/nla-monomial-bounds.ts +715 -0
  189. package/src/prove/smt/solver/lp/nla-monotone.ts +59 -0
  190. package/src/prove/smt/solver/lp/nla-order.ts +307 -0
  191. package/src/prove/smt/solver/lp/nla-patcher.ts +112 -0
  192. package/src/prove/smt/solver/lp/nla-tangents.ts +251 -0
  193. package/src/prove/smt/solver/lp/nla-throttle.ts +85 -0
  194. package/src/prove/smt/solver/lp/nla-types.ts +124 -0
  195. package/src/prove/smt/solver/lp/nla-var-eqs.ts +141 -0
  196. package/src/prove/smt/solver/lp/nra-solver.ts +440 -0
  197. package/src/prove/smt/solver/lp/numeric-pair.ts +65 -0
  198. package/src/prove/smt/solver/lp/pdd-interval.ts +31 -0
  199. package/src/prove/smt/solver/lp/pdd-simplifier.ts +184 -0
  200. package/src/prove/smt/solver/lp/pdd-solver.ts +408 -0
  201. package/src/prove/smt/solver/lp/pdd.ts +971 -0
  202. package/src/prove/smt/solver/lp/random-updater.ts +138 -0
  203. package/src/prove/smt/solver/lp/static-matrix.ts +246 -0
  204. package/src/prove/smt/solver/lp/union-find.ts +96 -0
  205. package/src/prove/smt/solver/mam-compiler.ts +847 -0
  206. package/src/prove/smt/solver/mam-interpreter.ts +400 -0
  207. package/src/prove/smt/solver/maximize-ac-sharing.ts +118 -0
  208. package/src/prove/smt/solver/model-checker.ts +347 -0
  209. package/src/prove/smt/solver/model-evaluator.ts +262 -0
  210. package/src/prove/smt/solver/model-finder.ts +155 -0
  211. package/src/prove/smt/solver/model-generator.ts +97 -0
  212. package/src/prove/smt/solver/model-macro-solver.ts +728 -0
  213. package/src/prove/smt/solver/nlsat/algebraic.ts +1161 -0
  214. package/src/prove/smt/solver/nlsat/atoms.ts +238 -0
  215. package/src/prove/smt/solver/nlsat/evaluator.ts +303 -0
  216. package/src/prove/smt/solver/nlsat/explain.ts +607 -0
  217. package/src/prove/smt/solver/nlsat/interval-set.ts +351 -0
  218. package/src/prove/smt/solver/nlsat/levelwise.ts +740 -0
  219. package/src/prove/smt/solver/nlsat/mpbq.ts +161 -0
  220. package/src/prove/smt/solver/nlsat/polynomial.ts +1722 -0
  221. package/src/prove/smt/solver/nlsat/solver.ts +911 -0
  222. package/src/prove/smt/solver/nlsat/types.ts +43 -0
  223. package/src/prove/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
  224. package/src/prove/smt/solver/nlsat/upolynomial.ts +996 -0
  225. package/src/prove/smt/solver/nnf.ts +469 -0
  226. package/src/prove/smt/solver/pattern-inference.ts +351 -0
  227. package/src/prove/smt/solver/poly-rewriter.ts +477 -0
  228. package/src/prove/smt/solver/propagate-values.ts +83 -0
  229. package/src/prove/smt/solver/proto-model.ts +551 -0
  230. package/src/prove/smt/solver/pull-quant.ts +139 -0
  231. package/src/prove/smt/solver/qe-lite-der.ts +526 -0
  232. package/src/prove/smt/solver/qe-lite-fm.ts +576 -0
  233. package/src/prove/smt/solver/qe-lite.ts +381 -0
  234. package/src/prove/smt/solver/qi-queue.ts +362 -0
  235. package/src/prove/smt/solver/quant-tactics.ts +117 -0
  236. package/src/prove/smt/solver/quantifier-analyzer.ts +562 -0
  237. package/src/prove/smt/solver/quantifier-manager.ts +186 -0
  238. package/src/prove/smt/solver/random-gen.ts +10 -0
  239. package/src/prove/smt/solver/rational.ts +165 -0
  240. package/src/prove/smt/solver/ref-vector.ts +62 -0
  241. package/src/prove/smt/solver/relevancy.ts +247 -0
  242. package/src/prove/smt/solver/rewriter-tpl.ts +1159 -0
  243. package/src/prove/smt/solver/rewriter.ts +843 -0
  244. package/src/prove/smt/solver/rlimit.ts +79 -0
  245. package/src/prove/smt/solver/smtlib-parser.ts +526 -0
  246. package/src/prove/smt/solver/solver.ts +71 -0
  247. package/src/prove/smt/solver/sort.ts +35 -0
  248. package/src/prove/smt/solver/sticky-generations.ts +69 -0
  249. package/src/prove/smt/solver/theory-lra-axioms.ts +340 -0
  250. package/src/prove/smt/solver/theory-lra-bounds.ts +226 -0
  251. package/src/prove/smt/solver/theory-lra-final-check.ts +252 -0
  252. package/src/prove/smt/solver/theory-lra-internalize.ts +476 -0
  253. package/src/prove/smt/solver/theory-lra-model.ts +243 -0
  254. package/src/prove/smt/solver/theory-lra-propagate.ts +405 -0
  255. package/src/prove/smt/solver/theory-lra-state.ts +43 -0
  256. package/src/prove/smt/solver/theory-lra.ts +56 -0
  257. package/src/prove/smt/solver/var-subst.ts +258 -0
  258. package/src/prove/smt/solver/z3-license.txt +10 -0
  259. package/src/prove/smt/table.ts +170 -0
  260. package/src/prove/tlapm/tlapm.ts +97 -0
  261. package/src/prove/tlaps-module.ts +510 -0
  262. package/src/prove/verdict.ts +19 -0
  263. package/src/prove/zenon/LICENSE +32 -0
  264. package/src/prove/zenon/check/check.ts +105 -0
  265. package/src/prove/zenon/check/definitions.ts +144 -0
  266. package/src/prove/zenon/check/equality.ts +182 -0
  267. package/src/prove/zenon/check/functions.ts +62 -0
  268. package/src/prove/zenon/check/keys.ts +125 -0
  269. package/src/prove/zenon/check/propositional.ts +104 -0
  270. package/src/prove/zenon/check/records.ts +188 -0
  271. package/src/prove/zenon/check/relations.ts +121 -0
  272. package/src/prove/zenon/check/rewrites.ts +224 -0
  273. package/src/prove/zenon/check/sets.ts +302 -0
  274. package/src/prove/zenon/check/terms.ts +127 -0
  275. package/src/prove/zenon/check/tla.ts +126 -0
  276. package/src/prove/zenon/decide.ts +113 -0
  277. package/src/prove/zenon/encode.ts +444 -0
  278. package/src/prove/zenon/eqrel.ts +300 -0
  279. package/src/prove/zenon/equality-rules.ts +435 -0
  280. package/src/prove/zenon/expr.ts +551 -0
  281. package/src/prove/zenon/index-table.ts +350 -0
  282. package/src/prove/zenon/isar-case.ts +513 -0
  283. package/src/prove/zenon/isar.ts +1249 -0
  284. package/src/prove/zenon/limits.ts +15 -0
  285. package/src/prove/zenon/llproof.ts +173 -0
  286. package/src/prove/zenon/mltoll.ts +1566 -0
  287. package/src/prove/zenon/node.ts +297 -0
  288. package/src/prove/zenon/ocaml.ts +169 -0
  289. package/src/prove/zenon/parse.ts +247 -0
  290. package/src/prove/zenon/phrase.ts +191 -0
  291. package/src/prove/zenon/proof.ts +198 -0
  292. package/src/prove/zenon/propositional-rules.ts +387 -0
  293. package/src/prove/zenon/rules.ts +94 -0
  294. package/src/prove/zenon/search.ts +288 -0
  295. package/src/prove/zenon/tla/functions.ts +151 -0
  296. package/src/prove/zenon/tla/llproof.ts +480 -0
  297. package/src/prove/zenon/tla/records.ts +225 -0
  298. package/src/prove/zenon/tla/rewrites.ts +447 -0
  299. package/src/prove/zenon/tla/sets.ts +304 -0
  300. package/src/prove/zenon/tla/terms.ts +181 -0
  301. package/src/prove/zenon/tla/tla.ts +506 -0
  302. package/src/run-cli.ts +338 -52
  303. package/src/tlapm-cache.ts +250 -0
  304. package/src/values.ts +8 -3
  305. package/src/worker.ts +65 -0
  306. package/dist/parallel.cjs +0 -12
  307. package/dist/parallel.js +0 -12
@@ -0,0 +1,1159 @@
1
+ // Ported from Z3 (src/ast/rewriter/rewriter.h, rewriter.cpp, rewriter_def.h), MIT license, see
2
+ // z3-license.txt. The main loop of Z3's rewriters, ported step for step with Z3's references: the
3
+ // result stack, the caches and m_r hold their expressions, so that expressions are created and
4
+ // deleted in Z3's order. A configuration overrides the hooks of default_rewriter_cfg.
5
+ import { ActCache } from "./act-cache";
6
+ import { KIND_APP, KIND_QUANTIFIER, KIND_VAR, OP_ITE, type AstManager } from "./ast";
7
+ import { grow, shrink } from "./array";
8
+
9
+ // A result that the main loop rewrites again to depth levels, like Z3's BR_REWRITE1 (depth 1) to
10
+ // BR_REWRITE_FULL (depth Infinity).
11
+ export interface Rewrite {
12
+ expr: number;
13
+ depth: number;
14
+ }
15
+
16
+ // The depth to rewrite a Rewrite's result to. A frame keeps its depth in two bits, where 3 is
17
+ // RW_UNBOUNDED_DEPTH, so BR_REWRITE3 rewrites the result without a bound.
18
+ export const getRewriteDepth = (rewrite: Rewrite) =>
19
+ rewrite.depth >= 3 ? Infinity : rewrite.depth;
20
+
21
+ // rewriter_tpl's frame states.
22
+ const PROCESS_CHILDREN = 0;
23
+ const REWRITE_BUILTIN = 1;
24
+ const EXPAND_DEF = 2;
25
+
26
+ // A rewrite still to finish: its expression, state, next child, the depth left to rewrite, where
27
+ // its children's results start on the result stack, whether to cache its result, and whether a
28
+ // child changed.
29
+ interface Frame {
30
+ curr: number;
31
+ state: number;
32
+ childIndex: number;
33
+ maxDepth: number;
34
+ spos: number;
35
+ isCaching: boolean;
36
+ hasNewChild: boolean;
37
+ }
38
+
39
+ interface Scope {
40
+ root: number;
41
+ numQvars: number;
42
+ }
43
+
44
+ // rewriter_core: the frames, the result stack and one cache per quantifier scope.
45
+ class RewriterCore {
46
+ // m_frame_stack: the first numFrames frames are open. Like the svector, the stack keeps the frames
47
+ // past them, which pushFrame writes over.
48
+ protected frames: Frame[] = [];
49
+ protected numFrames = 0;
50
+ protected resultStack: number[] = [];
51
+ protected caches: ActCache[];
52
+ protected scopes: Scope[] = [];
53
+ protected root = -1;
54
+ protected numQvars = 0;
55
+
56
+ constructor(protected manager: AstManager) {
57
+ this.caches = [new ActCache(manager, manager.getNumAsts())];
58
+ }
59
+
60
+ protected get cache() {
61
+ return this.caches[this.scopes.length];
62
+ }
63
+
64
+ // must_cache: the result of a shared application with arguments or quantifier.
65
+ protected isCaching(expr: number) {
66
+ const manager = this.manager;
67
+ return (
68
+ manager.getRefCount(expr) > 1 &&
69
+ expr !== this.root &&
70
+ (manager.kinds[expr] === KIND_QUANTIFIER ||
71
+ (manager.kinds[expr] === KIND_APP && manager.getNumArgs(expr) > 0))
72
+ );
73
+ }
74
+
75
+ protected pushResult(expr: number) {
76
+ this.manager.incRef(expr);
77
+ this.resultStack.push(expr);
78
+ }
79
+
80
+ protected shrinkResults(size: number) {
81
+ const resultStack = this.resultStack;
82
+ for (let index = size; index < resultStack.length; index++)
83
+ this.manager.decRef(resultStack[index]);
84
+ shrink(resultStack, size);
85
+ }
86
+
87
+ protected setNewChildFlag(oldExpr: number, newExpr: number) {
88
+ if (oldExpr !== newExpr && this.numFrames > 0)
89
+ this.frames[this.numFrames - 1].hasNewChild = true;
90
+ }
91
+
92
+ protected pushFrame(expr: number, isCaching: boolean, maxDepth: number) {
93
+ if (this.numFrames === this.frames.length)
94
+ this.frames.push({
95
+ curr: expr,
96
+ state: PROCESS_CHILDREN,
97
+ childIndex: 0,
98
+ maxDepth,
99
+ spos: this.resultStack.length,
100
+ isCaching,
101
+ hasNewChild: false,
102
+ });
103
+ else {
104
+ const frame = this.frames[this.numFrames];
105
+ frame.curr = expr;
106
+ frame.state = PROCESS_CHILDREN;
107
+ frame.childIndex = 0;
108
+ frame.maxDepth = maxDepth;
109
+ frame.spos = this.resultStack.length;
110
+ frame.isCaching = isCaching;
111
+ frame.hasNewChild = false;
112
+ }
113
+ this.numFrames++;
114
+ }
115
+
116
+ // begin_scope: each quantifier level gets its own cache, which starts empty.
117
+ protected beginScope() {
118
+ this.scopes.push({ root: this.root, numQvars: this.numQvars });
119
+ if (this.scopes.length === this.caches.length)
120
+ this.caches.push(new ActCache(this.manager, this.manager.getNumAsts()));
121
+ this.cache.reset();
122
+ }
123
+
124
+ protected endScope() {
125
+ this.cache.reset();
126
+ const scope = this.scopes[this.scopes.length - 1];
127
+ this.scopes.pop();
128
+ this.root = scope.root;
129
+ this.numQvars = scope.numQvars;
130
+ }
131
+
132
+ // rewriter_core::reset: releases the outermost cache and the result stack.
133
+ protected resetCore() {
134
+ this.caches[0].reset();
135
+ this.numFrames = 0;
136
+ this.shrinkResults(0);
137
+ this.root = -1;
138
+ this.numQvars = 0;
139
+ this.scopes = [];
140
+ }
141
+
142
+ // del_cache_stack: releases every cache, the outermost first.
143
+ protected releaseCaches() {
144
+ for (const cache of this.caches) cache.reset();
145
+ }
146
+
147
+ // rewriter_core::cleanup.
148
+ protected cleanupCore() {
149
+ this.releaseCaches();
150
+ this.shrinkResults(0);
151
+ this.caches = [new ActCache(this.manager, this.manager.getNumAsts())];
152
+ this.frames = [];
153
+ this.numFrames = 0;
154
+ this.scopes = [];
155
+ this.root = -1;
156
+ this.numQvars = 0;
157
+ }
158
+ }
159
+
160
+ // var_shifter_core: rebuilds an expression with its free variables renamed by processVar.
161
+ abstract class VarShifterCore extends RewriterCore {
162
+ protected abstract processVar(variable: number): void;
163
+
164
+ private visit(expr: number) {
165
+ const manager = this.manager;
166
+ if (manager.isGroundApp(expr)) {
167
+ this.pushResult(expr);
168
+ return true;
169
+ }
170
+ const isCaching = this.isCaching(expr);
171
+ if (isCaching) {
172
+ const cached = this.cache.find(expr, 0);
173
+ if (cached !== undefined) {
174
+ this.pushResult(cached);
175
+ this.setNewChildFlag(expr, cached);
176
+ return true;
177
+ }
178
+ }
179
+ if (manager.kinds[expr] === KIND_VAR) {
180
+ this.processVar(expr);
181
+ return true;
182
+ }
183
+ this.pushFrame(expr, isCaching, Infinity);
184
+ return false;
185
+ }
186
+
187
+ private processApp(expr: number, frame: Frame) {
188
+ const manager = this.manager;
189
+ const numArgs = manager.getNumArgs(expr);
190
+ while (frame.childIndex < numArgs) {
191
+ const arg = manager.getArg(expr, frame.childIndex);
192
+ frame.childIndex++;
193
+ if (!this.visit(arg)) return;
194
+ }
195
+ const result = frame.hasNewChild
196
+ ? manager.mkApp(manager.decls[expr], this.resultStack.slice(frame.spos), manager.sorts[expr])
197
+ : expr;
198
+ this.finish(expr, frame, result);
199
+ if (frame.isCaching) this.cache.insert(expr, 0, result);
200
+ }
201
+
202
+ private processQuantifier(expr: number, frame: Frame) {
203
+ const manager = this.manager;
204
+ const data = manager.getQuantifier(expr);
205
+ if (frame.childIndex === 0) {
206
+ this.beginScope();
207
+ this.numQvars += data.sorts.length;
208
+ this.root = data.body;
209
+ }
210
+ const children = [data.body, ...data.patterns];
211
+ while (frame.childIndex < children.length) {
212
+ const child = children[frame.childIndex];
213
+ frame.childIndex++;
214
+ if (!this.visit(child)) return;
215
+ }
216
+ const result = frame.hasNewChild
217
+ ? manager.mkQuantifier({
218
+ ...data,
219
+ body: this.resultStack[frame.spos],
220
+ patterns: this.resultStack.slice(frame.spos + 1),
221
+ })
222
+ : expr;
223
+ this.finish(expr, frame, result);
224
+ this.endScope();
225
+ if (frame.isCaching) this.cache.insert(expr, 0, result);
226
+ }
227
+
228
+ private finish(expr: number, frame: Frame, result: number) {
229
+ this.shrinkResults(frame.spos);
230
+ this.pushResult(result);
231
+ this.numFrames--;
232
+ this.setNewChildFlag(expr, result);
233
+ }
234
+
235
+ // main_loop: the result comes with a reference, which the caller releases.
236
+ protected mainLoop(expr: number) {
237
+ this.root = expr;
238
+ if (!this.visit(expr))
239
+ while (this.numFrames > 0) {
240
+ const frame = this.frames[this.numFrames - 1];
241
+ const curr = frame.curr;
242
+ if (frame.childIndex === 0 && frame.isCaching) {
243
+ const cached = this.cache.find(curr, 0);
244
+ if (cached !== undefined) {
245
+ this.pushResult(cached);
246
+ this.numFrames--;
247
+ this.setNewChildFlag(curr, cached);
248
+ continue;
249
+ }
250
+ }
251
+ if (this.manager.kinds[curr] === KIND_APP) this.processApp(curr, frame);
252
+ else this.processQuantifier(curr, frame);
253
+ }
254
+ const result = this.resultStack[this.resultStack.length - 1];
255
+ this.resultStack.pop();
256
+ return result;
257
+ }
258
+
259
+ reset() {
260
+ this.resetCore();
261
+ }
262
+
263
+ cleanup() {
264
+ this.cleanupCore();
265
+ }
266
+
267
+ release() {
268
+ this.releaseCaches();
269
+ }
270
+ }
271
+
272
+ // var_shifter: a free variable with index at least bound moves up by shift1, any other by shift2.
273
+ export class VarShifter extends VarShifterCore {
274
+ private bound = 0;
275
+ private shift1 = 0;
276
+ private shift2 = 0;
277
+
278
+ // Returns the result with a reference, which the caller releases.
279
+ shift(expr: number, bound: number, shift1: number, shift2: number) {
280
+ if (this.manager.isGroundApp(expr)) {
281
+ this.manager.incRef(expr);
282
+ return expr;
283
+ }
284
+ this.caches[0].reset();
285
+ this.bound = bound;
286
+ this.shift1 = shift1;
287
+ this.shift2 = shift2;
288
+ return this.mainLoop(expr);
289
+ }
290
+
291
+ protected processVar(variable: number) {
292
+ const manager = this.manager;
293
+ const index = manager.values[variable];
294
+ if (index < this.numQvars) {
295
+ this.pushResult(variable);
296
+ return;
297
+ }
298
+ const shifted = index + (index - this.numQvars >= this.bound ? this.shift1 : this.shift2);
299
+ this.pushResult(manager.mkVar(shifted, manager.sorts[variable]));
300
+ this.setNewChildFlag(variable, -1);
301
+ }
302
+ }
303
+
304
+ // inv_var_shifter: the free variables move down by shift.
305
+ export class InvVarShifter extends VarShifterCore {
306
+ private amount = 0;
307
+
308
+ // Returns the result with a reference, which the caller releases.
309
+ shift(expr: number, amount: number) {
310
+ if (this.manager.isGroundApp(expr) || amount === 0) {
311
+ this.manager.incRef(expr);
312
+ return expr;
313
+ }
314
+ this.caches[0].reset();
315
+ this.amount = amount;
316
+ return this.mainLoop(expr);
317
+ }
318
+
319
+ protected processVar(variable: number) {
320
+ const manager = this.manager;
321
+ const index = manager.values[variable];
322
+ if (index < this.numQvars) {
323
+ this.pushResult(variable);
324
+ return;
325
+ }
326
+ this.pushResult(manager.mkVar(index - this.amount, manager.sorts[variable]));
327
+ this.setNewChildFlag(variable, -1);
328
+ }
329
+ }
330
+
331
+ // The bindings of rewriter_tpl's substitution.
332
+ class SubstCore extends RewriterCore {
333
+ // m_bindings and m_shifts: the binding of each variable of the substitution, innermost last, and
334
+ // the number of bindings when it was bound.
335
+ protected bindings: number[] = [];
336
+ protected shifts: number[] = [];
337
+
338
+ // set_inv_bindings: (:var i) becomes bindings[n - 1 - i], and -1 keeps it.
339
+ setInvBindings(bindings: number[]) {
340
+ shrink(this.bindings, 0);
341
+ shrink(this.shifts, 0);
342
+ for (const binding of bindings) {
343
+ this.bindings.push(binding);
344
+ this.shifts.push(bindings.length);
345
+ }
346
+ }
347
+
348
+ // set_bindings: (:var i) becomes bindings[i].
349
+ setBindings(bindings: number[]) {
350
+ shrink(this.bindings, 0);
351
+ shrink(this.shifts, 0);
352
+ for (let index = bindings.length - 1; index >= 0; index--) {
353
+ this.bindings.push(bindings[index]);
354
+ this.shifts.push(bindings.length);
355
+ }
356
+ }
357
+
358
+ updateInvBindingAt(index: number, binding: number) {
359
+ this.bindings[index] = binding;
360
+ }
361
+ }
362
+
363
+ // rewriter_tpl's main loop, which calls the hooks of its configuration. It is a class of its own,
364
+ // rather than the base of the configurations, so that V8 sees one shape of it and keeps its property
365
+ // accesses and calls monomorphic.
366
+ class RewriterEngine extends SubstCore {
367
+ // m_r: the last result, which Z3 holds until it assigns the next one.
368
+ private lastResult = -1;
369
+ private newArgs: number[] = [];
370
+ private shifter: VarShifter;
371
+ private invShifter: InvVarShifter;
372
+ // m_blocked: the constants that a nested rewriter does not expand.
373
+ blocked = new Set<number>();
374
+ // Whether the config overrides getSubst and getMacro. Otherwise nothing is substituted or expanded
375
+ // and the call is skipped.
376
+ private isSubstituting: boolean;
377
+ private isExpandingMacros: boolean;
378
+ // The config's hooks, read once. Many configs share the engine, so a call through config at each
379
+ // site sees all of their classes and goes megamorphic.
380
+ private reduceAppHook: RewriterTpl["reduceApp"];
381
+ private isFlatAssocHook: RewriterTpl["isFlatAssoc"];
382
+ private getMacroHook: RewriterTpl["getMacro"];
383
+ private getSubstHook: RewriterTpl["getSubst"];
384
+
385
+ constructor(
386
+ manager: AstManager,
387
+ private config: RewriterTpl,
388
+ ) {
389
+ super(manager);
390
+ this.isSubstituting = config.getSubst !== RewriterTpl.prototype.getSubst;
391
+ this.isExpandingMacros = config.getMacro !== RewriterTpl.prototype.getMacro;
392
+ this.reduceAppHook = config.reduceApp;
393
+ this.isFlatAssocHook = config.isFlatAssoc;
394
+ this.getMacroHook = config.getMacro;
395
+ this.getSubstHook = config.getSubst;
396
+ this.shifter = new VarShifter(manager);
397
+ this.invShifter = new InvVarShifter(manager);
398
+ }
399
+
400
+ // reset: releases the outermost cache and the bindings.
401
+ reset() {
402
+ this.resetCore();
403
+ shrink(this.bindings, 0);
404
+ shrink(this.shifts, 0);
405
+ this.shifter.reset();
406
+ this.invShifter.reset();
407
+ }
408
+
409
+ // cleanup: releases every cache.
410
+ cleanup() {
411
+ this.cleanupCore();
412
+ this.bindings = [];
413
+ this.shifter.cleanup();
414
+ this.invShifter.cleanup();
415
+ this.shifts = [];
416
+ }
417
+
418
+ // ~rewriter_tpl: the shifters' caches go first, the inverse shifter's before the other's, then
419
+ // the rewriter's.
420
+ release() {
421
+ this.invShifter.release();
422
+ this.shifter.release();
423
+ this.releaseCaches();
424
+ }
425
+
426
+ // operator(): the result comes with a reference, which the caller releases.
427
+ rewrite(expr: number): number {
428
+ const manager = this.manager;
429
+ return manager.withoutTemporaries(() => {
430
+ if (this.numFrames > 0 || this.scopes.length > 0) {
431
+ this.numFrames = 0;
432
+ this.shrinkResults(0);
433
+ this.scopes = [];
434
+ this.caches[0].reset();
435
+ }
436
+ this.root = expr;
437
+ this.numQvars = 0;
438
+ // main_loop and each step of resume_core count a step of the resource limit, and throw
439
+ // SmtCanceledError when it cancels the rewrite.
440
+ manager.limit.checkpoint();
441
+ if (!this.visit(expr, Infinity))
442
+ while (this.numFrames > 0) {
443
+ manager.limit.checkpoint();
444
+ const frame = this.frames[this.numFrames - 1];
445
+ const curr = frame.curr;
446
+ if (frame.childIndex === 0 && frame.state === PROCESS_CHILDREN && frame.isCaching) {
447
+ const cached = this.cache.find(curr, 0);
448
+ if (cached !== undefined) {
449
+ this.pushResult(cached);
450
+ this.numFrames--;
451
+ this.setNewChildFlag(curr, cached);
452
+ continue;
453
+ }
454
+ }
455
+ const kind = manager.kinds[curr];
456
+ if (kind === KIND_APP) this.processApp(curr, frame);
457
+ else if (kind === KIND_QUANTIFIER) this.processQuantifier(curr, frame);
458
+ else {
459
+ // A definition that is a variable gets a frame of its own.
460
+ this.numFrames--;
461
+ this.processVar(curr);
462
+ }
463
+ }
464
+ const result = this.resultStack[this.resultStack.length - 1];
465
+ this.resultStack.pop();
466
+ return result;
467
+ });
468
+ }
469
+
470
+ // operator() with bindings: resets the rewriter and substitutes bindings[n - 1 - i] for (:var i).
471
+ rewriteWithBindings(expr: number, bindings: number[]) {
472
+ this.reset();
473
+ this.setInvBindings(bindings);
474
+ return this.rewrite(expr);
475
+ }
476
+
477
+ setLastResult(expr: number) {
478
+ if (expr !== -1) this.manager.incRef(expr);
479
+ if (this.lastResult !== -1) this.manager.decRef(this.lastResult);
480
+ this.lastResult = expr;
481
+ }
482
+
483
+ private cacheResult(expr: number, result: number, isCaching: boolean) {
484
+ if (isCaching) this.cache.insert(expr, 0, result);
485
+ }
486
+
487
+ // visit: whether expr is rewritten and its result is on the result stack. Otherwise it pushes a
488
+ // frame for expr.
489
+ private visit(expr: number, maxDepth: number) {
490
+ const manager = this.manager;
491
+ const replacement = this.isSubstituting ? this.getSubstHook.call(this.config, expr) : undefined;
492
+ if (replacement !== undefined) {
493
+ this.pushResult(replacement);
494
+ this.setNewChildFlag(expr, replacement);
495
+ return true;
496
+ }
497
+ if (maxDepth === 0) {
498
+ this.pushResult(expr);
499
+ return true;
500
+ }
501
+ const isCaching = this.isCaching(expr);
502
+ if (isCaching) {
503
+ const cached = this.cache.find(expr, 0);
504
+ if (cached !== undefined) {
505
+ this.pushResult(cached);
506
+ this.setNewChildFlag(expr, cached);
507
+ return true;
508
+ }
509
+ }
510
+ const kind = manager.kinds[expr];
511
+ if (kind === KIND_VAR) {
512
+ this.processVar(expr);
513
+ return true;
514
+ }
515
+ if (kind === KIND_APP && manager.getNumArgs(expr) === 0) {
516
+ if (!this.processConst(expr)) this.rewriteConstResult(expr);
517
+ return true;
518
+ }
519
+ this.pushFrame(expr, isCaching, maxDepth === Infinity ? maxDepth : maxDepth - 1);
520
+ return false;
521
+ }
522
+
523
+ // process_const: a constant is reduced, and a constant it rewrites to is reduced again. It
524
+ // returns false when the constant rewrites to a term with arguments, which m_r holds. The constant
525
+ // being reduced is held, like Z3's t.
526
+ private processConst(expr: number) {
527
+ const manager = this.manager;
528
+ let current = expr;
529
+ manager.incRef(current);
530
+ for (let isRetried = false; ; isRetried = true) {
531
+ const reduced = this.reduceAppHook.call(
532
+ this.config,
533
+ manager.decls[current],
534
+ [],
535
+ manager.sorts[current],
536
+ );
537
+ if (reduced !== -1) {
538
+ const result = typeof reduced === "number" ? reduced : reduced.expr;
539
+ this.setLastResult(result);
540
+ manager.decRef(result);
541
+ }
542
+ if (reduced === -1 && !isRetried) {
543
+ this.pushResult(current);
544
+ manager.decRef(current);
545
+ return true;
546
+ }
547
+ if (typeof reduced === "number") {
548
+ if (reduced === -1) this.setLastResult(current);
549
+ this.pushResult(this.lastResult);
550
+ this.setLastResult(-1);
551
+ this.setNewChildFlag(expr, -1);
552
+ manager.decRef(current);
553
+ return true;
554
+ }
555
+ const result = reduced.expr;
556
+ if (manager.kinds[result] !== KIND_APP || manager.getNumArgs(result) > 0) {
557
+ manager.decRef(current);
558
+ return false;
559
+ }
560
+ manager.incRef(result);
561
+ manager.decRef(current);
562
+ current = result;
563
+ }
564
+ }
565
+
566
+ // A constant that rewrites to a term with arguments: a rewriter with the same configuration
567
+ // rewrites the term, with the constant blocked so that it is not expanded again. m_r keeps the
568
+ // result.
569
+ private rewriteConstResult(expr: number) {
570
+ const manager = this.manager;
571
+ if (!this.blocked.has(expr)) {
572
+ const nested = this.config.mkNested();
573
+ for (const blocked of this.blocked) nested.engine.blocked.add(blocked);
574
+ nested.engine.blocked.add(expr);
575
+ const result = nested.rewrite(this.lastResult);
576
+ this.setLastResult(result);
577
+ manager.decRef(result);
578
+ nested.release();
579
+ }
580
+ this.setNewChildFlag(expr, -1);
581
+ this.pushResult(this.lastResult);
582
+ }
583
+
584
+ // process_var: a bound variable becomes its binding, shifted over the quantifiers entered since
585
+ // it was bound.
586
+ private processVar(variable: number) {
587
+ const manager = this.manager;
588
+ const index = manager.values[variable];
589
+ const position = this.bindings.length - index - 1;
590
+ const binding = index < this.bindings.length ? this.bindings[position] : -1;
591
+ if (binding === -1) {
592
+ this.pushResult(variable);
593
+ return;
594
+ }
595
+ const amount = this.bindings.length - this.shifts[position];
596
+ if (manager.isGroundApp(binding) || amount === 0) this.pushResult(binding);
597
+ else {
598
+ const cached = this.cache.find(binding, amount);
599
+ if (cached !== undefined) this.pushResult(cached);
600
+ else {
601
+ const shifted = this.shifter.shift(binding, 0, amount, 0);
602
+ this.pushResult(shifted);
603
+ this.cache.insert(binding, amount, shifted);
604
+ manager.decRef(shifted);
605
+ }
606
+ }
607
+ this.setNewChildFlag(variable, -1);
608
+ }
609
+
610
+ private processApp(expr: number, frame: Frame) {
611
+ const manager = this.manager;
612
+ if (frame.state === REWRITE_BUILTIN) {
613
+ this.finishRewriteBuiltin(expr, frame);
614
+ return;
615
+ }
616
+ if (frame.state === EXPAND_DEF) {
617
+ this.finishExpandDef(expr, frame);
618
+ return;
619
+ }
620
+ const numArgs = manager.getNumArgs(expr);
621
+ // visit pushes a child at depth 0 as it is when the config substitutes nothing.
622
+ if (frame.maxDepth === 0 && !this.isSubstituting && manager.getOp(expr) !== OP_ITE)
623
+ for (; frame.childIndex < numArgs; frame.childIndex++)
624
+ this.pushResult(manager.getArg(expr, frame.childIndex));
625
+ while (frame.childIndex < numArgs) {
626
+ if (this.isConstantFolded(expr, frame)) return;
627
+ const arg = manager.getArg(expr, frame.childIndex);
628
+ frame.childIndex++;
629
+ if (!this.visit(arg, frame.maxDepth)) return;
630
+ }
631
+ const decl = manager.decls[expr];
632
+ if (
633
+ this.isFlatAssocHook.call(this.config, decl) &&
634
+ manager.getRefCount(expr) <= 1 &&
635
+ this.numFrames > 1
636
+ ) {
637
+ const previous = this.frames[this.numFrames - 2];
638
+ if (
639
+ manager.kinds[previous.curr] === KIND_APP &&
640
+ manager.decls[previous.curr] === decl &&
641
+ previous.state === PROCESS_CHILDREN
642
+ ) {
643
+ this.numFrames--;
644
+ this.setNewChildFlag(expr, -1);
645
+ return;
646
+ }
647
+ }
648
+ // new_args: Z3 passes the results on the stack itself. Nothing rewrites with this engine before
649
+ // the arguments are used, so one buffer holds them.
650
+ const newArgs = this.newArgs;
651
+ const resultStack = this.resultStack;
652
+ shrink(newArgs, 0);
653
+ for (let index = frame.spos; index < resultStack.length; index++)
654
+ newArgs.push(resultStack[index]);
655
+ const reduced = this.reduceAppHook.call(this.config, decl, newArgs, manager.sorts[expr]);
656
+ if (typeof reduced !== "number") {
657
+ // BR_REWRITE: the result replaces the arguments and is rewritten to the depth it asks for.
658
+ this.setLastResult(reduced.expr);
659
+ manager.decRef(reduced.expr);
660
+ this.shrinkResults(frame.spos);
661
+ this.pushResult(reduced.expr);
662
+ frame.state = REWRITE_BUILTIN;
663
+ if (this.visit(reduced.expr, getRewriteDepth(reduced)))
664
+ this.finishRewriteBuiltin(expr, frame);
665
+ this.setLastResult(-1);
666
+ return;
667
+ }
668
+ const definition =
669
+ reduced === -1 && this.isExpandingMacros ? this.getMacroHook.call(this.config, decl) : -1;
670
+ if (definition !== -1 && (!manager.isGroundApp(definition) || this.config.isReducingMacro())) {
671
+ // The definition is rewritten with the arguments bound to its variables.
672
+ frame.state = EXPAND_DEF;
673
+ const size = this.bindings.length;
674
+ for (let index = newArgs.length - 1; index >= 0; index--) {
675
+ this.bindings.push(newArgs[index]);
676
+ this.shifts.push(size);
677
+ }
678
+ this.pushResult(definition);
679
+ this.beginScope();
680
+ this.numQvars += newArgs.length;
681
+ this.root = definition;
682
+ this.pushFrame(definition, false, Infinity);
683
+ return;
684
+ }
685
+ if (definition !== -1) this.setLastResult(definition);
686
+ else if (reduced === -1)
687
+ this.setLastResult(
688
+ frame.hasNewChild ? manager.mkApp(decl, newArgs, manager.sorts[expr]) : expr,
689
+ );
690
+ else {
691
+ this.setLastResult(reduced);
692
+ manager.decRef(reduced);
693
+ }
694
+ const result = this.lastResult;
695
+ this.shrinkResults(frame.spos);
696
+ this.pushResult(result);
697
+ this.cacheResult(expr, result, frame.isCaching);
698
+ this.numFrames--;
699
+ this.setNewChildFlag(expr, result);
700
+ this.setLastResult(-1);
701
+ }
702
+
703
+ // REWRITE_BUILTIN: the result of the rewritten term replaces the term on the result stack.
704
+ private finishRewriteBuiltin(expr: number, frame: Frame) {
705
+ const result = this.resultStack[this.resultStack.length - 1];
706
+ this.setLastResult(result);
707
+ this.shrinkResults(this.resultStack.length - 1);
708
+ this.shrinkResults(this.resultStack.length - 1);
709
+ this.pushResult(result);
710
+ this.cacheResult(expr, result, frame.isCaching);
711
+ this.numFrames--;
712
+ this.setNewChildFlag(expr, -1);
713
+ }
714
+
715
+ // EXPAND_DEF: the rewritten definition, with its variables moved back down, replaces the term.
716
+ // m_r keeps it until the next result.
717
+ private finishExpandDef(expr: number, frame: Frame) {
718
+ const manager = this.manager;
719
+ const numArgs = manager.getNumArgs(expr);
720
+ shrink(this.bindings, this.bindings.length - numArgs);
721
+ shrink(this.shifts, this.shifts.length - numArgs);
722
+ this.numQvars -= numArgs;
723
+ this.endScope();
724
+ const rewritten = this.resultStack[this.resultStack.length - 1];
725
+ if (manager.isGroundApp(rewritten)) this.setLastResult(rewritten);
726
+ else {
727
+ const shifted = this.invShifter.shift(rewritten, numArgs);
728
+ this.setLastResult(shifted);
729
+ manager.decRef(shifted);
730
+ }
731
+ const result = this.lastResult;
732
+ this.shrinkResults(frame.spos);
733
+ this.pushResult(result);
734
+ this.cacheResult(expr, result, frame.isCaching);
735
+ this.numFrames--;
736
+ this.setNewChildFlag(expr, -1);
737
+ }
738
+
739
+ // constant_fold: an ite whose condition rewrote to true or false becomes the chosen branch.
740
+ private isConstantFolded(expr: number, frame: Frame) {
741
+ const manager = this.manager;
742
+ if (frame.childIndex !== 1 || manager.getOp(expr) !== OP_ITE) return false;
743
+ const condition = this.resultStack[frame.spos];
744
+ const arg = manager.isTrue(condition)
745
+ ? manager.getArg(expr, 1)
746
+ : manager.isFalse(condition)
747
+ ? manager.getArg(expr, 2)
748
+ : -1;
749
+ if (arg === -1) return false;
750
+ this.shrinkResults(frame.spos);
751
+ this.pushResult(arg);
752
+ frame.state = REWRITE_BUILTIN;
753
+ if (this.visit(arg, frame.maxDepth)) this.finishRewriteBuiltin(expr, frame);
754
+ this.setLastResult(-1);
755
+ return true;
756
+ }
757
+
758
+ private processQuantifier(expr: number, frame: Frame) {
759
+ const manager = this.manager;
760
+ const data = manager.getQuantifier(expr);
761
+ const numDecls = data.sorts.length;
762
+ if (frame.childIndex === 0) {
763
+ this.beginScope();
764
+ this.root = data.body;
765
+ const size = this.bindings.length;
766
+ for (let index = 0; index < numDecls; index++) {
767
+ this.bindings.push(-1);
768
+ this.shifts.push(size);
769
+ }
770
+ this.numQvars += numDecls;
771
+ }
772
+ const isRewritingPatterns = this.config.isRewritingPatterns();
773
+ const children = isRewritingPatterns ? [data.body, ...data.patterns] : [data.body];
774
+ while (frame.childIndex < children.length) {
775
+ const child = children[frame.childIndex];
776
+ frame.childIndex++;
777
+ if (!this.visit(child, frame.maxDepth)) return;
778
+ }
779
+ const body = this.resultStack[frame.spos];
780
+ // new_pats: the patterns, which Z3 holds until the quantifier is done.
781
+ const patterns = isRewritingPatterns ? this.resultStack.slice(frame.spos + 1) : data.patterns;
782
+ for (const pattern of patterns) manager.incRef(pattern);
783
+ const reduced = this.config.reduceQuantifier(expr, body, patterns);
784
+ if (reduced === -1)
785
+ this.setLastResult(
786
+ frame.hasNewChild ? manager.mkQuantifier({ ...data, body, patterns }) : expr,
787
+ );
788
+ else {
789
+ this.setLastResult(reduced);
790
+ manager.decRef(reduced);
791
+ }
792
+ const result = this.lastResult;
793
+ this.shrinkResults(frame.spos);
794
+ this.pushResult(result);
795
+ shrink(this.bindings, this.bindings.length - numDecls);
796
+ shrink(this.shifts, this.shifts.length - numDecls);
797
+ this.endScope();
798
+ this.cacheResult(expr, result, frame.isCaching);
799
+ this.setLastResult(-1);
800
+ this.numFrames--;
801
+ this.setNewChildFlag(expr, result);
802
+ for (const pattern of patterns) manager.decRef(pattern);
803
+ }
804
+ }
805
+
806
+ // rewriter_tpl with default_rewriter_cfg. A configuration overrides the hooks. The results of
807
+ // reduceApp and reduceQuantifier come with a reference, and -1 is Z3's BR_FAILED or false. A
808
+ // Rewrite from reduceApp is rewritten again.
809
+ export class RewriterTpl {
810
+ readonly engine: RewriterEngine;
811
+
812
+ constructor(protected manager: AstManager) {
813
+ this.engine = new RewriterEngine(manager, this);
814
+ }
815
+
816
+ isFlatAssoc(_decl: number) {
817
+ return false;
818
+ }
819
+
820
+ isRewritingPatterns() {
821
+ return true;
822
+ }
823
+
824
+ getSubst(_expr: number): number | undefined {
825
+ return undefined;
826
+ }
827
+
828
+ reduceApp(_decl: number, _args: number[], _sort: number): number | Rewrite {
829
+ return -1;
830
+ }
831
+
832
+ // get_macro: the definition of decl over (:var i) for argument i, or -1.
833
+ getMacro(_decl: number) {
834
+ return -1;
835
+ }
836
+
837
+ // reduce_macro: whether a ground definition is rewritten rather than taken as it is.
838
+ isReducingMacro() {
839
+ return false;
840
+ }
841
+
842
+ reduceQuantifier(_expr: number, _body: number, _patterns: number[]) {
843
+ return -1;
844
+ }
845
+
846
+ // A rewriter with the same configuration, for a constant that rewrites to a term with arguments.
847
+ mkNested(): RewriterTpl {
848
+ return new RewriterTpl(this.manager);
849
+ }
850
+
851
+ setInvBindings(bindings: number[]) {
852
+ this.engine.setInvBindings(bindings);
853
+ }
854
+
855
+ setBindings(bindings: number[]) {
856
+ this.engine.setBindings(bindings);
857
+ }
858
+
859
+ updateInvBindingAt(index: number, binding: number) {
860
+ this.engine.updateInvBindingAt(index, binding);
861
+ }
862
+
863
+ reset() {
864
+ this.engine.reset();
865
+ }
866
+
867
+ cleanup() {
868
+ this.engine.cleanup();
869
+ }
870
+
871
+ release() {
872
+ this.engine.release();
873
+ }
874
+
875
+ rewrite(expr: number) {
876
+ return this.engine.rewrite(expr);
877
+ }
878
+
879
+ rewriteWithBindings(expr: number, bindings: number[]) {
880
+ return this.engine.rewriteWithBindings(expr, bindings);
881
+ }
882
+
883
+ protected setLastResult(expr: number) {
884
+ this.engine.setLastResult(expr);
885
+ }
886
+ }
887
+
888
+ // The flags of a BetaReducer frame: whether to cache its result, whether a child changed, and
889
+ // whether it waits for the branch that an ite folded to, REWRITE_BUILTIN.
890
+ const FRAME_CACHING = 1;
891
+ const FRAME_NEW_CHILD = 2;
892
+ const FRAME_FOLDED = 4;
893
+
894
+ // beta_reducer: rewriter_tpl with beta_reducer_cfg, which substitutes bindings for variables and
895
+ // rewrites nothing else. Its main loop is RewriterEngine's with the config's hooks folded in:
896
+ // pre_visit skips a ground term, and nothing reduces, expands or rewrites to a depth. So a frame
897
+ // needs no depth and only the ite folding's state, and its fields are typed arrays.
898
+ export class BetaReducer extends SubstCore {
899
+ // m_r: the last result, which Z3 holds until it assigns the next one.
900
+ private lastResult = -1;
901
+ private shifter: VarShifter;
902
+ // Whether the shifter's cache has entries, which reset releases.
903
+ private isShifted = false;
904
+ private frameExprs = new Int32Array(16);
905
+ private frameChildIndexes = new Int32Array(16);
906
+ private frameStarts = new Int32Array(16);
907
+ private frameFlags = new Uint8Array(16);
908
+
909
+ constructor(manager: AstManager) {
910
+ super(manager);
911
+ this.shifter = new VarShifter(manager);
912
+ }
913
+
914
+ // reset: releases the outermost cache and the bindings. The inverse shifter of rewriter_tpl only
915
+ // shifts an expanded definition, so it is always empty and left out.
916
+ reset() {
917
+ this.resetCore();
918
+ shrink(this.bindings, 0);
919
+ shrink(this.shifts, 0);
920
+ if (!this.isShifted) return;
921
+ this.shifter.reset();
922
+ this.isShifted = false;
923
+ }
924
+
925
+ // ~rewriter_tpl: the shifter's cache goes first, then the rewriter's.
926
+ release() {
927
+ this.shifter.release();
928
+ this.releaseCaches();
929
+ }
930
+
931
+ // operator(): the result comes with a reference, which the caller releases.
932
+ rewrite(expr: number): number {
933
+ const manager = this.manager;
934
+ return manager.withoutTemporaries(() => {
935
+ if (this.numFrames > 0 || this.scopes.length > 0) {
936
+ this.numFrames = 0;
937
+ this.shrinkResults(0);
938
+ this.scopes = [];
939
+ this.caches[0].reset();
940
+ }
941
+ this.root = expr;
942
+ this.numQvars = 0;
943
+ // main_loop and each step of resume_core count a step of the resource limit, and throw
944
+ // SmtCanceledError when it cancels the rewrite.
945
+ manager.limit.checkpoint();
946
+ if (!this.visit(expr))
947
+ while (this.numFrames > 0) {
948
+ manager.limit.checkpoint();
949
+ const top = this.numFrames - 1;
950
+ const curr = this.frameExprs[top];
951
+ if (
952
+ this.frameChildIndexes[top] === 0 &&
953
+ (this.frameFlags[top] & (FRAME_CACHING | FRAME_FOLDED)) === FRAME_CACHING
954
+ ) {
955
+ const cached = this.cache.find(curr, 0);
956
+ if (cached !== undefined) {
957
+ this.pushResult(cached);
958
+ this.numFrames--;
959
+ this.markNewChild(curr, cached);
960
+ continue;
961
+ }
962
+ }
963
+ if (manager.kinds[curr] === KIND_APP) this.processApp(curr, top);
964
+ else this.processQuantifier(curr, top);
965
+ }
966
+ const result = this.resultStack[this.resultStack.length - 1];
967
+ this.resultStack.pop();
968
+ return result;
969
+ });
970
+ }
971
+
972
+ // operator() with bindings: resets the rewriter and substitutes bindings[n - 1 - i] for (:var i).
973
+ rewriteWithBindings(expr: number, bindings: number[]) {
974
+ this.reset();
975
+ this.setInvBindings(bindings);
976
+ return this.rewrite(expr);
977
+ }
978
+
979
+ private setLastResult(expr: number) {
980
+ if (expr !== -1) this.manager.incRef(expr);
981
+ if (this.lastResult !== -1) this.manager.decRef(this.lastResult);
982
+ this.lastResult = expr;
983
+ }
984
+
985
+ private markNewChild(oldExpr: number, newExpr: number) {
986
+ if (oldExpr !== newExpr && this.numFrames > 0)
987
+ this.frameFlags[this.numFrames - 1] |= FRAME_NEW_CHILD;
988
+ }
989
+
990
+ private openFrame(expr: number, isCaching: boolean) {
991
+ const index = this.numFrames;
992
+ if (index === this.frameExprs.length) {
993
+ this.frameExprs = grow(this.frameExprs);
994
+ this.frameChildIndexes = grow(this.frameChildIndexes);
995
+ this.frameStarts = grow(this.frameStarts);
996
+ this.frameFlags = grow(this.frameFlags);
997
+ }
998
+ this.frameExprs[index] = expr;
999
+ this.frameChildIndexes[index] = 0;
1000
+ this.frameStarts[index] = this.resultStack.length;
1001
+ this.frameFlags[index] = isCaching ? FRAME_CACHING : 0;
1002
+ this.numFrames++;
1003
+ }
1004
+
1005
+ // visit: whether expr is rewritten and its result is on the result stack. Otherwise it opens a
1006
+ // frame for expr. pre_visit's test for a ground term comes before the cache's, which holds no
1007
+ // ground term.
1008
+ private visit(expr: number) {
1009
+ const manager = this.manager;
1010
+ if (manager.isGroundApp(expr)) {
1011
+ this.pushResult(expr);
1012
+ return true;
1013
+ }
1014
+ const isCaching = this.isCaching(expr);
1015
+ if (isCaching) {
1016
+ const cached = this.cache.find(expr, 0);
1017
+ if (cached !== undefined) {
1018
+ this.pushResult(cached);
1019
+ this.markNewChild(expr, cached);
1020
+ return true;
1021
+ }
1022
+ }
1023
+ if (manager.kinds[expr] === KIND_VAR) {
1024
+ this.processVar(expr);
1025
+ return true;
1026
+ }
1027
+ this.openFrame(expr, isCaching);
1028
+ return false;
1029
+ }
1030
+
1031
+ // process_var: a bound variable becomes its binding, shifted over the quantifiers entered since
1032
+ // it was bound.
1033
+ private processVar(variable: number) {
1034
+ const manager = this.manager;
1035
+ const index = manager.values[variable];
1036
+ const position = this.bindings.length - index - 1;
1037
+ const binding = index < this.bindings.length ? this.bindings[position] : -1;
1038
+ if (binding === -1) {
1039
+ this.pushResult(variable);
1040
+ return;
1041
+ }
1042
+ const amount = this.bindings.length - this.shifts[position];
1043
+ if (manager.isGroundApp(binding) || amount === 0) this.pushResult(binding);
1044
+ else {
1045
+ const cached = this.cache.find(binding, amount);
1046
+ if (cached !== undefined) this.pushResult(cached);
1047
+ else {
1048
+ this.isShifted = true;
1049
+ const shifted = this.shifter.shift(binding, 0, amount, 0);
1050
+ this.pushResult(shifted);
1051
+ this.cache.insert(binding, amount, shifted);
1052
+ manager.decRef(shifted);
1053
+ }
1054
+ }
1055
+ this.markNewChild(variable, -1);
1056
+ }
1057
+
1058
+ private processApp(expr: number, top: number) {
1059
+ const manager = this.manager;
1060
+ if (this.frameFlags[top] & FRAME_FOLDED) {
1061
+ this.finishFolded(expr, top);
1062
+ return;
1063
+ }
1064
+ const numArgs = manager.getNumArgs(expr);
1065
+ while (this.frameChildIndexes[top] < numArgs) {
1066
+ const childIndex = this.frameChildIndexes[top];
1067
+ if (childIndex === 1 && this.isConstantFolded(expr, top)) return;
1068
+ this.frameChildIndexes[top] = childIndex + 1;
1069
+ if (!this.visit(manager.getArg(expr, childIndex))) return;
1070
+ }
1071
+ const spos = this.frameStarts[top];
1072
+ this.setLastResult(
1073
+ this.frameFlags[top] & FRAME_NEW_CHILD
1074
+ ? manager.mkApp(manager.decls[expr], this.resultStack, manager.sorts[expr], spos)
1075
+ : expr,
1076
+ );
1077
+ const result = this.lastResult;
1078
+ this.shrinkResults(spos);
1079
+ this.pushResult(result);
1080
+ if (this.frameFlags[top] & FRAME_CACHING) this.cache.insert(expr, 0, result);
1081
+ this.numFrames--;
1082
+ this.markNewChild(expr, result);
1083
+ this.setLastResult(-1);
1084
+ }
1085
+
1086
+ // REWRITE_BUILTIN: the result of the folded branch replaces the ite on the result stack.
1087
+ private finishFolded(expr: number, top: number) {
1088
+ const result = this.resultStack[this.resultStack.length - 1];
1089
+ this.setLastResult(result);
1090
+ this.shrinkResults(this.resultStack.length - 1);
1091
+ this.shrinkResults(this.resultStack.length - 1);
1092
+ this.pushResult(result);
1093
+ if (this.frameFlags[top] & FRAME_CACHING) this.cache.insert(expr, 0, result);
1094
+ this.numFrames--;
1095
+ this.markNewChild(expr, -1);
1096
+ }
1097
+
1098
+ // constant_fold: an ite whose condition rewrote to true or false becomes the chosen branch.
1099
+ private isConstantFolded(expr: number, top: number) {
1100
+ const manager = this.manager;
1101
+ if (manager.getOp(expr) !== OP_ITE) return false;
1102
+ const spos = this.frameStarts[top];
1103
+ const condition = this.resultStack[spos];
1104
+ const arg = manager.isTrue(condition)
1105
+ ? manager.getArg(expr, 1)
1106
+ : manager.isFalse(condition)
1107
+ ? manager.getArg(expr, 2)
1108
+ : -1;
1109
+ if (arg === -1) return false;
1110
+ this.shrinkResults(spos);
1111
+ this.pushResult(arg);
1112
+ this.frameFlags[top] |= FRAME_FOLDED;
1113
+ if (this.visit(arg)) this.finishFolded(expr, top);
1114
+ this.setLastResult(-1);
1115
+ return true;
1116
+ }
1117
+
1118
+ private processQuantifier(expr: number, top: number) {
1119
+ const manager = this.manager;
1120
+ const data = manager.getQuantifier(expr);
1121
+ const numDecls = data.sorts.length;
1122
+ if (this.frameChildIndexes[top] === 0) {
1123
+ this.beginScope();
1124
+ this.root = data.body;
1125
+ const size = this.bindings.length;
1126
+ for (let index = 0; index < numDecls; index++) {
1127
+ this.bindings.push(-1);
1128
+ this.shifts.push(size);
1129
+ }
1130
+ this.numQvars += numDecls;
1131
+ }
1132
+ while (this.frameChildIndexes[top] <= data.patterns.length) {
1133
+ const childIndex = this.frameChildIndexes[top];
1134
+ this.frameChildIndexes[top] = childIndex + 1;
1135
+ if (!this.visit(childIndex === 0 ? data.body : data.patterns[childIndex - 1])) return;
1136
+ }
1137
+ const spos = this.frameStarts[top];
1138
+ const body = this.resultStack[spos];
1139
+ // new_pats: the patterns, which Z3 holds until the quantifier is done.
1140
+ const patterns = this.resultStack.slice(spos + 1);
1141
+ for (const pattern of patterns) manager.incRef(pattern);
1142
+ this.setLastResult(
1143
+ this.frameFlags[top] & FRAME_NEW_CHILD
1144
+ ? manager.mkQuantifier({ ...data, body, patterns })
1145
+ : expr,
1146
+ );
1147
+ const result = this.lastResult;
1148
+ this.shrinkResults(spos);
1149
+ this.pushResult(result);
1150
+ shrink(this.bindings, this.bindings.length - numDecls);
1151
+ shrink(this.shifts, this.shifts.length - numDecls);
1152
+ this.endScope();
1153
+ if (this.frameFlags[top] & FRAME_CACHING) this.cache.insert(expr, 0, result);
1154
+ this.setLastResult(-1);
1155
+ this.numFrames--;
1156
+ this.markNewChild(expr, result);
1157
+ for (const pattern of patterns) manager.decRef(pattern);
1158
+ }
1159
+ }