tla-ts 0.0.4 → 0.0.5-dev.552d313

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 (378) hide show
  1. package/README.md +38 -127
  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 +46 -12
  8. package/dist/index.d.ts +46 -12
  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 +76 -92
  23. package/dist/transfer.js +76 -92
  24. package/dist/worker.cjs +21 -0
  25. package/dist/worker.js +21 -0
  26. package/package.json +21 -12
  27. package/src/ast.ts +2 -2
  28. package/src/bags.ts +22 -23
  29. package/src/builtins.ts +16 -8
  30. package/src/check.ts +131 -1153
  31. package/src/{cli-options.ts → cli/options.ts} +54 -9
  32. package/src/cli/run.ts +63 -0
  33. package/src/cli/shared.ts +106 -0
  34. package/src/cli/tlapm.ts +374 -0
  35. package/src/{run-cli.ts → cli/tlc.ts} +115 -721
  36. package/src/cli/xml-exporter.ts +221 -0
  37. package/src/cli.ts +1 -1
  38. package/src/codegen.ts +43 -22
  39. package/src/config.ts +5 -5
  40. package/src/dependencies.ts +4 -6
  41. package/src/environment.ts +55 -60
  42. package/src/errors.ts +97 -35
  43. package/src/evaluate.ts +361 -181
  44. package/src/exit-codes.ts +1 -1
  45. package/src/fingerprints.ts +3 -3
  46. package/src/index.ts +10 -0
  47. package/src/instances.ts +56 -46
  48. package/src/java-overrides.ts +79 -41
  49. package/src/json.ts +31 -36
  50. package/src/levels.ts +10 -13
  51. package/src/{live-check.ts → liveness-checker.ts} +207 -141
  52. package/src/liveness.ts +38 -34
  53. package/src/{obligation-action.ts → obligations/action.ts} +248 -74
  54. package/src/{obligation-anon.ts → obligations/anonymize.ts} +55 -57
  55. package/src/obligations/equality.ts +260 -0
  56. package/src/obligations/expression.ts +618 -0
  57. package/src/{obligation-format.ts → obligations/format.ts} +7 -8
  58. package/src/obligations/generate.ts +457 -0
  59. package/src/{obligations.ts → obligations/index.ts} +46 -32
  60. package/src/{obligation-levels.ts → obligations/levels.ts} +44 -40
  61. package/src/obligations/module.ts +445 -0
  62. package/src/obligations/normalize.ts +554 -0
  63. package/src/obligations/operators.ts +176 -0
  64. package/src/obligations/pragma.ts +195 -0
  65. package/src/{obligation-printer.ts → obligations/printer.ts} +65 -230
  66. package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
  67. package/src/obligations/regroup.ts +100 -0
  68. package/src/{obligation-generate.ts → obligations/simplify.ts} +130 -536
  69. package/src/{obligation-source.ts → obligations/source.ts} +172 -216
  70. package/src/{obligation-deref.ts → obligations/subexpression.ts} +84 -115
  71. package/src/obligations/substitution.ts +261 -0
  72. package/src/obligations/temporal.ts +321 -0
  73. package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
  74. package/src/parallel-worker.ts +39 -51
  75. package/src/parallel.ts +39 -66
  76. package/src/parser.ts +67 -74
  77. package/src/{pcal-ast.ts → pcal/ast.ts} +2 -17
  78. package/src/{pcal-parse.ts → pcal/parse.ts} +28 -64
  79. package/src/{pcal-tokenize.ts → pcal/tokenize.ts} +10 -39
  80. package/src/{pcal-translate.ts → pcal/translate.ts} +30 -109
  81. package/src/{pcal-validate.ts → pcal/validate.ts} +10 -20
  82. package/src/prover/decide-pool.ts +95 -0
  83. package/src/prover/deciders.ts +33 -0
  84. package/src/prover/first-order.ts +175 -0
  85. package/src/prover/isabelle/LICENSE +36 -0
  86. package/src/prover/isabelle/LICENSE-tlapm +26 -0
  87. package/src/prover/isabelle/decide.ts +44 -0
  88. package/src/prover/isabelle/encode.ts +445 -0
  89. package/src/prover/isabelle/isar/inner-syntax.ts +579 -0
  90. package/src/prover/isabelle/isar/method.ts +367 -0
  91. package/src/prover/isabelle/isar/proof.ts +28 -0
  92. package/src/prover/isabelle/isar/statement.ts +57 -0
  93. package/src/prover/isabelle/isar/theory.ts +192 -0
  94. package/src/prover/isabelle/isar/type-check.ts +278 -0
  95. package/src/prover/isabelle/provers/blast/blast.ts +258 -0
  96. package/src/prover/isabelle/provers/blast/branch.ts +322 -0
  97. package/src/prover/isabelle/provers/blast/prove.ts +347 -0
  98. package/src/prover/isabelle/provers/blast/rules.ts +330 -0
  99. package/src/prover/isabelle/provers/blast/state.ts +180 -0
  100. package/src/prover/isabelle/provers/blast/term.ts +546 -0
  101. package/src/prover/isabelle/provers/clasimp.ts +150 -0
  102. package/src/prover/isabelle/provers/classical.ts +389 -0
  103. package/src/prover/isabelle/provers/hypsubst.ts +433 -0
  104. package/src/prover/isabelle/pure/bicompose.ts +174 -0
  105. package/src/prover/isabelle/pure/consts.ts +32 -0
  106. package/src/prover/isabelle/pure/conv.ts +79 -0
  107. package/src/prover/isabelle/pure/drule.ts +264 -0
  108. package/src/prover/isabelle/pure/envir.ts +331 -0
  109. package/src/prover/isabelle/pure/goal.ts +53 -0
  110. package/src/prover/isabelle/pure/interrupt.ts +16 -0
  111. package/src/prover/isabelle/pure/list.ts +12 -0
  112. package/src/prover/isabelle/pure/logic.ts +330 -0
  113. package/src/prover/isabelle/pure/name.ts +98 -0
  114. package/src/prover/isabelle/pure/net.ts +215 -0
  115. package/src/prover/isabelle/pure/object-logic.ts +41 -0
  116. package/src/prover/isabelle/pure/pattern.ts +631 -0
  117. package/src/prover/isabelle/pure/raw-simplifier.ts +338 -0
  118. package/src/prover/isabelle/pure/rewriter.ts +602 -0
  119. package/src/prover/isabelle/pure/search.ts +68 -0
  120. package/src/prover/isabelle/pure/seq.ts +133 -0
  121. package/src/prover/isabelle/pure/simplifier.ts +106 -0
  122. package/src/prover/isabelle/pure/table.ts +201 -0
  123. package/src/prover/isabelle/pure/tactic.ts +168 -0
  124. package/src/prover/isabelle/pure/tactical.ts +264 -0
  125. package/src/prover/isabelle/pure/term-ord.ts +105 -0
  126. package/src/prover/isabelle/pure/term.ts +688 -0
  127. package/src/prover/isabelle/pure/thm.ts +783 -0
  128. package/src/prover/isabelle/pure/type.ts +121 -0
  129. package/src/prover/isabelle/pure/unify.ts +753 -0
  130. package/src/prover/isabelle/pure/variable.ts +25 -0
  131. package/src/prover/isabelle/tla/classical-setup.ts +101 -0
  132. package/src/prover/isabelle/tla/context.ts +74 -0
  133. package/src/prover/isabelle/tla/logic-data.ts +5 -0
  134. package/src/prover/isabelle/tla/logic.ts +215 -0
  135. package/src/prover/isabelle/tla/simpdata.ts +241 -0
  136. package/src/prover/isabelle/tools/intuitionistic.ts +132 -0
  137. package/src/prover/ocaml.ts +22 -0
  138. package/src/prover/prep/action.ts +508 -0
  139. package/src/prover/prep/enabled-axioms.ts +189 -0
  140. package/src/prover/prep/fingerprint.ts +388 -0
  141. package/src/prover/prep/level-comparison.ts +529 -0
  142. package/src/prover/prep/md5.ts +60 -0
  143. package/src/prover/prep/methods.ts +221 -0
  144. package/src/prover/prep/normalize.ts +309 -0
  145. package/src/prover/prove-worker.ts +22 -0
  146. package/src/prover/prove.ts +492 -0
  147. package/src/prover/ptl/decide.ts +68 -0
  148. package/src/prover/ptl/encode.ts +244 -0
  149. package/src/prover/ptl/sat.ts +151 -0
  150. package/src/prover/ptl/tableau.ts +276 -0
  151. package/src/prover/smt/axioms.ts +209 -0
  152. package/src/prover/smt/context.ts +108 -0
  153. package/src/prover/smt/data.ts +636 -0
  154. package/src/prover/smt/encode.ts +275 -0
  155. package/src/prover/smt/extensionality.ts +65 -0
  156. package/src/prover/smt/flatten.ts +112 -0
  157. package/src/prover/smt/format.ts +154 -0
  158. package/src/prover/smt/instances.ts +181 -0
  159. package/src/prover/smt/rewrite.ts +766 -0
  160. package/src/prover/smt/solver/act-cache.ts +265 -0
  161. package/src/prover/smt/solver/arith-rewriter.ts +962 -0
  162. package/src/prover/smt/solver/array.ts +22 -0
  163. package/src/prover/smt/solver/asserted-formulas.ts +469 -0
  164. package/src/prover/smt/solver/ast-lt.ts +157 -0
  165. package/src/prover/smt/solver/ast-util.ts +91 -0
  166. package/src/prover/smt/solver/ast.ts +1315 -0
  167. package/src/prover/smt/solver/auf-solver.ts +572 -0
  168. package/src/prover/smt/solver/case-split-queue.ts +178 -0
  169. package/src/prover/smt/solver/cg-table.ts +175 -0
  170. package/src/prover/smt/solver/clause.ts +276 -0
  171. package/src/prover/smt/solver/conflict-resolution.ts +343 -0
  172. package/src/prover/smt/solver/context.ts +1292 -0
  173. package/src/prover/smt/solver/ctx-simplify.ts +334 -0
  174. package/src/prover/smt/solver/der.ts +224 -0
  175. package/src/prover/smt/solver/dyn-ack.ts +141 -0
  176. package/src/prover/smt/solver/e-matching.ts +659 -0
  177. package/src/prover/smt/solver/enode.ts +259 -0
  178. package/src/prover/smt/solver/expr-ref.ts +21 -0
  179. package/src/prover/smt/solver/expr-safe-replace.ts +172 -0
  180. package/src/prover/smt/solver/fingerprints.ts +92 -0
  181. package/src/prover/smt/solver/flat-literals.ts +51 -0
  182. package/src/prover/smt/solver/goal.ts +138 -0
  183. package/src/prover/smt/solver/hash.ts +173 -0
  184. package/src/prover/smt/solver/hashtable.ts +166 -0
  185. package/src/prover/smt/solver/internalizer.ts +1381 -0
  186. package/src/prover/smt/solver/lbool.ts +5 -0
  187. package/src/prover/smt/solver/literal.ts +24 -0
  188. package/src/prover/smt/solver/lp/bound-propagator.ts +391 -0
  189. package/src/prover/smt/solver/lp/constraints.ts +78 -0
  190. package/src/prover/smt/solver/lp/containers.ts +296 -0
  191. package/src/prover/smt/solver/lp/core-solver.ts +504 -0
  192. package/src/prover/smt/solver/lp/cross-nested.ts +224 -0
  193. package/src/prover/smt/solver/lp/dep-intervals.ts +642 -0
  194. package/src/prover/smt/solver/lp/dependency.ts +51 -0
  195. package/src/prover/smt/solver/lp/dioph-eq.ts +1181 -0
  196. package/src/prover/smt/solver/lp/explanation.ts +46 -0
  197. package/src/prover/smt/solver/lp/gomory.ts +255 -0
  198. package/src/prover/smt/solver/lp/hnf-cutter.ts +125 -0
  199. package/src/prover/smt/solver/lp/hnf.ts +281 -0
  200. package/src/prover/smt/solver/lp/int-cube.ts +295 -0
  201. package/src/prover/smt/solver/lp/int-solver.ts +514 -0
  202. package/src/prover/smt/solver/lp/lar-solver.ts +816 -0
  203. package/src/prover/smt/solver/lp/lar-term.ts +62 -0
  204. package/src/prover/smt/solver/lp/lp-settings.ts +91 -0
  205. package/src/prover/smt/solver/lp/maximize-term.ts +347 -0
  206. package/src/prover/smt/solver/lp/nex-creator.ts +381 -0
  207. package/src/prover/smt/solver/lp/nex.ts +120 -0
  208. package/src/prover/smt/solver/lp/nla-backoff.ts +43 -0
  209. package/src/prover/smt/solver/lp/nla-basics.ts +398 -0
  210. package/src/prover/smt/solver/lp/nla-core.ts +968 -0
  211. package/src/prover/smt/solver/lp/nla-divisions.ts +332 -0
  212. package/src/prover/smt/solver/lp/nla-emonics.ts +373 -0
  213. package/src/prover/smt/solver/lp/nla-factorization.ts +30 -0
  214. package/src/prover/smt/solver/lp/nla-grobner.ts +904 -0
  215. package/src/prover/smt/solver/lp/nla-horner.ts +126 -0
  216. package/src/prover/smt/solver/lp/nla-intervals.ts +265 -0
  217. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +715 -0
  218. package/src/prover/smt/solver/lp/nla-monotone.ts +59 -0
  219. package/src/prover/smt/solver/lp/nla-order.ts +307 -0
  220. package/src/prover/smt/solver/lp/nla-patcher.ts +112 -0
  221. package/src/prover/smt/solver/lp/nla-tangents.ts +251 -0
  222. package/src/prover/smt/solver/lp/nla-throttle.ts +85 -0
  223. package/src/prover/smt/solver/lp/nla-types.ts +124 -0
  224. package/src/prover/smt/solver/lp/nla-var-eqs.ts +141 -0
  225. package/src/prover/smt/solver/lp/nra-solver.ts +440 -0
  226. package/src/prover/smt/solver/lp/numeric-pair.ts +65 -0
  227. package/src/prover/smt/solver/lp/pdd-interval.ts +31 -0
  228. package/src/prover/smt/solver/lp/pdd-simplifier.ts +184 -0
  229. package/src/prover/smt/solver/lp/pdd-solver.ts +408 -0
  230. package/src/prover/smt/solver/lp/pdd.ts +971 -0
  231. package/src/prover/smt/solver/lp/random-updater.ts +138 -0
  232. package/src/prover/smt/solver/lp/static-matrix.ts +246 -0
  233. package/src/prover/smt/solver/lp/union-find.ts +96 -0
  234. package/src/prover/smt/solver/mam-compiler.ts +847 -0
  235. package/src/prover/smt/solver/mam-interpreter.ts +400 -0
  236. package/src/prover/smt/solver/maximize-ac-sharing.ts +118 -0
  237. package/src/prover/smt/solver/model-checker.ts +347 -0
  238. package/src/prover/smt/solver/model-evaluator.ts +262 -0
  239. package/src/prover/smt/solver/model-finder.ts +155 -0
  240. package/src/prover/smt/solver/model-generator.ts +97 -0
  241. package/src/prover/smt/solver/model-macro-solver.ts +728 -0
  242. package/src/prover/smt/solver/nlsat/algebraic.ts +1161 -0
  243. package/src/prover/smt/solver/nlsat/atoms.ts +238 -0
  244. package/src/prover/smt/solver/nlsat/evaluator.ts +303 -0
  245. package/src/prover/smt/solver/nlsat/explain.ts +607 -0
  246. package/src/prover/smt/solver/nlsat/interval-set.ts +351 -0
  247. package/src/prover/smt/solver/nlsat/levelwise.ts +737 -0
  248. package/src/prover/smt/solver/nlsat/mpbq.ts +161 -0
  249. package/src/prover/smt/solver/nlsat/polynomial.ts +1722 -0
  250. package/src/prover/smt/solver/nlsat/solver.ts +911 -0
  251. package/src/prover/smt/solver/nlsat/types.ts +43 -0
  252. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +645 -0
  253. package/src/prover/smt/solver/nlsat/upolynomial.ts +996 -0
  254. package/src/prover/smt/solver/nnf.ts +469 -0
  255. package/src/prover/smt/solver/pattern-inference.ts +351 -0
  256. package/src/prover/smt/solver/poly-rewriter.ts +477 -0
  257. package/src/prover/smt/solver/propagate-values.ts +83 -0
  258. package/src/prover/smt/solver/proto-model.ts +551 -0
  259. package/src/prover/smt/solver/pull-quant.ts +139 -0
  260. package/src/prover/smt/solver/qe-lite-der.ts +526 -0
  261. package/src/prover/smt/solver/qe-lite-fm.ts +576 -0
  262. package/src/prover/smt/solver/qe-lite.ts +381 -0
  263. package/src/prover/smt/solver/qi-queue.ts +362 -0
  264. package/src/prover/smt/solver/quant-tactics.ts +117 -0
  265. package/src/prover/smt/solver/quantifier-analyzer.ts +562 -0
  266. package/src/prover/smt/solver/quantifier-manager.ts +186 -0
  267. package/src/prover/smt/solver/random-gen.ts +10 -0
  268. package/src/prover/smt/solver/rational.ts +165 -0
  269. package/src/prover/smt/solver/ref-vector.ts +62 -0
  270. package/src/prover/smt/solver/relevancy.ts +247 -0
  271. package/src/prover/smt/solver/rewriter-tpl.ts +1159 -0
  272. package/src/prover/smt/solver/rewriter.ts +843 -0
  273. package/src/prover/smt/solver/rlimit.ts +79 -0
  274. package/src/prover/smt/solver/smtlib-parser.ts +526 -0
  275. package/src/prover/smt/solver/solver.ts +71 -0
  276. package/src/prover/smt/solver/sort.ts +35 -0
  277. package/src/prover/smt/solver/sticky-generations.ts +69 -0
  278. package/src/prover/smt/solver/theory-lra-axioms.ts +340 -0
  279. package/src/prover/smt/solver/theory-lra-bounds.ts +226 -0
  280. package/src/prover/smt/solver/theory-lra-final-check.ts +252 -0
  281. package/src/prover/smt/solver/theory-lra-internalize.ts +476 -0
  282. package/src/prover/smt/solver/theory-lra-model.ts +243 -0
  283. package/src/prover/smt/solver/theory-lra-propagate.ts +405 -0
  284. package/src/prover/smt/solver/theory-lra-state.ts +43 -0
  285. package/src/prover/smt/solver/theory-lra.ts +56 -0
  286. package/src/prover/smt/solver/var-subst.ts +258 -0
  287. package/src/prover/smt/solver/z3-license.txt +10 -0
  288. package/src/prover/smt/table.ts +170 -0
  289. package/src/prover/tlapm/cache.ts +236 -0
  290. package/src/prover/tlapm/ocaml-marshal.ts +166 -0
  291. package/src/prover/tlapm/tlapm.ts +96 -0
  292. package/src/prover/tlaps-module.ts +510 -0
  293. package/src/prover/verdict.ts +13 -0
  294. package/src/prover/zenon/LICENSE +32 -0
  295. package/src/prover/zenon/check/check.ts +105 -0
  296. package/src/prover/zenon/check/definitions.ts +144 -0
  297. package/src/prover/zenon/check/equality.ts +182 -0
  298. package/src/prover/zenon/check/functions.ts +62 -0
  299. package/src/prover/zenon/check/keys.ts +125 -0
  300. package/src/prover/zenon/check/propositional.ts +104 -0
  301. package/src/prover/zenon/check/records.ts +188 -0
  302. package/src/prover/zenon/check/relations.ts +121 -0
  303. package/src/prover/zenon/check/rewrites.ts +224 -0
  304. package/src/prover/zenon/check/sets.ts +302 -0
  305. package/src/prover/zenon/check/terms.ts +127 -0
  306. package/src/prover/zenon/check/tla.ts +126 -0
  307. package/src/prover/zenon/decide.ts +107 -0
  308. package/src/prover/zenon/encode.ts +378 -0
  309. package/src/prover/zenon/eqrel.ts +300 -0
  310. package/src/prover/zenon/equality-rules.ts +435 -0
  311. package/src/prover/zenon/expr.ts +551 -0
  312. package/src/prover/zenon/index-table.ts +350 -0
  313. package/src/prover/zenon/isar-case.ts +513 -0
  314. package/src/prover/zenon/isar.ts +1249 -0
  315. package/src/prover/zenon/limits.ts +15 -0
  316. package/src/prover/zenon/llproof.ts +173 -0
  317. package/src/prover/zenon/mltoll.ts +1566 -0
  318. package/src/prover/zenon/node.ts +297 -0
  319. package/src/prover/zenon/ocaml.ts +169 -0
  320. package/src/prover/zenon/parse.ts +247 -0
  321. package/src/prover/zenon/phrase.ts +191 -0
  322. package/src/prover/zenon/proof.ts +198 -0
  323. package/src/prover/zenon/propositional-rules.ts +387 -0
  324. package/src/prover/zenon/rules.ts +94 -0
  325. package/src/prover/zenon/search.ts +288 -0
  326. package/src/prover/zenon/tla/functions.ts +151 -0
  327. package/src/prover/zenon/tla/llproof.ts +480 -0
  328. package/src/prover/zenon/tla/records.ts +225 -0
  329. package/src/prover/zenon/tla/rewrites.ts +447 -0
  330. package/src/prover/zenon/tla/sets.ts +304 -0
  331. package/src/prover/zenon/tla/terms.ts +162 -0
  332. package/src/prover/zenon/tla/tla.ts +506 -0
  333. package/src/randomization.ts +19 -24
  334. package/src/resolve.ts +15 -22
  335. package/src/runtime.ts +8 -8
  336. package/src/{sany-builtins.ts → sany/builtins.ts} +1 -1
  337. package/src/{sany-generator.ts → sany/generator.ts} +38 -55
  338. package/src/{sany-levels.ts → sany/levels.ts} +45 -48
  339. package/src/{sany-lexer.ts → sany/lexer.ts} +2 -3
  340. package/src/{sany-linter.ts → sany/linter.ts} +1 -1
  341. package/src/{sany-modules.ts → sany/modules.ts} +2 -2
  342. package/src/{sany-nodes.ts → sany/nodes.ts} +2 -2
  343. package/src/{sany-parse-error.ts → sany/parse-error.ts} +2 -2
  344. package/src/{sany-report.ts → sany/report.ts} +16 -17
  345. package/src/{sany-selectors.ts → sany/selectors.ts} +3 -5
  346. package/src/{sany-source.ts → sany/source.ts} +6 -7
  347. package/src/{sany-xml.ts → sany/xml.ts} +5 -6
  348. package/src/search.ts +969 -0
  349. package/src/spec-description.ts +26 -20
  350. package/src/{standard-levels.ts → standard-definitions.ts} +118 -2
  351. package/src/standard-modules.ts +21 -22
  352. package/src/state-graph.ts +9 -8
  353. package/src/substitution-levels.ts +2 -2
  354. package/src/successors.ts +39 -23
  355. package/src/symmetry.ts +1 -1
  356. package/src/tableau.ts +6 -7
  357. package/src/tlc/fingerprint.ts +214 -0
  358. package/src/{tlc-messages.ts → tlc/messages.ts} +119 -7
  359. package/src/{tlc-order.ts → tlc/order.ts} +7 -7
  360. package/src/{tlc-random.ts → tlc/random.ts} +3 -190
  361. package/src/{tlc-replay.ts → tlc/replay.ts} +40 -36
  362. package/src/{tlc-module.ts → tlc/tlc-operators.ts} +9 -9
  363. package/src/{tlc-ext.ts → tlc/tlcext-operators.ts} +30 -25
  364. package/src/{tlc-version.ts → tlc/version.ts} +8 -7
  365. package/src/transfer.ts +32 -32
  366. package/src/values.ts +166 -111
  367. package/src/worker.ts +62 -0
  368. package/dist/parallel.cjs +0 -12
  369. package/dist/parallel.js +0 -12
  370. package/dist/run-cli.cjs +0 -55
  371. package/dist/run-cli.js +0 -55
  372. package/src/canonical.ts +0 -40
  373. package/src/obligation-expression.ts +0 -1060
  374. package/src/obligation-module.ts +0 -1042
  375. package/src/standard-ranges.ts +0 -116
  376. package/src/value-sources.ts +0 -29
  377. /package/src/{tlc-modules.ts → tlc/jar-data.ts} +0 -0
  378. /package/src/{tlc-summary.ts → tlc/summary.ts} +0 -0
@@ -0,0 +1,618 @@
1
+ // tlapm's expression IR (E_t) with de Bruijn indices, and the visitors that map over it (Visit).
2
+
3
+ import type { OldMethod, PragmaSetting } from "./pragma";
4
+
5
+ export interface Locus {
6
+ file: string;
7
+ startLine: number;
8
+ startColumn: number;
9
+ endLine: number;
10
+ endColumn: number;
11
+ }
12
+
13
+ interface ObligationWarning {
14
+ text: string;
15
+ channel: "toolbox" | "toolboxOnly" | "stderr" | "stdout";
16
+ }
17
+
18
+ export type Warn = (warning: ObligationWarning) => void;
19
+
20
+ export const getLocusPosition = (locus: Locus) => {
21
+ const end = locus.endColumn - 1;
22
+ if (locus.startLine !== locus.endLine)
23
+ return `line ${locus.startLine}, character ${locus.startColumn} to line ${locus.endLine}, character ${end}`;
24
+ return locus.startColumn >= end
25
+ ? `line ${locus.startLine}, character ${locus.startColumn}`
26
+ : `line ${locus.startLine}, characters ${locus.startColumn}-${end}`;
27
+ };
28
+
29
+ export const getLocusText = (locus: Locus) =>
30
+ `File ${JSON.stringify(locus.file)}, ${getLocusPosition(locus)}`;
31
+
32
+ export const getLocationText = (locus: Locus | undefined) =>
33
+ locus ? getLocusText(locus) : "<unknown location>";
34
+
35
+ export const getAtPrefix = (locus: Locus | undefined) => `${getLocationText(locus)}:\n`;
36
+
37
+ export const getErrorNote = (locus: Locus | undefined, message: string): ObligationWarning => ({
38
+ text: `${getAtPrefix(locus)}Error: ${message}`,
39
+ channel: "stderr",
40
+ });
41
+
42
+ export const getLocusPrefix = (locus: Locus | undefined) =>
43
+ locus ? `${getLocusText(locus)} :\n` : "";
44
+
45
+ export type Builtin =
46
+ | "TRUE"
47
+ | "FALSE"
48
+ | "Implies"
49
+ | "Equiv"
50
+ | "Conj"
51
+ | "Disj"
52
+ | "Neg"
53
+ | "Eq"
54
+ | "Neq"
55
+ | "STRING"
56
+ | "BOOLEAN"
57
+ | "SUBSET"
58
+ | "UNION"
59
+ | "DOMAIN"
60
+ | "Subseteq"
61
+ | "Mem"
62
+ | "Notmem"
63
+ | "Setminus"
64
+ | "Cap"
65
+ | "Cup"
66
+ | "Prime"
67
+ | "StrongPrime"
68
+ | "Leadsto"
69
+ | "ENABLED"
70
+ | "UNCHANGED"
71
+ | "Cdot"
72
+ | "Actplus"
73
+ | "BoxTrue"
74
+ | "BoxFalse"
75
+ | "Diamond"
76
+ | "Nat"
77
+ | "Int"
78
+ | "Real"
79
+ | "Plus"
80
+ | "Minus"
81
+ | "Uminus"
82
+ | "Times"
83
+ | "Ratio"
84
+ | "Quotient"
85
+ | "Remainder"
86
+ | "Exp"
87
+ | "Infinity"
88
+ | "Lteq"
89
+ | "Lt"
90
+ | "Gteq"
91
+ | "Gt"
92
+ | "Divides"
93
+ | "Range"
94
+ | "Seq"
95
+ | "Len"
96
+ | "BSeq"
97
+ | "Cat"
98
+ | "Append"
99
+ | "Head"
100
+ | "Tail"
101
+ | "SubSeq"
102
+ | "SelectSeq"
103
+ | "OneArg"
104
+ | "Extend"
105
+ | "Print"
106
+ | "PrintT"
107
+ | "Assert"
108
+ | "JavaTime"
109
+ | "TLCGet"
110
+ | "TLCSet"
111
+ | "Permutations"
112
+ | "SortSeq"
113
+ | "RandomElement"
114
+ | "Any"
115
+ | "ToString"
116
+ | "Unprimable"
117
+ | "Irregular";
118
+
119
+ export type Shape = number;
120
+
121
+ export type Level = "Constant" | "State" | "Action" | "Temporal" | "Unknown";
122
+
123
+ export type BoundDomain =
124
+ | { kind: "NoDomain" }
125
+ | { kind: "Domain"; expression: Expr }
126
+ | { kind: "Ditto" };
127
+
128
+ export interface Bound {
129
+ name: string;
130
+ level: Level;
131
+ domain: BoundDomain;
132
+ }
133
+
134
+ export type Selector =
135
+ | { kind: "Down" }
136
+ | { kind: "Number"; value: number }
137
+ | { kind: "Left" }
138
+ | { kind: "Right" }
139
+ | { kind: "At" }
140
+ | { kind: "Instance"; args: Expr[] }
141
+ | { kind: "Label"; label: string; args: Expr[] };
142
+
143
+ export type ExceptPoint = { kind: "Dot"; field: string } | { kind: "Apply"; expression: Expr };
144
+
145
+ interface ExceptSpec {
146
+ trail: ExceptPoint[];
147
+ value: Expr;
148
+ }
149
+
150
+ export type ParenForm =
151
+ | { kind: "Syntax" }
152
+ | { kind: "Nlabel"; label: string; names: string[] }
153
+ | { kind: "Xlabel"; label: string; indices: { name: string; index: number }[] };
154
+
155
+ export interface Param {
156
+ name: string;
157
+ shape: Shape;
158
+ location?: Locus;
159
+ }
160
+
161
+ // isAtomic is Expr.Deref.atomic: a resolved subexpression reference, which has no subexpressions.
162
+ interface ExprProps {
163
+ location?: Locus;
164
+ isAtomic?: boolean;
165
+ }
166
+
167
+ export type Expr = ExprCore & ExprProps;
168
+
169
+ export type ExprCore =
170
+ | { kind: "Ix"; index: number }
171
+ | { kind: "Opaque"; name: string }
172
+ | { kind: "Internal"; builtin: Builtin }
173
+ | { kind: "Lambda"; params: Param[]; body: Expr }
174
+ | { kind: "Sequent"; sequent: Sequent }
175
+ | { kind: "Bang"; expression: Expr; selectors: Selector[] }
176
+ | { kind: "Apply"; operator: Expr; args: Expr[] }
177
+ | { kind: "With"; expression: Expr; method: OldMethod }
178
+ | { kind: "If"; condition: Expr; consequent: Expr; alternative: Expr }
179
+ | { kind: "List"; bullet: "And" | "Or" | "Refs"; items: Expr[] }
180
+ | { kind: "Let"; definitions: Defn[]; body: Expr }
181
+ | { kind: "Quant"; quantifier: "Forall" | "Exists"; bounds: Bound[]; body: Expr }
182
+ | { kind: "Tquant"; quantifier: "Forall" | "Exists"; names: string[]; body: Expr }
183
+ | { kind: "Choose"; name: string; domain: Expr | undefined; body: Expr }
184
+ | { kind: "SetSt"; name: string; domain: Expr; body: Expr }
185
+ | { kind: "SetOf"; element: Expr; bounds: Bound[] }
186
+ | { kind: "SetEnum"; elements: Expr[] }
187
+ | { kind: "Product"; factors: Expr[] }
188
+ | { kind: "Tuple"; elements: Expr[] }
189
+ | { kind: "Fcn"; bounds: Bound[]; body: Expr }
190
+ | { kind: "FcnApp"; function: Expr; args: Expr[] }
191
+ | { kind: "Arrow"; domain: Expr; range: Expr }
192
+ | { kind: "Rect"; fields: Field[] }
193
+ | { kind: "Record"; fields: Field[] }
194
+ | { kind: "Except"; function: Expr; specs: ExceptSpec[] }
195
+ | { kind: "Dot"; expression: Expr; field: string }
196
+ | { kind: "Sub"; modal: "Box" | "Dia"; action: Expr; subscript: Expr }
197
+ | { kind: "Tsub"; modal: "Box" | "Dia"; action: Expr; subscript: Expr }
198
+ | { kind: "Fair"; fairness: "Weak" | "Strong"; subscript: Expr; action: Expr }
199
+ | { kind: "Case"; arms: { condition: Expr; body: Expr }[]; other: Expr | undefined }
200
+ | { kind: "String"; value: string }
201
+ | { kind: "Num"; integer: string; fraction: string }
202
+ | { kind: "At"; isExcept: boolean }
203
+ | { kind: "Parens"; expression: Expr; form: ParenForm };
204
+
205
+ export interface Field {
206
+ name: string;
207
+ value: Expr;
208
+ }
209
+
210
+ export interface InstanceDefinition {
211
+ args: string[];
212
+ module: string;
213
+ substitutions: { name: string; expression: Expr }[];
214
+ location?: Locus;
215
+ }
216
+
217
+ export type Defn = DefnCore & { location?: Locus };
218
+
219
+ type DefnCore =
220
+ | { kind: "Recursive"; name: string; shape: Shape }
221
+ | { kind: "Operator"; name: string; body: Expr }
222
+ | { kind: "Instance"; name: string; instance: InstanceDefinition }
223
+ | { kind: "Bpragma"; name: string; body: Expr; methods: PragmaSetting[][] };
224
+
225
+ export type Visibility = "Visible" | "Hidden";
226
+
227
+ // When a fact holds: at every point of a behavior, or only at the current one.
228
+ export type Time = "Now" | "Always";
229
+
230
+ // A step or a DEFINE in a proof keeps the time of the scope it is made in.
231
+ export type WhereDefined = "Builtin" | "User" | { kind: "Proof"; time: Time };
232
+
233
+ export type Hyp = HypCore & { location?: Locus };
234
+
235
+ type HypCore =
236
+ | {
237
+ kind: "Fresh";
238
+ name: string;
239
+ shape: Shape;
240
+ level: Level;
241
+ domain: Expr | undefined;
242
+ domainVisibility: Visibility;
243
+ }
244
+ | { kind: "Flex"; name: string }
245
+ | {
246
+ kind: "Defn";
247
+ definition: Defn;
248
+ whereDefined: WhereDefined;
249
+ visibility: Visibility;
250
+ isLocal: boolean;
251
+ // Module.Elab judges whether each module definition is constant. One that anonymization meets
252
+ // in a proof, such as a step or a DEFINE, has no judgment, so tlapm never counts it constant.
253
+ isConstnessUnset?: boolean;
254
+ }
255
+ | { kind: "Fact"; expression: Expr; visibility: Visibility; time: Time; useLocation?: Locus };
256
+
257
+ export interface Sequent {
258
+ context: Hyp[];
259
+ active: Expr;
260
+ }
261
+
262
+ // tlapm's core @@ from, which takes the properties of from.
263
+ export const at = (core: ExprCore, from: ExprProps): Expr => {
264
+ const located = from.location ? { ...core, location: from.location } : core;
265
+ return from.isAtomic ? { ...located, isAtomic: true } : located;
266
+ };
267
+
268
+ export const getHypName = (hyp: Hyp) => {
269
+ switch (hyp.kind) {
270
+ case "Fresh":
271
+ case "Flex":
272
+ return hyp.name;
273
+ case "Defn":
274
+ return hyp.definition.name;
275
+ case "Fact":
276
+ return undefined;
277
+ }
278
+ };
279
+
280
+ export const isBuiltinApply = (
281
+ expression: Expr,
282
+ builtin: Builtin,
283
+ ): expression is Extract<Expr, { kind: "Apply" }> =>
284
+ expression.kind === "Apply" &&
285
+ expression.operator.kind === "Internal" &&
286
+ expression.operator.builtin === builtin;
287
+
288
+ export const applyBuiltin = (builtin: Builtin, args: Expr[], from: ExprProps): Expr =>
289
+ at({ kind: "Apply", operator: at({ kind: "Internal", builtin }, from), args }, from);
290
+
291
+ export const exprifySequent = (sequent: Sequent): ExprCore =>
292
+ sequent.context.length === 0 ? sequent.active : { kind: "Sequent", sequent };
293
+
294
+ // A persistent context, newest hypothesis first, so binders extend it without copying.
295
+ export interface Scope {
296
+ hyp: Hyp;
297
+ rest: Scope | undefined;
298
+ size: number;
299
+ }
300
+
301
+ export const adjoin = (scope: Scope | undefined, hyp: Hyp): Scope => ({
302
+ hyp,
303
+ rest: scope,
304
+ size: (scope?.size ?? 0) + 1,
305
+ });
306
+
307
+ export const adjoinAll = (scope: Scope | undefined, hyps: Hyp[]) =>
308
+ hyps.reduce<Scope | undefined>(adjoin, scope);
309
+
310
+ export const getScopeHyps = (scope: Scope | undefined): Hyp[] => {
311
+ const hyps: Hyp[] = [];
312
+ for (let current = scope; current; current = current.rest) hyps.push(current.hyp);
313
+ return hyps.reverse();
314
+ };
315
+
316
+ export const findNamed = (scope: Scope | undefined, name: string) => {
317
+ let depth = 0;
318
+ for (let current = scope; current; current = current.rest) {
319
+ if (getHypName(current.hyp) === name) return { depth, hyp: current.hyp };
320
+ depth++;
321
+ }
322
+ return undefined;
323
+ };
324
+
325
+ export const getScopeAt = (scope: Scope | undefined, depth: number) => {
326
+ let current = scope;
327
+ for (let index = 0; index < depth && current; index++) current = current.rest;
328
+ return current;
329
+ };
330
+
331
+ export const mapAccumulate = <State, Item, Result>(
332
+ state: State,
333
+ items: Item[],
334
+ step: (state: State, item: Item) => [State, Result],
335
+ ): [State, Result[]] => {
336
+ const results: Result[] = [];
337
+ const final = items.reduce((current, item) => {
338
+ const [next, result] = step(current, item);
339
+ results.push(result);
340
+ return next;
341
+ }, state);
342
+ return [final, results];
343
+ };
344
+
345
+ // A mapped context prefix: the hypothesis its last key maps to, if any, and the state after it.
346
+ interface MappedPrefix<State> {
347
+ state: State;
348
+ hyp: Hyp | undefined;
349
+ parent: MappedPrefix<State> | undefined;
350
+ next: WeakMap<Hyp, MappedPrefix<State>>;
351
+ }
352
+
353
+ // mapAccumulate over a context that maps each prefix of keys once, as the obligations of a module
354
+ // share most of their context by identity. The input at each position must depend on the keys up
355
+ // to it alone, and the step on the input, its key, and the state.
356
+ export const createContextMap = <State>(
357
+ initial: State,
358
+ step: (state: State, input: Hyp, key: Hyp) => [State, Hyp | undefined],
359
+ ) => {
360
+ const empty: MappedPrefix<State> = {
361
+ state: initial,
362
+ hyp: undefined,
363
+ parent: undefined,
364
+ next: new WeakMap(),
365
+ };
366
+ return (keys: Hyp[], inputs = keys) => {
367
+ const last = keys.reduce((prefix, key, index) => {
368
+ const known = prefix.next.get(key);
369
+ if (known) return known;
370
+ const [state, hyp] = step(prefix.state, inputs[index], key);
371
+ const mapped: MappedPrefix<State> = { state, hyp, parent: prefix, next: new WeakMap() };
372
+ prefix.next.set(key, mapped);
373
+ return mapped;
374
+ }, empty);
375
+ const context: Hyp[] = [];
376
+ for (let prefix: MappedPrefix<State> | undefined = last; prefix; prefix = prefix.parent) {
377
+ if (prefix.hyp) context.push(prefix.hyp);
378
+ }
379
+ return { state: last.state, context: context.reverse() };
380
+ };
381
+ };
382
+
383
+ export const getFresh = (name: string, level: Level, shape = 0): Hyp => ({
384
+ kind: "Fresh",
385
+ name,
386
+ shape,
387
+ level,
388
+ domain: undefined,
389
+ domainVisibility: "Visible",
390
+ });
391
+
392
+ export const getBoundedFresh = (name: string, domain: Expr): Hyp => ({
393
+ kind: "Fresh",
394
+ name,
395
+ shape: 0,
396
+ level: "Constant",
397
+ domain,
398
+ domainVisibility: "Visible",
399
+ });
400
+
401
+ export const getFact = (
402
+ expression: Expr,
403
+ visibility: Visibility,
404
+ time: Time,
405
+ location: Locus | undefined,
406
+ ) => ({ kind: "Fact", expression, visibility, time, location }) satisfies Hyp;
407
+
408
+ // What a quantifier binds each name as, a LAMBDA each parameter, and a LET each definition, as
409
+ // Visit.map does.
410
+ export const getBoundFresh = (bound: Bound) => getFresh(bound.name, bound.level);
411
+
412
+ export const getParamFresh = (param: Param) => getFresh(param.name, "Unknown", param.shape);
413
+
414
+ export const getLocalDefn = (definition: Defn) =>
415
+ ({
416
+ kind: "Defn",
417
+ definition,
418
+ whereDefined: "User",
419
+ visibility: "Visible",
420
+ isLocal: true,
421
+ }) satisfies Hyp;
422
+
423
+ // The node without its properties, as tlapm's e.core.
424
+ export const stripProps = (expression: Expr): ExprCore => {
425
+ if (!expression.location && !expression.isAtomic) return expression;
426
+ const { location: _location, isAtomic: _isAtomic, ...core } = expression;
427
+ return core;
428
+ };
429
+
430
+ export const mapBoundDomains = (bounds: Bound[], map: (domain: Expr) => Expr): Bound[] =>
431
+ bounds.map((bound) =>
432
+ bound.domain.kind === "Domain"
433
+ ? { ...bound, domain: { kind: "Domain", expression: map(bound.domain.expression) } }
434
+ : bound,
435
+ );
436
+
437
+ export const mapSelectorArgs = (selector: Selector, map: (arg: Expr) => Expr): Selector =>
438
+ selector.kind === "Instance" || selector.kind === "Label"
439
+ ? { ...selector, args: selector.args.map(map) }
440
+ : selector;
441
+
442
+ export const mapExceptSpecs = (specs: ExceptSpec[], map: (inner: Expr) => Expr): ExceptSpec[] =>
443
+ specs.map((spec) => {
444
+ const value = map(spec.value);
445
+ const trail = spec.trail.map((point): ExceptPoint =>
446
+ point.kind === "Apply" ? { kind: "Apply", expression: map(point.expression) } : point,
447
+ );
448
+ return { trail, value };
449
+ });
450
+
451
+ export const mapDefnBody = (definition: Defn, map: (body: Expr) => Expr): Defn =>
452
+ definition.kind === "Operator" || definition.kind === "Bpragma"
453
+ ? { ...definition, body: map(definition.body) }
454
+ : definition;
455
+
456
+ export const mapHyp = (hyp: Hyp, map: (inner: Expr) => Expr, hyps: "visible" | "all"): Hyp => {
457
+ if (hyp.kind === "Fresh") return hyp.domain ? { ...hyp, domain: map(hyp.domain) } : hyp;
458
+ if (hyp.kind === "Flex" || (hyps === "visible" && hyp.visibility === "Hidden")) return hyp;
459
+ if (hyp.kind === "Defn") return { ...hyp, definition: mapDefnBody(hyp.definition, map) };
460
+ return { ...hyp, expression: map(hyp.expression) };
461
+ };
462
+
463
+ // Visit.map, threading a state through each binder: a scope or a substitution. Visit.map_visible_hyp
464
+ // binds hidden hypotheses without visiting them, and Visit.map visits them too. Children map in
465
+ // OCaml evaluation order, last constructor argument first, which decides the error tlapm reports.
466
+ export const mapScoped = <State>(
467
+ state: State,
468
+ expression: Expr,
469
+ map: (state: State, inner: Expr) => Expr,
470
+ bind: (state: State, hyp: Hyp) => State,
471
+ hyps: "visible" | "all" = "visible",
472
+ ): Expr => {
473
+ const recurse = (inner: Expr) => map(state, inner);
474
+ const bindBounds = (bounds: Bound[]) =>
475
+ bounds.reduce((current, bound) => bind(current, getBoundFresh(bound)), state);
476
+ switch (expression.kind) {
477
+ case "Ix":
478
+ case "Internal":
479
+ case "Opaque":
480
+ case "String":
481
+ case "Num":
482
+ case "At":
483
+ return expression;
484
+ case "Lambda": {
485
+ const inner = expression.params.reduce((current, param) => {
486
+ const fresh = getParamFresh(param);
487
+ return bind(current, param.location ? { ...fresh, location: param.location } : fresh);
488
+ }, state);
489
+ return { ...expression, body: map(inner, expression.body) };
490
+ }
491
+ case "Sequent": {
492
+ const [inner, context] = mapAccumulate(state, expression.sequent.context, (current, hyp) => {
493
+ const mapped = mapHyp(hyp, (child) => map(current, child), hyps);
494
+ return [bind(current, mapped), mapped];
495
+ });
496
+ return { ...expression, sequent: { context, active: map(inner, expression.sequent.active) } };
497
+ }
498
+ case "Let": {
499
+ const [inner, definitions] = mapAccumulate(
500
+ state,
501
+ expression.definitions,
502
+ (current, definition) => {
503
+ const mapped = mapDefnBody(definition, (body) => map(current, body));
504
+ return [bind(current, getLocalDefn(mapped)), mapped];
505
+ },
506
+ );
507
+ return { ...expression, definitions, body: map(inner, expression.body) };
508
+ }
509
+ case "Quant":
510
+ case "Fcn": {
511
+ const bounds = mapBoundDomains(expression.bounds, recurse);
512
+ return { ...expression, bounds, body: map(bindBounds(bounds), expression.body) };
513
+ }
514
+ case "SetOf": {
515
+ const bounds = mapBoundDomains(expression.bounds, recurse);
516
+ return { ...expression, bounds, element: map(bindBounds(bounds), expression.element) };
517
+ }
518
+ case "Tquant": {
519
+ const inner = expression.names.reduce(
520
+ (current, name) => bind(current, { kind: "Flex", name }),
521
+ state,
522
+ );
523
+ return { ...expression, body: map(inner, expression.body) };
524
+ }
525
+ case "Choose": {
526
+ const domain = expression.domain && recurse(expression.domain);
527
+ const hyp = domain
528
+ ? getBoundedFresh(expression.name, domain)
529
+ : getFresh(expression.name, "Constant");
530
+ return { ...expression, domain, body: map(bind(state, hyp), expression.body) };
531
+ }
532
+ case "SetSt": {
533
+ const domain = recurse(expression.domain);
534
+ const hyp = getBoundedFresh(expression.name, domain);
535
+ return { ...expression, domain, body: map(bind(state, hyp), expression.body) };
536
+ }
537
+ case "Bang": {
538
+ const selectors = expression.selectors.map((selector) => mapSelectorArgs(selector, recurse));
539
+ return { ...expression, expression: recurse(expression.expression), selectors };
540
+ }
541
+ case "Apply": {
542
+ const args = expression.args.map(recurse);
543
+ return { ...expression, operator: recurse(expression.operator), args };
544
+ }
545
+ case "With":
546
+ case "Dot":
547
+ case "Parens":
548
+ return { ...expression, expression: recurse(expression.expression) };
549
+ case "If": {
550
+ const alternative = recurse(expression.alternative);
551
+ const consequent = recurse(expression.consequent);
552
+ return { ...expression, condition: recurse(expression.condition), consequent, alternative };
553
+ }
554
+ case "List":
555
+ return { ...expression, items: expression.items.map(recurse) };
556
+ case "SetEnum":
557
+ case "Tuple":
558
+ return { ...expression, elements: expression.elements.map(recurse) };
559
+ case "Product":
560
+ return { ...expression, factors: expression.factors.map(recurse) };
561
+ case "FcnApp": {
562
+ const args = expression.args.map(recurse);
563
+ return { ...expression, function: recurse(expression.function), args };
564
+ }
565
+ case "Arrow": {
566
+ const range = recurse(expression.range);
567
+ return { ...expression, domain: recurse(expression.domain), range };
568
+ }
569
+ case "Rect":
570
+ case "Record":
571
+ return {
572
+ ...expression,
573
+ fields: expression.fields.map((field) => ({
574
+ name: field.name,
575
+ value: recurse(field.value),
576
+ })),
577
+ };
578
+ case "Except": {
579
+ const specs = mapExceptSpecs(expression.specs, recurse);
580
+ return { ...expression, function: recurse(expression.function), specs };
581
+ }
582
+ case "Sub":
583
+ case "Tsub":
584
+ case "Fair":
585
+ return {
586
+ ...expression,
587
+ subscript: recurse(expression.subscript),
588
+ action: recurse(expression.action),
589
+ };
590
+ case "Case": {
591
+ const other = expression.other && recurse(expression.other);
592
+ const arms = expression.arms.map((arm) => {
593
+ const body = recurse(arm.body);
594
+ return { condition: recurse(arm.condition), body };
595
+ });
596
+ return { ...expression, arms, other };
597
+ }
598
+ }
599
+ };
600
+
601
+ // Visit.map without a state: every child, the hidden hypotheses of an ASSUME included.
602
+ export const mapChildren = (expression: Expr, map: (inner: Expr) => Expr): Expr =>
603
+ mapScoped(
604
+ undefined,
605
+ expression,
606
+ (_state, inner) => map(inner),
607
+ () => undefined,
608
+ "all",
609
+ );
610
+
611
+ export const getChildren = (expression: Expr) => {
612
+ const children: Expr[] = [];
613
+ mapChildren(expression, (inner) => {
614
+ children.push(inner);
615
+ return inner;
616
+ });
617
+ return children;
618
+ };
@@ -60,7 +60,7 @@ const getSentinel = (): ScanElement => ({
60
60
  element: { size: UNKNOWN, token: { kind: "Text", text: "" }, length: 0 },
61
61
  });
62
62
 
63
- const createFormatter = (): Formatter => {
63
+ const createFormatter = (maxIndent: number): Formatter => {
64
64
  const systemBox: QueueElement = {
65
65
  size: UNKNOWN,
66
66
  token: { kind: "Begin", indent: 0, box: "hov" },
@@ -72,7 +72,7 @@ const createFormatter = (): Formatter => {
72
72
  scanStack: [getSentinel(), { leftTotal: 1, element: systemBox }],
73
73
  formatStack: [],
74
74
  margin: 78,
75
- maxIndent: 68,
75
+ maxIndent,
76
76
  spaceLeft: 78,
77
77
  currentIndent: 0,
78
78
  isNewLine: true,
@@ -242,7 +242,7 @@ const flush = (formatter: Formatter) => {
242
242
  const flushNewline = (formatter: Formatter) => {
243
243
  flush(formatter);
244
244
  const output = [...formatter.output, "\n"];
245
- Object.assign(formatter, createFormatter());
245
+ Object.assign(formatter, createFormatter(formatter.maxIndent));
246
246
  formatter.output = output;
247
247
  };
248
248
 
@@ -258,8 +258,6 @@ type Command =
258
258
 
259
259
  export type Doc = (Command | Doc)[];
260
260
 
261
- type FormatArgument = string | Doc;
262
-
263
261
  const BOX_KINDS: Record<string, BoxKind> = { h: "h", v: "v", hv: "hv", hov: "hov", b: "b" };
264
262
 
265
263
  const getText = (value: string): Command => ({ kind: "Text", text: value, size: getLength(value) });
@@ -282,7 +280,7 @@ const parseOpen = (directive: string): Command => {
282
280
 
283
281
  // Interprets the subset of OCaml format strings that tlapm's printers use, so ported printers read
284
282
  // like the OCaml they come from. Each %s or %a consumes one argument.
285
- export const format = (pattern: string, ...args: FormatArgument[]): Doc => {
283
+ export const format = (pattern: string, ...args: (string | Doc)[]): Doc => {
286
284
  const doc: Doc = [];
287
285
  let argumentIndex = 0;
288
286
  let pending = "";
@@ -399,8 +397,9 @@ const renderInto = (formatter: Formatter, doc: Doc) => {
399
397
  }
400
398
  };
401
399
 
402
- export const renderDoc = (doc: Doc) => {
403
- const formatter = createFormatter();
400
+ // 68 is Format's default maximum indent; tlapm's stderr formatter allows 35.
401
+ export const renderDoc = (doc: Doc, maxIndent = 68) => {
402
+ const formatter = createFormatter(maxIndent);
404
403
  renderInto(formatter, doc);
405
404
  return flush(formatter);
406
405
  };