LeanRDF

Certificates and the checker

A certificate is a proof DAG: one node per triple, each a fact of the graph or one rule application whose premises are earlier nodes. The checker in LeanRDF.Cert is the only code between an untrusted search and a theorem.

The data type

structure Node where
  id : Nat
  conclusion : Triple
  rule : String          -- "fact" for a triple of the graph
  premises : List Nat    -- ids of earlier nodes, in the rule's body order

structure Certificate where
  version : Nat := 1
  nodes : List Node
  goals : List Nat
  mapping : List (BlankNode × Term) := []   -- the instance of a query's blank nodes
  envelope : Option Envelope := none        -- version 2: what the certificate is about

structure Envelope where
  graph : String       -- Graph.digest of the data
  query : String       -- Graph.digest of the query
  rules : RulesRef     -- the rule set's id and rulesDigest
  regime : String
  checker : String     -- the package and version that wrote it
  toolchain : String
  thm : String         -- the theorem an acceptance stands for

JSON

Every term is written in N-Triples syntax, so a certificate is readable on its own and is read back with the N-Triples term parser:

{"version": 1,
 "mapping": [["_:pet", "<http://example.org/rex>"]],
 "goals": [4],
 "nodes": [
   {"id": 0, "conclusion": ["<http://example.org/A>", "<http://www.w3.org/2000/01/rdf-schema#subClassOf>", "<http://example.org/B>"], "rule": "fact", "premises": []},
   {"id": 4, "conclusion": ["<http://example.org/rex>", "<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>", "<http://example.org/B>"], "rule": "rdfs9", "premises": [0, 3]}]}

Certificate.render writes it and Certificate.parse reads it. mapping may be omitted. A literal may appear as a subject, because conclusions are generalized triples. Reading JSON is untrusted: a malformed certificate is a parse error, and a wrong one fails the checker.

Version 2: the envelope

Version 2 adds an envelope that says what the certificate is about:

"envelope": {"graph": "sha256:6956dd56…", "query": "sha256:7317c98d…",
             "rules": {"id": "rdfs", "digest": "sha256:3f141744…"}, "regime": "rdfs",
             "checker": "leanrdf-elpi 0.4.0", "toolchain": "leanprover/lean4:v4.35.0",
             "theorem": "checkQuery_sound"}

The envelope is untrusted metadata. checkQuery never reads it, and no soundness theorem mentions it. A checker verifies it beside the proof by recomputing the digests from what it read (Envelope.verify). A wrong envelope can make a check fail, never pass. Both versions are read: version 1 has no envelope, and any other version is an error. leanrdf-demo writes version 2.

What the checker accepts

checkNode accepts node n, given the conclusions accepted so far, exactly when:

  1. n.id equals the number of nodes before it, so ids are 0, 1, 2, … in order and premises can only point backwards (the DAG is acyclic by construction);
  2. for "fact": there are no premises and the conclusion is in the graph;
  3. otherwise: the rule name is in the rule list, every premise id is an accepted node, the premise triples match the rule's body patterns in order under one substitution, every guard holds, and the head instantiated by that substitution is the conclusion.

check rules g c checks every node and returns the conclusion of the first goal. checkQuery rules g q c checks every node and then that every triple of the query, with its blank nodes replaced by the mapping, is an accepted conclusion.

The theorems

TheoremStatement
check_soundcheck rules g c = .ok t → Derives rules g t, for any rule list
checkQuery_derivescheckQuery rules g q c = .ok () → every query triple, instantiated, is derivable
checkQuery_soundcheckQuery RDFS.rdfsRules g q c = .ok () → g.RDFSEntails q
checkB_sound, checkQueryB_soundthe same for the Boolean versions, which native_decide can evaluate

So a theorem about concrete data takes one line:

theorem x_type_C : Derives rdfsRules g (T (ex "x") ty (ex "C")) :=
  checkB_sound (c := cert) (by native_decide)

theorem g_entails_q : g.RDFSEntails q := checkQueryB_sound (c := cert) (by native_decide)

native_decide runs the compiled checker, so a theorem proved this way also trusts the Lean compiler. The library's own theorems never use it. It is meant for concrete data in tests and applications, where the alternative is a derivation written by hand.

Clash certificates

A clash certificate says the data has no model. It is a version-2 certificate with no goals and a clash member: a clash rule, a rule with conclusion false, and the ids of the premise nodes in body order.

{"version": 2, "goals": [],
 "clash": {"rule": "cax-dw", "premises": [0, 4, 2]},
 "nodes": [ … the DAG, checked as any certificate … ],
 "envelope": { … "theorem": "checkClash_sound_owl" … }}

checkClash rules clashes g c checks every node, finds the clash rule, and matches the premises against its body in order, with every guard true. checkClash_sound is general in the class of interpretations, and it is instantiated three times:

TheoremRules and clash rulesStatement
checkClash_sound_owlowlRules, owlClashRules¬ ∃ I : DInterp, I.IsOWL ∧ I.Satisfies g
checkClash_sound_elowlELRules, owlClashRulesthe same
checkClash_sound_rdfsrdfsRules, [dClashString]¬ ∃ I : DInterp, I.IsRDFS ∧ I.IsWellTyped ∧ I.Satisfies g

checkClash_explodes_owl derives g.OWLEntails q for every q. The RDFS clash is the one ill-typed literal over the recognized datatypes: an xsd:string containing U+0000, U+FFFE or U+FFFF. Its theorem assumes IsWellTyped, "ill-typed strings denote nothing", which RDF 1.2 Semantics §7.1 states for every D-interpretation and DInterp does not yet force. Only version 2 can carry a clash, so an older reader rejects it instead of misreading it.

Checking a certificate from the command line

leanrdf-check --data DATA --query QUERY --cert CERT.json --rules REGIME reads a certificate written by any tool. It verifies the envelope of a version-2 certificate, then runs the checker under one of five regimes (simple, rdf, rdfs, owl, el). checkRegime_sound gives the regime's entailment from an acceptance. See the command-line tools.

CertifyingDatalog export

toDatalog rules g c writes the certificate, its rules and its graph as a GraphProblemInput of CertifyingDatalog, the verified Datalog proof checker from TU Dresden. A second checker, developed independently, can then accept the same derivation. leanrdf-demo --datalog OUT.json writes one.