gdp-wired 0.35.0 → 0.36.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.
@@ -70,6 +70,7 @@ export declare const kindOfVar: (v: Var, sorts: Sorts, at?: string) => Kind<unkn
70
70
  export declare function ruleSnap(r: Rule, m: Model, st: State, sorts: Sorts): Snap;
71
71
  export declare function ruleState(base: State, s: Snap, m: Model, sorts: Sorts): State;
72
72
  export declare function constantsOf(m: Model): number[];
73
+ export declare function stringsOf(m: Model): string[];
73
74
  export declare function draw(sort: Var['sort'], sorts: Sorts, name: string, rand: Rand, near: number[], bound: number, strs: string[]): Raw;
74
75
  export type CheckOptions = {
75
76
  runs?: number;
@@ -334,11 +334,18 @@ const cellKind = (d, sorts) => record({ has: bool, ...Object.fromEntries(d.field
334
334
  const BIG = [Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER - 1, 2 ** 31, 2 ** 32];
335
335
  export function constantsOf(m) {
336
336
  const out = new Set([0, 1]);
337
- walk([m.writes, m.mapWrites, m.refuse, m.pre, m.init], o => { if (o.op === 'const' && typeof o.v === 'number')
337
+ walk([m.writes, m.mapWrites, m.refuse, m.pre, m.init, m.folds ?? []], o => { if (o.op === 'const' && typeof o.v === 'number')
338
338
  out.add(o.v); if (typeof o.k === 'number')
339
339
  out.add(o.k); });
340
340
  return [...out];
341
341
  }
342
+ // The strings drawn: a few, and the literals the rule compares with.
343
+ export function stringsOf(m) {
344
+ const out = new Set(['a', 'b', 'c', '']);
345
+ walk([m.writes, m.mapWrites, m.refuse, m.folds ?? []], o => { if (o.op === 'slit' && typeof o.value === 'string')
346
+ out.add(o.value); });
347
+ return [...out];
348
+ }
342
349
  export function draw(sort, sorts, name, rand, near, bound, strs) {
343
350
  switch (sort.k) {
344
351
  case 'bool': return rand(2) === 1;
@@ -447,7 +454,7 @@ export function drive(m, r, sorts, code, opts = {}) {
447
454
  const isClass = m.shape === 'class';
448
455
  const interp = r.interpreter(), gen = r.compiled(), machine = r.machine;
449
456
  const ev = { runs: 0, requests: 0, fromStates: 0, probes: { conditions: 0, witnesses: 0, steps: 0 }, refusedBoth: 0, outside: 0, edge: 0, compiledAgreed: 0, coverage: { arms: 0, hit: 0, missed: [] }, ms: 0 };
450
- const consts = constantsOf(m), strs = ['a', 'b', 'c', ''];
457
+ const consts = constantsOf(m), strs = stringsOf(m);
451
458
  const paramKinds = m.params.map(v => [v, kindOfVar(v, sorts)]), inputKinds = m.inputs.map(v => [v, kindOfVar(v, sorts)]);
452
459
  const fail = (what) => { ev.mismatch = what; return finish(); };
453
460
  const finish = () => {
@@ -475,6 +482,9 @@ export function drive(m, r, sorts, code, opts = {}) {
475
482
  const x = draw(v.sort, sorts, v.name, rand, near, bound, [...strs, ...keys]);
476
483
  inputs[v.name] = code.input ? code.input(v, x, rand) : opts.given?.[v.name] === 'nat' && typeof x === 'number' ? Math.abs(x) : x;
477
484
  }
485
+ // (a list the request carries: up to 4 items, mostly, each field drawn by its sort)
486
+ for (const q of m.seqs ?? [])
487
+ inputs[q.name] = Array.from({ length: rand(4) === 0 ? rand(bound + 1) : rand(5) }, () => Object.fromEntries(q.fields.map(f => [f.name, draw(f.sort, sorts, `${q.name}.${f.name}`, rand, near, bound, strs)])));
478
488
  return { e, inputs };
479
489
  };
480
490
  const ruleInputs = (inputs, e) => {
@@ -498,6 +508,8 @@ export function drive(m, r, sorts, code, opts = {}) {
498
508
  // through `next`), else the difference.
499
509
  const step = (params, ps, side, st, inputs, e, next, where) => {
500
510
  const args = code.args(inputs, e), raw = ruleInputs(inputs, e);
511
+ if (m.folds)
512
+ carry(m, raw, inputs, side.snap, params, r.decode(st));
501
513
  const nowVar = m.inputs.find(v => v.note?.startsWith('Date.now()'));
502
514
  if (nowVar)
503
515
  code.setNow(inputs[nowVar.name]);
@@ -756,8 +768,9 @@ export function drive(m, r, sorts, code, opts = {}) {
756
768
  if (diff)
757
769
  return fail(diff);
758
770
  }
759
- // Steps from the prover's counterexamples: each condition of the step, refuted as "never holds".
760
- if (opts.probes !== false) {
771
+ // Steps from the prover's counterexamples: each condition of the step, refuted as "never holds". (Not where the
772
+ // rule carries folds: their equations, quantified over every pass, make each probe slow, and a witness's lists empty.)
773
+ if (opts.probes !== false && !m.folds?.length) {
761
774
  const conds = conditionsOf(m);
762
775
  for (const [i, c] of conds.entries()) {
763
776
  ev.probes.conditions++;
@@ -889,11 +902,35 @@ export function condAt(c, env) {
889
902
  }
890
903
  import { yes as yesC, no as noC, when as whenDsl } from "./dsl.js";
891
904
  const whenInt = (c, a, b) => whenDsl(c, a, b);
905
+ // The lists a request carries, and each fold's running values as its passes compute them over the items (from the
906
+ // state before the step, the request and the params): what the rule assumes every request carries.
907
+ // (dec: the rule's state before the step, decoded: a list of the state at the rule's own places)
908
+ function carry(m, raw, inputs, st, params, dec) {
909
+ const lists = {};
910
+ for (const q of m.seqs ?? []) {
911
+ lists[q.name] = (inputs[q.name] ?? []);
912
+ raw[q.name] = lists[q.name];
913
+ raw[`${q.name}_len`] = lists[q.name].length;
914
+ }
915
+ const dflt = (map, field) => { const f = m.maps.find(d => d.name === map)?.fields.find(x => x.name === field); return field === 'has' || f?.sort.k === 'bool' ? false : f?.sort.k === 'choice' ? f.sort.values[0] : 0; };
916
+ const cell = (map, key, field) => (lists[map] ? lists[map][key][field] : dec[map]?.[String(key)]?.[field] ?? dflt(map, field));
917
+ for (const d of m.folds ?? []) {
918
+ // (the passes: a carried list's length, or one past a list of the state's last place)
919
+ const env = { ...params, ...st.leaves, ...raw }, runs = [];
920
+ const n = d.state ? Math.max(-1, ...Object.entries(dec[d.list] ?? {}).filter(([, c]) => c.has).map(([k]) => Number(k))) + 1 : (lists[d.list] ?? []).length;
921
+ const at = (t, i) => evalTerm(t.e, i === undefined ? env : { ...env, [I.PASS]: i }, cell);
922
+ runs.push(Object.fromEntries(d.fields.map(f => [f.name, at(d.init[f.name])])));
923
+ lists[d.run] = runs;
924
+ for (let i = 0; i < n; i++)
925
+ runs.push(Object.fromEntries(d.fields.map(f => [f.name, at(d.step[f.name], i)])));
926
+ raw[d.run] = runs;
927
+ }
928
+ }
892
929
  // A term at concrete values (a constant field over the params, a start, the precondition; with `cell`, a state's
893
930
  // map cells and list items too).
894
931
  export function evalTerm(t, env, cell) {
895
932
  const at = (map, k, field) => (cell ?? (() => { throw new Error('a map cell at the start'); }))(map, key(k), field);
896
- const key = (k) => (k.op === 'num' ? n(k.e) : k.op === 'svar' ? env[k.name] : k.op === 'slit' ? k.value : b(k.c) ? key(k.a) : key(k.b));
933
+ const key = (k) => (k.op === 'num' ? n(k.e) : k.op === 'svar' ? env[k.name] : k.op === 'slit' ? k.value : k.op === 'cell' ? at(k.map, k.key, k.field) : b(k.c) ? key(k.a) : key(k.b));
897
934
  const n = (e) => {
898
935
  switch (e.op) {
899
936
  case 'var': return env[e.name];
@@ -911,7 +948,7 @@ export function evalTerm(t, env, cell) {
911
948
  case 'cell': return at(e.map, e.key, e.field);
912
949
  }
913
950
  };
914
- const l = (x) => (x.op === 'var' ? env[x.name] : x.op === 'lit' ? x.value : x.op === 'ite' ? (b(x.c) ? l(x.a) : l(x.b)) : (() => { throw new Error('a map cell'); })());
951
+ const l = (x) => (x.op === 'var' ? env[x.name] : x.op === 'lit' ? x.value : x.op === 'ite' ? (b(x.c) ? l(x.a) : l(x.b)) : at(x.map, x.key, x.field));
915
952
  const b = (x) => {
916
953
  switch (x.op) {
917
954
  case 'const': return x.v;
@@ -104,7 +104,13 @@ export type K = {
104
104
  c: B;
105
105
  a: K;
106
106
  b: K;
107
+ } | {
108
+ op: 'cell';
109
+ map: string;
110
+ key: K;
111
+ field: string;
107
112
  };
113
+ export declare const PASS = "i$";
108
114
  export declare const keyOf: (x: object) => string;
109
115
  export declare const same: (a: object, b: object) => boolean;
110
116
  export declare const num: (v: number) => N;
@@ -1,3 +1,5 @@
1
+ // The pass a fold's step is at: a bound name in its terms (cells of carried lists at i$ and i$ + 1).
2
+ export const PASS = 'i$';
1
3
  // Structural keys (memoized: the same object is compared many times while paths merge).
2
4
  const keys = new WeakMap();
3
5
  export const keyOf = (x) => {
@@ -242,8 +242,32 @@ export function sortsOf(m, given = {}) {
242
242
  if (d.fields[0].sort.k === 'num')
243
243
  sorts.set(`${d.name}.$item`, given[`${d.name}.$item`] ?? 'nat');
244
244
  }
245
+ // A carried list's length and a fold's pass are naturals; an item's numbers integers (any the request carries), a
246
+ // fold's running ones naturals until its start or a step may be below 0.
247
+ for (const q of m.seqs ?? []) {
248
+ sorts.set(`${q.name}_len`, 'nat');
249
+ for (const f of q.fields)
250
+ if (f.sort.k === 'num')
251
+ sorts.set(`${q.name}.${f.name}`, given[`${q.name}.${f.name}`] ?? 'int');
252
+ }
253
+ for (const d of m.folds ?? []) {
254
+ sorts.set(`${d.run}_len`, 'nat');
255
+ for (const f of d.fields)
256
+ if (f.sort.k === 'num')
257
+ sorts.set(`${d.run}.${f.name}`, 'nat');
258
+ }
259
+ if (m.folds?.length)
260
+ sorts.set(I.PASS, 'nat');
245
261
  for (let changed = true; changed;) {
246
262
  changed = false;
263
+ for (const d of m.folds ?? [])
264
+ for (const f of d.fields) {
265
+ const n = `${d.run}.${f.name}`, a = d.init[f.name], b = d.step[f.name];
266
+ if (f.sort.k === 'num' && sorts.get(n) === 'nat' && ((a.s === 'num' && !natOK(a.e, ctx0())) || (b.s === 'num' && !natOK(b.e, ctx0())))) {
267
+ sorts.set(n, 'int');
268
+ changed = true;
269
+ }
270
+ }
247
271
  for (const v of nums(m.state)) {
248
272
  if (sorts.get(v.name) === 'int' || given[v.name])
249
273
  continue;
@@ -391,8 +415,8 @@ export function pb(c, P, ctx) {
391
415
  case 'is': return `${pl(c.l, P, ctx)}.is(${q(c.value)})`;
392
416
  case 'leq': return `${pl(c.a, P, ctx)}.eq(${pl(c.b, P, ctx)})`;
393
417
  case 'keq': {
394
- const s = (k) => (k.op === 'svar' ? k.name : k.op === 'slit' ? q(k.value) : pk(k, P, ctx));
395
- return c.a.op === 'svar' ? `${c.a.name}.eq(${s(c.b)})` : c.b.op === 'svar' ? `${c.b.name}.eq(${s(c.a)})` : `${pk(c.a, P, ctx)}.eq(${pk(c.b, P, ctx)})`;
418
+ const s = (k) => (k.op === 'svar' || k.op === 'cell' ? pstr(k, P, ctx) : k.op === 'slit' ? q(k.value) : pk(k, P, ctx));
419
+ return c.a.op === 'svar' || c.a.op === 'cell' ? `${pstr(c.a, P, ctx)}.eq(${s(c.b)})` : c.b.op === 'svar' || c.b.op === 'cell' ? `${pstr(c.b, P, ctx)}.eq(${s(c.a)})` : `${pk(c.a, P, ctx)}.eq(${pk(c.b, P, ctx)})`;
396
420
  }
397
421
  case 'cell': return cellText(c, P, ctx);
398
422
  }
@@ -428,6 +452,7 @@ export function pk(k, P, ctx) {
428
452
  throw new Refusal(P.m.file, 'a map key that may be negative (a rule\'s map is keyed by naturals)');
429
453
  return pn(k.e, P, ctx, 'nat');
430
454
  case 'svar': return `${k.name}.id`;
455
+ case 'cell': return `${cellText(k, P, ctx)}.id`;
431
456
  case 'slit': return `intern(${q(k.value)})`;
432
457
  case 'kite': return `when(${pb(k.c, P, ctx)}, ${pk(k.a, P, ctx)}, ${pk(k.b, P, ctx)})`;
433
458
  }
@@ -445,6 +470,8 @@ export function pterm(t, P, ctx, target) {
445
470
  function pstr(k, P, ctx, top = false) {
446
471
  if (k.op === 'svar')
447
472
  return k.name;
473
+ if (k.op === 'cell')
474
+ return cellText(k, P, ctx);
448
475
  if (k.op === 'slit') {
449
476
  if (top)
450
477
  return q(k.value);
@@ -48,6 +48,7 @@ export type MapDecl = {
48
48
  untyped?: boolean;
49
49
  len?: number;
50
50
  ordered?: true;
51
+ carried?: true;
51
52
  };
52
53
  export type LeastRead = {
53
54
  name: string;
@@ -194,6 +195,17 @@ export type Model = {
194
195
  imports?: string[];
195
196
  kind?: string;
196
197
  leasts?: LeastRead[];
198
+ seqs?: MapDecl[];
199
+ folds?: Fold[];
200
+ };
201
+ export type Fold = {
202
+ list: string;
203
+ run: string;
204
+ fields: Var[];
205
+ init: Record<string, Term>;
206
+ step: Record<string, Term>;
207
+ len: N;
208
+ state?: true;
197
209
  };
198
210
  type V = {
199
211
  t: 'num';
@@ -1,6 +1,6 @@
1
1
  import * as I from './extract-ir.js';
2
2
  import type { B, K, L, N } from './extract-ir.js';
3
- import { type LeastRead, type MapDecl, type Model, Refusal, type Term, type Var } from './extract-read.js';
3
+ import { type Fold, type LeastRead, type MapDecl, type Model, Refusal, type Term, type Var } from './extract-read.js';
4
4
  import { type Extraction, type Name } from './extract.js';
5
5
  import type { Rule } from './dsl.js';
6
6
  import { type Sorts } from './extract-print.js';
@@ -133,6 +133,31 @@ export type SModule = {
133
133
  split?: boolean;
134
134
  truth?: 'nil-false';
135
135
  stubs?: boolean;
136
+ kinds?: Record<string, string[]>;
137
+ exc?: {
138
+ mro: string[];
139
+ raise: unknown;
140
+ };
141
+ lists?: boolean;
142
+ };
143
+ export type Handler = {
144
+ p?: string;
145
+ any?: boolean;
146
+ names: string[];
147
+ mro?: string[];
148
+ open?: boolean;
149
+ raise?: unknown;
150
+ as?: string;
151
+ body: SS[];
152
+ };
153
+ type Kind = {
154
+ mro: string[];
155
+ open?: boolean;
156
+ } | null | undefined;
157
+ type Try = {
158
+ hs: Handler[];
159
+ to: [Path, number, Kind][];
160
+ ws: B[];
136
161
  };
137
162
  export type StoreDecl = {
138
163
  get?: string[];
@@ -356,6 +381,7 @@ type Path = {
356
381
  called: string[];
357
382
  locks: string[];
358
383
  defers: string[];
384
+ cont?: true;
359
385
  };
360
386
  type Flow = {
361
387
  live?: Path;
@@ -402,6 +428,10 @@ export type PArg = {
402
428
  name: string;
403
429
  } | {
404
430
  k: 'dflt';
431
+ } | {
432
+ k: 'list';
433
+ name: string;
434
+ item: PArg;
405
435
  };
406
436
  export type Answer = {
407
437
  key: string;
@@ -421,6 +451,12 @@ export type Answer = {
421
451
  }[];
422
452
  ty?: string;
423
453
  fit?: boolean;
454
+ raises?: {
455
+ name: string;
456
+ cls: unknown[];
457
+ m: string;
458
+ }[];
459
+ pass?: boolean;
424
460
  };
425
461
  export type StepRead = {
426
462
  ints: Record<string, IntTy>;
@@ -482,6 +518,11 @@ export declare class Stepper {
482
518
  args: [string, PArg][];
483
519
  vals: Map<string, V>;
484
520
  }>;
521
+ readonly seqs: MapDecl[];
522
+ readonly used: Set<string>;
523
+ readonly folds: Fold[];
524
+ hidden: number;
525
+ inFold: number;
485
526
  readonly initHeap: Map<string, Obj>;
486
527
  readonly storeMaps: Map<string, {
487
528
  store: string;
@@ -504,7 +545,7 @@ export declare class Stepper {
504
545
  rec(name: string, at: {
505
546
  p: string;
506
547
  }): SRec;
507
- newVar(name: string, sort: Var['sort'], role: Var['role'], note?: string): Var;
548
+ newVar(name: string, sort: Var['sort'], role: Var['role'], note?: string, own?: boolean): Var;
508
549
  valueOf(v: Var): V;
509
550
  scalar(t: SType): Var['sort'] | undefined;
510
551
  why(t: SType): string;
@@ -521,7 +562,35 @@ export declare class Stepper {
521
562
  assume(p: Path, c: B): Path;
522
563
  fail(p: Path, c: B, at: {
523
564
  p: string;
524
- }, what: string): void;
565
+ }, what: string, kind?: Kind): void;
566
+ tries: Try[];
567
+ handling: Kind[][];
568
+ cms: number[];
569
+ kindOf(what: string, at: {
570
+ p: string;
571
+ }): Kind;
572
+ catcher(k: Kind, at: {
573
+ p: string;
574
+ }, loose?: boolean): [Try, number] | undefined;
575
+ raise(p: Path, c: B, w: B, [t, i]: [Try, number], k: Kind, at: {
576
+ p: string;
577
+ }): void;
578
+ tryStmt(s: SS, p: Path): Flow;
579
+ listOf(v: V, p: Path, at: SX, carried?: boolean): {
580
+ id: string;
581
+ o: Extract<Obj, {
582
+ k: 'map';
583
+ }>;
584
+ len: N;
585
+ held: (k: K) => B;
586
+ } | undefined;
587
+ forStmt(s: SS, p: Path): Flow;
588
+ foldExpr(x: SX, p: Path): [V, Path];
589
+ changedFrom(e: Path, p: Path): boolean;
590
+ joinSome(ps: Path[], at: {
591
+ p: string;
592
+ }): Path | undefined;
593
+ mayRaise(a: Answer, name: string, at: SX, p: Path): Path;
525
594
  unwritten(p: Path, at: {
526
595
  p: string;
527
596
  }, what: string): void;
@@ -653,7 +722,12 @@ export declare class Stepper {
653
722
  match(list: string[] | undefined, names: string[]): string | undefined;
654
723
  skipped(names: string[]): boolean;
655
724
  answer(key: string, names: string[], patch: Patch | undefined, at: SX, p: Path, awaited: boolean, type?: string | SType, how?: string): [V, Path];
725
+ passing(key: string, name: string, patch: Patch | undefined, at: SX, p: Path, awaited: boolean): Path;
656
726
  readonly pend: Map<string, Pend>;
727
+ readonly returnedBy: Map<string, {
728
+ id: string;
729
+ roots: string[];
730
+ }>;
657
731
  readonly entryIds: Set<string>;
658
732
  boundary(x: SX, b: Bound, p: Path, awaited: boolean): [V, Path];
659
733
  argVals(x: SX, p: Path): [V[], Path];
@@ -665,6 +739,7 @@ export declare class Stepper {
665
739
  fnTag(fn: SFunc): Tag | undefined;
666
740
  ownPatch(fn: SFunc, self: V | undefined): Patch | undefined;
667
741
  ownCut(fn: SFunc, self: V | undefined, x: SX): Bound | undefined;
742
+ cuttable(why: string): SFunc | undefined;
668
743
  promote(e: Refusal): Refusal | undefined;
669
744
  ev(x: SX, p: Path, awaited?: boolean): [V, Path];
670
745
  copy(v: V, p: Path, at: SX): [V, Path];