Binary library wire format

Identities

A library names everything by content. A declaration's identity is the hash of its canonical bytes; a statement's statement identity is the hash of its key, so statements that differ only in the names of their type variables have one statement identity. Neither depends on a file, a kernel handle, or the order in which a kernel built things. The theory is on Identity across kernels (Definitions I-2, I-5, I-6, Propositions I-1, I-5, I-6).

Declaration identities

\[ \operatorname{id}(d) = \mathrm{SHA\text{-}256}(\mathtt{judgment/decl/1} \,\|\, \mathtt{00} \,\|\, \operatorname{ser}(d)) \]

where \(\operatorname{ser}(d)\) is the declaration bytes, tag included, and the prefix is the 15 ASCII bytes judgment/decl/1. A reader recomputes the identity of every declaration it reads and refuses one that differs from the identity recorded beside it (IDENTITY). Because a declaration's bytes contain the identities of the declarations it cites, identities form a Merkle DAG: changing a declaration changes the identity of everything that cites it.

Core identities

The core is never serialized; a core declaration's identity is the hash of its name:

\[ \operatorname{id}(\mathit{name}) = \mathrm{SHA\text{-}256}(\mathtt{judgment/core/1} \,\|\, \mathtt{00} \,\|\, \mathit{name}) \]
Name Identity
core.Prop d05ab1d0e3186c34ebc2d47be462bd13c0e5fdb4591770976d3895d0497629d9
core.True 4318ec595154d9eb47249c82a1ae14f7b5e8b58a45c4d551482b96a14bba76e9
core.and 139b1c61a4d4882bd2363ecc8670b34dbf0a6c0230396b8285c937efec821ddf
core.imp 54cd4299f779211d3841e64883f3f178fb9af9883e7ad338264060a9390b5608

tests/store/reference/identity_vectors.txt lists the identities of the whole core. A core identity always has part 0. It is never an import, a section-2 declaration, or a support entry of a partition.

Statement keys

A theorem's type variables are implicitly universally quantified, so two statements that differ by renaming type variables (keeping their sorts) state the same theorem. The key picks one representative:

  1. List the statement's type variables \(\alpha_1, \dots, \alpha_n\) in order of first occurrence (Definition S-3), with their sorts \(S_1, \dots, S_n\).
  2. For a permutation \(\pi\) of \(1, \dots, n\), rename \(\alpha_{\pi(i)}\) to the type variable named ?\(i\) (the ASCII ? and the decimal digits of \(i\), with no leading zero) with the sort \(S_{\pi(i)}\), and take the canonical form of the result: \(\varphi_\pi\).
  3. With \(n \le 6\), the key is the least \(\varphi_\pi\) over all \(n!\) permutations in the term bank's structural order (termbank T1.6). With \(n > 6\) it is \(\varphi_{\mathrm{id}}\), the first-occurrence renaming.

The key's order lists \(\alpha_{\pi(1)}, \dots, \alpha_{\pi(n)}\): the statement's own variables in the positions of ?0, ?1, …. With no type variables the key is the statement itself.

What an implementation needs for keys

For a statement without type variables nothing is computed: the key is the canonical statement as it was read. For one with type variables, a writer or loader needs the kernel's canonical form (ACU arguments are sorted by the structural order, so renaming can reorder them) and the structural order itself, which compares structural hashes first (termbank T1.2 and T1.6). These are part of the termbank format, version 2. A parser or a validating reader needs neither: it reads keys, it does not compute them.

Theorems stored by their key

A partition stores a theorem with one to six type variables as its key, and the writer's bank holds it so: the writer publishes such a theorem only when its declaration states its key, with the key's own type parameters, and refuses it otherwise (LIBRARY). The construction engine declares a theorem stating the canonical closure of its proof's sequent (Definition Q-3d), which with one to six type variables is its own key (Theorem Q-3e), with the key's type variables in first-occurrence order as its parameters. A loader declares the theorem as written; it does not check that a theorem with type parameters is in key form, since the identity covers the bytes as written (ST9.8). Section 7 lists, for each theorem stored by its key, the names its source gave the key's variables, in key order; a loader recomputes the list and refuses one that differs (FORMAT).

Statement identities

\[ \operatorname{sid}(\varphi) = \mathrm{SHA\text{-}256}(\mathtt{judgment/stmt/1} \,\|\, \mathtt{00} \,\|\, \operatorname{ser}(\kappa(\varphi))) \]

\(\kappa(\varphi)\) is the key, taken in the theory that declares the statement, and \(\operatorname{ser}(\kappa(\varphi))\) is the serialization of an object with one root and no fields: the reference table and then the node table, whose last entry is the root. No root position follows. The key depends on that theory's canonical signature: a statement about an operator keyed before and after the operator is declared ACU has two canonical forms and two identities.

The statements a declaration provides (Definition L-1), by part:

Declaration Part Statement
theorem 0 its statement
axiom 0 its proposition
definition, instance definition 0 its defining equation \(c[\bar\alpha] = \mathit{body}\), as the kernel's use states it at the declaration's own type parameters (for an instance definition, the type variables of its type arguments in first-occurrence order)
type definition 3 and 4 its two theorems
specification \(k + 1\) its theorem
every other form — none
the core — none

A theorem's and an axiom's statement is in its bytes. The other statements are produced by the kernel from the declaration (the canonical form of the equation, the type definition's and specification's theorems), so a parser without a kernel can recompute section 7 for theorems and axioms, and for the rest only read what section 7 records.

Partition section 7 lists these, with their statement identities; a loader recomputes each one and refuses a section 7 that differs.

Example

example.p has the identity b6b07971…202f. The statement p, the constant with no arguments, keys to itself, and its serialization is

01 00 00 00   b6 b0 79 71 … 20 2f   00 00 00 00     one reference: example.p, part 0
01 00 00 00   12  00 00 00 00  00 00 00 00  00 00 00 00   one node: CONST reference 0, no type or term arguments

so \(\operatorname{sid}(p)\) is the SHA-256 of judgment/stmt/1, 00, and those 57 bytes: abbdd280de67bc5c811eb19b2e21974d2a191839ee143ec330ca1be3ab78005b.

Canonical names

In a library, a theorem or axiom that provides a statement is named after its statement identity:

judgment.stmt.<64 lowercase hexadecimal digits of the statement identity>

A theorem whose statement an earlier provider of the library already states is not published (Rule L1): the writer leaves it out, and what cites it cites the provider. Providers of other kinds keep their names: a definition's equation is named by the definition, and the profile's names are its own. The name is in the declaration's bytes, so it is part of the declaration's identity; with at most six type variables, a theorem's or axiom's identity therefore depends only on its statement's key. An axiom with one to six type variables MUST be declared stating its key, since it is written as declared.

The names a source or a user gives theorems are kept as aliases (manifest section 4).

Statements with more than six type variables

With more than six type variables the key is the first-occurrence renaming, which canonical sorting of ACU arguments can make depend on names. Two renamings of such a statement can then have two statement identities, and a library may hold both. It never gives two different statements one identity. Such theorems are written as declared, not by key.

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