The trouble with a plain fact
A knowledge graph is made of small facts: Frankfurt has a route to London. Subject, predicate, object. That is enough until you want to say something about the fact:
The route is 655 km long. (A property of the connection, not of either city.)
We got that number from an atlas. (Where a fact came from.)
We trust the atlas about 90%. (A fact about where a fact came from.)
Alice booked that route, seat 12A, for €120. (One event with many parts.)
Another source says 650 km, which disagrees with the atlas. (A link between two facts.)
A plain triple has no name, so nothing can point at it. People work around this with blank nodes, named graphs or "reification" vocabularies, and each workaround loses something. A statement graph fixes the problem at its root. Every statement gets an identity, and a statement may point at other statements.
What a statement graph is
The idea comes from Gelling, Fletcher and Schmidt, who formalized the data model behind Amazon Neptune's OneGraph. In plain words, a statement graph is a graph with two kinds of things:
Values: cities, numbers, names, words. They sit at the bottom.
Statements: each has its own id and exactly three outgoing arrows, labelled s (subject), p (predicate) and o (object). The predicate arrow points at a value. The subject and object arrows point at a value or at another statement.
Two rules keep it honest. Every arrow must land on something that exists, so nothing is missing. And no statement may lean on itself, even through a chain of others, so there are no loops.
Figure 1. Four statements. Statement #1 is the plain fact. #2 points at #1 (the route is 655 km), #3 points at #2 (the number came from an atlas), and #4 points at #3 (we trust the atlas 90%).
That is the whole model. The power comes from what it leaves out: there is no special kind of edge, no separate annotation layer, no second type of node for metadata. A statement about a statement is just another statement.
Building bigger things from one brick
Because any statement can be the subject or object of another, structures that usually need special features fall out of the same rule. Here are five, all taken from one small travel memory (the full list of eleven statements is in the Lean section below).
Figure 2. One rule, many structures. (1) A property on an edge. (2) A chain of provenance. (3) An event with many parts, around a hub statement. (4) A group of statements, and a statement that links two other statements.
1. Edges with properties
Property graphs (Neo4j, Neptune, Kùzu) let an edge carry key-value pairs. Here that is two statements: #1 is the edge, and #2 is a statement whose subject is #1. Nothing new is needed, and the property can itself carry properties.
2. Provenance, as deep as you like
"Who said so" is a statement about a statement (#3 about #2). "How much do we trust them" is a statement about that (#4 about #3). Most systems stop at one level of metadata. This model has no levels: you add as many as your story needs.
3. Events with many parts
"Alice booked the route, seat 12A, fare €120" does not fit in one triple. Statement #5 (alice booked #1) acts as the hub, and #6 and #7 hang the seat and the fare off it. This is the n-ary relation pattern, with a real identity for the event instead of a blank node.
4. Groups of statements
Named graphs put triples into a box. Here the box is a value (tripPlan), and membership is an ordinary statement: #8 says #1 partOf tripPlan and #9 says #5 partOf tripPlan. A statement can belong to several groups, and membership can itself be annotated, dated or retracted.
5. Edges between edges
Statement #11 has a statement at both ends: #10 conflictsWith #2. One edge points at another edge, the core move of a metagraph, where edges can be the ends of other edges. Disagreements, justifications ("#x because #y") and versions ("#a replaces #b") are all edges of this kind.
And time
In tiramemsu, the store these records come from, each statement also carries two clocks. One records when we learned it (the transaction that added it, and the one that retracted it). The other records when it was true in the world (a valid-time interval). Retracting a statement doesn't delete it, so you can always ask what the graph looked like at transaction 42, or what was true on 1 March.
Why "no loops, nothing missing" matters
Real data is messy. A statement can point at one that was never loaded, or two statements can point at each other. These graphs can still be stored, but they are not statement graphs, and they cannot be written out as nested RDF 1.2: you can't write a triple term that contains itself, or one that contains a statement you don't have.
So LeanRDF computes the kernel: the largest part of the data that is a proper statement graph. It works like peeling an onion. First keep the statements that point only at values, then those that point only at statements already kept, and repeat until nothing changes. Whatever is left out is dangling, sits on a cycle, or depends on something that does.
Figure 3. The kernel of a broken graph. #2 points at a statement that isn't there, #3 leans on #2, and #4 and #5 point at each other. Only #1 and #6 form a proper statement graph.
The kernel isn't a heuristic. We prove it is closed (nothing missing), acyclic (no loops), and the largest such part: any other part with those two properties lies inside it.
What it looks like in RDF 1.2
RDF 1.2 adds triple terms: a whole triple, written <<( s p o )>>, can sit in the object position, and rdf:reifies connects a name to it. So each statement becomes a name (urn:tiramemsu:stmt:N), the triple it reifies, its clocks, and, if it is live in the view you asked for, the triple itself. This is LeanRDF's real output for the first three statements, unedited:
<http://example.org/FRA> <http://example.org/route> <http://example.org/LHR> .
<urn:tiramemsu:stmt:1> <http://example.org/distanceKm> "655"^^<http://www.w3.org/2001/XMLSchema#integer> .
<urn:tiramemsu:stmt:1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies> <<( <http://example.org/FRA> <http://example.org/route> <http://example.org/LHR> )>> .
<urn:tiramemsu:stmt:1> <urn:tiramemsu:tm:txAdded> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
<urn:tiramemsu:stmt:2> <http://example.org/source> <http://example.org/atlas> .
<urn:tiramemsu:stmt:2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies> <<( <urn:tiramemsu:stmt:1> <http://example.org/distanceKm> "655"^^<http://www.w3.org/2001/XMLSchema#integer> )>> .
<urn:tiramemsu:stmt:2> <urn:tiramemsu:tm:txAdded> "2"^^<http://www.w3.org/2001/XMLSchema#integer> .
<urn:tiramemsu:stmt:3> <http://www.w3.org/1999/02/22-rdf-syntax-ns#reifies> <<( <urn:tiramemsu:stmt:2> <http://example.org/source> <http://example.org/atlas> )>> .
<urn:tiramemsu:stmt:3> <urn:tiramemsu:tm:txAdded> "3"^^<http://www.w3.org/2001/XMLSchema#integer> .Inside the kernel, a statement can also be written as one nested triple term, a quotation of a quotation. Statement #4 becomes a triple term four levels deep: ((((FRA route LHR) distanceKm 655) source atlas) confidencePct 90).
Formalizing it in Lean
LeanRDF is an RDF library written in Lean 4, a programming language whose type checker also checks mathematical proofs. Its statements package (import LeanRDFStatements) models these records and proves the claims above. Every snippet below comes from statements/StatementsTests/Article.lean, which compiles with the test suite, so if the library changes and the article becomes false, the build fails.
The model
A record is a statement with an id, a subject and an object that are each a value or a reference to another id, a predicate, and two clocks. A graph keeps its records sorted by id, so ids are distinct and two graphs with the same records are equal.
structure Record where
eid : Nat
s : Node -- a term, or `.eid n`: a reference to statement n
p : IRI
o : Node
txAdded : Nat
txRetracted : Option Nat := none
validFrom : Option Int := none
validTo : Option Int := none
structure StatementGraph where
records : List Record
sorted : records.Pairwise (·.eid < ·.eid)The travel memory, in Lean
A small helper writes a statement whose id is also the transaction that added it. The eleven statements of Figure 2 then read almost like the sentences above:
def say (e : Nat) (s : Node) (p : String) (o : Node) : Record :=
{ eid := e, s, p := ex p, o, txAdded := e }
def memoryRecords : List Record := [
say 1 (thing "FRA") "route" (thing "LHR"), -- a plain fact
say 2 (stmt 1) "distanceKm" (num 655), -- a property of the edge 1
say 3 (stmt 2) "source" (thing "atlas"), -- who said the distance
say 4 (stmt 3) "confidencePct" (num 90), -- how sure we are of that source
say 5 (thing "alice") "booked" (stmt 1), -- an n-ary event, with 5 as its hub
say 6 (stmt 5) "seat" (str "12A"),
say 7 (stmt 5) "fare" (num 120),
say 8 (stmt 1) "partOf" (thing "tripPlan"), -- a group of statements
say 9 (stmt 5) "partOf" (thing "tripPlan"),
say 10 (stmt 1) "distanceKm" (num 650), -- a second opinion
say 11 (stmt 10) "conflictsWith" (stmt 2) -- an edge between two edges
]
/-- The records are listed by increasing id, which `decide` checks. -/
def memory : StatementGraph := ⟨memoryRecords, by decide⟩Checks that run at compile time
#guard evaluates an expression while the file compiles and fails the build if it is false. These are tests, run by the compiler:
#guard memory.check == [] -- well formed
#guard memory.isClosed && memory.isAcyclic -- no dangling reference, no cycle
#guard memory.kernel == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
#guard (memory.toRDF {}).size == 33 -- 11 × (reifier, clock, asserted triple)
#guard (decode {} (memory.toRDF {})).toOption.map (·.records) == some memory.records
#guard broken.check == [] -- every record is fine on its own
#guard !broken.isClosed && !broken.isAcyclic -- the graph as a whole is not
#guard broken.kernel == [1, 6] -- the largest part that is a statement graphTheorems from the library
Tests check one example. Theorems hold for every graph. These are the main ones the package provides, proved once for all inputs. The first three are the results of the export-boundaries paper (Zenodo 10.5281/zenodo.23335652), stated for these records:
Theorem | In plain words
criterion | A graph can be exported as a Statement Graph (in Gelling, Fletcher and Schmidt's sense) exactly when nothing is missing and there are no loops.
kernel_closed, kernel_acyclic, kernel_greatest | The kernel has nothing missing and no loops, and it is the largest part of the graph with both properties.
quote_kernel | Every statement in the kernel can be written as one nested RDF 1.2 triple term.
history_kernel | If statements only point at statements added earlier, the whole graph is its own kernel.
decode_toRDF, toRDF_decode | The RDF 1.2 export loses nothing: decoding it gives back exactly the graph you started with, and anything the decoder accepts re-exports to the same RDF.
Our own theorems, built on the library
Most real stores grow in layers: you can only point at a statement that already exists. TxStrict says exactly that: every reference points at a statement added in an earlier transaction. From the library's results we prove that such a graph always has a Statement Graph export, and that every one of its statements can be quoted, whatever it says:
/-- If every statement points only at statements added in earlier transactions, the graph has
a Statement Graph export (Gelling, Fletcher and Schmidt), whatever it says. -/
theorem layered_exports (g : StatementGraph) (h : g.TxStrict) :
∃ G : SGraph Nat Term, G.IsSG ∧ DirectExport g.content id g.S G := by
have ordered : g.TxOrdered := fun r hr x hx => by
obtain ⟨r', hr', he, hlt⟩ := h r hr x hx
exact ⟨r', hr', he, Nat.le_of_lt hlt⟩
exact (criterion g).2
⟨(isClosed_iff g).2 (history_closed ordered), (isAcyclic_iff g).2 (history_kernel h).1⟩
/-- In such a graph every statement can be written as one nested RDF 1.2 triple term. -/
theorem layered_quotes (g : StatementGraph) (h : g.TxStrict) {e : Nat} (he : e ∈ g.eids) :
∃ t, g.quote e = .ok t :=
quote_kernel ((history_kernel h).2 e |>.2 he)Then the travel memory gets a proof instead of a test. Lean's decide checks that the memory is built in layers, and the general theorem does the rest:
theorem memory_layered : memory.TxStrict := by unfold TxStrict; decide
theorem memory_exports : ∃ G : SGraph Nat Term, G.IsSG ∧ DirectExport memory.content id memory.S G :=
layered_exports memory memory_layeredThese proofs depend on nothing beyond Lean's three standard axioms (propext, Classical.choice, Quot.sound). There is no sorry and no native_decide.
Figure 4. The path from records to RDF 1.2 and back, with the theorem behind each step.
What is proven, and what is not
Proven, for every graph: the export criterion, the kernel's three properties, quoting inside the kernel, history closure, and the exact round trip through RDF 1.2. Also the two layered-graph theorems in this article, and that the travel memory has a Statement Graph export.
Tested, not proven: the #guard lines check particular examples. tiramemsu's Rust exporter (tm export --format sg-nt12) is not verified in Lean. Instead, its recorded exports are decoded and re-exported byte for byte by LeanRDF's test suite on every build.
Not claimed: that the facts are true. A proof that 655 km survives the export says nothing about whether the atlas is right. That is what statements #3, #4 and #11 are for.
Try it
git clone https://github.com/Volland/rdf-lean
cd rdf-lean/statements
lake build StatementsTests # compiles Article.lean: every #guard and every proofThe book chapter on statement graphs explains the ideas at a slower pace, and the documentation page lists the API and the leanrdf-sg command-line tool. The source is at github.com/Volland/rdf-lean, and the store the records come from is tiramemsu.
One simple brick, a statement that can point at statements, gives you edge properties, provenance chains, events, groups and edges between edges. Lean then lets you prove that none of it gets lost on the way to RDF 1.2 and back.