tla-ts 0.0.4-dev.a297123 → 0.0.4-dev.c4929d7

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 +27 -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 +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 +5 -3
  27. package/src/errors.ts +86 -0
  28. package/src/evaluate.ts +54 -19
  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 +80 -35
  311. package/src/worker.ts +65 -0
  312. package/dist/parallel.cjs +0 -12
  313. package/dist/parallel.js +0 -12
@@ -0,0 +1,79 @@
1
+ // Ported from Isabelle2025's Pure/conv.ML, conversions: functions from a term to a theorem that
2
+ // it equals another. Copyright (c) 1986-2025 University of Cambridge, Technische Universitaet
3
+ // Muenchen, and contributors, see LICENSE.
4
+
5
+ import { TlaIsabelleError } from "../../../errors";
6
+ import { destEquals, destImplies, implies, isImplies } from "./logic";
7
+ import { declareName, makeNameContext, variantName, type NameContext } from "./name";
8
+ import { aconv, addFreeNames, mkFree, substBound, type Term } from "./term";
9
+ import {
10
+ abstractRule,
11
+ combination,
12
+ equalElim,
13
+ nprems,
14
+ permutePrems,
15
+ premOf,
16
+ reflexive,
17
+ type Thm,
18
+ } from "./thm";
19
+
20
+ export type Conversion = (term: Term) => Thm;
21
+
22
+ const isReflexive = (eq: Thm) => {
23
+ const [lhs, rhs] = destEquals(eq.prop);
24
+ return aconv(lhs, rhs);
25
+ };
26
+
27
+ // Drule.imp_cong_rule
28
+ export const impCongRule = (first: Thm, second: Thm) =>
29
+ combination(combination(reflexive(implies), first), second);
30
+
31
+ // Conv.prems_conv ~1: converts every premise.
32
+ export const premsConv =
33
+ (convert: Conversion): Conversion =>
34
+ (term) =>
35
+ isImplies(term)
36
+ ? impCongRule(convert(destImplies(term)[0]), premsConv(convert)(destImplies(term)[1]))
37
+ : reflexive(term);
38
+
39
+ // Conv.params_conv ~1: converts the body under every outer parameter, fixed as a fresh free
40
+ // variable.
41
+ export const paramsConv = (convert: Conversion): Conversion => {
42
+ const under =
43
+ (names: NameContext): Conversion =>
44
+ (term) => {
45
+ if (
46
+ term.kind !== "App" ||
47
+ term.fun.kind !== "Const" ||
48
+ term.fun.name !== "Pure.all" ||
49
+ term.arg.kind !== "Abs"
50
+ )
51
+ return convert(term);
52
+ const abs = term.arg;
53
+ const [name, nextNames] = variantName(abs.name, names);
54
+ const free = mkFree(name, abs.type);
55
+ const eq = under(declareName(name, nextNames))(substBound(free, abs.body));
56
+ return isReflexive(eq)
57
+ ? reflexive(term)
58
+ : combination(reflexive(term.fun), abstractRule(abs.name, free, eq));
59
+ };
60
+ return (term) => under(makeNameContext(addFreeNames(term, [])))(term);
61
+ };
62
+
63
+ // Conv.gconv_rule: converts goal index of a state.
64
+ export const gconvRule = (convert: Conversion, index: number, state: Thm): Thm => {
65
+ if (index < 1 || index > nprems(state)) throw new TlaIsabelleError("THM", `gconv_rule ${index}`);
66
+ const eq = convert(premOf(state, index));
67
+ if (isReflexive(eq)) return state;
68
+ const rotated = permutePrems(0, index - 1, state);
69
+ const rest = rotated.prop;
70
+ if (!isImplies(rest)) throw new TlaIsabelleError("THM", "gconv_rule");
71
+ const converted = equalElim(impCongRule(eq, reflexive(destImplies(rest)[1])), rotated);
72
+ return permutePrems(0, 1 - index, converted);
73
+ };
74
+
75
+ // Conv.fconv_rule: converts a theorem's proposition.
76
+ export const fconvRule = (convert: Conversion, thm: Thm): Thm => {
77
+ const eq = convert(thm.prop);
78
+ return isReflexive(eq) ? thm : equalElim(eq, thm);
79
+ };
@@ -0,0 +1,264 @@
1
+ // Ported from Isabelle2025's Pure/drule.ML, Pure/term_subst.ML and the basic rules of Pure.thy.
2
+ // Copyright (c) 1986-2025 University of Cambridge, Technische Universitaet Muenchen, and
3
+ // contributors, see LICENSE.
4
+
5
+ import { cleanName, emptyNameContext, variantList, variantName, type NameContext } from "./name";
6
+ import {
7
+ emptyTable,
8
+ fastIndexnameOrd,
9
+ tableEntries,
10
+ updateTable,
11
+ lookupTable,
12
+ type IndexKey,
13
+ } from "./table";
14
+ import { TlaIsabelleError } from "../../../errors";
15
+ import { allConst, isProtected, mkEquals, mkImplies, mkTerm, protect } from "./logic";
16
+ import { listOfSeq } from "./seq";
17
+ import { eqThm } from "./tactical";
18
+ import {
19
+ foldVars,
20
+ foldAtyps,
21
+ foldTypes,
22
+ funT,
23
+ mkAbs,
24
+ mkApp,
25
+ mkBound,
26
+ mkTVar,
27
+ mkVar,
28
+ propT,
29
+ stripAllVars,
30
+ type Term,
31
+ type Typ,
32
+ type Var,
33
+ } from "./term";
34
+ import { normType, type TyEnv } from "./type";
35
+ import {
36
+ axiomThm,
37
+ bicompose,
38
+ forallElim,
39
+ forallIntr,
40
+ incrIndexesThm,
41
+ instantiate,
42
+ instantiateTypes,
43
+ rs,
44
+ type Instantiation,
45
+ type Thm,
46
+ } from "./thm";
47
+
48
+ const propVar = (name: string) => mkVar(name, 0, propT);
49
+
50
+ // The basic rules of Pure.thy, with the variable names Isabelle gives them.
51
+ export const cutRl = axiomThm(
52
+ mkImplies(
53
+ mkImplies(propVar("psi"), propVar("theta")),
54
+ mkImplies(propVar("psi"), propVar("theta")),
55
+ ),
56
+ );
57
+ export const revcutRl = axiomThm(
58
+ mkImplies(propVar("V"), mkImplies(mkImplies(propVar("V"), propVar("W")), propVar("W"))),
59
+ );
60
+ export const thinRl = axiomThm(mkImplies(propVar("V"), mkImplies(propVar("W"), propVar("W"))));
61
+ export const asmRl = axiomThm(mkImplies(propVar("psi"), propVar("psi")));
62
+ export const remdupsRl = axiomThm(
63
+ mkImplies(propVar("phi"), mkImplies(propVar("phi"), mkImplies(propVar("psi"), propVar("psi")))),
64
+ );
65
+ export const protectI = axiomThm(mkImplies(propVar("A"), protect(propVar("A"))));
66
+ export const protectD = axiomThm(mkImplies(protect(propVar("A")), propVar("A")));
67
+ export const reflexiveThm = axiomThm(
68
+ mkEquals(mkVar("x", 0, mkTVar("'a", 0)), mkVar("x", 0, mkTVar("'a", 0))),
69
+ );
70
+
71
+ const typeA = mkTVar("'a", 0);
72
+
73
+ export const termI = axiomThm(mkTerm(mkVar("x", 0, typeA)));
74
+
75
+ export const impCong = axiomThm(
76
+ mkImplies(
77
+ mkImplies(propVar("A"), mkEquals(propVar("B"), propVar("C"))),
78
+ mkEquals(mkImplies(propVar("A"), propVar("B")), mkImplies(propVar("A"), propVar("C"))),
79
+ ),
80
+ );
81
+ export const swapPremsEq = axiomThm(
82
+ mkEquals(
83
+ mkImplies(propVar("A"), mkImplies(propVar("B"), propVar("C"))),
84
+ mkImplies(propVar("B"), mkImplies(propVar("A"), propVar("C"))),
85
+ ),
86
+ );
87
+ export const symmetricThm = axiomThm(
88
+ mkImplies(
89
+ mkEquals(mkVar("x", 0, typeA), mkVar("y", 0, typeA)),
90
+ mkEquals(mkVar("y", 0, typeA), mkVar("x", 0, typeA)),
91
+ ),
92
+ );
93
+ export const trivForallEquality = axiomThm(
94
+ mkEquals(mkApp(allConst(typeA), mkAbs("x", typeA, propVar("V"))), propVar("V")),
95
+ );
96
+
97
+ // Drule.norm_hhf_eq: (PROP ?phi ==> (!!x. PROP ?psi x)) == (!!x. PROP ?phi ==> PROP ?psi x).
98
+ export const normHhfEq = axiomThm(
99
+ mkEquals(
100
+ mkImplies(
101
+ propVar("phi"),
102
+ mkApp(
103
+ allConst(typeA),
104
+ mkAbs("x", typeA, mkApp(mkVar("psi", 0, funT(typeA, propT)), mkBound(0))),
105
+ ),
106
+ ),
107
+ mkApp(
108
+ allConst(typeA),
109
+ mkAbs(
110
+ "x",
111
+ typeA,
112
+ mkImplies(propVar("phi"), mkApp(mkVar("psi", 0, funT(typeA, propT)), mkBound(0))),
113
+ ),
114
+ ),
115
+ ),
116
+ );
117
+
118
+ // Drule.is_norm_hhf: no quantifier right of an implication and no beta redex.
119
+ export const isNormHhf = (isProtecting: boolean, term: Term): boolean => {
120
+ if (term.kind === "Const") return term.name !== "Pure.sort_constraint";
121
+ if (term.kind === "Abs") return isNormHhf(isProtecting, term.body);
122
+ if (term.kind !== "App") return true;
123
+ if (term.fun.kind === "Abs") return false;
124
+ if (
125
+ term.fun.kind === "App" &&
126
+ term.fun.fun.kind === "Const" &&
127
+ term.fun.fun.name === "Pure.imp" &&
128
+ term.arg.kind === "App" &&
129
+ term.arg.fun.kind === "Const" &&
130
+ term.arg.fun.name === "Pure.all"
131
+ )
132
+ return false;
133
+ if (isProtected(term)) return isProtecting || isNormHhf(isProtecting, term.arg);
134
+ return isNormHhf(isProtecting, term.fun) && isNormHhf(isProtecting, term.arg);
135
+ };
136
+
137
+ // Drule.outer_params: the outermost parameters, named apart.
138
+ export const outerParams = (term: Term) => {
139
+ const vars = stripAllVars(term);
140
+ const names = variantList(
141
+ [],
142
+ vars.map((variable) => cleanName(variable.name)),
143
+ );
144
+ return vars.map((variable, index) => ({ name: names[index], type: variable.type }));
145
+ };
146
+
147
+ // Variable.gen_all: the outermost parameters become schematic variables.
148
+ export const genAll = (thm: Thm) => {
149
+ const index = Math.max(thm.maxidx, -1) + 1;
150
+ return outerParams(thm.prop).reduce(
151
+ (result, param) => forallElim(mkVar(param.name, index, param.type), result),
152
+ thm,
153
+ );
154
+ };
155
+
156
+ // Thm.forall_intr_vars: quantifies the schematic variables in order of appearance.
157
+ export const forallIntrVars = (thm: Thm) => {
158
+ const vars = fullProps(thm).reduce(
159
+ (result, term) =>
160
+ foldVars(
161
+ (atom, current: Var[]) =>
162
+ !current.some((other) => other.name === atom.name && other.index === atom.index)
163
+ ? [...current, atom]
164
+ : current,
165
+ term,
166
+ result,
167
+ ),
168
+ [] as Var[],
169
+ );
170
+ return vars.reduceRight((result, variable) => forallIntr(variable, result), thm);
171
+ };
172
+
173
+ const variantBound = (name: string, used: NameContext) =>
174
+ variantName(name.startsWith(":") ? "u" : name, used);
175
+
176
+ const fullProps = (thm: Thm) => [...thm.tpairs.flatMap((pair) => [pair[0], pair[1]]), thm.prop];
177
+
178
+ // Drule.zero_var_indexes: renames the schematic variables apart with index 0.
179
+ export const zeroVarIndexes = (thm: Thm): Thm => {
180
+ const terms = fullProps(thm);
181
+ const tvars = terms.reduce(
182
+ (table, term) =>
183
+ foldTypes(
184
+ (type, inner) =>
185
+ foldAtyps(
186
+ (atom, current: typeof table) =>
187
+ atom.kind === "TVar" ? updateTable(current, atom, atom) : current,
188
+ type,
189
+ inner,
190
+ ),
191
+ term,
192
+ table,
193
+ ),
194
+ emptyTable<IndexKey, Typ>(fastIndexnameOrd),
195
+ );
196
+ let used = emptyNameContext;
197
+ let tyenv: TyEnv = emptyTable(fastIndexnameOrd);
198
+ for (const [key, type] of tableEntries(tvars)) {
199
+ const [name, next] = variantBound(key.name, used);
200
+ used = next;
201
+ if (name !== key.name || key.index !== 0)
202
+ tyenv = updateTable(tyenv, key, mkTVar(name, 0, type.kind === "TVar" ? type.sort : []));
203
+ }
204
+ const vars = terms.reduce(
205
+ (table, term) =>
206
+ foldVars(
207
+ (atom, current: typeof table) =>
208
+ lookupTable(current, atom) === undefined
209
+ ? updateTable(current, atom, mkVar(atom.name, atom.index, normType(tyenv, atom.type)))
210
+ : current,
211
+ term,
212
+ table,
213
+ ),
214
+ emptyTable<IndexKey, Var>(fastIndexnameOrd),
215
+ );
216
+ used = emptyNameContext;
217
+ const insts: Instantiation[] = [];
218
+ for (const [key, variable] of tableEntries(vars)) {
219
+ const [name, next] = variantBound(key.name, used);
220
+ used = next;
221
+ if (name !== key.name || key.index !== 0)
222
+ insts.push({ variable, term: mkVar(name, 0, variable.type) });
223
+ }
224
+ return instantiate(insts, instantiateTypes(tyenv, thm));
225
+ };
226
+
227
+ // Tactic.make_elim: turns a destruction rule into an elimination rule.
228
+ export const makeElim = (rule: Thm) => zeroVarIndexes(rs(rule, revcutRl));
229
+
230
+ // Drule.compose: resolves tha into premise index of thb without lifting, with a unique result.
231
+ export const compose = (tha: Thm, index: number, thb: Thm): Thm => {
232
+ const results = listOfSeq(
233
+ bicompose(
234
+ undefined,
235
+ { flatten: true, match: false, incremented: true },
236
+ { isElim: false, rule: tha, subgoals: 0 },
237
+ index,
238
+ thb,
239
+ ),
240
+ ).filter((result, position, all) => all.findIndex((other) => eqThm(other, result)) === position);
241
+ if (results.length !== 1)
242
+ throw new TlaIsabelleError("THM", `compose: unique result expected ${index}`);
243
+ return results[0];
244
+ };
245
+
246
+ // COMP_INCR
247
+ export const compIncr = (tha: Thm, thb: Thm) =>
248
+ compose(tha, 1, incrIndexesThm(tha.maxidx + 1, thb));
249
+
250
+ // Drule.comp_no_flatten: composes th, keeping its first count premises, into premise index of
251
+ // rule.
252
+ export const compNoFlatten = (thm: Thm, count: number, index: number, rule: Thm): Thm => {
253
+ const results = listOfSeq(
254
+ bicompose(
255
+ undefined,
256
+ { flatten: false, match: false, incremented: true },
257
+ { isElim: false, rule: thm, subgoals: count },
258
+ index,
259
+ incrIndexesThm(thm.maxidx + 1, rule),
260
+ ),
261
+ ).filter((result, position, all) => all.findIndex((other) => eqThm(other, result)) === position);
262
+ if (results.length !== 1) throw new TlaIsabelleError("THM", `comp_no_flatten ${index}`);
263
+ return results[0];
264
+ };
@@ -0,0 +1,363 @@
1
+ // Ported from Isabelle2025's Pure/envir.ML. Copyright (c) 1986-2025 University of Cambridge,
2
+ // Technische Universitaet Muenchen, and contributors, see LICENSE.
3
+ //
4
+ // Environments of assignments to schematic variables, and beta and eta normalization.
5
+
6
+ import { TlaIsabelleError } from "../../../errors";
7
+ import {
8
+ emptyTable,
9
+ fastIndexnameOrd,
10
+ hasKey,
11
+ isEmptyTable,
12
+ lookupTable,
13
+ minTable,
14
+ indexnameOrd,
15
+ updateTable,
16
+ type IndexKey,
17
+ type Table,
18
+ } from "./table";
19
+ import {
20
+ binderTypes as plainBinderTypes,
21
+ flagsOf,
22
+ funT,
23
+ HAS_ETA_REDEX,
24
+ HAS_REDEX,
25
+ HAS_VAR,
26
+ isDependent,
27
+ listComb,
28
+ maxidxOfTerm,
29
+ mkAbs,
30
+ mkApp,
31
+ mkBound,
32
+ mkVar,
33
+ substBound,
34
+ updateAbs,
35
+ updateApp,
36
+ incrBoundvars,
37
+ fastypeOf1,
38
+ aconv,
39
+ type Term,
40
+ type Typ,
41
+ type Var,
42
+ } from "./term";
43
+ import { devar, emptyTyEnv, normType, substType, unifiedTypes, type TyEnv } from "./type";
44
+
45
+ export interface Binding {
46
+ readonly type: Typ;
47
+ readonly term: Term;
48
+ }
49
+
50
+ export type TEnv = Table<IndexKey, Binding>;
51
+
52
+ export interface Envir {
53
+ readonly maxidx: number;
54
+ readonly tenv: TEnv;
55
+ readonly tyenv: TyEnv;
56
+ }
57
+
58
+ export const emptyTEnv: TEnv = emptyTable(fastIndexnameOrd);
59
+
60
+ export const emptyEnvir = (maxidx: number): Envir => ({
61
+ maxidx,
62
+ tenv: emptyTEnv,
63
+ tyenv: emptyTyEnv,
64
+ });
65
+
66
+ export const initEnvir = emptyEnvir(-1);
67
+
68
+ export const isEmptyEnvir = (envir: Envir) => isEmptyTable(envir.tenv) && isEmptyTable(envir.tyenv);
69
+
70
+ // radixstring (26, "a", n)
71
+ const radixString = (value: number): string => {
72
+ const digits: number[] = [];
73
+ let remaining = value;
74
+ do {
75
+ digits.unshift(remaining % 26);
76
+ remaining = Math.floor(remaining / 26);
77
+ } while (remaining > 0);
78
+ return digits.map((digit) => String.fromCharCode(97 + digit)).join("");
79
+ };
80
+
81
+ // Envir.genvars: fresh variables with an index above every present one.
82
+ export const genvars = (name: string, envir: Envir, types: readonly Typ[]): [Envir, Var[]] => {
83
+ const index = envir.maxidx + 1;
84
+ const vars = types.map((type, position) =>
85
+ mkVar(types.length === 1 ? name : name + radixString(position), index, type),
86
+ );
87
+ return [{ ...envir, maxidx: index }, vars];
88
+ };
89
+
90
+ export const genvar = (name: string, envir: Envir, type: Typ): [Envir, Var] => {
91
+ const [updated, vars] = genvars(name, envir, [type]);
92
+ return [updated, vars[0]];
93
+ };
94
+
95
+ // Envir.lookup1: lookup without chasing type assignments, for environments from matching.
96
+ export const lookup1 = (tenv: TEnv, variable: Var): Term | undefined => {
97
+ const binding = lookupTable(tenv, variable);
98
+ if (binding === undefined) return undefined;
99
+ if (!eqTypExact(variable.type, binding.type))
100
+ throw new TlaIsabelleError("TYPE", "Variable has two distinct types");
101
+ return binding.term;
102
+ };
103
+
104
+ const eqTypExact = (left: Typ, right: Typ) => unifiedTypes(emptyTyEnv, left, right);
105
+
106
+ export const lookup = (envir: Envir, variable: Var): Term | undefined => {
107
+ const binding = lookupTable(envir.tenv, variable);
108
+ if (binding === undefined) return undefined;
109
+ if (!unifiedTypes(envir.tyenv, variable.type, binding.type))
110
+ throw new TlaIsabelleError("TYPE", "Variable has two distinct types");
111
+ return binding.term;
112
+ };
113
+
114
+ export const update = (variable: Var, term: Term, envir: Envir): Envir => {
115
+ if (hasKey(envir.tenv, variable))
116
+ throw new TlaIsabelleError("TERM", "Envir.update: duplicate assignment");
117
+ return {
118
+ ...envir,
119
+ tenv: updateTable(
120
+ envir.tenv,
121
+ { name: variable.name, index: variable.index },
122
+ { type: variable.type, term },
123
+ ),
124
+ };
125
+ };
126
+
127
+ // Envir.above: whether the least index updated exceeds limit.
128
+ export const above = (envir: Envir, limit: number) => {
129
+ const term = minTable(envir.tenv);
130
+ const type = minTable(envir.tyenv);
131
+ return (
132
+ (term === undefined || term[0].index > limit) && (type === undefined || type[0].index > limit)
133
+ );
134
+ };
135
+
136
+ // Envir.vupdate: an assignment that suppresses higher indexes between variables.
137
+ export const vupdate = (variable: Var, term: Term, envir: Envir): Envir => {
138
+ if (term.kind !== "Var") return update(variable, term, envir);
139
+ if (variable.name === term.name && variable.index === term.index) return envir;
140
+ if (indexnameOrd(variable, term) < 0) {
141
+ const assigned = lookup(envir, term);
142
+ return assigned === undefined
143
+ ? update(term, mkVar(variable.name, variable.index, term.type), envir)
144
+ : vupdate(variable, assigned, envir);
145
+ }
146
+ return update(variable, term, envir);
147
+ };
148
+
149
+ export const couldBetaContract = (term: Term) => (flagsOf(term) & HAS_REDEX) !== 0;
150
+
151
+ const normTerm1 = (tenv: TEnv, term: Term): Term => {
152
+ if ((flagsOf(term) & (HAS_VAR | HAS_REDEX)) === 0) return term;
153
+ switch (term.kind) {
154
+ case "Var": {
155
+ const assigned = lookup1(tenv, term);
156
+ return assigned === undefined ? term : normTerm1(tenv, assigned);
157
+ }
158
+ case "Abs":
159
+ return updateAbs(term, normTerm1(tenv, term.body));
160
+ case "App": {
161
+ if (term.fun.kind === "Abs") return normTerm1(tenv, substBound(term.arg, term.fun.body));
162
+ const fun = normTerm1(tenv, term.fun);
163
+ if (fun.kind === "Abs") return normTerm1(tenv, substBound(term.arg, fun.body));
164
+ return updateApp(term, fun, normTerm1(tenv, term.arg));
165
+ }
166
+ default:
167
+ return term;
168
+ }
169
+ };
170
+
171
+ const normTerm2 = (envir: Envir, term: Term): Term => {
172
+ if (maxidxOfTerm(term) < 0 && (flagsOf(term) & HAS_REDEX) === 0) return term;
173
+ switch (term.kind) {
174
+ case "Const":
175
+ case "Free": {
176
+ const type = normType(envir.tyenv, term.type);
177
+ return type === term.type ? term : { ...term, type };
178
+ }
179
+ case "Var": {
180
+ const assigned = lookup(envir, term);
181
+ if (assigned !== undefined) return normTerm2(envir, assigned);
182
+ const type = normType(envir.tyenv, term.type);
183
+ return type === term.type ? term : { ...term, type };
184
+ }
185
+ case "Abs":
186
+ return updateAbs(term, normTerm2(envir, term.body), normType(envir.tyenv, term.type));
187
+ case "App": {
188
+ if (term.fun.kind === "Abs") return normTerm2(envir, substBound(term.arg, term.fun.body));
189
+ const fun = normTerm2(envir, term.fun);
190
+ if (fun.kind === "Abs") return normTerm2(envir, substBound(term.arg, fun.body));
191
+ return updateApp(term, fun, normTerm2(envir, term.arg));
192
+ }
193
+ default:
194
+ return term;
195
+ }
196
+ };
197
+
198
+ // Envir.norm_term: instantiates the environment and beta-normalizes.
199
+ export const normTerm = (envir: Envir, term: Term): Term => {
200
+ if (isEmptyEnvir(envir) && !couldBetaContract(term)) return term;
201
+ return isEmptyTable(envir.tyenv) ? normTerm1(envir.tenv, term) : normTerm2(envir, term);
202
+ };
203
+
204
+ export const betaNorm = (term: Term) => normTerm(initEnvir, term);
205
+
206
+ // Envir.head_norm: normalizes the head only, for unification.
207
+ export const headNorm = (envir: Envir, term: Term): Term => {
208
+ if ((flagsOf(term) & (HAS_VAR | HAS_REDEX)) === 0) return term;
209
+ switch (term.kind) {
210
+ case "Var": {
211
+ const assigned = lookup(envir, term);
212
+ return assigned === undefined ? term : headNorm(envir, assigned);
213
+ }
214
+ case "Abs":
215
+ return updateAbs(term, headNorm(envir, term.body));
216
+ case "App": {
217
+ if (term.fun.kind === "Abs") return headNorm(envir, substBound(term.arg, term.fun.body));
218
+ const fun = headNorm(envir, term.fun);
219
+ if (fun.kind === "Abs") return headNorm(envir, substBound(term.arg, fun.body));
220
+ return fun === term.fun ? term : mkApp(fun, term.arg);
221
+ }
222
+ default:
223
+ return term;
224
+ }
225
+ };
226
+
227
+ export const etaLong = (types: readonly Typ[], term: Term): Term => {
228
+ if (term.kind === "Abs")
229
+ return mkAbs(term.name, term.type, etaLong([term.type, ...types], term.body));
230
+ const [head, args] = stripCombLocal(term);
231
+ if (head.kind === "Abs") return etaLong(types, betaNorm(term));
232
+ const binders = plainBinderTypes(fastypeOf1(types, term));
233
+ const count = binders.length;
234
+ const inner = [...binders].reverse().concat(types);
235
+ const long = (current: Term) => etaLong(inner, current);
236
+ const body = listComb(incrBoundvars(count, head), [
237
+ ...args.map((arg) => long(incrBoundvars(count, arg))),
238
+ ...binders.map((_type, position) => long(mkBound(count - 1 - position))),
239
+ ]);
240
+ return binders.reduceRight((result, type) => mkAbs("x", type, result), body);
241
+ };
242
+
243
+ const stripCombLocal = (term: Term): [Term, Term[]] => {
244
+ const args: Term[] = [];
245
+ let current = term;
246
+ while (current.kind === "App") {
247
+ args.unshift(current.arg);
248
+ current = current.fun;
249
+ }
250
+ return [current, args];
251
+ };
252
+
253
+ const decrementLoose = (term: Term, level: number): Term => {
254
+ switch (term.kind) {
255
+ case "Bound":
256
+ return term.index >= level ? mkBound(term.index - 1) : term;
257
+ case "Abs":
258
+ return updateAbs(term, decrementLoose(term.body, level + 1));
259
+ case "App":
260
+ return updateApp(term, decrementLoose(term.fun, level), decrementLoose(term.arg, level));
261
+ default:
262
+ return term;
263
+ }
264
+ };
265
+
266
+ export const couldEtaContract = (term: Term) => (flagsOf(term) & HAS_ETA_REDEX) !== 0;
267
+
268
+ const etaSame = (term: Term): Term => {
269
+ if (!couldEtaContract(term)) return term;
270
+ if (term.kind === "Abs") {
271
+ const body = etaSame(term.body);
272
+ if (body.kind === "App" && body.arg.kind === "Bound" && body.arg.index === 0) {
273
+ if (isDependent(body.fun))
274
+ return body === term.body ? term : mkAbs(term.name, term.type, body);
275
+ return decrementLoose(body.fun, 0);
276
+ }
277
+ return body === term.body ? term : mkAbs(term.name, term.type, body);
278
+ }
279
+ if (term.kind === "App") return updateApp(term, etaSame(term.fun), etaSame(term.arg));
280
+ return term;
281
+ };
282
+
283
+ // Envir.eta_contract
284
+ export const etaContract = (term: Term) => (couldEtaContract(term) ? etaSame(term) : term);
285
+
286
+ export const betaEtaContract = (term: Term) => etaContract(betaNorm(term));
287
+
288
+ export const aeconv = (left: Term, right: Term) =>
289
+ aconv(left, right) || aconv(etaContract(left), etaContract(right));
290
+
291
+ export const envBodyType = (envir: Envir, type: Typ): Typ => {
292
+ if (type.kind === "Type" && type.name === "fun") return envBodyType(envir, type.args[1]);
293
+ if (type.kind === "TVar") {
294
+ const assigned = lookupTable(envir.tyenv, type);
295
+ return assigned === undefined ? type : envBodyType(envir, assigned);
296
+ }
297
+ return type;
298
+ };
299
+
300
+ export const envBinderTypes = (envir: Envir, type: Typ): Typ[] => {
301
+ if (type.kind === "Type" && type.name === "fun")
302
+ return [type.args[0], ...envBinderTypes(envir, type.args[1])];
303
+ if (type.kind === "TVar") {
304
+ const assigned = lookupTable(envir.tyenv, type);
305
+ return assigned === undefined ? [] : envBinderTypes(envir, assigned);
306
+ }
307
+ return [];
308
+ };
309
+
310
+ // Envir.fastype: the type of a term, chasing type assignments at applications.
311
+ export const fastype = (envir: Envir, types: readonly Typ[], term: Term): Typ => {
312
+ switch (term.kind) {
313
+ case "App": {
314
+ const type = devar(envir.tyenv, fastype(envir, types, term.fun));
315
+ if (type.kind === "Type" && type.name === "fun") return type.args[1];
316
+ throw new TlaIsabelleError("TYPE", "fastype: expected function type");
317
+ }
318
+ case "Bound": {
319
+ const type = types[term.index];
320
+ if (type === undefined) throw new TlaIsabelleError("TYPE", "fastype: Bound");
321
+ return type;
322
+ }
323
+ case "Abs":
324
+ return funT(term.type, fastype(envir, [term.type, ...types], term.body));
325
+ default:
326
+ return term.type;
327
+ }
328
+ };
329
+
330
+ // Envir.subst_term: instantiates without chasing variables.
331
+ const substTermAux = (tyenv: TyEnv, tenv: TEnv, term: Term): Term => {
332
+ if (maxidxOfTerm(term) < 0) return term;
333
+ switch (term.kind) {
334
+ case "Const":
335
+ case "Free": {
336
+ const type = isEmptyTable(tyenv) ? term.type : substType(tyenv, term.type);
337
+ return type === term.type ? term : { ...term, type };
338
+ }
339
+ case "Var": {
340
+ const assigned = lookup1(tenv, term);
341
+ if (assigned !== undefined) return assigned;
342
+ const type = isEmptyTable(tyenv) ? term.type : substType(tyenv, term.type);
343
+ return type === term.type ? term : { ...term, type };
344
+ }
345
+ case "Bound":
346
+ return term;
347
+ case "Abs":
348
+ return updateAbs(
349
+ term,
350
+ substTermAux(tyenv, tenv, term.body),
351
+ isEmptyTable(tyenv) ? term.type : substType(tyenv, term.type),
352
+ );
353
+ case "App":
354
+ return updateApp(
355
+ term,
356
+ substTermAux(tyenv, tenv, term.fun),
357
+ substTermAux(tyenv, tenv, term.arg),
358
+ );
359
+ }
360
+ };
361
+
362
+ export const substTerm = (tyenv: TyEnv, tenv: TEnv, term: Term) =>
363
+ isEmptyTable(tenv) && isEmptyTable(tyenv) ? term : substTermAux(tyenv, tenv, term);