Logic & kernel theory

Syntax

This page defines types and terms, de Bruijn indices, and the substitution operations the kernel uses, and proves the facts about them that the implementation relies on, including that renaming the ids of nodes, as a workspace commit does, changes none of them. Typing is on the next page.

Types

Definition S-1 (types). Fix a countably infinite set \(\mathcal V\) of type variables and a set of type constructors \(\kappa\), each with an arity \(\operatorname{ar}(\kappa) \in \mathbb N\). Types are generated by

\[ \tau \;::=\; \alpha \;\mid\; \kappa(\tau_1, \dots, \tau_{\operatorname{ar}(\kappa)}) \;\mid\; \tau_1 \to \tau_2 \qquad (\alpha \in \mathcal V). \]

The arrow associates to the right. \(\operatorname{tv}(\tau)\) is the set of type variables occurring in \(\tau\); \(\tau\) is ground when \(\operatorname{tv}(\tau) = \emptyset\). The logic core declares the constructor \(\mathsf{Prop}\) of arity 0.

Definition S-2 (type substitution). A type substitution is a map \(\theta: \mathcal V \to \mathrm{Types}\) that is the identity outside a finite set. It acts on types by \(\alpha\theta = \theta(\alpha)\), \(\kappa(\bar\tau)\theta = \kappa(\bar\tau\theta)\), and \((\tau_1 \to \tau_2)\theta = \tau_1\theta \to \tau_2\theta\).

Definition S-3 (type-parameter order). The type parameters of a type \(\sigma\), written \(\bar\alpha_\sigma\), are the variables of \(\operatorname{tv}(\sigma)\) in the order of their first occurrence in a left-to-right traversal of \(\sigma\). The type parameters of a proposition are defined the same way, traversing its canonical form with all its type annotations: binder types of λ and let, and the type arguments of every constant instance. Since the canonical form sorts ACU arguments, clients read the order from the kernel (type_params, K1.3) rather than predict it. A constant declared with type \(\sigma_c\) takes the type arguments \(\bar\tau\) in this order: \(c[\bar\tau]\) stands for \(c\) at the type \(\sigma_c[\bar\alpha_{\sigma_c}:= \bar\tau]\).

Terms

Definition S-4 (terms). Given constants \(c\) with declared types \(\sigma_c\), a set \(\mathcal L\) of locals, and literal kinds \(k\), each with a set of values \(V_k\), terms are generated by

\[ t \;::=\; \#i \;\mid\; \ell \;\mid\; c[\bar\tau] \;\mid\; t(t_1, \dots, t_n) \;\mid\; \lambda{:}\tau.\, t \;\mid\; \mathsf{let}{:}\tau = t \;\mathsf{in}\; t \;\mid\; \mathsf{lit}_k(v) \]

with \(i \in \mathbb N\), \(\ell \in \mathcal L\), \(\lvert\bar\tau\rvert = \lvert\bar\alpha_{\sigma_c}\rvert\), \(n \ge 1\), and \(v \in V_k \setminus \{0, 1\}\): the values \(0\) and \(1\) of a literal kind are represented by the constants bound to its zero and one, not by atoms (Definition T-7). Each local has a binding site, the context it extends, its type and its name, which identifies it (K1.5, TD1.7). \(\#i\) is a bound variable in de Bruijn notation: it refers to the \(i\)-th enclosing binder, counting from \(0\) at the innermost [de Bruijn 1972]. \(\lambda\) and \(\mathsf{let}\) bind one variable over their body (for \(\mathsf{let}\), not over the value).

Definition S-5 (spines). In an application \(t(t_1, \dots, t_n)\), the function \(t\) is called the head and the \(t_i\) the arguments. A term is spine-normal when no head is itself an application. The kernel's application constructor keeps terms spine-normal by merging: \(\mathrm{app}(f(\bar a), \bar b) = f(\bar a, \bar b)\).

Definition S-6 (curried reading). The logic is defined on curried terms, whose application is binary. A spine stands for iterated application:

\[ \langle f(a_1, \dots, a_n) \rangle \;=\; (\cdots(\langle f\rangle\, \langle a_1\rangle)\cdots)\,\langle a_n\rangle, \]

except for a constant \(c[\bar\tau]\) declared ACU with carrier \(C\) (Definition T-6), whose spines with \(n \ge 3\) arguments are read right-nested:

\[ \langle c(a_1, a_2, \dots, a_n) \rangle \;=\; \langle c(a_1,\, c(a_2, \dots, a_n)) \rangle \qquad (n \ge 3). \]

The two readings never conflict: Theorem T-2 shows that a spine of an ACU constant with three or more arguments is ill typed under the first reading, because its carrier is not an arrow type. The choice of right nesting is immaterial, since the associativity law makes all nestings equal.

Loose bound variables

Definition S-7. The loose bound \(\operatorname{lb}(t)\) is one more than the largest index of a bound variable of \(t\) that is not captured by a binder of \(t\), and \(0\) if there is none:

\[ \begin{aligned} \operatorname{lb}(\#i) &= i + 1, \qquad \operatorname{lb}(\ell) = \operatorname{lb}(c[\bar\tau]) = \operatorname{lb}(\mathsf{lit}_k(v)) = 0,\\ \operatorname{lb}(t(t_1,\dots,t_n)) &= \max(\operatorname{lb}(t), \operatorname{lb}(t_1), \dots, \operatorname{lb}(t_n)),\\ \operatorname{lb}(\lambda{:}\tau.\, b) &= \max(\operatorname{lb}(b) - 1,\, 0),\\ \operatorname{lb}(\mathsf{let}{:}\tau = v \;\mathsf{in}\; b) &= \max(\operatorname{lb}(v),\, \operatorname{lb}(b) - 1,\, 0). \end{aligned} \]

A term is bound-closed when \(\operatorname{lb}(t) = 0\), and closed when it is bound-closed and contains no local.

Requirement S-R1. termbank's loose bound (T1.5: the bvar atom's index plus one; otherwise the maximum over the arguments, less the head's binder count for the last argument) equals \(\operatorname{lb}\). The binder heads are lam with arguments \((\tau, b)\) and let with arguments \((\tau, v, b)\), so only the body is under the binder, as in Definition S-7. Types have loose bound \(0\).

Shifting, instantiation, and abstraction

Definition S-8 (shifting). \(\uparrow^d_c t\) adds \(d\) to every bound variable of \(t\) with index at least the cutoff \(c\), where the cutoff increases by one under each binder:

\[ \uparrow^d_c \#i = \begin{cases} \#(i+d) & i \ge c\\ \#i & i < c\end{cases} \qquad \uparrow^d_c (\lambda{:}\tau.\, b) = \lambda{:}\tau.\, {\uparrow^d_{c+1}} b, \]

\(\uparrow^d_c(\mathsf{let}{:}\tau = v \;\mathsf{in}\; b) = \mathsf{let}{:}\tau = {\uparrow^d_c} v \;\mathsf{in}\; {\uparrow^d_{c+1}} b\), and \(\uparrow^d_c\) commutes with every other constructor and fixes locals, constants, and literals.

Definition S-9 (instantiation). \(b\{j:= s\}\) replaces the bound variable \(\#j\) of \(b\) by \(s\) and closes the gap:

\[ \#i\{j:= s\} = \begin{cases} {\uparrow^j_0}\, s & i = j\\ \#(i-1) & i > j\\ \#i & i < j\end{cases} \qquad (\lambda{:}\tau.\, b)\{j:= s\} = \lambda{:}\tau.\, b\{j+1:= s\}, \]

with the analogous clause for \(\mathsf{let}\) and homomorphic clauses elsewhere. β-reduction and ζ-reduction are

\[ (\lambda{:}\tau.\, b)(a) \;\to_\beta\; b\{0:= a\}, \qquad \mathsf{let}{:}\tau = v \;\mathsf{in}\; b \;\to_\zeta\; b\{0:= v\}. \]

For a spine with several arguments, \((\lambda{:}\tau.\, b)(a_1, a_2, \dots, a_n) \to_\beta \mathrm{app}(b\{0:= a_1\}, a_2, \dots, a_n)\).

Definition S-10 (abstraction). \(\operatorname{abs}^j_\ell\, t\) replaces the local \(\ell\) by the bound variable \(\#j\), raising the bound variables that are loose at depth \(j\):

\[ \operatorname{abs}^j_\ell\, \#i = \begin{cases} \#(i+1) & i \ge j\\ \#i & i < j\end{cases} \qquad \operatorname{abs}^j_\ell\, \ell = \#j, \qquad \operatorname{abs}^j_\ell\, \ell' = \ell' \;(\ell' \ne \ell), \]

with \(j+1\) under binders. The kernel forms \(\lambda{:}\tau.\, \operatorname{abs}^0_\ell\, t\) when it generalizes or abstracts over \(\ell\).

Definition S-11 (substitution for a local). For a bound-closed \(s\), \(t[\ell:= s]\) replaces every occurrence of \(\ell\) by \(s\). Because \(s\) has no loose bound variables, no shifting is needed under binders.

Facts the kernel relies on

Lemma S-1 (closed parts are fixed). If \(\operatorname{lb}(t) \le c\), then \(\uparrow^d_c t = t\). If \(\operatorname{lb}(b) \le j\), then \(b\{j:= s\} = b\).

Proof. Induction on \(t\) (respectively \(b\)). For \(\#i\): \(\operatorname{lb}(\#i) = i + 1 \le c\) gives \(i < c\), so the variable is unchanged; likewise \(i < j\) for instantiation. Under a binder, \(\operatorname{lb}(\lambda{:}\tau.\, b) \le c\) gives \(\operatorname{lb}(b) \le c + 1\), which is the cutoff used for the body. The other cases are immediate. \(\square\)

This justifies the implementation's skipping of subterms whose loose bound is at most the current depth (C1.2).

Lemma S-2 (loose bounds after instantiation). If \(\operatorname{lb}(b) \le j + 1\) and \(\operatorname{lb}(s) = 0\), then \(\operatorname{lb}(b\{j:= s\}) \le j\). In particular, if \(\lambda{:}\tau.\, b\) is bound-closed and \(s\) is bound-closed, so is \(b\{0:= s\}\).

Proof. Induction on \(b\), generalizing over \(j\). A variable \(\#i\) with \(i < j\) stays and has loose bound \(i + 1 \le j\); \(\#j\) becomes \({\uparrow^j_0} s = s\) by Lemma S-1, with loose bound \(0\); a variable with \(i > j\) cannot occur, since \(\operatorname{lb}(b) \le j + 1\). Under a binder, the bound and \(j\) both increase by one. \(\square\)

Theorem S-3 (substitution lemma, cited). For all terms \(b\), \(s\), \(u\) and every \(j\),

\[ \big(b\{0:= s\}\big)\{j:= u\} \;=\; \big(b\{j+1:= u\}\big)\{0:= s\{j:= u\}\}. \]

This is the de Bruijn form of the substitution lemma [de Bruijn 1972; Pierce 2002, ch. 6]. It is what makes β-reduction a well-defined operation on terms, independent of the order in which redexes are contracted, and it underlies the confluence of β (Theorem C-1).

Lemma S-4 (abstraction then instantiation). If \(\operatorname{lb}(t) \le j\) and \(\operatorname{lb}(s) = 0\), then

\[ (\operatorname{abs}^j_\ell\, t)\{j:= s\} \;=\; t[\ell:= s]. \]

In particular, for bound-closed \(t\), \((\operatorname{abs}^0_\ell\, t)\{0:= s\} = t[\ell:= s]\): eliminating a universal quantifier introduced over \(\ell\) substitutes for \(\ell\).

Proof. Induction on \(t\), generalizing over \(j\). Since \(\operatorname{lb}(t) \le j\), every loose \(\#i\) of \(t\) has \(i < j\); abstraction leaves it unchanged and instantiation then leaves it unchanged. The local \(\ell\) becomes \(\#j\) and then \({\uparrow^j_0} s = s\), by Lemma S-1 since \(\operatorname{lb}(s) = 0\). A local \(\ell' \ne \ell\), a constant, or a literal is unchanged by both. Under a binder, \(\operatorname{lb}\) of the body is at most \(j + 1\) and both operations use \(j + 1\). Spines and let follow from the induction hypothesis on each part. \(\square\)

Lemma S-5 (type substitution commutes). For every type substitution \(\theta\): \(\operatorname{lb}(t\theta) = \operatorname{lb}(t)\), \((\uparrow^d_c t)\theta = \uparrow^d_c (t\theta)\), \((b\{j:= s\})\theta = (b\theta)\{j:= s\theta\}\), and \((\operatorname{abs}^j_\ell\, t)\theta = \operatorname{abs}^j_\ell\,(t\theta)\), where \(t\theta\) applies \(\theta\) to every type annotation and every type argument of a constant.

Proof. Type substitution changes only type annotations and type arguments, and none of these operations inspects them. \(\square\)

Representation

Requirement S-R2 (interning). termbank assigns equal ids to nodes with equal head, payload, and argument ids, and different ids otherwise. Hence, by induction on terms, two terms are equal exactly when their ids are equal. Term equality is an id comparison.

Requirement S-R3 (heads). A constant is identified by its declaration, not by its name: the kernel registers one termbank head per declaration, and same-named declarations of sibling theories have different heads. An application whose head is a constant \(c[\bar\tau]\) is stored under the head of \(c\)'s declaration, with the type arguments first and the term arguments after them; other applications use the app head (see termbank). Since the number of type arguments of \(c\) is fixed by its declaration (Definition S-3), this encoding is injective on spine-normal terms.

A local \(\ell\) is stored as a local atom whose payload names its local row, and a bound variable as a bvar atom whose payload is its index; every other payload is pooled or absent. A node's structural hash, loose bound, and flags are computed from its head's name, binder count, and flags, its payload (an immediate payload itself, a pooled one through its bytes), its argument count, and the same quantities of its arguments; a local atom's structural hash from its binding depth, its name's bytes and its type's hash instead of its payload (T1.5). The structural order (T1.6) compares a pair of nodes lexicographically: equal ids first; then structural hashes; then head key, head name, payload hash, pooled bytes, and argument count; then the arguments left to right by the same order; and last, for a pair whose content is equal, their head indices. In particular two locals are ordered by their structural hashes, then their binding depths and names; two locals that tie on these lie on different chains, where their row ids break the tie, so their order within a well-formed term depends on no table index. The head-index tie-break separates only same-named declarations of sibling theories.

Lemma S-6 (renaming ids). Let \(\pi\) be an injective renaming of node ids, head ids, and pooled-string indices that maps each node \((h, p, a_1, \dots, a_n)\) to \((\pi h, p', \pi a_1, \dots, \pi a_n)\), where \(p' = \pi p\) for a pooled payload or a local's payload and \(p' = p\) for any other immediate one, that keeps each head's name, binder count, and flags, each pooled string's bytes, and the depth, the name and the type of each local, that maps each frame's parent to its image's parent, and that preserves the relative order of head ids. Then \(\pi\) preserves:

  1. the structural hash, the loose bound, and the flags of every node;
  2. the structural order on every pair of nodes whose locals lie on one chain: \(\operatorname{compare}(\pi a, \pi b) = \operatorname{compare}(a, b)\);
  3. identity: if no two nodes of the bank have equal content (Requirement S-R2), neither do their images, so equality of terms remains equality of ids;
  4. canonical forms: \(\pi\) maps a canonical term to a canonical term, and maps \(N(t)\) to \(N(\pi t)\), once the kernel renames each declaration's head with it.

Proof. (1) By induction on nodes, arguments first: each quantity is computed from data that \(\pi\) keeps (names, binder counts, flags, bytes, immediate payloads other than a local's, argument counts, and a local's depth, name and type) and from the same quantity of the arguments. (2) By induction on the pairs that \(\operatorname{compare}\) visits: equality of ids is preserved because \(\pi\) is injective, every content comparison is preserved by (1) and because \(\pi\) keeps names and bytes, the argument pairs are compared by the induction hypothesis, and the final comparison of head ids is preserved because \(\pi\) preserves their order; the final comparison of local row ids is reached only for locals of one depth and one name, which lie on different chains, so not for the pairs of (2). (3) The content of \(\pi n\) is the image of the content of \(n\) under \(\pi\), which is injective on heads, payloads, and arguments, so two images with equal content come from two nodes with equal content. (4) \(\pi n\) reads as the same term as \(n\) (Definition S-4), with each head standing for the same declaration; canonical construction (Definition C-3) depends on terms only through their structure, their declarations, literal values, equality, which (3) preserves, and the structural order, which (2) preserves. \(\square\)

A workspace commit translates the ids of the rows it appends and keeps every name and pooled byte string (local checking, Definition LC-20). Proposition LC-20 applies this lemma to that translation: committed terms keep their hashes, order, and canonical forms, and are not rebuilt.

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