Binary library wire format

The manifest and the directory

A library is a directory of immutable, content-addressed files. One small mutable file, CURRENT, names the current version's manifest; the manifest lists that version's partitions, every statement and who provides it, every alias, every declaration with its citations, and what the version's changes withdrew or superseded. The designs are ST7.1 to ST7.3 and ST9.1; the rules are Proof libraries.

The directory

<library>/
  CURRENT                  the current manifest's file hash: 64 lowercase hex digits and "\n"
  LOCK                     present only while a writer or collector holds the library
  manifests/<hash>.jlm     manifests (kind-4 containers)
  parts/<hash>.jlp         partitions (kind-6 containers)
Rule Detail
file names <hash> is the lowercase hexadecimal SHA-256 of the file's bytes (plain SHA-256, no prefix), so sha256sum checks it
immutability no file's bytes change after it is renamed into place
CURRENT exactly 65 bytes: 64 lowercase hexadecimal digits, then one 0a; anything else, or a symbolic link, is refused (FORMAT)
LOCK the text pid <decimal>\nstart <decimal seconds>\ntoken <32 hexadecimal digits>\n, created exclusively; its contents only identify its holder
temporaries a writer's .<hash>.tmp files and CURRENT.tmp; no other names occur. A reader opens only the names it needs and never lists the directory, so other names do not affect it; a collector removes only .jlm, .jlp, and temporary files

A reader refuses a manifest whose bytes do not hash to its name (DIGEST), a partition whose length differs from the manifest's (FORMAT) or whose bytes do not hash to its name (DIGEST), a library without CURRENT (FORMAT), a missing library directory (MISSING_DEPENDENCY), and a manifest or partition it cannot read (IO). Identities do not depend on files: the same content partitioned differently has other file hashes and the same declarations and statements.

The manifest

A manifest is a container of kind 4, read with the container's default limits (at most 64 MiB). It has eight sections, in library format 2.2. Inside it:

  • numbers are minimal varints of at most 9 bytes (primitives), except the library format version, a fixed u32;
  • a name is a varint byte length and well-formed UTF-8;
  • identities and file hashes are 32 raw bytes;
  • a flag is one byte, 0 or 1 (FORMAT otherwise);
  • an optional value is a flag and, if 1, the value.

Lists are a varint count, bounded by the remaining bytes and a minimum item size before they are read (FORMAT at the count). Every section is read to its exact end (FORMAT at the first extra byte).

Section 1: header

Field Encoding
library format version 4 bytes, little-endian u32: 4, format 2.2 (VERSION for every other value)
profile a list of identities (minimum 32 bytes each), strictly increasing: the identities of the profile's non-core declarations; empty for the profile none
parent optional: the file hash of the manifest this version extends, absent for a first version

Sections 7 and 8 MUST occur (FORMAT otherwise).

Section 2: partitions

A list (minimum item 41 bytes), in load order:

Field Encoding
name name: the library's theory name
sub-partition varint: the partition's position in the list
file hash 32 bytes: the partition file's name
length varint: the file's length
rows varint: the partition's row count
root 32 bytes: the Merkle root of its DAG blocks' digests
detached flag: the first partition of a chain written over an imported statement layer
dependencies a list of varints (minimum 1 byte each): positions of earlier partitions, strictly increasing
declarations varint
providers varint: the partition's own providers
stored proofs varint: its export-table entries
records varint: 0
expanders a list of varints: empty (FORMAT otherwise)

Load order. The partitions are in Kahn order of their dependencies, breaking ties by (name in byte order, sub-partition): at each position, the least partition all of whose dependencies are earlier. A reader simulates this and refuses another order (FORMAT at the first misplaced entry), and a repeated (name, sub-partition) pair. A detached partition has no dependencies.

Two entries MAY name one file hash. A partition's bytes hold no field for its position: its first row number is the sum of the row counts of the entries before it in its chain (sub-partitions). Two sub-partitions whose rows encode alike therefore have the same bytes, and the manifest names that one file at each of their positions.

What a library holds. A loader further requires every partition to have the first one's name and its own position as sub-partition number, the first partition not to be detached, a detached partition and the first to have no dependencies, and every other partition to depend on exactly the one before it (FORMAT). The counts are compared with what the load finds; a reader MUST NOT size an allocation from them, and the record count must be 0.

Section 3: statements

A list (minimum item 68 bytes) of every provider of the library and of the profile, strictly increasing by statement identity:

Field Encoding
statement identity 32 bytes
kind varint: 1 theorem, 2 axiom, 3 definitional statement, 4 profile
partition optional varint: the declaring partition's position; present exactly unless the kind is 4
provider 32-byte declaration identity, then the part as a varint (at most \(2^{32} - 1\))
reuse count varint: how many of the library's PROOF rows cite the provider

The reuse count of a provider counts the library's PROOF rows whose D operand names it, each row once (ST9.1). A statement with a high reuse count and no alias is a lemma sources use without naming. A statement that a version withdrew, and the earlier provider of a statement a version superseded, are not listed.

Section 4: names

A list (minimum item 3 bytes) of every alias, strictly increasing by name in byte order:

Field Encoding
name name
statement varint: a position in section 3
type variables a list of names: the statement's type-variable names in key order

The writer gives a new alias a library name (Definition L-3a) and keeps its base's aliases as written; a reader does not check the names. A load that declares an alias's provider checks its type-variable names against the statement: one nonempty name for each type variable of its key, and no name for two variables of one sort (FORMAT, naming the alias).

Section 5: expanders

A list that must be empty: a varint 0. A reader refuses any other count (FORMAT), so one library has one manifest encoding.

Section 6: withdrawn

A list (minimum item 36 bytes) of every declaration that a version's change withdrew, cumulatively over the version's ancestors, strictly increasing by (identity, part):

Field Encoding
declaration 32-byte identity, then the part as a varint
statement optional 32-byte statement identity: the statement it provided
reason name: why it was withdrawn, or empty for a dependent
cited optional reference (identity, varint part): the withdrawn declaration a dependent cited

The reason is empty exactly when cited is present (FORMAT otherwise). The reference writer writes one entry for each statement a change withdraws: its provider's reference, the statement identity, the reason withdrawn, and no citation.

Section 7: declarations

A list (minimum item 38 bytes) of every declaration of the version, a base version's included, strictly increasing by identity:

Field Encoding
identity 32 bytes
partition varint: the position in section 2 of the partition whose section 2 holds it
position varint: its position in that section 2
kind varint below 256: the declaration form tag less 32
name varint length and bytes: its name field; a multi-part declaration's part names joined by zero bytes; empty when it has none
record citations a list of varints, strictly increasing: the positions in this section of the library declarations its declaration bytes cite (core and profile declarations are not listed)
proof citations a varint count n, then n varint gaps: the first a position, each later one the difference from the previous position less one; a position below this section's length is an entry of it, and the length plus i is the profile declaration at position i of section 1's list; only a Proven has any

No two entries share a (partition, position), and each partition has exactly its declaration count of entries, except that a theorem whose proof a version replaced is listed once, at its newest proof's partition, and each earlier proof's partition counts it through its section-8 entry. A citation names another entry. The proof citations are those of ST9.1: the declarations the theorem's export-table proof row cites, directly or through its premises, other than another theorem's own proof.

Section 8: superseded

A list (minimum item 66 bytes), strictly increasing by (statement identity, provider identity, part, partition):

Field Encoding
statement 32 bytes: the statement identity
provider 32-byte identity, then the part as a varint
partition varint: the position in section 2 of the provider's partition

An entry is classified by its provider's kind in section 7. A Proven records a replaced proof: the theorem was declared again, with a new proof, at a later partition, and the entry's partition is the earlier proof's; if section 3 lists the statement, its provider is this one. An Axiom records a superseded axiom: if section 3 lists the statement, a Proven of another identity provides it. Any other kind is refused (FORMAT). Entries stay when a later version withdraws their statement.

Reader checks

Before reading any partition, a reader checks:

  1. the manifest's file hash (DIGEST), the container, and every section's grammar above, with every list bounded by the bytes that remain;
  2. section 3 strictly increasing by identity, section 4 by name, section 5 and every partition's expander list empty, section 6 by (identity, part), sections 7 and 8 as above; every position in range; the load order exactly as specified;
  3. the profile: the section-1 list is empty, or equals the identities of the non-core declarations of a profile the reader builds (classical or isabelle); a list that matches none is refused (LIBRARY). A whole load then requires section 3's kind-4 entries to be exactly the statements the profile's declarations provide, each with the reference of the declaration and part that provides it (FORMAT).

Publication

A writer publishes a new version:

  1. Create LOCK with O_CREAT | O_EXCL; if it exists, refuse (LOCKED).
  2. Read CURRENT, if any, as the base. The new manifest's parent MUST be the base; if CURRENT is another version, refuse (CONFLICT).
  3. For each new partition and then the manifest: write it to .<hash>.tmp in its directory, fsync it, rename it to <hash>.jlp or <hash>.jlm, and fsync the directory. A file whose name exists already is not written again: its bytes are compared with the new ones (DIGEST if they differ), and its modification time is set to now. A partition file the manifest names at several positions is so written once.
  4. Read CURRENT again; if it is no longer the base, or LOCK is no longer the file this writer created, refuse (CONFLICT).
  5. Write CURRENT.tmp, fsync it, rename it over CURRENT, set the new manifest's modification time to now, and fsync the library directory.
  6. Remove LOCK and fsync the library directory.

Before CURRENT is replaced, the manifest passes reader checks 1 and 2, its parent is the base, and it names every new partition with that partition's length (FORMAT otherwise); a partition it names that is not new MUST be present with its bytes (MISSING_DEPENDENCY when absent, DIGEST when its bytes differ). The reference writer installs each partition as soon as it has encoded it, so it holds one partition's bytes at a time. A failure before step 5's rename removes the writer's temporaries and LOCK, leaving CURRENT unchanged; files renamed into place are unreferenced and harmless. After that rename, the new version is current and complete. A writer never removes a LOCK it did not create.

A reader reads CURRENT once, then opens only immutable files, so it sees one complete version whatever writers do; it reads every partition it will load when it reads the manifest.

A collector takes LOCK as a writer does, keeps the current manifest, its first k ancestors by parent link, every further ancestor whose successor became current less than an hour ago (by the successor's modification time), and every partition they name; it removes other .jlm and .jlp files more than an hour old, and every temporary, then removes LOCK. The explicit unlock operation removes a LOCK left by a crashed holder; removing a live holder's LOCK is unsafe. The reference implementation has both (_library_dir); its public API does not export them.

The reference implementation runs on Linux x86-64 and 64-bit MacOS; its writer and collector open every name relative to a pinned directory without following symbolic links, and bound every read.

Versions, withdrawal and re-checking

A version is never changed: a change is published as a new version whose parent is the one it changed. It may add declarations, in new partitions appended to the base's; withdraw statements that nothing in the version cites (section 6); and replace a theorem's proof or supersede an axiom by a theorem (section 8). Rule L1 as amended governs all three (Proof libraries).

Loads are strict: one refused row refuses the whole load. Re-checking a version loads it whole into a fresh bank and checks every row; it writes nothing (ST7.5). A version that no longer checks is repaired by a change that withdraws or replaces what fails.

judgment v1.0.0 for Mojo 1.2 · Apache License 2.0 with LLVM Exceptions · Source on GitHub