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
@@ -1,3 +1,4 @@
1
+ import { searchNow as countedSearchNow } from "./wall.js";
1
2
  // Untrusted proof compression. Turn repeated forced splits into independently checked conditional cuts.
2
3
  // No search, protocol vocabulary, new facts or proof-budget changes: failed transport/check returns no proposal.
3
4
  import { n, plus, times, x } from "./machine.js";
@@ -74,7 +75,7 @@ const proofPath = (when, cut) => {
74
75
  };
75
76
  // Unlike widen, transport never calls a solver when a row is missing.
76
77
  function transport(from, to, tree, before, after, until, missing) {
77
- if (performance.now() >= until)
78
+ if (countedSearchNow() >= until)
78
79
  return undefined;
79
80
  if ('split' in tree) {
80
81
  const known = decide(tree.split, after);
@@ -127,7 +128,7 @@ class ObservedPath extends Map {
127
128
  has(key) { this.observed.add(key); return super.has(key); }
128
129
  }
129
130
  function dependencies(g, t, path, facts, atoms, until) {
130
- if (performance.now() >= until)
131
+ if (countedSearchNow() >= until)
131
132
  return false;
132
133
  if ('split' in t)
133
134
  return dependencies(g, t.yes, put(path, t.split, true), facts, atoms, until)
@@ -176,7 +177,7 @@ function scopeCondition(lin) {
176
177
  }
177
178
  // Candidate construction only. Call shareLemmaCuts or the containing machine checker before admission.
178
179
  export function proposeLemmaCuts(goal, tree, options = {}) {
179
- const started = performance.now(), root = options.path ?? new Map(), until = options.until ?? Infinity;
180
+ const started = countedSearchNow(), root = options.path ?? new Map(), until = options.until ?? Infinity;
180
181
  const sizes = new WeakMap(), repeats = new Map();
181
182
  const measure = (t) => {
182
183
  if ('leaf' in t) {
@@ -271,7 +272,7 @@ export function proposeLemmaCuts(goal, tree, options = {}) {
271
272
  return { when, cut: t.split, tree: proof, slice };
272
273
  };
273
274
  const visit = (t, original, real) => {
274
- if (performance.now() >= until)
275
+ if (countedSearchNow() >= until)
275
276
  return undefined;
276
277
  if ('leaf' in t) {
277
278
  const copy = { ...t };
@@ -290,8 +291,8 @@ export function proposeLemmaCuts(goal, tree, options = {}) {
290
291
  found = usable;
291
292
  }
292
293
  if (found === undefined) {
293
- const begun = performance.now(), lemma = makeLemma(t, original, real);
294
- options.trace?.({ stage: 'lemma', produced: !!lemma, nodes: sizes.get(t.no) ?? 0, ms: performance.now() - begun, when: lemma?.when.length ?? -1, hyps: lemma?.slice?.length ?? -1 });
294
+ const begun = countedSearchNow(), lemma = makeLemma(t, original, real);
295
+ options.trace?.({ stage: 'lemma', produced: !!lemma, nodes: sizes.get(t.no) ?? 0, ms: countedSearchNow() - begun, when: lemma?.when.length ?? -1, hyps: lemma?.slice?.length ?? -1 });
295
296
  if (lemma) {
296
297
  const key = keyOf(lemma.when, lemma.cut);
297
298
  found = shared.get(key);
@@ -345,21 +346,21 @@ export function proposeLemmaCuts(goal, tree, options = {}) {
345
346
  const count = (t) => 'leaf' in t ? 1 : 1 + count(t.yes) + count(t.no);
346
347
  const after = count(out) + lemmas.reduce((n, l) => n + count(l.tree), 0);
347
348
  const workBefore = before * Math.max(1, goal.hyps.length), workAfter = count(out) * Math.max(1, scoped.hyps.length) + lemmas.reduce((n, l) => n + count(l.tree) * Math.max(1, l.slice?.length ?? scoped.hyps.length), 0);
348
- if (workAfter >= workBefore || performance.now() >= until) {
349
+ if (workAfter >= workBefore || countedSearchNow() >= until) {
349
350
  options.trace?.({ stage: 'not-cheaper-or-expired', before, after, workBefore, workAfter });
350
351
  return undefined;
351
352
  }
352
- return { goal: scoped, tree: out, lemmas, stats: { before, after, cuts: lemmas.length, sourceHyps: goal.hyps.length, lemmaHyps: lemmas.reduce((n, l) => n + (l.slice?.length ?? scoped.hyps.length), 0), mainNodes: count(out), checkMs: 0, buildMs: performance.now() - started } };
353
+ return { goal: scoped, tree: out, lemmas, stats: { before, after, cuts: lemmas.length, sourceHyps: goal.hyps.length, lemmaHyps: lemmas.reduce((n, l) => n + (l.slice?.length ?? scoped.hyps.length), 0), mainNodes: count(out), checkMs: 0, buildMs: countedSearchNow() - started } };
353
354
  }
354
355
  export function shareLemmaCuts(goal, tree, options = {}) {
355
356
  const proposed = proposeLemmaCuts(goal, tree, options);
356
357
  if (!proposed)
357
358
  return undefined;
358
- const start = performance.now(), checked = checkGoalLemmas(proposed.goal, proposed.tree, proposed.lemmas);
359
+ const start = countedSearchNow(), checked = checkGoalLemmas(proposed.goal, proposed.tree, proposed.lemmas);
359
360
  if (!checked) {
360
361
  options?.trace?.({ stage: 'kernel-rejected' });
361
362
  return undefined;
362
363
  }
363
- proposed.stats.checkMs = performance.now() - start;
364
+ proposed.stats.checkMs = countedSearchNow() - start;
364
365
  return proposed;
365
366
  }
@@ -0,0 +1,14 @@
1
+ import { type Lin } from './solver.js';
2
+ export declare function fundedAdd(...inputs: Lin[]): Lin;
3
+ export declare const fundedSub: (a: Lin, b: Lin) => Lin;
4
+ export declare const fundedScale: (a: Lin, factor: number) => Lin;
5
+ export declare const fundedVariable: (name: string) => Lin;
6
+ export declare function fundedConstant(value: number): Lin;
7
+ export declare function admittedLinearProduct(a: Lin, b: Lin, keyOf: (p: string, q: string) => string | undefined, site: string, safeIntegers: boolean): Lin | undefined;
8
+ export declare function everyLinearCell(lin: Lin, test: (key: string, value: number) => boolean, site: string): boolean;
9
+ export declare function visitLinearCells(lin: Lin, visit: (key: string, value: number) => void, site: string): void;
10
+ export declare function admittedLinearSub(a: Lin, b: Lin, site: string): Lin;
11
+ export declare function admittedLinearAdd(a: Lin, b: Lin, site: string): Lin;
12
+ export declare function admittedLinearScale(a: Lin, factor: number, site: string): Lin;
13
+ export declare function admittedLinearCopy(a: Lin, site: string): Lin;
14
+ export declare function admittedLinearVariable(name: string, coefficient: number, site: string): Lin;
@@ -0,0 +1,151 @@
1
+ // Owned two-phase coefficient production. Discovery and output are DISTINCT
2
+ // physical cell operations; zero/cancelled inputs still cost discovery work.
3
+ // Domain: synchronous ordinary data records, no Proxy/exotic prototype. Native
4
+ // own-key enumeration still needs a candidate-specific M1/M3 summary; this
5
+ // module alone does not close the global SIZE/ORDER gate.
6
+ import { add, scale, sub, v } from "./solver.js";
7
+ import { completeWorkProfile } from "./work-profile.js";
8
+ import { BudgetExpired, startWork, withAdmittedChunk } from "./work-admission.js";
9
+ function emitOwned(plan, keepZero, site) {
10
+ // The plan is local and owns primitive values, never references input cells.
11
+ // Map.size and array.length below are intrinsic metadata of owned containers.
12
+ const output = [], iterator = plan.entries(), size = plan.size;
13
+ let nonzero = 0;
14
+ for (let i = 0; i < size; i++) {
15
+ startWork('tableCells', 1, `${site}:select`);
16
+ const entry = iterator.next();
17
+ if (entry.done)
18
+ throw new Error('coefficient plan changed during selection');
19
+ if (entry.value[1] !== 0)
20
+ nonzero++;
21
+ if (keepZero || entry.value[1] !== 0)
22
+ output.push({ key: entry.value[0], value: entry.value[1], charge: entry.value[1] === 0 ? 0 : 1 });
23
+ }
24
+ return withAdmittedChunk('tableCells', nonzero, lease => {
25
+ // This producer cannot return a partial row. Refuse before creating any
26
+ // output coefficient, then let finally retire the unused reservation.
27
+ if (lease.granted !== nonzero)
28
+ throw new BudgetExpired(`${site}:emit`);
29
+ const out = Object.create(null);
30
+ for (let i = 0; i < output.length; i++) {
31
+ const entry = output[i];
32
+ // Occupancy is cached metadata, not a repeated coefficient read. A zero
33
+ // representation entry is no nonzero emission; start(0) validates life.
34
+ lease.start(entry.charge);
35
+ out[entry.key] = entry.value;
36
+ }
37
+ return out;
38
+ }, `${site}:emit`);
39
+ }
40
+ function discover(inputs, keepZero, site) {
41
+ const plan = new Map();
42
+ for (const { lin, factor } of inputs) {
43
+ const prototype = Object.getPrototypeOf(lin);
44
+ if (prototype !== null && prototype !== Object.prototype)
45
+ throw new TypeError('coefficient input must be an ordinary data record');
46
+ for (const key in lin) {
47
+ startWork('tableCells', 1, `${site}:input`);
48
+ const descriptor = Object.getOwnPropertyDescriptor(lin, key);
49
+ if (!descriptor || !('value' in descriptor) || typeof descriptor.value !== 'number')
50
+ throw new TypeError('coefficient input must have own numeric data cells');
51
+ // This is the first cell lookup/arithmetic/accumulation, after start.
52
+ const value = descriptor.value;
53
+ plan.set(key, plan.has(key) ? plan.get(key) + value * factor : value * factor);
54
+ }
55
+ }
56
+ return emitOwned(plan, keepZero, site);
57
+ }
58
+ export function fundedAdd(...inputs) {
59
+ if (!completeWorkProfile.enabled)
60
+ return add(...inputs);
61
+ const plan = [];
62
+ for (let i = 0; i < inputs.length; i++) {
63
+ startWork('ticks', 1, 'linear add operand');
64
+ plan.push({ lin: inputs[i], factor: 1 });
65
+ }
66
+ return discover(plan, false, 'linear:add');
67
+ }
68
+ export const fundedSub = (a, b) => admittedLinearSub(a, b, 'linear:sub');
69
+ export const fundedScale = (a, factor) => admittedLinearScale(a, factor, 'linear:scale');
70
+ export const fundedVariable = (name) => admittedLinearVariable(name, 1, 'linear:variable');
71
+ export function fundedConstant(value) {
72
+ if (!completeWorkProfile.enabled)
73
+ return value === 0 ? {} : { 1: value };
74
+ startWork('tableCells', 1, 'linear:constant:input');
75
+ const plan = new Map();
76
+ if (value !== 0)
77
+ plan.set('1', value);
78
+ return emitOwned(plan, false, 'linear:constant');
79
+ }
80
+ export function admittedLinearProduct(a, b, keyOf, site, safeIntegers) {
81
+ const plan = new Map();
82
+ for (const p in a)
83
+ for (const q in b) {
84
+ startWork('tableCells', 1, `${site}:left`);
85
+ const ad = Object.getOwnPropertyDescriptor(a, p);
86
+ if (!ad || !('value' in ad) || typeof ad.value !== 'number')
87
+ throw new TypeError('product input must have numeric data cells');
88
+ const cp = ad.value;
89
+ startWork('tableCells', 1, `${site}:right`);
90
+ const bd = Object.getOwnPropertyDescriptor(b, q);
91
+ if (!bd || !('value' in bd) || typeof bd.value !== 'number')
92
+ throw new TypeError('product input must have numeric data cells');
93
+ const cq = bd.value, key = keyOf(p, q);
94
+ if (key === undefined)
95
+ return undefined;
96
+ const value = (plan.get(key) ?? 0) + cp * cq;
97
+ if (safeIntegers && ![cp, cq, cp * cq, value].every(Number.isSafeInteger))
98
+ return undefined;
99
+ plan.set(key, value);
100
+ }
101
+ return emitOwned(plan, false, site);
102
+ }
103
+ export function everyLinearCell(lin, test, site) {
104
+ if (!completeWorkProfile.enabled)
105
+ return Object.entries(lin).every(([key, value]) => test(key, value));
106
+ for (const key in lin) {
107
+ if (completeWorkProfile.enabled)
108
+ startWork('tableCells', 1, site);
109
+ const descriptor = Object.getOwnPropertyDescriptor(lin, key);
110
+ if (!descriptor || !('value' in descriptor) || typeof descriptor.value !== 'number')
111
+ throw new TypeError('coefficient input must have numeric data cells');
112
+ if (!test(key, descriptor.value))
113
+ return false;
114
+ }
115
+ return true;
116
+ }
117
+ export function visitLinearCells(lin, visit, site) {
118
+ if (!completeWorkProfile.enabled) {
119
+ for (const [key, value] of Object.entries(lin))
120
+ visit(key, value);
121
+ return;
122
+ }
123
+ for (const key in lin) {
124
+ if (completeWorkProfile.enabled)
125
+ startWork('tableCells', 1, `${site}:visit`);
126
+ const descriptor = Object.getOwnPropertyDescriptor(lin, key);
127
+ if (!descriptor || !('value' in descriptor) || typeof descriptor.value !== 'number')
128
+ throw new TypeError('coefficient input must have numeric data cells');
129
+ visit(key, descriptor.value);
130
+ }
131
+ }
132
+ export function admittedLinearSub(a, b, site) {
133
+ return completeWorkProfile.enabled ? discover([{ lin: a, factor: 1 }, { lin: b, factor: -1 }], false, site) : sub(a, b);
134
+ }
135
+ export function admittedLinearAdd(a, b, site) {
136
+ return completeWorkProfile.enabled ? discover([{ lin: a, factor: 1 }, { lin: b, factor: 1 }], false, site) : add(a, b);
137
+ }
138
+ export function admittedLinearScale(a, factor, site) {
139
+ return completeWorkProfile.enabled ? discover([{ lin: a, factor }], false, site) : scale(a, factor);
140
+ }
141
+ export function admittedLinearCopy(a, site) {
142
+ return completeWorkProfile.enabled ? discover([{ lin: a, factor: 1 }], true, site) : a;
143
+ }
144
+ export function admittedLinearVariable(name, coefficient, site) {
145
+ if (!completeWorkProfile.enabled)
146
+ return coefficient === 1 ? v(name) : { [name]: coefficient };
147
+ startWork('tableCells', 1, `${site}:construct`);
148
+ const plan = new Map();
149
+ plan.set(name, coefficient);
150
+ return emitOwned(plan, true, site);
151
+ }
@@ -3,7 +3,8 @@ export declare const lcg: (x: number) => number;
3
3
  export type Rand = (k: number) => number;
4
4
  export declare const seeded: (seed: number) => Rand;
5
5
  export declare const walk: (t: unknown, f: (o: Record<string, unknown>) => void) => void;
6
- export declare const walkOnce: (t: unknown, f: (o: object) => void) => void;
6
+ export declare const hasFinite: (value: unknown) => boolean;
7
+ export declare const walkOnce: (t: unknown, f: (o: object) => void, before?: () => void) => void;
7
8
  export type E = {
8
9
  op: 'var';
9
10
  name: string;
@@ -62,6 +63,7 @@ export type C = {
62
63
  keys?: string[];
63
64
  c: C;
64
65
  witness?: E;
66
+ finite?: string;
65
67
  };
66
68
  export type Machine = {
67
69
  name: string;
@@ -207,6 +209,7 @@ export type Law = {
207
209
  holds: C;
208
210
  forall?: string;
209
211
  keys?: string[];
212
+ finite?: string;
210
213
  stated?: C;
211
214
  some?: string[];
212
215
  witness?: SomeWitness;
@@ -295,7 +298,7 @@ export type Compiled = {
295
298
  head?: number;
296
299
  };
297
300
  fork: (st: State) => State;
298
- step: (params: object, st: State, input: object) => State & {
301
+ step: (params: object, st: State, input: object, finiteCheck?: () => void) => State & {
299
302
  head?: number;
300
303
  };
301
304
  cells?: (params: object, st: State, input: object, tick: () => void) => Record<string, Map<number, number>>;
@@ -5,9 +5,15 @@
5
5
  // Arithmetic is linear: +, −, × by a constant, min, max, if. Maps are naturals indexed by naturals, every cell starting
6
6
  // at the same value; a law about a map holds for every key x, and is proven at one arbitrary x (with the law assumed at
7
7
  // every other key the step reads).
8
- import { boundOf, crossFree, eachParts, historyMachine, tuple, keyTerm, mapArities, plainKey, scope, sumRuntime, tupleParts } from "../kernel/kernel.js";
8
+ import { boundOf, crossFree, eachParts, finiteBound, finiteLawBound, historyMachine, tuple, keyTerm, mapArities, plainKey, scope, sumRuntime, tupleParts } from "../kernel/kernel.js";
9
9
  import { add, env, eq, k, le, lt, nat, scale, show, solve, sub, v } from "./solver.js";
10
10
  import { runtimeWall } from "./runtime-wall.js";
11
+ import { traceNow } from "./trace-clock.js";
12
+ import { coreMeter } from "./meter.js";
13
+ import { observeEval } from "./work-observe.js";
14
+ import { observeAst } from "./meter.js";
15
+ import { startWork } from "./work-admission.js";
16
+ import { completeWorkProfile } from "./work-profile.js";
11
17
  // The one generator of the repo's random draws: x' = (1103515245 x + 12345) mod 2^31, exact (Math.imul). In doubles
12
18
  // the product passes 2^53 and rounds, which folds every seed into one of six short cycles (61 to 10,466 steps; 98.8% of
13
19
  // seeds into the same one). Its period is 2^31 (core/rand.test.ts). Draws are its high bits (the low ones repeat with a
@@ -28,13 +34,22 @@ export const walk = (t, f) => {
28
34
  for (const x of Object.values(o))
29
35
  walk(x, f);
30
36
  };
37
+ // Opt-in finite markers, including a law whose universal was pulled out by prenex.
38
+ // Unsupported evaluators use this to refuse rather than approximate its domain.
39
+ export const hasFinite = (value) => {
40
+ let found = false;
41
+ walk(value, o => { if (o.finite !== undefined && (o.op === 'all' || 'holds' in o))
42
+ found = true; });
43
+ return found;
44
+ };
31
45
  // Every object in a term once (arrays too), a part shared by several uses read at its first: a witness read at each of
32
46
  // its uses in a goal is one object, and a walk of the goal written out would read it at each.
33
- export const walkOnce = (t, f) => {
47
+ export const walkOnce = (t, f, before) => {
34
48
  const seen = new Set();
35
49
  const go = (x) => {
36
50
  if (!x || typeof x !== 'object' || seen.has(x))
37
51
  return;
52
+ before?.();
38
53
  seen.add(x);
39
54
  f(x);
40
55
  for (const y of Object.values(x))
@@ -91,16 +106,16 @@ export const pointWrite = (w, where) => { if ('each' in w)
91
106
  export const noQuantifier = (where) => { throw new Error(`${where} takes no quantified flag`); };
92
107
  export { unreachable, tuple, tupleParts, part } from "../kernel/kernel.js";
93
108
  // Builders.
94
- export const x = (name) => ({ op: 'var', name });
95
- export const n = (value) => ({ op: 'const', value });
96
- export const plus = (a, b) => ({ op: 'add', a, b });
97
- export const minus = (a, b) => ({ op: 'sub', a, b });
98
- export const times = (factor, a) => ({ op: 'mul', k: factor, a });
99
- export const min = (a, b) => ({ op: 'min', a, b });
100
- export const max = (a, b) => ({ op: 'max', a, b });
101
- export const prod = (a, b) => ({ op: 'prod', a, b });
102
- export const ite = (c, then, otherwise) => ({ op: 'if', c, then, else: otherwise });
103
- export const get = (map, key) => ({ op: 'get', map, key });
109
+ export const x = (name) => (observeAst(() => ({ op: 'var', name })));
110
+ export const n = (value) => (observeAst(() => ({ op: 'const', value })));
111
+ export const plus = (a, b) => (observeAst(() => ({ op: 'add', a, b })));
112
+ export const minus = (a, b) => (observeAst(() => ({ op: 'sub', a, b })));
113
+ export const times = (factor, a) => (observeAst(() => ({ op: 'mul', k: factor, a })));
114
+ export const min = (a, b) => (observeAst(() => ({ op: 'min', a, b })));
115
+ export const max = (a, b) => (observeAst(() => ({ op: 'max', a, b })));
116
+ export const prod = (a, b) => (observeAst(() => ({ op: 'prod', a, b })));
117
+ export const ite = (c, then, otherwise) => (observeAst(() => ({ op: 'if', c, then, else: otherwise })));
118
+ export const get = (map, key) => (observeAst(() => ({ op: 'get', map, key })));
104
119
  // The pairing and its inverse as the runtimes compute them, exactly (Lean's cpair and cun): tri(a + b) + b, and back.
105
120
  export const cpair = (a, b) => { const s = a + b; return exactPair(s % 2 === 0 ? (s / 2) * (s + 1) + b : s * ((s + 1) / 2) + b); };
106
121
  const exactPair = (v) => { if (!Number.isSafeInteger(v))
@@ -115,12 +130,12 @@ export const cunpair = (z) => {
115
130
  return [s - b, b];
116
131
  };
117
132
  const tri = (s) => (s % 2 === 0 ? (s / 2) * (s + 1) : s * ((s + 1) / 2));
118
- export const less = (a, b) => ({ op: '<', a, b });
119
- export const atMost = (a, b) => ({ op: '<=', a, b });
120
- export const same = (a, b) => ({ op: '=', a, b });
121
- export const all = (...xs) => ({ op: 'and', xs });
122
- export const any = (...xs) => ({ op: 'or', xs });
123
- export const not = (c) => ({ op: 'not', x: c });
133
+ export const less = (a, b) => (observeAst(() => ({ op: '<', a, b })));
134
+ export const atMost = (a, b) => (observeAst(() => ({ op: '<=', a, b })));
135
+ export const same = (a, b) => (observeAst(() => ({ op: '=', a, b })));
136
+ export const all = (...xs) => (observeAst(() => ({ op: 'and', xs })));
137
+ export const any = (...xs) => (observeAst(() => ({ op: 'or', xs })));
138
+ export const not = (c) => (observeAst(() => ({ op: 'not', x: c })));
124
139
  export const bit = (name) => any(same(x(name), n(0)), same(x(name), n(1)));
125
140
  // --- division and remainder by a constant ------------------------------------------------------------------------
126
141
  // x div k and x mod k, for a constant k >= 1, are reads of two functions of the rule, div$k and mod$k, at x. The kernel
@@ -306,6 +321,7 @@ const exact = (v) => {
306
321
  // (a value is a number: a name like constructor or toString, read through an object's prototype, is no value of it)
307
322
  const num = (x) => (typeof x === 'number' ? x : undefined);
308
323
  export function evalE(e, env, st, m) {
324
+ observeEval();
309
325
  switch (e.op) {
310
326
  case 'var': {
311
327
  const v = num(env[e.name]) ?? num(st.scalars[e.name]);
@@ -349,6 +365,7 @@ const directCall = (d) => {
349
365
  // Arithmetic inside an equation is evaluated just as in generated code; no paired key is needed for calls.
350
366
  const memo = new Map();
351
367
  const value = (e, env) => {
368
+ observeEval();
352
369
  switch (e.op) {
353
370
  case 'const': return e.value;
354
371
  case 'var': return env[e.name];
@@ -368,6 +385,7 @@ const directCall = (d) => {
368
385
  }
369
386
  };
370
387
  const condition = (c, env) => {
388
+ observeEval();
371
389
  switch (c.op) {
372
390
  case '<': return value(c.a, env) < value(c.b, env);
373
391
  case '<=': return value(c.a, env) <= value(c.b, env);
@@ -459,6 +477,7 @@ export function pairedArgs(key, n) {
459
477
  }
460
478
  const alike = (a, b) => a === b || JSON.stringify(a) === JSON.stringify(b);
461
479
  export function evalC(c, env, st, m) {
480
+ observeEval();
462
481
  switch (c.op) {
463
482
  case '<': return evalE(c.a, env, st, m) < evalE(c.b, env, st, m);
464
483
  case '<=': return evalE(c.a, env, st, m) <= evalE(c.b, env, st, m);
@@ -468,6 +487,18 @@ export function evalC(c, env, st, m) {
468
487
  case 'not': return !evalC(c.x, env, st, m);
469
488
  case 'flag': return env[`?${c.name}`] === 1;
470
489
  case 'all': {
490
+ const finite = finiteBound(c);
491
+ if (finite) {
492
+ runtimeWall.check?.('finite quantifier');
493
+ const size = naturalIn(evalE(finite, env, st, m), `size of ${c.finite}`);
494
+ for (let key = 0; key < size; key++) {
495
+ if ((key & 255) === 0)
496
+ runtimeWall.check?.('finite quantifier');
497
+ if (!evalC(c.c, { ...env, [c.key]: key }, st, m))
498
+ return false;
499
+ }
500
+ return true;
501
+ }
471
502
  const names = boundOf(c), keys = tupleKeys(c, env, st, m);
472
503
  const go = (i, at) => (i === names.length ? evalC(c.c, at, st, m) : keys.every(key => go(i + 1, { ...at, [names[i]]: key })));
473
504
  return go(0, env);
@@ -478,6 +509,11 @@ export function evalC(c, env, st, m) {
478
509
  // (a read reads the machine's functions as they are when it runs, which a table swap changes; a quantified condition and
479
510
  // a function of several arguments go through evalC and evalE). For a term read at many rows (the engine's views).
480
511
  export function compileE(e, m) {
512
+ const run = compileENode(e, m);
513
+ // A get either charges its own node or delegates it to evalE, never both.
514
+ return e.op === 'get' ? run : (env, st) => { observeEval(); return run(env, st); };
515
+ }
516
+ function compileENode(e, m) {
481
517
  switch (e.op) {
482
518
  case 'var': {
483
519
  const name = e.name;
@@ -530,6 +566,7 @@ export function compileE(e, m) {
530
566
  const fn = m.fns?.[map];
531
567
  if (fn?.call && pairedArgs(e.key, fn.arity))
532
568
  return evalE(e, env, st, m);
569
+ observeEval();
533
570
  const k = key(env, st);
534
571
  return fn ? naturalIn(fnOf(fn)(k), `${map}(${k})`) : st.maps[map].get(k) ?? st.defaults?.[map] ?? m.maps[map];
535
572
  };
@@ -537,6 +574,11 @@ export function compileE(e, m) {
537
574
  }
538
575
  }
539
576
  export function compileC(c, m) {
577
+ const run = compileCNode(c, m);
578
+ // Quantified conditions delegate the whole evaluation to the interpreter.
579
+ return c.op === 'all' ? run : (env, st) => { observeEval(); return run(env, st); };
580
+ }
581
+ function compileCNode(c, m) {
540
582
  switch (c.op) {
541
583
  case '<': {
542
584
  const a = compileE(c.a, m), b = compileE(c.b, m);
@@ -617,7 +659,7 @@ function tupleOf(w, parts) {
617
659
  tupleWrites.set(w, (byParts = new Map()));
618
660
  let t = byParts.get(parts);
619
661
  if (!t) {
620
- const e = eachParts(w, parts), body = { op: '=', a: e.value, b: { op: 'const', value: 0 } }, free = crossFree(body, e.names);
662
+ const e = eachParts(w, parts), body = observeAst(() => ({ op: '=', a: e.value, b: observeAst(() => ({ op: 'const', value: 0 })) })), free = crossFree(body, e.names);
621
663
  t = { ...e, bs: e.names.map(name => breaksOf(free, name)), crossed: JSON.stringify(free) !== JSON.stringify(body), reads: new WeakMap() };
622
664
  byParts.set(parts, t);
623
665
  }
@@ -706,13 +748,13 @@ function unchanged(t, map, env, st, m) {
706
748
  if (typeof v !== 'boolean')
707
749
  xs.push(v);
708
750
  }
709
- return xs.length === 0 ? c.op === 'and' : xs.length === 1 ? xs[0] : { op: c.op, xs };
751
+ return xs.length === 0 ? c.op === 'and' : xs.length === 1 ? xs[0] : observeAst(() => ({ op: c.op, xs }));
710
752
  }
711
753
  return c;
712
754
  };
713
755
  const term = (e) => { if (e.op !== 'if')
714
- return e; const c = cond(e.c); return c === true ? term(e.then) : c === false ? term(e.else) : { ...e, c: c }; };
715
- const v = term(t.value), own = (e) => e.op === 'get' && e.map === map && JSON.stringify(e.key) === JSON.stringify(tuple(t.names.map((name) => ({ op: 'var', name }))));
756
+ return e; const c = cond(e.c); return c === true ? term(e.then) : c === false ? term(e.else) : observeAst(() => ({ ...e, c: c })); };
757
+ const v = term(t.value), own = (e) => e.op === 'get' && e.map === map && JSON.stringify(e.key) === JSON.stringify(tuple(t.names.map((name) => (observeAst(() => ({ op: 'var', name }))))));
716
758
  if (own(v))
717
759
  return true;
718
760
  const zeroOne = () => [...(st.maps[map]?.values() ?? [])].every(x => x === 0 || x === 1) && (st.defaults?.[map] ?? m.maps[map]) <= 1;
@@ -732,7 +774,7 @@ function runnable(m) {
732
774
  export function leastsIn(m, env, st) {
733
775
  const out = {};
734
776
  for (const [name, l] of Object.entries(m.leasts ?? {})) {
735
- const keys = keysAt(st, mapsIn({ op: 'and', xs: [l.where, ...l.by.flatMap(b => [b.e, ...(b.neg ? [b.neg] : [])].map(e => ({ op: '=', a: e, b: e })))] }).filter(mp => !m.fns?.[mp])), past = keys.pop();
777
+ const keys = keysAt(st, mapsIn(observeAst(() => ({ op: 'and', xs: [l.where, ...l.by.flatMap(b => [b.e, ...(b.neg ? [b.neg] : [])].map(e => (observeAst(() => ({ op: '=', a: e, b: e })))))] }))).filter(mp => !m.fns?.[mp])), past = keys.pop();
736
778
  const at = (key) => ({ ...env, ...out, [l.key]: key });
737
779
  if (evalC(l.where, at(past), st, m))
738
780
  throw new RangeError(`${m.name}: ${name} has no least (its condition holds at every key past the cells)`);
@@ -968,7 +1010,7 @@ export function compileJs(m) { return compileJsMode(m, false); }
968
1010
  function compileJsMode(m, persistent, cells) {
969
1011
  const { h, plain, queries, carried } = runnable(m);
970
1012
  // (a condition or a write at every key over a tuple key: the interpreter decides those, part by part)
971
- const atEvery = [...Object.values(plain.step.flags ?? {}).filter(c => c.op === 'all'), ...Object.values(plain.step.maps ?? {}).filter(w => 'each' in w)];
1013
+ const atEvery = [...Object.values(plain.step.flags ?? {}).filter(c => c.op === 'all' && c.finite === undefined), ...Object.values(plain.step.maps ?? {}).filter(w => 'each' in w)];
972
1014
  const arity = mapArities(plain);
973
1015
  if (JSON.stringify(atEvery).includes('"op":"tup"') || Object.entries(plain.step.maps ?? {}).some(([map, w]) => 'each' in w && (arity[map] ?? 1) > 1))
974
1016
  throw new Error(`${m.name}: compileJs takes no tuple key at every key (interpret runs it)`);
@@ -1007,6 +1049,14 @@ function compileJsMode(m, persistent, cells) {
1007
1049
  case 'not': return `(!${cx(c.x)})`;
1008
1050
  case 'flag': return `flag${flags.indexOf(c.name)}`;
1009
1051
  case 'all': {
1052
+ const finite = finiteBound(c);
1053
+ if (finite) {
1054
+ const size = ex(finite), outer = bound;
1055
+ bound = { ...bound, [c.key]: 'fk' };
1056
+ const body = cx(c.c);
1057
+ bound = outer;
1058
+ return `(fn => { $finite(); for (let fk = 0; fk < fn; fk++) { if ((fk & 255) === 0) $finite(); if (!${body}) return false; } return true; })($n(${size}, ${id(`size of ${c.finite}`)}))`;
1059
+ }
1010
1060
  const names = boundOf(c), ks = names.map((_, i) => (i ? `k${i}` : 'k')), keys = names.length === 1 ? keysJs(c.c, c.key) : tupleJs(c), outer = bound;
1011
1061
  bound = { ...bound, ...Object.fromEntries(names.map((n, i) => [n, ks[i]])) };
1012
1062
  const body = cx(c.c);
@@ -1074,7 +1124,7 @@ function compileJsMode(m, persistent, cells) {
1074
1124
  bound = { ...bound, [l.key]: 'lk' };
1075
1125
  const where = cx(l.where), by = l.by.map(b => (b.neg ? `(${ex(b.e)} - ${ex(b.neg)})` : ex(b.e)));
1076
1126
  bound = outer;
1077
- const ms = mapsIn({ op: 'and', xs: [l.where, ...l.by.flatMap(b => [b.e, ...(b.neg ? [b.neg] : [])].map(e => ({ op: '=', a: e, b: e })))] }).filter(mp => !fns.includes(mp));
1127
+ const ms = mapsIn(observeAst(() => ({ op: 'and', xs: [l.where, ...l.by.flatMap(b => [b.e, ...(b.neg ? [b.neg] : [])].map(e => (observeAst(() => ({ op: '=', a: e, b: e })))))] }))).filter(mp => !fns.includes(mp));
1078
1128
  const better = l.by.map((b, i) => `by[${i}] !== bb[${i}] ? by[${i}] ${b.desc ? '>' : '<'} bb[${i}] : `).join('') + 'false';
1079
1129
  lines.push(`{ const ks = $keys(maps, ${JSON.stringify(ms)}), past = ks.pop(); let lk = past; if (${where}) throw new RangeError(${id(`${m.name}: ${name} has no least (its condition holds at every key past the cells)`)}); let bk, bb; for (lk of ks.sort((a, b) => a - b)) { if (!(${where})) continue; const by = [${by.join(', ')}]; if (bk === undefined || (${better})) { bk = lk; bb = by; } } env[${id(name)}] = bk ?? 0; env[${id(l.some)}] = bk === undefined ? 0 : 1; }`);
1080
1130
  }
@@ -1118,7 +1168,7 @@ function compileJsMode(m, persistent, cells) {
1118
1168
  export function fork(state) {
1119
1169
  return {scalars: {...state.scalars}, maps: Object.fromEntries(Object.entries(state.maps).map(([name, cells]) => [name, new Map(cells)])), ...(state.defaults ? {defaults: {...state.defaults}} : {})${h ? ', history: state.history.slice(state.head), head: 0, last: state.last' : ''}};
1120
1170
  }
1121
- export function step(params, state, input) {
1171
+ export function step(params, state, input${hasFinite(plain) ? ', $finite = () => { throw new Error("finite sort step requires a cancellation callback"); }' : ''}) {
1122
1172
  ${lines.join('\n ')}
1123
1173
  }
1124
1174
  `;
@@ -1133,11 +1183,24 @@ export function compileFunction(params, body) {
1133
1183
  return new Function(...params, body);
1134
1184
  }
1135
1185
  export const loadJs = (m) => loadJsMode(m, false);
1136
- const loadJsMode = (m, persistent, cells) => compileFunction([], `${compileJsMode(m, persistent, cells).replace(/^export function/gm, 'function')}\nreturn {init, fork, step${cells ? ', cells' : ''}}`)();
1186
+ const loadJsMode = (m, persistent, cells) => {
1187
+ const js = compileFunction([], `${compileJsMode(m, persistent, cells).replace(/^export function/gm, 'function')}\nreturn {init, fork, step${cells ? ', cells' : ''}}`)();
1188
+ if (hasFinite(m)) {
1189
+ // Standalone generated modules require an explicit cooperative callback.
1190
+ // The loaded runtime uses the same cancellation bridge as the interpreter.
1191
+ const step = js.step;
1192
+ js.step = (params, st, input, check = () => runtimeWall.check?.('finite quantifier')) => step(params, st, input, check);
1193
+ }
1194
+ return js;
1195
+ };
1137
1196
  // The interpreter's step (persistent: a copy of the state each) through loadJs's, several times faster, a request's counts
1138
1197
  // given to it as maps the request carries (carriedView's, each cell its sum when read); none with a history, or where the
1139
1198
  // generator refuses the machine.
1140
1199
  export function fastStep(m) {
1200
+ // Its static per-step charge cannot account for a parameter-sized loop.
1201
+ // Proof-time callers must use the interruptible, per-node interpreter instead.
1202
+ if (hasFinite(m))
1203
+ return undefined;
1141
1204
  // (the counts the step itself reads, in its guards, flags, writes or assumption: a count only a law reads is never
1142
1205
  // computed for a step; a count's body may read another count, so the closure is taken)
1143
1206
  const all = m.counts ?? {}, read = new Set();
@@ -1164,8 +1227,16 @@ export function fastStep(m) {
1164
1227
  catch {
1165
1228
  return undefined;
1166
1229
  }
1167
- const only = cs.length ? { ...m, counts: decls } : m, tick = () => runtimeWall.check?.('derived count');
1230
+ let stepEvals = 0;
1231
+ walk([m.step, m.assume ?? null, m.leasts ?? null], o => { if (typeof o.op === 'string')
1232
+ stepEvals++; });
1233
+ const only = cs.length ? { ...m, counts: decls } : m, tick = () => { if (completeWorkProfile.enabled)
1234
+ startWork('ticks', 1, 'derived count');
1235
+ else
1236
+ coreMeter.ticks += 1; runtimeWall.check?.('derived count'); };
1168
1237
  return (params, st, input) => {
1238
+ // Section 18's static expression-node charge per compiled step invocation.
1239
+ observeEval(stepEvals, true);
1169
1240
  if (!cs.length)
1170
1241
  return js.step(params, st, input);
1171
1242
  const view = compiledCells ? js.cells(params, st, input, tick) : carriedView(only, st, input, undefined, params).maps, carried = { ...input };
@@ -1250,7 +1321,7 @@ function symC(c, s, truth) {
1250
1321
  // PROVE_DEBUG=1 logs the case counts to stderr; read through globalThis so the module also loads where there is no process.
1251
1322
  const proveDebug = !!env.PROVE_DEBUG;
1252
1323
  const debug = (label, n) => { if (proveDebug)
1253
- console.error(`[prove] ${label}: ${n} (${Math.round(performance.now())} ms)`); };
1324
+ console.error(`[prove] ${label}: ${n} (${Math.round(traceNow())} ms)`); };
1254
1325
  // Drops the cases whose path is already impossible, so impossible branches don't multiply.
1255
1326
  function feasible(cases) {
1256
1327
  return cases.filter(c => {
@@ -1332,8 +1403,8 @@ export function prove(m, goal, invariants = [goal]) {
1332
1403
  // Keys first: how each key the step reads or writes compares with each focus key of the same map, decided once.
1333
1404
  const stepReads = [
1334
1405
  ...Object.values(m.step.flags ?? {}).flatMap(readsOf),
1335
- ...Object.values(m.step.state ?? {}).flatMap(e => readsOf({ op: '=', a: e, b: e })),
1336
- ...Object.entries(m.step.maps ?? {}).flatMap(([map, w]) => [...('each' in w ? [] : [{ map, key: w.key }]), ...readsOf({ op: '=', a: w.value, b: w.value })]),
1406
+ ...Object.values(m.step.state ?? {}).flatMap(e => readsOf(observeAst(() => ({ op: '=', a: e, b: e })))),
1407
+ ...Object.entries(m.step.maps ?? {}).flatMap(([map, w]) => [...('each' in w ? [] : [{ map, key: w.key }]), ...readsOf(observeAst(() => ({ op: '=', a: w.value, b: w.value })))]),
1337
1408
  ];
1338
1409
  const pairs = new Map();
1339
1410
  for (const r of stepReads) {
@@ -1411,6 +1482,9 @@ function renameKey(lin, from, to) {
1411
1482
  export function search(m, law, depth = 3, bound = 2) {
1412
1483
  const run = interpret(m);
1413
1484
  const holds = (params, st) => {
1485
+ const finite = finiteLawBound(law);
1486
+ if (finite)
1487
+ return evalC({ op: 'all', key: law.forall, c: law.holds, finite: law.finite }, params, st, m);
1414
1488
  const keys = law.forall ? [...new Set([0, ...Object.values(st.maps).flatMap(cells => [...cells.keys()])])] : [0];
1415
1489
  return keys.every(key => evalC(law.holds, { ...params, ...(law.forall ? { [law.forall]: key } : {}) }, st, m));
1416
1490
  };
@@ -0,0 +1,22 @@
1
+ export declare const coreMeter: {
2
+ tableCells: number;
3
+ checkSteps: number;
4
+ astNodes: number;
5
+ evals: number;
6
+ cubeTests: number;
7
+ ticks: number;
8
+ tinyNodes: number;
9
+ tinyTerms: number;
10
+ reachStates: number;
11
+ reachEdges: number;
12
+ reachEvals: number;
13
+ reachCompiledEvals: number;
14
+ };
15
+ export declare function observeAst<T>(build: () => T): T;
16
+ export declare function observeTableCells(count?: number): void;
17
+ export declare function observeCheckStep(): void;
18
+ export declare function observeCubeTest(): void;
19
+ export declare function observeLinearRows(rows: readonly Record<string, number>[]): void;
20
+ export declare function observeConstraintRows(rows: readonly {
21
+ lin: Record<string, number>;
22
+ }[], start?: number): void;
@@ -0,0 +1,34 @@
1
+ import { completeWorkProfile } from "./work-profile.js";
2
+ import { startWork, workCounterView } from "./work-admission.js";
3
+ // Numeric views are compatibility telemetry. In full mode only the central
4
+ // lease can update a counter while a bounded work scope is active.
5
+ export const coreMeter = workCounterView({ tableCells: 0, checkSteps: 0, astNodes: 0, evals: 0, cubeTests: 0, ticks: 0, tinyNodes: 0, tinyTerms: 0, reachStates: 0, reachEdges: 0, reachEvals: 0, reachCompiledEvals: 0 }, { tableCells: 'tableCells', checkSteps: 'checkSteps', astNodes: 'astNodes', evals: 'evals', cubeTests: 'cubeTests', ticks: 'ticks', tinyNodes: 'tinyNodes', tinyTerms: 'tinyTerms', reachStates: 'reachStates', reachEdges: 'reachEdges', reachEvals: 'reachEvals', reachCompiledEvals: 'reachCompiledEvals' });
6
+ // The factory is intentional: a post-allocation observer is too late. Child
7
+ // constructors have their own admissions; started work is never refunded.
8
+ export function observeAst(build) {
9
+ if (completeWorkProfile.enabled) {
10
+ startWork('astNodes', 1, 'AST allocation');
11
+ return build();
12
+ }
13
+ const node = build();
14
+ coreMeter.astNodes++;
15
+ return node;
16
+ }
17
+ export function observeTableCells(count = 1) { if (completeWorkProfile.enabled)
18
+ startWork('tableCells', count, 'table cells'); }
19
+ export function observeCheckStep() { if (completeWorkProfile.enabled)
20
+ startWork('checkSteps', 1, 'native check step'); }
21
+ export function observeCubeTest() { if (completeWorkProfile.enabled)
22
+ startWork('cubeTests', 1, 'cube test'); }
23
+ export function observeLinearRows(rows) {
24
+ if (completeWorkProfile.enabled)
25
+ for (const row of rows)
26
+ for (const key in row)
27
+ if (Object.hasOwn(row, key))
28
+ observeTableCells();
29
+ }
30
+ export function observeConstraintRows(rows, start = 0) {
31
+ if (completeWorkProfile.enabled)
32
+ for (let i = start; i < rows.length; i++)
33
+ observeLinearRows([rows[i].lin]);
34
+ }