Proofs (experimental)¶
Experimental
The proofs are a research project. They are not shipped with ddigraph, and CI does not check them. They prove facts about a small model of the code, not about the code itself. The model may change in any release.
The proofs/ folder of the repository holds a Lean 4
project. Lean checks each step of a proof. A proof that builds, and that uses
only Lean's standard axioms, has no gap. The project needs nothing but Lean:
no Mathlib, and no other package.
Each proof is about a model: a short Lean version of one part of ddigraph. A test runs the model on test vectors and compares what it writes with what ddigraph writes. So the model and the code are checked against each other.
What is proved¶
Two identities never share a text form¶
Each node has one identity, with one text form, such as
urn:ddigraph:scoped:survey%202024:v%3A1 (see
Node Identity). The node's key is a hash of
that text. So two different identities must never give the same text. Lean
proves it as text_injective: if two identities have the same text form,
they are the same identity.
The model follows ddigraph.urn.text:
- There are three kinds:
Urn,ScopedandDerived. - Each kind has its own prefix:
urn:ddi:,urn:ddigraph:scoped:orurn:ddigraph:derived:. - Each component is percent-encoded. Each byte of its UTF-8 form outside
A-Z a-z 0-9 - . _ ~becomes%XX, with uppercase hex digits. - The ordinal of a derived identity is written in decimal, from 0 to 2^64 - 1.
The proof rests on four lemmas:
| Lemma | What it says |
|---|---|
encode_injective |
Two different byte strings never encode alike. |
colon_not_mem_encode |
An encoded component holds no :. |
split_intercalate |
Splitting a text form on : gives back its parts. |
decimal_injective |
Two different numbers never have the same decimal form. |
The memory lens keeps its laws¶
A lens writes the graph to a backend and reads it back. Every lens must keep
two laws, which tests/lens_laws.py checks:
- PutGet: write a stream, read it back, and you get the stream.
- GetPut: write back what a read gives, and nothing changes.
Lean proves both laws for a model of the memory lens
(ddigraph.lens.memory). The model holds one dataset as a map from key to
node. A node is opaque, apart from its key. A write puts each node of a
chunk under its key. A read gives the nodes in key order.
put_get: write a chunk with distinct keys to an empty lens. A read then gives the chunk back, sorted by key.get_put: write what a read gives back to the lens. The lens is then just as it was.
What the model leaves out¶
The proofs hold for the model. Where the code does more than the model, the proofs say nothing about it.
For the identity:
- A Python
strholds code points, and these can be lone surrogates. A Lean string holds only Unicode scalar values, so the model starts from the UTF-8 bytes of each component.textcannot encode a lone surrogate, so it raises an error. Each component it does write has a model. - The model writes the text form as bytes. The text form is ASCII, so each byte is one character.
textrefuses an empty component. The model accepts one, and the proof does not need components to be non-empty. So the proof covers more thantextwrites.- In the model, the ordinal is a 64-bit unsigned integer, so its type sets
its range.
textchecks the range of a Pythonint, and refuses abool. - The key is a BLAKE3 hash of the text form. The proof is about the text form, not the hash.
For the memory lens, the code does all of this, and the model does not:
- It holds several datasets, and a read can give all of them at once.
- It writes stubs. A stub never changes a stored node, and a real node fills a stub in place.
- It writes relationships, and checks that both ends of each one exist.
- It keeps type labels by flavor, and the properties the spec owns for each label. A write sets those properties, and keeps the ones a user set.
- It refuses a stream that breaks a rule, and it has a dry run and a purge.
- A read gives chunks of 200 nodes, with the Dataset node first. In the
model, a read gives one chunk. A stream is its chunks written in order,
and the model proves that is the same as one chunk (
write_append).
The model also asks for a chunk with distinct keys. The lens tests compare
streams with ddigraph.conform.canonical, which refuses two nodes that
share a key but differ in content. If a node comes twice with the same
content, canonical keeps one copy. The model's put_get does not cover
that case.
How to check them¶
You need Lean 4.35.0. elan installs it,
and picks the version that proofs/lean-toolchain names. Then build the
project, from a clone of ddigraph:
cd proofs
lake build
The build checks every proof. It takes a few seconds. Then print the axioms that each main theorem uses:
lake env lean Axioms.lean
'Ddigraph.Identity.text_injective' depends on axioms: [propext, Classical.choice, Quot.sound]
'Ddigraph.Lens.put_get' depends on axioms: [propext, Classical.choice, Quot.sound]
'Ddigraph.Lens.get_put' depends on axioms: [propext, Classical.choice, Quot.sound]
These are Lean's standard axioms. A proof left open with sorry still
builds, with a warning, but it adds sorryAx here.
Then run the model on the test vectors in proofs/vectors.json. It prints
the text form of each identity, one per line:
lake exe vectors vectors.json
urn:ddi:int.example:V1:1.0.0
urn:ddi:a%3Ab:100%25:x%20y
urn:ddi:Qu%C3%A9bec:%F0%9F%98%80:AZaz09-._~
urn:ddigraph:scoped:survey%202024:v%3A1
urn:ddigraph:scoped:a%2Fb%3Fc%23d%5Be%5Df%40g:%21%24%26%27%28%29%2A%2B%2C%3B%3D%22%3C%3E%5C%5E%60%7B%7C%7D
urn:ddigraph:derived:0123456789abcdef0123456789abcdef:universe:fedcba9876543210fedcba9876543210:0
urn:ddigraph:derived:p:r%3As:h:9
urn:ddigraph:derived:p:r:%2520:10
urn:ddigraph:derived:%E6%97%A5%E6%9C%AC:%F0%9D%84%9E%20clef:h:18446744073709551615
The test tests/test_proofs.py does all of this. It builds the project, and
compares each line with ddigraph.urn.text of the same vector. It checks the
axioms, and that the vectors cover each kind and each hard case. It also
checks that no file in proofs/ holds sorry, sorryAx, admit, stop or
an axiom declaration. It finds Lake from the DDIGRAPH_LAKE variable, or as
lake on your PATH:
DDIGRAPH_LAKE=~/.elan/bin/lake pytest tests/test_proofs.py
CI does not run them¶
CI has no Lean, so the tests that need Lake skip there. The checks that need
no Lean still run: the vectors, the output shown on this page, the search
for sorry, and the check that the project needs no package. The search
only looks for words, so the axioms check, which needs Lake, is the full
one. Run the whole test yourself when you change
ddigraph.urn, ddigraph.lens.memory or proofs/.