gdp-wired 0.50.6 → 0.51.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/dsl.d.ts +4 -1
- package/dist/core/dsl.js +47 -3
- package/dist/core/fixed-division.d.ts +9 -0
- package/dist/core/fixed-division.js +87 -0
- package/dist/core/fixed-integers.d.ts +33 -0
- package/dist/core/fixed-integers.js +279 -0
- package/dist/core/machine.d.ts +1 -0
- package/dist/core/machine.js +33 -3
- package/dist/kernel/integer-primitives.d.ts +38 -0
- package/dist/kernel/integer-primitives.js +179 -0
- package/dist/kernel/kernel.d.ts +1 -0
- package/dist/kernel/kernel.js +44 -5
- package/dist/kernel/prove.js +13 -0
- package/dist/kernel/word-division-cuts.d.ts +6 -0
- package/dist/kernel/word-division-cuts.js +69 -0
- package/dist/kernel/word-division.d.ts +12 -0
- package/dist/kernel/word-division.js +167 -0
- package/dist/lean-kernel/emit.d.ts +4 -1
- package/dist/lean-kernel/emit.js +36 -11
- package/dist/lean-kernel/integer-primitives.d.ts +17 -0
- package/dist/lean-kernel/integer-primitives.js +81 -0
- package/dist/lean-kernel/library.js +13 -0
- package/dist/lean-kernel/word-division-export.d.ts +26 -0
- package/dist/lean-kernel/word-division-export.js +172 -0
- package/dist/lean-kernel/word-division-lower.d.ts +10 -0
- package/dist/lean-kernel/word-division-lower.js +103 -0
- package/package.json +1 -1
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// The reserved word maps are *not* arbitrary state maps. This module records the
|
|
2
|
+
// exact source-level encoding and renders their Nat semantics for Lean obligations.
|
|
3
|
+
// A machine certificate may use these only when the Lean checker itself derives
|
|
4
|
+
// the corresponding digit/carry facts; merely adding facts to a certificate is unsound.
|
|
5
|
+
import { parseWordDivisionName, wordDivisionArgs, wordDivisionName } from "../kernel/word-division.js";
|
|
6
|
+
import { leanTerms } from "./lean-terms.js";
|
|
7
|
+
/** Walk arbitrary machine/law syntax; invalid reserved names or tuple arity fail closed. */
|
|
8
|
+
export function wordReads(source) {
|
|
9
|
+
const out = [], seen = new WeakSet();
|
|
10
|
+
const visit = (node) => {
|
|
11
|
+
if (node === null || typeof node !== 'object' || seen.has(node))
|
|
12
|
+
return;
|
|
13
|
+
seen.add(node);
|
|
14
|
+
if (!Array.isArray(node)) {
|
|
15
|
+
const x = node;
|
|
16
|
+
if (x.op === 'get' && typeof x.map === 'string' && x.map.startsWith('worddiv$')) {
|
|
17
|
+
const spec = parseWordDivisionName(x.map);
|
|
18
|
+
if (!spec || !x.key || typeof x.key !== 'object')
|
|
19
|
+
throw new Error('invalid reserved word-division read');
|
|
20
|
+
const key = x.key, args = wordDivisionArgs(key, spec.width);
|
|
21
|
+
if (!args)
|
|
22
|
+
throw new Error('invalid reserved word-division tuple');
|
|
23
|
+
out.push({ spec, args, key, map: x.map });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
for (const child of Object.values(node))
|
|
27
|
+
visit(child);
|
|
28
|
+
};
|
|
29
|
+
visit(source);
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
/** Native helper IDs needed even when a word-valued update is irrelevant to all
|
|
33
|
+
* exported laws and therefore absent from the minimized proof obligations. */
|
|
34
|
+
export function wordSchemaMapNames(source) {
|
|
35
|
+
const widths = new Set(wordReads(source).map(read => read.spec.width));
|
|
36
|
+
return [...widths].flatMap(width => {
|
|
37
|
+
const n = width === 8 ? 1 : width / 16;
|
|
38
|
+
return [...Array.from({ length: n }, (_, i) => wordDivisionName(width, 'q', i)),
|
|
39
|
+
...Array.from({ length: n }, (_, i) => wordDivisionName(width, 'r', i)),
|
|
40
|
+
...Array.from({ length: 2 * n }, (_, i) => wordDivisionName(width, 'c', i + 1))];
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** Build a closed Lean computation from actual lowered limb values. The expression
|
|
44
|
+
* is independently evaluated by Lean's Nat division, not by a TS answer. */
|
|
45
|
+
export function wordDigitLean(read, operands) {
|
|
46
|
+
const { width, part, index } = read.spec;
|
|
47
|
+
const n = width === 8 ? 1 : width / 16, radix = width === 8 ? 256 : 65536;
|
|
48
|
+
if (operands.length !== 2 * n || operands.some(v => !Number.isSafeInteger(v) || v < 0 || v >= radix))
|
|
49
|
+
throw new Error('closed word gate requires exactly bounded limbs');
|
|
50
|
+
const args = '[' + operands.join(', ') + ']';
|
|
51
|
+
return `WordDivision.value ${width} ${part === 'q' ? 0 : part === 'r' ? 1 : 2} ${index} ${args}`;
|
|
52
|
+
}
|
|
53
|
+
/** Right-associated Kern.cpair key decoded in the same order as wordDivisionArgs. */
|
|
54
|
+
export function wordKeyPartsLean(key, count) {
|
|
55
|
+
if (!Number.isSafeInteger(count) || count < 2 || count > 8)
|
|
56
|
+
throw new Error('invalid word limb count');
|
|
57
|
+
let tail = key;
|
|
58
|
+
const parts = [];
|
|
59
|
+
for (let i = 1; i < count; i++) {
|
|
60
|
+
parts.push(`(Kern.pickP 0 ${tail})`);
|
|
61
|
+
tail = `(Kern.pickP 1 ${tail})`;
|
|
62
|
+
}
|
|
63
|
+
return [...parts, tail];
|
|
64
|
+
}
|
|
65
|
+
/** A concrete, transparent interpretation of every reserved map index, including
|
|
66
|
+
* helper digit/carry maps introduced only by generated certificate hypotheses.
|
|
67
|
+
* Ordinary map indices continue to read base. This overlay alone is NOT a proof
|
|
68
|
+
* of a machine certificate: all used helper facts need a Lean semantic discharge. */
|
|
69
|
+
export function wordMapOverlayLean(reads, maps, name) {
|
|
70
|
+
if (!/^[A-Za-z][A-Za-z0-9_]*$/.test(name))
|
|
71
|
+
throw new Error('invalid Lean name');
|
|
72
|
+
const cases = new Map();
|
|
73
|
+
for (const read of reads) {
|
|
74
|
+
const at = maps.indexOf(read.map);
|
|
75
|
+
if (at < 0)
|
|
76
|
+
throw new Error(`missing word map ${read.map}`);
|
|
77
|
+
const old = cases.get(read.map);
|
|
78
|
+
if (old && JSON.stringify(old) !== JSON.stringify(read.spec))
|
|
79
|
+
throw new Error('inconsistent reserved map');
|
|
80
|
+
cases.set(read.map, read.spec);
|
|
81
|
+
}
|
|
82
|
+
for (const map of maps) {
|
|
83
|
+
if (!map.startsWith('worddiv$'))
|
|
84
|
+
continue;
|
|
85
|
+
const spec = parseWordDivisionName(map);
|
|
86
|
+
if (!spec)
|
|
87
|
+
throw new Error(`invalid reserved word map ${map}`);
|
|
88
|
+
cases.set(map, spec);
|
|
89
|
+
}
|
|
90
|
+
let body = 'base mp key';
|
|
91
|
+
for (const [map, spec] of [...cases].reverse()) {
|
|
92
|
+
const n = spec.width === 8 ? 1 : spec.width / 16;
|
|
93
|
+
const args = `[${wordKeyPartsLean('key', 2 * n).join(', ')}]`;
|
|
94
|
+
const tag = spec.part === 'q' ? 0 : spec.part === 'r' ? 1 : 2;
|
|
95
|
+
body = `if mp = ${maps.indexOf(map)} then WordDivision.value ${spec.width} ${tag} ${spec.index} ${args} else ${body}`;
|
|
96
|
+
}
|
|
97
|
+
return `def ${name} (base : Nat → Nat → Nat) (mp key : Nat) : Nat :=\n ${body}`;
|
|
98
|
+
}
|
|
99
|
+
/** Exact descriptor data for the generic Lean schema. No caller-supplied fact
|
|
100
|
+
* proposition is trusted: Lean constructs the complete clause list itself. */
|
|
101
|
+
export function wordMachineBridgeLean(id, source, vars, maps) {
|
|
102
|
+
const reads = wordReads(source);
|
|
103
|
+
if (!reads.length)
|
|
104
|
+
return '';
|
|
105
|
+
const unique = new Map();
|
|
106
|
+
for (const read of reads)
|
|
107
|
+
unique.set(`${read.spec.width}:${JSON.stringify(read.args)}`, read);
|
|
108
|
+
const terms = leanTerms(vars, maps, id);
|
|
109
|
+
const list = (xs) => `[${xs.join(', ')}]`;
|
|
110
|
+
const reserved = maps.flatMap((map, i) => map.startsWith('worddiv$') ? [i] : []);
|
|
111
|
+
const mapId = (width, part, i) => {
|
|
112
|
+
const map = wordDivisionName(width, part, i), at = maps.indexOf(map);
|
|
113
|
+
if (at < 0)
|
|
114
|
+
throw new Error(`${id}: missing schema map ${map}`);
|
|
115
|
+
return at;
|
|
116
|
+
};
|
|
117
|
+
const names = [], blocks = [];
|
|
118
|
+
for (const [i, read] of [...unique.values()].entries()) {
|
|
119
|
+
const name = `${id}_word_spec${i}`, width = read.spec.width, n = width === 8 ? 1 : width / 16;
|
|
120
|
+
names.push(name);
|
|
121
|
+
const q = Array.from({ length: n }, (_, j) => mapId(width, 'q', j));
|
|
122
|
+
const r = Array.from({ length: n }, (_, j) => mapId(width, 'r', j));
|
|
123
|
+
const carries = Array.from({ length: 2 * n }, (_, j) => mapId(width, 'c', j + 1));
|
|
124
|
+
const ids = [...q, ...r, ...carries];
|
|
125
|
+
const finiteCases = (indices) => `have cases : ${indices.map(j => `i = ${j}`).join(' ∨ ')} := by omega\n rcases cases with ${indices.map(() => 'rfl').join(' | ')}\n all_goals rfl`;
|
|
126
|
+
blocks.push(`def ${name} : WordFacts.Spec := ⟨${width}, ${list(read.args.map(x => `(${terms.e(x)})`))}, ${list(q)}, ${list(r)}, ${list(carries)}⟩
|
|
127
|
+
theorem ${name}_valid : ${name}.Valid := by unfold WordFacts.Spec.Valid; decide +kernel
|
|
128
|
+
-- Dispatch is checked for every native digit at every concrete tuple key.
|
|
129
|
+
theorem ${name}_values : WordFacts.ValueMatches ${name} ${id}_word.value := by
|
|
130
|
+
refine ⟨?_, ?_, ?_⟩
|
|
131
|
+
· intro i hi key
|
|
132
|
+
change i < ${n} at hi
|
|
133
|
+
${finiteCases(Array.from({ length: n }, (_, j) => j))}
|
|
134
|
+
· intro i hi key
|
|
135
|
+
change i < ${n} at hi
|
|
136
|
+
${finiteCases(Array.from({ length: n }, (_, j) => j))}
|
|
137
|
+
· intro i hlo hhi key
|
|
138
|
+
change i ≤ ${2 * n} at hhi
|
|
139
|
+
${finiteCases(Array.from({ length: 2 * n }, (_, j) => j + 1))}
|
|
140
|
+
theorem ${name}_reserved {mp : Nat} (hm : mp ∈ ${name}.reserved) :
|
|
141
|
+
mp ∈ ${id}_word.reserved := by
|
|
142
|
+
change mp ∈ ${list(ids)} at hm
|
|
143
|
+
change mp ∈ ${list(reserved)}
|
|
144
|
+
simp only [List.mem_cons, List.not_mem_nil, or_false] at hm
|
|
145
|
+
rcases hm with ${ids.map(() => 'rfl').join(' | ')}
|
|
146
|
+
all_goals decide +kernel
|
|
147
|
+
`);
|
|
148
|
+
}
|
|
149
|
+
return `${wordMapOverlayLean(reads, maps, `${id}_word_maps`)}
|
|
150
|
+
def ${id}_word : Kern.WordReach.WordSMachine := {
|
|
151
|
+
s := ${id}_machine
|
|
152
|
+
reserved := ${list(reserved)}
|
|
153
|
+
value := ${id}_word_maps (fun _ _ => 0)
|
|
154
|
+
startDisjoint := by decide +kernel
|
|
155
|
+
carriedDisjoint := by decide +kernel
|
|
156
|
+
everyDisjoint := by decide +kernel
|
|
157
|
+
writesDisjoint := by decide +kernel
|
|
158
|
+
}
|
|
159
|
+
${blocks.join('\n')}
|
|
160
|
+
def ${id}_word_extra : List Kern.C := (${list(names)} : List WordFacts.Spec).flatMap WordFacts.facts
|
|
161
|
+
theorem ${id}_word_extra_sound : ∀ X N,
|
|
162
|
+
Kern.WordReach.WordInterpret ${id}_word.reserved ${id}_word.value N →
|
|
163
|
+
∀ c ∈ ${id}_word_extra, Kern.evalC X N c = true := by
|
|
164
|
+
intro X N h c hc
|
|
165
|
+
simp only [${id}_word_extra, List.mem_flatMap] at hc
|
|
166
|
+
obtain ⟨spec, hs, hc⟩ := hc
|
|
167
|
+
simp only [List.mem_cons, List.not_mem_nil, or_false] at hs
|
|
168
|
+
rcases hs with ${names.map(() => 'rfl').join(' | ')}
|
|
169
|
+
${names.map(name => ` · exact WordFacts.facts_sound ${name} ${name}_valid X N
|
|
170
|
+
(WordFacts.interpret_sound ${name} ${name}_valid ${id}_word.value N X ${name}_values
|
|
171
|
+
(fun mp hm key => h mp (${name}_reserved hm) key)) c hc`).join('\n')}`;
|
|
172
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Machine } from '../core/machine.js';
|
|
2
|
+
/** Certified scalar-machine range for any actual q/r/c digit (8/16/32/64).
|
|
3
|
+
* This is NOT a generic SMachine exporter; the exact source shape is verified. */
|
|
4
|
+
export declare function wordDigitRangeMachineToLean(id: string, m: Machine, opts?: {
|
|
5
|
+
fragment?: boolean;
|
|
6
|
+
}): string;
|
|
7
|
+
/** Compatibility wrapper: admit only the original one-limb u8 quotient lane. */
|
|
8
|
+
export declare function q8RangeMachineToLean(id: string, m: Machine, opts?: {
|
|
9
|
+
fragment?: boolean;
|
|
10
|
+
}): string;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { check } from "../kernel/kernel.js";
|
|
2
|
+
import { grow } from "../kernel/prove.js";
|
|
3
|
+
import { encode, TRUE } from "./emit.js";
|
|
4
|
+
import { leanTerms } from "./lean-terms.js";
|
|
5
|
+
import { wordReads, wordMapOverlayLean } from "./word-division-export.js";
|
|
6
|
+
import { wordDivisionKey } from "../kernel/word-division.js";
|
|
7
|
+
const constant = (value) => ({ op: 'const', value });
|
|
8
|
+
const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
9
|
+
/** Certified scalar-machine range for any actual q/r/c digit (8/16/32/64).
|
|
10
|
+
* This is NOT a generic SMachine exporter; the exact source shape is verified. */
|
|
11
|
+
export function wordDigitRangeMachineToLean(id, m, opts = {}) {
|
|
12
|
+
const reads = wordReads(m);
|
|
13
|
+
if (reads.length !== 1)
|
|
14
|
+
throw new Error('expected one valid reserved word read');
|
|
15
|
+
const { width, part, index } = reads[0].spec;
|
|
16
|
+
// Model shape is an allowlist, not a denylist: future Machine/step fields
|
|
17
|
+
// cannot silently change runtime semantics while this export omits them.
|
|
18
|
+
const machineKeys = new Set(['name', 'params', 'inputs', 'state', 'step']);
|
|
19
|
+
if (Object.keys(m).some(key => !machineKeys.has(key)) ||
|
|
20
|
+
Object.keys(m.step).some(key => key !== 'state'))
|
|
21
|
+
throw new Error('unsupported scalar machine metadata or step effect');
|
|
22
|
+
const n = width === 8 ? 1 : width / 16, radix = width === 8 ? 256 : 65536;
|
|
23
|
+
const bound = part === 'c' ? n * radix : radix, op = part === 'c' ? '<=' : '<';
|
|
24
|
+
if (!/^[A-Za-z][A-Za-z0-9_]*$/.test(id) || m.params.length || m.inputs.length !== 2 * n ||
|
|
25
|
+
Object.keys(m.state).length !== 1 || Object.keys(m.step.state ?? {}).length !== 1 ||
|
|
26
|
+
new Set([...m.inputs, ...Object.keys(m.state)]).size !== 2 * n + 1 ||
|
|
27
|
+
!Array.isArray(m.params) || !Array.isArray(m.inputs))
|
|
28
|
+
throw new Error('word digit range machine requires exact scalar shape without auxiliary effects');
|
|
29
|
+
const out = Object.keys(m.state)[0];
|
|
30
|
+
const key = wordDivisionKey(m.inputs.map((name) => ({ op: 'var', name })));
|
|
31
|
+
const expr = { op: 'get', map: reads[0].map, key };
|
|
32
|
+
if (!same(m.state[out], constant(0)) || !same(m.step.state?.[out], expr) || !same(reads[0].key, key))
|
|
33
|
+
throw new Error('word digit range machine must use its exact reserved read and zero initialization');
|
|
34
|
+
const law = { op, a: { op: 'var', name: out }, b: constant(bound) };
|
|
35
|
+
const fact = { op, a: expr, b: constant(bound) };
|
|
36
|
+
const neg = (x) => ({ op: 'not', x });
|
|
37
|
+
const pre = { op: '<=', a: constant(0), b: constant(0) };
|
|
38
|
+
const start = { name: id + '_start', hyps: [pre, neg({ op, a: constant(0), b: constant(bound) })] };
|
|
39
|
+
const step = { name: id + '_step', hyps: [pre, pre, law, fact, neg(fact)] };
|
|
40
|
+
const vars = [...m.inputs, out], maps = [expr.map];
|
|
41
|
+
const terms = leanTerms(vars, maps, id);
|
|
42
|
+
const prove = (g) => {
|
|
43
|
+
const result = grow(g);
|
|
44
|
+
if (!('tree' in result) || !check(g, result.tree))
|
|
45
|
+
throw new Error(id + ': proof tree does not check');
|
|
46
|
+
return encode({ name: g.name, goal: g, tree: result.tree }, false, true, vars, maps);
|
|
47
|
+
};
|
|
48
|
+
const a = prove(start), b = prove(step);
|
|
49
|
+
const overlay = wordMapOverlayLean(reads, maps, id + '_maps');
|
|
50
|
+
const e = terms.e, c = terms.c, state = m.inputs.length;
|
|
51
|
+
const values = `[${m.inputs.map((_, i) => `env ${i}`).join(', ')}]`;
|
|
52
|
+
const lemma = part === 'q' ? 'value_q_lt' : 'value_r_lt';
|
|
53
|
+
const factProof = part === 'c'
|
|
54
|
+
? `have hb := WordDivision.carry_bound ${width} ${values} ${index}
|
|
55
|
+
simpa [${id}_maps, Kern.evalC, Kern.evalE, Kern.pickP, Kern.cun_cpair,
|
|
56
|
+
WordDivision.value_c, WordDivision.limbs, WordDivision.radix] using hb`
|
|
57
|
+
: `have hb := WordDivision.${lemma} ${width} ${index} ${values}
|
|
58
|
+
simpa [${id}_maps, Kern.evalC, Kern.evalE, Kern.pickP,
|
|
59
|
+
Kern.cun_cpair, WordDivision.radix] using hb`;
|
|
60
|
+
const source = `${overlay}
|
|
61
|
+
def ${id}_machine : Kern.Machine := ⟨[${m.inputs.map((_, i) => i).join(', ')}], [(${state}, ${e(constant(0))})],
|
|
62
|
+
[(${state}, ${e(expr)})], ${TRUE}, ${TRUE}⟩
|
|
63
|
+
def ${id}_entry : WordDivisionBridge.Entry :=
|
|
64
|
+
⟨${c(law)}, [], [${c(fact)}], (${a.tab}, ${a.tree}), (${b.tab}, ${b.tree})⟩
|
|
65
|
+
theorem ${id}_check : WordDivisionBridge.checkEntries ${id}_machine [${id}_entry] = true := by decide +kernel
|
|
66
|
+
/-- Full reachable-state machine law, for all input sequences and all base maps. -/
|
|
67
|
+
theorem ${id}_range (base : Nat → Nat → Nat) (ρ : Nat → Nat)
|
|
68
|
+
(hr : Kern.Reach ${id}_machine (${id}_maps base) ρ) :
|
|
69
|
+
Kern.evalC ρ (${id}_maps base) (${c(law)}) = true := by
|
|
70
|
+
have hf : ∀ env p, p ∈ ([${id}_entry] : List WordDivisionBridge.Entry) →
|
|
71
|
+
(∀ f ∈ p.startFacts, Kern.evalC env (${id}_maps base) f = true) ∧
|
|
72
|
+
(∀ f ∈ p.stepFacts, Kern.evalC env (${id}_maps base) f = true) := by
|
|
73
|
+
intro env p hp
|
|
74
|
+
simp only [List.mem_singleton] at hp
|
|
75
|
+
subst p
|
|
76
|
+
constructor
|
|
77
|
+
· intro f h
|
|
78
|
+
change f ∈ ([] : List Kern.C) at h
|
|
79
|
+
simp at h
|
|
80
|
+
· intro f h
|
|
81
|
+
change f ∈ [${c(fact)}] at h
|
|
82
|
+
simp only [List.mem_singleton] at h
|
|
83
|
+
subst f
|
|
84
|
+
${factProof}
|
|
85
|
+
have hs := WordDivisionBridge.checkEntries_sound ${id}_machine [${id}_entry]
|
|
86
|
+
${id}_check (${id}_maps base) hf
|
|
87
|
+
exact hs ρ hr (${c(law)}) (by simp [${id}_entry])
|
|
88
|
+
#print axioms ${id}_range`;
|
|
89
|
+
return opts.fragment ? source : `import WordDivisionBridge
|
|
90
|
+
open Kern
|
|
91
|
+
set_option maxRecDepth 100000
|
|
92
|
+
set_option maxHeartbeats 0
|
|
93
|
+
|
|
94
|
+
${source}
|
|
95
|
+
`;
|
|
96
|
+
}
|
|
97
|
+
/** Compatibility wrapper: admit only the original one-limb u8 quotient lane. */
|
|
98
|
+
export function q8RangeMachineToLean(id, m, opts = {}) {
|
|
99
|
+
const reads = wordReads(m);
|
|
100
|
+
if (reads.length !== 1 || reads[0].spec.width !== 8 || reads[0].spec.part !== 'q' || reads[0].spec.index !== 0)
|
|
101
|
+
throw new Error('expected one actual u8 quotient digit');
|
|
102
|
+
return wordDigitRangeMachineToLean(id, m, opts);
|
|
103
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gdp-wired",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.51.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",
|