Content hashes¶
Alpha-invariant content hashes of checked declarations, the shared digest and canonical serialization every content identity is built from, and subterm hashing modulo alpha-equivalence. What a hash covers is defined in Content identity.
content_hash
¶
Content identity of checked QIEC declarations.
Every declaration of a checked QiecModule
has a content hash: a BLAKE3 digest of its core form, which is its stable
wire encoding (the language qiec-json admits)
after alpha-normalization. The core form keeps everything that determines
what a declaration means and drops everything that only names or locates it:
- Local binders become de Bruijn levels. A value binder (a computation or
handler parameter, a
bind,let, comprehension, or case-branch binder) records only its type, and each occurrence of the local records the level of its binder. Checked QIEC never rebinds a local within a body, so a level is unambiguous. - Names become ordinals. A static binder name (a telescope binder) and every name the core resolves by spelling (a site label, a plate axis, a program parameter, an index-sort constructor, the owner and member path of a learned parameter's name) becomes the ordinal of its first occurrence. Static names bound inside the declaration take ordinals in one space; names free in the declaration take ordinals in a second space, which is the declaration's free-name interface.
- 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 together with the substitution of the caller's ordinals for the callee's free names, so the correspondence of names across declarations is part of the caller's identity while their spelling is not.
- Generative identities become ordinals. An effect instance is generative: two instances of one interface are distinct even though their declarations agree. Instance identities are therefore free names, not content.
- Structure declarations are declarations. Each
StructureDecl(an object, a program, a deduction, a relation, a loss, ...) is hashed like any other declaration: the names its term binds become ordinals, a reference to another structure becomes that structure's hash, and every other string and number is content. A structure the core also elaborates (a program, whose parameters, steps, and returns are its entry) is hashed by a reference to that elaboration and its remaining fields, so how the elaborated fields are spelled does not enter its hash. - Names, comments, and positions are metadata. Declaration names, display names that duplicate an identity, source protocols, and file positions are dropped. Comments and layout never reach QIEC.
Mutually recursive declarations are hashed as one strongly connected
component. Its members are ordered canonically: first by the hash each member
has with references into the component erased, then by iterated refinement
over the component's reference structure, with any remaining symmetry broken
by trying each candidate and keeping the least serialization. Member n of a
component with hash h has the hash h.n.
Every digest is domain-separated by CONTENT_HASH_FORMAT
and the kernel ABI QIEC_ABI, never by
the source format, so a change of source syntax that elaborates to the same
core leaves every hash unchanged.
alpha_hash and
alpha_hashes hash individual terms
modulo alpha-equivalence with the algorithm of Maziarz, Ellis, Lawrence,
Fitzgibbon, and Peyton Jones (2021),
so a normalizer can memoize by subterm; HashConsTable
shares alpha-equivalent subterms.
CONTENT_HASH_FORMAT
module-attribute
¶
The domain tag every content digest is computed under, beside the kernel ABI.
CoreJson
¶
A core form: JSON data as json.dumps serializes it.
QiecDatum
¶
QiecDatum = None | bool | int | float | str | bytes | Enum | StableId | Model | tuple[QiecDatum, ...] | Mapping[str, QiecDatum]
A value of the stable QIEC graph: a record, an identifier, an enum, a scalar, or a tuple or string-keyed mapping of those.
DeclarationKind
¶
DeclarationKind = Literal['family', 'effect', 'handler', 'computation', 'entry', 'distribution', 'instance', 'index-sort', 'object', 'morphism', 'define', 'export', 'program', 'deduction', 'syntax', 'encoder', 'decoder', 'loss', 'category', 'rule', 'schema', 'bundle', 'contraction', 'composition', 'judgment', 'relation', 'relation-composition', 'relation-transform', 'string-graph', 'theory', 'view', 'model', 'semiring']
What a hashed declaration is: a kernel declaration, or a structure
declaration of one of the kinds in
STRUCTURE_KINDS.
ContentHashError
¶
Bases: ValueError
A value has no core form, so it cannot be content addressed.
DeclarationHash
didactic-model
¶
DeclarationHash(*, kind: DeclarationKind, name: str, hash: str, component: str, member: int, size: int, names: tuple[str, ...] = ())
Bases: Model
The content identity of one declaration of a module.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
What the declaration is.
TYPE:
|
name
|
Its name in the module. A name is metadata: it does not enter the hash.
TYPE:
|
hash
|
The declaration's content hash: the hash of its component when the
component has one member, else
TYPE:
|
component
|
The hash of the strongly connected component holding the declaration.
TYPE:
|
member
|
The declaration's position among the component's canonically ordered members.
TYPE:
|
size
|
How many declarations the component has.
TYPE:
|
names
|
The declaration's free names in ordinal order: the spellings the core resolves by name that the declaration does not bind (site labels, program parameters, plate axes, returned names). They are metadata of the hash but the observable vocabulary of the declaration, which is why a stored run is keyed by them as well as by the hash.
TYPE:
|
ModuleHashes
didactic-model
¶
ModuleHashes(*, module: str, declarations: tuple[DeclarationHash, ...] = ())
Bases: Model
The content identities of a checked module.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The module digest: the digest of the multiset of its declarations' kinds and hashes. It is invariant under renaming and reordering, like every declaration hash.
TYPE:
|
declarations
|
One identity per declaration, in module order.
TYPE:
|
declaration
¶
declaration(kind: DeclarationKind, name: str) -> DeclarationHash
Look up one declaration's identity.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The declaration's kind.
TYPE:
|
name
|
The declaration's name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DeclarationHash
|
The identity. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the module declares no such declaration. |
Source code in src/quivers/qiec/content_hash.py
hashes
¶
hashes(kind: DeclarationKind) -> dict[str, str]
The hash of every declaration of one kind, by name.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The kind to select.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, str]
|
Each declaration's name mapped to its hash. |
Source code in src/quivers/qiec/content_hash.py
ModuleContent
¶
ModuleContent(module: QiecModule)
The hashed declarations of one checked module.
Hashing a module walks every component once; this object keeps the result, so a caller that needs the module's hashes, a declaration's normal form, and the hash of a value that refers to the module pays for one walk.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The checked module, as the lowering returns it.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ContentHashError
|
If the module holds a value with no core form. |
Source code in src/quivers/qiec/content_hash.py
hashes
instance-attribute
¶
hashes = ModuleHashes(module=digest, declarations=tuple(identities))
The module digest and one identity per declaration.
normal_form
¶
normal_form(kind: DeclarationKind, name: str) -> str
The canonical serialization a declaration's component is hashed from.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
The declaration's kind.
TYPE:
|
name
|
The declaration's name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The JSON text of the component's core forms in canonical member order, which is what its hash digests. Useful for seeing why two declarations hash apart. |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the module has no such declaration. |
Source code in src/quivers/qiec/content_hash.py
value_hash
¶
value_hash(kind: str, value: QiecDatum) -> str
The content hash of a value that refers to the module's declarations.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
What the value is; it separates the digest's domain.
TYPE:
|
value
|
The value, such as the interpretation bindings that accompany a checked entry.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The digest of the value's core form, in which references to the module's declarations are their hashes, exactly as inside a declaration. |
| RAISES | DESCRIPTION |
|---|---|
ContentHashError
|
If the value has no core form. |
Source code in src/quivers/qiec/content_hash.py
SubtermHash
didactic-model
¶
Bases: Model
The alpha-invariant hash of one subterm.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
The field names and tuple positions leading from the root to the subterm.
TYPE:
|
hash
|
The subterm's hash modulo alpha-equivalence. Two subterms share it exactly when they are alpha-equivalent as open terms with the same free locals.
TYPE:
|
HashConsTable
¶
A table sharing alpha-equivalent terms.
intern returns, for each record term, the first alpha-equivalent term
the table has seen, so a normalizer that interns the terms it builds
shares their alpha-equivalent pieces, and can memoize a result by
alpha_hash and reuse it for every term alpha-equivalent to the one it
computed it for.
Source code in src/quivers/qiec/content_hash.py
__len__
¶
The number of distinct terms interned.
| RETURNS | DESCRIPTION |
|---|---|
int
|
How many alpha-equivalence classes the table holds. |
intern
¶
The table's representative of a term.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
A record term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
T
|
The first term the table saw with the same alpha hash, which is
|
| RAISES | DESCRIPTION |
|---|---|
ContentHashError
|
If the term holds a value with no core form. |
Source code in src/quivers/qiec/content_hash.py
content_digest
¶
The BLAKE3 digest every content identity is built from.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
The bytes to digest.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The 64-character lowercase hexadecimal BLAKE3 digest, the hash function panproto addresses its objects with. |
Source code in src/quivers/qiec/content_hash.py
canonical_json
¶
canonical_json(form: CoreJson) -> str
Serialize a core form deterministically.
| PARAMETER | DESCRIPTION |
|---|---|
form
|
The form to serialize.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Compact, key-sorted JSON. Floats use the shortest representation that reads back to the same double, so the text is the same on every run and every platform. |
| RAISES | DESCRIPTION |
|---|---|
ContentHashError
|
If the form contains a non-finite float, which has no JSON form. |
Source code in src/quivers/qiec/content_hash.py
tagged_digest
¶
tagged_digest(kind: str, form: CoreJson) -> str
Digest a core form under the content-hash domain.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
What the form is (
TYPE:
|
form
|
The form.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The digest of |
| RAISES | DESCRIPTION |
|---|---|
ContentHashError
|
If the form cannot be serialized. |
Source code in src/quivers/qiec/content_hash.py
canonical_order
¶
canonical_order(members: list[int], colors: Mapping[int, str], successors: Mapping[int, tuple[int, ...]], serialize: Callable[[list[int]], str]) -> list[int]
Order the members of a recursive component canonically.
| PARAMETER | DESCRIPTION |
|---|---|
members
|
The members, in any order.
TYPE:
|
colors
|
Each member's digest with its references into the component erased.
TYPE:
|
successors
|
Each member's references into the component, in occurrence order.
TYPE:
|
serialize
|
The component's serialization in a given member order.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[int]
|
The members ordered by their colors refined over the component's references until no class splits. Members the refinement cannot tell apart are individualized in turn, each choice refined again, and the order whose serialization is least is kept. The result is a canonical labeling: it depends only on the component's structure, never on the order the members were given in. |
Source code in src/quivers/qiec/content_hash.py
module_hashes
¶
module_hashes(module: QiecModule) -> ModuleHashes
Content identities of every declaration of a checked module.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The checked module, as the lowering returns it.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ModuleHashes
|
The module digest and one identity per declaration. |
| RAISES | DESCRIPTION |
|---|---|
ContentHashError
|
If the module holds a value with no core form. |
Source code in src/quivers/qiec/content_hash.py
normal_form
¶
normal_form(module: QiecModule, kind: DeclarationKind, name: str) -> str
The canonical serialization a declaration's component is hashed from.
| PARAMETER | DESCRIPTION |
|---|---|
module
|
The checked module.
TYPE:
|
kind
|
The declaration's kind.
TYPE:
|
name
|
The declaration's name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The JSON text of the component's core forms in canonical member
order, which is what |
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the module has no such declaration. |
ContentHashError
|
If the module holds a value with no core form. |
Source code in src/quivers/qiec/content_hash.py
alpha_hash
¶
alpha_hash(term: QiecDatum) -> str
Hash a term modulo alpha-equivalence.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
A value, computation, or any record of the stable QIEC graph.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A digest shared by exactly the terms alpha-equivalent to |
| RAISES | DESCRIPTION |
|---|---|
ContentHashError
|
If the term holds a value with no core form. |
Source code in src/quivers/qiec/content_hash.py
alpha_hashes
¶
alpha_hashes(term: QiecDatum) -> tuple[SubtermHash, ...]
Hash every record subterm of a term modulo alpha-equivalence.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[SubtermHash, ...]
|
The hash of every record inside |
| RAISES | DESCRIPTION |
|---|---|
ContentHashError
|
If the term holds a value with no core form. |