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,4 +1,9 @@
1
- import { SmtParseError, SmtUnsupportedError } from "../../../errors";
1
+ import {
2
+ SmtDefaultError,
3
+ SmtParseError,
4
+ type SmtPosition,
5
+ SmtUnsupportedError,
6
+ } from "../../../errors";
2
7
  import {
3
8
  type AstManager,
4
9
  getSymbolHash,
@@ -27,10 +32,12 @@ import {
27
32
  OP_GT,
28
33
  OP_IDIV,
29
34
  OP_IMPLIES,
35
+ OP_INT2BV,
30
36
  OP_IS_INT,
31
37
  OP_ITE,
32
38
  OP_LE,
33
39
  OP_LT,
40
+ OP_MKBV,
34
41
  OP_MOD,
35
42
  OP_REM,
36
43
  OP_MUL,
@@ -72,7 +79,7 @@ import {
72
79
  QUANTIFIER_LAMBDA,
73
80
  } from "./ast";
74
81
  import { getArraySignature, getMapSignature, mkAsArray } from "./array-decl-plugin";
75
- import { BV_OP_NAMES, getBvSignature } from "./bv-decl-plugin";
82
+ import { BV_OP_NAMES, getBvSignature, getMismatchedArgument } from "./bv-decl-plugin";
76
83
  import {
77
84
  type ConstructorDef,
78
85
  type DatatypeDef,
@@ -88,6 +95,7 @@ import {
88
95
  mkDatatypes,
89
96
  mkUpdateField,
90
97
  removeDatatype,
98
+ resetDatatypePlugin,
91
99
  substituteSort,
92
100
  } from "./datatype-decl-plugin";
93
101
  import {
@@ -120,6 +128,7 @@ import {
120
128
  } from "./smt-logics";
121
129
  import { setRecFunDefinition } from "./recfun-decl-plugin";
122
130
  import { DEFAULT_PRINT_OPTIONS, printExpr } from "./smt2-printer";
131
+ import { InvVarShifter } from "./rewriter-tpl";
123
132
  import { replaceFreeVars, VarSubst } from "./var-subst";
124
133
  import { UpolynomialManager } from "./nlsat/upolynomial";
125
134
 
@@ -131,12 +140,13 @@ const DEFAULT_PARSER_OPTIONS: ParserOptions = { isIgnoringBadPatterns: true };
131
140
 
132
141
  // Z3 counts the columns of the first line from 1 and of the others from 0.
133
142
  export interface SmtToken {
134
- kind: "(" | ")" | "symbol" | "keyword" | "string" | "numeral" | "decimal" | "bitvector";
143
+ kind: "(" | ")" | "symbol" | "keyword" | "string" | "numeral" | "decimal" | "bitvector" | "eof";
135
144
  text: string;
136
145
  line: number;
137
146
  column: number;
138
147
  offset: number;
139
148
  endOffset: number;
149
+ lastId: string;
140
150
  }
141
151
 
142
152
  export interface SmtList {
@@ -145,6 +155,7 @@ export interface SmtList {
145
155
  line: number;
146
156
  column: number;
147
157
  offset: number;
158
+ lastId: string;
148
159
  // Z3 reports an error in making an application at its closing parenthesis.
149
160
  end: SmtToken;
150
161
  // The token after a root-obj or an indexed or qualified identifier, where Z3 reports an error in
@@ -287,6 +298,17 @@ interface PatternFrame {
287
298
  next: number;
288
299
  }
289
300
 
301
+ interface MatchFrame {
302
+ kind: "match";
303
+ list: SmtList;
304
+ cases: SExpr[];
305
+ isCaseForm: boolean;
306
+ depth: number;
307
+ isTail: boolean;
308
+ start: number;
309
+ next: number;
310
+ }
311
+
290
312
  // smt2parser.cpp's expr_frame.
291
313
  type TermFrame =
292
314
  | ApplicationFrame
@@ -295,7 +317,8 @@ type TermFrame =
295
317
  | LetDeclFrame
296
318
  | QuantifierFrame
297
319
  | AttributeFrame
298
- | PatternFrame;
320
+ | PatternFrame
321
+ | MatchFrame;
299
322
 
300
323
  const QUANTIFIER_KINDS = new Map([
301
324
  ["forall", QUANTIFIER_FORALL],
@@ -516,6 +539,8 @@ const REAL_FUNCTION_OPS = new Set([
516
539
 
517
540
  // The arity of each builtin declaration but distinct, which takes the arguments it is given.
518
541
  const ARITIES = new Map<number, number>([
542
+ [OP_TRUE, 0],
543
+ [OP_FALSE, 0],
519
544
  [OP_ITE, 3],
520
545
  ...[OP_NOT, OP_UMINUS, OP_ABS, OP_TO_REAL, OP_TO_INT, OP_IS_INT, ...REAL_FUNCTION_OPS]
521
546
  .filter((op) => op !== OP_ATAN2)
@@ -586,6 +611,14 @@ const ARRAY_OPS = new Map([
586
611
  ]);
587
612
  const ARRAY_EXTENSION_OPS = new Map([["default", OP_ARRAY_DEFAULT]]);
588
613
 
614
+ const isPortedTheoryOperator = (name: string) =>
615
+ BV_OP_NAMES.has(name) ||
616
+ FPA_OP_NAMES.has(name) ||
617
+ SEQ_OP_NAMES.has(name) ||
618
+ CHAR_OP_NAMES.has(name) ||
619
+ ARRAY_OPS.has(name) ||
620
+ ARRAY_EXTENSION_OPS.has(name);
621
+
589
622
  const UNARY_BOOL_REFS = new Map([["not", OP_NOT]]);
590
623
  const BINARY_BOOL_REFS = new Map([
591
624
  ["and", OP_AND],
@@ -640,12 +673,14 @@ const FF_LITERAL = /^#f-?[0-9]+m[0-9]+$/;
640
673
  const FF_LOGICS = new Set(["QF_FF", "QF_FFA", "ALL", "HO_ALL"]);
641
674
 
642
675
  // rational::is_unsigned of a numeral token.
643
- const isUnsigned = (numeral: string) => BigInt(numeral) >= 0n && BigInt(numeral) <= 0xffffffffn;
676
+ export const isUnsigned = (numeral: string) =>
677
+ BigInt(numeral) >= 0n && BigInt(numeral) <= 0xffffffffn;
644
678
 
645
679
  const SYMBOL_CHARACTER = /[A-Za-z0-9~!@$%^&*_\-+=<>.?/,]/;
646
680
 
647
681
  const createScanner = (text: string) => {
648
- const state = { index: 0, line: 1, lineStart: -1 };
682
+ // Z3 prints the null symbol its scanner starts with as null.
683
+ const state = { index: 0, line: 1, lineStart: -1, lastId: "null" };
649
684
  const getColumn = () => state.index - state.lineStart - (state.line === 1 ? 0 : 1);
650
685
  const advance = () => {
651
686
  if (text[state.index] === "\n") {
@@ -671,13 +706,17 @@ const createScanner = (text: string) => {
671
706
  }
672
707
  const start = state.index;
673
708
  const position = { line: state.line, column: getColumn() };
674
- const getToken = (kind: SmtToken["kind"], tokenText = text.slice(start, state.index)) => ({
675
- kind,
676
- text: tokenText,
677
- ...position,
678
- offset: start,
679
- endOffset: state.index,
680
- });
709
+ const getToken = (kind: SmtToken["kind"], tokenText = text.slice(start, state.index)) => {
710
+ if (kind === "symbol" || kind === "keyword") state.lastId = tokenText;
711
+ return {
712
+ kind,
713
+ text: tokenText,
714
+ ...position,
715
+ offset: start,
716
+ endOffset: state.index,
717
+ lastId: state.lastId,
718
+ };
719
+ };
681
720
  if (character === "(" || character === ")") {
682
721
  advance();
683
722
  return getToken(character);
@@ -701,6 +740,9 @@ const createScanner = (text: string) => {
701
740
  // string, so the columns of the rest of the line count one more.
702
741
  if (current === "\n") state.lineStart--;
703
742
  if (current !== character) continue;
743
+ // Z3's scanner rereads the last character at the end of the file, as if the quote were doubled.
744
+ if (character === '"' && state.index === text.length)
745
+ throw new SmtParseError("unexpected end of file", position);
704
746
  if (character !== '"' || text[state.index] !== '"') break;
705
747
  advance();
706
748
  }
@@ -731,25 +773,23 @@ const createScanner = (text: string) => {
731
773
  }
732
774
  continue;
733
775
  }
734
- if (radix === "f") {
735
- advance();
736
- readWhile(/[0-9m-]/);
737
- return getToken("symbol");
738
- }
739
- if (radix !== "x" && radix !== "b")
776
+ if (radix !== "f" && radix !== "x" && radix !== "b")
740
777
  throw new SmtParseError("invalid bit-vector literal, expecting 'x' or 'b'", {
741
778
  line: state.line,
742
779
  column: getColumn(),
743
780
  });
744
781
  advance();
745
782
  const digitsStart = state.index;
746
- readWhile(radix === "x" ? /[0-9a-fA-F]/ : /[01]/);
747
- if (state.index === digitsStart)
783
+ readWhile(radix === "f" ? /[0-9m-]/ : radix === "x" ? /[0-9a-fA-F]/ : /[01]/);
784
+ if (state.index === digitsStart && radix !== "f")
748
785
  throw new SmtParseError("invalid empty bit-vector literal", {
749
786
  line: state.line,
750
787
  column: getColumn(),
751
788
  });
752
- return getToken("bitvector");
789
+ // Z3's scanner rereads the last digit at the end of the file.
790
+ if (state.index === text.length && state.index !== digitsStart)
791
+ throw new SmtParseError("unexpected end of file", position);
792
+ return getToken(radix === "f" ? "symbol" : "bitvector");
753
793
  }
754
794
  if (character === ":" || SYMBOL_CHARACTER.test(character)) {
755
795
  advance();
@@ -760,7 +800,11 @@ const createScanner = (text: string) => {
760
800
  throw new SmtParseError("unexpected character", position);
761
801
  }
762
802
  };
763
- return { next, getPosition: () => ({ line: state.line, column: getColumn() }) };
803
+ return {
804
+ next,
805
+ getPosition: () => ({ line: state.line, column: getColumn() }),
806
+ getLastId: () => state.lastId,
807
+ };
764
808
  };
765
809
 
766
810
  export const createCommandReader = (text: string) => {
@@ -782,13 +826,29 @@ export const createCommandReader = (text: string) => {
782
826
  line: open.line,
783
827
  column: open.column,
784
828
  offset: open.offset,
829
+ lastId: open.lastId,
785
830
  end: open,
786
831
  };
787
832
  let followed: SmtList | undefined;
788
833
  for (let current = root; ;) {
789
834
  const token = take();
790
- if (token === undefined)
791
- throw new SmtParseError("unexpected end of file", scanner.getPosition());
835
+ if (token === undefined) {
836
+ // Z3 parses a command as it reads it, and sees the end of the file where it wants a token.
837
+ const eof: SmtToken = {
838
+ kind: "eof",
839
+ text: "",
840
+ ...scanner.getPosition(),
841
+ offset: text.length,
842
+ endOffset: text.length,
843
+ lastId: scanner.getLastId(),
844
+ };
845
+ if (followed !== undefined) followed.follower = eof;
846
+ current.items.push(eof);
847
+ const end = { ...eof };
848
+ current.end = end;
849
+ for (const list of stack) list.end = end;
850
+ return root;
851
+ }
792
852
  if (followed !== undefined) followed.follower = token;
793
853
  followed = undefined;
794
854
  if (token.kind === "(") {
@@ -798,6 +858,7 @@ export const createCommandReader = (text: string) => {
798
858
  line: token.line,
799
859
  column: token.column,
800
860
  offset: token.offset,
861
+ lastId: token.lastId,
801
862
  end: token,
802
863
  root,
803
864
  };
@@ -820,12 +881,28 @@ export const createCommandReader = (text: string) => {
820
881
  current = parent;
821
882
  }
822
883
  };
823
- const read = (): SmtList | undefined => {
884
+ const readOpen = () => {
824
885
  const token = take();
825
886
  if (token === undefined) return undefined;
826
887
  if (token.kind !== "(") throw new SmtParseError("invalid command, '(' expected", token);
827
888
  state.depth = 1;
828
- return readList(token);
889
+ return token;
890
+ };
891
+ const read = () => {
892
+ const open = readOpen();
893
+ return open === undefined ? undefined : readList(open);
894
+ };
895
+ // parse_cmd checks the head of a command before it reads the rest.
896
+ const readCommand = () => {
897
+ const open = readOpen();
898
+ if (open === undefined) return undefined;
899
+ state.lookahead = take();
900
+ if (!isSymbol(state.lookahead))
901
+ throw new SmtParseError(
902
+ "invalid command, symbol expected",
903
+ state.lookahead ?? scanner.getPosition(),
904
+ );
905
+ return readList(open);
829
906
  };
830
907
  const skip = (report: (error: SmtParseError) => void) => {
831
908
  for (;;) {
@@ -844,27 +921,17 @@ export const createCommandReader = (text: string) => {
844
921
  }
845
922
  }
846
923
  };
847
- return { read, skip };
924
+ return { read, readCommand, skip };
848
925
  };
849
926
 
850
- export const isSymbol = (sexpr: SExpr | undefined, text?: string): sexpr is SmtToken =>
851
- sexpr !== undefined && sexpr.kind === "symbol" && (text === undefined || sexpr.text === text);
852
-
853
- // The identifier Z3's scanner read last, which check_next prints after "got".
854
- const getLastId = (sexpr: SExpr | undefined): string | undefined => {
855
- if (sexpr === undefined) return undefined;
856
- if (sexpr.kind !== "list")
857
- return sexpr.kind === "symbol" || sexpr.kind === "keyword" ? sexpr.text : undefined;
858
- return getLastIdBefore(sexpr.items, sexpr.items.length);
927
+ export const expectClosed = (sexpr: SExpr) => {
928
+ const end = sexpr.kind === "list" ? sexpr.end : sexpr;
929
+ if (end.kind === "eof")
930
+ throw new SmtParseError("invalid s-expression, unexpected end of file", end);
859
931
  };
860
932
 
861
- const getLastIdBefore = (items: SExpr[], end: number): string | undefined => {
862
- for (let index = end - 1; index >= 0; index--) {
863
- const id = getLastId(items[index]);
864
- if (id !== undefined) return id;
865
- }
866
- return undefined;
867
- };
933
+ export const isSymbol = (sexpr: SExpr | undefined, text?: string): sexpr is SmtToken =>
934
+ sexpr !== undefined && sexpr.kind === "symbol" && (text === undefined || sexpr.text === text);
868
935
 
869
936
  const isSimpleIndex = (index: SExpr) =>
870
937
  index.kind === "numeral" ||
@@ -877,11 +944,20 @@ const expectList = (sexpr: SExpr | undefined, message: string, at: SExpr): SmtLi
877
944
  return sexpr;
878
945
  };
879
946
 
947
+ const expectListNext = (sexpr: SExpr, message: string) => {
948
+ if (sexpr.kind !== "list") throw new SmtParseError(`${message} got ${sexpr.lastId}`, sexpr);
949
+ return sexpr;
950
+ };
951
+
880
952
  const expectSymbol = (sexpr: SExpr | undefined, message: string, at: SExpr): SmtToken => {
881
953
  if (!isSymbol(sexpr)) throw new SmtParseError(message, sexpr ?? at);
882
954
  return sexpr;
883
955
  };
884
956
 
957
+ const expectEnd = (extra: SExpr | undefined, message: string) => {
958
+ if (extra !== undefined) throw new SmtParseError(message, extra);
959
+ };
960
+
885
961
  export const createSmtlibReader = (
886
962
  manager: AstManager,
887
963
  logic?: string,
@@ -900,6 +976,10 @@ export const createSmtlibReader = (
900
976
  ]);
901
977
  const funcs = new Map<string, readonly number[]>();
902
978
  const macros = new Map<string, Macro>();
979
+ // A pop empties the macros of a name but keeps the name in the dictionary.
980
+ const macroNames: string[] = [];
981
+ const dictionaryNames = new Set<string>();
982
+ const macroDictionary = new ObjMap<string>((index) => getSymbolHash(macroNames[index]));
903
983
  const lastNamed = { name: "", expr: -1 };
904
984
  const notifiedSorts = new Set<number>();
905
985
  const undo: (() => void)[] = [];
@@ -924,6 +1004,9 @@ export const createSmtlibReader = (
924
1004
  const unrooted: number[] = [];
925
1005
  const pending: number[] = [];
926
1006
 
1007
+ const scopedFuncs: number[] = [];
1008
+ const scopedFuncStarts: number[] = [];
1009
+
927
1010
  const declare = <Value>(names: Map<string, Value>, name: string, value: Value) => {
928
1011
  const previous = names.get(name);
929
1012
  undo.push(() => {
@@ -933,6 +1016,14 @@ export const createSmtlibReader = (
933
1016
  names.set(name, value);
934
1017
  };
935
1018
 
1019
+ const insertMacro = (name: string, macro: Macro) => {
1020
+ if (!dictionaryNames.has(name)) {
1021
+ dictionaryNames.add(name);
1022
+ macroDictionary.set(macroNames.push(name) - 1, name);
1023
+ }
1024
+ declare(macros, name, macro);
1025
+ };
1026
+
936
1027
  const hasArrays = logic === undefined || logicHasArray(logic);
937
1028
  const hasFpa = logic === undefined || logicHasFpa(logic);
938
1029
  const hasArrayExtensions = logic === undefined || ARRAY_EXTENSION_LOGICS.has(logic);
@@ -972,6 +1063,7 @@ export const createSmtlibReader = (
972
1063
  at,
973
1064
  );
974
1065
  declare(funcs, name, [...existing, decl]);
1066
+ scopedFuncs.push(decl);
975
1067
  };
976
1068
 
977
1069
  const insertDatatypeDecls = (sort: number, at: SExpr) => {
@@ -1028,38 +1120,40 @@ export const createSmtlibReader = (
1028
1120
  ? decl.parameters.length
1029
1121
  : decl.arity;
1030
1122
 
1123
+ const registerList = () => {
1124
+ const element = manager.mkTypeParameter(0);
1125
+ mkDatatypes(manager, [
1126
+ mkDatatypeDef(
1127
+ manager,
1128
+ "List",
1129
+ [element],
1130
+ [
1131
+ { name: "nil", recognizer: "is-nil", accessors: [] },
1132
+ {
1133
+ name: "insert",
1134
+ recognizer: "is-insert",
1135
+ accessors: [
1136
+ { name: "head", range: element },
1137
+ { name: "tail", range: -1 },
1138
+ ],
1139
+ },
1140
+ ],
1141
+ ),
1142
+ ]);
1143
+ const list: DatatypeSortDecl = {
1144
+ kind: "datatype",
1145
+ name: "List",
1146
+ arity: 1,
1147
+ siblings: [],
1148
+ instances: new Map(),
1149
+ };
1150
+ sortDecls.set("List", list);
1151
+ return list;
1152
+ };
1153
+
1031
1154
  const instantiateSort = (decl: SortDecl, parameters: number[], at: SExpr): number => {
1032
1155
  if (decl.kind === "sort") return decl.sort;
1033
- if (decl.kind === "list") {
1034
- const element = manager.mkTypeParameter(0);
1035
- mkDatatypes(manager, [
1036
- mkDatatypeDef(
1037
- manager,
1038
- "List",
1039
- [element],
1040
- [
1041
- { name: "nil", recognizer: "is-nil", accessors: [] },
1042
- {
1043
- name: "insert",
1044
- recognizer: "is-insert",
1045
- accessors: [
1046
- { name: "head", range: element },
1047
- { name: "tail", range: -1 },
1048
- ],
1049
- },
1050
- ],
1051
- ),
1052
- ]);
1053
- const list: DatatypeSortDecl = {
1054
- kind: "datatype",
1055
- name: "List",
1056
- arity: 1,
1057
- siblings: [],
1058
- instances: new Map(),
1059
- };
1060
- sortDecls.set("List", list);
1061
- return instantiateSort(list, parameters, at);
1062
- }
1156
+ if (decl.kind === "list") return instantiateSort(registerList(), parameters, at);
1063
1157
  if (decl.kind === "user") return manager.mkUninterpretedSort(decl.name, parameters);
1064
1158
  if (decl.kind === "datatype") return instantiateDatatype(decl, parameters, at);
1065
1159
  return parseSort(
@@ -1075,7 +1169,11 @@ export const createSmtlibReader = (
1075
1169
  bindings: ReadonlyMap<string, number>,
1076
1170
  ): number => {
1077
1171
  const head = sexpr.items[0];
1078
- if (!isSymbol(head)) throw new SmtParseError("invalid sort, symbol or '_' expected", head);
1172
+ if (!isSymbol(head))
1173
+ throw new SmtParseError(
1174
+ `${context === "" ? "" : `${context} `}invalid sort, symbol or '_' expected`,
1175
+ head ?? sexpr.end,
1176
+ );
1079
1177
  const isArray =
1080
1178
  ARRAY_SORT_NAMES.has(head.text) || (head.text === "Set" && restricted.hasSetSort);
1081
1179
  if (isArray && hasArrays) {
@@ -1202,6 +1300,11 @@ export const createSmtlibReader = (
1202
1300
  throw new SmtParseError("bit-vector size must be greater than zero", sexpr.end);
1203
1301
  return manager.mkBvSort(Number(width.text));
1204
1302
  }
1303
+ if (sexpr.kind !== "symbol")
1304
+ throw new SmtParseError(
1305
+ `invalid sort, symbol, '_' or '(' expected got ${sexpr.lastId}`,
1306
+ sexpr,
1307
+ );
1205
1308
  const decl = sortDecls.get(sexpr.text);
1206
1309
  if (decl !== undefined) {
1207
1310
  if (getSortDeclArity(decl) !== 0)
@@ -1276,19 +1379,18 @@ export const createSmtlibReader = (
1276
1379
  shrink(exprs, 0);
1277
1380
  };
1278
1381
 
1279
- // push_local: var_shifter moves the free variables of a term that was bound under fewer binders.
1280
- // Its cache holds the terms it makes, which replaceFreeVars rebuilds.
1281
- const getLocalTerm = (local: Local) => {
1282
- if (local.level === binders.depth) return local.expr;
1283
- if (manager.kinds[local.expr] === KIND_VAR)
1284
- return manager.mkVar(
1285
- manager.values[local.expr] + binders.depth - local.level,
1286
- manager.sorts[local.expr],
1287
- );
1288
- return replaceFreeVars(manager, local.expr, (variable, index, shift) =>
1289
- manager.mkVar(index + shift + binders.depth - local.level, manager.sorts[variable]),
1290
- );
1291
- };
1382
+ // The parser's var_shifter, whose cache holds the terms it makes, which replaceFreeVars rebuilds.
1383
+ const shiftFreeVars = (expr: number, amount: number) =>
1384
+ manager.kinds[expr] === KIND_VAR
1385
+ ? manager.mkVar(manager.values[expr] + amount, manager.sorts[expr])
1386
+ : replaceFreeVars(manager, expr, (variable, index, shift) =>
1387
+ manager.mkVar(index + shift + amount, manager.sorts[variable]),
1388
+ );
1389
+
1390
+ const getLocalTerm = (local: Local) =>
1391
+ local.level === binders.depth
1392
+ ? local.expr
1393
+ : shiftFreeVars(local.expr, binders.depth - local.level);
1292
1394
 
1293
1395
  // basic_decl_plugin::join.
1294
1396
  const getJoin = (left: number, right: number, at: SExpr) => {
@@ -1467,19 +1569,19 @@ export const createSmtlibReader = (
1467
1569
  };
1468
1570
 
1469
1571
  // parse_sorted_vars: the variables are created the first declared with the largest index, and
1470
- // the stack holds them. previousId is the identifier before the list.
1471
- const bindSortedVars = (sexpr: SExpr | undefined, at: SExpr, previousId: string) => {
1572
+ // the stack holds them.
1573
+ const bindSortedVars = (sexpr: SExpr | undefined, at: SExpr) => {
1472
1574
  if (sexpr?.kind !== "list")
1473
1575
  throw new SmtParseError(
1474
- `invalid list of sorted variables, '(' expected got ${getLastId(sexpr) ?? previousId}`,
1576
+ `invalid list of sorted variables, '(' expected got ${(sexpr ?? at).lastId}`,
1475
1577
  sexpr ?? at,
1476
1578
  );
1477
1579
  const names: string[] = [];
1478
1580
  const varSorts: number[] = [];
1479
- for (const [index, declaration] of sexpr.items.entries()) {
1581
+ for (const declaration of sexpr.items) {
1480
1582
  if (declaration.kind !== "list")
1481
1583
  throw new SmtParseError(
1482
- `invalid sorted variable, '(' expected got ${getLastId(declaration) ?? getLastIdBefore(sexpr.items, index) ?? previousId}`,
1584
+ `invalid sorted variable, '(' expected got ${declaration.lastId}`,
1483
1585
  declaration,
1484
1586
  );
1485
1587
  const name = declaration.items[0];
@@ -1492,10 +1594,7 @@ export const createSmtlibReader = (
1492
1594
  varSorts.push(parseSort(declaration.items[1] ?? declaration.end, "invalid sorted variables"));
1493
1595
  const extra = declaration.items[2];
1494
1596
  if (extra !== undefined)
1495
- throw new SmtParseError(
1496
- `invalid sorted variable, ')' expected got ${getLastId(extra) ?? getLastId(declaration.items[1]) ?? name.text}`,
1497
- extra,
1498
- );
1597
+ throw new SmtParseError(`invalid sorted variable, ')' expected got ${extra.lastId}`, extra);
1499
1598
  }
1500
1599
  beginScope();
1501
1600
  binders.depth += varSorts.length;
@@ -1569,13 +1668,17 @@ export const createSmtlibReader = (
1569
1668
 
1570
1669
  const mkBvApp = (name: string, args: number[], indices: number[], at: SExpr) => {
1571
1670
  const op = BV_OP_NAMES.get(name) ?? -1;
1572
- const signature = getBvSignature(
1573
- manager,
1574
- op,
1575
- args.map((arg) => manager.sorts[arg]),
1576
- indices,
1577
- );
1671
+ const argSorts = args.map((arg) => manager.sorts[arg]);
1672
+ if (op !== OP_INT2BV && op !== OP_MKBV && (args.length === 0 || !manager.isBvSort(argSorts[0])))
1673
+ throw new SmtParseError("operator is applied to arguments of the wrong sort", at);
1674
+ const signature = getBvSignature(manager, op, argSorts, indices);
1578
1675
  if (typeof signature === "string") throw new SmtParseError(signature, at);
1676
+ const mismatched = getMismatchedArgument(signature, argSorts);
1677
+ if (mismatched !== -1)
1678
+ throw new SmtParseError(
1679
+ `Argument ${printExpr(manager, { options: DEFAULT_PRINT_OPTIONS, isUsed, isClashing }, args[mismatched])} at position ${mismatched} has sort ${manager.getSortName(argSorts[mismatched])} it does not match declaration (declare-fun ${signature.name} (${signature.domain.map((sort) => manager.getSortName(sort)).join(" ")}) ${manager.getSortName(signature.range)})`,
1680
+ at,
1681
+ );
1579
1682
  const decl = manager.mkBvDecl(op, signature.name, indices, signature.domain, signature.range);
1580
1683
  return manager.mkApp(decl, args, signature.range);
1581
1684
  };
@@ -1727,7 +1830,10 @@ export const createSmtlibReader = (
1727
1830
  if (BigInt(index.text) === 0n)
1728
1831
  throw new SmtParseError("invalid root-obj, index must be >= 1", index);
1729
1832
  if (list.items.length > 3)
1730
- throw new SmtParseError("invalid root-obj, ')' expected", list.items[3]);
1833
+ throw new SmtParseError(
1834
+ `invalid root-obj, ')' expected got ${list.items[3].lastId}`,
1835
+ list.items[3],
1836
+ );
1731
1837
  const coeffs = parseUpolynomial(polynomial);
1732
1838
  const follower = list.follower ?? list.end;
1733
1839
  if (coeffs.length === 0)
@@ -1753,7 +1859,7 @@ export const createSmtlibReader = (
1753
1859
  if (!isSymbol(name))
1754
1860
  throw new SmtParseError("invalid indexed identifier, symbol expected", name ?? list.end);
1755
1861
  for (const index of list.items.slice(2)) {
1756
- if (index.kind === "string")
1862
+ if (index.kind === "string" || index.kind === "eof")
1757
1863
  throw new SmtParseError(
1758
1864
  "invalid indexed identifier, integer, identifier or '(' expected",
1759
1865
  index,
@@ -1778,7 +1884,7 @@ export const createSmtlibReader = (
1778
1884
  const target = list.items[1];
1779
1885
  if (target === undefined || (target.kind !== "symbol" && target.kind !== "list"))
1780
1886
  throw new SmtParseError(
1781
- `invalid (indexed) identifier, '(_' or symbol expected got ${getLastId(target) ?? "as"}`,
1887
+ `invalid (indexed) identifier, '(_' or symbol expected got ${(target ?? list.end).lastId}`,
1782
1888
  target ?? list.end,
1783
1889
  );
1784
1890
  if (target.kind === "list") {
@@ -1793,7 +1899,7 @@ export const createSmtlibReader = (
1793
1899
  const extra = list.items[3];
1794
1900
  if (extra !== undefined)
1795
1901
  throw new SmtParseError(
1796
- `invalid qualified identifier, ')' expected got ${getLastId(extra) ?? getLastIdBefore(list.items, 3) ?? "as"}`,
1902
+ `invalid qualified identifier, ')' expected got ${extra.lastId}`,
1797
1903
  extra,
1798
1904
  );
1799
1905
  return sort;
@@ -1857,18 +1963,19 @@ export const createSmtlibReader = (
1857
1963
  const sort = parseQualifierSort(list);
1858
1964
  const target = list.items[1];
1859
1965
  const local = isSymbol(target) ? locals.get(target.text) : undefined;
1860
- if (isSymbol(target) && local === undefined)
1861
- return hasArrays && target.text === "const"
1862
- ? mkArrayTerm(OP_CONST_ARRAY, [], [sort], at)
1863
- : parseApp(target, [], at, [], sort);
1966
+ if (isSymbol(target) && local === undefined && hasArrays && target.text === "const")
1967
+ return mkArrayTerm(OP_CONST_ARRAY, [], [sort], at);
1968
+ // A macro takes no range, so check_qualifier catches the sort of the term it stands for.
1864
1969
  const term =
1865
- local === undefined
1866
- ? parseIndexedTerm(
1867
- expectList(target, "invalid indexed identifier, '_' expected", list),
1868
- at,
1869
- sort,
1870
- )
1871
- : getLocalTerm(local);
1970
+ local !== undefined
1971
+ ? getLocalTerm(local)
1972
+ : isSymbol(target)
1973
+ ? parseApp(target, [], at, [], sort)
1974
+ : parseIndexedTerm(
1975
+ expectList(target, "invalid indexed identifier, '_' expected", list),
1976
+ at,
1977
+ sort,
1978
+ );
1872
1979
  if (manager.sorts[term] !== sort)
1873
1980
  throw new SmtParseError(
1874
1981
  `sort mismatch in qualified identifier, expected: ${manager.getSortName(sort)}, got: ${manager.getSortName(manager.sorts[term])}`,
@@ -2005,7 +2112,15 @@ export const createSmtlibReader = (
2005
2112
  const argSort = args.length === 1 ? manager.sorts[args[0]] : -1;
2006
2113
  if (argSort === -1 || !manager.isDatatypeSort(argSort))
2007
2114
  throw new SmtParseError(IS_PARAMETER_MESSAGE, at);
2008
- const constructor = getDatatypeConstructors(manager, argSort).find(
2115
+ const constructors = (() => {
2116
+ try {
2117
+ return getDatatypeConstructors(manager, argSort);
2118
+ } catch (error) {
2119
+ if (!(error instanceof SmtDefaultError)) throw error;
2120
+ throw new SmtParseError(error.message, at);
2121
+ }
2122
+ })();
2123
+ const constructor = constructors.find(
2009
2124
  (decl) => manager.funcDecls[decl].name === constructorName.text,
2010
2125
  );
2011
2126
  if (constructor === undefined)
@@ -2163,10 +2278,19 @@ export const createSmtlibReader = (
2163
2278
  const transcendentalOp = isTranscendental ? TRANSCENDENTAL_OPS.get(builtin) : undefined;
2164
2279
  if (isTranscendental && transcendentalOp === undefined)
2165
2280
  throw new SmtUnsupportedError(`the operator ${name}`);
2166
- const named =
2281
+ const builtinOp =
2167
2282
  builtin === "~"
2168
2283
  ? OP_UMINUS
2169
- : (CHAINABLE_OPS.get(builtin) ?? BUILTIN_OPS.get(builtin) ?? transcendentalOp);
2284
+ : builtin === "true"
2285
+ ? OP_TRUE
2286
+ : builtin === "false"
2287
+ ? OP_FALSE
2288
+ : (CHAINABLE_OPS.get(builtin) ?? BUILTIN_OPS.get(builtin) ?? transcendentalOp);
2289
+ // load_plugin registers the operators of arithmetic only for a logic that has it.
2290
+ const named =
2291
+ builtinOp !== undefined && manager.funcDecls[builtinOp].family === FAMILY_ARITH && !hasArith
2292
+ ? undefined
2293
+ : builtinOp;
2170
2294
  if (
2171
2295
  args.length === 0 &&
2172
2296
  named !== undefined &&
@@ -2177,7 +2301,17 @@ export const createSmtlibReader = (
2177
2301
  const op = named === OP_SUB && args.length === 1 ? OP_UMINUS : named;
2178
2302
  const domainSort = op === undefined ? -1 : getDomainSort(op, args, at);
2179
2303
  if (op !== undefined && domainSort !== -1) return mkApp(op, args, at, domainSort);
2180
- if (op === undefined && (THEORY_OPERATOR.test(name) || THEORY_OPERATORS.has(name)))
2304
+ if (
2305
+ op === undefined &&
2306
+ !isPortedTheoryOperator(name) &&
2307
+ ((THEORY_OPERATOR.test(name) &&
2308
+ (name.startsWith("bv")
2309
+ ? hasBv
2310
+ : name.startsWith("fp.")
2311
+ ? hasFpa
2312
+ : name.startsWith("ff.") || hasSeq)) ||
2313
+ THEORY_OPERATORS.has(name))
2314
+ )
2181
2315
  throw new SmtUnsupportedError(`the operator ${name}`);
2182
2316
  const instance = mkDatatypeInstanceApp(name, args, at);
2183
2317
  if (instance !== undefined) return instance;
@@ -2214,7 +2348,9 @@ export const createSmtlibReader = (
2214
2348
  throw new SmtParseError(
2215
2349
  sexpr.kind === "keyword"
2216
2350
  ? "invalid expression, unexpected keyword"
2217
- : "invalid expression, unexpected ')'",
2351
+ : sexpr.kind === "eof"
2352
+ ? "invalid expression, unexpected input"
2353
+ : "invalid expression, unexpected ')'",
2218
2354
  sexpr,
2219
2355
  );
2220
2356
  };
@@ -2234,8 +2370,10 @@ export const createSmtlibReader = (
2234
2370
  manager.incRef(expr);
2235
2371
  lastNamed.name = name;
2236
2372
  lastNamed.expr = expr;
2237
- manager.mkMacro(name, expr);
2238
- declare(macros, name, { domain: [], range: manager.sorts[expr], body: expr });
2373
+ const decl = manager.mkMacro(name, expr);
2374
+ declare(funcs, name, [...(funcs.get(name) ?? []), decl]);
2375
+ scopedFuncs.push(decl);
2376
+ insertMacro(name, { domain: [], range: manager.sorts[expr], body: expr });
2239
2377
  };
2240
2378
 
2241
2379
  // push_app_frame's parse_qualified_identifier_core, on a head that is (_ <symbol> <index>*) or
@@ -2295,6 +2433,106 @@ export const createSmtlibReader = (
2295
2433
  return { head: name, indices: [] };
2296
2434
  };
2297
2435
 
2436
+ const parseMatchPattern = (sexpr: SExpr, sort: number, after: SExpr) => {
2437
+ if (sexpr.kind !== "list" && sexpr.kind !== "symbol")
2438
+ throw new SmtParseError(
2439
+ "expecting a constructor, _, variable or constructor application",
2440
+ sexpr,
2441
+ );
2442
+ const name = sexpr.kind === "list" ? (sexpr.items[0] ?? sexpr.end) : sexpr;
2443
+ if (!isSymbol(name)) throw new SmtParseError("constructor symbol expected", name);
2444
+ const vars: string[] = [];
2445
+ if (sexpr.kind === "list")
2446
+ for (const [index, item] of sexpr.items.slice(1).entries()) {
2447
+ if (!isSymbol(item)) throw new SmtParseError("variable symbol expected", item);
2448
+ if (item.text !== "_" && vars.includes(item.text))
2449
+ throw new SmtParseError(
2450
+ "unexpected repeated variable in pattern expression",
2451
+ sexpr.items[index + 2] ?? sexpr.end,
2452
+ );
2453
+ vars.push(item.text);
2454
+ }
2455
+ const decl = funcs
2456
+ .get(name.text)
2457
+ ?.find(
2458
+ (candidate) =>
2459
+ manager.funcDecls[candidate].range === sort &&
2460
+ manager.funcDecls[candidate].domain.length === vars.length,
2461
+ );
2462
+ if (decl === undefined && vars.length > 0)
2463
+ throw new SmtParseError(
2464
+ `invalid function declaration reference, unknown indexed function ${name.text}`,
2465
+ after,
2466
+ );
2467
+ if (decl === undefined && (name.text === "true" || name.text === "false"))
2468
+ throw new SmtParseError("expecting a constructor", after);
2469
+ if (decl === undefined && BUILTIN_NAMES.has(name.text))
2470
+ throw new SmtUnsupportedError("a builtin name as a pattern of match");
2471
+ if (decl === undefined) {
2472
+ binders.depth++;
2473
+ const variable = manager.mkVar(0, sort);
2474
+ if (name.text !== "_") insertLocal(name.text, variable);
2475
+ return variable;
2476
+ }
2477
+ if (manager.funcDecls[decl].op !== OP_DT_CONSTRUCTOR)
2478
+ throw new SmtParseError("expecting a constructor", after);
2479
+ binders.depth += vars.length;
2480
+ const args = vars.map((variable, index) => {
2481
+ const boundVar = manager.mkVar(index, manager.funcDecls[decl].domain[index]);
2482
+ if (variable !== "_") insertLocal(variable, boundVar);
2483
+ return boundVar;
2484
+ });
2485
+ return manager.mkApp(decl, args, sort);
2486
+ };
2487
+
2488
+ const compileMatch = (term: number, patterns: number[], bodies: number[], at: SExpr) => {
2489
+ const sort = manager.sorts[term];
2490
+ if (!manager.isDatatypeSort(sort))
2491
+ throw new SmtParseError("pattern matching is only supported for algebraic datatypes", at);
2492
+ const constructors = getDatatypeConstructors(manager, sort);
2493
+ if (!patterns.some((pattern) => manager.kinds[pattern] === KIND_VAR)) {
2494
+ if (patterns.length < constructors.length)
2495
+ throw new SmtParseError("non-exhaustive pattern match", at);
2496
+ const marked = new Set(patterns.map((pattern) => manager.decls[pattern]));
2497
+ if (constructors.some((constructor) => !marked.has(constructor)))
2498
+ throw new SmtParseError("a constructor is missing from pattern match", at);
2499
+ }
2500
+ const subst = new VarSubst(manager, false);
2501
+ let result = -1;
2502
+ for (let index = patterns.length - 1; index >= 0; index--) {
2503
+ const pattern = patterns[index];
2504
+ const isVar = manager.kinds[pattern] === KIND_VAR;
2505
+ const recognizer = isVar ? -1 : getConstructorIs(manager, manager.decls[pattern]);
2506
+ const accessors = isVar ? [] : getConstructorAccessors(manager, manager.decls[pattern]);
2507
+ const shifted = shiftFreeVars(term, isVar ? 1 : accessors.length);
2508
+ manager.incRef(shifted);
2509
+ const substitution = isVar
2510
+ ? [shifted]
2511
+ : accessors.map((accessor) =>
2512
+ manager.mkApp(accessor, [shifted], manager.funcDecls[accessor].range),
2513
+ );
2514
+ for (const expr of substitution) manager.incRef(expr);
2515
+ const condition = isVar ? manager.trueExpr : manager.mkApp(recognizer, [term], SORT_BOOL);
2516
+ manager.incRef(condition);
2517
+ manager.decRef(shifted);
2518
+ const substituted = subst.apply(bodies[index], substitution);
2519
+ const shifter = new InvVarShifter(manager);
2520
+ const body = shifter.shift(substituted, substitution.length);
2521
+ manager.decRef(substituted);
2522
+ shifter.release();
2523
+ const args = [condition, body, result];
2524
+ const next = result === -1 ? body : mkApp(OP_ITE, args, at, getDomainSort(OP_ITE, args, at));
2525
+ manager.incRef(next);
2526
+ if (result !== -1) manager.decRef(result);
2527
+ result = next;
2528
+ manager.decRef(body);
2529
+ manager.decRef(condition);
2530
+ for (const expr of substitution) manager.decRef(expr);
2531
+ }
2532
+ subst.release();
2533
+ return result;
2534
+ };
2535
+
2298
2536
  // parse_expr. Z3 leaves the term on its stack until the command ends. A tail term is one whose
2299
2537
  // value is the value of the whole: it stays off the stack, in parsed, so that the caller of
2300
2538
  // parseTerm can have a term that nothing else holds without a reference of the parser's.
@@ -2329,6 +2567,7 @@ export const createSmtlibReader = (
2329
2567
  // Z3 skips a bad pattern as an s-expression, which cannot start with ')'.
2330
2568
  if (value.kind === ")")
2331
2569
  throw new SmtParseError("invalid s-expression, unexpected ')'", value);
2570
+ expectClosed(value);
2332
2571
  frame.next++;
2333
2572
  exprStack.push(-1);
2334
2573
  return;
@@ -2396,12 +2635,25 @@ export const createSmtlibReader = (
2396
2635
  deliver(parseRootObj(current), isTail, exprStack.length);
2397
2636
  return;
2398
2637
  }
2399
- if (head.text === "match") throw new SmtUnsupportedError("the expression match");
2638
+ if (head.text === "match") {
2639
+ frames.push({
2640
+ kind: "match",
2641
+ list: current,
2642
+ cases: [],
2643
+ isCaseForm: false,
2644
+ depth: -1,
2645
+ isTail,
2646
+ start: exprStack.length,
2647
+ next: -1,
2648
+ });
2649
+ enter(current.items[1] ?? current.end, false);
2650
+ return;
2651
+ }
2400
2652
  if (head.text === "let") {
2401
2653
  const bindings = current.items[1];
2402
2654
  if (bindings?.kind !== "list")
2403
2655
  throw new SmtParseError(
2404
- `invalid let declaration, '(' expected got ${getLastId(bindings) ?? "let"}`,
2656
+ `invalid let declaration, '(' expected got ${(bindings ?? current.end).lastId}`,
2405
2657
  bindings ?? current.end,
2406
2658
  );
2407
2659
  frames.push({
@@ -2444,7 +2696,7 @@ export const createSmtlibReader = (
2444
2696
  }
2445
2697
  // push_quant_frame.
2446
2698
  const start = exprStack.length;
2447
- const bound = bindSortedVars(current.items[1], current.end, head.text);
2699
+ const bound = bindSortedVars(current.items[1], current.end);
2448
2700
  if (bound.varSorts.length === 0)
2449
2701
  throw new SmtParseError(
2450
2702
  "invalid quantifier, list of sorted variables is empty",
@@ -2476,7 +2728,7 @@ export const createSmtlibReader = (
2476
2728
  // push_let_decl_frame.
2477
2729
  if (declaration.kind !== "list")
2478
2730
  throw new SmtParseError(
2479
- `invalid let declaration, '(' expected got ${getLastId(declaration) ?? getLastIdBefore(frame.bindings.items, frame.next) ?? "let"}`,
2731
+ `invalid let declaration, '(' expected got ${declaration.lastId}`,
2480
2732
  declaration,
2481
2733
  );
2482
2734
  const name = declaration.items[0];
@@ -2512,6 +2764,64 @@ export const createSmtlibReader = (
2512
2764
  continue;
2513
2765
  }
2514
2766
  const items = frame.list.items;
2767
+ if (frame.kind === "match") {
2768
+ if (frame.next === -1) {
2769
+ const bindings = items[2] ?? frame.list.end;
2770
+ if (bindings.kind !== "list")
2771
+ throw new SmtParseError(
2772
+ `pattern bindings should be enclosed in a parenthesis got ${bindings.lastId}`,
2773
+ bindings,
2774
+ );
2775
+ // curr_id_is_case reads the last identifier, which a '(' leaves as it was.
2776
+ frame.isCaseForm = (bindings.items[0] ?? bindings.end).lastId === "case";
2777
+ frame.cases = frame.isCaseForm ? items.slice(2) : bindings.items;
2778
+ frame.next = 0;
2779
+ } else if (frame.depth !== -1) {
2780
+ binders.depth = frame.depth;
2781
+ frame.depth = -1;
2782
+ endScope();
2783
+ const binding = frame.cases[frame.next - 1];
2784
+ const extra =
2785
+ binding.kind === "list" ? binding.items[frame.isCaseForm ? 3 : 2] : undefined;
2786
+ if (extra !== undefined)
2787
+ throw new SmtParseError(
2788
+ `invalid pattern binding, ')' expected got ${extra.lastId}`,
2789
+ extra,
2790
+ );
2791
+ }
2792
+ const binding = frame.cases[frame.next];
2793
+ if (binding !== undefined) {
2794
+ if (frame.isCaseForm && !(binding.kind === "list" && isSymbol(binding.items[0], "case")))
2795
+ throw new SmtUnsupportedError("the terms after the cases of match");
2796
+ if (binding.kind !== "list")
2797
+ throw new SmtParseError(
2798
+ `invalid pattern binding, '(' expected got ${binding.lastId}`,
2799
+ binding,
2800
+ );
2801
+ frame.next++;
2802
+ beginScope();
2803
+ frame.depth = binders.depth;
2804
+ const offset = frame.isCaseForm ? 1 : 0;
2805
+ const body = binding.items[offset + 1] ?? binding.end;
2806
+ const sort = manager.sorts[exprStack[frame.start]];
2807
+ pushRef(exprStack, parseMatchPattern(binding.items[offset] ?? binding.end, sort, body));
2808
+ enter(body, false);
2809
+ continue;
2810
+ }
2811
+ if (!frame.isCaseForm && items.length > 3)
2812
+ throw new SmtUnsupportedError("the terms after the cases of match");
2813
+ frames.pop();
2814
+ const term = exprStack[frame.start];
2815
+ const cases = exprStack.slice(frame.start + 1);
2816
+ const patterns = cases.filter((_expr, index) => index % 2 === 0);
2817
+ const bodies = cases.filter((_expr, index) => index % 2 === 1);
2818
+ const result = compileMatch(term, patterns, bodies, frame.list.end);
2819
+ // push_match_frame releases the bodies, then the patterns, then the term.
2820
+ exprStack.splice(frame.start, cases.length + 1, ...bodies, ...patterns, term);
2821
+ deliver(result, frame.isTail, frame.start);
2822
+ manager.decRef(result);
2823
+ continue;
2824
+ }
2515
2825
  if (frame.kind === "attribute") {
2516
2826
  // consume_attributes.
2517
2827
  if (exprStack.length === frame.start) {
@@ -2574,6 +2884,7 @@ export const createSmtlibReader = (
2574
2884
  // Z3 warns of an unknown attribute and skips its value as an s-expression.
2575
2885
  if (value.kind === ")")
2576
2886
  throw new SmtParseError("invalid s-expression, unexpected ')'", value);
2887
+ expectClosed(value);
2577
2888
  frame.next += 2;
2578
2889
  } else if (quantifier === undefined)
2579
2890
  throw new SmtParseError("invalid attribute, not in the scope of a quantifier", keyword);
@@ -2653,13 +2964,13 @@ export const createSmtlibReader = (
2653
2964
  return expr;
2654
2965
  };
2655
2966
 
2656
- const parseDomain = (sexpr: SExpr | undefined, at: SExpr) => {
2967
+ const parseDomain = (sexpr: SExpr) => {
2657
2968
  const context = "Parsing function declaration. Expecting sort list '('";
2658
- return expectList(sexpr, context, at).items.map((item) => parseSort(item, context));
2969
+ return expectListNext(sexpr, context).items.map((item) => parseSort(item, context));
2659
2970
  };
2660
2971
 
2661
- const parseSortParameters = (sexpr: SExpr | undefined, message: string, at: SExpr) =>
2662
- expectList(sexpr, "invalid sort declaration, parameters missing", at).items.map(
2972
+ const parseSortParameters = (sexpr: SExpr, message: string) =>
2973
+ expectListNext(sexpr, "invalid sort declaration, parameters missing").items.map(
2663
2974
  (item) => expectSymbol(item, message, item).text,
2664
2975
  );
2665
2976
 
@@ -2673,8 +2984,12 @@ export const createSmtlibReader = (
2673
2984
  missing: string[],
2674
2985
  ) => {
2675
2986
  if (sexpr.kind === "list") return parseSort(sexpr, "", bindings);
2987
+ if (sexpr.kind === ")") throw new SmtParseError("invalid sort, unexpected ')'", sexpr);
2676
2988
  if (sexpr.kind !== "symbol")
2677
- throw new SmtParseError("invalid sort, symbol, '_' or '(' expected", sexpr);
2989
+ throw new SmtParseError(
2990
+ `invalid sort, symbol, '_' or '(' expected got ${sexpr.lastId}`,
2991
+ sexpr,
2992
+ );
2678
2993
  if (sortDecls.has(sexpr.text) || bindings.has(sexpr.text))
2679
2994
  return parseSort(sexpr, "", bindings);
2680
2995
  const index = datatypeIndexes.get(sexpr.text);
@@ -2697,21 +3012,16 @@ export const createSmtlibReader = (
2697
3012
  );
2698
3013
  return items.map((item) => {
2699
3014
  if (isSymbol(item)) return { name: item.text, recognizer: `is-${item.text}`, accessors: [] };
2700
- const constructor = expectList(
2701
- item,
2702
- "invalid datatype declaration, '(' or ')' expected",
2703
- item,
2704
- );
3015
+ const constructor = expectListNext(item, "invalid datatype declaration, '(' or ')' expected");
2705
3016
  const name = expectSymbol(
2706
3017
  constructor.items[0],
2707
3018
  "invalid constructor declaration, symbol (constructor name) expected",
2708
3019
  constructor,
2709
3020
  ).text;
2710
3021
  const accessors = constructor.items.slice(1).map((accessorItem) => {
2711
- const accessor = expectList(
3022
+ const accessor = expectListNext(
2712
3023
  accessorItem,
2713
3024
  "invalid datatype declaration, '(' or ')' expected",
2714
- accessorItem,
2715
3025
  );
2716
3026
  const accessorName = expectSymbol(
2717
3027
  accessor.items[0],
@@ -2725,7 +3035,10 @@ export const createSmtlibReader = (
2725
3035
  missing,
2726
3036
  );
2727
3037
  if (accessor.items.length > 2)
2728
- throw new SmtParseError("invalid accessor declaration, ')' expected", accessor.items[2]);
3038
+ throw new SmtParseError(
3039
+ `invalid accessor declaration, ')' expected got ${accessor.items[2].lastId}`,
3040
+ accessor.items[2],
3041
+ );
2729
3042
  return { name: accessorName, range };
2730
3043
  });
2731
3044
  return { name, recognizer: `is-${name}`, accessors };
@@ -2733,27 +3046,29 @@ export const createSmtlibReader = (
2733
3046
  };
2734
3047
 
2735
3048
  const parseDatatypeBody = (
2736
- sexpr: SExpr | undefined,
3049
+ sexpr: SExpr,
2737
3050
  datatypeIndexes: ReadonlyMap<string, number>,
2738
3051
  missing: string[],
2739
- at: SExpr,
3052
+ name?: string,
2740
3053
  ) => {
2741
- const body = expectList(sexpr, "invalid datatype declaration, '(' expected", at);
2742
- if (!isSymbol(body.items[0], "par"))
3054
+ const body = expectListNext(sexpr, "invalid datatype declaration, '(' expected");
3055
+ if (!isSymbol(body.items[0], "par")) {
3056
+ if (name !== undefined) declareDatatypeSort(name, 0, body.items[0] ?? body.end);
2743
3057
  return {
2744
3058
  parameters: [],
2745
3059
  constructors: parseConstructors(body.items, NO_BINDINGS, datatypeIndexes, missing, body),
2746
3060
  };
3061
+ }
2747
3062
  const parameters = parseSortParameters(
2748
- body.items[1],
3063
+ body.items[1] ?? body.end,
2749
3064
  "invalid sort parameter, symbol or ')' expected",
2750
- body,
2751
3065
  );
2752
- const constructors = expectList(
2753
- body.items[2],
3066
+ const constructors = expectListNext(
3067
+ body.items[2] ?? body.end,
2754
3068
  "invalid constructor declaration after par, '(' expected",
2755
- body,
2756
3069
  );
3070
+ if (name !== undefined && parameters.length > 0)
3071
+ declareDatatypeSort(name, parameters.length, constructors.items[0] ?? constructors.end);
2757
3072
  return {
2758
3073
  parameters: parameters.map((_name, index) => manager.mkTypeParameter(index)),
2759
3074
  constructors: parseConstructors(
@@ -2793,11 +3108,17 @@ export const createSmtlibReader = (
2793
3108
  declare(sortDecls, name, decl);
2794
3109
  };
2795
3110
 
3111
+ const declareDatatypeSort = (name: string, arity: number, at: SExpr) =>
3112
+ declareSortDecl(
3113
+ name,
3114
+ { kind: "datatype", name, arity, siblings: [], instances: new Map() },
3115
+ at,
3116
+ );
3117
+
2796
3118
  const declareDatatypes = (list: SmtList) => {
2797
- const declaration = expectList(
2798
- list.items[1],
3119
+ const declaration = expectListNext(
3120
+ list.items[1] ?? list.end,
2799
3121
  "invalid sort declaration, parameters missing",
2800
- list,
2801
3122
  );
2802
3123
  const isSmt26 = declaration.items[0]?.kind === "list";
2803
3124
  const datatypeIndexes = new Map<string, number>();
@@ -2811,11 +3132,11 @@ export const createSmtlibReader = (
2811
3132
  );
2812
3133
  return;
2813
3134
  }
2814
- const sortDeclaration = expectList(item, "invalid sort declaration, '(' expected", item);
3135
+ const sortDeclaration = expectListNext(item, "invalid sort declaration, '(' expected");
2815
3136
  const name = expectSymbol(
2816
3137
  sortDeclaration.items[0],
2817
3138
  "invalid sort name, identified expected",
2818
- sortDeclaration,
3139
+ sortDeclaration.end,
2819
3140
  ).text;
2820
3141
  const arity = sortDeclaration.items[1];
2821
3142
  if (arity?.kind !== "numeral")
@@ -2823,26 +3144,25 @@ export const createSmtlibReader = (
2823
3144
  "invalid sort declaration, arity expected",
2824
3145
  arity ?? sortDeclaration.end,
2825
3146
  );
2826
- if (sortDeclaration.items.length > 2)
2827
- throw new SmtParseError("invalid sort declaration, ')' expected", sortDeclaration.items[2]);
2828
3147
  datatypeIndexes.set(name, index);
2829
3148
  names.push(name);
2830
3149
  arities.push(Number(arity.text));
2831
- declareSortDecl(
3150
+ declareDatatypeSort(
2832
3151
  name,
2833
- { kind: "datatype", name, arity: Number(arity.text), siblings: [], instances: new Map() },
2834
- sortDeclaration,
3152
+ Number(arity.text),
3153
+ sortDeclaration.items[2] ?? sortDeclaration.end,
2835
3154
  );
3155
+ if (sortDeclaration.items.length > 2)
3156
+ throw new SmtParseError("invalid sort declaration, ')' expected", sortDeclaration.items[2]);
2836
3157
  });
2837
- const bodies = expectList(list.items[2], "invalid datatype declaration, '(' expected", list);
3158
+ const bodies = expectListNext(
3159
+ list.items[2] ?? list.end,
3160
+ "invalid datatype declaration, '(' expected",
3161
+ );
2838
3162
  // A datatype of the older syntax may refer to any datatype of its declaration.
2839
3163
  if (!isSmt26)
2840
3164
  bodies.items.forEach((item, index) => {
2841
- const datatype = expectList(
2842
- item,
2843
- "invalid datatype declaration, '(' or ')' expected",
2844
- item,
2845
- );
3165
+ const datatype = expectListNext(item, "invalid datatype declaration, '(' or ')' expected");
2846
3166
  const name = expectSymbol(
2847
3167
  datatype.items[0],
2848
3168
  "invalid datatype declaration, symbol (datatype name) expected",
@@ -2867,10 +3187,10 @@ export const createSmtlibReader = (
2867
3187
  Array.from({ length: arities[index] }, (_unused, parameter) =>
2868
3188
  manager.mkTypeParameter(parameter),
2869
3189
  ),
2870
- parseDatatypeBody(item, datatypeIndexes, missing, list).constructors,
3190
+ parseDatatypeBody(item, datatypeIndexes, missing).constructors,
2871
3191
  );
2872
3192
  }
2873
- const datatype = expectList(item, "invalid datatype declaration, '(' or ')' expected", item);
3193
+ const datatype = expectListNext(item, "invalid datatype declaration, '(' or ')' expected");
2874
3194
  return mkDatatypeDef(
2875
3195
  manager,
2876
3196
  names[index],
@@ -2903,29 +3223,21 @@ export const createSmtlibReader = (
2903
3223
  };
2904
3224
 
2905
3225
  const declareDatatype = (list: SmtList) => {
2906
- const name = expectSymbol(list.items[1], "unexpected token used as datatype name", list);
3226
+ const name = expectSymbol(list.items[1], "unexpected token used as datatype name", list.end);
2907
3227
  const missing: string[] = [];
2908
- const bodyList = list.items[2];
2909
- const arity =
2910
- bodyList?.kind === "list" &&
2911
- isSymbol(bodyList.items[0], "par") &&
2912
- bodyList.items[1]?.kind === "list"
2913
- ? bodyList.items[1].items.length
2914
- : 0;
2915
- declareSortDecl(
3228
+ const body = parseDatatypeBody(
3229
+ list.items[2] ?? list.end,
3230
+ new Map([[name.text, 0]]),
3231
+ missing,
2916
3232
  name.text,
2917
- { kind: "datatype", name: name.text, arity, siblings: [], instances: new Map() },
2918
- name,
2919
3233
  );
2920
- const body = parseDatatypeBody(bodyList, new Map([[name.text, 0]]), missing, list);
2921
- if (list.items.length > 3)
2922
- throw new SmtParseError("invalid end of datatype declaration, ')' expected", list.items[3]);
2923
3234
  commitDatatypes(
2924
3235
  [mkDatatypeDef(manager, name.text, body.parameters, body.constructors)],
2925
3236
  missing,
2926
3237
  name,
2927
- list.end,
3238
+ list.items[3] ?? list.end,
2928
3239
  );
3240
+ expectEnd(list.items[3], "invalid end of datatype declaration, ')' expected");
2929
3241
  };
2930
3242
 
2931
3243
  // parse_rec_fun_decl: the variables are bound only while the range is parsed, and Z3 holds them
@@ -2936,7 +3248,7 @@ export const createSmtlibReader = (
2936
3248
  "invalid function/constant definition, symbol expected",
2937
3249
  at,
2938
3250
  );
2939
- const bound = bindSortedVars(items[1], at, symbol.text);
3251
+ const bound = bindSortedVars(items[1], at);
2940
3252
  const range = parseSort(items[2] ?? at, "Invalid recursive function definition");
2941
3253
  unbindSortedVars(bound.varSorts.length);
2942
3254
  return manager.mkRecFuncDecl(symbol.text, bound.varSorts, range);
@@ -2945,7 +3257,7 @@ export const createSmtlibReader = (
2945
3257
  // parse_rec_fun_body: Z3 reports a body of another sort at the token after it.
2946
3258
  const defineRecFun = (decl: number, variables: SExpr, body: SExpr, next: SExpr) => {
2947
3259
  const start = exprStack.length;
2948
- const bound = bindSortedVars(variables, body, "");
3260
+ const bound = bindSortedVars(variables, body);
2949
3261
  runTerm(body, false);
2950
3262
  const parsedBody = exprStack[exprStack.length - 1];
2951
3263
  unbindSortedVars(bound.varSorts.length);
@@ -2973,12 +3285,15 @@ export const createSmtlibReader = (
2973
3285
  subst.release();
2974
3286
  }
2975
3287
  for (const [index, sort] of domain.entries()) manager.incRef(manager.mkVar(index, sort));
2976
- const reversedVars = domain.map((_, index) => manager.mkVar(index, domain[count - index - 1]));
3288
+ const reversedVars = domain.map((_value, index) =>
3289
+ manager.mkVar(index, domain[count - index - 1]),
3290
+ );
2977
3291
  for (const reversedVar of reversedVars) manager.incRef(reversedVar);
2978
3292
  const decl = manager.mkRecFuncDecl(name, domain, manager.sorts[body]);
2979
3293
  for (const reversedVar of reversedVars) manager.decRef(reversedVar);
2980
3294
  setRecFunDefinition(manager, manager.recFunDefs.get(decl)!, true, body);
2981
3295
  declare(funcs, name, [...(funcs.get(name) ?? []), decl]);
3296
+ scopedFuncs.push(decl);
2982
3297
  };
2983
3298
 
2984
3299
  const declareCommand = (list: SmtList) => {
@@ -2988,6 +3303,7 @@ export const createSmtlibReader = (
2988
3303
  const decl = declareRecFun(list.items.slice(1), list.end);
2989
3304
  insertFunc(decl, list.items[4] ?? list.end);
2990
3305
  defineRecFun(decl, list.items[2], list.items[4] ?? list.end, list.items[5] ?? list.end);
3306
+ expectEnd(list.items[5], "invalid function/constant definition, ')' expected");
2991
3307
  shrinkRefs(exprStack, 0);
2992
3308
  return;
2993
3309
  }
@@ -3016,9 +3332,11 @@ export const createSmtlibReader = (
3016
3332
  bodies.items[index + 1] ?? bodies.end,
3017
3333
  );
3018
3334
  });
3019
- const extra = bodies.items[signatures.length];
3020
- if (extra !== undefined)
3021
- throw new SmtParseError("invalid recursive function definition, ')' expected", extra);
3335
+ expectEnd(
3336
+ bodies.items[signatures.length],
3337
+ "invalid recursive function definition, ')' expected",
3338
+ );
3339
+ expectEnd(list.items[3], "invalid function/constant definition, ')' expected");
3022
3340
  shrinkRefs(exprStack, 0);
3023
3341
  return;
3024
3342
  }
@@ -3030,7 +3348,26 @@ export const createSmtlibReader = (
3030
3348
  declareDatatype(list);
3031
3349
  return;
3032
3350
  }
3033
- const symbol = expectSymbol(list.items[1], "invalid declaration, symbol expected", list);
3351
+ const symbol = list.items[1];
3352
+ // check_nonreserved_identifier rejects _ and as, which define-const's check_identifier takes.
3353
+ if (
3354
+ !isSymbol(symbol) ||
3355
+ (name !== "define-const" && (symbol.text === "_" || symbol.text === "as"))
3356
+ )
3357
+ throw new SmtParseError(
3358
+ name === "declare-sort"
3359
+ ? "invalid sort declaration, symbol expected"
3360
+ : name === "define-sort"
3361
+ ? "invalid sort definition, symbol expected"
3362
+ : name === "define-fun"
3363
+ ? "invalid function/constant definition, symbol expected"
3364
+ : name === "define-const"
3365
+ ? "invalid constant definition, symbol expected"
3366
+ : name === "declare-fun"
3367
+ ? "invalid function declaration, symbol expected"
3368
+ : "invalid constant declaration, symbol expected",
3369
+ symbol ?? list.end,
3370
+ );
3034
3371
  if (name === "declare-sort") {
3035
3372
  if (sortDecls.has(symbol.text))
3036
3373
  throw new SmtParseError("invalid sort declaration, sort already declared/defined", symbol);
@@ -3040,8 +3377,6 @@ export const createSmtlibReader = (
3040
3377
  "invalid sort declaration, arity (<numeral>) or ')' expected",
3041
3378
  arity,
3042
3379
  );
3043
- if (list.items.length > 3)
3044
- throw new SmtParseError("invalid sort declaration, ')' expected", list.items[3]);
3045
3380
  declare(
3046
3381
  sortDecls,
3047
3382
  symbol.text,
@@ -3049,26 +3384,25 @@ export const createSmtlibReader = (
3049
3384
  ? { kind: "sort", sort: manager.mkSort(symbol.text) }
3050
3385
  : { kind: "user", name: symbol.text, arity: Number(arity.text) },
3051
3386
  );
3387
+ expectEnd(list.items[3], "invalid sort declaration, ')' expected");
3052
3388
  return;
3053
3389
  }
3054
3390
  if (name === "define-sort") {
3055
3391
  if (sortDecls.has(symbol.text))
3056
3392
  throw new SmtParseError("invalid sort definition, sort already declared/defined", symbol);
3057
3393
  const parameters = parseSortParameters(
3058
- list.items[2],
3394
+ list.items[2] ?? list.end,
3059
3395
  "invalid sort parameter, symbol or ')' expected",
3060
- list,
3061
3396
  );
3062
3397
  const body = list.items[3] ?? list.end;
3063
3398
  checkPsort(body, bindTypeParameters(parameters));
3064
3399
  declare(sortDecls, symbol.text, { kind: "definition", parameters, body });
3400
+ expectEnd(list.items[4], "invalid sort definition, ')' expected");
3065
3401
  return;
3066
3402
  }
3067
3403
  if (name === "define-fun" || name === "define-const") {
3068
3404
  const isConst = name === "define-const";
3069
- const bound = isConst
3070
- ? { names: [], varSorts: [] }
3071
- : bindSortedVars(list.items[2], list.end, symbol.text);
3405
+ const bound = isConst ? { names: [], varSorts: [] } : bindSortedVars(list.items[2], list.end);
3072
3406
  const range = parseSort(
3073
3407
  list.items[isConst ? 2 : 3] ?? list.end,
3074
3408
  isConst ? "Invalid constant definition" : "Invalid function definition",
@@ -3076,20 +3410,27 @@ export const createSmtlibReader = (
3076
3410
  runTerm(list.items[isConst ? 3 : 4] ?? list.end, false);
3077
3411
  const body = exprStack[exprStack.length - 1];
3078
3412
  if (!isConst) unbindSortedVars(bound.varSorts.length);
3413
+ const extra = list.items[isConst ? 4 : 5];
3079
3414
  if (manager.sorts[body] !== range)
3080
3415
  throw new SmtParseError(
3081
3416
  isConst
3082
3417
  ? "invalid constant definition, sort mismatch"
3083
3418
  : "invalid function/constant definition, sort mismatch",
3084
- list.end,
3419
+ extra ?? list.end,
3085
3420
  );
3086
3421
  manager.incRef(body);
3087
- declare(macros, symbol.text, { domain: bound.varSorts, range, body });
3422
+ insertMacro(symbol.text, { domain: bound.varSorts, range, body });
3088
3423
  defineMacroFunc(symbol.text, bound.varSorts, body);
3424
+ expectEnd(
3425
+ extra,
3426
+ isConst
3427
+ ? "invalid constant definition, ')' expected"
3428
+ : "invalid function/constant definition, ')' expected",
3429
+ );
3089
3430
  shrinkRefs(exprStack, 0);
3090
3431
  return;
3091
3432
  }
3092
- const domain = name === "declare-fun" ? parseDomain(list.items[2], list) : [];
3433
+ const domain = name === "declare-fun" ? parseDomain(list.items[2] ?? list.end) : [];
3093
3434
  const range = parseSort(
3094
3435
  list.items[name === "declare-fun" ? 3 : 2] ?? list.end,
3095
3436
  name === "declare-fun" ? "Invalid function declaration" : "Invalid constant declaration",
@@ -3101,45 +3442,60 @@ export const createSmtlibReader = (
3101
3442
  for (const domainVar of vars) manager.decRef(domainVar);
3102
3443
  }
3103
3444
  const decl = manager.mkFuncDecl(symbol.text, domain, range);
3104
- checkDecl(decl, list.end);
3445
+ const extra = list.items[name === "declare-fun" ? 4 : 3];
3446
+ checkDecl(decl, extra ?? list.end);
3105
3447
  if (macros.has(symbol.text))
3106
3448
  throw new SmtUnsupportedError(`the overloaded symbol ${symbol.text}`);
3107
3449
  const existing = funcs.get(symbol.text) ?? [];
3108
3450
  if (existing.some((other) => hasSignature(other, domain, range)))
3109
3451
  throw new SmtParseError(
3110
3452
  `invalid declaration, ${domain.length === 0 ? "constant" : "function"} '${symbol.text}' (with the given signature) already declared`,
3111
- list.end,
3453
+ extra ?? list.end,
3112
3454
  );
3113
3455
  declare(funcs, symbol.text, [...existing, decl]);
3456
+ expectEnd(
3457
+ extra,
3458
+ name === "declare-fun"
3459
+ ? "invalid function declaration, ')' expected"
3460
+ : "invalid constant declaration, ')' expected",
3461
+ );
3114
3462
  };
3115
3463
 
3116
- // The caller takes the reference of the stack.
3117
- const parseFormula = (sexpr: SExpr) => {
3464
+ // The caller takes the stack's reference. Z3 reports a non-Boolean term at the token after it.
3465
+ const parseFormula = (
3466
+ sexpr: SExpr,
3467
+ follower: SmtPosition,
3468
+ message = "invalid assert command, term is not Boolean",
3469
+ ) => {
3118
3470
  runTerm(sexpr, false);
3119
3471
  const assertion = exprStack[exprStack.length - 1];
3120
- if (manager.sorts[assertion] !== SORT_BOOL)
3121
- throw new SmtParseError("invalid assert command, term is not Boolean", sexpr);
3472
+ if (manager.sorts[assertion] !== SORT_BOOL) throw new SmtParseError(message, follower);
3122
3473
  exprStack.pop();
3123
3474
  return assertion;
3124
3475
  };
3125
3476
 
3126
3477
  // An assertion is named when the last name of its term is its own.
3127
- const parseAssertion = (sexpr: SExpr) => {
3478
+ const parseAssertion = (sexpr: SExpr, follower: SmtPosition) => {
3128
3479
  releasePending();
3129
3480
  lastNamed.expr = -1;
3130
- const formula = parseFormula(sexpr);
3481
+ const formula = parseFormula(sexpr, follower);
3131
3482
  return { formula, name: lastNamed.expr === formula ? lastNamed.name : undefined };
3132
3483
  };
3133
3484
 
3134
3485
  const push = () => {
3135
3486
  releasePending();
3136
3487
  scopeStarts.push(undo.length);
3488
+ scopedFuncStarts.push(scopedFuncs.length);
3137
3489
  };
3138
3490
 
3139
- const pop = () => {
3491
+ const pop = (count: number) => {
3140
3492
  releasePending();
3141
- const start = scopeStarts.pop() ?? 0;
3493
+ const funcStart = scopedFuncStarts.splice(scopedFuncStarts.length - count)[0];
3494
+ for (const decl of scopedFuncs.splice(funcStart)) manager.eraseRecFunDef(decl);
3495
+ const start = scopeStarts.splice(scopeStarts.length - count)[0];
3142
3496
  for (const restore of undo.splice(start).reverse()) restore();
3497
+ // cmd_context::pop resets the plugin through its datatype handler.
3498
+ resetDatatypePlugin(manager);
3143
3499
  };
3144
3500
 
3145
3501
  // parser::reset, after an error: Z3 frees its stacks, and with them the terms that only the
@@ -3202,7 +3558,7 @@ export const createSmtlibReader = (
3202
3558
  getFuncs: (name: string) => funcs.get(name) ?? [],
3203
3559
  hasDefinitions,
3204
3560
  parseTerm,
3205
- parseSort: (sexpr: SExpr) => parseSort(sexpr, ""),
3561
+ parseSort: (sexpr: SExpr, context: string) => parseSort(sexpr, context),
3206
3562
  parseFormula,
3207
3563
  parseAssertion,
3208
3564
  getLogicError: logicChecker.checkExpr,
@@ -3210,9 +3566,15 @@ export const createSmtlibReader = (
3210
3566
  pop,
3211
3567
  reset,
3212
3568
  restrictSorts,
3569
+ registerList,
3213
3570
  isUsed,
3214
3571
  isClashing,
3215
3572
  getDeclaredFuncs,
3573
+ getConstantMacros: () =>
3574
+ [...macroDictionary.values()].flatMap((name) => {
3575
+ const macro = macros.get(name);
3576
+ return macro?.domain.length === 0 ? [{ name, body: macro.body }] : [];
3577
+ }),
3216
3578
  };
3217
3579
  };
3218
3580
 
@@ -3273,7 +3635,10 @@ export const parseSmtlib = (
3273
3635
  state.reader.declareCommand(list);
3274
3636
  } else if (name === "assert") {
3275
3637
  state.isInUse = true;
3276
- const assertion = state.reader.parseFormula(list.items[1] ?? list.end);
3638
+ const assertion = state.reader.parseFormula(
3639
+ list.items[1] ?? list.end,
3640
+ list.items[2] ?? list.end,
3641
+ );
3277
3642
  const logicError = state.reader.getLogicError(assertion);
3278
3643
  if (logicError !== undefined) {
3279
3644
  manager.decRef(assertion);
@@ -3286,7 +3651,7 @@ export const parseSmtlib = (
3286
3651
  };
3287
3652
  for (;;) {
3288
3653
  try {
3289
- const list = commands.read();
3654
+ const list = commands.readCommand();
3290
3655
  if (list === undefined) return { assertions, hasCheckSat: false, rlimit: state.rlimit };
3291
3656
  if (isSymbol(list.items[0], "check-sat"))
3292
3657
  return { assertions, hasCheckSat: true, rlimit: state.rlimit };