Typing¶
This page defines theories and the typing relation, and proves that types are unique, that the variadic reading of ACU spines is unambiguous, and that substitution, abstraction, and type instantiation preserve types. These are the facts that make the kernel's type inference correct. It also shows that the theories included in a theory are theories, which lets a judgment live in the smallest theory that contains what it uses, and that the kernel's version vectors represent theories faithfully.
Theories¶
Definition T-1 (declarations and theories). Each declaration \(d\) is made in a theory \(\operatorname{par}(d)\), its parent, and must be well formed there; a name is fresh when no declaration of the parent has it:
| Declaration | Content | Well formed when |
|---|---|---|
| type constructor | \(\kappa\), with arity \(n\) | \(\kappa\) is fresh |
| constant | \(c: \sigma_c\) | \(c\) fresh; \(\sigma_c\) well formed |
| definition | \(c: \sigma_c:= b\) | \(c\) fresh; \(b\) closed, \(\vdash b: \sigma_c\), \(b\) mentions only symbols of the parent, and \(\operatorname{tv}(b) \subseteq \operatorname{tv}(\sigma_c)\) |
| axiom | \(A\) | \(A\) closed, \(\vdash A: \mathsf{Prop}\) |
| theorem | \(A\) with a closed checked judgment \(\vdash A\) | as for an axiom, and \(\vdash A\) is derivable in the parent |
| ACU declaration | \(c\) at \(\bar\rho\), unit \(e\) or none, three law references | Definition T-6 |
| literal kind | \(k\), carrier \(C_k\), domain, bindings, cited laws | Definition T-7 |
A theory is a finite set \(T\) of declarations that contains the parent of each of its declarations and satisfies the global conditions:
- no name is declared twice in \(T\);
- two ACU declarations of \(T\) for one constant have type arguments that do not unify after their type variables are renamed apart (Definition T-6, condition 5);
- no two literal kinds of \(T\) have one carrier (Definition T-7);
- the instances that a literal kind of \(T\) binds are pairwise distinct,
and none of them bound to an operation other than
ADDandMULis an ACU instance of \(T\) (Definition T-7).
Theories are built from the empty theory by extension \(T + d\), which adds a declaration whose parent is \(T\), and by merge \(T_1 \sqcup T_2\), the union of two theories; each is defined when the result satisfies the global conditions. \(T\) is included in \(T'\), written \(T \sqsubseteq T'\), when \(T \subseteq T'\); in particular \(T \sqsubseteq T + d\) and \(T_i \sqsubseteq T_1 \sqcup T_2\). Under inclusion, theories form a directed acyclic graph rather than a tree, because a merge has two parts neither of which includes the other.
The kernel's root theory is the logic core (K1.4), built from the empty theory by extensions. Every theory the kernel builds is built from the root theory, so every theory of the kernel includes the logic core. For \(d \in T\), \(\operatorname{par}(d) \subsetneq T\), because \(d \notin \operatorname{par}(d)\); since strict inclusion of finite sets is well founded, statements about theories may be proved by induction on theories under strict inclusion, assuming them for the parents of a theory's declarations. This needs no fixed order of declarations: those of a merge come from two branches, neither of which precedes the other.
The signature \(\Sigma(T)\) is the set of type constructors, constants, ACU declarations, and literal kinds of \(T\). The ACU declarations and literal kinds of \(T\) are its canonical signature, and the least theory included in \(T\) that contains them (Lemma T-8) is its signature theory \(\operatorname{sig}(T)\). Two theories have the same canonical signature exactly when they have the same signature theory, since each determines the other.
Definition T-2 (well-formed types). \(\tau\) is well formed in \(\Sigma\) when every constructor \(\kappa\) occurring in it is declared in \(\Sigma\) and applied to \(\operatorname{ar}(\kappa)\) arguments.
The typing relation¶
Definition T-3 (typing contexts). A binder environment \(\Delta = \tau_0 \cdot \tau_1 \cdots \tau_{m-1}\) lists the types of the enclosing binders, innermost first; \(\Delta_i = \tau_i\). A local typing \(L\) is a finite map from locals to well-formed types.
Definition T-4 (typing). \(\Delta; L \vdash t: \tau\) is the least relation closed under the rules
where \(\bar\alpha_c\) are the type parameters of \(\sigma_c\) (Definition S-3),
and \(\tau\) in (Lam) and (Let) is well formed. The values \(0\) and \(1\) of a
literal kind are not atoms: they are the instances \(0_k\) and \(1_k\) bound to
ZERO and ONE (Definition T-7), typed by (Const). A proposition is a
term of type \(\mathsf{Prop}\).
Definition T-5 (type schemes). A constant, axiom, or theorem whose type or statement has type parameters \(\bar\alpha\) is a scheme; its instances are obtained by substituting types for \(\bar\alpha\).
Definition T-6 (ACU declarations). An ACU declaration for a constant \(c\) at type arguments \(\bar\rho\) is well formed when:
- \(\sigma_c[\bar\alpha_c:= \bar\rho] = C \to C \to C\) for a type \(C\) whose outermost symbol is a type constructor: \(C = \kappa(\bar\pi)\). \(C\) is neither an arrow type nor a type variable. (This restriction is used in Theorems T-1 and T-2 and in Lemma C-2.)
- \(c\) is not a constant of the logic core, and not a transparent definition.
- The referenced laws are theorems or axioms whose statements are, literally, \(\forall a\, b\, d{:}C.\; c[\bar\rho](c[\bar\rho](a,b),d) = c[\bar\rho](a, c[\bar\rho](b,d))\), \(\forall a\, b{:}C.\; c[\bar\rho](a,b) = c[\bar\rho](b,a)\), and, if a unit \(e\) is given, \(\forall a{:}C.\; c[\bar\rho](e,a) = a\), compared in the theory before the declaration.
- The unit \(e\), if any, is an atomic term of type \(C\): a literal, or an instance \(d[\bar\pi]\) of a constant \(d\) that is not a transparent definition. An atomic term is canonical for every signature, so the unit stays recognizable when a larger theory enlarges its canonical signature; a compound unit such as \(g(a, b)\) would change its canonical form once \(g\) were declared ACU.
- No other ACU declaration for \(c\) has type arguments that unify with \(\bar\rho\) after the type variables of the two declarations are renamed apart. Without the renaming, \([\mathsf{List}\,\alpha]\) and \([\alpha]\) would not unify (the occurs check fails), although \([\mathsf{List}\,\mathsf{Int}]\) is an instance of both.
The set of ACU instances is \(\mathrm{ACU}(\Sigma) = \{\, c[\bar\rho\theta] \mid \text{an ACU declaration for } c \text{ at } \bar\rho \text{ is in } \Sigma,\; \theta \text{ a type substitution} \,\}\), with carrier \(C\theta\) and unit \(e\theta\). An instance \(c[\bar\tau]\) finds its declaration by one-way matching: the declaration at \(\bar\rho\) with \(\bar\rho\theta = \bar\tau\) for some \(\theta\). By condition 5 there is at most one, since a common instance of two declarations, with their variables renamed apart, would be a unifier. The match fixes \(\theta\) on the variables of \(\bar\rho\), which include those of \(C\) and of \(e\) (the type parameters of a constant all occur in its type), so every ACU instance has exactly one carrier and at most one unit. The set is closed under type substitution by construction, and the laws of an instance are the corresponding instances of the declared laws (Definition T-5).
Definition T-7 (literal kinds). A literal kind \(k\) has a set of values
\(V_k\) (\(\mathbb Z\), \(\mathbb Q\), or formal polynomials), implemented by the kernel;
a carrier \(C_k\), which must be a ground type whose outermost symbol is a
type constructor, and which no other literal kind has; optionally a domain
predicate \(D_k: C_k \to \mathsf{Prop}\); and bindings of its operations to
instances of declared constants that are not definitions, except that the
exponent predicate, which only the literal laws mention, may be bound to an
instance of a definition (see
conversion). Among them are
instances \(0_k, 1_k: C_k\), and the addition and multiplication of \(k\) are
ACU instances, declared earlier, with units \(0_k\) and \(1_k\). The bound
instances are pairwise distinct, and only those bound to ADD and MUL
may be ACU instances; since their types mention \(C_k\), and
no other literal kind has that carrier, an instance determines the kind
and the operation it is bound to, if any. The values \(0\)
and \(1\) are the terms \(0_k\) and \(1_k\); every other value \(v\) is the atom
\(\mathsf{lit}_k(v)\). We write \(\underline v\), or
\(\underline v_k\), for the term that represents \(v \in V_k\): \(0_k\), \(1_k\),
or \(\mathsf{lit}_k(v)\). These terms are the literals of kind \(k\). The
declaration cites theorems or axioms whose
statements are exactly the literal laws of
Definition C-5 for these bindings.
The global conditions of Definition T-1 are the conditions of Definitions
T-1, T-6, and T-7 that relate a declaration to declarations outside its
parent: the freshness of names, condition 5 of Definition T-6, the
uniqueness of a carrier's literal kind, and the condition that no instance
a literal kind binds to an operation other than ADD and MUL is an ACU
instance, which an ACU declaration of another branch could break. The
distinctness of a kind's bindings concerns that declaration alone; it is
listed among the global conditions so that every theory satisfies it, and
a merge never has to check it again. Every other condition mentions only
the parent, so a merge checks only the global conditions; the kernel
checks them in extend and merge (K1.3).
These conditions make canonical construction determined in every theory,
including an inconsistent one: an ACU instance has one unit, and literal
folding applies to one operation of one kind.
Definitions T-6 and T-7 keep transparent definitions out of the canonical signature: no ACU constant, unit, or literal operation that construction evaluates unfolds by δ. The exponent predicate may unfold: no equation of conversion mentions it. The decision procedure for definitional equality relies on this (Lemma C-9(2), Theorem C-8).
Properties¶
Theorem T-1 (uniqueness of types). If \(\Delta; L \vdash t: \tau\) and \(\Delta; L \vdash t: \tau'\), then \(\tau = \tau'\).
Proof. Induction on \(t\). Every term form except the spine has exactly one rule. For a spine \(f(a_1, \dots, a_n)\), two rules may apply only when \(f\) is an ACU instance \(c[\bar\tau]\) with carrier \(C\) and \(n \ge 2\). If \(n = 2\), (App) gives the type obtained by applying \(C \to C \to C\) to two arguments, which is \(C\), the same as (ACU). If \(n \ge 3\), (App) would require \(\sigma_c[\bar\alpha_c:= \bar\tau] = C \to C \to C\) to have the form \(\tau_1 \to \tau_2 \to \tau_3 \to \tau\), so that \(C = \tau_3 \to \tau\); but \(C\) is not an arrow type (Definition T-6, condition 1; a class-level ACU declaration never applies at a function type, Definition Y-15). So only (ACU) applies, and types are unique. \(\square\)
Theorem T-2 (the variadic reading is unambiguous). If \(c[\bar\tau]\) is an ACU instance and \(n \ge 3\), then \(c[\bar\tau](a_1, \dots, a_n)\) is not typable by (App). If \(\Delta; L \vdash t: \tau\), the curried reading \(\langle t \rangle\) (Definition S-6) is a well-typed curried term of the same type.
Proof. The first statement is the case \(n \ge 3\) of the proof of Theorem T-1. For the second, induction on the typing derivation: an (App) spine reads as iterated binary application of matching types; an (ACU) spine reads as \(c(a_1, c(a_2, \dots))\), each application of type \(C \to C \to C\) to two arguments of type \(C\). \(\square\)
Theorem T-3 (weakening). If \(\Delta; L \vdash t: \tau\), then \(\Delta'; L' \vdash {\uparrow^d_c}\, t: \tau\) whenever \(L \subseteq L'\) and \(\Delta'\) is \(\Delta\) with \(d\) types inserted at position \(c\).
Proof. Standard induction on the derivation [Pierce 2002, ch. 9]. The variable case uses Definition S-8: indices at or above the cutoff move past the inserted types. The (ACU) case is like (App). \(\square\)
Theorem T-4 (substitution and abstraction preserve types).
- If \(\tau_s \cdot \Delta; L \vdash b: \tau\) and \(\Delta; L \vdash s: \tau_s\), then \(\Delta; L \vdash b\{0:= s\}: \tau\).
- If \(\Delta; L \vdash t: \tau\), \(L(\ell) = \tau_\ell\), and \(s\) is bound-closed with \([\,]; L \vdash s: \tau_\ell\), then \(\Delta; L \vdash t[\ell:= s]: \tau\).
- If \(\Delta; L \vdash t: \tau\) and \(L(\ell) = \tau_\ell\), then \(\tau_\ell \cdot \Delta; L \vdash \operatorname{abs}^0_\ell\, t: \tau\).
Proof. Each by induction on the derivation, generalizing the depth: statement 1 to \(b\{j:= s\}\) with \(\Delta = \Delta_{<j} \cdot \tau_s \cdot \Delta_{\ge j}\), using Theorem T-3 for the shifted \({\uparrow^j_0}\, s\); statement 3 to \(\operatorname{abs}^j_\ell\), inserting \(\tau_\ell\) at position \(j\). The spine rules are preserved because the argument types are unchanged; in particular an (ACU) spine keeps arguments of type \(C\). \(\square\)
Theorem T-5 (type instantiation preserves types). If \(\Delta; L \vdash t: \tau\), then \(\Delta\theta; L\theta \vdash t\theta: \tau\theta\) for every type substitution \(\theta\).
Proof. Induction on the derivation. (Const): the variables of \(\sigma_c\) are exactly \(\bar\alpha_c\), so \(\sigma_c[\bar\alpha_c:= \bar\tau\theta] = (\sigma_c[\bar\alpha_c:= \bar\tau])\theta\). (ACU): \(c[\bar\tau\theta] \in \mathrm{ACU}(\Sigma)\) with carrier \(C\theta\), because \(\mathrm{ACU}(\Sigma)\) is closed under type substitution (Definition T-6), unless \(\theta\) moves the carrier of a class-level declaration to a function type, where it does not apply (Definition Y-15); canonical substitution then reads the spine nested to the right (C1.2), each application of \(c[\bar\tau\theta]: C\theta \to C\theta \to C\theta\) typed by (App). (Lit): \(C_k\) is ground, so \(C_k\theta = C_k\). The other rules commute with \(\theta\) directly. \(\square\)
Proposition T-6 (type inference). Typing is syntax-directed: the type of a node is determined by its form and the types of its children, with (ACU) chosen exactly when the head is an ACU instance with at least three arguments (or two, where both rules agree). Bottom-up inference therefore decides typability, with one check per node. Because types are hash-consed (Requirement S-R2), each type comparison is an id comparison.
Proposition T-7 (reduction preserves types). If \(t\) is well typed and \(t \to_\beta u\) or \(t \to_\zeta u\), then \(u\) has the same type (Theorem T-4, statement 1). The canonicalization steps of the conversion page (flattening, sorting, unit removal, literal folding) replace an argument list of type-\(C\) terms by another, or a spine of type \(C\) by a term of type \(C\), and also preserve types.
Lemma T-8 (sub-theories). Every subset of a theory \(T\) that contains the parent of each of its members is a theory included in \(T\). In particular, the union of two theories included in \(T\) is a theory, and so is the least theory included in \(T\) that contains a given set \(S\) of declarations of \(T\): the union of \(\operatorname{par}(d) \cup \{d\}\) over \(d \in S\).
Proof. The global conditions of Definition T-1 forbid certain pairs of declarations, so a subset of \(T\) satisfies them because \(T\) does. Each member is well formed in its parent, which the subset contains by assumption. A union of sets that contain the parents of their members contains the parents of its members, and \(\operatorname{par}(d) \cup \{d\}\) is such a set, because \(\operatorname{par}(d)\) is a theory. \(\square\)
Typing in \(T\) depends only on the declarations a term mentions and on the ACU declarations of \(T\): rule (ACU) of Definition T-4 consults \(\mathrm{ACU}(\Sigma)\). A term of the language of a theory \(H \sqsubseteq T\) with \(\operatorname{sig}(T) \sqsubseteq H\) therefore has the same types in \(H\) as in \(T\). Theorem D-8 uses this.
Version vectors¶
The kernel represents theories by version vectors over segments
(K1.3).
A segment \(s\) is a sequence of declarations
\(d_{s,1}, d_{s,2}, \dots\) with a base theory \(B_s\), where
\(\operatorname{par}(d_{s,1}) = B_s\) and
\(\operatorname{par}(d_{s,i+1}) = \operatorname{par}(d_{s,i}) \cup \{d_{s,i}\}\):
extend appends a declaration to the segment whose last declaration
produced its parent, and otherwise starts a new segment based on the
parent. Every declaration lies in exactly one segment. The version
vector of a theory \(T\) maps each segment \(s\) to
\(v_T(s) = \lvert T \cap s\rvert\).
Lemma T-9 (version vectors are faithful). For theories \(T\), \(T'\), \(T_1\), \(T_2\):
- \(T \cap s = \{d_{s,1}, \dots, d_{s,v_T(s)}\}\) for every segment \(s\), so \(v_T\) determines \(T\), and \(T\) includes the base of every segment \(s\) with \(v_T(s) > 0\);
- \(T \sqsubseteq T'\) if and only if \(v_T \le v_{T'}\) pointwise;
- \(v_{T_1 \cup T_2} = \max(v_{T_1}, v_{T_2})\) pointwise, so the pointwise maximum is the vector of the union;
- every vector the kernel interns is \(v_T\) for a theory \(T\): it is the vector of an extension, of a merge, or of a join, starting from the empty theory, whose vector is \(0\).
Proof. (1) By induction on \(i\), \(\operatorname{par}(d_{s,i})\) contains
\(d_{s,j}\) for every \(j < i\). A theory contains the parent of each of its
members, so if \(T\) contains \(d_{s,i}\) it contains every earlier declaration
of \(s\) and \(\operatorname{par}(d_{s,1}) = B_s\). Hence \(T \cap s\) is the
prefix of \(s\) of length \(v_T(s)\), and \(T\) is the union of these prefixes.
(2) A prefix of \(s\) is included in another exactly when it is not longer.
(3) The union of two prefixes of \(s\) is the longer one. (4) By induction
on the kernel's calls (the root theory is built by extensions). If
\(d = d_{s,i+1}\),
then \(\operatorname{par}(d)\) is the theory produced by \(d_{s,i}\), so
\(v_{\operatorname{par}(d)}(s) = i\) by (1), and the vector of
\(\operatorname{par}(d) + d\) is \(v_{\operatorname{par}(d)}\) with the entry
of \(s\) raised to \(i + 1\); a declaration that starts a segment raises that
segment's entry from \(0\) to \(1\). merge interns the pointwise maximum
only after its checks pass, when the union is a theory, and
a join is the union of theories included in a common theory, a theory by
Lemma T-8; by (3) the maximum is the vector of that union. \(\square\)
Hence includes decides inclusion of theories and merge computes their
union, and two equal vectors name one theory, so interning them gives
each theory one TheoryId.