const DECL_NOISE_EXACT: &[&str] = &[
"Nat",
"Int",
"Eq",
"Iff",
"Exists",
"And",
"Or",
"Not",
"True",
"False",
"Bool",
"Prop",
"Finset",
"List",
"Set",
"Multiset",
"Subtype",
"Sigma",
"Prod",
"Sum",
"Option",
"Quot",
"Finset.card",
"Finset.filter",
"Finset.sum",
"Finset.image",
"Finset.instSetLike",
"LE.le",
"LT.lt",
"GE.ge",
"GT.gt",
"Membership.mem",
"SetLike.coe",
"SetLike.instMembership",
"HSMul.hSMul",
"HMul.hMul",
"HAdd.hAdd",
"HSub.hSub",
"HDiv.hDiv",
"HPow.hPow",
"Mul.mul",
"Add.add",
"OfNat.ofNat",
"Zero.zero",
"One.one",
"Function.comp",
"LinearOrder",
"PartialOrder",
"Preorder",
"Lattice",
"SemilatticeInf",
"SemilatticeSup",
"DistribLattice",
"CompleteLattice",
"Order",
"LinearOrder.toLattice",
"AddMonoid",
"Monoid",
"AddCommMonoid",
"CommMonoid",
"AddGroup",
"Group",
"AddCommGroup",
"CommGroup",
"Ring",
"CommRing",
"Field",
"Semiring",
"CommSemiring",
"Module",
"Algebra",
"Mul",
"Add",
"Zero",
"One",
"Neg",
"Inv",
"Sub",
"Div",
"Pow",
"SMul",
"Dvd",
"Fintype",
"DecidablePred",
"Nonempty",
"Finite",
"Countable",
"Encodable",
];
fn is_decl_noise(name: &str) -> bool {
DECL_NOISE_EXACT.contains(&name)
|| name.starts_with("inst")
|| name.starts_with("Decidable")
|| name.contains(".to") }
pub(crate) fn load_decl_edges(path: &str) -> Result<Vec<(String, String)>, String> {
let raw = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
let mut set: std::collections::BTreeSet<(String, String)> = std::collections::BTreeSet::new();
if let Ok(v) = serde_json::from_str::<serde_json::Value>(&raw)
&& let Some(edges) = v.get("edges").and_then(|e| e.as_array())
{
for e in edges {
if let (Some(from), Some(to)) = (
e.get("from").and_then(|x| x.as_str()),
e.get("to").and_then(|x| x.as_str()),
) && from != to
&& !is_decl_noise(to)
{
set.insert((from.to_string(), to.to_string()));
}
}
return Ok(set.into_iter().collect());
}
for line in raw.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
let Ok(v) = serde_json::from_str::<serde_json::Value>(line) else {
continue;
};
let (Some(from), Some(to)) = (
v.get("from").and_then(|x| x.as_str()),
v.get("to").and_then(|x| x.as_str()),
) else {
continue;
};
if from != to && !is_decl_noise(to) {
set.insert((from.to_string(), to.to_string()));
}
}
Ok(set.into_iter().collect())
}
#[cfg(test)]
mod decl_graph_tests {
use super::{is_decl_noise, load_decl_edges};
#[test]
fn noise_filter_drops_plumbing_keeps_lemmas() {
for n in [
"Nat",
"DecidableEq",
"Finset",
"LinearOrder",
"Lattice",
"instDistribLatticeOfLinearOrder",
"SemilatticeInf.toPartialOrder",
] {
assert!(is_decl_noise(n), "{n} should be noise");
}
for n in [
"Finset.univ",
"Fintype.exists_card_fiber_lt_of_card_lt_mul",
"Finset.exists_card_fiber_lt_of_card_lt_mul",
] {
assert!(!is_decl_noise(n), "{n} should be kept");
}
}
#[test]
fn load_decl_edges_is_deterministic_and_deduped() {
let dir = std::env::temp_dir().join(format!("vela_declgraph_{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let f = dir.join("edges.jsonl");
std::fs::write(
&f,
"{\"from\":\"A\",\"to\":\"B\",\"kind\":\"uses\"}\n\
{\"from\":\"A\",\"to\":\"Nat\",\"kind\":\"uses\"}\n\
{\"from\":\"A\",\"to\":\"B\",\"kind\":\"uses\"}\n\
{\"from\":\"C\",\"to\":\"A\",\"kind\":\"uses\"}\n\
{\"from\":\"C\",\"to\":\"DecidableEq\",\"kind\":\"uses\"}\n",
)
.unwrap();
let p = f.to_str().unwrap();
let a = load_decl_edges(p).unwrap();
let b = load_decl_edges(p).unwrap();
assert_eq!(a, b, "same input -> same output");
assert_eq!(
a,
vec![
("A".to_string(), "B".to_string()),
("C".to_string(), "A".to_string())
]
);
std::fs::remove_dir_all(&dir).ok();
}
}