tla-ts 0.0.9 → 0.0.10-dev.63a6693bd

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.
Files changed (400) hide show
  1. package/README.md +29 -1
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +29 -29
  5. package/dist/deciders.js +42 -42
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +106 -10
  8. package/dist/index.d.ts +106 -10
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +117 -37
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +2 -2
  20. package/dist/transfer.cjs +129 -104
  21. package/dist/transfer.js +129 -104
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +48 -62
  26. package/src/bags.ts +99 -102
  27. package/src/builtins.ts +28 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check-failure.ts +116 -0
  30. package/src/check-trace.ts +282 -0
  31. package/src/check.ts +599 -754
  32. package/src/cli/options.ts +12 -32
  33. package/src/cli/run.ts +0 -2
  34. package/src/cli/runtime-invariants.ts +136 -0
  35. package/src/cli/shared.ts +19 -12
  36. package/src/cli/tlapm.ts +14 -25
  37. package/src/cli/tlc-arguments.ts +652 -0
  38. package/src/cli/tlc-output.ts +687 -0
  39. package/src/cli/tlc-usage.ts +361 -0
  40. package/src/cli/tlc.ts +309 -658
  41. package/src/cli/typescript.ts +23 -0
  42. package/src/cli/xml-exporter.ts +27 -49
  43. package/src/cli.ts +0 -1
  44. package/src/codegen.ts +196 -74
  45. package/src/config.ts +140 -109
  46. package/src/coverage.ts +830 -0
  47. package/src/dependencies.ts +14 -53
  48. package/src/dfid.ts +618 -0
  49. package/src/environment-config.ts +315 -0
  50. package/src/environment.ts +169 -335
  51. package/src/errors.ts +71 -55
  52. package/src/evaluate.ts +598 -340
  53. package/src/exit-codes.ts +37 -3
  54. package/src/fingerprints.ts +75 -11
  55. package/src/index.ts +3 -0
  56. package/src/instances.ts +126 -21
  57. package/src/java-hash.ts +7 -0
  58. package/src/java-overrides.ts +4 -19
  59. package/src/json-reader.ts +455 -0
  60. package/src/json.ts +252 -139
  61. package/src/levels.ts +46 -58
  62. package/src/lexer.ts +80 -100
  63. package/src/liveness-checker.ts +103 -153
  64. package/src/liveness.ts +150 -109
  65. package/src/obligations/action.ts +8 -39
  66. package/src/obligations/anonymize.ts +88 -82
  67. package/src/obligations/equality.ts +1 -1
  68. package/src/obligations/expression.ts +20 -21
  69. package/src/obligations/format.ts +1 -9
  70. package/src/obligations/generate.ts +46 -49
  71. package/src/obligations/index.ts +2 -13
  72. package/src/obligations/levels.ts +4 -8
  73. package/src/obligations/module.ts +28 -53
  74. package/src/obligations/normalize.ts +7 -25
  75. package/src/obligations/operators.ts +1 -2
  76. package/src/obligations/pragma.ts +1 -10
  77. package/src/obligations/printer.ts +47 -46
  78. package/src/obligations/proof.ts +1 -5
  79. package/src/obligations/regroup.ts +1 -4
  80. package/src/obligations/simplify.ts +42 -58
  81. package/src/obligations/source.ts +48 -78
  82. package/src/obligations/subexpression.ts +5 -11
  83. package/src/obligations/substitution.ts +1 -3
  84. package/src/obligations/temporal.ts +13 -19
  85. package/src/obligations/tuples.ts +3 -9
  86. package/src/parallel-worker.ts +30 -60
  87. package/src/parallel.ts +57 -71
  88. package/src/parser.ts +584 -316
  89. package/src/pcal/ast.ts +53 -86
  90. package/src/pcal/parse.ts +144 -219
  91. package/src/pcal/tokenize.ts +91 -131
  92. package/src/pcal/translate.ts +83 -130
  93. package/src/pcal/validate.ts +48 -68
  94. package/src/pretty-print.ts +7 -8
  95. package/src/prover/decide-pool.ts +8 -13
  96. package/src/prover/deciders.ts +3 -7
  97. package/src/prover/first-order.ts +0 -11
  98. package/src/prover/isabelle/decide.ts +0 -6
  99. package/src/prover/isabelle/encode.ts +11 -23
  100. package/src/prover/isabelle/isar/inner-syntax.ts +114 -115
  101. package/src/prover/isabelle/isar/method.ts +29 -53
  102. package/src/prover/isabelle/isar/proof.ts +1 -10
  103. package/src/prover/isabelle/isar/statement.ts +1 -6
  104. package/src/prover/isabelle/isar/theory.ts +26 -36
  105. package/src/prover/isabelle/isar/type-check.ts +17 -31
  106. package/src/prover/isabelle/provers/blast/blast.ts +17 -104
  107. package/src/prover/isabelle/provers/blast/branch.ts +1 -32
  108. package/src/prover/isabelle/provers/blast/prove.ts +1 -35
  109. package/src/prover/isabelle/provers/blast/rules.ts +6 -44
  110. package/src/prover/isabelle/provers/blast/state.ts +2 -28
  111. package/src/prover/isabelle/provers/blast/term.ts +9 -36
  112. package/src/prover/isabelle/provers/clasimp.ts +1 -13
  113. package/src/prover/isabelle/provers/classical.ts +14 -42
  114. package/src/prover/isabelle/provers/hypsubst.ts +28 -51
  115. package/src/prover/isabelle/pure/bicompose.ts +25 -38
  116. package/src/prover/isabelle/pure/consts.ts +1 -7
  117. package/src/prover/isabelle/pure/conv.ts +3 -17
  118. package/src/prover/isabelle/pure/drule.ts +33 -42
  119. package/src/prover/isabelle/pure/envir.ts +3 -52
  120. package/src/prover/isabelle/pure/goal.ts +1 -7
  121. package/src/prover/isabelle/pure/interrupt.ts +0 -5
  122. package/src/prover/isabelle/pure/list.ts +0 -3
  123. package/src/prover/isabelle/pure/logic.ts +2 -15
  124. package/src/prover/isabelle/pure/name.ts +2 -9
  125. package/src/prover/isabelle/pure/net.ts +3 -59
  126. package/src/prover/isabelle/pure/object-logic.ts +3 -10
  127. package/src/prover/isabelle/pure/pattern.ts +2 -19
  128. package/src/prover/isabelle/pure/raw-simplifier.ts +17 -62
  129. package/src/prover/isabelle/pure/rewriter.ts +31 -67
  130. package/src/prover/isabelle/pure/search.ts +3 -10
  131. package/src/prover/isabelle/pure/seq.ts +1 -10
  132. package/src/prover/isabelle/pure/simplifier.ts +4 -12
  133. package/src/prover/isabelle/pure/table.ts +3 -32
  134. package/src/prover/isabelle/pure/tactic.ts +12 -22
  135. package/src/prover/isabelle/pure/tactical.ts +48 -47
  136. package/src/prover/isabelle/pure/term-ord.ts +2 -6
  137. package/src/prover/isabelle/pure/term.ts +2 -43
  138. package/src/prover/isabelle/pure/thm.ts +27 -47
  139. package/src/prover/isabelle/pure/type.ts +2 -15
  140. package/src/prover/isabelle/pure/unify.ts +20 -30
  141. package/src/prover/isabelle/pure/variable.ts +3 -7
  142. package/src/prover/isabelle/tla/classical-setup.ts +17 -24
  143. package/src/prover/isabelle/tla/context.ts +15 -18
  144. package/src/prover/isabelle/tla/logic.ts +14 -32
  145. package/src/prover/isabelle/tla/simpdata.ts +46 -43
  146. package/src/prover/isabelle/tools/intuitionistic.ts +1 -10
  147. package/src/prover/ocaml.ts +0 -6
  148. package/src/prover/prep/action.ts +14 -54
  149. package/src/prover/prep/enabled-axioms.ts +12 -19
  150. package/src/prover/prep/fingerprint.ts +2 -21
  151. package/src/prover/prep/level-comparison.ts +8 -12
  152. package/src/prover/prep/md5.ts +0 -2
  153. package/src/prover/prep/methods.ts +33 -39
  154. package/src/prover/prep/normalize.ts +4 -28
  155. package/src/prover/prove-worker.ts +0 -2
  156. package/src/prover/prove.ts +87 -100
  157. package/src/prover/ptl/decide.ts +7 -12
  158. package/src/prover/ptl/encode.ts +14 -36
  159. package/src/prover/ptl/sat.ts +24 -32
  160. package/src/prover/ptl/tableau.ts +12 -26
  161. package/src/prover/smt/axioms.ts +0 -7
  162. package/src/prover/smt/context.ts +20 -28
  163. package/src/prover/smt/data.ts +433 -424
  164. package/src/prover/smt/encode.ts +43 -52
  165. package/src/prover/smt/extensionality.ts +0 -10
  166. package/src/prover/smt/flatten.ts +3 -22
  167. package/src/prover/smt/format.ts +5 -10
  168. package/src/prover/smt/instances.ts +0 -10
  169. package/src/prover/smt/rewrite.ts +663 -667
  170. package/src/prover/smt/solver/act-cache.ts +0 -15
  171. package/src/prover/smt/solver/arith-rewriter.ts +3 -48
  172. package/src/prover/smt/solver/array.ts +1 -3
  173. package/src/prover/smt/solver/asserted-formulas.ts +1 -43
  174. package/src/prover/smt/solver/ast-lt.ts +1 -11
  175. package/src/prover/smt/solver/ast-util.ts +1 -6
  176. package/src/prover/smt/solver/ast.ts +1 -165
  177. package/src/prover/smt/solver/auf-solver.ts +8 -44
  178. package/src/prover/smt/solver/case-split-queue.ts +1 -9
  179. package/src/prover/smt/solver/cg-table.ts +1 -18
  180. package/src/prover/smt/solver/clause.ts +1 -27
  181. package/src/prover/smt/solver/conflict-resolution.ts +0 -9
  182. package/src/prover/smt/solver/context.ts +7 -64
  183. package/src/prover/smt/solver/ctx-simplify.ts +14 -41
  184. package/src/prover/smt/solver/der.ts +2 -21
  185. package/src/prover/smt/solver/dyn-ack.ts +1 -11
  186. package/src/prover/smt/solver/e-matching.ts +4 -35
  187. package/src/prover/smt/solver/enode.ts +1 -21
  188. package/src/prover/smt/solver/expr-ref.ts +1 -5
  189. package/src/prover/smt/solver/expr-safe-replace.ts +1 -6
  190. package/src/prover/smt/solver/fingerprints.ts +1 -12
  191. package/src/prover/smt/solver/flat-literals.ts +1 -9
  192. package/src/prover/smt/solver/goal.ts +2 -14
  193. package/src/prover/smt/solver/hash.ts +0 -8
  194. package/src/prover/smt/solver/hashtable.ts +1 -12
  195. package/src/prover/smt/solver/internalizer.ts +7 -100
  196. package/src/prover/smt/solver/lbool.ts +1 -2
  197. package/src/prover/smt/solver/literal.ts +0 -3
  198. package/src/prover/smt/solver/lp/bound-propagator.ts +8 -26
  199. package/src/prover/smt/solver/lp/constraints.ts +2 -8
  200. package/src/prover/smt/solver/lp/containers.ts +12 -30
  201. package/src/prover/smt/solver/lp/core-solver.ts +10 -31
  202. package/src/prover/smt/solver/lp/cross-nested.ts +6 -20
  203. package/src/prover/smt/solver/lp/dep-intervals.ts +7 -22
  204. package/src/prover/smt/solver/lp/dependency.ts +1 -4
  205. package/src/prover/smt/solver/lp/dioph-eq.ts +9 -102
  206. package/src/prover/smt/solver/lp/explanation.ts +1 -6
  207. package/src/prover/smt/solver/lp/gomory.ts +9 -22
  208. package/src/prover/smt/solver/lp/hnf-cutter.ts +17 -23
  209. package/src/prover/smt/solver/lp/hnf.ts +11 -26
  210. package/src/prover/smt/solver/lp/int-cube.ts +15 -36
  211. package/src/prover/smt/solver/lp/int-solver.ts +4 -34
  212. package/src/prover/smt/solver/lp/lar-solver.ts +32 -81
  213. package/src/prover/smt/solver/lp/lar-term.ts +1 -5
  214. package/src/prover/smt/solver/lp/lp-settings.ts +1 -14
  215. package/src/prover/smt/solver/lp/maximize-term.ts +20 -45
  216. package/src/prover/smt/solver/lp/nex-creator.ts +5 -28
  217. package/src/prover/smt/solver/lp/nex.ts +1 -6
  218. package/src/prover/smt/solver/lp/nla-backoff.ts +1 -4
  219. package/src/prover/smt/solver/lp/nla-basics.ts +5 -30
  220. package/src/prover/smt/solver/lp/nla-core.ts +36 -135
  221. package/src/prover/smt/solver/lp/nla-divisions.ts +32 -42
  222. package/src/prover/smt/solver/lp/nla-emonics.ts +4 -21
  223. package/src/prover/smt/solver/lp/nla-factorization.ts +1 -4
  224. package/src/prover/smt/solver/lp/nla-grobner.ts +15 -69
  225. package/src/prover/smt/solver/lp/nla-horner.ts +3 -11
  226. package/src/prover/smt/solver/lp/nla-intervals.ts +2 -12
  227. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +16 -41
  228. package/src/prover/smt/solver/lp/nla-monotone.ts +2 -6
  229. package/src/prover/smt/solver/lp/nla-order.ts +2 -13
  230. package/src/prover/smt/solver/lp/nla-patcher.ts +4 -7
  231. package/src/prover/smt/solver/lp/nla-tangents.ts +1 -9
  232. package/src/prover/smt/solver/lp/nla-throttle.ts +1 -6
  233. package/src/prover/smt/solver/lp/nla-types.ts +1 -11
  234. package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -25
  235. package/src/prover/smt/solver/lp/nra-solver.ts +5 -52
  236. package/src/prover/smt/solver/lp/numeric-pair.ts +1 -4
  237. package/src/prover/smt/solver/lp/pdd-interval.ts +1 -3
  238. package/src/prover/smt/solver/lp/pdd-simplifier.ts +2 -14
  239. package/src/prover/smt/solver/lp/pdd-solver.ts +4 -33
  240. package/src/prover/smt/solver/lp/pdd.ts +7 -70
  241. package/src/prover/smt/solver/lp/random-updater.ts +1 -8
  242. package/src/prover/smt/solver/lp/static-matrix.ts +9 -19
  243. package/src/prover/smt/solver/lp/union-find.ts +1 -6
  244. package/src/prover/smt/solver/mam-compiler.ts +18 -58
  245. package/src/prover/smt/solver/mam-interpreter.ts +1 -21
  246. package/src/prover/smt/solver/maximize-ac-sharing.ts +2 -17
  247. package/src/prover/smt/solver/model-checker.ts +3 -37
  248. package/src/prover/smt/solver/model-evaluator.ts +3 -29
  249. package/src/prover/smt/solver/model-finder.ts +15 -32
  250. package/src/prover/smt/solver/model-generator.ts +1 -12
  251. package/src/prover/smt/solver/model-macro-solver.ts +23 -88
  252. package/src/prover/smt/solver/nlsat/algebraic.ts +63 -159
  253. package/src/prover/smt/solver/nlsat/atoms.ts +25 -23
  254. package/src/prover/smt/solver/nlsat/evaluator.ts +17 -22
  255. package/src/prover/smt/solver/nlsat/explain.ts +10 -69
  256. package/src/prover/smt/solver/nlsat/interval-set.ts +9 -50
  257. package/src/prover/smt/solver/nlsat/levelwise.ts +49 -135
  258. package/src/prover/smt/solver/nlsat/mpbq.ts +3 -14
  259. package/src/prover/smt/solver/nlsat/polynomial.ts +29 -159
  260. package/src/prover/smt/solver/nlsat/solver.ts +18 -90
  261. package/src/prover/smt/solver/nlsat/types.ts +1 -8
  262. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +2 -45
  263. package/src/prover/smt/solver/nlsat/upolynomial.ts +11 -92
  264. package/src/prover/smt/solver/nnf.ts +3 -38
  265. package/src/prover/smt/solver/pattern-inference.ts +2 -15
  266. package/src/prover/smt/solver/poly-rewriter.ts +3 -23
  267. package/src/prover/smt/solver/propagate-values.ts +1 -10
  268. package/src/prover/smt/solver/proto-model.ts +3 -52
  269. package/src/prover/smt/solver/pull-quant.ts +1 -12
  270. package/src/prover/smt/solver/qe-lite-der.ts +1 -29
  271. package/src/prover/smt/solver/qe-lite-fm.ts +2 -25
  272. package/src/prover/smt/solver/qe-lite.ts +3 -33
  273. package/src/prover/smt/solver/qi-queue.ts +1 -16
  274. package/src/prover/smt/solver/quant-tactics.ts +5 -22
  275. package/src/prover/smt/solver/quantifier-analyzer.ts +27 -47
  276. package/src/prover/smt/solver/quantifier-manager.ts +2 -13
  277. package/src/prover/smt/solver/random-gen.ts +1 -2
  278. package/src/prover/smt/solver/rational.ts +1 -8
  279. package/src/prover/smt/solver/ref-vector.ts +1 -6
  280. package/src/prover/smt/solver/relevancy.ts +2 -15
  281. package/src/prover/smt/solver/rewriter-tpl.ts +3 -147
  282. package/src/prover/smt/solver/rewriter.ts +3 -85
  283. package/src/prover/smt/solver/rlimit.ts +2 -20
  284. package/src/prover/smt/solver/smtlib-parser.ts +80 -131
  285. package/src/prover/smt/solver/solver.ts +23 -34
  286. package/src/prover/smt/solver/sort.ts +1 -5
  287. package/src/prover/smt/solver/sticky-generations.ts +0 -5
  288. package/src/prover/smt/solver/theory-lra-axioms.ts +3 -24
  289. package/src/prover/smt/solver/theory-lra-bounds.ts +1 -7
  290. package/src/prover/smt/solver/theory-lra-final-check.ts +4 -22
  291. package/src/prover/smt/solver/theory-lra-internalize.ts +6 -43
  292. package/src/prover/smt/solver/theory-lra-model.ts +4 -19
  293. package/src/prover/smt/solver/theory-lra-propagate.ts +1 -15
  294. package/src/prover/smt/solver/theory-lra-state.ts +3 -8
  295. package/src/prover/smt/solver/theory-lra.ts +1 -8
  296. package/src/prover/smt/solver/var-subst.ts +2 -26
  297. package/src/prover/smt/table.ts +0 -8
  298. package/src/prover/tlapm/cache.ts +45 -40
  299. package/src/prover/tlapm/ocaml-marshal.ts +19 -21
  300. package/src/prover/tlapm/tlapm.ts +21 -20
  301. package/src/prover/verdict.ts +7 -2
  302. package/src/prover/zenon/check/check.ts +1 -11
  303. package/src/prover/zenon/check/definitions.ts +5 -15
  304. package/src/prover/zenon/check/equality.ts +0 -8
  305. package/src/prover/zenon/check/functions.ts +0 -4
  306. package/src/prover/zenon/check/keys.ts +0 -7
  307. package/src/prover/zenon/check/propositional.ts +0 -3
  308. package/src/prover/zenon/check/records.ts +1 -6
  309. package/src/prover/zenon/check/relations.ts +2 -14
  310. package/src/prover/zenon/check/rewrites.ts +2 -21
  311. package/src/prover/zenon/check/sets.ts +40 -61
  312. package/src/prover/zenon/check/terms.ts +1 -19
  313. package/src/prover/zenon/check/tla.ts +0 -6
  314. package/src/prover/zenon/decide.ts +0 -12
  315. package/src/prover/zenon/encode.ts +1 -8
  316. package/src/prover/zenon/eqrel.ts +2 -13
  317. package/src/prover/zenon/equality-rules.ts +0 -4
  318. package/src/prover/zenon/expr.ts +8 -26
  319. package/src/prover/zenon/index-table.ts +26 -57
  320. package/src/prover/zenon/isar-case.ts +9 -19
  321. package/src/prover/zenon/isar.ts +36 -64
  322. package/src/prover/zenon/limits.ts +0 -4
  323. package/src/prover/zenon/llproof.ts +2 -20
  324. package/src/prover/zenon/mltoll.ts +66 -104
  325. package/src/prover/zenon/node.ts +1 -11
  326. package/src/prover/zenon/ocaml.ts +1 -17
  327. package/src/prover/zenon/parse.ts +4 -12
  328. package/src/prover/zenon/phrase.ts +1 -11
  329. package/src/prover/zenon/proof.ts +0 -9
  330. package/src/prover/zenon/propositional-rules.ts +0 -5
  331. package/src/prover/zenon/rules.ts +2 -6
  332. package/src/prover/zenon/search.ts +5 -31
  333. package/src/prover/zenon/tla/functions.ts +59 -31
  334. package/src/prover/zenon/tla/llproof.ts +67 -76
  335. package/src/prover/zenon/tla/records.ts +57 -33
  336. package/src/prover/zenon/tla/rewrites.ts +12 -19
  337. package/src/prover/zenon/tla/sets.ts +83 -53
  338. package/src/prover/zenon/tla/terms.ts +0 -6
  339. package/src/prover/zenon/tla/tla.ts +66 -64
  340. package/src/randomization.ts +81 -35
  341. package/src/resolve.ts +67 -184
  342. package/src/runtime.ts +22 -22
  343. package/src/sany/builtins.ts +2 -5
  344. package/src/sany/generation.ts +394 -0
  345. package/src/sany/generator-def-entries.ts +210 -0
  346. package/src/sany/generator-definitions.ts +686 -0
  347. package/src/sany/generator-expressions.ts +1100 -0
  348. package/src/sany/generator-proofs.ts +662 -0
  349. package/src/sany/generator.ts +191 -2764
  350. package/src/sany/levels.ts +6 -34
  351. package/src/sany/lexer.ts +58 -77
  352. package/src/sany/linter.ts +1 -9
  353. package/src/sany/modules.ts +1 -5
  354. package/src/sany/nodes.ts +5 -43
  355. package/src/sany/parse-error.ts +39 -117
  356. package/src/sany/report.ts +32 -69
  357. package/src/sany/selectors.ts +49 -76
  358. package/src/sany/source.ts +1 -17
  359. package/src/sany/xml.ts +35 -117
  360. package/src/search.ts +541 -163
  361. package/src/simulate.ts +709 -0
  362. package/src/spec-description.ts +192 -100
  363. package/src/standard-definitions.ts +0 -4
  364. package/src/standard-modules.ts +264 -95
  365. package/src/state-graph.ts +52 -31
  366. package/src/subexpressions.ts +69 -52
  367. package/src/substitution-levels.ts +7 -7
  368. package/src/successors.ts +266 -99
  369. package/src/symmetry.ts +33 -25
  370. package/src/tableau.ts +27 -24
  371. package/src/tlc/fingerprint.ts +183 -67
  372. package/src/tlc/jar-data.ts +112 -6
  373. package/src/tlc/messages.ts +125 -54
  374. package/src/tlc/order.ts +39 -69
  375. package/src/tlc/random.ts +37 -49
  376. package/src/tlc/replay.ts +553 -228
  377. package/src/tlc/summary.ts +36 -47
  378. package/src/tlc/tlc-operators.ts +78 -44
  379. package/src/tlc/tlcext-operators.ts +139 -122
  380. package/src/tlc/trace-spec.ts +249 -0
  381. package/src/tlc/value-stream.ts +139 -0
  382. package/src/tlc/version.ts +1 -4
  383. package/src/transfer.ts +22 -25
  384. package/src/typescript/actions.ts +479 -0
  385. package/src/typescript/collections.ts +614 -0
  386. package/src/typescript/compile.ts +63 -0
  387. package/src/typescript/context.ts +94 -0
  388. package/src/typescript/emit.ts +121 -0
  389. package/src/typescript/expressions.ts +496 -0
  390. package/src/typescript/formula.ts +154 -0
  391. package/src/typescript/functions.ts +284 -0
  392. package/src/typescript/model.ts +162 -0
  393. package/src/typescript/names.ts +114 -0
  394. package/src/typescript/program.ts +48 -0
  395. package/src/typescript/properties.ts +139 -0
  396. package/src/typescript/scope.ts +112 -0
  397. package/src/typescript/steps.ts +167 -0
  398. package/src/typescript/types.ts +171 -0
  399. package/src/values.ts +832 -484
  400. package/src/worker.ts +7 -8
@@ -1,5 +1,3 @@
1
- // Proves a module's obligations with tla-ts's own backends, in tlapm's order, or with tlapm.
2
-
3
1
  import { TlaProverError, TlaUnsupportedError } from "../errors";
4
2
  import { getChildren, mapHyp, type Expr, type Hyp, type Sequent } from "../obligations/expression";
5
3
  import { getObligations, type PrintedObligation } from "../obligations";
@@ -17,14 +15,14 @@ import {
17
15
  normalizeExpand,
18
16
  normalizeSequent,
19
17
  } from "./prep/normalize";
20
- import { findMethods, type Method } from "./prep/methods";
18
+ import { findMethods, getBudget, type Method } from "./prep/methods";
21
19
  import { encodePtl } from "./ptl/encode";
22
20
  import { encodeSmt } from "./smt/encode";
23
21
  import type { EncodingSession } from "./smt/rewrite";
24
22
  import { encodeZenon } from "./zenon/encode";
25
23
  import type { TlapmResult } from "./tlapm/tlapm";
26
24
  import { TLAPS_MODULE } from "./tlaps-module";
27
- import type { Verdict } from "./verdict";
25
+ import { isBackend, type Backend, type Verdict } from "./verdict";
28
26
 
29
27
  interface ObligationBase {
30
28
  id: number;
@@ -65,71 +63,63 @@ export interface ProveOptions {
65
63
  workers?: number;
66
64
  }
67
65
 
68
- // What one method concluded about an obligation, as tlapm's toolbox messages report it.
69
- export interface ProofAttempt {
70
- method: Method;
71
- isProved: boolean;
72
- isReused: boolean;
73
- reason?: string;
74
- verdict?: Verdict;
75
- timeUsedMs?: number;
76
- }
66
+ export type ProofAttempt =
67
+ | { kind: "decided"; method: Method; verdict: Verdict; timeUsedMs: number }
68
+ | { kind: "error"; method: Method }
69
+ | { kind: "skipped"; method: Method; reason: string }
70
+ | { kind: "stored" | "reused"; method: Method; isProved: boolean; reason?: string };
77
71
 
78
72
  interface ProofReport {
79
73
  obligation: ProofObligation;
80
74
  attempts: ProofAttempt[];
81
75
  }
82
76
 
83
- // Why a method left an obligation unproved: a prover decided so, or tla-ts couldn't try it.
84
77
  interface MethodFailure {
85
78
  reason: string;
86
79
  isDecided: boolean;
87
80
  }
88
81
 
89
- // The SMT encoder numbers the CASE errors it standardizes across the run, so an obligation it
90
- // encodes one in waits for every earlier obligation and starts from the count they leave.
91
82
  interface ObligationTask {
83
+ report: Promise<ProofReport>;
92
84
  done: Promise<void>;
85
+ isSettled: boolean;
93
86
  session: { initial: number; current: EncodingSession } | undefined;
94
87
  }
95
88
 
96
- // What tlapm keeps across one run's obligations. An obligation waits for the last earlier one with
97
- // its fingerprint, so obligations prove concurrently as they would in turn.
98
89
  interface ProveRun {
99
90
  tasks: ObligationTask[];
100
91
  results: Map<string, Map<string, ProofAttempt>>;
101
92
  isFingerprinted: (obligation: PrintedObligation) => boolean;
102
93
  isProving: boolean;
103
- lastByFingerprint: Map<string, Promise<void>>;
94
+ lastByFingerprint: Map<string, number>;
104
95
  decide: DecidePool["decide"];
105
96
  }
106
97
 
107
- // The text Prep.ship sends a backend after Prep.normalize_expand. It throws a TlaUnsupportedError
108
- // for what tla-ts can't encode.
109
- type Encoder = (id: number, sequent: Sequent, method: Method, session: EncodingSession) => string;
98
+ interface Encoder {
99
+ encode: (id: number, sequent: Sequent, method: Method, session: EncodingSession) => string;
100
+ isCaseNumbered?: boolean;
101
+ }
110
102
 
111
- // Action.process_obligation, the frontend for every backend but LS4, Isabelle included, then
112
- // Prep.flatten.
113
103
  const shipFirstOrder = (sequent: Sequent) => flatten(processObligation(sequent));
114
104
 
115
- // Prep.gen_smt_solve writes the resource budget before the encoding.
116
- const encodeSmtProblem: Encoder = (_id, sequent, method, session) =>
117
- (method.rlimit === undefined ? "" : `(set-option :rlimit ${method.rlimit})\n`) +
118
- encodeSmt(shipFirstOrder(sequent), session);
119
-
120
- const ENCODERS: Record<string, Encoder> = {
121
- smt: encodeSmtProblem,
122
- z3: encodeSmtProblem,
123
- zenon: (id, sequent) => encodeZenon(id, shipFirstOrder(sequent)),
124
- // tlapm names the theory after its temporary file, and tla-ts after the obligation.
125
- isabelle: (id, sequent, method) =>
126
- encodeIsabelle(id, shipFirstOrder(sequent), method.tactic ?? "auto"),
127
- // Pltl.process_obligation is LS4's frontend.
128
- ls4: (_id, sequent) => encodePtl(sequent),
105
+ const SMT_ENCODER: Encoder = {
106
+ encode: (_id, sequent, method, session) =>
107
+ (method.rlimit === undefined ? "" : `(set-option :rlimit ${method.rlimit})\n`) +
108
+ encodeSmt(shipFirstOrder(sequent), session),
109
+ isCaseNumbered: true,
110
+ };
111
+
112
+ const ENCODERS: Record<Backend, Encoder> = {
113
+ smt: SMT_ENCODER,
114
+ z3: SMT_ENCODER,
115
+ zenon: { encode: (id, sequent) => encodeZenon(id, shipFirstOrder(sequent)) },
116
+ isabelle: {
117
+ encode: (id, sequent, method) =>
118
+ encodeIsabelle(id, shipFirstOrder(sequent), method.tactic ?? "auto"),
119
+ },
120
+ ls4: { encode: (_id, sequent) => encodePtl(sequent) },
129
121
  };
130
122
 
131
- // The frontends and Prep.flatten add no CASE without OTHER, so the SMT encoder standardizes one
132
- // only in an obligation that has one after Prep.normalize_expand.
133
123
  const hasIncompleteCase = (expression: Expr): boolean =>
134
124
  (expression.kind === "Case" && expression.other === undefined) ||
135
125
  getChildren(expression).some(hasIncompleteCase);
@@ -168,8 +158,23 @@ const getUnprovedReport = (
168
158
  attempts: ProofAttempt[] = [],
169
159
  ): ProofReport => ({ obligation: { ...getObligationBase(obligation), status, reason }, attempts });
170
160
 
171
- // A failed obligation as tlapm prints it: Prep.normalize_expand for a new failure, and
172
- // Toolbox.normalize, which keeps pragmas, for a reused one.
161
+ export const isProvedAttempt = (attempt: ProofAttempt) =>
162
+ attempt.kind === "decided"
163
+ ? attempt.verdict === "unsat"
164
+ : (attempt.kind === "stored" || attempt.kind === "reused") && attempt.isProved;
165
+
166
+ export const getFailureReason = (attempt: ProofAttempt) => {
167
+ switch (attempt.kind) {
168
+ case "decided":
169
+ if (attempt.verdict === "unsat") return undefined;
170
+ return attempt.timeUsedMs >= attempt.method.timeLimitMs ? "timeout" : "false";
171
+ case "error":
172
+ return "false";
173
+ default:
174
+ return attempt.reason;
175
+ }
176
+ };
177
+
173
178
  export const formatNormalized = (obligation: PrintedObligation, isReused: boolean) =>
174
179
  `${formatObligation(
175
180
  isReused
@@ -177,7 +182,6 @@ export const formatNormalized = (obligation: PrintedObligation, isReused: boolea
177
182
  : normalizeExpand(obligation),
178
183
  )}\n`;
179
184
 
180
- // The theory of the proofs Isabelle found. tlapm also writes Zenon's proofs there.
181
185
  export const encodeProofTheory = (
182
186
  name: string,
183
187
  reports: ProofReport[],
@@ -189,7 +193,9 @@ export const encodeProofTheory = (
189
193
  report.attempts
190
194
  .filter(
191
195
  (attempt) =>
192
- attempt.method.prover === "isabelle" && attempt.isProved && !attempt.isReused,
196
+ attempt.method.prover === "isabelle" &&
197
+ attempt.kind === "decided" &&
198
+ attempt.verdict === "unsat",
193
199
  )
194
200
  .map((attempt) => ({
195
201
  id: report.obligation.id,
@@ -199,24 +205,24 @@ export const encodeProofTheory = (
199
205
  ),
200
206
  );
201
207
 
202
- // The text one backend receives for an obligation, for comparing with tlapm's.
203
208
  export const encodeForBackend = (
204
209
  obligation: PrintedObligation,
205
- prover: string,
210
+ prover: Backend,
206
211
  session: EncodingSession,
207
212
  ) => {
208
- const encode = ENCODERS[prover];
209
- if (!encode) throw new TlaUnsupportedError(`the ${prover} backend`);
210
213
  const methods = findMethods(obligation);
211
214
  const method = methods.provers.find((candidate) => candidate.prover === prover) ?? {
212
215
  prover,
213
216
  timeLimitMs: Infinity,
214
217
  };
215
- return encode(obligation.id, normalizeExpand(obligation, methods), method, session);
218
+ return ENCODERS[prover].encode(
219
+ obligation.id,
220
+ normalizeExpand(obligation, methods),
221
+ method,
222
+ session,
223
+ );
216
224
  };
217
225
 
218
- // The SMT encoder's session for an obligation, from the count of CASE errors every earlier
219
- // obligation leaves.
220
226
  const getSession = async (run: ProveRun, index: number) => {
221
227
  const task = run.tasks[index];
222
228
  if (task.session) return task.session.current;
@@ -232,10 +238,6 @@ const getSession = async (run: ProveRun, index: number) => {
232
238
  return task.session.current;
233
239
  };
234
240
 
235
- // Prep.ship: the provers the step cites, or tlapm's default ones when it cites none, each tried in
236
- // turn until one proves the obligation. Prep.already_processed reuses what a prover concluded for
237
- // an earlier obligation with the same fingerprint: a proof, or a failure within at least as much
238
- // time. Unless the first prover's proof is reused, a trivial obligation needs no prover.
239
241
  const proveWithBackends = async (
240
242
  obligation: PrintedObligation,
241
243
  index: number,
@@ -247,24 +249,26 @@ const proveWithBackends = async (
247
249
  const methods = findMethods(obligation);
248
250
  const fingerprint = getFingerprint(obligation.sequent);
249
251
  const earlier = run.lastByFingerprint.get(fingerprint);
250
- run.lastByFingerprint.set(fingerprint, run.tasks[index].done);
251
- await earlier;
252
+ run.lastByFingerprint.set(fingerprint, index);
253
+ await (earlier === undefined ? undefined : run.tasks[earlier].done);
252
254
  const attempts: ProofAttempt[] = [];
253
255
  const failures: MethodFailure[] = [];
254
256
  const getProvedReport = (prover: string): ProofReport => ({
255
257
  obligation: { ...getObligationBase(obligation), status: "proved", prover },
256
258
  attempts,
257
259
  });
258
- // Prep.already_processed, which forgets the fingerprint's results on each call under --nofp.
259
260
  const getPrior = (method: Method) => {
260
261
  if (!run.isFingerprinted(obligation)) run.results.delete(fingerprint);
261
262
  const prior = run.results.get(fingerprint)?.get(method.prover);
262
263
  return prior?.method.tactic === method.tactic ? prior : undefined;
263
264
  };
264
- // Fpfile.get_timeout.
265
- const getBudget = (method: Method) => method.rlimit ?? method.timeLimitMs / 1000;
266
265
  const reuse = (prior: ProofAttempt) =>
267
- attempts.push({ ...prior, isReused: true, timeUsedMs: undefined });
266
+ attempts.push({
267
+ kind: "reused",
268
+ method: prior.method,
269
+ isProved: isProvedAttempt(prior),
270
+ reason: getFailureReason(prior),
271
+ });
268
272
  const record = (attempt: ProofAttempt) => {
269
273
  attempts.push(attempt);
270
274
  run.results.set(
@@ -273,23 +277,22 @@ const proveWithBackends = async (
273
277
  );
274
278
  };
275
279
  const skip = (method: Method, reason: string) => {
276
- attempts.push({ method, isProved: false, isReused: false, reason });
280
+ attempts.push({ kind: "skipped", method, reason });
277
281
  failures.push({ reason, isDecided: false });
278
282
  };
279
283
  const firstPrior = getPrior(methods.provers[0]);
280
- if (firstPrior?.isProved) {
284
+ if (firstPrior && isProvedAttempt(firstPrior)) {
281
285
  reuse(firstPrior);
282
286
  return getProvedReport(firstPrior.method.prover);
283
287
  }
284
288
  const sequent = normalizeExpand(obligation, methods);
285
- // Fpfile.fp_writes keeps no trivial result, but adds the fingerprint.
286
289
  if (obligation.kind.kind === "Support" && isTrivial(sequent)) {
287
290
  run.results.set(fingerprint, run.results.get(fingerprint) ?? new Map());
288
291
  return { obligation: { ...getObligationBase(obligation), status: "trivial" }, attempts };
289
292
  }
290
293
  for (const method of methods.provers) {
291
294
  const prior = getPrior(method);
292
- if (prior?.isProved) {
295
+ if (prior && isProvedAttempt(prior)) {
293
296
  reuse(prior);
294
297
  return getProvedReport(method.prover);
295
298
  }
@@ -302,41 +305,30 @@ const proveWithBackends = async (
302
305
  continue;
303
306
  }
304
307
  if (!run.isProving) continue;
305
- // Prep.prove_with: the Trivial method proves anything.
306
308
  if (method.prover === "trivial") return getProvedReport(method.prover);
307
- const encode = ENCODERS[method.prover];
308
- if (!encode) {
309
+ if (!isBackend(method.prover)) {
309
310
  skip(method, `tla-ts has no ${method.prover} backend`);
310
311
  continue;
311
312
  }
312
313
  try {
314
+ const encoder = ENCODERS[method.prover];
313
315
  const isCaseNumbered =
314
- encode === encodeSmtProblem &&
316
+ encoder.isCaseNumbered &&
315
317
  (sequent.context.some(isIncompleteCaseHyp) || hasIncompleteCase(sequent.active));
316
318
  const session = isCaseNumbered ? await getSession(run, index) : { caseErrors: 0 };
317
- const text = encode(obligation.id, sequent, method, session);
319
+ const text = encoder.encode(obligation.id, sequent, method, session);
318
320
  const decision = await run.decide(
319
321
  method.prover,
320
322
  text,
321
323
  { timeLimitMs: method.timeLimitMs, signal },
322
324
  index,
323
325
  );
324
- const isProved = decision.verdict === "unsat";
325
- record({
326
- method,
327
- isProved,
328
- isReused: false,
329
- ...(!isProved && {
330
- reason: decision.timeUsedMs >= method.timeLimitMs ? "timeout" : "false",
331
- }),
332
- verdict: decision.verdict,
333
- timeUsedMs: decision.timeUsedMs,
334
- });
335
- if (isProved) return getProvedReport(method.prover);
326
+ record({ kind: "decided", method, ...decision });
327
+ if (decision.verdict === "unsat") return getProvedReport(method.prover);
336
328
  failures.push({ reason: `${method.prover} answered ${decision.verdict}`, isDecided: true });
337
329
  } catch (error) {
338
330
  if (error instanceof TlaProverError) {
339
- record({ method, isProved: false, isReused: false, reason: "false" });
331
+ record({ kind: "error", method });
340
332
  failures.push({ reason: `${method.prover} failed: ${error.message}`, isDecided: true });
341
333
  continue;
342
334
  }
@@ -352,8 +344,6 @@ const proveWithBackends = async (
352
344
  );
353
345
  };
354
346
 
355
- // An error that escapes proveWithBackends: what tla-ts can't handle is unsupported, and what tlapm
356
- // refuses, as a frontend's soundness check, fails.
357
347
  const getErrorReport = (obligation: PrintedObligation, error: unknown) => {
358
348
  if (error instanceof TlaUnsupportedError)
359
349
  return getUnprovedReport(obligation, "unsupported", error.message);
@@ -367,9 +357,7 @@ interface ObligationsOptions {
367
357
  signal?: AbortSignal;
368
358
  onReport?: (report: ProofReport, obligation: PrintedObligation) => void;
369
359
  results?: Map<string, Map<string, ProofAttempt>>;
370
- // --nofp and --nofpl.
371
360
  isFingerprinted?: (obligation: PrintedObligation) => boolean;
372
- // --noproving.
373
361
  isProving?: boolean;
374
362
  }
375
363
 
@@ -391,16 +379,13 @@ export const proveObligations = async (
391
379
  decide:
392
380
  pool?.decide ?? (async (prover, text, decideOptions) => decide(prover, text, decideOptions)),
393
381
  };
394
- // A few obligations per worker prove at once, and each reports once every earlier one has.
395
382
  const limit = pool ? workerCount * 4 : 1;
396
- const pending: Promise<ProofReport>[] = [];
397
- const isSettled: boolean[] = [];
398
383
  const running = new Set<Promise<void>>();
399
384
  const reports: ProofReport[] = [];
400
385
  const reportSettled = async () => {
401
- while (isSettled[reports.length]) {
386
+ while (reports.length < run.tasks.length && run.tasks[reports.length].isSettled) {
402
387
  options.signal?.throwIfAborted();
403
- const report = await pending[reports.length];
388
+ const report = await run.tasks[reports.length].report;
404
389
  reports.push(report);
405
390
  options.onReport?.(report, obligations[reports.length - 1]);
406
391
  // HACK: backends on this thread run synchronously, so yield to let an abort land.
@@ -410,19 +395,21 @@ export const proveObligations = async (
410
395
  try {
411
396
  for (const [index, obligation] of obligations.entries()) {
412
397
  options.signal?.throwIfAborted();
413
- const done = Promise.withResolvers<void>();
414
- run.tasks.push({ done: done.promise, session: undefined });
415
398
  const report = proveWithBackends(obligation, index, run, options.signal).catch(
416
399
  (error: unknown) => getErrorReport(obligation, error),
417
400
  );
418
- pending.push(report);
419
- running.add(done.promise);
420
401
  const settle = () => {
421
- isSettled[index] = true;
422
- running.delete(done.promise);
423
- done.resolve();
402
+ task.isSettled = true;
403
+ running.delete(task.done);
404
+ };
405
+ const task: ObligationTask = {
406
+ report,
407
+ done: report.then(settle, settle),
408
+ isSettled: false,
409
+ session: undefined,
424
410
  };
425
- void report.then(settle, settle);
411
+ run.tasks.push(task);
412
+ running.add(task.done);
426
413
  if (running.size >= limit) await Promise.race(running);
427
414
  await reportSettled();
428
415
  }
@@ -1,17 +1,12 @@
1
- // Decides the text tlapm sends LS4, as LS4 does: unsat when the negated obligation has no model,
2
- // which proves the obligation.
3
-
4
1
  import { TlaSyntaxError } from "../../errors";
5
2
  import type { DecideOptions, Verdict } from "../verdict";
6
3
  import { getSatisfiability, type PtlFormula } from "./tableau";
7
4
 
8
- // LS4's input syntax, as tlapm writes it: every operator is parenthesized except the facts of a
9
- // sequent, which are joined by & before ->.
10
5
  const parseLs4 = (text: string): PtlFormula => {
11
6
  const tokens = text.match(/<->|->|[()~&|]|[\w']+|\S/g) ?? [];
12
- const state = { position: 0 };
7
+ let position = 0;
13
8
  const take = (expected?: string) => {
14
- const token = tokens[state.position++];
9
+ const token = tokens[position++];
15
10
  if (token === undefined || (expected !== undefined && token !== expected))
16
11
  throw new TlaSyntaxError(
17
12
  `Expected ${expected ?? "a formula"} in LS4 input, found ${token ?? "the end"}`,
@@ -35,7 +30,7 @@ const parseLs4 = (text: string): PtlFormula => {
35
30
  };
36
31
  const parseJunction = (operator: string, kind: "And" | "Or", parseOperand: () => PtlFormula) => {
37
32
  const operands = [parseOperand()];
38
- while (tokens[state.position] === operator) {
33
+ while (tokens[position] === operator) {
39
34
  take();
40
35
  operands.push(parseOperand());
41
36
  }
@@ -43,19 +38,19 @@ const parseLs4 = (text: string): PtlFormula => {
43
38
  };
44
39
  const parseImplies = (): PtlFormula => {
45
40
  const left = parseJunction("|", "Or", () => parseJunction("&", "And", parseUnary));
46
- if (tokens[state.position] !== "->") return left;
41
+ if (tokens[position] !== "->") return left;
47
42
  take();
48
43
  return { kind: "Implies", left, right: parseImplies() };
49
44
  };
50
45
  const parseEquiv = (): PtlFormula => {
51
46
  const left = parseImplies();
52
- if (tokens[state.position] !== "<->") return left;
47
+ if (tokens[position] !== "<->") return left;
53
48
  take();
54
49
  return { kind: "Equiv", left, right: parseEquiv() };
55
50
  };
56
51
  const formula = parseEquiv();
57
- if (state.position < tokens.length)
58
- throw new TlaSyntaxError(`Expected the end of LS4 input, found ${tokens[state.position]}`);
52
+ if (position < tokens.length)
53
+ throw new TlaSyntaxError(`Expected the end of LS4 input, found ${tokens[position]}`);
59
54
  return formula;
60
55
  };
61
56
 
@@ -1,8 +1,3 @@
1
- // tlapm's PTL backend up to the text it sends LS4 (Pltl.process_obligation, Prep.flatten, and
2
- // Ls4's printer): temporal operators reduce to [], <> and prime, assumptions that hold
3
- // always get a [], and every other subformula becomes an atom named by a hash of its printed form.
4
- // The text is the negated obligation.
5
-
6
1
  import {
7
2
  bumpBy,
8
3
  shift,
@@ -27,9 +22,6 @@ import { getLevelInfo } from "../../obligations/levels";
27
22
  import { expandAction, expandFairness, expandLeadsto, getTime } from "../../obligations/temporal";
28
23
  import { getHashName, LEVEL_NUMBER_PREFIXES, LEVEL_PREFIXES } from "../prep/action";
29
24
 
30
- // Coalesce.rename_with_loc: each declared name gets its kind in front, or its level for a definition,
31
- // as in VARIABLE_x_ and CONSTANT_i_, so atoms print with the names tlapm hashes. A variable a
32
- // temporal quantifier binds keeps its name.
33
25
  const renameSequent = (scope: Scope | undefined, sequent: Sequent): Sequent => {
34
26
  const [inner, context] = mapAccumulate(
35
27
  scope,
@@ -93,38 +85,30 @@ const renameDeclarations = (scope: Scope | undefined, expression: Expr): Expr =>
93
85
  }
94
86
  };
95
87
 
96
- interface PreparedHyps {
97
- scope: Scope | undefined;
98
- substitution: Substitution;
99
- context: Hyp[];
100
- }
101
-
102
- // Pltl.expand_domains and Pltl.box_assumptions: NEW x \in S becomes NEW x and the fact x \in S, and
103
- // each visible fact that holds always gets a [].
104
88
  const prepareHyps = (scope: Scope | undefined, expression: Expr): Expr => {
105
89
  if (expression.kind !== "Sequent")
106
90
  return mapScoped(scope, expression, prepareHyps, adjoin, "all");
107
- const state: PreparedHyps = { scope, substitution: shift(0), context: [] };
91
+ let innerScope = scope;
92
+ let substitution: Substitution = shift(0);
93
+ const context: Hyp[] = [];
108
94
  const push = (hyp: Hyp) => {
109
95
  const prepared =
110
96
  hyp.kind === "Fact" && hyp.visibility === "Visible" && hyp.time === "Always"
111
97
  ? { ...hyp, expression: applyBuiltin("BoxTrue", [hyp.expression], hyp.expression) }
112
98
  : hyp;
113
- state.context.push(prepared);
114
- state.scope = adjoin(state.scope, prepared);
99
+ context.push(prepared);
100
+ innerScope = adjoin(innerScope, prepared);
115
101
  };
116
102
  for (const original of expression.sequent.context) {
117
- const hyp = substituteHyp(state.substitution, original);
103
+ const hyp = substituteHyp(substitution, original);
118
104
  if (hyp.kind !== "Fresh" || !hyp.domain || original.kind !== "Fresh" || !original.domain) {
119
105
  push(
120
- hyp.kind === "Fact"
121
- ? { ...hyp, expression: prepareHyps(state.scope, hyp.expression) }
122
- : hyp,
106
+ hyp.kind === "Fact" ? { ...hyp, expression: prepareHyps(innerScope, hyp.expression) } : hyp,
123
107
  );
124
- state.substitution = bumpBy(1, state.substitution);
108
+ substitution = bumpBy(1, substitution);
125
109
  continue;
126
110
  }
127
- const domain = prepareHyps(state.scope, hyp.domain);
111
+ const domain = prepareHyps(innerScope, hyp.domain);
128
112
  push({ ...hyp, domain: undefined });
129
113
  // tlapm judges the time of the domain as written, one hypothesis too deep.
130
114
  push({
@@ -135,19 +119,19 @@ const prepareHyps = (scope: Scope | undefined, expression: Expr): Expr => {
135
119
  domain,
136
120
  ),
137
121
  visibility: hyp.domainVisibility,
138
- time: hyp.level === "Constant" ? getTime(state.scope, original.domain) : "Now",
122
+ time: hyp.level === "Constant" ? getTime(innerScope, original.domain) : "Now",
139
123
  location: hyp.location,
140
124
  });
141
- state.substitution = {
125
+ substitution = {
142
126
  kind: "Compose",
143
127
  outer: shift(1),
144
- inner: bumpBy(1, state.substitution),
128
+ inner: bumpBy(1, substitution),
145
129
  };
146
130
  }
147
- const active = substituteExpr(state.substitution, expression.sequent.active);
131
+ const active = substituteExpr(substitution, expression.sequent.active);
148
132
  return {
149
133
  ...expression,
150
- sequent: { context: state.context, active: prepareHyps(state.scope, active) },
134
+ sequent: { context, active: prepareHyps(innerScope, active) },
151
135
  };
152
136
  };
153
137
 
@@ -166,8 +150,6 @@ const UNARY: Partial<Record<Builtin, string>> = {
166
150
  Prime: "next",
167
151
  };
168
152
 
169
- // Prep.flatten, which tlapm applies after Pltl.process_obligation has made every equality an atom:
170
- // TRUE drops out of the conjunctions of each visible fact, and a fact left with nothing disappears.
171
153
  const removeTrue = (expression: Expr): Expr | undefined => {
172
154
  if (expression.kind === "Internal" && expression.builtin === "TRUE") return undefined;
173
155
  if (expression.kind === "List" && expression.bullet !== "Or") {
@@ -189,10 +171,7 @@ const removeTrue = (expression: Expr): Expr | undefined => {
189
171
 
190
172
  const TRUE_TEXT = "(cc | ~ cc)";
191
173
 
192
- // The text tlapm sends LS4 for an obligation after Prep.normalize_expand.
193
174
  export const encodePtl = (sequent: Sequent): string => {
194
- // Ls4's printer. A sequent becomes an implication from its visible facts, joined by &, and
195
- // Prep.flatten applies to the facts of the outermost one.
196
175
  const print = (hyps: Hyp[], expression: Expr, isOutermost = false): string => {
197
176
  const recurse = (inner: Expr) => print(hyps, inner);
198
177
  switch (expression.kind) {
@@ -235,7 +214,6 @@ export const encodePtl = (sequent: Sequent): string => {
235
214
  return facts.length === 0 ? goal : `(${facts.join(" & ")} -> ${goal})`;
236
215
  }
237
216
  }
238
- // Pltl.coalesce_fol names an atom as Coalesce does.
239
217
  return getHashName(hyps, expression);
240
218
  };
241
219
  const renamed = renameDeclarations(undefined, { kind: "Sequent", sequent });
@@ -1,9 +1,3 @@
1
- // A conflict-driven clause learning SAT solver for the clauses of one tableau node. A literal is a
2
- // variable number, negated when below zero. The solver keeps what it learns across calls, which add
3
- // clauses or assume literals. It decides each open variable false first, so a model sets few
4
- // variables, and a model holds 1 for each true variable and -1 for each false one. It gives up at
5
- // the first conflict after isExpired turns true.
6
-
7
1
  const getIndex = (literal: number) => (literal > 0 ? 2 * literal : -2 * literal + 1);
8
2
 
9
3
  export const createSolver = (variableCount: number, isExpired: () => boolean) => {
@@ -15,7 +9,9 @@ export const createSolver = (variableCount: number, isExpired: () => boolean) =>
15
9
  const watches: number[][][] = Array.from({ length: 2 * variableCount + 2 }, () => []);
16
10
  const trail: number[] = [];
17
11
  const levelStarts: number[] = [];
18
- const state = { head: 0, bump: 1, isUnsatisfiable: false };
12
+ let head = 0;
13
+ let bump = 1;
14
+ let isUnsatisfiable = false;
19
15
  const getValue = (literal: number) => (literal > 0 ? values[literal] : -values[-literal]);
20
16
  const assign = (literal: number, reason: number[] | undefined) => {
21
17
  const variable = Math.abs(literal);
@@ -28,11 +24,9 @@ export const createSolver = (variableCount: number, isExpired: () => boolean) =>
28
24
  watches[getIndex(clause[0])].push(clause);
29
25
  watches[getIndex(clause[1])].push(clause);
30
26
  };
31
- // Moves each watch off a literal that turned false, and returns the clause of a conflict. The
32
- // literal a clause implies sits first in it.
33
27
  const propagate = () => {
34
- for (; state.head < trail.length; state.head++) {
35
- const falsified = -trail[state.head];
28
+ for (; head < trail.length; head++) {
29
+ const falsified = -trail[head];
36
30
  const watching = watches[getIndex(falsified)];
37
31
  for (let position = 0; position < watching.length; position++) {
38
32
  const clause = watching[position];
@@ -59,39 +53,38 @@ export const createSolver = (variableCount: number, isExpired: () => boolean) =>
59
53
  const size = levelStarts[level];
60
54
  while (trail.length > size) values[Math.abs(trail.pop() ?? 0)] = 0;
61
55
  levelStarts.length = level;
62
- state.head = trail.length;
56
+ head = trail.length;
63
57
  };
64
- // Resolves the conflict back to the first literal of the current level that all its paths pass
65
- // through, and returns the learned clause with that literal first.
66
58
  const analyze = (conflict: number[]) => {
67
59
  const learned = [0];
68
60
  const current = levelStarts.length;
69
- const walk = { pending: 0, literal: 0, index: trail.length - 1 };
61
+ let pendingCount = 0;
62
+ let resolved = 0;
63
+ let trailIndex = trail.length - 1;
70
64
  for (let clause: number[] | undefined = conflict; ;) {
71
- for (const literal of clause?.slice(walk.literal === 0 ? 0 : 1) ?? []) {
65
+ for (const literal of clause?.slice(resolved === 0 ? 0 : 1) ?? []) {
72
66
  const variable = Math.abs(literal);
73
67
  if (seen[variable] || levels[variable] === 0) continue;
74
68
  seen[variable] = 1;
75
- activities[variable] += state.bump;
76
- if (levels[variable] === current) walk.pending++;
69
+ activities[variable] += bump;
70
+ if (levels[variable] === current) pendingCount++;
77
71
  else learned.push(literal);
78
72
  }
79
- while (!seen[Math.abs(trail[walk.index])]) walk.index--;
80
- walk.literal = trail[walk.index--];
81
- seen[Math.abs(walk.literal)] = 0;
82
- clause = reasons[Math.abs(walk.literal)];
83
- if (--walk.pending === 0) break;
73
+ while (!seen[Math.abs(trail[trailIndex])]) trailIndex--;
74
+ resolved = trail[trailIndex--];
75
+ seen[Math.abs(resolved)] = 0;
76
+ clause = reasons[Math.abs(resolved)];
77
+ if (--pendingCount === 0) break;
84
78
  }
85
- learned[0] = -walk.literal;
79
+ learned[0] = -resolved;
86
80
  learned.forEach((literal) => (seen[Math.abs(literal)] = 0));
87
- state.bump *= 1.05;
81
+ bump *= 1.05;
88
82
  return learned;
89
83
  };
90
- // Literals false for good are dropped, since the solver adds clauses only at level 0.
91
84
  const addClause = (clause: number[]) => {
92
85
  backtrack(0);
93
86
  const literals = [...new Set(clause)];
94
- if (state.isUnsatisfiable || literals.some((literal) => getValue(literal) > 0)) return;
87
+ if (isUnsatisfiable || literals.some((literal) => getValue(literal) > 0)) return;
95
88
  if (literals.some((literal) => literals.includes(-literal))) return;
96
89
  const open = literals.filter((literal) => getValue(literal) === 0);
97
90
  if (open.length > 1) {
@@ -99,21 +92,20 @@ export const createSolver = (variableCount: number, isExpired: () => boolean) =>
99
92
  return;
100
93
  }
101
94
  if (open.length === 0) {
102
- state.isUnsatisfiable = true;
95
+ isUnsatisfiable = true;
103
96
  return;
104
97
  }
105
98
  assign(open[0], undefined);
106
- if (propagate()) state.isUnsatisfiable = true;
99
+ if (propagate()) isUnsatisfiable = true;
107
100
  };
108
- // A model of the clauses in which every assumed literal holds, or unsat when there is none.
109
101
  const solve = (assumptions: number[]): Int8Array | "unsat" | "unknown" => {
110
102
  backtrack(0);
111
- if (state.isUnsatisfiable) return "unsat";
103
+ if (isUnsatisfiable) return "unsat";
112
104
  for (;;) {
113
105
  const conflict = propagate();
114
106
  if (conflict) {
115
107
  if (levelStarts.length === 0) {
116
- state.isUnsatisfiable = true;
108
+ isUnsatisfiable = true;
117
109
  return "unsat";
118
110
  }
119
111
  if (isExpired()) return "unknown";