Compiling index notation to sums of products: a verified functional pearl
Authors/Creators
Description
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.
Files
pavesi_jfp_pearl_index_notation.pdf
Additional details
Software
- Repository URL
- https://zenodo.org/records/235335885
- Programming language
- Lean
- Development Status
- Active