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
@@ -0,0 +1,10 @@
1
+ import { type C, type E } from './machine.js';
2
+ import { type Lin } from './solver.js';
3
+ import { type CountCert, type Tree } from '../kernel/kernel.js';
4
+ export declare function countCertificateRenamer(names: ReadonlyMap<string, string>, until: number): {
5
+ expression: (e: E) => E;
6
+ condition: (c: C) => C;
7
+ row: (source: Lin) => Lin;
8
+ tree: (source: Tree) => Tree;
9
+ certificate: (source: CountCert) => CountCert;
10
+ };
@@ -0,0 +1,69 @@
1
+ // Alpha-renaming proposals for counted certificates. This is not a checker:
2
+ // the caller must reindex and check the complete resulting certificate.
3
+ import { x } from "./machine.js";
4
+ import { canon, cellParts, columnTerm, factorsOf, isCell, substC, substE, termColumn } from "../kernel/kernel.js";
5
+ import { late } from "../kernel/prove.js";
6
+ export function countCertificateRenamer(names, until) {
7
+ const vars = Object.fromEntries([...names].map(([before, after]) => [before, x(after)])), sub = { vars, maps: {}, flags: {} };
8
+ const alive = () => { if (late(until))
9
+ throw Error('count certificate rename deadline'); };
10
+ const expression = (e) => { alive(); return substE(e, sub); };
11
+ const condition = (c) => { alive(); return substC(c, sub); };
12
+ const columns = new Map();
13
+ const column = (name) => {
14
+ alive();
15
+ const cached = columns.get(name);
16
+ if (cached !== undefined)
17
+ return cached;
18
+ const factors = factorsOf(name);
19
+ let out;
20
+ if (factors.length > 1)
21
+ out = factors.map(column).sort().join('*');
22
+ else if (isCell(name)) {
23
+ const parts = cellParts(name);
24
+ out = parts.keys ? `${parts.map}[⟨${parts.keys.map(key => canon(row(key))).join('|')}⟩]` : `${parts.map}[${canon(row(parts.key))}]`;
25
+ }
26
+ else if (name.startsWith('⟦')) {
27
+ const term = columnTerm(name);
28
+ if (!term)
29
+ throw Error('unknown term column');
30
+ out = termColumn(expression(term));
31
+ }
32
+ else
33
+ out = names.get(name) ?? name;
34
+ columns.set(name, out);
35
+ return out;
36
+ };
37
+ const row = (source) => {
38
+ alive();
39
+ const out = Object.create(null);
40
+ for (const [name, coefficient] of Object.entries(source)) {
41
+ const key = name === '1' ? name : column(name), value = (out[key] ?? 0) + coefficient;
42
+ if (!Number.isSafeInteger(value))
43
+ throw Error('inexact renamed row');
44
+ // Existing zero coefficients are part of the source path identity and
45
+ // hence its proof-row layout. Do not canonicalize them while renaming.
46
+ if (value || coefficient === 0)
47
+ out[key] = value;
48
+ else
49
+ delete out[key];
50
+ }
51
+ return out;
52
+ };
53
+ const tree = (source) => {
54
+ alive();
55
+ return 'split' in source ? { split: row(source.split), yes: tree(source.yes), no: tree(source.no) } : { leaf: source.leaf.map(step => ({ ...step })), ...(source.products ? { products: source.products.map(([a, b]) => [a, b]) } : {}), ...(source.squares ? { squares: source.squares.map(row) } : {}) };
56
+ };
57
+ const name = (id) => names.get(id) ?? id;
58
+ const certificate = (source) => ({
59
+ reads: source.reads.map(([id, map, key]) => [name(id), map, expression(key)]),
60
+ insts: source.insts.map(i => ({ ...i, wit: name(i.wit), below: expression(i.below), terms: i.terms.map(([k, id]) => [k, name(id)]) })),
61
+ tree: tree(source.tree),
62
+ ...(source.picks ? { picks: source.picks.map(([id, keys]) => [name(id), keys.map(expression)]) } : {}),
63
+ ...(source.frames ? { frames: source.frames.map(f => ({ post: name(f.post), pre: name(f.pre), when: f.when.map(row) })) } : {}),
64
+ ...(source.monos ? { monos: source.monos.map(m => ({ ...m, post: name(m.post), pre: name(m.pre), when: m.when.map(condition), tree: tree(m.tree) })) } : {}),
65
+ ...(source.lins ? { lins: source.lins.map(l => ({ when: l.when.map(condition), pos: l.pos.map(([k, id]) => [k, name(id)]), neg: l.neg.map(([k, id]) => [k, name(id)]), tree: tree(l.tree) })) } : {}),
66
+ ...(source.lemmas ? { lemmas: source.lemmas.map(l => ({ ...l, when: l.when.map(row), cut: row(l.cut), tree: tree(l.tree) })) } : {})
67
+ });
68
+ return { expression, condition, row, tree, certificate };
69
+ }
@@ -1,3 +1,11 @@
1
- import { type CountCert, type Goal } from '../kernel/kernel.js';
2
- export declare function transportCountCertificate(from: Goal, to: Goal, cert: CountCert, until: number): CountCert | undefined;
1
+ import { type CountCert, type CountDecl, type Goal } from '../kernel/kernel.js';
2
+ import { type ConditionalTransportOptions } from './count-recovery/conditional-proof.js';
3
+ export type CountTransportOptions = ConditionalTransportOptions & Readonly<{
4
+ declarations?: {
5
+ from: CountDecl[];
6
+ to: CountDecl[];
7
+ };
8
+ }>;
9
+ export declare function transportCountCertificate(from: Goal, to: Goal, cert: CountCert, until: number, options?: CountTransportOptions): CountCert | undefined;
10
+ export declare function replayCountCertificate(from: Goal, to: Goal, cert: CountCert, until: number, options?: ConditionalTransportOptions): CountCert | undefined;
3
11
  export declare function countedCertificateHyps(goal: Goal, cert: CountCert): Set<number> | undefined;
@@ -1,21 +1,68 @@
1
+ import { searchNow as countedSearchNow, WallExpired } from "./wall.js";
1
2
  // Untrusted integration helpers for complete counted certificates. The kernel
2
3
  // still checks the emitted ordinary tree and independent conditional cuts.
3
- import { canon, lemmaPath } from "../kernel/kernel.js";
4
- import { usedHyps, widen } from "../kernel/prove.js";
4
+ import { canon, framePath, lemmaPath } from "../kernel/kernel.js";
5
+ import { BudgetExpired, usedHyps, widen } from "../kernel/prove.js";
5
6
  import { collectConditionalCertificate, markConditionalProof, transportConditionalProof } from "./count-recovery/conditional-proof.js";
6
- export function transportCountCertificate(from, to, cert, until) {
7
- if (performance.now() >= until)
7
+ import { emitReplayDiagnostic, replayCertificateId, withReplayDiagnosticContext } from "./count-replay-diagnostic.js";
8
+ import { workLedger } from "./work-admission.js";
9
+ import { countsForReads } from "./count-facts.js";
10
+ export function transportCountCertificate(from, to, cert, until, options) {
11
+ if (countedSearchNow() >= until)
8
12
  return undefined;
13
+ if (cert.frames?.length || cert.monos?.length || cert.lins?.length) {
14
+ // Derive counts only for cut-bearing certificates, from the caller's exact
15
+ // source/target declarations and the original read header. No empty fallback.
16
+ const declarations = options?.declarations;
17
+ const source = declarations && countsForReads(declarations.from, cert.reads), target = declarations && countsForReads(declarations.to, cert.reads);
18
+ const native = declarations ? (source && target ? { from: { counts: source }, to: { counts: target } } : undefined) : options?.native;
19
+ return replayCountCertificate(from, to, cert, until, { ...options, native });
20
+ }
9
21
  if (!cert.lemmas?.length) {
10
22
  const tree = widen(from, to, cert.tree);
11
- return tree && performance.now() < until ? { ...cert, tree } : undefined;
23
+ return tree && countedSearchNow() < until ? { ...cert, tree } : undefined;
12
24
  }
13
25
  const marked = markConditionalProof(from, cert.tree, cert.lemmas), tree = transportConditionalProof(from, to, marked, new Map(), new Map(), until);
14
26
  const combined = tree && collectConditionalCertificate(to, tree, until);
15
27
  return combined ? { ...cert, ...combined } : undefined;
16
28
  }
29
+ // Across-round retention must never repair a missing premise by searching.
30
+ // The exact conditional transporter also handles ordinary trees with no cuts.
31
+ export function replayCountCertificate(from, to, cert, until, options) {
32
+ let detail;
33
+ const notify = (value) => { detail ??= value; try {
34
+ options?.onDecline?.(value);
35
+ }
36
+ catch { /* diagnostic callback only */ } };
37
+ const replay = () => {
38
+ if (countedSearchNow() >= until) {
39
+ notify({ stage: "transport", part: "main", reason: "deadline" });
40
+ return undefined;
41
+ }
42
+ const cutBearing = !!(cert.frames?.length || cert.monos?.length || cert.lins?.length);
43
+ if (cutBearing && !options?.native) {
44
+ notify({ stage: 'cut-validation', part: 'main', reason: 'missing-native-count-context' });
45
+ return undefined;
46
+ }
47
+ // Cut arrays remain the original certificate's, not caller-supplied assumptions.
48
+ const native = options?.native && { from: { ...options.native.from, frames: cert.frames, monos: cert.monos, lins: cert.lins }, to: { ...options.native.to, frames: cert.frames, monos: cert.monos, lins: cert.lins } };
49
+ const context = { ...options, native, onDecline: notify };
50
+ const marked = markConditionalProof(from, cert.tree, cert.lemmas ?? []);
51
+ const tree = transportConditionalProof(from, to, marked, new Map(), new Map(), until, context);
52
+ const combined = tree && collectConditionalCertificate(to, tree, until, context);
53
+ return combined ? { ...cert, ...combined } : undefined;
54
+ };
55
+ // The original error and its ledger-authenticated endpoint provenance survive
56
+ // nested pre-check/kernel catches; legacy callers retain their old boundary.
57
+ return withReplayDiagnosticContext(options?.diagnostic ?? {}, () => {
58
+ const result = options?.rethrowBudgetExpired ? workLedger.withCapacityRefusalBoundary(replay) : replay();
59
+ if (!result)
60
+ emitReplayDiagnostic({ kind: 'certificate-decline', certificateId: replayCertificateId(cert), cutCounts: { frames: cert.frames?.length ?? 0, lemmas: cert.lemmas?.length ?? 0, monos: cert.monos?.length ?? 0, lins: cert.lins?.length ?? 0 }, prefix: { source: detail?.stage === 'cut-validation' ? 'not-built' : options?.native ? 'framePath' : 'lemmaPath', target: detail?.stage === 'cut-validation' ? 'not-built' : options?.native ? 'framePath' : 'lemmaPath' }, detail: detail ?? { stage: 'transport', part: 'main', reason: 'unclassified' }, certificate: cert, sourceGoal: from, targetGoal: to, sourceCounts: options?.native?.from.counts, targetCounts: options?.native?.to.counts });
61
+ return result;
62
+ });
63
+ }
17
64
  export function countedCertificateHyps(goal, cert) {
18
- if (!cert.lemmas?.length)
65
+ if (!cert.lemmas?.length && !cert.frames?.length && !cert.monos?.length && !cert.lins?.length)
19
66
  return usedHyps(goal, cert.tree);
20
67
  try {
21
68
  const found = new Set(), seen = new WeakSet();
@@ -29,7 +76,7 @@ export function countedCertificateHyps(goal, cert) {
29
76
  seen.delete(tree);
30
77
  return valid;
31
78
  }
32
- for (const i of usedHyps(goal, tree, lemmaPath(cert.lemmas, path)))
79
+ for (const i of usedHyps(goal, tree, cert.frames?.length || cert.monos?.length || cert.lins?.length ? framePath(cert.frames ?? [], cert.lemmas ?? [], path, cert.monos ?? [], cert.lins ?? []) : lemmaPath(cert.lemmas ?? [], path)))
33
80
  found.add(i);
34
81
  seen.delete(tree);
35
82
  return true;
@@ -38,7 +85,7 @@ export function countedCertificateHyps(goal, cert) {
38
85
  return undefined;
39
86
  // Every listed cut is checked, even when the main tree does not cite it.
40
87
  // Its explicit source slice must survive dependency pruning as a whole.
41
- for (const lemma of cert.lemmas) {
88
+ for (const lemma of cert.lemmas ?? []) {
42
89
  if (lemma.slice === undefined) {
43
90
  for (let i = 0; i < goal.hyps.length; i++)
44
91
  found.add(i);
@@ -52,7 +99,11 @@ export function countedCertificateHyps(goal, cert) {
52
99
  }
53
100
  return found;
54
101
  }
55
- catch {
102
+ catch (error) {
103
+ // Cancellation is not malformed metadata. Preserve the original error,
104
+ // including the allocator's identity-bound capacity-refusal provenance.
105
+ if (error instanceof BudgetExpired || error instanceof WallExpired)
106
+ throw error;
56
107
  return undefined;
57
108
  }
58
109
  }
@@ -0,0 +1,57 @@
1
+ import { type C, type Law } from './machine.js';
2
+ import { type CountCert, type CountDecl, type Instances } from '../kernel/kernel.js';
3
+ export declare function countCertificateSize(cert: CountCert): {
4
+ leaves: number;
5
+ splits: number;
6
+ steps: number;
7
+ frames: number;
8
+ lins: number;
9
+ monos: number;
10
+ instances: number;
11
+ };
12
+ export declare function dedupeCountInstances(family: Law[], facts: Law[], original: Instances): {
13
+ instances: Instances;
14
+ removed: string[];
15
+ };
16
+ export type CountMinimizeOptions = {
17
+ lift?: boolean;
18
+ merge?: boolean;
19
+ maxVisits?: number;
20
+ };
21
+ export declare function minimizeCountCertificate(hyps: C[], decls: CountDecl[], original: CountCert, until: number, options?: CountMinimizeOptions): {
22
+ cert: CountCert;
23
+ stats: {
24
+ before: {
25
+ leaves: number;
26
+ splits: number;
27
+ steps: number;
28
+ frames: number;
29
+ lins: number;
30
+ monos: number;
31
+ instances: number;
32
+ };
33
+ after: {
34
+ leaves: number;
35
+ splits: number;
36
+ steps: number;
37
+ frames: number;
38
+ lins: number;
39
+ monos: number;
40
+ instances: number;
41
+ };
42
+ liftAttempts: number;
43
+ liftedSplits: number;
44
+ frameMerges: number;
45
+ unusedFrames: number;
46
+ unusedLins: number;
47
+ duplicateLins: number;
48
+ fastRejected: number;
49
+ visits: number;
50
+ rowsBuilt: number;
51
+ aborted: boolean;
52
+ missing: {
53
+ split: string;
54
+ rows: string[];
55
+ }[];
56
+ };
57
+ };
@@ -0,0 +1,263 @@
1
+ import { assumed, canon, frameOk, framePath, frameRows, linRows, mcGoal } from "../kernel/kernel.js";
2
+ import { late, rowsAt } from "../kernel/prove.js";
3
+ import { countsForReads } from "./count-facts.js";
4
+ export function countCertificateSize(cert) {
5
+ let leaves = 0, splits = 0, steps = 0;
6
+ const pending = [{ tree: cert.tree, exit: false }], active = new Set();
7
+ while (pending.length) {
8
+ const { tree, exit } = pending.pop();
9
+ if (exit) {
10
+ active.delete(tree);
11
+ continue;
12
+ }
13
+ if (active.has(tree))
14
+ throw Error('cyclic certificate tree');
15
+ if ('leaf' in tree) {
16
+ leaves++;
17
+ steps += tree.leaf.length;
18
+ }
19
+ else {
20
+ active.add(tree);
21
+ splits++;
22
+ pending.push({ tree, exit: true }, { tree: tree.no, exit: false }, { tree: tree.yes, exit: false });
23
+ }
24
+ }
25
+ return { leaves, splits, steps, frames: cert.frames?.length ?? 0, lins: cert.lins?.length ?? 0, monos: cert.monos?.length ?? 0, instances: cert.insts.length };
26
+ }
27
+ // Remove only exact duplicate instantiated formulas. Plain laws and unknown
28
+ // flag instances stay; the caller rebuilds its original goal and reindexes.
29
+ export function dedupeCountInstances(family, facts, original) {
30
+ const laws = [...family, ...facts], seen = new Set(laws.filter(l => !l.forall).flatMap(l => assumed([l], {})).map(h => JSON.stringify(h)));
31
+ const instances = {}, removed = [];
32
+ for (const [name, terms] of Object.entries(original)) {
33
+ const law = laws.find(l => l.name === name), arity = law?.forall ? 1 + (law.keys?.length ?? 0) : 0;
34
+ if (!law || !arity || terms.length % arity) {
35
+ instances[name] = terms;
36
+ continue;
37
+ }
38
+ const kept = [];
39
+ for (let i = 0; i < terms.length; i += arity) {
40
+ const tuple = terms.slice(i, i + arity), keys = assumed([law], { [name]: tuple }).map(h => JSON.stringify(h));
41
+ if (keys.length && keys.every(key => seen.has(key)))
42
+ removed.push(name);
43
+ else {
44
+ kept.push(...tuple);
45
+ keys.forEach(key => seen.add(key));
46
+ }
47
+ }
48
+ if (kept.length)
49
+ instances[name] = kept;
50
+ }
51
+ return { instances, removed };
52
+ }
53
+ export function minimizeCountCertificate(hyps, decls, original, until, options = {}) {
54
+ const before = countCertificateSize(original), stats = { before, after: before, liftAttempts: 0, liftedSplits: 0, frameMerges: 0, unusedFrames: 0, unusedLins: 0, duplicateLins: 0, fastRejected: 0, visits: 0, rowsBuilt: 0, aborted: false, missing: [] };
55
+ if (late(until)) {
56
+ stats.aborted = true;
57
+ return { cert: original, stats };
58
+ }
59
+ const goal = mcGoal(hyps, decls, original), counts = countsForReads(decls, original.reads);
60
+ if (!goal || !counts)
61
+ return { cert: original, stats };
62
+ const live = () => { const ok = !late(until) && stats.visits++ < (options.maxVisits ?? 200000); if (!ok)
63
+ stats.aborted = true; return ok; };
64
+ const rowGetter = (g, c) => {
65
+ const cache = new Map();
66
+ return (path) => {
67
+ const key = JSON.stringify([...path]);
68
+ let rows = cache.get(key);
69
+ if (!rows) {
70
+ rows = rowsAt(g, framePath(c.frames ?? [], c.lemmas ?? [], path, c.monos ?? [], c.lins ?? []));
71
+ stats.rowsBuilt++;
72
+ if (cache.size >= 4096)
73
+ cache.delete(cache.keys().next().value);
74
+ cache.set(key, rows);
75
+ }
76
+ return rows;
77
+ };
78
+ };
79
+ const rowIndices = new WeakMap();
80
+ const transport = (tree, from, to, oldRows, newRows, missing, depth = 0) => {
81
+ if (!live() || depth > 512)
82
+ return undefined;
83
+ if ('split' in tree) {
84
+ const id = canon(tree.split);
85
+ if (to.has(id))
86
+ return undefined;
87
+ const next = (path, holds) => new Map(path).set(id, { lin: tree.split, holds });
88
+ const yes = transport(tree.yes, next(from, true), next(to, true), oldRows, newRows, missing, depth + 1);
89
+ const no = yes && transport(tree.no, next(from, false), next(to, false), oldRows, newRows, missing, depth + 1);
90
+ return yes && no ? { split: tree.split, yes, no } : undefined;
91
+ }
92
+ const a = oldRows(from), b = newRows(to);
93
+ let indices = rowIndices.get(b);
94
+ if (!indices) {
95
+ indices = new Map();
96
+ b.forEach((r, i) => { const key = canon(r.lin); if (!indices.has(key))
97
+ indices.set(key, i); });
98
+ rowIndices.set(b, indices);
99
+ }
100
+ const squares = tree.squares?.length ?? 0, products = tree.products ?? [];
101
+ const index = (i, limit = a.length + squares + products.length) => {
102
+ if (!Number.isSafeInteger(i) || i < 0 || i >= limit)
103
+ return undefined;
104
+ if (i >= a.length)
105
+ return i - a.length + b.length;
106
+ const key = canon(a[i].lin), j = indices.get(key);
107
+ if (j === undefined && missing.length < 8)
108
+ missing.push(key);
109
+ return j;
110
+ };
111
+ const leaf = [];
112
+ for (const step of tree.leaf) {
113
+ if (step.kind !== 'input')
114
+ leaf.push(step);
115
+ else {
116
+ const i = index(step.index);
117
+ if (i === undefined)
118
+ return undefined;
119
+ leaf.push({ ...step, index: i });
120
+ }
121
+ }
122
+ const pairs = [];
123
+ for (let n = 0; n < products.length; n++) {
124
+ const [a0, b0] = products[n], i = index(a0, a.length + squares + n), j = index(b0, a.length + squares + n);
125
+ if (i === undefined || j === undefined)
126
+ return undefined;
127
+ pairs.push([i, j]);
128
+ }
129
+ return { leaf, ...(tree.products ? { products: pairs } : {}), ...(tree.squares ? { squares: tree.squares } : {}) };
130
+ };
131
+ let cert = original;
132
+ const oldRows = rowGetter(goal, original);
133
+ if (options.merge !== false) {
134
+ const used = new Set();
135
+ const collect = (tree, path, depth = 0) => {
136
+ if (!live() || depth > 512) {
137
+ stats.aborted = true;
138
+ return;
139
+ }
140
+ if ('split' in tree) {
141
+ const id = canon(tree.split);
142
+ collect(tree.yes, new Map(path).set(id, { lin: tree.split, holds: true }), depth + 1);
143
+ collect(tree.no, new Map(path).set(id, { lin: tree.split, holds: false }), depth + 1);
144
+ return;
145
+ }
146
+ const rows = oldRows(path), indices = [...tree.leaf.flatMap(s => s.kind === 'input' ? [s.index] : []), ...(tree.products ?? []).flat()];
147
+ for (const i of indices)
148
+ if (rows[i])
149
+ used.add(canon(rows[i].lin));
150
+ };
151
+ if ((original.frames?.length ?? 0) || (original.lins?.length ?? 0))
152
+ collect(original.tree, new Map());
153
+ const groups = new Map();
154
+ const neededFrames = (original.frames ?? []).filter(f => frameRows(f).some(row => used.has(canon(row))));
155
+ stats.unusedFrames = (original.frames?.length ?? 0) - neededFrames.length;
156
+ for (const frame of neededFrames) {
157
+ const key = JSON.stringify([frame.post, frame.pre]), group = groups.get(key) ?? [];
158
+ groups.set(key, group);
159
+ group.push(frame);
160
+ }
161
+ const frames = [];
162
+ for (const group of groups.values()) {
163
+ const kept = [];
164
+ for (const frame of group) {
165
+ let merged = false;
166
+ for (let i = 0; i < kept.length; i++) {
167
+ const prior = kept[i], keys = new Set(frame.when.map(canon)), candidate = { post: frame.post, pre: frame.pre, when: prior.when.filter(w => keys.has(canon(w))) };
168
+ if (frameOk(counts, candidate)) {
169
+ kept[i] = candidate;
170
+ stats.frameMerges++;
171
+ merged = true;
172
+ break;
173
+ }
174
+ }
175
+ if (!merged)
176
+ kept.push(frame);
177
+ }
178
+ frames.push(...kept);
179
+ }
180
+ const side = (terms) => { const sum = new Map(); for (const [k, name] of terms)
181
+ sum.set(name, (sum.get(name) ?? 0) + k); return [...sum].filter(([, k]) => k !== 0).sort(([a], [b]) => a.localeCompare(b)); };
182
+ const seen = new Set(), lins = (original.lins ?? []).filter(cut => { if (!linRows(cut).some(row => used.has(canon(row)))) {
183
+ stats.unusedLins++;
184
+ return false;
185
+ } const key = JSON.stringify([cut.when.map(c => JSON.stringify(c)).sort(), side(cut.pos), side(cut.neg)]); if (seen.has(key)) {
186
+ stats.duplicateLins++;
187
+ return false;
188
+ } seen.add(key); return true; });
189
+ const candidate = { ...original, frames, lins };
190
+ if (stats.frameMerges || stats.duplicateLins || stats.unusedFrames || stats.unusedLins) {
191
+ const tree = transport(original.tree, new Map(), new Map(), oldRows, rowGetter(goal, candidate), []);
192
+ if (tree)
193
+ cert = { ...candidate, tree };
194
+ else {
195
+ stats.frameMerges = 0;
196
+ stats.duplicateLins = 0;
197
+ stats.unusedFrames = 0;
198
+ stats.unusedLins = 0;
199
+ }
200
+ }
201
+ }
202
+ if (options.lift !== false) {
203
+ const rows = rowGetter(goal, cert), metadata = new WeakMap();
204
+ const mark = (tree, path) => {
205
+ const known = metadata.get(tree);
206
+ if (known)
207
+ return known;
208
+ let meta;
209
+ if ('leaf' in tree) {
210
+ const keys = [...framePath(cert.frames ?? [], cert.lemmas ?? [], path, cert.monos ?? [], cert.lins ?? []).keys()], used = new Set();
211
+ for (const i of [...tree.leaf.flatMap(s => s.kind === 'input' ? [s.index] : []), ...(tree.products ?? []).flat()])
212
+ if (i < keys.length && path.has(keys[i]))
213
+ used.add(keys[i]);
214
+ meta = { leaves: 1, used };
215
+ }
216
+ else {
217
+ const key = canon(tree.split), yes = mark(tree.yes, new Map(path).set(key, { lin: tree.split, holds: true })), no = mark(tree.no, new Map(path).set(key, { lin: tree.split, holds: false }));
218
+ meta = { leaves: yes.leaves + no.leaves, used: new Set([...yes.used, ...no.used]) };
219
+ }
220
+ metadata.set(tree, meta);
221
+ return meta;
222
+ };
223
+ const visit = (tree, path, depth = 0) => {
224
+ if (!live() || depth > 512) {
225
+ stats.aborted = true;
226
+ return tree;
227
+ }
228
+ if ('leaf' in tree) {
229
+ const leaf = { ...tree };
230
+ mark(leaf, path);
231
+ return leaf;
232
+ }
233
+ const key = canon(tree.split), yesPath = new Map(path).set(key, { lin: tree.split, holds: true }), noPath = new Map(path).set(key, { lin: tree.split, holds: false });
234
+ const yes = visit(tree.yes, yesPath, depth + 1), no = visit(tree.no, noPath, depth + 1), children = [[yes, yesPath], [no, noPath]];
235
+ if (!metadata.has(yes) || !metadata.has(no))
236
+ return { split: tree.split, yes, no };
237
+ children.sort(([a], [b]) => metadata.get(a).leaves - metadata.get(b).leaves);
238
+ for (const [child, from] of children) {
239
+ stats.liftAttempts++;
240
+ // Like the ordinary prover's lift, conservatively leave a subtree
241
+ // that directly uses the discarded path decision intact.
242
+ if (metadata.get(child).used.has(key)) {
243
+ stats.fastRejected++;
244
+ continue;
245
+ }
246
+ const missing = [], lifted = transport(child, from, path, rows, rows, missing);
247
+ if (lifted) {
248
+ stats.liftedSplits++;
249
+ mark(lifted, path);
250
+ return lifted;
251
+ }
252
+ if (stats.missing.length < 10 && missing.length)
253
+ stats.missing.push({ split: key, rows: missing });
254
+ }
255
+ const result = { split: tree.split, yes, no };
256
+ mark(result, path);
257
+ return result;
258
+ };
259
+ cert = { ...cert, tree: visit(cert.tree, new Map()) };
260
+ }
261
+ stats.after = countCertificateSize(cert);
262
+ return { cert, stats };
263
+ }
@@ -0,0 +1,32 @@
1
+ export type CountClosedOptions = Readonly<{
2
+ basis?: ReadonlySet<string>;
3
+ tick?: () => void;
4
+ }>;
5
+ export type CountBlockerPath = Readonly<{
6
+ blocker: string;
7
+ path: readonly string[];
8
+ }>;
9
+ export type ClosedCountFamily = Readonly<{
10
+ members: readonly string[];
11
+ missing: ReadonlyMap<string, readonly string[]>;
12
+ blockers: readonly string[];
13
+ paths: ReadonlyMap<string, readonly CountBlockerPath[]>;
14
+ }>;
15
+ export declare function greatestClosedCountFamily(order: readonly string[], held: ReadonlyMap<string, readonly string[]>, options?: CountClosedOptions): ClosedCountFamily;
16
+ export type HeldCountPayload<T> = Readonly<{
17
+ id: string;
18
+ premises: readonly string[];
19
+ payload: T;
20
+ }>;
21
+ export type CountCloseCallbacks<T, R> = Readonly<{
22
+ legacy: () => R;
23
+ reserve: (members: readonly HeldCountPayload<T>[]) => boolean;
24
+ submit: (members: readonly HeldCountPayload<T>[]) => R;
25
+ }>;
26
+ export declare function closeGreatestCountFamily<T, R>(target: string, held: readonly HeldCountPayload<T>[], callbacks: CountCloseCallbacks<T, R>, options?: CountClosedOptions): Readonly<{
27
+ plan: ClosedCountFamily;
28
+ targetInFamily: boolean;
29
+ route: 'legacy' | 'subset' | 'empty' | 'unfunded';
30
+ submitted: boolean;
31
+ result?: R;
32
+ }>;