gdp-wired 0.34.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.
- package/dist/core/counts.d.ts +31 -0
- package/dist/core/counts.js +264 -0
- package/dist/core/dsl.d.ts +10 -0
- package/dist/core/dsl.js +19 -1
- package/dist/core/extract-check.d.ts +1 -0
- package/dist/core/extract-check.js +43 -6
- package/dist/core/extract-ir.d.ts +6 -0
- package/dist/core/extract-ir.js +2 -0
- package/dist/core/extract-print.js +29 -2
- package/dist/core/extract-read.d.ts +12 -0
- package/dist/core/extract-step.d.ts +78 -3
- package/dist/core/extract-step.js +515 -34
- package/dist/core/extract.js +17 -2
- package/dist/core/machine.d.ts +8 -1
- package/dist/core/machine.js +35 -5
- package/dist/core/py/harness.py +28 -10
- package/dist/core/py/reader.py +57 -4
- package/dist/core/rb/harness.rb +10 -5
- package/dist/core/rb/reader.rb +49 -2
- package/dist/core/watch.js +1 -1
- package/dist/kernel/kernel.d.ts +25 -0
- package/dist/kernel/kernel.js +159 -2
- package/dist/lean-kernel/emit.d.ts +8 -1
- package/dist/lean-kernel/emit.js +68 -1
- package/package.json +1 -1
package/dist/kernel/kernel.js
CHANGED
|
@@ -803,7 +803,9 @@ const distinct = (what, names) => { const seen = new Set(); for (const name of n
|
|
|
803
803
|
// step reads params, inputs and the state before it; a flag reads the flags declared before it; a start value reads the
|
|
804
804
|
// params; a law reads the params, the state, the maps and its own key.
|
|
805
805
|
export function scope(m, laws = []) {
|
|
806
|
-
const state = Object.keys(m.state), maps = Object.keys(m.maps ?? {}), flags = Object.keys(m.step.flags ?? {}),
|
|
806
|
+
const state = Object.keys(m.state), maps = Object.keys(m.maps ?? {}), flags = Object.keys(m.step.flags ?? {}), fns = Object.keys(m.fns ?? {});
|
|
807
|
+
// (a count map is read as a map the request carries; a law reads the counts of the state, countsScope says which)
|
|
808
|
+
const counts = Object.keys(m.counts ?? {}), carried = [...(m.inputMaps ?? []), ...counts];
|
|
807
809
|
const names = [...m.params, ...m.inputs, ...state];
|
|
808
810
|
distinct(m.name, [...names, ...maps, ...carried, ...fns]);
|
|
809
811
|
for (const name of [...names, ...maps, ...carried, ...fns, ...flags])
|
|
@@ -883,7 +885,8 @@ export function scope(m, laws = []) {
|
|
|
883
885
|
}
|
|
884
886
|
const keysOf = (l) => [...(l.forall ? [l.forall] : []), ...(l.keys ?? [])];
|
|
885
887
|
for (const law of laws)
|
|
886
|
-
inScope(`law ${law.name}`, [law.holds], [...m.params, ...state, ...keysOf(law)], [...maps, ...fns]);
|
|
888
|
+
inScope(`law ${law.name}`, [law.holds], [...m.params, ...state, ...keysOf(law)], [...maps, ...fns, ...stateCounts(m)]);
|
|
889
|
+
countsScope(m, laws);
|
|
887
890
|
// A fact reads the params and the functions alone: what holds of them holds at every state.
|
|
888
891
|
for (const fact of facts)
|
|
889
892
|
inScope(`fact ${fact.name}`, [fact.holds], [...m.params, ...keysOf(fact)], fns);
|
|
@@ -1222,6 +1225,9 @@ export function obligations(m, laws, instances = {}, only) {
|
|
|
1222
1225
|
const goals = only === undefined ? sideGoals(m, laws) : [];
|
|
1223
1226
|
return [...obligations(plainOf(m), laws, instances, only), ...goals];
|
|
1224
1227
|
}
|
|
1228
|
+
// (a count changes with the state: a law reading one is read after the step at its post name, countedObligations')
|
|
1229
|
+
if (m.counts && Object.keys(m.counts).length)
|
|
1230
|
+
throw new Error(`${m.name} counts: its goals are countedObligations'`);
|
|
1225
1231
|
scope(m, laws);
|
|
1226
1232
|
const ok = [...Object.values(m.state), ...Object.values(m.step.state ?? {}), ...Object.values(m.step.maps ?? {}).flatMap(w => ('each' in w ? [w.value] : [w.key, w.value]))].every(natural)
|
|
1227
1233
|
&& Object.values(m.step.flags ?? {}).every(naturalC) && laws.every(l => naturalC(l.holds)) && Object.values(m.maps ?? {}).every(c => Number.isInteger(c) && c >= 0);
|
|
@@ -2016,6 +2022,157 @@ export function countedGoal(g, insts) {
|
|
|
2016
2022
|
const hs = countHyps(g.counts, insts);
|
|
2017
2023
|
return hs && { name: g.name, hyps: [...g.hyps, ...hs] };
|
|
2018
2024
|
}
|
|
2025
|
+
export const postName = (map) => `${map}#`;
|
|
2026
|
+
const declsOf = (m) => Object.entries(m.counts ?? {}).map(([map, c]) => ({ map, j: c.j, key: c.key, below: c.below, body: c.body }));
|
|
2027
|
+
// stateOnly: the bound and body read no input and no map a request carries (Lean: avoidsE (extVars ++ flagVars) and
|
|
2028
|
+
// avoidsMapsE carried; a count reads no flag, quantified flag or witness here, countsScope's).
|
|
2029
|
+
const stateOnly = (m, d) => [d.below, d.body].every(t => {
|
|
2030
|
+
const r = reads(t);
|
|
2031
|
+
return m.inputs.every(i => !r.vars.has(i)) && [...(m.inputMaps ?? []), ...Object.keys(m.counts ?? {})].every(mp => !r.maps.has(mp));
|
|
2032
|
+
});
|
|
2033
|
+
export const stateCounts = (m) => Object.entries(m.counts ?? {}).filter(([, d]) => stateOnly(m, d)).map(([c]) => c);
|
|
2034
|
+
// Each count, as Lean's cDeclOk and cApart: its post name none of the machine's; j and key distinct plain names that are
|
|
2035
|
+
// none of its names, bound keys, quantified flags or witnesses, nor a law's key or skolem (k, k#); the bound reading
|
|
2036
|
+
// the params, inputs, state and j, the body those and key, each of the maps, carried maps and functions (no count, no
|
|
2037
|
+
// post name, no flag); naturals only.
|
|
2038
|
+
function countsScope(m, laws) {
|
|
2039
|
+
const counts = m.counts ?? {};
|
|
2040
|
+
if (!Object.keys(counts).length)
|
|
2041
|
+
return;
|
|
2042
|
+
const state = Object.keys(m.state), maps = Object.keys(m.maps ?? {}), carried = m.inputMaps ?? [], fns = Object.keys(m.fns ?? {});
|
|
2043
|
+
const names = [...m.params, ...m.inputs, ...state], declared = [...names, ...maps, ...carried, ...fns, ...Object.keys(counts), ...Object.keys(m.step.flags ?? {})];
|
|
2044
|
+
const bound = [...quantified(m).flatMap(([f, q]) => [`?${f}`, ...witnessesOf(f, q), ...boundOf(q)]), ...Object.values(m.step.maps ?? {}).flatMap(w => ('each' in w ? [w.each] : []))];
|
|
2045
|
+
const lawNames = [...laws, ...(m.facts ?? [])].flatMap(l => [l.forall, ...(l.keys ?? [])].flatMap(k => (k ? [k, `${k}#`] : [])));
|
|
2046
|
+
for (const [c, d] of Object.entries(counts)) {
|
|
2047
|
+
const what = `${m.name}'s count ${c}`;
|
|
2048
|
+
if (declared.includes(postName(c)))
|
|
2049
|
+
throw new Error(`${what}: its post name ${postName(c)} is already a name of ${m.name}`);
|
|
2050
|
+
plainName(what, d.j);
|
|
2051
|
+
plainName(what, d.key);
|
|
2052
|
+
if (d.j === d.key)
|
|
2053
|
+
throw new Error(`${what}: j and key are one name`);
|
|
2054
|
+
for (const x of [d.j, d.key])
|
|
2055
|
+
if ([...declared, ...bound, ...lawNames].includes(x))
|
|
2056
|
+
throw new Error(`${what}: ${x} is already a name of ${m.name} or a law`);
|
|
2057
|
+
unquantified(what, d.below);
|
|
2058
|
+
unquantified(what, d.body);
|
|
2059
|
+
inScope(`${what}'s bound`, [d.below], [...names, d.j], [...maps, ...carried, ...fns]);
|
|
2060
|
+
inScope(`${what}'s body`, [d.body], [...names, d.j, d.key], [...maps, ...carried, ...fns]);
|
|
2061
|
+
if (!natural(d.below) || !natural(d.body))
|
|
2062
|
+
throw new Error(`${what}: a negative constant or factor is outside the kernel`);
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
// toCount: a read cell as Count.lean's count (its variable, the declaration with j at the key term); toCounts: each.
|
|
2066
|
+
const toCount = (ds, r) => {
|
|
2067
|
+
const d = ds.find(x => x.map === r[1]);
|
|
2068
|
+
if (!d)
|
|
2069
|
+
return undefined;
|
|
2070
|
+
const at = { vars: { [d.j]: r[2] }, maps: {}, flags: {} };
|
|
2071
|
+
return { v: r[0], key: d.key, below: substE(d.below, at), body: substE(d.body, at) };
|
|
2072
|
+
};
|
|
2073
|
+
function toCounts(ds, rs) {
|
|
2074
|
+
const out = [];
|
|
2075
|
+
for (const r of rs) {
|
|
2076
|
+
const c = toCount(ds, r);
|
|
2077
|
+
if (!c)
|
|
2078
|
+
return undefined;
|
|
2079
|
+
out.push(c);
|
|
2080
|
+
}
|
|
2081
|
+
return out;
|
|
2082
|
+
}
|
|
2083
|
+
const readHyp = (r) => ({ op: '=', a: { op: 'var', name: r[0] }, b: { op: 'get', map: r[1], key: r[2] } });
|
|
2084
|
+
// readFresh: no hypothesis, declaration or key term reads v, and v is no declaration's j or key.
|
|
2085
|
+
const readFresh = (hyps, ds, rs, v) => hyps.every(h => !readsVar(h, v)) && ds.every(d => !readsVar(d.below, v) && !readsVar(d.body, v) && d.j !== v && d.key !== v) && rs.every(r => !readsVar(r[2], v));
|
|
2086
|
+
// readsOk: the variables distinct (cDistinct), each fresh, each key term reading no declaration's key.
|
|
2087
|
+
const readsOk = (hyps, ds, rs) => new Set(rs.map(r => r[0])).size === rs.length && rs.every(r => readFresh(hyps, ds, rs, r[0]) && ds.every(d => !readsVar(r[2], d.key)));
|
|
2088
|
+
// TypeScript's own condition on the certificate, an input from outside: each read a name, a map's name and a term over
|
|
2089
|
+
// the naturals (Lean's E: every constant and factor a safe integer, which `safeTerm` checks), each instance's bound too.
|
|
2090
|
+
const safeTerm = (t) => {
|
|
2091
|
+
if (!t || typeof t !== 'object')
|
|
2092
|
+
return false;
|
|
2093
|
+
const e = t;
|
|
2094
|
+
switch (e.op) {
|
|
2095
|
+
case 'var': return typeof e.name === 'string';
|
|
2096
|
+
case 'const': return Number.isSafeInteger(e.value) && e.value >= 0;
|
|
2097
|
+
case 'mul': return Number.isSafeInteger(e.k) && e.k >= 0 && safeTerm(e.a);
|
|
2098
|
+
case 'get': return typeof e.map === 'string' && safeTerm(e.key);
|
|
2099
|
+
case 'add':
|
|
2100
|
+
case 'sub':
|
|
2101
|
+
case 'min':
|
|
2102
|
+
case 'max':
|
|
2103
|
+
case 'prod': return safeTerm(e.a) && safeTerm(e.b);
|
|
2104
|
+
case 'if': return safeCond(e.c) && safeTerm(e.then) && safeTerm(e.else);
|
|
2105
|
+
default: return false;
|
|
2106
|
+
}
|
|
2107
|
+
};
|
|
2108
|
+
const safeCond = (c) => !!c && typeof c === 'object' && (c.op === '<' || c.op === '<=' || c.op === '=' ? safeTerm(c.a) && safeTerm(c.b)
|
|
2109
|
+
: c.op === 'not' ? safeCond(c.x) : (c.op === 'and' || c.op === 'or') && Array.isArray(c.xs) && c.xs.every(safeCond));
|
|
2110
|
+
const readsTyped = (rs) => Array.isArray(rs) && rs.every(r => Array.isArray(r) && r.length === 3 && typeof r[0] === 'string' && typeof r[1] === 'string' && safeTerm(r[2]));
|
|
2111
|
+
// witFreshW, countedOkW, checkCountedW: Count.lean's checkCounted with each witness fresh for the counts and the
|
|
2112
|
+
// instances' bounds only. A machine's goal may read a witness in its instances of laws, facts and quantified flags,
|
|
2113
|
+
// which hold at every value of a variable the machine never reads (witsOk): CountMachine.lean's countedW_sound asks the
|
|
2114
|
+
// hypotheses at every value of the witnesses.
|
|
2115
|
+
const witFreshW = (cs, is, w) => cs.every(c => c.v !== w && !readsVar(c.below, w) && !readsVar(c.body, w)) && is.every(j => !readsVar(j.below, w));
|
|
2116
|
+
const countedOkW = (cs, is) => { const ws = is.map(i => i.wit); return new Set(ws).size === ws.length && is.every(i => witFreshW(cs, is, i.wit)); };
|
|
2117
|
+
function checkCountedW(hyps, cs, cert) {
|
|
2118
|
+
if (!(countedOkW(cs, cert.insts) && typed(cert.insts)))
|
|
2119
|
+
return false;
|
|
2120
|
+
const hs = countHyps(cs, cert.insts);
|
|
2121
|
+
return hs !== undefined && check({ name: 'counted', hyps: [...hyps, ...hs] }, cert.tree);
|
|
2122
|
+
}
|
|
2123
|
+
// checkMC: each declaration's j and key differ, the reads are fresh, no read is a witness or reads one in its key
|
|
2124
|
+
// term, and checkCountedW accepts the counted goal they make (the goal's hypotheses, each read variable equal to its
|
|
2125
|
+
// cell; Lean's names the hypotheses it uses, each the goal's).
|
|
2126
|
+
export function checkMC(hyps, ds, k) {
|
|
2127
|
+
if (!(ds.every(d => d.j !== d.key) && readsTyped(k.reads) && Array.isArray(k.insts) && k.insts.every(i => !!i && safeTerm(i.below)) && readsOk(hyps, ds, k.reads)))
|
|
2128
|
+
return false;
|
|
2129
|
+
if (!k.reads.every(r => k.insts.every(i => i.wit !== r[0] && !readsVar(r[2], i.wit))))
|
|
2130
|
+
return false;
|
|
2131
|
+
const cs = toCounts(ds, k.reads);
|
|
2132
|
+
return cs !== undefined && checkCountedW([...hyps, ...k.reads.map(readHyp)], cs, { insts: k.insts, tree: k.tree });
|
|
2133
|
+
}
|
|
2134
|
+
// The goal a certificate's tree refutes (what the search and the exporter read), or none.
|
|
2135
|
+
export function mcGoal(hyps, ds, k) {
|
|
2136
|
+
const cs = toCounts(ds, k.reads);
|
|
2137
|
+
return cs && countedGoal({ name: 'counted', hyps: [...hyps, ...k.reads.map(readHyp)], counts: cs }, k.insts);
|
|
2138
|
+
}
|
|
2139
|
+
// The goals of a machine with counts: obligations', each law proven renamed (renLaw), with the counts each declares.
|
|
2140
|
+
export function countedObligations(m, laws, instances = {}, only) {
|
|
2141
|
+
if (m.history || (m.sums && Object.keys(m.sums).length))
|
|
2142
|
+
throw new Error(`${m.name}: counts go with no history and no sums`);
|
|
2143
|
+
scope(m, laws);
|
|
2144
|
+
const ok = [...Object.values(m.state), ...Object.values(m.step.state ?? {}), ...Object.values(m.step.maps ?? {}).flatMap(w => ('each' in w ? [w.value] : [w.key, w.value]))].every(natural)
|
|
2145
|
+
&& Object.values(m.step.flags ?? {}).every(naturalC) && laws.every(l => naturalC(l.holds)) && Object.values(m.maps ?? {}).every(c => Number.isInteger(c) && c >= 0);
|
|
2146
|
+
if (!ok || !(m.facts ?? []).every(l => naturalC(l.holds)) || laws.some(l => mentions(l.holds, m.inputs)))
|
|
2147
|
+
throw new Error('a negative constant or factor, or a law about a request, is outside the kernel');
|
|
2148
|
+
const ren = { vars: {}, maps: Object.fromEntries(Object.keys(m.counts ?? {}).map(c => [c, (key) => ({ op: 'get', map: postName(c), key })])), flags: {} };
|
|
2149
|
+
const proven = (law) => { const h = substC(law.holds, ren); return law.forall ? substC(h, { vars: { [law.forall]: { op: 'var', name: `${law.forall}#` } }, maps: {}, flags: {} }) : h; };
|
|
2150
|
+
const all = declsOf(m), st = all.filter(d => stateOnly(m, d));
|
|
2151
|
+
const after = (at) => st.map(d => ({ map: postName(d.map), j: d.j, key: d.key, below: substE(d.below, at), body: substE(d.body, at) }));
|
|
2152
|
+
const pre = preOf(m), u = updateOf(m), assume = m.assume ? [inlineFlags(m, m.assume)] : [];
|
|
2153
|
+
return laws.flatMap((law, i) => {
|
|
2154
|
+
if (only !== undefined && i !== only)
|
|
2155
|
+
return [];
|
|
2156
|
+
const start = given(m, holds(from(startOf(m)), pre), instances[law.name] ?? {});
|
|
2157
|
+
const turn = step(holds(given(m, from(), instances[law.name] ?? {}), [...pre, ...assume, ...assumed(laws, instances[law.name] ?? {}), ...closing(m, instances[law.name] ?? {})]), u);
|
|
2158
|
+
return [{ goal: goal(`${law.name} @start`, start, atEnd(start, proven(law))), decls: after(start.at) },
|
|
2159
|
+
{ goal: goal(`${law.name} @step`, turn, atEnd(turn, proven(law))), decls: [...all, ...after(turn.at)] }];
|
|
2160
|
+
});
|
|
2161
|
+
}
|
|
2162
|
+
// witsOk: a certificate's witnesses are names nothing of the machine or its laws reads or binds: none of its names, bound
|
|
2163
|
+
// keys, quantified flags or witnesses, nor a law's key or skolem (Lean's witsOk: fresh for the machine, the precondition
|
|
2164
|
+
// and the laws, no entry's skolem or further key).
|
|
2165
|
+
function witsOk(m, laws, k) {
|
|
2166
|
+
const flags = Object.keys(m.step.flags ?? {});
|
|
2167
|
+
const taken = new Set([...m.params, ...m.inputs, ...Object.keys(m.state), ...Object.keys(m.maps ?? {}), ...(m.inputMaps ?? []), ...Object.keys(m.fns ?? {}), ...Object.keys(m.counts ?? {}), ...flags,
|
|
2168
|
+
...flags.map(f => `?${f}`), ...quantified(m).flatMap(([f, q]) => [...witnessesOf(f, q), ...boundOf(q)]), ...Object.values(m.step.maps ?? {}).flatMap(w => ('each' in w ? [w.each] : [])),
|
|
2169
|
+
...[...laws, ...(m.facts ?? [])].flatMap(l => [l.forall, ...(l.keys ?? [])].flatMap(x => (x ? [x, `${x}#`] : [])))]);
|
|
2170
|
+
return Array.isArray(k.insts) && k.insts.every(i => !!i && typeof i.wit === 'string' && !taken.has(i.wit));
|
|
2171
|
+
}
|
|
2172
|
+
// Lean's checkCMachine: every goal has a certificate, its witnesses fresh, and every certificate checks.
|
|
2173
|
+
export function checkCountedLaws(m, laws, proof) {
|
|
2174
|
+
return countedObligations(m, laws, proof.instances).every(({ goal: g, decls }) => { const k = proof.certs[g.name]; return k !== undefined && witsOk(m, laws, k) && checkMC(g.hyps, decls, k); });
|
|
2175
|
+
}
|
|
2019
2176
|
// SGhostedM.full: the original with each ghost's start and step after its own (Lean: lists appended; here records, whose
|
|
2020
2177
|
// names smghostOk keeps apart from the original's).
|
|
2021
2178
|
export function ghostedMachine(g) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { C, E, Law, Machine, Recurrence } from '../core/machine.js';
|
|
2
2
|
import { type Lin, type Step } from '../core/solver.js';
|
|
3
|
-
import { type CountedGoal, type CountInst, type Ghosted, type Fold, type Goal, type Instances, type KInduction, type Liveness, type Loop, type Path, type Recur, type RecurCases, type StepLaw, type Tree } from '../kernel/kernel.js';
|
|
3
|
+
import { type CountCert, type CountedGoal, type CountInst, type Ghosted, type Fold, type Goal, type Instances, type KInduction, type Liveness, type Loop, type Path, type Recur, type RecurCases, type StepLaw, type Tree } from '../kernel/kernel.js';
|
|
4
4
|
import { proveStepLaws } from '../kernel/prove.js';
|
|
5
5
|
import { type Cyclic } from '../kernel/cyclic.js';
|
|
6
6
|
import { leanLibrary } from './library.js';
|
|
@@ -34,6 +34,13 @@ export declare function countedToLean(id: string, g: CountedGoal, cert: {
|
|
|
34
34
|
what: string;
|
|
35
35
|
insts: CountInst[];
|
|
36
36
|
}[]): string;
|
|
37
|
+
export declare function cmachineToLean(id: string, m: Machine, laws: Law[], proof: {
|
|
38
|
+
instances: Record<string, Instances>;
|
|
39
|
+
certs: Record<string, CountCert>;
|
|
40
|
+
}, tampers?: {
|
|
41
|
+
what: string;
|
|
42
|
+
certs: Record<string, CountCert>;
|
|
43
|
+
}[]): string;
|
|
37
44
|
export declare function machineToLean(id: string, m: Machine, laws: Law[], corollary?: string): string;
|
|
38
45
|
export declare const TRUE = ".le (.const 0) (.const 0)";
|
|
39
46
|
export declare const grown: (g: Goal) => Tree;
|
package/dist/lean-kernel/emit.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { atMost, n, not, pointWrite } from "../core/machine.js";
|
|
2
2
|
import { add, k, scale, sub } from "../core/solver.js";
|
|
3
|
-
import { countedGoal, countHyps, boundOf, caseGoals, check, checkRecur, checkRecurCases, coverGoal, recurGoal, canon, decided, factorsOf, isCell, isMonomial, columnTerm, stateName, termColumn, truth, historyMachine, sumMachine, sumCovers, sumParts, foldObligations, kInductionStart, kInductionSteps, leafConstraints, loopLiveness, loopMachine, linear, livenessHelpful, livenessParts, livenessLevels, livenessRanks, strongly, loopObligations, trueC, obligations, progressObligations, stepLawObligations, substC, witnessesOf, withProducts } from "../kernel/kernel.js";
|
|
3
|
+
import { countedGoal, countedObligations, countHyps, mcGoal, postName, boundOf, caseGoals, check, checkRecur, checkRecurCases, coverGoal, recurGoal, canon, decided, factorsOf, isCell, isMonomial, columnTerm, stateName, termColumn, truth, historyMachine, sumMachine, sumCovers, sumParts, foldObligations, kInductionStart, kInductionSteps, leafConstraints, loopLiveness, loopMachine, linear, livenessHelpful, livenessParts, livenessLevels, livenessRanks, strongly, loopObligations, trueC, obligations, progressObligations, stepLawObligations, substC, witnessesOf, withProducts } from "../kernel/kernel.js";
|
|
4
4
|
import { chooseLevels, clock, grow, minimal, proveBuilt, proveLaws, proveSetFast, proveStepLaws, withDeadline } from "../kernel/prove.js";
|
|
5
5
|
import { refinement, refinementSides } from "../core/compose.js";
|
|
6
6
|
import { leanSurface, leanTerms } from "./lean-terms.js";
|
|
@@ -402,6 +402,73 @@ theorem ${id} : ∀ ρ M, (∀ cn ∈ ${id}_goal.counts, cn.Holds ρ M) → ¬
|
|
|
402
402
|
-- TAMPERED: ${t.what}. Lean computes the check to false, as kernel.ts refuses it.
|
|
403
403
|
example : checkCounted ${id}_goal ${instsLit(t.insts)} ${id}_tab ${id}_tree = false := by ${checkBy}`).join('')}`;
|
|
404
404
|
}
|
|
405
|
+
// A machine with counts through CountMachine.lean's cmachine_sound: Lean builds the goals and the counts they declare;
|
|
406
|
+
// each certificate names the kernel's goal's hypotheses (each one Lean builds), the cells it reads, Count.lean's
|
|
407
|
+
// instances and the tree. The count maps go to Lean as maps the request carries, each with its post name. `tampers`:
|
|
408
|
+
// certificates kernel.ts refuses, which Lean computes to false.
|
|
409
|
+
export function cmachineToLean(id, m, laws, proof, tampers = []) {
|
|
410
|
+
const obl = countedObligations(m, laws, proof.instances), counts = Object.entries(m.counts ?? {});
|
|
411
|
+
const sm = { ...m, counts: undefined, inputMaps: [...(m.inputMaps ?? []), ...counts.map(([cn]) => cn)] };
|
|
412
|
+
const named = machineNames(sm, laws, [], [], {});
|
|
413
|
+
const allCerts = [proof.certs, ...tampers.map(t => t.certs)];
|
|
414
|
+
const goals = obl.map(o => { const g = mcGoal(o.goal.hyps, o.decls, proof.certs[o.goal.name]); if (!g)
|
|
415
|
+
throw new Error(`${id}: ${o.goal.name}: a read of no count`); return g; });
|
|
416
|
+
const certVars = allCerts.flatMap(cs => Object.values(cs).flatMap(k => [...k.reads.flatMap(r => [r[0], ...varsOfE(r[2])]), ...k.insts.flatMap(i => [i.wit, ...varsOfE(i.below)])]));
|
|
417
|
+
const vars = [...new Set([...named.vars, ...counts.flatMap(([, d]) => [d.j, d.key, ...varsOfE(d.below), ...varsOfE(d.body)]), ...laws.map(l => (l.forall ? `${l.forall}#` : '#')),
|
|
418
|
+
...goals.flatMap(g => g.hyps.flatMap(varsOf)), ...certVars])];
|
|
419
|
+
const maps = [...new Set([...named.maps, ...counts.flatMap(([cn]) => [cn, postName(cn)]), ...goals.flatMap(g => g.hyps.flatMap(mapsOf)),
|
|
420
|
+
...allCerts.flatMap(cs => Object.values(cs).flatMap(k => k.reads.map(r => r[1])))])];
|
|
421
|
+
const { e, c } = leanTerms(vars, maps, id);
|
|
422
|
+
const ix = (name) => { const j = vars.indexOf(name); if (j < 0)
|
|
423
|
+
throw new Error(`${id}: no variable for ${name}`); return j; };
|
|
424
|
+
const mx = (name) => { const j = maps.indexOf(name); if (j < 0)
|
|
425
|
+
throw new Error(`${id}: no map for ${name}`); return j; };
|
|
426
|
+
const lawIx = (name) => laws.findIndex(l => l.name === name);
|
|
427
|
+
const facts = m.facts ?? [], facts1 = facts.filter(f => !f.keys?.length), factsN = facts.filter(f => f.keys?.length);
|
|
428
|
+
const tuples = (keys, n2) => Array.from({ length: Math.floor(keys.length / n2) }, (_, t) => keys.slice(t * n2, t * n2 + n2));
|
|
429
|
+
const insts = (inst) => Object.entries(inst).filter(([name]) => lawIx(name) >= 0 && !laws[lawIx(name)].keys?.length).flatMap(([name, keys]) => keys.map(key => `(${lawIx(name)}, ${e(key)})`));
|
|
430
|
+
const pinsts = (inst) => Object.entries(inst).filter(([name]) => lawIx(name) >= 0 && laws[lawIx(name)].keys?.length)
|
|
431
|
+
.flatMap(([name, keys]) => tuples(keys, laws[lawIx(name)].keys.length + 1).map(t => `(${lawIx(name)}, ${e(t[0])}, ${list(t.slice(1).map(e))})`));
|
|
432
|
+
const finsts = (inst) => Object.entries(inst).flatMap(([name, keys]) => { const j = facts1.findIndex(f => f.name === name); return j < 0 ? [] : keys.map(key => `(${j}, ${e(key)})`); });
|
|
433
|
+
const pfinsts = (inst) => Object.entries(inst).flatMap(([name, keys]) => {
|
|
434
|
+
const j = factsN.findIndex(f => f.name === name);
|
|
435
|
+
return j < 0 ? [] : tuples(keys, factsN[j].keys.length + 1).map(t => `(${j}, ${e(t[0])}, ${list(t.slice(1).map(e))})`);
|
|
436
|
+
});
|
|
437
|
+
const qinsts = (inst) => Object.entries(inst).filter(([name]) => name.startsWith('?')).flatMap(([f, keys]) => {
|
|
438
|
+
const q = m.step.flags?.[f.slice(1)], n2 = q?.op === 'all' ? boundOf(q).length : 1;
|
|
439
|
+
return tuples(keys, n2).map(t => `(${ix(f)}, ${list(t.map(e))})`);
|
|
440
|
+
});
|
|
441
|
+
// A certificate: the kernel's goal's hypotheses (Lean checks each is one it builds), the reads, the instances, the
|
|
442
|
+
// table and tree of the goal with the reads' and instances' hypotheses after them.
|
|
443
|
+
const certLit = (name, certs) => {
|
|
444
|
+
// (a tampered certificate keeps the table and tree of the one it tampers with)
|
|
445
|
+
const o = obl.find(o2 => o2.goal.name === name), k = certs[name], k0 = proof.certs[name];
|
|
446
|
+
const tt = encode({ name: `${id}: ${name}`, goal: mcGoal(o.goal.hyps, o.decls, k0), tree: k0.tree }, false, true, vars, maps);
|
|
447
|
+
return `⟨${list(o.goal.hyps.map(c))},
|
|
448
|
+
${list(k.reads.map(r => `(${ix(r[0])}, ${mx(r[1])}, ${e(r[2])})`))},
|
|
449
|
+
${list(k.insts.map(i => `⟨${ix(i.wit)}, ${e(i.below)}, ${list(i.terms.map(([z, v2]) => `(${int(z)}, ${ix(v2)})`))}, ${int(i.unit)}⟩`))},
|
|
450
|
+
${tt.tab},
|
|
451
|
+
${tt.tree}⟩`;
|
|
452
|
+
};
|
|
453
|
+
const entries = (certs) => laws.map(law => {
|
|
454
|
+
const inst = proof.instances[law.name] ?? {};
|
|
455
|
+
return `⟨⟨⟨${c(law.holds)}, ${law.forall ? `some ${ix(law.forall)}` : 'none'}⟩, ${ix(law.forall ? `${law.forall}#` : '#')}, ${list((law.keys ?? []).map(key => String(ix(key))))}, ${list(insts(inst))}, ${list(qinsts(inst))}, ${list(finsts(inst))}, ${list(pinsts(inst))}, ${list(pfinsts(inst))},
|
|
456
|
+
([], [], .leaf [] [] [] []), ([], [], .leaf [] [] [] [])⟩,
|
|
457
|
+
${certLit(`${law.name} @start`, certs)},
|
|
458
|
+
${certLit(`${law.name} @step`, certs)}⟩`;
|
|
459
|
+
});
|
|
460
|
+
return `-- ${m.name}: ${laws.map(l => l.name).join('; ')}, with counts ${counts.map(([cn]) => cn).join(', ')}
|
|
461
|
+
-- (variables ${vars.map((v2, j) => `${j} ${v2}`).join(', ')}; maps ${maps.map((v2, j) => `${j} ${v2}`).join(', ')})
|
|
462
|
+
def ${id}_machine : SMachine := ${machineLit(sm, { e, c }, ix, mx)}
|
|
463
|
+
def ${id}_decls : List CDecl := ${list(counts.map(([cn, d]) => `⟨${mx(cn)}, ${mx(postName(cn))}, ${ix(d.j)}, ${ix(d.key)}, ${e(d.below)}, ${e(d.body)}⟩`))}
|
|
464
|
+
def ${id}_proof : List CEntry := ${list(entries(proof.certs))}
|
|
465
|
+
theorem ${id}_check : checkCMachine ${id}_machine ${id}_decls ${id}_proof = true := by ${checkBy}
|
|
466
|
+
/-- ${laws.map(l => l.name).join('; ')}: each holds in every reachable state, each count map read as the state's count. -/
|
|
467
|
+
theorem ${id}_laws : ∀ ρ M, CReach ${id}_machine ${id}_decls ρ M → ∀ ce ∈ ${id}_proof, ce.e.Holds ρ (withCnt ${id}_decls ρ M) :=
|
|
468
|
+
cmachine_sound ${id}_machine ${id}_decls ${id}_proof ${id}_check${tampers.map(t => `
|
|
469
|
+
-- TAMPERED: ${t.what}. Lean computes the check to false, as kernel.ts refuses it.
|
|
470
|
+
example : checkCMachine ${id}_machine ${id}_decls ${list(entries(t.certs))} = false := by ${checkBy}`).join('')}`;
|
|
471
|
+
}
|
|
405
472
|
// Whole machines through Kern.machine_sound: Lean builds the goals from the machine; the trees are the kernel's.
|
|
406
473
|
export function machineToLean(id, m, laws, corollary) {
|
|
407
474
|
if (m.maps && Object.keys(m.maps).length)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gdp-wired",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.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",
|