gdp-wired 0.31.1 → 0.33.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 (84) hide show
  1. package/README.md +95 -6
  2. package/dist/core/c/reader.d.ts +86 -0
  3. package/dist/core/c/reader.js +860 -0
  4. package/dist/core/check.js +20 -6
  5. package/dist/core/cli.d.ts +76 -0
  6. package/dist/core/cli.js +636 -0
  7. package/dist/core/dsl.d.ts +6 -4
  8. package/dist/core/dsl.js +30 -16
  9. package/dist/core/effect.d.ts +6 -4
  10. package/dist/core/effect.js +48 -10
  11. package/dist/core/ex/harness.exs +177 -0
  12. package/dist/core/ex/json.exs +46 -0
  13. package/dist/core/ex/reader.exs +462 -0
  14. package/dist/core/exists.d.ts +3 -0
  15. package/dist/core/exists.js +19 -5
  16. package/dist/core/extract-c.d.ts +35 -0
  17. package/dist/core/extract-c.js +424 -0
  18. package/dist/core/extract-check.js +27 -6
  19. package/dist/core/extract-ex.d.ts +16 -0
  20. package/dist/core/extract-ex.js +33 -0
  21. package/dist/core/extract-go-check.d.ts +28 -0
  22. package/dist/core/extract-go-check.js +639 -0
  23. package/dist/core/extract-go.d.ts +231 -0
  24. package/dist/core/extract-go.js +1166 -0
  25. package/dist/core/extract-jvm.d.ts +23 -0
  26. package/dist/core/extract-jvm.js +220 -0
  27. package/dist/core/extract-print.js +17 -7
  28. package/dist/core/extract-py.d.ts +15 -0
  29. package/dist/core/extract-py.js +31 -0
  30. package/dist/core/extract-rb.d.ts +17 -0
  31. package/dist/core/extract-rb.js +31 -0
  32. package/dist/core/extract-read.d.ts +20 -0
  33. package/dist/core/extract-read.js +46 -6
  34. package/dist/core/extract-rs.d.ts +12 -0
  35. package/dist/core/extract-rs.js +306 -0
  36. package/dist/core/extract-shapes.d.ts +2 -0
  37. package/dist/core/extract-shapes.js +6 -5
  38. package/dist/core/extract-step.d.ts +806 -0
  39. package/dist/core/extract-step.js +4141 -0
  40. package/dist/core/extract-swift.d.ts +27 -0
  41. package/dist/core/extract-swift.js +484 -0
  42. package/dist/core/extract-xstate.d.ts +10 -3
  43. package/dist/core/extract-xstate.js +50 -4
  44. package/dist/core/extract.d.ts +22 -3
  45. package/dist/core/extract.js +107 -18
  46. package/dist/core/go/go.mod +3 -0
  47. package/dist/core/go/reader.go +901 -0
  48. package/dist/core/infer.d.ts +4 -0
  49. package/dist/core/infer.js +1838 -69
  50. package/dist/core/jvm/Harness.java +361 -0
  51. package/dist/core/jvm/JavaReader.java +825 -0
  52. package/dist/core/jvm/Jvm.java +332 -0
  53. package/dist/core/jvm/KtReader.java +960 -0
  54. package/dist/core/jvm/Rt.java +10 -0
  55. package/dist/core/laws.d.ts +83 -0
  56. package/dist/core/laws.js +623 -0
  57. package/dist/core/machine.d.ts +2 -0
  58. package/dist/core/machine.js +87 -0
  59. package/dist/core/py/harness.py +328 -0
  60. package/dist/core/py/reader.py +447 -0
  61. package/dist/core/rb/harness.rb +233 -0
  62. package/dist/core/rb/reader.rb +375 -0
  63. package/dist/core/relational.d.ts +49 -0
  64. package/dist/core/relational.js +345 -0
  65. package/dist/core/rs/Cargo.lock +108 -0
  66. package/dist/core/rs/Cargo.toml +14 -0
  67. package/dist/core/rs/src/main.rs +681 -0
  68. package/dist/core/swift/Harness.swift +124 -0
  69. package/dist/core/swift/Main.swift +34 -0
  70. package/dist/core/swift/Package.swift +13 -0
  71. package/dist/core/swift/Sources/gdp-swiftreader/main.swift +1341 -0
  72. package/dist/interchange/check.d.ts +16 -0
  73. package/dist/interchange/check.js +111 -0
  74. package/dist/interchange/format.d.ts +150 -0
  75. package/dist/interchange/format.js +678 -0
  76. package/dist/kernel/epr.d.ts +40 -0
  77. package/dist/kernel/epr.js +1882 -0
  78. package/dist/kernel/kernel.d.ts +45 -1
  79. package/dist/kernel/kernel.js +138 -13
  80. package/dist/kernel/prove.d.ts +45 -0
  81. package/dist/kernel/prove.js +658 -51
  82. package/dist/lean-kernel/emit.d.ts +17 -2
  83. package/dist/lean-kernel/emit.js +87 -7
  84. package/package.json +57 -2
@@ -0,0 +1,345 @@
1
+ // Laws about two runs (`gdp-wired/relational`): two runs of one rule, or of two rules, side by side as one rule, so a
2
+ // law comparing them is an ordinary law of that rule, proven by the same induction, search and kernel, and checked in
3
+ // Lean as any other. Nothing here is trusted: what is proven is a law of the rule built, and the rule built is the two
4
+ // rules' own steps, their names renamed (a_x for the first run's x, b_x for the second's).
5
+ //
6
+ // noninterference(rule, {secret: ['diagnosis'], visible: ['status']}) two runs whose requests and params differ
7
+ // only in the secret ones give the same visible state, and refuse the same requests: the secret never reaches
8
+ // what the party sees
9
+ // isolation(rule, {owner: 'user_id', sees: ['failures']}) what tenant B sees (its cells, its requests'
10
+ // results and refusals) is the same whether or not anyone else acts: run a takes every request, run b only B's
11
+ // agreement(old, next) the two rules agree on every history: the
12
+ // same state they share by name after every run of the same requests, and the same refusals (a migration)
13
+ // proveTwoRuns(law) the verdict, its run (two runs) if broken
14
+ //
15
+ // Each works on a rule read from code (gdp-wired/extract, extract-py, ...) as on one written with the builders; the
16
+ // rule's own invariants (an extraction's `invariants`) are given to proveTwoRuns and hold of each run.
17
+ // `twoRuns` is the construction itself, for laws of another shape: laws over the a_ and b_ names, proven by proveRule
18
+ // or proveSteps on the rule it returns.
19
+ //
20
+ // Refusals: by default (`refusals: 'same'`) a request one run refuses and the other takes is a difference the law is
21
+ // about (an error is seen as much as a value): the runs then keep their state, and `split$`, which the law
22
+ // `the two runs refuse the same requests` holds at 0, marks it. `refusals: 'assumed'` only takes requests both allow.
23
+ //
24
+ // Two of the law's names are reserved in the rule built: a$ok and b$ok (each run allows the request), split$.
25
+ import { Rule, Term, all, any, forall, nat, no, not, proveSteps, same, when, yes } from "./dsl.js";
26
+ import { proveLaws, proveStepLaws } from "../kernel/prove.js";
27
+ import { fromInterchange, toInterchange } from "../interchange/format.js";
28
+ import { checkCertificate } from "../interchange/check.js";
29
+ const specOf = (r) => r.spec;
30
+ const v = (name) => new Term({ op: 'var', name });
31
+ // The names a side declares that are its own in the rule built (renamed): state and its parts, maps, lists, cells,
32
+ // items, kinded values, flags, and the params and inputs not shared.
33
+ function ownNames(s, shared) {
34
+ const out = new Set();
35
+ const kindParts = (xs) => { for (const [n, k] of xs ?? []) {
36
+ out.add(n);
37
+ for (const p of k.vars(n))
38
+ out.add(p);
39
+ } };
40
+ for (const [n, k] of [...s.params, ...s.inputs])
41
+ if (!shared.has(n)) {
42
+ out.add(n);
43
+ for (const p of k.vars(n))
44
+ out.add(p);
45
+ }
46
+ for (const [n] of s.state)
47
+ out.add(n);
48
+ for (const [n] of s.maps)
49
+ out.add(n);
50
+ for (const n of s.lists ?? [])
51
+ out.add(n);
52
+ kindParts(s.cells);
53
+ kindParts(s.items);
54
+ kindParts(s.kinded);
55
+ return out;
56
+ }
57
+ // The rule's env as one side's callbacks read it: its own names without their prefix, the shared ones as they are.
58
+ function view(env, sd) {
59
+ const o = {}, other = sd.p === 'a_' ? 'b_' : 'a_';
60
+ for (const [k, x] of Object.entries(env)) {
61
+ if (k.startsWith(sd.p))
62
+ o[k.slice(2)] = x;
63
+ else if (!k.startsWith(other))
64
+ o[k] = x;
65
+ }
66
+ return o;
67
+ }
68
+ const sameKind = (k1, k2) => k1.constructor === k2.constructor && JSON.stringify(k1.vars('x')) === JSON.stringify(k2.vars('x'))
69
+ && JSON.stringify(k1.assume('x').map(c => c.c)) === JSON.stringify(k2.assume('x').map(c => c.c));
70
+ // Two runs side by side as one rule: a's step and b's on every request (b's unless `stays`), each over its own state.
71
+ export function twoRuns(a, b = a, opts = {}) {
72
+ const sa = specOf(a), sb = specOf(b), refusals = opts.refusals ?? 'same';
73
+ for (const s of [sa, sb]) {
74
+ const odd = ['record', 'seqs', 'seqTables', 'lists2', 'carried', 'nodes'].filter(f => { const x = s[f]; return Array.isArray(x) ? x.length : !!x; });
75
+ if (odd.length || s.sums.length || s.queries.length)
76
+ throw new Error(`twoRuns: ${s.name} has ${[...odd, ...(s.sums.length ? ['sums'] : []), ...(s.queries.length ? ['queries'] : [])].join(', ')}, which two runs side by side don't take yet`);
77
+ for (const [n] of [...s.params, ...s.inputs, ...s.state, ...s.maps])
78
+ if (/^[ab]_/.test(n))
79
+ throw new Error(`twoRuns: ${s.name}'s ${n}: a_ and b_ name the two runs' own values`);
80
+ }
81
+ const kindsOf = (s) => new Map([...s.params, ...s.inputs]);
82
+ const ka = kindsOf(sa), kb = kindsOf(sb);
83
+ const shared = new Set(opts.shared ?? [...ka.keys()].filter(n => kb.has(n)));
84
+ for (const n of shared) {
85
+ const x = ka.get(n), y = kb.get(n);
86
+ if (!x || !y)
87
+ throw new Error(`twoRuns: ${n} is shared but not a param or input of ${x ? sb.name : sa.name}`);
88
+ if (!sameKind(x, y))
89
+ throw new Error(`twoRuns: ${n} is of another kind in ${sa.name} and ${sb.name}`);
90
+ if (sa.params.some(([m]) => m === n) !== sb.params.some(([m]) => m === n))
91
+ throw new Error(`twoRuns: ${n} is a param of one rule and an input of the other`);
92
+ }
93
+ const A = { p: 'a_', s: sa, shared, own: ownNames(sa, shared) }, B = { p: 'b_', s: sb, shared, own: ownNames(sb, shared) };
94
+ const sides = [A, B];
95
+ const rn = (sd, n) => (sd.own.has(n) ? sd.p + n : n);
96
+ // (a part's name is its value's with a suffix: renamed with it)
97
+ for (const sd of sides)
98
+ for (const [n, k] of [...sd.s.params, ...sd.s.inputs, ...(sd.s.kinded ?? []), ...(sd.s.cells ?? []), ...(sd.s.items ?? [])]) {
99
+ if (sd.own.has(n) && JSON.stringify(k.vars(sd.p + n)) !== JSON.stringify(k.vars(n).map(x => sd.p + x)))
100
+ throw new Error(`twoRuns: ${n}'s parts aren't named after it`);
101
+ }
102
+ const once = (xs) => { const seen = new Set(); return xs.filter(([n]) => !seen.has(n) && !!seen.add(n)); };
103
+ const renamed = (f) => sides.flatMap(sd => (f(sd.s) ?? []).map(([n, t]) => [rn(sd, n), t]));
104
+ const fns = once(sides.flatMap(sd => sd.s.fns ?? []));
105
+ for (const [n, d] of fns)
106
+ for (const sd of sides) {
107
+ const o = (sd.s.fns ?? []).find(([m]) => m === n);
108
+ if (o && o[1] !== d)
109
+ throw new Error(`twoRuns: two functions named ${n}`);
110
+ }
111
+ const okOf = (sd) => (env) => all(...sd.s.assume.map(f => f(view(env, sd))));
112
+ const stays = (env) => opts.stays?.(env) ?? no;
113
+ // Where each run takes the request: both allow it (or b stays and a allows it); else neither moves.
114
+ const goA = (env) => (refusals === 'same' ? all(env['a$ok'], any(stays(env), env['b$ok'])) : yes);
115
+ const goB = (env) => (refusals === 'same' ? all(not(stays(env)), env['a$ok'], env['b$ok']) : not(stays(env)));
116
+ const spec = {
117
+ name: opts.name ?? (a === b ? `${sa.name}, two runs` : `${sa.name} | ${sb.name}`),
118
+ params: once(renamed(s => s.params)), inputs: once(renamed(s => s.inputs)),
119
+ state: [...sides.flatMap(sd => sd.s.state.map(([n, init]) => [sd.p + n, typeof init === 'number' ? init : (pe) => init(view(pe, sd))])),
120
+ ...(refusals === 'same' ? [['split$', 0]] : [])],
121
+ maps: renamed(s => s.maps),
122
+ lists: sides.flatMap(sd => (sd.s.lists ?? []).map(n => sd.p + n)),
123
+ cells: renamed(s => s.cells), items: renamed(s => s.items), kinded: renamed(s => s.kinded),
124
+ ...(fns.length ? { fns } : {}),
125
+ facts: sides.flatMap(sd => (sd.s.facts ?? []).map(f => (env) => Object.fromEntries(Object.entries(f(view(env, sd))).map(([n, c]) => [`${sd.p}${n}`, c])))),
126
+ arith: new Set([...(sa.arith ?? []), ...(sb.arith ?? [])]),
127
+ flags: [
128
+ ...sides.flatMap(sd => sd.s.flags.map(f => (env) => Object.fromEntries(Object.entries(f(view(env, sd))).map(([n, c]) => [sd.p + n, c])))),
129
+ ...(refusals === 'same' ? [(env) => ({ 'a$ok': okOf(A)(env), 'b$ok': okOf(B)(env) })] : []),
130
+ ],
131
+ pre: sides.flatMap(sd => sd.s.pre.map(f => (p) => f(view(p, sd)))),
132
+ assume: [
133
+ ...(refusals === 'assumed' ? [(env) => all(okOf(A)(env), any(stays(env), okOf(B)(env)))] : []),
134
+ ...(opts.given ? [(env) => opts.given(env)] : []),
135
+ ],
136
+ sums: [], queries: [],
137
+ step: (env) => {
138
+ const out = {};
139
+ for (const sd of sides) {
140
+ const go = (sd === A ? goA : goB)(env), side = view(env, sd);
141
+ for (const [k, w] of Object.entries(sd.s.step?.(side) ?? {})) {
142
+ if (w === undefined)
143
+ continue;
144
+ const key = sd.own.has(k) ? sd.p + k : k.startsWith(sd.p) && sd.own.has(k.slice(2)) ? k : undefined;
145
+ if (!key)
146
+ throw new Error(`twoRuns: ${sd.s.name}'s step writes ${k}, which it doesn't declare`);
147
+ out[key] = guarded(sd, key, w, go);
148
+ }
149
+ }
150
+ if (refusals === 'same')
151
+ out['split$'] = when(all(not(stays(env)), not(env['a$ok'].iff(env['b$ok']))), 1, env['split$']);
152
+ return out;
153
+ },
154
+ };
155
+ return new Rule(spec);
156
+ }
157
+ // A run's write kept only where it takes the request (else the value before it).
158
+ function guarded(sd, key, w, go) {
159
+ if (go === yes)
160
+ return w;
161
+ const kept = (value, old) => ({ op: 'if', c: go.c, then: value, else: old });
162
+ const mapWrite = (m) => ({ ...m, value: kept(m.value, { op: 'get', map: m.map, key: m.each ? { op: 'var', name: m.each } : m.key }), concrete: undefined });
163
+ if (typeof w === 'object' && w !== null && 'parts' in w)
164
+ return { parts: Object.fromEntries(Object.entries(w.parts).map(([p, m]) => [p, mapWrite(m)])) };
165
+ if (typeof w === 'object' && w !== null && 'map' in w)
166
+ return mapWrite(w);
167
+ const kind = (sd.s.kinded ?? []).find(([n]) => sd.p + n === key)?.[1];
168
+ if (kind) {
169
+ const parts = kind.store(key, w);
170
+ return kind.build(key, part => when(go, parts[part], v(part)));
171
+ }
172
+ return when(go, w, v(key));
173
+ }
174
+ // What a run's state holds by name: its scalars (state and kinded values) and maps (plain or of a kind).
175
+ function stateNames(s) {
176
+ const parts = new Set([...(s.kinded ?? []), ...(s.cells ?? []), ...(s.items ?? [])].flatMap(([n, k]) => k.vars(n).filter(p => p !== n)));
177
+ const lens = new Set([...(s.items ?? []).map(([n]) => `${n}_len`), ...(s.lists ?? []).map(n => `${n}_len`)]);
178
+ return {
179
+ scalars: [...s.state.map(([n]) => n).filter(n => !parts.has(n) && !lens.has(n)), ...(s.kinded ?? []).map(([n]) => n)].filter((n, i, xs) => xs.indexOf(n) === i),
180
+ maps: [...s.maps.map(([n]) => n).filter(n => !parts.has(n) && !(s.lists ?? []).includes(n)), ...(s.cells ?? []).map(([n]) => n)].filter((n, i, xs) => xs.indexOf(n) === i),
181
+ };
182
+ }
183
+ // The two runs' values of a name, the same: a scalar, or a map at every key (or at `at`).
184
+ const sameIn = (env, n, isMap, at) => {
185
+ const x = env[`a_${n}`], y = env[`b_${n}`];
186
+ if (x === undefined || y === undefined)
187
+ throw new Error(`no ${n} in both runs`);
188
+ return !isMap ? same(x, y) : at ? same(x.at(at), y.at(at)) : (k) => same(x.at(k), y.at(k));
189
+ };
190
+ function viewLaw(r, name, names, maps, at) {
191
+ const ms = names.filter(n => maps.has(n) && !at), ss = names.filter(n => !ms.includes(n));
192
+ return r.law(name, (env) => {
193
+ const flat = ss.map(n => sameIn(env, n, maps.has(n), at?.(env)));
194
+ return ms.length ? forall(k => all(...flat, ...ms.map(n => sameIn(env, n, true)(k)))) : all(...flat);
195
+ });
196
+ }
197
+ const refusalLaw = (r) => r.law('the two runs refuse the same requests', (env) => env['split$'].eq(0));
198
+ const unknownNames = (names, v, what) => { const bad = names.filter(n => !v.scalars.includes(n) && !v.maps.includes(n)); if (bad.length)
199
+ throw new Error(`${what}: no ${bad.join(', ')} in the rule's state (its names: ${[...v.scalars, ...v.maps].join(', ')})`); };
200
+ // The secret params and inputs never reach `visible`: two runs whose requests (and params) are the same but for the
201
+ // secret ones keep every visible value the same, and refuse the same requests. `visible` defaults to all the state that
202
+ // isn't secret itself.
203
+ export function noninterference(r, o) {
204
+ const s = specOf(r), names = [...s.params, ...s.inputs].map(([n]) => n), vw = stateNames(s);
205
+ const bad = o.secret.filter(n => !names.includes(n));
206
+ if (bad.length)
207
+ throw new Error(`noninterference: ${bad.join(', ')} isn't a param or input of ${s.name} (its params and inputs: ${names.join(', ')})`);
208
+ const visible = o.visible ?? [...vw.scalars, ...vw.maps];
209
+ unknownNames(visible, vw, 'noninterference');
210
+ const p = twoRuns(r, r, { name: `${s.name}: ${o.secret.join(', ')} secret`, shared: names.filter(n => !o.secret.includes(n)), refusals: o.refusals });
211
+ const maps = new Set(vw.maps), name = o.name ?? `${o.secret.join(', ')} never reaches ${visible.join(', ')}`;
212
+ return { name, rule: p, laws: [viewLaw(p, name, visible, maps), ...(o.refusals === 'assumed' ? [] : [refusalLaw(p)])], stepLaws: [],
213
+ helpers: [...vw.scalars, ...vw.maps].filter(n => !visible.includes(n)).map(n => viewLaw(p, `${n} is the same in both runs`, [n], maps)), sides: [r, r], shared: names.filter(n => !o.secret.includes(n)) };
214
+ }
215
+ // Tenant isolation: what the tenant `B` sees is the same whatever the others do. A request is B's where its owner (an
216
+ // input, a string's id or a natural) is B (a new param, `tenant`), or where `owner(env, B)` holds; run a takes every
217
+ // request, run b only B's. B sees the cells of `sees` (maps keyed by the owner) at B, the scalars of `results` after
218
+ // each of B's requests, and whether B's requests are refused. `sees` defaults to every map, `results` to none.
219
+ export function isolation(r, o) {
220
+ const s = specOf(r), vw = stateNames(s), tenant = o.tenant ?? 'tenant';
221
+ if (typeof o.owner === 'string' && !s.inputs.some(([n]) => n === o.owner))
222
+ throw new Error(`isolation: ${o.owner} isn't an input of ${s.name}`);
223
+ const sees = o.sees ?? vw.maps, results = o.results ?? [];
224
+ unknownNames([...sees, ...results], vw, 'isolation');
225
+ const scalarsSeen = sees.filter(n => !vw.maps.includes(n));
226
+ if (scalarsSeen.length)
227
+ throw new Error(`isolation: ${scalarsSeen.join(', ')} isn't a map (what B sees of the state is its cells; a result after B's request goes in results)`);
228
+ // (the owner as a key: a string's id, a natural)
229
+ const keyOf = (x) => (x instanceof Term ? x : x.id ?? (() => { throw new Error('isolation: the owner is a natural or a string'); })());
230
+ const mine = (env) => (typeof o.owner === 'string' ? keyOf(env[o.owner]).eq(env[tenant]) : o.owner(env, env[tenant]));
231
+ if ([...s.params, ...s.inputs].some(([n]) => n === tenant))
232
+ throw new Error(`isolation: ${tenant} is a name of ${s.name} already (name the tenant's param: tenant)`);
233
+ const base = new Rule({ ...s, params: [...s.params, [tenant, nat]] });
234
+ const all2 = [...s.params, ...s.inputs].map(([n]) => n);
235
+ const p = twoRuns(base, base, { name: `${s.name}: isolation of ${typeof o.owner === 'string' ? o.owner : 'the owner'}`, shared: [...all2, tenant], stays: env => not(mine(env)), refusals: o.refusals });
236
+ const maps = new Set(vw.maps), name = o.name ?? `what ${typeof o.owner === 'string' ? o.owner : 'a tenant'} B sees is the same whatever the others do`;
237
+ const at = (env) => env[tenant];
238
+ const laws = [...(sees.length ? [viewLaw(p, `${name}: ${sees.join(', ')} at B`, sees, maps, at)] : []), ...(o.refusals === 'assumed' ? [] : [refusalLaw(p)])];
239
+ const stepLaws = results.length ? [p.stepLaw(`${name}: ${results.join(', ')} of B's requests`, (_b, after) => all(...results.map(n => same(after[`a_${n}`], after[`b_${n}`]))), (env) => mine(env))] : [];
240
+ return { name, rule: p, laws, stepLaws, helpers: vw.maps.filter(n => !sees.includes(n)).map(n => viewLaw(p, `${n} at B is the same in both runs`, [n], maps, at)), sides: [base, base], shared: [...all2, tenant] };
241
+ }
242
+ // A migration: `next` agrees with `old` on every history: after the same requests, every value of the state they share
243
+ // by name (or `visible`) is the same, and they refuse the same requests. The requests and params of one name are one.
244
+ export function agreement(old, next, o = {}) {
245
+ const so = specOf(old), sn = specOf(next), vo = stateNames(so), vn = stateNames(sn);
246
+ const common = (xs, ys) => xs.filter(n => ys.includes(n));
247
+ const visible = o.visible ?? [...common(vo.scalars, vn.scalars), ...common(vo.maps, vn.maps)];
248
+ unknownNames(visible, vo, `agreement: ${so.name}`);
249
+ unknownNames(visible, vn, `agreement: ${sn.name}`);
250
+ const p = twoRuns(old, next, { name: `${sn.name} agrees with ${so.name}`, refusals: o.refusals });
251
+ const maps = new Set(vo.maps), name = o.name ?? `${sn.name} agrees with ${so.name} on ${visible.join(', ')}`;
252
+ const kinds = new Map([...so.params, ...so.inputs]), shared = [...sn.params, ...sn.inputs].map(([n]) => n).filter(n => kinds.has(n));
253
+ return { name, rule: p, laws: [viewLaw(p, name, visible, maps), ...(o.refusals === 'assumed' ? [] : [refusalLaw(p)])], stepLaws: [],
254
+ helpers: [...common(vo.scalars, vn.scalars), ...common(vo.maps, vn.maps)].filter(n => !visible.includes(n)).map(n => viewLaw(p, `${n} is the same in both`, [n], maps)), sides: [old, next], shared };
255
+ }
256
+ // --- proving -----------------------------------------------------------------------------------------------------------
257
+ // A law of one run read in the rule built: its names renamed as the run's.
258
+ export function lift(l, r, prefix, shared) {
259
+ const own = ownNames(specOf(r), new Set(shared)), n = (x) => (own.has(x) ? prefix + x : x);
260
+ const e = (x) => {
261
+ switch (x.op) {
262
+ case 'var': return { op: 'var', name: n(x.name) };
263
+ case 'const': return x;
264
+ case 'mul': return { ...x, a: e(x.a) };
265
+ case 'if': return { op: 'if', c: c(x.c), then: e(x.then), else: e(x.else) };
266
+ case 'get': return { op: 'get', map: n(x.map), key: e(x.key) };
267
+ case 'add':
268
+ case 'sub':
269
+ case 'min':
270
+ case 'max':
271
+ case 'prod': return { ...x, a: e(x.a), b: e(x.b) };
272
+ }
273
+ };
274
+ const c = (x) => {
275
+ switch (x.op) {
276
+ case 'flag': return { op: 'flag', name: n(x.name) };
277
+ case 'not': return { op: 'not', x: c(x.x) };
278
+ case 'and':
279
+ case 'or': return { op: x.op, xs: x.xs.map(c) };
280
+ case 'all': return { ...x, c: c(x.c), ...(x.witness ? { witness: e(x.witness) } : {}) };
281
+ case '<':
282
+ case '<=':
283
+ case '=': return { ...x, a: e(x.a), b: e(x.b) };
284
+ }
285
+ };
286
+ if (l.some?.length)
287
+ throw new Error(`${l.name}: a law with "there exists" isn't lifted to two runs`);
288
+ return { name: `${prefix.slice(0, 1)}: ${l.name}`, holds: c(l.holds), ...(l.forall ? { forall: l.forall } : {}), ...(l.keys ? { keys: l.keys } : {}) };
289
+ }
290
+ // The verdict on a law about two runs, with `invariants` (laws of each run's rule, an extraction's own) beside it. The
291
+ // helpers first (Houdini: the set proven together without the search, the one a failed goal names dropped, again), so a
292
+ // law that needs "this hidden value is the same in both runs" is proven with it; then the search, as proveSteps runs it.
293
+ // A run that breaks it is two runs side by side (`run`: its params and requests in the rule built's names).
294
+ export function proveTwoRuns(l, opts = {}) {
295
+ const two = (x) => x.length === 2 && Array.isArray(x[0]);
296
+ const inv = !opts.invariants ? [[], []] : two(opts.invariants) ? opts.invariants : [opts.invariants, opts.invariants];
297
+ const lifted = [...inv[0].map(x => lift(x, l.sides[0], 'a_', l.shared)), ...inv[1].map(x => lift(x, l.sides[1], 'b_', l.shared))];
298
+ let helpers = [...l.helpers];
299
+ for (let round = 0; helpers.length && round <= l.helpers.length; round++) {
300
+ const v = proveSteps(l.rule, [...lifted, ...helpers, ...l.laws], l.stepLaws, { search: false });
301
+ if (v.proven && v.checked)
302
+ return { ...v, found: { ...v.found, laws: [...v.found?.laws ?? [], ...helpers.map(h => ({ law: h, text: h.name }))] }, given: lifted };
303
+ const drop = helpers.find(h => v.goal?.startsWith(h.name));
304
+ if (!drop)
305
+ break;
306
+ helpers = helpers.filter(h => h !== drop);
307
+ }
308
+ const { invariants: _i, ...rest } = opts;
309
+ void _i;
310
+ return { ...proveSteps(l.rule, [...lifted, ...l.laws], l.stepLaws, rest), given: lifted };
311
+ }
312
+ // --- certificates ------------------------------------------------------------------------------------------------------
313
+ // A proven set of laws (and step laws) of a rule as one interchange document (docs/interchange.md), the usual trees its
314
+ // proof, checked by the kernel as it is made: `meta` is kept beside it and never read by a checker (the clauses a law
315
+ // answers, where it is stated), so what a citation says is never what is trusted. Undefined where the kernel doesn't
316
+ // prove the set as given (a proof by k-induction, say, or one the search needed more for).
317
+ export function certify(r, laws, stepLaws = [], meta = {}) {
318
+ const env = globalThis.process?.env ?? {};
319
+ const was = env.GDP_CERT;
320
+ env.GDP_CERT = 'usual';
321
+ try {
322
+ const rr = r.withSumsOf(laws, stepLaws), m = rr.machine, given = [...rr.kindLaws().filter(k => !laws.some(l => l.name === k.name)), ...laws];
323
+ let c;
324
+ if (stepLaws.length) {
325
+ const p = proveStepLaws(m, given, stepLaws);
326
+ if (!p.proven || !p.checked || !p.proof)
327
+ return undefined;
328
+ c = { kind: 'stepLaws', machine: p.machine ?? m, laws: p.laws ?? given, stepLaws: p.stepLaws ?? stepLaws, proof: p.proof };
329
+ }
330
+ else {
331
+ const p = proveLaws(m, given);
332
+ if (!p.proven || !p.checked || !p.proof)
333
+ return undefined;
334
+ c = { kind: 'laws', machine: p.machine ?? m, laws: p.laws ?? given, proof: { instances: p.proof.instances, trees: p.proof.trees } };
335
+ }
336
+ const doc = toInterchange(c, meta);
337
+ return checkCertificate(fromInterchange(JSON.parse(JSON.stringify(doc)))).every(x => x.accepted) ? doc : undefined;
338
+ }
339
+ finally {
340
+ if (was === undefined)
341
+ delete env.GDP_CERT;
342
+ else
343
+ env.GDP_CERT = was;
344
+ }
345
+ }
@@ -0,0 +1,108 @@
1
+ # This file is automatically @generated by Cargo.
2
+ # It is not intended for manual editing.
3
+ version = 4
4
+
5
+ [[package]]
6
+ name = "gdp-rsreader"
7
+ version = "0.1.0"
8
+ dependencies = [
9
+ "proc-macro2",
10
+ "quote",
11
+ "serde_json",
12
+ "syn",
13
+ ]
14
+
15
+ [[package]]
16
+ name = "itoa"
17
+ version = "1.0.18"
18
+ source = "registry+https://github.com/rust-lang/crates.io-index"
19
+ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
20
+
21
+ [[package]]
22
+ name = "memchr"
23
+ version = "2.8.2"
24
+ source = "registry+https://github.com/rust-lang/crates.io-index"
25
+ checksum = "88904434abc2901f197fe8cc55f0445e7ded921dba5911dad2e2b39b48e663c4"
26
+
27
+ [[package]]
28
+ name = "proc-macro2"
29
+ version = "1.0.106"
30
+ source = "registry+https://github.com/rust-lang/crates.io-index"
31
+ checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
32
+ dependencies = [
33
+ "unicode-ident",
34
+ ]
35
+
36
+ [[package]]
37
+ name = "quote"
38
+ version = "1.0.46"
39
+ source = "registry+https://github.com/rust-lang/crates.io-index"
40
+ checksum = "dfbc457d0c7a0759a614551b11a6409e5951f6c7537be1f1b7682b9ae9230368"
41
+ dependencies = [
42
+ "proc-macro2",
43
+ ]
44
+
45
+ [[package]]
46
+ name = "serde"
47
+ version = "1.0.228"
48
+ source = "registry+https://github.com/rust-lang/crates.io-index"
49
+ checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
50
+ dependencies = [
51
+ "serde_core",
52
+ ]
53
+
54
+ [[package]]
55
+ name = "serde_core"
56
+ version = "1.0.228"
57
+ source = "registry+https://github.com/rust-lang/crates.io-index"
58
+ checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
59
+ dependencies = [
60
+ "serde_derive",
61
+ ]
62
+
63
+ [[package]]
64
+ name = "serde_derive"
65
+ version = "1.0.228"
66
+ source = "registry+https://github.com/rust-lang/crates.io-index"
67
+ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
68
+ dependencies = [
69
+ "proc-macro2",
70
+ "quote",
71
+ "syn",
72
+ ]
73
+
74
+ [[package]]
75
+ name = "serde_json"
76
+ version = "1.0.150"
77
+ source = "registry+https://github.com/rust-lang/crates.io-index"
78
+ checksum = "e8014e44b4736ed0538adeecded0fce2a272f22dc9578a7eb6b2d9993c74cfb9"
79
+ dependencies = [
80
+ "itoa",
81
+ "memchr",
82
+ "serde",
83
+ "serde_core",
84
+ "zmij",
85
+ ]
86
+
87
+ [[package]]
88
+ name = "syn"
89
+ version = "2.0.118"
90
+ source = "registry+https://github.com/rust-lang/crates.io-index"
91
+ checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422"
92
+ dependencies = [
93
+ "proc-macro2",
94
+ "quote",
95
+ "unicode-ident",
96
+ ]
97
+
98
+ [[package]]
99
+ name = "unicode-ident"
100
+ version = "1.0.24"
101
+ source = "registry+https://github.com/rust-lang/crates.io-index"
102
+ checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
103
+
104
+ [[package]]
105
+ name = "zmij"
106
+ version = "1.0.21"
107
+ source = "registry+https://github.com/rust-lang/crates.io-index"
108
+ checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
@@ -0,0 +1,14 @@
1
+ [package]
2
+ name = "gdp-rsreader"
3
+ version = "0.1.0"
4
+ edition = "2021"
5
+ publish = false
6
+
7
+ [dependencies]
8
+ syn = {version = "2", features = ["full"]}
9
+ proc-macro2 = {version = "1", features = ["span-locations"]}
10
+ quote = "1"
11
+ serde_json = "1"
12
+
13
+ [profile.release]
14
+ opt-level = 1