tla-ts 0.0.4-dev.e9adbb1 → 0.0.4-dev.ee344a5
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 +45 -1
- 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 +515 -0
- package/dist/prove.js +515 -0
- package/dist/run-cli.cjs +30 -47
- package/dist/run-cli.js +30 -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/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-generate.ts → obligations/generate.ts} +134 -61
- package/src/{obligations.ts → obligations/index.ts} +26 -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} +5 -59
- 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/parallel-worker.ts +2 -18
- package/src/parallel.ts +10 -43
- package/src/prove/decide-pool.ts +100 -0
- package/src/prove/deciders.ts +27 -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 +477 -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 +27 -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 +29 -0
- package/src/prove/prove.ts +417 -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 +96 -0
- package/src/prove/tlaps-module.ts +510 -0
- package/src/prove/verdict.ts +13 -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 +63 -10
- package/src/worker.ts +65 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
- /package/src/{obligation-format.ts → obligations/format.ts} +0 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Decides the text tlapm sends LS4, as LS4 does: unsat when the negated obligation has no model,
|
|
2
|
+
// which proves the obligation.
|
|
3
|
+
|
|
4
|
+
import { TlaSyntaxError } from "../../errors";
|
|
5
|
+
import type { DecideOptions, Verdict } from "../verdict";
|
|
6
|
+
import { getSatisfiability, type PtlFormula } from "./tableau";
|
|
7
|
+
|
|
8
|
+
// LS4's input syntax, as tlapm writes it: every operator is parenthesized except the facts of a
|
|
9
|
+
// sequent, which are joined by & before ->.
|
|
10
|
+
const parseLs4 = (text: string): PtlFormula => {
|
|
11
|
+
const tokens = text.match(/<->|->|[()~&|]|[\w']+|\S/g) ?? [];
|
|
12
|
+
const state = { position: 0 };
|
|
13
|
+
const take = (expected?: string) => {
|
|
14
|
+
const token = tokens[state.position++];
|
|
15
|
+
if (token === undefined || (expected !== undefined && token !== expected))
|
|
16
|
+
throw new TlaSyntaxError(
|
|
17
|
+
`Expected ${expected ?? "a formula"} in LS4 input, found ${token ?? "the end"}`,
|
|
18
|
+
);
|
|
19
|
+
return token;
|
|
20
|
+
};
|
|
21
|
+
const parseUnary = (): PtlFormula => {
|
|
22
|
+
const token = take();
|
|
23
|
+
if (token === "(") {
|
|
24
|
+
const inner = parseEquiv();
|
|
25
|
+
take(")");
|
|
26
|
+
return inner;
|
|
27
|
+
}
|
|
28
|
+
if (token === "~" || token === "not") return { kind: "Not", body: parseUnary() };
|
|
29
|
+
if (token === "always") return { kind: "Always", body: parseUnary() };
|
|
30
|
+
if (token === "sometime") return { kind: "Eventually", body: parseUnary() };
|
|
31
|
+
if (token === "next") return { kind: "Next", body: parseUnary() };
|
|
32
|
+
if (!/^[\w']+$/.test(token))
|
|
33
|
+
throw new TlaSyntaxError(`Expected a formula in LS4 input, found ${token}`);
|
|
34
|
+
return { kind: "Atom", name: token };
|
|
35
|
+
};
|
|
36
|
+
const parseJunction = (operator: string, kind: "And" | "Or", parseOperand: () => PtlFormula) => {
|
|
37
|
+
const operands = [parseOperand()];
|
|
38
|
+
while (tokens[state.position] === operator) {
|
|
39
|
+
take();
|
|
40
|
+
operands.push(parseOperand());
|
|
41
|
+
}
|
|
42
|
+
return operands.reduce((left, right) => ({ kind, left, right }));
|
|
43
|
+
};
|
|
44
|
+
const parseImplies = (): PtlFormula => {
|
|
45
|
+
const left = parseJunction("|", "Or", () => parseJunction("&", "And", parseUnary));
|
|
46
|
+
if (tokens[state.position] !== "->") return left;
|
|
47
|
+
take();
|
|
48
|
+
return { kind: "Implies", left, right: parseImplies() };
|
|
49
|
+
};
|
|
50
|
+
const parseEquiv = (): PtlFormula => {
|
|
51
|
+
const left = parseImplies();
|
|
52
|
+
if (tokens[state.position] !== "<->") return left;
|
|
53
|
+
take();
|
|
54
|
+
return { kind: "Equiv", left, right: parseEquiv() };
|
|
55
|
+
};
|
|
56
|
+
const formula = parseEquiv();
|
|
57
|
+
if (state.position < tokens.length)
|
|
58
|
+
throw new TlaSyntaxError(`Expected the end of LS4 input, found ${tokens[state.position]}`);
|
|
59
|
+
return formula;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const decidePtl = (text: string, options: DecideOptions): Verdict => {
|
|
63
|
+
const deadline = performance.now() + options.timeLimitMs;
|
|
64
|
+
return getSatisfiability(
|
|
65
|
+
parseLs4(text),
|
|
66
|
+
() => options.signal?.aborted === true || performance.now() > deadline,
|
|
67
|
+
);
|
|
68
|
+
};
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// tlapm's PTL backend up to the text it sends LS4 (Pltl.process_obligation, Prep.flatten, and
|
|
2
|
+
// Ls4's printer): temporal operators reduce to [], <> and prime, assumptions that hold
|
|
3
|
+
// always get a [], and every other subformula becomes an atom named by a hash of its printed form.
|
|
4
|
+
// The text is the negated obligation.
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
adjoin,
|
|
8
|
+
applyBuiltin,
|
|
9
|
+
at,
|
|
10
|
+
bumpBy,
|
|
11
|
+
mapAccumulate,
|
|
12
|
+
mapScoped,
|
|
13
|
+
shift,
|
|
14
|
+
shiftExpr,
|
|
15
|
+
substituteExpr,
|
|
16
|
+
substituteHyp,
|
|
17
|
+
type Builtin,
|
|
18
|
+
type Expr,
|
|
19
|
+
type Hyp,
|
|
20
|
+
type Level,
|
|
21
|
+
type Scope,
|
|
22
|
+
type Sequent,
|
|
23
|
+
type Substitution,
|
|
24
|
+
} from "../../obligations/expression";
|
|
25
|
+
import { renderDoc } from "../../obligations/format";
|
|
26
|
+
import { getLevelInfo } from "../../obligations/levels";
|
|
27
|
+
import { printExpression } from "../../obligations/printer";
|
|
28
|
+
import { expandAction, expandFairness, expandLeadsto, getTime } from "../../obligations/temporal";
|
|
29
|
+
import { getMd5 } from "../prep/md5";
|
|
30
|
+
|
|
31
|
+
const KIND_NAMES: Record<Level, string> = {
|
|
32
|
+
Constant: "CONSTANT",
|
|
33
|
+
State: "STATE",
|
|
34
|
+
Action: "ACTION",
|
|
35
|
+
Temporal: "TEMPORAL",
|
|
36
|
+
Unknown: "Unknown",
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const LEVEL_NAMES = ["CONSTANT", "STATE", "ACTION", "TEMPORAL"];
|
|
40
|
+
|
|
41
|
+
// Coalesce.rename_with_loc: each declared name gets its kind in front, or its level for a definition,
|
|
42
|
+
// as in VARIABLE_x_ and CONSTANT_i_, so atoms print with the names tlapm hashes. A variable a
|
|
43
|
+
// temporal quantifier binds keeps its name.
|
|
44
|
+
const renameSequent = (scope: Scope | undefined, sequent: Sequent): Sequent => {
|
|
45
|
+
const [inner, context] = mapAccumulate(
|
|
46
|
+
scope,
|
|
47
|
+
sequent.context,
|
|
48
|
+
(current, hyp): [Scope | undefined, Hyp] => {
|
|
49
|
+
const renamed = ((): Hyp => {
|
|
50
|
+
switch (hyp.kind) {
|
|
51
|
+
case "Fresh":
|
|
52
|
+
return {
|
|
53
|
+
...hyp,
|
|
54
|
+
name: `${KIND_NAMES[hyp.level]}_${hyp.name}_`,
|
|
55
|
+
domain: hyp.domain && renameDeclarations(current, hyp.domain),
|
|
56
|
+
};
|
|
57
|
+
case "Flex":
|
|
58
|
+
return { ...hyp, name: `VARIABLE_${hyp.name}_` };
|
|
59
|
+
case "Defn": {
|
|
60
|
+
const definition = hyp.definition;
|
|
61
|
+
if (definition.kind !== "Operator") return hyp;
|
|
62
|
+
const body = renameDeclarations(current, definition.body);
|
|
63
|
+
const level = LEVEL_NAMES[getLevelInfo(current, body).level];
|
|
64
|
+
return {
|
|
65
|
+
...hyp,
|
|
66
|
+
definition: { ...definition, name: `${level}_${definition.name}_`, body },
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
case "Fact":
|
|
70
|
+
return { ...hyp, expression: renameDeclarations(current, hyp.expression) };
|
|
71
|
+
}
|
|
72
|
+
})();
|
|
73
|
+
return [adjoin(current, renamed), renamed];
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
return { context, active: renameDeclarations(inner, sequent.active) };
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const renameDeclarations = (scope: Scope | undefined, expression: Expr): Expr => {
|
|
80
|
+
if (expression.kind === "Sequent")
|
|
81
|
+
return { ...expression, sequent: renameSequent(scope, expression.sequent) };
|
|
82
|
+
const mapped = mapScoped(scope, expression, renameDeclarations, adjoin, "all");
|
|
83
|
+
switch (mapped.kind) {
|
|
84
|
+
case "Lambda":
|
|
85
|
+
return {
|
|
86
|
+
...mapped,
|
|
87
|
+
params: mapped.params.map((param) => ({ ...param, name: `Unknown_${param.name}_` })),
|
|
88
|
+
};
|
|
89
|
+
case "Quant":
|
|
90
|
+
case "Fcn":
|
|
91
|
+
case "SetOf":
|
|
92
|
+
return {
|
|
93
|
+
...mapped,
|
|
94
|
+
bounds: mapped.bounds.map((bound) => ({
|
|
95
|
+
...bound,
|
|
96
|
+
name: `${KIND_NAMES[bound.level]}_${bound.name}_`,
|
|
97
|
+
})),
|
|
98
|
+
};
|
|
99
|
+
case "Choose":
|
|
100
|
+
case "SetSt":
|
|
101
|
+
return { ...mapped, name: `CONSTANT_${mapped.name}_` };
|
|
102
|
+
default:
|
|
103
|
+
return mapped;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
interface PreparedHyps {
|
|
108
|
+
scope: Scope | undefined;
|
|
109
|
+
substitution: Substitution;
|
|
110
|
+
context: Hyp[];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Pltl.expand_domains and Pltl.box_assumptions: NEW x \in S becomes NEW x and the fact x \in S, and
|
|
114
|
+
// each visible fact that holds always gets a [].
|
|
115
|
+
const prepareHyps = (scope: Scope | undefined, expression: Expr): Expr => {
|
|
116
|
+
if (expression.kind !== "Sequent")
|
|
117
|
+
return mapScoped(scope, expression, prepareHyps, adjoin, "all");
|
|
118
|
+
const state: PreparedHyps = { scope, substitution: shift(0), context: [] };
|
|
119
|
+
const push = (hyp: Hyp) => {
|
|
120
|
+
const prepared =
|
|
121
|
+
hyp.kind === "Fact" && hyp.visibility === "Visible" && hyp.time === "Always"
|
|
122
|
+
? { ...hyp, expression: applyBuiltin("BoxTrue", [hyp.expression], hyp.expression) }
|
|
123
|
+
: hyp;
|
|
124
|
+
state.context.push(prepared);
|
|
125
|
+
state.scope = adjoin(state.scope, prepared);
|
|
126
|
+
};
|
|
127
|
+
for (const original of expression.sequent.context) {
|
|
128
|
+
const hyp = substituteHyp(state.substitution, original);
|
|
129
|
+
if (hyp.kind === "Fact") {
|
|
130
|
+
push({ ...hyp, expression: prepareHyps(state.scope, hyp.expression) });
|
|
131
|
+
state.substitution = bumpBy(1, state.substitution);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (hyp.kind !== "Fresh" || !hyp.domain || original.kind !== "Fresh" || !original.domain) {
|
|
135
|
+
push(hyp);
|
|
136
|
+
state.substitution = bumpBy(1, state.substitution);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
const domain = prepareHyps(state.scope, hyp.domain);
|
|
140
|
+
push({ ...hyp, domain: undefined });
|
|
141
|
+
// tlapm judges the time of the domain as written, one hypothesis too deep.
|
|
142
|
+
push({
|
|
143
|
+
kind: "Fact",
|
|
144
|
+
expression: applyBuiltin(
|
|
145
|
+
"Mem",
|
|
146
|
+
[at({ kind: "Ix", index: 1 }, domain), shiftExpr(1, domain)],
|
|
147
|
+
domain,
|
|
148
|
+
),
|
|
149
|
+
visibility: hyp.domainVisibility,
|
|
150
|
+
time: hyp.level === "Constant" ? getTime(state.scope, original.domain) : "Now",
|
|
151
|
+
location: hyp.location,
|
|
152
|
+
});
|
|
153
|
+
state.substitution = {
|
|
154
|
+
kind: "Compose",
|
|
155
|
+
outer: shift(1),
|
|
156
|
+
inner: bumpBy(1, state.substitution),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
const active = substituteExpr(state.substitution, expression.sequent.active);
|
|
160
|
+
return {
|
|
161
|
+
...expression,
|
|
162
|
+
sequent: { context: state.context, active: prepareHyps(state.scope, active) },
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const BINARY: Partial<Record<Builtin, string>> = {
|
|
167
|
+
Conj: "&",
|
|
168
|
+
Disj: "|",
|
|
169
|
+
Implies: "->",
|
|
170
|
+
Equiv: "<->",
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const UNARY: Partial<Record<Builtin, string>> = {
|
|
174
|
+
Neg: "~",
|
|
175
|
+
BoxTrue: "always",
|
|
176
|
+
BoxFalse: "always",
|
|
177
|
+
Diamond: "sometime",
|
|
178
|
+
Prime: "next",
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// Prep.flatten, which tlapm applies after Pltl.process_obligation has made every equality an atom:
|
|
182
|
+
// TRUE drops out of the conjunctions of each visible fact, and a fact left with nothing disappears.
|
|
183
|
+
const removeTrue = (expression: Expr): Expr | undefined => {
|
|
184
|
+
if (expression.kind === "Internal" && expression.builtin === "TRUE") return undefined;
|
|
185
|
+
if (expression.kind === "List" && expression.bullet !== "Or") {
|
|
186
|
+
const items = expression.items.flatMap((item) => removeTrue(item) ?? []);
|
|
187
|
+
if (items.length <= 1) return items[0];
|
|
188
|
+
return { ...expression, bullet: "And", items };
|
|
189
|
+
}
|
|
190
|
+
if (
|
|
191
|
+
expression.kind !== "Apply" ||
|
|
192
|
+
expression.operator.kind !== "Internal" ||
|
|
193
|
+
expression.operator.builtin !== "Conj" ||
|
|
194
|
+
expression.args.length !== 2
|
|
195
|
+
)
|
|
196
|
+
return expression;
|
|
197
|
+
const left = removeTrue(expression.args[0]);
|
|
198
|
+
const right = removeTrue(expression.args[1]);
|
|
199
|
+
return left && right ? { ...expression, args: [left, right] } : (left ?? right);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const TRUE_TEXT = "(cc | ~ cc)";
|
|
203
|
+
|
|
204
|
+
// The text tlapm sends LS4 for an obligation after Prep.normalize_expand.
|
|
205
|
+
export const encodePtl = (sequent: Sequent): string => {
|
|
206
|
+
// Ls4's printer. A sequent becomes an implication from its visible facts, joined by &, and
|
|
207
|
+
// Prep.flatten applies to the facts of the outermost one.
|
|
208
|
+
const print = (hyps: Hyp[], expression: Expr, isOutermost = false): string => {
|
|
209
|
+
const recurse = (inner: Expr) => print(hyps, inner);
|
|
210
|
+
switch (expression.kind) {
|
|
211
|
+
case "Internal":
|
|
212
|
+
if (expression.builtin === "TRUE") return TRUE_TEXT;
|
|
213
|
+
if (expression.builtin === "FALSE") return "(cc & ~ cc)";
|
|
214
|
+
break;
|
|
215
|
+
case "Apply": {
|
|
216
|
+
const operator = expression.operator;
|
|
217
|
+
const args = expression.args;
|
|
218
|
+
if (operator.kind !== "Internal") break;
|
|
219
|
+
const binary = BINARY[operator.builtin];
|
|
220
|
+
if (binary && args.length === 2)
|
|
221
|
+
return `(${recurse(args[0])} ${binary} ${recurse(args[1])})`;
|
|
222
|
+
const unary = UNARY[operator.builtin];
|
|
223
|
+
if (unary && args.length === 1) return `(${unary} ${recurse(args[0])})`;
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
case "List": {
|
|
227
|
+
if (expression.items.length === 0) return TRUE_TEXT;
|
|
228
|
+
const junction = expression.bullet === "Or" ? "|" : "&";
|
|
229
|
+
return expression.items
|
|
230
|
+
.map(recurse)
|
|
231
|
+
.reduceRight((right, left) => `(${left} ${junction} ${right})`);
|
|
232
|
+
}
|
|
233
|
+
case "Sequent": {
|
|
234
|
+
const inner = [...hyps];
|
|
235
|
+
const facts: string[] = [];
|
|
236
|
+
for (const hyp of expression.sequent.context) {
|
|
237
|
+
const fact =
|
|
238
|
+
hyp.kind !== "Fact" || hyp.visibility !== "Visible"
|
|
239
|
+
? undefined
|
|
240
|
+
: isOutermost
|
|
241
|
+
? removeTrue(hyp.expression)
|
|
242
|
+
: hyp.expression;
|
|
243
|
+
if (fact) facts.push(print(inner, fact));
|
|
244
|
+
inner.push(hyp);
|
|
245
|
+
}
|
|
246
|
+
const goal = print(inner, expression.sequent.active);
|
|
247
|
+
return facts.length === 0 ? goal : `(${facts.join(" & ")} -> ${goal})`;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
// Pltl.coalesce_fol: an atom is named by the first five hex digits of the MD5 of its print.
|
|
251
|
+
return `h${getMd5(renderDoc(printExpression(hyps, expression))).slice(0, 5)}`;
|
|
252
|
+
};
|
|
253
|
+
const renamed = renameDeclarations(undefined, { kind: "Sequent", sequent });
|
|
254
|
+
const prepared = prepareHyps(undefined, expandAction(expandLeadsto(expandFairness(renamed))));
|
|
255
|
+
return `not (${print([], prepared, true)})`;
|
|
256
|
+
};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// A conflict-driven clause learning SAT solver for the clauses of one tableau node. A literal is a
|
|
2
|
+
// variable number, negated when below zero. The solver keeps what it learns across calls, which add
|
|
3
|
+
// clauses or assume literals. It decides each open variable false first, so a model sets few
|
|
4
|
+
// variables, and a model holds 1 for each true variable and -1 for each false one. It gives up at
|
|
5
|
+
// the first conflict after isExpired turns true.
|
|
6
|
+
|
|
7
|
+
const getIndex = (literal: number) => (literal > 0 ? 2 * literal : -2 * literal + 1);
|
|
8
|
+
|
|
9
|
+
export const createSolver = (variableCount: number, isExpired: () => boolean) => {
|
|
10
|
+
const values = new Int8Array(variableCount + 1);
|
|
11
|
+
const levels = new Int32Array(variableCount + 1);
|
|
12
|
+
const reasons = Array.from<number[] | undefined>({ length: variableCount + 1 });
|
|
13
|
+
const activities = new Float64Array(variableCount + 1);
|
|
14
|
+
const seen = new Uint8Array(variableCount + 1);
|
|
15
|
+
const watches: number[][][] = Array.from({ length: 2 * variableCount + 2 }, () => []);
|
|
16
|
+
const trail: number[] = [];
|
|
17
|
+
const levelStarts: number[] = [];
|
|
18
|
+
const state = { head: 0, bump: 1, isUnsatisfiable: false };
|
|
19
|
+
const getValue = (literal: number) => (literal > 0 ? values[literal] : -values[-literal]);
|
|
20
|
+
const assign = (literal: number, reason: number[] | undefined) => {
|
|
21
|
+
const variable = Math.abs(literal);
|
|
22
|
+
values[variable] = literal > 0 ? 1 : -1;
|
|
23
|
+
levels[variable] = levelStarts.length;
|
|
24
|
+
reasons[variable] = reason;
|
|
25
|
+
trail.push(literal);
|
|
26
|
+
};
|
|
27
|
+
const watch = (clause: number[]) => {
|
|
28
|
+
watches[getIndex(clause[0])].push(clause);
|
|
29
|
+
watches[getIndex(clause[1])].push(clause);
|
|
30
|
+
};
|
|
31
|
+
// Moves each watch off a literal that turned false, and returns the clause of a conflict. The
|
|
32
|
+
// literal a clause implies sits first in it.
|
|
33
|
+
const propagate = () => {
|
|
34
|
+
for (; state.head < trail.length; state.head++) {
|
|
35
|
+
const falsified = -trail[state.head];
|
|
36
|
+
const watching = watches[getIndex(falsified)];
|
|
37
|
+
for (let position = 0; position < watching.length; position++) {
|
|
38
|
+
const clause = watching[position];
|
|
39
|
+
if (clause[0] === falsified) [clause[0], clause[1]] = [clause[1], clause[0]];
|
|
40
|
+
if (getValue(clause[0]) > 0) continue;
|
|
41
|
+
const replacement = clause.findIndex(
|
|
42
|
+
(literal, index) => index > 1 && getValue(literal) >= 0,
|
|
43
|
+
);
|
|
44
|
+
if (replacement !== -1) {
|
|
45
|
+
[clause[1], clause[replacement]] = [clause[replacement], clause[1]];
|
|
46
|
+
watches[getIndex(clause[1])].push(clause);
|
|
47
|
+
watching[position--] = watching[watching.length - 1];
|
|
48
|
+
watching.pop();
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (getValue(clause[0]) < 0) return clause;
|
|
52
|
+
assign(clause[0], clause);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return undefined;
|
|
56
|
+
};
|
|
57
|
+
const backtrack = (level: number) => {
|
|
58
|
+
if (level >= levelStarts.length) return;
|
|
59
|
+
const size = levelStarts[level];
|
|
60
|
+
while (trail.length > size) values[Math.abs(trail.pop() ?? 0)] = 0;
|
|
61
|
+
levelStarts.length = level;
|
|
62
|
+
state.head = trail.length;
|
|
63
|
+
};
|
|
64
|
+
// Resolves the conflict back to the first literal of the current level that all its paths pass
|
|
65
|
+
// through, and returns the learned clause with that literal first.
|
|
66
|
+
const analyze = (conflict: number[]) => {
|
|
67
|
+
const learned = [0];
|
|
68
|
+
const current = levelStarts.length;
|
|
69
|
+
const walk = { pending: 0, literal: 0, index: trail.length - 1 };
|
|
70
|
+
for (let clause: number[] | undefined = conflict; ;) {
|
|
71
|
+
for (const literal of clause?.slice(walk.literal === 0 ? 0 : 1) ?? []) {
|
|
72
|
+
const variable = Math.abs(literal);
|
|
73
|
+
if (seen[variable] || levels[variable] === 0) continue;
|
|
74
|
+
seen[variable] = 1;
|
|
75
|
+
activities[variable] += state.bump;
|
|
76
|
+
if (levels[variable] === current) walk.pending++;
|
|
77
|
+
else learned.push(literal);
|
|
78
|
+
}
|
|
79
|
+
while (!seen[Math.abs(trail[walk.index])]) walk.index--;
|
|
80
|
+
walk.literal = trail[walk.index--];
|
|
81
|
+
seen[Math.abs(walk.literal)] = 0;
|
|
82
|
+
clause = reasons[Math.abs(walk.literal)];
|
|
83
|
+
if (--walk.pending === 0) break;
|
|
84
|
+
}
|
|
85
|
+
learned[0] = -walk.literal;
|
|
86
|
+
learned.forEach((literal) => (seen[Math.abs(literal)] = 0));
|
|
87
|
+
state.bump *= 1.05;
|
|
88
|
+
return learned;
|
|
89
|
+
};
|
|
90
|
+
// Literals false for good are dropped, since the solver adds clauses only at level 0.
|
|
91
|
+
const addClause = (clause: number[]) => {
|
|
92
|
+
backtrack(0);
|
|
93
|
+
const literals = [...new Set(clause)];
|
|
94
|
+
if (state.isUnsatisfiable || literals.some((literal) => getValue(literal) > 0)) return;
|
|
95
|
+
if (literals.some((literal) => literals.includes(-literal))) return;
|
|
96
|
+
const open = literals.filter((literal) => getValue(literal) === 0);
|
|
97
|
+
if (open.length > 1) {
|
|
98
|
+
watch(open);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
if (open.length === 0) {
|
|
102
|
+
state.isUnsatisfiable = true;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
assign(open[0], undefined);
|
|
106
|
+
if (propagate()) state.isUnsatisfiable = true;
|
|
107
|
+
};
|
|
108
|
+
// A model of the clauses in which every assumed literal holds, or unsat when there is none.
|
|
109
|
+
const solve = (assumptions: number[]): Int8Array | "unsat" | "unknown" => {
|
|
110
|
+
backtrack(0);
|
|
111
|
+
if (state.isUnsatisfiable) return "unsat";
|
|
112
|
+
for (;;) {
|
|
113
|
+
const conflict = propagate();
|
|
114
|
+
if (conflict) {
|
|
115
|
+
if (levelStarts.length === 0) {
|
|
116
|
+
state.isUnsatisfiable = true;
|
|
117
|
+
return "unsat";
|
|
118
|
+
}
|
|
119
|
+
if (isExpired()) return "unknown";
|
|
120
|
+
const learned = analyze(conflict);
|
|
121
|
+
const deepest = learned
|
|
122
|
+
.slice(1)
|
|
123
|
+
.reduce(
|
|
124
|
+
(best, literal, index) =>
|
|
125
|
+
levels[Math.abs(literal)] > levels[Math.abs(learned[best])] ? index + 1 : best,
|
|
126
|
+
1,
|
|
127
|
+
);
|
|
128
|
+
if (learned.length > 1) [learned[1], learned[deepest]] = [learned[deepest], learned[1]];
|
|
129
|
+
backtrack(learned.length === 1 ? 0 : levels[Math.abs(learned[1])]);
|
|
130
|
+
if (learned.length > 1) watch(learned);
|
|
131
|
+
assign(learned[0], learned.length > 1 ? learned : undefined);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const assumption = assumptions.find((literal) => getValue(literal) <= 0);
|
|
135
|
+
if (assumption !== undefined && getValue(assumption) < 0) return "unsat";
|
|
136
|
+
const decision =
|
|
137
|
+
assumption ??
|
|
138
|
+
-values.reduce(
|
|
139
|
+
(best, value, variable) =>
|
|
140
|
+
variable > 0 && value === 0 && (best === 0 || activities[variable] > activities[best])
|
|
141
|
+
? variable
|
|
142
|
+
: best,
|
|
143
|
+
0,
|
|
144
|
+
);
|
|
145
|
+
if (decision === 0) return Int8Array.from(values);
|
|
146
|
+
levelStarts.push(trail.length);
|
|
147
|
+
assign(decision, undefined);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
return { addClause, solve };
|
|
151
|
+
};
|