Logic & kernel theory

Type definitions, overloading, and classes

The kernel implements four definitional mechanisms used by Isabelle/HOL's library: type definitions, loose constant specifications, overloaded definitions, and type classes with sorts. With them, Isabelle's statements can be read as they are stated, and a theorem proved once for a class applies to every type of the class. This page defines the mechanisms, gives their semantics, and proves or cites why each preserves consistency. Results are numbered Y.

None of the mechanisms adds logical strength beyond definitions: each either defines something, or records a fact that a cited theorem has already proved. What they add is fidelity (statements keep Isabelle's shape) and efficiency (class membership is a table lookup, not a proof).

Type definitions

Definition Y-1 (type definition). Let \(P: \sigma \to \mathsf{Prop}\) be a closed term whose type variables are exactly the distinct \(\alpha_1, \dots, \alpha_k\), and let \(\vdash \exists x{:}\sigma.\, P\,x\) be a theorem of the parent theory. The type definition of \(\kappa\) by \(P\) declares:

  • a type constructor \(\kappa\) of arity \(k\);
  • constants \(\mathsf{abs}_\kappa: \sigma \to \kappa(\bar\alpha)\) and \(\mathsf{rep}_\kappa: \kappa(\bar\alpha) \to \sigma\);
  • the two theorems \(\vdash \forall a{:}\kappa(\bar\alpha).\; \mathsf{abs}_\kappa(\mathsf{rep}_\kappa\,a) = a\) and \(\vdash \forall r{:}\sigma.\; P\,r = (\mathsf{rep}_\kappa(\mathsf{abs}_\kappa\,r) = r)\).

These are HOL Light's new_basic_type_definition and, by eq_abstract and β, OpenTheory's defineTypeOp, whose theorems are the \(\lambda\)-abstracted forms of the same equations [Harrison 2009].

Theorem Y-1 (type definitions preserve models). Every model \(M\) of the parent theory \(T\) expands to a model of \(T\) with the type definition.

Proof. For sets \(\bar X \in \mathcal U^k\), let \(S(\bar X) = \{x \in \sem{\sigma}_{\bar\alpha \mapsto \bar X} \mid \sem{P}_{\bar\alpha \mapsto \bar X}(x) = 1\}\). By soundness (Theorem M-1) the witness theorem holds in \(M\), so \(S(\bar X)\) is nonempty, and it belongs to \(\mathcal U\) by Definition M-1(2). Set \(M(\kappa)(\bar X) = S(\bar X)\), let \(\mathsf{rep}_\kappa\) denote the inclusion of \(S(\bar X)\) into \(\sem\sigma\), and \(\mathsf{abs}_\kappa\) the map that is the identity on \(S(\bar X)\) and sends every other element to \(\operatorname{ch}(S(\bar X))\). Then \(\mathsf{abs}(\mathsf{rep}\,a) = a\) for every \(a\), and \(\mathsf{rep}(\mathsf{abs}\,r) = r\) exactly when \(r \in S(\bar X)\), that is, when \(P\,r\) holds. No clause of Definition M-3 for the declarations of \(T\) mentions \(\kappa\), so they still hold. \(\square\)

By Theorem M-4's argument, a theory with a model keeps one, so type definitions preserve consistency.

Loose specifications

Definition Y-2 (constant specification). Let \(\vdash \forall v_1 \dots v_k.\; v_1 = t_1 \to \dots \to v_k = t_k \to \varphi\) be a theorem of the parent theory, with the \(t_i\) closed, the free variables of \(\varphi\) among \(\bar v\), and each type variable of each \(t_i\) occurring in the type of \(v_i\). The specification declares constants \(c_1, \dots, c_k\) of the types of \(\bar v\), and the theorem \(\vdash \varphi[\bar c / \bar v]\). Unlike a definition, it gives no equation \(c_i = t_i\): the \(c_i\) are characterized by \(\varphi\) alone.

This is Arthan's principle [Arthan 2014], OpenTheory's defineConstList, with the hypotheses \(v_i = t_i\) written as premises.

Theorem Y-2 (specifications preserve models). Every model of the parent theory expands to a model with the specification.

Proof. Interpret \(c_i\) at \(\bar X\) as \(\sem{t_i}_{\bar X}\), which is well defined by the type-variable condition. The theorem holds in the model, and instantiating its quantifiers with these values discharges the premises by reflexivity, so \(\varphi[\bar c/\bar v]\) holds. \(\square\)

Overloaded definitions

Definition Y-3 (overloaded constant, instance definition). An overloaded constant \(c\) is declared with a type scheme \(\sigma_c\) over type variables \(\bar\alpha_c\), and no body. An instance definition of \(c\) at type arguments \(\bar\tau\) (an instance of \(\bar\alpha_c\)) is a closed term \(b\) of type \(\sigma_c[\bar\tau/\bar\alpha_c]\) whose type variables occur in \(\bar\tau\); it contributes the theorem scheme \(\vdash c[\bar\tau] = b\) over those type variables. An ordinary definition (Theorem M-5) is the instance definition at the most general arguments.

Definition Y-4 (defining declarations, dependency relation). An item is a type \(\kappa(\bar\tau)\) headed by a type constructor, or a constant instance \(c[\bar\tau]\). A defining declaration has a left-hand item and a body:

  • a definition \(c:= b\) (Theorem M-5): item \(c[\bar\alpha_c]\), body \(b\);
  • an instance definition of \(c\) at \(\bar\tau\) with body \(b\): item \(c[\bar\tau]\);
  • a type definition of \(\kappa\) by \(P\): item \(\kappa(\bar\alpha)\), body \(P\);
  • a specification (Definition Y-2): for each of its constants \(c_i\), item \(c_i[\bar\alpha_i]\), where \(\bar\alpha_i\) are the type variables of the type of \(v_i\), and body \(t_i\), the term Theorem Y-2 interprets \(c_i\) by.

Item \(u\) depends directly on item \(v\), written \(u \rightsquigarrow_1 v\), when \(u\) is the left-hand item of a defining declaration and \(v\) occurs in its body: as a constant instance, or as a type \(\kappa(\bar\sigma)\) in the body or in the type of one of its subterms. A literal of a kind \(k\) in the body counts as an occurrence of each instance \(k\) binds (and of each its base kind binds, for a polynomial kind): it denotes what those instances make of \(0\) and \(1\) (Theorem C-11), and canonical construction folds an explicit ADD of \(1\) and \(1\) into the literal \(2\), so the dependency would otherwise be hidden. The dependency relation \(\rightsquigarrow\) is the closure of \(\rightsquigarrow_1\) under type substitution and transitivity: if \(u \rightsquigarrow_1 v\) then \(u\theta \rightsquigarrow v\theta\) for every type substitution \(\theta\).

In this section type variables are read without their sorts: items are compared, matched, and unified as unsorted types. Isabelle does the same: its check (the module Defs of Isabelle/Pure) compares type arguments by purely structural unification and matching (Type.raw_unifys, Type.raw_matches). Without sorts there are only more common instances and more dependencies, so the conditions below are at least as strict as with sorts, and they are the conditions under which Theorem Y-3 is proved for classes translated away (Theorem Y-6).

Definition Y-5 (the overloading conditions). A theory satisfies

  • orthogonality when no two defining declarations whose left-hand items have the same head have type arguments that unify, after renaming their type variables apart; and
  • termination when \(\rightsquigarrow\) has no infinite chain \(u_0 \rightsquigarrow u_1 \rightsquigarrow u_2 \rightsquigarrow \cdots\).

Only instance definitions can share a head: every other defining declaration declares its head.

Definition Y-5a (paths, composability, acyclicity) [Kunčar 2015]. Write \(u \le v\) when \(u\) is an instance of \(v\), \(u \mathrel{\#} v\) when they have no common instance, and \(u \bowtie v\), \(u\) overlaps \(v\), when they have a common instance but neither is an instance of the other. A path from a left-hand item \(p_0\) is a sequence of items \(e_0, \dots, e_n\) in which \(e_0\) is an item of \(p_0\)'s body and, for each \(i < n\), \(e_i = p\sigma\) for a left-hand item \(p\) and \(e_{i+1} = q\sigma\) for an item \(q\) of \(p\)'s body. Its end is \(e_n\); the path is maximal when its end is an instance of no left-hand item. The relation of a theory's defining declarations is

  • monotone when every item of a body has only type variables of its left-hand item;
  • composable when every path end \(e\) and every left-hand item \(p\) with the same head satisfy \(e \le p\), \(p \le e\), or \(e \mathrel{\#} p\), never \(e \bowtie p\); and
  • acyclic when no path from a left-hand item \(p_0\) ends in an instance of \(p_0\).

Under monotonicity and orthogonality the paths from a left-hand item are unique up to renaming, and composability holds as soon as it holds for the ends of maximal paths [Kunčar 2015, Lemmas 6.2 and 6.6].

Theorem Y-3a (cited; deciding termination). For the finitely many defining declarations of a theory: if their relation is monotone, orthogonal, and composable, then \(\rightsquigarrow\) terminates if and only if the relation is acyclic. The four properties are decided together by Kunčar's reduction algorithm, which extends every path from every left-hand item by one step per round, compares each new end with its start, and checks the ends of maximal paths for composability; it always terminates [Kunčar 2015, Lemma 5.17, Lemma 6.7, and Theorem 1].

Definition Y-5b (well-formed definitions). The defining declarations of a theory are well formed when their relation is monotone, orthogonal, composable, and acyclic. By Theorem Y-3a they then satisfy orthogonality and termination, and Theorem Y-3 applies.

Composability is a restriction beyond Definition Y-5, the price of a decision procedure: termination alone is undecidable for overloaded definitions [Obua 2006]. Isabelle enforces a stronger restriction. A left-hand item whose type arguments are distinct type variables, or one type constructor applied to distinct type variables, is composable with every item of its head, which is an instance of it, more general, or disjoint from it; for any other left-hand item Isabelle records a restriction, and requires every end of a maximal path with that head to have distinct type variables as arguments or no common instance with it. Every theory Isabelle accepts is therefore well formed here.

Lemma Y-3b (adding a defining declaration). Let the defining declarations of \(T\) be well formed, and let \(T'\) add one more, with left-hand item \(p^\ast\). Those of \(T'\) are well formed exactly when:

  1. \(p^\ast\) is orthogonal to every left-hand item of \(T\) with its head, and the items of the new body have only type variables of \(p^\ast\);
  2. no end of a maximal path of \(T\) overlaps \(p^\ast\); and
  3. the reduction algorithm of Theorem Y-3a, run in \(T'\) from the starts \(p^\ast\) and every left-hand item of \(T\) with a maximal path end that is an instance of \(p^\ast\), finds no cycle and no maximal end that overlaps a left-hand item.

Proof. The conditions are necessary: (1) is orthogonality and monotonicity, and (2) and (3) are composability and acyclicity for some of \(T'\)'s paths. They are sufficient. A path of \(T'\) that never steps through the new declaration is a path of \(T\). A path from a left-hand item \(p\) that does is, before its first such step, a path of \(T\) whose end is an instance of \(p^\ast\); that end is an instance of no other left-hand item, by orthogonality, so the path is maximal in \(T\), and \(p\) is a start, or \(p = p^\ast\). A cycle of \(T'\) is a path from some \(p_0\) that steps through the new declaration, since \(T\) is acyclic, so \(p_0\) is a start; the algorithm extends every path from every start [Kunčar 2015, Lemmas 6.4 and 6.5], and finds it. Every end of a maximal path of \(T'\) is either an end of a path from a start, checked by (3), or the end of a maximal path of \(T\), which is composable with the left-hand items of \(T\) and, by (2), with \(p^\ast\). \(\square\)

A merge adds the defining declarations one theory lacks to the other one at a time, in creation order, each by Lemma Y-3b to the other theory and those added before it.

Definition Y-6 (ground semantics). A type or constant instance is ground when it has no type variables. A ground interpretation assigns a nonempty set of \(\mathcal U\) to every ground type, with arrows and \(\mathsf{Prop}\) interpreted as in Definition M-2, and an element of the right set to every ground constant instance. A proposition with type variables holds when every ground instance of it holds; a judgment is valid when every ground instance of it is valid in the sense of Definition M-4. A ground interpretation is a ground model when, in addition to the clauses of Definition M-3 read on ground instances, each ground instance of an instance definition, and of the theorems a type definition or a specification declares, holds.

Definition M-2 interprets a constant as a function of the sets its type arguments denote. That cannot interpret overloading: two different types may denote the same set, and an overloaded constant may be defined differently at each. Ground interpretations index by the types themselves. On theories without instance definitions, every model in the sense of Definition M-3 restricts to a ground model, so nothing proved so far changes.

Theorem Y-3 (cited; overloading preserves consistency). Every theory obtained from a consistent HOL theory by constant definitions, type definitions, and instance definitions satisfying orthogonality and termination has a ground model, built by well-founded recursion along \(\rightsquigarrow\), and the rules of HOL are sound for ground models; hence it is consistent [Kunčar and Popescu 2019]. The 2017 paper proves consistency again by a translation into HOL with comprehension types [Kunčar and Popescu 2017].

Termination must be checked over both kinds of items, and the check must be exact. Isabelle's earlier check missed a cycle that ran through a type definition, which allowed a proof of False until it was corrected [Kunčar and Popescu 2019], and an incomplete composability test allowed another [Kunčar 2015]. A verified checker exists in HOL4 [Gengelbach and Pohjola 2022]. The kernel's soundness argument for every rule (Theorem M-1) carries over to ground models instance by instance, because each rule's premises and conclusion are instantiated by the same type substitution: Theorem Y-8 below.

Classes and sorts

Definition Y-7 (the type itself). The logic core declares a type constructor \(\mathsf{itself}\) of arity 1 and a constant \(\mathsf{TYPE}[\alpha]: \mathsf{itself}(\alpha)\). A predicate on \(\mathsf{itself}(\alpha)\) is a property of the type \(\alpha\), expressed in the logic without violating the type-variable condition of definitions.

Definition Y-8 (class). A class \(c\) is declared with superclasses \(c_1, \dots, c_m\), parameters \(o_1, \dots, o_n\) (overloaded constants whose schemes mention exactly one type variable \(\alpha\)), and an axiom \(A(\alpha)\), a closed proposition whose only type variable is \(\alpha\). It defines the class predicate

\[ \mathsf{of\_class}_c: \mathsf{itself}(\alpha) \to \mathsf{Prop}, \qquad \mathsf{of\_class}_c:= \lambda\_.\; \mathsf{of\_class}_{c_1}(\mathsf{TYPE}[\alpha]) \land \dots \land \mathsf{of\_class}_{c_m}(\mathsf{TYPE}[\alpha]) \land A(\alpha). \]

This is an ordinary definition (Theorem M-5): \(\alpha\) occurs in its type. Isabelle writes \(\mathsf{of\_class}_c(\mathsf{TYPE}[\tau])\) as \(\mathrm{OFCLASS}(\tau, c)\).

Definition Y-9 (sorts, class order, arities). A sort is a finite set of classes, read as their intersection. The class order: \(c \le c'\) when \(c'\) is \(c\) or, transitively, a superclass of \(c\), so that every type of \(c\) is a type of \(c'\). An arity \(\kappa:: (S_1, \dots, S_k)\, c\) states that \(\kappa(\tau_1, \dots, \tau_k)\) belongs to \(c\) whenever each \(\tau_i\) belongs to \(S_i\). Membership \(\tau: S\) is the least relation such that:

  • \(\alpha^{S'}: S\) when every class of \(S\) lies above some class of \(S'\) (a type variable carries its sort);
  • \(\kappa(\bar\tau): S\) when for every \(c \in S\) there is an arity \(\kappa:: (\bar S')\, c'\) with \(c' \le c\) and \(\tau_i: S'_i\) for all \(i\).

A sort \(S\) entails \(S'\) when every class of \(S'\) lies above some class of \(S\); a sort is normalized as the set of its minimal classes. The arities of a theory give each type constructor \(\kappa\) and class \(c\) at most one domain \(\mathrm{dom}(\kappa, c)\): the argument sorts of an arity \(\kappa:: (\bar S')\,c'\) with \(c' \le c\), the weakest when there are several, unique up to equivalence under componentwise sort entailment. The arities must be hereditarily coregular: in every included theory, every nonempty candidate set for a pair \((\kappa,c)\) has a weakest candidate. Equivalently, every pair in that set has a candidate weaker than both. Incomparable domains are allowed when their complete support retains such a bound; an unrelated third branch cannot supply a bound missing from a smaller included theory. Since every candidate at \(c\) is a candidate at \(c'\) whenever \(c \le c'\), the weakest domain at \(c\) entails the weakest domain at \(c'\) componentwise. The most general domain \(\mathrm{mgd}(\kappa, S)\) of \(\kappa\) at a nonempty sort \(S\) is the componentwise union of \(\mathrm{dom}(\kappa, c)\) for \(c \in S\), normalized, defined when every class of \(S\) has a domain. Then \(\kappa(\bar\tau): S\) exactly when \(\mathrm{mgd}(\kappa, S) = \bar S\) is defined and \(\tau_i: S_i\) for each \(i\), so membership is decided by a single descent through the type, without search, and every type has a least sort [Nipkow 1993]. This is Isabelle's order-sorted algebra (its Sorts.mg_domain and Sorts.of_sort), which completes each arity to the superclasses of its class and refuses conflicting arities.

Definition Y-10 (instance declaration). An instance declaration \(\kappa:: (\bar S)\, c\) cites a theorem of the parent theory whose statement is exactly

\[ \mathsf{of\_class}_c(\mathsf{TYPE}[\kappa(\alpha_1^{S_1}, \dots, \alpha_k^{S_k})]), \]

and adds the arity. Class instances of a type are therefore facts proved once, then looked up.

The intrinsic function constructor is included among the binary constructors in the arity and membership definitions (CoreName.ARROW). Its interpretation remains the existing function space; an instance requires the same checked witness and argument-sort obligations as any constructor. Naming it does not assert membership or add a foundation axiom.

Definition Y-10a (later class relation). A relation declaration \(c ≤ d\) cites a closed theorem of the parent with exactly the statement \(\mathsf{of\_class}_c(\mathsf{TYPE}[\alpha]) \to \mathsf{of\_class}_d(\mathsf{TYPE}[\alpha])\), for one unsorted type variable. It adds a directed edge only in theories containing the declaration. The graph of initial superclass edges and visible relation declarations must remain acyclic. The predicates remain unchanged.

The witness holds at every ground type, so the new edge preserves the implication used in Lemma Y-4. Every ground model of the parent therefore already satisfies the edge: the extension preserves models. Membership carries the relation declarations of its paths, in addition to the instance declarations of its descent. The same requirement applies when entailment removes a sort hypothesis. A relation's witness is a theorem of its parent, so every theory that contains the relation contains the witness. The dependencies therefore list the relation alone, and the witness comes with the relation's after-theory, as an instance's witness comes with the instance. Definition Y-13a fixes which paths are recorded.

Lemma Y-4c (hereditary coregularity). A proposed acyclic theory has coregular arities in every included theory exactly when the following check passes. For every pair of arities for the same constructor, every shared target class, and every pair of superclass paths to that target, form the union of the complete theories after the instance declarations and the relation declarations on the paths. Their domains must be comparable in that supporting union, or it must contain an actual candidate for the same constructor and that exact target class, with a domain weaker than both. The candidate's eligibility and both entailments must hold in that union.

Proof. Necessity: every supporting union is included in the proposed theory and makes the two arities candidates for the target. Its weakest candidate is a bound. Sufficiency: any included theory with two competing arities contains their complete declaration theories and a pair of superclass paths to the target. It therefore contains the corresponding supporting union. A comparable pair has one member as a bound; otherwise the check supplies a candidate bound. Eligibility and entailment persist under extension, so every pair in the included theory's candidate set has a bound in that set. For a finite nonempty set, repeatedly take a bound of the accumulated bound and the next candidate. The final candidate is weaker than every member, and any two such candidates entail each other. \(\square\)

Corollary Y-4d (merges that add no order). Let \(U = A \cup B\) for theories \(A\) and \(B\), and let \(D = U \setminus A\). If \(D\) contains no instance, class, or class relation declaration, \(U\) has coregular arities in every included theory. Proof. The check of Lemma Y-4c ranges over pairs of arities, superclass paths, and the supporting unions of their declarations. With no such declaration in \(D\), all of them belong to \(A\), and their supporting unions are included in \(A\). Every interned theory has coregular arities in every included theory, whether its pairs were checked at once or incrementally as it was extended, so the check passes for \(A\). \(\square\) A merge therefore rechecks coregularity only when the side it adds has one of these declarations.

Restrictions on rechecks

Lemmas Y-4e and Y-4f below justify restrictions of Lemma Y-4c's check. The theory pass's checks of class relations and merges (check_class_relation and check_merge, in judgment_core's checker/sorts.mojo) apply these restrictions. These results concern the mathematical accept/refuse verdict with sufficient resources; they assert neither equal work charges nor equal LIMIT margins. They do not change declaration order, witness selection, or any other extension or merge check.

For a committed declaration \(d\), write \(H(d)\) for its complete after-theory. For a proposed declaration that has no after-theory yet, write \(H(d)\) for its declared parent together with \(d\). These are declaration sets, not a request to intern an unchecked theory. An included theory containing \(d\) contains \(H(d)\); once \(d\) is committed, that support never grows when a later theory extends it. All comparisons below use the same declarations with the same records and supports, rather than declarations reconstructed in a different order.

Fix two distinct instance declarations \(i,j\) for one constructor \(\kappa\), with result classes \(c_i,c_j\). Let \(R_T(i,j)\) be the classes reachable upward from either \(c_i\) or \(c_j\) in \(T\), including the starting classes. The upward closure data includes those classes' initial superclass edges and every visible relation declaration whose subclass lies in \(R_T(i,j)\). Relation declarations are retained individually: two paths with the same endpoints can have different complete supports. For paths \(p:c_i\leadsto t\) and \(q:c_j\leadsto t\), their check item has support

\[ S(p,q)=H(i)\cup H(j)\cup \bigcup_{e\text{ a relation declaration on }p\text{ or }q} H(e). \]

The domains are comparable in \(S(p,q)\), or there is an actual candidate for \((\kappa,t)\) in \(S(p,q)\) weaker than both, with its eligibility and both entailments decided there (Lemma Y-4c). An initial superclass edge adds no separate relation support: its source class's declaration already contains its initial superclasses. The initial classes are supported by \(H(i)\) and \(H(j)\); after a later edge, its target class is supported by \(H(e)\). Induction along either path therefore supplies every initial edge's class support without adding the ambient theory.

Observation O (locality of a pair check). If two proposed acyclic scopes contain \(i,j\) and agree on their upward closure data, their check items for this pair and the pass/fail verdict of _check_pair agree.

Proof. The initial class records and the available later edges are the same, so induction on path length gives exactly the same paths from both starting classes, hence the same shared targets and path pairs. Each \(H(d)\) is fixed, so corresponding items have equal supports. This equality includes all declarations of each supporting parent, not only the classes on the paths. It therefore preserves candidate membership, candidate eligibility, and domain entailment even when those queries use classes outside \(R_T(i,j)\).

For the implementation, take two calls of _check_pair that differ only in their ambient scope. Each first tests comparability in the equal base support \(H(i)\cup H(j)\). Its walk from \(c_i\) and from \(c_j\) follows, at each reached class, the superclass edges of the class's declaration and then the relation declarations from that class that the ambient scope contains, in creation order. Those filters agree by the upward-closure hypothesis, and each step along a relation adds the same \(H(e)\) to the path's key, so both walks record the same states in the same order. A state is dropped by comparing keys and the flag that marks paths through a required relation, which agree as well. Each item is then decided in its support \(S(p,q)\) alone: candidate membership, the candidate's class order, and domain entailment are read there, never in the ambient scope, so neither call can use an ambient candidate or ambient entailment. An item is skipped by tests of \(S(p,q)\) against \(H(i)\), \(H(j)\), the \(H(e)\) of its relations, and the joined theories of a merge, which agree too. The acyclic, finite path graph terminates the walk, giving the same verdict. This argument does not identify the charges of different invocations or promise their completion under an arbitrary finite budget. \(\square\)

Lemma Y-4e (restricted class-relation recheck). Let \(T\) be hereditarily coregular, and let \(T'=T\cup\{r\}\) propose a class relation \(r:\mathit{sub}<\mathit{sup}\) whose visibility, witness, and acyclicity checks pass. Then \(T'\) is hereditarily coregular exactly when Lemma Y-4c's items pass in \(T'\) for every pair \(i,j\) with

\[ c_i\le_T\mathit{sub}\quad\text{or}\quad c_j\le_T\mathit{sub}. \]

The same selection results from using \(\le_{T'}\) in this condition.

Proof. No instance or class declaration is added, so the instance pairs are exactly those of \(T\). If a path to \(\mathit{sub}\) in \(T'\) used \(r\), the part up to its first use already reaches \(\mathit{sub}\) using only edges of \(T\); furthermore, in the acyclic proposed graph it cannot return to \(\mathit{sub}\) after that use. Thus \(c\le_{T'}\mathit{sub}\) iff \(c\le_T\mathit{sub}\).

For an unselected pair, neither starting class reaches \(\mathit{sub}\). A path using the only new edge would first have to reach that class, a contradiction. The pair's upward closure data is therefore unchanged. Observation O gives exactly the items and pair verdict already valid in \(T\). This also excludes a new bound or new domain entailment supplied by \(r\) for this pair: the old supports are fixed and none gains \(r\). Changes in the full ambient order of argument-sort classes alone do not change a support-local query. For selected pairs, perform the check in the proposed scope, including \(H(r)=T\cup\{r\}\) whenever a path uses \(r\). These and the unselected pairs exhaust Lemma Y-4c, proving sufficiency. Necessity follows by applying that lemma to each selected item. The argument includes a pair for which only \(c_j\) reaches \(\mathit{sub}\); selection is the disjunction for both endpoints, whatever orientation the existing creation-order enumeration gives the pair. \(\square\)

Lemma Y-4f (restricted merge recheck). Let \(A,B\) be hereditarily coregular theories, and let \(U=A\cup B\) pass the other merge checks, in particular acyclicity. A pair \(i,j\) is covered by a side \(X\in\{A,B\}\) when \(i,j\in X\) and every relation declaration of \(U\) whose subclass lies in \(R_U(i,j)\) also lies in \(X\). Then \(U\) is hereditarily coregular exactly when Lemma Y-4c's items pass in \(U\) for every pair covered by neither side.

Proof. Consider a pair covered by \(X\). Its starting classes belong to \(X\) because its two instances do. Induct along any upward path of \(U\). For an initial superclass step, the immutable source-class declaration in \(X\) includes its superclass declaration. For a later step, its subclass has been reached in \(R_U(i,j)\), so coverage puts that relation in \(X\); the relation's complete support puts its target class there as well. Thus every path of \(U\) from either starting class is a path of \(X\). Conversely every such path of \(X\) is in \(U\) because \(X\subseteq U\). All relation declarations leaving these reached classes, including alternative edges that add no new reachable endpoint, belong to \(X\). The upward closure data agrees.

For every corresponding item, \(H(i)\), \(H(j)\) and all its \(H(e)\) are included in \(X\). Their union is the same declaration set whether the ambient scope is \(X\) or \(U\). A candidate that lies in the other side but not in \(X\) cannot enter this support. A candidate present on both sides can enter it only if it was already in that same support in \(X\). Likewise no relation available only in the other side may be used to prove eligibility or entailment in this support. Observation O applies to both the items and the actual pruning decisions, so the pair's verdict is the passing verdict in \(X\). All other pairs are checked in \(U\); Lemma Y-4c gives sufficiency, and also necessity for those checked pairs. If \(U\setminus A\) contains no instance, class, or class relation, every pair and every relation of its upward closure belongs to \(A\). Hence every pair is covered by \(A\), recovering Corollary Y-4d. \(\square\)

Why containing both instances is insufficient. Over a shared base, let \(A\) add a relation \(c\le e\) and let \(B\) contain \(i=\kappa::(\{p\})c\) and \(j=\kappa::(\{q\})e\), with \(p,q\) incomparable in all the supports in question, no shared target for \(c,e\) in \(B\), and no other instance of \(\kappa\). The individual sides pass. In \(U\), \(e\) is a shared target, and neither of the only two candidates is weaker than both, so this item fails. Side \(B\) does not cover the pair: \(c\le e\) has its subclass in \(R_U(i,j)\) but is absent from \(B\). Side \(A\) lacks the instances. The restricted check therefore retains the pair and refuses it. A third-branch candidate outside its complete support cannot rescue this failure, even when that candidate is visible in \(U\).

Lemma Y-4g (one item per support). Fix a check of Lemma Y-4c's items in an ambient theory \(T\), possibly restricted to items with a given relation \(r\) on a path. Write \(K(p)=\bigcup_{e \text{ on } p} H(e)\) for the key of a path \(p\). This is a version vector, the entrywise maximum of the \(H(e)\) (Lemma T-8). Enumerate the upward paths from each instance class. Record a path ending at a class \(c\) only when no path already recorded at \(c\) that agrees with it on whether \(r\) is on the path has a key included in its key. For each pair of recorded paths to a shared target, let the item be either checked in its support \(S(p,q)\), or skipped because \(S(p,q)\) lies in a theory \(X\) that is hereditarily coregular. That theory is \(H(i)\), \(H(j)\), \(H(e)\) for a relation \(e\) on \(p\) or \(q\), or, in a merge, one of the joined theories. Then every item of every pair of paths passes exactly when every checked item passes.

Proof. (a) An item depends only on its target and its support (Observation O). Passing is monotone in the support: if the item passes in \(S\), it passes in every \(S' \supseteq S\), since comparability of the domains only gains edges, and a weaker candidate stays visible, below the target and entailed.

(b) A skipped item passes. Every relation on \(p\) and \(q\), and with it every class they reach (its complete support), lies in \(S(p,q) \subseteq X\), and so do \(i\) and \(j\). Hence \(p\) and \(q\) are paths of \(X\). \(S(p,q)\) is a join of after-theories, so it is itself a theory included in \(X\) (Lemma T-8). Because \(X\) is hereditarily coregular, its arities are coregular in \(S(p,q)\), which is where the item is evaluated. Each candidate \(X\) is hereditarily coregular, by induction on creation order: - \(H(i)\), \(H(j)\) and \(H(e)\) are the after-theories of declarations checked when they were added (Lemma Y-4e and the instance check). - A merge side is the after-theory of an entry's last declaration. That is a checked parent extended by declarations that were each checked when added. Declarations other than instances and relations add no arity or edge. - A parent that is itself a merge row was decided by its own merge check, or needed none because one of its sides includes it. Theories are decided in creation order, and a segment's base is included in it.

(c) Pruning holds across skipping. Let a path \(p'\) be dropped at \(c\) because a recorded path \(p\) has \(K(p) \subseteq K(p')\). Every extension of \(p'\) to a target extends \(p\) by the same steps, which adds the same \(H(e)\) to both keys and keeps the inclusion. Its items therefore have supports including those of the corresponding recorded items. Each of those recorded items passes, whether checked or skipped by (b), so by (a) the dropped item passes. Whether the dropped item would itself have been skipped does not matter, because skipping only saves work.

(d) Nothing valid is refused. A recorded pair is a pair of actual paths of \(T\), and Lemma Y-4c requires its item whatever else holds. \(\square\)

The checker's walk keeps one state per class and key. For a single chain of declarations, the \(H(e)\) are nested, and \(n\) stacked diamonds leave at most one state per class and relation instead of \(2^n\). In a merge of branches built independently, the keys range over vectors whose entry for segment \(s\) is one of the \(n_s\) values that the \(H(e)\) give it. A class then holds at most \(\prod_s (1+n_s)\) states, which is \((n+1)^2\) for two branches of \(n\) relations each. The product ranges over every segment the \(H(e)\) touch, base segments included. It is polynomial for a fixed number of segments, but that number grows when the branches are themselves chains of merges. Keying a merge by its sets of relation declarations instead would admit \(2^n\) incomparable sets for the same two ladders.

The checker and witness selection

The theory pass checks a declaration row once it is committed, so \(H(d)\) is the theory row just after \(d\). A support is a join of version vectors (Lemma T-8), built in the check's scratch, and membership in it compares a declaration's segment and position with the vector's entries. A new instance \(d\) is checked against each earlier instance of its constructor visible in its parent, in \(H(d)\) (check_instance). _check_pair decides each item once, by its target and its support: recorded paths whose keys have the same join give the same item. Every shared target is checked: a bound for an ancestor does not necessarily qualify for a more specific target. Comparable domains in \(H(i)\cup H(j)\) pass the whole pair; a candidate bound passes only the item for its target. Acyclicity makes the walk finite; its depth is bounded, and its steps, membership tests and item checks are charged to the check's allowance. Exhaustion reports LIMIT.

For a later class relation \(r\), check_class_relation applies Lemma Y-4e. For each constructor it enumerates the pairs of instances visible in the parent, in creation order, and checks in \(H(r)\) those of which either class reaches the subclass of \(r\) there, and of them only the items with \(r\) on a path. The theory pass's class-order cache keeps each reachability answer for the pass and charges a repeated question the units its walk charged, so it changes no decision and no charge.

For a merge, check_merge needs nothing when the merged theory is included in one of its joined theories, the after-theory of an entry's last declaration: that theory was checked, and acyclicity and hereditary coregularity hold in every theory it includes. Otherwise it checks that the order stays acyclic and that each constructor has one arity per class, and then every pair of visible instances. It skips each item whose support lies in a joined theory, or in the after-theory of \(i\), \(j\), or a relation on its paths: that item passed when that theory was checked (Lemma Y-4g(b)). The unit skipped is an item, not a pair, so a pair that a side covers in the sense of Lemma Y-4f has all its items skipped when that side is one of the joined theories.

After this invariant is certified, a single creation-order scan finds a weakest candidate: replace the current candidate when it entails the new one. At the first weakest candidate the current one entails it; every later replacement is equivalent to it. Thus the scan returns the last equivalent minimum in creation order. Reconstruction can change that order, so proof replay must preserve the checked statement and dependencies rather than assume an order-independent choice of equivalent witness.

Structural and semantic sorts. Type-variable identity uses a stable structural sort, normalized using only initial definitional superclass edges. Semantic entailment and membership use Definition Y-9 with the theory-scoped order. Newly redundant atoms may remain in a structural sort. Their intersection has the same ground meaning, and preserving them prevents later relations from identifying formerly distinct variables with the same name. Membership query keys preserve these atoms and check every requested class, retaining evidence for any implication that makes an atom redundant.

Definition Y-11 (sorted type variables, admissible substitutions). Every type variable carries a sort, the empty sort by default. A type substitution \(\theta\) is admissible in a theory when \(\theta(\alpha^S): S\) there for every \(\alpha^S\) it moves. The rule use and every type instantiation require admissible substitutions.

A membership \(\tau: S\) is decided with the arities of the theory, so it depends on the instance declarations whose arities its descent uses. A use at an admissible substitution and an of_class step therefore count those instance declarations among \(\operatorname{decls}(\mathcal D)\) (Definition D-4), and a type variable \(\alpha^S\) counts the classes of \(S\) as a type constructor counts its declaration, so that the home theory (Definition D-5) contains them and the membership holds in every theory that includes it (Theorem D-8). Without them a judgment could be used in a theory that includes its home but not the instance, where the membership fails.

Definition Y-12 (semantics of sorts). In a ground interpretation, a ground type \(\tau\) belongs to class \(c\) when \(\mathsf{of\_class}_c(\mathsf{TYPE}[\tau])\) holds, and to a sort when it belongs to each of its classes. A ground type substitution respects sorts when it maps each \(\alpha^S\) to a ground type belonging to \(S\). A judgment with sorted type variables is valid when every ground instance of it under a sort-respecting substitution is valid (Definition Y-6).

Lemma Y-4 (membership is sound). In every ground model of a theory, if \(\tau: S\) by Definition Y-9, then \(\tau\theta\) belongs to \(S\) for every sort-respecting ground substitution \(\theta\).

Proof. Induction on the derivation of \(\tau: S\). For a type variable \(\alpha^{S'}\): \(\theta(\alpha)\) belongs to \(S'\) because \(\theta\) respects sorts, and a type belonging to \(c'\) belongs to every \(c \ge c'\), because \(c\)'s predicate is a conjunct of the predicates of its subclasses. For \(\kappa(\bar\tau)\): each arity was added by an instance declaration citing a theorem, which holds in the model at every sort-respecting ground instance, in particular at \(\alpha_i \mapsto \tau_i\theta\), which respects \(S'_i\) by the induction hypothesis. \(\square\)

Definition Y-12a (witnessed sorts). The witnessed sorts of a theory form the least set \(W\) of normalized sorts that contains the empty sort, and contains \(S\) whenever some type constructor \(\kappa\) has \(\mathrm{mgd}(\kappa, S) = (S_1, \dots, S_k)\) with every \(S_i \in W\).

Lemma Y-4a (witnessing). A sort is witnessed in a theory exactly when some ground type belongs to it by Definition Y-9. Whether it is, is decidable, and a witnessed sort stays witnessed in every larger theory.

Proof. If a ground type \(\kappa(\bar\tau)\) belongs to \(S\), then \(\mathrm{mgd}(\kappa, S)\) is defined and each \(\tau_i\) belongs to \(S_i\), so by induction on the type each \(S_i\), and therefore \(S\), is in \(W\); every ground type belongs to the empty sort. Conversely, by induction on the stage at which \(S\) enters \(W\), a ground type of \(S\) is \(\kappa(\bar\tau)\) for ground types \(\tau_i\) of the \(S_i\), and \(\mathsf{Prop}\) belongs to the empty sort. Only the sorts reachable from \(S\) by most general domains matter; they are sets of the theory's finitely many classes, so iterating the closure over them until nothing changes decides membership in \(W\). A larger theory has more arities, whose domains are defined at more pairs and only weaker, so \(W\) grows. \(\square\)

Isabelle's own search (Sorts.witness_sorts) skips a constructor whose domain is no weaker than the sort sought, so it may keep a sort hypothesis that the least fixed point removes; removing more is sound by the lemma.

Definition Y-13 (sort hypotheses). Every judgment carries a finite set of sorts, its sort hypotheses. Write \(\mathrm{tv}(\Gamma \vdash A)\) for the type variables with a nonempty sort that occur in \(A\), in the hypotheses of \(\Gamma\), or in the types of its locals. A rule with premises \(J_1, \dots, J_n\) and conclusion \(J\) gives \(J\) the union of the premises' sort hypotheses and the sort \(S\) of every \(\alpha^S\) in \(\mathrm{tv}(J_1) \cup \dots \cup \mathrm{tv}(J_n)\) but not in \(\mathrm{tv}(J)\), unless \(S\) is redundant for \(J\): witnessed in the home theory of \(J\)'s derivation (Definitions Y-12a and D-5), or entailed by the sort of a variable in \(\mathrm{tv}(J)\). A sort redundant because it is witnessed brings, into \(J\)'s dependencies, the instance declarations whose arities build a ground type of it, and Proven adds those of each sort hypothesis it removes to the theorem's closure. The admissible rules count as rules here: export and transport record the sorts of the variables that leave with the entries they skip or map, as the derivations of Theorems D-4 and D-5 would. The home theory is included in every theory in which \(J\) is used (Theorem D-8), so a sort witnessed there stays witnessed (Lemma Y-4a); a sort witnessed only by an instance declaration outside the home could have no type in the home. Recording a hypothesis that \(J\)'s theory witnesses is sound, and Proven removes it. Terms and types a rule is given are not premises, with one exception: the premise of use is the cited declaration's statement at its type arguments. Every type parameter occurs in that statement, so the sorted variables of the type arguments count as a premise's, and a sort is recorded when type substitution, the lifting of the statement to the result's signature, or the quantifiers use instantiates, as forall_elim would, lose one of them. The rule unconstrain (Definition Y-14) adds nothing for the variable it unconstrains. A Proven declaration removes the sort hypotheses of its judgment that are witnessed in its parent theory, and refuses any other; its statement is read in the parent's signature, and the sort of a sorted variable that this reading loses counts as a sort hypothesis too.

Definition Y-13a (recorded evidence). Dependencies are compared exactly, so the declarations that Definitions Y-10a, Y-11 and Y-13 count are those of one fixed choice of evidence. In a theory \(T\) it is the following. Atom order is the stored order of a sort node's or sorted type variable's class atoms.

  1. Class path. \(c \le d\) is searched breadth-first from \(c\). A class's edges are taken in a fixed order: first its initial superclasses, in the order its declaration lists them, then the relation declarations from it that \(T\) contains, in commit order. Each class is entered once, at its first discovery, and the search stops when \(d\) is discovered. The evidence is the relation declarations on the tree path from \(c\) to \(d\). A superclass edge records nothing, because its source class's declaration contains it. Of several paths in a diamond, only this one is recorded.
  2. Weakest arity. \(\mathrm{dom}(\kappa, b)\) is given by a creation-order scan of \(\kappa\)'s instances in \(T\) whose class lies below \(b\). The current candidate is replaced when its domain entails the next one's componentwise. The scan itself records nothing.
  3. Membership. \(\tau : S\) is decided by one descent through \(\tau\). A type variable \(\alpha^{S'}\) records the evidence of \(S'\) entailing \(S\) (item 4). For \(\kappa(\bar\tau)\), each class \(b\) of \(S\) is taken in atom order. The descent records the instance \(x\) of item 2 and the class path from \(x\)'s class to \(b\), then checks each \(\tau_m\) against \(x\)'s own \(m\)-th argument sort, recursively. It goes through each class's instance, not through the normalized \(\mathrm{mgd}(\kappa, S)\). In a coregular theory both decide the same membership.
  4. Entailment. For \(S'\) to entail \(S\), each class \(b\) of \(S\) is taken in atom order. The first class of \(S'\) in atom order with a path to \(b\) gives that path's evidence.
  5. Witnessing. Sorts are normalized as their minimal classes under the initial superclass edges alone, ascending by declaration. A class above another only through a relation stays, so no relation is used without being recorded. The constructors are those with an instance in \(T\), in the commit order of their first such instance.
  6. Reachable sorts. The list starts with \(S\) normalized. For each listed sort in turn, and each constructor \(\kappa\) in order where \(\mathrm{mgd}(\kappa, \cdot)\) is defined, each nonempty component not yet listed is appended. Here \(\mathrm{mgd}\) is the componentwise union of item 2's domains, normalized.
  7. Fixed point. Passes are made over the listed sorts in order. A sort not yet admitted is admitted by the first constructor whose \(\mathrm{mgd}\) components are all admitted, counting admissions earlier in the same pass. The passes repeat until one admits nothing.
  8. Evidence. It is collected depth-first from \(S\), with the components of each admitted sort pushed in order and taken from the top. For each admitted sort, once, and each of its classes \(b\) in ascending order, it records item 2's instance for the admitting constructor and its class path to \(b\).
  9. Redundancy (Definition Y-13). The lost variables are taken in node order, and for each the kept variables in node order. The first kept variable whose sort entails the lost one's sort gives item 4's evidence. Otherwise witnessing in the home gives item 5's evidence. Otherwise the sort is recorded.

Each choice is deterministic, and it uses only declarations of the theory in which the answer is decided. A published answer (Definition LC-13) records the same set as a computed one. An independent implementation must make these choices to match the recorded dependencies exactly. Any other choice gives a set that is sound but different.

Theorem Y-5 (soundness with sorts). If \(\Gamma \vdash A\) is derivable with sort hypotheses \(H\), it is valid (Definition Y-12) in every ground model in which each sort of \(H\) is inhabited by some ground type; in particular in every ground model if each sort of \(H\) is witnessed.

Proof. As Theorem M-1, by induction on the derivation, read at ground instances under sort-respecting substitutions. The only new case is a rule whose premise mentions a type variable \(\alpha^S\) absent from the conclusion: the induction hypothesis holds at every sort-respecting ground instance of \(\alpha^S\), and one exists exactly when \(S\) is inhabited, which the sort hypothesis records. Without it, a premise about a sort with no types would hold vacuously and could be discharged into a conclusion that fails. A witnessed sort is inhabited in every ground model by Lemmas Y-4 and Y-4a. A sort entailed by the sort \(S'\) of a variable of the conclusion is redundant: if it has no ground type, neither has \(S'\), and the conclusion has no sort-respecting ground instance. unconstrain's conclusion holds at every ground instance of \(\alpha\): by the premise when the instance belongs to \(S\), and vacuously otherwise, its class premises being false. The rules use with an admissible substitution and of_class (Definition Y-14) are sound by Lemma Y-4, and use is the case above when its type substitution, lifting, or instantiated quantifiers lose a sorted variable of its type arguments. Without that premise, from the axiom \(\forall x{:}\alpha^S.\, \mathsf{False}\) of a sort \(S\) with no types, which holds vacuously, use with the term \(u[\alpha^S]\) would conclude \(\mathsf{False}\) in one step. \(\square\)

Isabelle's kernel keeps the same record (its shyps) for the same reason, and removes the same redundant sorts (Thm.strip_shyps), witnessed in the theory of the theorem; the kernel decides witnessing in the home theory, because its judgments move between theories (Theorem D-8).

Proposition Y-5a (type definitions at unwitnessed sorts). Let a type definition (Definition Y-1) have sorted parameters \(\alpha_1^{S_1}, \dots, \alpha_k^{S_k}\), its witness \(\vdash \exists x{:}\sigma.\, P\,x\) a theorem of the parent \(T\) without sort hypotheses, and suppose some \(S_i\) is not witnessed in \(T\). The definition is a conservative extension of \(T\), vacuous at the parameters: its theorems constrain \(\kappa\) only at sort-respecting ground instances, of which \(T\) has none. In any later theory \(T' \supseteq T\) in which every \(S_i\) is witnessed (an instance declared after the definition), its instances at sort-respecting ground types are sound.

Proof. Read at ground instances (Definition Y-12): the definition's theorems are valid when they hold at every ground instance under a sort-respecting substitution \(\theta\) of \(\bar\alpha\). By Theorem Y-5 the witness holds at every such \(\theta\) in every ground model of \(T\), since it has no sort hypothesis. Expand a model \(M\) of \(T\) as in Theorem Y-1, at each argument tuple \(\bar X\) whose sets belong to the sorts, with \(S(\bar X)\), nonempty by the witness; elsewhere \(M(\kappa)(\bar X)\) is any nonempty set of \(\mathcal U\), on which no theorem of the definition speaks. Every model of \(T\) so expands, so the extension is conservative; while some \(S_i\) has no ground type in \(T\) (Lemma Y-4a), the first case never arises at ground types of \(T\), so the definition is vacuous there. In \(T'\), an arity makes a ground type \(\tau_i\) belong to \(S_i\) (Lemma Y-4); then \(\theta = \bar\alpha \mapsto \bar\tau\) respects sorts, the witness holds at \(\theta\) by Theorem Y-5 read in \(T'\) (the witness's judgment moves into \(T'\) unchanged, Theorem D-8), and the expansion's first case applies: \(\mathsf{abs}\) and \(\mathsf{rep}\) are a bijection with the nonempty \(S(\bar\tau)\), so the definition's theorems hold there as in Theorem Y-1. \(\square\)

A sort that a derivation loses, by contrast, is recorded (Definition Y-13) and must be witnessed when a Proven declaration is admitted: a lemma that uses \(\alpha^{S}\) only in a premise, with \(S\) unwitnessed, would otherwise discharge a vacuous premise into a ground conclusion (Theorem Y-5). A type definition's witness keeps its parameters in its statement, so it loses none.

Definition Y-14 (the class rules). Two kernel rules relate sorts to class predicates:

  • of_class(Γ, τ, c): if \(\tau: \{c\}\), then \(\Gamma \vdash \mathsf{of\_class}_c(\mathsf{TYPE}[\tau])\);
  • unconstrain(p, α): from \(p: \Gamma \vdash A\) with a type variable \(\alpha^S\) not occurring in \(\Gamma\), the judgment \(\Gamma \vdash \mathsf{of\_class}_{c_1}(\mathsf{TYPE}[\alpha]) \to \dots \to \mathsf{of\_class}_{c_n}(\mathsf{TYPE}[\alpha]) \to A[\alpha/\alpha^S]\), where \(S = \{c_1, \dots, c_n\}\), listed as \(\alpha^S\)'s class atoms are: the classes of its structurally normalized sort in the structural order (T1.6, K2.8), and \(\alpha\) has the empty sort. The premises' order is part of the conclusion, so it is fixed by the variable alone, never by the derivation that made it.

In \(A[\alpha/\alpha^S]\), a spine of a class-level ACU declaration at \(\alpha^S\) is no longer an ACU spine; one of three or more operands is read as Definition M-2 reads an ACU spine, nested to the right, which is its denotation wherever the class premises hold. A type substitution that moves \(\alpha^S\) to a function type, where class-level ACU does not apply (Definition Y-15), reads such a spine the same way.

Theorem Y-6 (classes add no logical strength). Replacing every sorted type variable \(\alpha^S\) by an unsorted \(\alpha\) with the premises \(\mathsf{of\_class}_c(\mathsf{TYPE}[\alpha])\) for \(c \in S\) (the effect of unconstrain), and every use of an arity by the cited instance theorem, translates each derivation with sorts into a derivation without sorts, from the same declarations read as ordinary definitions and theorems.

Proof. By induction on derivations. use at an admissible substitution becomes use followed by discharging the class premises with instance theorems, combined along the derivation of membership (Definition Y-9) by the class predicates' definitions, which supply the superclass conjuncts. of_class becomes the same combination of instance theorems. A sort hypothesis becomes a premise \(\exists\)-quantified over the type, discharged by a witness type. The class predicates are definitions (Theorem M-5), and instance declarations add arities only, so the translated theory has the same ground models. \(\square\)

This is Wenzel's observation that type classes and overloading add no expressive power to HOL [Wenzel 1997], and the translation is how classes are written to formats without them, such as OpenTheory.

Associative and commutative operators at a class

Definition Y-15 (class-level ACU declaration). An AcuOperator declaration may name a constant at a sorted type variable, \(c[\alpha^S]\), citing class-level theorems (associativity, commutativity, and unit, each a scheme over \(\alpha^S\)). Canonical construction then treats every spine \(c[\tau](\dots)\) with \(\tau: S\) as an ACU spine, including \(\tau = \beta^{S'}\) with \(S'\) entailing \(S\), except when \(\tau\) is a function type. An instance declaration of the arrow constructor can put a function type in \(S\), and there, as in Isabelle, application stays curried: \((f \oplus g)(x)\) applies the function \(f \oplus g\) to \(x\). The laws still hold at that type (Theorem Y-7's argument), and canonical construction does not use them there: (Use) at a function type instantiates a declaration's statement in its parent's signature, which every theory that sees the declaration includes, and then reads it in the result's signature, so a cited law states at that type what it says, as in Isabelle, rather than the trivial equation its normal form at \(\alpha^S\) would give; δ unfolds a definition at such a type the same way. A declaration stated where the class-level declaration already applies (a theorem, an axiom, a definition, a fact, or a class predicate) is stored in its normal form at \(\alpha^S\), and at a function type it states what that form says. So the carrier of an ACU spine is never an arrow type, as Definition T-6 requires of an ordinary ACU declaration, and Theorem T-2 holds unchanged. It decides \(\tau: S\) in the theory's signature theory (Definition Y-15a), so that canonical forms depend on the signature theory alone.

Definition Y-15a (the signature theory with classes). For a theory \(\Sigma\), the ACU-relevant classes \(\mathrm{Rel}(\Sigma)\) are the least set that contains the classes of the sorts of \(\Sigma\)'s class-level ACU declarations and, with each class \(d\), the classes of the argument sorts of every arity \(\kappa:: (\bar S)\, c'\) of \(\Sigma\) with \(c' \le d\): of every candidate for a domain \(\mathrm{dom}_\Sigma(\kappa, d)\), not only of the weakest. \(\mathrm{Rel}\) then grows with \(\Sigma\), so the least signature theory below is reached from any start below it, whatever the order of the joins that build a theory. An instance declaration with class \(c\) is relevant to \(\Sigma\) when \(c \le d\) for some \(d \in \mathrm{Rel}(\Sigma)\): its arity, completed to the superclasses of \(c\), then gives a relevant class a domain. The signature theory of a theory \(T\) is the least theory \(\Sigma \subseteq T\) that contains the root theory, \(T\)'s ACU declarations and literal kinds, when \(T\) has a class-level ACU declaration every later class-relation declaration of \(T\) (K2.9), and every instance declaration of \(T\) relevant to \(\Sigma\). This extends Definition T-1, which has the first two clauses only. Without a class-level ACU declaration, class relations and instance declarations play no part in canonical forms, so they stay out of the signature theory; with one, the class order that membership in its sort reads (Lemma Y-4b) is the signature theory's. The least such \(\Sigma\) is reached by iterating from the theory of the first two clauses, adding the instance declarations of \(T\) relevant to the current \(\Sigma\); each round only adds declarations of \(T\), so the iteration ends.

Lemma Y-4b (membership in the signature theory). Let \(S\) be the sort of a class-level ACU declaration of a theory \(T\), with signature theory \(\Sigma\). For every type \(\tau\), \(\tau: S\) in \(T\) exactly when \(\tau: S\) in \(\Sigma\).

Proof. Membership grows with the arities, and \(\Sigma \subseteq T\). Conversely, every class of a sort met in the descent of Definition Y-9 from \(\tau: S\) in \(T\) is relevant: those of \(S\) are, and if \(d\) is, the arities that give \(\mathrm{dom}_T(\kappa, d)\) have classes below \(d\), so their instance declarations are relevant to \(\Sigma\) and belong to it; hence \(\mathrm{dom}_\Sigma(\kappa, d) = \mathrm{dom}_T(\kappa, d)\), and its classes are relevant. The descent in \(T\) is therefore a descent in \(\Sigma\). \(\square\)

Theorem Y-7 (class-level ACU is sound). The equalities canonical construction uses for a class-level ACU declaration hold at every type \(\tau\) with \(\tau: S\) in the signature theory, in every ground model of the theory.

Proof. Such a \(\tau\) has \(\tau: S\) in the theory (Lemma Y-4b). The cited laws are valid under every sort-respecting valuation (Theorem Y-5) at every sort-respecting ground instance, and every ground instance of \(\tau\) belongs to \(S\) (Lemma Y-4), so the laws hold for the operation \(c\) at that type; Theorem C-1's argument for ACU spines then applies unchanged. \(\square\)

Canonical form now depends on membership, which instance declarations change. By Definition Y-15a, an instance declaration that can change membership in a sort a class-level ACU declaration uses extends the signature theory, as ACU declarations and literal kinds do, and terms built before it are lifted when they are used past it; one that cannot leaves the signature theory, and every term, as they were. A merge's signature theory is computed by the same iteration over both sides, so an instance declared in a sibling without class-level ACU becomes part of it when the merge needs it. Overlap is checked by renaming type variables apart and unifying, with type arguments unified without their sorts: a class-level ACU declaration for \(c[\alpha^S]\) overlaps every other ACU declaration of \(c\) whose type arguments unify with \(\alpha^S\), and the later one is refused. When an instance declaration or a merge enlarges the signature theory, The condition that no instance a literal kind binds to an operation other than ADD and MUL is an ACU instance is checked again.

Soundness in ground models

Theorems M-1 and C-10 are stated for the models of Definition M-3, which interpret a constant by the sets its type arguments denote. Overloaded constants need ground models (Definition Y-6), so the kernel's soundness is restated for them, and with sort hypotheses.

Theorem Y-8 (the kernel is sound in ground models). Let \(T\) be a theory of the kernel and \(M\) a ground model of \(T\).

  1. If \(t \equiv u\) in \(T\), then under every sort-respecting ground substitution the instances of \(t\) and \(u\) denote the same value in \(M\), under every local and binder valuation.
  2. If \(\Gamma \vdash A\) is derivable in \(T\) with sort hypotheses \(H\), and each sort of \(H\) has a ground type belonging to it in \(M\), then \(\Gamma \vdash A\) is valid in \(M\) (Definition Y-12).

In particular a judgment whose sort hypotheses are witnessed in its home theory is valid in every ground model of every theory that includes the home.

Proof. As for Theorems M-1 and C-10, jointly, by induction on theories under strict inclusion, and for each theory by induction on the derivation, proving part 2 in the form: the conclusion holds at every sort-respecting ground instance (Definition Y-12), under every valuation that satisfies its context. We give the changes. Each premise's sort hypotheses are among the conclusion's (Definition Y-13), so the induction hypothesis applies to every premise. Given a sort-respecting ground substitution \(\theta\) for the type variables of a step's conclusion, extend it to those of its premises that the conclusion lacks: a variable whose sort is in \(H\) goes to a ground type belonging to that sort, which exists by assumption; one whose sort is entailed by the sort of a kept variable \(\beta\), to \(\theta(\beta)\), which belongs to it because a class's predicate is a conjunct of its subclasses' (Definition Y-8); one whose sort is witnessed in the home theory, to a ground type of that sort there (Lemma Y-4a), which belongs to it in \(M\) (Lemma Y-4); and one of the empty sort, to any ground type. The premises hold at the extended substitution, and each rule instantiates its premises and its conclusion by the same substitution, so Theorem M-1's case for the rule applies at that ground instance, with Definition M-2's clauses replaced by Definition Y-6's: a constant instance \(c[\bar\tau]\) at ground \(\bar\tau\) denotes \(M(c[\bar\tau])\), and Lemma M-L2 holds because a ground instance of \(t\theta'\) is a ground instance of \(t\). The cases that cite a declaration are:

  • (Use) at a substitution \(\sigma\), admissible in \(T\): by Lemma Y-4, \(\sigma\) followed by the ground substitution is sort-respecting for the declaration's own type variables. An axiom, a definition's or an instance definition's equation, and a fact of a type definition or a specification hold at every such instance by Definition Y-6. A theorem's closed judgment is derivable in its parent, a theory strictly included in \(T\), and Proven accepted it only with sort hypotheses witnessed there, which therefore have ground types in the reduct of \(M\) (Lemmas Y-4a and Y-4); by the joint induction it holds at every sort-respecting ground instance in that reduct, and the predicates of its classes denote the same there.
  • of_class and unconstrain (Definition Y-14): as in Theorem Y-5.
  • (Conv), which is part 1: each generator of \(\equiv_U\) (Definition C-1) holds at ground instances. β and the let rule (ζ) do by Lemma M-L1. A transparent definition unfolds, at a ground instance, to its body at that instance (Definition Y-6). An ACU equation for a declaration whose type arguments have no sorts holds by its cited laws, which are valid in the reduct of \(M\) to their theories by the joint induction; for a declaration at a class it holds by Theorem Y-7, whose proof uses part 2 only for those strictly smaller theories. A literal kind's laws are valid at its carrier, so Theorem C-11's embedding exists in \(M\), and literal evaluation is valid as in Theorem C-10.
  • Lifting and the moves of Theorems D-7 and D-8 preserve derivability; a reduct of a ground model to a smaller theory is a ground model of it, since each clause of Definition Y-6 concerns one declaration. Export and transport are derivable (Theorems D-4 and D-5); both record the sorts of the variables that leave with the entries they skip or map, which is Definition Y-13 applied to those derivations.

The remaining rules mention no declaration, and their cases are those of Theorem M-1 at the ground instance. For the last sentence: by Theorems D-8 and D-7 the judgment is derivable, with the same sort hypotheses, in every theory \(T'\) that includes its home theory, and a sort witnessed in the home is witnessed in \(T'\) (Lemma Y-4a), so it has a ground type in every ground model of \(T'\) (Lemma Y-4). \(\square\)

Theorem Y-3 gives a ground model to every theory built from a consistent HOL theory by definitions, type definitions, and instance definitions that satisfy the overloading conditions. In it \(\mathsf{False}\) denotes \(0\), so by Theorem Y-8 the kernel derives \(\vdash \mathsf{False}\) in no such theory with sort hypotheses that the theory witnesses. On theories without instance definitions, Theorem M-1 stands as proved and says more, since it holds under every valuation of the type variables, not only at ground types; Theorem Y-8 is what overloading needs.

Why the kernel, and what it costs

Isabelle's HOL session has 242 classes and 1,831 instance declarations over 87 type constructors, and 10,950 of its 33,673 theorems (32%) hold for a class (measured on Isabelle2025-2). With classes in the kernel these theorems are stated and stored once, used at a type by one membership check (Definition Y-11), indexed by their constant heads, and replayed from Isabelle's proofs close to step for step. Without them, each would need its operations as explicit arguments (Theorem Y-6's form), or one copy per instance type: about 97,000 theorems for HOL alone, and no finite set of copies for parametric instances such as polynomials over any commutative ring.

The cost is trusted code: membership and coregularity (Definition Y-9), admissibility in every instantiation (Definition Y-11), sort hypotheses (Definition Y-13), the overloading conditions (Definition Y-5), and class-level ACU (Definition Y-15). They are listed in the trusted base and specified in the theory mechanisms.

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