Compiling index notation to sums of products: a verified functional pearl
Abstract
Index notation writes a repeated index name for a summation, and any implementation must turn it into explicit loops. The translation is a small functional program, yet it has a classic trap: reusing the name of a contracted index as the name of the loop variable captures free occurrences of that name elsewhere in a product. We present a compiler from a calculus of named indices to sums of products (the plans of an einsum), written in a few lines of Lean 4 and proved correct. Bound names come from a counter, so capture cannot happen; typing is erased, so the correctness proof does not use it; and the whole proof rests on three small lemmas about summation, renaming and fresh names. Composed with a verified inference of the contracted names, the compiler turns an unannotated expression such as A^i_j B^j_k into the plan "ac,cb->ab". The development is about 600 lines, uses only the Lean core library, and is executable.
The record contains the manuscript (PDF and LaTeX sources, in the format of the Journal of Functional Programming) and the Lean 4 artifact (tic_lean_v1.0.zip), which builds with "bash build.sh" using Lean 4.35.0.
Related
- A mechanized calculus for index notation with automatic contraction
- Model Counting as a Sum of Products: Verified Translation and Variable Elimination in Lean 4
- Home-based telerehabilitation after CABG: higher objectively verified adherence and 12-week functional outcomes in a single-center observational cohort
- The flag major index on conjugacy classes of wreath products
- Sn-equivariant L-classes of Cartesian powers of Witt spaces and the Hodge index conjecture for symmetric products
BibTeX
@misc{pavesi2026compiling,
title = {{Compiling index notation to sums of products: a verified functional pearl}},
author = {Luca Eliseo Pavesi},
howpublished = {Zenodo},
year = {2026},
month = oct,
doi = {10.5281/zenodo.23544752},
url = {https://zenodo.org/records/23544752},
note = {CC BY 4.0},
}
RIS
TY - UNPB
TI - Compiling index notation to sums of products: a verified functional pearl
AU - Luca Eliseo Pavesi
PY - 2026
DA - 2026/10/21
PB - Zenodo
DO - 10.5281/zenodo.23544752
UR - https://zenodo.org/records/23544752
AB - Index notation writes a repeated index name for a summation, and any implementation must turn it into explicit loops. The translation is a small functional program, yet it has a classic trap: reusing the name of a contracted index as the name of the loop variable captures free occurrences of that name elsewhere in a product. We present a compiler from a calculus of named indices to sums of products (the plans of an einsum), written in a few lines of Lean 4 and proved correct. Bound names come from a counter, so capture cannot happen; typing is erased, so the correctness proof does not use it; and the whole proof rests on three small lemmas about summation, renaming and fresh names. Composed with a verified inference of the contracted names, the compiler turns an unannotated expression such as A^i_j B^j_k into the plan "ac,cb->ab". The development is about 600 lines, uses only the Lean core library, and is executable. The record contains the manuscript (PDF and LaTeX sources, in the format of the Journal of Functional Programming) and the Lean 4 artifact (tic_lean_v1.0.zip), which builds with "bash build.sh" using Lean 4.35.0.
ER -