prove: fix 4 review defects (honesty + compilability) + business validation

Code review after the initial prove commit found and fixed:

1. Calls inside table/list literals were silently dropped — a function
   with side-effectful cells lowered to "no ops" and claimed purity in
   Lean. lower_expr's fallback now rejects with proof.unsupported
   (author must bind the call to a variable first).
2. Same-callee call sites with different arities (optional params)
   shared one Op whose ctor declared the first-seen arity, emitting
   uncompilable Lean. register_op now suffixes `/N` per arity.
3. proof.name_shadow fired once per walked function (N functions → N
   duplicates) and checked params of claim-free functions. Predicate
   check runs once per module; param check only for functions that
   carry claims.
4. 长度(轨迹) = 2 emitted mqEq (Nat) (Val) — type-mismatched Lean.
   Nat-context detection renders trace-length comparisons natively;
   untranslatable shapes become comments instead of broken Lean.

Hardening: lean_ident reserves 轨迹₀ / h_前提 (theorem binder
collision); emit_theorems dedups repeated claim ids.

Tests: +4 integration regressions (p2_a..p2_d), 23 prove tests total;
full suite green except the documented env-limited failures (one
host::foreign_session flake passes standalone).

Business validation (doc §12): e-commerce order-approval SKILL with 3
theorems cross-checked against the real interpreter — 5000 → auto
(1-step trace), 20000/60000 → manual ticket T-20000/T-60000
(需人工→创建, exactly 2 steps); corrupted claim tables hit
check --proof codes while staying default-off; double emit
byte-identical.
This commit is contained in:
cflmy committed 2026-11-18 10:28:18 +08:00
1 parent a11b347659
commit 528a1f0dbc
5 files changed
+337 -113

No files matched your search

+43 -73
View File
@@ -21,41 +21,44 @@ pub const RESERVED_CLAIM_NAMES: &[&str] = &["结果", "轨迹"];
/// 谓词封闭集合(§7.3.5:改集合需重新评审 ADR)。
pub const PREDICATES: &[(&str, usize)] = &[("包含", 2), ("长度", 1), ("下标", 2)];
/// 入口:对整个模块做定理表校验。
/// 入口:对整个模块做定理表校验(只看主文件;与契约检查同一范围,
/// import 库文件不重复校验——诊断的行号锚在主文件上才有意义)。
pub fn check_proofs(module: &Module, path: Option<&Path>) -> Vec<Diagnostic> {
let mut out = Vec::new();
let mut function_names: Vec<String> = Vec::new();
collect_function_names(module, &mut function_names);
let has_claims = module_has_claims(module);
walk_checks(
module,
&module.functions,
path,
&function_names,
has_claims,
&mut out,
);
for (_, child) in module.import_modules.iter() {
out.extend(check_proofs_imported(child, path));
// 谓词名遮蔽是模块级事实(与单个函数无关),整个模块只报一次;
// 诊断锚在被遮蔽的函数上(任意深度)。
for pred in PREDICATES {
if function_names.iter().any(|n| n == pred.0) {
let Some(fun) = find_by_name(&module.functions, pred.0) else {
continue;
};
out.push(proof_diag(
module,
path,
fun.span,
"proof.name_shadow",
format!(
"函数名 `{}` 与定理谓词 `{}` 同名(谓词集合封闭:包含/长度/下标)",
fun.name, pred.0
),
format!("把函数 `{}` 改名,避免定理表里的 `{}` 歧义", fun.name, pred.0),
));
}
}
walk_checks(module, &module.functions, path, &mut out);
out
}
fn check_proofs_imported(module: &Module, path: Option<&Path>) -> Vec<Diagnostic> {
// import 的库文件自带定理表也按同规则校验(诊断指向本文件行号即可)。
let mut out = Vec::new();
let mut function_names: Vec<String> = Vec::new();
collect_function_names(module, &mut function_names);
let has_claims = module_has_claims(module);
walk_checks(
module,
&module.functions,
path,
&function_names,
has_claims,
&mut out,
);
out
fn walk_checks(module: &Module, funs: &[Function], path: Option<&Path>, out: &mut Vec<Diagnostic>) {
for fun in funs {
check_claim_duplicates(module, fun, path, out);
check_name_shadow(module, fun, path, out);
check_misplaced_and_malformed(module, fun, path, out);
check_claim_symbols(module, fun, path, out);
walk_checks(module, &fun.children, path, out);
}
}
fn collect_function_names(module: &Module, out: &mut Vec<String>) {
@@ -71,33 +74,17 @@ fn collect_function_names(module: &Module, out: &mut Vec<String>) {
}
}
fn module_has_claims(module: &Module) -> bool {
fn go(funs: &[Function]) -> bool {
funs.iter()
.any(|f| !f.claims.is_empty() || go(&f.children))
}
go(&module.functions)
|| module
.import_modules
.values()
.any(|m| module_has_claims(m))
}
fn walk_checks(
module: &Module,
funs: &[Function],
path: Option<&Path>,
function_names: &[String],
has_claims: bool,
out: &mut Vec<Diagnostic>,
) {
for fun in funs {
check_claim_duplicates(module, fun, path, out);
check_name_shadow(module, fun, function_names, has_claims, path, out);
check_misplaced_and_malformed(module, fun, path, out);
check_claim_symbols(module, fun, path, out);
walk_checks(module, &fun.children, path, function_names, has_claims, out);
/// 在函数树里按显示名找函数(诊断锚点用)。
fn find_by_name<'a>(funs: &'a [Function], name: &str) -> Option<&'a Function> {
for f in funs {
if f.name == name {
return Some(f);
}
if let Some(inner) = find_by_name(&f.children, name) {
return Some(inner);
}
}
None
}
fn proof_diag(
@@ -134,17 +121,15 @@ fn check_claim_duplicates(module: &Module, fun: &Function, path: Option<&Path>,
}
}
/// 遮蔽检查(§7.3.4):保留名 `结果`/`轨迹` 不得被形参占用;
/// 谓词名 `包含/长度/下标` 不得与用户函数同名(有定理表时才有意义)。
/// 遮蔽检查(§7.3.4):只看**自带定理表的函数**——形参 `结果`/`轨迹` 会
/// 与该函数定理作用域的输出绑定名冲突;没有定理表的函数不受影响。
fn check_name_shadow(
module: &Module,
fun: &Function,
function_names: &[String],
has_claims: bool,
path: Option<&Path>,
out: &mut Vec<Diagnostic>,
) {
if fun.claims.is_empty() && !has_claims {
if fun.claims.is_empty() {
return;
}
for param in &fun.params {
@@ -163,21 +148,6 @@ fn check_name_shadow(
));
}
}
for pred in PREDICATES {
if function_names.iter().any(|n| n == pred.0) {
out.push(proof_diag(
module,
path,
fun.span,
"proof.name_shadow",
format!(
"函数名 `{}` 与定理谓词 `{}` 同名(谓词集合封闭:包含/长度/下标)",
fun.name, pred.0
),
format!("把函数 `{}` 改名,避免定理表里的 `{}` 歧义", fun.name, pred.0),
));
}
}
}
/// 错位与畸形:body 里仍是定理形的表(默认关时解析器不提取它们)。