tla-ts 0.0.3 → 0.0.4-dev.2a873ee

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 (370) 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 +996 -0
  17. package/dist/prove.js +996 -0
  18. package/dist/run-cli.cjs +37 -20
  19. package/dist/run-cli.js +37 -20
  20. package/dist/tlapm.cjs +10 -0
  21. package/dist/tlapm.js +10 -0
  22. package/dist/transfer.cjs +1387 -18
  23. package/dist/transfer.js +1387 -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 +4 -7
  29. package/src/builtins.ts +29 -11
  30. package/src/bundled-modules.ts +3 -3
  31. package/src/canonical.ts +4 -3
  32. package/src/check.ts +1540 -235
  33. package/src/cli-options.ts +12 -6
  34. package/src/cli.ts +8 -1
  35. package/src/codegen.ts +98 -39
  36. package/src/config.ts +254 -181
  37. package/src/dependencies.ts +2 -2
  38. package/src/environment.ts +540 -140
  39. package/src/errors.ts +284 -4
  40. package/src/evaluate.ts +1043 -293
  41. package/src/exit-codes.ts +22 -43
  42. package/src/fingerprints.ts +46 -11
  43. package/src/index.ts +11 -0
  44. package/src/instances.ts +19 -16
  45. package/src/java-overrides.ts +104 -25
  46. package/src/levels.ts +191 -45
  47. package/src/lexer.ts +127 -30
  48. package/src/live-check.ts +867 -0
  49. package/src/liveness.ts +218 -572
  50. package/src/{obligation-action.ts → obligations/action.ts} +247 -69
  51. package/src/{obligation-anon.ts → obligations/anon.ts} +49 -47
  52. package/src/obligations/ctx.ts +64 -0
  53. package/src/{obligation-deref.ts → obligations/deref.ts} +51 -92
  54. package/src/{obligation-expression.ts → obligations/expression.ts} +117 -57
  55. package/src/{obligation-format.ts → obligations/format.ts} +7 -8
  56. package/src/{obligation-generate.ts → obligations/generate.ts} +170 -192
  57. package/src/{obligations.ts → obligations/index.ts} +32 -16
  58. package/src/{obligation-levels.ts → obligations/levels.ts} +26 -28
  59. package/src/{obligation-module.ts → obligations/module.ts} +113 -121
  60. package/src/obligations/pickle.ts +98 -0
  61. package/src/obligations/pragma.ts +189 -0
  62. package/src/{obligation-printer.ts → obligations/printer.ts} +22 -80
  63. package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
  64. package/src/{obligation-source.ts → obligations/source.ts} +156 -99
  65. package/src/obligations/temporal.ts +321 -0
  66. package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
  67. package/src/ocaml-marshal.ts +166 -0
  68. package/src/parallel-worker.ts +169 -224
  69. package/src/parallel.ts +140 -81
  70. package/src/parser.ts +456 -147
  71. package/src/pcal-ast.ts +770 -0
  72. package/src/pcal-parse.ts +1523 -0
  73. package/src/pcal-tokenize.ts +516 -0
  74. package/src/pcal-translate.ts +1230 -0
  75. package/src/pcal-validate.ts +454 -0
  76. package/src/prove/decide-pool.ts +95 -0
  77. package/src/prove/deciders.ts +33 -0
  78. package/src/prove/first-order.ts +78 -0
  79. package/src/prove/isabelle/LICENSE +36 -0
  80. package/src/prove/isabelle/LICENSE-tlapm +26 -0
  81. package/src/prove/isabelle/decide.ts +47 -0
  82. package/src/prove/isabelle/encode.ts +446 -0
  83. package/src/prove/isabelle/isar/inner-syntax.ts +579 -0
  84. package/src/prove/isabelle/isar/method.ts +367 -0
  85. package/src/prove/isabelle/isar/proof.ts +28 -0
  86. package/src/prove/isabelle/isar/statement.ts +57 -0
  87. package/src/prove/isabelle/isar/theory.ts +192 -0
  88. package/src/prove/isabelle/isar/type-check.ts +278 -0
  89. package/src/prove/isabelle/provers/blast/blast.ts +258 -0
  90. package/src/prove/isabelle/provers/blast/branch.ts +322 -0
  91. package/src/prove/isabelle/provers/blast/prove.ts +347 -0
  92. package/src/prove/isabelle/provers/blast/rules.ts +330 -0
  93. package/src/prove/isabelle/provers/blast/state.ts +180 -0
  94. package/src/prove/isabelle/provers/blast/term.ts +546 -0
  95. package/src/prove/isabelle/provers/clasimp.ts +150 -0
  96. package/src/prove/isabelle/provers/classical.ts +389 -0
  97. package/src/prove/isabelle/provers/hypsubst.ts +433 -0
  98. package/src/prove/isabelle/pure/bicompose.ts +174 -0
  99. package/src/prove/isabelle/pure/consts.ts +32 -0
  100. package/src/prove/isabelle/pure/conv.ts +79 -0
  101. package/src/prove/isabelle/pure/drule.ts +264 -0
  102. package/src/prove/isabelle/pure/envir.ts +331 -0
  103. package/src/prove/isabelle/pure/goal.ts +53 -0
  104. package/src/prove/isabelle/pure/interrupt.ts +16 -0
  105. package/src/prove/isabelle/pure/list.ts +12 -0
  106. package/src/prove/isabelle/pure/logic.ts +330 -0
  107. package/src/prove/isabelle/pure/name.ts +98 -0
  108. package/src/prove/isabelle/pure/net.ts +215 -0
  109. package/src/prove/isabelle/pure/object-logic.ts +41 -0
  110. package/src/prove/isabelle/pure/pattern.ts +631 -0
  111. package/src/prove/isabelle/pure/raw-simplifier.ts +338 -0
  112. package/src/prove/isabelle/pure/rewriter.ts +602 -0
  113. package/src/prove/isabelle/pure/search.ts +68 -0
  114. package/src/prove/isabelle/pure/seq.ts +133 -0
  115. package/src/prove/isabelle/pure/simplifier.ts +106 -0
  116. package/src/prove/isabelle/pure/table.ts +201 -0
  117. package/src/prove/isabelle/pure/tactic.ts +168 -0
  118. package/src/prove/isabelle/pure/tactical.ts +264 -0
  119. package/src/prove/isabelle/pure/term-ord.ts +105 -0
  120. package/src/prove/isabelle/pure/term.ts +688 -0
  121. package/src/prove/isabelle/pure/thm.ts +783 -0
  122. package/src/prove/isabelle/pure/type.ts +121 -0
  123. package/src/prove/isabelle/pure/unify.ts +753 -0
  124. package/src/prove/isabelle/pure/variable.ts +25 -0
  125. package/src/prove/isabelle/tla/classical-setup.ts +101 -0
  126. package/src/prove/isabelle/tla/context.ts +74 -0
  127. package/src/prove/isabelle/tla/logic-data.ts +5 -0
  128. package/src/prove/isabelle/tla/logic.ts +215 -0
  129. package/src/prove/isabelle/tla/simpdata.ts +241 -0
  130. package/src/prove/isabelle/tools/intuitionistic.ts +132 -0
  131. package/src/prove/ocaml.ts +22 -0
  132. package/src/prove/prep/action.ts +508 -0
  133. package/src/prove/prep/defs.ts +33 -0
  134. package/src/prove/prep/elab.ts +156 -0
  135. package/src/prove/prep/enabled-axioms.ts +189 -0
  136. package/src/prove/prep/enabled.ts +43 -0
  137. package/src/prove/prep/fingerprint.ts +388 -0
  138. package/src/prove/prep/flatten.ts +70 -0
  139. package/src/prove/prep/level-comparison.ts +529 -0
  140. package/src/prove/prep/md5.ts +67 -0
  141. package/src/prove/prep/pragma.ts +221 -0
  142. package/src/prove/prep/trivial.ts +22 -0
  143. package/src/prove/prove-worker.ts +22 -0
  144. package/src/prove/prove.ts +498 -0
  145. package/src/prove/ptl/decide.ts +68 -0
  146. package/src/prove/ptl/encode.ts +255 -0
  147. package/src/prove/ptl/sat.ts +151 -0
  148. package/src/prove/ptl/tableau.ts +276 -0
  149. package/src/prove/smt/axiomatize.ts +69 -0
  150. package/src/prove/smt/axioms.ts +209 -0
  151. package/src/prove/smt/context.ts +108 -0
  152. package/src/prove/smt/data.ts +636 -0
  153. package/src/prove/smt/encode.ts +164 -0
  154. package/src/prove/smt/extensionality.ts +65 -0
  155. package/src/prove/smt/flatten.ts +112 -0
  156. package/src/prove/smt/format.ts +154 -0
  157. package/src/prove/smt/instances.ts +181 -0
  158. package/src/prove/smt/rewrite.ts +772 -0
  159. package/src/prove/smt/simplify.ts +57 -0
  160. package/src/prove/smt/solver/act-cache.ts +265 -0
  161. package/src/prove/smt/solver/arith-rewriter.ts +962 -0
  162. package/src/prove/smt/solver/array.ts +22 -0
  163. package/src/prove/smt/solver/asserted-formulas.ts +469 -0
  164. package/src/prove/smt/solver/ast-lt.ts +157 -0
  165. package/src/prove/smt/solver/ast-util.ts +91 -0
  166. package/src/prove/smt/solver/ast.ts +1315 -0
  167. package/src/prove/smt/solver/auf-solver.ts +572 -0
  168. package/src/prove/smt/solver/case-split-queue.ts +178 -0
  169. package/src/prove/smt/solver/cg-table.ts +175 -0
  170. package/src/prove/smt/solver/clause.ts +276 -0
  171. package/src/prove/smt/solver/conflict-resolution.ts +343 -0
  172. package/src/prove/smt/solver/context.ts +1292 -0
  173. package/src/prove/smt/solver/ctx-simplify.ts +334 -0
  174. package/src/prove/smt/solver/der.ts +224 -0
  175. package/src/prove/smt/solver/dyn-ack.ts +141 -0
  176. package/src/prove/smt/solver/e-matching.ts +659 -0
  177. package/src/prove/smt/solver/enode.ts +259 -0
  178. package/src/prove/smt/solver/expr-ref.ts +21 -0
  179. package/src/prove/smt/solver/expr-safe-replace.ts +172 -0
  180. package/src/prove/smt/solver/fingerprints.ts +92 -0
  181. package/src/prove/smt/solver/flat-literals.ts +51 -0
  182. package/src/prove/smt/solver/goal.ts +138 -0
  183. package/src/prove/smt/solver/hash.ts +173 -0
  184. package/src/prove/smt/solver/hashtable.ts +166 -0
  185. package/src/prove/smt/solver/internalizer.ts +1381 -0
  186. package/src/prove/smt/solver/lbool.ts +5 -0
  187. package/src/prove/smt/solver/literal.ts +24 -0
  188. package/src/prove/smt/solver/lp/bound-propagator.ts +391 -0
  189. package/src/prove/smt/solver/lp/constraints.ts +78 -0
  190. package/src/prove/smt/solver/lp/containers.ts +296 -0
  191. package/src/prove/smt/solver/lp/core-solver.ts +504 -0
  192. package/src/prove/smt/solver/lp/cross-nested.ts +224 -0
  193. package/src/prove/smt/solver/lp/dep-intervals.ts +642 -0
  194. package/src/prove/smt/solver/lp/dependency.ts +51 -0
  195. package/src/prove/smt/solver/lp/dioph-eq.ts +1181 -0
  196. package/src/prove/smt/solver/lp/explanation.ts +46 -0
  197. package/src/prove/smt/solver/lp/gomory.ts +255 -0
  198. package/src/prove/smt/solver/lp/hnf-cutter.ts +125 -0
  199. package/src/prove/smt/solver/lp/hnf.ts +281 -0
  200. package/src/prove/smt/solver/lp/int-cube.ts +295 -0
  201. package/src/prove/smt/solver/lp/int-solver.ts +514 -0
  202. package/src/prove/smt/solver/lp/lar-solver.ts +816 -0
  203. package/src/prove/smt/solver/lp/lar-term.ts +62 -0
  204. package/src/prove/smt/solver/lp/lp-settings.ts +91 -0
  205. package/src/prove/smt/solver/lp/maximize-term.ts +347 -0
  206. package/src/prove/smt/solver/lp/nex-creator.ts +381 -0
  207. package/src/prove/smt/solver/lp/nex.ts +120 -0
  208. package/src/prove/smt/solver/lp/nla-backoff.ts +43 -0
  209. package/src/prove/smt/solver/lp/nla-basics.ts +398 -0
  210. package/src/prove/smt/solver/lp/nla-core.ts +968 -0
  211. package/src/prove/smt/solver/lp/nla-divisions.ts +332 -0
  212. package/src/prove/smt/solver/lp/nla-emonics.ts +373 -0
  213. package/src/prove/smt/solver/lp/nla-factorization.ts +30 -0
  214. package/src/prove/smt/solver/lp/nla-grobner.ts +904 -0
  215. package/src/prove/smt/solver/lp/nla-horner.ts +126 -0
  216. package/src/prove/smt/solver/lp/nla-intervals.ts +265 -0
  217. package/src/prove/smt/solver/lp/nla-monomial-bounds.ts +715 -0
  218. package/src/prove/smt/solver/lp/nla-monotone.ts +59 -0
  219. package/src/prove/smt/solver/lp/nla-order.ts +307 -0
  220. package/src/prove/smt/solver/lp/nla-patcher.ts +112 -0
  221. package/src/prove/smt/solver/lp/nla-tangents.ts +251 -0
  222. package/src/prove/smt/solver/lp/nla-throttle.ts +85 -0
  223. package/src/prove/smt/solver/lp/nla-types.ts +124 -0
  224. package/src/prove/smt/solver/lp/nla-var-eqs.ts +141 -0
  225. package/src/prove/smt/solver/lp/nra-solver.ts +440 -0
  226. package/src/prove/smt/solver/lp/numeric-pair.ts +65 -0
  227. package/src/prove/smt/solver/lp/pdd-interval.ts +31 -0
  228. package/src/prove/smt/solver/lp/pdd-simplifier.ts +184 -0
  229. package/src/prove/smt/solver/lp/pdd-solver.ts +408 -0
  230. package/src/prove/smt/solver/lp/pdd.ts +971 -0
  231. package/src/prove/smt/solver/lp/random-updater.ts +138 -0
  232. package/src/prove/smt/solver/lp/static-matrix.ts +246 -0
  233. package/src/prove/smt/solver/lp/union-find.ts +96 -0
  234. package/src/prove/smt/solver/mam-compiler.ts +847 -0
  235. package/src/prove/smt/solver/mam-interpreter.ts +400 -0
  236. package/src/prove/smt/solver/maximize-ac-sharing.ts +118 -0
  237. package/src/prove/smt/solver/model-checker.ts +347 -0
  238. package/src/prove/smt/solver/model-evaluator.ts +262 -0
  239. package/src/prove/smt/solver/model-finder.ts +155 -0
  240. package/src/prove/smt/solver/model-generator.ts +97 -0
  241. package/src/prove/smt/solver/model-macro-solver.ts +728 -0
  242. package/src/prove/smt/solver/nlsat/algebraic.ts +1161 -0
  243. package/src/prove/smt/solver/nlsat/atoms.ts +238 -0
  244. package/src/prove/smt/solver/nlsat/evaluator.ts +303 -0
  245. package/src/prove/smt/solver/nlsat/explain.ts +607 -0
  246. package/src/prove/smt/solver/nlsat/interval-set.ts +351 -0
  247. package/src/prove/smt/solver/nlsat/levelwise.ts +737 -0
  248. package/src/prove/smt/solver/nlsat/mpbq.ts +161 -0
  249. package/src/prove/smt/solver/nlsat/polynomial.ts +1722 -0
  250. package/src/prove/smt/solver/nlsat/solver.ts +911 -0
  251. package/src/prove/smt/solver/nlsat/types.ts +43 -0
  252. package/src/prove/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
  253. package/src/prove/smt/solver/nlsat/upolynomial.ts +996 -0
  254. package/src/prove/smt/solver/nnf.ts +469 -0
  255. package/src/prove/smt/solver/pattern-inference.ts +351 -0
  256. package/src/prove/smt/solver/poly-rewriter.ts +477 -0
  257. package/src/prove/smt/solver/propagate-values.ts +83 -0
  258. package/src/prove/smt/solver/proto-model.ts +551 -0
  259. package/src/prove/smt/solver/pull-quant.ts +139 -0
  260. package/src/prove/smt/solver/qe-lite-der.ts +526 -0
  261. package/src/prove/smt/solver/qe-lite-fm.ts +576 -0
  262. package/src/prove/smt/solver/qe-lite.ts +381 -0
  263. package/src/prove/smt/solver/qi-queue.ts +362 -0
  264. package/src/prove/smt/solver/quant-tactics.ts +117 -0
  265. package/src/prove/smt/solver/quantifier-analyzer.ts +562 -0
  266. package/src/prove/smt/solver/quantifier-manager.ts +186 -0
  267. package/src/prove/smt/solver/random-gen.ts +10 -0
  268. package/src/prove/smt/solver/rational.ts +165 -0
  269. package/src/prove/smt/solver/ref-vector.ts +62 -0
  270. package/src/prove/smt/solver/relevancy.ts +247 -0
  271. package/src/prove/smt/solver/rewriter-tpl.ts +1159 -0
  272. package/src/prove/smt/solver/rewriter.ts +843 -0
  273. package/src/prove/smt/solver/rlimit.ts +79 -0
  274. package/src/prove/smt/solver/smtlib-parser.ts +526 -0
  275. package/src/prove/smt/solver/solver.ts +71 -0
  276. package/src/prove/smt/solver/sort.ts +35 -0
  277. package/src/prove/smt/solver/sticky-generations.ts +69 -0
  278. package/src/prove/smt/solver/theory-lra-axioms.ts +340 -0
  279. package/src/prove/smt/solver/theory-lra-bounds.ts +226 -0
  280. package/src/prove/smt/solver/theory-lra-final-check.ts +252 -0
  281. package/src/prove/smt/solver/theory-lra-internalize.ts +476 -0
  282. package/src/prove/smt/solver/theory-lra-model.ts +243 -0
  283. package/src/prove/smt/solver/theory-lra-propagate.ts +405 -0
  284. package/src/prove/smt/solver/theory-lra-state.ts +43 -0
  285. package/src/prove/smt/solver/theory-lra.ts +56 -0
  286. package/src/prove/smt/solver/var-subst.ts +258 -0
  287. package/src/prove/smt/solver/z3-license.txt +10 -0
  288. package/src/prove/smt/table.ts +170 -0
  289. package/src/prove/tlapm/tlapm.ts +96 -0
  290. package/src/prove/tlaps-module.ts +510 -0
  291. package/src/prove/verdict.ts +18 -0
  292. package/src/prove/zenon/LICENSE +32 -0
  293. package/src/prove/zenon/check/check.ts +105 -0
  294. package/src/prove/zenon/check/definitions.ts +144 -0
  295. package/src/prove/zenon/check/equality.ts +182 -0
  296. package/src/prove/zenon/check/functions.ts +62 -0
  297. package/src/prove/zenon/check/keys.ts +125 -0
  298. package/src/prove/zenon/check/propositional.ts +104 -0
  299. package/src/prove/zenon/check/records.ts +188 -0
  300. package/src/prove/zenon/check/relations.ts +121 -0
  301. package/src/prove/zenon/check/rewrites.ts +224 -0
  302. package/src/prove/zenon/check/sets.ts +302 -0
  303. package/src/prove/zenon/check/terms.ts +127 -0
  304. package/src/prove/zenon/check/tla.ts +126 -0
  305. package/src/prove/zenon/decide.ts +113 -0
  306. package/src/prove/zenon/encode.ts +379 -0
  307. package/src/prove/zenon/eqrel.ts +300 -0
  308. package/src/prove/zenon/equality-rules.ts +435 -0
  309. package/src/prove/zenon/expr.ts +551 -0
  310. package/src/prove/zenon/index-table.ts +350 -0
  311. package/src/prove/zenon/isar-case.ts +513 -0
  312. package/src/prove/zenon/isar.ts +1249 -0
  313. package/src/prove/zenon/limits.ts +15 -0
  314. package/src/prove/zenon/llproof.ts +173 -0
  315. package/src/prove/zenon/mltoll.ts +1566 -0
  316. package/src/prove/zenon/node.ts +297 -0
  317. package/src/prove/zenon/ocaml.ts +169 -0
  318. package/src/prove/zenon/parse.ts +247 -0
  319. package/src/prove/zenon/phrase.ts +191 -0
  320. package/src/prove/zenon/proof.ts +198 -0
  321. package/src/prove/zenon/propositional-rules.ts +387 -0
  322. package/src/prove/zenon/rules.ts +94 -0
  323. package/src/prove/zenon/search.ts +288 -0
  324. package/src/prove/zenon/tla/functions.ts +151 -0
  325. package/src/prove/zenon/tla/llproof.ts +480 -0
  326. package/src/prove/zenon/tla/records.ts +225 -0
  327. package/src/prove/zenon/tla/rewrites.ts +447 -0
  328. package/src/prove/zenon/tla/sets.ts +304 -0
  329. package/src/prove/zenon/tla/terms.ts +162 -0
  330. package/src/prove/zenon/tla/tla.ts +506 -0
  331. package/src/randomization.ts +24 -7
  332. package/src/resolve.ts +1481 -426
  333. package/src/run-cli.ts +1483 -212
  334. package/src/runtime.ts +19 -1
  335. package/src/sany-builtins.ts +147 -0
  336. package/src/sany-generator.ts +3128 -0
  337. package/src/sany-levels.ts +1208 -0
  338. package/src/sany-lexer.ts +545 -0
  339. package/src/sany-linter.ts +122 -0
  340. package/src/sany-modules.ts +1398 -0
  341. package/src/sany-nodes.ts +463 -0
  342. package/src/sany-parse-error.ts +2161 -0
  343. package/src/sany-report.ts +356 -0
  344. package/src/sany-selectors.ts +738 -0
  345. package/src/sany-source.ts +248 -0
  346. package/src/sany-xml.ts +1017 -0
  347. package/src/spec-description.ts +42 -11
  348. package/src/standard-modules.ts +203 -148
  349. package/src/standard-ranges.ts +116 -0
  350. package/src/state-graph.ts +180 -0
  351. package/src/subexpressions.ts +231 -171
  352. package/src/substitution-levels.ts +38 -17
  353. package/src/successors.ts +241 -110
  354. package/src/symmetry.ts +194 -54
  355. package/src/tableau.ts +54 -25
  356. package/src/tlapm-cache.ts +236 -0
  357. package/src/tlc-ext.ts +42 -29
  358. package/src/tlc-messages.ts +329 -0
  359. package/src/tlc-module.ts +52 -43
  360. package/src/tlc-modules.ts +4 -0
  361. package/src/tlc-order.ts +78 -10
  362. package/src/tlc-random.ts +84 -33
  363. package/src/tlc-replay.ts +655 -0
  364. package/src/tlc-summary.ts +20 -5
  365. package/src/tlc-version.ts +19 -0
  366. package/src/transfer.ts +15 -3
  367. package/src/values.ts +787 -190
  368. package/src/worker.ts +65 -0
  369. package/dist/parallel.cjs +0 -12
  370. package/dist/parallel.js +0 -12
@@ -0,0 +1,236 @@
1
+ // tlapm's fingerprints file, ported from src/backend/fpfile.ml.
2
+
3
+ import {
4
+ cpSync,
5
+ existsSync,
6
+ mkdirSync,
7
+ readdirSync,
8
+ readFileSync,
9
+ renameSync,
10
+ rmSync,
11
+ statSync,
12
+ writeFileSync,
13
+ } from "node:fs";
14
+ import { basename, join } from "node:path";
15
+ import { version } from "../package.json";
16
+ import { TlaProofError } from "./errors";
17
+ import { block, isBlock, readMarshaled, writeMarshaled, type OcamlValue } from "./ocaml-marshal";
18
+ import type { ProofAttempt } from "./prove/prove";
19
+
20
+ // One optional result per prover, in V13.str's order.
21
+ export type FingerprintTable = Map<string, OcamlValue[]>;
22
+
23
+ const MAGIC_NUMBER = 20210223;
24
+
25
+ const RECORD_SIZE = 23;
26
+
27
+ // Each prover's field in V13.str and constructor in V13.meth.
28
+ const PROVERS: Record<string, { field: number; tag: number }> = {
29
+ zenon: { field: 1, tag: 1 },
30
+ isabelle: { field: 2, tag: 0 },
31
+ smt: { field: 10, tag: 8 },
32
+ z3: { field: 11, tag: 9 },
33
+ ls4: { field: 17, tag: 16 },
34
+ };
35
+
36
+ const FAIL_METHOD = 5;
37
+
38
+ const REASONS = ["false", "timeout"];
39
+
40
+ // Stored as the tlapm, Zenon and Isabelle versions, which --safefp compares.
41
+ const VERSION = `tla-ts ${version}`;
42
+
43
+ const CHECKED_VERSIONS = new Map([
44
+ [PROVERS.zenon.field, 3],
45
+ [PROVERS.isabelle.field, 4],
46
+ ]);
47
+
48
+ const HISTORY_SIZE = 10;
49
+
50
+ const getField = (value: OcamlValue | undefined, index: number) =>
51
+ value !== undefined && isBlock(value) ? value.fields[index] : undefined;
52
+
53
+ const getFloat = (value: OcamlValue | undefined) =>
54
+ value !== undefined && typeof value === "object" && "float" in value ? value.float : Infinity;
55
+
56
+ // Hashtbl.hash on a string.
57
+ const hashKey = (key: string) => {
58
+ const bytes = new TextEncoder().encode(key);
59
+ const rotate = (value: number, shift: number) => (value << shift) | (value >>> (32 - shift));
60
+ const mixed = Array.from(
61
+ { length: Math.ceil(bytes.length / 4) },
62
+ (_, index) =>
63
+ bytes[index * 4] |
64
+ (bytes[index * 4 + 1] << 8) |
65
+ (bytes[index * 4 + 2] << 16) |
66
+ (bytes[index * 4 + 3] << 24),
67
+ ).reduce((hash, word) => {
68
+ const data = Math.imul(rotate(Math.imul(word, 0xcc9e2d51), 15), 0x1b873593);
69
+ return (Math.imul(rotate(hash ^ data, 13), 5) + 0xe6546b64) | 0;
70
+ }, 0);
71
+ const first = Math.imul(mixed ^ bytes.length ^ ((mixed ^ bytes.length) >>> 16), 0x85ebca6b);
72
+ const second = Math.imul(first ^ (first >>> 13), 0xc2b2ae35);
73
+ return (second ^ (second >>> 16)) & 0x3fffffff;
74
+ };
75
+
76
+ const readBucket = (entry: OcamlValue | undefined): [OcamlValue, OcamlValue][] =>
77
+ entry !== undefined && isBlock(entry)
78
+ ? [[entry.fields[0], entry.fields[1]], ...readBucket(entry.fields[2])]
79
+ : [];
80
+
81
+ // Fpfile.load_fingerprints. Each bucket lists the latest fingerprint first.
82
+ export const loadFingerprints = (table: FingerprintTable, path: string, isSafe: boolean) => {
83
+ const bytes = readFileSync(path);
84
+ const magic = readMarshaled(bytes, 0);
85
+ const fp = readMarshaled(bytes, magic.next).value;
86
+ if (magic.value !== MAGIC_NUMBER || !isBlock(fp) || fp.tag !== 0)
87
+ throw new TlaProofError("corrupted fingerprint file");
88
+ const buckets = getField(getField(fp, 0), 1);
89
+ const isTrusted = (sti: OcamlValue, field: number) => {
90
+ const index = CHECKED_VERSIONS.get(field);
91
+ return !isSafe || index === undefined || getField(getField(sti, 0), index) === VERSION;
92
+ };
93
+ table.clear();
94
+ (buckets !== undefined && isBlock(buckets) ? buckets.fields : [])
95
+ .flatMap((bucket) => readBucket(bucket).toReversed())
96
+ .forEach(([key, record]) => {
97
+ if (typeof key !== "string" || !isBlock(record))
98
+ throw new TlaProofError("corrupted fingerprint file");
99
+ table.set(
100
+ key,
101
+ record.fields.map((sti, field) => (isTrusted(sti, field) ? sti : 0)),
102
+ );
103
+ });
104
+ };
105
+
106
+ const decodeResult = (prover: string, sti: OcamlValue | undefined): ProofAttempt | undefined => {
107
+ const status = getField(sti, 0);
108
+ const outcome = getField(status, 0);
109
+ const argument = getField(getField(status, 1), 0);
110
+ if (outcome === undefined || outcome === 1) return undefined;
111
+ const seconds = getFloat(getField(prover === "isabelle" ? getField(argument, 0) : argument, 0));
112
+ const tactic = prover === "isabelle" ? getField(argument, 1) : undefined;
113
+ const reason = getField(getField(outcome, 0), 0);
114
+ return {
115
+ method: {
116
+ prover,
117
+ timeLimitMs: seconds * 1000,
118
+ ...(typeof tactic === "string" && { tactic }),
119
+ },
120
+ isProved: outcome === 0,
121
+ isReused: false,
122
+ ...(reason !== undefined && {
123
+ reason: typeof reason === "number" ? REASONS[reason] : String(getField(reason, 0)),
124
+ }),
125
+ };
126
+ };
127
+
128
+ // Fpfile.fp_writes, whose date counts months from 0.
129
+ const encodeResult = (attempt: ProofAttempt) => {
130
+ const now = new Date();
131
+ const method = attempt.method;
132
+ const timeout = { float: method.rlimit ?? method.timeLimitMs / 1000 };
133
+ const argument =
134
+ method.prover === "zenon"
135
+ ? block(0, timeout, FAIL_METHOD)
136
+ : method.prover === "isabelle"
137
+ ? block(0, block(0, timeout), method.tactic ?? "auto")
138
+ : block(0, timeout);
139
+ const reasonIndex = REASONS.indexOf(attempt.reason ?? "");
140
+ const reason =
141
+ attempt.reason === undefined
142
+ ? 0
143
+ : block(0, reasonIndex === -1 ? block(0, attempt.reason) : reasonIndex);
144
+ return block(
145
+ 0,
146
+ block(
147
+ 0,
148
+ block(
149
+ 0,
150
+ attempt.isProved ? 0 : block(0, reason),
151
+ block(PROVERS[method.prover].tag, argument),
152
+ ),
153
+ block(
154
+ 0,
155
+ now.getFullYear(),
156
+ now.getMonth(),
157
+ now.getDate(),
158
+ now.getHours(),
159
+ now.getMinutes(),
160
+ now.getSeconds(),
161
+ ),
162
+ VERSION,
163
+ VERSION,
164
+ VERSION,
165
+ ),
166
+ );
167
+ };
168
+
169
+ const encodeTable = (table: FingerprintTable) => {
170
+ const length = 2 ** Math.max(9, Math.ceil(Math.log2(table.size / 2)));
171
+ const buckets: OcamlValue[] = Array.from({ length }, () => 0);
172
+ table.forEach((record, fingerprint) => {
173
+ const index = hashKey(fingerprint) & (length - 1);
174
+ buckets[index] = block(0, fingerprint, block(0, ...record), buckets[index]);
175
+ });
176
+ return block(0, block(0, table.size, block(0, ...buckets), 0, 512));
177
+ };
178
+
179
+ const pad = (value: number) => String(value).padStart(2, "0");
180
+
181
+ // Fpfile.fp_init.
182
+ export const saveHistory = (path: string, sources: string[]) => {
183
+ const now = new Date();
184
+ const history = `${path}.history`;
185
+ const directory = join(
186
+ history,
187
+ `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}_${pad(now.getHours())}_${pad(now.getMinutes())}_${pad(now.getSeconds())}`,
188
+ );
189
+ mkdirSync(directory, { recursive: true });
190
+ [path, ...sources]
191
+ .filter((file) => existsSync(file))
192
+ .forEach((file) => cpSync(file, join(directory, basename(file)), { preserveTimestamps: true }));
193
+ readdirSync(history)
194
+ .map((name) => join(history, name))
195
+ .sort((left, right) => statSync(right).mtimeMs - statSync(left).mtimeMs)
196
+ .slice(HISTORY_SIZE)
197
+ .forEach((old) => rmSync(old, { recursive: true, force: true }));
198
+ };
199
+
200
+ // Fpfile.query: what each prover concluded for each fingerprint. Saving them, as fp_writes,
201
+ // add_to_table and fp_close_and_consolidate do, keeps each record and result loaded unchanged, with
202
+ // its date, versions and other provers' results.
203
+ export const getKnownResults = (table: FingerprintTable) => {
204
+ const storedResults = new Map<ProofAttempt, OcamlValue>();
205
+ const storedRecords = new Map<Map<string, ProofAttempt>, OcamlValue[]>();
206
+ const results = new Map(
207
+ [...table].map(([fingerprint, record]) => {
208
+ const attempts = new Map(
209
+ Object.entries(PROVERS).flatMap(([prover, known]) => {
210
+ const result = decodeResult(prover, getField(record[known.field], 0));
211
+ if (!result) return [];
212
+ storedResults.set(result, record[known.field]);
213
+ return [[prover, result]];
214
+ }),
215
+ );
216
+ storedRecords.set(attempts, record);
217
+ return [fingerprint, attempts];
218
+ }),
219
+ );
220
+ const save = (path: string) => {
221
+ table.clear();
222
+ results.forEach((attempts, fingerprint) => {
223
+ const record = [...(storedRecords.get(attempts) ?? Array<OcamlValue>(RECORD_SIZE).fill(0))];
224
+ attempts.forEach((attempt, prover) => {
225
+ record[PROVERS[prover].field] = storedResults.get(attempt) ?? encodeResult(attempt);
226
+ });
227
+ table.set(fingerprint, record);
228
+ });
229
+ writeFileSync(
230
+ `${path}.tmp`,
231
+ Buffer.concat([writeMarshaled(MAGIC_NUMBER), writeMarshaled(encodeTable(table))]),
232
+ );
233
+ renameSync(`${path}.tmp`, path);
234
+ };
235
+ return { results, save };
236
+ };
package/src/tlc-ext.ts CHANGED
@@ -10,17 +10,9 @@ import {
10
10
  } from "./builtins";
11
11
  import { TlaEvaluationError, TlaModuleError, TlaUnsupportedError } from "./errors";
12
12
  import { getTlcFingerprint } from "./tlc-random";
13
- import {
14
- evaluate,
15
- expectBoolean,
16
- getChildren,
17
- getDefinition,
18
- isLocallyResolved,
19
- type Builtin,
20
- type Environment,
21
- } from "./evaluate";
22
- import type { Definition, Expression } from "./ast";
23
- import type { TraceStep } from "./check";
13
+ import { formatNestedMessage } from "./tlc-messages";
14
+ import { evaluate, expectBoolean, getDefinition, type Builtin, type Environment } from "./evaluate";
15
+ import { getChildren, isLocallyResolved, type Definition, type Expression } from "./ast";
24
16
  import {
25
17
  getNamedRegisters,
26
18
  getPossibleCounts,
@@ -75,27 +67,21 @@ export const createActionRecord = (action: string | undefined) =>
75
67
  ]);
76
68
 
77
69
  // TLC's CounterExample is a graph of the violating behavior: the states numbered from 1, an edge
78
- // for each step, and for a lasso an edge from the last state back to where the loop starts.
70
+ // for each step, and for a lasso an edge from the last state back to where the loop starts. Each
71
+ // edge holds the record of the action TLC found for the step.
79
72
  export const createCounterExample = (
80
- trace: TraceStep[],
81
- loop: { start: number; action: string | undefined } | undefined,
73
+ stateRecords: Record<string, Value>[],
74
+ actions: Value[],
75
+ loop: { start: number; action: Value } | undefined,
82
76
  ) => {
83
- const states = trace.map((step, index) =>
84
- createTuple([index + 1, createRecord(Object.entries(step.state))]),
77
+ const states = stateRecords.map((state, index) =>
78
+ createTuple([index + 1, createRecord(Object.entries(state))]),
85
79
  );
86
80
  const steps = states
87
81
  .slice(1)
88
- .map((state, index) =>
89
- createTuple([states[index], createActionRecord(trace[index + 1].action), state]),
90
- );
82
+ .map((state, index) => createTuple([states[index], actions[index], state]));
91
83
  const closing = loop
92
- ? [
93
- createTuple([
94
- states[states.length - 1],
95
- createActionRecord(loop.action),
96
- states[loop.start],
97
- ]),
98
- ]
84
+ ? [createTuple([states[states.length - 1], loop.action, states[loop.start]])]
99
85
  : [];
100
86
  return createRecord([
101
87
  ["action", createSet([...steps, ...closing])],
@@ -111,15 +97,35 @@ const getRegister = (runtime: CheckerRuntime, key: Value) => {
111
97
  return undefined;
112
98
  };
113
99
 
114
- const setRegister = (runtime: CheckerRuntime, key: Value, value: Value) => {
100
+ // TLC's TLCSet. TLCExt's TLCGetAndSet is a TLA+ definition that calls it.
101
+ export const setTlcValue = (
102
+ runtime: CheckerRuntime,
103
+ print: (text: string) => void,
104
+ key: Value,
105
+ value: Value,
106
+ ) => {
115
107
  if (typeof key === "number" && key >= 0) {
116
108
  runtime.registers.set(key, value);
117
109
  return;
118
110
  }
111
+ if (key === "exit") {
112
+ if (value === true) runtime.isStopped = true;
113
+ return;
114
+ }
115
+ if (key === "pause") {
116
+ if (value !== true) return;
117
+ print("Press enter to resume model checking.");
118
+ runtime.pause();
119
+ return;
120
+ }
119
121
  if (typeof key === "string" && key.startsWith(NAMED_REGISTER_PREFIX)) {
120
122
  runtime.namedRegisters.set(key, value);
121
123
  return;
122
124
  }
125
+ if (typeof key === "string" && key.startsWith("-D")) {
126
+ runtime.properties.set(key.slice(2), formatValue(value));
127
+ return;
128
+ }
123
129
  throw new TlaModuleError(
124
130
  `The first argument of TLCSet should be a nonnegative integer, but instead it is:\n${formatValue(key)}`,
125
131
  );
@@ -189,7 +195,9 @@ export const createTlcExtOperators = (
189
195
  try {
190
196
  getThunk(args, 1, "AssertError")();
191
197
  } catch (error) {
192
- if (error instanceof TlaEvaluationError) return error.message === expected;
198
+ if (error instanceof TlaEvaluationError) {
199
+ return formatNestedMessage(error.message, runtime.isTool) === expected;
200
+ }
193
201
  throw error;
194
202
  }
195
203
  return false;
@@ -203,7 +211,12 @@ export const createTlcExtOperators = (
203
211
  const key = getValue(args, 0, "TLCGetAndSet");
204
212
  const previous = getRegister(runtime, key) ?? getValue(args, 3, "TLCGetAndSet");
205
213
  const update = getOperator(args, 1, "TLCGetAndSet");
206
- setRegister(runtime, key, update.call([previous, getValue(args, 2, "TLCGetAndSet")]));
214
+ setTlcValue(
215
+ runtime,
216
+ print,
217
+ key,
218
+ update.call([previous, getValue(args, 2, "TLCGetAndSet")]),
219
+ );
207
220
  return previous;
208
221
  }),
209
222
  isStateLevel: true,
@@ -0,0 +1,329 @@
1
+ import { STACK_OVERFLOW_MESSAGE, TlaModuleError, type TlaEvaluationError } from "./errors";
2
+ import type { TlcVersion } from "./tlc-version";
3
+
4
+ // TLC's codes for the messages it prints, from tlc2.output.EC.
5
+ export const MESSAGE_CODE = {
6
+ general: 1000,
7
+ commandLineError: 1102,
8
+ initialState: 2102,
9
+ nestedExpression: 2103,
10
+ assumptionFalse: 2104,
11
+ assumptionEvaluationError: 2105,
12
+ exceptAppliedToUnknownField: 2144,
13
+ fingerprintFailed: 2147,
14
+ invariantViolatedInitial: 2107,
15
+ incompleteSuccessor: 2109,
16
+ propertyViolatedInitial: 2108,
17
+ invariantViolatedBehavior: 2110,
18
+ invariantEvaluationFailed: 2111,
19
+ actionPropertyViolatedBehavior: 2112,
20
+ actionPropertyEvaluationFailed: 2113,
21
+ deadlockReached: 2114,
22
+ temporalPropertyViolated: 2116,
23
+ behaviorUpToThisPoint: 2121,
24
+ backToState: 2122,
25
+ constantLevelInvariant: 2149,
26
+ javaOverrideLoaded: 2168,
27
+ starting: 2185,
28
+ modelChecking: 2187,
29
+ finished: 2186,
30
+ computingInit: 2189,
31
+ initGenerated1: 2190,
32
+ initGenerated2: 2191,
33
+ checkingTemporal: 2192,
34
+ success: 2193,
35
+ searchDepth: 2194,
36
+ stats: 2199,
37
+ progress: 2200,
38
+ temporalBranches: 2212,
39
+ statePrint2: 2217,
40
+ statePrint3: 2218,
41
+ sanyFinished: 2219,
42
+ startingSany: 2220,
43
+ stateLevelProperty: 2255,
44
+ version: 2262,
45
+ noSpecificationButProperty: 2257,
46
+ noFairnessButProperty: 2259,
47
+ counterExample: 2264,
48
+ checkingTemporalEnd: 2267,
49
+ stateGraphOutdegree: 2268,
50
+ computingInitProgress: 2269,
51
+ livenessSymmetry: 2279,
52
+ livenessConstraints: 2284,
53
+ symmetrySetTooSmall: 2300,
54
+ javaOverrideMismatch: 2400,
55
+ postconditionFalse: 2404,
56
+ postconditionFailed: 2405,
57
+ possibleUnwitnessed: 2780,
58
+ parsingFailed: 3002,
59
+ configFileError: 5001,
60
+ } as const;
61
+
62
+ // TLC's message levels, from tlc2.output.MP.
63
+ export const MESSAGE_LEVEL = { none: 0, error: 1, warning: 3, state: 4 } as const;
64
+
65
+ type MessageLevel = (typeof MESSAGE_LEVEL)[keyof typeof MESSAGE_LEVEL];
66
+
67
+ // Without -tool, TLC starts a message with the prefix of its level.
68
+ const LEVEL_PREFIXES: Record<MessageLevel, string> = {
69
+ 0: "",
70
+ 1: "Error: ",
71
+ 3: "Warning: ",
72
+ 4: "State ",
73
+ };
74
+
75
+ export interface MessagePrinter {
76
+ isTool: boolean;
77
+ version: TlcVersion;
78
+ print: (code: number, level: MessageLevel, text: string) => void;
79
+ }
80
+
81
+ // With -tool, TLC wraps each message in markers that name its code and level, so that a program
82
+ // can read the output. Lines outside the markers, such as the output of Print, stay as they are.
83
+ // MP.printWarning prints a warning once, which -nowarning drops, and without -tool, TLC follows the
84
+ // first warning with how to drop them.
85
+ export const createMessagePrinter = (
86
+ isTool: boolean,
87
+ isWarningEnabled = true,
88
+ version: TlcVersion = "master",
89
+ ): MessagePrinter => {
90
+ const warnings = new Set<string>();
91
+ return {
92
+ isTool,
93
+ version,
94
+ print: (code, level, text) => {
95
+ const isWarning = level === MESSAGE_LEVEL.warning;
96
+ if (isWarning && !isWarningEnabled) return;
97
+ const hint =
98
+ isWarning && !isTool && warnings.size === 0
99
+ ? "\n(Use the -nowarning option to disable this warning.)"
100
+ : "";
101
+ const message = isTool
102
+ ? `@!@!@STARTMSG ${code}:${level} @!@!@\n${text}\n@!@!@ENDMSG ${code} @!@!@`
103
+ : `${LEVEL_PREFIXES[level]}${text}${hint}`;
104
+ if (isWarning && warnings.has(message)) return;
105
+ if (isWarning) warnings.add(message);
106
+ console.log(message);
107
+ },
108
+ };
109
+ };
110
+
111
+ // TLC 1.7.4's message for a missing initial predicate, which blames a parameterized INSTANCE as
112
+ // well.
113
+ export const MISSING_INIT_174 =
114
+ "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.";
115
+
116
+ // The messages that TLC builds from a message code, from tlc2.output.MP, by their code and
117
+ // template, with whether TLC throws them as an EvalException rather than a TLCRuntimeException.
118
+ // TLC's -tool output wraps such a message in its own markers even inside another message.
119
+ const CODED_MESSAGES: [code: number, template: string, isEvalException: boolean][] = [
120
+ [1005, STACK_OVERFLOW_MESSAGE, false],
121
+ [2117, "Failed to recover the next state from its fingerprint.", true],
122
+ [2123, "Failed to recover the initial state from its fingerprint.", true],
123
+ [2132, "The first argument of Assert evaluated to FALSE; the second argument was:\n%1%", true],
124
+ [2145, "TLCGet(%1%) was undefined.", true],
125
+ [
126
+ 2154,
127
+ "Attempted to apply the operator overridden by the Java method\n%1%,\nbut it produced the following error:\n%2%",
128
+ false,
129
+ ],
130
+ [
131
+ 2148,
132
+ "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",
133
+ false,
134
+ ],
135
+ [2155, "Attempted to compare %1% with the value\n%2%", true],
136
+ [2158, "Attempted to check if the value:\n%1%\nis an element of %2%.", true],
137
+ [
138
+ 2157,
139
+ "Attempted to apply TransitiveClosure to a set containing\nthe following value:\n%1%",
140
+ true,
141
+ ],
142
+ [2165, "Choose can only deal with numbers up to %1%", false],
143
+ [2169, "The %1% argument of %2% should be a %3%, but instead it is:\n%4%", true],
144
+ [2176, "Applying %1% to the following value,\nwhich is not %2%:\n%3%", true],
145
+ [
146
+ 2177,
147
+ "Attempted to apply BagUnion to the following set, whose\nelement is not a bag:\n%1%",
148
+ true,
149
+ ],
150
+ [2178, "Overflow when computing %1%", false],
151
+ [2179, "The second argument of \\div is 0.", true],
152
+ [2180, "0^0 is undefined.", true],
153
+ [2181, "Attempted to compute cardinality of the value\n%1%", true],
154
+ [2182, "Evaluating an expression of the form %1% when s is not a %2%:\n%3%", true],
155
+ [
156
+ 2183,
157
+ "The %1% argument of %2% must be in the domain of its %3% argument:\n%4%\n, but instead it is\n%5%",
158
+ true,
159
+ ],
160
+ [2184, "Attempted to apply %1% to the empty sequence.", true],
161
+ [2115, "No next state actions defined to generate successor states from.", false],
162
+ [
163
+ 2146,
164
+ "The invariant %1% is not a state predicate (one with no primes or temporal operators).%2%",
165
+ false,
166
+ ],
167
+ [2213, "TLC cannot handle the temporal formula %1%", false],
168
+ [2214, "Temporal formulas containing actions must be of forms <>[]A or []<>A.", false],
169
+ [2215, "TLC expected a %1% value, but did not find one. %2%", false],
170
+ [2222, "The constant parameter %1% is not assigned a value by the configuration file.", false],
171
+ [
172
+ 2223,
173
+ "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.",
174
+ false,
175
+ ],
176
+ [2224, "The configuration file substitutes for %1% with the undefined identifier %2%.", false],
177
+ [
178
+ 2225,
179
+ "The configuration file substitutes for %1% with %2% of different number of arguments.",
180
+ false,
181
+ ],
182
+ [
183
+ 2226,
184
+ "In the configuration file, the identifier %1% does not appear in the specification.",
185
+ false,
186
+ ],
187
+ [2227, "The configuration file cannot specify both INIT/NEXT and SPECIFICATION fields.", false],
188
+ [2228, "TLC requires %1% not to take any argument.", false],
189
+ [2228, "TLC requires %1% not to take any argument, but one was given: %2%", false],
190
+ [
191
+ 2229,
192
+ "The %1% %2% specified in the configuration file\nis not defined in the specification.",
193
+ false,
194
+ ],
195
+ [2230, "The %1% of %2% is equal to %3%", false],
196
+ [2231, "The configuration file did not specify the initial state predicate.", false],
197
+ [2231, MISSING_INIT_174, false],
198
+ [2232, "The configuration file did not specify the next state predicate.", false],
199
+ [2233, "The %1% %2% cannot be a constant.", false],
200
+ [2234, "The operator %1% cannot take any argument.", false],
201
+ [2235, "The operator %1% is not defined in the spec.", false],
202
+ [2237, "The spec is trivially false because %1% is false.", false],
203
+ [2238, "TLC cannot handle subscript %1%", false],
204
+ [2239, "TLC cannot handle this conjunct of the spec:\n%1%", false],
205
+ [2241, "The property %1% is not correctly defined.", false],
206
+ [2242, "The arity of the operator %1% is inconsistent in the configuration file.", false],
207
+ [2244, "TLC can't handle real numbers.\n%1%", false],
208
+ [2247, "In computing %1%, TLC expected a %2% expression,\nbut instead found %3%.\n%4%", false],
209
+ [2248, "In computing %1%, TLC expected a %2% expression,\nbut didn't find one.\n%3%", false],
210
+ [2260, "In computing ENABLED, TLC encountered a temporal formula (%1%).\n%2%", false],
211
+ [
212
+ 2261,
213
+ "TLC encountered a temporal formula (%1%) when evaluating a predicate or action.\n%2%",
214
+ false,
215
+ ],
216
+ [2265, "TLC can't handle a number this big.\n%1%", false],
217
+ [2266, "The %1% argument of %2% should be an %3%, but instead it is:\n%4%", true],
218
+ [2280, "In evaluation, the identifier %1% is either undefined or not an operator.\n%2%", false],
219
+ [2281, "The configuration file substitutes constant %1% with non-constant %2%%3%", false],
220
+ [2283, "The argument of %1% should be a %2%, but instead it is:\n%3%", true],
221
+ [
222
+ 5003,
223
+ "TLC found an error in the configuration file at line %1%\nThe keyword %2% was not followed by an identifier.",
224
+ false,
225
+ ],
226
+ [
227
+ 5004,
228
+ "TLC found an error in the configuration file at line %1%\nThe keyword %2% appeared twice.",
229
+ false,
230
+ ],
231
+ [
232
+ 5005,
233
+ "TLC found an error in the configuration file at line %1%\nExpected an identifier after %2%.",
234
+ false,
235
+ ],
236
+ [
237
+ 5006,
238
+ "TLC found an error in the configuration file at line %1%\nIt was expecting %2%, but did not find it.",
239
+ false,
240
+ ],
241
+ [5007, "TLC found an error in the configuration file:\n%1%", false],
242
+ // The general message is a prefix of the others above.
243
+ [5002, "TLC found an error in the configuration file at line %1%\n", false],
244
+ [2253, "Temporal formula is a tautology (its negation is unsatisfiable).", false],
245
+ [
246
+ 2301,
247
+ "TLC does not support temporal existential, nor universal, quantification over state variables.",
248
+ false,
249
+ ],
250
+ ];
251
+
252
+ interface CodedMessage {
253
+ code: number;
254
+ isEvalException: boolean;
255
+ }
256
+
257
+ const codedMessagePatterns = {
258
+ value: undefined as { pattern: RegExp; message: CodedMessage }[] | undefined,
259
+ };
260
+
261
+ const getCodedMessagePatterns = () =>
262
+ (codedMessagePatterns.value ??= CODED_MESSAGES.map(([code, template, isEvalException]) => ({
263
+ pattern: new RegExp(
264
+ `^${template
265
+ .split(/%\d+%/)
266
+ .map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
267
+ .join("[\\s\\S]*")}$`,
268
+ ),
269
+ message: { code, isEvalException },
270
+ })));
271
+
272
+ // The overflows of SetOfFcnsValue and SetOfTuplesValue, which TLC raises with a plain message that
273
+ // reads like the coded overflow of SetOfRcdsValue and SubsetValue.
274
+ const UNCODED_MESSAGE_PATTERN =
275
+ /^Overflow when computing the number of elements in( |:\n\[(?![A-Za-z0-9_]+ : ))/;
276
+
277
+ export const getCodedMessage = (text: string): CodedMessage | undefined =>
278
+ UNCODED_MESSAGE_PATTERN.test(text)
279
+ ? undefined
280
+ : getCodedMessagePatterns().find((item) => item.pattern.test(text))?.message;
281
+
282
+ // Whether TLC raises an error as an EvalException, which only a standard module's Java code throws,
283
+ // rather than as the TLCRuntimeException of every Assert.fail.
284
+ export const isEvalException = (error: TlaEvaluationError) =>
285
+ error instanceof TlaModuleError || getCodedMessage(error.message)?.isEvalException === true;
286
+
287
+ // The messages that end with the message of the error they report, after this text.
288
+ const INNER_MESSAGE_PREFIXES = new Map([
289
+ // "The exception was a <class>\n: "
290
+ [1000, "\n: "],
291
+ [2105, "failed.\n"],
292
+ [2154, "but it produced the following error:\n"],
293
+ [2213, ":\n"],
294
+ [2281, " - specifically: "],
295
+ ]);
296
+
297
+ // The text of a coded message with the message of the error it reports marked in -tool output, as
298
+ // TLC's message holds that error's text as TLC printed it.
299
+ export const formatInnerMessage = (code: number, text: string, isTool: boolean) => {
300
+ const prefix = isTool ? INNER_MESSAGE_PREFIXES.get(code) : undefined;
301
+ const index = prefix === undefined ? -1 : text.indexOf(prefix);
302
+ if (prefix === undefined || index === -1) return text;
303
+ const end = index + prefix.length;
304
+ return `${text.slice(0, end)}${formatNestedMessage(text.slice(end), isTool)}`;
305
+ };
306
+
307
+ // The text of an error's message as TLC prints it inside another message.
308
+ export const formatNestedMessage = (text: string, isTool: boolean): string => {
309
+ const code = isTool ? getCodedMessage(text)?.code : undefined;
310
+ return code === undefined
311
+ ? text
312
+ : `@!@!@STARTMSG ${code}:${MESSAGE_LEVEL.none} @!@!@\n${formatInnerMessage(code, text, isTool)}\n@!@!@ENDMSG ${code} @!@!@`;
313
+ };
314
+
315
+ const pad = (value: number) => String(value).padStart(2, "0");
316
+
317
+ // TLC's convertRuntimeToHumanReadable: a duration in its two largest units.
318
+ export const formatTlcDuration = (milliseconds: number) => {
319
+ const seconds = Math.floor(milliseconds / 1000) % 60;
320
+ const minutes = Math.floor(milliseconds / 60_000) % 60;
321
+ const hours = Math.floor(milliseconds / 3_600_000);
322
+ if (milliseconds > 86_400_000) return `${Math.floor(hours / 24)}d ${pad(hours % 24)}h`;
323
+ if (milliseconds > 3_600_000) return `${pad(hours % 24)}h ${pad(minutes)}min`;
324
+ return milliseconds > 60_000 ? `${pad(minutes)}min ${pad(seconds)}s` : `${pad(seconds)}s`;
325
+ };
326
+
327
+ // TLC's SimpleDateFormat("yyyy-MM-dd HH:mm:ss") in the local time zone.
328
+ export const formatTlcDate = (date: Date) =>
329
+ `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;