gdp-wired 0.36.0 → 0.38.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.
package/dist/core/live.js CHANGED
@@ -367,13 +367,21 @@ export function refutedBy(m, laws, l, ring) {
367
367
  const want = l.fairness === 'strong' ? 'strong' : 'weak', out = [...fair];
368
368
  return { refuted: fair.has(want), fair: out, ...(fair.has(want) ? {} : { why: `the run is not ${want}ly fair` }) };
369
369
  }
370
- // A ring of one set, found: a state the runs reach where `from` holds and `to` doesn't and the helpful requests taken in
371
- // turn keep it so; the set, the literals of the rule's comparisons (its guards and steps, also as each helpful request
372
- // meets them, `from`, `to`, the regions) true there, the ring's requests the helpful requests allowed there. Literals a
373
- // request doesn't keep are dropped (each kept literal proven by the kernel after each request, as Houdini drops them)
374
- // until the rest are kept; then refutedBy checks the ring. Undefined where none is found.
370
+ // A lasso, found: a stem from the start, then a cycle of k steps the runs take, each from a state where `from` holds and
371
+ // `to` doesn't. The runs: from each state a random stem reaches, a walk of up to 3,000 steps, each a helpful request
372
+ // allowed there (in turn: the time passing in them, every deadline small params allow passed) or, in every other walk,
373
+ // half of them any request (drawn as the runs draw them, each input a helpful request names at its value there with odds
374
+ // of a half: the environment acting on the same key, a crash between two helpful steps). A state's abstraction is the
375
+ // literals of the rule's comparisons (its guards and steps, also as each helpful request meets them, `from`, `to`, the
376
+ // regions) true there; a step back to an abstraction the walk had (in its last 64 steps) closes a cycle, taken where the
377
+ // walk since then is fair to each helpful request as the law asks (weakly: taken, or not allowed at a state of it;
378
+ // strongly: taken in its region, or allowed there at none), the shortest, and where it runs again for 3,000 steps through
379
+ // the same abstractions (a cycle a deadline ends is none). Its sets R_0..R_{k-1} are the abstractions of
380
+ // its states, its requests the walk's (an input a helpful request's term where it has that value, else its value); a
381
+ // literal of R_{j+1} that R_j's request isn't proven to keep is dropped (Houdini, round the cycle) until each is kept;
382
+ // then refutedBy checks the ring. The first ring refuted; else the first checked; undefined where no cycle is found.
375
383
  export function findRing(m, laws, l, draws, opts = {}) {
376
- const until = clock() + (opts.ms ?? 10_000), rand = seeded(17), run = interpret(m), helpful = livenessHelpful(l);
384
+ const until = clock() + (opts.ms ?? 10_000), rand = seeded(17), run = interpret(m), helpful = livenessHelpful(l), H = helpful.length;
377
385
  const env = (ps, st) => ({ ...ps, ...st.scalars });
378
386
  const holds = (c, ps, st) => { try {
379
387
  return evalC(c, env(ps, st), st, m);
@@ -381,19 +389,149 @@ export function findRing(m, laws, l, draws, opts = {}) {
381
389
  catch {
382
390
  return undefined;
383
391
  } };
392
+ const value = (e, ps, st) => { try {
393
+ return evalE(e, env(ps, st), st, m);
394
+ }
395
+ catch {
396
+ return NaN;
397
+ } };
384
398
  const pending = (ps, st) => holds(l.from, ps, st) === true && holds(l.to, ps, st) === false;
385
399
  const full = (r) => Object.fromEntries(m.inputs.map(i => [i, r[i] ?? k(0)]));
386
400
  const concrete = (r, ps, st) => Object.fromEntries(Object.entries(full(r)).map(([i, t]) => [i, evalE(t, env(ps, st), st, m)]));
387
- // A state where `from` holds and `to` doesn't, then the helpful requests allowed taken in turn, up to 3,000 steps (the
388
- // time passing in them, every deadline small params allow passed), never reaching `to`: the state they end at.
389
- let found;
390
- const allowedAt = (ps, st) => helpful.flatMap((h, i) => { try {
391
- return allowed(m, run, ps, st, concrete(h.request, ps, st)) ? [i] : [];
392
- }
393
- catch {
394
- return [];
401
+ const atoms = new Map();
402
+ const add = (t) => walk(t, o => { if ((o.op === '<' || o.op === '<=' || o.op === '=') && typeof o.a === 'object' && typeof o.b === 'object') {
403
+ const c = o;
404
+ if (!readsC(c, m.inputs) && !constant(c) && !id(c).includes('"op":"flag"'))
405
+ atoms.set(id(c), c);
395
406
  } });
396
- for (let trial = 0; trial < 400 && !found && clock() < until; trial++) {
407
+ add([l.from, l.to, helpful.map(h => h.when), m.assume, m.step]);
408
+ for (const h of helpful) {
409
+ const sb = { vars: full(h.request), maps: {}, flags: {} };
410
+ if (m.assume)
411
+ add(substC(m.assume, sb));
412
+ for (const e of Object.values(m.step.state ?? {}))
413
+ add(substE(e, sb));
414
+ }
415
+ const lits = [...atoms.values()], litsAt = (ps, st) => lits.flatMap(c => { const b = holds(c, ps, st); return b === undefined ? [] : [b ? c : { op: 'not', x: c }]; });
416
+ const keyAt = (ps, st) => lits.map(c => { const b = holds(c, ps, st); return b === undefined ? 2 : +b; }).join('');
417
+ const after = { vars: Object.fromEntries(Object.keys(m.state).map(x => [x, v(`${x}'`)])), maps: Object.fromEntries(Object.keys(m.maps ?? {}).map(map => [map, (key) => ({ op: 'get', map: `${map}'`, key })])), flags: {} };
418
+ const fair = (cyc) => helpful.every((_, h) => (l.fairness === 'strong'
419
+ ? cyc.some(a => a.taken[h] && a.region[h]) || cyc.every(a => !(a.allows[h] && a.region[h]))
420
+ : cyc.some(a => a.taken[h] || !a.allows[h])));
421
+ // a request as terms at a state
422
+ const termsOf = (req, ps, st) => Object.fromEntries(m.inputs.map(i => [i, helpful.map(h => h.request[i]).find(t => t && value(t, ps, st) === req[i]) ?? k(req[i])]));
423
+ const kept = (when, then, request) => {
424
+ const p = withDeadline(Math.min(until, clock() + 2000), () => proveProgress(m, laws, { name: 'kept', when: { op: 'and', xs: when }, then: { op: 'and', xs: then.map(c => substC(c, after)) }, request: full(request) }));
425
+ return p.proven && p.checked;
426
+ };
427
+ const check = (ps, stem, cyc, reqs) => {
428
+ const sets = cyc.map(a => litsAt(ps, a.st));
429
+ for (let round = 0, changed = true; changed && round < 8 && clock() < until; round++) {
430
+ changed = false;
431
+ for (let j = 0; j < sets.length; j++) {
432
+ const u = (j + 1) % sets.length;
433
+ if (kept(sets[j], sets[u], reqs[j]))
434
+ continue;
435
+ const keep = sets[u].filter(c => kept(sets[j], [c], reqs[j]));
436
+ if (!keep.length)
437
+ return undefined;
438
+ sets[u] = keep;
439
+ changed = true;
440
+ }
441
+ }
442
+ const ring = { params: ps, stem, sets: sets.map((s, j) => ({ holds: { op: 'and', xs: s }, request: reqs[j] })) };
443
+ return { ring, verdict: refutedBy(m, laws, l, ring) };
444
+ };
445
+ // the cycle run again from where it closed, its requests as terms, for 3,000 steps: each allowed, pending after, at
446
+ // the abstraction it had (so the time passes every deadline small params allow: a cycle a deadline ends is none)
447
+ const repeats = (ps, st, cyc, reqs) => {
448
+ try {
449
+ for (let n = 0; n < 3000;)
450
+ for (const [j, a] of cyc.entries()) {
451
+ const q = concrete(reqs[j], ps, st);
452
+ if (keyAt(ps, st) !== a.key || !allowed(m, run, ps, st, q))
453
+ return false;
454
+ st = run.step(ps, st, q);
455
+ n++;
456
+ if (!pending(ps, st))
457
+ return false;
458
+ }
459
+ return true;
460
+ }
461
+ catch {
462
+ return false;
463
+ }
464
+ };
465
+ const tried = new Set();
466
+ let first;
467
+ const walkFrom = (ps, st, noisy, stem) => {
468
+ const path = [], seen = new Map();
469
+ let key = keyAt(ps, st);
470
+ for (let n = 0, turn = 0; n < 3000 && clock() < until; n++) {
471
+ const allows = helpful.map(h => { try {
472
+ return allowed(m, run, ps, st, concrete(h.request, ps, st));
473
+ }
474
+ catch {
475
+ return false;
476
+ } });
477
+ let req;
478
+ if (allows.some(Boolean) && (!noisy || rand(2))) {
479
+ const i = [...allows.keys()].find(i => i >= turn && allows[i]) ?? allows.indexOf(true);
480
+ turn = (i + 1) % H;
481
+ req = concrete(helpful[i].request, ps, st);
482
+ }
483
+ else
484
+ for (let tries = 0; tries < 20 && !req; tries++) {
485
+ try {
486
+ const r = draws.input(rand, 4, ps), h = helpful[rand(H)];
487
+ if (h)
488
+ for (const [x, t] of Object.entries(h.request))
489
+ if (rand(2))
490
+ r[x] = evalE(t, env(ps, st), st, m);
491
+ if (allowed(m, run, ps, st, r))
492
+ req = r;
493
+ }
494
+ catch { /* refused: another */ }
495
+ }
496
+ if (!req)
497
+ return undefined;
498
+ const r = req, at = { st, req: r, key, allows,
499
+ taken: helpful.map(h => Object.entries(h.request).every(([x, t]) => value(t, ps, st) === r[x])), region: helpful.map(h => holds(h.when, ps, st) === true) };
500
+ let next;
501
+ try {
502
+ next = run.step(ps, st, r);
503
+ }
504
+ catch {
505
+ return undefined;
506
+ }
507
+ if (!pending(ps, next))
508
+ return undefined;
509
+ seen.set(at.key, [...(seen.get(at.key) ?? []), path.length]);
510
+ path.push(at);
511
+ st = next;
512
+ key = keyAt(ps, st);
513
+ for (const i of [...(seen.get(key) ?? [])].reverse()) {
514
+ if (path.length - i > 64)
515
+ break;
516
+ const cyc = path.slice(i);
517
+ if (!fair(cyc))
518
+ continue;
519
+ const reqs = cyc.map(a => termsOf(a.req, ps, a.st)), shape = id([cyc.map(a => a.key), reqs]);
520
+ if (!tried.has(shape)) {
521
+ tried.add(shape);
522
+ if (!repeats(ps, st, cyc, reqs))
523
+ break;
524
+ const out = check(ps, [...stem, ...path.slice(0, i).map(a => a.req)], cyc, reqs);
525
+ if (out?.verdict.refuted)
526
+ return out;
527
+ first ??= out;
528
+ }
529
+ break;
530
+ }
531
+ }
532
+ return undefined;
533
+ };
534
+ for (let trial = 0; trial < 400 && clock() < until; trial++) {
397
535
  let ps, st;
398
536
  try {
399
537
  ps = draws.params(rand, 2 + (trial % 3) * 2);
@@ -405,27 +543,11 @@ export function findRing(m, laws, l, draws, opts = {}) {
405
543
  continue;
406
544
  }
407
545
  const stem = [];
408
- for (let turn = 0; turn < 40; turn++) {
546
+ for (let turn = 0; turn < 40 && clock() < until; turn++) {
409
547
  if (turn > 0 && pending(ps, st)) {
410
- let s2 = st;
411
- const laps = [];
412
- for (let lap = 0; lap < 1000 && s2 && laps.length < 3000; lap++)
413
- for (const i of allowedAt(ps, s2)) {
414
- if (!s2)
415
- break;
416
- const req = concrete(helpful[i].request, ps, s2);
417
- if (!allowed(m, run, ps, s2, req))
418
- continue;
419
- s2 = run.step(ps, s2, req);
420
- laps.push(req);
421
- if (!pending(ps, s2))
422
- s2 = undefined;
423
- }
424
- const hs = s2 ? allowedAt(ps, s2) : [];
425
- if (s2 && hs.length) {
426
- found = { ps, stem: [...stem, ...laps], st: s2, hs };
427
- break;
428
- }
548
+ const out = walkFrom(ps, st, trial % 2 === 1, stem);
549
+ if (out)
550
+ return out;
429
551
  }
430
552
  try {
431
553
  const req = draws.input(rand, 4, ps);
@@ -437,34 +559,7 @@ export function findRing(m, laws, l, draws, opts = {}) {
437
559
  catch { /* refused: another */ }
438
560
  }
439
561
  }
440
- if (!found)
441
- return undefined;
442
- // the literals there
443
- const atoms = new Map();
444
- const add = (t) => walk(t, o => { if ((o.op === '<' || o.op === '<=' || o.op === '=') && typeof o.a === 'object' && typeof o.b === 'object') {
445
- const c = o;
446
- if (!readsC(c, m.inputs) && !id(c).includes('"op":"flag"'))
447
- atoms.set(id(c), c);
448
- } });
449
- add([l.from, l.to, helpful.map(h => h.when), m.assume, m.step]);
450
- for (const h of helpful) {
451
- const sb = { vars: full(h.request), maps: {}, flags: {} };
452
- if (m.assume)
453
- add(substC(m.assume, sb));
454
- for (const e of Object.values(m.step.state ?? {}))
455
- add(substE(e, sb));
456
- }
457
- let lits = [...atoms.values()].flatMap(c => { const b = holds(c, found.ps, found.st); return b === undefined ? [] : [b ? c : { op: 'not', x: c }]; });
458
- const after = { vars: Object.fromEntries(Object.keys(m.state).map(x => [x, v(`${x}'`)])), maps: Object.fromEntries(Object.keys(m.maps ?? {}).map(map => [map, (key) => ({ op: 'get', map: `${map}'`, key })])), flags: {} };
459
- for (let round = 0; round < 8 && clock() < until; round++) {
460
- const R = { op: 'and', xs: lits };
461
- const kept = lits.filter(c => found.hs.every(i => { const p = withDeadline(Math.min(until, clock() + 2000), () => proveProgress(m, laws, { name: 'kept', when: R, then: substC(c, after), request: full(helpful[i].request) })); return p.proven && p.checked; }));
462
- if (kept.length === lits.length)
463
- break;
464
- lits = kept;
465
- }
466
- const ring = { params: found.ps, stem: found.stem, sets: found.hs.map(i => ({ holds: { op: 'and', xs: lits }, request: helpful[i].request })) };
467
- return { ring, verdict: refutedBy(m, laws, l, ring) };
562
+ return first;
468
563
  }
469
564
  const readsC = (c, names) => { let hit = false; walk(c, o => { if (o.op === 'var' && names.includes(o.name))
470
565
  hit = true; }); return hit; };
@@ -177,6 +177,10 @@ export type SomeProof = {
177
177
  body: C;
178
178
  sub?: Record<string, E>;
179
179
  ghost?: true;
180
+ map?: {
181
+ name: string;
182
+ at: E;
183
+ };
180
184
  };
181
185
  export { unreachable } from '../kernel/kernel.js';
182
186
  export declare const x: (name: string) => E;
@@ -386,8 +386,6 @@ export function compileC(c, m) {
386
386
  function writeEvery(w, env, st, m) {
387
387
  if (!plainKey(w.value, w.each)) {
388
388
  const ks = breakKeys(breaksOf(w.value, w.each), env, st, m), top = ks.at(-1);
389
- if (top > 1_000_000)
390
- throw new RangeError(`a write at every key reads its key in arithmetic past a million keys (${top})`);
391
389
  // (each breakpoint on its own, and each stretch between two at one key of it: no read moves and no comparison turns
392
390
  // inside one, so it holds one value; most stretches hold the default, and a stretch that doesn't is filled)
393
391
  const at = (key) => evalE(w.value, { ...env, [w.each]: key }, st, m);
@@ -398,9 +396,12 @@ function writeEvery(w, env, st, m) {
398
396
  cells.set(key, v);
399
397
  if (ks[i + 1] > key + 1) {
400
398
  const u = at(key + 1);
401
- if (u !== fallback)
399
+ if (u !== fallback) {
400
+ if (ks[i + 1] - key > 1_000_000)
401
+ throw new RangeError(`a write at every key fills a stretch of more than a million keys (${key + 1} to ${ks[i + 1] - 1})`);
402
402
  for (let k2 = key + 1; k2 < ks[i + 1]; k2++)
403
403
  cells.set(k2, u);
404
+ }
404
405
  }
405
406
  }
406
407
  return { cells, default: fallback };
@@ -607,7 +608,9 @@ const RUNTIME = [
607
608
  // A condition at every two keys that holds wherever x(k) differs from y(k1): only the pairs whose values match are read.
608
609
  ['$join', `function $join(ks, x, y, f) { const by = new Map(); for (const k1 of ks) { const v = y(k1), l = by.get(v); if (l) l.push(k1); else by.set(v, [k1]); } return ks.every(k => { const l = by.get(x(k)); return l === undefined || l.every(k1 => f(k, k1)); }); }`],
609
610
  ['$spread', `function $spread(keys, n) { const out = new Set(); for (const k of keys) for (let i = 0; i < n; i++) out.add(k + i); return [...out].sort((a, b) => a - b); }`],
610
- ['$dense', `function $dense(keys, f) { const top = keys[keys.length - 1]; if (top > 1000000) throw new RangeError('a write at every key reads its key in arithmetic past a million keys (' + top + ')'); const d = f(top), cells = new Map(); for (let k = 0; k < top; k++) { const v = f(k); if (v !== d) cells.set(k, v); } return {cells, d}; }`],
611
+ // (the interpreter's writeEvery: each breakpoint, and each stretch between two at one key of it, filled only where it
612
+ // holds other than the value past the last)
613
+ ['$dense', `function $dense(keys, f) { const top = keys[keys.length - 1], d = f(top), cells = new Map(); for (let i = 0; i + 1 < keys.length; i++) { const key = keys[i], v = f(key); if (v !== d) cells.set(key, v); if (keys[i + 1] > key + 1) { const u = f(key + 1); if (u !== d) { if (keys[i + 1] - key > 1000000) throw new RangeError('a write at every key fills a stretch of more than a million keys (' + (key + 1) + ' to ' + (keys[i + 1] - 1) + ')'); for (let k2 = key + 1; k2 < keys[i + 1]; k2++) cells.set(k2, u); } } } return {cells, d}; }`],
611
614
  ['$every', `function $every(keys, f) { const past = keys[keys.length - 1], d = f(past), cells = new Map(); for (let i = 0; i < keys.length - 1; i++) { const v = f(keys[i]); if (v !== d) cells.set(keys[i], v); } return {cells, d}; }`],
612
615
  ];
613
616
  function runtimeFor(code) {
@@ -12,7 +12,7 @@ import { someHolds } from "./exists.js";
12
12
  export const isStepLaw = (w) => 'then' in w;
13
13
  // A request's environment: the params, its inputs and queries, and the step's flags decided (as ?name).
14
14
  export function requestEnv(m, run, params, st, input) {
15
- const env = { ...params, ...numbersOf(input), ...run.queries(params, st, input) }, seen = carriedView(m, st, input);
15
+ const env = { ...params, ...numbersOf(input), ...run.queries(params, st, input) }, seen = carriedView(m, st, input, undefined, params);
16
16
  for (const [name, c] of Object.entries(m.step.flags ?? {}))
17
17
  env[`?${name}`] = evalC(c, env, seen, m) ? 1 : 0;
18
18
  return env;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gdp-wired",
3
- "version": "0.36.0",
3
+ "version": "0.38.0",
4
4
  "description": "An automatic prover for stateful rules (budgets, caps, windows, workflows, policies): laws proven with no proof to write, checked again in Lean, counterexamples found on the GPU, the rule shipped as code",
5
5
  "keywords": [
6
6
  "verification",