tla-ts 0.0.4-dev.6dec595 → 0.0.4-dev.702eb98
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 +26 -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 +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/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-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 +97 -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 +68 -25
- package/src/values.ts +8 -3
- 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,1249 @@
|
|
|
1
|
+
// Ported from Zenon's lltoisar.ml and the -oisar path of main.ml, as shipped in tlapm. Copyright
|
|
2
|
+
// 2008 INRIA, see LICENSE.
|
|
3
|
+
//
|
|
4
|
+
// Prints a proof as the Isar text that Zenon writes with -oisar, which tlapm checks with Isabelle.
|
|
5
|
+
// Zenon numbers each formula the first time it prints or names it: z_H<n> names a hypothesis and
|
|
6
|
+
// ?z_h<n> abbreviates a formula that an earlier (is "...") pattern bound.
|
|
7
|
+
|
|
8
|
+
import { TlaZenonError } from "../../errors";
|
|
9
|
+
import { FALSE, GOAL_NAME, TRUE, type BinderExpr, type Expr, type Expressions } from "./expr";
|
|
10
|
+
import type { Relations } from "./eqrel";
|
|
11
|
+
import { printCaseLemma, printRecordLemma } from "./isar-case";
|
|
12
|
+
import { getLimitFailure, type Limits } from "./limits";
|
|
13
|
+
import { optimise, type Lemma, type LlProof } from "./llproof";
|
|
14
|
+
import { isMetaName, translateProof, type MlProof } from "./mltoll";
|
|
15
|
+
import type { Phrase } from "./parse";
|
|
16
|
+
import { postprocessTla } from "./tla/llproof";
|
|
17
|
+
|
|
18
|
+
const INFIXES: Record<string, string> = {
|
|
19
|
+
"=": "=",
|
|
20
|
+
"TLA.cup": " \\\\cup ",
|
|
21
|
+
"TLA.cap": " \\\\cap ",
|
|
22
|
+
"TLA.setminus": " \\\\ ",
|
|
23
|
+
"TLA.in": " \\\\in ",
|
|
24
|
+
"TLA.subseteq": " \\\\subseteq ",
|
|
25
|
+
"arith.natrange": "..",
|
|
26
|
+
"arith.add": " + ",
|
|
27
|
+
"arith.sub": " - ",
|
|
28
|
+
"arith.mul": " * ",
|
|
29
|
+
"arith.le": " <= ",
|
|
30
|
+
"arith.lt": " < ",
|
|
31
|
+
"TLA.concat": " \\\\circ ",
|
|
32
|
+
"TLA.oneArg": " :> ",
|
|
33
|
+
"TLA.extend": " @@ ",
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const CONSTANTS: Record<string, string> = {
|
|
37
|
+
"TLA.emptyset": "{}",
|
|
38
|
+
"arith.N": "Nat",
|
|
39
|
+
"arith.Z": "Int",
|
|
40
|
+
"arith.R": "isReal",
|
|
41
|
+
"arith.Infinity": "isInfinity",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const PREFIXES: Record<string, string> = {
|
|
45
|
+
"arith.div": "isa'slash",
|
|
46
|
+
"arith.euclidiv": "isa'div",
|
|
47
|
+
"arith.mod": "isa'pc",
|
|
48
|
+
"arith.minus": " -.",
|
|
49
|
+
"arith.intrange": "isa'dotdot",
|
|
50
|
+
"TLA.box": "isa'box",
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Zenon drops the namespace of the symbols it doesn't rename.
|
|
54
|
+
const stripNamespace = (symbol: string) =>
|
|
55
|
+
symbol.length > 4 && symbol.startsWith("TLA.")
|
|
56
|
+
? symbol.slice(4)
|
|
57
|
+
: symbol.length > 6 && symbol.startsWith("arith.")
|
|
58
|
+
? symbol.slice(6)
|
|
59
|
+
: symbol;
|
|
60
|
+
|
|
61
|
+
// Zenon's Misc.base26.
|
|
62
|
+
const toBase26 = (number: number): string =>
|
|
63
|
+
number < 26
|
|
64
|
+
? String.fromCharCode(97 + number)
|
|
65
|
+
: `${toBase26(Math.floor(number / 26))}${String.fromCharCode(97 + (number % 26))}`;
|
|
66
|
+
|
|
67
|
+
const getPairs = <Item>(items: Item[]): [Item, Item][] =>
|
|
68
|
+
Array.from({ length: Math.floor(items.length / 2) }, (_, position) => [
|
|
69
|
+
items[2 * position],
|
|
70
|
+
items[2 * position + 1],
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
// The numeral that Zenon prints for 0 and its successors, up to 15.
|
|
74
|
+
const getNumeral = (expression: Expr, limit: number): number | undefined => {
|
|
75
|
+
if (limit < 0) return undefined;
|
|
76
|
+
if (expression.kind === "var" && expression.symbol === "0") return 0;
|
|
77
|
+
if (
|
|
78
|
+
expression.kind === "app" &&
|
|
79
|
+
expression.symbol === "TLA.fapply" &&
|
|
80
|
+
expression.args.length === 2 &&
|
|
81
|
+
expression.args[0].kind === "var" &&
|
|
82
|
+
expression.args[0].symbol === "TLA.Succ"
|
|
83
|
+
) {
|
|
84
|
+
const previous = getNumeral(expression.args[1], limit - 1);
|
|
85
|
+
return previous === undefined ? undefined : previous + 1;
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const getSingle = (proof: LlProof) => {
|
|
91
|
+
if (proof.subproofs.length !== 1) throw new TlaZenonError("Zenon: expected one subproof");
|
|
92
|
+
return proof.subproofs[0];
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const getLastBranchIsOther = (branches: Expr[][]) => {
|
|
96
|
+
const last = branches[branches.length - 1];
|
|
97
|
+
if (last === undefined || (last.length !== 1 && last.length !== 2))
|
|
98
|
+
throw new TlaZenonError("Zenon: a CASE rule with a malformed last branch");
|
|
99
|
+
return last.length === 2;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Zenon's iinc: the indentation stops growing at 15.
|
|
103
|
+
const increment = (indent: number) => (indent >= 15 ? indent : indent + 1);
|
|
104
|
+
|
|
105
|
+
// What the search knew when it found a proof: the .znn file's phrases, the proofs that it reused
|
|
106
|
+
// and the properties of relations that the hypotheses stated. The printer stops at the search's
|
|
107
|
+
// limits.
|
|
108
|
+
export interface IsarContext {
|
|
109
|
+
exprs: Expressions;
|
|
110
|
+
phrases: Phrase[];
|
|
111
|
+
sharedProofs: ReadonlySet<MlProof>;
|
|
112
|
+
relations: Relations;
|
|
113
|
+
limits: Limits;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// The Isar text that Zenon prints with -oisar for a proof that the search found, or undefined when
|
|
117
|
+
// the limits stop the printer first.
|
|
118
|
+
export const printIsar = (proof: MlProof, context: IsarContext): string | undefined => {
|
|
119
|
+
const exprs = context.exprs;
|
|
120
|
+
const numbers = new Map<Expr, number>();
|
|
121
|
+
// Zenon's Index.get_number.
|
|
122
|
+
const getNumber = (expression: Expr) => {
|
|
123
|
+
const existing = numbers.get(expression);
|
|
124
|
+
if (existing !== undefined) return existing;
|
|
125
|
+
numbers.set(expression, numbers.size);
|
|
126
|
+
return numbers.size - 1;
|
|
127
|
+
};
|
|
128
|
+
const lemmas = optimise(
|
|
129
|
+
exprs,
|
|
130
|
+
postprocessTla(
|
|
131
|
+
exprs,
|
|
132
|
+
translateProof(
|
|
133
|
+
{ exprs, getNumber, sharedProofs: context.sharedProofs, relations: context.relations },
|
|
134
|
+
context.phrases,
|
|
135
|
+
proof,
|
|
136
|
+
),
|
|
137
|
+
),
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
const output: string[] = [];
|
|
141
|
+
const write = (text: string) => output.push(text);
|
|
142
|
+
const line = (indent: number, text: string) => output.push(" ".repeat(indent), text);
|
|
143
|
+
const hname = (expression: Expr) => `z_H${toBase26(getNumber(expression))}`;
|
|
144
|
+
const vname = (expression: Expr) => `?z_h${toBase26(getNumber(expression))}`;
|
|
145
|
+
const equal = (left: Expr, right: Expr) => exprs.eapp("=", [left, right]);
|
|
146
|
+
const toLambda = (binder: Expr) => {
|
|
147
|
+
if (binder.kind !== "all" && binder.kind !== "ex")
|
|
148
|
+
throw new TlaZenonError("Zenon: expected a quantifier");
|
|
149
|
+
return exprs.elam(binder.args[0], binder.symbol, binder.args[1]);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
// The formulas that (is "...") patterns bound, as a persistent set: a version is the length of
|
|
153
|
+
// the log, and a version only reads the log's prefix, which later additions from it don't change.
|
|
154
|
+
const dictLog: number[] = [];
|
|
155
|
+
const dictPositions = new Map<number, number>();
|
|
156
|
+
const isBound = (dict: number, number: number) => {
|
|
157
|
+
const position = dictPositions.get(number);
|
|
158
|
+
return position !== undefined && position < dict && dictLog[position] === number;
|
|
159
|
+
};
|
|
160
|
+
const bind = (dict: number, number: number) => {
|
|
161
|
+
dictLog.length = dict;
|
|
162
|
+
dictPositions.set(number, dict);
|
|
163
|
+
dictLog.push(number);
|
|
164
|
+
return dict + 1;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// Zenon's p_expr: the bound variables are those that a pattern can't abbreviate.
|
|
168
|
+
const printExpr = (env: string[], dict: number, expression: Expr): string => {
|
|
169
|
+
const number = getNumber(expression);
|
|
170
|
+
if (isBound(dict, number) && !expression.freeVars.some((name) => env.includes(name)))
|
|
171
|
+
return `?z_h${toBase26(number)}`;
|
|
172
|
+
const print = (operand: Expr) => printExpr(env, dict, operand);
|
|
173
|
+
const printList = (operands: Expr[]) => operands.map(print).join(", ");
|
|
174
|
+
const printBinder = (opening: string, binder: BinderExpr, separator: string) =>
|
|
175
|
+
`(${opening}${binder.args[0].symbol}${separator}${printExpr([binder.args[0].symbol, ...env], dict, binder.args[1])})`;
|
|
176
|
+
switch (expression.kind) {
|
|
177
|
+
case "var":
|
|
178
|
+
return isMetaName(expression.symbol)
|
|
179
|
+
? "(CHOOSE x : TRUE)"
|
|
180
|
+
: (CONSTANTS[expression.symbol] ?? stripNamespace(expression.symbol));
|
|
181
|
+
case "app": {
|
|
182
|
+
const args = expression.args;
|
|
183
|
+
switch (expression.symbol) {
|
|
184
|
+
case "$string": {
|
|
185
|
+
const text = args[0];
|
|
186
|
+
if (args.length !== 1 || text.kind !== "var" || text.symbol.length < 2)
|
|
187
|
+
throw new TlaZenonError("Zenon: a malformed string");
|
|
188
|
+
return `''${text.symbol.slice(1, -1)}''`;
|
|
189
|
+
}
|
|
190
|
+
case "TLA.set":
|
|
191
|
+
return `{${printList(args)}}`;
|
|
192
|
+
case "TLA.tuple":
|
|
193
|
+
return `<<${printList(args)}>>`;
|
|
194
|
+
case "TLA.record":
|
|
195
|
+
return `(${getPairs(args)
|
|
196
|
+
.map((pair) => `${print(pair[0])} :> (${print(pair[1])})`)
|
|
197
|
+
.join(" @@ ")})`;
|
|
198
|
+
case "TLA.recordset":
|
|
199
|
+
return `[${getPairs(args)
|
|
200
|
+
.map((pair) => `${print(pair[0])} : (${print(pair[1])})`)
|
|
201
|
+
.join(", ")}]`;
|
|
202
|
+
case "TLA.CASE":
|
|
203
|
+
return `(CASE ${printCaseArms(env, dict, args)})`;
|
|
204
|
+
}
|
|
205
|
+
if (expression.symbol === "TLA.fapply" && args.length === 2) {
|
|
206
|
+
const numeral =
|
|
207
|
+
args[0].kind === "var" && args[0].symbol === "TLA.Succ"
|
|
208
|
+
? getNumeral(args[1], 14)
|
|
209
|
+
: undefined;
|
|
210
|
+
if (numeral !== undefined) return String(numeral + 1);
|
|
211
|
+
return `(${print(args[0])}[${print(args[1])}])`;
|
|
212
|
+
}
|
|
213
|
+
const infix = INFIXES[expression.symbol];
|
|
214
|
+
if (infix !== undefined && args.length === 2)
|
|
215
|
+
return `(${print(args[0])}${infix}${print(args[1])})`;
|
|
216
|
+
if (args.length === 0) return expression.symbol;
|
|
217
|
+
return `${PREFIXES[expression.symbol] ?? stripNamespace(expression.symbol)}(${printList(args)})`;
|
|
218
|
+
}
|
|
219
|
+
case "not": {
|
|
220
|
+
const operand = expression.args[0];
|
|
221
|
+
return operand.kind === "app" && operand.symbol === "=" && operand.args.length === 2
|
|
222
|
+
? `(${print(operand.args[0])}~=${print(operand.args[1])})`
|
|
223
|
+
: `(~${print(operand)})`;
|
|
224
|
+
}
|
|
225
|
+
case "and":
|
|
226
|
+
return `(${print(expression.args[0])}&${print(expression.args[1])})`;
|
|
227
|
+
case "or":
|
|
228
|
+
return `(${print(expression.args[0])}|${print(expression.args[1])})`;
|
|
229
|
+
case "imply":
|
|
230
|
+
return `(${print(expression.args[0])}=>${print(expression.args[1])})`;
|
|
231
|
+
case "equiv":
|
|
232
|
+
return `(${print(expression.args[0])}<=>${print(expression.args[1])})`;
|
|
233
|
+
case "true":
|
|
234
|
+
return "TRUE";
|
|
235
|
+
case "false":
|
|
236
|
+
return "FALSE";
|
|
237
|
+
case "all":
|
|
238
|
+
return printBinder("\\\\A ", expression, ":");
|
|
239
|
+
case "ex":
|
|
240
|
+
return printBinder("\\\\E ", expression, ":");
|
|
241
|
+
case "lam":
|
|
242
|
+
return printBinder("\\<lambda>", expression, ". ");
|
|
243
|
+
case "tau":
|
|
244
|
+
return printBinder("CHOOSE ", expression, ":");
|
|
245
|
+
case "meta":
|
|
246
|
+
throw new TlaZenonError("Zenon: a metavariable in the Isar output");
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
const printCaseArms = (env: string[], dict: number, arms: Expr[]): string => {
|
|
251
|
+
const print = (operand: Expr) => printExpr(env, dict, operand);
|
|
252
|
+
if (arms.length === 0) throw new TlaZenonError("Zenon: a CASE without arms");
|
|
253
|
+
if (arms.length === 1) return `OTHER -> ${print(arms[0])}`;
|
|
254
|
+
const head = `${print(arms[0])} -> ${print(arms[1])}`;
|
|
255
|
+
return arms.length === 2 ? head : `${head} [] ${printCaseArms(env, dict, arms.slice(2))}`;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const expr = (dict: number, expression: Expr) => printExpr([], dict, expression);
|
|
259
|
+
|
|
260
|
+
// Zenon's p_apply: the instance of a lambda, through the lambda's abbreviation if it has one.
|
|
261
|
+
const printApply = (dict: number, lambda: Expr, argument: Expr) => {
|
|
262
|
+
const number = getNumber(lambda);
|
|
263
|
+
return isBound(dict, number)
|
|
264
|
+
? `?z_h${toBase26(number)}(${expr(dict, argument)})`
|
|
265
|
+
: expr(dict, exprs.apply(lambda, argument));
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// Zenon's mk_pat: a pattern variable for a formula that has none yet.
|
|
269
|
+
const makePattern = (dict: number, expression: Expr): [string, number] => {
|
|
270
|
+
if (expression.kind === "var" && !isMetaName(expression.symbol)) return ["_", dict];
|
|
271
|
+
const number = getNumber(expression);
|
|
272
|
+
return isBound(dict, number) ? ["_", dict] : [vname(expression), bind(dict, number)];
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// Zenon's p_is: binds the formula's parts to pattern variables.
|
|
276
|
+
const printIs = (dict: number, formula: Expr): number => {
|
|
277
|
+
const binary = (prefix: string, left: Expr, operator: string, right: Expr, suffix: string) => {
|
|
278
|
+
const first = makePattern(dict, left);
|
|
279
|
+
const second = makePattern(first[1], right);
|
|
280
|
+
write(
|
|
281
|
+
first[0] === "_" && second[0] === "_"
|
|
282
|
+
? "\n"
|
|
283
|
+
: ` (is "${prefix}${first[0]}${operator}${second[0]}${suffix}")\n`,
|
|
284
|
+
);
|
|
285
|
+
return second[1];
|
|
286
|
+
};
|
|
287
|
+
const unary = (prefix: string, operand: Expr, suffix: string) => {
|
|
288
|
+
const pattern = makePattern(dict, operand);
|
|
289
|
+
write(pattern[0] === "_" ? "\n" : ` (is "${prefix}${pattern[0]}${suffix}")\n`);
|
|
290
|
+
return pattern[1];
|
|
291
|
+
};
|
|
292
|
+
const operand = formula.kind === "not" ? formula.args[0] : undefined;
|
|
293
|
+
switch (formula.kind) {
|
|
294
|
+
case "and":
|
|
295
|
+
return binary("", formula.args[0], "&", formula.args[1], "");
|
|
296
|
+
case "or":
|
|
297
|
+
return binary("", formula.args[0], "|", formula.args[1], "");
|
|
298
|
+
case "imply":
|
|
299
|
+
return binary("", formula.args[0], "=>", formula.args[1], "");
|
|
300
|
+
case "equiv":
|
|
301
|
+
return binary("", formula.args[0], "<=>", formula.args[1], "");
|
|
302
|
+
case "all":
|
|
303
|
+
return unary("\\\\A x : ", toLambda(formula), "(x)");
|
|
304
|
+
case "ex":
|
|
305
|
+
return unary("\\\\E x : ", toLambda(formula), "(x)");
|
|
306
|
+
}
|
|
307
|
+
if (formula.kind === "app" && formula.symbol === "=" && formula.args.length === 2)
|
|
308
|
+
return binary("", formula.args[0], "=", formula.args[1], "");
|
|
309
|
+
if (operand === undefined) return unary("", formula, "");
|
|
310
|
+
switch (operand.kind) {
|
|
311
|
+
case "and":
|
|
312
|
+
return binary("~(", operand.args[0], "&", operand.args[1], ")");
|
|
313
|
+
case "or":
|
|
314
|
+
return binary("~(", operand.args[0], "|", operand.args[1], ")");
|
|
315
|
+
case "imply":
|
|
316
|
+
return binary("~(", operand.args[0], "=>", operand.args[1], ")");
|
|
317
|
+
case "equiv":
|
|
318
|
+
return binary("~(", operand.args[0], "<=>", operand.args[1], ")");
|
|
319
|
+
case "all":
|
|
320
|
+
return unary("~(\\\\A x : ", toLambda(operand), "(x))");
|
|
321
|
+
case "ex":
|
|
322
|
+
return unary("~(\\\\E x : ", toLambda(operand), "(x))");
|
|
323
|
+
case "not":
|
|
324
|
+
return unary("~~", operand.args[0], "");
|
|
325
|
+
}
|
|
326
|
+
if (operand.kind === "app" && operand.symbol === "=" && operand.args.length === 2)
|
|
327
|
+
return binary("", operand.args[0], "~=", operand.args[1], "");
|
|
328
|
+
return unary("~", operand, "");
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
const printLet = (dict: number, indent: number, expression: Expr) => {
|
|
332
|
+
const pattern = makePattern(dict, expression);
|
|
333
|
+
if (pattern[0] !== "_") line(indent, `let ${pattern[0]} = "${expr(dict, expression)}"\n`);
|
|
334
|
+
return pattern[1];
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
const printSequent = (indent: number, dict: number, formulas: Expr[]) =>
|
|
338
|
+
formulas.reduce((current, formula) => {
|
|
339
|
+
line(indent, `assume ${hname(formula)}:"${expr(current, formula)}"`);
|
|
340
|
+
return printIs(current, formula);
|
|
341
|
+
}, dict);
|
|
342
|
+
|
|
343
|
+
// Zenon's p_sub: each branch assumes its formulas and proves FALSE.
|
|
344
|
+
const printBranches = (
|
|
345
|
+
indent: number,
|
|
346
|
+
dict: number,
|
|
347
|
+
branches: Expr[][],
|
|
348
|
+
subproofs: LlProof[],
|
|
349
|
+
) => {
|
|
350
|
+
if (branches.length !== subproofs.length)
|
|
351
|
+
throw new TlaZenonError("Zenon: a rule whose branches don't match its subproofs");
|
|
352
|
+
branches.forEach((branch, position) => {
|
|
353
|
+
printTree(
|
|
354
|
+
increment(indent),
|
|
355
|
+
printSequent(increment(indent), dict, branch),
|
|
356
|
+
subproofs[position],
|
|
357
|
+
);
|
|
358
|
+
line(indent, position === branches.length - 1 ? "qed\n" : "next\n");
|
|
359
|
+
});
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
// A have for each formula of the branch that the subproof uses, then the subproof.
|
|
363
|
+
const printHaves = (
|
|
364
|
+
indent: number,
|
|
365
|
+
dict: number,
|
|
366
|
+
formulas: Expr[],
|
|
367
|
+
subproof: LlProof,
|
|
368
|
+
getJustification: (dict: number, position: number) => string,
|
|
369
|
+
) => {
|
|
370
|
+
const final = formulas.reduce((current, formula, position) => {
|
|
371
|
+
if (!subproof.conclusions.includes(formula)) return current;
|
|
372
|
+
line(indent, `have ${hname(formula)}: "${expr(current, formula)}"`);
|
|
373
|
+
const next = printIs(current, formula);
|
|
374
|
+
line(indent, getJustification(next, position));
|
|
375
|
+
return next;
|
|
376
|
+
}, dict);
|
|
377
|
+
printTree(indent, final, subproof);
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const printAlpha = (
|
|
381
|
+
indent: number,
|
|
382
|
+
dict: number,
|
|
383
|
+
lemma: string,
|
|
384
|
+
conclusion: Expr,
|
|
385
|
+
formulas: Expr[],
|
|
386
|
+
proof: LlProof,
|
|
387
|
+
) => {
|
|
388
|
+
const subproof = getSingle(proof);
|
|
389
|
+
const conclusionName = hname(conclusion);
|
|
390
|
+
printHaves(
|
|
391
|
+
indent,
|
|
392
|
+
dict,
|
|
393
|
+
formulas,
|
|
394
|
+
subproof,
|
|
395
|
+
(_, position) => `by (rule zenon_${lemma}_${position} [OF ${conclusionName}])\n`,
|
|
396
|
+
);
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const printBeta = (
|
|
400
|
+
indent: number,
|
|
401
|
+
dict: number,
|
|
402
|
+
lemma: string,
|
|
403
|
+
conclusion: Expr,
|
|
404
|
+
branches: Expr[][],
|
|
405
|
+
proof: LlProof,
|
|
406
|
+
) => {
|
|
407
|
+
const conclusionName = hname(conclusion);
|
|
408
|
+
line(indent, "show FALSE\n");
|
|
409
|
+
line(indent, `proof (rule zenon_${lemma} [OF ${conclusionName}])\n`);
|
|
410
|
+
printBranches(indent, dict, branches, proof.subproofs);
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
// Zenon's p_gamma and p_delta: the instance of a quantified formula.
|
|
414
|
+
const printInstance = (
|
|
415
|
+
indent: number,
|
|
416
|
+
dict: number,
|
|
417
|
+
lemma: string,
|
|
418
|
+
isNegated: boolean,
|
|
419
|
+
quantified: Expr,
|
|
420
|
+
term: Expr,
|
|
421
|
+
conclusion: Expr,
|
|
422
|
+
proof: LlProof,
|
|
423
|
+
isGamma: boolean,
|
|
424
|
+
) => {
|
|
425
|
+
const subproof = getSingle(proof);
|
|
426
|
+
const lambda = toLambda(quantified);
|
|
427
|
+
const instance = exprs.apply(lambda, term);
|
|
428
|
+
const body = isNegated ? exprs.enot(instance) : instance;
|
|
429
|
+
line(indent, `have ${hname(body)}: "${isNegated ? "~" : ""}${printApply(dict, lambda, term)}"`);
|
|
430
|
+
const next = printIs(dict, body);
|
|
431
|
+
const conclusionName = hname(conclusion);
|
|
432
|
+
const terms = isGamma
|
|
433
|
+
? `"${expr(next, lambda)}" "${expr(next, term)}"`
|
|
434
|
+
: `"${expr(next, lambda)}"`;
|
|
435
|
+
line(indent, `by (rule zenon_${lemma}_0 [of ${terms}, OF ${conclusionName}])\n`);
|
|
436
|
+
printTree(indent, next, subproof);
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
// Zenon's p_sub_equal: proves an argument equal to the other, unless the subproof needn't.
|
|
440
|
+
const printSubEqual = (
|
|
441
|
+
indent: number,
|
|
442
|
+
dict: number,
|
|
443
|
+
left: Expr,
|
|
444
|
+
right: Expr,
|
|
445
|
+
subproof: LlProof,
|
|
446
|
+
) => {
|
|
447
|
+
const equality = equal(left, right);
|
|
448
|
+
if (left === right || subproof.conclusions.includes(equality)) return dict;
|
|
449
|
+
const negation = exprs.enot(equality);
|
|
450
|
+
line(indent, `have ${hname(equality)}: "${expr(dict, equality)}"`);
|
|
451
|
+
const next = printIs(dict, equality);
|
|
452
|
+
const reversed = equal(right, left);
|
|
453
|
+
if (subproof.conclusions.includes(reversed)) {
|
|
454
|
+
line(indent, `by (rule sym [OF ${hname(reversed)}])\n`);
|
|
455
|
+
return next;
|
|
456
|
+
}
|
|
457
|
+
line(indent, `proof (rule zenon_nnpp [of "${expr(next, equality)}"])\n`);
|
|
458
|
+
printTree(increment(indent), printSequent(increment(indent), next, [negation]), subproof);
|
|
459
|
+
line(indent, "qed\n");
|
|
460
|
+
return next;
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
// Zenon's p_subst: rewrites the arguments that differ one at a time.
|
|
464
|
+
const printSubstitution = (
|
|
465
|
+
indent: number,
|
|
466
|
+
dict: number,
|
|
467
|
+
make: (args: Expr[]) => Expr,
|
|
468
|
+
lefts: Expr[],
|
|
469
|
+
rights: Expr[],
|
|
470
|
+
done: Expr[],
|
|
471
|
+
previous: Expr,
|
|
472
|
+
): void => {
|
|
473
|
+
if (lefts.length !== rights.length)
|
|
474
|
+
throw new TlaZenonError("Zenon: arguments of different lengths");
|
|
475
|
+
if (lefts.length === 0) {
|
|
476
|
+
line(increment(indent), `thus "${expr(dict, previous)}" .\n`);
|
|
477
|
+
line(indent, "qed\n");
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const left = lefts[0];
|
|
481
|
+
const right = rights[0];
|
|
482
|
+
if (left === right) {
|
|
483
|
+
printSubstitution(
|
|
484
|
+
indent,
|
|
485
|
+
dict,
|
|
486
|
+
make,
|
|
487
|
+
lefts.slice(1),
|
|
488
|
+
rights.slice(1),
|
|
489
|
+
[...done, right],
|
|
490
|
+
previous,
|
|
491
|
+
);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
const variable = exprs.newVar();
|
|
495
|
+
const context = exprs.elam(variable, "", make([...done, variable, ...lefts.slice(1)]));
|
|
496
|
+
const instance = exprs.apply(context, right);
|
|
497
|
+
line(increment(indent), `have ${hname(instance)}: "${expr(dict, instance)}"`);
|
|
498
|
+
const next = printIs(dict, instance);
|
|
499
|
+
const previousName = hname(previous);
|
|
500
|
+
const equalityName = hname(equal(left, right));
|
|
501
|
+
line(
|
|
502
|
+
increment(indent),
|
|
503
|
+
`by (rule subst [where P="${expr(next, context)}", OF ${equalityName}], fact ${previousName})\n`,
|
|
504
|
+
);
|
|
505
|
+
printSubstitution(
|
|
506
|
+
indent,
|
|
507
|
+
next,
|
|
508
|
+
make,
|
|
509
|
+
lefts.slice(1),
|
|
510
|
+
rights.slice(1),
|
|
511
|
+
[...done, right],
|
|
512
|
+
instance,
|
|
513
|
+
);
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
const printArgs = (dict: number, args: Expr[]) =>
|
|
517
|
+
args.map((arg) => `"${expr(dict, arg)}"`).join(" ");
|
|
518
|
+
const printNames = (formulas: Expr[]) => formulas.map(hname).join(" ");
|
|
519
|
+
|
|
520
|
+
const printTree = (indent: number, dict: number, proof: LlProof): void => {
|
|
521
|
+
if (getLimitFailure(context.limits) !== undefined) return;
|
|
522
|
+
const rule = proof.rule;
|
|
523
|
+
switch (rule.kind) {
|
|
524
|
+
case "connect": {
|
|
525
|
+
const left = rule.left;
|
|
526
|
+
const right = rule.right;
|
|
527
|
+
switch (rule.connective) {
|
|
528
|
+
case "and":
|
|
529
|
+
return printAlpha(indent, dict, "and", exprs.eand(left, right), [left, right], proof);
|
|
530
|
+
case "or":
|
|
531
|
+
return printBeta(indent, dict, "or", exprs.eor(left, right), [[left], [right]], proof);
|
|
532
|
+
case "imply":
|
|
533
|
+
return printBeta(
|
|
534
|
+
indent,
|
|
535
|
+
dict,
|
|
536
|
+
"imply",
|
|
537
|
+
exprs.eimply(left, right),
|
|
538
|
+
[[exprs.enot(left)], [right]],
|
|
539
|
+
proof,
|
|
540
|
+
);
|
|
541
|
+
case "equiv":
|
|
542
|
+
return printBeta(
|
|
543
|
+
indent,
|
|
544
|
+
dict,
|
|
545
|
+
"equiv",
|
|
546
|
+
exprs.eequiv(left, right),
|
|
547
|
+
[
|
|
548
|
+
[exprs.enot(left), exprs.enot(right)],
|
|
549
|
+
[left, right],
|
|
550
|
+
],
|
|
551
|
+
proof,
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
case "notconnect": {
|
|
557
|
+
const left = rule.left;
|
|
558
|
+
const right = rule.right;
|
|
559
|
+
switch (rule.connective) {
|
|
560
|
+
case "and":
|
|
561
|
+
return printBeta(
|
|
562
|
+
indent,
|
|
563
|
+
dict,
|
|
564
|
+
"notand",
|
|
565
|
+
exprs.enot(exprs.eand(left, right)),
|
|
566
|
+
[[exprs.enot(left)], [exprs.enot(right)]],
|
|
567
|
+
proof,
|
|
568
|
+
);
|
|
569
|
+
case "or":
|
|
570
|
+
return printAlpha(
|
|
571
|
+
indent,
|
|
572
|
+
dict,
|
|
573
|
+
"notor",
|
|
574
|
+
exprs.enot(exprs.eor(left, right)),
|
|
575
|
+
[exprs.enot(left), exprs.enot(right)],
|
|
576
|
+
proof,
|
|
577
|
+
);
|
|
578
|
+
case "imply":
|
|
579
|
+
return printAlpha(
|
|
580
|
+
indent,
|
|
581
|
+
dict,
|
|
582
|
+
"notimply",
|
|
583
|
+
exprs.enot(exprs.eimply(left, right)),
|
|
584
|
+
[left, exprs.enot(right)],
|
|
585
|
+
proof,
|
|
586
|
+
);
|
|
587
|
+
case "equiv":
|
|
588
|
+
return printBeta(
|
|
589
|
+
indent,
|
|
590
|
+
dict,
|
|
591
|
+
"notequiv",
|
|
592
|
+
exprs.enot(exprs.eequiv(left, right)),
|
|
593
|
+
[
|
|
594
|
+
[exprs.enot(left), right],
|
|
595
|
+
[left, exprs.enot(right)],
|
|
596
|
+
],
|
|
597
|
+
proof,
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
case "extension":
|
|
603
|
+
return printExtension(indent, dict, proof, rule);
|
|
604
|
+
case "notnot":
|
|
605
|
+
return printAlpha(
|
|
606
|
+
indent,
|
|
607
|
+
dict,
|
|
608
|
+
"notnot",
|
|
609
|
+
exprs.enot(exprs.enot(rule.formula)),
|
|
610
|
+
[rule.formula],
|
|
611
|
+
proof,
|
|
612
|
+
);
|
|
613
|
+
case "ex":
|
|
614
|
+
return printInstance(
|
|
615
|
+
indent,
|
|
616
|
+
dict,
|
|
617
|
+
"ex_choose",
|
|
618
|
+
false,
|
|
619
|
+
rule.quantified,
|
|
620
|
+
rule.term,
|
|
621
|
+
rule.quantified,
|
|
622
|
+
proof,
|
|
623
|
+
false,
|
|
624
|
+
);
|
|
625
|
+
case "notall":
|
|
626
|
+
return printInstance(
|
|
627
|
+
indent,
|
|
628
|
+
dict,
|
|
629
|
+
"notall_choose",
|
|
630
|
+
true,
|
|
631
|
+
rule.quantified,
|
|
632
|
+
rule.term,
|
|
633
|
+
exprs.enot(rule.quantified),
|
|
634
|
+
proof,
|
|
635
|
+
false,
|
|
636
|
+
);
|
|
637
|
+
case "all":
|
|
638
|
+
return printInstance(
|
|
639
|
+
indent,
|
|
640
|
+
dict,
|
|
641
|
+
"all",
|
|
642
|
+
false,
|
|
643
|
+
rule.quantified,
|
|
644
|
+
rule.term,
|
|
645
|
+
rule.quantified,
|
|
646
|
+
proof,
|
|
647
|
+
true,
|
|
648
|
+
);
|
|
649
|
+
case "notex":
|
|
650
|
+
return printInstance(
|
|
651
|
+
indent,
|
|
652
|
+
dict,
|
|
653
|
+
"notex",
|
|
654
|
+
true,
|
|
655
|
+
rule.quantified,
|
|
656
|
+
rule.term,
|
|
657
|
+
exprs.enot(rule.quantified),
|
|
658
|
+
proof,
|
|
659
|
+
true,
|
|
660
|
+
);
|
|
661
|
+
case "lemma": {
|
|
662
|
+
const variables = rule.args.flatMap((arg) => {
|
|
663
|
+
if (arg.kind === "var") return [arg.symbol];
|
|
664
|
+
if (arg.kind === "tau") return [];
|
|
665
|
+
throw new TlaZenonError("Zenon: a lemma argument that is neither a variable nor a tau");
|
|
666
|
+
});
|
|
667
|
+
line(indent, "show FALSE\n");
|
|
668
|
+
line(indent, `by (rule ${rule.name} [`);
|
|
669
|
+
if (variables.length > 0)
|
|
670
|
+
write(`where ${variables.map((variable) => `?${variable}=${variable}`).join(" and ")}, `);
|
|
671
|
+
write(`OF ${printNames(proof.conclusions)}])\n`);
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
case "cut":
|
|
675
|
+
line(indent, "show FALSE\n");
|
|
676
|
+
line(indent, `proof (rule zenon_em [of "${expr(dict, rule.formula)}"])\n`);
|
|
677
|
+
return printBranches(
|
|
678
|
+
indent,
|
|
679
|
+
dict,
|
|
680
|
+
[[rule.formula], [exprs.enot(rule.formula)]],
|
|
681
|
+
proof.subproofs,
|
|
682
|
+
);
|
|
683
|
+
case "axiom": {
|
|
684
|
+
const positiveName = hname(rule.formula);
|
|
685
|
+
const negativeName = hname(exprs.enot(rule.formula));
|
|
686
|
+
line(indent, "show FALSE\n");
|
|
687
|
+
line(indent, `by (rule notE [OF ${negativeName} ${positiveName}])\n`);
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
case "definition": {
|
|
691
|
+
const conclusionName = hname(rule.folded);
|
|
692
|
+
const hypothesisName = hname(rule.unfolded);
|
|
693
|
+
line(
|
|
694
|
+
indent,
|
|
695
|
+
`have ${hypothesisName}_${conclusionName}: "${expr(dict, rule.unfolded)} == ${expr(dict, rule.folded)}"`,
|
|
696
|
+
);
|
|
697
|
+
const hypothesisPattern = makePattern(dict, rule.unfolded);
|
|
698
|
+
const conclusionPattern = makePattern(hypothesisPattern[1], rule.folded);
|
|
699
|
+
write(` (is "${hypothesisPattern[0]} == ${conclusionPattern[0]}")\n`);
|
|
700
|
+
line(indent, `by (unfold ${rule.name})\n`);
|
|
701
|
+
line(indent, `have ${hypothesisName}: "${expr(conclusionPattern[1], rule.unfolded)}"`);
|
|
702
|
+
const next = printIs(conclusionPattern[1], rule.unfolded);
|
|
703
|
+
line(indent, `by (unfold ${hypothesisName}_${conclusionName}, fact ${conclusionName})\n`);
|
|
704
|
+
return printTree(indent, next, getSingle(proof));
|
|
705
|
+
}
|
|
706
|
+
case "notequal": {
|
|
707
|
+
const left = rule.left;
|
|
708
|
+
const right = rule.right;
|
|
709
|
+
if (left.kind !== "app" || right.kind !== "app" || left.symbol !== right.symbol)
|
|
710
|
+
throw new TlaZenonError("Zenon: notequal on different applications");
|
|
711
|
+
const negation = exprs.enot(equal(left, right));
|
|
712
|
+
line(indent, "show FALSE\n");
|
|
713
|
+
line(indent, `proof (rule zenon_noteq [of "${expr(dict, right)}"])\n`);
|
|
714
|
+
const next = printArgumentEqualities(indent, dict, left.args, right.args, proof.subproofs);
|
|
715
|
+
return printSubstitution(
|
|
716
|
+
indent,
|
|
717
|
+
next,
|
|
718
|
+
(args) => exprs.enot(equal(exprs.eapp(left.symbol, args), right)),
|
|
719
|
+
left.args,
|
|
720
|
+
right.args,
|
|
721
|
+
[],
|
|
722
|
+
negation,
|
|
723
|
+
);
|
|
724
|
+
}
|
|
725
|
+
case "pnotp": {
|
|
726
|
+
const positive = rule.positive;
|
|
727
|
+
const negative = rule.negative;
|
|
728
|
+
const negated = negative.kind === "not" ? negative.args[0] : undefined;
|
|
729
|
+
if (
|
|
730
|
+
positive.kind !== "app" ||
|
|
731
|
+
negated?.kind !== "app" ||
|
|
732
|
+
positive.symbol !== negated.symbol
|
|
733
|
+
)
|
|
734
|
+
throw new TlaZenonError("Zenon: pnotp on different applications");
|
|
735
|
+
line(indent, "show FALSE\n");
|
|
736
|
+
line(indent, `proof (rule notE [OF ${hname(negative)}])\n`);
|
|
737
|
+
const next = printArgumentEqualities(
|
|
738
|
+
indent,
|
|
739
|
+
dict,
|
|
740
|
+
positive.args,
|
|
741
|
+
negated.args,
|
|
742
|
+
proof.subproofs,
|
|
743
|
+
);
|
|
744
|
+
return printSubstitution(
|
|
745
|
+
indent,
|
|
746
|
+
next,
|
|
747
|
+
(args) => exprs.eapp(positive.symbol, args),
|
|
748
|
+
positive.args,
|
|
749
|
+
negated.args,
|
|
750
|
+
[],
|
|
751
|
+
positive,
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
case "noteq": {
|
|
755
|
+
const name = hname(exprs.enot(equal(rule.term, rule.term)));
|
|
756
|
+
line(indent, "show FALSE\n");
|
|
757
|
+
line(indent, `by (rule zenon_noteq [OF ${name}])\n`);
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
case "eqsym": {
|
|
761
|
+
const equalityName = hname(equal(rule.left, rule.right));
|
|
762
|
+
const negationName = hname(exprs.enot(equal(rule.right, rule.left)));
|
|
763
|
+
line(indent, "show FALSE\n");
|
|
764
|
+
line(indent, `by (rule zenon_eqsym [OF ${equalityName} ${negationName}])\n`);
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
case "nottrue":
|
|
768
|
+
line(indent, "show FALSE\n");
|
|
769
|
+
line(indent, `by (rule zenon_nottrue [OF ${hname(exprs.enot(TRUE))}])\n`);
|
|
770
|
+
return;
|
|
771
|
+
case "false":
|
|
772
|
+
line(indent, "show FALSE\n");
|
|
773
|
+
line(indent, `by (rule ${hname(FALSE)})\n`);
|
|
774
|
+
return;
|
|
775
|
+
case "congruencelr":
|
|
776
|
+
case "congruencerl": {
|
|
777
|
+
const subproof = getSingle(proof);
|
|
778
|
+
const equality =
|
|
779
|
+
rule.kind === "congruencelr"
|
|
780
|
+
? equal(rule.left, rule.right)
|
|
781
|
+
: equal(rule.right, rule.left);
|
|
782
|
+
const hypothesis = exprs.apply(rule.context, rule.left);
|
|
783
|
+
const conclusion = exprs.apply(rule.context, rule.right);
|
|
784
|
+
line(indent, `have ${hname(conclusion)}: "${expr(dict, conclusion)}"`);
|
|
785
|
+
const next = printIs(dict, conclusion);
|
|
786
|
+
const hypothesisName = hname(hypothesis);
|
|
787
|
+
const equalityName = hname(equality);
|
|
788
|
+
const lemma = rule.kind === "congruencelr" ? "subst" : "ssubst";
|
|
789
|
+
line(
|
|
790
|
+
indent,
|
|
791
|
+
`by (rule ${lemma} [where P="${expr(next, rule.context)}", OF ${equalityName} ${hypothesisName}])\n`,
|
|
792
|
+
);
|
|
793
|
+
return printTree(indent, next, subproof);
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
const printArgumentEqualities = (
|
|
799
|
+
indent: number,
|
|
800
|
+
dict: number,
|
|
801
|
+
lefts: Expr[],
|
|
802
|
+
rights: Expr[],
|
|
803
|
+
subproofs: LlProof[],
|
|
804
|
+
) => {
|
|
805
|
+
if (lefts.length !== rights.length || lefts.length !== subproofs.length)
|
|
806
|
+
throw new TlaZenonError("Zenon: list_fold_left3");
|
|
807
|
+
return lefts.reduce(
|
|
808
|
+
(current, left, position) =>
|
|
809
|
+
printSubEqual(increment(indent), current, left, rights[position], subproofs[position]),
|
|
810
|
+
dict,
|
|
811
|
+
);
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
// A hypothesis of a product or record set's rules, and its negation, each in its own branch.
|
|
815
|
+
const printNegatedHaves = (
|
|
816
|
+
indent: number,
|
|
817
|
+
dict: number,
|
|
818
|
+
branches: Expr[][],
|
|
819
|
+
subproofs: LlProof[],
|
|
820
|
+
) => {
|
|
821
|
+
if (branches.length !== subproofs.length) throw new TlaZenonError("Zenon: List.fold_left2");
|
|
822
|
+
return branches.reduce((current, branch, position) => {
|
|
823
|
+
const negation = branch[0];
|
|
824
|
+
if (branch.length !== 1 || negation.kind !== "not")
|
|
825
|
+
throw new TlaZenonError("Zenon: a branch that isn't one negation");
|
|
826
|
+
const formula = negation.args[0];
|
|
827
|
+
line(indent, `have ${hname(formula)}: "${expr(current, formula)}"`);
|
|
828
|
+
const next = printIs(current, formula);
|
|
829
|
+
line(indent, "proof (rule zenon_nnpp)\n");
|
|
830
|
+
line(indent + 1, `assume ${hname(negation)}: "${expr(next, negation)}"`);
|
|
831
|
+
printTree(indent + 1, printIs(next, negation), subproofs[position]);
|
|
832
|
+
line(indent, "qed\n");
|
|
833
|
+
return next;
|
|
834
|
+
}, dict);
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
const getNegatedNames = (branches: Expr[][]) =>
|
|
838
|
+
branches
|
|
839
|
+
.map((branch) => {
|
|
840
|
+
const negation = branch[0];
|
|
841
|
+
if (branch.length !== 1 || negation.kind !== "not")
|
|
842
|
+
throw new TlaZenonError("Zenon: a branch that isn't one negation");
|
|
843
|
+
return `${hname(negation.args[0])} `;
|
|
844
|
+
})
|
|
845
|
+
.join("");
|
|
846
|
+
|
|
847
|
+
const printExtension = (
|
|
848
|
+
indent: number,
|
|
849
|
+
dict: number,
|
|
850
|
+
proof: LlProof,
|
|
851
|
+
rule: Extract<LlProof["rule"], { kind: "extension" }>,
|
|
852
|
+
) => {
|
|
853
|
+
const args = rule.args;
|
|
854
|
+
const conclusions = rule.conclusions;
|
|
855
|
+
const branches = rule.branches;
|
|
856
|
+
const malformed = () => new TlaZenonError(`Zenon: a malformed ${rule.name} rule`);
|
|
857
|
+
switch (rule.name) {
|
|
858
|
+
case "zenon_case": {
|
|
859
|
+
if (args.length !== 1 || conclusions.length !== 1) throw malformed();
|
|
860
|
+
const isOther = getLastBranchIsOther(branches);
|
|
861
|
+
const conclusionName = hname(conclusions[0]);
|
|
862
|
+
line(indent, "show FALSE\n");
|
|
863
|
+
line(
|
|
864
|
+
indent,
|
|
865
|
+
`proof (rule zenon_case${isOther ? "other" : ""}${branches.length - 1} [of "${expr(dict, args[0])}", OF ${conclusionName}])\n`,
|
|
866
|
+
);
|
|
867
|
+
return printBranches(indent, dict, branches, proof.subproofs);
|
|
868
|
+
}
|
|
869
|
+
case "zenon_stringequal": {
|
|
870
|
+
if (args.length !== 2 || conclusions.length !== 1 || branches.length !== 0)
|
|
871
|
+
throw malformed();
|
|
872
|
+
const conclusion = conclusions[0];
|
|
873
|
+
const negation = exprs.enot(conclusion);
|
|
874
|
+
line(indent, `have ${hname(negation)}: "${expr(dict, negation)}"\n`);
|
|
875
|
+
line(indent, "by (simp only: zenon_sa_1 zenon_sa_2,\n");
|
|
876
|
+
line(indent, " ((rule zenon_sa_diff_2)+)?,\n");
|
|
877
|
+
line(indent, " (rule zenon_sa_diff_3, auto)?,\n");
|
|
878
|
+
line(indent, " (rule zenon_sa_diff_1, auto)?,\n");
|
|
879
|
+
line(indent, " (rule zenon_sa_diff_0a)?, (rule zenon_sa_diff_0b)?)\n");
|
|
880
|
+
line(indent, "show FALSE\n");
|
|
881
|
+
const conclusionName = hname(conclusion);
|
|
882
|
+
line(indent, `by (rule notE [OF ${hname(negation)} ${conclusionName}])\n`);
|
|
883
|
+
return;
|
|
884
|
+
}
|
|
885
|
+
case "zenon_stringdiffll":
|
|
886
|
+
case "zenon_stringdifflr":
|
|
887
|
+
case "zenon_stringdiffrl":
|
|
888
|
+
case "zenon_stringdiffrr": {
|
|
889
|
+
const hypothesis = branches[0]?.[0];
|
|
890
|
+
if (
|
|
891
|
+
args.length !== 4 ||
|
|
892
|
+
conclusions.length !== 2 ||
|
|
893
|
+
branches.length !== 1 ||
|
|
894
|
+
branches[0].length !== 1 ||
|
|
895
|
+
hypothesis === undefined
|
|
896
|
+
)
|
|
897
|
+
throw malformed();
|
|
898
|
+
const subproof = getSingle(proof);
|
|
899
|
+
const difference = exprs.enot(equal(args[1], args[3]));
|
|
900
|
+
line(indent, `have ${hname(difference)}: "${expr(dict, difference)}"\n`);
|
|
901
|
+
line(indent, "by auto\n");
|
|
902
|
+
line(indent, `have ${hname(hypothesis)}: "${expr(dict, hypothesis)}"`);
|
|
903
|
+
const next = printIs(dict, hypothesis);
|
|
904
|
+
const secondName = hname(conclusions[1]);
|
|
905
|
+
const firstName = hname(conclusions[0]);
|
|
906
|
+
line(
|
|
907
|
+
indent,
|
|
908
|
+
`by (rule ${rule.name} [OF ${hname(difference)} ${firstName} ${secondName}])\n`,
|
|
909
|
+
);
|
|
910
|
+
return printTree(increment(indent), next, subproof);
|
|
911
|
+
}
|
|
912
|
+
case "zenon_tuple_eq_match":
|
|
913
|
+
case "zenon_record_eq_match": {
|
|
914
|
+
if (args.length !== 0 || conclusions.length !== 1 || branches.length !== 1)
|
|
915
|
+
throw malformed();
|
|
916
|
+
const conclusion = conclusions[0];
|
|
917
|
+
return printHaves(
|
|
918
|
+
indent,
|
|
919
|
+
dict,
|
|
920
|
+
branches[0],
|
|
921
|
+
getSingle(proof),
|
|
922
|
+
() => `using ${hname(conclusion)} by auto\n`,
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
case "zenon_tuple_eq_mismatch":
|
|
926
|
+
case "zenon_record_eq_mismatch":
|
|
927
|
+
if (args.length !== 1 || conclusions.length !== 1 || branches.length !== 0)
|
|
928
|
+
throw malformed();
|
|
929
|
+
line(indent, "show FALSE\n");
|
|
930
|
+
line(indent, `using ${hname(args[0])} by auto\n`);
|
|
931
|
+
return;
|
|
932
|
+
case "zenon_tuple_access":
|
|
933
|
+
case "zenon_tuple_len":
|
|
934
|
+
case "zenon_record_domain": {
|
|
935
|
+
const hypothesis = branches[0]?.[0];
|
|
936
|
+
if (
|
|
937
|
+
args.length !== 3 ||
|
|
938
|
+
conclusions.length !== 1 ||
|
|
939
|
+
branches.length !== 1 ||
|
|
940
|
+
branches[0].length !== 1 ||
|
|
941
|
+
hypothesis === undefined
|
|
942
|
+
)
|
|
943
|
+
throw malformed();
|
|
944
|
+
const subproof = getSingle(proof);
|
|
945
|
+
const equation = equal(args[1], args[2]);
|
|
946
|
+
line(indent, `have ${hname(equation)}: "${expr(dict, equation)}"`);
|
|
947
|
+
const afterEquation = printIs(dict, equation);
|
|
948
|
+
line(indent, "by auto\n");
|
|
949
|
+
line(indent, `have ${hname(hypothesis)}: "${expr(afterEquation, hypothesis)}"`);
|
|
950
|
+
const next = printIs(afterEquation, hypothesis);
|
|
951
|
+
const conclusionName = hname(conclusions[0]);
|
|
952
|
+
const equationName = hname(equation);
|
|
953
|
+
line(
|
|
954
|
+
indent,
|
|
955
|
+
`by (rule subst [where P="${expr(dict, args[0])}", OF ${equationName} ${conclusionName}])\n`,
|
|
956
|
+
);
|
|
957
|
+
return printTree(indent, next, subproof);
|
|
958
|
+
}
|
|
959
|
+
case "zenon_record_access": {
|
|
960
|
+
const hypothesis = branches[0]?.[0];
|
|
961
|
+
const old = args[1];
|
|
962
|
+
if (
|
|
963
|
+
args.length !== 3 ||
|
|
964
|
+
conclusions.length !== 1 ||
|
|
965
|
+
branches.length !== 1 ||
|
|
966
|
+
branches[0].length !== 1 ||
|
|
967
|
+
hypothesis === undefined
|
|
968
|
+
)
|
|
969
|
+
throw malformed();
|
|
970
|
+
const isApplication =
|
|
971
|
+
old.kind === "app" && old.symbol === "TLA.fapply" && old.args.length === 2;
|
|
972
|
+
const record = isApplication ? old.args[0] : undefined;
|
|
973
|
+
const field = isApplication ? old.args[1] : undefined;
|
|
974
|
+
if (record?.kind !== "app" || record.symbol !== "TLA.record" || field === undefined)
|
|
975
|
+
throw malformed();
|
|
976
|
+
const subproof = getSingle(proof);
|
|
977
|
+
const fieldPosition = record.args.indexOf(field);
|
|
978
|
+
if (fieldPosition === -1) throw new TlaZenonError("Zenon: list_indexq");
|
|
979
|
+
const position = Math.floor(fieldPosition / 2) + 1;
|
|
980
|
+
const length = Math.floor(record.args.length / 2);
|
|
981
|
+
const inDomain = exprs.eapp("TLA.in", [field, exprs.eapp("TLA.DOMAIN", [record])]);
|
|
982
|
+
const equation = equal(old, args[2]);
|
|
983
|
+
const both = exprs.eand(inDomain, equation);
|
|
984
|
+
line(indent, `have ${hname(both)}: "${expr(dict, both)}"`);
|
|
985
|
+
const afterBoth = printIs(dict, both);
|
|
986
|
+
line(
|
|
987
|
+
indent,
|
|
988
|
+
position === 1
|
|
989
|
+
? "by ((rule zenon_recfield_1)+, rule zenon_recfield_2b)\n"
|
|
990
|
+
: `by (${"rule zenon_recfield_1, ".repeat(Math.max(0, length - position))}rule zenon_recfield_2, ((rule zenon_recfield_3)+)?, rule zenon_recfield_3b, auto)\n`,
|
|
991
|
+
);
|
|
992
|
+
line(indent, `have ${hname(equation)}: "${expr(afterBoth, equation)}"`);
|
|
993
|
+
const afterEquation = printIs(afterBoth, equation);
|
|
994
|
+
line(indent, `by (rule conjD2 [OF ${hname(both)}])\n`);
|
|
995
|
+
line(indent, `have ${hname(hypothesis)}: "${expr(afterEquation, hypothesis)}"`);
|
|
996
|
+
const next = printIs(afterEquation, hypothesis);
|
|
997
|
+
const conclusionName = hname(conclusions[0]);
|
|
998
|
+
const equationName = hname(equation);
|
|
999
|
+
line(
|
|
1000
|
+
indent,
|
|
1001
|
+
`by (rule subst [where P="${expr(next, args[0])}", OF ${equationName} ${conclusionName}])\n`,
|
|
1002
|
+
);
|
|
1003
|
+
return printTree(indent, next, subproof);
|
|
1004
|
+
}
|
|
1005
|
+
case "zenon_in_product":
|
|
1006
|
+
case "zenon_in_recordset": {
|
|
1007
|
+
const hypotheses = branches[0] ?? [];
|
|
1008
|
+
if (
|
|
1009
|
+
args.length !== 2 ||
|
|
1010
|
+
conclusions.length !== 1 ||
|
|
1011
|
+
branches.length !== 1 ||
|
|
1012
|
+
hypotheses.length < 2
|
|
1013
|
+
)
|
|
1014
|
+
throw malformed();
|
|
1015
|
+
const subproof = getSingle(proof);
|
|
1016
|
+
const conclusion = conclusions[0];
|
|
1017
|
+
const element = args[0];
|
|
1018
|
+
const set = args[1];
|
|
1019
|
+
const isProduct = rule.name === "zenon_in_product";
|
|
1020
|
+
const afterElement = printLet(dict, indent, element);
|
|
1021
|
+
const afterSet = isProduct ? printLet(afterElement, indent, set) : afterElement;
|
|
1022
|
+
const printSimple = (formula: Expr) => {
|
|
1023
|
+
if (!subproof.conclusions.includes(formula)) return;
|
|
1024
|
+
const conclusionName = hname(conclusion);
|
|
1025
|
+
line(
|
|
1026
|
+
indent,
|
|
1027
|
+
`have ${hname(formula)}: "${expr(afterSet, formula)}" using ${conclusionName} by auto\n`,
|
|
1028
|
+
);
|
|
1029
|
+
};
|
|
1030
|
+
printSimple(hypotheses[0]);
|
|
1031
|
+
printSimple(hypotheses[1]);
|
|
1032
|
+
const one = exprs.eapp("TLA.fapply", [exprs.evar("TLA.Succ"), exprs.evar("0")]);
|
|
1033
|
+
const succ = (number: Expr) => exprs.eapp("TLA.fapply", [exprs.evar("TLA.Succ"), number]);
|
|
1034
|
+
if (isProduct) {
|
|
1035
|
+
const isSequence = exprs.eapp("TLA.isASeq", [set]);
|
|
1036
|
+
line(indent, `have ${hname(isSequence)}: "${expr(afterSet, isSequence)}" by auto\n`);
|
|
1037
|
+
const final = hypotheses.slice(2).reduce(
|
|
1038
|
+
(state: [number, Expr], formula): [number, Expr] => {
|
|
1039
|
+
const current = state[0];
|
|
1040
|
+
const index = state[1];
|
|
1041
|
+
if (!subproof.conclusions.includes(formula)) return [current, succ(index)];
|
|
1042
|
+
const inLength = exprs.eapp("TLA.in", [
|
|
1043
|
+
index,
|
|
1044
|
+
exprs.eapp("arith.natrange", [one, exprs.eapp("TLA.Len", [set])]),
|
|
1045
|
+
]);
|
|
1046
|
+
line(indent, `have ${hname(inLength)}: "${expr(current, inLength)}" by auto\n`);
|
|
1047
|
+
const component = exprs.eapp("TLA.in", [
|
|
1048
|
+
exprs.eapp("TLA.fapply", [element, index]),
|
|
1049
|
+
exprs.eapp("TLA.fapply", [set, index]),
|
|
1050
|
+
]);
|
|
1051
|
+
line(indent, `have ${hname(component)}: "${expr(current, component)}"`);
|
|
1052
|
+
const afterComponent = printIs(current, component);
|
|
1053
|
+
const inLengthName = hname(inLength);
|
|
1054
|
+
const isSequenceName = hname(isSequence);
|
|
1055
|
+
line(
|
|
1056
|
+
indent,
|
|
1057
|
+
`by (rule zenon_in_product_i [OF ${hname(conclusion)} ${isSequenceName} ${inLengthName}])\n`,
|
|
1058
|
+
);
|
|
1059
|
+
line(indent, `have ${hname(formula)}: "${expr(afterComponent, formula)}"`);
|
|
1060
|
+
const afterFormula = printIs(afterComponent, formula);
|
|
1061
|
+
line(indent, `using ${hname(component)} by auto\n`);
|
|
1062
|
+
return [afterFormula, succ(index)];
|
|
1063
|
+
},
|
|
1064
|
+
[afterSet, one],
|
|
1065
|
+
);
|
|
1066
|
+
return printTree(indent, final[0], subproof);
|
|
1067
|
+
}
|
|
1068
|
+
if (set.kind !== "app" || set.symbol !== "TLA.recordset") throw malformed();
|
|
1069
|
+
const fields = getPairs(set.args);
|
|
1070
|
+
const domains = exprs.eapp(
|
|
1071
|
+
"TLA.tuple",
|
|
1072
|
+
fields.map((pair) => pair[0]),
|
|
1073
|
+
);
|
|
1074
|
+
const ranges = exprs.eapp(
|
|
1075
|
+
"TLA.tuple",
|
|
1076
|
+
fields.map((pair) => pair[1]),
|
|
1077
|
+
);
|
|
1078
|
+
const afterDomains = printLet(afterSet, indent, domains);
|
|
1079
|
+
const afterRanges = printLet(afterDomains, indent, ranges);
|
|
1080
|
+
const final = hypotheses.slice(2).reduce(
|
|
1081
|
+
(state: [number, Expr], formula): [number, Expr] => {
|
|
1082
|
+
const current = state[0];
|
|
1083
|
+
const index = state[1];
|
|
1084
|
+
if (!subproof.conclusions.includes(formula)) return [current, succ(index)];
|
|
1085
|
+
const inDomain = exprs.eapp("TLA.in", [index, exprs.eapp("TLA.DOMAIN", [domains])]);
|
|
1086
|
+
line(indent, `have ${hname(inDomain)}: "${expr(current, inDomain)}" by auto\n`);
|
|
1087
|
+
const component = exprs.eapp("TLA.in", [
|
|
1088
|
+
exprs.eapp("TLA.fapply", [element, exprs.eapp("TLA.fapply", [domains, index])]),
|
|
1089
|
+
exprs.eapp("TLA.fapply", [ranges, index]),
|
|
1090
|
+
]);
|
|
1091
|
+
line(indent, `have ${hname(component)}: "${expr(current, component)}"`);
|
|
1092
|
+
const afterComponent = printIs(current, component);
|
|
1093
|
+
const inDomainName = hname(inDomain);
|
|
1094
|
+
line(
|
|
1095
|
+
indent,
|
|
1096
|
+
`by (rule zenon_in_recordset_field [OF ${hname(conclusion)} ${inDomainName}])\n`,
|
|
1097
|
+
);
|
|
1098
|
+
line(indent, `have ${hname(formula)}: "${expr(afterComponent, formula)}"`);
|
|
1099
|
+
const afterFormula = printIs(afterComponent, formula);
|
|
1100
|
+
line(indent, `using ${hname(component)} by auto\n`);
|
|
1101
|
+
return [afterFormula, succ(index)];
|
|
1102
|
+
},
|
|
1103
|
+
[afterRanges, one],
|
|
1104
|
+
);
|
|
1105
|
+
return printTree(indent, final[0], subproof);
|
|
1106
|
+
}
|
|
1107
|
+
case "zenon_notin_product": {
|
|
1108
|
+
if (args.length !== 2 || conclusions.length !== 1) throw malformed();
|
|
1109
|
+
const final = printNegatedHaves(
|
|
1110
|
+
indent,
|
|
1111
|
+
printLet(dict, indent, args[0]),
|
|
1112
|
+
branches,
|
|
1113
|
+
proof.subproofs,
|
|
1114
|
+
);
|
|
1115
|
+
line(indent, `show FALSE using ${hname(conclusions[0])} `);
|
|
1116
|
+
write(getNegatedNames(branches));
|
|
1117
|
+
write(`inProductI[where s="${expr(final, args[1])}"] `);
|
|
1118
|
+
write("by auto\n");
|
|
1119
|
+
return;
|
|
1120
|
+
}
|
|
1121
|
+
case "zenon_notin_recordset": {
|
|
1122
|
+
if (args.length !== 2 || conclusions.length !== 1) throw malformed();
|
|
1123
|
+
printNegatedHaves(indent, printLet(dict, indent, args[0]), branches, proof.subproofs);
|
|
1124
|
+
line(indent, `show FALSE by (rule notE [OF ${hname(conclusions[0])}],\n`);
|
|
1125
|
+
line(indent, ` rule zenon_inrecordsetI${branches.length - 2} [OF `);
|
|
1126
|
+
write(getNegatedNames(branches));
|
|
1127
|
+
write("])\n");
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
case "zenon_tuple_notisaseq":
|
|
1131
|
+
case "zenon_record_notisafcn":
|
|
1132
|
+
if (args.length !== 0 || conclusions.length !== 1 || branches.length !== 0)
|
|
1133
|
+
throw malformed();
|
|
1134
|
+
line(indent, `show FALSE using ${hname(conclusions[0])} by auto\n`);
|
|
1135
|
+
return;
|
|
1136
|
+
case "zenon_record_neq_match":
|
|
1137
|
+
line(indent, "sorry\n");
|
|
1138
|
+
return;
|
|
1139
|
+
case "zenon_neq_record":
|
|
1140
|
+
case "zenon_record_neq":
|
|
1141
|
+
if (args.length !== 2 || conclusions.length !== 1 || branches.length < 2) throw malformed();
|
|
1142
|
+
line(indent, "sorry\n");
|
|
1143
|
+
return;
|
|
1144
|
+
}
|
|
1145
|
+
if (branches.length === 0) {
|
|
1146
|
+
line(indent, "show FALSE\n");
|
|
1147
|
+
line(
|
|
1148
|
+
indent,
|
|
1149
|
+
`by (rule ${rule.name} [of ${printArgs(dict, args)}, OF ${printNames(conclusions)}])\n`,
|
|
1150
|
+
);
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
if (branches.length === 1)
|
|
1154
|
+
return printHaves(
|
|
1155
|
+
indent,
|
|
1156
|
+
dict,
|
|
1157
|
+
branches[0],
|
|
1158
|
+
getSingle(proof),
|
|
1159
|
+
(next, position) =>
|
|
1160
|
+
`by (rule ${rule.name}_${position} [of ${printArgs(next, args)}, OF ${printNames(conclusions)}])\n`,
|
|
1161
|
+
);
|
|
1162
|
+
line(indent, "show FALSE\n");
|
|
1163
|
+
line(
|
|
1164
|
+
indent,
|
|
1165
|
+
`proof (rule ${rule.name} [of ${printArgs(dict, args)}, OF ${printNames(conclusions)}])\n`,
|
|
1166
|
+
);
|
|
1167
|
+
printBranches(indent, dict, branches, proof.subproofs);
|
|
1168
|
+
};
|
|
1169
|
+
|
|
1170
|
+
const printLemma = (indent: number, dict: number, lemma: Lemma) => {
|
|
1171
|
+
if (getLimitFailure(context.limits) !== undefined) return;
|
|
1172
|
+
const params = lemma.params.flatMap((param) => {
|
|
1173
|
+
if (param[1].kind === "var") return [param[1].symbol];
|
|
1174
|
+
if (param[1].kind === "tau") return [];
|
|
1175
|
+
throw new TlaZenonError("Zenon: a lemma parameter that is neither a variable nor a tau");
|
|
1176
|
+
});
|
|
1177
|
+
const conclusions = lemma.proof.conclusions;
|
|
1178
|
+
line(indent, `have ${lemma.name}: "`);
|
|
1179
|
+
write(params.map((param) => `!!${param}. `).join(""));
|
|
1180
|
+
write(conclusions.map((conclusion) => `${expr(dict, conclusion)} ==> `).join(""));
|
|
1181
|
+
write('FALSE"');
|
|
1182
|
+
const patterns: string[] = [];
|
|
1183
|
+
const afterPatterns = conclusions.reduce((current, conclusion) => {
|
|
1184
|
+
const pattern = makePattern(current, conclusion);
|
|
1185
|
+
patterns.push(pattern[0]);
|
|
1186
|
+
return pattern[1];
|
|
1187
|
+
}, dict);
|
|
1188
|
+
write(
|
|
1189
|
+
patterns.every((pattern) => pattern === "_")
|
|
1190
|
+
? "\n"
|
|
1191
|
+
: ` (is "${patterns.map((pattern) => `${pattern} ==> `).join("")}FALSE")\n`,
|
|
1192
|
+
);
|
|
1193
|
+
line(indent, "proof -\n");
|
|
1194
|
+
for (const param of params) line(increment(indent), `fix "${param}"\n`);
|
|
1195
|
+
printTree(
|
|
1196
|
+
increment(indent),
|
|
1197
|
+
printSequent(increment(indent), afterPatterns, conclusions),
|
|
1198
|
+
lemma.proof,
|
|
1199
|
+
);
|
|
1200
|
+
line(indent, "qed\n");
|
|
1201
|
+
};
|
|
1202
|
+
|
|
1203
|
+
// Zenon's p_theorem.
|
|
1204
|
+
const theorem = lemmas[lemmas.length - 1];
|
|
1205
|
+
const hypotheses = context.phrases.flatMap((phrase) => (phrase.kind === "hyp" ? [phrase] : []));
|
|
1206
|
+
const negatedGoal =
|
|
1207
|
+
hypotheses.find((phrase) => phrase.name === GOAL_NAME)?.formula ?? exprs.enot(FALSE);
|
|
1208
|
+
if (negatedGoal.kind !== "not") throw new TlaZenonError("Zenon: a goal that isn't negated");
|
|
1209
|
+
const goal = negatedGoal.args[0];
|
|
1210
|
+
const assumptions = theorem.proof.conclusions.filter((formula) => formula !== negatedGoal);
|
|
1211
|
+
const realNames = new Map<number, string>();
|
|
1212
|
+
for (const phrase of hypotheses)
|
|
1213
|
+
if (phrase.name !== "") realNames.set(getNumber(phrase.formula), phrase.name);
|
|
1214
|
+
write("(* PROOF-FOUND *)\n(* BEGIN-PROOF *)\n");
|
|
1215
|
+
line(0, "proof (rule zenon_nnpp)\n");
|
|
1216
|
+
const naryRules = new Map<string, string>();
|
|
1217
|
+
const collectNaryRules = (tree: LlProof) => {
|
|
1218
|
+
const rule = tree.rule;
|
|
1219
|
+
if (rule.kind === "extension" && rule.name === "zenon_case") {
|
|
1220
|
+
const arity = rule.branches.length - 1;
|
|
1221
|
+
const isOther = getLastBranchIsOther(rule.branches);
|
|
1222
|
+
if (arity > 5)
|
|
1223
|
+
naryRules.set(
|
|
1224
|
+
`0 ${String(arity).padStart(9, "0")} ${isOther ? 1 : 0}`,
|
|
1225
|
+
printCaseLemma(arity, isOther),
|
|
1226
|
+
);
|
|
1227
|
+
}
|
|
1228
|
+
if (rule.kind === "extension" && rule.name === "zenon_notin_recordset") {
|
|
1229
|
+
const arity = rule.branches.length - 2;
|
|
1230
|
+
if (arity > 7) naryRules.set(`1 ${String(arity).padStart(9, "0")}`, printRecordLemma(arity));
|
|
1231
|
+
}
|
|
1232
|
+
tree.subproofs.forEach(collectNaryRules);
|
|
1233
|
+
};
|
|
1234
|
+
[theorem, ...lemmas.slice(0, -1)].forEach((lemma) => collectNaryRules(lemma.proof));
|
|
1235
|
+
for (const key of [...naryRules.keys()].sort()) write(naryRules.get(key) ?? "");
|
|
1236
|
+
const indent = increment(0);
|
|
1237
|
+
const dict = assumptions.reduce((current, formula) => {
|
|
1238
|
+
line(indent, `have ${hname(formula)}:"${expr(current, formula)}"`);
|
|
1239
|
+
const next = printIs(current, formula);
|
|
1240
|
+
const realName = realNames.get(getNumber(formula));
|
|
1241
|
+
line(indent, realName === undefined ? "by fact\n" : `using ${realName} by blast\n`);
|
|
1242
|
+
return next;
|
|
1243
|
+
}, 0);
|
|
1244
|
+
for (const lemma of lemmas.slice(0, -1)) printLemma(indent, dict, lemma);
|
|
1245
|
+
printTree(indent, printSequent(indent, dict, [exprs.enot(goal)]), theorem.proof);
|
|
1246
|
+
line(0, "qed\n");
|
|
1247
|
+
write("(* END-PROOF *)\n");
|
|
1248
|
+
return getLimitFailure(context.limits) === undefined ? output.join("") : undefined;
|
|
1249
|
+
};
|