tla-ts 0.0.9 → 0.0.10-dev.b4a856304

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 (390) 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 +28 -28
  5. package/dist/deciders.js +43 -43
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +72 -10
  8. package/dist/index.d.ts +72 -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 +116 -36
  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 +128 -103
  21. package/dist/transfer.js +128 -103
  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 +7 -21
  26. package/src/bags.ts +99 -99
  27. package/src/builtins.ts +44 -75
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check.ts +576 -300
  30. package/src/cli/options.ts +11 -6
  31. package/src/cli/run.ts +2 -2
  32. package/src/cli/runtime-invariants.ts +135 -0
  33. package/src/cli/shared.ts +20 -9
  34. package/src/cli/tlapm.ts +0 -12
  35. package/src/cli/tlc-arguments.ts +646 -0
  36. package/src/cli/tlc-usage.ts +362 -0
  37. package/src/cli/tlc.ts +538 -220
  38. package/src/cli/typescript.ts +23 -0
  39. package/src/cli/xml-exporter.ts +4 -16
  40. package/src/cli.ts +0 -1
  41. package/src/codegen.ts +191 -64
  42. package/src/config.ts +140 -109
  43. package/src/coverage.ts +837 -0
  44. package/src/dependencies.ts +6 -40
  45. package/src/dfid.ts +618 -0
  46. package/src/environment.ts +240 -118
  47. package/src/errors.ts +70 -55
  48. package/src/evaluate.ts +630 -297
  49. package/src/exit-codes.ts +37 -3
  50. package/src/fingerprints.ts +10 -10
  51. package/src/index.ts +3 -0
  52. package/src/instances.ts +79 -11
  53. package/src/java-overrides.ts +2 -12
  54. package/src/json-reader.ts +456 -0
  55. package/src/json.ts +262 -139
  56. package/src/levels.ts +2 -3
  57. package/src/lexer.ts +17 -26
  58. package/src/liveness-checker.ts +10 -18
  59. package/src/liveness.ts +121 -49
  60. package/src/obligations/action.ts +4 -28
  61. package/src/obligations/anonymize.ts +84 -80
  62. package/src/obligations/equality.ts +1 -1
  63. package/src/obligations/expression.ts +2 -19
  64. package/src/obligations/format.ts +1 -9
  65. package/src/obligations/generate.ts +1 -11
  66. package/src/obligations/index.ts +0 -11
  67. package/src/obligations/levels.ts +4 -8
  68. package/src/obligations/module.ts +1 -15
  69. package/src/obligations/normalize.ts +5 -24
  70. package/src/obligations/operators.ts +1 -2
  71. package/src/obligations/pragma.ts +1 -10
  72. package/src/obligations/printer.ts +47 -46
  73. package/src/obligations/proof.ts +1 -5
  74. package/src/obligations/regroup.ts +1 -4
  75. package/src/obligations/simplify.ts +3 -14
  76. package/src/obligations/source.ts +1 -21
  77. package/src/obligations/subexpression.ts +5 -11
  78. package/src/obligations/substitution.ts +1 -3
  79. package/src/obligations/temporal.ts +8 -14
  80. package/src/obligations/tuples.ts +3 -9
  81. package/src/parallel-worker.ts +29 -39
  82. package/src/parallel.ts +29 -13
  83. package/src/parser.ts +436 -90
  84. package/src/pcal/ast.ts +7 -28
  85. package/src/pcal/parse.ts +11 -39
  86. package/src/pcal/tokenize.ts +4 -20
  87. package/src/pcal/translate.ts +10 -42
  88. package/src/pcal/validate.ts +3 -21
  89. package/src/pretty-print.ts +1 -2
  90. package/src/prover/decide-pool.ts +5 -10
  91. package/src/prover/deciders.ts +0 -4
  92. package/src/prover/first-order.ts +0 -11
  93. package/src/prover/isabelle/decide.ts +0 -6
  94. package/src/prover/isabelle/encode.ts +11 -23
  95. package/src/prover/isabelle/isar/inner-syntax.ts +108 -114
  96. package/src/prover/isabelle/isar/method.ts +25 -38
  97. package/src/prover/isabelle/isar/proof.ts +1 -10
  98. package/src/prover/isabelle/isar/statement.ts +1 -6
  99. package/src/prover/isabelle/isar/theory.ts +15 -26
  100. package/src/prover/isabelle/isar/type-check.ts +11 -25
  101. package/src/prover/isabelle/provers/blast/blast.ts +10 -41
  102. package/src/prover/isabelle/provers/blast/branch.ts +1 -32
  103. package/src/prover/isabelle/provers/blast/prove.ts +1 -35
  104. package/src/prover/isabelle/provers/blast/rules.ts +6 -44
  105. package/src/prover/isabelle/provers/blast/state.ts +2 -28
  106. package/src/prover/isabelle/provers/blast/term.ts +9 -36
  107. package/src/prover/isabelle/provers/clasimp.ts +1 -13
  108. package/src/prover/isabelle/provers/classical.ts +14 -42
  109. package/src/prover/isabelle/provers/hypsubst.ts +11 -25
  110. package/src/prover/isabelle/pure/bicompose.ts +16 -13
  111. package/src/prover/isabelle/pure/consts.ts +1 -7
  112. package/src/prover/isabelle/pure/conv.ts +1 -9
  113. package/src/prover/isabelle/pure/drule.ts +24 -40
  114. package/src/prover/isabelle/pure/envir.ts +3 -52
  115. package/src/prover/isabelle/pure/goal.ts +1 -7
  116. package/src/prover/isabelle/pure/interrupt.ts +0 -5
  117. package/src/prover/isabelle/pure/list.ts +0 -3
  118. package/src/prover/isabelle/pure/logic.ts +1 -14
  119. package/src/prover/isabelle/pure/name.ts +2 -9
  120. package/src/prover/isabelle/pure/net.ts +3 -59
  121. package/src/prover/isabelle/pure/object-logic.ts +3 -10
  122. package/src/prover/isabelle/pure/pattern.ts +2 -19
  123. package/src/prover/isabelle/pure/raw-simplifier.ts +13 -49
  124. package/src/prover/isabelle/pure/rewriter.ts +26 -58
  125. package/src/prover/isabelle/pure/search.ts +3 -10
  126. package/src/prover/isabelle/pure/seq.ts +1 -10
  127. package/src/prover/isabelle/pure/simplifier.ts +4 -12
  128. package/src/prover/isabelle/pure/table.ts +1 -31
  129. package/src/prover/isabelle/pure/tactic.ts +12 -22
  130. package/src/prover/isabelle/pure/tactical.ts +6 -12
  131. package/src/prover/isabelle/pure/term-ord.ts +2 -6
  132. package/src/prover/isabelle/pure/term.ts +2 -43
  133. package/src/prover/isabelle/pure/thm.ts +13 -36
  134. package/src/prover/isabelle/pure/type.ts +2 -15
  135. package/src/prover/isabelle/pure/unify.ts +20 -30
  136. package/src/prover/isabelle/pure/variable.ts +3 -7
  137. package/src/prover/isabelle/tla/classical-setup.ts +9 -19
  138. package/src/prover/isabelle/tla/context.ts +8 -16
  139. package/src/prover/isabelle/tla/logic.ts +4 -11
  140. package/src/prover/isabelle/tla/simpdata.ts +46 -34
  141. package/src/prover/isabelle/tools/intuitionistic.ts +1 -10
  142. package/src/prover/ocaml.ts +0 -6
  143. package/src/prover/prep/action.ts +4 -36
  144. package/src/prover/prep/enabled-axioms.ts +12 -19
  145. package/src/prover/prep/fingerprint.ts +0 -16
  146. package/src/prover/prep/level-comparison.ts +8 -12
  147. package/src/prover/prep/md5.ts +0 -2
  148. package/src/prover/prep/methods.ts +9 -36
  149. package/src/prover/prep/normalize.ts +4 -28
  150. package/src/prover/prove-worker.ts +0 -2
  151. package/src/prover/prove.ts +0 -36
  152. package/src/prover/ptl/decide.ts +7 -12
  153. package/src/prover/ptl/encode.ts +14 -36
  154. package/src/prover/ptl/sat.ts +24 -32
  155. package/src/prover/ptl/tableau.ts +12 -26
  156. package/src/prover/smt/axioms.ts +0 -7
  157. package/src/prover/smt/context.ts +19 -26
  158. package/src/prover/smt/data.ts +51 -54
  159. package/src/prover/smt/encode.ts +14 -35
  160. package/src/prover/smt/extensionality.ts +0 -10
  161. package/src/prover/smt/flatten.ts +3 -22
  162. package/src/prover/smt/format.ts +5 -10
  163. package/src/prover/smt/instances.ts +0 -10
  164. package/src/prover/smt/rewrite.ts +66 -101
  165. package/src/prover/smt/solver/act-cache.ts +0 -15
  166. package/src/prover/smt/solver/arith-rewriter.ts +3 -48
  167. package/src/prover/smt/solver/array.ts +1 -3
  168. package/src/prover/smt/solver/asserted-formulas.ts +1 -43
  169. package/src/prover/smt/solver/ast-lt.ts +1 -11
  170. package/src/prover/smt/solver/ast-util.ts +1 -6
  171. package/src/prover/smt/solver/ast.ts +1 -143
  172. package/src/prover/smt/solver/auf-solver.ts +6 -39
  173. package/src/prover/smt/solver/case-split-queue.ts +1 -9
  174. package/src/prover/smt/solver/cg-table.ts +1 -18
  175. package/src/prover/smt/solver/clause.ts +1 -27
  176. package/src/prover/smt/solver/conflict-resolution.ts +0 -9
  177. package/src/prover/smt/solver/context.ts +4 -61
  178. package/src/prover/smt/solver/ctx-simplify.ts +14 -41
  179. package/src/prover/smt/solver/der.ts +2 -21
  180. package/src/prover/smt/solver/dyn-ack.ts +1 -11
  181. package/src/prover/smt/solver/e-matching.ts +3 -34
  182. package/src/prover/smt/solver/enode.ts +1 -21
  183. package/src/prover/smt/solver/expr-ref.ts +1 -5
  184. package/src/prover/smt/solver/expr-safe-replace.ts +0 -5
  185. package/src/prover/smt/solver/fingerprints.ts +1 -12
  186. package/src/prover/smt/solver/flat-literals.ts +1 -9
  187. package/src/prover/smt/solver/goal.ts +2 -14
  188. package/src/prover/smt/solver/hash.ts +0 -8
  189. package/src/prover/smt/solver/hashtable.ts +1 -12
  190. package/src/prover/smt/solver/internalizer.ts +6 -98
  191. package/src/prover/smt/solver/lbool.ts +1 -2
  192. package/src/prover/smt/solver/literal.ts +0 -3
  193. package/src/prover/smt/solver/lp/bound-propagator.ts +8 -26
  194. package/src/prover/smt/solver/lp/constraints.ts +2 -8
  195. package/src/prover/smt/solver/lp/containers.ts +12 -30
  196. package/src/prover/smt/solver/lp/core-solver.ts +10 -31
  197. package/src/prover/smt/solver/lp/cross-nested.ts +6 -20
  198. package/src/prover/smt/solver/lp/dep-intervals.ts +7 -22
  199. package/src/prover/smt/solver/lp/dependency.ts +1 -4
  200. package/src/prover/smt/solver/lp/dioph-eq.ts +12 -98
  201. package/src/prover/smt/solver/lp/explanation.ts +1 -6
  202. package/src/prover/smt/solver/lp/gomory.ts +9 -22
  203. package/src/prover/smt/solver/lp/hnf-cutter.ts +17 -23
  204. package/src/prover/smt/solver/lp/hnf.ts +11 -26
  205. package/src/prover/smt/solver/lp/int-cube.ts +15 -36
  206. package/src/prover/smt/solver/lp/int-solver.ts +4 -34
  207. package/src/prover/smt/solver/lp/lar-solver.ts +32 -81
  208. package/src/prover/smt/solver/lp/lar-term.ts +1 -5
  209. package/src/prover/smt/solver/lp/lp-settings.ts +1 -14
  210. package/src/prover/smt/solver/lp/maximize-term.ts +20 -45
  211. package/src/prover/smt/solver/lp/nex-creator.ts +5 -28
  212. package/src/prover/smt/solver/lp/nex.ts +1 -6
  213. package/src/prover/smt/solver/lp/nla-backoff.ts +1 -4
  214. package/src/prover/smt/solver/lp/nla-basics.ts +5 -30
  215. package/src/prover/smt/solver/lp/nla-core.ts +35 -130
  216. package/src/prover/smt/solver/lp/nla-divisions.ts +32 -42
  217. package/src/prover/smt/solver/lp/nla-emonics.ts +4 -21
  218. package/src/prover/smt/solver/lp/nla-factorization.ts +1 -4
  219. package/src/prover/smt/solver/lp/nla-grobner.ts +15 -69
  220. package/src/prover/smt/solver/lp/nla-horner.ts +3 -11
  221. package/src/prover/smt/solver/lp/nla-intervals.ts +2 -12
  222. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +16 -41
  223. package/src/prover/smt/solver/lp/nla-monotone.ts +2 -6
  224. package/src/prover/smt/solver/lp/nla-order.ts +2 -13
  225. package/src/prover/smt/solver/lp/nla-patcher.ts +4 -7
  226. package/src/prover/smt/solver/lp/nla-tangents.ts +1 -9
  227. package/src/prover/smt/solver/lp/nla-throttle.ts +1 -6
  228. package/src/prover/smt/solver/lp/nla-types.ts +1 -11
  229. package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -25
  230. package/src/prover/smt/solver/lp/nra-solver.ts +5 -52
  231. package/src/prover/smt/solver/lp/numeric-pair.ts +1 -4
  232. package/src/prover/smt/solver/lp/pdd-interval.ts +1 -3
  233. package/src/prover/smt/solver/lp/pdd-simplifier.ts +2 -14
  234. package/src/prover/smt/solver/lp/pdd-solver.ts +4 -33
  235. package/src/prover/smt/solver/lp/pdd.ts +7 -70
  236. package/src/prover/smt/solver/lp/random-updater.ts +1 -8
  237. package/src/prover/smt/solver/lp/static-matrix.ts +9 -19
  238. package/src/prover/smt/solver/lp/union-find.ts +1 -6
  239. package/src/prover/smt/solver/mam-compiler.ts +14 -56
  240. package/src/prover/smt/solver/mam-interpreter.ts +1 -21
  241. package/src/prover/smt/solver/maximize-ac-sharing.ts +2 -17
  242. package/src/prover/smt/solver/model-checker.ts +2 -36
  243. package/src/prover/smt/solver/model-evaluator.ts +3 -29
  244. package/src/prover/smt/solver/model-finder.ts +3 -15
  245. package/src/prover/smt/solver/model-generator.ts +1 -12
  246. package/src/prover/smt/solver/model-macro-solver.ts +12 -69
  247. package/src/prover/smt/solver/nlsat/algebraic.ts +55 -151
  248. package/src/prover/smt/solver/nlsat/atoms.ts +5 -19
  249. package/src/prover/smt/solver/nlsat/evaluator.ts +17 -22
  250. package/src/prover/smt/solver/nlsat/explain.ts +3 -47
  251. package/src/prover/smt/solver/nlsat/interval-set.ts +9 -50
  252. package/src/prover/smt/solver/nlsat/levelwise.ts +48 -129
  253. package/src/prover/smt/solver/nlsat/mpbq.ts +3 -14
  254. package/src/prover/smt/solver/nlsat/polynomial.ts +17 -156
  255. package/src/prover/smt/solver/nlsat/solver.ts +7 -67
  256. package/src/prover/smt/solver/nlsat/types.ts +1 -8
  257. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +1 -44
  258. package/src/prover/smt/solver/nlsat/upolynomial.ts +11 -92
  259. package/src/prover/smt/solver/nnf.ts +3 -38
  260. package/src/prover/smt/solver/pattern-inference.ts +2 -15
  261. package/src/prover/smt/solver/poly-rewriter.ts +3 -23
  262. package/src/prover/smt/solver/propagate-values.ts +1 -10
  263. package/src/prover/smt/solver/proto-model.ts +3 -48
  264. package/src/prover/smt/solver/pull-quant.ts +1 -12
  265. package/src/prover/smt/solver/qe-lite-der.ts +1 -29
  266. package/src/prover/smt/solver/qe-lite-fm.ts +2 -25
  267. package/src/prover/smt/solver/qe-lite.ts +3 -33
  268. package/src/prover/smt/solver/qi-queue.ts +1 -16
  269. package/src/prover/smt/solver/quant-tactics.ts +5 -22
  270. package/src/prover/smt/solver/quantifier-analyzer.ts +17 -37
  271. package/src/prover/smt/solver/quantifier-manager.ts +1 -8
  272. package/src/prover/smt/solver/random-gen.ts +1 -2
  273. package/src/prover/smt/solver/rational.ts +1 -8
  274. package/src/prover/smt/solver/ref-vector.ts +1 -6
  275. package/src/prover/smt/solver/relevancy.ts +2 -15
  276. package/src/prover/smt/solver/rewriter-tpl.ts +3 -147
  277. package/src/prover/smt/solver/rewriter.ts +3 -85
  278. package/src/prover/smt/solver/rlimit.ts +2 -20
  279. package/src/prover/smt/solver/smtlib-parser.ts +2 -37
  280. package/src/prover/smt/solver/solver.ts +23 -34
  281. package/src/prover/smt/solver/sort.ts +1 -5
  282. package/src/prover/smt/solver/sticky-generations.ts +0 -5
  283. package/src/prover/smt/solver/theory-lra-axioms.ts +3 -24
  284. package/src/prover/smt/solver/theory-lra-bounds.ts +0 -5
  285. package/src/prover/smt/solver/theory-lra-final-check.ts +4 -22
  286. package/src/prover/smt/solver/theory-lra-internalize.ts +1 -27
  287. package/src/prover/smt/solver/theory-lra-model.ts +4 -19
  288. package/src/prover/smt/solver/theory-lra-propagate.ts +1 -15
  289. package/src/prover/smt/solver/theory-lra-state.ts +3 -8
  290. package/src/prover/smt/solver/theory-lra.ts +1 -8
  291. package/src/prover/smt/solver/var-subst.ts +2 -26
  292. package/src/prover/smt/table.ts +0 -8
  293. package/src/prover/tlapm/cache.ts +20 -25
  294. package/src/prover/tlapm/ocaml-marshal.ts +19 -21
  295. package/src/prover/tlapm/tlapm.ts +12 -17
  296. package/src/prover/verdict.ts +0 -2
  297. package/src/prover/zenon/check/check.ts +0 -9
  298. package/src/prover/zenon/check/definitions.ts +5 -15
  299. package/src/prover/zenon/check/equality.ts +0 -8
  300. package/src/prover/zenon/check/functions.ts +0 -4
  301. package/src/prover/zenon/check/keys.ts +0 -7
  302. package/src/prover/zenon/check/propositional.ts +0 -3
  303. package/src/prover/zenon/check/records.ts +0 -5
  304. package/src/prover/zenon/check/relations.ts +0 -13
  305. package/src/prover/zenon/check/rewrites.ts +0 -6
  306. package/src/prover/zenon/check/sets.ts +0 -3
  307. package/src/prover/zenon/check/terms.ts +0 -17
  308. package/src/prover/zenon/check/tla.ts +0 -6
  309. package/src/prover/zenon/decide.ts +0 -12
  310. package/src/prover/zenon/encode.ts +1 -8
  311. package/src/prover/zenon/eqrel.ts +0 -13
  312. package/src/prover/zenon/equality-rules.ts +0 -4
  313. package/src/prover/zenon/expr.ts +1 -26
  314. package/src/prover/zenon/index-table.ts +17 -38
  315. package/src/prover/zenon/isar-case.ts +9 -19
  316. package/src/prover/zenon/isar.ts +1 -29
  317. package/src/prover/zenon/limits.ts +0 -4
  318. package/src/prover/zenon/llproof.ts +0 -9
  319. package/src/prover/zenon/mltoll.ts +54 -76
  320. package/src/prover/zenon/node.ts +1 -11
  321. package/src/prover/zenon/ocaml.ts +1 -17
  322. package/src/prover/zenon/parse.ts +2 -7
  323. package/src/prover/zenon/phrase.ts +1 -11
  324. package/src/prover/zenon/proof.ts +0 -9
  325. package/src/prover/zenon/propositional-rules.ts +0 -5
  326. package/src/prover/zenon/rules.ts +2 -6
  327. package/src/prover/zenon/search.ts +1 -17
  328. package/src/prover/zenon/tla/functions.ts +57 -29
  329. package/src/prover/zenon/tla/llproof.ts +63 -70
  330. package/src/prover/zenon/tla/records.ts +56 -27
  331. package/src/prover/zenon/tla/rewrites.ts +12 -19
  332. package/src/prover/zenon/tla/sets.ts +68 -39
  333. package/src/prover/zenon/tla/terms.ts +0 -6
  334. package/src/prover/zenon/tla/tla.ts +59 -58
  335. package/src/randomization.ts +84 -35
  336. package/src/resolve.ts +18 -60
  337. package/src/runtime.ts +11 -4
  338. package/src/sany/builtins.ts +3 -4
  339. package/src/sany/generator.ts +119 -169
  340. package/src/sany/levels.ts +6 -30
  341. package/src/sany/lexer.ts +7 -23
  342. package/src/sany/linter.ts +1 -8
  343. package/src/sany/modules.ts +1 -5
  344. package/src/sany/nodes.ts +0 -31
  345. package/src/sany/parse-error.ts +39 -117
  346. package/src/sany/report.ts +29 -63
  347. package/src/sany/selectors.ts +15 -39
  348. package/src/sany/source.ts +1 -17
  349. package/src/sany/xml.ts +13 -63
  350. package/src/search.ts +494 -154
  351. package/src/simulate.ts +694 -0
  352. package/src/spec-description.ts +166 -48
  353. package/src/standard-definitions.ts +2 -3
  354. package/src/standard-modules.ts +264 -68
  355. package/src/state-graph.ts +51 -18
  356. package/src/subexpressions.ts +31 -5
  357. package/src/substitution-levels.ts +2 -3
  358. package/src/successors.ts +267 -77
  359. package/src/symmetry.ts +31 -8
  360. package/src/tableau.ts +27 -24
  361. package/src/tlc/fingerprint.ts +183 -61
  362. package/src/tlc/jar-data.ts +113 -6
  363. package/src/tlc/messages.ts +122 -37
  364. package/src/tlc/order.ts +33 -38
  365. package/src/tlc/random.ts +16 -16
  366. package/src/tlc/replay.ts +563 -211
  367. package/src/tlc/summary.ts +2 -4
  368. package/src/tlc/tlc-operators.ts +81 -36
  369. package/src/tlc/tlcext-operators.ts +138 -117
  370. package/src/tlc/trace-spec.ts +273 -0
  371. package/src/tlc/value-stream.ts +143 -0
  372. package/src/tlc/version.ts +2 -4
  373. package/src/transfer.ts +22 -25
  374. package/src/typescript/actions.ts +481 -0
  375. package/src/typescript/collections.ts +600 -0
  376. package/src/typescript/compile.ts +65 -0
  377. package/src/typescript/context.ts +94 -0
  378. package/src/typescript/emit.ts +121 -0
  379. package/src/typescript/expressions.ts +496 -0
  380. package/src/typescript/formula.ts +152 -0
  381. package/src/typescript/functions.ts +284 -0
  382. package/src/typescript/model.ts +163 -0
  383. package/src/typescript/names.ts +114 -0
  384. package/src/typescript/program.ts +48 -0
  385. package/src/typescript/properties.ts +139 -0
  386. package/src/typescript/scope.ts +112 -0
  387. package/src/typescript/steps.ts +165 -0
  388. package/src/typescript/types.ts +171 -0
  389. package/src/values.ts +826 -276
  390. package/src/worker.ts +7 -8
@@ -9,7 +9,7 @@ import {
9
9
  type OperatorParam,
10
10
  type SourceLocation,
11
11
  } from "./ast";
12
- import { BUNDLED_MODULES } from "./bundled-modules";
12
+ import type { ModuleOperator } from "./check";
13
13
  import type { ConfigValue, ModelConfig } from "./config";
14
14
  import type { FileSystem } from "./json";
15
15
  import {
@@ -19,17 +19,23 @@ import {
19
19
  formatSanyWarnings,
20
20
  TlaConfigError,
21
21
  TlaEvaluationError,
22
+ TlaFeatureUnsupportedError,
22
23
  TlaModuleError,
23
24
  TlaSyntaxError,
25
+ UNEXPECTED_EXCEPTION,
24
26
  type SanyError,
25
27
  } from "./errors";
26
28
  import { EXIT_STATUS, setExitCode } from "./exit-codes";
27
29
  import {
30
+ bindOperatorArguments,
28
31
  createBigNumberError,
29
32
  createRealNumberError,
30
33
  evaluate,
31
34
  getDefinition,
32
35
  type Builtin,
36
+ type BuiltinArgument,
37
+ type Closure,
38
+ type Context,
33
39
  type Environment,
34
40
  } from "./evaluate";
35
41
  import {
@@ -60,10 +66,10 @@ import {
60
66
  } from "./resolve";
61
67
  import { createRuntime } from "./runtime";
62
68
  import { getSanyStandardModules } from "./sany/modules";
63
- import { USER_STANDARD_MODULES } from "./tlc/jar-data";
64
69
  import { getSanyParseError } from "./sany/parse-error";
65
70
  import {
66
71
  CORE_OPERATORS,
72
+ getJavaOverrides,
67
73
  getStandardModule,
68
74
  getTraceSpecArithmetic,
69
75
  STANDARD_DEPENDENCIES,
@@ -78,9 +84,9 @@ import { createTlcOrder, type TlcOrder } from "./tlc/order";
78
84
  import {
79
85
  isTlcInteger,
80
86
  createModelValue,
81
- createSet,
82
87
  formatValue,
83
88
  valuesEqual,
89
+ toFiniteSet,
84
90
  type Value,
85
91
  markNormalized,
86
92
  } from "./values";
@@ -89,34 +95,32 @@ import type { TlcVersion } from "./tlc/version";
89
95
  interface EnvironmentOptions {
90
96
  source: string;
91
97
  loadModule: ((name: string) => string | undefined) | undefined;
92
- // The TLC orders of the same spec, when the caller already reads them.
93
98
  order?: TlcOrder;
94
99
  loadClass?: (name: string) => Uint8Array | undefined;
95
100
  print: (text: string) => void;
96
101
  files: FileSystem;
97
102
  seed?: bigint;
98
- // Links the modules without a model, as SANY does, so a declared constant needs no value.
99
- isUnconfigured?: boolean;
100
103
  isTool?: boolean;
101
- // TLC loads Integers for the binary trace of its trace exploration spec, and Integers' Java
102
- // methods then compute the arithmetic of Naturals.
103
- isTraceRecorded?: boolean;
104
+ traceFile?: string;
104
105
  isSanyReported?: boolean;
105
106
  isWarningEnabled?: boolean;
106
107
  version?: TlcVersion;
108
+ moduleOperators?: ModuleOperator[];
107
109
  }
108
110
 
109
- interface Specification {
111
+ export interface ResolvedModuleOperator extends ModuleOperator {
112
+ key: string | undefined;
113
+ }
114
+
115
+ export interface Specification {
110
116
  environment: Environment;
111
117
  assumptions: Assumption[];
112
118
  variableLocations: SourceLocation[];
113
119
  instanceActions: Map<string, InstanceAction>;
114
- // The definitions TLC computes when it starts, in the order it computes them.
115
120
  startupDefinitions: Definition[];
116
- rootConstants: Declaration[];
117
- rootSymbols: Symbols;
118
- // The methods of the modules' classes, in the order TLC loads the classes.
119
121
  javaOverrides: JavaOverride[];
122
+ moduleOperators: ResolvedModuleOperator[];
123
+ rootName: string;
120
124
  }
121
125
 
122
126
  export interface InstanceAction {
@@ -124,13 +128,15 @@ export interface InstanceAction {
124
128
  location: SourceLocation;
125
129
  }
126
130
 
131
+ const ANNOTATED_OVERRIDE_MODULES = new Set(["IOUtils", "Json", "TLCExt"]);
132
+
127
133
  const toValue = (value: ConfigValue): Value => {
128
134
  switch (value.kind) {
129
135
  case "Number":
130
- // TLC fails with an unexpected exception on a configuration number too big for it.
136
+ // Deviation from TLC: ModelConfig#parseValue lets Integer.parseInt fail on a number too big, where tla-ts reports it as TLC reports such a number in a module.
131
137
  if (!isTlcInteger(value.value)) {
132
138
  throw setExitCode(
133
- new TlaModuleError(`Integer overflow: ${value.value} is outside TLC's 32-bit range`),
139
+ new TlaModuleError(`TLC can't handle a number this big.\n${value.value}`),
134
140
  EXIT_STATUS.error,
135
141
  );
136
142
  }
@@ -141,7 +147,25 @@ const toValue = (value: ConfigValue): Value => {
141
147
  case "ModelValue":
142
148
  return createModelValue(value.name);
143
149
  case "Set":
144
- return createSet(value.elements.map(toValue));
150
+ return toFiniteSet({ kind: "MixedSet", elements: value.elements.map(toValue) });
151
+ }
152
+ };
153
+
154
+ const toConstantValue = (value: ConfigValue) => {
155
+ try {
156
+ return toValue(value);
157
+ } catch (error) {
158
+ if (
159
+ !(error instanceof TlaEvaluationError) ||
160
+ error instanceof TlaModuleError ||
161
+ error instanceof TlaFeatureUnsupportedError
162
+ ) {
163
+ throw error;
164
+ }
165
+ throw setExitCode(
166
+ new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`),
167
+ EXIT_STATUS.error,
168
+ );
145
169
  }
146
170
  };
147
171
 
@@ -157,8 +181,6 @@ interface LinkedModule {
157
181
  facts: Set<string>;
158
182
  }
159
183
 
160
- // errors and warnings collect what SANY reports for the module, and levelChecks the level checks of
161
- // the modules in its file, which SANY runs only when it reports no error.
162
184
  interface ModuleScope {
163
185
  id: string;
164
186
  symbols: Symbols;
@@ -178,8 +200,6 @@ const getDeclarations = (linked: LinkedModule): Declaration[] => [
178
200
  })),
179
201
  ];
180
202
 
181
- // Parses the module SANY reads from the file name.tla. A module SANY's parser rejects fails as it
182
- // does in TLC: SANY's message, then a fatal error that names the module.
183
203
  const parseModuleFile = (source: string, name: string, order: TlcOrder) => {
184
204
  try {
185
205
  return order.parseModule(source);
@@ -212,12 +232,9 @@ const getModuleRedefinition = (module: Module, existing: Module): SanyError | un
212
232
  message: `Multiply-defined module '${module.name}': this definition or declaration conflicts \nwith the one at ${formatRange(existing.location.range)}.`,
213
233
  };
214
234
 
215
- // Context.kindOfNode names a definition or a parameter a definition, and any other symbol a
216
- // declaration.
217
235
  const getNodeWord = (kind: SymbolKind) =>
218
236
  ["Builtin", "Definition", "Instance", "Parameter"].includes(kind) ? "definition" : "declaration";
219
237
 
220
- // The Java class of the node SANY binds a symbol to.
221
238
  const getNodeClass = (kind: SymbolKind) =>
222
239
  kind === "Theorem" ? "ThmOrAssumpDefNode" : getNodeWord(kind);
223
240
 
@@ -306,9 +323,9 @@ export const createEnvironment = (
306
323
  const root = order.parseModule(options.source);
307
324
  const definitions = new Map<string, Definition>();
308
325
  const parameterized = new Map<string, boolean>();
309
- // The definition each instance copy came from, and the instance parameters the copy adds.
310
326
  const copySources = new Map<string, { source: string; prefix: number }>();
311
327
  const substitutedDefinitions = new Set<Definition>();
328
+ const instanceParams = new Set<OperatorParam>();
312
329
  const builtins = new Map<string, Builtin>(
313
330
  CORE_OPERATORS.map((operator) => [operator.name, operator]),
314
331
  );
@@ -318,13 +335,10 @@ export const createEnvironment = (
318
335
  runtime.isTool = options.isTool ?? false;
319
336
  runtime.version = options.version ?? "master";
320
337
  const linkedModules = new Map<string, LinkedModule>();
321
- const nestedModules = new Map<string, LinkedModule>();
322
- // Each module by name, from the time its level checks run.
323
338
  const checkedModules = new Map<string, LinkedModule>();
324
339
  const linkOrder: LinkedModule[] = [];
325
340
  const dependencies = new Map<string, Set<string>>();
326
341
  const loading: Set<string>[] = [];
327
- // The top-level modules being linked, to report a cycle of EXTENDS or INSTANCE.
328
342
  const linking: string[] = [];
329
343
 
330
344
  const useModule = (name: string, used: Iterable<string>) =>
@@ -332,8 +346,8 @@ export const createEnvironment = (
332
346
  closure.add(name);
333
347
  [...used].forEach((dependency) => closure.add(dependency));
334
348
  });
335
- const copyCount = { value: 0 };
336
- const createName = (name: string) => `${name}#${copyCount.value++}`;
349
+ let copyCount = 0;
350
+ const createName = (name: string) => `${name}#${copyCount++}`;
337
351
  const instanceChecks: InstanceCheck[] = [];
338
352
  const warnings: SanyError[] = [];
339
353
  const checkInstance = createInstanceChecker((name) => {
@@ -360,8 +374,6 @@ export const createEnvironment = (
360
374
  if (cached) return cached;
361
375
  const standardModule = getStandardModule(name, options.print, runtime, options.files, builtins);
362
376
  if (!standardModule) return undefined;
363
- // TLC loads the modules a standard module instantiates too, and the Java methods of each
364
- // replace the operators of every module, as those of Integers replace those of Naturals.
365
377
  STANDARD_DEPENDENCIES.get(name)?.forEach(addStandardModule);
366
378
  const exported: Symbols = new Map();
367
379
  standardModule.extends.forEach((extended) =>
@@ -384,19 +396,18 @@ export const createEnvironment = (
384
396
  };
385
397
 
386
398
  const getStandardSymbols = (name: string) => {
387
- // SANY reads a module from the user's directories before its standard modules.
388
- if (USER_STANDARD_MODULES.has(name) && options.loadModule?.(name) !== undefined)
399
+ const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
400
+ if (
401
+ name === "TLCExt" ||
402
+ [name, ...dependencies].some((module) => options.loadModule?.(module) !== undefined)
403
+ )
389
404
  return undefined;
390
405
  const symbols = addStandardModule(name);
391
- const dependencies = STANDARD_DEPENDENCIES.get(name) ?? [];
392
406
  if (symbols) useModule(name, dependencies);
393
- // TLC loads the Java class of each module a standard module uses, so one that uses Integers
394
- // overrides the arithmetic of Naturals.
395
407
  if (symbols && dependencies.includes("Integers")) addStandardModule("Integers");
396
408
  return symbols;
397
409
  };
398
410
 
399
- // Like TLC, reject a decimal or a big integer anywhere in a module, even where nothing evaluates it.
400
411
  const rejectModuleLiterals = (module: Module): Module => {
401
412
  rejectLiterals([
402
413
  ...module.definitions.flatMap((definition) =>
@@ -418,7 +429,7 @@ export const createEnvironment = (
418
429
  const loadModule = (name: string, importer: string) => {
419
430
  const source =
420
431
  options.loadModule?.(name) ??
421
- BUNDLED_MODULES.get(name) ??
432
+ getSanyStandardModules(runtime.version).get(name) ??
422
433
  extractModule(options.source, name);
423
434
  if (source === undefined) throw createMissingModuleError(name, importer);
424
435
  const module = parseModuleFile(source, name, order);
@@ -478,14 +489,49 @@ export const createEnvironment = (
478
489
  return cached;
479
490
  }
480
491
  const top = linkTopLevel(loadModule(name, importer));
481
- // TLC replaces each definition of a module that has a standard module's name with the Java
482
- // method of that name, so a user's Bags.tla keeps the definitions that Bags has no method for.
483
- const standardModule = getSanyStandardModules(runtime.version).has(name)
484
- ? getStandardModule(name, options.print, runtime, options.files, builtins)
485
- : undefined;
486
- standardModule?.operators.forEach((operator) => {
492
+ const replaced = new Map<string, Definition>();
493
+ const evaluateReplaced = (
494
+ operator: string,
495
+ args: BuiltinArgument[],
496
+ context: Context | undefined,
497
+ ) => {
498
+ const definition = replaced.get(operator);
499
+ const expressions = args.flatMap((arg) =>
500
+ typeof arg === "object" && arg.kind === "Operator" && arg.expression
501
+ ? [arg.expression]
502
+ : [],
503
+ );
504
+ if (definition?.kind !== "Operator" || !context || expressions.length !== args.length) {
505
+ throw new TlaEvaluationError(`No definition of ${operator} to fall back to`);
506
+ }
507
+ const closure: Closure = {
508
+ kind: "Closure",
509
+ name: definition.name,
510
+ params: definition.params,
511
+ body: definition.body,
512
+ scope: undefined,
513
+ };
514
+ return evaluate(definition.body, {
515
+ environment: context.environment,
516
+ scope: bindOperatorArguments(closure, expressions, context),
517
+ state: context.state,
518
+ next: context.next,
519
+ isKeepLazy: context.isKeepLazy,
520
+ });
521
+ };
522
+ getJavaOverrides(
523
+ name,
524
+ options.print,
525
+ runtime,
526
+ options.files,
527
+ builtins,
528
+ evaluateReplaced,
529
+ )?.forEach((operator) => {
487
530
  const symbol = top.linked.visible.get(operator.name);
488
- if (symbol?.kind !== "Definition" || symbol.arity !== operator.arity) return;
531
+ if (symbol?.kind !== "Definition") return;
532
+ if (symbol.arity !== operator.arity && ANNOTATED_OVERRIDE_MODULES.has(name)) return;
533
+ const definition = definitions.get(symbol.key);
534
+ if (definition) replaced.set(operator.name, definition);
489
535
  builtins.set(symbol.key, operator);
490
536
  definitions.delete(symbol.key);
491
537
  });
@@ -509,7 +555,7 @@ export const createEnvironment = (
509
555
  unit.facts.add(instance.name === undefined ? name : `${instance.name}!${name}`),
510
556
  );
511
557
  const declarations = getDeclarations(linked);
512
- const suffix = `#${copyCount.value++}`;
558
+ const suffix = `#${copyCount++}`;
513
559
  const renamed = new Map(instance.params.map((param) => [param.name, `${param.name}${suffix}`]));
514
560
  const substitutions = resolveSubstitutions(
515
561
  resolveInstanceSubexpressions(instance, module),
@@ -524,13 +570,23 @@ export const createEnvironment = (
524
570
  substitutions,
525
571
  location: instance.location,
526
572
  });
573
+ const params = instance.params.map((param) => ({ ...param, name: `${param.name}${suffix}` }));
574
+ params.forEach((param) => instanceParams.add(param));
575
+ const substitutionRange = instance.definitionRange
576
+ ? {
577
+ ...instance.definitionRange,
578
+ start: { line: instance.location.line, column: instance.location.column },
579
+ }
580
+ : instance.instanceRange;
527
581
  const instantiation = createInstantiation({
528
- params: instance.params.map((param) => ({ ...param, name: `${param.name}${suffix}` })),
582
+ params,
529
583
  substitutions: new Map(
530
584
  [...substitutions].map(([name, expression]) => [name, renameLocals(expression, renamed)]),
531
585
  ),
532
586
  variables: new Set(linked.variables),
533
587
  getDefinition: (key) => definitions.get(key),
588
+ getOuterDefinition: (name) =>
589
+ module.definitions.find((definition) => definition.name === name),
534
590
  isParameterized: (key) => parameterized.get(key) === true,
535
591
  addDefinition: (source, name, create) => {
536
592
  const key = createName(`${name}@${module.name}`);
@@ -545,6 +601,13 @@ export const createEnvironment = (
545
601
  return key;
546
602
  },
547
603
  createName,
604
+ ...(declarations.length > 0 && {
605
+ frame: {
606
+ kind: "Identifier",
607
+ name: instance.name ?? instance.module,
608
+ location: { ...substitutionRange.start, range: substitutionRange },
609
+ },
610
+ }),
548
611
  });
549
612
  return getInstanceSymbols(instance, linked.exported, declarations, (key, name) => {
550
613
  const copy =
@@ -567,9 +630,6 @@ export const createEnvironment = (
567
630
  ]),
568
631
  );
569
632
 
570
- // The names an INSTANCE adds for the definitions of the module it instantiates, each with the key
571
- // that getKey gives it. SANY binds a copy of a definition that depends on the parameters at the
572
- // INSTANCE.
573
633
  const getInstanceSymbols = (
574
634
  instance: Instance,
575
635
  exported: Symbols,
@@ -636,7 +696,7 @@ export const createEnvironment = (
636
696
  });
637
697
 
638
698
  const createArgumentParams = (arity: number) =>
639
- Array.from({ length: arity }, (_, index) => ({ name: `argument${index}`, arity: 0 }));
699
+ Array.from({ length: arity }, (_value, index) => ({ name: `argument${index}`, arity: 0 }));
640
700
 
641
701
  const getSubstitutionBody = (
642
702
  replacement: Expression,
@@ -685,7 +745,7 @@ export const createEnvironment = (
685
745
  });
686
746
  }
687
747
  const linked = nested ?? getLinkedModule(instance.module, instancer);
688
- const suffix = `#${copyCount.value++}`;
748
+ const suffix = `#${copyCount++}`;
689
749
  const explicitSubstitutions = new Map(
690
750
  instance.substitutions.map((item) => [item.name, item.expression]),
691
751
  );
@@ -742,6 +802,7 @@ export const createEnvironment = (
742
802
  ),
743
803
  variables: new Set(linked.variables),
744
804
  getDefinition: (key) => definitions.get(key),
805
+ getOuterDefinition: () => undefined,
745
806
  isParameterized: (key) => parameterized.get(key) === true,
746
807
  addDefinition: (_source, name, create) => {
747
808
  const key = `${name}${KEY_SEPARATOR}${instance.module}${suffix}.${copies.length}`;
@@ -796,12 +857,9 @@ export const createEnvironment = (
796
857
  ]),
797
858
  ...scope.symbols,
798
859
  ]);
799
- // Where an INSTANCE first made each name it adds visible.
800
860
  const importedAt = new Map<string, SourceLocation>();
801
861
  const extendsReport: ModuleReport = { location: MODULE_START, errors: [], warnings: [] };
802
862
  const reports = [extendsReport];
803
- // Context.mergeExtendContext keeps the first of the symbols the extended modules share by name,
804
- // and warns of two definitions or two declarations but rejects a definition and a declaration.
805
863
  const mergeExtended = (symbols: Symbols) =>
806
864
  symbols.forEach((symbol, name) => {
807
865
  const current = exported.get(name);
@@ -836,8 +894,12 @@ export const createEnvironment = (
836
894
  const modules = new Map<string, LinkedModule>();
837
895
  const extended: LinkedModule[] = [];
838
896
  const constants = [...parsed.constants];
839
- const variables = parsed.variables.map((variable) => variable.name);
840
- const variableLocations = parsed.variables.map((variable) => variable.location);
897
+ const declaredVariables = parsed.variables.filter(
898
+ (variable, index) =>
899
+ parsed.variables.findIndex((other) => other.name === variable.name) === index,
900
+ );
901
+ const variables = declaredVariables.map((variable) => variable.name);
902
+ const variableLocations = declaredVariables.map((variable) => variable.location);
841
903
  parsed.extends.forEach((name) => {
842
904
  const nested = scope.modules.get(name);
843
905
  const standardSymbols = nested ? undefined : getStandardSymbols(name);
@@ -847,12 +909,16 @@ export const createEnvironment = (
847
909
  }
848
910
  const linked = nested ?? getLinkedModule(name, parsed.name);
849
911
  constants.push(
850
- ...linked.constants.filter((constant) =>
851
- constants.every((existing) => existing.name !== constant.name),
912
+ ...linked.constants.filter(
913
+ (constant) =>
914
+ constants.every((existing) => existing.name !== constant.name) &&
915
+ !variables.includes(constant.name),
852
916
  ),
853
917
  );
854
918
  linked.variables.forEach((variable, index) => {
855
- if (variables.includes(variable)) return;
919
+ const isDeclared =
920
+ variables.includes(variable) || constants.some((constant) => constant.name === variable);
921
+ if (isDeclared) return;
856
922
  variables.push(variable);
857
923
  variableLocations.push(linked.variableLocations[index]);
858
924
  });
@@ -887,14 +953,12 @@ export const createEnvironment = (
887
953
  parsed === root ? definition.name : `${definition.name}${KEY_SEPARATOR}${scope.id}`;
888
954
  const instanced = new Map<Instance, Symbols>();
889
955
  const getVisibleModules = () => new Map([...scope.modules, ...modules]);
890
- const isInstancedAfterDefinition = (nested: Module) => {
891
- const first = parsed.instances.find((instance) => instance.module === nested.name);
892
- return first !== undefined && isBefore(nested.location, first.location);
893
- };
894
956
  const getLetModule = (instance: Instance) => {
895
957
  const ownModule = parsed.modules.find((nested) => nested.name === instance.module);
896
958
  if (!ownModule) return getVisibleModules().get(instance.module);
897
- return isInstancedAfterDefinition(ownModule) ? modules.get(ownModule.name) : undefined;
959
+ return isBefore(ownModule.location, instance.location)
960
+ ? modules.get(ownModule.name)
961
+ : undefined;
898
962
  };
899
963
  const getUnit = (module: Module): ModuleUnit => ({
900
964
  imported: visible,
@@ -927,7 +991,6 @@ export const createEnvironment = (
927
991
  reports,
928
992
  warnings: scope.warnings,
929
993
  });
930
- // SANY keeps the first of two symbols of a name.
931
994
  const getSymbolsBefore = (location: SourceLocation): Symbols =>
932
995
  [
933
996
  ...declarations
@@ -946,8 +1009,6 @@ export const createEnvironment = (
946
1009
  (symbols, [name, symbol]) => (symbols.has(name) ? symbols : symbols.set(name, symbol)),
947
1010
  new Map(visible),
948
1011
  );
949
- // Generator.generateModule reports the errors of a nested module and goes on, and then
950
- // SymbolTable.addModule reports a module already defined.
951
1012
  const linkNested = (nested: Module) => {
952
1013
  const visibleModules = getVisibleModules();
953
1014
  const existing = visibleModules.get(nested.name);
@@ -967,7 +1028,6 @@ export const createEnvironment = (
967
1028
  });
968
1029
  if (redefinition) report.errors.push(redefinition);
969
1030
  if (!existing) modules.set(nested.name, linked);
970
- if (!nestedModules.has(nested.name)) nestedModules.set(nested.name, linked);
971
1031
  };
972
1032
  [
973
1033
  ...parsed.instances.map((instance) => ({
@@ -1009,7 +1069,6 @@ export const createEnvironment = (
1009
1069
  parameterized.set(key, !isParameterFree);
1010
1070
  const symbol = getDefinitionSymbol(definition);
1011
1071
  const importedLocation = importedAt.get(definition.name);
1012
- // SANY binds the name of a fact to a fact the module imports before it.
1013
1072
  if (
1014
1073
  symbol.kind === "Theorem" &&
1015
1074
  visible.get(definition.name)?.kind === "Theorem" &&
@@ -1035,8 +1094,6 @@ export const createEnvironment = (
1035
1094
  ]),
1036
1095
  };
1037
1096
  if (!checkedModules.has(parsed.name)) checkedModules.set(parsed.name, linked);
1038
- // SANY checks the instances of a module with its definitions and theorems, so it reports their
1039
- // errors together.
1040
1097
  const checks = instanceChecks
1041
1098
  .splice(firstCheck)
1042
1099
  .map((check) => ({ location: check.location, check: () => checkInstance(check) }));
@@ -1058,12 +1115,37 @@ export const createEnvironment = (
1058
1115
  };
1059
1116
 
1060
1117
  const linkedRoot = linkTopLevel(root).linked;
1118
+ const traceFile = options.traceFile;
1119
+ const traceOperators: ModuleOperator[] =
1120
+ traceFile === undefined
1121
+ ? []
1122
+ : [
1123
+ {
1124
+ kind: "postcondition",
1125
+ module: "_TLCTrace",
1126
+ operator: "_TLCTraceSilent",
1127
+ constants: { _TLCTraceFile: traceFile },
1128
+ },
1129
+ ];
1130
+ const moduleOperators = [...(options.moduleOperators ?? []), ...traceOperators].map(
1131
+ (operator): ResolvedModuleOperator => {
1132
+ const symbol = getLinkedModule(operator.module, root.name).visible.get(operator.operator);
1133
+ return { ...operator, key: symbol?.kind === "Definition" ? symbol.key : undefined };
1134
+ },
1135
+ );
1136
+ linkOrder.push(...linkOrder.splice(linkOrder.indexOf(linkedRoot), 1));
1061
1137
  if (warnings.length > 0 && !options.isSanyReported) options.print(formatSanyWarnings(warnings));
1062
1138
  const getRootKey = (name: string) => linkedRoot.visible.get(name)?.key ?? name;
1063
1139
  linkedRoot.visible.forEach((symbol, name) => {
1064
1140
  const definition = definitions.get(symbol.key);
1065
1141
  if (definition && symbol.key !== name) definitions.set(name, definition);
1066
1142
  });
1143
+ moduleOperators.forEach((operator) => {
1144
+ const definition = operator.key === undefined ? undefined : definitions.get(operator.key);
1145
+ if (operator.kind !== "postcondition" && definition) {
1146
+ definitions.set(operator.operator, definition);
1147
+ }
1148
+ });
1067
1149
 
1068
1150
  const assumptions: Assumption[] = [];
1069
1151
  const addAssumptions = (linked: LinkedModule, visited: Set<LinkedModule>) => {
@@ -1076,17 +1158,29 @@ export const createEnvironment = (
1076
1158
  const declaredConstants = linkedRoot.constants.map((constant) => constant.name);
1077
1159
  const variables = linkedRoot.variables;
1078
1160
 
1079
- const getScopedKeys = (item: { name: string; module?: string }) => {
1161
+ const moduleSymbols = new Map([
1162
+ ...standardExports,
1163
+ ...[...linkedModules].map(([name, linked]): [string, Symbols] => [name, linked.visible]),
1164
+ [root.name, linkedRoot.visible],
1165
+ ]);
1166
+ const getScopedSymbol = (item: { name: string; module?: string }) => {
1167
+ const symbol = moduleSymbols.get(item.module ?? "")?.get(item.name);
1168
+ return symbol?.kind === "Definition" ? symbol : undefined;
1169
+ };
1170
+ const getSubstitutedKeys = (item: { name: string; module?: string }) => {
1080
1171
  if (item.module === undefined) return [item.name, getRootKey(item.name)];
1081
- if (item.module === root.name || declaredConstants.includes(item.name)) return [];
1082
- const linked = linkedModules.get(item.module) ?? nestedModules.get(item.module);
1083
- return [linked?.visible.get(item.name)?.key ?? item.name];
1172
+ const symbol = getScopedSymbol(item);
1173
+ return symbol ? [symbol.key] : [];
1084
1174
  };
1175
+ const getScopedKeys = (item: { name: string; module?: string }) =>
1176
+ item.module === root.name ? [] : getSubstitutedKeys(item);
1085
1177
  const getKeyArity = (key: string) => {
1086
1178
  const definition = definitions.get(key);
1087
1179
  return definition?.kind === "Operator"
1088
1180
  ? definition.params.length
1089
- : (linkedRoot.constants.find((constant) => constant.name === key)?.arity ?? 0);
1181
+ : (linkedRoot.constants.find((constant) => constant.name === key)?.arity ??
1182
+ builtins.get(key)?.arity ??
1183
+ 0);
1090
1184
  };
1091
1185
  const constants = new Map<string, Value>();
1092
1186
  const operatorKeys = new Set<string>();
@@ -1099,7 +1193,7 @@ export const createEnvironment = (
1099
1193
  config.constants
1100
1194
  .filter((assignment) => assignment.args === undefined)
1101
1195
  .forEach((assignment) => {
1102
- const value = toValue(assignment.value);
1196
+ const value = toConstantValue(assignment.value);
1103
1197
  markNormalized(value);
1104
1198
  getScopedKeys(assignment).forEach((key) => {
1105
1199
  const arity = getKeyArity(key);
@@ -1135,20 +1229,38 @@ export const createEnvironment = (
1135
1229
  });
1136
1230
  tables.forEach((table) =>
1137
1231
  getScopedKeys(table).forEach((key) =>
1138
- setOperator(
1139
- key,
1140
- createOperatorTable(table.name, getKeyArity(key) || table.rows[0][0].length, table.rows),
1141
- ),
1232
+ setOperator(key, createOperatorTable(table.name, getKeyArity(key), table.rows)),
1142
1233
  ),
1143
1234
  );
1144
1235
  const substitutions = new Map(
1145
1236
  config.substitutions.flatMap((substitution) =>
1146
- getScopedKeys(substitution).map((key): [string, string] => [
1237
+ getSubstitutedKeys(substitution).map((key): [string, string] => [
1147
1238
  key,
1148
1239
  getRootKey(substitution.target),
1149
1240
  ]),
1150
1241
  ),
1151
1242
  );
1243
+ [...substitutions.keys()].forEach((key) => constants.delete(key));
1244
+ const checkSubstitutionTarget = (name: string, target: string, arity: number | undefined) => {
1245
+ const targetKey = getRootKey(target);
1246
+ const definition = definitions.get(targetKey);
1247
+ const isDefined =
1248
+ definition !== undefined ||
1249
+ builtins.has(targetKey) ||
1250
+ constants.has(targetKey) ||
1251
+ declaredConstants.includes(target) ||
1252
+ variables.includes(target);
1253
+ if (!isDefined) {
1254
+ throw new TlaConfigError(
1255
+ `The configuration file substitutes for ${name} with the undefined identifier ${target}.`,
1256
+ );
1257
+ }
1258
+ if (definition?.kind === "Operator" && definition.params.length !== arity) {
1259
+ throw new TlaConfigError(
1260
+ `The configuration file substitutes for ${name} with ${target} of different number of arguments.`,
1261
+ );
1262
+ }
1263
+ };
1152
1264
  config.substitutions
1153
1265
  .filter((substitution) => substitution.module === undefined)
1154
1266
  .forEach((substitution) => {
@@ -1164,32 +1276,43 @@ export const createEnvironment = (
1164
1276
  `In the configuration file, the identifier ${target} appears\non the right-hand side of a <- after already appearing on the\nleft-hand side of one.`,
1165
1277
  );
1166
1278
  }
1167
- const targetKey = getRootKey(target);
1168
- const definition = definitions.get(targetKey);
1169
- const isDefined =
1170
- definition !== undefined ||
1171
- builtins.has(targetKey) ||
1172
- constants.has(targetKey) ||
1173
- declaredConstants.includes(target) ||
1174
- variables.includes(target);
1175
- if (!isDefined) {
1176
- throw new TlaConfigError(
1177
- `The configuration file substitutes for ${name} with the undefined identifier ${target}.`,
1178
- );
1179
- }
1180
- if (
1181
- definition?.kind === "Operator" &&
1182
- definition.params.length !==
1183
- (linkedRoot.constants.find((constant) => constant.name === name)?.arity ??
1184
- linkedRoot.visible.get(name)?.arity)
1185
- ) {
1279
+ checkSubstitutionTarget(
1280
+ name,
1281
+ target,
1282
+ linkedRoot.constants.find((constant) => constant.name === name)?.arity ??
1283
+ linkedRoot.visible.get(name)?.arity,
1284
+ );
1285
+ });
1286
+ const scopedSubstitutions = config.substitutions.filter(
1287
+ (substitution) => substitution.module !== undefined,
1288
+ );
1289
+ [...moduleSymbols.keys()].forEach((name) => {
1290
+ const inModule = scopedSubstitutions.filter((substitution) => substitution.module === name);
1291
+ inModule.forEach((substitution) => {
1292
+ const symbol = getScopedSymbol(substitution);
1293
+ if (!symbol) return;
1294
+ if (inModule.some((other) => other.name === substitution.target)) {
1186
1295
  throw new TlaConfigError(
1187
- `The configuration file substitutes for ${name} with ${target} of different number of arguments.`,
1296
+ `In the configuration file, the identifier ${substitution.target} appears\non the right-hand side of a <- after already appearing on the\nleft-hand side of one.`,
1188
1297
  );
1189
1298
  }
1299
+ checkSubstitutionTarget(substitution.name, substitution.target, symbol.arity);
1190
1300
  });
1191
- // TLC sets a module-scoped entry on the body of the module's definition, which every instance of
1192
- // the module shares.
1301
+ const missing = inModule.find((substitution) => !getScopedSymbol(substitution));
1302
+ if (missing) {
1303
+ throw new TlaConfigError(
1304
+ `In the configuration file, the identifier ${missing.name} does not appear in the specification.`,
1305
+ );
1306
+ }
1307
+ });
1308
+ const unknownModule = scopedSubstitutions.find(
1309
+ (substitution) => !moduleSymbols.has(substitution.module ?? ""),
1310
+ );
1311
+ if (unknownModule) {
1312
+ throw new TlaConfigError(
1313
+ `In the configuration file, the module name ${unknownModule.module} is not a module in the specification.`,
1314
+ );
1315
+ }
1193
1316
  const scopedKeys = new Set(
1194
1317
  [...config.constants, ...config.substitutions]
1195
1318
  .filter((item) => item.module !== undefined)
@@ -1220,11 +1343,16 @@ export const createEnvironment = (
1220
1343
  if (symbol.key === key && name !== key) definitions.set(name, alias);
1221
1344
  });
1222
1345
  });
1346
+ moduleOperators.forEach((operator) =>
1347
+ getLinkedModule(operator.module, root.name).constants.forEach((constant) => {
1348
+ const value = operator.constants?.[constant.name];
1349
+ if (value !== undefined) constants.set(constant.name, value);
1350
+ }),
1351
+ );
1223
1352
  const unassigned = declaredConstants.filter(
1224
1353
  (name) => !constants.has(name) && !substitutions.has(name) && !operatorKeys.has(name),
1225
1354
  );
1226
- if (unassigned.length > 0 && !options.isUnconfigured) {
1227
- // TLC reports the first constant it finds unassigned in SANY's order of declarations.
1355
+ if (unassigned.length > 0) {
1228
1356
  const name =
1229
1357
  order.getDeclarationOrder("constant").find((constant) => unassigned.includes(constant)) ??
1230
1358
  unassigned[0];
@@ -1233,7 +1361,7 @@ export const createEnvironment = (
1233
1361
  );
1234
1362
  }
1235
1363
 
1236
- if (options.isTraceRecorded) {
1364
+ if (traceFile !== undefined) {
1237
1365
  builtins.forEach((builtin, name) => {
1238
1366
  const replacement = getTraceSpecArithmetic(builtin);
1239
1367
  if (replacement) builtins.set(name, replacement);
@@ -1264,13 +1392,11 @@ export const createEnvironment = (
1264
1392
  facts,
1265
1393
  startupValues: Array.from({ length: facts.ids.size }, (): Value | undefined => undefined),
1266
1394
  substitutedDefinitions,
1395
+ instanceParams,
1267
1396
  evaluators: new WeakMap(),
1268
1397
  entryEvaluators: new WeakMap(),
1269
1398
  definitionEvaluators: new WeakMap(),
1270
1399
  };
1271
- // TLC evaluates a substitute at startup and rejects it only when that fails and the level bound it
1272
- // computes is above constant. The bound counts the operators that TLC's Java code overrides with
1273
- // a minimum level as state-level, so the analyzer takes them for variables.
1274
1400
  const analyzer = createLevelAnalyzer({
1275
1401
  constants: new Set(constants.keys()),
1276
1402
  variables: new Set([
@@ -1298,17 +1424,13 @@ export const createEnvironment = (
1298
1424
  ? getEvaluationError(definition.body)
1299
1425
  : undefined;
1300
1426
  if (error) {
1301
- // TLC's SpecProcessor adds the message of a TLCRuntimeException, not of an EvalException.
1302
1427
  const addendum = isEvalException(error) ? "" : ` - specifically: ${error.message}`;
1303
1428
  throw new TlaConfigError(
1304
1429
  `The configuration file substitutes constant ${name} with non-constant ${target}${addendum}`,
1305
1430
  );
1306
1431
  }
1307
1432
  });
1308
- // TLC 1.7.4 processes the whole spec before it creates its model checker, which TLCGet reads.
1309
1433
  if (runtime.version !== "1.7.4") runtime.isStarted = true;
1310
- // TLC computes the constant definitions of each module in the order SANY finishes the modules,
1311
- // except those of a module that an INSTANCE gives parameters to.
1312
1434
  const instantiated = new Set(
1313
1435
  linkOrder.flatMap((linked) => linked.module.instances.map((instance) => instance.module)),
1314
1436
  );
@@ -1330,8 +1452,8 @@ export const createEnvironment = (
1330
1452
  },
1331
1453
  ),
1332
1454
  ),
1333
- rootConstants: linkedRoot.constants,
1334
- rootSymbols: linkedRoot.visible,
1335
1455
  javaOverrides: linkOrder.flatMap((linked) => javaOverrides.get(linked.module.name) ?? []),
1456
+ moduleOperators,
1457
+ rootName: root.name,
1336
1458
  };
1337
1459
  };