prove: P2 — 离线符号执行(F1–F5)+ 单调性护栏 + 语料库

- offline.rs:SymVal 符号执行(Const/Struct/OpResult/Param/Unknown);
  If 分支克隆状态并接续后续语句(修复 Return-in-outer 编排丢结果);
  可行性检查(前提∩路径条件为空 → 空真关闭,Lean 假设矛盾同构);
  F2 轨迹包含/长度、F3 构造器结构求值、F4 自由原子二分、
  F5 区间:字面重述关闭 + 区间违反反例(区间端点见证,如 金额 = 10001);
  区间蕴含保守 unverified(当前 Lean 脚本无 Val 传递引理,先于 Lean
  关闭即触发 divergence——诚实优先)
- 判定编排:离线先行(毫秒级 checked ≠ proved)→ Lean 复核升级 proved;
  分歧护栏:checked 但 Lean 关不掉 / 反例被 Lean 证出 ⇒
  error[proof.divergence] 退 1 永不落绿;MARQDO_PROOF_INJECT_DIVERGENCE 验证
- emit:try split 按 if 嵌套深度重复(三路分支可达);simp 清单补
  mqResultError/mqValueOr/mqAnd/mqOr/mqNot(分歧护栏暴露)
- tests/prove_corpus.rs:8 类 × 5 = 40 夹具语料库,
  offline_checked ⊆ lean_proved 强制 + 无 Lean 禁 proved;
  f5_interval_closure_scope 单测锁定 F5 诚实范围
- 设计文档 §12 记录 P2 落地、修复与文法限制

实测:biz 3 定理离线 checked→Lean proved;假定理反例 金额 = 10001
指回定理表行;注入分歧 → error/divergence 退 1;全量 516 通过
(仅剩 2 个已记录环境基线失败)。
This commit is contained in:
cflmy committed 2026-11-18 10:28:18 +08:00
1 parent 44d2928c06
commit 63d60e3ba1
47 files changed
+2699 -7

No files matched your search

+22 -3
View File
@@ -49,6 +49,21 @@ pub fn emit_module(plan: &Plan, module_stem: &str) -> (String, String) {
}
/// 定理脚手架(§7.2):受限文法可解析的命题 → `theorem` 陈述 + `sorry`
/// 函数体里 if 的最大嵌套深度(0 = 无分支)。
fn max_if_depth(steps: &[Step]) -> usize {
fn walk(steps: &[Step], depth: usize) -> usize {
let mut max = depth;
for s in steps {
if let Step::If { then, otherwise, .. } = s {
max = max.max(walk(then, depth + 1));
max = max.max(walk(otherwise, depth + 1));
}
}
max
}
walk(steps, 0)
}
fn emit_theorems(out: &mut String, fun: &FnPlan) {
if fun.claims.is_empty() {
return;
@@ -111,15 +126,19 @@ fn emit_theorems(out: &mut String, fun: &FnPlan) {
}
let _ = writeln!(out, " intro «结果» «轨迹» h_run");
let _ = writeln!(out, " unfold {fun_name} at h_run");
// 嵌套分支(else 里再嵌 if)需要逐层拆:按最大 if 深度发射
// `try split at h_run`(拆无可拆时 try 吞掉,幂等)。
for _ in 0..=max_if_depth(&fun.steps) {
let _ = writeln!(out, " try split at h_run");
}
// 每个分支:simp_all(完整默认集处理 List.map/contains/length 的
// 结构化消去;等式替换输出;Op.name 方程化简)+ 附加域定义
// (mqAccess/mqResultOk/mqEq 字段与构造)。互斥比较假设由库中
// @[simp] 矛盾引理消掉,剩余线性算术交给 omega。
let _ = writeln!(
out,
" try split at h_run
all_goals
simp_all [mqTruth, Op.name, mqAccess, mqResultOk, mqEq]
" all_goals
simp_all [mqTruth, Op.name, mqAccess, mqResultOk, mqResultError, mqValueOr, mqAnd, mqOr, mqNot, mqEq]
try
first
| exact (mqGt_mqLe_false ‹mqGt _ (Val.int _) = true› ‹mqLe _ (Val.int _) = true›).elim
+112 -3
View File
@@ -14,6 +14,7 @@ pub mod driver;
pub mod emit;
pub mod ir;
pub mod lower;
pub mod offline;
pub mod proof_check;
pub mod types;
@@ -386,10 +387,14 @@ pub fn prove_path(path: &Path, opts: &ProveOptions) -> Result<Report> {
status = Verdict::Error;
}
// ⑤ Lean 判定(P1):有定理、有产物、非 report-only 时进行。
// ⑤ 判定(P2):离线符号执行先行(毫秒级,checked ≠ proved),
// 有 Lean 工具链时随即复核(checked 升 proved;分歧 → error,永不落绿)。
let mut lean_info: Option<LeanInfo> = None;
let claims_present = plan.functions.iter().any(|f| !f.claims.is_empty());
if claims_present && opts.judge && !opts.report_only {
// ⑤a 离线(§5):F1–F5 片段内给 checked/反例,超出片段不猜。
judge_offline(&plan, &mut functions, &mut diagnostics);
// ⑤b Lean 复核(§8):证书缓存 → 现算。
match judge_with_lean(
&plan,
&source_sha256,
@@ -445,6 +450,77 @@ pub fn prove_path(path: &Path, opts: &ProveOptions) -> Result<Report> {
})
}
/// 离线判定(§5):checked/反例贴回定理,绝不冒充 proved。
/// 单调性护栏在此埋点:离线结论会在 judge_with_lean 里被 Lean 复核。
fn judge_offline(
plan: &crate::prove::ir::Plan,
functions: &mut [FunctionReport],
diagnostics: &mut Vec<Diagnostic>,
) {
use crate::diagnostics::Span;
let debug = std::env::var("MARQDO_PROOF_DEBUG").is_ok_and(|v| v == "1");
for f in plan.functions.iter() {
for claim in &f.claims {
let verdict = offline::check_claim(f, claim);
if debug {
eprintln!(
"[offline] {}.{} → {:?}",
f.name, claim.id, verdict
);
}
let Some(fr) = functions.iter_mut().find(|x| x.name == f.name) else {
continue;
};
let Some(cr) = fr.claims.iter_mut().find(|c| c.id == claim.id) else {
continue;
};
match verdict {
offline::OfflineVerdict::Checked { cases } => {
cr.status = Verdict::Checked;
cr.decided_by = "trace".into();
cr.evidence = Some(format!("离线符号执行:{cases} 条路径全部关闭(F1–F5)"));
cr.source_line = Some(claim.line);
fr.tier = Tier::Offline;
}
offline::OfflineVerdict::Unproved { counterexample } => {
cr.status = Verdict::Unproved;
cr.decided_by = "trace".into();
cr.source_line = Some(claim.line);
cr.evidence = counterexample.as_ref().map(|c| {
format!(
"反例(离线符号执行):{} — 路径 #{}",
c.detail, c.path_id
)
});
if let Some(c) = &counterexample {
diagnostics.push(
Diagnostic::new(
None,
Span::new(claim.line, 0),
format!(
"定理 {}.{} 反例:{}",
f.name, claim.id, c.detail
),
)
.with_code("proof.claim_failed"),
);
}
fr.tier = Tier::Offline;
}
offline::OfflineVerdict::Unverified { reason } => {
// 不猜:保留 unverified(不写 evidence,让位给 Lean 结果)。
let _ = reason;
}
}
fr.verdict = fr
.claims
.iter()
.map(|x| x.status)
.fold(Verdict::Proved, Verdict::worst);
}
}
}
/// 单一判定来源:缓存证书 → Lean 现算。返回(整体判定,lean 信息)。
#[allow(clippy::too_many_arguments)]
fn judge_with_lean(
@@ -533,8 +609,8 @@ fn judge_with_lean(
}
}
// ② 现算:lake env lean 等价的直接 lean 调用(LEAN_PATH 指向底座 olean)。
let outcome = driver::check_module(
// ② 现算:lean 直接调用(LEAN_PATH 指向底座 olean)。
let mut outcome = driver::check_module(
&toolchain,
&lib_dir,
lean_path,
@@ -542,6 +618,14 @@ fn judge_with_lean(
opts.timeout,
opts.max_heartbeats,
)?;
// 测试注入(roadmap P2 单调性护栏):让首条定理在 Lean 侧"关不掉",
// 验证离线 checked + Lean unproved ⇒ error[proof.divergence] 的护栏真的咬人。
if std::env::var("MARQDO_PROOF_INJECT_DIVERGENCE").is_ok_and(|v| v == "1") {
if let Some(first) = outcome.claims.iter_mut().find(|c| c.status == Verdict::Proved) {
first.status = Verdict::Unproved;
first.detail = "injected divergence (MARQDO_PROOF_INJECT_DIVERGENCE)".into();
}
}
// ③ 证据 → 诊断(proof.claim_failed,指回源码行)。
let claim_records: Vec<cache::ClaimRecord> = outcome
@@ -610,6 +694,31 @@ fn apply_outcome(
};
let f = &mut functions[pos];
if let Some(claim) = f.claims.iter_mut().find(|x| x.id == c.claim) {
// 单调性护栏(§5.4):离线结论必须被 Lean 复核印证。
// checked 但 Lean 关不掉,或离线反例被 Lean 证出来 ⇒ 判定器有 bug。
let divergence = match (claim.status, c.status) {
(Verdict::Checked, Verdict::Unproved)
| (Verdict::Unproved, Verdict::Proved) => true,
_ => false,
};
if divergence {
diagnostics.push(
Diagnostic::new(
None,
Span::new(claim.source_line.unwrap_or(0), 0),
format!(
"离线与 Lean 判定分歧:定理 {}.{} 离线 {:?},Lean {}——判定器缺陷,拒绝落绿",
f.name, c.claim, claim.status, c.status.as_str()
),
)
.with_code("proof.divergence"),
);
claim.status = Verdict::Error;
claim.decided_by = "lean".into();
f.verdict = Verdict::Error;
f.tier = Tier::Lean;
continue;
}
claim.status = c.status;
claim.decided_by = "lean".into();
claim.source_line = c.source_line;
+1581
View File
File diff suppressed because it is too large. Load diff