tla-ts 0.0.4-dev.7c70fed → 0.0.4-dev.8fefa42
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 +62 -70
- 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/check.ts +1 -0
- package/src/errors.ts +86 -0
- package/src/evaluate.ts +54 -19
- 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 +73 -20
- package/src/successors.ts +24 -5
- package/src/tlc-version.ts +8 -7
- package/src/values.ts +47 -4
- 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,53 @@
|
|
|
1
|
+
// Ported from Isabelle2025's Pure/goal.ML: focusing a tactic on one subgoal. Copyright (c)
|
|
2
|
+
// 1986-2025 University of Cambridge, Technische Universitaet Muenchen, and contributors, see
|
|
3
|
+
// LICENSE.
|
|
4
|
+
|
|
5
|
+
import { TlaIsabelleError } from "../../../errors";
|
|
6
|
+
import { compNoFlatten, protectD, protectI } from "./drule";
|
|
7
|
+
import { emptySeq } from "./seq";
|
|
8
|
+
import { primitive, thenTac, type GoalTactic, type Tactic } from "./tactical";
|
|
9
|
+
import { nprems, permutePrems, type Thm } from "./thm";
|
|
10
|
+
|
|
11
|
+
// A ==> ... ==> C becomes A ==> #(... ==> C), keeping count premises.
|
|
12
|
+
const protect = (count: number, thm: Thm) => compNoFlatten(thm, count, 1, protectI);
|
|
13
|
+
|
|
14
|
+
// A ==> ... ==> #C becomes A ==> ... ==> C.
|
|
15
|
+
const conclude = (thm: Thm) => compNoFlatten(thm, nprems(thm), 1, protectD);
|
|
16
|
+
|
|
17
|
+
const restrict = (index: number, count: number, state: Thm) => {
|
|
18
|
+
if (index < 1 || count < 1 || index + count - 1 > nprems(state))
|
|
19
|
+
throw new TlaIsabelleError("THM", "Goal.restrict");
|
|
20
|
+
return protect(count, permutePrems(0, index - 1, state));
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const unrestrict = (index: number, state: Thm) => permutePrems(0, 1 - index, conclude(state));
|
|
24
|
+
|
|
25
|
+
// SELECT_GOAL: runs the tactic on goal index alone.
|
|
26
|
+
export const selectGoal =
|
|
27
|
+
(tactic: Tactic): GoalTactic =>
|
|
28
|
+
(index) =>
|
|
29
|
+
(state) => {
|
|
30
|
+
if (nprems(state) === 1 && index === 1) return tactic(state);
|
|
31
|
+
return thenTac(
|
|
32
|
+
thenTac(
|
|
33
|
+
primitive((current) => restrict(index, 1, current)),
|
|
34
|
+
tactic,
|
|
35
|
+
),
|
|
36
|
+
primitive((current) => unrestrict(index, current)),
|
|
37
|
+
)(state);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// PREFER_GOAL: runs the tactic with goal index moved to the front.
|
|
41
|
+
export const preferGoal =
|
|
42
|
+
(tactic: Tactic): GoalTactic =>
|
|
43
|
+
(index) =>
|
|
44
|
+
(state) => {
|
|
45
|
+
if (index < 1 || index > nprems(state)) return emptySeq;
|
|
46
|
+
return thenTac(
|
|
47
|
+
thenTac(
|
|
48
|
+
primitive((current) => permutePrems(0, index - 1, current)),
|
|
49
|
+
tactic,
|
|
50
|
+
),
|
|
51
|
+
primitive((current) => permutePrems(0, 1 - index, current)),
|
|
52
|
+
)(state);
|
|
53
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Isabelle interrupts a proof that runs out of time, wherever it is; this port checks for that at
|
|
2
|
+
// the points where Isabelle's tactics and unifier spend their time.
|
|
3
|
+
|
|
4
|
+
import { TlaIsabelleError } from "../../../errors";
|
|
5
|
+
|
|
6
|
+
// When a proof gives up: past the deadline, a time from Date.now(), or once the signal aborts.
|
|
7
|
+
export interface Limits {
|
|
8
|
+
readonly deadline: number;
|
|
9
|
+
readonly signal?: AbortSignal;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// tlapm reports no success for an interrupted proof.
|
|
13
|
+
export const checkInterrupt = (limits: Limits) => {
|
|
14
|
+
if (Date.now() > limits.deadline || limits.signal?.aborted === true)
|
|
15
|
+
throw new TlaIsabelleError("Interrupt", "Interrupt");
|
|
16
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// ML's lists: each shares its tail, so that adding to the front takes constant time and keeps the
|
|
2
|
+
// rest of the list alive only through the new cell.
|
|
3
|
+
|
|
4
|
+
export interface List<Item> {
|
|
5
|
+
readonly head: Item;
|
|
6
|
+
readonly tail: List<Item> | undefined;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const cons = <Item>(head: Item, tail: List<Item> | undefined): List<Item> => ({
|
|
10
|
+
head,
|
|
11
|
+
tail,
|
|
12
|
+
});
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
// Ported from Isabelle2025's Pure/logic.ML, the abstract syntax of the meta logic. Copyright (c)
|
|
2
|
+
// 1986-2025 University of Cambridge, Technische Universitaet Muenchen, and contributors, see
|
|
3
|
+
// LICENSE.
|
|
4
|
+
|
|
5
|
+
import { TlaIsabelleError } from "../../../errors";
|
|
6
|
+
import { makeNameContext, variantList, variantNames } from "./name";
|
|
7
|
+
import {
|
|
8
|
+
aconv,
|
|
9
|
+
addFreeNames,
|
|
10
|
+
fastypeOf,
|
|
11
|
+
funT,
|
|
12
|
+
funTs,
|
|
13
|
+
incrBoundvars,
|
|
14
|
+
isDependent,
|
|
15
|
+
abstractOver,
|
|
16
|
+
termName,
|
|
17
|
+
mapAtyps,
|
|
18
|
+
mkAbs,
|
|
19
|
+
mkApp,
|
|
20
|
+
mkBound,
|
|
21
|
+
mkConst,
|
|
22
|
+
mkFree,
|
|
23
|
+
mkTVar,
|
|
24
|
+
mkVar,
|
|
25
|
+
mayOccur,
|
|
26
|
+
propT,
|
|
27
|
+
substBounds,
|
|
28
|
+
type App,
|
|
29
|
+
type Term,
|
|
30
|
+
type Typ,
|
|
31
|
+
} from "./term";
|
|
32
|
+
|
|
33
|
+
// Logic.occs: whether a subterm of other is aconv to term.
|
|
34
|
+
export const occs = (term: Term, other: Term): boolean =>
|
|
35
|
+
mayOccur(term, other) &&
|
|
36
|
+
(aconv(term, other) ||
|
|
37
|
+
(other.kind === "App"
|
|
38
|
+
? occs(term, other.fun) || occs(term, other.arg)
|
|
39
|
+
: other.kind === "Abs" && occs(term, other.body)));
|
|
40
|
+
|
|
41
|
+
export interface Binder {
|
|
42
|
+
readonly name: string;
|
|
43
|
+
readonly type: Typ;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const allConst = (type: Typ) => mkConst("Pure.all", funT(funT(type, propT), propT));
|
|
47
|
+
|
|
48
|
+
export const isAll = (term: Term) =>
|
|
49
|
+
term.kind === "App" &&
|
|
50
|
+
term.fun.kind === "Const" &&
|
|
51
|
+
term.fun.name === "Pure.all" &&
|
|
52
|
+
term.arg.kind === "Abs";
|
|
53
|
+
|
|
54
|
+
export const listAll = (vars: readonly Binder[], body: Term): Term =>
|
|
55
|
+
vars.reduceRight<Term>(
|
|
56
|
+
(result, variable) =>
|
|
57
|
+
mkApp(allConst(variable.type), mkAbs(variable.name, variable.type, result)),
|
|
58
|
+
body,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// Logic.dependent_all_name
|
|
62
|
+
export const dependentAllName = (name: string, variable: Term, body: Term) => {
|
|
63
|
+
const abstracted = abstractOver(variable, body);
|
|
64
|
+
const type = fastypeOf(variable);
|
|
65
|
+
return isDependent(abstracted)
|
|
66
|
+
? mkApp(allConst(type), mkAbs(name === "" ? termName(variable) : name, type, abstracted))
|
|
67
|
+
: body;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const mkEquals = (left: Term, right: Term) => {
|
|
71
|
+
const type = fastypeOf(left);
|
|
72
|
+
return mkApp(mkApp(mkConst("Pure.eq", funTs([type, type], propT)), left), right);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// A term c $ x $ y.
|
|
76
|
+
interface ConstApp2 extends App {
|
|
77
|
+
readonly fun: App;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const isConstApp2 = (term: Term, name: string): term is ConstApp2 =>
|
|
81
|
+
term.kind === "App" &&
|
|
82
|
+
term.fun.kind === "App" &&
|
|
83
|
+
term.fun.fun.kind === "Const" &&
|
|
84
|
+
term.fun.fun.name === name;
|
|
85
|
+
|
|
86
|
+
export const isEquals = (term: Term): term is ConstApp2 => isConstApp2(term, "Pure.eq");
|
|
87
|
+
|
|
88
|
+
export const destEquals = (term: Term): [Term, Term] => {
|
|
89
|
+
if (!isConstApp2(term, "Pure.eq")) throw new TlaIsabelleError("TERM", "dest_equals");
|
|
90
|
+
return [term.fun.arg, term.arg];
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const implies = mkConst("Pure.imp", funTs([propT, propT], propT));
|
|
94
|
+
|
|
95
|
+
export const mkImplies = (premise: Term, conclusion: Term) =>
|
|
96
|
+
mkApp(mkApp(implies, premise), conclusion);
|
|
97
|
+
|
|
98
|
+
export const isImplies = (term: Term): term is ConstApp2 => isConstApp2(term, "Pure.imp");
|
|
99
|
+
|
|
100
|
+
export const destImplies = (term: Term): [Term, Term] => {
|
|
101
|
+
if (!isConstApp2(term, "Pure.imp")) throw new TlaIsabelleError("TERM", "dest_implies");
|
|
102
|
+
return [term.fun.arg, term.arg];
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const listImplies = (premises: readonly Term[], conclusion: Term) =>
|
|
106
|
+
premises.reduceRight((result, premise) => mkImplies(premise, result), conclusion);
|
|
107
|
+
|
|
108
|
+
export const stripImpPrems = (term: Term): Term[] => {
|
|
109
|
+
const premises: Term[] = [];
|
|
110
|
+
let current = term;
|
|
111
|
+
while (isConstApp2(current, "Pure.imp")) {
|
|
112
|
+
premises.push(current.fun.arg);
|
|
113
|
+
current = current.arg;
|
|
114
|
+
}
|
|
115
|
+
return premises;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const stripImpConcl = (term: Term): Term => {
|
|
119
|
+
let current = term;
|
|
120
|
+
while (isConstApp2(current, "Pure.imp")) current = current.arg;
|
|
121
|
+
return current;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// Logic.strip_prems: removes count premises, returning them reversed onto premises.
|
|
125
|
+
export const stripPrems = (
|
|
126
|
+
count: number,
|
|
127
|
+
premises: readonly Term[],
|
|
128
|
+
term: Term,
|
|
129
|
+
): [Term[], Term] => {
|
|
130
|
+
if (count === 0) return [[...premises], term];
|
|
131
|
+
if (!isConstApp2(term, "Pure.imp")) throw new TlaIsabelleError("TERM", "strip_prems");
|
|
132
|
+
return stripPrems(count - 1, [term.fun.arg, ...premises], term.arg);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export const countPrems = (term: Term) => stripImpPrems(term).length;
|
|
136
|
+
|
|
137
|
+
export const nthPrem = (index: number, term: Term): Term => {
|
|
138
|
+
const premise = stripImpPrems(term)[index - 1];
|
|
139
|
+
if (index < 1 || premise === undefined) throw new TlaIsabelleError("TERM", "nth_prem");
|
|
140
|
+
return premise;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export const protectConst = mkConst("Pure.prop", funT(propT, propT));
|
|
144
|
+
|
|
145
|
+
export const protect = (term: Term) => mkApp(protectConst, term);
|
|
146
|
+
|
|
147
|
+
export const isProtected = (term: Term) =>
|
|
148
|
+
term.kind === "App" && term.fun.kind === "Const" && term.fun.name === "Pure.prop";
|
|
149
|
+
|
|
150
|
+
export const unprotect = (term: Term) => {
|
|
151
|
+
if (term.kind !== "App" || term.fun.kind !== "Const" || term.fun.name !== "Pure.prop")
|
|
152
|
+
throw new TlaIsabelleError("TERM", "unprotect");
|
|
153
|
+
return term.arg;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export const mkTerm = (term: Term) =>
|
|
157
|
+
mkApp(mkConst("Pure.term", funT(fastypeOf(term), propT)), term);
|
|
158
|
+
|
|
159
|
+
// Logic.combound: term(Bound (start+count-1), ..., Bound start).
|
|
160
|
+
export const combound = (term: Term, start: number, count: number): Term =>
|
|
161
|
+
count > 0 ? mkApp(combound(term, start + 1, count - 1), mkBound(start)) : term;
|
|
162
|
+
|
|
163
|
+
// Logic.rlist_abs: ([xn, ..., x1], body) to %x1 ... xn. body.
|
|
164
|
+
export const rlistAbs = (binders: readonly Binder[], body: Term) =>
|
|
165
|
+
binders.reduce((result, binder) => mkAbs(binder.name, binder.type, result), body);
|
|
166
|
+
|
|
167
|
+
const incrTVar = (increment: number, type: Typ) =>
|
|
168
|
+
increment === 0
|
|
169
|
+
? type
|
|
170
|
+
: mapAtyps(
|
|
171
|
+
(atom) =>
|
|
172
|
+
atom.kind === "TVar" ? mkTVar(atom.name, atom.index + increment, atom.sort) : atom,
|
|
173
|
+
type,
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
// Logic.incr_indexes_operation: lifts the variables over the types and increments their indexes.
|
|
177
|
+
export const incrIndexesFixed = (
|
|
178
|
+
fixed: readonly string[],
|
|
179
|
+
types: readonly Typ[],
|
|
180
|
+
increment: number,
|
|
181
|
+
level: number,
|
|
182
|
+
term: Term,
|
|
183
|
+
): Term => {
|
|
184
|
+
if (fixed.length === 0 && types.length === 0 && increment === 0) return term;
|
|
185
|
+
const count = types.length;
|
|
186
|
+
const incr = (depth: number, current: Term): Term => {
|
|
187
|
+
switch (current.kind) {
|
|
188
|
+
case "Var":
|
|
189
|
+
return combound(
|
|
190
|
+
mkVar(
|
|
191
|
+
current.name,
|
|
192
|
+
current.index + increment,
|
|
193
|
+
funTs(types, incrTVar(increment, current.type)),
|
|
194
|
+
),
|
|
195
|
+
depth,
|
|
196
|
+
count,
|
|
197
|
+
);
|
|
198
|
+
case "Free":
|
|
199
|
+
return fixed.includes(current.name)
|
|
200
|
+
? combound(
|
|
201
|
+
mkFree(current.name, funTs(types, incrTVar(increment, current.type))),
|
|
202
|
+
depth,
|
|
203
|
+
count,
|
|
204
|
+
)
|
|
205
|
+
: mkFree(current.name, incrTVar(increment, current.type));
|
|
206
|
+
case "Abs":
|
|
207
|
+
return mkAbs(
|
|
208
|
+
current.name,
|
|
209
|
+
incrTVar(increment, current.type),
|
|
210
|
+
incr(depth + 1, current.body),
|
|
211
|
+
);
|
|
212
|
+
case "App":
|
|
213
|
+
return mkApp(incr(depth, current.fun), incr(depth, current.arg));
|
|
214
|
+
case "Const":
|
|
215
|
+
return mkConst(current.name, incrTVar(increment, current.type));
|
|
216
|
+
case "Bound":
|
|
217
|
+
return current;
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
return incr(level, term);
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
export const incrIndexes = (types: readonly Typ[], increment: number, term: Term) =>
|
|
224
|
+
incrIndexesFixed([], types, increment, 0, term);
|
|
225
|
+
|
|
226
|
+
const isAllAbs = (
|
|
227
|
+
term: Term,
|
|
228
|
+
): term is Extract<Term, { kind: "App" }> & {
|
|
229
|
+
arg: Extract<Term, { kind: "Abs" }>;
|
|
230
|
+
} =>
|
|
231
|
+
term.kind === "App" &&
|
|
232
|
+
term.fun.kind === "Const" &&
|
|
233
|
+
term.fun.name === "Pure.all" &&
|
|
234
|
+
term.arg.kind === "Abs";
|
|
235
|
+
|
|
236
|
+
// Logic.lift_abs: lifts a term over the parameters of a goal.
|
|
237
|
+
export const liftAbs = (increment: number, goal: Term, term: Term): Term => {
|
|
238
|
+
const lift = (types: Typ[], current: Term): Term => {
|
|
239
|
+
if (isConstApp2(current, "Pure.imp")) return lift(types, current.arg);
|
|
240
|
+
if (isAllAbs(current))
|
|
241
|
+
return mkAbs(
|
|
242
|
+
current.arg.name,
|
|
243
|
+
current.arg.type,
|
|
244
|
+
lift([current.arg.type, ...types], current.arg.body),
|
|
245
|
+
);
|
|
246
|
+
return incrIndexes([...types].reverse(), increment, term);
|
|
247
|
+
};
|
|
248
|
+
return lift([], goal);
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
// Logic.lift_all: lifts a proposition over the parameters and premises of a goal.
|
|
252
|
+
export const liftAll = (increment: number, goal: Term, term: Term): Term => {
|
|
253
|
+
const lift = (types: Typ[], current: Term): Term => {
|
|
254
|
+
if (isConstApp2(current, "Pure.imp"))
|
|
255
|
+
return mkApp(mkApp(current.fun.fun, current.fun.arg), lift(types, current.arg));
|
|
256
|
+
if (isAllAbs(current))
|
|
257
|
+
return mkApp(
|
|
258
|
+
current.fun,
|
|
259
|
+
mkAbs(
|
|
260
|
+
current.arg.name,
|
|
261
|
+
current.arg.type,
|
|
262
|
+
lift([current.arg.type, ...types], current.arg.body),
|
|
263
|
+
),
|
|
264
|
+
);
|
|
265
|
+
return incrIndexes([...types].reverse(), increment, term);
|
|
266
|
+
};
|
|
267
|
+
return lift([], goal);
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
export const stripAssumsHyp = (term: Term): Term[] => {
|
|
271
|
+
let hyps: Term[] = [];
|
|
272
|
+
let current = term;
|
|
273
|
+
for (;;) {
|
|
274
|
+
if (isConstApp2(current, "Pure.imp")) {
|
|
275
|
+
hyps = [current.fun.arg, ...hyps];
|
|
276
|
+
current = current.arg;
|
|
277
|
+
} else if (isAllAbs(current)) {
|
|
278
|
+
hyps = hyps.map((hyp) => incrBoundvars(1, hyp));
|
|
279
|
+
current = current.arg.body;
|
|
280
|
+
} else return hyps.reverse();
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
export const stripAssumsConcl = (term: Term): Term => {
|
|
285
|
+
let current = term;
|
|
286
|
+
for (;;) {
|
|
287
|
+
if (isConstApp2(current, "Pure.imp")) current = current.arg;
|
|
288
|
+
else if (isAllAbs(current)) current = current.arg.body;
|
|
289
|
+
else return current;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
export const stripParams = (term: Term): Binder[] => {
|
|
294
|
+
const params: Binder[] = [];
|
|
295
|
+
let current = term;
|
|
296
|
+
for (;;) {
|
|
297
|
+
if (isConstApp2(current, "Pure.imp")) current = current.arg;
|
|
298
|
+
else if (isAllAbs(current)) {
|
|
299
|
+
params.push({ name: current.arg.name, type: current.arg.type });
|
|
300
|
+
current = current.arg.body;
|
|
301
|
+
} else return params;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const isMeta = (term: Term) =>
|
|
306
|
+
isConstApp2(term, "Pure.eq") ||
|
|
307
|
+
isConstApp2(term, "Pure.imp") ||
|
|
308
|
+
(term.kind === "App" && term.fun.kind === "Const" && term.fun.name === "Pure.all");
|
|
309
|
+
|
|
310
|
+
export const hasMetaPrems = (term: Term): boolean => {
|
|
311
|
+
if (isConstApp2(term, "Pure.imp")) return isMeta(term.fun.arg) || hasMetaPrems(term.arg);
|
|
312
|
+
if (isAllAbs(term)) return hasMetaPrems(term.arg.body);
|
|
313
|
+
return false;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// Logic.remove_params: drops count parameters and premise number index.
|
|
317
|
+
const removeParams = (count: number, index: number, term: Term): Term => {
|
|
318
|
+
if (count === 0 && index <= 0) return term;
|
|
319
|
+
if (isConstApp2(term, "Pure.imp"))
|
|
320
|
+
return index === 1
|
|
321
|
+
? removeParams(count, index - 1, term.arg)
|
|
322
|
+
: mkImplies(incrBoundvars(count, term.fun.arg), removeParams(count, index - 1, term.arg));
|
|
323
|
+
if (isAllAbs(term)) return removeParams(count - 1, index, term.arg.body);
|
|
324
|
+
if (index > 0) throw new TlaIsabelleError("TERM", "remove_params");
|
|
325
|
+
return term;
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
// Logic.flatten_params: moves all parameters to the front, renaming them apart.
|
|
329
|
+
export const flattenParams = (index: number, term: Term) => {
|
|
330
|
+
const params = stripParams(term);
|
|
331
|
+
const names = variantList(
|
|
332
|
+
[],
|
|
333
|
+
params.map((param) => param.name),
|
|
334
|
+
);
|
|
335
|
+
const vars = params.map((param, position) => ({ name: names[position], type: param.type }));
|
|
336
|
+
return listAll(vars, removeParams(vars.length, index, term));
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
export const listRenameParams = (names: readonly string[], term: Term): Term => {
|
|
340
|
+
if (isConstApp2(term, "Pure.imp"))
|
|
341
|
+
return mkImplies(term.fun.arg, listRenameParams(names, term.arg));
|
|
342
|
+
if (names.length > 0 && isAllAbs(term))
|
|
343
|
+
return mkApp(
|
|
344
|
+
term.fun,
|
|
345
|
+
mkAbs(names[0], term.arg.type, listRenameParams(names.slice(1), term.arg.body)),
|
|
346
|
+
);
|
|
347
|
+
return term;
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
// Logic.strip_assums_imp: strips at most count premises.
|
|
351
|
+
const stripAssumsImp = (count: number, hyps: Term[], term: Term): [Term[], Term] => {
|
|
352
|
+
if (count === 0) return [hyps, term];
|
|
353
|
+
if (isConstApp2(term, "Pure.imp"))
|
|
354
|
+
return stripAssumsImp(count - 1, [term.fun.arg, ...hyps], term.arg);
|
|
355
|
+
return [hyps, term];
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
const stripAssumsAll = (params: Binder[], term: Term): [Binder[], Term] =>
|
|
359
|
+
isAllAbs(term)
|
|
360
|
+
? stripAssumsAll([{ name: term.arg.name, type: term.arg.type }, ...params], term.arg.body)
|
|
361
|
+
: [params, term];
|
|
362
|
+
|
|
363
|
+
// Logic.assum_pairs: the disagreement pairs, one for each assumption of the lifted premise.
|
|
364
|
+
export const assumPairs = (count: number, term: Term): [Term, Term][] => {
|
|
365
|
+
const [params, body] = stripAssumsAll([], term);
|
|
366
|
+
const [hyps, conclusion] = stripAssumsImp(count, [], body);
|
|
367
|
+
const target = rlistAbs(params, conclusion);
|
|
368
|
+
return [...hyps].reverse().map((hyp): [Term, Term] => [rlistAbs(params, hyp), target]);
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
export interface AssumProblems {
|
|
372
|
+
readonly abstract: (term: Term) => Term;
|
|
373
|
+
readonly hyps: Term[];
|
|
374
|
+
readonly conclusion: Term;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export const assumProblems = (count: number, term: Term): AssumProblems => {
|
|
378
|
+
const [params, body] = stripAssumsAll([], term);
|
|
379
|
+
const [hyps, conclusion] = stripAssumsImp(count, [], body);
|
|
380
|
+
return { abstract: (inner) => rlistAbs(params, inner), hyps: [...hyps].reverse(), conclusion };
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
// Logic.goal_params
|
|
384
|
+
export const goalParams = (state: Term, index: number): [Term, Term[]] => {
|
|
385
|
+
const goal = nthPrem(index, state);
|
|
386
|
+
const params = stripParams(goal);
|
|
387
|
+
const names = variantNames(
|
|
388
|
+
params.map((param) => param.name),
|
|
389
|
+
makeNameContext(addFreeNames(goal, [])),
|
|
390
|
+
);
|
|
391
|
+
return [goal, params.map((param, position) => mkFree(names[position], param.type)).reverse()];
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
export const conclOfGoal = (state: Term, index: number) => {
|
|
395
|
+
const [goal, frees] = goalParams(state, index);
|
|
396
|
+
return substBounds(frees, stripAssumsConcl(goal));
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
export const premsOfGoal = (state: Term, index: number) => {
|
|
400
|
+
const [goal, frees] = goalParams(state, index);
|
|
401
|
+
return stripAssumsHyp(goal).map((hyp) => substBounds(frees, hyp));
|
|
402
|
+
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Ported from Isabelle2025's Pure/name.ML and the name bumping of Pure/General/symbol.ML.
|
|
2
|
+
// Copyright (c) 1986-2025 University of Cambridge, Technische Universitaet Muenchen, and
|
|
3
|
+
// contributors, see LICENSE.
|
|
4
|
+
|
|
5
|
+
// Symbol.explode: \<name> symbols count as one symbol.
|
|
6
|
+
export const explodeSymbols = (text: string): string[] =>
|
|
7
|
+
text.match(/\\<\^?[A-Za-z0-9_']*>|[^]/gu) ?? [];
|
|
8
|
+
|
|
9
|
+
const isRawSymbolic = (symbol: string) => symbol.startsWith("\\<") && !symbol.startsWith("\\<^");
|
|
10
|
+
|
|
11
|
+
const isQuasi = (symbol: string) => symbol === "_" || symbol === "'";
|
|
12
|
+
|
|
13
|
+
const isSymbolicEnd = (reversed: readonly string[]): boolean => {
|
|
14
|
+
if (reversed.length === 0) return false;
|
|
15
|
+
if (reversed.length > 1 && reversed[1] === "\\<^sub>") return true;
|
|
16
|
+
if (reversed[0] === "'") return isSymbolicEnd(reversed.slice(1));
|
|
17
|
+
return isRawSymbolic(reversed[0]);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const bumpInit = (text: string) =>
|
|
21
|
+
isSymbolicEnd(explodeSymbols(text).reverse()) ? `${text}'` : `${text}a`;
|
|
22
|
+
|
|
23
|
+
export const bumpString = (text: string) => {
|
|
24
|
+
const symbols = explodeSymbols(text);
|
|
25
|
+
let suffixStart = symbols.length;
|
|
26
|
+
while (suffixStart > 0 && isQuasi(symbols[suffixStart - 1])) suffixStart -= 1;
|
|
27
|
+
const reversed = symbols.slice(0, suffixStart).reverse();
|
|
28
|
+
const bump = (rest: readonly string[]): string[] => {
|
|
29
|
+
if (rest.length === 0) return ["a"];
|
|
30
|
+
if (rest[0] === "z") return ["a", ...bump(rest.slice(1))];
|
|
31
|
+
const code = rest[0].length === 1 ? rest[0].charCodeAt(0) : -1;
|
|
32
|
+
return code >= 97 && code < 122
|
|
33
|
+
? [String.fromCharCode(code + 1), ...rest.slice(1)]
|
|
34
|
+
: ["a", ...rest];
|
|
35
|
+
};
|
|
36
|
+
const bumped = isSymbolicEnd(reversed) ? ["'", ...reversed] : bump(reversed);
|
|
37
|
+
return [...bumped.reverse(), ...symbols.slice(suffixStart)].join("");
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const cleanIndex = (name: string): [string, number] => {
|
|
41
|
+
let base = name;
|
|
42
|
+
let count = 0;
|
|
43
|
+
while (base.endsWith("_")) {
|
|
44
|
+
base = base.slice(0, -1);
|
|
45
|
+
count += 1;
|
|
46
|
+
}
|
|
47
|
+
return [base, count];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const cleanName = (name: string) => cleanIndex(name)[0];
|
|
51
|
+
|
|
52
|
+
// Name.context: the declared names with their latest renaming.
|
|
53
|
+
export type NameContext = ReadonlyMap<string, string | undefined>;
|
|
54
|
+
|
|
55
|
+
export const declareName = (name: string, context: NameContext): NameContext => {
|
|
56
|
+
const clean = cleanName(name);
|
|
57
|
+
if (context.has(clean)) return context;
|
|
58
|
+
return new Map(context).set(clean, undefined);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const emptyNameContext: NameContext = new Map([
|
|
62
|
+
["", undefined],
|
|
63
|
+
["'", undefined],
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
export const makeNameContext = (names: readonly string[]) =>
|
|
67
|
+
names.reduce((context, name) => declareName(name, context), emptyNameContext);
|
|
68
|
+
|
|
69
|
+
// Name.variant: a name distinct from the declared ones, keeping a suffix of underscores.
|
|
70
|
+
export const variantName = (name: string, context: NameContext): [string, NameContext] => {
|
|
71
|
+
const [base, count] = cleanIndex(name);
|
|
72
|
+
if (!context.has(base)) return [base + "_".repeat(count), declareName(base, context)];
|
|
73
|
+
const initial = bumpInit(base);
|
|
74
|
+
let current = initial;
|
|
75
|
+
while (context.has(current)) current = bumpString(context.get(current) ?? current);
|
|
76
|
+
const renamed =
|
|
77
|
+
cleanName(initial) === cleanName(current)
|
|
78
|
+
? context
|
|
79
|
+
: new Map(context).set(cleanName(initial), cleanName(current));
|
|
80
|
+
return [current + "_".repeat(count), declareName(current, renamed)];
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const variantNames = (names: readonly string[], context: NameContext) => {
|
|
84
|
+
let current = context;
|
|
85
|
+
return names.map((name) => {
|
|
86
|
+
const [variant, next] = variantName(name, current);
|
|
87
|
+
current = next;
|
|
88
|
+
return variant;
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const variantList = (used: readonly string[], names: readonly string[]) =>
|
|
93
|
+
variantNames(names, makeNameContext(used));
|
|
94
|
+
|
|
95
|
+
// Name.invent: count fresh names starting from name.
|
|
96
|
+
export const inventNames = (context: NameContext, name: string, count: number) => {
|
|
97
|
+
const names: string[] = [];
|
|
98
|
+
let current = cleanName(name);
|
|
99
|
+
while (names.length < count) {
|
|
100
|
+
if (!context.has(current)) names.push(current);
|
|
101
|
+
current = bumpString(current);
|
|
102
|
+
}
|
|
103
|
+
return names;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// Name.is_bound and Name.bound: the names of loose bound variables in nets and printing.
|
|
107
|
+
// Name.is_bound: the names the simplifier gives the free variables standing for bound ones.
|
|
108
|
+
export const isBoundName = (name: string) => name.startsWith(":");
|
|
109
|
+
|
|
110
|
+
export const boundName = (index: number) => `:${String(index).padStart(3, "0")}`;
|