When a proof assistant user is stuck on a goal, the system has to find, among roughly two hundred thousand library theorems, the few whose statements fit. Structural matching treats each theorem's statement as an expression tree and compares trees directly. On Lean's Mathlib library it already beats semantic search by a wide margin. Its main weakness is that it judges every theorem in isolation. The library is not a bag of isolated trees. It is a graph of graphs: each theorem is a tree, and theorems are linked by which proofs use them on which goals. We propose to represent each candidate theorem by the tree fragments of its own statement and also by the fragments of the goals it has historically closed. We compute both through an explicit, cached feature map, so that retrieval over the whole library becomes a sparse matrix product rather than millions of pairwise tree comparisons.
The two-sentence version is this. A tree kernel compares two trees by counting the small sub-trees they share. If we write those shared-fragment counts out explicitly and average them along the library's usage graph, a theorem becomes findable from goals that look like the goals it solved, not only from goals that look like its statement. The bet is that this usage signal repairs the known failure of structural retrieval on surface variants such as n+1 versus n.succ, and that it does so cheaply enough to replace the coarse clustering stage of current pipelines.
Core Claim
Tree-Based Premise Selection for Lean4 parses Mathlib statements into expression trees and merges repeated subexpressions. It then filters 217,555 theorems with a Weisfeiler-Lehman kernel (a hashing scheme that summarizes each node's neighborhood) plus k-means clustering, and reranks with tree edit distance. It reports Recall@10 of 28.7% on 6,119 real (proof state, theorem) pairs (Test Set B), against 22.0% for Lean State Search and 13.6% for Moogle. Its scoring uses fixed heuristics, treats each statement independently, and is admittedly brittle to compiler-level unfolding.
Our claim is that a two-tier representation closes a measurable part of that gap. The micro tier is the explicit tree-fragment vector of an expression. The macro tier is the usage graph linking premises to the goals they closed in training proofs. The extra quality should come at lower cost than the clustering stage, because explicit fragment vectors with an inverted index replace the O(M²) pairwise structure.
Concretely, let $\psi(T)$ be the sparse vector of fragment counts of expression tree $T$. Let $N(P)$ be the set of goal trees that premise $P$ closed in training proofs, and let $\alpha \in [0,1]$ be a mixing weight. The premise is represented as
$$\phi(P) = \alpha\,\psi(T_P) + (1-\alpha)\,\frac{1}{|N(P)|}\sum_{g \in N(P)} \psi(T_g),$$
and a query goal $q$ is scored by the weighted inner product $\langle W\psi(T_q), \phi(P)\rangle$. Here $W$ is a diagonal reweighting with only a handful of learned parameters, so the score remains a valid positive semidefinite kernel.
How This Differs From the Closest Work
Graph-of-graphs models already pass messages between a micro and a macro level. GoGNN pools molecular graphs with GCN attention and then runs attention over drug-interaction graphs. N2GON uses two neural encoders with a PageRank-based constraint. SEAL/SAGE classifies subgraphs connected in a social macro-network. These papers motivate the two-tier design, since each reports gains over flat models. They also constrain it: all three compress each micro-graph into one learned vector, which is exactly the information bottleneck the research file flags. None has been applied to retrieval in formal mathematics.
Our micro tier is not learned pooling. It is an explicit fragment map with exact-match semantics, which is what strict formal syntax demands.
The method for computing that map comes from kernel complexity work. Mapping kernels for trees organizes tree kernels into a grid of fragment shape (path, tree, forest) by matching constraint. Its "Extract" algorithm caches unique substructures across a dataset, giving a 36x speedup on 442 glycan trees. The authors left memory behavior on large, less redundant databases uncharacterized. Mathlib, at 217k trees with heavy subexpression sharing, is a natural test of that open question. Fast Kernels for String and Tree Matching supplies the linear-time fragment enumeration for contiguous fragments. The convolution kernel of Collins and Duffy defines the subset-tree fragments we count.
The small learned weighting follows the spirit of HSP-GKN, which makes an explicit graph-kernel map learnable via prototypes. It operates on single flat graphs only, and we transplant the idea to a two-tier setting. GAWL shows that WL-based explicit alignment can keep linear cost and a PSD guarantee. We use it as a micro-tier alternative in an ablation.
Research Program
The first component is a fragment extraction table for Mathlib. We will parse the 217,555 theorems with the subexpression merging of the tree-based pipeline. We will then enumerate subset-tree fragments up to depth $d$ and contiguous path fragments, and hash them into a global table.
We will measure three things as functions of $d$: table size, the share of fragments appearing in only one theorem, and wall-clock build time. This directly answers whether Extract-style caching survives a large formal corpus. To counter the diagonal dominance that plagues deep tree kernels, where each tree mostly matches only itself, fragments are weighted by inverse document frequency and depth decay. The learned diagonal $W$ is restricted to one weight per fragment shape and depth bucket, about thirty parameters.
The second component is the usage graph. From Mathlib proofs, we record each exact-style premise application together with the goal it closed. This yields edges from premises to goal trees. Leakage is the central methodological risk. Every proof from which a Test Set B pair was extracted, and every proof of a test theorem, is removed before building $N(P)$. We will additionally report a stricter split by Mathlib file, so that usage neighbors never come from the same module as the query.
The third component is retrieval. Because $\phi(P)$ is an explicit sparse vector, the top candidates are found through an inverted index over fragments. The cost scales with the number of nonzero fragment entries touched, rather than with library size times tree size. Tree edit distance reranking from the existing pipeline is then applied to the top pool, unchanged. Recent fine-grained tree edit distance results suggest that this stage can be made faster, but we treat it as fixed.
Baselines
The do-nothing reference is the published tree-based pipeline from Tree-Based Premise Selection for Lean4, reported at Recall@10 of 28.7% on Test Set B. We will rerun it on our hardware and split to confirm we reproduce roughly that figure before comparing. All of our variants keep its tree edit distance stage, so differences come only from candidate generation and scoring. Beating it means a Recall@10 gain on Test Set B that holds across bootstrap resamples of the 6,119 pairs, and that persists under the strict file-level split.
The strongest semantic baselines are Moogle at 13.6% and Lean State Search at 22.0%, as reported in the same paper. We cite them, and also rerun Lean State Search if it is available.
The strongest learned competitor is the LeanHammer neural premise selector from Premise Selection for a Lean Hammer. That paper reports a 33.3% cumulative proof rate on Mathlib and solves 21% more goals than MePo or ReProver. We found no reported Recall@10 for it on Test Set B on Lacuna. We will therefore retrain or run it on our exact usage split, with the same training proofs we use to build $N(P)$, so that both methods see identical supervision.
The structural ablation that isolates the macro tier is $\alpha = 1$, which gives our fragment kernel with no usage propagation. A second ablation isolates the micro tier: we replace $\psi$ with GAWL-style WL alignment features. A third, GoGNN-style variant also replaces it, with a GNN over expression trees whose pooled outputs are averaged along the same usage edges. All variants get the same tuning budget of a fixed number of validation configurations, and the same CPU and GPU hours.
Why not just…?
Why not just add a premise-popularity prior to the existing tree pipeline? This is the most dangerous alternative. Usage counts alone might explain any gain, because frequently used lemmas are frequently correct. We include it as an explicit baseline: tree pipeline score plus log usage count, with the weight tuned on validation. If our method does not beat this prior, the claim that fragment content propagated along usage matters is false. In that case we would report the paper as a negative result about structure in the macro tier.
Why not just train a dense neural retriever on the same usage data? The LeanHammer results show that learned selectors are strong, so this is a real threat on Test Set B. Our expectation rests on the evidence that semantic embeddings miss strict syntactic fit: the 13.6% versus 28.7% gap in the tree-based paper. It also rests on the distillation plateau around 100,000 examples reported in Injecting Relational Structural Representation in Neural Networks for Question Similarity, where neural students only partially absorbed tree-kernel structure. The experiments settle this on the 100 structural-variation problems of Test Set A and on out-of-distribution miniCTX-v2 projects. There, a learned model has less familiar data to lean on, while our score needs no retraining. If the neural retriever wins everywhere, we will say so, and test whether adding $\phi$ as a feature still helps it.
Why not just encode expression trees with a GNN and propagate over the usage graph, GoGNN-style? This is a cleaner neural version of our own idea, and it is run as the third ablation above. GoGNN reports CCI950 AUC of 0.963, which shows that such models work on molecules. However, the pooled vector cannot represent exact fragment identity, and that is precisely what distinguishes compatible from incompatible Lean premises. If the GNN variant matches ours at equal compute, the explicit kernel offers only interpretability and no-training transfer, and we would state that plainly.
Proposed Experiments
The decisive check is a three-way comparison on Test Set B under the strict file-level split. The three systems are the full method, the $\alpha = 1$ ablation, and the popularity-prior baseline. A gain that survives the popularity prior is the evidence for the macro-tier claim. We will also break results down on the subset of Test Set B pairs where the goal and the premise statement differ by known unfoldings. The hypothesis predicts the largest gains there, and no gain on pairs that already match syntactically.
The complexity check reports four quantities against the k-means stage at its reported 2.13x speedup (about 14.5 seconds per query): extraction table size versus fragment depth, index build time, query latency, and Recall@10. Success is matching or exceeding recall at lower latency. Failure modes worth reporting are a table that grows superlinearly with depth, or a singleton fragment share high enough that caching saves nothing.
As a sanity check on our Extract reimplementation, we will rerun it on the KEGG glycan trees. It should recover the roughly 3,000 unique substructures reported by Mapping kernels for trees.
Finally, Test Set A and miniCTX-v2 test transfer. For miniCTX-v2, the usage graph is rebuilt from the new project's own proofs, with no relearning of $W$. For it we found no reported number to anchor against, so all comparisons there are internal reruns.
References
- Tree-Based Premise Selection for Lean4
- GoGNN: Graph of Graphs Neural Network for Predicting Structured Entity Interactions
- N2GON: Neural Networks for Graph-of-Net with Position Awareness
- Semi-Supervised Graph Classification: A Hierarchical Graph Perspective
- Mapping kernels for trees
- Fast Kernels for String and Tree Matching
- Convolution Kernels for Natural Language
- Hierarchical Shortest-Path Graph Kernel Network
- Graph Alignment Kernels using Weisfeiler and Leman Hierarchies
- Faster Weighted and Unweighted Tree Edit Distance and APSP Equivalence
- Premise Selection for a Lean Hammer
- Injecting Relational Structural Representation in Neural Networks for Question Similarity