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
Stop naming a W3C rule that is not that rule, implement cls-avf, and make the two reasoning paths agree
FABIOTESS7324ccc100ee
diff --git a/lean/OOCert/Rules.lean b/lean/OOCert/Rules.leanindex bce2ad3c..8f732517 100644--- a/lean/OOCert/Rules.lean+++ b/lean/OOCert/Rules.lean@@ -31,11 +31,10 @@ wrong order is rejected, which is a false alarm and never a false pass.| prp-inv1 | `p owl:inverseOf q`, `x p y` | `y q x` || prp-inv2 | `p owl:inverseOf q`, `x q y` | `y p x` || eq-sym | `a owl:sameAs b` | `b owl:sameAs a` |-| scm-eqc1 | `a owl:equivalentClass b` | `a sc b` |-| scm-eqc2 | `a owl:equivalentClass b` | `b sc a` |-| scm-eqp1 | `a owl:equivalentProperty b` | `a sp b` |-| scm-eqp2 | `a owl:equivalentProperty b` | `b sp a` |+| scm-eqc1 | `a owl:equivalentClass b` | `a sc b` or `b sc a` |+| scm-eqp1 | `a owl:equivalentProperty b` | `a sp b` or `b sp a` || cls-svf1 | `r owl:onProperty p`, `r owl:someValuesFrom c`, `x p y`, `y rdf:type c` | `x rdf:type r` |+| cls-avf | `r owl:onProperty p`, `r owl:allValuesFrom c`, `x rdf:type r`, `x p y` | `y rdf:type c` || cls-hv1 | `r owl:onProperty p`, `r owl:hasValue v`, `x rdf:type r` | `x p v` || cls-hv2 | `r owl:onProperty p`, `r owl:hasValue v`, `x p v` | `x rdf:type r` || cls-int1 | `c owl:intersectionOf l`, the list chain of `l`, `x rdf:type m` for every member `m` | `x rdf:type c` |@@ -45,14 +44,20 @@ The list chain is the sequence `l rdf:first m₁`, `l rdf:rest l₂`,`l₂ rdf:first m₂`, `l₂ rdf:rest l₃`, … down to a node whose `rdf:rest` is`rdf:nil`. The chain and the constructor triple must be asserted, not derived,because `Model` reads lists off the asserted graph.++`scm-eqc1` and `scm-eqp1` each license TWO conclusions from one premise, so a+certificate carries two steps under one rule id and the checker accepts either+conclusion. They used to emit the second under the ids `scm-eqc2` and `scm-eqp2`,+which name different W3C rules concluding in the opposite direction. Those names+are now free for the rules that own them.-/namespace OOCertinductive Rule| rdfs2 | rdfs3 | rdfs5 | rdfs7 | rdfs9 | rdfs11| prpTrp | prpSymp | prpInv1 | prpInv2 | eqSym- | scmEqc1 | scmEqc2 | scmEqp1 | scmEqp2- | clsSvf1 | clsHv1 | clsHv2 | clsInt1 | clsUni+ | scmEqc1 | scmEqp1+ | clsSvf1 | clsAvf | clsHv1 | clsHv2 | clsInt1 | clsUnideriving DecidableEq, Reprdef Rule.name : Rule → String@@ -60,16 +65,16 @@ def Rule.name : Rule → String| .rdfs9 => "rdfs9" | .rdfs11 => "rdfs11"| .prpTrp => "prp-trp" | .prpSymp => "prp-symp" | .prpInv1 => "prp-inv1"| .prpInv2 => "prp-inv2" | .eqSym => "eq-sym"- | .scmEqc1 => "scm-eqc1" | .scmEqc2 => "scm-eqc2" | .scmEqp1 => "scm-eqp1"- | .scmEqp2 => "scm-eqp2"- | .clsSvf1 => "cls-svf1" | .clsHv1 => "cls-hv1" | .clsHv2 => "cls-hv2"+ | .scmEqc1 => "scm-eqc1" | .scmEqp1 => "scm-eqp1"+ | .clsSvf1 => "cls-svf1" | .clsAvf => "cls-avf"+ | .clsHv1 => "cls-hv1" | .clsHv2 => "cls-hv2"| .clsInt1 => "cls-int1" | .clsUni => "cls-uni"def Rule.all : List Rule :=[.rdfs2, .rdfs3, .rdfs5, .rdfs7, .rdfs9, .rdfs11,.prpTrp, .prpSymp, .prpInv1, .prpInv2, .eqSym,- .scmEqc1, .scmEqc2, .scmEqp1, .scmEqp2,- .clsSvf1, .clsHv1, .clsHv2, .clsInt1, .clsUni]+ .scmEqc1, .scmEqp1,+ .clsSvf1, .clsAvf, .clsHv1, .clsHv2, .clsInt1, .clsUni]def Rule.ofName? (s : String) : Option Rule :=Rule.all.find? (fun r => r.name == s)@@ -141,18 +146,21 @@ def checkStep (inG derived : Triple → Bool) (st : Step) : Bool :=| .eqSym, [⟨a, sa, b⟩] =>sa = V.sameAs ∧ k ⟨a, sa, b⟩ ∧ st.conclusion = ⟨b, V.sameAs, a⟩| .scmEqc1, [⟨a, e, b⟩] =>- e = V.equivalentClass ∧ k ⟨a, e, b⟩ ∧ st.conclusion = ⟨a, V.subClassOf, b⟩- | .scmEqc2, [⟨a, e, b⟩] =>- e = V.equivalentClass ∧ k ⟨a, e, b⟩ ∧ st.conclusion = ⟨b, V.subClassOf, a⟩+ e = V.equivalentClass ∧ k ⟨a, e, b⟩ ∧+ (st.conclusion = ⟨a, V.subClassOf, b⟩ ∨ st.conclusion = ⟨b, V.subClassOf, a⟩)| .scmEqp1, [⟨a, e, b⟩] =>- e = V.equivalentProperty ∧ k ⟨a, e, b⟩ ∧ st.conclusion = ⟨a, V.subPropertyOf, b⟩- | .scmEqp2, [⟨a, e, b⟩] =>- e = V.equivalentProperty ∧ k ⟨a, e, b⟩ ∧ st.conclusion = ⟨b, V.subPropertyOf, a⟩+ e = V.equivalentProperty ∧ k ⟨a, e, b⟩ ∧+ (st.conclusion = ⟨a, V.subPropertyOf, b⟩ ∨ st.conclusion = ⟨b, V.subPropertyOf, a⟩)| .clsSvf1, [⟨r, op, p⟩, ⟨r', sv, c⟩, ⟨x, p1, y⟩, ⟨y', t, c'⟩] =>op = V.onProperty ∧ sv = V.someValuesFrom ∧ r' = r ∧ p1 = p ∧ y' = y ∧t = V.type ∧ c' = c ∧k ⟨r, op, p⟩ ∧ k ⟨r', sv, c⟩ ∧ k ⟨x, p1, y⟩ ∧ k ⟨y', t, c'⟩ ∧st.conclusion = ⟨x, V.type, r⟩+ | .clsAvf, [⟨r, op, p⟩, ⟨r', av, c⟩, ⟨x, t, r''⟩, ⟨x', p1, y⟩] =>+ op = V.onProperty ∧ av = V.allValuesFrom ∧ r' = r ∧ t = V.type ∧ r'' = r ∧+ x' = x ∧ p1 = p ∧+ k ⟨r, op, p⟩ ∧ k ⟨r', av, c⟩ ∧ k ⟨x, t, r''⟩ ∧ k ⟨x', p1, y⟩ ∧+ st.conclusion = ⟨y, V.type, c⟩| .clsHv1, [⟨r, op, p⟩, ⟨r', hv, v⟩, ⟨x, t, r''⟩] =>op = V.onProperty ∧ hv = V.hasValue ∧ r' = r ∧ t = V.type ∧ r'' = r ∧k ⟨r, op, p⟩ ∧ k ⟨r', hv, v⟩ ∧ k ⟨x, t, r''⟩ ∧diff --git a/lean/OOCert/Semantics.lean b/lean/OOCert/Semantics.leanindex 55178e71..b47264fa 100644--- a/lean/OOCert/Semantics.lean+++ b/lean/OOCert/Semantics.lean@@ -99,6 +99,10 @@ structure Conditions (I : Interp) : Prop wheredirection is assumed; the converse is not needed by any rule. -/svf : ∀ r p c, I.iext (I.ι V.onProperty) r p → I.iext (I.ι V.someValuesFrom) r c →∀ x y, I.iext p x y → I.cext c y → I.cext r x+ /-- cls-avf. Everything an `x` in the restriction reaches by `p` is in the+ filler. The mirror of `svf`, and like it only the direction the rule uses. -/+ avf : ∀ r p c, I.iext (I.ι V.onProperty) r p → I.iext (I.ι V.allValuesFrom) r c →+ ∀ x y, I.cext r x → I.iext p x y → I.cext c y/-- cls-hv1 needs the forward direction, cls-hv2 the backward one. -/hv : ∀ r p v, I.iext (I.ι V.onProperty) r p → I.iext (I.ι V.hasValue) r v →∀ x, (I.cext r x ↔ I.iext p x v)diff --git a/lean/OOCert/Soundness.lean b/lean/OOCert/Soundness.leanindex eae20a33..e361a1d8 100644--- a/lean/OOCert/Soundness.lean+++ b/lean/OOCert/Soundness.lean@@ -184,27 +184,19 @@ theorem checkStep_sound {inG derived : Triple → Bool} {st : Step}case scmEqc1 =>rcases premises with _ | ⟨⟨a, e, b⟩, _ | _⟩ <;>simp only [decide_eq_true_eq, Bool.false_eq_true] at h- obtain ⟨rfl, h1, rfl⟩ := h+ obtain ⟨rfl, h1, hc⟩ := hintro I M- exact (M.conds.eqc _ _ (hk _ h1 I M)).1- case scmEqc2 =>- rcases premises with _ | ⟨⟨a, e, b⟩, _ | _⟩ <;>- simp only [decide_eq_true_eq, Bool.false_eq_true] at h- obtain ⟨rfl, h1, rfl⟩ := h- intro I M- exact (M.conds.eqc _ _ (hk _ h1 I M)).2+ rcases hc with rfl | rfl+ · exact (M.conds.eqc _ _ (hk _ h1 I M)).1+ · exact (M.conds.eqc _ _ (hk _ h1 I M)).2case scmEqp1 =>rcases premises with _ | ⟨⟨a, e, b⟩, _ | _⟩ <;>simp only [decide_eq_true_eq, Bool.false_eq_true] at h- obtain ⟨rfl, h1, rfl⟩ := h- intro I M- exact (M.conds.eqp _ _ (hk _ h1 I M)).1- case scmEqp2 =>- rcases premises with _ | ⟨⟨a, e, b⟩, _ | _⟩ <;>- simp only [decide_eq_true_eq, Bool.false_eq_true] at h- obtain ⟨rfl, h1, rfl⟩ := h+ obtain ⟨rfl, h1, hc⟩ := hintro I M- exact (M.conds.eqp _ _ (hk _ h1 I M)).2+ rcases hc with rfl | rfl+ · exact (M.conds.eqp _ _ (hk _ h1 I M)).1+ · exact (M.conds.eqp _ _ (hk _ h1 I M)).2case clsSvf1 =>rcases premises with_ | ⟨⟨r, op, p⟩, _ | ⟨⟨r', sv, c⟩, _ | ⟨⟨x, p1, y⟩, _ | ⟨⟨y', t, c'⟩, _ | _⟩⟩⟩⟩ <;>@@ -212,6 +204,13 @@ theorem checkStep_sound {inG derived : Triple → Bool} {st : Step}obtain ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, h1, h2, h3, h4, rfl⟩ := hintro I Mexact M.conds.svf _ _ _ (hk _ h1 I M) (hk _ h2 I M) _ _ (hk _ h3 I M) (hk _ h4 I M)+ case clsAvf =>+ rcases premises with+ _ | ⟨⟨r, op, p⟩, _ | ⟨⟨r', av, c⟩, _ | ⟨⟨x, t, r''⟩, _ | ⟨⟨x', p1, y⟩, _ | _⟩⟩⟩⟩ <;>+ simp only [decide_eq_true_eq, Bool.false_eq_true] at h+ obtain ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, h1, h2, h3, h4, rfl⟩ := h+ intro I M+ exact M.conds.avf _ _ _ (hk _ h1 I M) (hk _ h2 I M) _ _ (hk _ h3 I M) (hk _ h4 I M)case clsHv1 =>rcases premises with_ | ⟨⟨r, op, p⟩, _ | ⟨⟨r', hv, v⟩, _ | ⟨⟨x, t, r''⟩, _ | _⟩⟩⟩ <;>diff --git a/lean/OOCert/Triple.lean b/lean/OOCert/Triple.leanindex 4f1ba376..7bcb15a6 100644--- a/lean/OOCert/Triple.lean+++ b/lean/OOCert/Triple.lean@@ -40,6 +40,7 @@ def equivalentClass : Term := "<http://www.w3.org/2002/07/owl#equivalentClass>"def equivalentProperty : Term := "<http://www.w3.org/2002/07/owl#equivalentProperty>"def onProperty : Term := "<http://www.w3.org/2002/07/owl#onProperty>"def someValuesFrom : Term := "<http://www.w3.org/2002/07/owl#someValuesFrom>"+def allValuesFrom : Term := "<http://www.w3.org/2002/07/owl#allValuesFrom>"def hasValue : Term := "<http://www.w3.org/2002/07/owl#hasValue>"def intersectionOf : Term := "<http://www.w3.org/2002/07/owl#intersectionOf>"def unionOf : Term := "<http://www.w3.org/2002/07/owl#unionOf>"diff --git a/lean/OOCert/Witness.lean b/lean/OOCert/Witness.leanindex 1521c237..0825e752 100644--- a/lean/OOCert/Witness.lean+++ b/lean/OOCert/Witness.lean@@ -55,6 +55,7 @@ theorem saturated_is_a_model (G : List Triple) : Model saturated G whereeqc := fun _ _ _ => ⟨trivial, trivial⟩eqp := fun _ _ _ => ⟨trivial, trivial⟩svf := fun _ _ _ _ _ _ _ _ _ => trivial+ avf := fun _ _ _ _ _ _ _ _ _ => trivialhv := fun _ _ _ _ _ _ => Iff.intro (fun _ => trivial) (fun _ => trivial) }facts := fun _ _ => trivialint := fun _ _ _ _ _ _ _ => trivial@@ -112,6 +113,7 @@ theorem empty_herbrand_is_a_model : Model (herbrand []) [] whereeqc := fun a b hab => absurd hab (not_mem_pred [] V.equivalentClass (by simp) a b)eqp := fun a b hab => absurd hab (not_mem_pred [] V.equivalentProperty (by simp) a b)svf := fun r p _ hop => absurd hop (not_mem_pred [] V.onProperty (by simp) r p)+ avf := fun r p _ hop => absurd hop (not_mem_pred [] V.onProperty (by simp) r p)hv := fun r p _ hop => absurd hop (not_mem_pred [] V.onProperty (by simp) r p) }facts := by intro t ht; simp at htint := by intro c l _ hc; exact absurd hc (not_mem_pred [] V.intersectionOf (by simp) c l)@@ -201,6 +203,8 @@ theorem svf_witness_is_a_model : Model (herbrand svfWitness) svfPremises wheresvf := fun r p c hop hsv x y hxy hy =>svf_closed ⟨r, V.onProperty, p⟩ hop ⟨r, V.someValuesFrom, c⟩ hsv⟨x, p, y⟩ hxy ⟨y, V.type, c⟩ hy ⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩+ avf := fun r _ c _ hav =>+ absurd hav (not_mem_pred svfWitness V.allValuesFrom (by decide) r c)hv := fun r _ v _ hhv => absurd hhv (not_mem_pred svfWitness V.hasValue (by decide) r v) }facts := fun t ht => List.mem_cons_of_mem _ htint := by intro c l _ hc; exact absurd hc (not_mem_pred svfPremises V.intersectionOf (by decide) c l)diff --git a/src/reason.rs b/src/reason.rsindex 98cc9830..bc696108 100644--- a/src/reason.rs+++ b/src/reason.rs@@ -267,6 +267,7 @@ impl Reasoner {let mut union_classes: Vec<(u32, u32, Vec<Fact>, Vec<u32>)> = Vec::new();let mut svf_rules: Vec<(u32, u32, u32)> = Vec::new();let mut hv_rules: Vec<(u32, u32, u32)> = Vec::new();+ let mut avf_rules: Vec<(u32, u32, u32)> = Vec::new();if include_ext {let mut restr_avf: HashMap<u32, u32> = HashMap::new();for &(s, p, o) in triple_set.iter() {@@ -275,7 +276,9 @@ impl Reasoner {if p == owl_all_values { restr_avf.insert(s, o); }if p == owl_has_value { restr_hv.insert(s, o); }}- let _ = restr_avf;+ avf_rules = restr_avf.iter()+ .filter_map(|(&r, &filler)| restr_prop.get(&r).map(|&prop| (prop, filler, r)))+ .collect();svf_rules = restr_svf.iter().filter_map(|(&r, &filler)| restr_prop.get(&r).map(|&prop| (prop, filler, r))).collect();@@ -502,15 +505,24 @@ impl Reasoner {}// scm-eqc1, scm-eqc2: equivalentClass → bidirectional subClassOf+ // Both conclusions are W3C scm-eqc1, which licenses two of them+ // from one premise. The second used to be emitted as "scm-eqc2",+ // which is a DIFFERENT W3C rule: it concludes owl:equivalentClass+ // from two subClassOf triples, the opposite direction. An auditor+ // reading that id and looking it up found the wrong rule, which+ // is precisely what the cls-hv1 comment below forbids. Emitting+ // both steps under the rule that licenses them also frees the+ // name for the real scm-eqc2 when it is implemented.for &(a, b) in &equiv_class {emit((a, rdfs_subclass, b), "scm-eqc1", &[(a, owl_equiv_class, b)]);- emit((b, rdfs_subclass, a), "scm-eqc2", &[(a, owl_equiv_class, b)]);+ emit((b, rdfs_subclass, a), "scm-eqc1", &[(a, owl_equiv_class, b)]);}// scm-eqp1, scm-eqp2: equivalentProperty → bidirectional subPropertyOf+ // Same for scm-eqp1 and the name scm-eqp2.for &(a, b) in &equiv_prop {emit((a, rdfs_subprop, b), "scm-eqp1", &[(a, owl_equiv_prop, b)]);- emit((b, rdfs_subprop, a), "scm-eqp2", &[(a, owl_equiv_prop, b)]);+ emit((b, rdfs_subprop, a), "scm-eqp1", &[(a, owl_equiv_prop, b)]);}}@@ -558,6 +570,34 @@ impl Reasoner {}}+ // cls-avf: x type restriction(P, allValuesFrom c), x P y+ // → y type c+ //+ // The restriction was parsed and the parse was thrown away, so+ // 123 owl:allValuesFrom axioms across six shipped files licensed+ // nothing. It is the cheapest missing rule in the OWL 2 RL+ // profile on both axes: the Rust is the cls-svf1 loop with the+ // premises the other way round, and the semantic condition is+ // the mirror of `svf`.+ for &(prop, filler, restr) in &avf_rules {+ let in_restr: HashSet<u32> = type_idx.iter()+ .filter(|&&(_, cls)| cls == restr)+ .map(|&(inst, _)| inst).collect();+ if in_restr.is_empty() {+ continue;+ }+ for &(x, p, y) in triple_set.iter() {+ if p == prop && in_restr.contains(&x) {+ emit((y, rdf_type, filler), "cls-avf", &[+ (restr, owl_on_property, prop),+ (restr, owl_all_values, filler),+ (x, rdf_type, restr),+ (x, prop, y),+ ]);+ }+ }+ }+// cls-hv1: x type restriction(P, hasValue v) → x P v// cls-hv2: x P v, restriction(P, hasValue v) → x type restriction//diff --git a/src/reason_incremental.rs b/src/reason_incremental.rsindex bc5ca51d..9e626116 100644--- a/src/reason_incremental.rs+++ b/src/reason_incremental.rs@@ -35,7 +35,6 @@ const OWL_SYMMETRIC: &str = "<http://www.w3.org/2002/07/owl#SymmetricProperty>";const OWL_FUNCTIONAL: &str = "<http://www.w3.org/2002/07/owl#FunctionalProperty>";const OWL_INVERSE_FUNCTIONAL: &str = "<http://www.w3.org/2002/07/owl#InverseFunctionalProperty>";const OWL_INVERSE: &str = "<http://www.w3.org/2002/07/owl#inverseOf>";-const OWL_SAMEAS: &str = "<http://www.w3.org/2002/07/owl#sameAs>";/// Classes whose assertion `p rdf:type <class>` gives an EXISTING property a new/// characteristic, changing what the store already entails over that property's@@ -327,25 +326,21 @@ impl IncrementalReasoner {emit((near, p.clone(), o.clone()), &mut next);}}- if p == OWL_SAMEAS && o.starts_with('<') {- // The subject's existing triples are a question about the- // whole store, so read every graph, not the default alone.- let q = format!("SELECT ?p ?v WHERE {{ {s} ?p ?v }} LIMIT 10000");- if let Ok(raw) = graph.sparql_select_union(&q)- && let Ok(v) = serde_json::from_str::<serde_json::Value>(&raw)- {- {- for row in v.get("results").and_then(|r| r.as_array()).into_iter().flatten() {- if let (Some(pp), Some(vv)) = (- row.get("p").and_then(|x| x.as_str()),- row.get("v").and_then(|x| x.as_str()),- ) {- emit((o.clone(), pp.to_string(), vv.to_string()), &mut next);- }- }- }- }- }+ // eq-rep-s (owl:sameAs subject replacement) used to run+ // HERE and nowhere else. The full reasoner in reason.rs does+ // not implement it, so loading one graph incrementally and+ // loading it whole produced two different closures, and the+ // incremental one emitted no rule ids and therefore no+ // certificate: its extra triples were materialised outside+ // the Lean guarantee entirely.+ //+ // Removed rather than promoted. eq-rep-* is quadratic in the+ // size of a sameAs clique, and owl:sameAs occurs exactly once+ // in this repository's whole corpus, so the rule bought+ // nothing and cost a divergence between two reasoning paths.+ // If entity resolution is wanted it belongs in an explicit+ // canonicalisation pass with its own contract, not in a+ // forward chainer that only one of two entry points runs.}}frontier = next;diff --git a/tests/lean_certificate_test.rs b/tests/lean_certificate_test.rsindex 7276a70e..80b7397d 100644--- a/tests/lean_certificate_test.rs+++ b/tests/lean_certificate_test.rs@@ -131,17 +131,22 @@ const EVERY_RULE: &str = r#":a owl:sameAs :a2 .:E1 owl:equivalentClass :E2 . :pe1 owl:equivalentProperty :pe2 .:R1 a owl:Restriction ; owl:onProperty :has ; owl:someValuesFrom :F . :w :has :f . :f a :F .+ :RA a owl:Restriction ; owl:onProperty :hasPart ; owl:allValuesFrom :Part .+ :whole a :RA ; :hasPart :bit .:R2 a owl:Restriction ; owl:onProperty :col ; owl:hasValue :red .:K rdfs:subClassOf :R2 . :k a :K . :j :col :red .:I owl:intersectionOf ( :M1 :M2 ) . :i a :M1 , :M2 .:U owl:unionOf ( :N1 :N2 ) . :n a :N2 ."#;-const ALL_RULES: [&str; 20] = [+const ALL_RULES: [&str; 19] = ["rdfs2", "rdfs3", "rdfs5", "rdfs7", "rdfs9", "rdfs11","prp-trp", "prp-symp", "prp-inv1", "prp-inv2", "eq-sym",- "scm-eqc1", "scm-eqc2", "scm-eqp1", "scm-eqp2",- "cls-svf1", "cls-hv1", "cls-hv2", "cls-int1", "cls-uni",+ // scm-eqc1 and scm-eqp1 each license two conclusions, so they appear twice+ // in a certificate under one id. The second used to be emitted as+ // "scm-eqc2" / "scm-eqp2", which name different W3C rules.+ "scm-eqc1", "scm-eqp1",+ "cls-svf1", "cls-avf", "cls-hv1", "cls-hv2", "cls-int1", "cls-uni",];#[test]