tla-ts 0.0.9 → 0.0.10-dev.63a6693bd

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (400) hide show
  1. package/README.md +29 -1
  2. package/dist/compile.cjs +23 -0
  3. package/dist/compile.js +23 -0
  4. package/dist/deciders.cjs +29 -29
  5. package/dist/deciders.js +42 -42
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +106 -10
  8. package/dist/index.d.ts +106 -10
  9. package/dist/index.js +1 -1
  10. package/dist/parallel-worker.cjs +1 -1
  11. package/dist/parallel-worker.js +1 -1
  12. package/dist/prove-worker.cjs +1 -1
  13. package/dist/prove-worker.js +1 -1
  14. package/dist/prove.cjs +117 -37
  15. package/dist/prove.js +116 -36
  16. package/dist/run.cjs +143 -31
  17. package/dist/run.js +143 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +2 -2
  20. package/dist/transfer.cjs +129 -104
  21. package/dist/transfer.js +129 -104
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +3 -1
  25. package/src/ast.ts +48 -62
  26. package/src/bags.ts +99 -102
  27. package/src/builtins.ts +28 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check-failure.ts +116 -0
  30. package/src/check-trace.ts +282 -0
  31. package/src/check.ts +599 -754
  32. package/src/cli/options.ts +12 -32
  33. package/src/cli/run.ts +0 -2
  34. package/src/cli/runtime-invariants.ts +136 -0
  35. package/src/cli/shared.ts +19 -12
  36. package/src/cli/tlapm.ts +14 -25
  37. package/src/cli/tlc-arguments.ts +652 -0
  38. package/src/cli/tlc-output.ts +687 -0
  39. package/src/cli/tlc-usage.ts +361 -0
  40. package/src/cli/tlc.ts +309 -658
  41. package/src/cli/typescript.ts +23 -0
  42. package/src/cli/xml-exporter.ts +27 -49
  43. package/src/cli.ts +0 -1
  44. package/src/codegen.ts +196 -74
  45. package/src/config.ts +140 -109
  46. package/src/coverage.ts +830 -0
  47. package/src/dependencies.ts +14 -53
  48. package/src/dfid.ts +618 -0
  49. package/src/environment-config.ts +315 -0
  50. package/src/environment.ts +169 -335
  51. package/src/errors.ts +71 -55
  52. package/src/evaluate.ts +598 -340
  53. package/src/exit-codes.ts +37 -3
  54. package/src/fingerprints.ts +75 -11
  55. package/src/index.ts +3 -0
  56. package/src/instances.ts +126 -21
  57. package/src/java-hash.ts +7 -0
  58. package/src/java-overrides.ts +4 -19
  59. package/src/json-reader.ts +455 -0
  60. package/src/json.ts +252 -139
  61. package/src/levels.ts +46 -58
  62. package/src/lexer.ts +80 -100
  63. package/src/liveness-checker.ts +103 -153
  64. package/src/liveness.ts +150 -109
  65. package/src/obligations/action.ts +8 -39
  66. package/src/obligations/anonymize.ts +88 -82
  67. package/src/obligations/equality.ts +1 -1
  68. package/src/obligations/expression.ts +20 -21
  69. package/src/obligations/format.ts +1 -9
  70. package/src/obligations/generate.ts +46 -49
  71. package/src/obligations/index.ts +2 -13
  72. package/src/obligations/levels.ts +4 -8
  73. package/src/obligations/module.ts +28 -53
  74. package/src/obligations/normalize.ts +7 -25
  75. package/src/obligations/operators.ts +1 -2
  76. package/src/obligations/pragma.ts +1 -10
  77. package/src/obligations/printer.ts +47 -46
  78. package/src/obligations/proof.ts +1 -5
  79. package/src/obligations/regroup.ts +1 -4
  80. package/src/obligations/simplify.ts +42 -58
  81. package/src/obligations/source.ts +48 -78
  82. package/src/obligations/subexpression.ts +5 -11
  83. package/src/obligations/substitution.ts +1 -3
  84. package/src/obligations/temporal.ts +13 -19
  85. package/src/obligations/tuples.ts +3 -9
  86. package/src/parallel-worker.ts +30 -60
  87. package/src/parallel.ts +57 -71
  88. package/src/parser.ts +584 -316
  89. package/src/pcal/ast.ts +53 -86
  90. package/src/pcal/parse.ts +144 -219
  91. package/src/pcal/tokenize.ts +91 -131
  92. package/src/pcal/translate.ts +83 -130
  93. package/src/pcal/validate.ts +48 -68
  94. package/src/pretty-print.ts +7 -8
  95. package/src/prover/decide-pool.ts +8 -13
  96. package/src/prover/deciders.ts +3 -7
  97. package/src/prover/first-order.ts +0 -11
  98. package/src/prover/isabelle/decide.ts +0 -6
  99. package/src/prover/isabelle/encode.ts +11 -23
  100. package/src/prover/isabelle/isar/inner-syntax.ts +114 -115
  101. package/src/prover/isabelle/isar/method.ts +29 -53
  102. package/src/prover/isabelle/isar/proof.ts +1 -10
  103. package/src/prover/isabelle/isar/statement.ts +1 -6
  104. package/src/prover/isabelle/isar/theory.ts +26 -36
  105. package/src/prover/isabelle/isar/type-check.ts +17 -31
  106. package/src/prover/isabelle/provers/blast/blast.ts +17 -104
  107. package/src/prover/isabelle/provers/blast/branch.ts +1 -32
  108. package/src/prover/isabelle/provers/blast/prove.ts +1 -35
  109. package/src/prover/isabelle/provers/blast/rules.ts +6 -44
  110. package/src/prover/isabelle/provers/blast/state.ts +2 -28
  111. package/src/prover/isabelle/provers/blast/term.ts +9 -36
  112. package/src/prover/isabelle/provers/clasimp.ts +1 -13
  113. package/src/prover/isabelle/provers/classical.ts +14 -42
  114. package/src/prover/isabelle/provers/hypsubst.ts +28 -51
  115. package/src/prover/isabelle/pure/bicompose.ts +25 -38
  116. package/src/prover/isabelle/pure/consts.ts +1 -7
  117. package/src/prover/isabelle/pure/conv.ts +3 -17
  118. package/src/prover/isabelle/pure/drule.ts +33 -42
  119. package/src/prover/isabelle/pure/envir.ts +3 -52
  120. package/src/prover/isabelle/pure/goal.ts +1 -7
  121. package/src/prover/isabelle/pure/interrupt.ts +0 -5
  122. package/src/prover/isabelle/pure/list.ts +0 -3
  123. package/src/prover/isabelle/pure/logic.ts +2 -15
  124. package/src/prover/isabelle/pure/name.ts +2 -9
  125. package/src/prover/isabelle/pure/net.ts +3 -59
  126. package/src/prover/isabelle/pure/object-logic.ts +3 -10
  127. package/src/prover/isabelle/pure/pattern.ts +2 -19
  128. package/src/prover/isabelle/pure/raw-simplifier.ts +17 -62
  129. package/src/prover/isabelle/pure/rewriter.ts +31 -67
  130. package/src/prover/isabelle/pure/search.ts +3 -10
  131. package/src/prover/isabelle/pure/seq.ts +1 -10
  132. package/src/prover/isabelle/pure/simplifier.ts +4 -12
  133. package/src/prover/isabelle/pure/table.ts +3 -32
  134. package/src/prover/isabelle/pure/tactic.ts +12 -22
  135. package/src/prover/isabelle/pure/tactical.ts +48 -47
  136. package/src/prover/isabelle/pure/term-ord.ts +2 -6
  137. package/src/prover/isabelle/pure/term.ts +2 -43
  138. package/src/prover/isabelle/pure/thm.ts +27 -47
  139. package/src/prover/isabelle/pure/type.ts +2 -15
  140. package/src/prover/isabelle/pure/unify.ts +20 -30
  141. package/src/prover/isabelle/pure/variable.ts +3 -7
  142. package/src/prover/isabelle/tla/classical-setup.ts +17 -24
  143. package/src/prover/isabelle/tla/context.ts +15 -18
  144. package/src/prover/isabelle/tla/logic.ts +14 -32
  145. package/src/prover/isabelle/tla/simpdata.ts +46 -43
  146. package/src/prover/isabelle/tools/intuitionistic.ts +1 -10
  147. package/src/prover/ocaml.ts +0 -6
  148. package/src/prover/prep/action.ts +14 -54
  149. package/src/prover/prep/enabled-axioms.ts +12 -19
  150. package/src/prover/prep/fingerprint.ts +2 -21
  151. package/src/prover/prep/level-comparison.ts +8 -12
  152. package/src/prover/prep/md5.ts +0 -2
  153. package/src/prover/prep/methods.ts +33 -39
  154. package/src/prover/prep/normalize.ts +4 -28
  155. package/src/prover/prove-worker.ts +0 -2
  156. package/src/prover/prove.ts +87 -100
  157. package/src/prover/ptl/decide.ts +7 -12
  158. package/src/prover/ptl/encode.ts +14 -36
  159. package/src/prover/ptl/sat.ts +24 -32
  160. package/src/prover/ptl/tableau.ts +12 -26
  161. package/src/prover/smt/axioms.ts +0 -7
  162. package/src/prover/smt/context.ts +20 -28
  163. package/src/prover/smt/data.ts +433 -424
  164. package/src/prover/smt/encode.ts +43 -52
  165. package/src/prover/smt/extensionality.ts +0 -10
  166. package/src/prover/smt/flatten.ts +3 -22
  167. package/src/prover/smt/format.ts +5 -10
  168. package/src/prover/smt/instances.ts +0 -10
  169. package/src/prover/smt/rewrite.ts +663 -667
  170. package/src/prover/smt/solver/act-cache.ts +0 -15
  171. package/src/prover/smt/solver/arith-rewriter.ts +3 -48
  172. package/src/prover/smt/solver/array.ts +1 -3
  173. package/src/prover/smt/solver/asserted-formulas.ts +1 -43
  174. package/src/prover/smt/solver/ast-lt.ts +1 -11
  175. package/src/prover/smt/solver/ast-util.ts +1 -6
  176. package/src/prover/smt/solver/ast.ts +1 -165
  177. package/src/prover/smt/solver/auf-solver.ts +8 -44
  178. package/src/prover/smt/solver/case-split-queue.ts +1 -9
  179. package/src/prover/smt/solver/cg-table.ts +1 -18
  180. package/src/prover/smt/solver/clause.ts +1 -27
  181. package/src/prover/smt/solver/conflict-resolution.ts +0 -9
  182. package/src/prover/smt/solver/context.ts +7 -64
  183. package/src/prover/smt/solver/ctx-simplify.ts +14 -41
  184. package/src/prover/smt/solver/der.ts +2 -21
  185. package/src/prover/smt/solver/dyn-ack.ts +1 -11
  186. package/src/prover/smt/solver/e-matching.ts +4 -35
  187. package/src/prover/smt/solver/enode.ts +1 -21
  188. package/src/prover/smt/solver/expr-ref.ts +1 -5
  189. package/src/prover/smt/solver/expr-safe-replace.ts +1 -6
  190. package/src/prover/smt/solver/fingerprints.ts +1 -12
  191. package/src/prover/smt/solver/flat-literals.ts +1 -9
  192. package/src/prover/smt/solver/goal.ts +2 -14
  193. package/src/prover/smt/solver/hash.ts +0 -8
  194. package/src/prover/smt/solver/hashtable.ts +1 -12
  195. package/src/prover/smt/solver/internalizer.ts +7 -100
  196. package/src/prover/smt/solver/lbool.ts +1 -2
  197. package/src/prover/smt/solver/literal.ts +0 -3
  198. package/src/prover/smt/solver/lp/bound-propagator.ts +8 -26
  199. package/src/prover/smt/solver/lp/constraints.ts +2 -8
  200. package/src/prover/smt/solver/lp/containers.ts +12 -30
  201. package/src/prover/smt/solver/lp/core-solver.ts +10 -31
  202. package/src/prover/smt/solver/lp/cross-nested.ts +6 -20
  203. package/src/prover/smt/solver/lp/dep-intervals.ts +7 -22
  204. package/src/prover/smt/solver/lp/dependency.ts +1 -4
  205. package/src/prover/smt/solver/lp/dioph-eq.ts +9 -102
  206. package/src/prover/smt/solver/lp/explanation.ts +1 -6
  207. package/src/prover/smt/solver/lp/gomory.ts +9 -22
  208. package/src/prover/smt/solver/lp/hnf-cutter.ts +17 -23
  209. package/src/prover/smt/solver/lp/hnf.ts +11 -26
  210. package/src/prover/smt/solver/lp/int-cube.ts +15 -36
  211. package/src/prover/smt/solver/lp/int-solver.ts +4 -34
  212. package/src/prover/smt/solver/lp/lar-solver.ts +32 -81
  213. package/src/prover/smt/solver/lp/lar-term.ts +1 -5
  214. package/src/prover/smt/solver/lp/lp-settings.ts +1 -14
  215. package/src/prover/smt/solver/lp/maximize-term.ts +20 -45
  216. package/src/prover/smt/solver/lp/nex-creator.ts +5 -28
  217. package/src/prover/smt/solver/lp/nex.ts +1 -6
  218. package/src/prover/smt/solver/lp/nla-backoff.ts +1 -4
  219. package/src/prover/smt/solver/lp/nla-basics.ts +5 -30
  220. package/src/prover/smt/solver/lp/nla-core.ts +36 -135
  221. package/src/prover/smt/solver/lp/nla-divisions.ts +32 -42
  222. package/src/prover/smt/solver/lp/nla-emonics.ts +4 -21
  223. package/src/prover/smt/solver/lp/nla-factorization.ts +1 -4
  224. package/src/prover/smt/solver/lp/nla-grobner.ts +15 -69
  225. package/src/prover/smt/solver/lp/nla-horner.ts +3 -11
  226. package/src/prover/smt/solver/lp/nla-intervals.ts +2 -12
  227. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +16 -41
  228. package/src/prover/smt/solver/lp/nla-monotone.ts +2 -6
  229. package/src/prover/smt/solver/lp/nla-order.ts +2 -13
  230. package/src/prover/smt/solver/lp/nla-patcher.ts +4 -7
  231. package/src/prover/smt/solver/lp/nla-tangents.ts +1 -9
  232. package/src/prover/smt/solver/lp/nla-throttle.ts +1 -6
  233. package/src/prover/smt/solver/lp/nla-types.ts +1 -11
  234. package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -25
  235. package/src/prover/smt/solver/lp/nra-solver.ts +5 -52
  236. package/src/prover/smt/solver/lp/numeric-pair.ts +1 -4
  237. package/src/prover/smt/solver/lp/pdd-interval.ts +1 -3
  238. package/src/prover/smt/solver/lp/pdd-simplifier.ts +2 -14
  239. package/src/prover/smt/solver/lp/pdd-solver.ts +4 -33
  240. package/src/prover/smt/solver/lp/pdd.ts +7 -70
  241. package/src/prover/smt/solver/lp/random-updater.ts +1 -8
  242. package/src/prover/smt/solver/lp/static-matrix.ts +9 -19
  243. package/src/prover/smt/solver/lp/union-find.ts +1 -6
  244. package/src/prover/smt/solver/mam-compiler.ts +18 -58
  245. package/src/prover/smt/solver/mam-interpreter.ts +1 -21
  246. package/src/prover/smt/solver/maximize-ac-sharing.ts +2 -17
  247. package/src/prover/smt/solver/model-checker.ts +3 -37
  248. package/src/prover/smt/solver/model-evaluator.ts +3 -29
  249. package/src/prover/smt/solver/model-finder.ts +15 -32
  250. package/src/prover/smt/solver/model-generator.ts +1 -12
  251. package/src/prover/smt/solver/model-macro-solver.ts +23 -88
  252. package/src/prover/smt/solver/nlsat/algebraic.ts +63 -159
  253. package/src/prover/smt/solver/nlsat/atoms.ts +25 -23
  254. package/src/prover/smt/solver/nlsat/evaluator.ts +17 -22
  255. package/src/prover/smt/solver/nlsat/explain.ts +10 -69
  256. package/src/prover/smt/solver/nlsat/interval-set.ts +9 -50
  257. package/src/prover/smt/solver/nlsat/levelwise.ts +49 -135
  258. package/src/prover/smt/solver/nlsat/mpbq.ts +3 -14
  259. package/src/prover/smt/solver/nlsat/polynomial.ts +29 -159
  260. package/src/prover/smt/solver/nlsat/solver.ts +18 -90
  261. package/src/prover/smt/solver/nlsat/types.ts +1 -8
  262. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +2 -45
  263. package/src/prover/smt/solver/nlsat/upolynomial.ts +11 -92
  264. package/src/prover/smt/solver/nnf.ts +3 -38
  265. package/src/prover/smt/solver/pattern-inference.ts +2 -15
  266. package/src/prover/smt/solver/poly-rewriter.ts +3 -23
  267. package/src/prover/smt/solver/propagate-values.ts +1 -10
  268. package/src/prover/smt/solver/proto-model.ts +3 -52
  269. package/src/prover/smt/solver/pull-quant.ts +1 -12
  270. package/src/prover/smt/solver/qe-lite-der.ts +1 -29
  271. package/src/prover/smt/solver/qe-lite-fm.ts +2 -25
  272. package/src/prover/smt/solver/qe-lite.ts +3 -33
  273. package/src/prover/smt/solver/qi-queue.ts +1 -16
  274. package/src/prover/smt/solver/quant-tactics.ts +5 -22
  275. package/src/prover/smt/solver/quantifier-analyzer.ts +27 -47
  276. package/src/prover/smt/solver/quantifier-manager.ts +2 -13
  277. package/src/prover/smt/solver/random-gen.ts +1 -2
  278. package/src/prover/smt/solver/rational.ts +1 -8
  279. package/src/prover/smt/solver/ref-vector.ts +1 -6
  280. package/src/prover/smt/solver/relevancy.ts +2 -15
  281. package/src/prover/smt/solver/rewriter-tpl.ts +3 -147
  282. package/src/prover/smt/solver/rewriter.ts +3 -85
  283. package/src/prover/smt/solver/rlimit.ts +2 -20
  284. package/src/prover/smt/solver/smtlib-parser.ts +80 -131
  285. package/src/prover/smt/solver/solver.ts +23 -34
  286. package/src/prover/smt/solver/sort.ts +1 -5
  287. package/src/prover/smt/solver/sticky-generations.ts +0 -5
  288. package/src/prover/smt/solver/theory-lra-axioms.ts +3 -24
  289. package/src/prover/smt/solver/theory-lra-bounds.ts +1 -7
  290. package/src/prover/smt/solver/theory-lra-final-check.ts +4 -22
  291. package/src/prover/smt/solver/theory-lra-internalize.ts +6 -43
  292. package/src/prover/smt/solver/theory-lra-model.ts +4 -19
  293. package/src/prover/smt/solver/theory-lra-propagate.ts +1 -15
  294. package/src/prover/smt/solver/theory-lra-state.ts +3 -8
  295. package/src/prover/smt/solver/theory-lra.ts +1 -8
  296. package/src/prover/smt/solver/var-subst.ts +2 -26
  297. package/src/prover/smt/table.ts +0 -8
  298. package/src/prover/tlapm/cache.ts +45 -40
  299. package/src/prover/tlapm/ocaml-marshal.ts +19 -21
  300. package/src/prover/tlapm/tlapm.ts +21 -20
  301. package/src/prover/verdict.ts +7 -2
  302. package/src/prover/zenon/check/check.ts +1 -11
  303. package/src/prover/zenon/check/definitions.ts +5 -15
  304. package/src/prover/zenon/check/equality.ts +0 -8
  305. package/src/prover/zenon/check/functions.ts +0 -4
  306. package/src/prover/zenon/check/keys.ts +0 -7
  307. package/src/prover/zenon/check/propositional.ts +0 -3
  308. package/src/prover/zenon/check/records.ts +1 -6
  309. package/src/prover/zenon/check/relations.ts +2 -14
  310. package/src/prover/zenon/check/rewrites.ts +2 -21
  311. package/src/prover/zenon/check/sets.ts +40 -61
  312. package/src/prover/zenon/check/terms.ts +1 -19
  313. package/src/prover/zenon/check/tla.ts +0 -6
  314. package/src/prover/zenon/decide.ts +0 -12
  315. package/src/prover/zenon/encode.ts +1 -8
  316. package/src/prover/zenon/eqrel.ts +2 -13
  317. package/src/prover/zenon/equality-rules.ts +0 -4
  318. package/src/prover/zenon/expr.ts +8 -26
  319. package/src/prover/zenon/index-table.ts +26 -57
  320. package/src/prover/zenon/isar-case.ts +9 -19
  321. package/src/prover/zenon/isar.ts +36 -64
  322. package/src/prover/zenon/limits.ts +0 -4
  323. package/src/prover/zenon/llproof.ts +2 -20
  324. package/src/prover/zenon/mltoll.ts +66 -104
  325. package/src/prover/zenon/node.ts +1 -11
  326. package/src/prover/zenon/ocaml.ts +1 -17
  327. package/src/prover/zenon/parse.ts +4 -12
  328. package/src/prover/zenon/phrase.ts +1 -11
  329. package/src/prover/zenon/proof.ts +0 -9
  330. package/src/prover/zenon/propositional-rules.ts +0 -5
  331. package/src/prover/zenon/rules.ts +2 -6
  332. package/src/prover/zenon/search.ts +5 -31
  333. package/src/prover/zenon/tla/functions.ts +59 -31
  334. package/src/prover/zenon/tla/llproof.ts +67 -76
  335. package/src/prover/zenon/tla/records.ts +57 -33
  336. package/src/prover/zenon/tla/rewrites.ts +12 -19
  337. package/src/prover/zenon/tla/sets.ts +83 -53
  338. package/src/prover/zenon/tla/terms.ts +0 -6
  339. package/src/prover/zenon/tla/tla.ts +66 -64
  340. package/src/randomization.ts +81 -35
  341. package/src/resolve.ts +67 -184
  342. package/src/runtime.ts +22 -22
  343. package/src/sany/builtins.ts +2 -5
  344. package/src/sany/generation.ts +394 -0
  345. package/src/sany/generator-def-entries.ts +210 -0
  346. package/src/sany/generator-definitions.ts +686 -0
  347. package/src/sany/generator-expressions.ts +1100 -0
  348. package/src/sany/generator-proofs.ts +662 -0
  349. package/src/sany/generator.ts +191 -2764
  350. package/src/sany/levels.ts +6 -34
  351. package/src/sany/lexer.ts +58 -77
  352. package/src/sany/linter.ts +1 -9
  353. package/src/sany/modules.ts +1 -5
  354. package/src/sany/nodes.ts +5 -43
  355. package/src/sany/parse-error.ts +39 -117
  356. package/src/sany/report.ts +32 -69
  357. package/src/sany/selectors.ts +49 -76
  358. package/src/sany/source.ts +1 -17
  359. package/src/sany/xml.ts +35 -117
  360. package/src/search.ts +541 -163
  361. package/src/simulate.ts +709 -0
  362. package/src/spec-description.ts +192 -100
  363. package/src/standard-definitions.ts +0 -4
  364. package/src/standard-modules.ts +264 -95
  365. package/src/state-graph.ts +52 -31
  366. package/src/subexpressions.ts +69 -52
  367. package/src/substitution-levels.ts +7 -7
  368. package/src/successors.ts +266 -99
  369. package/src/symmetry.ts +33 -25
  370. package/src/tableau.ts +27 -24
  371. package/src/tlc/fingerprint.ts +183 -67
  372. package/src/tlc/jar-data.ts +112 -6
  373. package/src/tlc/messages.ts +125 -54
  374. package/src/tlc/order.ts +39 -69
  375. package/src/tlc/random.ts +37 -49
  376. package/src/tlc/replay.ts +553 -228
  377. package/src/tlc/summary.ts +36 -47
  378. package/src/tlc/tlc-operators.ts +78 -44
  379. package/src/tlc/tlcext-operators.ts +139 -122
  380. package/src/tlc/trace-spec.ts +249 -0
  381. package/src/tlc/value-stream.ts +139 -0
  382. package/src/tlc/version.ts +1 -4
  383. package/src/transfer.ts +22 -25
  384. package/src/typescript/actions.ts +479 -0
  385. package/src/typescript/collections.ts +614 -0
  386. package/src/typescript/compile.ts +63 -0
  387. package/src/typescript/context.ts +94 -0
  388. package/src/typescript/emit.ts +121 -0
  389. package/src/typescript/expressions.ts +496 -0
  390. package/src/typescript/formula.ts +154 -0
  391. package/src/typescript/functions.ts +284 -0
  392. package/src/typescript/model.ts +162 -0
  393. package/src/typescript/names.ts +114 -0
  394. package/src/typescript/program.ts +48 -0
  395. package/src/typescript/properties.ts +139 -0
  396. package/src/typescript/scope.ts +112 -0
  397. package/src/typescript/steps.ts +167 -0
  398. package/src/typescript/types.ts +171 -0
  399. package/src/values.ts +832 -484
  400. package/src/worker.ts +7 -8
package/src/resolve.ts CHANGED
@@ -22,8 +22,6 @@ import {
22
22
  } from "./ast";
23
23
  import { formatRange, TlaSyntaxError, type SanyError } from "./errors";
24
24
 
25
- // The kinds of symbol SANY tells apart when it binds a name twice. A parameter is also a bound name,
26
- // and a NEW symbol has a kind for each level.
27
25
  export type SymbolKind =
28
26
  | "Builtin"
29
27
  | "Constant"
@@ -34,8 +32,6 @@ export type SymbolKind =
34
32
  | "Parameter"
35
33
  | `New${NewSymbol["level"]}`;
36
34
 
37
- // range is where SANY reports the symbol, and two symbols at one range are one node to SANY. params
38
- // holds the arity of each parameter of a defined operator.
39
35
  interface Binding {
40
36
  kind: SymbolKind;
41
37
  arity: number;
@@ -49,17 +45,12 @@ export interface ModuleSymbol extends Binding {
49
45
 
50
46
  export type Symbols = Map<string, ModuleSymbol>;
51
47
 
52
- // A named theorem or proof step whose parts a name such as foo!lab or <1>2!1 selects. isInProof
53
- // holds while SANY analyzes the proof of the theorem or step.
54
48
  interface Goal {
55
49
  statement: Expression | AssumeProve;
56
50
  isSuffices: boolean;
57
51
  isInProof: boolean;
58
52
  }
59
53
 
60
- // isPending holds for an operator a RECURSIVE declaration names until its definition.
61
- // isNonFact holds for the name of a USE, HIDE, DEFINE or INSTANCE step, which SANY takes neither as
62
- // a fact nor as an expression.
63
54
  interface LocalName extends Binding {
64
55
  name: string;
65
56
  parent: LocalName | undefined;
@@ -68,13 +59,11 @@ interface LocalName extends Binding {
68
59
  isNonFact?: boolean;
69
60
  }
70
61
 
71
- // A name the module binds, from position on.
72
62
  interface ModuleBinding extends Binding {
73
63
  position: SourceLocation;
74
64
  isPending?: boolean;
75
65
  }
76
66
 
77
- // What SANY reports while it goes on analyzing a unit of a module, and the scope it analyzes it in.
78
67
  interface Unit {
79
68
  imported: Symbols;
80
69
  getInstance: (instance: Instance) => InstancedModule;
@@ -87,15 +76,11 @@ interface Unit {
87
76
  bindings: Map<string, ModuleBinding>;
88
77
  errors: SanyError[];
89
78
  warnings: SanyError[];
90
- // In a definition, a named theorem, or a numbered step.
91
79
  hasLabelScope: boolean;
92
- // In the scope of a NEW in a nested ASSUME.
93
80
  isLabelForbidden: boolean;
94
81
  isInExcept: boolean;
95
- // The names bound since the innermost definition, label, or step, which a label must take.
96
82
  labelParams: string[];
97
83
  labels: Set<string>;
98
- // In a definition generated for a LET INSTANCE, or a subexpression analyzed where it is defined.
99
84
  isGenerated: boolean;
100
85
  }
101
86
 
@@ -114,8 +99,6 @@ type Reference = Expression & { kind: "Identifier" | "Apply" | "Subexpression" }
114
99
 
115
100
  export const LOCAL_RESOLUTION = { kind: "Local" } as const;
116
101
 
117
- // The subexpressions that names select, and for a name that selects none, the error SANY reports and
118
- // the arguments it analyzes before it.
119
102
  export const selections = new WeakSet<Expression>();
120
103
  export const selectionErrors = new WeakMap<Expression, SelectionError>();
121
104
 
@@ -123,7 +106,6 @@ const renamedArities = new WeakMap<Expression, number>();
123
106
 
124
107
  export const KEY_SEPARATOR = "@";
125
108
 
126
- // A key is a name, the separator, and an id, so the operator @@ is not one.
127
109
  const isKey = (name: string) => {
128
110
  const index = name.lastIndexOf(KEY_SEPARATOR);
129
111
  return index > 0 && index < name.length - 1;
@@ -151,8 +133,6 @@ const lookupLocal = (scope: LocalName | undefined, name: string) => {
151
133
  return undefined;
152
134
  };
153
135
 
154
- // A name the module binds before the unit and before the range, which a copy of a subexpression
155
- // that a reference selects keeps from its definition.
156
136
  const getModuleBinding = (name: string, unit: Unit, range: SourceRange | undefined) => {
157
137
  const binding = unit.bindings.get(name);
158
138
  return binding &&
@@ -165,8 +145,6 @@ const getModuleBinding = (name: string, unit: Unit, range: SourceRange | undefin
165
145
  const isSameRange = (left: SourceRange | undefined, right: SourceRange | undefined) =>
166
146
  left !== undefined && right !== undefined && formatRange(left) === formatRange(right);
167
147
 
168
- // SymbolTable.addSymbol: rejects redefining a built-in, a bound name, or a symbol of another kind
169
- // or arity, and warns of any other duplicate. A symbol SANY never sees, such as a generated one, binds.
170
148
  const isBindable = (name: string, current: Binding | undefined, added: Binding, unit: Unit) => {
171
149
  if (current === undefined) return true;
172
150
  if (isSameRange(current.range, added.range)) return false;
@@ -214,7 +192,6 @@ const bindName = (
214
192
  ? { ...added, name, parent: scope }
215
193
  : scope;
216
194
 
217
- // Binds the names of a module from the unit on.
218
195
  const bindModuleName = (name: string, added: Binding, unit: Unit) => {
219
196
  if (isBindable(name, unit.bindings.get(name), added, unit)) {
220
197
  unit.bindings.set(name, { ...added, position: unit.location });
@@ -276,14 +253,12 @@ const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
276
253
  const findBinding = (name: string, scope: LocalName | undefined, unit: Unit): Binding | undefined =>
277
254
  lookupLocal(scope, name) ?? findSymbol(name, unit);
278
255
 
279
- // SANY reports a name at the range of its text.
280
256
  const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
281
257
  module: location.range?.module ?? "",
282
258
  start: location,
283
259
  end: { line: location.line, column: location.column + name.length - 1 },
284
260
  });
285
261
 
286
- // SANY reports a name at its tokens, such as M ! ⊕ for M!\oplus.
287
262
  const getSpelledNameRange = (
288
263
  expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
289
264
  name: string,
@@ -295,7 +270,6 @@ const getSpelledNameRange = (
295
270
  : getNameRange(expression.location, expression.spelling ?? name);
296
271
  };
297
272
 
298
- // SANY reports an operator symbol applied to operands as one it cannot resolve.
299
273
  const getFixity = (expression: Expression) => {
300
274
  if (expression.kind !== "Apply") return undefined;
301
275
  if (expression.args.length === 2) return "infix";
@@ -303,7 +277,6 @@ const getFixity = (expression: Expression) => {
303
277
  return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
304
278
  };
305
279
 
306
- // SANY names an undefined operator symbol such as + differently from an undefined identifier.
307
280
  const isOperatorSymbol = (name: string) => !/^[\p{L}\d_]/u.test(name);
308
281
 
309
282
  const isStepName = (name: string) =>
@@ -315,8 +288,6 @@ export const getOperandIndex = (operand: number | "<<" | ">>", count: number) =>
315
288
  return count === 2 ? 1 : -1;
316
289
  };
317
290
 
318
- // The parts of a name such as F(x)!l(y), each with its own arguments. counts holds the number each
319
- // part takes, when a part before the last takes some.
320
291
  export const getNameSelectors = (
321
292
  name: string,
322
293
  args: Expression[],
@@ -324,7 +295,7 @@ export const getNameSelectors = (
324
295
  ): Selector[] => {
325
296
  const parts = name.split("!");
326
297
  const partCounts =
327
- counts ?? parts.map((_, index) => (index === parts.length - 1 ? args.length : 0));
298
+ counts ?? parts.map((_part, index) => (index === parts.length - 1 ? args.length : 0));
328
299
  return parts.map((part, index) => {
329
300
  const start = partCounts.slice(0, index).reduce((sum, count) => sum + count, 0);
330
301
  return { kind: "Name", name: part, args: args.slice(start, start + partCounts[index]) };
@@ -343,8 +314,6 @@ const getBoundSets = (bounds: Bound[]) =>
343
314
  .filter((bound, index) => index === 0 || bounds[index - 1].set !== bound.set)
344
315
  .map((bound) => bound.set);
345
316
 
346
- // The names a binder binds, its body, and the sets it binds them to, which a selector such as !1
347
- // chooses from.
348
317
  export const getBinder = (
349
318
  expression: Expression,
350
319
  ): { names: string[]; body: Expression; sets: Expression[] } | undefined => {
@@ -387,7 +356,6 @@ export const getBinder = (
387
356
  }
388
357
  };
389
358
 
390
- // The operands that a selector such as !1 chooses from, where they do not depend on bound names.
391
359
  export const getOperands = (expression: Expression): Expression[] | undefined => {
392
360
  switch (expression.kind) {
393
361
  case "Apply":
@@ -456,7 +424,6 @@ const findLabel = (expression: Expression, name: string): LabelExpression | unde
456
424
  );
457
425
  };
458
426
 
459
- // SANY drops a label in the scope of a NEW in a nested ASSUME, after it reports the label.
460
427
  const findClauseLabel = (
461
428
  clause: Expression | NewSymbol | AssumeProve,
462
429
  name: string,
@@ -482,10 +449,6 @@ const getOperandName = (operand: number | "<<" | ">>") =>
482
449
 
483
450
  const SCOPE_MESSAGE = "within the scope of a declaration\n from outside that declaration's scope.";
484
451
 
485
- // Generator.selectorToNode for a name that selects part of a theorem or step. A part in the scope of
486
- // a NEW in its ASSUME may be selected only within the proof of the theorem, or only after the proof
487
- // of a SUFFICES step. A label takes its own arguments, and an operand must exist, unless the part
488
- // selected depends on bound names. A fact may select an ASSUME/PROVE or a NEW clause.
489
452
  const checkSelection = (
490
453
  reference: Reference,
491
454
  selectors: Selector[],
@@ -623,7 +586,6 @@ const checkSelection = (
623
586
  }
624
587
  };
625
588
 
626
- // Generator.generateLabel reports a label where it may not appear, and skips the label's body.
627
589
  const isLabelReported = (label: LabelExpression, unit: Unit) => {
628
590
  const message = unit.isGenerated
629
591
  ? undefined
@@ -639,9 +601,6 @@ const isLabelReported = (label: LabelExpression, unit: Unit) => {
639
601
  return true;
640
602
  };
641
603
 
642
- // Generator.generateLabel reports, after the label's body, each parameter that names no bound name,
643
- // then formalParamsEqual each parameter given twice, each name bound since the label scope began
644
- // that the label leaves out, and the parameters it adds, and then a label of a name already used.
645
604
  const checkLabel = (label: LabelExpression, scope: LocalName | undefined, unit: Unit) => {
646
605
  const range = label.location.range;
647
606
  if (!range || unit.isGenerated) return;
@@ -689,7 +648,6 @@ const checkLabel = (label: LabelExpression, scope: LocalName | undefined, unit:
689
648
  unit.labels.add(label.name);
690
649
  };
691
650
 
692
- // Checks a name that selects part of a theorem or step, and tells whether it is one.
693
651
  const checkReference = (
694
652
  expression: Expression,
695
653
  scope: LocalName | undefined,
@@ -707,9 +665,6 @@ const checkReference = (
707
665
  return goal !== undefined;
708
666
  };
709
667
 
710
- // Generator.selectorToNode reports an unknown operator and goes on without analyzing its arguments,
711
- // so its arity is undefined. It does the same for an operator given the wrong number of arguments,
712
- // and for a name that selects nothing, after the arguments of the parts before.
713
668
  const getSymbolArity = (
714
669
  expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
715
670
  name: string,
@@ -741,8 +696,6 @@ const getSymbolArity = (
741
696
  renamedArities.set(expression, symbol.arity);
742
697
  return symbol.arity;
743
698
  }
744
- // A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
745
- // arguments.
746
699
  const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
747
700
  const isSelected =
748
701
  prefix !== "" &&
@@ -796,10 +749,6 @@ const markRecursiveConstructions = (expression: Expression, name: string): void
796
749
  getChildren(expression).forEach((child) => markRecursiveConstructions(child, name));
797
750
  };
798
751
 
799
- // Generator.processOperator binds the parameters, reports a name already bound unless a RECURSIVE
800
- // declaration of this level awaits the definition, analyzes the body, and then binds the name.
801
- // processFunction binds the bound names before it looks up the name, and binds the name only where
802
- // nothing else does. recursive holds the declarations of this level. Tells whether to bind the name.
803
752
  const resolveDefinition = (
804
753
  definition: Definition,
805
754
  scope: LocalName | undefined,
@@ -858,8 +807,6 @@ const resolveDefinition = (
858
807
  );
859
808
  };
860
809
 
861
- // SANY analyzes an INSTANCE in a LET where it appears, before the definitions that follow it, which
862
- // tla-ts generates at its location and analyzes without reporting.
863
810
  const resolveDefinitions = (
864
811
  definitions: Definition[],
865
812
  scope: LocalName | undefined,
@@ -918,7 +865,6 @@ const resolveDefinitions = (
918
865
  return resolveInstances(resolved, () => true);
919
866
  };
920
867
 
921
- // The INSTANCE that a name such as K!C starts with, whose arguments SANY requires at its name.
922
868
  const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) => {
923
869
  const prefix = name.split("!")[0];
924
870
  const binding =
@@ -928,8 +874,6 @@ const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) =>
928
874
  return binding?.kind === "Instance" ? { name: prefix, arity: binding.arity } : undefined;
929
875
  };
930
876
 
931
- // Generator.selectorToNode reports an INSTANCE given the wrong number of arguments at its name,
932
- // as in K!C or K(1, 2)!C, and goes on without analyzing them. Tells whether it reports one.
933
877
  const isInstanceArityReported = (
934
878
  expression: Expression,
935
879
  scope: LocalName | undefined,
@@ -955,11 +899,7 @@ const isInstanceArityReported = (
955
899
  return true;
956
900
  };
957
901
 
958
- // Generator.generateExprOrOpArg analyzes an argument as an expression, unless a defined operator
959
- // takes an operator in its place, which a name, a built-in operator, or a LAMBDA of the parameter's
960
- // arity must give. OpDefNode#match then reports each argument that gives no such operator, or one
961
- // whose own parameters take operators. tla-ts lets an operator whose parameters it does not know
962
- // take any argument.
902
+ // Deviation from SANY: an operator whose parameters tla-ts does not know takes any argument.
963
903
  const resolveApply = (
964
904
  expression: Expression & { kind: "Apply" },
965
905
  scope: LocalName | undefined,
@@ -990,8 +930,7 @@ const resolveApply = (
990
930
  const expected = params[index] ?? 0;
991
931
  const range = arg.location.range;
992
932
  if (expected === 0) {
993
- // SANY fails on an operator here with an internal error, so tla-ts words it as it does for
994
- // a name.
933
+ // Deviation from SANY: SANY fails here with an internal error; tla-ts words it as for a name.
995
934
  if (arg.kind === "Lambda" && arg.operator !== undefined) {
996
935
  report(range, `The operator ${arg.operator} requires 1 arguments.`);
997
936
  } else if (arg.kind === "Lambda" && range) {
@@ -1004,7 +943,6 @@ const resolveApply = (
1004
943
  } else resolveExpression(arg, scope, unit);
1005
944
  return true;
1006
945
  }
1007
- // A name that selects a subexpression gives an operator of the names it leaves unbound.
1008
946
  if (arg.kind === "Lambda") {
1009
947
  resolveExpression(arg, scope, unit);
1010
948
  const isMatched = arg.params.length === expected;
@@ -1025,13 +963,11 @@ const resolveApply = (
1025
963
  resolveExpression(arg, scope, unit);
1026
964
  return true;
1027
965
  }
1028
- // Generator.selectorToNode: a name such as K(1)!C names an operator, but none of its parts
1029
- // may take arguments.
1030
966
  if (arg.kind === "Apply" && arg.argumentCounts?.at(-1) === 0) {
1031
- const index = arg.argumentCounts.findIndex((count) => count > 0);
967
+ const selectorIndex = arg.argumentCounts.findIndex((count) => count > 0);
1032
968
  report(
1033
- arg.selectorRanges?.[index],
1034
- `Selector \`${arg.operator.split("!")[index]}' should not have argument(s).`,
969
+ arg.selectorRanges?.[selectorIndex],
970
+ `Selector \`${arg.operator.split("!")[selectorIndex]}' should not have argument(s).`,
1035
971
  );
1036
972
  return false;
1037
973
  }
@@ -1063,13 +999,11 @@ const resolveApply = (
1063
999
  });
1064
1000
  };
1065
1001
 
1066
- // An argument may name an operator that takes arguments of its own.
1067
1002
  const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
1068
1003
  if (arg.kind === "Identifier") getSymbolArity(arg, arg.name, scope, unit);
1069
1004
  else resolveExpression(arg, scope, unit);
1070
1005
  };
1071
1006
 
1072
- // Reports the error of a name that selects nothing, after the arguments SANY analyzes before it.
1073
1007
  const isSelectionReported = (expression: Expression, scope: LocalName | undefined, unit: Unit) => {
1074
1008
  const failed = selectionErrors.get(expression);
1075
1009
  if (!failed) return false;
@@ -1245,9 +1179,6 @@ const resolveExpression = (
1245
1179
  }
1246
1180
  };
1247
1181
 
1248
- // Generator.generateUseOrHide looks up a fact that is a name as it looks up a name in an expression,
1249
- // except that the name may select an ASSUME/PROVE or a NEW clause or name a theorem or assumption,
1250
- // and analyzes any other fact as an expression.
1251
1182
  const checkFacts = (facts: Fact[], scope: LocalName | undefined, unit: Unit) =>
1252
1183
  facts.forEach((fact) => {
1253
1184
  if (
@@ -1277,8 +1208,6 @@ const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit:
1277
1208
  );
1278
1209
  };
1279
1210
 
1280
- // The NEW symbols of an ASSUME stay in scope for its conclusion and the proof that follows. SANY
1281
- // forbids a label in the scope of a NEW in a nested ASSUME.
1282
1211
  const resolveAssumeProve = (
1283
1212
  assumeProve: AssumeProve,
1284
1213
  scope: LocalName | undefined,
@@ -1314,8 +1243,6 @@ const resolveStatement = (
1314
1243
  return scope;
1315
1244
  };
1316
1245
 
1317
- // SANY resolves the expressions of a proof, while its facts and DEF clauses may also name
1318
- // theorems, assumptions, and modules.
1319
1246
  const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, outer: Unit) => {
1320
1247
  const unit = outer.hasLabelScope ? { ...outer, hasLabelScope: false } : outer;
1321
1248
  if (proof?.kind === "By") checkFacts(proof.facts, scope, unit);
@@ -1323,7 +1250,6 @@ const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, ou
1323
1250
  proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
1324
1251
  };
1325
1252
 
1326
- // A LET or a step binds the name of the INSTANCE after the names it adds.
1327
1253
  const resolveLocalInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
1328
1254
  const instanced = resolveInstance(instance, scope, unit);
1329
1255
  return instance.name === undefined
@@ -1336,8 +1262,6 @@ const resolveLocalInstance = (instance: Instance, scope: LocalName | undefined,
1336
1262
  );
1337
1263
  };
1338
1264
 
1339
- // Generator.generateProof binds the name of a step after its statement and before its proof, or
1340
- // after the step where it takes no proof.
1341
1265
  const resolveStep = (
1342
1266
  step: ProofStep,
1343
1267
  scope: LocalName | undefined,
@@ -1355,8 +1279,6 @@ const resolveStep = (
1355
1279
  ? parent
1356
1280
  : { name: step.name, arity: 0, kind: "Theorem", parent, isNonFact: true };
1357
1281
  switch (body.kind) {
1358
- // The NEW symbols of a SUFFICES ASSUME are in scope after its proof, and those of any other
1359
- // ASSUME in its proof.
1360
1282
  case "Assert": {
1361
1283
  const assumed = resolveStatement(body.statement, scope, stepUnit);
1362
1284
  const proofScope = body.isSuffices ? scope : assumed;
@@ -1423,7 +1345,6 @@ const resolveStep = (
1423
1345
  }
1424
1346
  };
1425
1347
 
1426
- // SANY binds the name of a theorem after its statement and before its proof.
1427
1348
  const resolveTheorem = (theorem: Theorem, unit: Unit) => {
1428
1349
  const isDefinition = theorem.name !== undefined && theorem.statement.kind !== "AssumeProve";
1429
1350
  const scope = isDefinition ? undefined : resolveStatement(theorem.statement, undefined, unit);
@@ -1440,16 +1361,12 @@ const resolveTheorem = (theorem: Theorem, unit: Unit) => {
1440
1361
  if (goal) goal.isInProof = false;
1441
1362
  };
1442
1363
 
1443
- // The declarations of the module an INSTANCE instantiates, the names the INSTANCE adds, and the
1444
- // module that instantiates it.
1445
1364
  interface InstancedModule {
1446
1365
  declarations: Declaration[];
1447
1366
  symbols: Symbols;
1448
1367
  instancer: string;
1449
1368
  }
1450
1369
 
1451
- // What SANY reports at a location as it links a module, such as a conflict between the modules it
1452
- // extends.
1453
1370
  export interface ModuleReport {
1454
1371
  location: SourceLocation;
1455
1372
  errors: SanyError[];
@@ -1459,13 +1376,10 @@ export interface ModuleReport {
1459
1376
  export interface ModuleUnit {
1460
1377
  imported: Symbols;
1461
1378
  getInstance: (instance: Instance) => InstancedModule;
1462
- // The built-in operators and the names of the enclosing and extended modules.
1463
1379
  bound: Symbols;
1464
1380
  definitions: Map<string, Definition>;
1465
1381
  getKey: (definition: Definition) => string;
1466
- // The names each INSTANCE adds.
1467
1382
  instanced: Map<Instance, Symbols>;
1468
- // The theorems and assumptions the extended and instantiated modules export.
1469
1383
  facts: Set<string>;
1470
1384
  reports: ModuleReport[];
1471
1385
  warnings: SanyError[];
@@ -1473,7 +1387,6 @@ export interface ModuleUnit {
1473
1387
 
1474
1388
  export const MODULE_START: SourceLocation = { line: 0, column: 0 };
1475
1389
 
1476
- // A named theorem or assumption is linked as a definition too.
1477
1390
  export const isTheoremDefinition = (module: Module, definition: Definition) =>
1478
1391
  [...module.theorems, ...module.assumptions].some(
1479
1392
  (item) =>
@@ -1493,9 +1406,6 @@ const createSymbols = (module: Module, imported: Symbols) => {
1493
1406
  return symbols;
1494
1407
  };
1495
1408
 
1496
- // Generator.processSubst analyzes the explicit substitutions in the order they appear, then the
1497
- // implicit ones, and then reports each declaration left without one. The parameters of the INSTANCE
1498
- // are in scope.
1499
1409
  const checkSubstitutions = (
1500
1410
  instance: Instance,
1501
1411
  declarations: Declaration[],
@@ -1568,8 +1478,6 @@ const checkSubstitutions = (
1568
1478
  return substitutions;
1569
1479
  };
1570
1480
 
1571
- // Generator.processSubst analyzes an INSTANCE in a LET or a proof, which then binds the names the
1572
- // INSTANCE adds.
1573
1481
  const resolveInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
1574
1482
  const instanced = unit.getInstance(instance);
1575
1483
  checkSubstitutions(instance, instanced.declarations, instanced.instancer, scope, unit);
@@ -1579,7 +1487,6 @@ const resolveInstance = (instance: Instance, scope: LocalName | undefined, unit:
1579
1487
  );
1580
1488
  };
1581
1489
 
1582
- // tla-ts reports what SANY goes on after with an INSTANCE of the module.
1583
1490
  export const resolveSubstitutions = (
1584
1491
  instance: Instance,
1585
1492
  declarations: Declaration[],
@@ -1612,17 +1519,12 @@ export const resolveSubstitutions = (
1612
1519
  });
1613
1520
  };
1614
1521
 
1615
- // SANY reports the errors of every unit it checks, in the order of the units in the module, after
1616
- // the warnings it reports for the spec. A unit reports to its lists what SANY goes on after, and
1617
- // throws the error that stops it. An error tla-ts cannot word as SANY does yields to the errors the
1618
- // unit reported before it. Returns the errors.
1619
- export const collectSemanticErrors = (
1620
- units: {
1621
- location: SourceLocation;
1622
- check: (reported: SanyError[], warned: SanyError[]) => void;
1623
- }[],
1624
- warnings: SanyError[],
1625
- ) => {
1522
+ interface SemanticUnit {
1523
+ location: SourceLocation;
1524
+ check: (reported: SanyError[], warned: SanyError[]) => void;
1525
+ }
1526
+
1527
+ export const collectSemanticErrors = (units: SemanticUnit[], warnings: SanyError[]) => {
1626
1528
  const errors: SanyError[] = [];
1627
1529
  const addUnique = (list: SanyError[], item: SanyError) => {
1628
1530
  if (
@@ -1658,8 +1560,6 @@ export const collectSemanticErrors = (
1658
1560
  return errors;
1659
1561
  };
1660
1562
 
1661
- // SANY binds the names of a module in the order of its units, and reports a RECURSIVE declaration
1662
- // that no definition follows at the end. Returns the errors.
1663
1563
  export const resolveModule = (module: Module, unit: ModuleUnit) => {
1664
1564
  const symbols = createSymbols(module, unit.imported);
1665
1565
  const facts = new Set([
@@ -1681,41 +1581,44 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1681
1581
  );
1682
1582
  const recursive = new Set<Binding>();
1683
1583
  const declarations: { declaration: Declaration; binding: ModuleBinding | undefined }[] = [];
1684
- const createUnit = (
1584
+ const createCheck = (
1685
1585
  location: SourceLocation,
1686
1586
  self: Definition | undefined,
1687
- errors: SanyError[],
1688
- warnings: SanyError[],
1689
1587
  hasLabelScope: boolean,
1690
- ): Unit => ({
1691
- imported: symbols,
1692
- getInstance: unit.getInstance,
1693
- definitions: unit.definitions,
1694
- getKey: unit.getKey,
1588
+ check: (inner: Unit) => void,
1589
+ ): SemanticUnit => ({
1695
1590
  location,
1696
- self,
1697
- facts,
1698
- goals,
1699
- bindings,
1700
- errors,
1701
- warnings,
1702
- hasLabelScope,
1703
- isLabelForbidden: false,
1704
- isInExcept: false,
1705
- labelParams: [],
1706
- labels: new Set(),
1707
- isGenerated: false,
1591
+ check: (errors, warnings) =>
1592
+ check({
1593
+ imported: symbols,
1594
+ getInstance: unit.getInstance,
1595
+ definitions: unit.definitions,
1596
+ getKey: unit.getKey,
1597
+ location,
1598
+ self,
1599
+ facts,
1600
+ goals,
1601
+ bindings,
1602
+ errors,
1603
+ warnings,
1604
+ hasLabelScope,
1605
+ isLabelForbidden: false,
1606
+ isInExcept: false,
1607
+ labelParams: [],
1608
+ labels: new Set(),
1609
+ isGenerated: false,
1610
+ }),
1708
1611
  });
1709
1612
  const bindDeclarations = (declarations: Declaration[], kind: "Constant" | "Variable") =>
1710
- declarations.map((declaration) => ({
1711
- location: declaration.location,
1712
- check: (errors: SanyError[], warnings: SanyError[]) =>
1613
+ declarations.map((declaration) =>
1614
+ createCheck(declaration.location, undefined, false, (inner) =>
1713
1615
  bindModuleName(
1714
1616
  declaration.name,
1715
1617
  { kind, arity: declaration.arity, range: declaration.location.range },
1716
- createUnit(declaration.location, undefined, errors, warnings, false),
1618
+ inner,
1717
1619
  ),
1718
- }));
1620
+ ),
1621
+ );
1719
1622
  return collectSemanticErrors(
1720
1623
  [
1721
1624
  ...unit.reports.map((report) => ({
@@ -1729,9 +1632,8 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1729
1632
  ...bindDeclarations(module.variables, "Variable"),
1730
1633
  ...module.units
1731
1634
  .flatMap((item) => (item.kind === "Recursive" ? item.declarations : []))
1732
- .map((declaration) => ({
1733
- location: declaration.location,
1734
- check: (errors: SanyError[], warnings: SanyError[]) => {
1635
+ .map((declaration) =>
1636
+ createCheck(declaration.location, undefined, false, (inner) => {
1735
1637
  const binding: ModuleBinding = {
1736
1638
  kind: "Definition",
1737
1639
  arity: declaration.arity,
@@ -1743,19 +1645,17 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1743
1645
  declaration.name,
1744
1646
  bindings.get(declaration.name),
1745
1647
  binding,
1746
- createUnit(declaration.location, undefined, errors, warnings, false),
1648
+ inner,
1747
1649
  );
1748
1650
  if (isBound) {
1749
1651
  bindings.set(declaration.name, binding);
1750
1652
  recursive.add(binding);
1751
1653
  }
1752
1654
  declarations.push({ declaration, binding: isBound ? binding : undefined });
1753
- },
1754
- })),
1755
- ...module.definitions.map((definition) => ({
1756
- location: definition.location,
1757
- check: (errors: SanyError[], warnings: SanyError[]) => {
1758
- const inner = createUnit(definition.location, definition, errors, warnings, true);
1655
+ }),
1656
+ ),
1657
+ ...module.definitions.map((definition) =>
1658
+ createCheck(definition.location, definition, true, (inner) => {
1759
1659
  if (isTheoremDefinition(module, definition)) {
1760
1660
  resolveExpression(definition.body, undefined, inner);
1761
1661
  } else if (resolveDefinition(definition, undefined, inner, recursive)) {
@@ -1766,12 +1666,10 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1766
1666
  position: definition.location,
1767
1667
  });
1768
1668
  }
1769
- },
1770
- })),
1771
- ...module.instances.map((instance) => ({
1772
- location: instance.location,
1773
- check: (errors: SanyError[], warnings: SanyError[]) => {
1774
- const inner = createUnit(instance.location, undefined, errors, warnings, false);
1669
+ }),
1670
+ ),
1671
+ ...module.instances.map((instance) =>
1672
+ createCheck(instance.location, undefined, false, (inner) => {
1775
1673
  bindParams(undefined, instance.params, inner);
1776
1674
  unit.instanced
1777
1675
  .get(instance)
@@ -1783,18 +1681,10 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1783
1681
  inner,
1784
1682
  );
1785
1683
  }
1786
- },
1787
- })),
1788
- ...module.assumptions.map((assumption) => ({
1789
- location: assumption.location,
1790
- check: (errors: SanyError[], warnings: SanyError[]) => {
1791
- const inner = createUnit(
1792
- assumption.location,
1793
- undefined,
1794
- errors,
1795
- warnings,
1796
- assumption.name !== undefined,
1797
- );
1684
+ }),
1685
+ ),
1686
+ ...module.assumptions.map((assumption) =>
1687
+ createCheck(assumption.location, undefined, assumption.name !== undefined, (inner) => {
1798
1688
  resolveExpression(assumption.expression, undefined, inner);
1799
1689
  if (assumption.name !== undefined) {
1800
1690
  bindModuleName(
@@ -1803,25 +1693,18 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1803
1693
  inner,
1804
1694
  );
1805
1695
  }
1806
- },
1807
- })),
1808
- ...module.theorems.map((theorem) => ({
1809
- location: theorem.location,
1810
- check: (errors: SanyError[], warnings: SanyError[]) =>
1811
- resolveTheorem(
1812
- theorem,
1813
- createUnit(theorem.location, undefined, errors, warnings, theorem.name !== undefined),
1814
- ),
1815
- })),
1816
- ...module.uses.map((use) => ({
1817
- location: use.location,
1818
- check: (errors: SanyError[], warnings: SanyError[]) =>
1819
- checkFacts(
1820
- use.facts,
1821
- undefined,
1822
- createUnit(use.location, undefined, errors, warnings, false),
1823
- ),
1824
- })),
1696
+ }),
1697
+ ),
1698
+ ...module.theorems.map((theorem) =>
1699
+ createCheck(theorem.location, undefined, theorem.name !== undefined, (inner) =>
1700
+ resolveTheorem(theorem, inner),
1701
+ ),
1702
+ ),
1703
+ ...module.uses.map((use) =>
1704
+ createCheck(use.location, undefined, false, (inner) =>
1705
+ checkFacts(use.facts, undefined, inner),
1706
+ ),
1707
+ ),
1825
1708
  {
1826
1709
  location: module.location.range?.end ?? module.location,
1827
1710
  check: (errors: SanyError[]) =>