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,236 @@
1
+ // tlapm's fingerprints file, ported from src/backend/fpfile.ml.
2
+
3
+ import {
4
+ cpSync,
5
+ existsSync,
6
+ mkdirSync,
7
+ readdirSync,
8
+ readFileSync,
9
+ renameSync,
10
+ rmSync,
11
+ statSync,
12
+ writeFileSync,
13
+ } from "node:fs";
14
+ import { basename, join } from "node:path";
15
+ import { version } from "../package.json";
16
+ import { TlaProofError } from "./errors";
17
+ import { block, isBlock, readMarshaled, writeMarshaled, type OcamlValue } from "./ocaml-marshal";
18
+ import type { ProofAttempt } from "./prove/prove";
19
+
20
+ // One optional result per prover, in V13.str's order.
21
+ export type FingerprintTable = Map<string, OcamlValue[]>;
22
+
23
+ const MAGIC_NUMBER = 20210223;
24
+
25
+ const RECORD_SIZE = 23;
26
+
27
+ // Each prover's field in V13.str and constructor in V13.meth.
28
+ const PROVERS: Record<string, { field: number; tag: number }> = {
29
+ zenon: { field: 1, tag: 1 },
30
+ isabelle: { field: 2, tag: 0 },
31
+ smt: { field: 10, tag: 8 },
32
+ z3: { field: 11, tag: 9 },
33
+ ls4: { field: 17, tag: 16 },
34
+ };
35
+
36
+ const FAIL_METHOD = 5;
37
+
38
+ const REASONS = ["false", "timeout"];
39
+
40
+ // Stored as the tlapm, Zenon and Isabelle versions, which --safefp compares.
41
+ const VERSION = `tla-ts ${version}`;
42
+
43
+ const CHECKED_VERSIONS = new Map([
44
+ [PROVERS.zenon.field, 3],
45
+ [PROVERS.isabelle.field, 4],
46
+ ]);
47
+
48
+ const HISTORY_SIZE = 10;
49
+
50
+ const getField = (value: OcamlValue | undefined, index: number) =>
51
+ value !== undefined && isBlock(value) ? value.fields[index] : undefined;
52
+
53
+ const getFloat = (value: OcamlValue | undefined) =>
54
+ value !== undefined && typeof value === "object" && "float" in value ? value.float : Infinity;
55
+
56
+ // Hashtbl.hash on a string.
57
+ const hashKey = (key: string) => {
58
+ const bytes = new TextEncoder().encode(key);
59
+ const rotate = (value: number, shift: number) => (value << shift) | (value >>> (32 - shift));
60
+ const mixed = Array.from(
61
+ { length: Math.ceil(bytes.length / 4) },
62
+ (_, index) =>
63
+ bytes[index * 4] |
64
+ (bytes[index * 4 + 1] << 8) |
65
+ (bytes[index * 4 + 2] << 16) |
66
+ (bytes[index * 4 + 3] << 24),
67
+ ).reduce((hash, word) => {
68
+ const data = Math.imul(rotate(Math.imul(word, 0xcc9e2d51), 15), 0x1b873593);
69
+ return (Math.imul(rotate(hash ^ data, 13), 5) + 0xe6546b64) | 0;
70
+ }, 0);
71
+ const first = Math.imul(mixed ^ bytes.length ^ ((mixed ^ bytes.length) >>> 16), 0x85ebca6b);
72
+ const second = Math.imul(first ^ (first >>> 13), 0xc2b2ae35);
73
+ return (second ^ (second >>> 16)) & 0x3fffffff;
74
+ };
75
+
76
+ const readBucket = (entry: OcamlValue | undefined): [OcamlValue, OcamlValue][] =>
77
+ entry !== undefined && isBlock(entry)
78
+ ? [[entry.fields[0], entry.fields[1]], ...readBucket(entry.fields[2])]
79
+ : [];
80
+
81
+ // Fpfile.load_fingerprints. Each bucket lists the latest fingerprint first.
82
+ export const loadFingerprints = (table: FingerprintTable, path: string, isSafe: boolean) => {
83
+ const bytes = readFileSync(path);
84
+ const magic = readMarshaled(bytes, 0);
85
+ const fp = readMarshaled(bytes, magic.next).value;
86
+ if (magic.value !== MAGIC_NUMBER || !isBlock(fp) || fp.tag !== 0)
87
+ throw new TlaProofError("corrupted fingerprint file");
88
+ const buckets = getField(getField(fp, 0), 1);
89
+ const isTrusted = (sti: OcamlValue, field: number) => {
90
+ const index = CHECKED_VERSIONS.get(field);
91
+ return !isSafe || index === undefined || getField(getField(sti, 0), index) === VERSION;
92
+ };
93
+ table.clear();
94
+ (buckets !== undefined && isBlock(buckets) ? buckets.fields : [])
95
+ .flatMap((bucket) => readBucket(bucket).toReversed())
96
+ .forEach(([key, record]) => {
97
+ if (typeof key !== "string" || !isBlock(record))
98
+ throw new TlaProofError("corrupted fingerprint file");
99
+ table.set(
100
+ key,
101
+ record.fields.map((sti, field) => (isTrusted(sti, field) ? sti : 0)),
102
+ );
103
+ });
104
+ };
105
+
106
+ const decodeResult = (prover: string, sti: OcamlValue | undefined): ProofAttempt | undefined => {
107
+ const status = getField(sti, 0);
108
+ const outcome = getField(status, 0);
109
+ const argument = getField(getField(status, 1), 0);
110
+ if (outcome === undefined || outcome === 1) return undefined;
111
+ const seconds = getFloat(getField(prover === "isabelle" ? getField(argument, 0) : argument, 0));
112
+ const tactic = prover === "isabelle" ? getField(argument, 1) : undefined;
113
+ const reason = getField(getField(outcome, 0), 0);
114
+ return {
115
+ method: {
116
+ prover,
117
+ timeLimitMs: seconds * 1000,
118
+ ...(typeof tactic === "string" && { tactic }),
119
+ },
120
+ isProved: outcome === 0,
121
+ isReused: false,
122
+ ...(reason !== undefined && {
123
+ reason: typeof reason === "number" ? REASONS[reason] : String(getField(reason, 0)),
124
+ }),
125
+ };
126
+ };
127
+
128
+ // Fpfile.fp_writes, whose date counts months from 0.
129
+ const encodeResult = (attempt: ProofAttempt) => {
130
+ const now = new Date();
131
+ const method = attempt.method;
132
+ const timeout = { float: method.rlimit ?? method.timeLimitMs / 1000 };
133
+ const argument =
134
+ method.prover === "zenon"
135
+ ? block(0, timeout, FAIL_METHOD)
136
+ : method.prover === "isabelle"
137
+ ? block(0, block(0, timeout), method.tactic ?? "auto")
138
+ : block(0, timeout);
139
+ const reasonIndex = REASONS.indexOf(attempt.reason ?? "");
140
+ const reason =
141
+ attempt.reason === undefined
142
+ ? 0
143
+ : block(0, reasonIndex === -1 ? block(0, attempt.reason) : reasonIndex);
144
+ return block(
145
+ 0,
146
+ block(
147
+ 0,
148
+ block(
149
+ 0,
150
+ attempt.isProved ? 0 : block(0, reason),
151
+ block(PROVERS[method.prover].tag, argument),
152
+ ),
153
+ block(
154
+ 0,
155
+ now.getFullYear(),
156
+ now.getMonth(),
157
+ now.getDate(),
158
+ now.getHours(),
159
+ now.getMinutes(),
160
+ now.getSeconds(),
161
+ ),
162
+ VERSION,
163
+ VERSION,
164
+ VERSION,
165
+ ),
166
+ );
167
+ };
168
+
169
+ const encodeTable = (table: FingerprintTable) => {
170
+ const length = 2 ** Math.max(9, Math.ceil(Math.log2(table.size / 2)));
171
+ const buckets: OcamlValue[] = Array.from({ length }, () => 0);
172
+ table.forEach((record, fingerprint) => {
173
+ const index = hashKey(fingerprint) & (length - 1);
174
+ buckets[index] = block(0, fingerprint, block(0, ...record), buckets[index]);
175
+ });
176
+ return block(0, block(0, table.size, block(0, ...buckets), 0, 512));
177
+ };
178
+
179
+ const pad = (value: number) => String(value).padStart(2, "0");
180
+
181
+ // Fpfile.fp_init.
182
+ export const saveHistory = (path: string, sources: string[]) => {
183
+ const now = new Date();
184
+ const history = `${path}.history`;
185
+ const directory = join(
186
+ history,
187
+ `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}_${pad(now.getHours())}_${pad(now.getMinutes())}_${pad(now.getSeconds())}`,
188
+ );
189
+ mkdirSync(directory, { recursive: true });
190
+ [path, ...sources]
191
+ .filter((file) => existsSync(file))
192
+ .forEach((file) => cpSync(file, join(directory, basename(file)), { preserveTimestamps: true }));
193
+ readdirSync(history)
194
+ .map((name) => join(history, name))
195
+ .sort((left, right) => statSync(right).mtimeMs - statSync(left).mtimeMs)
196
+ .slice(HISTORY_SIZE)
197
+ .forEach((old) => rmSync(old, { recursive: true, force: true }));
198
+ };
199
+
200
+ // Fpfile.query: what each prover concluded for each fingerprint. Saving them, as fp_writes,
201
+ // add_to_table and fp_close_and_consolidate do, keeps each record and result loaded unchanged, with
202
+ // its date, versions and other provers' results.
203
+ export const getKnownResults = (table: FingerprintTable) => {
204
+ const storedResults = new Map<ProofAttempt, OcamlValue>();
205
+ const storedRecords = new Map<Map<string, ProofAttempt>, OcamlValue[]>();
206
+ const results = new Map(
207
+ [...table].map(([fingerprint, record]) => {
208
+ const attempts = new Map(
209
+ Object.entries(PROVERS).flatMap(([prover, known]) => {
210
+ const result = decodeResult(prover, getField(record[known.field], 0));
211
+ if (!result) return [];
212
+ storedResults.set(result, record[known.field]);
213
+ return [[prover, result]];
214
+ }),
215
+ );
216
+ storedRecords.set(attempts, record);
217
+ return [fingerprint, attempts];
218
+ }),
219
+ );
220
+ const save = (path: string) => {
221
+ table.clear();
222
+ results.forEach((attempts, fingerprint) => {
223
+ const record = [...(storedRecords.get(attempts) ?? Array<OcamlValue>(RECORD_SIZE).fill(0))];
224
+ attempts.forEach((attempt, prover) => {
225
+ record[PROVERS[prover].field] = storedResults.get(attempt) ?? encodeResult(attempt);
226
+ });
227
+ table.set(fingerprint, record);
228
+ });
229
+ writeFileSync(
230
+ `${path}.tmp`,
231
+ Buffer.concat([writeMarshaled(MAGIC_NUMBER), writeMarshaled(encodeTable(table))]),
232
+ );
233
+ renameSync(`${path}.tmp`, path);
234
+ };
235
+ return { results, save };
236
+ };
package/src/tlc-random.ts CHANGED
@@ -182,24 +182,14 @@ const createShiftTable = (length: number) => {
182
182
 
183
183
  // Extending a fingerprint by a byte is linear over GF(2), so extending it by a value's bytes
184
184
  // XORs the value's own fingerprint from zero into the fingerprint shifted by their length.
185
- // The fingerprinter caches each distinct variable value for one pass over the stored states. It
186
- // reads a state's variables in `order` and writes each fingerprint's low and high words into
187
- // `words`, an Int32Array over a little-endian BigInt64Array.
185
+ // The fingerprinter caches each distinct function or set it meets, nested ones included, for one
186
+ // pass over the stored states, as states share most of their values. It reads a state's variables
187
+ // in `order` and writes each fingerprint's low and high words into `words`, an Int32Array over a
188
+ // little-endian BigInt64Array.
188
189
  export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
189
190
  const shiftTables = new Map<number, Int32Array>();
190
191
  const indices = new Map<Value, number>();
191
192
  const contributions: number[] = [];
192
- const getContribution = (value: Value) => {
193
- const cached = indices.get(value);
194
- if (cached !== undefined) return cached;
195
- const length = getFingerprintLength(value);
196
- high = 0;
197
- low = 0;
198
- extendValue(value);
199
- indices.set(value, contributions.length);
200
- contributions.push(length, high, low);
201
- return contributions.length - 3;
202
- };
203
193
  const getShiftTable = (length: number) => {
204
194
  const cached = shiftTables.get(length);
205
195
  if (cached) return cached;
@@ -207,29 +197,62 @@ export const createTlcFingerprinter = (order: number[], words: Int32Array) => {
207
197
  shiftTables.set(length, table);
208
198
  return table;
209
199
  };
210
- return (state: Value[], position: number) => {
200
+ // Extends the fingerprint by a value's bytes and returns how many there are.
201
+ const extendElement = (value: Value): number => {
202
+ if (typeof value !== "object" || isModelValue(value)) {
203
+ extendValue(value);
204
+ return getFingerprintLength(value);
205
+ }
206
+ const contribution = getContribution(value);
207
+ const shifted = { high, low };
208
+ const table = getShiftTable(contributions[contribution]);
209
+ high = contributions[contribution + 1];
210
+ low = contributions[contribution + 2];
211
+ for (let position = 0; position < 8; position++) {
212
+ const word = position < 4 ? shifted.low : shifted.high;
213
+ const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
214
+ high ^= table[entry];
215
+ low ^= table[entry + 1];
216
+ }
217
+ return contributions[contribution];
218
+ };
219
+ const getContribution = (value: Value) => {
220
+ const cached = indices.get(value);
221
+ if (cached !== undefined) return cached;
211
222
  const outer = { high, low };
212
- const fingerprint = { high: INITIAL_HIGH, low: INITIAL_LOW };
213
- for (const index of order) {
214
- const contribution = getContribution(state[index]);
215
- const table = getShiftTable(contributions[contribution]);
216
- const extended = {
217
- high: contributions[contribution + 1],
218
- low: contributions[contribution + 2],
219
- };
220
- for (let position = 0; position < 8; position++) {
221
- const word = position < 4 ? fingerprint.low : fingerprint.high;
222
- const entry = (position * 256 + ((word >>> ((position & 3) * 8)) & 0xff)) * 2;
223
- extended.high ^= table[entry];
224
- extended.low ^= table[entry + 1];
223
+ high = 0;
224
+ low = 0;
225
+ const length = { value: 5 };
226
+ if (isFunctionValue(value)) {
227
+ const functionValue = toFiniteFunction(value);
228
+ extendByte(FCN_RCD_VALUE);
229
+ extendInt(functionValue.domain.length);
230
+ for (let index = 0; index < functionValue.domain.length; index++) {
231
+ length.value += extendElement(functionValue.domain[index]);
232
+ length.value += extendElement(functionValue.range[index]);
225
233
  }
226
- fingerprint.high = extended.high;
227
- fingerprint.low = extended.low;
234
+ } else {
235
+ const elements = toFiniteSet(value).elements;
236
+ extendByte(SET_ENUM_VALUE);
237
+ extendInt(elements.length);
238
+ for (const element of elements) length.value += extendElement(element);
228
239
  }
240
+ const contribution = contributions.length;
241
+ indices.set(value, contribution);
242
+ contributions.push(length.value, high, low);
243
+ high = outer.high;
244
+ low = outer.low;
245
+ return contribution;
246
+ };
247
+ return (state: Value[], position: number) => {
248
+ const outer = { high, low };
249
+ high = INITIAL_HIGH;
250
+ low = INITIAL_LOW;
251
+ for (const index of order) extendElement(state[index]);
252
+ words[2 * position] = low;
253
+ words[2 * position + 1] = high;
229
254
  high = outer.high;
230
255
  low = outer.low;
231
- words[2 * position] = fingerprint.low;
232
- words[2 * position + 1] = fingerprint.high;
233
256
  };
234
257
  };
235
258
 
@@ -1,5 +1,4 @@
1
1
  import { existsSync, readFileSync } from "node:fs";
2
- import { strFromU8, unzipSync } from "fflate";
3
2
 
4
3
  // The TLC whose output tla-ts reproduces: the 1.7.4 release, or the master builds that tla-ts
5
4
  // follows for any other tla2tools.jar.
@@ -7,12 +6,14 @@ export type TlcVersion = "1.7.4" | "master";
7
6
 
8
7
  const MANIFEST = "META-INF/MANIFEST.MF";
9
8
 
10
- // The version of a tla2tools.jar, from the Git tag that its manifest records.
11
- export const getTlcVersion = (jar: string | undefined): TlcVersion => {
9
+ // The version of a tla2tools.jar, from the Git tag that its manifest records. fflate loads
10
+ // node:worker_threads, so only a run with a jar imports it.
11
+ export const getTlcVersion = async (jar: string | undefined): Promise<TlcVersion> => {
12
12
  if (jar === undefined || !existsSync(jar)) return "master";
13
- const manifest = unzipSync(readFileSync(jar), { filter: (file) => file.name === MANIFEST })[
14
- MANIFEST
15
- ];
16
- const tag = manifest && /^X-Git-Tag: *(\S*)/m.exec(strFromU8(manifest))?.[1];
13
+ const fflate = await import("fflate");
14
+ const manifest = fflate.unzipSync(readFileSync(jar), {
15
+ filter: (file) => file.name === MANIFEST,
16
+ })[MANIFEST];
17
+ const tag = manifest && /^X-Git-Tag: *(\S*)/m.exec(fflate.strFromU8(manifest))?.[1];
17
18
  return tag === "v1.7.4" ? "1.7.4" : "master";
18
19
  };
package/src/values.ts CHANGED
@@ -14,6 +14,7 @@ export interface SetValue {
14
14
  elements: Value[];
15
15
  hash: number;
16
16
  fingerprint: number;
17
+ isFingerprinted: boolean;
17
18
  }
18
19
 
19
20
  // A set enumeration whose elements TLC can't sort, such as {1, "a"}. TLC sorts a set enumeration
@@ -31,6 +32,7 @@ export interface FunctionValue {
31
32
  fingerprint: number;
32
33
  writtenKeys: Value[] | undefined;
33
34
  functionClass: FunctionClass | undefined;
35
+ isFingerprinted: boolean;
34
36
  }
35
37
 
36
38
  export interface IntervalValue {
@@ -232,7 +234,13 @@ const compareFunctions = (left: FunctionValue, right: FunctionValue) => {
232
234
  const rightRange = right.range;
233
235
  if (leftRange === rightRange) return 0;
234
236
  for (let index = 0; index < leftRange.length; index++) {
235
- const valueComparison = compareValues(leftRange[index], rightRange[index]);
237
+ const leftValue = leftRange[index];
238
+ const rightValue = rightRange[index];
239
+ // V8 can't inline the recursive compareValues here, so two numbers skip the call.
240
+ const valueComparison =
241
+ typeof leftValue === "number" && typeof rightValue === "number"
242
+ ? leftValue - rightValue
243
+ : compareValues(leftValue, rightValue);
236
244
  if (valueComparison !== 0) return valueComparison;
237
245
  }
238
246
  return 0;
@@ -433,6 +441,7 @@ export const createSortedSet = (elements: Value[]): SetValue => ({
433
441
  elements,
434
442
  hash: 0,
435
443
  fingerprint: 0,
444
+ isFingerprinted: false,
436
445
  });
437
446
 
438
447
  export const createSortedFunction = (domain: Value[], range: Value[]): FunctionValue => ({
@@ -443,6 +452,7 @@ export const createSortedFunction = (domain: Value[], range: Value[]): FunctionV
443
452
  fingerprint: 0,
444
453
  writtenKeys: undefined,
445
454
  functionClass: undefined,
455
+ isFingerprinted: false,
446
456
  });
447
457
 
448
458
  // TLC words a failed function application by the Java class of the function: FcnLambdaValue for
@@ -512,14 +522,16 @@ const cacheSet = (value: Value) => {
512
522
  // FcnLambdaValue#fingerPrint caches the FcnRcdValue the function enumerates to, which applies it
513
523
  // from then on, so a function TLC has fingerprinted is no longer a lambda.
514
524
  export const markFingerprinted = (value: Value): void => {
515
- if (
516
- (!lambdaPresence.isPresent && !streamedSetPresence.isPresent) ||
517
- typeof value !== "object" ||
518
- fingerprintedValues.has(value)
519
- ) {
525
+ if ((!lambdaPresence.isPresent && !streamedSetPresence.isPresent) || typeof value !== "object") {
520
526
  return;
521
527
  }
522
- fingerprintedValues.add(value);
528
+ if (value.kind === "Function" || value.kind === "Set") {
529
+ if (value.isFingerprinted) return;
530
+ value.isFingerprinted = true;
531
+ } else {
532
+ if (fingerprintedValues.has(value)) return;
533
+ fingerprintedValues.add(value);
534
+ }
523
535
  if (value.kind === "Function") {
524
536
  if (value.functionClass === "lambda") value.functionClass = undefined;
525
537
  value.domain.forEach(markFingerprinted);
@@ -613,9 +625,14 @@ export const valuesEqual = (left: Value, right: Value): boolean => {
613
625
  // TLC prints and words errors about an equal function by its Java class, so a value can stand for
614
626
  // another in a state only if each function in it has the same class.
615
627
  const createSameValueCheck = (isClassSensitive: boolean) => {
616
- const areSameValues = (left: Value[], right: Value[]) =>
617
- left === right ||
618
- (left.length === right.length && left.every((element, index) => isSame(element, right[index])));
628
+ const areSameValues = (left: Value[], right: Value[]) => {
629
+ if (left === right) return true;
630
+ if (left.length !== right.length) return false;
631
+ for (let index = 0; index < left.length; index++) {
632
+ if (!isSame(left[index], right[index])) return false;
633
+ }
634
+ return true;
635
+ };
619
636
  const isSame = (left: Value, right: Value): boolean => {
620
637
  if (left === right) return true;
621
638
  if (typeof left !== "object" || typeof right !== "object") return false;
@@ -1019,10 +1036,7 @@ export const markNormalized = (value: Value): void => {
1019
1036
  };
1020
1037
 
1021
1038
  export const isEqualLikeTlc = (left: Value, right: Value) =>
1022
- computeLikeTlcOnFailure(
1023
- () => valuesEqual(left, right),
1024
- () => equalsLikeTlc(left, right),
1025
- );
1039
+ computeLikeTlcOnFailure(valuesEqual, equalsLikeTlc, left, right);
1026
1040
 
1027
1041
  const isStrictlySorted = (elements: Value[]) => {
1028
1042
  for (let index = 1; index < elements.length; index++) {
@@ -1046,9 +1060,11 @@ export const createSet = (elements: Value[]): SetValue => {
1046
1060
  // TLC builds a set enumeration without sorting it, so one whose elements it can't compare still
1047
1061
  // evaluates.
1048
1062
  export const createEnumeratedSet = (elements: Value[]): SetValue | MixedSetValue =>
1049
- computeLikeTlcOnFailure<SetValue | MixedSetValue>(
1050
- () => createSet(elements),
1051
- () => ({ kind: "MixedSet", elements }),
1063
+ computeLikeTlcOnFailure<Value[], undefined, SetValue | MixedSetValue>(
1064
+ createSet,
1065
+ (mixed) => ({ kind: "MixedSet", elements: mixed }),
1066
+ elements,
1067
+ undefined,
1052
1068
  );
1053
1069
 
1054
1070
  export const EMPTY_SET = createSortedSet([]);
@@ -1590,6 +1606,38 @@ const getCombinedSets = (value: ObjectValue) =>
1590
1606
  ? [value.range]
1591
1607
  : undefined;
1592
1608
 
1609
+ // Whether a value TLC builds alike at each evaluation and that enumerating, comparing or
1610
+ // fingerprinting it leaves alike, so that one evaluation can stand for the next. It holds no UNION or
1611
+ // \cup, which streams until TLC caches it, no lambda, which fingerprinting turns into a record, and
1612
+ // no record a comparison reorders.
1613
+ export const isSettledValue = (value: Value): boolean => {
1614
+ if (typeof value !== "object") return true;
1615
+ switch (value.kind) {
1616
+ case "ModelValue":
1617
+ case "Interval":
1618
+ return true;
1619
+ case "Set":
1620
+ return value.elements.every(isSettledValue);
1621
+ case "Function":
1622
+ return (
1623
+ value.functionClass !== "lambda" &&
1624
+ value.writtenKeys === undefined &&
1625
+ value.domain.every(isSettledValue) &&
1626
+ value.range.every(isSettledValue)
1627
+ );
1628
+ case "SetOperation":
1629
+ return (
1630
+ value.operator !== "\\cup" && isSettledValue(value.left) && isSettledValue(value.right)
1631
+ );
1632
+ case "FilteredSet":
1633
+ return isSettledValue(value.base);
1634
+ case "ProductSet":
1635
+ return value.factors.every(isSettledValue);
1636
+ default:
1637
+ return false;
1638
+ }
1639
+ };
1640
+
1593
1641
  // Whether a set streams its elements in another order than the normalized set, so that a set of
1594
1642
  // tuples, records, or functions it is a component of has to stream too.
1595
1643
  const isStreamedApart = (value: Value): boolean => {
@@ -1864,17 +1912,20 @@ export const setTrace: { sets: Value[] | undefined; callStack?: TracedCallStack
1864
1912
  };
1865
1913
 
1866
1914
  // Takes a fast path, and computes as TLC does where it fails. TLC never takes the fast path, so
1867
- // the failure's leftovers in the replay's trace go first.
1868
- export const computeLikeTlcOnFailure = <Result>(
1869
- compute: () => Result,
1870
- computeLikeTlc: (error: TlaEvaluationError) => Result,
1915
+ // the failure's leftovers in the replay's trace go first. Both paths take their operands as
1916
+ // arguments, so a hot caller passes functions it doesn't allocate.
1917
+ export const computeLikeTlcOnFailure = <Left, Right, Result>(
1918
+ compute: (left: Left, right: Right) => Result,
1919
+ computeLikeTlc: (left: Left, right: Right, error: TlaEvaluationError) => Result,
1920
+ left: Left,
1921
+ right: Right,
1871
1922
  ): Result => {
1872
1923
  const sets = setTrace.sets;
1873
1924
  const callStack = setTrace.callStack;
1874
1925
  const depth = sets?.length ?? 0;
1875
1926
  const trace = callStack && { ...callStack, length: callStack.expressions.length };
1876
1927
  try {
1877
- return compute();
1928
+ return compute(left, right);
1878
1929
  } catch (error) {
1879
1930
  if (!(error instanceof TlaEvaluationError)) throw error;
1880
1931
  if (sets) sets.length = depth;
@@ -1883,7 +1934,7 @@ export const computeLikeTlcOnFailure = <Result>(
1883
1934
  callStack.isFrozen = trace.isFrozen;
1884
1935
  callStack.fingerprint = trace.fingerprint;
1885
1936
  }
1886
- return computeLikeTlc(error);
1937
+ return computeLikeTlc(left, right, error);
1887
1938
  }
1888
1939
  };
1889
1940
 
@@ -1896,19 +1947,20 @@ export const isElementOf = (element: Value, set: Value): boolean => {
1896
1947
  return isElement;
1897
1948
  };
1898
1949
 
1950
+ const isInSortedSet = (element: Value, set: SetValue) => findIndex(set.elements, element) !== -1;
1951
+
1952
+ const isInSortedSetLikeTlc = (element: Value, set: SetValue) =>
1953
+ normalizedSets.has(set)
1954
+ ? searchLikeTlc(element, set.elements).isFound
1955
+ : isElementOfLikeTlc(element, set.elements);
1956
+
1899
1957
  const isElementOfSet = (element: Value, set: Value): boolean => {
1900
1958
  if (!isSetValue(set)) throw new TlaEvaluationError(describeNonSetMembership(element, set));
1901
1959
  switch (set.kind) {
1902
1960
  case "Set":
1903
1961
  // SetEnumValue.member compares the element with the set's.
1904
1962
  cacheSet(element);
1905
- return computeLikeTlcOnFailure(
1906
- () => findIndex(set.elements, element) !== -1,
1907
- () =>
1908
- normalizedSets.has(set)
1909
- ? searchLikeTlc(element, set.elements).isFound
1910
- : isElementOfLikeTlc(element, set.elements),
1911
- );
1963
+ return computeLikeTlcOnFailure(isInSortedSet, isInSortedSetLikeTlc, element, set);
1912
1964
  case "MixedSet":
1913
1965
  return isElementOfLikeTlc(element, set.elements);
1914
1966
  case "Interval":
@@ -2101,17 +2153,14 @@ const describeExceptKey = (functionValue: FunctionValue, argument: Value) => {
2101
2153
  : `Attempted to apply record to a non-string argument ${shown}.`;
2102
2154
  };
2103
2155
 
2104
- const findKeyIndex = (domain: Value[], argument: Value) => {
2156
+ // A key the domain holds itself, which TLC finds without a comparison that can fail.
2157
+ const findIdenticalKey = (domain: Value[], argument: Value) => {
2105
2158
  const first = domain[0];
2106
2159
  if (typeof argument === "number" && typeof first === "number") {
2107
2160
  const offset = argument - first;
2108
2161
  if (domain[offset] === argument) return offset;
2109
2162
  }
2110
- if (domain.length <= 8) {
2111
- for (let index = 0; index < domain.length; index++)
2112
- if (domain[index] === argument) return index;
2113
- }
2114
- return findIndex(domain, argument);
2163
+ return domain.length <= 8 ? domain.indexOf(argument) : -1;
2115
2164
  };
2116
2165
 
2117
2166
  export const createNonIntegerKeyError = (argument: Value) =>
@@ -2123,6 +2172,15 @@ export const createNonIntegerKeyError = (argument: Value) =>
2123
2172
  // binary search with compareTo.
2124
2173
  const LINEAR_SEARCH_LIMIT = 32;
2125
2174
 
2175
+ const failKeyLikeTlc = (domain: Value[], argument: Value, error: TlaEvaluationError): never => {
2176
+ if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
2177
+ domain.forEach((key) => equalsLikeTlc(key, argument));
2178
+ } else {
2179
+ findIndex(domain, argument, compareLikeTlc);
2180
+ }
2181
+ throw error;
2182
+ };
2183
+
2126
2184
  // TLC looks a key up by the Java class of the function: a tuple or a record rejects a key of the
2127
2185
  // wrong type, and another function rejects a non-integer for a domain it keeps as an interval, which
2128
2186
  // TLC 1.7.4 doesn't. A key of another type than the domain's keys otherwise fails TLC's comparison.
@@ -2143,17 +2201,9 @@ const findKeyIndexLikeTlc = (
2143
2201
  throw createNonIntegerKeyError(argument);
2144
2202
  }
2145
2203
  }
2146
- return computeLikeTlcOnFailure(
2147
- () => findKeyIndex(domain, argument),
2148
- (error) => {
2149
- if (valueVersion.is174 || domain.length < LINEAR_SEARCH_LIMIT) {
2150
- domain.forEach((key) => equalsLikeTlc(key, argument));
2151
- } else {
2152
- findIndex(domain, argument, compareLikeTlc);
2153
- }
2154
- throw error;
2155
- },
2156
- );
2204
+ const identical = findIdenticalKey(domain, argument);
2205
+ if (identical !== -1) return identical;
2206
+ return computeLikeTlcOnFailure(findIndex, failKeyLikeTlc, domain, argument);
2157
2207
  };
2158
2208
 
2159
2209
  export const applyFunction = (