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,322 @@
|
|
|
1
|
+
// Ported from Isabelle2025's Provers/blast.ML. Copyright (c) 1986-2025 University of Cambridge,
|
|
2
|
+
// Technische Universitaet Muenchen, and contributors, see LICENSE.
|
|
3
|
+
//
|
|
4
|
+
// Operations on branches: naive substitution for equalities, closing a branch by complementary
|
|
5
|
+
// literals, and the pruning of choice points.
|
|
6
|
+
|
|
7
|
+
import { orElseGoal, thenGoal, type GoalTactic } from "../../pure/tactical";
|
|
8
|
+
import { assumeTac, eqAssumeTac, ematchTac } from "../../pure/tactic";
|
|
9
|
+
import {
|
|
10
|
+
unify,
|
|
11
|
+
type BlastState,
|
|
12
|
+
type Branch,
|
|
13
|
+
type Choices,
|
|
14
|
+
type Formula,
|
|
15
|
+
type Level,
|
|
16
|
+
} from "./state";
|
|
17
|
+
import {
|
|
18
|
+
GOAL_NAME,
|
|
19
|
+
blastAconv,
|
|
20
|
+
destConstApp,
|
|
21
|
+
hasLooseBound0,
|
|
22
|
+
incrBlastBoundvars,
|
|
23
|
+
insTerm,
|
|
24
|
+
isGoal,
|
|
25
|
+
mkBlastAbs,
|
|
26
|
+
mkBlastApp,
|
|
27
|
+
mkBlastConst,
|
|
28
|
+
mkBlastVar,
|
|
29
|
+
varOccur,
|
|
30
|
+
type BlastTerm,
|
|
31
|
+
} from "./term";
|
|
32
|
+
|
|
33
|
+
export const negate = (state: BlastState, term: BlastTerm) =>
|
|
34
|
+
mkBlastApp(mkBlastConst(state.context.data.notName), term);
|
|
35
|
+
|
|
36
|
+
// Converts a *Goal* to an ordinary negation.
|
|
37
|
+
export const negOfGoal = (state: BlastState, term: BlastTerm) => {
|
|
38
|
+
const goal = destConstApp(term, GOAL_NAME);
|
|
39
|
+
return goal === undefined ? term : negate(state, goal);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const negOfGoal2 = (state: BlastState, formula: Formula): Formula => ({
|
|
43
|
+
term: negOfGoal(state, formula.term),
|
|
44
|
+
md: formula.md,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Converts the goals to negations in the safe parts of a branch. They could have been moved there
|
|
48
|
+
// from the literals by an equality substitution.
|
|
49
|
+
export const negOfGoals = (state: BlastState, pairs: readonly Level[]): Level[] =>
|
|
50
|
+
pairs.map((level) => ({
|
|
51
|
+
safe: level.safe.map((formula) => negOfGoal2(state, formula)),
|
|
52
|
+
unsafe: level.unsafe,
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
// Replaces the atomic term old by new, returning the very same term where nothing changes.
|
|
56
|
+
const substAtomic = (old: BlastTerm, replacement: BlastTerm, term: BlastTerm): BlastTerm => {
|
|
57
|
+
if (term.kind === "Var" && term.ref.value !== undefined)
|
|
58
|
+
return substAtomic(old, replacement, term.ref.value);
|
|
59
|
+
if (term.kind === "Abs") {
|
|
60
|
+
const body = substAtomic(old, replacement, term.body);
|
|
61
|
+
return body === term.body ? term : mkBlastAbs(term.name, body);
|
|
62
|
+
}
|
|
63
|
+
if (term.kind === "App") {
|
|
64
|
+
const fun = substAtomic(old, replacement, term.fun);
|
|
65
|
+
const arg = substAtomic(old, replacement, term.arg);
|
|
66
|
+
return fun === term.fun && arg === term.arg ? term : mkBlastApp(fun, arg);
|
|
67
|
+
}
|
|
68
|
+
return blastAconv(term, old) ? replacement : term;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// Eta-contracts a term from outside, just enough to reduce it to an atom.
|
|
72
|
+
const etaContractAtom = (term: BlastTerm): BlastTerm => {
|
|
73
|
+
if (term.kind !== "Abs") return term;
|
|
74
|
+
const body = etaContract2(term.body);
|
|
75
|
+
return body.kind === "App" && body.arg.kind === "Bound" && body.arg.index === 0
|
|
76
|
+
? hasLooseBound0(body.fun)
|
|
77
|
+
? term
|
|
78
|
+
: etaContractAtom(incrBlastBoundvars(-1, body.fun))
|
|
79
|
+
: term;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const etaContract2 = (term: BlastTerm): BlastTerm =>
|
|
83
|
+
term.kind === "App" ? mkBlastApp(term.fun, etaContractAtom(term.arg)) : etaContractAtom(term);
|
|
84
|
+
|
|
85
|
+
// Can term occur in other? Does not examine the arguments of Skolem terms, which substitution does
|
|
86
|
+
// not affect, and is reflexive because hyp_subst_tac fails on x = x. No variables may occur in
|
|
87
|
+
// other except the arguments of a Skolem term, so that no equation that could be instantiated to
|
|
88
|
+
// a cycle is deleted.
|
|
89
|
+
const substOccur = (term: BlastTerm, other: BlastTerm) => {
|
|
90
|
+
const vars = term.kind === "Skolem" ? term.args : [];
|
|
91
|
+
const occEq = (current: BlastTerm): boolean => blastAconv(term, current) || occ(current);
|
|
92
|
+
const occ = (current: BlastTerm): boolean => {
|
|
93
|
+
switch (current.kind) {
|
|
94
|
+
case "Var":
|
|
95
|
+
return current.ref.value === undefined
|
|
96
|
+
? !vars.includes(current.ref)
|
|
97
|
+
: occEq(current.ref.value);
|
|
98
|
+
case "Abs":
|
|
99
|
+
return occEq(current.body);
|
|
100
|
+
case "App":
|
|
101
|
+
return occEq(current.arg) || occEq(current.fun);
|
|
102
|
+
default:
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
return occEq(other);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// Takes apart an equality and orients it to eliminate a Skolem term or a free variable, or gives
|
|
110
|
+
// undefined (DEST_EQ) if it has no substitutable side.
|
|
111
|
+
const orientEquality = (state: BlastState, term: BlastTerm): [BlastTerm, BlastTerm] | undefined => {
|
|
112
|
+
if (
|
|
113
|
+
term.kind !== "App" ||
|
|
114
|
+
term.fun.kind !== "App" ||
|
|
115
|
+
term.fun.fun.kind !== "Const" ||
|
|
116
|
+
term.fun.fun.name !== state.context.data.equalityName
|
|
117
|
+
)
|
|
118
|
+
return undefined;
|
|
119
|
+
const left = etaContractAtom(term.fun.arg);
|
|
120
|
+
const right = etaContractAtom(term.arg);
|
|
121
|
+
const check = (old: BlastTerm, replacement: BlastTerm): [BlastTerm, BlastTerm] | undefined =>
|
|
122
|
+
substOccur(old, replacement) ? undefined : [old, replacement];
|
|
123
|
+
if (left.kind === "Skolem") return check(left, right);
|
|
124
|
+
if (right.kind === "Skolem") return check(right, left);
|
|
125
|
+
if (left.kind === "Free") return check(left, right);
|
|
126
|
+
if (right.kind === "Free") return check(right, left);
|
|
127
|
+
return undefined;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
interface Substituted {
|
|
131
|
+
readonly changed: readonly Formula[];
|
|
132
|
+
readonly kept: readonly Formula[];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Substitutes through the branch if the formula is an equality with a substitutable variable.
|
|
136
|
+
// Moves the affected formulae and literals back to the safe formulae, but it is not clear where
|
|
137
|
+
// they should go: this could make proofs fail.
|
|
138
|
+
export const equalSubst = (
|
|
139
|
+
state: BlastState,
|
|
140
|
+
formula: BlastTerm,
|
|
141
|
+
branch: Branch,
|
|
142
|
+
): Branch | undefined => {
|
|
143
|
+
const oriented = orientEquality(state, formula);
|
|
144
|
+
if (oriented === undefined) return undefined;
|
|
145
|
+
const subst = (term: BlastTerm) => substAtomic(oriented[0], oriented[1], term);
|
|
146
|
+
const subForms = (forms: readonly Formula[], changed: readonly Formula[]): Substituted =>
|
|
147
|
+
forms.reduceRight<Substituted>(
|
|
148
|
+
(result, form) => {
|
|
149
|
+
const substituted = subst(form.term);
|
|
150
|
+
return blastAconv(substituted, form.term)
|
|
151
|
+
? { changed: result.changed, kept: [form, ...result.kept] }
|
|
152
|
+
: { changed: [{ term: substituted, md: form.md }, ...result.changed], kept: result.kept };
|
|
153
|
+
},
|
|
154
|
+
{ changed, kept: [] },
|
|
155
|
+
);
|
|
156
|
+
const lits = branch.lits.reduceRight<{ changed: readonly Formula[]; lits: readonly BlastTerm[] }>(
|
|
157
|
+
(result, lit) => {
|
|
158
|
+
const substituted = subst(lit);
|
|
159
|
+
return blastAconv(substituted, lit)
|
|
160
|
+
? { changed: result.changed, lits: [substituted, ...result.lits] }
|
|
161
|
+
: { changed: [{ term: substituted, md: true }, ...result.changed], lits: result.lits };
|
|
162
|
+
},
|
|
163
|
+
{ changed: [], lits: [] },
|
|
164
|
+
);
|
|
165
|
+
const frames = branch.pairs.reduceRight<{
|
|
166
|
+
changed: readonly Formula[];
|
|
167
|
+
frames: readonly Level[];
|
|
168
|
+
}>(
|
|
169
|
+
(result, level) => {
|
|
170
|
+
const safe = subForms(level.safe, result.changed);
|
|
171
|
+
const unsafe = subForms(level.unsafe, safe.changed);
|
|
172
|
+
return {
|
|
173
|
+
changed: unsafe.changed,
|
|
174
|
+
frames: [{ safe: safe.kept, unsafe: unsafe.kept }, ...result.frames],
|
|
175
|
+
};
|
|
176
|
+
},
|
|
177
|
+
{ changed: lits.changed, frames: [] },
|
|
178
|
+
);
|
|
179
|
+
return {
|
|
180
|
+
pairs: [{ safe: frames.changed, unsafe: [] }, ...frames.frames],
|
|
181
|
+
lits: lits.lits,
|
|
182
|
+
vars: branch.vars,
|
|
183
|
+
lim: branch.lim,
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// Tries to unify complementary literals, giving the tactic that closes the branch.
|
|
188
|
+
const eAssumeTac = (state: BlastState) => orElseGoal(eqAssumeTac, assumeTac(state.context.limits));
|
|
189
|
+
|
|
190
|
+
// contr_tac: trying eq_contr_tac first increases the effort, slowing reconstruction.
|
|
191
|
+
const eContrTac = (state: BlastState) =>
|
|
192
|
+
thenGoal(ematchTac(state.context.limits, [state.context.data.notE]), eAssumeTac(state));
|
|
193
|
+
|
|
194
|
+
const close = (
|
|
195
|
+
state: BlastState,
|
|
196
|
+
left: BlastTerm,
|
|
197
|
+
right: BlastTerm,
|
|
198
|
+
tactic: (state: BlastState) => GoalTactic,
|
|
199
|
+
) => (unify(state, [], left, right) ? tactic(state) : undefined);
|
|
200
|
+
|
|
201
|
+
export const tryClose = (
|
|
202
|
+
state: BlastState,
|
|
203
|
+
formula: BlastTerm,
|
|
204
|
+
literal: BlastTerm,
|
|
205
|
+
): GoalTactic | undefined => {
|
|
206
|
+
const notName = state.context.data.notName;
|
|
207
|
+
const formulaGoal = destConstApp(formula, GOAL_NAME);
|
|
208
|
+
if (formulaGoal !== undefined) return close(state, formulaGoal, literal, eAssumeTac);
|
|
209
|
+
const literalGoal = destConstApp(literal, GOAL_NAME);
|
|
210
|
+
if (literalGoal !== undefined) return close(state, formula, literalGoal, eAssumeTac);
|
|
211
|
+
const formulaNot = destConstApp(formula, notName);
|
|
212
|
+
if (formulaNot !== undefined) return close(state, formulaNot, literal, eContrTac);
|
|
213
|
+
const literalNot = destConstApp(literal, notName);
|
|
214
|
+
if (literalNot !== undefined) return close(state, formula, literalNot, eContrTac);
|
|
215
|
+
return undefined;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// Were there Skolem terms in the premise? Must not chase variables.
|
|
219
|
+
const hasSkolem = (term: BlastTerm): boolean => {
|
|
220
|
+
switch (term.kind) {
|
|
221
|
+
case "Skolem":
|
|
222
|
+
return true;
|
|
223
|
+
case "Abs":
|
|
224
|
+
return hasSkolem(term.body);
|
|
225
|
+
case "App":
|
|
226
|
+
return hasSkolem(term.fun) || hasSkolem(term.arg);
|
|
227
|
+
default:
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// Attaches the may-duplicate flag to new formulae: those with Skolem terms may be duplicated.
|
|
233
|
+
export const joinMd = (md: boolean, terms: readonly BlastTerm[]): Formula[] =>
|
|
234
|
+
terms.map((term) => ({ term, md: hasSkolem(term) || md }));
|
|
235
|
+
|
|
236
|
+
// Whether any of the last count assignments of the trail below top occurs in vars or their
|
|
237
|
+
// instantiations.
|
|
238
|
+
const clashVar = (state: BlastState, vars: readonly BlastTerm[], count: number, top: number) => {
|
|
239
|
+
if (vars.length === 0) return false;
|
|
240
|
+
for (
|
|
241
|
+
let remaining = count, position = top - 1;
|
|
242
|
+
remaining !== 0 && position >= 0;
|
|
243
|
+
remaining -= 1
|
|
244
|
+
) {
|
|
245
|
+
const ref = state.trail[position];
|
|
246
|
+
if (vars.some((term) => varOccur(ref, term))) return true;
|
|
247
|
+
position -= 1;
|
|
248
|
+
}
|
|
249
|
+
return false;
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// Deletes the choice points for goals the latest inference proved, provided no variable of the
|
|
253
|
+
// next branch was updated. nbrs is the number of branches just before closing this one. Does not
|
|
254
|
+
// prune at the very end, to allow backtracking over bad proofs.
|
|
255
|
+
export const prune = (
|
|
256
|
+
state: BlastState,
|
|
257
|
+
nbrs: number,
|
|
258
|
+
nxtVars: readonly BlastTerm[],
|
|
259
|
+
choices: Choices,
|
|
260
|
+
): Choices => {
|
|
261
|
+
if (nbrs === 1) return choices;
|
|
262
|
+
let last = choices;
|
|
263
|
+
let ntrl = state.trail.length;
|
|
264
|
+
for (let current = choices; current !== undefined; current = current.tail) {
|
|
265
|
+
const choice = current.head;
|
|
266
|
+
if (choice.nbrs < nbrs) return last;
|
|
267
|
+
if (choice.nbrs > nbrs) continue;
|
|
268
|
+
if (clashVar(state, nxtVars, ntrl - choice.ntrl, ntrl)) return last;
|
|
269
|
+
last = current.tail;
|
|
270
|
+
ntrl = choice.ntrl;
|
|
271
|
+
}
|
|
272
|
+
return last;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
export const nextVars = (brs: readonly Branch[]) =>
|
|
276
|
+
brs.length === 0 ? [] : brs[0].vars.map((ref) => mkBlastVar(ref));
|
|
277
|
+
|
|
278
|
+
// Adds the literal, handling *Goal* and detecting duplicates. A new *Goal* turns the other goals
|
|
279
|
+
// into negations.
|
|
280
|
+
export const addLit = (state: BlastState, term: BlastTerm, lits: readonly BlastTerm[]) => {
|
|
281
|
+
const goal = destConstApp(term, GOAL_NAME);
|
|
282
|
+
if (goal === undefined) return insTerm(term, lits);
|
|
283
|
+
const notName = state.context.data.notName;
|
|
284
|
+
const isBad = (lit: BlastTerm) => {
|
|
285
|
+
if (isGoal(lit)) return true;
|
|
286
|
+
const negated = destConstApp(lit, notName);
|
|
287
|
+
return negated !== undefined && blastAconv(goal, negated);
|
|
288
|
+
};
|
|
289
|
+
const change = (lit: BlastTerm): BlastTerm[] => {
|
|
290
|
+
const inner = destConstApp(lit, GOAL_NAME) ?? destConstApp(lit, notName);
|
|
291
|
+
if (inner === undefined) return [lit];
|
|
292
|
+
return blastAconv(goal, inner) ? [] : [negate(state, inner)];
|
|
293
|
+
};
|
|
294
|
+
return [
|
|
295
|
+
mkBlastApp(mkBlastConst(GOAL_NAME), goal),
|
|
296
|
+
...(lits.some(isBad) ? lits.flatMap(change) : lits),
|
|
297
|
+
];
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
// The penalty for a branching factor: log2 seems a little too severe.
|
|
301
|
+
export const log4 = (count: number): number => (count < 4 ? 0 : 1 + log4(Math.floor(count / 4)));
|
|
302
|
+
|
|
303
|
+
// Whether term might be an instance of pattern, to detect recursive rules such as transitivity.
|
|
304
|
+
export const match = (state: BlastState, pattern: BlastTerm, term: BlastTerm): boolean => {
|
|
305
|
+
if (pattern.kind === "Var") return true;
|
|
306
|
+
if (pattern.kind === "Const" && term.kind === "Const") {
|
|
307
|
+
const notName = state.context.data.notName;
|
|
308
|
+
return (
|
|
309
|
+
(pattern.name === GOAL_NAME && term.name === notName) ||
|
|
310
|
+
(pattern.name === notName && term.name === GOAL_NAME) ||
|
|
311
|
+
(pattern.name === term.name &&
|
|
312
|
+
pattern.typargs.length === term.typargs.length &&
|
|
313
|
+
pattern.typargs.every((arg, index) => match(state, arg, term.typargs[index])))
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
if (pattern.kind === "Free" && term.kind === "Free") return pattern.name === term.name;
|
|
317
|
+
if (pattern.kind === "Bound" && term.kind === "Bound") return pattern.index === term.index;
|
|
318
|
+
if (pattern.kind === "Abs" && term.kind === "Abs") return match(state, pattern.body, term.body);
|
|
319
|
+
if (pattern.kind === "App" && term.kind === "App")
|
|
320
|
+
return match(state, pattern.fun, term.fun) && match(state, pattern.arg, term.arg);
|
|
321
|
+
return false;
|
|
322
|
+
};
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
// Ported from Isabelle2025's Provers/blast.ML. Copyright (c) 1986-2025 University of Cambridge,
|
|
2
|
+
// Technische Universitaet Muenchen, and contributors, see LICENSE.
|
|
3
|
+
//
|
|
4
|
+
// The tableau prover, based on leanTaP. It works on a list of branches, each with its unexpanded
|
|
5
|
+
// formulae, its literals and a bound on unsafe expansions. Every inference records the tactic
|
|
6
|
+
// that replays it; once all branches close, the continuation reconstructs the proof. Each
|
|
7
|
+
// recursion level that can be backtracked to owns a token, and a choice point raises it.
|
|
8
|
+
|
|
9
|
+
import type { Seq } from "../../pure/seq";
|
|
10
|
+
import type { GoalTactic } from "../../pure/tactical";
|
|
11
|
+
import { checkInterrupt } from "../../pure/interrupt";
|
|
12
|
+
import { cons, type List } from "../../pure/list";
|
|
13
|
+
import type { Thm } from "../../pure/thm";
|
|
14
|
+
import {
|
|
15
|
+
addLit,
|
|
16
|
+
equalSubst,
|
|
17
|
+
joinMd,
|
|
18
|
+
log4,
|
|
19
|
+
match,
|
|
20
|
+
negOfGoal,
|
|
21
|
+
negOfGoals,
|
|
22
|
+
nextVars,
|
|
23
|
+
prune,
|
|
24
|
+
tryClose,
|
|
25
|
+
} from "./branch";
|
|
26
|
+
import { negOfGoalTac, netMkRules, type TableauRule } from "./rules";
|
|
27
|
+
import {
|
|
28
|
+
backtrack,
|
|
29
|
+
clearTo,
|
|
30
|
+
isBacktrackTo,
|
|
31
|
+
unify,
|
|
32
|
+
type BlastState,
|
|
33
|
+
type Branch,
|
|
34
|
+
type Choices,
|
|
35
|
+
type Formula,
|
|
36
|
+
type Level,
|
|
37
|
+
} from "./state";
|
|
38
|
+
import {
|
|
39
|
+
addTermVars,
|
|
40
|
+
addTermsVars,
|
|
41
|
+
isGoal,
|
|
42
|
+
norm,
|
|
43
|
+
sameVars,
|
|
44
|
+
varsInVars,
|
|
45
|
+
type BlastTerm,
|
|
46
|
+
type VarRef,
|
|
47
|
+
} from "./term";
|
|
48
|
+
|
|
49
|
+
// The tactics that replay the inferences so far, latest first.
|
|
50
|
+
type Tactics = List<GoalTactic> | undefined;
|
|
51
|
+
|
|
52
|
+
export type Continuation = (tactics: Tactics, choices: Choices) => Seq<Thm>;
|
|
53
|
+
|
|
54
|
+
// A step either finishes the search or continues the same branches without a choice point, which
|
|
55
|
+
// blast.ML does by a tail call. prv loops on the latter, so a long search can't exhaust the stack.
|
|
56
|
+
type Step =
|
|
57
|
+
| { readonly kind: "done"; readonly result: Seq<Thm> }
|
|
58
|
+
| {
|
|
59
|
+
readonly kind: "next";
|
|
60
|
+
readonly tactics: Tactics;
|
|
61
|
+
readonly choices: Choices;
|
|
62
|
+
readonly brs: readonly Branch[];
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const done = (result: Seq<Thm>): Step => ({ kind: "done", result });
|
|
66
|
+
|
|
67
|
+
const addPremsVars = (prems: readonly (readonly BlastTerm[])[], vars: readonly VarRef[]) =>
|
|
68
|
+
prems.reduceRight((result, prem) => addTermsVars(prem, result), vars);
|
|
69
|
+
|
|
70
|
+
export const prove = (
|
|
71
|
+
state: BlastState,
|
|
72
|
+
initial: readonly Branch[],
|
|
73
|
+
cont: Continuation,
|
|
74
|
+
): Seq<Thm> => {
|
|
75
|
+
const context = state.context;
|
|
76
|
+
const safeList = [context.claset.safe0, context.claset.safep];
|
|
77
|
+
const unsafeList = [context.claset.unsafe];
|
|
78
|
+
|
|
79
|
+
// Applies a safe rule only, possibly allowing instantiation; defers any unsafe formulae.
|
|
80
|
+
const safeStep = (tactics: Tactics, choices: Choices, brs: readonly Branch[]): Step => {
|
|
81
|
+
const token = {};
|
|
82
|
+
const branch = brs[0];
|
|
83
|
+
const rest = brs.slice(1);
|
|
84
|
+
const head = branch.pairs[0];
|
|
85
|
+
const md = head.safe[0].md;
|
|
86
|
+
const queued: Level = { safe: head.safe.slice(1), unsafe: head.unsafe };
|
|
87
|
+
const pairs = [queued, ...branch.pairs.slice(1)];
|
|
88
|
+
const ntrl = state.trail.length;
|
|
89
|
+
const nbrs = brs.length;
|
|
90
|
+
const nxtVars = nextVars(rest);
|
|
91
|
+
const formula = norm(head.safe[0].term);
|
|
92
|
+
const rules = netMkRules(state, formula, branch.vars, safeList);
|
|
93
|
+
// Makes the new branches, decrementing lim if instantiations occur.
|
|
94
|
+
const newBr = (vars: readonly VarRef[], lim: number, prems: TableauRule["prems"]): Branch[] => [
|
|
95
|
+
...prems.map((prem): Branch => {
|
|
96
|
+
const level = { safe: joinMd(md, prem), unsafe: [] };
|
|
97
|
+
return prem.some(isGoal)
|
|
98
|
+
? {
|
|
99
|
+
pairs: [level, ...negOfGoals(state, pairs)],
|
|
100
|
+
lits: branch.lits.map((lit) => negOfGoal(state, lit)),
|
|
101
|
+
vars,
|
|
102
|
+
lim,
|
|
103
|
+
}
|
|
104
|
+
: { pairs: [level, ...pairs], lits: branch.lits, vars, lim };
|
|
105
|
+
}),
|
|
106
|
+
...rest,
|
|
107
|
+
];
|
|
108
|
+
// Seeks a matching rule; if unifiable, adds its premises to the branch. Undefined is
|
|
109
|
+
// NEWBRANCHES.
|
|
110
|
+
const deeper = (): Seq<Thm> | undefined => {
|
|
111
|
+
for (let position = 0; position < rules.length; position += 1) {
|
|
112
|
+
const rule = rules[position];
|
|
113
|
+
if (!unify(state, addTermVars(rule.major, []), rule.major, formula)) continue;
|
|
114
|
+
const isUpdated = ntrl < state.trail.length;
|
|
115
|
+
// Discourages branching updates.
|
|
116
|
+
const lim = isUpdated ? branch.lim - (1 + log4(rules.length)) : branch.lim;
|
|
117
|
+
const vars = addPremsVars(rule.prems, varsInVars(branch.vars));
|
|
118
|
+
const nextChoices = cons({ ntrl, nbrs, token }, choices);
|
|
119
|
+
const nextTactics = cons(rule.tactic(isUpdated, false, true), tactics);
|
|
120
|
+
try {
|
|
121
|
+
if (rule.prems.length === 0)
|
|
122
|
+
return prv(nextTactics, prune(state, nbrs, nxtVars, nextChoices), rest);
|
|
123
|
+
// Faster to kill all the alternatives.
|
|
124
|
+
if (lim < 0) {
|
|
125
|
+
clearTo(state, ntrl);
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
return prv(nextTactics, nextChoices, newBr(vars, lim, rule.prems));
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (!isBacktrackTo(error, token)) throw error;
|
|
131
|
+
// Backtracks at this level, resetting variables and trying another rule, or to the
|
|
132
|
+
// previous level.
|
|
133
|
+
if (!isUpdated) return backtrack(choices);
|
|
134
|
+
clearTo(state, ntrl);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return undefined;
|
|
138
|
+
};
|
|
139
|
+
// Tries to close the branch by unifying the formula with a literal. Undefined is CLOSEF.
|
|
140
|
+
const closeF = (literals: readonly BlastTerm[]): Seq<Thm> | undefined => {
|
|
141
|
+
for (const literal of literals) {
|
|
142
|
+
const tactic = tryClose(state, formula, literal);
|
|
143
|
+
if (tactic === undefined) continue;
|
|
144
|
+
const nextChoices = prune(state, nbrs, nxtVars, cons({ ntrl, nbrs, token }, choices));
|
|
145
|
+
try {
|
|
146
|
+
return prv(cons(tactic, tactics), nextChoices, rest);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (!isBacktrackTo(error, token)) throw error;
|
|
149
|
+
clearTo(state, ntrl);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return undefined;
|
|
153
|
+
};
|
|
154
|
+
// Tries to unify a queued formula, safe or unsafe, with the formula.
|
|
155
|
+
const closeFl = (levels: readonly Level[]): Seq<Thm> | undefined => {
|
|
156
|
+
for (const level of levels) {
|
|
157
|
+
const closed =
|
|
158
|
+
closeF(level.safe.map((queuedFormula) => queuedFormula.term)) ??
|
|
159
|
+
closeF(level.unsafe.map((queuedFormula) => queuedFormula.term));
|
|
160
|
+
if (closed !== undefined) return closed;
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
};
|
|
164
|
+
if (branch.lim < 0) return backtrack(choices);
|
|
165
|
+
const substituted = equalSubst(state, formula, { ...branch, pairs });
|
|
166
|
+
if (substituted !== undefined)
|
|
167
|
+
return {
|
|
168
|
+
kind: "next",
|
|
169
|
+
tactics: cons(context.data.hypSubstTac(context.limits), tactics),
|
|
170
|
+
choices,
|
|
171
|
+
brs: [substituted, ...rest],
|
|
172
|
+
};
|
|
173
|
+
const closed = closeF(branch.lits) ?? closeFl(pairs) ?? deeper();
|
|
174
|
+
if (closed !== undefined) return done(closed);
|
|
175
|
+
if (netMkRules(state, formula, branch.vars, unsafeList).length === 0)
|
|
176
|
+
// There are no plausible unsafe rules: the formula becomes a literal.
|
|
177
|
+
return {
|
|
178
|
+
kind: "next",
|
|
179
|
+
tactics,
|
|
180
|
+
choices,
|
|
181
|
+
brs: [
|
|
182
|
+
{ pairs, lits: addLit(state, formula, branch.lits), vars: branch.vars, lim: branch.lim },
|
|
183
|
+
...rest,
|
|
184
|
+
],
|
|
185
|
+
};
|
|
186
|
+
// The formula admits some unsafe rules: try them later.
|
|
187
|
+
const deferred: Formula = { term: negOfGoal(state, formula), md };
|
|
188
|
+
return {
|
|
189
|
+
kind: "next",
|
|
190
|
+
tactics: isGoal(formula) ? cons(negOfGoalTac(state), tactics) : tactics,
|
|
191
|
+
choices,
|
|
192
|
+
brs: [
|
|
193
|
+
{
|
|
194
|
+
pairs: [{ safe: queued.safe, unsafe: [...queued.unsafe, deferred] }, ...pairs.slice(1)],
|
|
195
|
+
lits: branch.lits,
|
|
196
|
+
vars: branch.vars,
|
|
197
|
+
lim: branch.lim,
|
|
198
|
+
},
|
|
199
|
+
...rest,
|
|
200
|
+
],
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
// No safe steps are possible at any level: applies an unsafe rule.
|
|
205
|
+
const unsafeStep = (tactics: Tactics, choices: Choices, brs: readonly Branch[]): Step => {
|
|
206
|
+
const token = {};
|
|
207
|
+
const branch = brs[0];
|
|
208
|
+
const rest = brs.slice(1);
|
|
209
|
+
const queued = branch.pairs[0].unsafe;
|
|
210
|
+
const md = queued[0].md;
|
|
211
|
+
const others = queued.slice(1);
|
|
212
|
+
const formula = norm(queued[0].term);
|
|
213
|
+
const ntrl = state.trail.length;
|
|
214
|
+
const rules = netMkRules(state, formula, branch.vars, unsafeList);
|
|
215
|
+
// The new premises of unsafe rules may not be duplicated.
|
|
216
|
+
const newPrem = (
|
|
217
|
+
vars: readonly VarRef[],
|
|
218
|
+
pattern: BlastTerm,
|
|
219
|
+
isDup: boolean,
|
|
220
|
+
lim: number,
|
|
221
|
+
prem: readonly BlastTerm[],
|
|
222
|
+
): Branch => {
|
|
223
|
+
const safe = prem.map((term) => ({ term, md: false }));
|
|
224
|
+
const unsafe = isDup ? [...others, { term: negOfGoal(state, formula), md }] : others;
|
|
225
|
+
return {
|
|
226
|
+
// A premise recursive in the rule makes no new stack frame: its unsafe premises end up at
|
|
227
|
+
// the back of the queue, preventing the exclusion of others.
|
|
228
|
+
pairs: prem.some((term) => match(state, pattern, term))
|
|
229
|
+
? [{ safe, unsafe }]
|
|
230
|
+
: [
|
|
231
|
+
{ safe, unsafe: [] },
|
|
232
|
+
{ safe: [], unsafe },
|
|
233
|
+
],
|
|
234
|
+
lits: prem.some(isGoal) ? branch.lits.map((lit) => negOfGoal(state, lit)) : branch.lits,
|
|
235
|
+
vars,
|
|
236
|
+
lim,
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
const deeper = (): Seq<Thm> | undefined => {
|
|
240
|
+
for (let position = 0; position < rules.length; position += 1) {
|
|
241
|
+
const rule = rules[position];
|
|
242
|
+
if (!unify(state, addTermVars(rule.major, []), rule.major, formula)) continue;
|
|
243
|
+
const isUpdated = ntrl < state.trail.length;
|
|
244
|
+
const vars = varsInVars(branch.vars);
|
|
245
|
+
const newVars = addPremsVars(rule.prems, vars);
|
|
246
|
+
// Duplicates the formula if md permits.
|
|
247
|
+
const isDup = md;
|
|
248
|
+
// Decrements lim extra if updates occur.
|
|
249
|
+
const lim = isUpdated ? branch.lim - (1 + log4(rules.length)) : branch.lim - 1;
|
|
250
|
+
// Allows backtracking from a rule application if other matching rules exist, if the rule
|
|
251
|
+
// updated variables, or if it introduced no new variables, so that it is not a standard
|
|
252
|
+
// gamma-rule. This emulates fast_tac, which allows all unsafe steps to be undone.
|
|
253
|
+
const mayUndo = position + 1 < rules.length || isUpdated || sameVars(vars, newVars);
|
|
254
|
+
const tactic = rule.tactic(isUpdated, isDup, true);
|
|
255
|
+
// Faster to kill all the alternatives.
|
|
256
|
+
if (lim < 0 && rule.prems.length > 0) {
|
|
257
|
+
clearTo(state, ntrl);
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
return prv(cons(tactic, tactics), cons({ ntrl, nbrs: brs.length, token }, choices), [
|
|
262
|
+
...rule.prems.map((prem) => newPrem(newVars, rule.major, isDup, lim, prem)),
|
|
263
|
+
...rest,
|
|
264
|
+
]);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
if (!isBacktrackTo(error, token)) throw error;
|
|
267
|
+
if (!mayUndo) return backtrack(choices);
|
|
268
|
+
clearTo(state, ntrl);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return undefined;
|
|
272
|
+
};
|
|
273
|
+
if (branch.lim < 1) return backtrack(choices);
|
|
274
|
+
const closed = deeper();
|
|
275
|
+
if (closed !== undefined) return done(closed);
|
|
276
|
+
// If no rule closes the branch, the formula becomes a literal.
|
|
277
|
+
return {
|
|
278
|
+
kind: "next",
|
|
279
|
+
tactics,
|
|
280
|
+
choices,
|
|
281
|
+
brs: [
|
|
282
|
+
{
|
|
283
|
+
pairs: [{ safe: [], unsafe: others }],
|
|
284
|
+
lits: [formula, ...branch.lits],
|
|
285
|
+
vars: branch.vars,
|
|
286
|
+
lim: branch.lim,
|
|
287
|
+
},
|
|
288
|
+
...rest,
|
|
289
|
+
],
|
|
290
|
+
};
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const prv = (
|
|
294
|
+
initialTactics: Tactics,
|
|
295
|
+
initialChoices: Choices,
|
|
296
|
+
branches: readonly Branch[],
|
|
297
|
+
): Seq<Thm> => {
|
|
298
|
+
let tactics = initialTactics;
|
|
299
|
+
let choices = initialChoices;
|
|
300
|
+
let brs = branches;
|
|
301
|
+
for (;;) {
|
|
302
|
+
checkInterrupt(context.limits);
|
|
303
|
+
// All branches closed.
|
|
304
|
+
if (brs.length === 0) return cont(tactics, choices);
|
|
305
|
+
const branch = brs[0];
|
|
306
|
+
const head = branch.pairs[0];
|
|
307
|
+
if (head === undefined) return backtrack(choices);
|
|
308
|
+
const step =
|
|
309
|
+
head.safe.length > 0
|
|
310
|
+
? safeStep(tactics, choices, brs)
|
|
311
|
+
: branch.pairs.length === 1 && head.unsafe.length > 0
|
|
312
|
+
? unsafeStep(tactics, choices, brs)
|
|
313
|
+
: undefined;
|
|
314
|
+
if (step?.kind === "done") return step.result;
|
|
315
|
+
if (step !== undefined) {
|
|
316
|
+
tactics = step.tactics;
|
|
317
|
+
choices = step.choices;
|
|
318
|
+
brs = step.brs;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (branch.pairs.length > 1) {
|
|
322
|
+
// No more safe formulae: transfers the unsafe ones down a level.
|
|
323
|
+
const below = branch.pairs[1];
|
|
324
|
+
brs = [
|
|
325
|
+
{
|
|
326
|
+
pairs: [
|
|
327
|
+
{ safe: below.safe, unsafe: [...head.unsafe, ...below.unsafe] },
|
|
328
|
+
...branch.pairs.slice(2),
|
|
329
|
+
],
|
|
330
|
+
lits: branch.lits,
|
|
331
|
+
vars: branch.vars,
|
|
332
|
+
lim: branch.lim,
|
|
333
|
+
},
|
|
334
|
+
...brs.slice(1),
|
|
335
|
+
];
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
return backtrack(choices);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
return prv(
|
|
343
|
+
undefined,
|
|
344
|
+
cons({ ntrl: state.trail.length, nbrs: initial.length, token: undefined }, undefined),
|
|
345
|
+
initial,
|
|
346
|
+
);
|
|
347
|
+
};
|