Proof authoring theory

Matching and metavariables

Theorem lookup and rewriting need to find instances of a theorem's pattern in a term. Because the kernel's terms are canonical (associative and commutative operators flattened and sorted, units removed, literals folded), matching must work modulo the same equations.

Patterns and matches

Definition M-1 (pattern). A pattern is a canonical term whose pattern variables are designated locals (in practice, the parameters of a guarded theorem, opened as locals). A pattern is first-order when no pattern variable is the function of an application. A match of pattern \(p\) against a bound-closed canonical term \(t\) is an assignment \(\sigma\) of bound-closed terms to the pattern variables, each of the variable's type, such that \(N(\sigma p) = t\), where \(N\) is the kernel's canonical form (its Definition C-4) and \(\sigma p\) is \(p\) with the variables replaced.

Because the kernel rebuilds a substituted term canonically, \(N(\sigma p)\) is exactly what the session's application and substitution produce from \(p\) and \(\sigma\).

The algorithm

Algorithm M-1 (matching). Match \(p\) against \(t\) with a partial assignment, depth first, with an explicit stack (G2):

  1. If \(p\) is a pattern variable: if it is assigned, require its value to be \(t\) (identity); otherwise assign \(t\) if the types agree.
  2. If \(p\) has no pattern variable: require \(p = t\) (identity).
  3. If \(p\) is an application with a rigid head that is not an ACU instance (a constant instance, a local that is not a pattern variable): require \(t\) to have the same head and arity, and match the arguments pairwise.
  4. If \(p\) is a spine of an ACU instance \(c\) with arguments \(p_1, \dots, p_k\): let \(A\) be the multiset of \(t\)'s atoms for \(c\) (its arguments if \(t\) is a spine of \(c\), empty if \(t\) is its unit, otherwise \(\{t\}\)). Distribute atoms among the pattern arguments, trying choices in the canonical order of \(A\) (the kernel's structural order, which is deterministic). Each argument takes a sub-multiset: the unit if it takes none, the atom itself if it takes one, the spine of \(c\) over its atoms otherwise. A pattern variable already assigned must equal the canonical form of its sub-multiset. Rigid arguments are recursively matched against their groups. Allowing groups for these arguments also covers nested ACU terms that collapse to this operator or its unit after substitution.
  5. Literals match only identical literals (no arithmetic splitting).
  6. Every candidate assignment is verified: the kernel builds \(N(\sigma p)\), which must be \(t\) (identity).

The search is bounded by the budget; the number of distributions in step 4 is exponential in the worst case (AC matching is NP-complete [Benanav, Kapur and Narendran 1987]), and the budget makes that explicit.

Theorem M-2 (soundness). Every assignment Algorithm M-1 returns is a match.

Proof. Step 6 checks the defining property \(N(\sigma p) = t\) directly, with the kernel's constructors. \(\square\)

Soundness therefore does not depend on steps 1 to 5 being right; they only decide which candidates are tried.

Theorem M-3 (completeness). For a first-order pattern without literals and without repeated pattern variables inside one ACU spine, Algorithm M-1, given enough budget, returns every match.

Proof. By induction on the pattern. Steps 1 to 3 are the usual first-order matching, complete for syntactic equality, which canonical forms reduce matching to outside ACU spines (the kernel's Theorem C-6: two terms are equal modulo the equations exactly when their canonical forms are identical). In an ACU spine, \(N(\sigma p) = t\) holds exactly when the multiset of atoms of the canonical form of \(\sigma p\) equals \(A\); each rigid pattern argument contributes the atoms of its instance, and each variable the atoms of its value; step 4 enumerates every assignment of rigid arguments to atoms and every distribution of the rest. Units correspond to empty sub-multisets. \(\square\)

Limitations. Patterns with a pattern variable in function position (higher-order patterns), and matches that need literal arithmetic (such as \(2 + x\) against \(5 + y\) with \(x = 3 + y\)), are not found. They are missed opportunities, never wrong matches.

Proposition M-4 (sorts). Let the pattern's type parameters carry sorts (the kernel's K2.8). Extend Algorithm M-1 with a type assignment: a constant instance or a pattern variable of the pattern meets the target's type, matching type parameters as first-order variables, and a type parameter \(\alpha^S\) accepts a type \(\tau\) only when \(\tau : S\) in the target's theory. Every returned match then gives an admissible type substitution, so the kernel's use accepts the instantiation.

Proof. The type assignment is checked at each binding against the same membership relation the kernel uses (its Definition Y-9), and matching verifies each match by construction (Theorem M-2). \(\square\)

Metavariables

Engines that search for proofs need metavariables: placeholders for terms still to be found, shared across goals (the architecture note, section 18.5).

Definition M-4 (metavariable store). A store holds metavariables \(?m\), each with a type and a context in which its value must be usable, and a partial assignment. A trail records each assignment so that it can be undone in reverse order.

Proposition M-5 (the store stays acyclic and scoped). If every assignment \(?m := t\) is accepted only when (a) \(t\) has \(?m\)'s type, (b) every local of \(t\) is available in \(?m\)'s context, and (c) \(?m\) does not occur in \(t\) after replacing every assigned metavariable in \(t\) by its value (the occurs check), then at every moment the assignment, viewed as a substitution, is idempotent after finitely many applications, and instantiating any term with it yields a term usable in the term's context. Undoing assignments by the trail restores every earlier state exactly.

Proof. (c) keeps the dependency graph of the assignment acyclic, so repeated application terminates; (a) and (b) give the typing and scope. The trail records, for each assignment, the metavariable it set, and undoing resets it; assignments are made and undone in stack order. \(\square\)

This is the architecture note's acceptance test A10: an assignment that would make a metavariable contain itself, or mention an unavailable local, is rejected and the trail is restored.

Metavariables are search state only. A recipe never contains one: before a recipe runs, every metavariable it mentions must be assigned, and the assignment is substituted (the kernel's terms contain no metavariables).

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