tla-ts 0.0.3 → 0.0.4-dev.1a3acf6

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 (381) hide show
  1. package/README.md +46 -115
  2. package/dist/cli.cjs +1 -1
  3. package/dist/cli.js +1 -1
  4. package/dist/deciders.cjs +366 -0
  5. package/dist/deciders.js +366 -0
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +194 -8
  8. package/dist/index.d.ts +194 -8
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -4
  11. package/dist/parallel-worker.js +1 -4
  12. package/dist/prove-worker.cjs +9 -0
  13. package/dist/prove-worker.d.cts +9 -0
  14. package/dist/prove-worker.d.ts +9 -0
  15. package/dist/prove-worker.js +9 -0
  16. package/dist/prove.cjs +1006 -0
  17. package/dist/prove.js +1006 -0
  18. package/dist/run.cjs +45 -0
  19. package/dist/run.js +45 -0
  20. package/dist/tlapm.cjs +10 -0
  21. package/dist/tlapm.js +10 -0
  22. package/dist/transfer.cjs +1377 -18
  23. package/dist/transfer.js +1377 -18
  24. package/dist/worker.cjs +21 -0
  25. package/dist/worker.js +21 -0
  26. package/package.json +24 -11
  27. package/src/ast.ts +149 -8
  28. package/src/bags.ts +26 -30
  29. package/src/builtins.ts +45 -19
  30. package/src/bundled-modules.ts +3 -3
  31. package/src/check.ts +1182 -900
  32. package/src/cli/options.ts +109 -0
  33. package/src/cli/run.ts +63 -0
  34. package/src/cli/shared.ts +106 -0
  35. package/src/cli/tlapm.ts +374 -0
  36. package/src/cli/tlc.ts +941 -0
  37. package/src/cli/xml-exporter.ts +221 -0
  38. package/src/cli.ts +9 -2
  39. package/src/codegen.ts +112 -51
  40. package/src/config.ts +254 -181
  41. package/src/dependencies.ts +5 -7
  42. package/src/environment.ts +579 -184
  43. package/src/errors.ts +287 -4
  44. package/src/evaluate.ts +1095 -365
  45. package/src/exit-codes.ts +22 -43
  46. package/src/fingerprints.ts +49 -14
  47. package/src/index.ts +11 -0
  48. package/src/instances.ts +73 -60
  49. package/src/java-overrides.ts +183 -66
  50. package/src/json.ts +31 -36
  51. package/src/levels.ts +197 -54
  52. package/src/lexer.ts +127 -30
  53. package/src/liveness-checker.ts +894 -0
  54. package/src/liveness.ts +246 -596
  55. package/src/{obligation-action.ts → obligations/action.ts} +248 -74
  56. package/src/{obligation-anon.ts → obligations/anonymize.ts} +55 -57
  57. package/src/obligations/equality.ts +260 -0
  58. package/src/obligations/expression.ts +618 -0
  59. package/src/{obligation-format.ts → obligations/format.ts} +7 -8
  60. package/src/obligations/generate.ts +457 -0
  61. package/src/{obligations.ts → obligations/index.ts} +46 -32
  62. package/src/{obligation-levels.ts → obligations/levels.ts} +44 -40
  63. package/src/obligations/module.ts +445 -0
  64. package/src/obligations/normalize.ts +554 -0
  65. package/src/obligations/operators.ts +176 -0
  66. package/src/obligations/pragma.ts +195 -0
  67. package/src/{obligation-printer.ts → obligations/printer.ts} +65 -230
  68. package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
  69. package/src/obligations/regroup.ts +100 -0
  70. package/src/{obligation-generate.ts → obligations/simplify.ts} +130 -536
  71. package/src/{obligation-source.ts → obligations/source.ts} +174 -216
  72. package/src/{obligation-deref.ts → obligations/subexpression.ts} +84 -115
  73. package/src/obligations/substitution.ts +261 -0
  74. package/src/obligations/temporal.ts +321 -0
  75. package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
  76. package/src/parallel-worker.ts +184 -235
  77. package/src/parallel.ts +156 -91
  78. package/src/parser.ts +490 -188
  79. package/src/pcal/ast.ts +755 -0
  80. package/src/pcal/parse.ts +1487 -0
  81. package/src/pcal/tokenize.ts +487 -0
  82. package/src/pcal/translate.ts +1151 -0
  83. package/src/pcal/validate.ts +444 -0
  84. package/src/prover/decide-pool.ts +95 -0
  85. package/src/prover/deciders.ts +33 -0
  86. package/src/prover/first-order.ts +175 -0
  87. package/src/prover/isabelle/LICENSE +36 -0
  88. package/src/prover/isabelle/LICENSE-tlapm +26 -0
  89. package/src/prover/isabelle/decide.ts +44 -0
  90. package/src/prover/isabelle/encode.ts +445 -0
  91. package/src/prover/isabelle/isar/inner-syntax.ts +579 -0
  92. package/src/prover/isabelle/isar/method.ts +367 -0
  93. package/src/prover/isabelle/isar/proof.ts +28 -0
  94. package/src/prover/isabelle/isar/statement.ts +57 -0
  95. package/src/prover/isabelle/isar/theory.ts +192 -0
  96. package/src/prover/isabelle/isar/type-check.ts +278 -0
  97. package/src/prover/isabelle/provers/blast/blast.ts +258 -0
  98. package/src/prover/isabelle/provers/blast/branch.ts +322 -0
  99. package/src/prover/isabelle/provers/blast/prove.ts +347 -0
  100. package/src/prover/isabelle/provers/blast/rules.ts +330 -0
  101. package/src/prover/isabelle/provers/blast/state.ts +180 -0
  102. package/src/prover/isabelle/provers/blast/term.ts +546 -0
  103. package/src/prover/isabelle/provers/clasimp.ts +150 -0
  104. package/src/prover/isabelle/provers/classical.ts +389 -0
  105. package/src/prover/isabelle/provers/hypsubst.ts +433 -0
  106. package/src/prover/isabelle/pure/bicompose.ts +174 -0
  107. package/src/prover/isabelle/pure/consts.ts +32 -0
  108. package/src/prover/isabelle/pure/conv.ts +79 -0
  109. package/src/prover/isabelle/pure/drule.ts +264 -0
  110. package/src/prover/isabelle/pure/envir.ts +331 -0
  111. package/src/prover/isabelle/pure/goal.ts +53 -0
  112. package/src/prover/isabelle/pure/interrupt.ts +16 -0
  113. package/src/prover/isabelle/pure/list.ts +12 -0
  114. package/src/prover/isabelle/pure/logic.ts +330 -0
  115. package/src/prover/isabelle/pure/name.ts +98 -0
  116. package/src/prover/isabelle/pure/net.ts +215 -0
  117. package/src/prover/isabelle/pure/object-logic.ts +41 -0
  118. package/src/prover/isabelle/pure/pattern.ts +631 -0
  119. package/src/prover/isabelle/pure/raw-simplifier.ts +338 -0
  120. package/src/prover/isabelle/pure/rewriter.ts +602 -0
  121. package/src/prover/isabelle/pure/search.ts +68 -0
  122. package/src/prover/isabelle/pure/seq.ts +133 -0
  123. package/src/prover/isabelle/pure/simplifier.ts +106 -0
  124. package/src/prover/isabelle/pure/table.ts +201 -0
  125. package/src/prover/isabelle/pure/tactic.ts +168 -0
  126. package/src/prover/isabelle/pure/tactical.ts +264 -0
  127. package/src/prover/isabelle/pure/term-ord.ts +105 -0
  128. package/src/prover/isabelle/pure/term.ts +688 -0
  129. package/src/prover/isabelle/pure/thm.ts +783 -0
  130. package/src/prover/isabelle/pure/type.ts +121 -0
  131. package/src/prover/isabelle/pure/unify.ts +753 -0
  132. package/src/prover/isabelle/pure/variable.ts +25 -0
  133. package/src/prover/isabelle/tla/classical-setup.ts +101 -0
  134. package/src/prover/isabelle/tla/context.ts +74 -0
  135. package/src/prover/isabelle/tla/logic-data.ts +5 -0
  136. package/src/prover/isabelle/tla/logic.ts +215 -0
  137. package/src/prover/isabelle/tla/simpdata.ts +241 -0
  138. package/src/prover/isabelle/tools/intuitionistic.ts +132 -0
  139. package/src/prover/ocaml.ts +22 -0
  140. package/src/prover/prep/action.ts +508 -0
  141. package/src/prover/prep/enabled-axioms.ts +189 -0
  142. package/src/prover/prep/fingerprint.ts +388 -0
  143. package/src/prover/prep/level-comparison.ts +529 -0
  144. package/src/prover/prep/md5.ts +60 -0
  145. package/src/prover/prep/methods.ts +221 -0
  146. package/src/prover/prep/normalize.ts +309 -0
  147. package/src/prover/prove-worker.ts +22 -0
  148. package/src/prover/prove.ts +492 -0
  149. package/src/prover/ptl/decide.ts +68 -0
  150. package/src/prover/ptl/encode.ts +244 -0
  151. package/src/prover/ptl/sat.ts +151 -0
  152. package/src/prover/ptl/tableau.ts +276 -0
  153. package/src/prover/smt/axioms.ts +209 -0
  154. package/src/prover/smt/context.ts +108 -0
  155. package/src/prover/smt/data.ts +636 -0
  156. package/src/prover/smt/encode.ts +275 -0
  157. package/src/prover/smt/extensionality.ts +65 -0
  158. package/src/prover/smt/flatten.ts +112 -0
  159. package/src/prover/smt/format.ts +154 -0
  160. package/src/prover/smt/instances.ts +181 -0
  161. package/src/prover/smt/rewrite.ts +766 -0
  162. package/src/prover/smt/solver/act-cache.ts +265 -0
  163. package/src/prover/smt/solver/arith-rewriter.ts +962 -0
  164. package/src/prover/smt/solver/array.ts +22 -0
  165. package/src/prover/smt/solver/asserted-formulas.ts +469 -0
  166. package/src/prover/smt/solver/ast-lt.ts +157 -0
  167. package/src/prover/smt/solver/ast-util.ts +91 -0
  168. package/src/prover/smt/solver/ast.ts +1315 -0
  169. package/src/prover/smt/solver/auf-solver.ts +572 -0
  170. package/src/prover/smt/solver/case-split-queue.ts +178 -0
  171. package/src/prover/smt/solver/cg-table.ts +175 -0
  172. package/src/prover/smt/solver/clause.ts +276 -0
  173. package/src/prover/smt/solver/conflict-resolution.ts +343 -0
  174. package/src/prover/smt/solver/context.ts +1292 -0
  175. package/src/prover/smt/solver/ctx-simplify.ts +334 -0
  176. package/src/prover/smt/solver/der.ts +224 -0
  177. package/src/prover/smt/solver/dyn-ack.ts +141 -0
  178. package/src/prover/smt/solver/e-matching.ts +659 -0
  179. package/src/prover/smt/solver/enode.ts +259 -0
  180. package/src/prover/smt/solver/expr-ref.ts +21 -0
  181. package/src/prover/smt/solver/expr-safe-replace.ts +172 -0
  182. package/src/prover/smt/solver/fingerprints.ts +92 -0
  183. package/src/prover/smt/solver/flat-literals.ts +51 -0
  184. package/src/prover/smt/solver/goal.ts +138 -0
  185. package/src/prover/smt/solver/hash.ts +173 -0
  186. package/src/prover/smt/solver/hashtable.ts +166 -0
  187. package/src/prover/smt/solver/internalizer.ts +1381 -0
  188. package/src/prover/smt/solver/lbool.ts +5 -0
  189. package/src/prover/smt/solver/literal.ts +24 -0
  190. package/src/prover/smt/solver/lp/bound-propagator.ts +391 -0
  191. package/src/prover/smt/solver/lp/constraints.ts +78 -0
  192. package/src/prover/smt/solver/lp/containers.ts +296 -0
  193. package/src/prover/smt/solver/lp/core-solver.ts +504 -0
  194. package/src/prover/smt/solver/lp/cross-nested.ts +224 -0
  195. package/src/prover/smt/solver/lp/dep-intervals.ts +642 -0
  196. package/src/prover/smt/solver/lp/dependency.ts +51 -0
  197. package/src/prover/smt/solver/lp/dioph-eq.ts +1181 -0
  198. package/src/prover/smt/solver/lp/explanation.ts +46 -0
  199. package/src/prover/smt/solver/lp/gomory.ts +255 -0
  200. package/src/prover/smt/solver/lp/hnf-cutter.ts +125 -0
  201. package/src/prover/smt/solver/lp/hnf.ts +281 -0
  202. package/src/prover/smt/solver/lp/int-cube.ts +295 -0
  203. package/src/prover/smt/solver/lp/int-solver.ts +514 -0
  204. package/src/prover/smt/solver/lp/lar-solver.ts +816 -0
  205. package/src/prover/smt/solver/lp/lar-term.ts +62 -0
  206. package/src/prover/smt/solver/lp/lp-settings.ts +91 -0
  207. package/src/prover/smt/solver/lp/maximize-term.ts +347 -0
  208. package/src/prover/smt/solver/lp/nex-creator.ts +381 -0
  209. package/src/prover/smt/solver/lp/nex.ts +120 -0
  210. package/src/prover/smt/solver/lp/nla-backoff.ts +43 -0
  211. package/src/prover/smt/solver/lp/nla-basics.ts +398 -0
  212. package/src/prover/smt/solver/lp/nla-core.ts +968 -0
  213. package/src/prover/smt/solver/lp/nla-divisions.ts +332 -0
  214. package/src/prover/smt/solver/lp/nla-emonics.ts +373 -0
  215. package/src/prover/smt/solver/lp/nla-factorization.ts +30 -0
  216. package/src/prover/smt/solver/lp/nla-grobner.ts +904 -0
  217. package/src/prover/smt/solver/lp/nla-horner.ts +126 -0
  218. package/src/prover/smt/solver/lp/nla-intervals.ts +265 -0
  219. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +715 -0
  220. package/src/prover/smt/solver/lp/nla-monotone.ts +59 -0
  221. package/src/prover/smt/solver/lp/nla-order.ts +307 -0
  222. package/src/prover/smt/solver/lp/nla-patcher.ts +112 -0
  223. package/src/prover/smt/solver/lp/nla-tangents.ts +251 -0
  224. package/src/prover/smt/solver/lp/nla-throttle.ts +85 -0
  225. package/src/prover/smt/solver/lp/nla-types.ts +124 -0
  226. package/src/prover/smt/solver/lp/nla-var-eqs.ts +141 -0
  227. package/src/prover/smt/solver/lp/nra-solver.ts +440 -0
  228. package/src/prover/smt/solver/lp/numeric-pair.ts +65 -0
  229. package/src/prover/smt/solver/lp/pdd-interval.ts +31 -0
  230. package/src/prover/smt/solver/lp/pdd-simplifier.ts +184 -0
  231. package/src/prover/smt/solver/lp/pdd-solver.ts +408 -0
  232. package/src/prover/smt/solver/lp/pdd.ts +971 -0
  233. package/src/prover/smt/solver/lp/random-updater.ts +138 -0
  234. package/src/prover/smt/solver/lp/static-matrix.ts +246 -0
  235. package/src/prover/smt/solver/lp/union-find.ts +96 -0
  236. package/src/prover/smt/solver/mam-compiler.ts +847 -0
  237. package/src/prover/smt/solver/mam-interpreter.ts +400 -0
  238. package/src/prover/smt/solver/maximize-ac-sharing.ts +118 -0
  239. package/src/prover/smt/solver/model-checker.ts +347 -0
  240. package/src/prover/smt/solver/model-evaluator.ts +262 -0
  241. package/src/prover/smt/solver/model-finder.ts +155 -0
  242. package/src/prover/smt/solver/model-generator.ts +97 -0
  243. package/src/prover/smt/solver/model-macro-solver.ts +728 -0
  244. package/src/prover/smt/solver/nlsat/algebraic.ts +1161 -0
  245. package/src/prover/smt/solver/nlsat/atoms.ts +238 -0
  246. package/src/prover/smt/solver/nlsat/evaluator.ts +303 -0
  247. package/src/prover/smt/solver/nlsat/explain.ts +607 -0
  248. package/src/prover/smt/solver/nlsat/interval-set.ts +351 -0
  249. package/src/prover/smt/solver/nlsat/levelwise.ts +737 -0
  250. package/src/prover/smt/solver/nlsat/mpbq.ts +161 -0
  251. package/src/prover/smt/solver/nlsat/polynomial.ts +1722 -0
  252. package/src/prover/smt/solver/nlsat/solver.ts +911 -0
  253. package/src/prover/smt/solver/nlsat/types.ts +43 -0
  254. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
  255. package/src/prover/smt/solver/nlsat/upolynomial.ts +996 -0
  256. package/src/prover/smt/solver/nnf.ts +469 -0
  257. package/src/prover/smt/solver/pattern-inference.ts +351 -0
  258. package/src/prover/smt/solver/poly-rewriter.ts +477 -0
  259. package/src/prover/smt/solver/propagate-values.ts +83 -0
  260. package/src/prover/smt/solver/proto-model.ts +551 -0
  261. package/src/prover/smt/solver/pull-quant.ts +139 -0
  262. package/src/prover/smt/solver/qe-lite-der.ts +526 -0
  263. package/src/prover/smt/solver/qe-lite-fm.ts +576 -0
  264. package/src/prover/smt/solver/qe-lite.ts +381 -0
  265. package/src/prover/smt/solver/qi-queue.ts +362 -0
  266. package/src/prover/smt/solver/quant-tactics.ts +117 -0
  267. package/src/prover/smt/solver/quantifier-analyzer.ts +562 -0
  268. package/src/prover/smt/solver/quantifier-manager.ts +186 -0
  269. package/src/prover/smt/solver/random-gen.ts +10 -0
  270. package/src/prover/smt/solver/rational.ts +165 -0
  271. package/src/prover/smt/solver/ref-vector.ts +62 -0
  272. package/src/prover/smt/solver/relevancy.ts +247 -0
  273. package/src/prover/smt/solver/rewriter-tpl.ts +1159 -0
  274. package/src/prover/smt/solver/rewriter.ts +843 -0
  275. package/src/prover/smt/solver/rlimit.ts +79 -0
  276. package/src/prover/smt/solver/smtlib-parser.ts +526 -0
  277. package/src/prover/smt/solver/solver.ts +71 -0
  278. package/src/prover/smt/solver/sort.ts +35 -0
  279. package/src/prover/smt/solver/sticky-generations.ts +69 -0
  280. package/src/prover/smt/solver/theory-lra-axioms.ts +340 -0
  281. package/src/prover/smt/solver/theory-lra-bounds.ts +226 -0
  282. package/src/prover/smt/solver/theory-lra-final-check.ts +252 -0
  283. package/src/prover/smt/solver/theory-lra-internalize.ts +476 -0
  284. package/src/prover/smt/solver/theory-lra-model.ts +243 -0
  285. package/src/prover/smt/solver/theory-lra-propagate.ts +405 -0
  286. package/src/prover/smt/solver/theory-lra-state.ts +43 -0
  287. package/src/prover/smt/solver/theory-lra.ts +56 -0
  288. package/src/prover/smt/solver/var-subst.ts +258 -0
  289. package/src/prover/smt/solver/z3-license.txt +10 -0
  290. package/src/prover/smt/table.ts +170 -0
  291. package/src/prover/tlapm/cache.ts +236 -0
  292. package/src/prover/tlapm/ocaml-marshal.ts +166 -0
  293. package/src/prover/tlapm/tlapm.ts +96 -0
  294. package/src/prover/tlaps-module.ts +510 -0
  295. package/src/prover/verdict.ts +13 -0
  296. package/src/prover/zenon/LICENSE +32 -0
  297. package/src/prover/zenon/check/check.ts +105 -0
  298. package/src/prover/zenon/check/definitions.ts +144 -0
  299. package/src/prover/zenon/check/equality.ts +182 -0
  300. package/src/prover/zenon/check/functions.ts +62 -0
  301. package/src/prover/zenon/check/keys.ts +125 -0
  302. package/src/prover/zenon/check/propositional.ts +104 -0
  303. package/src/prover/zenon/check/records.ts +188 -0
  304. package/src/prover/zenon/check/relations.ts +121 -0
  305. package/src/prover/zenon/check/rewrites.ts +224 -0
  306. package/src/prover/zenon/check/sets.ts +302 -0
  307. package/src/prover/zenon/check/terms.ts +127 -0
  308. package/src/prover/zenon/check/tla.ts +126 -0
  309. package/src/prover/zenon/decide.ts +107 -0
  310. package/src/prover/zenon/encode.ts +378 -0
  311. package/src/prover/zenon/eqrel.ts +300 -0
  312. package/src/prover/zenon/equality-rules.ts +435 -0
  313. package/src/prover/zenon/expr.ts +551 -0
  314. package/src/prover/zenon/index-table.ts +350 -0
  315. package/src/prover/zenon/isar-case.ts +513 -0
  316. package/src/prover/zenon/isar.ts +1249 -0
  317. package/src/prover/zenon/limits.ts +15 -0
  318. package/src/prover/zenon/llproof.ts +173 -0
  319. package/src/prover/zenon/mltoll.ts +1566 -0
  320. package/src/prover/zenon/node.ts +297 -0
  321. package/src/prover/zenon/ocaml.ts +169 -0
  322. package/src/prover/zenon/parse.ts +247 -0
  323. package/src/prover/zenon/phrase.ts +191 -0
  324. package/src/prover/zenon/proof.ts +198 -0
  325. package/src/prover/zenon/propositional-rules.ts +387 -0
  326. package/src/prover/zenon/rules.ts +94 -0
  327. package/src/prover/zenon/search.ts +288 -0
  328. package/src/prover/zenon/tla/functions.ts +151 -0
  329. package/src/prover/zenon/tla/llproof.ts +480 -0
  330. package/src/prover/zenon/tla/records.ts +225 -0
  331. package/src/prover/zenon/tla/rewrites.ts +447 -0
  332. package/src/prover/zenon/tla/sets.ts +304 -0
  333. package/src/prover/zenon/tla/terms.ts +162 -0
  334. package/src/prover/zenon/tla/tla.ts +506 -0
  335. package/src/randomization.ts +39 -27
  336. package/src/resolve.ts +1474 -426
  337. package/src/runtime.ts +25 -7
  338. package/src/sany/builtins.ts +147 -0
  339. package/src/sany/generator.ts +3111 -0
  340. package/src/sany/levels.ts +1205 -0
  341. package/src/sany/lexer.ts +544 -0
  342. package/src/sany/linter.ts +122 -0
  343. package/src/sany/modules.ts +1398 -0
  344. package/src/sany/nodes.ts +463 -0
  345. package/src/sany/parse-error.ts +2161 -0
  346. package/src/sany/report.ts +355 -0
  347. package/src/sany/selectors.ts +736 -0
  348. package/src/sany/source.ts +247 -0
  349. package/src/sany/xml.ts +1016 -0
  350. package/src/search.ts +969 -0
  351. package/src/spec-description.ts +66 -29
  352. package/src/{standard-levels.ts → standard-definitions.ts} +118 -2
  353. package/src/standard-modules.ts +215 -164
  354. package/src/state-graph.ts +181 -0
  355. package/src/subexpressions.ts +231 -171
  356. package/src/substitution-levels.ts +40 -19
  357. package/src/successors.ts +253 -125
  358. package/src/symmetry.ts +194 -54
  359. package/src/tableau.ts +60 -32
  360. package/src/tlc/fingerprint.ts +214 -0
  361. package/src/{tlc-modules.ts → tlc/jar-data.ts} +4 -0
  362. package/src/tlc/messages.ts +441 -0
  363. package/src/{tlc-order.ts → tlc/order.ts} +84 -16
  364. package/src/{tlc-random.ts → tlc/random.ts} +30 -189
  365. package/src/tlc/replay.ts +659 -0
  366. package/src/{tlc-summary.ts → tlc/summary.ts} +20 -5
  367. package/src/{tlc-module.ts → tlc/tlc-operators.ts} +59 -50
  368. package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +63 -45
  369. package/src/tlc/version.ts +19 -0
  370. package/src/transfer.ts +44 -32
  371. package/src/values.ts +843 -241
  372. package/src/worker.ts +62 -0
  373. package/dist/parallel.cjs +0 -12
  374. package/dist/parallel.js +0 -12
  375. package/dist/run-cli.cjs +0 -28
  376. package/dist/run-cli.js +0 -28
  377. package/src/canonical.ts +0 -39
  378. package/src/cli-options.ts +0 -58
  379. package/src/obligation-expression.ts +0 -1060
  380. package/src/obligation-module.ts +0 -1042
  381. package/src/run-cli.ts +0 -478
@@ -0,0 +1,214 @@
1
+ import {
2
+ getStringRank,
3
+ isFunctionValue,
4
+ isModelValue,
5
+ toFiniteFunction,
6
+ toFiniteSet,
7
+ type Value,
8
+ } from "../values";
9
+
10
+ // FP64 with TLC's first irreducible polynomial, FP64.Polys[0], which `-fp 0` selects.
11
+ const IRREDUCIBLE_POLYNOMIAL = 0x911498ae0e66bad6n;
12
+
13
+ const BYTE_MOD_TABLE = (() => {
14
+ const powers: bigint[] = [];
15
+ for (let power = 1n << 63n; powers.length < 72;) {
16
+ powers.push(power);
17
+ power = (power >> 1n) ^ ((power & 1n) === 0n ? 0n : IRREDUCIBLE_POLYNOMIAL);
18
+ }
19
+ return Array.from({ length: 256 }, (_, byte) =>
20
+ powers
21
+ .slice(64, 72)
22
+ .reverse()
23
+ .reduce((value, power, bit) => ((byte >> bit) & 1 ? value ^ power : value), 0n),
24
+ );
25
+ })();
26
+
27
+ const BYTE_MOD_HIGH = Int32Array.from(BYTE_MOD_TABLE, (entry) =>
28
+ Number(BigInt.asIntN(32, entry >> 32n)),
29
+ );
30
+ const BYTE_MOD_LOW = Int32Array.from(BYTE_MOD_TABLE, (entry) => Number(BigInt.asIntN(32, entry)));
31
+
32
+ // The fingerprint being extended, as its high and low 32 bits, which avoids a BigInt operation
33
+ // for every byte.
34
+ const INITIAL_HIGH = Number(BigInt.asIntN(32, IRREDUCIBLE_POLYNOMIAL >> 32n));
35
+ const INITIAL_LOW = Number(BigInt.asIntN(32, IRREDUCIBLE_POLYNOMIAL));
36
+ let high = INITIAL_HIGH;
37
+ let low = INITIAL_LOW;
38
+
39
+ const extendByte = (byte: number) => {
40
+ const index = (byte ^ low) & 0xff;
41
+ low = ((low >>> 8) | (high << 24)) ^ BYTE_MOD_LOW[index];
42
+ high = (high >>> 8) ^ BYTE_MOD_HIGH[index];
43
+ };
44
+
45
+ const extendInt = (value: number) => {
46
+ for (let shift = 0; shift < 32; shift += 8) extendByte(value >>> shift);
47
+ };
48
+
49
+ const BOOL_VALUE = 0;
50
+ const INT_VALUE = 1;
51
+ const STRING_VALUE = 3;
52
+ const SET_ENUM_VALUE = 5;
53
+ const FCN_RCD_VALUE = 9;
54
+ const MODEL_VALUE = 21;
55
+
56
+ // Value#fingerPrint for each of TLC's value classes. A model value extends the fingerprint with
57
+ // its UniqueString token, and TLC numbers tokens from 1 in intern order.
58
+ const extendValue = (value: Value) => {
59
+ if (typeof value === "boolean") {
60
+ extendByte(BOOL_VALUE);
61
+ extendByte(value ? 116 : 102);
62
+ } else if (typeof value === "number") {
63
+ extendByte(INT_VALUE);
64
+ extendInt(value);
65
+ } else if (typeof value === "string") {
66
+ extendByte(STRING_VALUE);
67
+ extendInt(value.length);
68
+ for (let index = 0; index < value.length; index++) {
69
+ extendByte(value.charCodeAt(index));
70
+ }
71
+ } else if (isModelValue(value)) {
72
+ extendByte(MODEL_VALUE);
73
+ extendInt(getStringRank(value.name) + 1);
74
+ } else if (isFunctionValue(value)) {
75
+ const functionValue = toFiniteFunction(value);
76
+ extendByte(FCN_RCD_VALUE);
77
+ extendInt(functionValue.domain.length);
78
+ for (let index = 0; index < functionValue.domain.length; index++) {
79
+ extendValue(functionValue.domain[index]);
80
+ extendValue(functionValue.range[index]);
81
+ }
82
+ } else {
83
+ const elements = toFiniteSet(value).elements;
84
+ extendByte(SET_ENUM_VALUE);
85
+ extendInt(elements.length);
86
+ for (const element of elements) extendValue(element);
87
+ }
88
+ };
89
+
90
+ // TLCStateMut#fingerPrint of variable values in TLC's variable order. Evaluating a lazy value can
91
+ // fingerprint another, so this restores the fingerprint it interrupts.
92
+ export const getTlcFingerprint = (state: Value[]) => {
93
+ const outer = { high, low };
94
+ high = INITIAL_HIGH;
95
+ low = INITIAL_LOW;
96
+ for (const value of state) extendValue(value);
97
+ const fingerprint = (BigInt(high >>> 0) << 32n) | BigInt(low >>> 0);
98
+ high = outer.high;
99
+ low = outer.low;
100
+ return fingerprint;
101
+ };
102
+
103
+ const getFingerprintLength = (value: Value): number => {
104
+ if (typeof value === "boolean") return 2;
105
+ if (typeof value === "number" || isModelValue(value)) return 5;
106
+ if (typeof value === "string") return 5 + value.length;
107
+ if (isFunctionValue(value)) {
108
+ const functionValue = toFiniteFunction(value);
109
+ return functionValue.domain.reduce<number>(
110
+ (length, key, index) =>
111
+ length + getFingerprintLength(key) + getFingerprintLength(functionValue.range[index]),
112
+ 5,
113
+ );
114
+ }
115
+ return toFiniteSet(value).elements.reduce<number>(
116
+ (length, element) => length + getFingerprintLength(element),
117
+ 5,
118
+ );
119
+ };
120
+
121
+ // The fingerprint after `length` zero bytes, as one table per byte of the fingerprint it shifts.
122
+ const createShiftTable = (length: number) => {
123
+ const table = new Int32Array(8 * 256 * 2);
124
+ for (let bit = 0; bit < 64; bit++) {
125
+ high = bit < 32 ? 0 : 1 << (bit - 32);
126
+ low = bit < 32 ? 1 << bit : 0;
127
+ for (let step = 0; step < length; step++) extendByte(0);
128
+ const position = bit >> 3;
129
+ const mask = 1 << (bit & 7);
130
+ for (let byte = mask; byte < 2 * mask; byte++) {
131
+ const entry = (position * 256 + byte) * 2;
132
+ const rest = (position * 256 + byte - mask) * 2;
133
+ table[entry] = table[rest] ^ high;
134
+ table[entry + 1] = table[rest + 1] ^ low;
135
+ }
136
+ }
137
+ return table;
138
+ };
139
+
140
+ // Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
141
+ // XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
142
+ // The fingerprinter caches each distinct function or set it meets, nested ones included, for one
143
+ // pass over the stored states, as states share most of their values. It reads a state's variables
144
+ // in `order` and writes each fingerprint's low and high words into `words`, an Int32Array over a
145
+ // little-endian BigInt64Array.
146
+ export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
147
+ const shiftTables = new Map<number, Int32Array>();
148
+ const indices = new Map<Value, number>();
149
+ const contributions: number[] = [];
150
+ const getShiftTable = (length: number) => {
151
+ const cached = shiftTables.get(length);
152
+ if (cached) return cached;
153
+ const table = createShiftTable(length);
154
+ shiftTables.set(length, table);
155
+ return table;
156
+ };
157
+ // Extends the fingerprint by a value's bytes and returns how many there are.
158
+ const extendElement = (value: Value): number => {
159
+ if (typeof value !== "object" || isModelValue(value)) {
160
+ extendValue(value);
161
+ return getFingerprintLength(value);
162
+ }
163
+ const contribution = getContribution(value);
164
+ const shifted = { high, low };
165
+ const table = getShiftTable(contributions[contribution]);
166
+ high = contributions[contribution + 1];
167
+ low = contributions[contribution + 2];
168
+ for (let position = 0; position < 8; position++) {
169
+ const word = position < 4 ? shifted.low : shifted.high;
170
+ const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
171
+ high ^= table[entry];
172
+ low ^= table[entry + 1];
173
+ }
174
+ return contributions[contribution];
175
+ };
176
+ const getContribution = (value: Value) => {
177
+ const cached = indices.get(value);
178
+ if (cached !== undefined) return cached;
179
+ const outer = { high, low };
180
+ high = 0;
181
+ low = 0;
182
+ const length = { value: 5 };
183
+ if (isFunctionValue(value)) {
184
+ const functionValue = toFiniteFunction(value);
185
+ extendByte(FCN_RCD_VALUE);
186
+ extendInt(functionValue.domain.length);
187
+ for (let index = 0; index < functionValue.domain.length; index++) {
188
+ length.value += extendElement(functionValue.domain[index]);
189
+ length.value += extendElement(functionValue.range[index]);
190
+ }
191
+ } else {
192
+ const elements = toFiniteSet(value).elements;
193
+ extendByte(SET_ENUM_VALUE);
194
+ extendInt(elements.length);
195
+ for (const element of elements) length.value += extendElement(element);
196
+ }
197
+ const contribution = contributions.length;
198
+ indices.set(value, contribution);
199
+ contributions.push(length.value, high, low);
200
+ high = outer.high;
201
+ low = outer.low;
202
+ return contribution;
203
+ };
204
+ return (state: Value[], position: number) => {
205
+ const outer = { high, low };
206
+ high = INITIAL_HIGH;
207
+ low = INITIAL_LOW;
208
+ for (const index of order) extendElement(state[index]);
209
+ words[2 * position] = low;
210
+ words[2 * position + 1] = high;
211
+ high = outer.high;
212
+ low = outer.low;
213
+ };
214
+ };
@@ -550,6 +550,10 @@ export const SANY_NODE_NAMES = [
550
550
  "N_AssertStep",
551
551
  ];
552
552
 
553
+ // The standard modules that a spec may ship its own file of, such as CommunityModules' Json.tla,
554
+ // which SANY reads in place of its own.
555
+ export const USER_STANDARD_MODULES = new Set(["Bags", "Json", "Randomization", "TLCExt"]);
556
+
553
557
  export const TLC_MODULE_SOURCES = new Map([
554
558
  [
555
559
  "Naturals",
@@ -0,0 +1,441 @@
1
+ import {
2
+ getErrorExitCode,
3
+ STACK_OVERFLOW_MESSAGE,
4
+ TlaEvaluationError,
5
+ TlaModuleError,
6
+ UNEXPECTED_EVAL_EXCEPTION,
7
+ UNEXPECTED_EXCEPTION,
8
+ } from "../errors";
9
+ import { setExitCode } from "../exit-codes";
10
+ import type { TlcVersion } from "./version";
11
+
12
+ // TLC's codes for the messages it prints, from tlc2.output.EC.
13
+ export const MESSAGE_CODE = {
14
+ general: 1000,
15
+ commandLineError: 1102,
16
+ initialState: 2102,
17
+ nestedExpression: 2103,
18
+ assumptionFalse: 2104,
19
+ assumptionEvaluationError: 2105,
20
+ exceptAppliedToUnknownField: 2144,
21
+ fingerprintFailed: 2147,
22
+ invariantViolatedInitial: 2107,
23
+ incompleteSuccessor: 2109,
24
+ propertyViolatedInitial: 2108,
25
+ invariantViolatedBehavior: 2110,
26
+ invariantEvaluationFailed: 2111,
27
+ actionPropertyViolatedBehavior: 2112,
28
+ actionPropertyEvaluationFailed: 2113,
29
+ deadlockReached: 2114,
30
+ temporalPropertyViolated: 2116,
31
+ behaviorUpToThisPoint: 2121,
32
+ backToState: 2122,
33
+ constantLevelInvariant: 2149,
34
+ javaOverrideLoaded: 2168,
35
+ starting: 2185,
36
+ modelChecking: 2187,
37
+ finished: 2186,
38
+ computingInit: 2189,
39
+ initGenerated1: 2190,
40
+ initGenerated2: 2191,
41
+ checkingTemporal: 2192,
42
+ success: 2193,
43
+ searchDepth: 2194,
44
+ stats: 2199,
45
+ progress: 2200,
46
+ temporalBranches: 2212,
47
+ statePrint2: 2217,
48
+ statePrint3: 2218,
49
+ sanyFinished: 2219,
50
+ startingSany: 2220,
51
+ stateLevelProperty: 2255,
52
+ version: 2262,
53
+ noSpecificationButProperty: 2257,
54
+ noFairnessButProperty: 2259,
55
+ counterExample: 2264,
56
+ checkingTemporalEnd: 2267,
57
+ stateGraphOutdegree: 2268,
58
+ computingInitProgress: 2269,
59
+ livenessSymmetry: 2279,
60
+ livenessConstraints: 2284,
61
+ symmetrySetTooSmall: 2300,
62
+ javaOverrideMismatch: 2400,
63
+ postconditionFalse: 2404,
64
+ postconditionFailed: 2405,
65
+ possibleUnwitnessed: 2780,
66
+ parsingFailed: 3002,
67
+ configFileError: 5001,
68
+ } as const;
69
+
70
+ // TLC's message levels, from tlc2.output.MP.
71
+ export const MESSAGE_LEVEL = { none: 0, error: 1, warning: 3, state: 4 } as const;
72
+
73
+ type MessageLevel = (typeof MESSAGE_LEVEL)[keyof typeof MESSAGE_LEVEL];
74
+
75
+ // Without -tool, TLC starts a message with the prefix of its level.
76
+ const LEVEL_PREFIXES: Record<MessageLevel, string> = {
77
+ 0: "",
78
+ 1: "Error: ",
79
+ 3: "Warning: ",
80
+ 4: "State ",
81
+ };
82
+
83
+ export interface MessagePrinter {
84
+ isTool: boolean;
85
+ version: TlcVersion;
86
+ print: (code: number, level: MessageLevel, text: string) => void;
87
+ }
88
+
89
+ // With -tool, TLC wraps each message in markers that name its code and level, so that a program
90
+ // can read the output. Lines outside the markers, such as the output of Print, stay as they are.
91
+ // MP.printWarning prints a warning once, which -nowarning drops, and without -tool, TLC follows the
92
+ // first warning with how to drop them.
93
+ export const createMessagePrinter = (
94
+ isTool: boolean,
95
+ isWarningEnabled = true,
96
+ version: TlcVersion = "master",
97
+ ): MessagePrinter => {
98
+ const warnings = new Set<string>();
99
+ return {
100
+ isTool,
101
+ version,
102
+ print: (code, level, text) => {
103
+ const isWarning = level === MESSAGE_LEVEL.warning;
104
+ if (isWarning && !isWarningEnabled) return;
105
+ const hint =
106
+ isWarning && !isTool && warnings.size === 0
107
+ ? "\n(Use the -nowarning option to disable this warning.)"
108
+ : "";
109
+ const message = isTool
110
+ ? `@!@!@STARTMSG ${code}:${level} @!@!@\n${text}\n@!@!@ENDMSG ${code} @!@!@`
111
+ : `${LEVEL_PREFIXES[level]}${text}${hint}`;
112
+ if (isWarning && warnings.has(message)) return;
113
+ if (isWarning) warnings.add(message);
114
+ console.log(message);
115
+ },
116
+ };
117
+ };
118
+
119
+ // TLC 1.7.4's message for a missing initial predicate, which blames a parameterized INSTANCE as
120
+ // well.
121
+ export const MISSING_INIT_174 =
122
+ "The configuration file did not specify the initial state predicate.\nCan also be caused by trying to run TLC on a specification from\na module imported with a parameterized INSTANCE statement.";
123
+
124
+ // The messages that TLC builds from a message code, from tlc2.output.MP, by their code and
125
+ // template, with whether TLC throws them as an EvalException rather than a TLCRuntimeException.
126
+ // TLC's -tool output wraps such a message in its own markers even inside another message.
127
+ const CODED_MESSAGES: [code: number, template: string, isEvalException: boolean][] = [
128
+ [1005, STACK_OVERFLOW_MESSAGE, false],
129
+ [2117, "Failed to recover the next state from its fingerprint.", true],
130
+ [2123, "Failed to recover the initial state from its fingerprint.", true],
131
+ [2132, "The first argument of Assert evaluated to FALSE; the second argument was:\n%1%", true],
132
+ [2145, "TLCGet(%1%) was undefined.", true],
133
+ [
134
+ 2154,
135
+ "Attempted to apply the operator overridden by the Java method\n%1%,\nbut it produced the following error:\n%2%",
136
+ false,
137
+ ],
138
+ [
139
+ 2148,
140
+ "The action formula A appearing in a WF_v(A) or SF_v(A) operator does not specify the primed value of the variable %1% occurring in the state formula v.\n",
141
+ false,
142
+ ],
143
+ [2155, "Attempted to compare %1% with the value\n%2%", true],
144
+ [2158, "Attempted to check if the value:\n%1%\nis an element of %2%.", true],
145
+ [
146
+ 2157,
147
+ "Attempted to apply TransitiveClosure to a set containing\nthe following value:\n%1%",
148
+ true,
149
+ ],
150
+ [2165, "Choose can only deal with numbers up to %1%", false],
151
+ [2169, "The %1% argument of %2% should be a %3%, but instead it is:\n%4%", true],
152
+ [2176, "Applying %1% to the following value,\nwhich is not %2%:\n%3%", true],
153
+ [
154
+ 2177,
155
+ "Attempted to apply BagUnion to the following set, whose\nelement is not a bag:\n%1%",
156
+ true,
157
+ ],
158
+ [2178, "Overflow when computing %1%", false],
159
+ [2179, "The second argument of \\div is 0.", true],
160
+ [2180, "0^0 is undefined.", true],
161
+ [2181, "Attempted to compute cardinality of the value\n%1%", true],
162
+ [2182, "Evaluating an expression of the form %1% when s is not a %2%:\n%3%", true],
163
+ [
164
+ 2183,
165
+ "The %1% argument of %2% must be in the domain of its %3% argument:\n%4%\n, but instead it is\n%5%",
166
+ true,
167
+ ],
168
+ [2184, "Attempted to apply %1% to the empty sequence.", true],
169
+ [2115, "No next state actions defined to generate successor states from.", false],
170
+ [
171
+ 2146,
172
+ "The invariant %1% is not a state predicate (one with no primes or temporal operators).%2%",
173
+ false,
174
+ ],
175
+ [2213, "TLC cannot handle the temporal formula %1%", false],
176
+ [2214, "Temporal formulas containing actions must be of forms <>[]A or []<>A.", false],
177
+ [2215, "TLC expected a %1% value, but did not find one. %2%", false],
178
+ [2222, "The constant parameter %1% is not assigned a value by the configuration file.", false],
179
+ [
180
+ 2223,
181
+ "In the configuration file, the identifier %1% appears\non the right-hand side of a <- after already appearing on the\nleft-hand side of one.",
182
+ false,
183
+ ],
184
+ [2224, "The configuration file substitutes for %1% with the undefined identifier %2%.", false],
185
+ [
186
+ 2225,
187
+ "The configuration file substitutes for %1% with %2% of different number of arguments.",
188
+ false,
189
+ ],
190
+ [
191
+ 2226,
192
+ "In the configuration file, the identifier %1% does not appear in the specification.",
193
+ false,
194
+ ],
195
+ [2227, "The configuration file cannot specify both INIT/NEXT and SPECIFICATION fields.", false],
196
+ [2228, "TLC requires %1% not to take any argument.", false],
197
+ [2228, "TLC requires %1% not to take any argument, but one was given: %2%", false],
198
+ [
199
+ 2229,
200
+ "The %1% %2% specified in the configuration file\nis not defined in the specification.",
201
+ false,
202
+ ],
203
+ [2230, "The %1% of %2% is equal to %3%", false],
204
+ [2231, "The configuration file did not specify the initial state predicate.", false],
205
+ [2231, MISSING_INIT_174, false],
206
+ [2232, "The configuration file did not specify the next state predicate.", false],
207
+ [2233, "The %1% %2% cannot be a constant.", false],
208
+ [2234, "The operator %1% cannot take any argument.", false],
209
+ [2235, "The operator %1% is not defined in the spec.", false],
210
+ [2237, "The spec is trivially false because %1% is false.", false],
211
+ [2238, "TLC cannot handle subscript %1%", false],
212
+ [2239, "TLC cannot handle this conjunct of the spec:\n%1%", false],
213
+ [2241, "The property %1% is not correctly defined.", false],
214
+ [2242, "The arity of the operator %1% is inconsistent in the configuration file.", false],
215
+ [2244, "TLC can't handle real numbers.\n%1%", false],
216
+ [2247, "In computing %1%, TLC expected a %2% expression,\nbut instead found %3%.\n%4%", false],
217
+ [2248, "In computing %1%, TLC expected a %2% expression,\nbut didn't find one.\n%3%", false],
218
+ [2260, "In computing ENABLED, TLC encountered a temporal formula (%1%).\n%2%", false],
219
+ [
220
+ 2261,
221
+ "TLC encountered a temporal formula (%1%) when evaluating a predicate or action.\n%2%",
222
+ false,
223
+ ],
224
+ [2265, "TLC can't handle a number this big.\n%1%", false],
225
+ [2266, "The %1% argument of %2% should be an %3%, but instead it is:\n%4%", true],
226
+ [2280, "In evaluation, the identifier %1% is either undefined or not an operator.\n%2%", false],
227
+ [2281, "The configuration file substitutes constant %1% with non-constant %2%%3%", false],
228
+ [2283, "The argument of %1% should be a %2%, but instead it is:\n%3%", true],
229
+ [
230
+ 5003,
231
+ "TLC found an error in the configuration file at line %1%\nThe keyword %2% was not followed by an identifier.",
232
+ false,
233
+ ],
234
+ [
235
+ 5004,
236
+ "TLC found an error in the configuration file at line %1%\nThe keyword %2% appeared twice.",
237
+ false,
238
+ ],
239
+ [
240
+ 5005,
241
+ "TLC found an error in the configuration file at line %1%\nExpected an identifier after %2%.",
242
+ false,
243
+ ],
244
+ [
245
+ 5006,
246
+ "TLC found an error in the configuration file at line %1%\nIt was expecting %2%, but did not find it.",
247
+ false,
248
+ ],
249
+ [5007, "TLC found an error in the configuration file:\n%1%", false],
250
+ // The general message is a prefix of the others above.
251
+ [5002, "TLC found an error in the configuration file at line %1%\n", false],
252
+ [2253, "Temporal formula is a tautology (its negation is unsatisfiable).", false],
253
+ [
254
+ 2301,
255
+ "TLC does not support temporal existential, nor universal, quantification over state variables.",
256
+ false,
257
+ ],
258
+ ];
259
+
260
+ interface CodedMessage {
261
+ code: number;
262
+ isEvalException: boolean;
263
+ }
264
+
265
+ const codedMessagePatterns = {
266
+ value: undefined as { pattern: RegExp; message: CodedMessage }[] | undefined,
267
+ };
268
+
269
+ const getCodedMessagePatterns = () =>
270
+ (codedMessagePatterns.value ??= CODED_MESSAGES.map(([code, template, isEvalException]) => ({
271
+ pattern: new RegExp(
272
+ `^${template
273
+ .split(/%\d+%/)
274
+ .map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
275
+ .join("[\\s\\S]*")}$`,
276
+ ),
277
+ message: { code, isEvalException },
278
+ })));
279
+
280
+ // The overflows of SetOfFcnsValue and SetOfTuplesValue, which TLC raises with a plain message that
281
+ // reads like the coded overflow of SetOfRcdsValue and SubsetValue.
282
+ const UNCODED_MESSAGE_PATTERN =
283
+ /^Overflow when computing the number of elements in( |:\n\[(?![A-Za-z0-9_]+ : ))/;
284
+
285
+ export const getCodedMessage = (text: string): CodedMessage | undefined =>
286
+ UNCODED_MESSAGE_PATTERN.test(text)
287
+ ? undefined
288
+ : getCodedMessagePatterns().find((item) => item.pattern.test(text))?.message;
289
+
290
+ // Whether TLC raises an error as an EvalException, which only a standard module's Java code throws,
291
+ // rather than as the TLCRuntimeException of every Assert.fail.
292
+ export const isEvalException = (error: TlaEvaluationError) =>
293
+ error instanceof TlaModuleError || getCodedMessage(error.message)?.isEvalException === true;
294
+
295
+ // The messages that end with the message of the error they report, after this text.
296
+ const INNER_MESSAGE_PREFIXES = new Map([
297
+ // "The exception was a <class>\n: "
298
+ [1000, "\n: "],
299
+ [2105, "failed.\n"],
300
+ [2154, "but it produced the following error:\n"],
301
+ [2213, ":\n"],
302
+ [2281, " - specifically: "],
303
+ ]);
304
+
305
+ // The text of a coded message with the message of the error it reports marked in -tool output, as
306
+ // TLC's message holds that error's text as TLC printed it.
307
+ export const formatInnerMessage = (code: number, text: string, isTool: boolean) => {
308
+ const prefix = isTool ? INNER_MESSAGE_PREFIXES.get(code) : undefined;
309
+ const index = prefix === undefined ? -1 : text.indexOf(prefix);
310
+ if (prefix === undefined || index === -1) return text;
311
+ const end = index + prefix.length;
312
+ return `${text.slice(0, end)}${formatNestedMessage(text.slice(end), isTool)}`;
313
+ };
314
+
315
+ // The text of an error's message as TLC prints it inside another message.
316
+ export const formatNestedMessage = (text: string, isTool: boolean): string => {
317
+ const code = isTool ? getCodedMessage(text)?.code : undefined;
318
+ return code === undefined
319
+ ? text
320
+ : `@!@!@STARTMSG ${code}:${MESSAGE_LEVEL.none} @!@!@\n${formatInnerMessage(code, text, isTool)}\n@!@!@ENDMSG ${code} @!@!@`;
321
+ };
322
+
323
+ const zeroPad = (value: number) => String(value).padStart(2, "0");
324
+
325
+ // TLC's convertRuntimeToHumanReadable: a duration in its two largest units.
326
+ export const formatTlcDuration = (milliseconds: number) => {
327
+ const seconds = Math.floor(milliseconds / 1000) % 60;
328
+ const minutes = Math.floor(milliseconds / 60_000) % 60;
329
+ const hours = Math.floor(milliseconds / 3_600_000);
330
+ if (milliseconds > 86_400_000) return `${Math.floor(hours / 24)}d ${zeroPad(hours % 24)}h`;
331
+ if (milliseconds > 3_600_000) return `${zeroPad(hours % 24)}h ${zeroPad(minutes)}min`;
332
+ return milliseconds > 60_000
333
+ ? `${zeroPad(minutes)}min ${zeroPad(seconds)}s`
334
+ : `${zeroPad(seconds)}s`;
335
+ };
336
+
337
+ // TLC's SimpleDateFormat("yyyy-MM-dd HH:mm:ss") in the local time zone.
338
+ export const formatTlcDate = (date: Date) =>
339
+ `${date.getFullYear()}-${zeroPad(date.getMonth() + 1)}-${zeroPad(date.getDate())} ${zeroPad(date.getHours())}:${zeroPad(date.getMinutes())}:${zeroPad(date.getSeconds())}`;
340
+
341
+ // An error that TLC reports with a message of its own, by that message's code and first line, and
342
+ // the error whose message follows.
343
+ interface HeadedError {
344
+ code: number;
345
+ headline: string;
346
+ inner: TlaEvaluationError;
347
+ }
348
+
349
+ const headedErrors = new WeakMap<Error, HeadedError>();
350
+
351
+ export const withHeadline = (error: unknown, code: number, headline: string) => {
352
+ if (!(error instanceof TlaEvaluationError)) return error;
353
+ const headed = setExitCode(
354
+ new TlaEvaluationError(`${headline}\n${error.message}`, error),
355
+ getErrorExitCode(error),
356
+ );
357
+ headedErrors.set(headed, { code, headline, inner: error });
358
+ return headed;
359
+ };
360
+
361
+ export const isHeadedError = (error: Error) => headedErrors.has(error);
362
+
363
+ const UNEXPECTED_HEADLINE = UNEXPECTED_EXCEPTION.slice(0, UNEXPECTED_EXCEPTION.indexOf("\n"));
364
+
365
+ // TLC 1.7.4's Worker wraps an error in adding a successor in a RuntimeException, whose message
366
+ // names the class of the error.
367
+ const WRAPPED_EVAL_EXCEPTION = `${UNEXPECTED_EXCEPTION}tlc2.tool.EvalException: `;
368
+ const WRAPPED_RUNTIME_EXCEPTION = `${UNEXPECTED_EXCEPTION}util.Assert$TLCRuntimeException: `;
369
+
370
+ export const getWrappedException = (error: TlaEvaluationError) =>
371
+ isEvalException(error) ? WRAPPED_EVAL_EXCEPTION : WRAPPED_RUNTIME_EXCEPTION;
372
+
373
+ // TLC reports an EvalException, or a TLCRuntimeException without a message of its own, as an
374
+ // exception it didn't expect.
375
+ export const withUnexpectedException = <Result>(compute: () => Result) => {
376
+ try {
377
+ return compute();
378
+ } catch (error) {
379
+ if (!(error instanceof TlaEvaluationError)) throw error;
380
+ if (isEvalException(error)) {
381
+ throw new TlaEvaluationError(`${UNEXPECTED_EVAL_EXCEPTION}${error.message}`);
382
+ }
383
+ if (getCodedMessage(error.message)) throw error;
384
+ throw new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`);
385
+ }
386
+ };
387
+
388
+ const UNEXPECTED_EXCEPTION_PREFIXES = [
389
+ WRAPPED_EVAL_EXCEPTION,
390
+ WRAPPED_RUNTIME_EXCEPTION,
391
+ UNEXPECTED_EXCEPTION,
392
+ UNEXPECTED_EVAL_EXCEPTION,
393
+ ];
394
+
395
+ // The text of an error's message, with the message of a coded error inside it marked in -tool
396
+ // output as TLC marks it.
397
+ const formatErrorMessage = (error: TlaEvaluationError, isTool: boolean): string => {
398
+ const headed = headedErrors.get(error);
399
+ if (headed) return `${headed.headline}\n${formatErrorMessage(headed.inner, isTool)}`;
400
+ const message = error.message;
401
+ const prefix = UNEXPECTED_EXCEPTION_PREFIXES.find((start) => message.startsWith(start)) ?? "";
402
+ return `${prefix}${formatNestedMessage(message.slice(prefix.length), isTool)}`;
403
+ };
404
+
405
+ // How TLC reports an error: with the code of its own message for an error that it reports with a
406
+ // headline; and, when the error escapes the computation of next states, with the code of an
407
+ // EvalException, or as an unexpected exception for any other error.
408
+ export interface FailureMessage {
409
+ code: number;
410
+ message: string;
411
+ toolMessage: string;
412
+ }
413
+
414
+ export const getFailureMessage = (
415
+ error: TlaEvaluationError,
416
+ isNextState: boolean,
417
+ ): FailureMessage => {
418
+ const headed = headedErrors.get(error);
419
+ const message = error.message;
420
+ if (headed) {
421
+ return {
422
+ code: headed.code,
423
+ message,
424
+ toolMessage: formatErrorMessage(error, true),
425
+ };
426
+ }
427
+ if (isNextState && isEvalException(error)) {
428
+ return {
429
+ code: getCodedMessage(message)?.code ?? MESSAGE_CODE.general,
430
+ message,
431
+ toolMessage: message,
432
+ };
433
+ }
434
+ const isUnexpected = isNextState && !message.startsWith(UNEXPECTED_HEADLINE);
435
+ const prefix = isUnexpected ? UNEXPECTED_EXCEPTION : "";
436
+ return {
437
+ code: MESSAGE_CODE.general,
438
+ message: `${prefix}${message}`,
439
+ toolMessage: `${prefix}${formatErrorMessage(error, true)}`,
440
+ };
441
+ };