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.
@@ -0,0 +1,26 @@
1
+ import type { Lin, Step } from '../core/solver.js';
2
+ import { type Path, type QC, type QE, type QGoal, type QLaw, type QMachine, type Rat, type Tree } from '../kernel/kernel.js';
3
+ export declare const ratLean: (r: Rat) => string;
4
+ export declare function qTermLean(e: QE, ix: (name: string) => number): string;
5
+ export declare function qCondLean(c: QC, ix: (name: string) => number): string;
6
+ export declare const qRowLean: (lin: Lin, ix: (name: string) => number) => string;
7
+ export type LeafTamper = (rows: {
8
+ lin: Lin;
9
+ strict: boolean;
10
+ }[], leaf: {
11
+ lin: Lin;
12
+ strict: boolean;
13
+ }[]) => {
14
+ lin: Lin;
15
+ strict: boolean;
16
+ }[] | undefined;
17
+ export declare function qLeafLean(goal: QGoal, path: Path, steps: Step[], ix: (name: string) => number, tamper?: LeafTamper): string;
18
+ export declare function qTreeLean(goal: QGoal, tree: Tree, ix: (name: string) => number, path?: Path, tamper?: LeafTamper): string;
19
+ export declare function qmachineToLean(id: string, m: QMachine, laws: QLaw[], opts?: {
20
+ ms?: number;
21
+ }): string;
22
+ export declare function rationalsToLean(items: {
23
+ id: string;
24
+ machine: QMachine;
25
+ laws: QLaw[];
26
+ }[], title?: string): string;
@@ -0,0 +1,105 @@
1
+ import { canon, qLeafRows, qObligations, ratNorm } from "../kernel/kernel.js";
2
+ import { proveQMachine } from "../core/rationals.js";
3
+ const int = (n) => (n < 0 ? `(${n})` : String(n));
4
+ export const ratLean = (r) => { const x = ratNorm(r); return x.den === 1n ? `(${int(x.num)} : Rat)` : `(${int(x.num)} / ${x.den} : Rat)`; };
5
+ export function qTermLean(e, ix) {
6
+ switch (e.op) {
7
+ case 'var': return `.var ${ix(e.name)}`;
8
+ case 'const': return `.const ${ratLean(e.q)}`;
9
+ case 'mul': return `.mul ${ratLean(e.k)} (${qTermLean(e.a, ix)})`;
10
+ case 'if': return `.ite (${qCondLean(e.c, ix)}) (${qTermLean(e.then, ix)}) (${qTermLean(e.else, ix)})`;
11
+ case 'add':
12
+ case 'sub':
13
+ case 'min':
14
+ case 'max': return `.${e.op} (${qTermLean(e.a, ix)}) (${qTermLean(e.b, ix)})`;
15
+ }
16
+ }
17
+ export function qCondLean(c, ix) {
18
+ switch (c.op) {
19
+ case '<=':
20
+ case '<':
21
+ case '=': return `.${c.op === '<=' ? 'le' : c.op === '<' ? 'lt' : 'eq'} (${qTermLean(c.a, ix)}) (${qTermLean(c.b, ix)})`;
22
+ case 'and':
23
+ case 'or': return `.${c.op} (${qCondLean(c.a, ix)}) (${qCondLean(c.b, ix)})`;
24
+ case 'not': return `.not (${qCondLean(c.x, ix)})`;
25
+ }
26
+ }
27
+ // A row as Cert's: its nonzero coefficients in column order, and its constant.
28
+ export const qRowLean = (lin, ix) => {
29
+ const cs = Object.entries(lin).filter(([key, c]) => key !== '1' && c !== 0).map(([key, c]) => [ix(key), c]).sort((a, b) => a[0] - b[0]);
30
+ return `(sp [${cs.map(([j, c]) => `(${j}, ${int(c)})`).join(', ')}] ${int(lin['1'] ?? 0)})`;
31
+ };
32
+ const stepLean = (s) => (s.kind === 'input' ? `.input ${s.index}` : s.kind === 'combine' ? `.combine ${s.a} ${s.b} ${int(s.ma)} ${int(s.mb)}` : `.cut ${s.of} ${int(s.g)} [] 0`);
33
+ export function qLeafLean(goal, path, steps, ix, tamper) {
34
+ const rows = qLeafRows(goal, path), at = new Map(), claimed = [];
35
+ const renum = steps.map(s => {
36
+ if (s.kind !== 'input')
37
+ return s;
38
+ let j = at.get(s.index);
39
+ if (j === undefined) {
40
+ j = claimed.length;
41
+ at.set(s.index, j);
42
+ const r = rows[s.index];
43
+ claimed.push(r ? { lin: r.lin, strict: r.strict === true } : { lin: {}, strict: false });
44
+ }
45
+ return { ...s, index: j };
46
+ });
47
+ const written = (tamper && tamper(claimed, rows.map(r => ({ lin: r.lin, strict: r.strict === true })))) || claimed;
48
+ return `(.leaf [${written.map(r => `(${qRowLean(r.lin, ix)}, ${r.strict})`).join(', ')}] [${renum.map(stepLean).join(', ')}])`;
49
+ }
50
+ export function qTreeLean(goal, tree, ix, path = new Map(), tamper) {
51
+ if ('leaf' in tree)
52
+ return qLeafLean(goal, path, tree.leaf, ix, tamper);
53
+ const key = canon(tree.split);
54
+ const yes = qTreeLean(goal, tree.yes, ix, new Map(path).set(key, { lin: tree.split, holds: true }), tamper);
55
+ const no = qTreeLean(goal, tree.no, ix, new Map(path).set(key, { lin: tree.split, holds: false }), tamper);
56
+ return `(.split ${qRowLean(tree.split, ix)} (${yes}) (${no}))`;
57
+ }
58
+ // A rule over the rationals as Lean: the machine, each law's start and step trees, `<id>_check` (checkMachine computed
59
+ // to true by Lean's kernel), `<id>_laws` (machine_sound: every law in every reachable state) and `<id>_lawN` (law N
60
+ // alone). Throws where the kernel can't prove a law. Variables are numbered params, inputs, state.
61
+ export function qmachineToLean(id, m, laws, opts = {}) {
62
+ const v = proveQMachine(m, laws, opts);
63
+ if (!v.proven || !v.checked || !v.proof)
64
+ throw new Error(`${id}: not proven (${v.goal ?? ''} ${v.stuck ?? ''})`);
65
+ const names = [...m.params, ...m.inputs, ...Object.keys(m.state)];
66
+ const ix = (n) => { const j = names.indexOf(n); if (j < 0)
67
+ throw new Error(`${id}: ${n} is not a variable of the rule`); return j; };
68
+ const pair = (r) => `[${Object.entries(r).map(([n, e]) => `(${ix(n)}, ${qTermLean(e, ix)})`).join(', ')}]`;
69
+ const T = '.le (.const (0 : Rat)) (.const (0 : Rat))';
70
+ const entries = laws.map(law => {
71
+ const goals = qObligations(m, laws, law), t = v.proof.trees[law.name];
72
+ return `(${qCondLean(law.holds, ix)}, ${qTreeLean(goals.start, t.start, ix)}, ${qTreeLean(goals.step, t.step, ix)})`;
73
+ });
74
+ return `-- ${m.name}: variables ${names.map((n, j) => `${j} ${n}`).join(', ')}
75
+ noncomputable def ${id}_m : Q.Machine := {
76
+ inputs := [${m.inputs.map(ix).join(', ')}],
77
+ init := ${pair(m.state)},
78
+ step := ${pair(m.step)},
79
+ pre := ${m.pre ? qCondLean(m.pre, ix) : T},
80
+ assume := ${m.assume ? qCondLean(m.assume, ix) : T} }
81
+ noncomputable def ${id}_ps : Q.MachineProof := [
82
+ ${entries.join(',\n ')}]
83
+ theorem ${id}_check : Q.checkMachine ${id}_m ${id}_ps = true := by decide +kernel
84
+ theorem ${id}_laws : ∀ ρ, Q.Reach ${id}_m ρ → ∀ l ∈ ${id}_ps.map (·.1), Q.evalC ρ l = true := Q.machine_sound ${id}_m ${id}_ps ${id}_check
85
+ ${laws.map((law, j) => `-- ${law.name}
86
+ theorem ${id}_law${j} (ρ : Nat → Rat) (h : Q.Reach ${id}_m ρ) : Q.evalC ρ (${qCondLean(law.holds, ix)}) = true :=
87
+ ${id}_laws ρ h _ (List.mem_map.mpr ⟨${id}_ps[${j}]'(by decide +kernel), List.getElem_mem _, rfl⟩)`).join('\n')}`;
88
+ }
89
+ // A file of rules over the rationals, each through Q.machine_sound.
90
+ export function rationalsToLean(items, title = 'Laws over the rationals proven by gdp-wired') {
91
+ const ident = (id) => { const s = id.replace(/[^A-Za-z0-9_]/g, '_'); return /^[A-Za-z]/.test(s) ? s : `x_${s}`; };
92
+ const parts = items.map(it => qmachineToLean(ident(it.id), it.machine, it.laws));
93
+ const theorems = items.flatMap(it => [`${ident(it.id)}_laws`, ...it.laws.map((_, j) => `${ident(it.id)}_law${j}`)]);
94
+ return `import Rationals
95
+ open Cert Kern
96
+ set_option maxRecDepth 100000
97
+ set_option maxHeartbeats 0
98
+
99
+ /-! ${title}: the kernel's trees, checked by Lean against Rationals.lean, whose soundness theorems are proven there. -/
100
+
101
+ ${parts.join('\n\n')}
102
+
103
+ ${theorems.map(t => `#print axioms ${t}`).join('\n')}
104
+ `;
105
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gdp-wired",
3
- "version": "0.22.0",
3
+ "version": "0.23.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",
@@ -84,6 +84,10 @@
84
84
  "types": "./dist/core/solver.d.ts",
85
85
  "default": "./dist/core/solver.js"
86
86
  },
87
+ "./rationals": {
88
+ "types": "./dist/core/rationals.d.ts",
89
+ "default": "./dist/core/rationals.js"
90
+ },
87
91
  "./gdp": {
88
92
  "types": "./dist/core/gdp.d.ts",
89
93
  "default": "./dist/core/gdp.js"
@@ -113,7 +117,7 @@
113
117
  "build": "bun scripts/build.ts",
114
118
  "prepublishOnly": "bun scripts/build.ts",
115
119
  "smoke": "sh scripts/smoke.sh",
116
- "test": "bun scripts/gen-lean-library.ts --check && cd core && bun test solver.test.ts dsl.test.ts arith.test.ts conformance.test.ts compose.test.ts quantifiers.test.ts functions.test.ts keyterms.test.ts lists.test.ts liveness.test.ts sizes.test.ts values.test.ts builders.test.ts gdp.test.ts datatypes.test.ts lemmas.test.ts discover.test.ts infer.test.ts cyclic.test.ts cycles.test.ts data.test.ts seqs.test.ts search.test.ts invoices.test.ts folds.test.ts trees.test.ts pairs.test.ts chart.test.ts xstate.test.ts && cd ../kernel && bun test tamper.test.ts refusals.test.ts steplaw.test.ts sets.test.ts cdcl.test.ts && cd ../examples && bun test examples.test.ts && cd .. && bun examples/readme.ts && bun examples/guide.ts && bun examples/composition.ts && bun examples/sets.ts && bun examples/quantifiers.ts && bun examples/functions.ts && bun examples/datatypes.ts && bun examples/lemmas.ts && bun examples/discover.ts && bun examples/invoices.ts && bun examples/folds.ts && bun examples/trees.ts && bun examples/pairs.ts && bun examples/groups.ts && bun examples/charts.ts && bun examples/xstate.ts && bun examples/infer.ts && bun examples/arith.ts && bun examples/cyclic.ts && bun examples/cycles.ts && bun examples/data.ts && bun examples/scopes.ts && bun examples/lists.ts && bun examples/sizes.ts && bun examples/liveness.ts && bun examples/budget.ts && bun examples/gdp.ts && bun examples/steps.ts && bun examples/jev-fabric/run.ts"
120
+ "test": "bun scripts/gen-lean-library.ts --check && cd core && bun test solver.test.ts dsl.test.ts arith.test.ts conformance.test.ts compose.test.ts quantifiers.test.ts functions.test.ts keyterms.test.ts lists.test.ts liveness.test.ts sizes.test.ts values.test.ts rationals.test.ts builders.test.ts gdp.test.ts datatypes.test.ts lemmas.test.ts discover.test.ts infer.test.ts cyclic.test.ts cycles.test.ts nonterm.test.ts data.test.ts seqs.test.ts abstract.test.ts search.test.ts invoices.test.ts folds.test.ts trees.test.ts pairs.test.ts chart.test.ts xstate.test.ts && cd ../kernel && bun test tamper.test.ts refusals.test.ts steplaw.test.ts sets.test.ts cdcl.test.ts && cd ../examples && bun test examples.test.ts && cd .. && bun examples/readme.ts && bun examples/guide.ts && bun examples/composition.ts && bun examples/sets.ts && bun examples/quantifiers.ts && bun examples/functions.ts && bun examples/datatypes.ts && bun examples/lemmas.ts && bun examples/discover.ts && bun examples/invoices.ts && bun examples/folds.ts && bun examples/trees.ts && bun examples/pairs.ts && bun examples/groups.ts && bun examples/charts.ts && bun examples/xstate.ts && bun examples/infer.ts && bun examples/arith.ts && bun examples/cyclic.ts && bun examples/cycles.ts && bun examples/data.ts && bun examples/abstract.ts && bun test compare/tip/translate.test.ts && bun examples/scopes.ts && bun examples/lists.ts && bun examples/sizes.ts && bun examples/liveness.ts && bun examples/nonterm.ts && bun examples/rationals.ts && bun examples/budget.ts && bun examples/gdp.ts && bun examples/steps.ts && bun examples/jev-fabric/run.ts"
117
121
  },
118
122
  "devDependencies": {
119
123
  "@gdp-ts/core": "0.1.0",