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/parser.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type {
2
2
  AssumeProve,
3
3
  Assumption,
4
- Bound,
5
4
  BoundPattern,
6
5
  CaseArm,
7
6
  Declaration,
@@ -22,7 +21,7 @@ import type {
22
21
  StepBody,
23
22
  UseOrHide,
24
23
  } from "./ast";
25
- import { createSanyError, formatRange, TlaSyntaxError } from "./errors";
24
+ import { TlaSyntaxError } from "./errors";
26
25
  import { getNumeralValue, readAtAsIdentifier, tokenize, type Token } from "./lexer";
27
26
  import { getSanyParseError } from "./sany/parse-error";
28
27
 
@@ -35,16 +34,26 @@ interface InfixOperator extends Precedence {
35
34
  isAssociative: boolean;
36
35
  }
37
36
 
38
- // TLAplusParser.lastOp, the operator parsed last, and the operator an operand being parsed follows
39
- // in its expression.
40
- interface OperatorContext {
41
- last?: Precedence;
42
- preceding?: Precedence;
37
+ interface TlapmOperand {
38
+ start: number;
39
+ end: number;
40
+ expression: Expression;
41
+ joinedBy?: string;
43
42
  }
44
43
 
45
- // SANY's precedences of a postfix operator and of the "[" of a function application.
46
- const POSTFIX_PRECEDENCE: Precedence = { low: 15, high: 15 };
47
- const FUNCTION_PRECEDENCE: Precedence = { low: 16, high: 16 };
44
+ interface TlapmOperator extends Precedence {
45
+ start: number;
46
+ end: number;
47
+ fixity: "Prefix" | "Infix" | "Postfix";
48
+ isLeft: boolean;
49
+ joins?: string;
50
+ apply: (operands: TlapmOperand[]) => Expression;
51
+ }
52
+
53
+ type TlapmItem = TlapmOperand | TlapmOperator;
54
+
55
+ const POSTFIX_PRECEDENCE = { low: 15, high: 15 };
56
+ const FUNCTION_PRECEDENCE = { low: 16, high: 16 };
48
57
 
49
58
  const createInfix = (low: number, high: number, isAssociative: boolean) => ({
50
59
  low,
@@ -152,11 +161,55 @@ const INFIX_OPERATORS = new Map<string, InfixOperator>([
152
161
 
153
162
  const NEGATION_PRECEDENCE = 12;
154
163
 
164
+ // tlapm's Optable has SANY's infix precedences, groups % and \X to the left with the operators
165
+ // SANY takes as associative, and has these prefix precedences. A function application and a field
166
+ // bind above every operator.
167
+ const isTlapmLeft = (text: string, infix: InfixOperator) =>
168
+ infix.isAssociative || text === "%" || text === "\\X";
169
+
170
+ const TLAPM_PREFIX_OPERATORS = new Map<string, Precedence>([
171
+ ["~", { low: 4, high: 4 }],
172
+ ["[]", { low: 4, high: 15 }],
173
+ ["<>", { low: 4, high: 15 }],
174
+ ["ENABLED", { low: 4, high: 15 }],
175
+ ["UNCHANGED", { low: 4, high: 15 }],
176
+ ["SUBSET", { low: 8, high: 8 }],
177
+ ["UNION", { low: 8, high: 8 }],
178
+ ["DOMAIN", { low: 9, high: 9 }],
179
+ ["-", { low: 12, high: 12 }],
180
+ ["-.", { low: 12, high: 12 }],
181
+ ]);
182
+
183
+ const TLAPM_SELECTION_PRECEDENCE = { low: 17, high: 17 };
184
+
185
+ const TLAPM_ATOM_STARTS = new Set([
186
+ "(",
187
+ "[",
188
+ "{",
189
+ "<<",
190
+ "@",
191
+ "\\A",
192
+ "\\E",
193
+ "\\AA",
194
+ "\\EE",
195
+ "IF",
196
+ "CASE",
197
+ "LET",
198
+ "CHOOSE",
199
+ "LAMBDA",
200
+ "WF_",
201
+ "SF_",
202
+ ]);
203
+
204
+ // The brackets whose contents tlapm reads without committing, so a failure in them backtracks.
205
+ const TLAPM_OPEN_BRACKETS = new Set(["(", "{", "<<"]);
206
+
155
207
  const POSTFIX_OPERATORS = new Set(["^+", "^*", "^#"]);
156
208
 
209
+ const DEFINABLE_POSTFIX_OPERATORS = new Set([...POSTFIX_OPERATORS, "'"]);
210
+
157
211
  const PREFIX_OPERATOR_REFERENCES = new Set(["-", "~", "-."]);
158
212
 
159
- // The prefix operators SANY reads in a definition's left-hand side, built-in ones included.
160
213
  const DEFINABLE_PREFIX_OPERATORS = new Set([
161
214
  "-.",
162
215
  "~",
@@ -193,12 +246,12 @@ const OPERATOR_ARGUMENTS = new Set([
193
246
  "UNCHANGED",
194
247
  "[]",
195
248
  "<>",
196
- "'",
197
- ...POSTFIX_OPERATORS,
249
+ ...DEFINABLE_POSTFIX_OPERATORS,
198
250
  ]);
199
251
 
200
252
  const SELECTOR_SYMBOLS = new Set(["<<", ">>", ":", "@", "("]);
201
253
 
254
+ // Deviation from SANY: TLAplusParser#OpOrExpr's lookahead omits PROPOSITION, LEMMA and COROLLARY, so SANY parses an operator argument before them as an expression.
202
255
  const UNIT_KEYWORDS = new Set([
203
256
  "ASSUME",
204
257
  "ASSUMPTION",
@@ -259,6 +312,96 @@ const TLAPM_PUNCTUATION = new Set([
259
312
 
260
313
  const UNNAMED_STEP_LABELS = new Set(["", "*", "-"]);
261
314
 
315
+ interface Point {
316
+ line: number;
317
+ column: number;
318
+ }
319
+
320
+ interface Span {
321
+ start: Point;
322
+ stop: Point;
323
+ }
324
+
325
+ interface RangedName {
326
+ name: string;
327
+ range: SourceRange;
328
+ }
329
+
330
+ type Preproof =
331
+ | { kind: "Leaf"; proof: Proof; span: Span; first: number; end: number }
332
+ | {
333
+ kind: "Step";
334
+ token: Token;
335
+ isProof: boolean;
336
+ body: StepBody;
337
+ head: SourceRange;
338
+ statement: SourceRange;
339
+ span: Span;
340
+ first: number;
341
+ start: number;
342
+ end: number;
343
+ };
344
+
345
+ interface NestedProof {
346
+ proof: Proof | undefined;
347
+ span: Span;
348
+ next: number;
349
+ end: number | undefined;
350
+ }
351
+
352
+ interface NestedSteps {
353
+ level: number;
354
+ steps: ProofStep[];
355
+ span: Span;
356
+ next: number;
357
+ end: number;
358
+ }
359
+
360
+ const LEAF_NAMES: Record<string, string> = {
361
+ Obvious: "n OBVIOUS",
362
+ Omitted: "n OMITTED",
363
+ By: " BY",
364
+ };
365
+
366
+ const comparePoints = (left: Point, right: Point) =>
367
+ left.line - right.line || left.column - right.column;
368
+
369
+ const mergeSpans = (left: Span, right: Span): Span => ({
370
+ start: comparePoints(left.start, right.start) <= 0 ? left.start : right.start,
371
+ stop: comparePoints(left.stop, right.stop) >= 0 ? left.stop : right.stop,
372
+ });
373
+
374
+ const getSpan = (range: SourceRange): Span => ({
375
+ start: { line: range.start.line, column: range.start.column },
376
+ stop: { line: range.end.line, column: range.end.column + 1 },
377
+ });
378
+
379
+ interface ProofErrorEnds {
380
+ printed: string;
381
+ set: string;
382
+ }
383
+
384
+ const NO_NEWLINES: ProofErrorEnds = { printed: "", set: "" };
385
+
386
+ const PRINTED_NEWLINE: ProofErrorEnds = { printed: "\n", set: "" };
387
+
388
+ const NEWLINES: ProofErrorEnds = { printed: "\n", set: "\n" };
389
+
390
+ // p_parser.ml ends some of the messages it prints, and of those it sets, with a newline.
391
+ const createProofError = (
392
+ message: string,
393
+ span: Span,
394
+ ends: ProofErrorEnds,
395
+ exception = "Proof.Parser",
396
+ ) =>
397
+ new TlaSyntaxError(message, undefined, {
398
+ reason: `${message}${ends.set}`,
399
+ line: span.start.line,
400
+ column: span.start.column,
401
+ isEnd: false,
402
+ proof: { stop: span.stop, exception, printed: `${message}${ends.printed}` },
403
+ });
404
+
262
405
  const FACT_KEYWORDS = new Set([
263
406
  "CASE",
264
407
  "CHOOSE",
@@ -297,7 +440,6 @@ const BUILTIN_CONSTANTS = new Set(["TRUE", "FALSE", "BOOLEAN", "STRING"]);
297
440
 
298
441
  const NO_OFFSIDE = -1;
299
442
 
300
- // A SANY InfixExpr or PostfixExpr.
301
443
  const isOperatorExpression = (expression: Expression) =>
302
444
  expression.kind === "Prime" ||
303
445
  expression.kind === "Product" ||
@@ -307,6 +449,11 @@ const isOperatorExpression = (expression: Expression) =>
307
449
  ? INFIX_OPERATORS.has(expression.operator)
308
450
  : POSTFIX_OPERATORS.has(expression.operator)));
309
451
 
452
+ const isBullet = (token: Token) =>
453
+ (token.text === "/\\" || token.text === "\\/") &&
454
+ token.spelling !== "\\land" &&
455
+ token.spelling !== "\\lor";
456
+
310
457
  const isOperatorName = (text: string) =>
311
458
  INFIX_OPERATORS.has(text) || POSTFIX_OPERATORS.has(text) || text === "-.";
312
459
 
@@ -331,34 +478,37 @@ const getOperatorApplication = (
331
478
  }
332
479
  };
333
480
 
334
- // MonolithSpecExtractor.module: a module that no file defines can follow the root module in the
335
- // root module's file, from its ---- MODULE line to the next ==== line.
481
+ export const parenthesizedRanges = new WeakMap<Expression, SourceRange>();
482
+
336
483
  export const extractModule = (source: string, name: string) => {
337
484
  const header = new RegExp(`^-{4,}\\s*MODULE\\s+${name}\\s*-{3,}$`);
338
- const lines = source.split(/\r?\n/);
485
+ const lines = source.replace(/\r?\n$/, "").split(/\r?\n/);
339
486
  const start = lines.findIndex((line) => header.test(line));
340
487
  if (start === -1) return undefined;
341
488
  const end = lines.findIndex((line, index) => index > start && line.startsWith("===="));
342
489
  return `${lines.slice(start, end === -1 ? undefined : end + 1).join("\n")}\n`;
343
490
  };
344
491
 
345
- // The tlapm mode parses as tlapm does for proof obligations, which accepts and rejects some modules
346
- // that SANY does not. The sany mode leaves the checks that SANY makes after parsing to the SANY
347
- // generator.
348
492
  type ParseMode = "tlc" | "tlapm" | "sany";
349
493
 
350
- const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source)): Module => {
494
+ const parse = (
495
+ source: string,
496
+ mode: ParseMode,
497
+ sourceTokens = tokenize(source, mode === "tlapm"),
498
+ ): Module => {
351
499
  const isTlapm = mode === "tlapm";
352
500
  const tokens = isTlapm ? sourceTokens : sourceTokens.map(readAtAsIdentifier);
353
- const cursor = { index: 0, offsideColumns: [] as number[] };
354
- const operators: OperatorContext = {};
355
- // A RECURSIVE declaration covers the definitions of its own LET or module.
501
+ let cursor = 0;
502
+ const offsideColumns: number[] = [];
503
+ let lastOperator: Precedence | undefined;
504
+ let precedingOperator: Precedence | undefined;
356
505
  const recursiveScopes: Set<string>[] = [];
357
- const factNames = new Set<string>();
358
- // The NEW, TAKE, and PICK names in scope, which a DEF clause cannot cite.
359
- const proofScopes: Set<string>[] = [];
360
- // SANY reports a location in a nested module as one in the module of its file.
361
506
  const openModules: Module[] = [];
507
+ // Pco's backtracking: a failure not past a commit rewinds to its attempt's frame.
508
+ let attemptFrame: number | undefined;
509
+ let attemptCount = 0;
510
+ const restorePoints: number[] = [];
511
+ const backtracks = new WeakMap<TlaSyntaxError, number>();
362
512
 
363
513
  const isRecursiveName = (name: string) => recursiveScopes[recursiveScopes.length - 1].has(name);
364
514
 
@@ -372,7 +522,7 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
372
522
  const tokenEnds: SourceLocation[] = [];
373
523
  const tokenRanges: SourceRange[] = [];
374
524
 
375
- const getRange = (startIndex: number, endIndex = cursor.index - 1) => {
525
+ const getRange = (startIndex: number, endIndex = cursor - 1) => {
376
526
  const cached = startIndex === endIndex ? tokenRanges[startIndex] : undefined;
377
527
  if (cached) return cached;
378
528
  const last = tokens[endIndex];
@@ -387,17 +537,16 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
387
537
  return range;
388
538
  };
389
539
 
390
- // Each node gets its own location object from getLocation, so the range can be set in place.
391
540
  const withRange = <Node extends { location: SourceLocation }>(node: Node, startIndex: number) => {
392
541
  node.location.range = getRange(startIndex);
393
542
  return node;
394
543
  };
395
544
 
396
- const peekRaw = (offset = 0) => tokens[Math.min(cursor.index + offset, tokens.length - 1)];
545
+ const peekRaw = (offset = 0) => tokens[Math.min(cursor + offset, tokens.length - 1)];
397
546
 
398
547
  const peek = (offset = 0) => {
399
548
  const token = peekRaw(offset);
400
- const offsideColumn = cursor.offsideColumns[cursor.offsideColumns.length - 1];
549
+ const offsideColumn = offsideColumns[offsideColumns.length - 1];
401
550
  if (offsideColumn === undefined || offsideColumn === NO_OFFSIDE || token.kind === "End") {
402
551
  return token;
403
552
  }
@@ -407,20 +556,10 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
407
556
  const describeToken = (token: Token) =>
408
557
  token.kind === "End" ? "end of input" : JSON.stringify(token.text);
409
558
 
410
- const unitStart = { index: 0 };
411
-
412
- const getDefinitionEnd = (fallback: { line: number; column: number }) => {
413
- const equals = tokens
414
- .slice(unitStart.index, cursor.index)
415
- .find((token) => token.text === "==" && token.kind !== "String");
416
- return equals ? { line: equals.line, column: equals.endColumn + 1 } : fallback;
417
- };
418
-
419
- // tlapm reads TRUE, FALSE, BOOLEAN, STRING and the other keywords only as keywords.
420
559
  const isTlapmKeyword = (token: Token) =>
421
560
  isTlapm && (token.kind === "Keyword" || BUILTIN_CONSTANTS.has(token.text));
422
561
 
423
- const getPreviousEnd = (position: { line: number; column: number }) => {
562
+ const getPreviousEnd = (position: Point) => {
424
563
  const previous = tokens.findLast(
425
564
  (token) =>
426
565
  token.kind !== "End" &&
@@ -431,53 +570,90 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
431
570
  };
432
571
 
433
572
  const getTlapmName = (token: Token) => {
573
+ if (token.kind === "End") return "EOF";
434
574
  if (/^-{4,}$/.test(token.text)) return "----";
435
575
  if (/^={4,}$/.test(token.text)) return "====";
436
576
  if (TLAPM_PUNCTUATION.has(token.text) || token.kind === "Number" || token.kind === "Decimal")
437
577
  return token.text;
438
578
  if (token.kind === "String") return JSON.stringify(token.text);
579
+ if (token.kind === "Step") return token.text;
439
580
  if (isTlapmKeyword(token)) return `keyword ${token.text}`;
440
581
  return token.kind === "Identifier" ? `identifier ${token.text}` : `operator ${token.text}`;
441
582
  };
442
583
 
443
- const createError = (
444
- message: string,
445
- token: { line: number; column: number } = peekRaw(),
446
- unexpected?: Token,
447
- ) => {
584
+ const createError = (message: string, token: Point = peekRaw(), unexpected?: Token) => {
448
585
  const end = tokens[tokens.length - 1];
449
586
  const isEnd = token.line === end.line && token.column === end.column;
450
- return new TlaSyntaxError(
587
+ const error = new TlaSyntaxError(
451
588
  `${message} at line ${token.line}, column ${token.column}`,
452
589
  undefined,
453
590
  getFailure(message, token, isEnd, unexpected),
454
591
  );
592
+ if (attemptFrame !== undefined) backtracks.set(error, attemptFrame);
593
+ return error;
455
594
  };
456
595
 
596
+ // tlapm reports a failure after the last token it read before the outermost attempt around it.
457
597
  const getFailure = (
458
598
  message: string,
459
- token: { line: number; column: number },
599
+ token: Point,
460
600
  isEnd: boolean,
461
601
  unexpected: Token | undefined,
462
602
  ) => {
463
- if (isTlapm && isEnd) {
464
- const end = tokens[tokens.length - 1];
465
- return { reason: "Unexpected EOF", ...getDefinitionEnd(getPreviousEnd(end)), isEnd };
466
- }
467
- if (isTlapm && unexpected) {
468
- return {
469
- reason: `Unexpected ${getTlapmName(unexpected)}`,
470
- ...getPreviousEnd(unexpected),
471
- isEnd,
472
- };
473
- }
474
- return { reason: message, line: token.line, column: token.column, isEnd };
603
+ if (!isTlapm) return { reason: message, line: token.line, column: token.column, isEnd };
604
+ const restored = tokens[restorePoints[0] - 1];
605
+ const reason = unexpected ? `Unexpected ${getTlapmName(unexpected)}` : message;
606
+ if (restored) return { reason, line: restored.line, column: restored.endColumn + 1, isEnd };
607
+ const position = unexpected ? getPreviousEnd(unexpected) : token;
608
+ return { reason, line: position.line, column: position.column, isEnd };
475
609
  };
476
610
 
477
611
  const createUnexpected = (message: string, token: Token = peekRaw()) =>
478
612
  createError(message, token, token);
479
613
 
480
- // TLAplusParser.isFieldNameToken: a keyword or an underscore names a field or a module too.
614
+ // Pco.attempt: a failure that backtracks to it leaves the input as it was, and gives undefined.
615
+ const attempt = <Result>(parse: () => Result): Result | undefined => {
616
+ const saved = {
617
+ cursor,
618
+ frame: attemptFrame,
619
+ restorePoints: restorePoints.length,
620
+ offsideColumns: offsideColumns.length,
621
+ recursiveScopes: recursiveScopes.length,
622
+ };
623
+ const frame = ++attemptCount;
624
+ attemptFrame = frame;
625
+ try {
626
+ const result = parse();
627
+ attemptFrame = saved.frame;
628
+ return result;
629
+ } catch (error) {
630
+ if (!(error instanceof TlaSyntaxError) || backtracks.get(error) !== frame) throw error;
631
+ cursor = saved.cursor;
632
+ attemptFrame = saved.frame;
633
+ restorePoints.length = saved.restorePoints;
634
+ offsideColumns.length = saved.offsideColumns;
635
+ recursiveScopes.length = saved.recursiveScopes;
636
+ return undefined;
637
+ }
638
+ };
639
+
640
+ // Pco.commit: a failure in parse no longer backtracks.
641
+ const commit = <Result>(parse: () => Result) => {
642
+ const frame = attemptFrame;
643
+ attemptFrame = undefined;
644
+ const result = parse();
645
+ attemptFrame = frame;
646
+ return result;
647
+ };
648
+
649
+ const withRestorePoint = <Result>(parse: () => Result) => {
650
+ restorePoints.push(cursor);
651
+ const result = parse();
652
+ restorePoints.pop();
653
+ return result;
654
+ };
655
+
656
+ // Deviation from SANY: TLAplusParser#belchDEF reads a field named THEOREM, AXIOM, DOMAIN or a like keyword as that keyword, so SANY rejects the definition after it.
481
657
  const isFieldName = (token: Token) =>
482
658
  (token.kind === "Identifier" ||
483
659
  token.kind === "Keyword" ||
@@ -491,14 +667,14 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
491
667
 
492
668
  const advance = () => {
493
669
  const token = peek();
494
- if (token.kind === "End") throw createError("Unexpected end of expression");
495
- cursor.index++;
670
+ if (token.kind === "End") throw createUnexpected("Unexpected end of expression");
671
+ cursor++;
496
672
  return token;
497
673
  };
498
674
 
499
675
  const accept = (text: string) => {
500
676
  if (!isAt(text)) return false;
501
- cursor.index++;
677
+ cursor++;
502
678
  return true;
503
679
  };
504
680
 
@@ -514,49 +690,44 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
514
690
  throw createUnexpected(`Expected an identifier but found ${describeToken(token)}`);
515
691
  }
516
692
  if (isTlapmKeyword(token)) throw createUnexpected(`Unexpected keyword ${token.text}`);
517
- cursor.index++;
693
+ cursor++;
518
694
  return token.text;
519
695
  };
520
696
 
521
697
  const expectName = () => {
522
- const start = cursor.index;
698
+ const start = cursor;
523
699
  return { name: expectIdentifier(), range: getRange(start) };
524
700
  };
525
701
 
526
- const getNames = (names: { name: string; range: SourceRange }[]) => ({
527
- names: names.map((item) => item.name),
528
- ranges: names.map((item) => item.range),
702
+ const getNames = (names: RangedName[]) => ({
703
+ names: names.map((named) => named.name),
704
+ ranges: names.map((named) => named.range),
529
705
  });
530
706
 
531
- // TLAplusParser.Substitution: an identifier, or a prefix, infix or postfix operator.
532
707
  const expectSubstitutionTarget = () => {
533
708
  const token = peek();
534
709
  const isOperator =
535
710
  token.kind !== "String" &&
536
711
  (DEFINABLE_PREFIX_OPERATORS.has(token.text) ||
537
712
  INFIX_OPERATORS.has(token.text) ||
538
- POSTFIX_OPERATORS.has(token.text) ||
539
- token.text === "'");
713
+ DEFINABLE_POSTFIX_OPERATORS.has(token.text));
540
714
  if (!isOperator) return expectIdentifier();
541
- cursor.index++;
715
+ cursor++;
542
716
  return token.text;
543
717
  };
544
718
 
545
- // SANY takes a keyword for the name of a module, as it does for a field.
546
- const keywordModuleName = { isUsed: false };
719
+ let isKeywordModuleName = false;
547
720
  const expectModuleName = () => {
548
721
  if (peek().kind === "Identifier" || !isFieldName(peek())) return expectIdentifier();
549
- keywordModuleName.isUsed = true;
722
+ isKeywordModuleName = true;
550
723
  return advance().text;
551
724
  };
552
725
 
553
- // tlapm reads a bracketed expression apart from the junction list around it, whose alignment
554
- // SANY keeps applying.
555
726
  const withBracketLayout = <Result>(parse: () => Result) => {
556
727
  if (!isTlapm) return parse();
557
- cursor.offsideColumns.push(NO_OFFSIDE);
728
+ offsideColumns.push(NO_OFFSIDE);
558
729
  const result = parse();
559
- cursor.offsideColumns.pop();
730
+ offsideColumns.pop();
560
731
  return result;
561
732
  };
562
733
 
@@ -566,17 +737,17 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
566
737
  return items;
567
738
  };
568
739
 
569
- const isBoundAhead = () => {
570
- if (isAt("<<")) {
571
- const offset = { value: 1 };
572
- while (peek(offset.value).kind === "Identifier" || isAt(",", offset.value)) offset.value++;
573
- return isAt(">>", offset.value) && isAt("\\in", offset.value + 1);
740
+ const isBoundAhead = (start = 0) => {
741
+ if (isAt("<<", start)) {
742
+ let offset = start + 1;
743
+ while (peek(offset).kind === "Identifier" || isAt(",", offset)) offset++;
744
+ return isAt(">>", offset) && isAt("\\in", offset + 1);
574
745
  }
575
- const offset = { value: 0 };
576
- while (peek(offset.value).kind === "Identifier") {
577
- if (isAt("\\in", offset.value + 1)) return true;
578
- if (!isAt(",", offset.value + 1)) return false;
579
- offset.value += 2;
746
+ let offset = start;
747
+ while (peek(offset).kind === "Identifier") {
748
+ if (isAt("\\in", offset + 1)) return true;
749
+ if (!isAt(",", offset + 1)) return false;
750
+ offset += 2;
580
751
  }
581
752
  return false;
582
753
  };
@@ -584,14 +755,9 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
584
755
  const isLabelAhead = () => {
585
756
  if (isAt("::")) return true;
586
757
  if (!isAt("(")) return false;
587
- const offset = { value: 1 };
588
- while (peek(offset.value).kind === "Identifier" && isAt(",", offset.value + 1))
589
- offset.value += 2;
590
- return (
591
- peek(offset.value).kind === "Identifier" &&
592
- isAt(")", offset.value + 1) &&
593
- isAt("::", offset.value + 2)
594
- );
758
+ let offset = 1;
759
+ while (peek(offset).kind === "Identifier" && isAt(",", offset + 1)) offset += 2;
760
+ return peek(offset).kind === "Identifier" && isAt(")", offset + 1) && isAt("::", offset + 2);
595
761
  };
596
762
 
597
763
  const parsePattern = (): BoundPattern => {
@@ -599,34 +765,80 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
599
765
  const named = expectName();
600
766
  return { kind: "Name", name: named.name, ranges: [named.range] };
601
767
  }
602
- const names = getNames(parseSeparated(expectName));
768
+ const names = getNames(isAt(">>") ? [] : parseSeparated(expectName));
603
769
  expect(">>");
604
770
  return { kind: "Tuple", names: names.names, ranges: names.ranges };
605
771
  };
606
772
 
607
- const parseBounds = () =>
608
- parseSeparated(() => {
773
+ // tlapm's func_boundeds reads another bound only where names and \in follow the comma.
774
+ const parseBounds = () => {
775
+ const parseBound = () => {
609
776
  const patterns = parseSeparated(parsePattern);
610
777
  expect("\\in");
611
- const set = parseExpression();
778
+ const set = commit(parseExpression);
612
779
  return patterns.map((pattern) => ({ pattern, set }));
613
- }).flat();
780
+ };
781
+ const bounds = [parseBound()];
782
+ while (isAt(",") && (!isTlapm || isBoundAhead(1))) {
783
+ advance();
784
+ bounds.push(parseBound());
785
+ }
786
+ return bounds.flat();
787
+ };
614
788
 
615
- const parseSubscript = (): Expression => {
789
+ const parseSubscript = (): { subscript: Expression; lastArgs: Expression[] } => {
616
790
  const token = peek();
791
+ if (isTlapm && BUILTIN_CONSTANTS.has(token.text)) {
792
+ const start = cursor;
793
+ advance();
794
+ const constant: Expression = {
795
+ kind: "Identifier",
796
+ name: token.text,
797
+ location: getLocation(token),
798
+ };
799
+ return { subscript: withRange(constant, start), lastArgs: [] };
800
+ }
801
+ const isTlapmAtom =
802
+ isTlapm && (token.kind === "Number" || token.kind === "String" || isAt("@"));
617
803
  if (token.kind === "Identifier") {
618
- const start = cursor.index;
619
- const names = [advance().text];
804
+ const start = cursor;
805
+ const names: string[] = [];
806
+ const selectorRanges: SourceRange[] = [];
807
+ const parseElement = (isApplicable: boolean) => {
808
+ selectorRanges.push(getRange(cursor, cursor));
809
+ names.push(expectIdentifier());
810
+ return isApplicable && isAt("(") ? parseArguments() : [];
811
+ };
812
+ const elementArgs = [parseElement(!isTlapm)];
620
813
  while (isAt("!") && peek(1).kind === "Identifier") {
621
814
  advance();
622
- names.push(expectIdentifier());
815
+ elementArgs.push(parseElement(true));
623
816
  }
624
- return withRange(
625
- { kind: "Identifier", name: names.join("!"), location: getLocation(token) },
626
- start,
627
- );
817
+ const prefixArgs = elementArgs.slice(0, -1);
818
+ const location = getLocation(token);
819
+ location.range = { ...getRange(start), end: selectorRanges[selectorRanges.length - 1].end };
820
+ const name = names.join("!");
821
+ const subscript: Expression = prefixArgs.every((args) => args.length === 0)
822
+ ? { kind: "Identifier", name, location }
823
+ : {
824
+ kind: "Apply",
825
+ operator: name,
826
+ args: prefixArgs.flat(),
827
+ argumentCounts: [...prefixArgs.map((args) => args.length), 0],
828
+ argumentsRange: location.range,
829
+ selectorRanges,
830
+ location,
831
+ };
832
+ return { subscript, lastArgs: elementArgs[elementArgs.length - 1] };
833
+ }
834
+ if (isAt("<<") || isAt("(") || isAt("{") || isAt("[") || isTlapmAtom) {
835
+ const start = cursor;
836
+ const subscript = parsePrimary();
837
+ return {
838
+ subscript: subscript.location.range ? subscript : withRange(subscript, start),
839
+ lastArgs: [],
840
+ };
628
841
  }
629
- if (isAt("<<") || isAt("(")) return parsePrimary();
630
842
  throw createUnexpected(`Expected a subscript but found ${describeToken(token)}`);
631
843
  };
632
844
 
@@ -640,12 +852,11 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
640
852
  token.kind === "Identifier" &&
641
853
  (next.text === "(" ||
642
854
  next.text === "[" ||
643
- (next.kind === "Operator" && isAt("==", POSTFIX_OPERATORS.has(next.text) ? 3 : 4)))
855
+ (next.kind === "Operator" &&
856
+ isAt("==", DEFINABLE_POSTFIX_OPERATORS.has(next.text) ? 3 : 4)))
644
857
  );
645
858
  };
646
859
 
647
- // SANY's OpOrExpr reads an operator as an argument only before what ends the argument, and as
648
- // the start of an expression, such as -.(1), otherwise.
649
860
  const isStandalone = (operator: Token) => {
650
861
  const token = peek(1);
651
862
  return (
@@ -665,8 +876,6 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
665
876
  token.kind === "Operator" &&
666
877
  (INFIX_OPERATORS.has(token.text) || PREFIX_OPERATOR_REFERENCES.has(token.text)) &&
667
878
  isStandalone(token);
668
- // tlapm reads a prefix operator as the start of an expression, and (+), (-), (/) and (\X) as
669
- // the operators \oplus, \ominus, \oslash and \otimes.
670
879
  const isTlapmIncomplete =
671
880
  isTlapm &&
672
881
  token.kind !== "String" &&
@@ -682,8 +891,11 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
682
891
  peek(1).line === token.line &&
683
892
  peek(1).column === token.endColumn + 1));
684
893
  if (isTlapmIncomplete && isStandalone(token)) throw createError("incomplete expression", token);
894
+ if (isOperatorAlone && !isTlapm && token.text === "\\X" && token.spelling !== "×") {
895
+ throw createError("\\X may not be used as an infix operator", token);
896
+ }
685
897
  if (isOperatorAlone) {
686
- cursor.index++;
898
+ cursor++;
687
899
  return withRange(
688
900
  {
689
901
  kind: "OperatorReference",
@@ -691,18 +903,18 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
691
903
  spelling: token.spelling,
692
904
  location: getLocation(token),
693
905
  },
694
- cursor.index - 1,
906
+ cursor - 1,
695
907
  );
696
908
  }
697
909
  const isPrefixAlone =
698
910
  token.kind !== "String" && OPERATOR_ARGUMENTS.has(token.text) && isStandalone(token);
699
911
  if (isPrefixAlone) {
700
- cursor.index++;
912
+ cursor++;
701
913
  const location = getLocation(token);
702
914
  if (POSTFIX_OPERATORS.has(token.text)) {
703
915
  return withRange(
704
916
  { kind: "OperatorReference", operator: token.text, spelling: token.spelling, location },
705
- cursor.index - 1,
917
+ cursor - 1,
706
918
  );
707
919
  }
708
920
  const argument: Expression = { kind: "Identifier", name: "argument", location };
@@ -714,11 +926,11 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
714
926
  operator: token.text,
715
927
  location: getLocation(token),
716
928
  },
717
- cursor.index - 1,
929
+ cursor - 1,
718
930
  );
719
931
  }
720
932
  if (isAt("LAMBDA")) {
721
- const start = cursor.index;
933
+ const start = cursor;
722
934
  advance();
723
935
  const params = getNames(parseSeparated(expectName));
724
936
  expect(":");
@@ -734,30 +946,43 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
734
946
  start,
735
947
  );
736
948
  }
737
- return parseExpression();
949
+ if (!isTlapm) return parseExpression();
950
+ // tlapm's oparg reads an operator where an expression doesn't start, and fails otherwise.
951
+ const argument = attempt(parseExpression);
952
+ if (argument) return argument;
953
+ throw createUnexpected(`Unexpected ${describeToken(peek())}`);
738
954
  };
739
955
 
740
- const lastArguments: { range: SourceRange | undefined } = { range: undefined };
956
+ let lastArgumentsRange: SourceRange | undefined;
741
957
 
742
958
  const parseArguments = () =>
743
959
  withBracketLayout(() => {
744
- const start = cursor.index;
960
+ const start = cursor;
745
961
  expect("(");
746
962
  const args = parseSeparated(parseArgument);
747
963
  expect(")");
748
- lastArguments.range = getRange(start);
964
+ lastArgumentsRange = getRange(start);
749
965
  return args;
750
966
  });
751
967
 
752
968
  const parseJunctionList = (): Expression => {
753
969
  const bullet = advance();
754
- cursor.offsideColumns.push(bullet.column);
970
+ offsideColumns.push(bullet.column);
755
971
  const operands = [parseExpression()];
756
- while (peekRaw().text === bullet.text && peekRaw().column === bullet.column) {
757
- cursor.index++;
758
- operands.push(parseExpression());
759
- }
760
- cursor.offsideColumns.pop();
972
+ const parseItems = () => {
973
+ while (
974
+ isBullet(peekRaw()) &&
975
+ peekRaw().text === bullet.text &&
976
+ peekRaw().column === bullet.column
977
+ ) {
978
+ cursor++;
979
+ operands.push(parseExpression());
980
+ }
981
+ };
982
+ // bulleted_list: a first item that fails leaves expr_or_op's other readings to fail after it.
983
+ if (isTlapm) commit(parseItems);
984
+ else parseItems();
985
+ offsideColumns.pop();
761
986
  return {
762
987
  kind: bullet.text === "/\\" ? "And" : "Or",
763
988
  operands,
@@ -769,13 +994,12 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
769
994
  const parseBracket = (start: Token): Expression =>
770
995
  withBracketLayout(() => {
771
996
  const location = getLocation(start);
772
- // SANY takes a keyword for the first field of a record, as it does for a selected field.
773
997
  const isIdentifier = peek().kind === "Identifier";
774
998
  const isRecord = isAt("|->", 1) && (isIdentifier || isFieldName(peek()));
775
999
  if (isRecord || (isIdentifier && isAt(":", 1))) {
776
1000
  const kind = isRecord ? "Record" : "RecordSet";
777
1001
  const parseField = (readName: () => string) => {
778
- const fieldStart = cursor.index;
1002
+ const fieldStart = cursor;
779
1003
  const fieldLocation = getLocation(peek());
780
1004
  const name = readName();
781
1005
  fieldLocation.range = getRange(fieldStart);
@@ -790,11 +1014,15 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
790
1014
  return { kind, fields, location };
791
1015
  }
792
1016
  if (isBoundAhead()) {
1017
+ const checkpoint = cursor;
793
1018
  const bounds = parseBounds();
794
- expect("|->");
795
- const body = parseExpression();
796
- expect("]");
797
- return { kind: "FunctionConstruction", bounds, body, location };
1019
+ if (!isTlapm || isAt("|->")) {
1020
+ expect("|->");
1021
+ const body = parseExpression();
1022
+ expect("]");
1023
+ return { kind: "FunctionConstruction", bounds, body, location };
1024
+ }
1025
+ cursor = checkpoint;
798
1026
  }
799
1027
  const expression = parseExpression();
800
1028
  if (accept("->")) {
@@ -810,6 +1038,7 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
810
1038
  if (accept(".")) {
811
1039
  if (!isFieldName(peek()))
812
1040
  throw createUnexpected(`Expected a field name but found ${describeToken(peek())}`);
1041
+ if (!isTlapm && isAt("@")) throw createUnexpected("@ used in !.@");
813
1042
  path.push({ kind: "Field", name: advance().text });
814
1043
  continue;
815
1044
  }
@@ -830,16 +1059,20 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
830
1059
  return { kind: "Bracket", args, location };
831
1060
  }
832
1061
  expect("]_");
833
- return { kind: "BoxAction", action: expression, subscript: parseSubscript(), location };
1062
+ return {
1063
+ kind: "BoxAction",
1064
+ action: expression,
1065
+ subscript: parseSubscript().subscript,
1066
+ location,
1067
+ };
834
1068
  });
835
1069
 
836
- // SANY reads the set after the \in of {x \in S} as a whole expression, whatever operators it has.
837
1070
  const parseSetMembership = (): Expression => {
838
- const start = cursor.index;
1071
+ const start = cursor;
839
1072
  const element = withRange(parsePrefix(), start);
840
1073
  const location = getLocation(advance());
841
1074
  return withRange(
842
- { kind: "Apply", operator: "\\in", args: [element, parseExpression()], location },
1075
+ { kind: "Apply", operator: "\\in", args: [element, commit(parseExpression)], location },
843
1076
  start,
844
1077
  );
845
1078
  };
@@ -849,16 +1082,16 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
849
1082
  const location = getLocation(start);
850
1083
  if (accept("}")) return { kind: "SetEnumeration", elements: [], location };
851
1084
  if (isBoundAhead() && (peek(1).text === "\\in" || isAt("<<"))) {
852
- const checkpoint = cursor.index;
1085
+ const checkpoint = cursor;
853
1086
  const pattern = parsePattern();
854
1087
  expect("\\in");
855
- const set = parseExpression();
1088
+ const set = commit(parseExpression);
856
1089
  if (accept(":")) {
857
- const predicate = parseExpression();
1090
+ const predicate = commit(parseExpression);
858
1091
  expect("}");
859
1092
  return { kind: "SetFilter", bound: { pattern, set }, predicate, location };
860
1093
  }
861
- cursor.index = checkpoint;
1094
+ cursor = checkpoint;
862
1095
  }
863
1096
  const first =
864
1097
  peek().kind === "Identifier" && isAt("\\in", 1) ? parseSetMembership() : parseExpression();
@@ -881,7 +1114,12 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
881
1114
  if (accept(">>_")) {
882
1115
  if (elements.length !== 1)
883
1116
  throw createError("Expected a single action inside <<A>>_v", start);
884
- return { kind: "AngleAction", action: elements[0], subscript: parseSubscript(), location };
1117
+ return {
1118
+ kind: "AngleAction",
1119
+ action: elements[0],
1120
+ subscript: commit(parseSubscript).subscript,
1121
+ location,
1122
+ };
885
1123
  }
886
1124
  expect(">>");
887
1125
  return { kind: "Tuple", elements, location };
@@ -912,9 +1150,17 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
912
1150
  const parseCase = (start: Token): Expression => {
913
1151
  const arms: CaseArm[] = [];
914
1152
  do {
1153
+ const otherStart = cursor;
915
1154
  if (accept("OTHER")) {
916
1155
  expect("->");
917
- return { kind: "Case", arms, other: parseExpression(), location: getLocation(start) };
1156
+ const other = parseExpression();
1157
+ return {
1158
+ kind: "Case",
1159
+ arms,
1160
+ other,
1161
+ otherRange: getRange(otherStart),
1162
+ location: getLocation(start),
1163
+ };
918
1164
  }
919
1165
  const condition = parseExpression();
920
1166
  expect("->");
@@ -931,9 +1177,9 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
931
1177
  next.kind === "Number" ||
932
1178
  SELECTOR_SYMBOLS.has(next.text) ||
933
1179
  (next.kind === "Operator" &&
934
- (INFIX_OPERATORS.has(next.text) ||
935
- POSTFIX_OPERATORS.has(next.text) ||
936
- PREFIX_OPERATOR_REFERENCES.has(next.text))))
1180
+ (INFIX_OPERATORS.has(next.text) || DEFINABLE_POSTFIX_OPERATORS.has(next.text))) ||
1181
+ ((next.kind === "Operator" || next.kind === "Keyword") &&
1182
+ DEFINABLE_PREFIX_OPERATORS.has(next.text)))
937
1183
  );
938
1184
  };
939
1185
 
@@ -949,15 +1195,64 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
949
1195
  case "@":
950
1196
  return { kind: "At" };
951
1197
  case "(": {
952
- cursor.index--;
1198
+ cursor--;
953
1199
  return { kind: "Bound", args: parseArguments() };
954
1200
  }
955
1201
  }
956
1202
  return { kind: "Name", name: token.text, args: isAt("(") ? parseArguments() : [] };
957
1203
  };
958
1204
 
1205
+ const parseSelections = (
1206
+ name: string,
1207
+ args: Expression[],
1208
+ argumentsRange: SourceRange | undefined,
1209
+ start: number,
1210
+ location: SourceLocation,
1211
+ ): Expression => {
1212
+ const selectors: Selector[] = [{ kind: "Name", name, args }];
1213
+ const selectorRanges = [getRange(start, start)];
1214
+ const argumentRanges = [argumentsRange];
1215
+ while (isSelectorAhead()) {
1216
+ advance();
1217
+ const selectorStart = cursor;
1218
+ lastArgumentsRange = undefined;
1219
+ const selector = parseSelector();
1220
+ selectorRanges.push(
1221
+ selector.kind === "Bound"
1222
+ ? getRange(selectorStart)
1223
+ : getRange(selectorStart, selectorStart),
1224
+ );
1225
+ selectors.push(selector);
1226
+ argumentRanges.push(lastArgumentsRange);
1227
+ }
1228
+ const names = selectors.flatMap((selector) =>
1229
+ selector.kind === "Name" ? [selector.name] : [],
1230
+ );
1231
+ const operator = names.join("!");
1232
+ const qualifiedArgs = selectors.flatMap((selector) =>
1233
+ "args" in selector ? selector.args : [],
1234
+ );
1235
+ const argumentCounts = selectors.map((selector) =>
1236
+ "args" in selector ? selector.args.length : 0,
1237
+ );
1238
+ return names.length < selectors.length
1239
+ ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
1240
+ : qualifiedArgs.length === 0
1241
+ ? { kind: "Identifier", name: operator, selectorRanges, location }
1242
+ : {
1243
+ kind: "Apply",
1244
+ operator,
1245
+ args: qualifiedArgs,
1246
+ ...(argumentCounts.slice(0, -1).some((count) => count > 0) && { argumentCounts }),
1247
+ argumentsRange: argumentRanges.at(-1) ?? getRange(start),
1248
+ selectorRanges,
1249
+ argumentRanges,
1250
+ location,
1251
+ };
1252
+ };
1253
+
959
1254
  const parsePrimary = (preceding?: Precedence): Expression => {
960
- const start = cursor.index;
1255
+ const start = cursor;
961
1256
  const token = advance();
962
1257
  const location = getLocation(token);
963
1258
  if (isTlapm && token.kind === "Keyword" && token.text === "LAMBDA") {
@@ -965,14 +1260,12 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
965
1260
  expect(":");
966
1261
  return { kind: "Lambda", params, body: parseExpression(), location };
967
1262
  }
968
- if (token.kind === "Number") {
969
- if (isTlapm && /^\\[BOH]/.test(token.text)) throw createError("incomplete expression", token);
1263
+ if (token.kind === "Number")
970
1264
  return { kind: "Number", value: getNumeralValue(token.text), text: token.text, location };
971
- }
972
1265
  if (token.kind === "Decimal") return { kind: "Decimal", text: token.text, location };
973
1266
  if (token.kind === "String") return { kind: "String", value: token.text, location };
974
1267
  const isStep = token.kind === "Step" && isStepReference(token);
975
- if (isStep && getProofLevel() < 0 && proofLevels.length <= 1) {
1268
+ if (!isTlapm && isStep && getProofLevel() < 0 && proofLevels.length <= 1) {
976
1269
  throw createError("Step number used outside proof", token);
977
1270
  }
978
1271
  if (isStep && token.text[1] === "+") {
@@ -984,14 +1277,14 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
984
1277
  const params = getNames(accept("(") ? parseSeparated(expectName) : []);
985
1278
  if (params.names.length > 0) expect(")");
986
1279
  expect("::");
1280
+ const head = getRange(start);
987
1281
  const body = parseExpression();
988
- // TLAplusParser.labelDoesNotChangeParse.
989
1282
  if (
990
1283
  !isTlapm &&
991
1284
  preceding !== undefined &&
992
- operators.last !== undefined &&
1285
+ lastOperator !== undefined &&
993
1286
  isOperatorExpression(body) &&
994
- preceding.high >= operators.last.low
1287
+ preceding.high >= lastOperator.low
995
1288
  ) {
996
1289
  throw createError("Removing the label would change expression parsing", token);
997
1290
  }
@@ -1000,6 +1293,7 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1000
1293
  name,
1001
1294
  params: params.names,
1002
1295
  paramRanges: params.ranges,
1296
+ head,
1003
1297
  body,
1004
1298
  location,
1005
1299
  };
@@ -1008,64 +1302,51 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1008
1302
  if (name === "@" && !hasArguments && !isSelectorAhead())
1009
1303
  return { kind: "ExceptAt", location };
1010
1304
  const args = hasArguments ? parseArguments() : [];
1011
- const argumentRanges = [hasArguments ? lastArguments.range : undefined];
1012
1305
  if (!isSelectorAhead()) {
1013
1306
  return hasArguments
1014
- ? { kind: "Apply", operator: name, args, argumentsRange: lastArguments.range, location }
1307
+ ? { kind: "Apply", operator: name, args, argumentsRange: lastArgumentsRange, location }
1015
1308
  : { kind: "Identifier", name, location };
1016
1309
  }
1017
- const selectors: Selector[] = [{ kind: "Name", name, args }];
1018
- const selectorRanges = [getRange(start, start)];
1019
- while (isSelectorAhead()) {
1020
- advance();
1021
- selectorRanges.push(getRange(cursor.index, cursor.index));
1022
- lastArguments.range = undefined;
1023
- selectors.push(parseSelector());
1024
- argumentRanges.push(lastArguments.range);
1025
- }
1026
- const names = selectors.flatMap((selector) =>
1027
- selector.kind === "Name" ? [selector.name] : [],
1028
- );
1029
- const operator = names.join("!");
1030
- const qualifiedArgs = selectors.flatMap((selector) =>
1031
- "args" in selector ? selector.args : [],
1032
- );
1033
- const argumentCounts = selectors.map((selector) =>
1034
- "args" in selector ? selector.args.length : 0,
1310
+ return parseSelections(
1311
+ name,
1312
+ args,
1313
+ hasArguments ? lastArgumentsRange : undefined,
1314
+ start,
1315
+ location,
1035
1316
  );
1036
- const argumentsRange = argumentRanges.at(-1) ?? getRange(start);
1037
- return names.length < selectors.length
1038
- ? { kind: "Subexpression", selectors, selectorRanges, argumentRanges, location }
1039
- : qualifiedArgs.length === 0
1040
- ? { kind: "Identifier", name: operator, selectorRanges, location }
1041
- : {
1042
- kind: "Apply",
1043
- operator,
1044
- args: qualifiedArgs,
1045
- ...(argumentCounts.slice(0, -1).some((count) => count > 0) && { argumentCounts }),
1046
- argumentsRange,
1047
- selectorRanges,
1048
- argumentRanges,
1049
- location,
1050
- };
1051
1317
  }
1052
- // SANY reads an infix or postfix operator as a primary expression, with or without arguments.
1053
- if (token.kind === "Operator" && isOperatorName(token.text) && isAt("(")) {
1054
- return {
1055
- kind: "Apply",
1056
- operator: token.text,
1057
- spelling: token.spelling,
1058
- args: parseArguments(),
1059
- location,
1060
- };
1318
+ const isOperatorNamed =
1319
+ token.kind === "Operator" && (isOperatorName(token.text) || (token.text === "'" && !isTlapm));
1320
+ if (isOperatorNamed && isAt("(")) {
1321
+ const args = parseArguments();
1322
+ if (!isTlapm && isSelectorAhead()) {
1323
+ return parseSelections(token.text, args, lastArgumentsRange, start, location);
1324
+ }
1325
+ // Deviation from TLC: SANY's built-in \X has no definition for TLC to evaluate, a TLC bug.
1326
+ if (mode === "tlc" && token.text === "\\X" && args.length === 2) {
1327
+ return { kind: "Product", factors: args, location };
1328
+ }
1329
+ return token.text === "'" && args.length === 1
1330
+ ? { kind: "Prime", expression: args[0], location }
1331
+ : {
1332
+ kind: "Apply",
1333
+ operator: token.text,
1334
+ spelling: token.spelling,
1335
+ args,
1336
+ argumentsRange: lastArgumentsRange,
1337
+ location,
1338
+ };
1061
1339
  }
1062
- if (token.kind === "Operator" && isOperatorName(token.text) && !isTlapm) {
1063
- return { kind: "Identifier", name: token.text, spelling: token.spelling, location };
1340
+ if (isOperatorNamed && !isTlapm) {
1341
+ return isSelectorAhead()
1342
+ ? parseSelections(token.text, [], undefined, start, location)
1343
+ : { kind: "Identifier", name: token.text, spelling: token.spelling, location };
1064
1344
  }
1065
1345
  switch (token.text) {
1066
1346
  case "(": {
1067
1347
  const expression = withBracketLayout(() => parseExpression());
1068
1348
  expect(")");
1349
+ parenthesizedRanges.set(expression, getRange(start));
1069
1350
  return expression;
1070
1351
  }
1071
1352
  case "[":
@@ -1093,23 +1374,27 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1093
1374
  case "LET": {
1094
1375
  const definitions: Definition[] = [];
1095
1376
  const instances: Instance[] = [];
1377
+ const recursive: Declaration[] = [];
1096
1378
  recursiveScopes.push(new Set());
1097
1379
  while (!accept("IN")) {
1098
- if (isTlapm && isAt("RECURSIVE"))
1099
- throw createError("Unexpected keyword RECURSIVE", getDefinitionEnd(peekRaw()));
1380
+ if (isTlapm && isAt("RECURSIVE")) throw createUnexpected("Unexpected keyword RECURSIVE");
1100
1381
  if (accept("RECURSIVE")) {
1101
- declareRecursive();
1382
+ recursive.push(...declareRecursive());
1102
1383
  continue;
1103
1384
  }
1104
- const unit = parseDefinition(false);
1385
+ const unit = parseDefinition(!isTlapm && accept("LOCAL"));
1105
1386
  if (unit.kind === "Instance") instances.push(unit);
1106
1387
  else definitions.push(unit);
1107
1388
  }
1108
1389
  recursiveScopes.pop();
1109
- const body = parseExpression();
1110
- return instances.length === 0
1111
- ? { kind: "Let", definitions, body, location }
1112
- : { kind: "Let", definitions, instances, body, location };
1390
+ return {
1391
+ kind: "Let",
1392
+ definitions,
1393
+ ...(instances.length > 0 && { instances }),
1394
+ ...(recursive.length > 0 && { recursive }),
1395
+ body: parseExpression(),
1396
+ location,
1397
+ };
1113
1398
  }
1114
1399
  case "CHOOSE": {
1115
1400
  const pattern = parsePattern();
@@ -1120,15 +1405,18 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1120
1405
  case "WF_":
1121
1406
  case "SF_": {
1122
1407
  const subscript = parseSubscript();
1123
- const action = withBracketLayout(() => {
1124
- expect("(");
1125
- const expression = parseExpression();
1126
- expect(")");
1127
- return expression;
1128
- });
1408
+ const action =
1409
+ !isAt("(") && subscript.lastArgs.length === 1
1410
+ ? subscript.lastArgs[0]
1411
+ : withBracketLayout(() => {
1412
+ expect("(");
1413
+ const expression = parseExpression();
1414
+ expect(")");
1415
+ return expression;
1416
+ });
1129
1417
  return {
1130
1418
  kind: token.text === "WF_" ? "WeakFairness" : "StrongFairness",
1131
- subscript,
1419
+ subscript: subscript.subscript,
1132
1420
  action,
1133
1421
  location,
1134
1422
  };
@@ -1137,28 +1425,25 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1137
1425
  throw createUnexpected(`Unexpected ${describeToken(token)}`, token);
1138
1426
  };
1139
1427
 
1140
- // SANY rejects the operator after a prefix operator's operand unless it binds looser than the
1141
- // prefix operator.
1142
1428
  const parsePrefixOperand = (operator: Token, low: number, high: number) => {
1143
- operators.last = operators.preceding = { low, high };
1429
+ lastOperator = precedingOperator = { low, high };
1144
1430
  const operand = parseExpression(high + 1);
1145
1431
  const next = peek();
1146
- const nextHigh =
1147
- next.text === "'" || POSTFIX_OPERATORS.has(next.text)
1148
- ? POSTFIX_PRECEDENCE.high
1149
- : INFIX_OPERATORS.get(next.text)?.high;
1150
- if (!isTlapm && next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
1432
+ const nextHigh = DEFINABLE_POSTFIX_OPERATORS.has(next.text)
1433
+ ? POSTFIX_PRECEDENCE.high
1434
+ : INFIX_OPERATORS.get(next.text)?.high;
1435
+ if (next.kind === "Operator" && nextHigh !== undefined && nextHigh >= low) {
1151
1436
  throw createError(`Precedence conflict between ${operator.text} and ${next.text}`, next);
1152
1437
  }
1153
1438
  return operand;
1154
1439
  };
1155
1440
 
1156
1441
  const parsePrefix = (): Expression => {
1157
- const preceding = operators.preceding;
1158
- operators.preceding = undefined;
1442
+ const preceding = precedingOperator;
1443
+ precedingOperator = undefined;
1159
1444
  const token = peek();
1160
1445
  const location = getLocation(token);
1161
- if (token.kind === "Operator" && (token.text === "/\\" || token.text === "\\/")) {
1446
+ if (token.kind === "Operator" && isBullet(token)) {
1162
1447
  return parseJunctionList();
1163
1448
  }
1164
1449
  if (token.kind === "Operator" || token.kind === "Keyword") {
@@ -1180,17 +1465,11 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1180
1465
  location,
1181
1466
  };
1182
1467
  case "[]":
1183
- advance();
1184
- return { kind: "Always", expression: parsePrefixOperand(token, 4, 15), location };
1185
1468
  case "<>":
1186
- advance();
1187
- return { kind: "Eventually", expression: parsePrefixOperand(token, 4, 15), location };
1188
1469
  case "ENABLED":
1189
- advance();
1190
- return { kind: "Enabled", expression: parsePrefixOperand(token, 4, 15), location };
1191
1470
  case "UNCHANGED":
1192
1471
  advance();
1193
- return { kind: "Unchanged", expression: parsePrefixOperand(token, 4, 15), location };
1472
+ return getOperatorApplication(token.text, parsePrefixOperand(token, 4, 15), location);
1194
1473
  case "SUBSET":
1195
1474
  case "UNION":
1196
1475
  case "DOMAIN":
@@ -1198,8 +1477,7 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1198
1477
  return {
1199
1478
  kind: "Apply",
1200
1479
  operator: token.text,
1201
- // tlapm gives these operators the precedence 8 of the TLA+ book, and SANY 10-13.
1202
- args: [isTlapm ? parseExpression(9) : parsePrefixOperand(token, 10, 13)],
1480
+ args: [parsePrefixOperand(token, 10, 13)],
1203
1481
  location,
1204
1482
  };
1205
1483
  }
@@ -1207,58 +1485,85 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1207
1485
  return parsePrimary(preceding);
1208
1486
  };
1209
1487
 
1210
- const parseExpression = (minimumPrecedence = 0): Expression => {
1211
- const start = cursor.index;
1212
- const prefix = parsePrefix();
1213
- const state = {
1214
- left: prefix.location.range ? prefix : withRange(prefix, start),
1215
- previous: undefined as (InfixOperator & { text: string }) | undefined,
1488
+ const getInfixApplication = (
1489
+ token: Token,
1490
+ left: Expression,
1491
+ right: Expression,
1492
+ isChained: boolean,
1493
+ prefixEnd: Point,
1494
+ ): Expression => {
1495
+ const location = getLocation(token);
1496
+ if (token.text === "/\\" || token.text === "\\/") {
1497
+ const kind = token.text === "/\\" ? "And" : "Or";
1498
+ return left.kind === kind && isChained
1499
+ ? {
1500
+ ...left,
1501
+ operands: [...left.operands, right],
1502
+ prefixEnds: [...(left.prefixEnds ?? []), prefixEnd],
1503
+ }
1504
+ : { kind, operands: [left, right], prefixEnds: [prefixEnd], location };
1505
+ }
1506
+ if (token.text === "\\X") {
1507
+ return left.kind === "Product" && isChained
1508
+ ? { ...left, factors: [...left.factors, right] }
1509
+ : { kind: "Product", factors: [left, right], location };
1510
+ }
1511
+ return {
1512
+ kind: "Apply",
1513
+ operator: token.text,
1514
+ spelling: token.spelling,
1515
+ args: [left, right],
1516
+ location,
1216
1517
  };
1518
+ };
1519
+
1520
+ const getPostfixApplication = (token: Token, operand: Expression): Expression =>
1521
+ token.text === "'"
1522
+ ? { kind: "Prime", expression: operand, location: getLocation(token) }
1523
+ : {
1524
+ kind: "Apply",
1525
+ operator: token.text,
1526
+ spelling: token.spelling,
1527
+ args: [operand],
1528
+ location: getLocation(token),
1529
+ };
1530
+
1531
+ const parseFunctionArguments = () => {
1532
+ advance();
1533
+ const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
1534
+ withBracketLayout(() => expect("]"));
1535
+ return args;
1536
+ };
1537
+
1538
+ const parseSanyExpression = (minimumPrecedence: number): Expression => {
1539
+ const start = cursor;
1540
+ const prefix = parsePrefix();
1541
+ let left = prefix.location.range ? prefix : withRange(prefix, start);
1542
+ let previous: (InfixOperator & { text: string }) | undefined;
1217
1543
  while (true) {
1218
1544
  const token = peek();
1219
1545
  if (token.kind !== "Operator") break;
1220
1546
  const location = getLocation(token);
1221
- if (token.text === "'" && minimumPrecedence <= 15) {
1222
- advance();
1223
- operators.last = POSTFIX_PRECEDENCE;
1224
- state.left = withRange({ kind: "Prime", expression: state.left, location }, start);
1225
- continue;
1226
- }
1227
- if (POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
1547
+ if (DEFINABLE_POSTFIX_OPERATORS.has(token.text) && minimumPrecedence <= 15) {
1228
1548
  advance();
1229
- operators.last = POSTFIX_PRECEDENCE;
1230
- state.left = withRange(
1231
- {
1232
- kind: "Apply",
1233
- operator: token.text,
1234
- spelling: token.spelling,
1235
- args: [state.left],
1236
- location,
1237
- },
1238
- start,
1239
- );
1549
+ lastOperator = POSTFIX_PRECEDENCE;
1550
+ left = withRange(getPostfixApplication(token, left), start);
1240
1551
  continue;
1241
1552
  }
1242
1553
  if (token.text === "[") {
1243
- advance();
1244
- const args = withBracketLayout(() => parseSeparated(() => parseExpression()));
1245
- withBracketLayout(() => expect("]"));
1246
- operators.last = FUNCTION_PRECEDENCE;
1247
- state.left = withRange(
1248
- { kind: "FunctionApplication", function: state.left, args, location },
1249
- start,
1250
- );
1554
+ const args = parseFunctionArguments();
1555
+ lastOperator = FUNCTION_PRECEDENCE;
1556
+ left = withRange({ kind: "FunctionApplication", function: left, args, location }, start);
1251
1557
  continue;
1252
1558
  }
1253
1559
  if (token.text === "." && isFieldName(peek(1))) {
1254
1560
  advance();
1255
1561
  const field = advance().text;
1256
- state.left = withRange({ kind: "FieldAccess", record: state.left, field, location }, start);
1562
+ left = withRange({ kind: "FieldAccess", record: left, field, location }, start);
1257
1563
  continue;
1258
1564
  }
1259
1565
  const infix = INFIX_OPERATORS.get(token.text);
1260
1566
  if (!infix || infix.low < minimumPrecedence) break;
1261
- const previous = state.previous;
1262
1567
  if (previous) {
1263
1568
  const isOverlapping = !(previous.low > infix.high || previous.high < infix.low);
1264
1569
  const isChain = previous.text === token.text && infix.isAssociative;
@@ -1270,44 +1575,260 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1270
1575
  }
1271
1576
  }
1272
1577
  advance();
1273
- // SANY reads - after an infix operator as prefix -, and rejects it after one that binds tighter.
1274
1578
  if (infix.low > NEGATION_PRECEDENCE && isAt("-")) {
1275
1579
  throw createError(`Precedence conflict between ${token.text} and -`, peek());
1276
1580
  }
1277
- operators.last = operators.preceding = infix;
1581
+ lastOperator = precedingOperator = infix;
1278
1582
  const right = parseExpression(infix.high + 1);
1279
- const left = state.left;
1280
- if (token.text === "/\\" || token.text === "\\/") {
1281
- const kind = token.text === "/\\" ? "And" : "Or";
1282
- const prefixEnds = [getRange(start).end];
1283
- state.left =
1284
- left.kind === kind && previous?.text === token.text
1285
- ? {
1286
- ...left,
1287
- operands: [...left.operands, right],
1288
- prefixEnds: [...(left.prefixEnds ?? []), ...prefixEnds],
1289
- }
1290
- : { kind, operands: [left, right], prefixEnds, location };
1291
- } else if (token.text === "\\X") {
1292
- state.left =
1293
- left.kind === "Product" && previous?.text === "\\X"
1294
- ? { ...left, factors: [...left.factors, right] }
1295
- : { kind: "Product", factors: [left, right], location };
1296
- } else {
1297
- state.left = {
1298
- kind: "Apply",
1299
- operator: token.text,
1300
- spelling: token.spelling,
1301
- args: [left, right],
1583
+ const isChained = previous?.text === token.text;
1584
+ left = withRange(
1585
+ getInfixApplication(token, left, right, isChained, getRange(start).end),
1586
+ start,
1587
+ );
1588
+ previous = { ...infix, text: token.text };
1589
+ }
1590
+ return left;
1591
+ };
1592
+
1593
+ const isTlapmAtomStart = (token: Token) =>
1594
+ token.kind === "Number" ||
1595
+ token.kind === "Decimal" ||
1596
+ token.kind === "String" ||
1597
+ token.kind === "Identifier" ||
1598
+ (token.kind === "Step" && isStepReference(token) && /^<\d/.test(token.text)) ||
1599
+ ((token.kind === "Operator" || token.kind === "Keyword") && TLAPM_ATOM_STARTS.has(token.text));
1600
+
1601
+ const getTlapmOperand = (expression: Expression, start: number): TlapmOperand => ({
1602
+ start,
1603
+ end: cursor - 1,
1604
+ expression: expression.location.range ? expression : withRange(expression, start),
1605
+ });
1606
+
1607
+ const getTlapmOperator = (
1608
+ start: number,
1609
+ fixity: TlapmOperator["fixity"],
1610
+ precedence: Precedence,
1611
+ apply: TlapmOperator["apply"],
1612
+ ): TlapmOperator => ({
1613
+ ...precedence,
1614
+ start,
1615
+ end: cursor - 1,
1616
+ fixity,
1617
+ isLeft: false,
1618
+ apply,
1619
+ });
1620
+
1621
+ // expr_or_op's non_test: a postfix operator before an expression in parentheses is an atom.
1622
+ const parseTlapmNonfixPostfix = (token: Token) =>
1623
+ token.kind === "Operator" && DEFINABLE_POSTFIX_OPERATORS.has(token.text) && isAt("(", 1)
1624
+ ? attempt(() =>
1625
+ withBracketLayout(() => {
1626
+ advance();
1627
+ advance();
1628
+ const operand = parseExpression();
1629
+ const end = cursor - 1;
1630
+ expect(")");
1631
+ return { end, expression: getPostfixApplication(token, operand) };
1632
+ }),
1633
+ )
1634
+ : undefined;
1635
+
1636
+ // A comma directly inside the parentheses after the operator, without which non_test's
1637
+ // attempt at `(e, f)` fails. A selector's << or >> isn't a bracket.
1638
+ const hasNonfixComma = () => {
1639
+ let depth = 0;
1640
+ for (let index = cursor + 1; index < tokens.length; index++) {
1641
+ const token = tokens[index];
1642
+ if (token.kind !== "Operator") continue;
1643
+ if (depth === 1 && token.text === ",") return true;
1644
+ if (tokens[index - 1].text === "!") continue;
1645
+ if (OPENING_BRACKETS.has(token.text)) depth++;
1646
+ if (CLOSING_BRACKETS.has(token.text) && --depth === 0) return false;
1647
+ }
1648
+ return false;
1649
+ };
1650
+
1651
+ // expr_or_op's non_test: an infix operator before two expressions in parentheses is an atom.
1652
+ const parseTlapmNonfixInfix = (token: Token) =>
1653
+ isAt("(", 1) && hasNonfixComma()
1654
+ ? attempt(() =>
1655
+ withBracketLayout((): Expression => {
1656
+ advance();
1657
+ advance();
1658
+ const left = parseExpression();
1659
+ expect(",");
1660
+ const right = parseExpression();
1661
+ expect(")");
1662
+ return {
1663
+ kind: "Apply",
1664
+ operator: token.text,
1665
+ args: [left, right],
1666
+ location: getLocation(token),
1667
+ };
1668
+ }),
1669
+ )
1670
+ : undefined;
1671
+
1672
+ const parseTlapmItem = (isStart: boolean): TlapmItem | undefined => {
1673
+ const start = cursor;
1674
+ const token = peek();
1675
+ const location = getLocation(token);
1676
+ const isOperator = token.kind === "Operator" || token.kind === "Keyword";
1677
+ const infix = isOperator ? INFIX_OPERATORS.get(token.text) : undefined;
1678
+ if (!isStart) {
1679
+ if (isAt("[")) {
1680
+ const args = parseFunctionArguments();
1681
+ return getTlapmOperator(start, "Postfix", TLAPM_SELECTION_PRECEDENCE, (operands) => ({
1682
+ kind: "FunctionApplication",
1683
+ function: operands[0].expression,
1684
+ args,
1685
+ location,
1686
+ }));
1687
+ }
1688
+ if (isAt(".") && isFieldName(peek(1))) {
1689
+ advance();
1690
+ const field = advance().text;
1691
+ return getTlapmOperator(start, "Postfix", TLAPM_SELECTION_PRECEDENCE, (operands) => ({
1692
+ kind: "FieldAccess",
1693
+ record: operands[0].expression,
1694
+ field,
1302
1695
  location,
1696
+ }));
1697
+ }
1698
+ if (infix) {
1699
+ // Pco.resolve has no operator to put before that atom, so the expression ends here.
1700
+ if (parseTlapmNonfixInfix(token)) {
1701
+ cursor = start;
1702
+ return undefined;
1703
+ }
1704
+ advance();
1705
+ return {
1706
+ ...getTlapmOperator(start, "Infix", infix, (operands) =>
1707
+ getInfixApplication(
1708
+ token,
1709
+ operands[0].expression,
1710
+ operands[1].expression,
1711
+ operands[0].joinedBy === token.text,
1712
+ getRange(operands[0].start, operands[1].end).end,
1713
+ ),
1714
+ ),
1715
+ isLeft: isTlapmLeft(token.text, infix),
1716
+ joins: token.text,
1303
1717
  };
1304
1718
  }
1305
- state.left = withRange(state.left, start);
1306
- state.previous = { ...infix, text: token.text };
1719
+ if (token.kind !== "Operator" || !DEFINABLE_POSTFIX_OPERATORS.has(token.text))
1720
+ return undefined;
1721
+ // Pco.resolve has no operator to put before that atom, so the expression ends here.
1722
+ if (parseTlapmNonfixPostfix(token)) {
1723
+ cursor = start;
1724
+ return undefined;
1725
+ }
1726
+ advance();
1727
+ return getTlapmOperator(start, "Postfix", POSTFIX_PRECEDENCE, (operands) =>
1728
+ getPostfixApplication(token, operands[0].expression),
1729
+ );
1730
+ }
1731
+ if (token.kind === "Operator" && isBullet(token))
1732
+ return getTlapmOperand(parseJunctionList(), start);
1733
+ const nonfix = infix ? parseTlapmNonfixInfix(token) : undefined;
1734
+ if (nonfix) return getTlapmOperand(nonfix, start);
1735
+ const nonfixPostfix = parseTlapmNonfixPostfix(token);
1736
+ if (nonfixPostfix) {
1737
+ nonfixPostfix.expression.location.range = getRange(start, nonfixPostfix.end);
1738
+ return { start, end: nonfixPostfix.end, expression: nonfixPostfix.expression };
1739
+ }
1740
+ const prefix = isOperator ? TLAPM_PREFIX_OPERATORS.get(token.text) : undefined;
1741
+ if (prefix) {
1742
+ advance();
1743
+ return getTlapmOperator(start, "Prefix", prefix, (operands) =>
1744
+ getOperatorApplication(
1745
+ token.text === "-" ? "-." : token.text,
1746
+ operands[0].expression,
1747
+ location,
1748
+ ),
1749
+ );
1307
1750
  }
1308
- return state.left;
1751
+ if (!isTlapmAtomStart(token)) return undefined;
1752
+ const isTransparent = token.kind === "Operator" && TLAPM_OPEN_BRACKETS.has(token.text);
1753
+ return getTlapmOperand(isTransparent ? parsePrimary() : commit(parsePrimary), start);
1309
1754
  };
1310
1755
 
1756
+ const isTlapmOperator = (item: TlapmItem | undefined): item is TlapmOperator =>
1757
+ item !== undefined && "fixity" in item;
1758
+
1759
+ const isTlapmOperand = (item: TlapmItem | undefined): item is TlapmOperand =>
1760
+ item !== undefined && !("fixity" in item);
1761
+
1762
+ const applyTlapmOperator = (operator: TlapmOperator, operands: TlapmOperand[]) => {
1763
+ const start = Math.min(operator.start, operands[0].start);
1764
+ const end = Math.max(operator.end, operands[operands.length - 1].end);
1765
+ const expression = operator.apply(operands);
1766
+ expression.location.range = getRange(start, end);
1767
+ return { start, end, expression, joinedBy: operator.joins };
1768
+ };
1769
+
1770
+ const reduceTlapmItems = (stack: TlapmItem[]) => {
1771
+ const right = stack.pop();
1772
+ const operator = stack.pop();
1773
+ if (!isTlapmOperand(right) || !isTlapmOperator(operator)) {
1774
+ throw createError("incomplete expression");
1775
+ }
1776
+ const left = operator.fixity === "Infix" ? stack.pop() : undefined;
1777
+ stack.push(applyTlapmOperator(operator, isTlapmOperand(left) ? [left, right] : [right]));
1778
+ };
1779
+
1780
+ const getPreviousOperator = (stack: TlapmItem[]) => {
1781
+ const previous = stack[stack.length - 2];
1782
+ return isTlapmOperator(previous) ? previous : undefined;
1783
+ };
1784
+
1785
+ const isBelow = (left: Precedence, right: Precedence | undefined) =>
1786
+ right !== undefined && left.high < right.low;
1787
+
1788
+ // Port of Pco.resolve. A failure after the first item backtracks no further than its attempt.
1789
+ const parseTlapmExpression = (): Expression =>
1790
+ withRestorePoint(() => {
1791
+ const first = attempt(() => parseTlapmItem(true));
1792
+ if (first === undefined) {
1793
+ const token = peekRaw();
1794
+ throw token.kind === "End"
1795
+ ? createUnexpected("Unexpected end of expression", token)
1796
+ : createError(`required expressions(s) missing before '${getTlapmName(token)}'`, token);
1797
+ }
1798
+ return commit(() => {
1799
+ const stack = [first];
1800
+ while (true) {
1801
+ const item = attempt(() => parseTlapmItem(isTlapmOperator(stack[stack.length - 1])));
1802
+ if (item === undefined) break;
1803
+ if (isTlapmOperand(item) || item.fixity === "Prefix") {
1804
+ stack.push(item);
1805
+ continue;
1806
+ }
1807
+ while (isBelow(item, getPreviousOperator(stack))) reduceTlapmItems(stack);
1808
+ const operand = stack[stack.length - 1];
1809
+ if (item.fixity === "Postfix" && isTlapmOperand(operand)) {
1810
+ stack.splice(-1, 1, applyTlapmOperator(item, [operand]));
1811
+ continue;
1812
+ }
1813
+ const previous = getPreviousOperator(stack);
1814
+ const isLeftChain =
1815
+ previous?.fixity === "Infix" &&
1816
+ previous.isLeft &&
1817
+ item.isLeft &&
1818
+ !isBelow(previous, item);
1819
+ if (isLeftChain) reduceTlapmItems(stack);
1820
+ stack.push(item);
1821
+ }
1822
+ while (stack.length > 1) reduceTlapmItems(stack);
1823
+ const result = stack[0];
1824
+ if (!isTlapmOperand(result)) throw createError("incomplete expression");
1825
+ return result.expression;
1826
+ });
1827
+ });
1828
+
1829
+ const parseExpression = (minimumPrecedence = 0) =>
1830
+ isTlapm ? parseTlapmExpression() : parseSanyExpression(minimumPrecedence);
1831
+
1311
1832
  const parseParams = () => {
1312
1833
  if (!accept("(")) return [];
1313
1834
  const params = parseDeclarations();
@@ -1318,17 +1839,17 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1318
1839
  const parseBody = () => {
1319
1840
  const start = getLocation(peek());
1320
1841
  const body = parseExpression();
1321
- const last = tokens[cursor.index - 1];
1842
+ const last = tokens[cursor - 1];
1322
1843
  const end = { line: last.line, column: last.endColumn };
1323
1844
  return { body, bodyRange: { module: openModules[0].name, start, end } };
1324
1845
  };
1325
1846
 
1326
1847
  const parseDefinition = (isLocal: boolean): Definition | Instance => {
1327
- const start = cursor.index;
1848
+ const start = cursor;
1328
1849
  return withRange(parseDefinitionUnit(isLocal), start);
1329
1850
  };
1330
1851
 
1331
- const toParam = (named: { name: string; range: SourceRange }): OperatorParam => ({
1852
+ const toParam = (named: RangedName): OperatorParam => ({
1332
1853
  name: named.name,
1333
1854
  arity: 0,
1334
1855
  location: {
@@ -1340,11 +1861,13 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1340
1861
 
1341
1862
  const parseDefinitionUnit = (isLocal: boolean): Definition | Instance => {
1342
1863
  const token = peek();
1864
+ const start = cursor;
1343
1865
  const location = getLocation(token);
1344
1866
  const parseOperator = (name: string, params: OperatorParam[]): Definition => ({
1345
1867
  kind: "Operator",
1346
1868
  name,
1347
1869
  params,
1870
+ head: getRange(start, cursor - 2),
1348
1871
  ...parseBody(),
1349
1872
  isLocal,
1350
1873
  isRecursive: isRecursiveName(name),
@@ -1366,7 +1889,7 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1366
1889
  const isPrefixDefinition =
1367
1890
  token.kind !== "String" && DEFINABLE_PREFIX_OPERATORS.has(token.text);
1368
1891
  const isPostfixDefinition =
1369
- token.kind === "Identifier" && POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1892
+ token.kind === "Identifier" && DEFINABLE_POSTFIX_OPERATORS.has(peek(1).text) && isAt("==", 2);
1370
1893
  if (isPrefixDefinition || isPostfixDefinition) {
1371
1894
  const operator = isPrefixDefinition ? token.text : peek(1).text;
1372
1895
  if (isPrefixDefinition) advance();
@@ -1390,7 +1913,7 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1390
1913
  location,
1391
1914
  };
1392
1915
  }
1393
- const definitionStart = cursor.index;
1916
+ const definitionStart = cursor;
1394
1917
  const params = parseParams();
1395
1918
  expect("==");
1396
1919
  if (isAt("INSTANCE")) {
@@ -1406,12 +1929,12 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1406
1929
  isLocal: boolean,
1407
1930
  location: SourceLocation,
1408
1931
  ): Instance => {
1409
- const start = cursor.index;
1932
+ const start = cursor;
1410
1933
  expect("INSTANCE");
1411
1934
  const module = expectModuleName();
1412
1935
  const substitutions = accept("WITH")
1413
1936
  ? parseSeparated(() => {
1414
- const targetStart = cursor.index;
1937
+ const targetStart = cursor;
1415
1938
  const target = expectSubstitutionTarget();
1416
1939
  const nameRange = getRange(targetStart);
1417
1940
  expect("<-");
@@ -1431,18 +1954,25 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1431
1954
  };
1432
1955
 
1433
1956
  const parseDeclaration = (): Declaration => {
1434
- const start = cursor.index;
1957
+ const start = cursor;
1435
1958
  return withRange(parseDeclarationUnit(), start);
1436
1959
  };
1437
1960
 
1438
1961
  const parseDeclarationUnit = (): Declaration => {
1439
1962
  const location = getLocation(peek());
1440
- if (isAt("_") && peek(1).kind === "Operator") {
1963
+ const isOperatorDeclaration =
1964
+ isAt("_") &&
1965
+ peek(1).kind === "Operator" &&
1966
+ (INFIX_OPERATORS.has(peek(1).text) || DEFINABLE_POSTFIX_OPERATORS.has(peek(1).text));
1967
+ if (isOperatorDeclaration) {
1441
1968
  advance();
1442
1969
  const name = advance().text;
1443
1970
  return { name, arity: accept("_") ? 2 : 1, location };
1444
1971
  }
1445
- if (peek().kind === "Operator" && isAt("_", 1)) {
1972
+ const isPrefixOperator =
1973
+ peek().kind !== "String" &&
1974
+ (DEFINABLE_PREFIX_OPERATORS.has(peek().text) || (isTlapm && peek().kind === "Operator"));
1975
+ if (isPrefixOperator && isAt("_", 1)) {
1446
1976
  const operator = advance().text;
1447
1977
  advance();
1448
1978
  return { name: operator === "-" ? "-." : operator, arity: 1, location };
@@ -1464,19 +1994,19 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1464
1994
  return declarations;
1465
1995
  };
1466
1996
 
1467
- const getClosingOffset = (offset: number) => {
1468
- const depth = { value: 0, offset };
1997
+ const getClosingOffset = (start: number) => {
1998
+ let depth = 0;
1999
+ let offset = start;
1469
2000
  do {
1470
- const token = peek(depth.offset);
1471
- if (token.kind === "End") return depth.offset;
1472
- if (token.kind === "Operator" && OPENING_BRACKETS.has(token.text)) depth.value++;
1473
- if (token.kind === "Operator" && CLOSING_BRACKETS.has(token.text)) depth.value--;
1474
- depth.offset++;
1475
- } while (depth.value > 0);
1476
- return depth.offset;
2001
+ const token = peek(offset);
2002
+ if (token.kind === "End") return offset;
2003
+ if (token.kind === "Operator" && OPENING_BRACKETS.has(token.text)) depth++;
2004
+ if (token.kind === "Operator" && CLOSING_BRACKETS.has(token.text)) depth--;
2005
+ offset++;
2006
+ } while (depth > 0);
2007
+ return offset;
1477
2008
  };
1478
2009
 
1479
- // SANY inserts a break before each definition, which ends the fact list or step before it.
1480
2010
  const isDefinitionStart = () => {
1481
2011
  const token = peek();
1482
2012
  const next = peek(1);
@@ -1489,14 +2019,14 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1489
2019
  );
1490
2020
  }
1491
2021
  if (isAt("(", 1) || isAt("[", 1)) return isAt("==", getClosingOffset(1));
1492
- if (next.kind === "Operator" && POSTFIX_OPERATORS.has(next.text)) return isAt("==", 2);
2022
+ if (next.kind === "Operator" && DEFINABLE_POSTFIX_OPERATORS.has(next.text))
2023
+ return isAt("==", 2);
1493
2024
  if (next.kind === "Operator" && INFIX_OPERATORS.has(next.text)) {
1494
2025
  return peek(2).kind === "Identifier" && isAt("==", 3);
1495
2026
  }
1496
2027
  return isAt("==", 1);
1497
2028
  };
1498
2029
 
1499
- // SANY's proof levels, one for each open proof, which stay -1 until a step sets them.
1500
2030
  const proofLevels: number[] = [];
1501
2031
 
1502
2032
  const getProofLevel = () => proofLevels[proofLevels.length - 1] ?? -1;
@@ -1509,9 +2039,9 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1509
2039
 
1510
2040
  const getStepLabel = (step: Token) => {
1511
2041
  const label = step.text.slice(step.text.indexOf(">") + 1);
1512
- const end = { value: label.length };
1513
- while (label[end.value - 1] === ".") end.value--;
1514
- return label.slice(0, end.value);
2042
+ let end = label.length;
2043
+ while (label[end - 1] === ".") end--;
2044
+ return label.slice(0, end);
1515
2045
  };
1516
2046
 
1517
2047
  const isStepReference = (step: Token) =>
@@ -1547,23 +2077,6 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1547
2077
  return isFirst ? level > outer : proofLevels[top] === level;
1548
2078
  };
1549
2079
 
1550
- const isDeclared = (name: string) =>
1551
- !factNames.has(name) &&
1552
- openModules.some(
1553
- (module) =>
1554
- module.constants.some((constant) => constant.name === name) ||
1555
- module.variables.some((variable) => variable.name === name) ||
1556
- module.definitions.some((definition) => definition.name === name),
1557
- );
1558
-
1559
- const isHideFact = (fact: Fact) => {
1560
- if (fact.kind === "ModuleFact" || fact.kind === "Subexpression") return true;
1561
- if (fact.kind === "Apply") return fact.operator.includes("!");
1562
- if (fact.kind !== "Identifier") return false;
1563
- if (fact.name.startsWith("<") || fact.name.includes("!")) return true;
1564
- return !BUILTIN_CONSTANTS.has(fact.name) && !isDeclared(fact.name);
1565
- };
1566
-
1567
2080
  const isFactAhead = () => {
1568
2081
  const token = peek();
1569
2082
  if (token.kind === "Step") return isStepReference(token);
@@ -1574,17 +2087,15 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1574
2087
  return !isDefinitionStart();
1575
2088
  };
1576
2089
 
1577
- // SANY reads an infix or postfix operator alone as its name, but a prefix operator such as - as the
1578
- // start of an expression.
1579
2090
  const parseFact = (): Fact => {
1580
- const start = cursor.index;
2091
+ const start = cursor;
1581
2092
  const token = peek();
1582
2093
  if (
1583
2094
  token.kind === "Operator" &&
1584
2095
  !EXPRESSION_PREFIXES.has(token.text) &&
1585
2096
  isOperatorName(token.text)
1586
2097
  ) {
1587
- cursor.index++;
2098
+ cursor++;
1588
2099
  return withRange(
1589
2100
  {
1590
2101
  kind: "OperatorReference",
@@ -1602,26 +2113,8 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1602
2113
  );
1603
2114
  };
1604
2115
 
1605
- const declareProofSymbol = (name: string) => proofScopes[proofScopes.length - 1]?.add(name);
1606
-
1607
- const withProofScope = <Result>(parse: () => Result) => {
1608
- proofScopes.push(new Set());
1609
- const result = parse();
1610
- proofScopes.pop();
1611
- return result;
1612
- };
1613
-
1614
- const isDeclaredSymbol = (name: string) =>
1615
- proofScopes.some((scope) => scope.has(name)) ||
1616
- openModules.some(
1617
- (module) =>
1618
- module.constants.some((constant) => constant.name === name) ||
1619
- module.variables.some((variable) => variable.name === name),
1620
- );
1621
-
1622
- // tlapm's definable: names and operators joined by !.
1623
2116
  const parseDefinable = (): Fact => {
1624
- const start = cursor.index;
2117
+ const start = cursor;
1625
2118
  const token = peek();
1626
2119
  const parts = parseSeparated(() => {
1627
2120
  const part = peek();
@@ -1629,7 +2122,7 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1629
2122
  if (!isName && part.kind !== "Operator") {
1630
2123
  throw createUnexpected(`Expected a definition name but found ${describeToken(part)}`, part);
1631
2124
  }
1632
- cursor.index++;
2125
+ cursor++;
1633
2126
  return part.text;
1634
2127
  }, "!");
1635
2128
  const location = getLocation(token);
@@ -1641,54 +2134,56 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1641
2134
  );
1642
2135
  };
1643
2136
 
1644
- const parseDefs = () => {
1645
- if (isTlapm) return parseSeparated(parseDefinable);
1646
- const defs = parseSeparated(parseFact);
1647
- const invalid =
1648
- mode === "tlc"
1649
- ? defs.find((def) => def.kind === "Identifier" && isDeclaredSymbol(def.name))
1650
- : undefined;
1651
- if (invalid) {
1652
- throw createError("DEF clause entry should describe a defined operator", invalid.location);
1653
- }
1654
- return defs;
2137
+ const parseDefs = () => parseSeparated(isTlapm ? parseDefinable : parseFact);
2138
+
2139
+ // tlapm's usebody reads facts while an expression starts after the comma.
2140
+ const parseTlapmFacts = (): Fact[] => {
2141
+ const first = attempt(parseExpression);
2142
+ if (!first) return [];
2143
+ const facts = [first];
2144
+ withRestorePoint(() => {
2145
+ while (isAt(",")) {
2146
+ const fact = attempt(() => {
2147
+ advance();
2148
+ return parseExpression();
2149
+ });
2150
+ if (!fact) return;
2151
+ facts.push(fact);
2152
+ }
2153
+ });
2154
+ return facts;
1655
2155
  };
1656
2156
 
1657
- const parseFacts = () => ({
1658
- facts: isFactAhead() ? parseSeparated(parseFact) : [],
1659
- defs: accept("DEF") || accept("DEFS") ? parseDefs() : [],
1660
- });
2157
+ const parseFacts = () => {
2158
+ const facts = isTlapm ? parseTlapmFacts() : isFactAhead() ? parseSeparated(parseFact) : [];
2159
+ const isDef = accept("DEF") || accept("DEFS");
2160
+ if (isTlapm && facts.length === 0 && !isDef) throw createUnexpected("Expected a fact");
2161
+ return { facts, defs: isDef ? parseDefs() : [] };
2162
+ };
1661
2163
 
1662
2164
  const parseUseOrHide = (): UseOrHide => {
1663
- const start = cursor.index;
2165
+ const start = cursor;
1664
2166
  const keyword = advance();
1665
2167
  const isOnly = keyword.text === "USE" && accept("ONLY");
1666
2168
  const keywordRange = getRange(start);
1667
- const use: UseOrHide = {
1668
- kind: keyword.text === "USE" ? "Use" : "Hide",
1669
- isOnly,
1670
- ...parseFacts(),
1671
- keyword: keywordRange,
1672
- location: getLocation(keyword),
1673
- };
1674
- const invalid =
1675
- use.kind === "Hide" && mode === "tlc" ? use.facts.filter((fact) => !isHideFact(fact)) : [];
1676
- if (invalid.length > 0) {
1677
- throw createSanyError(
1678
- invalid.map((fact) => ({
1679
- location: formatRange(fact.location.range ?? getRange(start)),
1680
- message:
1681
- "The only expression allowed as a fact in a HIDE is \nthe name of a theorem, assumption, or step.",
1682
- })),
1683
- );
1684
- }
1685
- return withRange(use, start);
2169
+ return withRange(
2170
+ {
2171
+ kind: keyword.text === "USE" ? "Use" : "Hide",
2172
+ isOnly,
2173
+ ...parseFacts(),
2174
+ keyword: keywordRange,
2175
+ location: getLocation(keyword),
2176
+ },
2177
+ start,
2178
+ );
1686
2179
  };
1687
2180
 
1688
2181
  const parseNewSymbol = (): NewSymbol => {
1689
- const start = cursor.index;
1690
- const location = getLocation(peek());
2182
+ const newStart = cursor;
2183
+ const newLocation = getLocation(peek());
1691
2184
  const isNew = accept("NEW");
2185
+ const start = isTlapm ? cursor : newStart;
2186
+ const location = isTlapm ? getLocation(peek()) : newLocation;
1692
2187
  const level = peek().kind === "Keyword" ? NEW_SYMBOL_LEVELS.get(peek().text) : undefined;
1693
2188
  if (!isNew && level === undefined) {
1694
2189
  throw createUnexpected(`Expected a declaration but found ${describeToken(peek())}`);
@@ -1696,7 +2191,6 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1696
2191
  if (level !== undefined) advance();
1697
2192
  if (level === "Variable") {
1698
2193
  const named = expectName();
1699
- declareProofSymbol(named.name);
1700
2194
  return withRange(
1701
2195
  {
1702
2196
  kind: "NewSymbol",
@@ -1711,7 +2205,6 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1711
2205
  );
1712
2206
  }
1713
2207
  const declaration = parseDeclaration();
1714
- declareProofSymbol(declaration.name);
1715
2208
  const isBounded = (level ?? "Constant") === "Constant" && isAt("\\in");
1716
2209
  if (isBounded && declaration.arity > 0) {
1717
2210
  throw createError("declared symbol with arguments before \\in");
@@ -1731,47 +2224,69 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1731
2224
  );
1732
2225
  };
1733
2226
 
2227
+ const parseHypothesis = () => {
2228
+ if (peek().kind === "Identifier" && isAt("::", 1) && isAt("ASSUME", 2)) {
2229
+ advance();
2230
+ advance();
2231
+ }
2232
+ if (isAt("ASSUME")) return parseAssumeProve();
2233
+ if (peek().kind === "Keyword" && NEW_SYMBOL_STARTS.has(peek().text))
2234
+ return isTlapm ? commit(parseNewSymbol) : parseNewSymbol();
2235
+ if (!isTlapm) return parseExpression();
2236
+ // expr_or_sequent: where no expression starts, the sequent's ASSUME fails last.
2237
+ const expression = attempt(parseExpression);
2238
+ if (expression === undefined) throw createUnexpected("Expected a hypothesis");
2239
+ return expression;
2240
+ };
2241
+
2242
+ // comma1 (hyp b): Pco.star attempts the hypotheses after the first, each inside the one before,
2243
+ // so a failure that backtracks ends the list, and tlapm reports any other before the first comma.
2244
+ const parseTlapmHypotheses = () => {
2245
+ const assumptions = [parseHypothesis()];
2246
+ withRestorePoint(() => {
2247
+ while (true) {
2248
+ const assumption = attempt(() => {
2249
+ expect(",");
2250
+ return parseHypothesis();
2251
+ });
2252
+ if (assumption === undefined) break;
2253
+ assumptions.push(assumption);
2254
+ }
2255
+ });
2256
+ return assumptions;
2257
+ };
2258
+
1734
2259
  const parseAssumeProve = (): AssumeProve => {
1735
- const start = cursor.index;
2260
+ const start = cursor;
1736
2261
  const location = getLocation(expect("ASSUME"));
1737
- const assumptions = parseSeparated(() => {
1738
- if (isAt("ASSUME")) return withProofScope(parseAssumeProve);
1739
- if (peek().kind === "Keyword" && NEW_SYMBOL_STARTS.has(peek().text)) return parseNewSymbol();
1740
- return parseExpression();
1741
- });
1742
- expect("PROVE");
1743
- return withRange(
1744
- { kind: "AssumeProve", assumptions, conclusion: parseExpression(), location },
1745
- start,
1746
- );
2262
+ const parseSequent = (): AssumeProve => {
2263
+ const assumptions = isTlapm ? parseTlapmHypotheses() : parseSeparated(parseHypothesis);
2264
+ expect("PROVE");
2265
+ return withRange(
2266
+ { kind: "AssumeProve", assumptions, conclusion: parseExpression(), location },
2267
+ start,
2268
+ );
2269
+ };
2270
+ return isTlapm ? commit(parseSequent) : parseSequent();
1747
2271
  };
1748
2272
 
1749
2273
  const isNameListAhead = (terminator: string) => {
1750
- const offset = { value: 0 };
1751
- while (peek(offset.value).kind === "Identifier" && isAt(",", offset.value + 1)) {
1752
- offset.value += 2;
2274
+ let offset = 0;
2275
+ while (peek(offset).kind === "Identifier" && isAt(",", offset + 1)) {
2276
+ offset += 2;
1753
2277
  }
1754
- return peek(offset.value).kind === "Identifier" && isAt(terminator, offset.value + 1);
1755
- };
1756
-
1757
- const declareProofSymbols = (names: string[], bounds: Bound[]) => {
1758
- names.forEach(declareProofSymbol);
1759
- bounds.forEach((bound) =>
1760
- (bound.pattern.kind === "Name" ? [bound.pattern.name] : bound.pattern.names).forEach(
1761
- declareProofSymbol,
1762
- ),
1763
- );
2278
+ return peek(offset).kind === "Identifier" && isAt(terminator, offset + 1);
1764
2279
  };
1765
2280
 
1766
2281
  const parseStepBody = (): StepBody => {
1767
2282
  const token = peek();
1768
2283
  if (isAt("USE") || isAt("HIDE")) return parseUseOrHide();
1769
- if (isAt("INSTANCE")) {
1770
- const start = cursor.index;
2284
+ if (!isTlapm && isAt("INSTANCE")) {
2285
+ const start = cursor;
1771
2286
  return withRange(parseInstance(undefined, [], false, getLocation(token)), start);
1772
2287
  }
1773
2288
  if (isAt("DEFINE") || isDefinitionStart()) {
1774
- const start = cursor.index;
2289
+ const start = cursor;
1775
2290
  accept("DEFINE");
1776
2291
  const definitions = [parseDefinition(false)];
1777
2292
  while (isDefinitionStart()) definitions.push(parseDefinition(false));
@@ -1783,7 +2298,6 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1783
2298
  const isBounded = isBoundAhead();
1784
2299
  const names = getNames(isBounded ? [] : parseSeparated(expectName));
1785
2300
  const bounds = isBounded ? parseBounds() : [];
1786
- declareProofSymbols(names.names, bounds);
1787
2301
  return { kind: "Take", names: names.names, nameRanges: names.ranges, bounds };
1788
2302
  }
1789
2303
  if (accept("WITNESS")) {
@@ -1793,7 +2307,6 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1793
2307
  const isNames = isNameListAhead(":");
1794
2308
  const names = getNames(isNames ? parseSeparated(expectName) : []);
1795
2309
  const bounds = isNames ? [] : parseBounds();
1796
- declareProofSymbols(names.names, bounds);
1797
2310
  expect(":");
1798
2311
  return {
1799
2312
  kind: "Pick",
@@ -1819,21 +2332,17 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1819
2332
  if (!isCorrectLevel(step)) {
1820
2333
  throw createError(`${isQed ? "QED step" : "step"}'s number has bad level`, step);
1821
2334
  }
1822
- if (getStepLevel(step) < 0 && getStepName(step) !== undefined) {
2335
+ if (mode !== "sany" && getStepLevel(step) < 0 && getStepName(step) !== undefined) {
1823
2336
  throw createError("<*> and <+> cannot be used for a named step", step);
1824
2337
  }
1825
- cursor.index++;
2338
+ cursor++;
1826
2339
  return { name: getStepName(step), token: step.text, location: getLocation(step) };
1827
2340
  };
1828
2341
 
1829
- // The names an assertion assumes stay in its own proof, and other steps declare theirs for the rest
1830
- // of the enclosing proof.
1831
2342
  const parseStep = (): ProofStep => {
1832
- const start = cursor.index;
2343
+ const start = cursor;
1833
2344
  const step = parseStepStart(isAt("QED", 1));
1834
- const statementStart = cursor.index;
1835
- const scope = new Set<string>();
1836
- proofScopes.push(scope);
2345
+ const statementStart = cursor;
1837
2346
  const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
1838
2347
  const head = getRange(start);
1839
2348
  const statement = getRange(statementStart);
@@ -1842,97 +2351,251 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1842
2351
  throw createError("Proof of a step that does not take a proof");
1843
2352
  }
1844
2353
  const proof = hasProof ? parseProof() : undefined;
1845
- proofScopes.pop();
1846
- if (body.kind !== "Assert" || body.isSuffices) scope.forEach(declareProofSymbol);
1847
2354
  return withRange({ ...step, body, head, statement, proof }, start);
1848
2355
  };
1849
2356
 
1850
- const parseProofBody = (): Proof => {
1851
- const location = getLocation(peek());
1852
- if (isAt("BY") || (isAt("PROOF") && isAt("BY", 1))) {
1853
- accept("PROOF");
1854
- advance();
1855
- const keyword = getRange(cursor.index - 1);
2357
+ const parseLeafProof = (
2358
+ location: SourceLocation,
2359
+ ): Exclude<Proof, { kind: "Steps" }> | undefined => {
2360
+ if (accept("BY")) {
2361
+ const keyword = getRange(cursor - 1);
1856
2362
  return { kind: "By", isOnly: accept("ONLY"), ...parseFacts(), keyword, location };
1857
2363
  }
1858
- const terminal = peek(isAt("PROOF") ? 1 : 0);
1859
- if (
1860
- terminal.kind === "Keyword" &&
1861
- (terminal.text === "OBVIOUS" || terminal.text === "OMITTED")
1862
- ) {
1863
- accept("PROOF");
1864
- advance();
1865
- const keyword = getRange(cursor.index - 1);
1866
- return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", keyword, location };
1867
- }
2364
+ const terminal = peek();
2365
+ if (terminal.kind !== "Keyword" || (terminal.text !== "OBVIOUS" && terminal.text !== "OMITTED"))
2366
+ return undefined;
2367
+ advance();
2368
+ const keyword = getRange(cursor - 1);
2369
+ return { kind: terminal.text === "OBVIOUS" ? "Obvious" : "Omitted", keyword, location };
2370
+ };
2371
+
2372
+ const parseProofBody = (): Proof => {
2373
+ const location = getLocation(peek());
1868
2374
  accept("PROOF");
2375
+ const leaf = parseLeafProof(location);
2376
+ if (leaf) return leaf;
1869
2377
  const steps = [parseStep()];
1870
2378
  while (steps[steps.length - 1].body.kind !== "Qed") steps.push(parseStep());
1871
2379
  return { kind: "Steps", level: getProofLevel(), steps, location };
1872
2380
  };
1873
2381
 
1874
2382
  const parseProof = (): Proof => {
1875
- const start = cursor.index;
2383
+ const start = cursor;
1876
2384
  proofLevels.push(-1);
1877
- const proof = withProofScope(parseProofBody);
2385
+ const proof = parseProofBody();
1878
2386
  proofLevels.pop();
1879
2387
  return withRange(proof, start);
1880
2388
  };
1881
2389
 
1882
- // SANY binds the name of a theorem or assumption that a module already defines to the first
1883
- // definition, warning of the second.
1884
- const addFactDefinition = (module: Module, definition: Definition) => {
1885
- if (module.definitions.some((other) => other.name === definition.name)) return;
1886
- module.definitions.push(definition);
2390
+ const isPreproofStart = () =>
2391
+ peek().kind === "Step" || (peek().kind === "Keyword" && PROOF_STARTS.has(peek().text));
2392
+
2393
+ const getStepSpan = (head: SourceRange, body: StepBody) => {
2394
+ if (body.kind !== "Use" && body.kind !== "Hide") return getSpan(head);
2395
+ const lastDef = body.defs[body.defs.length - 1]?.location.range;
2396
+ return mergeSpans(getSpan({ ...head, end: head.start }), getSpan(lastDef ?? body.keyword));
1887
2397
  };
1888
2398
 
1889
- const parseTheorem = (module: Module) => {
1890
- const start = cursor.index;
1891
- const keyword = advance();
1892
- const isNamed = peek().kind === "Identifier" && isAt("==", 1);
1893
- const name = isNamed ? expectIdentifier() : undefined;
1894
- if (name !== undefined) {
1895
- factNames.add(name);
1896
- advance();
2399
+ const parsePreproof = (): Preproof => {
2400
+ const first = cursor;
2401
+ const location = getLocation(peek());
2402
+ const isProof = accept("PROOF");
2403
+ const leaf = parseLeafProof(location);
2404
+ if (leaf) {
2405
+ const proof = withRange(leaf, first);
2406
+ return { kind: "Leaf", proof, span: getSpan(proof.keyword), first, end: cursor - 1 };
1897
2407
  }
1898
- const parsed = withProofScope(() => {
1899
- const statement = isAt("ASSUME") ? parseAssumeProve() : parseExpression();
1900
- return { statement, proof: isProofStart(peek()) ? parseProof() : undefined };
1901
- });
1902
- const statement = parsed.statement;
1903
- const theorem = withRange(
1904
- { name, statement, proof: parsed.proof, location: getLocation(keyword) },
2408
+ const token = peek();
2409
+ if (token.kind !== "Step") {
2410
+ throw createUnexpected(`Expected a step number but found ${describeToken(token)}`);
2411
+ }
2412
+ const start = cursor;
2413
+ advance();
2414
+ const statementStart = cursor;
2415
+ const body: StepBody = accept("QED") ? { kind: "Qed" } : parseStepBody();
2416
+ const head = getRange(start);
2417
+ return {
2418
+ kind: "Step",
2419
+ token,
2420
+ isProof,
2421
+ body,
2422
+ head,
2423
+ statement: getRange(statementStart),
2424
+ span: getStepSpan(head, body),
2425
+ first,
1905
2426
  start,
2427
+ end: cursor - 1,
2428
+ };
2429
+ };
2430
+
2431
+ const nestProof = (
2432
+ items: Preproof[],
2433
+ index: number,
2434
+ level: number,
2435
+ levelSpan: Span,
2436
+ ): NestedProof => {
2437
+ const item = items[index];
2438
+ if (!item) return { proof: undefined, span: levelSpan, next: index, end: undefined };
2439
+ if (item.kind === "Leaf")
2440
+ return { proof: item.proof, span: item.span, next: index + 1, end: item.end };
2441
+ const nested = nestSteps(items, index, level, levelSpan, true);
2442
+ if (!nested) return { proof: undefined, span: levelSpan, next: index, end: undefined };
2443
+ const location = {
2444
+ ...getLocation(tokens[item.first]),
2445
+ range: getRange(item.first, nested.end),
2446
+ };
2447
+ return {
2448
+ proof: { kind: "Steps", level: nested.level, steps: nested.steps, location },
2449
+ span: nested.span,
2450
+ next: nested.next,
2451
+ end: nested.end,
2452
+ };
2453
+ };
2454
+
2455
+ const nestSteps = (
2456
+ items: Preproof[],
2457
+ index: number,
2458
+ level: number,
2459
+ levelSpan: Span,
2460
+ isFirst: boolean,
2461
+ ): NestedSteps | undefined => {
2462
+ const item = items[index];
2463
+ if (!item) {
2464
+ throw createProofError(
2465
+ `Unexpected end of (sub)proof at level ${level} before QED step`,
2466
+ levelSpan,
2467
+ NEWLINES,
2468
+ );
2469
+ }
2470
+ if (item.kind === "Leaf") {
2471
+ throw createProofError(
2472
+ `Expecting a proof step but found a${LEAF_NAMES[item.proof.kind]} leaf proof`,
2473
+ item.span,
2474
+ NEWLINES,
2475
+ );
2476
+ }
2477
+ if (!isFirst && item.isProof) {
2478
+ throw createProofError(
2479
+ "PROOF keyword found in step that does not begin subproof",
2480
+ item.span,
2481
+ PRINTED_NEWLINE,
2482
+ );
2483
+ }
2484
+ const implicit = item.token.text[1];
2485
+ if (implicit === "+" && !isFirst) {
2486
+ throw createProofError("<+> used but no subproof expected", item.span, PRINTED_NEWLINE);
2487
+ }
2488
+ const stepLevel =
2489
+ implicit === "*"
2490
+ ? level - (isFirst && !item.isProof ? 1 : 0)
2491
+ : implicit === "+"
2492
+ ? level
2493
+ : getStepLevel(item.token);
2494
+ if (stepLevel < level || (!isFirst && stepLevel > level)) return undefined;
2495
+ const proof = PROVABLE_STEPS.has(item.body.kind)
2496
+ ? nestProof(items, index + 1, stepLevel + 1, item.span)
2497
+ : { proof: undefined, span: item.span, next: index + 1, end: undefined };
2498
+ const end = proof.end ?? item.end;
2499
+ const step: ProofStep = {
2500
+ name: getStepName(item.token),
2501
+ token: item.token.text,
2502
+ body: item.body,
2503
+ head: item.head,
2504
+ statement: item.statement,
2505
+ proof: proof.proof,
2506
+ location: { ...getLocation(item.token), range: getRange(item.start, end) },
2507
+ };
2508
+ const span = mergeSpans(item.span, proof.span);
2509
+ if (item.body.kind === "Qed") {
2510
+ return { level: stepLevel, steps: [step], span, next: proof.next, end };
2511
+ }
2512
+ const rest = nestSteps(
2513
+ items,
2514
+ proof.next,
2515
+ stepLevel,
2516
+ { start: span.stop, stop: span.stop },
2517
+ false,
2518
+ );
2519
+ return (
2520
+ rest && {
2521
+ ...rest,
2522
+ level: stepLevel,
2523
+ steps: [step, ...rest.steps],
2524
+ span: mergeSpans(span, rest.span),
2525
+ }
1906
2526
  );
2527
+ };
2528
+
2529
+ const parseTlapmProof = () => {
2530
+ if (!isPreproofStart()) return undefined;
2531
+ const items: Preproof[] = [];
2532
+ while (isPreproofStart()) items.push(parsePreproof());
2533
+ const first = items[0];
2534
+ if (first.kind === "Step") items[0] = { ...first, isProof: true };
2535
+ const nested = nestProof(items, 0, 0, first.span);
2536
+ const extra = items[nested.next];
2537
+ if (extra) {
2538
+ throw createProofError(
2539
+ "extra step(s) after finished proof",
2540
+ extra.span,
2541
+ NO_NEWLINES,
2542
+ "Proof.Parser.toplevel",
2543
+ );
2544
+ }
2545
+ return nested.proof;
2546
+ };
2547
+
2548
+ const addFactDefinition = (
2549
+ module: Module,
2550
+ name: string,
2551
+ body: Expression,
2552
+ location: SourceLocation,
2553
+ ) => {
2554
+ if (module.definitions.some((other) => other.name === name)) return;
2555
+ module.definitions.push({
2556
+ kind: "Operator",
2557
+ name,
2558
+ params: [],
2559
+ body,
2560
+ isLocal: false,
2561
+ isRecursive: false,
2562
+ location,
2563
+ });
2564
+ };
2565
+
2566
+ const parseFactName = () => {
2567
+ if (peek().kind !== "Identifier" || !isAt("==", 1)) return undefined;
2568
+ const name = expectIdentifier();
2569
+ advance();
2570
+ return name;
2571
+ };
2572
+
2573
+ // A theorem whose statement fails to start backtracks in tlapm, whose module ends there.
2574
+ const parseTheoremExpression = () => {
2575
+ if (!isTlapm) return parseExpression();
2576
+ const token = peekRaw();
2577
+ const statement = attempt(parseExpression);
2578
+ if (statement) return statement;
2579
+ throw createUnexpected(`Expected "====" but found ${describeToken(token)}`, token);
2580
+ };
2581
+
2582
+ const parseTheorem = (module: Module) => {
2583
+ const start = cursor;
2584
+ const keyword = advance();
2585
+ const name = parseFactName();
2586
+ const statement = isAt("ASSUME") ? parseAssumeProve() : parseTheoremExpression();
2587
+ const proof = isTlapm ? parseTlapmProof() : isProofStart(peek()) ? parseProof() : undefined;
2588
+ const theorem = withRange({ name, statement, proof, location: getLocation(keyword) }, start);
1907
2589
  module.theorems.push(theorem);
1908
2590
  module.units.push({ kind: "Theorem", theorem });
1909
2591
  if (name === undefined || statement.kind === "AssumeProve") return;
1910
- addFactDefinition(
1911
- module,
1912
- withRange(
1913
- {
1914
- kind: "Operator",
1915
- name,
1916
- params: [],
1917
- body: statement,
1918
- isLocal: false,
1919
- isRecursive: false,
1920
- location: getLocation(keyword),
1921
- },
1922
- start,
1923
- ),
1924
- );
2592
+ addFactDefinition(module, name, statement, theorem.location);
1925
2593
  };
1926
2594
 
1927
2595
  const parseAssumption = (): Assumption => {
1928
- const start = cursor.index;
2596
+ const start = cursor;
1929
2597
  const keyword = advance();
1930
- const isNamed = peek().kind === "Identifier" && isAt("==", 1);
1931
- const name = isNamed ? expectIdentifier() : undefined;
1932
- if (name !== undefined) {
1933
- factNames.add(name);
1934
- advance();
1935
- }
2598
+ const name = parseFactName();
1936
2599
  return withRange(
1937
2600
  {
1938
2601
  name,
@@ -1968,14 +2631,13 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
1968
2631
  recursiveScopes.push(new Set());
1969
2632
  if (accept("EXTENDS")) {
1970
2633
  parseSeparated(() => {
1971
- const start = cursor.index;
2634
+ const start = cursor;
1972
2635
  module.extends.push(expectModuleName());
1973
2636
  module.extendsRanges.push(getRange(start));
1974
2637
  });
1975
2638
  }
1976
2639
  while (true) {
1977
- const tokenIndex = cursor.index;
1978
- unitStart.index = tokenIndex;
2640
+ const tokenIndex = cursor;
1979
2641
  const token = peek();
1980
2642
  if (token.kind === "ModuleEnd") {
1981
2643
  location.range = {
@@ -2012,7 +2674,7 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
2012
2674
  case "VARIABLES": {
2013
2675
  const unitLocation = getLocation(advance());
2014
2676
  const declarations = parseSeparated(() => {
2015
- const start = cursor.index;
2677
+ const start = cursor;
2016
2678
  const location = getLocation(peek());
2017
2679
  return withRange({ name: expectIdentifier(), arity: 0, location }, start);
2018
2680
  });
@@ -2028,21 +2690,12 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
2028
2690
  module.assumptions.push(assumption);
2029
2691
  module.units.push({ kind: "Assumption", assumption });
2030
2692
  if (assumption.name !== undefined) {
2031
- addFactDefinition(module, {
2032
- kind: "Operator",
2033
- name: assumption.name,
2034
- params: [],
2035
- body: assumption.expression,
2036
- isLocal: false,
2037
- isRecursive: false,
2038
- location: assumption.location,
2039
- });
2693
+ addFactDefinition(module, assumption.name, assumption.expression, assumption.location);
2040
2694
  }
2041
2695
  continue;
2042
2696
  }
2043
2697
  case "USE":
2044
2698
  case "HIDE": {
2045
- // SANY's module body takes a USE only when ONLY doesn't follow it.
2046
2699
  if (isAt("USE") && isAt("ONLY", 1)) {
2047
2700
  throw createUnexpected(`Expected a module unit but found ${describeToken(token)}`);
2048
2701
  }
@@ -2051,10 +2704,13 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
2051
2704
  module.units.push({ kind: "Use", use });
2052
2705
  continue;
2053
2706
  }
2054
- case "RECURSIVE":
2055
- advance();
2056
- module.units.push({ kind: "Recursive", declarations: declareRecursive() });
2707
+ case "RECURSIVE": {
2708
+ const unitLocation = getLocation(advance());
2709
+ const declarations = declareRecursive();
2710
+ unitLocation.range = getRange(tokenIndex);
2711
+ module.units.push({ kind: "Recursive", declarations, location: unitLocation });
2057
2712
  continue;
2713
+ }
2058
2714
  case "INSTANCE":
2059
2715
  addDefinitionUnit(
2060
2716
  module,
@@ -2093,16 +2749,13 @@ const parse = (source: string, mode: ParseMode, sourceTokens = tokenize(source))
2093
2749
  };
2094
2750
 
2095
2751
  const module = parseModuleBody();
2096
- // SANY's lexer marks where each definition begins before its parser runs, which can split a
2097
- // module's name from a keyword that the parser would take for one.
2098
- const sanyError = keywordModuleName.isUsed ? getSanyParseError(source) : undefined;
2752
+ const sanyError = isKeywordModuleName ? getSanyParseError(source) : undefined;
2099
2753
  if (sanyError !== undefined) throw new TlaSyntaxError(sanyError);
2100
2754
  return module;
2101
2755
  };
2102
2756
 
2103
2757
  export const parseModule = (source: string) => parse(source, "tlc");
2104
2758
 
2105
- // Parses a module the caller has tokenized already.
2106
2759
  export const parseModuleTokens = (source: string, tokens: Token[]) => parse(source, "tlc", tokens);
2107
2760
 
2108
2761
  export const parseTlapmModule = (source: string) => parse(source, "tlapm");