tla-ts 0.0.9 → 0.0.10-dev.b4a856304
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 +29 -1
- package/dist/compile.cjs +23 -0
- package/dist/compile.js +23 -0
- package/dist/deciders.cjs +28 -28
- package/dist/deciders.js +43 -43
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +72 -10
- package/dist/index.d.ts +72 -10
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +1 -1
- package/dist/parallel-worker.js +1 -1
- package/dist/prove-worker.cjs +1 -1
- package/dist/prove-worker.js +1 -1
- package/dist/prove.cjs +116 -36
- package/dist/prove.js +116 -36
- package/dist/run.cjs +143 -31
- package/dist/run.js +143 -31
- package/dist/tlapm.cjs +2 -2
- package/dist/tlapm.js +2 -2
- package/dist/transfer.cjs +128 -103
- package/dist/transfer.js +128 -103
- package/dist/worker.cjs +7 -7
- package/dist/worker.js +7 -7
- package/package.json +3 -1
- package/src/ast.ts +7 -21
- package/src/bags.ts +99 -99
- package/src/builtins.ts +44 -75
- package/src/bundled-modules.ts +1 -8
- package/src/check.ts +576 -300
- package/src/cli/options.ts +11 -6
- package/src/cli/run.ts +2 -2
- package/src/cli/runtime-invariants.ts +135 -0
- package/src/cli/shared.ts +20 -9
- package/src/cli/tlapm.ts +0 -12
- package/src/cli/tlc-arguments.ts +646 -0
- package/src/cli/tlc-usage.ts +362 -0
- package/src/cli/tlc.ts +538 -220
- package/src/cli/typescript.ts +23 -0
- package/src/cli/xml-exporter.ts +4 -16
- package/src/cli.ts +0 -1
- package/src/codegen.ts +191 -64
- package/src/config.ts +140 -109
- package/src/coverage.ts +837 -0
- package/src/dependencies.ts +6 -40
- package/src/dfid.ts +618 -0
- package/src/environment.ts +240 -118
- package/src/errors.ts +70 -55
- package/src/evaluate.ts +630 -297
- package/src/exit-codes.ts +37 -3
- package/src/fingerprints.ts +10 -10
- package/src/index.ts +3 -0
- package/src/instances.ts +79 -11
- package/src/java-overrides.ts +2 -12
- package/src/json-reader.ts +456 -0
- package/src/json.ts +262 -139
- package/src/levels.ts +2 -3
- package/src/lexer.ts +17 -26
- package/src/liveness-checker.ts +10 -18
- package/src/liveness.ts +121 -49
- package/src/obligations/action.ts +4 -28
- package/src/obligations/anonymize.ts +84 -80
- package/src/obligations/equality.ts +1 -1
- package/src/obligations/expression.ts +2 -19
- package/src/obligations/format.ts +1 -9
- package/src/obligations/generate.ts +1 -11
- package/src/obligations/index.ts +0 -11
- package/src/obligations/levels.ts +4 -8
- package/src/obligations/module.ts +1 -15
- package/src/obligations/normalize.ts +5 -24
- package/src/obligations/operators.ts +1 -2
- package/src/obligations/pragma.ts +1 -10
- package/src/obligations/printer.ts +47 -46
- package/src/obligations/proof.ts +1 -5
- package/src/obligations/regroup.ts +1 -4
- package/src/obligations/simplify.ts +3 -14
- package/src/obligations/source.ts +1 -21
- package/src/obligations/subexpression.ts +5 -11
- package/src/obligations/substitution.ts +1 -3
- package/src/obligations/temporal.ts +8 -14
- package/src/obligations/tuples.ts +3 -9
- package/src/parallel-worker.ts +29 -39
- package/src/parallel.ts +29 -13
- package/src/parser.ts +436 -90
- package/src/pcal/ast.ts +7 -28
- package/src/pcal/parse.ts +11 -39
- package/src/pcal/tokenize.ts +4 -20
- package/src/pcal/translate.ts +10 -42
- package/src/pcal/validate.ts +3 -21
- package/src/pretty-print.ts +1 -2
- package/src/prover/decide-pool.ts +5 -10
- package/src/prover/deciders.ts +0 -4
- package/src/prover/first-order.ts +0 -11
- package/src/prover/isabelle/decide.ts +0 -6
- package/src/prover/isabelle/encode.ts +11 -23
- package/src/prover/isabelle/isar/inner-syntax.ts +108 -114
- package/src/prover/isabelle/isar/method.ts +25 -38
- package/src/prover/isabelle/isar/proof.ts +1 -10
- package/src/prover/isabelle/isar/statement.ts +1 -6
- package/src/prover/isabelle/isar/theory.ts +15 -26
- package/src/prover/isabelle/isar/type-check.ts +11 -25
- package/src/prover/isabelle/provers/blast/blast.ts +10 -41
- package/src/prover/isabelle/provers/blast/branch.ts +1 -32
- package/src/prover/isabelle/provers/blast/prove.ts +1 -35
- package/src/prover/isabelle/provers/blast/rules.ts +6 -44
- package/src/prover/isabelle/provers/blast/state.ts +2 -28
- package/src/prover/isabelle/provers/blast/term.ts +9 -36
- package/src/prover/isabelle/provers/clasimp.ts +1 -13
- package/src/prover/isabelle/provers/classical.ts +14 -42
- package/src/prover/isabelle/provers/hypsubst.ts +11 -25
- package/src/prover/isabelle/pure/bicompose.ts +16 -13
- package/src/prover/isabelle/pure/consts.ts +1 -7
- package/src/prover/isabelle/pure/conv.ts +1 -9
- package/src/prover/isabelle/pure/drule.ts +24 -40
- package/src/prover/isabelle/pure/envir.ts +3 -52
- package/src/prover/isabelle/pure/goal.ts +1 -7
- package/src/prover/isabelle/pure/interrupt.ts +0 -5
- package/src/prover/isabelle/pure/list.ts +0 -3
- package/src/prover/isabelle/pure/logic.ts +1 -14
- package/src/prover/isabelle/pure/name.ts +2 -9
- package/src/prover/isabelle/pure/net.ts +3 -59
- package/src/prover/isabelle/pure/object-logic.ts +3 -10
- package/src/prover/isabelle/pure/pattern.ts +2 -19
- package/src/prover/isabelle/pure/raw-simplifier.ts +13 -49
- package/src/prover/isabelle/pure/rewriter.ts +26 -58
- package/src/prover/isabelle/pure/search.ts +3 -10
- package/src/prover/isabelle/pure/seq.ts +1 -10
- package/src/prover/isabelle/pure/simplifier.ts +4 -12
- package/src/prover/isabelle/pure/table.ts +1 -31
- package/src/prover/isabelle/pure/tactic.ts +12 -22
- package/src/prover/isabelle/pure/tactical.ts +6 -12
- package/src/prover/isabelle/pure/term-ord.ts +2 -6
- package/src/prover/isabelle/pure/term.ts +2 -43
- package/src/prover/isabelle/pure/thm.ts +13 -36
- package/src/prover/isabelle/pure/type.ts +2 -15
- package/src/prover/isabelle/pure/unify.ts +20 -30
- package/src/prover/isabelle/pure/variable.ts +3 -7
- package/src/prover/isabelle/tla/classical-setup.ts +9 -19
- package/src/prover/isabelle/tla/context.ts +8 -16
- package/src/prover/isabelle/tla/logic.ts +4 -11
- package/src/prover/isabelle/tla/simpdata.ts +46 -34
- package/src/prover/isabelle/tools/intuitionistic.ts +1 -10
- package/src/prover/ocaml.ts +0 -6
- package/src/prover/prep/action.ts +4 -36
- package/src/prover/prep/enabled-axioms.ts +12 -19
- package/src/prover/prep/fingerprint.ts +0 -16
- package/src/prover/prep/level-comparison.ts +8 -12
- package/src/prover/prep/md5.ts +0 -2
- package/src/prover/prep/methods.ts +9 -36
- package/src/prover/prep/normalize.ts +4 -28
- package/src/prover/prove-worker.ts +0 -2
- package/src/prover/prove.ts +0 -36
- package/src/prover/ptl/decide.ts +7 -12
- package/src/prover/ptl/encode.ts +14 -36
- package/src/prover/ptl/sat.ts +24 -32
- package/src/prover/ptl/tableau.ts +12 -26
- package/src/prover/smt/axioms.ts +0 -7
- package/src/prover/smt/context.ts +19 -26
- package/src/prover/smt/data.ts +51 -54
- package/src/prover/smt/encode.ts +14 -35
- package/src/prover/smt/extensionality.ts +0 -10
- package/src/prover/smt/flatten.ts +3 -22
- package/src/prover/smt/format.ts +5 -10
- package/src/prover/smt/instances.ts +0 -10
- package/src/prover/smt/rewrite.ts +66 -101
- package/src/prover/smt/solver/act-cache.ts +0 -15
- package/src/prover/smt/solver/arith-rewriter.ts +3 -48
- package/src/prover/smt/solver/array.ts +1 -3
- package/src/prover/smt/solver/asserted-formulas.ts +1 -43
- package/src/prover/smt/solver/ast-lt.ts +1 -11
- package/src/prover/smt/solver/ast-util.ts +1 -6
- package/src/prover/smt/solver/ast.ts +1 -143
- package/src/prover/smt/solver/auf-solver.ts +6 -39
- package/src/prover/smt/solver/case-split-queue.ts +1 -9
- package/src/prover/smt/solver/cg-table.ts +1 -18
- package/src/prover/smt/solver/clause.ts +1 -27
- package/src/prover/smt/solver/conflict-resolution.ts +0 -9
- package/src/prover/smt/solver/context.ts +4 -61
- package/src/prover/smt/solver/ctx-simplify.ts +14 -41
- package/src/prover/smt/solver/der.ts +2 -21
- package/src/prover/smt/solver/dyn-ack.ts +1 -11
- package/src/prover/smt/solver/e-matching.ts +3 -34
- package/src/prover/smt/solver/enode.ts +1 -21
- package/src/prover/smt/solver/expr-ref.ts +1 -5
- package/src/prover/smt/solver/expr-safe-replace.ts +0 -5
- package/src/prover/smt/solver/fingerprints.ts +1 -12
- package/src/prover/smt/solver/flat-literals.ts +1 -9
- package/src/prover/smt/solver/goal.ts +2 -14
- package/src/prover/smt/solver/hash.ts +0 -8
- package/src/prover/smt/solver/hashtable.ts +1 -12
- package/src/prover/smt/solver/internalizer.ts +6 -98
- package/src/prover/smt/solver/lbool.ts +1 -2
- package/src/prover/smt/solver/literal.ts +0 -3
- package/src/prover/smt/solver/lp/bound-propagator.ts +8 -26
- package/src/prover/smt/solver/lp/constraints.ts +2 -8
- package/src/prover/smt/solver/lp/containers.ts +12 -30
- package/src/prover/smt/solver/lp/core-solver.ts +10 -31
- package/src/prover/smt/solver/lp/cross-nested.ts +6 -20
- package/src/prover/smt/solver/lp/dep-intervals.ts +7 -22
- package/src/prover/smt/solver/lp/dependency.ts +1 -4
- package/src/prover/smt/solver/lp/dioph-eq.ts +12 -98
- package/src/prover/smt/solver/lp/explanation.ts +1 -6
- package/src/prover/smt/solver/lp/gomory.ts +9 -22
- package/src/prover/smt/solver/lp/hnf-cutter.ts +17 -23
- package/src/prover/smt/solver/lp/hnf.ts +11 -26
- package/src/prover/smt/solver/lp/int-cube.ts +15 -36
- package/src/prover/smt/solver/lp/int-solver.ts +4 -34
- package/src/prover/smt/solver/lp/lar-solver.ts +32 -81
- package/src/prover/smt/solver/lp/lar-term.ts +1 -5
- package/src/prover/smt/solver/lp/lp-settings.ts +1 -14
- package/src/prover/smt/solver/lp/maximize-term.ts +20 -45
- package/src/prover/smt/solver/lp/nex-creator.ts +5 -28
- package/src/prover/smt/solver/lp/nex.ts +1 -6
- package/src/prover/smt/solver/lp/nla-backoff.ts +1 -4
- package/src/prover/smt/solver/lp/nla-basics.ts +5 -30
- package/src/prover/smt/solver/lp/nla-core.ts +35 -130
- package/src/prover/smt/solver/lp/nla-divisions.ts +32 -42
- package/src/prover/smt/solver/lp/nla-emonics.ts +4 -21
- package/src/prover/smt/solver/lp/nla-factorization.ts +1 -4
- package/src/prover/smt/solver/lp/nla-grobner.ts +15 -69
- package/src/prover/smt/solver/lp/nla-horner.ts +3 -11
- package/src/prover/smt/solver/lp/nla-intervals.ts +2 -12
- package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +16 -41
- package/src/prover/smt/solver/lp/nla-monotone.ts +2 -6
- package/src/prover/smt/solver/lp/nla-order.ts +2 -13
- package/src/prover/smt/solver/lp/nla-patcher.ts +4 -7
- package/src/prover/smt/solver/lp/nla-tangents.ts +1 -9
- package/src/prover/smt/solver/lp/nla-throttle.ts +1 -6
- package/src/prover/smt/solver/lp/nla-types.ts +1 -11
- package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -25
- package/src/prover/smt/solver/lp/nra-solver.ts +5 -52
- package/src/prover/smt/solver/lp/numeric-pair.ts +1 -4
- package/src/prover/smt/solver/lp/pdd-interval.ts +1 -3
- package/src/prover/smt/solver/lp/pdd-simplifier.ts +2 -14
- package/src/prover/smt/solver/lp/pdd-solver.ts +4 -33
- package/src/prover/smt/solver/lp/pdd.ts +7 -70
- package/src/prover/smt/solver/lp/random-updater.ts +1 -8
- package/src/prover/smt/solver/lp/static-matrix.ts +9 -19
- package/src/prover/smt/solver/lp/union-find.ts +1 -6
- package/src/prover/smt/solver/mam-compiler.ts +14 -56
- package/src/prover/smt/solver/mam-interpreter.ts +1 -21
- package/src/prover/smt/solver/maximize-ac-sharing.ts +2 -17
- package/src/prover/smt/solver/model-checker.ts +2 -36
- package/src/prover/smt/solver/model-evaluator.ts +3 -29
- package/src/prover/smt/solver/model-finder.ts +3 -15
- package/src/prover/smt/solver/model-generator.ts +1 -12
- package/src/prover/smt/solver/model-macro-solver.ts +12 -69
- package/src/prover/smt/solver/nlsat/algebraic.ts +55 -151
- package/src/prover/smt/solver/nlsat/atoms.ts +5 -19
- package/src/prover/smt/solver/nlsat/evaluator.ts +17 -22
- package/src/prover/smt/solver/nlsat/explain.ts +3 -47
- package/src/prover/smt/solver/nlsat/interval-set.ts +9 -50
- package/src/prover/smt/solver/nlsat/levelwise.ts +48 -129
- package/src/prover/smt/solver/nlsat/mpbq.ts +3 -14
- package/src/prover/smt/solver/nlsat/polynomial.ts +17 -156
- package/src/prover/smt/solver/nlsat/solver.ts +7 -67
- package/src/prover/smt/solver/nlsat/types.ts +1 -8
- package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +1 -44
- package/src/prover/smt/solver/nlsat/upolynomial.ts +11 -92
- package/src/prover/smt/solver/nnf.ts +3 -38
- package/src/prover/smt/solver/pattern-inference.ts +2 -15
- package/src/prover/smt/solver/poly-rewriter.ts +3 -23
- package/src/prover/smt/solver/propagate-values.ts +1 -10
- package/src/prover/smt/solver/proto-model.ts +3 -48
- package/src/prover/smt/solver/pull-quant.ts +1 -12
- package/src/prover/smt/solver/qe-lite-der.ts +1 -29
- package/src/prover/smt/solver/qe-lite-fm.ts +2 -25
- package/src/prover/smt/solver/qe-lite.ts +3 -33
- package/src/prover/smt/solver/qi-queue.ts +1 -16
- package/src/prover/smt/solver/quant-tactics.ts +5 -22
- package/src/prover/smt/solver/quantifier-analyzer.ts +17 -37
- package/src/prover/smt/solver/quantifier-manager.ts +1 -8
- package/src/prover/smt/solver/random-gen.ts +1 -2
- package/src/prover/smt/solver/rational.ts +1 -8
- package/src/prover/smt/solver/ref-vector.ts +1 -6
- package/src/prover/smt/solver/relevancy.ts +2 -15
- package/src/prover/smt/solver/rewriter-tpl.ts +3 -147
- package/src/prover/smt/solver/rewriter.ts +3 -85
- package/src/prover/smt/solver/rlimit.ts +2 -20
- package/src/prover/smt/solver/smtlib-parser.ts +2 -37
- package/src/prover/smt/solver/solver.ts +23 -34
- package/src/prover/smt/solver/sort.ts +1 -5
- package/src/prover/smt/solver/sticky-generations.ts +0 -5
- package/src/prover/smt/solver/theory-lra-axioms.ts +3 -24
- package/src/prover/smt/solver/theory-lra-bounds.ts +0 -5
- package/src/prover/smt/solver/theory-lra-final-check.ts +4 -22
- package/src/prover/smt/solver/theory-lra-internalize.ts +1 -27
- package/src/prover/smt/solver/theory-lra-model.ts +4 -19
- package/src/prover/smt/solver/theory-lra-propagate.ts +1 -15
- package/src/prover/smt/solver/theory-lra-state.ts +3 -8
- package/src/prover/smt/solver/theory-lra.ts +1 -8
- package/src/prover/smt/solver/var-subst.ts +2 -26
- package/src/prover/smt/table.ts +0 -8
- package/src/prover/tlapm/cache.ts +20 -25
- package/src/prover/tlapm/ocaml-marshal.ts +19 -21
- package/src/prover/tlapm/tlapm.ts +12 -17
- package/src/prover/verdict.ts +0 -2
- package/src/prover/zenon/check/check.ts +0 -9
- package/src/prover/zenon/check/definitions.ts +5 -15
- package/src/prover/zenon/check/equality.ts +0 -8
- package/src/prover/zenon/check/functions.ts +0 -4
- package/src/prover/zenon/check/keys.ts +0 -7
- package/src/prover/zenon/check/propositional.ts +0 -3
- package/src/prover/zenon/check/records.ts +0 -5
- package/src/prover/zenon/check/relations.ts +0 -13
- package/src/prover/zenon/check/rewrites.ts +0 -6
- package/src/prover/zenon/check/sets.ts +0 -3
- package/src/prover/zenon/check/terms.ts +0 -17
- package/src/prover/zenon/check/tla.ts +0 -6
- package/src/prover/zenon/decide.ts +0 -12
- package/src/prover/zenon/encode.ts +1 -8
- package/src/prover/zenon/eqrel.ts +0 -13
- package/src/prover/zenon/equality-rules.ts +0 -4
- package/src/prover/zenon/expr.ts +1 -26
- package/src/prover/zenon/index-table.ts +17 -38
- package/src/prover/zenon/isar-case.ts +9 -19
- package/src/prover/zenon/isar.ts +1 -29
- package/src/prover/zenon/limits.ts +0 -4
- package/src/prover/zenon/llproof.ts +0 -9
- package/src/prover/zenon/mltoll.ts +54 -76
- package/src/prover/zenon/node.ts +1 -11
- package/src/prover/zenon/ocaml.ts +1 -17
- package/src/prover/zenon/parse.ts +2 -7
- package/src/prover/zenon/phrase.ts +1 -11
- package/src/prover/zenon/proof.ts +0 -9
- package/src/prover/zenon/propositional-rules.ts +0 -5
- package/src/prover/zenon/rules.ts +2 -6
- package/src/prover/zenon/search.ts +1 -17
- package/src/prover/zenon/tla/functions.ts +57 -29
- package/src/prover/zenon/tla/llproof.ts +63 -70
- package/src/prover/zenon/tla/records.ts +56 -27
- package/src/prover/zenon/tla/rewrites.ts +12 -19
- package/src/prover/zenon/tla/sets.ts +68 -39
- package/src/prover/zenon/tla/terms.ts +0 -6
- package/src/prover/zenon/tla/tla.ts +59 -58
- package/src/randomization.ts +84 -35
- package/src/resolve.ts +18 -60
- package/src/runtime.ts +11 -4
- package/src/sany/builtins.ts +3 -4
- package/src/sany/generator.ts +119 -169
- package/src/sany/levels.ts +6 -30
- package/src/sany/lexer.ts +7 -23
- package/src/sany/linter.ts +1 -8
- package/src/sany/modules.ts +1 -5
- package/src/sany/nodes.ts +0 -31
- package/src/sany/parse-error.ts +39 -117
- package/src/sany/report.ts +29 -63
- package/src/sany/selectors.ts +15 -39
- package/src/sany/source.ts +1 -17
- package/src/sany/xml.ts +13 -63
- package/src/search.ts +494 -154
- package/src/simulate.ts +694 -0
- package/src/spec-description.ts +166 -48
- package/src/standard-definitions.ts +2 -3
- package/src/standard-modules.ts +264 -68
- package/src/state-graph.ts +51 -18
- package/src/subexpressions.ts +31 -5
- package/src/substitution-levels.ts +2 -3
- package/src/successors.ts +267 -77
- package/src/symmetry.ts +31 -8
- package/src/tableau.ts +27 -24
- package/src/tlc/fingerprint.ts +183 -61
- package/src/tlc/jar-data.ts +113 -6
- package/src/tlc/messages.ts +122 -37
- package/src/tlc/order.ts +33 -38
- package/src/tlc/random.ts +16 -16
- package/src/tlc/replay.ts +563 -211
- package/src/tlc/summary.ts +2 -4
- package/src/tlc/tlc-operators.ts +81 -36
- package/src/tlc/tlcext-operators.ts +138 -117
- package/src/tlc/trace-spec.ts +273 -0
- package/src/tlc/value-stream.ts +143 -0
- package/src/tlc/version.ts +2 -4
- package/src/transfer.ts +22 -25
- package/src/typescript/actions.ts +481 -0
- package/src/typescript/collections.ts +600 -0
- package/src/typescript/compile.ts +65 -0
- package/src/typescript/context.ts +94 -0
- package/src/typescript/emit.ts +121 -0
- package/src/typescript/expressions.ts +496 -0
- package/src/typescript/formula.ts +152 -0
- package/src/typescript/functions.ts +284 -0
- package/src/typescript/model.ts +163 -0
- package/src/typescript/names.ts +114 -0
- package/src/typescript/program.ts +48 -0
- package/src/typescript/properties.ts +139 -0
- package/src/typescript/scope.ts +112 -0
- package/src/typescript/steps.ts +165 -0
- package/src/typescript/types.ts +171 -0
- package/src/values.ts +826 -276
- package/src/worker.ts +7 -8
package/src/evaluate.ts
CHANGED
|
@@ -13,9 +13,15 @@ import {
|
|
|
13
13
|
type SourceRange,
|
|
14
14
|
} from "./ast";
|
|
15
15
|
import { getKindString } from "./builtins";
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
isStackOverflow,
|
|
18
|
+
TlaEvaluationError,
|
|
19
|
+
TlaSetBoundError,
|
|
20
|
+
TlaUnassignedError,
|
|
21
|
+
} from "./errors";
|
|
17
22
|
import { EXIT_STATUS, setExitCode } from "./exit-codes";
|
|
18
23
|
import { generateEvaluator } from "./codegen";
|
|
24
|
+
import { countCoverage, getCoverageNode, runAtCoverageNode, type Coverage } from "./coverage";
|
|
19
25
|
import { MESSAGE_CODE } from "./tlc/messages";
|
|
20
26
|
import type { CheckerRuntime } from "./runtime";
|
|
21
27
|
import {
|
|
@@ -24,7 +30,7 @@ import {
|
|
|
24
30
|
isNonconstant,
|
|
25
31
|
type DefinitionFacts,
|
|
26
32
|
} from "./dependencies";
|
|
27
|
-
import { contextualApplications } from "./instances";
|
|
33
|
+
import { contextualApplications, substitutionFrames } from "./instances";
|
|
28
34
|
import { prettyPrint } from "./pretty-print";
|
|
29
35
|
import { isEnabled } from "./successors";
|
|
30
36
|
import {
|
|
@@ -32,12 +38,15 @@ import {
|
|
|
32
38
|
describeMissingArgument,
|
|
33
39
|
compareValues,
|
|
34
40
|
createFunction,
|
|
41
|
+
addBoundedElement,
|
|
35
42
|
createNonIntegerKeyError,
|
|
43
|
+
forEachElement,
|
|
44
|
+
isBeyondSetBound,
|
|
36
45
|
createRecord,
|
|
37
46
|
createEnumeratedSet,
|
|
38
47
|
createSortedFunction,
|
|
39
48
|
createSortedRecord,
|
|
40
|
-
|
|
49
|
+
createNormalizedSet,
|
|
41
50
|
createWrittenRecord,
|
|
42
51
|
createTuple,
|
|
43
52
|
formatValue,
|
|
@@ -47,6 +56,7 @@ import {
|
|
|
47
56
|
getIntervalElements,
|
|
48
57
|
getLazySubsets,
|
|
49
58
|
getStreamedElements,
|
|
59
|
+
normalizeShallowly,
|
|
50
60
|
getFunctionClass,
|
|
51
61
|
getNormalizedSetElements,
|
|
52
62
|
getSequenceElements,
|
|
@@ -81,7 +91,6 @@ export interface OperatorValue {
|
|
|
81
91
|
arity: number;
|
|
82
92
|
call: (args: Value[]) => Value;
|
|
83
93
|
expression?: Expression;
|
|
84
|
-
literalBody?: Value;
|
|
85
94
|
}
|
|
86
95
|
|
|
87
96
|
export type BuiltinArgument = Value | OperatorValue;
|
|
@@ -96,6 +105,8 @@ export interface Builtin {
|
|
|
96
105
|
isStateLevel?: boolean;
|
|
97
106
|
// Whether TLC takes an application for a constant one although its value depends on the state.
|
|
98
107
|
isLevelConstant?: boolean;
|
|
108
|
+
// TLC's minLevel of ActionLevel for a Java override.
|
|
109
|
+
isActionLevel?: boolean;
|
|
99
110
|
isLazy?: boolean;
|
|
100
111
|
}
|
|
101
112
|
|
|
@@ -116,12 +127,17 @@ export interface Thunk {
|
|
|
116
127
|
state: State | undefined;
|
|
117
128
|
next: State | undefined;
|
|
118
129
|
cache: Value | undefined;
|
|
119
|
-
// Whether the
|
|
120
|
-
// caches only a settled value.
|
|
130
|
+
// Whether the value may stream until TLC caches it, so the thunk caches only a settled value.
|
|
121
131
|
isStreamed: boolean;
|
|
132
|
+
// Whether the thunk dropped its scope after caching a value that holds in every state.
|
|
133
|
+
isReleased?: boolean;
|
|
122
134
|
}
|
|
123
135
|
|
|
124
|
-
type Binding =
|
|
136
|
+
type Binding =
|
|
137
|
+
// A literal argument keeps its expression, which TLC binds lazily like any other.
|
|
138
|
+
| { kind: "Value"; value: Value; argument?: Expression }
|
|
139
|
+
| Thunk
|
|
140
|
+
| { kind: "Operator"; operator: Operator };
|
|
125
141
|
|
|
126
142
|
export interface Scope {
|
|
127
143
|
name: string;
|
|
@@ -135,55 +151,44 @@ export interface Environment {
|
|
|
135
151
|
builtins: Map<string, Builtin>;
|
|
136
152
|
constants: Map<string, Value>;
|
|
137
153
|
substitutions: Map<string, string>;
|
|
138
|
-
// The name SANY gives each definition that a named instance such as `M == INSTANCE Module`
|
|
139
|
-
// copies, such as M!Op, by the key of the copy.
|
|
140
154
|
instanceNames: Map<string, string>;
|
|
141
155
|
variables: string[];
|
|
142
156
|
variableIndex: Map<string, number>;
|
|
143
157
|
facts: DefinitionFacts;
|
|
144
|
-
//
|
|
145
|
-
// facts. TLC evaluates a definition it failed to compute where the spec uses it.
|
|
158
|
+
// TLC evaluates a constant definition it failed to compute at startup where the spec uses it.
|
|
146
159
|
startupValues: (Value | undefined)[];
|
|
147
|
-
//
|
|
148
|
-
// wraps the body of each in its substitutions.
|
|
160
|
+
// SANY wraps the body of each of these INSTANCE copies in its substitutions.
|
|
149
161
|
substitutedDefinitions: Set<Definition>;
|
|
150
|
-
|
|
151
|
-
// code can serve, and of each reference to a definition.
|
|
162
|
+
instanceParams: Set<OperatorParam>;
|
|
152
163
|
evaluators: WeakMap<Expression, Evaluator>;
|
|
153
164
|
entryEvaluators: WeakMap<Expression, Evaluator>;
|
|
154
165
|
definitionEvaluators: WeakMap<Definition, Evaluator>;
|
|
155
|
-
// The expressions being evaluated, outermost first, when the environment replays an error to
|
|
156
|
-
// find where it occurred, as TLC's CallStackTool does.
|
|
157
166
|
callStack?: CallStack;
|
|
167
|
+
coverage?: Coverage;
|
|
158
168
|
}
|
|
159
169
|
|
|
160
170
|
export interface CallStack {
|
|
161
171
|
expressions: Expression[];
|
|
162
172
|
// TLC stops popping the stack once an evaluation fails, so it keeps where the failure occurred.
|
|
163
173
|
isFrozen: boolean;
|
|
164
|
-
// The expression that built each value of the replay that TLC's CallStackTool gives a source.
|
|
165
174
|
sources: WeakMap<ObjectValue, Expression>;
|
|
166
|
-
// The value each expression of the replay last evaluated to.
|
|
167
175
|
values: WeakMap<Expression, Value>;
|
|
168
|
-
//
|
|
169
|
-
// method that computes the operator.
|
|
176
|
+
// Tells a failure of an operand from one of the method that computes the operator.
|
|
170
177
|
failures: WeakMap<Error, Expression>;
|
|
171
|
-
// The failure of a value with a source, which TLC raises as a FingerprintException.
|
|
172
178
|
fingerprint?: FingerprintFailure;
|
|
173
179
|
}
|
|
174
180
|
|
|
175
181
|
export interface FingerprintFailure {
|
|
176
182
|
// The sources of the values whose methods failed, outermost first.
|
|
177
183
|
sources: Expression[];
|
|
178
|
-
// The message of the error the innermost one raised.
|
|
179
184
|
reason: string;
|
|
180
185
|
}
|
|
181
186
|
|
|
182
|
-
// The operators
|
|
187
|
+
// The operators whose value TLC's CallStackTool gives a source.
|
|
183
188
|
const SOURCED_OPERATORS = new Set(["SUBSET", "UNION", "DOMAIN", "\\cup", "\\cap", "\\"]);
|
|
184
189
|
|
|
185
|
-
// CallStackTool
|
|
186
|
-
//
|
|
190
|
+
// CallStackTool returns these lazy values without their source, and a function TLC builds
|
|
191
|
+
// from a FcnLambdaValue has none.
|
|
187
192
|
const SOURCED_KINDS = new Set<Expression["kind"]>([
|
|
188
193
|
"SetEnumeration",
|
|
189
194
|
"SetFilter",
|
|
@@ -199,8 +204,7 @@ const isSourced = (expression: Expression) =>
|
|
|
199
204
|
SOURCED_KINDS.has(expression.kind) ||
|
|
200
205
|
(expression.kind === "Apply" && SOURCED_OPERATORS.has(expression.operator));
|
|
201
206
|
|
|
202
|
-
// The
|
|
203
|
-
// e \in S, f.apply(e) for f[e] when f is a function, or S.diff(T) for S \ T when S is Reducible.
|
|
207
|
+
// The operand whose Java method computes the operator, such as S in e \in S or f in f[e].
|
|
204
208
|
const getReceiver = (expression: Expression, values: CallStack["values"]) => {
|
|
205
209
|
if (expression.kind === "FunctionApplication" || expression.kind === "Except") {
|
|
206
210
|
return expression.function;
|
|
@@ -221,8 +225,7 @@ export interface Context {
|
|
|
221
225
|
scope: Scope | undefined;
|
|
222
226
|
state: State | undefined;
|
|
223
227
|
next: State | undefined;
|
|
224
|
-
// EvalControl.KeepLazy,
|
|
225
|
-
// the evaluation passes on, so that a function constructor within builds a FcnLambdaValue.
|
|
228
|
+
// TLC's EvalControl.KeepLazy, so a function constructor within an applied function stays lazy.
|
|
226
229
|
isKeepLazy?: boolean;
|
|
227
230
|
}
|
|
228
231
|
|
|
@@ -254,6 +257,18 @@ export const describeRange = (expression: Expression) => {
|
|
|
254
257
|
return range ? formatRange(range) : describeLocation(expression);
|
|
255
258
|
};
|
|
256
259
|
|
|
260
|
+
const TEMPORAL_FORMS = {
|
|
261
|
+
Always: "[]A",
|
|
262
|
+
Eventually: "<>A",
|
|
263
|
+
WeakFairness: "WF",
|
|
264
|
+
StrongFairness: "SF",
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const createTemporalFormulaError = (form: string, expression: Expression) =>
|
|
268
|
+
new TlaEvaluationError(
|
|
269
|
+
`TLC encountered a temporal formula (${form}) when evaluating a predicate or action.\n${describeRange(expression)}`,
|
|
270
|
+
);
|
|
271
|
+
|
|
257
272
|
// SANY prints a numeral as written and any other expression by its location.
|
|
258
273
|
export const describeNode = (expression: Expression) =>
|
|
259
274
|
expression.kind === "Number"
|
|
@@ -265,8 +280,7 @@ export const createConditionError = (expression: Expression, value: Value) =>
|
|
|
265
280
|
`A non-boolean expression (${getKindString(value)}) was used as the condition of an IF.\n${describeRange(expression)}`,
|
|
266
281
|
);
|
|
267
282
|
|
|
268
|
-
// TLC
|
|
269
|
-
// junction list, and it words the error differently for each.
|
|
283
|
+
// TLC words the error differently for an infix chain of /\ or \/ and for a bulleted list.
|
|
270
284
|
export const createJunctionError = (
|
|
271
285
|
expression: Expression & { kind: "And" | "Or" },
|
|
272
286
|
index: number,
|
|
@@ -287,12 +301,11 @@ export const createJunctionError = (
|
|
|
287
301
|
);
|
|
288
302
|
};
|
|
289
303
|
|
|
290
|
-
// TLC selects a field
|
|
291
|
-
// which for a tuple keeps its domain as an interval.
|
|
304
|
+
// TLC selects a field from the function it converts a non-record to, which keeps a tuple's domain.
|
|
292
305
|
export const getFieldSelector = (expression: Expression & { kind: "FieldAccess" }) => {
|
|
293
306
|
const field = expression.field;
|
|
294
307
|
const describeMissing = (record: Value) =>
|
|
295
|
-
`Attempted to select nonexistent field "${field}" from the record\n${formatValue(record)}\n${describeRange(expression)}`;
|
|
308
|
+
`Attempted to select nonexistent field "${field}" from the record\n${prettyPrint(formatValue(record))}\n${describeRange(expression)}`;
|
|
296
309
|
const describeNotInDomain = (functionValue: Value) =>
|
|
297
310
|
`Attempted to apply function:\n${prettyPrint(formatValue(functionValue))}\nto argument "${field}", which is not in the domain of the function.`;
|
|
298
311
|
return (value: Value) => {
|
|
@@ -303,10 +316,9 @@ export const getFieldSelector = (expression: Expression & { kind: "FieldAccess"
|
|
|
303
316
|
}
|
|
304
317
|
const functionClass = getFunctionClass(value);
|
|
305
318
|
if (functionClass === "tuple") throw createNonIntegerKeyError(field);
|
|
306
|
-
// Only TLC's RecordValue names the field it lacks
|
|
307
|
-
// whose keys are strings, reports an argument outside its domain.
|
|
319
|
+
// Only TLC's RecordValue names the field it lacks, and TLC enumerates a FcnLambdaValue first.
|
|
308
320
|
return applyFunction(
|
|
309
|
-
value,
|
|
321
|
+
value.kind === "LazyFunction" ? toFiniteFunction(value) : value,
|
|
310
322
|
field,
|
|
311
323
|
functionClass === "record" ? describeMissing : describeNotInDomain,
|
|
312
324
|
);
|
|
@@ -336,35 +348,34 @@ export const getSubsetOperator =
|
|
|
336
348
|
`Attempted to evaluate an expression of form S \\subseteq T, but S was not enumerable.\n${describeRange(expression)}`,
|
|
337
349
|
);
|
|
338
350
|
}
|
|
339
|
-
return builtin.
|
|
351
|
+
return builtin.evaluateBinary
|
|
352
|
+
? builtin.evaluateBinary(left, right)
|
|
353
|
+
: builtin.evaluate([left, right]);
|
|
340
354
|
};
|
|
341
355
|
|
|
342
356
|
// TLC rejects a decimal, or an integer literal too big for it, while it processes the spec.
|
|
343
357
|
export const createRealNumberError = (expression: DecimalExpression) =>
|
|
344
358
|
setExitCode(
|
|
345
|
-
new TlaEvaluationError(
|
|
346
|
-
`TLC can't handle real numbers: ${expression.text} at ${describeLocation(expression)}`,
|
|
347
|
-
),
|
|
359
|
+
new TlaEvaluationError(`TLC can't handle real numbers.\n${expression.text}`),
|
|
348
360
|
EXIT_STATUS.configParseError,
|
|
349
361
|
);
|
|
350
362
|
|
|
351
363
|
export const createBigNumberError = (expression: NumberExpression) =>
|
|
352
364
|
setExitCode(
|
|
353
365
|
new TlaEvaluationError(
|
|
354
|
-
`TLC can't handle a number this big
|
|
366
|
+
`TLC can't handle a number this big.\n${expression.text ?? expression.value}`,
|
|
355
367
|
),
|
|
356
368
|
EXIT_STATUS.error,
|
|
357
369
|
);
|
|
358
370
|
|
|
359
|
-
// TLC's Tool#evalAppl fails to look up a variable the state doesn't assign.
|
|
360
371
|
export const createUnassignedError = (name: string, expression: Expression) =>
|
|
361
372
|
new TlaUnassignedError(
|
|
362
373
|
`In evaluation, the identifier ${name} is either undefined or not an operator.\n${describeRange(expression)}`,
|
|
363
374
|
name,
|
|
364
375
|
);
|
|
365
376
|
|
|
366
|
-
// TLC
|
|
367
|
-
// names the fairness condition when A leaves a variable of v unassigned
|
|
377
|
+
// TLC reads the subscript of <<A>>_v in the successor as a state of its own, and after 1.7.4
|
|
378
|
+
// names the fairness condition when A leaves a variable of v unassigned.
|
|
368
379
|
export const toUnspecifiedSubscriptError = (error: unknown, version: TlcVersion | undefined) =>
|
|
369
380
|
error instanceof TlaUnassignedError && version !== "1.7.4"
|
|
370
381
|
? new TlaEvaluationError(
|
|
@@ -372,11 +383,6 @@ export const toUnspecifiedSubscriptError = (error: unknown, version: TlcVersion
|
|
|
372
383
|
)
|
|
373
384
|
: error;
|
|
374
385
|
|
|
375
|
-
export const createPrimeOutsideActionError = (expression: Expression) =>
|
|
376
|
-
new TlaEvaluationError(
|
|
377
|
-
`A primed expression appears outside an action at ${describeLocation(expression)}`,
|
|
378
|
-
);
|
|
379
|
-
|
|
380
386
|
export const lookupScope = (scope: Scope | undefined, name: string): Binding | undefined => {
|
|
381
387
|
for (let current = scope; current; current = current.parent) {
|
|
382
388
|
if (current.name === name) return current.binding;
|
|
@@ -390,12 +396,13 @@ export const bindValue = (scope: Scope | undefined, name: string, value: Value):
|
|
|
390
396
|
parent: scope,
|
|
391
397
|
});
|
|
392
398
|
|
|
393
|
-
const bindPattern = (scope: Scope | undefined, pattern: BoundPattern, value: Value) => {
|
|
399
|
+
export const bindPattern = (scope: Scope | undefined, pattern: BoundPattern, value: Value) => {
|
|
394
400
|
if (pattern.kind === "Name") return bindValue(scope, pattern.name, value);
|
|
395
401
|
const elements = getSequenceElements(value);
|
|
396
402
|
if (!elements || elements.length !== pattern.names.length) {
|
|
403
|
+
const range = pattern.ranges?.[0];
|
|
397
404
|
throw new TlaEvaluationError(
|
|
398
|
-
`
|
|
405
|
+
`Argument mismatch in operator application.${range ? formatRange(range) : ""}`,
|
|
399
406
|
);
|
|
400
407
|
}
|
|
401
408
|
return pattern.names.reduce<Scope | undefined>(
|
|
@@ -440,6 +447,42 @@ export const expectBoolean = (value: Value, description: string) => {
|
|
|
440
447
|
return value;
|
|
441
448
|
};
|
|
442
449
|
|
|
450
|
+
export const expectImplication = (value: Value, expression: Expression, side: "P" | "Q") => {
|
|
451
|
+
if (typeof value !== "boolean") {
|
|
452
|
+
throw new TlaEvaluationError(
|
|
453
|
+
`Attempted to evaluate an expression of form P => Q when ${side} was\n${getKindString(value)}.\n${describeRange(expression)}`,
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
return value;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
export const negate = (value: Value, expression: Expression) => {
|
|
460
|
+
if (typeof value !== "boolean") {
|
|
461
|
+
throw new TlaEvaluationError(
|
|
462
|
+
`Attempted to apply the operator ~ to a non-boolean\n(${getKindString(value)})\n${describeRange(expression)}`,
|
|
463
|
+
);
|
|
464
|
+
}
|
|
465
|
+
return !value;
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
export const isEquivalent = (left: Value, right: Value, expression: Expression) => {
|
|
469
|
+
if (typeof left !== "boolean" || typeof right !== "boolean") {
|
|
470
|
+
throw new TlaEvaluationError(
|
|
471
|
+
`Attempted to evaluate an expression of form P <=> Q when P or Q was not a boolean.\n${describeRange(expression)}`,
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
return left === right;
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
export const expectBooleanBody = (value: Value, expression: Expression) => {
|
|
478
|
+
if (typeof value !== "boolean") {
|
|
479
|
+
throw new TlaEvaluationError(
|
|
480
|
+
`TLC expected a boolean value, but did not find one. ${describeNode(expression)}`,
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
return value;
|
|
484
|
+
};
|
|
485
|
+
|
|
443
486
|
export const getDefinition = (name: string, environment: Environment) =>
|
|
444
487
|
environment.definitions.get(environment.substitutions.get(name) ?? name);
|
|
445
488
|
|
|
@@ -459,8 +502,7 @@ const getBoundExplanation = (set: Value, text: string) => {
|
|
|
459
502
|
return undefined;
|
|
460
503
|
};
|
|
461
504
|
|
|
462
|
-
// TLC enumerates every set
|
|
463
|
-
// class does.
|
|
505
|
+
// TLC enumerates every set but its Java ones, so any other set fails as its own class does.
|
|
464
506
|
const checkEnumerable = (set: Value, expression: Expression, version: TlcVersion) => {
|
|
465
507
|
if (!isSetValue(set) || isOverriddenSet(set)) {
|
|
466
508
|
const text = formatValue(set);
|
|
@@ -480,16 +522,14 @@ const getEnumeratedElements = (set: Value, expression: Expression, version: TlcV
|
|
|
480
522
|
return getLazySubsets(set) ?? getNormalizedSetElements(set);
|
|
481
523
|
};
|
|
482
524
|
|
|
483
|
-
//
|
|
484
|
-
// lazy set's elements, where later versions normalize the set first.
|
|
525
|
+
// TLC 1.7.4 streams a lazy set's elements, where later versions normalize the set first.
|
|
485
526
|
export const getContextElements = (set: Value, expression: Expression, version: TlcVersion) => {
|
|
486
527
|
if (version !== "1.7.4") return getEnumeratedElements(set, expression, version);
|
|
487
528
|
checkEnumerable(set, expression, version);
|
|
488
529
|
return getLazySubsets(set) ?? getStreamedElements(set);
|
|
489
530
|
};
|
|
490
531
|
|
|
491
|
-
// Like TLC,
|
|
492
|
-
// in a large interval such as 1..2000000000 without listing it.
|
|
532
|
+
// Like TLC, keep an interval so a quantifier can stop at a witness in 1..2000000000 early.
|
|
493
533
|
const getKeptInterval = (set: Value) => {
|
|
494
534
|
if (typeof set !== "object" || set.kind !== "Interval") return undefined;
|
|
495
535
|
getCardinality(set);
|
|
@@ -499,7 +539,6 @@ const getKeptInterval = (set: Value) => {
|
|
|
499
539
|
export const getQuantifierElements = (set: Value, expression: Expression, version: TlcVersion) =>
|
|
500
540
|
getKeptInterval(set) ?? getContextElements(set, expression, version);
|
|
501
541
|
|
|
502
|
-
// CHOOSE and a function constructor visit the elements of the normalized set.
|
|
503
542
|
const getNormalizedElements = (set: Value, expression: Expression, version: TlcVersion) =>
|
|
504
543
|
getKeptInterval(set) ?? getEnumeratedElements(set, expression, version);
|
|
505
544
|
|
|
@@ -514,16 +553,20 @@ const someElement = (
|
|
|
514
553
|
return false;
|
|
515
554
|
};
|
|
516
555
|
|
|
556
|
+
// The names of one bound, as in \A x, y \in S, share its set, which TLC evaluates once.
|
|
557
|
+
export const mapBoundSets = <Result>(bounds: Bound[], map: (set: Expression) => Result) =>
|
|
558
|
+
bounds.reduce<Result[]>((results, bound, index) => {
|
|
559
|
+
results.push(
|
|
560
|
+
index > 0 && bounds[index - 1].set === bound.set ? results[index - 1] : map(bound.set),
|
|
561
|
+
);
|
|
562
|
+
return results;
|
|
563
|
+
}, []);
|
|
564
|
+
|
|
517
565
|
export const getBoundElements = (bounds: Bound[], context: Context) =>
|
|
518
|
-
bounds
|
|
519
|
-
getQuantifierElements(
|
|
520
|
-
evaluate(bound.set, context),
|
|
521
|
-
bound.set,
|
|
522
|
-
context.environment.runtime.version,
|
|
523
|
-
),
|
|
566
|
+
mapBoundSets(bounds, (set) =>
|
|
567
|
+
getQuantifierElements(evaluate(set, context), set, context.environment.runtime.version),
|
|
524
568
|
);
|
|
525
569
|
|
|
526
|
-
// Visits each binding of the bounds to the elements of their sets, until visit returns true.
|
|
527
570
|
export const forEachBoundElement = (
|
|
528
571
|
bounds: Bound[],
|
|
529
572
|
sets: ReturnType<typeof getBoundElements>,
|
|
@@ -546,7 +589,7 @@ export const forEachBinding = (
|
|
|
546
589
|
) => forEachBoundElement(bounds, getBoundElements(bounds, context), context.scope, visit);
|
|
547
590
|
|
|
548
591
|
const getBoundsDomain = (bounds: Bound[], context: Context): Value => {
|
|
549
|
-
const sets = bounds
|
|
592
|
+
const sets = mapBoundSets(bounds, (set) => evaluate(set, context));
|
|
550
593
|
return sets.length === 1 ? sets[0] : { kind: "ProductSet", factors: sets };
|
|
551
594
|
};
|
|
552
595
|
|
|
@@ -559,8 +602,6 @@ const bindArgument = (bounds: Bound[], scope: Scope | undefined, argument: Value
|
|
|
559
602
|
);
|
|
560
603
|
};
|
|
561
604
|
|
|
562
|
-
// The parameters, body and scope of each function a constructor or a definition keeps lazy, whose
|
|
563
|
-
// body TLC's getInitStates and getNextStates walk for an argument, as they do a FcnLambdaValue's.
|
|
564
605
|
interface LazyFunction {
|
|
565
606
|
value: LazyFunctionValue;
|
|
566
607
|
bounds: Bound[];
|
|
@@ -568,8 +609,7 @@ interface LazyFunction {
|
|
|
568
609
|
scope: Scope | undefined;
|
|
569
610
|
}
|
|
570
611
|
|
|
571
|
-
//
|
|
572
|
-
// short-lived function makes every garbage collection slower.
|
|
612
|
+
// Kept on the value itself, since a WeakMap entry per short-lived function slows every GC.
|
|
573
613
|
interface KeptLazyFunctionValue extends LazyFunctionValue {
|
|
574
614
|
lambda: LazyFunction;
|
|
575
615
|
}
|
|
@@ -582,8 +622,6 @@ const keepLambda = (lambda: LazyFunction) => Object.assign(lambda.value, { lambd
|
|
|
582
622
|
export const getLazyFunction = (value: Value) =>
|
|
583
623
|
isKeptLazyFunction(value) ? value.lambda : undefined;
|
|
584
624
|
|
|
585
|
-
// Tool.getFcnContext: the scope of a lazy function's body for the arguments of an application. It
|
|
586
|
-
// ignores arguments beyond the parameters.
|
|
587
625
|
export const bindLazyArguments = (
|
|
588
626
|
lambda: LazyFunction,
|
|
589
627
|
expression: Expression & { kind: "FunctionApplication" },
|
|
@@ -608,12 +646,15 @@ export const bindLazyArguments = (
|
|
|
608
646
|
typeof value.domain === "object" && value.domain.kind === "ProductSet"
|
|
609
647
|
? value.domain.factors
|
|
610
648
|
: [];
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
649
|
+
// FcnLambdaValue#apply indexes past an argument tuple shorter than the bounds and ignores one
|
|
650
|
+
// longer, where this rejects the list.
|
|
651
|
+
const createListError = () =>
|
|
652
|
+
new TlaEvaluationError(
|
|
653
|
+
`In applying the function\n${prettyPrint(formatValue(value))},\nthe argument list is:\n${prettyPrint(formatValue(argument))}\nwhich does not match its formal parameter.\n`,
|
|
614
654
|
);
|
|
615
|
-
|
|
655
|
+
if (elements === undefined) throw createListError();
|
|
616
656
|
bounds.forEach((bound, index) => {
|
|
657
|
+
if (index >= elements.length) throw createListError();
|
|
617
658
|
const element = elements[index];
|
|
618
659
|
const ordinal = `argument number ${index + 1}`;
|
|
619
660
|
if (!isElementOf(element, domains[index])) {
|
|
@@ -634,11 +675,11 @@ export const bindLazyArguments = (
|
|
|
634
675
|
);
|
|
635
676
|
}
|
|
636
677
|
});
|
|
637
|
-
|
|
678
|
+
if (elements.length > bounds.length) throw createListError();
|
|
679
|
+
return bindArgument(bounds, lambda.scope, argument);
|
|
638
680
|
};
|
|
639
681
|
|
|
640
|
-
// TLC prints a function it can't enumerate
|
|
641
|
-
// prints only the first name of a group such as `m, n \in S`.
|
|
682
|
+
// TLC prints a function it can't enumerate with only the first name of a group such as m, n.
|
|
642
683
|
interface LazyFunctionTemplate {
|
|
643
684
|
parameters: { index: number; name: string }[];
|
|
644
685
|
body: string;
|
|
@@ -684,21 +725,29 @@ const getLazyFunctionSource = (
|
|
|
684
725
|
};
|
|
685
726
|
};
|
|
686
727
|
|
|
687
|
-
// TLC's FcnLambdaValue evaluates its body at each application.
|
|
688
728
|
const createLazyFunction = (
|
|
689
729
|
domain: Value,
|
|
690
730
|
compute: (argument: Value, self: LazyFunctionValue) => Value,
|
|
691
731
|
source: LazyFunctionSource | undefined,
|
|
732
|
+
boundTupleLengths: (number | undefined)[] | undefined,
|
|
692
733
|
) => {
|
|
693
734
|
const self: LazyFunctionValue = {
|
|
694
735
|
kind: "LazyFunction",
|
|
695
736
|
domain,
|
|
696
737
|
source,
|
|
738
|
+
boundTupleLengths,
|
|
697
739
|
apply: (argument) => compute(argument, self),
|
|
698
740
|
};
|
|
699
741
|
return self;
|
|
700
742
|
};
|
|
701
743
|
|
|
744
|
+
const getBoundTupleLengths = (bounds: Bound[]) =>
|
|
745
|
+
bounds.length === 1
|
|
746
|
+
? undefined
|
|
747
|
+
: bounds.map((bound) =>
|
|
748
|
+
bound.pattern.kind === "Tuple" ? bound.pattern.names.length : undefined,
|
|
749
|
+
);
|
|
750
|
+
|
|
702
751
|
// The function constructor of a definition, which SANY locates at the whole definition.
|
|
703
752
|
const getDefinitionSource = (definition: FunctionDefinition): Expression => ({
|
|
704
753
|
kind: "FunctionConstruction",
|
|
@@ -707,7 +756,10 @@ const getDefinitionSource = (definition: FunctionDefinition): Expression => ({
|
|
|
707
756
|
location: definition.location,
|
|
708
757
|
});
|
|
709
758
|
|
|
710
|
-
const createRecursiveFunction = (
|
|
759
|
+
const createRecursiveFunction = (
|
|
760
|
+
definition: FunctionDefinition,
|
|
761
|
+
context: Context,
|
|
762
|
+
): RebindableLazyFunction => {
|
|
711
763
|
const domain = getBoundsDomain(definition.bounds, context);
|
|
712
764
|
const functionValue = createLazyFunction(
|
|
713
765
|
domain,
|
|
@@ -724,6 +776,7 @@ const createRecursiveFunction = (definition: FunctionDefinition, context: Contex
|
|
|
724
776
|
definition.bounds,
|
|
725
777
|
domain,
|
|
726
778
|
),
|
|
779
|
+
getBoundTupleLengths(definition.bounds),
|
|
727
780
|
);
|
|
728
781
|
keepLambda({
|
|
729
782
|
value: functionValue,
|
|
@@ -731,11 +784,17 @@ const createRecursiveFunction = (definition: FunctionDefinition, context: Contex
|
|
|
731
784
|
body: definition.body,
|
|
732
785
|
scope: bindValue(undefined, definition.name, functionValue),
|
|
733
786
|
});
|
|
734
|
-
|
|
787
|
+
const rebindable: RebindableLazyFunction = Object.assign(functionValue, {
|
|
788
|
+
rebind: (tracing: Environment) => {
|
|
789
|
+
const rebound = createRecursiveFunction(definition, { ...context, environment: tracing });
|
|
790
|
+
rebound.enumerated = functionValue.enumerated;
|
|
791
|
+
return rebound;
|
|
792
|
+
},
|
|
793
|
+
});
|
|
794
|
+
return definition.bounds.length === 1 ? markLambdaFunction(rebindable) : rebindable;
|
|
735
795
|
};
|
|
736
796
|
|
|
737
|
-
// TLC computes a constant definition
|
|
738
|
-
// lazy, so a function over an interval or an enumerated set is a FcnRcdValue.
|
|
797
|
+
// TLC computes a constant definition eagerly, so a function over a reducible set is a FcnRcdValue.
|
|
739
798
|
const toStartupValue = (definition: Definition, value: Value): Value => {
|
|
740
799
|
if (typeof value !== "object" || getFunctionClass(value) !== "lambda") return value;
|
|
741
800
|
if (
|
|
@@ -756,8 +815,7 @@ const toStartupValue = (definition: Definition, value: Value): Value => {
|
|
|
756
815
|
}
|
|
757
816
|
};
|
|
758
817
|
|
|
759
|
-
//
|
|
760
|
-
// isn't recursive as it does a function constructor.
|
|
818
|
+
// TLC evaluates a function definition that isn't recursive as it does a function constructor.
|
|
761
819
|
const compileDefinitionBody = (definition: Definition, environment: Environment): Evaluator => {
|
|
762
820
|
if (definition.kind === "Operator" && definition.params.length > 0) {
|
|
763
821
|
return () => {
|
|
@@ -772,8 +830,6 @@ const compileDefinitionBody = (definition: Definition, environment: Environment)
|
|
|
772
830
|
return getEntryEvaluator(getDefinitionSource(definition), environment);
|
|
773
831
|
};
|
|
774
832
|
|
|
775
|
-
// The code that evaluates a reference to a definition: the value TLC computed at startup when it
|
|
776
|
-
// has one, and the body at the top level otherwise.
|
|
777
833
|
const getDefinitionEvaluator = (definition: Definition, environment: Environment): Evaluator => {
|
|
778
834
|
const compiled = environment.definitionEvaluators.get(definition);
|
|
779
835
|
if (compiled) return compiled;
|
|
@@ -793,8 +849,6 @@ export const getStartupValue = (definition: Definition, environment: Environment
|
|
|
793
849
|
return id === undefined ? undefined : environment.startupValues[id];
|
|
794
850
|
};
|
|
795
851
|
|
|
796
|
-
// TLC computes each constant definition the configuration doesn't replace when it starts, and
|
|
797
|
-
// leaves one it fails to compute to evaluate where the spec uses it.
|
|
798
852
|
export const evaluateStartupDefinitions = (definitions: Definition[], environment: Environment) =>
|
|
799
853
|
definitions.forEach((definition) => {
|
|
800
854
|
if (definition.kind === "Operator" && definition.params.length > 0) return;
|
|
@@ -816,11 +870,46 @@ export const evaluateStartupDefinitions = (definitions: Definition[], environmen
|
|
|
816
870
|
}
|
|
817
871
|
});
|
|
818
872
|
|
|
873
|
+
// TLC reuses a cached value while each variable the cached state assigns keeps its value.
|
|
874
|
+
const isStateWithin = (state: State | undefined, cached: State | undefined) => {
|
|
875
|
+
if (cached === undefined || state === cached) return true;
|
|
876
|
+
try {
|
|
877
|
+
return cached.every(
|
|
878
|
+
(value, index) =>
|
|
879
|
+
value === undefined || (state?.[index] !== undefined && valuesEqual(state[index], value)),
|
|
880
|
+
);
|
|
881
|
+
} catch (error) {
|
|
882
|
+
if (error instanceof TlaEvaluationError) return false;
|
|
883
|
+
throw error;
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
|
|
819
887
|
const forceThunk = (thunk: Thunk, context: Context) => {
|
|
820
|
-
const
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
888
|
+
const cache = thunk.cache;
|
|
889
|
+
const isCached =
|
|
890
|
+
cache !== undefined &&
|
|
891
|
+
isStateWithin(context.state, thunk.state) &&
|
|
892
|
+
isStateWithin(context.next, thunk.next);
|
|
893
|
+
if (isCached) return cache;
|
|
894
|
+
const coverage = context.environment.coverage;
|
|
895
|
+
const node = coverage?.current === -1 ? undefined : coverage?.values.get(thunk);
|
|
896
|
+
const value =
|
|
897
|
+
coverage && node !== undefined
|
|
898
|
+
? runAtCoverageNode(coverage, node, () =>
|
|
899
|
+
evaluate(thunk.expression, withScope(context, thunk.scope)),
|
|
900
|
+
)
|
|
901
|
+
: evaluate(thunk.expression, withScope(context, thunk.scope));
|
|
902
|
+
if (!thunk.isStreamed || isSettledValue(value)) {
|
|
903
|
+
thunk.cache = value;
|
|
904
|
+
thunk.state = context.state;
|
|
905
|
+
thunk.next = context.next;
|
|
906
|
+
}
|
|
907
|
+
// A value a thunk cached with no state read no variable, so it holds in every state. Releasing
|
|
908
|
+
// its scope frees the evaluations the scope's bindings cached, which a long script keeps alive.
|
|
909
|
+
if (thunk.cache !== undefined && thunk.state === undefined && thunk.next === undefined) {
|
|
910
|
+
thunk.scope = undefined;
|
|
911
|
+
thunk.isReleased = true;
|
|
912
|
+
}
|
|
824
913
|
return value;
|
|
825
914
|
};
|
|
826
915
|
|
|
@@ -888,6 +977,12 @@ const evaluateOperatorArgument = (expression: Expression, context: Context): Ope
|
|
|
888
977
|
};
|
|
889
978
|
|
|
890
979
|
const callOperatorWithValues = (operator: Operator, args: Value[], context: Context): Value => {
|
|
980
|
+
if (operator.kind === "Builtin" && operator.isLazy) {
|
|
981
|
+
return operator.evaluate(
|
|
982
|
+
args.map((value) => ({ kind: "Operator", name: "argument", arity: 0, call: () => value })),
|
|
983
|
+
context,
|
|
984
|
+
);
|
|
985
|
+
}
|
|
891
986
|
if (operator.kind === "Builtin") return operator.evaluate(args, context);
|
|
892
987
|
if (operator.kind === "Operator") return operator.call(args);
|
|
893
988
|
const scope = operator.params.reduce<Scope | undefined>(
|
|
@@ -905,11 +1000,6 @@ const toOperatorValue = (operator: Operator, context: Context): OperatorValue =>
|
|
|
905
1000
|
name: operator.name,
|
|
906
1001
|
arity: getArity(operator),
|
|
907
1002
|
call: (args) => callOperatorWithValues(operator, args, context),
|
|
908
|
-
literalBody:
|
|
909
|
-
operator.kind === "Closure" &&
|
|
910
|
-
(operator.body.kind === "Number" || operator.body.kind === "String")
|
|
911
|
-
? operator.body.value
|
|
912
|
-
: undefined,
|
|
913
1003
|
};
|
|
914
1004
|
|
|
915
1005
|
const isOperatorArgument = (expression: Expression, context: Context) => {
|
|
@@ -961,9 +1051,18 @@ const compileArgumentBinder = (argument: Expression, environment: Environment):
|
|
|
961
1051
|
const isLiteral =
|
|
962
1052
|
argument.kind === "String" || (argument.kind === "Number" && isTlcInteger(argument.value));
|
|
963
1053
|
if (isLiteral) {
|
|
964
|
-
const binding: Binding = { kind: "Value", value: argument.value };
|
|
1054
|
+
const binding: Binding = { kind: "Value", value: argument.value, argument };
|
|
965
1055
|
return () => binding;
|
|
966
1056
|
}
|
|
1057
|
+
// TLC counts an argument where the operator's body evaluates it, under the call.
|
|
1058
|
+
const coverage = environment.coverage;
|
|
1059
|
+
if (coverage) {
|
|
1060
|
+
return (context) => {
|
|
1061
|
+
const thunk = createThunk(argument, context);
|
|
1062
|
+
if (coverage.current !== -1) coverage.values.set(thunk, coverage.current);
|
|
1063
|
+
return thunk;
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
967
1066
|
if (argument.kind === "Identifier") {
|
|
968
1067
|
const global = isLocallyResolved(argument)
|
|
969
1068
|
? undefined
|
|
@@ -972,7 +1071,7 @@ const compileArgumentBinder = (argument: Expression, environment: Environment):
|
|
|
972
1071
|
const binding: Binding = { kind: "Value", value: global.value };
|
|
973
1072
|
return () => binding;
|
|
974
1073
|
}
|
|
975
|
-
if (global === undefined) {
|
|
1074
|
+
if (global === undefined && !environment.callStack) {
|
|
976
1075
|
return (context) => {
|
|
977
1076
|
const binding = lookupScope(context.scope, argument.name);
|
|
978
1077
|
return binding?.kind === "Value" || binding?.kind === "Thunk"
|
|
@@ -1020,65 +1119,85 @@ const callOperator = (
|
|
|
1020
1119
|
return operator.evaluate(evaluateBuiltinArguments(args, context), context);
|
|
1021
1120
|
};
|
|
1022
1121
|
|
|
1023
|
-
// The LET definitions that a reference evaluates again, and those that cache only a settled value.
|
|
1024
1122
|
interface ReevaluatedDefinitions {
|
|
1025
1123
|
impure: ReadonlySet<Definition>;
|
|
1026
1124
|
streamed: ReadonlySet<Definition>;
|
|
1027
1125
|
}
|
|
1028
1126
|
|
|
1127
|
+
// A LET's function definition has one function constructor, as in SANY, so its code compiles once
|
|
1128
|
+
// however often the LET is evaluated.
|
|
1129
|
+
const letFunctionConstructions = new WeakMap<FunctionDefinition, Expression>();
|
|
1130
|
+
|
|
1131
|
+
const getLetFunctionConstruction = (definition: FunctionDefinition): Expression => {
|
|
1132
|
+
const known = letFunctionConstructions.get(definition);
|
|
1133
|
+
if (known) return known;
|
|
1134
|
+
const construction: Expression = {
|
|
1135
|
+
kind: "FunctionConstruction",
|
|
1136
|
+
bounds: definition.bounds,
|
|
1137
|
+
body: definition.body,
|
|
1138
|
+
location: definition.location,
|
|
1139
|
+
isRecursive: true,
|
|
1140
|
+
};
|
|
1141
|
+
letFunctionConstructions.set(definition, construction);
|
|
1142
|
+
return construction;
|
|
1143
|
+
};
|
|
1144
|
+
|
|
1029
1145
|
export const bindDefinitions = (
|
|
1030
1146
|
definitions: Definition[],
|
|
1031
1147
|
context: Context,
|
|
1032
1148
|
reevaluated?: ReevaluatedDefinitions,
|
|
1033
|
-
): Scope | undefined =>
|
|
1034
|
-
definitions.
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
node.binding = {
|
|
1042
|
-
kind: "Thunk",
|
|
1043
|
-
expression: {
|
|
1044
|
-
kind: "FunctionConstruction",
|
|
1045
|
-
bounds: definition.bounds,
|
|
1046
|
-
body: definition.body,
|
|
1047
|
-
location: definition.location,
|
|
1048
|
-
isRecursive: true,
|
|
1049
|
-
},
|
|
1050
|
-
scope: node,
|
|
1051
|
-
state: context.state,
|
|
1052
|
-
next: context.next,
|
|
1053
|
-
cache: undefined,
|
|
1054
|
-
isStreamed: false,
|
|
1055
|
-
};
|
|
1056
|
-
return node;
|
|
1057
|
-
}
|
|
1058
|
-
if (definition.params.length > 0 || reevaluated?.impure.has(definition)) {
|
|
1059
|
-
node.binding = {
|
|
1060
|
-
kind: "Operator",
|
|
1061
|
-
operator: {
|
|
1062
|
-
kind: "Closure",
|
|
1063
|
-
name: definition.name,
|
|
1064
|
-
params: definition.params,
|
|
1065
|
-
body: definition.body,
|
|
1066
|
-
scope: node,
|
|
1067
|
-
},
|
|
1068
|
-
};
|
|
1069
|
-
return node;
|
|
1070
|
-
}
|
|
1071
|
-
node.binding = {
|
|
1072
|
-
kind: "Thunk",
|
|
1073
|
-
expression: definition.body,
|
|
1074
|
-
scope: node,
|
|
1075
|
-
state: context.state,
|
|
1076
|
-
next: context.next,
|
|
1077
|
-
cache: undefined,
|
|
1078
|
-
isStreamed: reevaluated?.streamed.has(definition) ?? false,
|
|
1079
|
-
};
|
|
1149
|
+
): Scope | undefined => {
|
|
1150
|
+
const nodes = definitions.map((definition): Scope => ({
|
|
1151
|
+
name: definition.name,
|
|
1152
|
+
binding: { kind: "Value", value: false },
|
|
1153
|
+
parent: undefined,
|
|
1154
|
+
}));
|
|
1155
|
+
const scope = nodes.reduce<Scope | undefined>((parent, node) => {
|
|
1156
|
+
node.parent = parent;
|
|
1080
1157
|
return node;
|
|
1081
1158
|
}, context.scope);
|
|
1159
|
+
definitions.forEach((definition, index) => {
|
|
1160
|
+
nodes[index].binding =
|
|
1161
|
+
definition.kind === "Function"
|
|
1162
|
+
? {
|
|
1163
|
+
kind: "Thunk",
|
|
1164
|
+
expression: getLetFunctionConstruction(definition),
|
|
1165
|
+
scope,
|
|
1166
|
+
state: context.state,
|
|
1167
|
+
next: context.next,
|
|
1168
|
+
cache: undefined,
|
|
1169
|
+
isStreamed: false,
|
|
1170
|
+
}
|
|
1171
|
+
: definition.params.length > 0 || reevaluated?.impure.has(definition)
|
|
1172
|
+
? {
|
|
1173
|
+
kind: "Operator",
|
|
1174
|
+
operator: {
|
|
1175
|
+
kind: "Closure",
|
|
1176
|
+
name: definition.name,
|
|
1177
|
+
params: definition.params,
|
|
1178
|
+
body: definition.body,
|
|
1179
|
+
scope,
|
|
1180
|
+
},
|
|
1181
|
+
}
|
|
1182
|
+
: {
|
|
1183
|
+
kind: "Thunk",
|
|
1184
|
+
expression: definition.body,
|
|
1185
|
+
scope,
|
|
1186
|
+
state: context.state,
|
|
1187
|
+
next: context.next,
|
|
1188
|
+
cache: undefined,
|
|
1189
|
+
isStreamed: reevaluated?.streamed.has(definition) ?? false,
|
|
1190
|
+
};
|
|
1191
|
+
});
|
|
1192
|
+
// TLC counts a LET definition under the node that encloses the LET.
|
|
1193
|
+
const coverage = context.environment.coverage;
|
|
1194
|
+
if (coverage && coverage.current !== -1) {
|
|
1195
|
+
nodes.forEach((node) => {
|
|
1196
|
+
if (node.binding.kind === "Thunk") coverage.values.set(node.binding, coverage.current);
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
return scope;
|
|
1200
|
+
};
|
|
1082
1201
|
|
|
1083
1202
|
const evaluateLocalIdentifier = (name: string, expression: Expression, context: Context): Value => {
|
|
1084
1203
|
const binding = lookupScope(context.scope, name);
|
|
@@ -1105,9 +1224,8 @@ const evaluateLocalIdentifier = (name: string, expression: Expression, context:
|
|
|
1105
1224
|
throw new TlaEvaluationError(`Unknown identifier ${name} at ${describeLocation(expression)}`);
|
|
1106
1225
|
};
|
|
1107
1226
|
|
|
1108
|
-
// TLC's next-state generator
|
|
1109
|
-
//
|
|
1110
|
-
// with UNION or \cup starts out uncached.
|
|
1227
|
+
// TLC's next-state generator evaluates a LET definition again at each reference, so one that
|
|
1228
|
+
// calls an impure builtin repeats its effects, and a UNION or \cup set starts out uncached.
|
|
1111
1229
|
export const getReevaluatedDefinitions = (
|
|
1112
1230
|
definitions: Definition[],
|
|
1113
1231
|
environment: Environment,
|
|
@@ -1123,23 +1241,41 @@ export const isImpure = (environment: Environment) =>
|
|
|
1123
1241
|
isImpureDefinition(definition, environment),
|
|
1124
1242
|
);
|
|
1125
1243
|
|
|
1126
|
-
// Whether evaluating a definition again can show, such as by printing twice.
|
|
1127
1244
|
export const hasRepeatedEffects = (environment: Environment) =>
|
|
1128
1245
|
[...environment.facts.ids.keys()].some((definition) =>
|
|
1129
1246
|
isImpureDefinition(definition, environment, "repeated"),
|
|
1130
1247
|
);
|
|
1131
1248
|
|
|
1132
|
-
// A set filter TLC evaluates lazily, with how to rebuild it so that its predicate runs in another
|
|
1133
|
-
// environment.
|
|
1134
1249
|
interface RebindableFilteredSet extends FilteredSetValue {
|
|
1135
1250
|
rebind: (environment: Environment) => Value;
|
|
1136
1251
|
}
|
|
1137
1252
|
|
|
1138
|
-
|
|
1139
|
-
|
|
1253
|
+
interface RebindableLazyFunction extends LazyFunctionValue {
|
|
1254
|
+
rebind: (environment: Environment) => Value;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
const isRebindable = (value: Value): value is RebindableFilteredSet | RebindableLazyFunction =>
|
|
1258
|
+
typeof value === "object" &&
|
|
1259
|
+
(value.kind === "FilteredSet" || value.kind === "LazyFunction") &&
|
|
1260
|
+
"rebind" in value;
|
|
1261
|
+
|
|
1262
|
+
const toNextContext = (context: Context): Context => ({
|
|
1263
|
+
environment: context.environment,
|
|
1264
|
+
scope: context.scope,
|
|
1265
|
+
state: context.next,
|
|
1266
|
+
next: undefined,
|
|
1267
|
+
isKeepLazy: context.isKeepLazy,
|
|
1268
|
+
});
|
|
1269
|
+
|
|
1270
|
+
// TLC evaluates the expression in the current state first.
|
|
1271
|
+
const compileUnchanged =
|
|
1272
|
+
(evaluator: Evaluator): Evaluator =>
|
|
1273
|
+
(context) => {
|
|
1274
|
+
const current = evaluator(context);
|
|
1275
|
+
return valuesEqual(current, evaluator(toNextContext(context)));
|
|
1276
|
+
};
|
|
1140
1277
|
|
|
1141
|
-
// TLC's CallStackTool rebinds the lazy values
|
|
1142
|
-
// records its call stack too.
|
|
1278
|
+
// TLC's CallStackTool rebinds the lazy values it returns, so their later evaluation records too.
|
|
1143
1279
|
const traceEvaluator =
|
|
1144
1280
|
(
|
|
1145
1281
|
expression: Expression,
|
|
@@ -1157,13 +1293,17 @@ const traceEvaluator =
|
|
|
1157
1293
|
setTrace.callStack = callStack;
|
|
1158
1294
|
try {
|
|
1159
1295
|
const computed = evaluator(context);
|
|
1160
|
-
const value =
|
|
1296
|
+
const value = isRebindable(computed) ? computed.rebind(environment) : computed;
|
|
1161
1297
|
callStack.values.set(expression, value);
|
|
1162
1298
|
if (typeof value === "object" && value.kind !== "FilteredSet" && isSourced(expression)) {
|
|
1163
1299
|
callStack.sources.set(value, expression);
|
|
1164
1300
|
}
|
|
1165
1301
|
return value;
|
|
1166
1302
|
} catch (error) {
|
|
1303
|
+
if (error instanceof TlaSetBoundError && !callStack.isFrozen) {
|
|
1304
|
+
callStack.expressions.length = 0;
|
|
1305
|
+
callStack.isFrozen = true;
|
|
1306
|
+
}
|
|
1167
1307
|
const failed = error instanceof Error ? callStack.failures.get(error) : undefined;
|
|
1168
1308
|
if (error instanceof Error) callStack.failures.set(error, expression);
|
|
1169
1309
|
const receiver = getReceiver(expression, callStack.values);
|
|
@@ -1191,9 +1331,8 @@ const traceEvaluator =
|
|
|
1191
1331
|
}
|
|
1192
1332
|
};
|
|
1193
1333
|
|
|
1194
|
-
// A method of a value with a source
|
|
1195
|
-
// whose method called it
|
|
1196
|
-
// first. The values are those whose methods failed, outermost first.
|
|
1334
|
+
// A FingerprintException from a method of a value with a source is headed by each such value
|
|
1335
|
+
// whose method called it in turn, outermost first.
|
|
1197
1336
|
export const freezeCallStack = (
|
|
1198
1337
|
callStack: CallStack,
|
|
1199
1338
|
values: (Value | undefined)[],
|
|
@@ -1216,8 +1355,6 @@ export const freezeCallStack = (
|
|
|
1216
1355
|
callStack.isFrozen = true;
|
|
1217
1356
|
};
|
|
1218
1357
|
|
|
1219
|
-
// A FingerprintException that a method of a value with a source raises, or that passes through one,
|
|
1220
|
-
// takes the value's source as its new head.
|
|
1221
1358
|
const headFingerprint = (callStack: CallStack, source: Expression, error: unknown) => {
|
|
1222
1359
|
if (!(error instanceof TlaEvaluationError)) return;
|
|
1223
1360
|
if (!callStack.isFrozen) callStack.expressions.push(source);
|
|
@@ -1229,10 +1366,10 @@ const headFingerprint = (callStack: CallStack, source: Expression, error: unknow
|
|
|
1229
1366
|
};
|
|
1230
1367
|
};
|
|
1231
1368
|
|
|
1232
|
-
//
|
|
1233
|
-
// own since the environment's evaluators record nothing.
|
|
1369
|
+
// With evaluators of its own, since the environment's evaluators record nothing.
|
|
1234
1370
|
export const createTracingEnvironment = (environment: Environment): Environment => ({
|
|
1235
1371
|
...environment,
|
|
1372
|
+
coverage: undefined,
|
|
1236
1373
|
evaluators: new WeakMap(),
|
|
1237
1374
|
entryEvaluators: new WeakMap(),
|
|
1238
1375
|
definitionEvaluators: new WeakMap(),
|
|
@@ -1245,20 +1382,63 @@ export const createTracingEnvironment = (environment: Environment): Environment
|
|
|
1245
1382
|
},
|
|
1246
1383
|
});
|
|
1247
1384
|
|
|
1385
|
+
// A replay that walks into the expression has recorded its frames already.
|
|
1386
|
+
const traceFrames =
|
|
1387
|
+
(
|
|
1388
|
+
expression: Expression,
|
|
1389
|
+
frames: Expression[],
|
|
1390
|
+
evaluator: Evaluator,
|
|
1391
|
+
callStack: CallStack,
|
|
1392
|
+
): Evaluator =>
|
|
1393
|
+
(context) => {
|
|
1394
|
+
if (callStack.expressions.at(-1) === expression) return evaluator(context);
|
|
1395
|
+
callStack.expressions.push(...frames);
|
|
1396
|
+
try {
|
|
1397
|
+
return evaluator(context);
|
|
1398
|
+
} finally {
|
|
1399
|
+
if (!callStack.isFrozen) callStack.expressions.length -= frames.length;
|
|
1400
|
+
}
|
|
1401
|
+
};
|
|
1402
|
+
|
|
1403
|
+
// TLC counts an evaluation under the node of the expression in the tree of the check it counts,
|
|
1404
|
+
// or, when the tree has none there, under the node it evaluates in.
|
|
1405
|
+
const countEvaluator =
|
|
1406
|
+
(expression: Expression, evaluator: Evaluator, coverage: Coverage): Evaluator =>
|
|
1407
|
+
(context) => {
|
|
1408
|
+
const outer = coverage.current;
|
|
1409
|
+
if (outer === -1 || !coverage.covered.has(expression)) return evaluator(context);
|
|
1410
|
+
const node = getCoverageNode(coverage, outer, expression);
|
|
1411
|
+
countCoverage(coverage, node);
|
|
1412
|
+
coverage.current = node;
|
|
1413
|
+
try {
|
|
1414
|
+
const value = evaluator(context);
|
|
1415
|
+
if (typeof value === "object" && expression.kind === "SetEnumeration") {
|
|
1416
|
+
coverage.values.set(value, node);
|
|
1417
|
+
}
|
|
1418
|
+
return value;
|
|
1419
|
+
} finally {
|
|
1420
|
+
coverage.current = outer;
|
|
1421
|
+
}
|
|
1422
|
+
};
|
|
1423
|
+
|
|
1248
1424
|
export const getEvaluator = (expression: Expression, environment: Environment): Evaluator => {
|
|
1249
1425
|
const cached = environment.evaluators.get(expression);
|
|
1250
1426
|
if (cached) return cached;
|
|
1251
1427
|
const compiled = compile(expression, environment);
|
|
1252
1428
|
const callStack = environment.callStack;
|
|
1253
|
-
const
|
|
1429
|
+
const frames = callStack && substitutionFrames.get(expression);
|
|
1430
|
+
const traced = callStack
|
|
1254
1431
|
? traceEvaluator(expression, compiled, environment, callStack)
|
|
1255
1432
|
: compiled;
|
|
1433
|
+
const framed = callStack && frames ? traceFrames(expression, frames, traced, callStack) : traced;
|
|
1434
|
+
const coverage = environment.coverage;
|
|
1435
|
+
const evaluator = coverage ? countEvaluator(expression, framed, coverage) : framed;
|
|
1256
1436
|
environment.evaluators.set(expression, evaluator);
|
|
1257
1437
|
return evaluator;
|
|
1258
1438
|
};
|
|
1259
1439
|
|
|
1260
1440
|
export const getEntryEvaluator = (expression: Expression, environment: Environment) => {
|
|
1261
|
-
if (environment.callStack) return getEvaluator(expression, environment);
|
|
1441
|
+
if (environment.callStack || environment.coverage) return getEvaluator(expression, environment);
|
|
1262
1442
|
const compiled = environment.entryEvaluators.get(expression);
|
|
1263
1443
|
if (compiled) return compiled;
|
|
1264
1444
|
const evaluator =
|
|
@@ -1289,8 +1469,7 @@ const compileIdentifier = (
|
|
|
1289
1469
|
return () => value;
|
|
1290
1470
|
}
|
|
1291
1471
|
if (global?.kind === "Definition") {
|
|
1292
|
-
// The body compiles when
|
|
1293
|
-
// refers back to it.
|
|
1472
|
+
// The body compiles when first evaluated, as a recursive definition's body refers back to it.
|
|
1294
1473
|
const definition = global.definition;
|
|
1295
1474
|
const compiled = { reference: undefined as Evaluator | undefined };
|
|
1296
1475
|
return (context) =>
|
|
@@ -1397,12 +1576,24 @@ const compileApply = (
|
|
|
1397
1576
|
const left = getEvaluator(args[0], environment);
|
|
1398
1577
|
const right = getEvaluator(args[1], environment);
|
|
1399
1578
|
return (context) =>
|
|
1400
|
-
!
|
|
1401
|
-
|
|
1579
|
+
!expectImplication(left(context), expression, "P") ||
|
|
1580
|
+
expectImplication(right(context), expression, "Q");
|
|
1581
|
+
}
|
|
1582
|
+
if (expression.operator === "~" && args.length === 1) {
|
|
1583
|
+
const operand = getEvaluator(args[0], environment);
|
|
1584
|
+
return (context) => negate(operand(context), expression);
|
|
1402
1585
|
}
|
|
1403
|
-
if (expression.operator === "
|
|
1586
|
+
if (expression.operator === "<=>") {
|
|
1587
|
+
const left = getEvaluator(args[0], environment);
|
|
1588
|
+
const right = getEvaluator(args[1], environment);
|
|
1589
|
+
return (context) => isEquivalent(left(context), right(context), expression);
|
|
1590
|
+
}
|
|
1591
|
+
if (expression.operator === "~>" || expression.operator === "-+->") {
|
|
1404
1592
|
return () => {
|
|
1405
|
-
throw
|
|
1593
|
+
throw createTemporalFormulaError(
|
|
1594
|
+
expression.operator === "~>" ? "a ~> b" : "a -+-> formula",
|
|
1595
|
+
expression,
|
|
1596
|
+
);
|
|
1406
1597
|
};
|
|
1407
1598
|
}
|
|
1408
1599
|
const global = isLocallyResolved(expression)
|
|
@@ -1414,14 +1605,15 @@ const compileApply = (
|
|
|
1414
1605
|
const domain = getDomainOperator(expression);
|
|
1415
1606
|
return (context) => domain(operand(context));
|
|
1416
1607
|
}
|
|
1417
|
-
// TLC
|
|
1608
|
+
// TLC caches the value of a constant TLCEval argument only where no context binds a name.
|
|
1418
1609
|
if (global?.kind === "Builtin" && global.name === "TLCEval" && args.length === 1) {
|
|
1419
|
-
const
|
|
1420
|
-
|
|
1421
|
-
return operand;
|
|
1422
|
-
}
|
|
1610
|
+
const isCached =
|
|
1611
|
+
!contextualApplications.has(expression) && !isNonconstant(args[0], environment);
|
|
1423
1612
|
return (context) =>
|
|
1424
|
-
|
|
1613
|
+
global.evaluate(
|
|
1614
|
+
getLazyArguments(args, context),
|
|
1615
|
+
isCached && context.scope === undefined ? undefined : context,
|
|
1616
|
+
);
|
|
1425
1617
|
}
|
|
1426
1618
|
if (global?.kind === "Builtin" && global.name === "\\subseteq" && args.length === 2) {
|
|
1427
1619
|
const left = getEvaluator(args[0], environment);
|
|
@@ -1459,7 +1651,6 @@ const compileQuantifier = (
|
|
|
1459
1651
|
environment: Environment,
|
|
1460
1652
|
): Evaluator => {
|
|
1461
1653
|
const isForall = expression.kind === "Forall";
|
|
1462
|
-
const description = isForall ? "the body of \\A" : "the body of \\E";
|
|
1463
1654
|
const body = getEvaluator(expression.body, environment);
|
|
1464
1655
|
const bound = expression.bounds[0];
|
|
1465
1656
|
if (expression.bounds.length === 1 && bound.pattern.kind === "Name") {
|
|
@@ -1469,9 +1660,9 @@ const compileQuantifier = (
|
|
|
1469
1660
|
const isFound = someElement(
|
|
1470
1661
|
getQuantifierElements(set(context), bound.set, environment.runtime.version),
|
|
1471
1662
|
(element) =>
|
|
1472
|
-
|
|
1663
|
+
expectBooleanBody(
|
|
1473
1664
|
body(withScope(context, bindValue(context.scope, name, element))),
|
|
1474
|
-
|
|
1665
|
+
expression,
|
|
1475
1666
|
) !== isForall,
|
|
1476
1667
|
);
|
|
1477
1668
|
return isFound ? !isForall : isForall;
|
|
@@ -1480,14 +1671,17 @@ const compileQuantifier = (
|
|
|
1480
1671
|
const bounds = expression.bounds;
|
|
1481
1672
|
if (bounds.every((item) => item.pattern.kind === "Name")) {
|
|
1482
1673
|
const names = bounds.map((item) => (item.pattern.kind === "Name" ? item.pattern.name : ""));
|
|
1483
|
-
const sets = bounds.map((item) => getEvaluator(item.set, environment));
|
|
1484
1674
|
return (context) => {
|
|
1485
|
-
const lists =
|
|
1486
|
-
getQuantifierElements(
|
|
1675
|
+
const lists = mapBoundSets(bounds, (set) =>
|
|
1676
|
+
getQuantifierElements(
|
|
1677
|
+
getEvaluator(set, environment)(context),
|
|
1678
|
+
set,
|
|
1679
|
+
environment.runtime.version,
|
|
1680
|
+
),
|
|
1487
1681
|
);
|
|
1488
1682
|
const visit = (index: number, scope: Scope | undefined): boolean => {
|
|
1489
1683
|
if (index < 0) {
|
|
1490
|
-
return
|
|
1684
|
+
return expectBooleanBody(body(withScope(context, scope)), expression) !== isForall;
|
|
1491
1685
|
}
|
|
1492
1686
|
return someElement(lists[index], (element) =>
|
|
1493
1687
|
visit(index - 1, bindValue(scope, names[index], element)),
|
|
@@ -1500,7 +1694,7 @@ const compileQuantifier = (
|
|
|
1500
1694
|
const isFound = forEachBinding(
|
|
1501
1695
|
bounds,
|
|
1502
1696
|
context,
|
|
1503
|
-
(scope) =>
|
|
1697
|
+
(scope) => expectBooleanBody(body(withScope(context, scope)), expression) !== isForall,
|
|
1504
1698
|
);
|
|
1505
1699
|
return isFound ? !isForall : isForall;
|
|
1506
1700
|
};
|
|
@@ -1519,8 +1713,7 @@ const getBoundValue = (pattern: BoundPattern, scope: Scope | undefined) => {
|
|
|
1519
1713
|
return binding?.kind === "Value" ? binding.value : undefined;
|
|
1520
1714
|
};
|
|
1521
1715
|
|
|
1522
|
-
// TLC keeps a function constructor lazy when it is
|
|
1523
|
-
// an EXCEPT that is, so it evaluates the body only at the arguments it is applied to.
|
|
1716
|
+
// TLC keeps a function constructor lazy when it is applied, or is the base of an applied EXCEPT.
|
|
1524
1717
|
const keptLazyExpressions = new WeakSet<Expression>();
|
|
1525
1718
|
|
|
1526
1719
|
export const keepLazy = (expression: Expression) => {
|
|
@@ -1529,34 +1722,72 @@ export const keepLazy = (expression: Expression) => {
|
|
|
1529
1722
|
}
|
|
1530
1723
|
};
|
|
1531
1724
|
|
|
1532
|
-
|
|
1725
|
+
// The names an expression may look up in its scope, or undefined when a subexpression selector may
|
|
1726
|
+
// look up others.
|
|
1727
|
+
const scopeNames = new WeakMap<Expression, string[] | undefined>();
|
|
1728
|
+
|
|
1729
|
+
const getScopeNames = (expression: Expression) => {
|
|
1730
|
+
if (scopeNames.has(expression)) return scopeNames.get(expression);
|
|
1731
|
+
const names = new Set<string>();
|
|
1732
|
+
const visit = (node: Expression): boolean => {
|
|
1733
|
+
if (node.kind === "Subexpression") return false;
|
|
1734
|
+
if (node.kind === "Identifier") names.add(node.name);
|
|
1735
|
+
if (node.kind === "Apply" || node.kind === "OperatorReference") names.add(node.operator);
|
|
1736
|
+
if (node.kind === "ExceptAt") names.add(EXCEPT_AT);
|
|
1737
|
+
return getChildren(node).every(visit);
|
|
1738
|
+
};
|
|
1739
|
+
const result = visit(expression) ? [...names] : undefined;
|
|
1740
|
+
scopeNames.set(expression, result);
|
|
1741
|
+
return result;
|
|
1742
|
+
};
|
|
1743
|
+
|
|
1744
|
+
// The bindings of a scope that names look up, so that a lazy function keeps only those alive and
|
|
1745
|
+
// not the values its other bindings cached. An unnamed node ends the chain, since a TLCEval in the
|
|
1746
|
+
// body sorts its value only where no context binds a name.
|
|
1747
|
+
const trimScope = (scope: Scope | undefined, names: string[] | undefined) =>
|
|
1748
|
+
names === undefined || scope === undefined
|
|
1749
|
+
? scope
|
|
1750
|
+
: names.reduce<Scope>(
|
|
1751
|
+
(trimmed, name) => {
|
|
1752
|
+
const binding = lookupScope(scope, name);
|
|
1753
|
+
return binding ? { name, binding, parent: trimmed } : trimmed;
|
|
1754
|
+
},
|
|
1755
|
+
{ name: "", binding: { kind: "Value", value: false }, parent: undefined },
|
|
1756
|
+
);
|
|
1757
|
+
|
|
1758
|
+
export const isKeptLazy = (expression: Expression) => keptLazyExpressions.has(expression);
|
|
1759
|
+
|
|
1760
|
+
// Builds the function of a constructor from its evaluated domain.
|
|
1761
|
+
export const getFunctionBuilder = (
|
|
1533
1762
|
expression: Expression & { kind: "FunctionConstruction" },
|
|
1534
1763
|
environment: Environment,
|
|
1535
|
-
)
|
|
1536
|
-
const
|
|
1764
|
+
) => {
|
|
1765
|
+
const isKeptLazyExpression = isKeptLazy(expression);
|
|
1537
1766
|
const body = getEvaluator(expression.body, environment);
|
|
1538
1767
|
const bounds = expression.bounds;
|
|
1539
1768
|
const bound = bounds[0];
|
|
1540
1769
|
const isSingleName = bounds.length === 1 && bound.pattern.kind === "Name";
|
|
1541
|
-
const set = getEvaluator(bound.set, environment);
|
|
1542
1770
|
const construct = (domain: Value, context: Context) => {
|
|
1543
1771
|
if (isSingleName && bound.pattern.kind === "Name") {
|
|
1544
1772
|
const name = bound.pattern.name;
|
|
1773
|
+
const evaluateAt = (key: Value) =>
|
|
1774
|
+
body(withScope(context, bindValue(context.scope, name, key)));
|
|
1775
|
+
// TLC sizes the domain, then evaluates the body at each element. A domain too large to hold
|
|
1776
|
+
// the values stays lazy.
|
|
1777
|
+
if (isBeyondSetBound(domain)) {
|
|
1778
|
+
getCardinality(domain);
|
|
1779
|
+
forEachElement(domain, evaluateAt);
|
|
1780
|
+
return createLazy(domain, context);
|
|
1781
|
+
}
|
|
1545
1782
|
const keys = getFunctionDomain(domain);
|
|
1546
|
-
const functionValue = createSortedFunction(
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
);
|
|
1550
|
-
// FcnLambdaValue#toFcnRcd builds a FcnRcdValue over an interval or an enumerated set.
|
|
1551
|
-
return isReducibleSet(domain) ? functionValue : markLambdaFunction(functionValue);
|
|
1783
|
+
const functionValue = createSortedFunction(keys, keys.map(evaluateAt));
|
|
1784
|
+
if (isReducibleSet(domain)) return functionValue;
|
|
1785
|
+
functionValue.lambdaDomain = domain;
|
|
1786
|
+
return markLambdaFunction(functionValue);
|
|
1552
1787
|
}
|
|
1553
1788
|
const pairs: [Value, Value][] = [];
|
|
1554
|
-
const sets = bounds
|
|
1555
|
-
getNormalizedElements(
|
|
1556
|
-
evaluate(item.set, context),
|
|
1557
|
-
item.set,
|
|
1558
|
-
context.environment.runtime.version,
|
|
1559
|
-
),
|
|
1789
|
+
const sets = mapBoundSets(bounds, (set) =>
|
|
1790
|
+
getNormalizedElements(evaluate(set, context), set, context.environment.runtime.version),
|
|
1560
1791
|
);
|
|
1561
1792
|
forEachBoundElement(bounds, sets, context.scope, (scope) => {
|
|
1562
1793
|
const key =
|
|
@@ -1569,13 +1800,17 @@ const compileFunctionConstruction = (
|
|
|
1569
1800
|
return createFunction(pairs);
|
|
1570
1801
|
};
|
|
1571
1802
|
const template = getLazyFunctionTemplate(bounds, expression.body);
|
|
1803
|
+
const boundTupleLengths = getBoundTupleLengths(bounds);
|
|
1804
|
+
const bodyNames = getScopeNames(expression.body);
|
|
1572
1805
|
const createLazy = (domain: Value, context: Context) => {
|
|
1573
|
-
const scope = context.scope;
|
|
1806
|
+
const scope = trimScope(context.scope, bodyNames);
|
|
1807
|
+
const lambdaContext = withScope(context, scope);
|
|
1574
1808
|
const functionValue = keepLambda({
|
|
1575
1809
|
value: createLazyFunction(
|
|
1576
1810
|
domain,
|
|
1577
|
-
(argument) => body(withScope(
|
|
1811
|
+
(argument) => body(withScope(lambdaContext, bindArgument(bounds, scope, argument))),
|
|
1578
1812
|
getLazyFunctionSource(template, bounds, domain),
|
|
1813
|
+
boundTupleLengths,
|
|
1579
1814
|
),
|
|
1580
1815
|
bounds,
|
|
1581
1816
|
body: expression.body,
|
|
@@ -1584,15 +1819,23 @@ const compileFunctionConstruction = (
|
|
|
1584
1819
|
return bounds.length === 1 ? markLambdaFunction(functionValue) : functionValue;
|
|
1585
1820
|
};
|
|
1586
1821
|
const callStack = environment.callStack;
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1822
|
+
const build = (domain: Value, context: Context) => {
|
|
1823
|
+
if (
|
|
1824
|
+
expression.isRecursive ||
|
|
1825
|
+
isKeptLazyExpression ||
|
|
1826
|
+
context.isKeepLazy ||
|
|
1827
|
+
!isEnumerable(domain)
|
|
1828
|
+
) {
|
|
1590
1829
|
return createLazy(domain, context);
|
|
1591
1830
|
}
|
|
1592
|
-
// TLC builds a function
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1831
|
+
// TLC builds a function lazily unless each bound is reducible, so its body fails where forced.
|
|
1832
|
+
const isReducibleDomain =
|
|
1833
|
+
bounds.length === 1
|
|
1834
|
+
? isReducibleSet(domain)
|
|
1835
|
+
: typeof domain === "object" &&
|
|
1836
|
+
domain.kind === "ProductSet" &&
|
|
1837
|
+
domain.factors.every(isReducibleSet);
|
|
1838
|
+
if (!isReducibleDomain) {
|
|
1596
1839
|
if (callStack) return createLazy(domain, context);
|
|
1597
1840
|
try {
|
|
1598
1841
|
return construct(domain, context);
|
|
@@ -1610,6 +1853,20 @@ const compileFunctionConstruction = (
|
|
|
1610
1853
|
throw error;
|
|
1611
1854
|
}
|
|
1612
1855
|
};
|
|
1856
|
+
return { build, createLazy };
|
|
1857
|
+
};
|
|
1858
|
+
|
|
1859
|
+
const compileFunctionConstruction = (
|
|
1860
|
+
expression: Expression & { kind: "FunctionConstruction" },
|
|
1861
|
+
environment: Environment,
|
|
1862
|
+
): Evaluator => {
|
|
1863
|
+
const build = getFunctionBuilder(expression, environment).build;
|
|
1864
|
+
const bounds = expression.bounds;
|
|
1865
|
+
if (bounds.length === 1 && bounds[0].pattern.kind === "Name") {
|
|
1866
|
+
const set = getEvaluator(bounds[0].set, environment);
|
|
1867
|
+
return (context) => build(set(context), context);
|
|
1868
|
+
}
|
|
1869
|
+
return (context) => build(getBoundsDomain(bounds, context), context);
|
|
1613
1870
|
};
|
|
1614
1871
|
|
|
1615
1872
|
const compileExcept = (
|
|
@@ -1673,6 +1930,101 @@ const compileExcept = (
|
|
|
1673
1930
|
};
|
|
1674
1931
|
};
|
|
1675
1932
|
|
|
1933
|
+
// TLC filters any value it can't enumerate lazily, and fails only when it uses the result.
|
|
1934
|
+
export const getLazyFilter = (
|
|
1935
|
+
expression: Expression & { kind: "SetFilter" },
|
|
1936
|
+
environment: Environment,
|
|
1937
|
+
) => {
|
|
1938
|
+
const predicate = getEvaluator(expression.predicate, environment);
|
|
1939
|
+
const pattern = expression.bound.pattern;
|
|
1940
|
+
const range = expression.predicate.location.range;
|
|
1941
|
+
const source = range && {
|
|
1942
|
+
names: pattern.kind === "Name" ? pattern.name : pattern.names.join(", "),
|
|
1943
|
+
predicate: formatRange(range),
|
|
1944
|
+
};
|
|
1945
|
+
const predicateNode = describeNode(expression.predicate);
|
|
1946
|
+
const coverage = environment.coverage;
|
|
1947
|
+
// TLC evaluates a lazy set's predicate under the set's node only as it enumerates the set,
|
|
1948
|
+
// and costs each element it tests and each it keeps.
|
|
1949
|
+
const testCovered = (node: number, isMember: boolean, test: () => Value) => {
|
|
1950
|
+
if (!coverage || node === -1) return test();
|
|
1951
|
+
if (!isMember) coverage.secondary[node]++;
|
|
1952
|
+
const value = runAtCoverageNode(coverage, isMember ? -1 : node, test);
|
|
1953
|
+
if (!isMember && value === true) coverage.secondary[node]++;
|
|
1954
|
+
return value;
|
|
1955
|
+
};
|
|
1956
|
+
const createFiltered = (
|
|
1957
|
+
base: Value,
|
|
1958
|
+
context: Context,
|
|
1959
|
+
evaluator: Evaluator,
|
|
1960
|
+
node: number,
|
|
1961
|
+
): RebindableFilteredSet => ({
|
|
1962
|
+
kind: "FilteredSet",
|
|
1963
|
+
base,
|
|
1964
|
+
test: (element, isMember) => {
|
|
1965
|
+
if (
|
|
1966
|
+
pattern.kind === "Tuple" &&
|
|
1967
|
+
getSequenceElements(element)?.length !== pattern.names.length
|
|
1968
|
+
) {
|
|
1969
|
+
throw new TlaEvaluationError(
|
|
1970
|
+
`Attempted to check if the value:\n${prettyPrint(formatValue(element))}\nis an element of a set of ${pattern.names.length}-tuples.`,
|
|
1971
|
+
);
|
|
1972
|
+
}
|
|
1973
|
+
const value = testCovered(node, isMember, () =>
|
|
1974
|
+
evaluator(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1975
|
+
);
|
|
1976
|
+
if (typeof value !== "boolean") {
|
|
1977
|
+
throw new TlaEvaluationError(
|
|
1978
|
+
`${isMember ? "The evaluation of predicate" : "Evaluating predicate"} ${predicateNode} yielded non-Boolean value.`,
|
|
1979
|
+
);
|
|
1980
|
+
}
|
|
1981
|
+
return value;
|
|
1982
|
+
},
|
|
1983
|
+
source,
|
|
1984
|
+
rebind: (tracing) =>
|
|
1985
|
+
createFiltered(
|
|
1986
|
+
base,
|
|
1987
|
+
{ ...context, environment: tracing },
|
|
1988
|
+
getEvaluator(expression.predicate, tracing),
|
|
1989
|
+
-1,
|
|
1990
|
+
),
|
|
1991
|
+
});
|
|
1992
|
+
return (base: Value, context: Context, node = -1) =>
|
|
1993
|
+
createFiltered(base, context, predicate, node);
|
|
1994
|
+
};
|
|
1995
|
+
|
|
1996
|
+
export const expectFilterPredicate = (
|
|
1997
|
+
value: Value,
|
|
1998
|
+
expression: Expression & { kind: "SetFilter" },
|
|
1999
|
+
) => {
|
|
2000
|
+
if (typeof value !== "boolean") {
|
|
2001
|
+
throw new TlaEvaluationError(
|
|
2002
|
+
`Attempted to evaluate an expression of form {x \\in S : P(x)} when P was ${getKindString(value)}.\n${describeNode(expression.predicate)}`,
|
|
2003
|
+
);
|
|
2004
|
+
}
|
|
2005
|
+
return value;
|
|
2006
|
+
};
|
|
2007
|
+
|
|
2008
|
+
export const getChoiceElements = (
|
|
2009
|
+
base: Value,
|
|
2010
|
+
expression: Expression & { kind: "Choose" },
|
|
2011
|
+
set: Expression,
|
|
2012
|
+
version: TlcVersion,
|
|
2013
|
+
) => {
|
|
2014
|
+
if (!isSetValue(base) || isOverriddenSet(base)) {
|
|
2015
|
+
throw new TlaEvaluationError(
|
|
2016
|
+
`Attempted to compute the value of an expression of\nform CHOOSE x \\in S: P, but S was not enumerable.\n${describeRange(expression)}`,
|
|
2017
|
+
);
|
|
2018
|
+
}
|
|
2019
|
+
normalizeShallowly(base);
|
|
2020
|
+
return getNormalizedElements(base, set, version);
|
|
2021
|
+
};
|
|
2022
|
+
|
|
2023
|
+
export const createNoChoiceError = (expression: Expression & { kind: "Choose" }) =>
|
|
2024
|
+
new TlaEvaluationError(
|
|
2025
|
+
`Attempted to compute the value of an expression of form\nCHOOSE x \\in S: P, but no element of S satisfied P.\n${describeRange(expression)}`,
|
|
2026
|
+
);
|
|
2027
|
+
|
|
1676
2028
|
const compileSetComprehension = (
|
|
1677
2029
|
expression: Expression & { kind: "SetFilter" | "SetMap" | "Choose" },
|
|
1678
2030
|
environment: Environment,
|
|
@@ -1680,38 +2032,25 @@ const compileSetComprehension = (
|
|
|
1680
2032
|
if (expression.kind === "SetFilter") {
|
|
1681
2033
|
const predicate = getEvaluator(expression.predicate, environment);
|
|
1682
2034
|
const set = getEvaluator(expression.bound.set, environment);
|
|
2035
|
+
const filterLazily = getLazyFilter(expression, environment);
|
|
1683
2036
|
const pattern = expression.bound.pattern;
|
|
1684
|
-
const
|
|
1685
|
-
const source = range && {
|
|
1686
|
-
names: pattern.kind === "Name" ? pattern.name : pattern.names.join(", "),
|
|
1687
|
-
predicate: formatRange(range),
|
|
1688
|
-
};
|
|
1689
|
-
const createTest = (evaluator: Evaluator, context: Context) => (element: Value) =>
|
|
1690
|
-
expectBoolean(
|
|
1691
|
-
evaluator(withScope(context, bindPattern(context.scope, pattern, element))),
|
|
1692
|
-
"a set filter predicate",
|
|
1693
|
-
);
|
|
1694
|
-
const createFiltered = (
|
|
1695
|
-
base: Value,
|
|
1696
|
-
context: Context,
|
|
1697
|
-
evaluator: Evaluator,
|
|
1698
|
-
): RebindableFilteredSet => ({
|
|
1699
|
-
kind: "FilteredSet",
|
|
1700
|
-
base,
|
|
1701
|
-
test: createTest(evaluator, context),
|
|
1702
|
-
source,
|
|
1703
|
-
rebind: (tracing) =>
|
|
1704
|
-
createFiltered(
|
|
1705
|
-
base,
|
|
1706
|
-
{ ...context, environment: tracing },
|
|
1707
|
-
getEvaluator(expression.predicate, tracing),
|
|
1708
|
-
),
|
|
1709
|
-
});
|
|
2037
|
+
const coverage = environment.coverage;
|
|
1710
2038
|
return (context) => {
|
|
1711
2039
|
const base = set(context);
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
2040
|
+
if (!isReducibleSet(base)) return filterLazily(base, context, coverage?.current);
|
|
2041
|
+
const kept: Value[] = [];
|
|
2042
|
+
const visit = (element: Value) => {
|
|
2043
|
+
const value = predicate(withScope(context, bindPattern(context.scope, pattern, element)));
|
|
2044
|
+
if (expectFilterPredicate(value, expression)) addBoundedElement(kept, element);
|
|
2045
|
+
};
|
|
2046
|
+
const elements = base.kind === "Interval" ? base : toFiniteSet(base).elements;
|
|
2047
|
+
forEachElement(elements, visit);
|
|
2048
|
+
if (coverage && coverage.current !== -1) {
|
|
2049
|
+
coverage.secondary[coverage.current] +=
|
|
2050
|
+
(Array.isArray(elements) ? elements.length : elements.high - elements.low + 1) +
|
|
2051
|
+
kept.length;
|
|
2052
|
+
}
|
|
2053
|
+
return createNormalizedSet(kept);
|
|
1715
2054
|
};
|
|
1716
2055
|
}
|
|
1717
2056
|
if (expression.kind === "SetMap") {
|
|
@@ -1719,7 +2058,7 @@ const compileSetComprehension = (
|
|
|
1719
2058
|
return (context) => {
|
|
1720
2059
|
const elements: Value[] = [];
|
|
1721
2060
|
forEachBinding(expression.bounds, context, (scope) => {
|
|
1722
|
-
elements
|
|
2061
|
+
addBoundedElement(elements, mapped(withScope(context, scope)));
|
|
1723
2062
|
return false;
|
|
1724
2063
|
});
|
|
1725
2064
|
return createEnumeratedSet(elements);
|
|
@@ -1729,37 +2068,40 @@ const compileSetComprehension = (
|
|
|
1729
2068
|
if (!set) {
|
|
1730
2069
|
return () => {
|
|
1731
2070
|
throw new TlaEvaluationError(
|
|
1732
|
-
`TLC
|
|
2071
|
+
`TLC attempted to evaluate an unbounded CHOOSE.\nMake sure that the expression is of form CHOOSE x \\in S: P(x).\n${describeRange(expression)}`,
|
|
1733
2072
|
);
|
|
1734
2073
|
};
|
|
1735
2074
|
}
|
|
1736
2075
|
const body = getEvaluator(expression.body, environment);
|
|
1737
2076
|
const evaluateSet = getEvaluator(set, environment);
|
|
1738
2077
|
return (context) => {
|
|
1739
|
-
const
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
2078
|
+
const elements = getChoiceElements(
|
|
2079
|
+
evaluateSet(context),
|
|
2080
|
+
expression,
|
|
2081
|
+
set,
|
|
2082
|
+
context.environment.runtime.version,
|
|
2083
|
+
);
|
|
1745
2084
|
const chosen = { value: undefined as Value | undefined };
|
|
1746
|
-
const
|
|
2085
|
+
const pattern = expression.pattern;
|
|
1747
2086
|
someElement(elements, (element) => {
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
2087
|
+
if (
|
|
2088
|
+
pattern.kind === "Tuple" &&
|
|
2089
|
+
getSequenceElements(element)?.length !== pattern.names.length
|
|
2090
|
+
) {
|
|
2091
|
+
throw new TlaEvaluationError(
|
|
2092
|
+
`Attempted to compute the value of an expression of form\nCHOOSE <<x1, ... , xN>> \\in S: P, but S was not a set\nof N-tuples.\n${describeRange(expression)}`,
|
|
2093
|
+
);
|
|
2094
|
+
}
|
|
2095
|
+
const scope = bindPattern(context.scope, pattern, element);
|
|
2096
|
+
if (!expectBooleanBody(body(withScope(context, scope)), expression)) return false;
|
|
2097
|
+
chosen.value = getBoundValue(pattern, scope);
|
|
1751
2098
|
return true;
|
|
1752
2099
|
});
|
|
1753
|
-
if (chosen.value === undefined)
|
|
1754
|
-
throw new TlaEvaluationError(
|
|
1755
|
-
`Attempted to compute the value of an expression of form\nCHOOSE x \\in S: P, but no element of S satisfied P.\n${describeRange(expression)}`,
|
|
1756
|
-
);
|
|
1757
|
-
}
|
|
2100
|
+
if (chosen.value === undefined) throw createNoChoiceError(expression);
|
|
1758
2101
|
return chosen.value;
|
|
1759
2102
|
};
|
|
1760
2103
|
};
|
|
1761
2104
|
|
|
1762
|
-
// TLC orders a record's fields by name.
|
|
1763
2105
|
export const getSortedFields = (fields: RecordField[]) =>
|
|
1764
2106
|
[...fields].sort((left, right) => compareValues(left.name, right.name));
|
|
1765
2107
|
|
|
@@ -1853,26 +2195,10 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1853
2195
|
}
|
|
1854
2196
|
case "Prime": {
|
|
1855
2197
|
const inner = getChildEvaluator(expression.expression);
|
|
1856
|
-
return (context) =>
|
|
1857
|
-
if (!context.next) throw createPrimeOutsideActionError(expression);
|
|
1858
|
-
return inner({
|
|
1859
|
-
environment: context.environment,
|
|
1860
|
-
scope: context.scope,
|
|
1861
|
-
state: context.next,
|
|
1862
|
-
next: undefined,
|
|
1863
|
-
isKeepLazy: context.isKeepLazy,
|
|
1864
|
-
});
|
|
1865
|
-
};
|
|
1866
|
-
}
|
|
1867
|
-
case "Unchanged": {
|
|
1868
|
-
const primed = getChildEvaluator({
|
|
1869
|
-
kind: "Prime",
|
|
1870
|
-
expression: expression.expression,
|
|
1871
|
-
location: expression.location,
|
|
1872
|
-
});
|
|
1873
|
-
const current = getChildEvaluator(expression.expression);
|
|
1874
|
-
return (context) => valuesEqual(primed(context), current(context));
|
|
2198
|
+
return (context) => inner(toNextContext(context));
|
|
1875
2199
|
}
|
|
2200
|
+
case "Unchanged":
|
|
2201
|
+
return compileUnchanged(getChildEvaluator(expression.expression));
|
|
1876
2202
|
case "If": {
|
|
1877
2203
|
const condition = getChildEvaluator(expression.condition);
|
|
1878
2204
|
const consequent = getChildEvaluator(expression.consequent);
|
|
@@ -1916,8 +2242,15 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1916
2242
|
return compileQuantifier(expression, environment);
|
|
1917
2243
|
case "UnboundedQuantifier":
|
|
1918
2244
|
return () => {
|
|
2245
|
+
const operator = expression.quantifier === "Forall" ? "\\A" : "\\E";
|
|
2246
|
+
if (expression.isTemporal) {
|
|
2247
|
+
throw createTemporalFormulaError(
|
|
2248
|
+
expression.quantifier === "Forall" ? "\\AA" : "\\EE",
|
|
2249
|
+
expression,
|
|
2250
|
+
);
|
|
2251
|
+
}
|
|
1919
2252
|
throw new TlaEvaluationError(
|
|
1920
|
-
`TLC
|
|
2253
|
+
`TLC attempted to evaluate an unbounded ${operator}.\nMake sure that the expression is of form ${operator} x \\in S: P(x).\n${describeRange(expression)}`,
|
|
1921
2254
|
);
|
|
1922
2255
|
};
|
|
1923
2256
|
case "Choose":
|
|
@@ -1993,16 +2326,18 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
1993
2326
|
case "BoxAction":
|
|
1994
2327
|
case "AngleAction": {
|
|
1995
2328
|
const action = getChildEvaluator(expression.action);
|
|
1996
|
-
const unchanged = getChildEvaluator(
|
|
1997
|
-
kind: "Unchanged",
|
|
1998
|
-
expression: expression.subscript,
|
|
1999
|
-
location: expression.location,
|
|
2000
|
-
});
|
|
2329
|
+
const unchanged = compileUnchanged(getChildEvaluator(expression.subscript));
|
|
2001
2330
|
const isBox = expression.kind === "BoxAction";
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2331
|
+
const form = isBox ? "[A]_e" : "<A>_e";
|
|
2332
|
+
return (context) => {
|
|
2333
|
+
const value = action(context);
|
|
2334
|
+
if (typeof value !== "boolean") {
|
|
2335
|
+
throw new TlaEvaluationError(
|
|
2336
|
+
`Attempted to evaluate an expression of form ${form}, but A was not a boolean.\n${describeRange(expression)}`,
|
|
2337
|
+
);
|
|
2338
|
+
}
|
|
2339
|
+
return isBox ? value || unchanged(context) : value && !unchanged(context);
|
|
2340
|
+
};
|
|
2006
2341
|
}
|
|
2007
2342
|
case "Enabled": {
|
|
2008
2343
|
const action = expression.expression;
|
|
@@ -2021,9 +2356,7 @@ const compile = (expression: Expression, environment: Environment): Evaluator =>
|
|
|
2021
2356
|
case "WeakFairness":
|
|
2022
2357
|
case "StrongFairness":
|
|
2023
2358
|
return () => {
|
|
2024
|
-
throw
|
|
2025
|
-
`TLC cannot evaluate a temporal formula in a state at ${describeLocation(expression)}`,
|
|
2026
|
-
);
|
|
2359
|
+
throw createTemporalFormulaError(TEMPORAL_FORMS[expression.kind], expression);
|
|
2027
2360
|
};
|
|
2028
2361
|
}
|
|
2029
2362
|
};
|