Binary library wire format

Declarations and terms

Declarations and statements write their types and terms the same way: a reference table of the declarations they cite, a node table of the distinct nodes of their terms and types, and then their own fields, which cite declarations by reference-table position and terms and types by node-table position. This page gives the bytes; the definition and its proofs (sharing is kept, the size is linear, the order is canonical) are Definition I-1 and ST1.4.

All integers on this page are u32 and all strings are u32-length UTF-8 (primitives). A position is a u32 index into a table, from 0.

The logic

The bytes encode the higher-order logic of the judgment kernel (the logic): simple types with type constructors, sorted type variables (a sort is a set of classes), and the function type; terms with constants applied to type and term arguments, bound variables as de Bruijn indices, λ and let, and literals. The 15 declarations of the core exist in every kernel and are never serialized:

Core name Kind
core.Prop type constructor, arity 0 propositions
core.True, core.False constants Prop
core.and, core.or, core.imp constants Prop → Prop → Prop
core.forall constant (α → Prop) → Prop
core.eq constant α → α → Prop the kernel's equality
core.not, core.iff, core.exists transparent definitions
core.propext axiom propositional extensionality
core.itself type constructor, arity 1 properties of types
core.TYPE constant itself(α)
core.arrow the function type constructor written as ARROW nodes, never as TCON

A core declaration's identity is computed from its name (identities).

References

A reference names a declaration:

Field Encoding
identity 32 bytes: the identity of the declaration's block (identities)
part u32: 0 for the block itself, \(i \ge 1\) for its \(i\)-th part

Some declaration forms make several declarations at once, a block with parts. Parts are never serialized on their own; they are cited by number:

Block Parts
type definition of \(\kappa\) (tag 39) 1 the abs constant, 2 the rep constant, 3 the theorem \(\forall a.\ \mathsf{abs}(\mathsf{rep}\ a) = a\), 4 the theorem \(\forall r.\ P\ r = (\mathsf{rep}(\mathsf{abs}\ r) = r)\)
specification of \(c_1, \dots, c_k\) (tag 40) 1 to \(k\) the constants, in the order of the names field; \(k + 1\) its theorem
class \(c\) (tag 44) 1 its predicate \(\mathsf{of\_class}_c\)
every other form none: only part 0 exists

The type constructor of a type definition is part 0 itself.

The reference table

reference table := u32 count, then count × (identity[32], u32 part)

The table lists each distinct reference the object cites, once, in the order in which it is first cited when the bytes after the table are read from left to right: the node table first, then the object's fields. Within a node, a TVAR cites its classes in order, and TCON, CONST, and LIT cite their declaration first. A reader of a declaration can check the order by re-serializing.

The node table

node table := u32 count, then count × node
node       := u8 tag, then the tag's fields
Tag Name Fields Category
1 TVAR name (string); list of reference positions: the classes of its sort, in the kernel's order (an empty list for the empty sort) type
2 TCON reference position (a type constructor or type definition); list of node positions (the type arguments) type
3 ARROW node position (domain type), node position (codomain type) type
16 BVAR u32 de Bruijn index term
17 LOCAL reserved: a declaration's terms are closed, and a reader refuses the tag term
18 CONST reference position (a constant, definition, or overloaded constant); list of node positions (its type arguments, exactly as many as the constant has type parameters); list of node positions (its term arguments, possibly empty) term
19 APP node position (the head, a term that is not a constant); list of node positions (the arguments, at least one) term
20 LAM node position (the binder's type), node position (the body, a term) term
21 LET node position (the bound value's type), node position (the value, a term), node position (the body, a term) term
22 LIT reference position (a literal kind); blob: the kernel's encoding of the value term

A constant applied to arguments is one CONST node, not an APP of a CONST: the kernel's canonical form folds the application into the constant's node. A function type is always an ARROW; a TCON naming core.arrow is non-canonical and refused.

Order. The node table is a post-order traversal of the object's roots, each distinct node listed once, when the traversal first finishes it. The roots are the object's terms and types in the order of its fields. A node's children are visited in field order: a CONST's type arguments before its term arguments, a binder's type before its body; a TVAR's classes are references, not children. So every child precedes its parent, and two equal subterms are one entry. A node's fields name only earlier entries.

Reader checks (FORMAT at the field's first byte unless stated):

  1. every node position is smaller than the position of the entry that holds it, and every reference position is smaller than the reference count;
  2. a field of category type names a type node (tags 1–3) and a field of category term a term node (tags 16–22);
  3. an unknown tag is refused at the tag byte;
  4. a LOCAL (tag 17) is refused: a declaration's terms are closed;
  5. names are well-formed UTF-8 (primitives);
  6. a loader then rebuilds the nodes in its bank, declares the declaration, serializes the declared row again, and requires exact byte equality, which refuses unused or duplicate entries, other table orders, and syntax that is not in the canonical form (an ACU operator's arguments in another order, for example). A mismatch is refused (FORMAT): the loader compares the partition's whole section 2 with the serialization of what it loaded (loading).

Declarations

declaration := u8 tag, reference table, node table, fields

The tag is 32 plus the kernel's DeclKind code. The node table's roots are the declaration's types and terms in field order. In the fields, type and term mean a node position, ref a reference position, and params a list of node positions: the declaration's type parameters, the type variables of its type or statement in order of first occurrence (Definition S-3), as the kernel stores them.

Tag Form Fields, in order
32 type constructor name; u32 arity
33 constant name; type; params
34 definition name; type; body (term); u8 transparent (0 or 1); params
35 axiom name; proposition (term); params
36 theorem (Proven) name; statement (term); params — see below
37 ACU operator ref (the constant); list of types (its type arguments); optional unit (term); ref (associativity law); ref (commutativity law); optional ref (unit law)
38 literal kind name; u8 implementation; carrier (type); optional domain (term); list of (u8 operation, term); list of (u8 law, ref); optional ref (base kind)
39 type definition name; abs name; rep name; list of types (its parameters); predicate (term); ref (witness)
40 specification list of names; ref (witness)
42 overloaded constant name; type; params
43 instance definition ref (the overloaded constant); list of types (the instance's type arguments); body (term)
44 class name; list of refs (superclasses); list of refs (parameters: overloaded constants); axiom (term)
45 instance ref (type constructor); list of argument sorts, each a list of refs (classes); ref (class); ref (witness)
46 class relation ref (subclass); ref (superclass); ref (witness)

Minimum item sizes for these lists: 4 bytes for a list of types, refs, or strings, or of sorts (each an empty class list at least); 5 for a list of (u8, term) or (u8, ref).

Tag 41 (a fact) is never written: facts are parts of type definitions and specifications. Only the forms listed with params write that list.

Theorems. A theorem's bytes hold its statement, never its proof. A theorem with one to six type variables is written by its key: the statement after renaming its type variables to ?0, ?1, … (identities), with the params of the key, so its identity does not depend on the names its source used. Such a theorem writes the key in the place of the statement; the bytes are otherwise those of an axiom's fields under tag 36. The names its source used are kept beside it, in partition section 7 (partitions). A theorem with no type variables, or more than six, is written as declared. Its proof is the PROOF row that the partition's export table names (partitions).

Codes.

Field Codes
literal implementation 1 exact integers, 2 exact rationals, 3 formal polynomials
literal operation 0 ZERO, 1 ONE, 2 ADD, 3 MUL, 4 NEG, 5 INV, 6 POW, 7 EXPONENT, 8 LT, 9 LE, 10 INDET, 11 COEFF
literal law R1–R7 are 0–6, O1–O7 are 7–13, F1–F2 are 14–15, E1–E4 are 16–19, P1–P4 are 20–23, Q1–Q8 are 24–31
DeclKind 0 type constructor, 1 constant, 2 definition, 3 axiom, 4 theorem, 5 ACU operator, 6 literal kind, 7 type definition, 8 specification, 9 fact, 10 overloaded constant, 11 instance definition, 12 class, 13 instance, 14 class relation

The binding and law lists of a literal kind are in the order the kernel's declaration readback gives. The meanings of the forms, and the obligations the kernel checks for each, are in the kernel specification: K1.3 for the basic forms and the theory mechanisms K2.2 and K2.6 to K2.9 for the others.

Example: example.p : Prop

The constant example.p of type Prop:

21                                   tag 33: constant
01 00 00 00                          1 reference
d0 5a b1 d0 … 29 d9  00 00 00 00     core.Prop, part 0
01 00 00 00                          1 node
02  00 00 00 00  00 00 00 00         node 0: TCON reference 0, no arguments
09 00 00 00  65 78 61 6d 70 6c 65 2e 70     name "example.p"
00 00 00 00                          type: node 0
00 00 00 00                          params: none

d05ab1d0…29d9 is the core identity of core.Prop. The declaration's identity, b6b07971…202f, is the SHA-256 of judgment/decl/1, a zero byte, and these 75 bytes (identities).

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