Binary library wire format

Loading a library

A loader turns a library's bytes into rows of a checked bank. It trusts nothing it reads: every row is committed again into a fresh bank, every declaration is rebuilt from its bytes and declared again, and every row is checked again by the checker. This page is the loader's checklist, in order, in implementer terms. It is Algorithm L-5 with the checks of ST7.3 and ST9.8 spelled out. What a successful load establishes is Theorem L-6.

A loader needs a termbank.dag bank of termbank format 2 that holds the logic core and the profile the library extends, built exactly as the library's writer built it, and the stateless checker of judgment_core. Everything the load commits goes into that fresh bank, which the loader returns only if every check passes. Any refusal drops the bank and changes nothing else. Library.snapshot and Library.recheck run this checklist over the whole version.

Declaration inspection and reconstruction use the same bounded ST1.4 cursor and field walker. The cursor borrows names, literal payloads and operand spans from the input; inspection skips unneeded payloads without copying them. Reconstruction resolves references and backward node edges before interning. Both consumers require complete input consumption; sharing syntax does not replace the declaration or kernel checks.

Disk sections and sealed snapshots feed one row reconstruction loop through immutable input adapters. The disk adapter checks section envelopes and stated identities before any bank mutation. The snapshot adapter borrows each canonical declaration record and its computed identity, avoiding a bulk section buffer and repeat hashing. Both rebuild declarations through the same ST1.4 consumer and preserve its typed refusals. Snapshot publication still checks every row in a fresh bank before writing, and subsequent disk loads check them again.

1. Open the version

  1. Read CURRENT once: 65 bytes, 64 lowercase hexadecimal digits and a newline (directory); its absence is FORMAT.
  2. Read manifests/<hash>.jlm; its SHA-256 MUST be <hash> (DIGEST). To load an earlier version, follow parent links from CURRENT to it; a version that is not CURRENT or an ancestor of it is refused (FORMAT).
  3. Read every partition the load needs, parts/<file hash>.jlp, now: later reads then see this version even if a collector removes names.

2. Check the manifest

  1. Decode the manifest with reader checks 1 and 2: the container, the grammar, every order and range, the load order. Library format 2.2 is REQUIRED (VERSION for every other version).
  2. Identify the profile from section 1's list: empty for none, otherwise the profile whose non-core declarations' identities, sorted, equal it (LIBRARY when none does). Build it in the fresh bank.

3. Each partition, in load order

For partition \(k\) of section 2:

  1. Its record. Its name MUST be partition 0's and its sub-partition number \(k\); partition 0 MUST NOT be detached; partition 0 and a detached partition MUST have no dependencies, and every other partition exactly \(k - 1\) (FORMAT).
  2. Its file. Its length MUST be the manifest's (FORMAT) and its SHA-256 the file hash (DIGEST). Open the container with the default limits and ten sections: kind 6 (FORMAT at 56), all ten sections present (FORMAT at 57). Inflate the compressed sections (partitions).
  3. Its rows. Read section 5 and recompute the Merkle tree hash of its digests: it MUST equal the directory's root and the manifest's (DIGEST). Compare each DAG block's digest (DIGEST), and decode the blocks (ST9.4) to rows numbered from \(B_k\), the rows the earlier partitions of the chain stored, checking every reference's kind against the tags of the rows it names. Their number MUST be the manifest's row count (FORMAT).
  4. Its export table. Read section 10 against the rows: every proof row is a PROOF row of the chain (FORMAT).
  5. Commit. Resolve section 1 among the core, the profile and the declarations already loaded, and commit the rows into the bank in row order, with one writer (ST9.8, step 6):

    1. a REF row maps to its section-1 declaration (MISSING_DEPENDENCY when there is none);
    2. a DECL row MUST name the next section-2 position; its declaration's bytes MUST have the stated identity (IDENTITY), are rebuilt in the bank, and are declared with its export-table entry's proof row, which MUST precede it;
    3. a PART row maps to the head its declaration registered;
    4. a LOCAL row MUST have a name and MUST be followed by its fix frame, a CONTEXT row of entry kind 1 whose local is that row, and the partition MUST NOT end with a LOCAL row (FORMAT, checked before any row is committed). The local's parent is the frame's parent. The two are committed together; a binding site that the bank held before the chain's first partition (the profile's, or in a whole load an earlier chain's) maps to its local and frame, and one that an earlier row of the same chain committed is refused (FORMAT);
    5. a CONTEXT row with an entry (a fix or an assume frame) takes its parent's theory, so its theory field MUST name that theory (FORMAT); its sorts set is recomputed (ST9.7);
    6. every other row is interned, which recomputes its structural hashes, bounds and flags; a row equal to one committed earlier in this load, by this partition or an earlier one of its chain, is refused (FORMAT), and one equal to a row the bank held before maps to it.

    In a detached chain, section 1 MUST list exactly the declarations its REF rows name (FORMAT).

4. Check and compare

  1. Proof citations. Recompute the proof citations of every loaded theorem (manifest section 7) and compare them with the manifest's (FORMAT, naming the first differing declaration).
  2. Check every row. Decide every theory, then check every committed row in parallel (KC1.12). Every row MUST be verified (LIBRARY otherwise). The check of a theorem reads its proof row as its judgment and requires the canonical closure \(K\) of that row's sequent (Definition Q-3d) to be its statement.
  3. Partition sections. From the bank, recompute each partition's sections 1 to 4, 6, 7, 9 and 10 and its declaration, provider and stored-proof counts; each MUST equal what was read, and its record count MUST be 0 (FORMAT). Section 4's classes MUST be known (FORMAT).
  4. Rule L1. Recompute the statement identity of every provider the load declared; two providers of one statement are refused (FORMAT), unless the manifest's section 8 records the earlier one as superseded.
  5. Aliases. Each alias whose provider the load declared, the library's or the profile's, MUST give one nonempty name to each type variable of its statement, in key order, and no name to two variables of one sort (FORMAT, naming the alias).
  6. The manifest (whole loads). The providers the load declared and the profile's, with their kinds, partitions, provider references and reuse counts, less the statements a version superseded or withdrew, MUST be exactly section 3 (FORMAT). The declaration index the load computes MUST be exactly section 7, and every theorem declared again MUST have its replaced-proof entry in section 8 (FORMAT).

The result is the bank, holding the core, the profile and every row of the version, and the version's declarations in load order. Library.snapshot seals it as a snapshot that a session opens over; Library.recheck reports what it verified.

What a load establishes

Theorem L-6. If a whole load succeeds:

  1. every declaration was rebuilt and declared through the checker's rules, every row was checked by the checker, and every theorem's statement is the canonical closure of its proof row's sequent, so it is derivable from the row's judgment; nothing else was declared;
  2. if no two distinct byte strings involved have one SHA-256, no two providers state one statement up to renaming its type variables (for statements with at most six type variables);
  3. a failure changes nothing outside the fresh bank.

The proof is on the theory page. In implementer terms:

  • Soundness does not depend on hashes. Steps 10 and 12 make every declaration and every row through the bank's interning and the checker's own checks, whatever the identities and digests say (ground rule SG1). A SHA-256 collision could at worst bind a name to an unintended declaration, which the checker would still check.
  • Identities make the library one whole. Step 10 resolves references only to declarations already loaded, by identity; steps 14 and 16 make section 3 exactly the declared providers, and its strict order makes their statement identities distinct.
  • A parser establishes none of this. Steps 1 to 9 need no checker and validate a library's structure; only a loader that also makes the checks of steps 10 to 16 may treat its theorems as proved.

A statement-only import (Library.declare, through a session's import_theorem) is not a load: it reads only section 2 of the partitions that hold the wanted statements' closure, declares a theorem as an axiom stand-in, and checks the imported declarations but not their proofs. The library version's producer is then in the importer's trusted base for those statements (ST9.8).

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