Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.
Rust ai-nativedescription-logicsformal-verificationinfrastructure-as-codeisabelle-languageknowledge-graphleanlean4mcpmcp-serverontologyowlowl2reasoningrustshaclsparqltheorem-proving
fix(tableaux): make cardinality clashes hierarchy-aware
FABIOTESSa2cc0a199788
diff --git a/src/tableaux.rs b/src/tableaux.rsindex 2bd038b7..a72f07b1 100644--- a/src/tableaux.rs+++ b/src/tableaux.rs@@ -835,6 +835,10 @@ struct ProcessedTBox {super_to_sub: HashMap<u32, HashSet<u32>>,/// Inverse role mapping (bidirectional): R → R⁻ and R⁻ → R.inverse_roles: HashMap<u32, u32>,+ /// Transitive closure of asserted Atom ⊑ Atom subsumption, used to decide+ /// cardinality clashes whose fillers are related by subsumption rather than+ /// syntactically equal.+ class_closure: HashMap<u32, HashSet<u32>>,}impl ProcessedTBox {@@ -884,6 +888,34 @@ impl ProcessedTBox {}}+ // Transitive closure of asserted Atom ⊑ Atom subsumption. Needed because a+ // cardinality bound stated on a superclass constrains its subclasses too,+ // which a syntactic filler comparison cannot see.+ let mut direct: HashMap<u32, HashSet<u32>> = HashMap::new();+ for (sub, sup) in axioms {+ if let (Concept::Atom(a), Concept::Atom(b)) = (sub, sup)+ && a != b+ {+ direct.entry(*a).or_default().insert(*b);+ }+ }+ let mut class_closure: HashMap<u32, HashSet<u32>> = HashMap::new();+ for sub in direct.keys().copied().collect::<Vec<_>>() {+ let mut seen: HashSet<u32> = HashSet::new();+ let mut stack: Vec<u32> = direct+ .get(&sub)+ .map(|v| v.iter().copied().collect())+ .unwrap_or_default();+ while let Some(x) = stack.pop() {+ if seen.insert(x)+ && let Some(next) = direct.get(&x)+ {+ stack.extend(next.iter().copied());+ }+ }+ class_closure.insert(sub, seen);+ }+Self {concept_defs,gcis,@@ -891,6 +923,7 @@ impl ProcessedTBox {transitive_roles,super_to_sub: super_to_sub_map,inverse_roles,+ class_closure,}}}@@ -1559,7 +1592,54 @@ impl Tableau {}fn any_clash(&self) -> bool {- self.nodes.values().any(|n| !n.blocked && n.has_clash())+ self.nodes+ .values()+ .any(|n| !n.blocked && (n.has_clash() || self.card_clash_via_hierarchy(n)))+ }++ /// ≥n1 R.C against ≤n2 R.D where n1 > n2 and C ⊑ D.+ ///+ /// `TNode::has_clash` only fires when the two fillers are syntactically equal+ /// or the max's filler is ⊤. That misses the common shape where a class+ /// asserts ≥2 R.C while inheriting ≤1 R.D from an ancestor with C ⊑ D: every+ /// C-successor is also a D-successor, so the bound is violated and the class+ /// is unsatisfiable.+ ///+ /// Measured on the GCHQ-published hqdm.owl, whose 39 unsatisfiable classes+ /// are all of this shape. Ablating its cardinality axioms takes the count to+ /// 0, while ablating rdfs:domain, rdfs:range or owl:disjointWith each leave+ /// all 39 standing.+ fn card_clash_via_hierarchy(&self, n: &TNode) -> bool {+ let subsumed = |a: &Concept, b: &Concept| -> bool {+ match (a, b) {+ (Concept::Atom(x), Concept::Atom(y)) => {+ x == y+ || self+ .tbox+ .class_closure+ .get(x)+ .is_some_and(|anc| anc.contains(y))+ }+ _ => false,+ }+ };+ for label in &n.labels {+ let (r1, n1, f1) = match label {+ Concept::MinCard(r, k, f) => (*r, *k, f.as_ref()),+ Concept::Exists(r, f) => (*r, 1u32, f.as_ref()),+ _ => continue,+ };+ for other in &n.labels {+ if let Concept::MaxCard(r2, n2, f2) = other+ && r1 == *r2+ && n1 > *n2+ && subsumed(f1, f2)+ {+ return true;+ }+ }+ }+ false}/// Subset blocking: node blocked by ancestor with ⊇ labels.diff --git a/tests/tableaux_test.rs b/tests/tableaux_test.rsindex f6ce3c7c..80813d60 100644--- a/tests/tableaux_test.rs+++ b/tests/tableaux_test.rs@@ -1097,3 +1097,53 @@ fn test_dl_datatype_range_is_not_a_concept() {);assert!(unsat.is_empty(), "datatype range must not break reasoning, got {unsat:?}");}++#[test]+fn test_dl_cardinality_clash_through_subsumption() {+ // >=2 p.C against an INHERITED <=1 p.D with C [= D. Every C-successor is a+ // D-successor, so the bound is violated. Syntactic filler comparison misses+ // this because C and D are different concepts.+ let unsat = unsat_names(+ r#"+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .+ @prefix ex: <http://example.org/> .+ ex:D a owl:Class . ex:C a owl:Class ; rdfs:subClassOf ex:D .+ ex:p a owl:ObjectProperty .+ ex:Sup a owl:Class ; rdfs:subClassOf+ [ a owl:Restriction ; owl:onProperty ex:p ;+ owl:maxQualifiedCardinality "1"^^xsd:nonNegativeInteger ; owl:onClass ex:D ] .+ ex:A a owl:Class ; rdfs:subClassOf ex:Sup ,+ [ a owl:Restriction ; owl:onProperty ex:p ;+ owl:minQualifiedCardinality "2"^^xsd:nonNegativeInteger ; owl:onClass ex:C ] .+ "#,+ );+ assert!(+ unsat.iter().any(|u| u.ends_with("/A>")),+ "cardinality bound on a superclass must constrain subclasses, got {unsat:?}"+ );+}++#[test]+fn test_dl_cardinality_subsumption_no_false_positive() {+ // Same shape with the subsumption REVERSED (D [= C, bound on the subclass)+ // is satisfiable: a >=2 p.C requirement is not constrained by <=1 p.D when+ // D is narrower than C.+ let unsat = unsat_names(+ r#"+ @prefix owl: <http://www.w3.org/2002/07/owl#> .+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .+ @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .+ @prefix ex: <http://example.org/> .+ ex:C a owl:Class . ex:D a owl:Class ; rdfs:subClassOf ex:C .+ ex:p a owl:ObjectProperty .+ ex:A a owl:Class ; rdfs:subClassOf+ [ a owl:Restriction ; owl:onProperty ex:p ;+ owl:maxQualifiedCardinality "1"^^xsd:nonNegativeInteger ; owl:onClass ex:D ] ,+ [ a owl:Restriction ; owl:onProperty ex:p ;+ owl:minQualifiedCardinality "2"^^xsd:nonNegativeInteger ; owl:onClass ex:C ] .+ "#,+ );+ assert!(unsat.is_empty(), "must not fire on the reversed subsumption, got {unsat:?}");+}