gdp-wired 0.47.0 → 0.48.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/README.md +15 -4
- package/dist/core/carry.d.ts +15 -0
- package/dist/core/carry.js +651 -0
- package/dist/core/cli.d.ts +3 -1
- package/dist/core/cli.js +28 -8
- package/dist/core/count-cases.d.ts +24 -0
- package/dist/core/count-cases.js +320 -0
- package/dist/core/count-cert-grow.d.ts +4 -0
- package/dist/core/count-cert-grow.js +37 -0
- package/dist/core/count-cert-transport.d.ts +3 -0
- package/dist/core/count-cert-transport.js +58 -0
- package/dist/core/count-completion.d.ts +9 -0
- package/dist/core/count-completion.js +27 -0
- package/dist/core/count-cubes.d.ts +8 -0
- package/dist/core/count-cubes.js +69 -0
- package/dist/core/count-demand.d.ts +37 -0
- package/dist/core/count-demand.js +361 -0
- package/dist/core/count-facts.d.ts +52 -0
- package/dist/core/count-facts.js +247 -0
- package/dist/core/count-focus-proof.d.ts +8 -0
- package/dist/core/count-focus-proof.js +140 -0
- package/dist/core/count-focused.d.ts +22 -0
- package/dist/core/count-focused.js +676 -0
- package/dist/core/count-ranges.d.ts +5 -0
- package/dist/core/count-ranges.js +65 -0
- package/dist/core/count-recovery/action-guard.d.ts +5 -0
- package/dist/core/count-recovery/action-guard.js +51 -0
- package/dist/core/count-recovery/boolean-core.d.ts +16 -0
- package/dist/core/count-recovery/boolean-core.js +258 -0
- package/dist/core/count-recovery/carried-case.d.ts +2 -0
- package/dist/core/count-recovery/carried-case.js +24 -0
- package/dist/core/count-recovery/conditional-check-cache.d.ts +23 -0
- package/dist/core/count-recovery/conditional-check-cache.js +130 -0
- package/dist/core/count-recovery/conditional-cuts.d.ts +6 -0
- package/dist/core/count-recovery/conditional-cuts.js +318 -0
- package/dist/core/count-recovery/conditional-proof.d.ts +19 -0
- package/dist/core/count-recovery/conditional-proof.js +306 -0
- package/dist/core/count-recovery/derivability-closure.d.ts +14 -0
- package/dist/core/count-recovery/derivability-closure.js +533 -0
- package/dist/core/count-recovery/empty-count-source.d.ts +2 -0
- package/dist/core/count-recovery/empty-count-source.js +40 -0
- package/dist/core/count-recovery/field-bound-lemmas.d.ts +23 -0
- package/dist/core/count-recovery/field-bound-lemmas.js +640 -0
- package/dist/core/count-recovery/field-case.d.ts +5 -0
- package/dist/core/count-recovery/field-case.js +89 -0
- package/dist/core/count-recovery/field-demand.d.ts +46 -0
- package/dist/core/count-recovery/field-demand.js +47 -0
- package/dist/core/count-recovery/field-only.d.ts +3 -0
- package/dist/core/count-recovery/field-only.js +18 -0
- package/dist/core/count-recovery/fold-path.d.ts +6 -0
- package/dist/core/count-recovery/fold-path.js +35 -0
- package/dist/core/count-recovery/frame-lemmas.d.ts +9 -0
- package/dist/core/count-recovery/frame-lemmas.js +120 -0
- package/dist/core/count-recovery/guard-cases.d.ts +12 -0
- package/dist/core/count-recovery/guard-cases.js +138 -0
- package/dist/core/count-recovery/key-closure.d.ts +14 -0
- package/dist/core/count-recovery/key-closure.js +62 -0
- package/dist/core/count-recovery/leaf-allowance.d.ts +4 -0
- package/dist/core/count-recovery/leaf-allowance.js +7 -0
- package/dist/core/count-recovery/lift-count-proxies.d.ts +6 -0
- package/dist/core/count-recovery/lift-count-proxies.js +175 -0
- package/dist/core/count-recovery/live-dependencies.d.ts +6 -0
- package/dist/core/count-recovery/live-dependencies.js +76 -0
- package/dist/core/count-recovery/member-inclusions.d.ts +11 -0
- package/dist/core/count-recovery/member-inclusions.js +168 -0
- package/dist/core/count-recovery/member-premises.d.ts +29 -0
- package/dist/core/count-recovery/member-premises.js +114 -0
- package/dist/core/count-recovery/natural-cases.d.ts +6 -0
- package/dist/core/count-recovery/natural-cases.js +59 -0
- package/dist/core/count-recovery/ops.d.ts +6 -0
- package/dist/core/count-recovery/ops.js +8 -0
- package/dist/core/count-recovery/pre-state-source.d.ts +8 -0
- package/dist/core/count-recovery/pre-state-source.js +18 -0
- package/dist/core/count-recovery/rebase-path.d.ts +9 -0
- package/dist/core/count-recovery/rebase-path.js +70 -0
- package/dist/core/count-recovery/rename-proof.d.ts +2 -0
- package/dist/core/count-recovery/rename-proof.js +102 -0
- package/dist/core/count-recovery/row-cases.d.ts +16 -0
- package/dist/core/count-recovery/row-cases.js +291 -0
- package/dist/core/count-recovery/scalar-guards.d.ts +7 -0
- package/dist/core/count-recovery/scalar-guards.js +291 -0
- package/dist/core/count-recovery/source-memo.d.ts +12 -0
- package/dist/core/count-recovery/source-memo.js +67 -0
- package/dist/core/count-select.d.ts +16 -0
- package/dist/core/count-select.js +24 -0
- package/dist/core/count-support.d.ts +598 -0
- package/dist/core/count-support.js +2080 -0
- package/dist/core/count-unions.d.ts +2 -0
- package/dist/core/count-unions.js +139 -0
- package/dist/core/count-witness-flags.d.ts +9 -0
- package/dist/core/count-witness-flags.js +37 -0
- package/dist/core/count-write-frames.d.ts +12 -0
- package/dist/core/count-write-frames.js +206 -0
- package/dist/core/counts.d.ts +20 -2
- package/dist/core/counts.js +807 -156
- package/dist/core/dsl.d.ts +50 -6
- package/dist/core/dsl.js +307 -65
- package/dist/core/effect.js +8 -1
- package/dist/core/exists.d.ts +1 -1
- package/dist/core/exists.js +80 -15
- package/dist/core/extract-check.d.ts +7 -1
- package/dist/core/extract-check.js +130 -13
- package/dist/core/extract-read.d.ts +22 -8
- package/dist/core/extract-read.js +29 -11
- package/dist/core/extract-shapes.js +122 -41
- package/dist/core/extract-standalone.js +53 -9
- package/dist/core/extract.js +9 -3
- package/dist/core/field-laws.d.ts +2 -0
- package/dist/core/field-laws.js +100 -0
- package/dist/core/finite-frontiers.d.ts +5 -0
- package/dist/core/finite-frontiers.js +116 -0
- package/dist/core/finite-houdini.d.ts +52 -0
- package/dist/core/finite-houdini.js +460 -0
- package/dist/core/finite-model.d.ts +13 -0
- package/dist/core/finite-model.js +68 -0
- package/dist/core/finite-tiny.d.ts +72 -0
- package/dist/core/finite-tiny.js +1052 -0
- package/dist/core/infer.d.ts +1 -1
- package/dist/core/infer.js +32 -9
- package/dist/core/laws.d.ts +2 -1
- package/dist/core/laws.js +47 -14
- package/dist/core/lemma-cuts.d.ts +38 -0
- package/dist/core/lemma-cuts.js +365 -0
- package/dist/core/machine.d.ts +39 -2
- package/dist/core/machine.js +240 -35
- package/dist/core/mutate.d.ts +55 -0
- package/dist/core/mutate.js +210 -0
- package/dist/core/native-domain.d.ts +6 -0
- package/dist/core/native-domain.js +114 -0
- package/dist/core/native-law.d.ts +3 -0
- package/dist/core/native-law.js +174 -0
- package/dist/core/quorum-laws.d.ts +5 -0
- package/dist/core/quorum-laws.js +239 -0
- package/dist/core/relational.js +10 -2
- package/dist/core/relevance.d.ts +4 -0
- package/dist/core/relevance.js +79 -0
- package/dist/core/runtime-wall.d.ts +3 -0
- package/dist/core/runtime-wall.js +4 -0
- package/dist/core/sample-step.d.ts +16 -0
- package/dist/core/sample-step.js +28 -0
- package/dist/core/source-check.d.ts +44 -0
- package/dist/core/source-check.js +126 -0
- package/dist/core/source-diagnostic.d.ts +1 -0
- package/dist/core/source-diagnostic.js +34 -0
- package/dist/core/support-budget.d.ts +2 -0
- package/dist/core/support-budget.js +6 -0
- package/dist/core/system.js +2 -0
- package/dist/core/wall.d.ts +6 -0
- package/dist/core/wall.js +23 -0
- package/dist/core/watch.js +4 -0
- package/dist/kernel/cdcl.d.ts +4 -0
- package/dist/kernel/cdcl.js +20 -5
- package/dist/kernel/kernel.d.ts +10 -0
- package/dist/kernel/kernel.js +56 -3
- package/dist/kernel/model-sequence.d.ts +2 -2
- package/dist/kernel/model-sequence.js +4 -3
- package/dist/kernel/model-values.d.ts +2 -0
- package/dist/kernel/model-values.js +60 -0
- package/dist/kernel/prove.d.ts +10 -0
- package/dist/kernel/prove.js +56 -8
- package/dist/kernel/prover-rows.d.ts +29 -0
- package/dist/kernel/prover-rows.js +114 -0
- package/dist/lean-kernel/emit.d.ts +39 -2
- package/dist/lean-kernel/emit.js +136 -24
- package/dist/lean-kernel/lean-terms.d.ts +1 -0
- package/dist/lean-kernel/lean-terms.js +13 -5
- package/dist/lean-kernel/library.js +4 -0
- package/dist/lean-kernel/qalpha.d.ts +7 -0
- package/dist/lean-kernel/qalpha.js +40 -0
- package/package.json +5 -1
package/dist/core/cli.js
CHANGED
|
@@ -10,6 +10,7 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
|
|
|
10
10
|
import { fiberRun } from "./fiber-run.js";
|
|
11
11
|
import { securityTemplates } from "./security.js";
|
|
12
12
|
import { proveLeadsTo, proveRule, proveSteps } from "./dsl.js";
|
|
13
|
+
import { sourceDiagnostic } from "./source-diagnostic.js";
|
|
13
14
|
import { LawError, buildLaw, buildLeadsTo, buildTwoRuns, citesIn, isLeadsTo, isTwoRuns, lawsIn, namesWithClock } from "./laws.js";
|
|
14
15
|
import { certify, proveTwoRuns } from "./relational.js";
|
|
15
16
|
import { contend, envOf, inEnv, shrinkRun } from "./system.js";
|
|
@@ -278,6 +279,11 @@ const merge = (a, b) => {
|
|
|
278
279
|
out[k] = v && typeof v === 'object' && !Array.isArray(v) && out[k] && typeof out[k] === 'object' && !Array.isArray(out[k]) ? merge(out[k], v) : v;
|
|
279
280
|
return out;
|
|
280
281
|
};
|
|
282
|
+
const translationOf = (v) => ({
|
|
283
|
+
...(v.translation ? { translation: v.translation } : {}), ...(v.translationChecks !== undefined ? { translationChecks: v.translationChecks } : {}),
|
|
284
|
+
...(v.kernelProven !== undefined ? { kernelProven: v.kernelProven } : {}),
|
|
285
|
+
...(v.translationWitness ? { translationWitness: sourceDiagnostic(v.translationWitness) } : {}),
|
|
286
|
+
});
|
|
281
287
|
// Each initializer is a separate scope, with its own original-code replay and laws. Never choose one
|
|
282
288
|
// overload on the caller's behalf or let a successful variant hide another variant's refusal.
|
|
283
289
|
async function expandInitializers(t, o, laws, rows) {
|
|
@@ -700,7 +706,7 @@ function prove(x0, l, ms, proven, ctx) {
|
|
|
700
706
|
catch (e) {
|
|
701
707
|
return { ...base, kind: 'leads to', verdict: 'unknown', why: e.message, ms: Date.now() - t0 };
|
|
702
708
|
}
|
|
703
|
-
return { ...base, kind: 'leads to', verdict: v.proven && v.checked ? 'proven' : 'unknown', ...(v.proven && v.checked ? {} : { why: [v.goal, v.stuck, v.why].filter(Boolean).join('; ') || 'not proven' }), ms: Date.now() - t0 };
|
|
709
|
+
return { ...base, ...translationOf(v), kind: 'leads to', verdict: v.proven && v.checked ? 'proven' : 'unknown', ...(v.proven && v.checked ? {} : { why: [v.goal, v.stuck, v.why].filter(Boolean).join('; ') || 'not proven' }), ms: Date.now() - t0 };
|
|
704
710
|
}
|
|
705
711
|
let b, two;
|
|
706
712
|
// (a law that can't be built is not read, with why: never the whole check aborted)
|
|
@@ -725,9 +731,9 @@ function prove(x0, l, ms, proven, ctx) {
|
|
|
725
731
|
catch (e) {
|
|
726
732
|
return { ...base, kind: 'law', verdict: 'unknown', why: e.message, ms: Date.now() - t0 };
|
|
727
733
|
}
|
|
728
|
-
if (r.run)
|
|
729
|
-
return { ...base, kind: 'law', verdict: 'broken', run: runOf(x, r.run), ms: Date.now() - t0 };
|
|
730
|
-
return { ...base, kind: 'law', verdict: 'unknown', why: r.proven && r.checked ? `true in every state a run from the start reaches, not inductive from every state the laws allow (from = "any"): state the law it needs beside it${r.found?.laws?.length ? `: ${r.found.laws.map(f => f.text).join('; ')}` : ''}` : r.why ?? r.stuck ?? 'not proven', ...(r.found?.laws?.length ? { found: r.found.laws.map(f => f.text) } : {}), ms: Date.now() - t0 };
|
|
734
|
+
if (r.run && r.translation !== 'unchecked')
|
|
735
|
+
return { ...base, ...translationOf(r), kind: 'law', verdict: 'broken', run: runOf(x, r.run), ms: Date.now() - t0 };
|
|
736
|
+
return { ...base, ...translationOf(r), kind: 'law', verdict: 'unknown', why: r.proven && r.checked ? `true in every state a run from the start reaches, not inductive from every state the laws allow (from = "any"): state the law it needs beside it${r.found?.laws?.length ? `: ${r.found.laws.map(f => f.text).join('; ')}` : ''}` : r.why ?? r.stuck ?? 'not proven', ...(r.found?.laws?.length ? { found: r.found.laws.map(f => f.text) } : {}), ms: Date.now() - t0 };
|
|
731
737
|
}
|
|
732
738
|
// (from any state: a law about states proven with the others stated beside it, as one inductive set)
|
|
733
739
|
const beside = () => (ctx?.together ?? []).filter(m => m.name !== l.name).flatMap(m => { try {
|
|
@@ -757,6 +763,10 @@ function prove(x0, l, ms, proven, ctx) {
|
|
|
757
763
|
}
|
|
758
764
|
const brokenAt = !!(ctx?.from && !v.proven && !v.run && v.stuck === 'sat' && v.witness);
|
|
759
765
|
let verdict = v.proven && v.checked && !(ctx?.from && v.assumes) ? 'proven' : v.run || brokenAt ? 'broken' : 'unknown', replayed;
|
|
766
|
+
if (v.translation === 'unchecked')
|
|
767
|
+
verdict = 'unknown';
|
|
768
|
+
if (v.translation === 'error')
|
|
769
|
+
verdict = 'not read';
|
|
760
770
|
if (ctx?.from && v.proven && v.assumes)
|
|
761
771
|
v.why = `proven only in states a run from the start reaches (from = "any" asks for every state the laws allow): it needs ${v.assumes.laws.join('; ') || 'k-induction'}`;
|
|
762
772
|
// (a run the front end replays on the code: broken only where the code runs it so)
|
|
@@ -786,7 +796,7 @@ function prove(x0, l, ms, proven, ctx) {
|
|
|
786
796
|
fs.writeFileSync(cert, JSON.stringify(doc));
|
|
787
797
|
}
|
|
788
798
|
}
|
|
789
|
-
return { ...base, kind, verdict, ...(ctx?.from && verdict === 'proven' ? { from: 'any' } : {}), ...(replayed ? { replayed } : {}), ...(v.run && verdict === 'broken' ? { run: two ? pairRunOf(x, v.run, two) : runOf(x, v.run) } : {}), ...(verdict === 'unknown' || (verdict === 'broken' && !v.run) ? { why: [v.why ?? v.stuck ?? 'not proven', ...(at.length ? [`a state the laws allow where a step breaks it, no run from the start found reaching it: ${at.join(', ')}`] : [])].join('; ') } : {}),
|
|
799
|
+
return { ...base, ...translationOf(v), kind, verdict, ...(ctx?.from && verdict === 'proven' ? { from: 'any' } : {}), ...(replayed ? { replayed } : {}), ...(v.run && verdict === 'broken' ? { run: two ? pairRunOf(x, v.run, two) : runOf(x, v.run) } : {}), ...(verdict === 'unknown' || verdict === 'not read' || (verdict === 'broken' && !v.run) ? { why: [v.why ?? v.stuck ?? 'not proven', ...(at.length ? [`a state the laws allow where a step breaks it, no run from the start found reaching it: ${at.join(', ')}`] : [])].join('; ') } : {}),
|
|
790
800
|
...(v.run && !two ? { assumes: assumedOf(x, v.run) } : {}), ...(found?.length ? { found } : {}), ...(cert ? { cert } : {}), ms: Date.now() - t0 };
|
|
791
801
|
}
|
|
792
802
|
// A law of a rule in its environment (core/system.ts's inEnv): built on the rule as the code's names read it, then
|
|
@@ -825,11 +835,11 @@ function proveInEnv(x, l, ms, proven, ctx) {
|
|
|
825
835
|
if (run)
|
|
826
836
|
v = { ...v, run: run };
|
|
827
837
|
}
|
|
828
|
-
const verdict = v.proven && v.checked ? 'proven' : v.run ? 'broken' : 'unknown';
|
|
838
|
+
const verdict = v.translation === 'unchecked' ? 'unknown' : v.translation === 'error' ? 'not read' : v.proven && v.checked ? 'proven' : v.run ? 'broken' : 'unknown';
|
|
829
839
|
if (verdict === 'proven' && b.kind === 'law')
|
|
830
840
|
proven.push(law);
|
|
831
841
|
const found = v.found?.laws?.map(f => f.text);
|
|
832
|
-
return { ...base, kind: b.kind, verdict, ...(v.run && verdict === 'broken' ? { run: sysRunOf(x, sys, shrinkRun(sys.rule, law, v.run)) } : {}), ...(verdict === 'unknown' ? { why: v.why ?? v.stuck ?? 'not proven' } : {}), ...(found?.length ? { found } : {}), ms: Date.now() - t0 };
|
|
842
|
+
return { ...base, ...translationOf(v), kind: b.kind, verdict, ...(v.run && verdict === 'broken' ? { run: sysRunOf(x, sys, shrinkRun(sys.rule, law, v.run)) } : {}), ...(verdict === 'unknown' || verdict === 'not read' ? { why: v.why ?? v.stuck ?? 'not proven' } : {}), ...(found?.length ? { found } : {}), ms: Date.now() - t0 };
|
|
833
843
|
}
|
|
834
844
|
// A run of the system as each call's steps: a call begins with its request, reads, commits; a store's key expires.
|
|
835
845
|
function sysRunOf(x, sys, run) {
|
|
@@ -1062,6 +1072,8 @@ function printRule(r, o) {
|
|
|
1062
1072
|
out(` proven with what the search found: ${l.found.join('; ')}`);
|
|
1063
1073
|
if (l.why && l.verdict !== 'proven')
|
|
1064
1074
|
out(` ${l.why}`);
|
|
1075
|
+
if (l.translationWitness)
|
|
1076
|
+
out(` translation witness: ${JSON.stringify(l.translationWitness)}`);
|
|
1065
1077
|
if (proposed)
|
|
1066
1078
|
out(` ${l.where}`);
|
|
1067
1079
|
};
|
|
@@ -1090,6 +1102,7 @@ function printRule(r, o) {
|
|
|
1090
1102
|
}
|
|
1091
1103
|
const label = (n) => (n.role === 'answer' ? `${n.code.split('.').pop()}(...)` : n.role === 'input' && n.method ? `${n.method}(${n.code})` : n.role === 'result' && n.method ? `${n.method}() result` : n.code);
|
|
1092
1104
|
const USAGE = `gdp-wired check <file|dir>... [--entry X] [--mode standalone] [--methods a,b] [--failures kept] [--on-error discard] [--json] [--ms 10000] [--no-propose] [--clock s|ms] [--cert <dir>] [--from any] [--require-review] [--security]
|
|
1105
|
+
gdp-wired mutate <config.json> [--json]
|
|
1093
1106
|
gdp-wired draft <file> --clause <text> | --clause-file <file> --cite <ref> [--llm <command> | --answer <file>] [--by <who>]
|
|
1094
1107
|
|
|
1095
1108
|
Reads the rule in each file (Python, Ruby, Elixir, Java, Kotlin, Go, Rust, Swift, C, C++, TypeScript, Effect, XState), checks it
|
|
@@ -1114,7 +1127,7 @@ export async function check(targets, given = {}, each) {
|
|
|
1114
1127
|
rs.forEach(each);
|
|
1115
1128
|
}
|
|
1116
1129
|
const stated = all.flatMap(r => r.laws);
|
|
1117
|
-
return { rules: all, exit: stated.some(l => l.verdict === 'broken') ? 1 : stated.some(l => l.verdict === 'not read') || o.security && all.some(r => !r.ok) ? 2 : 0 };
|
|
1130
|
+
return { rules: all, exit: stated.some(l => l.translation === 'unchecked' || l.translation === 'error') ? 2 : stated.some(l => l.verdict === 'broken') ? 1 : stated.some(l => l.verdict === 'not read') || o.security && all.some(r => !r.ok) ? 2 : 0 };
|
|
1118
1131
|
}
|
|
1119
1132
|
// --- draft: laws from a clause, for a person to review ----------------------------------------------------------------
|
|
1120
1133
|
// gdp-wired draft <file> --clause <text> | --clause-file <f> --cite <ref> [--llm <command> | --answer <file>] [--by <who>]:
|
|
@@ -1166,6 +1179,13 @@ async function draft(target, o) {
|
|
|
1166
1179
|
// --require-review, one cited unreviewed).
|
|
1167
1180
|
export async function main(argv) {
|
|
1168
1181
|
const { cmd, targets, o } = args(argv);
|
|
1182
|
+
if (cmd === 'mutate' && targets.length === 1) {
|
|
1183
|
+
const { checkMutations } = await import("./mutate.js");
|
|
1184
|
+
const file = path.resolve(targets[0]), config = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
1185
|
+
const result = checkMutations({ ...config, cwd: path.resolve(path.dirname(file), config.cwd ?? '.') });
|
|
1186
|
+
out(o.json ? JSON.stringify(result, null, 1) : result.markdown);
|
|
1187
|
+
return result.passed ? 0 : 1;
|
|
1188
|
+
}
|
|
1169
1189
|
if (cmd === 'draft' && targets.length === 1)
|
|
1170
1190
|
return draft(targets[0], o);
|
|
1171
1191
|
if (cmd !== 'check' || !targets.length) {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Law, type Machine } from './machine.js';
|
|
2
|
+
import { type CountCert, type CountDecl, type Goal, type Instances } from '../kernel/kernel.js';
|
|
3
|
+
type Packet = {
|
|
4
|
+
goal: Goal;
|
|
5
|
+
decls: CountDecl[];
|
|
6
|
+
};
|
|
7
|
+
export declare function finiteCountChoice(m: Machine): [string, Set<number>] | undefined;
|
|
8
|
+
export { selectCountBounds, type CountBoundCandidate, type CountBoundSelection } from './count-select.js';
|
|
9
|
+
export declare function withoutCountWitnesses(m: Machine, laws: Law[], at: Instances, discard: ReadonlySet<string>): Instances;
|
|
10
|
+
export type CountCaseOptions = {
|
|
11
|
+
deferCheck?: boolean;
|
|
12
|
+
propose?: (goal: Goal, decls: CountDecl[]) => Pick<CountCert, 'reads' | 'insts'>;
|
|
13
|
+
localModels?: boolean;
|
|
14
|
+
saturate?: boolean;
|
|
15
|
+
queryMs?: number;
|
|
16
|
+
coreLimit?: number;
|
|
17
|
+
units?: boolean;
|
|
18
|
+
corePointOnly?: boolean;
|
|
19
|
+
trace?: (event: Record<string, unknown>) => void;
|
|
20
|
+
};
|
|
21
|
+
export declare function proveCountCases(m: Machine, laws: Law[], at: Instances, cert: Pick<CountCert, 'reads' | 'insts'>, until: number, build: (at: Instances) => Packet, options?: CountCaseOptions): {
|
|
22
|
+
instances: Instances;
|
|
23
|
+
cert: CountCert;
|
|
24
|
+
} | undefined;
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// Untrusted finite-input case search for a fixed counted certificate. A small
|
|
2
|
+
// model/core query chooses ordinary instances; every combined tree is checked
|
|
3
|
+
// against the unspecialized counted obligation before it is returned, unless
|
|
4
|
+
// an enclosing final-certificate check explicitly defers this pre-check.
|
|
5
|
+
import { walkOnce, x } from "./machine.js";
|
|
6
|
+
import { boundOf, canon, check, checkMC, linear, mcGoal, truth } from "../kernel/kernel.js";
|
|
7
|
+
import { atPoint, clock, grow, modelInstances, pastWall, usedHyps, wallAt, widen, withDeadline } from "../kernel/prove.js";
|
|
8
|
+
import { sub } from "./solver.js";
|
|
9
|
+
import { seedCountWitnessFlags } from "./count-witness-flags.js";
|
|
10
|
+
import { modelSequence } from "../kernel/model-sequence.js";
|
|
11
|
+
import { REFUTED } from "../kernel/cdcl.js";
|
|
12
|
+
import { selectCountBounds } from "./count-select.js";
|
|
13
|
+
import { actionGuard } from "./count-recovery/action-guard.js";
|
|
14
|
+
import { guardCases } from "./count-recovery/guard-cases.js";
|
|
15
|
+
import { appendCountCert } from "./count-cert-grow.js";
|
|
16
|
+
import { independentCountInstances } from "./count-focused.js";
|
|
17
|
+
import { fieldCase } from "./count-recovery/field-case.js";
|
|
18
|
+
import { sourceProofMemo } from "./count-recovery/source-memo.js";
|
|
19
|
+
import { collectConditionalCertificate, hasConditionalProof, transportConditionalProof } from "./count-recovery/conditional-proof.js";
|
|
20
|
+
import { activeWriteKey, rowPairs, writesReadState } from "./count-recovery/row-cases.js";
|
|
21
|
+
import { frameLemmas } from "./count-recovery/frame-lemmas.js";
|
|
22
|
+
import { refuteFast, rowsAt } from "./count-recovery/ops.js";
|
|
23
|
+
export function finiteCountChoice(m) {
|
|
24
|
+
const values = new Map();
|
|
25
|
+
walkOnce([m.step, m.assume], (o) => {
|
|
26
|
+
if (o.op !== '=')
|
|
27
|
+
return;
|
|
28
|
+
const [a, b] = o.a.op === 'var' ? [o.a, o.b] : [o.b, o.a];
|
|
29
|
+
if (a.op !== 'var' || b.op !== 'const' || !m.inputs.includes(a.name))
|
|
30
|
+
return;
|
|
31
|
+
const vs = values.get(a.name) ?? new Set();
|
|
32
|
+
vs.add(b.value);
|
|
33
|
+
values.set(a.name, vs);
|
|
34
|
+
});
|
|
35
|
+
return [...values].filter(([, v]) => v.size > 2 && v.size <= 16).sort((a, b) => b[1].size - a[1].size)[0];
|
|
36
|
+
}
|
|
37
|
+
export { selectCountBounds } from "./count-select.js";
|
|
38
|
+
// Remove whole tuples mentioning count witnesses no longer in the packet.
|
|
39
|
+
// This only weakens the available source context; its proof is checked again.
|
|
40
|
+
export function withoutCountWitnesses(m, laws, at, discard) {
|
|
41
|
+
if (!discard.size)
|
|
42
|
+
return at;
|
|
43
|
+
const out = {};
|
|
44
|
+
for (const [name, terms] of Object.entries(at)) {
|
|
45
|
+
const law = laws.find(l => l.name === name), flag = name.startsWith('?') ? m.step.flags?.[name.slice(1)] : undefined;
|
|
46
|
+
const arity = law ? 1 + (law.keys?.length ?? 0) : flag?.op === 'all' ? boundOf(flag).length : 1, kept = [];
|
|
47
|
+
for (let i = 0; i < terms.length; i += arity) {
|
|
48
|
+
const tuple = terms.slice(i, i + arity);
|
|
49
|
+
let unwanted = false;
|
|
50
|
+
walkOnce(tuple, (o) => { if (o.op === 'var' && discard.has(o.name))
|
|
51
|
+
unwanted = true; });
|
|
52
|
+
if (!unwanted)
|
|
53
|
+
kept.push(...tuple);
|
|
54
|
+
}
|
|
55
|
+
out[name] = kept;
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
function selectedCase(m, laws, at, cert, packet, path, until, build, options) {
|
|
60
|
+
const full = mcGoal(packet.goal.hyps, packet.decls, cert);
|
|
61
|
+
if (!full || !cert.insts.length)
|
|
62
|
+
return undefined;
|
|
63
|
+
const offset = full.hyps.length - cert.insts.length, base = { ...full, hyps: full.hyps.slice(0, offset) }, excluded = new Set();
|
|
64
|
+
const candidates = cert.insts.flatMap((_, index) => { const h = full.hyps[offset + index]; return h?.op === 'or' && h.xs[0]?.op === '<=' ? [{ index, bound: h.xs[0] }] : []; });
|
|
65
|
+
while (clock() < until && !pastWall()) {
|
|
66
|
+
const selected = selectCountBounds(base, candidates.filter(c => !excluded.has(c.index)), until, { path, queryMs: options.queryMs, trace: options.trace });
|
|
67
|
+
if (!selected || selected.fallback)
|
|
68
|
+
return undefined;
|
|
69
|
+
const chosen = new Set(selected.indices), insts = cert.insts.filter((_, i) => chosen.has(i)), slim = { ...cert, insts };
|
|
70
|
+
const discard = new Set(cert.insts.filter((_, i) => !chosen.has(i)).map(c => c.wit)), clean = withoutCountWitnesses(m, laws, at, discard);
|
|
71
|
+
const instances = seedCountWitnessFlags(m, clean, [{ decls: packet.decls, cert: slim }], true), source = build(instances), goal = mcGoal(source.goal.hyps, source.decls, slim);
|
|
72
|
+
if (!goal)
|
|
73
|
+
return undefined;
|
|
74
|
+
let confirmed = new Map(path), failed;
|
|
75
|
+
const cuts = [];
|
|
76
|
+
for (const index of selected.indices) {
|
|
77
|
+
const bound = candidates.find(c => c.index === index).bound;
|
|
78
|
+
if (truth(bound, confirmed) === true)
|
|
79
|
+
continue;
|
|
80
|
+
if (bound.op !== '<=') {
|
|
81
|
+
failed = index;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
const a = linear(bound.a, confirmed), b = linear(bound.b, confirmed);
|
|
85
|
+
if (!a || !b) {
|
|
86
|
+
failed = index;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
const split = sub(a, b), id = canon(split), noPath = new Map(confirmed).set(id, { lin: split, holds: false });
|
|
90
|
+
const proof = grow(goal, noPath, { nodes: 0, until: Math.min(until, clock() + (options.queryMs ?? 200)), units: options.units });
|
|
91
|
+
if (!('tree' in proof)) {
|
|
92
|
+
failed = index;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
cuts.push({ split, no: proof.tree });
|
|
96
|
+
confirmed = new Map(confirmed).set(id, { lin: split, holds: true });
|
|
97
|
+
}
|
|
98
|
+
if (failed !== undefined) {
|
|
99
|
+
excluded.add(failed);
|
|
100
|
+
options.trace?.({ stage: 'count-core-excluded', index: failed, excluded: [...excluded] });
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const closed = grow(goal, confirmed, { nodes: 0, until, units: options.units });
|
|
104
|
+
if (!('tree' in closed))
|
|
105
|
+
return undefined;
|
|
106
|
+
let tree = closed.tree;
|
|
107
|
+
for (const cut of cuts.reverse())
|
|
108
|
+
tree = { split: cut.split, yes: tree, no: cut.no };
|
|
109
|
+
if (!check(goal, tree, path))
|
|
110
|
+
return undefined;
|
|
111
|
+
options.trace?.({ stage: 'count-case-certificate', before: cert.insts.length, after: insts.length, indices: selected.indices, hyps: goal.hyps.length });
|
|
112
|
+
return { goal, tree, indices: selected.indices };
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
export function proveCountCases(m, laws, at, cert, until, build, options = {}) {
|
|
117
|
+
return withDeadline(until, () => {
|
|
118
|
+
if (clock() >= until || pastWall())
|
|
119
|
+
return undefined;
|
|
120
|
+
const choice = finiteCountChoice(m);
|
|
121
|
+
if (!choice)
|
|
122
|
+
return undefined;
|
|
123
|
+
const [name, vs] = choice, cuts = [...vs].sort((a, b) => a - b), path = new Map(), conditions = [];
|
|
124
|
+
const splitAt = (v) => v ? { [name]: 1, '1': -v } : { [name]: 1 };
|
|
125
|
+
let activeCert = cert, packet = build(at), root = mcGoal(packet.goal.hyps, packet.decls, activeCert);
|
|
126
|
+
if (!root)
|
|
127
|
+
return undefined;
|
|
128
|
+
let goal = { ...root, name: packet.goal.name };
|
|
129
|
+
const rebuild = () => {
|
|
130
|
+
packet = build(at);
|
|
131
|
+
if (options.propose) {
|
|
132
|
+
const known = new Set(cert.reads.map(r => JSON.stringify(r.slice(1))));
|
|
133
|
+
let missing = false;
|
|
134
|
+
walkOnce(packet.goal.hyps, (o) => { if (o.op === 'get' && packet.decls.some(d => d.map === o.map) && !known.has(JSON.stringify([o.map, o.key])))
|
|
135
|
+
missing = true; });
|
|
136
|
+
if (missing && appendCountCert(cert, options.propose(packet.goal, packet.decls), packet.goal.hyps)) {
|
|
137
|
+
at = seedCountWitnessFlags(m, at, [{ decls: packet.decls, cert }], true);
|
|
138
|
+
packet = build(at);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
root = mcGoal(packet.goal.hyps, packet.decls, activeCert);
|
|
142
|
+
if (root)
|
|
143
|
+
goal = { ...root, name: packet.goal.name };
|
|
144
|
+
return !!root;
|
|
145
|
+
};
|
|
146
|
+
const seq = modelSequence(), records = [], branches = [];
|
|
147
|
+
const fallbackPath = new Map(cuts.map(value => { const lin = splitAt(value); return [canon(lin), { lin, holds: false }]; })), fallbackResult = refuteFast(rowsAt(goal, fallbackPath), 256);
|
|
148
|
+
const fallbackTree = fallbackResult?.kind === 'refuted' ? { leaf: fallbackResult.steps } : undefined, needed = fallbackTree ? usedHyps(goal, fallbackTree, fallbackPath) : new Set();
|
|
149
|
+
const fallback = fallbackTree ? { goal, tree: fallbackTree, indices: cert.insts.flatMap((_, i) => needed.has(goal.hyps.length - cert.insts.length + i) ? [i] : []) } : undefined;
|
|
150
|
+
options.trace?.({ stage: 'case-fallback', proposed: !!fallback });
|
|
151
|
+
const memo = sourceProofMemo(m, packet.goal.name), initial = at, merged = { ...at }, previous = atPoint.on;
|
|
152
|
+
atPoint.on = !options.corePointOnly;
|
|
153
|
+
const coreGrow = (g, end) => { const before = atPoint.on; atPoint.on = true; try {
|
|
154
|
+
return grow(g, new Map(), { nodes: 0, until: end, units: options.units });
|
|
155
|
+
}
|
|
156
|
+
finally {
|
|
157
|
+
atPoint.on = before;
|
|
158
|
+
} };
|
|
159
|
+
try {
|
|
160
|
+
for (const [i, value] of [...cuts, undefined].entries()) {
|
|
161
|
+
if (clock() >= until || pastWall())
|
|
162
|
+
return undefined;
|
|
163
|
+
const split = value === undefined ? undefined : splitAt(value), cut = value === undefined ? undefined : { op: '<=', a: x(name), b: { op: 'const', value } };
|
|
164
|
+
if (split)
|
|
165
|
+
path.set(canon(split), { lin: split, holds: true });
|
|
166
|
+
const root = new Map(path), tails = options.saturate && value !== undefined ? cuts.filter(v => v > value) : [];
|
|
167
|
+
for (const v of tails) {
|
|
168
|
+
const lin = splitAt(v);
|
|
169
|
+
path.set(canon(lin), { lin, holds: true });
|
|
170
|
+
}
|
|
171
|
+
const prefix = [...conditions, ...cut ? [cut] : [], ...tails.map(v => ({ op: '<=', a: x(name), b: { op: 'const', value: v } }))];
|
|
172
|
+
branches.push({ prefix, path: new Map(path), root, tails });
|
|
173
|
+
if (value === undefined && fallback) {
|
|
174
|
+
records.push({ ...fallback, path: new Map(path), from: fallbackPath });
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (options.localModels) {
|
|
178
|
+
at = initial;
|
|
179
|
+
if (!rebuild())
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
let tree, source, from, indices = cert.insts.map((_, j) => j);
|
|
183
|
+
const caseEnd = Math.min(until, clock() + Math.max(600, (until - clock()) / (cuts.length + 1 - i - (fallback ? 1 : 0))));
|
|
184
|
+
const claim = packet.goal.hyps.at(-1), rowDependent = activeWriteKey(m, path) !== undefined && rowPairs(m, cert, path, claim).length > 0;
|
|
185
|
+
tree = rowDependent ? undefined : fieldCase(m, goal, cert, claim, path, Math.min(caseEnd, clock() + 150), e => options.trace?.({ value, ...e }));
|
|
186
|
+
if (tree) {
|
|
187
|
+
source = goal;
|
|
188
|
+
from = new Map(path);
|
|
189
|
+
}
|
|
190
|
+
if (!tree && !writesReadState(m, cert, path, claim)) {
|
|
191
|
+
tree = frameLemmas(goal, cert, Math.min(caseEnd, clock() + 150), [], path, { deferCheck: true });
|
|
192
|
+
options.trace?.({ value, stage: 'write-free-case', proposed: !!tree });
|
|
193
|
+
if (tree) {
|
|
194
|
+
source = goal;
|
|
195
|
+
from = new Map(path);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const guard = actionGuard(m, name, value);
|
|
199
|
+
if (!tree && guard) {
|
|
200
|
+
const events = [], begun = clock(), derived = guardCases(m, packet.decls, goal, cert, guard, caseEnd, events, 0, path, { deferCheck: true, claim: packet.goal.hyps.at(-1), memo, conditional: true });
|
|
201
|
+
for (const event of events)
|
|
202
|
+
options.trace?.({ value, ...event });
|
|
203
|
+
options.trace?.({ value, stage: 'case-derivations', ms: Math.round(clock() - begun), proposed: !!derived });
|
|
204
|
+
if (derived) {
|
|
205
|
+
tree = derived;
|
|
206
|
+
source = goal;
|
|
207
|
+
from = new Map(path);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const selected = !tree && selectedCase(m, laws, at, cert, packet, path, caseEnd, build, options);
|
|
211
|
+
if (!tree)
|
|
212
|
+
packet = build(at); // Restore a builder after local selection attempts.
|
|
213
|
+
if (selected) {
|
|
214
|
+
const kept = new Set(selected.indices), slim = { ...cert, insts: cert.insts.filter((_, j) => kept.has(j)) };
|
|
215
|
+
at = seedCountWitnessFlags(m, at, [{ decls: packet.decls, cert: slim }], true);
|
|
216
|
+
tree = selected.tree;
|
|
217
|
+
source = selected.goal;
|
|
218
|
+
from = new Map(path);
|
|
219
|
+
indices = selected.indices;
|
|
220
|
+
}
|
|
221
|
+
for (let round = 0; !tree && round < 6 && clock() < caseEnd && !pastWall(); round++) {
|
|
222
|
+
const part = { name: goal.name, hyps: [...prefix, ...goal.hyps] }, start = clock(), q = seq(part, Math.min(caseEnd, clock() + (options.queryMs ?? 200)));
|
|
223
|
+
options.trace?.({ value, stage: 'query', round, hyps: part.hyps.length, ms: Math.round(clock() - start), outcome: 'tree' in q ? 'tree' : q.stuck, core: q.core?.length });
|
|
224
|
+
if (!('tree' in q) && q.why === REFUTED && q.core?.length && q.core.length <= (options.coreLimit ?? Infinity)) {
|
|
225
|
+
const core = { name: goal.name, hyps: q.core.map(j => part.hyps[j]) }, out = coreGrow(core, caseEnd);
|
|
226
|
+
options.trace?.({ value, stage: 'core', hyps: core.hyps.length, outcome: 'tree' in out ? 'tree' : out.stuck });
|
|
227
|
+
if ('tree' in out) {
|
|
228
|
+
tree = out.tree;
|
|
229
|
+
source = core;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (tree || 'tree' in q || q.stuck !== 'sat' || !q.witness)
|
|
233
|
+
break;
|
|
234
|
+
const proposed = modelInstances(m, laws, at, part, q.witness, 8);
|
|
235
|
+
const next = proposed && independentCountInstances(m, laws, at, proposed, cert.insts.map(i => i.wit));
|
|
236
|
+
if (!next)
|
|
237
|
+
break;
|
|
238
|
+
at = next;
|
|
239
|
+
if (!rebuild())
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
if (!tree && clock() < caseEnd && !pastWall()) {
|
|
243
|
+
const part = { name: goal.name, hyps: [...prefix, ...goal.hyps] }, out = grow(part, new Map(), { nodes: 0, until: caseEnd, units: options.units });
|
|
244
|
+
options.trace?.({ value, stage: 'grow', outcome: 'tree' in out ? 'tree' : out.stuck });
|
|
245
|
+
if ('tree' in out) {
|
|
246
|
+
tree = out.tree;
|
|
247
|
+
source = part;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
records.push(tree && source ? { goal: source, tree, path: new Map(path), indices, ...(from ? { from } : {}) } : undefined);
|
|
251
|
+
if (options.localModels)
|
|
252
|
+
for (const [n, terms] of Object.entries(at))
|
|
253
|
+
merged[n] = [...merged[n] ?? [], ...terms.slice(initial[n]?.length ?? 0)];
|
|
254
|
+
for (const v of tails)
|
|
255
|
+
path.delete(canon(splitAt(v)));
|
|
256
|
+
if (split) {
|
|
257
|
+
path.set(canon(split), { lin: split, holds: false });
|
|
258
|
+
conditions.push({ op: 'not', x: cut });
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (options.localModels) {
|
|
262
|
+
at = merged;
|
|
263
|
+
if (!rebuild())
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
const missing = records.flatMap((r, i) => r ? [] : [i]);
|
|
267
|
+
for (const [j, i] of missing.entries()) {
|
|
268
|
+
if (clock() >= until || pastWall())
|
|
269
|
+
break;
|
|
270
|
+
const { prefix, path: p } = branches[i], part = { name: goal.name, hyps: [...prefix, ...goal.hyps] };
|
|
271
|
+
const end = Math.min(until, clock() + (until - clock()) / (missing.length - j)), guard = actionGuard(m, name, cuts[i]), events = [];
|
|
272
|
+
const derived = guard && guardCases(m, packet.decls, goal, cert, guard, end, events, 0, p, { deferCheck: true, claim: packet.goal.hyps.at(-1), memo, conditional: true });
|
|
273
|
+
for (const event of events)
|
|
274
|
+
options.trace?.({ value: cuts[i], ...event });
|
|
275
|
+
if (derived) {
|
|
276
|
+
records[i] = { goal, tree: derived, path: p, from: p, indices: cert.insts.map((_, j) => j) };
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
const out = grow(part, new Map(), { nodes: 0, until: end, units: options.units });
|
|
280
|
+
if ('tree' in out)
|
|
281
|
+
records[i] = { goal: part, tree: out.tree, path: p, indices: cert.insts.map((_, j) => j) };
|
|
282
|
+
}
|
|
283
|
+
if (records.some(r => !r))
|
|
284
|
+
return undefined;
|
|
285
|
+
const kept = new Set(records.flatMap(r => r?.indices ?? [])), discard = new Set(cert.insts.filter((_, i) => !kept.has(i)).map(c => c.wit));
|
|
286
|
+
activeCert = { ...cert, insts: cert.insts.filter((_, i) => kept.has(i)) };
|
|
287
|
+
at = withoutCountWitnesses(m, laws, at, discard);
|
|
288
|
+
if (!rebuild())
|
|
289
|
+
return undefined;
|
|
290
|
+
options.trace?.({ stage: 'count-cases-certificate', before: cert.insts.length, after: activeCert.insts.length });
|
|
291
|
+
const trees = records.map((r, i) => {
|
|
292
|
+
let tree = r && (hasConditionalProof(r.tree) ? transportConditionalProof(r.goal, goal, r.tree, r.from ?? new Map(), r.path, Math.min(wallAt(), performance.now() + Math.max(0, until - clock()) * 10)) : widen(r.goal, goal, r.tree, {}, r.from ?? new Map(), r.path));
|
|
293
|
+
if (!tree)
|
|
294
|
+
return undefined;
|
|
295
|
+
const { root, tails } = branches[i];
|
|
296
|
+
for (let j = tails.length - 1; j >= 0; j--) {
|
|
297
|
+
const before = new Map(root);
|
|
298
|
+
for (const v of tails.slice(0, j)) {
|
|
299
|
+
const lin = splitAt(v);
|
|
300
|
+
before.set(canon(lin), { lin, holds: true });
|
|
301
|
+
}
|
|
302
|
+
const upper = [...before.keys()].indexOf(canon(splitAt(cuts[i]))), bad = before.size;
|
|
303
|
+
if (upper < 0)
|
|
304
|
+
return undefined;
|
|
305
|
+
tree = { split: splitAt(tails[j]), yes: tree, no: { leaf: [{ kind: 'input', index: upper }, { kind: 'input', index: bad }, { kind: 'combine', a: 0, b: 1, ma: 1, mb: 1 }] } };
|
|
306
|
+
}
|
|
307
|
+
return tree;
|
|
308
|
+
});
|
|
309
|
+
let tree = trees.at(-1);
|
|
310
|
+
for (let i = cuts.length - 1; i >= 0; i--)
|
|
311
|
+
tree = tree && trees[i] ? { split: splitAt(cuts[i]), yes: trees[i], no: tree } : undefined;
|
|
312
|
+
const combined = tree && (hasConditionalProof(tree) ? collectConditionalCertificate(goal, tree, Math.min(wallAt(), performance.now() + Math.max(0, until - clock()) * 10)) : { tree, lemmas: undefined });
|
|
313
|
+
const result = combined && { ...activeCert, ...combined };
|
|
314
|
+
return result && (options.deferCheck || checkMC(packet.goal.hyps, packet.decls, result)) ? { instances: at, cert: result } : undefined;
|
|
315
|
+
}
|
|
316
|
+
finally {
|
|
317
|
+
atPoint.on = previous;
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Extend an untrusted count-certificate proposal without renaming any read or
|
|
2
|
+
// witness already used by a proof tree. All new instances still go through mcGoal
|
|
3
|
+
// and checkMC; this only keeps proposal identities stable across model rounds.
|
|
4
|
+
import { walkOnce } from "./machine.js";
|
|
5
|
+
export function appendCountCert(current, next, context) {
|
|
6
|
+
const used = new Set([...current.reads.map(r => r[0]), ...current.insts.map(i => i.wit)]);
|
|
7
|
+
walkOnce([context, current, next], (o) => { if (o.op === 'var')
|
|
8
|
+
used.add(o.name); });
|
|
9
|
+
let serial = 0;
|
|
10
|
+
const fresh = (prefix) => { let name; do {
|
|
11
|
+
name = `${prefix}$${serial++}`;
|
|
12
|
+
} while (used.has(name)); used.add(name); return name; };
|
|
13
|
+
const key = (r) => JSON.stringify(r.slice(1));
|
|
14
|
+
const byKey = new Map(current.reads.map(r => [key(r), r[0]])), renamed = new Map();
|
|
15
|
+
let changed = false;
|
|
16
|
+
for (const read of next.reads) {
|
|
17
|
+
let name = byKey.get(key(read));
|
|
18
|
+
if (name === undefined) {
|
|
19
|
+
name = fresh('countRead');
|
|
20
|
+
current.reads.push([name, read[1], read[2]]);
|
|
21
|
+
byKey.set(key(read), name);
|
|
22
|
+
changed = true;
|
|
23
|
+
}
|
|
24
|
+
renamed.set(read[0], name);
|
|
25
|
+
}
|
|
26
|
+
const signature = (i) => JSON.stringify([i.unit, i.below, [...i.terms].sort((a, b) => a[1].localeCompare(b[1]) || a[0] - b[0])]);
|
|
27
|
+
const have = new Set(current.insts.map(signature));
|
|
28
|
+
for (const instance of next.insts) {
|
|
29
|
+
const terms = instance.terms.map(([k, id]) => [k, renamed.get(id) ?? id]), candidate = { ...instance, terms }, sig = signature(candidate);
|
|
30
|
+
if (have.has(sig))
|
|
31
|
+
continue;
|
|
32
|
+
current.insts.push({ ...candidate, wit: fresh('countWitness') });
|
|
33
|
+
have.add(sig);
|
|
34
|
+
changed = true;
|
|
35
|
+
}
|
|
36
|
+
return changed;
|
|
37
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type CountCert, type Goal } from '../kernel/kernel.js';
|
|
2
|
+
export declare function transportCountCertificate(from: Goal, to: Goal, cert: CountCert, until: number): CountCert | undefined;
|
|
3
|
+
export declare function countedCertificateHyps(goal: Goal, cert: CountCert): Set<number> | undefined;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Untrusted integration helpers for complete counted certificates. The kernel
|
|
2
|
+
// still checks the emitted ordinary tree and independent conditional cuts.
|
|
3
|
+
import { canon, lemmaPath } from "../kernel/kernel.js";
|
|
4
|
+
import { usedHyps, widen } from "../kernel/prove.js";
|
|
5
|
+
import { collectConditionalCertificate, markConditionalProof, transportConditionalProof } from "./count-recovery/conditional-proof.js";
|
|
6
|
+
export function transportCountCertificate(from, to, cert, until) {
|
|
7
|
+
if (performance.now() >= until)
|
|
8
|
+
return undefined;
|
|
9
|
+
if (!cert.lemmas?.length) {
|
|
10
|
+
const tree = widen(from, to, cert.tree);
|
|
11
|
+
return tree && performance.now() < until ? { ...cert, tree } : undefined;
|
|
12
|
+
}
|
|
13
|
+
const marked = markConditionalProof(from, cert.tree, cert.lemmas), tree = transportConditionalProof(from, to, marked, new Map(), new Map(), until);
|
|
14
|
+
const combined = tree && collectConditionalCertificate(to, tree, until);
|
|
15
|
+
return combined ? { ...cert, ...combined } : undefined;
|
|
16
|
+
}
|
|
17
|
+
export function countedCertificateHyps(goal, cert) {
|
|
18
|
+
if (!cert.lemmas?.length)
|
|
19
|
+
return usedHyps(goal, cert.tree);
|
|
20
|
+
try {
|
|
21
|
+
const found = new Set(), seen = new WeakSet();
|
|
22
|
+
const visit = (tree, path) => {
|
|
23
|
+
if (seen.has(tree))
|
|
24
|
+
return false;
|
|
25
|
+
seen.add(tree);
|
|
26
|
+
if ('split' in tree) {
|
|
27
|
+
const id = canon(tree.split), yes = new Map(path).set(id, { lin: tree.split, holds: true }), no = new Map(path).set(id, { lin: tree.split, holds: false });
|
|
28
|
+
const valid = visit(tree.yes, yes) && visit(tree.no, no);
|
|
29
|
+
seen.delete(tree);
|
|
30
|
+
return valid;
|
|
31
|
+
}
|
|
32
|
+
for (const i of usedHyps(goal, tree, lemmaPath(cert.lemmas, path)))
|
|
33
|
+
found.add(i);
|
|
34
|
+
seen.delete(tree);
|
|
35
|
+
return true;
|
|
36
|
+
};
|
|
37
|
+
if (!visit(cert.tree, new Map()))
|
|
38
|
+
return undefined;
|
|
39
|
+
// Every listed cut is checked, even when the main tree does not cite it.
|
|
40
|
+
// Its explicit source slice must survive dependency pruning as a whole.
|
|
41
|
+
for (const lemma of cert.lemmas) {
|
|
42
|
+
if (lemma.slice === undefined) {
|
|
43
|
+
for (let i = 0; i < goal.hyps.length; i++)
|
|
44
|
+
found.add(i);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
for (const i of lemma.slice) {
|
|
48
|
+
if (!Number.isSafeInteger(i) || i < 0 || i >= goal.hyps.length)
|
|
49
|
+
return undefined;
|
|
50
|
+
found.add(i);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return found;
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type E, type Law } from './machine.js';
|
|
2
|
+
import { type Instances } from '../kernel/kernel.js';
|
|
3
|
+
export declare function completeCountLawInstances(laws: Law[], at: Instances, terms: E[], readsCount: (law: Law) => boolean, more?: () => boolean): {
|
|
4
|
+
at: Instances;
|
|
5
|
+
added: {
|
|
6
|
+
law: string;
|
|
7
|
+
tuple: E[];
|
|
8
|
+
}[];
|
|
9
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function completeCountLawInstances(laws, at, terms, readsCount, more = () => true) {
|
|
2
|
+
const keys = [...new Map(terms.map(t => [JSON.stringify(t), t])).values()].slice(0, 4), added = [];
|
|
3
|
+
let next = at;
|
|
4
|
+
for (const law of laws) {
|
|
5
|
+
if (!law.forall || !readsCount(law))
|
|
6
|
+
continue;
|
|
7
|
+
const arity = 1 + (law.keys?.length ?? 0), old = at[law.name] ?? [];
|
|
8
|
+
if (arity > 2 || old.length % arity)
|
|
9
|
+
continue;
|
|
10
|
+
const seen = new Set(Array.from({ length: old.length / arity }, (_, i) => JSON.stringify(old.slice(i * arity, (i + 1) * arity)))), fresh = [];
|
|
11
|
+
const tuples = arity === 1 ? keys.map(t => [t]) : keys.flatMap(a => keys.map(b => [a, b]));
|
|
12
|
+
for (const tuple of tuples) {
|
|
13
|
+
if (added.length >= 48 || !more())
|
|
14
|
+
return { at: next, added };
|
|
15
|
+
const id = JSON.stringify(tuple);
|
|
16
|
+
if (seen.has(id))
|
|
17
|
+
continue;
|
|
18
|
+
seen.add(id);
|
|
19
|
+
fresh.push(...tuple);
|
|
20
|
+
added.push({ law: law.name, tuple });
|
|
21
|
+
if (next === at)
|
|
22
|
+
next = { ...at };
|
|
23
|
+
next[law.name] = [...old, ...fresh];
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return { at: next, added };
|
|
27
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Law, type Machine, type State } from './machine.js';
|
|
2
|
+
type Samples = {
|
|
3
|
+
p: Record<string, number>;
|
|
4
|
+
sts: State[];
|
|
5
|
+
}[];
|
|
6
|
+
export declare function liftedOneKeyCounts(source: Machine, m: Machine): string[];
|
|
7
|
+
export declare function countCubes(source: Machine, m: Machine, samples: Samples, only?: Set<string>): Law[];
|
|
8
|
+
export {};
|