use std::fs;
use std::path::PathBuf;
fn workspace_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.and_then(|p| p.parent())
.expect("workspace root")
.to_path_buf()
}
#[test]
fn every_referenced_lean_theorem_exists_in_its_file() {
let root = workspace_root();
let contracts_dir = root.join("contracts");
let entries = fs::read_dir(&contracts_dir).expect("read contracts/");
let mut checked = 0;
for entry in entries.flatten() {
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) != Some("yaml") {
continue;
}
let contents = fs::read_to_string(&path).expect("read contract yaml");
let lines: Vec<&str> = contents.lines().collect();
for (i, line) in lines.iter().enumerate() {
let Some(thm) = extract_quoted_value(line, "lean_theorem:") else {
continue;
};
let file = lines
.iter()
.skip(i + 1)
.take(3)
.find_map(|l| extract_quoted_value(l, "lean_file:"))
.unwrap_or_else(|| {
panic!(
"{}: lean_theorem `{}` missing adjacent lean_file: \"...\"",
path.display(),
thm
)
});
let lean_path = root.join(&file);
assert!(
lean_path.is_file(),
"{}: lean_theorem `{}` references missing file `{}`",
path.display(),
thm,
file
);
let lean_src = fs::read_to_string(&lean_path).expect("read lean file");
let unqualified = thm.rsplit('.').next().unwrap_or(&thm);
let has_theorem = lean_src.lines().any(|l| {
l.trim_start()
.starts_with(&format!("theorem {unqualified}"))
});
assert!(
has_theorem,
"{}: lean_theorem `{}` not found in `{}` (looked for `theorem {}`)",
path.display(),
thm,
file,
unqualified
);
checked += 1;
}
}
assert!(
checked >= 1,
"expected at least one lean_theorem field across contracts/; \
XPILE-REFINE-001 was supposed to plant one in py-int-arith-v1.yaml"
);
}
fn extract_quoted_value(line: &str, key: &str) -> Option<String> {
let trimmed = line.trim_start();
let rest = trimmed.strip_prefix(key)?;
let rest = rest.trim();
let rest = rest.strip_prefix('"')?;
let end = rest.find('"')?;
Some(rest[..end].to_string())
}
#[test]
fn py_int_arith_lean_file_carries_required_landmarks() {
let root = workspace_root();
let lean = root.join("contracts/lean/PyIntArith.lean");
assert!(lean.is_file(), "{} must exist", lean.display());
let src = fs::read_to_string(&lean).expect("read PyIntArith.lean");
for needle in &[
"namespace XpileContracts.CPyIntArith",
"theorem fast_path_eq_slow_path",
"theorem mul_fast_path_eq_slow_path",
"theorem floor_div_fast_path_eq_slow_path",
"theorem mod_fast_path_eq_slow_path",
"theorem shl_fast_path_eq_slow_path",
"theorem shr_fast_path_eq_slow_path",
"theorem pow_fast_path_eq_slow_path",
"theorem add_slow_path_eq_python",
"Int.bmod_def",
] {
assert!(
src.contains(needle),
"PyIntArith.lean must contain `{needle}` — see refinement_proofs.rs for context"
);
}
let no_sorry_lines: Vec<&str> = src
.lines()
.filter(|l| {
let trimmed = l.trim_start();
!trimmed.starts_with("--") && !trimmed.starts_with("/-") && !trimmed.starts_with("|")
})
.filter(|l| l.contains("sorry"))
.collect();
assert!(
no_sorry_lines.is_empty(),
"PyIntArith.lean must not carry `sorry` in proof code (PMAT-028/029 discharged all four); \
found on lines: {no_sorry_lines:?}"
);
let trivial_lines: Vec<&str> = src
.lines()
.filter(|l| {
let trimmed = l.trim_start();
!trimmed.starts_with("--") && !trimmed.starts_with("/-")
})
.filter(|l| l.contains("by trivial") || l.contains(":= by trivial"))
.collect();
assert!(
trivial_lines.is_empty(),
"PyIntArith.lean must not carry `by trivial` placeholders (PMAT-029 discharged the stub trio); \
found on lines: {trivial_lines:?}"
);
}
#[test]
fn extract_quoted_value_handles_common_yaml_shapes() {
assert_eq!(
extract_quoted_value(" lean_theorem: \"Foo.Bar\"", "lean_theorem:"),
Some("Foo.Bar".to_string())
);
assert_eq!(
extract_quoted_value(
" lean_file: \"contracts/lean/X.lean\"",
"lean_file:"
),
Some("contracts/lean/X.lean".to_string())
);
assert_eq!(
extract_quoted_value(" lean_theorem: \"Foo\"", "lean_file:"),
None
);
assert_eq!(
extract_quoted_value(" lean_theorem: Foo", "lean_theorem:"),
None
);
}