Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

Read a path's form before its spelling (#205) (#217)

Fabio Rovaib583d6222a51
diff --git a/lean/Shacl/Compile.lean b/lean/Shacl/Compile.lean
index 8288acc5..60113f66 100644
--- a/lean/Shacl/Compile.lean
+++ b/lean/Shacl/Compile.lean
@@ -234,42 +234,59 @@ def compilePath (G : Graph) : Nat → Term → Except String Path
| 0, node =>
.error s!"the path at {node} nests deeper than the shapes graph is long, so it is cyclic"
| fuel + 1, node =>
- if isIriSpelling node then .ok (.pred node)
- else
- match pathForms G node with
- | [] => .error s!"{node} is not a path this development understands"
- | ["sh:inversePath"] =>
- match objectsOf G node SH.inversePath with
- | [inner] =>
- if isIriSpelling inner then .ok (.inv inner)
- else .error "sh:inversePath of something other than a predicate is not implemented"
- | _ => .error "more than one sh:inversePath on a path node"
- | ["sh:alternativePath"] =>
- match objectsOf G node SH.alternativePath with
- | [l] => do
- let members ← readList G (G.length + 1) l
- let mut ps : List Path := []
- for m in members do
- ps := ps ++ [← compilePath G fuel m]
- foldAltPath ps
- | _ => .error "more than one sh:alternativePath on a path node"
- | ["sh:zeroOrOnePath"] =>
- match objectsOf G node SH.zeroOrOnePath with
- | [inner] => do
- let p ← compilePath G fuel inner
- .ok (.zeroOrOne p)
- | _ => .error "more than one sh:zeroOrOnePath on a path node"
- | ["a sequence path"] => do
- let members ← readList G (G.length + 1) node
- let mut ps : List Path := []
- for m in members do
- ps := ps ++ [← compilePath G fuel m]
- foldSeq ps
- | [one] => .error s!"{one} is not implemented"
- | many =>
- .error s!"the path node {node} carries more than one path form \
- ({String.intercalate ", " many}); SHACL allows exactly one, so this shapes graph is \
- ill formed and no reading of it is chosen"
+ -- The path FORMS are examined before the spelling, and the order is the
+ -- whole of #205.
+ --
+ -- This used to read `if isIriSpelling node then .ok (.pred node)` first, so
+ -- a path node spelled as a named IRI that carried `sh:zeroOrMorePath` was
+ -- compiled as a plain predicate. The evaluator then answered a question
+ -- nobody asked, exit 0, with a verdict: the same shape written with a blank
+ -- node was refused by name with exit 3. A shapes author chooses that
+ -- spelling freely and has no reason to think it matters.
+ --
+ -- That is the collapse the three-state verdict exists to prevent, reached
+ -- by a route the theorem cannot see: `Shacl.validate_spec` covers the
+ -- EVALUATOR, and the compiler had handed it a different shape from the one
+ -- that was written. A node carrying any path predicate now takes the same
+ -- branch whatever its spelling, so the two unimplemented forms are refused
+ -- by name either way, and only a node carrying NO path predicate is read as
+ -- a plain predicate.
+ match pathForms G node with
+ | [] =>
+ if isIriSpelling node then .ok (.pred node)
+ else .error s!"{node} is not a path this development understands"
+ | ["sh:inversePath"] =>
+ match objectsOf G node SH.inversePath with
+ | [inner] =>
+ if isIriSpelling inner then .ok (.inv inner)
+ else .error "sh:inversePath of something other than a predicate is not implemented"
+ | _ => .error "more than one sh:inversePath on a path node"
+ | ["sh:alternativePath"] =>
+ match objectsOf G node SH.alternativePath with
+ | [l] => do
+ let members ← readList G (G.length + 1) l
+ let mut ps : List Path := []
+ for m in members do
+ ps := ps ++ [← compilePath G fuel m]
+ foldAltPath ps
+ | _ => .error "more than one sh:alternativePath on a path node"
+ | ["sh:zeroOrOnePath"] =>
+ match objectsOf G node SH.zeroOrOnePath with
+ | [inner] => do
+ let p ← compilePath G fuel inner
+ .ok (.zeroOrOne p)
+ | _ => .error "more than one sh:zeroOrOnePath on a path node"
+ | ["a sequence path"] => do
+ let members ← readList G (G.length + 1) node
+ let mut ps : List Path := []
+ for m in members do
+ ps := ps ++ [← compilePath G fuel m]
+ foldSeq ps
+ | [one] => .error s!"{one} is not implemented"
+ | many =>
+ .error s!"the path node {node} carries more than one path form \
+ ({String.intercalate ", " many}); SHACL allows exactly one, so this shapes graph is \
+ ill formed and no reading of it is chosen"
/-- Is this parameter present with the literal value `true`?
diff --git a/tests/fixtures/shaclcore/unsupported-path-iri-shapes.nt b/tests/fixtures/shaclcore/unsupported-path-iri-shapes.nt
new file mode 100644
index 00000000..cf659c11
--- /dev/null
+++ b/tests/fixtures/shaclcore/unsupported-path-iri-shapes.nt
@@ -0,0 +1,14 @@
+# THE SAME SHAPE AS unsupported-path-shapes.nt, with the path node spelled as a
+# named IRI instead of a blank node. Nothing else differs.
+#
+# #205: the compiler tested the SPELLING before the path FORMS, so this one was
+# read as a plain predicate `<http://ex.org/thePath>` and answered with a verdict
+# and exit 0, while its blank-node twin was refused by name with exit 3. A shapes
+# author picks that spelling freely and has no reason to think it matters, and
+# the two answers are "I checked this" and "I did not check this".
+<http://ex.org/S> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/shacl#NodeShape> .
+<http://ex.org/S> <http://www.w3.org/ns/shacl#targetClass> <http://ex.org/Person> .
+<http://ex.org/S> <http://www.w3.org/ns/shacl#property> <http://ex.org/NameShape> .
+<http://ex.org/NameShape> <http://www.w3.org/ns/shacl#path> <http://ex.org/thePath> .
+<http://ex.org/thePath> <http://www.w3.org/ns/shacl#zeroOrMorePath> <http://ex.org/name> .
+<http://ex.org/NameShape> <http://www.w3.org/ns/shacl#minCount> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
diff --git a/tests/shacl_core_verified_test.rs b/tests/shacl_core_verified_test.rs
index 6d105eae..feacac1f 100644
--- a/tests/shacl_core_verified_test.rs
+++ b/tests/shacl_core_verified_test.rs
@@ -963,13 +963,22 @@ fn the_bridge_refuses_what_it_cannot_check() {
dup.raw
);
- // Six refusals, each naming what it refused. One per way the pipeline can
- // decline: an unknown parameter, an unimplemented path form, a regular
- // expression outside the proved subset, recursion, the extension mechanism, and
- // a datatype whose lexical space is not implemented.
- let refusals: [(&str, &str, &str); 6] = [
+ // Seven refusals, each naming what it refused. One per way the pipeline can
+ // decline: an unknown parameter, an unimplemented path form in each of its
+ // two SPELLINGS, a regular expression outside the proved subset, recursion,
+ // the extension mechanism, and a datatype whose lexical space is not
+ // implemented.
+ //
+ // The IRI-spelled path is #205 and it is the reason the pair is listed
+ // rather than one of them: the compiler tested the spelling before the path
+ // forms, so a named path node carrying `sh:zeroOrMorePath` was read as a
+ // plain predicate and answered with a verdict, while its blank-node twin was
+ // refused. Two shapes graphs saying the same thing, one checked and one not,
+ // and no way for the author to tell which they had written.
+ let refusals: [(&str, &str, &str); 7] = [
("conforming-data.nt", "unsupported-shapes.nt", "shacl#prefixes"),
("conforming-data.nt", "unsupported-path-shapes.nt", "zeroOrMorePath"),
+ ("conforming-data.nt", "unsupported-path-iri-shapes.nt", "zeroOrMorePath"),
("conforming-data.nt", "unsupported-regex-shapes.nt", "'+'"),
("conforming-data.nt", "recursive-shapes.nt", "recursive"),
("conforming-data.nt", "component-shapes.nt", "extension mechanism"),
@@ -991,6 +1000,22 @@ fn the_bridge_refuses_what_it_cannot_check() {
);
}
+ // The property behind the pair above, asserted directly: the two spellings
+ // must give the SAME answer. Listing both in the table would still pass if
+ // some future change made both of them answer rather than both refuse.
+ let by_bnode = run_bridge(&f.join("conforming-data.nt"), &f.join("unsupported-path-shapes.nt"));
+ let by_iri = run_bridge(&f.join("conforming-data.nt"), &f.join("unsupported-path-iri-shapes.nt"));
+ assert_eq!(
+ by_bnode.code, by_iri.code,
+ "the same shape spelled two ways gave exit {} and exit {}. Whichever is right, a \
+ shapes author cannot be expected to know that the spelling decided it.\n{}\n{}",
+ by_bnode.code, by_iri.code, by_bnode.raw, by_iri.raw
+ );
+ assert_eq!(
+ by_bnode.json["status"], by_iri.json["status"],
+ "{}\n{}", by_bnode.raw, by_iri.raw
+ );
+
// A file that is not N-Triples is exit 2, which is a different answer again.
let broken = run_bridge(&f.join("malformed.nt"), &shapes);
assert_eq!(broken.code, 2, "a parse failure must be exit 2, not a verdict: {}", broken.raw);