tla-ts 0.0.4-dev.a0009b7 → 0.0.4-dev.b254db0

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