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
@@ -3,6 +3,7 @@ import {
3
3
  isStackOverflow,
4
4
  SmtCanceledError,
5
5
  SmtParseError,
6
+ SmtDefaultError,
6
7
  SmtTacticError,
7
8
  SmtUnsupportedError,
8
9
  } from "../../../errors";
@@ -38,19 +39,23 @@ import {
38
39
  createCommandReader,
39
40
  createSmtlibReader,
40
41
  DECLARATIONS,
42
+ expectClosed,
41
43
  isSymbol,
44
+ isUnsigned,
42
45
  type SExpr,
43
46
  type SmtList,
44
47
  type SmtlibReader,
48
+ type SmtToken,
45
49
  } from "./smtlib-parser";
46
50
  import { O_MAXIMIZE, O_MAXSMT, O_MINIMIZE, OptContext, type OptObjective } from "./opt-context";
47
51
  import { getLogicStrategy } from "./logic-tactics";
48
52
  import { isMbqiByFeatures, isMbqiByLogic } from "./smt-setup";
49
- import { isSupportedLogic } from "./smt-logics";
53
+ import { isSupportedLogic, logicHasSeq } from "./smt-logics";
50
54
  import { getVerdict, solveAssertions, solveAssuming } from "./solver";
51
55
  import { getConsequences } from "./smt-consequences";
52
- import { getFdConsequences } from "./fd-solver";
56
+ import { FdSolver } from "./fd-solver";
53
57
  import { SmtKernel } from "./smt-kernel";
58
+ import { RefVector } from "./ref-vector";
54
59
  import { IncSatSolver } from "./sat/inc-sat-solver";
55
60
  import { mkThSolver } from "./euf/th-solvers";
56
61
  import { isAstLess } from "./ast-lt";
@@ -61,6 +66,7 @@ import { L_FALSE, L_TRUE, L_UNDEF } from "./lbool";
61
66
  import type { DatalogContext } from "./muz/dl-context";
62
67
  import { createDatalogContext } from "./muz/horn-tactic";
63
68
  import { printModel } from "./model-printer";
69
+ import { ModelEvaluator } from "./model-evaluator";
64
70
  import { FuncInterp, getManagerSomeValue, ProtoModel } from "./proto-model";
65
71
  import {
66
72
  applyTactic,
@@ -73,6 +79,56 @@ import {
73
79
  type TacticParam,
74
80
  } from "./tactic";
75
81
 
82
+ const UNPORTED_COMMANDS = new Set([
83
+ "assert-not",
84
+ "assume",
85
+ "dbg-bool-rewriter",
86
+ "dbg-elim-and",
87
+ "dbg-get-qbody",
88
+ "dbg-params",
89
+ "dbg-pp-var",
90
+ "dbg-set",
91
+ "dbg-set-next-id",
92
+ "dbg-sexpr",
93
+ "dbg-shift-vars",
94
+ "dbg-size",
95
+ "dbg-subst",
96
+ "dbg-th-rewriter",
97
+ "dbg-translator",
98
+ "declare-map",
99
+ "declare-tactic",
100
+ "declare-type-var",
101
+ "del",
102
+ "display-dimacs",
103
+ "euf-project",
104
+ "eufi",
105
+ "ff-boolean-certify",
106
+ "ff-certify",
107
+ "ff-check-native-proof",
108
+ "ff-integrated-certify",
109
+ "get-interpolant",
110
+ "get-proof",
111
+ "get-proof-graph",
112
+ "get-user-tactics",
113
+ "help",
114
+ "help-simplifier",
115
+ "help-tactic",
116
+ "include",
117
+ "infer",
118
+ "labels",
119
+ "mbi",
120
+ "mbp",
121
+ "mbp-qel",
122
+ "model-add",
123
+ "model-del",
124
+ "prefer",
125
+ "qe-lite",
126
+ "qel",
127
+ "reset-preferences",
128
+ "set-initial-value",
129
+ "set-simplifier",
130
+ ]);
131
+
76
132
  const SILENT_COMMANDS = new Set([
77
133
  ...DECLARATIONS,
78
134
  "assert",
@@ -82,8 +138,6 @@ const SILENT_COMMANDS = new Set([
82
138
  "reset-assertions",
83
139
  "set-option",
84
140
  "set-info",
85
- "minimize",
86
- "maximize",
87
141
  "assert-soft",
88
142
  "declare-rel",
89
143
  "declare-var",
@@ -122,6 +176,7 @@ const SIMPLIFY_PARAMETERS = new Set([
122
176
  "som",
123
177
  "sort_sums",
124
178
  "arith_lhs",
179
+ "arith_ineq_lhs",
125
180
  "gcd_rounding",
126
181
  "hi_div0",
127
182
  "mul2concat",
@@ -176,7 +231,9 @@ const getSoftParameters = (items: SExpr[]) => {
176
231
  const parameters = { weight: Rational.one, id: null as string | null };
177
232
  for (let index = 0; index < items.length; index += 2) {
178
233
  const keyword = items[index];
179
- const name = keyword.kind === "keyword" ? getParameterName(keyword.text) : "";
234
+ if (keyword.kind !== "keyword")
235
+ throw new SmtParseError("invalid command argument, keyword expected", keyword);
236
+ const name = getParameterName(keyword.text);
180
237
  const value = items[index + 1];
181
238
  if (name === "weight" && (value?.kind === "numeral" || value?.kind === "decimal")) {
182
239
  const [whole, fraction = ""] = value.text.split(".");
@@ -194,7 +251,9 @@ const getSimplifyParams = (items: SExpr[]) => {
194
251
  const params = new Map<string, TacticParam>();
195
252
  for (let index = 0; index < items.length; index += 2) {
196
253
  const keyword = items[index];
197
- const name = keyword.kind === "keyword" ? getParameterName(keyword.text) : "";
254
+ if (keyword.kind !== "keyword")
255
+ throw new SmtParseError("invalid command argument, keyword expected", keyword);
256
+ const name = getParameterName(keyword.text);
198
257
  const value = items[index + 1];
199
258
  if (SIMPLIFY_UINT_PARAMETERS.has(name) && value?.kind === "numeral") {
200
259
  params.set(name, Number(value.text));
@@ -207,6 +266,17 @@ const getSimplifyParams = (items: SExpr[]) => {
207
266
  return params;
208
267
  };
209
268
 
269
+ const GET_INFO_KEYWORDS = [
270
+ ":reason-unknown",
271
+ ":status",
272
+ ":version",
273
+ ":authors",
274
+ ":error-behavior",
275
+ ":parameters",
276
+ ":rlimit",
277
+ ":assertion-stack-levels",
278
+ ];
279
+
210
280
  const APPLY_PARAMETERS = new Map([
211
281
  ["print", true],
212
282
  ["print_model_converter", true],
@@ -246,12 +316,29 @@ const getCommandParams = (items: SExpr[], isBooleans: ReadonlyMap<string, boolea
246
316
  "invalid command argument, unsigned integer expected",
247
317
  value ?? keyword,
248
318
  );
319
+ if (!isUnsigned(value.text))
320
+ throw new SmtParseError(
321
+ "invalid indexed identifier, index is too big to fit in an unsigned machine integer",
322
+ value,
323
+ );
249
324
  params.set(name, Number(value.text));
250
325
  }
251
326
  }
252
327
  return params;
253
328
  };
254
329
 
330
+ const getFirstArgument = (list: SmtList) => {
331
+ const argument = list.items[1];
332
+ if (argument === undefined)
333
+ throw new SmtParseError("invalid command, argument(s) missing", list.end);
334
+ return argument;
335
+ };
336
+
337
+ const checkArgumentCount = (list: SmtList, count: number) => {
338
+ const extra = list.items[count + 1];
339
+ if (extra !== undefined) throw new SmtParseError("invalid command, too many arguments", extra);
340
+ };
341
+
255
342
  // Z3's parse_opt_unsigned names the push command for pop as well.
256
343
  const getScopeCount = (list: SmtList) => {
257
344
  const argument = list.items[1];
@@ -267,13 +354,16 @@ interface Scope {
267
354
  assertions: number;
268
355
  objectives: number;
269
356
  softTrail: number;
357
+ rlimit: number;
270
358
  }
271
359
 
272
360
  interface CheckResult {
273
361
  status: number;
274
362
  context: Context | undefined;
275
363
  isOptimizing: boolean;
364
+ isFromTactic: boolean;
276
365
  model: ProtoModel | undefined;
366
+ fdSolver: FdSolver | undefined;
277
367
  core: number[] | undefined;
278
368
  // check_sat_result::reason_unknown, where the port tracks it.
279
369
  reasonUnknown?: string;
@@ -306,9 +396,8 @@ interface ScriptState {
306
396
  // combined_solver's incremental smt kernel, which keeps one context across the checks, or
307
397
  // undefined while Z3 has made no such kernel.
308
398
  kernel: SmtKernel | undefined;
309
- // The incremental solver of sat.euf, inc_sat_solver in euf mode, or undefined once it reached
310
- // an unported theory and the smt kernel answers in its place.
311
- eufSolver: IncSatSolver | undefined;
399
+ satSolver: IncSatSolver | undefined;
400
+ fdSolver: FdSolver | undefined;
312
401
  }
313
402
 
314
403
  // Z3 fixes the proof mode at the first command that needs terms.
@@ -338,6 +427,7 @@ export const runSmtlibScript = (
338
427
  isPrintingSuccess: false,
339
428
  isProducingModels: true,
340
429
  isProducingUnsatCores: false,
430
+ isProducingUnsatAssumptions: false,
341
431
  isProducingProofs: false,
342
432
  isExitingOnError: false,
343
433
  isInteractive: false,
@@ -351,6 +441,8 @@ export const runSmtlibScript = (
351
441
  rlimit: 0,
352
442
  printOptions: { ...DEFAULT_PRINT_OPTIONS },
353
443
  parserOptions: { isIgnoringBadPatterns: true },
444
+ // Z3 keeps set-info's :status across reset.
445
+ status: L_UNDEF,
354
446
  };
355
447
  const createState = (): ScriptState => {
356
448
  const manager = new AstManager(new ResourceLimit(Infinity, options.signal));
@@ -374,7 +466,8 @@ export const runSmtlibScript = (
374
466
  hasChecked: false,
375
467
  datalog: undefined,
376
468
  kernel: undefined,
377
- eufSolver: undefined,
469
+ satSolver: undefined,
470
+ fdSolver: undefined,
378
471
  };
379
472
  };
380
473
  let state = createState();
@@ -415,7 +508,7 @@ export const runSmtlibScript = (
415
508
  paretoMaxLemmas: Number(parameters.get("opt.pareto_nlsat_max_lemmas") ?? 128),
416
509
  };
417
510
  };
418
- const optimize = (extra: number[]) => {
511
+ const optimize = (extra: number[], end: SmtToken) => {
419
512
  const manager = state.manager;
420
513
  const objectives = state.objectives;
421
514
  const hard = [...getNamedAssertions(), ...state.names.filter((name) => name !== -1), ...extra];
@@ -471,10 +564,16 @@ export const runSmtlibScript = (
471
564
  resetOptimizer();
472
565
  return false;
473
566
  }
567
+ if (error instanceof SmtDefaultError) {
568
+ setCheck(L_UNDEF, { isFromTactic: true, reasonUnknown: optimizer.getReasonUnknown() });
569
+ throw new SmtParseError(error.message, end);
570
+ }
474
571
  if (!(error instanceof SmtCanceledError || isStackOverflow(error))) throw error;
475
572
  const reasonUnknown = optimizer.getReasonUnknown();
476
573
  optimizer.release();
477
574
  setCheck(L_UNDEF, { isOptimizing: true, reasonUnknown });
575
+ if (error instanceof SmtCanceledError)
576
+ throw new SmtParseError(manager.limit.getCancelMessage(), end);
478
577
  print("unknown");
479
578
  return true;
480
579
  } finally {
@@ -488,11 +587,11 @@ export const runSmtlibScript = (
488
587
  };
489
588
  // The port keeps answering with the smt kernel where it lacks a piece of the logic's strategy.
490
589
  const solveWithStrategy = () => {
491
- const goal = getGoal();
492
- const strategy =
493
- settings.parameters.get("tactic.default_tactic") ??
494
- getLogicStrategy(state.logic, goal.isProofsEnabled);
495
590
  try {
591
+ const goal = getGoal();
592
+ const strategy =
593
+ settings.parameters.get("tactic.default_tactic") ??
594
+ getLogicStrategy(state.logic, goal.isProofsEnabled);
496
595
  const checked = checkSatUsing(compileInternalTactic(getTacticEnvironment(), strategy), goal);
497
596
  return {
498
597
  result: checked.status,
@@ -539,20 +638,28 @@ export const runSmtlibScript = (
539
638
  isMbqi: settings.isMbqi || (!isSetUp && isMbqiByLogic(getSetup())),
540
639
  mbqiMaxIterations: settings.mbqiMaxIterations,
541
640
  isFindingMacros: settings.isFindingMacros,
641
+ isCoreMinimizing: settings.parameters.get("smt.core.minimize") === "true",
542
642
  setup: getSetup(),
543
643
  });
544
- // mk_special_solver_for_logic gives these logics the finite-domain solver, incremental or not.
545
- const isUsingFdSolver = () =>
546
- (state.logic === "QF_FD" || state.logic === "SAT") &&
547
- !settings.isProducingProofs &&
548
- !settings.parameters.has("tactic.default_tactic");
549
- const checkSat = (extra: number[]) => {
644
+ const checkFdSolver = (fdSolver: FdSolver, extra: number[]) => {
645
+ const result = fdSolver.check(extra);
646
+ // cmd_context::check_sat counts a step to see whether the limit stopped a search that gave up.
647
+ if (result === L_UNDEF) state.manager.limit.inc();
648
+ return {
649
+ result,
650
+ context: undefined,
651
+ model: undefined,
652
+ core: result === L_FALSE ? [...fdSolver.getUnsatCore()] : undefined,
653
+ reasonUnknown: fdSolver.reasonUnknown,
654
+ };
655
+ };
656
+ const checkSat = (extra: number[], end: SmtToken) => {
550
657
  const deadline = Date.now() + options.timeLimitMs;
551
658
  state.manager.limit.restart(deadline);
552
659
  // opt::context::optimize answers as the search of the hard constraints does: once they are
553
660
  // satisfiable, optsmt and maxsmt only improve the model. The unported parts of optimization
554
661
  // only get that answer.
555
- if (state.objectives.length > 0 && optimize(extra)) return;
662
+ if (state.objectives.length > 0 && optimize(extra, end)) return;
556
663
  state.manager.limit.push(settings.rlimit);
557
664
  const isOptimizing = state.objectives.length > 0;
558
665
  const manager = state.manager;
@@ -561,37 +668,54 @@ export const runSmtlibScript = (
561
668
  const isAssuming = isNamed || extra.length > 0;
562
669
  if (isAssuming) state.isIncremental = true;
563
670
  if (!isOptimizing) state.hasChecked = true;
671
+ const fdSolver = state.fdSolver;
564
672
  const isKernelChecking =
565
- isAssuming || (state.isIncremental && !isOptimizing && !isUsingFdSolver());
673
+ fdSolver === undefined && (isAssuming || (state.isIncremental && !isOptimizing));
566
674
  try {
567
- const assumptions = isNamed ? [...names, ...extra] : extra;
568
- const kernel = isKernelChecking ? checkKernel(assumptions) : undefined;
675
+ const kernel = isKernelChecking ? checkKernel(names, extra) : undefined;
569
676
  // Z3 retries a quantifier-free problem with its tactic when the incremental kernel gives up.
570
- const solved =
677
+ const isFromTactic =
571
678
  kernel === undefined ||
572
679
  (!isAssuming &&
573
680
  kernel.result === L_UNDEF &&
574
681
  !state.assertions.some((assertion) => manager.hasQuantifierFlags[assertion]) &&
575
- manager.limit.inc())
576
- ? { ...solveWithStrategy(), core: undefined }
577
- : kernel;
682
+ manager.limit.inc());
683
+ const solved =
684
+ fdSolver !== undefined
685
+ ? checkFdSolver(fdSolver, extra)
686
+ : isFromTactic
687
+ ? { ...solveWithStrategy(), core: undefined }
688
+ : kernel;
578
689
  setCheck(solved.result, {
579
690
  context: solved.context,
580
691
  isOptimizing,
692
+ isFromTactic,
581
693
  model: solved.model,
694
+ fdSolver,
582
695
  core: solved.core ?? solved.context?.unsatCore ?? [],
583
696
  reasonUnknown: getReasonUnknown(solved, deadline),
584
697
  });
585
698
  print(getVerdict(solved.result));
586
699
  } catch (error) {
587
- if (!(error instanceof SmtCanceledError || isStackOverflow(error))) throw error;
700
+ if (
701
+ !(
702
+ error instanceof SmtCanceledError ||
703
+ error instanceof SmtDefaultError ||
704
+ isStackOverflow(error)
705
+ )
706
+ )
707
+ throw error;
588
708
  const context = isKernelChecking ? state.kernel?.context : undefined;
709
+ const reasonUnknown = isKernelChecking
710
+ ? state.satSolver?.reasonUnknown
711
+ : state.fdSolver?.reasonUnknown;
589
712
  setCheck(L_UNDEF, {
590
713
  isOptimizing,
591
- reasonUnknown: getReasonUnknown(
592
- { result: L_UNDEF, context, reasonUnknown: undefined },
593
- deadline,
594
- ),
714
+ isFromTactic: !isKernelChecking,
715
+ reasonUnknown:
716
+ error instanceof SmtDefaultError
717
+ ? error.message
718
+ : getReasonUnknown({ result: L_UNDEF, context, reasonUnknown }, deadline),
595
719
  });
596
720
  print("unknown");
597
721
  } finally {
@@ -606,8 +730,17 @@ export const runSmtlibScript = (
606
730
  if (check.model !== undefined) return check.model;
607
731
  if (check.isOptimizing) throw new SmtUnsupportedError("the models of optimization");
608
732
  if (check.context === undefined && check.status !== L_TRUE)
609
- throw new SmtUnsupportedError("the model of a canceled search");
610
- const model = check.context?.getModel() ?? new ProtoModel(state.manager);
733
+ throw check.isFromTactic
734
+ ? new SmtParseError("model is not available", list.end)
735
+ : new SmtUnsupportedError("the model of a canceled search");
736
+ const context = check.context;
737
+ const model =
738
+ check.fdSolver !== undefined
739
+ ? check.fdSolver.getModel()
740
+ : context === undefined
741
+ ? new ProtoModel(state.manager)
742
+ : context.getModel();
743
+ if (model === undefined) throw new SmtParseError("model is not available", list.end);
611
744
  model.incRef();
612
745
  check.model = model;
613
746
  return model;
@@ -616,6 +749,12 @@ export const runSmtlibScript = (
616
749
  state.check?.model?.decRef();
617
750
  state.check = undefined;
618
751
  };
752
+ // cmd_context::validate_check_sat_result runs after the answer is printed.
753
+ const validateCheck = (end: SmtToken) => {
754
+ const status = state.check?.status ?? L_UNDEF;
755
+ if (status !== L_UNDEF && settings.status !== L_UNDEF && status !== settings.status)
756
+ throw new SmtParseError(`check annotation that says ${getVerdict(settings.status)}`, end);
757
+ };
619
758
  // The check holds a reference to its model until clearCheck.
620
759
  const setCheck = (status: number, result: Partial<Omit<CheckResult, "status">> = {}) => {
621
760
  result.model?.incRef();
@@ -623,14 +762,13 @@ export const runSmtlibScript = (
623
762
  status,
624
763
  context: undefined,
625
764
  isOptimizing: false,
765
+ isFromTactic: false,
626
766
  model: undefined,
767
+ fdSolver: undefined,
627
768
  core: undefined,
628
769
  ...result,
629
770
  };
630
771
  };
631
- // cmd_context::mk_solver: mk_solver_for_logic makes the incremental solver the smt kernel unless
632
- // the logic, the default tactic or the sat parameters pick a sat solver. Under sat.euf that
633
- // solver is inc_sat_solver in euf mode; the other sat solvers only store their assertions.
634
772
  const resetKernel = () => {
635
773
  const logic = state.logic;
636
774
  const parameters = settings.parameters;
@@ -644,43 +782,64 @@ export const runSmtlibScript = (
644
782
  parameters.get("tactic.default_tactic") === "sat" ||
645
783
  parameters.get("sat.smt") === "true" ||
646
784
  isEuf;
647
- state.eufSolver?.release();
785
+ state.satSolver?.release();
648
786
  state.kernel = isSatSolver ? undefined : new SmtKernel(state.manager, getKernelParams());
649
- state.eufSolver =
650
- isEuf && !isFdSolver && parameters.get("sat.smt") !== "true"
651
- ? new IncSatSolver(state.manager, getSatParams(parameters), true, mkThSolver)
787
+ state.satSolver =
788
+ isSatSolver && !isFdSolver && parameters.get("sat.smt") !== "true"
789
+ ? new IncSatSolver(state.manager, getSatParams(parameters), true, isEuf ? mkThSolver : null)
790
+ : undefined;
791
+ state.fdSolver?.release();
792
+ state.fdSolver =
793
+ (logic === "QF_FD" || logic === "SAT") &&
794
+ !isProofsEnabled &&
795
+ !parameters.has("tactic.default_tactic")
796
+ ? new FdSolver(state.manager, getSatParams(parameters))
652
797
  : undefined;
653
798
  for (let index = 0; index < state.scopes.length; index++) {
654
799
  state.kernel?.push();
655
- useEufSolver((solver) => solver.push());
800
+ useSatSolver((solver) => solver.push());
801
+ state.fdSolver?.push();
656
802
  }
657
803
  };
658
- // The parts of sat.euf that are not ported leave its answers to the smt kernel.
659
- const useEufSolver = <Result>(action: (solver: IncSatSolver) => Result) => {
660
- const solver = state.eufSolver;
804
+ const initManager = () => {
805
+ if (state.isProofsEnabled !== undefined) return;
806
+ state.isProofsEnabled = settings.parameters.get("proof") === "true";
807
+ if (state.logic === undefined || logicHasSeq(state.logic)) state.manager.initSeqPlugin();
808
+ if (state.logic === undefined) state.reader.registerList();
809
+ resetKernel();
810
+ };
811
+ // The parts of inc_sat_solver that are not ported leave its answers to the smt kernel.
812
+ const useSatSolver = <Result>(action: (solver: IncSatSolver) => Result) => {
813
+ const solver = state.satSolver;
661
814
  if (solver === undefined) return undefined;
662
815
  try {
663
816
  return action(solver);
664
817
  } catch (error) {
665
818
  if (!(error instanceof SmtUnsupportedError)) throw error;
666
819
  solver.release();
667
- state.eufSolver = undefined;
820
+ state.satSolver = undefined;
668
821
  return undefined;
669
822
  }
670
823
  };
671
824
  // solver_na2as asserts a named assertion as an implication from its name.
672
825
  const assertInKernel = (formula: number, name: number) => {
673
826
  const manager = state.manager;
827
+ useSatSolver((solver) =>
828
+ name === -1 ? solver.assertExpr(formula) : solver.assertNamed(formula, name),
829
+ );
830
+ state.fdSolver?.assertExpr(formula, name);
831
+ const kernel = state.kernel;
832
+ if (kernel === undefined) return;
674
833
  const asserted = name === -1 ? formula : manager.mkApp(OP_IMPLIES, [name, formula], SORT_BOOL);
675
834
  manager.incRef(asserted);
676
- state.kernel?.assertExpr(asserted);
677
- useEufSolver((solver) => solver.assertExpr(asserted));
835
+ kernel.assertExpr(asserted);
678
836
  manager.decRef(asserted);
679
837
  };
680
- const checkKernel = (assumptions: number[]) => {
838
+ const checkKernel = (names: number[], extra: number[]) => {
681
839
  const kernel = state.kernel;
682
- const found = useEufSolver((solver) => {
683
- const result = solver.check(assumptions);
840
+ const assumptions = [...names, ...extra];
841
+ const found = useSatSolver((solver) => {
842
+ const result = solver.check(extra);
684
843
  return {
685
844
  result,
686
845
  context: undefined,
@@ -708,7 +867,9 @@ export const runSmtlibScript = (
708
867
  ...solved,
709
868
  model: undefined,
710
869
  core: undefined,
711
- reasonUnknown: solved.context.getLastFailureMessage(),
870
+ // inc_sat_solver, which these logics use, keeps "no reason given" after an answer.
871
+ reasonUnknown:
872
+ solved.result === L_UNDEF ? solved.context.getLastFailureMessage() : "no reason given",
712
873
  };
713
874
  };
714
875
  const printDiagnostic = (line: string) => print(line, !settings.isDiagnosticOnStdout);
@@ -742,11 +903,10 @@ export const runSmtlibScript = (
742
903
  );
743
904
  return state.datalog;
744
905
  };
745
- const getTactic = (sexpr: SExpr, end: SmtList["end"]) =>
746
- compileTactic(getTacticEnvironment(), sexpr, end);
747
906
  const getGoal = (isTrackingDependencies = false) => {
748
907
  const goal = new Goal(state.manager);
749
908
  goal.isProofsEnabled = settings.isProducingProofs || state.isProofsEnabled === true;
909
+ goal.isModelsEnabled = settings.isProducingModels;
750
910
  goal.isCoresEnabled = settings.isProducingUnsatCores;
751
911
  for (const [index, assertion] of state.assertions.entries())
752
912
  goal.assert(
@@ -757,22 +917,43 @@ export const runSmtlibScript = (
757
917
  );
758
918
  return goal;
759
919
  };
920
+ const getCommandGoal = (isTrackingDependencies: boolean, end: SmtToken) => {
921
+ if (
922
+ settings.isProducingUnsatCores &&
923
+ (settings.isProducingProofs || state.isProofsEnabled === true)
924
+ )
925
+ throw new SmtParseError(
926
+ "Frontend does not support simultaneous generation of proofs and unsat cores",
927
+ end,
928
+ );
929
+ return getGoal(isTrackingDependencies);
930
+ };
760
931
  const hasNames = () => state.names.some((name) => name !== -1);
761
932
  // parse_check_sat shrinks the expression stack once the check has run.
762
- const checkSatAssuming = (items: SmtList["items"]) => {
933
+ const checkSatAssuming = (items: SmtList["items"], itemsEnd: SmtToken, end: SmtToken) => {
763
934
  const literals: number[] = [];
764
935
  try {
765
- for (const item of items) literals.push(state.reader.parseFormula(item));
766
- checkSat(literals);
936
+ for (const [index, item] of items.entries())
937
+ literals.push(
938
+ state.reader.parseFormula(
939
+ item,
940
+ items[index + 1] ?? itemsEnd,
941
+ "invalid check-sat command, argument must be a Boolean literal",
942
+ ),
943
+ );
944
+ checkSat(literals, end);
767
945
  } finally {
768
946
  for (const literal of literals) state.manager.decRef(literal);
769
947
  }
770
948
  };
771
949
  const addObjective = (list: SmtList, isMax: boolean) => {
772
- const term = state.reader.parseTerm(list.items[1] ?? list.end);
950
+ const term = state.reader.parseTerm(getFirstArgument(list));
773
951
  const sort = state.manager.sorts[term];
774
952
  if (sort !== SORT_INT && sort !== SORT_REAL && !state.manager.isBvSort(sort))
775
- throw new SmtParseError("Objective must be bit-vector, integer or real", list.end);
953
+ throw new SmtParseError(
954
+ "Objective must be bit-vector, integer or real",
955
+ list.items[2] ?? list.end,
956
+ );
776
957
  resetOptimizer();
777
958
  state.manager.incRef(term);
778
959
  state.objectives.push({
@@ -782,6 +963,8 @@ export const runSmtlibScript = (
782
963
  weights: [],
783
964
  id: null,
784
965
  });
966
+ if (settings.isPrintingSuccess) print("success");
967
+ checkArgumentCount(list, 1);
785
968
  };
786
969
  const releaseObjectives = (size: number, softSize: number) => {
787
970
  for (const index of state.softTrail.splice(softSize).reverse()) {
@@ -850,20 +1033,25 @@ export const runSmtlibScript = (
850
1033
  if (name.kind === "list")
851
1034
  throw new SmtUnsupportedError("an indexed function declaration reference");
852
1035
  throw new SmtParseError(
853
- "invalid function declaration reference, symbol or '(' expected got query",
1036
+ `invalid function declaration reference, symbol or '(' expected got ${name.lastId}`,
854
1037
  name,
855
1038
  );
856
1039
  }
857
1040
  const domain = reference.items[1] ?? reference.end;
858
1041
  if (domain.kind !== "list")
859
1042
  throw new SmtParseError(
860
- `Invalid function name. Expecting sort list starting with '(' to disambiguate function name got ${domain.kind === "symbol" || domain.kind === "keyword" ? domain.text : name.text}`,
1043
+ `Invalid function name. Expecting sort list starting with '(' to disambiguate function name got ${domain.lastId}`,
861
1044
  domain,
862
1045
  );
863
- const sorts = domain.items.map((sort) => state.reader.parseSort(sort));
1046
+ const sorts = domain.items.map((sort) =>
1047
+ state.reader.parseSort(
1048
+ sort,
1049
+ "Invalid function name. Expecting sort list starting with '(' to disambiguate function name",
1050
+ ),
1051
+ );
864
1052
  if (reference.items[2] === undefined)
865
1053
  throw new SmtParseError("Invalid function name invalid sort, unexpected ')'", reference.end);
866
- const range = state.reader.parseSort(reference.items[2]);
1054
+ const range = state.reader.parseSort(reference.items[2], "Invalid function name");
867
1055
  const follower = reference.items[3] ?? reference.end;
868
1056
  const found = state.reader.getFuncs(name.text).find((decl) => {
869
1057
  const funcDecl = state.manager.funcDecls[decl];
@@ -882,16 +1070,87 @@ export const runSmtlibScript = (
882
1070
  throw new SmtUnsupportedError("a function declaration reference with trailing terms");
883
1071
  return found;
884
1072
  };
1073
+ // get_info_cmd prints in set_next_arg, before the parser checks the rest of the command.
1074
+ const printInfo = (list: SmtList, keyword: string) => {
1075
+ if (keyword === ":reason-unknown") {
1076
+ const check = state.check;
1077
+ const reasonUnknown =
1078
+ check === undefined
1079
+ ? "state of the most recent check-sat command is not known"
1080
+ : check.reasonUnknown;
1081
+ if (reasonUnknown === undefined)
1082
+ throw new SmtUnsupportedError("the information :reason-unknown");
1083
+ print(`(:reason-unknown "${reasonUnknown.replaceAll('"', '\\"')}")`);
1084
+ return;
1085
+ }
1086
+ if (keyword === ":rlimit") {
1087
+ initManager();
1088
+ print(`(:rlimit ${state.manager.limit.count})`);
1089
+ return;
1090
+ }
1091
+ if (keyword === ":error-behavior") {
1092
+ print(
1093
+ `(:error-behavior ${settings.isExitingOnError ? "immediate-exit" : "continued-execution"})`,
1094
+ );
1095
+ return;
1096
+ }
1097
+ if (keyword === ":name") {
1098
+ print('(:name "Z3")');
1099
+ return;
1100
+ }
1101
+ if (keyword === ":authors") {
1102
+ print(
1103
+ '(:authors "Leonardo de Moura, Nikolaj Bjorner, Lev Nachmanson and Christoph Wintersteiger")',
1104
+ );
1105
+ return;
1106
+ }
1107
+ if (keyword === ":version") {
1108
+ print('(:version "5.3.2")');
1109
+ return;
1110
+ }
1111
+ if (keyword === ":status") {
1112
+ print(`(:status ${getVerdict(settings.status)})`);
1113
+ return;
1114
+ }
1115
+ if (keyword === ":assertion-stack-levels") {
1116
+ print(`(:assertion-stack-levels ${state.scopes.length})`);
1117
+ return;
1118
+ }
1119
+ if (keyword === ":all-statistics" || keyword === ":parameters")
1120
+ throw new SmtUnsupportedError(`the information ${keyword}`);
1121
+ if (keyword !== ":?") {
1122
+ print("unsupported");
1123
+ printDiagnostic(`; ${keyword} line: ${list.line} position: ${list.column}`);
1124
+ }
1125
+ print("; Suppported get-info parameters:");
1126
+ for (const supported of GET_INFO_KEYWORDS) print(`; (get-info ${supported})`);
1127
+ };
1128
+ const printCore = () => {
1129
+ const check = state.check;
1130
+ if (check?.core === undefined)
1131
+ throw new SmtUnsupportedError("the unsat core of a tactic that drops dependencies");
1132
+ const kernel = state.kernel;
1133
+ if (kernel === undefined || check.context !== kernel.context) {
1134
+ print(`(${check.core.map(format).join(" ")})`);
1135
+ return;
1136
+ }
1137
+ state.manager.limit.restart(Date.now() + options.timeLimitMs);
1138
+ const core = new RefVector(state.manager);
1139
+ kernel.getUnsatCore(core);
1140
+ print(`(${core.items.map(format).join(" ")})`);
1141
+ core.reset();
1142
+ };
885
1143
  const runCommand = new Map<string, (list: SmtList) => void>([
886
1144
  [
887
1145
  "assert",
888
1146
  (list) => {
889
1147
  const term = list.items[1] ?? list.end;
890
- const assertion = state.reader.parseAssertion(term);
1148
+ const follower = list.items[2] ?? list.end;
1149
+ const assertion = state.reader.parseAssertion(term, follower);
891
1150
  const logicError = state.reader.getLogicError(assertion.formula);
892
1151
  if (logicError !== undefined) {
893
1152
  state.manager.decRef(assertion.formula);
894
- throw new SmtParseError(logicError, list.end);
1153
+ throw new SmtParseError(logicError, follower);
895
1154
  }
896
1155
  state.assertions.push(assertion.formula);
897
1156
  state.assertionTexts.push(
@@ -912,9 +1171,17 @@ export const runSmtlibScript = (
912
1171
  }
913
1172
  assertInKernel(assertion.formula, state.names[state.names.length - 1]);
914
1173
  clearCheck();
1174
+ if (follower !== list.end)
1175
+ throw new SmtParseError("invalid assert command, ')' expected", follower);
1176
+ },
1177
+ ],
1178
+ [
1179
+ "check-sat",
1180
+ (list) => {
1181
+ checkSatAssuming(list.items.slice(1), list.end, list.end);
1182
+ validateCheck(list.end);
915
1183
  },
916
1184
  ],
917
- ["check-sat", (list) => checkSatAssuming(list.items.slice(1))],
918
1185
  [
919
1186
  "declare-rel",
920
1187
  (list) => {
@@ -924,7 +1191,9 @@ export const runSmtlibScript = (
924
1191
  throw new SmtParseError("at least 2 arguments expected", list.end);
925
1192
  const decl = state.manager.mkFuncDecl(
926
1193
  name.text,
927
- domain.items.map((sort) => state.reader.parseSort(sort)),
1194
+ domain.items.map((sort) =>
1195
+ state.reader.parseSort(sort, "expecting sort list starting with '('"),
1196
+ ),
928
1197
  SORT_BOOL,
929
1198
  );
930
1199
  state.reader.insertFunc(decl, list.end);
@@ -942,7 +1211,7 @@ export const runSmtlibScript = (
942
1211
  list.end,
943
1212
  );
944
1213
  const manager = state.manager;
945
- const range = state.reader.parseSort(sort);
1214
+ const range = state.reader.parseSort(sort, "invalid command argument, sort expected");
946
1215
  const decl = manager.mkFuncDecl(name.text, [], range);
947
1216
  state.reader.insertFunc(decl, list.end);
948
1217
  getDatalog().registerVariable(manager.mkApp(decl, [], range));
@@ -954,7 +1223,10 @@ export const runSmtlibScript = (
954
1223
  const rule = list.items[1];
955
1224
  if (rule === undefined) throw new SmtParseError("invalid rule, expected formula", list.end);
956
1225
  const name = list.items[2];
957
- getDatalog().addRule(state.reader.parseFormula(rule), isSymbol(name) ? name.text : "");
1226
+ getDatalog().addRule(
1227
+ state.reader.parseFormula(rule, rule),
1228
+ isSymbol(name) ? name.text : undefined,
1229
+ );
958
1230
  },
959
1231
  ],
960
1232
  [
@@ -995,38 +1267,48 @@ export const runSmtlibScript = (
995
1267
  const params = getCommandParams(list.items, CHECK_SAT_USING_PARAMETERS);
996
1268
  if (list.items[1] === undefined)
997
1269
  throw new SmtParseError("check-sat-using needs a tactic argument", list.end);
998
- const builder = getTactic(list.items[1], list.end);
1270
+ expectClosed(list.items[1]);
1271
+ const builder = compileTactic(getTacticEnvironment(), list.items[1]);
999
1272
  const tactic = builder.build(params);
1273
+ const goal = getCommandGoal(builder.isTrackingDependencies, list.end);
1000
1274
  clearCheck();
1001
1275
  const deadline = Date.now() + options.timeLimitMs;
1002
1276
  state.manager.limit.restart(deadline);
1003
1277
  state.manager.limit.push(settings.rlimit);
1004
1278
  try {
1005
- const checked = checkSatUsing(tactic, getGoal(builder.isTrackingDependencies));
1279
+ const checked = checkSatUsing(tactic, goal);
1280
+ const reasonUnknown = checked.reasonUnknown ?? getCancelMessage(deadline);
1006
1281
  setCheck(checked.status, {
1282
+ isFromTactic: true,
1007
1283
  model: checked.model,
1008
1284
  core:
1009
1285
  builder.isTrackingDependencies || !hasNames()
1010
1286
  ? getDependencyValues(checked.core)
1011
1287
  : undefined,
1012
- reasonUnknown: checked.reasonUnknown ?? getCancelMessage(deadline),
1288
+ reasonUnknown,
1013
1289
  });
1014
- print(getVerdict(checked.status));
1290
+ print(
1291
+ checked.isConversionCanceled
1292
+ ? `(error "tactic failed: ${reasonUnknown}")`
1293
+ : getVerdict(checked.status),
1294
+ );
1015
1295
  } finally {
1016
1296
  state.manager.limit.pop();
1017
1297
  }
1298
+ validateCheck(list.end);
1018
1299
  },
1019
1300
  ],
1020
1301
  [
1021
1302
  "check-sat-assuming",
1022
1303
  (list) => {
1023
- const literals = list.items[1];
1024
- if (literals?.kind !== "list")
1304
+ const literals = list.items[1] ?? list.end;
1305
+ if (literals.kind !== "list")
1025
1306
  throw new SmtParseError(
1026
- "invalid check-sat-assuming command, '(' expected",
1027
- literals ?? list.end,
1307
+ `invalid check-sat-assuming command, '(', expected got ${literals.lastId}`,
1308
+ literals,
1028
1309
  );
1029
- checkSatAssuming(literals.items);
1310
+ checkSatAssuming(literals.items, literals.end, list.end);
1311
+ validateCheck(list.end);
1030
1312
  },
1031
1313
  ],
1032
1314
  [
@@ -1037,7 +1319,10 @@ export const runSmtlibScript = (
1037
1319
  if (terms === undefined)
1038
1320
  throw new SmtParseError("invalid command, argument(s) missing", list.end);
1039
1321
  if (terms.kind !== "list")
1040
- throw new SmtParseError("invalid list of terms, '(' expected", terms);
1322
+ throw new SmtParseError(
1323
+ `invalid list of terms, '(' expected got ${terms.lastId}`,
1324
+ terms,
1325
+ );
1041
1326
  return terms.items.map((item) => state.reader.parseTerm(item));
1042
1327
  });
1043
1328
  if (list.items.length > 3)
@@ -1048,20 +1333,14 @@ export const runSmtlibScript = (
1048
1333
  manager.limit.push(settings.rlimit);
1049
1334
  const names = state.names.filter((name) => name !== -1);
1050
1335
  try {
1051
- if (isUsingFdSolver()) {
1052
- if (names.length > 0)
1053
- throw new SmtUnsupportedError("the named assertions of the finite-domain solver");
1054
- const found = getFdConsequences(
1055
- manager,
1056
- new Map(),
1057
- state.assertions,
1058
- assumptions,
1059
- vars,
1060
- );
1336
+ const fdSolver = state.fdSolver;
1337
+ if (fdSolver !== undefined) {
1338
+ const found = fdSolver.getConsequences(assumptions, vars);
1061
1339
  setCheck(found.result, {
1062
- model: found.model,
1063
- core: found.core,
1064
- reasonUnknown: found.reasonUnknown,
1340
+ isFromTactic: true,
1341
+ fdSolver,
1342
+ core: [...fdSolver.getUnsatCore()],
1343
+ reasonUnknown: fdSolver.reasonUnknown,
1065
1344
  });
1066
1345
  print(getVerdict(found.result));
1067
1346
  print("");
@@ -1132,39 +1411,60 @@ export const runSmtlibScript = (
1132
1411
  assertions: state.assertions.length,
1133
1412
  objectives: state.objectives.length,
1134
1413
  softTrail: state.softTrail.length,
1414
+ rlimit: settings.rlimit,
1135
1415
  });
1136
1416
  state.reader.push();
1137
1417
  state.isIncremental = true;
1138
- state.kernel?.push();
1139
- useEufSolver((solver) => solver.push());
1418
+ const limit = state.manager.limit;
1419
+ limit.push(settings.rlimit);
1420
+ try {
1421
+ state.kernel?.push();
1422
+ useSatSolver((solver) => solver.push());
1423
+ state.fdSolver?.push();
1424
+ } catch (error) {
1425
+ if (!(error instanceof SmtCanceledError)) throw error;
1426
+ throw new SmtParseError(limit.getCancelMessage(), list.end);
1427
+ } finally {
1428
+ limit.pop();
1429
+ }
1140
1430
  }
1431
+ if (list.items[2] !== undefined)
1432
+ throw new SmtParseError("invalid push command, ')' expected", list.items[2]);
1141
1433
  },
1142
1434
  ],
1143
1435
  [
1144
1436
  "pop",
1145
1437
  (list) => {
1146
1438
  const count = getScopeCount(list);
1439
+ const follower = list.items[2] ?? list.end;
1440
+ clearCheck();
1147
1441
  if (count > state.scopes.length)
1148
1442
  throw new SmtParseError(
1149
1443
  "invalid pop command, argument is greater than the current stack depth",
1150
- list.end,
1444
+ follower,
1151
1445
  );
1152
- clearCheck();
1153
- if (count === 0) return;
1154
- state.isIncremental = true;
1155
- const scope = state.scopes[state.scopes.length - count];
1156
- state.kernel?.pop(count);
1157
- useEufSolver((solver) => solver.pop(count));
1158
- for (let index = 0; index < count; index++) state.reader.pop();
1159
- restoreAssertions(scope.assertions);
1160
- resetOptimizer();
1161
- releaseObjectives(scope.objectives, scope.softTrail);
1162
- state.scopes.length -= count;
1446
+ if (count > 0) {
1447
+ state.isIncremental = true;
1448
+ const scope = state.scopes[state.scopes.length - count];
1449
+ state.kernel?.pop(count);
1450
+ useSatSolver((solver) => solver.pop(count));
1451
+ state.fdSolver?.pop(count);
1452
+ state.reader.pop(count);
1453
+ restoreAssertions(scope.assertions);
1454
+ settings.rlimit = scope.rlimit;
1455
+ resetOptimizer();
1456
+ releaseObjectives(scope.objectives, scope.softTrail);
1457
+ state.scopes.length -= count;
1458
+ }
1459
+ if (follower !== list.end)
1460
+ throw new SmtParseError("invalid pop command, ')' expected", follower);
1163
1461
  },
1164
1462
  ],
1165
1463
  [
1166
1464
  "reset",
1167
- () => {
1465
+ (list) => {
1466
+ if (list.items[1] !== undefined)
1467
+ throw new SmtParseError("invalid reset command, ')' expected", list.items[1]);
1168
1468
  clearCheck();
1169
1469
  resetOptimizer();
1170
1470
  state = createState();
@@ -1172,12 +1472,17 @@ export const runSmtlibScript = (
1172
1472
  ],
1173
1473
  [
1174
1474
  "reset-assertions",
1175
- () => {
1176
- clearCheck();
1475
+ (list) => {
1476
+ checkArgumentCount(list, 0);
1177
1477
  restoreAssertions(0);
1178
1478
  resetOptimizer();
1179
1479
  releaseObjectives(0, 0);
1180
- state.scopes = state.scopes.map(() => ({ assertions: 0, objectives: 0, softTrail: 0 }));
1480
+ state.scopes = state.scopes.map((scope) => ({
1481
+ ...scope,
1482
+ assertions: 0,
1483
+ objectives: 0,
1484
+ softTrail: 0,
1485
+ }));
1181
1486
  if (state.isProofsEnabled !== undefined) resetKernel();
1182
1487
  state.isIncremental = state.scopes.length > 0;
1183
1488
  state.hasChecked = false;
@@ -1195,7 +1500,17 @@ export const runSmtlibScript = (
1195
1500
  manager,
1196
1501
  getRewriterOptions(manager, getSimplifyParams(list.items.slice(2))),
1197
1502
  );
1198
- const simplified = rewriter.rewrite(term);
1503
+ // simplify_cmd prints a failure of the rewriter and then the term as it was.
1504
+ const simplified = (() => {
1505
+ try {
1506
+ return rewriter.rewrite(term);
1507
+ } catch (error) {
1508
+ if (!(error instanceof SmtDefaultError)) throw error;
1509
+ print(`(error "simplifier failed: ${error.message}")`);
1510
+ manager.incRef(term);
1511
+ return term;
1512
+ }
1513
+ })();
1199
1514
  rewriter.cleanup();
1200
1515
  display(simplified);
1201
1516
  rewriter.release();
@@ -1214,8 +1529,7 @@ export const runSmtlibScript = (
1214
1529
  manager.incRef(term);
1215
1530
  const visited = new Set<number>();
1216
1531
  const todo = [term];
1217
- while (todo.length > 0) {
1218
- const expr = todo.pop() as number;
1532
+ for (let expr = todo.pop(); expr !== undefined; expr = todo.pop()) {
1219
1533
  if (visited.has(expr)) continue;
1220
1534
  visited.add(expr);
1221
1535
  const family = manager.kinds[expr] === KIND_APP ? manager.getFamily(expr) : -1;
@@ -1239,13 +1553,19 @@ export const runSmtlibScript = (
1239
1553
  const params = getCommandParams(list.items, APPLY_PARAMETERS);
1240
1554
  if (list.items[1] === undefined)
1241
1555
  throw new SmtParseError("apply needs a tactic argument", list.end);
1242
- const builder = getTactic(list.items[1], list.end);
1556
+ expectClosed(list.items[1]);
1557
+ // apply_tactic_cmd, unlike check-sat-using, gives the tactic no logic.
1558
+ const builder = compileTactic(
1559
+ { ...getTacticEnvironment(), setup: { ...getSetup(), logic: undefined } },
1560
+ list.items[1],
1561
+ );
1243
1562
  const tactic = builder.build(params);
1563
+ const input = getCommandGoal(builder.isTrackingDependencies, list.end);
1244
1564
  const isPrintingDependencies = params.get("print_dependencies") === true;
1245
1565
  if (isPrintingDependencies && !builder.isTrackingDependencies && hasNames())
1246
1566
  throw new SmtUnsupportedError("the dependencies of a tactic that drops dependencies");
1247
1567
  state.manager.limit.restart(Date.now() + options.timeLimitMs);
1248
- const input = getGoal(builder.isTrackingDependencies);
1568
+ state.manager.limit.push(settings.rlimit);
1249
1569
  const goals = (() => {
1250
1570
  try {
1251
1571
  return applyTactic(tactic, input);
@@ -1253,6 +1573,8 @@ export const runSmtlibScript = (
1253
1573
  if (!(error instanceof SmtTacticError)) throw error;
1254
1574
  print(`(error "tactic failed: ${error.message}")`);
1255
1575
  return undefined;
1576
+ } finally {
1577
+ state.manager.limit.pop();
1256
1578
  }
1257
1579
  })();
1258
1580
  // apply_tactic_cmd releases the result goals and then the input goal.
@@ -1310,8 +1632,7 @@ export const runSmtlibScript = (
1310
1632
  const decls = new Set<number>();
1311
1633
  for (const assertion of assertions) {
1312
1634
  const todo = [assertion];
1313
- while (todo.length > 0) {
1314
- const expr = todo.pop() as number;
1635
+ for (let expr = todo.pop(); expr !== undefined; expr = todo.pop()) {
1315
1636
  if (visited.has(expr)) continue;
1316
1637
  visited.add(expr);
1317
1638
  if (manager.kinds[expr] === KIND_QUANTIFIER) {
@@ -1352,6 +1673,7 @@ export const runSmtlibScript = (
1352
1673
  [
1353
1674
  "get-assertions",
1354
1675
  (list) => {
1676
+ checkArgumentCount(list, 0);
1355
1677
  if (!settings.isInteractive)
1356
1678
  throw new SmtParseError(
1357
1679
  "command is only available in interactive mode, use command (set-option :interactive-mode true)",
@@ -1363,17 +1685,29 @@ export const runSmtlibScript = (
1363
1685
  [
1364
1686
  "get-unsat-core",
1365
1687
  (list) => {
1688
+ checkArgumentCount(list, 0);
1366
1689
  if (!settings.isProducingUnsatCores)
1367
1690
  throw new SmtParseError(
1368
1691
  "unsat core construction is not enabled, use command (set-option :produce-unsat-cores true)",
1369
1692
  list.end,
1370
1693
  );
1371
- const check = state.check;
1372
- if (check === undefined || check.status !== L_FALSE)
1694
+ if (state.check?.status !== L_FALSE)
1373
1695
  throw new SmtParseError("unsat core is not available", list.end);
1374
- if (check.core === undefined)
1375
- throw new SmtUnsupportedError("the unsat core of a tactic that drops dependencies");
1376
- print(`(${check.core.map(format).join(" ")})`);
1696
+ printCore();
1697
+ },
1698
+ ],
1699
+ [
1700
+ "get-unsat-assumptions",
1701
+ (list) => {
1702
+ checkArgumentCount(list, 0);
1703
+ if (!settings.isProducingUnsatAssumptions)
1704
+ throw new SmtParseError(
1705
+ "unsat assumptions construction is not enabled, use command (set-option :produce-unsat-assumptions true)",
1706
+ list.end,
1707
+ );
1708
+ if (state.check?.status !== L_FALSE)
1709
+ throw new SmtParseError("unsat assumptions is not available", list.end);
1710
+ printCore();
1377
1711
  },
1378
1712
  ],
1379
1713
  [
@@ -1381,13 +1715,19 @@ export const runSmtlibScript = (
1381
1715
  (list) => {
1382
1716
  const target = list.items[1];
1383
1717
  const term = target === undefined ? -1 : state.reader.parseTerm(target);
1384
- if (list.items.length > 2) throw new SmtUnsupportedError("the parameters of eval");
1718
+ const parameter = list.items[2];
1719
+ if (parameter !== undefined && parameter.kind !== "keyword")
1720
+ throw new SmtParseError("invalid command argument, keyword expected", parameter);
1721
+ if (parameter !== undefined) throw new SmtUnsupportedError("the parameters of eval");
1385
1722
  const model = getModel(list);
1386
1723
  if (term === -1) throw new SmtParseError("no arguments passed to eval", list.end);
1387
1724
  const manager = state.manager;
1388
1725
  manager.incRef(term);
1389
- const value = model.eval(term, false);
1726
+ const evaluator = new ModelEvaluator(manager, model);
1727
+ const value = evaluator.rewrite(term);
1390
1728
  display(value);
1729
+ // eval_cmd frees its model_evaluator before the value, and the parser frees the term last.
1730
+ evaluator.release();
1391
1731
  manager.decRef(value);
1392
1732
  manager.decRef(term);
1393
1733
  },
@@ -1395,30 +1735,57 @@ export const runSmtlibScript = (
1395
1735
  [
1396
1736
  "get-value",
1397
1737
  (list) => {
1398
- const terms = list.items[1];
1399
- if (terms?.kind !== "list")
1400
- throw new SmtParseError("invalid get-value command, '(' expected", terms ?? list.end);
1738
+ const terms = list.items[1] ?? list.end;
1739
+ if (terms.kind !== "list")
1740
+ throw new SmtParseError(
1741
+ `invalid get-value command, '(' expected got ${terms.lastId}`,
1742
+ terms,
1743
+ );
1401
1744
  const manager = state.manager;
1402
1745
  const exprs = terms.items.map((item) => state.reader.parseTerm(item));
1403
1746
  if (exprs.length === 0)
1404
1747
  throw new SmtParseError("invalid get-value command, empty list of terms", terms.end);
1405
- if (list.items.length > 2) throw new SmtUnsupportedError("the parameters of get-value");
1748
+ const extra = list.items[2];
1749
+ if (
1750
+ extra?.kind === "keyword" &&
1751
+ (extra.text === ":model-index" || extra.text === ":model_index")
1752
+ )
1753
+ throw new SmtUnsupportedError("the model index of get-value");
1754
+ if (extra !== undefined)
1755
+ throw new SmtParseError("invalid get-value command, ')' expected", extra);
1406
1756
  const model = getModel(list);
1757
+ pendingText += "(";
1407
1758
  model.compact();
1408
1759
  // Z3 prints each term as the script spells it, from the end of the term before.
1409
1760
  const offsets = [
1410
1761
  terms.offset + 1,
1411
1762
  ...terms.items.map((item) => (item.kind === "list" ? item.end : item).endOffset),
1412
1763
  ];
1413
- const lines = exprs.map((expr, index) => {
1764
+ exprs.forEach((expr, index) => {
1414
1765
  manager.incRef(expr);
1415
1766
  const value = model.eval(expr, true);
1416
- const line = `(${text.slice(offsets[index], offsets[index + 1]).trim()} ${format(value)})`;
1767
+ pendingText += `${index > 0 ? "\n " : ""}(${text.slice(offsets[index], offsets[index + 1]).trim()} ${format(value)})`;
1417
1768
  manager.decRef(value);
1418
1769
  manager.decRef(expr);
1419
- return line;
1420
1770
  });
1421
- print(`(${lines.join("\n ")})`);
1771
+ print(")");
1772
+ },
1773
+ ],
1774
+ [
1775
+ "get-assignment",
1776
+ (list) => {
1777
+ checkArgumentCount(list, 0);
1778
+ const manager = state.manager;
1779
+ const model = getModel(list);
1780
+ const assignments = state.reader.getConstantMacros().flatMap((macro) => {
1781
+ if (manager.sorts[macro.body] !== SORT_BOOL) return [];
1782
+ const value = model.eval(macro.body, true);
1783
+ const assignment = `(${getQuotedName(macro.name)} ${manager.isTrue(value)})`;
1784
+ const isDecided = manager.isTrue(value) || manager.isFalse(value);
1785
+ manager.decRef(value);
1786
+ return isDecided ? [assignment] : [];
1787
+ });
1788
+ print(`(${assignments.join(" ")})`);
1422
1789
  },
1423
1790
  ],
1424
1791
  [
@@ -1433,14 +1800,27 @@ export const runSmtlibScript = (
1433
1800
  [
1434
1801
  "get-model",
1435
1802
  (list) => {
1436
- if (list.items.length > 1) throw new SmtUnsupportedError("the index of get-model");
1803
+ for (const index of list.items.slice(1)) {
1804
+ if (index.kind !== "numeral")
1805
+ throw new SmtParseError("invalid command argument, unsigned integer expected", index);
1806
+ if (!isUnsigned(index.text))
1807
+ throw new SmtParseError(
1808
+ "invalid indexed identifier, index is too big to fit in an unsigned machine integer",
1809
+ index,
1810
+ );
1811
+ }
1437
1812
  const manager = state.manager;
1438
1813
  const model = getModel(list);
1814
+ const last = list.items[list.items.length - 1];
1815
+ // get_model_cmd takes any number of indices and keeps the last.
1816
+ if (last.kind === "numeral" && BigInt(last.text) > 0n)
1817
+ throw new SmtUnsupportedError("the index of get-model");
1439
1818
  model.compact();
1440
1819
  for (const decl of state.reader.getDeclaredFuncs()) {
1441
1820
  const funcDecl = manager.funcDecls[decl];
1442
1821
  if (
1443
1822
  funcDecl.family !== FAMILY_USER ||
1823
+ manager.macros.has(decl) ||
1444
1824
  model.constInterps.has(decl) ||
1445
1825
  model.funcInterps.has(decl)
1446
1826
  )
@@ -1458,7 +1838,13 @@ export const runSmtlibScript = (
1458
1838
  print(`(\n${printModel(manager, getPrintEnvironment(), model, 2)})`);
1459
1839
  },
1460
1840
  ],
1461
- ["display", (list) => display(state.reader.parseTerm(list.items[1] ?? list.end))],
1841
+ [
1842
+ "display",
1843
+ (list) => {
1844
+ display(state.reader.parseTerm(getFirstArgument(list)));
1845
+ checkArgumentCount(list, 1);
1846
+ },
1847
+ ],
1462
1848
  [
1463
1849
  "dbg-lt",
1464
1850
  (list) => {
@@ -1471,7 +1857,13 @@ export const runSmtlibScript = (
1471
1857
  "dbg-some-value",
1472
1858
  (list) => {
1473
1859
  display(
1474
- getManagerSomeValue(state.manager, state.reader.parseSort(list.items[1] ?? list.end)),
1860
+ getManagerSomeValue(
1861
+ state.manager,
1862
+ state.reader.parseSort(
1863
+ list.items[1] ?? list.end,
1864
+ "invalid command argument, sort expected",
1865
+ ),
1866
+ ),
1475
1867
  );
1476
1868
  },
1477
1869
  ],
@@ -1507,10 +1899,11 @@ export const runSmtlibScript = (
1507
1899
  [
1508
1900
  "echo",
1509
1901
  (list) => {
1510
- const message = list.items[1];
1511
- if (message?.kind !== "string")
1512
- throw new SmtParseError("invalid echo command, string expected", message ?? list.end);
1902
+ const message = getFirstArgument(list);
1903
+ if (message.kind !== "string")
1904
+ throw new SmtParseError("invalid command argument, string expected", message);
1513
1905
  print(message.text);
1906
+ checkArgumentCount(list, 1);
1514
1907
  },
1515
1908
  ],
1516
1909
  [
@@ -1521,8 +1914,19 @@ export const runSmtlibScript = (
1521
1914
  const optionName = `:${getParameterName(keyword)}`;
1522
1915
  const value = list.items[2];
1523
1916
  const isTrue = isSymbol(value, "true");
1917
+ if (
1918
+ (keyword === ":produce-proofs" ||
1919
+ keyword === ":produce-unsat-cores" ||
1920
+ keyword === ":produce-unsat-assumptions") &&
1921
+ state.assertions.length > 0
1922
+ )
1923
+ throw new SmtParseError(
1924
+ `error setting '${keyword}', option value cannot be modified after assertions have been added`,
1925
+ value ?? list.end,
1926
+ );
1524
1927
  if (keyword === ":produce-models") settings.isProducingModels = isTrue;
1525
1928
  if (keyword === ":produce-unsat-cores") settings.isProducingUnsatCores = isTrue;
1929
+ if (keyword === ":produce-unsat-assumptions") settings.isProducingUnsatAssumptions = isTrue;
1526
1930
  if (keyword === ":produce-proofs") settings.isProducingProofs = isTrue;
1527
1931
  if (keyword === ":interactive-mode" || keyword === ":produce-assertions") {
1528
1932
  if (state.isProofsEnabled !== undefined)
@@ -1599,6 +2003,7 @@ export const runSmtlibScript = (
1599
2003
  [":print-success", settings.isPrintingSuccess],
1600
2004
  [":produce-models", settings.isProducingModels],
1601
2005
  [":produce-unsat-cores", settings.isProducingUnsatCores],
2006
+ [":produce-unsat-assumptions", settings.isProducingUnsatAssumptions],
1602
2007
  [":interactive-mode", settings.isInteractive],
1603
2008
  [":produce-assertions", settings.isInteractive],
1604
2009
  [":int-real-coercions", true],
@@ -1618,34 +2023,20 @@ export const runSmtlibScript = (
1618
2023
  "get-info",
1619
2024
  (list) => {
1620
2025
  const info = list.items[1];
1621
- if (info?.kind !== "keyword")
1622
- throw new SmtParseError("invalid command argument, keyword expected", info ?? list.end);
1623
- const check = state.check;
1624
- const reasonUnknown =
1625
- check === undefined
1626
- ? "state of the most recent check-sat command is not known"
1627
- : check.reasonUnknown;
1628
- if (info.text === ":reason-unknown" && reasonUnknown !== undefined) {
1629
- print(`(:reason-unknown "${reasonUnknown.replaceAll('"', '\\"')}")`);
1630
- return;
1631
- }
1632
- if (info.text === ":rlimit") {
1633
- print(`(:rlimit ${state.manager.limit.count})`);
1634
- return;
1635
- }
1636
- if (info.text !== ":error-behavior")
1637
- throw new SmtUnsupportedError(`the information ${info.text}`);
1638
- print(
1639
- `(:error-behavior ${settings.isExitingOnError ? "immediate-exit" : "continued-execution"})`,
1640
- );
2026
+ if (info === undefined)
2027
+ throw new SmtParseError("invalid command, argument(s) missing", list.end);
2028
+ if (info.kind !== "keyword")
2029
+ throw new SmtParseError("invalid command argument, keyword expected", info);
2030
+ printInfo(list, info.text);
2031
+ checkArgumentCount(list, 1);
1641
2032
  },
1642
2033
  ],
1643
2034
  [
1644
2035
  "set-logic",
1645
2036
  (list) => {
1646
- const logic = list.items[1];
2037
+ const logic = getFirstArgument(list);
1647
2038
  if (!isSymbol(logic))
1648
- throw new SmtParseError("invalid logic name, symbol expected", logic ?? list.end);
2039
+ throw new SmtParseError("invalid command argument, symbol expected", logic);
1649
2040
  if (state.logic !== undefined)
1650
2041
  throw new SmtParseError("the logic has already been set", logic);
1651
2042
  if (state.assertions.length > 0)
@@ -1655,6 +2046,7 @@ export const runSmtlibScript = (
1655
2046
  printDiagnostic(
1656
2047
  `; ignoring unsupported logic ${logic.text} line: ${list.line} position: ${list.column}`,
1657
2048
  );
2049
+ checkArgumentCount(list, 1);
1658
2050
  return;
1659
2051
  }
1660
2052
  state.logic = logic.text;
@@ -1666,9 +2058,33 @@ export const runSmtlibScript = (
1666
2058
  resetKernel();
1667
2059
  }
1668
2060
  if (settings.isPrintingSuccess) print("success");
2061
+ checkArgumentCount(list, 1);
2062
+ },
2063
+ ],
2064
+ [
2065
+ "set-info",
2066
+ (list) => {
2067
+ const info = list.items[1];
2068
+ if (info === undefined)
2069
+ throw new SmtParseError("invalid command, argument(s) missing", list.end);
2070
+ if (info.kind !== "keyword")
2071
+ throw new SmtParseError("invalid command argument, keyword expected", info);
2072
+ const value = list.items[2];
2073
+ if (value === undefined)
2074
+ throw new SmtParseError("invalid command, argument(s) missing", list.end);
2075
+ if (value.kind === "list" || value.kind === "keyword" || value.kind === "eof")
2076
+ throw new SmtParseError("invalid option value", value);
2077
+ if (info.text === ":status" && value.kind === "symbol") {
2078
+ const status = [L_TRUE, L_FALSE, L_UNDEF].find(
2079
+ (innerStatus) => getVerdict(innerStatus) === value.text,
2080
+ );
2081
+ if (status === undefined) throw new SmtParseError("invalid ':status' attribute", value);
2082
+ settings.status = status;
2083
+ }
2084
+ if (list.items[3] !== undefined)
2085
+ throw new SmtParseError("invalid command, too many arguments", list.items[3]);
1669
2086
  },
1670
2087
  ],
1671
- ["set-info", () => {}],
1672
2088
  ]);
1673
2089
  const run = (list: SmtList) => {
1674
2090
  const head = list.items[0];
@@ -1677,20 +2093,34 @@ export const runSmtlibScript = (
1677
2093
  const command = DECLARATIONS.has(head.text)
1678
2094
  ? (declaration: SmtList) => state.reader.declareCommand(declaration)
1679
2095
  : runCommand.get(head.text);
1680
- if (command === undefined) throw new SmtUnsupportedError(`the command ${head.text}`);
2096
+ if (command === undefined && UNPORTED_COMMANDS.has(head.text))
2097
+ throw new SmtUnsupportedError(`the command ${head.text}`);
2098
+ if (command === undefined) {
2099
+ expectClosed(list);
2100
+ print("unsupported");
2101
+ printDiagnostic(`; ${head.text} line: ${list.end.line} position: ${list.end.column}`);
2102
+ return;
2103
+ }
1681
2104
  // Z3's scoped_timer ends with the command that set it, so a command starts without one.
1682
2105
  state.manager.limit.restart(Infinity);
1683
- if (!MANAGERLESS_COMMANDS.has(head.text) && state.isProofsEnabled === undefined) {
1684
- state.isProofsEnabled = settings.parameters.get("proof") === "true";
1685
- resetKernel();
2106
+ if (!MANAGERLESS_COMMANDS.has(head.text)) initManager();
2107
+ try {
2108
+ command(list);
2109
+ } catch (error) {
2110
+ if (!(error instanceof SmtDefaultError)) throw error;
2111
+ throw new SmtParseError(error.message, list.end);
1686
2112
  }
1687
- command(list);
1688
2113
  if (settings.isPrintingSuccess && SILENT_COMMANDS.has(head.text)) print("success");
1689
2114
  };
1690
2115
  for (;;) {
1691
2116
  try {
1692
- const list = commands.read();
1693
- if (list === undefined || isSymbol(list.items[0], "exit")) break;
2117
+ const list = commands.readCommand();
2118
+ if (list === undefined) break;
2119
+ if (isSymbol(list.items[0], "exit")) {
2120
+ checkArgumentCount(list, 0);
2121
+ if (settings.isPrintingSuccess) print("success");
2122
+ break;
2123
+ }
1694
2124
  run(list);
1695
2125
  } catch (error) {
1696
2126
  if (!(error instanceof SmtParseError)) throw error;