Hashing modulo alpha-equivalence

Krzysztof Maziarz, Tom Ellis, Alan Lawrence, Andrew W. Fitzgibbon, Simon Peyton Jones

Abstract

Identifying identical code fragments is a cornerstone of compiler optimization, yet standard syntactic hashing fails to account for $\alpha$-equivalence—the principle that variable names are arbitrary. We present a novel, asymptotically efficient algorithm ($O(n \log^2 n)$) that leverages "e-summaries" and commutative hash combiners to generate renaming-invariant hashes. Our approach demonstrates significant speedups over traditional $O(n^2)$ methods, particularly on large-scale abstract syntax trees (ASTs) found in modern machine learning models like BERT.

Introduction

In modern compiler design and static analysis, the ability to identify "identical" subtrees within an Abstract Syntax Tree (AST) is critical. This process enables optimizations such as Common Subexpression Elimination (CSE), where redundant computations are replaced by a single calculation, and structure sharing, which reduces memory overhead by reusing identical tree nodes.

However, a significant challenge arises from $\alpha$-equivalence. To a compiler, the functions \x. x + 7 and \y. y + 7 are semantically identical, differing only in the choice of variable names. A naive syntactic hash would treat these as distinct, missing optimization opportunities. While techniques like de Bruijn indexing attempt to solve this by replacing names with relative distances to binders, they often introduce complexities or performance bottlenecks when applied to large, isolated subexpressions.

The Alpha-Equivalence Hurdle

Existing robust methods, such as the "locally nameless" approach, typically suffer from $O(n^2)$ time complexity. While acceptable for small programs, this quadratic growth becomes a major bottleneck for the massive ASTs generated by modern machine learning frameworks or large-scale software systems. There is a clear need for an algorithm that is both correct regarding $\alpha$-equivalence and asymptotically efficient.

Proposed Methodology

Researchers at Microsoft Research have introduced a compositional approach to generate hashes invariant under variable renaming. The algorithm centers on the creation of an e-summary for every node in the AST.

The E-Summary representation: separating structural shape from variable occurrences.

The E-Summary Representation

As shown in the figure above, an e-summary consists of two primary components:

  1. Structure: A representation of the expression's shape where all variables are replaced by anonymous "holes."
  2. Free Variable Map (VarMap): A mapping that tracks the exact positions of each free variable within the subexpression.

Efficient Hashing with XOR

To convert these summaries into a compact hash, the authors employ a commutative hash combiner—specifically, XOR. While XOR is often avoided in cryptographic contexts due to its simplicity, the researchers provide a mathematical proof that it is "strong enough" for this application, maintaining exponentially low collision probabilities. This choice allows for extremely fast merging of variable maps.

Complexity Optimization

To achieve $O(n \log^2 n)$ complexity, the algorithm utilizes a "smaller-to-larger" merging strategy. When combining two variable maps at an application node, the algorithm iterates over the smaller map and inserts its elements into the larger one. This classic algorithmic trick prevents the quadratic cost associated with naive map merging.

Experimental Evaluation

The algorithm was tested against both synthetic lambda terms and real-world datasets, including:

  • Synthetic Terms: Balanced and unbalanced trees designed to test asymptotic limits.
  • Machine Learning Models: ASTs for MNIST CNNs, Gaussian Mixture Models (GMM), and the BERT Transformer model.

Key Results

The results highlight the asymptotic superiority of the e-summary approach. On deeply nested, unbalanced trees, the "locally nameless" baseline exhibited clear $O(n^2)$ growth, whereas the new algorithm followed its predicted $O(n \log^2 n)$ trajectory. For a BERT model with approximately 13,000 nodes, the proposed method was over 200 times faster than the baseline (3.6ms vs 820ms). Furthermore, the overhead compared to "incorrect" syntactic hashes was minimal (2-4x), making it a viable replacement in production compilers.

Conclusion and Future Work

By combining structural abstraction with efficient commutative hashing, this new algorithm provides a scalable solution for $\alpha$-equivalence in large-scale program analysis. While currently focused on lambda calculus, the principles of e-summaries are extensible to more complex language features. Future work may explore extending this framework to handle graph equivalence or more advanced semantic identities, further pushing the boundaries of automated code optimization.

Summary updated

BibTeX
@inproceedings{maziarz2021hashing,
  author = {Krzysztof Maziarz and Tom Ellis and Alan Lawrence and Andrew W. Fitzgibbon and Simon Peyton Jones},
  title = {{Hashing modulo alpha-equivalence}},
  booktitle = {PLDI},
  year = {2021},
  url = {https://openreview.net/forum?id=sCqY5SYZiH6},
}
Figure