tla-ts 0.0.10-dev.37a75eb5b → 0.0.10-dev.45c0eaef5

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 (229) hide show
  1. package/dist/compile.cjs +3 -3
  2. package/dist/compile.js +1 -1
  3. package/dist/deciders.cjs +33 -33
  4. package/dist/deciders.js +53 -53
  5. package/dist/index.cjs +1 -1
  6. package/dist/index.d.cts +17 -3
  7. package/dist/index.d.ts +17 -3
  8. package/dist/index.js +1 -1
  9. package/dist/parallel-worker.cjs +1 -1
  10. package/dist/parallel-worker.js +1 -1
  11. package/dist/prove-worker.cjs +1 -1
  12. package/dist/prove-worker.js +1 -1
  13. package/dist/prove.cjs +55 -55
  14. package/dist/prove.js +78 -78
  15. package/dist/run.cjs +40 -42
  16. package/dist/run.js +42 -44
  17. package/dist/tlapm.cjs +1 -1
  18. package/dist/tlapm.js +2 -2
  19. package/dist/transfer.cjs +96 -91
  20. package/dist/transfer.js +95 -90
  21. package/dist/worker.cjs +7 -7
  22. package/dist/worker.js +7 -7
  23. package/package.json +1 -1
  24. package/src/ast.ts +31 -3
  25. package/src/bags.ts +12 -10
  26. package/src/builtins.ts +4 -3
  27. package/src/check-failure.ts +13 -11
  28. package/src/check-trace.ts +146 -93
  29. package/src/check.ts +418 -246
  30. package/src/cli/tlc-output.ts +17 -2
  31. package/src/cli/tlc.ts +5 -4
  32. package/src/cli/xml-exporter.ts +1 -1
  33. package/src/codegen.ts +23 -10
  34. package/src/coverage.ts +255 -18
  35. package/src/dependencies.ts +68 -15
  36. package/src/dfid.ts +93 -35
  37. package/src/environment.ts +106 -39
  38. package/src/errors.ts +7 -0
  39. package/src/evaluate.ts +415 -132
  40. package/src/exit-codes.ts +28 -15
  41. package/src/instances.ts +135 -11
  42. package/src/java-hash.ts +14 -0
  43. package/src/java-overrides.ts +2 -2
  44. package/src/levels.ts +674 -236
  45. package/src/lexer.ts +12 -2
  46. package/src/liveness-checker.ts +161 -60
  47. package/src/liveness.ts +55 -10
  48. package/src/obligations/index.ts +1 -1
  49. package/src/obligations/normalize.ts +1 -4
  50. package/src/obligations/printer.ts +5 -5
  51. package/src/obligations/source.ts +2 -0
  52. package/src/parser.ts +110 -111
  53. package/src/prover/isabelle/isar/attrib.ts +4 -4
  54. package/src/prover/isabelle/isar/rule-cases.ts +8 -6
  55. package/src/prover/isabelle/provers/clasimp.ts +1 -1
  56. package/src/prover/isabelle/provers/hypsubst.ts +2 -2
  57. package/src/prover/isabelle/pure/rewriter.ts +3 -3
  58. package/src/prover/isabelle/pure/term.ts +3 -3
  59. package/src/prover/prep/md5.ts +26 -29
  60. package/src/prover/smt/rewrite.ts +2 -7
  61. package/src/prover/smt/simplify.ts +1 -1
  62. package/src/prover/smt/solver/arith-rewriter.ts +11 -3
  63. package/src/prover/smt/solver/array-rewriter.ts +12 -2
  64. package/src/prover/smt/solver/asserted-formulas.ts +164 -58
  65. package/src/prover/smt/solver/ast-lt.ts +1 -1
  66. package/src/prover/smt/solver/ast.ts +65 -13
  67. package/src/prover/smt/solver/auf-solver.ts +23 -12
  68. package/src/prover/smt/solver/bit-blast-tactic.ts +27 -1
  69. package/src/prover/smt/solver/blast-term-ite.ts +47 -0
  70. package/src/prover/smt/solver/bound-simplifier.ts +11 -13
  71. package/src/prover/smt/solver/bv-decl-plugin.ts +13 -10
  72. package/src/prover/smt/solver/bv-rewriter.ts +1 -1
  73. package/src/prover/smt/solver/card2bv.ts +35 -18
  74. package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -24
  75. package/src/prover/smt/solver/context.ts +65 -14
  76. package/src/prover/smt/solver/ctx-simplify.ts +1 -2
  77. package/src/prover/smt/solver/datatype-decl-plugin.ts +15 -7
  78. package/src/prover/smt/solver/dependent-expr-state.ts +28 -16
  79. package/src/prover/smt/solver/diff-neq.ts +1 -2
  80. package/src/prover/smt/solver/dt2bv-tactic.ts +57 -21
  81. package/src/prover/smt/solver/elim-bounds.ts +89 -0
  82. package/src/prover/smt/solver/elim-term-ite-tactic.ts +48 -0
  83. package/src/prover/smt/solver/elim-uncnstr.ts +5 -8
  84. package/src/prover/smt/solver/euf/arith-internalize.ts +1 -1
  85. package/src/prover/smt/solver/euf/array-internalize.ts +1 -1
  86. package/src/prover/smt/solver/euf/bv-internalize.ts +1 -1
  87. package/src/prover/smt/solver/euf/egraph.ts +3 -9
  88. package/src/prover/smt/solver/euf/euf-solver.ts +1 -2
  89. package/src/prover/smt/solver/euf/mam.ts +1 -5
  90. package/src/prover/smt/solver/euf/q-ematch.ts +1 -2
  91. package/src/prover/smt/solver/euf/th-solver.ts +0 -7
  92. package/src/prover/smt/solver/fd-solver.ts +608 -141
  93. package/src/prover/smt/solver/fix-dl-var.ts +1 -2
  94. package/src/prover/smt/solver/fpa2bv-rewriter.ts +1 -0
  95. package/src/prover/smt/solver/goal.ts +4 -3
  96. package/src/prover/smt/solver/hashtable.ts +4 -0
  97. package/src/prover/smt/solver/internalizer.ts +1 -1
  98. package/src/prover/smt/solver/logic-tactics.ts +15 -5
  99. package/src/prover/smt/solver/lp/core-solver.ts +12 -8
  100. package/src/prover/smt/solver/lp/lar-solver.ts +1 -1
  101. package/src/prover/smt/solver/lp/lp-settings.ts +6 -2
  102. package/src/prover/smt/solver/lp/nla-grobner.ts +2 -2
  103. package/src/prover/smt/solver/lp/pdd.ts +2 -3
  104. package/src/prover/smt/solver/lp/union-find.ts +1 -1
  105. package/src/prover/smt/solver/mam-compiler.ts +1 -1
  106. package/src/prover/smt/solver/maxcore.ts +2 -1
  107. package/src/prover/smt/solver/maxsmt-base.ts +1 -0
  108. package/src/prover/smt/solver/model-based-opt.ts +1 -1
  109. package/src/prover/smt/solver/model-checker.ts +1 -1
  110. package/src/prover/smt/solver/model-evaluator.ts +2 -0
  111. package/src/prover/smt/solver/model-generator.ts +85 -43
  112. package/src/prover/smt/solver/mpf.ts +2 -4
  113. package/src/prover/smt/solver/mus.ts +2 -3
  114. package/src/prover/smt/solver/muz/dl-context.ts +14 -42
  115. package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +1 -1
  116. package/src/prover/smt/solver/muz/dl-mk-slice.ts +3 -3
  117. package/src/prover/smt/solver/muz/dl-rule.ts +8 -9
  118. package/src/prover/smt/solver/muz/hnf.ts +2 -2
  119. package/src/prover/smt/solver/muz/horn-subsume-model-converter.ts +1 -1
  120. package/src/prover/smt/solver/muz/horn-tactic.ts +15 -2
  121. package/src/prover/smt/solver/muz/rule-properties.ts +22 -0
  122. package/src/prover/smt/solver/muz/spacer-context.ts +1 -4
  123. package/src/prover/smt/solver/muz/spacer-prop-solver.ts +1 -1
  124. package/src/prover/smt/solver/nlqsat.ts +1 -1
  125. package/src/prover/smt/solver/nlsat/algebraic.ts +4 -5
  126. package/src/prover/smt/solver/nlsat/levelwise.ts +0 -4
  127. package/src/prover/smt/solver/nlsat/polynomial.ts +1 -36
  128. package/src/prover/smt/solver/nlsat/upolynomial.ts +1 -1
  129. package/src/prover/smt/solver/nlsat-tactic.ts +1 -1
  130. package/src/prover/smt/solver/nnf.ts +6 -2
  131. package/src/prover/smt/solver/opt-context.ts +16 -7
  132. package/src/prover/smt/solver/opt-pareto.ts +19 -0
  133. package/src/prover/smt/solver/opt-preprocess.ts +1 -1
  134. package/src/prover/smt/solver/opt-solver.ts +4 -3
  135. package/src/prover/smt/solver/optsmt.ts +1 -1
  136. package/src/prover/smt/solver/parallel-tactic.ts +1 -1
  137. package/src/prover/smt/solver/pb-rewriter.ts +1 -4
  138. package/src/prover/smt/solver/poly-rewriter.ts +75 -18
  139. package/src/prover/smt/solver/probes.ts +88 -23
  140. package/src/prover/smt/solver/proto-model.ts +8 -8
  141. package/src/prover/smt/solver/push-app-ite.ts +37 -0
  142. package/src/prover/smt/solver/qe-arith-plugin.ts +2 -4
  143. package/src/prover/smt/solver/qe-lite.ts +2 -2
  144. package/src/prover/smt/solver/qe.ts +3 -2
  145. package/src/prover/smt/solver/qsat.ts +23 -8
  146. package/src/prover/smt/solver/recfun-decl-plugin.ts +2 -1
  147. package/src/prover/smt/solver/rewriter-tpl.ts +7 -6
  148. package/src/prover/smt/solver/rewriter.ts +31 -25
  149. package/src/prover/smt/solver/rlimit.ts +7 -3
  150. package/src/prover/smt/solver/sat/config.ts +1 -1
  151. package/src/prover/smt/solver/sat/goal2sat.ts +65 -12
  152. package/src/prover/smt/solver/sat/inc-sat-solver.ts +129 -43
  153. package/src/prover/smt/solver/sat/model-converter.ts +1 -1
  154. package/src/prover/smt/solver/sat/pb-constraint.ts +7 -0
  155. package/src/prover/smt/solver/sat/pb-solver.ts +26 -7
  156. package/src/prover/smt/solver/sat/sat-tactic.ts +15 -6
  157. package/src/prover/smt/solver/sat/sat2goal.ts +72 -48
  158. package/src/prover/smt/solver/sat/simplifier.ts +1 -1
  159. package/src/prover/smt/solver/sat/solver.ts +27 -47
  160. package/src/prover/smt/solver/seq-axioms.ts +20 -35
  161. package/src/prover/smt/solver/seq-decl-plugin.ts +1 -1
  162. package/src/prover/smt/solver/seq-derive.ts +1 -1
  163. package/src/prover/smt/solver/seq-eq-solver.ts +2 -2
  164. package/src/prover/smt/solver/seq-monadic.ts +7 -5
  165. package/src/prover/smt/solver/seq-re-rewriter.ts +2 -1
  166. package/src/prover/smt/solver/seq-re-util.ts +1 -1
  167. package/src/prover/smt/solver/seq-regex-bisim.ts +1 -1
  168. package/src/prover/smt/solver/seq-regex-live.ts +2 -2
  169. package/src/prover/smt/solver/seq-rewriter.ts +31 -29
  170. package/src/prover/smt/solver/seq-skolem.ts +0 -23
  171. package/src/prover/smt/solver/seq-view-witness.ts +4 -4
  172. package/src/prover/smt/solver/smt-consequences.ts +1 -1
  173. package/src/prover/smt/solver/smt-kernel.ts +40 -6
  174. package/src/prover/smt/solver/smt-setup.ts +106 -13
  175. package/src/prover/smt/solver/smtlib-parser.ts +579 -214
  176. package/src/prover/smt/solver/smtlib-script.ts +600 -170
  177. package/src/prover/smt/solver/solve-eqs.ts +36 -37
  178. package/src/prover/smt/solver/solver.ts +18 -3
  179. package/src/prover/smt/solver/tactic.ts +342 -196
  180. package/src/prover/smt/solver/theory-bv.ts +13 -3
  181. package/src/prover/smt/solver/theory-fpa.ts +1 -1
  182. package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
  183. package/src/prover/smt/solver/theory-lra-internalize.ts +2 -0
  184. package/src/prover/smt/solver/theory-lra-propagate.ts +11 -3
  185. package/src/prover/smt/solver/theory-lra-state.ts +1 -1
  186. package/src/prover/smt/solver/theory-lra.ts +1 -1
  187. package/src/prover/smt/solver/theory-pb.ts +24 -7
  188. package/src/prover/smt/solver/theory-seq-solve.ts +1 -1
  189. package/src/prover/smt/solver/theory-seq-state.ts +2 -21
  190. package/src/prover/smt/solver/theory-without-terms.ts +24 -0
  191. package/src/prover/smt/solver/tseitin-cnf.ts +1 -0
  192. package/src/prover/tlapm/cache.ts +8 -8
  193. package/src/prover/zenon/index-table.ts +5 -2
  194. package/src/prover/zenon/isar-case.ts +1 -1
  195. package/src/prover/zenon/isar.ts +2 -2
  196. package/src/prover/zenon/phrase.ts +1 -1
  197. package/src/resolve.ts +1226 -246
  198. package/src/runtime.ts +19 -2
  199. package/src/sany/builtins.ts +10 -10
  200. package/src/sany/generation.ts +4 -2
  201. package/src/sany/generator-def-entries.ts +123 -36
  202. package/src/sany/generator-definitions.ts +4 -2
  203. package/src/sany/generator-expressions.ts +51 -20
  204. package/src/sany/generator-proofs.ts +17 -11
  205. package/src/sany/generator.ts +11 -5
  206. package/src/sany/lexer.ts +15 -7
  207. package/src/sany/modules.ts +4 -2
  208. package/src/sany/nodes.ts +1 -1
  209. package/src/sany/selectors.ts +11 -6
  210. package/src/sany/xml.ts +2 -3
  211. package/src/search.ts +283 -126
  212. package/src/simulate.ts +247 -185
  213. package/src/spec-description.ts +15 -7
  214. package/src/standard-definitions.ts +1 -0
  215. package/src/standard-modules.ts +56 -22
  216. package/src/subexpressions.ts +563 -139
  217. package/src/successors.ts +50 -20
  218. package/src/symmetry.ts +2 -1
  219. package/src/tlc/jar-data.ts +1 -3
  220. package/src/tlc/messages.ts +3 -3
  221. package/src/tlc/random.ts +2 -1
  222. package/src/tlc/replay.ts +147 -46
  223. package/src/tlc/tlc-operators.ts +10 -8
  224. package/src/tlc/tlcext-operators.ts +26 -10
  225. package/src/tlc/trace-spec.ts +9 -22
  226. package/src/trace-liveness.ts +529 -0
  227. package/src/typed-tier.ts +52 -31
  228. package/src/typescript/types.ts +2 -2
  229. package/src/values.ts +150 -79
@@ -1,24 +1,16 @@
1
1
  import {
2
2
  SmtCanceledError,
3
3
  SmtParseError,
4
+ SmtDefaultError,
4
5
  SmtTacticError,
5
6
  SmtUnsupportedError,
6
7
  } from "../../../errors";
7
- import {
8
- type AstManager,
9
- KIND_APP,
10
- OP_DIV0,
11
- OP_IDIV0,
12
- OP_ITE,
13
- OP_MOD0,
14
- OP_POWER0,
15
- SORT_BOOL,
16
- } from "./ast";
8
+ import { type AstManager, OP_DIV0, OP_IDIV0, OP_MOD0, OP_OR, OP_POWER0, SORT_BOOL } from "./ast";
17
9
  import { createQfufbvAckrTactic, runAckermannizeBv } from "./ackermannize-bv-tactic";
18
- import { AssertedFormulas } from "./asserted-formulas";
19
10
  import { createAigTactic } from "./aig-tactic";
11
+ import { blastTermIte } from "./blast-term-ite";
20
12
  import { card2bv } from "./card2bv";
21
- import { Context, MBQI_MAX_ITERATIONS } from "./context";
13
+ import { type Context, MBQI_MAX_ITERATIONS } from "./context";
22
14
  import { ctxSimplify } from "./ctx-simplify";
23
15
  import { createCtxSolverSimplify } from "./ctx-solver-simplify";
24
16
  import {
@@ -32,6 +24,7 @@ import {
32
24
  import type { AckrSolver } from "./lackr";
33
25
  import { L_FALSE, L_TRUE, L_UNDEF } from "./lbool";
34
26
  import { DerRewriter } from "./der";
27
+ import { finishSimplifier, runSimplifier } from "./dependent-expr-state";
35
28
  import { fixDlVar } from "./fix-dl-var";
36
29
  import { runFpa2Bv } from "./fpa2bv-tactic";
37
30
  import { type BitBlasterRewriter, runBitBlast } from "./bit-blast-tactic";
@@ -44,6 +37,7 @@ import { createSatTactic } from "./sat/sat-tactic";
44
37
  import { mkThSolver } from "./euf/th-solvers";
45
38
  import { createFdTactic } from "./fd-solver";
46
39
  import { elimUncnstr } from "./elim-uncnstr";
40
+ import { runElimTermIte } from "./elim-term-ite-tactic";
47
41
  import { rewriteUfbv } from "./demodulator";
48
42
  import { distributeForall } from "./distribute-forall";
49
43
  import { createDt2BvTactic } from "./dt2bv-tactic";
@@ -72,6 +66,7 @@ import { propagateValues } from "./propagate-values";
72
66
  import { purifyArith } from "./purify-arith";
73
67
  import { ModelEvaluator } from "./model-evaluator";
74
68
  import { FuncInterp, ProtoModel } from "./proto-model";
69
+ import { SmtKernel } from "./smt-kernel";
75
70
  import { isMbqiByFeatures, type SmtSetup } from "./smt-setup";
76
71
  import { compileProbe, formatProbeValue, isQfufbv, type Probe } from "./probes";
77
72
  import { runHorn } from "./muz/horn-tactic";
@@ -87,6 +82,7 @@ import { LOGIC_TACTICS, QFAUFBV, QFUFBV_ACKR_PREAMBLE } from "./logic-tactics";
87
82
  import {
88
83
  createCommandReader,
89
84
  isSymbol,
85
+ isUnsigned,
90
86
  type SExpr,
91
87
  type SmtList,
92
88
  type SmtToken,
@@ -145,9 +141,9 @@ interface NamedTactic {
145
141
 
146
142
  const getDescrs = (module: string, entries: [string, number, TacticParam, boolean?][]) =>
147
143
  new Map(
148
- entries.map(([name, kind, defaultValue, isPorted]) => [
149
- name,
150
- { kind, module, defaultValue, isPorted: isPorted ?? false },
144
+ entries.map((entry) => [
145
+ entry[0],
146
+ { kind: entry[1], module, defaultValue: entry[2], isPorted: entry[3] ?? false },
151
147
  ]),
152
148
  );
153
149
 
@@ -176,7 +172,7 @@ const REWRITER_DESCRS = getDescrs("rewriter", [
176
172
  ["sort_sums", PARAM_BOOL, false, true],
177
173
  ["gcd_rounding", PARAM_BOOL, false, true],
178
174
  ["arith_lhs", PARAM_BOOL, false, true],
179
- ["arith_ineq_lhs", PARAM_BOOL, false],
175
+ ["arith_ineq_lhs", PARAM_BOOL, false, true],
180
176
  ["elim_to_real", PARAM_BOOL, false],
181
177
  ["push_to_real", PARAM_BOOL, true],
182
178
  ["eq2ineq", PARAM_BOOL, false],
@@ -255,7 +251,8 @@ export const getRewriterOptions = (manager: AstManager, params: TacticParams): R
255
251
  arith: {
256
252
  isSom: getBool(params, "flat", true) && getBool(params, "som", false),
257
253
  isSortSums: getBool(params, "sort_sums", false),
258
- isArithIneqLhs: getBool(params, "arith_lhs", false),
254
+ isArithLhs: getBool(params, "arith_lhs", false),
255
+ isArithIneqLhs: getBool(params, "arith_ineq_lhs", false),
259
256
  isGcdRounding: getBool(params, "gcd_rounding", false),
260
257
  isAnumSimp: getBool(params, "algebraic_number_evaluator", true),
261
258
  isExpandPower: getBool(params, "expand_power", false),
@@ -280,6 +277,8 @@ const SMT_DESCRS = getDescrs("smt", [
280
277
  ["mbqi", PARAM_BOOL, true, true],
281
278
  ["mbqi.max_iterations", PARAM_UINT, MBQI_MAX_ITERATIONS, true],
282
279
  ["arith.nl.rounds", PARAM_UINT, 1024, true],
280
+ ["random_seed", PARAM_UINT, 0, true],
281
+ ["auto_config", PARAM_BOOL, true, true],
283
282
  ]);
284
283
 
285
284
  // The dependencies are assertion names, which are Boolean constants, so each one is its own
@@ -287,13 +286,26 @@ const SMT_DESCRS = getDescrs("smt", [
287
286
  const getKernelParams = (environment: TacticEnvironment, params: TacticParams, goal: Goal) => {
288
287
  const manager = environment.manager;
289
288
  const hasQuantifiers = goal.forms.items.some((form) => manager.hasQuantifierFlags[form]);
289
+ // smt_params ands the auto_config of the tactic with the global one, which smt does not scope.
290
+ const setup = {
291
+ ...environment.setup,
292
+ isAutoConfig:
293
+ getBool(
294
+ params,
295
+ "auto_config",
296
+ getParameter(environment, "smt.auto_config", "true") !== "false",
297
+ ) && getParameter(environment, "auto_config", "true") !== "false",
298
+ };
290
299
  return {
291
- isMbqi:
292
- getBool(params, "mbqi", environment.isMbqi) ||
293
- isMbqiByFeatures(environment.setup, hasQuantifiers),
300
+ isMbqi: getBool(params, "mbqi", environment.isMbqi) || isMbqiByFeatures(setup, hasQuantifiers),
294
301
  mbqiMaxIterations: getUint(params, "mbqi.max_iterations", environment.mbqiMaxIterations),
295
302
  isFindingMacros: environment.isFindingMacros,
296
- setup: environment.setup,
303
+ setup,
304
+ randomSeed: getUint(
305
+ params,
306
+ "random_seed",
307
+ Number(getParameter(environment, "smt.random_seed", "0")),
308
+ ),
297
309
  };
298
310
  };
299
311
 
@@ -360,11 +372,15 @@ const runSmt = (environment: TacticEnvironment, params: TacticParams) => (goal:
360
372
  ? solveGoal(manager, goal.forms.items, kernelParams)
361
373
  : solveAssuming(manager, extracted.clauses.items, extracted.assumptions, kernelParams);
362
374
  extracted.clauses.reset();
363
- if (solved.result === L_UNDEF) throw new SmtTacticError(getSmtTacticFailure(solved.context));
375
+ if (solved.result === L_UNDEF)
376
+ throw new SmtTacticError(solved.reasonUnknown ?? getSmtTacticFailure(solved.context));
364
377
  goal.reset();
365
- if (solved.result === L_FALSE)
378
+ if (solved.result === L_FALSE) {
366
379
  goal.assert(environment.manager.falseExpr, mkCoreDependency(solved.context.unsatCore));
367
- else goal.model = solved.context.getModel();
380
+ return [goal];
381
+ }
382
+ if (goal.isModelsEnabled) goal.model = solved.context.getModel();
383
+ if (solved.context.checkCanceled()) throw new SmtTacticError("canceled");
368
384
  return [goal];
369
385
  };
370
386
 
@@ -428,30 +444,42 @@ const createTacticSolver = (manager: AstManager, getTactic: () => Tactic): AckrS
428
444
  // Each clause gets a fresh context, where Z3 pushes a scope on one context.
429
445
  const runUnitSubsume = (environment: TacticEnvironment) => (goal: Goal) => {
430
446
  const manager = environment.manager;
447
+ const kernel = new SmtKernel(manager, {
448
+ isMbqi: environment.isMbqi,
449
+ mbqiMaxIterations: environment.mbqiMaxIterations,
450
+ isFindingMacros: false,
451
+ });
452
+ const trail = new RefVector(manager);
431
453
  const clauses = goal.forms.items.map(() => manager.mkFreshConst("#clause", SORT_BOOL));
432
- const definitions = goal.forms.items.map((form, index) => manager.mkEq(clauses[index], form));
433
- for (const expr of [...clauses, ...definitions]) manager.incRef(expr);
454
+ for (const clause of clauses) trail.push(clause);
455
+ kernel.push();
456
+ for (const [index, form] of goal.forms.items.entries()) {
457
+ const definition = manager.mkEq(clauses[index], form);
458
+ trail.push(definition);
459
+ kernel.assertExpr(definition);
460
+ }
461
+ kernel.push();
462
+ const isDeleted = clauses.map(() => false);
434
463
  const deleted: number[] = [];
435
464
  for (const [index, clause] of clauses.entries()) {
436
- const asserted = new AssertedFormulas(manager);
437
- const context = new Context(manager, {
438
- isMbqi: environment.isMbqi,
439
- mbqiMaxIterations: environment.mbqiMaxIterations,
440
- relevancyLevel: 2,
441
- });
442
- for (const definition of definitions) asserted.assertExpr(definition);
465
+ kernel.push();
443
466
  for (const [other, otherClause] of clauses.entries())
444
- if (other === index) asserted.assertExpr(manager.mkNot(clause));
445
- else if (!deleted.includes(other)) asserted.assertExpr(otherClause);
446
- // The context has no logic, so setup_default picks setup_unknown.
447
- context.setupContext(undefined, false, asserted);
448
- context.internalizeAssertions(asserted);
449
- if (!context.isInconsistent()) context.propagate();
450
- if (context.isInconsistent()) deleted.push(index);
467
+ if (other === index) {
468
+ const negated = manager.mkNot(clause);
469
+ trail.push(negated);
470
+ kernel.assertExpr(negated);
471
+ } else if (!isDeleted[other]) kernel.assertExpr(otherClause);
472
+ kernel.push();
473
+ const isUnsat = kernel.context.isInconsistent();
474
+ kernel.pop(2);
475
+ if (!isUnsat) continue;
476
+ isDeleted[index] = true;
477
+ deleted.push(index);
451
478
  }
452
479
  for (const index of deleted) goal.update(index, manager.trueExpr);
453
480
  goal.elimTrue();
454
- for (const expr of [...clauses, ...definitions]) manager.decRef(expr);
481
+ kernel.pop(2);
482
+ trail.reset();
455
483
  return [goal];
456
484
  };
457
485
 
@@ -460,19 +488,50 @@ const inPlace = (run: (goal: Goal) => void) => (goal: Goal) => {
460
488
  return [goal];
461
489
  };
462
490
 
463
- const runDer = (manager: AstManager) => (goal: Goal) => {
464
- const rewriter = new DerRewriter(manager);
465
- const size = goal.forms.size;
466
- for (let index = 0; !goal.isInconsistent && index < size; index++) {
467
- const result = rewriter.rewrite(goal.forms.items[index]);
468
- goal.update(index, result);
469
- manager.decRef(result);
491
+ const selectClause = (goal: Goal, isLargest: boolean) => {
492
+ const manager = goal.manager;
493
+ let position = -1;
494
+ let length = 0;
495
+ for (let index = 0; index < goal.forms.size; index++) {
496
+ const form = goal.forms.items[index];
497
+ if (manager.getOp(form) !== OP_OR) continue;
498
+ const currentLength = manager.getNumArgs(form);
499
+ if (currentLength < 2) continue;
500
+ if (!isLargest) return index;
501
+ if (currentLength > length) {
502
+ position = index;
503
+ length = currentLength;
504
+ }
470
505
  }
471
- goal.elimTrue();
472
- goal.elimRedundancies();
473
- goal.depth++;
474
- rewriter.der.release();
475
- rewriter.release();
506
+ return position;
507
+ };
508
+
509
+ const runSplitClause = (isLargest: boolean) => (goal: Goal) => {
510
+ const manager = goal.manager;
511
+ const position = selectClause(goal, isLargest);
512
+ if (position === -1)
513
+ throw new SmtTacticError("split-clause tactic failed, goal does not contain any clause");
514
+ if (goal.isProofsEnabled) throw new SmtUnsupportedError("the proofs of split-clause");
515
+ const dependency = goal.dependencies[position];
516
+ return manager.getArgs(goal.forms.items[position]).map((literal) => {
517
+ const subgoal = goal.copy();
518
+ subgoal.update(position, literal, dependency);
519
+ subgoal.depth++;
520
+ return subgoal;
521
+ });
522
+ };
523
+
524
+ const runDer = (manager: AstManager) => (goal: Goal) => {
525
+ runSimplifier(goal, true, (state) => {
526
+ const rewriter = new DerRewriter(manager);
527
+ return {
528
+ reduce: () => state.rewriteEach((form) => rewriter.rewrite(form)),
529
+ release: () => {
530
+ rewriter.der.release();
531
+ rewriter.release();
532
+ },
533
+ };
534
+ });
476
535
  return [goal];
477
536
  };
478
537
 
@@ -512,10 +571,16 @@ const runNnf = (manager: AstManager, params: TacticParams) => (goal: Goal) => {
512
571
  const nnf = new Nnf(manager, names, mode, getBool(params, "ignore_labels", false));
513
572
  const definitions = new RefVector(manager);
514
573
  const size = goal.forms.size;
515
- for (let index = 0; !goal.isInconsistent && index < size; index++) {
516
- const result = nnf.apply(goal.forms.items[index], definitions);
517
- goal.update(index, result);
518
- manager.decRef(result);
574
+ try {
575
+ for (let index = 0; !goal.isInconsistent && index < size; index++) {
576
+ const result = nnf.apply(goal.forms.items[index], definitions);
577
+ goal.update(index, result);
578
+ manager.decRef(result);
579
+ }
580
+ } catch (error) {
581
+ // Z3's nnf stops at the limit with a default_exception, which or-else lets pass.
582
+ if (!(error instanceof SmtCanceledError)) throw error;
583
+ throw new SmtDefaultError(manager.limit.getCancelMessage());
519
584
  }
520
585
  for (const definition of definitions.items) {
521
586
  if (goal.isInconsistent) break;
@@ -696,40 +761,30 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
696
761
  ]),
697
762
  build: (environment, params) => {
698
763
  const manager = environment.manager;
699
- const snf = runNnf(manager, new Map());
700
764
  const elimRemParams = new Map([...params, ["elim_rem", true]]);
701
- return (goal) => {
702
- snf(goal);
703
- simplifyGoal(manager, goal, getRewriterOptions(manager, elimRemParams));
704
- purifyArith(manager, goal, {
705
- isComplete: getBool(params, "complete", true),
706
- isElimRootObjects: getBool(params, "elim_root_objects", true),
707
- isElimInverses: getBool(params, "elim_inverses", true),
708
- });
709
- simplifyGoal(manager, goal, getRewriterOptions(manager, params));
710
- return [goal];
711
- };
765
+ return [
766
+ runNnf(manager, new Map()),
767
+ inPlace((goal) =>
768
+ simplifyGoal(manager, goal, getRewriterOptions(manager, elimRemParams)),
769
+ ),
770
+ inPlace((goal) =>
771
+ purifyArith(manager, goal, {
772
+ isComplete: getBool(params, "complete", true),
773
+ isElimRootObjects: getBool(params, "elim_root_objects", true),
774
+ isElimInverses: getBool(params, "elim_inverses", true),
775
+ }),
776
+ ),
777
+ inPlace((goal) => simplifyGoal(manager, goal, getRewriterOptions(manager, params))),
778
+ ].reduceRight((second, first) => runAndThen(first, second));
712
779
  },
713
780
  },
714
781
  ],
715
782
  [
716
783
  "elim-term-ite",
717
784
  {
785
+ isTrackingDependencies: true,
718
786
  descrs: getDescrs("", [["max_memory", PARAM_UINT, UINT_MAX, true]]),
719
- build: (environment) =>
720
- inPlace((goal) => {
721
- const manager = environment.manager;
722
- const seen = new Set<number>();
723
- const todo = [...goal.forms.items];
724
- for (let expr = todo.pop(); expr !== undefined; expr = todo.pop()) {
725
- if (seen.has(expr) || manager.kinds[expr] !== KIND_APP) continue;
726
- seen.add(expr);
727
- if (manager.getOp(expr) === OP_ITE && manager.sorts[expr] !== SORT_BOOL)
728
- throw new SmtUnsupportedError("elim-term-ite on a goal it changes");
729
- manager.pushArgs(expr, todo);
730
- }
731
- goal.depth++;
732
- }),
787
+ build: (environment) => runElimTermIte(environment.manager),
733
788
  },
734
789
  ],
735
790
  [
@@ -757,7 +812,7 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
757
812
  isEliminatingAnd: true,
758
813
  isKeepingDistinct: false,
759
814
  });
760
- tseitinCnf(manager, goal);
815
+ if (!goal.isDecided()) tseitinCnf(manager, goal);
761
816
  } finally {
762
817
  original.reset();
763
818
  }
@@ -769,14 +824,9 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
769
824
  {
770
825
  descrs: new Map(),
771
826
  build: (environment) =>
772
- inPlace((goal) => {
773
- if (!goal.isProofsEnabled) applyQeLite(environment.manager, goal);
774
- else {
775
- goal.elimTrue();
776
- goal.elimRedundancies();
777
- }
778
- goal.depth++;
779
- }),
827
+ inPlace((goal) =>
828
+ goal.isProofsEnabled ? finishSimplifier(goal) : applyQeLite(environment.manager, goal),
829
+ ),
780
830
  },
781
831
  ],
782
832
  [
@@ -808,6 +858,7 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
808
858
  [
809
859
  "elim-uncnstr",
810
860
  {
861
+ isTrackingDependencies: true,
811
862
  descrs: getDescrs("", [
812
863
  ["max_memory", PARAM_UINT, UINT_MAX, true],
813
864
  ["max_steps", PARAM_UINT, UINT_MAX],
@@ -903,7 +954,7 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
903
954
  "pb2bv",
904
955
  {
905
956
  descrs: new Map([
906
- ...[...REWRITER_DESCRS].filter(([name]) => BOOL_REWRITER_PARAMS.includes(name)),
957
+ ...[...REWRITER_DESCRS].filter((entry) => BOOL_REWRITER_PARAMS.includes(entry[0])),
907
958
  ...getDescrs("", [
908
959
  ["max_memory", PARAM_UINT, UINT_MAX, true],
909
960
  ["pb2bv_all_clauses_limit", PARAM_UINT, 8, true],
@@ -1048,6 +1099,14 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
1048
1099
  },
1049
1100
  ],
1050
1101
  ["der", { descrs: new Map(), build: (environment) => runDer(environment.manager) }],
1102
+ [
1103
+ "blast-term-ite",
1104
+ {
1105
+ isTrackingDependencies: true,
1106
+ descrs: new Map(),
1107
+ build: (environment) => inPlace((goal) => blastTermIte(environment.manager, goal)),
1108
+ },
1109
+ ],
1051
1110
  [
1052
1111
  "distribute-forall",
1053
1112
  {
@@ -1109,6 +1168,15 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
1109
1168
  build: (environment) => inPlace((goal) => domSimplify(environment.manager, goal)),
1110
1169
  },
1111
1170
  ],
1171
+ [
1172
+ "split-clause",
1173
+ {
1174
+ descrs: getDescrs("", [["split_largest_clause", PARAM_BOOL, false, true]]),
1175
+ isTrackingDependencies: true,
1176
+ build: (_environment, params) =>
1177
+ runSplitClause(getBool(params, "split_largest_clause", false)),
1178
+ },
1179
+ ],
1112
1180
  [
1113
1181
  "occf",
1114
1182
  {
@@ -1213,7 +1281,7 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
1213
1281
  const manager = environment.manager;
1214
1282
  const preamble = createCommandReader(QFUFBV_ACKR_PREAMBLE).read() as SmtList;
1215
1283
  return runAndThen(
1216
- compile(environment, preamble, preamble.end, true).build(params),
1284
+ compile(environment, preamble, true).build(params),
1217
1285
  runCond(
1218
1286
  isQfufbv,
1219
1287
  createQfufbvAckrTactic(manager, getRewriterOptions(manager, new Map()), true, () =>
@@ -1294,7 +1362,7 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
1294
1362
  {
1295
1363
  descrs: new Map(),
1296
1364
  isRejectingUnknown: false,
1297
- build: (environment) => runQsat(environment.manager, QSAT_SAT),
1365
+ build: (environment) => runQsat(environment, QSAT_SAT),
1298
1366
  },
1299
1367
  ],
1300
1368
  [
@@ -1302,7 +1370,7 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
1302
1370
  {
1303
1371
  descrs: new Map(),
1304
1372
  isRejectingUnknown: false,
1305
- build: (environment) => runQsat(environment.manager, QSAT_QE),
1373
+ build: (environment) => runQsat(environment, QSAT_QE),
1306
1374
  },
1307
1375
  ],
1308
1376
  [
@@ -1310,7 +1378,7 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
1310
1378
  {
1311
1379
  descrs: new Map(),
1312
1380
  isRejectingUnknown: false,
1313
- build: (environment) => runQsat(environment.manager, QSAT_QE_REC),
1381
+ build: (environment) => runQsat(environment, QSAT_QE_REC),
1314
1382
  },
1315
1383
  ],
1316
1384
  [
@@ -1386,20 +1454,19 @@ const NAMED_TACTICS = new Map<string, NamedTactic>([
1386
1454
 
1387
1455
  const UNPORTED_TACTICS = new Set(
1388
1456
  (
1389
- "blast-term-ite " +
1390
1457
  "bv-slice bvarray2uf collect-statistics " +
1391
1458
  "degree-shift demodulator " +
1392
1459
  "elim-predicates elim-uncnstr2 " +
1393
1460
  "euf-completion ff-sat ff-simplify ff-solve ff-unique ff2bv fold-unfold fpa2bv " +
1394
1461
  "horn-simplify injectivity pb-preprocess pqffd propagate-bv-bounds " +
1395
- "propagate-bv-bounds2 propagate-values2 qfbv-sls qfufbv recover-01 reduce-args2 " +
1462
+ "propagate-bv-bounds2 propagate-values2 qfbv-sls recover-01 reduce-args2 " +
1396
1463
  "sat-preprocess sls-smt smtfd solver-subsumption special-relations " +
1397
- "split-clause subpaving symmetry-reduce"
1464
+ "subpaving symmetry-reduce"
1398
1465
  ).split(" "),
1399
1466
  );
1400
1467
 
1401
- const isUnsigned = (sexpr: SExpr | undefined): sexpr is SmtToken =>
1402
- sexpr?.kind === "numeral" && BigInt(sexpr.text) <= BigInt(UINT_MAX);
1468
+ const isUnsignedNumeral = (sexpr: SExpr | undefined): sexpr is SmtToken =>
1469
+ sexpr?.kind === "numeral" && isUnsigned(sexpr.text);
1403
1470
 
1404
1471
  const getParamName = (keyword: string) =>
1405
1472
  keyword.replace(/^:/, "").toLowerCase().replaceAll("-", "_");
@@ -1420,7 +1487,7 @@ const parseParamValue = (kind: number, value: SExpr) =>
1420
1487
  kind === PARAM_BOOL
1421
1488
  ? isSymbol(value, "true") || (isSymbol(value, "false") ? false : undefined)
1422
1489
  : kind === PARAM_UINT
1423
- ? isUnsigned(value)
1490
+ ? isUnsignedNumeral(value)
1424
1491
  ? Number(value.text)
1425
1492
  : undefined
1426
1493
  : kind === PARAM_NUMERAL
@@ -1449,27 +1516,33 @@ const checkParams = (descrs: ParamDescrs, params: TacticParams) => {
1449
1516
  }
1450
1517
  };
1451
1518
 
1519
+ // sexpr2tactic reports an error at the s-expression: a list at its closing parenthesis.
1520
+ const getSexprPosition = (sexpr: SExpr) => (sexpr.kind === "list" ? sexpr.end : sexpr);
1521
+
1452
1522
  const getParams = (
1453
1523
  builder: TacticBuilder,
1454
1524
  items: SExpr[],
1455
- end: SmtToken,
1456
1525
  isInternal: boolean,
1457
1526
  ): Map<string, TacticParam> => {
1458
1527
  const params = new Map<string, TacticParam>();
1459
1528
  for (let index = 2; index < items.length; index += 2) {
1460
1529
  const keyword = items[index];
1461
1530
  if (keyword.kind !== "keyword")
1462
- throw new SmtParseError("invalid using-params combinator, keyword expected", end);
1531
+ throw new SmtParseError(
1532
+ "invalid using-params combinator, keyword expected",
1533
+ getSexprPosition(keyword),
1534
+ );
1463
1535
  const value = items[index + 1];
1464
1536
  if (value === undefined)
1465
- throw new SmtParseError("invalid using-params combinator, parameter value expected", end);
1537
+ throw new SmtParseError("invalid using-params combinator, parameter value expected", keyword);
1466
1538
  const found = findParam(builder.descrs, getParamName(keyword.text));
1467
1539
  if (isInternal) {
1468
1540
  const parsed =
1469
1541
  found === undefined
1470
1542
  ? getUntypedParamValue(value)
1471
1543
  : parseParamValue(found.descr.kind, value);
1472
- if (parsed === undefined) throw new SmtParseError("invalid parameter value", end);
1544
+ if (parsed === undefined)
1545
+ throw new SmtParseError("invalid parameter value", getSexprPosition(value));
1473
1546
  params.set(found?.name ?? getParamName(keyword.text), parsed);
1474
1547
  continue;
1475
1548
  }
@@ -1478,7 +1551,7 @@ const getParams = (
1478
1551
  throw new SmtUnsupportedError(`the parameter ${keyword.text} of the tactic`);
1479
1552
  throw new SmtParseError(
1480
1553
  `invalid using-params combinator, unknown parameter ${getParamName(keyword.text)}`,
1481
- end,
1554
+ getSexprPosition(value),
1482
1555
  );
1483
1556
  }
1484
1557
  const parsed = parseParamValue(found.descr.kind, value);
@@ -1491,7 +1564,7 @@ const getParams = (
1491
1564
  : found.descr.kind === PARAM_NUMERAL
1492
1565
  ? "invalid parameter value, numeral expected"
1493
1566
  : "invalid parameter value, symbol expected",
1494
- end,
1567
+ getSexprPosition(value),
1495
1568
  );
1496
1569
  if (isUnportedParam(found.descr, parsed))
1497
1570
  throw new SmtUnsupportedError(`the parameter ${found.name} of the tactic`);
@@ -1510,19 +1583,30 @@ const isDecidedSat = (goals: Goal[]) => goals.length === 1 && goals[0].isDecided
1510
1583
 
1511
1584
  const isDecidedUnsat = (goals: Goal[]) => goals.length === 1 && goals[0].isDecidedUnsat();
1512
1585
 
1586
+ // Deviation from Z3: and_then and repeat join the subgoals' cores, where Z3 answers an empty core.
1587
+ const assertUnsat = (goal: Goal, unsatGoals: Goal[]) => {
1588
+ goal.resetAll();
1589
+ goal.assert(
1590
+ goal.manager.falseExpr,
1591
+ unsatGoals.reduce<Dependency | undefined>(
1592
+ (core, unsatGoal) => joinDependencies(unsatGoal.getDependency(0), core),
1593
+ undefined,
1594
+ ),
1595
+ );
1596
+ return [goal];
1597
+ };
1598
+
1513
1599
  const runAndThen = (first: Tactic, second: Tactic) => (goal: Goal) => {
1514
1600
  const goals = first(goal);
1515
1601
  if (goals.length === 1) return goals[0].isDecided() ? goals : second(goals[0]);
1516
1602
  const results: Goal[] = [];
1603
+ const unsatGoals: Goal[] = [];
1517
1604
  for (const subgoal of goals) {
1518
1605
  const subresults = second(subgoal);
1519
1606
  if (isDecidedSat(subresults)) return subresults;
1520
- if (!isDecidedUnsat(subresults)) results.push(...subresults);
1607
+ (isDecidedUnsat(subresults) ? unsatGoals : results).push(...subresults);
1521
1608
  }
1522
- if (results.length > 0) return results;
1523
- goal.resetAll();
1524
- goal.assert(goal.manager.falseExpr);
1525
- return [goal];
1609
+ return results.length > 0 ? results : assertUnsat(goal, unsatGoals);
1526
1610
  };
1527
1611
 
1528
1612
  const runOrElse = (tactics: Tactic[]) => (goal: Goal) => {
@@ -1531,12 +1615,12 @@ const runOrElse = (tactics: Tactic[]) => (goal: Goal) => {
1531
1615
  try {
1532
1616
  return tactic(goal);
1533
1617
  } catch (error) {
1534
- if (!(error instanceof SmtTacticError)) throw error;
1618
+ if (!(error instanceof SmtTacticError || error instanceof SmtCanceledError)) throw error;
1535
1619
  }
1536
1620
  goal.resetAll();
1537
1621
  goal.copyFrom(original);
1538
1622
  }
1539
- return (tactics.at(-1) as Tactic)(goal);
1623
+ return tactics[tactics.length - 1](goal);
1540
1624
  };
1541
1625
 
1542
1626
  const isEqualGoal = (first: Goal, second: Goal) => {
@@ -1556,15 +1640,13 @@ const runRepeat = (tactic: Tactic, maxDepth: number) => {
1556
1640
  if (goals.length === 1 && isEqualGoal(original, goals[0])) return goals;
1557
1641
  if (goals.length === 1) return goals[0].isDecided() ? goals : run(depth + 1, goals[0]);
1558
1642
  const results: Goal[] = [];
1643
+ const unsatGoals: Goal[] = [];
1559
1644
  for (const subgoal of goals) {
1560
1645
  const subresults = run(depth + 1, subgoal);
1561
1646
  if (isDecidedSat(subresults)) return subresults;
1562
- if (!isDecidedUnsat(subresults)) results.push(...subresults);
1647
+ (isDecidedUnsat(subresults) ? unsatGoals : results).push(...subresults);
1563
1648
  }
1564
- if (results.length > 0) return results;
1565
- goal.resetAll();
1566
- goal.assert(goal.manager.falseExpr);
1567
- return [goal];
1649
+ return results.length > 0 ? results : assertUnsat(goal, unsatGoals);
1568
1650
  };
1569
1651
  return (goal: Goal) => run(0, goal);
1570
1652
  };
@@ -1572,22 +1654,21 @@ const runRepeat = (tactic: Tactic, maxDepth: number) => {
1572
1654
  const runCond = (probe: Probe, onTrue: Tactic, onFalse: Tactic) => (goal: Goal) =>
1573
1655
  probe(goal) === 0 ? onFalse(goal) : onTrue(goal);
1574
1656
 
1575
- const checkHasArgument = (list: SmtList, combinator: string, end: SmtToken) => {
1657
+ const checkHasArgument = (list: SmtList, combinator: string) => {
1576
1658
  if (list.items.length < 2)
1577
1659
  throw new SmtParseError(
1578
1660
  `invalid ${combinator} combinator, at least one argument expected`,
1579
- end,
1661
+ list.end,
1580
1662
  );
1581
1663
  };
1582
1664
 
1583
1665
  const compile = (
1584
1666
  environment: TacticEnvironment,
1585
1667
  sexpr: SExpr,
1586
- end: SmtToken,
1587
1668
  isInternal: boolean,
1588
1669
  ): TacticBuilder => {
1589
1670
  if (sexpr.kind !== "list") {
1590
- if (!isSymbol(sexpr)) throw new SmtParseError("invalid tactic, unexpected input", end);
1671
+ if (!isSymbol(sexpr)) throw new SmtParseError("invalid tactic, unexpected input", sexpr);
1591
1672
  const named = NAMED_TACTICS.get(sexpr.text);
1592
1673
  if (named !== undefined)
1593
1674
  return {
@@ -1605,7 +1686,7 @@ const compile = (
1605
1686
  };
1606
1687
  const strategy = LOGIC_TACTICS.get(sexpr.text);
1607
1688
  if (strategy !== undefined)
1608
- return compile(environment, createCommandReader(strategy).read() as SmtList, end, true);
1689
+ return compile(environment, createCommandReader(strategy).read() as SmtList, true);
1609
1690
  if (UNPORTED_TACTICS.has(sexpr.text)) {
1610
1691
  const error = new SmtUnsupportedError(`the tactic ${sexpr.text}`);
1611
1692
  if (!isInternal) throw error;
@@ -1618,14 +1699,14 @@ const compile = (
1618
1699
  },
1619
1700
  };
1620
1701
  }
1621
- throw new SmtParseError(`invalid tactic, unknown tactic ${sexpr.text}`, end);
1702
+ throw new SmtParseError(`invalid tactic, unknown tactic ${sexpr.text}`, sexpr);
1622
1703
  }
1623
1704
  const head = sexpr.items[0];
1624
- if (head === undefined) throw new SmtParseError("invalid tactic, arguments expected", end);
1625
- if (!isSymbol(head)) throw new SmtParseError("invalid tactic, symbol expected", end);
1705
+ if (head === undefined) throw new SmtParseError("invalid tactic, arguments expected", sexpr.end);
1706
+ if (!isSymbol(head)) throw new SmtParseError("invalid tactic, symbol expected", sexpr.end);
1626
1707
  const compileChildren = (combinator: string) => {
1627
- checkHasArgument(sexpr, combinator, end);
1628
- return sexpr.items.slice(1).map((child) => compile(environment, child, end, isInternal));
1708
+ checkHasArgument(sexpr, combinator);
1709
+ return sexpr.items.slice(1).map((child) => compile(environment, child, isInternal));
1629
1710
  };
1630
1711
  switch (head.text) {
1631
1712
  case "then":
@@ -1675,27 +1756,43 @@ const compile = (
1675
1756
  // The timeout is not ported, so the child runs to the end.
1676
1757
  case "try-for": {
1677
1758
  if (sexpr.items.length !== 3)
1678
- throw new SmtParseError("invalid try-for combinator, two arguments expected", end);
1679
- if (!isUnsigned(sexpr.items[2]))
1759
+ throw new SmtParseError("invalid try-for combinator, two arguments expected", sexpr.end);
1760
+ if (!isUnsignedNumeral(sexpr.items[2]))
1680
1761
  throw new SmtParseError(
1681
1762
  "invalid try-for combinator, second argument must be an unsigned integer",
1682
- end,
1763
+ sexpr.end,
1683
1764
  );
1684
- return compile(environment, sexpr.items[1], end, isInternal);
1765
+ const child = compile(environment, sexpr.items[1], isInternal);
1766
+ return {
1767
+ ...child,
1768
+ build: (params) => {
1769
+ const tactic = child.build(params);
1770
+ return (goal) => {
1771
+ try {
1772
+ return tactic(goal);
1773
+ } catch (error) {
1774
+ if (error instanceof SmtDefaultError) throw new SmtTacticError(error.message);
1775
+ if (error instanceof SmtCanceledError)
1776
+ throw new SmtTacticError(goal.manager.limit.getCancelMessage());
1777
+ throw error;
1778
+ }
1779
+ };
1780
+ },
1781
+ };
1685
1782
  }
1686
1783
  case "fail-if-branching": {
1687
1784
  if (sexpr.items.length !== 3 && sexpr.items.length !== 2)
1688
1785
  throw new SmtParseError(
1689
1786
  "invalid fail-if-branching combinator, one or two arguments expected",
1690
- end,
1787
+ sexpr.end,
1691
1788
  );
1692
1789
  const bound = sexpr.items[2];
1693
- if (bound !== undefined && !isUnsigned(bound))
1790
+ if (bound !== undefined && !isUnsignedNumeral(bound))
1694
1791
  throw new SmtParseError(
1695
1792
  "invalid fail-if-branching combinator, second argument must be an unsigned integer",
1696
- end,
1793
+ sexpr.end,
1697
1794
  );
1698
- const child = compile(environment, sexpr.items[1], end, isInternal);
1795
+ const child = compile(environment, sexpr.items[1], isInternal);
1699
1796
  const threshold = bound === undefined ? 1 : Number(bound.text);
1700
1797
  return {
1701
1798
  ...mergeDescrs([child]),
@@ -1713,11 +1810,17 @@ const compile = (
1713
1810
  case "if-no-unsat-cores":
1714
1811
  case "if-no-models": {
1715
1812
  if (sexpr.items.length !== 2)
1716
- throw new SmtParseError(`invalid ${head.text} combinator, one argument expected`, end);
1717
- const child = compile(environment, sexpr.items[1], end, isInternal);
1813
+ throw new SmtParseError(
1814
+ `invalid ${head.text} combinator, one argument expected`,
1815
+ sexpr.end,
1816
+ );
1817
+ const child = compile(environment, sexpr.items[1], isInternal);
1718
1818
  const isSkipping = (goal: Goal) =>
1719
- head.text === "if-no-models" ||
1720
- (head.text === "if-no-proofs" ? goal.isProofsEnabled : goal.isCoresEnabled);
1819
+ head.text === "if-no-models"
1820
+ ? goal.isModelsEnabled
1821
+ : head.text === "if-no-proofs"
1822
+ ? goal.isProofsEnabled
1823
+ : goal.isCoresEnabled;
1721
1824
  return {
1722
1825
  ...mergeDescrs([child]),
1723
1826
  build: (params) => {
@@ -1728,14 +1831,17 @@ const compile = (
1728
1831
  }
1729
1832
  case "repeat": {
1730
1833
  if (sexpr.items.length !== 3 && sexpr.items.length !== 2)
1731
- throw new SmtParseError("invalid repeat combinator, one or two arguments expected", end);
1834
+ throw new SmtParseError(
1835
+ "invalid repeat combinator, one or two arguments expected",
1836
+ sexpr.end,
1837
+ );
1732
1838
  const bound = sexpr.items[2];
1733
- if (bound !== undefined && !isUnsigned(bound))
1839
+ if (bound !== undefined && !isUnsignedNumeral(bound))
1734
1840
  throw new SmtParseError(
1735
1841
  "invalid repeat combinator, second argument must be an unsigned integer",
1736
- end,
1842
+ sexpr.end,
1737
1843
  );
1738
- const child = compile(environment, sexpr.items[1], end, isInternal);
1844
+ const child = compile(environment, sexpr.items[1], isInternal);
1739
1845
  const maxDepth = bound === undefined ? UINT_MAX : Number(bound.text);
1740
1846
  return {
1741
1847
  ...mergeDescrs([child]),
@@ -1745,10 +1851,10 @@ const compile = (
1745
1851
  case "!":
1746
1852
  case "using-params":
1747
1853
  case "with": {
1748
- checkHasArgument(sexpr, "using-params", end);
1749
- const child = compile(environment, sexpr.items[1], end, isInternal);
1854
+ checkHasArgument(sexpr, "using-params");
1855
+ const child = compile(environment, sexpr.items[1], isInternal);
1750
1856
  if (sexpr.items.length === 2) return child;
1751
- const own = getParams(child, sexpr.items, end, isInternal);
1857
+ const own = getParams(child, sexpr.items, isInternal);
1752
1858
  return {
1753
1859
  ...mergeDescrs([child]),
1754
1860
  build: (params) => child.build(new Map([...params, ...own])),
@@ -1756,8 +1862,11 @@ const compile = (
1756
1862
  }
1757
1863
  case "skip-if-failed": {
1758
1864
  if (sexpr.items.length !== 2)
1759
- throw new SmtParseError("invalid skip-if-failed combinator, one argument expected", end);
1760
- const child = compile(environment, sexpr.items[1], end, isInternal);
1865
+ throw new SmtParseError(
1866
+ "invalid skip-if-failed combinator, one argument expected",
1867
+ sexpr.end,
1868
+ );
1869
+ const child = compile(environment, sexpr.items[1], isInternal);
1761
1870
  return {
1762
1871
  ...mergeDescrs([child]),
1763
1872
  build: (params) => runOrElse([child.build(params), (goal) => [goal]]),
@@ -1767,11 +1876,12 @@ const compile = (
1767
1876
  case "ite":
1768
1877
  case "cond": {
1769
1878
  if (sexpr.items.length !== 4)
1770
- throw new SmtParseError("invalid if/conditional combinator, three arguments expected", end);
1771
- const probe = compileProbe(sexpr.items[1], end, isInternal);
1772
- const children = sexpr.items
1773
- .slice(2)
1774
- .map((child) => compile(environment, child, end, isInternal));
1879
+ throw new SmtParseError(
1880
+ "invalid if/conditional combinator, three arguments expected",
1881
+ sexpr.end,
1882
+ );
1883
+ const probe = compileProbe(sexpr.items[1], isInternal);
1884
+ const children = sexpr.items.slice(2).map((child) => compile(environment, child, isInternal));
1775
1885
  return {
1776
1886
  ...mergeDescrs(children),
1777
1887
  build: (params) => runCond(probe, children[0].build(params), children[1].build(params)),
@@ -1779,9 +1889,9 @@ const compile = (
1779
1889
  }
1780
1890
  case "when": {
1781
1891
  if (sexpr.items.length !== 3)
1782
- throw new SmtParseError("invalid when combinator, two arguments expected", end);
1783
- const probe = compileProbe(sexpr.items[1], end, isInternal);
1784
- const child = compile(environment, sexpr.items[2], end, isInternal);
1892
+ throw new SmtParseError("invalid when combinator, two arguments expected", sexpr.end);
1893
+ const probe = compileProbe(sexpr.items[1], isInternal);
1894
+ const child = compile(environment, sexpr.items[2], isInternal);
1785
1895
  return {
1786
1896
  ...mergeDescrs([child]),
1787
1897
  build: (params) => runCond(probe, child.build(params), (goal) => [goal]),
@@ -1789,8 +1899,8 @@ const compile = (
1789
1899
  }
1790
1900
  case "fail-if": {
1791
1901
  if (sexpr.items.length !== 2)
1792
- throw new SmtParseError("invalid fail-if tactic, one argument expected", end);
1793
- const probe = compileProbe(sexpr.items[1], end, isInternal);
1902
+ throw new SmtParseError("invalid fail-if tactic, one argument expected", sexpr.end);
1903
+ const probe = compileProbe(sexpr.items[1], isInternal);
1794
1904
  return {
1795
1905
  descrs: new Map(),
1796
1906
  isRejectingUnknown: true,
@@ -1803,7 +1913,7 @@ const compile = (
1803
1913
  }
1804
1914
  case "echo": {
1805
1915
  if (sexpr.items.length < 2)
1806
- throw new SmtParseError("invalid echo tactic, must have at least one argument", end);
1916
+ throw new SmtParseError("invalid echo tactic, must have at least one argument", sexpr.end);
1807
1917
  const pieces = sexpr.items.slice(1).map((piece, index, all): Tactic => {
1808
1918
  const newline = index === all.length - 1 ? "\n" : "";
1809
1919
  if (piece.kind === "string")
@@ -1811,7 +1921,7 @@ const compile = (
1811
1921
  environment.write(piece.text + newline, false);
1812
1922
  return [goal];
1813
1923
  };
1814
- const probe = compileProbe(piece, end, isInternal);
1924
+ const probe = compileProbe(piece, isInternal);
1815
1925
  return (goal) => {
1816
1926
  environment.write(formatProbeValue(probe(goal)) + newline, true);
1817
1927
  return [goal];
@@ -1825,7 +1935,7 @@ const compile = (
1825
1935
  };
1826
1936
  }
1827
1937
  default:
1828
- throw new SmtParseError(`invalid tactic, unknown tactic combinator ${head.text}`, end);
1938
+ throw new SmtParseError(`invalid tactic, unknown tactic combinator ${head.text}`, sexpr.end);
1829
1939
  }
1830
1940
  };
1831
1941
 
@@ -1873,12 +1983,12 @@ export const mkIncSatPreprocess = (manager: AstManager, bitBlaster: BitBlasterRe
1873
1983
  ),
1874
1984
  ].reduceRight((second, first) => runAndThen(first, second));
1875
1985
 
1876
- export const compileTactic = (environment: TacticEnvironment, sexpr: SExpr, end: SmtToken) =>
1877
- compile(environment, sexpr, end, false);
1986
+ export const compileTactic = (environment: TacticEnvironment, sexpr: SExpr) =>
1987
+ compile(environment, sexpr, false);
1878
1988
 
1879
1989
  export const compileInternalTactic = (environment: TacticEnvironment, text: string) => {
1880
1990
  const list = createCommandReader(`(${text})`).read() as SmtList;
1881
- return compile(environment, list.items[0], list.end, true).build(new Map());
1991
+ return compile(environment, list.items[0], true).build(new Map());
1882
1992
  };
1883
1993
 
1884
1994
  interface TacticCheck {
@@ -1886,13 +1996,14 @@ interface TacticCheck {
1886
1996
  model: ProtoModel | undefined;
1887
1997
  core: Dependency | undefined;
1888
1998
  reasonUnknown: string | undefined;
1999
+ isConversionCanceled: boolean;
1889
2000
  }
1890
2001
 
1891
2002
  // The port's declarations of division by zero leave out their domain; Z3's have two arguments.
1892
2003
  const DIVISION_BY_ZERO_DECLS = new Set([OP_DIV0, OP_IDIV0, OP_MOD0, OP_POWER0]);
1893
2004
 
1894
2005
  // A constant of an uninterpreted sort the model lacks puts its value in that sort's universe.
1895
- const applyDefinitions = (definitions: Definition[], model: ProtoModel) => {
2006
+ export const applyDefinitions = (definitions: readonly Definition[], model: ProtoModel) => {
1896
2007
  const manager = model.manager;
1897
2008
  const universe: number[] = [];
1898
2009
  let evaluator = new ModelEvaluator(manager, model);
@@ -1944,52 +2055,87 @@ const applyDefinitions = (definitions: Definition[], model: ProtoModel) => {
1944
2055
  return model;
1945
2056
  };
1946
2057
 
2058
+ const applyConverter = (entry: Goal["converters"][number], model: ProtoModel) => {
2059
+ if ("convert" in entry) return entry.convert(model);
2060
+ if ("definitions" in entry) return applyDefinitions(entry.definitions, model);
2061
+ model.unregisterDecl(entry.decl);
2062
+ return model;
2063
+ };
2064
+
1947
2065
  export const convertModel = (goal: Goal, model: ProtoModel) =>
1948
- goal.converters.reduceRight((converted, entry) => {
1949
- if ("convert" in entry) return entry.convert(converted);
1950
- if ("definitions" in entry) return applyDefinitions(entry.definitions, converted);
1951
- converted.unregisterDecl(entry.decl);
1952
- return converted;
1953
- }, model);
1954
-
1955
- export const checkSatUsing = (tactic: Tactic, goal: Goal): TacticCheck => {
1956
- const undecided = { status: L_UNDEF, model: undefined, core: undefined };
1957
- const goals = (() => {
2066
+ goal.converters.reduceRight((converted, entry) => applyConverter(entry, converted), model);
2067
+
2068
+ const getConvertedModel = (goal: Goal) => {
2069
+ let model = goal.model?.copy() ?? new ProtoModel(goal.manager).mkModel();
2070
+ for (let index = goal.converters.length - 1; index >= 0; index--) {
1958
2071
  try {
1959
- return tactic(goal);
2072
+ model = applyConverter(goal.converters[index], model);
1960
2073
  } catch (error) {
1961
- if (error instanceof SmtTacticError) return error.message;
1962
- if (error instanceof SmtCanceledError) return undefined;
1963
- throw error;
2074
+ if (!(error instanceof SmtCanceledError)) throw error;
2075
+ return { model, isCanceled: true };
1964
2076
  }
1965
- })();
2077
+ }
2078
+ return { model, isCanceled: false };
2079
+ };
2080
+
2081
+ const getTacticCheck = (goal: Goal, goals: Goal[] | string | undefined): TacticCheck => {
2082
+ const undecided = {
2083
+ status: L_UNDEF,
2084
+ model: undefined,
2085
+ core: undefined,
2086
+ isConversionCanceled: false,
2087
+ };
1966
2088
  if (goals === undefined) return { ...undecided, reasonUnknown: undefined };
1967
2089
  if (typeof goals === "string") return { ...undecided, reasonUnknown: goals };
1968
- if (isDecidedSat(goals)) {
1969
- const model = convertModel(goals[0], goals[0].model ?? new ProtoModel(goal.manager));
2090
+ if (isDecidedUnsat(goals))
2091
+ return { ...undecided, status: L_FALSE, core: goals[0].getDependency(0), reasonUnknown: "" };
2092
+ const isSat = isDecidedSat(goals);
2093
+ const converted =
2094
+ isSat ||
2095
+ (goal.isModelsEnabled &&
2096
+ goals.length > 0 &&
2097
+ (goals[0].model !== undefined || goals[0].converters.length > 0))
2098
+ ? getConvertedModel(goals[0])
2099
+ : undefined;
2100
+ const model = converted?.model;
2101
+ if (converted?.isCanceled === true)
2102
+ return { ...undecided, model, reasonUnknown: undefined, isConversionCanceled: true };
2103
+ if (isSat)
1970
2104
  return goal.manager.limit.inc()
1971
- ? { status: L_TRUE, model, core: undefined, reasonUnknown: "" }
2105
+ ? { ...undecided, status: L_TRUE, model, reasonUnknown: "" }
1972
2106
  : { ...undecided, model, reasonUnknown: "canceled" };
1973
- }
1974
- if (isDecidedUnsat(goals))
1975
- return {
1976
- status: L_FALSE,
1977
- model: undefined,
1978
- core: goals[0].getDependency(0),
1979
- reasonUnknown: "",
1980
- };
1981
2107
  return {
1982
2108
  ...undecided,
2109
+ model,
1983
2110
  reasonUnknown:
1984
2111
  goals.length === 1 && goals[0].reasonUnknown !== "" ? goals[0].reasonUnknown : "unknown",
1985
2112
  };
1986
2113
  };
1987
2114
 
2115
+ export const checkSatUsing = (tactic: Tactic, goal: Goal) => {
2116
+ const goals = (() => {
2117
+ try {
2118
+ return tactic(goal);
2119
+ } catch (error) {
2120
+ if (error instanceof SmtTacticError || error instanceof SmtDefaultError) return error.message;
2121
+ if (error instanceof SmtCanceledError) return undefined;
2122
+ throw error;
2123
+ }
2124
+ })();
2125
+ try {
2126
+ return getTacticCheck(goal, goals);
2127
+ } finally {
2128
+ goal.reset();
2129
+ if (Array.isArray(goals)) for (const result of goals) result.reset();
2130
+ }
2131
+ };
2132
+
1988
2133
  export const applyTactic = (tactic: Tactic, goal: Goal) => {
1989
2134
  try {
1990
2135
  return tactic(goal);
1991
2136
  } catch (error) {
1992
- if (error instanceof SmtCanceledError) throw new SmtTacticError("canceled");
2137
+ if (error instanceof SmtCanceledError)
2138
+ throw new SmtTacticError(goal.manager.limit.getCancelMessage());
1993
2139
  throw error;
1994
2140
  }
1995
2141
  };