gdp-wired 0.21.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.
Files changed (44) hide show
  1. package/README.md +16 -1
  2. package/dist/core/bmc.d.ts +38 -0
  3. package/dist/core/bmc.js +137 -0
  4. package/dist/core/check.d.ts +1 -1
  5. package/dist/core/check.js +19 -6
  6. package/dist/core/cycles.d.ts +15 -0
  7. package/dist/core/cycles.js +446 -8
  8. package/dist/core/cyclic.d.ts +6 -1
  9. package/dist/core/cyclic.js +13 -1
  10. package/dist/core/data.d.ts +17 -1
  11. package/dist/core/data.js +637 -97
  12. package/dist/core/dsl.d.ts +10 -1
  13. package/dist/core/dsl.js +47 -12
  14. package/dist/core/infer.js +518 -5
  15. package/dist/core/nonterm.d.ts +34 -0
  16. package/dist/core/nonterm.js +610 -0
  17. package/dist/core/q.d.ts +17 -0
  18. package/dist/core/q.js +52 -0
  19. package/dist/core/rationals.d.ts +112 -0
  20. package/dist/core/rationals.js +281 -0
  21. package/dist/core/relation.d.ts +28 -0
  22. package/dist/core/relation.js +480 -0
  23. package/dist/core/solver.d.ts +5 -2
  24. package/dist/core/solver.js +65 -20
  25. package/dist/core/transfer.d.ts +3 -3
  26. package/dist/core/transfer.js +77 -42
  27. package/dist/kernel/cdcl.d.ts +43 -0
  28. package/dist/kernel/cdcl.js +564 -0
  29. package/dist/kernel/kernel.d.ts +91 -0
  30. package/dist/kernel/kernel.js +310 -0
  31. package/dist/kernel/prove.d.ts +20 -1
  32. package/dist/kernel/prove.js +119 -44
  33. package/dist/kernel/qprove.d.ts +18 -0
  34. package/dist/kernel/qprove.js +157 -0
  35. package/dist/kernel/reader.d.ts +79 -0
  36. package/dist/kernel/reader.js +539 -0
  37. package/dist/kernel/simplex.d.ts +36 -0
  38. package/dist/kernel/simplex.js +259 -0
  39. package/dist/lean-kernel/emit.d.ts +19 -1
  40. package/dist/lean-kernel/emit.js +71 -3
  41. package/dist/lean-kernel/library.js +2 -0
  42. package/dist/lean-kernel/qlean.d.ts +26 -0
  43. package/dist/lean-kernel/qlean.js +105 -0
  44. package/package.json +6 -2
@@ -0,0 +1,18 @@
1
+ import { type Lin } from '../core/solver.js';
2
+ import type { Rat } from '../core/q.js';
3
+ import { type Path, type QGoal, type Tree } from './kernel.js';
4
+ export type QOutcome = {
5
+ tree: Tree;
6
+ nodes: number;
7
+ } | {
8
+ stuck: 'sat' | 'unknown';
9
+ witness?: Record<string, number>;
10
+ rational?: Record<string, Rat>;
11
+ path: Path;
12
+ why?: string;
13
+ };
14
+ export declare function openQ(goal: QGoal, path: Path): Lin[];
15
+ export declare function growQ(goal: QGoal, opts?: {
16
+ until?: number;
17
+ maxNodes?: number;
18
+ }): QOutcome;
@@ -0,0 +1,157 @@
1
+ // Untrusted, like prove.ts: the tree search for goals over the rationals (kernel.ts's QGoal). A node reads its rows
2
+ // as the kernel does (qLeafRows); the incremental simplex (simplex.ts, over the rationals) picks a conflict's rows, the
3
+ // elimination (solver.ts, no cut) refutes them exactly, and that derivation is the leaf. A node it can't refute is
4
+ // split on an open atom: a choice a term leaves open (min, max, if), or a comparison a hypothesis needs decided before
5
+ // it gives rows (a disjunction, ≠). With no atom open, every hypothesis' rows say all it says under the path, so the
6
+ // elimination decides the node: refuted, or a model, exact (a counterexample to the goal, checked by evaluation).
7
+ // Linear arithmetic over ℚ is decidable and this search is complete for it: every split is on a finite set of atoms,
8
+ // and each leaf is decided.
9
+ import { solve, work } from "../core/solver.js";
10
+ import { canon, qdec, qdif, qLeafRows, qLin, qlinear, qtruth } from "./kernel.js";
11
+ import { Simplex } from "./simplex.js";
12
+ // The open atoms of a term: its undecided choices (and those inside), and an if's condition's while it is open.
13
+ function openE(e, path, out) {
14
+ switch (e.op) {
15
+ case 'var':
16
+ case 'const': return;
17
+ case 'mul': return openE(e.a, path, out);
18
+ case 'add':
19
+ case 'sub':
20
+ openE(e.a, path, out);
21
+ openE(e.b, path, out);
22
+ return;
23
+ case 'min':
24
+ case 'max': {
25
+ openE(e.a, path, out);
26
+ openE(e.b, path, out);
27
+ const a = qlinear(e.a, path), b = qlinear(e.b, path);
28
+ if (a && b)
29
+ push(qdif(a, b), path, out);
30
+ return;
31
+ }
32
+ case 'if': {
33
+ const t = qtruth(e.c, path);
34
+ if (t === undefined)
35
+ openC(e.c, path, out);
36
+ else
37
+ openE(t ? e.then : e.else, path, out);
38
+ return;
39
+ }
40
+ }
41
+ }
42
+ const push = (row, path, out) => { if (qdec(path, row) === undefined) {
43
+ const lin = qLin(row);
44
+ if (lin)
45
+ out.push(lin);
46
+ } };
47
+ const forms = (c, path) => { const a = qlinear(c.a, path), b = qlinear(c.b, path); return a && b ? [a, b] : undefined; };
48
+ // The atoms that decide a condition's truth.
49
+ function openC(c, path, out) {
50
+ switch (c.op) {
51
+ case 'not': return openC(c.x, path, out);
52
+ case 'and':
53
+ case 'or':
54
+ openC(c.a, path, out);
55
+ openC(c.b, path, out);
56
+ return;
57
+ case '<=':
58
+ case '<':
59
+ case '=': {
60
+ openE(c.a, path, out);
61
+ openE(c.b, path, out);
62
+ const f = forms(c, path);
63
+ if (!f)
64
+ return;
65
+ if (c.op !== '<')
66
+ push(qdif(f[0], f[1]), path, out);
67
+ if (c.op !== '<=')
68
+ push(qdif(f[1], f[0]), path, out);
69
+ }
70
+ }
71
+ }
72
+ // The atoms a hypothesis needs decided before its rows (kernel.ts's qfacts) say all it says: none for a comparison
73
+ // whose terms are decided, both parts of a conjunction, and for a disjunction the atoms that decide its parts.
74
+ function openH(c, path, out, pos = true) {
75
+ switch (c.op) {
76
+ case 'not': return openH(c.x, path, out, !pos);
77
+ case 'and':
78
+ case 'or': {
79
+ if ((c.op === 'and') === pos) {
80
+ openH(c.a, path, out, pos);
81
+ openH(c.b, path, out, pos);
82
+ return;
83
+ }
84
+ if (qtruth(c.a, path) === !pos)
85
+ return openH(c.b, path, out, pos);
86
+ if (qtruth(c.b, path) === !pos)
87
+ return openH(c.a, path, out, pos);
88
+ openC(c.a, path, out);
89
+ openC(c.b, path, out);
90
+ return;
91
+ }
92
+ case '<=':
93
+ case '<':
94
+ case '=': {
95
+ const f = forms(c, path);
96
+ if (!f) {
97
+ openE(c.a, path, out);
98
+ openE(c.b, path, out);
99
+ return;
100
+ }
101
+ if (c.op === '=' && !pos && qdec(path, qdif(f[0], f[1])) !== true && qdec(path, qdif(f[1], f[0])) !== true)
102
+ push(qdif(f[0], f[1]), path, out);
103
+ }
104
+ }
105
+ }
106
+ export function openQ(goal, path) {
107
+ const out = [], last = goal.hyps.length - 1;
108
+ if (last >= 0)
109
+ openH(goal.hyps[last], path, out);
110
+ for (let i = 0; i < last; i++)
111
+ openH(goal.hyps[i], path, out);
112
+ const seen = new Set();
113
+ return out.filter(lin => { const key = canon(lin); return !seen.has(key) && (seen.add(key), true); });
114
+ }
115
+ // A derivation of the rows, by the simplex's conflict (its rows refuted exactly) or else the elimination over all.
116
+ function refute(rows, sx, whole) {
117
+ let lp = sx.load(rows);
118
+ if (lp.kind === 'sat')
119
+ lp = sx.check();
120
+ if (lp.kind === 'conflict') {
121
+ const support = lp.rows.sort((x, y) => x - y), out = solve(support.map(i => rows[i]), 20_000, false);
122
+ if (out.kind === 'refuted')
123
+ return { steps: out.steps.map(s => (s.kind === 'input' ? { ...s, index: support[s.index] } : s)) };
124
+ }
125
+ if (!whole && lp.kind === 'sat')
126
+ return {};
127
+ const verdict = solve(rows, 20_000, false);
128
+ return verdict.kind === 'refuted' ? { steps: verdict.steps } : { verdict };
129
+ }
130
+ export function growQ(goal, opts = {}) {
131
+ const until = opts.until ?? performance.now() + 20_000, maxNodes = opts.maxNodes ?? 100_000;
132
+ let nodes = 0;
133
+ const sx = new Simplex(false);
134
+ const go = (path) => {
135
+ if (++nodes > maxNodes || performance.now() > until)
136
+ return { stuck: 'unknown', path, why: nodes > maxNodes ? 'nodes' : 'time' };
137
+ const rows = qLeafRows(goal, path);
138
+ work.n += 1 + rows.length;
139
+ const open = openQ(goal, path);
140
+ const r = refute(rows, sx, open.length === 0);
141
+ if (r.steps)
142
+ return { tree: { leaf: r.steps }, nodes };
143
+ if (!open.length) {
144
+ const v = r.verdict;
145
+ return v?.kind === 'sat' ? { stuck: 'sat', witness: v.witness, ...(v.rational ? { rational: v.rational } : {}), path } : { stuck: 'unknown', path, why: 'no split' };
146
+ }
147
+ const lin = open[0], key = canon(lin);
148
+ const yes = go(new Map(path).set(key, { lin, holds: true }));
149
+ if (!('tree' in yes))
150
+ return yes;
151
+ const no = go(new Map(path).set(key, { lin, holds: false }));
152
+ if (!('tree' in no))
153
+ return no;
154
+ return { tree: { split: lin, yes: yes.tree, no: no.tree }, nodes };
155
+ };
156
+ return go(new Map());
157
+ }
@@ -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 {};