tla-ts 0.0.4-dev.4819fb7 → 0.0.4-dev.55f2117
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 +37 -126
- package/dist/cli.cjs +1 -1
- package/dist/cli.js +1 -1
- package/dist/deciders.cjs +366 -0
- package/dist/deciders.js +366 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +33 -1
- package/dist/index.d.ts +33 -1
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -4
- package/dist/parallel-worker.js +1 -4
- package/dist/prove-worker.cjs +9 -0
- package/dist/prove-worker.d.cts +9 -0
- package/dist/prove-worker.d.ts +9 -0
- package/dist/prove-worker.js +9 -0
- package/dist/prove.cjs +996 -0
- package/dist/prove.js +996 -0
- package/dist/run.cjs +45 -0
- package/dist/run.js +45 -0
- 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/ast.ts +2 -2
- package/src/check.ts +8 -7
- package/src/{cli-options.ts → cli/options.ts} +1 -1
- package/src/{run-cli.ts → cli/run.ts} +302 -100
- package/src/cli.ts +1 -1
- package/src/codegen.ts +26 -7
- package/src/config.ts +2 -2
- package/src/environment.ts +9 -9
- package/src/errors.ts +89 -30
- package/src/evaluate.ts +58 -23
- package/src/index.ts +10 -0
- package/src/levels.ts +1 -1
- package/src/live-check.ts +1 -1
- package/src/liveness.ts +1 -1
- package/src/{obligation-action.ts → obligations/action.ts} +247 -69
- package/src/{obligation-anon.ts → obligations/anon.ts} +49 -47
- package/src/obligations/ctx.ts +64 -0
- package/src/{obligation-deref.ts → obligations/deref.ts} +51 -92
- package/src/{obligation-expression.ts → obligations/expression.ts} +117 -57
- package/src/{obligation-format.ts → obligations/format.ts} +7 -8
- package/src/{obligation-generate.ts → obligations/generate.ts} +170 -192
- package/src/{obligations.ts → obligations/index.ts} +32 -16
- package/src/{obligation-levels.ts → obligations/levels.ts} +26 -28
- package/src/{obligation-module.ts → obligations/module.ts} +113 -121
- package/src/obligations/pickle.ts +98 -0
- package/src/obligations/pragma.ts +189 -0
- package/src/{obligation-printer.ts → obligations/printer.ts} +22 -80
- package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
- package/src/{obligation-source.ts → obligations/source.ts} +154 -99
- package/src/obligations/temporal.ts +321 -0
- package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
- package/src/parallel-worker.ts +4 -20
- package/src/parallel.ts +13 -46
- package/src/parser.ts +1 -1
- package/src/{pcal-ast.ts → pcal/ast.ts} +2 -2
- package/src/{pcal-parse.ts → pcal/parse.ts} +3 -3
- package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +2 -2
- package/src/{pcal-translate.ts → pcal/translate.ts} +2 -2
- package/src/{pcal-validate.ts → pcal/validate.ts} +6 -6
- package/src/prove/decide-pool.ts +95 -0
- package/src/prove/deciders.ts +33 -0
- package/src/prove/first-order.ts +78 -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 +446 -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 +546 -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 +433 -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 +331 -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 +330 -0
- package/src/prove/isabelle/pure/name.ts +98 -0
- package/src/prove/isabelle/pure/net.ts +215 -0
- package/src/prove/isabelle/pure/object-logic.ts +41 -0
- package/src/prove/isabelle/pure/pattern.ts +631 -0
- package/src/prove/isabelle/pure/raw-simplifier.ts +338 -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 +133 -0
- package/src/prove/isabelle/pure/simplifier.ts +106 -0
- package/src/prove/isabelle/pure/table.ts +201 -0
- package/src/prove/isabelle/pure/tactic.ts +168 -0
- package/src/prove/isabelle/pure/tactical.ts +264 -0
- package/src/prove/isabelle/pure/term-ord.ts +105 -0
- package/src/prove/isabelle/pure/term.ts +688 -0
- package/src/prove/isabelle/pure/thm.ts +783 -0
- package/src/prove/isabelle/pure/type.ts +121 -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 +508 -0
- package/src/prove/prep/defs.ts +33 -0
- package/src/prove/prep/elab.ts +156 -0
- package/src/prove/prep/enabled-axioms.ts +189 -0
- package/src/prove/prep/enabled.ts +43 -0
- package/src/prove/prep/fingerprint.ts +388 -0
- package/src/prove/prep/flatten.ts +70 -0
- package/src/prove/prep/level-comparison.ts +529 -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 +22 -0
- package/src/prove/prove.ts +498 -0
- package/src/prove/ptl/decide.ts +68 -0
- package/src/prove/ptl/encode.ts +255 -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 +209 -0
- package/src/prove/smt/context.ts +108 -0
- package/src/prove/smt/data.ts +636 -0
- package/src/prove/smt/encode.ts +164 -0
- package/src/prove/smt/extensionality.ts +65 -0
- package/src/prove/smt/flatten.ts +112 -0
- package/src/prove/smt/format.ts +154 -0
- package/src/prove/smt/instances.ts +181 -0
- package/src/prove/smt/rewrite.ts +772 -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 +91 -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 +642 -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 +737 -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/cache.ts +236 -0
- package/src/prove/tlapm/ocaml-marshal.ts +166 -0
- package/src/prove/tlapm/tlapm.ts +96 -0
- package/src/prove/tlaps-module.ts +510 -0
- package/src/prove/verdict.ts +18 -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 +379 -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 +162 -0
- package/src/prove/zenon/tla/tla.ts +506 -0
- package/src/randomization.ts +1 -1
- package/src/resolve.ts +1 -1
- package/src/runtime.ts +2 -2
- package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
- package/src/{sany-generator.ts → sany/generator.ts} +14 -14
- package/src/{sany-levels.ts → sany/levels.ts} +2 -2
- package/src/{sany-lexer.ts → sany/lexer.ts} +1 -1
- package/src/{sany-linter.ts → sany/linter.ts} +1 -1
- package/src/{sany-modules.ts → sany/modules.ts} +2 -2
- package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
- package/src/{sany-report.ts → sany/report.ts} +15 -15
- package/src/{sany-selectors.ts → sany/selectors.ts} +3 -3
- package/src/{sany-source.ts → sany/source.ts} +6 -6
- package/src/{sany-xml.ts → sany/xml.ts} +5 -5
- package/src/spec-description.ts +1 -1
- package/src/{standard-levels.ts → standard/levels.ts} +1 -1
- package/src/{standard-modules.ts → standard/modules.ts} +17 -14
- package/src/{standard-ranges.ts → standard/ranges.ts} +1 -1
- package/src/state-graph.ts +1 -1
- package/src/successors.ts +25 -6
- package/src/symmetry.ts +1 -1
- package/src/{tlc-messages.ts → tlc/messages.ts} +2 -2
- package/src/{tlc-order.ts → tlc/order.ts} +7 -7
- package/src/{tlc-random.ts → tlc/random.ts} +58 -35
- package/src/{tlc-replay.ts → tlc/replay.ts} +5 -5
- package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
- package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +14 -8
- package/src/{tlc-version.ts → tlc/version.ts} +8 -7
- package/src/values.ts +81 -36
- package/src/worker.ts +65 -0
- package/dist/parallel.cjs +0 -12
- package/dist/parallel.js +0 -12
- package/dist/run-cli.cjs +0 -55
- package/dist/run-cli.js +0 -55
- package/src/value-sources.ts +0 -29
- /package/src/{sany-nodes.ts → sany/nodes.ts} +0 -0
- /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
- /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
// A port of tlapm's action frontend (Action.process_obligation), which makes an obligation first
|
|
2
|
+
// order for the backends: [A]_v, <<A>>_v and UNCHANGED become primes (Expr.Tla_norm), primes move
|
|
3
|
+
// down to the variables (Symbol_commute), and what remains modal becomes opaque (Coalesce).
|
|
4
|
+
|
|
5
|
+
import { TlapmBugError } from "../../errors";
|
|
6
|
+
import { rewriteUnchanged } from "../../obligations/temporal";
|
|
7
|
+
import {
|
|
8
|
+
adjoin,
|
|
9
|
+
adjoinAll,
|
|
10
|
+
applyBuiltin,
|
|
11
|
+
at,
|
|
12
|
+
createContextMap,
|
|
13
|
+
getBoundedFresh,
|
|
14
|
+
getBoundFresh,
|
|
15
|
+
getFresh,
|
|
16
|
+
getLocalDefn,
|
|
17
|
+
getParamFresh,
|
|
18
|
+
getScopeAt,
|
|
19
|
+
getScopeHyps,
|
|
20
|
+
mapAccumulate,
|
|
21
|
+
mapHyp,
|
|
22
|
+
mapScoped,
|
|
23
|
+
type Builtin,
|
|
24
|
+
type Defn,
|
|
25
|
+
type Expr,
|
|
26
|
+
type Hyp,
|
|
27
|
+
type Level,
|
|
28
|
+
type Scope,
|
|
29
|
+
type Sequent,
|
|
30
|
+
} from "../../obligations/expression";
|
|
31
|
+
import { renderDoc } from "../../obligations/format";
|
|
32
|
+
import { getLevelInfo } from "../../obligations/levels";
|
|
33
|
+
import { printExpression } from "../../obligations/printer";
|
|
34
|
+
import { getMd5 } from "./md5";
|
|
35
|
+
|
|
36
|
+
type Rewrite = (scope: Scope | undefined, expression: Expr) => Expr;
|
|
37
|
+
|
|
38
|
+
const getHyp = (scope: Scope | undefined, index: number) => {
|
|
39
|
+
const hyp = getScopeAt(scope, index - 1)?.hyp;
|
|
40
|
+
if (!hyp) throw new TlapmBugError();
|
|
41
|
+
return hyp;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const getLevel = (scope: Scope | undefined, expression: Expr) =>
|
|
45
|
+
getLevelInfo(scope, expression).level;
|
|
46
|
+
|
|
47
|
+
const isBuiltin = (expression: Expr, builtins: Builtin[]) =>
|
|
48
|
+
expression.kind === "Apply" &&
|
|
49
|
+
expression.operator.kind === "Internal" &&
|
|
50
|
+
builtins.includes(expression.operator.builtin);
|
|
51
|
+
|
|
52
|
+
// Visit.map over a sequent: each hypothesis maps in the scope before it, hidden ones too if hyps
|
|
53
|
+
// is "all". The context maps by the obligation's own hypotheses, from which it derives.
|
|
54
|
+
const createSequentMap = (map: Rewrite, hyps: "visible" | "all") => {
|
|
55
|
+
const mapContext = createContextMap<Scope | undefined>(undefined, (scope, hyp) => {
|
|
56
|
+
const mapped = mapHyp(hyp, (inner) => map(scope, inner), hyps);
|
|
57
|
+
return [adjoin(scope, mapped), mapped];
|
|
58
|
+
});
|
|
59
|
+
return (keys: Hyp[], sequent: Sequent): Sequent => {
|
|
60
|
+
const mapped = mapContext(keys, sequent.context);
|
|
61
|
+
return { context: mapped.context, active: map(mapped.state, sequent.active) };
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Expr.Tla_norm.expand_action, then expand_unchanged.
|
|
66
|
+
const expandAction: Rewrite = (scope, expression) => {
|
|
67
|
+
if (expression.kind === "Sub" || expression.kind === "Tsub") {
|
|
68
|
+
const action = expandAction(scope, expression.action);
|
|
69
|
+
const subscript = expandAction(scope, expression.subscript);
|
|
70
|
+
const unchanged = applyBuiltin("UNCHANGED", [subscript], expression);
|
|
71
|
+
const step =
|
|
72
|
+
expression.modal === "Box"
|
|
73
|
+
? applyBuiltin("Disj", [action, unchanged], expression)
|
|
74
|
+
: applyBuiltin("Conj", [action, applyBuiltin("Neg", [unchanged], expression)], expression);
|
|
75
|
+
if (expression.kind === "Sub") return step;
|
|
76
|
+
return applyBuiltin(expression.modal === "Box" ? "BoxTrue" : "Diamond", [step], expression);
|
|
77
|
+
}
|
|
78
|
+
return mapScoped(scope, expression, expandAction, adjoin, "all");
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const expandUnchanged: Rewrite = (scope, expression) =>
|
|
82
|
+
expression.kind === "Apply" &&
|
|
83
|
+
isBuiltin(expression, ["UNCHANGED"]) &&
|
|
84
|
+
expression.args.length === 1
|
|
85
|
+
? rewriteUnchanged(expression.args[0])
|
|
86
|
+
: mapScoped(scope, expression, expandUnchanged, adjoin, "all");
|
|
87
|
+
|
|
88
|
+
const getModal = (builtin: Builtin, expression: Expr) =>
|
|
89
|
+
applyBuiltin(builtin, [expression], expression);
|
|
90
|
+
|
|
91
|
+
// prime_commuter: a prime distributes down to the declarations that are not constant.
|
|
92
|
+
const commutePrime: Rewrite = (scope, expression) => {
|
|
93
|
+
if (expression.kind === "Ix") {
|
|
94
|
+
const hyp = getHyp(scope, expression.index);
|
|
95
|
+
if (hyp.kind === "Fresh" && hyp.level === "Constant") return expression;
|
|
96
|
+
if (
|
|
97
|
+
hyp.kind === "Defn" &&
|
|
98
|
+
hyp.definition.kind === "Operator" &&
|
|
99
|
+
getLevel(getScopeAt(scope, expression.index), hyp.definition.body) === 0
|
|
100
|
+
)
|
|
101
|
+
return expression;
|
|
102
|
+
return getModal("Prime", expression);
|
|
103
|
+
}
|
|
104
|
+
if (
|
|
105
|
+
isBuiltin(expression, ["ENABLED"]) ||
|
|
106
|
+
expression.kind === "Opaque" ||
|
|
107
|
+
expression.kind === "Sequent" ||
|
|
108
|
+
expression.kind === "Let"
|
|
109
|
+
)
|
|
110
|
+
return getModal("Prime", expression);
|
|
111
|
+
if (
|
|
112
|
+
isBuiltin(expression, [
|
|
113
|
+
"UNCHANGED",
|
|
114
|
+
"Cdot",
|
|
115
|
+
"Leadsto",
|
|
116
|
+
"Actplus",
|
|
117
|
+
"BoxTrue",
|
|
118
|
+
"BoxFalse",
|
|
119
|
+
"Diamond",
|
|
120
|
+
]) ||
|
|
121
|
+
expression.kind === "Tquant" ||
|
|
122
|
+
expression.kind === "Sub" ||
|
|
123
|
+
expression.kind === "Tsub" ||
|
|
124
|
+
expression.kind === "Fair"
|
|
125
|
+
)
|
|
126
|
+
throw new TlapmBugError();
|
|
127
|
+
return mapScoped(scope, expression, commutePrime, adjoin);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// box_commuter and dia_commuter: [] distributes over /\ and \A, and <> over \/ and \E, and both
|
|
131
|
+
// vanish on a constant.
|
|
132
|
+
const getModalCommuter = (builtin: "BoxTrue" | "Diamond"): Rewrite => {
|
|
133
|
+
const connective: Builtin = builtin === "BoxTrue" ? "Conj" : "Disj";
|
|
134
|
+
const quantifier = builtin === "BoxTrue" ? "Forall" : "Exists";
|
|
135
|
+
const commute: Rewrite = (scope, expression) => {
|
|
136
|
+
if (getLevel(scope, expression) === 0) return expression;
|
|
137
|
+
const isDistributed =
|
|
138
|
+
isBuiltin(expression, [connective]) ||
|
|
139
|
+
((expression.kind === "Quant" || expression.kind === "Tquant") &&
|
|
140
|
+
expression.quantifier === quantifier);
|
|
141
|
+
return isDistributed
|
|
142
|
+
? mapScoped(scope, expression, commute, adjoin)
|
|
143
|
+
: getModal(builtin, expression);
|
|
144
|
+
};
|
|
145
|
+
return commute;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const COMMUTERS: Partial<Record<Builtin, Rewrite>> = {
|
|
149
|
+
Prime: commutePrime,
|
|
150
|
+
BoxTrue: getModalCommuter("BoxTrue"),
|
|
151
|
+
Diamond: getModalCommuter("Diamond"),
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// symbol_commute: each prime, [] and <> commutes down its operand.
|
|
155
|
+
const commuteSymbols: Rewrite = (scope, expression) => {
|
|
156
|
+
if (expression.kind === "Apply" && expression.operator.kind === "Internal") {
|
|
157
|
+
const commuter = COMMUTERS[expression.operator.builtin];
|
|
158
|
+
if (commuter) {
|
|
159
|
+
if (expression.args.length !== 1) throw new TlapmBugError();
|
|
160
|
+
return commuter(scope, expression.args[0]);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return mapScoped(scope, expression, commuteSymbols, adjoin, "all");
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const LEVEL_KINDS = ["CONSTANT", "STATE", "ACTION", "TEMPORAL"];
|
|
167
|
+
|
|
168
|
+
const FRESH_KINDS: Record<Level, string> = {
|
|
169
|
+
Constant: "CONSTANT",
|
|
170
|
+
State: "STATE",
|
|
171
|
+
Action: "ACTION",
|
|
172
|
+
Temporal: "TEMPORAL",
|
|
173
|
+
Unknown: "Unknown",
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const getRenamed = (kind: string, name: string) => `${kind}_${name}_`;
|
|
177
|
+
|
|
178
|
+
interface RenameState {
|
|
179
|
+
scope: Scope | undefined;
|
|
180
|
+
original: Scope | undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// rename_with_loc: each declaration's name tells its level, as in CONSTANT_x_ or VARIABLE_x_. A
|
|
184
|
+
// definition's level is the one its body had before the commuters ran, unless given.
|
|
185
|
+
const renameDefn = (scope: Scope | undefined, definition: Defn, level?: number): Defn => {
|
|
186
|
+
if (definition.kind !== "Operator") {
|
|
187
|
+
return definition.kind === "Bpragma"
|
|
188
|
+
? { ...definition, body: rename(scope, definition.body) }
|
|
189
|
+
: definition;
|
|
190
|
+
}
|
|
191
|
+
const body = rename(scope, definition.body);
|
|
192
|
+
const kind = LEVEL_KINDS[level ?? getLevel(scope, body)];
|
|
193
|
+
if (kind === undefined) throw new TlapmBugError();
|
|
194
|
+
return { ...definition, name: getRenamed(kind, definition.name), body };
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const renameHyp = (scope: Scope | undefined, hyp: Hyp, level?: number): Hyp => {
|
|
198
|
+
switch (hyp.kind) {
|
|
199
|
+
case "Fresh":
|
|
200
|
+
return {
|
|
201
|
+
...hyp,
|
|
202
|
+
name: getRenamed(FRESH_KINDS[hyp.level], hyp.name),
|
|
203
|
+
domain: hyp.domain && rename(scope, hyp.domain),
|
|
204
|
+
};
|
|
205
|
+
case "Flex":
|
|
206
|
+
return { ...hyp, name: getRenamed("VARIABLE", hyp.name) };
|
|
207
|
+
case "Defn":
|
|
208
|
+
return { ...hyp, definition: renameDefn(scope, hyp.definition, level) };
|
|
209
|
+
case "Fact":
|
|
210
|
+
return { ...hyp, expression: rename(scope, hyp.expression) };
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
// tlapm reads levels from properties that Expr.Levels computes before the frontend rewrites
|
|
215
|
+
// anything, and each node it builds takes the properties of a node it replaces: UNCHANGED's
|
|
216
|
+
// equalities those of the tuple or its only element, and a commuted prime, [] or <> those of its
|
|
217
|
+
// operand. A definition's name tells the level its body's top node ends up with.
|
|
218
|
+
const getOriginalLevel = (scope: Scope | undefined, hyp: Hyp) => {
|
|
219
|
+
if (hyp.kind !== "Defn" || hyp.definition.kind !== "Operator") return undefined;
|
|
220
|
+
const getNodeLevel = (expression: Expr) => {
|
|
221
|
+
if (!isBuiltin(expression, ["UNCHANGED"]) || expression.kind !== "Apply") {
|
|
222
|
+
return getLevel(scope, expression);
|
|
223
|
+
}
|
|
224
|
+
const flatten = (inner: Expr): Expr[] =>
|
|
225
|
+
inner.kind === "Tuple" ? inner.elements.flatMap(flatten) : [inner];
|
|
226
|
+
const items = flatten(expression.args[0]);
|
|
227
|
+
return getLevel(scope, items.length === 1 ? items[0] : expression.args[0]);
|
|
228
|
+
};
|
|
229
|
+
const body = hyp.definition.body;
|
|
230
|
+
const isCommuted =
|
|
231
|
+
isBuiltin(body, ["Prime", "BoxTrue", "Diamond"]) &&
|
|
232
|
+
body.kind === "Apply" &&
|
|
233
|
+
body.args.length === 1;
|
|
234
|
+
return getNodeLevel(isCommuted && body.kind === "Apply" ? body.args[0] : body);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// renameSequent on the obligation, with the levels of its definitions from before commuting.
|
|
238
|
+
const renameContext = createContextMap<RenameState>(
|
|
239
|
+
{ scope: undefined, original: undefined },
|
|
240
|
+
(current, hyp, original) => {
|
|
241
|
+
const renamed = renameHyp(current.scope, hyp, getOriginalLevel(current.original, original));
|
|
242
|
+
const next = {
|
|
243
|
+
scope: adjoin(current.scope, renamed),
|
|
244
|
+
original: adjoin(current.original, original),
|
|
245
|
+
};
|
|
246
|
+
return [next, renamed];
|
|
247
|
+
},
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
const renameObligation = (commuted: Sequent, original: Sequent): Sequent => {
|
|
251
|
+
const renamed = renameContext(original.context, commuted.context);
|
|
252
|
+
return { context: renamed.context, active: rename(renamed.state.scope, commuted.active) };
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const renameSequent = (scope: Scope | undefined, sequent: Sequent): Sequent => {
|
|
256
|
+
const [inner, context] = mapAccumulate(scope, sequent.context, (current, hyp) => {
|
|
257
|
+
const renamed = renameHyp(current, hyp);
|
|
258
|
+
return [adjoin(current, renamed), renamed];
|
|
259
|
+
});
|
|
260
|
+
return { context, active: rename(inner, sequent.active) };
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const rename: Rewrite = (scope, expression) => {
|
|
264
|
+
switch (expression.kind) {
|
|
265
|
+
case "Lambda": {
|
|
266
|
+
const params = expression.params.map((param) => ({
|
|
267
|
+
...param,
|
|
268
|
+
name: getRenamed("Unknown", param.name),
|
|
269
|
+
}));
|
|
270
|
+
const inner = adjoinAll(scope, params.map(getParamFresh));
|
|
271
|
+
return { ...expression, params, body: rename(inner, expression.body) };
|
|
272
|
+
}
|
|
273
|
+
case "Tquant": {
|
|
274
|
+
const inner = adjoinAll(
|
|
275
|
+
scope,
|
|
276
|
+
expression.names.map((name) => ({ kind: "Flex", name: getRenamed("VARIABLE", name) })),
|
|
277
|
+
);
|
|
278
|
+
return { ...expression, body: rename(inner, expression.body) };
|
|
279
|
+
}
|
|
280
|
+
case "Choose": {
|
|
281
|
+
const domain = expression.domain && rename(scope, expression.domain);
|
|
282
|
+
const name = getRenamed("CONSTANT", expression.name);
|
|
283
|
+
const hyp = domain ? getBoundedFresh(name, domain) : getFresh(name, "Constant");
|
|
284
|
+
return { ...expression, name, domain, body: rename(adjoin(scope, hyp), expression.body) };
|
|
285
|
+
}
|
|
286
|
+
case "SetSt": {
|
|
287
|
+
const domain = rename(scope, expression.domain);
|
|
288
|
+
const name = getRenamed("CONSTANT", expression.name);
|
|
289
|
+
const body = rename(adjoin(scope, getBoundedFresh(name, domain)), expression.body);
|
|
290
|
+
return { ...expression, name, domain, body };
|
|
291
|
+
}
|
|
292
|
+
case "Quant":
|
|
293
|
+
case "Fcn":
|
|
294
|
+
case "SetOf": {
|
|
295
|
+
const bounds = expression.bounds.map((bound) => ({
|
|
296
|
+
...bound,
|
|
297
|
+
name: getRenamed(FRESH_KINDS[bound.level], bound.name),
|
|
298
|
+
domain:
|
|
299
|
+
bound.domain.kind === "Domain"
|
|
300
|
+
? { kind: "Domain" as const, expression: rename(scope, bound.domain.expression) }
|
|
301
|
+
: bound.domain,
|
|
302
|
+
}));
|
|
303
|
+
const inner = adjoinAll(scope, bounds.map(getBoundFresh));
|
|
304
|
+
return expression.kind === "SetOf"
|
|
305
|
+
? { ...expression, bounds, element: rename(inner, expression.element) }
|
|
306
|
+
: { ...expression, bounds, body: rename(inner, expression.body) };
|
|
307
|
+
}
|
|
308
|
+
case "Let": {
|
|
309
|
+
const [inner, definitions] = mapAccumulate(
|
|
310
|
+
scope,
|
|
311
|
+
expression.definitions,
|
|
312
|
+
(current, definition) => {
|
|
313
|
+
const renamed = renameDefn(current, definition);
|
|
314
|
+
return [adjoin(current, getLocalDefn(renamed)), renamed];
|
|
315
|
+
},
|
|
316
|
+
);
|
|
317
|
+
return { ...expression, definitions, body: rename(inner, expression.body) };
|
|
318
|
+
}
|
|
319
|
+
case "Sequent":
|
|
320
|
+
return { ...expression, sequent: renameSequent(scope, expression.sequent) };
|
|
321
|
+
default:
|
|
322
|
+
return mapScoped(scope, expression, rename, adjoin, "all");
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// E_substitutive.make_array: for each parameter of an operator, counting from the last, whether
|
|
327
|
+
// the body uses it only outside primes and the other modal operators.
|
|
328
|
+
const substitutiveArgs = new WeakMap<Defn, boolean[]>();
|
|
329
|
+
|
|
330
|
+
const getSubstitutiveArgs = (scope: Scope | undefined, definition: Defn): boolean[] => {
|
|
331
|
+
const cached = substitutiveArgs.get(definition);
|
|
332
|
+
if (cached) return cached;
|
|
333
|
+
const body = definition.kind === "Operator" ? definition.body : undefined;
|
|
334
|
+
if (body?.kind !== "Lambda") return [];
|
|
335
|
+
const inner = adjoinAll(scope, body.params.map(getParamFresh));
|
|
336
|
+
const result = body.params.map((_param, index) =>
|
|
337
|
+
isSubstitutive(inner, body.body, index + 1, true),
|
|
338
|
+
);
|
|
339
|
+
substitutiveArgs.set(definition, result);
|
|
340
|
+
return result;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const UNARY_MODALS: Builtin[] = [
|
|
344
|
+
"Prime",
|
|
345
|
+
"StrongPrime",
|
|
346
|
+
"BoxTrue",
|
|
347
|
+
"BoxFalse",
|
|
348
|
+
"Diamond",
|
|
349
|
+
"ENABLED",
|
|
350
|
+
"UNCHANGED",
|
|
351
|
+
];
|
|
352
|
+
|
|
353
|
+
const BINARY_MODALS: Builtin[] = ["Leadsto", "Actplus", "Cdot"];
|
|
354
|
+
|
|
355
|
+
// compute_substitutivity_array: whether the parameter at index occurs only where scope holds.
|
|
356
|
+
const isSubstitutive = (
|
|
357
|
+
scope: Scope | undefined,
|
|
358
|
+
expression: Expr,
|
|
359
|
+
index: number,
|
|
360
|
+
isInScope: boolean,
|
|
361
|
+
): boolean => {
|
|
362
|
+
if (expression.kind === "Ix") return isInScope || expression.index !== index;
|
|
363
|
+
if (expression.kind === "Apply" && expression.operator.kind === "Internal") {
|
|
364
|
+
const builtin = expression.operator.builtin;
|
|
365
|
+
if (
|
|
366
|
+
(UNARY_MODALS.includes(builtin) && expression.args.length === 1) ||
|
|
367
|
+
(BINARY_MODALS.includes(builtin) && expression.args.length === 2)
|
|
368
|
+
) {
|
|
369
|
+
return expression.args.every((arg) => isSubstitutive(scope, arg, index, false));
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (expression.kind === "Sub") {
|
|
373
|
+
return [expression.action, expression.subscript].every((inner) =>
|
|
374
|
+
isSubstitutive(scope, inner, index, false),
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
if (expression.kind === "Apply" && expression.operator.kind === "Ix") {
|
|
378
|
+
const hypScope = getScopeAt(scope, expression.operator.index - 1);
|
|
379
|
+
const hyp = hypScope?.hyp;
|
|
380
|
+
if (hypScope && hyp?.kind === "Defn" && hyp.definition.kind === "Operator") {
|
|
381
|
+
const args = getSubstitutiveArgs(hypScope.rest, hyp.definition);
|
|
382
|
+
return expression.args.every((arg, position) => {
|
|
383
|
+
const isArgInScope = args[position];
|
|
384
|
+
if (isArgInScope === undefined) throw new TlapmBugError();
|
|
385
|
+
return isSubstitutive(scope, arg, index, isArgInScope);
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
const found = { isSubstitutive: true };
|
|
390
|
+
mapScoped(
|
|
391
|
+
{ scope, index },
|
|
392
|
+
expression,
|
|
393
|
+
(state, inner) => {
|
|
394
|
+
found.isSubstitutive &&= isSubstitutive(state.scope, inner, state.index, isInScope);
|
|
395
|
+
return inner;
|
|
396
|
+
},
|
|
397
|
+
(state, hyp) => ({ scope: adjoin(state.scope, hyp), index: state.index + 1 }),
|
|
398
|
+
"all",
|
|
399
|
+
);
|
|
400
|
+
return found.isSubstitutive;
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
// Coalesce.crypthash: h and the first five hexadecimal digits of the MD5 of the expression as
|
|
404
|
+
// tlapm prints it.
|
|
405
|
+
const getHashName = (scope: Scope | undefined, expression: Expr) => {
|
|
406
|
+
const text = renderDoc(printExpression(getScopeHyps(scope), expression));
|
|
407
|
+
return `h${getMd5(text).slice(0, 5)}`;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
const coalesce = (scope: Scope | undefined, expression: Expr): Expr =>
|
|
411
|
+
at({ kind: "Opaque", name: getHashName(scope, expression) }, expression);
|
|
412
|
+
|
|
413
|
+
// coalesce_operator: an operator applied to arguments that are not all substitutive is opaque,
|
|
414
|
+
// hiding the arguments that are constant or substitutive.
|
|
415
|
+
const coalesceOperator = (
|
|
416
|
+
scope: Scope | undefined,
|
|
417
|
+
name: string,
|
|
418
|
+
substitutive: boolean[],
|
|
419
|
+
from: Expr,
|
|
420
|
+
args: Expr[],
|
|
421
|
+
) => {
|
|
422
|
+
const starred = args.map((arg, position) => {
|
|
423
|
+
const isHidden = getLevel(scope, arg) === 0 || substitutive[args.length - position - 1];
|
|
424
|
+
return isHidden ? at({ kind: "Opaque", name: "*" }, arg) : arg;
|
|
425
|
+
});
|
|
426
|
+
return coalesce(
|
|
427
|
+
scope,
|
|
428
|
+
at({ kind: "Apply", operator: { kind: "Opaque", name }, args: starred }, from),
|
|
429
|
+
);
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
const MODAL_BUILTINS: Builtin[] = [
|
|
433
|
+
"Prime",
|
|
434
|
+
"StrongPrime",
|
|
435
|
+
"BoxTrue",
|
|
436
|
+
"BoxFalse",
|
|
437
|
+
"Diamond",
|
|
438
|
+
"Leadsto",
|
|
439
|
+
"ENABLED",
|
|
440
|
+
"Cdot",
|
|
441
|
+
"UNCHANGED",
|
|
442
|
+
];
|
|
443
|
+
|
|
444
|
+
// coalesce_modal_visitor: a primed declaration becomes the opaque name#prime, and every other
|
|
445
|
+
// modal expression an opaque name. tlapm means to apply that name to the expression's free
|
|
446
|
+
// variables, but compute_vars collects them into an instance variable and returns its own empty
|
|
447
|
+
// set, so the name always stands alone.
|
|
448
|
+
const coalesceModal: Rewrite = (scope, expression) => {
|
|
449
|
+
if (expression.kind !== "Apply") return mapScoped(scope, expression, coalesceModal, adjoin);
|
|
450
|
+
const operator = expression.operator;
|
|
451
|
+
const [arg] = expression.args;
|
|
452
|
+
if (
|
|
453
|
+
isBuiltin(expression, ["Prime", "StrongPrime"]) &&
|
|
454
|
+
expression.args.length === 1 &&
|
|
455
|
+
arg.kind === "Ix"
|
|
456
|
+
) {
|
|
457
|
+
const hyp = getHyp(scope, arg.index);
|
|
458
|
+
if (hyp.kind === "Fresh" || hyp.kind === "Flex") {
|
|
459
|
+
return at({ kind: "Opaque", name: `${hyp.name}#prime` }, expression);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (isBuiltin(expression, MODAL_BUILTINS)) return coalesce(scope, expression);
|
|
463
|
+
if (operator.kind !== "Ix") return mapScoped(scope, expression, coalesceModal, adjoin);
|
|
464
|
+
const hypScope = getScopeAt(scope, operator.index - 1);
|
|
465
|
+
const hyp = hypScope?.hyp;
|
|
466
|
+
if (!hyp) throw new TlapmBugError();
|
|
467
|
+
const args = expression.args;
|
|
468
|
+
if (hyp.kind === "Defn" && hyp.definition.kind === "Operator") {
|
|
469
|
+
const substitutive = getSubstitutiveArgs(hypScope.rest, hyp.definition);
|
|
470
|
+
const isKept = args.every(
|
|
471
|
+
(inner, position) => getLevel(scope, inner) === 0 || substitutive[args.length - position - 1],
|
|
472
|
+
);
|
|
473
|
+
if (isKept) return mapScoped(scope, expression, coalesceModal, adjoin);
|
|
474
|
+
const opaque = coalesceOperator(scope, hyp.definition.name, substitutive, operator, args);
|
|
475
|
+
return at(
|
|
476
|
+
{ kind: "Apply", operator: opaque, args: args.map((inner) => coalesceModal(scope, inner)) },
|
|
477
|
+
expression,
|
|
478
|
+
);
|
|
479
|
+
}
|
|
480
|
+
if (hyp.kind === "Flex") return mapScoped(scope, expression, coalesceModal, adjoin);
|
|
481
|
+
if (hyp.kind === "Fresh") {
|
|
482
|
+
if (hyp.level === "Constant") {
|
|
483
|
+
return at(
|
|
484
|
+
{ kind: "Apply", operator, args: args.map((inner) => coalesceModal(scope, inner)) },
|
|
485
|
+
expression,
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
const notSubstitutive = args.map(() => false);
|
|
489
|
+
return coalesceOperator(scope, hyp.name, notSubstitutive, operator, args);
|
|
490
|
+
}
|
|
491
|
+
throw new TlapmBugError();
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// process_eob, on an obligation's sequent.
|
|
495
|
+
const expandActionSequent = createSequentMap(expandAction, "all");
|
|
496
|
+
|
|
497
|
+
const expandUnchangedSequent = createSequentMap(expandUnchanged, "all");
|
|
498
|
+
|
|
499
|
+
const commuteSequent = createSequentMap(commuteSymbols, "all");
|
|
500
|
+
|
|
501
|
+
const coalesceSequent = createSequentMap(coalesceModal, "visible");
|
|
502
|
+
|
|
503
|
+
export const processObligation = (sequent: Sequent): Sequent => {
|
|
504
|
+
const keys = sequent.context;
|
|
505
|
+
const expanded = expandUnchangedSequent(keys, expandActionSequent(keys, sequent));
|
|
506
|
+
const commuted = commuteSequent(keys, expanded);
|
|
507
|
+
return coalesceSequent(keys, renameObligation(commuted, sequent));
|
|
508
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bumpBy,
|
|
3
|
+
consSubstitution,
|
|
4
|
+
createContextMap,
|
|
5
|
+
shift,
|
|
6
|
+
substituteExpr,
|
|
7
|
+
substituteHyp,
|
|
8
|
+
type Sequent,
|
|
9
|
+
} from "../../obligations/expression";
|
|
10
|
+
|
|
11
|
+
const createExpander = (isPragmaExpanded: boolean) =>
|
|
12
|
+
createContextMap(shift(0), (current, hyp) => {
|
|
13
|
+
if (
|
|
14
|
+
hyp.kind === "Defn" &&
|
|
15
|
+
((isPragmaExpanded && hyp.definition.kind === "Bpragma") ||
|
|
16
|
+
(hyp.definition.kind === "Operator" && hyp.visibility === "Visible"))
|
|
17
|
+
) {
|
|
18
|
+
return [consSubstitution(substituteExpr(current, hyp.definition.body), current), undefined];
|
|
19
|
+
}
|
|
20
|
+
return [bumpBy(1, current), substituteHyp(current, hyp)];
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const expandWithPragmas = createExpander(true);
|
|
24
|
+
|
|
25
|
+
const expandOperators = createExpander(false);
|
|
26
|
+
|
|
27
|
+
// expand_defs: each visible operator definition and each pragma, whatever its visibility,
|
|
28
|
+
// substitutes into the rest of the obligation and leaves the context. Toolbox.expand_defs leaves
|
|
29
|
+
// the pragmas.
|
|
30
|
+
export const expandDefs = (sequent: Sequent, isPragmaExpanded = true): Sequent => {
|
|
31
|
+
const expanded = (isPragmaExpanded ? expandWithPragmas : expandOperators)(sequent.context);
|
|
32
|
+
return { context: expanded.context, active: substituteExpr(expanded.state, sequent.active) };
|
|
33
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// A port of tlapm's Expr.Elab.normalize, which the backends run on each obligation after expanding
|
|
2
|
+
// its definitions: LET definitions are inlined, and the updates of an EXCEPT fold into the
|
|
3
|
+
// function they update.
|
|
4
|
+
|
|
5
|
+
import { TlaProofError } from "../../errors";
|
|
6
|
+
import {
|
|
7
|
+
at,
|
|
8
|
+
consSubstitution,
|
|
9
|
+
createContextMap,
|
|
10
|
+
getChildren,
|
|
11
|
+
isEqualExpr,
|
|
12
|
+
mapChildren,
|
|
13
|
+
mapExceptSpecs,
|
|
14
|
+
mapHyp,
|
|
15
|
+
shift,
|
|
16
|
+
substituteExpr,
|
|
17
|
+
type Expr,
|
|
18
|
+
type ExceptPoint,
|
|
19
|
+
type Hyp,
|
|
20
|
+
type Sequent,
|
|
21
|
+
} from "../../obligations/expression";
|
|
22
|
+
|
|
23
|
+
type ExceptExpr = Extract<Expr, { kind: "Except" }>;
|
|
24
|
+
|
|
25
|
+
type ExceptSpec = ExceptExpr["specs"][number];
|
|
26
|
+
|
|
27
|
+
const TEMPORAL_BUILTINS = new Set(["BoxTrue", "BoxFalse", "Diamond", "Actplus", "Leadsto", "Cdot"]);
|
|
28
|
+
|
|
29
|
+
// The expressions of a hypothesis that Visit.iter visits, none for a hidden one.
|
|
30
|
+
const getVisitedExprs = (hyp: Hyp): Expr[] => {
|
|
31
|
+
switch (hyp.kind) {
|
|
32
|
+
case "Fresh":
|
|
33
|
+
return hyp.domain ? [hyp.domain] : [];
|
|
34
|
+
case "Flex":
|
|
35
|
+
return [];
|
|
36
|
+
case "Defn":
|
|
37
|
+
return hyp.visibility === "Hidden" ||
|
|
38
|
+
(hyp.definition.kind !== "Operator" && hyp.definition.kind !== "Bpragma")
|
|
39
|
+
? []
|
|
40
|
+
: [hyp.definition.body];
|
|
41
|
+
case "Fact":
|
|
42
|
+
return hyp.visibility === "Hidden" ? [] : [hyp.expression];
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// non_temporal: no temporal operator, but in hidden hypotheses.
|
|
47
|
+
const isNonTemporal = (expression: Expr): boolean => {
|
|
48
|
+
if (expression.kind === "Tsub" || expression.kind === "Fair") return false;
|
|
49
|
+
if (
|
|
50
|
+
expression.kind === "Apply" &&
|
|
51
|
+
expression.operator.kind === "Internal" &&
|
|
52
|
+
TEMPORAL_BUILTINS.has(expression.operator.builtin)
|
|
53
|
+
)
|
|
54
|
+
return false;
|
|
55
|
+
if (expression.kind === "Sequent") {
|
|
56
|
+
const sequent = expression.sequent;
|
|
57
|
+
return [...sequent.context.flatMap(getVisitedExprs), sequent.active].every(isNonTemporal);
|
|
58
|
+
}
|
|
59
|
+
return getChildren(expression).every(isNonTemporal);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// let_normalize: each LET definition is substituted into the rest of the LET.
|
|
63
|
+
const normalizeLet = (expression: Expr): Expr => {
|
|
64
|
+
if (expression.kind !== "Let") return mapChildren(expression, normalizeLet);
|
|
65
|
+
const [definition, ...definitions] = expression.definitions;
|
|
66
|
+
if (!definition) return normalizeLet(expression.body);
|
|
67
|
+
if (definition.kind !== "Operator" && definition.kind !== "Bpragma") {
|
|
68
|
+
throw new TlaProofError(
|
|
69
|
+
definition.kind === "Instance"
|
|
70
|
+
? "Found INSTANCE in Expr.Elab.let_normalize, all INSTANCE statements have been replaced with definitions in Module.Elab "
|
|
71
|
+
: "Expr.Elab.let_normalize",
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
const operator = normalizeLet(definition.body);
|
|
75
|
+
const rest: Expr = { ...expression, definitions };
|
|
76
|
+
return normalizeLet(substituteExpr(consSubstitution(operator, shift(0)), rest));
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const getAccess = (base: Expr, point: ExceptPoint): Expr =>
|
|
80
|
+
point.kind === "Dot"
|
|
81
|
+
? at({ kind: "Dot", expression: base, field: point.field }, base)
|
|
82
|
+
: at({ kind: "FcnApp", function: base, args: [point.expression] }, base);
|
|
83
|
+
|
|
84
|
+
const isSameHead = (left: ExceptPoint, right: ExceptPoint) =>
|
|
85
|
+
left.kind === "Dot"
|
|
86
|
+
? right.kind === "Dot" && left.field === right.field
|
|
87
|
+
: right.kind === "Apply" && isEqualExpr(left.expression, right.expression);
|
|
88
|
+
|
|
89
|
+
// The field of a record constructor that a point names, as a field or as a string argument.
|
|
90
|
+
const getRecordField = (record: Expr, point: ExceptPoint) => {
|
|
91
|
+
const name =
|
|
92
|
+
point.kind === "Dot"
|
|
93
|
+
? point.field
|
|
94
|
+
: point.expression.kind === "String"
|
|
95
|
+
? point.expression.value
|
|
96
|
+
: undefined;
|
|
97
|
+
return record.kind === "Record" && name !== undefined
|
|
98
|
+
? record.fields.find((field) => field.name === name)
|
|
99
|
+
: undefined;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// except_normalize: [f EXCEPT p1 = b1, ...] folds left to right into f, sharing each prefix of
|
|
103
|
+
// adjacent updates and updating a record constructor in place on a field it has.
|
|
104
|
+
const normalizeExcept = (expression: Expr): Expr => {
|
|
105
|
+
if (expression.kind !== "Except") return mapChildren(expression, normalizeExcept);
|
|
106
|
+
const except = expression;
|
|
107
|
+
const select = (base: Expr, point: ExceptPoint) =>
|
|
108
|
+
getRecordField(base, point)?.value ?? getAccess(base, point);
|
|
109
|
+
const update = (base: Expr, point: ExceptPoint, value: Expr): Expr => {
|
|
110
|
+
const field = getRecordField(base, point);
|
|
111
|
+
if (base.kind !== "Record" || !field)
|
|
112
|
+
return at({ kind: "Except", function: base, specs: [{ trail: [point], value }] }, except);
|
|
113
|
+
const fields = base.fields.map((entry) =>
|
|
114
|
+
entry.name === field.name ? { name: entry.name, value } : entry,
|
|
115
|
+
);
|
|
116
|
+
return at({ kind: "Record", fields }, base);
|
|
117
|
+
};
|
|
118
|
+
const apply = (base: Expr, specs: ExceptSpec[]): Expr => {
|
|
119
|
+
const [first, ...others] = specs;
|
|
120
|
+
if (!first) return base;
|
|
121
|
+
if (first.trail.length === 0) return apply(first.value, others);
|
|
122
|
+
const head = first.trail[0];
|
|
123
|
+
const end = specs.findIndex(
|
|
124
|
+
(spec) => spec.trail.length === 0 || !isSameHead(spec.trail[0], head),
|
|
125
|
+
);
|
|
126
|
+
const count = end === -1 ? specs.length : end;
|
|
127
|
+
const tails = specs
|
|
128
|
+
.slice(0, count)
|
|
129
|
+
.map((spec) => ({ trail: spec.trail.slice(1), value: spec.value }));
|
|
130
|
+
return apply(update(base, head, apply(select(base, head), tails)), specs.slice(count));
|
|
131
|
+
};
|
|
132
|
+
return apply(normalizeExcept(except.function), mapExceptSpecs(except.specs, normalizeExcept));
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// let_normalize on each hypothesis, and whether the context so far is non_temporal.
|
|
136
|
+
const normalizeLetContext = createContextMap(true, (isContextNonTemporal, hyp) => [
|
|
137
|
+
isContextNonTemporal && getVisitedExprs(hyp).every(isNonTemporal),
|
|
138
|
+
mapHyp(hyp, normalizeLet, "all"),
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
const normalizeExceptContext = createContextMap(undefined, (_state, hyp) => [
|
|
142
|
+
undefined,
|
|
143
|
+
mapHyp(hyp, normalizeExcept, "all"),
|
|
144
|
+
]);
|
|
145
|
+
|
|
146
|
+
// normalize, on an obligation as a sequent expression, as Backend.Prep.normalize_expr calls it.
|
|
147
|
+
export const normalizeSequent = (sequent: Sequent): Sequent => {
|
|
148
|
+
const inlined = normalizeLetContext(sequent.context);
|
|
149
|
+
const active = normalizeLet(sequent.active);
|
|
150
|
+
return inlined.state && isNonTemporal(sequent.active)
|
|
151
|
+
? {
|
|
152
|
+
context: normalizeExceptContext(sequent.context, inlined.context).context,
|
|
153
|
+
active: normalizeExcept(active),
|
|
154
|
+
}
|
|
155
|
+
: { context: inlined.context, active };
|
|
156
|
+
};
|