gdp-wired 0.48.0 → 0.49.0

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 (295) hide show
  1. package/README.md +2 -2
  2. package/dist/core/bmc.js +10 -7
  3. package/dist/core/check.js +7 -2
  4. package/dist/core/count-attempt-work.d.ts +29 -0
  5. package/dist/core/count-attempt-work.js +96 -0
  6. package/dist/core/count-boolean-partition.d.ts +5 -0
  7. package/dist/core/count-boolean-partition.js +47 -0
  8. package/dist/core/count-bounded-quotient.d.ts +2 -0
  9. package/dist/core/count-bounded-quotient.js +13 -0
  10. package/dist/core/count-case-compose.d.ts +20 -0
  11. package/dist/core/count-case-compose.js +289 -0
  12. package/dist/core/count-cases.js +3 -2
  13. package/dist/core/count-cert-rename.d.ts +10 -0
  14. package/dist/core/count-cert-rename.js +69 -0
  15. package/dist/core/count-cert-transport.d.ts +10 -2
  16. package/dist/core/count-cert-transport.js +60 -9
  17. package/dist/core/count-certificate-minimize.d.ts +57 -0
  18. package/dist/core/count-certificate-minimize.js +263 -0
  19. package/dist/core/count-closed-family.d.ts +32 -0
  20. package/dist/core/count-closed-family.js +143 -0
  21. package/dist/core/count-closing-budget.d.ts +10 -0
  22. package/dist/core/count-closing-budget.js +49 -0
  23. package/dist/core/count-closing-needs.d.ts +5 -0
  24. package/dist/core/count-closing-needs.js +9 -0
  25. package/dist/core/count-component-budget.d.ts +5 -0
  26. package/dist/core/count-component-budget.js +9 -0
  27. package/dist/core/count-copy-shape.d.ts +11 -0
  28. package/dist/core/count-copy-shape.js +40 -0
  29. package/dist/core/count-disjoint-funding.d.ts +75 -0
  30. package/dist/core/count-disjoint-funding.js +284 -0
  31. package/dist/core/count-emitter-frame-pool.d.ts +23 -0
  32. package/dist/core/count-emitter-frame-pool.js +87 -0
  33. package/dist/core/count-final-step.d.ts +5 -0
  34. package/dist/core/count-final-step.js +20 -0
  35. package/dist/core/count-fixed-frame.d.ts +4 -0
  36. package/dist/core/count-fixed-frame.js +30 -0
  37. package/dist/core/count-frames.d.ts +25 -0
  38. package/dist/core/count-frames.js +118 -0
  39. package/dist/core/count-frozen-guard.d.ts +2 -0
  40. package/dist/core/count-frozen-guard.js +31 -0
  41. package/dist/core/count-generation-phase.d.ts +7 -0
  42. package/dist/core/count-generation-phase.js +33 -0
  43. package/dist/core/count-guard-composition.d.ts +12 -0
  44. package/dist/core/count-guard-composition.js +48 -0
  45. package/dist/core/count-guard-context.d.ts +11 -0
  46. package/dist/core/count-guard-context.js +50 -0
  47. package/dist/core/count-guard-facts.d.ts +50 -0
  48. package/dist/core/count-guard-facts.js +97 -0
  49. package/dist/core/count-guard-proposals.d.ts +7 -0
  50. package/dist/core/count-guard-proposals.js +106 -0
  51. package/dist/core/count-guard-shape.d.ts +18 -0
  52. package/dist/core/count-guard-shape.js +100 -0
  53. package/dist/core/count-guard-tree.d.ts +17 -0
  54. package/dist/core/count-guard-tree.js +125 -0
  55. package/dist/core/count-leader-facts.d.ts +10 -0
  56. package/dist/core/count-leader-facts.js +52 -0
  57. package/dist/core/count-leader.d.ts +10 -0
  58. package/dist/core/count-leader.js +331 -0
  59. package/dist/core/count-lemma-transport.d.ts +8 -0
  60. package/dist/core/count-lemma-transport.js +47 -0
  61. package/dist/core/count-lifted-budget.d.ts +54 -0
  62. package/dist/core/count-lifted-budget.js +97 -0
  63. package/dist/core/count-lifted-copy.d.ts +3 -0
  64. package/dist/core/count-lifted-copy.js +166 -0
  65. package/dist/core/count-lifted-increment.d.ts +3 -0
  66. package/dist/core/count-lifted-increment.js +146 -0
  67. package/dist/core/count-lifted-load.d.ts +5 -0
  68. package/dist/core/count-lifted-load.js +179 -0
  69. package/dist/core/count-lifted-prerequisite.d.ts +14 -0
  70. package/dist/core/count-lifted-prerequisite.js +53 -0
  71. package/dist/core/count-lifted-replacement.d.ts +3 -0
  72. package/dist/core/count-lifted-replacement.js +540 -0
  73. package/dist/core/count-lifted-shape.d.ts +63 -0
  74. package/dist/core/count-lifted-shape.js +115 -0
  75. package/dist/core/count-lifted-strategy.d.ts +7 -0
  76. package/dist/core/count-lifted-strategy.js +226 -0
  77. package/dist/core/count-lifted-transports.d.ts +32 -0
  78. package/dist/core/count-lifted-transports.js +293 -0
  79. package/dist/core/count-load-shape.d.ts +17 -0
  80. package/dist/core/count-load-shape.js +67 -0
  81. package/dist/core/count-minimal-view.d.ts +26 -0
  82. package/dist/core/count-minimal-view.js +46 -0
  83. package/dist/core/count-operation-observation.d.ts +9 -0
  84. package/dist/core/count-operation-observation.js +22 -0
  85. package/dist/core/count-operation-order.d.ts +5 -0
  86. package/dist/core/count-operation-order.js +4 -0
  87. package/dist/core/count-phase.d.ts +9 -0
  88. package/dist/core/count-phase.js +127 -0
  89. package/dist/core/count-premise-search.d.ts +9 -0
  90. package/dist/core/count-premise-search.js +41 -0
  91. package/dist/core/count-premise-view.d.ts +19 -0
  92. package/dist/core/count-premise-view.js +220 -0
  93. package/dist/core/count-prerequisite-components.d.ts +67 -0
  94. package/dist/core/count-prerequisite-components.js +239 -0
  95. package/dist/core/count-prerequisite-memory.d.ts +24 -0
  96. package/dist/core/count-prerequisite-memory.js +37 -0
  97. package/dist/core/count-prerequisite-plan.d.ts +12 -0
  98. package/dist/core/count-prerequisite-plan.js +88 -0
  99. package/dist/core/count-primitive-budget.d.ts +1 -0
  100. package/dist/core/count-primitive-budget.js +8 -0
  101. package/dist/core/count-project-free.d.ts +3 -0
  102. package/dist/core/count-project-free.js +24 -0
  103. package/dist/core/count-project-witness.d.ts +4 -0
  104. package/dist/core/count-project-witness.js +48 -0
  105. package/dist/core/count-proof-rescope.d.ts +14 -0
  106. package/dist/core/count-proof-rescope.js +71 -0
  107. package/dist/core/count-proof-scope.d.ts +68 -0
  108. package/dist/core/count-proof-scope.js +333 -0
  109. package/dist/core/count-prune-owned-root.d.ts +5 -0
  110. package/dist/core/count-prune-owned-root.js +27 -0
  111. package/dist/core/count-query-context.d.ts +3 -0
  112. package/dist/core/count-query-context.js +7 -0
  113. package/dist/core/count-query.d.ts +15 -0
  114. package/dist/core/count-query.js +59 -0
  115. package/dist/core/count-quorum-leaf.d.ts +9 -0
  116. package/dist/core/count-quorum-leaf.js +38 -0
  117. package/dist/core/count-reachability-budget.d.ts +1 -0
  118. package/dist/core/count-reachability-budget.js +7 -0
  119. package/dist/core/count-reachability-reconcile.d.ts +11 -0
  120. package/dist/core/count-reachability-reconcile.js +19 -0
  121. package/dist/core/count-reachability.d.ts +50 -0
  122. package/dist/core/count-reachability.js +351 -0
  123. package/dist/core/count-rebase-path.d.ts +9 -0
  124. package/dist/core/count-rebase-path.js +68 -0
  125. package/dist/core/count-recipe-context.d.ts +45 -0
  126. package/dist/core/count-recipe-context.js +71 -0
  127. package/dist/core/count-recipe-controller.d.ts +115 -0
  128. package/dist/core/count-recipe-controller.js +417 -0
  129. package/dist/core/count-recipe-graph.d.ts +36 -0
  130. package/dist/core/count-recipe-graph.js +211 -0
  131. package/dist/core/count-recovery/conditional-check-cache.js +3 -2
  132. package/dist/core/count-recovery/conditional-cuts.js +5 -4
  133. package/dist/core/count-recovery/conditional-proof.d.ts +37 -3
  134. package/dist/core/count-recovery/conditional-proof.js +72 -30
  135. package/dist/core/count-recovery/derivability-closure.js +2 -1
  136. package/dist/core/count-recovery/guard-cases.js +2 -1
  137. package/dist/core/count-recovery/reframe-tree.d.ts +2 -0
  138. package/dist/core/count-recovery/reframe-tree.js +24 -0
  139. package/dist/core/count-recovery/replay-linear-skeleton.d.ts +2 -0
  140. package/dist/core/count-recovery/replay-linear-skeleton.js +28 -0
  141. package/dist/core/count-recovery/row-cases.js +2 -1
  142. package/dist/core/count-rejection-budget.d.ts +8 -0
  143. package/dist/core/count-rejection-budget.js +15 -0
  144. package/dist/core/count-replacement.d.ts +5 -0
  145. package/dist/core/count-replacement.js +129 -0
  146. package/dist/core/count-replay-diagnostic.d.ts +21 -0
  147. package/dist/core/count-replay-diagnostic.js +49 -0
  148. package/dist/core/count-replay-skeleton.d.ts +4 -0
  149. package/dist/core/count-replay-skeleton.js +28 -0
  150. package/dist/core/count-rescope-funding.d.ts +3 -0
  151. package/dist/core/count-rescope-funding.js +17 -0
  152. package/dist/core/count-restrictions.d.ts +11 -0
  153. package/dist/core/count-restrictions.js +31 -0
  154. package/dist/core/count-retry-budget.d.ts +67 -0
  155. package/dist/core/count-retry-budget.js +114 -0
  156. package/dist/core/count-retry-round.d.ts +24 -0
  157. package/dist/core/count-retry-round.js +44 -0
  158. package/dist/core/count-row-agreement.d.ts +35 -0
  159. package/dist/core/count-row-agreement.js +270 -0
  160. package/dist/core/count-row-aliases.d.ts +3 -0
  161. package/dist/core/count-row-aliases.js +59 -0
  162. package/dist/core/count-row-composition.d.ts +6 -0
  163. package/dist/core/count-row-composition.js +55 -0
  164. package/dist/core/count-row-copy-linear.d.ts +4 -0
  165. package/dist/core/count-row-copy-linear.js +83 -0
  166. package/dist/core/count-row-copy.d.ts +4 -0
  167. package/dist/core/count-row-copy.js +94 -0
  168. package/dist/core/count-row-policy.d.ts +3 -0
  169. package/dist/core/count-row-policy.js +56 -0
  170. package/dist/core/count-row-reindex.d.ts +11 -0
  171. package/dist/core/count-row-reindex.js +126 -0
  172. package/dist/core/count-row-support.d.ts +9 -0
  173. package/dist/core/count-row-support.js +42 -0
  174. package/dist/core/count-row-symmetry.d.ts +7 -0
  175. package/dist/core/count-row-symmetry.js +100 -0
  176. package/dist/core/count-scope-hyps.d.ts +2 -0
  177. package/dist/core/count-scope-hyps.js +163 -0
  178. package/dist/core/count-scope-row-reuse.d.ts +3 -0
  179. package/dist/core/count-scope-row-reuse.js +92 -0
  180. package/dist/core/count-search-allowance.d.ts +1 -0
  181. package/dist/core/count-search-allowance.js +17 -0
  182. package/dist/core/count-stabilize-tree.d.ts +9 -0
  183. package/dist/core/count-stabilize-tree.js +35 -0
  184. package/dist/core/count-stage-trace.d.ts +8 -0
  185. package/dist/core/count-stage-trace.js +45 -0
  186. package/dist/core/count-strategies.d.ts +16 -0
  187. package/dist/core/count-strategies.js +155 -0
  188. package/dist/core/count-strategy-premises.d.ts +5 -0
  189. package/dist/core/count-strategy-premises.js +14 -0
  190. package/dist/core/count-strategy.d.ts +41 -0
  191. package/dist/core/count-strategy.js +76 -0
  192. package/dist/core/count-support.js +3 -2
  193. package/dist/core/count-tier-budget.d.ts +14 -0
  194. package/dist/core/count-tier-budget.js +64 -0
  195. package/dist/core/count-unwritten-prefix.d.ts +24 -0
  196. package/dist/core/count-unwritten-prefix.js +97 -0
  197. package/dist/core/count-write-shape.d.ts +15 -0
  198. package/dist/core/count-write-shape.js +73 -0
  199. package/dist/core/counts.d.ts +37 -13
  200. package/dist/core/counts.js +990 -127
  201. package/dist/core/cycles.js +5 -4
  202. package/dist/core/cyclic.js +11 -10
  203. package/dist/core/data.js +16 -15
  204. package/dist/core/dsl-deadline.d.ts +3 -0
  205. package/dist/core/dsl-deadline.js +27 -0
  206. package/dist/core/dsl.d.ts +9 -0
  207. package/dist/core/dsl.js +156 -51
  208. package/dist/core/evaluation-admission.d.ts +1 -0
  209. package/dist/core/evaluation-admission.js +3 -0
  210. package/dist/core/exists.d.ts +6 -1
  211. package/dist/core/exists.js +51 -9
  212. package/dist/core/field-laws.d.ts +3 -1
  213. package/dist/core/field-laws.js +140 -2
  214. package/dist/core/finite-houdini.d.ts +6 -0
  215. package/dist/core/finite-houdini.js +63 -21
  216. package/dist/core/finite-tiny.js +95 -67
  217. package/dist/core/fixtures/count-completion/paxos-decision.json +1 -0
  218. package/dist/core/fresh.js +5 -1
  219. package/dist/core/infer.js +45 -30
  220. package/dist/core/kind.js +2 -1
  221. package/dist/core/lemma-cuts.js +11 -10
  222. package/dist/core/linear-admission.d.ts +14 -0
  223. package/dist/core/linear-admission.js +151 -0
  224. package/dist/core/machine.d.ts +5 -2
  225. package/dist/core/machine.js +105 -31
  226. package/dist/core/meter.d.ts +22 -0
  227. package/dist/core/meter.js +34 -0
  228. package/dist/core/nonterm.js +4 -3
  229. package/dist/core/prover-row-budget.d.ts +3 -0
  230. package/dist/core/prover-row-budget.js +106 -0
  231. package/dist/core/quorum-laws.d.ts +21 -1
  232. package/dist/core/quorum-laws.js +27 -3
  233. package/dist/core/rationals.js +3 -2
  234. package/dist/core/rejection-discovery.d.ts +34 -0
  235. package/dist/core/rejection-discovery.js +36 -0
  236. package/dist/core/rejection-stage.d.ts +39 -0
  237. package/dist/core/rejection-stage.js +205 -0
  238. package/dist/core/request-quotient.d.ts +8 -0
  239. package/dist/core/request-quotient.js +299 -0
  240. package/dist/core/row-agreement-shape.d.ts +15 -0
  241. package/dist/core/row-agreement-shape.js +68 -0
  242. package/dist/core/runtime-wall.d.ts +2 -0
  243. package/dist/core/search-policy.d.ts +15 -0
  244. package/dist/core/search-policy.js +68 -0
  245. package/dist/core/solver.d.ts +1 -1
  246. package/dist/core/solver.js +40 -6
  247. package/dist/core/step-work.d.ts +12 -0
  248. package/dist/core/step-work.js +14 -0
  249. package/dist/core/strategy-premises.d.ts +8 -0
  250. package/dist/core/strategy-premises.js +64 -0
  251. package/dist/core/system.js +4 -3
  252. package/dist/core/table-admission.d.ts +1 -0
  253. package/dist/core/table-admission.js +3 -0
  254. package/dist/core/trace-clock.d.ts +3 -0
  255. package/dist/core/trace-clock.js +16 -0
  256. package/dist/core/transfer.js +3 -2
  257. package/dist/core/translate.js +16 -3
  258. package/dist/core/wall.d.ts +6 -1
  259. package/dist/core/wall.js +23 -9
  260. package/dist/core/watch.d.ts +1 -1
  261. package/dist/core/watch.js +13 -4
  262. package/dist/core/work-admission.d.ts +164 -0
  263. package/dist/core/work-admission.js +418 -0
  264. package/dist/core/work-amount.d.ts +21 -0
  265. package/dist/core/work-amount.js +80 -0
  266. package/dist/core/work-collections.d.ts +11 -0
  267. package/dist/core/work-collections.js +198 -0
  268. package/dist/core/work-kinds.d.ts +11 -0
  269. package/dist/core/work-kinds.js +9 -0
  270. package/dist/core/work-meter.d.ts +8 -0
  271. package/dist/core/work-meter.js +83 -0
  272. package/dist/core/work-observe.d.ts +12 -0
  273. package/dist/core/work-observe.js +39 -0
  274. package/dist/core/work-profile.d.ts +10 -0
  275. package/dist/core/work-profile.js +21 -0
  276. package/dist/core/work-refusal.d.ts +1 -0
  277. package/dist/core/work-refusal.js +6 -0
  278. package/dist/interchange/format.js +2 -0
  279. package/dist/kernel/cdcl.js +93 -40
  280. package/dist/kernel/epr.js +23 -8
  281. package/dist/kernel/kernel.d.ts +47 -1
  282. package/dist/kernel/kernel.js +417 -68
  283. package/dist/kernel/prove.d.ts +49 -3
  284. package/dist/kernel/prove.js +468 -122
  285. package/dist/kernel/prover-rows.d.ts +1 -0
  286. package/dist/kernel/prover-rows.js +12 -1
  287. package/dist/kernel/reader.d.ts +1 -1
  288. package/dist/kernel/reader.js +57 -12
  289. package/dist/kernel/simplex.js +9 -2
  290. package/dist/lean-kernel/emit.d.ts +9 -1
  291. package/dist/lean-kernel/emit.js +144 -22
  292. package/dist/lean-kernel/legacy-family-library.d.ts +1 -0
  293. package/dist/lean-kernel/legacy-family-library.js +5 -0
  294. package/dist/lean-kernel/library.js +3 -3
  295. package/package.json +1 -1
@@ -18,7 +18,12 @@
18
18
  // meet it), so it holds at the start and at every step; `assume`, about each request, holds only at a step, since no
19
19
  // request has come at the start.
20
20
  import { checkRefutation } from "../core/check.js";
21
- import { add, k, scale, sub, v } from "../core/solver.js";
21
+ import { observeCheckStep, observeTableCells } from "../core/meter.js";
22
+ import { admittedLinearSub as rowSub, admittedLinearAdd as rowAdd, admittedLinearScale as rowScale, admittedLinearCopy as rowCopy, admittedLinearVariable as rowVariable, visitLinearCells } from "../core/linear-admission.js";
23
+ import { fundedAdd as add, fundedConstant as k, fundedScale as scale, fundedSub as sub, fundedVariable as v, admittedLinearProduct, everyLinearCell } from "../core/linear-admission.js";
24
+ import { workMap, workFilter, workConcat, workStrings, workJoin, workSlice, workSetArray, workEntries, workJSON, workFlatMap, workArraySlice } from "../core/work-collections.js";
25
+ import { completeWorkProfile } from "../core/work-profile.js";
26
+ import { startWork } from "../core/work-admission.js";
22
27
  // An exhaustive switch's default: TypeScript proves it unreachable, and if it is reached anyway (a value from outside
23
28
  // the types), it fails loudly instead of returning undefined.
24
29
  export function unreachable(value) { throw new Error(`unhandled case: ${JSON.stringify(value)}`); }
@@ -32,26 +37,34 @@ export const part = (key, i, n) => { let e = key; for (let j = 0; j < i; j++)
32
37
  // (Remembered per linear form: a form is never changed once built, and the same forms are looked up at every node.)
33
38
  const canonMemo = new WeakMap(), flipMemo = new WeakMap();
34
39
  export const canon = (lin) => {
40
+ observeTableCells();
35
41
  let out = canonMemo.get(lin);
36
- if (out === undefined)
37
- canonMemo.set(lin, (out = Object.keys(lin).sort().map(key => `${key}:${lin[key]}`).join(',')));
42
+ if (out === undefined) {
43
+ const keys = [], values = new Map();
44
+ visitLinearCells(lin, (key, value) => { keys.push(key); values.set(key, value); }, 'canon:keys');
45
+ const parts = workMap(workStrings(keys, 'canon:sort'), key => { observeTableCells(key.length + 1); return `${key}:${values.get(key)}`; }, 'canon:format');
46
+ canonMemo.set(lin, (out = workJoin(parts, ',', 'canon:join')));
47
+ }
38
48
  return out;
39
49
  };
40
50
  // canon(1 − lin), the atom that decides lin <= 0 the other way, without building it.
41
51
  export const canonFlip = (lin) => {
52
+ observeTableCells();
42
53
  let out = flipMemo.get(lin);
43
54
  if (out !== undefined)
44
55
  return out;
45
- const keys = Object.keys(lin);
46
- if (!('1' in lin))
56
+ const keys = [], values = new Map();
57
+ visitLinearCells(lin, (key, value) => { keys.push(key); values.set(key, value); }, 'canonFlip:keys');
58
+ if (!values.has('1'))
47
59
  keys.push('1');
48
60
  const parts = [];
49
- for (const key of keys.sort()) {
50
- const c = key === '1' ? 1 - (lin['1'] ?? 0) : -lin[key];
61
+ for (const key of workStrings(keys, 'canonFlip:sort')) {
62
+ observeTableCells(key.length + 1);
63
+ const c = key === '1' ? 1 - (values.get('1') ?? 0) : -values.get(key);
51
64
  if (c !== 0)
52
65
  parts.push(`${key}:${c}`);
53
66
  }
54
- flipMemo.set(lin, (out = parts.join(',')));
67
+ flipMemo.set(lin, (out = workJoin(parts, ',', 'canonFlip:join')));
55
68
  return out;
56
69
  };
57
70
  export const atomOf = (lin, holds) => ({ lin: holds ? lin : add(k(1), scale(lin, -1)), label: '' });
@@ -60,8 +73,10 @@ export const atomOf = (lin, holds) => ({ lin: holds ? lin : add(k(1), scale(lin,
60
73
  // A form with a coefficient past a safe integer decides nothing: it may be a rounded difference, and its flip is
61
74
  // rounded again (1 - (2^53 + 1) reads as the safe 1 - 2^53), so an atom about another form would decide it.
62
75
  function decide(lin, path) {
76
+ observeTableCells();
63
77
  let constant = true;
64
78
  for (const key in lin) {
79
+ observeTableCells(key.length + 1);
65
80
  if (!Number.isSafeInteger(lin[key]))
66
81
  return undefined;
67
82
  if (key !== '1')
@@ -87,6 +102,7 @@ const look = (path, key) => { const out = path.get(key)?.holds; if (recording)
87
102
  const linearMemo = new WeakMap();
88
103
  const freeMemo = new WeakMap(), freeLinear = new WeakMap();
89
104
  const choiceFree = (e) => {
105
+ startWork('ticks', 1, 'kernel choiceFree');
90
106
  let out = freeMemo.get(e);
91
107
  if (out !== undefined)
92
108
  return out;
@@ -120,6 +136,7 @@ const choiceFree = (e) => {
120
136
  return out;
121
137
  };
122
138
  export function linear(e, path) {
139
+ startWork('ticks', 1, 'kernel linear');
123
140
  if (choiceFree(e)) {
124
141
  if (freeLinear.has(e))
125
142
  return freeLinear.get(e);
@@ -144,7 +161,7 @@ function linearOf(e, path) {
144
161
  const out = linearOp(e, path);
145
162
  return out && safe(out) ? out : undefined;
146
163
  }
147
- const safe = (lin) => Object.values(lin).every(c => Number.isSafeInteger(c));
164
+ const safe = (lin) => everyLinearCell(lin, (_key, c) => Number.isSafeInteger(c), 'linear safety');
148
165
  function linearOp(e, path) {
149
166
  switch (e.op) {
150
167
  case 'var': return e.name === '1' || /[*[\],:⟦⟨⟩|]/.test(e.name) ? undefined : v(e.name); // those characters build cells, monomials, term columns and tuple keys
@@ -210,7 +227,7 @@ export const plainMap = (name) => { let ok = plainMaps.get(name); if (ok === und
210
227
  function keyParts(e, path) {
211
228
  if (e.op === 'proj') {
212
229
  const ps = e.k === 0 ? undefined : keyParts(e.a, path);
213
- return ps && ps.length >= 3 ? ps.slice(1) : undefined;
230
+ return ps && ps.length >= 3 ? workArraySlice(ps, 1, 'keyParts:rest') : undefined;
214
231
  }
215
232
  if (e.op !== 'tup')
216
233
  return undefined;
@@ -232,11 +249,19 @@ function keyParts(e, path) {
232
249
  // deep term (a k-step run nests k steps) is read once, not once per lookup.
233
250
  const termIds = new Map(), termTable = [], memo = new WeakMap();
234
251
  function termId(t) {
252
+ startWork('ticks', 1, 'kernel termId');
235
253
  const had = memo.get(t);
236
254
  if (had !== undefined)
237
255
  return had;
238
- const parts = Object.entries(t).sort(([x], [y]) => (x < y ? -1 : x > y ? 1 : 0)).map(([key, val]) => (Array.isArray(val) ? `${key}=[${val.map(x => termId(x)).join(',')}]` : typeof val === 'object' && val !== null ? `${key}=#${termId(val)}` : `${key}=${JSON.stringify(val)}`));
239
- const key = parts.join(';');
256
+ const entries = workEntries(t, 'termId:members'), values = new Map(entries);
257
+ const keys = workStrings(workMap(entries, entry => entry[0], 'termId:keys'), 'termId:sort');
258
+ const parts = workMap(keys, key => {
259
+ const val = values.get(key);
260
+ if (Array.isArray(val))
261
+ return `${key}=[${workJoin(workMap(val, x => String(termId(x)), 'termId:array'), ',', 'termId:array-join')}]`;
262
+ return typeof val === 'object' && val !== null ? `${key}=#${termId(val)}` : `${key}=${workJSON(val, 'termId:scalar')}`;
263
+ }, 'termId:parts');
264
+ const key = workJoin(parts, ';', 'termId:join');
240
265
  let id = termIds.get(key);
241
266
  if (id === undefined) {
242
267
  id = termTable.length;
@@ -247,6 +272,10 @@ function termId(t) {
247
272
  return id;
248
273
  }
249
274
  export const termColumn = (e) => `⟦${termId(e)}⟧`;
275
+ // Output-only state: certificate columns contain these process-local ordinals.
276
+ // Copies protect the table; obtaining a snapshot never registers a new term.
277
+ export const snapshotKernelTermState = () => ({ terms: termTable.slice(), identities: [...termIds] });
278
+ export const snapshotKernelTermMemo = (object) => memo.get(object);
250
279
  export const columnTerm = (name) => { const m = /^⟦(\d+)⟧$/.exec(name); return m ? termTable[Number(m[1])] : undefined; };
251
280
  // Whether the path decides a choice's top (an if's condition, which side of min, max or ∸).
252
281
  export function decided(e, path) {
@@ -260,30 +289,30 @@ export function decided(e, path) {
260
289
  // The rows a term column brings: its bounds, true whichever way the choice goes (min(a, b) ≤ a and ≤ b; max(a, b) ≥ a
261
290
  // and ≥ b; a ∸ b ≥ a − b and ≤ a), and, once the path decides it, its equality to the branch taken.
262
291
  export function termRows(name, path) {
263
- const e = columnTerm(name), col = v(name), out = [];
292
+ const e = columnTerm(name), col = rowVariable(name, 1, 'termRows:column'), out = [];
264
293
  if (e.op === 'min' || e.op === 'max' || e.op === 'sub') {
265
294
  const a = linear(e.a, path), b = linear(e.b, path);
266
295
  if (a && b) {
267
296
  if (e.op === 'min')
268
- out.push(sub(col, a), sub(col, b));
297
+ out.push(rowSub(col, a, 'termRows:min-left'), rowSub(col, b, 'termRows:min-right'));
269
298
  if (e.op === 'max')
270
- out.push(sub(a, col), sub(b, col));
299
+ out.push(rowSub(a, col, 'termRows:max-left'), rowSub(b, col, 'termRows:max-right'));
271
300
  if (e.op === 'sub')
272
- out.push(sub(sub(a, b), col), sub(col, a));
301
+ out.push(rowSub(rowSub(a, b, 'termRows:subtract-inputs'), col, 'termRows:subtract-bound'), rowSub(col, a, 'termRows:subtract-upper'));
273
302
  }
274
303
  }
275
304
  // An if is one of its two naturals, so at most their sum, whichever way its condition goes (a yes/no's if(c, 1, 0)
276
305
  // is at most 1 without deciding c). Only over constant branches: over a variable one (a map's if(c, Given[k], 0)),
277
306
  // each such row is one more in every leaf and slows the solve more than it saves.
278
307
  if (e.op === 'if') {
279
- const a = linear(e.then, path), b = linear(e.else, path), constant = (l) => Object.entries(l).every(([key, c]) => key === '1' || c === 0);
308
+ const a = linear(e.then, path), b = linear(e.else, path), constant = (l) => everyLinearCell(l, (key, c) => key === '1' || c === 0, 'constant branch');
280
309
  if (a && b && constant(a) && constant(b))
281
- out.push(sub(col, add(a, b)));
310
+ out.push(rowSub(col, rowAdd(a, b, 'termRows:if-sum'), 'termRows:if-bound'));
282
311
  }
283
312
  if (decided(e, path)) {
284
313
  const r = linear(e, path);
285
314
  if (r)
286
- out.push(sub(col, r), sub(r, col));
315
+ out.push(rowSub(col, r, 'termRows:equal-left'), rowSub(r, col, 'termRows:equal-right'));
287
316
  }
288
317
  return out;
289
318
  }
@@ -295,9 +324,18 @@ export function termRows(name, path) {
295
324
  // Every coefficient, product and partial sum a safe integer, else no product: a rounded product is past 2^53, but a
296
325
  // monomial's other products could bring its sum back under it (and a square's form is the prover's, any float).
297
326
  function times(a, b) {
327
+ if (completeWorkProfile.enabled)
328
+ return admittedLinearProduct(a, b, (p, q) => {
329
+ if (p.includes('⟦') || q.includes('⟦'))
330
+ return undefined;
331
+ const fs = workStrings(workConcat(p === '1' ? [] : factorsOf(p), q === '1' ? [] : factorsOf(q), 'product:factors'), 'product:sort');
332
+ return fs.length ? workJoin(fs, '*', 'product:join') : '1';
333
+ }, 'kernel product', true);
298
334
  const out = Object.create(null);
299
- for (const [p, cp] of Object.entries(a))
300
- for (const [q, cq] of Object.entries(b)) {
335
+ for (const p in a)
336
+ for (const q in b) {
337
+ observeTableCells();
338
+ const cp = a[p], cq = b[q];
301
339
  if (p.includes('⟦') || q.includes('⟦'))
302
340
  return undefined;
303
341
  const fs = [...(p === '1' ? [] : factorsOf(p)), ...(q === '1' ? [] : factorsOf(q))].sort(), key = fs.length ? fs.join('*') : '1', c = (out[key] ?? 0) + cp * cq;
@@ -305,9 +343,11 @@ function times(a, b) {
305
343
  return undefined;
306
344
  out[key] = c;
307
345
  }
308
- for (const key in out)
346
+ for (const key in out) {
347
+ observeTableCells();
309
348
  if (out[key] === 0)
310
349
  delete out[key];
350
+ }
311
351
  return out;
312
352
  }
313
353
  // A name's factors: split at each '*' outside a cell's brackets (a cell's key may itself read a monomial).
@@ -319,13 +359,14 @@ export function factorsOf(name) {
319
359
  out = [];
320
360
  let depth = 0, start = 0;
321
361
  for (let i = 0; i <= name.length; i++) {
362
+ observeTableCells();
322
363
  const ch = name[i];
323
364
  if (ch === '[')
324
365
  depth++;
325
366
  else if (ch === ']')
326
367
  depth--;
327
368
  else if ((ch === '*' && depth === 0) || i === name.length) {
328
- out.push(name.slice(start, i));
369
+ out.push(workSlice(name, start, i, 'factor slice'));
329
370
  start = i + 1;
330
371
  }
331
372
  }
@@ -372,6 +413,7 @@ function comparisonOf(c, path) {
372
413
  // Kleene truth under the path: true, false, or undefined when open (remembered per path and condition).
373
414
  const truthMemo = new WeakMap();
374
415
  export function truth(c, path) {
416
+ startWork('ticks', 1, 'kernel truth');
375
417
  if (reuse)
376
418
  return reusing(seenOf(truthSeen, c), path, p => truthOf(c, p));
377
419
  let memo = truthMemo.get(path);
@@ -393,7 +435,7 @@ function truthOf(c, path) {
393
435
  }
394
436
  case 'and':
395
437
  case 'or': {
396
- const ts = c.xs.map(y => truth(y, path)), dominant = c.op === 'or';
438
+ const ts = workMap(c.xs, y => truth(y, path), 'truth:children'), dominant = c.op === 'or';
397
439
  return ts.includes(dominant) ? dominant : ts.every(t => t === !dominant) ? !dominant : undefined;
398
440
  }
399
441
  case '<':
@@ -435,6 +477,7 @@ export function tupleEq(c) {
435
477
  }
436
478
  // The linear facts a hypothesis gives under the path (each lin <= 0).
437
479
  export function facts(c, path, positive = true) {
480
+ startWork('ticks', 1, 'kernel facts');
438
481
  switch (c.op) {
439
482
  case 'flag': throw new Error('flags are inlined before the kernel sees them');
440
483
  case 'all': return [];
@@ -442,8 +485,8 @@ export function facts(c, path, positive = true) {
442
485
  case 'and':
443
486
  case 'or': {
444
487
  if ((c.op === 'and') === positive)
445
- return c.xs.flatMap(y => facts(y, path, positive));
446
- const open = c.xs.filter(y => truth(y, path) !== !positive);
488
+ return workFlatMap(c.xs, y => facts(y, path, positive), 'facts:conjunction');
489
+ const open = workFilter(c.xs, y => truth(y, path) !== !positive, 'facts:disjunction');
447
490
  return open.length === 0 ? [k(1)] : open.length === 1 ? facts(open[0], path, positive) : [];
448
491
  }
449
492
  case '<':
@@ -453,8 +496,8 @@ export function facts(c, path, positive = true) {
453
496
  const parts = tupleEq(c);
454
497
  if (parts) {
455
498
  if (positive)
456
- return [...facts(parts[0], path, true), ...facts(parts[1], path, true)];
457
- const open = parts.filter(y => truth(y, path) !== true);
499
+ return workConcat(facts(parts[0], path, true), facts(parts[1], path, true), 'facts:tuple');
500
+ const open = workFilter(parts, y => truth(y, path) !== true, 'facts:tuple-open');
458
501
  return open.length === 0 ? [k(1)] : open.length === 1 ? facts(open[0], path, false) : [];
459
502
  }
460
503
  const cmp = comparison(c, path);
@@ -550,40 +593,50 @@ const linearSeen = new WeakMap(), comparisonSeen = new WeakMap(), truthSeen = ne
550
593
  const seenOf = (m, k) => { let s = m.get(k); if (!s)
551
594
  m.set(k, (s = {})); return s; };
552
595
  function leafRows(goal, path, factsOf) {
553
- const rows = [...path.values()].map((a, i) => ({ ...atomOf(a.lin, a.holds), src: { atom: i } }));
596
+ const rows = [];
597
+ let atom = 0;
598
+ for (const a of path.values()) {
599
+ const lin = a.holds ? rowCopy(a.lin, 'leafRows:atom-copy') : rowAdd(rowVariable('1', 1, 'leafRows:atom-one'), rowScale(a.lin, -1, 'leafRows:atom-negate'), 'leafRows:atom-flip');
600
+ rows.push({ lin, label: '', src: { atom: atom++ } });
601
+ }
554
602
  for (const [i, h] of goal.hyps.entries())
555
603
  for (const lin of factsOf(h))
556
- rows.push({ lin, label: '', src: { fact: i } });
557
- const names = new Set(rows.flatMap(r => Object.keys(r.lin).filter(key => key !== '1')));
604
+ rows.push({ lin: rowCopy(lin, 'leafRows:fact'), label: '', src: { fact: i } });
605
+ const names = new Set();
606
+ for (const row of rows)
607
+ visitLinearCells(row.lin, key => { if (key !== '1')
608
+ names.add(key); }, 'leafRows:names');
558
609
  // Two reads of one map whose keys the path proves equal are equal.
559
- const cells = [...names].filter(isCell).map(name => ({ name, ...cellParts(name) }));
560
- for (const [i, p] of cells.entries())
561
- for (const q of cells.slice(i + 1)) {
610
+ const cells = workMap(workFilter(workSetArray(names, 'leafRows:names-copy'), isCell, 'leafRows:cell-filter'), name => ({ name, ...cellParts(name) }), 'leafRows:cell-parts');
611
+ for (let i = 0; i < cells.length; i++)
612
+ for (let j = i + 1; j < cells.length; j++) {
613
+ const p = cells[i], q = cells[j];
614
+ observeTableCells();
562
615
  if (p.map !== q.map)
563
616
  continue;
564
617
  if (!sameKey(p, q, path))
565
618
  continue;
566
- rows.push({ lin: sub(v(p.name), v(q.name)), label: '', src: { pair: [p.name, q.name] } }, { lin: sub(v(q.name), v(p.name)), label: '', src: { pair: [p.name, q.name] } });
619
+ const pRow = rowVariable(p.name, 1, 'leafRows:pair-p'), qRow = rowVariable(q.name, 1, 'leafRows:pair-q');
620
+ rows.push({ lin: rowSub(pRow, qRow, 'leafRows:pair-left'), label: '', src: { pair: [p.name, q.name] } }, { lin: rowSub(qRow, pRow, 'leafRows:pair-right'), label: '', src: { pair: [p.name, q.name] } });
567
621
  }
568
622
  // Each term column's rows, and the columns those rows bring in turn.
569
- const seen = new Set(), todo = [...names].filter(n => n.startsWith('⟦'));
623
+ const seen = new Set(), todo = workFilter(workSetArray(names, 'leafRows:todo-copy'), n => n.startsWith('⟦'), 'leafRows:todo-filter');
570
624
  while (todo.length) {
571
625
  const name = todo.pop();
572
626
  if (seen.has(name))
573
627
  continue;
574
628
  seen.add(name);
575
629
  for (const lin of termRows(name, path)) {
576
- rows.push({ lin, label: '', src: { term: name } });
577
- for (const key of Object.keys(lin))
578
- if (key !== '1') {
579
- names.add(key);
580
- if (key.startsWith('⟦') && !seen.has(key))
581
- todo.push(key);
582
- }
630
+ rows.push({ lin: rowCopy(lin, 'leafRows:term'), label: '', src: { term: name } });
631
+ visitLinearCells(lin, key => { if (key !== '1') {
632
+ names.add(key);
633
+ if (key.startsWith('⟦') && !seen.has(key))
634
+ todo.push(key);
635
+ } }, 'leafRows:term-names');
583
636
  }
584
637
  }
585
638
  for (const name of names)
586
- rows.push({ lin: { [name]: -1 }, label: '', src: { nat: name } });
639
+ rows.push({ lin: rowVariable(name, -1, 'leafRows:natural'), label: '', src: { nat: name } });
587
640
  return rows;
588
641
  }
589
642
  // A cell's map and key: M[k] a key, M[⟨k1|k2|…⟩] a tuple's parts (each a canonical text). Split at the outer level only:
@@ -591,6 +644,7 @@ function leafRows(goal, path, factsOf) {
591
644
  // (kept per name: every leaf reads its cells' keys again; a form is never changed once built)
592
645
  const cellMemo = new Map();
593
646
  export function cellParts(cell) {
647
+ observeTableCells();
594
648
  let out = cellMemo.get(cell);
595
649
  if (!out) {
596
650
  if (cellMemo.size > 200_000)
@@ -609,6 +663,7 @@ const splitOuter = (text, sep) => {
609
663
  const parts = [];
610
664
  let depth = 0, start = 0;
611
665
  for (let i = 0; i <= text.length; i++) {
666
+ observeTableCells();
612
667
  const ch = text[i];
613
668
  if (ch === '[' || ch === '⟨')
614
669
  depth++;
@@ -625,11 +680,11 @@ const splitOuter = (text, sep) => {
625
680
  const linOf = (text) => {
626
681
  const out = Object.create(null);
627
682
  for (const part of splitOuter(text, ','))
628
- if (part) {
683
+ if ((observeTableCells(part.length + 1), part)) {
629
684
  const at = part.lastIndexOf(':');
630
685
  out[part.slice(0, at)] = Number(part.slice(at + 1));
631
686
  }
632
- return out;
687
+ return completeWorkProfile.enabled ? rowCopy(out, 'linOf:output') : out;
633
688
  };
634
689
  // Two keys the path proves equal: two plain keys whose difference it decides 0 both ways, or two tuples of as many parts,
635
690
  // each pair of parts so (Lean's cellEq). A plain key and a tuple are never claimed equal.
@@ -647,15 +702,16 @@ export function withProducts(rows, pairs, squares = []) {
647
702
  const out = [...rows];
648
703
  for (const t of squares) {
649
704
  const tt = times(t, t);
650
- out.push(tt ? { lin: scale(tt, -1), label: '' } : { lin: {}, label: '' });
705
+ out.push(tt ? { lin: rowScale(tt, -1, 'withProducts:square'), label: '' } : { lin: {}, label: '' });
651
706
  }
652
707
  for (const [i, j] of pairs) {
653
708
  const p = out[i], q = out[j], pq = p && q ? times(p.lin, q.lin) : undefined;
654
- out.push(pq ? { lin: scale(pq, -1), label: '' } : { lin: {}, label: '' });
709
+ out.push(pq ? { lin: rowScale(pq, -1, 'withProducts:pair'), label: '' } : { lin: {}, label: '' });
655
710
  }
656
711
  return out;
657
712
  }
658
713
  export function check(goal, tree, path = new Map()) {
714
+ observeCheckStep();
659
715
  if ('leaf' in tree)
660
716
  return checkRefutation(withProducts(leafConstraints(goal, path), tree.products ?? [], tree.squares), tree.leaf);
661
717
  const id = canon(tree.split);
@@ -686,17 +742,24 @@ export function lemmaPath(lemmas, path) {
686
742
  const active = lemmas.filter(l => l.when.every(w => decide(w, path) === true));
687
743
  return active.length ? prependAtoms(active.map(l => ({ lin: l.cut, holds: true })), path) : path;
688
744
  }
689
- export function checkGoalLemmas(goal, tree, lemmas = []) {
690
- if (!Array.isArray(lemmas))
745
+ // checkGoalLemmas (Lean's checkGoalFrames with frames, checkGoalLemmas without): the lemma checks unchanged; each frame
746
+ // validated once by frameOk over the counted goal's counts; a leaf sees the active frames' rows in front of its lemma path.
747
+ export function checkGoalLemmas(goal, tree, lemmas = [], frames = [], counts = [], monos = [], lins = []) {
748
+ if (!Array.isArray(lemmas) || !Array.isArray(frames) || !Array.isArray(monos) || !Array.isArray(lins))
691
749
  return false;
692
- if (!lemmas.length)
750
+ if (!lemmas.length && !frames.length && !monos.length && !lins.length)
693
751
  return check(goal, tree); // Exact legacy path, including all old refusal checks.
694
752
  try {
753
+ if (!Array.from(frames).every(frameShape) || !Array.from(monos).every(monoShape) || !Array.from(lins).every(linShape))
754
+ return false;
755
+ if (!frames.every(f => frameOk(counts, f)) || !monos.every(m => monoOk(counts, m)) || !lins.every(l => linOk(counts, l)))
756
+ return false;
695
757
  if (!Array.from(lemmas).every(l => !!l && typeof l === 'object' && Array.isArray(l.when)
696
758
  && Array.from(l.when).every(lemmaLin) && lemmaLin(l.cut) && !!l.tree && typeof l.tree === 'object'
697
759
  && (l.slice === undefined || Array.isArray(l.slice) && Array.from(l.slice).every(i => Number.isSafeInteger(i) && i >= 0 && i < goal.hyps.length))))
698
760
  return false;
699
761
  for (const l of lemmas) {
762
+ observeCheckStep();
700
763
  const hyps = l.slice === undefined ? goal.hyps : l.slice.map(i => goal.hyps[i]);
701
764
  const path = prependAtoms([{ lin: l.cut, holds: false }, ...l.when.map(lin => ({ lin, holds: true }))], new Map());
702
765
  if (!check({ ...goal, hyps }, l.tree, path))
@@ -704,7 +767,8 @@ export function checkGoalLemmas(goal, tree, lemmas = []) {
704
767
  }
705
768
  const visit = (t, path) => {
706
769
  if ('leaf' in t)
707
- return check(goal, t, lemmaPath(lemmas, path));
770
+ return check(goal, t, framePath(frames, lemmas, path, monos, lins));
771
+ observeCheckStep();
708
772
  const id = canon(t.split);
709
773
  if (path.has(id) || Object.keys(t.split).every(key => key === '1'))
710
774
  return false;
@@ -717,6 +781,242 @@ export function checkGoalLemmas(goal, tree, lemmas = []) {
717
781
  return false;
718
782
  } // Malformed new payloads, including non-tree lemma references, are refusals.
719
783
  }
784
+ const mentionsKey = (t, name) => {
785
+ switch (t.op) {
786
+ case 'var': return t.name === name;
787
+ case 'const':
788
+ case 'flag': return false;
789
+ case 'add':
790
+ case 'sub':
791
+ case 'min':
792
+ case 'max':
793
+ case 'prod':
794
+ case 'tup': return mentionsKey(t.a, name) || mentionsKey(t.b, name);
795
+ case 'mul':
796
+ case 'proj': return mentionsKey(t.a, name);
797
+ case 'if': return mentionsKey(t.c, name) || mentionsKey(t.then, name) || mentionsKey(t.else, name);
798
+ case 'get': return mentionsKey(t.key, name);
799
+ case '<':
800
+ case '<=':
801
+ case '=': return mentionsKey(t.a, name) || mentionsKey(t.b, name);
802
+ case 'and':
803
+ case 'or': return t.xs.some(x => mentionsKey(x, name));
804
+ case 'not': return mentionsKey(t.x, name);
805
+ case 'all': return mentionsKey(t.c, name);
806
+ default: return unreachable(t);
807
+ }
808
+ };
809
+ // Lean's monoE: a term that only rises with any variable (variables, constants, sums, scalings, products, minima, maxima).
810
+ const monoE = (e) => e.op === 'var' || e.op === 'const' || ((e.op === 'add' || e.op === 'min' || e.op === 'max' || e.op === 'prod') && monoE(e.a) && monoE(e.b)) || (e.op === 'mul' && monoE(e.a));
811
+ // Lean's zeroK: the key replaced by 0.
812
+ const zeroK = (e, key) => {
813
+ switch (e.op) {
814
+ case 'var': return e.name === key ? { op: 'const', value: 0 } : e;
815
+ case 'const': return e;
816
+ case 'add':
817
+ case 'sub':
818
+ case 'min':
819
+ case 'max':
820
+ case 'prod':
821
+ case 'tup': return { op: e.op, a: zeroK(e.a, key), b: zeroK(e.b, key) };
822
+ case 'mul': return { op: 'mul', k: e.k, a: zeroK(e.a, key) };
823
+ case 'proj': return { op: 'proj', k: e.k, a: zeroK(e.a, key) };
824
+ case 'get': return { op: 'get', map: e.map, key: zeroK(e.key, key) };
825
+ case 'if': return { op: 'if', c: e.c, then: zeroK(e.then, key), else: zeroK(e.else, key) };
826
+ default: return unreachable(e);
827
+ }
828
+ };
829
+ // Lean's atomK: an atom reading the key, decided for every value of the key from its key-free instance at 0 — the key
830
+ // only on the left and the left rising with it, false at 0 is false everywhere; only on the right, true at 0 is true
831
+ // everywhere.
832
+ const atomK = (op, a, b, path, key) => {
833
+ if (mentionsKey(a, key) && !mentionsKey(b, key) && monoE(a))
834
+ return truth({ op, a: zeroK(a, key), b }, path) === false ? false : undefined;
835
+ if (!mentionsKey(a, key) && mentionsKey(b, key) && monoE(b))
836
+ return truth({ op, a, b: zeroK(b, key) }, path) === true ? true : undefined;
837
+ return undefined;
838
+ };
839
+ // Lean's truthK: atoms reading no key by truth, atoms reading it by atomK, equalities through their inequalities,
840
+ // and / or / not three-valued.
841
+ export function truthK(c, path, key) {
842
+ switch (c.op) {
843
+ case 'and': {
844
+ let open = false;
845
+ for (const x of c.xs) {
846
+ const t = truthK(x, path, key);
847
+ if (t === false)
848
+ return false;
849
+ if (t === undefined)
850
+ open = true;
851
+ }
852
+ return open ? undefined : true;
853
+ }
854
+ case 'or': {
855
+ let open = false;
856
+ for (const x of c.xs) {
857
+ const t = truthK(x, path, key);
858
+ if (t === true)
859
+ return true;
860
+ if (t === undefined)
861
+ open = true;
862
+ }
863
+ return open ? undefined : false;
864
+ }
865
+ case 'not': {
866
+ const t = truthK(c.x, path, key);
867
+ return t === undefined ? undefined : !t;
868
+ }
869
+ case 'flag':
870
+ case 'all': return undefined;
871
+ case '<':
872
+ case '<=': return mentionsKey(c, key) ? atomK(c.op, c.a, c.b, path, key) : truth(c, path);
873
+ case '=': return !mentionsKey(c, key) ? truth(c, path)
874
+ : atomK('<=', c.a, c.b, path, key) === false || atomK('<=', c.b, c.a, path, key) === false ? false : undefined;
875
+ default: return unreachable(c);
876
+ }
877
+ }
878
+ // Lean's cTrue (0 ≤ 0) and cFalse (0 < 0), as the kernel's n-ary conditions: the empty conjunction and disjunction.
879
+ const cTrue = { op: 'and', xs: [] }, cFalse = { op: 'or', xs: [] };
880
+ const isTrueC = (c) => c.op === 'and' && c.xs.length === 0, isFalseC = (c) => c.op === 'or' && c.xs.length === 0;
881
+ const zeroOneIf = (e) => e.op === 'if' && e.then.op === 'const' && e.then.value === 1 && e.else.op === 'const' && e.else.value === 0 ? e.c : undefined;
882
+ // Lean's simpC: the terms inside a condition simplified; a conjunct decided true dropped, one decided false collapsing
883
+ // the conjunction, dually for a disjunction and a negation; `(if C then 1 else 0) = 1` is C and `= 0` is ¬C; an atom
884
+ // decided for every value of the key by truthK becomes cTrue or cFalse.
885
+ export function simpC(c, path, key) {
886
+ switch (c.op) {
887
+ case '<':
888
+ case '<=':
889
+ case '=': {
890
+ const a = simpE(c.a, path, key), b = simpE(c.b, path, key);
891
+ if (c.op === '=') {
892
+ const ca = zeroOneIf(a), cb = zeroOneIf(b);
893
+ if (ca && b.op === 'const' && (b.value === 1 || b.value === 0))
894
+ return b.value === 1 ? ca : notC(ca);
895
+ if (cb && a.op === 'const' && (a.value === 1 || a.value === 0))
896
+ return a.value === 1 ? cb : notC(cb);
897
+ }
898
+ const out = { op: c.op, a, b }, t = truthK(out, path, key);
899
+ return t === true ? cTrue : t === false ? cFalse : out;
900
+ }
901
+ case 'and': {
902
+ const xs = [];
903
+ for (const x of c.xs) {
904
+ const y = simpC(x, path, key);
905
+ if (isFalseC(y))
906
+ return cFalse;
907
+ if (!isTrueC(y))
908
+ xs.push(...(y.op === 'and' ? y.xs : [y]));
909
+ }
910
+ return xs.length === 1 ? xs[0] : { op: 'and', xs };
911
+ }
912
+ case 'or': {
913
+ const xs = [];
914
+ for (const x of c.xs) {
915
+ const y = simpC(x, path, key);
916
+ if (isTrueC(y))
917
+ return cTrue;
918
+ if (!isFalseC(y))
919
+ xs.push(...(y.op === 'or' ? y.xs : [y]));
920
+ }
921
+ return xs.length === 1 ? xs[0] : { op: 'or', xs };
922
+ }
923
+ case 'not': return notC(simpC(c.x, path, key));
924
+ case 'flag':
925
+ case 'all': return c;
926
+ default: return unreachable(c);
927
+ }
928
+ }
929
+ const notC = (y) => isTrueC(y) ? cFalse : isFalseC(y) ? cTrue : y.op === 'not' ? y.x : { op: 'not', x: y };
930
+ // Lean's simpE: each `if` whose condition is decided for every value of the key under the path, resolved; nothing else.
931
+ export function simpE(e, path, key) {
932
+ switch (e.op) {
933
+ case 'var':
934
+ case 'const': return e;
935
+ case 'add':
936
+ case 'sub':
937
+ case 'min':
938
+ case 'max':
939
+ case 'prod':
940
+ case 'tup': return { op: e.op, a: simpE(e.a, path, key), b: simpE(e.b, path, key) };
941
+ case 'mul': return { op: 'mul', k: e.k, a: simpE(e.a, path, key) };
942
+ case 'proj': return { op: 'proj', k: e.k, a: simpE(e.a, path, key) };
943
+ case 'get': return { op: 'get', map: e.map, key: simpE(e.key, path, key) };
944
+ case 'if': {
945
+ const t = simpE(e.then, path, key), f = simpE(e.else, path, key), c = simpC(e.c, path, key);
946
+ return isTrueC(c) ? t : isFalseC(c) ? f : { op: 'if', c, then: t, else: f };
947
+ }
948
+ default: return unreachable(e);
949
+ }
950
+ }
951
+ export const whenPath = (f) => new Map(f.when.map(w => [canon(w), { lin: w, holds: true }]));
952
+ const sameTerm = (a, b) => JSON.stringify(a) === JSON.stringify(b);
953
+ // Lean's frameOk: the two counts share their key, and under `when` their bounds and bodies are the same terms.
954
+ export function frameOk(counts, f) {
955
+ const post = counts.find(c => c.v === f.post), pre = counts.find(c => c.v === f.pre);
956
+ if (!post || !pre || post.key !== pre.key)
957
+ return false;
958
+ const path = whenPath(f);
959
+ return sameTerm(simpE(post.below, path, post.key), simpE(pre.below, path, post.key)) && sameTerm(simpE(post.body, path, post.key), simpE(pre.body, path, post.key));
960
+ }
961
+ // Lean's frameRows: post − pre ≤ 0 and pre − post ≤ 0.
962
+ export const frameRows = (f) => [{ [f.post]: 1, [f.pre]: -1 }, { [f.pre]: 1, [f.post]: -1 }];
963
+ // Lean's frameActive: eligibility from the original split path only.
964
+ export const frameActive = (f, path) => f.when.every(w => decide(w, path) === true);
965
+ // Lean's framePath: the active frames' rows in front of the lemma-cut path, both read off the original path.
966
+ export function framePath(frames, lemmas, path, monos = [], lins = []) {
967
+ const active = frames.filter(f => frameActive(f, path)), cuts = monos.filter(m => monoActive(m, path)), linear = lins.filter(l => linActive(l, path));
968
+ return prependAtoms([...linear.flatMap(l => linRows(l).map(lin => ({ lin, holds: true }))), ...cuts.flatMap(m => monoRows(m).map(lin => ({ lin, holds: true }))), ...active.flatMap(f => frameRows(f).map(lin => ({ lin, holds: true })))], lemmaPath(lemmas, path));
969
+ }
970
+ const monoHyps = (m, post, pre) => [...m.when, { op: '<', a: { op: 'var', name: post.key }, b: post.below }, { op: '<', a: pre.body, b: post.body }];
971
+ export function monoOk(counts, m) {
972
+ const post = counts.find(c => c.v === m.post), pre = counts.find(c => c.v === m.pre);
973
+ if (!post || !pre || post.key !== pre.key || !sameTerm(post.below, pre.below) || mentionsKey(post.below, post.key) || m.when.some(c => mentionsKey(c, post.key)))
974
+ return false;
975
+ return check({ name: 'per-key cut', hyps: monoHyps(m, post, pre) }, m.tree);
976
+ }
977
+ export const monoRows = (m) => [{ [m.post]: 1, [m.pre]: -1 }];
978
+ export const monoActive = (m, path) => m.when.every(c => truth(c, path) === true);
979
+ const monoShape = (m) => !!m && typeof m === 'object' && Array.isArray(m.when) && typeof m.post === 'string'
980
+ && typeof m.pre === 'string' && !!m.tree && typeof m.tree === 'object';
981
+ const linCounts = (counts, side) => {
982
+ const out = [];
983
+ let total = 0;
984
+ for (const [m, v] of side) {
985
+ const c = counts.find(k => k.v === v);
986
+ // A stricter native refusal than Lean: row accumulation must stay exact in JS.
987
+ if (!c || !Number.isSafeInteger(m) || m < 0 || m > Number.MAX_SAFE_INTEGER - total)
988
+ return undefined;
989
+ total += m;
990
+ out.push([m, c]);
991
+ }
992
+ return out;
993
+ };
994
+ const linTerm = (xs) => xs.reduceRight((rest, [m, c]) => ({ op: 'add', a: { op: 'mul', k: m, a: c.body }, b: rest }), { op: 'const', value: 0 });
995
+ const linSide = (side, sign) => {
996
+ const out = Object.create(null);
997
+ for (let i = 0; i < side.length; i++) {
998
+ startWork('tableCells', 1, 'linear cut input');
999
+ const [m, v] = side[i];
1000
+ out[v] = (out[v] ?? 0) + sign * m;
1001
+ }
1002
+ return completeWorkProfile.enabled ? rowCopy(out, 'linear cut side') : out;
1003
+ };
1004
+ export function linOk(counts, l) {
1005
+ const ps = linCounts(counts, l.pos), ns = linCounts(counts, l.neg);
1006
+ if (!ps || !ns || !ps.length && !ns.length)
1007
+ return false;
1008
+ const c0 = (ps[0] ?? ns[0])[1];
1009
+ const shape = (xs) => xs.every(([, c]) => c.key === c0.key && sameTerm(c.below, c0.below));
1010
+ if (!shape(ps) || !shape(ns) || mentionsKey(c0.below, c0.key) || l.when.some(c => mentionsKey(c, c0.key)))
1011
+ return false;
1012
+ return check({ name: 'linear cut', hyps: [...l.when, { op: '<', a: { op: 'var', name: c0.key }, b: c0.below }, { op: '<', a: linTerm(ns), b: linTerm(ps) }] }, l.tree);
1013
+ }
1014
+ export const linRows = (l) => [add(linSide(l.pos, 1), linSide(l.neg, -1))];
1015
+ export const linActive = (l, path) => l.when.every(c => truth(c, path) === true);
1016
+ const linShape = (l) => !!l && typeof l === 'object' && Array.isArray(l.when) && Array.isArray(l.pos) && Array.isArray(l.neg)
1017
+ && [...l.pos, ...l.neg].every(p => Array.isArray(p) && Number.isSafeInteger(p[0]) && p[0] >= 0 && typeof p[1] === 'string') && !!l.tree && typeof l.tree === 'object';
1018
+ const frameShape = (f) => !!f && typeof f === 'object' && Array.isArray(f.when) && Array.from(f.when).every(lemmaLin)
1019
+ && typeof f.post === 'string' && typeof f.pre === 'string';
720
1020
  // --- machines ----------------------------------------------------------------------------------------------------------
721
1021
  // Every constant and factor is a natural, so every value a machine computes is one (∸ truncates).
722
1022
  function natural(e) {
@@ -1002,23 +1302,27 @@ export function scope(m, laws = []) {
1002
1302
  // read only as a map's key (so every key the maps hold nothing at reads alike); a quantifier is a whole flag, nowhere
1003
1303
  // else.
1004
1304
  const readable = [...maps, ...carried, ...fns];
1005
- const bound = (what, key, t) => {
1305
+ const bound = (what, key, t, finite = false) => {
1006
1306
  plainName(what, key);
1007
1307
  if ([...names, ...readable, ...flags].includes(key))
1008
1308
  throw new Error(`${what}: the key ${key} is already a name of ${m.name}`);
1009
1309
  // (a count map, like a function, has a value at every key, not cells a runtime can list: read at no key term)
1010
- keyOnly(what, t, key, [...fns, ...counts]);
1310
+ if (!finite)
1311
+ keyOnly(what, t, key, [...fns, ...counts]);
1011
1312
  };
1012
1313
  inScope(`${m.name}'s start`, Object.values(m.state), m.params);
1013
1314
  flags.forEach((f, i) => {
1014
1315
  const c = m.step.flags[f], what = `${m.name}'s flag ${f}`;
1015
1316
  // A quantified flag's body reads no flag (the twin decides quantified flags before the plain ones).
1016
1317
  if (c.op === 'all') {
1017
- const keys = boundOf(c), body = crossFree(c.c, keys);
1318
+ const finite = finiteBound(c), keys = boundOf(c), body = crossFree(c.c, keys);
1319
+ if (finite)
1320
+ inScope(`${what}'s finite size`, [finite], m.params);
1018
1321
  distinct(what, keys);
1019
1322
  for (const key of keys) {
1020
- bound(what, key, body);
1021
- apart(what, body, key, keys);
1323
+ bound(what, key, body, finite !== undefined);
1324
+ if (!finite)
1325
+ apart(what, body, key, keys);
1022
1326
  }
1023
1327
  unquantified(what, c.c);
1024
1328
  inScope(what, [c.c], [...names, ...keys], readable, []);
@@ -1031,6 +1335,8 @@ export function scope(m, laws = []) {
1031
1335
  const arity = mapArities(m, laws);
1032
1336
  for (const [map, w] of Object.entries(m.step.maps ?? {})) {
1033
1337
  const what = `${m.name}'s write to ${map}`;
1338
+ if ('finite' in w)
1339
+ throw new Error(`${what}: a finite marker is only for quantifiers, not writes at every key`);
1034
1340
  // At every key of a tuple-keyed map, the key is read as its parts (proj k, or the key whole at a read of the map):
1035
1341
  // the runtimes compute the write at every tuple of parts, each part read as a bound key is.
1036
1342
  if ('each' in w && (own(arity, map) ?? 1) > 1) {
@@ -1079,6 +1385,9 @@ export function scope(m, laws = []) {
1079
1385
  throw new Error(`${what}: further keys come with a first, each a different name`);
1080
1386
  if (law.name.startsWith('?'))
1081
1387
  throw new Error(`${what}: a name starting with ? is a flag's (its instances)`);
1388
+ const finite = finiteLawBound(law);
1389
+ if (finite)
1390
+ inScope(`${what}'s finite size`, [finite], m.params);
1082
1391
  unquantified(what, law.holds);
1083
1392
  }
1084
1393
  const keysOf = (l) => [...(l.forall ? [l.forall] : []), ...(l.keys ?? [])];
@@ -1167,6 +1476,33 @@ const tupleSides = (u) => {
1167
1476
  const as = tupleParts(u.a), bs = tupleParts(u.b);
1168
1477
  return as.length === bs.length ? as.map((a, i) => [a, bs[i]]) : undefined;
1169
1478
  };
1479
+ // A marker is a request for exact finite enumeration, never a semantic axiom.
1480
+ // The guard remains in the formula sent to closing/assumed and the Lean twin.
1481
+ // Bounded.lean proves this exact shape equivalent to enumeration, including n=0.
1482
+ export function finiteBound(q) {
1483
+ if (q.finite === undefined)
1484
+ return undefined;
1485
+ const bad = () => { throw new Error('a finite quantifier requires one key and the exact guard k < size implies body'); };
1486
+ if (typeof q.finite !== 'string' || !q.finite.length || q.keys?.length)
1487
+ return bad();
1488
+ const c = q.c;
1489
+ if (c.op !== 'or' || c.xs.length !== 2)
1490
+ return bad();
1491
+ const guard = c.xs[0];
1492
+ if (guard.op !== 'not' || guard.x.op !== '<' || guard.x.a.op !== 'var' || guard.x.a.name !== q.key)
1493
+ return bad();
1494
+ if (readsKey(guard.x.b, q.key))
1495
+ return bad();
1496
+ return guard.x.b;
1497
+ }
1498
+ // Prenexing a finite law removes its quantifier, but not its guard or runtime domain.
1499
+ export function finiteLawBound(law) {
1500
+ if (law.finite === undefined)
1501
+ return undefined;
1502
+ if (!law.forall || law.some?.length || law.ex)
1503
+ throw new Error('a finite law requires one universal key and no existential witness metadata');
1504
+ return finiteBound({ op: 'all', key: law.forall, ...(law.keys ? { keys: law.keys } : {}), c: law.holds, finite: law.finite });
1505
+ }
1170
1506
  export const keyTerm = (e, key) => e.op === 'var' ? e.name === key
1171
1507
  : e.op === 'add' ? (e.a.op === 'var' && e.a.name === key && !readsKey(e.b, key)) || (e.b.op === 'var' && e.b.name === key && !readsKey(e.a, key))
1172
1508
  : e.op === 'sub' && e.a.op === 'var' && e.a.name === key && !readsKey(e.b, key);
@@ -2059,26 +2395,35 @@ export const qdif = (a, b) => radd(rscale(b.den, a.row), rscale(-a.den, b.row));
2059
2395
  // never wrong); and its key, as `canon` writes a Lin (zero coefficients dropped: Lean's `same` compares by value).
2060
2396
  export const qLin = (r) => {
2061
2397
  const out = Object.create(null);
2062
- for (const [x, c] of r.cs) {
2398
+ const iterator = r.cs.entries(), size = r.cs.size;
2399
+ for (let i = 0; i < size; i++) {
2400
+ startWork('tableCells', 1, 'qLin input');
2401
+ const entry = iterator.next();
2402
+ if (entry.done)
2403
+ throw new Error('rational row changed during traversal');
2404
+ const [x, c] = entry.value;
2063
2405
  if (c < -(2n ** 53n) + 1n || c > 2n ** 53n - 1n)
2064
2406
  return undefined;
2065
2407
  out[x] = Number(c);
2066
2408
  }
2409
+ startWork('tableCells', 1, 'qLin constant');
2067
2410
  if (r.c !== 0n) {
2068
2411
  if (r.c < -(2n ** 53n) + 1n || r.c > 2n ** 53n - 1n)
2069
2412
  return undefined;
2070
2413
  out['1'] = Number(r.c);
2071
2414
  }
2072
- return out;
2415
+ return completeWorkProfile.enabled ? rowCopy(out, 'qLin output') : out;
2073
2416
  };
2074
2417
  const linRow = (lin) => {
2075
- if (!Object.values(lin).every(c => Number.isSafeInteger(c)))
2418
+ if (!everyLinearCell(lin, (_key, c) => Number.isSafeInteger(c), 'linRow safety'))
2076
2419
  return undefined;
2077
2420
  const cs = new Map();
2078
- for (const [x, c] of Object.entries(lin))
2079
- if (x !== '1' && c !== 0)
2080
- cs.set(x, BigInt(c));
2081
- return { cs, c: BigInt(lin['1'] ?? 0) };
2421
+ let constant = 0n;
2422
+ visitLinearCells(lin, (x, c) => { if (x === '1')
2423
+ constant = BigInt(c);
2424
+ else if (c !== 0)
2425
+ cs.set(x, BigInt(c)); }, 'linRow conversion');
2426
+ return { cs, c: constant };
2082
2427
  };
2083
2428
  const rowKey = (r) => { const lin = qLin(r); return lin && canon(lin); };
2084
2429
  export function qdec(path, r) {
@@ -2189,10 +2534,11 @@ export function qfacts(c, path, pos = true) {
2189
2534
  export function qLeafRows(goal, path) {
2190
2535
  const rows = [...[...path.values()].map(a => (a.holds ? [linRow(a.lin), false] : [rscale(-1n, linRow(a.lin)), true])),
2191
2536
  ...goal.hyps.flatMap(h => qfacts(h, path))];
2192
- return rows.flatMap(([r, strict]) => { const lin = qLin(r); return lin ? [{ lin, label: '', strict }] : []; });
2537
+ return rows.flatMap(([r, strict]) => { const lin = qLin(r); observeTableCells(lin ? Object.keys(lin).length : 0); return lin ? [{ lin, label: '', strict }] : []; });
2193
2538
  }
2194
2539
  // A split on a row the path decides already (or a constant one) is refused, as Lean's checkT refuses it.
2195
2540
  export function checkQTree(goal, tree, path = new Map()) {
2541
+ observeCheckStep();
2196
2542
  if ('leaf' in tree)
2197
2543
  return !tree.products?.length && !tree.squares?.length && checkRefutation(qLeafRows(goal, path), tree.leaf, false);
2198
2544
  const row = linRow(tree.split), lin = row && qLin(row);
@@ -2324,10 +2670,11 @@ export function countedOk(hyps, cs, is) {
2324
2670
  // (Lean's are Int).
2325
2671
  const typed = (is) => is.every(i => typeof i.wit === 'string' && Number.isSafeInteger(i.unit) && i.terms.every(([z]) => Number.isSafeInteger(z)));
2326
2672
  export function checkCounted(g, cert) {
2673
+ observeCheckStep();
2327
2674
  if (!(countedOk(g.hyps, g.counts, cert.insts) && typed(cert.insts)))
2328
2675
  return false;
2329
2676
  const goal = countedGoal(g, cert.insts);
2330
- return goal !== undefined && checkGoalLemmas(goal, cert.tree, cert.lemmas);
2677
+ return goal !== undefined && checkGoalLemmas(goal, cert.tree, cert.lemmas, cert.frames, g.counts, cert.monos, cert.lins);
2331
2678
  }
2332
2679
  // The goal a certificate's tree refutes: the goal's hypotheses, then the instances' (also what the search and the
2333
2680
  // exporter read), or none.
@@ -2457,21 +2804,23 @@ const readsTyped = (rs) => Array.isArray(rs) && rs.every(r => Array.isArray(r) &
2457
2804
  const witFreshW = (cs, is, w) => cs.every(c => c.v !== w && !readsVar(c.below, w) && !readsVar(c.body, w)) && is.every(j => !readsVar(j.below, w));
2458
2805
  const countedOkW = (cs, is) => { const ws = is.map(i => i.wit); return new Set(ws).size === ws.length && is.every(i => witFreshW(cs, is, i.wit)); };
2459
2806
  function checkCountedW(hyps, cs, cert) {
2807
+ observeCheckStep();
2460
2808
  if (!(countedOkW(cs, cert.insts) && typed(cert.insts)))
2461
2809
  return false;
2462
2810
  const hs = countHyps(cs, cert.insts);
2463
- return hs !== undefined && checkGoalLemmas({ name: 'counted', hyps: [...hyps, ...hs] }, cert.tree, cert.lemmas);
2811
+ return hs !== undefined && checkGoalLemmas({ name: 'counted', hyps: [...hyps, ...hs] }, cert.tree, cert.lemmas, cert.frames, cs, cert.monos, cert.lins);
2464
2812
  }
2465
2813
  // checkMC: each declaration's j and lo differ from its key, the reads are fresh, no read is a witness or reads one in
2466
2814
  // its key term, and checkCountedW accepts the counted goal they make (the goal's hypotheses, each read variable equal to
2467
2815
  // its cell; Lean's names the hypotheses it uses, each the goal's).
2468
2816
  export function checkMC(hyps, ds, k) {
2817
+ observeCheckStep();
2469
2818
  if (!(ds.every(d => d.j !== d.key && loOf(d) !== d.key) && picksTyped(k.picks) && readsTyped(k.reads) && Array.isArray(k.insts) && k.insts.every(i => !!i && safeTerm(i.below)) && readsOk(hyps, ds, k.reads)))
2470
2819
  return false;
2471
2820
  if (!k.reads.every(r => k.insts.every(i => i.wit !== r[0] && !readsVar(r[2], i.wit))))
2472
2821
  return false;
2473
2822
  const cs = toCounts(ds, k.reads);
2474
- return cs !== undefined && checkCountedW([...hyps, ...k.reads.map(r => readHyp(ds, r)), ...pickHyps(ds, k.reads, k.picks ?? [])], cs, { insts: k.insts, tree: k.tree, lemmas: k.lemmas });
2823
+ return cs !== undefined && checkCountedW([...hyps, ...k.reads.map(r => readHyp(ds, r)), ...pickHyps(ds, k.reads, k.picks ?? [])], cs, { insts: k.insts, tree: k.tree, lemmas: k.lemmas, frames: k.frames, monos: k.monos, lins: k.lins });
2475
2824
  }
2476
2825
  // The goal a certificate's tree refutes (what the search and the exporter read), or none.
2477
2826
  export function mcGoal(hyps, ds, k) {