tla-ts 0.0.4-dev.6dec595 → 0.0.4-dev.7b5087a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -141
- package/dist/deciders.cjs +366 -0
- package/dist/deciders.js +366 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +35 -1
- package/dist/index.d.ts +35 -1
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -4
- package/dist/parallel-worker.js +1 -4
- package/dist/prove-worker.cjs +9 -0
- package/dist/prove-worker.d.cts +9 -0
- package/dist/prove-worker.d.ts +9 -0
- package/dist/prove-worker.js +9 -0
- package/dist/prove.cjs +996 -0
- package/dist/prove.js +996 -0
- package/dist/run-cli.cjs +517 -47
- package/dist/run-cli.js +517 -47
- package/dist/tlapm.cjs +10 -0
- package/dist/tlapm.js +10 -0
- package/dist/transfer.cjs +38 -44
- package/dist/transfer.js +38 -44
- package/dist/worker.cjs +21 -0
- package/dist/worker.js +21 -0
- package/package.json +21 -12
- package/src/errors.ts +86 -0
- package/src/evaluate.ts +28 -7
- package/src/index.ts +10 -0
- package/src/{obligation-action.ts → obligations/action.ts} +258 -37
- package/src/{obligation-anon.ts → obligations/anon.ts} +13 -7
- package/src/obligations/ctx.ts +64 -0
- package/src/{obligation-deref.ts → obligations/deref.ts} +2 -2
- package/src/{obligation-expression.ts → obligations/expression.ts} +54 -8
- package/src/{obligation-format.ts → obligations/format.ts} +6 -5
- package/src/{obligation-generate.ts → obligations/generate.ts} +134 -61
- package/src/{obligations.ts → obligations/index.ts} +28 -12
- package/src/{obligation-levels.ts → obligations/levels.ts} +12 -2
- package/src/{obligation-module.ts → obligations/module.ts} +46 -20
- package/src/obligations/pickle.ts +100 -0
- package/src/obligations/pragma.ts +189 -0
- package/src/{obligation-printer.ts → obligations/printer.ts} +9 -63
- package/src/{obligation-proof.ts → obligations/proof.ts} +7 -1
- package/src/{obligation-source.ts → obligations/source.ts} +91 -35
- package/src/obligations/temporal.ts +319 -0
- package/src/{obligation-tuples.ts → obligations/tuples.ts} +1 -1
- package/src/ocaml-marshal.ts +172 -0
- package/src/parallel-worker.ts +2 -18
- package/src/parallel.ts +10 -43
- package/src/prove/decide-pool.ts +100 -0
- package/src/prove/deciders.ts +33 -0
- package/src/prove/isabelle/LICENSE +36 -0
- package/src/prove/isabelle/LICENSE-tlapm +26 -0
- package/src/prove/isabelle/decide.ts +47 -0
- package/src/prove/isabelle/encode.ts +512 -0
- package/src/prove/isabelle/isar/inner-syntax.ts +579 -0
- package/src/prove/isabelle/isar/method.ts +367 -0
- package/src/prove/isabelle/isar/proof.ts +28 -0
- package/src/prove/isabelle/isar/statement.ts +57 -0
- package/src/prove/isabelle/isar/theory.ts +192 -0
- package/src/prove/isabelle/isar/type-check.ts +278 -0
- package/src/prove/isabelle/provers/blast/blast.ts +258 -0
- package/src/prove/isabelle/provers/blast/branch.ts +322 -0
- package/src/prove/isabelle/provers/blast/prove.ts +347 -0
- package/src/prove/isabelle/provers/blast/rules.ts +330 -0
- package/src/prove/isabelle/provers/blast/state.ts +180 -0
- package/src/prove/isabelle/provers/blast/term.ts +547 -0
- package/src/prove/isabelle/provers/clasimp.ts +150 -0
- package/src/prove/isabelle/provers/classical.ts +389 -0
- package/src/prove/isabelle/provers/hypsubst.ts +441 -0
- package/src/prove/isabelle/pure/bicompose.ts +174 -0
- package/src/prove/isabelle/pure/consts.ts +32 -0
- package/src/prove/isabelle/pure/conv.ts +79 -0
- package/src/prove/isabelle/pure/drule.ts +264 -0
- package/src/prove/isabelle/pure/envir.ts +363 -0
- package/src/prove/isabelle/pure/goal.ts +53 -0
- package/src/prove/isabelle/pure/interrupt.ts +16 -0
- package/src/prove/isabelle/pure/list.ts +12 -0
- package/src/prove/isabelle/pure/logic.ts +402 -0
- package/src/prove/isabelle/pure/name.ts +110 -0
- package/src/prove/isabelle/pure/net.ts +262 -0
- package/src/prove/isabelle/pure/object-logic.ts +41 -0
- package/src/prove/isabelle/pure/pattern.ts +650 -0
- package/src/prove/isabelle/pure/raw-simplifier.ts +353 -0
- package/src/prove/isabelle/pure/rewriter.ts +602 -0
- package/src/prove/isabelle/pure/search.ts +68 -0
- package/src/prove/isabelle/pure/seq.ts +156 -0
- package/src/prove/isabelle/pure/simplifier.ts +112 -0
- package/src/prove/isabelle/pure/table.ts +208 -0
- package/src/prove/isabelle/pure/tactic.ts +276 -0
- package/src/prove/isabelle/pure/tactical.ts +347 -0
- package/src/prove/isabelle/pure/term-ord.ts +159 -0
- package/src/prove/isabelle/pure/term.ts +808 -0
- package/src/prove/isabelle/pure/thm.ts +806 -0
- package/src/prove/isabelle/pure/type.ts +133 -0
- package/src/prove/isabelle/pure/unify.ts +753 -0
- package/src/prove/isabelle/pure/variable.ts +25 -0
- package/src/prove/isabelle/tla/classical-setup.ts +101 -0
- package/src/prove/isabelle/tla/context.ts +74 -0
- package/src/prove/isabelle/tla/logic-data.ts +5 -0
- package/src/prove/isabelle/tla/logic.ts +215 -0
- package/src/prove/isabelle/tla/simpdata.ts +241 -0
- package/src/prove/isabelle/tools/intuitionistic.ts +132 -0
- package/src/prove/ocaml.ts +22 -0
- package/src/prove/prep/action.ts +520 -0
- package/src/prove/prep/defs.ts +33 -0
- package/src/prove/prep/elab.ts +156 -0
- package/src/prove/prep/enabled-axioms.ts +189 -0
- package/src/prove/prep/enabled.ts +43 -0
- package/src/prove/prep/fingerprint.ts +388 -0
- package/src/prove/prep/flatten.ts +70 -0
- package/src/prove/prep/level-comparison.ts +552 -0
- package/src/prove/prep/md5.ts +67 -0
- package/src/prove/prep/pragma.ts +221 -0
- package/src/prove/prep/trivial.ts +22 -0
- package/src/prove/prove-worker.ts +27 -0
- package/src/prove/prove.ts +528 -0
- package/src/prove/ptl/decide.ts +68 -0
- package/src/prove/ptl/encode.ts +256 -0
- package/src/prove/ptl/sat.ts +151 -0
- package/src/prove/ptl/tableau.ts +276 -0
- package/src/prove/smt/axiomatize.ts +69 -0
- package/src/prove/smt/axioms.ts +210 -0
- package/src/prove/smt/context.ts +108 -0
- package/src/prove/smt/data.ts +665 -0
- package/src/prove/smt/encode.ts +169 -0
- package/src/prove/smt/extensionality.ts +63 -0
- package/src/prove/smt/flatten.ts +127 -0
- package/src/prove/smt/format.ts +143 -0
- package/src/prove/smt/instances.ts +182 -0
- package/src/prove/smt/rewrite.ts +784 -0
- package/src/prove/smt/simplify.ts +57 -0
- package/src/prove/smt/solver/act-cache.ts +265 -0
- package/src/prove/smt/solver/arith-rewriter.ts +962 -0
- package/src/prove/smt/solver/array.ts +22 -0
- package/src/prove/smt/solver/asserted-formulas.ts +469 -0
- package/src/prove/smt/solver/ast-lt.ts +157 -0
- package/src/prove/smt/solver/ast-util.ts +147 -0
- package/src/prove/smt/solver/ast.ts +1315 -0
- package/src/prove/smt/solver/auf-solver.ts +572 -0
- package/src/prove/smt/solver/case-split-queue.ts +178 -0
- package/src/prove/smt/solver/cg-table.ts +175 -0
- package/src/prove/smt/solver/clause.ts +276 -0
- package/src/prove/smt/solver/conflict-resolution.ts +343 -0
- package/src/prove/smt/solver/context.ts +1292 -0
- package/src/prove/smt/solver/ctx-simplify.ts +334 -0
- package/src/prove/smt/solver/der.ts +224 -0
- package/src/prove/smt/solver/dyn-ack.ts +141 -0
- package/src/prove/smt/solver/e-matching.ts +659 -0
- package/src/prove/smt/solver/enode.ts +259 -0
- package/src/prove/smt/solver/expr-ref.ts +21 -0
- package/src/prove/smt/solver/expr-safe-replace.ts +172 -0
- package/src/prove/smt/solver/fingerprints.ts +92 -0
- package/src/prove/smt/solver/flat-literals.ts +51 -0
- package/src/prove/smt/solver/goal.ts +138 -0
- package/src/prove/smt/solver/hash.ts +173 -0
- package/src/prove/smt/solver/hashtable.ts +166 -0
- package/src/prove/smt/solver/internalizer.ts +1381 -0
- package/src/prove/smt/solver/lbool.ts +5 -0
- package/src/prove/smt/solver/literal.ts +24 -0
- package/src/prove/smt/solver/lp/bound-propagator.ts +391 -0
- package/src/prove/smt/solver/lp/constraints.ts +78 -0
- package/src/prove/smt/solver/lp/containers.ts +296 -0
- package/src/prove/smt/solver/lp/core-solver.ts +504 -0
- package/src/prove/smt/solver/lp/cross-nested.ts +224 -0
- package/src/prove/smt/solver/lp/dep-intervals.ts +656 -0
- package/src/prove/smt/solver/lp/dependency.ts +51 -0
- package/src/prove/smt/solver/lp/dioph-eq.ts +1181 -0
- package/src/prove/smt/solver/lp/explanation.ts +46 -0
- package/src/prove/smt/solver/lp/gomory.ts +255 -0
- package/src/prove/smt/solver/lp/hnf-cutter.ts +125 -0
- package/src/prove/smt/solver/lp/hnf.ts +281 -0
- package/src/prove/smt/solver/lp/int-cube.ts +295 -0
- package/src/prove/smt/solver/lp/int-solver.ts +514 -0
- package/src/prove/smt/solver/lp/lar-solver.ts +816 -0
- package/src/prove/smt/solver/lp/lar-term.ts +62 -0
- package/src/prove/smt/solver/lp/lp-settings.ts +91 -0
- package/src/prove/smt/solver/lp/maximize-term.ts +347 -0
- package/src/prove/smt/solver/lp/nex-creator.ts +381 -0
- package/src/prove/smt/solver/lp/nex.ts +120 -0
- package/src/prove/smt/solver/lp/nla-backoff.ts +43 -0
- package/src/prove/smt/solver/lp/nla-basics.ts +398 -0
- package/src/prove/smt/solver/lp/nla-core.ts +968 -0
- package/src/prove/smt/solver/lp/nla-divisions.ts +332 -0
- package/src/prove/smt/solver/lp/nla-emonics.ts +373 -0
- package/src/prove/smt/solver/lp/nla-factorization.ts +30 -0
- package/src/prove/smt/solver/lp/nla-grobner.ts +904 -0
- package/src/prove/smt/solver/lp/nla-horner.ts +126 -0
- package/src/prove/smt/solver/lp/nla-intervals.ts +265 -0
- package/src/prove/smt/solver/lp/nla-monomial-bounds.ts +715 -0
- package/src/prove/smt/solver/lp/nla-monotone.ts +59 -0
- package/src/prove/smt/solver/lp/nla-order.ts +307 -0
- package/src/prove/smt/solver/lp/nla-patcher.ts +112 -0
- package/src/prove/smt/solver/lp/nla-tangents.ts +251 -0
- package/src/prove/smt/solver/lp/nla-throttle.ts +85 -0
- package/src/prove/smt/solver/lp/nla-types.ts +124 -0
- package/src/prove/smt/solver/lp/nla-var-eqs.ts +141 -0
- package/src/prove/smt/solver/lp/nra-solver.ts +440 -0
- package/src/prove/smt/solver/lp/numeric-pair.ts +65 -0
- package/src/prove/smt/solver/lp/pdd-interval.ts +31 -0
- package/src/prove/smt/solver/lp/pdd-simplifier.ts +184 -0
- package/src/prove/smt/solver/lp/pdd-solver.ts +408 -0
- package/src/prove/smt/solver/lp/pdd.ts +971 -0
- package/src/prove/smt/solver/lp/random-updater.ts +138 -0
- package/src/prove/smt/solver/lp/static-matrix.ts +246 -0
- package/src/prove/smt/solver/lp/union-find.ts +96 -0
- package/src/prove/smt/solver/mam-compiler.ts +847 -0
- package/src/prove/smt/solver/mam-interpreter.ts +400 -0
- package/src/prove/smt/solver/maximize-ac-sharing.ts +118 -0
- package/src/prove/smt/solver/model-checker.ts +347 -0
- package/src/prove/smt/solver/model-evaluator.ts +262 -0
- package/src/prove/smt/solver/model-finder.ts +155 -0
- package/src/prove/smt/solver/model-generator.ts +97 -0
- package/src/prove/smt/solver/model-macro-solver.ts +728 -0
- package/src/prove/smt/solver/nlsat/algebraic.ts +1161 -0
- package/src/prove/smt/solver/nlsat/atoms.ts +238 -0
- package/src/prove/smt/solver/nlsat/evaluator.ts +303 -0
- package/src/prove/smt/solver/nlsat/explain.ts +607 -0
- package/src/prove/smt/solver/nlsat/interval-set.ts +351 -0
- package/src/prove/smt/solver/nlsat/levelwise.ts +740 -0
- package/src/prove/smt/solver/nlsat/mpbq.ts +161 -0
- package/src/prove/smt/solver/nlsat/polynomial.ts +1722 -0
- package/src/prove/smt/solver/nlsat/solver.ts +911 -0
- package/src/prove/smt/solver/nlsat/types.ts +43 -0
- package/src/prove/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
- package/src/prove/smt/solver/nlsat/upolynomial.ts +996 -0
- package/src/prove/smt/solver/nnf.ts +469 -0
- package/src/prove/smt/solver/pattern-inference.ts +351 -0
- package/src/prove/smt/solver/poly-rewriter.ts +477 -0
- package/src/prove/smt/solver/propagate-values.ts +83 -0
- package/src/prove/smt/solver/proto-model.ts +551 -0
- package/src/prove/smt/solver/pull-quant.ts +139 -0
- package/src/prove/smt/solver/qe-lite-der.ts +526 -0
- package/src/prove/smt/solver/qe-lite-fm.ts +576 -0
- package/src/prove/smt/solver/qe-lite.ts +381 -0
- package/src/prove/smt/solver/qi-queue.ts +362 -0
- package/src/prove/smt/solver/quant-tactics.ts +117 -0
- package/src/prove/smt/solver/quantifier-analyzer.ts +562 -0
- package/src/prove/smt/solver/quantifier-manager.ts +186 -0
- package/src/prove/smt/solver/random-gen.ts +10 -0
- package/src/prove/smt/solver/rational.ts +165 -0
- package/src/prove/smt/solver/ref-vector.ts +62 -0
- package/src/prove/smt/solver/relevancy.ts +247 -0
- package/src/prove/smt/solver/rewriter-tpl.ts +1159 -0
- package/src/prove/smt/solver/rewriter.ts +843 -0
- package/src/prove/smt/solver/rlimit.ts +79 -0
- package/src/prove/smt/solver/smtlib-parser.ts +526 -0
- package/src/prove/smt/solver/solver.ts +71 -0
- package/src/prove/smt/solver/sort.ts +35 -0
- package/src/prove/smt/solver/sticky-generations.ts +69 -0
- package/src/prove/smt/solver/theory-lra-axioms.ts +340 -0
- package/src/prove/smt/solver/theory-lra-bounds.ts +226 -0
- package/src/prove/smt/solver/theory-lra-final-check.ts +252 -0
- package/src/prove/smt/solver/theory-lra-internalize.ts +476 -0
- package/src/prove/smt/solver/theory-lra-model.ts +243 -0
- package/src/prove/smt/solver/theory-lra-propagate.ts +405 -0
- package/src/prove/smt/solver/theory-lra-state.ts +43 -0
- package/src/prove/smt/solver/theory-lra.ts +56 -0
- package/src/prove/smt/solver/var-subst.ts +258 -0
- package/src/prove/smt/solver/z3-license.txt +10 -0
- package/src/prove/smt/table.ts +170 -0
- package/src/prove/tlapm/tlapm.ts +97 -0
- package/src/prove/tlaps-module.ts +510 -0
- package/src/prove/verdict.ts +19 -0
- package/src/prove/zenon/LICENSE +32 -0
- package/src/prove/zenon/check/check.ts +105 -0
- package/src/prove/zenon/check/definitions.ts +144 -0
- package/src/prove/zenon/check/equality.ts +182 -0
- package/src/prove/zenon/check/functions.ts +62 -0
- package/src/prove/zenon/check/keys.ts +125 -0
- package/src/prove/zenon/check/propositional.ts +104 -0
- package/src/prove/zenon/check/records.ts +188 -0
- package/src/prove/zenon/check/relations.ts +121 -0
- package/src/prove/zenon/check/rewrites.ts +224 -0
- package/src/prove/zenon/check/sets.ts +302 -0
- package/src/prove/zenon/check/terms.ts +127 -0
- package/src/prove/zenon/check/tla.ts +126 -0
- package/src/prove/zenon/decide.ts +113 -0
- package/src/prove/zenon/encode.ts +444 -0
- package/src/prove/zenon/eqrel.ts +300 -0
- package/src/prove/zenon/equality-rules.ts +435 -0
- package/src/prove/zenon/expr.ts +551 -0
- package/src/prove/zenon/index-table.ts +350 -0
- package/src/prove/zenon/isar-case.ts +513 -0
- package/src/prove/zenon/isar.ts +1249 -0
- package/src/prove/zenon/limits.ts +15 -0
- package/src/prove/zenon/llproof.ts +173 -0
- package/src/prove/zenon/mltoll.ts +1566 -0
- package/src/prove/zenon/node.ts +297 -0
- package/src/prove/zenon/ocaml.ts +169 -0
- package/src/prove/zenon/parse.ts +247 -0
- package/src/prove/zenon/phrase.ts +191 -0
- package/src/prove/zenon/proof.ts +198 -0
- package/src/prove/zenon/propositional-rules.ts +387 -0
- package/src/prove/zenon/rules.ts +94 -0
- package/src/prove/zenon/search.ts +288 -0
- package/src/prove/zenon/tla/functions.ts +151 -0
- package/src/prove/zenon/tla/llproof.ts +480 -0
- package/src/prove/zenon/tla/records.ts +225 -0
- package/src/prove/zenon/tla/rewrites.ts +447 -0
- package/src/prove/zenon/tla/sets.ts +304 -0
- package/src/prove/zenon/tla/terms.ts +181 -0
- package/src/prove/zenon/tla/tla.ts +506 -0
- package/src/run-cli.ts +338 -52
- package/src/tlapm-cache.ts +250 -0
- package/src/values.ts +8 -3
- package/src/worker.ts +65 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// Encodes a prepared proof obligation in SMT-LIB as tlapm's SMT backend does (encode/ and
|
|
2
|
+
// backend/smtlib.ml): TLA+ values share one sort Idv, set membership is the predicate TLA__Mem,
|
|
3
|
+
// and each symbol the obligation uses brings the symbols and axioms encode/n_data.ml lists for it.
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
adjoin,
|
|
7
|
+
type Expr,
|
|
8
|
+
type Hyp,
|
|
9
|
+
type Scope,
|
|
10
|
+
type Sequent,
|
|
11
|
+
} from "../../obligations/expression";
|
|
12
|
+
import { compareStrings } from "../ocaml";
|
|
13
|
+
import { collectSymbols } from "./axiomatize";
|
|
14
|
+
import { createPrinter } from "./context";
|
|
15
|
+
import type { Entry } from "./flatten";
|
|
16
|
+
import { declare, MEM } from "./format";
|
|
17
|
+
import { simplifyRecordEqualities, sortRecordFields } from "./simplify";
|
|
18
|
+
import { createEncoder, type Binding, type EncodingSession, type Env, type Mode } from "./rewrite";
|
|
19
|
+
import { compareAxioms } from "./table";
|
|
20
|
+
|
|
21
|
+
type Encoder = ReturnType<typeof createEncoder>;
|
|
22
|
+
|
|
23
|
+
const HYPOTHESIS: Mode = { polarity: "Negative", at: undefined, depth: 0 };
|
|
24
|
+
|
|
25
|
+
// Prep.expand_defs substitutes visible definitions and pragmas, so they declare nothing.
|
|
26
|
+
const isExpanded = (hyp: Extract<Hyp, { kind: "Defn" }>) =>
|
|
27
|
+
hyp.definition.kind === "Bpragma" ||
|
|
28
|
+
(hyp.definition.kind === "Operator" && hyp.visibility === "Visible");
|
|
29
|
+
|
|
30
|
+
// Smtlib.is_sndord_fact: an ASSUME that declares an operator, which tlapm leaves out.
|
|
31
|
+
const isSecondOrder = (expression: Expr) =>
|
|
32
|
+
expression.kind === "Sequent" &&
|
|
33
|
+
expression.sequent.context.some((hyp) => hyp.kind === "Fresh" && hyp.shape > 0);
|
|
34
|
+
|
|
35
|
+
// Declares each hypothesis's names and asserts its facts, in order, as Smtlib.pp_print_obligation.
|
|
36
|
+
// Visible definitions and TLAPS's pragmas are expanded, so they declare nothing.
|
|
37
|
+
const encodeContext = (context: Hyp[], encoder: Encoder) =>
|
|
38
|
+
context.reduce<{ env: Env | undefined; scope: Scope | undefined; entries: Entry[] }>(
|
|
39
|
+
(state, hyp) => {
|
|
40
|
+
const next = (binding: Binding, entries: Entry[] = []) => ({
|
|
41
|
+
env: encoder.push(state.env, binding),
|
|
42
|
+
scope: adjoin(state.scope, hyp),
|
|
43
|
+
entries: [...state.entries, ...entries],
|
|
44
|
+
});
|
|
45
|
+
switch (hyp.kind) {
|
|
46
|
+
case "Fresh": {
|
|
47
|
+
const symbol = encoder.getUniqueName(hyp.name);
|
|
48
|
+
const binding: Binding =
|
|
49
|
+
hyp.shape === 0
|
|
50
|
+
? { kind: "constant", symbol }
|
|
51
|
+
: { kind: "operator", symbol, arity: hyp.shape };
|
|
52
|
+
const declaration = declare(symbol, hyp.shape);
|
|
53
|
+
const domain = hyp.domain;
|
|
54
|
+
if (!domain || hyp.domainVisibility === "Hidden") return next(binding, [declaration]);
|
|
55
|
+
// Rewrite.elim_bounds: NEW x \in S is NEW x and the fact x \in S.
|
|
56
|
+
const fact = `(assert (${MEM} ${symbol} ${encoder.encodeIdv(domain, state.env, HYPOTHESIS)}))`;
|
|
57
|
+
return next(binding, [declaration, ...encoder.getEntries(fact)]);
|
|
58
|
+
}
|
|
59
|
+
// Rewrite.elim_flex: a variable is two constants, the second for its primed value.
|
|
60
|
+
case "Flex": {
|
|
61
|
+
const symbol = encoder.getUniqueName(hyp.name);
|
|
62
|
+
const primed = encoder.getUniqueName(`${hyp.name}_prime`);
|
|
63
|
+
return next({ kind: "variable", name: hyp.name, symbol, primed }, [
|
|
64
|
+
declare(symbol, 0),
|
|
65
|
+
declare(primed, 0),
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
case "Defn": {
|
|
69
|
+
const definition = hyp.definition;
|
|
70
|
+
if (isExpanded(hyp))
|
|
71
|
+
return next({ kind: "unsupported", reason: "an expanded definition" });
|
|
72
|
+
if (definition.kind === "Instance")
|
|
73
|
+
return next({ kind: "unsupported", reason: "an instance" });
|
|
74
|
+
if (definition.kind === "Recursive") {
|
|
75
|
+
const symbol = encoder.getUniqueName(`CONSTANT_${definition.name}_`);
|
|
76
|
+
return next({ kind: "operator", symbol, arity: definition.shape }, [
|
|
77
|
+
declare(symbol, definition.shape),
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
if (definition.kind !== "Operator")
|
|
81
|
+
return next({ kind: "unsupported", reason: "a definition" });
|
|
82
|
+
const body = definition.body;
|
|
83
|
+
const params = body.kind === "Lambda" ? body.params : [];
|
|
84
|
+
if (params.some((param) => param.shape > 0))
|
|
85
|
+
return next({
|
|
86
|
+
kind: "secondOrder",
|
|
87
|
+
name: definition.name,
|
|
88
|
+
shapes: params.map((param) => param.shape),
|
|
89
|
+
});
|
|
90
|
+
const symbol = encoder.getUniqueName(definition.name);
|
|
91
|
+
return next({ kind: "operator", symbol, arity: params.length }, [
|
|
92
|
+
declare(symbol, params.length),
|
|
93
|
+
]);
|
|
94
|
+
}
|
|
95
|
+
case "Fact": {
|
|
96
|
+
const binding: Binding = { kind: "unsupported", reason: "a reference to a fact" };
|
|
97
|
+
if (hyp.visibility === "Hidden") return next(binding);
|
|
98
|
+
if (!isSecondOrder(hyp.expression)) {
|
|
99
|
+
const fact = `(assert ${encoder.encodeBool(hyp.expression, state.env, HYPOTHESIS)})`;
|
|
100
|
+
return next(binding, encoder.getEntries(fact));
|
|
101
|
+
}
|
|
102
|
+
const fact = `(assert ${encoder.encodeBool(hyp.expression, state.env, { ...HYPOTHESIS, isOmitted: true })})`;
|
|
103
|
+
return next(
|
|
104
|
+
binding,
|
|
105
|
+
encoder
|
|
106
|
+
.getEntries(fact)
|
|
107
|
+
.map((entry) => (typeof entry === "string" ? { omitted: entry } : entry)),
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
{ env: undefined, scope: undefined, entries: [] },
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
// Smtlib.pp_print_obligation: the logic, the sort, the builtin symbols by name, their axioms,
|
|
116
|
+
// then the hypotheses in order, the goal last. It throws a TlaUnsupportedError for what the
|
|
117
|
+
// encoding can't express, where tlapm's encoder fails.
|
|
118
|
+
export const encodeSmt = (prepared: Sequent, session: EncodingSession) => {
|
|
119
|
+
const simplified = sortRecordFields(
|
|
120
|
+
simplifyRecordEqualities({ kind: "Sequent", sequent: prepared }),
|
|
121
|
+
);
|
|
122
|
+
const sequent = simplified.kind === "Sequent" ? simplified.sequent : prepared;
|
|
123
|
+
const encoder = createEncoder(session);
|
|
124
|
+
const context = encodeContext(sequent.context, encoder);
|
|
125
|
+
const goal = isSecondOrder(sequent.active)
|
|
126
|
+
? []
|
|
127
|
+
: encoder.getEntries(
|
|
128
|
+
`(assert (! (not ${encoder.encodeBool(sequent.active, context.env, { ...HYPOTHESIS, polarity: "Positive" })}) :named |Goal|))`,
|
|
129
|
+
);
|
|
130
|
+
const entries = [...context.entries, ...goal];
|
|
131
|
+
const printer = createPrinter(encoder.declared);
|
|
132
|
+
const collected = collectSymbols(
|
|
133
|
+
entries,
|
|
134
|
+
encoder.flattened,
|
|
135
|
+
encoder.anonymous,
|
|
136
|
+
encoder.recordFields,
|
|
137
|
+
);
|
|
138
|
+
const declarations = [...collected.symbols]
|
|
139
|
+
.toSorted((left, right) => compareStrings(left[0], right[0]))
|
|
140
|
+
.flatMap((symbol) => (symbol[1].declaration === undefined ? [] : [symbol[1].declaration]));
|
|
141
|
+
const axioms = [...collected.axioms.values()]
|
|
142
|
+
.toSorted(compareAxioms)
|
|
143
|
+
.flatMap((entry) =>
|
|
144
|
+
entry.formula === undefined ? [] : [`(assert (! ${entry.formula} :named |${entry.label}|))`],
|
|
145
|
+
);
|
|
146
|
+
const keys = new Set(collected.axioms.keys());
|
|
147
|
+
const lines = [
|
|
148
|
+
...declarations,
|
|
149
|
+
...axioms,
|
|
150
|
+
...entries.flatMap((entry) => {
|
|
151
|
+
if (typeof entry === "string") return [entry];
|
|
152
|
+
return "omitted" in entry ? [] : entry.getLines(keys);
|
|
153
|
+
}),
|
|
154
|
+
];
|
|
155
|
+
// Smtlib.collect_sorts declares Idv only when a symbol or a bound variable has that sort.
|
|
156
|
+
// Type.Synthesize gives that sort to each definition left in the context, though none is printed.
|
|
157
|
+
const isIdvUsed =
|
|
158
|
+
lines.some((line) => /\bIdv\b/.test(line)) ||
|
|
159
|
+
sequent.context.some(
|
|
160
|
+
(hyp) =>
|
|
161
|
+
hyp.kind === "Defn" &&
|
|
162
|
+
(hyp.definition.kind === "Operator" || hyp.definition.kind === "Recursive") &&
|
|
163
|
+
!isExpanded(hyp),
|
|
164
|
+
);
|
|
165
|
+
return ["(set-logic UFNIA)", ...(isIdvUsed ? ["(declare-sort Idv 0)"] : []), ...lines]
|
|
166
|
+
.map(printer.resolve)
|
|
167
|
+
.concat("(check-sat)", "(exit)")
|
|
168
|
+
.join("\n");
|
|
169
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Standardize.set_extensionality_visitor: an equality of sets where it may need proving triggers
|
|
2
|
+
// the set extensionality axiom, through TrigEq.
|
|
3
|
+
|
|
4
|
+
import { CAP, CUP, FUN_SET, INT_RANGE, SEQ, SET_MINUS, SUBSET, TRIG_EQ, UNION } from "./format";
|
|
5
|
+
|
|
6
|
+
export type Polarity = "Positive" | "Negative" | "Neither";
|
|
7
|
+
|
|
8
|
+
// The head of a set equality whose polarity keeps it from triggering set extensionality unless a
|
|
9
|
+
// cast encloses it: (?trigger triggered kept).
|
|
10
|
+
const UNRESOLVED_TRIGGER = "?trigger";
|
|
11
|
+
|
|
12
|
+
const SET_HEADS = new Set([UNION, SUBSET, CUP, CAP, SET_MINUS, INT_RANGE, FUN_SET, SEQ]);
|
|
13
|
+
|
|
14
|
+
// Standardize.is_set: the applications of constructors whose values are sets. Standardize leaves
|
|
15
|
+
// BOOLEAN, STRING, Int, and Nat as bare symbols, which is_set doesn't match.
|
|
16
|
+
const isSetTerm = (text: string) => {
|
|
17
|
+
const head = /^\(?([^\s()]+)/.exec(text)?.[1] ?? "";
|
|
18
|
+
return (
|
|
19
|
+
SET_HEADS.has(head) ||
|
|
20
|
+
/^smt__TLA____(SetEnum__\d+|SetSt__flatnd__\d+|SetOf__\d+__flatnd__\d+|Product__\d+|RecordSet__)/.test(
|
|
21
|
+
head,
|
|
22
|
+
)
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Picks the triggered or the kept form of each unresolved set equality in text.
|
|
27
|
+
export const resolveTriggers = (text: string, isTriggered: boolean): string => {
|
|
28
|
+
const start = text.indexOf(`(${UNRESOLVED_TRIGGER} `);
|
|
29
|
+
if (start === -1) return text;
|
|
30
|
+
const getEnd = (from: number) => {
|
|
31
|
+
const end = { depth: 0, index: from };
|
|
32
|
+
for (; end.index < text.length; end.index++) {
|
|
33
|
+
if (text[end.index] === "(") end.depth++;
|
|
34
|
+
if (text[end.index] === ")") end.depth--;
|
|
35
|
+
if (end.depth === 0) return end.index + 1;
|
|
36
|
+
}
|
|
37
|
+
return text.length;
|
|
38
|
+
};
|
|
39
|
+
const triggeredStart = start + UNRESOLVED_TRIGGER.length + 2;
|
|
40
|
+
const triggeredEnd = getEnd(triggeredStart);
|
|
41
|
+
const keptEnd = getEnd(triggeredEnd + 1);
|
|
42
|
+
const chosen = isTriggered
|
|
43
|
+
? text.slice(triggeredStart, triggeredEnd)
|
|
44
|
+
: text.slice(triggeredEnd + 1, keptEnd);
|
|
45
|
+
return text.slice(0, start) + chosen + resolveTriggers(text.slice(keptEnd + 1), isTriggered);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// The equality, or disequality, of two Idv terms at a polarity.
|
|
49
|
+
export const getEquality = (
|
|
50
|
+
left: string,
|
|
51
|
+
right: string,
|
|
52
|
+
polarity: Polarity,
|
|
53
|
+
isNegated: boolean,
|
|
54
|
+
) => {
|
|
55
|
+
const triggered = `(${TRIG_EQ} ${left} ${right})`;
|
|
56
|
+
const kept = isNegated ? `(distinct ${left} ${right})` : `(= ${left} ${right})`;
|
|
57
|
+
if (!isSetTerm(left) && !isSetTerm(right)) return kept;
|
|
58
|
+
const text = isNegated ? `(not ${triggered})` : triggered;
|
|
59
|
+
// A cast around the equality may still trigger it, so both forms wait for resolveTriggers.
|
|
60
|
+
return polarity === (isNegated ? "Positive" : "Negative")
|
|
61
|
+
? `(${UNRESOLVED_TRIGGER} ${text} ${kept})`
|
|
62
|
+
: text;
|
|
63
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// encode/n_flatten.ml: each application of a second-order construct becomes a fresh first-order
|
|
2
|
+
// symbol, declared with its axioms before the fact that uses it.
|
|
3
|
+
|
|
4
|
+
import { declare, formatName } from "./format";
|
|
5
|
+
import type { Axiom } from "./table";
|
|
6
|
+
|
|
7
|
+
// A symbol that Flatten introduces, with the text tlapm's collect pass visits for its
|
|
8
|
+
// higher-order arguments.
|
|
9
|
+
export interface Flattened {
|
|
10
|
+
symbol: string;
|
|
11
|
+
kind: string;
|
|
12
|
+
visit: string;
|
|
13
|
+
getLines: (axioms: Set<string>) => string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// A fact that tlapm encodes, so its symbols bring their axioms, but leaves out of the text.
|
|
17
|
+
export interface OmittedFact {
|
|
18
|
+
omitted: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type Entry = string | Flattened | OmittedFact;
|
|
22
|
+
|
|
23
|
+
// A bound variable's placeholder: its name and its depth, which the printer names in scope.
|
|
24
|
+
export const getVariable = (name: string, depth: number) => `${name}#${depth}`;
|
|
25
|
+
|
|
26
|
+
export const getDepth = (variable: string) => Number(variable.slice(variable.lastIndexOf("#") + 1));
|
|
27
|
+
|
|
28
|
+
// The head that marks the Unprimable WITNESS wraps around its witness, which Prep never removes:
|
|
29
|
+
// (?unprimable term). Flatten.compare_expr tells it apart, and the printed text drops it.
|
|
30
|
+
export const UNPRIMABLE = "?unprimable";
|
|
31
|
+
|
|
32
|
+
const getClosing = (text: string, from: number) => {
|
|
33
|
+
const state = { depth: 0 };
|
|
34
|
+
for (const index of Array.from(
|
|
35
|
+
{ length: text.length - from },
|
|
36
|
+
(_value, offset) => from + offset,
|
|
37
|
+
)) {
|
|
38
|
+
if (text[index] === "(") state.depth++;
|
|
39
|
+
if (text[index] === ")") state.depth--;
|
|
40
|
+
if (state.depth === 0) return index;
|
|
41
|
+
}
|
|
42
|
+
return text.length;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// The text with each Unprimable mark dropped.
|
|
46
|
+
export const removeUnprimable = (text: string): string => {
|
|
47
|
+
const start = text.indexOf(`(${UNPRIMABLE} `);
|
|
48
|
+
if (start === -1) return text;
|
|
49
|
+
const end = getClosing(text, start);
|
|
50
|
+
return (
|
|
51
|
+
text.slice(0, start) +
|
|
52
|
+
removeUnprimable(text.slice(start + UNPRIMABLE.length + 2, end)) +
|
|
53
|
+
removeUnprimable(text.slice(end + 1))
|
|
54
|
+
);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Flatten.compare_expr: an application matches an earlier one with the same higher-order
|
|
58
|
+
// arguments, whatever the variables bound outside them. It can't compare quantifiers or
|
|
59
|
+
// conditionals, so an argument with one matches nothing.
|
|
60
|
+
const getFlatKey = (text: string, depth: number) =>
|
|
61
|
+
/\((forall|exists|ite) /.test(text)
|
|
62
|
+
? undefined
|
|
63
|
+
: text.replace(/[^\s()]+#(\d+)/g, (_match, found: string) =>
|
|
64
|
+
Number(found) < depth ? "#" : `#${Number(found) - depth}`,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
// Where an application occurs: its depth of bound variables, and whether it's in a fact that
|
|
68
|
+
// Smtlib leaves out, which Flatten doesn't enter.
|
|
69
|
+
interface Site {
|
|
70
|
+
depth: number;
|
|
71
|
+
isOmitted?: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// The state of Flatten.main over one obligation: its blueprint count, which init_bp_count resets
|
|
75
|
+
// for each obligation, and the symbols it introduced.
|
|
76
|
+
export const createFlattener = () => {
|
|
77
|
+
const flattened = new Map<string, Flattened>();
|
|
78
|
+
const flatKeys = new Map<string, Flattened>();
|
|
79
|
+
const counter = { blueprints: 0 };
|
|
80
|
+
// The symbols that the fact being encoded introduces, declared before it.
|
|
81
|
+
const introduced: Flattened[] = [];
|
|
82
|
+
|
|
83
|
+
const flatten = (
|
|
84
|
+
kind: string,
|
|
85
|
+
visit: string,
|
|
86
|
+
site: Site,
|
|
87
|
+
getAxioms: (symbol: string, axioms: Set<string>) => Axiom[],
|
|
88
|
+
arity: number,
|
|
89
|
+
name = `TLA__${kind}`,
|
|
90
|
+
) => {
|
|
91
|
+
// Flatten.find_hoapp skips a second-order ASSUME, so the application stays and only
|
|
92
|
+
// N_axiomatize.collect, which runs before Flatten, visits it.
|
|
93
|
+
if (site.isOmitted) {
|
|
94
|
+
const symbol = formatName(`${name}_omitted_${flattened.size}`);
|
|
95
|
+
flattened.set(symbol, { symbol, kind, visit: removeUnprimable(visit), getLines: () => [] });
|
|
96
|
+
return symbol;
|
|
97
|
+
}
|
|
98
|
+
const key = getFlatKey(`${name} ${visit}`, site.depth);
|
|
99
|
+
// Flatten.new_bp_id counts every application, even one that reuses an earlier symbol.
|
|
100
|
+
counter.blueprints++;
|
|
101
|
+
const known = key === undefined ? undefined : flatKeys.get(key);
|
|
102
|
+
if (known) return known.symbol;
|
|
103
|
+
const raw = `${name}_flatnd_${counter.blueprints}`;
|
|
104
|
+
const symbol = formatName(raw);
|
|
105
|
+
const flat: Flattened = {
|
|
106
|
+
symbol,
|
|
107
|
+
kind,
|
|
108
|
+
visit: removeUnprimable(visit),
|
|
109
|
+
getLines: (axioms) => [
|
|
110
|
+
declare(symbol, arity),
|
|
111
|
+
...getAxioms(symbol, axioms).map(
|
|
112
|
+
(entry) =>
|
|
113
|
+
`(assert (! ${removeUnprimable(entry.formula ?? "")} :named |${entry.label} ${raw}|))`,
|
|
114
|
+
),
|
|
115
|
+
],
|
|
116
|
+
};
|
|
117
|
+
if (key !== undefined) flatKeys.set(key, flat);
|
|
118
|
+
flattened.set(symbol, flat);
|
|
119
|
+
introduced.push(flat);
|
|
120
|
+
return symbol;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// The symbols introduced since the last call, in order.
|
|
124
|
+
const takeIntroduced = () => introduced.splice(0);
|
|
125
|
+
|
|
126
|
+
return { flattened, flatten, takeIntroduced };
|
|
127
|
+
};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// backend/smtlib.ml's names and the SMT-LIB terms the encoding builds.
|
|
2
|
+
|
|
3
|
+
import type { Builtin } from "../../obligations/expression";
|
|
4
|
+
|
|
5
|
+
// Smtlib.format_smt: spells out each special character, then doubles each underscore.
|
|
6
|
+
const SPELLED: Record<string, string> = {
|
|
7
|
+
"\\": "backslash_",
|
|
8
|
+
"+": "plussign_",
|
|
9
|
+
"-": "hyphen_",
|
|
10
|
+
"*": "asterisk_",
|
|
11
|
+
"/": "slash_",
|
|
12
|
+
"%": "percentsign_",
|
|
13
|
+
"^": "circumflexaccent_",
|
|
14
|
+
"&": "ampersand_",
|
|
15
|
+
"@": "atsign_",
|
|
16
|
+
"#": "pound_",
|
|
17
|
+
$: "dollarsign_",
|
|
18
|
+
"(": "leftparenthesis_",
|
|
19
|
+
")": "rightparenthesis_",
|
|
20
|
+
"|": "verticalbar_",
|
|
21
|
+
".": "period_",
|
|
22
|
+
":": "colon_",
|
|
23
|
+
"?": "questionmark_",
|
|
24
|
+
"!": "exclamationmark_",
|
|
25
|
+
"<": "lessthansign_",
|
|
26
|
+
">": "greaterthansign_",
|
|
27
|
+
"=": "equalsign_",
|
|
28
|
+
" ": "space_",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const formatName = (name: string) =>
|
|
32
|
+
`smt__${name.replace(/[\\+\-*/%^&@#$()|.:?!<>= ]/g, (character) => SPELLED[character]).replaceAll("_", "__")}`;
|
|
33
|
+
|
|
34
|
+
const TLA_PREFIX = formatName("TLA__");
|
|
35
|
+
|
|
36
|
+
const UNSPELLED = new Map(Object.entries(SPELLED).map((entry) => [entry[1], entry[0]]));
|
|
37
|
+
|
|
38
|
+
const SPELLED_WORDS = new RegExp(`(${[...UNSPELLED.keys()].join("|")})`, "g");
|
|
39
|
+
|
|
40
|
+
export const tla = (name: string) => formatName(`TLA__${name}`);
|
|
41
|
+
|
|
42
|
+
// The builtin a symbol that tla names stands for, or undefined for any other symbol.
|
|
43
|
+
export const getTlaName = (symbol: string) =>
|
|
44
|
+
symbol.startsWith(TLA_PREFIX)
|
|
45
|
+
? symbol
|
|
46
|
+
.slice(TLA_PREFIX.length)
|
|
47
|
+
.replaceAll("__", "_")
|
|
48
|
+
.replace(SPELLED_WORDS, (word) => UNSPELLED.get(word) ?? word)
|
|
49
|
+
: undefined;
|
|
50
|
+
|
|
51
|
+
export const MEM = tla("Mem");
|
|
52
|
+
export const SET_EXT_TRIGGER = tla("SetExtTrigger");
|
|
53
|
+
export const SUBSET_EQ = tla("SubsetEq");
|
|
54
|
+
export const CUP = tla("Cup");
|
|
55
|
+
export const CAP = tla("Cap");
|
|
56
|
+
export const SET_MINUS = tla("SetMinus");
|
|
57
|
+
export const SUBSET = tla("Subset");
|
|
58
|
+
export const UNION = tla("Union");
|
|
59
|
+
export const TT_IDV = tla("Tt_Idv");
|
|
60
|
+
export const CAST_BOOL = tla("Cast_Bool");
|
|
61
|
+
export const BOOL_SET = tla("BoolSet");
|
|
62
|
+
export const STR_SET = tla("StrSet");
|
|
63
|
+
export const INT_SET = tla("IntSet");
|
|
64
|
+
export const NAT_SET = tla("NatSet");
|
|
65
|
+
export const CAST_INT = tla("Cast_Int");
|
|
66
|
+
export const PROJ_INT = tla("Proj_Int");
|
|
67
|
+
export const INT_LTEQ = tla("IntLteq");
|
|
68
|
+
export const INT_RANGE = tla("IntRange");
|
|
69
|
+
export const FUN_APP = tla("FunApp");
|
|
70
|
+
export const FUN_DOM = tla("FunDom");
|
|
71
|
+
export const FUN_ISAFCN = tla("FunIsafcn");
|
|
72
|
+
export const FUN_SET = tla("FunSet");
|
|
73
|
+
export const FUN_EXCEPT = tla("FunExcept");
|
|
74
|
+
export const TRIG_EQ = tla("TrigEq_Set$Idv$");
|
|
75
|
+
export const IS_SET_OF = tla("IsSetOf");
|
|
76
|
+
export const SEQ = tla("Seq");
|
|
77
|
+
export const LEN = tla("Len");
|
|
78
|
+
export const CAT = tla("Cat");
|
|
79
|
+
export const APPEND = tla("Append");
|
|
80
|
+
export const HEAD = tla("Head");
|
|
81
|
+
export const TAIL = tla("Tail");
|
|
82
|
+
export const SUB_SEQ = tla("SubSeq");
|
|
83
|
+
export const TUPLE_0 = tla("Tuple_0");
|
|
84
|
+
|
|
85
|
+
export const BUILTIN_SYMBOLS: Partial<Record<Builtin, string>> = {
|
|
86
|
+
Cup: CUP,
|
|
87
|
+
Cap: CAP,
|
|
88
|
+
Setminus: SET_MINUS,
|
|
89
|
+
SUBSET,
|
|
90
|
+
UNION,
|
|
91
|
+
DOMAIN: FUN_DOM,
|
|
92
|
+
Plus: tla("IntPlus"),
|
|
93
|
+
Minus: tla("IntMinus"),
|
|
94
|
+
Times: tla("IntTimes"),
|
|
95
|
+
Exp: tla("IntExp"),
|
|
96
|
+
Quotient: tla("IntQuotient"),
|
|
97
|
+
Remainder: tla("IntRemainder"),
|
|
98
|
+
Uminus: tla("IntUminus"),
|
|
99
|
+
Range: INT_RANGE,
|
|
100
|
+
Seq: SEQ,
|
|
101
|
+
Len: LEN,
|
|
102
|
+
BSeq: tla("BSeq"),
|
|
103
|
+
Cat: CAT,
|
|
104
|
+
Append: APPEND,
|
|
105
|
+
Head: HEAD,
|
|
106
|
+
Tail: TAIL,
|
|
107
|
+
SubSeq: SUB_SEQ,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const getNames = (prefix: string, count: number) =>
|
|
111
|
+
Array.from({ length: count }, (_value, index) => `${prefix}${index + 1}`);
|
|
112
|
+
|
|
113
|
+
const getSortedBinders = (variables: [string, string][]) =>
|
|
114
|
+
variables.map((variable) => `(${variable[0]} ${variable[1]})`).join(" ");
|
|
115
|
+
|
|
116
|
+
export const forall = (
|
|
117
|
+
variables: string[],
|
|
118
|
+
body: string,
|
|
119
|
+
patterns: string[][] = [],
|
|
120
|
+
sort = "Idv",
|
|
121
|
+
) =>
|
|
122
|
+
variables.length === 0
|
|
123
|
+
? body
|
|
124
|
+
: `(forall (${getSortedBinders(variables.map((variable) => [variable, sort]))}) ${
|
|
125
|
+
patterns.length === 0
|
|
126
|
+
? body
|
|
127
|
+
: `(! ${body} ${patterns.map((pattern) => `:pattern (${pattern.join(" ")})`).join(" ")})`
|
|
128
|
+
})`;
|
|
129
|
+
|
|
130
|
+
export const conjoin = (items: string[]) =>
|
|
131
|
+
items.length === 0 ? "true" : items.length === 1 ? items[0] : `(and ${items.join(" ")})`;
|
|
132
|
+
|
|
133
|
+
export const disjoin = (items: string[]) =>
|
|
134
|
+
items.length === 0 ? "false" : items.length === 1 ? items[0] : `(or ${items.join(" ")})`;
|
|
135
|
+
|
|
136
|
+
export const call = (symbol: string, args: string[]) =>
|
|
137
|
+
args.length === 0 ? symbol : `(${symbol} ${args.join(" ")})`;
|
|
138
|
+
|
|
139
|
+
export const declare = (symbol: string, arity: number, sort = "Idv", argSort = "Idv") =>
|
|
140
|
+
`(declare-fun ${symbol} (${Array.from({ length: arity }, () => argSort).join(" ")}) ${sort})`;
|
|
141
|
+
|
|
142
|
+
export const replaceToken = (text: string, token: string, replacement: string) =>
|
|
143
|
+
text.replace(/[^\s()]+/g, (found) => (found === token ? replacement : found));
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// Flatten.instantiate: the axioms of encode/n_axioms.ml for a second-order construct, instantiated
|
|
2
|
+
// for one flattened symbol. Each takes the symbol's name, the bound variables its argument uses,
|
|
3
|
+
// which become its last arguments, and the argument's text with the variable it binds.
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
APPEND,
|
|
7
|
+
call,
|
|
8
|
+
conjoin,
|
|
9
|
+
forall,
|
|
10
|
+
FUN_APP,
|
|
11
|
+
FUN_DOM,
|
|
12
|
+
FUN_ISAFCN,
|
|
13
|
+
FUN_SET,
|
|
14
|
+
getNames,
|
|
15
|
+
IS_SET_OF,
|
|
16
|
+
LEN,
|
|
17
|
+
MEM,
|
|
18
|
+
NAT_SET,
|
|
19
|
+
PROJ_INT,
|
|
20
|
+
replaceToken,
|
|
21
|
+
SEQ,
|
|
22
|
+
TUPLE_0,
|
|
23
|
+
} from "./format";
|
|
24
|
+
import { axiom, getAxiomKey } from "./table";
|
|
25
|
+
|
|
26
|
+
// SetStDef for { x \in a : body }.
|
|
27
|
+
export const getSetStAxioms = (name: string, free: string[], body: string, variable: string) => {
|
|
28
|
+
const set = call(name, ["a", ...free]);
|
|
29
|
+
return [
|
|
30
|
+
axiom(
|
|
31
|
+
"SetStDef",
|
|
32
|
+
[],
|
|
33
|
+
forall(
|
|
34
|
+
[...free, "a", "x"],
|
|
35
|
+
`(= (${MEM} x ${set}) (and (${MEM} x a) ${replaceToken(body, variable, "x")}))`,
|
|
36
|
+
[[`(${MEM} x ${set})`], [`(${MEM} x a)`, set]],
|
|
37
|
+
),
|
|
38
|
+
),
|
|
39
|
+
];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// SetOfIntro, SetOfElim, and AssertIsSetOf for { element : y1 \in a1, ..., yn \in an }.
|
|
43
|
+
export const getSetOfAxioms = (name: string, free: string[], element: string, bound: string[]) => {
|
|
44
|
+
const count = bound.length;
|
|
45
|
+
const sets = getNames("a", count);
|
|
46
|
+
const variables = getNames("y", count);
|
|
47
|
+
const value = bound.reduce(
|
|
48
|
+
(text, variable, index) => replaceToken(text, variable, variables[index]),
|
|
49
|
+
element,
|
|
50
|
+
);
|
|
51
|
+
const set = call(name, [...sets, ...free]);
|
|
52
|
+
const guards = variables.map((variable, index) => `(${MEM} ${variable} ${sets[index]})`);
|
|
53
|
+
const binders = variables.map((variable) => `(${variable} Idv)`).join(" ");
|
|
54
|
+
// N_axioms.assert_issetof names its one set a.
|
|
55
|
+
const single = count === 1 ? ["a"] : sets;
|
|
56
|
+
return [
|
|
57
|
+
axiom(
|
|
58
|
+
"SetOfIntro",
|
|
59
|
+
[count],
|
|
60
|
+
forall([...free, ...sets, ...variables], `(=> ${conjoin(guards)} (${MEM} ${value} ${set}))`, [
|
|
61
|
+
[value, set],
|
|
62
|
+
[set, ...guards],
|
|
63
|
+
]),
|
|
64
|
+
),
|
|
65
|
+
axiom(
|
|
66
|
+
"SetOfElim",
|
|
67
|
+
[count],
|
|
68
|
+
forall(
|
|
69
|
+
[...free, ...sets, "x"],
|
|
70
|
+
`(=> (${MEM} x ${set}) (exists (${binders}) ${conjoin([...guards, `(= x ${value})`])}))`,
|
|
71
|
+
[[`(${MEM} x ${set})`]],
|
|
72
|
+
),
|
|
73
|
+
),
|
|
74
|
+
axiom(
|
|
75
|
+
"AssertIsSetOf",
|
|
76
|
+
[count],
|
|
77
|
+
forall([...free, ...single], `(${IS_SET_OF} ${call(name, [...single, ...free])})`, [
|
|
78
|
+
[call(name, [...single, ...free])],
|
|
79
|
+
]),
|
|
80
|
+
),
|
|
81
|
+
];
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// FunConstrIsafcn, FunDomDef, FunAppDef, and FunTyping when collect brought it, for
|
|
85
|
+
// [x \in a |-> body].
|
|
86
|
+
export const getFunctionAxioms = (
|
|
87
|
+
name: string,
|
|
88
|
+
free: string[],
|
|
89
|
+
body: string,
|
|
90
|
+
variable: string,
|
|
91
|
+
axioms: Set<string>,
|
|
92
|
+
) => {
|
|
93
|
+
const value = call(name, ["a", ...free]);
|
|
94
|
+
const result = replaceToken(body, variable, "x");
|
|
95
|
+
const typing = axioms.has(getAxiomKey(axiom("FunTyping", [], undefined)))
|
|
96
|
+
? [
|
|
97
|
+
axiom(
|
|
98
|
+
"FunTyping",
|
|
99
|
+
[],
|
|
100
|
+
forall(
|
|
101
|
+
[...free, "a", "b"],
|
|
102
|
+
`(=> (forall ((x Idv)) (=> (${MEM} x a) (${MEM} ${result} b))) (${MEM} ${value} (${FUN_SET} a b)))`,
|
|
103
|
+
[[value, `(${FUN_SET} a b)`]],
|
|
104
|
+
),
|
|
105
|
+
),
|
|
106
|
+
]
|
|
107
|
+
: [];
|
|
108
|
+
return [
|
|
109
|
+
axiom("FunConstrIsafcn", [], forall([...free, "a"], `(${FUN_ISAFCN} ${value})`, [[value]])),
|
|
110
|
+
axiom("FunDomDef", [], forall([...free, "a"], `(= (${FUN_DOM} ${value}) a)`, [[value]])),
|
|
111
|
+
axiom(
|
|
112
|
+
"FunAppDef",
|
|
113
|
+
[],
|
|
114
|
+
forall([...free, "a", "x"], `(=> (${MEM} x a) (= (${FUN_APP} ${value} x) ${result}))`, [
|
|
115
|
+
[`(${FUN_APP} ${value} x)`],
|
|
116
|
+
[`(${MEM} x a)`, value],
|
|
117
|
+
]),
|
|
118
|
+
),
|
|
119
|
+
...typing,
|
|
120
|
+
];
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// ChooseDef for CHOOSE x : body.
|
|
124
|
+
export const getChooseAxioms = (name: string, free: string[], body: string, variable: string) => [
|
|
125
|
+
axiom(
|
|
126
|
+
"ChooseDef",
|
|
127
|
+
[],
|
|
128
|
+
forall(
|
|
129
|
+
[...free, "x"],
|
|
130
|
+
`(=> ${replaceToken(body, variable, "x")} ${replaceToken(body, variable, call(name, free))})`,
|
|
131
|
+
),
|
|
132
|
+
),
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
// The SeqSelectseq axioms for SelectSeq(s, LAMBDA x : body).
|
|
136
|
+
export const getSelectSeqAxioms = (
|
|
137
|
+
name: string,
|
|
138
|
+
free: string[],
|
|
139
|
+
body: string,
|
|
140
|
+
variable: string,
|
|
141
|
+
) => {
|
|
142
|
+
const select = (sequence: string) => call(name, [sequence, ...free]);
|
|
143
|
+
const test = (term: string) => replaceToken(body, variable, term);
|
|
144
|
+
const appended = select(`(${APPEND} s x)`);
|
|
145
|
+
return [
|
|
146
|
+
axiom(
|
|
147
|
+
"SeqSelectseqTyping",
|
|
148
|
+
[],
|
|
149
|
+
forall([...free, "a", "s"], `(=> (${MEM} s (${SEQ} a)) (${MEM} ${select("s")} (${SEQ} a)))`, [
|
|
150
|
+
[`(${MEM} s (${SEQ} a))`, select("s")],
|
|
151
|
+
]),
|
|
152
|
+
),
|
|
153
|
+
axiom(
|
|
154
|
+
"SeqSelectseqLen",
|
|
155
|
+
[],
|
|
156
|
+
forall(
|
|
157
|
+
[...free, "s"],
|
|
158
|
+
`(=> (${MEM} (${LEN} s) ${NAT_SET}) (<= (${PROJ_INT} (${LEN} ${select("s")})) (${PROJ_INT} (${LEN} s))))`,
|
|
159
|
+
[[select("s")]],
|
|
160
|
+
),
|
|
161
|
+
),
|
|
162
|
+
axiom("SeqSelectseqNil", [], forall(free, `(= ${select(TUPLE_0)} ${TUPLE_0})`)),
|
|
163
|
+
axiom(
|
|
164
|
+
"SeqSelectseqApp",
|
|
165
|
+
[],
|
|
166
|
+
forall(
|
|
167
|
+
[...free, "s", "x"],
|
|
168
|
+
`(=> (${MEM} x (${FUN_DOM} ${select("s")})) ${test(`(${FUN_APP} ${select("s")} x)`)})`,
|
|
169
|
+
[[`(${FUN_APP} ${select("s")} x)`]],
|
|
170
|
+
),
|
|
171
|
+
),
|
|
172
|
+
axiom(
|
|
173
|
+
"SeqSelectseqAppend",
|
|
174
|
+
[],
|
|
175
|
+
forall(
|
|
176
|
+
[...free, "s", "x"],
|
|
177
|
+
`(and (=> ${test("x")} (= ${appended} (${APPEND} ${select("s")} x))) (=> (not ${test("x")}) (= ${appended} ${select("s")})))`,
|
|
178
|
+
[[appended]],
|
|
179
|
+
),
|
|
180
|
+
),
|
|
181
|
+
];
|
|
182
|
+
};
|