gdp-wired 0.21.0 → 0.22.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.
@@ -1,8 +1,9 @@
1
1
  import { add, k, refuteFast, scale, solve, sub, v, work } from "../core/solver.js";
2
2
  import { checkRefutation } from "../core/check.js";
3
- import { allowNode, allowTranslated, certCyclic, certOn, certSearch, closingInsts, crossCyclic, cyclicOn, cycLog, hasQuantified, machineNodes, proveCyclic, reqTS, searchedStepNodes, skMachine, timedKind, withWitness, translatedProof, uncovered, uncoveredWhy } from "../core/cyclic.js";
3
+ import { allowNode, allowTranslated, certCyclic, certOn, certSearch, closingInsts, crossCyclic, cyclicOn, cycLog, hasQuantified, loopNodes, machineNodes, proveCyclic, reqTS, searchedStepNodes, skMachine, timedKind, withWitness, translatedProof, uncovered, uncoveredWhy } from "../core/cyclic.js";
4
4
  import { proveSome, someLaws } from "../core/exists.js";
5
- import { assumed, atomOf, boundOf, canon, canonFlip, columnTerm, decided, factorsOf, isCell, isMonomial, stepZone, check, checkLaws, checkStepLaws, historyMachine, leafConstraints, leafConstraintsReusing, reusing, linear, livenessHelpful, livenessParts, obligations, plainKey, progressObligations, stepLawObligations, trueC, truth, unreachable, witnessesOf, withProducts } from "./kernel.js";
5
+ import { growCdcl } from "./cdcl.js";
6
+ import { assumed, atomOf, boundOf, canon, canonFlip, columnTerm, decided, factorsOf, isCell, isMonomial, stepZone, check, checkLaws, checkStepLaws, historyMachine, leafConstraints, leafConstraintsReusing, reusing, linear, livenessHelpful, livenessParts, loopMachine, obligations, plainKey, progressObligations, stepLawObligations, trueC, truth, unreachable, witnessesOf, withProducts } from "./kernel.js";
6
7
  // The atoms the path leaves open, in evaluation order: deciding any of them makes progress.
7
8
  function openE(e, path, out) {
8
9
  switch (e.op) {
@@ -83,7 +84,7 @@ function openH(c, path, out, positive = true) {
83
84
  // keys a pairing builds ((a + b)² + b, keys whose difference has monomials) are left uncompared: that encoding states
84
85
  // their congruence as facts, and comparing their keys only brings products into leaves its linear rows decide.
85
86
  export const pairedKeys = { skip: false };
86
- function openCells(rows, path) {
87
+ export function openCells(rows, path) {
87
88
  const names = [...new Set(rows.flatMap(r => Object.keys(r.lin)))].filter(isCell);
88
89
  const out = [];
89
90
  const keyText = (n) => n.slice(n.indexOf('[') + 1, -1);
@@ -255,14 +256,25 @@ export function grow(goal, path = new Map(), budget = { nodes: 0, until: clock()
255
256
  profile.goals.push({ name: goal.name, ms: performance.now() - t, nodes: profile.nodes - n0, solves: profile.solves - s0, solveMs: profile.solveMs - m0, rowsMs: profile.rowsMs - r0, proven: 'tree' in out });
256
257
  return out;
257
258
  }
259
+ // The share of a root goal's budget the CDCL search gets (GDP_CDCL_SHARE; 0 turns it off), and the quick search's nodes
260
+ // before it (GDP_QUICK_CAP; 64 where the CDCL search isn't tried, as before it): research/step's comparisons.
261
+ const proveEnv = globalThis.process?.env ?? {};
262
+ const cdclShare = Number(proveEnv.GDP_CDCL_SHARE ?? 0.75), quickCap = Number(proveEnv.GDP_QUICK_CAP ?? 16);
258
263
  function growRoot(goal, path, budget) {
264
+ // Past a few cases, CDCL over the atoms (kernel/cdcl.ts), with most of the budget: a linear goal's refutation there
265
+ // is a resolution of short clauses, which the tree search would find again in every case that reaches one. Unknown
266
+ // there, the plain search, the cuts and the plain search again have the rest. Not for a law over k steps in a row
267
+ // (kernel.ts names its goals `@k steps`): the cuts make its tree a chain of one step's refutations, which the CDCL
268
+ // search's tree, unfolded, repeats (research/step: four times the Lean text of examples/steps.ts). Nor for products.
269
+ const root = path.size === 0 && budget.nodes === 0 ? leafConstraints(goal, path) : undefined;
270
+ const cdcl = !!root && cdclShare > 0 && !cellProducts.on && Number(/@(\d+) steps/.exec(goal.name)?.[1] ?? 1) <= 1 && !root.some(r => Object.keys(r.lin).some(isMonomial));
259
271
  // At the root: plainly first; where the cases multiply past a budget, through cuts. The plain budget halves with each
260
272
  // two open choices (what a plain search's cases grow by), so a long run's steps reach the cuts early.
261
- if (path.size === 0 && budget.nodes === 0 && unitFirst && unitsNow) {
273
+ if (root && unitFirst && unitsNow) {
262
274
  // A goal that a few cases settle needs no units (finding them costs a Farkas check per atom); the rest get them.
263
275
  unitsNow = false;
264
276
  try {
265
- const quick = growPlain(goal, path, { nodes: 0, until: budget.until, cap: 64 });
277
+ const quick = growPlain(goal, path, { nodes: 0, until: budget.until, cap: cdcl ? quickCap : 64 });
266
278
  if ('tree' in quick || quick.stuck === 'sat' || quick.why === undefined)
267
279
  return quick;
268
280
  }
@@ -270,8 +282,14 @@ function growRoot(goal, path, budget) {
270
282
  unitsNow = true;
271
283
  }
272
284
  }
273
- if (path.size === 0 && budget.nodes === 0) {
274
- const open = new Set(leafConstraints(goal, path).flatMap(r => Object.keys(r.lin)).filter(n => n.startsWith('⟦') && columnTerm(n)?.op === 'if')).size;
285
+ if (root) {
286
+ if (cdcl) {
287
+ const now = clock(), end = withinDeadline(budget.until ?? Infinity);
288
+ const out = growCdcl(goal, path, { nodes: 0, until: end === Infinity ? end : now + cdclShare * (end - now) });
289
+ if ('tree' in out || out.stuck === 'sat')
290
+ return out;
291
+ }
292
+ const open = new Set(root.flatMap(r => Object.keys(r.lin)).filter(n => n.startsWith('⟦') && columnTerm(n)?.op === 'if')).size;
275
293
  const plain = growPlain(goal, path, { nodes: 0, until: budget.until, cap: Math.max(32, Math.round(4_000 / 2 ** (open / 2))) });
276
294
  if ('tree' in plain || plain.stuck === 'sat' || plain.why === undefined)
277
295
  return plain;
@@ -299,30 +317,7 @@ function growPlain(goal, path, budget) {
299
317
  const { steps, products, squares } = products0 || squares0 ? trimmed(rows.length, steps0, products0 ?? [], squares0 ?? []) : { steps: steps0, products: undefined, squares: undefined };
300
318
  return { leaf: steps.map(st => (st.kind === 'input' ? { ...st, index: at(st.index) } : st)), ...(products?.length ? { products: products.map(([i, j]) => [at(i), at(j)]) } : {}), ...(squares?.length ? { squares } : {}) };
301
319
  };
302
- // The conclusion's own atoms first (the goal's last hypothesis is the law refuted): a case it doesn't read is often
303
- // one no refutation needs, and splitting it first multiplies every case after it.
304
- const open = [];
305
- const hyps = goal.hyps, last = hyps.length - 1;
306
- let byHyp = openSeen.get(goal);
307
- if (!byHyp)
308
- openSeen.set(goal, (byHyp = new Map()));
309
- const openOf = (h) => { let seen = byHyp.get(h); if (!seen)
310
- byHyp.set(h, (seen = {})); return reusing(seen, path, p => { const out = []; openH(h, p, out); return out; }); };
311
- // Then the hypotheses about only the names the conclusion reads (an assumption about another key, a witness, can
312
- // wait: a refutation seldom needs it), then the rest, each group in the goal's order.
313
- if (unitFirst && unitsNow)
314
- open.push(...unitsOf(goal));
315
- if (last >= 0)
316
- open.push(...openOf(hyps[last]));
317
- const near = nearOf(goal);
318
- for (const [i, h] of hyps.entries())
319
- if (i !== last && near[i])
320
- open.push(...openOf(h));
321
- for (const [i, h] of hyps.entries())
322
- if (i !== last && !near[i])
323
- open.push(...openOf(h));
324
- open.push(...openCells(rows, path));
325
- const next = open.find(lin => Object.keys(lin).some(key => key !== '1') && !path.has(canon(lin)) && !path.has(canonFlip(lin)));
320
+ const next = openAt(goal, path, rows).find(lin => Object.keys(lin).some(key => key !== '1') && !path.has(canon(lin)) && !path.has(canonFlip(lin)));
326
321
  // With a case still to split, only "refuted or not" matters (a witness here goes unused): a large system is tried by
327
322
  // its Farkas rows alone, and left to the split if they don't refute it. Elimination to the last variable, which a
328
323
  // satisfiable system needs to say so, is kept for small systems and for the leaves, where its answer is the outcome.
@@ -584,7 +579,7 @@ function squareForms(rows) {
584
579
  // A leaf's rows, by its path's decisions in order (what they are a function of): a lift reads every leaf of a subtree
585
580
  // at its own path, found when it was grown, and at the path without the split, which the next lift up reads again.
586
581
  const rowsSeen = new WeakMap();
587
- function rowsAt(goal, path) {
582
+ export function rowsAt(goal, path) {
588
583
  let byPath = rowsSeen.get(goal);
589
584
  if (!byPath)
590
585
  rowsSeen.set(goal, (byPath = new Map()));
@@ -722,7 +717,7 @@ export function widen(part, whole, tree, opaque = {}, pp = new Map(), wp = pp) {
722
717
  // Whether each hypothesis reads only names the goal's conclusion (its last hypothesis) reads.
723
718
  const nearSeen = new WeakMap();
724
719
  const namesOf = (t) => (t === undefined ? [] : [...JSON.stringify(t).matchAll(/"(?:name|map)":"([^"]+)"/g)].map(m => m[1]));
725
- function nearOf(goal) {
720
+ export function nearOf(goal) {
726
721
  let out = nearSeen.get(goal);
727
722
  if (!out) {
728
723
  const theirs = new Set(namesOf(goal.hyps.at(-1)));
@@ -736,6 +731,8 @@ function nearOf(goal) {
736
731
  // GDP_UNITS=0 leaves them in the goal's order (for comparing).
737
732
  const unitFirst = globalThis.process?.env?.GDP_UNITS !== '0';
738
733
  let unitsNow = true;
734
+ // The units growPlain splits first, where it does (kernel/cdcl.ts reads them too).
735
+ export const unitAtoms = (goal) => (unitFirst && unitsNow ? unitsOf(goal) : []);
739
736
  const unitSeen = new WeakMap();
740
737
  function unitsOf(goal) {
741
738
  let out = unitSeen.get(goal);
@@ -777,6 +774,36 @@ function unitsOf(goal) {
777
774
  }
778
775
  // Each hypothesis' open atoms, as at an earlier path that decided the same atoms it reads (kernel.ts's reusing).
779
776
  const openSeen = new WeakMap();
777
+ // The atoms a split may take at the path, in the order growPlain tries them (some may be decided already). The
778
+ // conclusion's own atoms first (the goal's last hypothesis is the law refuted): a case it doesn't read is often one no
779
+ // refutation needs, and splitting it first multiplies every case after it. Then the hypotheses about only the names
780
+ // the conclusion reads (an assumption about another key, a witness, can wait: a refutation seldom needs it), then the
781
+ // rest, each group in the goal's order, then pairs of cells.
782
+ export function openAt(goal, path, rows) {
783
+ const open = [];
784
+ const hyps = goal.hyps, last = hyps.length - 1;
785
+ let byHyp = openSeen.get(goal);
786
+ if (!byHyp)
787
+ openSeen.set(goal, (byHyp = new Map()));
788
+ const openOf = (h) => { let seen = byHyp.get(h); if (!seen)
789
+ byHyp.set(h, (seen = {})); return reusing(seen, path, p => { const out = []; openH(h, p, out); return out; }); };
790
+ // (pushed one by one: a k-step unrolling's lists outgrow a spread's argument limit)
791
+ const put = (xs) => { for (const x of xs)
792
+ open.push(x); };
793
+ if (unitFirst && unitsNow)
794
+ put(unitsOf(goal));
795
+ if (last >= 0)
796
+ put(openOf(hyps[last]));
797
+ const near = nearOf(goal);
798
+ for (const [i, h] of hyps.entries())
799
+ if (i !== last && near[i])
800
+ put(openOf(h));
801
+ for (const [i, h] of hyps.entries())
802
+ if (i !== last && !near[i])
803
+ put(openOf(h));
804
+ put(openCells(rows, path));
805
+ return open;
806
+ }
780
807
  // What ran out, for a goal left unknown: its size, how deep the case splits went, and the two shapes that usually cause it.
781
808
  function outOfBudget(goal, limit, depth) {
782
809
  const steps = Number(/@(\d+) steps/.exec(goal.name)?.[1] ?? 1);
@@ -1585,7 +1612,10 @@ function proveStepLawsAsGiven(m, laws, stepLaws, base) {
1585
1612
  // trees are its input, as a machine's laws' are: checkStepLaws (the laws' usual trees again, then the step laws') runs
1586
1613
  // only where no certificate is made.
1587
1614
  const byCert = certOn() && !cyclicOn() && !why && one.length > 0 && base.checked;
1588
- const out = { proven: true, nodes, leaves, checked: byCert || checkStepLaws(m, laws, stepLaws, proof), proof };
1615
+ // (`uncovered`: why each step law left on the usual rule is, where the certificate is on: none, every step law has its
1616
+ // cyclic certificate; proveLiveness reads it)
1617
+ const left = [], miss = (sl, why) => { uncovered((sl.steps ?? 1) > 1 ? 'k-step law' : 'step law', m.name, why); left.push(why); };
1618
+ const out = { proven: true, nodes, leaves, checked: byCert || checkStepLaws(m, laws, stepLaws, proof), proof, ...(certOn() ? { uncovered: left } : {}) };
1589
1619
  if (!modes)
1590
1620
  return out;
1591
1621
  if (!why && one.length && out.checked) {
@@ -1603,7 +1633,7 @@ function proveStepLawsAsGiven(m, laws, stepLaws, base) {
1603
1633
  for (const x of all) {
1604
1634
  const c = all.length > 1 ? certCyclic('step law', m.name, translatedOf([x]), undefined, usualMs) : together;
1605
1635
  if (!c.checked)
1606
- uncovered((x.sl.steps ?? 1) > 1 ? 'k-step law' : 'step law', m.name, c.why);
1636
+ miss(x.sl, c.why);
1607
1637
  }
1608
1638
  }
1609
1639
  }
@@ -1617,11 +1647,11 @@ function proveStepLawsAsGiven(m, laws, stepLaws, base) {
1617
1647
  };
1618
1648
  const c = certCyclic('step law', m.name, undefined, searchedSteps, performance.now() - t0);
1619
1649
  for (const sl of c.checked ? [] : rest)
1620
- uncovered((sl.steps ?? 1) > 1 ? 'k-step law' : 'step law', m.name, `${restWhy(sl)}: ${c.why}`);
1650
+ miss(sl, `${restWhy(sl)}: ${c.why}`);
1621
1651
  }
1622
1652
  else if (cyclicOn() || certOn())
1623
1653
  for (const sl of rest)
1624
- uncovered((sl.steps ?? 1) > 1 ? 'k-step law' : 'step law', m.name, restWhy(sl));
1654
+ miss(sl, restWhy(sl));
1625
1655
  return out;
1626
1656
  }
1627
1657
  // Progress (kernel.ts's progressObligations): the laws proven first, then each goal, the every-key laws assumed at every
@@ -1671,25 +1701,39 @@ export function proveProgress(m, laws, p) {
1671
1701
  return found?.out ?? last;
1672
1702
  };
1673
1703
  const c = certCyclic('progress', m.name, translated, certSearch() ? allowed : undefined, performance.now() - t0);
1674
- if (!c.checked)
1675
- uncovered('progress', m.name, `a request allowed: ${c.why}`);
1704
+ if (!c.checked) {
1705
+ const why = `a request allowed: ${c.why}`;
1706
+ uncovered('progress', m.name, why);
1707
+ return { proven: true, nodes, leaves, checked, instances, trees, uncovered: why };
1708
+ }
1709
+ }
1710
+ else if (cyclicOn() || certOn()) {
1711
+ const why = alone ? 'a request allowed' : 'a request allowed and a claim after it';
1712
+ uncovered('progress', m.name, why);
1713
+ if (certOn())
1714
+ return { proven: true, nodes, leaves, checked, instances, trees, uncovered: why };
1676
1715
  }
1677
- else if (cyclicOn() || certOn())
1678
- uncovered('progress', m.name, alone ? 'a request allowed' : 'a request allowed and a claim after it');
1679
1716
  return { proven: true, nodes, leaves, checked, instances, trees };
1680
1717
  }
1681
1718
  // Liveness (kernel.ts's livenessParts): its step laws, proven together. The claim needs no more (a run takes the
1682
1719
  // request, so it is allowed); but a request never allowed would leave the claim about no run, so where a helpful request
1683
1720
  // names every input a client sends and carries no maps, it is proven allowed in its region too.
1721
+ // The certificate is its parts': the step laws' cyclic proof (translated, as any machine's step laws), and each helpful
1722
+ // request's allowed node. The claim about runs that never end is no certificate's, under either: the rank that can't
1723
+ // drop forever, the region that stays once it stops and the request that comes there are Live.lean's `leads_of_lex`,
1724
+ // read from the step laws' meaning (BridgeRules.lean's `sleads_of_holds`, of a cyclic proof's step laws through
1725
+ // `cyc_kstep_sound`) as from their usual trees (Live.lean's `sleads_sound`): one claim, its statement the same. So a
1726
+ // liveness is covered exactly where every part has its cyclic certificate (`cyclic: {kind: 'liveness', ...}`), and
1727
+ // uncovered, with the part's why, where one is on the usual rule.
1728
+ // (`uncovered`, where the certificate is on: why each part is on the usual rule; none, the liveness is covered)
1684
1729
  export function proveLiveness(m, laws, l) {
1685
- if (cyclicOn() || certOn())
1686
- uncovered('liveness', m.name, 'a claim about runs that never end');
1687
1730
  const { stay, cover, helps } = livenessParts(m, l);
1688
1731
  const a = proveStepLaws(m, laws, [stay, cover, ...helps]);
1689
1732
  if (!a.proven || !a.checked)
1690
1733
  return { proven: false, nodes: a.nodes, leaves: a.leaves, ...(a.failed ? { failed: a.failed } : {}), checked: false };
1691
1734
  const plain = m.history ? historyMachine(m).machine : m;
1692
1735
  let nodes = a.nodes, leaves = a.leaves;
1736
+ const left = [...(a.uncovered ?? []).map(why => `a step law: ${why}`)];
1693
1737
  for (const [i, h] of livenessHelpful(l).entries()) {
1694
1738
  if (m.history || plain.inputMaps?.length || !plain.assume || !plain.inputs.every(inp => inp in h.request))
1695
1739
  continue;
@@ -1698,8 +1742,39 @@ export function proveLiveness(m, laws, l) {
1698
1742
  leaves += b.leaves;
1699
1743
  if (!b.proven || !b.checked)
1700
1744
  return { proven: false, nodes, leaves, ...(b.failed ? { failed: b.failed } : {}), checked: false };
1745
+ if (b.uncovered)
1746
+ left.push(`helpful request ${i + 1} allowed: ${b.uncovered}`);
1747
+ }
1748
+ if (certOn()) {
1749
+ if (left.length)
1750
+ uncovered('liveness', m.name, left.join('; '));
1751
+ else
1752
+ cycLog({ kind: 'liveness', name: m.name, cert: true, usual: true, cyclic: true, route: 'its step laws', parts: 2 + helps.length });
1753
+ }
1754
+ else if (cyclicOn())
1755
+ uncovered('liveness', m.name, 'a claim about runs that never end');
1756
+ return { proven: true, checked: true, nodes, leaves, steps: a, ...(certOn() ? { uncovered: left } : {}) };
1757
+ }
1758
+ // A loop's halting (kernel.ts's loopObligations: its machine's laws, and its liveness's step laws, `while` failing later
1759
+ // from anywhere, every request helpful), its usual trees given: translated to one cyclic proof as proveStepLaws
1760
+ // translates a machine's step laws (core/cyclic.ts's machineNodes, the laws' nodes and the step laws' after them), the
1761
+ // proof Lean reads (lean-kernel/emit.ts's cycMachineToLean: Live.lean's loop_halts_of from BridgeRules.lean's
1762
+ // sleads_of_holds). The claim is the usual one: loop_halts_of reads the liveness's meaning, from either certificate.
1763
+ // Undefined where the cyclic certificate is off (GDP_CERT=usual; with GDP_CYCLIC=1 the cross-check runs); else checked,
1764
+ // the proof, and why not.
1765
+ export function certLoop(l, laws, rank, trees, usualMs) {
1766
+ if (!certOn() && !cyclicOn())
1767
+ return undefined;
1768
+ // (a proof made from the same loop, laws and rank before is that proof: it proves the same claim)
1769
+ const translated = (st) => translatedProof(() => JSON.stringify(['loop', loopMachine(l), laws, rank]), () => timedKind('instance', 0, () => loopNodes(l, laws, rank, trees, st)), st);
1770
+ if (!certOn()) {
1771
+ crossCyclic('loop', l.name, true, undefined, usualMs, translated);
1772
+ return undefined;
1701
1773
  }
1702
- return { proven: true, checked: true, nodes, leaves, steps: a };
1774
+ const c = certCyclic('loop', l.name, translated, undefined, usualMs);
1775
+ if (!c.checked)
1776
+ uncovered('loop', l.name, c.why);
1777
+ return c;
1703
1778
  }
1704
1779
  // The key terms a condition reads maps at; a primed map's key is a key of the map before the step too.
1705
1780
  function readKeysC(c) {
@@ -0,0 +1,79 @@
1
+ import type { C, E } from '../core/machine.js';
2
+ import { type Lin } from '../core/solver.js';
3
+ import { type Goal } from './kernel.js';
4
+ export type W = ReadonlySet<string>;
5
+ export declare const none: W;
6
+ export declare const join: (a: W, b: W) => W;
7
+ export type Trail = {
8
+ get(name: string): {
9
+ val?: boolean;
10
+ at: number;
11
+ } | undefined;
12
+ stamp(at: number): number;
13
+ size(): number;
14
+ lin(at: number): {
15
+ name: string;
16
+ lin: Lin;
17
+ holds: boolean;
18
+ };
19
+ };
20
+ type R = {
21
+ w: W;
22
+ pos: number;
23
+ fin: boolean;
24
+ };
25
+ type Kept = {
26
+ pos: number;
27
+ stamp: number;
28
+ node: number;
29
+ };
30
+ type TV = R & Kept & {
31
+ t: boolean | undefined;
32
+ };
33
+ type LV = R & Kept & {
34
+ lin: Lin | undefined;
35
+ };
36
+ type FV = Kept & {
37
+ rows: Row[];
38
+ fin: boolean;
39
+ };
40
+ export type Row = {
41
+ lin: Lin;
42
+ w: W;
43
+ label: '';
44
+ };
45
+ export declare class Reader {
46
+ readonly goal: Goal;
47
+ readonly trail: Trail;
48
+ private node;
49
+ private natRows;
50
+ private atomRows;
51
+ private ls;
52
+ private ts;
53
+ private fs;
54
+ constructor(goal: Goal, trail: Trail);
55
+ begin(): void;
56
+ private ok;
57
+ private kept;
58
+ private units;
59
+ private unit;
60
+ dec(lin: Lin): R & {
61
+ t: boolean | undefined;
62
+ };
63
+ linear(e: E): LV;
64
+ private atoms;
65
+ truth(c: C): TV;
66
+ private factsOf;
67
+ facts(i: number): FV;
68
+ termRows(name: string): Row[];
69
+ private decided;
70
+ rows(): Row[];
71
+ leafRows(): Row[];
72
+ open(i: number, out: Lin[]): void;
73
+ private openE;
74
+ private openC;
75
+ private openH;
76
+ }
77
+ export declare const namesOf: (lin: Lin) => string[];
78
+ export declare const keyLin: (cell: string) => Lin;
79
+ export {};