tla-ts 0.0.4-dev.2be6058 → 0.0.4-dev.2da24e1

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 (314) hide show
  1. package/README.md +37 -126
  2. package/dist/deciders.cjs +366 -0
  3. package/dist/deciders.js +366 -0
  4. package/dist/index.cjs +1 -1
  5. package/dist/index.d.cts +35 -1
  6. package/dist/index.d.ts +35 -1
  7. package/dist/index.js +1 -1
  8. package/dist/parallel-worker.cjs +1 -4
  9. package/dist/parallel-worker.js +1 -4
  10. package/dist/prove-worker.cjs +9 -0
  11. package/dist/prove-worker.d.cts +9 -0
  12. package/dist/prove-worker.d.ts +9 -0
  13. package/dist/prove-worker.js +9 -0
  14. package/dist/prove.cjs +996 -0
  15. package/dist/prove.js +996 -0
  16. package/dist/run-cli.cjs +37 -47
  17. package/dist/run-cli.js +37 -47
  18. package/dist/tlapm.cjs +10 -0
  19. package/dist/tlapm.js +10 -0
  20. package/dist/transfer.cjs +38 -44
  21. package/dist/transfer.js +38 -44
  22. package/dist/worker.cjs +21 -0
  23. package/dist/worker.js +21 -0
  24. package/package.json +21 -12
  25. package/src/check.ts +1 -0
  26. package/src/codegen.ts +26 -7
  27. package/src/errors.ts +86 -0
  28. package/src/evaluate.ts +255 -56
  29. package/src/index.ts +10 -0
  30. package/src/{obligation-action.ts → obligations/action.ts} +247 -69
  31. package/src/{obligation-anon.ts → obligations/anon.ts} +49 -47
  32. package/src/obligations/ctx.ts +64 -0
  33. package/src/{obligation-deref.ts → obligations/deref.ts} +51 -92
  34. package/src/{obligation-expression.ts → obligations/expression.ts} +117 -57
  35. package/src/{obligation-format.ts → obligations/format.ts} +7 -8
  36. package/src/{obligation-generate.ts → obligations/generate.ts} +170 -192
  37. package/src/{obligations.ts → obligations/index.ts} +32 -16
  38. package/src/{obligation-levels.ts → obligations/levels.ts} +26 -28
  39. package/src/{obligation-module.ts → obligations/module.ts} +113 -121
  40. package/src/obligations/pickle.ts +98 -0
  41. package/src/obligations/pragma.ts +189 -0
  42. package/src/{obligation-printer.ts → obligations/printer.ts} +22 -80
  43. package/src/{obligation-proof.ts → obligations/proof.ts} +7 -2
  44. package/src/{obligation-source.ts → obligations/source.ts} +154 -99
  45. package/src/obligations/temporal.ts +321 -0
  46. package/src/{obligation-tuples.ts → obligations/tuples.ts} +38 -54
  47. package/src/ocaml-marshal.ts +166 -0
  48. package/src/parallel-worker.ts +2 -18
  49. package/src/parallel.ts +10 -43
  50. package/src/prove/decide-pool.ts +95 -0
  51. package/src/prove/deciders.ts +33 -0
  52. package/src/prove/first-order.ts +78 -0
  53. package/src/prove/isabelle/LICENSE +36 -0
  54. package/src/prove/isabelle/LICENSE-tlapm +26 -0
  55. package/src/prove/isabelle/decide.ts +47 -0
  56. package/src/prove/isabelle/encode.ts +446 -0
  57. package/src/prove/isabelle/isar/inner-syntax.ts +579 -0
  58. package/src/prove/isabelle/isar/method.ts +367 -0
  59. package/src/prove/isabelle/isar/proof.ts +28 -0
  60. package/src/prove/isabelle/isar/statement.ts +57 -0
  61. package/src/prove/isabelle/isar/theory.ts +192 -0
  62. package/src/prove/isabelle/isar/type-check.ts +278 -0
  63. package/src/prove/isabelle/provers/blast/blast.ts +258 -0
  64. package/src/prove/isabelle/provers/blast/branch.ts +322 -0
  65. package/src/prove/isabelle/provers/blast/prove.ts +347 -0
  66. package/src/prove/isabelle/provers/blast/rules.ts +330 -0
  67. package/src/prove/isabelle/provers/blast/state.ts +180 -0
  68. package/src/prove/isabelle/provers/blast/term.ts +547 -0
  69. package/src/prove/isabelle/provers/clasimp.ts +150 -0
  70. package/src/prove/isabelle/provers/classical.ts +389 -0
  71. package/src/prove/isabelle/provers/hypsubst.ts +441 -0
  72. package/src/prove/isabelle/pure/bicompose.ts +174 -0
  73. package/src/prove/isabelle/pure/consts.ts +32 -0
  74. package/src/prove/isabelle/pure/conv.ts +79 -0
  75. package/src/prove/isabelle/pure/drule.ts +264 -0
  76. package/src/prove/isabelle/pure/envir.ts +363 -0
  77. package/src/prove/isabelle/pure/goal.ts +53 -0
  78. package/src/prove/isabelle/pure/interrupt.ts +16 -0
  79. package/src/prove/isabelle/pure/list.ts +12 -0
  80. package/src/prove/isabelle/pure/logic.ts +402 -0
  81. package/src/prove/isabelle/pure/name.ts +110 -0
  82. package/src/prove/isabelle/pure/net.ts +262 -0
  83. package/src/prove/isabelle/pure/object-logic.ts +41 -0
  84. package/src/prove/isabelle/pure/pattern.ts +650 -0
  85. package/src/prove/isabelle/pure/raw-simplifier.ts +353 -0
  86. package/src/prove/isabelle/pure/rewriter.ts +602 -0
  87. package/src/prove/isabelle/pure/search.ts +68 -0
  88. package/src/prove/isabelle/pure/seq.ts +156 -0
  89. package/src/prove/isabelle/pure/simplifier.ts +112 -0
  90. package/src/prove/isabelle/pure/table.ts +208 -0
  91. package/src/prove/isabelle/pure/tactic.ts +276 -0
  92. package/src/prove/isabelle/pure/tactical.ts +347 -0
  93. package/src/prove/isabelle/pure/term-ord.ts +159 -0
  94. package/src/prove/isabelle/pure/term.ts +808 -0
  95. package/src/prove/isabelle/pure/thm.ts +806 -0
  96. package/src/prove/isabelle/pure/type.ts +133 -0
  97. package/src/prove/isabelle/pure/unify.ts +753 -0
  98. package/src/prove/isabelle/pure/variable.ts +25 -0
  99. package/src/prove/isabelle/tla/classical-setup.ts +101 -0
  100. package/src/prove/isabelle/tla/context.ts +74 -0
  101. package/src/prove/isabelle/tla/logic-data.ts +5 -0
  102. package/src/prove/isabelle/tla/logic.ts +215 -0
  103. package/src/prove/isabelle/tla/simpdata.ts +241 -0
  104. package/src/prove/isabelle/tools/intuitionistic.ts +132 -0
  105. package/src/prove/ocaml.ts +22 -0
  106. package/src/prove/prep/action.ts +508 -0
  107. package/src/prove/prep/defs.ts +33 -0
  108. package/src/prove/prep/elab.ts +156 -0
  109. package/src/prove/prep/enabled-axioms.ts +189 -0
  110. package/src/prove/prep/enabled.ts +43 -0
  111. package/src/prove/prep/fingerprint.ts +388 -0
  112. package/src/prove/prep/flatten.ts +70 -0
  113. package/src/prove/prep/level-comparison.ts +529 -0
  114. package/src/prove/prep/md5.ts +67 -0
  115. package/src/prove/prep/pragma.ts +221 -0
  116. package/src/prove/prep/trivial.ts +22 -0
  117. package/src/prove/prove-worker.ts +22 -0
  118. package/src/prove/prove.ts +498 -0
  119. package/src/prove/ptl/decide.ts +68 -0
  120. package/src/prove/ptl/encode.ts +255 -0
  121. package/src/prove/ptl/sat.ts +151 -0
  122. package/src/prove/ptl/tableau.ts +276 -0
  123. package/src/prove/smt/axiomatize.ts +69 -0
  124. package/src/prove/smt/axioms.ts +209 -0
  125. package/src/prove/smt/context.ts +108 -0
  126. package/src/prove/smt/data.ts +636 -0
  127. package/src/prove/smt/encode.ts +164 -0
  128. package/src/prove/smt/extensionality.ts +65 -0
  129. package/src/prove/smt/flatten.ts +112 -0
  130. package/src/prove/smt/format.ts +154 -0
  131. package/src/prove/smt/instances.ts +181 -0
  132. package/src/prove/smt/rewrite.ts +772 -0
  133. package/src/prove/smt/simplify.ts +57 -0
  134. package/src/prove/smt/solver/act-cache.ts +265 -0
  135. package/src/prove/smt/solver/arith-rewriter.ts +962 -0
  136. package/src/prove/smt/solver/array.ts +22 -0
  137. package/src/prove/smt/solver/asserted-formulas.ts +469 -0
  138. package/src/prove/smt/solver/ast-lt.ts +157 -0
  139. package/src/prove/smt/solver/ast-util.ts +147 -0
  140. package/src/prove/smt/solver/ast.ts +1315 -0
  141. package/src/prove/smt/solver/auf-solver.ts +572 -0
  142. package/src/prove/smt/solver/case-split-queue.ts +178 -0
  143. package/src/prove/smt/solver/cg-table.ts +175 -0
  144. package/src/prove/smt/solver/clause.ts +276 -0
  145. package/src/prove/smt/solver/conflict-resolution.ts +343 -0
  146. package/src/prove/smt/solver/context.ts +1292 -0
  147. package/src/prove/smt/solver/ctx-simplify.ts +334 -0
  148. package/src/prove/smt/solver/der.ts +224 -0
  149. package/src/prove/smt/solver/dyn-ack.ts +141 -0
  150. package/src/prove/smt/solver/e-matching.ts +659 -0
  151. package/src/prove/smt/solver/enode.ts +259 -0
  152. package/src/prove/smt/solver/expr-ref.ts +21 -0
  153. package/src/prove/smt/solver/expr-safe-replace.ts +172 -0
  154. package/src/prove/smt/solver/fingerprints.ts +92 -0
  155. package/src/prove/smt/solver/flat-literals.ts +51 -0
  156. package/src/prove/smt/solver/goal.ts +138 -0
  157. package/src/prove/smt/solver/hash.ts +173 -0
  158. package/src/prove/smt/solver/hashtable.ts +166 -0
  159. package/src/prove/smt/solver/internalizer.ts +1381 -0
  160. package/src/prove/smt/solver/lbool.ts +5 -0
  161. package/src/prove/smt/solver/literal.ts +24 -0
  162. package/src/prove/smt/solver/lp/bound-propagator.ts +391 -0
  163. package/src/prove/smt/solver/lp/constraints.ts +78 -0
  164. package/src/prove/smt/solver/lp/containers.ts +296 -0
  165. package/src/prove/smt/solver/lp/core-solver.ts +504 -0
  166. package/src/prove/smt/solver/lp/cross-nested.ts +224 -0
  167. package/src/prove/smt/solver/lp/dep-intervals.ts +656 -0
  168. package/src/prove/smt/solver/lp/dependency.ts +51 -0
  169. package/src/prove/smt/solver/lp/dioph-eq.ts +1181 -0
  170. package/src/prove/smt/solver/lp/explanation.ts +46 -0
  171. package/src/prove/smt/solver/lp/gomory.ts +255 -0
  172. package/src/prove/smt/solver/lp/hnf-cutter.ts +125 -0
  173. package/src/prove/smt/solver/lp/hnf.ts +281 -0
  174. package/src/prove/smt/solver/lp/int-cube.ts +295 -0
  175. package/src/prove/smt/solver/lp/int-solver.ts +514 -0
  176. package/src/prove/smt/solver/lp/lar-solver.ts +816 -0
  177. package/src/prove/smt/solver/lp/lar-term.ts +62 -0
  178. package/src/prove/smt/solver/lp/lp-settings.ts +91 -0
  179. package/src/prove/smt/solver/lp/maximize-term.ts +347 -0
  180. package/src/prove/smt/solver/lp/nex-creator.ts +381 -0
  181. package/src/prove/smt/solver/lp/nex.ts +120 -0
  182. package/src/prove/smt/solver/lp/nla-backoff.ts +43 -0
  183. package/src/prove/smt/solver/lp/nla-basics.ts +398 -0
  184. package/src/prove/smt/solver/lp/nla-core.ts +968 -0
  185. package/src/prove/smt/solver/lp/nla-divisions.ts +332 -0
  186. package/src/prove/smt/solver/lp/nla-emonics.ts +373 -0
  187. package/src/prove/smt/solver/lp/nla-factorization.ts +30 -0
  188. package/src/prove/smt/solver/lp/nla-grobner.ts +904 -0
  189. package/src/prove/smt/solver/lp/nla-horner.ts +126 -0
  190. package/src/prove/smt/solver/lp/nla-intervals.ts +265 -0
  191. package/src/prove/smt/solver/lp/nla-monomial-bounds.ts +715 -0
  192. package/src/prove/smt/solver/lp/nla-monotone.ts +59 -0
  193. package/src/prove/smt/solver/lp/nla-order.ts +307 -0
  194. package/src/prove/smt/solver/lp/nla-patcher.ts +112 -0
  195. package/src/prove/smt/solver/lp/nla-tangents.ts +251 -0
  196. package/src/prove/smt/solver/lp/nla-throttle.ts +85 -0
  197. package/src/prove/smt/solver/lp/nla-types.ts +124 -0
  198. package/src/prove/smt/solver/lp/nla-var-eqs.ts +141 -0
  199. package/src/prove/smt/solver/lp/nra-solver.ts +440 -0
  200. package/src/prove/smt/solver/lp/numeric-pair.ts +65 -0
  201. package/src/prove/smt/solver/lp/pdd-interval.ts +31 -0
  202. package/src/prove/smt/solver/lp/pdd-simplifier.ts +184 -0
  203. package/src/prove/smt/solver/lp/pdd-solver.ts +408 -0
  204. package/src/prove/smt/solver/lp/pdd.ts +971 -0
  205. package/src/prove/smt/solver/lp/random-updater.ts +138 -0
  206. package/src/prove/smt/solver/lp/static-matrix.ts +246 -0
  207. package/src/prove/smt/solver/lp/union-find.ts +96 -0
  208. package/src/prove/smt/solver/mam-compiler.ts +847 -0
  209. package/src/prove/smt/solver/mam-interpreter.ts +400 -0
  210. package/src/prove/smt/solver/maximize-ac-sharing.ts +118 -0
  211. package/src/prove/smt/solver/model-checker.ts +347 -0
  212. package/src/prove/smt/solver/model-evaluator.ts +262 -0
  213. package/src/prove/smt/solver/model-finder.ts +155 -0
  214. package/src/prove/smt/solver/model-generator.ts +97 -0
  215. package/src/prove/smt/solver/model-macro-solver.ts +728 -0
  216. package/src/prove/smt/solver/nlsat/algebraic.ts +1161 -0
  217. package/src/prove/smt/solver/nlsat/atoms.ts +238 -0
  218. package/src/prove/smt/solver/nlsat/evaluator.ts +303 -0
  219. package/src/prove/smt/solver/nlsat/explain.ts +607 -0
  220. package/src/prove/smt/solver/nlsat/interval-set.ts +351 -0
  221. package/src/prove/smt/solver/nlsat/levelwise.ts +740 -0
  222. package/src/prove/smt/solver/nlsat/mpbq.ts +161 -0
  223. package/src/prove/smt/solver/nlsat/polynomial.ts +1722 -0
  224. package/src/prove/smt/solver/nlsat/solver.ts +911 -0
  225. package/src/prove/smt/solver/nlsat/types.ts +43 -0
  226. package/src/prove/smt/solver/nlsat/upolynomial-factorization.ts +648 -0
  227. package/src/prove/smt/solver/nlsat/upolynomial.ts +996 -0
  228. package/src/prove/smt/solver/nnf.ts +469 -0
  229. package/src/prove/smt/solver/pattern-inference.ts +351 -0
  230. package/src/prove/smt/solver/poly-rewriter.ts +477 -0
  231. package/src/prove/smt/solver/propagate-values.ts +83 -0
  232. package/src/prove/smt/solver/proto-model.ts +551 -0
  233. package/src/prove/smt/solver/pull-quant.ts +139 -0
  234. package/src/prove/smt/solver/qe-lite-der.ts +526 -0
  235. package/src/prove/smt/solver/qe-lite-fm.ts +576 -0
  236. package/src/prove/smt/solver/qe-lite.ts +381 -0
  237. package/src/prove/smt/solver/qi-queue.ts +362 -0
  238. package/src/prove/smt/solver/quant-tactics.ts +117 -0
  239. package/src/prove/smt/solver/quantifier-analyzer.ts +562 -0
  240. package/src/prove/smt/solver/quantifier-manager.ts +186 -0
  241. package/src/prove/smt/solver/random-gen.ts +10 -0
  242. package/src/prove/smt/solver/rational.ts +165 -0
  243. package/src/prove/smt/solver/ref-vector.ts +62 -0
  244. package/src/prove/smt/solver/relevancy.ts +247 -0
  245. package/src/prove/smt/solver/rewriter-tpl.ts +1159 -0
  246. package/src/prove/smt/solver/rewriter.ts +843 -0
  247. package/src/prove/smt/solver/rlimit.ts +79 -0
  248. package/src/prove/smt/solver/smtlib-parser.ts +526 -0
  249. package/src/prove/smt/solver/solver.ts +71 -0
  250. package/src/prove/smt/solver/sort.ts +35 -0
  251. package/src/prove/smt/solver/sticky-generations.ts +69 -0
  252. package/src/prove/smt/solver/theory-lra-axioms.ts +340 -0
  253. package/src/prove/smt/solver/theory-lra-bounds.ts +226 -0
  254. package/src/prove/smt/solver/theory-lra-final-check.ts +252 -0
  255. package/src/prove/smt/solver/theory-lra-internalize.ts +476 -0
  256. package/src/prove/smt/solver/theory-lra-model.ts +243 -0
  257. package/src/prove/smt/solver/theory-lra-propagate.ts +405 -0
  258. package/src/prove/smt/solver/theory-lra-state.ts +43 -0
  259. package/src/prove/smt/solver/theory-lra.ts +56 -0
  260. package/src/prove/smt/solver/var-subst.ts +258 -0
  261. package/src/prove/smt/solver/z3-license.txt +10 -0
  262. package/src/prove/smt/table.ts +170 -0
  263. package/src/prove/tlapm/tlapm.ts +96 -0
  264. package/src/prove/tlaps-module.ts +510 -0
  265. package/src/prove/verdict.ts +18 -0
  266. package/src/prove/zenon/LICENSE +32 -0
  267. package/src/prove/zenon/check/check.ts +105 -0
  268. package/src/prove/zenon/check/definitions.ts +144 -0
  269. package/src/prove/zenon/check/equality.ts +182 -0
  270. package/src/prove/zenon/check/functions.ts +62 -0
  271. package/src/prove/zenon/check/keys.ts +125 -0
  272. package/src/prove/zenon/check/propositional.ts +104 -0
  273. package/src/prove/zenon/check/records.ts +188 -0
  274. package/src/prove/zenon/check/relations.ts +121 -0
  275. package/src/prove/zenon/check/rewrites.ts +224 -0
  276. package/src/prove/zenon/check/sets.ts +302 -0
  277. package/src/prove/zenon/check/terms.ts +127 -0
  278. package/src/prove/zenon/check/tla.ts +126 -0
  279. package/src/prove/zenon/decide.ts +113 -0
  280. package/src/prove/zenon/encode.ts +379 -0
  281. package/src/prove/zenon/eqrel.ts +300 -0
  282. package/src/prove/zenon/equality-rules.ts +435 -0
  283. package/src/prove/zenon/expr.ts +551 -0
  284. package/src/prove/zenon/index-table.ts +350 -0
  285. package/src/prove/zenon/isar-case.ts +513 -0
  286. package/src/prove/zenon/isar.ts +1249 -0
  287. package/src/prove/zenon/limits.ts +15 -0
  288. package/src/prove/zenon/llproof.ts +173 -0
  289. package/src/prove/zenon/mltoll.ts +1566 -0
  290. package/src/prove/zenon/node.ts +297 -0
  291. package/src/prove/zenon/ocaml.ts +169 -0
  292. package/src/prove/zenon/parse.ts +247 -0
  293. package/src/prove/zenon/phrase.ts +191 -0
  294. package/src/prove/zenon/proof.ts +198 -0
  295. package/src/prove/zenon/propositional-rules.ts +387 -0
  296. package/src/prove/zenon/rules.ts +94 -0
  297. package/src/prove/zenon/search.ts +288 -0
  298. package/src/prove/zenon/tla/functions.ts +151 -0
  299. package/src/prove/zenon/tla/llproof.ts +480 -0
  300. package/src/prove/zenon/tla/records.ts +225 -0
  301. package/src/prove/zenon/tla/rewrites.ts +447 -0
  302. package/src/prove/zenon/tla/sets.ts +304 -0
  303. package/src/prove/zenon/tla/terms.ts +181 -0
  304. package/src/prove/zenon/tla/tla.ts +506 -0
  305. package/src/run-cli.ts +283 -81
  306. package/src/standard-modules.ts +5 -2
  307. package/src/successors.ts +25 -6
  308. package/src/tlapm-cache.ts +236 -0
  309. package/src/tlc-random.ts +55 -32
  310. package/src/tlc-version.ts +8 -7
  311. package/src/values.ts +97 -47
  312. package/src/worker.ts +65 -0
  313. package/dist/parallel.cjs +0 -12
  314. package/dist/parallel.js +0 -12
@@ -0,0 +1,529 @@
1
+ // A port of tlapm's Expr.LevelComparison, the LevelComparison frontend: it proves an obligation
2
+ // whose goal is a visible fact, or the goal of a visible ASSUME/PROVE fact, with identifiers
3
+ // renamed to identifiers of no higher level, each renamed one declared by the fact.
4
+
5
+ import { TlapmBugError, TlaProverError } from "../../errors";
6
+ import {
7
+ adjoin,
8
+ adjoinAll,
9
+ getBoundedFresh,
10
+ getBoundFresh,
11
+ getFresh,
12
+ getLocalDefn,
13
+ getParamFresh,
14
+ getScopeAt,
15
+ getScopeHyps,
16
+ type Bound,
17
+ type Defn,
18
+ type Expr,
19
+ type Hyp,
20
+ type Level,
21
+ type Param,
22
+ type Scope,
23
+ type Sequent,
24
+ } from "../../obligations/expression";
25
+ import { getLevelInfo } from "../../obligations/levels";
26
+
27
+ interface Declaration {
28
+ name: string;
29
+ kind: Level | "Variable" | "Definition";
30
+ level: number;
31
+ }
32
+
33
+ const RANKS: Partial<Record<Declaration["kind"], number>> = {
34
+ Constant: 0,
35
+ State: 1,
36
+ Action: 2,
37
+ Temporal: 3,
38
+ };
39
+
40
+ // The lists match item by item, and tlapm compares the items before it finds a missing one.
41
+ const isPairwise = <Item>(
42
+ left: Item[],
43
+ right: Item[],
44
+ isSameItem: (left: Item, right: Item) => boolean,
45
+ ) =>
46
+ left.every((item, index) => index < right.length && isSameItem(item, right[index])) &&
47
+ left.length === right.length;
48
+
49
+ // Util.Coll.Sm built from a list, where the first entry of a name wins, in key order.
50
+ const getSortedFields = (fields: { name: string; value: Expr }[]) =>
51
+ [...new Map(fields.toReversed().map((field) => [field.name, field.value]))].toSorted(
52
+ (first, second) => (first[0] < second[0] ? -1 : 1),
53
+ );
54
+
55
+ const getDeclaration = (scope: Scope | undefined, index: number): Declaration => {
56
+ const hypScope = getScopeAt(scope, index - 1);
57
+ if (!hypScope) throw new TlapmBugError();
58
+ const hyp = hypScope.hyp;
59
+ if (hyp.kind === "Fresh")
60
+ return { name: hyp.name, kind: hyp.level, level: RANKS[hyp.level] ?? -1 };
61
+ if (hyp.kind === "Flex") return { name: hyp.name, kind: "Variable", level: 1 };
62
+ if (hyp.kind === "Defn" && hyp.definition.kind === "Operator") {
63
+ const level = getLevelInfo(hypScope.rest, hyp.definition.body).level;
64
+ return { name: hyp.definition.name, kind: "Definition", level };
65
+ }
66
+ throw new TlapmBugError();
67
+ };
68
+
69
+ const bindBounds = (scope: Scope | undefined, bounds: Bound[]) =>
70
+ adjoinAll(scope, bounds.map(getBoundFresh));
71
+
72
+ const bindParams = (scope: Scope | undefined, params: Param[]) =>
73
+ adjoinAll(scope, params.map(getParamFresh));
74
+
75
+ const bindDefinitions = (scope: Scope | undefined, definitions: Defn[]) =>
76
+ adjoinAll(scope, definitions.map(getLocalDefn));
77
+
78
+ const bindFlexible = (scope: Scope | undefined, names: string[]) =>
79
+ adjoinAll(
80
+ scope,
81
+ names.map((name) => ({ kind: "Flex", name })),
82
+ );
83
+
84
+ // level_comparison#compare: the expressions match up to a renaming that maps each name one way,
85
+ // and the fact's context declares each name it changes.
86
+ const compare = (
87
+ factScope: Scope | undefined,
88
+ goalScope: Scope | undefined,
89
+ fact: Expr,
90
+ goal: Expr,
91
+ ) => {
92
+ const renaming = new Map<string, string>();
93
+
94
+ const isSameIndex = (
95
+ leftScope: Scope | undefined,
96
+ rightScope: Scope | undefined,
97
+ leftIndex: number,
98
+ rightIndex: number,
99
+ ) => {
100
+ const left = getDeclaration(leftScope, leftIndex);
101
+ const right = getDeclaration(rightScope, rightIndex);
102
+ if (left.kind === "Unknown") return leftIndex === rightIndex;
103
+ if (left.kind === "Variable" || left.kind === "Definition") {
104
+ return left.name === right.name && right.kind === left.kind;
105
+ }
106
+ const rightRank = RANKS[right.kind];
107
+ const isLower =
108
+ (rightRank !== undefined && rightRank <= left.level) ||
109
+ (right.kind === "Definition" && right.level <= left.level);
110
+ if (!isLower) return false;
111
+ const renamed = renaming.get(left.name);
112
+ if (renamed !== undefined) return renamed === right.name;
113
+ renaming.set(left.name, right.name);
114
+ return true;
115
+ };
116
+
117
+ const isSameDefinition = (
118
+ leftScope: Scope | undefined,
119
+ rightScope: Scope | undefined,
120
+ left: Defn,
121
+ right: Defn,
122
+ ): boolean => {
123
+ if (
124
+ (left.kind === "Operator" && right.kind === "Operator") ||
125
+ (left.kind === "Bpragma" && right.kind === "Bpragma")
126
+ ) {
127
+ return isSame(leftScope, rightScope, left.body, right.body);
128
+ }
129
+ if (left.kind !== "Instance" || right.kind !== "Instance") return false;
130
+ const leftArgs = left.instance.args;
131
+ const rightArgs = right.instance.args;
132
+ const bindArgs = (scope: Scope | undefined, args: string[]) =>
133
+ adjoinAll(
134
+ scope,
135
+ args.map((name) => getFresh(name, "Unknown")),
136
+ );
137
+ const getFields = (definition: typeof left.instance) =>
138
+ getSortedFields(
139
+ definition.substitutions.map((entry) => ({ name: entry.name, value: entry.expression })),
140
+ );
141
+ return (
142
+ isPairwise(leftArgs, rightArgs, (leftArg, rightArg) => leftArg === rightArg) &&
143
+ left.instance.module === right.instance.module &&
144
+ isSameFields(
145
+ bindArgs(leftScope, leftArgs),
146
+ bindArgs(rightScope, rightArgs),
147
+ getFields(left.instance),
148
+ getFields(right.instance),
149
+ )
150
+ );
151
+ };
152
+
153
+ const isSameFields = (
154
+ leftScope: Scope | undefined,
155
+ rightScope: Scope | undefined,
156
+ left: [string, Expr][],
157
+ right: [string, Expr][],
158
+ ) =>
159
+ isPairwise(
160
+ left,
161
+ right,
162
+ (leftField, rightField) =>
163
+ leftField[0] === rightField[0] &&
164
+ isSame(leftScope, rightScope, leftField[1], rightField[1]),
165
+ );
166
+
167
+ const isSameBounds = (
168
+ leftScope: Scope | undefined,
169
+ rightScope: Scope | undefined,
170
+ left: Bound[],
171
+ right: Bound[],
172
+ ) =>
173
+ isPairwise(left, right, (leftBound, rightBound) => {
174
+ if (leftBound.level !== rightBound.level) return false;
175
+ const leftDomain = leftBound.domain;
176
+ const rightDomain = rightBound.domain;
177
+ if (leftDomain.kind === "Domain" && rightDomain.kind === "Domain") {
178
+ return isSame(leftScope, rightScope, leftDomain.expression, rightDomain.expression);
179
+ }
180
+ return leftDomain.kind === rightDomain.kind && leftDomain.kind !== "Domain";
181
+ });
182
+
183
+ const isSameHyp = (
184
+ leftScope: Scope | undefined,
185
+ rightScope: Scope | undefined,
186
+ left: Hyp,
187
+ right: Hyp,
188
+ ) => {
189
+ if (left.kind === "Fresh" && right.kind === "Fresh") {
190
+ if (left.level !== right.level || left.shape !== right.shape) return false;
191
+ if (left.domain && right.domain) {
192
+ return isSame(leftScope, rightScope, left.domain, right.domain);
193
+ }
194
+ return !left.domain && !right.domain;
195
+ }
196
+ if (left.kind === "Flex" && right.kind === "Flex") return true;
197
+ if (left.kind === "Defn" && right.kind === "Defn") {
198
+ return (
199
+ isSameDefinition(leftScope, rightScope, left.definition, right.definition) &&
200
+ left.isLocal === right.isLocal
201
+ );
202
+ }
203
+ if (left.kind === "Fact" && right.kind === "Fact") {
204
+ return isSame(leftScope, rightScope, left.expression, right.expression);
205
+ }
206
+ return false;
207
+ };
208
+
209
+ const isSameContext = (
210
+ leftScope: Scope | undefined,
211
+ rightScope: Scope | undefined,
212
+ left: Hyp[],
213
+ right: Hyp[],
214
+ ): boolean => {
215
+ if (left.length === 0 || right.length === 0) return left.length === right.length;
216
+ return (
217
+ isSameHyp(leftScope, rightScope, left[0], right[0]) &&
218
+ isSameContext(
219
+ adjoin(leftScope, left[0]),
220
+ adjoin(rightScope, right[0]),
221
+ left.slice(1),
222
+ right.slice(1),
223
+ )
224
+ );
225
+ };
226
+
227
+ const isSameBinder = (
228
+ leftScope: Scope | undefined,
229
+ rightScope: Scope | undefined,
230
+ left: { name: string; domain: Expr | undefined; body: Expr },
231
+ right: { name: string; domain: Expr | undefined; body: Expr },
232
+ ) => {
233
+ if (!left.domain || !right.domain) {
234
+ return (
235
+ !left.domain &&
236
+ !right.domain &&
237
+ isSame(
238
+ adjoin(leftScope, getFresh(left.name, "Constant")),
239
+ adjoin(rightScope, getFresh(right.name, "Constant")),
240
+ left.body,
241
+ right.body,
242
+ )
243
+ );
244
+ }
245
+ return (
246
+ isSame(leftScope, rightScope, left.domain, right.domain) &&
247
+ isSame(
248
+ adjoin(leftScope, getBoundedFresh(left.name, left.domain)),
249
+ adjoin(rightScope, getBoundedFresh(right.name, right.domain)),
250
+ left.body,
251
+ right.body,
252
+ )
253
+ );
254
+ };
255
+
256
+ const isSame = (
257
+ leftScope: Scope | undefined,
258
+ rightScope: Scope | undefined,
259
+ left: Expr,
260
+ right: Expr,
261
+ ): boolean => {
262
+ if (left.kind === "Parens") return isSame(leftScope, rightScope, left.expression, right);
263
+ if (right.kind === "Parens") return isSame(leftScope, rightScope, left, right.expression);
264
+ const recurse = (leftInner: Expr, rightInner: Expr) =>
265
+ isSame(leftScope, rightScope, leftInner, rightInner);
266
+ const recurseList = (leftInner: Expr[], rightInner: Expr[]) =>
267
+ isPairwise(leftInner, rightInner, recurse);
268
+ switch (left.kind) {
269
+ case "Ix":
270
+ return right.kind === "Ix" && isSameIndex(leftScope, rightScope, left.index, right.index);
271
+ case "Opaque":
272
+ return right.kind === "Opaque" && left.name === right.name;
273
+ case "Internal":
274
+ return right.kind === "Internal" && left.builtin === right.builtin;
275
+ case "Lambda":
276
+ return (
277
+ right.kind === "Lambda" &&
278
+ left.params.length === right.params.length &&
279
+ isSame(
280
+ bindParams(leftScope, left.params),
281
+ bindParams(rightScope, right.params),
282
+ left.body,
283
+ right.body,
284
+ )
285
+ );
286
+ case "Apply":
287
+ return (
288
+ right.kind === "Apply" &&
289
+ recurse(left.operator, right.operator) &&
290
+ recurseList(left.args, right.args)
291
+ );
292
+ case "Sequent":
293
+ return (
294
+ right.kind === "Sequent" &&
295
+ isSameContext(leftScope, rightScope, left.sequent.context, right.sequent.context) &&
296
+ isSame(
297
+ adjoinAll(leftScope, left.sequent.context),
298
+ adjoinAll(rightScope, right.sequent.context),
299
+ left.sequent.active,
300
+ right.sequent.active,
301
+ )
302
+ );
303
+ case "Bang": {
304
+ if (right.kind !== "Bang" || !recurse(left.expression, right.expression)) return false;
305
+ const rightSelectors = right.selectors;
306
+ const isPrefix = left.selectors.every((selector, index) => {
307
+ const other = rightSelectors[index];
308
+ if (!other) return true;
309
+ if (selector.kind === "Number" && other.kind === "Number") {
310
+ return selector.value === other.value;
311
+ }
312
+ if (selector.kind === "Instance" && other.kind === "Instance") {
313
+ return recurseList(selector.args, other.args);
314
+ }
315
+ if (selector.kind === "Label" && other.kind === "Label") {
316
+ return selector.label === other.label && recurseList(selector.args, other.args);
317
+ }
318
+ return selector.kind === other.kind;
319
+ });
320
+ // List.for_all2 raises when the lists differ in length after a matching prefix.
321
+ if (isPrefix && left.selectors.length !== rightSelectors.length) {
322
+ throw new TlapmBugError("Invalid_argument");
323
+ }
324
+ return isPrefix;
325
+ }
326
+ case "With":
327
+ return right.kind === "With" && recurse(left.expression, right.expression);
328
+ case "Let":
329
+ return (
330
+ right.kind === "Let" &&
331
+ isPairwise(left.definitions, right.definitions, (leftDefinition, rightDefinition) =>
332
+ isSameDefinition(leftScope, rightScope, leftDefinition, rightDefinition),
333
+ ) &&
334
+ isSame(
335
+ bindDefinitions(leftScope, left.definitions),
336
+ bindDefinitions(rightScope, right.definitions),
337
+ left.body,
338
+ right.body,
339
+ )
340
+ );
341
+ case "If":
342
+ return (
343
+ right.kind === "If" &&
344
+ recurse(left.condition, right.condition) &&
345
+ recurse(left.consequent, right.consequent) &&
346
+ recurse(left.alternative, right.alternative)
347
+ );
348
+ case "List":
349
+ return (
350
+ right.kind === "List" &&
351
+ left.bullet === right.bullet &&
352
+ recurseList(left.items, right.items)
353
+ );
354
+ // tlapm binds the goal's bound names as the fact's.
355
+ case "Quant":
356
+ return (
357
+ right.kind === "Quant" &&
358
+ left.quantifier === right.quantifier &&
359
+ isSameBounds(leftScope, rightScope, left.bounds, right.bounds) &&
360
+ isSame(
361
+ bindBounds(leftScope, left.bounds),
362
+ bindBounds(rightScope, left.bounds),
363
+ left.body,
364
+ right.body,
365
+ )
366
+ );
367
+ case "Tquant":
368
+ return (
369
+ right.kind === "Tquant" &&
370
+ left.quantifier === right.quantifier &&
371
+ isPairwise(left.names, right.names, (leftName, rightName) => leftName === rightName) &&
372
+ isSame(
373
+ bindFlexible(leftScope, left.names),
374
+ bindFlexible(rightScope, right.names),
375
+ left.body,
376
+ right.body,
377
+ )
378
+ );
379
+ case "Choose":
380
+ case "SetSt":
381
+ return right.kind === left.kind && isSameBinder(leftScope, rightScope, left, right);
382
+ case "SetOf":
383
+ return (
384
+ right.kind === "SetOf" &&
385
+ isSame(
386
+ bindBounds(leftScope, left.bounds),
387
+ bindBounds(rightScope, right.bounds),
388
+ left.element,
389
+ right.element,
390
+ ) &&
391
+ isSameBounds(leftScope, rightScope, left.bounds, right.bounds)
392
+ );
393
+ case "SetEnum":
394
+ return right.kind === "SetEnum" && recurseList(left.elements, right.elements);
395
+ case "Arrow":
396
+ return (
397
+ right.kind === "Arrow" &&
398
+ recurse(left.domain, right.domain) &&
399
+ recurse(left.range, right.range)
400
+ );
401
+ case "Fcn":
402
+ return (
403
+ right.kind === "Fcn" &&
404
+ isSameBounds(leftScope, rightScope, left.bounds, right.bounds) &&
405
+ isSame(
406
+ bindBounds(leftScope, left.bounds),
407
+ bindBounds(rightScope, right.bounds),
408
+ left.body,
409
+ right.body,
410
+ )
411
+ );
412
+ case "FcnApp":
413
+ return (
414
+ right.kind === "FcnApp" &&
415
+ recurseList([left.function, ...left.args], [right.function, ...right.args])
416
+ );
417
+ case "Tuple":
418
+ return right.kind === "Tuple" && recurseList(left.elements, right.elements);
419
+ case "Product":
420
+ return right.kind === "Product" && recurseList(left.factors, right.factors);
421
+ case "Rect":
422
+ case "Record":
423
+ return (
424
+ right.kind === left.kind &&
425
+ isSameFields(
426
+ leftScope,
427
+ rightScope,
428
+ getSortedFields(left.fields),
429
+ getSortedFields(right.fields),
430
+ )
431
+ );
432
+ case "Except":
433
+ return (
434
+ right.kind === "Except" &&
435
+ recurse(left.function, right.function) &&
436
+ isPairwise(
437
+ left.specs,
438
+ right.specs,
439
+ (leftSpec, rightSpec) =>
440
+ isPairwise(leftSpec.trail, rightSpec.trail, (leftPoint, rightPoint) => {
441
+ if (leftPoint.kind === "Dot") {
442
+ return rightPoint.kind === "Dot" && leftPoint.field === rightPoint.field;
443
+ }
444
+ return (
445
+ rightPoint.kind === "Apply" &&
446
+ recurse(leftPoint.expression, rightPoint.expression)
447
+ );
448
+ }) && recurse(leftSpec.value, rightSpec.value),
449
+ )
450
+ );
451
+ case "Dot":
452
+ return (
453
+ right.kind === "Dot" &&
454
+ recurse(left.expression, right.expression) &&
455
+ left.field === right.field
456
+ );
457
+ case "Sub":
458
+ case "Tsub":
459
+ return (
460
+ right.kind === left.kind &&
461
+ left.modal === right.modal &&
462
+ recurse(left.action, right.action) &&
463
+ recurse(left.subscript, right.subscript)
464
+ );
465
+ case "Fair":
466
+ return (
467
+ right.kind === "Fair" &&
468
+ left.fairness === right.fairness &&
469
+ recurse(left.subscript, right.subscript) &&
470
+ recurse(left.action, right.action)
471
+ );
472
+ // Like E_eq, tlapm compares only the first arms.
473
+ case "Case": {
474
+ if (right.kind !== "Case") return false;
475
+ const leftArm = left.arms[0];
476
+ const rightArm = right.arms[0];
477
+ const isSameArm =
478
+ leftArm && rightArm
479
+ ? recurse(leftArm.condition, rightArm.condition) && recurse(leftArm.body, rightArm.body)
480
+ : !leftArm && !rightArm;
481
+ if (!isSameArm) return false;
482
+ if (left.other && right.other) return recurse(left.other, right.other);
483
+ return !left.other && !right.other;
484
+ }
485
+ case "String":
486
+ return right.kind === "String" && left.value === right.value;
487
+ case "Num":
488
+ return (
489
+ right.kind === "Num" && left.integer === right.integer && left.fraction === right.fraction
490
+ );
491
+ case "At":
492
+ return right.kind === "At" && left.isExcept === right.isExcept;
493
+ }
494
+ };
495
+
496
+ const isMatch = isSame(factScope, goalScope, fact, goal);
497
+ const factHyps = getScopeHyps(factScope);
498
+ const isDeclared = [...renaming].map(
499
+ (entry) =>
500
+ entry[0] === entry[1] ||
501
+ factHyps.reduce((isFound, hyp) => {
502
+ if (hyp.kind === "Fact") return isFound;
503
+ if (hyp.kind === "Defn" && hyp.definition.kind !== "Operator") throw new TlapmBugError();
504
+ return isFound || (hyp.kind === "Defn" ? hyp.definition.name : hyp.name) === entry[0];
505
+ }, false),
506
+ );
507
+ return isMatch && isDeclared.every(Boolean);
508
+ };
509
+
510
+ // check_level_change: the first visible fact that matches proves the goal, which becomes TRUE.
511
+ // When none does, tlapm records the obligation as not tried.
512
+ export const checkLevelChange = (sequent: Sequent): Sequent => {
513
+ const goalScope = adjoinAll(undefined, sequent.context);
514
+ const isProved = sequent.context.some((hyp, index) => {
515
+ if (hyp.kind !== "Fact" || hyp.visibility !== "Visible") return false;
516
+ const factScope = getScopeAt(goalScope, sequent.context.length - index);
517
+ const fact = hyp.expression;
518
+ return fact.kind === "Sequent"
519
+ ? compare(
520
+ adjoinAll(factScope, fact.sequent.context),
521
+ goalScope,
522
+ fact.sequent.active,
523
+ sequent.active,
524
+ )
525
+ : compare(factScope, goalScope, fact, sequent.active);
526
+ });
527
+ if (!isProved) throw new TlaProverError("LevelComparison proof directive did not succeed.\n");
528
+ return { ...sequent, active: { kind: "Internal", builtin: "TRUE" } };
529
+ };
@@ -0,0 +1,67 @@
1
+ // MD5 (RFC 1321), as OCaml's Digest computes it. tlapm names the operators it coalesces by
2
+ // the digest of their printed form, so matching its SMT-LIB output needs the same digest.
3
+ const SHIFTS = [7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21];
4
+
5
+ const CONSTANTS = Array.from({ length: 64 }, (_value, index) =>
6
+ Math.floor(Math.abs(Math.sin(index + 1)) * 2 ** 32),
7
+ );
8
+
9
+ const rotate = (value: number, count: number) => (value << count) | (value >>> (32 - count));
10
+
11
+ export const getMd5 = (text: string): string => {
12
+ const message = new TextEncoder().encode(text);
13
+ const length = Math.ceil((message.length + 9) / 64) * 64;
14
+ const bytes = new Uint8Array(length);
15
+ bytes.set(message);
16
+ bytes[message.length] = 0x80;
17
+ const view = new DataView(bytes.buffer);
18
+ view.setUint32(length - 8, (message.length * 8) >>> 0, true);
19
+ view.setUint32(length - 4, Math.floor(message.length / 2 ** 29), true);
20
+ const state = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476];
21
+ for (let offset = 0; offset < length; offset += 64) {
22
+ const words = Array.from({ length: 16 }, (_value, index) =>
23
+ view.getUint32(offset + index * 4, true),
24
+ );
25
+ const [a, b, c, d] = Array.from({ length: 64 }).reduce<number[]>(
26
+ ([innerA, innerB, innerC, innerD], _value, index) => {
27
+ const round = Math.floor(index / 16);
28
+ const mixed =
29
+ round === 0
30
+ ? (innerB & innerC) | (~innerB & innerD)
31
+ : round === 1
32
+ ? (innerD & innerB) | (~innerD & innerC)
33
+ : round === 2
34
+ ? innerB ^ innerC ^ innerD
35
+ : innerC ^ (innerB | ~innerD);
36
+ const word =
37
+ round === 0
38
+ ? index
39
+ : round === 1
40
+ ? (5 * index + 1) % 16
41
+ : round === 2
42
+ ? (3 * index + 5) % 16
43
+ : (7 * index) % 16;
44
+ const sum = (innerA + mixed + CONSTANTS[index] + words[word]) | 0;
45
+ return [
46
+ innerD,
47
+ (innerB + rotate(sum, SHIFTS[round * 4 + (index % 4)])) | 0,
48
+ innerB,
49
+ innerC,
50
+ ];
51
+ },
52
+ state,
53
+ );
54
+ state.splice(
55
+ 0,
56
+ 4,
57
+ (state[0] + a) | 0,
58
+ (state[1] + b) | 0,
59
+ (state[2] + c) | 0,
60
+ (state[3] + d) | 0,
61
+ );
62
+ }
63
+ return state
64
+ .flatMap((word) => [word, word >>> 8, word >>> 16, word >>> 24])
65
+ .map((byte) => (byte & 0xff).toString(16).padStart(2, "0"))
66
+ .join("");
67
+ };