tla-ts 0.0.10-dev.99796fe03 → 0.0.10-dev.a17d6d9fa
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.
- package/dist/compile.cjs +3 -3
- package/dist/compile.js +1 -1
- package/dist/deciders.cjs +36 -36
- package/dist/deciders.js +54 -54
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +60 -60
- package/dist/prove.js +78 -78
- package/dist/run.cjs +42 -44
- package/dist/run.js +44 -46
- package/dist/tlapm.cjs +1 -1
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +43 -43
- package/dist/transfer.js +39 -39
- package/dist/worker.cjs +6 -6
- package/dist/worker.js +6 -6
- package/package.json +1 -1
- package/src/ast.ts +1 -0
- package/src/bags.ts +50 -29
- package/src/builtins.ts +4 -3
- package/src/check-failure.ts +1 -1
- package/src/check-trace.ts +90 -51
- package/src/check.ts +367 -178
- package/src/cli/runtime-invariants.ts +20 -56
- package/src/cli/tlc-arguments.ts +2 -2
- package/src/cli/tlc-output.ts +1 -2
- package/src/cli/tlc.ts +16 -10
- package/src/cli/xml-exporter.ts +1 -1
- package/src/codegen.ts +26 -9
- package/src/dependencies.ts +49 -9
- package/src/dfid.ts +22 -10
- package/src/environment.ts +58 -33
- package/src/errors.ts +7 -0
- package/src/evaluate.ts +117 -41
- package/src/exit-codes.ts +4 -2
- package/src/instances.ts +5 -5
- package/src/java-hash.ts +14 -0
- package/src/java-overrides.ts +2 -2
- package/src/levels.ts +138 -90
- package/src/liveness-checker.ts +81 -33
- package/src/liveness.ts +49 -11
- package/src/obligations/index.ts +1 -1
- package/src/obligations/normalize.ts +1 -4
- package/src/obligations/printer.ts +5 -5
- package/src/parallel-worker.ts +8 -2
- package/src/parallel.ts +5 -0
- package/src/parser.ts +112 -145
- package/src/prover/isabelle/isar/attrib.ts +4 -4
- package/src/prover/isabelle/isar/rule-cases.ts +8 -6
- package/src/prover/isabelle/provers/clasimp.ts +1 -1
- package/src/prover/isabelle/provers/hypsubst.ts +2 -2
- package/src/prover/isabelle/pure/rewriter.ts +3 -3
- package/src/prover/isabelle/pure/term.ts +3 -3
- package/src/prover/prep/md5.ts +26 -29
- package/src/prover/smt/rewrite.ts +2 -7
- package/src/prover/smt/simplify.ts +1 -1
- package/src/prover/smt/solver/arith-rewriter.ts +11 -3
- package/src/prover/smt/solver/array-rewriter.ts +12 -2
- package/src/prover/smt/solver/asserted-formulas.ts +169 -58
- package/src/prover/smt/solver/ast-lt.ts +1 -1
- package/src/prover/smt/solver/ast.ts +63 -14
- package/src/prover/smt/solver/auf-solver.ts +23 -12
- package/src/prover/smt/solver/bit-blast-tactic.ts +26 -0
- package/src/prover/smt/solver/blast-term-ite.ts +47 -0
- package/src/prover/smt/solver/bound-simplifier.ts +11 -13
- package/src/prover/smt/solver/bv-decl-plugin.ts +13 -10
- package/src/prover/smt/solver/bv-rewriter.ts +1 -1
- package/src/prover/smt/solver/card2bv.ts +32 -18
- package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -24
- package/src/prover/smt/solver/context.ts +47 -9
- package/src/prover/smt/solver/ctx-simplify.ts +1 -2
- package/src/prover/smt/solver/datatype-decl-plugin.ts +15 -7
- package/src/prover/smt/solver/dependent-expr-state.ts +9 -4
- package/src/prover/smt/solver/diff-neq.ts +1 -2
- package/src/prover/smt/solver/elim-bounds.ts +89 -0
- package/src/prover/smt/solver/elim-term-ite-tactic.ts +48 -0
- package/src/prover/smt/solver/elim-uncnstr.ts +5 -8
- package/src/prover/smt/solver/euf/arith-internalize.ts +1 -1
- package/src/prover/smt/solver/euf/array-internalize.ts +1 -1
- package/src/prover/smt/solver/euf/bv-internalize.ts +1 -1
- package/src/prover/smt/solver/euf/egraph.ts +4 -11
- package/src/prover/smt/solver/euf/euf-solver.ts +1 -2
- package/src/prover/smt/solver/euf/mam.ts +1 -5
- package/src/prover/smt/solver/euf/q-ematch.ts +1 -2
- package/src/prover/smt/solver/euf/th-solver.ts +0 -7
- package/src/prover/smt/solver/expr2polynomial.ts +204 -79
- package/src/prover/smt/solver/factor.ts +36 -14
- package/src/prover/smt/solver/fd-solver.ts +2 -0
- package/src/prover/smt/solver/fix-dl-var.ts +1 -2
- package/src/prover/smt/solver/fpa-rewriter.ts +1 -2
- package/src/prover/smt/solver/fpa2bv-converter.ts +3 -1
- package/src/prover/smt/solver/fpa2bv-rewriter.ts +1 -0
- package/src/prover/smt/solver/goal.ts +6 -3
- package/src/prover/smt/solver/goal2nlsat.ts +57 -30
- package/src/prover/smt/solver/hashtable.ts +4 -0
- package/src/prover/smt/solver/internalizer.ts +1 -1
- package/src/prover/smt/solver/logic-tactics.ts +9 -2
- package/src/prover/smt/solver/lp/lp-settings.ts +5 -1
- package/src/prover/smt/solver/lp/nla-core.ts +1 -1
- package/src/prover/smt/solver/lp/nla-grobner.ts +2 -2
- package/src/prover/smt/solver/lp/nra-solver.ts +45 -39
- package/src/prover/smt/solver/lp/pdd.ts +2 -3
- package/src/prover/smt/solver/lp/union-find.ts +1 -1
- package/src/prover/smt/solver/mam-compiler.ts +1 -1
- package/src/prover/smt/solver/model-based-opt.ts +1 -1
- package/src/prover/smt/solver/model-evaluator.ts +2 -0
- package/src/prover/smt/solver/model-generator.ts +85 -43
- package/src/prover/smt/solver/mpf.ts +2 -4
- package/src/prover/smt/solver/mus.ts +2 -3
- package/src/prover/smt/solver/muz/dl-context.ts +10 -4
- package/src/prover/smt/solver/muz/dl-mk-slice.ts +3 -3
- package/src/prover/smt/solver/muz/dl-rule.ts +2 -2
- package/src/prover/smt/solver/muz/horn-subsume-model-converter.ts +1 -1
- package/src/prover/smt/solver/muz/horn-tactic.ts +1 -1
- package/src/prover/smt/solver/muz/spacer-context.ts +2 -4
- package/src/prover/smt/solver/muz/spacer-pdr.ts +1 -0
- package/src/prover/smt/solver/nlqsat.ts +1 -1
- package/src/prover/smt/solver/nlsat/algebraic.ts +196 -92
- package/src/prover/smt/solver/nlsat/atoms.ts +52 -33
- package/src/prover/smt/solver/nlsat/explain.ts +375 -217
- package/src/prover/smt/solver/nlsat/levelwise.ts +460 -240
- package/src/prover/smt/solver/nlsat/polynomial.ts +2366 -681
- package/src/prover/smt/solver/nlsat/solver.ts +74 -64
- package/src/prover/smt/solver/nlsat/transcendentals.ts +41 -17
- package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +8 -4
- package/src/prover/smt/solver/nlsat/upolynomial.ts +41 -18
- package/src/prover/smt/solver/nlsat-tactic.ts +1 -1
- package/src/prover/smt/solver/nnf.ts +6 -2
- package/src/prover/smt/solver/opt-context.ts +20 -7
- package/src/prover/smt/solver/opt-nlsat.ts +29 -18
- package/src/prover/smt/solver/opt-pareto.ts +19 -0
- package/src/prover/smt/solver/opt-preprocess.ts +1 -1
- package/src/prover/smt/solver/opt-solver.ts +9 -5
- package/src/prover/smt/solver/optsmt.ts +1 -1
- package/src/prover/smt/solver/pb-rewriter.ts +1 -4
- package/src/prover/smt/solver/poly-rewriter.ts +75 -18
- package/src/prover/smt/solver/probes.ts +88 -23
- package/src/prover/smt/solver/proto-model.ts +8 -8
- package/src/prover/smt/solver/push-app-ite.ts +37 -0
- package/src/prover/smt/solver/qe-arith-plugin.ts +2 -4
- package/src/prover/smt/solver/qe-lite.ts +2 -2
- package/src/prover/smt/solver/qe.ts +3 -2
- package/src/prover/smt/solver/qsat.ts +16 -6
- package/src/prover/smt/solver/quantifier-manager.ts +14 -9
- package/src/prover/smt/solver/recfun-decl-plugin.ts +2 -1
- package/src/prover/smt/solver/rewriter.ts +31 -25
- package/src/prover/smt/solver/rlimit.ts +4 -0
- package/src/prover/smt/solver/sat/config.ts +1 -1
- package/src/prover/smt/solver/sat/goal2sat.ts +0 -1
- package/src/prover/smt/solver/sat/inc-sat-solver.ts +39 -5
- package/src/prover/smt/solver/sat/model-converter.ts +1 -1
- package/src/prover/smt/solver/sat/pb-solver.ts +1 -1
- package/src/prover/smt/solver/sat/sat-tactic.ts +13 -5
- package/src/prover/smt/solver/sat/simplifier.ts +1 -1
- package/src/prover/smt/solver/sat/solver.ts +25 -47
- package/src/prover/smt/solver/seq-axioms.ts +20 -35
- package/src/prover/smt/solver/seq-decl-plugin.ts +1 -1
- package/src/prover/smt/solver/seq-derive.ts +2 -2
- package/src/prover/smt/solver/seq-eq-solver.ts +2 -2
- package/src/prover/smt/solver/seq-monadic.ts +7 -5
- package/src/prover/smt/solver/seq-re-rewriter.ts +2 -1
- package/src/prover/smt/solver/seq-re-util.ts +1 -1
- package/src/prover/smt/solver/seq-regex-bisim.ts +1 -1
- package/src/prover/smt/solver/seq-regex-live.ts +2 -2
- package/src/prover/smt/solver/seq-rewriter.ts +31 -29
- package/src/prover/smt/solver/seq-skolem.ts +0 -23
- package/src/prover/smt/solver/seq-view-witness.ts +4 -4
- package/src/prover/smt/solver/smt-kernel.ts +39 -5
- package/src/prover/smt/solver/smt-setup.ts +96 -10
- package/src/prover/smt/solver/smtlib-parser.ts +584 -223
- package/src/prover/smt/solver/smtlib-script.ts +509 -154
- package/src/prover/smt/solver/solve-eqs.ts +36 -37
- package/src/prover/smt/solver/solver.ts +18 -3
- package/src/prover/smt/solver/tactic.ts +284 -168
- package/src/prover/smt/solver/theory-bv.ts +13 -3
- package/src/prover/smt/solver/theory-fpa.ts +1 -1
- package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
- package/src/prover/smt/solver/theory-lra-internalize.ts +2 -0
- package/src/prover/smt/solver/theory-lra-propagate.ts +11 -3
- package/src/prover/smt/solver/theory-lra.ts +1 -1
- package/src/prover/smt/solver/theory-pb.ts +24 -7
- package/src/prover/smt/solver/theory-seq-solve.ts +1 -1
- package/src/prover/smt/solver/theory-seq-state.ts +2 -21
- package/src/prover/smt/solver/tseitin-cnf.ts +1 -0
- package/src/prover/tlapm/cache.ts +8 -8
- package/src/prover/zenon/index-table.ts +5 -2
- package/src/prover/zenon/isar-case.ts +1 -1
- package/src/prover/zenon/isar.ts +2 -2
- package/src/prover/zenon/phrase.ts +1 -1
- package/src/resolve.ts +959 -243
- package/src/runtime.ts +7 -1
- package/src/sany/builtins.ts +10 -10
- package/src/sany/generation.ts +2 -13
- package/src/sany/generator-definitions.ts +3 -1
- package/src/sany/generator-expressions.ts +36 -30
- package/src/sany/generator-proofs.ts +5 -3
- package/src/sany/generator.ts +2 -1
- package/src/sany/lexer.ts +3 -3
- package/src/sany/modules.ts +4 -2
- package/src/sany/nodes.ts +1 -1
- package/src/sany/selectors.ts +11 -6
- package/src/sany/xml.ts +2 -3
- package/src/search.ts +166 -101
- package/src/simulate.ts +110 -50
- package/src/spec-description.ts +5 -6
- package/src/standard-modules.ts +15 -6
- package/src/subexpressions.ts +398 -108
- package/src/successors.ts +4 -2
- package/src/symmetry.ts +1 -1
- package/src/tlc/jar-data.ts +10 -0
- package/src/tlc/messages.ts +3 -3
- package/src/tlc/order.ts +14 -3
- package/src/tlc/random.ts +2 -1
- package/src/tlc/replay.ts +3 -2
- package/src/tlc/tlc-operators.ts +10 -8
- package/src/tlc/tlcext-operators.ts +44 -15
- package/src/tlc/trace-spec.ts +9 -22
- package/src/typed-tier.ts +32 -25
- package/src/typescript/types.ts +2 -2
- package/src/values.ts +135 -78
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
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
|
|
@@ -208,8 +219,6 @@ type SortDecl = ConstantSortDecl | UserSortDecl | DatatypeSortDecl | DefinedSort
|
|
|
208
219
|
|
|
209
220
|
const NO_BINDINGS: ReadonlyMap<string, number> = new Map();
|
|
210
221
|
|
|
211
|
-
const UPOLYNOMIALS = new UpolynomialManager();
|
|
212
|
-
|
|
213
222
|
interface SmtProblem {
|
|
214
223
|
assertions: number[];
|
|
215
224
|
hasCheckSat: boolean;
|
|
@@ -289,6 +298,17 @@ interface PatternFrame {
|
|
|
289
298
|
next: number;
|
|
290
299
|
}
|
|
291
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
|
+
|
|
292
312
|
// smt2parser.cpp's expr_frame.
|
|
293
313
|
type TermFrame =
|
|
294
314
|
| ApplicationFrame
|
|
@@ -297,7 +317,8 @@ type TermFrame =
|
|
|
297
317
|
| LetDeclFrame
|
|
298
318
|
| QuantifierFrame
|
|
299
319
|
| AttributeFrame
|
|
300
|
-
| PatternFrame
|
|
320
|
+
| PatternFrame
|
|
321
|
+
| MatchFrame;
|
|
301
322
|
|
|
302
323
|
const QUANTIFIER_KINDS = new Map([
|
|
303
324
|
["forall", QUANTIFIER_FORALL],
|
|
@@ -518,6 +539,8 @@ const REAL_FUNCTION_OPS = new Set([
|
|
|
518
539
|
|
|
519
540
|
// The arity of each builtin declaration but distinct, which takes the arguments it is given.
|
|
520
541
|
const ARITIES = new Map<number, number>([
|
|
542
|
+
[OP_TRUE, 0],
|
|
543
|
+
[OP_FALSE, 0],
|
|
521
544
|
[OP_ITE, 3],
|
|
522
545
|
...[OP_NOT, OP_UMINUS, OP_ABS, OP_TO_REAL, OP_TO_INT, OP_IS_INT, ...REAL_FUNCTION_OPS]
|
|
523
546
|
.filter((op) => op !== OP_ATAN2)
|
|
@@ -588,6 +611,14 @@ const ARRAY_OPS = new Map([
|
|
|
588
611
|
]);
|
|
589
612
|
const ARRAY_EXTENSION_OPS = new Map([["default", OP_ARRAY_DEFAULT]]);
|
|
590
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
|
+
|
|
591
622
|
const UNARY_BOOL_REFS = new Map([["not", OP_NOT]]);
|
|
592
623
|
const BINARY_BOOL_REFS = new Map([
|
|
593
624
|
["and", OP_AND],
|
|
@@ -642,12 +673,14 @@ const FF_LITERAL = /^#f-?[0-9]+m[0-9]+$/;
|
|
|
642
673
|
const FF_LOGICS = new Set(["QF_FF", "QF_FFA", "ALL", "HO_ALL"]);
|
|
643
674
|
|
|
644
675
|
// rational::is_unsigned of a numeral token.
|
|
645
|
-
const isUnsigned = (numeral: string) =>
|
|
676
|
+
export const isUnsigned = (numeral: string) =>
|
|
677
|
+
BigInt(numeral) >= 0n && BigInt(numeral) <= 0xffffffffn;
|
|
646
678
|
|
|
647
679
|
const SYMBOL_CHARACTER = /[A-Za-z0-9~!@$%^&*_\-+=<>.?/,]/;
|
|
648
680
|
|
|
649
681
|
const createScanner = (text: string) => {
|
|
650
|
-
|
|
682
|
+
// Z3 prints the null symbol its scanner starts with as null.
|
|
683
|
+
const state = { index: 0, line: 1, lineStart: -1, lastId: "null" };
|
|
651
684
|
const getColumn = () => state.index - state.lineStart - (state.line === 1 ? 0 : 1);
|
|
652
685
|
const advance = () => {
|
|
653
686
|
if (text[state.index] === "\n") {
|
|
@@ -673,13 +706,17 @@ const createScanner = (text: string) => {
|
|
|
673
706
|
}
|
|
674
707
|
const start = state.index;
|
|
675
708
|
const position = { line: state.line, column: getColumn() };
|
|
676
|
-
const getToken = (kind: SmtToken["kind"], tokenText = text.slice(start, state.index)) =>
|
|
677
|
-
kind
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
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
|
+
};
|
|
683
720
|
if (character === "(" || character === ")") {
|
|
684
721
|
advance();
|
|
685
722
|
return getToken(character);
|
|
@@ -703,6 +740,9 @@ const createScanner = (text: string) => {
|
|
|
703
740
|
// string, so the columns of the rest of the line count one more.
|
|
704
741
|
if (current === "\n") state.lineStart--;
|
|
705
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);
|
|
706
746
|
if (character !== '"' || text[state.index] !== '"') break;
|
|
707
747
|
advance();
|
|
708
748
|
}
|
|
@@ -733,25 +773,23 @@ const createScanner = (text: string) => {
|
|
|
733
773
|
}
|
|
734
774
|
continue;
|
|
735
775
|
}
|
|
736
|
-
if (radix
|
|
737
|
-
advance();
|
|
738
|
-
readWhile(/[0-9m-]/);
|
|
739
|
-
return getToken("symbol");
|
|
740
|
-
}
|
|
741
|
-
if (radix !== "x" && radix !== "b")
|
|
776
|
+
if (radix !== "f" && radix !== "x" && radix !== "b")
|
|
742
777
|
throw new SmtParseError("invalid bit-vector literal, expecting 'x' or 'b'", {
|
|
743
778
|
line: state.line,
|
|
744
779
|
column: getColumn(),
|
|
745
780
|
});
|
|
746
781
|
advance();
|
|
747
782
|
const digitsStart = state.index;
|
|
748
|
-
readWhile(radix === "x" ? /[0-9a-fA-F]/ : /[01]/);
|
|
749
|
-
if (state.index === digitsStart)
|
|
783
|
+
readWhile(radix === "f" ? /[0-9m-]/ : radix === "x" ? /[0-9a-fA-F]/ : /[01]/);
|
|
784
|
+
if (state.index === digitsStart && radix !== "f")
|
|
750
785
|
throw new SmtParseError("invalid empty bit-vector literal", {
|
|
751
786
|
line: state.line,
|
|
752
787
|
column: getColumn(),
|
|
753
788
|
});
|
|
754
|
-
|
|
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");
|
|
755
793
|
}
|
|
756
794
|
if (character === ":" || SYMBOL_CHARACTER.test(character)) {
|
|
757
795
|
advance();
|
|
@@ -762,7 +800,11 @@ const createScanner = (text: string) => {
|
|
|
762
800
|
throw new SmtParseError("unexpected character", position);
|
|
763
801
|
}
|
|
764
802
|
};
|
|
765
|
-
return {
|
|
803
|
+
return {
|
|
804
|
+
next,
|
|
805
|
+
getPosition: () => ({ line: state.line, column: getColumn() }),
|
|
806
|
+
getLastId: () => state.lastId,
|
|
807
|
+
};
|
|
766
808
|
};
|
|
767
809
|
|
|
768
810
|
export const createCommandReader = (text: string) => {
|
|
@@ -784,13 +826,29 @@ export const createCommandReader = (text: string) => {
|
|
|
784
826
|
line: open.line,
|
|
785
827
|
column: open.column,
|
|
786
828
|
offset: open.offset,
|
|
829
|
+
lastId: open.lastId,
|
|
787
830
|
end: open,
|
|
788
831
|
};
|
|
789
832
|
let followed: SmtList | undefined;
|
|
790
833
|
for (let current = root; ;) {
|
|
791
834
|
const token = take();
|
|
792
|
-
if (token === undefined)
|
|
793
|
-
|
|
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
|
+
}
|
|
794
852
|
if (followed !== undefined) followed.follower = token;
|
|
795
853
|
followed = undefined;
|
|
796
854
|
if (token.kind === "(") {
|
|
@@ -800,6 +858,7 @@ export const createCommandReader = (text: string) => {
|
|
|
800
858
|
line: token.line,
|
|
801
859
|
column: token.column,
|
|
802
860
|
offset: token.offset,
|
|
861
|
+
lastId: token.lastId,
|
|
803
862
|
end: token,
|
|
804
863
|
root,
|
|
805
864
|
};
|
|
@@ -822,12 +881,28 @@ export const createCommandReader = (text: string) => {
|
|
|
822
881
|
current = parent;
|
|
823
882
|
}
|
|
824
883
|
};
|
|
825
|
-
const
|
|
884
|
+
const readOpen = () => {
|
|
826
885
|
const token = take();
|
|
827
886
|
if (token === undefined) return undefined;
|
|
828
887
|
if (token.kind !== "(") throw new SmtParseError("invalid command, '(' expected", token);
|
|
829
888
|
state.depth = 1;
|
|
830
|
-
return
|
|
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);
|
|
831
906
|
};
|
|
832
907
|
const skip = (report: (error: SmtParseError) => void) => {
|
|
833
908
|
for (;;) {
|
|
@@ -846,27 +921,17 @@ export const createCommandReader = (text: string) => {
|
|
|
846
921
|
}
|
|
847
922
|
}
|
|
848
923
|
};
|
|
849
|
-
return { read, skip };
|
|
924
|
+
return { read, readCommand, skip };
|
|
850
925
|
};
|
|
851
926
|
|
|
852
|
-
export const
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
const getLastId = (sexpr: SExpr | undefined): string | undefined => {
|
|
857
|
-
if (sexpr === undefined) return undefined;
|
|
858
|
-
if (sexpr.kind !== "list")
|
|
859
|
-
return sexpr.kind === "symbol" || sexpr.kind === "keyword" ? sexpr.text : undefined;
|
|
860
|
-
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);
|
|
861
931
|
};
|
|
862
932
|
|
|
863
|
-
const
|
|
864
|
-
|
|
865
|
-
const id = getLastId(items[index]);
|
|
866
|
-
if (id !== undefined) return id;
|
|
867
|
-
}
|
|
868
|
-
return undefined;
|
|
869
|
-
};
|
|
933
|
+
export const isSymbol = (sexpr: SExpr | undefined, text?: string): sexpr is SmtToken =>
|
|
934
|
+
sexpr !== undefined && sexpr.kind === "symbol" && (text === undefined || sexpr.text === text);
|
|
870
935
|
|
|
871
936
|
const isSimpleIndex = (index: SExpr) =>
|
|
872
937
|
index.kind === "numeral" ||
|
|
@@ -879,11 +944,20 @@ const expectList = (sexpr: SExpr | undefined, message: string, at: SExpr): SmtLi
|
|
|
879
944
|
return sexpr;
|
|
880
945
|
};
|
|
881
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
|
+
|
|
882
952
|
const expectSymbol = (sexpr: SExpr | undefined, message: string, at: SExpr): SmtToken => {
|
|
883
953
|
if (!isSymbol(sexpr)) throw new SmtParseError(message, sexpr ?? at);
|
|
884
954
|
return sexpr;
|
|
885
955
|
};
|
|
886
956
|
|
|
957
|
+
const expectEnd = (extra: SExpr | undefined, message: string) => {
|
|
958
|
+
if (extra !== undefined) throw new SmtParseError(message, extra);
|
|
959
|
+
};
|
|
960
|
+
|
|
887
961
|
export const createSmtlibReader = (
|
|
888
962
|
manager: AstManager,
|
|
889
963
|
logic?: string,
|
|
@@ -902,6 +976,9 @@ export const createSmtlibReader = (
|
|
|
902
976
|
]);
|
|
903
977
|
const funcs = new Map<string, readonly number[]>();
|
|
904
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 macroDictionary = new ObjMap<string>((index) => getSymbolHash(macroNames[index]));
|
|
905
982
|
const lastNamed = { name: "", expr: -1 };
|
|
906
983
|
const notifiedSorts = new Set<number>();
|
|
907
984
|
const undo: (() => void)[] = [];
|
|
@@ -926,6 +1003,9 @@ export const createSmtlibReader = (
|
|
|
926
1003
|
const unrooted: number[] = [];
|
|
927
1004
|
const pending: number[] = [];
|
|
928
1005
|
|
|
1006
|
+
const scopedFuncs: number[] = [];
|
|
1007
|
+
const scopedFuncStarts: number[] = [];
|
|
1008
|
+
|
|
929
1009
|
const declare = <Value>(names: Map<string, Value>, name: string, value: Value) => {
|
|
930
1010
|
const previous = names.get(name);
|
|
931
1011
|
undo.push(() => {
|
|
@@ -935,6 +1015,11 @@ export const createSmtlibReader = (
|
|
|
935
1015
|
names.set(name, value);
|
|
936
1016
|
};
|
|
937
1017
|
|
|
1018
|
+
const insertMacro = (name: string, macro: Macro) => {
|
|
1019
|
+
if (!macroNames.includes(name)) macroDictionary.set(macroNames.push(name) - 1, name);
|
|
1020
|
+
declare(macros, name, macro);
|
|
1021
|
+
};
|
|
1022
|
+
|
|
938
1023
|
const hasArrays = logic === undefined || logicHasArray(logic);
|
|
939
1024
|
const hasFpa = logic === undefined || logicHasFpa(logic);
|
|
940
1025
|
const hasArrayExtensions = logic === undefined || ARRAY_EXTENSION_LOGICS.has(logic);
|
|
@@ -974,6 +1059,7 @@ export const createSmtlibReader = (
|
|
|
974
1059
|
at,
|
|
975
1060
|
);
|
|
976
1061
|
declare(funcs, name, [...existing, decl]);
|
|
1062
|
+
scopedFuncs.push(decl);
|
|
977
1063
|
};
|
|
978
1064
|
|
|
979
1065
|
const insertDatatypeDecls = (sort: number, at: SExpr) => {
|
|
@@ -1030,38 +1116,40 @@ export const createSmtlibReader = (
|
|
|
1030
1116
|
? decl.parameters.length
|
|
1031
1117
|
: decl.arity;
|
|
1032
1118
|
|
|
1119
|
+
const registerList = () => {
|
|
1120
|
+
const element = manager.mkTypeParameter(0);
|
|
1121
|
+
mkDatatypes(manager, [
|
|
1122
|
+
mkDatatypeDef(
|
|
1123
|
+
manager,
|
|
1124
|
+
"List",
|
|
1125
|
+
[element],
|
|
1126
|
+
[
|
|
1127
|
+
{ name: "nil", recognizer: "is-nil", accessors: [] },
|
|
1128
|
+
{
|
|
1129
|
+
name: "insert",
|
|
1130
|
+
recognizer: "is-insert",
|
|
1131
|
+
accessors: [
|
|
1132
|
+
{ name: "head", range: element },
|
|
1133
|
+
{ name: "tail", range: -1 },
|
|
1134
|
+
],
|
|
1135
|
+
},
|
|
1136
|
+
],
|
|
1137
|
+
),
|
|
1138
|
+
]);
|
|
1139
|
+
const list: DatatypeSortDecl = {
|
|
1140
|
+
kind: "datatype",
|
|
1141
|
+
name: "List",
|
|
1142
|
+
arity: 1,
|
|
1143
|
+
siblings: [],
|
|
1144
|
+
instances: new Map(),
|
|
1145
|
+
};
|
|
1146
|
+
sortDecls.set("List", list);
|
|
1147
|
+
return list;
|
|
1148
|
+
};
|
|
1149
|
+
|
|
1033
1150
|
const instantiateSort = (decl: SortDecl, parameters: number[], at: SExpr): number => {
|
|
1034
1151
|
if (decl.kind === "sort") return decl.sort;
|
|
1035
|
-
if (decl.kind === "list")
|
|
1036
|
-
const element = manager.mkTypeParameter(0);
|
|
1037
|
-
mkDatatypes(manager, [
|
|
1038
|
-
mkDatatypeDef(
|
|
1039
|
-
manager,
|
|
1040
|
-
"List",
|
|
1041
|
-
[element],
|
|
1042
|
-
[
|
|
1043
|
-
{ name: "nil", recognizer: "is-nil", accessors: [] },
|
|
1044
|
-
{
|
|
1045
|
-
name: "insert",
|
|
1046
|
-
recognizer: "is-insert",
|
|
1047
|
-
accessors: [
|
|
1048
|
-
{ name: "head", range: element },
|
|
1049
|
-
{ name: "tail", range: -1 },
|
|
1050
|
-
],
|
|
1051
|
-
},
|
|
1052
|
-
],
|
|
1053
|
-
),
|
|
1054
|
-
]);
|
|
1055
|
-
const list: DatatypeSortDecl = {
|
|
1056
|
-
kind: "datatype",
|
|
1057
|
-
name: "List",
|
|
1058
|
-
arity: 1,
|
|
1059
|
-
siblings: [],
|
|
1060
|
-
instances: new Map(),
|
|
1061
|
-
};
|
|
1062
|
-
sortDecls.set("List", list);
|
|
1063
|
-
return instantiateSort(list, parameters, at);
|
|
1064
|
-
}
|
|
1152
|
+
if (decl.kind === "list") return instantiateSort(registerList(), parameters, at);
|
|
1065
1153
|
if (decl.kind === "user") return manager.mkUninterpretedSort(decl.name, parameters);
|
|
1066
1154
|
if (decl.kind === "datatype") return instantiateDatatype(decl, parameters, at);
|
|
1067
1155
|
return parseSort(
|
|
@@ -1077,7 +1165,11 @@ export const createSmtlibReader = (
|
|
|
1077
1165
|
bindings: ReadonlyMap<string, number>,
|
|
1078
1166
|
): number => {
|
|
1079
1167
|
const head = sexpr.items[0];
|
|
1080
|
-
if (!isSymbol(head))
|
|
1168
|
+
if (!isSymbol(head))
|
|
1169
|
+
throw new SmtParseError(
|
|
1170
|
+
`${context === "" ? "" : `${context} `}invalid sort, symbol or '_' expected`,
|
|
1171
|
+
head ?? sexpr.end,
|
|
1172
|
+
);
|
|
1081
1173
|
const isArray =
|
|
1082
1174
|
ARRAY_SORT_NAMES.has(head.text) || (head.text === "Set" && restricted.hasSetSort);
|
|
1083
1175
|
if (isArray && hasArrays) {
|
|
@@ -1204,6 +1296,11 @@ export const createSmtlibReader = (
|
|
|
1204
1296
|
throw new SmtParseError("bit-vector size must be greater than zero", sexpr.end);
|
|
1205
1297
|
return manager.mkBvSort(Number(width.text));
|
|
1206
1298
|
}
|
|
1299
|
+
if (sexpr.kind !== "symbol")
|
|
1300
|
+
throw new SmtParseError(
|
|
1301
|
+
`invalid sort, symbol, '_' or '(' expected got ${sexpr.lastId}`,
|
|
1302
|
+
sexpr,
|
|
1303
|
+
);
|
|
1207
1304
|
const decl = sortDecls.get(sexpr.text);
|
|
1208
1305
|
if (decl !== undefined) {
|
|
1209
1306
|
if (getSortDeclArity(decl) !== 0)
|
|
@@ -1278,19 +1375,18 @@ export const createSmtlibReader = (
|
|
|
1278
1375
|
shrink(exprs, 0);
|
|
1279
1376
|
};
|
|
1280
1377
|
|
|
1281
|
-
//
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
};
|
|
1378
|
+
// The parser's var_shifter, whose cache holds the terms it makes, which replaceFreeVars rebuilds.
|
|
1379
|
+
const shiftFreeVars = (expr: number, amount: number) =>
|
|
1380
|
+
manager.kinds[expr] === KIND_VAR
|
|
1381
|
+
? manager.mkVar(manager.values[expr] + amount, manager.sorts[expr])
|
|
1382
|
+
: replaceFreeVars(manager, expr, (variable, index, shift) =>
|
|
1383
|
+
manager.mkVar(index + shift + amount, manager.sorts[variable]),
|
|
1384
|
+
);
|
|
1385
|
+
|
|
1386
|
+
const getLocalTerm = (local: Local) =>
|
|
1387
|
+
local.level === binders.depth
|
|
1388
|
+
? local.expr
|
|
1389
|
+
: shiftFreeVars(local.expr, binders.depth - local.level);
|
|
1294
1390
|
|
|
1295
1391
|
// basic_decl_plugin::join.
|
|
1296
1392
|
const getJoin = (left: number, right: number, at: SExpr) => {
|
|
@@ -1469,19 +1565,19 @@ export const createSmtlibReader = (
|
|
|
1469
1565
|
};
|
|
1470
1566
|
|
|
1471
1567
|
// parse_sorted_vars: the variables are created the first declared with the largest index, and
|
|
1472
|
-
// the stack holds them.
|
|
1473
|
-
const bindSortedVars = (sexpr: SExpr | undefined, at: SExpr
|
|
1568
|
+
// the stack holds them.
|
|
1569
|
+
const bindSortedVars = (sexpr: SExpr | undefined, at: SExpr) => {
|
|
1474
1570
|
if (sexpr?.kind !== "list")
|
|
1475
1571
|
throw new SmtParseError(
|
|
1476
|
-
`invalid list of sorted variables, '(' expected got ${
|
|
1572
|
+
`invalid list of sorted variables, '(' expected got ${(sexpr ?? at).lastId}`,
|
|
1477
1573
|
sexpr ?? at,
|
|
1478
1574
|
);
|
|
1479
1575
|
const names: string[] = [];
|
|
1480
1576
|
const varSorts: number[] = [];
|
|
1481
|
-
for (const
|
|
1577
|
+
for (const declaration of sexpr.items) {
|
|
1482
1578
|
if (declaration.kind !== "list")
|
|
1483
1579
|
throw new SmtParseError(
|
|
1484
|
-
`invalid sorted variable, '(' expected got ${
|
|
1580
|
+
`invalid sorted variable, '(' expected got ${declaration.lastId}`,
|
|
1485
1581
|
declaration,
|
|
1486
1582
|
);
|
|
1487
1583
|
const name = declaration.items[0];
|
|
@@ -1494,10 +1590,7 @@ export const createSmtlibReader = (
|
|
|
1494
1590
|
varSorts.push(parseSort(declaration.items[1] ?? declaration.end, "invalid sorted variables"));
|
|
1495
1591
|
const extra = declaration.items[2];
|
|
1496
1592
|
if (extra !== undefined)
|
|
1497
|
-
throw new SmtParseError(
|
|
1498
|
-
`invalid sorted variable, ')' expected got ${getLastId(extra) ?? getLastId(declaration.items[1]) ?? name.text}`,
|
|
1499
|
-
extra,
|
|
1500
|
-
);
|
|
1593
|
+
throw new SmtParseError(`invalid sorted variable, ')' expected got ${extra.lastId}`, extra);
|
|
1501
1594
|
}
|
|
1502
1595
|
beginScope();
|
|
1503
1596
|
binders.depth += varSorts.length;
|
|
@@ -1571,13 +1664,17 @@ export const createSmtlibReader = (
|
|
|
1571
1664
|
|
|
1572
1665
|
const mkBvApp = (name: string, args: number[], indices: number[], at: SExpr) => {
|
|
1573
1666
|
const op = BV_OP_NAMES.get(name) ?? -1;
|
|
1574
|
-
const
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
indices,
|
|
1579
|
-
);
|
|
1667
|
+
const argSorts = args.map((arg) => manager.sorts[arg]);
|
|
1668
|
+
if (op !== OP_INT2BV && op !== OP_MKBV && (args.length === 0 || !manager.isBvSort(argSorts[0])))
|
|
1669
|
+
throw new SmtParseError("operator is applied to arguments of the wrong sort", at);
|
|
1670
|
+
const signature = getBvSignature(manager, op, argSorts, indices);
|
|
1580
1671
|
if (typeof signature === "string") throw new SmtParseError(signature, at);
|
|
1672
|
+
const mismatched = getMismatchedArgument(signature, argSorts);
|
|
1673
|
+
if (mismatched !== -1)
|
|
1674
|
+
throw new SmtParseError(
|
|
1675
|
+
`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)})`,
|
|
1676
|
+
at,
|
|
1677
|
+
);
|
|
1581
1678
|
const decl = manager.mkBvDecl(op, signature.name, indices, signature.domain, signature.range);
|
|
1582
1679
|
return manager.mkApp(decl, args, signature.range);
|
|
1583
1680
|
};
|
|
@@ -1649,8 +1746,10 @@ export const createSmtlibReader = (
|
|
|
1649
1746
|
return mkSeqAppOf(manager, signature, args);
|
|
1650
1747
|
};
|
|
1651
1748
|
|
|
1749
|
+
const upolynomials = new UpolynomialManager(manager.limit);
|
|
1750
|
+
|
|
1652
1751
|
const parseUpolynomial = (sexpr: SExpr): bigint[] => {
|
|
1653
|
-
if (sexpr.kind === "numeral") return
|
|
1752
|
+
if (sexpr.kind === "numeral") return upolynomials.set([BigInt(sexpr.text)]);
|
|
1654
1753
|
if (sexpr.kind === "decimal") {
|
|
1655
1754
|
const parts = sexpr.text.split(".");
|
|
1656
1755
|
const value = Rational.of(BigInt(parts[0] + parts[1]), 10n ** BigInt(parts[1].length));
|
|
@@ -1659,7 +1758,7 @@ export const createSmtlibReader = (
|
|
|
1659
1758
|
"invalid univariate polynomial, integer coefficient expected",
|
|
1660
1759
|
sexpr,
|
|
1661
1760
|
);
|
|
1662
|
-
return
|
|
1761
|
+
return upolynomials.set([value.num]);
|
|
1663
1762
|
}
|
|
1664
1763
|
if (sexpr.kind === "symbol") {
|
|
1665
1764
|
if (sexpr.text !== "x")
|
|
@@ -1684,15 +1783,15 @@ export const createSmtlibReader = (
|
|
|
1684
1783
|
"invalid univariate polynomial, exponent must be an unsigned integer",
|
|
1685
1784
|
exponent,
|
|
1686
1785
|
);
|
|
1687
|
-
return
|
|
1786
|
+
return upolynomials.pw(parseUpolynomial(operands[0]), Number(exponent.text));
|
|
1688
1787
|
}
|
|
1689
1788
|
const combine =
|
|
1690
1789
|
head.text === "+"
|
|
1691
|
-
?
|
|
1790
|
+
? upolynomials.add
|
|
1692
1791
|
: head.text === "-"
|
|
1693
|
-
?
|
|
1792
|
+
? upolynomials.sub
|
|
1694
1793
|
: head.text === "*"
|
|
1695
|
-
?
|
|
1794
|
+
? upolynomials.mul
|
|
1696
1795
|
: undefined;
|
|
1697
1796
|
if (combine === undefined)
|
|
1698
1797
|
throw new SmtParseError(
|
|
@@ -1705,11 +1804,11 @@ export const createSmtlibReader = (
|
|
|
1705
1804
|
sexpr,
|
|
1706
1805
|
);
|
|
1707
1806
|
const first = parseUpolynomial(operands[0]);
|
|
1708
|
-
if (head.text === "-" && operands.length === 1) return
|
|
1807
|
+
if (head.text === "-" && operands.length === 1) return upolynomials.neg(first);
|
|
1709
1808
|
return operands
|
|
1710
1809
|
.slice(1)
|
|
1711
1810
|
.reduce(
|
|
1712
|
-
(result, operand) => combine.call(
|
|
1811
|
+
(result, operand) => combine.call(upolynomials, result, parseUpolynomial(operand)),
|
|
1713
1812
|
first,
|
|
1714
1813
|
);
|
|
1715
1814
|
};
|
|
@@ -1727,7 +1826,10 @@ export const createSmtlibReader = (
|
|
|
1727
1826
|
if (BigInt(index.text) === 0n)
|
|
1728
1827
|
throw new SmtParseError("invalid root-obj, index must be >= 1", index);
|
|
1729
1828
|
if (list.items.length > 3)
|
|
1730
|
-
throw new SmtParseError(
|
|
1829
|
+
throw new SmtParseError(
|
|
1830
|
+
`invalid root-obj, ')' expected got ${list.items[3].lastId}`,
|
|
1831
|
+
list.items[3],
|
|
1832
|
+
);
|
|
1731
1833
|
const coeffs = parseUpolynomial(polynomial);
|
|
1732
1834
|
const follower = list.follower ?? list.end;
|
|
1733
1835
|
if (coeffs.length === 0)
|
|
@@ -1753,7 +1855,7 @@ export const createSmtlibReader = (
|
|
|
1753
1855
|
if (!isSymbol(name))
|
|
1754
1856
|
throw new SmtParseError("invalid indexed identifier, symbol expected", name ?? list.end);
|
|
1755
1857
|
for (const index of list.items.slice(2)) {
|
|
1756
|
-
if (index.kind === "string")
|
|
1858
|
+
if (index.kind === "string" || index.kind === "eof")
|
|
1757
1859
|
throw new SmtParseError(
|
|
1758
1860
|
"invalid indexed identifier, integer, identifier or '(' expected",
|
|
1759
1861
|
index,
|
|
@@ -1778,7 +1880,7 @@ export const createSmtlibReader = (
|
|
|
1778
1880
|
const target = list.items[1];
|
|
1779
1881
|
if (target === undefined || (target.kind !== "symbol" && target.kind !== "list"))
|
|
1780
1882
|
throw new SmtParseError(
|
|
1781
|
-
`invalid (indexed) identifier, '(_' or symbol expected got ${
|
|
1883
|
+
`invalid (indexed) identifier, '(_' or symbol expected got ${(target ?? list.end).lastId}`,
|
|
1782
1884
|
target ?? list.end,
|
|
1783
1885
|
);
|
|
1784
1886
|
if (target.kind === "list") {
|
|
@@ -1793,7 +1895,7 @@ export const createSmtlibReader = (
|
|
|
1793
1895
|
const extra = list.items[3];
|
|
1794
1896
|
if (extra !== undefined)
|
|
1795
1897
|
throw new SmtParseError(
|
|
1796
|
-
`invalid qualified identifier, ')' expected got ${
|
|
1898
|
+
`invalid qualified identifier, ')' expected got ${extra.lastId}`,
|
|
1797
1899
|
extra,
|
|
1798
1900
|
);
|
|
1799
1901
|
return sort;
|
|
@@ -1857,18 +1959,19 @@ export const createSmtlibReader = (
|
|
|
1857
1959
|
const sort = parseQualifierSort(list);
|
|
1858
1960
|
const target = list.items[1];
|
|
1859
1961
|
const local = isSymbol(target) ? locals.get(target.text) : undefined;
|
|
1860
|
-
if (isSymbol(target) && local === undefined)
|
|
1861
|
-
return
|
|
1862
|
-
|
|
1863
|
-
: parseApp(target, [], at, [], sort);
|
|
1962
|
+
if (isSymbol(target) && local === undefined && hasArrays && target.text === "const")
|
|
1963
|
+
return mkArrayTerm(OP_CONST_ARRAY, [], [sort], at);
|
|
1964
|
+
// A macro takes no range, so check_qualifier catches the sort of the term it stands for.
|
|
1864
1965
|
const term =
|
|
1865
|
-
local
|
|
1866
|
-
?
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1966
|
+
local !== undefined
|
|
1967
|
+
? getLocalTerm(local)
|
|
1968
|
+
: isSymbol(target)
|
|
1969
|
+
? parseApp(target, [], at, [], sort)
|
|
1970
|
+
: parseIndexedTerm(
|
|
1971
|
+
expectList(target, "invalid indexed identifier, '_' expected", list),
|
|
1972
|
+
at,
|
|
1973
|
+
sort,
|
|
1974
|
+
);
|
|
1872
1975
|
if (manager.sorts[term] !== sort)
|
|
1873
1976
|
throw new SmtParseError(
|
|
1874
1977
|
`sort mismatch in qualified identifier, expected: ${manager.getSortName(sort)}, got: ${manager.getSortName(manager.sorts[term])}`,
|
|
@@ -2005,7 +2108,15 @@ export const createSmtlibReader = (
|
|
|
2005
2108
|
const argSort = args.length === 1 ? manager.sorts[args[0]] : -1;
|
|
2006
2109
|
if (argSort === -1 || !manager.isDatatypeSort(argSort))
|
|
2007
2110
|
throw new SmtParseError(IS_PARAMETER_MESSAGE, at);
|
|
2008
|
-
const
|
|
2111
|
+
const constructors = (() => {
|
|
2112
|
+
try {
|
|
2113
|
+
return getDatatypeConstructors(manager, argSort);
|
|
2114
|
+
} catch (error) {
|
|
2115
|
+
if (!(error instanceof SmtDefaultError)) throw error;
|
|
2116
|
+
throw new SmtParseError(error.message, at);
|
|
2117
|
+
}
|
|
2118
|
+
})();
|
|
2119
|
+
const constructor = constructors.find(
|
|
2009
2120
|
(decl) => manager.funcDecls[decl].name === constructorName.text,
|
|
2010
2121
|
);
|
|
2011
2122
|
if (constructor === undefined)
|
|
@@ -2163,10 +2274,19 @@ export const createSmtlibReader = (
|
|
|
2163
2274
|
const transcendentalOp = isTranscendental ? TRANSCENDENTAL_OPS.get(builtin) : undefined;
|
|
2164
2275
|
if (isTranscendental && transcendentalOp === undefined)
|
|
2165
2276
|
throw new SmtUnsupportedError(`the operator ${name}`);
|
|
2166
|
-
const
|
|
2277
|
+
const builtinOp =
|
|
2167
2278
|
builtin === "~"
|
|
2168
2279
|
? OP_UMINUS
|
|
2169
|
-
:
|
|
2280
|
+
: builtin === "true"
|
|
2281
|
+
? OP_TRUE
|
|
2282
|
+
: builtin === "false"
|
|
2283
|
+
? OP_FALSE
|
|
2284
|
+
: (CHAINABLE_OPS.get(builtin) ?? BUILTIN_OPS.get(builtin) ?? transcendentalOp);
|
|
2285
|
+
// load_plugin registers the operators of arithmetic only for a logic that has it.
|
|
2286
|
+
const named =
|
|
2287
|
+
builtinOp !== undefined && manager.funcDecls[builtinOp].family === FAMILY_ARITH && !hasArith
|
|
2288
|
+
? undefined
|
|
2289
|
+
: builtinOp;
|
|
2170
2290
|
if (
|
|
2171
2291
|
args.length === 0 &&
|
|
2172
2292
|
named !== undefined &&
|
|
@@ -2177,7 +2297,17 @@ export const createSmtlibReader = (
|
|
|
2177
2297
|
const op = named === OP_SUB && args.length === 1 ? OP_UMINUS : named;
|
|
2178
2298
|
const domainSort = op === undefined ? -1 : getDomainSort(op, args, at);
|
|
2179
2299
|
if (op !== undefined && domainSort !== -1) return mkApp(op, args, at, domainSort);
|
|
2180
|
-
if (
|
|
2300
|
+
if (
|
|
2301
|
+
op === undefined &&
|
|
2302
|
+
!isPortedTheoryOperator(name) &&
|
|
2303
|
+
((THEORY_OPERATOR.test(name) &&
|
|
2304
|
+
(name.startsWith("bv")
|
|
2305
|
+
? hasBv
|
|
2306
|
+
: name.startsWith("fp.")
|
|
2307
|
+
? hasFpa
|
|
2308
|
+
: name.startsWith("ff.") || hasSeq)) ||
|
|
2309
|
+
THEORY_OPERATORS.has(name))
|
|
2310
|
+
)
|
|
2181
2311
|
throw new SmtUnsupportedError(`the operator ${name}`);
|
|
2182
2312
|
const instance = mkDatatypeInstanceApp(name, args, at);
|
|
2183
2313
|
if (instance !== undefined) return instance;
|
|
@@ -2214,7 +2344,9 @@ export const createSmtlibReader = (
|
|
|
2214
2344
|
throw new SmtParseError(
|
|
2215
2345
|
sexpr.kind === "keyword"
|
|
2216
2346
|
? "invalid expression, unexpected keyword"
|
|
2217
|
-
:
|
|
2347
|
+
: sexpr.kind === "eof"
|
|
2348
|
+
? "invalid expression, unexpected input"
|
|
2349
|
+
: "invalid expression, unexpected ')'",
|
|
2218
2350
|
sexpr,
|
|
2219
2351
|
);
|
|
2220
2352
|
};
|
|
@@ -2234,8 +2366,10 @@ export const createSmtlibReader = (
|
|
|
2234
2366
|
manager.incRef(expr);
|
|
2235
2367
|
lastNamed.name = name;
|
|
2236
2368
|
lastNamed.expr = expr;
|
|
2237
|
-
manager.mkMacro(name, expr);
|
|
2238
|
-
declare(
|
|
2369
|
+
const decl = manager.mkMacro(name, expr);
|
|
2370
|
+
declare(funcs, name, [...(funcs.get(name) ?? []), decl]);
|
|
2371
|
+
scopedFuncs.push(decl);
|
|
2372
|
+
insertMacro(name, { domain: [], range: manager.sorts[expr], body: expr });
|
|
2239
2373
|
};
|
|
2240
2374
|
|
|
2241
2375
|
// push_app_frame's parse_qualified_identifier_core, on a head that is (_ <symbol> <index>*) or
|
|
@@ -2295,6 +2429,106 @@ export const createSmtlibReader = (
|
|
|
2295
2429
|
return { head: name, indices: [] };
|
|
2296
2430
|
};
|
|
2297
2431
|
|
|
2432
|
+
const parseMatchPattern = (sexpr: SExpr, sort: number, after: SExpr) => {
|
|
2433
|
+
if (sexpr.kind !== "list" && sexpr.kind !== "symbol")
|
|
2434
|
+
throw new SmtParseError(
|
|
2435
|
+
"expecting a constructor, _, variable or constructor application",
|
|
2436
|
+
sexpr,
|
|
2437
|
+
);
|
|
2438
|
+
const name = sexpr.kind === "list" ? (sexpr.items[0] ?? sexpr.end) : sexpr;
|
|
2439
|
+
if (!isSymbol(name)) throw new SmtParseError("constructor symbol expected", name);
|
|
2440
|
+
const vars: string[] = [];
|
|
2441
|
+
if (sexpr.kind === "list")
|
|
2442
|
+
for (const [index, item] of sexpr.items.slice(1).entries()) {
|
|
2443
|
+
if (!isSymbol(item)) throw new SmtParseError("variable symbol expected", item);
|
|
2444
|
+
if (item.text !== "_" && vars.includes(item.text))
|
|
2445
|
+
throw new SmtParseError(
|
|
2446
|
+
"unexpected repeated variable in pattern expression",
|
|
2447
|
+
sexpr.items[index + 2] ?? sexpr.end,
|
|
2448
|
+
);
|
|
2449
|
+
vars.push(item.text);
|
|
2450
|
+
}
|
|
2451
|
+
const decl = funcs
|
|
2452
|
+
.get(name.text)
|
|
2453
|
+
?.find(
|
|
2454
|
+
(candidate) =>
|
|
2455
|
+
manager.funcDecls[candidate].range === sort &&
|
|
2456
|
+
manager.funcDecls[candidate].domain.length === vars.length,
|
|
2457
|
+
);
|
|
2458
|
+
if (decl === undefined && vars.length > 0)
|
|
2459
|
+
throw new SmtParseError(
|
|
2460
|
+
`invalid function declaration reference, unknown indexed function ${name.text}`,
|
|
2461
|
+
after,
|
|
2462
|
+
);
|
|
2463
|
+
if (decl === undefined && (name.text === "true" || name.text === "false"))
|
|
2464
|
+
throw new SmtParseError("expecting a constructor", after);
|
|
2465
|
+
if (decl === undefined && BUILTIN_NAMES.has(name.text))
|
|
2466
|
+
throw new SmtUnsupportedError("a builtin name as a pattern of match");
|
|
2467
|
+
if (decl === undefined) {
|
|
2468
|
+
binders.depth++;
|
|
2469
|
+
const variable = manager.mkVar(0, sort);
|
|
2470
|
+
if (name.text !== "_") insertLocal(name.text, variable);
|
|
2471
|
+
return variable;
|
|
2472
|
+
}
|
|
2473
|
+
if (manager.funcDecls[decl].op !== OP_DT_CONSTRUCTOR)
|
|
2474
|
+
throw new SmtParseError("expecting a constructor", after);
|
|
2475
|
+
binders.depth += vars.length;
|
|
2476
|
+
const args = vars.map((variable, index) => {
|
|
2477
|
+
const boundVar = manager.mkVar(index, manager.funcDecls[decl].domain[index]);
|
|
2478
|
+
if (variable !== "_") insertLocal(variable, boundVar);
|
|
2479
|
+
return boundVar;
|
|
2480
|
+
});
|
|
2481
|
+
return manager.mkApp(decl, args, sort);
|
|
2482
|
+
};
|
|
2483
|
+
|
|
2484
|
+
const compileMatch = (term: number, patterns: number[], bodies: number[], at: SExpr) => {
|
|
2485
|
+
const sort = manager.sorts[term];
|
|
2486
|
+
if (!manager.isDatatypeSort(sort))
|
|
2487
|
+
throw new SmtParseError("pattern matching is only supported for algebraic datatypes", at);
|
|
2488
|
+
const constructors = getDatatypeConstructors(manager, sort);
|
|
2489
|
+
if (!patterns.some((pattern) => manager.kinds[pattern] === KIND_VAR)) {
|
|
2490
|
+
if (patterns.length < constructors.length)
|
|
2491
|
+
throw new SmtParseError("non-exhaustive pattern match", at);
|
|
2492
|
+
const marked = new Set(patterns.map((pattern) => manager.decls[pattern]));
|
|
2493
|
+
if (constructors.some((constructor) => !marked.has(constructor)))
|
|
2494
|
+
throw new SmtParseError("a constructor is missing from pattern match", at);
|
|
2495
|
+
}
|
|
2496
|
+
const subst = new VarSubst(manager, false);
|
|
2497
|
+
let result = -1;
|
|
2498
|
+
for (let index = patterns.length - 1; index >= 0; index--) {
|
|
2499
|
+
const pattern = patterns[index];
|
|
2500
|
+
const isVar = manager.kinds[pattern] === KIND_VAR;
|
|
2501
|
+
const recognizer = isVar ? -1 : getConstructorIs(manager, manager.decls[pattern]);
|
|
2502
|
+
const accessors = isVar ? [] : getConstructorAccessors(manager, manager.decls[pattern]);
|
|
2503
|
+
const shifted = shiftFreeVars(term, isVar ? 1 : accessors.length);
|
|
2504
|
+
manager.incRef(shifted);
|
|
2505
|
+
const substitution = isVar
|
|
2506
|
+
? [shifted]
|
|
2507
|
+
: accessors.map((accessor) =>
|
|
2508
|
+
manager.mkApp(accessor, [shifted], manager.funcDecls[accessor].range),
|
|
2509
|
+
);
|
|
2510
|
+
for (const expr of substitution) manager.incRef(expr);
|
|
2511
|
+
const condition = isVar ? manager.trueExpr : manager.mkApp(recognizer, [term], SORT_BOOL);
|
|
2512
|
+
manager.incRef(condition);
|
|
2513
|
+
manager.decRef(shifted);
|
|
2514
|
+
const substituted = subst.apply(bodies[index], substitution);
|
|
2515
|
+
const shifter = new InvVarShifter(manager);
|
|
2516
|
+
const body = shifter.shift(substituted, substitution.length);
|
|
2517
|
+
manager.decRef(substituted);
|
|
2518
|
+
shifter.release();
|
|
2519
|
+
const args = [condition, body, result];
|
|
2520
|
+
const next = result === -1 ? body : mkApp(OP_ITE, args, at, getDomainSort(OP_ITE, args, at));
|
|
2521
|
+
manager.incRef(next);
|
|
2522
|
+
if (result !== -1) manager.decRef(result);
|
|
2523
|
+
result = next;
|
|
2524
|
+
manager.decRef(body);
|
|
2525
|
+
manager.decRef(condition);
|
|
2526
|
+
for (const expr of substitution) manager.decRef(expr);
|
|
2527
|
+
}
|
|
2528
|
+
subst.release();
|
|
2529
|
+
return result;
|
|
2530
|
+
};
|
|
2531
|
+
|
|
2298
2532
|
// parse_expr. Z3 leaves the term on its stack until the command ends. A tail term is one whose
|
|
2299
2533
|
// value is the value of the whole: it stays off the stack, in parsed, so that the caller of
|
|
2300
2534
|
// parseTerm can have a term that nothing else holds without a reference of the parser's.
|
|
@@ -2329,6 +2563,7 @@ export const createSmtlibReader = (
|
|
|
2329
2563
|
// Z3 skips a bad pattern as an s-expression, which cannot start with ')'.
|
|
2330
2564
|
if (value.kind === ")")
|
|
2331
2565
|
throw new SmtParseError("invalid s-expression, unexpected ')'", value);
|
|
2566
|
+
expectClosed(value);
|
|
2332
2567
|
frame.next++;
|
|
2333
2568
|
exprStack.push(-1);
|
|
2334
2569
|
return;
|
|
@@ -2396,12 +2631,25 @@ export const createSmtlibReader = (
|
|
|
2396
2631
|
deliver(parseRootObj(current), isTail, exprStack.length);
|
|
2397
2632
|
return;
|
|
2398
2633
|
}
|
|
2399
|
-
if (head.text === "match")
|
|
2634
|
+
if (head.text === "match") {
|
|
2635
|
+
frames.push({
|
|
2636
|
+
kind: "match",
|
|
2637
|
+
list: current,
|
|
2638
|
+
cases: [],
|
|
2639
|
+
isCaseForm: false,
|
|
2640
|
+
depth: -1,
|
|
2641
|
+
isTail,
|
|
2642
|
+
start: exprStack.length,
|
|
2643
|
+
next: -1,
|
|
2644
|
+
});
|
|
2645
|
+
enter(current.items[1] ?? current.end, false);
|
|
2646
|
+
return;
|
|
2647
|
+
}
|
|
2400
2648
|
if (head.text === "let") {
|
|
2401
2649
|
const bindings = current.items[1];
|
|
2402
2650
|
if (bindings?.kind !== "list")
|
|
2403
2651
|
throw new SmtParseError(
|
|
2404
|
-
`invalid let declaration, '(' expected got ${
|
|
2652
|
+
`invalid let declaration, '(' expected got ${(bindings ?? current.end).lastId}`,
|
|
2405
2653
|
bindings ?? current.end,
|
|
2406
2654
|
);
|
|
2407
2655
|
frames.push({
|
|
@@ -2444,7 +2692,7 @@ export const createSmtlibReader = (
|
|
|
2444
2692
|
}
|
|
2445
2693
|
// push_quant_frame.
|
|
2446
2694
|
const start = exprStack.length;
|
|
2447
|
-
const bound = bindSortedVars(current.items[1], current.end
|
|
2695
|
+
const bound = bindSortedVars(current.items[1], current.end);
|
|
2448
2696
|
if (bound.varSorts.length === 0)
|
|
2449
2697
|
throw new SmtParseError(
|
|
2450
2698
|
"invalid quantifier, list of sorted variables is empty",
|
|
@@ -2476,7 +2724,7 @@ export const createSmtlibReader = (
|
|
|
2476
2724
|
// push_let_decl_frame.
|
|
2477
2725
|
if (declaration.kind !== "list")
|
|
2478
2726
|
throw new SmtParseError(
|
|
2479
|
-
`invalid let declaration, '(' expected got ${
|
|
2727
|
+
`invalid let declaration, '(' expected got ${declaration.lastId}`,
|
|
2480
2728
|
declaration,
|
|
2481
2729
|
);
|
|
2482
2730
|
const name = declaration.items[0];
|
|
@@ -2512,6 +2760,64 @@ export const createSmtlibReader = (
|
|
|
2512
2760
|
continue;
|
|
2513
2761
|
}
|
|
2514
2762
|
const items = frame.list.items;
|
|
2763
|
+
if (frame.kind === "match") {
|
|
2764
|
+
if (frame.next === -1) {
|
|
2765
|
+
const bindings = items[2] ?? frame.list.end;
|
|
2766
|
+
if (bindings.kind !== "list")
|
|
2767
|
+
throw new SmtParseError(
|
|
2768
|
+
`pattern bindings should be enclosed in a parenthesis got ${bindings.lastId}`,
|
|
2769
|
+
bindings,
|
|
2770
|
+
);
|
|
2771
|
+
// curr_id_is_case reads the last identifier, which a '(' leaves as it was.
|
|
2772
|
+
frame.isCaseForm = (bindings.items[0] ?? bindings.end).lastId === "case";
|
|
2773
|
+
frame.cases = frame.isCaseForm ? items.slice(2) : bindings.items;
|
|
2774
|
+
frame.next = 0;
|
|
2775
|
+
} else if (frame.depth !== -1) {
|
|
2776
|
+
binders.depth = frame.depth;
|
|
2777
|
+
frame.depth = -1;
|
|
2778
|
+
endScope();
|
|
2779
|
+
const binding = frame.cases[frame.next - 1];
|
|
2780
|
+
const extra =
|
|
2781
|
+
binding.kind === "list" ? binding.items[frame.isCaseForm ? 3 : 2] : undefined;
|
|
2782
|
+
if (extra !== undefined)
|
|
2783
|
+
throw new SmtParseError(
|
|
2784
|
+
`invalid pattern binding, ')' expected got ${extra.lastId}`,
|
|
2785
|
+
extra,
|
|
2786
|
+
);
|
|
2787
|
+
}
|
|
2788
|
+
const binding = frame.cases[frame.next];
|
|
2789
|
+
if (binding !== undefined) {
|
|
2790
|
+
if (frame.isCaseForm && !(binding.kind === "list" && isSymbol(binding.items[0], "case")))
|
|
2791
|
+
throw new SmtUnsupportedError("the terms after the cases of match");
|
|
2792
|
+
if (binding.kind !== "list")
|
|
2793
|
+
throw new SmtParseError(
|
|
2794
|
+
`invalid pattern binding, '(' expected got ${binding.lastId}`,
|
|
2795
|
+
binding,
|
|
2796
|
+
);
|
|
2797
|
+
frame.next++;
|
|
2798
|
+
beginScope();
|
|
2799
|
+
frame.depth = binders.depth;
|
|
2800
|
+
const offset = frame.isCaseForm ? 1 : 0;
|
|
2801
|
+
const body = binding.items[offset + 1] ?? binding.end;
|
|
2802
|
+
const sort = manager.sorts[exprStack[frame.start]];
|
|
2803
|
+
pushRef(exprStack, parseMatchPattern(binding.items[offset] ?? binding.end, sort, body));
|
|
2804
|
+
enter(body, false);
|
|
2805
|
+
continue;
|
|
2806
|
+
}
|
|
2807
|
+
if (!frame.isCaseForm && items.length > 3)
|
|
2808
|
+
throw new SmtUnsupportedError("the terms after the cases of match");
|
|
2809
|
+
frames.pop();
|
|
2810
|
+
const term = exprStack[frame.start];
|
|
2811
|
+
const cases = exprStack.slice(frame.start + 1);
|
|
2812
|
+
const patterns = cases.filter((_expr, index) => index % 2 === 0);
|
|
2813
|
+
const bodies = cases.filter((_expr, index) => index % 2 === 1);
|
|
2814
|
+
const result = compileMatch(term, patterns, bodies, frame.list.end);
|
|
2815
|
+
// push_match_frame releases the bodies, then the patterns, then the term.
|
|
2816
|
+
exprStack.splice(frame.start, cases.length + 1, ...bodies, ...patterns, term);
|
|
2817
|
+
deliver(result, frame.isTail, frame.start);
|
|
2818
|
+
manager.decRef(result);
|
|
2819
|
+
continue;
|
|
2820
|
+
}
|
|
2515
2821
|
if (frame.kind === "attribute") {
|
|
2516
2822
|
// consume_attributes.
|
|
2517
2823
|
if (exprStack.length === frame.start) {
|
|
@@ -2574,6 +2880,7 @@ export const createSmtlibReader = (
|
|
|
2574
2880
|
// Z3 warns of an unknown attribute and skips its value as an s-expression.
|
|
2575
2881
|
if (value.kind === ")")
|
|
2576
2882
|
throw new SmtParseError("invalid s-expression, unexpected ')'", value);
|
|
2883
|
+
expectClosed(value);
|
|
2577
2884
|
frame.next += 2;
|
|
2578
2885
|
} else if (quantifier === undefined)
|
|
2579
2886
|
throw new SmtParseError("invalid attribute, not in the scope of a quantifier", keyword);
|
|
@@ -2653,13 +2960,13 @@ export const createSmtlibReader = (
|
|
|
2653
2960
|
return expr;
|
|
2654
2961
|
};
|
|
2655
2962
|
|
|
2656
|
-
const parseDomain = (sexpr: SExpr
|
|
2963
|
+
const parseDomain = (sexpr: SExpr) => {
|
|
2657
2964
|
const context = "Parsing function declaration. Expecting sort list '('";
|
|
2658
|
-
return
|
|
2965
|
+
return expectListNext(sexpr, context).items.map((item) => parseSort(item, context));
|
|
2659
2966
|
};
|
|
2660
2967
|
|
|
2661
|
-
const parseSortParameters = (sexpr: SExpr
|
|
2662
|
-
|
|
2968
|
+
const parseSortParameters = (sexpr: SExpr, message: string) =>
|
|
2969
|
+
expectListNext(sexpr, "invalid sort declaration, parameters missing").items.map(
|
|
2663
2970
|
(item) => expectSymbol(item, message, item).text,
|
|
2664
2971
|
);
|
|
2665
2972
|
|
|
@@ -2673,8 +2980,12 @@ export const createSmtlibReader = (
|
|
|
2673
2980
|
missing: string[],
|
|
2674
2981
|
) => {
|
|
2675
2982
|
if (sexpr.kind === "list") return parseSort(sexpr, "", bindings);
|
|
2983
|
+
if (sexpr.kind === ")") throw new SmtParseError("invalid sort, unexpected ')'", sexpr);
|
|
2676
2984
|
if (sexpr.kind !== "symbol")
|
|
2677
|
-
throw new SmtParseError(
|
|
2985
|
+
throw new SmtParseError(
|
|
2986
|
+
`invalid sort, symbol, '_' or '(' expected got ${sexpr.lastId}`,
|
|
2987
|
+
sexpr,
|
|
2988
|
+
);
|
|
2678
2989
|
if (sortDecls.has(sexpr.text) || bindings.has(sexpr.text))
|
|
2679
2990
|
return parseSort(sexpr, "", bindings);
|
|
2680
2991
|
const index = datatypeIndexes.get(sexpr.text);
|
|
@@ -2697,21 +3008,16 @@ export const createSmtlibReader = (
|
|
|
2697
3008
|
);
|
|
2698
3009
|
return items.map((item) => {
|
|
2699
3010
|
if (isSymbol(item)) return { name: item.text, recognizer: `is-${item.text}`, accessors: [] };
|
|
2700
|
-
const constructor =
|
|
2701
|
-
item,
|
|
2702
|
-
"invalid datatype declaration, '(' or ')' expected",
|
|
2703
|
-
item,
|
|
2704
|
-
);
|
|
3011
|
+
const constructor = expectListNext(item, "invalid datatype declaration, '(' or ')' expected");
|
|
2705
3012
|
const name = expectSymbol(
|
|
2706
3013
|
constructor.items[0],
|
|
2707
3014
|
"invalid constructor declaration, symbol (constructor name) expected",
|
|
2708
3015
|
constructor,
|
|
2709
3016
|
).text;
|
|
2710
3017
|
const accessors = constructor.items.slice(1).map((accessorItem) => {
|
|
2711
|
-
const accessor =
|
|
3018
|
+
const accessor = expectListNext(
|
|
2712
3019
|
accessorItem,
|
|
2713
3020
|
"invalid datatype declaration, '(' or ')' expected",
|
|
2714
|
-
accessorItem,
|
|
2715
3021
|
);
|
|
2716
3022
|
const accessorName = expectSymbol(
|
|
2717
3023
|
accessor.items[0],
|
|
@@ -2725,7 +3031,10 @@ export const createSmtlibReader = (
|
|
|
2725
3031
|
missing,
|
|
2726
3032
|
);
|
|
2727
3033
|
if (accessor.items.length > 2)
|
|
2728
|
-
throw new SmtParseError(
|
|
3034
|
+
throw new SmtParseError(
|
|
3035
|
+
`invalid accessor declaration, ')' expected got ${accessor.items[2].lastId}`,
|
|
3036
|
+
accessor.items[2],
|
|
3037
|
+
);
|
|
2729
3038
|
return { name: accessorName, range };
|
|
2730
3039
|
});
|
|
2731
3040
|
return { name, recognizer: `is-${name}`, accessors };
|
|
@@ -2733,27 +3042,29 @@ export const createSmtlibReader = (
|
|
|
2733
3042
|
};
|
|
2734
3043
|
|
|
2735
3044
|
const parseDatatypeBody = (
|
|
2736
|
-
sexpr: SExpr
|
|
3045
|
+
sexpr: SExpr,
|
|
2737
3046
|
datatypeIndexes: ReadonlyMap<string, number>,
|
|
2738
3047
|
missing: string[],
|
|
2739
|
-
|
|
3048
|
+
name?: string,
|
|
2740
3049
|
) => {
|
|
2741
|
-
const body =
|
|
2742
|
-
if (!isSymbol(body.items[0], "par"))
|
|
3050
|
+
const body = expectListNext(sexpr, "invalid datatype declaration, '(' expected");
|
|
3051
|
+
if (!isSymbol(body.items[0], "par")) {
|
|
3052
|
+
if (name !== undefined) declareDatatypeSort(name, 0, body.items[0] ?? body.end);
|
|
2743
3053
|
return {
|
|
2744
3054
|
parameters: [],
|
|
2745
3055
|
constructors: parseConstructors(body.items, NO_BINDINGS, datatypeIndexes, missing, body),
|
|
2746
3056
|
};
|
|
3057
|
+
}
|
|
2747
3058
|
const parameters = parseSortParameters(
|
|
2748
|
-
body.items[1],
|
|
3059
|
+
body.items[1] ?? body.end,
|
|
2749
3060
|
"invalid sort parameter, symbol or ')' expected",
|
|
2750
|
-
body,
|
|
2751
3061
|
);
|
|
2752
|
-
const constructors =
|
|
2753
|
-
body.items[2],
|
|
3062
|
+
const constructors = expectListNext(
|
|
3063
|
+
body.items[2] ?? body.end,
|
|
2754
3064
|
"invalid constructor declaration after par, '(' expected",
|
|
2755
|
-
body,
|
|
2756
3065
|
);
|
|
3066
|
+
if (name !== undefined && parameters.length > 0)
|
|
3067
|
+
declareDatatypeSort(name, parameters.length, constructors.items[0] ?? constructors.end);
|
|
2757
3068
|
return {
|
|
2758
3069
|
parameters: parameters.map((_name, index) => manager.mkTypeParameter(index)),
|
|
2759
3070
|
constructors: parseConstructors(
|
|
@@ -2793,11 +3104,17 @@ export const createSmtlibReader = (
|
|
|
2793
3104
|
declare(sortDecls, name, decl);
|
|
2794
3105
|
};
|
|
2795
3106
|
|
|
3107
|
+
const declareDatatypeSort = (name: string, arity: number, at: SExpr) =>
|
|
3108
|
+
declareSortDecl(
|
|
3109
|
+
name,
|
|
3110
|
+
{ kind: "datatype", name, arity, siblings: [], instances: new Map() },
|
|
3111
|
+
at,
|
|
3112
|
+
);
|
|
3113
|
+
|
|
2796
3114
|
const declareDatatypes = (list: SmtList) => {
|
|
2797
|
-
const declaration =
|
|
2798
|
-
list.items[1],
|
|
3115
|
+
const declaration = expectListNext(
|
|
3116
|
+
list.items[1] ?? list.end,
|
|
2799
3117
|
"invalid sort declaration, parameters missing",
|
|
2800
|
-
list,
|
|
2801
3118
|
);
|
|
2802
3119
|
const isSmt26 = declaration.items[0]?.kind === "list";
|
|
2803
3120
|
const datatypeIndexes = new Map<string, number>();
|
|
@@ -2811,11 +3128,11 @@ export const createSmtlibReader = (
|
|
|
2811
3128
|
);
|
|
2812
3129
|
return;
|
|
2813
3130
|
}
|
|
2814
|
-
const sortDeclaration =
|
|
3131
|
+
const sortDeclaration = expectListNext(item, "invalid sort declaration, '(' expected");
|
|
2815
3132
|
const name = expectSymbol(
|
|
2816
3133
|
sortDeclaration.items[0],
|
|
2817
3134
|
"invalid sort name, identified expected",
|
|
2818
|
-
sortDeclaration,
|
|
3135
|
+
sortDeclaration.end,
|
|
2819
3136
|
).text;
|
|
2820
3137
|
const arity = sortDeclaration.items[1];
|
|
2821
3138
|
if (arity?.kind !== "numeral")
|
|
@@ -2823,26 +3140,25 @@ export const createSmtlibReader = (
|
|
|
2823
3140
|
"invalid sort declaration, arity expected",
|
|
2824
3141
|
arity ?? sortDeclaration.end,
|
|
2825
3142
|
);
|
|
2826
|
-
if (sortDeclaration.items.length > 2)
|
|
2827
|
-
throw new SmtParseError("invalid sort declaration, ')' expected", sortDeclaration.items[2]);
|
|
2828
3143
|
datatypeIndexes.set(name, index);
|
|
2829
3144
|
names.push(name);
|
|
2830
3145
|
arities.push(Number(arity.text));
|
|
2831
|
-
|
|
3146
|
+
declareDatatypeSort(
|
|
2832
3147
|
name,
|
|
2833
|
-
|
|
2834
|
-
sortDeclaration,
|
|
3148
|
+
Number(arity.text),
|
|
3149
|
+
sortDeclaration.items[2] ?? sortDeclaration.end,
|
|
2835
3150
|
);
|
|
3151
|
+
if (sortDeclaration.items.length > 2)
|
|
3152
|
+
throw new SmtParseError("invalid sort declaration, ')' expected", sortDeclaration.items[2]);
|
|
2836
3153
|
});
|
|
2837
|
-
const bodies =
|
|
3154
|
+
const bodies = expectListNext(
|
|
3155
|
+
list.items[2] ?? list.end,
|
|
3156
|
+
"invalid datatype declaration, '(' expected",
|
|
3157
|
+
);
|
|
2838
3158
|
// A datatype of the older syntax may refer to any datatype of its declaration.
|
|
2839
3159
|
if (!isSmt26)
|
|
2840
3160
|
bodies.items.forEach((item, index) => {
|
|
2841
|
-
const datatype =
|
|
2842
|
-
item,
|
|
2843
|
-
"invalid datatype declaration, '(' or ')' expected",
|
|
2844
|
-
item,
|
|
2845
|
-
);
|
|
3161
|
+
const datatype = expectListNext(item, "invalid datatype declaration, '(' or ')' expected");
|
|
2846
3162
|
const name = expectSymbol(
|
|
2847
3163
|
datatype.items[0],
|
|
2848
3164
|
"invalid datatype declaration, symbol (datatype name) expected",
|
|
@@ -2867,10 +3183,10 @@ export const createSmtlibReader = (
|
|
|
2867
3183
|
Array.from({ length: arities[index] }, (_unused, parameter) =>
|
|
2868
3184
|
manager.mkTypeParameter(parameter),
|
|
2869
3185
|
),
|
|
2870
|
-
parseDatatypeBody(item, datatypeIndexes, missing
|
|
3186
|
+
parseDatatypeBody(item, datatypeIndexes, missing).constructors,
|
|
2871
3187
|
);
|
|
2872
3188
|
}
|
|
2873
|
-
const datatype =
|
|
3189
|
+
const datatype = expectListNext(item, "invalid datatype declaration, '(' or ')' expected");
|
|
2874
3190
|
return mkDatatypeDef(
|
|
2875
3191
|
manager,
|
|
2876
3192
|
names[index],
|
|
@@ -2903,29 +3219,21 @@ export const createSmtlibReader = (
|
|
|
2903
3219
|
};
|
|
2904
3220
|
|
|
2905
3221
|
const declareDatatype = (list: SmtList) => {
|
|
2906
|
-
const name = expectSymbol(list.items[1], "unexpected token used as datatype name", list);
|
|
3222
|
+
const name = expectSymbol(list.items[1], "unexpected token used as datatype name", list.end);
|
|
2907
3223
|
const missing: string[] = [];
|
|
2908
|
-
const
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
bodyList.items[1]?.kind === "list"
|
|
2913
|
-
? bodyList.items[1].items.length
|
|
2914
|
-
: 0;
|
|
2915
|
-
declareSortDecl(
|
|
3224
|
+
const body = parseDatatypeBody(
|
|
3225
|
+
list.items[2] ?? list.end,
|
|
3226
|
+
new Map([[name.text, 0]]),
|
|
3227
|
+
missing,
|
|
2916
3228
|
name.text,
|
|
2917
|
-
{ kind: "datatype", name: name.text, arity, siblings: [], instances: new Map() },
|
|
2918
|
-
name,
|
|
2919
3229
|
);
|
|
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
3230
|
commitDatatypes(
|
|
2924
3231
|
[mkDatatypeDef(manager, name.text, body.parameters, body.constructors)],
|
|
2925
3232
|
missing,
|
|
2926
3233
|
name,
|
|
2927
|
-
list.end,
|
|
3234
|
+
list.items[3] ?? list.end,
|
|
2928
3235
|
);
|
|
3236
|
+
expectEnd(list.items[3], "invalid end of datatype declaration, ')' expected");
|
|
2929
3237
|
};
|
|
2930
3238
|
|
|
2931
3239
|
// parse_rec_fun_decl: the variables are bound only while the range is parsed, and Z3 holds them
|
|
@@ -2936,7 +3244,7 @@ export const createSmtlibReader = (
|
|
|
2936
3244
|
"invalid function/constant definition, symbol expected",
|
|
2937
3245
|
at,
|
|
2938
3246
|
);
|
|
2939
|
-
const bound = bindSortedVars(items[1], at
|
|
3247
|
+
const bound = bindSortedVars(items[1], at);
|
|
2940
3248
|
const range = parseSort(items[2] ?? at, "Invalid recursive function definition");
|
|
2941
3249
|
unbindSortedVars(bound.varSorts.length);
|
|
2942
3250
|
return manager.mkRecFuncDecl(symbol.text, bound.varSorts, range);
|
|
@@ -2945,7 +3253,7 @@ export const createSmtlibReader = (
|
|
|
2945
3253
|
// parse_rec_fun_body: Z3 reports a body of another sort at the token after it.
|
|
2946
3254
|
const defineRecFun = (decl: number, variables: SExpr, body: SExpr, next: SExpr) => {
|
|
2947
3255
|
const start = exprStack.length;
|
|
2948
|
-
const bound = bindSortedVars(variables, body
|
|
3256
|
+
const bound = bindSortedVars(variables, body);
|
|
2949
3257
|
runTerm(body, false);
|
|
2950
3258
|
const parsedBody = exprStack[exprStack.length - 1];
|
|
2951
3259
|
unbindSortedVars(bound.varSorts.length);
|
|
@@ -2973,12 +3281,15 @@ export const createSmtlibReader = (
|
|
|
2973
3281
|
subst.release();
|
|
2974
3282
|
}
|
|
2975
3283
|
for (const [index, sort] of domain.entries()) manager.incRef(manager.mkVar(index, sort));
|
|
2976
|
-
const reversedVars = domain.map((
|
|
3284
|
+
const reversedVars = domain.map((_value, index) =>
|
|
3285
|
+
manager.mkVar(index, domain[count - index - 1]),
|
|
3286
|
+
);
|
|
2977
3287
|
for (const reversedVar of reversedVars) manager.incRef(reversedVar);
|
|
2978
3288
|
const decl = manager.mkRecFuncDecl(name, domain, manager.sorts[body]);
|
|
2979
3289
|
for (const reversedVar of reversedVars) manager.decRef(reversedVar);
|
|
2980
3290
|
setRecFunDefinition(manager, manager.recFunDefs.get(decl)!, true, body);
|
|
2981
3291
|
declare(funcs, name, [...(funcs.get(name) ?? []), decl]);
|
|
3292
|
+
scopedFuncs.push(decl);
|
|
2982
3293
|
};
|
|
2983
3294
|
|
|
2984
3295
|
const declareCommand = (list: SmtList) => {
|
|
@@ -2988,6 +3299,7 @@ export const createSmtlibReader = (
|
|
|
2988
3299
|
const decl = declareRecFun(list.items.slice(1), list.end);
|
|
2989
3300
|
insertFunc(decl, list.items[4] ?? list.end);
|
|
2990
3301
|
defineRecFun(decl, list.items[2], list.items[4] ?? list.end, list.items[5] ?? list.end);
|
|
3302
|
+
expectEnd(list.items[5], "invalid function/constant definition, ')' expected");
|
|
2991
3303
|
shrinkRefs(exprStack, 0);
|
|
2992
3304
|
return;
|
|
2993
3305
|
}
|
|
@@ -3016,9 +3328,11 @@ export const createSmtlibReader = (
|
|
|
3016
3328
|
bodies.items[index + 1] ?? bodies.end,
|
|
3017
3329
|
);
|
|
3018
3330
|
});
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3331
|
+
expectEnd(
|
|
3332
|
+
bodies.items[signatures.length],
|
|
3333
|
+
"invalid recursive function definition, ')' expected",
|
|
3334
|
+
);
|
|
3335
|
+
expectEnd(list.items[3], "invalid function/constant definition, ')' expected");
|
|
3022
3336
|
shrinkRefs(exprStack, 0);
|
|
3023
3337
|
return;
|
|
3024
3338
|
}
|
|
@@ -3030,7 +3344,26 @@ export const createSmtlibReader = (
|
|
|
3030
3344
|
declareDatatype(list);
|
|
3031
3345
|
return;
|
|
3032
3346
|
}
|
|
3033
|
-
const symbol =
|
|
3347
|
+
const symbol = list.items[1];
|
|
3348
|
+
// check_nonreserved_identifier rejects _ and as, which define-const's check_identifier takes.
|
|
3349
|
+
if (
|
|
3350
|
+
!isSymbol(symbol) ||
|
|
3351
|
+
(name !== "define-const" && (symbol.text === "_" || symbol.text === "as"))
|
|
3352
|
+
)
|
|
3353
|
+
throw new SmtParseError(
|
|
3354
|
+
name === "declare-sort"
|
|
3355
|
+
? "invalid sort declaration, symbol expected"
|
|
3356
|
+
: name === "define-sort"
|
|
3357
|
+
? "invalid sort definition, symbol expected"
|
|
3358
|
+
: name === "define-fun"
|
|
3359
|
+
? "invalid function/constant definition, symbol expected"
|
|
3360
|
+
: name === "define-const"
|
|
3361
|
+
? "invalid constant definition, symbol expected"
|
|
3362
|
+
: name === "declare-fun"
|
|
3363
|
+
? "invalid function declaration, symbol expected"
|
|
3364
|
+
: "invalid constant declaration, symbol expected",
|
|
3365
|
+
symbol ?? list.end,
|
|
3366
|
+
);
|
|
3034
3367
|
if (name === "declare-sort") {
|
|
3035
3368
|
if (sortDecls.has(symbol.text))
|
|
3036
3369
|
throw new SmtParseError("invalid sort declaration, sort already declared/defined", symbol);
|
|
@@ -3040,8 +3373,6 @@ export const createSmtlibReader = (
|
|
|
3040
3373
|
"invalid sort declaration, arity (<numeral>) or ')' expected",
|
|
3041
3374
|
arity,
|
|
3042
3375
|
);
|
|
3043
|
-
if (list.items.length > 3)
|
|
3044
|
-
throw new SmtParseError("invalid sort declaration, ')' expected", list.items[3]);
|
|
3045
3376
|
declare(
|
|
3046
3377
|
sortDecls,
|
|
3047
3378
|
symbol.text,
|
|
@@ -3049,26 +3380,25 @@ export const createSmtlibReader = (
|
|
|
3049
3380
|
? { kind: "sort", sort: manager.mkSort(symbol.text) }
|
|
3050
3381
|
: { kind: "user", name: symbol.text, arity: Number(arity.text) },
|
|
3051
3382
|
);
|
|
3383
|
+
expectEnd(list.items[3], "invalid sort declaration, ')' expected");
|
|
3052
3384
|
return;
|
|
3053
3385
|
}
|
|
3054
3386
|
if (name === "define-sort") {
|
|
3055
3387
|
if (sortDecls.has(symbol.text))
|
|
3056
3388
|
throw new SmtParseError("invalid sort definition, sort already declared/defined", symbol);
|
|
3057
3389
|
const parameters = parseSortParameters(
|
|
3058
|
-
list.items[2],
|
|
3390
|
+
list.items[2] ?? list.end,
|
|
3059
3391
|
"invalid sort parameter, symbol or ')' expected",
|
|
3060
|
-
list,
|
|
3061
3392
|
);
|
|
3062
3393
|
const body = list.items[3] ?? list.end;
|
|
3063
3394
|
checkPsort(body, bindTypeParameters(parameters));
|
|
3064
3395
|
declare(sortDecls, symbol.text, { kind: "definition", parameters, body });
|
|
3396
|
+
expectEnd(list.items[4], "invalid sort definition, ')' expected");
|
|
3065
3397
|
return;
|
|
3066
3398
|
}
|
|
3067
3399
|
if (name === "define-fun" || name === "define-const") {
|
|
3068
3400
|
const isConst = name === "define-const";
|
|
3069
|
-
const bound = isConst
|
|
3070
|
-
? { names: [], varSorts: [] }
|
|
3071
|
-
: bindSortedVars(list.items[2], list.end, symbol.text);
|
|
3401
|
+
const bound = isConst ? { names: [], varSorts: [] } : bindSortedVars(list.items[2], list.end);
|
|
3072
3402
|
const range = parseSort(
|
|
3073
3403
|
list.items[isConst ? 2 : 3] ?? list.end,
|
|
3074
3404
|
isConst ? "Invalid constant definition" : "Invalid function definition",
|
|
@@ -3076,20 +3406,27 @@ export const createSmtlibReader = (
|
|
|
3076
3406
|
runTerm(list.items[isConst ? 3 : 4] ?? list.end, false);
|
|
3077
3407
|
const body = exprStack[exprStack.length - 1];
|
|
3078
3408
|
if (!isConst) unbindSortedVars(bound.varSorts.length);
|
|
3409
|
+
const extra = list.items[isConst ? 4 : 5];
|
|
3079
3410
|
if (manager.sorts[body] !== range)
|
|
3080
3411
|
throw new SmtParseError(
|
|
3081
3412
|
isConst
|
|
3082
3413
|
? "invalid constant definition, sort mismatch"
|
|
3083
3414
|
: "invalid function/constant definition, sort mismatch",
|
|
3084
|
-
list.end,
|
|
3415
|
+
extra ?? list.end,
|
|
3085
3416
|
);
|
|
3086
3417
|
manager.incRef(body);
|
|
3087
|
-
|
|
3418
|
+
insertMacro(symbol.text, { domain: bound.varSorts, range, body });
|
|
3088
3419
|
defineMacroFunc(symbol.text, bound.varSorts, body);
|
|
3420
|
+
expectEnd(
|
|
3421
|
+
extra,
|
|
3422
|
+
isConst
|
|
3423
|
+
? "invalid constant definition, ')' expected"
|
|
3424
|
+
: "invalid function/constant definition, ')' expected",
|
|
3425
|
+
);
|
|
3089
3426
|
shrinkRefs(exprStack, 0);
|
|
3090
3427
|
return;
|
|
3091
3428
|
}
|
|
3092
|
-
const domain = name === "declare-fun" ? parseDomain(list.items[2]
|
|
3429
|
+
const domain = name === "declare-fun" ? parseDomain(list.items[2] ?? list.end) : [];
|
|
3093
3430
|
const range = parseSort(
|
|
3094
3431
|
list.items[name === "declare-fun" ? 3 : 2] ?? list.end,
|
|
3095
3432
|
name === "declare-fun" ? "Invalid function declaration" : "Invalid constant declaration",
|
|
@@ -3101,45 +3438,60 @@ export const createSmtlibReader = (
|
|
|
3101
3438
|
for (const domainVar of vars) manager.decRef(domainVar);
|
|
3102
3439
|
}
|
|
3103
3440
|
const decl = manager.mkFuncDecl(symbol.text, domain, range);
|
|
3104
|
-
|
|
3441
|
+
const extra = list.items[name === "declare-fun" ? 4 : 3];
|
|
3442
|
+
checkDecl(decl, extra ?? list.end);
|
|
3105
3443
|
if (macros.has(symbol.text))
|
|
3106
3444
|
throw new SmtUnsupportedError(`the overloaded symbol ${symbol.text}`);
|
|
3107
3445
|
const existing = funcs.get(symbol.text) ?? [];
|
|
3108
3446
|
if (existing.some((other) => hasSignature(other, domain, range)))
|
|
3109
3447
|
throw new SmtParseError(
|
|
3110
3448
|
`invalid declaration, ${domain.length === 0 ? "constant" : "function"} '${symbol.text}' (with the given signature) already declared`,
|
|
3111
|
-
list.end,
|
|
3449
|
+
extra ?? list.end,
|
|
3112
3450
|
);
|
|
3113
3451
|
declare(funcs, symbol.text, [...existing, decl]);
|
|
3452
|
+
expectEnd(
|
|
3453
|
+
extra,
|
|
3454
|
+
name === "declare-fun"
|
|
3455
|
+
? "invalid function declaration, ')' expected"
|
|
3456
|
+
: "invalid constant declaration, ')' expected",
|
|
3457
|
+
);
|
|
3114
3458
|
};
|
|
3115
3459
|
|
|
3116
|
-
// The caller takes the reference
|
|
3117
|
-
const parseFormula = (
|
|
3460
|
+
// The caller takes the stack's reference. Z3 reports a non-Boolean term at the token after it.
|
|
3461
|
+
const parseFormula = (
|
|
3462
|
+
sexpr: SExpr,
|
|
3463
|
+
follower: SmtPosition,
|
|
3464
|
+
message = "invalid assert command, term is not Boolean",
|
|
3465
|
+
) => {
|
|
3118
3466
|
runTerm(sexpr, false);
|
|
3119
3467
|
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);
|
|
3468
|
+
if (manager.sorts[assertion] !== SORT_BOOL) throw new SmtParseError(message, follower);
|
|
3122
3469
|
exprStack.pop();
|
|
3123
3470
|
return assertion;
|
|
3124
3471
|
};
|
|
3125
3472
|
|
|
3126
3473
|
// An assertion is named when the last name of its term is its own.
|
|
3127
|
-
const parseAssertion = (sexpr: SExpr) => {
|
|
3474
|
+
const parseAssertion = (sexpr: SExpr, follower: SmtPosition) => {
|
|
3128
3475
|
releasePending();
|
|
3129
3476
|
lastNamed.expr = -1;
|
|
3130
|
-
const formula = parseFormula(sexpr);
|
|
3477
|
+
const formula = parseFormula(sexpr, follower);
|
|
3131
3478
|
return { formula, name: lastNamed.expr === formula ? lastNamed.name : undefined };
|
|
3132
3479
|
};
|
|
3133
3480
|
|
|
3134
3481
|
const push = () => {
|
|
3135
3482
|
releasePending();
|
|
3136
3483
|
scopeStarts.push(undo.length);
|
|
3484
|
+
scopedFuncStarts.push(scopedFuncs.length);
|
|
3137
3485
|
};
|
|
3138
3486
|
|
|
3139
|
-
const pop = () => {
|
|
3487
|
+
const pop = (count: number) => {
|
|
3140
3488
|
releasePending();
|
|
3141
|
-
const
|
|
3489
|
+
const funcStart = scopedFuncStarts.splice(scopedFuncStarts.length - count)[0];
|
|
3490
|
+
for (const decl of scopedFuncs.splice(funcStart)) manager.eraseRecFunDef(decl);
|
|
3491
|
+
const start = scopeStarts.splice(scopeStarts.length - count)[0];
|
|
3142
3492
|
for (const restore of undo.splice(start).reverse()) restore();
|
|
3493
|
+
// cmd_context::pop resets the plugin through its datatype handler.
|
|
3494
|
+
resetDatatypePlugin(manager);
|
|
3143
3495
|
};
|
|
3144
3496
|
|
|
3145
3497
|
// parser::reset, after an error: Z3 frees its stacks, and with them the terms that only the
|
|
@@ -3210,9 +3562,15 @@ export const createSmtlibReader = (
|
|
|
3210
3562
|
pop,
|
|
3211
3563
|
reset,
|
|
3212
3564
|
restrictSorts,
|
|
3565
|
+
registerList,
|
|
3213
3566
|
isUsed,
|
|
3214
3567
|
isClashing,
|
|
3215
3568
|
getDeclaredFuncs,
|
|
3569
|
+
getConstantMacros: () =>
|
|
3570
|
+
[...macroDictionary.values()].flatMap((name) => {
|
|
3571
|
+
const macro = macros.get(name);
|
|
3572
|
+
return macro?.domain.length === 0 ? [{ name, body: macro.body }] : [];
|
|
3573
|
+
}),
|
|
3216
3574
|
};
|
|
3217
3575
|
};
|
|
3218
3576
|
|
|
@@ -3273,7 +3631,10 @@ export const parseSmtlib = (
|
|
|
3273
3631
|
state.reader.declareCommand(list);
|
|
3274
3632
|
} else if (name === "assert") {
|
|
3275
3633
|
state.isInUse = true;
|
|
3276
|
-
const assertion = state.reader.parseFormula(
|
|
3634
|
+
const assertion = state.reader.parseFormula(
|
|
3635
|
+
list.items[1] ?? list.end,
|
|
3636
|
+
list.items[2] ?? list.end,
|
|
3637
|
+
);
|
|
3277
3638
|
const logicError = state.reader.getLogicError(assertion);
|
|
3278
3639
|
if (logicError !== undefined) {
|
|
3279
3640
|
manager.decRef(assertion);
|
|
@@ -3286,7 +3647,7 @@ export const parseSmtlib = (
|
|
|
3286
3647
|
};
|
|
3287
3648
|
for (;;) {
|
|
3288
3649
|
try {
|
|
3289
|
-
const list = commands.
|
|
3650
|
+
const list = commands.readCommand();
|
|
3290
3651
|
if (list === undefined) return { assertions, hasCheckSat: false, rlimit: state.rlimit };
|
|
3291
3652
|
if (isSymbol(list.items[0], "check-sat"))
|
|
3292
3653
|
return { assertions, hasCheckSat: true, rlimit: state.rlimit };
|