tla-ts 0.0.4-dev.e4c2d0e → 0.0.4-dev.e92649a

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