Content identity¶
Every checked declaration has a content hash: a digest of what the declaration means, with everything that only names, locates, or lays it out factored away. Two declarations that differ only by a consistent renaming, by the order of the module's declarations, by comments, or by formatting have the same hash; any change to a constant, a constructor, a rule, a lexical entry, or any other part of their meaning changes it. Fits, query results, and stored runs are addressed by these hashes, so a refactor that leaves the meaning unchanged reuses every artifact computed before it.
This page defines the hash: what enters it, what does not, and why the result is sound. The
implementation is quivers.qiec.content_hash; qvr hash
(CLI) prints it.
The core form¶
A declaration is hashed from its core form, a canonical JSON document derived from its checked
QIEC record, the same record the stable wire format
qiec-json serializes. The core form keeps every field of the
record except the ones listed below, and rewrites names as follows.
Local binders become de Bruijn levels. A value binder (a computation or handler parameter, a
bind or let binder, a comprehension index, a case-branch field, a handler clause parameter)
keeps only its type. An occurrence of a local records the level of the innermost binder of its
name. The checker scopes binders exactly this way, so the level identifies the binder, and
alpha-equivalent declarations
have equal core forms.
Static names become ordinals. The names of type, index, and effect binders take the ordinal of their first occurrence. The kernel refuses a shadowed static binder within a declaration, so a static name denotes one binder throughout the declaration and the ordinal is unambiguous. A program entry's domain parameters are static names of the entry too: the value a program is applied to reaches them by position, so their spelling is not part of the entry's interface, and two scan cells whose parameters share a spelling share nothing.
Names the core resolves by spelling become free-name ordinals. Site labels, program
parameter names, plate axes, returned names, and the constructors of a user index sort are names
the core matches by spelling. A name of this kind that the declaration does not bind takes the
ordinal of its first occurrence among the declaration's free names. The list of free names, in
ordinal order, is the declaration's free-name interface. A scan cell's data inputs are such names,
and the apply step running the scan binds each to a name of the scanning program where that name
is in scope (apply run_all [W = weights]). The scanning program's own bindings stay bound
variables, so renaming one together with the binding that names it keeps every hash, and binding a
label to another value changes them.
References become hashes. A reference to another declaration of the module (a computation, a family or one of its constructors, an effect or one of its operations, a handler, a custom distribution family) becomes that declaration's hash, the position of the constructor or operation within it, and the substitution of the referring declaration's ordinals for the target's free names. The correspondence between names in two declarations is therefore part of the referring declaration's identity, while their spelling is not. A reference to a builtin (a primitive, a builtin type, effect, or distribution family) keeps the builtin's identifier, which is the same in every module.
Generative identities become free-name ordinals. An effect instance is generative: two instances of one interface are two distinct resources even though their declarations agree. An instance's identity therefore enters a referring declaration as a free name, never as the hash of its declaration. Identifiers the kernel derives from source paths (sites, static scopes, rigid static variables, row variables) become ordinals local to the declaration, which records which of its positions share an identity without recording the paths.
Sets are sorted by content. The kernel stores an effect row's entries, and a row variable's
lacks list, in the order of their identifiers. That order derives from names, so the core form
sorts these elements by their own core forms instead, after every name outside them has taken its
ordinal from the declaration's ordered structure.
Metadata is dropped. Declaration names, the display names a reference carries beside its identifier, a type constructor's copy of its telescope, constructor field and operation argument names (which the core addresses by position), the module name, the source protocol, the program gap diagnostic, and file positions do not enter the core form. Comments, blank lines, and layout never reach QIEC.
What stays is content: literals, operators, primitive names, distribution parameter keywords,
types, effect rows, handler coverage and resumption grades, foreign semantics, and every string
literal, including the parameter keys a deduction reads its learned weights by and the strings a
generated show computation prints.
Structure declarations¶
A module's objects, programs, deductions, relations, losses, encoders, decoders, contractions,
categories, rules, schemas, bundles, syntax declarations, compositions, judgments, defines,
morphisms, and exports are hashed declarations too: the checked module records each as a
structure declaration, whose kind is the
declaration's kind (object, program, deduction, relation, loss, ...). Its core form is
the core form of its structure term, under the same rules:
- a name the declaration binds (a deduction's atoms, rule names, and schematic variables, a rule's variables, a program's parameters and the names its steps bind, the variables of an encoder's or decoder's rules, a syntax constructor's bound variables and the argument labels their annotations name) is a static name and takes the ordinal of its first occurrence;
- a free variable of a program is one of its data names, a free name like a program parameter of its entry;
- a reference to another structure declaration is that declaration's hash, as for any reference;
- every other string (a string literal, a family or semiring name, an operator, an option key) and every number is content;
- the declaration's own name, positions, and documentation are metadata.
A structure declaration that the core also elaborates is hashed by its elaboration. A program's
parameters, static binders, steps, and returns are elaborated into its entry and the computation
the entry names, so the core form of a program structure is a reference to that entry together
with the term's other fields (its domain, codomain, and options); the elaborated fields stay in the
checked term, where the runtime decodes them, and are metadata of the hash. Two spellings that
elaborate alike (a type written Real[Fin Row] or (Real)[Fin Row], keyword arguments in either
order) therefore give one hash, while any change of what the program does changes its entry and so
its structure. A program the lowering could not elaborate has no entry, and its whole term is its
content.
The size of an object, the domain and codomain of a program or deduction, a rule's
#[learnable], an arc's labels and weight, a relation's states, and a loss's weight are all
content, so editing any of them changes the hash of the declaration that states it and of every
declaration that refers to it.
Some kinds of structure declaration are identified by name as well as content, and each has a reason:
| Kind | Why its name is content |
|---|---|
| category | an atom of a categorial calculus, distinct from every other atom by its name alone |
| morphism, encoder, decoder, relation | owns learned parameters that the compiled runtime and its fit state key by the declaration's name, outside the checked core, where no other part of a fit's key sees a rename |
| theory | its name is its identity of origin, so two theories with one presentation stay apart in a colimit |
For these nominal kinds the name is content, so two such declarations with equal content remain distinct and renaming one changes its hash. Every other structure declaration is identified by content: two objects of the same carrier are one object.
A deduction is not nominal, although it owns learned parameters. The checked core reads them with
Param.get at a ParameterName: the deduction's name as the
owner and the production's member path (lex.0, rule.app:), both names the core resolves by
spelling, so they become ordinals like site labels. Renaming the deduction, or one of its rules,
therefore changes no hash, while the spellings stay among the free names of every entry that reads
them, which a fit's key records (see what a fit is keyed by): a renamed
grammar reuses no stored fit whose parameters it would read under other names. Two deductions that
differ in a rule, a lexical entry, or an option differ in content and hash differently; two that
differ only in their names hash alike, as two equal computations do.
An export has no name. Its position among the module's exports is content, since the first export is the module's primary entry: reordering exports changes the module digest.
Typed holes¶
A declaration with typed holes has a content hash, so incremental checking and
qvr hash work on incomplete modules. A hole's name relates its occurrences and is what a filling
is keyed by, so it is a name in the sense of the core form: it becomes the ordinal of its first
occurrence. Renaming a hole consistently keeps the hash, two distinct holes in one declaration stay
distinct, and the hole's goal type is content. Its origin becomes a token local to the
declaration, as every source position does. A hole is its own form, never equal to a value, so a
declaration with a hole never shares a hash with a complete declaration, and filling a hole changes
the hash. A type hole is an opaque family with no constructors; its name is metadata, as every
family's is, so two type holes in different places hash alike, and a reference to one is a
reference to that family.
Content identity does not make a holed module exportable: the stable wire form refuses holes, and a fit or an experiment of a holed module is refused before any hash is recorded.
Components¶
A declaration that refers to itself, directly or through others, is hashed together with every
declaration in its strongly connected component. The members of a component are put in a
canonical order before hashing: each member is first colored by the hash of its core form with
references into the component erased, the coloring is refined by the colors of the members each
member refers to until no class splits, and any members the refinement cannot tell apart are
individualized one at a time, keeping the order whose serialization is least. This is a canonical
labeling: the order depends on the component's structure alone, never on the order of the
declarations in the source. Member n of a component with hash h has the hash h.n, as in
Unison.
Digests and domain separation¶
Every digest is BLAKE3, the hash function panproto addresses its objects with, of the bytes
where the kernel ABI is QIEC_ABI and the kind separates
components, modules, subterms, and every other use. The domain is the core format, never the
source format: a change of surface syntax that elaborates to the same core leaves every hash
unchanged. The canonical JSON is compact and key-sorted, and a float is written in the shortest
form that reads back to the same double, so the digest is the same on every run and platform.
The module digest is the digest of the multiset of the module's declaration kinds and hashes. It is invariant under everything the declaration hashes are invariant under.
Subterm hashes¶
alpha_hash hashes a single
term modulo alpha-equivalence, and
alpha_hashes hashes every
subterm of a term in one bottom-up pass, with the e-summaries of Maziarz, Ellis, Lawrence,
Fitzgibbon, and Peyton Jones (2021). The summary of a
subterm is its structure, in which locals are anonymous and each binder carries the position tree
of the local it binds, together with a map from each free local to the position tree of its
occurrences. Two subterms have equal hashes exactly when they are alpha-equivalent as open terms
with the same free locals, wherever they occur, so a normalizer can memoize by subterm hash and
HashConsTable can share
alpha-equivalent subterms.
Source identity¶
A program also has a source identity, recorded separately from its core hashes and keyed by the
panproto commit of the grammar its source is written in
(identify): the hash of
its exact bytes, for exact-text audits, and an alpha-normalized hash of each top-level statement.
A statement's source hash drops comments, blank lines, indentation width, spacing, and number
spelling; turns the names it declares into their positions, local binders and the references they
bind into ordinals, and references to other statements into those statements' source hashes; and
keeps every other token as written.
The source identity covers what the checked core does not yet record: a relation's arcs, a loss, the declared type of a program or deduction, whether a deduction rule's weight is learned. It is a function of the surface syntax, so a migration may change it; that is why it is keyed by the grammar commit and never mixed into the core hashes.
The migration invariant¶
A syntactic migration hop must preserve the core hash of every declaration it rewrites. A hop
that may change meaning declares so (in SEMANTIC_HOPS, or in the semantic field of a feature
of the release hop; see Migrating source). The gate
(check_migration_invariance) runs every hop of the
chain, and every feature of the release hop on its own, over the whole corpus: each corpus file
that is valid source at a hop's revision is migrated from there to the current grammar, and its
core hashes must equal the file's own. A hop that rewrites a construct it should have left alone,
renames inconsistently, or changes a constant fails the gate even when every parse gate passes.
What a fit is keyed by¶
A fit (FitCache) is keyed by the core hash of its entry point and
of the interpretation bindings it runs with, the digest of the source's statement hashes, the
entry's free names (its site labels, the names of the parameters a run supplies by name, and
returned names, which key the values of the stored result), the data by parameter position, the
fixed sites by ordinal, the inference method and every option of the fit, the seeds, and the
engine identity (the quivers version, the kernel ABI, the hash format, the torch and Python
versions, and the platform). Renaming a computation, a family, a handler, a local, a domain
parameter, or the program itself, reordering declarations, and
editing comments or formatting leave the key unchanged, and the stored fit is returned exactly as
it was saved. A fit that has no content identity (one using native family implementations, a
programmatic runtime provider, or a method configured without a seed) is refused rather than
cached.
Incremental compilation¶
ModuleBuild runs compilation as memoized queries with
red-green invalidation and early cutoff, in the sense of
Salsa and of rustc's incremental compilation, a
constructive-trace build system in the terminology of Mokhov, Mitchell, and Peyton Jones
(2018). Each module is checked against the interfaces of the
modules it imports, never their bodies, and an interface's fingerprint is its digest, so editing a
private body re-runs that module's check and stops at its interface: no dependent is checked
again. The flat program's elaboration into checked QIEC, including the deductions compiled into
it, persists in the project's cache, so a later process reuses it
for an unchanged program. A computation's static specialization (its telescope instantiated at closed
arguments) is a query too, cut off when re-elaboration leaves it unchanged.
Limits¶
- A string literal is content even when it spells a name, so renaming a deduction changes the hashes of the computations that read its learned weights by key.
- A deduction's body (its atoms, rules, and lexical entries) is a language of labels to the source-level name analysis, so renaming a rule or an atom changes the deduction's statement source hash while leaving its core hashes unchanged.