tla-ts 0.0.9-dev.8298a52 → 0.0.10-dev.06b481ee0

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 (631) hide show
  1. package/README.md +8 -0
  2. package/dist/compile.cjs +6 -6
  3. package/dist/compile.js +10 -10
  4. package/dist/deciders.cjs +41 -37
  5. package/dist/deciders.js +57 -53
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.d.cts +121 -12
  8. package/dist/index.d.ts +121 -12
  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 +2746 -410
  15. package/dist/prove.js +2745 -409
  16. package/dist/run.cjs +144 -31
  17. package/dist/run.js +144 -31
  18. package/dist/tlapm.cjs +2 -2
  19. package/dist/tlapm.js +2 -2
  20. package/dist/transfer.cjs +134 -104
  21. package/dist/transfer.js +134 -104
  22. package/dist/worker.cjs +7 -7
  23. package/dist/worker.js +7 -7
  24. package/package.json +1 -1
  25. package/src/ast.ts +56 -60
  26. package/src/bags.ts +128 -110
  27. package/src/builtins.ts +31 -77
  28. package/src/bundled-modules.ts +1 -8
  29. package/src/check-failure.ts +118 -0
  30. package/src/check-trace.ts +320 -0
  31. package/src/check.ts +1155 -900
  32. package/src/cli/options.ts +12 -32
  33. package/src/cli/run.ts +0 -2
  34. package/src/cli/runtime-invariants.ts +100 -0
  35. package/src/cli/shared.ts +19 -12
  36. package/src/cli/tlapm.ts +433 -82
  37. package/src/cli/tlc-arguments.ts +652 -0
  38. package/src/cli/tlc-output.ts +701 -0
  39. package/src/cli/tlc-usage.ts +360 -0
  40. package/src/cli/tlc.ts +318 -656
  41. package/src/cli/typescript.ts +0 -3
  42. package/src/cli/xml-exporter.ts +27 -49
  43. package/src/cli.ts +0 -1
  44. package/src/codegen.ts +217 -82
  45. package/src/config.ts +140 -109
  46. package/src/coverage.ts +1019 -0
  47. package/src/dependencies.ts +101 -66
  48. package/src/dfid.ts +683 -0
  49. package/src/environment-config.ts +315 -0
  50. package/src/environment.ts +260 -371
  51. package/src/errors.ts +93 -58
  52. package/src/evaluate.ts +848 -374
  53. package/src/exit-codes.ts +63 -13
  54. package/src/fingerprints.ts +75 -11
  55. package/src/index.ts +3 -0
  56. package/src/instances.ts +97 -43
  57. package/src/java-hash.ts +21 -0
  58. package/src/java-overrides.ts +6 -21
  59. package/src/json-reader.ts +455 -0
  60. package/src/json.ts +252 -139
  61. package/src/levels.ts +231 -154
  62. package/src/lexer.ts +83 -101
  63. package/src/liveness-checker.ts +743 -325
  64. package/src/liveness.ts +224 -131
  65. package/src/obligations/action.ts +17 -44
  66. package/src/obligations/anonymize.ts +120 -95
  67. package/src/obligations/enabled-axioms.ts +218 -0
  68. package/src/obligations/equality.ts +1 -3
  69. package/src/obligations/expression.ts +37 -26
  70. package/src/obligations/format.ts +0 -10
  71. package/src/obligations/generate.ts +66 -64
  72. package/src/obligations/index.ts +39 -20
  73. package/src/obligations/levels.ts +27 -18
  74. package/src/obligations/module.ts +28 -55
  75. package/src/obligations/normalize.ts +31 -41
  76. package/src/obligations/operators.ts +0 -3
  77. package/src/obligations/pragma.ts +52 -24
  78. package/src/obligations/printer.ts +94 -63
  79. package/src/obligations/proof.ts +3 -6
  80. package/src/obligations/regroup.ts +0 -5
  81. package/src/obligations/simplify.ts +57 -63
  82. package/src/obligations/source.ts +121 -95
  83. package/src/obligations/subexpression.ts +49 -28
  84. package/src/obligations/substitution.ts +1 -5
  85. package/src/obligations/temporal.ts +12 -20
  86. package/src/obligations/tuples.ts +2 -10
  87. package/src/parallel-worker.ts +34 -57
  88. package/src/parallel.ts +62 -72
  89. package/src/parser.ts +1184 -531
  90. package/src/pcal/ast.ts +53 -86
  91. package/src/pcal/parse.ts +144 -219
  92. package/src/pcal/tokenize.ts +91 -131
  93. package/src/pcal/translate.ts +83 -130
  94. package/src/pcal/validate.ts +48 -68
  95. package/src/pretty-print.ts +7 -8
  96. package/src/prover/decide-pool.ts +8 -13
  97. package/src/prover/deciders.ts +12 -13
  98. package/src/prover/first-order.ts +0 -11
  99. package/src/prover/isabelle/decide.ts +3 -31
  100. package/src/prover/isabelle/encode.ts +82 -64
  101. package/src/prover/isabelle/isar/attrib.ts +250 -0
  102. package/src/prover/isabelle/isar/context.ts +335 -0
  103. package/src/prover/isabelle/isar/induct.ts +382 -0
  104. package/src/prover/isabelle/isar/inner-syntax.ts +262 -117
  105. package/src/prover/isabelle/isar/method.ts +510 -226
  106. package/src/prover/isabelle/isar/proof.ts +570 -23
  107. package/src/prover/isabelle/isar/rule-cases.ts +226 -0
  108. package/src/prover/isabelle/isar/theory.ts +488 -88
  109. package/src/prover/isabelle/isar/toplevel.ts +331 -0
  110. package/src/prover/isabelle/isar/type-check.ts +99 -38
  111. package/src/prover/isabelle/provers/blast/blast.ts +16 -105
  112. package/src/prover/isabelle/provers/blast/branch.ts +0 -33
  113. package/src/prover/isabelle/provers/blast/prove.ts +0 -36
  114. package/src/prover/isabelle/provers/blast/rules.ts +5 -45
  115. package/src/prover/isabelle/provers/blast/state.ts +1 -29
  116. package/src/prover/isabelle/provers/blast/term.ts +8 -37
  117. package/src/prover/isabelle/provers/clasimp.ts +1 -15
  118. package/src/prover/isabelle/provers/classical.ts +58 -61
  119. package/src/prover/isabelle/provers/hypsubst.ts +29 -54
  120. package/src/prover/isabelle/pure/bicompose.ts +24 -39
  121. package/src/prover/isabelle/pure/consts.ts +0 -8
  122. package/src/prover/isabelle/pure/conv.ts +2 -18
  123. package/src/prover/isabelle/pure/drule.ts +95 -45
  124. package/src/prover/isabelle/pure/envir.ts +2 -53
  125. package/src/prover/isabelle/pure/goal.ts +1 -9
  126. package/src/prover/isabelle/pure/interrupt.ts +0 -5
  127. package/src/prover/isabelle/pure/list.ts +0 -3
  128. package/src/prover/isabelle/pure/logic.ts +1 -16
  129. package/src/prover/isabelle/pure/name.ts +1 -10
  130. package/src/prover/isabelle/pure/net.ts +2 -60
  131. package/src/prover/isabelle/pure/object-logic.ts +15 -16
  132. package/src/prover/isabelle/pure/pattern.ts +1 -20
  133. package/src/prover/isabelle/pure/raw-simplifier.ts +16 -63
  134. package/src/prover/isabelle/pure/rewriter.ts +34 -77
  135. package/src/prover/isabelle/pure/search.ts +2 -11
  136. package/src/prover/isabelle/pure/seq.ts +0 -11
  137. package/src/prover/isabelle/pure/simplifier.ts +27 -16
  138. package/src/prover/isabelle/pure/table.ts +2 -33
  139. package/src/prover/isabelle/pure/tactic.ts +11 -23
  140. package/src/prover/isabelle/pure/tactical.ts +47 -48
  141. package/src/prover/isabelle/pure/term-ord.ts +1 -7
  142. package/src/prover/isabelle/pure/term.ts +4 -47
  143. package/src/prover/isabelle/pure/thm.ts +26 -48
  144. package/src/prover/isabelle/pure/type.ts +1 -16
  145. package/src/prover/isabelle/pure/unify.ts +19 -31
  146. package/src/prover/isabelle/pure/variable.ts +2 -8
  147. package/src/prover/isabelle/tla/classical-setup.ts +17 -25
  148. package/src/prover/isabelle/tla/context.ts +65 -26
  149. package/src/prover/isabelle/tla/logic-data.ts +1 -1
  150. package/src/prover/isabelle/tla/logic.ts +53 -32
  151. package/src/prover/isabelle/tla/simpdata.ts +45 -44
  152. package/src/prover/isabelle/tools/intuitionistic.ts +28 -21
  153. package/src/prover/ocaml.ts +4 -7
  154. package/src/prover/prep/action.ts +33 -66
  155. package/src/prover/prep/enabled-axioms.ts +22 -20
  156. package/src/prover/prep/fingerprint.ts +2 -21
  157. package/src/prover/prep/level-comparison.ts +8 -12
  158. package/src/prover/prep/md5.ts +26 -31
  159. package/src/prover/prep/methods.ts +189 -86
  160. package/src/prover/prep/normalize.ts +42 -37
  161. package/src/prover/prove-worker.ts +0 -2
  162. package/src/prover/prove.ts +398 -133
  163. package/src/prover/ptl/decide.ts +96 -56
  164. package/src/prover/ptl/encode.ts +15 -38
  165. package/src/prover/ptl/sat.ts +24 -32
  166. package/src/prover/ptl/tableau.ts +40 -28
  167. package/src/prover/smt/arithmetic.ts +310 -0
  168. package/src/prover/smt/axioms.ts +12 -15
  169. package/src/prover/smt/context.ts +60 -32
  170. package/src/prover/smt/data.ts +517 -401
  171. package/src/prover/smt/encode.ts +155 -85
  172. package/src/prover/smt/extensionality.ts +5 -13
  173. package/src/prover/smt/flatten.ts +7 -28
  174. package/src/prover/smt/format.ts +33 -14
  175. package/src/prover/smt/instances.ts +7 -11
  176. package/src/prover/smt/layout.ts +91 -0
  177. package/src/prover/smt/rewrite.ts +1021 -658
  178. package/src/prover/smt/simplify.ts +441 -0
  179. package/src/prover/smt/solver/ackermannize-bv-tactic.ts +61 -0
  180. package/src/prover/smt/solver/ackr-helper.ts +179 -0
  181. package/src/prover/smt/solver/ackr-model-converter.ts +105 -0
  182. package/src/prover/smt/solver/act-cache.ts +0 -16
  183. package/src/prover/smt/solver/add-bounds-tactic.ts +59 -0
  184. package/src/prover/smt/solver/aig-tactic.ts +52 -0
  185. package/src/prover/smt/solver/aig.ts +889 -0
  186. package/src/prover/smt/solver/arith-rewriter.ts +626 -56
  187. package/src/prover/smt/solver/arith-terms.ts +121 -0
  188. package/src/prover/smt/solver/array-decl-plugin.ts +175 -0
  189. package/src/prover/smt/solver/array-rewriter.ts +319 -0
  190. package/src/prover/smt/solver/array.ts +1 -3
  191. package/src/prover/smt/solver/asserted-formulas.ts +241 -99
  192. package/src/prover/smt/solver/ast-lt.ts +165 -21
  193. package/src/prover/smt/solver/ast-util.ts +160 -7
  194. package/src/prover/smt/solver/ast.ts +1480 -184
  195. package/src/prover/smt/solver/auf-solver.ts +30 -56
  196. package/src/prover/smt/solver/bit-blast-tactic.ts +608 -0
  197. package/src/prover/smt/solver/bit-blaster.ts +871 -0
  198. package/src/prover/smt/solver/blast-term-ite.ts +47 -0
  199. package/src/prover/smt/solver/bound-manager.ts +196 -0
  200. package/src/prover/smt/solver/bound-simplifier.ts +598 -0
  201. package/src/prover/smt/solver/bv-bound-chk.ts +492 -0
  202. package/src/prover/smt/solver/bv-decl-plugin.ts +341 -0
  203. package/src/prover/smt/solver/bv-divrem-bounds.ts +95 -0
  204. package/src/prover/smt/solver/bv-rewriter.ts +1901 -0
  205. package/src/prover/smt/solver/bv1-blast.ts +214 -0
  206. package/src/prover/smt/solver/bv2fpa-converter.ts +174 -0
  207. package/src/prover/smt/solver/bv2int-rewriter.ts +544 -0
  208. package/src/prover/smt/solver/bv2real-rewriter.ts +533 -0
  209. package/src/prover/smt/solver/card2bv.ts +127 -0
  210. package/src/prover/smt/solver/case-split-queue.ts +0 -10
  211. package/src/prover/smt/solver/cg-table.ts +39 -42
  212. package/src/prover/smt/solver/check-logic.ts +330 -0
  213. package/src/prover/smt/solver/clause.ts +0 -27
  214. package/src/prover/smt/solver/cofactor-elim-term-ite.ts +293 -0
  215. package/src/prover/smt/solver/conflict-resolution.ts +78 -11
  216. package/src/prover/smt/solver/context.ts +600 -187
  217. package/src/prover/smt/solver/ctx-simplify.ts +34 -52
  218. package/src/prover/smt/solver/ctx-solver-simplify.ts +122 -0
  219. package/src/prover/smt/solver/datatype-decl-plugin.ts +580 -0
  220. package/src/prover/smt/solver/datatype-rewriter.ts +82 -0
  221. package/src/prover/smt/solver/demodulator.ts +786 -0
  222. package/src/prover/smt/solver/dependent-expr-state.ts +93 -0
  223. package/src/prover/smt/solver/der.ts +39 -30
  224. package/src/prover/smt/solver/diff-neq.ts +205 -0
  225. package/src/prover/smt/solver/distribute-forall.ts +57 -0
  226. package/src/prover/smt/solver/dom-simplify.ts +511 -0
  227. package/src/prover/smt/solver/dt2bv-tactic.ts +296 -0
  228. package/src/prover/smt/solver/dyn-ack.ts +0 -11
  229. package/src/prover/smt/solver/e-matching.ts +9 -41
  230. package/src/prover/smt/solver/elim-bounds.ts +89 -0
  231. package/src/prover/smt/solver/elim-small-bv-tactic.ts +129 -0
  232. package/src/prover/smt/solver/elim-term-ite-tactic.ts +48 -0
  233. package/src/prover/smt/solver/elim-uncnstr.ts +542 -0
  234. package/src/prover/smt/solver/enode.ts +100 -23
  235. package/src/prover/smt/solver/eq2bv-tactic.ts +218 -0
  236. package/src/prover/smt/solver/euf/arith-axioms.ts +403 -0
  237. package/src/prover/smt/solver/euf/arith-internalize.ts +653 -0
  238. package/src/prover/smt/solver/euf/arith-solver.ts +889 -0
  239. package/src/prover/smt/solver/euf/array-axioms.ts +527 -0
  240. package/src/prover/smt/solver/euf/array-internalize.ts +428 -0
  241. package/src/prover/smt/solver/euf/array-solver.ts +306 -0
  242. package/src/prover/smt/solver/euf/bv-ackerman.ts +162 -0
  243. package/src/prover/smt/solver/euf/bv-internalize.ts +748 -0
  244. package/src/prover/smt/solver/euf/bv-solver.ts +449 -0
  245. package/src/prover/smt/solver/euf/dt-solver.ts +618 -0
  246. package/src/prover/smt/solver/euf/egraph.ts +901 -0
  247. package/src/prover/smt/solver/euf/euf-solver.ts +1204 -0
  248. package/src/prover/smt/solver/euf/fpa-solver.ts +317 -0
  249. package/src/prover/smt/solver/euf/mam.ts +1046 -0
  250. package/src/prover/smt/solver/euf/q-ematch.ts +829 -0
  251. package/src/prover/smt/solver/euf/q-mbi.ts +587 -0
  252. package/src/prover/smt/solver/euf/q-model-fixer.ts +296 -0
  253. package/src/prover/smt/solver/euf/q-solver.ts +361 -0
  254. package/src/prover/smt/solver/euf/th-solver.ts +328 -0
  255. package/src/prover/smt/solver/euf/th-solvers.ts +30 -0
  256. package/src/prover/smt/solver/expr-ref.ts +0 -5
  257. package/src/prover/smt/solver/expr-safe-replace.ts +26 -7
  258. package/src/prover/smt/solver/expr2polynomial.ts +271 -0
  259. package/src/prover/smt/solver/factor.ts +163 -0
  260. package/src/prover/smt/solver/fd-solver.ts +734 -0
  261. package/src/prover/smt/solver/fingerprints.ts +15 -26
  262. package/src/prover/smt/solver/fix-dl-var.ts +147 -0
  263. package/src/prover/smt/solver/flat-literals.ts +0 -9
  264. package/src/prover/smt/solver/fm-tactic.ts +884 -0
  265. package/src/prover/smt/solver/fpa-decl-plugin.ts +618 -0
  266. package/src/prover/smt/solver/fpa-rewriter.ts +639 -0
  267. package/src/prover/smt/solver/fpa2bv-converter.ts +2251 -0
  268. package/src/prover/smt/solver/fpa2bv-rewriter.ts +349 -0
  269. package/src/prover/smt/solver/fpa2bv-tactic.ts +73 -0
  270. package/src/prover/smt/solver/goal.ts +179 -28
  271. package/src/prover/smt/solver/goal2nlsat.ts +216 -0
  272. package/src/prover/smt/solver/guard-set.ts +219 -0
  273. package/src/prover/smt/solver/hash.ts +0 -9
  274. package/src/prover/smt/solver/hashtable.ts +30 -23
  275. package/src/prover/smt/solver/inf-eps.ts +63 -0
  276. package/src/prover/smt/solver/inj-axiom.ts +101 -0
  277. package/src/prover/smt/solver/internalizer.ts +113 -133
  278. package/src/prover/smt/solver/lackr.ts +381 -0
  279. package/src/prover/smt/solver/lbool.ts +0 -2
  280. package/src/prover/smt/solver/lia2card.ts +268 -0
  281. package/src/prover/smt/solver/lia2pb.ts +154 -0
  282. package/src/prover/smt/solver/literal.ts +0 -4
  283. package/src/prover/smt/solver/logic-tactics.ts +355 -0
  284. package/src/prover/smt/solver/lp/bound-propagator.ts +7 -26
  285. package/src/prover/smt/solver/lp/constraints.ts +1 -8
  286. package/src/prover/smt/solver/lp/containers.ts +18 -31
  287. package/src/prover/smt/solver/lp/core-solver.ts +35 -37
  288. package/src/prover/smt/solver/lp/cross-nested.ts +4 -21
  289. package/src/prover/smt/solver/lp/dep-intervals.ts +6 -24
  290. package/src/prover/smt/solver/lp/dependency.ts +0 -5
  291. package/src/prover/smt/solver/lp/dioph-eq.ts +8 -103
  292. package/src/prover/smt/solver/lp/explanation.ts +0 -6
  293. package/src/prover/smt/solver/lp/gomory.ts +8 -22
  294. package/src/prover/smt/solver/lp/hnf-cutter.ts +16 -23
  295. package/src/prover/smt/solver/lp/hnf.ts +10 -28
  296. package/src/prover/smt/solver/lp/int-cube.ts +17 -39
  297. package/src/prover/smt/solver/lp/int-solver.ts +3 -35
  298. package/src/prover/smt/solver/lp/lar-solver.ts +70 -83
  299. package/src/prover/smt/solver/lp/lar-term.ts +0 -5
  300. package/src/prover/smt/solver/lp/lp-settings.ts +6 -16
  301. package/src/prover/smt/solver/lp/maximize-term.ts +76 -52
  302. package/src/prover/smt/solver/lp/nex-creator.ts +7 -30
  303. package/src/prover/smt/solver/lp/nex.ts +2 -7
  304. package/src/prover/smt/solver/lp/nla-backoff.ts +0 -5
  305. package/src/prover/smt/solver/lp/nla-basics.ts +48 -30
  306. package/src/prover/smt/solver/lp/nla-core.ts +92 -136
  307. package/src/prover/smt/solver/lp/nla-divisions.ts +31 -43
  308. package/src/prover/smt/solver/lp/nla-emonics.ts +3 -21
  309. package/src/prover/smt/solver/lp/nla-factorization.ts +0 -4
  310. package/src/prover/smt/solver/lp/nla-grobner.ts +177 -149
  311. package/src/prover/smt/solver/lp/nla-horner.ts +2 -12
  312. package/src/prover/smt/solver/lp/nla-intervals.ts +1 -12
  313. package/src/prover/smt/solver/lp/nla-monomial-bounds.ts +15 -42
  314. package/src/prover/smt/solver/lp/nla-monotone.ts +1 -6
  315. package/src/prover/smt/solver/lp/nla-order.ts +0 -13
  316. package/src/prover/smt/solver/lp/nla-patcher.ts +3 -7
  317. package/src/prover/smt/solver/lp/nla-powers.ts +220 -0
  318. package/src/prover/smt/solver/lp/nla-tangents.ts +0 -10
  319. package/src/prover/smt/solver/lp/nla-throttle.ts +0 -6
  320. package/src/prover/smt/solver/lp/nla-transcendentals.ts +324 -0
  321. package/src/prover/smt/solver/lp/nla-types.ts +0 -12
  322. package/src/prover/smt/solver/lp/nla-var-eqs.ts +4 -26
  323. package/src/prover/smt/solver/lp/nra-solver.ts +164 -147
  324. package/src/prover/smt/solver/lp/numeric-pair.ts +0 -4
  325. package/src/prover/smt/solver/lp/pdd-interval.ts +6 -5
  326. package/src/prover/smt/solver/lp/pdd-simplifier.ts +61 -34
  327. package/src/prover/smt/solver/lp/pdd-solver.ts +74 -72
  328. package/src/prover/smt/solver/lp/pdd.ts +730 -358
  329. package/src/prover/smt/solver/lp/random-updater.ts +0 -9
  330. package/src/prover/smt/solver/lp/static-matrix.ts +8 -19
  331. package/src/prover/smt/solver/lp/union-find.ts +5 -5
  332. package/src/prover/smt/solver/macro-finder.ts +530 -0
  333. package/src/prover/smt/solver/mam-compiler.ts +129 -139
  334. package/src/prover/smt/solver/mam-interpreter.ts +0 -21
  335. package/src/prover/smt/solver/max-bv-sharing-tactic.ts +17 -0
  336. package/src/prover/smt/solver/max-cliques.ts +105 -0
  337. package/src/prover/smt/solver/maxcore.ts +310 -0
  338. package/src/prover/smt/solver/maximize-ac-sharing.ts +8 -21
  339. package/src/prover/smt/solver/maxlex.ts +115 -0
  340. package/src/prover/smt/solver/maxsmt-base.ts +106 -0
  341. package/src/prover/smt/solver/maxsmt.ts +92 -0
  342. package/src/prover/smt/solver/mbp/mbp-arith.ts +467 -0
  343. package/src/prover/smt/solver/mbp/mbp-basic-tg.ts +101 -0
  344. package/src/prover/smt/solver/mbp/mbp-plugin.ts +287 -0
  345. package/src/prover/smt/solver/mbp/mbp-qel.ts +48 -0
  346. package/src/prover/smt/solver/mbp/mbp-solve-plugin.ts +271 -0
  347. package/src/prover/smt/solver/mbp/mbp-term-graph.ts +606 -0
  348. package/src/prover/smt/solver/model-based-opt.ts +844 -0
  349. package/src/prover/smt/solver/model-checker.ts +20 -63
  350. package/src/prover/smt/solver/model-evaluator.ts +192 -40
  351. package/src/prover/smt/solver/model-finder.ts +14 -32
  352. package/src/prover/smt/solver/model-generator.ts +104 -46
  353. package/src/prover/smt/solver/model-macro-solver.ts +60 -114
  354. package/src/prover/smt/solver/model-printer.ts +214 -0
  355. package/src/prover/smt/solver/mpf.ts +709 -0
  356. package/src/prover/smt/solver/mus.ts +50 -0
  357. package/src/prover/smt/solver/muz/dl-context.ts +382 -0
  358. package/src/prover/smt/solver/muz/dl-mk-bit-blast.ts +97 -0
  359. package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +167 -0
  360. package/src/prover/smt/solver/muz/dl-mk-elim-term-ite.ts +125 -0
  361. package/src/prover/smt/solver/muz/dl-mk-interp-tail-simplifier.ts +299 -0
  362. package/src/prover/smt/solver/muz/dl-mk-rule-inliner.ts +455 -0
  363. package/src/prover/smt/solver/muz/dl-mk-slice.ts +340 -0
  364. package/src/prover/smt/solver/muz/dl-mk-subsumption-checker.ts +163 -0
  365. package/src/prover/smt/solver/muz/dl-rule-set.ts +273 -0
  366. package/src/prover/smt/solver/muz/dl-rule-transformer.ts +123 -0
  367. package/src/prover/smt/solver/muz/dl-rule.ts +495 -0
  368. package/src/prover/smt/solver/muz/hnf.ts +201 -0
  369. package/src/prover/smt/solver/muz/horn-subsume-model-converter.ts +176 -0
  370. package/src/prover/smt/solver/muz/horn-tactic.ts +214 -0
  371. package/src/prover/smt/solver/muz/rule-properties.ts +359 -0
  372. package/src/prover/smt/solver/muz/spacer-context.ts +1404 -0
  373. package/src/prover/smt/solver/muz/spacer-dl-interface.ts +52 -0
  374. package/src/prover/smt/solver/muz/spacer-generalizers.ts +241 -0
  375. package/src/prover/smt/solver/muz/spacer-manager.ts +127 -0
  376. package/src/prover/smt/solver/muz/spacer-pdr.ts +219 -0
  377. package/src/prover/smt/solver/muz/spacer-prop-solver.ts +333 -0
  378. package/src/prover/smt/solver/muz/spacer-util.ts +282 -0
  379. package/src/prover/smt/solver/muz/substitution.ts +203 -0
  380. package/src/prover/smt/solver/nla2bv-tactic.ts +285 -0
  381. package/src/prover/smt/solver/nlqsat.ts +484 -0
  382. package/src/prover/smt/solver/nlsat/algebraic.ts +357 -236
  383. package/src/prover/smt/solver/nlsat/atoms.ts +73 -54
  384. package/src/prover/smt/solver/nlsat/evaluator.ts +16 -22
  385. package/src/prover/smt/solver/nlsat/explain.ts +429 -275
  386. package/src/prover/smt/solver/nlsat/interval-set.ts +9 -51
  387. package/src/prover/smt/solver/nlsat/levelwise.ts +477 -344
  388. package/src/prover/smt/solver/nlsat/mpbq.ts +47 -14
  389. package/src/prover/smt/solver/nlsat/polynomial.ts +2385 -832
  390. package/src/prover/smt/solver/nlsat/solver.ts +297 -156
  391. package/src/prover/smt/solver/nlsat/transcendental-eval.ts +310 -0
  392. package/src/prover/smt/solver/nlsat/transcendentals.ts +391 -0
  393. package/src/prover/smt/solver/nlsat/types.ts +0 -9
  394. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +9 -51
  395. package/src/prover/smt/solver/nlsat/upolynomial.ts +49 -109
  396. package/src/prover/smt/solver/nlsat-tactic.ts +82 -0
  397. package/src/prover/smt/solver/nnf.ts +112 -61
  398. package/src/prover/smt/solver/normalize-bounds.ts +53 -0
  399. package/src/prover/smt/solver/occf.ts +97 -0
  400. package/src/prover/smt/solver/opt-context.ts +801 -0
  401. package/src/prover/smt/solver/opt-nlsat.ts +386 -0
  402. package/src/prover/smt/solver/opt-pareto.ts +301 -0
  403. package/src/prover/smt/solver/opt-preprocess.ts +101 -0
  404. package/src/prover/smt/solver/opt-solver.ts +312 -0
  405. package/src/prover/smt/solver/opt-value.ts +55 -0
  406. package/src/prover/smt/solver/optsmt.ts +692 -0
  407. package/src/prover/smt/solver/parallel-tactic.ts +299 -0
  408. package/src/prover/smt/solver/pattern-inference.ts +21 -26
  409. package/src/prover/smt/solver/pb-rewriter.ts +281 -0
  410. package/src/prover/smt/solver/pb2bv-tactic.ts +540 -0
  411. package/src/prover/smt/solver/poly-rewriter.ts +261 -79
  412. package/src/prover/smt/solver/probes.ts +577 -0
  413. package/src/prover/smt/solver/propagate-values.ts +37 -30
  414. package/src/prover/smt/solver/proto-model.ts +650 -66
  415. package/src/prover/smt/solver/pull-quant.ts +28 -23
  416. package/src/prover/smt/solver/purify-arith.ts +632 -0
  417. package/src/prover/smt/solver/push-app-ite.ts +37 -0
  418. package/src/prover/smt/solver/qe-arith-plugin.ts +1096 -0
  419. package/src/prover/smt/solver/qe-lite-der.ts +2 -31
  420. package/src/prover/smt/solver/qe-lite-fm.ts +4 -28
  421. package/src/prover/smt/solver/qe-lite.ts +41 -51
  422. package/src/prover/smt/solver/qe-mbp.ts +270 -0
  423. package/src/prover/smt/solver/qe.ts +1499 -0
  424. package/src/prover/smt/solver/qel.ts +25 -0
  425. package/src/prover/smt/solver/qi-queue.ts +0 -16
  426. package/src/prover/smt/solver/qsat.ts +829 -0
  427. package/src/prover/smt/solver/quant-hoist.ts +196 -0
  428. package/src/prover/smt/solver/quant-tactics.ts +17 -74
  429. package/src/prover/smt/solver/quantifier-analyzer.ts +26 -47
  430. package/src/prover/smt/solver/quantifier-manager.ts +22 -32
  431. package/src/prover/smt/solver/quasi-macros.ts +243 -0
  432. package/src/prover/smt/solver/random-gen.ts +0 -2
  433. package/src/prover/smt/solver/rational.ts +45 -8
  434. package/src/prover/smt/solver/recfun-decl-plugin.ts +262 -0
  435. package/src/prover/smt/solver/recfun-rewriter.ts +107 -0
  436. package/src/prover/smt/solver/reduce-args.ts +225 -0
  437. package/src/prover/smt/solver/reduce-bv-size.ts +119 -0
  438. package/src/prover/smt/solver/ref-vector.ts +0 -6
  439. package/src/prover/smt/solver/relevancy.ts +2 -16
  440. package/src/prover/smt/solver/rewriter-tpl.ts +59 -135
  441. package/src/prover/smt/solver/rewriter.ts +507 -144
  442. package/src/prover/smt/solver/rlimit.ts +46 -24
  443. package/src/prover/smt/solver/sat/asymm-branch.ts +325 -0
  444. package/src/prover/smt/solver/sat/big.ts +177 -0
  445. package/src/prover/smt/solver/sat/clause-use-list.ts +71 -0
  446. package/src/prover/smt/solver/sat/clause.ts +203 -0
  447. package/src/prover/smt/solver/sat/cleaner.ts +94 -0
  448. package/src/prover/smt/solver/sat/config.ts +122 -0
  449. package/src/prover/smt/solver/sat/consequences.ts +203 -0
  450. package/src/prover/smt/solver/sat/elim-eqs.ts +142 -0
  451. package/src/prover/smt/solver/sat/goal2sat.ts +579 -0
  452. package/src/prover/smt/solver/sat/inc-sat-solver.ts +464 -0
  453. package/src/prover/smt/solver/sat/model-converter.ts +136 -0
  454. package/src/prover/smt/solver/sat/pb-constraint.ts +268 -0
  455. package/src/prover/smt/solver/sat/pb-solver.ts +1596 -0
  456. package/src/prover/smt/solver/sat/probing.ts +170 -0
  457. package/src/prover/smt/solver/sat/sat-tactic.ts +53 -0
  458. package/src/prover/smt/solver/sat/sat2goal.ts +189 -0
  459. package/src/prover/smt/solver/sat/scc.ts +142 -0
  460. package/src/prover/smt/solver/sat/simplifier.ts +1618 -0
  461. package/src/prover/smt/solver/sat/solver.ts +2107 -0
  462. package/src/prover/smt/solver/sat/var-queue.ts +117 -0
  463. package/src/prover/smt/solver/sat/watch-list.ts +140 -0
  464. package/src/prover/smt/solver/search-tree.ts +284 -0
  465. package/src/prover/smt/solver/seq-axioms.ts +995 -0
  466. package/src/prover/smt/solver/seq-decl-plugin.ts +819 -0
  467. package/src/prover/smt/solver/seq-derive.ts +1298 -0
  468. package/src/prover/smt/solver/seq-eq-approx.ts +236 -0
  469. package/src/prover/smt/solver/seq-eq-solver.ts +439 -0
  470. package/src/prover/smt/solver/seq-factory.ts +89 -0
  471. package/src/prover/smt/solver/seq-monadic.ts +730 -0
  472. package/src/prover/smt/solver/seq-range-predicate.ts +235 -0
  473. package/src/prover/smt/solver/seq-re-rewriter.ts +1171 -0
  474. package/src/prover/smt/solver/seq-re-util.ts +758 -0
  475. package/src/prover/smt/solver/seq-regex-bisim.ts +144 -0
  476. package/src/prover/smt/solver/seq-regex-live.ts +200 -0
  477. package/src/prover/smt/solver/seq-rewriter.ts +2404 -0
  478. package/src/prover/smt/solver/seq-skolem.ts +294 -0
  479. package/src/prover/smt/solver/seq-subset.ts +165 -0
  480. package/src/prover/smt/solver/seq-view-witness.ts +353 -0
  481. package/src/prover/smt/solver/smt-consequences.ts +297 -0
  482. package/src/prover/smt/solver/smt-kernel.ts +124 -0
  483. package/src/prover/smt/solver/smt-logics.ts +66 -0
  484. package/src/prover/smt/solver/smt-setup.ts +589 -0
  485. package/src/prover/smt/solver/smt2-printer.ts +765 -0
  486. package/src/prover/smt/solver/smtlib-parser.ts +3546 -406
  487. package/src/prover/smt/solver/smtlib-script.ts +2134 -0
  488. package/src/prover/smt/solver/solve-eqs.ts +928 -0
  489. package/src/prover/smt/solver/solver.ts +129 -38
  490. package/src/prover/smt/solver/sort.ts +1 -5
  491. package/src/prover/smt/solver/static-features.ts +314 -0
  492. package/src/prover/smt/solver/sticky-generations.ts +0 -6
  493. package/src/prover/smt/solver/tactic.ts +2141 -0
  494. package/src/prover/smt/solver/term-enumeration.ts +205 -0
  495. package/src/prover/smt/solver/theory-array.ts +1240 -0
  496. package/src/prover/smt/solver/theory-bv.ts +1210 -0
  497. package/src/prover/smt/solver/theory-char.ts +355 -0
  498. package/src/prover/smt/solver/theory-datatype.ts +598 -0
  499. package/src/prover/smt/solver/theory-finite-set.ts +30 -0
  500. package/src/prover/smt/solver/theory-fpa.ts +389 -0
  501. package/src/prover/smt/solver/theory-lra-axioms.ts +55 -25
  502. package/src/prover/smt/solver/theory-lra-bounds.ts +31 -11
  503. package/src/prover/smt/solver/theory-lra-final-check.ts +60 -91
  504. package/src/prover/smt/solver/theory-lra-internalize.ts +96 -53
  505. package/src/prover/smt/solver/theory-lra-model.ts +23 -53
  506. package/src/prover/smt/solver/theory-lra-propagate.ts +34 -42
  507. package/src/prover/smt/solver/theory-lra-state.ts +7 -28
  508. package/src/prover/smt/solver/theory-lra.ts +189 -9
  509. package/src/prover/smt/solver/theory-pb.ts +951 -0
  510. package/src/prover/smt/solver/theory-polymorphism.ts +58 -0
  511. package/src/prover/smt/solver/theory-recfun.ts +307 -0
  512. package/src/prover/smt/solver/theory-seq-regex.ts +867 -0
  513. package/src/prover/smt/solver/theory-seq-solve.ts +1373 -0
  514. package/src/prover/smt/solver/theory-seq-state.ts +852 -0
  515. package/src/prover/smt/solver/theory-seq.ts +961 -0
  516. package/src/prover/smt/solver/theory-without-terms.ts +24 -0
  517. package/src/prover/smt/solver/theory.ts +189 -0
  518. package/src/prover/smt/solver/tseitin-cnf.ts +486 -0
  519. package/src/prover/smt/solver/var-subst.ts +37 -39
  520. package/src/prover/smt/table.ts +3 -10
  521. package/src/prover/smt/types.ts +146 -0
  522. package/src/prover/tlapm/cache.ts +74 -48
  523. package/src/prover/tlapm/ocaml-marshal.ts +19 -21
  524. package/src/prover/tlapm/tlapm.ts +21 -20
  525. package/src/prover/tlapm-library.ts +3208 -0
  526. package/src/prover/verdict.ts +9 -2
  527. package/src/prover/zenon/check/check.ts +1 -11
  528. package/src/prover/zenon/check/definitions.ts +5 -15
  529. package/src/prover/zenon/check/equality.ts +0 -8
  530. package/src/prover/zenon/check/functions.ts +0 -4
  531. package/src/prover/zenon/check/keys.ts +0 -7
  532. package/src/prover/zenon/check/propositional.ts +0 -3
  533. package/src/prover/zenon/check/records.ts +1 -6
  534. package/src/prover/zenon/check/relations.ts +2 -14
  535. package/src/prover/zenon/check/rewrites.ts +2 -21
  536. package/src/prover/zenon/check/sets.ts +40 -61
  537. package/src/prover/zenon/check/terms.ts +1 -19
  538. package/src/prover/zenon/check/tla.ts +0 -6
  539. package/src/prover/zenon/decide.ts +10 -19
  540. package/src/prover/zenon/encode.ts +0 -9
  541. package/src/prover/zenon/eqrel.ts +2 -15
  542. package/src/prover/zenon/equality-rules.ts +24 -6
  543. package/src/prover/zenon/expr.ts +29 -28
  544. package/src/prover/zenon/index-table.ts +33 -61
  545. package/src/prover/zenon/isar-case.ts +9 -21
  546. package/src/prover/zenon/isar.ts +37 -67
  547. package/src/prover/zenon/limits.ts +0 -4
  548. package/src/prover/zenon/llproof.ts +2 -22
  549. package/src/prover/zenon/mltoll.ts +66 -106
  550. package/src/prover/zenon/node.ts +20 -15
  551. package/src/prover/zenon/ocaml.ts +1 -17
  552. package/src/prover/zenon/parse.ts +9 -13
  553. package/src/prover/zenon/phrase.ts +2 -14
  554. package/src/prover/zenon/proof.ts +0 -11
  555. package/src/prover/zenon/propositional-rules.ts +0 -7
  556. package/src/prover/zenon/rules.ts +3 -8
  557. package/src/prover/zenon/search.ts +11 -35
  558. package/src/prover/zenon/tla/functions.ts +59 -33
  559. package/src/prover/zenon/tla/llproof.ts +67 -78
  560. package/src/prover/zenon/tla/records.ts +57 -35
  561. package/src/prover/zenon/tla/rewrites.ts +12 -21
  562. package/src/prover/zenon/tla/sets.ts +88 -59
  563. package/src/prover/zenon/tla/terms.ts +0 -8
  564. package/src/prover/zenon/tla/tla.ts +66 -66
  565. package/src/randomization.ts +81 -35
  566. package/src/resolve.ts +1132 -430
  567. package/src/runtime.ts +33 -23
  568. package/src/sany/builtins.ts +11 -14
  569. package/src/sany/generation.ts +383 -0
  570. package/src/sany/generator-def-entries.ts +210 -0
  571. package/src/sany/generator-definitions.ts +688 -0
  572. package/src/sany/generator-expressions.ts +1108 -0
  573. package/src/sany/generator-proofs.ts +665 -0
  574. package/src/sany/generator.ts +192 -2764
  575. package/src/sany/levels.ts +5 -35
  576. package/src/sany/lexer.ts +61 -80
  577. package/src/sany/linter.ts +0 -10
  578. package/src/sany/modules.ts +5 -7
  579. package/src/sany/nodes.ts +6 -44
  580. package/src/sany/parse-error.ts +39 -117
  581. package/src/sany/report.ts +32 -69
  582. package/src/sany/selectors.ts +60 -82
  583. package/src/sany/source.ts +1 -17
  584. package/src/sany/xml.ts +37 -120
  585. package/src/search.ts +913 -227
  586. package/src/simulate.ts +765 -0
  587. package/src/spec-description.ts +198 -104
  588. package/src/standard-definitions.ts +4 -4
  589. package/src/standard-modules.ts +302 -99
  590. package/src/state-graph.ts +52 -33
  591. package/src/subexpressions.ts +578 -166
  592. package/src/substitution-levels.ts +7 -7
  593. package/src/successors.ts +296 -105
  594. package/src/symmetry.ts +37 -28
  595. package/src/tableau.ts +29 -26
  596. package/src/tlc/fingerprint.ts +267 -79
  597. package/src/tlc/jar-data.ts +122 -6
  598. package/src/tlc/messages.ts +127 -56
  599. package/src/tlc/order.ts +53 -72
  600. package/src/tlc/random.ts +38 -49
  601. package/src/tlc/replay.ts +579 -230
  602. package/src/tlc/summary.ts +64 -60
  603. package/src/tlc/tlc-operators.ts +83 -47
  604. package/src/tlc/tlcext-operators.ts +205 -127
  605. package/src/tlc/trace-spec.ts +235 -0
  606. package/src/tlc/value-stream.ts +139 -0
  607. package/src/tlc/version.ts +1 -4
  608. package/src/trace-liveness.ts +529 -0
  609. package/src/transfer.ts +22 -25
  610. package/src/typed-tier.ts +6556 -0
  611. package/src/typescript/actions.ts +10 -25
  612. package/src/typescript/collections.ts +34 -35
  613. package/src/typescript/compile.ts +1 -8
  614. package/src/typescript/context.ts +0 -12
  615. package/src/typescript/emit.ts +0 -1
  616. package/src/typescript/expressions.ts +0 -12
  617. package/src/typescript/formula.ts +9 -15
  618. package/src/typescript/functions.ts +6 -17
  619. package/src/typescript/model.ts +14 -16
  620. package/src/typescript/names.ts +0 -2
  621. package/src/typescript/program.ts +0 -3
  622. package/src/typescript/properties.ts +6 -8
  623. package/src/typescript/scope.ts +0 -5
  624. package/src/typescript/steps.ts +20 -28
  625. package/src/typescript/types.ts +3 -10
  626. package/src/values.ts +917 -522
  627. package/src/worker.ts +7 -8
  628. package/src/prover/isabelle/isar/statement.ts +0 -57
  629. package/src/prover/smt/solver/z3-license.txt +0 -10
  630. package/src/prover/tlaps-module.ts +0 -510
  631. package/src/prover/zenon/LICENSE +0 -32
package/src/resolve.ts CHANGED
@@ -18,12 +18,14 @@ import {
18
18
  type Selector,
19
19
  type SourceLocation,
20
20
  type SourceRange,
21
+ type StepBody,
21
22
  type Theorem,
23
+ type UseOrHide,
22
24
  } from "./ast";
23
25
  import { formatRange, TlaSyntaxError, type SanyError } from "./errors";
26
+ import { parenthesizedRanges } from "./parser";
27
+ import { resolveSynonym } from "./tlc/jar-data";
24
28
 
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
29
  export type SymbolKind =
28
30
  | "Builtin"
29
31
  | "Constant"
@@ -34,8 +36,6 @@ export type SymbolKind =
34
36
  | "Parameter"
35
37
  | `New${NewSymbol["level"]}`;
36
38
 
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
39
  interface Binding {
40
40
  kind: SymbolKind;
41
41
  arity: number;
@@ -49,37 +49,32 @@ export interface ModuleSymbol extends Binding {
49
49
 
50
50
  export type Symbols = Map<string, ModuleSymbol>;
51
51
 
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
52
  interface Goal {
55
53
  statement: Expression | AssumeProve;
56
54
  isSuffices: boolean;
57
55
  isInProof: boolean;
56
+ keyword: string | undefined;
58
57
  }
59
58
 
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
59
  interface LocalName extends Binding {
64
60
  name: string;
65
61
  parent: LocalName | undefined;
66
62
  goal?: Goal;
67
63
  isPending?: boolean;
68
- isNonFact?: boolean;
64
+ nonFactStep?: "Use" | "Hide" | "Instance" | "Define";
69
65
  }
70
66
 
71
- // A name the module binds, from position on.
72
67
  interface ModuleBinding extends Binding {
73
68
  position: SourceLocation;
74
69
  isPending?: boolean;
75
70
  }
76
71
 
77
- // What SANY reports while it goes on analyzing a unit of a module, and the scope it analyzes it in.
78
72
  interface Unit {
79
73
  imported: Symbols;
80
74
  getInstance: (instance: Instance) => InstancedModule;
81
75
  definitions: Map<string, Definition>;
82
76
  getKey: (definition: Definition) => string;
77
+ isModuleKnown: (name: string, location: SourceLocation) => boolean;
83
78
  location: SourceLocation;
84
79
  self: Definition | undefined;
85
80
  facts: Set<string>;
@@ -87,20 +82,24 @@ interface Unit {
87
82
  bindings: Map<string, ModuleBinding>;
88
83
  errors: SanyError[];
89
84
  warnings: SanyError[];
90
- // In a definition, a named theorem, or a numbered step.
91
85
  hasLabelScope: boolean;
92
- // In the scope of a NEW in a nested ASSUME.
93
86
  isLabelForbidden: boolean;
94
87
  isInExcept: boolean;
95
- // The names bound since the innermost definition, label, or step, which a label must take.
96
88
  labelParams: string[];
97
89
  labels: Set<string>;
98
- // In a definition generated for a LET INSTANCE, or a subexpression analyzed where it is defined.
99
90
  isGenerated: boolean;
100
91
  }
101
92
 
102
- interface SelectionError {
93
+ export interface SelectionArguments {
94
+ operator: string;
95
+ range: SourceRange | undefined;
103
96
  args: Expression[];
97
+ params: number[];
98
+ }
99
+
100
+ interface SelectionError {
101
+ applications: SelectionArguments[];
102
+ nulls: NullSelection[];
104
103
  error: SanyError;
105
104
  }
106
105
 
@@ -114,28 +113,63 @@ type Reference = Expression & { kind: "Identifier" | "Apply" | "Subexpression" }
114
113
 
115
114
  export const LOCAL_RESOLUTION = { kind: "Local" } as const;
116
115
 
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
- export const selections = new WeakSet<Expression>();
116
+ export const SELECTION_NAME = "$subexpression";
117
+
118
+ export interface NullSelection {
119
+ node: Expression;
120
+ error: SanyError;
121
+ isEmpty: boolean;
122
+ }
123
+
124
+ interface SelectionArgument {
125
+ operator: string;
126
+ index: number;
127
+ range: SourceRange | undefined;
128
+ }
129
+
130
+ export const selections = new WeakMap<Expression, Expression>();
120
131
  export const selectionErrors = new WeakMap<Expression, SelectionError>();
132
+ export const operatorSelectionErrors = new WeakMap<Expression, SanyError>();
133
+ export const factReferences = new WeakMap<Fact[], Fact[]>();
134
+ export const selectionArguments = new WeakMap<Expression, SelectionArgument>();
135
+ export const nullSelections = new WeakMap<Expression, NullSelection[]>();
136
+ const unresolvedExpressions = new WeakSet<Expression>();
137
+ const emptyExpressions = new WeakSet<Expression>();
138
+
139
+ // SANY's failed selection is an operator application with no operands.
140
+ const isEmptyExpression = (expression: Expression): boolean =>
141
+ emptyExpressions.has(expression) || findNullError(nullSelections.get(expression)) !== undefined;
142
+
143
+ const findNullError = (nulls: NullSelection[] | undefined) =>
144
+ nulls?.find((selection) =>
145
+ selection.isEmpty
146
+ ? isEmptyExpression(selection.node)
147
+ : unresolvedExpressions.has(selection.node),
148
+ )?.error;
121
149
 
122
150
  const renamedArities = new WeakMap<Expression, number>();
123
151
 
124
152
  export const KEY_SEPARATOR = "@";
125
153
 
126
- // A key is a name, the separator, and an id, so the operator @@ is not one.
127
154
  const isKey = (name: string) => {
128
155
  const index = name.lastIndexOf(KEY_SEPARATOR);
129
156
  return index > 0 && index < name.length - 1;
130
157
  };
131
158
 
132
- const CORE_SYMBOLS: Symbols = new Map(
133
- ["~>", "-+->", "\\cdot"].map((name) => [name, { key: name, arity: 2, kind: "Builtin" }]),
134
- );
159
+ const CORE_SYMBOLS: Symbols = new Map([
160
+ ...["~>", "-+->", "\\cdot", "\\X"].map((name): [string, ModuleSymbol] => [
161
+ name,
162
+ { key: name, arity: 2, kind: "Builtin" },
163
+ ]),
164
+ ["'", { key: "'", arity: 1, kind: "Builtin" }],
165
+ ]);
135
166
 
136
167
  export const isBefore = (left: SourceLocation, right: SourceLocation) =>
137
168
  left.line < right.line || (left.line === right.line && left.column < right.column);
138
169
 
170
+ export const compareLocations = (left: SourceLocation, right: SourceLocation) =>
171
+ isBefore(left, right) ? -1 : isBefore(right, left) ? 1 : 0;
172
+
139
173
  export const getArity = (definition: Definition) =>
140
174
  definition.kind === "Operator" ? definition.params.length : 0;
141
175
 
@@ -151,8 +185,6 @@ const lookupLocal = (scope: LocalName | undefined, name: string) => {
151
185
  return undefined;
152
186
  };
153
187
 
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
188
  const getModuleBinding = (name: string, unit: Unit, range: SourceRange | undefined) => {
157
189
  const binding = unit.bindings.get(name);
158
190
  return binding &&
@@ -165,8 +197,6 @@ const getModuleBinding = (name: string, unit: Unit, range: SourceRange | undefin
165
197
  const isSameRange = (left: SourceRange | undefined, right: SourceRange | undefined) =>
166
198
  left !== undefined && right !== undefined && formatRange(left) === formatRange(right);
167
199
 
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
200
  const isBindable = (name: string, current: Binding | undefined, added: Binding, unit: Unit) => {
171
201
  if (current === undefined) return true;
172
202
  if (isSameRange(current.range, added.range)) return false;
@@ -179,7 +209,7 @@ const isBindable = (name: string, current: Binding | undefined, added: Binding,
179
209
  if (current.kind === "Builtin") {
180
210
  unit.errors.push({
181
211
  location,
182
- message: `Symbol ${name} is a built-in operator, and cannot be redefined.`,
212
+ message: `Symbol ${resolveSynonym(name)} is a built-in operator, and cannot be redefined.`,
183
213
  });
184
214
  } else if (
185
215
  added.kind === "Parameter" ||
@@ -188,12 +218,12 @@ const isBindable = (name: string, current: Binding | undefined, added: Binding,
188
218
  ) {
189
219
  unit.errors.push({
190
220
  location,
191
- message: `Multiply-defined symbol '${name}': this definition or declaration conflicts \nwith the one at ${existing}.`,
221
+ message: `Multiply-defined symbol '${resolveSynonym(name)}': this definition or declaration conflicts \nwith the one at ${existing}.`,
192
222
  });
193
223
  } else {
194
224
  unit.warnings.push({
195
225
  location,
196
- message: `Multiple declarations or definitions for symbol ${name}. \nThis duplicates the one at ${existing}.`,
226
+ message: `Multiple declarations or definitions for symbol ${resolveSynonym(name)}. \nThis duplicates the one at ${existing}.`,
197
227
  });
198
228
  }
199
229
  return false;
@@ -214,7 +244,6 @@ const bindName = (
214
244
  ? { ...added, name, parent: scope }
215
245
  : scope;
216
246
 
217
- // Binds the names of a module from the unit on.
218
247
  const bindModuleName = (name: string, added: Binding, unit: Unit) => {
219
248
  if (isBindable(name, unit.bindings.get(name), added, unit)) {
220
249
  unit.bindings.set(name, { ...added, position: unit.location });
@@ -256,34 +285,47 @@ const getVisibleDefinition = (name: string, unit: Unit) => {
256
285
  const isVisible =
257
286
  definition !== undefined &&
258
287
  (definition === unit.self
259
- ? definition.kind === "Function" || definition.isRecursive
260
- : definition.isRecursive || isBefore(definition.location, unit.location));
288
+ ? definition.isRecursive
289
+ : definition.isRecursive
290
+ ? getModuleBinding(name, unit, undefined) !== undefined
291
+ : isBefore(definition.location, unit.location));
261
292
  return isVisible ? definition : undefined;
262
293
  };
263
294
 
264
295
  const findSymbol = (name: string, unit: Unit): ModuleSymbol | undefined => {
265
296
  const definition = getVisibleDefinition(name, unit);
266
- return definition
267
- ? {
268
- key: unit.getKey(definition),
269
- arity: getArity(definition),
270
- params: getParamArities(definition),
271
- kind: "Definition",
272
- }
273
- : unit.imported.get(name);
297
+ if (definition) {
298
+ const declared = definition.isRecursive ? unit.bindings.get(name) : undefined;
299
+ return {
300
+ key: unit.getKey(definition),
301
+ arity: declared?.arity ?? getArity(definition),
302
+ params: declared?.params ?? getParamArities(definition),
303
+ kind: "Definition",
304
+ range: definition.location.range,
305
+ };
306
+ }
307
+ const binding = getModuleBinding(name, unit, undefined);
308
+ if (binding?.isPending) {
309
+ return { key: name, arity: binding.arity, params: binding.params, kind: binding.kind };
310
+ }
311
+ return binding ? unit.imported.get(name) : undefined;
274
312
  };
275
313
 
314
+ const isFact = (name: string, unit: Unit) =>
315
+ unit.facts.has(name) || unit.bindings.get(name)?.kind === "Theorem";
316
+
317
+ const getGoal = (name: string, unit: Unit) =>
318
+ unit.bindings.get(name)?.kind === "Theorem" ? unit.goals.get(name) : undefined;
319
+
276
320
  const findBinding = (name: string, scope: LocalName | undefined, unit: Unit): Binding | undefined =>
277
321
  lookupLocal(scope, name) ?? findSymbol(name, unit);
278
322
 
279
- // SANY reports a name at the range of its text.
280
323
  const getNameRange = (location: SourceLocation, name: string): SourceRange => ({
281
324
  module: location.range?.module ?? "",
282
325
  start: location,
283
326
  end: { line: location.line, column: location.column + name.length - 1 },
284
327
  });
285
328
 
286
- // SANY reports a name at its tokens, such as M ! ⊕ for M!\oplus.
287
329
  const getSpelledNameRange = (
288
330
  expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
289
331
  name: string,
@@ -295,15 +337,23 @@ const getSpelledNameRange = (
295
337
  : getNameRange(expression.location, expression.spelling ?? name);
296
338
  };
297
339
 
298
- // SANY reports an operator symbol applied to operands as one it cannot resolve.
299
340
  const getFixity = (expression: Expression) => {
300
- if (expression.kind !== "Apply") return undefined;
341
+ if (expression.kind !== "Apply" || expression.argumentsRange) return undefined;
301
342
  if (expression.args.length === 2) return "infix";
302
343
  if (expression.args.length !== 1) return undefined;
303
- return isBefore(expression.location, expression.args[0].location) ? "prefix" : "postfix";
344
+ return expression.operator === "-." ? "prefix" : "postfix";
304
345
  };
305
346
 
306
- // SANY names an undefined operator symbol such as + differently from an undefined identifier.
347
+ const BUILTIN_LOCATION = "In module --TLA+ BUILTINS--";
348
+
349
+ const SELECTABLE_KINDS = new Set<SymbolKind>(["Definition", "Instance", "Theorem"]);
350
+
351
+ const reportUnselectable = (name: string, range: SourceRange, unit: Unit) =>
352
+ unit.errors.push({
353
+ location: formatRange(range),
354
+ message: `Cannot take subexpression of \`${resolveSynonym(name)}'.`,
355
+ });
356
+
307
357
  const isOperatorSymbol = (name: string) => !/^[\p{L}\d_]/u.test(name);
308
358
 
309
359
  const isStepName = (name: string) =>
@@ -315,8 +365,6 @@ export const getOperandIndex = (operand: number | "<<" | ">>", count: number) =>
315
365
  return count === 2 ? 1 : -1;
316
366
  };
317
367
 
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
368
  export const getNameSelectors = (
321
369
  name: string,
322
370
  args: Expression[],
@@ -324,7 +372,7 @@ export const getNameSelectors = (
324
372
  ): Selector[] => {
325
373
  const parts = name.split("!");
326
374
  const partCounts =
327
- counts ?? parts.map((_, index) => (index === parts.length - 1 ? args.length : 0));
375
+ counts ?? parts.map((_part, index) => (index === parts.length - 1 ? args.length : 0));
328
376
  return parts.map((part, index) => {
329
377
  const start = partCounts.slice(0, index).reduce((sum, count) => sum + count, 0);
330
378
  return { kind: "Name", name: part, args: args.slice(start, start + partCounts[index]) };
@@ -343,8 +391,6 @@ const getBoundSets = (bounds: Bound[]) =>
343
391
  .filter((bound, index) => index === 0 || bounds[index - 1].set !== bound.set)
344
392
  .map((bound) => bound.set);
345
393
 
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
394
  export const getBinder = (
349
395
  expression: Expression,
350
396
  ): { names: string[]; body: Expression; sets: Expression[] } | undefined => {
@@ -387,7 +433,6 @@ export const getBinder = (
387
433
  }
388
434
  };
389
435
 
390
- // The operands that a selector such as !1 chooses from, where they do not depend on bound names.
391
436
  export const getOperands = (expression: Expression): Expression[] | undefined => {
392
437
  switch (expression.kind) {
393
438
  case "Apply":
@@ -420,6 +465,7 @@ export const getOperands = (expression: Expression): Expression[] | undefined =>
420
465
  case "Enabled":
421
466
  case "Always":
422
467
  case "Eventually":
468
+ case "Selection":
423
469
  return [expression.expression];
424
470
  case "BoxAction":
425
471
  case "AngleAction":
@@ -448,7 +494,7 @@ const isInScopeOfDeclaration = (assumeProve: AssumeProve, clause: number) =>
448
494
 
449
495
  const findLabel = (expression: Expression, name: string): LabelExpression | undefined => {
450
496
  if (expression.kind === "Label") return expression.name === name ? expression : undefined;
451
- if (expression.kind === "Lambda") return undefined;
497
+ if (expression.kind === "Lambda" || expression.kind === "Selection") return undefined;
452
498
  if (expression.kind === "Let") return findLabel(expression.body, name);
453
499
  return getChildren(expression).reduce<LabelExpression | undefined>(
454
500
  (found, child) => found ?? findLabel(child, name),
@@ -456,7 +502,6 @@ const findLabel = (expression: Expression, name: string): LabelExpression | unde
456
502
  );
457
503
  };
458
504
 
459
- // SANY drops a label in the scope of a NEW in a nested ASSUME, after it reports the label.
460
505
  const findClauseLabel = (
461
506
  clause: Expression | NewSymbol | AssumeProve,
462
507
  name: string,
@@ -482,15 +527,10 @@ const getOperandName = (operand: number | "<<" | ">>") =>
482
527
 
483
528
  const SCOPE_MESSAGE = "within the scope of a declaration\n from outside that declaration's scope.";
484
529
 
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
530
  const checkSelection = (
490
531
  reference: Reference,
491
532
  selectors: Selector[],
492
533
  goal: Goal,
493
- scope: LocalName | undefined,
494
534
  unit: Unit,
495
535
  isFact: boolean,
496
536
  ) => {
@@ -499,6 +539,7 @@ const checkSelection = (
499
539
  (index === undefined ? undefined : reference.selectorRanges?.[index]) ??
500
540
  reference.location.range;
501
541
  if (range) unit.errors.push({ location: formatRange(range), message });
542
+ return undefined;
502
543
  };
503
544
  const isOutOfScope = goal.isSuffices === goal.isInProof;
504
545
  const statement = goal.statement;
@@ -507,6 +548,8 @@ const checkSelection = (
507
548
  isGoal: true,
508
549
  isSufficesOpen: goal.isSuffices,
509
550
  };
551
+ let params = 0;
552
+ let args = 0;
510
553
  for (const [offset, selector] of selectors.slice(1).entries()) {
511
554
  const index = offset + 1;
512
555
  const node = state.node;
@@ -516,9 +559,7 @@ const checkSelection = (
516
559
  node.kind !== "NewSymbol" &&
517
560
  node.kind !== "AssumeProve" &&
518
561
  getLabelBody(node).kind === "Let";
519
- return isLet
520
- ? undefined
521
- : report(index - 1, "A name selector here must be from a LET clause.");
562
+ return isLet ? node : report(index - 1, "A name selector here must be from a LET clause.");
522
563
  }
523
564
  const clauses =
524
565
  statement.kind === "AssumeProve"
@@ -556,10 +597,18 @@ const checkSelection = (
556
597
  state.isSufficesOpen = false;
557
598
  continue;
558
599
  }
559
- if (selector.kind !== "Operand") return;
560
- const operand = getOperandName(selector.operand);
600
+ if (selector.kind === "Colon") return node;
601
+ const operand =
602
+ selector.kind === "Operand"
603
+ ? getOperandName(selector.operand)
604
+ : selector.kind === "At"
605
+ ? "@"
606
+ : "!(...)";
607
+ const getPosition = (count: number) =>
608
+ selector.kind === "Operand" ? getOperandIndex(selector.operand, count) : -1;
609
+ const unexpectedNull = `An unexpected null node specified by '${operand}'.\nThis is probably due to a previous error.`;
561
610
  if (state.isSufficesOpen) {
562
- if (getOperandIndex(selector.operand, 1) !== 0) {
611
+ if (getPosition(1) !== 0) {
563
612
  return report(
564
613
  index,
565
614
  node.kind === "AssumeProve"
@@ -570,20 +619,37 @@ const checkSelection = (
570
619
  state.isSufficesOpen = false;
571
620
  continue;
572
621
  }
573
- if (node.kind === "NewSymbol") return;
622
+ if (node.kind === "NewSymbol") return node;
574
623
  if (node.kind !== "AssumeProve") {
575
624
  const expression = getLabelBody(node);
625
+ if (unresolvedExpressions.has(expression)) {
626
+ return report(
627
+ index,
628
+ "Subexpression selection failed, probably due to an error in the selected expression.",
629
+ );
630
+ }
576
631
  if (["Number", "Decimal", "String"].includes(expression.kind)) {
577
632
  return report(index, "Selecting subexpression of expression that has none.");
578
633
  }
579
- const kind =
580
- expression.kind === "Identifier"
581
- ? findBinding(expression.name, scope, unit)?.kind
582
- : undefined;
583
- if (kind === "Variable" || kind?.startsWith("New")) {
634
+ if (isEmptyExpression(expression)) {
584
635
  return report(index, `Choosing operand \`${operand}' of subexpression with no operands.`);
585
636
  }
586
- const sets = getBinder(expression)?.sets;
637
+ const binder = getBinder(expression);
638
+ if (binder && selector.kind !== "Operand") {
639
+ const count = selector.kind === "Bound" ? selector.args.length : 0;
640
+ if (selector.kind === "Bound" && count !== binder.names.length) {
641
+ return report(
642
+ index,
643
+ `Selector with ${count} argument(s) used for quantifier with ${binder.names.length} bound identifier(s).`,
644
+ );
645
+ }
646
+ params += binder.names.length;
647
+ args += count;
648
+ state.node = binder.body;
649
+ state.isGoal = false;
650
+ continue;
651
+ }
652
+ const sets = binder?.sets;
587
653
  const operands =
588
654
  expression.kind === "Identifier"
589
655
  ? []
@@ -591,18 +657,21 @@ const checkSelection = (
591
657
  ? sets.length > 0
592
658
  ? sets
593
659
  : undefined
594
- : expression.kind === "Except"
595
- ? undefined
596
- : getOperands(expression);
597
- if (!operands) return;
598
- const position = getOperandIndex(selector.operand, operands.length);
660
+ : getOperands(expression);
661
+ if (!operands) return node;
662
+ const isExcept = expression.kind === "Except";
663
+ const position = getPosition(isExcept ? expression.updates.length + 1 : operands.length);
599
664
  if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
665
+ if (isExcept && position > 0) {
666
+ return report(index, "Selecting subexpression of an EXCEPT not yet implemented.");
667
+ }
600
668
  state.node = operands[position];
601
669
  state.isGoal = false;
670
+ if (unresolvedExpressions.has(state.node)) return report(index, unexpectedNull);
602
671
  continue;
603
672
  }
604
673
  const clauses = [...node.assumptions, node.conclusion];
605
- const position = getOperandIndex(selector.operand, clauses.length);
674
+ const position = getPosition(clauses.length);
606
675
  if (position === -1) return report(index, `Nonexistent operand specified by \`${operand}'.`);
607
676
  if (isInScopeOfDeclaration(node, position) && (!state.isGoal || isOutOfScope)) {
608
677
  return report(index, `Accessing ASSUME/PROVE clause ${SCOPE_MESSAGE}`);
@@ -613,17 +682,26 @@ const checkSelection = (
613
682
  }
614
683
  state.node = clause;
615
684
  state.isGoal = false;
685
+ if (
686
+ clause.kind !== "NewSymbol" &&
687
+ clause.kind !== "AssumeProve" &&
688
+ unresolvedExpressions.has(clause)
689
+ ) {
690
+ return report(index, unexpectedNull);
691
+ }
616
692
  }
617
- if (isFact) return;
618
693
  if (state.node.kind === "AssumeProve") {
619
- report(undefined, "Selected ASSUME/PROVE instead of expression.");
694
+ return isFact ? state.node : report(undefined, "Selected ASSUME/PROVE instead of expression.");
620
695
  }
621
696
  if (state.node.kind === "NewSymbol") {
622
- report(undefined, "Selected a NEW declaration as an expression or operator.");
697
+ return isFact
698
+ ? state.node
699
+ : report(undefined, "Selected a NEW declaration as an expression or operator.");
623
700
  }
701
+ if (params !== args) return report(undefined, `Expected ${params} arguments but found ${args}.`);
702
+ return state.node;
624
703
  };
625
704
 
626
- // Generator.generateLabel reports a label where it may not appear, and skips the label's body.
627
705
  const isLabelReported = (label: LabelExpression, unit: Unit) => {
628
706
  const message = unit.isGenerated
629
707
  ? undefined
@@ -639,9 +717,6 @@ const isLabelReported = (label: LabelExpression, unit: Unit) => {
639
717
  return true;
640
718
  };
641
719
 
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
720
  const checkLabel = (label: LabelExpression, scope: LocalName | undefined, unit: Unit) => {
646
721
  const range = label.location.range;
647
722
  if (!range || unit.isGenerated) return;
@@ -689,7 +764,6 @@ const checkLabel = (label: LabelExpression, scope: LocalName | undefined, unit:
689
764
  unit.labels.add(label.name);
690
765
  };
691
766
 
692
- // Checks a name that selects part of a theorem or step, and tells whether it is one.
693
767
  const checkReference = (
694
768
  expression: Expression,
695
769
  scope: LocalName | undefined,
@@ -701,15 +775,20 @@ const checkReference = (
701
775
  const first = selectors[0];
702
776
  const goal =
703
777
  selectors.length > 1 && first.kind === "Name"
704
- ? (lookupLocal(scope, first.name)?.goal ?? unit.goals.get(first.name))
778
+ ? (lookupLocal(scope, first.name)?.goal ?? getGoal(first.name, unit))
705
779
  : undefined;
706
- if (goal) checkSelection(expression, selectors, goal, scope, unit, isFact);
707
- return goal !== undefined;
780
+ return goal && { node: checkSelection(expression, selectors, goal, unit, isFact) };
781
+ };
782
+
783
+ const isInstanceName = (name: string, scope: LocalName | undefined, unit: Unit) => {
784
+ const binding = lookupLocal(scope, name) ?? getModuleBinding(name, unit, undefined);
785
+ if (binding) return binding.kind === "Instance";
786
+ return (
787
+ findInstance(name, scope, unit) !== undefined &&
788
+ [...unit.bindings.keys()].some((key) => key.startsWith(`${name}!`))
789
+ );
708
790
  };
709
791
 
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
792
  const getSymbolArity = (
714
793
  expression: Expression & { kind: "Identifier" | "Apply" | "OperatorReference" },
715
794
  name: string,
@@ -722,16 +801,38 @@ const getSymbolArity = (
722
801
  if (renamed !== undefined) return renamed;
723
802
  if (isKey(name)) return expression.kind === "Apply" ? expression.args.length : 0;
724
803
  const local = lookupLocal(scope, name);
725
- if (local?.isNonFact) {
804
+ if (local?.nonFactStep) {
726
805
  unit.errors.push({
727
806
  location: formatRange(getSpelledNameRange(expression, name)),
728
807
  message: "Step number of non-expression step used as an expression.",
729
808
  });
730
809
  return undefined;
731
810
  }
811
+ const keyword = local?.goal?.keyword;
812
+ if (keyword) {
813
+ unit.errors.push({
814
+ location: formatRange(getSpelledNameRange(expression, name)),
815
+ message: `${keyword} proof step selected instead of expression.`,
816
+ });
817
+ return undefined;
818
+ }
732
819
  if (checkReference(expression, scope, unit, false) || (isStepName(name) && local)) {
733
820
  return expression.kind === "Apply" ? expression.args.length : 0;
734
821
  }
822
+ if (isInstanceName(name, scope, unit)) {
823
+ const range = getSpelledNameRange(expression, name);
824
+ unit.errors.push({
825
+ location: formatRange({
826
+ ...range,
827
+ start: {
828
+ line: range.end.line,
829
+ column: range.end.column - name.slice(name.lastIndexOf("!") + 1).length + 1,
830
+ },
831
+ }),
832
+ message: `Operator name ${name} is incomplete.`,
833
+ });
834
+ return undefined;
835
+ }
735
836
  if (local && expression.kind !== "OperatorReference") expression.resolution = LOCAL_RESOLUTION;
736
837
  if (local) return local.arity;
737
838
  const symbol = findSymbol(name, unit);
@@ -741,32 +842,56 @@ const getSymbolArity = (
741
842
  renamedArities.set(expression, symbol.arity);
742
843
  return symbol.arity;
743
844
  }
744
- // A name such as Inv!P1 selects the label P1 of a definition or theorem, which takes its own
745
- // arguments.
746
845
  const prefix = name.includes("!") ? name.slice(0, name.indexOf("!")) : "";
747
- const isSelected =
846
+ const selected =
847
+ prefix === "" ? undefined : (lookupLocal(scope, prefix) ?? findSymbol(prefix, unit));
848
+ if (selected && !SELECTABLE_KINDS.has(selected.kind)) {
849
+ reportUnselectable(
850
+ prefix,
851
+ (expression.kind !== "OperatorReference" && expression.selectorRanges?.[0]) ||
852
+ getSpelledNameRange(expression, name),
853
+ unit,
854
+ );
855
+ return undefined;
856
+ }
857
+ if (
858
+ expression.kind !== "OperatorReference" &&
748
859
  prefix !== "" &&
749
- (lookupLocal(scope, prefix) !== undefined ||
750
- findSymbol(prefix, unit) !== undefined ||
751
- unit.facts.has(prefix));
752
- if (isSelected) {
860
+ isPendingSelectionReported(
861
+ prefix,
862
+ expression.kind === "Apply" && expression.argumentCounts
863
+ ? expression.args.slice(0, expression.argumentCounts[0])
864
+ : [],
865
+ expression.selectorRanges?.[0],
866
+ (expression.kind === "Apply" ? expression.argumentRanges?.[0] : undefined) ?? location.range,
867
+ scope,
868
+ unit,
869
+ )
870
+ ) {
871
+ return undefined;
872
+ }
873
+ if (selected || (prefix !== "" && isFact(prefix, unit))) {
753
874
  return expression.kind === "Apply" ? expression.args.length : 0;
754
875
  }
755
- const spelling = (expression.kind === "Identifier" ? undefined : expression.spelling) ?? name;
756
- const range = formatRange(
757
- expression.kind === "Apply" && spelling === "-."
758
- ? getNameRange(location, "-")
759
- : getSpelledNameRange(expression, name),
760
- );
761
- if (!isOperatorSymbol(name) || isStepName(name)) {
762
- unit.errors.push({ location: range, message: `Unknown operator: \`${name}'.` });
876
+ const fixity = getFixity(expression);
877
+ const isOperatorApplication = expression.kind === "OperatorReference" || fixity !== undefined;
878
+ if (!isOperatorSymbol(name) || isStepName(name) || !isOperatorApplication) {
879
+ unit.errors.push({
880
+ location: formatRange(getSpelledNameRange(expression, name)),
881
+ message: `Unknown operator: \`${resolveSynonym(name)}'.`,
882
+ });
763
883
  return undefined;
764
884
  }
885
+ const spelling = expression.spelling ?? name;
765
886
  unit.errors.push({
766
- location: range,
887
+ location: formatRange(
888
+ fixity === "prefix" && spelling === "-."
889
+ ? getNameRange(location, "-")
890
+ : getSpelledNameRange(expression, name),
891
+ ),
767
892
  message: `Could not find declaration or definition of symbol '${spelling}'.`,
768
893
  });
769
- const fixity = getFixity(expression);
894
+ if (fixity) unresolvedExpressions.add(expression);
770
895
  if (fixity && location.range) {
771
896
  unit.errors.push({
772
897
  location: formatRange(location.range),
@@ -796,10 +921,6 @@ const markRecursiveConstructions = (expression: Expression, name: string): void
796
921
  getChildren(expression).forEach((child) => markRecursiveConstructions(child, name));
797
922
  };
798
923
 
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
924
  const resolveDefinition = (
804
925
  definition: Definition,
805
926
  scope: LocalName | undefined,
@@ -825,14 +946,29 @@ const resolveDefinition = (
825
946
  const isDeclared = current?.isPending === true && recursive.has(current);
826
947
  if (current?.isPending && !isDeclared) {
827
948
  report(
828
- `Recursive ${definition.kind === "Function" ? "function" : "operator"} ${name} defined at wrong LET/IN level.`,
949
+ `Recursive ${definition.kind === "Function" ? "function" : "operator"} ${resolveSynonym(name)} defined at wrong LET/IN level.`,
829
950
  );
830
951
  } else if (current && !isDeclared) {
831
952
  report(
832
953
  definition.kind === "Function"
833
954
  ? `Function name \`${name}' already defined or declared.`
834
- : `Operator ${name} already defined or declared.`,
955
+ : `Operator ${resolveSynonym(name)} already defined or declared.`,
835
956
  );
957
+ } else if (current && definition.kind === "Function" && current.arity > 0) {
958
+ report(`Function ${resolveSynonym(name)} has operator arguments in its RECURSIVE declaration.`);
959
+ } else if (
960
+ current &&
961
+ definition.kind === "Operator" &&
962
+ // Deviation from SANY: Generator#processOperator compares only the last parameter's arity with the RECURSIVE declaration's.
963
+ (current.arity !== definition.params.length ||
964
+ definition.params.some((param) => param.arity > 0))
965
+ ) {
966
+ report(
967
+ `Definition of ${resolveSynonym(name)} has different arity than its RECURSIVE declaration.`,
968
+ );
969
+ }
970
+ if (current && isDeclared && definition.kind === "Operator") {
971
+ current.params = getParamArities(definition);
836
972
  }
837
973
  if (definition.kind === "Function") {
838
974
  resolveExpression(
@@ -844,8 +980,10 @@ const resolveDefinition = (
844
980
  definition.isSelfReferential = isReferenced(definition.body, name);
845
981
  } else resolveExpression(definition.body, inner, unit);
846
982
  if (current && isDeclared) {
847
- current.isPending = false;
848
- current.range = range;
983
+ if (definition.kind === "Operator" || current.arity === 0) {
984
+ current.isPending = false;
985
+ current.range = range;
986
+ }
849
987
  return false;
850
988
  }
851
989
  return definition.kind === "Function"
@@ -858,17 +996,20 @@ const resolveDefinition = (
858
996
  );
859
997
  };
860
998
 
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
999
  const resolveDefinitions = (
864
1000
  definitions: Definition[],
865
1001
  scope: LocalName | undefined,
866
1002
  unit: Unit,
867
1003
  instances: Instance[] = [],
1004
+ declarations: Declaration[] = [],
868
1005
  ) => {
869
1006
  const recursive = new Set<Binding>();
870
1007
  const declared = definitions
871
- .filter((definition) => definition.isRecursive)
1008
+ .filter(
1009
+ (definition) =>
1010
+ definition.isRecursive &&
1011
+ declarations.every((declaration) => declaration.name !== definition.name),
1012
+ )
872
1013
  .reduce<LocalName | undefined>((parent, definition) => {
873
1014
  if (lookupLocal(parent, definition.name)?.isPending) return parent;
874
1015
  const local: LocalName = {
@@ -882,20 +1023,47 @@ const resolveDefinitions = (
882
1023
  recursive.add(local);
883
1024
  return local;
884
1025
  }, scope);
885
- const pending = unit.isGenerated ? [] : [...instances];
886
- const resolveInstances = (
1026
+ const declaredLocals = new Map<Declaration, LocalName | undefined>();
1027
+ const bindDeclaration = (parent: LocalName | undefined, declaration: Declaration) => {
1028
+ const binding: Binding = {
1029
+ kind: "Definition",
1030
+ arity: declaration.arity,
1031
+ params: Array(declaration.arity).fill(0),
1032
+ range: declaration.location.range,
1033
+ };
1034
+ const local: LocalName | undefined = isBindable(
1035
+ declaration.name,
1036
+ lookupLocal(parent, declaration.name) ??
1037
+ getModuleBinding(declaration.name, unit, binding.range),
1038
+ binding,
1039
+ unit,
1040
+ )
1041
+ ? { ...binding, name: declaration.name, parent, isPending: true }
1042
+ : undefined;
1043
+ if (local) recursive.add(local);
1044
+ declaredLocals.set(declaration, local);
1045
+ return local ?? parent;
1046
+ };
1047
+ const pending = [...(unit.isGenerated ? [] : instances), ...declarations].toSorted(
1048
+ (left, right) => compareLocations(left.location, right.location),
1049
+ );
1050
+ const resolvePending = (
887
1051
  current: LocalName | undefined,
888
- isDue: (instance: Instance) => boolean,
1052
+ isDue: (item: Instance | Declaration) => boolean,
889
1053
  ) => {
890
- const count = pending.findIndex((instance) => !isDue(instance));
1054
+ const count = pending.findIndex((item) => !isDue(item));
891
1055
  return pending
892
1056
  .splice(0, count === -1 ? pending.length : count)
893
- .reduce((parent, instance) => resolveLocalInstance(instance, parent, unit), current);
1057
+ .reduce(
1058
+ (parent, item) =>
1059
+ "kind" in item ? resolveLocalInstance(item, parent, unit) : bindDeclaration(parent, item),
1060
+ current,
1061
+ );
894
1062
  };
895
1063
  const resolved = definitions.reduce<LocalName | undefined>((previous, definition) => {
896
1064
  const current = isKey(definition.name)
897
1065
  ? previous
898
- : resolveInstances(previous, (instance) => !isBefore(definition.location, instance.location));
1066
+ : resolvePending(previous, (item) => !isBefore(definition.location, item.location));
899
1067
  const local: LocalName = {
900
1068
  name: definition.name,
901
1069
  arity: getArity(definition),
@@ -903,7 +1071,7 @@ const resolveDefinitions = (
903
1071
  range: definition.location.range,
904
1072
  parent: current,
905
1073
  };
906
- if (isInstantiated(definition)) return local;
1074
+ if (isInstantiated(definition)) return { ...local, params: getParamArities(definition) };
907
1075
  const isInstanced = instances.some((instance) => instance.location === definition.location);
908
1076
  const isGenerated = unit.isGenerated || definition.isLocal || isKey(definition.name);
909
1077
  const inner = isInstanced
@@ -915,36 +1083,51 @@ const resolveDefinitions = (
915
1083
  ? { ...local, params: getParamArities(definition) }
916
1084
  : current;
917
1085
  }, declared);
918
- return resolveInstances(resolved, () => true);
1086
+ const resolvedScope = resolvePending(resolved, () => true);
1087
+ declaredLocals.forEach((local, declaration) => {
1088
+ const range = declaration.location.range;
1089
+ if (range && !unit.isGenerated && (!local || local.isPending)) {
1090
+ unit.errors.push({
1091
+ location: formatRange(range),
1092
+ message: `Symbol ${resolveSynonym(declaration.name)} declared in RECURSIVE statement but not defined.`,
1093
+ });
1094
+ }
1095
+ });
1096
+ return resolvedScope;
919
1097
  };
920
1098
 
921
- // The INSTANCE that a name such as K!C starts with, whose arguments SANY requires at its name.
922
1099
  const findInstance = (name: string, scope: LocalName | undefined, unit: Unit) => {
923
1100
  const prefix = name.split("!")[0];
924
1101
  const binding =
925
1102
  prefix === name
926
1103
  ? undefined
927
1104
  : (lookupLocal(scope, prefix) ?? getModuleBinding(prefix, unit, undefined));
928
- return binding?.kind === "Instance" ? { name: prefix, arity: binding.arity } : undefined;
1105
+ return binding?.kind === "Instance"
1106
+ ? { name: prefix, arity: binding.arity, range: binding.range }
1107
+ : undefined;
929
1108
  };
930
1109
 
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
1110
  const isInstanceArityReported = (
934
1111
  expression: Expression,
935
1112
  scope: LocalName | undefined,
936
1113
  unit: Unit,
937
1114
  ) => {
938
- if (expression.kind !== "Identifier" && expression.kind !== "Apply") return false;
1115
+ if (!isReference(expression)) return false;
1116
+ const selectors = getSelectors(expression);
1117
+ const nameCount = selectors.findIndex((selector) => selector.kind !== "Name");
939
1118
  const instance = findInstance(
940
- expression.kind === "Identifier" ? expression.name : expression.operator,
1119
+ selectors
1120
+ .slice(0, nameCount === -1 ? selectors.length : nameCount)
1121
+ .flatMap((selector) => (selector.kind === "Name" ? [selector.name] : []))
1122
+ .join("!"),
941
1123
  scope,
942
1124
  unit,
943
1125
  );
944
- const count = expression.kind === "Apply" ? (expression.argumentCounts?.[0] ?? 0) : 0;
1126
+ const first = selectors[0];
1127
+ const count = first.kind === "Name" ? first.args.length : 0;
945
1128
  if (!instance || count === instance.arity) return false;
946
1129
  const range =
947
- (expression.kind === "Apply" ? expression.argumentRanges?.[0] : undefined) ??
1130
+ (expression.kind === "Identifier" ? undefined : expression.argumentRanges?.[0]) ??
948
1131
  expression.location.range;
949
1132
  if (range) {
950
1133
  unit.errors.push({
@@ -955,135 +1138,264 @@ const isInstanceArityReported = (
955
1138
  return true;
956
1139
  };
957
1140
 
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.
1141
+ const isArgumentCountReported = (
1142
+ callee: Binding | undefined,
1143
+ name: string,
1144
+ count: number,
1145
+ range: SourceRange | undefined,
1146
+ unit: Unit,
1147
+ ) => {
1148
+ const params = callee?.kind === "Definition" ? callee.params : undefined;
1149
+ if (!callee || !params || params.length === callee.arity) return false;
1150
+ if (range && !unit.isGenerated) {
1151
+ unit.errors.push({
1152
+ location: formatRange(range),
1153
+ message: `Wrong number of arguments (${count}) given to operator '${resolveSynonym(name)}', \nwhich requires ${params.length} arguments.`,
1154
+ });
1155
+ }
1156
+ return true;
1157
+ };
1158
+
1159
+ const resolveExpressionArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
1160
+ const range = arg.location.range;
1161
+ const report = (message: string) => {
1162
+ if (range) unit.errors.push({ location: formatRange(range), message });
1163
+ };
1164
+ const selectionError = operatorSelectionErrors.get(arg);
1165
+ // Deviation from SANY: SANY names its internal node kind here, where tla-ts words it as for a name.
1166
+ if (arg.kind === "Lambda" && arg.operator !== undefined) {
1167
+ report(`The operator ${resolveSynonym(arg.operator)} requires 1 arguments.`);
1168
+ } else if (selectionError) {
1169
+ unit.errors.push(selectionError);
1170
+ } else if (arg.kind === "Lambda" && range) {
1171
+ unresolvedExpressions.add(arg);
1172
+ report("LAMBDA expression used where an expression is required.");
1173
+ } else if (arg.kind === "OperatorReference") {
1174
+ const argArity = getSymbolArity(arg, arg.operator, scope, unit);
1175
+ if (argArity !== undefined) {
1176
+ report(`The operator ${resolveSynonym(arg.operator)} requires ${argArity} arguments.`);
1177
+ }
1178
+ } else resolveExpression(arg, scope, unit);
1179
+ };
1180
+
1181
+ const matchArgument = (
1182
+ arg: Expression,
1183
+ expected: number,
1184
+ index: number,
1185
+ operator: string,
1186
+ operatorRange: SourceRange | undefined,
1187
+ scope: LocalName | undefined,
1188
+ unit: Unit,
1189
+ ) => {
1190
+ const report = (range: SourceRange | undefined, message: string) => {
1191
+ if (range) unit.errors.push({ location: formatRange(range), message });
1192
+ };
1193
+ const reportOperatorArity = (name: string, arity: number) => {
1194
+ const definition =
1195
+ findInstance(operator, scope, unit)?.range ?? findBinding(operator, scope, unit)?.range;
1196
+ if (!definition) return;
1197
+ report(
1198
+ operatorRange,
1199
+ `Operator with incorrect arity passed as argument. \nOperator '${resolveSynonym(name)}' of arity ${arity} is argument number ${index + 1} (counting from 1) to operator \`${formatRange(definition)}', \nbut an operator of arity ${expected} was expected.`,
1200
+ );
1201
+ };
1202
+ const range = arg.location.range;
1203
+ if (expected === 0) {
1204
+ resolveExpressionArgument(arg, scope, unit);
1205
+ return true;
1206
+ }
1207
+ const parenthesized = parenthesizedRanges.get(arg);
1208
+ if (parenthesized) {
1209
+ report(
1210
+ parenthesized,
1211
+ `An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
1212
+ );
1213
+ return false;
1214
+ }
1215
+ const reference = selections.get(arg) ?? arg;
1216
+ // SANY reads a reference that ends in arguments as an application, not as a selector's arguments.
1217
+ const selectors =
1218
+ reference.kind === "Subexpression" ||
1219
+ (reference.kind === "Apply" && reference.argumentCounts?.at(-1) === 0)
1220
+ ? getSelectors(reference)
1221
+ : [];
1222
+ const selectorIndex = selectors.findIndex(
1223
+ (selector) => "args" in selector && selector.args.length > 0,
1224
+ );
1225
+ if (selectorIndex !== -1 && isReference(reference)) {
1226
+ const selector = selectors[selectorIndex];
1227
+ report(
1228
+ reference.selectorRanges?.[selectorIndex],
1229
+ `Selector \`${selector.kind === "Name" ? selector.name : "!(...)"}' should not have argument(s).`,
1230
+ );
1231
+ return false;
1232
+ }
1233
+ if (arg.kind === "Lambda") {
1234
+ if (selections.has(arg)) resolveExpression(arg, scope, unit);
1235
+ else resolveLambda(arg, scope, unit);
1236
+ if (findNullError(nullSelections.get(arg))) return false;
1237
+ const isMatched = arg.params.length === expected;
1238
+ if (!isMatched && selections.has(arg)) {
1239
+ report(range, `Expected arity ${expected} but found operator of arity ${arg.params.length}.`);
1240
+ } else if (!isMatched && range && arg.operator === undefined) {
1241
+ report(
1242
+ operatorRange,
1243
+ `Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
1244
+ );
1245
+ } else if (!isMatched && arg.operator !== undefined) {
1246
+ reportOperatorArity(arg.operator, arg.params.length);
1247
+ }
1248
+ return isMatched;
1249
+ }
1250
+ if (reference !== arg) {
1251
+ const failed = findNullError(nullSelections.get(arg));
1252
+ if (failed) unit.errors.push(failed);
1253
+ if (!failed) {
1254
+ report(reference.location.range, `Expected arity ${expected} but found operator of arity 0.`);
1255
+ }
1256
+ return false;
1257
+ }
1258
+ if (arg.kind === "Subexpression") {
1259
+ resolveExpression(arg, scope, unit);
1260
+ return false;
1261
+ }
1262
+ if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
1263
+ report(
1264
+ range,
1265
+ `An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
1266
+ );
1267
+ return false;
1268
+ }
1269
+ const name = arg.kind === "Identifier" ? arg.name : arg.operator;
1270
+ const argParams = findBinding(name, scope, unit)?.params ?? [];
1271
+ const argArity = getSymbolArity(arg, name, scope, unit);
1272
+ if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
1273
+ report(
1274
+ getSpelledNameRange(arg, name),
1275
+ `Expected arity ${expected} but found operator of arity ${argArity}.`,
1276
+ );
1277
+ }
1278
+ if (arg.kind === "OperatorReference" && argArity !== undefined && argArity !== expected) {
1279
+ reportOperatorArity(name, argArity);
1280
+ }
1281
+ // Deviation from SANY: OpDefNode#matchingOpArgOperand reads as many parameters as a RECURSIVE declaration gives and crashes when the definition has fewer.
1282
+ return argArity === expected && argParams.every((param) => param === 0);
1283
+ };
1284
+
963
1285
  const resolveApply = (
964
1286
  expression: Expression & { kind: "Apply" },
965
1287
  scope: LocalName | undefined,
966
1288
  unit: Unit,
967
1289
  ) => {
968
- if (isInstanceArityReported(expression, scope, unit)) return;
969
1290
  const operator = expression.operator;
970
1291
  const callee = findBinding(operator, scope, unit);
971
- const arity = getSymbolArity(expression, operator, scope, unit);
972
- if (arity === undefined) return;
973
- if (arity !== expression.args.length) {
1292
+ const arity = isInstanceArityReported(expression, scope, unit)
1293
+ ? undefined
1294
+ : getSymbolArity(expression, operator, scope, unit);
1295
+ if (arity !== undefined && arity !== expression.args.length) {
974
1296
  unit.errors.push({
975
1297
  location: formatRange(expression.argumentsRange ?? getSpelledNameRange(expression, operator)),
976
- message: `The operator ${operator} requires ${arity - (findInstance(operator, scope, unit)?.arity ?? 0)} arguments.`,
1298
+ message: `The operator ${resolveSynonym(operator)} requires ${arity - (findInstance(operator, scope, unit)?.arity ?? 0)} arguments.`,
977
1299
  });
1300
+ }
1301
+ if (arity !== expression.args.length) {
1302
+ emptyExpressions.add(expression);
978
1303
  return;
979
1304
  }
980
- const params = callee && (callee.kind === "Definition" ? callee.params : []);
1305
+ const params =
1306
+ callee && (callee.kind === "Definition" ? callee.params : expression.args.map(() => 0));
981
1307
  if (params === undefined) {
982
1308
  expression.args.forEach((arg) => resolveArgument(arg, scope, unit));
983
1309
  return;
984
1310
  }
985
- const report = (range: SourceRange | undefined, message: string) => {
986
- if (range) unit.errors.push({ location: formatRange(range), message });
987
- };
988
- expression.args
989
- .map((arg, index) => {
990
- const expected = params[index] ?? 0;
991
- const range = arg.location.range;
992
- 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.
995
- if (arg.kind === "Lambda" && arg.operator !== undefined) {
996
- report(range, `The operator ${arg.operator} requires 1 arguments.`);
997
- } else if (arg.kind === "Lambda" && range) {
998
- report(range, "LAMBDA expression used where an expression is required.");
999
- } else if (arg.kind === "OperatorReference") {
1000
- const argArity = getSymbolArity(arg, arg.operator, scope, unit);
1001
- if (argArity !== undefined) {
1002
- report(range, `The operator ${arg.operator} requires ${argArity} arguments.`);
1003
- }
1004
- } else resolveExpression(arg, scope, unit);
1005
- return true;
1006
- }
1007
- // A name that selects a subexpression gives an operator of the names it leaves unbound.
1008
- if (arg.kind === "Lambda") {
1009
- resolveExpression(arg, scope, unit);
1010
- const isMatched = arg.params.length === expected;
1011
- if (!isMatched && selections.has(arg)) {
1012
- report(
1013
- range,
1014
- `Expected arity ${expected} but found operator of arity ${arg.params.length}.`,
1015
- );
1016
- } else if (!isMatched && range && arg.operator === undefined) {
1017
- report(
1018
- expression.selectorRanges?.at(-1) ?? getSpelledNameRange(expression, operator),
1019
- `Lambda expression with arity ${arg.params.length} used as argument ${index + 1} of operator \`${operator}', \nbut an operator of arity ${expected} is required.`,
1020
- );
1021
- }
1022
- return isMatched;
1023
- }
1024
- if (arg.kind === "Subexpression") {
1025
- resolveExpression(arg, scope, unit);
1026
- return true;
1027
- }
1028
- // Generator.selectorToNode: a name such as K(1)!C names an operator, but none of its parts
1029
- // may take arguments.
1030
- if (arg.kind === "Apply" && arg.argumentCounts?.at(-1) === 0) {
1031
- const index = arg.argumentCounts.findIndex((count) => count > 0);
1032
- report(
1033
- arg.selectorRanges?.[index],
1034
- `Selector \`${arg.operator.split("!")[index]}' should not have argument(s).`,
1035
- );
1036
- return false;
1037
- }
1038
- if (arg.kind !== "Identifier" && arg.kind !== "OperatorReference") {
1039
- report(
1040
- range,
1041
- `An expression appears as argument number ${index + 1} (counting from 1) to operator '${operator}', in a position an operator is required.`,
1042
- );
1043
- return false;
1044
- }
1045
- const name = arg.kind === "Identifier" ? arg.name : arg.operator;
1046
- const argParams = findBinding(name, scope, unit)?.params ?? [];
1047
- const argArity = getSymbolArity(arg, name, scope, unit);
1048
- if (arg.kind === "Identifier" && argArity !== undefined && argArity !== expected) {
1049
- report(
1050
- getSpelledNameRange(arg, name),
1051
- `Expected arity ${expected} but found operator of arity ${argArity}.`,
1052
- );
1053
- }
1054
- return argArity === expected && argParams.every((param) => param === 0);
1055
- })
1056
- .forEach((isMatched, index) => {
1057
- if (!isMatched) {
1058
- report(
1059
- expression.location.range,
1060
- `Argument number ${index + 1} to operator '${operator}' \nshould be a ${params[index]}-parameter operator.`,
1061
- );
1062
- }
1063
- });
1311
+ const operatorRange =
1312
+ expression.selectorRanges?.at(-1) ?? getSpelledNameRange(expression, operator);
1313
+ const isSelection = operator === SELECTION_NAME;
1314
+ const matches = expression.args.slice(0, params.length).map((arg, index) => {
1315
+ const source = isSelection ? selectionArguments.get(arg) : undefined;
1316
+ return matchArgument(
1317
+ arg,
1318
+ params[index],
1319
+ source?.index ?? index,
1320
+ source?.operator ?? operator,
1321
+ source?.range ?? operatorRange,
1322
+ scope,
1323
+ isSelection ? { ...unit, isGenerated: false } : unit,
1324
+ );
1325
+ });
1326
+ if (
1327
+ isArgumentCountReported(
1328
+ callee,
1329
+ operator,
1330
+ expression.args.length,
1331
+ expression.location.range,
1332
+ unit,
1333
+ )
1334
+ ) {
1335
+ return;
1336
+ }
1337
+ matches.forEach((isMatched, index) => {
1338
+ if (!isMatched && expression.location.range) {
1339
+ unit.errors.push({
1340
+ location: formatRange(expression.location.range),
1341
+ message: `Argument number ${index + 1} to operator '${isSelection ? "LAMBDA" : operator}' \nshould be a ${params[index]}-parameter operator.`,
1342
+ });
1343
+ }
1344
+ });
1064
1345
  };
1065
1346
 
1066
- // An argument may name an operator that takes arguments of its own.
1067
1347
  const resolveArgument = (arg: Expression, scope: LocalName | undefined, unit: Unit) => {
1068
1348
  if (arg.kind === "Identifier") getSymbolArity(arg, arg.name, scope, unit);
1069
1349
  else resolveExpression(arg, scope, unit);
1070
1350
  };
1071
1351
 
1072
- // Reports the error of a name that selects nothing, after the arguments SANY analyzes before it.
1073
1352
  const isSelectionReported = (expression: Expression, scope: LocalName | undefined, unit: Unit) => {
1074
1353
  const failed = selectionErrors.get(expression);
1075
1354
  if (!failed) return false;
1076
- failed.args.forEach((arg) => resolveArgument(arg, scope, unit));
1077
- unit.errors.push(failed.error);
1355
+ failed.applications.forEach((application) =>
1356
+ application.args
1357
+ .slice(0, application.params.length)
1358
+ .forEach((arg, index) =>
1359
+ matchArgument(
1360
+ arg,
1361
+ application.params[index],
1362
+ index,
1363
+ application.operator,
1364
+ application.range,
1365
+ scope,
1366
+ unit,
1367
+ ),
1368
+ ),
1369
+ );
1370
+ unit.errors.push(findNullError(failed.nulls) ?? failed.error);
1078
1371
  return true;
1079
1372
  };
1080
1373
 
1374
+ const resolveLambda = (
1375
+ expression: Expression & { kind: "Lambda" },
1376
+ scope: LocalName | undefined,
1377
+ unit: Unit,
1378
+ ) =>
1379
+ resolveExpression(
1380
+ expression.body,
1381
+ bindNames(scope, expression.params, expression.paramRanges, unit),
1382
+ {
1383
+ ...unit,
1384
+ labelParams: [...unit.labelParams, ...expression.params],
1385
+ },
1386
+ );
1387
+
1081
1388
  const resolveExpression = (
1082
1389
  expression: Expression,
1083
1390
  scope: LocalName | undefined,
1084
1391
  unit: Unit,
1085
1392
  ): void => {
1086
1393
  if (selections.has(expression) && !unit.isGenerated) {
1394
+ const failed = findNullError(nullSelections.get(expression));
1395
+ if (failed) {
1396
+ unit.errors.push(failed);
1397
+ return;
1398
+ }
1087
1399
  resolveExpression(expression, scope, { ...unit, isGenerated: true });
1088
1400
  return;
1089
1401
  }
@@ -1097,18 +1409,43 @@ const resolveExpression = (
1097
1409
  case "Number":
1098
1410
  case "Decimal":
1099
1411
  case "String":
1412
+ return;
1100
1413
  case "ExceptAt":
1414
+ if (!unit.isInExcept && expression.location.range) {
1415
+ emptyExpressions.add(expression);
1416
+ unit.errors.push({
1417
+ location: formatRange(expression.location.range),
1418
+ message: "@ used where its meaning is not defined.",
1419
+ });
1420
+ }
1101
1421
  return;
1102
1422
  case "Identifier": {
1103
- if (isInstanceArityReported(expression, scope, unit)) return;
1104
1423
  const name = expression.name;
1105
- const arity = getSymbolArity(expression, name, scope, unit);
1106
- if (arity !== undefined && arity > 0) {
1424
+ const arity = isInstanceArityReported(expression, scope, unit)
1425
+ ? undefined
1426
+ : getSymbolArity(expression, name, scope, unit);
1427
+ if (arity === 0) {
1428
+ const binding = findBinding(name, scope, unit);
1429
+ const kind = binding?.kind;
1430
+ // SANY's selector chooses no operand of a variable, theorem, step or NEW symbol, unlike of a constant or definition.
1431
+ if (
1432
+ kind === "Variable" ||
1433
+ kind === "Theorem" ||
1434
+ kind?.startsWith("New") ||
1435
+ isFact(name, unit)
1436
+ ) {
1437
+ emptyExpressions.add(expression);
1438
+ }
1439
+ isArgumentCountReported(binding, name, 0, getSpelledNameRange(expression, name), unit);
1440
+ return;
1441
+ }
1442
+ if (arity !== undefined) {
1107
1443
  unit.errors.push({
1108
1444
  location: formatRange(getSpelledNameRange(expression, name)),
1109
- message: `The operator ${name} requires ${arity} arguments.`,
1445
+ message: `The operator ${resolveSynonym(name)} requires ${arity} arguments.`,
1110
1446
  });
1111
1447
  }
1448
+ emptyExpressions.add(expression);
1112
1449
  return;
1113
1450
  }
1114
1451
  case "OperatorReference":
@@ -1118,11 +1455,14 @@ const resolveExpression = (
1118
1455
  resolveApply(expression, scope, unit);
1119
1456
  return;
1120
1457
  case "Lambda":
1121
- resolveExpression(
1122
- expression.body,
1123
- bindNames(scope, expression.params, expression.paramRanges, unit),
1124
- bindLabelParams(expression.params),
1125
- );
1458
+ if (selections.has(expression)) resolveLambda(expression, scope, unit);
1459
+ else if (expression.location.range) {
1460
+ unresolvedExpressions.add(expression);
1461
+ unit.errors.push({
1462
+ location: formatRange(expression.location.range),
1463
+ message: "LAMBDA expression used where an expression is required.",
1464
+ });
1465
+ }
1126
1466
  return;
1127
1467
  case "And":
1128
1468
  case "Or":
@@ -1145,7 +1485,13 @@ const resolveExpression = (
1145
1485
  case "Let":
1146
1486
  resolveExpression(
1147
1487
  expression.body,
1148
- resolveDefinitions(expression.definitions, scope, unit, expression.instances),
1488
+ resolveDefinitions(
1489
+ expression.definitions,
1490
+ scope,
1491
+ unit,
1492
+ expression.instances,
1493
+ expression.recursive,
1494
+ ),
1149
1495
  unit,
1150
1496
  );
1151
1497
  return;
@@ -1235,37 +1581,286 @@ const resolveExpression = (
1235
1581
  resolveExpression(expression.body, scope, { ...unit, labelParams: [], labels: new Set() });
1236
1582
  checkLabel(expression, scope, unit);
1237
1583
  return;
1238
- case "Subexpression": {
1239
- if (isSelectionReported(expression, scope, unit)) return;
1240
- expression.selectors
1241
- .flatMap((selector) => ("args" in selector ? selector.args : []))
1242
- .forEach((arg) => resolveArgument(arg, scope, unit));
1243
- checkReference(expression, scope, unit, false);
1584
+ case "Subexpression":
1585
+ if (isSubexpressionReported(expression, scope, unit)) emptyExpressions.add(expression);
1586
+ return;
1587
+ case "Selection":
1588
+ return;
1589
+ }
1590
+ };
1591
+
1592
+ const isPendingSelectionReported = (
1593
+ name: string,
1594
+ args: Expression[],
1595
+ selectorRange: SourceRange | undefined,
1596
+ argumentsRange: SourceRange | undefined,
1597
+ scope: LocalName | undefined,
1598
+ unit: Unit,
1599
+ ) => {
1600
+ const pending = lookupLocal(scope, name) ?? getModuleBinding(name, unit, undefined);
1601
+ if (!pending?.isPending || !selectorRange || !argumentsRange) return false;
1602
+ if (args.length !== pending.arity) {
1603
+ unit.errors.push({
1604
+ location: formatRange(argumentsRange),
1605
+ message: `The operator ${name} requires ${pending.arity} arguments.`,
1606
+ });
1607
+ return true;
1608
+ }
1609
+ args.forEach((arg) => resolveExpression(arg, scope, unit));
1610
+ unit.errors.push({
1611
+ location: formatRange(selectorRange),
1612
+ message: `Subexpression of \`${name}' used inside the operator's definition.`,
1613
+ });
1614
+ return true;
1615
+ };
1616
+
1617
+ const isSubexpressionReported = (
1618
+ expression: Expression & { kind: "Subexpression" },
1619
+ scope: LocalName | undefined,
1620
+ unit: Unit,
1621
+ ) => {
1622
+ if (
1623
+ isInstanceArityReported(expression, scope, unit) ||
1624
+ isSelectionReported(expression, scope, unit)
1625
+ ) {
1626
+ return true;
1627
+ }
1628
+ const head = expression.selectors[0];
1629
+ const range = expression.selectorRanges?.[0];
1630
+ const nameCount = expression.selectors.findIndex((selector) => selector.kind !== "Name");
1631
+ const names = expression.selectors
1632
+ .slice(0, nameCount)
1633
+ .flatMap((selector) => (selector.kind === "Name" ? [selector.name] : []));
1634
+ const symbolIndex = names.findIndex(
1635
+ (_name, index) => index > 0 && findSymbol(names.slice(0, index + 1).join("!"), unit),
1636
+ );
1637
+ const isInstance =
1638
+ names.length > 1 &&
1639
+ (lookupLocal(scope, names[0]) ?? getModuleBinding(names[0], unit, undefined))?.kind ===
1640
+ "Instance";
1641
+ const lastRange = expression.selectorRanges?.[nameCount - 1];
1642
+ if (isInstance && symbolIndex === -1 && range && lastRange) {
1643
+ unit.errors.push({
1644
+ location: formatRange({ module: range.module, start: range.start, end: lastRange.end }),
1645
+ message: `Unknown operator: \`${resolveSynonym(names.join("!"))}'.`,
1646
+ });
1647
+ return true;
1648
+ }
1649
+ const name = names.slice(0, symbolIndex + 1).join("!");
1650
+ const symbol = symbolIndex > 0 ? findSymbol(name, unit) : undefined;
1651
+ const argumentCount = expression.selectors
1652
+ .slice(0, symbolIndex + 1)
1653
+ .flatMap((selector) => ("args" in selector ? selector.args : [])).length;
1654
+ const argumentRange = expression.argumentRanges?.[symbolIndex] ?? expression.location.range;
1655
+ if (symbol && symbol.arity !== argumentCount && argumentRange) {
1656
+ unit.errors.push({
1657
+ location: formatRange(argumentRange),
1658
+ message: `The operator ${resolveSynonym(name)} requires ${symbol.arity} arguments.`,
1659
+ });
1660
+ return true;
1661
+ }
1662
+ if (head.kind === "Name" && expression.selectors[1].kind !== "Name" && range) {
1663
+ const local = lookupLocal(scope, head.name);
1664
+ const binding = local ?? findSymbol(head.name, unit);
1665
+ if ((binding === undefined && !isFact(head.name, unit)) || binding?.kind === "Instance") {
1666
+ unit.errors.push({
1667
+ location: formatRange(range),
1668
+ message: `Unknown operator: \`${resolveSynonym(head.name)}'.`,
1669
+ });
1670
+ return true;
1671
+ }
1672
+ if (binding && (!SELECTABLE_KINDS.has(binding.kind) || local?.nonFactStep)) {
1673
+ reportUnselectable(head.name, range, unit);
1674
+ return true;
1244
1675
  }
1245
1676
  }
1677
+ if (
1678
+ head.kind === "Name" &&
1679
+ isPendingSelectionReported(
1680
+ head.name,
1681
+ head.args,
1682
+ range,
1683
+ expression.argumentRanges?.[0] ?? expression.location.range,
1684
+ scope,
1685
+ unit,
1686
+ )
1687
+ ) {
1688
+ return true;
1689
+ }
1690
+ expression.selectors
1691
+ .flatMap((selector) => ("args" in selector ? selector.args : []))
1692
+ .forEach((arg) => resolveArgument(arg, scope, unit));
1693
+ const selected = checkReference(expression, scope, unit, false);
1694
+ return selected ? selected.node === undefined : false;
1246
1695
  };
1247
1696
 
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
- const checkFacts = (facts: Fact[], scope: LocalName | undefined, unit: Unit) =>
1252
- facts.forEach((fact) => {
1253
- if (
1254
- fact.kind === "ModuleFact" ||
1255
- isInstanceArityReported(fact, scope, unit) ||
1256
- checkReference(fact, scope, unit, true)
1257
- ) {
1258
- return;
1697
+ const reportUnknownModule = (fact: Fact & { kind: "ModuleFact" }, unit: Unit) => {
1698
+ const range = fact.location.range;
1699
+ if (unit.isModuleKnown(fact.name, fact.location) || !range) return;
1700
+ unit.errors.push({
1701
+ location: formatRange({
1702
+ ...range,
1703
+ start: { line: range.end.line, column: range.end.column - fact.name.length + 1 },
1704
+ }),
1705
+ message: `Module \`${fact.name}' used without being extended or instantiated.`,
1706
+ });
1707
+ };
1708
+
1709
+ const checkFact = (fact: Fact, reference: Fact, scope: LocalName | undefined, unit: Unit) => {
1710
+ if (fact.kind === "ModuleFact") {
1711
+ reportUnknownModule(fact, unit);
1712
+ return undefined;
1713
+ }
1714
+ if (fact.kind === "ExceptAt" && fact.location.range) {
1715
+ unit.errors.push({
1716
+ location: formatRange(fact.location.range),
1717
+ message: "Unknown operator: `@'.",
1718
+ });
1719
+ return BUILTIN_LOCATION;
1720
+ }
1721
+ const hidden = getHiddenExpressionLocation(fact, reference, scope, unit);
1722
+ if (selections.has(fact)) {
1723
+ resolveExpression(fact, scope, unit);
1724
+ return hidden;
1725
+ }
1726
+ if (isInstanceArityReported(fact, scope, unit)) return hidden;
1727
+ const selected = checkReference(fact, scope, unit, true);
1728
+ if (selected) {
1729
+ const kind = selected.node?.kind;
1730
+ if (kind === undefined) return BUILTIN_LOCATION;
1731
+ return kind === "AssumeProve" || kind === "NewSymbol"
1732
+ ? undefined
1733
+ : fact.location.range && formatRange(fact.location.range);
1734
+ }
1735
+ if (fact.kind === "Subexpression") {
1736
+ return isSubexpressionReported(fact, scope, unit) ? BUILTIN_LOCATION : hidden;
1737
+ }
1738
+ const name = fact.kind === "Identifier" ? fact.name : fact.kind === "Apply" ? fact.operator : "";
1739
+ if (fact.kind === "Identifier" && lookupLocal(scope, name)?.nonFactStep) {
1740
+ unit.errors.push({
1741
+ location: formatRange(getSpelledNameRange(fact, name)),
1742
+ message: "Step number of non-fact used as a fact",
1743
+ });
1744
+ return BUILTIN_LOCATION;
1745
+ }
1746
+ if (
1747
+ !isFact(name, unit) &&
1748
+ !lookupLocal(scope, name)?.goal &&
1749
+ !(fact.kind === "Identifier" && isInstanceName(name, scope, unit))
1750
+ ) {
1751
+ resolveExpression(fact, scope, unit);
1752
+ }
1753
+ return hidden;
1754
+ };
1755
+
1756
+ const isDefinitionEntry = (def: Fact, scope: LocalName | undefined, unit: Unit) => {
1757
+ if (def.kind === "ModuleFact") {
1758
+ reportUnknownModule(def, unit);
1759
+ return unit.isModuleKnown(def.name, def.location);
1760
+ }
1761
+ const range = def.location.range;
1762
+ const name =
1763
+ def.kind === "Identifier" ? def.name : def.kind === "OperatorReference" ? def.operator : "";
1764
+ if (name === "" || !range) return true;
1765
+ const local = lookupLocal(scope, name);
1766
+ const binding = local ?? findSymbol(name, unit) ?? getModuleBinding(name, unit, range);
1767
+ if (binding === undefined && isFact(name, unit)) return true;
1768
+ if (binding === undefined) {
1769
+ unit.errors.push({
1770
+ location: formatRange(range),
1771
+ message: `Unknown operator: \`${resolveSynonym(name)}'.`,
1772
+ });
1773
+ }
1774
+ if (local?.nonFactStep === "Define") return true;
1775
+ if (local?.nonFactStep) {
1776
+ unit.errors.push({
1777
+ location: formatRange(range),
1778
+ message: "DEF clause entry refers to a non-definition step.",
1779
+ });
1780
+ }
1781
+ if (binding && SELECTABLE_KINDS.has(binding.kind) && !isStepName(name)) return true;
1782
+ unit.errors.push({
1783
+ location: formatRange(range),
1784
+ message: "DEF clause entry should describe a defined operator.",
1785
+ });
1786
+ return false;
1787
+ };
1788
+
1789
+ const getHiddenExpressionLocation = (
1790
+ fact: Expression,
1791
+ reference: Fact,
1792
+ scope: LocalName | undefined,
1793
+ unit: Unit,
1794
+ ) => {
1795
+ const range = fact.location.range && formatRange(fact.location.range);
1796
+ if (selections.has(fact)) {
1797
+ return findNullError(nullSelections.get(fact))
1798
+ ? BUILTIN_LOCATION
1799
+ : reference.location.range && formatRange(reference.location.range);
1800
+ }
1801
+ if (selectionErrors.has(fact) || fact.kind === "ExceptAt") return BUILTIN_LOCATION;
1802
+ switch (fact.kind) {
1803
+ case "Number":
1804
+ case "String":
1805
+ case "Decimal":
1806
+ case "Let":
1807
+ case "Label":
1808
+ return undefined;
1809
+ case "Identifier":
1810
+ case "Apply":
1811
+ case "Subexpression": {
1812
+ const first = getSelectors(fact)[0];
1813
+ if (fact.kind === "Subexpression" || first.kind !== "Name") return undefined;
1814
+ const name = fact.kind === "Identifier" ? fact.name : fact.operator;
1815
+ const goal = lookupLocal(scope, first.name)?.goal ?? getGoal(first.name, unit);
1816
+ if (
1817
+ isFact(name, unit) ||
1818
+ goal ||
1819
+ (isStepName(name) && lookupLocal(scope, name)) ||
1820
+ (fact.kind === "Identifier" && isInstanceName(name, scope, unit))
1821
+ ) {
1822
+ return undefined;
1823
+ }
1824
+ const binding = findBinding(name, scope, unit);
1825
+ const count = fact.kind === "Apply" ? fact.args.length : 0;
1826
+ // Deviation from SANY: UseOrHideNode#factCheck crashes on a fact whose operator Generator#generateExpression could not resolve, where tla-ts reports it like a null node.
1827
+ return binding?.arity === count ? range : BUILTIN_LOCATION;
1259
1828
  }
1260
- const name =
1261
- fact.kind === "Identifier" ? fact.name : fact.kind === "Apply" ? fact.operator : "";
1262
- if (fact.kind === "Identifier" && lookupLocal(scope, name)?.isNonFact) {
1829
+ default:
1830
+ return range;
1831
+ }
1832
+ };
1833
+
1834
+ const checkUseOrHide = (
1835
+ statement: UseOrHide | Extract<Proof, { kind: "By" }>,
1836
+ scope: LocalName | undefined,
1837
+ unit: Unit,
1838
+ ) => {
1839
+ const references = factReferences.get(statement.facts) ?? statement.facts;
1840
+ const hidden = statement.facts.map((fact, index) =>
1841
+ checkFact(fact, references[index], scope, unit),
1842
+ );
1843
+ const factCount = statement.facts.filter(
1844
+ (fact) => fact.kind !== "ModuleFact" || unit.isModuleKnown(fact.name, fact.location),
1845
+ ).length;
1846
+ const defCount = statement.defs.filter((def) => isDefinitionEntry(def, scope, unit)).length;
1847
+ hidden.forEach((location) => {
1848
+ if (location && statement.kind === "Hide") {
1263
1849
  unit.errors.push({
1264
- location: formatRange(getSpelledNameRange(fact, name)),
1265
- message: "Step number of non-fact used as a fact",
1850
+ location,
1851
+ message:
1852
+ "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1266
1853
  });
1267
- } else if (!unit.facts.has(name)) resolveExpression(fact, scope, unit);
1854
+ }
1268
1855
  });
1856
+ const range = statement.location.range;
1857
+ if (factCount + defCount === 0 && range) {
1858
+ unit.errors.push({
1859
+ location: formatRange(range),
1860
+ message: statement.kind === "By" ? "Empty BY" : "Empty USE or HIDE statement.",
1861
+ });
1862
+ }
1863
+ };
1269
1864
 
1270
1865
  const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit: Unit) => {
1271
1866
  if (symbol.set) resolveExpression(symbol.set, scope, unit);
@@ -1277,8 +1872,6 @@ const resolveNewSymbol = (symbol: NewSymbol, scope: LocalName | undefined, unit:
1277
1872
  );
1278
1873
  };
1279
1874
 
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
1875
  const resolveAssumeProve = (
1283
1876
  assumeProve: AssumeProve,
1284
1877
  scope: LocalName | undefined,
@@ -1314,16 +1907,13 @@ const resolveStatement = (
1314
1907
  return scope;
1315
1908
  };
1316
1909
 
1317
- // SANY resolves the expressions of a proof, while its facts and DEF clauses may also name
1318
- // theorems, assumptions, and modules.
1319
1910
  const resolveProof = (proof: Proof | undefined, scope: LocalName | undefined, outer: Unit) => {
1320
1911
  const unit = outer.hasLabelScope ? { ...outer, hasLabelScope: false } : outer;
1321
- if (proof?.kind === "By") checkFacts(proof.facts, scope, unit);
1912
+ if (proof?.kind === "By") checkUseOrHide(proof, scope, unit);
1322
1913
  if (proof?.kind !== "Steps") return;
1323
1914
  proof.steps.reduce((current, step) => resolveStep(step, current, unit), scope);
1324
1915
  };
1325
1916
 
1326
- // A LET or a step binds the name of the INSTANCE after the names it adds.
1327
1917
  const resolveLocalInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
1328
1918
  const instanced = resolveInstance(instance, scope, unit);
1329
1919
  return instance.name === undefined
@@ -1336,8 +1926,42 @@ const resolveLocalInstance = (instance: Instance, scope: LocalName | undefined,
1336
1926
  );
1337
1927
  };
1338
1928
 
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.
1929
+ const getStepGoal = (body: StepBody, location: SourceLocation): Goal | undefined => {
1930
+ const createGoal = (keyword: string, statement: Expression) => ({
1931
+ statement,
1932
+ isSuffices: false,
1933
+ isInProof: false,
1934
+ keyword,
1935
+ });
1936
+ switch (body.kind) {
1937
+ case "Have":
1938
+ return createGoal("HAVE", { kind: "Tuple", elements: [body.expression], location });
1939
+ case "Case":
1940
+ return createGoal("CASE", { kind: "Tuple", elements: [body.expression], location });
1941
+ case "Witness":
1942
+ return createGoal("WITNESS", { kind: "Tuple", elements: body.expressions, location });
1943
+ case "Take":
1944
+ return createGoal("TAKE", { kind: "Tuple", elements: getBoundSets(body.bounds), location });
1945
+ case "Pick":
1946
+ return createGoal(
1947
+ "PICK",
1948
+ body.bounds.length > 0
1949
+ ? { kind: "Exists", bounds: body.bounds, body: body.body, location }
1950
+ : {
1951
+ kind: "UnboundedQuantifier",
1952
+ quantifier: "Exists",
1953
+ names: body.names,
1954
+ body: body.body,
1955
+ location,
1956
+ },
1957
+ );
1958
+ case "Qed":
1959
+ return createGoal("QED step", { kind: "Tuple", elements: [], location });
1960
+ default:
1961
+ return undefined;
1962
+ }
1963
+ };
1964
+
1341
1965
  const resolveStep = (
1342
1966
  step: ProofStep,
1343
1967
  scope: LocalName | undefined,
@@ -1348,15 +1972,17 @@ const resolveStep = (
1348
1972
  step.name === undefined
1349
1973
  ? unit
1350
1974
  : { ...unit, hasLabelScope: true, labelParams: [], labels: new Set<string>() };
1975
+ const goal = getStepGoal(body, step.location);
1351
1976
  const bindStep = (parent: LocalName | undefined): LocalName | undefined =>
1352
- step.name === undefined ? parent : { name: step.name, arity: 0, kind: "Theorem", parent };
1353
- const bindNonFactStep = (parent: LocalName | undefined): LocalName | undefined =>
1977
+ step.name === undefined ? parent : { name: step.name, arity: 0, kind: "Theorem", parent, goal };
1978
+ const bindNonFactStep = (
1979
+ parent: LocalName | undefined,
1980
+ nonFactStep: LocalName["nonFactStep"],
1981
+ ): LocalName | undefined =>
1354
1982
  step.name === undefined
1355
1983
  ? parent
1356
- : { name: step.name, arity: 0, kind: "Theorem", parent, isNonFact: true };
1984
+ : { name: step.name, arity: 0, kind: "Theorem", parent, nonFactStep };
1357
1985
  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
1986
  case "Assert": {
1361
1987
  const assumed = resolveStatement(body.statement, scope, stepUnit);
1362
1988
  const proofScope = body.isSuffices ? scope : assumed;
@@ -1365,17 +1991,32 @@ const resolveStep = (
1365
1991
  resolveProof(step.proof, proofScope, unit);
1366
1992
  return body.isSuffices ? assumed : scope;
1367
1993
  }
1368
- const goal = { statement: body.statement, isSuffices: body.isSuffices, isInProof: true };
1369
- resolveProof(step.proof, { name, arity: 0, kind: "Theorem", parent: proofScope, goal }, unit);
1370
- goal.isInProof = false;
1371
- return { name, arity: 0, kind: "Theorem", parent: body.isSuffices ? assumed : scope, goal };
1994
+ const assertion = {
1995
+ statement: body.statement,
1996
+ isSuffices: body.isSuffices,
1997
+ isInProof: true,
1998
+ keyword: body.isSuffices && body.statement.kind !== "AssumeProve" ? "SUFFICES" : undefined,
1999
+ };
2000
+ resolveProof(
2001
+ step.proof,
2002
+ { name, arity: 0, kind: "Theorem", parent: proofScope, goal: assertion },
2003
+ unit,
2004
+ );
2005
+ assertion.isInProof = false;
2006
+ return {
2007
+ name,
2008
+ arity: 0,
2009
+ kind: "Theorem",
2010
+ parent: body.isSuffices ? assumed : scope,
2011
+ goal: assertion,
2012
+ };
1372
2013
  }
1373
2014
  case "Use":
1374
2015
  case "Hide":
1375
- checkFacts(body.facts, scope, unit);
1376
- return bindNonFactStep(scope);
2016
+ checkUseOrHide(body, scope, unit);
2017
+ return bindNonFactStep(scope, body.kind);
1377
2018
  case "Instance":
1378
- return bindNonFactStep(resolveLocalInstance(body, scope, stepUnit));
2019
+ return bindNonFactStep(resolveLocalInstance(body, scope, stepUnit), body.kind);
1379
2020
  case "Define":
1380
2021
  return bindNonFactStep(
1381
2022
  body.definitions.reduce(
@@ -1385,6 +2026,7 @@ const resolveStep = (
1385
2026
  : resolveDefinitions([definition], current, unit),
1386
2027
  scope,
1387
2028
  ),
2029
+ body.kind,
1388
2030
  );
1389
2031
  case "Have":
1390
2032
  case "Case":
@@ -1423,7 +2065,6 @@ const resolveStep = (
1423
2065
  }
1424
2066
  };
1425
2067
 
1426
- // SANY binds the name of a theorem after its statement and before its proof.
1427
2068
  const resolveTheorem = (theorem: Theorem, unit: Unit) => {
1428
2069
  const isDefinition = theorem.name !== undefined && theorem.statement.kind !== "AssumeProve";
1429
2070
  const scope = isDefinition ? undefined : resolveStatement(theorem.statement, undefined, unit);
@@ -1440,16 +2081,12 @@ const resolveTheorem = (theorem: Theorem, unit: Unit) => {
1440
2081
  if (goal) goal.isInProof = false;
1441
2082
  };
1442
2083
 
1443
- // The declarations of the module an INSTANCE instantiates, the names the INSTANCE adds, and the
1444
- // module that instantiates it.
1445
2084
  interface InstancedModule {
1446
2085
  declarations: Declaration[];
1447
2086
  symbols: Symbols;
1448
2087
  instancer: string;
1449
2088
  }
1450
2089
 
1451
- // What SANY reports at a location as it links a module, such as a conflict between the modules it
1452
- // extends.
1453
2090
  export interface ModuleReport {
1454
2091
  location: SourceLocation;
1455
2092
  errors: SanyError[];
@@ -1459,13 +2096,11 @@ export interface ModuleReport {
1459
2096
  export interface ModuleUnit {
1460
2097
  imported: Symbols;
1461
2098
  getInstance: (instance: Instance) => InstancedModule;
1462
- // The built-in operators and the names of the enclosing and extended modules.
1463
2099
  bound: Symbols;
1464
2100
  definitions: Map<string, Definition>;
1465
2101
  getKey: (definition: Definition) => string;
1466
- // The names each INSTANCE adds.
2102
+ isModuleKnown: (name: string, location: SourceLocation) => boolean;
1467
2103
  instanced: Map<Instance, Symbols>;
1468
- // The theorems and assumptions the extended and instantiated modules export.
1469
2104
  facts: Set<string>;
1470
2105
  reports: ModuleReport[];
1471
2106
  warnings: SanyError[];
@@ -1473,7 +2108,6 @@ export interface ModuleUnit {
1473
2108
 
1474
2109
  export const MODULE_START: SourceLocation = { line: 0, column: 0 };
1475
2110
 
1476
- // A named theorem or assumption is linked as a definition too.
1477
2111
  export const isTheoremDefinition = (module: Module, definition: Definition) =>
1478
2112
  [...module.theorems, ...module.assumptions].some(
1479
2113
  (item) =>
@@ -1493,9 +2127,6 @@ const createSymbols = (module: Module, imported: Symbols) => {
1493
2127
  return symbols;
1494
2128
  };
1495
2129
 
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
2130
  const checkSubstitutions = (
1500
2131
  instance: Instance,
1501
2132
  declarations: Declaration[],
@@ -1507,18 +2138,74 @@ const checkSubstitutions = (
1507
2138
  (parent, param) => ({ name: param.name, arity: param.arity, kind: "Parameter", parent }),
1508
2139
  outer,
1509
2140
  );
2141
+ const reportArity = (declaration: Declaration, location: string) =>
2142
+ unit.errors.push({
2143
+ location,
2144
+ message: `An operator must be substituted for symbol '${resolveSynonym(declaration.name)}', and it must have arity ${declaration.arity}.`,
2145
+ });
1510
2146
  const resolveSubstitute = (declaration: Declaration, expression: Expression) => {
1511
- if (declaration.arity === 0 || expression.kind !== "Identifier") {
1512
- resolveExpression(expression, scope, unit);
2147
+ const range = expression.location.range;
2148
+ if (declaration.arity === 0) {
2149
+ resolveExpressionArgument(expression, scope, unit);
2150
+ return;
2151
+ }
2152
+ const parenthesized = parenthesizedRanges.get(expression);
2153
+ if (expression.kind === "Lambda" && !parenthesized) {
2154
+ resolveLambda(expression, scope, unit);
2155
+ if (expression.params.length !== declaration.arity && range) {
2156
+ reportArity(declaration, formatRange(range));
2157
+ }
2158
+ return;
2159
+ }
2160
+ if (expression.kind === "OperatorReference" && !parenthesized) {
2161
+ const arity = getSymbolArity(expression, expression.operator, scope, unit);
2162
+ if (arity === undefined) reportArity(declaration, BUILTIN_LOCATION);
2163
+ else if (arity !== declaration.arity && range) reportArity(declaration, formatRange(range));
2164
+ return;
2165
+ }
2166
+ if (expression.kind !== "Identifier" || parenthesized) {
2167
+ const errorRange = parenthesized ?? range;
2168
+ if (errorRange) {
2169
+ unit.errors.push({
2170
+ location: formatRange(errorRange),
2171
+ message: `Arity ${declaration.arity} operator (not an expression) is expected \nto substitute for CONSTANT '${resolveSynonym(declaration.name)}'.`,
2172
+ });
2173
+ }
2174
+ reportArity(declaration, BUILTIN_LOCATION);
1513
2175
  return;
1514
2176
  }
1515
2177
  const arity = getSymbolArity(expression, expression.name, scope, unit);
1516
2178
  if (arity !== undefined && arity !== declaration.arity) {
1517
- throw new TlaSyntaxError(
1518
- `An operator must be substituted for symbol '${declaration.name}', and it must have arity ${declaration.arity} in the instance of ${instance.module} at line ${instance.location.line}, column ${instance.location.column}`,
1519
- );
2179
+ unit.errors.push({
2180
+ location: formatRange(getSpelledNameRange(expression, expression.name)),
2181
+ message: `Expected arity ${declaration.arity} but found operator of arity ${arity}.`,
2182
+ });
1520
2183
  }
2184
+ if (arity !== declaration.arity) reportArity(declaration, BUILTIN_LOCATION);
1521
2185
  };
2186
+ const instanceRange = instance.location.range;
2187
+ const isFound = (declaration: Declaration) =>
2188
+ lookupLocal(scope, declaration.name) !== undefined ||
2189
+ findSymbol(declaration.name, unit) !== undefined;
2190
+ const implicit = declarations.filter(isFound).map((declaration) => {
2191
+ const expression: Expression & { kind: "Identifier" } = {
2192
+ kind: "Identifier",
2193
+ name: declaration.name,
2194
+ location: instance.location,
2195
+ };
2196
+ return { declaration, expression };
2197
+ });
2198
+ implicit
2199
+ .filter((item) => item.declaration.arity === 0)
2200
+ .forEach((item) => {
2201
+ const arity = getSymbolArity(item.expression, item.declaration.name, scope, unit);
2202
+ if (arity && instanceRange) {
2203
+ unit.errors.push({
2204
+ location: formatRange(instanceRange),
2205
+ message: `Wrong number of arguments (0) given to operator '${resolveSynonym(item.declaration.name)}', \nwhich requires ${arity} arguments.`,
2206
+ });
2207
+ }
2208
+ });
1522
2209
  const substitutions = new Map<string, Expression>();
1523
2210
  instance.substitutions.forEach((substitution) => {
1524
2211
  const name = substitution.name;
@@ -1541,45 +2228,41 @@ const checkSubstitutions = (
1541
2228
  });
1542
2229
  }
1543
2230
  });
1544
- const implicit = declarations.filter((declaration) => !substitutions.has(declaration.name));
1545
- const isMissing = (declaration: Declaration) =>
1546
- !lookupLocal(scope, declaration.name) && !findSymbol(declaration.name, unit);
1547
2231
  implicit
1548
- .filter((declaration) => !isMissing(declaration))
2232
+ .filter((item) => !substitutions.has(item.declaration.name))
2233
+ .forEach((item) => {
2234
+ const arity =
2235
+ item.declaration.arity === 0
2236
+ ? 0
2237
+ : getSymbolArity(item.expression, item.declaration.name, scope, unit);
2238
+ if (arity !== item.declaration.arity && instanceRange) {
2239
+ reportArity(item.declaration, formatRange(instanceRange));
2240
+ }
2241
+ substitutions.set(item.declaration.name, item.expression);
2242
+ });
2243
+ declarations
2244
+ .filter((declaration) => !substitutions.has(declaration.name))
1549
2245
  .forEach((declaration) => {
1550
- const expression: Expression = {
1551
- kind: "Identifier",
1552
- name: declaration.name,
1553
- location: instance.location,
1554
- };
1555
- resolveSubstitute(declaration, expression);
1556
- substitutions.set(declaration.name, expression);
2246
+ const declared = declaration.location.range;
2247
+ if (instanceRange && declared) {
2248
+ unit.errors.push({
2249
+ location: formatRange(instanceRange),
2250
+ message: `Substitution missing for symbol ${resolveSynonym(declaration.name)} declared at ${formatRange(declared)} \nand instantiated in module ${instancer}.`,
2251
+ });
2252
+ }
1557
2253
  });
1558
- implicit.filter(isMissing).forEach((declaration) => {
1559
- const range = instance.location.range;
1560
- const declared = declaration.location.range;
1561
- if (range && declared) {
1562
- unit.errors.push({
1563
- location: formatRange(range),
1564
- message: `Substitution missing for symbol ${declaration.name} declared at ${formatRange(declared)} \nand instantiated in module ${instancer}.`,
1565
- });
1566
- }
1567
- });
1568
2254
  return substitutions;
1569
2255
  };
1570
2256
 
1571
- // Generator.processSubst analyzes an INSTANCE in a LET or a proof, which then binds the names the
1572
- // INSTANCE adds.
1573
2257
  const resolveInstance = (instance: Instance, scope: LocalName | undefined, unit: Unit) => {
1574
2258
  const instanced = unit.getInstance(instance);
1575
2259
  checkSubstitutions(instance, instanced.declarations, instanced.instancer, scope, unit);
1576
2260
  return [...instanced.symbols].reduce(
1577
- (parent, [name, symbol]) => bindName(parent, name, symbol, unit),
2261
+ (parent, entry) => bindName(parent, entry[0], entry[1], unit),
1578
2262
  scope,
1579
2263
  );
1580
2264
  };
1581
2265
 
1582
- // tla-ts reports what SANY goes on after with an INSTANCE of the module.
1583
2266
  export const resolveSubstitutions = (
1584
2267
  instance: Instance,
1585
2268
  declarations: Declaration[],
@@ -1587,6 +2270,12 @@ export const resolveSubstitutions = (
1587
2270
  unit: ModuleUnit,
1588
2271
  ) => {
1589
2272
  const imported = createSymbols(module, unit.imported);
2273
+ const positions = new Map(
2274
+ [...module.constants, ...module.variables].map((declaration) => [
2275
+ declaration.name,
2276
+ declaration.location,
2277
+ ]),
2278
+ );
1590
2279
  const report: ModuleReport = { location: instance.location, errors: [], warnings: [] };
1591
2280
  unit.reports.push(report);
1592
2281
  return checkSubstitutions(instance, declarations, module.name, undefined, {
@@ -1594,12 +2283,16 @@ export const resolveSubstitutions = (
1594
2283
  getInstance: unit.getInstance,
1595
2284
  definitions: unit.definitions,
1596
2285
  getKey: unit.getKey,
2286
+ isModuleKnown: unit.isModuleKnown,
1597
2287
  location: instance.location,
1598
2288
  self: undefined,
1599
2289
  facts: new Set(),
1600
2290
  goals: new Map(),
1601
2291
  bindings: new Map(
1602
- [...imported].map(([name, symbol]) => [name, { ...symbol, position: MODULE_START }]),
2292
+ [...imported].map((entry) => [
2293
+ entry[0],
2294
+ { ...entry[1], position: positions.get(entry[0]) ?? MODULE_START },
2295
+ ]),
1603
2296
  ),
1604
2297
  errors: report.errors,
1605
2298
  warnings: report.warnings,
@@ -1612,17 +2305,12 @@ export const resolveSubstitutions = (
1612
2305
  });
1613
2306
  };
1614
2307
 
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
- ) => {
2308
+ interface SemanticUnit {
2309
+ location: SourceLocation;
2310
+ check: (reported: SanyError[], warned: SanyError[]) => void;
2311
+ }
2312
+
2313
+ export const collectSemanticErrors = (units: SemanticUnit[], warnings: SanyError[]) => {
1626
2314
  const errors: SanyError[] = [];
1627
2315
  const addUnique = (list: SanyError[], item: SanyError) => {
1628
2316
  if (
@@ -1634,13 +2322,7 @@ export const collectSemanticErrors = (
1634
2322
  }
1635
2323
  };
1636
2324
  units
1637
- .toSorted((left, right) =>
1638
- isBefore(left.location, right.location)
1639
- ? -1
1640
- : isBefore(right.location, left.location)
1641
- ? 1
1642
- : 0,
1643
- )
2325
+ .toSorted((left, right) => compareLocations(left.location, right.location))
1644
2326
  .forEach((unit) => {
1645
2327
  const reported: SanyError[] = [];
1646
2328
  const warned: SanyError[] = [];
@@ -1658,66 +2340,105 @@ export const collectSemanticErrors = (
1658
2340
  return errors;
1659
2341
  };
1660
2342
 
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
2343
  export const resolveModule = (module: Module, unit: ModuleUnit) => {
1664
2344
  const symbols = createSymbols(module, unit.imported);
1665
- const facts = new Set([
1666
- ...unit.facts,
1667
- ...[...module.theorems, ...module.assumptions].flatMap((fact) => fact.name ?? []),
1668
- ]);
1669
2345
  const goals = new Map(
1670
2346
  module.theorems.flatMap((theorem): [string, Goal][] =>
1671
2347
  theorem.name !== undefined && theorem.statement.kind === "AssumeProve"
1672
- ? [[theorem.name, { statement: theorem.statement, isSuffices: false, isInProof: false }]]
2348
+ ? [
2349
+ [
2350
+ theorem.name,
2351
+ {
2352
+ statement: theorem.statement,
2353
+ isSuffices: false,
2354
+ isInProof: false,
2355
+ keyword: undefined,
2356
+ },
2357
+ ],
2358
+ ]
1673
2359
  : [],
1674
2360
  ),
1675
2361
  );
1676
2362
  const bindings = new Map<string, ModuleBinding>(
1677
- [...CORE_SYMBOLS, ...unit.bound].map(([name, symbol]) => [
1678
- name,
1679
- { ...symbol, position: MODULE_START },
2363
+ [...CORE_SYMBOLS, ...unit.bound].map((entry) => [
2364
+ entry[0],
2365
+ { ...entry[1], position: MODULE_START },
1680
2366
  ]),
1681
2367
  );
1682
2368
  const recursive = new Set<Binding>();
1683
2369
  const declarations: { declaration: Declaration; binding: ModuleBinding | undefined }[] = [];
1684
- const createUnit = (
2370
+ const createCheck = (
1685
2371
  location: SourceLocation,
1686
2372
  self: Definition | undefined,
1687
- errors: SanyError[],
1688
- warnings: SanyError[],
1689
2373
  hasLabelScope: boolean,
1690
- ): Unit => ({
1691
- imported: symbols,
1692
- getInstance: unit.getInstance,
1693
- definitions: unit.definitions,
1694
- getKey: unit.getKey,
2374
+ check: (inner: Unit) => void,
2375
+ ): SemanticUnit => ({
1695
2376
  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,
2377
+ check: (errors, warnings) =>
2378
+ check({
2379
+ imported: symbols,
2380
+ getInstance: unit.getInstance,
2381
+ definitions: unit.definitions,
2382
+ getKey: unit.getKey,
2383
+ isModuleKnown: unit.isModuleKnown,
2384
+ location,
2385
+ self,
2386
+ facts: unit.facts,
2387
+ goals,
2388
+ bindings,
2389
+ errors,
2390
+ warnings,
2391
+ hasLabelScope,
2392
+ isLabelForbidden: false,
2393
+ isInExcept: false,
2394
+ labelParams: [],
2395
+ labels: new Set(),
2396
+ isGenerated: false,
2397
+ }),
1708
2398
  });
1709
2399
  const bindDeclarations = (declarations: Declaration[], kind: "Constant" | "Variable") =>
1710
- declarations.map((declaration) => ({
1711
- location: declaration.location,
1712
- check: (errors: SanyError[], warnings: SanyError[]) =>
2400
+ declarations.map((declaration) =>
2401
+ createCheck(declaration.location, undefined, false, (inner) =>
1713
2402
  bindModuleName(
1714
2403
  declaration.name,
1715
2404
  { kind, arity: declaration.arity, range: declaration.location.range },
1716
- createUnit(declaration.location, undefined, errors, warnings, false),
2405
+ inner,
1717
2406
  ),
1718
- }));
2407
+ ),
2408
+ );
1719
2409
  return collectSemanticErrors(
1720
2410
  [
2411
+ ...module.units.flatMap((item): SemanticUnit[] => {
2412
+ const section =
2413
+ item.kind === "Variables"
2414
+ ? { type: "A VARIABLE declaration", location: item.location }
2415
+ : item.kind === "Constants"
2416
+ ? { type: "A declaration", location: item.location }
2417
+ : item.kind === "Module"
2418
+ ? { type: "A MODULE ", location: item.module.location }
2419
+ : item.kind === "Theorem"
2420
+ ? { type: "A THEOREM", location: item.theorem.location }
2421
+ : item.kind === "Assumption"
2422
+ ? { type: "An ASSUME", location: item.assumption.location }
2423
+ : item.kind === "Use"
2424
+ ? { type: "A USE or HIDE", location: item.use.location }
2425
+ : undefined;
2426
+ const range = section?.location.range;
2427
+ if (!section || !range) return [];
2428
+ return [
2429
+ {
2430
+ location: section.location,
2431
+ check: (errors) => {
2432
+ if (declarations.some((declared) => declared.binding?.isPending)) {
2433
+ errors.push({
2434
+ location: formatRange(range),
2435
+ message: `${section.type} may not appear within a recursive definition section.`,
2436
+ });
2437
+ }
2438
+ },
2439
+ },
2440
+ ];
2441
+ }),
1721
2442
  ...unit.reports.map((report) => ({
1722
2443
  location: report.location,
1723
2444
  check: (errors: SanyError[], warnings: SanyError[]) => {
@@ -1729,12 +2450,12 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1729
2450
  ...bindDeclarations(module.variables, "Variable"),
1730
2451
  ...module.units
1731
2452
  .flatMap((item) => (item.kind === "Recursive" ? item.declarations : []))
1732
- .map((declaration) => ({
1733
- location: declaration.location,
1734
- check: (errors: SanyError[], warnings: SanyError[]) => {
2453
+ .map((declaration) =>
2454
+ createCheck(declaration.location, undefined, false, (inner) => {
1735
2455
  const binding: ModuleBinding = {
1736
2456
  kind: "Definition",
1737
2457
  arity: declaration.arity,
2458
+ params: Array(declaration.arity).fill(0),
1738
2459
  range: declaration.location.range,
1739
2460
  position: declaration.location,
1740
2461
  isPending: true,
@@ -1743,19 +2464,17 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1743
2464
  declaration.name,
1744
2465
  bindings.get(declaration.name),
1745
2466
  binding,
1746
- createUnit(declaration.location, undefined, errors, warnings, false),
2467
+ inner,
1747
2468
  );
1748
2469
  if (isBound) {
1749
2470
  bindings.set(declaration.name, binding);
1750
2471
  recursive.add(binding);
1751
2472
  }
1752
2473
  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);
2474
+ }),
2475
+ ),
2476
+ ...module.definitions.map((definition) =>
2477
+ createCheck(definition.location, definition, true, (inner) => {
1759
2478
  if (isTheoremDefinition(module, definition)) {
1760
2479
  resolveExpression(definition.body, undefined, inner);
1761
2480
  } else if (resolveDefinition(definition, undefined, inner, recursive)) {
@@ -1766,12 +2485,10 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1766
2485
  position: definition.location,
1767
2486
  });
1768
2487
  }
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);
2488
+ }),
2489
+ ),
2490
+ ...module.instances.map((instance) =>
2491
+ createCheck(instance.location, undefined, false, (inner) => {
1775
2492
  bindParams(undefined, instance.params, inner);
1776
2493
  unit.instanced
1777
2494
  .get(instance)
@@ -1783,18 +2500,10 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1783
2500
  inner,
1784
2501
  );
1785
2502
  }
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
- );
2503
+ }),
2504
+ ),
2505
+ ...module.assumptions.map((assumption) =>
2506
+ createCheck(assumption.location, undefined, assumption.name !== undefined, (inner) => {
1798
2507
  resolveExpression(assumption.expression, undefined, inner);
1799
2508
  if (assumption.name !== undefined) {
1800
2509
  bindModuleName(
@@ -1803,25 +2512,18 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1803
2512
  inner,
1804
2513
  );
1805
2514
  }
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
- })),
2515
+ }),
2516
+ ),
2517
+ ...module.theorems.map((theorem) =>
2518
+ createCheck(theorem.location, undefined, theorem.name !== undefined, (inner) =>
2519
+ resolveTheorem(theorem, inner),
2520
+ ),
2521
+ ),
2522
+ ...module.uses.map((use) =>
2523
+ createCheck(use.location, undefined, false, (inner) =>
2524
+ checkUseOrHide(use, undefined, inner),
2525
+ ),
2526
+ ),
1825
2527
  {
1826
2528
  location: module.location.range?.end ?? module.location,
1827
2529
  check: (errors: SanyError[]) =>
@@ -1830,7 +2532,7 @@ export const resolveModule = (module: Module, unit: ModuleUnit) => {
1830
2532
  if (range && (!item.binding || item.binding.isPending)) {
1831
2533
  errors.push({
1832
2534
  location: formatRange(range),
1833
- message: `Symbol ${item.declaration.name} declared in RECURSIVE statement but not defined.`,
2535
+ message: `Symbol ${resolveSynonym(item.declaration.name)} declared in RECURSIVE statement but not defined.`,
1834
2536
  });
1835
2537
  }
1836
2538
  }),