gdp-wired 0.22.0 → 0.23.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,6 +1,6 @@
1
1
  import { atMost, n, not, pointWrite } from "../core/machine.js";
2
2
  import { add, k, scale, sub } from "../core/solver.js";
3
- import { boundOf, canon, factorsOf, isCell, isMonomial, columnTerm, stateName, termColumn, historyMachine, foldObligations, leafConstraints, loopLiveness, loopMachine, linear, livenessHelpful, livenessParts, livenessRanks, loopObligations, trueC, obligations, progressObligations, stepLawObligations, substC, witnessesOf, withProducts } from "../kernel/kernel.js";
3
+ import { boundOf, check, checkRecur, recurGoal, canon, factorsOf, isCell, isMonomial, columnTerm, stateName, termColumn, historyMachine, foldObligations, leafConstraints, loopLiveness, loopMachine, linear, livenessHelpful, livenessParts, livenessRanks, loopObligations, trueC, obligations, progressObligations, stepLawObligations, substC, witnessesOf, withProducts } from "../kernel/kernel.js";
4
4
  import { grow, minimal, proveBuilt, proveLaws, proveStepLaws, withDeadline } from "../kernel/prove.js";
5
5
  import { refinement, refinementSides } from "../core/compose.js";
6
6
  import { leanSurface, leanTerms } from "./lean-terms.js";
@@ -226,7 +226,14 @@ export function encode(ex, tamper = false, dropStep = true, globalVars, globalMa
226
226
  return `.combine ${s.a} ${s.b} ${int(s.ma)} ${int(s.mb)}`;
227
227
  }
228
228
  const from = replay[s.of];
229
- const q = Object.fromEntries(Object.entries(from).filter(([key]) => key !== '1').map(([key, v]) => [key, v / s.g]));
229
+ // A cut's quotient is the row it divides (Cert.lean's cutOk: coefs = g · q over the integers). A tree read against a
230
+ // goal it doesn't refute can cut a row g doesn't divide: that is no derivation of this goal, and a quotient written
231
+ // as a fraction isn't Lean (Int has no fractional literal), so it is refused here, never printed.
232
+ const q = Object.fromEntries(Object.entries(from).filter(([key]) => key !== '1').map(([key, v]) => {
233
+ if (!Number.isInteger(v / s.g))
234
+ throw new Error(`${ex.name}: a cut by ${s.g} of a row it doesn't divide (${key}: ${v}); the tree doesn't refute this goal`);
235
+ return [key, v / s.g];
236
+ }));
230
237
  const cc = Math.ceil((from['1'] ?? 0) / s.g);
231
238
  replay.push({ ...q, 1: cc });
232
239
  return `.cut ${s.of} ${int(s.g)} ${sparse(dense(q))} ${int(cc)}`;
@@ -336,6 +343,61 @@ export function loopToLean(id, l, laws, rank, tamper) {
336
343
  loopObligations(l, laws, rank);
337
344
  return smachineToLean(id, loopMachine(l), laws, { liveness: [loopLiveness(l, rank)], loop: { loop: l, ...(tamper ? { tamper } : {}) } });
338
345
  }
346
+ // A loop with a run that never ends, through Kern.never_ends_of (NonTerm.lean): the loop, the recurrent set and its
347
+ // witness, the start and the stem as values, and the closure goal's table and tree; Lean evaluates the stem and builds
348
+ // the goal from the loop and the set (the certificate names no hypothesis, so the tree is read against Lean's goal).
349
+ // Each tampered certificate's check is computed to false.
350
+ export function neverEndsToLean(id, l, r, tampered = []) {
351
+ if (!checkRecur(l, r))
352
+ throw new Error(`${id}: the certificate does not check`);
353
+ const { vars, loop, recur } = recurLean(id, l, r);
354
+ return `-- ${l.name}: a run that never ends (variables ${vars.map((v2, j) => `${j} ${v2}`).join(', ')})
355
+ def ${id}_loop : Loop := ${loop}
356
+ def ${id}_recur : Recur := ${recur(r)}
357
+ /-- ${l.name} never ends: for some params meeting its precondition, a run of turns from the start goes on forever. -/
358
+ theorem ${id}_never_ends : NeverEnds ${id}_loop :=
359
+ never_ends_of ${id}_loop ${id}_recur (by ${checkBy})${tampered.map((t, j) => `
360
+ -- TAMPERED: ${t.what}. Lean computes the check to false.
361
+ def ${id}_tampered${j} : Recur := ${recur(t.recur)}
362
+ example : checkRecur ${id}_loop ${id}_tampered${j} = false := by ${checkBy}`).join('')}`;
363
+ }
364
+ // The loop and a certificate as NonTerm.lean's Loop and Recur (the domain every variable; a certificate whose tree
365
+ // doesn't refute its own goal carries `fallback`'s table and tree, which refutes `fallback`'s). lean-kernel/conformance.ts prints its cases with it.
366
+ export function recurLean(id, l, fallback) {
367
+ const vars = [...l.params, ...l.inputs, ...Object.keys(l.state)];
368
+ const { e, c } = leanTerms(vars, [], id);
369
+ const ix = (name) => { const j = vars.indexOf(name); if (j < 0)
370
+ throw new Error(`${id}: no variable for ${name}`); return j; };
371
+ const loop = `⟨${list(l.inputs.map(name => String(ix(name))))},
372
+ ${list(Object.entries(l.state).map(([name, e2]) => `(${ix(name)}, ${e(e2)})`))},
373
+ ${list(Object.entries(l.body).map(([name, e2]) => `(${ix(name)}, ${e(e2)})`))},
374
+ ${c(l.while)},
375
+ ${l.pre ? c(l.pre) : TRUE},
376
+ ${l.assume ? c(l.assume) : TRUE}⟩`;
377
+ const vals = (o) => list(Object.entries(o).map(([name, v]) => `(${ix(name)}, ${v})`));
378
+ const recur = (rr) => {
379
+ const enc = (g) => encode({ name: `${id}: ${l.name} never ends`, goal: recurGoal(l, g), tree: g.tree }, false, true, vars, []);
380
+ // The tree is written against the goal it refutes: the certificate's own where kernel.ts's check accepts it there,
381
+ // else `fallback`'s (which must check), and Lean reads it against the goal it builds from `rr` either way. So every
382
+ // number written is one kernel.ts's check replayed: integers, every cut dividing its row.
383
+ const fits = (g) => { try {
384
+ return check(recurGoal(l, g), g.tree);
385
+ }
386
+ catch {
387
+ return false;
388
+ } };
389
+ // (neither refuting its goal, as a forged certificate's: a leaf with no step, the tree that refutes nothing)
390
+ const by = fits(rr) ? rr : fits(fallback) ? fallback : undefined;
391
+ const coded = by ? enc(by) : { tab: '[]', tree: '.leafAt [] [] [] []' };
392
+ return `⟨${list(vars.map((_, j) => String(j)))},
393
+ ${vals(rr.start)},
394
+ ${list(rr.stem.map(vals))},
395
+ ${c(rr.set)},
396
+ ${list(Object.entries(rr.wit).map(([name, t]) => `(${ix(name)}, ${e(t)})`))},
397
+ (${coded.tab}, [], ${coded.tree})⟩`;
398
+ };
399
+ return { vars, loop, recur };
400
+ }
339
401
  // Folds through Kern.fold_sound: Lean builds the leaf and node goals; the trees are the kernel's.
340
402
  export function foldToLean(id, f, laws, extra = '') {
341
403
  const st = Object.keys(f.leaf);
@@ -1729,6 +1791,10 @@ function toLeanWith(items, opts) {
1729
1791
  parts.push(`-- ${item.id}\n${cyc && 'text' in cyc ? cyc.text : loopToLean(id, item.loop, item.laws ?? [], item.rank)}`);
1730
1792
  theorems.push(`${id}_halts`);
1731
1793
  }
1794
+ else if ('neverEnds' in item) {
1795
+ parts.push(`-- ${item.id}\n${neverEndsToLean(id, item.neverEnds, item.recur, item.tampered ?? [])}`);
1796
+ theorems.push(`${id}_never_ends`);
1797
+ }
1732
1798
  else if ('cyclic' in item) {
1733
1799
  parts.push(`-- ${item.id}\n${cyclicToLean(id, item.cyclic, item.tampered ?? [], item.after, item.bridge, item.recur, item.fold)}`);
1734
1800
  theorems.push(`${id}_sound`, ...(item.bridge?.laws ?? item.fold?.laws ?? []).map((_, j) => `${id}_law${j}_holds`), ...(item.recur ? [`${id}_values`] : []), ...(item.recur ? [...item.recur.lemmas.map((_, j) => `${id}_lemma${j}`), ...item.recur.between.map((_, j) => `${id}_between${j}`)] : []),
@@ -1755,7 +1821,7 @@ function toLeanWith(items, opts) {
1755
1821
  }
1756
1822
  // The file the items need: Live.lean for a liveness or a loop, Sets.lean (which imports Steps.lean) for any machine.
1757
1823
  return `import History
1758
- ${cycTexts ? 'import BridgeRules\n' : ''}${items.some(item => 'cyclic' in item) ? (items.some(item => 'cyclic' in item && (item.bridge || item.recur || item.fold)) ? 'import Bridge\n' : 'import Cyclic\n') : ''}${items.some(item => 'cyclic' in item && item.data) ? 'import Data\n' : ''}import ${items.some(item => 'refinement' in item) ? 'Refine' : items.some(item => 'machine' in item && (item.ghosts?.length || item.laws.some(l => l.some?.length))) ? 'Ghost' : items.some(item => ('machine' in item && item.liveness?.length) || 'loop' in item) ? 'Live' : items.some(item => 'machine' in item) ? 'Sets' : 'Steps'}
1824
+ ${items.some(item => 'neverEnds' in item) ? 'import NonTerm\n' : ''}${cycTexts ? 'import BridgeRules\n' : ''}${items.some(item => 'cyclic' in item) ? (items.some(item => 'cyclic' in item && (item.bridge || item.recur || item.fold)) ? 'import Bridge\n' : 'import Cyclic\n') : ''}${items.some(item => 'cyclic' in item && item.data) ? 'import Data\n' : ''}import ${items.some(item => 'refinement' in item) ? 'Refine' : items.some(item => 'machine' in item && (item.ghosts?.length || item.laws.some(l => l.some?.length))) ? 'Ghost' : items.some(item => ('machine' in item && item.liveness?.length) || 'loop' in item) ? 'Live' : items.some(item => 'machine' in item) ? 'Sets' : 'Steps'}
1759
1825
  open Kern Cert
1760
1826
  set_option maxRecDepth 100000
1761
1827
  set_option maxHeartbeats 0
@@ -1768,6 +1834,8 @@ ${parts.join('\n\n')}
1768
1834
  ${theorems.map(t => `#print axioms ${t}`).join('\n')}
1769
1835
  `;
1770
1836
  }
1837
+ // Rules over the rationals (core/rationals.ts), each through Rationals.lean's Q.machine_sound: lean-kernel/qlean.ts.
1838
+ export { rationalsToLean } from "./qlean.js";
1771
1839
  // A Lake project that builds the twin and the file: `lake build` in its folder checks every proof.
1772
1840
  export function leanProject(file, opts = {}) {
1773
1841
  const name = opts.name ?? 'Proofs';