gdp-wired 0.27.0 → 0.29.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.
@@ -29,6 +29,10 @@ export type Accelerated = {
29
29
  cycle: number[];
30
30
  locs: number[];
31
31
  targets: Target[];
32
+ cases?: {
33
+ pins: [string, number][];
34
+ cond: C;
35
+ }[];
32
36
  };
33
37
  export type AccelOptions = {
34
38
  now: () => number;
@@ -42,6 +46,7 @@ export type AccelOptions = {
42
46
  io?: StemIO;
43
47
  };
44
48
  export declare function accelerated(l: Loop, opts: AccelOptions): Generator<Accelerated>;
49
+ export declare const relationBuilt: (l: Loop) => boolean;
45
50
  type Env = Record<string, number>;
46
51
  export type StemIO = {
47
52
  start: (params: Env) => Env;
@@ -211,18 +211,18 @@ const implied = (rows, a) => solve([...rows, plus(times(a, -1), konst(1))].map(l
211
211
  // the relation's cycles, then those of its cases where a state value that a product reads with a turn's input is fixed
212
212
  // (`fixedCases`), which have 30% of the time where there are any.
213
213
  export function* accelerated(l, opts) {
214
- const rel = relOf(l), fixed = fixedCases(l), { now } = opts;
214
+ const rel = relOf(l), fixed = fixedCases(l), { now } = opts, t0 = now();
215
215
  if (rel)
216
- yield* cyclesOf(l, rel, fixed.length ? { ...opts, until: now() + (opts.until - now()) * 0.7 } : opts, []);
216
+ yield* cyclesOf(l, rel, fixed.length ? { ...opts, get until() { return t0 + (opts.until - t0) * 0.7; } } : opts, []);
217
217
  for (const f of fixed) {
218
218
  if (now() > opts.until)
219
219
  return;
220
- yield* cyclesOf(l, f.rel, { now, until: opts.until, ...(opts.maxLen ? { maxLen: opts.maxLen } : {}) }, f.pins);
220
+ yield* cyclesOf(l, f.rel, { now, get until() { return opts.until; }, ...(opts.maxLen ? { maxLen: opts.maxLen } : {}) }, f.pins);
221
221
  }
222
222
  }
223
223
  // (`pins`: rows every part of the set keeps, the fixed values of a case)
224
224
  function* cyclesOf(l, rel, opts, pins) {
225
- const { now, until } = opts, maxLen = opts.maxLen ?? 6;
225
+ const { now } = opts, maxLen = opts.maxLen ?? 6;
226
226
  const S = rel.state, P = new Set(rel.params), Sset = new Set(S), NP = rel.paths.length;
227
227
  const natState = S.filter(x => !rel.ints.has(x));
228
228
  // Each path's inputs as terms of the state before it (a few choices each, the first the likeliest).
@@ -232,7 +232,8 @@ function* cyclesOf(l, rel, opts, pins) {
232
232
  // The cycles: closed walks of paths (a location may come twice: two turns at one location, taken in turn), each once
233
233
  // (its least rotation), with at most `maxLen` choices among a location's paths (a chain of locations with one path out,
234
234
  // as a translated program's straight-line code, is free) and at most 64 turns; the shortest first.
235
- const enumerate = (ids) => {
235
+ // (`fewest`: the fewest choices a walk makes, so a deeper search finds only walks the first didn't)
236
+ const enumerate = (ids, fewest = 0, upTo = maxLen, cap = opts.maxCycles ?? 400) => {
236
237
  let cycles = [];
237
238
  // (those with an inner loop counted out apart, at most 60, after the rest)
238
239
  const counted = [];
@@ -241,7 +242,6 @@ function* cyclesOf(l, rel, opts, pins) {
241
242
  const f = from(i);
242
243
  byFrom.set(f, [...(byFrom.get(f) ?? []), i]);
243
244
  }
244
- const cap = opts.maxCycles ?? 400;
245
245
  const least = (seq) => seq.every((_, r) => r === 0 || cmp(seq, [...seq.slice(r), ...seq.slice(0, r)]) < 0);
246
246
  // (a turn costs 1 at a location of more than one path, 0 else; each location's least cost back to the cycle's start,
247
247
  // so a walk that can't come back within the budget is cut)
@@ -288,15 +288,15 @@ function* cyclesOf(l, rel, opts, pins) {
288
288
  const open = (sigma, i, k) => (i >= NP ? stepTurn(rel, natState, { inner: loops[i - NP] }, sigma) : turn(rel, natState, i, sigma, symbolic(rel.paths[i], k, Sset, P), k));
289
289
  const id0 = Object.fromEntries(S.map(x => [x, { [x]: 1 }]));
290
290
  let nodes = 20_000;
291
- for (let budget = 0; budget <= maxLen && cycles.length < cap && nodes > 0; budget++) {
292
- for (let i = 0; i < NP && cycles.length < cap && nodes > 0 && now() < until; i++) {
291
+ for (let budget = fewest; budget <= upTo && cycles.length < cap && nodes > 0; budget++) {
292
+ for (let i = 0; i < NP && cycles.length < cap && nodes > 0 && now() < opts.until; i++) {
293
293
  const s0 = from(i);
294
294
  if (!backs.has(s0))
295
295
  backs.set(s0, back(s0));
296
296
  const d = backs.get(s0);
297
297
  // (`inner`: the self-loop just taken as many times as its rows allow, not taken again at once)
298
298
  const walk = (seq, at, cost, sigma, rows, inner) => {
299
- if (--nodes < 0 || cycles.length >= cap || now() > until)
299
+ if (--nodes < 0 || cycles.length >= cap || now() > opts.until)
300
300
  return;
301
301
  if (at === s0 && cost === budget && least(seq) && sat(rows)) {
302
302
  if (!seq.some((x, q) => q > 0 && x === seq[q - 1] && to(x) === from(x) && x < NP))
@@ -353,7 +353,7 @@ function* cyclesOf(l, rel, opts, pins) {
353
353
  rel.paths.forEach((_, i) => { byLoc.set(from(i), [...(byLoc.get(from(i)) ?? []), i]); });
354
354
  let budget = 20_000;
355
355
  for (const r of opts.runs) {
356
- if (now() > until || budget <= 0)
356
+ if (now() > opts.until || budget <= 0)
357
357
  break;
358
358
  let st = opts.io.start(r.params);
359
359
  const seq = [];
@@ -423,13 +423,13 @@ function* cyclesOf(l, rel, opts, pins) {
423
423
  sets.push(...lookahead(rel, seq.map((pi, j) => stepOf(pi, firsts[j])), natState, Sset, P));
424
424
  }
425
425
  for (const ins of sets) {
426
- if (now() > until)
426
+ if (now() > opts.until)
427
427
  return;
428
428
  const key = `${seq.join(',')}|${JSON.stringify(ins)}`;
429
429
  if (tried.has(key))
430
430
  continue;
431
431
  tried.add(key);
432
- const out = candidate(rel, seq.map((pi, j) => stepOf(pi, ins[j])), natState, l, () => now() > until, pins);
432
+ const out = candidate(rel, seq.map((pi, j) => stepOf(pi, ins[j])), natState, l, () => now() > opts.until, pins);
433
433
  if (out)
434
434
  yield { ...out, cycle: seq, locs: seq.map(from) };
435
435
  }
@@ -438,13 +438,13 @@ function* cyclesOf(l, rel, opts, pins) {
438
438
  };
439
439
  // The relation's cycles, then the cycles with an inner loop taken to its end, then the lookahead's choices on the first
440
440
  yield* tryAll(cycles, 'no');
441
- if (now() > until)
441
+ if (now() > opts.until)
442
442
  return;
443
443
  // The inner loops: a self-loop or a cycle of two turns at a location whose turn, with its inputs' choices, is a
444
444
  // translation with a metering function (core/accel.ts's `accelerate`), taken to its end as one step of a walk; each
445
445
  // named NP + k among the paths.
446
446
  let tries = 2000;
447
- for (let i = 0; i < NP && loops.length < 40 && tries > 0 && now() < until; i++) {
447
+ for (let i = 0; i < NP && loops.length < 40 && tries > 0 && now() < opts.until; i++) {
448
448
  const at = rel.loc ? rel.from[i] : 0;
449
449
  if (readsPi(rel.paths[i]))
450
450
  continue;
@@ -466,9 +466,14 @@ function* cyclesOf(l, rel, opts, pins) {
466
466
  }
467
467
  if (loops.length)
468
468
  yield* tryAll(enumerate(NP + loops.length).filter(c => c.some(i => i >= NP)), 'yes');
469
- if (now() < until)
469
+ if (now() < opts.until)
470
470
  yield* tryAll(cycles, 'only');
471
+ // Deeper, in the time left: walks of up to 16 choices, at most 5,000 of them (hqr's cycle of 25 turns makes 16), each
472
+ // tried with its first inputs.
473
+ if (now() < opts.until && maxLen < DEEP)
474
+ yield* tryAll(enumerate(NP, maxLen + 1, DEEP, 5000), 'no');
471
475
  }
476
+ const DEEP = 16;
472
477
  // The choices one input at a time of a turn's that the rest of the cycle bounds: the cycle from that turn on (its other
473
478
  // inputs as chosen), the bounds its rows put on the input, as terms of the state at that turn (x ≤ u gives x), the
474
479
  // first three that leave the rows satisfiable; at most 12.
@@ -510,6 +515,9 @@ function pathAt(rel, among, st, input) {
510
515
  const raws = new WeakMap(), rels = new WeakMap();
511
516
  const rawOf = (l) => { if (!raws.has(l))
512
517
  raws.set(l, relationOf(l)); return raws.get(l); };
518
+ // The loop's relation and its cases with a value fixed, built (once a loop): a caller may build them before the search,
519
+ // and not count the time against it.
520
+ export const relationBuilt = (l) => { relOf(l); fixedCases(l); return !!rels.get(l); };
513
521
  function relOf(l) {
514
522
  if (!rels.has(l))
515
523
  rels.set(l, kept(l, rawOf(l)));
@@ -1021,7 +1029,7 @@ function candidate(rel, steps, natState, l, late, pins) {
1021
1029
  e = { op: 'if', c: conds[j], then: vals[j], else: e };
1022
1030
  wit[n] = e;
1023
1031
  }
1024
- return { set, wit, targets: parts.map(p => ({ loc: p.loc, rows: p.rows })) };
1032
+ return { set, wit, targets: parts.map(p => ({ loc: p.loc, rows: p.rows })), ...(rel.loc ? { cases: parts.map((p, j) => ({ pins: [[rel.loc, p.loc]], cond: full[j] })) } : {}) };
1025
1033
  }
1026
1034
  const cmp = (a, b) => { for (let i = 0; i < a.length; i++)
1027
1035
  if (a[i] !== b[i])
@@ -4,6 +4,7 @@ import type { Liveness, Loop, Tree } from '../kernel/kernel.js';
4
4
  import { type Cyclic, type Fact } from '../kernel/cyclic.js';
5
5
  import { type NodeSpec } from './cyclic.js';
6
6
  import { type Lin } from './solver.js';
7
+ import { inferLaws } from './infer.js';
7
8
  import { type Relation } from './relation.js';
8
9
  import type { StepLaw } from '../kernel/kernel.js';
9
10
  export type Claim = {
@@ -109,6 +110,7 @@ export declare function findRank(l: Loop, laws?: Law[], opts?: {
109
110
  export type Sampler = {
110
111
  params: (rand: (n: number) => number, bound: number) => Record<string, number>;
111
112
  input: (rand: (n: number) => number, bound: number, ps: Record<string, number>) => Request;
113
+ rule?: Parameters<typeof inferLaws>[0];
112
114
  };
113
115
  export type LeadsVerdict = {
114
116
  proven: boolean;
@@ -1,5 +1,5 @@
1
1
  import { argsKey, atMost as le, compileFunction, evalC as evalMC, evalE as evalME, fnValue, get, interpret, minus as sub, n as k, pairedArgs, plus as add, same as eqC, seeded, walk, x as v } from "./machine.js";
2
- import { check, livenessHelpful, livenessParts, loopLiveness, loopMachine, loopObligations, substC, substE } from "../kernel/kernel.js";
2
+ import { check, livenessHelpful, livenessParts, loopLiveness, loopMachine, loopObligations, strongly, substC, substE } from "../kernel/kernel.js";
3
3
  import { checkCyclic, factsAvoid, lexLt } from "../kernel/cyclic.js";
4
4
  import { and, proveCyclic, vars as vs } from "./cyclic.js";
5
5
  import { canon, env, gcd, konst, pivot, plus, ratioRow, refuteFast, solve, subst, times, vars, work } from "./solver.js";
@@ -1790,7 +1790,7 @@ export function findRank(l, laws = [], opts = {}) {
1790
1790
  }
1791
1791
  export function findLeadsTo(m, laws, l, sample, opts = {}) {
1792
1792
  const t0 = clock(), until = t0 + (opts.ms ?? 10_000), rand = seeded(29), run = interpret(m);
1793
- const names = [...Object.keys(m.state), ...m.params].sort(), helpful = livenessHelpful(l);
1793
+ const names = [...Object.keys(m.state), ...m.params].sort(), helpful = livenessHelpful(l), fair = strongly(l);
1794
1794
  const node = { name: l.name, hyps: [], concl: yes, vars: names, level: 0, given: true };
1795
1795
  const env = (ps, st) => ({ ...ps, ...st.scalars });
1796
1796
  const holds = (c, ps, st) => { try {
@@ -1801,6 +1801,9 @@ export function findLeadsTo(m, laws, l, sample, opts = {}) {
1801
1801
  } };
1802
1802
  const pending = (ps, st) => holds(l.from, ps, st) === true && holds(l.to, ps, st) === false;
1803
1803
  const points = new Map(), strong = new Map(), weak = new Map();
1804
+ // (under strong fairness, the helpful steps that stay pending, by the regions they land in, and the regions whose
1805
+ // helpful step reached `to`)
1806
+ const handed = [], finishes = new Set();
1804
1807
  for (const bound of [2, 4, 8, 16])
1805
1808
  for (let trial = 0; trial < 20 && clock() < t0 + 1000; trial++) {
1806
1809
  let ps;
@@ -1840,11 +1843,35 @@ export function findLeadsTo(m, laws, l, sample, opts = {}) {
1840
1843
  break;
1841
1844
  if (was && pending(ps, next)) {
1842
1845
  const e = { from: 0, to: 0, s: here, t: env(ps, next) };
1843
- (h ? strong : weak).set(id([e.s, e.t]), e);
1846
+ if (h && fair)
1847
+ handed.push({ e, from: helpful.indexOf(h), to: helpful.flatMap((g, j) => (g !== h && holds(g.when, ps, next) === true ? [j] : [])) });
1848
+ else
1849
+ (h ? strong : weak).set(id([e.s, e.t]), e);
1844
1850
  }
1851
+ else if (was && h && fair)
1852
+ finishes.add(helpful.indexOf(h));
1845
1853
  st = next;
1846
1854
  }
1847
1855
  }
1856
+ // Under strong fairness a helpful step may hand over to the region of a higher level instead of lowering the rank
1857
+ // (kernel.ts's livenessParts; the levels the kernel's search chooses, kernel/prove.ts's chooseLevels): the levels
1858
+ // read off the runs, a region's the fewer hand-overs from it to one whose helpful step reached `to`, so a step that
1859
+ // hands over closer only keeps the rank and every other must lower it (BullMQ's fetch, wait to active, and promote,
1860
+ // delayed to wait, keep it; a retry, active to wait, lowers it).
1861
+ if (fair) {
1862
+ const dist = helpful.map((_, i) => (finishes.has(i) ? 0 : Infinity));
1863
+ for (let changed = true; changed;) {
1864
+ changed = false;
1865
+ for (const x of handed)
1866
+ for (const j of x.to)
1867
+ if (dist[j] + 1 < dist[x.from]) {
1868
+ dist[x.from] = dist[j] + 1;
1869
+ changed = true;
1870
+ }
1871
+ }
1872
+ for (const x of handed)
1873
+ (x.to.some(j => dist[j] < dist[x.from]) ? weak : strong).set(id([x.e.s, x.e.t]), x.e);
1874
+ }
1848
1875
  // The trivial ranks first, where the runs allow them: the constant 0 (every helpful step in its region reaches `to` at
1849
1876
  // once: none sampled stays pending, which leaves the fit below nothing to fit), then each state variable alone (every
1850
1877
  // helpful step sampled that stays pending lowers it, and no other raises it), up to four of them. Rank 0 may take half
@@ -1876,7 +1903,7 @@ export function findLeadsTo(m, laws, l, sample, opts = {}) {
1876
1903
  return { proven: true, checked: true, rank, laws: [], found: { rank: text, laws: [] } };
1877
1904
  const ms = until - clock();
1878
1905
  if (ms > 300) {
1879
- const { stay, cover, helps } = livenessParts(m, lv), inf = inferLaws(m, laws, [stay, cover, ...helps], { ms });
1906
+ const { stay, cover, helps } = livenessParts(m, lv), inf = inferLaws(sample.rule ?? m, laws, [stay, cover, ...helps], { ms });
1880
1907
  const kept = inf.found.filter(f => !f.law.forall && !f.law.keys?.length), more = kept.map(f => f.law);
1881
1908
  if (inf.proven && more.length) {
1882
1909
  const again = withDeadline(until + 5000, () => proveLiveness(m, [...laws, ...more], lv));
@@ -248,6 +248,10 @@ export type Measure = {
248
248
  comps: {
249
249
  coef: number[];
250
250
  c: number;
251
+ fold?: {
252
+ fn: string;
253
+ of: number;
254
+ };
251
255
  }[];
252
256
  };
253
257
  export type Termination = {
@@ -435,6 +439,7 @@ export declare const lemmaCaps: {
435
439
  perCase: number;
436
440
  spare: number;
437
441
  joined: number;
442
+ self: number;
438
443
  };
439
444
  export declare function conjectures(p: Program, l: DataLaw, max?: number, maxSize?: number): DataLaw[];
440
445
  export declare function calculated(p: Program, l: DataLaw, depth?: number, max?: number): DataLaw[];