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,16 @@
1
+ import { type Certificate, type Json } from './format.js';
2
+ export type Claim = {
3
+ name: string;
4
+ accepted: boolean;
5
+ why?: string;
6
+ };
7
+ export type Report = {
8
+ kind?: string;
9
+ accepted: boolean;
10
+ claims: Claim[];
11
+ malformed?: string;
12
+ };
13
+ export declare function checkCertificate(c: Certificate): Claim[];
14
+ export declare function checkDocument(doc: unknown): Report;
15
+ export declare function goalsOf(c: Certificate): Certificate;
16
+ export type { Json };
@@ -0,0 +1,111 @@
1
+ // The standalone checker (gdp-wired-check): a document in, accepted or refused for each claim out. Every verdict is the
2
+ // trusted kernel's (kernel/kernel.ts, kernel/cyclic.ts, core/check.ts, as they are): each kind's check is the kernel's
3
+ // own entry point where it has one (checkLaws, checkStepLaws, checkKInduction, checkRecur, refuseCyclic, checkQMachine,
4
+ // checkQTree, check), and where it has none, the kernel's goals with the tree named for each (as checkLaws reads them):
5
+ // loops (loopObligations), folds (foldObligations), progress (progressObligations), liveness (livenessParts, then
6
+ // checkStepLaws). Around them, untrusted: reading the document (format.ts) and the guard that it was read faithfully
7
+ // (written again, it must be the document read).
8
+ import { check, checkKInduction, checkLaws, checkQMachine, checkQTree, checkRecur, checkStepLaws, foldObligations, kInductionStart, kInductionSteps, livenessParts, loopObligations, obligations, progressObligations, qObligations, recurGoal, stepLawObligations } from "../kernel/kernel.js";
9
+ import { descentGoal, mainGoal, premiseGoal, refuseCyclic } from "../kernel/cyclic.js";
10
+ import { FormatError, fromInterchange, resolved, sameJson, toInterchange } from "./format.js";
11
+ const attempt = (f) => { try {
12
+ return { ok: f() };
13
+ }
14
+ catch (e) {
15
+ return { ok: false, why: e instanceof Error ? e.message : String(e) };
16
+ } };
17
+ const treeOk = (g, trees) => trees[g.name] !== undefined && check(g, trees[g.name]);
18
+ // Each goal's own answer, for a refusal's why (never the verdict: that is the kernel's entry point's).
19
+ const failing = (goals, trees) => {
20
+ try {
21
+ const bad = goals().filter(g => !treeOk(g, trees)).map(g => (trees[g.name] ? `${g.name}: its tree doesn't refute it` : `${g.name}: no tree`));
22
+ return bad.length ? bad.slice(0, 3).join('; ') + (bad.length > 3 ? `; and ${bad.length - 3} more` : '') : undefined;
23
+ }
24
+ catch (e) {
25
+ return e instanceof Error ? e.message : String(e);
26
+ }
27
+ };
28
+ // (A certificate of no claims, an empty set of laws, is the kernel's answer on it all the same: a claim of its own.)
29
+ const all = (names, r, why) => (names.length ? names : ['(no claims)']).map(name => ({ name, accepted: r.ok, ...(r.ok ? {} : { why: r.why ?? why?.() ?? 'refused' }) }));
30
+ // The kernel's verdict on a certificate, for each thing it claims.
31
+ export function checkCertificate(c) {
32
+ switch (c.kind) {
33
+ case 'goals': return c.goals.map(g => { const r = attempt(() => check(g.goal, g.tree)); return { name: g.goal.name, accepted: r.ok, ...(r.ok ? {} : { why: r.why ?? 'the tree doesn\'t refute the goal' }) }; });
34
+ case 'qgoals': return c.goals.map(g => { const r = attempt(() => checkQTree(g.goal, g.tree)); return { name: g.goal.name, accepted: r.ok, ...(r.ok ? {} : { why: r.why ?? 'the tree doesn\'t refute the goal' }) }; });
35
+ // A set of laws is proven together (each step assumes them all), so each is accepted exactly when the set is.
36
+ case 'laws': return all(c.laws.map(l => l.name), attempt(() => checkLaws(c.machine, c.laws, c.proof)), () => failing(() => obligations(c.machine, c.laws, c.proof.instances), c.proof.trees));
37
+ case 'stepLaws': return all([...c.laws.map(l => l.name), ...c.stepLaws.map(s => s.name)], attempt(() => checkStepLaws(c.machine, c.laws, c.stepLaws, c.proof)), () => failing(() => [...obligations(c.machine, c.laws, c.proof.laws.instances), ...c.stepLaws.flatMap(s => stepLawObligations(c.machine, c.laws, c.proof.perStep?.[s.name] ?? c.proof.instances, s))], { ...c.proof.laws.trees, ...c.proof.trees })
38
+ ?? 'a later step\'s names read before it (the zone condition)');
39
+ case 'kInduction': return all([c.kInduction.law.name], attempt(() => checkKInduction(c.machine, c.laws, c.kInduction, c.proof)), () => failing(() => [kInductionStart(c.machine, c.kInduction, c.proof.perStep?.[`${c.kInduction.law.name} @start`] ?? c.proof.instances), ...kInductionSteps(c.machine, c.kInduction).flatMap(s => stepLawObligations(c.machine, c.laws, c.proof.perStep?.[s.name] ?? c.proof.instances, s))], c.proof.trees)
40
+ ?? failing(() => obligations(c.machine, c.laws, c.proof.laws.instances), c.proof.laws.trees));
41
+ case 'liveness': {
42
+ const r = attempt(() => { const { stay, cover, helps } = livenessParts(c.machine, c.liveness); return checkStepLaws(c.machine, c.laws, [stay, cover, ...helps], c.proof); });
43
+ return all([c.liveness.name], r, () => failing(() => { const { stay, cover, helps } = livenessParts(c.machine, c.liveness); return [stay, cover, ...helps].flatMap(s => stepLawObligations(c.machine, c.laws, c.proof.perStep?.[s.name] ?? c.proof.instances, s)); }, c.proof.trees));
44
+ }
45
+ // Progress assumes the laws: proven here by their own trees where the document has them, else not accepted.
46
+ case 'progress': {
47
+ if (!c.lawsProof)
48
+ return [{ name: c.progress.name, accepted: false, why: 'the laws it assumes have no proof in this document (lawsProof)' }];
49
+ const lp = c.lawsProof;
50
+ return all([c.progress.name], attempt(() => checkLaws(c.machine, c.laws, lp) && progressObligations(c.machine, c.laws, c.instances, c.progress).every(g => treeOk(g, c.trees))), () => failing(() => obligations(c.machine, c.laws, lp.instances), lp.trees) ?? failing(() => progressObligations(c.machine, c.laws, c.instances, c.progress), c.trees));
51
+ }
52
+ case 'loop': return all([`${c.loop.name} halts`, ...c.laws.map(l => l.name)], attempt(() => loopObligations(c.loop, c.laws, c.rank).every(g => treeOk(g, c.trees))), () => failing(() => loopObligations(c.loop, c.laws, c.rank), c.trees));
53
+ case 'fold': return all(c.laws.map(l => l.name), attempt(() => foldObligations(c.fold, c.laws).every(g => treeOk(g, c.trees))), () => failing(() => foldObligations(c.fold, c.laws), c.trees));
54
+ case 'recur': return all([`${c.loop.name} never ends`], attempt(() => checkRecur(c.loop, c.recur)), () => failing(() => [recurGoal(c.loop, c.recur)], { [recurGoal(c.loop, c.recur).name]: c.recur.tree }) ?? 'the start or the stem');
55
+ case 'cyclic': {
56
+ const why = attempt(() => refuseCyclic(c.cyclic) === undefined);
57
+ return all(c.cyclic.nodes.map(n => n.name), why, () => refuseCyclic(c.cyclic));
58
+ }
59
+ case 'qmachine': return all(c.laws.map(l => l.name), attempt(() => checkQMachine(c.machine, c.laws, c.trees)));
60
+ // A ring's termination: each call's obligation by its cyclic proof (that these are the measure's obligations is
61
+ // read from the ring's definitions, which the format doesn't carry: Lean checks that, Data.lean's termination_by).
62
+ case 'termination': return c.obligations.map(o => { const r = attempt(() => refuseCyclic(o.cyclic) === undefined); return { name: `${o.function}: ${o.law}`, accepted: r.ok, ...(r.ok ? {} : { why: r.why ?? refuseCyclic(o.cyclic) ?? 'refused' }) }; });
63
+ }
64
+ }
65
+ // A document's report: read, guarded, checked. Never throws.
66
+ export function checkDocument(doc) {
67
+ let c;
68
+ try {
69
+ c = fromInterchange(doc);
70
+ // The guard: the certificate read, written again, is the document (its columns in any order).
71
+ if (!sameJson(resolved(toInterchange(c, doc.meta)), resolved(doc)))
72
+ return { accepted: false, claims: [], malformed: 'read back, the document is not the same (a field the format drops, or a value it changes)' };
73
+ }
74
+ catch (e) {
75
+ return { accepted: false, claims: [], malformed: e instanceof FormatError ? e.message : `unreadable: ${e instanceof Error ? e.message : String(e)}` };
76
+ }
77
+ const claims = checkCertificate(c);
78
+ return { kind: c.kind, accepted: claims.every(x => x.accepted), claims };
79
+ }
80
+ // Every goal the kernel builds for a certificate, with the tree the certificate names for it: the tree part of every
81
+ // check, as a `goals` (or `qgoals`) certificate a second checker reads (interchange/py/check.py checks trees alone).
82
+ // (The checks past trees stay the kernel's: a stem evaluated, a cyclic proof's ranks of one length, the step laws' zone.)
83
+ export function goalsOf(c) {
84
+ const pick = (goals, trees) => goals.flatMap(goal => (trees[goal.name] ? [{ goal, tree: trees[goal.name] }] : []));
85
+ const cyclic = (p) => p.nodes.flatMap(n => [{ goal: mainGoal(p.facts, p.nodes, n), tree: n.main }, ...n.uses.flatMap(u => {
86
+ const m = p.nodes[u.node];
87
+ return m ? [...m.hyps.flatMap((h, i) => (u.premises[i] ? [{ goal: premiseGoal(p.facts, n, u, h, i), tree: u.premises[i] }] : [])), ...(m.rank.length === n.rank.length ? [{ goal: descentGoal(p.facts, n, u, m), tree: u.descent }] : [])] : [];
88
+ })]);
89
+ const steps = (m, sls) => [...pick(obligations(m.machine, m.laws, m.proof.laws.instances), m.proof.laws.trees), ...pick(sls.flatMap(s => stepLawObligations(m.machine, m.laws, m.proof.perStep?.[s.name] ?? m.proof.instances, s)), m.proof.trees)];
90
+ switch (c.kind) {
91
+ case 'goals':
92
+ case 'qgoals': return c;
93
+ case 'qmachine': return { kind: 'qgoals', goals: c.laws.flatMap(l => { const g = qObligations(c.machine, c.laws, l), t = c.trees[l.name]; return t ? [{ goal: g.start, tree: t.start }, { goal: g.step, tree: t.step }] : []; }) };
94
+ case 'laws': return { kind: 'goals', goals: pick(obligations(c.machine, c.laws, c.proof.instances), c.proof.trees) };
95
+ case 'stepLaws': return { kind: 'goals', goals: steps(c, c.stepLaws) };
96
+ case 'liveness': {
97
+ const { stay, cover, helps } = livenessParts(c.machine, c.liveness);
98
+ return { kind: 'goals', goals: steps(c, [stay, cover, ...helps]) };
99
+ }
100
+ case 'kInduction': {
101
+ const start = kInductionStart(c.machine, c.kInduction, c.proof.perStep?.[`${c.kInduction.law.name} @start`] ?? c.proof.instances);
102
+ return { kind: 'goals', goals: [...pick([start], c.proof.trees), ...steps(c, kInductionSteps(c.machine, c.kInduction))] };
103
+ }
104
+ case 'progress': return { kind: 'goals', goals: [...(c.lawsProof ? pick(obligations(c.machine, c.laws, c.lawsProof.instances), c.lawsProof.trees) : []), ...pick(progressObligations(c.machine, c.laws, c.instances, c.progress), c.trees)] };
105
+ case 'loop': return { kind: 'goals', goals: pick(loopObligations(c.loop, c.laws, c.rank), c.trees) };
106
+ case 'fold': return { kind: 'goals', goals: pick(foldObligations(c.fold, c.laws), c.trees) };
107
+ case 'recur': return { kind: 'goals', goals: [{ goal: recurGoal(c.loop, c.recur), tree: c.recur.tree }] };
108
+ case 'cyclic': return { kind: 'goals', goals: cyclic(c.cyclic) };
109
+ case 'termination': return { kind: 'goals', goals: c.obligations.flatMap(o => cyclic(o.cyclic)) };
110
+ }
111
+ }
@@ -0,0 +1,150 @@
1
+ import type { C, E, Law, Machine } from '../core/machine.js';
2
+ import type { Fold, Goal, Instances, KInduction, Liveness, Loop, QC, QE, QGoal, QLaw, QMachine, Recur, StepLaw, Tree } from '../kernel/kernel.js';
3
+ import type { Cyclic } from '../kernel/cyclic.js';
4
+ import type { Lin } from '../core/solver.js';
5
+ export declare const FORMAT = "gdp-wired-interchange", VERSION = "1.0";
6
+ export type LawsProof = {
7
+ instances: Record<string, Instances>;
8
+ trees: Record<string, Tree>;
9
+ };
10
+ export type StepProof = {
11
+ laws: LawsProof;
12
+ instances: Instances;
13
+ perStep?: Record<string, Instances>;
14
+ trees: Record<string, Tree>;
15
+ };
16
+ export type Progress = {
17
+ name: string;
18
+ when: C;
19
+ then: C;
20
+ request: Record<string, E>;
21
+ };
22
+ export type Measure = {
23
+ comps: {
24
+ coef: number[];
25
+ c: number;
26
+ }[];
27
+ };
28
+ export type Obligation = {
29
+ function: string;
30
+ law: string;
31
+ cyclic: Cyclic;
32
+ };
33
+ export type Certificate = {
34
+ kind: 'goals';
35
+ goals: {
36
+ goal: Goal;
37
+ tree: Tree;
38
+ }[];
39
+ } | {
40
+ kind: 'laws';
41
+ machine: Machine;
42
+ laws: Law[];
43
+ proof: LawsProof;
44
+ } | {
45
+ kind: 'stepLaws';
46
+ machine: Machine;
47
+ laws: Law[];
48
+ stepLaws: StepLaw[];
49
+ proof: StepProof;
50
+ } | {
51
+ kind: 'kInduction';
52
+ machine: Machine;
53
+ laws: Law[];
54
+ kInduction: KInduction;
55
+ proof: StepProof;
56
+ } | {
57
+ kind: 'progress';
58
+ machine: Machine;
59
+ laws: Law[];
60
+ progress: Progress;
61
+ instances: Instances;
62
+ trees: Record<string, Tree>;
63
+ lawsProof?: LawsProof;
64
+ } | {
65
+ kind: 'liveness';
66
+ machine: Machine;
67
+ laws: Law[];
68
+ liveness: Liveness;
69
+ proof: StepProof;
70
+ } | {
71
+ kind: 'loop';
72
+ loop: Loop;
73
+ laws: Law[];
74
+ rank: E | E[];
75
+ trees: Record<string, Tree>;
76
+ } | {
77
+ kind: 'recur';
78
+ loop: Loop;
79
+ recur: Recur;
80
+ } | {
81
+ kind: 'fold';
82
+ fold: Fold;
83
+ laws: Law[];
84
+ trees: Record<string, Tree>;
85
+ } | {
86
+ kind: 'cyclic';
87
+ cyclic: Cyclic;
88
+ } | {
89
+ kind: 'qgoals';
90
+ goals: {
91
+ goal: QGoal;
92
+ tree: Tree;
93
+ }[];
94
+ } | {
95
+ kind: 'qmachine';
96
+ machine: QMachine;
97
+ laws: QLaw[];
98
+ trees: Record<string, {
99
+ start: Tree;
100
+ step: Tree;
101
+ }>;
102
+ } | {
103
+ kind: 'termination';
104
+ functions: string[];
105
+ text: string;
106
+ measures: Measure[];
107
+ obligations: Obligation[];
108
+ };
109
+ export type Kind = Certificate['kind'];
110
+ export declare const KINDS: Kind[];
111
+ export type Json = null | boolean | number | string | Json[] | {
112
+ [key: string]: Json | undefined;
113
+ };
114
+ export type Document = {
115
+ format: string;
116
+ version: string;
117
+ kind: Kind;
118
+ meta?: Json;
119
+ columns?: Json[];
120
+ [field: string]: Json | undefined;
121
+ };
122
+ export declare class FormatError extends Error {
123
+ }
124
+ export declare function encE(e: E, at?: string): Json;
125
+ export declare function decE(x: unknown, at?: string): E;
126
+ export declare function encC(c: C, at?: string): Json;
127
+ export declare function decC(x: unknown, at?: string): C;
128
+ export declare function encQE(e: QE, at?: string): Json;
129
+ export declare function decQE(x: unknown, at?: string): QE;
130
+ export declare function encQC(c: QC, at?: string): Json;
131
+ export declare function decQC(x: unknown, at?: string): QC;
132
+ type Columns = {
133
+ terms: Json[];
134
+ index: Map<string, number>;
135
+ } | {
136
+ read: E[];
137
+ };
138
+ export declare function encLin(lin: Lin, cols: Columns, at?: string): Json;
139
+ export declare function decLin(x: unknown, cols: Columns, at?: string): Lin;
140
+ export declare function encTree(t: Tree, cols: Columns, at?: string): Json;
141
+ export declare function decTree(x: unknown, cols: Columns, at?: string): Tree;
142
+ export declare function encMachine(m: Machine, at?: string): Json;
143
+ export declare function decMachine(x: unknown, at?: string): Machine;
144
+ export declare function encLaw(l: Law, at?: string): Json;
145
+ export declare function decLaw(x: unknown, at?: string): Law;
146
+ export declare function toInterchange(c: Certificate, meta?: Json): Document;
147
+ export declare function fromInterchange(doc: unknown): Certificate;
148
+ export declare function resolved(doc: Document): Json;
149
+ export declare function sameJson(a: Json | undefined, b: Json | undefined): boolean;
150
+ export {};